kill.js
This file
Path
lib/kill.js
102 lines · 2.9 KiB · JavaScript
Last changed by
Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
History of this fileTake it
Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
lines 1–2why, one step back1import os from 'node:os';2import onExit from 'signal-exit';Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 3–6why, one step back34const DEFAULT_FORCE_KILL_TIMEOUT = 1000 * 5;56// Monkey-patches `childProcess.kill()` to add `forceKillAfterTimeout` behaviorRequire Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 7why, one step back7export const spawnedKill = (kill, signal = 'SIGTERM', options = {}) => {Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 8–18why, one step back8 const killResult = kill(signal);9 setKillTimeout(kill, signal, options, killResult);10 return killResult;11};1213const setKillTimeout = (kill, signal, options, killResult) => {14 if (!shouldForceKill(signal, options, killResult)) {15 return;16 }1718 const timeout = getForceKillAfterTimeout(options);Fix unref error (#400)Mikael Finstadbefore adoption09cd28a
line 19why, one step back19 const t = setTimeout(() => {Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
line 20why, one step back20 kill('SIGKILL');Fix unref error (#400)Mikael Finstadbefore adoption09cd28a
lines 21–22why, one step back21 }, timeout);22Add code comment related to setTimeout().unref (#401)ehmickybefore adoption6201405
lines 23–25why, one step back23 // Guarded because there's no `.unref()` when `execa` is used in the renderer24 // process in Electron. This cannot be tested since we don't run tests in25 // Electron.Fix unref error (#400)Mikael Finstadbefore adoption09cd28a
lines 26–29why, one step back26 // istanbul ignore else27 if (t.unref) {28 t.unref();29 }Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 30–31why, one step back30};31Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 32why, one step back32const shouldForceKill = (signal, {forceKillAfterTimeout}, killResult) => isSigterm(signal) && forceKillAfterTimeout !== false && killResult;Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
line 33why, one step back33Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
lines 34–35why, one step back34const isSigterm = signal => signal === os.constants.signals.SIGTERM35 || (typeof signal === 'string' && signal.toUpperCase() === 'SIGTERM');Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 36–41why, one step back3637const getForceKillAfterTimeout = ({forceKillAfterTimeout = true}) => {38 if (forceKillAfterTimeout === true) {39 return DEFAULT_FORCE_KILL_TIMEOUT;40 }41Fix use of floating number for the `timeout` and `forceKillAfterTimeout` options (#431)Kiko Beatsbefore adoption9a157b3
line 42why, one step back42 if (!Number.isFinite(forceKillAfterTimeout) || forceKillAfterTimeout < 0) {Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 43–49why, one step back43 throw new TypeError(`Expected the \`forceKillAfterTimeout\` option to be a non-negative integer, got \`${forceKillAfterTimeout}\` (${typeof forceKillAfterTimeout})`);44 }4546 return forceKillAfterTimeout;47};4849// `childProcess.cancel()`Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 50why, one step back50export const spawnedCancel = (spawned, context) => {Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 51–57why, one step back51 const killResult = spawned.kill();5253 if (killResult) {54 context.isCanceled = true;55 }56};57Refactor the `timeout` option (#333)ehmickybefore adoption2a7551f
lines 58–62why, one step back58const timeoutKill = (spawned, signal, reject) => {59 spawned.kill(signal);60 reject(Object.assign(new Error('Timed out'), {timedOut: true, signal}));61};62Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
line 63why, one step back63// `timeout` option handlingRequire Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 64why, one step back64export const setupTimeout = (spawned, {timeout, killSignal = 'SIGTERM'}, spawnedPromise) => {Refactor the `timeout` option (#333)ehmickybefore adoption2a7551f
lines 65–66why, one step back65 if (timeout === 0 || timeout === undefined) {66 return spawnedPromise;Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
line 67why, one step back67 }Refactor the `timeout` option (#333)ehmickybefore adoption2a7551f
lines 68–75why, one step back6869 let timeoutId;70 const timeoutPromise = new Promise((resolve, reject) => {71 timeoutId = setTimeout(() => {72 timeoutKill(spawned, killSignal, reject);73 }, timeout);74 });75Require Node.js 10Sindre Sorhusbefore adoption5a9c76f
line 76why, one step back76 const safeSpawnedPromise = spawnedPromise.finally(() => {Refactor the `timeout` option (#333)ehmickybefore adoption2a7551f
lines 77–80why, one step back77 clearTimeout(timeoutId);78 });7980 return Promise.race([timeoutPromise, safeSpawnedPromise]);Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 81–82why, one step back81};82Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 83why, one step back83export const validateTimeout = ({timeout}) => {Fix `timeout` option validation (#463)ehmickybefore adoption427c5c2
lines 84–88why, one step back84 if (timeout !== undefined && (!Number.isFinite(timeout) || timeout < 0)) {85 throw new TypeError(`Expected the \`timeout\` option to be a non-negative integer, got \`${timeout}\` (${typeof timeout})`);86 }87};88Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
line 89why, one step back89// `cleanup` option handlingRequire Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 90why, one step back90export const setExitHandler = async (spawned, {cleanup, detached}, timedPromise) => {Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
line 91why, one step back91 if (!cleanup || detached) {Fix errors being thrown when `detached: true` or `cleanup: false` is used (#360)ehmickybefore adoptionc3526cd
line 92why, one step back92 return timedPromise;Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 93–94why, one step back93 }94Move `onExit()` cleanup code next to `onExit()` (#337)ehmickybefore adoption7d51047
line 95why, one step back95 const removeExitHandler = onExit(() => {Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 96–98why, one step back96 spawned.kill();97 });98Require Node.js 10Sindre Sorhusbefore adoption5a9c76f
lines 99–101why, one step back99 return timedPromise.finally(() => {100 removeExitHandler();101 });Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
line 102why, one step back102};