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 fileTake it
Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
line 1why, one step back1import {isStream as isNodeStream} from 'is-stream';Split files (#837)ehmickybefore adoption672c2d7
line 2why, one step back2import {STANDARD_STREAMS} from '../utils.js';Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
lines 3–8why, one step back34// 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 back9// - any file descriptor integer becomes `process.stdio[fdNumber]`Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
line 10why, one step back10// All of the above transformations tell Execa to perform manual piping.Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 11–12why, one step back11export 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 back1314 if (!isStdioArray || type !== 'native') {Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 15why, one step back15 return stdioItem;Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
lines 16–18why, one step back16 }1718 if (value === 'inherit') {Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 19why, one step back19 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 back20 }2122 if (typeof value === 'number') {Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 23why, one step back23 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 back24 }25Upgrade `is-stream` to `4.0.1` (#835)ehmickybefore adoption9cd62aa
line 26why, one step back26 if (isNodeStream(value, {checkOpen: false})) {Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 27why, one step back27 return {type: 'nodeStream', value, optionName};Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
lines 28–29why, one step back28 }29Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 30why, one step back30 return stdioItem;Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
lines 31–37why, one step back31};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_r1435905707Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40
lines 38–39why, one step back38const 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 back4041 if (standardStream === undefined) {42 throw new TypeError(`The \`${optionName}: ${value}\` option is invalid: no such standard stream.`);43 }4445 return standardStream;46};