peerpigeon 0.0.4 → 0.0.6
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 +144 -4
- package/examples/browser/app.js +40 -39
- package/examples/browser/crypto-test.html +427 -0
- package/examples/browser/index.html +161 -0
- package/examples/browser/styles.css +216 -0
- package/examples/browser/ui-old.js +1926 -0
- package/examples/browser/ui.js +2226 -1692
- package/examples/node/test-crypto-media.js +137 -0
- package/examples/node/test-crypto.js +146 -0
- package/examples/node/test-node.js +114 -115
- package/package.json +51 -8
- package/src/CleanupManager.js +154 -154
- package/src/ConnectionManager.js +772 -749
- package/src/CryptoManager.js +612 -0
- package/src/EnvironmentDetector.js +381 -381
- package/src/EventEmitter.js +19 -19
- package/src/EvictionManager.js +211 -211
- package/src/GossipManager.js +447 -366
- package/src/MediaManager.js +218 -195
- package/src/MeshOptimizer.js +165 -166
- package/src/PeerConnection.js +850 -758
- package/src/PeerDiscovery.js +255 -259
- package/src/PeerPigeonMesh.js +1082 -723
- package/src/SignalingClient.js +517 -518
- package/src/SignalingHandler.js +295 -291
- package/src/StorageManager.js +105 -105
- package/src/TimerUtils.js +29 -29
- package/src/WebDHT.js +951 -951
- package/websocket-server/server.js +371 -332
package/src/CleanupManager.js
CHANGED
|
@@ -4,178 +4,178 @@ import { EventEmitter } from './EventEmitter.js';
|
|
|
4
4
|
* Manages cleanup operations for signaling data and peer connections
|
|
5
5
|
*/
|
|
6
6
|
export class CleanupManager extends EventEmitter {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
constructor(mesh) {
|
|
8
|
+
super();
|
|
9
|
+
this.mesh = mesh;
|
|
10
|
+
this.cleanupInProgress = new Set();
|
|
11
|
+
}
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
this.cleanupInProgress.add(peerId);
|
|
21
|
-
|
|
22
|
-
try {
|
|
23
|
-
console.log('Cleaning up signaling data for', peerId);
|
|
24
|
-
|
|
25
|
-
const response = await this.mesh.signalingClient.sendSignalingMessage({
|
|
26
|
-
type: 'cleanup',
|
|
27
|
-
data: {
|
|
28
|
-
peerId: this.mesh.peerId,
|
|
29
|
-
targetPeerId: peerId,
|
|
30
|
-
timestamp: Date.now(),
|
|
31
|
-
reason: 'connection_established'
|
|
32
|
-
},
|
|
33
|
-
targetPeerId: peerId
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
// Only log if something was actually cleaned up
|
|
37
|
-
if (response.cleaned && (response.cleaned.signaling > 0 || response.cleaned.discovery > 0)) {
|
|
38
|
-
console.log('Signaling cleanup completed for', peerId, response);
|
|
39
|
-
this.mesh.emit('statusChanged', { type: 'info', message: `Cleaned up signaling data with ${peerId.substring(0, 8)}...` });
|
|
40
|
-
}
|
|
41
|
-
} catch (error) {
|
|
42
|
-
console.error('Failed to cleanup signaling data for', peerId, error);
|
|
43
|
-
// Don't show error to user as this is background cleanup
|
|
44
|
-
} finally {
|
|
45
|
-
this.cleanupInProgress.delete(peerId);
|
|
46
|
-
}
|
|
13
|
+
async cleanupSignalingData(peerId) {
|
|
14
|
+
// Prevent duplicate cleanup calls
|
|
15
|
+
if (this.cleanupInProgress.has(peerId)) {
|
|
16
|
+
console.log('Cleanup already in progress for', peerId);
|
|
17
|
+
return;
|
|
47
18
|
}
|
|
48
19
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
20
|
+
this.cleanupInProgress.add(peerId);
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
console.log('Cleaning up signaling data for', peerId);
|
|
24
|
+
|
|
25
|
+
const response = await this.mesh.signalingClient.sendSignalingMessage({
|
|
26
|
+
type: 'cleanup',
|
|
27
|
+
data: {
|
|
28
|
+
peerId: this.mesh.peerId,
|
|
29
|
+
targetPeerId: peerId,
|
|
30
|
+
timestamp: Date.now(),
|
|
31
|
+
reason: 'connection_established'
|
|
32
|
+
},
|
|
33
|
+
targetPeerId: peerId
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// Only log if something was actually cleaned up
|
|
37
|
+
if (response.cleaned && (response.cleaned.signaling > 0 || response.cleaned.discovery > 0)) {
|
|
38
|
+
console.log('Signaling cleanup completed for', peerId, response);
|
|
39
|
+
this.mesh.emit('statusChanged', { type: 'info', message: `Cleaned up signaling data with ${peerId.substring(0, 8)}...` });
|
|
40
|
+
}
|
|
41
|
+
} catch (error) {
|
|
42
|
+
console.error('Failed to cleanup signaling data for', peerId, error);
|
|
43
|
+
// Don't show error to user as this is background cleanup
|
|
44
|
+
} finally {
|
|
45
|
+
this.cleanupInProgress.delete(peerId);
|
|
69
46
|
}
|
|
47
|
+
}
|
|
70
48
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
} catch (error) {
|
|
87
|
-
console.log('Failed to send cleanup-all message:', error.message);
|
|
88
|
-
}
|
|
49
|
+
async cleanupAllSignalingData() {
|
|
50
|
+
if (this.mesh.signalingClient && this.mesh.peerId) {
|
|
51
|
+
try {
|
|
52
|
+
const response = await this.mesh.signalingClient.sendSignalingMessage({
|
|
53
|
+
type: 'cleanup-all',
|
|
54
|
+
data: {
|
|
55
|
+
peerId: this.mesh.peerId,
|
|
56
|
+
timestamp: Date.now(),
|
|
57
|
+
reason: 'peer_cleanup'
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// Only log if something was actually cleaned
|
|
62
|
+
if (response.cleaned > 0) {
|
|
63
|
+
console.log(`Cleaned up ${response.cleaned} stale signaling items`);
|
|
89
64
|
}
|
|
65
|
+
} catch (error) {
|
|
66
|
+
console.log('Failed to cleanup all signaling data:', error.message);
|
|
67
|
+
}
|
|
90
68
|
}
|
|
69
|
+
}
|
|
91
70
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
71
|
+
cleanupAllSignalingDataSync() {
|
|
72
|
+
if (this.mesh.signalingClient && this.mesh.connected && this.mesh.peerId) {
|
|
73
|
+
try {
|
|
74
|
+
// Send cleanup message via WebSocket synchronously
|
|
75
|
+
this.mesh.signalingClient.sendSignalingMessage({
|
|
76
|
+
type: 'cleanup-all',
|
|
77
|
+
data: {
|
|
78
|
+
peerId: this.mesh.peerId,
|
|
79
|
+
timestamp: Date.now(),
|
|
80
|
+
reason: 'browser_unload'
|
|
81
|
+
}
|
|
82
|
+
}).catch(error => {
|
|
83
|
+
console.log('Failed to send cleanup-all message:', error.message);
|
|
84
|
+
});
|
|
85
|
+
console.log('Cleanup-all message sent via WebSocket');
|
|
86
|
+
} catch (error) {
|
|
87
|
+
console.log('Failed to send cleanup-all message:', error.message);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
98
91
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
// Use safer iteration to avoid race conditions
|
|
106
|
-
if (this.mesh.connectionManager && this.mesh.connectionManager.peers) {
|
|
107
|
-
const peerEntries = Array.from(this.mesh.connectionManager.peers.entries());
|
|
108
|
-
|
|
109
|
-
for (const [peerId, peerConnection] of peerEntries) {
|
|
110
|
-
try {
|
|
111
|
-
if (peerConnection && peerConnection.getStatus && peerConnection.getStatus() === 'connected') {
|
|
112
|
-
cleanupPromises.push(this.cleanupSignalingData(peerId));
|
|
113
|
-
}
|
|
114
|
-
} catch (error) {
|
|
115
|
-
console.log(`Error checking peer ${peerId} status during cleanup:`, error.message);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
// Wait for all peer-specific cleanups to complete
|
|
121
|
-
if (cleanupPromises.length > 0) {
|
|
122
|
-
await Promise.allSettled(cleanupPromises);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// Then do a comprehensive cleanup
|
|
126
|
-
await this.cleanupAllSignalingData();
|
|
127
|
-
|
|
128
|
-
console.log('Manual cleanup completed for all connected peers');
|
|
129
|
-
this.mesh.emit('statusChanged', { type: 'info', message: 'Cleaned up stale signaling data' });
|
|
130
|
-
} catch (error) {
|
|
131
|
-
console.error('Manual cleanup failed:', error);
|
|
132
|
-
this.mesh.emit('statusChanged', { type: 'error', message: 'Failed to cleanup signaling data' });
|
|
133
|
-
}
|
|
92
|
+
// Manual cleanup method for already-connected mesh networks
|
|
93
|
+
async cleanupStaleSignalingData() {
|
|
94
|
+
if (!this.mesh.signalingClient || !this.mesh.connected) {
|
|
95
|
+
console.log('Cannot cleanup - not connected to signaling server');
|
|
96
|
+
return;
|
|
134
97
|
}
|
|
135
98
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
99
|
+
console.log('Manually cleaning up stale signaling data for all connected peers...');
|
|
100
|
+
|
|
101
|
+
try {
|
|
102
|
+
// Clean up signaling data for each connected peer
|
|
103
|
+
const cleanupPromises = [];
|
|
104
|
+
|
|
105
|
+
// Use safer iteration to avoid race conditions
|
|
106
|
+
if (this.mesh.connectionManager && this.mesh.connectionManager.peers) {
|
|
107
|
+
const peerEntries = Array.from(this.mesh.connectionManager.peers.entries());
|
|
108
|
+
|
|
109
|
+
for (const [peerId, peerConnection] of peerEntries) {
|
|
110
|
+
try {
|
|
111
|
+
if (peerConnection && peerConnection.getStatus && peerConnection.getStatus() === 'connected') {
|
|
112
|
+
cleanupPromises.push(this.cleanupSignalingData(peerId));
|
|
144
113
|
}
|
|
114
|
+
} catch (error) {
|
|
115
|
+
console.log(`Error checking peer ${peerId} status during cleanup:`, error.message);
|
|
116
|
+
}
|
|
145
117
|
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Wait for all peer-specific cleanups to complete
|
|
121
|
+
if (cleanupPromises.length > 0) {
|
|
122
|
+
await Promise.allSettled(cleanupPromises);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Then do a comprehensive cleanup
|
|
126
|
+
await this.cleanupAllSignalingData();
|
|
127
|
+
|
|
128
|
+
console.log('Manual cleanup completed for all connected peers');
|
|
129
|
+
this.mesh.emit('statusChanged', { type: 'info', message: 'Cleaned up stale signaling data' });
|
|
130
|
+
} catch (error) {
|
|
131
|
+
console.error('Manual cleanup failed:', error);
|
|
132
|
+
this.mesh.emit('statusChanged', { type: 'error', message: 'Failed to cleanup signaling data' });
|
|
146
133
|
}
|
|
134
|
+
}
|
|
147
135
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
}
|
|
158
|
-
}
|
|
136
|
+
sendGoodbyeMessageSync() {
|
|
137
|
+
if (this.mesh.signalingClient && this.mesh.connected) {
|
|
138
|
+
try {
|
|
139
|
+
// For WebSocket, send goodbye message synchronously
|
|
140
|
+
this.mesh.signalingClient.sendGoodbyeMessage();
|
|
141
|
+
console.log('Goodbye message sent via WebSocket');
|
|
142
|
+
} catch (error) {
|
|
143
|
+
console.log('Failed to send goodbye message:', error.message);
|
|
144
|
+
}
|
|
159
145
|
}
|
|
146
|
+
}
|
|
160
147
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
// DO NOT cleanup on focus loss or visibility change (breaks multi-tab usage)
|
|
172
|
-
window.addEventListener('beforeunload', handleUnload);
|
|
173
|
-
window.addEventListener('unload', handleUnload);
|
|
174
|
-
window.addEventListener('pagehide', handleUnload);
|
|
175
|
-
}
|
|
148
|
+
async sendGoodbyeMessage() {
|
|
149
|
+
if (this.mesh.signalingClient && this.mesh.connected) {
|
|
150
|
+
try {
|
|
151
|
+
await this.mesh.signalingClient.sendSignalingMessage({
|
|
152
|
+
type: 'goodbye',
|
|
153
|
+
data: { peerId: this.mesh.peerId, timestamp: Date.now() }
|
|
154
|
+
});
|
|
155
|
+
} catch (error) {
|
|
156
|
+
console.log('Failed to send goodbye message:', error.message);
|
|
157
|
+
}
|
|
176
158
|
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
setupUnloadHandlers() {
|
|
162
|
+
// Handle page unload/refresh - ONLY clean up ALL peer data on actual unload
|
|
163
|
+
if (typeof window !== 'undefined') {
|
|
164
|
+
const handleUnload = () => {
|
|
165
|
+
console.log('Page unloading - cleaning up ALL peer data');
|
|
166
|
+
this.sendGoodbyeMessageSync();
|
|
167
|
+
this.cleanupAllSignalingDataSync();
|
|
168
|
+
};
|
|
177
169
|
|
|
178
|
-
|
|
179
|
-
|
|
170
|
+
// Only trigger cleanup on actual page unload scenarios
|
|
171
|
+
// DO NOT cleanup on focus loss or visibility change (breaks multi-tab usage)
|
|
172
|
+
window.addEventListener('beforeunload', handleUnload);
|
|
173
|
+
window.addEventListener('unload', handleUnload);
|
|
174
|
+
window.addEventListener('pagehide', handleUnload);
|
|
180
175
|
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
cleanup() {
|
|
179
|
+
this.cleanupInProgress.clear();
|
|
180
|
+
}
|
|
181
181
|
}
|