//////////////////////////////////////////////////////// // Adds prototype inspection function addInspection(type, cb) { type.prototype[npm.util.inspect.custom] = cb; }
function inspect(obj) { return obj[util.inspect.custom](); }
[util.inspect.custom]() { // http://stackoverflow.com/questions/5905492/dynamic-function-name-in-javascript const copy = new {[this.constructor.name]: class {}}[this.constructor.name](); for(const key in this) { if(this.hasOwnProperty(key) && !key.startsWith("_") && this[key] !== undefined) { copy[key] = this[key]; } } return copy; }
[util.inspect.custom](depth, opts) { const type = `${this.type[0].toUpperCase()}${this.type.slice(1)}`; const name = this.name ? `<${this.name}>` : ''; const prefix = `${type}${name} `; return util.inspect(this.properties, opts) .replace(/^Map /, prefix); }
function createAgentProxy(domain, client) { const agent = new EventEmitter(); agent.then = (...args) => { // TODO: potentially fetch the protocol and pretty-print it here. const descriptor = { [util.inspect.custom](depth, { stylize }) { return stylize(`[Agent ${domain}]`, 'special'); }, }; return Promise.resolve(descriptor).then(...args); }; return new Proxy(agent, { get(target, name) { if (name in target) return target[name]; return function callVirtualMethod(params) { return client.callMethod(`${domain}.${name}`, params); }; }, }); }
[util.inspect.custom](depth, opts) { function formatProperty(prop) { switch (prop.type) {
[util.inspect.custom](depth, options) { const { scriptId, lineNumber, columnNumber, delta, scriptSource } = this; const start = Math.max(1, lineNumber - delta + 1);
[util.inspect.custom]() { return this.map((callFrame, idx) => { const { location: { scriptId, lineNumber, columnNumber }, functionName } = callFrame; const name = functionName || '(anonymous)'; const script = knownScripts[scriptId]; const relativeUrl = (script && getRelativePath(script.url)) || '<unknown>'; const frameLocation = `${relativeUrl}:${lineNumber + 1}:${columnNumber}`; return `#${idx} ${name} ${frameLocation}`; }).join('\n'); }
[util.inspect.custom] (depth, options) { if (this.id) return `Peer {id=${this.id}}` return `Peer {addr=${this.remoteAddress}:${this.remotePort}}` }
[util.inspect.custom] (depth, options) { return `Connection {${this.remoteAddress}:${this.remotePort} key=${this.key}}` }
/** * Gets called when `console.log(instance)` is invoked. * @example * const post = await Post.first * post.inspect() // => 'Post { "id": 1, ... }' * @returns {String} */ [util.inspect.custom]() { return this.constructor.name + ' ' + util.inspect(this.toJSON()); }
[util.inspect.custom](depth, { stylize }) { const { startTime, endTime } = this.data; return stylize(`[Profile ${endTime - startTime}μs]`, 'special'); }