Skip to content

Commit 46bf0d0

Browse files
committed
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: #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 61d1334 commit 46bf0d0

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

lib/util.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,14 @@ function callbackify(original) {
196196

197197
Object.setPrototypeOf(callbackified, Object.getPrototypeOf(original));
198198
const descriptors = Object.getOwnPropertyDescriptors(original);
199-
// It is possible to manipulate a functions `length` property. This guards
200-
// against the manipulation.
199+
// It is possible to manipulate a functions `length` or `name` property. This
200+
// guards against the manipulation.
201201
if (typeof descriptors.length.value === 'number') {
202202
descriptors.length.value++;
203203
}
204+
if (typeof descriptors.name.value === 'string') {
205+
descriptors.name.value += 'Callbackified';
206+
}
204207
Object.defineProperties(callbackified, descriptors);
205208
return callbackified;
206209
}

test/parallel/test-util-callbackify.js

+9
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ const values = [
7575

7676
const cbAsyncFn = callbackify(asyncFn);
7777
assert.strictEqual(cbAsyncFn.length, 1);
78+
assert.strictEqual(cbAsyncFn.name, 'asyncFnCallbackified');
7879
cbAsyncFn(common.mustCall((err, ret) => {
7980
assert.strictEqual(ret, undefined);
8081
if (err instanceof Error) {
@@ -94,8 +95,16 @@ const values = [
9495
function promiseFn() {
9596
return Promise.reject(value);
9697
}
98+
const obj = {};
99+
Object.defineProperty(promiseFn, 'name', {
100+
value: obj,
101+
writable: false,
102+
enumerable: false,
103+
configurable: true
104+
});
97105

98106
const cbPromiseFn = callbackify(promiseFn);
107+
assert.strictEqual(promiseFn.name, obj);
99108
cbPromiseFn(common.mustCall((err, ret) => {
100109
assert.strictEqual(ret, undefined);
101110
if (err instanceof Error) {

0 commit comments

Comments
 (0)