kill.js
This file
Path
lib/kill.js
104 lines · 2.8 KiB · JavaScript
Last changed by
Move `onExit()` cleanup code next to `onExit()` (#337)ehmickybefore adoption7d51047
History of this fileTake it
Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 1–3why, one step back1'use strict';2const os = require('os');3const onExit = require('signal-exit');Refactor the `timeout` option (#333)ehmickybefore adoption2a7551f
line 4why, one step back4const pFinally = require('p-finally');Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 5–55why, one step back56const DEFAULT_FORCE_KILL_TIMEOUT = 1000 * 5;78// Monkey-patches `childProcess.kill()` to add `forceKillAfterTimeout` behavior9const spawnedKill = (kill, signal = 'SIGTERM', options = {}) => {10 const killResult = kill(signal);11 setKillTimeout(kill, signal, options, killResult);12 return killResult;13};1415const setKillTimeout = (kill, signal, options, killResult) => {16 if (!shouldForceKill(signal, options, killResult)) {17 return;18 }1920 const timeout = getForceKillAfterTimeout(options);21 setTimeout(() => {22 kill('SIGKILL');23 }, timeout).unref();24};2526const shouldForceKill = (signal, {forceKillAfterTimeout}, killResult) => {27 return isSigterm(signal) && forceKillAfterTimeout !== false && killResult;28};2930const isSigterm = signal => {31 return signal === os.constants.signals.SIGTERM ||32 (typeof signal === 'string' && signal.toUpperCase() === 'SIGTERM');33};3435const getForceKillAfterTimeout = ({forceKillAfterTimeout = true}) => {36 if (forceKillAfterTimeout === true) {37 return DEFAULT_FORCE_KILL_TIMEOUT;38 }3940 if (!Number.isInteger(forceKillAfterTimeout) || forceKillAfterTimeout < 0) {41 throw new TypeError(`Expected the \`forceKillAfterTimeout\` option to be a non-negative integer, got \`${forceKillAfterTimeout}\` (${typeof forceKillAfterTimeout})`);42 }4344 return forceKillAfterTimeout;45};4647// `childProcess.cancel()`48const spawnedCancel = (spawned, context) => {49 const killResult = spawned.kill();5051 if (killResult) {52 context.isCanceled = true;53 }54};55Refactor the `timeout` option (#333)ehmickybefore adoption2a7551f
lines 56–60why, one step back56const timeoutKill = (spawned, signal, reject) => {57 spawned.kill(signal);58 reject(Object.assign(new Error('Timed out'), {timedOut: true, signal}));59};60Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
line 61why, one step back61// `timeout` option handlingRefactor the `timeout` option (#333)ehmickybefore adoption2a7551f
lines 62–64why, one step back62const setupTimeout = (spawned, {timeout, killSignal = 'SIGTERM'}, spawnedPromise) => {63 if (timeout === 0 || timeout === undefined) {64 return spawnedPromise;Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
line 65why, one step back65 }Refactor the `timeout` option (#333)ehmickybefore adoption2a7551f
lines 66–82why, one step back6667 if (!Number.isInteger(timeout) || timeout < 0) {68 throw new TypeError(`Expected the \`timeout\` option to be a non-negative integer, got \`${timeout}\` (${typeof timeout})`);69 }7071 let timeoutId;72 const timeoutPromise = new Promise((resolve, reject) => {73 timeoutId = setTimeout(() => {74 timeoutKill(spawned, killSignal, reject);75 }, timeout);76 });7778 const safeSpawnedPromise = pFinally(spawnedPromise, () => {79 clearTimeout(timeoutId);80 });8182 return Promise.race([timeoutPromise, safeSpawnedPromise]);Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 83–85why, one step back83};8485// `cleanup` option handlingMove `onExit()` cleanup code next to `onExit()` (#337)ehmickybefore adoption7d51047
line 86why, one step back86const setExitHandler = (spawned, {cleanup, detached}, timedPromise) => {Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 87–90why, one step back87 if (!cleanup || detached) {88 return;89 }90Move `onExit()` cleanup code next to `onExit()` (#337)ehmickybefore adoption7d51047
line 91why, one step back91 const removeExitHandler = onExit(() => {Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 92–94why, one step back92 spawned.kill();93 });94Move `onExit()` cleanup code next to `onExit()` (#337)ehmickybefore adoption7d51047
lines 95–96why, one step back95 // TODO: Use native "finally" syntax when targeting Node.js 1096 return pFinally(timedPromise, removeExitHandler);Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
lines 97–102why, one step back97};9899module.exports = {100 spawnedKill,101 spawnedCancel,102 setupTimeout,Move `onExit()` cleanup code next to `onExit()` (#337)ehmickybefore adoption7d51047
line 103why, one step back103 setExitHandlerSeparate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7
line 104why, one step back104};