node-rtc-connection 1.0.5 → 1.0.7
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.
- package/README.md +8 -8
- package/dist/index.cjs +125 -17
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +125 -17
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/NativePeerConnectionFactory.js +125 -17
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-rtc-connection",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
4
4
|
"description": "WebRTC DataChannel implementation for Node.js with STUN, TURN, NAT traversal, and encryption. Pure Node.js, no native dependencies.",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -100,6 +100,9 @@ class NativePeerConnection extends EventEmitter {
|
|
|
100
100
|
this._secureConnection = null;
|
|
101
101
|
this._udpTransport = null;
|
|
102
102
|
this._iceCandidates = [];
|
|
103
|
+
this._remoteCandidates = [];
|
|
104
|
+
this._selectedLocalCandidate = null;
|
|
105
|
+
this._selectedRemoteCandidate = null;
|
|
103
106
|
|
|
104
107
|
console.log('[NativePeerConnection] Created with STUN/ICE/Encryption support');
|
|
105
108
|
console.log(` - Encryption: ${this._useEncryption ? 'enabled' : 'disabled (requires valid certs)'}`);
|
|
@@ -245,22 +248,106 @@ class NativePeerConnection extends EventEmitter {
|
|
|
245
248
|
}
|
|
246
249
|
|
|
247
250
|
if (!candidate || !candidate.candidate) {
|
|
251
|
+
// null candidate signals end of candidates - try to select best pair
|
|
252
|
+
if (this._remoteCandidates.length > 0 && !this._selectedRemoteCandidate) {
|
|
253
|
+
this._selectBestCandidatePair();
|
|
254
|
+
}
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// Parse ICE candidate
|
|
259
|
+
const parsed = this._parseIceCandidate(candidate.candidate);
|
|
260
|
+
if (!parsed) {
|
|
248
261
|
return;
|
|
249
262
|
}
|
|
250
263
|
|
|
251
|
-
//
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
264
|
+
// Store the remote candidate
|
|
265
|
+
this._remoteCandidates.push(parsed);
|
|
266
|
+
console.log(`[NativePeerConnection] Added remote ICE candidate (${parsed.type}): ${parsed.ip}:${parsed.port}`);
|
|
267
|
+
|
|
268
|
+
// For backward compatibility, set remote address immediately if not set
|
|
269
|
+
if (!this._remoteAddress) {
|
|
270
|
+
this._remoteAddress = parsed.ip;
|
|
271
|
+
this._remotePort = parsed.port;
|
|
256
272
|
console.log(`[NativePeerConnection] Remote address: ${this._remoteAddress}:${this._remotePort}`);
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// If we haven't selected a pair yet, try to select now
|
|
276
|
+
if (!this._selectedRemoteCandidate && this._iceCandidates.length > 0) {
|
|
277
|
+
this._selectBestCandidatePair();
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// If we selected a pair and are offerer, connect
|
|
281
|
+
if (this._selectedRemoteCandidate && this._isOfferer &&
|
|
282
|
+
this._signalingState === 0 && !this._socket) {
|
|
283
|
+
await this._connectToPeer();
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Parse ICE candidate string
|
|
289
|
+
* @private
|
|
290
|
+
*/
|
|
291
|
+
_parseIceCandidate(candidateStr) {
|
|
292
|
+
const parts = candidateStr.split(' ');
|
|
293
|
+
if (parts.length < 6) {
|
|
294
|
+
return null;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
return {
|
|
298
|
+
foundation: parts[0].replace('candidate:', ''),
|
|
299
|
+
component: parts[1],
|
|
300
|
+
protocol: parts[2],
|
|
301
|
+
priority: parseInt(parts[3], 10),
|
|
302
|
+
ip: parts[4],
|
|
303
|
+
port: parseInt(parts[5], 10),
|
|
304
|
+
type: parts[7], // typ host/srflx/relay
|
|
305
|
+
relatedAddress: parts[9] || null,
|
|
306
|
+
relatedPort: parts[11] ? parseInt(parts[11], 10) : null
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Select best candidate pair for connection
|
|
312
|
+
* Prioritizes: relay > srflx > host
|
|
313
|
+
* @private
|
|
314
|
+
*/
|
|
315
|
+
_selectBestCandidatePair() {
|
|
316
|
+
if (this._remoteCandidates.length === 0 || this._iceCandidates.length === 0) {
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// Priority order: relay > srflx > host
|
|
321
|
+
const typePriority = { 'relay': 3, 'srflx': 2, 'host': 1 };
|
|
322
|
+
|
|
323
|
+
// Find best local candidate
|
|
324
|
+
let bestLocal = this._iceCandidates[0];
|
|
325
|
+
for (const candidate of this._iceCandidates) {
|
|
326
|
+
if (typePriority[candidate.type] > typePriority[bestLocal.type]) {
|
|
327
|
+
bestLocal = candidate;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// Find best remote candidate
|
|
332
|
+
let bestRemote = this._remoteCandidates[0];
|
|
333
|
+
for (const candidate of this._remoteCandidates) {
|
|
334
|
+
if (typePriority[candidate.type] > typePriority[bestRemote.type]) {
|
|
335
|
+
bestRemote = candidate;
|
|
262
336
|
}
|
|
263
337
|
}
|
|
338
|
+
|
|
339
|
+
this._selectedLocalCandidate = bestLocal;
|
|
340
|
+
this._selectedRemoteCandidate = bestRemote;
|
|
341
|
+
|
|
342
|
+
// Update addresses for connection
|
|
343
|
+
this._localAddress = bestLocal.ip;
|
|
344
|
+
this._localPort = bestLocal.port;
|
|
345
|
+
this._remoteAddress = bestRemote.ip;
|
|
346
|
+
this._remotePort = bestRemote.port;
|
|
347
|
+
|
|
348
|
+
console.log(`[NativePeerConnection] Selected candidate pair:`);
|
|
349
|
+
console.log(` Local: ${bestLocal.type} ${this._localAddress}:${this._localPort}`);
|
|
350
|
+
console.log(` Remote: ${bestRemote.type} ${this._remoteAddress}:${this._remotePort}`);
|
|
264
351
|
}
|
|
265
352
|
|
|
266
353
|
/**
|
|
@@ -396,14 +483,24 @@ class NativePeerConnection extends EventEmitter {
|
|
|
396
483
|
return;
|
|
397
484
|
}
|
|
398
485
|
|
|
399
|
-
//
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
486
|
+
// Check if we're using relay candidates - if so, skip tie-breaking
|
|
487
|
+
const usingRelay = this._selectedLocalCandidate?.type === 'relay' ||
|
|
488
|
+
this._selectedRemoteCandidate?.type === 'relay';
|
|
489
|
+
|
|
490
|
+
if (!usingRelay) {
|
|
491
|
+
// Tie-breaking: only connect if our port is higher than remote port
|
|
492
|
+
// This ensures only one peer connects, avoiding the race condition
|
|
493
|
+
// Note: This only works for direct connections (host/srflx)
|
|
494
|
+
if (this._localPort < this._remotePort) {
|
|
495
|
+
console.log(`[NativePeerConnection] Not connecting (local port ${this._localPort} < remote port ${this._remotePort}), waiting for incoming`);
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
404
498
|
}
|
|
405
499
|
|
|
406
500
|
console.log(`[NativePeerConnection] Connecting to ${this._remoteAddress}:${this._remotePort}`);
|
|
501
|
+
if (usingRelay) {
|
|
502
|
+
console.log(`[NativePeerConnection] Using TURN relay connection`);
|
|
503
|
+
}
|
|
407
504
|
|
|
408
505
|
this._socket = new net.Socket();
|
|
409
506
|
|
|
@@ -696,7 +793,12 @@ a=max-message-size:262144
|
|
|
696
793
|
|
|
697
794
|
// Emit each candidate
|
|
698
795
|
for (const candidate of candidates) {
|
|
699
|
-
|
|
796
|
+
// Parse and store the candidate
|
|
797
|
+
const parsed = this._parseIceCandidate(candidate.candidate);
|
|
798
|
+
if (parsed) {
|
|
799
|
+
this._iceCandidates.push(parsed);
|
|
800
|
+
}
|
|
801
|
+
|
|
700
802
|
this.emit('icecandidate', {
|
|
701
803
|
candidate: candidate.candidate,
|
|
702
804
|
sdpMid: candidate.sdpMid,
|
|
@@ -710,8 +812,14 @@ a=max-message-size:262144
|
|
|
710
812
|
|
|
711
813
|
// Fallback: emit only local host candidate
|
|
712
814
|
if (this._localAddress && this._localPort) {
|
|
815
|
+
const candidateStr = `candidate:1 1 tcp 2130706431 ${this._localAddress} ${this._localPort} typ host`;
|
|
816
|
+
const parsed = this._parseIceCandidate(candidateStr);
|
|
817
|
+
if (parsed) {
|
|
818
|
+
this._iceCandidates.push(parsed);
|
|
819
|
+
}
|
|
820
|
+
|
|
713
821
|
const candidate = {
|
|
714
|
-
candidate:
|
|
822
|
+
candidate: candidateStr,
|
|
715
823
|
sdpMid: 'data',
|
|
716
824
|
sdpMLineIndex: 0
|
|
717
825
|
};
|