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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133import {debuglog} from 'node:util';import isPlainObject from 'is-plain-obj';import {STANDARD_STREAMS_ALIASES} from '../utils/standard-stream.js';import {checkBooleanOption} from '../utils/options.js';
// Some options can have different values for `stdout`/`stderr`/`fd3`.// This normalizes those to array of values.// For example, `{verbose: {stdout: 'none', stderr: 'full'}}` becomes `{verbose: ['none', 'none', 'full']}`export const normalizeFdSpecificOptions = options => { // Keep the null prototype, otherwise a polluted `Object.prototype` would inject option values const optionsCopy = {__proto__: null, ...options};
for (const optionName of FD_SPECIFIC_OPTIONS) { optionsCopy[optionName] = normalizeFdSpecificOption(options, optionName); }
return optionsCopy;};
export const normalizeFdSpecificOption = (options, optionName) => { const stdioLength = getStdioLength(options); const optionBaseArray = Array.from({length: stdioLength + 1}); const optionArray = normalizeFdSpecificValue(options[optionName], optionBaseArray, optionName, stdioLength); return addDefaultValue(optionArray, optionName);};
const getStdioLength = ({stdio}) => Array.isArray(stdio) ? Math.max(stdio.length, STANDARD_STREAMS_ALIASES.length) : STANDARD_STREAMS_ALIASES.length;
const normalizeFdSpecificValue = (optionValue, optionArray, optionName, stdioLength) => { if (isPlainObject(optionValue)) { return normalizeOptionObject(optionValue, optionArray, optionName, stdioLength); }
validateFdSpecificValue(optionValue, optionName); return optionArray.fill(optionValue);};
const normalizeOptionObject = (optionValue, optionArray, optionName, stdioLength) => { for (const fdName of Object.keys(optionValue).sort(compareFdName)) { validateFdSpecificValue(optionValue[fdName], optionName, `${optionName}.${fdName}`); for (const fdNumber of parseFdName(fdName, optionName, stdioLength)) { optionArray[fdNumber] = optionValue[fdName]; } }
return optionArray;};
// Ensure priority order when setting both `stdout`/`stderr`, `fd1`/`fd2`, and `all`const compareFdName = (fdNameA, fdNameB) => getFdNameOrder(fdNameA) < getFdNameOrder(fdNameB) ? 1 : -1;
const getFdNameOrder = fdName => { if (fdName === 'stdout' || fdName === 'stderr') { return 0; }
return fdName === 'all' ? 2 : 1;};
const parseFdName = (fdName, optionName, stdioLength) => { if (fdName === 'ipc') { return [stdioLength]; }
const fdNumber = parseFd(fdName); if (fdNumber === undefined || fdNumber === 0) { throw new TypeError(`"${optionName}.${fdName}" is invalid.It must be "${optionName}.stdout", "${optionName}.stderr", "${optionName}.all", "${optionName}.ipc", or "${optionName}.fd3", "${optionName}.fd4" (and so on).`); }
if (fdNumber !== 'all' && fdNumber >= stdioLength) { throw new TypeError(`"${optionName}.${fdName}" is invalid: that file descriptor does not exist.Please set the "stdio" option to ensure that file descriptor exists.`); }
return fdNumber === 'all' ? [1, 2] : [fdNumber];};
// Use the same syntax for fd-specific options and the `from`/`to` optionsexport const parseFd = fdName => { if (fdName === 'all') { return fdName; }
if (STANDARD_STREAMS_ALIASES.includes(fdName)) { return STANDARD_STREAMS_ALIASES.indexOf(fdName); }
const regexpResult = FD_REGEXP.exec(fdName); if (regexpResult !== null) { return Number(regexpResult.groups.fdNumber); }};
const FD_REGEXP = /^fd(?<fdNumber>\d+)$/;
const addDefaultValue = (optionArray, optionName) => optionArray.map(optionValue => optionValue === undefined ? DEFAULT_OPTIONS[optionName] : optionValue);
const BOOLEAN_FD_SPECIFIC_OPTIONS = new Set(['lines', 'buffer', 'stripFinalNewline']);
// Wrong types must be reported instead of being silently coerced, e.g. a truthy string enabling the `buffer` option.// `undefined` uses the default value.const validateFdSpecificValue = (optionValue, optionName, fullOptionName = optionName) => { if (BOOLEAN_FD_SPECIFIC_OPTIONS.has(optionName)) { checkBooleanOption(optionValue, fullOptionName); } else if (optionName === 'maxBuffer' && optionValue !== undefined && (typeof optionValue !== 'number' || Number.isNaN(optionValue))) { throw new TypeError(`The \`${fullOptionName}\` option must use a number.`); }};
// Default value for the `verbose` optionconst verboseDefault = debuglog('execa').enabled ? 'full' : 'none';
const DEFAULT_OPTIONS = { lines: false, buffer: true, maxBuffer: 1000 * 1000 * 100, verbose: verboseDefault, stripFinalNewline: true,};
// List of options which can have different values for `stdout`/`stderr`export const FD_SPECIFIC_OPTIONS = ['lines', 'buffer', 'maxBuffer', 'verbose', 'stripFinalNewline'];
// Retrieve fd-specific optionexport const getFdSpecificValue = (optionArray, fdNumber) => fdNumber === 'ipc' ? optionArray.at(-1) : optionArray[fdNumber];