*Refactor and add peer methods

This commit is contained in:
Derrick Hammer 2022-11-19 19:50:11 -05:00
parent 3bad936cd4
commit b43ec0b236
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 17 additions and 10 deletions

View File

@ -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<any>;
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({