Skip to content
Myr

handle.js

This file

Path

lib/stdio/handle.js

107 lines · 5.0 KiB · JavaScript

Last changed by

Refactor `encoding` option (#735)ehmickybefore adoption1907e7c

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';

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 7why, one step back
7import {normalizeGenerators} from './generator.js';

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

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

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

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

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

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

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

lines 12–14why, one step back
12 const [stdinStreams, ...otherStreamsGroups] = stdio.map((stdioOption, index) => getStdioStreams(stdioOption, index));
13 const stdioStreamsGroups = [[...stdinStreams, ...handleInputOptions(options)], ...otherStreamsGroups]14 .map(stdioStreams => validateStreams(stdioStreams))

Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb

line 15why, one step back
15 .map(stdioStreams => addStreamDirection(stdioStreams))

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

line 16why, one step back
16 .map(stdioStreams => handleStreamsEncoding(stdioStreams, options, isSync))

Refactor `encoding` option (#735)ehmickybefore adoption1907e7c

line 17why, one step back
17 .map(stdioStreams => normalizeGenerators(stdioStreams))

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

line 18why, one step back
18 .map(stdioStreams => addStreamsProperties(stdioStreams, addProperties));

Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb

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

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

line 20why, one step back
20 return stdioStreamsGroups;

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

lines 21–22why, one step back
21};
22

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

lines 23–25why, one step back
23// We make sure passing an array with a single item behaves the same as passing that item without an array.
24// This is what users would expect.25// For example, `stdout: ['ignore']` behaves the same as `stdout: 'ignore'`.

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

line 26why, one step back
26const getStdioStreams = (stdioOption, index) => {

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

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

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

lines 28–33why, one step back
28 const stdioOptions = Array.isArray(stdioOption) ? stdioOption : [stdioOption];
29 const rawStdioStreams = stdioOptions.map(stdioOption => getStdioStream(stdioOption, optionName, index));30 const stdioStreams = filterDuplicates(rawStdioStreams);31 const isStdioArray = stdioStreams.length > 1;32 validateStdioArray(stdioStreams, isStdioArray, optionName);33 return stdioStreams.map(stdioStream => handleNativeStream(stdioStream, isStdioArray));

Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb

line 34why, one step back
34};

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

line 35why, one step back
35

Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb

lines 36–37why, one step back
36const getOptionName = index => KNOWN_OPTION_NAMES[index] ?? `stdio[${index}]`;
37const KNOWN_OPTION_NAMES = ['stdin', 'stdout', 'stderr'];

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

line 38why, one step back
38

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

lines 39–49why, one step back
39const getStdioStream = (stdioOption, optionName, index) => {
40 const type = getStdioOptionType(stdioOption, optionName);41 return {type, value: stdioOption, optionName, index};42};4344const filterDuplicates = stdioStreams => stdioStreams.filter((stdioStreamOne, indexOne) =>45 stdioStreams.every((stdioStreamTwo, indexTwo) =>46 stdioStreamOne.value !== stdioStreamTwo.value || indexOne >= indexTwo || stdioStreamOne.type === 'generator'));4748const validateStdioArray = (stdioStreams, isStdioArray, optionName) => {49 if (stdioStreams.length === 0) {

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

lines 50–52why, one step back
50 throw new TypeError(`The \`${optionName}\` option must not be an empty array.`);
51 }52

Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb

lines 53–54why, one step back
53 if (!isStdioArray) {
54 return;

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

lines 55–56why, one step back
55 }
56

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

lines 57–59why, one step back
57 for (const {value, optionName} of stdioStreams) {
58 if (INVALID_STDIO_ARRAY_OPTIONS.has(value)) {59 throw new Error(`The \`${optionName}\` option must not include \`${value}\`.`);

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

lines 60–65why, one step back
60 }
61 }62};6364// Using those `stdio` values together with others for the same stream does not make sense, so we make it fail.65// However, we do allow it if the array has a single item.

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

line 66why, one step back
66const INVALID_STDIO_ARRAY_OPTIONS = new Set(['ignore', 'ipc']);

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

line 67why, one step back
67

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

lines 68–71why, one step back
68const validateStreams = stdioStreams => {
69 for (const stdioStream of stdioStreams) {70 validateFileStdio(stdioStream);71 }

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

line 72why, one step back
72

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

line 73why, one step back
73 return stdioStreams;

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

lines 74–82why, one step back
74};
7576const validateFileStdio = ({type, value, optionName}) => {77 if (isRegularUrl(value)) {78 throw new TypeError(`The \`${optionName}: URL\` option must use the \`file:\` scheme.79For example, you can use the \`pathToFileURL()\` method of the \`url\` core module.`);80 }8182 if (isUnknownStdioString(type, value)) {

Improve syntax of `std*: filePath` option (#671)ehmickybefore adoption90e0e13

line 83why, one step back
83 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 84–85why, one step back
84 }
85};

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

lines 86–89why, one step back
86
87// Some `stdio` values require Execa to create streams.88// For example, file paths create file read/write streams.89// Those transformations are specified in `addProperties`, which is both direction-specific and type-specific.

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

line 90why, one step back
90const addStreamsProperties = (stdioStreams, addProperties) => stdioStreams.map(stdioStream => ({

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

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

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

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

Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb

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

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

lines 94–95why, one step back
94
95// 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 96why, one step back
96// 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 97why, one step back
97// Therefore the `std*` options must be either `pipe` or `overlapped`. Other values do not set `spawned.std*`.

Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb

line 98why, one step back
98const transformStdio = stdioStreamsGroups => stdioStreamsGroups.map(stdioStreams => transformStdioItem(stdioStreams));

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

line 99why, one step back
99

Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb

lines 100–102why, one step back
100const transformStdioItem = stdioStreams => {
101 if (stdioStreams.length > 1) {102 return stdioStreams.some(({value}) => value === 'overlapped') ? 'overlapped' : 'pipe';

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

lines 103–104why, one step back
103 }
104

Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb

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

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

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