@yz-social/webrtc 0.1.13 → 0.1.15

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.
Files changed (2) hide show
  1. package/index.js +17 -9
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -26,6 +26,13 @@ export class WebRTC {
26
26
  delete this.dataChannelOursPromises;
27
27
  delete this.dataChannelTheirsPromises;
28
28
  }
29
+ reportStates(label) {
30
+ const pc = this.pc;
31
+ const sctp = pc?.sctp;
32
+ const transport = sctp?.transport;
33
+ const iceTransport = transport?.iceTransport;
34
+ this.flog(label, 'connectionState:', pc?.connectionState, 'signalingState:', pc?.signalingState, 'iceConnectionState:', pc?.iceConnectionState, 'iceGatheringState:', pc?.iceGatheringState, 'transport state:', sctp?.state, transport?.state, iceTransport?.state, iceTransport?.gatheringState);
35
+ }
29
36
 
30
37
  // Number of instances at a time (if previous have been garbage collected), as of 1/27/26:
31
38
  static suggestedInstancesLimit =
@@ -46,9 +53,8 @@ export class WebRTC {
46
53
  if (!pc) return null;
47
54
  const state = pc.connectionState;
48
55
  if (state === 'connected') return this.signalsReadyResolver?.();
49
- if (['new', 'connecting'].includes(state)) return null;
50
- // closed, disconnected, failed: resolve this.closed promise.
51
- this.log('connectionstatechange signaling/connection:', pc.signalingState, state);
56
+ if (['new', 'connecting', 'failed'].includes(state)) return null; // On failed, iceconnectionstatechange will restartIce.
57
+ // closed, disconnected: resolve this.closed promise.
52
58
  this.cleanup();
53
59
  return resolve(pc);
54
60
  };
@@ -75,8 +81,8 @@ export class WebRTC {
75
81
  case 'completed':
76
82
  this._resolveIceCompleted?.(true);
77
83
  break;
78
- case 'failed':
79
- this.pc.restartIce();
84
+ case 'failed': // This doesn't mean the connection is broken. It could just be looking for a better transport.
85
+ this.renegotiate();
80
86
  break;
81
87
  default:
82
88
  ;
@@ -98,7 +104,7 @@ export class WebRTC {
98
104
  };
99
105
  }
100
106
  async renegotiate() { // Trigger negotiationneeded and promise to resolve when completed. Used in testing.
101
- this._resolveIceCompleted?.(false); // clearing old promise, if any.
107
+ if (this._iceConnected) return this._iceConnected;
102
108
  const promise = this.iceConnected;
103
109
  this.pc.restartIce();
104
110
  return promise;
@@ -206,7 +212,8 @@ export class WebRTC {
206
212
  // This is used by a peer that is receiving signals in an out-of-band network request, and waiting for a response. (Compare transferSignals.)
207
213
  return this.responseSerializer = this.responseSerializer.then(async () => {
208
214
  this.log('respond', signals.length, 'signals');
209
- const {promise, resolve} = Promise.withResolvers();
215
+ const {promise, resolve} = Promise.withResolvers(); // FIXME: this breaks Firefox.
216
+ //const {promise, resolve} = {}; // FIXME: tests "pass" on Firefox with this version.
210
217
  this.signalsReadyResolver = resolve;
211
218
  await this.onSignals(signals);
212
219
  if (!this.pendingSignals.length) await promise;
@@ -330,11 +337,12 @@ export class WebRTC {
330
337
  return;
331
338
  }
332
339
  const remote = stats.get(candidatePair.remoteCandidateId);
333
- const {protocol, candidateType, address} = remote;
340
+ const {protocol, candidateType, address, port} = remote;
334
341
  const now = Date.now();
335
342
  const statsElapsed = now - this.connectionStartTime;
343
+ const local = stats.get(candidatePair.localCandidateId);
336
344
  Object.assign(this, {stats, transport, candidatePair, remote, protocol, candidateType, statsTime: now, statsElapsed});
337
- if (doLogging) console.info(this.name, 'connected', protocol, candidateType, address, (statsElapsed/1e3).toFixed(1));
345
+ if (doLogging) console.info(new Date(), this.name, this.pc?.connectionState, protocol, candidateType, `${local.address}:${local.port}=>${address}:${port}`, (statsElapsed/1e3).toFixed(1));
338
346
  }
339
347
 
340
348
  static getPublicIP(stunServer = "stun:stun.l.google.com:19302") { // Promise external/WAN/public IP addresses for this device.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yz-social/webrtc",
3
- "version": "0.1.13",
3
+ "version": "0.1.15",
4
4
  "description": "Streamlined portable webrtc management of p2p and client2server.",
5
5
  "keywords": [
6
6
  "webrtc",