peerpigeon 0.0.6 → 0.0.8

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.
@@ -18,6 +18,7 @@ export class EnvironmentDetector {
18
18
  this._cache.set('isSharedWorker', this._detectSharedWorker());
19
19
  this._cache.set('isDeno', this._detectDeno());
20
20
  this._cache.set('isBun', this._detectBun());
21
+ this._cache.set('isNativeScript', this._detectNativeScript());
21
22
  }
22
23
 
23
24
  // Primary environment detection
@@ -41,6 +42,15 @@ export class EnvironmentDetector {
41
42
  return typeof Bun !== 'undefined';
42
43
  }
43
44
 
45
+ _detectNativeScript() {
46
+ return typeof global !== 'undefined' &&
47
+ ((typeof global.NativeScriptGlobals !== 'undefined') ||
48
+ (typeof global.__ANDROID__ !== 'undefined') ||
49
+ (typeof global.__IOS__ !== 'undefined') ||
50
+ (typeof global.__VISIONOS__ !== 'undefined') ||
51
+ (typeof global.loadModule === 'function' && typeof global.registerModule === 'function'));
52
+ }
53
+
44
54
  _detectWorker() {
45
55
  return typeof importScripts !== 'undefined' ||
46
56
  this._detectServiceWorker() ||
@@ -99,12 +109,16 @@ export class EnvironmentDetector {
99
109
  return this._cache.get('isBun');
100
110
  }
101
111
 
112
+ get isNativeScript() {
113
+ return this._cache.get('isNativeScript');
114
+ }
115
+
102
116
  get isServer() {
103
117
  return this.isNodeJS || this.isDeno || this.isBun;
104
118
  }
105
119
 
106
120
  get isClient() {
107
- return this.isBrowser || this.isWorker;
121
+ return this.isBrowser || this.isWorker || this.isNativeScript;
108
122
  }
109
123
 
110
124
  // WebRTC capability detection
@@ -120,20 +134,27 @@ export class EnvironmentDetector {
120
134
  typeof global.RTCPeerConnection !== 'undefined') ||
121
135
  typeof RTCPeerConnection !== 'undefined';
122
136
  }
137
+ if (this.isNativeScript) {
138
+ // NativeScript can access native WebRTC implementations through native modules
139
+ // Check for globally injected WebRTC first
140
+ return (typeof global !== 'undefined' &&
141
+ typeof global.RTCPeerConnection !== 'undefined') ||
142
+ typeof RTCPeerConnection !== 'undefined';
143
+ }
123
144
  return false;
124
145
  }
125
146
 
126
147
  get hasDataChannel() {
127
- return this.hasWebRTC && this.isBrowser;
148
+ return this.hasWebRTC && (this.isBrowser || this.isNativeScript);
128
149
  }
129
150
 
130
151
  get hasGetUserMedia() {
131
- if (!this.isBrowser) return false;
132
- return !!(navigator.mediaDevices && navigator.mediaDevices.getUserMedia) ||
133
- !!(navigator.getUserMedia ||
152
+ if (!this.isBrowser && !this.isNativeScript) return false;
153
+ return !!(navigator && navigator.mediaDevices && navigator.mediaDevices.getUserMedia) ||
154
+ !!(navigator && (navigator.getUserMedia ||
134
155
  navigator.webkitGetUserMedia ||
135
156
  navigator.mozGetUserMedia ||
136
- navigator.msGetUserMedia);
157
+ navigator.msGetUserMedia));
137
158
  }
138
159
 
139
160
  // WebSocket capability detection
@@ -164,12 +185,17 @@ export class EnvironmentDetector {
164
185
  return false;
165
186
  }
166
187
  }
188
+ if (this.isNativeScript) {
189
+ // NativeScript has WebSocket support through global injection or native modules
190
+ return typeof WebSocket !== 'undefined' ||
191
+ (typeof global !== 'undefined' && typeof global.WebSocket !== 'undefined');
192
+ }
167
193
  return false;
168
194
  }
169
195
 
170
196
  // Storage capability detection
