Skip to content
Myr

Improve `buffer: false` behavior (#747)

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

authored by ehmicky · committed by GitHub · parent 74f4cc5 · Browse files · 5 files+66 added−47 removed
This commit

State

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

Diff stats

5 files+66 added−47 removed

  • tests:+46 added−24 removed
  • other:+20 added−23 removed

5 files+66 added−47 removed

modified:lib/stream.js+12 added-19 removed
added:test/fixtures/noop-fd-ipc.js+6 added
modified:test/stream.js+40 added-24 removed
1import {Buffer} from 'node:buffer';2import {once} from 'node:events';3import process from 'node:process';3import {getDefaultHighWaterMark} from 'node:stream';4import {setTimeout, setImmediate} from 'node:timers/promises';5import test from 'ava';6import getStream from 'get-stream';
76 unmodified lines
83 const [output] = await once(stream, 'data');84 return output.toString();85};8687const testLateStream = async (t, index, all) => {88 const subprocess = execa('noop-fd-ipc.js', [`${index}`, foobarString], {...getStdio(4, 'ipc', 4), buffer: false, all});89 await once(subprocess, 'message');90 await setImmediate();91 const [output, allOutput] = await Promise.all([92 getStream(subprocess.stdio[index]),93 all ? getStream(subprocess.all) : undefined,94 subprocess,95 ]);9697 t.is(output, '');9899 if (all) {100 t.is(allOutput, '');101 }102};103104test('Lacks some data when stdout is read too late `buffer` set to `false`', testLateStream, 1, false);105test('Lacks some data when stderr is read too late `buffer` set to `false`', testLateStream, 2, false);106test('Lacks some data when stdio[*] is read too late `buffer` set to `false`', testLateStream, 3, false);107test('Lacks some data when all is read too late `buffer` set to `false`', testLateStream, 1, true);108109// eslint-disable-next-line max-params110const testIterationBuffer = async (t, index, buffer, useDataEvents, all) => {111 const subprocess = execa('noop-fd.js', [`${index}`, foobarString], {...fullStdio, buffer, all});112 const getOutput = useDataEvents ? getFirstDataEvent : getStream;
175 unmodified lines
288test('Process buffers stdout, which does not prevent exit if ignored', testBufferIgnore, 1, false);289test('Process buffers stderr, which does not prevent exit if ignored', testBufferIgnore, 2, false);290test('Process buffers all, which does not prevent exit if ignored', testBufferIgnore, 1, true);291271// This specific behavior does not happen on Windows.272// Also, on macOS, it randomly happens, which would make those tests randomly fail.273if (process.platform === 'linux') {274 const testBufferNotRead = async (t, index, all) => {275 const subprocess = execa('max-buffer.js', [`${index}`], {...fullStdio, buffer: false, all, timeout: 1e3});276 const {timedOut} = await t.throwsAsync(subprocess);277 t.true(timedOut);292const testBufferNotRead = async (t, index, all) => {293 const subprocess = execa('max-buffer.js', [`${index}`], {...fullStdio, buffer: false, all});294 await t.notThrowsAsync(subprocess);295};278 };296280 test('Process buffers stdout, which prevents exit if not read and buffer is false', testBufferNotRead, 1, false);281 test('Process buffers stderr, which prevents exit if not read and buffer is false', testBufferNotRead, 2, false);282 test('Process buffers stdio[*], which prevents exit if not read and buffer is false', testBufferNotRead, 3, false);283 test('Process buffers all, which prevents exit if not read and buffer is false', testBufferNotRead, 1, true);297test('Process buffers stdout, which does not prevent exit if not read and buffer is false', testBufferNotRead, 1, false);298test('Process buffers stderr, which does not prevent exit if not read and buffer is false', testBufferNotRead, 2, false);299test('Process buffers stdio[*], which does not prevent exit if not read and buffer is false', testBufferNotRead, 3, false);300test('Process buffers all, which does not prevent exit if not read and buffer is false', testBufferNotRead, 1, true);301285 const testBufferRead = async (t, index, all) => {286 const subprocess = execa('max-buffer.js', [`${index}`], {...fullStdio, buffer: false, all, timeout: 1e4});287 const stream = all ? subprocess.all : subprocess.stdio[index];288 stream.resume();289 await t.notThrowsAsync(subprocess);290 };302const testBufferRead = async (t, index, all) => {303 const subprocess = execa('max-buffer.js', [`${index}`], {...fullStdio, buffer: false, all});304 const stream = all ? subprocess.all : subprocess.stdio[index];305 stream.resume();306 await t.notThrowsAsync(subprocess);307};308292 test.serial('Process buffers stdout, which does not prevent exit if read and buffer is false', testBufferRead, 1, false);293 test.serial('Process buffers stderr, which does not prevent exit if read and buffer is false', testBufferRead, 2, false);294 test.serial('Process buffers stdio[*], which does not prevent exit if read and buffer is false', testBufferRead, 3, false);295 test.serial('Process buffers all, which does not prevent exit if read and buffer is false', testBufferRead, 1, true);296}309test('Process buffers stdout, which does not prevent exit if read and buffer is false', testBufferRead, 1, false);310test('Process buffers stderr, which does not prevent exit if read and buffer is false', testBufferRead, 2, false);311test('Process buffers stdio[*], which does not prevent exit if read and buffer is false', testBufferRead, 3, false);312test('Process buffers all, which does not prevent exit if read and buffer is false', testBufferRead, 1, true);313314const testStreamDestroy = async (t, index) => {315 const childProcess = execa('forever.js', fullStdio);316 const error = new Error('test');
80 unmodified lines
modified:index.d.ts+4 added-2 removed
536 unmodified lines
537 */538 readonly cleanup?: IfAsync<IsSync, boolean>;539540 /**541 Buffer the output from the spawned process. When set to `false`, you must read the output of `stdout` and `stderr` (or `all` if the `all` option is `true`). Otherwise the returned promise will not be resolved/rejected.541 Whether to return the child process' output using the `result.stdout`, `result.stderr`, `result.all` and `result.stdio` properties.542543 If the spawned process fails, `error.stdout`, `error.stderr`, and `error.all` will contain the buffered data.543 On failure, the `error.stdout`, `error.stderr`, `error.all` and `error.stdio` properties are used instead.544545 When `buffer` is `false`, the output can still be read using the `childProcess.stdout`, `childProcess.stderr`, `childProcess.stdio` and `childProcess.all` streams. If the output is read, this should be done right away to avoid missing any data.546547 @default true548 */549 readonly buffer?: IfAsync<IsSync, boolean>;550
754 unmodified lines
modified:readme.md+4 added-2 removed
532 unmodified lines
533534Type: `boolean`\535Default: `true`536537Buffer the output from the spawned process. When set to `false`, you must read the output of [`stdout`](#stdout-1) and [`stderr`](#stderr-1) (or [`all`](#all) if the [`all`](#all-2) option is `true`). Otherwise the returned promise will not be resolved/rejected.537Whether to return the child process' output using the [`result.stdout`](#stdout), [`result.stderr`](#stderr), [`result.all`](#all-1) and [`result.stdio`](#stdio) properties.538539If the spawned process fails, [`error.stdout`](#stdout), [`error.stderr`](#stderr), [`error.all`](#all) and [`error.stdio`](#stdio) will contain the buffered data.539On failure, the [`error.stdout`](#stdout), [`error.stderr`](#stderr), [`error.all`](#all-1) and [`error.stdio`](#stdio) properties are used instead.540541When `buffer` is `false`, the output can still be read using the [`childProcess.stdout`](#stdout-1), [`childProcess.stderr`](#stderr-1), [`childProcess.stdio`](https://nodejs.org/api/child_process.html#subprocessstdio) and [`childProcess.all`](#all) streams. If the output is read, this should be done right away to avoid missing any data.542543#### input544545Type: `string | Uint8Array | stream.Readable`546
410 unmodified lines