Skip to content
Myr

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

6 files+52 added−25 removed

modified:lib/error.js+1 added-1 removed
modified:lib/kill.js+27 added-11 removed
modified:lib/promise.js+1 added-6 removed
25 unmodified lines
26 return spawned;27};2829// Use promises instead of `child_process` events30const getSpawnedPromise = (spawned, context) => {30const 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 }3733 resolve({code, signal});34 });3536 spawned.on('error', error => {
16 unmodified lines
modified:lib/stream.js+1 added-1 removed
81 unmodified lines
82 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 lines
modified:test/kill.js+14 added
96 unmodified lines
97 const {timedOut} = await execa('delay', ['500'], {timeout: 1e8});98 t.false(timedOut);99});100101const INVALID_TIMEOUT_REGEXP = /`timeout` option to be a non-negative integer/;102103test('timeout must not be negative', async t => {104 await t.throws(() => {105 execa('noop', {timeout: -1});106 }, INVALID_TIMEOUT_REGEXP);107});108109test('timeout must be an integer', async t => {110 await t.throws(() => {111 execa('noop', {timeout: false});112 }, INVALID_TIMEOUT_REGEXP);113});114115test('timedOut is false if no timeout was set', async t => {116 const {timedOut} = await execa('noop');117 t.false(timedOut);118});
139 unmodified lines
modified:index.js+8 added-6 removed
92 unmodified lines
93 }));94 return mergePromise(dummySpawned, errorPromise);95 }9697 const context = {timedOut: false, isCanceled: false};97 const spawnedPromise = getSpawnedPromise(spawned);9899 const context = {isCanceled: false};100101 spawned.kill = spawnedKill.bind(null, spawned.kill.bind(spawned));102 spawned.cancel = spawnedCancel.bind(null, spawned, context);103102 const timeoutId = setupTimeout(spawned, parsed.options, context);104 const timedPromise = setupTimeout(spawned, parsed.options, spawnedPromise);105 const removeExitHandler = setExitHandler(spawned, parsed.options);106107 // 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 });111112 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 lines
124 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