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 fileTake 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 back1import {once} from 'node:events';Support file path and URL types in the `stdout` and `stderr` options (#621)ehmickybefore adoptionb369efe
line 2why, one step back2import {finished} from 'node:stream/promises';Fix `error.stdout|stderr|all` type when `encoding` option is used (#652)ehmickybefore adoptioncff6847
line 3why, one step back3import getStream, {getStreamAsArrayBuffer} from 'get-stream';Require Node.js 18Sindre Sorhusbefore adoption834e372
line 4why, one step back4import mergeStreams from '@sindresorhus/merge-streams';Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767
line 5why, one step back5import {throwOnTimeout, cleanupOnExit} from './kill.js';Fix Node.js streams not being cleaned up on process error (#660)ehmickybefore adoption61a1623
line 6why, one step back6import {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 back78// `all` interleaves `stdout` and `stderr`Fix `all` option combined with `ignore` (#703)ehmickybefore adoption7707fc7
lines 9–11why, one step back9export 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 back1213// On failure, `result.stdout|stderr|all` should contain the currently buffered streamImprove error handling (#647)ehmickybefore adoption5b6d859
lines 14–15why, one step back14// They are automatically closed and flushed by Node.js when the child process exits15// 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 back16// When `buffer` is `false`, `streamPromise` is `undefined` and there is no buffered data to retrieve17const getBufferedData = async (streamPromise, encoding) => {Upgrade `npm-run-path` from 3.0.0 to 4.0.0 (#376)ehmickybefore adoption39c1968
lines 18–20why, one step back18 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 back21 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 back22 }23};24Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3
lines 25–27why, one step back25const 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 back28Fix `error.stdout|stderr|all` type when `encoding` option is used (#652)ehmickybefore adoptioncff6847
line 29why, one step back29const 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 back30 if (!stream || !buffer) {31 return;32 }33Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2
lines 34–36why, one step back34 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 back37 return applyEncoding(contents, encoding);38};39Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2
line 40why, one step back40const 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 back41Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767
line 42why, one step back42// Retrieve streams created by the `std*` optionsSimplify `std*` options logic (#691)ehmickybefore adoption98b0ee3
line 43why, one step back43const getCustomStreams = stdioStreamsGroups => stdioStreamsGroups.flat().filter(({type}) => type !== 'native');Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767
line 44why, one step back44Wait for `WritableStream` completion (#645)ehmickybefore adoptionfb73bd1
lines 45–51why, one step back45// 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 back52// 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 back53const waitForCustomStreamsEnd = customStreams => customStreams54 .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 back56Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767
lines 57–59why, one step back57const throwOnCustomStreamsError = customStreams => customStreams58 .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 back60Improve syntax of `std*: filePath` option (#671)ehmickybefore adoption90e0e13
lines 61–62why, one step back61const 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 back63Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767
lines 64–65why, one step back64const throwIfStreamError = stream => stream === null ? [] : [throwOnStreamError(stream)];65Allow `stdin` to be a sync/async iterable (#604)ehmickybefore adoptionec380db
lines 66–70why, one step back66const throwOnStreamError = async stream => {67 const [error] = await once(stream, 'error');68 throw error;69};70Improve error handling (#647)ehmickybefore adoption5b6d859
lines 71–73why, one step back71// 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 back74const cleanupStdioStreams = (customStreams, error) => {Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3
lines 75–77why, one step back75 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 back78 }79 }80};81Upgrade `npm-run-path` from 3.0.0 to 4.0.0 (#376)ehmickybefore adoption39c1968
line 82why, one step back82// 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 back83export const getSpawnedResult = async (84 spawned,Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767
lines 85–86why, one step back85 {encoding, buffer, maxBuffer, timeout, killSignal, cleanup, detached},86 context,Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3
line 87why, one step back87 stdioStreamsGroups,Support file path and URL types in the `stdout` and `stderr` options (#621)ehmickybefore adoptionb369efe
line 88why, one step back88) => {Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767
lines 89–90why, one step back89 const finalizers = [];90 cleanupOnExit(spawned, cleanup, detached, finalizers);Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3
line 91why, one step back91 const customStreams = getCustomStreams(stdioStreamsGroups);Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767
line 92why, one step back92Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3
line 93why, one step back93 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 back94 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 back9596 try {Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767
lines 97–99why, one step back97 return await Promise.race([98 Promise.all([99 once(spawned, 'exit'),Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207
line 100why, one step back100 Promise.all(stdioPromises),Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767
lines 101–106why, one step back101 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 back107 ]);Upgrade `npm-run-path` from 3.0.0 to 4.0.0 (#376)ehmickybefore adoption39c1968
line 108why, one step back108 } catch (error) {Improve error handling (#647)ehmickybefore adoption5b6d859
lines 109–110why, one step back109 spawned.kill();110 const results = await Promise.all([Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767
line 111why, one step back111 [undefined, context.signal, error],Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207
line 112why, one step back112 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 back113 getBufferedData(allPromise, encoding),Upgrade `npm-run-path` from 3.0.0 to 4.0.0 (#376)ehmickybefore adoption39c1968
line 114why, one step back114 ]);Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767
line 115why, one step back115 cleanupStdioStreams(customStreams, error);Improve error handling (#647)ehmickybefore adoption5b6d859
line 116why, one step back116 return results;Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767
lines 117–120why, one step back117 } 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 back121 }122};