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:
parent
61d1334e5b
commit
46bf0d0f4f
@ -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;
|
||||
}
|
||||
|
@ -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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user