OverviewFilesCommitsIssuesDocsReleasesPeers

keet-cli / keet-core-4.14.4-pretty / lib / api / v1 / inspector.js

53 lines · javascript ref:
const { Console: s, Server: e } = require("bare-inspector"),
  { once: o } = require("bare-events")
class l {
  constructor({ port: s = 0, host: e = "localhost" } = {}) {
    ;((this.port = s),
      (this.host = e),
      (this._server = null),
      (this._url = null),
      (this._bareConsole = null),
      (this._oldConsole = global.console))
  }
  get key() {
    return this._url
  }
  _overrideConsole() {
    this._bareConsole || (this._bareConsole = new s())
    const e = {}
    for (const s of Object.keys(this._bareConsole))
      e[s] = (...e) => {
        ;(this._bareConsole[s](...e), this._oldConsole[s](...e))
      }
    global.console = e
  }
  _resetConsole() {
    global.console = this._oldConsole
  }
  async enable() {
    ;((this._server = new e(this.port, this.host)),
      this._server.on("close", () => {
        this._resetConsole()
      }),
      await o(this._server, "listening"),
      this._overrideConsole())
    const { port: s } = this._server.address()
    return (
      (this._url = `devtools://devtools/bundled/js_app.html?ws=localhost:${s}`),
      console.log(`Keet core inspector url: ${this._url}`),
      this._url
    )
  }
  async disable() {
    this._server &&
      (await new Promise((s) => {
        this._server.close(s)
      }),
      (this._server = null),
      this._resetConsole(),
      (this._bareConsole = null),
      (this._url = null))
  }
}
module.exports = l