handle.js
This file
Path
lib/stdio/handle.js
105 lines · 4.8 KiB · JavaScript
Last changed by
Add passing the same transform multiple times (#697)ehmickybefore adoption35ec8de
History of this fileTake it
Refactor `stdio` option logic (#639)ehmickybefore adoptiond6b4553
lines 1–2why, one step back1import {getStdioOptionType, isRegularUrl, isUnknownStdioString} from './type.js';2import {addStreamDirection} from './direction.js';Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297
line 3why, one step back3import {normalizeStdio} from './normalize.js';Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
line 4why, one step back4import {handleNativeStream} from './native.js';Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25
line 5why, one step back5import {handleInputOptions} from './input.js';Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2
line 6why, one step back6import {handleStreamsEncoding} from './encoding.js';Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297
lines 7–8why, one step back78// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in async/sync modeFix `encoding` option (#696)ehmickybefore adoptionb6f9fa2
line 9why, one step back9export const handleInput = (addProperties, options, isSync) => {Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297
line 10why, one step back10 const stdio = normalizeStdio(options);Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25
lines 11–13why, one step back11 const [stdinStreams, ...otherStreamsGroups] = stdio.map((stdioOption, index) => getStdioStreams(stdioOption, index));12 const stdioStreamsGroups = [[...stdinStreams, ...handleInputOptions(options)], ...otherStreamsGroups]13 .map(stdioStreams => validateStreams(stdioStreams))Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb
line 14why, one step back14 .map(stdioStreams => addStreamDirection(stdioStreams))Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2
lines 15–16why, one step back15 .map(stdioStreams => handleStreamsEncoding(stdioStreams, options, isSync))16 .map(stdioStreams => addStreamsProperties(stdioStreams, addProperties));Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb
line 17why, one step back17 options.stdio = transformStdio(stdioStreamsGroups);Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3
line 18why, one step back18 return stdioStreamsGroups;Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297
lines 19–20why, one step back19};20Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
lines 21–23why, one step back21// We make sure passing an array with a single item behaves the same as passing that item without an array.22// This is what users would expect.23// For example, `stdout: ['ignore']` behaves the same as `stdout: 'ignore'`.Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25
line 24why, one step back24const getStdioStreams = (stdioOption, index) => {Refactor `stdio` option logic (#639)ehmickybefore adoptiond6b4553
line 25why, one step back25 const optionName = getOptionName(index);Add passing the same transform multiple times (#697)ehmickybefore adoption35ec8de
lines 26–31why, one step back26 const stdioOptions = Array.isArray(stdioOption) ? stdioOption : [stdioOption];27 const rawStdioStreams = stdioOptions.map(stdioOption => getStdioStream(stdioOption, optionName, index));28 const stdioStreams = filterDuplicates(rawStdioStreams);29 const isStdioArray = stdioStreams.length > 1;30 validateStdioArray(stdioStreams, isStdioArray, optionName);31 return stdioStreams.map(stdioStream => handleNativeStream(stdioStream, isStdioArray));Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb
line 32why, one step back32};Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
line 33why, one step back33Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb
lines 34–35why, one step back34const getOptionName = index => KNOWN_OPTION_NAMES[index] ?? `stdio[${index}]`;35const KNOWN_OPTION_NAMES = ['stdin', 'stdout', 'stderr'];Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
line 36why, one step back36Add passing the same transform multiple times (#697)ehmickybefore adoption35ec8de
lines 37–47why, one step back37const getStdioStream = (stdioOption, optionName, index) => {38 const type = getStdioOptionType(stdioOption, optionName);39 return {type, value: stdioOption, optionName, index};40};4142const filterDuplicates = stdioStreams => stdioStreams.filter((stdioStreamOne, indexOne) =>43 stdioStreams.every((stdioStreamTwo, indexTwo) =>44 stdioStreamOne.value !== stdioStreamTwo.value || indexOne >= indexTwo || stdioStreamOne.type === 'generator'));4546const validateStdioArray = (stdioStreams, isStdioArray, optionName) => {47 if (stdioStreams.length === 0) {Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
lines 48–50why, one step back48 throw new TypeError(`The \`${optionName}\` option must not be an empty array.`);49 }50Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb
lines 51–52why, one step back51 if (!isStdioArray) {52 return;Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
lines 53–54why, one step back53 }54Add passing the same transform multiple times (#697)ehmickybefore adoption35ec8de
lines 55–57why, one step back55 for (const {value, optionName} of stdioStreams) {56 if (INVALID_STDIO_ARRAY_OPTIONS.has(value)) {57 throw new Error(`The \`${optionName}\` option must not include \`${value}\`.`);Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
lines 58–63why, one step back58 }59 }60};6162// Using those `stdio` values together with others for the same stream does not make sense, so we make it fail.63// However, we do allow it if the array has a single item.Add passing the same transform multiple times (#697)ehmickybefore adoption35ec8de
line 64why, one step back64const INVALID_STDIO_ARRAY_OPTIONS = new Set(['ignore', 'ipc']);Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297
line 65why, one step back65Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25
lines 66–69why, one step back66const validateStreams = stdioStreams => {67 for (const stdioStream of stdioStreams) {68 validateFileStdio(stdioStream);69 }Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297
line 70why, one step back70Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25
line 71why, one step back71 return stdioStreams;Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297
lines 72–80why, one step back72};7374const validateFileStdio = ({type, value, optionName}) => {75 if (isRegularUrl(value)) {76 throw new TypeError(`The \`${optionName}: URL\` option must use the \`file:\` scheme.77For example, you can use the \`pathToFileURL()\` method of the \`url\` core module.`);78 }7980 if (isUnknownStdioString(type, value)) {Improve syntax of `std*: filePath` option (#671)ehmickybefore adoption90e0e13
line 81why, one step back81 throw new TypeError(`The \`${optionName}: { file: '...' }\` option must be used instead of \`${optionName}: '...'\`.`);Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297
lines 82–83why, one step back82 }83};Refactor `stdio` option logic (#639)ehmickybefore adoptiond6b4553
lines 84–87why, one step back8485// Some `stdio` values require Execa to create streams.86// For example, file paths create file read/write streams.87// Those transformations are specified in `addProperties`, which is both direction-specific and type-specific.Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2
line 88why, one step back88const addStreamsProperties = (stdioStreams, addProperties) => stdioStreams.map(stdioStream => ({Refactor `stdio` option logic (#639)ehmickybefore adoptiond6b4553
line 89why, one step back89 ...stdioStream,Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2
line 90why, one step back90 ...addProperties[stdioStream.direction][stdioStream.type]?.(stdioStream),Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb
line 91why, one step back91}));Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297
lines 92–93why, one step back9293// When the `std*: Iterable | WebStream | URL | filePath`, `input` or `inputFile` option is used, we pipe to `spawned.std*`.Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
line 94why, one step back94// 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*`.Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297
line 95why, one step back95// Therefore the `std*` options must be either `pipe` or `overlapped`. Other values do not set `spawned.std*`.Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb
line 96why, one step back96const transformStdio = stdioStreamsGroups => stdioStreamsGroups.map(stdioStreams => transformStdioItem(stdioStreams));Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297
line 97why, one step back97Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb
lines 98–100why, one step back98const transformStdioItem = stdioStreams => {99 if (stdioStreams.length > 1) {100 return stdioStreams.some(({value}) => value === 'overlapped') ? 'overlapped' : 'pipe';Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
lines 101–102why, one step back101 }102Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb
line 103why, one step back103 const [stdioStream] = stdioStreams;Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
lines 104–105why, one step back104 return stdioStream.type !== 'native' && stdioStream.value !== 'overlapped' ? 'pipe' : stdioStream.value;105};