*Refactor and add peer methods
This commit is contained in:
parent
3bad936cd4
commit
b43ec0b236
|
@ -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");
|
||||
}
|
||||
|
||||
getPeerData(id: Buffer | string) {
|
||||
let data = this.graph.node.get(this._maybeHexify(id));
|
||||
|
||||
if (!data) {
|
||||
return false;
|
||||
getPeerRaw(id: Buffer | string) {
|
||||
return this.graph.node.get(this._maybeHexify(id));
|
||||
}
|
||||
|
||||
return data?.data;
|
||||
getPeerField(id: Buffer | string, field: string) {
|
||||
return this.getPeerRaw(id)?.[field];
|
||||
}
|
||||
|
||||
getPeerData(id: Buffer | string) {
|
||||
return this.getPeerField(id, "data");
|
||||
}
|
||||
getPeerTimestamp(id: Buffer | string) {
|
||||
return this.getPeerField(id, "timestamp");
|
||||
}
|
||||
|
||||
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({
|
||||
|
|
Loading…
Reference in New Issue