Reimplement missing features. Fixes #30 & #32 (#68)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Reimplement stream cleanup behavior from node core. Fixes #32 * Implement timeout function. Fixes #30. * add longer delay to timeout test, because AppVeyor.
authored by James Talmage · committed by Sindre Sorhus · parent 842be7c · Browse files · 4 files·+83 added−14 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+83 added−14 removed
- other:+83 added−14 removed
Provenance
- Commit
e96350d- Tree
da0ef2b- Author
James Talmage <james.talmage@jrtechnical.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
842be7c
4 files changed+83 added−14 removed
added:fixtures/delay+9 added
1+#!/usr/bin/env node2+'use strict';3+4+setTimeout(() => {5+ console.log('delay completed');6+ process.exit(Number(process.argv[3] || 0));7+}, Number(process.argv[2]));8+9+console.log('delay started');modified:index.js+47 added-14 removed
4 unmodified lines5 const stripEof = require('strip-eof');6 const npmRunPath = require('npm-run-path');7 const isStream = require('is-stream');8 const _getStream = require('get-stream');9+const pFinally = require('p-finally');10 const onExit = require('signal-exit');11 const errname = require('./lib/errname');12 13 const TEN_MEGABYTES = 1000 * 1000 * 10;99 unmodified lines113 throw err;114 });115 }116 116-const processDone = spawned => new Promise(resolve => {117- spawned.on('exit', (code, signal) => {118- resolve({code, signal});119- });120-121- spawned.on('error', err => {122- resolve({err});123- });124-});125-117 module.exports = (cmd, args, opts) => {118 let joinedCmd = cmd;119 120 if (Array.isArray(args) && args.length > 0) {17 unmodified lines138 spawned.kill();139 });140 }141 142+ let timeoutId = null;143+ let timedOut = false;144+145+ const cleanupTimeout = () => {146+ if (timeoutId) {147+ clearTimeout(timeoutId);148+ timeoutId = null;149+ }150+ };151+152+ if (parsed.opts.timeout > 0) {153+ timeoutId = setTimeout(() => {154+ timeoutId = null;155+ timedOut = true;156+ spawned.kill(parsed.killSignal);157+ }, parsed.opts.timeout);158+ }159+160+ const processDone = new Promise(resolve => {161+ spawned.on('exit', (code, signal) => {162+ cleanupTimeout();163+ resolve({code, signal});164+ });165+166+ spawned.on('error', err => {167+ cleanupTimeout();168+ resolve({err});169+ });170+ });171+172+ function destroy() {173+ if (spawned.stdout) {174+ spawned.stdout.destroy();175+ }176+177+ if (spawned.stderr) {178+ spawned.stderr.destroy();179+ }180+ }181+151- const promise = Promise.all([152- processDone(spawned),182+ const promise = pFinally(Promise.all([183+ processDone,184 getStream(spawned, 'stdout', encoding, maxBuffer),185 getStream(spawned, 'stderr', encoding, maxBuffer)186 ]).then(arr => {187 const result = arr[0];23 unmodified lines211 err.stderr = stderr;212 err.failed = true;213 err.signal = signal || null;214 err.cmd = joinedCmd;215+ err.timedOut = timedOut;216 217 if (!parsed.opts.reject) {218 return err;219 }7 unmodified lines227 code: 0,228 failed: false,229 killed: false,230 signal: null,199- cmd: joinedCmd231+ cmd: joinedCmd,232+ timedOut: false233 };201- });234+ }), destroy);235 236 crossSpawn._enoent.hookChildProcess(spawned, parsed);237 238 handleInput(spawned, parsed.opts);37 unmodified linesmodified:package.json+1 added
46 unmodified lines47484950515253545524 unmodified lines46 unmodified lines "cross-spawn": "^4.0.0", "get-stream": "^2.2.0", "is-stream": "^1.1.0", "npm-run-path": "^2.0.0", "p-finally": "^1.0.0", "signal-exit": "^3.0.0", "strip-eof": "^1.0.0" }, "devDependencies": {24 unmodified linesmodified:test.js+26 added
257 unmodified lines258 test('err.code is 2', code, 2);259 test('err.code is 3', code, 3);260 test('err.code is 4', code, 4);261 262+test('timeout will kill the process early', async t => {263+ const err = await t.throws(m('delay', ['3000', '0'], {timeout: 1500}));264+265+ t.true(err.timedOut);266+ t.not(err.code, 22);267+});268+269+test('timeout will not kill the process early', async t => {270+ const err = await t.throws(m('delay', ['3000', '22'], {timeout: 9000}));271+272+ t.false(err.timedOut);273+ t.is(err.code, 22);274+});275+276+test('timedOut will be false if no timeout was set and zero exit code', async t => {277+ const result = await m('delay', ['1000', '0']);278+279+ t.false(result.timedOut);280+});281+282+test('timedOut will be false if no timeout was set and non-zero exit code', async t => {283+ const err = await t.throws(m('delay', ['1000', '3']));284+285+ t.false(err.timedOut);286+});287+288 async function errorMessage(t, expected, ...args) {289 const err = await t.throws(m('exit', args));290 291 t.regex(err.message, expected);64 unmodified lines