Skip to content

Commit 61d1334

Browse files
committed
util: increase function length when using callbackify()
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 <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent b1094db commit 61d1334

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

lib/util.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,13 @@ function callbackify(original) {
195195
}
196196

197197
Object.setPrototypeOf(callbackified, Object.getPrototypeOf(original));
198-
Object.defineProperties(callbackified,
199-
Object.getOwnPropertyDescriptors(original));
198+
const descriptors = Object.getOwnPropertyDescriptors(original);
199+
// It is possible to manipulate a functions `length` property. This guards
200+
// against the manipulation.
201+
if (typeof descriptors.length.value === 'number') {
202+
descriptors.length.value++;
203+
}
204+
Object.defineProperties(callbackified, descriptors);
200205
return callbackified;
201206
}
202207

test/parallel/test-util-callbackify.js

+10
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ const values = [
7474
}
7575

7676
const cbAsyncFn = callbackify(asyncFn);
77+
assert.strictEqual(cbAsyncFn.length, 1);
7778
cbAsyncFn(common.mustCall((err, ret) => {
7879
assert.strictEqual(ret, undefined);
7980
if (err instanceof Error) {
@@ -146,6 +147,7 @@ const values = [
146147
}
147148

148149
const cbAsyncFn = callbackify(asyncFn);
150+
assert.strictEqual(cbAsyncFn.length, 2);
149151
cbAsyncFn(value, common.mustCall((err, ret) => {
150152
assert.ifError(err);
151153
assert.strictEqual(ret, value);
@@ -155,8 +157,16 @@ const values = [
155157
assert.strictEqual(arg, value);
156158
return Promise.resolve(arg);
157159
}
160+
const obj = {};
161+
Object.defineProperty(promiseFn, 'length', {
162+
value: obj,
163+
writable: false,
164+
enumerable: false,
165+
configurable: true
166+
});
158167

159168
const cbPromiseFn = callbackify(promiseFn);
169+
assert.strictEqual(promiseFn.length, obj);
160170
cbPromiseFn(value, common.mustCall((err, ret) => {
161171
assert.ifError(err);
162172
assert.strictEqual(ret, value);

0 commit comments

Comments
 (0)