stream.js
This file
Path
lib/stream.js
86 lines · 2.1 KiB · JavaScript
Last changed by
Drop support for Node 12 (#497)ehmickybefore adoptiona09cbc0
History of this fileTake it
Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
lines 1–3why, one step back1import {isStream} from 'is-stream';2import getStream from 'get-stream';3import mergeStream from 'merge-stream';Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40
lines 4–5why, one step back45// `input` optionRequire Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 6why, one step back6export const handleInput = (spawned, input) => {Drop support for Node 12 (#497)ehmickybefore adoptiona09cbc0
line 7why, one step back7 if (input === undefined) {Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40
lines 8–18why, one step back8 return;9 }1011 if (isStream(input)) {12 input.pipe(spawned.stdin);13 } else {14 spawned.stdin.end(input);15 }16};1718// `all` interleaves `stdout` and `stderr`Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 19why, one step back19export const makeAllStream = (spawned, {all}) => {Add `all` option (#353)Tiago Nápolibefore adoption56b749d
line 20why, one step back20 if (!all || (!spawned.stdout && !spawned.stderr)) {Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40
lines 21–52why, one step back21 return;22 }2324 const mixed = mergeStream();2526 if (spawned.stdout) {27 mixed.add(spawned.stdout);28 }2930 if (spawned.stderr) {31 mixed.add(spawned.stderr);32 }3334 return mixed;35};3637// On failure, `result.stdout|stderr|all` should contain the currently buffered stream38const getBufferedData = async (stream, streamPromise) => {39 if (!stream) {40 return;41 }4243 stream.destroy();4445 try {46 return await streamPromise;47 } catch (error) {48 return error.bufferedData;49 }50};5152const getStreamPromise = (stream, {encoding, buffer, maxBuffer}) => {Add `all` option (#353)Tiago Nápolibefore adoption56b749d
line 53why, one step back53 if (!stream || !buffer) {Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40
lines 54–64why, one step back54 return;55 }5657 if (encoding) {58 return getStream(stream, {encoding, maxBuffer});59 }6061 return getStream.buffer(stream, {maxBuffer});62};6364// Retrieve result of child process: exit code, signal, error, streams (stdout/stderr/all)Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 65why, one step back65export const getSpawnedResult = async ({stdout, stderr, all}, {encoding, buffer, maxBuffer}, processDone) => {Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40
lines 66–73why, one step back66 const stdoutPromise = getStreamPromise(stdout, {encoding, buffer, maxBuffer});67 const stderrPromise = getStreamPromise(stderr, {encoding, buffer, maxBuffer});68 const allPromise = getStreamPromise(all, {encoding, buffer, maxBuffer: maxBuffer * 2});6970 try {71 return await Promise.all([processDone, stdoutPromise, stderrPromise, allPromise]);72 } catch (error) {73 return Promise.all([Fix error properties (#375)ehmickybefore adoptionc1b511b
line 74why, one step back74 {error, signal: error.signal, timedOut: error.timedOut},Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40
lines 75–76why, one step back75 getBufferedData(stdout, stdoutPromise),76 getBufferedData(stderr, stderrPromise),Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 77why, one step back77 getBufferedData(all, allPromise),Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40
lines 78–81why, one step back78 ]);79 }80};81Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 82why, one step back82export const validateInputSync = ({input}) => {Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40
lines 83–86why, one step back83 if (isStream(input)) {84 throw new TypeError('The `input` option cannot be a stream in sync mode');85 }86};