| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | // The Node team wants to deprecate `process.bind(...)`. |
| 3 | // https://github.com/nodejs/node/pull/2768 |
| 4 | // |
| 5 | // However, we need the 'uv' binding for errname support. |
| 6 | // This is a defensive wrapper around it so `execa` will not fail entirely if it stops working someday. |
| 7 | // |
| 8 | // If this ever stops working. See: https://github.com/sindresorhus/execa/issues/31#issuecomment-215939939 for another possible solution. |
| 9 | let uv; |
| 10 | |
| 11 | try { |
| 12 | uv = process.binding('uv'); |
| 13 | |
| 14 | if (typeof uv.errname !== 'function') { |
| 15 | throw new TypeError('uv.errname is not a function'); |
| 16 | } |
| 17 | } catch (err) { |
| 18 | console.error('execa/lib/errname: unable to establish process.binding(\'uv\')', err); |
| 19 | uv = null; |
| 20 | } |
| 21 | |
| 22 | function errname(uv, code) { |
| 23 | if (uv) { |
| 24 | return uv.errname(code); |
| 25 | } |
| 26 | |
| 27 | if (!(code < 0)) { |
| 28 | throw new Error('err >= 0'); |
| 29 | } |
| 30 | |
| 31 | return `Unknown system error ${code}`; |
| 32 | } |
| 33 | |
| 34 | module.exports = code => errname(uv, code); |
| 35 | |
| 36 | // Used for testing the fallback behavior |
| 37 | module.exports.__test__ = errname; |