Compare commits

...

2 Commits

Author SHA1 Message Date
Derrick Hammer 8640dcdb6f
*Update dist 2022-12-17 10:17:02 -05:00
Derrick Hammer 2465b80441
*Refactor bootstrap so for every peer we get, we will trigger a connection to it
*Move initial hello handshake to private method and remove us from the explicit peer list in case it was added via bootstrap
2022-12-17 10:15:24 -05:00
4 changed files with 43 additions and 16 deletions

1
dist/index.d.ts vendored
View File

@ -46,5 +46,6 @@ export default class DHTCache extends EventEmitter {
private _pruneItems; private _pruneItems;
private _heartbeatCheck; private _heartbeatCheck;
private _emitHeartbeat; private _emitHeartbeat;
private _hello;
} }
//# sourceMappingURL=index.d.ts.map //# sourceMappingURL=index.d.ts.map

2
dist/index.d.ts.map vendored
View File

@ -1 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,OAAO,YAAY,MAAM,QAAQ,CAAC;AAYlC,OAAO,QAAQ,MAAM,oBAAoB,CAAC;AAc1C,MAAM,CAAC,OAAO,OAAO,QAAS,SAAQ,YAAY;IAChD,SAAS,CAAC,KAAK,EAAE,GAAG,CAAC;IACrB,OAAO,CAAC,EAAE,CAAS;IACnB,OAAO,CAAC,YAAY,CAAU;IAC9B,OAAO,CAAC,KAAK,CAAM;IACnB,OAAO,CAAC,WAAW,CAAW;IAC9B,OAAO,CAAC,iBAAiB,CAAS;IAElC,SAAS,CAAC,KAAK,EAAE,QAAQ,CAAC;IAC1B,OAAO,CAAC,GAAG,CAAa;gBAGtB,KAAK,EAAE,GAAG,EACV,EACE,EAA4B,EAC5B,iBAAsB,EACtB,GAAG,IAAI,EACR,GAAE;QAAE,EAAE,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;KAAO;IAyC7C,OAAO,CAAC,MAAM,CAAc;IAE5B,IAAW,KAAK,IAAI,MAAM,EAAE,CAE3B;IAED,IAAW,QAAQ,IAAI,MAAM,EAAE,CAU9B;IAEM,WAAW,CAAC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,GAAG;IAqBhC,SAAS,CAAC,cAAc,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM;IA6FpD,OAAO,CAAC,SAAS;IAMjB,OAAO,CAAC,UAAU;IAIlB,OAAO,CAAC,WAAW;IAInB,OAAO,CAAC,WAAW;IAInB,OAAO,CAAC,aAAa;IAOrB,OAAO,CAAC,cAAc;IAItB,OAAO,CAAC,oBAAoB;IAY5B,OAAO,CAAC,uBAAuB;IAa/B,OAAO,CAAC,aAAa;IAIrB,OAAO,CAAC,cAAc;IAmCtB,OAAO,CAAC,iBAAiB;IAoBzB,OAAO,CAAC,YAAY;IA8CpB,OAAO,CAAC,YAAY;IAQpB,OAAO,CAAC,WAAW;IA0BnB,OAAO,CAAC,eAAe;IA0BvB,OAAO,CAAC,cAAc;IA4BtB,OAAO,CAAC,MAAM;CAIf"}

20
dist/index.js vendored
View File

