peerpigeon 0.0.8 โ 0.1.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.
- package/README.md +34 -0
- package/examples/browser/index.html +104 -16
- package/examples/browser/ui.js +381 -0
- package/examples/debug-configuration.js +171 -0
- package/examples/node/storage-spaces-test.js +329 -0
- package/examples/node/storage-test.js +141 -6
- package/index.js +3 -0
- package/package.json +13 -8
- 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 +490 -102
- 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/LexicalStorageInterface.js +332 -0
- 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 +241 -33
- 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
|
@@ -74,27 +74,9 @@ function isConnectionAlive(connection) {
|
|
|
74
74
|
return false;
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
-
//
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
const timeSinceLastPong = now - lastPong;
|
|
81
|
-
|
|
82
|
-
// Consider connection dead if no pong for more than 12 minutes (since we ping every 9 minutes)
|
|
83
|
-
return timeSinceLastPong < 720000; // 12 minutes
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
function pingConnection(peerId, connection) {
|
|
87
|
-
if (connection.readyState === WebSocket.OPEN) {
|
|
88
|
-
try {
|
|
89
|
-
connection.ping();
|
|
90
|
-
return true;
|
|
91
|
-
} catch (error) {
|
|
92
|
-
console.log(`๐ Failed to ping ${peerId.substring(0, 8)}...:`, error.message);
|
|
93
|
-
cleanupPeer(peerId);
|
|
94
|
-
return false;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
return false;
|
|
77
|
+
// Simple alive check - if connection is open, it's considered alive
|
|
78
|
+
// Detailed health monitoring is handled by the peer mesh itself
|
|
79
|
+
return true;
|
|
98
80
|
}
|
|
99
81
|
|
|
100
82
|
function sendToConnection(peerId, data) {
|
|
@@ -172,62 +154,8 @@ setInterval(() => {
|
|
|
172
154
|
}
|
|
173
155
|
}, 30000); // Clean up every 30 seconds
|
|
174
156
|
|
|
175
|
-
//
|
|
176
|
-
//
|
|
177
|
-
setInterval(() => {
|
|
178
|
-
const activePeers = Array.from(connections.keys());
|
|
179
|
-
|
|
180
|
-
if (activePeers.length === 0) {
|
|
181
|
-
console.log('๐ No peers to ping');
|
|
182
|
-
return;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
// Use XOR distance to select the peer closest to a deterministic target
|
|
186
|
-
// This ensures the same peer is consistently selected across intervals
|
|
187
|
-
const pingTarget = '0000000000000000000000000000000000000000'; // Deterministic target for ping selection
|
|
188
|
-
const selectedPeer = selectPeerForPing(pingTarget, activePeers);
|
|
189
|
-
|
|
190
|
-
if (selectedPeer) {
|
|
191
|
-
const connection = connections.get(selectedPeer);
|
|
192
|
-
console.log(`๐ DHT-selected peer ${selectedPeer.substring(0, 8)}... for heartbeat ping (${activePeers.length} total peers)`);
|
|
193
|
-
|
|
194
|
-
if (!pingConnection(selectedPeer, connection)) {
|
|
195
|
-
console.log(`๐งน DHT-selected peer ${selectedPeer.substring(0, 8)}... failed ping, cleaned up`);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
}, 540000); // Ping every 9 minutes (540 seconds)
|
|
199
|
-
|
|
200
|
-
// DHT-based peer selection for ping duty
|
|
201
|
-
function selectPeerForPing(target, peerIds) {
|
|
202
|
-
if (!peerIds || peerIds.length === 0) return null;
|
|
203
|
-
|
|
204
|
-
// Use XOR distance to find the peer closest to the target
|
|
205
|
-
let closestPeer = null;
|
|
206
|
-
let closestDistance = Infinity;
|
|
207
|
-
|
|
208
|
-
for (const peerId of peerIds) {
|
|
209
|
-
const distance = calculateXORDistance(target, peerId);
|
|
210
|
-
if (distance < closestDistance) {
|
|
211
|
-
closestDistance = distance;
|
|
212
|
-
closestPeer = peerId;
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
return closestPeer;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
// XOR distance calculation for DHT peer selection
|
|
220
|
-
function calculateXORDistance(id1, id2) {
|
|
221
|
-
let distance = 0;
|
|
222
|
-
const minLength = Math.min(id1.length, id2.length);
|
|
223
|
-
|
|
224
|
-
for (let i = 0; i < minLength; i++) {
|
|
225
|
-
const xor = parseInt(id1[i], 16) ^ parseInt(id2[i], 16);
|
|
226
|
-
distance += xor;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
return distance;
|
|
230
|
-
}
|
|
157
|
+
// Note: Signaling servers should NOT initiate pings to peers
|
|
158
|
+
// Health monitoring is the responsibility of the peer mesh network itself
|
|
231
159
|
|
|
232
160
|
wss.on('connection', (ws, req) => {
|
|
233
161
|
let peerId = null;
|
|
@@ -268,12 +196,6 @@ wss.on('connection', (ws, req) => {
|
|
|
268
196
|
|
|
269
197
|
// Set up connection metadata
|
|
270
198
|
ws.connectedAt = Date.now();
|
|
271
|
-
ws.lastPong = Date.now();
|
|
272
|
-
|
|
273
|
-
// Set up pong handler for heartbeat
|
|
274
|
-
ws.on('pong', () => {
|
|
275
|
-
ws.lastPong = Date.now();
|
|
276
|
-
});
|
|
277
199
|
|
|
278
200
|
console.log(`โ
Peer ${peerId.substring(0, 8)}... connected (${connections.size} total)`);
|
|
279
201
|
|
|
@@ -288,7 +210,7 @@ wss.on('connection', (ws, req) => {
|
|
|
288
210
|
ws.on('message', (data) => {
|
|
289
211
|
try {
|
|
290
212
|
const message = JSON.parse(data);
|
|
291
|
-
const { type, data: messageData, targetPeerId
|
|
213
|
+
const { type, data: messageData, targetPeerId } = message;
|
|
292
214
|
|
|
293
215
|
console.log(`๐จ Received ${type} from ${peerId.substring(0, 8)}...`);
|
|
294
216
|
|
|
@@ -300,17 +222,8 @@ wss.on('connection', (ws, req) => {
|
|
|
300
222
|
timestamp: Date.now()
|
|
301
223
|
};
|
|
302
224
|
|
|
303
|
-
// Handle different message types
|
|
225
|
+
// Handle different message types - SIGNALING ONLY
|
|
304
226
|
switch (type) {
|
|
305
|
-
case 'ping':
|
|
306
|
-
// Respond with pong
|
|
307
|
-
ws.send(JSON.stringify({
|
|
308
|
-
type: 'pong',
|
|
309
|
-
timestamp: Date.now(),
|
|
310
|
-
originalTimestamp: messageData?.timestamp
|
|
311
|
-
}));
|
|
312
|
-
break;
|
|
313
|
-
|
|
314
227
|
case 'announce': {
|
|
315
228
|
// Handle peer announcement
|
|
316
229
|
peerData.set(peerId, {
|
|
@@ -330,12 +243,12 @@ wss.on('connection', (ws, req) => {
|
|
|
330
243
|
if (connection && isConnectionAlive(connection)) {
|
|
331
244
|
validatedPeers.push(otherPeerId);
|
|
332
245
|
} else {
|
|
333
|
-
console.log(
|
|
246
|
+
console.log(`๐งน Found dead connection during announce: ${otherPeerId.substring(0, 8)}...`);
|
|
334
247
|
cleanupPeer(otherPeerId);
|
|
335
248
|
}
|
|
336
249
|
}
|
|
337
250
|
|
|
338
|
-
console.log(
|
|
251
|
+
console.log(`๐ข Announcing ${peerId.substring(0, 8)}... to ${validatedPeers.length} validated peers`);
|
|
339
252
|
|
|
340
253
|
// Send peer-discovered messages to validated peers only
|
|
341
254
|
validatedPeers.forEach(otherPeerId => {
|
|
@@ -372,11 +285,24 @@ wss.on('connection', (ws, req) => {
|
|
|
372
285
|
case 'offer':
|
|
373
286
|
case 'answer':
|
|
374
287
|
case 'ice-candidate': {
|
|
375
|
-
//
|
|
288
|
+
// CRITICAL DEBUG: Log answer routing details
|
|
289
|
+
if (type === 'answer') {
|
|
290
|
+
console.log('๐ WEBSOCKET DEBUG: Received answer message:', {
|
|
291
|
+
type,
|
|
292
|
+
fromPeerId: peerId?.substring(0, 8) + '...',
|
|
293
|
+
targetPeerId: targetPeerId?.substring(0, 8) + '...',
|
|
294
|
+
hasTargetPeerId: !!targetPeerId,
|
|
295
|
+
hasData: !!messageData
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// Handle WebRTC signaling - this is the server's primary purpose
|
|
376
300
|
if (targetPeerId) {
|
|
377
301
|
const success = sendToSpecificPeer(targetPeerId, responseMessage);
|
|
378
302
|
if (!success) {
|
|
379
303
|
console.log(`โ ๏ธ Failed to send ${type} to ${targetPeerId.substring(0, 8)}... (peer not found)`);
|
|
304
|
+
} else if (type === 'answer') {
|
|
305
|
+
console.log(`โ
WEBSOCKET DEBUG: Answer successfully routed to ${targetPeerId.substring(0, 8)}...`);
|
|
380
306
|
}
|
|
381
307
|
} else {
|
|
382
308
|
console.log(`โ ๏ธ ${type} message missing targetPeerId`);
|
|
@@ -385,12 +311,14 @@ wss.on('connection', (ws, req) => {
|
|
|
385
311
|
}
|
|
386
312
|
|
|
387
313
|
default:
|
|
388
|
-
//
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
314
|
+
// Signaling server should NOT route regular peer messages
|
|
315
|
+
// Peers handle their own message routing through WebRTC data channels
|
|
316
|
+
console.log(`โ ๏ธ Ignoring non-signaling message type '${type}' - peers should route their own messages`);
|
|
317
|
+
ws.send(JSON.stringify({
|
|
318
|
+
type: 'error',
|
|
319
|
+
error: `Signaling server does not route '${type}' messages. Use WebRTC data channels for peer-to-peer communication.`,
|
|
320
|
+
timestamp: Date.now()
|
|
321
|
+
}));
|
|
394
322
|
break;
|
|
395
323
|
}
|
|
396
324
|
} catch (error) {
|