3 const uv = process.binding('uv');
4 const Debug = require('vm').runInDebugContext('Debug');
6 const formatRegExp = /%[sdj%]/g;
7 exports.format = function(f) {
8 if (typeof f !== 'string') {
10 for (var i = 0; i < arguments.length; i++) {
11 objects.push(inspect(arguments[i]));
13 return objects.join(' ');
18 var len = args.length;
19 var str = String(f).replace(formatRegExp, function(x) {
20 if (x === '%%') return '%';
21 if (i >= len) return x;
23 case '%s': return String(args[i++]);
24 case '%d': return Number(args[i++]);
27 return JSON.stringify(args[i++]);
35 for (var x = args[i]; i < len; x = args[++i]) {
36 if (x === null || (typeof x !== 'object' && typeof x !== 'symbol')) {
39 str += ' ' + inspect(x);
46 // Mark that a method should not be used.
47 // Returns a modified function which warns once by default.
48 // If --no-deprecation is set, then it is a no-op.
49 exports.deprecate = function(fn, msg) {
50 // Allow for deprecating things in the process of starting up.
51 if (global.process === undefined) {
53 return exports.deprecate(fn, msg).apply(this, arguments);
57 if (process.noDeprecation === true) {
62 function deprecated() {
64 if (process.throwDeprecation) {
66 } else if (process.traceDeprecation) {
73 return fn.apply(this, arguments);
82 exports.debuglog = function(set) {
83 if (debugEnviron === undefined)
84 debugEnviron = process.env.NODE_DEBUG || '';
85 set = set.toUpperCase();
87 if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) {
88 var pid = process.pid;
89 debugs[set] = function() {
90 var msg = exports.format.apply(exports, arguments);
91 console.error('%s %d: %s', set, pid, msg);
94 debugs[set] = function() {};
102 * Echos the value of a value. Trys to print the value out
103 * in the best way possible given the different types.
105 * @param {Object} obj The object to print out.
106 * @param {Object} opts Optional options object that alters the output.
108 /* legacy: obj, showHidden, depth, colors*/
109 function inspect(obj, opts) {
113 stylize: stylizeNoColor
116 if (arguments.length >= 3) ctx.depth = arguments[2];
117 if (arguments.length >= 4) ctx.colors = arguments[3];
118 if (typeof opts === 'boolean') {
120 ctx.showHidden = opts;
122 // got an "options" object
123 exports._extend(ctx, opts);
125 // set default options
126 if (ctx.showHidden === undefined) ctx.showHidden = false;
127 if (ctx.depth === undefined) ctx.depth = 2;
128 if (ctx.colors === undefined) ctx.colors = false;
129 if (ctx.customInspect === undefined) ctx.customInspect = true;
130 if (ctx.colors) ctx.stylize = stylizeWithColor;
131 return formatValue(ctx, obj, ctx.depth);
133 exports.inspect = inspect;
136 // http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
140 'underline' : [4, 24],
148 'magenta' : [35, 39],
153 // Don't use 'blue' not visible on cmd.exe
163 // "name": intentionally not styling
168 function stylizeWithColor(str, styleType) {
169 var style = inspect.styles[styleType];
172 return '\u001b[' + inspect.colors[style][0] + 'm' + str +
173 '\u001b[' + inspect.colors[style][1] + 'm';
180 function stylizeNoColor(str, styleType) {
185 function arrayToHash(array) {
188 array.forEach(function(val, idx) {
196 function inspectPromise(p) {
197 var mirror = Debug.MakeMirror(p, true);
198 if (!mirror.isPromise())
200 return {status: mirror.status(), value: mirror.promiseValue().value_};
204 function formatValue(ctx, value, recurseTimes) {
205 // Provide a hook for user-specified inspect functions.
206 // Check that value is an object with an inspect function on it
207 if (ctx.customInspect &&
209 typeof value.inspect === 'function' &&
210 // Filter out the util module, it's inspect function is special
211 value.inspect !== exports.inspect &&
212 // Also filter out any prototype objects using the circular check.
213 !(value.constructor && value.constructor.prototype === value)) {
214 var ret = value.inspect(recurseTimes, ctx);
215 if (typeof ret !== 'string') {
216 ret = formatValue(ctx, ret, recurseTimes);
221 // Primitive types cannot have properties
222 var primitive = formatPrimitive(ctx, value);
227 // Look up the keys of the object.
228 var keys = Object.keys(value);
229 var visibleKeys = arrayToHash(keys);
231 if (ctx.showHidden) {
232 keys = Object.getOwnPropertyNames(value);
233 keys = keys.concat(Object.getOwnPropertySymbols(value));
236 // This could be a boxed primitive (new String(), etc.), check valueOf()
237 // NOTE: Avoid calling `valueOf` on `Date` instance because it will return
238 // a number which, when object has some additional user-stored `keys`,
239 // will be printed out.
243 // the .valueOf() call can fail for a multitude of reasons
245 raw = value.valueOf();
250 if (typeof raw === 'string') {
251 // for boxed Strings, we have to remove the 0-n indexed entries,
252 // since they just noisey up the output and are redundant
253 keys = keys.filter(function(key) {
254 return !(key >= 0 && key < raw.length);
258 // Some type of object without properties can be shortcutted.
259 if (keys.length === 0) {
260 if (typeof value === 'function') {
261 var name = value.name ? ': ' + value.name : '';
262 return ctx.stylize('[Function' + name + ']', 'special');
264 if (isRegExp(value)) {
265 return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
268 return ctx.stylize(Date.prototype.toString.call(value), 'date');
270 if (isError(value)) {
271 return formatError(value);
273 // now check the `raw` value to handle boxed primitives
274 if (typeof raw === 'string') {
275 formatted = formatPrimitiveNoColor(ctx, raw);
276 return ctx.stylize('[String: ' + formatted + ']', 'string');
278 if (typeof raw === 'number') {
279 formatted = formatPrimitiveNoColor(ctx, raw);
280 return ctx.stylize('[Number: ' + formatted + ']', 'number');
282 if (typeof raw === 'boolean') {
283 formatted = formatPrimitiveNoColor(ctx, raw);
284 return ctx.stylize('[Boolean: ' + formatted + ']', 'boolean');
288 var base = '', empty = false, braces, formatter;
290 if (Array.isArray(value)) {
292 empty = value.length === 0;
293 formatter = formatArray;
294 } else if (value instanceof Set) {
295 braces = ['Set {', '}'];
296 // With `showHidden`, `length` will display as a hidden property for
297 // arrays. For consistency's sake, do the same for `size`, even though this
298 // property isn't selected by Object.getOwnPropertyNames().
300 keys.unshift('size');
301 empty = value.size === 0;
302 formatter = formatSet;
303 } else if (value instanceof Map) {
304 braces = ['Map {', '}'];
307 keys.unshift('size');
308 empty = value.size === 0;
309 formatter = formatMap;
311 // Only create a mirror if the object superficially looks like a Promise.
312 var promiseInternals = value instanceof Promise && inspectPromise(value);
313 if (promiseInternals) {
314 braces = ['Promise {', '}'];
315 formatter = formatPromise;
318 empty = true; // No other data than keys.
319 formatter = formatObject;
323 empty = empty === true && keys.length === 0;
325 // Make functions say that they are functions
326 if (typeof value === 'function') {
327 var n = value.name ? ': ' + value.name : '';
328 base = ' [Function' + n + ']';
331 // Make RegExps say that they are RegExps
332 if (isRegExp(value)) {
333 base = ' ' + RegExp.prototype.toString.call(value);
336 // Make dates with properties first say the date
338 base = ' ' + Date.prototype.toUTCString.call(value);
341 // Make error with message first say the error
342 if (isError(value)) {
343 base = ' ' + formatError(value);
346 // Make boxed primitive Strings look like such
347 if (typeof raw === 'string') {
348 formatted = formatPrimitiveNoColor(ctx, raw);
349 base = ' ' + '[String: ' + formatted + ']';
352 // Make boxed primitive Numbers look like such
353 if (typeof raw === 'number') {
354 formatted = formatPrimitiveNoColor(ctx, raw);
355 base = ' ' + '[Number: ' + formatted + ']';
358 // Make boxed primitive Booleans look like such
359 if (typeof raw === 'boolean') {
360 formatted = formatPrimitiveNoColor(ctx, raw);
361 base = ' ' + '[Boolean: ' + formatted + ']';
364 if (empty === true) {
365 return braces[0] + base + braces[1];
368 if (recurseTimes < 0) {
369 if (isRegExp(value)) {
370 return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
372 return ctx.stylize('[Object]', 'special');
376 ctx.seen.push(value);
378 var output = formatter(ctx, value, recurseTimes, visibleKeys, keys);
382 return reduceToSingleString(output, base, braces);
386 function formatPrimitive(ctx, value) {
387 if (value === undefined)
388 return ctx.stylize('undefined', 'undefined');
390 // For some reason typeof null is "object", so special case here.
392 return ctx.stylize('null', 'null');
394 var type = typeof value;
396 if (type === 'string') {
397 var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '')
398 .replace(/'/g, "\\'")
399 .replace(/\\"/g, '"') + '\'';
400 return ctx.stylize(simple, 'string');
402 if (type === 'number') {
403 // Format -0 as '-0'. Strict equality won't distinguish 0 from -0,
404 // so instead we use the fact that 1 / -0 < 0 whereas 1 / 0 > 0 .
405 if (value === 0 && 1 / value < 0)
406 return ctx.stylize('-0', 'number');
407 return ctx.stylize('' + value, 'number');
409 if (type === 'boolean')
410 return ctx.stylize('' + value, 'boolean');
411 // es6 symbol primitive
412 if (type === 'symbol')
413 return ctx.stylize(value.toString(), 'symbol');
417 function formatPrimitiveNoColor(ctx, value) {
418 var stylize = ctx.stylize;
419 ctx.stylize = stylizeNoColor;
420 var str = formatPrimitive(ctx, value);
421 ctx.stylize = stylize;
426 function formatError(value) {
427 return '[' + Error.prototype.toString.call(value) + ']';
431 function formatObject(ctx, value, recurseTimes, visibleKeys, keys) {
432 return keys.map(function(key) {
433 return formatProperty(ctx, value, recurseTimes, visibleKeys, key, false);
438 function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
440 for (var i = 0, l = value.length; i < l; ++i) {
441 if (hasOwnProperty(value, String(i))) {
442 output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
448 keys.forEach(function(key) {
449 if (typeof key === 'symbol' || !key.match(/^\d+$/)) {
450 output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
458 function formatSet(ctx, value, recurseTimes, visibleKeys, keys) {
460 value.forEach(function(v) {
461 var nextRecurseTimes = recurseTimes === null ? null : recurseTimes - 1;
462 var str = formatValue(ctx, v, nextRecurseTimes);
465 keys.forEach(function(key) {
466 output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
473 function formatMap(ctx, value, recurseTimes, visibleKeys, keys) {
475 value.forEach(function(v, k) {
476 var nextRecurseTimes = recurseTimes === null ? null : recurseTimes - 1;
477 var str = formatValue(ctx, k, nextRecurseTimes);
479 str += formatValue(ctx, v, nextRecurseTimes);
482 keys.forEach(function(key) {
483 output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
489 function formatPromise(ctx, value, recurseTimes, visibleKeys, keys) {
491 var internals = inspectPromise(value);
492 if (internals.status === 'pending') {
493 output.push('<pending>');
495 var nextRecurseTimes = recurseTimes === null ? null : recurseTimes - 1;
496 var str = formatValue(ctx, internals.value, nextRecurseTimes);
497 if (internals.status === 'rejected') {
498 output.push('<rejected> ' + str);
503 keys.forEach(function(key) {
504 output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
511 function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
513 desc = Object.getOwnPropertyDescriptor(value, key) || { value: value[key] };
516 str = ctx.stylize('[Getter/Setter]', 'special');
518 str = ctx.stylize('[Getter]', 'special');
522 str = ctx.stylize('[Setter]', 'special');
525 if (!hasOwnProperty(visibleKeys, key)) {
526 if (typeof key === 'symbol') {
527 name = '[' + ctx.stylize(key.toString(), 'symbol') + ']';
529 name = '[' + key + ']';
533 if (ctx.seen.indexOf(desc.value) < 0) {
534 if (recurseTimes === null) {
535 str = formatValue(ctx, desc.value, null);
537 str = formatValue(ctx, desc.value, recurseTimes - 1);
539 if (str.indexOf('\n') > -1) {
541 str = str.split('\n').map(function(line) {
543 }).join('\n').substr(2);
545 str = '\n' + str.split('\n').map(function(line) {
551 str = ctx.stylize('[Circular]', 'special');
554 if (name === undefined) {
555 if (array && key.match(/^\d+$/)) {
558 name = JSON.stringify('' + key);
559 if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
560 name = name.substr(1, name.length - 2);
561 name = ctx.stylize(name, 'name');
563 name = name.replace(/'/g, "\\'")
564 .replace(/\\"/g, '"')
565 .replace(/(^"|"$)/g, "'")
566 .replace(/\\\\/g, '\\');
567 name = ctx.stylize(name, 'string');
571 return name + ': ' + str;
575 function reduceToSingleString(output, base, braces) {
576 var length = output.reduce(function(prev, cur) {
577 return prev + cur.replace(/\u001b\[\d\d?m/g, '').length + 1;
582 // If the opening "brace" is too large, like in the case of "Set {",
583 // we need to force the first item to be on the next line or the
584 // items will not line up correctly.
585 (base === '' && braces[0].length === 1 ? '' : base + '\n ') +
587 output.join(',\n ') +
592 return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];
596 // NOTE: These type checking functions intentionally don't use `instanceof`
597 // because it is fragile and can be easily faked with `Object.create()`.
598 exports.isArray = Array.isArray;
600 function isBoolean(arg) {
601 return typeof arg === 'boolean';
603 exports.isBoolean = isBoolean;
605 function isNull(arg) {
608 exports.isNull = isNull;
610 function isNullOrUndefined(arg) {
611 return arg === null || arg === undefined;
613 exports.isNullOrUndefined = isNullOrUndefined;
615 function isNumber(arg) {
616 return typeof arg === 'number';
618 exports.isNumber = isNumber;
620 function isString(arg) {
621 return typeof arg === 'string';
623 exports.isString = isString;
625 function isSymbol(arg) {
626 return typeof arg === 'symbol';
628 exports.isSymbol = isSymbol;
630 function isUndefined(arg) {
631 return arg === undefined;
633 exports.isUndefined = isUndefined;
635 function isRegExp(re) {
636 return re !== null && typeof re === 'object' &&
637 objectToString(re) === '[object RegExp]';
639 exports.isRegExp = isRegExp;
641 function isObject(arg) {
642 return arg !== null && typeof arg === 'object';
644 exports.isObject = isObject;
647 return d !== null && typeof d === 'object' &&
648 objectToString(d) === '[object Date]';
650 exports.isDate = isDate;
652 function isError(e) {
653 return e !== null && typeof e === 'object' &&
654 (objectToString(e) === '[object Error]' || e instanceof Error);
656 exports.isError = isError;
658 function isFunction(arg) {
659 return typeof arg === 'function';
661 exports.isFunction = isFunction;
663 function isPrimitive(arg) {
664 return arg === null ||
665 typeof arg !== 'object' && typeof arg !== 'function';
667 exports.isPrimitive = isPrimitive;
669 function isBuffer(arg) {
670 return arg instanceof Buffer;
672 exports.isBuffer = isBuffer;
674 function objectToString(o) {
675 return Object.prototype.toString.call(o);
680 return n < 10 ? '0' + n.toString(10) : n.toString(10);
684 const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
685 'Oct', 'Nov', 'Dec'];
688 function timestamp() {
690 var time = [pad(d.getHours()),
692 pad(d.getSeconds())].join(':');
693 return [d.getDate(), months[d.getMonth()], time].join(' ');
697 // log is just a thin wrapper to console.log that prepends a timestamp
698 exports.log = function() {
699 console.log('%s - %s', timestamp(), exports.format.apply(exports, arguments));
704 * Inherit the prototype methods from one constructor into another.
706 * The Function.prototype.inherits from lang.js rewritten as a standalone
707 * function (not on Function.prototype). NOTE: If this file is to be loaded
708 * during bootstrapping this function needs to be rewritten using some native
709 * functions as prototype setup using normal JavaScript does not work as
710 * expected during bootstrapping (see mirror.js in r114903).
712 * @param {function} ctor Constructor function which needs to inherit the
714 * @param {function} superCtor Constructor function to inherit prototype from.
715 * @throws {TypeError} Will error if either constructor is null, or if
716 * the super constructor lacks a prototype.
718 exports.inherits = function(ctor, superCtor) {
720 if (ctor === undefined || ctor === null)
721 throw new TypeError('The constructor to `inherits` must not be ' +
722 'null or undefined.');
724 if (superCtor === undefined || superCtor === null)
725 throw new TypeError('The super constructor to `inherits` must not ' +
726 'be null or undefined.');
728 if (superCtor.prototype === undefined)
729 throw new TypeError('The super constructor to `inherits` must ' +
730 'have a prototype.');
732 ctor.super_ = superCtor;
733 ctor.prototype = Object.create(superCtor.prototype, {
743 exports._extend = function(origin, add) {
744 // Don't do anything if add isn't an object
745 if (add === null || typeof add !== 'object') return origin;
747 var keys = Object.keys(add);
750 origin[keys[i]] = add[keys[i]];
755 function hasOwnProperty(obj, prop) {
756 return Object.prototype.hasOwnProperty.call(obj, prop);
760 // Deprecated old stuff.
762 exports.p = exports.deprecate(function() {
763 for (var i = 0, len = arguments.length; i < len; ++i) {
764 console.error(exports.inspect(arguments[i]));
766 }, 'util.p: Use console.error() instead');
769 exports.exec = exports.deprecate(function() {
770 return require('child_process').exec.apply(this, arguments);
771 }, 'util.exec is now called `child_process.exec`.');
774 exports.print = exports.deprecate(function() {
775 for (var i = 0, len = arguments.length; i < len; ++i) {
776 process.stdout.write(String(arguments[i]));
778 }, 'util.print: Use console.log instead');
781 exports.puts = exports.deprecate(function() {
782 for (var i = 0, len = arguments.length; i < len; ++i) {
783 process.stdout.write(arguments[i] + '\n');
785 }, 'util.puts: Use console.log instead');
788 exports.debug = exports.deprecate(function(x) {
789 process.stderr.write('DEBUG: ' + x + '\n');
790 }, 'util.debug: Use console.error instead');
793 exports.error = exports.deprecate(function(x) {
794 for (var i = 0, len = arguments.length; i < len; ++i) {
795 process.stderr.write(arguments[i] + '\n');
797 }, 'util.error: Use console.error instead');
800 exports.pump = exports.deprecate(function(readStream, writeStream, callback) {
801 var callbackCalled = false;
803 function call(a, b, c) {
804 if (callback && !callbackCalled) {
806 callbackCalled = true;
810 readStream.addListener('data', function(chunk) {
811 if (writeStream.write(chunk) === false) readStream.pause();
814 writeStream.addListener('drain', function() {
818 readStream.addListener('end', function() {
822 readStream.addListener('close', function() {
826 readStream.addListener('error', function(err) {
831 writeStream.addListener('error', function(err) {
832 readStream.destroy();
835 }, 'util.pump(): Use readableStream.pipe() instead');
838 exports._errnoException = function(err, syscall, original) {
839 var errname = uv.errname(err);
840 var message = syscall + ' ' + errname;
842 message += ' ' + original;
843 var e = new Error(message);
851 exports._exceptionWithHostPort = function(err,
857 if (port && port > 0) {
858 details = address + ':' + port;
864 details += ' - Local (' + additional + ')';
866 var ex = exports._errnoException(err, syscall, details);
867 ex.address = address;