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 fileTake it
Allow `stdin` to be a sync/async iterable (#604)ehmickybefore adoptionec380db
line 1why, one step back1import {once} from 'node:events';Upgrade `get-stream` (#571)ehmickybefore adoption88ee3bd
line 2why, one step back2import {setTimeout} from 'node:timers/promises';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';Make `buffer` encoding option return `Uint8Array` instead of `Buffer` (#586)Youngbefore adoption1afeb81
line 5why, one step back5import getStream, {getStreamAsBuffer, getStreamAsArrayBuffer} from 'get-stream';Require Node.js 18Sindre Sorhusbefore adoption834e372
line 6why, one step back6import mergeStreams from '@sindresorhus/merge-streams';Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40
lines 7–8why, one step back78// `all` interleaves `stdout` and `stderr`Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 9why, one step back9export const makeAllStream = (spawned, {all}) => {Improve performance of `childProcess.all` when `stdout` or `stderr` is `ignore` (#646)ehmickybefore adoption249e7c4
line 10why, one step back10 if (!all) {Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40
lines 11–13why, one step back11 return;12 }13Improve performance of `childProcess.all` when `stdout` or `stderr` is `ignore` (#646)ehmickybefore adoption249e7c4
lines 14–22why, one step back14 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 back23};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 back27 // When `buffer` is `false`, `streamPromise` is `undefined` and there is no buffered data to retrieve28 if (!stream || streamPromise === undefined) {Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40
lines 29–31why, one step back29 return;30 }31Upgrade `get-stream` (#571)ehmickybefore adoption88ee3bd
lines 32–34why, one step back32 // Wait for the `all` stream to receive the last chunk before destroying the stream33 await setTimeout(0);34Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40
lines 35–44why, one step back35 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 back45 if (!stream || !buffer) {Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40
lines 46–48why, one step back46 return;47 }48Upgrade `get-stream` (#571)ehmickybefore adoption88ee3bd
lines 49–51why, one step back49 // eslint-disable-next-line unicorn/text-encoding-identifier-case50 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 back52 }53Make `buffer` encoding option return `Uint8Array` instead of `Buffer` (#586)Youngbefore adoption1afeb81
lines 54–55why, one step back54 if (encoding === 'buffer') {55 return getStreamAsArrayBuffer(stream, {maxBuffer}).then(arrayBuffer => new Uint8Array(arrayBuffer));Upgrade `get-stream` (#571)ehmickybefore adoption88ee3bd
lines 56–63why, one step back56 }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 back64};65Wait for `WritableStream` completion (#645)ehmickybefore adoptionfb73bd1
lines 66–74why, one step back66// 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 back75 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 back76 return;77 }Allow `stdin` to be a sync/async iterable (#604)ehmickybefore adoptionec380db
line 78why, one step back78Wait for `WritableStream` completion (#645)ehmickybefore adoptionfb73bd1
line 79why, one step back79 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 back80 ? finished(value)81 : Promise.race([processDone, throwOnStreamError(value)]);82};83Allow `stdin` to be a sync/async iterable (#604)ehmickybefore adoptionec380db
lines 84–88why, one step back84const throwOnStreamError = async stream => {85 const [error] = await once(stream, 'error');86 throw error;87};88Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
lines 89–96why, one step back89const cleanupStdioStreams = stdioStreams => {90 for (const stdioStream of stdioStreams) {91 if (stdioStream.autoDestroy) {92 stdioStream.value.destroy();93 }94 }95};96Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40
line 97why, one step back97// 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 back98export 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 back104 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 back107108 try {Support file path and URL types in the `stdout` and `stderr` options (#621)ehmickybefore adoptionb369efe
lines 109–115why, one step back109 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 back116 } catch (error) {117 return Promise.all([Fix error properties (#375)ehmickybefore adoptionc1b511b
line 118why, one step back118 {error, signal: error.signal, timedOut: error.timedOut},Ensure process exits on stream errors (#607)ehmickybefore adoption2f89219
lines 119–121why, one step back119 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 back122 ]);Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
lines 123–125why, one step back123 } finally {124 cleanupStdioStreams(stdioStreams);125 spawned.kill();Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40
lines 126–127why, one step back126 }127};