171
197
  get hasLocalStorage() {
172
- if (!this.isBrowser) return false;
198
+ if (!this.isBrowser && !this.isNativeScript) return false;
173
199
  try {
174
200
  const test = '__storage_test__';
175
201
  localStorage.setItem(test, test);
@@ -181,7 +207,7 @@ export class EnvironmentDetector {
181
207
  }
182
208
 
183
209
  get hasSessionStorage() {
184
- if (!this.isBrowser) return false;
210
+ if (!this.isBrowser && !this.isNativeScript) return false;
185
211
  try {
186
212
  const test = '__storage_test__';
187
213
  sessionStorage.setItem(test, test);
@@ -193,12 +219,12 @@ export class EnvironmentDetector {
193
219
  }
194
220
 
195
221
  get hasIndexedDB() {
196
- if (!this.isBrowser) return false;
222
+ if (!this.isBrowser) return false; // NativeScript doesn't typically have IndexedDB
197
223
  return typeof indexedDB !== 'undefined';
198
224
  }
199
225
 
200
226
  get hasCookies() {
201
- if (!this.isBrowser) return false;
227
+ if (!this.isBrowser) return false; // NativeScript doesn't have cookies
202
228
  return typeof document !== 'undefined' &&
203
229
  typeof document.cookie === 'string';
204
230
  }
@@ -243,6 +269,11 @@ export class EnvironmentDetector {
243
269
  return false;
244
270
  }
245
271
  }
272
+ if (this.isNativeScript) {
273
+ // NativeScript may have crypto through global polyfills or native modules
274
+ return (typeof crypto !== 'undefined' && typeof crypto.subtle !== 'undefined') ||
275
+ (typeof global !== 'undefined' && typeof global.crypto !== 'undefined');
276
+ }
246
277
  return false;
247
278
  }
248
279
 
@@ -264,6 +295,13 @@ export class EnvironmentDetector {
264
295
  return false;
265
296
  }
266
297
  }
298
+ if (this.isNativeScript) {
299
+ // NativeScript may have crypto random values through global polyfills
300
+ return (typeof crypto !== 'undefined' && typeof crypto.getRandomValues === 'function') ||
301
+ (typeof global !== 'undefined' &&
302
+ typeof global.crypto !== 'undefined' &&
303
+ typeof global.crypto.getRandomValues === 'function');
304
+ }
267
305
  return false;
268
306
  }
269
307
 
@@ -318,6 +356,34 @@ export class EnvironmentDetector {
318
356
  };
319
357
  }
320
358
 
359
+ // NativeScript-specific detection
360
+ getNativeScriptInfo() {
361
+ if (!this.isNativeScript) return null;
362
+
363
+ const info = {
364
+ runtime: 'nativescript'
365
+ };
366
+
367
+ // Detect platform if globals are available
368
+ if (typeof global !== 'undefined') {
369
+ if (global.__ANDROID__) {
370
+ info.platform = 'android';
371
+ } else if (global.__IOS__) {
372
+ info.platform = 'ios';
373
+ } else if (global.__VISIONOS__) {
374
+ info.platform = 'visionos';
375
+ }
376
+
377
+ // Add additional NativeScript globals if available
378
+ if (global.NativeScriptGlobals) {
379
+ info.launched = global.NativeScriptGlobals.launched;
380
+ info.appInstanceReady = global.NativeScriptGlobals.appInstanceReady;
381
+ }
382
+ }
383
+
384
+ return info;
385
+ }
386
+
321
387
  // Device and platform detection
