Skip to content
Myr

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 file

Take it

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

lines 1–2why, one step back
1import os from 'node:os';
2import onExit from 'signal-exit';

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 3–6why, one step back
3
4const DEFAULT_FORCE_KILL_TIMEOUT = 1000 * 5;56// Monkey-patches `childProcess.kill()` to add `forceKillAfterTimeout` behavior

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 7why, one step back
7export const spawnedKill = (kill, signal = 'SIGTERM', options = {}) => {

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 8–18why, one step back
8 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 back
19 const t = setTimeout(() => {

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

line 20why, one step back
20 kill('SIGKILL');

Fix unref error (#400)Mikael Finstadbefore adoption09cd28a

lines 21–22why, one step back
21 }, timeout);
22

Add code comment related to setTimeout().unref (#401)ehmickybefore adoption6201405

lines 23–25why, one step back
23 // Guarded because there's no `.unref()` when `execa` is used in the renderer
24 // 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 back
26 // istanbul ignore else
27 if (t.unref) {28 t.unref();29 }

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 30–31why, one step back
30};
31

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 32why, one step back
32const shouldForceKill = (signal, {forceKillAfterTimeout}, killResult) => isSigterm(signal) && forceKillAfterTimeout !== false && killResult;

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

line 33why, one step back
33

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

lines 34–35why, one step back
34const isSigterm = signal => signal === os.constants.signals.SIGTERM
35 || (typeof signal === 'string' && signal.toUpperCase() === 'SIGTERM');

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 36–41why, one step back
36
37const getForceKillAfterTimeout = ({forceKillAfterTimeout = true}) => {38 if (forceKillAfterTimeout === true) {39 return DEFAULT_FORCE_KILL_TIMEOUT;40 }41

Fix use of floating number for the `timeout` and `forceKillAfterTimeout` options (#431)Kiko Beatsbefore adoption9a157b3

line 42why, one step back
42 if (!Number.isFinite(forceKillAfterTimeout) || forceKillAfterTimeout < 0) {

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 43–49why, one step back
43 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 back
50export const spawnedCancel = (spawned, context) => {

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 51–57why, one step back
51 const killResult = spawned.kill();
5253 if (killResult) {54 context.isCanceled = true;55 }56};57

Refactor the `timeout` option (#333)ehmickybefore adoption2a7551f

lines 58–62why, one step back
58const timeoutKill = (spawned, signal, reject) => {
59 spawned.kill(signal);60 reject(Object.assign(new Error('Timed out'), {timedOut: true, signal}));61};62

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

line 63why, one step back
63// `timeout` option handling

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 64why, one step back
64export const setupTimeout = (spawned, {timeout, killSignal = 'SIGTERM'}, spawnedPromise) => {

Refactor the `timeout` option (#333)ehmickybefore adoption2a7551f

lines 65–66why, one step back
65 if (timeout === 0 || timeout === undefined) {
66 return spawnedPromise;

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

line 67why, one step back
67 }

Refactor the `timeout` option (#333)ehmickybefore adoption2a7551f

lines 68–75why, one step back
68
69 let timeoutId;70 const timeoutPromise = new Promise((resolve, reject) => {71 timeoutId = setTimeout(() => {72 timeoutKill(spawned, killSignal, reject);73 }, timeout);74 });75

Require Node.js 10Sindre Sorhusbefore adoption5a9c76f

line 76why, one step back
76 const safeSpawnedPromise = spawnedPromise.finally(() => {

Refactor the `timeout` option (#333)ehmickybefore adoption2a7551f

lines 77–80why, one step back
77 clearTimeout(timeoutId);
78 });7980 return Promise.race([timeoutPromise, safeSpawnedPromise]);

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 81–82why, one step back
81};
82

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 83why, one step back
83export const validateTimeout = ({timeout}) => {

Fix `timeout` option validation (#463)ehmickybefore adoption427c5c2

lines 84–88why, one step back
84 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};88

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

line 89why, one step back
89// `cleanup` option handling

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 90why, one step back
90export const setExitHandler = async (spawned, {cleanup, detached}, timedPromise) => {

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

line 91why, one step back
91 if (!cleanup || detached) {

Fix errors being thrown when `detached: true` or `cleanup: false` is used (#360)ehmickybefore adoptionc3526cd

line 92why, one step back
92 return timedPromise;

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 93–94why, one step back
93 }
94

Move `onExit()` cleanup code next to `onExit()` (#337)ehmickybefore adoption7d51047

line 95why, one step back
95 const removeExitHandler = onExit(() => {

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 96–98why, one step back
96 spawned.kill();
97 });98

Require Node.js 10Sindre Sorhusbefore adoption5a9c76f

lines 99–101why, one step back
99 return timedPromise.finally(() => {
100 removeExitHandler();101 });

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

line 102why, one step back
102};