diff --git a/dist/index.d.ts b/dist/index.d.ts index 224fa81..12b5c95 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -10,6 +10,7 @@ export default class DHTCache extends EventEmitter { private connectedTo; private heartBeatInterval; protected flood: DHTFlood; + private log; constructor(swarm: any, { id, heartBeatInterval, ...opts }?: { id?: Buffer; [key: string]: any; diff --git a/dist/index.d.ts.map b/dist/index.d.ts.map index f325691..e32912d 100644 --- a/dist/index.d.ts.map +++ b/dist/index.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,OAAO,YAAY,MAAM,QAAQ,CAAC;AAYlC,OAAO,QAAQ,MAAM,oBAAoB,CAAC;AAa1C,MAAM,CAAC,OAAO,OAAO,QAAS,SAAQ,YAAY;IAChD,SAAS,CAAC,KAAK,EAAE,GAAG,CAAC;IACrB,OAAO,CAAC,EAAE,CAAS;IACnB,OAAO,CAAC,YAAY,CAAU;IAC9B,OAAO,CAAC,KAAK,CAAM;IACnB,OAAO,CAAC,WAAW,CAAW;IAC9B,OAAO,CAAC,iBAAiB,CAAS;IAElC,SAAS,CAAC,KAAK,EAAE,QAAQ,CAAC;gBAGxB,KAAK,EAAE,GAAG,EACV,EACE,EAA4B,EAC5B,iBAAsB,EACtB,GAAG,IAAI,EACR,GAAE;QAAE,EAAE,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;KAAO;IAqC7C,OAAO,CAAC,MAAM,CAAc;IAE5B,IAAW,KAAK,IAAI,MAAM,EAAE,CAE3B;IAED,IAAW,QAAQ,IAAI,MAAM,EAAE,CAU9B;IAEM,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO;IAOlE,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM;IAsBpC,OAAO,CAAC,eAAe;IAIvB,O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AC,WAAW;IAInB,OAAO,CAAC,WAAW;IAInB,OAAO,CAAC,aAAa;IAOrB,OAAO,CAAC,cAAc;IAItB,OAAO,CAAC,oBAAoB;IAY5B,OAAO,CAAC,uBAAuB;IAa/B,OAAO,CAAC,aAAa;IAIrB,OAAO,CAAC,cAAc;IAwBtB,OAAO,CAAC,iBAAiB;IAoBzB,OAAO,CAAC,YAAY;IA8CpB,OAAO,CAAC,YAAY;IAQpB,OAAO,CAAC,WAAW;IA0BnB,OAAO,CAAC,eAAe;IA0BvB,OAAO,CAAC,cAAc;CA2BvB"} \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index 7396c44..7752f8c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -11,6 +11,7 @@ const hypercore_crypto_1 = __importDefault(require("hypercore-crypto")); const b4a_1 = __importDefault(require("b4a")); const messages_js_1 = require("./messages.js"); const dht_flood_1 = __importDefault(require("@lumeweb/dht-flood")); +const loglevel_1 = require("loglevel"); const EntityType = { PUBKEY: Symbol.for("PUBKEY"), ITEM: Symbol.for("ITEM"), @@ -24,6 +25,7 @@ class DHTCache extends events_1.default { connectedTo; heartBeatInterval; flood; + log; constructor(swarm, { id = swarm.keyPair.publicKey, heartBeatInterval = 15, ...opts } = {}) { super(); if (!id) @@ -37,6 +39,7 @@ class DHTCache extends events_1.default { this._online = new Set([this._maybeHexify(this.id)]); this.swarm = swarm; this.flood = new dht_flood_1.default({ id, swarm, ...opts }); + this.log = (0, loglevel_1.getLogger)("dht-cache"); this.flood.on("peer-open", (peer) => this.addPeerHandler(peer)); this.flood.on("peer-remove", (peer) => this.removePeerHandler(peer)); this.flood.on("message", (message, id) => this.onGetBroadcast(message, id)); @@ -146,9 +149,11 @@ class DHTCache extends events_1.default { id, }); this._emitHeartbeat(peer); + this.log.debug(`Relay peer connected: ${stringId}`); if (this.bootstrapped) { return; } + this.log.debug(`Broadcasting bootstrap request`); // If this is the first person we've met, get their graph this._broadcastMessage({ type: messages_js_1.Type.BOOTSTRAP_REQUEST }, 0); } @@ -161,6 +166,7 @@ class DHTCache extends events_1.default { } this.onRemovePeer(peer); }, DISCONNECT_SMOOTH); + this.log.debug(`Relay peer might have disconnected: ${id.toString("hex")}`); } onRemovePeer(peer) { const id = peer.remotePublicKey; @@ -172,6 +178,7 @@ class DHTCache extends events_1.default { type: messages_js_1.Type.DISCONNECTED, id, }, 0); + this.log.debug(`Relay peer confirmed disconnected: ${id.toString("hex")}`); } onGetBroadcast(message, id) { let decoded; @@ -193,26 +200,32 @@ class DHTCache extends events_1.default { this._ensureItem(bufData); this._addEntityConnection(id, bufData); this.emit("item-added", id, bufData); + this.log.debug(`New item added: ${bufData.toString("hex")} from ${id.toString("hex")}`); } if (messages_js_1.Type.REMOVE_ITEM === type) { this.removeItem(bufData); this._pruneItems(); this.emit("item-removed", id, bufData); + this.log.debug(`Item removed: ${bufData.toString("hex")} from ${id.toString("hex")}`); } } } else if (type === messages_js_1.Type.CONNECTED) { const { id: toId } = decoded; - this._addEntityConnection(id, b4a_1.default.from(toId)); - this.emit("peer-add-seen", id, toId); + let bufId = b4a_1.default.from(toId); + this._addEntityConnection(id, bufId); + this.emit("peer-add-seen", id, bufId); this._recalculate(); + this.log.debug(`Network peer connected: ${bufId.toString("hex")}`); } else if (type === messages_js_1.Type.DISCONNECTED) { const { id: toId } = decoded; + let bufId = b4a_1.default.from(toId); this._removeEntityConnection(id, b4a_1.default.from(toId)); this.emit("peer-remove-seen", id, toId); this._recalculate(); this._pruneItems(); + this.log.debug(`Network peer disconnected: ${bufId.toString("hex")}`); } else if (type === messages_js_1.Type.BOOTSTRAP_REQUEST) { const bootstrap = this._getBootstrapInfo(); @@ -220,18 +233,20 @@ class DHTCache extends events_1.default { type: messages_js_1.Type.BOOTSTRAP_RESPONSE, bootstrap, })), 0); + this.log.debug(`Bootstrap request received`); } else if (type === messages_js_1.Type.BOOTSTRAP_RESPONSE) { const { bootstrap } = decoded; this._bootstrapFrom(bootstrap); } else if (type === messages_js_1.Type.HEARTBEAT) { - let { id: toId, signature, data: bufData } = decoded; - toId = b4a_1.default.from(toId); + const { id: toId, signature, data: bufData } = decoded; + let bufId = b4a_1.default.from(toId); if (signature && hypercore_crypto_1.default.verify(bufData, signature, id)) { - this._addEntityConnection(id, toId); - this._setEntity(toId, { heartbeat: Date.now() }); + this._addEntityConnection(id, bufId); + this._setEntity(bufId, { heartbeat: Date.now() }); } + this.log.debug(`Bootstrap response received from ${bufId.toString("hex")}`); } } _signItem(item) { @@ -342,6 +357,7 @@ class DHTCache extends events_1.default { } this._online = online; this.emit("online", online); + this.log.debug(`Online list updated: ${online.size} network peers online, ${offline.size} network peers offline and removed from DAG`); } _maybeHexify(data) { if (b4a_1.default.isBuffer(data)) {