Fix failure of series of `.pipe()` (#849)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 55800c3 · Browse files · 6 files·+17 added−17 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+17 added−17 removed
- tests:+9 added−9 removed
- other:+8 added−8 removed
Provenance
- Commit
45744fa- Tree
5eb71fd- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
55800c3
6 files changed+17 added−17 removed
modified:lib/pipe/sequence.js+2 added-2 removed
123455678991011121311 unmodified lines// Like Bash, we await both processes. This is unlike some other shells which only await the destination process.// Like Bash with the `pipefail` option, if either process fails, the whole pipe fails.// Like Bash, if both processes fail, we return the failure of the destination.// This ensures both processes' errors are present, using `error.pipedFrom`.export const waitForBothProcesses = async (source, destination) => {export const waitForBothProcesses = async (sourcePromise, destination) => { const [ {status: sourceStatus, reason: sourceReason, value: sourceResult = sourceReason}, {status: destinationStatus, reason: destinationReason, value: destinationResult = destinationReason}, ] = await Promise.allSettled([source, destination]); ] = await Promise.allSettled([sourcePromise, destination]);
if (!destinationResult.pipedFrom.includes(sourceResult)) { destinationResult.pipedFrom.push(sourceResult); }11 unmodified linesmodified:lib/pipe/setup.js+3 added-3 removed
4 unmodified lines567899101112131414151617181919202122238 unmodified lines4 unmodified lines
// Pipe a process' `stdout`/`stderr`/`stdio` into another process' `stdin`export const pipeToProcess = (sourceInfo, destination, pipeOptions) => { const promise = handlePipePromise(sourceInfo, destination, pipeOptions); promise.pipe = pipeToProcess.bind(undefined, {...sourceInfo, spawned: destination}); promise.pipe = pipeToProcess.bind(undefined, {...sourceInfo, source: destination, sourcePromise: promise}); return promise;};
const handlePipePromise = async (sourceInfo, destination, {from, signal} = {}) => { const {source, sourceStream, destinationStream} = normalizePipeArguments(destination, from, sourceInfo); const {sourcePromise, sourceStream, destinationStream} = normalizePipeArguments(destination, from, sourceInfo); const maxListenersController = new AbortController(); try { const mergedStream = pipeProcessStream(sourceStream, destinationStream, maxListenersController); return await Promise.race([ waitForBothProcesses(source, destination), waitForBothProcesses(sourcePromise, destination), ...unpipeOnAbort(signal, sourceStream, mergedStream, sourceInfo), ]); } finally { maxListenersController.abort();8 unmodified linesmodified:lib/pipe/validate.js+2 added-2 removed
1234556789910111213127 unmodified linesExpand allimport {STANDARD_STREAMS_ALIASES, isExecaChildProcess} from '../utils.js';import {makeEarlyError} from '../return/error.js';import {abortSourceStream, endDestinationStream} from './streaming.js';
export const normalizePipeArguments = (destination, from, {spawned: source, stdioStreamsGroups, options}) => {export const normalizePipeArguments = (destination, from, {source, sourcePromise, stdioStreamsGroups, options}) => { const {destinationStream, destinationError} = getDestinationStream(destination); const {sourceStream, sourceError} = getSourceStream(source, stdioStreamsGroups, from, options); handlePipeArgumentsError({sourceStream, sourceError, destinationStream, destinationError, stdioStreamsGroups, options}); return {source, sourceStream, destinationStream}; return {source, sourcePromise, sourceStream, destinationStream};};
const getDestinationStream = destination => { try {127 unmodified linesExpand allmodified:lib/async.js+1 added-1 removed
30 unmodified lines31 cleanupOnExit(spawned, options, controller);32 33 spawned.kill = spawnedKill.bind(undefined, {kill: spawned.kill.bind(spawned), spawned, options, controller});34 spawned.all = makeAllStream(spawned, options);35- spawned.pipe = pipeToProcess.bind(undefined, {spawned, stdioStreamsGroups, options});35 36 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;40 };41 48 unmodified linesmodified:test/pipe/sequence.js+5 added-5 removed
92 unmodified lines93 const source = execa('noop-fail.js', ['1', foobarString]);94 const destination = execa('stdin.js');95 const secondDestination = execa('stdin.js');96 const pipePromise = source.pipe(destination);97- const secondPipePromise = destination.pipe(secondDestination);97+ const secondPipePromise = pipePromise.pipe(secondDestination);98 99 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(source));100- await secondPipePromise;100+ t.is(await t.throwsAsync(secondPipePromise), await t.throwsAsync(source));101 t.like(await t.throwsAsync(source), {stdout: foobarString, exitCode: 2});102- await destination;103- await secondDestination;102+ t.like(await destination, {stdout: foobarString});103+ t.like(await secondDestination, {stdout: foobarString});104 });105 106 const testSourceTerminated = async (t, signal) => {107 const source = execa('noop-repeat.js');35 unmodified lines143 const source = execa('noop-repeat.js');144 const destination = execa('stdin.js');145 const secondDestination = execa('fail.js');146 const pipePromise = source.pipe(destination);147- const secondPipePromise = destination.pipe(secondDestination);147+ const secondPipePromise = pipePromise.pipe(secondDestination);148 149 t.is(await t.throwsAsync(pipePromise), await t.throwsAsync(destination));150 t.is(await t.throwsAsync(secondPipePromise), await t.throwsAsync(secondDestination));151 t.like(await t.throwsAsync(secondDestination), {exitCode: 2});162 unmodified linesmodified:test/pipe/streaming.js+4 added-4 removed
177 unmodified lines178 const source = execa('noop.js', [foobarString]);179 const destination = execa('stdin.js');180 const secondDestination = execa('stdin.js');181 const pipePromise = source.pipe(destination);182- const secondPipePromise = destination.pipe(secondDestination);182+ const secondPipePromise = pipePromise.pipe(secondDestination);183 const thirdPipePromise = source.pipe(secondDestination);184 185 t.like(await source, {stdout: foobarString});186 t.like(await destination, {stdout: foobarString});68 unmodified lines255 const source = execa('noop.js', [foobarString]);256 const destination = execa('stdin.js');257 const secondDestination = execa('stdin.js');258 const pipePromise = source.pipe(destination);259- const secondPipePromise = destination.pipe(secondDestination);259+ const secondPipePromise = pipePromise.pipe(secondDestination);260 261 t.like(await source, {stdout: foobarString});262 t.like(await destination, {stdout: foobarString});263 t.like(await secondDestination, {stdout: foobarString});19 unmodified lines283 const source = execa('noop.js', [foobarString]);284 const destination = execa('stdin.js');285 const secondDestination = execa('stdin.js');286 const pipePromise = source.pipe(destination);287- const secondPipePromise = destination.pipe(secondDestination);287+ const secondPipePromise = pipePromise.pipe(secondDestination);288 289 const destinationResult = await destination;290 t.deepEqual(destinationResult.pipedFrom, []);291 const secondDestinationResult = await secondDestination;51 unmodified lines343 const source = execa('noop-fail.js', ['1', foobarString]);344 const destination = execa('stdin-fail.js');345 const secondDestination = execa('stdin.js');346 const pipePromise = source.pipe(destination);347- const secondPipePromise = destination.pipe(secondDestination);347+ const secondPipePromise = pipePromise.pipe(secondDestination);348 349 const destinationResult = await t.throwsAsync(destination);350 const secondDestinationResult = await secondDestination;351 t.deepEqual(secondDestinationResult.pipedFrom, []);77 unmodified lines