@serfab/cadre-core 1.10.0 → 1.11.0

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 (61) hide show
  1. package/dist/cadre-node.d.ts +0 -27
  2. package/dist/cadre-node.js +2 -94
  3. package/dist/cadre-node.js.map +1 -1
  4. package/dist/control-schema.js +1066 -1066
  5. package/dist/diagnostics/connection-path.d.ts +1 -18
  6. package/dist/diagnostics/connection-path.js +1 -20
  7. package/dist/diagnostics/connection-path.js.map +1 -1
  8. package/dist/identity-key.d.ts +0 -30
  9. package/dist/identity-key.js +7 -40
  10. package/dist/identity-key.js.map +1 -1
  11. package/dist/index.d.ts +3 -2
  12. package/dist/index.js +6 -4
  13. package/dist/index.js.map +1 -1
  14. package/dist/relay-stun.d.ts +24 -0
  15. package/dist/relay-stun.js +51 -0
  16. package/dist/relay-stun.js.map +1 -0
  17. package/dist/seed-bootstrap.js +4 -4
  18. package/dist/strand-membership-writer.js +32 -32
  19. package/package.json +2 -2
  20. package/src/cadre-node.ts +8654 -8753
  21. package/src/control-authorization.ts +117 -117
  22. package/src/control-cohort.ts +128 -128
  23. package/src/control-formation-recorder.ts +353 -353
  24. package/src/control-read-retry.ts +216 -216
  25. package/src/control-schema.ts +1078 -1078
  26. package/src/control-write-retry.ts +567 -567
  27. package/src/diagnostics/connection-path.ts +1 -29
  28. package/src/formation-approval.ts +610 -610
  29. package/src/hibernation-manager.ts +638 -638
  30. package/src/identity-key.ts +7 -62
  31. package/src/index.ts +644 -647
  32. package/src/invitation-bootstrap.ts +40 -40
  33. package/src/joined-strand-store.ts +498 -498
  34. package/src/link-budget.ts +684 -684
  35. package/src/membership-connection-gater.ts +570 -570
  36. package/src/node-local-snapshot.ts +298 -298
  37. package/src/peer-dial.ts +315 -315
  38. package/src/peer-join-backfill.ts +710 -710
  39. package/src/pending-join-runner.ts +735 -735
  40. package/src/relay-reservation.ts +1121 -1121
  41. package/src/relay-server.ts +98 -98
  42. package/src/relay-stun.ts +58 -0
  43. package/src/seed-bootstrap.ts +1448 -1448
  44. package/src/strand-addr-protocol.ts +547 -547
  45. package/src/strand-database.ts +287 -287
  46. package/src/strand-first-sync-gate.ts +391 -391
  47. package/src/strand-formation-deadlines.ts +227 -227
  48. package/src/strand-formation-manager.ts +725 -725
  49. package/src/strand-fret-addrs.ts +145 -145
  50. package/src/strand-instance-manager.ts +1789 -1789
  51. package/src/strand-membership-writer.ts +1974 -1974
  52. package/src/strand-network-state-file.ts +40 -40
  53. package/src/strand-network-state.ts +243 -243
  54. package/src/strand-revocation-enforcer.ts +654 -654
  55. package/src/strand-solicitation.ts +532 -532
  56. package/src/strand-wake-protocol.ts +401 -401
  57. package/src/types.ts +2222 -2222
  58. package/dist/diagnostics/webrtc-turn-tracker.d.ts +0 -50
  59. package/dist/diagnostics/webrtc-turn-tracker.js +0 -196
  60. package/dist/diagnostics/webrtc-turn-tracker.js.map +0 -1
  61. package/src/diagnostics/webrtc-turn-tracker.ts +0 -222
