wait-subprocess.js
This file
Path
lib/resolve/wait-subprocess.js
102 lines · 3.3 KiB · JavaScript
Last changed by
Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103import {once} from 'node:events';import {isStream as isNodeStream} from 'is-stream';import {throwOnTimeout} from '../terminate/timeout.js';import {isStandardStream} from '../utils/standard-stream.js';import {TRANSFORM_TYPES} from '../stdio/type.js';import {getBufferedData} from '../io/contents.js';import {waitForAllStream} from './all-async.js';import {waitForStdioStreams} from './stdio.js';import {waitForExit, waitForSuccessfulExit} from './exit-async.js';import {waitForStream} from './wait-stream.js';
// Retrieve result of subprocess: exit code, signal, error, streams (stdout/stderr/all)export const waitForSubprocessResult = async ({ subprocess, options: {encoding, buffer, maxBuffer, lines, timeoutDuration: timeout, stripFinalNewline}, context, verboseInfo, fileDescriptors, originalStreams, onInternalError, controller,}) => { const exitPromise = waitForExit(subprocess); const streamInfo = { originalStreams, fileDescriptors, subprocess, exitPromise, propagating: false, };
const stdioPromises = waitForStdioStreams({ subprocess, encoding, buffer, maxBuffer, lines, stripFinalNewline, verboseInfo, streamInfo, }); const allPromise = waitForAllStream({ subprocess, encoding, buffer, maxBuffer, lines, stripFinalNewline, verboseInfo, streamInfo, }); const originalPromises = waitForOriginalStreams(originalStreams, subprocess, streamInfo); const customStreamsEndPromises = waitForCustomStreamsEnd(fileDescriptors, streamInfo);
try { return await Promise.race([ Promise.all([ {}, waitForSuccessfulExit(exitPromise), Promise.all(stdioPromises), allPromise, ...originalPromises, ...customStreamsEndPromises, ]), onInternalError, throwOnSubprocessError(subprocess, controller), ...throwOnTimeout(subprocess, timeout, context, controller), ]); } catch (error) { return Promise.all([ {error}, exitPromise, Promise.all(stdioPromises.map(stdioPromise => getBufferedData(stdioPromise))), getBufferedData(allPromise), Promise.allSettled(originalPromises), Promise.allSettled(customStreamsEndPromises), ]); }};
// Transforms replace `subprocess.std*`, which means they are not exposed to users.// However, we still want to wait for their completion.const waitForOriginalStreams = (originalStreams, subprocess, streamInfo) => originalStreams.map((stream, fdNumber) => stream === subprocess.stdio[fdNumber] ? undefined : waitForStream(stream, fdNumber, streamInfo));
// Some `stdin`/`stdout`/`stderr` options create a stream, e.g. when passing a file path.// The `.pipe()` method automatically ends that stream when `subprocess` ends.// This makes sure we wait for the completion of those streams, in order to catch any error.const waitForCustomStreamsEnd = (fileDescriptors, streamInfo) => fileDescriptors.flatMap(({stdioItems}, fdNumber) => stdioItems .filter(({value, stream = value}) => isNodeStream(stream, {checkOpen: false}) && !isStandardStream(stream)) .map(({type, value, stream = value}) => waitForStream(stream, fdNumber, streamInfo, { isSameDirection: TRANSFORM_TYPES.has(type), stopOnExit: type === 'native', })));
// Fails when the subprocess emits an `error` eventconst throwOnSubprocessError = async (subprocess, {signal}) => { const [error] = await once(subprocess, 'error', {signal}); throw error;};