Refactor `kill()` again (#280)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by Sindre Sorhus · parent 55337f7 · Browse files · 2 files·+32 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+32 added−10 removed
- other:+32 added−10 removed
Provenance
- Commit
1b38dc9- Tree
27a3dc0- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
55337f7
2 files changed+32 added−10 removed
modified:index.js+20 added-10 removed
12 unmodified lines13 const onExit = require('signal-exit');14 const stdio = require('./lib/stdio');15 16 const TEN_MEGABYTES = 1000 * 1000 * 10;17+const DEFAULT_FORCE_KILL_TIMEOUT = 1000 * 5;18 19 const SPACES_REGEXP = / +/g;20 21 function handleArgs(file, args, options = {}) {202 unmodified lines224 if (!shouldForceKill(signal, options, killResult)) {225 return;226 }227 227- const forceKillAfter = Number.isInteger(options.forceKillAfter) ?228- options.forceKillAfter :229- 5000;230- setTimeout(() => kill('SIGKILL'), forceKillAfter).unref();228+ const timeout = getForceKillAfterTimeout(options);229+ setTimeout(() => {230+ kill('SIGKILL');231+ }, timeout).unref();232 }233 233-function shouldForceKill(signal, options, killResult) {234- return ((typeof signal === 'string' &&235- signal.toUpperCase() === 'SIGTERM') ||236- signal === os.constants.signals.SIGTERM) &&237- options.forceKill !== false &&238- killResult;234+function shouldForceKill(signal, {forceKill}, killResult) {235+ return isSigterm(signal) && forceKill !== false && killResult;236 }237 238+function isSigterm(signal) {239+ return signal === os.constants.signals.SIGTERM ||240+ (typeof signal === 'string' && signal.toUpperCase() === 'SIGTERM');241+}242+243+function getForceKillAfterTimeout({forceKillAfter = DEFAULT_FORCE_KILL_TIMEOUT}) {244+ if (!Number.isInteger(forceKillAfter) || forceKillAfter < 0) {245+ throw new TypeError(`Expected the \`forceKillAfter\` option to be a non-negative integer, got \`${forceKillAfter}\` (${typeof forceKillAfter})`);246+ }247+248+ return forceKillAfter;249+}250+251 const execa = (file, args, options) => {252 const parsed = handleArgs(file, args, options);253 const {encoding, buffer, maxBuffer} = parsed.options;254 const command = joinCommand(file, args);250 unmodified linesmodified:test.js+12 added
173 unmodified lines174 175 const {signal} = await t.throwsAsync(subprocess);176 t.is(signal, 'SIGKILL');177 });178+179+ test('.kill() `forceKillAfter` should not be a float', t => {180+ t.throws(() => {181+ execa('noop').kill('SIGTERM', {forceKillAfter: 0.5});182+ }, {instanceOf: TypeError, message: /non-negative integer/});183+ });184+185+ test('.kill() `forceKillAfter` should not be negative', t => {186+ t.throws(() => {187+ execa('noop').kill('SIGTERM', {forceKillAfter: -1});188+ }, {instanceOf: TypeError, message: /non-negative integer/});189+ });190 }191 192 test('stripFinalNewline: true', async t => {193 const {stdout} = await execa('noop', ['foo']);619 unmodified lines