Skip to content
Myr

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

2 files+32 added−10 removed

modified:index.js+20 added-10 removed
12 unmodified lines
13const onExit = require('signal-exit');14const stdio = require('./lib/stdio');1516const TEN_MEGABYTES = 1000 * 1000 * 10;17const DEFAULT_FORCE_KILL_TIMEOUT = 1000 * 5;1819const SPACES_REGEXP = / +/g;2021function handleArgs(file, args, options = {}) {
202 unmodified lines
224 if (!shouldForceKill(signal, options, killResult)) {225 return;226 }227227 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}233233function 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;234function shouldForceKill(signal, {forceKill}, killResult) {235 return isSigterm(signal) && forceKill !== false && killResult;236}237238function isSigterm(signal) {239 return signal === os.constants.signals.SIGTERM ||240 (typeof signal === 'string' && signal.toUpperCase() === 'SIGTERM');241}242243function 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 }247248 return forceKillAfter;249}250251const 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 lines
modified:test.js+12 added
173 unmodified lines
174175 const {signal} = await t.throwsAsync(subprocess);176 t.is(signal, 'SIGKILL');177 });178179 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 });184185 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}191192test('stripFinalNewline: true', async t => {193 const {stdout} = await execa('noop', ['foo']);
619 unmodified lines