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 f2409b2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 31 deletions.
10 changes: 10 additions & 0 deletions test/browser/callbackify.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,13 @@ 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
[async () => { }, async (a) => { }, async (a, b) => { }].forEach(function (fct) {

const callbackified = callbackify(fct);
t.strictEqual(callbackified.length, fct.length + 1, "callbackified function should have one more argument");
});
t.end();
});
19 changes: 19 additions & 0 deletions test/node/callbackify.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,22 @@ if (false) {
if (require('is-async-supported')()) {
require('./callbackify-async');
}

(function callbackify_resulting_function_should_have_one_more_argument() {

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

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

if (node_callbackified.length !== fct.length + 1) {
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");
}
});
})();
63 changes: 32 additions & 31 deletions util.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ function stylizeWithColor(str, styleType) {

if (style) {
return '\u001b[' + inspect.colors[style][0] + 'm' + str +
'\u001b[' + inspect.colors[style][1] + 'm';
'\u001b[' + inspect.colors[style][1] + 'm';
} else {
return str;
}
Expand All @@ -229,12 +229,12 @@ function formatValue(ctx, value, recurseTimes) {
// Provide a hook for user-specified inspect functions.
// Check that value is an object with an inspect function on it
if (ctx.customInspect &&
value &&
isFunction(value.inspect) &&
// Filter out the util module, it's inspect function is special
value.inspect !== exports.inspect &&
// Also filter out any prototype objects using the circular check.
!(value.constructor && value.constructor.prototype === value)) {
value &&
isFunction(value.inspect) &&
// Filter out the util module, it's inspect function is special
value.inspect !== exports.inspect &&
// Also filter out any prototype objects using the circular check.
!(value.constructor && value.constructor.prototype === value)) {
var ret = value.inspect(recurseTimes, ctx);
if (!isString(ret)) {
ret = formatValue(ctx, ret, recurseTimes);
Expand All @@ -259,7 +259,7 @@ function formatValue(ctx, value, recurseTimes) {
// IE doesn't make error fields non-enumerable
// http://msdn.microsoft.com/en-us/library/ie/dww52sbt(v=vs.94).aspx
if (isError(value)
&& (keys.indexOf('message') >= 0 || keys.indexOf('description') >= 0)) {
&& (keys.indexOf('message') >= 0 || keys.indexOf('description') >= 0)) {
return formatError(value);
}

Expand Down Expand Up @@ -343,8 +343,8 @@ function formatPrimitive(ctx, value) {
return ctx.stylize('undefined', 'undefined');
if (isString(value)) {
var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '')
.replace(/'/g, "\\'")
.replace(/\\"/g, '"') + '\'';
.replace(/'/g, "\\'")
.replace(/\\"/g, '"') + '\'';
return ctx.stylize(simple, 'string');
}
if (isNumber(value))
Expand All @@ -367,15 +367,15 @@ function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
for (var i = 0, l = value.length; i < l; ++i) {
if (hasOwnProperty(value, String(i))) {
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
String(i), true));
String(i), true));
} else {
output.push('');
}
}
keys.forEach(function(key) {
if (!key.match(/^\d+$/)) {
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
key, true));
key, true));
}
});
return output;
Expand Down Expand Up @@ -431,8 +431,8 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
name = ctx.stylize(name, 'name');
} else {
name = name.replace(/'/g, "\\'")
.replace(/\\"/g, '"')
.replace(/(^"|"$)/g, "'");
.replace(/\\"/g, '"')
.replace(/(^"|"$)/g, "'");
name = ctx.stylize(name, 'string');
}
}
Expand All @@ -451,11 +451,11 @@ function reduceToSingleString(output, base, braces) {

if (length > 60) {
return braces[0] +
(base === '' ? '' : base + '\n ') +
' ' +
output.join(',\n ') +
' ' +
braces[1];
(base === '' ? '' : base + '\n ') +
' ' +
output.join(',\n ') +
' ' +
braces[1];
}

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

function isError(e) {
return isObject(e) &&
(objectToString(e) === '[object Error]' || e instanceof Error);
(objectToString(e) === '[object Error]' || e instanceof Error);
}
exports.isError = isError;
exports.types.isNativeError = isError;
Expand All @@ -537,11 +537,11 @@ exports.isFunction = isFunction;

function isPrimitive(arg) {
return arg === null ||
typeof arg === 'boolean' ||
typeof arg === 'number' ||
typeof arg === 'string' ||
typeof arg === 'symbol' || // ES6 symbol
typeof arg === 'undefined';
typeof arg === 'boolean' ||
typeof arg === 'number' ||
typeof arg === 'string' ||
typeof arg === 'symbol' || // ES6 symbol
typeof arg === 'undefined';
}
exports.isPrimitive = isPrimitive;

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


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

// 26 Feb 16:19:34
function timestamp() {
var d = new Date();
var time = [pad(d.getHours()),
pad(d.getMinutes()),
pad(d.getSeconds())].join(':');
pad(d.getMinutes()),
pad(d.getSeconds())].join(':');
return [d.getDate(), months[d.getMonth()], time].join(' ');
}

Expand Down Expand Up @@ -704,12 +704,13 @@ function callbackify(original) {
// implications (stack, `uncaughtException`, `async_hooks`)
original.apply(this, args)
.then(function(ret) { process.nextTick(cb.bind(null, null, ret)) },
function(rej) { process.nextTick(callbackifyOnRejected.bind(null, rej, cb)) });
function(rej) { process.nextTick(callbackifyOnRejected.bind(null, rej, cb)) });
}

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 f2409b2

Please sign in to comment.