From 09820c7295746a5acae619bae444a58d31594f41 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Mon, 10 Jun 2019 10:00:00 +0200 Subject: [PATCH] Refactor `kill()` --- index.js | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 34bf68b89f..8d5363904d 100644 --- a/index.js +++ b/index.js @@ -224,20 +224,29 @@ function setKillTimeout(kill, signal, options, killResult) { return; } - const forceKillAfter = Number.isInteger(options.forceKillAfter) ? - options.forceKillAfter : - 5000; - setTimeout(() => kill('SIGKILL'), forceKillAfter).unref(); + const timeout = getForceKillAfterTimeout(options); + setTimeout(() => kill('SIGKILL'), timeout).unref(); } -function shouldForceKill(signal, options, killResult) { - return ((typeof signal === 'string' && - signal.toUpperCase() === 'SIGTERM') || - signal === os.constants.signals.SIGTERM) && - options.forceKill !== false && - killResult; +function shouldForceKill(signal, {forceKill}, killResult) { + return isSigterm(signal) && forceKill !== false && killResult; } +function isSigterm(signal) { + return signal === os.constants.signals.SIGTERM || + (typeof signal === 'string' && signal.toUpperCase() === 'SIGTERM'); +} + +function getForceKillAfterTimeout({forceKillAfter}) { + if (Number.isInteger(forceKillAfter)) { + return forceKillAfter; + } + + return DEFAULT_FORCE_KILL_TIMEOUT; +} + +const DEFAULT_FORCE_KILL_TIMEOUT = 5000; + const execa = (file, args, options) => { const parsed = handleArgs(file, args, options); const {encoding, buffer, maxBuffer} = parsed.options;