kill.js
This file
Path
lib/kill.js
88 lines · 2.8 KiB · JavaScript
Last changed by
Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767
History of this fileTake it
The walk stopped after 200 commits: lines older than 1ac56ea point at it.
Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 1why, one step back1import os from 'node:os';Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b
line 2why, one step back2import {setTimeout as pSetTimeout} from 'node:timers/promises';Upgrade `signal-exit` to v4 (#570)ehmickybefore adoption468fbe1
line 3why, one step back3import {onExit} from 'signal-exit';Rename internal variables (#379)ehmickybefore adoption1ac56ea
lines 4–7why, one step back45const DEFAULT_FORCE_KILL_TIMEOUT = 1000 * 5;67// Monkey-patches `childProcess.kill()` to add `forceKillAfterTimeout` behaviorRefactor `childProcess.kill()` to use `async/await` (#651)ehmickybefore adoption7701e9e
line 8why, one step back8export const spawnedKill = (kill, signal = 'SIGTERM', {forceKillAfterTimeout = true} = {}) => {Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 9why, one step back9 const killResult = kill(signal);Refactor `childProcess.kill()` to use `async/await` (#651)ehmickybefore adoption7701e9e
lines 10–11why, one step back10 const timeout = getForceKillAfterTimeout(signal, forceKillAfterTimeout, killResult);11 setKillTimeout(kill, timeout);Rename internal variables (#379)ehmickybefore adoption1ac56ea
lines 12–14why, one step back12 return killResult;13};14Refactor `childProcess.kill()` to use `async/await` (#651)ehmickybefore adoption7701e9e
lines 15–16why, one step back15const setKillTimeout = async (kill, timeout) => {16 if (timeout === undefined) {Rename internal variables (#379)ehmickybefore adoption1ac56ea
lines 17–19why, one step back17 return;18 }19Refactor `childProcess.kill()` to use `async/await` (#651)ehmickybefore adoption7701e9e
lines 20–21why, one step back20 await pSetTimeout(timeout, undefined, {ref: false});21 kill('SIGKILL');Rename internal variables (#379)ehmickybefore adoption1ac56ea
lines 22–23why, one step back22};23Refactor `childProcess.kill()` to use `async/await` (#651)ehmickybefore adoption7701e9e
line 24why, one step back24const shouldForceKill = (signal, forceKillAfterTimeout, killResult) => isSigterm(signal) && forceKillAfterTimeout !== false && killResult;Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 25why, one step back25Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 26why, one step back26const isSigterm = signal => signal === os.constants.signals.SIGTERMRefactor `childProcess.kill()` to use `async/await` (#651)ehmickybefore adoption7701e9e
line 27why, one step back27 || (typeof signal === 'string' && signal.toUpperCase() === 'SIGTERM');Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 28why, one step back28Refactor `childProcess.kill()` to use `async/await` (#651)ehmickybefore adoption7701e9e
lines 29–33why, one step back29const getForceKillAfterTimeout = (signal, forceKillAfterTimeout, killResult) => {30 if (!shouldForceKill(signal, forceKillAfterTimeout, killResult)) {31 return;32 }33Rename internal variables (#379)ehmickybefore adoption1ac56ea
lines 34–37why, one step back34 if (forceKillAfterTimeout === true) {35 return DEFAULT_FORCE_KILL_TIMEOUT;36 }37Fix use of floating number for the `timeout` and `forceKillAfterTimeout` options (#431)Kiko Beatsbefore adoption9a157b3
line 38why, one step back38 if (!Number.isFinite(forceKillAfterTimeout) || forceKillAfterTimeout < 0) {Rename internal variables (#379)ehmickybefore adoption1ac56ea
lines 39–45why, one step back39 throw new TypeError(`Expected the \`forceKillAfterTimeout\` option to be a non-negative integer, got \`${forceKillAfterTimeout}\` (${typeof forceKillAfterTimeout})`);40 }4142 return forceKillAfterTimeout;43};4445// `childProcess.cancel()`Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 46why, one step back46export const spawnedCancel = (spawned, context) => {Rename internal variables (#379)ehmickybefore adoption1ac56ea
lines 47–53why, one step back47 const killResult = spawned.kill();4849 if (killResult) {50 context.isCanceled = true;51 }52};53Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767
line 54why, one step back54const killAfterTimeout = async ({spawned, timeout, killSignal, context, controller}) => {Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b
lines 55–56why, one step back55 await pSetTimeout(timeout, undefined, {ref: false, signal: controller.signal});56 spawned.kill(killSignal);Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767
lines 57–58why, one step back57 Object.assign(context, {timedOut: true, signal: killSignal});58 throw new Error('Timed out');Rename internal variables (#379)ehmickybefore adoption1ac56ea
lines 59–61why, one step back59};6061// `timeout` option handlingUse `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767
line 62why, one step back62export const throwOnTimeout = ({spawned, timeout, killSignal, context, finalizers}) => {Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 63why, one step back63 if (timeout === 0 || timeout === undefined) {Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767
line 64why, one step back64 return [];Rename internal variables (#379)ehmickybefore adoption1ac56ea
lines 65–66why, one step back65 }66Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b
line 67why, one step back67 const controller = new AbortController();Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767
lines 68–69why, one step back68 finalizers.push(controller.abort.bind(controller));69 return [killAfterTimeout({spawned, timeout, killSignal, context, controller})];Rename internal variables (#379)ehmickybefore adoption1ac56ea
lines 70–71why, one step back70};71Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 72why, one step back72export const validateTimeout = ({timeout}) => {Fix `timeout` option validation (#463)ehmickybefore adoption427c5c2
lines 73–77why, one step back73 if (timeout !== undefined && (!Number.isFinite(timeout) || timeout < 0)) {74 throw new TypeError(`Expected the \`timeout\` option to be a non-negative integer, got \`${timeout}\` (${typeof timeout})`);75 }76};77Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 78why, one step back78// `cleanup` option handlingUse `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767
line 79why, one step back79export const cleanupOnExit = (spawned, cleanup, detached, finalizers) => {Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 80why, one step back80 if (!cleanup || detached) {Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767
line 81why, one step back81 return;Rename internal variables (#379)ehmickybefore adoption1ac56ea
lines 82–86why, one step back82 }8384 const removeExitHandler = onExit(() => {85 spawned.kill();86 });Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767
line 87why, one step back87 finalizers.push(removeExitHandler);Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 88why, one step back88};