@ -35,7 +35,7 @@ class DHTCache extends events_1.default {
this.flood.on("peer-open", (peer) => this.addPeerHandler(peer)); this.flood.on("peer-open", (peer) => this.addPeerHandler(peer));
this.flood.on("peer-remove", (peer) => this.removePeerHandler(peer)); this.flood.on("peer-remove", (peer) => this.removePeerHandler(peer));
this.flood.on("message", (message, id) => this.onGetBroadcast(message, id)); this.flood.on("message", (message, id) => this.onGetBroadcast(message, id));
this.swarm.on("connection", (peer) => this.send(peer, b4a_1.default.from("hello"))); this.swarm.on("connection", this._hello.bind(this));
[...this.swarm.peers.values()] [...this.swarm.peers.values()]
.map((item) => { .map((item) => {
remotePublicKey: item.publicKey; remotePublicKey: item.publicKey;
@ -287,22 +287,30 @@ class DHTCache extends events_1.default {
this.graph.removeNode(this._maybeHexify(id)); this.graph.removeNode(this._maybeHexify(id));
} }
_bootstrapFrom(bootstrap) { _bootstrapFrom(bootstrap) {
if (this.bootstrapped) {
return;
}
for (const id in bootstrap) { for (const id in bootstrap) {
const { connectedTo } = bootstrap[id]; const { connectedTo } = bootstrap[id];
if (id === this.id.toString("hex")) { if (id === this.id.toString("hex")) {
continue; continue;
} }
if (!this.connectedTo.has(id)) {
this.swarm.joinPeer(id);
}
for (const connection of connectedTo) { for (const connection of connectedTo) {
const peer = b4a_1.default.from(connection); const peer = b4a_1.default.from(connection);
if (b4a_1.default.equals(peer, this.id)) {
continue;
}
this._ensurePeer(peer); this._ensurePeer(peer);
this._addEntityConnection(id, peer); this._addEntityConnection(id, peer);
if (!this.connectedTo.has(peer.toString("hex"))) {
this.swarm.joinPeer(peer);
} }
} }
}
if (!this.bootstrapped) {
this.bootstrapped = true; this.bootstrapped = true;
this.emit("bootstrapped"); this.emit("bootstrapped");
}
this._recalculate(); this._recalculate();
} }
_getBootstrapInfo() { _getBootstrapInfo() {
@ -426,5 +434,9 @@ class DHTCache extends events_1.default {
})); }));
} }
} }
_hello(peer) {
this.send(peer, b4a_1.default.from("hello"));
this.swarm.leavePeer(peer.remotePublicKey);
}
} }
exports.default = DHTCache; exports.default = DHTCache;

View File

@ -61,9 +61,7 @@ export default class DHTCache extends EventEmitter {
this.flood.on("peer-remove", (peer) => this.removePeerHandler(peer)); this.flood.on("peer-remove", (peer) => this.removePeerHandler(peer));
this.flood.on("message", (message, id) => this.onGetBroadcast(message, id)); this.flood.on("message", (message, id) => this.onGetBroadcast(message, id));
this.swarm.on("connection", (peer: any) => this.swarm.on("connection", this._hello.bind(this));
this.send(peer, b4a.from("hello"))
);
[...this.swarm.peers.values()] [...this.swarm.peers.values()]
.map((item) => { .map((item) => {
@ -398,25 +396,36 @@ export default class DHTCache extends EventEmitter {
} }
private _bootstrapFrom(bootstrap: Bootstrap) { private _bootstrapFrom(bootstrap: Bootstrap) {
if (this.bootstrapped) {
return;
}
for (const id in bootstrap) { for (const id in bootstrap) {
const { connectedTo } = bootstrap[id]; const { connectedTo } = bootstrap[id];
if (id === this.id.toString("hex")) { if (id === this.id.toString("hex")) {
continue; continue;
} }
if (!this.connectedTo.has(id)) {
this.swarm.joinPeer(id);
}
for (const connection of connectedTo) { for (const connection of connectedTo) {
const peer = b4a.from(connection) as Buffer; const peer = b4a.from(connection) as Buffer;
if (b4a.equals(peer, this.id)) {
continue;
}
this._ensurePeer(peer); this._ensurePeer(peer);
this._addEntityConnection(id, peer); this._addEntityConnection(id, peer);
if (!this.connectedTo.has(peer.toString("hex"))) {
this.swarm.joinPeer(peer);
}
} }
} }
if (!this.bootstrapped) {
this.bootstrapped = true; this.bootstrapped = true;
this.emit("bootstrapped"); this.emit("bootstrapped");
}
this._recalculate(); this._recalculate();
} }
@ -574,4 +583,9 @@ export default class DHTCache extends EventEmitter {
); );
} }
} }
private _hello(peer: any) {
this.send(peer, b4a.from("hello"));
this.swarm.leavePeer(peer.remotePublicKey);
}
} }