UNPKG

peek42

Version:

Touch based browser console

228 lines (181 loc) 5.29 kB
import EventEmitter from 'events'; const name = "peek42"; const version = "5.9.2"; function _isNullProtoObject(val) { return typeof val === 'object' && val !== null && val.__proto__ === undefined; } function _string(val) { return _isNullProtoObject(val) ? '[object Object(proto:Null)]' : String(val); } function _comment(comment, val, note = undefined) { if (comment !== undefined && comment !== '') { return comment; } let str = _string(val).replace(/\s+/gm, ' '); let max = 69; if (str.length > max) { str = `${str.substr(0, max)}...`; } return note === undefined ? str : `(${note}) ${str}`; } const _outputOptsDefaults = { level: 'log', collapsed: false }; function _prettyMakesSense(val) { return val instanceof Object && !(val instanceof Function) || _isNullProtoObject(val); } function pretty(val) { let objs = []; let keys = []; return JSON.stringify(val, (k, v) => { if (v instanceof Object || _isNullProtoObject(v)) { let seen = objs.indexOf(v); if (seen === -1) { objs.push(v); keys.push(k || 'ROOT'); return v; } return `${_string(v)} (ref to ${keys[seen]})`; } return v; }, 2); } function p(val, comment = undefined, opts = undefined) { peek42._output(val, _comment(comment, val, 'value'), opts); } p.pretty = function (val, comment = undefined, opts = undefined) { peek42._output(_prettyMakesSense(val) ? pretty(val) : val, _comment(comment, val, 'pretty'), opts); }; function pp(...args) { (p.inspect ? p.inspect : p.pretty)(...args); } function use(lib) { Object.assign(p, lib.peek42(p, _comment)); return peek42; } const peek42 = { get [Symbol.toStringTag]() { return name; }, version: version, pretty, p, pp, use }; function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } const cable = { __proto__: EventEmitter.prototype, // Call as early as possible on new client connecting /* e.g. in a pre-routing http get middleware httpRouter.get('/', (ctx, next) => { peek42.cable.init0(); next(); }); */ init0() { this.init(null); }, // Between init0 and init calls, any cp call will // wait for the websocket promise to resolve. // (If init0 is not called, there will be a dead zone during // which cp calls will use the previous (probably stale) // peek42 websocket) // Call as soon as peek42 websocket is available /* e.g. in the peek42 websocket get middleware wsRouter.get('/peek42', ctx => { peek42.cable.init(ctx.websocket); }); */ init(websocket) { if (this._websocket) { this._websocket.terminate(); } if (!websocket) { this._websocket = null; } else { this._websocket = websocket; this.emit('ready'); } }, get websocket() { return new Promise((resolve, reject) => { if (this._websocket) { resolve(this._websocket); } else { this.on('ready', () => resolve(this._websocket)); } }); } }; const _send = async (val, comment, opts) => (await cable.websocket).send(JSON.stringify({ val, comment, opts })); function _output(val, comment, opts = {}) { opts = _objectSpread({}, _outputOptsDefaults, opts); let str = comment === null ? _string(val) : `// ${String(comment)}\n${_string(val)}`; console[opts.level](str); } function cp(val, comment = undefined, opts = undefined) { _send(val, _comment(comment, val, 'value'), opts); } function cpp(val, comment = undefined, opts = undefined) { _send(_prettyMakesSense(val) ? pretty(val) : val, _comment(comment, val, 'pretty'), opts); } p.trace = (comment = undefined, opts = undefined) => { let stack = (new Error().stack || '\n').split('\n').map(entry => entry.trim()); let trace = (stack.shift(), stack.shift(), stack); let loc = trace[0]; _output(trace.join('\n'), _comment(comment, loc, `trace`), opts); }; cp.trace = (comment = undefined, opts = undefined) => { let stack = (new Error().stack || '\n').split('\n').map(entry => entry.trim()); let trace = (stack.shift(), stack.shift(), stack); let loc = trace[0]; _send(trace.join('\n'), _comment(comment, loc, `trace`), opts); }; function use$1(lib) { Object.assign(p, lib.peek42(p, _comment)); Object.assign(cp, lib.peek42(cp, _comment)); return peek42; } Object.assign(peek42, { _output, cable, cp, cpp, use: use$1 // Overwrite the value from '../universal/base' }); export default peek42; export { pretty, p, pp }; //# sourceMappingURL=peek42.node.mjs.map