Skip to content
Myr

native.js

This file

Path

lib/stdio/native.js

46 lines · 1.9 KiB · JavaScript

Last changed by

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

line 10why, one step back
10// All of the above transformations tell Execa to perform manual piping.

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 11–12why, one step back
11export const handleNativeStream = (stdioItem, isStdioArray, fdNumber) => {
12 const {type, value, optionName} = stdioItem;

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

lines 13–14why, one step back
13
14 if (!isStdioArray || type !== 'native') {

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 15why, one step back
15 return stdioItem;

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

lines 16–18why, one step back
16 }
1718 if (value === 'inherit') {

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

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

lines 20–22why, one step back
20 }
2122 if (typeof value === 'number') {

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 23why, one step back
23 return {type: 'nodeStream', value: getStandardStream(value, value, optionName), optionName};

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

lines 24–25why, one step back
24 }
25

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

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 27why, one step back
27 return {type: 'nodeStream', value, optionName};

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

lines 28–29why, one step back
28 }
29

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 30why, one step back
30 return stdioItem;

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

lines 31–37why, one step back
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};