Skip to content
Myr

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 file

Take it

Refactor `stdio` option logic (#639)ehmickybefore adoptiond6b4553

lines 1–2why, one step back
1import {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 back
3import {normalizeStdio} from './normalize.js';

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

line 4why, one step back
4import {handleNativeStream} from './native.js';

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

line 5why, one step back
5import {handleInputOptions} from './input.js';

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

line 6why, one step back
6import {handleStreamsEncoding} from './encoding.js';

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 7–8why, one step back
7
8// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in async/sync mode

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

line 9why, one step back
9export const handleInput = (addProperties, options, isSync) => {

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

line 10why, one step back
10 const stdio = normalizeStdio(options);

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 11–13why, one step back
11 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 back
14 .map(stdioStreams => addStreamDirection(stdioStreams))

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

lines 15–16why, one step back
15 .map(stdioStreams => handleStreamsEncoding(stdioStreams, options, isSync))
16 .map(stdioStreams => addStreamsProperties(stdioStreams, addProperties));

Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb

line 17why, one step back
17 options.stdio = transformStdio(stdioStreamsGroups);

Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3

line 18why, one step back
18 return stdioStreamsGroups;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 19–20why, one step back
19};
20

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

lines 21–23why, one step back
21// 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 back
24const getStdioStreams = (stdioOption, index) => {

Refactor `stdio` option logic (#639)ehmickybefore adoptiond6b4553

line 25why, one step back
25 const optionName = getOptionName(index);

Add passing the same transform multiple times (#697)ehmickybefore adoption35ec8de

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

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

line 33why, one step back
33

Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb

lines 34–35why, one step back
34const 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 back
36

Add passing the same transform multiple times (#697)ehmickybefore adoption35ec8de

lines 37–47why, one step back
37const 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 back
48 throw new TypeError(`The \`${optionName}\` option must not be an empty array.`);
49 }50

Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb

lines 51–52why, one step back
51 if (!isStdioArray) {
52 return;

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

lines 53–54why, one step back
53 }
54

Add passing the same transform multiple times (#697)ehmickybefore adoption35ec8de

lines 55–57why, one step back
55 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 back
58 }
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 back
64const INVALID_STDIO_ARRAY_OPTIONS = new Set(['ignore', 'ipc']);

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

line 65why, one step back
65

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 66–69why, one step back
66const 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 back
70

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

line 71why, one step back
71 return stdioStreams;

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 72–80why, one step back
72};
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 back
81 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 back
82 }
83};

Refactor `stdio` option logic (#639)ehmickybefore adoptiond6b4553

lines 84–87why, one step back
84
85// 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 back
88const addStreamsProperties = (stdioStreams, addProperties) => stdioStreams.map(stdioStream => ({

Refactor `stdio` option logic (#639)ehmickybefore adoptiond6b4553

line 89why, one step back
89 ...stdioStream,

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

line 90why, one step back
90 ...addProperties[stdioStream.direction][stdioStream.type]?.(stdioStream),

Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb

line 91why, one step back
91}));

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

lines 92–93why, one step back
92
93// 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 back
94// 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 back
95// 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 back
96const transformStdio = stdioStreamsGroups => stdioStreamsGroups.map(stdioStreams => transformStdioItem(stdioStreams));

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

line 97why, one step back
97

Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb

lines 98–100why, one step back
98const 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 back
101 }
102

Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb

line 103why, one step back
103 const [stdioStream] = stdioStreams;

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

lines 104–105why, one step back
104 return stdioStream.type !== 'native' && stdioStream.value !== 'overlapped' ? 'pipe' : stdioStream.value;
105};