Skip to content
Myr

Fix errors thrown by `.pipe()` (#854)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by GitHub · parent 4c4e882 · Browse files · 11 files+267 added−189 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

11 files+267 added−189 removed

  • tests:+126 added−101 removed
  • other:+141 added−88 removed

11 files+267 added−189 removed

modified:lib/pipe/abort.js+3 added-3 removed
modified:lib/pipe/setup.js+26 added-11 removed
added:lib/pipe/throw.js+54 added
modified:lib/pipe/validate.js+39 added-52 removed
1import {STANDARD_STREAMS_ALIASES, isExecaChildProcess} from '../utils.js';2import {makeEarlyError} from '../return/error.js';3import {abortSourceStream, endDestinationStream} from './streaming.js';1import {STANDARD_STREAMS_ALIASES} from '../utils.js';25export const normalizePipeArguments = (destination, from, {source, sourcePromise, stdioStreamsGroups, options}) => {6 const {destinationStream, destinationError} = getDestinationStream(destination);7 const {sourceStream, sourceError} = getSourceStream(source, stdioStreamsGroups, from, options);8 handlePipeArgumentsError({sourceStream, sourceError, destinationStream, destinationError, stdioStreamsGroups, options});9 return {source, sourcePromise, sourceStream, destinationStream};3export const normalizePipeArguments = ({source, sourcePromise, stdioStreamsGroups}, ...args) => {4 const sourceOptions = PROCESS_OPTIONS.get(source);5 const {6 destination,7 destinationStream,8 destinationError,9 pipeOptions: {from, signal} = {},10 } = getDestinationStream(...args);11 const {sourceStream, sourceError} = getSourceStream(source, stdioStreamsGroups, from, sourceOptions);12 return {13 sourcePromise,14 sourceStream,15 sourceOptions,16 sourceError,17 destination,18 destinationStream,19 destinationError,20 signal,21 stdioStreamsGroups,22 };23};2412const getDestinationStream = destination => {25const getDestinationStream = (...args) => {26 try {14 if (!isExecaChildProcess(destination)) {15 throw new TypeError(`The first argument must be an Execa child process: ${destination}`);16 }1727 const {destination, pipeOptions} = getDestination(...args);28 const destinationStream = destination.stdin;29 if (destinationStream === null) {30 throw new TypeError('The destination child process\'s stdin must be available. Please set its "stdin" option to "pipe".');31 }3223 return {destinationStream};33 return {destination, destinationStream, pipeOptions};34 } catch (error) {35 return {destinationError: error};36 }37};3839const getDestination = (firstArgument, ...args) => {40 if (!PROCESS_OPTIONS.has(firstArgument)) {41 throw new TypeError(`The first argument must be an Execa child process: ${firstArgument}`);42 }4344 return {destination: firstArgument, pipeOptions: args[0]};45};4647export const PROCESS_OPTIONS = new WeakMap();4829const getSourceStream = (source, stdioStreamsGroups, from, options) => {49const getSourceStream = (source, stdioStreamsGroups, from, sourceOptions) => {50 try {51 const streamIndex = getStreamIndex(stdioStreamsGroups, from);52 const sourceStream = streamIndex === 'all' ? source.all : source.stdio[streamIndex];5354 if (sourceStream === null || sourceStream === undefined) {35 throw new TypeError(getInvalidStdioOptionMessage(streamIndex, from, options));55 throw new TypeError(getInvalidStdioOptionMessage(streamIndex, from, sourceOptions));56 }5758 return {sourceStream};59 } catch (error) {
32 unmodified lines
9293 return streamIndex;94};9576const getInvalidStdioOptionMessage = (streamIndex, from, options) => {77 if (streamIndex === 'all' && !options.all) {96const getInvalidStdioOptionMessage = (streamIndex, from, sourceOptions) => {97 if (streamIndex === 'all' && !sourceOptions.all) {98 return 'The "all" option must be true to use `childProcess.pipe(destinationProcess, {from: "all"})`.';99 }10081 const {optionName, optionValue} = getInvalidStdioOption(streamIndex, options);101 const {optionName, optionValue} = getInvalidStdioOption(streamIndex, sourceOptions);102 const pipeOptions = from === undefined ? '' : `, {from: ${serializeOptionValue(from)}}`;103 return `The \`${optionName}: ${serializeOptionValue(optionValue)}\` option is incompatible with using \`childProcess.pipe(destinationProcess${pipeOptions})\`.104Please set this option with "pipe" instead.`;105};
18 unmodified lines
124 }125126 return typeof optionValue === 'number' ? `${optionValue}` : 'Stream';127};108109const handlePipeArgumentsError = ({sourceStream, sourceError, destinationStream, destinationError, stdioStreamsGroups, options}) => {110 const error = getPipeArgumentsError({sourceStream, sourceError, destinationStream, destinationError});111 if (error !== undefined) {112 throw createNonCommandError({error, stdioStreamsGroups, options});113 }114};115116const getPipeArgumentsError = ({sourceStream, sourceError, destinationStream, destinationError}) => {117 if (sourceError !== undefined && destinationError !== undefined) {118 return destinationError;119 }120121 if (destinationError !== undefined) {122 abortSourceStream(sourceStream);123 return destinationError;124 }125126 if (sourceError !== undefined) {127 endDestinationStream(destinationStream);128 return sourceError;129 }130};131132export const createNonCommandError = ({error, stdioStreamsGroups, options}) => makeEarlyError({133 error,134 command: PIPE_COMMAND_MESSAGE,135 escapedCommand: PIPE_COMMAND_MESSAGE,136 stdioStreamsGroups,137 options,138});139140const PIPE_COMMAND_MESSAGE = 'source.pipe(destination)';
modified:lib/return/early-error.js+6 added-13 removed
modified:lib/async.js+10 added-3 removed
6 unmodified lines
7import {handleInputAsync, pipeOutputAsync} from './stdio/async.js';8import {spawnedKill} from './exit/kill.js';9import {cleanupOnExit} from './exit/cleanup.js';10import {pipeToProcess} from './pipe/setup.js';11import {PROCESS_OPTIONS} from './pipe/validate.js';12import {makeAllStream} from './stream/all.js';13import {getSpawnedResult} from './stream/resolve.js';14import {mergePromise} from './promise.js';1516export const execa = (rawFile, rawArgs, rawOptions) => {17 const {spawned, promise, options, stdioStreamsGroups} = runExeca(rawFile, rawArgs, rawOptions);18 spawned.pipe = pipeToProcess.bind(undefined, {source: spawned, sourcePromise: promise, stdioStreamsGroups});19 mergePromise(spawned, promise);20 PROCESS_OPTIONS.set(spawned, options);21 return spawned;22};2324const runExeca = (rawFile, rawArgs, rawOptions) => {25 const {file, args, command, escapedCommand, options} = handleAsyncArguments(rawFile, rawArgs, rawOptions);26 const stdioStreamsGroups = handleInputAsync(options);2728 let spawned;
13 unmodified lines
42 spawned.kill = spawnedKill.bind(undefined, {kill: spawned.kill.bind(spawned), spawned, options, controller});43 spawned.all = makeAllStream(spawned, options);4445 const promise = handlePromise({spawned, options, stdioStreamsGroups, originalStreams, command, escapedCommand, controller});37 spawned.pipe = pipeToProcess.bind(undefined, {source: spawned, sourcePromise: promise, stdioStreamsGroups, options});38 mergePromise(spawned, promise);39 return spawned;46 return {spawned, promise, options, stdioStreamsGroups};47};4849const handleAsyncArguments = (rawFile, rawArgs, rawOptions) => {50 [rawFile, rawArgs, rawOptions] = normalizeArguments(rawFile, rawArgs, rawOptions);
46 unmodified lines
modified:lib/script.js+3 added-2 removed
1import isPlainObject from 'is-plain-obj';2import {isBinary, binaryToString, isChildProcess, isExecaChildProcess} from './utils.js';2import {isBinary, binaryToString, isChildProcess} from './utils.js';3import {execa} from './async.js';4import {execaSync} from './sync.js';5import {PROCESS_OPTIONS} from './pipe/validate.js';67const create$ = options => {8 function $(templatesOrOptions, ...expressions) {9 if (isPlainObject(templatesOrOptions)) {
36 unmodified lines
46 return returnValue;47};4849const scriptPipe = (pipeMethod, options, firstArgument, ...args) => {49 if (isExecaChildProcess(firstArgument)) {50 if (PROCESS_OPTIONS.has(firstArgument)) {51 if (Object.keys(options).length > 0) {52 throw new TypeError('Please use .pipe(options)`command` or .pipe($(options)`command`) instead of .pipe(options)($`command`).');53 }54
152 unmodified lines
modified:lib/utils.js-4 removed
added:test/pipe/template.js+106 added
1import {spawn} from 'node:child_process';2import test from 'ava';3import {$} from '../../index.js';4import {setFixtureDir} from '../helpers/fixtures-dir.js';5import {foobarString} from '../helpers/input.js';67setFixtureDir();89test('$.pipe(childProcess)', async t => {10 const {stdout} = await $`noop.js ${foobarString}`.pipe($({stdin: 'pipe'})`stdin.js`);11 t.is(stdout, foobarString);12});1314test('$.pipe.pipe(childProcess)', async t => {15 const {stdout} = await $`noop.js ${foobarString}`16 .pipe($({stdin: 'pipe'})`stdin.js`)17 .pipe($({stdin: 'pipe'})`stdin.js`);18 t.is(stdout, foobarString);19});2021test('$.pipe`command`', async t => {22 const {stdout} = await $`noop.js ${foobarString}`.pipe`stdin.js`;23 t.is(stdout, foobarString);24});2526test('$.pipe.pipe`command`', async t => {27 const {stdout} = await $`noop.js ${foobarString}`28 .pipe`stdin.js`29 .pipe`stdin.js`;30 t.is(stdout, foobarString);31});3233test('$.pipe(childProcess, pipeOptions)', async t => {34 const {stdout} = await $`noop-fd.js 2 ${foobarString}`.pipe($({stdin: 'pipe'})`stdin.js`, {from: 'stderr'});35 t.is(stdout, foobarString);36});3738test('$.pipe.pipe(childProcess, pipeOptions)', async t => {39 const {stdout} = await $`noop-fd.js 2 ${foobarString}`40 .pipe($({stdin: 'pipe'})`noop-stdin-fd.js 2`, {from: 'stderr'})41 .pipe($({stdin: 'pipe'})`stdin.js`, {from: 'stderr'});42 t.is(stdout, foobarString);43});4445test('$.pipe(pipeOptions)`command`', async t => {46 const {stdout} = await $`noop-fd.js 2 ${foobarString}`.pipe({from: 'stderr'})`stdin.js`;47 t.is(stdout, foobarString);48});4950test('$.pipe.pipe(pipeOptions)`command`', async t => {51 const {stdout} = await $`noop-fd.js 2 ${foobarString}`52 .pipe({from: 'stderr'})`noop-stdin-fd.js 2`53 .pipe({from: 'stderr'})`stdin.js`;54 t.is(stdout, foobarString);55});5657test('$.pipe(options)`command`', async t => {58 const {stdout} = await $`noop.js ${foobarString}`.pipe({stripFinalNewline: false})`stdin.js`;59 t.is(stdout, `${foobarString}\n`);60});6162test('$.pipe.pipe(options)`command`', async t => {63 const {stdout} = await $`noop.js ${foobarString}`64 .pipe({})`stdin.js`65 .pipe({stripFinalNewline: false})`stdin.js`;66 t.is(stdout, `${foobarString}\n`);67});6869test('$.pipe(pipeAndProcessOptions)`command`', async t => {70 const {stdout} = await $`noop-fd.js 2 ${foobarString}\n`.pipe({from: 'stderr', stripFinalNewline: false})`stdin.js`;71 t.is(stdout, `${foobarString}\n`);72});7374test('$.pipe.pipe(pipeAndProcessOptions)`command`', async t => {75 const {stdout} = await $`noop-fd.js 2 ${foobarString}\n`76 .pipe({from: 'stderr'})`noop-stdin-fd.js 2`77 .pipe({from: 'stderr', stripFinalNewline: false})`stdin.js`;78 t.is(stdout, `${foobarString}\n`);79});8081test('$.pipe(options)(secondOptions)`command`', async t => {82 const {stdout} = await $`noop.js ${foobarString}`.pipe({stripFinalNewline: false})({stripFinalNewline: true})`stdin.js`;83 t.is(stdout, foobarString);84});8586test('$.pipe.pipe(options)(secondOptions)`command`', async t => {87 const {stdout} = await $`noop.js ${foobarString}`88 .pipe({})({})`stdin.js`89 .pipe({stripFinalNewline: false})({stripFinalNewline: true})`stdin.js`;90 t.is(stdout, foobarString);91});9293test('$.pipe(options)(childProcess) fails', t => {94 t.throws(() => {95 $`empty.js`.pipe({stdout: 'pipe'})($`empty.js`);96 }, {message: /Please use \.pipe/});97});9899const testInvalidPipe = (t, ...args) => {100 t.throws(() => {101 $`empty.js`.pipe(...args);102 }, {message: /must be a template string/});103};104105test('$.pipe(nonExecaChildProcess) fails', testInvalidPipe, spawn('node', ['--version']));106test('$.pipe(false) fails', testInvalidPipe, false);
modified:test/pipe/validate.js+20 added
161 unmodified lines
162163 await assertPipeError(t, pipePromise, 'an Execa child process');164 t.like(await source, {stdout: undefined});165});166167test('Sets the right error message when the "all" option is incompatible - execa.execa', async t => {168 await assertPipeError(169 t,170 execa('empty.js')171 .pipe(execa('stdin.js', {all: false}))172 .pipe(execa('empty.js'), {from: 'all'}),173 '"all" option must be true',174 );175});176177test('Sets the right error message when the "all" option is incompatible - early error', async t => {178 await assertPipeError(179 t,180 execa('empty.js', {killSignal: false})181 .pipe(execa('stdin.js', {all: false}))182 .pipe(execa('empty.js'), {from: 'all'}),183 '"all" option must be true',184 );185});
modified:test/script.js-101 removed
1import {spawn} from 'node:child_process';1import test from 'ava';2import {isStream} from 'is-stream';3import {$} from '../index.js';4import {setFixtureDir} from './helpers/fixtures-dir.js';6import {foobarString} from './helpers/input.js';56setFixtureDir();78// Workaround since some text editors or IDEs do not allow inputting \r directly
356 unmodified lines
365366test('$ stdin has no default value when stdio is set', t => {367 t.true(isStream($({stdio: 'pipe'})`noop.js`.stdin));368});371372test('$.pipe(childProcess)', async t => {373 const {stdout} = await $`noop.js ${foobarString}`.pipe($({stdin: 'pipe'})`stdin.js`);374 t.is(stdout, foobarString);375});376377test('$.pipe.pipe(childProcess)', async t => {378 const {stdout} = await $`noop.js ${foobarString}`379 .pipe($({stdin: 'pipe'})`stdin.js`)380 .pipe($({stdin: 'pipe'})`stdin.js`);381 t.is(stdout, foobarString);382});383384test('$.pipe`command`', async t => {385 const {stdout} = await $`noop.js ${foobarString}`.pipe`stdin.js`;386 t.is(stdout, foobarString);387});388389test('$.pipe.pipe`command`', async t => {390 const {stdout} = await $`noop.js ${foobarString}`391 .pipe`stdin.js`392 .pipe`stdin.js`;393 t.is(stdout, foobarString);394});395396test('$.pipe(childProcess, pipeOptions)', async t => {397 const {stdout} = await $`noop-fd.js 2 ${foobarString}`.pipe($({stdin: 'pipe'})`stdin.js`, {from: 'stderr'});398 t.is(stdout, foobarString);399});400401test('$.pipe.pipe(childProcess, pipeOptions)', async t => {402 const {stdout} = await $`noop-fd.js 2 ${foobarString}`403 .pipe($({stdin: 'pipe'})`noop-stdin-fd.js 2`, {from: 'stderr'})404 .pipe($({stdin: 'pipe'})`stdin.js`, {from: 'stderr'});405 t.is(stdout, foobarString);406});407408test('$.pipe(pipeOptions)`command`', async t => {409 const {stdout} = await $`noop-fd.js 2 ${foobarString}`.pipe({from: 'stderr'})`stdin.js`;410 t.is(stdout, foobarString);411});412413test('$.pipe.pipe(pipeOptions)`command`', async t => {414 const {stdout} = await $`noop-fd.js 2 ${foobarString}`415 .pipe({from: 'stderr'})`noop-stdin-fd.js 2`416 .pipe({from: 'stderr'})`stdin.js`;417 t.is(stdout, foobarString);418});419420test('$.pipe(options)`command`', async t => {421 const {stdout} = await $`noop.js ${foobarString}`.pipe({stripFinalNewline: false})`stdin.js`;422 t.is(stdout, `${foobarString}\n`);423});424425test('$.pipe.pipe(options)`command`', async t => {426 const {stdout} = await $`noop.js ${foobarString}`427 .pipe({})`stdin.js`428 .pipe({stripFinalNewline: false})`stdin.js`;429 t.is(stdout, `${foobarString}\n`);430});431432test('$.pipe(pipeAndProcessOptions)`command`', async t => {433 const {stdout} = await $`noop-fd.js 2 ${foobarString}\n`.pipe({from: 'stderr', stripFinalNewline: false})`stdin.js`;434 t.is(stdout, `${foobarString}\n`);435});436437test('$.pipe.pipe(pipeAndProcessOptions)`command`', async t => {438 const {stdout} = await $`noop-fd.js 2 ${foobarString}\n`439 .pipe({from: 'stderr'})`noop-stdin-fd.js 2`440 .pipe({from: 'stderr', stripFinalNewline: false})`stdin.js`;441 t.is(stdout, `${foobarString}\n`);442});443444test('$.pipe(options)(secondOptions)`command`', async t => {445 const {stdout} = await $`noop.js ${foobarString}`.pipe({stripFinalNewline: false})({stripFinalNewline: true})`stdin.js`;446 t.is(stdout, foobarString);447});448449test('$.pipe.pipe(options)(secondOptions)`command`', async t => {450 const {stdout} = await $`noop.js ${foobarString}`451 .pipe({})({})`stdin.js`452 .pipe({stripFinalNewline: false})({stripFinalNewline: true})`stdin.js`;453 t.is(stdout, foobarString);454});455456test('$.pipe(options)(childProcess) fails', t => {457 t.throws(() => {458 $`empty.js`.pipe({stdout: 'pipe'})($`empty.js`);459 }, {message: /Please use \.pipe/});460});461462const testInvalidPipe = (t, ...args) => {463 t.throws(() => {464 $`empty.js`.pipe(...args);465 }, {message: /must be a template string/});466};467468test('$.pipe(nonExecaChildProcess) fails', testInvalidPipe, spawn('node', ['--version']));469test('$.pipe(false) fails', testInvalidPipe, false);