const e = require("hypercore-id-encoding"),
s = require("ready-resource"),
o = require("bare-inspector"),
t = require("safety-catch"),
r = require("b4a"),
{ hashDebugPassword: i } = require("../../../core/debug"),
n = global.console
class c extends s {
constructor() {
;(super(),
(this.url = null),
(this._server = null),
(this._console = null),
this.ready().catch(t))
}
async _open() {
;(console.log(0),
(this._server = new o.Server(0, "127.0.0.1")),
console.log(1),
await new Promise((e) => this._server.once("listening", e)),
console.log(2))
const { port: e } = this._server.address()
;(console.log(5),
(this.url = `devtools://devtools/bundled/js_app.html?ws=127.0.0.1:${e}`),
console.log(`Keet core inspector url: ${this.url}`),
console.log(6))
}
async _close() {
;((global.console = n),
await new Promise((e, s) =>
this._server.close((o) => {
o ? s(o) : e(o)
}),
))
}
}
module.exports = class extends s {
constructor(e) {
;(super(),
(this.backend = e),
(this.keet = this.backend.keet),
(this.rpc = this.backend.rpc),
(this._inspector = null),
(this._onupdate = this._onConfigUpdated.bind(this)),
this.keet.on("force-update", this._onupdate),
this.rpc.onDebugEnable(this.onEnable.bind(this)),
this.rpc.onDebugDisable(this.onDisable.bind(this)))
}
_close() {
this.keet.removeListener("force-update", this._onupdate)
}
async _onConfigUpdated() {
try {
await this.onDisable()
} catch (e) {
t(e)
}
}
_respond(e) {
return { header: { duration: 0 }, ...e }
}
async onEnable({ password: s }) {
if ((this.keet.opened || (await this.keet.ready()), this.keet.isProduction)) {
const o = this.keet.config.debug.unlockHash,
t = i(e.decode(s))
if (!r.equals(o, t)) throw new Error("Invalid password")
}
return (
this._inspector || (this._inspector = new c()),
console.log("pre inspector ready"),
await this._inspector.ready(),
console.log("post inspector ready"),
this._respond({ inspector: this._inspector.url })
)
}
async onDisable() {
if (this.inspector) return (await this.inspector.close(), this._respond(null))
}
}