From 8f8631b04c3f2cf1bd082837c8d73431e356eb2f Mon Sep 17 00:00:00 2001 From: ExE Boss <3889017+ExE-Boss@users.noreply.github.com> Date: Fri, 7 Feb 2020 17:00:00 +0100 Subject: [PATCH] =?UTF-8?q?[New]=20use=C2=A0a=C2=A0global=C2=A0symbol=20fo?= =?UTF-8?q?r=C2=A0`util.promisify.custom`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: https://github.com/nodejs/node/issues/31647 Refs: https://github.com/nodejs/node/pull/31672 --- implementation.js | 3 ++- package.json | 2 +- polyfill.js | 2 +- test/tests.js | 10 ++++++++++ 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/implementation.js b/implementation.js index d866197..e581735 100644 --- a/implementation.js +++ b/implementation.js @@ -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) { diff --git a/package.json b/package.json index 742ade5..f715320 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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" diff --git a/polyfill.js b/polyfill.js index e8484b8..cabc550 100644 --- a/polyfill.js +++ b/polyfill.js @@ -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; diff --git a/test/tests.js b/test/tests.js index 5b20f13..796ad9f 100644 --- a/test/tests.js +++ b/test/tests.js @@ -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'); @@ -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(); + }); };