Skip to content
Myr

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 file

Take 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 back
1import os from 'node:os';

Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b

line 2why, one step back
2import {setTimeout as pSetTimeout} from 'node:timers/promises';

Upgrade `signal-exit` to v4 (#570)ehmickybefore adoption468fbe1

line 3why, one step back
3import {onExit} from 'signal-exit';

Rename internal variables (#379)ehmickybefore adoption1ac56ea

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

Refactor `childProcess.kill()` to use `async/await` (#651)ehmickybefore adoption7701e9e

line 8why, one step back
8export const spawnedKill = (kill, signal = 'SIGTERM', {forceKillAfterTimeout = true} = {}) => {

Rename internal variables (#379)ehmickybefore adoption1ac56ea

line 9why, one step back
9 const killResult = kill(signal);

Refactor `childProcess.kill()` to use `async/await` (#651)ehmickybefore adoption7701e9e

lines 10–11why, one step back
10 const timeout = getForceKillAfterTimeout(signal, forceKillAfterTimeout, killResult);
11 setKillTimeout(kill, timeout);

Rename internal variables (#379)ehmickybefore adoption1ac56ea

lines 12–14why, one step back
12 return killResult;
13};14

Refactor `childProcess.kill()` to use `async/await` (#651)ehmickybefore adoption7701e9e

lines 15–16why, one step back
15const setKillTimeout = async (kill, timeout) => {
16 if (timeout === undefined) {

Rename internal variables (#379)ehmickybefore adoption1ac56ea

lines 17–19why, one step back
17 return;
18 }19

Refactor `childProcess.kill()` to use `async/await` (#651)ehmickybefore adoption7701e9e

lines 20–21why, one step back
20 await pSetTimeout(timeout, undefined, {ref: false});
21 kill('SIGKILL');

Rename internal variables (#379)ehmickybefore adoption1ac56ea

lines 22–23why, one step back
22};
23

Refactor `childProcess.kill()` to use `async/await` (#651)ehmickybefore adoption7701e9e

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

Rename internal variables (#379)ehmickybefore adoption1ac56ea

line 25why, one step back
25

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

line 26why, one step back
26const isSigterm = signal => signal === os.constants.signals.SIGTERM

Refactor `childProcess.kill()` to use `async/await` (#651)ehmickybefore adoption7701e9e

line 27why, one step back
27 || (typeof signal === 'string' && signal.toUpperCase() === 'SIGTERM');

Rename internal variables (#379)ehmickybefore adoption1ac56ea

line 28why, one step back
28

Refactor `childProcess.kill()` to use `async/await` (#651)ehmickybefore adoption7701e9e

lines 29–33why, one step back
29const getForceKillAfterTimeout = (signal, forceKillAfterTimeout, killResult) => {
30 if (!shouldForceKill(signal, forceKillAfterTimeout, killResult)) {31 return;32 }33

Rename internal variables (#379)ehmickybefore adoption1ac56ea

lines 34–37why, one step back
34 if (forceKillAfterTimeout === true) {
35 return DEFAULT_FORCE_KILL_TIMEOUT;36 }37

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

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

Rename internal variables (#379)ehmickybefore adoption1ac56ea

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

Rename internal variables (#379)ehmickybefore adoption1ac56ea

lines 47–53why, one step back
47 const killResult = spawned.kill();
4849 if (killResult) {50 context.isCanceled = true;51 }52};53

Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767

line 54why, one step back
54const killAfterTimeout = async ({spawned, timeout, killSignal, context, controller}) => {

Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b

lines 55–56why, one step back
55 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 back
57 Object.assign(context, {timedOut: true, signal: killSignal});
58 throw new Error('Timed out');

Rename internal variables (#379)ehmickybefore adoption1ac56ea

lines 59–61why, one step back
59};
6061// `timeout` option handling

Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767

line 62why, one step back
62export const throwOnTimeout = ({spawned, timeout, killSignal, context, finalizers}) => {

Rename internal variables (#379)ehmickybefore adoption1ac56ea

line 63why, one step back
63 if (timeout === 0 || timeout === undefined) {

Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767

line 64why, one step back
64 return [];

Rename internal variables (#379)ehmickybefore adoption1ac56ea

lines 65–66why, one step back
65 }
66

Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b

line 67why, one step back
67 const controller = new AbortController();

Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767

lines 68–69why, one step back
68 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 back
70};
71

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

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

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

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

Rename internal variables (#379)ehmickybefore adoption1ac56ea

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

Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767

line 79why, one step back
79export const cleanupOnExit = (spawned, cleanup, detached, finalizers) => {

Rename internal variables (#379)ehmickybefore adoption1ac56ea

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

Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767

line 81why, one step back
81 return;

Rename internal variables (#379)ehmickybefore adoption1ac56ea

lines 82–86why, one step back
82 }
8384 const removeExitHandler = onExit(() => {85 spawned.kill();86 });

Use `async/await` instead of direct `Promise` manipulation (#659)ehmickybefore adoption7122767

line 87why, one step back
87 finalizers.push(removeExitHandler);

Rename internal variables (#379)ehmickybefore adoption1ac56ea

line 88why, one step back
88};