Skip to content
Myr

stream.js

This file

Path

lib/stream.js

147 lines · 5.3 KiB · JavaScript

Last changed by

Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767

History of this file

Take it

Fix `error.stdout|stderr|all` type when `encoding` option is used (#652)ehmickybefore adoptioncff6847

line 1why, one step back
1import {Buffer} from 'node:buffer';

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

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

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';

Fix `error.stdout|stderr|all` type when `encoding` option is used (#652)ehmickybefore adoptioncff6847

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

Require Node.js 18Sindre Sorhusbefore adoption834e372

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

Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767

line 7why, one step back
7import {throwOnTimeout, cleanupOnExit} from './kill.js';

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

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

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

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

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

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

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

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

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

lines 15–23why, one step back
15 if (!spawned.stdout) {
16 return spawned.stderr;17 }1819 if (!spawned.stderr) {20 return spawned.stdout;21 }2223 return mergeStreams([spawned.stdout, spawned.stderr]);

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

lines 24–26why, one step back
24};
2526// On failure, `result.stdout|stderr|all` should contain the currently buffered stream

Improve error handling (#647)ehmickybefore adoption5b6d859

lines 27–28why, one step back
27// They are automatically closed and flushed by Node.js when the child process exits
28// We guarantee this by calling `childProcess.kill()`

Fix `error.stdout|stderr|all` type when `encoding` option is used (#652)ehmickybefore adoptioncff6847

lines 29–30why, one step back
29// When `buffer` is `false`, `streamPromise` is `undefined` and there is no buffered data to retrieve
30const getBufferedData = async (streamPromise, encoding) => {

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

lines 31–33why, one step back
31 try {
32 return await streamPromise;33 } catch (error) {

Fix `error.stdout|stderr|all` type when `encoding` option is used (#652)ehmickybefore adoptioncff6847

line 34why, one step back
34 return error.bufferedData === undefined ? undefined : applyEncoding(error.bufferedData, encoding);

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

lines 35–37why, one step back
35 }
36};37

Fix `error.stdout|stderr|all` type when `encoding` option is used (#652)ehmickybefore adoptioncff6847

line 38why, one step back
38const getStreamPromise = async (stream, {encoding, buffer, maxBuffer}) => {

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

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

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

lines 40–42why, one step back
40 return;
41 }42

Fix `error.stdout|stderr|all` type when `encoding` option is used (#652)ehmickybefore adoptioncff6847

lines 43–51why, one step back
43 const contents = isUtf8Encoding(encoding)
44 ? await getStream(stream, {maxBuffer})45 : await getStreamAsArrayBuffer(stream, {maxBuffer});46 return applyEncoding(contents, encoding);47};4849const applyEncoding = (contents, encoding) => {50 if (isUtf8Encoding(encoding)) {51 return contents;

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

line 54why, one step back
54 if (encoding === 'buffer') {

Fix `error.stdout|stderr|all` type when `encoding` option is used (#652)ehmickybefore adoptioncff6847

line 55why, one step back
55 return new Uint8Array(contents);

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

lines 56–57why, one step back
56 }
57

Fix `error.stdout|stderr|all` type when `encoding` option is used (#652)ehmickybefore adoptioncff6847

line 58why, one step back
58 return Buffer.from(contents).toString(encoding);

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

lines 59–60why, one step back
59};
60

Fix `error.stdout|stderr|all` type when `encoding` option is used (#652)ehmickybefore adoptioncff6847

lines 61–62why, one step back
61// eslint-disable-next-line unicorn/text-encoding-identifier-case
62const isUtf8Encoding = encoding => encoding === 'utf8' || encoding === 'utf-8';

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

line 63why, one step back
63

Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767

lines 64–66why, one step back
64// Retrieve streams created by the `std*` options
65const getCustomStreams = stdioStreams => stdioStreams.filter(({type}) => type !== 'native' && type !== 'stringOrBuffer');66

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

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

Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767

lines 75–77why, one step back
75const waitForCustomStreamsEnd = customStreams => customStreams
76 .filter(({value, type, direction}) => shouldWaitForCustomStream(value, type, direction))77 .map(({value}) => finished(value));

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

line 78why, one step back
78

Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767

lines 79–81why, one step back
79const throwOnCustomStreamsError = customStreams => customStreams
80 .filter(({value, type, direction}) => !shouldWaitForCustomStream(value, type, direction))81 .map(({value}) => throwOnStreamError(value));

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

line 82why, one step back
82

Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767

lines 83–84why, one step back
83const shouldWaitForCustomStream = (value, type, direction) => type === 'filePath'
84 || (direction === 'output' && value !== process.stdout && value !== process.stderr);

Refactor execa promise to use `async/await` (#653)ehmickybefore adoptiond69fd23

line 85why, one step back
85

Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767

lines 86–87why, one step back
86const throwIfStreamError = stream => stream === null ? [] : [throwOnStreamError(stream)];
87

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

lines 88–92why, one step back
88const throwOnStreamError = async stream => {
89 const [error] = await once(stream, 'error');90 throw error;91};92

Improve error handling (#647)ehmickybefore adoption5b6d859

lines 93–95why, one step back
93// The streams created by the `std*` options are automatically ended by `.pipe()`.
94// However `.pipe()` only does so when the source stream ended, not when it errored.95// Therefore, when `childProcess.stdin|stdout|stderr` errors, those streams must be manually destroyed.

Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767

lines 96–99why, one step back
96const cleanupStdioStreams = (customStreams, error) => {
97 for (const customStream of customStreams) {98 if (customStream.autoDestroy) {99 customStream.value.destroy(error);

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

lines 100–103why, one step back
100 }
101 }102};103

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

line 104why, one step back
104// 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 105–106why, one step back
105export const getSpawnedResult = async (
106 spawned,

Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767

lines 107–108why, one step back
107 {encoding, buffer, maxBuffer, timeout, killSignal, cleanup, detached},
108 context,

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

lines 109–110why, one step back
109 stdioStreams,
110) => {

Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767

lines 111–114why, one step back
111 const finalizers = [];
112 cleanupOnExit(spawned, cleanup, detached, finalizers);113 const customStreams = getCustomStreams(stdioStreams);114

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

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

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

lines 118–119why, one step back
118
119 try {

Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767

lines 120–130why, one step back
120 return await Promise.race([
121 Promise.all([122 once(spawned, 'exit'),123 stdoutPromise,124 stderrPromise,125 allPromise,126 ...waitForCustomStreamsEnd(customStreams),127 ]),128 ...throwOnCustomStreamsError(customStreams),129 ...throwIfStreamError(spawned.stdin),130 ...throwOnTimeout({spawned, timeout, killSignal, context, finalizers}),

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

line 131why, one step back
131 ]);

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

line 132why, one step back
132 } catch (error) {

Improve error handling (#647)ehmickybefore adoption5b6d859

lines 133–134why, one step back
133 spawned.kill();
134 const results = await Promise.all([

Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767

line 135why, one step back
135 [undefined, context.signal, error],

Fix `error.stdout|stderr|all` type when `encoding` option is used (#652)ehmickybefore adoptioncff6847

lines 136–138why, one step back
136 getBufferedData(stdoutPromise, encoding),
137 getBufferedData(stderrPromise, encoding),138 getBufferedData(allPromise, encoding),

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

line 139why, one step back
139 ]);

Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767

line 140why, one step back
140 cleanupStdioStreams(customStreams, error);

Improve error handling (#647)ehmickybefore adoption5b6d859

line 141why, one step back
141 return results;

Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767

lines 142–145why, one step back
142 } finally {
143 for (const finalizer of finalizers) {144 finalizer();145 }

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

lines 146–147why, one step back
146 }
147};