Skip to content

Commit

Permalink
[New] use a global symbol for util.promisify.custom
Browse files Browse the repository at this point in the history
Define `util.promisify.custom` as `Symbol.for("nodejs.util.inspect.custom")`, rather than as `Symbol("util.inspect.custom")`.

This allows custom `promisify` wrappers to easily/safely be defined in non‑Node.js environments.

Refs: nodejs/node#31647
Refs: nodejs/node#31672
  • Loading branch information
ExE-Boss authored and ljharb committed Feb 7, 2020
1 parent e68f500 commit 8f8631b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
3 changes: 2 additions & 1 deletion implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ var $forEach = callBound('Array.prototype.forEach');

var hasSymbols = require('has-symbols')();

var kCustomPromisifiedSymbol = hasSymbols ? Symbol('util.promisify.custom') : null;
// eslint-disable-next-line no-restricted-properties
var kCustomPromisifiedSymbol = hasSymbols ? Symbol['for']('nodejs.util.promisify.custom') : null;
var kCustomPromisifyArgsSymbol = hasSymbols ? Symbol('customPromisifyArgs') : null;

module.exports = function promisify(orig) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"call-bind": "^1.0.0",
"define-properties": "^1.1.3",
"for-each": "^0.3.3",
"has-symbols": "^1.0.1",
"object.getownpropertydescriptors": "^2.1.0"
},
"devDependencies": {
Expand All @@ -16,6 +15,7 @@
"aud": "^1.1.3",
"auto-changelog": "^2.2.1",
"eslint": "^7.17.0",
"has-symbols": "^1.0.1",
"nyc": "^10.3.2",
"safe-publish-latest": "^1.1.4",
"tape": "^5.1.1"
Expand Down
2 changes: 1 addition & 1 deletion polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var util = require('util');
var implementation = require('./implementation');

module.exports = function getPolyfill() {
if (typeof util.promisify === 'function') {
if (typeof util.promisify === 'function' && util.promisify.custom === implementation.custom) {
return util.promisify;
}
return implementation;
Expand Down
10 changes: 10 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var hasSymbols = require('has-symbols')();

module.exports = function runTests(promisify, t) {
t.equal(typeof promisify, 'function', 'promisify is a function');

Expand Down Expand Up @@ -41,4 +43,12 @@ module.exports = function runTests(promisify, t) {
st.deepEqual(args, [1, 2, 3], 'rejection: arguments are preserved');
});
});

t.test('custom symbol', { skip: !hasSymbols }, function (st) {
st.equal(Symbol.keyFor(promisify.custom), 'nodejs.util.promisify.custom', 'is a global symbol with the right key');
// eslint-disable-next-line no-restricted-properties
st.equal(Symbol['for']('nodejs.util.promisify.custom'), promisify.custom, 'is the global symbol');

st.end();
});
};

0 comments on commit 8f8631b

Please sign in to comment.