Skip to content
Myr

resolve.js

This file

Path

lib/stream/resolve.js

85 lines · 3.7 KiB · JavaScript

Last changed by

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

History of this file

Take it

Split files (#837)ehmickybefore adoption672c2d7

line 1why, one step back
1import {once} from 'node:events';

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 2why, one step back
2import {isStream as isNodeStream} from 'is-stream';

Split files (#837)ehmickybefore adoption672c2d7

lines 3–6why, one step back
3import {waitForSuccessfulExit} from '../exit/code.js';
4import {errorSignal} from '../exit/kill.js';5import {throwOnTimeout} from '../exit/timeout.js';6import {isStandardStream} from '../utils.js';

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

line 7why, one step back
7import {TRANSFORM_TYPES} from '../stdio/generator.js';

Split files (#837)ehmickybefore adoption672c2d7

line 8why, one step back
8import {waitForAllStream} from './all.js';

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

line 9why, one step back
9import {waitForSubprocessStream, getBufferedData} from './subprocess.js';

Split files (#837)ehmickybefore adoption672c2d7

lines 10–12why, one step back
10import {waitForExit} from './exit.js';
11import {waitForStream} from './wait.js';12

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

lines 13–15why, one step back
13// Retrieve result of subprocess: exit code, signal, error, streams (stdout/stderr/all)
14export const getSubprocessResult = async ({15 subprocess,

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

line 16why, one step back
16 options: {encoding, buffer, maxBuffer, lines, timeoutDuration: timeout, stripFinalNewline},

Split files (#837)ehmickybefore adoption672c2d7

line 17why, one step back
17 context,

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 18why, one step back
18 fileDescriptors,

Split files (#837)ehmickybefore adoption672c2d7

lines 19–21why, one step back
19 originalStreams,
20 controller,21}) => {

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

line 22why, one step back
22 const exitPromise = waitForExit(subprocess);

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 23why, one step back
23 const streamInfo = {originalStreams, fileDescriptors, subprocess, exitPromise, propagating: false};

Split files (#837)ehmickybefore adoption672c2d7

line 24why, one step back
24

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

lines 25–26why, one step back
25 const stdioPromises = waitForSubprocessStreams({subprocess, encoding, buffer, maxBuffer, lines, stripFinalNewline, streamInfo});
26 const allPromise = waitForAllStream({subprocess, encoding, buffer, maxBuffer, lines, stripFinalNewline, streamInfo});

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

line 27why, one step back
27 const originalPromises = waitForOriginalStreams(originalStreams, subprocess, streamInfo);

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 28why, one step back
28 const customStreamsEndPromises = waitForCustomStreamsEnd(fileDescriptors, streamInfo);

Split files (#837)ehmickybefore adoption672c2d7

lines 29–39why, one step back
29
30 try {31 return await Promise.race([32 Promise.all([33 {},34 waitForSuccessfulExit(exitPromise),35 Promise.all(stdioPromises),36 allPromise,37 ...originalPromises,38 ...customStreamsEndPromises,39 ]),

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

lines 40–42why, one step back
40 throwOnSubprocessError(subprocess, controller),
41 throwOnInternalError(subprocess, controller),42 ...throwOnTimeout(subprocess, timeout, context, controller),

Split files (#837)ehmickybefore adoption672c2d7

lines 43–47why, one step back
43 ]);
44 } catch (error) {45 return Promise.all([46 {error},47 exitPromise,

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

lines 48–49why, one step back
48 Promise.all(stdioPromises.map(stdioPromise => getBufferedData(stdioPromise))),
49 getBufferedData(allPromise),

Split files (#837)ehmickybefore adoption672c2d7

lines 50–55why, one step back
50 Promise.allSettled(originalPromises),
51 Promise.allSettled(customStreamsEndPromises),52 ]);53 }54};55

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

line 56why, one step back
56// Read the contents of `subprocess.std*` and|or wait for its completion

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

lines 57–58why, one step back
57const waitForSubprocessStreams = ({subprocess, encoding, buffer, maxBuffer, lines, stripFinalNewline, streamInfo}) =>
58 subprocess.stdio.map((stream, fdNumber) => waitForSubprocessStream({stream, subprocess, fdNumber, encoding, buffer, maxBuffer, lines, allMixed: false, stripFinalNewline, streamInfo}));

Split files (#837)ehmickybefore adoption672c2d7

line 59why, one step back
59

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

line 60why, one step back
60// Transforms replace `subprocess.std*`, which means they are not exposed to users.

Split files (#837)ehmickybefore adoption672c2d7

line 61why, one step back
61// However, we still want to wait for their completion.

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

lines 62–63why, one step back
62const waitForOriginalStreams = (originalStreams, subprocess, streamInfo) =>
63 originalStreams.map((stream, fdNumber) => stream === subprocess.stdio[fdNumber]

Split files (#837)ehmickybefore adoption672c2d7

line 64why, one step back
64 ? undefined

Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40

line 65why, one step back
65 : waitForStream(stream, fdNumber, streamInfo));

Split files (#837)ehmickybefore adoption672c2d7

lines 66–67why, one step back
66
67// Some `stdin`/`stdout`/`stderr` options create a stream, e.g. when passing a file path.

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

line 68why, one step back
68// The `.pipe()` method automatically ends that stream when `subprocess` ends.

Split files (#837)ehmickybefore adoption672c2d7

line 69why, one step back
69// This makes sure we wait for the completion of those streams, in order to catch any error.

Simplify implementation (#955)ehmickybefore adoption8ad9ee7

line 70why, one step back
70const waitForCustomStreamsEnd = (fileDescriptors, streamInfo) => fileDescriptors.flatMap(({stdioItems}, fdNumber) => stdioItems

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 71–72why, one step back
71 .filter(({value, stream = value}) => isNodeStream(stream, {checkOpen: false}) && !isStandardStream(stream))
72 .map(({type, value, stream = value}) => waitForStream(stream, fdNumber, streamInfo, {

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

line 73why, one step back
73 isSameDirection: TRANSFORM_TYPES.has(type),

Split files (#837)ehmickybefore adoption672c2d7

line 74why, one step back
74 stopOnExit: type === 'native',

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 75why, one step back
75 })));

Split files (#837)ehmickybefore adoption672c2d7

line 76why, one step back
76

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

lines 77–78why, one step back
77const throwOnSubprocessError = async (subprocess, {signal}) => {
78 const [error] = await once(subprocess, 'error', {signal});

Split files (#837)ehmickybefore adoption672c2d7

lines 79–81why, one step back
79 throw error;
80};81

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

lines 82–83why, one step back
82const throwOnInternalError = async (subprocess, {signal}) => {
83 const [error] = await once(subprocess, errorSignal, {signal});

Split files (#837)ehmickybefore adoption672c2d7

lines 84–85why, one step back
84 throw error;
85};