Simplify `stdio` logic (#663)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 710ecd5 · Browse files · 2 files·+29 added−47 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+29 added−47 removed
- other:+29 added−47 removed
Provenance
- Commit
66d0efb- Tree
05d3435- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
710ecd5
2 files changed+29 added−47 removed
modified:lib/stdio/direction.js+4 added-13 removed
8 unmodified lines9101112131415131415171819202122232416261718192030212223242537 unmodified lines8 unmodified lines// For `stdio[index]` beyond stdin/stdout/stderr, we need to guess whether the value passed is intended for inputs or outputs.// This allows us to know whether to pipe _into_ or _from_ the stream.// When `stdio[index]` is a single value, this guess is fairly straightforward.// However, when it is an array instead, we also need to make sure the different values are not incompatible with each other.export const addStreamDirection = stdioStream => Array.isArray(stdioStream) ? addStreamArrayDirection(stdioStream) : addStreamSingleDirection(stdioStream);export const addStreamDirection = stdioStreams => { const directions = stdioStreams.map(stdioStream => getStreamDirection(stdioStream));
const addStreamSingleDirection = stdioStream => { const direction = getStreamDirection(stdioStream); return addDirection(stdioStream, direction);};
const addStreamArrayDirection = stdioStream => { const directions = stdioStream.map(stdioStreamItem => getStreamDirection(stdioStreamItem));
if (directions.includes('input') && directions.includes('output')) { throw new TypeError(`The \`${stdioStream[0].optionName}\` option must not be an array of both readable and writable values.`); throw new TypeError(`The \`${stdioStreams[0].optionName}\` option must not be an array of both readable and writable values.`); }
const direction = directions.find(Boolean); return stdioStream.map(stdioStreamItem => addDirection(stdioStreamItem, direction)); return stdioStreams.map(stdioStream => addDirection(stdioStream, direction));};
const getStreamDirection = stdioStream => KNOWN_DIRECTIONS[stdioStream.index] ?? guessStreamDirection[stdioStream.type](stdioStream.value);
37 unmodified linesmodified:lib/stdio/handle.js+25 added-34 removed
5 unmodified lines6789101011121314151213141516171819202122232324252627282526272930313229333435363334353738394038394041424344454641484243444546474849505158525354555618 unmodified lines7576777885868788897980819282838485869787889910010189909192939495965 unmodified lines
// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in async/sync modeexport const handleInput = (addProperties, options) => { const stdio = normalizeStdio(options); const stdioStreams = stdio const stdioStreamsGroups = stdio .map((stdioOption, index) => getStdioStreams(stdioOption, index, options)) .map(stdioStream => addStreamDirection(stdioStream)) .map(stdioStream => addStreamsProperties(stdioStream, addProperties)); options.stdio = transformStdio(stdioStreams); return stdioStreams.flat(); .map(stdioStreams => addStreamDirection(stdioStreams)) .map(stdioStreams => addStreamsProperties(stdioStreams, addProperties)); options.stdio = transformStdio(stdioStreamsGroups); return stdioStreamsGroups.flat();};
// We make sure passing an array with a single item behaves the same as passing that item without an array.// This is what users would expect.// For example, `stdout: ['ignore']` behaves the same as `stdout: 'ignore'`.const getStdioStreams = (stdioOption, index, options) => { const optionName = getOptionName(index); const stdioParameters = {...options, optionName, index}; const stdioOptions = Array.isArray(stdioOption) ? [...new Set(stdioOption)] : [stdioOption]; const isStdioArray = stdioOptions.length > 1; validateStdioArray(stdioOptions, isStdioArray, optionName); return stdioOptions.map(stdioOption => getStdioStream({stdioOption, optionName, index, isStdioArray, options}));};
if (!Array.isArray(stdioOption)) { return getStdioStream(stdioOption, false, stdioParameters); }const getOptionName = index => KNOWN_OPTION_NAMES[index] ?? `stdio[${index}]`;const KNOWN_OPTION_NAMES = ['stdin', 'stdout', 'stderr'];
const validateStdioArray = (stdioOptions, isStdioArray, optionName) => { if (stdioOption.length === 0) { if (stdioOptions.length === 0) { throw new TypeError(`The \`${optionName}\` option must not be an empty array.`); }
const stdioOptionArray = [...new Set(stdioOption)]; if (stdioOptionArray.length === 1) { return getStdioStream(stdioOptionArray[0], false, stdioParameters); if (!isStdioArray) { return; }
validateStdioArray(stdioOptionArray, optionName);
return stdioOptionArray.map(stdioOptionItem => getStdioStream(stdioOptionItem, true, stdioParameters));};
const getOptionName = index => KNOWN_OPTION_NAMES[index] ?? `stdio[${index}]`;const KNOWN_OPTION_NAMES = ['stdin', 'stdout', 'stderr'];
const validateStdioArray = (stdioOptionArray, optionName) => { for (const invalidStdioOption of INVALID_STDIO_ARRAY_OPTIONS) { if (stdioOptionArray.includes(invalidStdioOption)) { if (stdioOptions.includes(invalidStdioOption)) { throw new Error(`The \`${optionName}\` option must not include \`${invalidStdioOption}\`.`); } }};
// Using those `stdio` values together with others for the same stream does not make sense, so we make it fail.// However, we do allow it if the array has a single item.const INVALID_STDIO_ARRAY_OPTIONS = ['ignore', 'ipc'];
const getStdioStream = (stdioOption, isStdioArray, {optionName, index, input, inputFile}) => {const getStdioStream = ({stdioOption, optionName, index, isStdioArray, options: {input, inputFile}}) => { const type = getStdioOptionType(stdioOption); let stdioStream = {type, value: stdioOption, optionName, index};
stdioStream = handleInputOption(stdioStream, input);18 unmodified lines
// Some `stdio` values require Execa to create streams.// For example, file paths create file read/write streams.// Those transformations are specified in `addProperties`, which is both direction-specific and type-specific.const addStreamsProperties = (stdioStream, addProperties) => Array.isArray(stdioStream) ? stdioStream.map(stdioStreamItem => addStreamProperties(stdioStreamItem, addProperties)) : addStreamProperties(stdioStream, addProperties);
const addStreamProperties = (stdioStream, addProperties) => ({const addStreamsProperties = (stdioStreams, addProperties) => stdioStreams.map(stdioStream => ({ ...stdioStream, ...addProperties[stdioStream.direction][stdioStream.type]?.(stdioStream),});}));
// When the `std*: Iterable | WebStream | URL | filePath`, `input` or `inputFile` option is used, we pipe to `spawned.std*`.// When the `std*: Array` option is used, we emulate some of the native values ('inherit', Node.js stream and file descriptor integer). To do so, we also need to pipe to `spawned.std*`.// Therefore the `std*` options must be either `pipe` or `overlapped`. Other values do not set `spawned.std*`.const transformStdio = stdioStreams => stdioStreams.map(stdioStream => transformStdioItem(stdioStream));const transformStdio = stdioStreamsGroups => stdioStreamsGroups.map(stdioStreams => transformStdioItem(stdioStreams));
const transformStdioItem = stdioStream => { if (Array.isArray(stdioStream)) { return stdioStream.some(({value}) => value === 'overlapped') ? 'overlapped' : 'pipe';const transformStdioItem = stdioStreams => { if (stdioStreams.length > 1) { return stdioStreams.some(({value}) => value === 'overlapped') ? 'overlapped' : 'pipe'; }
const [stdioStream] = stdioStreams; return stdioStream.type !== 'native' && stdioStream.value !== 'overlapped' ? 'pipe' : stdioStream.value;};