peerpigeon 0.0.1

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