Skip to content

Commit 8f8631b

Browse files
ExE-Bossljharb
authored andcommitted
[New] use a global symbol for util.promisify.custom
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
1 parent e68f500 commit 8f8631b

4 files changed

Lines changed: 14 additions & 3 deletions

File tree

implementation.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ var $forEach = callBound('Array.prototype.forEach');
2424

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

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

3031
module.exports = function promisify(orig) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"call-bind": "^1.0.0",
88
"define-properties": "^1.1.3",
99
"for-each": "^0.3.3",
10-
"has-symbols": "^1.0.1",
1110
"object.getownpropertydescriptors": "^2.1.0"
1211
},
1312
"devDependencies": {
@@ -16,6 +15,7 @@
1615
"aud": "^1.1.3",
1716
"auto-changelog": "^2.2.1",
1817
"eslint": "^7.17.0",
18+
"has-symbols": "^1.0.1",
1919
"nyc": "^10.3.2",
2020
"safe-publish-latest": "^1.1.4",
2121
"tape": "^5.1.1"

polyfill.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var util = require('util');
44
var implementation = require('./implementation');
55

66
module.exports = function getPolyfill() {
7-
if (typeof util.promisify === 'function') {
7+
if (typeof util.promisify === 'function' && util.promisify.custom === implementation.custom) {
88
return util.promisify;
99
}
1010
return implementation;

test/tests.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
var hasSymbols = require('has-symbols')();
4+
35
module.exports = function runTests(promisify, t) {
46
t.equal(typeof promisify, 'function', 'promisify is a function');
57

@@ -41,4 +43,12 @@ module.exports = function runTests(promisify, t) {
4143
st.deepEqual(args, [1, 2, 3], 'rejection: arguments are preserved');
4244
});
4345
});
46+
47+
t.test('custom symbol', { skip: !hasSymbols }, function (st) {
48+
st.equal(Symbol.keyFor(promisify.custom), 'nodejs.util.promisify.custom', 'is a global symbol with the right key');
49+
// eslint-disable-next-line no-restricted-properties
50+
st.equal(Symbol['for']('nodejs.util.promisify.custom'), promisify.custom, 'is the global symbol');
51+
52+
st.end();
53+
});
4454
};

0 commit comments

Comments
 (0)