Skip to content
Myr

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

4 files+83 added−14 removed

added:fixtures/delay+9 added
1#!/usr/bin/env node2'use strict';34setTimeout(() => {5 console.log('delay completed');6 process.exit(Number(process.argv[3] || 0));7}, Number(process.argv[2]));89console.log('delay started');
modified:index.js+47 added-14 removed
4 unmodified lines
5const stripEof = require('strip-eof');6const npmRunPath = require('npm-run-path');7const isStream = require('is-stream');8const _getStream = require('get-stream');9const pFinally = require('p-finally');10const onExit = require('signal-exit');11const errname = require('./lib/errname');1213const TEN_MEGABYTES = 1000 * 1000 * 10;
99 unmodified lines
113 throw err;114 });115}116116const processDone = spawned => new Promise(resolve => {117 spawned.on('exit', (code, signal) => {118 resolve({code, signal});119 });120121 spawned.on('error', err => {122 resolve({err});123 });124});125117module.exports = (cmd, args, opts) => {118 let joinedCmd = cmd;119120 if (Array.isArray(args) && args.length > 0) {
17 unmodified lines
138 spawned.kill();139 });140 }141142 let timeoutId = null;143 let timedOut = false;144145 const cleanupTimeout = () => {146 if (timeoutId) {147 clearTimeout(timeoutId);148 timeoutId = null;149 }150 };151152 if (parsed.opts.timeout > 0) {153 timeoutId = setTimeout(() => {154 timeoutId = null;155 timedOut = true;156 spawned.kill(parsed.killSignal);157 }, parsed.opts.timeout);158 }159160 const processDone = new Promise(resolve => {161 spawned.on('exit', (code, signal) => {162 cleanupTimeout();163 resolve({code, signal});164 });165166 spawned.on('error', err => {167 cleanupTimeout();168 resolve({err});169 });170 });171172 function destroy() {173 if (spawned.stdout) {174 spawned.stdout.destroy();175 }176177 if (spawned.stderr) {178 spawned.stderr.destroy();179 }180 }181151 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 lines
211 err.stderr = stderr;212 err.failed = true;213 err.signal = signal || null;214 err.cmd = joinedCmd;215 err.timedOut = timedOut;216217 if (!parsed.opts.reject) {218 return err;219 }
7 unmodified lines
227 code: 0,228 failed: false,229 killed: false,230 signal: null,199 cmd: joinedCmd231 cmd: joinedCmd,232 timedOut: false233 };201 });234 }), destroy);235236 crossSpawn._enoent.hookChildProcess(spawned, parsed);237238 handleInput(spawned, parsed.opts);
37 unmodified lines
modified:package.json+1 added
modified:test.js+26 added
257 unmodified lines
258test('err.code is 2', code, 2);259test('err.code is 3', code, 3);260test('err.code is 4', code, 4);261262test('timeout will kill the process early', async t => {263 const err = await t.throws(m('delay', ['3000', '0'], {timeout: 1500}));264265 t.true(err.timedOut);266 t.not(err.code, 22);267});268269test('timeout will not kill the process early', async t => {270 const err = await t.throws(m('delay', ['3000', '22'], {timeout: 9000}));271272 t.false(err.timedOut);273 t.is(err.code, 22);274});275276test('timedOut will be false if no timeout was set and zero exit code', async t => {277 const result = await m('delay', ['1000', '0']);278279 t.false(result.timedOut);280});281282test('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']));284285 t.false(err.timedOut);286});287288async function errorMessage(t, expected, ...args) {289 const err = await t.throws(m('exit', args));290291 t.regex(err.message, expected);
64 unmodified lines