peerpigeon 0.1.1 → 1.0.2

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 (77) hide show
  1. package/README.md +141 -1365
  2. package/cli.js +1382 -0
  3. package/dist/peerpigeon-browser.js +12450 -0
  4. package/dist/peerpigeon-browser.js.map +7 -0
  5. package/docs/API_DOCUMENTATION.md +1976 -0
  6. package/docs/BOOTSTRAP_HUBS.md +319 -0
  7. package/docs/CLI_README.md +371 -0
  8. package/docs/CUSTOM_HUB_NAMESPACES.md +182 -0
  9. package/docs/CUSTOM_NAMESPACE_IMPLEMENTATION.md +187 -0
  10. package/docs/DOCUMENTATION_REORGANIZATION.md +172 -0
  11. package/docs/HUB_QUICK_REF.md +108 -0
  12. package/docs/HUB_SCRIPTS.md +341 -0
  13. package/docs/HUB_SYSTEM.md +599 -0
  14. package/docs/MIGRATION_COMPLETE.md +93 -0
  15. package/docs/NETWORK_NAMESPACES.md +212 -0
  16. package/docs/PIGEONRTC_MIGRATION.md +280 -0
  17. package/docs/README.md +108 -0
  18. package/docs/SELECTIVE_STREAMING_GUIDE.md +427 -0
  19. package/docs/TEST_CLEANUP_COMPLETE.md +80 -0
  20. package/examples/bootstrap-hub-example.js +292 -0
  21. package/examples/browser/README.md +278 -0
  22. package/examples/browser/app.js +2426 -49
  23. package/examples/browser/index.html +481 -630
  24. package/examples/browser/minimal/README.md +55 -0
  25. package/examples/browser/minimal/index.html +75 -0
  26. package/examples/browser/minimal/minimal.js +234 -0
  27. package/examples/browser/styles.css +687 -1425
  28. package/examples/browser/test-bundle.html +35 -0
  29. package/examples/browser-2/index.html +30 -0
  30. package/examples/browser-2/package.json +21 -0
  31. package/examples/browser-2/src/App-simple.vue +21 -0
  32. package/examples/browser-2/src/App.vue +420 -0
  33. package/examples/browser-2/src/components/CryptoView.vue +1661 -0
  34. package/examples/browser-2/src/components/DHTView.vue +1281 -0
  35. package/examples/browser-2/src/components/DebugView.vue +1773 -0
  36. package/examples/browser-2/src/components/MediaView.vue +1048 -0
  37. package/examples/browser-2/src/components/MessagingView.vue +775 -0
  38. package/examples/browser-2/src/components/NetworkView.vue +930 -0
  39. package/examples/browser-2/src/components/StorageView.vue +1376 -0
  40. package/examples/browser-2/src/main.js +5 -0
  41. package/examples/browser-2/src/stores/peerpigeon.js +895 -0
  42. package/examples/browser-2/vite.config.js +6 -0
  43. package/examples/hub-example.js +189 -0
  44. package/examples/multi-hub-test.js +186 -0
  45. package/examples/node/README.md +2 -2
  46. package/examples/programmatic-namespace-example.js +108 -0
  47. package/examples/selective-streaming-demo.html +698 -0
  48. package/examples/standard-events-demo.js +126 -0
  49. package/index.js +7 -1
  50. package/package.json +34 -16
  51. package/server/index.js +1058 -98
  52. package/server/start.js +52 -0
  53. package/src/ConnectionManager.js +239 -225
  54. package/src/CryptoManager.js +147 -18
  55. package/src/DebugLogger.js +0 -7
  56. package/src/DistributedStorageManager.js +224 -205
  57. package/src/EnvironmentDetector.js +88 -4
  58. package/src/EventEmitter.js +79 -1
  59. package/src/EvictionManager.js +10 -9
  60. package/src/GossipManager.js +163 -11
  61. package/src/MediaManager.js +12 -2
  62. package/src/PeerConnection.js +343 -311
  63. package/src/PeerPigeonMesh.js +576 -135
  64. package/src/PigeonRTC-browser.js +247 -0
  65. package/src/SignalingClient.js +14 -243
  66. package/src/SignalingHandler.js +31 -10
  67. package/src/StorageManager.js +0 -39
  68. package/src/WebDHT-Simple.js +389 -0
  69. package/src/WebDHT.js +363 -1165
  70. package/src/browser-entry.js +58 -0
  71. package/examples/browser/assets/images/favicon.png +0 -0
  72. package/examples/browser/assets/images/pigeon.png +0 -0
  73. package/examples/browser/crypto-test.html +0 -427
  74. package/examples/browser/ui-old.js +0 -1926
  75. package/examples/browser/ui.js +0 -3391
  76. package/websocket-server/README.md +0 -247
  77. package/websocket-server/server.js +0 -391
