From e954a70aa7129681ac7b6d22b3b4b97a3806b8be Mon Sep 17 00:00:00 2001 From: Bas van den Aakster Date: Sat, 13 Dec 2025 18:47:27 +0100 Subject: [PATCH] Add WebRTC polyfill support for Node.js environments Allow users to pass WebRTC polyfills (RTCPeerConnection, RTCIceCandidate) through RondevuOptions instead of manually setting global variables. The client now automatically applies these to globalThis when provided. This simplifies Node.js integration: - Before: Users had to manually set globalThis.RTCPeerConnection - After: Pass rtcPeerConnection and rtcIceCandidate options Example: const rondevu = await Rondevu.connect({ apiUrl: 'https://api.example.com', username: 'alice', cryptoAdapter: new NodeCryptoAdapter(), rtcPeerConnection: wrtc.RTCPeerConnection, rtcIceCandidate: wrtc.RTCIceCandidate }) --- src/rondevu.ts | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/rondevu.ts b/src/rondevu.ts index 0511f99..9c0da05 100644 --- a/src/rondevu.ts +++ b/src/rondevu.ts @@ -57,6 +57,9 @@ export interface RondevuOptions { batching?: BatcherOptions | false // Optional, defaults to enabled with default options iceServers?: IceServerPreset | RTCIceServer[] // Optional: preset name or custom STUN/TURN servers debug?: boolean // Optional: enable debug logging (default: false) + // WebRTC polyfills for Node.js environments (e.g., wrtc) + rtcPeerConnection?: typeof RTCPeerConnection + rtcIceCandidate?: typeof RTCIceCandidate } export interface OfferContext { @@ -167,6 +170,8 @@ export class Rondevu { private batchingOptions?: BatcherOptions | false private iceServers: RTCIceServer[] private debugEnabled: boolean + private rtcPeerConnection?: typeof RTCPeerConnection + private rtcIceCandidate?: typeof RTCIceCandidate // Service management private currentService: string | null = null @@ -188,7 +193,9 @@ export class Rondevu { iceServers: RTCIceServer[], cryptoAdapter?: CryptoAdapter, batchingOptions?: BatcherOptions | false, - debugEnabled = false + debugEnabled = false, + rtcPeerConnection?: typeof RTCPeerConnection, + rtcIceCandidate?: typeof RTCIceCandidate ) { this.apiUrl = apiUrl this.username = username @@ -198,6 +205,8 @@ export class Rondevu { this.cryptoAdapter = cryptoAdapter this.batchingOptions = batchingOptions this.debugEnabled = debugEnabled + this.rtcPeerConnection = rtcPeerConnection + this.rtcIceCandidate = rtcIceCandidate this.debug('Instance created:', { username: this.username, @@ -230,6 +239,14 @@ export class Rondevu { static async connect(options: RondevuOptions): Promise { const username = options.username || Rondevu.generateAnonymousUsername() + // Apply WebRTC polyfills to global scope if provided (Node.js environments) + if (options.rtcPeerConnection) { + globalThis.RTCPeerConnection = options.rtcPeerConnection as any + } + if (options.rtcIceCandidate) { + globalThis.RTCIceCandidate = options.rtcIceCandidate as any + } + // Handle preset string or custom array let iceServers: RTCIceServer[] if (typeof options.iceServers === 'string') { @@ -277,7 +294,9 @@ export class Rondevu { iceServers, options.cryptoAdapter, options.batching, - options.debug || false + options.debug || false, + options.rtcPeerConnection, + options.rtcIceCandidate ) }