peerpigeon 0.0.8 → 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 +34 -0
- package/examples/browser/index.html +16 -16
- package/examples/debug-configuration.js +171 -0
- package/examples/node/storage-spaces-test.js +329 -0
- 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 +469 -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/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
package/src/MeshOptimizer.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 mesh optimization, connection strategies, and peer discovery optimization
|
|
@@ -6,6 +7,7 @@ import { EventEmitter } from './EventEmitter.js';
|
|
|
6
7
|
export class MeshOptimizer extends EventEmitter {
|
|
7
8
|
constructor(mesh, connectionManager, evictionManager) {
|
|
8
9
|
super();
|
|
10
|
+
this.debug = DebugLogger.create('MeshOptimizer');
|
|
9
11
|
this.mesh = mesh;
|
|
10
12
|
this.connectionManager = connectionManager;
|
|
11
13
|
this.evictionManager = evictionManager;
|
|
@@ -23,7 +25,7 @@ export class MeshOptimizer extends EventEmitter {
|
|
|
23
25
|
const belowMinimum = currentConnected < this.mesh.minPeers;
|
|
24
26
|
|
|
25
27
|
if (hasNoConnections && unconnectedPeers.length > 0) {
|
|
26
|
-
|
|
28
|
+
this.debug.log(`Peer has no connections but found ${unconnectedPeers.length} unconnected peers - forcing connection attempt`);
|
|
27
29
|
// Force connection to the closest peer by XOR distance
|
|
28
30
|
const sortedByDistance = unconnectedPeers.sort((a, b) => {
|
|
29
31
|
const distA = this.calculateXorDistance(this.mesh.peerId, a);
|
|
@@ -32,13 +34,13 @@ export class MeshOptimizer extends EventEmitter {
|
|
|
32
34
|
});
|
|
33
35
|
|
|
34
36
|
const targetPeer = sortedByDistance[0];
|
|
35
|
-
|
|
37
|
+
this.debug.log(`Forcing connection to closest peer: ${targetPeer.substring(0, 8)}... (no connections, mesh connectivity required)`);
|
|
36
38
|
this.connectionManager.connectToPeer(targetPeer);
|
|
37
39
|
return;
|
|
38
40
|
}
|
|
39
41
|
|
|
40
42
|
if (belowMinimum && unconnectedPeers.length > 0) {
|
|
41
|
-
|
|
43
|
+
this.debug.log(`Below minimum peers (${currentConnected}/${this.mesh.minPeers}) - connecting to additional peers`);
|
|
42
44
|
const needed = Math.min(this.mesh.minPeers - currentConnected, unconnectedPeers.length);
|
|
43
45
|
|
|
44
46
|
// Sort by XOR distance and connect to closest peers
|
|
@@ -50,7 +52,7 @@ export class MeshOptimizer extends EventEmitter {
|
|
|
50
52
|
|
|
51
53
|
for (let i = 0; i < needed; i++) {
|
|
52
54
|
const targetPeer = sortedByDistance[i];
|
|
53
|
-
|
|
55
|
+
this.debug.log(`Connecting to reach minimum: ${targetPeer.substring(0, 8)}...`);
|
|
54
56
|
this.connectionManager.connectToPeer(targetPeer);
|
|
55
57
|
}
|
|
56
58
|
return;
|
|
@@ -60,14 +62,14 @@ export class MeshOptimizer extends EventEmitter {
|
|
|
60
62
|
if (this.mesh.maxPeers <= 3) {
|
|
61
63
|
// For small meshes, always try to reach max capacity
|
|
62
64
|
if (currentConnected >= this.mesh.maxPeers) {
|
|
63
|
-
|
|
65
|
+
this.debug.log(`Skipping optimization - already at max capacity ${currentConnected}/${this.mesh.maxPeers}`);
|
|
64
66
|
return;
|
|
65
67
|
}
|
|
66
68
|
} else {
|
|
67
69
|
// For larger meshes, only optimize if below 70% capacity
|
|
68
70
|
const targetThreshold = Math.floor(this.mesh.maxPeers * 0.7);
|
|
69
71
|
if (currentConnected >= targetThreshold) {
|
|
70
|
-
|
|
72
|
+
this.debug.log(`Skipping optimization - ${currentConnected}/${this.mesh.maxPeers} peers connected (threshold: ${targetThreshold})`);
|
|
71
73
|
return;
|
|
72
74
|
}
|
|
73
75
|
}
|
|
@@ -75,21 +77,21 @@ export class MeshOptimizer extends EventEmitter {
|
|
|
75
77
|
const availableSlots = this.mesh.maxPeers - currentConnected;
|
|
76
78
|
const peersToConnect = unconnectedPeers.slice(0, Math.min(availableSlots, 1)); // Only connect to 1 peer at a time
|
|
77
79
|
|
|
78
|
-
|
|
80
|
+
this.debug.log(`Optimizing connections carefully: ${availableSlots} slots available, connecting to ${peersToConnect.length} peer(s)`);
|
|
79
81
|
|
|
80
82
|
peersToConnect.forEach((peerId, _index) => {
|
|
81
83
|
if (this.mesh.peerDiscovery.shouldInitiateConnection(peerId)) {
|
|
82
|
-
|
|
84
|
+
this.debug.log(`Initiating immediate connection to ${peerId.substring(0, 8)}... in optimization`);
|
|
83
85
|
// Double-check conditions before connecting
|
|
84
86
|
if (this.connectionManager.canAcceptMorePeers() &&
|
|
85
87
|
!this.connectionManager.hasPeer(peerId) &&
|
|
86
88
|
!this.mesh.peerDiscovery.isAttemptingConnection(peerId)) {
|
|
87
89
|
this.connectionManager.connectToPeer(peerId);
|
|
88
90
|
} else {
|
|
89
|
-
|
|
91
|
+
this.debug.log(`Skipping connection to ${peerId.substring(0, 8)}... - conditions changed`);
|
|
90
92
|
}
|
|
91
93
|
} else {
|
|
92
|
-
|
|
94
|
+
this.debug.log(`Not initiating connection to ${peerId.substring(0, 8)}... (should not initiate)`);
|
|
93
95
|
}
|
|
94
96
|
});
|
|
95
97
|
}
|
|
@@ -110,17 +112,17 @@ export class MeshOptimizer extends EventEmitter {
|
|
|
110
112
|
const discoveredPeers = this.mesh.getDiscoveredPeers();
|
|
111
113
|
let connectionAttempts = 0;
|
|
112
114
|
|
|
113
|
-
|
|
115
|
+
this.debug.log(`Forcing connections to ${discoveredPeers.length} discovered peers...`);
|
|
114
116
|
|
|
115
117
|
discoveredPeers.forEach(peer => {
|
|
116
118
|
if (!this.connectionManager.hasPeer(peer.peerId) && this.connectionManager.canAcceptMorePeers()) {
|
|
117
|
-
|
|
119
|
+
this.debug.log(`Force connecting to ${peer.peerId.substring(0, 8)}...`);
|
|
118
120
|
this.connectionManager.connectToPeer(peer.peerId);
|
|
119
121
|
connectionAttempts++;
|
|
120
122
|
}
|
|
121
123
|
});
|
|
122
124
|
|
|
123
|
-
|
|
125
|
+
this.debug.log(`Initiated ${connectionAttempts} forced connection attempts`);
|
|
124
126
|
return connectionAttempts;
|
|
125
127
|
}
|
|
126
128
|
|
|
@@ -130,39 +132,39 @@ export class MeshOptimizer extends EventEmitter {
|
|
|
130
132
|
const discoveredPeers = this.mesh.getDiscoveredPeers();
|
|
131
133
|
const totalPeers = this.connectionManager.peers.size;
|
|
132
134
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
135
|
+
this.debug.log('=== CONNECTIVITY DEBUG ===');
|
|
136
|
+
this.debug.log(`My Peer ID: ${this.mesh.peerId}`);
|
|
137
|
+
this.debug.log(`Connected Peers: ${connectedPeers}/${this.mesh.maxPeers}`);
|
|
138
|
+
this.debug.log(`Total Peer Objects: ${totalPeers}`);
|
|
139
|
+
this.debug.log(`Discovered Peers: ${discoveredPeers.length}`);
|
|
138
140
|
|
|
139
|
-
|
|
141
|
+
this.debug.log('\nPeer Details:');
|
|
140
142
|
this.connectionManager.peers.forEach((peerConnection, peerId) => {
|
|
141
143
|
const status = peerConnection.getStatus();
|
|
142
144
|
const dataChannelReady = peerConnection.dataChannelReady;
|
|
143
145
|
const connectionState = peerConnection.connection?.connectionState || 'unknown';
|
|
144
146
|
const dataChannelState = peerConnection.dataChannel?.readyState || 'unknown';
|
|
145
147
|
|
|
146
|
-
|
|
148
|
+
this.debug.log(` ${peerId.substring(0, 8)}... - Status: ${status}, WebRTC: ${connectionState}, DataChannel: ${dataChannelState}, Ready: ${dataChannelReady}`);
|
|
147
149
|
});
|
|
148
150
|
|
|
149
|
-
|
|
151
|
+
this.debug.log('\nDiscovered Peers:');
|
|
150
152
|
discoveredPeers.forEach(peer => {
|
|
151
153
|
const shouldInitiate = this.mesh.peerId < peer.peerId;
|
|
152
154
|
const isConnected = this.connectionManager.hasPeer(peer.peerId);
|
|
153
|
-
|
|
155
|
+
this.debug.log(` ${peer.peerId.substring(0, 8)}... - ShouldInitiate: ${shouldInitiate}, IsConnected: ${isConnected}`);
|
|
154
156
|
});
|
|
155
157
|
|
|
156
|
-
|
|
158
|
+
this.debug.log('\nConnection Attempts:');
|
|
157
159
|
this.connectionManager.connectionAttempts.forEach((attempts, peerId) => {
|
|
158
|
-
|
|
160
|
+
this.debug.log(` ${peerId.substring(0, 8)}... - Attempts: ${attempts}/${this.connectionManager.maxConnectionAttempts}`);
|
|
159
161
|
});
|
|
160
162
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
163
|
+
this.debug.log('\nEviction Status:');
|
|
164
|
+
this.debug.log(` Eviction Strategy: ${this.mesh.evictionStrategy ? 'enabled' : 'disabled'}`);
|
|
165
|
+
this.debug.log(` XOR Routing: ${this.mesh.xorRouting ? 'enabled' : 'disabled'}`);
|
|
164
166
|
|
|
165
|
-
|
|
167
|
+
this.debug.log('=== END DEBUG ===\n');
|
|
166
168
|
|
|
167
169
|
return {
|
|
168
170
|
connectedPeers,
|