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.
22 var formatRegExp = /%[sdj%]/g;
23 exports.format = function(f) {
26 for (var i = 0; i < arguments.length; i++) {
27 objects.push(inspect(arguments[i]));
29 return objects.join(' ');
34 var len = args.length;
35 var str = String(f).replace(formatRegExp, function(x) {
36 if (x === '%%') return '%';
37 if (i >= len) return x;
39 case '%s': return String(args[i++]);
40 case '%d': return Number(args[i++]);
43 return JSON.stringify(args[i++]);
51 for (var x = args[i]; i < len; x = args[++i]) {
52 if (isNull(x) || !isObject(x)) {
55 str += ' ' + inspect(x);
62 // Mark that a method should not be used.
63 // Returns a modified function which warns once by default.
64 // If --no-deprecation is set, then it is a no-op.
65 exports.deprecate = function(fn, msg) {
66 // Allow for deprecating things in the process of starting up.
67 if (isUndefined(global.process)) {
69 return exports.deprecate(fn, msg).apply(this, arguments);
73 if (process.noDeprecation === true) {
78 function deprecated() {
80 if (process.throwDeprecation) {
82 } else if (process.traceDeprecation) {
89 return fn.apply(this, arguments);
98 exports.debuglog = function(set) {
99 if (isUndefined(debugEnviron))
100 debugEnviron = process.env.NODE_DEBUG || '';
101 set = set.toUpperCase();
103 if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) {
104 var pid = process.pid;
105 debugs[set] = function() {
106 var msg = exports.format.apply(exports, arguments);
107 console.error('%s %d: %s', set, pid, msg);
110 debugs[set] = function() {};
118 * Echos the value of a value. Trys to print the value out
119 * in the best way possible given the different types.
121 * @param {Object} obj The object to print out.
122 * @param {Object} opts Optional options object that alters the output.
124 /* legacy: obj, showHidden, depth, colors*/
125 function inspect(obj, opts) {
129 stylize: stylizeNoColor
132 if (arguments.length >= 3) ctx.depth = arguments[2];
133 if (arguments.length >= 4) ctx.colors = arguments[3];
134 if (isBoolean(opts)) {
136 ctx.showHidden = opts;
138 // got an "options" object
139 exports._extend(ctx, opts);
141 // set default options
142 if (isUndefined(ctx.showHidden)) ctx.showHidden = false;
143 if (isUndefined(ctx.depth)) ctx.depth = 2;
144 if (isUndefined(ctx.colors)) ctx.colors = false;
145 if (isUndefined(ctx.customInspect)) ctx.customInspect = true;
146 if (ctx.colors) ctx.stylize = stylizeWithColor;
147 return formatValue(ctx, obj, ctx.depth);
149 exports.inspect = inspect;
152 // http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
156 'underline' : [4, 24],
164 'magenta' : [35, 39],
169 // Don't use 'blue' not visible on cmd.exe
178 // "name": intentionally not styling
183 function stylizeWithColor(str, styleType) {
184 var style = inspect.styles[styleType];
187 return '\u001b[' + inspect.colors[style][0] + 'm' + str +
188 '\u001b[' + inspect.colors[style][1] + 'm';
195 function stylizeNoColor(str, styleType) {
200 function arrayToHash(array) {
203 array.forEach(function(val, idx) {
211 function formatValue(ctx, value, recurseTimes) {
212 // Provide a hook for user-specified inspect functions.
213 // Check that value is an object with an inspect function on it
214 if (ctx.customInspect &&
216 isFunction(value.inspect) &&
217 // Filter out the util module, it's inspect function is special
218 value.inspect !== exports.inspect &&
219 // Also filter out any prototype objects using the circular check.
220 !(value.constructor && value.constructor.prototype === value)) {
221 var ret = value.inspect(recurseTimes, ctx);
222 if (!isString(ret)) {
223 ret = formatValue(ctx, ret, recurseTimes);
228 // Primitive types cannot have properties
229 var primitive = formatPrimitive(ctx, value);
234 // Look up the keys of the object.
235 var keys = Object.keys(value);
236 var visibleKeys = arrayToHash(keys);
238 if (ctx.showHidden) {
239 keys = Object.getOwnPropertyNames(value);
242 // Some type of object without properties can be shortcutted.
243 if (keys.length === 0) {
244 if (isFunction(value)) {
245 var name = value.name ? ': ' + value.name : '';
246 return ctx.stylize('[Function' + name + ']', 'special');
248 if (isRegExp(value)) {
249 return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
252 return ctx.stylize(Date.prototype.toString.call(value), 'date');
254 if (isError(value)) {
255 return formatError(value);
259 var base = '', array = false, braces = ['{', '}'];
261 // Make Array say that they are Array
262 if (isArray(value)) {
267 // Make functions say that they are functions
268 if (isFunction(value)) {
269 var n = value.name ? ': ' + value.name : '';
270 base = ' [Function' + n + ']';
273 // Make RegExps say that they are RegExps
274 if (isRegExp(value)) {
275 base = ' ' + RegExp.prototype.toString.call(value);
278 // Make dates with properties first say the date
280 base = ' ' + Date.prototype.toUTCString.call(value);
283 // Make error with message first say the error
284 if (isError(value)) {
285 base = ' ' + formatError(value);
288 if (keys.length === 0 && (!array || value.length == 0)) {
289 return braces[0] + base + braces[1];
292 if (recurseTimes < 0) {
293 if (isRegExp(value)) {
294 return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
296 return ctx.stylize('[Object]', 'special');
300 ctx.seen.push(value);
304 output = formatArray(ctx, value, recurseTimes, visibleKeys, keys);
306 output = keys.map(function(key) {
307 return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array);
313 return reduceToSingleString(output, base, braces);
317 function formatPrimitive(ctx, value) {
318 if (isUndefined(value))
319 return ctx.stylize('undefined', 'undefined');
320 if (isString(value)) {
321 var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '')
322 .replace(/'/g, "\\'")
323 .replace(/\\"/g, '"') + '\'';
324 return ctx.stylize(simple, 'string');
327 return ctx.stylize('' + value, 'number');
328 if (isBoolean(value))
329 return ctx.stylize('' + value, 'boolean');
330 // For some reason typeof null is "object", so special case here.
332 return ctx.stylize('null', 'null');
336 function formatError(value) {
337 return '[' + Error.prototype.toString.call(value) + ']';
341 function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
343 for (var i = 0, l = value.length; i < l; ++i) {
344 if (hasOwnProperty(value, String(i))) {
345 output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
351 keys.forEach(function(key) {
352 if (!key.match(/^\d+$/)) {
353 output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
361 function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
363 desc = Object.getOwnPropertyDescriptor(value, key) || { value: value[key] };
366 str = ctx.stylize('[Getter/Setter]', 'special');
368 str = ctx.stylize('[Getter]', 'special');
372 str = ctx.stylize('[Setter]', 'special');
375 if (!hasOwnProperty(visibleKeys, key)) {
376 name = '[' + key + ']';
379 if (ctx.seen.indexOf(desc.value) < 0) {
380 if (isNull(recurseTimes)) {
381 str = formatValue(ctx, desc.value, null);
383 str = formatValue(ctx, desc.value, recurseTimes - 1);
385 if (str.indexOf('\n') > -1) {
387 str = str.split('\n').map(function(line) {
389 }).join('\n').substr(2);
391 str = '\n' + str.split('\n').map(function(line) {
397 str = ctx.stylize('[Circular]', 'special');
400 if (isUndefined(name)) {
401 if (array && key.match(/^\d+$/)) {
404 name = JSON.stringify('' + key);
405 if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
406 name = name.substr(1, name.length - 2);
407 name = ctx.stylize(name, 'name');
409 name = name.replace(/'/g, "\\'")
410 .replace(/\\"/g, '"')
411 .replace(/(^"|"$)/g, "'");
412 name = ctx.stylize(name, 'string');
416 return name + ': ' + str;
420 function reduceToSingleString(output, base, braces) {
422 var length = output.reduce(function(prev, cur) {
424 if (cur.indexOf('\n') >= 0) numLinesEst++;
425 return prev + cur.replace(/\u001b\[\d\d?m/g, '').length + 1;
430 (base === '' ? '' : base + '\n ') +
432 output.join(',\n ') +
437 return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];
441 // NOTE: These type checking functions intentionally don't use `instanceof`
442 // because it is fragile and can be easily faked with `Object.create()`.
443 function isArray(ar) {
444 return Array.isArray(ar);
446 exports.isArray = isArray;
448 function isBoolean(arg) {
449 return typeof arg === 'boolean';
451 exports.isBoolean = isBoolean;
453 function isNull(arg) {
456 exports.isNull = isNull;
458 function isNullOrUndefined(arg) {
461 exports.isNullOrUndefined = isNullOrUndefined;
463 function isNumber(arg) {
464 return typeof arg === 'number';
466 exports.isNumber = isNumber;
468 function isString(arg) {
469 return typeof arg === 'string';
471 exports.isString = isString;
473 function isSymbol(arg) {
474 return typeof arg === 'symbol';
476 exports.isSymbol = isSymbol;
478 function isUndefined(arg) {
479 return arg === void 0;
481 exports.isUndefined = isUndefined;
483 function isRegExp(re) {
484 return isObject(re) && objectToString(re) === '[object RegExp]';
486 exports.isRegExp = isRegExp;
488 function isObject(arg) {
489 return typeof arg === 'object' && arg !== null;
491 exports.isObject = isObject;
494 return isObject(d) && objectToString(d) === '[object Date]';
496 exports.isDate = isDate;
498 function isError(e) {
499 return isObject(e) &&
500 (objectToString(e) === '[object Error]' || e instanceof Error);
502 exports.isError = isError;
504 function isFunction(arg) {
505 return typeof arg === 'function';
507 exports.isFunction = isFunction;
509 function isPrimitive(arg) {
510 return arg === null ||
511 typeof arg === 'boolean' ||
512 typeof arg === 'number' ||
513 typeof arg === 'string' ||
514 typeof arg === 'symbol' || // ES6 symbol
515 typeof arg === 'undefined';
517 exports.isPrimitive = isPrimitive;
519 function isBuffer(arg) {
520 return arg instanceof Buffer;
522 exports.isBuffer = isBuffer;
524 function objectToString(o) {
525 return Object.prototype.toString.call(o);
530 return n < 10 ? '0' + n.toString(10) : n.toString(10);
534 var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
535 'Oct', 'Nov', 'Dec'];
538 function timestamp() {
540 var time = [pad(d.getHours()),
542 pad(d.getSeconds())].join(':');
543 return [d.getDate(), months[d.getMonth()], time].join(' ');
547 // log is just a thin wrapper to console.log that prepends a timestamp
548 exports.log = function() {
549 console.log('%s - %s', timestamp(), exports.format.apply(exports, arguments));
554 * Inherit the prototype methods from one constructor into another.
556 * The Function.prototype.inherits from lang.js rewritten as a standalone
557 * function (not on Function.prototype). NOTE: If this file is to be loaded
558 * during bootstrapping this function needs to be rewritten using some native
559 * functions as prototype setup using normal JavaScript does not work as
560 * expected during bootstrapping (see mirror.js in r114903).
562 * @param {function} ctor Constructor function which needs to inherit the
564 * @param {function} superCtor Constructor function to inherit prototype from.
566 exports.inherits = function(ctor, superCtor) {
567 ctor.super_ = superCtor;
568 ctor.prototype = Object.create(superCtor.prototype, {
578 exports._extend = function(origin, add) {
579 // Don't do anything if add isn't an object
580 if (!add || !isObject(add)) return origin;
582 var keys = Object.keys(add);
585 origin[keys[i]] = add[keys[i]];
590 function hasOwnProperty(obj, prop) {
591 return Object.prototype.hasOwnProperty.call(obj, prop);
595 // Deprecated old stuff.
597 exports.p = exports.deprecate(function() {
598 for (var i = 0, len = arguments.length; i < len; ++i) {
599 console.error(exports.inspect(arguments[i]));
601 }, 'util.p: Use console.error() instead');
604 exports.exec = exports.deprecate(function() {
605 return require('child_process').exec.apply(this, arguments);
606 }, 'util.exec is now called `child_process.exec`.');
609 exports.print = exports.deprecate(function() {
610 for (var i = 0, len = arguments.length; i < len; ++i) {
611 process.stdout.write(String(arguments[i]));
613 }, 'util.print: Use console.log instead');
616 exports.puts = exports.deprecate(function() {
617 for (var i = 0, len = arguments.length; i < len; ++i) {
618 process.stdout.write(arguments[i] + '\n');
620 }, 'util.puts: Use console.log instead');
623 exports.debug = exports.deprecate(function(x) {
624 process.stderr.write('DEBUG: ' + x + '\n');
625 }, 'util.debug: Use console.error instead');
628 exports.error = exports.deprecate(function(x) {
629 for (var i = 0, len = arguments.length; i < len; ++i) {
630 process.stderr.write(arguments[i] + '\n');
632 }, 'util.error: Use console.error instead');
635 exports.pump = exports.deprecate(function(readStream, writeStream, callback) {
636 var callbackCalled = false;
638 function call(a, b, c) {
639 if (callback && !callbackCalled) {
641 callbackCalled = true;
645 readStream.addListener('data', function(chunk) {
646 if (writeStream.write(chunk) === false) readStream.pause();
649 writeStream.addListener('drain', function() {
653 readStream.addListener('end', function() {
657 readStream.addListener('close', function() {
661 readStream.addListener('error', function(err) {
666 writeStream.addListener('error', function(err) {
667 readStream.destroy();
670 }, 'util.pump(): Use readableStream.pipe() instead');
674 exports._errnoException = function(err, syscall) {
675 if (isUndefined(uv)) uv = process.binding('uv');
676 var errname = uv.errname(err);
677 var e = new Error(syscall + ' ' + errname);