Skip to content
Myr

native.js

This file

Path

lib/stdio/native.js

46 lines · 1.9 KiB · JavaScript

Last changed by

Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40

History of this file

Take it

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

line 1why, one step back
1import {isStream as isNodeStream} from 'is-stream';

Split files (#837)ehmickybefore adoption672c2d7

line 2why, one step back
2import {STANDARD_STREAMS} from '../utils.js';

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

lines 3–8why, one step back
3
4// When we use multiple `stdio` values for the same streams, we pass 'pipe' to `child_process.spawn()`.5// We then emulate the piping done by core Node.js.6// To do so, we transform the following values:7// - Node.js streams are marked as `type: nodeStream`8// - 'inherit' becomes `process.stdin|stdout|stderr`

Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40

line 9why, one step back
9// - any file descriptor integer becomes `process.stdio[fdNumber]`

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

lines 10–11why, one step back
10// All of the above transformations tell Execa to perform manual piping.
11export const handleNativeStream = (stdioStream, isStdioArray) => {

Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40

line 12why, one step back
12 const {type, value, fdNumber, optionName} = stdioStream;

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

lines 13–18why, one step back
13
14 if (!isStdioArray || type !== 'native') {15 return stdioStream;16 }1718 if (value === 'inherit') {

Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40

line 19why, one step back
19 return {...stdioStream, type: 'nodeStream', value: getStandardStream(fdNumber, value, optionName)};

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

lines 20–25why, one step back
20 }
2122 if (typeof value === 'number') {23 return {...stdioStream, type: 'nodeStream', value: getStandardStream(value, value, optionName)};24 }25

Upgrade `is-stream` to `4.0.1` (#835)ehmickybefore adoption9cd62aa

line 26why, one step back
26 if (isNodeStream(value, {checkOpen: false})) {

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

lines 27–37why, one step back
27 return {...stdioStream, type: 'nodeStream'};
28 }2930 return stdioStream;31};3233// Node.js does not allow to easily retrieve file descriptors beyond stdin/stdout/stderr as streams.34// - `fs.createReadStream()`/`fs.createWriteStream()` with the `fd` option do not work with character devices that use blocking reads/writes (such as interactive TTYs).35// - Using a TCP `Socket` would work but be rather complex to implement.36// Since this is an edge case, we simply throw an error message.37// See https://github.com/sindresorhus/execa/pull/643#discussion_r1435905707

Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40

lines 38–39why, one step back
38const getStandardStream = (fdNumber, value, optionName) => {
39 const standardStream = STANDARD_STREAMS[fdNumber];

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

lines 40–46why, one step back
40
41 if (standardStream === undefined) {42 throw new TypeError(`The \`${optionName}: ${value}\` option is invalid: no such standard stream.`);43 }4445 return standardStream;46};