322
388
  getPlatformInfo() {
323
389
  if (this.isBrowser) {
@@ -336,6 +402,10 @@ export class EnvironmentDetector {
336
402
  return this.getNodeInfo();
337
403
  }
338
404
 
405
+ if (this.isNativeScript) {
406
+ return this.getNativeScriptInfo();
407
+ }
408
+
339
409
  return null;
340
410
  }
341
411
 
@@ -390,6 +460,7 @@ export class EnvironmentDetector {
390
460
  isSharedWorker: this.isSharedWorker,
391
461
  isDeno: this.isDeno,
392
462
  isBun: this.isBun,
463
+ isNativeScript: this.isNativeScript,
393
464
  isServer: this.isServer,
394
465
  isClient: this.isClient
395
466
  },
@@ -410,6 +481,7 @@ export class EnvironmentDetector {
410
481
  platform: this.getPlatformInfo(),
411
482
  browser: this.getBrowserInfo(),
412
483
  node: this.getNodeInfo(),
484
+ nativescript: this.getNativeScriptInfo(),
413
485
  network: {
414
486
  online: this.isOnline,
415
487
  type: this.networkType
@@ -436,6 +508,7 @@ export const environmentDetector = new EnvironmentDetector();
436
508
  export const isBrowser = () => environmentDetector.isBrowser;
437
509
  export const isNodeJS = () => environmentDetector.isNodeJS;
438
510
  export const isWorker = () => environmentDetector.isWorker;
511
+ export const isNativeScript = () => environmentDetector.isNativeScript;
439
512
  export const hasWebRTC = () => environmentDetector.hasWebRTC;
440
513
  export const hasWebSocket = () => environmentDetector.hasWebSocket;
441
514
  export const getEnvironmentReport = () => environmentDetector.getEnvironmentReport();
@@ -53,8 +53,10 @@ export class MediaManager extends EventEmitter {
53
53
  * Get available media devices
54
54
  */
55
55
  async enumerateDevices() {
56
- // Only available in browser environments with media device support
57
- if (!environmentDetector.isBrowser || typeof navigator.mediaDevices === 'undefined') {
56
+ // Only available in browser and NativeScript environments with media device support
57
+ if ((!environmentDetector.isBrowser && !environmentDetector.isNativeScript) ||
58
+ typeof navigator === 'undefined' ||
59
+ typeof navigator.mediaDevices === 'undefined') {
58
60
  console.warn('Media device enumeration not available in this environment');
59
61
  return;
60
62
  }
@@ -11,6 +11,7 @@ import { GossipManager } from './GossipManager.js';
11
11
  import { MediaManager } from './MediaManager.js';
12
12
  import { WebDHT } from './WebDHT.js';
13
13
  import { CryptoManager } from './CryptoManager.js';
14
+ import { DistributedStorageManager } from './DistributedStorageManager.js';
14
15
  import { environmentDetector } from './EnvironmentDetector.js';
15
16
 
16
17
  export class PeerPigeonMesh extends EventEmitter {
@@ -51,6 +52,7 @@ export class PeerPigeonMesh extends EventEmitter {
51
52
  this.signalingHandler = new SignalingHandler(this, this.connectionManager);
52
53
  this.gossipManager = new GossipManager(this, this.connectionManager);
53
54
  this.webDHT = null; // Will be initialized after peerId is set
55
+ this.distributedStorage = null; // Will be initialized after WebDHT is set
54
56
 
55
57
  // Initialize crypto manager if enabled
56
58
  this.cryptoManager = null;
@@ -135,10 +137,11 @@ export class PeerPigeonMesh extends EventEmitter {
135
137
 
136
138
  // Log environment info
137
139
  console.log('🔍 PeerPigeon Environment Detection:', {
138
- runtime: `${report.runtime.isBrowser ? 'Browser' : ''}${report.runtime.isNodeJS ? 'Node.js' : ''}${report.runtime.isWorker ? 'Worker' : ''}`,
140
+ runtime: `${report.runtime.isBrowser ? 'Browser' : ''}${report.runtime.isNodeJS ? 'Node.js' : ''}${report.runtime.isWorker ? 'Worker' : ''}${report.runtime.isNativeScript ? 'NativeScript' : ''}`,
139
141
  webrtc: report.capabilities.webrtc,
140
142
  websocket: report.capabilities.webSocket,
141
- browser: report.browser?.name || 'N/A'
143
+ browser: report.browser?.name || 'N/A',
144
+ nativescript: report.nativescript?.platform || 'N/A'
142
145
  });
143
146
 
144
147
  // Check WebRTC support (required for peer connections)
@@ -147,6 +150,8 @@ export class PeerPigeonMesh extends EventEmitter {
147
150
  errors.push('WebRTC is not supported in this browser. PeerPigeon requires WebRTC for peer-to-peer connections.');
148
151
  } else if (report.runtime.isNodeJS) {
149
152
  warnings.push('WebRTC support not detected in Node.js environment. Consider using a WebRTC library like node-webrtc.');
153
+ } else if (report.runtime.isNativeScript) {
154
+ warnings.push('WebRTC support not detected in NativeScript environment. Consider using a native WebRTC plugin.');
150
155
  }
151
156
  }
152
157
 
@@ -156,11 +161,13 @@ export class PeerPigeonMesh extends EventEmitter {
156
161
  errors.push('WebSocket is not supported in this browser. PeerPigeon requires WebSocket for signaling.');
157
162
  } else if (report.runtime.isNodeJS) {
158
163
  warnings.push('WebSocket support not detected. Install the "ws" package for WebSocket support in Node.js.');
164
+ } else if (report.runtime.isNativeScript) {
165
+ warnings.push('WebSocket support not detected. Consider using a native WebSocket plugin or polyfill.');
159
166
  }
160
167
  }
161
168
 
162
169
  // Check storage capabilities for persistent peer ID
163
- if (report.runtime.isBrowser && !report.capabilities.localStorage && !report.capabilities.sessionStorage) {
170
+ if ((report.runtime.isBrowser || report.runtime.isNativeScript) && !report.capabilities.localStorage && !report.capabilities.sessionStorage) {
164
171
  warnings.push('No storage mechanism available. Peer ID will not persist between sessions.');
165
172
  }
166
173
 
@@ -188,6 +195,21 @@ export class PeerPigeonMesh extends EventEmitter {
188
195
  }
189
196
  }
190
197
 
198
+ if (report.runtime.isNativeScript) {
199
+ // NativeScript-specific checks
200
+ const nativeScript = report.nativescript;
201
+ if (nativeScript && nativeScript.platform) {
202
+ console.log(`🔮 Running on NativeScript ${nativeScript.platform} platform`);
203
+
204
+ // Platform-specific considerations
205
+ if (nativeScript.platform === 'android') {
206
+ warnings.push('Android WebRTC may require network permissions and appropriate security configurations.');
207
+ } else if (nativeScript.platform === 'ios' || nativeScript.platform === 'visionos') {
208
+ warnings.push('iOS/visionOS WebRTC may require camera/microphone permissions for media features.');
209
+ }
210
+ }
211
+ }
212
+
191
213
  // Handle errors and warnings
192
214
  if (errors.length > 0) {
193
215
  const errorMessage = 'PeerPigeon environment validation failed:\n' + errors.join('\n');
@@ -230,6 +252,13 @@ export class PeerPigeonMesh extends EventEmitter {
230
252
 
231
253
  // Setup WebDHT event handlers now that it's initialized
232
254
  this.setupWebDHTEventHandlers();
255
+
256
+ // Initialize DistributedStorageManager if WebDHT is enabled
257
+ this.distributedStorage = new DistributedStorageManager(this);
258
+ console.log('DistributedStorageManager initialized');
259
+
260
+ // Setup DistributedStorageManager event handlers
261
+ this.setupDistributedStorageEventHandlers();
233
262
  } else {
234
263
  console.log('WebDHT disabled by configuration');
235
264
  }
@@ -811,6 +840,38 @@ export class PeerPigeonMesh extends EventEmitter {
811
840
  }
812
841
  }
813
842
 
843
+ /**
844
+ * Setup event handlers for DistributedStorageManager
845
+ */
846
+ setupDistributedStorageEventHandlers() {
847
+ // Only set up if distributedStorage exists
848
+ if (this.distributedStorage) {
849
+ this.distributedStorage.addEventListener('dataStored', (data) => {
850
+ this.emit('storageDataStored', data);
851
+ });
852
+
853
+ this.distributedStorage.addEventListener('dataRetrieved', (data) => {
854
+ this.emit('storageDataRetrieved', data);
855
+ });
856
+
857
+ this.distributedStorage.addEventListener('dataUpdated', (data) => {
858
+ this.emit('storageDataUpdated', data);
859
+ });
860
+
861
+ this.distributedStorage.addEventListener('dataDeleted', (data) => {
862
+ this.emit('storageDataDeleted', data);
863
+ });
864
+
865
+ this.distributedStorage.addEventListener('accessGranted', (data) => {
866
+ this.emit('storageAccessGranted', data);
867
+ });
868
+
869
+ this.distributedStorage.addEventListener('accessRevoked', (data) => {
870
+ this.emit('storageAccessRevoked', data);
871
+ });
872
+ }
873
+ }
874
+
814
875
  /**
815
876
  * Connect to a specific peer by ID
816
877
  */
@@ -60,8 +60,8 @@ export class SignalingClient extends EventEmitter {
60
60
  console.warn('ws package not found in Node.js environment. Install with: npm install ws');
61
61
  throw new Error('WebSocket not available in Node.js. Install the "ws" package.');
62
62
  }
63
- } else if (environmentDetector.isBrowser || environmentDetector.isWorker) {
64
- // In browser or worker environments, use the native WebSocket
63
+ } else if (environmentDetector.isBrowser || environmentDetector.isWorker || environmentDetector.isNativeScript) {
64
+ // In browser, worker, or NativeScript environments, use the native WebSocket
65
65
  return new WebSocket(url);
66
66
  } else {
67
67
  throw new Error('WebSocket not supported in this environment');
@@ -383,8 +383,8 @@ export class SignalingClient extends EventEmitter {
383
383
  }
384
384
  };
385
385
 
386
- if (environmentDetector.isBrowser) {
387
- this.keepAliveInterval = window.setInterval(keepAliveCallback, this.keepAliveIntervalMs);
386
+ if (environmentDetector.isBrowser || environmentDetector.isNativeScript) {
387
+ this.keepAliveInterval = (typeof window !== 'undefined' ? window.setInterval : setInterval)(keepAliveCallback, this.keepAliveIntervalMs);
388
388
  } else {
389
389
  this.keepAliveInterval = setInterval(keepAliveCallback, this.keepAliveIntervalMs);
390
390
  }
@@ -467,8 +467,8 @@ export class SignalingClient extends EventEmitter {
467
467
  }
468
468
  };
469
469
 
470
- if (environmentDetector.isBrowser) {
471
- this.healthCheckInterval = window.setInterval(healthCheckCallback, 30000);
470
+ if (environmentDetector.isBrowser || environmentDetector.isNativeScript) {
471
+ this.healthCheckInterval = (typeof window !== 'undefined' ? window.setInterval : setInterval)(healthCheckCallback, 30000);
472
472
  } else {
473
473
  this.healthCheckInterval = setInterval(healthCheckCallback, 30000);
474
474
  }
@@ -36,11 +36,25 @@ export class StorageManager extends EventEmitter {
36
36
  }
37
37
 
38
38
  loadSignalingUrlFromQuery() {
39
- // Only works in browser environments
40
- if (!environmentDetector.isBrowser) return this.loadSignalingUrlFromStorage();
39
+ // Only works in browser and NativeScript environments with location/URL support
40
+ if (!environmentDetector.isBrowser && !environmentDetector.isNativeScript) return this.loadSignalingUrlFromStorage();
41
41
 
42
- const urlParams = new URLSearchParams(window.location.search);
43
- const signalingUrl = urlParams.get('api') || urlParams.get('url') || urlParams.get('signaling');
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();
47
+ }
48
+
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();
55
+ }
56
+
57
+ const signalingUrl = searchParams?.get('api') || searchParams?.get('url') || searchParams?.get('signaling');
44
58
 
45
59
  if (signalingUrl) {
46
60
  // Only emit event if URL is different from current one
@@ -63,7 +77,7 @@ export class StorageManager extends EventEmitter {
63
77
 
64
78
  // Environment-aware random value generation
65
79
  if (environmentDetector.hasRandomValues) {
66
- if (environmentDetector.isBrowser || environmentDetector.isWorker) {
80
+ if (environmentDetector.isBrowser || environmentDetector.isWorker || environmentDetector.isNativeScript) {
67
81
  crypto.getRandomValues(array);
68
82
  } else if (environmentDetector.isNodeJS) {
69
83
  // In Node.js, use crypto module (handle both CommonJS and ES modules)