@hive-p2p/browser 1.0.97 → 1.0.98

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -50,7 +50,8 @@ export class OfferQueue {
50
50
 
51
51
  export class Topologist {
52
52
  verbose; id; cryptoCodex; gossip; messager; peerStore; bootstraps; offersQueue = new OfferQueue();
53
- /** @type {Map<string, boolean>} */ bootstrapsConnectionState = new Map();
53
+ /** @type {Map<string, boolean>} */ bootstrapConnexionStates = new Map();
54
+ /** @type {Set<string>} */ connectedBootstrapIds = new Set();
54
55
  /** @type {import('./node-services.mjs').NodeServices | undefined} */ services;
55
56
  /** @type {number} */ NEIGHBORS_TARGET;
56
57
  /** @type {number} */ HALF_TARGET;
@@ -70,7 +71,6 @@ export class Topologist {
70
71
  constructor(selfId, cryptoCodex, gossip, messager, peerStore, bootstraps, verbose = 1) {
71
72
  this.setNeighborsTarget(DISCOVERY.TARGET_NEIGHBORS_COUNT);
72
73
  this.verbose = verbose; this.id = selfId; this.cryptoCodex = cryptoCodex; this.gossip = gossip; this.messager = messager; this.peerStore = peerStore;
73
- for (const url of bootstraps) this.bootstrapsConnectionState.set(url, false);
74
74
  this.bootstraps = [...bootstraps].sort(() => Math.random() - 0.5); // shuffle
75
75
  this.nextBootstrapIndex = Math.random() * this.bootstraps.length | 0;
76
76
  }
@@ -132,7 +132,7 @@ export class Topologist {
132
132
  else if (connectingCount + nonPublicNeighborsCount > this.TWICE_TARGET) return; // no more bootstrap needed
133
133
 
134
134
  const publicUrl = this.bootstraps[this.nextBootstrapIndex++ % this.bootstraps.length];
135
- if (this.bootstrapsConnectionState.get(publicUrl)) return; // already connecting/connected
135
+ if (this.bootstrapConnexionStates.get(publicUrl)) return; // already connected/connecting
136
136
  this.#connectToPublicNode(publicUrl);
137
137
  }
138
138
 
@@ -169,12 +169,15 @@ export class Topologist {
169
169
  }
170
170
  #connectToPublicNode(publicUrl = 'localhost:8080') {
171
171
  let remoteId = null;
172
+ let isAliasDetected = false; // for early close on "ids match" case
172
173
  const ws = new TRANSPORTS.WS_CLIENT(this.#getFullWsUrl(publicUrl)); ws.binaryType = 'arraybuffer';
173
174
  if (this.verbose > 1) ws.onerror = (error) => console.warn(`WebSocket error:`, error.stack);
174
175
  ws.onopen = () => {
175
- this.bootstrapsConnectionState.set(publicUrl, true);
176
+ this.bootstrapConnexionStates.set(publicUrl, true);
176
177
  ws.onclose = () => {
177
- this.bootstrapsConnectionState.set(publicUrl, false);
178
+ if (isAliasDetected) return;
179
+ this.bootstrapConnexionStates.set(publicUrl, false);
180
+ this.connectedBootstrapIds.delete(remoteId);
178
181
  for (const cb of this.peerStore.callbacks.disconnect) cb(remoteId, 'out');
179
182
  }
180
183
  ws.onmessage = (data) => {
@@ -192,6 +195,15 @@ export class Topologist {
192
195
  if (!this.cryptoCodex.verifySignature(pubkey, signedData, signature)) return;
193
196
 
194
197
  remoteId = route[0];
198
+
199
+ if (this.connectedBootstrapIds.has(remoteId)) { // AVOID ALIASES => REMOVE URL FROM BOOTSTRAP LIST
200
+ isAliasDetected = true;
201
+ this.bootstraps = this.bootstraps.filter(url => url !== publicUrl);
202
+ return ws.close();
203
+ }
204
+
205
+ // FINALIZE PEERSTORE CONNECTION
206
+ this.connectedBootstrapIds.add(remoteId);
195
207
  this.peerStore.digestPeerNeighbors(remoteId, neighborsList); // Update known store
196
208
  this.peerStore.connecting[remoteId]?.in?.close(); // close incoming connection if any
197
209
  if (!this.peerStore.connecting[remoteId]) this.peerStore.connecting[remoteId] = {};