subprocess.js
This file
Path
lib/stream/subprocess.js
86 lines · 2.8 KiB · JavaScript
Last changed by
Fix `lines` option (#957)ehmickybefore adoption15ab4e6
History of this fileTake it
Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
lines 1–2why, one step back1import {setImmediate} from 'node:timers/promises';2import getStream, {getStreamAsArrayBuffer, getStreamAsArray, MaxBufferError} from 'get-stream';Fix `lines` option (#957)ehmickybefore adoption15ab4e6
lines 3–4why, one step back3import {iterateForResult} from '../convert/loop.js';4import {isArrayBuffer} from '../stdio/uint-array.js';Simplify implementation (#955)ehmickybefore adoption8ad9ee7
line 5why, one step back5import {waitForStream, handleStreamError, isInputFileDescriptor} from './wait.js';Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 6why, one step back6Fix `lines` option (#957)ehmickybefore adoption15ab4e6
line 7why, one step back7export const waitForSubprocessStream = async ({stream, subprocess, fdNumber, encoding, buffer, maxBuffer, lines, allMixed, stripFinalNewline, streamInfo}) => {Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
lines 8–11why, one step back8 if (!stream) {9 return;10 }11Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc
line 12why, one step back12 if (isInputFileDescriptor(streamInfo, fdNumber)) {Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
lines 13–25why, one step back13 await waitForStream(stream, fdNumber, streamInfo);14 return;15 }1617 if (!buffer) {18 await Promise.all([19 waitForStream(stream, fdNumber, streamInfo),20 resumeStream(stream),21 ]);22 return;23 }2425 try {Fix `lines` option (#957)ehmickybefore adoption15ab4e6
line 26why, one step back26 return await getAnyStream({stream, fdNumber, encoding, maxBuffer, lines, allMixed, stripFinalNewline, streamInfo});Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
lines 27–32why, one step back27 } catch (error) {28 if (error instanceof MaxBufferError) {29 subprocess.kill();30 }3132 handleStreamError(error, fdNumber, streamInfo);Fix `lines` option (#957)ehmickybefore adoption15ab4e6
line 33why, one step back33 return handleBufferedData(error);Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
lines 34–45why, one step back34 }35};3637// When using `buffer: false`, users need to read `subprocess.stdout|stderr|all` right away38// See https://github.com/sindresorhus/execa/issues/730 and https://github.com/sindresorhus/execa/pull/729#discussion_r146549631039const resumeStream = async stream => {40 await setImmediate();41 if (stream.readableFlowing === null) {42 stream.resume();43 }44};45Fix `lines` option (#957)ehmickybefore adoption15ab4e6
lines 46–50why, one step back46const getAnyStream = async ({stream, fdNumber, encoding, maxBuffer, lines, allMixed, stripFinalNewline, streamInfo}) => {47 if (allMixed) {48 return getStreamLines({stream, fdNumber, encoding, maxBuffer, lines, stripFinalNewline, streamInfo});49 }50Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
lines 51–54why, one step back51 if (stream.readableObjectMode) {52 return getStreamAsArray(stream, {maxBuffer});53 }54Fix `lines` option (#957)ehmickybefore adoption15ab4e6
lines 55–56why, one step back55 if (encoding === 'buffer') {56 return new Uint8Array(await getStreamAsArrayBuffer(stream, {maxBuffer}));Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc
lines 57–58why, one step back57 }58Fix `lines` option (#957)ehmickybefore adoption15ab4e6
lines 59–63why, one step back59 if (!lines) {60 return getStream(stream, {maxBuffer});61 }6263 return getStreamLines({stream, fdNumber, encoding, maxBuffer, lines, stripFinalNewline, streamInfo});Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
lines 64–65why, one step back64};65Fix `lines` option (#957)ehmickybefore adoption15ab4e6
lines 66–69why, one step back66const getStreamLines = ({stream, fdNumber, encoding, maxBuffer, lines, stripFinalNewline, streamInfo}) => {67 const onStreamEnd = waitForStream(stream, fdNumber, streamInfo);68 const iterable = iterateForResult({stream, onStreamEnd, lines, encoding, stripFinalNewline});69 return getStreamAsArray(iterable, {maxBuffer});Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc
lines 70–71why, one step back70};71Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
lines 72–74why, one step back72// On failure, `result.stdout|stderr|all` should contain the currently buffered stream73// They are automatically closed and flushed by Node.js when the subprocess exits74// When `buffer` is `false`, `streamPromise` is `undefined` and there is no buffered data to retrieveFix `lines` option (#957)ehmickybefore adoption15ab4e6
line 75why, one step back75export const getBufferedData = async streamPromise => {Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
lines 76–78why, one step back76 try {77 return await streamPromise;78 } catch (error) {Fix `lines` option (#957)ehmickybefore adoption15ab4e6
line 79why, one step back79 return handleBufferedData(error);Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
lines 80–82why, one step back80 }81};82Fix `lines` option (#957)ehmickybefore adoption15ab4e6
lines 83–85why, one step back83const handleBufferedData = ({bufferedData}) => isArrayBuffer(bufferedData)84 ? new Uint8Array(bufferedData)85 : bufferedData;Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 86why, one step back86