1 // Copyright Joyent, Inc. and other Node contributors.
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
24 var formatRegExp = /%[sdj%]/g;
25 exports.format = function(f) {
28 for (var i = 0; i < arguments.length; i++) {
29 objects.push(inspect(arguments[i]));
31 return objects.join(' ');
36 var len = args.length;
37 var str = String(f).replace(formatRegExp, function(x) {
38 if (x === '%%') return '%';
39 if (i >= len) return x;
41 case '%s': return String(args[i++]);
42 case '%d': return Number(args[i++]);
45 return JSON.stringify(args[i++]);
53 for (var x = args[i]; i < len; x = args[++i]) {
54 if (isNull(x) || !isObject(x)) {
57 str += ' ' + inspect(x);
64 // Mark that a method should not be used.
65 // Returns a modified function which warns once by default.
66 // If --no-deprecation is set, then it is a no-op.
67 exports.deprecate = function(fn, msg) {
68 // Allow for deprecating things in the process of starting up.
69 if (isUndefined(global.process)) {
71 return exports.deprecate(fn, msg).apply(this, arguments);
75 if (process.noDeprecation === true) {
80 function deprecated() {
82 if (process.throwDeprecation) {
84 } else if (process.traceDeprecation) {
91 return fn.apply(this, arguments);
100 exports.debuglog = function(set) {
101 if (isUndefined(debugEnviron))
102 debugEnviron = process.env.NODE_DEBUG || '';
103 set = set.toUpperCase();
105 if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) {
106 var pid = process.pid;
107 debugs[set] = function() {
108 var msg = exports.format.apply(exports, arguments);
109 console.error('%s %d: %s', set, pid, msg);
112 debugs[set] = function() {};
120 * Echos the value of a value. Trys to print the value out
121 * in the best way possible given the different types.
123 * @param {Object} obj The object to print out.
124 * @param {Object} opts Optional options object that alters the output.
126 /* legacy: obj, showHidden, depth, colors*/
127 function inspect(obj, opts) {
131 stylize: stylizeNoColor
134 if (arguments.length >= 3) ctx.depth = arguments[2];
135 if (arguments.length >= 4) ctx.colors = arguments[3];
136 if (isBoolean(opts)) {
138 ctx.showHidden = opts;
140 // got an "options" object
141 exports._extend(ctx, opts);
143 // set default options
144 if (isUndefined(ctx.showHidden)) ctx.showHidden = false;
145 if (isUndefined(ctx.depth)) ctx.depth = 2;
146 if (isUndefined(ctx.colors)) ctx.colors = false;
147 if (isUndefined(ctx.customInspect)) ctx.customInspect = true;
148 if (ctx.colors) ctx.stylize = stylizeWithColor;
149 return formatValue(ctx, obj, ctx.depth);
151 exports.inspect = inspect;
154 // http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
158 'underline' : [4, 24],
166 'magenta' : [35, 39],
171 // Don't use 'blue' not visible on cmd.exe
181 // "name": intentionally not styling
186 function stylizeWithColor(str, styleType) {
187 var style = inspect.styles[styleType];
190 return '\u001b[' + inspect.colors[style][0] + 'm' + str +
191 '\u001b[' + inspect.colors[style][1] + 'm';
198 function stylizeNoColor(str, styleType) {
203 function arrayToHash(array) {
206 array.forEach(function(val, idx) {
214 function formatValue(ctx, value, recurseTimes) {
215 // Provide a hook for user-specified inspect functions.
216 // Check that value is an object with an inspect function on it
217 if (ctx.customInspect &&
219 isFunction(value.inspect) &&
220 // Filter out the util module, it's inspect function is special
221 value.inspect !== exports.inspect &&
222 // Also filter out any prototype objects using the circular check.
223 !(value.constructor && value.constructor.prototype === value)) {
224 var ret = value.inspect(recurseTimes, ctx);
225 if (!isString(ret)) {
226 ret = formatValue(ctx, ret, recurseTimes);
231 // Primitive types cannot have properties
232 var primitive = formatPrimitive(ctx, value);
237 // Look up the keys of the object.
238 var keys = Object.keys(value);
239 var visibleKeys = arrayToHash(keys);
241 if (ctx.showHidden) {
242 keys = Object.getOwnPropertyNames(value);
245 // This could be a boxed primitive (new String(), etc.), check valueOf()
246 // NOTE: Avoid calling `valueOf` on `Date` instance because it will return
247 // a number which, when object has some additional user-stored `keys`,
248 // will be printed out.
252 // the .valueOf() call can fail for a multitude of reasons
254 raw = value.valueOf();
260 // for boxed Strings, we have to remove the 0-n indexed entries,
261 // since they just noisey up the output and are redundant
262 keys = keys.filter(function(key) {
263 return !(key >= 0 && key < raw.length);
267 // Some type of object without properties can be shortcutted.
268 if (keys.length === 0) {
269 if (isFunction(value)) {
270 var name = value.name ? ': ' + value.name : '';
271 return ctx.stylize('[Function' + name + ']', 'special');
273 if (isRegExp(value)) {
274 return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
277 return ctx.stylize(Date.prototype.toString.call(value), 'date');
279 if (isError(value)) {
280 return formatError(value);
282 // now check the `raw` value to handle boxed primitives
284 formatted = formatPrimitiveNoColor(ctx, raw);
285 return ctx.stylize('[String: ' + formatted + ']', 'string');
288 formatted = formatPrimitiveNoColor(ctx, raw);
289 return ctx.stylize('[Number: ' + formatted + ']', 'number');
291 if (isBoolean(raw)) {
292 formatted = formatPrimitiveNoColor(ctx, raw);
293 return ctx.stylize('[Boolean: ' + formatted + ']', 'boolean');
297 var base = '', array = false, braces = ['{', '}'];
299 // Make Array say that they are Array
300 if (isArray(value)) {
305 // Make functions say that they are functions
306 if (isFunction(value)) {
307 var n = value.name ? ': ' + value.name : '';
308 base = ' [Function' + n + ']';
311 // Make RegExps say that they are RegExps
312 if (isRegExp(value)) {
313 base = ' ' + RegExp.prototype.toString.call(value);
316 // Make dates with properties first say the date
318 base = ' ' + Date.prototype.toUTCString.call(value);
321 // Make error with message first say the error
322 if (isError(value)) {
323 base = ' ' + formatError(value);
326 // Make boxed primitive Strings look like such
328 formatted = formatPrimitiveNoColor(ctx, raw);
329 base = ' ' + '[String: ' + formatted + ']';
332 // Make boxed primitive Numbers look like such
334 formatted = formatPrimitiveNoColor(ctx, raw);
335 base = ' ' + '[Number: ' + formatted + ']';
338 // Make boxed primitive Booleans look like such
339 if (isBoolean(raw)) {
340 formatted = formatPrimitiveNoColor(ctx, raw);
341 base = ' ' + '[Boolean: ' + formatted + ']';
344 if (keys.length === 0 && (!array || value.length === 0)) {
345 return braces[0] + base + braces[1];
348 if (recurseTimes < 0) {
349 if (isRegExp(value)) {
350 return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
352 return ctx.stylize('[Object]', 'special');
356 ctx.seen.push(value);
360 output = formatArray(ctx, value, recurseTimes, visibleKeys, keys);
362 output = keys.map(function(key) {
363 return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array);
369 return reduceToSingleString(output, base, braces);
373 function formatPrimitive(ctx, value) {
374 if (isUndefined(value))
375 return ctx.stylize('undefined', 'undefined');
376 if (isString(value)) {
377 var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '')
378 .replace(/'/g, "\\'")
379 .replace(/\\"/g, '"') + '\'';
380 return ctx.stylize(simple, 'string');
382 if (isNumber(value)) {
383 // Format -0 as '-0'. Strict equality won't distinguish 0 from -0,
384 // so instead we use the fact that 1 / -0 < 0 whereas 1 / 0 > 0 .
385 if (value === 0 && 1 / value < 0)
386 return ctx.stylize('-0', 'number');
387 return ctx.stylize('' + value, 'number');
389 if (isBoolean(value))
390 return ctx.stylize('' + value, 'boolean');
391 // For some reason typeof null is "object", so special case here.
393 return ctx.stylize('null', 'null');
394 // es6 symbol primitive
396 return ctx.stylize(value.toString(), 'symbol');
400 function formatPrimitiveNoColor(ctx, value) {
401 var stylize = ctx.stylize;
402 ctx.stylize = stylizeNoColor;
403 var str = formatPrimitive(ctx, value);
404 ctx.stylize = stylize;
409 function formatError(value) {
410 return '[' + Error.prototype.toString.call(value) + ']';
414 function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
416 for (var i = 0, l = value.length; i < l; ++i) {
417 if (hasOwnProperty(value, String(i))) {
418 output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
424 keys.forEach(function(key) {
425 if (!key.match(/^\d+$/)) {
426 output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
434 function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
436 desc = Object.getOwnPropertyDescriptor(value, key) || { value: value[key] };
439 str = ctx.stylize('[Getter/Setter]', 'special');
441 str = ctx.stylize('[Getter]', 'special');
445 str = ctx.stylize('[Setter]', 'special');
448 if (!hasOwnProperty(visibleKeys, key)) {
449 name = '[' + key + ']';
452 if (ctx.seen.indexOf(desc.value) < 0) {
453 if (isNull(recurseTimes)) {
454 str = formatValue(ctx, desc.value, null);
456 str = formatValue(ctx, desc.value, recurseTimes - 1);
458 if (str.indexOf('\n') > -1) {
460 str = str.split('\n').map(function(line) {
462 }).join('\n').substr(2);
464 str = '\n' + str.split('\n').map(function(line) {
470 str = ctx.stylize('[Circular]', 'special');
473 if (isUndefined(name)) {
474 if (array && key.match(/^\d+$/)) {
477 name = JSON.stringify('' + key);
478 if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
479 name = name.substr(1, name.length - 2);
480 name = ctx.stylize(name, 'name');
482 name = name.replace(/'/g, "\\'")
483 .replace(/\\"/g, '"')
484 .replace(/(^"|"$)/g, "'")
485 .replace(/\\\\/g, '\\');
486 name = ctx.stylize(name, 'string');
490 return name + ': ' + str;
494 function reduceToSingleString(output, base, braces) {
495 var length = output.reduce(function(prev, cur) {
496 return prev + cur.replace(/\u001b\[\d\d?m/g, '').length + 1;
501 (base === '' ? '' : base + '\n ') +
503 output.join(',\n ') +
508 return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];
512 // NOTE: These type checking functions intentionally don't use `instanceof`
513 // because it is fragile and can be easily faked with `Object.create()`.
514 var isArray = exports.isArray = Array.isArray;
516 function isBoolean(arg) {
517 return typeof arg === 'boolean';
519 exports.isBoolean = isBoolean;
521 function isNull(arg) {
524 exports.isNull = isNull;
526 function isNullOrUndefined(arg) {
529 exports.isNullOrUndefined = isNullOrUndefined;
531 function isNumber(arg) {
532 return typeof arg === 'number';
534 exports.isNumber = isNumber;
536 function isString(arg) {
537 return typeof arg === 'string';
539 exports.isString = isString;
541 function isSymbol(arg) {
542 return typeof arg === 'symbol';
544 exports.isSymbol = isSymbol;
546 function isUndefined(arg) {
547 return arg === void 0;
549 exports.isUndefined = isUndefined;
551 function isRegExp(re) {
552 return isObject(re) && objectToString(re) === '[object RegExp]';
554 exports.isRegExp = isRegExp;
556 function isObject(arg) {
557 return typeof arg === 'object' && arg !== null;
559 exports.isObject = isObject;
562 return isObject(d) && objectToString(d) === '[object Date]';
564 exports.isDate = isDate;
566 function isError(e) {
567 return isObject(e) &&
568 (objectToString(e) === '[object Error]' || e instanceof Error);
570 exports.isError = isError;
572 function isFunction(arg) {
573 return typeof arg === 'function';
575 exports.isFunction = isFunction;
577 function isPrimitive(arg) {
578 return arg === null ||
579 typeof arg === 'boolean' ||
580 typeof arg === 'number' ||
581 typeof arg === 'string' ||
582 typeof arg === 'symbol' || // ES6 symbol
583 typeof arg === 'undefined';
585 exports.isPrimitive = isPrimitive;
587 function isBuffer(arg) {
588 return arg instanceof Buffer;
590 exports.isBuffer = isBuffer;
592 function objectToString(o) {
593 return Object.prototype.toString.call(o);
598 return n < 10 ? '0' + n.toString(10) : n.toString(10);
602 var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
603 'Oct', 'Nov', 'Dec'];
606 function timestamp() {
608 var time = [pad(d.getHours()),
610 pad(d.getSeconds())].join(':');
611 return [d.getDate(), months[d.getMonth()], time].join(' ');
615 // log is just a thin wrapper to console.log that prepends a timestamp
616 exports.log = function() {
617 console.log('%s - %s', timestamp(), exports.format.apply(exports, arguments));
622 * Inherit the prototype methods from one constructor into another.
624 * The Function.prototype.inherits from lang.js rewritten as a standalone
625 * function (not on Function.prototype). NOTE: If this file is to be loaded
626 * during bootstrapping this function needs to be rewritten using some native
627 * functions as prototype setup using normal JavaScript does not work as
628 * expected during bootstrapping (see mirror.js in r114903).
630 * @param {function} ctor Constructor function which needs to inherit the
632 * @param {function} superCtor Constructor function to inherit prototype from.
634 exports.inherits = function(ctor, superCtor) {
635 ctor.super_ = superCtor;
636 ctor.prototype = Object.create(superCtor.prototype, {
646 exports._extend = function(origin, add) {
647 // Don't do anything if add isn't an object
648 if (!add || !isObject(add)) return origin;
650 var keys = Object.keys(add);
653 origin[keys[i]] = add[keys[i]];
658 function hasOwnProperty(obj, prop) {
659 return Object.prototype.hasOwnProperty.call(obj, prop);
663 // Deprecated old stuff.
665 exports.p = exports.deprecate(function() {
666 for (var i = 0, len = arguments.length; i < len; ++i) {
667 console.error(exports.inspect(arguments[i]));
669 }, 'util.p: Use console.error() instead');
672 exports.exec = exports.deprecate(function() {
673 return require('child_process').exec.apply(this, arguments);
674 }, 'util.exec is now called `child_process.exec`.');
677 exports.print = exports.deprecate(function() {
678 for (var i = 0, len = arguments.length; i < len; ++i) {
679 process.stdout.write(String(arguments[i]));
681 }, 'util.print: Use console.log instead');
684 exports.puts = exports.deprecate(function() {
685 for (var i = 0, len = arguments.length; i < len; ++i) {
686 process.stdout.write(arguments[i] + '\n');
688 }, 'util.puts: Use console.log instead');
691 exports.debug = exports.deprecate(function(x) {
692 process.stderr.write('DEBUG: ' + x + '\n');
693 }, 'util.debug: Use console.error instead');
696 exports.error = exports.deprecate(function(x) {
697 for (var i = 0, len = arguments.length; i < len; ++i) {
698 process.stderr.write(arguments[i] + '\n');
700 }, 'util.error: Use console.error instead');
703 exports.pump = exports.deprecate(function(readStream, writeStream, callback) {
704 var callbackCalled = false;
706 function call(a, b, c) {
707 if (callback && !callbackCalled) {
709 callbackCalled = true;
713 readStream.addListener('data', function(chunk) {
714 if (writeStream.write(chunk) === false) readStream.pause();
717 writeStream.addListener('drain', function() {
721 readStream.addListener('end', function() {
725 readStream.addListener('close', function() {
729 readStream.addListener('error', function(err) {
734 writeStream.addListener('error', function(err) {
735 readStream.destroy();
738 }, 'util.pump(): Use readableStream.pipe() instead');
742 exports._errnoException = function(err, syscall, original) {
743 if (isUndefined(uv)) uv = process.binding('uv');
744 var errname = uv.errname(err);
745 var message = syscall + ' ' + errname;
747 message += ' ' + original;
748 var e = new Error(message);