Skip to content

Commit f2409b2

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

File tree

3 files changed

+61
-31
lines changed

3 files changed

+61
-31
lines changed

test/browser/callbackify.js

+10
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,13 @@ 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+
[async () => { }, async (a) => { }, async (a, b) => { }].forEach(function (fct) {
174+
175+
const callbackified = callbackify(fct);
176+
t.strictEqual(callbackified.length, fct.length + 1, "callbackified function should have one more argument");
177+
});
178+
t.end();
179+
});

test/node/callbackify.js

+19
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,22 @@ 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+
console.log("Testing callbackify resulting function should have one more argument")
200+
const original_callbackify = require('util').callbackify;
201+
// Test that resulting function should have one more argument
202+
[async () => { }, async (a) => { }, async (a, b) => { }].forEach(function (fct) {
203+
204+
const node_callbackified = original_callbackify(fct);
205+
const browser_callbackified = callbackify(fct);
206+
207+
if (node_callbackified.length !== fct.length + 1) {
208+
throw new Error("callbackified function should have one more argument");
209+
}
210+
if (browser_callbackified.length !== node_callbackified.length) {
211+
throw new Error("callbackified function should have one more argument, like in node");
212+
}
213+
});
214+
})();

util.js

+32-31
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ function stylizeWithColor(str, styleType) {
202202

203203
if (style) {
204204
return '\u001b[' + inspect.colors[style][0] + 'm' + str +
205-
'\u001b[' + inspect.colors[style][1] + 'm';
205+
'\u001b[' + inspect.colors[style][1] + 'm';
206206
} else {
207207
return str;
208208
}
@@ -229,12 +229,12 @@ function formatValue(ctx, value, recurseTimes) {
229229
// Provide a hook for user-specified inspect functions.
230230
// Check that value is an object with an inspect function on it
231231
if (ctx.customInspect &&
232-
value &&
233-
isFunction(value.inspect) &&
234-
// Filter out the util module, it's inspect function is special
235-
value.inspect !== exports.inspect &&
236-
// Also filter out any prototype objects using the circular check.
237-
!(value.constructor && value.constructor.prototype === value)) {
232+
value &&
233+
isFunction(value.inspect) &&
234+
// Filter out the util module, it's inspect function is special
235+
value.inspect !== exports.inspect &&
236+
// Also filter out any prototype objects using the circular check.
237+
!(value.constructor && value.constructor.prototype === value)) {
238238
var ret = value.inspect(recurseTimes, ctx);
239239
if (!isString(ret)) {
240240
ret = formatValue(ctx, ret, recurseTimes);
@@ -259,7 +259,7 @@ function formatValue(ctx, value, recurseTimes) {
259259
// IE doesn't make error fields non-enumerable
260260
// http://msdn.microsoft.com/en-us/library/ie/dww52sbt(v=vs.94).aspx
261261
if (isError(value)
262-
&& (keys.indexOf('message') >= 0 || keys.indexOf('description') >= 0)) {
262+
&& (keys.indexOf('message') >= 0 || keys.indexOf('description') >= 0)) {
263263
return formatError(value);
264264
}
265265

@@ -343,8 +343,8 @@ function formatPrimitive(ctx, value) {
343343
return ctx.stylize('undefined', 'undefined');
344344
if (isString(value)) {
345345
var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '')
346-
.replace(/'/g, "\\'")
347-
.replace(/\\"/g, '"') + '\'';
346+
.replace(/'/g, "\\'")
347+
.replace(/\\"/g, '"') + '\'';
348348
return ctx.stylize(simple, 'string');
349349
}
350350
if (isNumber(value))
@@ -367,15 +367,15 @@ function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
367367
for (var i = 0, l = value.length; i < l; ++i) {
368368
if (hasOwnProperty(value, String(i))) {
369369
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
370-
String(i), true));
370+
String(i), true));
371371
} else {
372372
output.push('');
373373
}
374374
}
375375
keys.forEach(function(key) {
376376
if (!key.match(/^\d+$/)) {
377377
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
378-
key, true));
378+
key, true));
379379
}
380380
});
381381
return output;
@@ -431,8 +431,8 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
431431
name = ctx.stylize(name, 'name');
432432
} else {
433433
name = name.replace(/'/g, "\\'")
434-
.replace(/\\"/g, '"')
435-
.replace(/(^"|"$)/g, "'");
434+
.replace(/\\"/g, '"')
435+
.replace(/(^"|"$)/g, "'");
436436
name = ctx.stylize(name, 'string');
437437
}
438438
}
@@ -451,11 +451,11 @@ function reduceToSingleString(output, base, braces) {
451451

452452
if (length > 60) {
453453
return braces[0] +
454-
(base === '' ? '' : base + '\n ') +
455-
' ' +
456-
output.join(',\n ') +
457-
' ' +
458-
braces[1];
454+
(base === '' ? '' : base + '\n ') +
455+
' ' +
456+
output.join(',\n ') +
457+
' ' +
458+
braces[1];
459459
}
460460

461461
return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];
@@ -525,7 +525,7 @@ exports.types.isDate = isDate;
525525

526526
function isError(e) {
527527
return isObject(e) &&
528-
(objectToString(e) === '[object Error]' || e instanceof Error);
528+
(objectToString(e) === '[object Error]' || e instanceof Error);
529529
}
530530
exports.isError = isError;
531531
exports.types.isNativeError = isError;
@@ -537,11 +537,11 @@ exports.isFunction = isFunction;
537537

538538
function isPrimitive(arg) {
539539
return arg === null ||
540-
typeof arg === 'boolean' ||
541-
typeof arg === 'number' ||
542-
typeof arg === 'string' ||
543-
typeof arg === 'symbol' || // ES6 symbol
544-
typeof arg === 'undefined';
540+
typeof arg === 'boolean' ||
541+
typeof arg === 'number' ||
542+
typeof arg === 'string' ||
543+
typeof arg === 'symbol' || // ES6 symbol
544+
typeof arg === 'undefined';
545545
}
546546
exports.isPrimitive = isPrimitive;
547547

@@ -558,14 +558,14 @@ function pad(n) {
558558

559559

560560
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
561-
'Oct', 'Nov', 'Dec'];
561+
'Oct', 'Nov', 'Dec'];
562562

563563
// 26 Feb 16:19:34
564564
function timestamp() {
565565
var d = new Date();
566566
var time = [pad(d.getHours()),
567-
pad(d.getMinutes()),
568-
pad(d.getSeconds())].join(':');
567+
pad(d.getMinutes()),
568+
pad(d.getSeconds())].join(':');
569569
return [d.getDate(), months[d.getMonth()], time].join(' ');
570570
}
571571

@@ -704,12 +704,13 @@ function callbackify(original) {
704704
// implications (stack, `uncaughtException`, `async_hooks`)
705705
original.apply(this, args)
706706
.then(function(ret) { process.nextTick(cb.bind(null, null, ret)) },
707-
function(rej) { process.nextTick(callbackifyOnRejected.bind(null, rej, cb)) });
707+
function(rej) { process.nextTick(callbackifyOnRejected.bind(null, rej, cb)) });
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)