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');
326 if (isNumber(value)) {
327 // Format -0 as '-0'. Strict equality won't distinguish 0 from -0,
328 // so instead we use the fact that 1 / -0 < 0 whereas 1 / 0 > 0 .
329 if (value === 0 && 1 / value < 0)
330 return ctx.stylize('-0', 'number');
331 return ctx.stylize('' + value, 'number');
333 if (isBoolean(value))
334 return ctx.stylize('' + value, 'boolean');
335 // For some reason typeof null is "object", so special case here.
337 return ctx.stylize('null', 'null');
341 function formatError(value) {
342 return '[' + Error.prototype.toString.call(value) + ']';
346 function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
348 for (var i = 0, l = value.length; i < l; ++i) {
349 if (hasOwnProperty(value, String(i))) {
350 output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
356 keys.forEach(function(key) {
357 if (!key.match(/^\d+$/)) {
358 output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
366 function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
368 desc = Object.getOwnPropertyDescriptor(value, key) || { value: value[key] };
371 str = ctx.stylize('[Getter/Setter]', 'special');
373 str = ctx.stylize('[Getter]', 'special');
377 str = ctx.stylize('[Setter]', 'special');
380 if (!hasOwnProperty(visibleKeys, key)) {
381 name = '[' + key + ']';
384 if (ctx.seen.indexOf(desc.value) < 0) {
385 if (isNull(recurseTimes)) {
386 str = formatValue(ctx, desc.value, null);
388 str = formatValue(ctx, desc.value, recurseTimes - 1);
390 if (str.indexOf('\n') > -1) {
392 str = str.split('\n').map(function(line) {
394 }).join('\n').substr(2);
396 str = '\n' + str.split('\n').map(function(line) {
402 str = ctx.stylize('[Circular]', 'special');
405 if (isUndefined(name)) {
406 if (array && key.match(/^\d+$/)) {
409 name = JSON.stringify('' + key);
410 if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
411 name = name.substr(1, name.length - 2);
412 name = ctx.stylize(name, 'name');
414 name = name.replace(/'/g, "\\'")
415 .replace(/\\"/g, '"')
416 .replace(/(^"|"$)/g, "'");
417 name = ctx.stylize(name, 'string');
421 return name + ': ' + str;
425 function reduceToSingleString(output, base, braces) {
427 var length = output.reduce(function(prev, cur) {
429 if (cur.indexOf('\n') >= 0) numLinesEst++;
430 return prev + cur.replace(/\u001b\[\d\d?m/g, '').length + 1;
435 (base === '' ? '' : base + '\n ') +
437 output.join(',\n ') +
442 return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];
446 // NOTE: These type checking functions intentionally don't use `instanceof`
447 // because it is fragile and can be easily faked with `Object.create()`.
448 function isArray(ar) {
449 return Array.isArray(ar);
451 exports.isArray = isArray;
453 function isBoolean(arg) {
454 return typeof arg === 'boolean';
456 exports.isBoolean = isBoolean;
458 function isNull(arg) {
461 exports.isNull = isNull;
463 function isNullOrUndefined(arg) {
466 exports.isNullOrUndefined = isNullOrUndefined;
468 function isNumber(arg) {
469 return typeof arg === 'number';
471 exports.isNumber = isNumber;
473 function isString(arg) {
474 return typeof arg === 'string';
476 exports.isString = isString;
478 function isSymbol(arg) {
479 return typeof arg === 'symbol';
481 exports.isSymbol = isSymbol;
483 function isUndefined(arg) {
484 return arg === void 0;
486 exports.isUndefined = isUndefined;
488 function isRegExp(re) {
489 return isObject(re) && objectToString(re) === '[object RegExp]';
491 exports.isRegExp = isRegExp;
493 function isObject(arg) {
494 return typeof arg === 'object' && arg !== null;
496 exports.isObject = isObject;
499 return isObject(d) && objectToString(d) === '[object Date]';
501 exports.isDate = isDate;
503 function isError(e) {
504 return isObject(e) &&
505 (objectToString(e) === '[object Error]' || e instanceof Error);
507 exports.isError = isError;
509 function isFunction(arg) {
510 return typeof arg === 'function';
512 exports.isFunction = isFunction;
514 function isPrimitive(arg) {
515 return arg === null ||
516 typeof arg === 'boolean' ||
517 typeof arg === 'number' ||
518 typeof arg === 'string' ||
519 typeof arg === 'symbol' || // ES6 symbol
520 typeof arg === 'undefined';
522 exports.isPrimitive = isPrimitive;
524 function isBuffer(arg) {
525 return arg instanceof Buffer;
527 exports.isBuffer = isBuffer;
529 function objectToString(o) {
530 return Object.prototype.toString.call(o);
535 return n < 10 ? '0' + n.toString(10) : n.toString(10);
539 var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
540 'Oct', 'Nov', 'Dec'];
543 function timestamp() {
545 var time = [pad(d.getHours()),
547 pad(d.getSeconds())].join(':');
548 return [d.getDate(), months[d.getMonth()], time].join(' ');
552 // log is just a thin wrapper to console.log that prepends a timestamp
553 exports.log = function() {
554 console.log('%s - %s', timestamp(), exports.format.apply(exports, arguments));
559 * Inherit the prototype methods from one constructor into another.
561 * The Function.prototype.inherits from lang.js rewritten as a standalone
562 * function (not on Function.prototype). NOTE: If this file is to be loaded
563 * during bootstrapping this function needs to be rewritten using some native
564 * functions as prototype setup using normal JavaScript does not work as
565 * expected during bootstrapping (see mirror.js in r114903).
567 * @param {function} ctor Constructor function which needs to inherit the
569 * @param {function} superCtor Constructor function to inherit prototype from.
571 exports.inherits = function(ctor, superCtor) {
572 ctor.super_ = superCtor;
573 ctor.prototype = Object.create(superCtor.prototype, {
583 exports._extend = function(origin, add) {
584 // Don't do anything if add isn't an object
585 if (!add || !isObject(add)) return origin;
587 var keys = Object.keys(add);
590 origin[keys[i]] = add[keys[i]];
595 function hasOwnProperty(obj, prop) {
596 return Object.prototype.hasOwnProperty.call(obj, prop);
600 // Deprecated old stuff.
602 exports.p = exports.deprecate(function() {
603 for (var i = 0, len = arguments.length; i < len; ++i) {
604 console.error(exports.inspect(arguments[i]));
606 }, 'util.p: Use console.error() instead');
609 exports.exec = exports.deprecate(function() {
610 return require('child_process').exec.apply(this, arguments);
611 }, 'util.exec is now called `child_process.exec`.');
614 exports.print = exports.deprecate(function() {
615 for (var i = 0, len = arguments.length; i < len; ++i) {
616 process.stdout.write(String(arguments[i]));
618 }, 'util.print: Use console.log instead');
621 exports.puts = exports.deprecate(function() {
622 for (var i = 0, len = arguments.length; i < len; ++i) {
623 process.stdout.write(arguments[i] + '\n');
625 }, 'util.puts: Use console.log instead');
628 exports.debug = exports.deprecate(function(x) {
629 process.stderr.write('DEBUG: ' + x + '\n');
630 }, 'util.debug: Use console.error instead');
633 exports.error = exports.deprecate(function(x) {
634 for (var i = 0, len = arguments.length; i < len; ++i) {
635 process.stderr.write(arguments[i] + '\n');
637 }, 'util.error: Use console.error instead');
640 exports.pump = exports.deprecate(function(readStream, writeStream, callback) {
641 var callbackCalled = false;
643 function call(a, b, c) {
644 if (callback && !callbackCalled) {
646 callbackCalled = true;
650 readStream.addListener('data', function(chunk) {
651 if (writeStream.write(chunk) === false) readStream.pause();
654 writeStream.addListener('drain', function() {
658 readStream.addListener('end', function() {
662 readStream.addListener('close', function() {
666 readStream.addListener('error', function(err) {
671 writeStream.addListener('error', function(err) {
672 readStream.destroy();
675 }, 'util.pump(): Use readableStream.pipe() instead');
679 exports._errnoException = function(err, syscall) {
680 if (isUndefined(uv)) uv = process.binding('uv');
681 var errname = uv.errname(err);
682 var e = new Error(syscall + ' ' + errname);