Skip to content

Commit

Permalink
util: rename callbackified function
Browse files Browse the repository at this point in the history
This makes sure the function returned by `util.callbackify()` has a
new name that is not identical to the inputs function name.

PR-URL: nodejs#26893
Fixes: nodejs#26890
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
BridgeAR committed Mar 27, 2019
1 parent 61d1334 commit 46bf0d0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
9 changes: 9 additions & 0 deletions test/parallel/test-util-callbackify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit 46bf0d0

Please sign in to comment.