Skip to content

Commit

Permalink
fix: callbackify resulting function should have one more argument
Browse files Browse the repository at this point in the history
  • Loading branch information
erossignon committed May 18, 2022
1 parent ad9dbe0 commit 87caee5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
15 changes: 15 additions & 0 deletions test/browser/callbackify.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,18 @@ test('util.callbackify non-function inputs throw', function (t) {
});
t.end();
});

test('util.callbackify resulting function should have one more argument', function (t) {
// Test that resulting function should have one more argument
[
function() { },
function(a) { },
function(a, b) { }
].forEach(function (fct) {

var callbackified = callbackify(fct);
t.strictEqual(callbackified.length, fct.length + 1, "callbackified function should have one more argument");
});
t.end();
});

26 changes: 26 additions & 0 deletions test/node/callbackify.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,29 @@ if (false) {
if (require('is-async-supported')()) {
require('./callbackify-async');
}

(function callbackify_resulting_function_should_have_one_more_argument() {

var nodeJSVersion = parseInt(process.version.substring(1,3),10);

console.log("Testing callbackify resulting function should have one more argument")
var original_callbackify = require('util').callbackify;
// Test that resulting function should have one more argument
[
function(){ },
function(a){ },
function(a, b) { }
].forEach(function (fct) {

var node_callbackified = original_callbackify(fct);
var browser_callbackified = callbackify(fct);

if (nodeJSVersion >= 12 && node_callbackified.length !== fct.length + 1) {
// this behavior is only true with node 12 and above, where the bug was fixed
throw new Error("callbackified function should have one more argument");
}
if (browser_callbackified.length !== node_callbackified.length) {
throw new Error("callbackified function should have one more argument, like in node");
}
});
})();
5 changes: 3 additions & 2 deletions util.js
Original file line number Diff line number Diff line change
Expand Up @@ -708,8 +708,9 @@ function callbackify(original) {
}

Object.setPrototypeOf(callbackified, Object.getPrototypeOf(original));
Object.defineProperties(callbackified,
getOwnPropertyDescriptors(original));
const desc = getOwnPropertyDescriptors(original);
desc.length.value += 1;
Object.defineProperties(callbackified, desc);
return callbackified;
}
exports.callbackify = callbackify;

0 comments on commit 87caee5

Please sign in to comment.