Skip to content
Myr

stream.js

This file

Path

lib/stream.js

122 lines · 5.1 KiB · JavaScript

Last changed by

Fix `all` option combined with `ignore` (#703)ehmickybefore adoption7707fc7

History of this file

Take it

The walk stopped after 200 commits: lines older than 39c1968 point at it.

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

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

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

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

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

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

Require Node.js 18Sindre Sorhusbefore adoption834e372

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

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

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

Fix Node.js streams not being cleaned up on process error (#660)ehmickybefore adoption61a1623

line 6why, one step back
6import {STANDARD_STREAMS} from './stdio/native.js';

Upgrade `npm-run-path` from 3.0.0 to 4.0.0 (#376)ehmickybefore adoption39c1968

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

Fix `all` option combined with `ignore` (#703)ehmickybefore adoption7707fc7

lines 9–11why, one step back
9export const makeAllStream = ({stdout, stderr}, {all}) => all && (stdout || stderr)
10 ? mergeStreams([stdout, stderr].filter(Boolean))11 : undefined;

Upgrade `npm-run-path` from 3.0.0 to 4.0.0 (#376)ehmickybefore adoption39c1968

lines 12–13why, one step back
12
13// On failure, `result.stdout|stderr|all` should contain the currently buffered stream

Improve error handling (#647)ehmickybefore adoption5b6d859

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

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

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

Upgrade `npm-run-path` from 3.0.0 to 4.0.0 (#376)ehmickybefore adoption39c1968

lines 18–20why, one step back
18 try {
19 return await streamPromise;20 } catch (error) {

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

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

Upgrade `npm-run-path` from 3.0.0 to 4.0.0 (#376)ehmickybefore adoption39c1968

lines 22–24why, one step back
22 }
23};24

Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3

lines 25–27why, one step back
25const getStdioPromise = ({stream, index, stdioStreamsGroups, encoding, buffer, maxBuffer}) => stdioStreamsGroups[index]?.[0]?.direction === 'output'
26 ? getStreamPromise(stream, {encoding, buffer, maxBuffer})27 : undefined;

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 28why, one step back
28

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

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

Upgrade `npm-run-path` from 3.0.0 to 4.0.0 (#376)ehmickybefore adoption39c1968

lines 30–33why, one step back
30 if (!stream || !buffer) {
31 return;32 }33

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

lines 34–36why, one step back
34 const contents = encoding === 'buffer'
35 ? await getStreamAsArrayBuffer(stream, {maxBuffer})36 : await getStream(stream, {maxBuffer});

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

lines 37–39why, one step back
37 return applyEncoding(contents, encoding);
38};39

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

line 40why, one step back
40const applyEncoding = (contents, encoding) => encoding === 'buffer' ? new Uint8Array(contents) : contents;

Upgrade `npm-run-path` from 3.0.0 to 4.0.0 (#376)ehmickybefore adoption39c1968

line 41why, one step back
41

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

line 42why, one step back
42// Retrieve streams created by the `std*` options

Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3

line 43why, one step back
43const getCustomStreams = stdioStreamsGroups => stdioStreamsGroups.flat().filter(({type}) => type !== 'native');

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

line 44why, one step back
44

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

lines 45–51why, one step back
45// Some `stdout`/`stderr` options create a stream, e.g. when passing a file path.
46// The `.pipe()` method automatically ends that stream when `childProcess.stdout|stderr` ends.47// This makes sure we want for the completion of those streams, in order to catch any error.48// Since we want to end those streams, they cannot be infinite, except for `process.stdout|stderr`.49// However, for the `stdin`/`input`/`inputFile` options, we only wait for errors, not completion.50// This is because source streams completion should end destination streams, but not the other way around.51// This allows for source streams to pipe to multiple destinations.

Improve syntax of `std*: filePath` option (#671)ehmickybefore adoption90e0e13

line 52why, one step back
52// We make an exception for `fileUrl` and `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 53–55why, one step back
53const waitForCustomStreamsEnd = customStreams => customStreams
54 .filter(({value, type, direction}) => shouldWaitForCustomStream(value, type, direction))55 .map(({value}) => finished(value));

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

line 56why, one step back
56

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

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

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

line 60why, one step back
60

Improve syntax of `std*: filePath` option (#671)ehmickybefore adoption90e0e13

lines 61–62why, one step back
61const shouldWaitForCustomStream = (value, type, direction) => (type === 'fileUrl' || type === 'filePath')
62 || (direction === 'output' && !STANDARD_STREAMS.includes(value));

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

line 63why, one step back
63

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

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

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

lines 66–70why, one step back
66const throwOnStreamError = async stream => {
67 const [error] = await once(stream, 'error');68 throw error;69};70

Improve error handling (#647)ehmickybefore adoption5b6d859

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

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

line 74why, one step back
74const cleanupStdioStreams = (customStreams, error) => {

Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3

lines 75–77why, one step back
75 for (const {value} of customStreams) {
76 if (!STANDARD_STREAMS.includes(value)) {77 value.destroy(error);

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

lines 78–81why, one step back
78 }
79 }80};81

Upgrade `npm-run-path` from 3.0.0 to 4.0.0 (#376)ehmickybefore adoption39c1968

line 82why, one step back
82// 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 83–84why, one step back
83export const getSpawnedResult = async (
84 spawned,

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

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

Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3

line 87why, one step back
87 stdioStreamsGroups,

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

line 88why, one step back
88) => {

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

lines 89–90why, one step back
89 const finalizers = [];
90 cleanupOnExit(spawned, cleanup, detached, finalizers);

Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3

line 91why, one step back
91 const customStreams = getCustomStreams(stdioStreamsGroups);

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

line 92why, one step back
92

Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3

line 93why, one step back
93 const stdioPromises = spawned.stdio.map((stream, index) => getStdioPromise({stream, index, stdioStreamsGroups, encoding, buffer, maxBuffer}));

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

line 94why, one step back
94 const allPromise = getStreamPromise(spawned.all, {encoding, buffer, maxBuffer: maxBuffer * 2});

Upgrade `npm-run-path` from 3.0.0 to 4.0.0 (#376)ehmickybefore adoption39c1968

lines 95–96why, one step back
95
96 try {

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

lines 97–99why, one step back
97 return await Promise.race([
98 Promise.all([99 once(spawned, 'exit'),

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 100why, one step back
100 Promise.all(stdioPromises),

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

lines 101–106why, one step back
101 allPromise,
102 ...waitForCustomStreamsEnd(customStreams),103 ]),104 ...throwOnCustomStreamsError(customStreams),105 ...throwIfStreamError(spawned.stdin),106 ...throwOnTimeout({spawned, timeout, killSignal, context, finalizers}),

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

line 107why, one step back
107 ]);

Upgrade `npm-run-path` from 3.0.0 to 4.0.0 (#376)ehmickybefore adoption39c1968

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

Improve error handling (#647)ehmickybefore adoption5b6d859

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

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

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

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 112why, one step back
112 Promise.all(stdioPromises.map(stdioPromise => getBufferedData(stdioPromise, encoding))),

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

line 113why, one step back
113 getBufferedData(allPromise, encoding),

Upgrade `npm-run-path` from 3.0.0 to 4.0.0 (#376)ehmickybefore adoption39c1968

line 114why, one step back
114 ]);

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

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

Improve error handling (#647)ehmickybefore adoption5b6d859

line 116why, one step back
116 return results;

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

lines 117–120why, one step back
117 } finally {
118 for (const finalizer of finalizers) {119 finalizer();120 }

Upgrade `npm-run-path` from 3.0.0 to 4.0.0 (#376)ehmickybefore adoption39c1968

lines 121–122why, one step back
121 }
122};