*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 {
|
export default class DHTOnlineBase extends EventEmitter {
|
||||||
|
protected swarm: any;
|
||||||
private id: Buffer;
|
private id: Buffer;
|
||||||
private bootstrapped: boolean;
|
private bootstrapped: boolean;
|
||||||
private graph: any;
|
private graph: any;
|
||||||
private connectedTo: Set<any>;
|
private connectedTo: Set<any>;
|
||||||
protected swarm: any;
|
|
||||||
|
|
||||||
constructor(id: Buffer, { swarm }: { swarm?: any } = {}) {
|
constructor(id: Buffer, { swarm }: { swarm?: any } = {}) {
|
||||||
super();
|
super();
|
||||||
|
@ -69,14 +69,23 @@ export default class DHTOnlineBase extends EventEmitter {
|
||||||
throw new TypeError("Broadcast has not been implemented");
|
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) {
|
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) {
|
getPeerSignature(id: Buffer | string) {
|
||||||
return false;
|
return this.getPeerField(id, "signature");
|
||||||
}
|
|
||||||
|
|
||||||
return data?.data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected onAddPeer(id: Buffer) {
|
protected onAddPeer(id: Buffer) {
|
||||||
|
@ -192,9 +201,7 @@ export default class DHTOnlineBase extends EventEmitter {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const data = orderedJSON.stringify(rawData);
|
const data = orderedJSON.stringify(rawData);
|
||||||
const { timestamp, signature } = this.graph.node.get(
|
const { timestamp, signature } = this.getPeerRaw(this.id);
|
||||||
this._maybeHexify(this.id)
|
|
||||||
);
|
|
||||||
this.broadcast(
|
this.broadcast(
|
||||||
Message.toBinary(
|
Message.toBinary(
|
||||||
Message.create({
|
Message.create({
|
||||||
|
|
Loading…
Reference in New Issue