Improve `.pipe()` validation error message (#778)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 27bb798 · Browse files · 4 files·+72 added−40 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+72 added−40 removed
- tests:+25 added−10 removed
- other:+47 added−30 removed
Provenance
- Commit
dd5f30f- Tree
5330e2f- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
27bb798
4 files changed+72 added−40 removed
modified:lib/stdio/normalize.js+6 added-6 removed
123456789101110121314151416171819202 unmodified lines23242526252728293029313231323334353637import {STANDARD_STREAMS_ALIASES} from './utils.js';
// Add support for `stdin`/`stdout`/`stderr` as an alias for `stdio`export const normalizeStdio = options => { if (!options) { return [undefined, undefined, undefined]; }
const {stdio} = options;
if (stdio === undefined) { return aliases.map(alias => options[alias]); return STANDARD_STREAMS_ALIASES.map(alias => options[alias]); }
if (hasAlias(options)) { throw new Error(`It's not possible to provide \`stdio\` in combination with one of ${aliases.map(alias => `\`${alias}\``).join(', ')}`); throw new Error(`It's not possible to provide \`stdio\` in combination with one of ${STANDARD_STREAMS_ALIASES.map(alias => `\`${alias}\``).join(', ')}`); }
if (typeof stdio === 'string') { return [stdio, stdio, stdio];2 unmodified lines if (!Array.isArray(stdio)) { throw new TypeError(`Expected \`stdio\` to be of type \`string\` or \`Array\`, got \`${typeof stdio}\``); }
const length = Math.max(stdio.length, aliases.length); const length = Math.max(stdio.length, STANDARD_STREAMS_ALIASES.length); return Array.from({length}, (value, index) => stdio[index]);};
const hasAlias = options => aliases.some(alias => options[alias] !== undefined);const hasAlias = options => STANDARD_STREAMS_ALIASES.some(alias => options[alias] !== undefined);
const aliases = ['stdin', 'stdout', 'stderr'];
// Same but for `execaNode()`, i.e. push `ipc` unless already presentexport const normalizeStdioNode = options => { const stdio = normalizeStdio(options); return stdio.includes('ipc') ? stdio : [...stdio, 'ipc'];};modified:lib/stdio/utils.js+1 added
23 unmodified lines242526272823 unmodified lines};
export const isStandardStream = stream => STANDARD_STREAMS.includes(stream);export const STANDARD_STREAMS = [process.stdin, process.stdout, process.stderr];export const STANDARD_STREAMS_ALIASES = ['stdin', 'stdout', 'stderr'];modified:lib/pipe.js+40 added-24 removed
12344567878910111213149 unmodified lines242526272627282829303132333435363137383940353641424344454641474843495051525348545556515758595455566061626364616566676865697071727374697071757677787980817475828384857886878889909192939495import {ChildProcess} from 'node:child_process';import {isWritableStream} from 'is-stream';import {STANDARD_STREAMS_ALIASES} from './stdio/utils.js';
export const pipeToProcess = ({spawned, stdioStreamsGroups, options}, targetProcess, streamName = 'stdout') => {export const pipeToProcess = ({spawned, stdioStreamsGroups, options}, targetProcess, streamName) => { validateTargetProcess(targetProcess);
const streamIndex = getStreamIndex(streamName); const inputStream = getInputStream(spawned, streamName, stdioStreamsGroups); validateStdioOption(inputStream, spawned, streamName, options); const inputStream = getInputStream(spawned, streamIndex, stdioStreamsGroups); validateStdioOption(inputStream, streamIndex, streamName, options);
inputStream.pipe(targetProcess.stdin); return targetProcess;};9 unmodified lines};
const isExecaChildProcess = target => target instanceof ChildProcess && typeof target.then === 'function';
const getInputStream = (spawned, streamName, stdioStreamsGroups) => { if (VALID_STREAM_NAMES.has(streamName)) { return spawned[streamName];const getStreamIndex = (streamName = 'stdout') => STANDARD_STREAMS_ALIASES.includes(streamName) ? STANDARD_STREAMS_ALIASES.indexOf(streamName) : streamName;
const getInputStream = (spawned, streamIndex, stdioStreamsGroups) => { if (streamIndex === 'all') { return spawned.all; }
if (streamName === 'stdin') { if (streamIndex === 0) { throw new TypeError('The second argument must not be "stdin".'); }
if (!Number.isInteger(streamName) || streamName < 0) { throw new TypeError(`The second argument must not be "${streamName}". if (!Number.isInteger(streamIndex) || streamIndex < 0) { throw new TypeError(`The second argument must not be "${streamIndex}".It must be "stdout", "stderr", "all" or a file descriptor integer.It is optional and defaults to "stdout".`); }
const stdioStreams = stdioStreamsGroups[streamName]; const stdioStreams = stdioStreamsGroups[streamIndex]; if (stdioStreams === undefined) { throw new TypeError(`The second argument must not be ${streamName}: that file descriptor does not exist. throw new TypeError(`The second argument must not be ${streamIndex}: that file descriptor does not exist.Please set the "stdio" option to ensure that file descriptor exists.`); }
if (stdioStreams[0].direction === 'input') { throw new TypeError(`The second argument must not be ${streamName}: it must be a readable stream, not writable.`); throw new TypeError(`The second argument must not be ${streamIndex}: it must be a readable stream, not writable.`); }
return spawned.stdio[streamName]; return spawned.stdio[streamIndex];};
const VALID_STREAM_NAMES = new Set(['stdout', 'stderr', 'all']);
const validateStdioOption = (inputStream, spawned, streamName, options) => {const validateStdioOption = (inputStream, streamIndex, streamName, options) => { if (inputStream !== null && inputStream !== undefined) { return; }
if (streamName === 'all' && !options.all) { if (streamIndex === 'all' && !options.all) { throw new TypeError('The "all" option must be true to use `childProcess.pipe(targetProcess, "all")`.'); }
throw new TypeError(`The "${getInvalidStdioOption(inputStream, spawned, options)}" option's value is incompatible with using \`childProcess.pipe(targetProcess)\`. const {optionName, optionValue} = getInvalidStdioOption(streamIndex, options); const pipeArgument = streamName === undefined ? '' : `, ${streamName}`; throw new TypeError(`The \`${optionName}: ${serializeOptionValue(optionValue)}\` option is incompatible with using \`childProcess.pipe(targetProcess${pipeArgument})\`.Please set this option with "pipe" instead.`);};
const getInvalidStdioOption = (inputStream, spawned, options) => { if (inputStream === spawned.stdout && options.stdout !== undefined) { return 'stdout';const getInvalidStdioOption = (streamIndex, {stdout, stderr, stdio}) => { const usedIndex = streamIndex === 'all' ? 1 : streamIndex;
if (usedIndex === 1 && stdout !== undefined) { return {optionName: 'stdout', optionValue: stdout}; }
if (inputStream === spawned.stderr && options.stderr !== undefined) { return 'stderr'; if (usedIndex === 2 && stderr !== undefined) { return {optionName: 'stderr', optionValue: stderr}; }
return 'stdio'; return {optionName: `stdio[${usedIndex}]`, optionValue: stdio[usedIndex]};};
const serializeOptionValue = optionValue => { if (typeof optionValue === 'string') { return `"${optionValue}"`; }
return typeof optionValue === 'number' ? `${optionValue}` : 'Stream';};modified:test/pipe.js+25 added-10 removed
77 unmodified lines78 execa('empty.js').pipe(execa('stdin.js', {stdin: 'ignore'}));79 }, {message: /stdin must be available/});80 });81 82+// eslint-disable-next-line max-params82-const invalidSource = (t, optionName, streamName, options) => {83+const invalidSource = (t, optionName, optionValue, streamName, options) => {84 t.throws(() => {85 execa('empty.js', options).pipe(execa('empty.js'), streamName);85- }, {message: new RegExp(`"${optionName}" option's value is incompatible`)});86+ }, {message: new RegExp(`\`${optionName}: ${optionValue}\` option is incompatible`)});87 };88 88-test('Cannot set "stdout" option to "ignore" to use pipe()', invalidSource, 'stdout', 1, {stdout: 'ignore'});89-test('Cannot set "stderr" option to "ignore" to use pipe()', invalidSource, 'stderr', 2, {stderr: 'ignore'});90-test('Cannot set "stdio[*]" option to "ignore" to use pipe()', invalidSource, 'stdio', 3, {stdio: ['pipe', 'pipe', 'pipe', 'ignore']});91-test('Cannot set "stdout" + "stderr" option to "ignore" to use pipe() with "all"', invalidSource, 'stdout', 1, {stdout: 'ignore', stderr: 'ignore', all: true});92-test('Cannot set "stdout" option to "inherit" to use pipe()', invalidSource, 'stdout', 1, {stdout: 'inherit'});93-test('Cannot set "stdout" option to "ipc" to use pipe()', invalidSource, 'stdout', 1, {stdout: 'ipc'});94-test('Cannot set "stdout" option to file descriptors to use pipe()', invalidSource, 'stdout', 1, {stdout: 1});95-test('Cannot set "stdout" option to Node.js streams to use pipe()', invalidSource, 'stdout', 1, {stdout: process.stdout});89+test('Cannot set "stdout" option to "ignore" to use pipe(...)', invalidSource, 'stdout', '"ignore"', undefined, {stdout: 'ignore'});90+test('Cannot set "stdout" option to "ignore" to use pipe(..., 1)', invalidSource, 'stdout', '"ignore"', 1, {stdout: 'ignore'});91+test('Cannot set "stdout" option to "ignore" to use pipe(..., "stdout")', invalidSource, 'stdout', '"ignore"', 'stdout', {stdout: 'ignore'});92+test('Cannot set "stdout" + "stderr" option to "ignore" to use pipe(...)', invalidSource, 'stdout', '"ignore"', undefined, {stdout: 'ignore', stderr: 'ignore'});93+test('Cannot set "stdout" + "stderr" option to "ignore" to use pipe(..., 1)', invalidSource, 'stdout', '"ignore"', 1, {stdout: 'ignore', stderr: 'ignore'});94+test('Cannot set "stdout" + "stderr" option to "ignore" to use pipe(..., "stdout")', invalidSource, 'stdout', '"ignore"', 'stdout', {stdout: 'ignore', stderr: 'ignore'});95+test('Cannot set "stdio[1]" option to "ignore" to use pipe(...)', invalidSource, 'stdio\\[1\\]', '"ignore"', undefined, {stdio: ['pipe', 'ignore', 'pipe']});96+test('Cannot set "stdio[1]" option to "ignore" to use pipe(..., 1)', invalidSource, 'stdio\\[1\\]', '"ignore"', 1, {stdio: ['pipe', 'ignore', 'pipe']});97+test('Cannot set "stdio[1]" option to "ignore" to use pipe(..., "stdout")', invalidSource, 'stdio\\[1\\]', '"ignore"', 'stdout', {stdio: ['pipe', 'ignore', 'pipe']});98+test('Cannot set "stderr" option to "ignore" to use pipe(..., 2)', invalidSource, 'stderr', '"ignore"', 2, {stderr: 'ignore'});99+test('Cannot set "stderr" option to "ignore" to use pipe(..., "stderr")', invalidSource, 'stderr', '"ignore"', 'stderr', {stderr: 'ignore'});100+test('Cannot set "stdout" + "stderr" option to "ignore" to use pipe(..., 2)', invalidSource, 'stderr', '"ignore"', 2, {stdout: 'ignore', stderr: 'ignore'});101+test('Cannot set "stdout" + "stderr" option to "ignore" to use pipe(..., "stderr")', invalidSource, 'stderr', '"ignore"', 'stderr', {stdout: 'ignore', stderr: 'ignore'});102+test('Cannot set "stdio[2]" option to "ignore" to use pipe(..., 2)', invalidSource, 'stdio\\[2\\]', '"ignore"', 2, {stdio: ['pipe', 'pipe', 'ignore']});103+test('Cannot set "stdio[2]" option to "ignore" to use pipe(..., "stderr")', invalidSource, 'stdio\\[2\\]', '"ignore"', 'stderr', {stdio: ['pipe', 'pipe', 'ignore']});104+test('Cannot set "stdio[3]" option to "ignore" to use pipe(..., 3)', invalidSource, 'stdio\\[3\\]', '"ignore"', 3, {stdio: ['pipe', 'pipe', 'pipe', 'ignore']});105+test('Cannot set "stdout" + "stderr" option to "ignore" to use pipe(..., "all")', invalidSource, 'stdout', '"ignore"', 'all', {stdout: 'ignore', stderr: 'ignore', all: true});106+test('Cannot set "stdio[1]" + "stdio[2]" option to "ignore" to use pipe(..., "all")', invalidSource, 'stdio\\[1\\]', '"ignore"', 'all', {stdio: ['pipe', 'ignore', 'ignore'], all: true});107+test('Cannot set "stdout" option to "inherit" to use pipe()', invalidSource, 'stdout', '"inherit"', 1, {stdout: 'inherit'});108+test('Cannot set "stdout" option to "ipc" to use pipe()', invalidSource, 'stdout', '"ipc"', 1, {stdout: 'ipc'});109+test('Cannot set "stdout" option to file descriptors to use pipe()', invalidSource, 'stdout', '1', 1, {stdout: 1});110+test('Cannot set "stdout" option to Node.js streams to use pipe()', invalidSource, 'stdout', 'Stream', 1, {stdout: process.stdout});