Allow streams to end before the other stream piping to them (#821)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 268ad95 · Browse files · 2 files·+19 added−26 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+19 added−26 removed
- tests:+6 added−1 removed
- other:+13 added−25 removed
Provenance
- Commit
4fb8e9a- Tree
ccb1c7d- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
268ad95
2 files changed+19 added−26 removed
modified:lib/stdio/pipeline.js+13 added-25 removed
10 unmodified lines1112131415151617181920192021222324252627262728293031323233343435363738383940414243444546474849505152394041425743444546474849646550515253545510 unmodified lines sources[0].pipe(destination); } else { const mergedSource = mergeStreams(sources); mergedSource.pipe(destination); handleDestinationComplete(mergedSource, destination, finishedStreams); handleStreamError(destination, mergedSource, finishedStreams); }
for (const source of sources) { handleSourceAbortOrError(source, destination, finishedStreams); handleDestinationComplete(source, destination, finishedStreams); handleStreamError(source, destination, finishedStreams); handleStreamError(destination, source, finishedStreams); }};
// `source.pipe(destination)` makes `destination` end when `source` ends.// But it does not propagate aborts or errors. This function does it.const handleSourceAbortOrError = async (source, destination, finishedStreams) => { if (isStandardStream(source) || isStandardStream(destination)) {// We do the same thing in the other direction as well.const handleStreamError = async (stream, otherStream, finishedStreams) => { if (isStandardStream(stream) || isStandardStream(otherStream)) { return; }
try { await onFinishedStream(source, finishedStreams); await onFinishedStream(stream, finishedStreams); } catch (error) { destroyStream(destination, finishedStreams, error); destroyStream(otherStream, finishedStreams, error); }};
// The `destination` should never complete before the `source`.// If it does, this indicates something abnormal, so we abort or error `source`.const handleDestinationComplete = async (source, destination, finishedStreams) => { if (isStandardStream(source) || isStandardStream(destination)) { return; }
try { await onFinishedStream(destination, finishedStreams); destroyStream(source, finishedStreams); } catch (error) { destroyStream(source, finishedStreams, error); }};
// Both functions above call each other recursively.// `finishedStreams` prevents this cycle.const onFinishedStream = async (stream, finishedStreams) => { try { return await finished(stream, {cleanup: true}); await finished(stream, {cleanup: true}); } finally { finishedStreams.add(stream); }};
const destroyStream = (stream, finishedStreams, error) => { if (!finishedStreams.has(stream)) { stream.destroy(error); if (finishedStreams.has(stream)) { return; }
stream.destroy(error);};modified:test/stdio/node-stream.js+6 added-1 removed
191 unmodified lines192 193 test('Handles output streams ends', async t => {194 const stream = noopWritable();195 const childProcess = execa('stdin.js', {stdout: [stream, 'pipe']});196- stream.end();196 childProcess.stdin.end();197+ await setImmediate();198+ stream.destroy();199+200 const error = await t.throwsAsync(childProcess, {code: 'ERR_STREAM_PREMATURE_CLOSE'});201 assertStreamError(t, error);202 });203 24 unmodified lines228 229 const testChildStreamEnd = async (t, stream) => {230 const childProcess = execa('stdin.js', {stdin: [stream, 'pipe']});231 childProcess.stdin.end();232+ await setImmediate();233+ stream.destroy();234+235 const error = await t.throwsAsync(childProcess, {code: 'ERR_STREAM_PREMATURE_CLOSE'});236 assertStreamError(t, error);237 t.true(stream.destroyed);238 };30 unmodified lines