@@ -1,50 +0,0 @@
1
- /**
2
- * webrtc-turn-tracker.ts — observe whether ICE selected a TURN relay candidate
3
- * for each WebRTC session, so a TURN-relayed `/webrtc` connection can be
4
- * reclassified `relayed`/`webrtc-turn` instead of silently counted `direct`.
5
- *
6
- * `@libp2p/webrtc` keeps the underlying `RTCPeerConnection` private and never
7
- * surfaces it on the libp2p `Connection`, so the multiaddr classifier
8
- * (`connection-path.ts`) cannot see the ICE candidate types. This tracker hooks
9
- * `globalThis.RTCPeerConnection` at install time: every connection that reaches
10
- * `connectionState === 'connected'` is inspected via `getStats()`, and the
11
- * relay/not-relay verdict for the selected candidate pair is pushed onto a FIFO
12
- * queue. {@link CadreNode} drains that queue on each `connection:open` to tag the
13
- * matching peer (see {@link consume}).
14
- *
15
- * The correlation between a queued settlement and a `connection:open` is
16
- * timing-based and best-effort — it degrades gracefully (unknown → treated as not
17
- * relayed, a safe default that never produces a false `relayed`). It is inert on
18
- * Node.js / any runtime with no `RTCPeerConnection`.
19
- */
20
- export declare class TurnRelayTracker {
21
- private readonly queue;
22
- private OriginalRTCPeerConnection;
23
- /** One settlement record per connection; guards against repeated `connected` events. */
24
- private readonly observed;
25
- /**
26
- * Wrap `globalThis.RTCPeerConnection` with a subclass that records each
27
- * connection's TURN-relay verdict when it settles. Idempotent. A NOP when no
28
- * `RTCPeerConnection` exists (Node.js / no WebRTC polyfill) — the tracker stays
29
- * inert and {@link consume} always returns `null`.
30
- */
31
- install(): void;
32
- /**
33
- * Pop the most recent settled entry within `windowMs` ms of now, returning its
34
- * relay verdict (`true`/`false`); `null` when the queue has no matching entry.
35
- * Also prunes entries older than the window (they can never match a later
36
- * consume). Synchronous and safe to call from a `connection:open` handler.
37
- */
38
- consume(windowMs: number): boolean | null;
39
- /** Restore the original `RTCPeerConnection` and clear the queue. Idempotent. */
40
- dispose(): void;
41
- /**
42
- * Inspect a settled connection's stats and queue its TURN-relay verdict. Any
43
- * failure (stats API absent, rejected promise, malformed report) is caught and
44
- * recorded as `isRelay: false` — the safe default (unknown → not relayed) keeps
45
- * the connection counted rather than dropping it.
46
- */
47
- private recordSettlement;
48
- /** Drop queued entries settled before `cutoffMs` (the queue is in push order). */
49
- private pruneOlderThan;
50
- }
@@ -1,196 +0,0 @@
1
- /**
2
- * webrtc-turn-tracker.ts — observe whether ICE selected a TURN relay candidate
3
- * for each WebRTC session, so a TURN-relayed `/webrtc` connection can be
4
- * reclassified `relayed`/`webrtc-turn` instead of silently counted `direct`.
5
- *
6
- * `@libp2p/webrtc` keeps the underlying `RTCPeerConnection` private and never
7
- * surfaces it on the libp2p `Connection`, so the multiaddr classifier
8
- * (`connection-path.ts`) cannot see the ICE candidate types. This tracker hooks
9
- * `globalThis.RTCPeerConnection` at install time: every connection that reaches
10
- * `connectionState === 'connected'` is inspected via `getStats()`, and the
11
- * relay/not-relay verdict for the selected candidate pair is pushed onto a FIFO
12
- * queue. {@link CadreNode} drains that queue on each `connection:open` to tag the
13
- * matching peer (see {@link consume}).
14
- *
15
- * The correlation between a queued settlement and a `connection:open` is
16
- * timing-based and best-effort — it degrades gracefully (unknown → treated as not
17
- * relayed, a safe default that never produces a false `relayed`). It is inert on
18
- * Node.js / any runtime with no `RTCPeerConnection`.
19
- */
20
- import debug from 'debug';
21
- const log = debug('sereus:cadre:turn-tracker');
22
- /**
23
- * Hard cap on the FIFO queue. In steady state every settlement is consumed by a
24
- * `connection:open`, so the queue stays tiny; the cap only bounds a pathological
25
- * leak (settlements that never get a matching consume).
26
- */
27
- const MAX_QUEUE = 64;
28
- export class TurnRelayTracker {
29
- constructor() {
30
- this.queue = [];
31
- /** One settlement record per connection; guards against repeated `connected` events. */
32
- this.observed = new WeakSet();
33
- }
34
- /**
35
- * Wrap `globalThis.RTCPeerConnection` with a subclass that records each
36
- * connection's TURN-relay verdict when it settles. Idempotent. A NOP when no
37
- * `RTCPeerConnection` exists (Node.js / no WebRTC polyfill) — the tracker stays
38
- * inert and {@link consume} always returns `null`.
39
- */
40
- install() {
41
- if (this.OriginalRTCPeerConnection) {
42
- return; // already installed
43
- }
44
- const Original = globalThis.RTCPeerConnection;
45
- if (typeof Original !== 'function') {
46
- log('install: no globalThis.RTCPeerConnection — tracker inert (Node.js / no polyfill)');
47
- return;
48
- }
49
- this.OriginalRTCPeerConnection = Original;
50
- // eslint-disable-next-line @typescript-eslint/no-this-alias
51
- const tracker = this;
52
- class TrackedRTCPeerConnection extends Original {
53
- constructor(...args) {
54
- super(...args);
55
- try {
56
- this.addEventListener('connectionstatechange', () => {
57
- try {
58
- if (this.connectionState === 'connected' && !tracker.observed.has(this)) {
59
- tracker.observed.add(this);
60
- void tracker.recordSettlement(this);
61
- }
62
- }
63
- catch (error) {
64
- log('connectionstatechange handler failed: %o', error);
65
- }
66
- });
67
- }
68
- catch (error) {
69
- log('install: failed to attach connectionstatechange listener: %o', error);
70
- }
71
- }
72
- }
73
- globalThis.RTCPeerConnection = TrackedRTCPeerConnection;
74
- log('install: wrapped globalThis.RTCPeerConnection');
75
- }
76
- /**
77
- * Pop the most recent settled entry within `windowMs` ms of now, returning its
78
- * relay verdict (`true`/`false`); `null` when the queue has no matching entry.
79
- * Also prunes entries older than the window (they can never match a later
80
- * consume). Synchronous and safe to call from a `connection:open` handler.
81
- */
82
- consume(windowMs) {
83
- const now = Date.now();
84
- let result = null;
85
- // Walk newest → oldest; the most recent in-window settlement is the best
86
- // correlation for a just-opened connection.
87
- for (let i = this.queue.length - 1; i >= 0; i--) {
88
- if (now - this.queue[i].settledAtMs <= windowMs) {
89
- result = this.queue[i].isRelay;
90
- this.queue.splice(i, 1);
91
- break;
92
- }
93
- }
94
- this.pruneOlderThan(now - windowMs);
95
- return result;
96
- }
97
- /** Restore the original `RTCPeerConnection` and clear the queue. Idempotent. */
98
- dispose() {
99
- if (this.OriginalRTCPeerConnection) {
100
- globalThis.RTCPeerConnection = this.OriginalRTCPeerConnection;
101
- this.OriginalRTCPeerConnection = undefined;
102
- }
103
- this.queue.length = 0;
104
- }
105
- /**
106
- * Inspect a settled connection's stats and queue its TURN-relay verdict. Any
107
- * failure (stats API absent, rejected promise, malformed report) is caught and
108
- * recorded as `isRelay: false` — the safe default (unknown → not relayed) keeps
109
- * the connection counted rather than dropping it.
110
- */
111
- async recordSettlement(pc) {
112
- // Defaults to not-relayed (the safe "unknown" verdict); only the success path
113
- // below overrides it, so a getStats failure leaves it false.
114
- let isRelay = false;
115
- try {
116
- const report = await pc.getStats();
117
- isRelay = selectedCandidatePairIsRelay(statsToMap(report));
118
- }
119
- catch (error) {
120
- log('recordSettlement: getStats failed, keeping not-relayed default: %o', error);
121
- }
122
- this.queue.push({ settledAtMs: Date.now(), isRelay });
123
- if (this.queue.length > MAX_QUEUE) {
124
- this.queue.shift();
125
- }
126
- }
127
- /** Drop queued entries settled before `cutoffMs` (the queue is in push order). */
128
- pruneOlderThan(cutoffMs) {
129
- while (this.queue.length > 0 && this.queue[0].settledAtMs < cutoffMs) {
130
- this.queue.shift();
131
- }
132
- }
133
- }
134
- /** Materialise an `RTCStatsReport` into a plain map keyed by stats id. */
135
- function statsToMap(report) {
136
- const map = new Map();
137
- report.forEach((value, key) => {
138
- map.set(key, value);
139
- });
140
- return map;
141
- }
142
- /**
143
- * Whether the selected ICE candidate pair used a TURN relay candidate on either
144
- * end. The pair is the transport's `selectedCandidatePairId` when present, else
145
- * the nominated succeeded pair, else any succeeded pair.
146
- */
147
- function selectedCandidatePairIsRelay(stats) {
148
- const pair = findSelectedPair(stats);
149
- if (!pair) {
150
- return false;
151
- }
152
- const local = pair.localCandidateId ? stats.get(pair.localCandidateId) : undefined;
153
- const remote = pair.remoteCandidateId ? stats.get(pair.remoteCandidateId) : undefined;
154
- return isRelayCandidate(local) || isRelayCandidate(remote);
155
- }
156
- function isRelayCandidate(stat) {
157
- return stat?.type === 'local-candidate' || stat?.type === 'remote-candidate'
158
- ? stat.candidateType === 'relay'
159
- : false;
160
- }
161
- function findSelectedPair(stats) {
162
- const selectedId = selectedPairIdFromTransport(stats);
163
- if (selectedId) {
164
- const stat = stats.get(selectedId);
165
- if (stat?.type === 'candidate-pair') {
166
- return stat;
167
- }
168
- }
169
- let succeeded;
170
- for (const stat of stats.values()) {
171
- if (stat.type !== 'candidate-pair') {
172
- continue;
173
- }
174
- const pair = stat;
175
- if (pair.state !== 'succeeded') {
176
- continue;
177
- }
178
- if (pair.nominated) {
179
- return pair; // nominated succeeded pair wins outright
180
- }
181
- succeeded ?? (succeeded = pair);
182
- }
183
- return succeeded;
184
- }
185
- function selectedPairIdFromTransport(stats) {
186
- for (const stat of stats.values()) {
187
- if (stat.type === 'transport') {
188
- const id = stat.selectedCandidatePairId;
189
- if (typeof id === 'string') {
190
- return id;
191
- }
192
- }
193
- }
194
- return undefined;
195
- }
196
- //# sourceMappingURL=webrtc-turn-tracker.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"webrtc-turn-tracker.js","sourceRoot":"","sources":["../../src/diagnostics/webrtc-turn-tracker.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;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IAAI,CAAC,CAAC,yCAAyC;QACxD,CAAC;QACD,SAAS,KAAT,SAAS,GAAK,IAAI,EAAC;IACrB,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,SAAS,2BAA2B,CAAC,KAA4B;IAC/D,KAAK,MAAM,IAAI,IAAI,KAAK,CAAC,MAAM,EAAE,EAAE,CAAC;QAClC,IAAI,IAAI,CAAC,IAAI,KAAK,WAAW,EAAE,CAAC;YAC9B,MAAM,EAAE,GAAI,IAA0B,CAAC,uBAAuB,CAAC;YAC/D,IAAI,OAAO,EAAE,KAAK,QAAQ,EAAE,CAAC;gBAC3B,OAAO,EAAE,CAAC;YACZ,CAAC;QACH,CAAC;IACH,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC"}
@@ -1,222 +0,0 @@
1
- /**
2
- * webrtc-turn-tracker.ts — observe whether ICE selected a TURN relay candidate
3
- * for each WebRTC session, so a TURN-relayed `/webrtc` connection can be
4
- * reclassified `relayed`/`webrtc-turn` instead of silently counted `direct`.
5
- *
6
- * `@libp2p/webrtc` keeps the underlying `RTCPeerConnection` private and never
7
- * surfaces it on the libp2p `Connection`, so the multiaddr classifier
8
- * (`connection-path.ts`) cannot see the ICE candidate types. This tracker hooks
9
- * `globalThis.RTCPeerConnection` at install time: every connection that reaches
10
- * `connectionState === 'connected'` is inspected via `getStats()`, and the
11
- * relay/not-relay verdict for the selected candidate pair is pushed onto a FIFO
12
- * queue. {@link CadreNode} drains that queue on each `connection:open` to tag the
13
- * matching peer (see {@link consume}).
14
- *
15
- * The correlation between a queued settlement and a `connection:open` is
16
- * timing-based and best-effort — it degrades gracefully (unknown → treated as not
17
- * relayed, a safe default that never produces a false `relayed`). It is inert on
18
- * Node.js / any runtime with no `RTCPeerConnection`.
19
- */
20
-
21
- import debug from 'debug';
22
-
23
- const log = debug('sereus:cadre:turn-tracker');
24
-
25
- /** A single observed WebRTC settlement: when it connected and whether ICE relayed. */
26
- interface SettlementEntry {
27
- settledAtMs: number;
28
- isRelay: boolean;
29
- }
30
-
31
- /**
32
- * Hard cap on the FIFO queue. In steady state every settlement is consumed by a
33
- * `connection:open`, so the queue stays tiny; the cap only bounds a pathological
34
- * leak (settlements that never get a matching consume).
35
- */
36
- const MAX_QUEUE = 64;
37
-
38
- export class TurnRelayTracker {
39
- private readonly queue: SettlementEntry[] = [];
40
- private OriginalRTCPeerConnection: typeof RTCPeerConnection | undefined;
41
- /** One settlement record per connection; guards against repeated `connected` events. */
42
- private readonly observed = new WeakSet<RTCPeerConnection>();
43
-
44
- /**
45
- * Wrap `globalThis.RTCPeerConnection` with a subclass that records each
46
- * connection's TURN-relay verdict when it settles. Idempotent. A NOP when no
47
- * `RTCPeerConnection` exists (Node.js / no WebRTC polyfill) — the tracker stays
48
- * inert and {@link consume} always returns `null`.
49
- */
50
- install(): void {
51
- if (this.OriginalRTCPeerConnection) {
52
- return; // already installed
53
- }
54
- const Original = globalThis.RTCPeerConnection;
55
- if (typeof Original !== 'function') {
56
- log('install: no globalThis.RTCPeerConnection — tracker inert (Node.js / no polyfill)');
57
- return;
58
- }
59
- this.OriginalRTCPeerConnection = Original;
60
-
61
- // eslint-disable-next-line @typescript-eslint/no-this-alias
62
- const tracker = this;
63
- class TrackedRTCPeerConnection extends Original {
64
- constructor(...args: ConstructorParameters<typeof RTCPeerConnection>) {
65
- super(...args);
66
- try {
67
- this.addEventListener('connectionstatechange', () => {
68
- try {
69
- if (this.connectionState === 'connected' && !tracker.observed.has(this)) {
70
- tracker.observed.add(this);
71
- void tracker.recordSettlement(this);
72
- }
73
- } catch (error) {
74
- log('connectionstatechange handler failed: %o', error);
75
- }
76
- });
77
- } catch (error) {
78
- log('install: failed to attach connectionstatechange listener: %o', error);
79
- }
80
- }
81
- }
82
-
83
- globalThis.RTCPeerConnection = TrackedRTCPeerConnection as typeof RTCPeerConnection;
84
- log('install: wrapped globalThis.RTCPeerConnection');
85
- }
86
-
87
- /**
88
- * Pop the most recent settled entry within `windowMs` ms of now, returning its
89
- * relay verdict (`true`/`false`); `null` when the queue has no matching entry.
90
- * Also prunes entries older than the window (they can never match a later
91
- * consume). Synchronous and safe to call from a `connection:open` handler.
92
- */
93
- consume(windowMs: number): boolean | null {
94
- const now = Date.now();
95
- let result: boolean | null = null;
96
- // Walk newest → oldest; the most recent in-window settlement is the best
97
- // correlation for a just-opened connection.
98
- for (let i = this.queue.length - 1; i >= 0; i--) {
99
- if (now - this.queue[i]!.settledAtMs <= windowMs) {
100
- result = this.queue[i]!.isRelay;
101
- this.queue.splice(i, 1);
102
- break;
103
- }
104
- }
105
- this.pruneOlderThan(now - windowMs);
106
- return result;
107
- }
108
-
109
- /** Restore the original `RTCPeerConnection` and clear the queue. Idempotent. */
110
- dispose(): void {
111
- if (this.OriginalRTCPeerConnection) {
112
- globalThis.RTCPeerConnection = this.OriginalRTCPeerConnection;
113
- this.OriginalRTCPeerConnection = undefined;
114
- }
115
- this.queue.length = 0;
116
- }
117
-
118
- /**
119
- * Inspect a settled connection's stats and queue its TURN-relay verdict. Any
120
- * failure (stats API absent, rejected promise, malformed report) is caught and
121
- * recorded as `isRelay: false` — the safe default (unknown → not relayed) keeps
122
- * the connection counted rather than dropping it.
123
- */
124
- private async recordSettlement(pc: RTCPeerConnection): Promise<void> {
125
- // Defaults to not-relayed (the safe "unknown" verdict); only the success path
126
- // below overrides it, so a getStats failure leaves it false.
127
- let isRelay = false;
128
- try {
129
- const report = await pc.getStats();
130
- isRelay = selectedCandidatePairIsRelay(statsToMap(report));
131
- } catch (error) {
132
- log('recordSettlement: getStats failed, keeping not-relayed default: %o', error);
133
- }
134
- this.queue.push({ settledAtMs: Date.now(), isRelay });
135
- if (this.queue.length > MAX_QUEUE) {
136
- this.queue.shift();
137
- }
138
- }
139
-
140
- /** Drop queued entries settled before `cutoffMs` (the queue is in push order). */
141
- private pruneOlderThan(cutoffMs: number): void {
142
- while (this.queue.length > 0 && this.queue[0]!.settledAtMs < cutoffMs) {
143
- this.queue.shift();
144
- }
145
- }
146
- }
147
-
148
- /** Materialise an `RTCStatsReport` into a plain map keyed by stats id. */
149
- function statsToMap(report: RTCStatsReport): Map<string, RTCStats> {
150
- const map = new Map<string, RTCStats>();
151
- report.forEach((value: RTCStats, key: string) => {
152
- map.set(key, value);
153
- });
154
- return map;
155
- }
156
-
157
- /**
158
- * Whether the selected ICE candidate pair used a TURN relay candidate on either
159
- * end. The pair is the transport's `selectedCandidatePairId` when present, else
160
- * the nominated succeeded pair, else any succeeded pair.
161
- */
162
- function selectedCandidatePairIsRelay(stats: Map<string, RTCStats>): boolean {
163
- const pair = findSelectedPair(stats);
164
- if (!pair) {
165
- return false;
166
- }
167
- const local = pair.localCandidateId ? stats.get(pair.localCandidateId) : undefined;
168
- const remote = pair.remoteCandidateId ? stats.get(pair.remoteCandidateId) : undefined;
169
- return isRelayCandidate(local) || isRelayCandidate(remote);
170
- }
171
-
172
- /**
173
- * Minimal shape of a local/remote ICE candidate stats entry. Declared locally
174
- * rather than relying on a DOM `RTCIceCandidateStats` lib type (absent in some
175
- * TS lib versions); only `candidateType` is read.
176
- */
177
- interface IceCandidateStats extends RTCStats {
178
- candidateType?: string;
179
- }
180
-
181
- function isRelayCandidate(stat: RTCStats | undefined): boolean {
182
- return stat?.type === 'local-candidate' || stat?.type === 'remote-candidate'
183
- ? (stat as IceCandidateStats).candidateType === 'relay'
184
- : false;
185
- }
186
-
187
- function findSelectedPair(stats: Map<string, RTCStats>): RTCIceCandidatePairStats | undefined {
188
- const selectedId = selectedPairIdFromTransport(stats);
189
- if (selectedId) {
190
- const stat = stats.get(selectedId);
191
- if (stat?.type === 'candidate-pair') {
192
- return stat as RTCIceCandidatePairStats;
193
- }
194
- }
195
- let succeeded: RTCIceCandidatePairStats | undefined;
196
- for (const stat of stats.values()) {
197
- if (stat.type !== 'candidate-pair') {
198
- continue;
199
- }
200
- const pair = stat as RTCIceCandidatePairStats;
201
- if (pair.state !== 'succeeded') {
202
- continue;
203
- }
204
- if (pair.nominated) {
205
- return pair; // nominated succeeded pair wins outright
206
- }
207
- succeeded ??= pair;
208
- }
209
- return succeeded;
210
- }
211
-
212
- function selectedPairIdFromTransport(stats: Map<string, RTCStats>): string | undefined {
213
- for (const stat of stats.values()) {
214
- if (stat.type === 'transport') {
215
- const id = (stat as RTCTransportStats).selectedCandidatePairId;
216
- if (typeof id === 'string') {
217
- return id;
218
- }
219
- }
220
- }
221
- return undefined;
222
- }