peerpigeon 0.0.5 → 0.0.7
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 +190 -12
- package/examples/browser/app.js +40 -40
- 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 +1704 -1708
- package/examples/browser/ui.js +2226 -1692
- package/examples/nativescript/README.md +73 -0
- package/examples/nativescript/app.js +152 -0
- package/examples/nativescript/package.json +27 -0
- 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 +16 -13
- package/src/CleanupManager.js +154 -154
- package/src/ConnectionManager.js +772 -753
- package/src/CryptoManager.js +612 -0
- package/src/EnvironmentDetector.js +457 -384
- package/src/EventEmitter.js +19 -19
- package/src/EvictionManager.js +211 -211
- package/src/GossipManager.js +447 -366
- package/src/MediaManager.js +216 -214
- package/src/MeshOptimizer.js +165 -166
- package/src/PeerConnection.js +847 -941
- package/src/PeerDiscovery.js +255 -259
- package/src/PeerPigeonMesh.js +1100 -721
- package/src/SignalingClient.js +517 -518
- package/src/SignalingHandler.js +295 -295
- package/src/StorageManager.js +118 -104
- package/src/TimerUtils.js +29 -29
- package/src/WebDHT.js +1123 -948
- package/websocket-server/server.js +360 -362
package/src/StorageManager.js
CHANGED
|
@@ -5,126 +5,140 @@ import { environmentDetector } from './EnvironmentDetector.js';
|
|
|
5
5
|
* Manages storage operations, URL handling, and configuration persistence
|
|
6
6
|
*/
|
|
7
7
|
export class StorageManager extends EventEmitter {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
constructor(mesh) {
|
|
9
|
+
super();
|
|
10
|
+
this.mesh = mesh;
|
|
11
|
+
}
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
return null;
|
|
13
|
+
loadSignalingUrlFromStorage() {
|
|
14
|
+
// Use environment-aware storage detection
|
|
15
|
+
if (environmentDetector.hasLocalStorage) {
|
|
16
|
+
const savedUrl = localStorage.getItem('pigon-signaling-url');
|
|
17
|
+
if (savedUrl) {
|
|
18
|
+
this.mesh.signalingUrl = savedUrl;
|
|
19
|
+
this.mesh.emit('statusChanged', { type: 'urlLoaded', signalingUrl: savedUrl });
|
|
20
|
+
return savedUrl;
|
|
21
|
+
}
|
|
22
|
+
} else if (environmentDetector.isNodeJS) {
|
|
23
|
+
// In Node.js, we could potentially use file-based storage
|
|
24
|
+
console.log('Local storage not available in Node.js environment');
|
|
27
25
|
}
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
28
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
29
|
+
saveSignalingUrlToStorage(url) {
|
|
30
|
+
if (environmentDetector.hasLocalStorage && url) {
|
|
31
|
+
localStorage.setItem('pigon-signaling-url', url);
|
|
32
|
+
} else if (environmentDetector.isNodeJS) {
|
|
33
|
+
// In Node.js, we could potentially save to a config file
|
|
34
|
+
console.log('Storage not implemented for Node.js environment');
|
|
36
35
|
}
|
|
36
|
+
}
|
|
37
37
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const currentUrl = this.mesh.signalingUrl;
|
|
48
|
-
this.mesh.signalingUrl = signalingUrl;
|
|
49
|
-
this.saveSignalingUrlToStorage(signalingUrl);
|
|
50
|
-
|
|
51
|
-
if (currentUrl !== signalingUrl) {
|
|
52
|
-
this.mesh.emit('statusChanged', { type: 'urlLoaded', signalingUrl });
|
|
53
|
-
}
|
|
54
|
-
return signalingUrl;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// Fallback to localStorage if no URL in query params
|
|
58
|
-
return this.loadSignalingUrlFromStorage();
|
|
38
|
+
loadSignalingUrlFromQuery() {
|
|
39
|
+
// Only works in browser and NativeScript environments with location/URL support
|
|
40
|
+
if (!environmentDetector.isBrowser && !environmentDetector.isNativeScript) return this.loadSignalingUrlFromStorage();
|
|
41
|
+
|
|
42
|
+
// Check if we have URL search capabilities
|
|
43
|
+
if (typeof URLSearchParams === 'undefined' ||
|
|
44
|
+
(environmentDetector.isBrowser && typeof window === 'undefined') ||
|
|
45
|
+
(environmentDetector.isBrowser && typeof window.location === 'undefined')) {
|
|
46
|
+
return this.loadSignalingUrlFromStorage();
|
|
59
47
|
}
|
|
60
48
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
crypto.getRandomValues(array);
|
|
68
|
-
} else if (environmentDetector.isNodeJS) {
|
|
69
|
-
// In Node.js, use crypto module (handle both CommonJS and ES modules)
|
|
70
|
-
try {
|
|
71
|
-
if (typeof require !== 'undefined') {
|
|
72
|
-
const crypto = require('crypto');
|
|
73
|
-
const randomBytes = crypto.randomBytes(20);
|
|
74
|
-
array.set(randomBytes);
|
|
75
|
-
} else {
|
|
76
|
-
// ES module approach - import crypto dynamically
|
|
77
|
-
const crypto = await import('crypto');
|
|
78
|
-
const randomBytes = crypto.randomBytes(20);
|
|
79
|
-
array.set(randomBytes);
|
|
80
|
-
}
|
|
81
|
-
} catch (e) {
|
|
82
|
-
console.warn('Could not use Node.js crypto, falling back to Math.random');
|
|
83
|
-
// Fallback to Math.random
|
|
84
|
-
for (let i = 0; i < array.length; i++) {
|
|
85
|
-
array[i] = Math.floor(Math.random() * 256);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
} else {
|
|
90
|
-
// Fallback to less secure random generation
|
|
91
|
-
console.warn('Secure random values not available, using fallback method');
|
|
92
|
-
for (let i = 0; i < array.length; i++) {
|
|
93
|
-
array[i] = Math.floor(Math.random() * 256);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
return Array.from(array, byte => byte.toString(16).padStart(2, '0')).join('');
|
|
49
|
+
let searchParams;
|
|
50
|
+
if (environmentDetector.isBrowser) {
|
|
51
|
+
searchParams = new URLSearchParams(window.location.search);
|
|
52
|
+
} else if (environmentDetector.isNativeScript) {
|
|
53
|
+
// NativeScript might not have window.location, fallback to storage
|
|
54
|
+
return this.loadSignalingUrlFromStorage();
|
|
98
55
|
}
|
|
99
56
|
|
|
100
|
-
|
|
101
|
-
|
|
57
|
+
const signalingUrl = searchParams?.get('api') || searchParams?.get('url') || searchParams?.get('signaling');
|
|
58
|
+
|
|
59
|
+
if (signalingUrl) {
|
|
60
|
+
// Only emit event if URL is different from current one
|
|
61
|
+
const currentUrl = this.mesh.signalingUrl;
|
|
62
|
+
this.mesh.signalingUrl = signalingUrl;
|
|
63
|
+
this.saveSignalingUrlToStorage(signalingUrl);
|
|
64
|
+
|
|
65
|
+
if (currentUrl !== signalingUrl) {
|
|
66
|
+
this.mesh.emit('statusChanged', { type: 'urlLoaded', signalingUrl });
|
|
67
|
+
}
|
|
68
|
+
return signalingUrl;
|
|
102
69
|
}
|
|
103
70
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
71
|
+
// Fallback to localStorage if no URL in query params
|
|
72
|
+
return this.loadSignalingUrlFromStorage();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async generatePeerId() {
|
|
76
|
+
const array = new Uint8Array(20);
|
|
77
|
+
|
|
78
|
+
// Environment-aware random value generation
|
|
79
|
+
if (environmentDetector.hasRandomValues) {
|
|
80
|
+
if (environmentDetector.isBrowser || environmentDetector.isWorker || environmentDetector.isNativeScript) {
|
|
81
|
+
crypto.getRandomValues(array);
|
|
82
|
+
} else if (environmentDetector.isNodeJS) {
|
|
83
|
+
// In Node.js, use crypto module (handle both CommonJS and ES modules)
|
|
84
|
+
try {
|
|
85
|
+
if (typeof require !== 'undefined') {
|
|
86
|
+
const crypto = require('crypto');
|
|
87
|
+
const randomBytes = crypto.randomBytes(20);
|
|
88
|
+
array.set(randomBytes);
|
|
89
|
+
} else {
|
|
90
|
+
// ES module approach - import crypto dynamically
|
|
91
|
+
const crypto = await import('crypto');
|
|
92
|
+
const randomBytes = crypto.randomBytes(20);
|
|
93
|
+
array.set(randomBytes);
|
|
94
|
+
}
|
|
95
|
+
} catch (e) {
|
|
96
|
+
console.warn('Could not use Node.js crypto, falling back to Math.random');
|
|
97
|
+
// Fallback to Math.random
|
|
98
|
+
for (let i = 0; i < array.length; i++) {
|
|
99
|
+
array[i] = Math.floor(Math.random() * 256);
|
|
100
|
+
}
|
|
107
101
|
}
|
|
102
|
+
}
|
|
103
|
+
} else {
|
|
104
|
+
// Fallback to less secure random generation
|
|
105
|
+
console.warn('Secure random values not available, using fallback method');
|
|
106
|
+
for (let i = 0; i < array.length; i++) {
|
|
107
|
+
array[i] = Math.floor(Math.random() * 256);
|
|
108
|
+
}
|
|
108
109
|
}
|
|
109
110
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
}
|
|
121
|
-
return {};
|
|
111
|
+
return Array.from(array, byte => byte.toString(16).padStart(2, '0')).join('');
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
validatePeerId(peerId) {
|
|
115
|
+
return typeof peerId === 'string' && /^[a-fA-F0-9]{40}$/.test(peerId);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
saveSettings(settings) {
|
|
119
|
+
if (environmentDetector.hasLocalStorage) {
|
|
120
|
+
localStorage.setItem('pigon-settings', JSON.stringify(settings));
|
|
122
121
|
}
|
|
122
|
+
}
|
|
123
123
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
124
|
+
loadSettings() {
|
|
125
|
+
if (environmentDetector.hasLocalStorage) {
|
|
126
|
+
const saved = localStorage.getItem('pigon-settings');
|
|
127
|
+
if (saved) {
|
|
128
|
+
try {
|
|
129
|
+
return JSON.parse(saved);
|
|
130
|
+
} catch (error) {
|
|
131
|
+
console.error('Failed to parse saved settings:', error);
|
|
128
132
|
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return {};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
clearStorage() {
|
|
139
|
+
if (typeof localStorage !== 'undefined') {
|
|
140
|
+
localStorage.removeItem('pigon-signaling-url');
|
|
141
|
+
localStorage.removeItem('pigon-settings');
|
|
129
142
|
}
|
|
143
|
+
}
|
|
130
144
|
}
|
package/src/TimerUtils.js
CHANGED
|
@@ -9,16 +9,16 @@ import { environmentDetector } from './EnvironmentDetector.js';
|
|
|
9
9
|
let originalSetInterval, originalSetTimeout, originalClearInterval, originalClearTimeout;
|
|
10
10
|
|
|
11
11
|
if (environmentDetector.isBrowser) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
originalSetInterval = window.setInterval;
|
|
13
|
+
originalSetTimeout = window.setTimeout;
|
|
14
|
+
originalClearInterval = window.clearInterval;
|
|
15
|
+
originalClearTimeout = window.clearTimeout;
|
|
16
16
|
} else {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
// In Node.js or other environments, use global functions
|
|
18
|
+
originalSetInterval = setInterval;
|
|
19
|
+
originalSetTimeout = setTimeout;
|
|
20
|
+
originalClearInterval = clearInterval;
|
|
21
|
+
originalClearTimeout = clearTimeout;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
/**
|
|
@@ -28,11 +28,11 @@ if (environmentDetector.isBrowser) {
|
|
|
28
28
|
* @returns {number} - Interval ID
|
|
29
29
|
*/
|
|
30
30
|
export function safeSetInterval(callback, delay) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
31
|
+
if (environmentDetector.isBrowser) {
|
|
32
|
+
return originalSetInterval.call(window, callback, delay);
|
|
33
|
+
} else {
|
|
34
|
+
return originalSetInterval(callback, delay);
|
|
35
|
+
}
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
/**
|
|
@@ -42,11 +42,11 @@ export function safeSetInterval(callback, delay) {
|
|
|
42
42
|
* @returns {number} - Timeout ID
|
|
43
43
|
*/
|
|
44
44
|
export function safeSetTimeout(callback, delay) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
45
|
+
if (environmentDetector.isBrowser) {
|
|
46
|
+
return originalSetTimeout.call(window, callback, delay);
|
|
47
|
+
} else {
|
|
48
|
+
return originalSetTimeout(callback, delay);
|
|
49
|
+
}
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
/**
|
|
@@ -54,11 +54,11 @@ export function safeSetTimeout(callback, delay) {
|
|
|
54
54
|
* @param {number} id - Interval ID to clear
|
|
55
55
|
*/
|
|
56
56
|
export function safeClearInterval(id) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
57
|
+
if (environmentDetector.isBrowser) {
|
|
58
|
+
return originalClearInterval.call(window, id);
|
|
59
|
+
} else {
|
|
60
|
+
return originalClearInterval(id);
|
|
61
|
+
}
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
/**
|
|
@@ -66,9 +66,9 @@ export function safeClearInterval(id) {
|
|
|
66
66
|
* @param {number} id - Timeout ID to clear
|
|
67
67
|
*/
|
|
68
68
|
export function safeClearTimeout(id) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
69
|
+
if (environmentDetector.isBrowser) {
|
|
70
|
+
return originalClearTimeout.call(window, id);
|
|
71
|
+
} else {
|
|
72
|
+
return originalClearTimeout(id);
|
|
73
|
+
}
|
|
74
74
|
}
|