diff --git a/dist/index.d.ts b/dist/index.d.ts index c1c5897..e24d885 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -16,6 +16,7 @@ export default class DHTCache extends EventEmitter { private _cache; get cache(): string[]; get allCache(): string[]; + peerHasItem(peer: string | Buffer, item: string | Buffer): boolean; addItem(item: string | Buffer): void; private _compileMessage; private _broadcastMessage; diff --git a/dist/index.d.ts.map b/dist/index.d.ts.map index b811f06..1d3ef32 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;IAE9B,SAAS,CAAC,KAAK,EAAE,QAAQ,CAAC;gBAGxB,KAAK,EAAE,GAAG,EACV,EACE,EAA4B,EAC5B,GAAG,IAAI,EACR,GAAE;QAAE,EAAE,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;KAAO;IAkC7C,OAAO,CAAC,MAAM,CAAc;IAE5B,IAAW,KAAK,IAAI,MAAM,EAAE,CAE3B;IAED,IAAW,QAAQ,IAAI,MAAM,EAAE,CAU9B;IAEM,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM;IAqBpC,OAAO,CAAC,eAAe;IAIvB,OAAO,CAAC,iBAAiB;IAIlB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO;IAmBjD,OAAO,CAAC,OAAO,CAAc;IAE7B,IAAW,MAAM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No newline at end of file diff --git a/dist/index.js b/dist/index.js index 0379ab4..f5ea03c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -38,7 +38,7 @@ class DHTCache extends events_1.default { 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)); - this.swarm.on("connection", (peer) => this.flood.send(peer, Buffer.from("hello"), 0)); + this.swarm.on("connection", (peer) => this.flood.send(peer, b4a_1.default.from("hello"), 0)); [...this.swarm.peers.values()] .map((item) => { remotePublicKey: item.publicKey; @@ -64,10 +64,16 @@ class DHTCache extends events_1.default { } return items.sort(); } + peerHasItem(peer, item) { + peer = this._maybeHexify(peer); + item = this._maybeHexify(item); + return this.graph.hasSuccessor(peer, item); + } addItem(item) { item = this._maybeHexify(item); this._cache.add(item); this._ensureItem(item); + this._addEntityConnection(this.id, item); const broadcast = () => { this._broadcastMessage({ type: messages_js_1.Type.ADD_ITEM, @@ -173,22 +179,24 @@ class DHTCache extends events_1.default { if (messages_js_1.Type.ADD_ITEM === type) { this._ensureItem(bufData); this._addEntityConnection(id, bufData); + this.emit("item-added", id, bufData); } if (messages_js_1.Type.REMOVE_ITEM === type) { this.removeItem(bufData); this._pruneItems(); + this.emit("item-removed", id, bufData); } } } else if (type === messages_js_1.Type.CONNECTED) { const { id: toId } = decoded; - this._addEntityConnection(id, Buffer.from(toId)); + this._addEntityConnection(id, b4a_1.default.from(toId)); this.emit("peer-add-seen", id, toId); this._recalculate(); } else if (type === messages_js_1.Type.DISCONNECTED) { const { id: toId } = decoded; - this._removeEntityConnection(id, Buffer.from(toId)); + this._removeEntityConnection(id, b4a_1.default.from(toId)); this.emit("peer-remove-seen", id, toId); this._recalculate(); } @@ -272,7 +280,8 @@ class DHTCache extends events_1.default { } const connectedTo = this.graph .neighbors(id) - .map((id) => Buffer.from(id, "hex")); + .filter((item) => this.graph.node.get(item)?.type === EntityType.PUBKEY) + .map((id) => b4a_1.default.from(id, "hex")); state[id] = { connectedTo }; } return state;