Skip to content
Myr

handle.js

This file

Path

lib/stdio/handle.js

223 lines · 7.6 KiB · JavaScript

Last changed by

Remove the old `stdio: [..., 'ipc']` syntax (#1245)Sindre Sorhusbefore adoptiondcf611c

History of this file

Take it

The walk stopped after 200 commits: lines older than cc565f9 point at it.

Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7

line 1why, one step back
1import {getStreamName, isStandardStream} from '../utils/standard-stream.js';

Rename files (#977)ehmickybefore adoption4b2e316

lines 2–3why, one step back
2import {normalizeTransforms} from '../transform/normalize.js';
3import {getFdObjectMode} from '../transform/object-mode.js';

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 4–9why, one step back
4import {
5 getStdioItemType,6 isRegularUrl,7 isUnknownStdioString,8 FILE_TYPES,9} from './type.js';

Improve validation of `execaSync()` `stdio` option (#941)ehmickybefore adoptioncc565f9

line 10why, one step back
10import {getStreamDirection} from './direction.js';

Rename files (#977)ehmickybefore adoption4b2e316

line 11why, one step back
11import {normalizeStdioOption} from './stdio-option.js';

Improve validation of `execaSync()` `stdio` option (#941)ehmickybefore adoptioncc565f9

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

Rename files (#977)ehmickybefore adoption4b2e316

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

Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7

line 14why, one step back
14import {filterDuplicates, getDuplicateStream} from './duplicate.js';

Improve validation of `execaSync()` `stdio` option (#941)ehmickybefore adoptioncc565f9

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

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

lines 17–18why, one step back
17// They are converted into an array of `fileDescriptors`.
18// Each `fileDescriptor` is normalized, validated and contains all information necessary for further handling.

Rename files (#977)ehmickybefore adoption4b2e316

line 19why, one step back
19export const handleStdio = (addProperties, options, verboseInfo, isSync) => {

Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199

line 20why, one step back
20 const stdio = normalizeStdioOption(options, verboseInfo, isSync);

Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7

line 21why, one step back
21 const initialFileDescriptors = stdio.map((stdioOption, fdNumber) => getFileDescriptor({

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 22–26why, one step back
22 stdioOption,
23 fdNumber,24 options,25 isSync,26 }));

Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7

lines 27–32why, one step back
27 const fileDescriptors = getFinalFileDescriptors({
28 initialFileDescriptors,29 addProperties,30 options,31 isSync,32 });

Improve validation of `execaSync()` `stdio` option (#941)ehmickybefore adoptioncc565f9

line 33why, one step back
33 options.stdio = fileDescriptors.map(({stdioItems}) => forwardStdio(stdioItems));

Improve `verbose` option (#962)ehmickybefore adoption58beaa6

line 34why, one step back
34 return fileDescriptors;

Improve validation of `execaSync()` `stdio` option (#941)ehmickybefore adoptioncc565f9

lines 35–36why, one step back
35};
36

Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7

line 37why, one step back
37const getFileDescriptor = ({stdioOption, fdNumber, options, isSync}) => {

Allow setting different `maxBuffer` values for `stdout`/`stderr` (#966)ehmickybefore adoptionfdffcb6

line 38why, one step back
38 const optionName = getStreamName(fdNumber);

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 39–44why, one step back
39 const {stdioItems: initialStdioItems, isStdioArray} = initializeStdioItems({
40 stdioOption,41 fdNumber,42 options,43 optionName,44 });

Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc

line 45why, one step back
45 const direction = getStreamDirection(initialStdioItems, fdNumber, optionName);

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 46–52why, one step back
46 const stdioItems = initialStdioItems.map(stdioItem => handleNativeStream({
47 stdioItem,48 isStdioArray,49 fdNumber,50 direction,51 isSync,52 }));

Improve `verbose` option (#962)ehmickybefore adoption58beaa6

line 53why, one step back
53 const normalizedStdioItems = normalizeTransforms(stdioItems, optionName, direction, options);

Split files into smaller ones (#976)ehmickybefore adoptiond7c6853

line 54why, one step back
54 const objectMode = getFdObjectMode(normalizedStdioItems, direction);

Improve `verbose` option (#962)ehmickybefore adoption58beaa6

line 55why, one step back
55 validateFileObjectMode(normalizedStdioItems, objectMode);

Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7

line 56why, one step back
56 return {direction, objectMode, stdioItems: normalizedStdioItems};

Improve validation of `execaSync()` `stdio` option (#941)ehmickybefore adoptioncc565f9

lines 57–58why, one step back
57};
58

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

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

Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc

line 62why, one step back
62const initializeStdioItems = ({stdioOption, fdNumber, options, optionName}) => {

Improve validation of `execaSync()` `stdio` option (#941)ehmickybefore adoptioncc565f9

line 63why, one step back
63 const values = Array.isArray(stdioOption) ? stdioOption : [stdioOption];

Fix input with inherited stdin (#1232)Puneet Dixitbefore adoption3ed0544

line 64why, one step back
64 const inputStdioItems = handleInputOptions(options, fdNumber);

Improve validation of `execaSync()` `stdio` option (#941)ehmickybefore adoptioncc565f9

line 65why, one step back
65 const initialStdioItems = [

Fix input with inherited stdin (#1232)Puneet Dixitbefore adoption3ed0544

lines 66–67why, one step back
66 ...omitInheritedStdin(values.map(value => initializeStdioItem(value, optionName)), inputStdioItems),
67 ...inputStdioItems,

Improve validation of `execaSync()` `stdio` option (#941)ehmickybefore adoptioncc565f9

lines 68–73why, one step back
68 ];
6970 const stdioItems = filterDuplicates(initialStdioItems);71 const isStdioArray = stdioItems.length > 1;72 validateStdioArray(stdioItems, isStdioArray, optionName);73 validateStreams(stdioItems);

Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc

line 74why, one step back
74 return {stdioItems, isStdioArray};

Improve validation of `execaSync()` `stdio` option (#941)ehmickybefore adoptioncc565f9

line 75why, one step back
75};

Fix input with inherited stdin (#1232)Puneet Dixitbefore adoption3ed0544

lines 76–83why, one step back
76
77const omitInheritedStdin = (stdioItems, inputStdioItems) => inputStdioItems.length > 0 && isInheritedStdinOnly(stdioItems)78 ? []79 : stdioItems;8081const isInheritedStdinOnly = stdioItems => stdioItems.length === 182 && stdioItems[0].type === 'native'83 && stdioItems[0].value === 'inherit';

Improve validation of `execaSync()` `stdio` option (#941)ehmickybefore adoptioncc565f9

lines 84–106why, one step back
84
85const initializeStdioItem = (value, optionName) => ({86 type: getStdioItemType(value, optionName),87 value,88 optionName,89});9091const validateStdioArray = (stdioItems, isStdioArray, optionName) => {92 if (stdioItems.length === 0) {93 throw new TypeError(`The \`${optionName}\` option must not be an empty array.`);94 }9596 if (!isStdioArray) {97 return;98 }99100 for (const {value, optionName} of stdioItems) {101 if (INVALID_STDIO_ARRAY_OPTIONS.has(value)) {102 throw new Error(`The \`${optionName}\` option must not include \`${value}\`.`);103 }104 }105};106

Remove the old `stdio: [..., 'ipc']` syntax (#1245)Sindre Sorhusbefore adoptiondcf611c

line 107why, one step back
107// Using the `ignore` value together with others for the same stream does not make sense, so we make it fail.

Improve validation of `execaSync()` `stdio` option (#941)ehmickybefore adoptioncc565f9

line 108why, one step back
108// However, we do allow it if the array has a single item.

Remove the old `stdio: [..., 'ipc']` syntax (#1245)Sindre Sorhusbefore adoptiondcf611c

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

Improve validation of `execaSync()` `stdio` option (#941)ehmickybefore adoptioncc565f9

lines 110–127why, one step back
110
111const validateStreams = stdioItems => {112 for (const stdioItem of stdioItems) {113 validateFileStdio(stdioItem);114 }115};116117const validateFileStdio = ({type, value, optionName}) => {118 if (isRegularUrl(value)) {119 throw new TypeError(`The \`${optionName}: URL\` option must use the \`file:\` scheme.120For example, you can use the \`pathToFileURL()\` method of the \`url\` core module.`);121 }122123 if (isUnknownStdioString(type, value)) {124 throw new TypeError(`The \`${optionName}: { file: '...' }\` option must be used instead of \`${optionName}: '...'\`.`);125 }126};127

Improve `verbose` option (#962)ehmickybefore adoption58beaa6

lines 128–136why, one step back
128const validateFileObjectMode = (stdioItems, objectMode) => {
129 if (!objectMode) {130 return;131 }132133 const fileStdioItem = stdioItems.find(({type}) => FILE_TYPES.has(type));134 if (fileStdioItem !== undefined) {135 throw new TypeError(`The \`${fileStdioItem.optionName}\` option cannot use both files and transforms in objectMode.`);136 }

Improve validation of `execaSync()` `stdio` option (#941)ehmickybefore adoptioncc565f9

lines 137–141why, one step back
137};
138139// Some `stdio` values require Execa to create streams.140// For example, file paths create file read/write streams.141// Those transformations are specified in `addProperties`, which is both direction-specific and type-specific.

Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7

lines 142–211why, one step back
142const getFinalFileDescriptors = ({initialFileDescriptors, addProperties, options, isSync}) => {
143 const fileDescriptors = [];144145 try {146 for (const fileDescriptor of initialFileDescriptors) {147 fileDescriptors.push(getFinalFileDescriptor({148 fileDescriptor,149 fileDescriptors,150 addProperties,151 options,152 isSync,153 }));154 }155156 return fileDescriptors;157 } catch (error) {158 cleanupCustomStreams(fileDescriptors);159 throw error;160 }161};162163const getFinalFileDescriptor = ({164 fileDescriptor: {direction, objectMode, stdioItems},165 fileDescriptors,166 addProperties,167 options,168 isSync,169}) => {170 const finalStdioItems = stdioItems.map(stdioItem => addStreamProperties({171 stdioItem,172 addProperties,173 direction,174 options,175 fileDescriptors,176 isSync,177 }));178 return {direction, objectMode, stdioItems: finalStdioItems};179};180181const addStreamProperties = ({stdioItem, addProperties, direction, options, fileDescriptors, isSync}) => {182 const duplicateStream = getDuplicateStream({183 stdioItem,184 direction,185 fileDescriptors,186 isSync,187 });188189 if (duplicateStream !== undefined) {190 return {...stdioItem, stream: duplicateStream};191 }192193 return {194 ...stdioItem,195 ...addProperties[direction][stdioItem.type](stdioItem, options),196 };197};198199// The stream error handling is performed by the piping logic above, which cannot be performed before subprocess spawning.200// If the subprocess spawning fails (e.g. due to an invalid command), the streams need to be manually destroyed.201// We need to create those streams before subprocess spawning, in case their creation fails, e.g. when passing an invalid generator as argument.202// Like this, an exception would be thrown, which would prevent spawning a subprocess.203export const cleanupCustomStreams = fileDescriptors => {204 for (const {stdioItems} of fileDescriptors) {205 for (const {stream} of stdioItems) {206 if (stream !== undefined && !isStandardStream(stream)) {207 stream.destroy();208 }209 }210 }211};

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

line 212why, one step back
212

Improve `verbose` option (#962)ehmickybefore adoption58beaa6

lines 213–218why, one step back
213// When the `std*: Iterable | WebStream | URL | filePath`, `input` or `inputFile` option is used, we pipe to `subprocess.std*`.
214// 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 `subprocess.std*`.215// Therefore the `std*` options must be either `pipe` or `overlapped`. Other values do not set `subprocess.std*`.216const forwardStdio = stdioItems => {217 if (stdioItems.length > 1) {218 return stdioItems.some(({value}) => value === 'overlapped') ? 'overlapped' : 'pipe';

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 219–220why, one step back
219 }
220

Improve `verbose` option (#962)ehmickybefore adoption58beaa6

lines 221–222why, one step back
221 const [{type, value}] = stdioItems;
222 return type === 'native' ? value : 'pipe';

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

line 223why, one step back
223};