Separate spawned.kill() features into own file (#312)
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 ec10a4c · Browse files · 4 files·+338 added−313 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+338 added−313 removed
- tests:+245 added−234 removed
- other:+93 added−79 removed
Provenance
- Commit
230c7a7- Tree
ae78cbc- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
ec10a4c
4 files changed+338 added−313 removed
added:lib/kill.js+92 added
1+'use strict';2+const os = require('os');3+const onExit = require('signal-exit');4+5+const DEFAULT_FORCE_KILL_TIMEOUT = 1000 * 5;6+7+// Monkey-patches `childProcess.kill()` to add `forceKillAfterTimeout` behavior8+const spawnedKill = (kill, signal = 'SIGTERM', options = {}) => {9+ const killResult = kill(signal);10+ setKillTimeout(kill, signal, options, killResult);11+ return killResult;12+};13+14+const setKillTimeout = (kill, signal, options, killResult) => {15+ if (!shouldForceKill(signal, options, killResult)) {16+ return;17+ }18+19+ const timeout = getForceKillAfterTimeout(options);20+ setTimeout(() => {21+ kill('SIGKILL');22+ }, timeout).unref();23+};24+25+const shouldForceKill = (signal, {forceKillAfterTimeout}, killResult) => {26+ return isSigterm(signal) && forceKillAfterTimeout !== false && killResult;27+};28+29+const isSigterm = signal => {30+ return signal === os.constants.signals.SIGTERM ||31+ (typeof signal === 'string' && signal.toUpperCase() === 'SIGTERM');32+};33+34+const getForceKillAfterTimeout = ({forceKillAfterTimeout = true}) => {35+ if (forceKillAfterTimeout === true) {36+ return DEFAULT_FORCE_KILL_TIMEOUT;37+ }38+39+ if (!Number.isInteger(forceKillAfterTimeout) || forceKillAfterTimeout < 0) {40+ throw new TypeError(`Expected the \`forceKillAfterTimeout\` option to be a non-negative integer, got \`${forceKillAfterTimeout}\` (${typeof forceKillAfterTimeout})`);41+ }42+43+ return forceKillAfterTimeout;44+};45+46+// `childProcess.cancel()`47+const spawnedCancel = (spawned, context) => {48+ const killResult = spawned.kill();49+50+ if (killResult) {51+ context.isCanceled = true;52+ }53+};54+55+// `timeout` option handling56+const setupTimeout = (spawned, {timeout, killSignal}, context) => {57+ if (timeout > 0) {58+ return setTimeout(() => {59+ context.timedOut = true;60+ spawned.kill(killSignal);61+ }, timeout);62+ }63+};64+65+// `cleanup` option handling66+const setExitHandler = (spawned, {cleanup, detached}) => {67+ if (!cleanup || detached) {68+ return;69+ }70+71+ return onExit(() => {72+ spawned.kill();73+ });74+};75+76+const cleanup = (timeoutId, removeExitHandler) => {77+ if (timeoutId !== undefined) {78+ clearTimeout(timeoutId);79+ }80+81+ if (removeExitHandler !== undefined) {82+ removeExitHandler();83+ }84+};85+86+module.exports = {87+ spawnedKill,88+ spawnedCancel,89+ setupTimeout,90+ setExitHandler,91+ cleanup92+};added:test/kill.js+243 added
1+import path from 'path';2+import test from 'ava';3+import pEvent from 'p-event';4+import isRunning from 'is-running';5+import execa from '..';6+7+process.env.PATH = path.join(__dirname, 'fixtures') + path.delimiter + process.env.PATH;8+9+const TIMEOUT_REGEXP = /timed out after/;10+11+test('kill("SIGKILL") should terminate cleanly', async t => {12+ const subprocess = execa('node', ['./test/fixtures/no-killable'], {stdio: ['ipc']});13+ await pEvent(subprocess, 'message');14+15+ subprocess.kill('SIGKILL');16+17+ const {signal} = await t.throwsAsync(subprocess);18+ t.is(signal, 'SIGKILL');19+});20+21+// `SIGTERM` cannot be caught on Windows, and it always aborts the process (like `SIGKILL` on Unix).22+// Therefore, this feature and those tests do not make sense on Windows.23+if (process.platform !== 'win32') {24+ test('`forceKillAfterTimeout: false` should not kill after a timeout', async t => {25+ const subprocess = execa('node', ['./test/fixtures/no-killable'], {stdio: ['ipc']});26+ await pEvent(subprocess, 'message');27+28+ subprocess.kill('SIGTERM', {forceKillAfterTimeout: false});29+30+ t.true(isRunning(subprocess.pid));31+ subprocess.kill('SIGKILL');32+ });33+34+ test('`forceKillAfterTimeout: number` should kill after a timeout', async t => {35+ const subprocess = execa('node', ['./test/fixtures/no-killable'], {stdio: ['ipc']});36+ await pEvent(subprocess, 'message');37+38+ subprocess.kill('SIGTERM', {forceKillAfterTimeout: 50});39+40+ const {signal} = await t.throwsAsync(subprocess);41+ t.is(signal, 'SIGKILL');42+ });43+44+ test('`forceKillAfterTimeout: true` should kill after a timeout', async t => {45+ const subprocess = execa('node', ['./test/fixtures/no-killable'], {stdio: ['ipc']});46+ await pEvent(subprocess, 'message');47+48+ subprocess.kill('SIGTERM', {forceKillAfterTimeout: true});49+50+ const {signal} = await t.throwsAsync(subprocess);51+ t.is(signal, 'SIGKILL');52+ });53+54+ test('kill() with no arguments should kill after a timeout', async t => {55+ const subprocess = execa('node', ['./test/fixtures/no-killable'], {stdio: ['ipc']});56+ await pEvent(subprocess, 'message');57+58+ subprocess.kill();59+60+ const {signal} = await t.throwsAsync(subprocess);61+ t.is(signal, 'SIGKILL');62+ });63+64+ test('`forceKillAfterTimeout` should not be a float', t => {65+ t.throws(() => {66+ execa('noop').kill('SIGTERM', {forceKillAfterTimeout: 0.5});67+ }, {instanceOf: TypeError, message: /non-negative integer/});68+ });69+70+ test('`forceKillAfterTimeout` should not be negative', t => {71+ t.throws(() => {72+ execa('noop').kill('SIGTERM', {forceKillAfterTimeout: -1});73+ }, {instanceOf: TypeError, message: /non-negative integer/});74+ });75+}76+77+test('execa() returns a promise with kill()', t => {78+ const {kill} = execa('noop', ['foo']);79+ t.is(typeof kill, 'function');80+});81+82+test('timeout kills the process if it times out', async t => {83+ const {killed, timedOut} = await t.throwsAsync(execa('forever', {timeout: 1, message: TIMEOUT_REGEXP}));84+ t.false(killed);85+ t.true(timedOut);86+});87+88+test('timeout kills the process if it times out, in sync mode', async t => {89+ const {killed, timedOut} = await t.throws(() => {90+ execa.sync('forever', {timeout: 1, message: TIMEOUT_REGEXP});91+ });92+ t.false(killed);93+ t.true(timedOut);94+});95+96+test('timeout does not kill the process if it does not time out', async t => {97+ const {timedOut} = await execa('delay', ['500'], {timeout: 1e8});98+ t.false(timedOut);99+});100+101+test('timedOut is false if no timeout was set', async t => {102+ const {timedOut} = await execa('noop');103+ t.false(timedOut);104+});105+106+test('timedOut will be false if no timeout was set and zero exit code in sync mode', t => {107+ const {timedOut} = execa.sync('noop');108+ t.false(timedOut);109+});110+111+// When child process exits before parent process112+const spawnAndExit = async (t, cleanup, detached) => {113+ await t.notThrowsAsync(execa('sub-process-exit', [cleanup, detached]));114+};115+116+test('spawnAndExit', spawnAndExit, false, false);117+test('spawnAndExit cleanup', spawnAndExit, true, false);118+test('spawnAndExit detached', spawnAndExit, false, true);119+test('spawnAndExit cleanup detached', spawnAndExit, true, true);120+121+// When parent process exits before child process122+const spawnAndKill = async (t, signal, cleanup, detached, isKilled) => {123+ const subprocess = execa('sub-process', [cleanup, detached], {stdio: ['ignore', 'ignore', 'ignore', 'ipc']});124+125+ const pid = await pEvent(subprocess, 'message');126+ t.true(Number.isInteger(pid));127+ t.true(isRunning(pid));128+129+ process.kill(subprocess.pid, signal);130+131+ await t.throwsAsync(subprocess);132+133+ t.false(isRunning(subprocess.pid));134+ t.is(isRunning(pid), !isKilled);135+136+ if (!isKilled) {137+ process.kill(pid, 'SIGKILL');138+ }139+};140+141+// Without `options.cleanup`:142+// - on Windows subprocesses are killed if `options.detached: false`, but not143+// if `options.detached: true`144+// - on Linux subprocesses are never killed regardless of `options.detached`145+// With `options.cleanup`, subprocesses are always killed146+// - `options.cleanup` with SIGKILL is a noop, since it cannot be handled147+const exitIfWindows = process.platform === 'win32';148+test('spawnAndKill SIGTERM', spawnAndKill, 'SIGTERM', false, false, exitIfWindows);149+test('spawnAndKill SIGKILL', spawnAndKill, 'SIGKILL', false, false, exitIfWindows);150+test('spawnAndKill cleanup SIGTERM', spawnAndKill, 'SIGTERM', true, false, true);151+test('spawnAndKill cleanup SIGKILL', spawnAndKill, 'SIGKILL', true, false, exitIfWindows);152+test('spawnAndKill detached SIGTERM', spawnAndKill, 'SIGTERM', false, true, false);153+test('spawnAndKill detached SIGKILL', spawnAndKill, 'SIGKILL', false, true, false);154+test('spawnAndKill cleanup detached SIGTERM', spawnAndKill, 'SIGTERM', true, true, false);155+test('spawnAndKill cleanup detached SIGKILL', spawnAndKill, 'SIGKILL', true, true, false);156+157+// See #128158+test('removes exit handler on exit', async t => {159+ // FIXME: This relies on `signal-exit` internals160+ const ee = process.__signal_exit_emitter__;161+162+ const child = execa('noop');163+ const listener = ee.listeners('exit').pop();164+165+ await new Promise((resolve, reject) => {166+ child.on('error', reject);167+ child.on('exit', resolve);168+ });169+170+ const included = ee.listeners('exit').includes(listener);171+ t.false(included);172+});173+174+test('cancel method kills the subprocess', t => {175+ const subprocess = execa('node');176+ subprocess.cancel();177+ t.true(subprocess.killed);178+});179+180+test('result.isCanceled is false when spawned.cancel() isn\'t called (success)', async t => {181+ const {isCanceled} = await execa('noop');182+ t.false(isCanceled);183+});184+185+test('result.isCanceled is false when spawned.cancel() isn\'t called (failure)', async t => {186+ const {isCanceled} = await t.throwsAsync(execa('fail'));187+ t.false(isCanceled);188+});189+190+test('result.isCanceled is false when spawned.cancel() isn\'t called in sync mode (success)', t => {191+ const {isCanceled} = execa.sync('noop');192+ t.false(isCanceled);193+});194+195+test('result.isCanceled is false when spawned.cancel() isn\'t called in sync mode (failure)', t => {196+ const {isCanceled} = t.throws(() => {197+ execa.sync('fail');198+ });199+ t.false(isCanceled);200+});201+202+test('calling cancel method throws an error with message "Command was canceled"', async t => {203+ const subprocess = execa('noop');204+ subprocess.cancel();205+ await t.throwsAsync(subprocess, {message: /Command was canceled/});206+});207+208+test('error.isCanceled is true when cancel method is used', async t => {209+ const subprocess = execa('noop');210+ subprocess.cancel();211+ const {isCanceled} = await t.throwsAsync(subprocess);212+ t.true(isCanceled);213+});214+215+test('error.isCanceled is false when kill method is used', async t => {216+ const subprocess = execa('noop');217+ subprocess.kill();218+ const {isCanceled} = await t.throwsAsync(subprocess);219+ t.false(isCanceled);220+});221+222+test('calling cancel method twice should show the same behaviour as calling it once', async t => {223+ const subprocess = execa('noop');224+ subprocess.cancel();225+ subprocess.cancel();226+ const {isCanceled} = await t.throwsAsync(subprocess);227+ t.true(isCanceled);228+ t.true(subprocess.killed);229+});230+231+test('calling cancel method on a successfully completed process does not make result.isCanceled true', async t => {232+ const subprocess = execa('noop');233+ const {isCanceled} = await subprocess;234+ subprocess.cancel();235+ t.false(isCanceled);236+});237+238+test('calling cancel method on a process which has been killed does not make error.isCanceled true', async t => {239+ const subprocess = execa('noop');240+ subprocess.kill();241+ const {isCanceled} = await t.throwsAsync(subprocess);242+ t.false(isCanceled);243+});modified:test/test.js+2 added-234 removed
3 unmodified lines4 import test from 'ava';5 import getStream from 'get-stream';6 import isRunning from 'is-running';7 import tempfile from 'tempfile';8-import pEvent from 'p-event';8 import execa from '..';9 10 process.env.PATH = path.join(__dirname, 'fixtures') + path.delimiter + process.env.PATH;11 process.env.FOO = 'foo';12 14-const TIMEOUT_REGEXP = /timed out after/;13 const ENOENT_REGEXP = process.platform === 'win32' ? /failed with exit code 1/ : /spawn.* ENOENT/;14 15 test('execa()', async t => {16 const {stdout} = await execa('noop', ['foo']);69 unmodified lines86 const {exitCode} = execa.sync('fail', {reject: false});87 t.is(exitCode, 2);88 });89 92-test('kill("SIGKILL") should terminate cleanly', async t => {93- const subprocess = execa('node', ['./test/fixtures/no-killable'], {stdio: ['ipc']});94- await pEvent(subprocess, 'message');95-96- subprocess.kill('SIGKILL');97-98- const {signal} = await t.throwsAsync(subprocess);99- t.is(signal, 'SIGKILL');100-});101-102-// `SIGTERM` cannot be caught on Windows, and it always aborts the process (like `SIGKILL` on Unix).103-// Therefore, this feature and those tests do not make sense on Windows.104-if (process.platform !== 'win32') {105- test('`forceKillAfterTimeout: false` should not kill after a timeout', async t => {106- const subprocess = execa('node', ['./test/fixtures/no-killable'], {stdio: ['ipc']});107- await pEvent(subprocess, 'message');108-109- subprocess.kill('SIGTERM', {forceKillAfterTimeout: false});110-111- t.true(isRunning(subprocess.pid));112- subprocess.kill('SIGKILL');113- });114-115- test('`forceKillAfterTimeout: number` should kill after a timeout', async t => {116- const subprocess = execa('node', ['./test/fixtures/no-killable'], {stdio: ['ipc']});117- await pEvent(subprocess, 'message');118-119- subprocess.kill('SIGTERM', {forceKillAfterTimeout: 50});120-121- const {signal} = await t.throwsAsync(subprocess);122- t.is(signal, 'SIGKILL');123- });124-125- test('`forceKillAfterTimeout: true` should kill after a timeout', async t => {126- const subprocess = execa('node', ['./test/fixtures/no-killable'], {stdio: ['ipc']});127- await pEvent(subprocess, 'message');128-129- subprocess.kill('SIGTERM', {forceKillAfterTimeout: true});130-131- const {signal} = await t.throwsAsync(subprocess);132- t.is(signal, 'SIGKILL');133- });134-135- test('kill() with no arguments should kill after a timeout', async t => {136- const subprocess = execa('node', ['./test/fixtures/no-killable'], {stdio: ['ipc']});137- await pEvent(subprocess, 'message');138-139- subprocess.kill();140-141- const {signal} = await t.throwsAsync(subprocess);142- t.is(signal, 'SIGKILL');143- });144-145- test('`forceKillAfterTimeout` should not be a float', t => {146- t.throws(() => {147- execa('noop').kill('SIGTERM', {forceKillAfterTimeout: 0.5});148- }, {instanceOf: TypeError, message: /non-negative integer/});149- });150-151- test('`forceKillAfterTimeout` should not be negative', t => {152- t.throws(() => {153- execa('noop').kill('SIGTERM', {forceKillAfterTimeout: -1});154- }, {instanceOf: TypeError, message: /non-negative integer/});155- });156-}157-90 test('stripFinalNewline: true', async t => {91 const {stdout} = await execa('noop', ['foo']);92 t.is(stdout, 'foo');93 });109 unmodified lines203 child.emit('error', new Error('test'));204 await t.throwsAsync(child, /test/);205 });206 275-test('execa() returns a promise with kill() and pid', t => {276- const {kill, pid} = execa('noop', ['foo']);277- t.is(typeof kill, 'function');207+test('execa() returns a promise with pid', t => {208+ const {pid} = execa('noop', ['foo']);209 t.is(typeof pid, 'number');210 });211 212 test('child_process.spawn() propagated errors have correct shape', t => {71 unmodified lines284 await t.throwsAsync(execa('non-executable', {input: 'Hey!'}), /EACCES/);285 });286 }287 357-test('timeout kills the process if it times out', async t => {358- const {killed, timedOut} = await t.throwsAsync(execa('forever', {timeout: 1, message: TIMEOUT_REGEXP}));359- t.false(killed);360- t.true(timedOut);361-});362-363-test('timeout kills the process if it times out, in sync mode', async t => {364- const {killed, timedOut} = await t.throws(() => {365- execa.sync('forever', {timeout: 1, message: TIMEOUT_REGEXP});366- });367- t.false(killed);368- t.true(timedOut);369-});370-371-test('timeout does not kill the process if it does not time out', async t => {372- const {timedOut} = await execa('delay', ['500'], {timeout: 1e8});373- t.false(timedOut);374-});375-376-test('timedOut is false if no timeout was set', async t => {377- const {timedOut} = await execa('noop');378- t.false(timedOut);379-});380-381-test('timedOut will be false if no timeout was set and zero exit code in sync mode', t => {382- const {timedOut} = execa.sync('noop');383- t.false(timedOut);384-});385-288 const command = async (t, expected, ...args) => {289 const {command: failCommand} = await t.throwsAsync(execa('fail', args));290 t.is(failCommand, `fail${expected}`);291 6 unmodified lines298 test(command, ' foo bar', 'foo', 'bar');299 test(command, ' baz quz', 'baz', 'quz');300 test(command, '');301 400-// When child process exits before parent process401-const spawnAndExit = async (t, cleanup, detached) => {402- await t.notThrowsAsync(execa('sub-process-exit', [cleanup, detached]));403-};404-405-test('spawnAndExit', spawnAndExit, false, false);406-test('spawnAndExit cleanup', spawnAndExit, true, false);407-test('spawnAndExit detached', spawnAndExit, false, true);408-test('spawnAndExit cleanup detached', spawnAndExit, true, true);409-410-// When parent process exits before child process411-const spawnAndKill = async (t, signal, cleanup, detached, isKilled) => {412- const subprocess = execa('sub-process', [cleanup, detached], {stdio: ['ignore', 'ignore', 'ignore', 'ipc']});413-414- const pid = await pEvent(subprocess, 'message');415- t.true(Number.isInteger(pid));416- t.true(isRunning(pid));417-418- process.kill(subprocess.pid, signal);419-420- await t.throwsAsync(subprocess);421-422- t.false(isRunning(subprocess.pid));423- t.is(isRunning(pid), !isKilled);424-425- if (!isKilled) {426- process.kill(pid, 'SIGKILL');427- }428-};429-430-// Without `options.cleanup`:431-// - on Windows subprocesses are killed if `options.detached: false`, but not432-// if `options.detached: true`433-// - on Linux subprocesses are never killed regardless of `options.detached`434-// With `options.cleanup`, subprocesses are always killed435-// - `options.cleanup` with SIGKILL is a noop, since it cannot be handled436-const exitIfWindows = process.platform === 'win32';437-test('spawnAndKill SIGTERM', spawnAndKill, 'SIGTERM', false, false, exitIfWindows);438-test('spawnAndKill SIGKILL', spawnAndKill, 'SIGKILL', false, false, exitIfWindows);439-test('spawnAndKill cleanup SIGTERM', spawnAndKill, 'SIGTERM', true, false, true);440-test('spawnAndKill cleanup SIGKILL', spawnAndKill, 'SIGKILL', true, false, exitIfWindows);441-test('spawnAndKill detached SIGTERM', spawnAndKill, 'SIGTERM', false, true, false);442-test('spawnAndKill detached SIGKILL', spawnAndKill, 'SIGKILL', false, true, false);443-test('spawnAndKill cleanup detached SIGTERM', spawnAndKill, 'SIGTERM', true, true, false);444-test('spawnAndKill cleanup detached SIGKILL', spawnAndKill, 'SIGKILL', true, true, false);445-302 if (process.platform !== 'win32') {303 test('write to fast-exit process', async t => {304 // Try-catch here is necessary, because this test is not 100% accurate305 // Sometimes process can manage to accept input before exiting54 unmodified lines360 361 process.kill(pid, 'SIGKILL');362 });363 508-// See #128509-test('removes exit handler on exit', async t => {510- // FIXME: This relies on `signal-exit` internals511- const ee = process.__signal_exit_emitter__;512-513- const child = execa('noop');514- const listener = ee.listeners('exit').pop();515-516- await new Promise((resolve, reject) => {517- child.on('error', reject);518- child.on('exit', resolve);519- });520-521- const included = ee.listeners('exit').includes(listener);522- t.false(included);523-});524-364 test('promise methods are not enumerable', t => {365 const descriptors = Object.getOwnPropertyDescriptors(execa('noop'));366 // eslint-disable-next-line promise/prefer-await-to-then367 t.false(descriptors.then.enumerable);39 unmodified lines407 t.is(message, 'called');408 });409 }410 572-test('cancel method kills the subprocess', t => {573- const subprocess = execa('node');574- subprocess.cancel();575- t.true(subprocess.killed);576-});577-578-test('result.isCanceled is false when spawned.cancel() isn\'t called (success)', async t => {579- const {isCanceled} = await execa('noop');580- t.false(isCanceled);581-});582-583-test('result.isCanceled is false when spawned.cancel() isn\'t called (failure)', async t => {584- const {isCanceled} = await t.throwsAsync(execa('fail'));585- t.false(isCanceled);586-});587-588-test('result.isCanceled is false when spawned.cancel() isn\'t called in sync mode (success)', t => {589- const {isCanceled} = execa.sync('noop');590- t.false(isCanceled);591-});592-593-test('result.isCanceled is false when spawned.cancel() isn\'t called in sync mode (failure)', t => {594- const {isCanceled} = t.throws(() => {595- execa.sync('fail');596- });597- t.false(isCanceled);598-});599-600-test('calling cancel method throws an error with message "Command was canceled"', async t => {601- const subprocess = execa('noop');602- subprocess.cancel();603- await t.throwsAsync(subprocess, {message: /Command was canceled/});604-});605-606-test('error.isCanceled is true when cancel method is used', async t => {607- const subprocess = execa('noop');608- subprocess.cancel();609- const {isCanceled} = await t.throwsAsync(subprocess);610- t.true(isCanceled);611-});612-613-test('error.isCanceled is false when kill method is used', async t => {614- const subprocess = execa('noop');615- subprocess.kill();616- const {isCanceled} = await t.throwsAsync(subprocess);617- t.false(isCanceled);618-});619-620-test('calling cancel method twice should show the same behaviour as calling it once', async t => {621- const subprocess = execa('noop');622- subprocess.cancel();623- subprocess.cancel();624- const {isCanceled} = await t.throwsAsync(subprocess);625- t.true(isCanceled);626- t.true(subprocess.killed);627-});628-629-test('calling cancel method on a successfully completed process does not make result.isCanceled true', async t => {630- const subprocess = execa('noop');631- const {isCanceled} = await subprocess;632- subprocess.cancel();633- t.false(isCanceled);634-});635-636-test('calling cancel method on a process which has been killed does not make error.isCanceled true', async t => {637- const subprocess = execa('noop');638- subprocess.kill();639- const {isCanceled} = await t.throwsAsync(subprocess);640- t.false(isCanceled);641-});642-411 test('allow commands with spaces and no array arguments', async t => {412 const {stdout} = await execa('command with space');413 t.is(stdout, '');414 });40 unmodified linesmodified:index.js+1 added-79 removed
1 'use strict';2 const path = require('path');3-const os = require('os');3 const childProcess = require('child_process');4 const crossSpawn = require('cross-spawn');5 const stripFinalNewline = require('strip-final-newline');6 const npmRunPath = require('npm-run-path');7 const isStream = require('is-stream');8 const getStream = require('get-stream');9 const mergeStream = require('merge-stream');10 const pFinally = require('p-finally');12-const onExit = require('signal-exit');11 const makeError = require('./lib/error');12 const normalizeStdio = require('./lib/stdio');13+const {spawnedKill, spawnedCancel, setupTimeout, setExitHandler, cleanup} = require('./lib/kill');14 15 const DEFAULT_MAX_BUFFER = 1000 * 1000 * 100;17-const DEFAULT_FORCE_KILL_TIMEOUT = 1000 * 5;16 17 const SPACES_REGEXP = / +/g;18 19 const handleArgs = (file, args, options = {}) => {167 unmodified lines187 188 return spawned;189 };190 193-const spawnedKill = (kill, signal = 'SIGTERM', options = {}) => {194- const killResult = kill(signal);195- setKillTimeout(kill, signal, options, killResult);196- return killResult;197-};198-199-const setKillTimeout = (kill, signal, options, killResult) => {200- if (!shouldForceKill(signal, options, killResult)) {201- return;202- }203-204- const timeout = getForceKillAfterTimeout(options);205- setTimeout(() => {206- kill('SIGKILL');207- }, timeout).unref();208-};209-210-const shouldForceKill = (signal, {forceKillAfterTimeout}, killResult) => {211- return isSigterm(signal) && forceKillAfterTimeout !== false && killResult;212-};213-214-const isSigterm = signal => {215- return signal === os.constants.signals.SIGTERM ||216- (typeof signal === 'string' && signal.toUpperCase() === 'SIGTERM');217-};218-219-const getForceKillAfterTimeout = ({forceKillAfterTimeout = true}) => {220- if (forceKillAfterTimeout === true) {221- return DEFAULT_FORCE_KILL_TIMEOUT;222- }223-224- if (!Number.isInteger(forceKillAfterTimeout) || forceKillAfterTimeout < 0) {225- throw new TypeError(`Expected the \`forceKillAfterTimeout\` option to be a non-negative integer, got \`${forceKillAfterTimeout}\` (${typeof forceKillAfterTimeout})`);226- }227-228- return forceKillAfterTimeout;229-};230-231-const spawnedCancel = (spawned, context) => {232- const killResult = spawned.kill();233-234- if (killResult) {235- context.isCanceled = true;236- }237-};238-191 const handleSpawned = (spawned, context) => {192 return new Promise((resolve, reject) => {193 spawned.on('exit', (code, signal) => {194 if (context.timedOut) {15 unmodified lines210 }211 });212 };213 262-const setupTimeout = (spawned, {timeout, killSignal}, context) => {263- if (timeout > 0) {264- return setTimeout(() => {265- context.timedOut = true;266- spawned.kill(killSignal);267- }, timeout);268- }269-};270-271-// #115272-const setExitHandler = (spawned, {cleanup, detached}) => {273- if (!cleanup || detached) {274- return;275- }276-277- return onExit(() => {278- spawned.kill();279- });280-};281-282-const cleanup = (timeoutId, removeExitHandler) => {283- if (timeoutId !== undefined) {284- clearTimeout(timeoutId);285- }286-287- if (removeExitHandler !== undefined) {288- removeExitHandler();289- }290-};291-214 const execa = (file, args, options) => {215 const parsed = handleArgs(file, args, options);216 const command = joinCommand(file, args);217 198 unmodified lines