Skip to content
Myr

subprocess.js

This file

Path

lib/stream/subprocess.js

79 lines · 2.6 KiB · JavaScript

Last changed by

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

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';

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

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

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

line 4why, one step back
4

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

line 5why, one step back
5export const waitForSubprocessStream = async ({stream, subprocess, fdNumber, encoding, buffer, maxBuffer, lines, streamInfo}) => {

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

lines 6–9why, one step back
6 if (!stream) {
7 return;8 }9

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

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

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

lines 11–23why, one step back
11 await waitForStream(stream, fdNumber, streamInfo);
12 return;13 }1415 if (!buffer) {16 await Promise.all([17 waitForStream(stream, fdNumber, streamInfo),18 resumeStream(stream),19 ]);20 return;21 }2223 try {

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

line 24why, one step back
24 return await getAnyStream({stream, fdNumber, encoding, maxBuffer, lines, streamInfo});

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

lines 25–43why, one step back
25 } catch (error) {
26 if (error instanceof MaxBufferError) {27 subprocess.kill();28 }2930 handleStreamError(error, fdNumber, streamInfo);31 return handleBufferedData(error, encoding);32 }33};3435// When using `buffer: false`, users need to read `subprocess.stdout|stderr|all` right away36// See https://github.com/sindresorhus/execa/issues/730 and https://github.com/sindresorhus/execa/pull/729#discussion_r146549631037const resumeStream = async stream => {38 await setImmediate();39 if (stream.readableFlowing === null) {40 stream.resume();41 }42};43

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

line 44why, one step back
44const getAnyStream = async ({stream, fdNumber, encoding, maxBuffer, lines, streamInfo}) => {

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

lines 45–48why, one step back
45 if (stream.readableObjectMode) {
46 return getStreamAsArray(stream, {maxBuffer});47 }48

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

lines 49–52why, one step back
49 if (lines) {
50 return getOutputLines(stream, fdNumber, streamInfo);51 }52

Stricter values for the `encoding` option (#928)ehmickybefore adoption477bbc4

line 53why, one step back
53 const contents = encoding === 'buffer'

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

lines 54–58why, one step back
54 ? await getStreamAsArrayBuffer(stream, {maxBuffer})
55 : await getStream(stream, {maxBuffer});56 return applyEncoding(contents, encoding);57};58

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

lines 59–63why, one step back
59const getOutputLines = async (stream, fdNumber, streamInfo) => {
60 await waitForStream(stream, fdNumber, streamInfo);61 return getFileDescriptor(streamInfo, fdNumber).outputLines;62};63

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

lines 64–74why, one step back
64// On failure, `result.stdout|stderr|all` should contain the currently buffered stream
65// They are automatically closed and flushed by Node.js when the subprocess exits66// When `buffer` is `false`, `streamPromise` is `undefined` and there is no buffered data to retrieve67export const getBufferedData = async (streamPromise, encoding) => {68 try {69 return await streamPromise;70 } catch (error) {71 return handleBufferedData(error, encoding);72 }73};74

Simplify `lines` option (#929)ehmickybefore adoptiond30fcbc

lines 75–77why, one step back
75const handleBufferedData = ({bufferedData}, encoding) => bufferedData === undefined || Array.isArray(bufferedData)
76 ? bufferedData77 : applyEncoding(bufferedData, encoding);

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

line 78why, one step back
78

Stricter values for the `encoding` option (#928)ehmickybefore adoption477bbc4

line 79why, one step back
79const applyEncoding = (contents, encoding) => encoding === 'buffer' ? new Uint8Array(contents) : contents;