Refactor the `timeout` option (#333)
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 9020f28 · Browse files · 6 files·+52 added−25 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+52 added−25 removed
- tests:+14 added−0 removed
- other:+38 added−25 removed
Provenance
- Commit
2a7551f- Tree
469aec0- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
9020f28
6 files changed+52 added−25 removed
modified:lib/error.js+1 added-1 removed
72 unmodified lines737475767777787980816 unmodified lines72 unmodified lines delete error.bufferedData; }
error.failed = true; error.timedOut = timedOut; error.timedOut = Boolean(timedOut); error.isCanceled = isCanceled; error.killed = killed && !timedOut; // `signal` emitted on `spawned.on('exit')` event can be `null`. We normalize // it to `undefined`6 unmodified linesmodified:lib/kill.js+27 added-11 removed
1234567843 unmodified lines52535455565758596061565758596061626364656667686970717273747576777879808182838485865 unmodified lines929394957677787980969798991008 unmodified lines'use strict';const os = require('os');const onExit = require('signal-exit');const pFinally = require('p-finally');
const DEFAULT_FORCE_KILL_TIMEOUT = 1000 * 5;
// Monkey-patches `childProcess.kill()` to add `forceKillAfterTimeout` behavior43 unmodified lines context.isCanceled = true; }};
const timeoutKill = (spawned, signal, reject) => { spawned.kill(signal); reject(Object.assign(new Error('Timed out'), {timedOut: true, signal}));};
// `timeout` option handlingconst setupTimeout = (spawned, {timeout, killSignal}, context) => { if (timeout > 0) { return setTimeout(() => { context.timedOut = true; spawned.kill(killSignal); }, timeout);const setupTimeout = (spawned, {timeout, killSignal = 'SIGTERM'}, spawnedPromise) => { if (timeout === 0 || timeout === undefined) { return spawnedPromise; }
if (!Number.isInteger(timeout) || timeout < 0) { throw new TypeError(`Expected the \`timeout\` option to be a non-negative integer, got \`${timeout}\` (${typeof timeout})`); }
let timeoutId; const timeoutPromise = new Promise((resolve, reject) => { timeoutId = setTimeout(() => { timeoutKill(spawned, killSignal, reject); }, timeout); });
const safeSpawnedPromise = pFinally(spawnedPromise, () => { clearTimeout(timeoutId); });
return Promise.race([timeoutPromise, safeSpawnedPromise]);};
// `cleanup` option handlingconst setExitHandler = (spawned, {cleanup, detached}) => {5 unmodified lines spawned.kill(); });};
const cleanup = (timeoutId, removeExitHandler) => { if (timeoutId !== undefined) { clearTimeout(timeoutId); }
const cleanup = removeExitHandler => { if (removeExitHandler !== undefined) { removeExitHandler(); }};8 unmodified linesmodified:lib/promise.js+1 added-6 removed
25 unmodified lines26 return spawned;27 };28 29 // Use promises instead of `child_process` events30-const getSpawnedPromise = (spawned, context) => {30+const getSpawnedPromise = spawned => {31 return new Promise((resolve, reject) => {32 spawned.on('exit', (code, signal) => {33- if (context.timedOut) {34- reject(Object.assign(new Error('Timed out'), {code, signal}));35- return;36- }37-33 resolve({code, signal});34 });35 36 spawned.on('error', error => {16 unmodified linesmodified:lib/stream.js+1 added-1 removed
81 unmodified lines82 try {83 return await Promise.all([processDone, stdoutPromise, stderrPromise, allPromise]);84 } catch (error) {85 return Promise.all([86- {error, code: error.code, signal: error.signal},86+ {error, code: error.code, signal: error.signal, timedOut: error.timedOut},87 getBufferedData(stdout, stdoutPromise),88 getBufferedData(stderr, stderrPromise),89 getBufferedData(all, allPromise)90 ]);16 unmodified linesmodified:test/kill.js+14 added
96 unmodified lines97 const {timedOut} = await execa('delay', ['500'], {timeout: 1e8});98 t.false(timedOut);99 });100 101+const INVALID_TIMEOUT_REGEXP = /`timeout` option to be a non-negative integer/;102+103+test('timeout must not be negative', async t => {104+ await t.throws(() => {105+ execa('noop', {timeout: -1});106+ }, INVALID_TIMEOUT_REGEXP);107+});108+109+test('timeout must be an integer', async t => {110+ await t.throws(() => {111+ execa('noop', {timeout: false});112+ }, INVALID_TIMEOUT_REGEXP);113+});114+115 test('timedOut is false if no timeout was set', async t => {116 const {timedOut} = await execa('noop');117 t.false(timedOut);118 });139 unmodified linesmodified:index.js+8 added-6 removed
92 unmodified lines93 }));94 return mergePromise(dummySpawned, errorPromise);95 }96 97- const context = {timedOut: false, isCanceled: false};97+ const spawnedPromise = getSpawnedPromise(spawned);98 99+ const context = {isCanceled: false};100+101 spawned.kill = spawnedKill.bind(null, spawned.kill.bind(spawned));102 spawned.cancel = spawnedCancel.bind(null, spawned, context);103 102- const timeoutId = setupTimeout(spawned, parsed.options, context);104+ const timedPromise = setupTimeout(spawned, parsed.options, spawnedPromise);105 const removeExitHandler = setExitHandler(spawned, parsed.options);106 107 // TODO: Use native "finally" syntax when targeting Node.js 10106- const processDone = pFinally(getSpawnedPromise(spawned, context), () => {107- cleanup(timeoutId, removeExitHandler);108+ const processDone = pFinally(timedPromise, () => {109+ cleanup(removeExitHandler);110 });111 112 const handlePromise = async () => {111- const [{error, code, signal}, stdoutResult, stderrResult, allResult] = await getSpawnedResult(spawned, parsed.options, processDone);113+ const [{error, code, signal, timedOut}, stdoutResult, stderrResult, allResult] = await getSpawnedResult(spawned, parsed.options, processDone);114 const stdout = handleOutput(parsed.options, stdoutResult);115 const stderr = handleOutput(parsed.options, stderrResult);116 const all = handleOutput(parsed.options, allResult);117 6 unmodified lines124 stderr,125 all,126 command,127 parsed,126- timedOut: context.timedOut,128+ timedOut,129 isCanceled: context.isCanceled,130 killed: spawned.killed131 });132 126 unmodified lines