reimplement errname lookup. (#38)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by James Talmage · committed by Sindre Sorhus · parent 6288806 · Browse files · 3 files·+74 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+74 added−4 removed
- tests:+33 added−0 removed
- other:+41 added−4 removed
Provenance
- Commit
a1d1764- Tree
5604a86- Author
James Talmage <james.talmage@jrtechnical.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
6288806
3 files changed+74 added−4 removed
added:lib/errname.js+39 added
123456789101112131415161718192021222324252627282930313233343536373839'use strict';// The Node team wants to deprecate `process.bind(...)`.// https://github.com/nodejs/node/pull/2768//// However, we need the 'uv' binding for errname support.// This is a defensive wrapper around it so `execa` will not fail entirely if it stops working someday.//// If this ever stops working. See: https://github.com/sindresorhus/execa/issues/31#issuecomment-215939939 for another possible solution.var uv;
try { uv = process.binding('uv');
if (typeof uv.errname !== 'function') { throw new Error('uv.errname is not a function'); }} catch (err) { console.error('execa/lib/errname: unable to establish process.binding(\'uv\')', err); uv = null;}
function errname(uv, code) { if (uv) { return uv.errname(code); }
if (!(code < 0)) { throw new Error('err >= 0'); }
return 'Unknown system error ' + code;}
module.exports = function getErrname(code) { return errname(uv, code);};
// used for testing the fallback behavior.module.exports.__test__ = errname;added:test/errname.js+33 added
123456789101112131415161718192021222324252627282930313233import test from 'ava';import errname from '../lib/errname';
const isWin = process.platform === 'win32';const majorVersion = Number(process.version.substr(1).split('.')[0]);
// simulates failure to capture process.binding('uv');function fallback(code) { return errname.__test__(null, code);}
function makeTests(name, m, expected) { test(`${name}: >=0 exit codes`, t => { // throws >= 0 t.throws(() => m(0), /err >= 0/); t.throws(() => m(1), /err >= 0/); t.throws(() => m('2'), /err >= 0/); t.throws(() => m('foo'), /err >= 0/); });
if (!(isWin && majorVersion < 1)) { // causes process.exit() on Node 0.12 for Windows. test(`${name}: negative exit codes`, t => { t.is(m(-2), expected); t.is(m('-2'), expected); }); }}
const unknown = 'Unknown system error -2';
makeTests('native', errname, isWin ? unknown : 'ENOENT');makeTests('fallback', fallback, unknown);modified:index.js+2 added-4 removed
6 unmodified lines7 var npmRunPath = require('npm-run-path');8 var isStream = require('is-stream');9 var _getStream = require('get-stream');10 var pathKey = require('path-key')();11+var errname = require('./lib/errname');12 var TEN_MEBIBYTE = 1024 * 1024 * 10;13 14 function handleArgs(cmd, args, opts) {15 var parsed;143 unmodified lines159 // https://github.com/nodejs/node/blob/master/lib/child_process.js#L203160 // err.killed = spawned.killed || killed;161 err.killed = spawned.killed;162 162- // TODO: child_process applies the following logic for resolving the code:163- // var uv = process.bind('uv');164- // ex.code = code < 0 ? uv.errname(code) : code;165- err.code = code;163+ err.code = code < 0 ? errname(code) : code;164 }165 166 err.stdout = stdout;167 err.stderr = stderr;57 unmodified lines