2017-01-03 21:16:48 +00:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2014-11-22 15:59:48 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-05-30 11:17:09 +00:00
|
|
|
const errors = require('internal/errors');
|
2017-06-12 15:25:53 +00:00
|
|
|
const { TextDecoder, TextEncoder } = require('internal/encoding');
|
2015-09-30 00:07:56 +00:00
|
|
|
|
2017-06-19 20:17:29 +00:00
|
|
|
const { errname } = process.binding('uv');
|
|
|
|
|
|
|
|
const {
|
|
|
|
getPromiseDetails,
|
|
|
|
getProxyDetails,
|
|
|
|
isAnyArrayBuffer,
|
|
|
|
isDataView,
|
|
|
|
isExternal,
|
|
|
|
isMap,
|
|
|
|
isMapIterator,
|
|
|
|
isPromise,
|
|
|
|
isSet,
|
|
|
|
isSetIterator,
|
|
|
|
isTypedArray,
|
|
|
|
isRegExp: _isRegExp,
|
|
|
|
isDate: _isDate,
|
|
|
|
kPending,
|
|
|
|
kRejected,
|
|
|
|
} = process.binding('util');
|
|
|
|
|
|
|
|
const {
|
|
|
|
customInspectSymbol,
|
|
|
|
deprecate,
|
|
|
|
getConstructorOf,
|
|
|
|
isError,
|
|
|
|
promisify
|
|
|
|
} = require('internal/util');
|
2016-08-09 18:48:56 +00:00
|
|
|
|
|
|
|
const inspectDefaultOptions = Object.seal({
|
|
|
|
showHidden: false,
|
|
|
|
depth: 2,
|
|
|
|
colors: false,
|
|
|
|
customInspect: true,
|
|
|
|
showProxy: false,
|
|
|
|
maxArrayLength: 100,
|
|
|
|
breakLength: 60
|
|
|
|
});
|
2015-11-25 21:37:43 +00:00
|
|
|
|
2017-06-07 23:00:33 +00:00
|
|
|
const numbersOnlyRE = /^\d+$/;
|
|
|
|
|
2017-06-19 20:17:29 +00:00
|
|
|
const propertyIsEnumerable = Object.prototype.propertyIsEnumerable;
|
|
|
|
const regExpToString = RegExp.prototype.toString;
|
|
|
|
const dateToISOString = Date.prototype.toISOString;
|
|
|
|
const errorToString = Error.prototype.toString;
|
|
|
|
|
2017-03-15 00:48:28 +00:00
|
|
|
var CIRCULAR_ERROR_MESSAGE;
|
2015-07-25 13:01:24 +00:00
|
|
|
var Debug;
|
2015-04-07 08:37:13 +00:00
|
|
|
|
2016-02-22 08:55:55 +00:00
|
|
|
function tryStringify(arg) {
|
|
|
|
try {
|
|
|
|
return JSON.stringify(arg);
|
2017-03-06 05:57:02 +00:00
|
|
|
} catch (err) {
|
2017-03-15 00:48:28 +00:00
|
|
|
// Populate the circular error message lazily
|
|
|
|
if (!CIRCULAR_ERROR_MESSAGE) {
|
|
|
|
try {
|
|
|
|
const a = {}; a.a = a; JSON.stringify(a);
|
|
|
|
} catch (err) {
|
|
|
|
CIRCULAR_ERROR_MESSAGE = err.message;
|
|
|
|
}
|
|
|
|
}
|
2017-03-06 05:57:02 +00:00
|
|
|
if (err.name === 'TypeError' && err.message === CIRCULAR_ERROR_MESSAGE)
|
|
|
|
return '[Circular]';
|
|
|
|
throw err;
|
2016-02-22 08:55:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-19 20:17:29 +00:00
|
|
|
function format(f) {
|
2015-01-29 01:05:53 +00:00
|
|
|
if (typeof f !== 'string') {
|
2016-02-22 08:55:55 +00:00
|
|
|
const objects = new Array(arguments.length);
|
2016-01-30 04:34:13 +00:00
|
|
|
for (var index = 0; index < arguments.length; index++) {
|
2016-02-22 08:55:55 +00:00
|
|
|
objects[index] = inspect(arguments[index]);
|
2011-07-29 15:26:45 +00:00
|
|
|
}
|
|
|
|
return objects.join(' ');
|
|
|
|
}
|
|
|
|
|
2017-04-14 00:51:56 +00:00
|
|
|
if (arguments.length === 1) return f;
|
2016-02-22 08:55:55 +00:00
|
|
|
|
2016-02-23 01:47:20 +00:00
|
|
|
var str = '';
|
|
|
|
var a = 1;
|
|
|
|
var lastPos = 0;
|
|
|
|
for (var i = 0; i < f.length;) {
|
|
|
|
if (f.charCodeAt(i) === 37/*'%'*/ && i + 1 < f.length) {
|
2017-04-14 00:51:56 +00:00
|
|
|
if (f.charCodeAt(i + 1) !== 37/*'%'*/ && a >= arguments.length) {
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
2016-02-23 01:47:20 +00:00
|
|
|
switch (f.charCodeAt(i + 1)) {
|
|
|
|
case 100: // 'd'
|
|
|
|
if (lastPos < i)
|
|
|
|
str += f.slice(lastPos, i);
|
|
|
|
str += Number(arguments[a++]);
|
2017-04-14 00:51:56 +00:00
|
|
|
break;
|
2016-12-16 20:20:15 +00:00
|
|
|
case 105: // 'i'
|
|
|
|
if (lastPos < i)
|
|
|
|
str += f.slice(lastPos, i);
|
|
|
|
str += parseInt(arguments[a++]);
|
2017-04-14 00:51:56 +00:00
|
|
|
break;
|
2016-12-16 20:20:15 +00:00
|
|
|
case 102: // 'f'
|
|
|
|
if (lastPos < i)
|
|
|
|
str += f.slice(lastPos, i);
|
|
|
|
str += parseFloat(arguments[a++]);
|
2017-04-14 00:51:56 +00:00
|
|
|
break;
|
2016-02-23 01:47:20 +00:00
|
|
|
case 106: // 'j'
|
|
|
|
if (lastPos < i)
|
|
|
|
str += f.slice(lastPos, i);
|
|
|
|
str += tryStringify(arguments[a++]);
|
2017-04-14 00:51:56 +00:00
|
|
|
break;
|
2016-02-23 01:47:20 +00:00
|
|
|
case 115: // 's'
|
|
|
|
if (lastPos < i)
|
|
|
|
str += f.slice(lastPos, i);
|
|
|
|
str += String(arguments[a++]);
|
2017-04-14 00:51:56 +00:00
|
|
|
break;
|
2017-07-31 22:08:44 +00:00
|
|
|
case 79: // 'O'
|
|
|
|
if (lastPos < i)
|
|
|
|
str += f.slice(lastPos, i);
|
|
|
|
str += inspect(arguments[a++]);
|
|
|
|
break;
|
|
|
|
case 111: // 'o'
|
|
|
|
if (lastPos < i)
|
|
|
|
str += f.slice(lastPos, i);
|
|
|
|
str += inspect(arguments[a++],
|
|
|
|
{ showHidden: true, depth: 4, showProxy: true });
|
|
|
|
break;
|
2016-02-23 01:47:20 +00:00
|
|
|
case 37: // '%'
|
|
|
|
if (lastPos < i)
|
|
|
|
str += f.slice(lastPos, i);
|
|
|
|
str += '%';
|
2017-04-14 00:51:56 +00:00
|
|
|
break;
|
2017-06-14 08:46:50 +00:00
|
|
|
default: // any other character is not a correct placeholder
|
|
|
|
if (lastPos < i)
|
|
|
|
str += f.slice(lastPos, i);
|
|
|
|
str += '%';
|
|
|
|
lastPos = i = i + 1;
|
|
|
|
continue;
|
2016-02-23 01:47:20 +00:00
|
|
|
}
|
2017-04-14 00:51:56 +00:00
|
|
|
lastPos = i = i + 2;
|
|
|
|
continue;
|
2011-07-29 15:26:45 +00:00
|
|
|
}
|
2016-02-23 01:47:20 +00:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
if (lastPos === 0)
|
|
|
|
str = f;
|
|
|
|
else if (lastPos < f.length)
|
|
|
|
str += f.slice(lastPos);
|
2017-04-14 00:51:56 +00:00
|
|
|
while (a < arguments.length) {
|
2016-02-23 01:47:20 +00:00
|
|
|
const x = arguments[a++];
|
2015-02-23 22:32:18 +00:00
|
|
|
if (x === null || (typeof x !== 'object' && typeof x !== 'symbol')) {
|
2017-06-19 20:17:29 +00:00
|
|
|
str += ` ${x}`;
|
2011-07-29 15:26:45 +00:00
|
|
|
} else {
|
2017-06-19 20:17:29 +00:00
|
|
|
str += ` ${inspect(x)}`;
|
2011-07-29 15:26:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return str;
|
2017-06-19 20:17:29 +00:00
|
|
|
}
|
2012-06-21 18:42:33 +00:00
|
|
|
|
|
|
|
|
2013-05-21 22:22:05 +00:00
|
|
|
var debugs = {};
|
2013-07-26 21:38:08 +00:00
|
|
|
var debugEnviron;
|
2017-06-19 20:17:29 +00:00
|
|
|
|
|
|
|
function debuglog(set) {
|
2017-06-21 13:37:42 +00:00
|
|
|
if (debugEnviron === undefined) {
|
|
|
|
debugEnviron = new Set(
|
|
|
|
(process.env.NODE_DEBUG || '').split(',').map((s) => s.toUpperCase()));
|
|
|
|
}
|
2013-05-21 22:22:05 +00:00
|
|
|
set = set.toUpperCase();
|
|
|
|
if (!debugs[set]) {
|
2017-06-21 13:37:42 +00:00
|
|
|
if (debugEnviron.has(set)) {
|
2013-05-21 22:22:05 +00:00
|
|
|
var pid = process.pid;
|
|
|
|
debugs[set] = function() {
|
|
|
|
var msg = exports.format.apply(exports, arguments);
|
|
|
|
console.error('%s %d: %s', set, pid, msg);
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
debugs[set] = function() {};
|
|
|
|
}
|
2010-10-11 21:04:09 +00:00
|
|
|
}
|
2013-05-21 22:22:05 +00:00
|
|
|
return debugs[set];
|
2017-06-19 20:17:29 +00:00
|
|
|
}
|
2010-10-11 21:04:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2016-02-17 11:40:55 +00:00
|
|
|
* Echos the value of a value. Tries to print the value out
|
2010-10-11 21:04:09 +00:00
|
|
|
* in the best way possible given the different types.
|
|
|
|
*
|
2010-12-02 20:11:23 +00:00
|
|
|
* @param {Object} obj The object to print out.
|
2012-10-10 01:47:08 +00:00
|
|
|
* @param {Object} opts Optional options object that alters the output.
|
2010-10-11 21:04:09 +00:00
|
|
|
*/
|
2012-10-12 18:44:02 +00:00
|
|
|
/* legacy: obj, showHidden, depth, colors*/
|
|
|
|
function inspect(obj, opts) {
|
2012-10-10 01:47:08 +00:00
|
|
|
// default options
|
2011-09-08 12:00:02 +00:00
|
|
|
var ctx = {
|
|
|
|
seen: [],
|
2012-10-10 01:47:08 +00:00
|
|
|
stylize: stylizeNoColor
|
2010-10-11 21:04:09 +00:00
|
|
|
};
|
2012-10-10 01:47:08 +00:00
|
|
|
// legacy...
|
2017-01-02 00:42:19 +00:00
|
|
|
if (arguments.length >= 3 && arguments[2] !== undefined) {
|
|
|
|
ctx.depth = arguments[2];
|
|
|
|
}
|
|
|
|
if (arguments.length >= 4 && arguments[3] !== undefined) {
|
|
|
|
ctx.colors = arguments[3];
|
|
|
|
}
|
2015-01-29 01:05:53 +00:00
|
|
|
if (typeof opts === 'boolean') {
|
2012-10-10 01:47:08 +00:00
|
|
|
// legacy...
|
|
|
|
ctx.showHidden = opts;
|
|
|
|
}
|
2016-08-09 18:48:56 +00:00
|
|
|
// Set default and user-specified options
|
|
|
|
ctx = Object.assign({}, inspect.defaultOptions, ctx, opts);
|
2012-10-10 01:47:08 +00:00
|
|
|
if (ctx.colors) ctx.stylize = stylizeWithColor;
|
2016-04-21 17:29:24 +00:00
|
|
|
if (ctx.maxArrayLength === null) ctx.maxArrayLength = Infinity;
|
2012-10-10 01:47:08 +00:00
|
|
|
return formatValue(ctx, obj, ctx.depth);
|
2011-09-08 12:00:02 +00:00
|
|
|
}
|
2017-06-19 20:17:29 +00:00
|
|
|
inspect.custom = customInspectSymbol;
|
2010-10-11 21:04:09 +00:00
|
|
|
|
2016-08-09 18:48:56 +00:00
|
|
|
Object.defineProperty(inspect, 'defaultOptions', {
|
|
|
|
get: function() {
|
|
|
|
return inspectDefaultOptions;
|
|
|
|
},
|
|
|
|
set: function(options) {
|
|
|
|
if (options === null || typeof options !== 'object') {
|
2017-05-30 11:17:09 +00:00
|
|
|
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'object');
|
2016-08-09 18:48:56 +00:00
|
|
|
}
|
|
|
|
Object.assign(inspectDefaultOptions, options);
|
|
|
|
return inspectDefaultOptions;
|
|
|
|
}
|
|
|
|
});
|
2010-10-11 21:04:09 +00:00
|
|
|
|
2011-09-08 12:00:02 +00:00
|
|
|
// http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
|
2017-02-28 18:58:56 +00:00
|
|
|
inspect.colors = Object.assign(Object.create(null), {
|
2016-05-05 05:20:27 +00:00
|
|
|
'bold': [1, 22],
|
|
|
|
'italic': [3, 23],
|
|
|
|
'underline': [4, 24],
|
|
|
|
'inverse': [7, 27],
|
|
|
|
'white': [37, 39],
|
|
|
|
'grey': [90, 39],
|
|
|
|
'black': [30, 39],
|
|
|
|
'blue': [34, 39],
|
|
|
|
'cyan': [36, 39],
|
|
|
|
'green': [32, 39],
|
|
|
|
'magenta': [35, 39],
|
|
|
|
'red': [31, 39],
|
|
|
|
'yellow': [33, 39]
|
2017-02-28 18:58:56 +00:00
|
|
|
});
|
2010-12-02 20:11:23 +00:00
|
|
|
|
2011-11-11 07:35:35 +00:00
|
|
|
// Don't use 'blue' not visible on cmd.exe
|
2017-02-28 18:58:56 +00:00
|
|
|
inspect.styles = Object.assign(Object.create(null), {
|
2011-10-04 22:08:18 +00:00
|
|
|
'special': 'cyan',
|
2011-11-11 07:35:35 +00:00
|
|
|
'number': 'yellow',
|
2011-10-04 22:08:18 +00:00
|
|
|
'boolean': 'yellow',
|
|
|
|
'undefined': 'grey',
|
|
|
|
'null': 'bold',
|
|
|
|
'string': 'green',
|
2014-10-01 02:44:39 +00:00
|
|
|
'symbol': 'green',
|
2011-10-04 22:08:18 +00:00
|
|
|
'date': 'magenta',
|
|
|
|
// "name": intentionally not styling
|
|
|
|
'regexp': 'red'
|
2017-02-28 18:58:56 +00:00
|
|
|
});
|
2010-12-02 20:11:23 +00:00
|
|
|
|
2011-09-08 12:00:02 +00:00
|
|
|
function stylizeWithColor(str, styleType) {
|
2012-07-16 21:50:15 +00:00
|
|
|
var style = inspect.styles[styleType];
|
2010-10-11 21:04:09 +00:00
|
|
|
|
2011-09-08 12:00:02 +00:00
|
|
|
if (style) {
|
2016-10-17 17:16:04 +00:00
|
|
|
return `\u001b[${inspect.colors[style][0]}m${str}` +
|
|
|
|
`\u001b[${inspect.colors[style][1]}m`;
|
2011-09-08 12:00:02 +00:00
|
|
|
} else {
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
}
|
2010-10-11 21:04:09 +00:00
|
|
|
|
2011-07-03 06:20:25 +00:00
|
|
|
|
2011-09-08 12:00:02 +00:00
|
|
|
function stylizeNoColor(str, styleType) {
|
|
|
|
return str;
|
|
|
|
}
|
2010-10-11 21:04:09 +00:00
|
|
|
|
|
|
|
|
2012-06-27 17:15:28 +00:00
|
|
|
function arrayToHash(array) {
|
2015-11-15 04:56:05 +00:00
|
|
|
var hash = Object.create(null);
|
2012-06-27 17:15:28 +00:00
|
|
|
|
2015-11-22 09:08:45 +00:00
|
|
|
for (var i = 0; i < array.length; i++) {
|
|
|
|
var val = array[i];
|
2012-06-27 17:15:28 +00:00
|
|
|
hash[val] = true;
|
2015-11-22 09:08:45 +00:00
|
|
|
}
|
2012-06-27 17:15:28 +00:00
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-30 00:07:56 +00:00
|
|
|
function ensureDebugIsInitialized() {
|
|
|
|
if (Debug === undefined) {
|
|
|
|
const runInDebugContext = require('vm').runInDebugContext;
|
2015-10-06 01:44:09 +00:00
|
|
|
Debug = runInDebugContext('Debug');
|
2015-09-30 00:07:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-08 12:00:02 +00:00
|
|
|
function formatValue(ctx, value, recurseTimes) {
|
2016-04-29 08:06:48 +00:00
|
|
|
if (ctx.showProxy &&
|
|
|
|
((typeof value === 'object' && value !== null) ||
|
|
|
|
typeof value === 'function')) {
|
|
|
|
var proxy = undefined;
|
|
|
|
var proxyCache = ctx.proxyCache;
|
|
|
|
if (!proxyCache)
|
|
|
|
proxyCache = ctx.proxyCache = new Map();
|
|
|
|
// Determine if we've already seen this object and have
|
|
|
|
// determined that it either is or is not a proxy.
|
|
|
|
if (proxyCache.has(value)) {
|
|
|
|
// We've seen it, if the value is not undefined, it's a Proxy.
|
|
|
|
proxy = proxyCache.get(value);
|
|
|
|
} else {
|
|
|
|
// Haven't seen it. Need to check.
|
|
|
|
// If it's not a Proxy, this will return undefined.
|
|
|
|
// Otherwise, it'll return an array. The first item
|
|
|
|
// is the target, the second item is the handler.
|
|
|
|
// We ignore (and do not return) the Proxy isRevoked property.
|
2017-06-19 20:17:29 +00:00
|
|
|
proxy = getProxyDetails(value);
|
2016-04-29 08:06:48 +00:00
|
|
|
if (proxy) {
|
|
|
|
// We know for a fact that this isn't a Proxy.
|
|
|
|
// Mark it as having already been evaluated.
|
|
|
|
// We do this because this object is passed
|
|
|
|
// recursively to formatValue below in order
|
|
|
|
// for it to get proper formatting, and because
|
|
|
|
// the target and handle objects also might be
|
|
|
|
// proxies... it's unfortunate but necessary.
|
|
|
|
proxyCache.set(proxy, undefined);
|
|
|
|
}
|
|
|
|
// If the object is not a Proxy, then this stores undefined.
|
|
|
|
// This tells the code above that we've already checked and
|
|
|
|
// ruled it out. If the object is a proxy, this caches the
|
|
|
|
// results of the getProxyDetails call.
|
|
|
|
proxyCache.set(value, proxy);
|
|
|
|
}
|
|
|
|
if (proxy) {
|
2017-06-19 20:17:29 +00:00
|
|
|
return `Proxy ${formatValue(ctx, proxy, recurseTimes)}`;
|
2016-04-29 08:06:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-08 12:00:02 +00:00
|
|
|
// Provide a hook for user-specified inspect functions.
|
|
|
|
// Check that value is an object with an inspect function on it
|
2016-08-18 22:26:36 +00:00
|
|
|
if (ctx.customInspect && value) {
|
|
|
|
const maybeCustomInspect = value[customInspectSymbol] || value.inspect;
|
|
|
|
|
|
|
|
if (typeof maybeCustomInspect === 'function' &&
|
|
|
|
// Filter out the util module, its inspect function is special
|
|
|
|
maybeCustomInspect !== exports.inspect &&
|
|
|
|
// Also filter out any prototype objects using the circular check.
|
|
|
|
!(value.constructor && value.constructor.prototype === value)) {
|
|
|
|
let ret = maybeCustomInspect.call(value, recurseTimes, ctx);
|
2016-08-18 22:44:38 +00:00
|
|
|
|
|
|
|
// If the custom inspection method returned `this`, don't go into
|
|
|
|
// infinite recursion.
|
|
|
|
if (ret !== value) {
|
|
|
|
if (typeof ret !== 'string') {
|
|
|
|
ret = formatValue(ctx, ret, recurseTimes);
|
|
|
|
}
|
|
|
|
return ret;
|
2016-08-18 22:26:36 +00:00
|
|
|
}
|
2013-01-30 03:06:32 +00:00
|
|
|
}
|
2011-09-08 12:00:02 +00:00
|
|
|
}
|
2010-10-11 21:04:09 +00:00
|
|
|
|
2011-09-08 12:00:02 +00:00
|
|
|
// Primitive types cannot have properties
|
|
|
|
var primitive = formatPrimitive(ctx, value);
|
|
|
|
if (primitive) {
|
|
|
|
return primitive;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look up the keys of the object.
|
2012-06-27 17:15:28 +00:00
|
|
|
var keys = Object.keys(value);
|
|
|
|
var visibleKeys = arrayToHash(keys);
|
2016-11-21 22:14:36 +00:00
|
|
|
const symbolKeys = Object.getOwnPropertySymbols(value);
|
|
|
|
const enumSymbolKeys = symbolKeys
|
2017-06-19 20:17:29 +00:00
|
|
|
.filter((key) => propertyIsEnumerable.call(value, key));
|
2016-11-21 22:14:36 +00:00
|
|
|
keys = keys.concat(enumSymbolKeys);
|
2012-06-27 17:15:28 +00:00
|
|
|
|
|
|
|
if (ctx.showHidden) {
|
2016-11-21 22:14:36 +00:00
|
|
|
keys = Object.getOwnPropertyNames(value).concat(symbolKeys);
|
2012-06-27 17:15:28 +00:00
|
|
|
}
|
2011-09-08 12:00:02 +00:00
|
|
|
|
2014-02-06 02:09:23 +00:00
|
|
|
// This could be a boxed primitive (new String(), etc.), check valueOf()
|
|
|
|
// NOTE: Avoid calling `valueOf` on `Date` instance because it will return
|
|
|
|
// a number which, when object has some additional user-stored `keys`,
|
|
|
|
// will be printed out.
|
|
|
|
var formatted;
|
|
|
|
var raw = value;
|
|
|
|
try {
|
|
|
|
// the .valueOf() call can fail for a multitude of reasons
|
|
|
|
if (!isDate(value))
|
|
|
|
raw = value.valueOf();
|
|
|
|
} catch (e) {
|
|
|
|
// ignore...
|
|
|
|
}
|
|
|
|
|
2015-01-29 01:05:53 +00:00
|
|
|
if (typeof raw === 'string') {
|
2014-02-06 02:09:23 +00:00
|
|
|
// for boxed Strings, we have to remove the 0-n indexed entries,
|
2016-02-17 11:40:55 +00:00
|
|
|
// since they just noisy up the output and are redundant
|
2014-02-06 02:09:23 +00:00
|
|
|
keys = keys.filter(function(key) {
|
2017-03-03 12:43:16 +00:00
|
|
|
if (typeof key === 'symbol') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-06 02:09:23 +00:00
|
|
|
return !(key >= 0 && key < raw.length);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-19 20:17:29 +00:00
|
|
|
var constructor = getConstructorOf(value);
|
2017-02-07 09:46:12 +00:00
|
|
|
|
2011-09-08 12:00:02 +00:00
|
|
|
// Some type of object without properties can be shortcutted.
|
|
|
|
if (keys.length === 0) {
|
2015-01-29 01:05:53 +00:00
|
|
|
if (typeof value === 'function') {
|
2017-02-07 09:46:12 +00:00
|
|
|
const ctorName = constructor ? constructor.name : 'Function';
|
|
|
|
return ctx.stylize(
|
2017-07-05 16:21:40 +00:00
|
|
|
`[${ctorName}${value.name ? `: ${value.name}` : ''}]`, 'special');
|
2010-10-11 21:04:09 +00:00
|
|
|
}
|
2011-07-03 06:20:25 +00:00
|
|
|
if (isRegExp(value)) {
|
2017-06-19 20:17:29 +00:00
|
|
|
return ctx.stylize(regExpToString.call(value), 'regexp');
|
2011-07-03 06:20:25 +00:00
|
|
|
}
|
2010-10-11 21:04:09 +00:00
|
|
|
if (isDate(value)) {
|
2016-05-01 15:44:46 +00:00
|
|
|
if (Number.isNaN(value.getTime())) {
|
|
|
|
return ctx.stylize(value.toString(), 'date');
|
|
|
|
} else {
|
2017-06-19 20:17:29 +00:00
|
|
|
return ctx.stylize(dateToISOString.call(value), 'date');
|
2016-05-01 15:44:46 +00:00
|
|
|
}
|
2010-10-11 21:04:09 +00:00
|
|
|
}
|
2011-09-08 16:16:48 +00:00
|
|
|
if (isError(value)) {
|
|
|
|
return formatError(value);
|
|
|
|
}
|
2014-02-06 02:09:23 +00:00
|
|
|
// now check the `raw` value to handle boxed primitives
|
2015-01-29 01:05:53 +00:00
|
|
|
if (typeof raw === 'string') {
|
2014-02-06 02:09:23 +00:00
|
|
|
formatted = formatPrimitiveNoColor(ctx, raw);
|
2016-10-17 17:16:04 +00:00
|
|
|
return ctx.stylize(`[String: ${formatted}]`, 'string');
|
2014-02-06 02:09:23 +00:00
|
|
|
}
|
2016-07-09 20:27:38 +00:00
|
|
|
if (typeof raw === 'symbol') {
|
|
|
|
formatted = formatPrimitiveNoColor(ctx, raw);
|
2016-10-17 17:16:04 +00:00
|
|
|
return ctx.stylize(`[Symbol: ${formatted}]`, 'symbol');
|
2016-07-09 20:27:38 +00:00
|
|
|
}
|
2015-01-29 01:05:53 +00:00
|
|
|
if (typeof raw === 'number') {
|
2014-02-06 02:09:23 +00:00
|
|
|
formatted = formatPrimitiveNoColor(ctx, raw);
|
2016-10-17 17:16:04 +00:00
|
|
|
return ctx.stylize(`[Number: ${formatted}]`, 'number');
|
2014-02-06 02:09:23 +00:00
|
|
|
}
|
2015-01-29 01:05:53 +00:00
|
|
|
if (typeof raw === 'boolean') {
|
2014-02-06 02:09:23 +00:00
|
|
|
formatted = formatPrimitiveNoColor(ctx, raw);
|
2016-10-17 17:16:04 +00:00
|
|
|
return ctx.stylize(`[Boolean: ${formatted}]`, 'boolean');
|
2014-02-06 02:09:23 +00:00
|
|
|
}
|
2016-09-17 09:01:39 +00:00
|
|
|
// Fast path for ArrayBuffer and SharedArrayBuffer.
|
|
|
|
// Can't do the same for DataView because it has a non-primitive
|
|
|
|
// .buffer property that we need to recurse for.
|
2017-06-19 20:17:29 +00:00
|
|
|
if (isAnyArrayBuffer(value)) {
|
2017-02-07 09:46:12 +00:00
|
|
|
return `${constructor.name}` +
|
2015-11-12 12:28:15 +00:00
|
|
|
` { byteLength: ${formatNumber(ctx, value.byteLength)} }`;
|
|
|
|
}
|
2017-06-19 20:17:29 +00:00
|
|
|
if (isExternal(value)) {
|
2017-03-31 20:35:54 +00:00
|
|
|
return ctx.stylize('[External]', 'special');
|
|
|
|
}
|
2011-09-08 12:00:02 +00:00
|
|
|
}
|
2010-10-11 21:04:09 +00:00
|
|
|
|
2017-02-19 12:45:30 +00:00
|
|
|
var base = '';
|
|
|
|
var empty = false;
|
2015-11-12 12:28:15 +00:00
|
|
|
var formatter = formatObject;
|
2017-02-19 12:45:30 +00:00
|
|
|
var braces;
|
2010-10-11 21:04:09 +00:00
|
|
|
|
2015-10-15 19:55:42 +00:00
|
|
|
// We can't compare constructors for various objects using a comparison like
|
|
|
|
// `constructor === Array` because the object could have come from a different
|
|
|
|
// context and thus the constructor won't match. Instead we check the
|
|
|
|
// constructor names (including those up the prototype chain where needed) to
|
|
|
|
// determine object types.
|
2015-01-29 01:05:53 +00:00
|
|
|
if (Array.isArray(value)) {
|
2015-10-15 19:55:42 +00:00
|
|
|
// Unset the constructor to prevent "Array [...]" for ordinary arrays.
|
2015-10-05 21:29:27 +00:00
|
|
|
if (constructor && constructor.name === 'Array')
|
2015-06-10 08:25:04 +00:00
|
|
|
constructor = null;
|
2011-09-08 12:00:02 +00:00
|
|
|
braces = ['[', ']'];
|
2015-04-20 00:29:59 +00:00
|
|
|
empty = value.length === 0;
|
|
|
|
formatter = formatArray;
|
2017-06-19 20:17:29 +00:00
|
|
|
} else if (isSet(value)) {
|
2015-06-10 08:25:04 +00:00
|
|
|
braces = ['{', '}'];
|
2015-04-20 00:29:59 +00:00
|
|
|
// With `showHidden`, `length` will display as a hidden property for
|
|
|
|
// arrays. For consistency's sake, do the same for `size`, even though this
|
|
|
|
// property isn't selected by Object.getOwnPropertyNames().
|
|
|
|
if (ctx.showHidden)
|
|
|
|
keys.unshift('size');
|
|
|
|
empty = value.size === 0;
|
|
|
|
formatter = formatSet;
|
2017-06-19 20:17:29 +00:00
|
|
|
} else if (isMap(value)) {
|
2015-06-10 08:25:04 +00:00
|
|
|
braces = ['{', '}'];
|
2015-04-20 00:29:59 +00:00
|
|
|
// Ditto.
|
|
|
|
if (ctx.showHidden)
|
|
|
|
keys.unshift('size');
|
|
|
|
empty = value.size === 0;
|
|
|
|
formatter = formatMap;
|
2017-06-19 20:17:29 +00:00
|
|
|
} else if (isAnyArrayBuffer(value)) {
|
2015-11-12 12:28:15 +00:00
|
|
|
braces = ['{', '}'];
|
|
|
|
keys.unshift('byteLength');
|
|
|
|
visibleKeys.byteLength = true;
|
2017-06-19 20:17:29 +00:00
|
|
|
} else if (isDataView(value)) {
|
2015-11-12 12:28:15 +00:00
|
|
|
braces = ['{', '}'];
|
|
|
|
// .buffer goes last, it's not a primitive like the others.
|
|
|
|
keys.unshift('byteLength', 'byteOffset', 'buffer');
|
|
|
|
visibleKeys.byteLength = true;
|
|
|
|
visibleKeys.byteOffset = true;
|
|
|
|
visibleKeys.buffer = true;
|
2017-06-19 20:17:29 +00:00
|
|
|
} else if (isTypedArray(value)) {
|
2015-11-12 12:28:15 +00:00
|
|
|
braces = ['[', ']'];
|
|
|
|
formatter = formatTypedArray;
|
|
|
|
if (ctx.showHidden) {
|
|
|
|
// .buffer goes last, it's not a primitive like the others.
|
|
|
|
keys.unshift('BYTES_PER_ELEMENT',
|
|
|
|
'length',
|
|
|
|
'byteLength',
|
|
|
|
'byteOffset',
|
|
|
|
'buffer');
|
|
|
|
}
|
2017-06-19 20:17:29 +00:00
|
|
|
} else if (isPromise(value)) {
|
2017-04-06 05:30:25 +00:00
|
|
|
braces = ['{', '}'];
|
|
|
|
formatter = formatPromise;
|
2017-06-19 20:17:29 +00:00
|
|
|
} else if (isMapIterator(value)) {
|
2017-04-06 05:30:25 +00:00
|
|
|
constructor = { name: 'MapIterator' };
|
|
|
|
braces = ['{', '}'];
|
|
|
|
empty = false;
|
|
|
|
formatter = formatCollectionIterator;
|
2017-06-19 20:17:29 +00:00
|
|
|
} else if (isSetIterator(value)) {
|
2017-04-06 05:30:25 +00:00
|
|
|
constructor = { name: 'SetIterator' };
|
|
|
|
braces = ['{', '}'];
|
|
|
|
empty = false;
|
|
|
|
formatter = formatCollectionIterator;
|
2015-04-20 00:29:59 +00:00
|
|
|
} else {
|
2017-04-06 05:30:25 +00:00
|
|
|
// Unset the constructor to prevent "Object {...}" for ordinary objects.
|
|
|
|
if (constructor && constructor.name === 'Object')
|
|
|
|
constructor = null;
|
|
|
|
braces = ['{', '}'];
|
|
|
|
empty = true; // No other data than keys.
|
2011-09-08 12:00:02 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 00:29:59 +00:00
|
|
|
empty = empty === true && keys.length === 0;
|
|
|
|
|
2011-09-08 12:00:02 +00:00
|
|
|
// Make functions say that they are functions
|
2015-01-29 01:05:53 +00:00
|
|
|
if (typeof value === 'function') {
|
2017-02-07 09:46:12 +00:00
|
|
|
const ctorName = constructor ? constructor.name : 'Function';
|
|
|
|
base = ` [${ctorName}${value.name ? `: ${value.name}` : ''}]`;
|
2011-09-08 12:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make RegExps say that they are RegExps
|
|
|
|
if (isRegExp(value)) {
|
2017-06-19 20:17:29 +00:00
|
|
|
base = ` ${regExpToString.call(value)}`;
|
2011-09-08 12:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make dates with properties first say the date
|
|
|
|
if (isDate(value)) {
|
2017-06-19 20:17:29 +00:00
|
|
|
base = ` ${dateToISOString.call(value)}`;
|
2011-09-08 12:00:02 +00:00
|
|
|
}
|
|
|
|
|
2011-09-08 16:16:48 +00:00
|
|
|
// Make error with message first say the error
|
|
|
|
if (isError(value)) {
|
2017-06-19 20:17:29 +00:00
|
|
|
base = ` ${formatError(value)}`;
|
2011-09-08 16:16:48 +00:00
|
|
|
}
|
|
|
|
|
2014-02-06 02:09:23 +00:00
|
|
|
// Make boxed primitive Strings look like such
|
2015-01-29 01:05:53 +00:00
|
|
|
if (typeof raw === 'string') {
|
2014-02-06 02:09:23 +00:00
|
|
|
formatted = formatPrimitiveNoColor(ctx, raw);
|
2016-10-17 17:16:04 +00:00
|
|
|
base = ` [String: ${formatted}]`;
|
2014-02-06 02:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make boxed primitive Numbers look like such
|
2015-01-29 01:05:53 +00:00
|
|
|
if (typeof raw === 'number') {
|
2014-02-06 02:09:23 +00:00
|
|
|
formatted = formatPrimitiveNoColor(ctx, raw);
|
2016-10-17 17:16:04 +00:00
|
|
|
base = ` [Number: ${formatted}]`;
|
2014-02-06 02:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make boxed primitive Booleans look like such
|
2015-01-29 01:05:53 +00:00
|
|
|
if (typeof raw === 'boolean') {
|
2014-02-06 02:09:23 +00:00
|
|
|
formatted = formatPrimitiveNoColor(ctx, raw);
|
2016-10-17 17:16:04 +00:00
|
|
|
base = ` [Boolean: ${formatted}]`;
|
2014-02-06 02:09:23 +00:00
|
|
|
}
|
|
|
|
|
2015-06-10 08:25:04 +00:00
|
|
|
// Add constructor name if available
|
|
|
|
if (base === '' && constructor)
|
2016-10-17 17:16:04 +00:00
|
|
|
braces[0] = `${constructor.name} ${braces[0]}`;
|
2015-06-10 08:25:04 +00:00
|
|
|
|
2015-04-20 00:29:59 +00:00
|
|
|
if (empty === true) {
|
2017-06-19 20:17:29 +00:00
|
|
|
return `${braces[0]}${base}${braces[1]}`;
|
2011-09-08 12:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (recurseTimes < 0) {
|
|
|
|
if (isRegExp(value)) {
|
2017-06-19 20:17:29 +00:00
|
|
|
return ctx.stylize(regExpToString.call(value), 'regexp');
|
2017-03-26 00:28:23 +00:00
|
|
|
} else if (Array.isArray(value)) {
|
|
|
|
return ctx.stylize('[Array]', 'special');
|
2011-09-08 12:00:02 +00:00
|
|
|
} else {
|
|
|
|
return ctx.stylize('[Object]', 'special');
|
2010-10-11 21:04:09 +00:00
|
|
|
}
|
2011-09-08 12:00:02 +00:00
|
|
|
}
|
2010-10-11 21:04:09 +00:00
|
|
|
|
2017-08-12 12:37:12 +00:00
|
|
|
// TODO(addaleax): Make `seen` a Set to avoid linear-time lookup.
|
|
|
|
if (ctx.seen.includes(value)) {
|
|
|
|
return ctx.stylize('[Circular]', 'special');
|
|
|
|
}
|
2011-09-08 12:00:02 +00:00
|
|
|
|
2017-08-12 12:37:12 +00:00
|
|
|
ctx.seen.push(value);
|
|
|
|
const output = formatter(ctx, value, recurseTimes, visibleKeys, keys);
|
2011-09-08 12:00:02 +00:00
|
|
|
ctx.seen.pop();
|
|
|
|
|
2016-06-30 16:03:02 +00:00
|
|
|
return reduceToSingleString(output, base, braces, ctx.breakLength);
|
2011-09-08 12:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-12 12:28:15 +00:00
|
|
|
function formatNumber(ctx, value) {
|
2017-02-13 08:15:26 +00:00
|
|
|
// Format -0 as '-0'. Strict equality won't distinguish 0 from -0.
|
|
|
|
if (Object.is(value, -0))
|
2015-11-12 12:28:15 +00:00
|
|
|
return ctx.stylize('-0', 'number');
|
2017-06-19 20:17:29 +00:00
|
|
|
return ctx.stylize(`${value}`, 'number');
|
2015-11-12 12:28:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-08 12:00:02 +00:00
|
|
|
function formatPrimitive(ctx, value) {
|
2015-01-29 01:05:53 +00:00
|
|
|
if (value === undefined)
|
2013-07-24 16:03:53 +00:00
|
|
|
return ctx.stylize('undefined', 'undefined');
|
2015-01-29 01:05:53 +00:00
|
|
|
|
|
|
|
// For some reason typeof null is "object", so special case here.
|
|
|
|
if (value === null)
|
|
|
|
return ctx.stylize('null', 'null');
|
|
|
|
|
|
|
|
var type = typeof value;
|
|
|
|
|
|
|
|
if (type === 'string') {
|
2017-06-19 20:17:29 +00:00
|
|
|
var simple = JSON.stringify(value)
|
2016-04-16 04:21:12 +00:00
|
|
|
.replace(/^"|"$/g, '')
|
|
|
|
.replace(/'/g, "\\'")
|
2017-06-19 20:17:29 +00:00
|
|
|
.replace(/\\"/g, '"');
|
|
|
|
return ctx.stylize(`'${simple}'`, 'string');
|
2013-07-24 16:03:53 +00:00
|
|
|
}
|
2015-11-12 12:28:15 +00:00
|
|
|
if (type === 'number')
|
|
|
|
return formatNumber(ctx, value);
|
2015-01-29 01:05:53 +00:00
|
|
|
if (type === 'boolean')
|
2017-06-19 20:17:29 +00:00
|
|
|
return ctx.stylize(`${value}`, 'boolean');
|
2014-10-01 02:44:39 +00:00
|
|
|
// es6 symbol primitive
|
2015-01-29 01:05:53 +00:00
|
|
|
if (type === 'symbol')
|
2014-10-01 02:44:39 +00:00
|
|
|
return ctx.stylize(value.toString(), 'symbol');
|
2011-09-08 12:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-06 02:09:23 +00:00
|
|
|
function formatPrimitiveNoColor(ctx, value) {
|
|
|
|
var stylize = ctx.stylize;
|
|
|
|
ctx.stylize = stylizeNoColor;
|
|
|
|
var str = formatPrimitive(ctx, value);
|
|
|
|
ctx.stylize = stylize;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-08 16:16:48 +00:00
|
|
|
function formatError(value) {
|
2017-06-19 20:17:29 +00:00
|
|
|
return value.stack || `[${errorToString.call(value)}]`;
|
2011-09-08 16:16:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-20 00:29:59 +00:00
|
|
|
function formatObject(ctx, value, recurseTimes, visibleKeys, keys) {
|
|
|
|
return keys.map(function(key) {
|
|
|
|
return formatProperty(ctx, value, recurseTimes, visibleKeys, key, false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-08 15:20:01 +00:00
|
|
|
function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
|
2017-08-17 00:21:29 +00:00
|
|
|
const maxLength = Math.min(Math.max(0, ctx.maxArrayLength), value.length);
|
2011-09-08 15:20:01 +00:00
|
|
|
var output = [];
|
2017-02-27 07:20:27 +00:00
|
|
|
let visibleLength = 0;
|
|
|
|
let index = 0;
|
2017-07-26 02:21:12 +00:00
|
|
|
for (const elem of keys) {
|
2017-08-17 00:21:29 +00:00
|
|
|
if (visibleLength === maxLength)
|
2017-07-26 02:21:12 +00:00
|
|
|
break;
|
|
|
|
// Symbols might have been added to the keys
|
|
|
|
if (typeof elem !== 'string')
|
|
|
|
continue;
|
|
|
|
const i = +elem;
|
|
|
|
if (index !== i) {
|
|
|
|
// Skip zero and negative numbers as well as non numbers
|
|
|
|
if (i > 0 === false)
|
|
|
|
continue;
|
|
|
|
const emptyItems = i - index;
|
2017-02-27 07:20:27 +00:00
|
|
|
const ending = emptyItems > 1 ? 's' : '';
|
|
|
|
const message = `<${emptyItems} empty item${ending}>`;
|
|
|
|
output.push(ctx.stylize(message, 'undefined'));
|
2017-07-26 02:21:12 +00:00
|
|
|
index = i;
|
2017-08-17 00:21:29 +00:00
|
|
|
if (++visibleLength === maxLength)
|
2017-07-26 02:21:12 +00:00
|
|
|
break;
|
2011-09-08 15:20:01 +00:00
|
|
|
}
|
2017-07-26 02:21:12 +00:00
|
|
|
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
|
|
|
|
elem, true));
|
2017-02-27 07:20:27 +00:00
|
|
|
visibleLength++;
|
2017-07-26 02:21:12 +00:00
|
|
|
index++;
|
|
|
|
}
|
2017-08-17 00:21:29 +00:00
|
|
|
if (index < value.length && visibleLength !== maxLength) {
|
2017-07-26 02:21:12 +00:00
|
|
|
const len = value.length - index;
|
|
|
|
const ending = len > 1 ? 's' : '';
|
|
|
|
const message = `<${len} empty item${ending}>`;
|
|
|
|
output.push(ctx.stylize(message, 'undefined'));
|
|
|
|
index = value.length;
|
2011-09-08 15:20:01 +00:00
|
|
|
}
|
2017-04-24 06:21:20 +00:00
|
|
|
var remaining = value.length - index;
|
2016-04-21 17:29:24 +00:00
|
|
|
if (remaining > 0) {
|
|
|
|
output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
|
|
|
|
}
|
2017-02-27 17:43:40 +00:00
|
|
|
for (var n = 0; n < keys.length; n++) {
|
|
|
|
var key = keys[n];
|
2017-06-07 23:00:33 +00:00
|
|
|
if (typeof key === 'symbol' || !numbersOnlyRE.test(key)) {
|
2011-09-08 15:20:01 +00:00
|
|
|
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
|
2017-01-01 05:39:57 +00:00
|
|
|
key, true));
|
2011-09-08 15:20:01 +00:00
|
|
|
}
|
2017-02-27 17:43:40 +00:00
|
|
|
}
|
2011-09-08 15:20:01 +00:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-12 12:28:15 +00:00
|
|
|
function formatTypedArray(ctx, value, recurseTimes, visibleKeys, keys) {
|
2016-04-21 17:29:24 +00:00
|
|
|
const maxLength = Math.min(Math.max(0, ctx.maxArrayLength), value.length);
|
|
|
|
const remaining = value.length - maxLength;
|
|
|
|
var output = new Array(maxLength);
|
|
|
|
for (var i = 0; i < maxLength; ++i)
|
2015-11-12 12:28:15 +00:00
|
|
|
output[i] = formatNumber(ctx, value[i]);
|
2016-04-21 17:29:24 +00:00
|
|
|
if (remaining > 0) {
|
|
|
|
output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
|
|
|
|
}
|
2015-11-12 12:28:15 +00:00
|
|
|
for (const key of keys) {
|
2017-06-07 23:00:33 +00:00
|
|
|
if (typeof key === 'symbol' || !numbersOnlyRE.test(key)) {
|
2015-11-12 12:28:15 +00:00
|
|
|
output.push(
|
2017-07-05 16:21:40 +00:00
|
|
|
formatProperty(ctx, value, recurseTimes, visibleKeys, key, true));
|
2015-11-12 12:28:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-20 00:29:59 +00:00
|
|
|
function formatSet(ctx, value, recurseTimes, visibleKeys, keys) {
|
|
|
|
var output = [];
|
|
|
|
value.forEach(function(v) {
|
|
|
|
var nextRecurseTimes = recurseTimes === null ? null : recurseTimes - 1;
|
|
|
|
var str = formatValue(ctx, v, nextRecurseTimes);
|
|
|
|
output.push(str);
|
|
|
|
});
|
2017-02-27 17:43:40 +00:00
|
|
|
for (var n = 0; n < keys.length; n++) {
|
2015-04-20 00:29:59 +00:00
|
|
|
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
|
2017-02-27 17:43:40 +00:00
|
|
|
keys[n], false));
|
|
|
|
}
|
2015-04-20 00:29:59 +00:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function formatMap(ctx, value, recurseTimes, visibleKeys, keys) {
|
|
|
|
var output = [];
|
|
|
|
value.forEach(function(v, k) {
|
|
|
|
var nextRecurseTimes = recurseTimes === null ? null : recurseTimes - 1;
|
|
|
|
var str = formatValue(ctx, k, nextRecurseTimes);
|
|
|
|
str += ' => ';
|
|
|
|
str += formatValue(ctx, v, nextRecurseTimes);
|
|
|
|
output.push(str);
|
|
|
|
});
|
2017-02-27 17:43:40 +00:00
|
|
|
for (var n = 0; n < keys.length; n++) {
|
2015-04-20 00:29:59 +00:00
|
|
|
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
|
2017-02-27 17:43:40 +00:00
|
|
|
keys[n], false));
|
|
|
|
}
|
2015-04-20 00:29:59 +00:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2015-09-29 19:30:22 +00:00
|
|
|
function formatCollectionIterator(ctx, value, recurseTimes, visibleKeys, keys) {
|
|
|
|
ensureDebugIsInitialized();
|
|
|
|
const mirror = Debug.MakeMirror(value, true);
|
|
|
|
var nextRecurseTimes = recurseTimes === null ? null : recurseTimes - 1;
|
|
|
|
var vals = mirror.preview();
|
|
|
|
var output = [];
|
2015-10-08 17:41:29 +00:00
|
|
|
for (const o of vals) {
|
2015-09-29 19:30:22 +00:00
|
|
|
output.push(formatValue(ctx, o, nextRecurseTimes));
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2015-04-20 00:29:59 +00:00
|
|
|
function formatPromise(ctx, value, recurseTimes, visibleKeys, keys) {
|
2017-04-06 05:30:25 +00:00
|
|
|
const output = [];
|
2017-06-19 20:17:29 +00:00
|
|
|
const [state, result] = getPromiseDetails(value);
|
2017-04-06 05:30:25 +00:00
|
|
|
|
2017-06-19 20:17:29 +00:00
|
|
|
if (state === kPending) {
|
2015-04-20 00:29:59 +00:00
|
|
|
output.push('<pending>');
|
|
|
|
} else {
|
|
|
|
var nextRecurseTimes = recurseTimes === null ? null : recurseTimes - 1;
|
2017-04-06 05:30:25 +00:00
|
|
|
var str = formatValue(ctx, result, nextRecurseTimes);
|
2017-06-19 20:17:29 +00:00
|
|
|
if (state === kRejected) {
|
|
|
|
output.push(`<rejected> ${str}`);
|
2015-04-20 00:29:59 +00:00
|
|
|
} else {
|
|
|
|
output.push(str);
|
|
|
|
}
|
|
|
|
}
|
2017-02-27 17:43:40 +00:00
|
|
|
for (var n = 0; n < keys.length; n++) {
|
2015-04-20 00:29:59 +00:00
|
|
|
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
|
2017-02-27 17:43:40 +00:00
|
|
|
keys[n], false));
|
|
|
|
}
|
2015-04-20 00:29:59 +00:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-08 12:00:02 +00:00
|
|
|
function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
|
2011-11-14 20:42:14 +00:00
|
|
|
var name, str, desc;
|
|
|
|
desc = Object.getOwnPropertyDescriptor(value, key) || { value: value[key] };
|
|
|
|
if (desc.get) {
|
|
|
|
if (desc.set) {
|
|
|
|
str = ctx.stylize('[Getter/Setter]', 'special');
|
2011-09-08 12:00:02 +00:00
|
|
|
} else {
|
2011-11-14 20:42:14 +00:00
|
|
|
str = ctx.stylize('[Getter]', 'special');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (desc.set) {
|
|
|
|
str = ctx.stylize('[Setter]', 'special');
|
2011-09-08 12:00:02 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-26 02:21:12 +00:00
|
|
|
if (visibleKeys[key] === undefined) {
|
2015-01-29 01:05:53 +00:00
|
|
|
if (typeof key === 'symbol') {
|
2016-10-17 17:16:04 +00:00
|
|
|
name = `[${ctx.stylize(key.toString(), 'symbol')}]`;
|
2015-01-07 21:54:25 +00:00
|
|
|
} else {
|
2016-10-17 17:16:04 +00:00
|
|
|
name = `[${key}]`;
|
2015-01-07 21:54:25 +00:00
|
|
|
}
|
2011-09-08 12:00:02 +00:00
|
|
|
}
|
|
|
|
if (!str) {
|
2017-08-12 12:37:12 +00:00
|
|
|
if (recurseTimes === null) {
|
|
|
|
str = formatValue(ctx, desc.value, null);
|
|
|
|
} else {
|
|
|
|
str = formatValue(ctx, desc.value, recurseTimes - 1);
|
|
|
|
}
|
|
|
|
if (str.indexOf('\n') > -1) {
|
|
|
|
if (array) {
|
|
|
|
str = str.replace(/\n/g, '\n ');
|
2011-09-08 12:00:02 +00:00
|
|
|
} else {
|
2017-08-12 12:37:12 +00:00
|
|
|
str = str.replace(/^|\n/g, '\n ');
|
2010-10-11 21:04:09 +00:00
|
|
|
}
|
|
|
|
}
|
2011-09-08 12:00:02 +00:00
|
|
|
}
|
2015-01-29 01:05:53 +00:00
|
|
|
if (name === undefined) {
|
2017-06-07 23:00:33 +00:00
|
|
|
if (array && numbersOnlyRE.test(key)) {
|
2011-09-08 12:00:02 +00:00
|
|
|
return str;
|
|
|
|
}
|
2017-06-19 20:17:29 +00:00
|
|
|
name = JSON.stringify(`${key}`);
|
2017-06-16 17:14:58 +00:00
|
|
|
if (/^"[a-zA-Z_][a-zA-Z_0-9]*"$/.test(name)) {
|
2011-09-08 12:00:02 +00:00
|
|
|
name = name.substr(1, name.length - 2);
|
|
|
|
name = ctx.stylize(name, 'name');
|
|
|
|
} else {
|
|
|
|
name = name.replace(/'/g, "\\'")
|
|
|
|
.replace(/\\"/g, '"')
|
2017-06-16 17:14:58 +00:00
|
|
|
.replace(/^"|"$/g, "'")
|
2014-01-10 19:53:47 +00:00
|
|
|
.replace(/\\\\/g, '\\');
|
2011-09-08 12:00:02 +00:00
|
|
|
name = ctx.stylize(name, 'string');
|
|
|
|
}
|
|
|
|
}
|
2010-10-11 21:04:09 +00:00
|
|
|
|
2016-10-17 17:16:04 +00:00
|
|
|
return `${name}: ${str}`;
|
2011-09-08 12:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-30 16:03:02 +00:00
|
|
|
function reduceToSingleString(output, base, braces, breakLength) {
|
2011-09-08 12:00:02 +00:00
|
|
|
var length = output.reduce(function(prev, cur) {
|
2013-03-18 00:44:43 +00:00
|
|
|
return prev + cur.replace(/\u001b\[\d\d?m/g, '').length + 1;
|
2011-09-08 12:00:02 +00:00
|
|
|
}, 0);
|
|
|
|
|
2016-06-30 16:03:02 +00:00
|
|
|
if (length > breakLength) {
|
2011-09-08 12:00:02 +00:00
|
|
|
return braces[0] +
|
2015-04-20 00:29:59 +00:00
|
|
|
// If the opening "brace" is too large, like in the case of "Set {",
|
|
|
|
// we need to force the first item to be on the next line or the
|
|
|
|
// items will not line up correctly.
|
2017-06-19 20:17:29 +00:00
|
|
|
(base === '' && braces[0].length === 1 ? '' : `${base}\n `) +
|
2016-10-17 17:16:04 +00:00
|
|
|
` ${output.join(',\n ')} ${braces[1]}`;
|
2010-10-11 21:04:09 +00:00
|
|
|
}
|
2011-09-08 12:00:02 +00:00
|
|
|
|
2016-10-17 17:16:04 +00:00
|
|
|
return `${braces[0]}${base} ${output.join(', ')} ${braces[1]}`;
|
2011-07-29 15:26:45 +00:00
|
|
|
}
|
2010-10-11 21:04:09 +00:00
|
|
|
|
2013-07-26 21:38:08 +00:00
|
|
|
function isBoolean(arg) {
|
2013-12-20 21:44:56 +00:00
|
|
|
return typeof arg === 'boolean';
|
2013-07-26 21:38:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function isNull(arg) {
|
|
|
|
return arg === null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isNullOrUndefined(arg) {
|
2015-01-29 01:05:53 +00:00
|
|
|
return arg === null || arg === undefined;
|
2013-07-26 21:38:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function isNumber(arg) {
|
2013-12-20 21:44:56 +00:00
|
|
|
return typeof arg === 'number';
|
2013-07-26 21:38:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function isString(arg) {
|
2013-12-20 21:44:56 +00:00
|
|
|
return typeof arg === 'string';
|
2013-07-26 21:38:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function isSymbol(arg) {
|
|
|
|
return typeof arg === 'symbol';
|
|
|
|
}
|
|
|
|
|
|
|
|
function isUndefined(arg) {
|
2015-01-29 01:05:53 +00:00
|
|
|
return arg === undefined;
|
2013-07-26 21:38:08 +00:00
|
|
|
}
|
2010-10-11 21:04:09 +00:00
|
|
|
|
2010-12-02 04:59:06 +00:00
|
|
|
function isRegExp(re) {
|
2017-06-19 20:17:29 +00:00
|
|
|
return _isRegExp(re);
|
2010-10-11 21:04:09 +00:00
|
|
|
}
|
|
|
|
|
2013-07-26 21:38:08 +00:00
|
|
|
function isObject(arg) {
|
2015-01-29 01:05:53 +00:00
|
|
|
return arg !== null && typeof arg === 'object';
|
2013-07-26 21:38:08 +00:00
|
|
|
}
|
2010-10-11 21:04:09 +00:00
|
|
|
|
2010-12-02 04:59:06 +00:00
|
|
|
function isDate(d) {
|
2017-06-19 20:17:29 +00:00
|
|
|
return _isDate(d);
|
2011-09-08 16:16:48 +00:00
|
|
|
}
|
|
|
|
|
2013-07-26 21:38:08 +00:00
|
|
|
function isFunction(arg) {
|
|
|
|
return typeof arg === 'function';
|
|
|
|
}
|
|
|
|
|
2013-08-02 19:52:34 +00:00
|
|
|
function isPrimitive(arg) {
|
2013-08-12 20:41:51 +00:00
|
|
|
return arg === null ||
|
2015-02-06 13:06:07 +00:00
|
|
|
typeof arg !== 'object' && typeof arg !== 'function';
|
2013-08-02 19:52:34 +00:00
|
|
|
}
|
2011-09-08 16:16:48 +00:00
|
|
|
|
2010-12-02 04:59:06 +00:00
|
|
|
function pad(n) {
|
2017-06-19 20:17:29 +00:00
|
|
|
return n < 10 ? `0${n.toString(10)}` : n.toString(10);
|
2010-10-11 21:04:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-21 16:36:59 +00:00
|
|
|
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
|
|
|
|
'Oct', 'Nov', 'Dec'];
|
2010-10-11 21:04:09 +00:00
|
|
|
|
|
|
|
// 26 Feb 16:19:34
|
2010-12-02 04:59:06 +00:00
|
|
|
function timestamp() {
|
2010-10-11 21:04:09 +00:00
|
|
|
var d = new Date();
|
2010-12-02 20:11:23 +00:00
|
|
|
var time = [pad(d.getHours()),
|
|
|
|
pad(d.getMinutes()),
|
|
|
|
pad(d.getSeconds())].join(':');
|
|
|
|
return [d.getDate(), months[d.getMonth()], time].join(' ');
|
2010-10-11 21:04:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-10 01:19:02 +00:00
|
|
|
// log is just a thin wrapper to console.log that prepends a timestamp
|
2017-06-19 20:17:29 +00:00
|
|
|
function log() {
|
2013-05-10 01:19:02 +00:00
|
|
|
console.log('%s - %s', timestamp(), exports.format.apply(exports, arguments));
|
2017-06-19 20:17:29 +00:00
|
|
|
}
|
2010-10-11 21:04:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inherit the prototype methods from one constructor into another.
|
|
|
|
*
|
|
|
|
* The Function.prototype.inherits from lang.js rewritten as a standalone
|
|
|
|
* function (not on Function.prototype). NOTE: If this file is to be loaded
|
2012-02-18 05:28:13 +00:00
|
|
|
* during bootstrapping this function needs to be rewritten using some native
|
2010-10-11 21:04:09 +00:00
|
|
|
* functions as prototype setup using normal JavaScript does not work as
|
|
|
|
* expected during bootstrapping (see mirror.js in r114903).
|
|
|
|
*
|
|
|
|
* @param {function} ctor Constructor function which needs to inherit the
|
2010-12-02 04:59:06 +00:00
|
|
|
* prototype.
|
|
|
|
* @param {function} superCtor Constructor function to inherit prototype from.
|
2015-03-23 00:45:16 +00:00
|
|
|
* @throws {TypeError} Will error if either constructor is null, or if
|
|
|
|
* the super constructor lacks a prototype.
|
2010-10-11 21:04:09 +00:00
|
|
|
*/
|
2017-06-19 20:17:29 +00:00
|
|
|
function inherits(ctor, superCtor) {
|
2015-03-23 00:45:16 +00:00
|
|
|
|
|
|
|
if (ctor === undefined || ctor === null)
|
2017-05-30 11:17:09 +00:00
|
|
|
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'ctor', 'function');
|
2015-03-23 00:45:16 +00:00
|
|
|
|
|
|
|
if (superCtor === undefined || superCtor === null)
|
2017-05-30 11:17:09 +00:00
|
|
|
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'superCtor', 'function');
|
2015-03-23 00:45:16 +00:00
|
|
|
|
2017-06-20 21:37:00 +00:00
|
|
|
if (superCtor.prototype === undefined) {
|
2017-05-30 11:17:09 +00:00
|
|
|
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'superCtor.prototype',
|
|
|
|
'function');
|
2017-06-20 21:37:00 +00:00
|
|
|
}
|
2010-12-02 20:11:23 +00:00
|
|
|
ctor.super_ = superCtor;
|
2015-10-20 19:39:49 +00:00
|
|
|
Object.setPrototypeOf(ctor.prototype, superCtor.prototype);
|
2017-06-19 20:17:29 +00:00
|
|
|
}
|
2012-02-20 17:59:56 +00:00
|
|
|
|
2017-06-19 20:17:29 +00:00
|
|
|
function _extend(target, source) {
|
2016-08-20 02:53:28 +00:00
|
|
|
// Don't do anything if source isn't an object
|
|
|
|
if (source === null || typeof source !== 'object') return target;
|
2012-02-20 17:59:56 +00:00
|
|
|
|
2016-08-20 02:53:28 +00:00
|
|
|
var keys = Object.keys(source);
|
2012-02-20 17:59:56 +00:00
|
|
|
var i = keys.length;
|
|
|
|
while (i--) {
|
2016-08-20 02:53:28 +00:00
|
|
|
target[keys[i]] = source[keys[i]];
|
2012-02-20 17:59:56 +00:00
|
|
|
}
|
2016-08-20 02:53:28 +00:00
|
|
|
return target;
|
2017-06-19 20:17:29 +00:00
|
|
|
}
|
2012-09-08 22:09:59 +00:00
|
|
|
|
2013-05-21 22:22:05 +00:00
|
|
|
// Deprecated old stuff.
|
|
|
|
|
2017-06-19 20:17:29 +00:00
|
|
|
function print(...args) {
|
|
|
|
for (var i = 0, len = args.length; i < len; ++i) {
|
|
|
|
process.stdout.write(String(args[i]));
|
2013-05-21 22:22:05 +00:00
|
|
|
}
|
2017-06-19 20:17:29 +00:00
|
|
|
}
|
2013-05-21 22:22:05 +00:00
|
|
|
|
2017-06-19 20:17:29 +00:00
|
|
|
function puts(...args) {
|
|
|
|
for (var i = 0, len = args.length; i < len; ++i) {
|
|
|
|
process.stdout.write(`${args[i]}\n`);
|
2013-05-21 22:22:05 +00:00
|
|
|
}
|
2017-06-19 20:17:29 +00:00
|
|
|
}
|
2013-05-21 22:22:05 +00:00
|
|
|
|
2017-06-19 20:17:29 +00:00
|
|
|
function debug(x) {
|
2016-10-17 17:16:04 +00:00
|
|
|
process.stderr.write(`DEBUG: ${x}\n`);
|
2017-06-19 20:17:29 +00:00
|
|
|
}
|
2013-05-21 22:22:05 +00:00
|
|
|
|
2017-06-19 20:17:29 +00:00
|
|
|
function error(...args) {
|
|
|
|
for (var i = 0, len = args.length; i < len; ++i) {
|
|
|
|
process.stderr.write(`${args[i]}\n`);
|
2013-05-21 22:22:05 +00:00
|
|
|
}
|
2017-06-19 20:17:29 +00:00
|
|
|
}
|
2013-05-21 22:22:05 +00:00
|
|
|
|
2017-06-19 20:17:29 +00:00
|
|
|
function _errnoException(err, syscall, original) {
|
2017-08-18 22:37:35 +00:00
|
|
|
if (typeof err !== 'number' || err >= 0 || !Number.isSafeInteger(err)) {
|
|
|
|
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'err',
|
|
|
|
'negative number');
|
|
|
|
}
|
|
|
|
const name = errname(err);
|
2017-06-19 20:17:29 +00:00
|
|
|
var message = `${syscall} ${name}`;
|
2014-01-24 18:08:25 +00:00
|
|
|
if (original)
|
2017-06-19 20:17:29 +00:00
|
|
|
message += ` ${original}`;
|
2017-08-18 22:37:35 +00:00
|
|
|
const e = new Error(message);
|
|
|
|
e.code = e.errno = name;
|
2013-07-16 21:28:38 +00:00
|
|
|
e.syscall = syscall;
|
|
|
|
return e;
|
2017-06-19 20:17:29 +00:00
|
|
|
}
|
2015-01-08 00:03:24 +00:00
|
|
|
|
|
|
|
|
2017-06-19 20:17:29 +00:00
|
|
|
function _exceptionWithHostPort(err,
|
|
|
|
syscall,
|
|
|
|
address,
|
|
|
|
port,
|
|
|
|
additional) {
|
2015-01-08 00:03:24 +00:00
|
|
|
var details;
|
|
|
|
if (port && port > 0) {
|
2016-10-17 17:16:04 +00:00
|
|
|
details = `${address}:${port}`;
|
2015-01-08 00:03:24 +00:00
|
|
|
} else {
|
|
|
|
details = address;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (additional) {
|
2016-10-17 17:16:04 +00:00
|
|
|
details += ` - Local (${additional})`;
|
2015-01-08 00:03:24 +00:00
|
|
|
}
|
|
|
|
var ex = exports._errnoException(err, syscall, details);
|
|
|
|
ex.address = address;
|
|
|
|
if (port) {
|
|
|
|
ex.port = port;
|
|
|
|
}
|
|
|
|
return ex;
|
2017-06-19 20:17:29 +00:00
|
|
|
}
|
2016-10-24 18:24:21 +00:00
|
|
|
|
|
|
|
// process.versions needs a custom function as some values are lazy-evaluated.
|
2017-06-19 20:17:29 +00:00
|
|
|
process.versions[inspect.custom] =
|
2017-07-16 07:10:50 +00:00
|
|
|
() => exports.format(JSON.parse(JSON.stringify(process.versions)));
|
2017-04-14 16:28:16 +00:00
|
|
|
|
2017-04-28 01:57:12 +00:00
|
|
|
function callbackifyOnRejected(reason, cb) {
|
|
|
|
// `!reason` guard inspired by bluebird (Ref: https://goo.gl/t5IS6M).
|
|
|
|
// Because `null` is a special error value in callbacks which means "no error
|
|
|
|
// occurred", we error-wrap so the callback consumer can distinguish between
|
|
|
|
// "the promise rejected with null" or "the promise fulfilled with undefined".
|
|
|
|
if (!reason) {
|
2017-06-11 03:31:51 +00:00
|
|
|
const newReason = new errors.Error('ERR_FALSY_VALUE_REJECTION');
|
2017-04-28 01:57:12 +00:00
|
|
|
newReason.reason = reason;
|
|
|
|
reason = newReason;
|
|
|
|
Error.captureStackTrace(reason, callbackifyOnRejected);
|
|
|
|
}
|
|
|
|
return cb(reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function callbackify(original) {
|
|
|
|
if (typeof original !== 'function') {
|
|
|
|
throw new errors.TypeError(
|
|
|
|
'ERR_INVALID_ARG_TYPE',
|
|
|
|
'original',
|
|
|
|
'function');
|
|
|
|
}
|
|
|
|
|
|
|
|
// We DO NOT return the promise as it gives the user a false sense that
|
|
|
|
// the promise is actually somehow related to the callback's execution
|
|
|
|
// and that the callback throwing will reject the promise.
|
|
|
|
function callbackified(...args) {
|
|
|
|
const maybeCb = args.pop();
|
|
|
|
if (typeof maybeCb !== 'function') {
|
|
|
|
throw new errors.TypeError(
|
|
|
|
'ERR_INVALID_ARG_TYPE',
|
|
|
|
'last argument',
|
|
|
|
'function');
|
|
|
|
}
|
|
|
|
const cb = (...args) => { Reflect.apply(maybeCb, this, args); };
|
|
|
|
// In true node style we process the callback on `nextTick` with all the
|
|
|
|
// implications (stack, `uncaughtException`, `async_hooks`)
|
|
|
|
Reflect.apply(original, this, args)
|
|
|
|
.then((ret) => process.nextTick(cb, null, ret),
|
|
|
|
(rej) => process.nextTick(callbackifyOnRejected, rej, cb));
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.setPrototypeOf(callbackified, Object.getPrototypeOf(original));
|
|
|
|
Object.defineProperties(callbackified,
|
|
|
|
Object.getOwnPropertyDescriptors(original));
|
|
|
|
return callbackified;
|
|
|
|
}
|
|
|
|
|
2017-06-19 20:17:29 +00:00
|
|
|
// Keep the `exports =` so that various functions can still be monkeypatched
|
|
|
|
module.exports = exports = {
|
|
|
|
_errnoException,
|
|
|
|
_exceptionWithHostPort,
|
|
|
|
_extend,
|
|
|
|
callbackify,
|
|
|
|
debuglog,
|
|
|
|
deprecate,
|
|
|
|
format,
|
|
|
|
inherits,
|
|
|
|
inspect,
|
|
|
|
isArray: Array.isArray,
|
|
|
|
isBoolean,
|
|
|
|
isNull,
|
|
|
|
isNullOrUndefined,
|
|
|
|
isNumber,
|
|
|
|
isString,
|
|
|
|
isSymbol,
|
|
|
|
isUndefined,
|
|
|
|
isRegExp,
|
|
|
|
isObject,
|
|
|
|
isDate,
|
|
|
|
isError,
|
|
|
|
isFunction,
|
|
|
|
isPrimitive,
|
|
|
|
log,
|
|
|
|
promisify,
|
2017-06-12 15:25:53 +00:00
|
|
|
TextDecoder,
|
|
|
|
TextEncoder,
|
2017-06-19 20:17:29 +00:00
|
|
|
|
|
|
|
// Deprecated Old Stuff
|
|
|
|
debug: deprecate(debug,
|
|
|
|
'util.debug is deprecated. Use console.error instead.',
|
|
|
|
'DEP0028'),
|
|
|
|
error: deprecate(error,
|
|
|
|
'util.error is deprecated. Use console.error instead.',
|
|
|
|
'DEP0029'),
|
|
|
|
print: deprecate(print,
|
|
|
|
'util.print is deprecated. Use console.log instead.',
|
|
|
|
'DEP0026'),
|
|
|
|
puts: deprecate(puts,
|
|
|
|
'util.puts is deprecated. Use console.log instead.',
|
|
|
|
'DEP0027')
|
|
|
|
};
|
|
|
|
|
|
|
|
// Avoid a circular dependency
|
|
|
|
var isBuffer;
|
|
|
|
Object.defineProperty(exports, 'isBuffer', {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get() {
|
|
|
|
if (!isBuffer)
|
|
|
|
isBuffer = require('buffer').Buffer.isBuffer;
|
|
|
|
return isBuffer;
|
|
|
|
},
|
|
|
|
set(val) {
|
|
|
|
isBuffer = val;
|
|
|
|
}
|
|
|
|
});
|