diff --git a/src/dhtOnlineBase.ts b/src/dhtOnlineBase.ts index fea4b32..2e05018 100644 --- a/src/dhtOnlineBase.ts +++ b/src/dhtOnlineBase.ts @@ -18,11 +18,11 @@ type Bootstrap = { }; export default class DHTOnlineBase extends EventEmitter { + protected swarm: any; private id: Buffer; private bootstrapped: boolean; private graph: any; private connectedTo: Set; - protected swarm: any; constructor(id: Buffer, { swarm }: { swarm?: any } = {}) { super(); @@ -69,14 +69,23 @@ export default class DHTOnlineBase extends EventEmitter { throw new TypeError("Broadcast has not been implemented"); } + getPeerRaw(id: Buffer | string) { + return this.graph.node.get(this._maybeHexify(id)); + } + + getPeerField(id: Buffer | string, field: string) { + return this.getPeerRaw(id)?.[field]; + } + getPeerData(id: Buffer | string) { - let data = this.graph.node.get(this._maybeHexify(id)); + return this.getPeerField(id, "data"); + } + getPeerTimestamp(id: Buffer | string) { + return this.getPeerField(id, "timestamp"); + } - if (!data) { - return false; - } - - return data?.data; + getPeerSignature(id: Buffer | string) { + return this.getPeerField(id, "signature"); } protected onAddPeer(id: Buffer) { @@ -192,9 +201,7 @@ export default class DHTOnlineBase extends EventEmitter { return; } const data = orderedJSON.stringify(rawData); - const { timestamp, signature } = this.graph.node.get( - this._maybeHexify(this.id) - ); + const { timestamp, signature } = this.getPeerRaw(this.id); this.broadcast( Message.toBinary( Message.create({