Add Node.js support via WebRTC polyfill injection

- Added WebRTCPolyfill interface for injecting WebRTC implementations
- Added wrtc option to RondevuOptions and RondevuConnectionParams
- Updated Rondevu and RondevuConnection to use injected APIs
- Added helpful error message when RTCPeerConnection is not available
- Updated README with Node.js usage examples
- Version bumped to 0.3.0

Fixes: RTCPeerConnection not defined error in Node.js

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-11-12 22:07:01 +01:00
parent 2b73e6ba44
commit 2e4d0d6a54
6 changed files with 85 additions and 8 deletions

View File

@@ -1,6 +1,6 @@
import { EventEmitter } from './event-emitter.js';
import { RondevuAPI } from './client.js';
import { RondevuConnectionParams } from './types.js';
import { RondevuConnectionParams, WebRTCPolyfill } from './types.js';
/**
* Represents a WebRTC connection with automatic signaling and ICE exchange
@@ -21,6 +21,8 @@ export class RondevuConnection extends EventEmitter {
private connectionTimer?: ReturnType<typeof setTimeout>;
private isPolling: boolean = false;
private isClosed: boolean = false;
private wrtc?: WebRTCPolyfill;
private RTCIceCandidate: typeof RTCIceCandidate;
constructor(params: RondevuConnectionParams, client: RondevuAPI) {
super();
@@ -34,6 +36,10 @@ export class RondevuConnection extends EventEmitter {
this.dataChannels = new Map();
this.pollingIntervalMs = params.pollingInterval;
this.connectionTimeoutMs = params.connectionTimeout;
this.wrtc = params.wrtc;
// Use injected WebRTC polyfill or fall back to global
this.RTCIceCandidate = params.wrtc?.RTCIceCandidate || globalThis.RTCIceCandidate;
this.setupEventHandlers();
this.startConnectionTimeout();
@@ -187,7 +193,7 @@ export class RondevuConnection extends EventEmitter {
for (const candidateStr of offererResponse.answerCandidates) {
try {
const candidate = JSON.parse(candidateStr);
await this.pc.addIceCandidate(new RTCIceCandidate(candidate));
await this.pc.addIceCandidate(new this.RTCIceCandidate(candidate));
} catch (err) {
console.warn('Failed to add ICE candidate:', err);
}
@@ -202,7 +208,7 @@ export class RondevuConnection extends EventEmitter {
for (const candidateStr of answererResponse.offerCandidates) {
try {
const candidate = JSON.parse(candidateStr);
await this.pc.addIceCandidate(new RTCIceCandidate(candidate));
await this.pc.addIceCandidate(new this.RTCIceCandidate(candidate));
} catch (err) {
console.warn('Failed to add ICE candidate:', err);
}