Skip to content
Myr

native.js

This file

Path

lib/stdio/native.js

48 lines · 1.9 KiB · JavaScript

Last changed by

Fix Node.js streams not being cleaned up on process error (#660)ehmickybefore adoption61a1623

History of this file

Take it

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

lines 1–47why, one step back
1import process from 'node:process';
2import {isStream as isNodeStream} from 'is-stream';34// 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`9// - any file descriptor integer becomes `process.stdio[index]`10// All of the above transformations tell Execa to perform manual piping.11export const handleNativeStream = (stdioStream, isStdioArray) => {12 const {type, value, index, optionName} = stdioStream;1314 if (!isStdioArray || type !== 'native') {15 return stdioStream;16 }1718 if (value === 'inherit') {19 return {...stdioStream, type: 'nodeStream', value: getStandardStream(index, value, optionName)};20 }2122 if (typeof value === 'number') {23 return {...stdioStream, type: 'nodeStream', value: getStandardStream(value, value, optionName)};24 }2526 if (isNodeStream(value)) {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_r143590570738const getStandardStream = (index, value, optionName) => {39 const standardStream = STANDARD_STREAMS[index];4041 if (standardStream === undefined) {42 throw new TypeError(`The \`${optionName}: ${value}\` option is invalid: no such standard stream.`);43 }4445 return standardStream;46};47

Fix Node.js streams not being cleaned up on process error (#660)ehmickybefore adoption61a1623

line 48why, one step back
48export const STANDARD_STREAMS = [process.stdin, process.stdout, process.stderr];