Skip to content
Myr

stream.js

This file

Path

lib/stream.js

97 lines · 2.3 KiB · JavaScript

Last changed by

Fix error properties (#375)ehmickybefore adoptionc1b511b

History of this file

Take it

Add missing 'use strict' (#326)ehmickybefore adoptioncba87e1

line 1why, one step back
1'use strict';

Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40

lines 2–21why, one step back
2const isStream = require('is-stream');
3const getStream = require('get-stream');4const mergeStream = require('merge-stream');56// `input` option7const handleInput = (spawned, input) => {8 // Checking for stdin is workaround for https://github.com/nodejs/node/issues/268529 // TODO: Remove `|| spawned.stdin === undefined` once we drop support for Node.js <=12.2.010 if (input === undefined || spawned.stdin === undefined) {11 return;12 }1314 if (isStream(input)) {15 input.pipe(spawned.stdin);16 } else {17 spawned.stdin.end(input);18 }19};2021// `all` interleaves `stdout` and `stderr`

Add `all` option (#353)Tiago Nápolibefore adoption56b749d

lines 22–23why, one step back
22const makeAllStream = (spawned, {all}) => {
23 if (!all || (!spawned.stdout && !spawned.stderr)) {

Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40

lines 24–55why, one step back
24 return;
25 }2627 const mixed = mergeStream();2829 if (spawned.stdout) {30 mixed.add(spawned.stdout);31 }3233 if (spawned.stderr) {34 mixed.add(spawned.stderr);35 }3637 return mixed;38};3940// On failure, `result.stdout|stderr|all` should contain the currently buffered stream41const getBufferedData = async (stream, streamPromise) => {42 if (!stream) {43 return;44 }4546 stream.destroy();4748 try {49 return await streamPromise;50 } catch (error) {51 return error.bufferedData;52 }53};5455const getStreamPromise = (stream, {encoding, buffer, maxBuffer}) => {

Add `all` option (#353)Tiago Nápolibefore adoption56b749d

line 56why, one step back
56 if (!stream || !buffer) {

Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40

lines 57–76why, one step back
57 return;
58 }5960 if (encoding) {61 return getStream(stream, {encoding, maxBuffer});62 }6364 return getStream.buffer(stream, {maxBuffer});65};6667// Retrieve result of child process: exit code, signal, error, streams (stdout/stderr/all)68const getSpawnedResult = async ({stdout, stderr, all}, {encoding, buffer, maxBuffer}, processDone) => {69 const stdoutPromise = getStreamPromise(stdout, {encoding, buffer, maxBuffer});70 const stderrPromise = getStreamPromise(stderr, {encoding, buffer, maxBuffer});71 const allPromise = getStreamPromise(all, {encoding, buffer, maxBuffer: maxBuffer * 2});7273 try {74 return await Promise.all([processDone, stdoutPromise, stderrPromise, allPromise]);75 } catch (error) {76 return Promise.all([

Fix error properties (#375)ehmickybefore adoptionc1b511b

line 77why, one step back
77 {error, signal: error.signal, timedOut: error.timedOut},

Separate stream-related logic into their own file (#324)ehmickybefore adoption2b06e40

lines 78–97why, one step back
78 getBufferedData(stdout, stdoutPromise),
79 getBufferedData(stderr, stderrPromise),80 getBufferedData(all, allPromise)81 ]);82 }83};8485const validateInputSync = ({input}) => {86 if (isStream(input)) {87 throw new TypeError('The `input` option cannot be a stream in sync mode');88 }89};9091module.exports = {92 handleInput,93 makeAllStream,94 getSpawnedResult,95 validateInputSync96};97