@@ -0,0 +1,389 @@
1
+ import { EventEmitter } from './EventEmitter.js';
2
+ import DebugLogger from './DebugLogger.js';
3
+
4
+ /**
5
+ * SimpleWebDHT - Efficient Distributed Hash Table for millions of WebRTC peers
6
+ *
7
+ * Design principles:
8
+ * 1. SIMPLE: Minimal complexity, maximum reliability
9
+ * 2. EFFICIENT: Optimized for millions of peers with O(log N) operations
10
+ * 3. SCALABLE: Consistent hashing with automatic load balancing
11
+ * 4. RELIABLE: Gossip-based replication with eventual consistency
12
+ *
13
+ * Key features:
14
+ * - Consistent hashing for efficient key distribution
15
+ * - Minimal routing table (only closest peers)
16
+ * - Gossip-based replication (fire-and-forget)
17
+ * - Automatic peer discovery and failure handling
18
+ * - No complex Kademlia routing - just simple consistent hashing
19
+ */
20
+ export class SimpleWebDHT extends EventEmitter {
21
+ constructor(mesh) {
22
+ super();
23
+ this.debug = DebugLogger.create('SimpleWebDHT');
24
+ this.mesh = mesh;
25
+ this.peerId = mesh.peerId;
26
+
27
+ // Simple local storage
28
+ this.storage = new Map();
29
+
30
+ // Minimal routing: just track closest peers for efficient forwarding
31
+ this.closestPeers = new Set();
32
+
33
+ // Simple replication factor
34
+ this.replicationFactor = 3;
35
+
36
+ // Hash ring position for this peer
37
+ this.hashPosition = this.hashPeerId(this.peerId);
38
+
39
+ this.debug.log(`SimpleWebDHT initialized for peer ${this.peerId.substring(0, 8)} at position ${this.hashPosition.toString(16).substring(0, 8)}`);
40
+
41
+ this.setupMessageHandling();
42
+ this.startMaintenance();
43
+ }
44
+
45
+ /**
46
+ * Simple hash function for consistent hashing
47
+ */
48
+ async hash(data) {
49
+ const encoder = new TextEncoder();
50
+ const dataBuffer = encoder.encode(typeof data === 'string' ? data : JSON.stringify(data));
51
+ const hashBuffer = await crypto.subtle.digest('SHA-256', dataBuffer);
52
+ const hashArray = new Uint8Array(hashBuffer);
53
+
54
+ // Convert to number for position on hash ring
55
+ let hash = 0;
56
+ for (let i = 0; i < 4; i++) {
57
+ hash = (hash * 256 + hashArray[i]) >>> 0;
58
+ }
59
+ return hash;
60
+ }
61
+
62
+ /**
63
+ * Hash peer ID to position on ring
64
+ */
65
+ hashPeerId(peerId) {
66
+ // Simple deterministic hash of peer ID
67
+ let hash = 0;
68
+ for (let i = 0; i < peerId.length; i++) {
69
+ hash = ((hash << 5) - hash + peerId.charCodeAt(i)) >>> 0;
70
+ }
71
+ return hash;
72
+ }
73
+
74
+ /**
75
+ * Calculate distance between two positions on hash ring
76
+ */
77
+ ringDistance(pos1, pos2) {
78
+ const diff = Math.abs(pos1 - pos2);
79
+ const maxUint32 = 0xFFFFFFFF;
80
+ return Math.min(diff, maxUint32 - diff);
81
+ }
82
+
83
+ /**
84
+ * Find closest peers to a hash position
85
+ */
86
+ findClosestPeers(targetHash, count = this.replicationFactor) {
87
+ const connectedPeers = Array.from(this.mesh.connectionManager.peers.keys())
88
+ .filter(peerId => this.mesh.connectionManager.peers.get(peerId).getStatus() === 'connected');
89
+
90
+ if (connectedPeers.length === 0) {
91
+ return [];
92
+ }
93
+
94
+ // Calculate distances and sort
95
+ const peersWithDistance = connectedPeers.map(peerId => ({
96
+ peerId,
97
+ distance: this.ringDistance(targetHash, this.hashPeerId(peerId))
98
+ }));
99
+
100
+ peersWithDistance.sort((a, b) => a.distance - b.distance);
101
+
102
+ return peersWithDistance.slice(0, count).map(p => p.peerId);
103
+ }
104
+
105
+ /**
106
+ * Store key-value pair
107
+ */
108
+ async put(key, value) {
109
+ const keyHash = await this.hash(key);
110
+
111
+ const storeData = {
112
+ key,
113
+ value,
114
+ timestamp: Date.now(),
115
+ publisher: this.peerId
116
+ };
117
+
118
+ // Always store locally first
119
+ this.storage.set(key, storeData);
120
+ this.debug.log(`PUT: Stored ${key} locally`);
121
+
122
+ // Find closest peers for replication
123
+ const targetPeers = this.findClosestPeers(keyHash, this.replicationFactor);
124
+
125
+ // Simple gossip replication - fire and forget
126
+ const replicationPromises = targetPeers.map(async (peerId) => {
127
+ if (peerId !== this.peerId) {
128
+ try {
129
+ this.sendMessage(peerId, 'dht_store', storeData);
130
+ } catch (error) {
131
+ // Silent failure - gossip is best effort
132
+ this.debug.warn(`Replication to ${peerId.substring(0, 8)} failed:`, error.message);
133
+ }
134
+ }
135
+ });
136
+
137
+ // Don't wait for replication - fire and forget for speed
138
+ Promise.allSettled(replicationPromises);
139
+
140
+ this.debug.log(`PUT: ${key} replicated to ${targetPeers.length} peers`);
141
+ return true;
142
+ }
143
+
144
+ /**
145
+ * Retrieve value by key
146
+ */
147
+ async get(key, options = {}) {
148
+ const forceRefresh = options.forceRefresh || false;
149
+
150
+ // Check local storage first (unless force refresh)
151
+ if (!forceRefresh && this.storage.has(key)) {
152
+ const data = this.storage.get(key);
153
+ this.debug.log(`GET: Found ${key} locally`);
154
+ return data.value;
155
+ }
156
+
157
+ // If not found locally or force refresh, query the network
158
+ const keyHash = await this.hash(key);
159
+ const targetPeers = this.findClosestPeers(keyHash, this.replicationFactor);
160
+
161
+ this.debug.log(`GET: Querying ${targetPeers.length} peers for ${key}`);
162
+
163
+ // Query peers in parallel
164
+ const queryPromises = targetPeers.map(async (peerId) => {
165
+ if (peerId === this.peerId) return null;
166
+
167
+ try {
168
+ return await this.queryPeer(peerId, key);
169
+ } catch (error) {
170
+ this.debug.warn(`Query to ${peerId.substring(0, 8)} failed:`, error.message);
171
+ return null;
172
+ }
173
+ });
174
+
175
+ const results = await Promise.allSettled(queryPromises);
176
+
177
+ // Find the first successful result
178
+ for (const result of results) {
179
+ if (result.status === 'fulfilled' && result.value) {
180
+ const data = result.value;
181
+
182
+ // Cache locally for future use
183
+ this.storage.set(key, data);
184
+
185
+ this.debug.log(`GET: Found ${key} from network`);
186
+ return data.value;
187
+ }
188
+ }
189
+
190
+ this.debug.log(`GET: ${key} not found`);
191
+ return null;
192
+ }
193
+
194
+ /**
195
+ * Query a specific peer for a key
196
+ */
197
+ async queryPeer(peerId, key) {
198
+ return new Promise((resolve, reject) => {
199
+ const requestId = Math.random().toString(36).substring(7);
200
+
201
+ // Set timeout for query
202
+ const timeout = setTimeout(() => {
203
+ this.responseHandlers.delete(requestId);
204
+ reject(new Error('Query timeout'));
205
+ }, 5000);
206
+
207
+ // Store response handler
208
+ this.responseHandlers.set(requestId, (response) => {
209
+ clearTimeout(timeout);
210
+ this.responseHandlers.delete(requestId);
211
+
212
+ if (response.found) {
213
+ resolve(response.data);
214
+ } else {
215
+ resolve(null);
216
+ }
217
+ });
218
+
219
+ // Send query
220
+ this.sendMessage(peerId, 'dht_query', { key, requestId });
221
+ });
222
+ }
223
+
224
+ /**
225
+ * Send message to peer through connection manager
226
+ */
227
+ sendMessage(peerId, type, data) {
228
+ const message = {
229
+ type: 'dht',
230
+ messageType: type,
231
+ data,
232
+ from: this.peerId,
233
+ timestamp: Date.now()
234
+ };
235
+
236
+ const peer = this.mesh.connectionManager.peers.get(peerId);
237
+ if (peer && peer.getStatus() === 'connected') {
238
+ peer.send(message);
239
+ } else {
240
+ throw new Error(`Peer ${peerId} not connected`);
241
+ }
242
+ }
243
+
244
+ /**
245
+ * Setup message handling
246
+ */
247
+ setupMessageHandling() {
248
+ this.responseHandlers = new Map();
249
+ }
250
+
251
+ /**
252
+ * Handle incoming DHT message
253
+ */
254
+ async handleMessage(message, fromPeerId) {
255
+ const { messageType, data } = message;
256
+
257
+ switch (messageType) {
258
+ case 'dht_store':
259
+ this.handleStore(data, fromPeerId);
260
+ break;
261
+
262
+ case 'dht_query':
263
+ await this.handleQuery(data, fromPeerId);
264
+ break;
265
+
266
+ case 'dht_query_response':
267
+ this.handleQueryResponse(data);
268
+ break;
269
+
270
+ default:
271
+ this.debug.warn(`Unknown DHT message type: ${messageType}`);
272
+ }
273
+ }
274
+
275
+ /**
276
+ * Handle store request from peer
277
+ */
278
+ handleStore(data, fromPeerId) {
279
+ const { key, value, timestamp, publisher } = data;
280
+
281
+ // Simple conflict resolution: latest timestamp wins
282
+ if (this.storage.has(key)) {
283
+ const existing = this.storage.get(key);
284
+ if (existing.timestamp >= timestamp) {
285
+ return; // Ignore older data
286
+ }
287
+ }
288
+
289
+ // Store the data
290
+ this.storage.set(key, { key, value, timestamp, publisher });
291
+ this.debug.log(`STORE: Received ${key} from ${fromPeerId.substring(0, 8)}`);
292
+ }
293
+
294
+ /**
295
+ * Handle query request from peer
296
+ */
297
+ async handleQuery(data, fromPeerId) {
298
+ const { key, requestId } = data;
299
+
300
+ const response = {
301
+ requestId,
302
+ found: false,
303
+ data: null
304
+ };
305
+
306
+ if (this.storage.has(key)) {
307
+ response.found = true;
308
+ response.data = this.storage.get(key);
309
+ }
310
+
311
+ // Send response
312
+ this.sendMessage(fromPeerId, 'dht_query_response', response);
313
+ }
314
+
315
+ /**
316
+ * Handle query response
317
+ */
318
+ handleQueryResponse(data) {
319
+ const { requestId } = data;
320
+ const handler = this.responseHandlers.get(requestId);
321
+
322
+ if (handler) {
323
+ handler(data);
324
+ }
325
+ }
326
+
327
+ /**
328
+ * Update closest peers for efficient routing
329
+ */
330
+ updateClosestPeers() {
331
+ const connectedPeers = Array.from(this.mesh.connectionManager.peers.keys())
332
+ .filter(peerId => this.mesh.connectionManager.peers.get(peerId).getStatus() === 'connected');
333
+
334
+ // Keep track of closest peers for efficient routing
335
+ const peersWithDistance = connectedPeers.map(peerId => ({
336
+ peerId,
337
+ distance: this.ringDistance(this.hashPosition, this.hashPeerId(peerId))
338
+ }));
339
+
340
+ peersWithDistance.sort((a, b) => a.distance - b.distance);
341
+
342
+ // Keep top 10 closest peers for efficient routing
343
+ this.closestPeers = new Set(
344
+ peersWithDistance.slice(0, 10).map(p => p.peerId)
345
+ );
346
+ }
347
+
348
+ /**
349
+ * Periodic maintenance
350
+ */
351
+ startMaintenance() {
352
+ // Update routing table every 30 seconds
353
+ setInterval(() => {
354
+ this.updateClosestPeers();
355
+ }, 30000);
356
+
357
+ // Clean up old data every 5 minutes
358
+ setInterval(() => {
359
+ this.cleanupOldData();
360
+ }, 300000);
361
+ }
362
+
363
+ /**
364
+ * Clean up old data (optional TTL support)
365
+ */
366
+ cleanupOldData() {
367
+ const now = Date.now();
368
+ const maxAge = 24 * 60 * 60 * 1000; // 24 hours
369
+
370
+ for (const [key, data] of this.storage) {
371
+ if (now - data.timestamp > maxAge) {
372
+ this.storage.delete(key);
373
+ this.debug.log(`Cleaned up old data: ${key}`);
374
+ }
375
+ }
376
+ }
377
+
378
+ /**
379
+ * Get DHT statistics
380
+ */
381
+ getStats() {
382
+ return {
383
+ localKeys: this.storage.size,
384
+ connectedPeers: this.mesh.connectionManager.getConnectedPeerCount(),
385
+ closestPeers: this.closestPeers.size,
386
+ hashPosition: this.hashPosition.toString(16).substring(0, 8)
387
+ };
388
+ }
389
+ }