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.
@@ -5,126 +5,126 @@ 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
- constructor(mesh) {
9
- super();
10
- this.mesh = mesh;
11
- }
8
+ constructor(mesh) {
9
+ super();
10
+ this.mesh = mesh;
11
+ }
12
12
 
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');
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
- 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');
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
- loadSignalingUrlFromQuery() {
39
- // Only works in browser environments
40
- if (!environmentDetector.isBrowser) return this.loadSignalingUrlFromStorage();
41
-
42
- const urlParams = new URLSearchParams(window.location.search);
43
- const signalingUrl = urlParams.get('api') || urlParams.get('url') || urlParams.get('signaling');
44
-
45
- if (signalingUrl) {
46
- // Only emit event if URL is different from current one
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 environments
40
+ if (!environmentDetector.isBrowser) return this.loadSignalingUrlFromStorage();
41
+
42
+ const urlParams = new URLSearchParams(window.location.search);
43
+ const signalingUrl = urlParams.get('api') || urlParams.get('url') || urlParams.get('signaling');
44
+
45
+ if (signalingUrl) {
46
+ // Only emit event if URL is different from current one
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;
59
55
  }
60
56
 
61
- async generatePeerId() {
62
- const array = new Uint8Array(20);
63
-
64
- // Environment-aware random value generation
65
- if (environmentDetector.hasRandomValues) {
66
- if (environmentDetector.isBrowser || environmentDetector.isWorker) {
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
- }
57
+ // Fallback to localStorage if no URL in query params
58
+ return this.loadSignalingUrlFromStorage();
59
+ }
60
+
61
+ async generatePeerId() {
62
+ const array = new Uint8Array(20);
63
+
64
+ // Environment-aware random value generation
65
+ if (environmentDetector.hasRandomValues) {
66
+ if (environmentDetector.isBrowser || environmentDetector.isWorker) {
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
+ }
95
87
  }
96
-
97
- return Array.from(array, byte => byte.toString(16).padStart(2, '0')).join('');
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
+ }
98
95
  }
99
96
 
100
- validatePeerId(peerId) {
101
- return typeof peerId === 'string' && /^[a-fA-F0-9]{40}$/.test(peerId);
102
- }
97
+ return Array.from(array, byte => byte.toString(16).padStart(2, '0')).join('');
98
+ }
103
99
 
104
- saveSettings(settings) {
105
- if (environmentDetector.hasLocalStorage) {
106
- localStorage.setItem('pigon-settings', JSON.stringify(settings));
107
- }
100
+ validatePeerId(peerId) {
101
+ return typeof peerId === 'string' && /^[a-fA-F0-9]{40}$/.test(peerId);
102
+ }
103
+
104
+ saveSettings(settings) {
105
+ if (environmentDetector.hasLocalStorage) {
106
+ localStorage.setItem('pigon-settings', JSON.stringify(settings));
108
107
  }
108
+ }
109
109
 
110
- loadSettings() {
111
- if (environmentDetector.hasLocalStorage) {
112
- const saved = localStorage.getItem('pigon-settings');
113
- if (saved) {
114
- try {
115
- return JSON.parse(saved);
116
- } catch (error) {
117
- console.error('Failed to parse saved settings:', error);
118
- }
119
- }
110
+ loadSettings() {
111
+ if (environmentDetector.hasLocalStorage) {
112
+ const saved = localStorage.getItem('pigon-settings');
113
+ if (saved) {
114
+ try {
115
+ return JSON.parse(saved);
116
+ } catch (error) {
117
+ console.error('Failed to parse saved settings:', error);
120
118
  }
121
- return {};
119
+ }
122
120
  }
121
+ return {};
122
+ }
123
123
 
124
- clearStorage() {
125
- if (typeof localStorage !== 'undefined') {
126
- localStorage.removeItem('pigon-signaling-url');
127
- localStorage.removeItem('pigon-settings');
128
- }
124
+ clearStorage() {
125
+ if (typeof localStorage !== 'undefined') {
126
+ localStorage.removeItem('pigon-signaling-url');
127
+ localStorage.removeItem('pigon-settings');
129
128
  }
129
+ }
130
130
  }
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
- originalSetInterval = window.setInterval;
13
- originalSetTimeout = window.setTimeout;
14
- originalClearInterval = window.clearInterval;
15
- originalClearTimeout = window.clearTimeout;
12
+ originalSetInterval = window.setInterval;
13
+ originalSetTimeout = window.setTimeout;
14
+ originalClearInterval = window.clearInterval;
15
+ originalClearTimeout = window.clearTimeout;
16
16
  } else {
17
- // In Node.js or other environments, use global functions
18
- originalSetInterval = setInterval;
19
- originalSetTimeout = setTimeout;
20
- originalClearInterval = clearInterval;
21
- originalClearTimeout = clearTimeout;
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
- if (environmentDetector.isBrowser) {
32
- return originalSetInterval.call(window, callback, delay);
33
- } else {
34
- return originalSetInterval(callback, delay);
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
- if (environmentDetector.isBrowser) {
46
- return originalSetTimeout.call(window, callback, delay);
47
- } else {
48
- return originalSetTimeout(callback, delay);
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
- if (environmentDetector.isBrowser) {
58
- return originalClearInterval.call(window, id);
59
- } else {
60
- return originalClearInterval(id);
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
- if (environmentDetector.isBrowser) {
70
- return originalClearTimeout.call(window, id);
71
- } else {
72
- return originalClearTimeout(id);
73
- }
69
+ if (environmentDetector.isBrowser) {
70
+ return originalClearTimeout.call(window, id);
71
+ } else {
72
+ return originalClearTimeout(id);
73
+ }
74
74
  }