Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: improve callbackify #26893

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,16 @@ function callbackify(original) {
(rej) => process.nextTick(callbackifyOnRejected, rej, cb));
}

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` 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
24 changes: 24 additions & 0 deletions test/parallel/test-util-callbackify.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ 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 @@ -93,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 Expand Up @@ -146,6 +156,12 @@ const values = [
}

const cbAsyncFn = callbackify(asyncFn);
assert.strictEqual(cbAsyncFn.length, 2);
assert.notStrictEqual(
Object.getPrototypeOf(cbAsyncFn),
Object.getPrototypeOf(asyncFn)
);
assert.strictEqual(Object.getPrototypeOf(cbAsyncFn), Function.prototype);
cbAsyncFn(value, common.mustCall((err, ret) => {
assert.ifError(err);
assert.strictEqual(ret, value);
Expand All @@ -155,8 +171,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