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 fileTake 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 back1import {getStreamName, isStandardStream} from '../utils/standard-stream.js';Rename files (#977)ehmickybefore adoption4b2e316
lines 2–3why, one step back2import {normalizeTransforms} from '../transform/normalize.js';3import {getFdObjectMode} from '../transform/object-mode.js';Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 4–9why, one step back4import {5 getStdioItemType,6 isRegularUrl,7 isUnknownStdioString,8 FILE_TYPES,9} from './type.js';Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 10why, one step back10import {getStreamDirection} from './direction.js';Rename files (#977)ehmickybefore adoption4b2e316
line 11why, one step back11import {normalizeStdioOption} from './stdio-option.js';Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51
line 12why, one step back12import {handleNativeStream} from './native.js';Rename files (#977)ehmickybefore adoption4b2e316
line 13why, one step back13import {handleInputOptions} from './input-option.js';Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7
line 14why, one step back14import {filterDuplicates, getDuplicateStream} from './duplicate.js';Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51
lines 15–16why, one step back1516// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in async/sync modeAdd more code comments (#984)ehmickybefore adoptionfdd8a9f
lines 17–18why, one step back17// 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 back19export 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 back21 const initialFileDescriptors = stdio.map((stdioOption, fdNumber) => getFileDescriptor({Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 22–26why, one step back22 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 back27 const fileDescriptors = getFinalFileDescriptors({28 initialFileDescriptors,29 addProperties,30 options,31 isSync,32 });Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 33why, one step back33 options.stdio = fileDescriptors.map(({stdioItems}) => forwardStdio(stdioItems));Improve `verbose` option (#962)ehmickybefore adoption58beaa6
line 34why, one step back34 return fileDescriptors;Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51
lines 35–36why, one step back35};36Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7
line 37why, one step back37const getFileDescriptor = ({stdioOption, fdNumber, options, isSync}) => {Allow setting different `maxBuffer` values for `stdout`/`stderr` (#966)ehmickybefore adoptionfdffcb6
line 38why, one step back38 const optionName = getStreamName(fdNumber);Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 39–44why, one step back39 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 back45 const direction = getStreamDirection(initialStdioItems, fdNumber, optionName);Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 46–52why, one step back46 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 back53 const normalizedStdioItems = normalizeTransforms(stdioItems, optionName, direction, options);Split files into smaller ones (#976)ehmickybefore adoptiond7c6853
line 54why, one step back54 const objectMode = getFdObjectMode(normalizedStdioItems, direction);Improve `verbose` option (#962)ehmickybefore adoption58beaa6
line 55why, one step back55 validateFileObjectMode(normalizedStdioItems, objectMode);Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7
line 56why, one step back56 return {direction, objectMode, stdioItems: normalizedStdioItems};Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51
lines 57–58why, one step back57};58Add more code comments (#984)ehmickybefore adoptionfdd8a9f
lines 59–61why, one step back59// 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 back62const initializeStdioItems = ({stdioOption, fdNumber, options, optionName}) => {Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 63–72why, one step back63 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 back73 return {stdioItems, isStdioArray};Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51
lines 74–75why, one step back74};75Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 76–80why, one step back76const 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 back81Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 82–83why, one step back82const validateStdioArray = (stdioItems, isStdioArray, optionName) => {83 if (stdioItems.length === 0) {Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51
lines 84–90why, one step back84 throw new TypeError(`The \`${optionName}\` option must not be an empty array.`);85 }8687 if (!isStdioArray) {88 return;89 }90Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 91why, one step back91 for (const {value, optionName} of stdioItems) {Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51
lines 92–101why, one step back92 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']);101Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 102–104why, one step back102const validateStreams = stdioItems => {103 for (const stdioItem of stdioItems) {104 validateFileStdio(stdioItem);Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51
lines 105–118why, one step back105 }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};118Improve `verbose` option (#962)ehmickybefore adoption58beaa6
lines 119–127why, one step back119const 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 back128};129Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51
lines 130–132why, one step back130// 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 back133const 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 back203Improve `verbose` option (#962)ehmickybefore adoption58beaa6
lines 204–209why, one step back204// 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 back210 }211Improve `verbose` option (#962)ehmickybefore adoption58beaa6
lines 212–213why, one step back212 const [{type, value}] = stdioItems;213 return type === 'native' ? value : 'pipe';Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
line 214why, one step back214};