Skip to content

Commit 87caee5

Browse files
committed
fix: callbackify resulting function should have one more argument
1 parent ad9dbe0 commit 87caee5

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

test/browser/callbackify.js

+15
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,18 @@ test('util.callbackify non-function inputs throw', function (t) {
167167
});
168168
t.end();
169169
});
170+
171+
test('util.callbackify resulting function should have one more argument', function (t) {
172+
// Test that resulting function should have one more argument
173+
[
174+
function() { },
175+
function(a) { },
176+
function(a, b) { }
177+
].forEach(function (fct) {
178+
179+
var callbackified = callbackify(fct);
180+
t.strictEqual(callbackified.length, fct.length + 1, "callbackified function should have one more argument");
181+
});
182+
t.end();
183+
});
184+

test/node/callbackify.js

+26
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,29 @@ if (false) {
193193
if (require('is-async-supported')()) {
194194
require('./callbackify-async');
195195
}
196+
197+
(function callbackify_resulting_function_should_have_one_more_argument() {
198+
199+
var nodeJSVersion = parseInt(process.version.substring(1,3),10);
200+
201+
console.log("Testing callbackify resulting function should have one more argument")
202+
var original_callbackify = require('util').callbackify;
203+
// Test that resulting function should have one more argument
204+
[
205+
function(){ },
206+
function(a){ },
207+
function(a, b) { }
208+
].forEach(function (fct) {
209+
210+
var node_callbackified = original_callbackify(fct);
211+
var browser_callbackified = callbackify(fct);
212+
213+
if (nodeJSVersion >= 12 && node_callbackified.length !== fct.length + 1) {
214+
// this behavior is only true with node 12 and above, where the bug was fixed
215+
throw new Error("callbackified function should have one more argument");
216+
}
217+
if (browser_callbackified.length !== node_callbackified.length) {
218+
throw new Error("callbackified function should have one more argument, like in node");
219+
}
220+
});
221+
})();

util.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -708,8 +708,9 @@ function callbackify(original) {
708708
}
709709

710710
Object.setPrototypeOf(callbackified, Object.getPrototypeOf(original));
711-
Object.defineProperties(callbackified,
712-
getOwnPropertyDescriptors(original));
711+
const desc = getOwnPropertyDescriptors(original);
712+
desc.length.value += 1;
713+
Object.defineProperties(callbackified, desc);
713714
return callbackified;
714715
}
715716
exports.callbackify = callbackify;

0 commit comments

Comments
 (0)