Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New] use a global symbol for util.promisify.custom #19

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Copy link
Contributor Author

@ExE-Boss ExE-Boss Jan 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ljharb It just occurred to me that this change is wrong, because has‑symbols is used in implementation.js:

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

This was fixed in #25

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed; it’s been fixed in #25.

"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();
});
};