forked from sindresorhus/execa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
63 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'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 (e) { | ||
console.error('execa/lib/errname: unable to establish process.binding("uv")', e); | ||
uv = null; | ||
} | ||
|
||
function errname(uv, code) { | ||
if (uv) { | ||
return uv.errname(code); | ||
} | ||
|
||
if (!(code < 0)) { | ||
throw new Error('err >= 0'); | ||
} | ||
|
||
return 'UNKNOWN CODE: ' + code; | ||
} | ||
|
||
module.exports = function getErrname(code) { | ||
return errname(uv, code); | ||
}; | ||
|
||
// used for testing the fallback behavior. | ||
module.exports._test = errname; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import test from 'ava'; | ||
import errname from '../lib/errname'; | ||
|
||
// simulates failure to capture process.binding('uv'); | ||
function fallback(code) { | ||
return errname._test(null, code); | ||
} | ||
|
||
function makeTests(name, m, expected) { | ||
test(name, 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/); | ||
|
||
t.is(m(-2), expected); | ||
t.is(m('-2'), expected); | ||
}); | ||
} | ||
|
||
makeTests('native', errname, 'ENOENT'); | ||
makeTests('fallback', fallback, 'UNKNOWN CODE: -2'); |