peerpigeon 0.0.5 → 0.0.6
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.
- package/README.md +144 -4
- package/examples/browser/app.js +40 -40
- package/examples/browser/crypto-test.html +427 -0
- package/examples/browser/index.html +161 -0
- package/examples/browser/styles.css +216 -0
- package/examples/browser/ui-old.js +1704 -1708
- package/examples/browser/ui.js +2226 -1692
- package/examples/node/test-crypto-media.js +137 -0
- package/examples/node/test-crypto.js +146 -0
- package/examples/node/test-node.js +114 -115
- package/package.json +51 -8
- package/src/CleanupManager.js +154 -154
- package/src/ConnectionManager.js +772 -753
- package/src/CryptoManager.js +612 -0
- package/src/EnvironmentDetector.js +381 -381
- package/src/EventEmitter.js +19 -19
- package/src/EvictionManager.js +211 -211
- package/src/GossipManager.js +447 -366
- package/src/MediaManager.js +214 -214
- package/src/MeshOptimizer.js +165 -166
- package/src/PeerConnection.js +847 -941
- package/src/PeerDiscovery.js +255 -259
- package/src/PeerPigeonMesh.js +1082 -723
- package/src/SignalingClient.js +517 -518
- package/src/SignalingHandler.js +295 -295
- package/src/StorageManager.js +105 -105
- package/src/TimerUtils.js +29 -29
- package/src/WebDHT.js +951 -951
- package/websocket-server/server.js +360 -362
package/src/PeerDiscovery.js
CHANGED
|
@@ -2,303 +2,299 @@ import { EventEmitter } from './EventEmitter.js';
|
|
|
2
2
|
import { environmentDetector } from './EnvironmentDetector.js';
|
|
3
3
|
|
|
4
4
|
export class PeerDiscovery extends EventEmitter {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
5
|
+
constructor(peerId, options = {}) {
|
|
6
|
+
super();
|
|
7
|
+
this.peerId = peerId;
|
|
8
|
+
this.discoveredPeers = new Map();
|
|
9
|
+
this.connectionAttempts = new Map();
|
|
10
|
+
this.cleanupInterval = null;
|
|
11
|
+
this.meshOptimizationTimeout = null;
|
|
12
|
+
this.autoDiscovery = options.autoDiscovery ?? true;
|
|
13
|
+
this.evictionStrategy = options.evictionStrategy ?? true;
|
|
14
|
+
this.xorRouting = options.xorRouting ?? true;
|
|
15
|
+
this.minPeers = options.minPeers ?? 0;
|
|
16
|
+
this.maxPeers = options.maxPeers ?? 10;
|
|
17
|
+
}
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
start() {
|
|
20
|
+
this.startCleanupInterval();
|
|
21
|
+
}
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (this.meshOptimizationTimeout) {
|
|
30
|
-
clearTimeout(this.meshOptimizationTimeout);
|
|
31
|
-
this.meshOptimizationTimeout = null;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
this.discoveredPeers.clear();
|
|
35
|
-
this.connectionAttempts.clear();
|
|
23
|
+
stop() {
|
|
24
|
+
if (this.cleanupInterval) {
|
|
25
|
+
clearInterval(this.cleanupInterval);
|
|
26
|
+
this.cleanupInterval = null;
|
|
36
27
|
}
|
|
37
28
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
// Update timestamp but don't emit events or trigger connections
|
|
42
|
-
this.discoveredPeers.set(peerId, Date.now());
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
this.discoveredPeers.set(peerId, Date.now());
|
|
47
|
-
this.emit('peerDiscovered', { peerId });
|
|
48
|
-
|
|
49
|
-
console.log(`Discovered peer ${peerId.substring(0, 8)}...`);
|
|
50
|
-
|
|
51
|
-
// Only auto-connect if we should initiate to this peer and we're not already trying
|
|
52
|
-
if (this.autoDiscovery && this.shouldInitiateConnection(peerId) && !this.connectionAttempts.has(peerId)) {
|
|
53
|
-
console.log(`Considering connection to ${peerId.substring(0, 8)}...`);
|
|
54
|
-
|
|
55
|
-
const canAccept = this.canAcceptMorePeers();
|
|
56
|
-
if (canAccept) {
|
|
57
|
-
console.log(`Connecting to ${peerId.substring(0, 8)}...`);
|
|
58
|
-
this.emit('connectToPeer', { peerId });
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
this.scheduleMeshOptimization();
|
|
29
|
+
if (this.meshOptimizationTimeout) {
|
|
30
|
+
clearTimeout(this.meshOptimizationTimeout);
|
|
31
|
+
this.meshOptimizationTimeout = null;
|
|
63
32
|
}
|
|
64
33
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
34
|
+
this.discoveredPeers.clear();
|
|
35
|
+
this.connectionAttempts.clear();
|
|
36
|
+
}
|
|
69
37
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
38
|
+
addDiscoveredPeer(peerId) {
|
|
39
|
+
// Skip if we already know about this peer (prevent duplicate processing)
|
|
40
|
+
if (this.discoveredPeers.has(peerId)) {
|
|
41
|
+
// Update timestamp but don't emit events or trigger connections
|
|
42
|
+
this.discoveredPeers.set(peerId, Date.now());
|
|
43
|
+
return;
|
|
73
44
|
}
|
|
74
45
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
46
|
+
this.discoveredPeers.set(peerId, Date.now());
|
|
47
|
+
this.emit('peerDiscovered', { peerId });
|
|
78
48
|
|
|
79
|
-
|
|
80
|
-
this.connectionAttempts.delete(peerId);
|
|
81
|
-
}
|
|
49
|
+
console.log(`Discovered peer ${peerId.substring(0, 8)}...`);
|
|
82
50
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
const byte1 = parseInt(peerId1.substr(i, 2), 16);
|
|
87
|
-
const byte2 = parseInt(peerId2.substr(i, 2), 16);
|
|
88
|
-
const xor = byte1 ^ byte2;
|
|
89
|
-
distance = (distance << 8n) | BigInt(xor);
|
|
90
|
-
}
|
|
91
|
-
return distance;
|
|
92
|
-
}
|
|
51
|
+
// Only auto-connect if we should initiate to this peer and we're not already trying
|
|
52
|
+
if (this.autoDiscovery && this.shouldInitiateConnection(peerId) && !this.connectionAttempts.has(peerId)) {
|
|
53
|
+
console.log(`Considering connection to ${peerId.substring(0, 8)}...`);
|
|
93
54
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// Check current connection count to handle isolation
|
|
100
|
-
this.emit('checkCapacity');
|
|
101
|
-
const currentConnectionCount = this._currentConnectionCount || 0;
|
|
102
|
-
|
|
103
|
-
// Standard rule: Lexicographically larger peer ID initiates
|
|
104
|
-
const lexicographicShouldInitiate = this.peerId > targetPeerId;
|
|
105
|
-
|
|
106
|
-
// CRITICAL FIX: If we have no connections, override lexicographic rule to avoid isolation
|
|
107
|
-
if (currentConnectionCount === 0 && this.discoveredPeers.size > 0) {
|
|
108
|
-
// For completely isolated peers, be more aggressive about connecting
|
|
109
|
-
const discoveredPeers = Array.from(this.discoveredPeers.keys());
|
|
110
|
-
const naturalInitiators = discoveredPeers.filter(peerId => this.peerId > peerId);
|
|
111
|
-
|
|
112
|
-
// First priority: peers where we'd naturally be the initiator
|
|
113
|
-
if (naturalInitiators.length > 0 && naturalInitiators.includes(targetPeerId)) {
|
|
114
|
-
console.log(`shouldInitiateConnection: Isolation override (natural) - ${this.peerId.substring(0, 8)}... will initiate to ${targetPeerId.substring(0, 8)}...`);
|
|
115
|
-
return true;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// Second priority: if no natural initiators, try the closest 3 peers by XOR distance
|
|
119
|
-
if (naturalInitiators.length === 0) {
|
|
120
|
-
const sortedByDistance = discoveredPeers.sort((a, b) => {
|
|
121
|
-
const distA = this.calculateXorDistance(this.peerId, a);
|
|
122
|
-
const distB = this.calculateXorDistance(this.peerId, b);
|
|
123
|
-
return distA < distB ? -1 : 1;
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
// Try to connect to the closest 3 peers (or all if less than 3)
|
|
127
|
-
const closestPeers = sortedByDistance.slice(0, Math.min(3, sortedByDistance.length));
|
|
128
|
-
if (closestPeers.includes(targetPeerId)) {
|
|
129
|
-
const index = closestPeers.indexOf(targetPeerId);
|
|
130
|
-
console.log(`shouldInitiateConnection: Isolation override (closest ${index + 1}) - ${this.peerId.substring(0, 8)}... will initiate to ${targetPeerId.substring(0, 8)}...`);
|
|
131
|
-
return true;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// Third priority: if we still have no connections and have attempted several peers,
|
|
136
|
-
// be even more aggressive and try ANY peer to break isolation
|
|
137
|
-
const attemptedConnections = this.connectionAttempts.size;
|
|
138
|
-
if (attemptedConnections >= 2 && discoveredPeers.includes(targetPeerId)) {
|
|
139
|
-
console.log(`shouldInitiateConnection: Isolation override (desperate) - ${this.peerId.substring(0, 8)}... will initiate to ${targetPeerId.substring(0, 8)}... (${attemptedConnections} attempts failed)`);
|
|
140
|
-
return true;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
console.log(`shouldInitiateConnection: ${this.peerId.substring(0, 8)}... > ${targetPeerId.substring(0, 8)}... = ${lexicographicShouldInitiate}`);
|
|
145
|
-
return lexicographicShouldInitiate;
|
|
55
|
+
const canAccept = this.canAcceptMorePeers();
|
|
56
|
+
if (canAccept) {
|
|
57
|
+
console.log(`Connecting to ${peerId.substring(0, 8)}...`);
|
|
58
|
+
this.emit('connectToPeer', { peerId });
|
|
59
|
+
}
|
|
146
60
|
}
|
|
147
61
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
}
|
|
62
|
+
this.scheduleMeshOptimization();
|
|
63
|
+
}
|
|
151
64
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
65
|
+
// Update connection attempts tracking (no complex isolation logic needed)
|
|
66
|
+
onConnectionEstablished() {
|
|
67
|
+
console.log('Connection established');
|
|
68
|
+
}
|
|
156
69
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
70
|
+
removeDiscoveredPeer(peerId) {
|
|
71
|
+
this.discoveredPeers.delete(peerId);
|
|
72
|
+
this.connectionAttempts.delete(peerId);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
trackConnectionAttempt(peerId) {
|
|
76
|
+
this.connectionAttempts.set(peerId, Date.now());
|
|
77
|
+
}
|
|
161
78
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
79
|
+
clearConnectionAttempt(peerId) {
|
|
80
|
+
this.connectionAttempts.delete(peerId);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
calculateXorDistance(peerId1, peerId2) {
|
|
84
|
+
let distance = 0n;
|
|
85
|
+
for (let i = 0; i < Math.min(peerId1.length, peerId2.length); i += 2) {
|
|
86
|
+
const byte1 = parseInt(peerId1.substr(i, 2), 16);
|
|
87
|
+
const byte2 = parseInt(peerId2.substr(i, 2), 16);
|
|
88
|
+
const xor = byte1 ^ byte2;
|
|
89
|
+
distance = (distance << 8n) | BigInt(xor);
|
|
166
90
|
}
|
|
91
|
+
return distance;
|
|
92
|
+
}
|
|
167
93
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
console.log('Optimizing mesh connections...');
|
|
172
|
-
|
|
173
|
-
// Find unconnected peers that we should initiate connections to
|
|
174
|
-
const unconnectedPeers = Array.from(this.discoveredPeers.keys())
|
|
175
|
-
.filter(peerId => {
|
|
176
|
-
const notConnected = !currentPeers.has(peerId);
|
|
177
|
-
const notConnecting = !this.connectionAttempts.has(peerId);
|
|
178
|
-
const shouldInitiate = this.shouldInitiateConnection(peerId);
|
|
179
|
-
|
|
180
|
-
return notConnected && notConnecting && shouldInitiate;
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
if (unconnectedPeers.length === 0) {
|
|
184
|
-
console.log('No unconnected peers to optimize');
|
|
185
|
-
return;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
// Sort by XOR distance to prioritize closer peers (if XOR routing enabled)
|
|
189
|
-
if (this.xorRouting) {
|
|
190
|
-
unconnectedPeers.sort((a, b) => {
|
|
191
|
-
const distA = this.calculateXorDistance(this.peerId, a);
|
|
192
|
-
const distB = this.calculateXorDistance(this.peerId, b);
|
|
193
|
-
return distA < distB ? -1 : 1;
|
|
194
|
-
});
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
this.emit('optimizeConnections', { unconnectedPeers });
|
|
94
|
+
shouldInitiateConnection(targetPeerId) {
|
|
95
|
+
if (this.connectionAttempts.has(targetPeerId)) {
|
|
96
|
+
return false;
|
|
198
97
|
}
|
|
199
98
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
99
|
+
// Check current connection count to handle isolation
|
|
100
|
+
this.emit('checkCapacity');
|
|
101
|
+
const currentConnectionCount = this._currentConnectionCount || 0;
|
|
102
|
+
|
|
103
|
+
// Standard rule: Lexicographically larger peer ID initiates
|
|
104
|
+
const lexicographicShouldInitiate = this.peerId > targetPeerId;
|
|
105
|
+
|
|
106
|
+
// CRITICAL FIX: If we have no connections, override lexicographic rule to avoid isolation
|
|
107
|
+
if (currentConnectionCount === 0 && this.discoveredPeers.size > 0) {
|
|
108
|
+
// For completely isolated peers, be more aggressive about connecting
|
|
109
|
+
const discoveredPeers = Array.from(this.discoveredPeers.keys());
|
|
110
|
+
const naturalInitiators = discoveredPeers.filter(peerId => this.peerId > peerId);
|
|
111
|
+
|
|
112
|
+
// First priority: peers where we'd naturally be the initiator
|
|
113
|
+
if (naturalInitiators.length > 0 && naturalInitiators.includes(targetPeerId)) {
|
|
114
|
+
console.log(`shouldInitiateConnection: Isolation override (natural) - ${this.peerId.substring(0, 8)}... will initiate to ${targetPeerId.substring(0, 8)}...`);
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Second priority: if no natural initiators, try the closest 3 peers by XOR distance
|
|
119
|
+
if (naturalInitiators.length === 0) {
|
|
120
|
+
const sortedByDistance = discoveredPeers.sort((a, b) => {
|
|
121
|
+
const distA = this.calculateXorDistance(this.peerId, a);
|
|
122
|
+
const distB = this.calculateXorDistance(this.peerId, b);
|
|
123
|
+
return distA < distB ? -1 : 1;
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
// Try to connect to the closest 3 peers (or all if less than 3)
|
|
127
|
+
const closestPeers = sortedByDistance.slice(0, Math.min(3, sortedByDistance.length));
|
|
128
|
+
if (closestPeers.includes(targetPeerId)) {
|
|
129
|
+
const index = closestPeers.indexOf(targetPeerId);
|
|
130
|
+
console.log(`shouldInitiateConnection: Isolation override (closest ${index + 1}) - ${this.peerId.substring(0, 8)}... will initiate to ${targetPeerId.substring(0, 8)}...`);
|
|
131
|
+
return true;
|
|
203
132
|
}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
},
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Third priority: if we still have no connections and have attempted several peers,
|
|
136
|
+
// be even more aggressive and try ANY peer to break isolation
|
|
137
|
+
const attemptedConnections = this.connectionAttempts.size;
|
|
138
|
+
if (attemptedConnections >= 2 && discoveredPeers.includes(targetPeerId)) {
|
|
139
|
+
console.log(`shouldInitiateConnection: Isolation override (desperate) - ${this.peerId.substring(0, 8)}... will initiate to ${targetPeerId.substring(0, 8)}... (${attemptedConnections} attempts failed)`);
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
211
142
|
}
|
|
212
143
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
144
|
+
console.log(`shouldInitiateConnection: ${this.peerId.substring(0, 8)}... > ${targetPeerId.substring(0, 8)}... = ${lexicographicShouldInitiate}`);
|
|
145
|
+
return lexicographicShouldInitiate;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
isAttemptingConnection(peerId) {
|
|
149
|
+
return this.connectionAttempts.has(peerId);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
shouldEvictForPeer(newPeerId) {
|
|
153
|
+
if (!this.evictionStrategy || !this.xorRouting) {
|
|
154
|
+
return null;
|
|
223
155
|
}
|
|
224
156
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
this.discoveredPeers.forEach((timestamp, peerId) => {
|
|
231
|
-
if (now - timestamp > staleThreshold) {
|
|
232
|
-
this.discoveredPeers.delete(peerId);
|
|
233
|
-
this.connectionAttempts.delete(peerId);
|
|
234
|
-
removedCount++;
|
|
235
|
-
console.log('Removed stale peer:', peerId.substring(0, 8));
|
|
236
|
-
}
|
|
237
|
-
});
|
|
157
|
+
// This would need access to current peers, so we'll emit an event instead
|
|
158
|
+
this.emit('checkEviction', { newPeerId });
|
|
159
|
+
return this._shouldEvictForPeer ?? null; // Use stored value from mesh, default to null
|
|
160
|
+
}
|
|
238
161
|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
162
|
+
canAcceptMorePeers() {
|
|
163
|
+
// This needs to be determined by the mesh
|
|
164
|
+
this.emit('checkCapacity');
|
|
165
|
+
return this._canAcceptMorePeers ?? true; // Use stored value from mesh, default to true
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
optimizeMeshConnections(currentPeers) {
|
|
169
|
+
if (!this.autoDiscovery) return;
|
|
170
|
+
|
|
171
|
+
console.log('Optimizing mesh connections...');
|
|
172
|
+
|
|
173
|
+
// Find unconnected peers that we should initiate connections to
|
|
174
|
+
const unconnectedPeers = Array.from(this.discoveredPeers.keys())
|
|
175
|
+
.filter(peerId => {
|
|
176
|
+
const notConnected = !currentPeers.has(peerId);
|
|
177
|
+
const notConnecting = !this.connectionAttempts.has(peerId);
|
|
178
|
+
const shouldInitiate = this.shouldInitiateConnection(peerId);
|
|
179
|
+
|
|
180
|
+
return notConnected && notConnecting && shouldInitiate;
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
if (unconnectedPeers.length === 0) {
|
|
184
|
+
console.log('No unconnected peers to optimize');
|
|
185
|
+
return;
|
|
242
186
|
}
|
|
243
187
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
}));
|
|
188
|
+
// Sort by XOR distance to prioritize closer peers (if XOR routing enabled)
|
|
189
|
+
if (this.xorRouting) {
|
|
190
|
+
unconnectedPeers.sort((a, b) => {
|
|
191
|
+
const distA = this.calculateXorDistance(this.peerId, a);
|
|
192
|
+
const distB = this.calculateXorDistance(this.peerId, b);
|
|
193
|
+
return distA < distB ? -1 : 1;
|
|
194
|
+
});
|
|
252
195
|
}
|
|
253
196
|
|
|
254
|
-
|
|
255
|
-
|
|
197
|
+
this.emit('optimizeConnections', { unconnectedPeers });
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
scheduleMeshOptimization() {
|
|
201
|
+
if (this.meshOptimizationTimeout) {
|
|
202
|
+
clearTimeout(this.meshOptimizationTimeout);
|
|
256
203
|
}
|
|
257
204
|
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
205
|
+
// Simple scheduling - optimize every 10-15 seconds
|
|
206
|
+
const delay = 10000 + Math.random() * 5000;
|
|
207
|
+
|
|
208
|
+
this.meshOptimizationTimeout = setTimeout(() => {
|
|
209
|
+
this.emit('optimizeMesh');
|
|
210
|
+
}, delay);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
startCleanupInterval() {
|
|
214
|
+
if (environmentDetector.isBrowser) {
|
|
215
|
+
this.cleanupInterval = window.setInterval(() => {
|
|
216
|
+
this.cleanupStaleDiscoveredPeers();
|
|
217
|
+
}, 30000);
|
|
218
|
+
} else {
|
|
219
|
+
this.cleanupInterval = setInterval(() => {
|
|
220
|
+
this.cleanupStaleDiscoveredPeers();
|
|
221
|
+
}, 30000);
|
|
274
222
|
}
|
|
223
|
+
}
|
|
275
224
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
225
|
+
cleanupStaleDiscoveredPeers() {
|
|
226
|
+
const now = Date.now();
|
|
227
|
+
const staleThreshold = 5 * 60 * 1000;
|
|
228
|
+
let removedCount = 0;
|
|
229
|
+
|
|
230
|
+
this.discoveredPeers.forEach((timestamp, peerId) => {
|
|
231
|
+
if (now - timestamp > staleThreshold) {
|
|
232
|
+
this.discoveredPeers.delete(peerId);
|
|
233
|
+
this.connectionAttempts.delete(peerId);
|
|
234
|
+
removedCount++;
|
|
235
|
+
console.log('Removed stale peer:', peerId.substring(0, 8));
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
if (removedCount > 0) {
|
|
240
|
+
this.emit('peersUpdated', { removedCount });
|
|
280
241
|
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
getDiscoveredPeers() {
|
|
245
|
+
return Array.from(this.discoveredPeers.entries()).map(([peerId, timestamp]) => ({
|
|
246
|
+
peerId,
|
|
247
|
+
timestamp,
|
|
248
|
+
distance: this.calculateXorDistance(this.peerId, peerId),
|
|
249
|
+
isConnecting: this.connectionAttempts.has(peerId),
|
|
250
|
+
isConnected: false // Will be set by the mesh when it has peer info
|
|
251
|
+
}));
|
|
252
|
+
}
|
|
281
253
|
|
|
282
|
-
|
|
283
|
-
|
|
254
|
+
hasPeer(peerId) {
|
|
255
|
+
return this.discoveredPeers.has(peerId);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
setSettings(settings) {
|
|
259
|
+
if (settings.autoDiscovery !== undefined) {
|
|
260
|
+
this.autoDiscovery = settings.autoDiscovery;
|
|
261
|
+
}
|
|
262
|
+
if (settings.evictionStrategy !== undefined) {
|
|
263
|
+
this.evictionStrategy = settings.evictionStrategy;
|
|
284
264
|
}
|
|
265
|
+
if (settings.xorRouting !== undefined) {
|
|
266
|
+
this.xorRouting = settings.xorRouting;
|
|
267
|
+
}
|
|
268
|
+
if (settings.minPeers !== undefined) {
|
|
269
|
+
this.minPeers = settings.minPeers;
|
|
270
|
+
}
|
|
271
|
+
if (settings.maxPeers !== undefined) {
|
|
272
|
+
this.maxPeers = settings.maxPeers;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
285
275
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
console.log('=== PEER DISCOVERY DEBUG ===');
|
|
291
|
-
console.log(`Our peer ID: ${this.peerId.substring(0, 8)}...`);
|
|
292
|
-
console.log(`Discovered peers (${discoveredPeerIds.length}): ${discoveredPeerIds.join(', ')}`);
|
|
293
|
-
console.log(`Connection attempts (${connectionAttempts.length}): ${connectionAttempts.join(', ')}`);
|
|
294
|
-
|
|
295
|
-
// Show which peers we should/shouldn't initiate to
|
|
296
|
-
discoveredPeerIds.forEach(peerId => {
|
|
297
|
-
const fullPeerId = Array.from(this.discoveredPeers.keys()).find(p => p.startsWith(peerId));
|
|
298
|
-
const shouldInitiate = this.shouldInitiateConnection(fullPeerId);
|
|
299
|
-
const comparison = this.peerId > fullPeerId;
|
|
300
|
-
console.log(` ${peerId}...: should initiate = ${shouldInitiate} (${this.peerId.substring(0, 8)}... > ${peerId}... = ${comparison})`);
|
|
301
|
-
});
|
|
302
|
-
console.log('=== END DEBUG ===');
|
|
276
|
+
updateDiscoveryTimestamp(peerId) {
|
|
277
|
+
if (this.discoveredPeers.has(peerId)) {
|
|
278
|
+
this.discoveredPeers.set(peerId, Date.now());
|
|
303
279
|
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
debugCurrentState() {
|
|
283
|
+
const discoveredPeerIds = Array.from(this.discoveredPeers.keys()).map(p => p.substring(0, 8));
|
|
284
|
+
const connectionAttempts = Array.from(this.connectionAttempts.keys()).map(p => p.substring(0, 8));
|
|
285
|
+
|
|
286
|
+
console.log('=== PEER DISCOVERY DEBUG ===');
|
|
287
|
+
console.log(`Our peer ID: ${this.peerId.substring(0, 8)}...`);
|
|
288
|
+
console.log(`Discovered peers (${discoveredPeerIds.length}): ${discoveredPeerIds.join(', ')}`);
|
|
289
|
+
console.log(`Connection attempts (${connectionAttempts.length}): ${connectionAttempts.join(', ')}`);
|
|
290
|
+
|
|
291
|
+
// Show which peers we should/shouldn't initiate to
|
|
292
|
+
discoveredPeerIds.forEach(peerId => {
|
|
293
|
+
const fullPeerId = Array.from(this.discoveredPeers.keys()).find(p => p.startsWith(peerId));
|
|
294
|
+
const shouldInitiate = this.shouldInitiateConnection(fullPeerId);
|
|
295
|
+
const comparison = this.peerId > fullPeerId;
|
|
296
|
+
console.log(` ${peerId}...: should initiate = ${shouldInitiate} (${this.peerId.substring(0, 8)}... > ${peerId}... = ${comparison})`);
|
|
297
|
+
});
|
|
298
|
+
console.log('=== END DEBUG ===');
|
|
299
|
+
}
|
|
304
300
|
}
|