exit-async.js
This file
Path
lib/resolve/exit-async.js
54 lines · 1.8 KiB · JavaScript
Last changed by
Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 1–4why, one step back1import {once} from 'node:events';2import {DiscardedError} from '../return/final-error.js';34// If `error` is emitted before `spawn`, `exit` will never be emitted.Improve `cancelSignal` option (#1108)ehmickybefore adoption3b56977
line 5why, one step back5// However, `error` might be emitted after `spawn`.100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 6–10why, one step back6// In that case, `exit` will still be emitted.7// Since the `exit` event contains the signal name, we want to make sure we are listening for it.8// This function also takes into account the following unlikely cases:9// - `exit` being emitted in the same microtask as `spawn`10// - `error` being emitted multiple timesAdd `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
lines 11–17why, one step back11export const waitForExit = async (subprocess, context) => {12 const [exitCode, signal] = await waitForExitOrError(subprocess);13 context.isForcefullyTerminated ??= false;14 return [exitCode, signal];15};1617const waitForExitOrError = async subprocess => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 18–54why, one step back18 const [spawnPayload, exitPayload] = await Promise.allSettled([19 once(subprocess, 'spawn'),20 once(subprocess, 'exit'),21 ]);2223 if (spawnPayload.status === 'rejected') {24 return [];25 }2627 return exitPayload.status === 'rejected'28 ? waitForSubprocessExit(subprocess)29 : exitPayload.value;30};3132const waitForSubprocessExit = async subprocess => {33 try {34 return await once(subprocess, 'exit');35 } catch {36 return waitForSubprocessExit(subprocess);37 }38};3940// Retrieve the final exit code and|or signal name41export const waitForSuccessfulExit = async exitPromise => {42 const [exitCode, signal] = await exitPromise;4344 if (!isSubprocessErrorExit(exitCode, signal) && isFailedExit(exitCode, signal)) {45 throw new DiscardedError();46 }4748 return [exitCode, signal];49};5051// When the subprocess fails due to an `error` event52const isSubprocessErrorExit = (exitCode, signal) => exitCode === undefined && signal === undefined;53// When the subprocess fails due to a non-0 exit code or to a signal termination54export const isFailedExit = (exitCode, signal) => exitCode !== 0 || signal !== null;