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
Rename files (#977)ehmickybefore adoption4b2e316
lines 1–19why, one step back1import {once} from 'node:events';2import {isStream as isNodeStream} from 'is-stream';3import {throwOnTimeout} from '../terminate/timeout.js';4import {isStandardStream} from '../utils/standard-stream.js';5import {TRANSFORM_TYPES} from '../stdio/type.js';6import {getBufferedData} from '../io/contents.js';7import {waitForAllStream} from './all-async.js';8import {waitForStdioStreams} from './stdio.js';9import {waitForExit, waitForSuccessfulExit} from './exit-async.js';10import {waitForStream} from './wait-stream.js';1112// Retrieve result of subprocess: exit code, signal, error, streams (stdout/stderr/all)13export const waitForSubprocessResult = async ({14 subprocess,15 options: {encoding, buffer, maxBuffer, lines, timeoutDuration: timeout, stripFinalNewline},16 context,17 verboseInfo,18 fileDescriptors,19 originalStreams,Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335
line 20why, one step back20 onInternalError,Rename files (#977)ehmickybefore adoption4b2e316
lines 21–23why, one step back21 controller,22}) => {23 const exitPromise = waitForExit(subprocess);Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 24–30why, one step back24 const streamInfo = {25 originalStreams,26 fileDescriptors,27 subprocess,28 exitPromise,29 propagating: false,30 };Rename files (#977)ehmickybefore adoption4b2e316
line 31why, one step back31Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 32–51why, one step back32 const stdioPromises = waitForStdioStreams({33 subprocess,34 encoding,35 buffer,36 maxBuffer,37 lines,38 stripFinalNewline,39 verboseInfo,40 streamInfo,41 });42 const allPromise = waitForAllStream({43 subprocess,44 encoding,45 buffer,46 maxBuffer,47 lines,48 stripFinalNewline,49 verboseInfo,50 streamInfo,51 });Rename files (#977)ehmickybefore adoption4b2e316
lines 52–64why, one step back52 const originalPromises = waitForOriginalStreams(originalStreams, subprocess, streamInfo);53 const customStreamsEndPromises = waitForCustomStreamsEnd(fileDescriptors, streamInfo);5455 try {56 return await Promise.race([57 Promise.all([58 {},59 waitForSuccessfulExit(exitPromise),60 Promise.all(stdioPromises),61 allPromise,62 ...originalPromises,63 ...customStreamsEndPromises,64 ]),Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335
line 65why, one step back65 onInternalError,Rename files (#977)ehmickybefore adoption4b2e316
lines 66–97why, one step back66 throwOnSubprocessError(subprocess, controller),67 ...throwOnTimeout(subprocess, timeout, context, controller),68 ]);69 } catch (error) {70 return Promise.all([71 {error},72 exitPromise,73 Promise.all(stdioPromises.map(stdioPromise => getBufferedData(stdioPromise))),74 getBufferedData(allPromise),75 Promise.allSettled(originalPromises),76 Promise.allSettled(customStreamsEndPromises),77 ]);78 }79};8081// Transforms replace `subprocess.std*`, which means they are not exposed to users.82// However, we still want to wait for their completion.83const waitForOriginalStreams = (originalStreams, subprocess, streamInfo) =>84 originalStreams.map((stream, fdNumber) => stream === subprocess.stdio[fdNumber]85 ? undefined86 : waitForStream(stream, fdNumber, streamInfo));8788// Some `stdin`/`stdout`/`stderr` options create a stream, e.g. when passing a file path.89// The `.pipe()` method automatically ends that stream when `subprocess` ends.90// This makes sure we wait for the completion of those streams, in order to catch any error.91const waitForCustomStreamsEnd = (fileDescriptors, streamInfo) => fileDescriptors.flatMap(({stdioItems}, fdNumber) => stdioItems92 .filter(({value, stream = value}) => isNodeStream(stream, {checkOpen: false}) && !isStandardStream(stream))93 .map(({type, value, stream = value}) => waitForStream(stream, fdNumber, streamInfo, {94 isSameDirection: TRANSFORM_TYPES.has(type),95 stopOnExit: type === 'native',96 })));97Add more code comments (#984)ehmickybefore adoptionfdd8a9f
line 98why, one step back98// Fails when the subprocess emits an `error` eventRename files (#977)ehmickybefore adoption4b2e316
lines 99–102why, one step back99const throwOnSubprocessError = async (subprocess, {signal}) => {100 const [error] = await once(subprocess, 'error', {signal});101 throw error;102};