From b43ec0b2365b6c91d6a6b728d45edfd583cc31b9 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sat, 19 Nov 2022 19:50:11 -0500 Subject: [PATCH] *Refactor and add peer methods --- src/dhtOnlineBase.ts | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) 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({