peerpigeon 0.0.7 → 0.1.0

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.
@@ -1,4 +1,5 @@
1
1
  import { EventEmitter } from './EventEmitter.js';
2
+ import DebugLogger from './DebugLogger.js';
2
3
 
3
4
  /**
4
5
  * Manages mesh optimization, connection strategies, and peer discovery optimization
@@ -6,6 +7,7 @@ import { EventEmitter } from './EventEmitter.js';
6
7
  export class MeshOptimizer extends EventEmitter {
7
8
  constructor(mesh, connectionManager, evictionManager) {
8
9
  super();
10
+ this.debug = DebugLogger.create('MeshOptimizer');
9
11
  this.mesh = mesh;
10
12
  this.connectionManager = connectionManager;
11
13
  this.evictionManager = evictionManager;
@@ -23,7 +25,7 @@ export class MeshOptimizer extends EventEmitter {
23
25
  const belowMinimum = currentConnected < this.mesh.minPeers;
24
26
 
25
27
  if (hasNoConnections && unconnectedPeers.length > 0) {
26
- console.log(`Peer has no connections but found ${unconnectedPeers.length} unconnected peers - forcing connection attempt`);
28
+ this.debug.log(`Peer has no connections but found ${unconnectedPeers.length} unconnected peers - forcing connection attempt`);
27
29
  // Force connection to the closest peer by XOR distance
28
30
  const sortedByDistance = unconnectedPeers.sort((a, b) => {
29
31
  const distA = this.calculateXorDistance(this.mesh.peerId, a);
@@ -32,13 +34,13 @@ export class MeshOptimizer extends EventEmitter {
32
34
  });
33
35
 
34
36
  const targetPeer = sortedByDistance[0];
35
- console.log(`Forcing connection to closest peer: ${targetPeer.substring(0, 8)}... (no connections, mesh connectivity required)`);
37
+ this.debug.log(`Forcing connection to closest peer: ${targetPeer.substring(0, 8)}... (no connections, mesh connectivity required)`);
36
38
  this.connectionManager.connectToPeer(targetPeer);
37
39
  return;
38
40
  }
39
41
 
40
42
  if (belowMinimum && unconnectedPeers.length > 0) {
41
- console.log(`Below minimum peers (${currentConnected}/${this.mesh.minPeers}) - connecting to additional peers`);
43
+ this.debug.log(`Below minimum peers (${currentConnected}/${this.mesh.minPeers}) - connecting to additional peers`);
42
44
  const needed = Math.min(this.mesh.minPeers - currentConnected, unconnectedPeers.length);
43
45
 
44
46
  // Sort by XOR distance and connect to closest peers
@@ -50,7 +52,7 @@ export class MeshOptimizer extends EventEmitter {
50
52
 
51
53
  for (let i = 0; i < needed; i++) {
52
54
  const targetPeer = sortedByDistance[i];
53
- console.log(`Connecting to reach minimum: ${targetPeer.substring(0, 8)}...`);
55
+ this.debug.log(`Connecting to reach minimum: ${targetPeer.substring(0, 8)}...`);
54
56
  this.connectionManager.connectToPeer(targetPeer);
55
57
  }
56
58
  return;
@@ -60,14 +62,14 @@ export class MeshOptimizer extends EventEmitter {
60
62
  if (this.mesh.maxPeers <= 3) {
61
63
  // For small meshes, always try to reach max capacity
62
64
  if (currentConnected >= this.mesh.maxPeers) {
63
- console.log(`Skipping optimization - already at max capacity ${currentConnected}/${this.mesh.maxPeers}`);
65
+ this.debug.log(`Skipping optimization - already at max capacity ${currentConnected}/${this.mesh.maxPeers}`);
64
66
  return;
65
67
  }
66
68
  } else {
67
69
  // For larger meshes, only optimize if below 70% capacity
68
70
  const targetThreshold = Math.floor(this.mesh.maxPeers * 0.7);
69
71
  if (currentConnected >= targetThreshold) {
70
- console.log(`Skipping optimization - ${currentConnected}/${this.mesh.maxPeers} peers connected (threshold: ${targetThreshold})`);
72
+ this.debug.log(`Skipping optimization - ${currentConnected}/${this.mesh.maxPeers} peers connected (threshold: ${targetThreshold})`);
71
73
  return;
72
74
  }
73
75
  }
@@ -75,21 +77,21 @@ export class MeshOptimizer extends EventEmitter {
75
77
  const availableSlots = this.mesh.maxPeers - currentConnected;
76
78
  const peersToConnect = unconnectedPeers.slice(0, Math.min(availableSlots, 1)); // Only connect to 1 peer at a time
77
79
 
78
- console.log(`Optimizing connections carefully: ${availableSlots} slots available, connecting to ${peersToConnect.length} peer(s)`);
80
+ this.debug.log(`Optimizing connections carefully: ${availableSlots} slots available, connecting to ${peersToConnect.length} peer(s)`);
79
81
 
80
82
  peersToConnect.forEach((peerId, _index) => {
81
83
  if (this.mesh.peerDiscovery.shouldInitiateConnection(peerId)) {
82
- console.log(`Initiating immediate connection to ${peerId.substring(0, 8)}... in optimization`);
84
+ this.debug.log(`Initiating immediate connection to ${peerId.substring(0, 8)}... in optimization`);
83
85
  // Double-check conditions before connecting
84
86
  if (this.connectionManager.canAcceptMorePeers() &&
85
87
  !this.connectionManager.hasPeer(peerId) &&
86
88
  !this.mesh.peerDiscovery.isAttemptingConnection(peerId)) {
87
89
  this.connectionManager.connectToPeer(peerId);
88
90
  } else {
89
- console.log(`Skipping connection to ${peerId.substring(0, 8)}... - conditions changed`);
91
+ this.debug.log(`Skipping connection to ${peerId.substring(0, 8)}... - conditions changed`);
90
92
  }
91
93
  } else {
92
- console.log(`Not initiating connection to ${peerId.substring(0, 8)}... (should not initiate)`);
94
+ this.debug.log(`Not initiating connection to ${peerId.substring(0, 8)}... (should not initiate)`);
93
95
  }
94
96
  });
95
97
  }
@@ -110,17 +112,17 @@ export class MeshOptimizer extends EventEmitter {
110
112
  const discoveredPeers = this.mesh.getDiscoveredPeers();
111
113
  let connectionAttempts = 0;
112
114
 
113
- console.log(`Forcing connections to ${discoveredPeers.length} discovered peers...`);
115
+ this.debug.log(`Forcing connections to ${discoveredPeers.length} discovered peers...`);
114
116
 
115
117
  discoveredPeers.forEach(peer => {
116
118
  if (!this.connectionManager.hasPeer(peer.peerId) && this.connectionManager.canAcceptMorePeers()) {
117
- console.log(`Force connecting to ${peer.peerId.substring(0, 8)}...`);
119
+ this.debug.log(`Force connecting to ${peer.peerId.substring(0, 8)}...`);
118
120
  this.connectionManager.connectToPeer(peer.peerId);
119
121
  connectionAttempts++;
120
122
  }
121
123
  });
122
124
 
123
- console.log(`Initiated ${connectionAttempts} forced connection attempts`);
125
+ this.debug.log(`Initiated ${connectionAttempts} forced connection attempts`);
124
126
  return connectionAttempts;
125
127
  }
126
128
 
@@ -130,39 +132,39 @@ export class MeshOptimizer extends EventEmitter {
130
132
  const discoveredPeers = this.mesh.getDiscoveredPeers();
131
133
  const totalPeers = this.connectionManager.peers.size;
132
134
 
133
- console.log('=== CONNECTIVITY DEBUG ===');
134
- console.log(`My Peer ID: ${this.mesh.peerId}`);
135
- console.log(`Connected Peers: ${connectedPeers}/${this.mesh.maxPeers}`);
136
- console.log(`Total Peer Objects: ${totalPeers}`);
137
- console.log(`Discovered Peers: ${discoveredPeers.length}`);
135
+ this.debug.log('=== CONNECTIVITY DEBUG ===');
136
+ this.debug.log(`My Peer ID: ${this.mesh.peerId}`);
137
+ this.debug.log(`Connected Peers: ${connectedPeers}/${this.mesh.maxPeers}`);
138
+ this.debug.log(`Total Peer Objects: ${totalPeers}`);
139
+ this.debug.log(`Discovered Peers: ${discoveredPeers.length}`);
138
140
 
139
- console.log('\nPeer Details:');
141
+ this.debug.log('\nPeer Details:');
140
142
  this.connectionManager.peers.forEach((peerConnection, peerId) => {
141
143
  const status = peerConnection.getStatus();
142
144
  const dataChannelReady = peerConnection.dataChannelReady;
143
145
  const connectionState = peerConnection.connection?.connectionState || 'unknown';
144
146
  const dataChannelState = peerConnection.dataChannel?.readyState || 'unknown';
145
147
 
146
- console.log(` ${peerId.substring(0, 8)}... - Status: ${status}, WebRTC: ${connectionState}, DataChannel: ${dataChannelState}, Ready: ${dataChannelReady}`);
148
+ this.debug.log(` ${peerId.substring(0, 8)}... - Status: ${status}, WebRTC: ${connectionState}, DataChannel: ${dataChannelState}, Ready: ${dataChannelReady}`);
147
149
  });
148
150
 
149
- console.log('\nDiscovered Peers:');
151
+ this.debug.log('\nDiscovered Peers:');
150
152
  discoveredPeers.forEach(peer => {
151
153
  const shouldInitiate = this.mesh.peerId < peer.peerId;
152
154
  const isConnected = this.connectionManager.hasPeer(peer.peerId);
153
- console.log(` ${peer.peerId.substring(0, 8)}... - ShouldInitiate: ${shouldInitiate}, IsConnected: ${isConnected}`);
155
+ this.debug.log(` ${peer.peerId.substring(0, 8)}... - ShouldInitiate: ${shouldInitiate}, IsConnected: ${isConnected}`);
154
156
  });
155
157
 
156
- console.log('\nConnection Attempts:');
158
+ this.debug.log('\nConnection Attempts:');
157
159
  this.connectionManager.connectionAttempts.forEach((attempts, peerId) => {
158
- console.log(` ${peerId.substring(0, 8)}... - Attempts: ${attempts}/${this.connectionManager.maxConnectionAttempts}`);
160
+ this.debug.log(` ${peerId.substring(0, 8)}... - Attempts: ${attempts}/${this.connectionManager.maxConnectionAttempts}`);
159
161
  });
160
162
 
161
- console.log('\nEviction Status:');
162
- console.log(` Eviction Strategy: ${this.mesh.evictionStrategy ? 'enabled' : 'disabled'}`);
163
- console.log(` XOR Routing: ${this.mesh.xorRouting ? 'enabled' : 'disabled'}`);
163
+ this.debug.log('\nEviction Status:');
164
+ this.debug.log(` Eviction Strategy: ${this.mesh.evictionStrategy ? 'enabled' : 'disabled'}`);
165
+ this.debug.log(` XOR Routing: ${this.mesh.xorRouting ? 'enabled' : 'disabled'}`);
164
166
 
165
- console.log('=== END DEBUG ===\n');
167
+ this.debug.log('=== END DEBUG ===\n');
166
168
 
167
169
  return {
168
170
  connectedPeers,