stdio-option.js
This file
Path
lib/stdio/stdio-option.js
91 lines · 4.2 KiB · JavaScript
Last changed by
Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
100% test coverage (#1018)ehmickybefore adoption9f65b86
line 1why, one step back1import {STANDARD_STREAMS_ALIASES} from '../utils/standard-stream.js';Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 2why, one step back2import {normalizeIpcStdioArray} from '../ipc/array.js';Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348
line 3why, one step back3import {isFullVerbose} from '../verbose/values.js';Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
line 4why, one step back4import {getTypeName} from '../utils/type-name.js';Generalize `{value, input}` to any direction-ambiguous stdio value (#1249)Sindre Sorhusbefore adoption9fbf5ba
line 5why, one step back5import {isStdioValueObject} from './type.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 6–8why, one step back67// Add support for `stdin`/`stdout`/`stderr` as an alias for `stdio`.8// Also normalize the `stdio` option.Harden some thingsSindre Sorhusbefore adoption4a0a121
lines 9–11why, one step back9// A rest pattern is not used to read the other options, since the object it creates inherits from `Object.prototype`, which a prototype pollution could then use to inject option values10export const normalizeStdioOption = (options, verboseInfo, isSync) => {11 const {stdio, ipc, buffer} = options;100% test coverage (#1018)ehmickybefore adoption9f65b86
line 12why, one step back12 const stdioArray = getStdioArray(stdio, options).map((stdioOption, fdNumber) => addDefaultValue(stdioOption, fdNumber));Remove the old `stdio: [..., 'ipc']` syntax (#1245)Sindre Sorhusbefore adoptiondcf611c
line 13why, one step back13 validateIpcStdioOption(stdioArray);Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 14why, one step back14 return isSyncRefactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199
line 15why, one step back15 ? normalizeStdioSync(stdioArray, buffer, verboseInfo)Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 16why, one step back16 : normalizeIpcStdioArray(stdioArray, ipc);Remove the old `stdio: [..., 'ipc']` syntax (#1245)Sindre Sorhusbefore adoptiondcf611c
lines 17–20why, one step back17};1819// The `stdio: 'ipc'` value (raw `child_process` syntax) was replaced by the `ipc: true` option20const validateIpcStdioOption = stdioArray => {Generalize `{value, input}` to any direction-ambiguous stdio value (#1249)Sindre Sorhusbefore adoption9fbf5ba
line 21why, one step back21 if (stdioArray.some(stdioOption => hasIpcStdioOption(stdioOption))) {Remove the old `stdio: [..., 'ipc']` syntax (#1245)Sindre Sorhusbefore adoptiondcf611c
lines 22–23why, one step back22 throw new Error('The `ipc: true` option must be used instead of `stdio: \'ipc\'`.');23 }100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 24–25why, one step back24};25TweaksSindre Sorhusbefore adoption698a1b8
line 26why, one step back26const hasIpcStdioOption = stdioOption => Array.isArray(stdioOption) ? stdioOption.some(item => hasIpcStdioItem(item)) : hasIpcStdioItem(stdioOption);Generalize `{value, input}` to any direction-ambiguous stdio value (#1249)Sindre Sorhusbefore adoption9fbf5ba
line 27why, one step back27TweaksSindre Sorhusbefore adoption698a1b8
line 28why, one step back28const hasIpcStdioItem = stdioOption => isStdioValueObject(stdioOption) ? stdioOption.value === 'ipc' : stdioOption === 'ipc';Generalize `{value, input}` to any direction-ambiguous stdio value (#1249)Sindre Sorhusbefore adoption9fbf5ba
line 29why, one step back29100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 30–43why, one step back30const getStdioArray = (stdio, options) => {31 if (stdio === undefined) {32 return STANDARD_STREAMS_ALIASES.map(alias => options[alias]);33 }3435 if (hasAlias(options)) {36 throw new Error(`It's not possible to provide \`stdio\` in combination with one of ${STANDARD_STREAMS_ALIASES.map(alias => `\`${alias}\``).join(', ')}`);37 }3839 if (typeof stdio === 'string') {40 return [stdio, stdio, stdio];41 }4243 if (!Array.isArray(stdio)) {Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
line 44why, one step back44 throw new TypeError(`Expected \`stdio\` to be of type \`string\` or \`Array\`, got \`${getTypeName(stdio)}\``);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 45–66why, one step back45 }4647 const length = Math.max(stdio.length, STANDARD_STREAMS_ALIASES.length);48 return Array.from({length}, (_, fdNumber) => stdio[fdNumber]);49};5051const hasAlias = options => STANDARD_STREAMS_ALIASES.some(alias => options[alias] !== undefined);5253const addDefaultValue = (stdioOption, fdNumber) => {54 if (Array.isArray(stdioOption)) {55 return stdioOption.map(item => addDefaultValue(item, fdNumber));56 }5758 if (stdioOption === null || stdioOption === undefined) {59 return fdNumber >= STANDARD_STREAMS_ALIASES.length ? 'ignore' : 'pipe';60 }6162 return stdioOption;63};6465// Using `buffer: false` with synchronous methods implies `stdout`/`stderr`: `ignore`.66// Unless the output is needed, e.g. due to `verbose: 'full'` or to redirecting to a file.Fix `result.all` and `subprocess.all` when only some file descriptors are bufferedSindre Sorhusbefore adoption758768f
lines 67–68why, one step back67// This is only done for `stdout`/`stderr`, since `ignore` redirects them to `/dev/null`.68// With additional file descriptors, it does not pass them to the subprocess at all, which would make it fail when writing to them.Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199
line 69why, one step back69const normalizeStdioSync = (stdioArray, buffer, verboseInfo) => stdioArray.map((stdioOption, fdNumber) =>100% test coverage (#1018)ehmickybefore adoption9f65b86
line 70why, one step back70 !buffer[fdNumber]Fix `result.all` and `subprocess.all` when only some file descriptors are bufferedSindre Sorhusbefore adoption758768f
line 71why, one step back71 && isStandardOutputFd(fdNumber)Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199
line 72why, one step back72 && !isFullVerbose(verboseInfo, fdNumber)Add `{value: 'pipe', input: true}` to use an additional file descriptor as input (#1246)Sindre Sorhusbefore adoption487a3a4
line 73why, one step back73 && isOutputPipeOnly(stdioOption, fdNumber)100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 74–76why, one step back74 ? 'ignore'75 : stdioOption);76Fix invalid `stdio` values being acceptedSindre Sorhusbefore adoptionc6bd387
line 77why, one step back77// An empty array is not output pipe only: it must keep being rejected as an empty array belowAdd `{value: 'pipe', input: true}` to use an additional file descriptor as input (#1246)Sindre Sorhusbefore adoption487a3a4
line 78why, one step back78const isOutputPipeOnly = (stdioOption, fdNumber) => isOutputPipe(stdioOption, fdNumber)Fix invalid `stdio` values being acceptedSindre Sorhusbefore adoptionc6bd387
line 79why, one step back79 || (Array.isArray(stdioOption) && stdioOption.length > 0 && stdioOption.every(item => isOutputPipe(item, fdNumber)));Add `{value: 'pipe', input: true}` to use an additional file descriptor as input (#1246)Sindre Sorhusbefore adoption487a3a4
lines 80–83why, one step back8081const isOutputPipe = (stdioOption, fdNumber) => stdioOption === 'pipe'82 || isOutputPipeObject(stdioOption, fdNumber);83Generalize `{value, input}` to any direction-ambiguous stdio value (#1249)Sindre Sorhusbefore adoption9fbf5ba
line 84why, one step back84const isOutputPipeObject = (stdioOption, fdNumber) => isStdioValueObject(stdioOption)Add `{value: 'pipe', input: true}` to use an additional file descriptor as input (#1246)Sindre Sorhusbefore adoption487a3a4
lines 85–88why, one step back85 && stdioOption.value === 'pipe'86 && (stdioOption.input === undefined || stdioOption.input === false || isFixedOutputPipe(fdNumber, stdioOption.input));8788// On `stdout`/`stderr`, the direction is always output, so `input: true` is ignored and the pipe stays output.Fix `result.all` and `subprocess.all` when only some file descriptors are bufferedSindre Sorhusbefore adoption758768f
lines 89–91why, one step back89const isFixedOutputPipe = (fdNumber, input) => input === true && isStandardOutputFd(fdNumber);9091const isStandardOutputFd = fdNumber => fdNumber === 1 || fdNumber === 2;