specific.js
This file
Path
lib/arguments/specific.js
132 lines · 4.7 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.
Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199
line 1why, one step back1import {debuglog} from 'node:util';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 2–3why, one step back2import isPlainObject from 'is-plain-obj';3import {STANDARD_STREAMS_ALIASES} from '../utils/standard-stream.js';Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
line 4why, one step back4import {checkBooleanOption} from '../utils/options.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 5–9why, one step back56// Some options can have different values for `stdout`/`stderr`/`fd3`.7// This normalizes those to array of values.8// For example, `{verbose: {stdout: 'none', stderr: 'full'}}` becomes `{verbose: ['none', 'none', 'full']}`9export const normalizeFdSpecificOptions = options => {Harden some thingsSindre Sorhusbefore adoption4a0a121
lines 10–11why, one step back10 // Keep the null prototype, otherwise a polluted `Object.prototype` would inject option values11 const optionsCopy = {__proto__: null, ...options};100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 12–20why, one step back1213 for (const optionName of FD_SPECIFIC_OPTIONS) {14 optionsCopy[optionName] = normalizeFdSpecificOption(options, optionName);15 }1617 return optionsCopy;18};1920export const normalizeFdSpecificOption = (options, optionName) => {Fix fd-specific options to account for `ipc` (#1241)Sindre Sorhusbefore adoptionb886883
lines 21–23why, one step back21 const stdioLength = getStdioLength(options);22 const optionBaseArray = Array.from({length: stdioLength + 1});23 const optionArray = normalizeFdSpecificValue(options[optionName], optionBaseArray, optionName, stdioLength);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 24–30why, one step back24 return addDefaultValue(optionArray, optionName);25};2627const getStdioLength = ({stdio}) => Array.isArray(stdio)28 ? Math.max(stdio.length, STANDARD_STREAMS_ALIASES.length)29 : STANDARD_STREAMS_ALIASES.length;30Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
lines 31–34why, one step back31const normalizeFdSpecificValue = (optionValue, optionArray, optionName, stdioLength) => {32 if (isPlainObject(optionValue)) {33 return normalizeOptionObject(optionValue, optionArray, optionName, stdioLength);34 }100% test coverage (#1018)ehmickybefore adoption9f65b86
line 35why, one step back35Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
lines 36–39why, one step back36 validateFdSpecificValue(optionValue, optionName);37 return optionArray.fill(optionValue);38};39Fix fd-specific options to account for `ipc` (#1241)Sindre Sorhusbefore adoptionb886883
line 40why, one step back40const normalizeOptionObject = (optionValue, optionArray, optionName, stdioLength) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
line 41why, one step back41 for (const fdName of Object.keys(optionValue).sort(compareFdName)) {Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
line 42why, one step back42 validateFdSpecificValue(optionValue[fdName], optionName, `${optionName}.${fdName}`);Fix fd-specific options to account for `ipc` (#1241)Sindre Sorhusbefore adoptionb886883
line 43why, one step back43 for (const fdNumber of parseFdName(fdName, optionName, stdioLength)) {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 44–61why, one step back44 optionArray[fdNumber] = optionValue[fdName];45 }46 }4748 return optionArray;49};5051// Ensure priority order when setting both `stdout`/`stderr`, `fd1`/`fd2`, and `all`52const compareFdName = (fdNameA, fdNameB) => getFdNameOrder(fdNameA) < getFdNameOrder(fdNameB) ? 1 : -1;5354const getFdNameOrder = fdName => {55 if (fdName === 'stdout' || fdName === 'stderr') {56 return 0;57 }5859 return fdName === 'all' ? 2 : 1;60};61Fix fd-specific options to account for `ipc` (#1241)Sindre Sorhusbefore adoptionb886883
line 62why, one step back62const parseFdName = (fdName, optionName, stdioLength) => {Log IPC messages with `verbose: 'full'` (#1063)ehmickybefore adoption839c2fe
line 63why, one step back63 if (fdName === 'ipc') {Fix fd-specific options to account for `ipc` (#1241)Sindre Sorhusbefore adoptionb886883
line 64why, one step back64 return [stdioLength];Log IPC messages with `verbose: 'full'` (#1063)ehmickybefore adoption839c2fe
lines 65–66why, one step back65 }66100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 67–69why, one step back67 const fdNumber = parseFd(fdName);68 if (fdNumber === undefined || fdNumber === 0) {69 throw new TypeError(`"${optionName}.${fdName}" is invalid.Log IPC messages with `verbose: 'full'` (#1063)ehmickybefore adoption839c2fe
line 70why, one step back70It must be "${optionName}.stdout", "${optionName}.stderr", "${optionName}.all", "${optionName}.ipc", or "${optionName}.fd3", "${optionName}.fd4" (and so on).`);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 71–72why, one step back71 }72Fix fd-specific options to account for `ipc` (#1241)Sindre Sorhusbefore adoptionb886883
line 73why, one step back73 if (fdNumber !== 'all' && fdNumber >= stdioLength) {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 74–92why, one step back74 throw new TypeError(`"${optionName}.${fdName}" is invalid: that file descriptor does not exist.75Please set the "stdio" option to ensure that file descriptor exists.`);76 }7778 return fdNumber === 'all' ? [1, 2] : [fdNumber];79};8081// Use the same syntax for fd-specific options and the `from`/`to` options82export const parseFd = fdName => {83 if (fdName === 'all') {84 return fdName;85 }8687 if (STANDARD_STREAMS_ALIASES.includes(fdName)) {88 return STANDARD_STREAMS_ALIASES.indexOf(fdName);89 }9091 const regexpResult = FD_REGEXP.exec(fdName);92 if (regexpResult !== null) {Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454
line 93why, one step back93 return Number(regexpResult.groups.fdNumber);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 94–96why, one step back94 }95};96Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454
line 97why, one step back97const FD_REGEXP = /^fd(?<fdNumber>\d+)$/;100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 98–101why, one step back9899const addDefaultValue = (optionArray, optionName) => optionArray.map(optionValue => optionValue === undefined100 ? DEFAULT_OPTIONS[optionName]101 : optionValue);Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
lines 102–113why, one step back102103const BOOLEAN_FD_SPECIFIC_OPTIONS = new Set(['lines', 'buffer', 'stripFinalNewline']);104105// Wrong types must be reported instead of being silently coerced, e.g. a truthy string enabling the `buffer` option.106// `undefined` uses the default value.107const validateFdSpecificValue = (optionValue, optionName, fullOptionName = optionName) => {108 if (BOOLEAN_FD_SPECIFIC_OPTIONS.has(optionName)) {109 checkBooleanOption(optionValue, fullOptionName);110 } else if (optionName === 'maxBuffer' && optionValue !== undefined && (typeof optionValue !== 'number' || Number.isNaN(optionValue))) {111 throw new TypeError(`The \`${fullOptionName}\` option must use a number.`);112 }113};100% test coverage (#1018)ehmickybefore adoption9f65b86
line 114why, one step back114Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199
lines 115–117why, one step back115// Default value for the `verbose` option116const verboseDefault = debuglog('execa').enabled ? 'full' : 'none';117100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 118–127why, one step back118const DEFAULT_OPTIONS = {119 lines: false,120 buffer: true,121 maxBuffer: 1000 * 1000 * 100,122 verbose: verboseDefault,123 stripFinalNewline: true,124};125126// List of options which can have different values for `stdout`/`stderr`127export const FD_SPECIFIC_OPTIONS = ['lines', 'buffer', 'maxBuffer', 'verbose', 'stripFinalNewline'];Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199
lines 128–132why, one step back128129// Retrieve fd-specific option130export const getFdSpecificValue = (optionArray, fdNumber) => fdNumber === 'ipc'131 ? optionArray.at(-1)132 : optionArray[fdNumber];