Skip to content
Myr

stream.js

This file

Path

lib/stream.js

127 lines · 4.0 KiB · JavaScript

Last changed by

Improve performance of `childProcess.all` when `stdout` or `stderr` is `ignore` (#646)ehmickybefore adoption249e7c4

History of this file

Take it

Allow `stdin` to be a sync/async iterable (#604)ehmickybefore adoptionec380db

line 1why, one step back
1import {once} from 'node:events';

Upgrade `get-stream` (#571)ehmickybefore adoption88ee3bd

line 2why, one step back
2import {setTimeout} from 'node:timers/promises';

Support file path and URL types in the `stdout` and `stderr` options (#621)ehmickybefore adoptionb369efe

line 3why, one step back
3import {finished} from 'node:stream/promises';

Wait for `WritableStream` completion (#645)ehmickybefore adoptionfb73bd1

line 4why, one step back
4import process from 'node:process';

Make `buffer` encoding option return `Uint8Array` instead of `Buffer` (#586)Youngbefore adoption1afeb81

line 5why, one step back
5import getStream, {getStreamAsBuffer, getStreamAsArrayBuffer} from 'get-stream';

Require Node.js 18Sindre Sorhusbefore adoption834e372

line 6why, one step back
6import mergeStreams from '@sindresorhus/merge-streams';

Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40

lines 7–8why, one step back
7
8// `all` interleaves `stdout` and `stderr`

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 9why, one step back
9export const makeAllStream = (spawned, {all}) => {

Improve performance of `childProcess.all` when `stdout` or `stderr` is `ignore` (#646)ehmickybefore adoption249e7c4

line 10why, one step back
10 if (!all) {

Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40

lines 11–13why, one step back
11 return;
12 }13

Improve performance of `childProcess.all` when `stdout` or `stderr` is `ignore` (#646)ehmickybefore adoption249e7c4

lines 14–22why, one step back
14 if (!spawned.stdout) {
15 return spawned.stderr;16 }1718 if (!spawned.stderr) {19 return spawned.stdout;20 }2122 return mergeStreams([spawned.stdout, spawned.stderr]);

Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40

lines 23–26why, one step back
23};
2425// On failure, `result.stdout|stderr|all` should contain the currently buffered stream26const getBufferedData = async (stream, streamPromise) => {

Emit `end` event on streams when process fails (#518)ehmickybefore adoption30c7a7a

lines 27–28why, one step back
27 // When `buffer` is `false`, `streamPromise` is `undefined` and there is no buffered data to retrieve
28 if (!stream || streamPromise === undefined) {

Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40

lines 29–31why, one step back
29 return;
30 }31

Upgrade `get-stream` (#571)ehmickybefore adoption88ee3bd

lines 32–34why, one step back
32 // Wait for the `all` stream to receive the last chunk before destroying the stream
33 await setTimeout(0);34

Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40

lines 35–44why, one step back
35 stream.destroy();
3637 try {38 return await streamPromise;39 } catch (error) {40 return error.bufferedData;41 }42};4344const getStreamPromise = (stream, {encoding, buffer, maxBuffer}) => {

Add `all` option (#353)Tiago Nápolibefore adoption56b749d

line 45why, one step back
45 if (!stream || !buffer) {

Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40

lines 46–48why, one step back
46 return;
47 }48

Upgrade `get-stream` (#571)ehmickybefore adoption88ee3bd

lines 49–51why, one step back
49 // eslint-disable-next-line unicorn/text-encoding-identifier-case
50 if (encoding === 'utf8' || encoding === 'utf-8') {51 return getStream(stream, {maxBuffer});

Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40

lines 52–53why, one step back
52 }
53

Make `buffer` encoding option return `Uint8Array` instead of `Buffer` (#586)Youngbefore adoption1afeb81

lines 54–55why, one step back
54 if (encoding === 'buffer') {
55 return getStreamAsArrayBuffer(stream, {maxBuffer}).then(arrayBuffer => new Uint8Array(arrayBuffer));

Upgrade `get-stream` (#571)ehmickybefore adoption88ee3bd

lines 56–63why, one step back
56 }
5758 return applyEncoding(stream, maxBuffer, encoding);59};6061const applyEncoding = async (stream, maxBuffer, encoding) => {62 const buffer = await getStreamAsBuffer(stream, {maxBuffer});63 return buffer.toString(encoding);

Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40

lines 64–65why, one step back
64};
65

Wait for `WritableStream` completion (#645)ehmickybefore adoptionfb73bd1

lines 66–74why, one step back
66// Some `stdout`/`stderr` options create a stream, e.g. when passing a file path.
67// The `.pipe()` method automatically ends that stream when `childProcess.stdout|stderr` ends.68// This makes sure we want for the completion of those streams, in order to catch any error.69// Since we want to end those streams, they cannot be infinite, except for `process.stdout|stderr`.70// However, for the `stdin`/`input`/`inputFile` options, we only wait for errors, not completion.71// This is because source streams completion should end destination streams, but not the other way around.72// This allows for source streams to pipe to multiple destinations.73// We make an exception for `filePath`, since we create that source stream and we know it is piped to a single destination.74const waitForStreamEnd = ({value, type, direction}, processDone) => {

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

line 75why, one step back
75 if (type === 'native' || type === 'stringOrBuffer') {

Support file path and URL types in the `stdout` and `stderr` options (#621)ehmickybefore adoptionb369efe

lines 76–77why, one step back
76 return;
77 }

Allow `stdin` to be a sync/async iterable (#604)ehmickybefore adoptionec380db

line 78why, one step back
78

Wait for `WritableStream` completion (#645)ehmickybefore adoptionfb73bd1

line 79why, one step back
79 return type === 'filePath' || (direction === 'output' && value !== process.stdout && value !== process.stderr)

Support file path and URL types in the `stdout` and `stderr` options (#621)ehmickybefore adoptionb369efe

lines 80–83why, one step back
80 ? finished(value)
81 : Promise.race([processDone, throwOnStreamError(value)]);82};83

Allow `stdin` to be a sync/async iterable (#604)ehmickybefore adoptionec380db

lines 84–88why, one step back
84const throwOnStreamError = async stream => {
85 const [error] = await once(stream, 'error');86 throw error;87};88

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

lines 89–96why, one step back
89const cleanupStdioStreams = stdioStreams => {
90 for (const stdioStream of stdioStreams) {91 if (stdioStream.autoDestroy) {92 stdioStream.value.destroy();93 }94 }95};96

Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40

line 97why, one step back
97// Retrieve result of child process: exit code, signal, error, streams (stdout/stderr/all)

Support file path and URL types in the `stdout` and `stderr` options (#621)ehmickybefore adoptionb369efe

lines 98–103why, one step back
98export const getSpawnedResult = async (
99 spawned,100 {encoding, buffer, maxBuffer},101 stdioStreams,102 processDone,103) => {

Ensure process exits on stream errors (#607)ehmickybefore adoption2f89219

lines 104–106why, one step back
104 const stdoutPromise = getStreamPromise(spawned.stdout, {encoding, buffer, maxBuffer});
105 const stderrPromise = getStreamPromise(spawned.stderr, {encoding, buffer, maxBuffer});106 const allPromise = getStreamPromise(spawned.all, {encoding, buffer, maxBuffer: maxBuffer * 2});

Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40

lines 107–108why, one step back
107
108 try {

Support file path and URL types in the `stdout` and `stderr` options (#621)ehmickybefore adoptionb369efe

lines 109–115why, one step back
109 return await Promise.all([
110 processDone,111 stdoutPromise,112 stderrPromise,113 allPromise,114 ...stdioStreams.map(stdioStream => waitForStreamEnd(stdioStream, processDone)),115 ]);

Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40

lines 116–117why, one step back
116 } catch (error) {
117 return Promise.all([

Fix error properties (#375)ehmickybefore adoptionc1b511b

line 118why, one step back
118 {error, signal: error.signal, timedOut: error.timedOut},

Ensure process exits on stream errors (#607)ehmickybefore adoption2f89219

lines 119–121why, one step back
119 getBufferedData(spawned.stdout, stdoutPromise),
120 getBufferedData(spawned.stderr, stderrPromise),121 getBufferedData(spawned.all, allPromise),

Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40

line 122why, one step back
122 ]);

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

lines 123–125why, one step back
123 } finally {
124 cleanupStdioStreams(stdioStreams);125 spawned.kill();

Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40

lines 126–127why, one step back
126 }
127};