graceful.js
This file
Path
lib/terminate/graceful.js
73 lines · 1.9 KiB · JavaScript
Last changed by
Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
History of this fileTake it
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374import {onAbortedSignal} from '../utils/abort-signal.js';import {sendAbort} from '../ipc/graceful.js';import {killOnTimeout} from './kill.js';
// Validate the `gracefulCancel` optionexport const validateGracefulCancel = ({gracefulCancel, cancelSignal, ipc, serialization}) => { if (!gracefulCancel) { return; }
if (cancelSignal === undefined) { throw new Error('The `cancelSignal` option must be defined when setting the `gracefulCancel` option.'); }
if (!ipc) { throw new Error('The `ipc` option cannot be false when setting the `gracefulCancel` option.'); }
if (serialization === 'json') { throw new Error('The `serialization` option cannot be \'json\' when setting the `gracefulCancel` option.'); }};
// Send abort reason to the subprocess when aborting the `cancelSignal` option and `gracefulCancel` is `true`export const throwOnGracefulCancel = ({ subprocess, kill, cancelSignal, gracefulCancel, forceKillAfterDelay, context, controller,}) => gracefulCancel ? [sendOnAbort({ subprocess, kill, cancelSignal, forceKillAfterDelay, context, controller, })] : [];
const sendOnAbort = async ({subprocess, kill, cancelSignal, forceKillAfterDelay, context, controller: {signal}}) => { await onAbortedSignal(cancelSignal, signal); const reason = getReason(cancelSignal); await sendAbort(subprocess, reason); killOnTimeout({ kill, forceKillAfterDelay, context, controllerSignal: signal, }); context.terminationReason ??= 'gracefulCancel'; throw cancelSignal.reason;};
// The default `reason` is a DOMException, which is not serializable with V8// See https://github.com/nodejs/node/issues/53225const getReason = ({reason}) => { if (!(reason instanceof DOMException)) { return reason; }
const error = new Error(reason.message); Object.defineProperty(error, 'stack', { value: reason.stack, enumerable: false, configurable: true, writable: true, }); return error;};