kill.js
This file
Path
lib/kill.js
103 lines · 3.0 KiB · JavaScript
Last changed by
Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b
History of this fileTake 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';Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 4–7why, one step back45const DEFAULT_FORCE_KILL_TIMEOUT = 1000 * 5;67// Monkey-patches `childProcess.kill()` to add `forceKillAfterTimeout` behaviorRequire Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 8why, one step back8export const spawnedKill = (kill, signal = 'SIGTERM', options = {}) => {Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 9–19why, one step back9 const killResult = kill(signal);10 setKillTimeout(kill, signal, options, killResult);11 return killResult;12};1314const setKillTimeout = (kill, signal, options, killResult) => {15 if (!shouldForceKill(signal, options, killResult)) {16 return;17 }1819 const timeout = getForceKillAfterTimeout(options);Fix unref error (#400)Mikael Finstadbefore adoption09cd28a
line 20why, one step back20 const t = setTimeout(() => {Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
line 21why, one step back21 kill('SIGKILL');Fix unref error (#400)Mikael Finstadbefore adoption09cd28a
lines 22–23why, one step back22 }, timeout);23Add code comment related to setTimeout().unref (#401)ehmickybefore adoption6201405
lines 24–26why, one step back24 // Guarded because there's no `.unref()` when `execa` is used in the renderer25 // process in Electron. This cannot be tested since we don't run tests in26 // Electron.Fix unref error (#400)Mikael Finstadbefore adoption09cd28a
lines 27–30why, one step back27 // istanbul ignore else28 if (t.unref) {29 t.unref();30 }Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 31–32why, one step back31};32Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 33why, one step back33const shouldForceKill = (signal, {forceKillAfterTimeout}, killResult) => isSigterm(signal) && forceKillAfterTimeout !== false && killResult;Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
line 34why, one step back34Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
lines 35–36why, one step back35const isSigterm = signal => signal === os.constants.signals.SIGTERM36 || (typeof signal === 'string' && signal.toUpperCase() === 'SIGTERM');Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 37–42why, one step back3738const getForceKillAfterTimeout = ({forceKillAfterTimeout = true}) => {39 if (forceKillAfterTimeout === true) {40 return DEFAULT_FORCE_KILL_TIMEOUT;41 }42Fix use of floating number for the `timeout` and `forceKillAfterTimeout` options (#431)Kiko Beatsbefore adoption9a157b3
line 43why, one step back43 if (!Number.isFinite(forceKillAfterTimeout) || forceKillAfterTimeout < 0) {Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 44–50why, one step back44 throw new TypeError(`Expected the \`forceKillAfterTimeout\` option to be a non-negative integer, got \`${forceKillAfterTimeout}\` (${typeof forceKillAfterTimeout})`);45 }4647 return forceKillAfterTimeout;48};4950// `childProcess.cancel()`Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 51why, one step back51export const spawnedCancel = (spawned, context) => {Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 52–58why, one step back52 const killResult = spawned.kill();5354 if (killResult) {55 context.isCanceled = true;56 }57};58Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b
lines 59–62why, one step back59const killAfterTimeout = async (spawned, timeout, killSignal, controller) => {60 await pSetTimeout(timeout, undefined, {ref: false, signal: controller.signal});61 spawned.kill(killSignal);62 throw Object.assign(new Error('Timed out'), {timedOut: true, signal: killSignal});Refactor the `timeout` option (#333)ehmickybefore adoption2a7551f
lines 63–64why, one step back63};64Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
line 65why, one step back65// `timeout` option handlingRefactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b
line 66why, one step back66export const setupTimeout = async (spawned, {timeout, killSignal = 'SIGTERM'}, spawnedPromise) => {Refactor the `timeout` option (#333)ehmickybefore adoption2a7551f
lines 67–68why, one step back67 if (timeout === 0 || timeout === undefined) {68 return spawnedPromise;Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
line 69why, one step back69 }Refactor the `timeout` option (#333)ehmickybefore adoption2a7551f
line 70why, one step back70Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b
lines 71–79why, one step back71 const controller = new AbortController();72 try {73 return await Promise.race([74 spawnedPromise,75 killAfterTimeout(spawned, timeout, killSignal, controller),76 ]);77 } finally {78 controller.abort();79 }Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 80–81why, one step back80};81Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 82why, one step back82export const validateTimeout = ({timeout}) => {Fix `timeout` option validation (#463)ehmickybefore adoption427c5c2
lines 83–87why, one step back83 if (timeout !== undefined && (!Number.isFinite(timeout) || timeout < 0)) {84 throw new TypeError(`Expected the \`timeout\` option to be a non-negative integer, got \`${timeout}\` (${typeof timeout})`);85 }86};87Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
line 88why, one step back88// `cleanup` option handlingRequire Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 89why, one step back89export const setExitHandler = async (spawned, {cleanup, detached}, timedPromise) => {Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
line 90why, one step back90 if (!cleanup || detached) {Fix errors being thrown when `detached: true` or `cleanup: false` is used (#360)ehmickybefore adoptionc3526cd
line 91why, one step back91 return timedPromise;Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 92–93why, one step back92 }93Move `onExit()` cleanup code next to `onExit()` (#337)ehmickybefore adoption7d51047
line 94why, one step back94 const removeExitHandler = onExit(() => {Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 95–97why, one step back95 spawned.kill();96 });97Refactor `cleanup` option to use `async/await` (#649)ehmickybefore adoption813e2c6
lines 98–100why, one step back98 try {99 return await timedPromise;100 } finally {Require Node.js 10Sindre Sorhusbefore adoption5a9c76f
line 101why, one step back101 removeExitHandler();Refactor `cleanup` option to use `async/await` (#649)ehmickybefore adoption813e2c6
line 102why, one step back102 }Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
line 103why, one step back103};