peerpigeon 1.0.8 → 1.0.9

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.
@@ -4397,37 +4397,20 @@ ${b64.match(/.{1,64}/g).join("\n")}
4397
4397
  this.connectionAttempts.clear();
4398
4398
  }
4399
4399
  addDiscoveredPeer(peerId) {
4400
- const wasKnown = this.discoveredPeers.has(peerId);
4401
- const now = Date.now();
4402
- this.discoveredPeers.set(peerId, now);
4403
- if (wasKnown) {
4404
- this.debug.log(`Re-discovered peer ${peerId.substring(0, 8)}... (updating timestamp)`);
4405
- if (this.connectionAttempts.has(peerId)) {
4406
- const attemptTime = this.connectionAttempts.get(peerId);
4407
- if (now - attemptTime > 5e3) {
4408
- this.debug.log(`Clearing stale connection attempt for ${peerId.substring(0, 8)}...`);
4409
- this.connectionAttempts.delete(peerId);
4410
- }
4411
- }
4412
- } else {
4413
- this.debug.log(`Discovered new peer ${peerId.substring(0, 8)}...`);
4414
- this.emit("peerDiscovered", { peerId });
4400
+ if (this.discoveredPeers.has(peerId)) {
4401
+ this.discoveredPeers.set(peerId, Date.now());
4402
+ return;
4415
4403
  }
4416
- const alreadyTrying = this.connectionAttempts.has(peerId);
4417
- const shouldInitiate = this.shouldInitiateConnection(peerId);
4418
- if (this.autoDiscovery && shouldInitiate && !alreadyTrying) {
4419
- this.debug.log(`Considering connection to ${peerId.substring(0, 8)}... (shouldInitiate: ${shouldInitiate}, alreadyTrying: ${alreadyTrying})`);
4404
+ this.discoveredPeers.set(peerId, Date.now());
4405
+ this.emit("peerDiscovered", { peerId });
4406
+ this.debug.log(`Discovered peer ${peerId.substring(0, 8)}...`);
4407
+ if (this.autoDiscovery && this.shouldInitiateConnection(peerId) && !this.connectionAttempts.has(peerId)) {
4408
+ this.debug.log(`Considering connection to ${peerId.substring(0, 8)}...`);
4420
4409
  const canAccept = this.canAcceptMorePeers();
4421
4410
  if (canAccept) {
4422
- this.debug.log(`\u{1F517} Initiating connection to ${peerId.substring(0, 8)}...`);
4411
+ this.debug.log(`Connecting to ${peerId.substring(0, 8)}...`);
4423
4412
  this.emit("connectToPeer", { peerId });
4424
- } else {
4425
- this.debug.log(`Cannot connect to ${peerId.substring(0, 8)}... (at max peers)`);
4426
4413
  }
4427
- } else if (alreadyTrying) {
4428
- this.debug.log(`Already attempting connection to ${peerId.substring(0, 8)}...`);
4429
- } else if (!shouldInitiate) {
4430
- this.debug.log(`Not initiating to ${peerId.substring(0, 8)}... (should wait for them to initiate)`);
4431
4414
  }
4432
4415
  this.scheduleMeshOptimization();
4433
4416
  }
@@ -4456,6 +4439,9 @@ ${b64.match(/.{1,64}/g).join("\n")}
4456
4439
  return distance;
4457
4440
  }
4458
4441
  shouldInitiateConnection(targetPeerId) {
4442
+ if (this.connectionAttempts.has(targetPeerId)) {
4443
+ return false;
4444
+ }
4459
4445
  this.emit("checkCapacity");
4460
4446
  const currentConnectionCount = this._currentConnectionCount || 0;
4461
4447
  const lexicographicShouldInitiate = this.peerId > targetPeerId;
@@ -4826,18 +4812,19 @@ ${b64.match(/.{1,64}/g).join("\n")}
4826
4812
  const hasSignaling = this.mesh && this.mesh.signalingClient && this.mesh.signalingClient.isConnected();
4827
4813
  const hasMeshConnectivity = this.mesh && this.mesh.connected && this.mesh.connectionManager.getConnectedPeerCount() > 0;
4828
4814
  if (hasSignaling || hasMeshConnectivity) {
4829
- this.debug.log(`\u26A0\uFE0F ICE remained disconnected for ${this.peerId}, attempting restart (signaling: ${hasSignaling}, mesh: ${hasMeshConnectivity})`);
4815
+ this.debug.log(`\u274C ICE remained disconnected for ${this.peerId}, attempting restart (signaling: ${hasSignaling}, mesh: ${hasMeshConnectivity})`);
4830
4816
  try {
4831
4817
  this.restartIceViaSignaling().catch((error) => {
4832
4818
  this.debug.error("Failed to restart ICE after disconnection:", error);
4833
- this.debug.log(`Will wait for ICE to reach "failed" state before disconnecting ${this.peerId}`);
4819
+ this.emit("disconnected", { peerId: this.peerId, reason: "ICE disconnected" });
4834
4820
  });
4835
4821
  } catch (error) {
4836
4822
  this.debug.error("Failed to restart ICE after disconnection:", error);
4837
- this.debug.log(`Will wait for ICE to reach "failed" state before disconnecting ${this.peerId}`);
4823
+ this.emit("disconnected", { peerId: this.peerId, reason: "ICE disconnected" });
4838
4824
  }
4839
4825
  } else {
4840
- this.debug.log(`\u26A0\uFE0F ICE disconnected for ${this.peerId} without signaling - waiting for ICE state to resolve naturally`);
4826
+ this.debug.log(`\u274C ICE remained disconnected for ${this.peerId}, disconnecting`);
4827
+ this.emit("disconnected", { peerId: this.peerId, reason: "ICE disconnected" });
4841
4828
  }
4842
4829
  }
4843
4830
  }, 5e3);
