Skip to content
Myr

Improve streams pipe logic (#885)

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

authored by ehmicky · committed by GitHub · parent 6fda30f · Browse files · 9 files+90 added−90 removed
This commit

State

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

Diff stats

9 files+90 added−90 removed

  • tests:+48 added−20 removed
  • other:+42 added−70 removed

9 files+90 added−90 removed

modified:lib/pipe/streaming.js+8 added-26 removed
modified:lib/pipe/throw.js+1 added-1 removed
modified:lib/stdio/async.js+4 added-2 removed
1import {createReadStream, createWriteStream} from 'node:fs';2import {Buffer} from 'node:buffer';3import {Readable, Writable} from 'node:stream';4import mergeStreams from '@sindresorhus/merge-streams';5import {isStandardStream, incrementMaxListeners} from '../utils.js';6import {handleInput} from './handle.js';7import {pipeStreams} from './pipeline.js';8import {TYPE_TO_MESSAGE} from './type.js';
42 unmodified lines
51 }52 }5354 for (const [fdNumber, inputStreams] of Object.entries(inputStreamsGroups)) {55 const inputStream = inputStreams.length === 1 ? inputStreams[0] : mergeStreams(inputStreams);54 pipeStreams(inputStreams, spawned.stdio[fdNumber]);56 pipeStreams(inputStream, spawned.stdio[fdNumber]);57 }58};5960const pipeStdioOption = (spawned, {type, value, direction, fdNumber}, inputStreamsGroups, controller) => {
3 unmodified lines
6465 setStandardStreamMaxListeners(value, controller);6667 if (direction === 'output') {66 pipeStreams([spawned.stdio[fdNumber]], value);68 pipeStreams(spawned.stdio[fdNumber], value);69 } else {70 inputStreamsGroups[fdNumber] = [...(inputStreamsGroups[fdNumber] ?? []), value];71 }72};
25 unmodified lines
modified:lib/stdio/generator.js+2 added-2 removed
85 unmodified lines
8687// `childProcess.stdin|stdout|stderr|stdio` is directly mutated.88export const pipeGenerator = (spawned, {value, direction, fdNumber}) => {89 if (direction === 'output') {90 pipeStreams([spawned.stdio[fdNumber]], value);90 pipeStreams(spawned.stdio[fdNumber], value);91 } else {92 pipeStreams([value], spawned.stdio[fdNumber]);92 pipeStreams(value, spawned.stdio[fdNumber]);93 }9495 const streamProperty = PROCESS_STREAM_PROPERTIES[fdNumber];96 if (streamProperty !== undefined) {
7 unmodified lines
modified:lib/stdio/pipeline.js+26 added-38 removed
modified:lib/stream/wait.js+1 added-1 removed
52 unmodified lines
5354// When `stream.destroy()` is called without an `error` argument, stream is aborted.55// This is the only way to abort a readable stream, which can be useful in some instances.56// Therefore, we ignore this error on readable streams.57export const isStreamAbort = error => error?.code === 'ERR_STREAM_PREMATURE_CLOSE';57const isStreamAbort = error => error?.code === 'ERR_STREAM_PREMATURE_CLOSE';5859// When `stream.write()` is called but the underlying source has been closed, `EPIPE` is emitted.60// When piping processes, the source process usually decides when to stop piping.61// However, there are some instances when the destination does instead, such as `... | head -n1`.
3 unmodified lines
modified:test/helpers/stream.js+2 added-2 removed
modified:test/stdio/node-stream.js+40 added
264 unmodified lines
265266test('Can pass Duplex streams to stdout', testOutputDuplexStream, 1);267test('Can pass Duplex streams to stderr', testOutputDuplexStream, 2);268test('Can pass Duplex streams to output stdio[*]', testOutputDuplexStream, 3);269270const testInputStreamAbort = async (t, fdNumber) => {271 const stream = new PassThrough();272 stream.destroy();273274 const childProcess = execa('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, [stream, new Uint8Array()]));275 await childProcess;276 t.true(childProcess.stdio[fdNumber].writableEnded);277};278279test('childProcess.stdin is ended when an input stream aborts', testInputStreamAbort, 0);280test('childProcess.stdio[*] is ended when an input stream aborts', testInputStreamAbort, 3);281282const testInputStreamError = async (t, fdNumber) => {283 const stream = new PassThrough();284 const error = new Error(foobarString);285 stream.destroy(error);286287 const childProcess = execa('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, [stream, new Uint8Array()]));288 t.is(await t.throwsAsync(childProcess), error);289 t.true(childProcess.stdio[fdNumber].writableEnded);290};291292test('childProcess.stdin is ended when an input stream errors', testInputStreamError, 0);293test('childProcess.stdio[*] is ended when an input stream errors', testInputStreamError, 3);294295const testOutputStreamError = async (t, fdNumber) => {296 const stream = new PassThrough();297 const error = new Error(foobarString);298 stream.destroy(error);299300 const childProcess = execa('noop-fd.js', [`${fdNumber}`], getStdio(fdNumber, [stream, 'pipe']));301 t.is(await t.throwsAsync(childProcess), error);302 t.true(childProcess.stdio[fdNumber].readableAborted);303 t.is(childProcess.stdio[fdNumber].errored, null);304};305306test('childProcess.stdout is aborted when an output stream errors', testOutputStreamError, 1);307test('childProcess.stderr is aborted when an output stream errors', testOutputStreamError, 2);308test('childProcess.stdio[*] is aborted when an output stream errors', testOutputStreamError, 3);
modified:test/stream/wait.js+6 added-18 removed
115 unmodified lines
116test('Throws abort error when output stdio[*] option aborts with no more writes, with a transform', testStreamAbortFail, destroyOptionStream, noopWritable(), 3, true);117test('Throws abort error when output stdio[*] Duplex option aborts with no more writes, with a transform', testStreamAbortFail, destroyOptionStream, noopDuplex(), 3, true);118119// eslint-disable-next-line max-params120const testStreamEpipeSuccess = async (t, streamMethod, stream, fdNumber, useTransform) => {121 const childProcess = execa('noop-stdin-fd.js', [`${fdNumber}`], getStreamStdio(fdNumber, stream, useTransform));122 streamMethod({stream, childProcess, fdNumber});123 childProcess.stdin.end(foobarString);124125 const {stdio} = await childProcess;126 t.is(stdio[fdNumber], foobarString);127 t.true(stream.destroyed);128};129130test('Passes when stdout option ends with more writes', testStreamEpipeSuccess, endOptionStream, noopWritable(), 1, false);131test('Passes when stderr option ends with more writes', testStreamEpipeSuccess, endOptionStream, noopWritable(), 2, false);132test('Passes when output stdio[*] option ends with more writes', testStreamEpipeSuccess, endOptionStream, noopWritable(), 3, false);133test('Passes when stdout option ends with more writes, with a transform', testStreamEpipeSuccess, endOptionStream, noopWritable(), 1, true);134test('Passes when stderr option ends with more writes, with a transform', testStreamEpipeSuccess, endOptionStream, noopWritable(), 2, true);135test('Passes when output stdio[*] option ends with more writes, with a transform', testStreamEpipeSuccess, endOptionStream, noopWritable(), 3, true);136137// eslint-disable-next-line max-params120const testStreamEpipeFail = async (t, streamMethod, stream, fdNumber, useTransform) => {121 const childProcess = execa('noop-stdin-fd.js', [`${fdNumber}`], getStreamStdio(fdNumber, stream, useTransform));122 streamMethod({stream, childProcess, fdNumber});123 await setImmediate();
7 unmodified lines
131 t.true(stderr.includes('EPIPE'));132 }133};134135test('Throws EPIPE when stdout option ends with more writes', testStreamEpipeFail, endOptionStream, noopWritable(), 1, false);136test('Throws EPIPE when stdout option aborts with more writes', testStreamEpipeFail, destroyOptionStream, noopWritable(), 1, false);137test('Throws EPIPE when stdout option Duplex aborts with more writes', testStreamEpipeFail, destroyOptionStream, noopDuplex(), 1, false);138test('Throws EPIPE when stderr option ends with more writes', testStreamEpipeFail, endOptionStream, noopWritable(), 2, false);139test('Throws EPIPE when stderr option aborts with more writes', testStreamEpipeFail, destroyOptionStream, noopWritable(), 2, false);140test('Throws EPIPE when stderr option Duplex aborts with more writes', testStreamEpipeFail, destroyOptionStream, noopDuplex(), 2, false);141test('Throws EPIPE when output stdio[*] option ends with more writes', testStreamEpipeFail, endOptionStream, noopWritable(), 3, false);142test('Throws EPIPE when output stdio[*] option aborts with more writes', testStreamEpipeFail, destroyOptionStream, noopWritable(), 3, false);143test('Throws EPIPE when output stdio[*] option Duplex aborts with more writes', testStreamEpipeFail, destroyOptionStream, noopDuplex(), 3, false);144test('Throws EPIPE when childProcess.stdout aborts with more writes', testStreamEpipeFail, destroyChildStream, noopWritable(), 1, false);145test('Throws EPIPE when childProcess.stdout Duplex aborts with more writes', testStreamEpipeFail, destroyChildStream, noopDuplex(), 1, false);146test('Throws EPIPE when childProcess.stderr aborts with more writes', testStreamEpipeFail, destroyChildStream, noopWritable(), 2, false);147test('Throws EPIPE when childProcess.stderr Duplex aborts with more writes', testStreamEpipeFail, destroyChildStream, noopDuplex(), 2, false);148test('Throws EPIPE when output childProcess.stdio[*] aborts with more writes', testStreamEpipeFail, destroyChildStream, noopWritable(), 3, false);149test('Throws EPIPE when output childProcess.stdio[*] Duplex aborts with more writes', testStreamEpipeFail, destroyChildStream, noopDuplex(), 3, false);150test('Throws EPIPE when stdout option ends with more writes, with a transform', testStreamEpipeFail, endOptionStream, noopWritable(), 1, true);151test('Throws EPIPE when stdout option aborts with more writes, with a transform', testStreamEpipeFail, destroyOptionStream, noopWritable(), 1, true);152test('Throws EPIPE when stdout option Duplex aborts with more writes, with a transform', testStreamEpipeFail, destroyOptionStream, noopDuplex(), 1, true);153test('Throws EPIPE when stderr option ends with more writes, with a transform', testStreamEpipeFail, endOptionStream, noopWritable(), 2, true);154test('Throws EPIPE when stderr option aborts with more writes, with a transform', testStreamEpipeFail, destroyOptionStream, noopWritable(), 2, true);155test('Throws EPIPE when stderr option Duplex aborts with more writes, with a transform', testStreamEpipeFail, destroyOptionStream, noopDuplex(), 2, true);156test('Throws EPIPE when output stdio[*] option ends with more writes, with a transform', testStreamEpipeFail, endOptionStream, noopWritable(), 3, true);157test('Throws EPIPE when output stdio[*] option aborts with more writes, with a transform', testStreamEpipeFail, destroyOptionStream, noopWritable(), 3, true);158test('Throws EPIPE when output stdio[*] option Duplex aborts with more writes, with a transform', testStreamEpipeFail, destroyOptionStream, noopDuplex(), 3, true);159test('Throws EPIPE when childProcess.stdout aborts with more writes, with a transform', testStreamEpipeFail, destroyChildStream, noopWritable(), 1, true);160test('Throws EPIPE when childProcess.stdout Duplex aborts with more writes, with a transform', testStreamEpipeFail, destroyChildStream, noopDuplex(), 1, true);
56 unmodified lines