Skip to content
Myr

handle.js

This file

Path

lib/stdio/handle.js

214 lines · 7.2 KiB · JavaScript

Last changed by

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

History of this file

Take it

The walk stopped after 200 commits: lines older than db95b51 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';

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51

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

Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51

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

lines 19–20why, one step back
19export const handleStdio = (addProperties, options, verboseInfo, isSync) => {
20 const stdio = normalizeStdioOption(options, 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 });

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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;

Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51

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

Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51

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}) => {

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 63–72why, one step back
63 const values = Array.isArray(stdioOption) ? stdioOption : [stdioOption];
64 const initialStdioItems = [65 ...values.map(value => initializeStdioItem(value, optionName)),66 ...handleInputOptions(options, fdNumber),67 ];6869 const stdioItems = filterDuplicates(initialStdioItems);70 const isStdioArray = stdioItems.length > 1;71 validateStdioArray(stdioItems, isStdioArray, optionName);72 validateStreams(stdioItems);

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

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

Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51

lines 74–75why, one step back
74};
75

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 76–80why, one step back
76const initializeStdioItem = (value, optionName) => ({
77 type: getStdioItemType(value, optionName),78 value,79 optionName,80});

Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51

line 81why, one step back
81

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 82–83why, one step back
82const validateStdioArray = (stdioItems, isStdioArray, optionName) => {
83 if (stdioItems.length === 0) {

Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51

lines 84–90why, one step back
84 throw new TypeError(`The \`${optionName}\` option must not be an empty array.`);
85 }8687 if (!isStdioArray) {88 return;89 }90

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 91why, one step back
91 for (const {value, optionName} of stdioItems) {

Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51

lines 92–101why, one step back
92 if (INVALID_STDIO_ARRAY_OPTIONS.has(value)) {
93 throw new Error(`The \`${optionName}\` option must not include \`${value}\`.`);94 }95 }96};9798// Using those `stdio` values together with others for the same stream does not make sense, so we make it fail.99// However, we do allow it if the array has a single item.100const INVALID_STDIO_ARRAY_OPTIONS = new Set(['ignore', 'ipc']);101

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 102–104why, one step back
102const validateStreams = stdioItems => {
103 for (const stdioItem of stdioItems) {104 validateFileStdio(stdioItem);

Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51

lines 105–118why, one step back
105 }
106};107108const validateFileStdio = ({type, value, optionName}) => {109 if (isRegularUrl(value)) {110 throw new TypeError(`The \`${optionName}: URL\` option must use the \`file:\` scheme.111For example, you can use the \`pathToFileURL()\` method of the \`url\` core module.`);112 }113114 if (isUnknownStdioString(type, value)) {115 throw new TypeError(`The \`${optionName}: { file: '...' }\` option must be used instead of \`${optionName}: '...'\`.`);116 }117};118

Improve `verbose` option (#962)ehmickybefore adoption58beaa6

lines 119–127why, one step back
119const validateFileObjectMode = (stdioItems, objectMode) => {
120 if (!objectMode) {121 return;122 }123124 const fileStdioItem = stdioItems.find(({type}) => FILE_TYPES.has(type));125 if (fileStdioItem !== undefined) {126 throw new TypeError(`The \`${fileStdioItem.optionName}\` option cannot use both files and transforms in objectMode.`);127 }

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 128–129why, one step back
128};
129

Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51

lines 130–132why, one step back
130// Some `stdio` values require Execa to create streams.
131// For example, file paths create file read/write streams.132// 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 133–202why, one step back
133const getFinalFileDescriptors = ({initialFileDescriptors, addProperties, options, isSync}) => {
134 const fileDescriptors = [];135136 try {137 for (const fileDescriptor of initialFileDescriptors) {138 fileDescriptors.push(getFinalFileDescriptor({139 fileDescriptor,140 fileDescriptors,141 addProperties,142 options,143 isSync,144 }));145 }146147 return fileDescriptors;148 } catch (error) {149 cleanupCustomStreams(fileDescriptors);150 throw error;151 }152};153154const getFinalFileDescriptor = ({155 fileDescriptor: {direction, objectMode, stdioItems},156 fileDescriptors,157 addProperties,158 options,159 isSync,160}) => {161 const finalStdioItems = stdioItems.map(stdioItem => addStreamProperties({162 stdioItem,163 addProperties,164 direction,165 options,166 fileDescriptors,167 isSync,168 }));169 return {direction, objectMode, stdioItems: finalStdioItems};170};171172const addStreamProperties = ({stdioItem, addProperties, direction, options, fileDescriptors, isSync}) => {173 const duplicateStream = getDuplicateStream({174 stdioItem,175 direction,176 fileDescriptors,177 isSync,178 });179180 if (duplicateStream !== undefined) {181 return {...stdioItem, stream: duplicateStream};182 }183184 return {185 ...stdioItem,186 ...addProperties[direction][stdioItem.type](stdioItem, options),187 };188};189190// The stream error handling is performed by the piping logic above, which cannot be performed before subprocess spawning.191// If the subprocess spawning fails (e.g. due to an invalid command), the streams need to be manually destroyed.192// We need to create those streams before subprocess spawning, in case their creation fails, e.g. when passing an invalid generator as argument.193// Like this, an exception would be thrown, which would prevent spawning a subprocess.194export const cleanupCustomStreams = fileDescriptors => {195 for (const {stdioItems} of fileDescriptors) {196 for (const {stream} of stdioItems) {197 if (stream !== undefined && !isStandardStream(stream)) {198 stream.destroy();199 }200 }201 }202};

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

line 203why, one step back
203

Improve `verbose` option (#962)ehmickybefore adoption58beaa6

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

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

lines 210–211why, one step back
210 }
211

Improve `verbose` option (#962)ehmickybefore adoption58beaa6

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

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

line 214why, one step back
214};