peerpigeon 1.0.0 → 1.0.2

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.
@@ -6,6 +6,7 @@
6
6
  export class EnvironmentDetector {
7
7
  constructor() {
8
8
  this._cache = new Map();
9
+ this._pigeonRTC = null;
9
10
  this._init();
10
11
  }
11
12
 
@@ -21,63 +22,34 @@ export class EnvironmentDetector {
21
22
  this._cache.set('isBun', this._detectBun());
22
23
  this._cache.set('isNativeScript', this._detectNativeScript());
23
24
 
24
- // Initialize WebRTC polyfill for Node.js if available
25
- if (this._cache.get('isNodeJS')) {
26
- this._initNodeWebRTC();
27
- }
25
+ // Initialize PigeonRTC for cross-platform WebRTC support
26
+ this._initPigeonRTC();
28
27
  }
29
28
 
30
29
  /**
31
- * Initialize WebRTC polyfill for Node.js environment
32
- * Attempts to load @koush/wrtc and set up WebRTC globals
30
+ * Initialize PigeonRTC for cross-platform WebRTC support
31
+ * PigeonRTC provides a unified WebRTC interface across browser and Node.js
33
32
  * @private
34
33
  */
35
- _initNodeWebRTC() {
34
+ _initPigeonRTC() {
36
35
  try {
37
- // Check if WebRTC is already available globally
38
- if (typeof global !== 'undefined' && typeof global.RTCPeerConnection !== 'undefined') {
39
- return; // WebRTC already available
40
- }
41
-
42
- // Try to import @koush/wrtc package
43
- let wrtc;
44
- if (typeof require !== 'undefined') {
45
- // CommonJS require
46
- try {
47
- wrtc = require('@koush/wrtc');
48
- } catch (e) {
49
- // @koush/wrtc not available, try node-webrtc as fallback
50
- try {
51
- wrtc = require('node-webrtc');
52
- } catch (e2) {
53
- // No WebRTC package available
54
- return;
55
- }
56
- }
57
- } else {
58
- // ES modules - dynamic import would be needed
59
- // Since we can't do synchronous dynamic imports, we'll skip this
60
- // The async method initWebRTCAsync() should be used instead
61
- return;
62
- }
63
-
64
- // Set up WebRTC globals if we successfully loaded a package
65
- if (wrtc && typeof global !== 'undefined') {
66
- global.RTCPeerConnection = wrtc.RTCPeerConnection;
67
- global.RTCSessionDescription = wrtc.RTCSessionDescription;
68
- global.RTCIceCandidate = wrtc.RTCIceCandidate;
69
- global.MediaStream = wrtc.MediaStream || global.MediaStream;
70
- global.MediaStreamTrack = wrtc.MediaStreamTrack || global.MediaStreamTrack;
71
-
72
- // Update cache to reflect that WebRTC is now available
73
- this._webrtcPolyfilled = true;
74
- }
36
+ // PigeonRTC will be initialized asynchronously via initWebRTCAsync()
37
+ // This is a placeholder to mark that we're using PigeonRTC
38
+ this._webrtcPolyfilled = false;
75
39
  } catch (error) {
76
40
  // Silently fail - WebRTC polyfill is optional
77
41
  // The application will handle missing WebRTC through normal capability detection
78
42
  }
79
43
  }
80
44
 
45
+ /**
46
+ * Get the PigeonRTC instance
47
+ * @returns {PigeonRTC|null} The PigeonRTC instance or null if not initialized
48
+ */
49
+ getPigeonRTC() {
50
+ return this._pigeonRTC;
51
+ }
52
+
81
53
  // Primary environment detection
82
54
  _detectBrowser() {
83
55
  return typeof window !== 'undefined' &&
@@ -180,14 +152,19 @@ export class EnvironmentDetector {
180
152
 
181
153
  // WebRTC capability detection
182
154
  get hasWebRTC() {
155
+ // If PigeonRTC is initialized, use its support check
156
+ if (this._pigeonRTC) {
157
+ return this._pigeonRTC.isSupported();
158
+ }
159
+
160
+ // Fallback to native detection
183
161
  if (this.isBrowser) {
184
162
  return typeof RTCPeerConnection !== 'undefined' ||
185
163
  typeof webkitRTCPeerConnection !== 'undefined' ||
186
164
  typeof mozRTCPeerConnection !== 'undefined';
187
165
  }
188
166
  if (this.isNodeJS) {
189
- // Check for Node.js WebRTC implementations or globally injected WebRTC
190
- // This includes polyfills loaded by _initNodeWebRTC()
167
+ // Check for globally injected WebRTC (from PigeonRTC or other sources)
191
168
  return (typeof global !== 'undefined' &&
192
169
  typeof global.RTCPeerConnection !== 'undefined') ||
193
170
  typeof RTCPeerConnection !== 'undefined' ||
@@ -195,7 +172,6 @@ export class EnvironmentDetector {
195
172
  }
196
173
  if (this.isNativeScript) {
197
174
  // NativeScript can access native WebRTC implementations through native modules
198
- // Check for globally injected WebRTC first
199
175
  return (typeof global !== 'undefined' &&
200
176
  typeof global.RTCPeerConnection !== 'undefined') ||
201
177
  typeof RTCPeerConnection !== 'undefined';
@@ -549,52 +525,53 @@ export class EnvironmentDetector {
549
525
  }
550
526
 
551
527
  /**
552
- * Asynchronously initialize WebRTC polyfill for Node.js environment
553
- * This method is useful for ES module environments where dynamic imports are preferred
528
+ * Asynchronously initialize WebRTC using PigeonRTC
529
+ * This method initializes PigeonRTC for cross-platform WebRTC support
554
530
  * @returns {Promise<boolean>} True if WebRTC was successfully initialized
555
531
  */
556
532
  async initWebRTCAsync() {
557
- if (!this.isNodeJS) {
558
- return false; // Only works in Node.js
559
- }
560
-
561
533
  try {
562
- // Check if WebRTC is already available globally
563
- if (typeof global !== 'undefined' && typeof global.RTCPeerConnection !== 'undefined') {
564
- return true; // WebRTC already available
534
+ // Check if already initialized
535
+ if (this._pigeonRTC) {
536
+ return true;
565
537
  }
566
538
 
567
- let wrtc;
568
- try {
569
- // Try to import @koush/wrtc first
570
- wrtc = await import('@koush/wrtc');
571
- } catch (e) {
539
+ // Check for bundled PigeonRTC in browser builds
540
+ let createPigeonRTC;
541
+ if (typeof globalThis !== 'undefined' && globalThis.__PEERPIGEON_PIGEONRTC__) {
542
+ createPigeonRTC = globalThis.__PEERPIGEON_PIGEONRTC__.createPigeonRTC ||
543
+ globalThis.__PEERPIGEON_PIGEONRTC__.default;
544
+ } else if (typeof window !== 'undefined' && window.__PEERPIGEON_PIGEONRTC__) {
545
+ createPigeonRTC = window.__PEERPIGEON_PIGEONRTC__.createPigeonRTC ||
546
+ window.__PEERPIGEON_PIGEONRTC__.default;
547
+ }
548
+
549
+ // If not found in globals, try dynamic import (Node.js only)
550
+ if (!createPigeonRTC && this.isNodeJS) {
572
551
  try {
573
- // Fallback to node-webrtc
574
- wrtc = await import('node-webrtc');
575
- } catch (e2) {
576
- // No WebRTC package available
577
- return false;
552
+ // Import PigeonRTC dynamically (for non-bundled Node.js environments)
553
+ const pigeonRTCModule = await import('pigeonrtc');
554
+ createPigeonRTC = pigeonRTCModule.createPigeonRTC || pigeonRTCModule.default;
555
+ } catch (error) {
556
+ console.error('Failed to dynamically import PigeonRTC:', error);
578
557
  }
579
558
  }
580
-
581
- // Set up WebRTC globals if we successfully loaded a package
582
- if (wrtc && typeof global !== 'undefined') {
583
- global.RTCPeerConnection = wrtc.RTCPeerConnection || wrtc.default?.RTCPeerConnection;
584
- global.RTCSessionDescription = wrtc.RTCSessionDescription || wrtc.default?.RTCSessionDescription;
585
- global.RTCIceCandidate = wrtc.RTCIceCandidate || wrtc.default?.RTCIceCandidate;
586
- global.MediaStream = wrtc.MediaStream || wrtc.default?.MediaStream || global.MediaStream;
587
- global.MediaStreamTrack = wrtc.MediaStreamTrack || wrtc.default?.MediaStreamTrack || global.MediaStreamTrack;
588
-
589
- // Update cache to reflect that WebRTC is now available
590
- this._webrtcPolyfilled = true;
591
- return true;
559
+
560
+ if (!createPigeonRTC) {
561
+ throw new Error('PigeonRTC createPigeonRTC function not found');
592
562
  }
563
+
564
+ // Initialize PigeonRTC (auto-detects environment)
565
+ this._pigeonRTC = await createPigeonRTC();
566
+
567
+ // Update cache to reflect that WebRTC is now available
568
+ this._webrtcPolyfilled = true;
569
+
570
+ return this._pigeonRTC.isSupported();
593
571
  } catch (error) {
594
- // Silently fail - WebRTC polyfill is optional
572
+ console.error('Failed to initialize PigeonRTC:', error);
573
+ return false;
595
574
  }
596
-
597
- return false;
598
575
  }
599
576
 
600
577
  // Static method for quick environment check
@@ -111,7 +111,14 @@ export class MediaManager extends EventEmitter {
111
111
  throw new Error('At least one of video or audio must be enabled');
112
112
  }
113
113
 
114
- this.localStream = await navigator.mediaDevices.getUserMedia(constraints);
114
+ // Try to use PigeonRTC's getUserMedia if available, otherwise fall back to navigator
115
+ const pigeonRTC = environmentDetector.getPigeonRTC();
116
+ if (pigeonRTC) {
117
+ this.localStream = await pigeonRTC.getUserMedia(constraints);
118
+ } else {
119
+ this.localStream = await navigator.mediaDevices.getUserMedia(constraints);
120
+ }
121
+
115
122
  this.isVideoEnabled = video;
116
123
  this.isAudioEnabled = audio;
117
124
 
@@ -215,10 +222,13 @@ export class MediaManager extends EventEmitter {
215
222
  * Check if browser supports required APIs
216
223
  */
217
224
  static checkSupport() {
225
+ // Get PigeonRTC instance if available
226
+ const pigeonRTC = environmentDetector.getPigeonRTC();
227
+
218
228
  const support = {
219
229
  getUserMedia: !!(navigator.mediaDevices && navigator.mediaDevices.getUserMedia),
220
230
  enumerateDevices: !!(navigator.mediaDevices && navigator.mediaDevices.enumerateDevices),
221
- webRTC: !!(window.RTCPeerConnection)
231
+ webRTC: pigeonRTC ? pigeonRTC.isSupported() : !!(window.RTCPeerConnection)
222
232
  };
223
233
 
224
234
  support.fullSupport = support.getUserMedia && support.enumerateDevices && support.webRTC;
@@ -49,7 +49,16 @@ export class PeerConnection extends EventEmitter {
49
49
  throw error;
50
50
  }
51
51
 
52
- this.connection = new RTCPeerConnection({
52
+ // Get PigeonRTC instance for cross-platform WebRTC support
53
+ const pigeonRTC = environmentDetector.getPigeonRTC();
54
+ if (!pigeonRTC) {
55
+ const error = new Error('PigeonRTC not initialized - call initWebRTCAsync() first');
56
+ this.emit('connectionFailed', { peerId: this.peerId, reason: error.message });
57
+ throw error;
58
+ }
59
+
60
+ // Create peer connection using PigeonRTC
61
+ this.connection = pigeonRTC.createPeerConnection({
53
62
  iceServers: [
54
63
  { urls: 'stun:stun.l.google.com:19302' },
55
64
  { urls: 'stun:stun1.l.google.com:19302' },
@@ -308,16 +308,15 @@ export class PeerPigeonMesh extends EventEmitter {
308
308
 
309
309
  async init() {
310
310
  try {
311
- // Initialize WebRTC polyfill for Node.js if needed
312
- if (this.runtimeInfo?.isNodeJS) {
313
- try {
314
- const webrtcInitialized = await environmentDetector.initWebRTCAsync();
315
- if (webrtcInitialized) {
316
- this.debug.log('🌐 WebRTC polyfill initialized successfully for Node.js environment');
317
- }
318
- } catch (error) {
319
- this.debug.warn('WebRTC polyfill initialization failed:', error.message);
311
+ // Initialize PigeonRTC for cross-platform WebRTC support
312
+ try {
313
+ const webrtcInitialized = await environmentDetector.initWebRTCAsync();
314
+ if (webrtcInitialized) {
315
+ const adapterName = environmentDetector.getPigeonRTC()?.getAdapterName();
316
+ this.debug.log(`🌐 PigeonRTC initialized successfully (${adapterName})`);
320
317
  }
318
+ } catch (error) {
319
+ this.debug.warn('PigeonRTC initialization failed:', error.message);
321
320
  }
322
321
 
323
322
  // Use provided peer ID if valid, otherwise generate one
@@ -0,0 +1,247 @@
1
+ /**
2
+ * Browser-specific PigeonRTC implementation
3
+ * This is a simplified version that only includes browser-compatible code
4
+ */
5
+
6
+ // Simple RTCAdapter base class
7
+ export class RTCAdapter {
8
+ getRTCPeerConnection() {
9
+ throw new Error('getRTCPeerConnection must be implemented by adapter');
10
+ }
11
+
12
+ getRTCSessionDescription() {
13
+ throw new Error('getRTCSessionDescription must be implemented by adapter');
14
+ }
15
+
16
+ getRTCIceCandidate() {
17
+ throw new Error('getRTCIceCandidate must be implemented by adapter');
18
+ }
19
+
20
+ getMediaStream() {
21
+ return null;
22
+ }
23
+
24
+ isSupported() {
25
+ throw new Error('isSupported must be implemented by adapter');
26
+ }
27
+
28
+ getName() {
29
+ throw new Error('getName must be implemented by adapter');
30
+ }
31
+
32
+ async initialize() {
33
+ // Default implementation does nothing
34
+ }
35
+
36
+ async getUserMedia(_constraints) {
37
+ throw new Error('getUserMedia not supported by this adapter');
38
+ }
39
+
40
+ async getDisplayMedia(_constraints) {
41
+ throw new Error('getDisplayMedia not supported by this adapter');
42
+ }
43
+ }
44
+
45
+ // Browser RTCAdapter
46
+ export class BrowserRTCAdapter extends RTCAdapter {
47
+ constructor() {
48
+ super();
49
+ this._checkSupport();
50
+ }
51
+
52
+ _checkSupport() {
53
+ if (typeof window === 'undefined' || typeof navigator === 'undefined') {
54
+ return;
55
+ }
56
+
57
+ this.hasRTCPeerConnection = !!(
58
+ window.RTCPeerConnection ||
59
+ window.webkitRTCPeerConnection ||
60
+ window.mozRTCPeerConnection
61
+ );
62
+
63
+ this.hasGetUserMedia = !!(
64
+ navigator.mediaDevices?.getUserMedia ||
65
+ navigator.getUserMedia ||
66
+ navigator.webkitGetUserMedia ||
67
+ navigator.mozGetUserMedia
68
+ );
69
+
70
+ this.hasGetDisplayMedia = !!(
71
+ navigator.mediaDevices?.getDisplayMedia
72
+ );
73
+ }
74
+
75
+ getRTCPeerConnection() {
76
+ if (typeof window === 'undefined') {
77
+ throw new Error('BrowserRTCAdapter requires a browser environment');
78
+ }
79
+
80
+ return window.RTCPeerConnection ||
81
+ window.webkitRTCPeerConnection ||
82
+ window.mozRTCPeerConnection;
83
+ }
84
+
85
+ getRTCSessionDescription() {
86
+ if (typeof window === 'undefined') {
87
+ throw new Error('BrowserRTCAdapter requires a browser environment');
88
+ }
89
+
90
+ return window.RTCSessionDescription ||
91
+ window.mozRTCSessionDescription;
92
+ }
93
+
94
+ getRTCIceCandidate() {
95
+ if (typeof window === 'undefined') {
96
+ throw new Error('BrowserRTCAdapter requires a browser environment');
97
+ }
98
+
99
+ return window.RTCIceCandidate ||
100
+ window.mozRTCIceCandidate;
101
+ }
102
+
103
+ getMediaStream() {
104
+ if (typeof window === 'undefined') {
105
+ return null;
106
+ }
107
+
108
+ return window.MediaStream || window.webkitMediaStream;
109
+ }
110
+
111
+ isSupported() {
112
+ return typeof window !== 'undefined' && this.hasRTCPeerConnection;
113
+ }
114
+
115
+ getName() {
116
+ return 'BrowserRTCAdapter';
117
+ }
118
+
119
+ async getUserMedia(constraints) {
120
+ if (typeof navigator === 'undefined') {
121
+ throw new Error('getUserMedia requires a browser environment');
122
+ }
123
+
124
+ if (navigator.mediaDevices?.getUserMedia) {
125
+ return await navigator.mediaDevices.getUserMedia(constraints);
126
+ }
127
+
128
+ const getUserMedia = navigator.getUserMedia ||
129
+ navigator.webkitGetUserMedia ||
130
+ navigator.mozGetUserMedia;
131
+
132
+ if (!getUserMedia) {
133
+ throw new Error('getUserMedia is not supported in this browser');
134
+ }
135
+
136
+ return new Promise((resolve, reject) => {
137
+ getUserMedia.call(navigator, constraints, resolve, reject);
138
+ });
139
+ }
140
+
141
+ async getDisplayMedia(constraints) {
142
+ if (typeof navigator === 'undefined') {
143
+ throw new Error('getDisplayMedia requires a browser environment');
144
+ }
145
+
146
+ if (!navigator.mediaDevices?.getDisplayMedia) {
147
+ throw new Error('getDisplayMedia is not supported in this browser');
148
+ }
149
+
150
+ return await navigator.mediaDevices.getDisplayMedia(constraints);
151
+ }
152
+ }
153
+
154
+ // Simplified PigeonRTC class for browser
155
+ export class PigeonRTC {
156
+ constructor(options = {}) {
157
+ this.adapter = options.adapter || null;
158
+ this.initialized = false;
159
+ }
160
+
161
+ async initialize(options = {}) {
162
+ if (this.initialized) {
163
+ return;
164
+ }
165
+
166
+ if (options.adapter) {
167
+ this.adapter = options.adapter;
168
+ }
169
+
170
+ if (!this.adapter) {
171
+ this.adapter = new BrowserRTCAdapter();
172
+ }
173
+
174
+ await this.adapter.initialize();
175
+ this.initialized = true;
176
+ }
177
+
178
+ _ensureInitialized() {
179
+ if (!this.initialized || !this.adapter) {
180
+ throw new Error('PigeonRTC not initialized. Call initialize() first.');
181
+ }
182
+ }
183
+
184
+ getRTCPeerConnection() {
185
+ this._ensureInitialized();
186
+ return this.adapter.getRTCPeerConnection();
187
+ }
188
+
189
+ getRTCSessionDescription() {
190
+ this._ensureInitialized();
191
+ return this.adapter.getRTCSessionDescription();
192
+ }
193
+
194
+ getRTCIceCandidate() {
195
+ this._ensureInitialized();
196
+ return this.adapter.getRTCIceCandidate();
197
+ }
198
+
199
+ getMediaStream() {
200
+ this._ensureInitialized();
201
+ return this.adapter.getMediaStream();
202
+ }
203
+
204
+ createPeerConnection(config) {
205
+ this._ensureInitialized();
206
+ const RTCPeerConnection = this.adapter.getRTCPeerConnection();
207
+ return new RTCPeerConnection(config);
208
+ }
209
+
210
+ createSessionDescription(init) {
211
+ this._ensureInitialized();
212
+ const RTCSessionDescription = this.adapter.getRTCSessionDescription();
213
+ return new RTCSessionDescription(init);
214
+ }
215
+
216
+ createIceCandidate(init) {
217
+ this._ensureInitialized();
218
+ const RTCIceCandidate = this.adapter.getRTCIceCandidate();
219
+ return new RTCIceCandidate(init);
220
+ }
221
+
222
+ async getUserMedia(constraints) {
223
+ this._ensureInitialized();
224
+ return await this.adapter.getUserMedia(constraints);
225
+ }
226
+
227
+ async getDisplayMedia(constraints) {
228
+ this._ensureInitialized();
229
+ return await this.adapter.getDisplayMedia(constraints);
230
+ }
231
+
232
+ isSupported() {
233
+ return this.adapter ? this.adapter.isSupported() : false;
234
+ }
235
+
236
+ getAdapterName() {
237
+ return this.adapter ? this.adapter.getName() : 'None';
238
+ }
239
+ }
240
+
241
+ export async function createPigeonRTC(options = {}) {
242
+ const rtc = new PigeonRTC(options);
243
+ await rtc.initialize(options);
244
+ return rtc;
245
+ }
246
+
247
+ export default createPigeonRTC;
@@ -4,6 +4,9 @@
4
4
  // Import UnSEA directly from node_modules (esbuild will bundle it)
5
5
  import * as UnSEA from 'unsea';
6
6
 
7
+ // Import browser-specific PigeonRTC (avoids Node.js dependencies)
8
+ import { createPigeonRTC, PigeonRTC, BrowserRTCAdapter, RTCAdapter } from './PigeonRTC-browser.js';
9
+
7
10
  // Import only browser-compatible components
8
11
  export { PeerPigeonMesh } from '../src/PeerPigeonMesh.js';
9
12
  export { PeerConnection } from '../src/PeerConnection.js';
@@ -23,18 +26,33 @@ export {
23
26
  isWorker,
24
27
  hasWebRTC,
25
28
  hasWebSocket,
26
- getEnvironmentReport
29
+ getEnvironmentReport,
30
+ initWebRTCAsync
27
31
  } from '../src/EnvironmentDetector.js';
28
32
 
29
33
  // Make UnSEA available globally for the existing dynamic import logic
30
34
  // This allows the CryptoManager to detect and use the bundled version
31
35
  if (typeof globalThis !== 'undefined') {
32
36
  globalThis.__PEERPIGEON_UNSEA__ = UnSEA;
37
+ globalThis.__PEERPIGEON_PIGEONRTC__ = {
38
+ createPigeonRTC,
39
+ PigeonRTC,
40
+ BrowserRTCAdapter,
41
+ RTCAdapter,
42
+ default: createPigeonRTC
43
+ };
33
44
  }
34
45
 
35
46
  // Also set it on window for browser compatibility
36
47
  if (typeof window !== 'undefined') {
37
48
  window.__PEERPIGEON_UNSEA__ = UnSEA;
49
+ window.__PEERPIGEON_PIGEONRTC__ = {
50
+ createPigeonRTC,
51
+ PigeonRTC,
52
+ BrowserRTCAdapter,
53
+ RTCAdapter,
54
+ default: createPigeonRTC
55
+ };
38
56
  }
39
57
 
40
- console.log('🔐 PeerPigeon browser bundle loaded with embedded UnSEA crypto');
58
+ console.log('🔐 PeerPigeon browser bundle loaded with embedded UnSEA crypto and PigeonRTC');