Download node_modules/execa/lib/kill.js from Snapkitty/sovereign-apollo: direct link, hf CLI and curl.
- Browser
- Download file 3.09 kB
-
https://huggingface.co/Snapkitty/sovereign-apollo/resolve/main/node_modules/execa/lib/kill.js
- Command line
-
hf download hf://Snapkitty/sovereign-apollo/node_modules/execa/lib/kill.js
-
curl -L -o kill.js https://huggingface.co/Snapkitty/sovereign-apollo/resolve/main/node_modules/execa/lib/kill.js
3.09 kB
| import os from 'node:os'; | |
| import {onExit} from 'signal-exit'; | |
| const DEFAULT_FORCE_KILL_TIMEOUT = 1000 * 5; | |
| // Monkey-patches `childProcess.kill()` to add `forceKillAfterTimeout` behavior | |
| export const spawnedKill = (kill, signal = 'SIGTERM', options = {}) => { | |
| const killResult = kill(signal); | |
| setKillTimeout(kill, signal, options, killResult); | |
| return killResult; | |
| }; | |
| const setKillTimeout = (kill, signal, options, killResult) => { | |
| if (!shouldForceKill(signal, options, killResult)) { | |
| return; | |
| } | |
| const timeout = getForceKillAfterTimeout(options); | |
| const t = setTimeout(() => { | |
| kill('SIGKILL'); | |
| }, timeout); | |
| // Guarded because there's no `.unref()` when `execa` is used in the renderer | |
| // process in Electron. This cannot be tested since we don't run tests in | |
| // Electron. | |
| // istanbul ignore else | |
| if (t.unref) { | |
| t.unref(); | |
| } | |
| }; | |
| const shouldForceKill = (signal, {forceKillAfterTimeout}, killResult) => isSigterm(signal) && forceKillAfterTimeout !== false && killResult; | |
| const isSigterm = signal => signal === os.constants.signals.SIGTERM | |
| || (typeof signal === 'string' && signal.toUpperCase() === 'SIGTERM'); | |
| const getForceKillAfterTimeout = ({forceKillAfterTimeout = true}) => { | |
| if (forceKillAfterTimeout === true) { | |
| return DEFAULT_FORCE_KILL_TIMEOUT; | |
| } | |
| if (!Number.isFinite(forceKillAfterTimeout) || forceKillAfterTimeout < 0) { | |
| throw new TypeError(`Expected the \`forceKillAfterTimeout\` option to be a non-negative integer, got \`${forceKillAfterTimeout}\` (${typeof forceKillAfterTimeout})`); | |
| } | |
| return forceKillAfterTimeout; | |
| }; | |
| // `childProcess.cancel()` | |
| export const spawnedCancel = (spawned, context) => { | |
| const killResult = spawned.kill(); | |
| if (killResult) { | |
| context.isCanceled = true; | |
| } | |
| }; | |
| const timeoutKill = (spawned, signal, reject) => { | |
| spawned.kill(signal); | |
| reject(Object.assign(new Error('Timed out'), {timedOut: true, signal})); | |
| }; | |
| // `timeout` option handling | |
| export const setupTimeout = (spawned, {timeout, killSignal = 'SIGTERM'}, spawnedPromise) => { | |
| if (timeout === 0 || timeout === undefined) { | |
| return spawnedPromise; | |
| } | |
| let timeoutId; | |
| const timeoutPromise = new Promise((resolve, reject) => { | |
| timeoutId = setTimeout(() => { | |
| timeoutKill(spawned, killSignal, reject); | |
| }, timeout); | |
| }); | |
| const safeSpawnedPromise = spawnedPromise.finally(() => { | |
| clearTimeout(timeoutId); | |
| }); | |
| return Promise.race([timeoutPromise, safeSpawnedPromise]); | |
| }; | |
| export const validateTimeout = ({timeout}) => { | |
| if (timeout !== undefined && (!Number.isFinite(timeout) || timeout < 0)) { | |
| throw new TypeError(`Expected the \`timeout\` option to be a non-negative integer, got \`${timeout}\` (${typeof timeout})`); | |
| } | |
| }; | |
| // `cleanup` option handling | |
| export const setExitHandler = async (spawned, {cleanup, detached}, timedPromise) => { | |
| if (!cleanup || detached) { | |
| return timedPromise; | |
| } | |
| const removeExitHandler = onExit(() => { | |
| spawned.kill(); | |
| }); | |
| return timedPromise.finally(() => { | |
| removeExitHandler(); | |
| }); | |
| }; | |