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.
- package/README.md +93 -1
- package/examples/browser/app.js +3 -2
- package/examples/browser/index.html +179 -16
- package/examples/browser/styles.css +279 -47
- package/examples/browser/ui.js +560 -14
- package/examples/debug-configuration.js +171 -0
- package/examples/node/README.md +110 -0
- package/examples/node/basic-storage-example.js +90 -0
- package/examples/node/package.json +17 -0
- package/examples/node/storage-spaces-test.js +329 -0
- package/examples/node/storage-test.js +520 -0
- package/index.js +13 -9
- package/package.json +14 -7
- package/server/index.js +12 -12
- package/src/CleanupManager.js +20 -18
- package/src/ConnectionManager.js +407 -102
- package/src/CryptoManager.js +28 -26
- package/src/DebugLogger.js +211 -0
- package/src/DistributedStorageManager.js +1701 -0
- package/src/EnvironmentDetector.js +3 -1
- package/src/EventEmitter.js +1 -0
- package/src/EvictionManager.js +26 -24
- package/src/GossipManager.js +69 -33
- package/src/MediaManager.js +12 -10
- package/src/MeshOptimizer.js +30 -28
- package/src/PeerConnection.js +621 -159
- package/src/PeerDiscovery.js +19 -17
- package/src/PeerPigeonMesh.js +281 -32
- package/src/SignalingClient.js +59 -44
- package/src/SignalingHandler.js +280 -51
- package/src/StorageManager.js +7 -5
- package/src/TimerUtils.js +1 -0
- package/src/WebDHT.js +125 -123
- package/websocket-server/server.js +31 -103
package/server/index.js
CHANGED
|
@@ -10,7 +10,7 @@ import { EventEmitter } from 'events';
|
|
|
10
10
|
export class PeerPigeonServer extends EventEmitter {
|
|
11
11
|
constructor(options = {}) {
|
|
12
12
|
super();
|
|
13
|
-
|
|
13
|
+
|
|
14
14
|
this.port = options.port || 3000;
|
|
15
15
|
this.host = options.host || 'localhost';
|
|
16
16
|
this.maxConnections = options.maxConnections || 1000;
|
|
@@ -18,7 +18,7 @@ export class PeerPigeonServer extends EventEmitter {
|
|
|
18
18
|
this.peerTimeout = options.peerTimeout || 300000; // 5 minutes
|
|
19
19
|
this.corsOrigin = options.corsOrigin || '*';
|
|
20
20
|
this.maxMessageSize = options.maxMessageSize || 1048576; // 1MB
|
|
21
|
-
|
|
21
|
+
|
|
22
22
|
this.httpServer = null;
|
|
23
23
|
this.wss = null;
|
|
24
24
|
this.connections = new Map(); // peerId -> WebSocket
|
|
@@ -87,12 +87,12 @@ export class PeerPigeonServer extends EventEmitter {
|
|
|
87
87
|
this.httpServer.listen(this.port, this.host, () => {
|
|
88
88
|
this.isRunning = true;
|
|
89
89
|
this.startCleanupTimer();
|
|
90
|
-
|
|
90
|
+
|
|
91
91
|
console.log(`๐ PeerPigeon WebSocket server started on ws://${this.host}:${this.port}`);
|
|
92
92
|
console.log(`๐ Max connections: ${this.maxConnections}`);
|
|
93
93
|
console.log(`๐งน Cleanup interval: ${this.cleanupInterval}ms`);
|
|
94
94
|
console.log(`โฐ Peer timeout: ${this.peerTimeout}ms`);
|
|
95
|
-
|
|
95
|
+
|
|
96
96
|
this.emit('started', { host: this.host, port: this.port });
|
|
97
97
|
resolve();
|
|
98
98
|
});
|
|
@@ -120,7 +120,7 @@ export class PeerPigeonServer extends EventEmitter {
|
|
|
120
120
|
|
|
121
121
|
return new Promise((resolve) => {
|
|
122
122
|
console.log('๐ Stopping PeerPigeon WebSocket server...');
|
|
123
|
-
|
|
123
|
+
|
|
124
124
|
// Stop cleanup timer
|
|
125
125
|
if (this.cleanupTimer) {
|
|
126
126
|
clearInterval(this.cleanupTimer);
|
|
@@ -140,7 +140,7 @@ export class PeerPigeonServer extends EventEmitter {
|
|
|
140
140
|
if (this.wss) {
|
|
141
141
|
this.wss.close(() => {
|
|
142
142
|
console.log('โ
WebSocket server closed');
|
|
143
|
-
|
|
143
|
+
|
|
144
144
|
// Close HTTP server
|
|
145
145
|
if (this.httpServer) {
|
|
146
146
|
this.httpServer.close(() => {
|
|
@@ -170,7 +170,7 @@ export class PeerPigeonServer extends EventEmitter {
|
|
|
170
170
|
this.wss.on('connection', (ws, req) => {
|
|
171
171
|
const url = new URL(req.url, `http://${req.headers.host}`);
|
|
172
172
|
const peerId = url.searchParams.get('peerId');
|
|
173
|
-
|
|
173
|
+
|
|
174
174
|
// Validate peer ID
|
|
175
175
|
if (!peerId || !/^[a-fA-F0-9]{40}$/.test(peerId)) {
|
|
176
176
|
console.log('โ Invalid peer ID, closing connection');
|
|
@@ -281,7 +281,7 @@ export class PeerPigeonServer extends EventEmitter {
|
|
|
281
281
|
*/
|
|
282
282
|
handleAnnounce(peerId, message) {
|
|
283
283
|
console.log(`๐ข Peer announced: ${peerId.substring(0, 8)}...`);
|
|
284
|
-
|
|
284
|
+
|
|
285
285
|
// Update peer data
|
|
286
286
|
const peerInfo = this.peerData.get(peerId);
|
|
287
287
|
if (peerInfo) {
|
|
@@ -309,7 +309,7 @@ export class PeerPigeonServer extends EventEmitter {
|
|
|
309
309
|
*/
|
|
310
310
|
handleGoodbye(peerId, message) {
|
|
311
311
|
console.log(`๐ Peer goodbye: ${peerId.substring(0, 8)}...`);
|
|
312
|
-
|
|
312
|
+
|
|
313
313
|
// Notify other peers about disconnection
|
|
314
314
|
this.broadcastToOthers(peerId, {
|
|
315
315
|
type: 'peer-disconnected',
|
|
@@ -383,7 +383,7 @@ export class PeerPigeonServer extends EventEmitter {
|
|
|
383
383
|
*/
|
|
384
384
|
handleDisconnection(peerId, code, reason) {
|
|
385
385
|
console.log(`โ Peer disconnected: ${peerId.substring(0, 8)}... (code: ${code}, reason: ${reason})`);
|
|
386
|
-
|
|
386
|
+
|
|
387
387
|
// Notify other peers about disconnection
|
|
388
388
|
this.broadcastToOthers(peerId, {
|
|
389
389
|
type: 'peer-disconnected',
|
|
@@ -464,7 +464,7 @@ export class PeerPigeonServer extends EventEmitter {
|
|
|
464
464
|
for (const [peerId, peerInfo] of this.peerData) {
|
|
465
465
|
if (now - peerInfo.lastActivity > this.peerTimeout) {
|
|
466
466
|
console.log(`๐งน Cleaning up inactive peer: ${peerId.substring(0, 8)}...`);
|
|
467
|
-
|
|
467
|
+
|
|
468
468
|
const connection = this.connections.get(peerId);
|
|
469
469
|
if (connection) {
|
|
470
470
|
try {
|
|
@@ -473,7 +473,7 @@ export class PeerPigeonServer extends EventEmitter {
|
|
|
473
473
|
console.error(`Error closing inactive connection for ${peerId}:`, error);
|
|
474
474
|
}
|
|
475
475
|
}
|
|
476
|
-
|
|
476
|
+
|
|
477
477
|
disconnectedPeers.push(peerId);
|
|
478
478
|
}
|
|
479
479
|
}
|
package/src/CleanupManager.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { EventEmitter } from './EventEmitter.js';
|
|
2
|
+
import DebugLogger from './DebugLogger.js';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Manages cleanup operations for signaling data and peer connections
|
|
@@ -6,6 +7,7 @@ import { EventEmitter } from './EventEmitter.js';
|
|
|
6
7
|
export class CleanupManager extends EventEmitter {
|
|
7
8
|
constructor(mesh) {
|
|
8
9
|
super();
|
|
10
|
+
this.debug = DebugLogger.create('CleanupManager');
|
|
9
11
|
this.mesh = mesh;
|
|
10
12
|
this.cleanupInProgress = new Set();
|
|
11
13
|
}
|
|
@@ -13,14 +15,14 @@ export class CleanupManager extends EventEmitter {
|
|
|
13
15
|
async cleanupSignalingData(peerId) {
|
|
14
16
|
// Prevent duplicate cleanup calls
|
|
15
17
|
if (this.cleanupInProgress.has(peerId)) {
|
|
16
|
-
|
|
18
|
+
this.debug.log('Cleanup already in progress for', peerId);
|
|
17
19
|
return;
|
|
18
20
|
}
|
|
19
21
|
|
|
20
22
|
this.cleanupInProgress.add(peerId);
|
|
21
23
|
|
|
22
24
|
try {
|
|
23
|
-
|
|
25
|
+
this.debug.log('Cleaning up signaling data for', peerId);
|
|
24
26
|
|
|
25
27
|
const response = await this.mesh.signalingClient.sendSignalingMessage({
|
|
26
28
|
type: 'cleanup',
|
|
@@ -35,11 +37,11 @@ export class CleanupManager extends EventEmitter {
|
|
|
35
37
|
|
|
36
38
|
// Only log if something was actually cleaned up
|
|
37
39
|
if (response.cleaned && (response.cleaned.signaling > 0 || response.cleaned.discovery > 0)) {
|
|
38
|
-
|
|
40
|
+
this.debug.log('Signaling cleanup completed for', peerId, response);
|
|
39
41
|
this.mesh.emit('statusChanged', { type: 'info', message: `Cleaned up signaling data with ${peerId.substring(0, 8)}...` });
|
|
40
42
|
}
|
|
41
43
|
} catch (error) {
|
|
42
|
-
|
|
44
|
+
this.debug.error('Failed to cleanup signaling data for', peerId, error);
|
|
43
45
|
// Don't show error to user as this is background cleanup
|
|
44
46
|
} finally {
|
|
45
47
|
this.cleanupInProgress.delete(peerId);
|
|
@@ -60,10 +62,10 @@ export class CleanupManager extends EventEmitter {
|
|
|
60
62
|
|
|
61
63
|
// Only log if something was actually cleaned
|
|
62
64
|
if (response.cleaned > 0) {
|
|
63
|
-
|
|
65
|
+
this.debug.log(`Cleaned up ${response.cleaned} stale signaling items`);
|
|
64
66
|
}
|
|
65
67
|
} catch (error) {
|
|
66
|
-
|
|
68
|
+
this.debug.log('Failed to cleanup all signaling data:', error.message);
|
|
67
69
|
}
|
|
68
70
|
}
|
|
69
71
|
}
|
|
@@ -80,11 +82,11 @@ export class CleanupManager extends EventEmitter {
|
|
|
80
82
|
reason: 'browser_unload'
|
|
81
83
|
}
|
|
82
84
|
}).catch(error => {
|
|
83
|
-
|
|
85
|
+
this.debug.log('Failed to send cleanup-all message:', error.message);
|
|
84
86
|
});
|
|
85
|
-
|
|
87
|
+
this.debug.log('Cleanup-all message sent via WebSocket');
|
|
86
88
|
} catch (error) {
|
|
87
|
-
|
|
89
|
+
this.debug.log('Failed to send cleanup-all message:', error.message);
|
|
88
90
|
}
|
|
89
91
|
}
|
|
90
92
|
}
|
|
@@ -92,11 +94,11 @@ export class CleanupManager extends EventEmitter {
|
|
|
92
94
|
// Manual cleanup method for already-connected mesh networks
|
|
93
95
|
async cleanupStaleSignalingData() {
|
|
94
96
|
if (!this.mesh.signalingClient || !this.mesh.connected) {
|
|
95
|
-
|
|
97
|
+
this.debug.log('Cannot cleanup - not connected to signaling server');
|
|
96
98
|
return;
|
|
97
99
|
}
|
|
98
100
|
|
|
99
|
-
|
|
101
|
+
this.debug.log('Manually cleaning up stale signaling data for all connected peers...');
|
|
100
102
|
|
|
101
103
|
try {
|
|
102
104
|
// Clean up signaling data for each connected peer
|
|
@@ -112,7 +114,7 @@ export class CleanupManager extends EventEmitter {
|
|
|
112
114
|
cleanupPromises.push(this.cleanupSignalingData(peerId));
|
|
113
115
|
}
|
|
114
116
|
} catch (error) {
|
|
115
|
-
|
|
117
|
+
this.debug.log(`Error checking peer ${peerId} status during cleanup:`, error.message);
|
|
116
118
|
}
|
|
117
119
|
}
|
|
118
120
|
}
|
|
@@ -125,10 +127,10 @@ export class CleanupManager extends EventEmitter {
|
|
|
125
127
|
// Then do a comprehensive cleanup
|
|
126
128
|
await this.cleanupAllSignalingData();
|
|
127
129
|
|
|
128
|
-
|
|
130
|
+
this.debug.log('Manual cleanup completed for all connected peers');
|
|
129
131
|
this.mesh.emit('statusChanged', { type: 'info', message: 'Cleaned up stale signaling data' });
|
|
130
132
|
} catch (error) {
|
|
131
|
-
|
|
133
|
+
this.debug.error('Manual cleanup failed:', error);
|
|
132
134
|
this.mesh.emit('statusChanged', { type: 'error', message: 'Failed to cleanup signaling data' });
|
|
133
135
|
}
|
|
134
136
|
}
|
|
@@ -138,9 +140,9 @@ export class CleanupManager extends EventEmitter {
|
|
|
138
140
|
try {
|
|
139
141
|
// For WebSocket, send goodbye message synchronously
|
|
140
142
|
this.mesh.signalingClient.sendGoodbyeMessage();
|
|
141
|
-
|
|
143
|
+
this.debug.log('Goodbye message sent via WebSocket');
|
|
142
144
|
} catch (error) {
|
|
143
|
-
|
|
145
|
+
this.debug.log('Failed to send goodbye message:', error.message);
|
|
144
146
|
}
|
|
145
147
|
}
|
|
146
148
|
}
|
|
@@ -153,7 +155,7 @@ export class CleanupManager extends EventEmitter {
|
|
|
153
155
|
data: { peerId: this.mesh.peerId, timestamp: Date.now() }
|
|
154
156
|
});
|
|
155
157
|
} catch (error) {
|
|
156
|
-
|
|
158
|
+
this.debug.log('Failed to send goodbye message:', error.message);
|
|
157
159
|
}
|
|
158
160
|
}
|
|
159
161
|
}
|
|
@@ -162,7 +164,7 @@ export class CleanupManager extends EventEmitter {
|
|
|
162
164
|
// Handle page unload/refresh - ONLY clean up ALL peer data on actual unload
|
|
163
165
|
if (typeof window !== 'undefined') {
|
|
164
166
|
const handleUnload = () => {
|
|
165
|
-
|
|
167
|
+
this.debug.log('Page unloading - cleaning up ALL peer data');
|
|
166
168
|
this.sendGoodbyeMessageSync();
|
|
167
169
|
this.cleanupAllSignalingDataSync();
|
|
168
170
|
};
|