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 fileTake it
Fix `error.stdout|stderr|all` type when `encoding` option is used (#652)ehmickybefore adoptioncff6847
line 1why, one step back1import {Buffer} from 'node:buffer';Allow `stdin` to be a sync/async iterable (#604)ehmickybefore adoptionec380db
line 2why, one step back2import {once} from 'node:events';Support file path and URL types in the `stdout` and `stderr` options (#621)ehmickybefore adoptionb369efe
line 3why, one step back3import {finished} from 'node:stream/promises';Wait for `WritableStream` completion (#645)ehmickybefore adoptionfb73bd1
line 4why, one step back4import process from 'node:process';Fix `error.stdout|stderr|all` type when `encoding` option is used (#652)ehmickybefore adoptioncff6847
line 5why, one step back5import getStream, {getStreamAsArrayBuffer} from 'get-stream';Require Node.js 18Sindre Sorhusbefore adoption834e372
line 6why, one step back6import mergeStreams from '@sindresorhus/merge-streams';Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767
line 7why, one step back7import {throwOnTimeout, cleanupOnExit} from './kill.js';Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40
lines 8–9why, one step back89// `all` interleaves `stdout` and `stderr`Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 10why, one step back10export const makeAllStream = (spawned, {all}) => {Improve performance of `childProcess.all` when `stdout` or `stderr` is `ignore` (#646)ehmickybefore adoption249e7c4
line 11why, one step back11 if (!all) {Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40
lines 12–14why, one step back12 return;13 }14Improve performance of `childProcess.all` when `stdout` or `stderr` is `ignore` (#646)ehmickybefore adoption249e7c4
lines 15–23why, one step back15 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 back24};2526// On failure, `result.stdout|stderr|all` should contain the currently buffered streamImprove error handling (#647)ehmickybefore adoption5b6d859
lines 27–28why, one step back27// They are automatically closed and flushed by Node.js when the child process exits28// 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 back29// When `buffer` is `false`, `streamPromise` is `undefined` and there is no buffered data to retrieve30const getBufferedData = async (streamPromise, encoding) => {Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40
lines 31–33why, one step back31 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 back34 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 back35 }36};37Fix `error.stdout|stderr|all` type when `encoding` option is used (#652)ehmickybefore adoptioncff6847
line 38why, one step back38const getStreamPromise = async (stream, {encoding, buffer, maxBuffer}) => {Add `all` option (#353)Tiago Nápolibefore adoption56b749d
line 39why, one step back39 if (!stream || !buffer) {Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40
lines 40–42why, one step back40 return;41 }42Fix `error.stdout|stderr|all` type when `encoding` option is used (#652)ehmickybefore adoptioncff6847
lines 43–51why, one step back43 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 back52 }53Make `buffer` encoding option return `Uint8Array` instead of `Buffer` (#586)Youngbefore adoption1afeb81
line 54why, one step back54 if (encoding === 'buffer') {Fix `error.stdout|stderr|all` type when `encoding` option is used (#652)ehmickybefore adoptioncff6847
line 55why, one step back55 return new Uint8Array(contents);Upgrade `get-stream` (#571)ehmickybefore adoption88ee3bd
lines 56–57why, one step back56 }57Fix `error.stdout|stderr|all` type when `encoding` option is used (#652)ehmickybefore adoptioncff6847
line 58why, one step back58 return Buffer.from(contents).toString(encoding);Upgrade `get-stream` (#571)ehmickybefore adoption88ee3bd
lines 59–60why, one step back59};60Fix `error.stdout|stderr|all` type when `encoding` option is used (#652)ehmickybefore adoptioncff6847
lines 61–62why, one step back61// eslint-disable-next-line unicorn/text-encoding-identifier-case62const isUtf8Encoding = encoding => encoding === 'utf8' || encoding === 'utf-8';Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40
line 63why, one step back63Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767
lines 64–66why, one step back64// Retrieve streams created by the `std*` options65const getCustomStreams = stdioStreams => stdioStreams.filter(({type}) => type !== 'native' && type !== 'stringOrBuffer');66Wait for `WritableStream` completion (#645)ehmickybefore adoptionfb73bd1
lines 67–74why, one step back67// 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 back75const waitForCustomStreamsEnd = customStreams => customStreams76 .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 back78Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767
lines 79–81why, one step back79const throwOnCustomStreamsError = customStreams => customStreams80 .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 back82Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767
lines 83–84why, one step back83const 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 back85Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767
lines 86–87why, one step back86const throwIfStreamError = stream => stream === null ? [] : [throwOnStreamError(stream)];87Allow `stdin` to be a sync/async iterable (#604)ehmickybefore adoptionec380db
lines 88–92why, one step back88const throwOnStreamError = async stream => {89 const [error] = await once(stream, 'error');90 throw error;91};92Improve error handling (#647)ehmickybefore adoption5b6d859
lines 93–95why, one step back93// 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 back96const 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 back100 }101 }102};103Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40
line 104why, one step back104// 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 back105export const getSpawnedResult = async (106 spawned,Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767
lines 107–108why, one step back107 {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 back109 stdioStreams,110) => {Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767
lines 111–114why, one step back111 const finalizers = [];112 cleanupOnExit(spawned, cleanup, detached, finalizers);113 const customStreams = getCustomStreams(stdioStreams);114Ensure process exits on stream errors (#607)ehmickybefore adoption2f89219
lines 115–117why, one step back115 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 back118119 try {Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767
lines 120–130why, one step back120 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 back131 ]);Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40
line 132why, one step back132 } catch (error) {Improve error handling (#647)ehmickybefore adoption5b6d859
lines 133–134why, one step back133 spawned.kill();134 const results = await Promise.all([Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767
line 135why, one step back135 [undefined, context.signal, error],Fix `error.stdout|stderr|all` type when `encoding` option is used (#652)ehmickybefore adoptioncff6847
lines 136–138why, one step back136 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 back139 ]);Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767
line 140why, one step back140 cleanupStdioStreams(customStreams, error);Improve error handling (#647)ehmickybefore adoption5b6d859
line 141why, one step back141 return results;Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767
lines 142–145why, one step back142 } 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 back146 }147};