Skip to content
Myr

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 file

Take it

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 1–2why, one step back
1import {setImmediate} from 'node:timers/promises';
2import getStream, {getStreamAsArrayBuffer, getStreamAsArray, MaxBufferError} from 'get-stream';

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

lines 3–4why, one step back
3import {iterateForResult} from '../convert/loop.js';
4import {isArrayBuffer} from '../stdio/uint-array.js';

Simplify implementation (#955)ehmickybefore adoption8ad9ee7

line 5why, one step back
5import {waitForStream, handleStreamError, isInputFileDescriptor} from './wait.js';

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 6why, one step back
6

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

line 7why, one step back
7export 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 back
8 if (!stream) {
9 return;10 }11

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

line 12why, one step back
12 if (isInputFileDescriptor(streamInfo, fdNumber)) {

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 13–25why, one step back
13 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 back
26 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 back
27 } 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 back
33 return handleBufferedData(error);

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 34–45why, one step back
34 }
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};45

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

lines 46–50why, one step back
46const getAnyStream = async ({stream, fdNumber, encoding, maxBuffer, lines, allMixed, stripFinalNewline, streamInfo}) => {
47 if (allMixed) {48 return getStreamLines({stream, fdNumber, encoding, maxBuffer, lines, stripFinalNewline, streamInfo});49 }50

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 51–54why, one step back
51 if (stream.readableObjectMode) {
52 return getStreamAsArray(stream, {maxBuffer});53 }54

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

lines 55–56why, one step back
55 if (encoding === 'buffer') {
56 return new Uint8Array(await getStreamAsArrayBuffer(stream, {maxBuffer}));

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

lines 57–58why, one step back
57 }
58

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

lines 59–63why, one step back
59 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 back
64};
65

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

lines 66–69why, one step back
66const 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 back
70};
71

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 72–74why, one step back
72// On failure, `result.stdout|stderr|all` should contain the currently buffered stream
73// 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 retrieve

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

line 75why, one step back
75export const getBufferedData = async streamPromise => {

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 76–78why, one step back
76 try {
77 return await streamPromise;78 } catch (error) {

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

line 79why, one step back
79 return handleBufferedData(error);

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 80–82why, one step back
80 }
81};82

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

lines 83–85why, one step back
83const handleBufferedData = ({bufferedData}) => isArrayBuffer(bufferedData)
84 ? new Uint8Array(bufferedData)85 : bufferedData;

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 86why, one step back
86