parameters.js
This file
Path
lib/methods/parameters.js
40 lines · 1.8 KiB · JavaScript
Last changed by
Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
History of this fileTake it
1234567891011121314151617181920212223242526272829303132333435363738394041import isPlainObject from 'is-plain-obj';import {safeNormalizeFileUrl} from '../arguments/file-url.js';
// The command `arguments` and `options` are both optional.// This also does basic validation on them and on the command file.export const normalizeParameters = (rawFile, rawArguments = [], rawOptions, ...rawExtraArguments) => { if (rawExtraArguments.length > 0) { throw new TypeError('Execa accepts at most three arguments: file, arguments, options.'); }
if (rawOptions !== undefined && isPlainObject(rawArguments)) { throw new TypeError('Execa accepts at most one options object. Please merge the second and third arguments.'); }
const filePath = safeNormalizeFileUrl(rawFile, 'First argument'); const [commandArguments, options = {}] = isPlainObject(rawArguments) ? [[], rawArguments] : [rawArguments, rawOptions];
if (!Array.isArray(commandArguments)) { throw new TypeError(`Second argument must be either an array of arguments or an options object: ${commandArguments}`); }
if (commandArguments.some(commandArgument => typeof commandArgument === 'object' && commandArgument !== null)) { throw new TypeError(`Second argument must be an array of strings: ${commandArguments}`); }
const normalizedArguments = commandArguments.map(String); const nullByteArgument = normalizedArguments.find(normalizedArgument => normalizedArgument.includes('\0')); if (nullByteArgument !== undefined) { throw new TypeError(`Arguments cannot contain null bytes ("\\0"): ${nullByteArgument}`); }
if (!isPlainObject(options)) { throw new TypeError(`Last argument must be an options object: ${options}`); }
// Prevent prototype pollution by copying only own properties to a null-prototype object return [filePath, normalizedArguments, {__proto__: null, ...options}];};