util: rename callbackified function

This makes sure the function returned by `util.callbackify()` has a
new name that is not identical to the inputs function name.

PR-URL: https://github.com/nodejs/node/pull/26893
Fixes: https://github.com/nodejs/node/issues/26890
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
This commit is contained in:
Ruben Bridgewater 2019-03-24 22:11:47 +01:00
parent 61d1334e5b
commit 46bf0d0f4f
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
2 changed files with 14 additions and 2 deletions

View File

@ -196,11 +196,14 @@ function callbackify(original) {
Object.setPrototypeOf(callbackified, Object.getPrototypeOf(original));
const descriptors = Object.getOwnPropertyDescriptors(original);
// It is possible to manipulate a functions `length` property. This guards
// against the manipulation.
// It is possible to manipulate a functions `length` or `name` property. This
// guards against the manipulation.
if (typeof descriptors.length.value === 'number') {
descriptors.length.value++;
}
if (typeof descriptors.name.value === 'string') {
descriptors.name.value += 'Callbackified';
}
Object.defineProperties(callbackified, descriptors);
return callbackified;
}

View File

@ -75,6 +75,7 @@ const values = [
const cbAsyncFn = callbackify(asyncFn);
assert.strictEqual(cbAsyncFn.length, 1);
assert.strictEqual(cbAsyncFn.name, 'asyncFnCallbackified');
cbAsyncFn(common.mustCall((err, ret) => {
assert.strictEqual(ret, undefined);
if (err instanceof Error) {
@ -94,8 +95,16 @@ const values = [
function promiseFn() {
return Promise.reject(value);
}
const obj = {};
Object.defineProperty(promiseFn, 'name', {
value: obj,
writable: false,
enumerable: false,
configurable: true
});
const cbPromiseFn = callbackify(promiseFn);
assert.strictEqual(promiseFn.name, obj);
cbPromiseFn(common.mustCall((err, ret) => {
assert.strictEqual(ret, undefined);
if (err instanceof Error) {