Skip to content

Commit

Permalink
util: increase function length when using callbackify()
Browse files Browse the repository at this point in the history
The returned function from `util.callbackify()` should increase the
`length` property by one due to the added callback.

PR-URL: #26893
Fixes: #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 b1094db commit 61d1334
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,13 @@ function callbackify(original) {
}

Object.setPrototypeOf(callbackified, Object.getPrototypeOf(original));
Object.defineProperties(callbackified,
Object.getOwnPropertyDescriptors(original));
const descriptors = Object.getOwnPropertyDescriptors(original);
// It is possible to manipulate a functions `length` property. This guards
// against the manipulation.
if (typeof descriptors.length.value === 'number') {
descriptors.length.value++;
}
Object.defineProperties(callbackified, descriptors);
return callbackified;
}

Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-util-callbackify.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const values = [
}

const cbAsyncFn = callbackify(asyncFn);
assert.strictEqual(cbAsyncFn.length, 1);
cbAsyncFn(common.mustCall((err, ret) => {
assert.strictEqual(ret, undefined);
if (err instanceof Error) {
Expand Down Expand Up @@ -146,6 +147,7 @@ const values = [
}

const cbAsyncFn = callbackify(asyncFn);
assert.strictEqual(cbAsyncFn.length, 2);
cbAsyncFn(value, common.mustCall((err, ret) => {
assert.ifError(err);
assert.strictEqual(ret, value);
Expand All @@ -155,8 +157,16 @@ const values = [
assert.strictEqual(arg, value);
return Promise.resolve(arg);
}
const obj = {};
Object.defineProperty(promiseFn, 'length', {
value: obj,
writable: false,
enumerable: false,
configurable: true
});

const cbPromiseFn = callbackify(promiseFn);
assert.strictEqual(promiseFn.length, obj);
cbPromiseFn(value, common.mustCall((err, ret) => {
assert.ifError(err);
assert.strictEqual(ret, value);
Expand Down

0 comments on commit 61d1334

Please sign in to comment.