@@ -6079,30 +6066,12 @@ ${b64.match(/.{1,64}/g).join("\n")}
6079
6066
  async connectToPeer(targetPeerId) {
6080
6067
  this.debug.log(`connectToPeer called for ${targetPeerId.substring(0, 8)}...`);
6081
6068
  if (this.peers.has(targetPeerId)) {
6082
- const existingPeer = this.peers.get(targetPeerId);
6083
- const status = existingPeer.getStatus();
6084
- if (status === "connected") {
6085
- this.debug.log(`Already connected to ${targetPeerId.substring(0, 8)}... (status: ${status})`);
6086
- return;
6087
- }
6088
- if (status === "connecting") {
6089
- const connectionAge = Date.now() - (existingPeer.createdAt || 0);
6090
- if (connectionAge < 1e4) {
6091
- this.debug.log(`Connection to ${targetPeerId.substring(0, 8)}... still in progress (${Math.round(connectionAge / 1e3)}s)`);
6092
- return;
6093
- }
6094
- this.debug.log(`Connection to ${targetPeerId.substring(0, 8)}... appears stuck (${Math.round(connectionAge / 1e3)}s), will retry`);
6095
- this.cleanupFailedConnection(targetPeerId);
6096
- }
6069
+ this.debug.log(`Already connected to ${targetPeerId.substring(0, 8)}...`);
6070
+ return;
6097
6071
  }
6098
6072
  if (this.mesh.peerDiscovery.isAttemptingConnection(targetPeerId)) {
6099
- const attemptAge = Date.now() - (this.lastConnectionAttempt.get(targetPeerId) || 0);
6100
- if (attemptAge < 1e4) {
6101
- this.debug.log(`Already attempting connection to ${targetPeerId.substring(0, 8)}... via PeerDiscovery (${Math.round(attemptAge / 1e3)}s ago)`);
6102
- return;
6103
- }
6104
- this.debug.log(`Old connection attempt to ${targetPeerId.substring(0, 8)}... (${Math.round(attemptAge / 1e3)}s ago), allowing retry`);
6105
- this.mesh.peerDiscovery.clearConnectionAttempt(targetPeerId);
6073
+ this.debug.log(`Already attempting connection to ${targetPeerId.substring(0, 8)}... via PeerDiscovery`);
6074
+ return;
6106
6075
  }
6107
6076
  const shouldBeInitiator = this.mesh.peerId > targetPeerId;
6108
6077
  if (!shouldBeInitiator) {
@@ -6118,22 +6087,17 @@ ${b64.match(/.{1,64}/g).join("\n")}
6118
6087
  const attempts = this.connectionAttempts.get(targetPeerId) || 0;
6119
6088
  if (attempts > 0) {
6120
6089
  const lastAttempt = this.lastConnectionAttempt.get(targetPeerId) || 0;
6121
- const connectedCount2 = this.getConnectedPeerCount();
6122
- const retryDelay = connectedCount2 === 0 ? 200 : this.retryDelay;
6090
+ const connectedCount = this.getConnectedPeerCount();
6091
+ const retryDelay = connectedCount === 0 ? 200 : this.retryDelay;
6123
6092
  if (now - lastAttempt < retryDelay) {
6124
6093
  const remaining = retryDelay - (now - lastAttempt);
6125
- this.debug.log(`Connection to ${targetPeerId.substring(0, 8)}... on cooldown (${Math.round(remaining)}ms remaining, isolated: ${connectedCount2 === 0})`);
6094
+ this.debug.log(`Connection to ${targetPeerId.substring(0, 8)}... on cooldown (${Math.round(remaining / 1e3)}s remaining, isolated: ${connectedCount === 0})`);
6126
6095
  return;
6127
6096
  }
6128
6097
  }
6129
- const connectedCount = this.getConnectedPeerCount();
6130
- const maxAttempts = connectedCount === 0 ? this.maxConnectionAttempts * 2 : this.maxConnectionAttempts;
6131
- if (attempts >= maxAttempts) {
6132
- this.debug.log(`Max connection attempts (${maxAttempts}) reached for ${targetPeerId.substring(0, 8)}...`);
6098
+ if (attempts >= this.maxConnectionAttempts) {
6133
6099
  this.mesh.emit("statusChanged", { type: "warning", message: `Max connection attempts reached for ${targetPeerId.substring(0, 8)}...` });
6134
- this.connectionAttempts.delete(targetPeerId);
6135
- this.lastConnectionAttempt.delete(targetPeerId);
6136
- this.mesh.peerDiscovery.clearConnectionAttempt(targetPeerId);
6100
+ this.mesh.peerDiscovery.removeDiscoveredPeer(targetPeerId);
6137
6101
  return;
6138
6102
  }
6139
6103
  this.debug.log(`Starting connection to ${targetPeerId.substring(0, 8)}... (attempt ${attempts + 1})`);
@@ -7064,22 +7028,11 @@ ${b64.match(/.{1,64}/g).join("\n")}
7064
7028
  }
7065
7029
  }
7066
7030
  handlePeerAnnouncement(fromPeerId) {
7067
- if (this.connectionManager.hasPeer(fromPeerId)) {
7068
- const peerStatus = this.connectionManager.getPeer(fromPeerId).getStatus();
7069
- if (peerStatus === "connected") {
7070
- this.debug.log(`Peer ${fromPeerId.substring(0, 8)}... already connected (${peerStatus}), updating timestamp`);
7071
- this.mesh.peerDiscovery.updateDiscoveryTimestamp(fromPeerId);
7072
- return;
7073
- }
7074
- this.debug.log(`Peer ${fromPeerId.substring(0, 8)}... exists but not connected (${peerStatus}), allowing re-discovery`);
7075
- }
7076
- const wasKnown = this.mesh.peerDiscovery.hasPeer(fromPeerId);
7077
- if (wasKnown) {
7078
- this.debug.log(`Peer ${fromPeerId.substring(0, 8)}... already known, refreshing discovery`);
7079
- } else {
7080
- this.debug.log(`New peer discovered: ${fromPeerId.substring(0, 8)}...`);
7031
+ if (this.mesh.peerDiscovery.hasPeer(fromPeerId)) {
7032
+ this.debug.log(`Peer ${fromPeerId.substring(0, 8)}... already known, skipping announcement`);
7033
+ return;
7081
7034
  }
7082
- this.mesh.emit("statusChanged", { type: "info", message: `Peer ${fromPeerId.substring(0, 8)}... ${wasKnown ? "re-announced" : "announced"}` });
7035
+ this.mesh.emit("statusChanged", { type: "info", message: `Peer ${fromPeerId.substring(0, 8)}... announced` });
7083
7036
  this.debug.log(`Adding discovered peer: ${fromPeerId.substring(0, 8)}... to PeerDiscovery`);
7084
7037
  this.debug.log(`Current peer count: ${this.connectionManager.getConnectedPeerCount()}/${this.mesh.maxPeers}`);
7085
7038
  this.debug.log(`All discovered peers: ${Array.from(this.mesh.peerDiscovery.discoveredPeers.keys()).map((p) => p.substring(0, 8)).join(", ")}`);