Wait for `WritableStream` completion (#645)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 2500696 · Browse files · 2 files·+28 added−6 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+28 added−6 removed
- tests:+17 added−0 removed
- other:+11 added−6 removed
Provenance
- Commit
fb73bd1- Tree
a19f388- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
2500696
2 files changed+28 added−6 removed
modified:lib/stream.js+11 added-6 removed
1234567845 unmodified lines5455565757585960615859606162636465666768697066717273747544 unmodified linesimport {once} from 'node:events';import {setTimeout} from 'node:timers/promises';import {finished} from 'node:stream/promises';import process from 'node:process';import getStream, {getStreamAsBuffer, getStreamAsArrayBuffer} from 'get-stream';import mergeStreams from '@sindresorhus/merge-streams';
// `all` interleaves `stdout` and `stderr`45 unmodified lines const buffer = await getStreamAsBuffer(stream, {maxBuffer}); return buffer.toString(encoding);};
// We need to handle any `error` coming from the `stdin|stdout|stderr` options.// However, those might be infinite streams, e.g. a TTY passed as input or output.// We wait for completion or not depending on whether `finite` is `true`.// In either case, we handle `error` events while the process is running.const waitForStreamEnd = ({value, type}, processDone) => {// Some `stdout`/`stderr` options create a stream, e.g. when passing a file path.// The `.pipe()` method automatically ends that stream when `childProcess.stdout|stderr` ends.// This makes sure we want for the completion of those streams, in order to catch any error.// Since we want to end those streams, they cannot be infinite, except for `process.stdout|stderr`.// However, for the `stdin`/`input`/`inputFile` options, we only wait for errors, not completion.// This is because source streams completion should end destination streams, but not the other way around.// This allows for source streams to pipe to multiple destinations.// We make an exception for `filePath`, since we create that source stream and we know it is piped to a single destination.const waitForStreamEnd = ({value, type, direction}, processDone) => { if (type === 'native' || type === 'stringOrBuffer') { return; }
return type === 'filePath' return type === 'filePath' || (direction === 'output' && value !== process.stdout && value !== process.stderr) ? finished(value) : Promise.race([processDone, throwOnStreamError(value)]);};
44 unmodified linesmodified:test/stdio/web-stream.js+17 added
12345635 unmodified lines4243444546474849505152535455565758596061626364659 unmodified linesimport {Readable} from 'node:stream';import {setTimeout} from 'node:timers/promises';import test from 'ava';import {execa, execaSync} from '../../index.js';import {setFixtureDir} from '../helpers/fixtures-dir.js';import {getStdinOption, getStdoutOption, getStderrOption, getStdioOption} from '../helpers/stdio.js';35 unmodified linestest('stdout cannot be a WritableStream - sync', testWebStreamSync, WritableStream, getStdoutOption, 'stdout');test('stderr cannot be a WritableStream - sync', testWebStreamSync, WritableStream, getStderrOption, 'stderr');test('stdio[*] cannot be a WritableStream - sync', testWebStreamSync, WritableStream, getStdioOption, 'stdio[3]');
const testLongWritableStream = async (t, getOptions) => { let result = false; const writableStream = new WritableStream({ async close() { await setTimeout(0); result = true; }, }); await execa('empty.js', getOptions(writableStream)); t.true(result);};
test('stdout waits for WritableStream completion', testLongWritableStream, getStdoutOption);test('stderr waits for WritableStream completion', testLongWritableStream, getStderrOption);test('stdio[*] waits for WritableStream completion', testLongWritableStream, getStdioOption);
const testWritableStreamError = async (t, getOptions) => { const writableStream = new WritableStream({ start(controller) { controller.error(new Error('foobar'));9 unmodified lines