Skip to content
Myr

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 file

Take it

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 1–26why, one step back
1import {onAbortedSignal} from '../utils/abort-signal.js';
2import {sendAbort} from '../ipc/graceful.js';3import {killOnTimeout} from './kill.js';45// Validate the `gracefulCancel` option6export const validateGracefulCancel = ({gracefulCancel, cancelSignal, ipc, serialization}) => {7 if (!gracefulCancel) {8 return;9 }1011 if (cancelSignal === undefined) {12 throw new Error('The `cancelSignal` option must be defined when setting the `gracefulCancel` option.');13 }1415 if (!ipc) {16 throw new Error('The `ipc` option cannot be false when setting the `gracefulCancel` option.');17 }1819 if (serialization === 'json') {20 throw new Error('The `serialization` option cannot be \'json\' when setting the `gracefulCancel` option.');21 }22};2324// Send abort reason to the subprocess when aborting the `cancelSignal` option and `gracefulCancel` is `true`25export const throwOnGracefulCancel = ({26 subprocess,

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 27why, one step back
27 kill,

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 28–35why, one step back
28 cancelSignal,
29 gracefulCancel,30 forceKillAfterDelay,31 context,32 controller,33}) => gracefulCancel34 ? [sendOnAbort({35 subprocess,

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 36why, one step back
36 kill,

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 37–43why, one step back
37 cancelSignal,
38 forceKillAfterDelay,39 context,40 controller,41 })]42 : [];43

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 44why, one step back
44const sendOnAbort = async ({subprocess, kill, cancelSignal, forceKillAfterDelay, context, controller: {signal}}) => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 45–48why, one step back
45 await onAbortedSignal(cancelSignal, signal);
46 const reason = getReason(cancelSignal);47 await sendAbort(subprocess, reason);48 killOnTimeout({

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 49why, one step back
49 kill,

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 50–73why, one step back
50 forceKillAfterDelay,
51 context,52 controllerSignal: signal,53 });54 context.terminationReason ??= 'gracefulCancel';55 throw cancelSignal.reason;56};5758// The default `reason` is a DOMException, which is not serializable with V859// See https://github.com/nodejs/node/issues/5322560const getReason = ({reason}) => {61 if (!(reason instanceof DOMException)) {62 return reason;63 }6465 const error = new Error(reason.message);66 Object.defineProperty(error, 'stack', {67 value: reason.stack,68 enumerable: false,69 configurable: true,70 writable: true,71 });72 return error;73};