werift 0.27.0 → 0.28.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 (224) hide show
  1. package/README.md +119 -8
  2. package/lib/common/src/event.d.ts +26 -0
  3. package/lib/common/src/event.js +87 -2
  4. package/lib/common/src/event.js.map +1 -1
  5. package/lib/common/src/network.d.ts +6 -1
  6. package/lib/common/src/network.js +8 -2
  7. package/lib/common/src/network.js.map +1 -1
  8. package/lib/common/src/transport.d.ts +12 -5
  9. package/lib/common/src/transport.js +3 -2
  10. package/lib/common/src/transport.js.map +1 -1
  11. package/lib/dtls/src/alert.d.ts +49 -0
  12. package/lib/dtls/src/alert.js +103 -0
  13. package/lib/dtls/src/alert.js.map +1 -0
  14. package/lib/dtls/src/client.js +15 -1
  15. package/lib/dtls/src/client.js.map +1 -1
  16. package/lib/dtls/src/context/dtls.d.ts +2 -0
  17. package/lib/dtls/src/context/dtls.js +7 -0
  18. package/lib/dtls/src/context/dtls.js.map +1 -1
  19. package/lib/dtls/src/context/transport.d.ts +17 -1
  20. package/lib/dtls/src/context/transport.js +38 -2
  21. package/lib/dtls/src/context/transport.js.map +1 -1
  22. package/lib/dtls/src/flight/client/flight5.js +4 -1
  23. package/lib/dtls/src/flight/client/flight5.js.map +1 -1
  24. package/lib/dtls/src/flight/flight.d.ts +5 -0
  25. package/lib/dtls/src/flight/flight.js +10 -1
  26. package/lib/dtls/src/flight/flight.js.map +1 -1
  27. package/lib/dtls/src/flight/server/flight6.js +4 -1
  28. package/lib/dtls/src/flight/server/flight6.js.map +1 -1
  29. package/lib/dtls/src/index.d.ts +3 -1
  30. package/lib/dtls/src/index.js +10 -1
  31. package/lib/dtls/src/index.js.map +1 -1
  32. package/lib/dtls/src/record/const.d.ts +9 -0
  33. package/lib/dtls/src/record/const.js +9 -1
  34. package/lib/dtls/src/record/const.js.map +1 -1
  35. package/lib/dtls/src/record/receive.d.ts +6 -0
  36. package/lib/dtls/src/record/receive.js +50 -19
  37. package/lib/dtls/src/record/receive.js.map +1 -1
  38. package/lib/dtls/src/server.js +19 -1
  39. package/lib/dtls/src/server.js.map +1 -1
  40. package/lib/dtls/src/socket.d.ts +134 -2
  41. package/lib/dtls/src/socket.js +322 -6
  42. package/lib/dtls/src/socket.js.map +1 -1
  43. package/lib/ice/src/candidateError.d.ts +155 -0
  44. package/lib/ice/src/candidateError.js +246 -0
  45. package/lib/ice/src/candidateError.js.map +1 -0
  46. package/lib/ice/src/candidateGatherer.d.ts +127 -0
  47. package/lib/ice/src/candidateGatherer.js +745 -0
  48. package/lib/ice/src/candidateGatherer.js.map +1 -0
  49. package/lib/ice/src/consent.d.ts +49 -0
  50. package/lib/ice/src/consent.js +258 -0
  51. package/lib/ice/src/consent.js.map +1 -0
  52. package/lib/ice/src/ice.d.ts +223 -71
  53. package/lib/ice/src/ice.js +873 -975
  54. package/lib/ice/src/ice.js.map +1 -1
  55. package/lib/ice/src/iceBase.d.ts +207 -8
  56. package/lib/ice/src/iceBase.js +94 -31
  57. package/lib/ice/src/iceBase.js.map +1 -1
  58. package/lib/ice/src/index.d.ts +1 -0
  59. package/lib/ice/src/index.js +1 -0
  60. package/lib/ice/src/index.js.map +1 -1
  61. package/lib/ice/src/internal/checkMessages.d.ts +41 -0
  62. package/lib/ice/src/internal/checkMessages.js +63 -0
  63. package/lib/ice/src/internal/checkMessages.js.map +1 -0
  64. package/lib/ice/src/internal/defaultRoute.d.ts +20 -0
  65. package/lib/ice/src/internal/defaultRoute.js +134 -0
  66. package/lib/ice/src/internal/defaultRoute.js.map +1 -0
  67. package/lib/ice/src/internal/hostInterfaces.d.ts +17 -0
  68. package/lib/ice/src/internal/hostInterfaces.js +77 -0
  69. package/lib/ice/src/internal/hostInterfaces.js.map +1 -0
  70. package/lib/ice/src/internal/hostPreference.d.ts +24 -0
  71. package/lib/ice/src/internal/hostPreference.js +38 -0
  72. package/lib/ice/src/internal/hostPreference.js.map +1 -0
  73. package/lib/ice/src/internal/resolveHostAddresses.d.ts +56 -8
  74. package/lib/ice/src/internal/resolveHostAddresses.js +127 -24
  75. package/lib/ice/src/internal/resolveHostAddresses.js.map +1 -1
  76. package/lib/ice/src/internal/selectAddresses.d.ts +27 -0
  77. package/lib/ice/src/internal/selectAddresses.js +37 -18
  78. package/lib/ice/src/internal/selectAddresses.js.map +1 -1
  79. package/lib/ice/src/nomination/regularNomination.d.ts +200 -0
  80. package/lib/ice/src/nomination/regularNomination.js +311 -0
  81. package/lib/ice/src/nomination/regularNomination.js.map +1 -0
  82. package/lib/ice/src/nomination/selectionReplacement.d.ts +138 -0
  83. package/lib/ice/src/nomination/selectionReplacement.js +371 -0
  84. package/lib/ice/src/nomination/selectionReplacement.js.map +1 -0
  85. package/lib/ice/src/provisional.d.ts +139 -0
  86. package/lib/ice/src/provisional.js +468 -0
  87. package/lib/ice/src/provisional.js.map +1 -0
  88. package/lib/ice/src/stun/protocol.d.ts +9 -2
  89. package/lib/ice/src/stun/protocol.js +16 -17
  90. package/lib/ice/src/stun/protocol.js.map +1 -1
  91. package/lib/ice/src/stun/tcpProtocol.d.ts +1 -1
  92. package/lib/ice/src/stun/tcpProtocol.js +7 -7
  93. package/lib/ice/src/stun/tcpProtocol.js.map +1 -1
  94. package/lib/ice/src/stun/transaction.js +4 -2
  95. package/lib/ice/src/stun/transaction.js.map +1 -1
  96. package/lib/ice/src/turn/protocol.d.ts +2 -2
  97. package/lib/ice/src/turn/protocol.js +6 -3
  98. package/lib/ice/src/turn/protocol.js.map +1 -1
  99. package/lib/ice/src/types/model.d.ts +16 -5
  100. package/lib/ice/src/types/model.js.map +1 -1
  101. package/lib/ice-server/src/stun/attributes.js +4 -0
  102. package/lib/ice-server/src/stun/attributes.js.map +1 -1
  103. package/lib/ice-server/src/stun/const.d.ts +20 -1
  104. package/lib/ice-server/src/stun/const.js +9 -1
  105. package/lib/ice-server/src/stun/const.js.map +1 -1
  106. package/lib/ice-server/src/stun/message.d.ts +28 -1
  107. package/lib/ice-server/src/stun/message.js +102 -15
  108. package/lib/ice-server/src/stun/message.js.map +1 -1
  109. package/lib/index.mjs +13394 -7732
  110. package/lib/nonstandard/index.mjs +680 -286
  111. package/lib/polyfill/dom.mjs +19605 -14243
  112. package/lib/polyfill/index.mjs +19605 -14243
  113. package/lib/rtp/src/index.d.ts +1 -0
  114. package/lib/rtp/src/index.js +1 -0
  115. package/lib/rtp/src/index.js.map +1 -1
  116. package/lib/rtp/src/rtp/continuity.d.ts +14 -1
  117. package/lib/rtp/src/rtp/continuity.js +24 -4
  118. package/lib/rtp/src/rtp/continuity.js.map +1 -1
  119. package/lib/rtp/src/rtp/timelineRewriter.d.ts +178 -0
  120. package/lib/rtp/src/rtp/timelineRewriter.js +417 -0
  121. package/lib/rtp/src/rtp/timelineRewriter.js.map +1 -0
  122. package/lib/sctp/src/sctp.d.ts +5 -0
  123. package/lib/sctp/src/sctp.js +81 -47
  124. package/lib/sctp/src/sctp.js.map +1 -1
  125. package/lib/webrtc/src/api/compatibilityOptions.d.ts +33 -0
  126. package/lib/webrtc/src/api/compatibilityOptions.js +33 -0
  127. package/lib/webrtc/src/api/compatibilityOptions.js.map +1 -0
  128. package/lib/webrtc/src/api/peerConfig.d.ts +348 -34
  129. package/lib/webrtc/src/api/peerConfig.js +90 -24
  130. package/lib/webrtc/src/api/peerConfig.js.map +1 -1
  131. package/lib/webrtc/src/api/peerConnectionEvents.d.ts +77 -1
  132. package/lib/webrtc/src/api/peerConnectionEvents.js +105 -1
  133. package/lib/webrtc/src/api/peerConnectionEvents.js.map +1 -1
  134. package/lib/webrtc/src/helper.d.ts +8 -0
  135. package/lib/webrtc/src/helper.js +40 -4
  136. package/lib/webrtc/src/helper.js.map +1 -1
  137. package/lib/webrtc/src/media/codecCompatibility.d.ts +87 -1
  138. package/lib/webrtc/src/media/codecCompatibility.js +338 -12
  139. package/lib/webrtc/src/media/codecCompatibility.js.map +1 -1
  140. package/lib/webrtc/src/media/receiver/nack.d.ts +43 -10
  141. package/lib/webrtc/src/media/receiver/nack.js +245 -88
  142. package/lib/webrtc/src/media/receiver/nack.js.map +1 -1
  143. package/lib/webrtc/src/media/router.d.ts +239 -51
  144. package/lib/webrtc/src/media/router.js +664 -237
  145. package/lib/webrtc/src/media/router.js.map +1 -1
  146. package/lib/webrtc/src/media/routing/pendingRoutes.d.ts +26 -0
  147. package/lib/webrtc/src/media/routing/pendingRoutes.js +48 -0
  148. package/lib/webrtc/src/media/routing/pendingRoutes.js.map +1 -0
  149. package/lib/webrtc/src/media/routing/ridRouting.d.ts +37 -0
  150. package/lib/webrtc/src/media/routing/ridRouting.js +74 -0
  151. package/lib/webrtc/src/media/routing/ridRouting.js.map +1 -0
  152. package/lib/webrtc/src/media/routing/routeTable.d.ts +60 -0
  153. package/lib/webrtc/src/media/routing/routeTable.js +95 -0
  154. package/lib/webrtc/src/media/routing/routeTable.js.map +1 -0
  155. package/lib/webrtc/src/media/routing/rtcpRouting.d.ts +34 -0
  156. package/lib/webrtc/src/media/routing/rtcpRouting.js +165 -0
  157. package/lib/webrtc/src/media/routing/rtcpRouting.js.map +1 -0
  158. package/lib/webrtc/src/media/routing/unknownSsrc.d.ts +55 -0
  159. package/lib/webrtc/src/media/routing/unknownSsrc.js +117 -0
  160. package/lib/webrtc/src/media/routing/unknownSsrc.js.map +1 -0
  161. package/lib/webrtc/src/media/rtpReceiver.d.ts +222 -0
  162. package/lib/webrtc/src/media/rtpReceiver.js +668 -28
  163. package/lib/webrtc/src/media/rtpReceiver.js.map +1 -1
  164. package/lib/webrtc/src/media/rtpSender.d.ts +7 -0
  165. package/lib/webrtc/src/media/rtpSender.js +9 -3
  166. package/lib/webrtc/src/media/rtpSender.js.map +1 -1
  167. package/lib/webrtc/src/media/rtpTransceiver.d.ts +18 -0
  168. package/lib/webrtc/src/media/rtpTransceiver.js +64 -2
  169. package/lib/webrtc/src/media/rtpTransceiver.js.map +1 -1
  170. package/lib/webrtc/src/media/sender/senderReportHistory.d.ts +51 -0
  171. package/lib/webrtc/src/media/sender/senderReportHistory.js +111 -0
  172. package/lib/webrtc/src/media/sender/senderReportHistory.js.map +1 -0
  173. package/lib/webrtc/src/media/sender/senderStats.d.ts +6 -5
  174. package/lib/webrtc/src/media/sender/senderStats.js +14 -19
  175. package/lib/webrtc/src/media/sender/senderStats.js.map +1 -1
  176. package/lib/webrtc/src/media/track.d.ts +9 -0
  177. package/lib/webrtc/src/media/track.js +9 -0
  178. package/lib/webrtc/src/media/track.js.map +1 -1
  179. package/lib/webrtc/src/negotiation/remoteMediaApplication.d.ts +23 -6
  180. package/lib/webrtc/src/negotiation/remoteMediaApplication.js +46 -22
  181. package/lib/webrtc/src/negotiation/remoteMediaApplication.js.map +1 -1
  182. package/lib/webrtc/src/negotiation/transceiverPairing.d.ts +1 -0
  183. package/lib/webrtc/src/negotiation/transceiverPairing.js +62 -0
  184. package/lib/webrtc/src/negotiation/transceiverPairing.js.map +1 -0
  185. package/lib/webrtc/src/negotiation/transportActivation.d.ts +8 -1
  186. package/lib/webrtc/src/negotiation/transportActivation.js +21 -0
  187. package/lib/webrtc/src/negotiation/transportActivation.js.map +1 -1
  188. package/lib/webrtc/src/negotiationTransaction.d.ts +35 -4
  189. package/lib/webrtc/src/negotiationTransaction.js +111 -8
  190. package/lib/webrtc/src/negotiationTransaction.js.map +1 -1
  191. package/lib/webrtc/src/peerConnection.d.ts +65 -6
  192. package/lib/webrtc/src/peerConnection.js +283 -28
  193. package/lib/webrtc/src/peerConnection.js.map +1 -1
  194. package/lib/webrtc/src/polyfill/globals.d.ts +2 -1
  195. package/lib/webrtc/src/polyfill/globals.js.map +1 -1
  196. package/lib/webrtc/src/polyfill/install.js +2 -0
  197. package/lib/webrtc/src/polyfill/install.js.map +1 -1
  198. package/lib/webrtc/src/sctpManager.js +4 -0
  199. package/lib/webrtc/src/sctpManager.js.map +1 -1
  200. package/lib/webrtc/src/sdp.d.ts +69 -0
  201. package/lib/webrtc/src/sdp.js +139 -14
  202. package/lib/webrtc/src/sdp.js.map +1 -1
  203. package/lib/webrtc/src/sdpManager.d.ts +85 -4
  204. package/lib/webrtc/src/sdpManager.js +214 -14
  205. package/lib/webrtc/src/sdpManager.js.map +1 -1
  206. package/lib/webrtc/src/secureTransportManager.d.ts +38 -1
  207. package/lib/webrtc/src/secureTransportManager.js +82 -7
  208. package/lib/webrtc/src/secureTransportManager.js.map +1 -1
  209. package/lib/webrtc/src/transceiverManager.d.ts +82 -4
  210. package/lib/webrtc/src/transceiverManager.js +231 -38
  211. package/lib/webrtc/src/transceiverManager.js.map +1 -1
  212. package/lib/webrtc/src/transport/dtls.d.ts +95 -1
  213. package/lib/webrtc/src/transport/dtls.js +324 -28
  214. package/lib/webrtc/src/transport/dtls.js.map +1 -1
  215. package/lib/webrtc/src/transport/ice.d.ts +61 -4
  216. package/lib/webrtc/src/transport/ice.js +180 -11
  217. package/lib/webrtc/src/transport/ice.js.map +1 -1
  218. package/lib/webrtc/src/transport/sctp.d.ts +23 -0
  219. package/lib/webrtc/src/transport/sctp.js +87 -2
  220. package/lib/webrtc/src/transport/sctp.js.map +1 -1
  221. package/lib/webrtc/src/utils.d.ts +33 -0
  222. package/lib/webrtc/src/utils.js +52 -3
  223. package/lib/webrtc/src/utils.js.map +1 -1
  224. package/package.json +2 -1
@@ -0,0 +1,138 @@
1
+ import { type CandidatePair } from "../iceBase";
2
+ import { type Address } from "../imports/common";
3
+ import type { Protocol } from "../types/model";
4
+ import type { NominationOutcome } from "./regularNomination";
5
+ /**
6
+ * Semi-aggressive nomination and renomination: delays before a failed pair
7
+ * that outranks the selection is checked again (`retryOutrankingPair`), one
8
+ * per retry. As libwebrtc keeps pinging a connection until it has heard
9
+ * nothing for 30 s, the retries end about 31 s after the failure.
10
+ */
11
+ export declare const OUTRANKING_RETRY_DELAYS_MS: readonly [1000, 2000, 4000, 8000, 16000];
12
+ /** What the agent supplies to its `SelectionReplacement`. */
13
+ export type SelectionHost = {
14
+ /** The live checklist, highest priority first. */
15
+ checkList: () => readonly CandidatePair[];
16
+ /** The selected pair (`Connection.nominated`). */
17
+ selected: () => CandidatePair | undefined;
18
+ controlling: () => boolean;
19
+ closed: () => boolean;
20
+ remoteGeneration: () => number;
21
+ usesRenomination: () => boolean;
22
+ usesSemiAggressiveNomination: () => boolean;
23
+ /** The controlling agent may move the selection to a higher-priority pair. */
24
+ replacesSelection: () => boolean;
25
+ /** A pair semi-aggressive nomination and renomination may select (not ICE-TCP, checked locally). */
26
+ replacesWith: (pair: CandidatePair) => boolean;
27
+ outranks: (pair: CandidatePair, other: CandidatePair) => boolean;
28
+ bestValidPair: (pairs: CandidatePair[]) => CandidatePair | undefined;
29
+ isTcpPair: (pair: CandidatePair) => boolean;
30
+ /** Start the ordinary check of `pair` (its handle is kept on the pair). */
31
+ check: (pair: CandidatePair) => void;
32
+ /** Apply the outcome of `pair` (`Connection.checkComplete`). */
33
+ complete: (pair: CandidatePair) => void;
34
+ /** Send the nominating check (USE-CANDIDATE, or NOMINATION `value`); never rejects. */
35
+ sendNomination: (pair: CandidatePair, signal: AbortSignal, value?: number) => Promise<NominationOutcome>;
36
+ /** A nomination of `pair` sent in remote `generation` no longer applies. */
37
+ isStaleNomination: (pair: CandidatePair, generation: number) => boolean;
38
+ /** 487: the peer is controlling and wins; the agent becomes controlled. */
39
+ roleConflict: () => void;
40
+ };
41
+ /**
42
+ * Semi-aggressive nomination and renomination (as libwebrtc): how a selection
43
+ * is replaced after the first one.
44
+ *
45
+ * - Controlling: the pairs that outrank the selection go on being checked
46
+ * (`checkOutrankingPairs`), a failed one again after
47
+ * `OUTRANKING_RETRY_DELAYS_MS` (`scheduleRetries`), and the best valid pair
48
+ * is nominated with a separate check when no check of it did
49
+ * (`evaluate`; renomination sends increasing NOMINATION values).
50
+ * - Controlled: between equal nominations the pair the peer sends data on
51
+ * wins (`controlledDataReceived`, `replaces`).
52
+ */
53
+ export declare class SelectionReplacement {
54
+ /**
55
+ * Semi-aggressive nomination or renomination: the separate check that
56
+ * nominates a valid pair which outranks the selection (see
57
+ * `evaluate`). One at a time.
58
+ */
59
+ private inFlight?;
60
+ /** The last NOMINATION value sent (renomination); it only increases. */
61
+ private nominationValue;
62
+ /** `checkOutrankingPairs` is running. */
63
+ private outrankingChecks;
64
+ /**
65
+ * Controlled agent: when each live pair last received application data
66
+ * (an increasing sequence, larger is more recent). Between nominated pairs
67
+ * of equal nomination, the one the peer sends on wins (`replaces`).
68
+ */
69
+ private readonly lastData;
70
+ private dataSequence;
71
+ /**
72
+ * Retries of failed pairs that outrank the selection
73
+ * (`scheduleRetries`): how many ran, and the pending timer.
74
+ */
75
+ private readonly retries;
76
+ /**
77
+ * Semi-aggressive nomination and renomination (controlling): a pair that
78
+ * outranks the selection but failed (a check or nomination that went
79
+ * unanswered, e.g. while the path was briefly down) is checked again after
80
+ * `OUTRANKING_RETRY_DELAYS_MS`, as libwebrtc keeps pinging its
81
+ * connections. Without it the agents stay on the lower path when the
82
+ * peer does not trigger a check on that pair either (e.g. its own check
83
+ * failed too). Each pair waits for one retry at a time.
84
+ */
85
+ scheduleRetries(): void;
86
+ /** `pair` failed, outranks `selected` and may replace it. */
87
+ private retriesOutranking;
88
+ /** A retry of `scheduleRetries` is due: check `pair` again. */
89
+ private retryOutrankingPair;
90
+ /** `pair` succeeded: its retries of `scheduleRetries` are over. */
91
+ forgetRetry(pair: CandidatePair): void;
92
+ /** Stop the retries of `scheduleRetries` (restart, close, role change). */
93
+ clearRetries(): void;
94
+ /**
95
+ * Whether the nominated `pair` replaces the selected pair:
96
+ * - controlled: the peer's larger nomination (the NOMINATION value;
97
+ * USE-CANDIDATE counts as 1, as in libwebrtc), the higher-priority pair
98
+ * between equal ones (RFC 5245 §8.1.1.2: the highest-priority nominated
99
+ * pair is used). A controlling peer nominating once (regular nomination)
100
+ * never causes a switch.
101
+ * - controlling with renomination: the pair renominated last.
102
+ * - controlling, semi-aggressive: a higher-priority pair.
103
+ * - controlling, regular nomination or toward ICE-lite: never.
104
+ */
105
+ replaces(pair: CandidatePair): boolean;
106
+ /**
107
+ * Controlled agent: application data arrived on the live pair of
108
+ * `protocol` and `addr`. Record it (`lastData`) and, when the pair is
109
+ * nominated but not selected, let it replace the selection if the peer
110
+ * now sends on it (`replaces`). ICE-TCP pairs only record:
111
+ * selecting one prunes the other TCP connections.
112
+ */
113
+ controlledDataReceived(protocol: Protocol, addr: Address): void;
114
+ /**
115
+ * Semi-aggressive nomination and renomination: nominate the best valid
116
+ * pair with a separate check when it outranks the selection (or nothing is
117
+ * selected yet) and no check of it nominated it already. With renomination
118
+ * ordinary checks never nominate; with semi-aggressive nomination a pair
119
+ * whose check went without USE-CANDIDATE (sent while controlled) still
120
+ * needs one. One such check at a time; its outcome re-evaluates.
121
+ */
122
+ evaluate(): void;
123
+ private nominateSelection;
124
+ abort(): void;
125
+ /**
126
+ * After the first selection (semi-aggressive nomination, renomination):
127
+ * check the pairs that outrank the selected one, paced like the ordinary
128
+ * schedule. A pair added later that outranks it (a trickled candidate, a
129
+ * late relay allocation) restarts it (`addPair`).
130
+ */
131
+ checkOutrankingPairs(): Promise<void>;
132
+ private readonly host;
133
+ constructor(host: SelectionHost);
134
+ /** A nominating check of `evaluate` is in flight. */
135
+ get nominating(): boolean;
136
+ /** The next NOMINATION value (renomination); it only increases. */
137
+ nextNominationValue(): number;
138
+ }
@@ -0,0 +1,371 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.SelectionReplacement = exports.OUTRANKING_RETRY_DELAYS_MS = void 0;
37
+ const timers = __importStar(require("node:timers/promises"));
38
+ const iceBase_1 = require("../iceBase");
39
+ const common_1 = require("../imports/common");
40
+ const log = (0, common_1.debug)("werift-ice : packages/ice/src/nomination/selectionReplacement.ts : log");
41
+ /**
42
+ * Semi-aggressive nomination and renomination: delays before a failed pair
43
+ * that outranks the selection is checked again (`retryOutrankingPair`), one
44
+ * per retry. As libwebrtc keeps pinging a connection until it has heard
45
+ * nothing for 30 s, the retries end about 31 s after the failure.
46
+ */
47
+ exports.OUTRANKING_RETRY_DELAYS_MS = [
48
+ 1000, 2000, 4000, 8000, 16000,
49
+ ];
50
+ /**
51
+ * Semi-aggressive nomination and renomination (as libwebrtc): how a selection
52
+ * is replaced after the first one.
53
+ *
54
+ * - Controlling: the pairs that outrank the selection go on being checked
55
+ * (`checkOutrankingPairs`), a failed one again after
56
+ * `OUTRANKING_RETRY_DELAYS_MS` (`scheduleRetries`), and the best valid pair
57
+ * is nominated with a separate check when no check of it did
58
+ * (`evaluate`; renomination sends increasing NOMINATION values).
59
+ * - Controlled: between equal nominations the pair the peer sends data on
60
+ * wins (`controlledDataReceived`, `replaces`).
61
+ */
62
+ class SelectionReplacement {
63
+ /**
64
+ * Semi-aggressive nomination and renomination (controlling): a pair that
65
+ * outranks the selection but failed (a check or nomination that went
66
+ * unanswered, e.g. while the path was briefly down) is checked again after
67
+ * `OUTRANKING_RETRY_DELAYS_MS`, as libwebrtc keeps pinging its
68
+ * connections. Without it the agents stay on the lower path when the
69
+ * peer does not trigger a check on that pair either (e.g. its own check
70
+ * failed too). Each pair waits for one retry at a time.
71
+ */
72
+ scheduleRetries() {
73
+ const selected = this.host.selected();
74
+ if (!selected ||
75
+ this.host.closed() ||
76
+ !this.host.controlling() ||
77
+ !this.host.replacesSelection()) {
78
+ return;
79
+ }
80
+ for (const pair of this.host.checkList()) {
81
+ if (!this.retriesOutranking(pair, selected))
82
+ continue;
83
+ let entry = this.retries.get(pair);
84
+ if (!entry) {
85
+ entry = { retries: 0 };
86
+ this.retries.set(pair, entry);
87
+ }
88
+ if (entry.timer !== undefined ||
89
+ entry.retries >= exports.OUTRANKING_RETRY_DELAYS_MS.length) {
90
+ continue;
91
+ }
92
+ const current = entry;
93
+ const remoteGeneration = this.host.remoteGeneration();
94
+ current.timer = setTimeout(() => {
95
+ current.timer = undefined;
96
+ current.retries++;
97
+ this.retryOutrankingPair(pair, remoteGeneration);
98
+ }, exports.OUTRANKING_RETRY_DELAYS_MS[current.retries]);
99
+ }
100
+ }
101
+ /** `pair` failed, outranks `selected` and may replace it. */
102
+ retriesOutranking(pair, selected) {
103
+ return (pair.state === iceBase_1.CandidatePairState.FAILED &&
104
+ this.host.replacesWith(pair) &&
105
+ this.host.outranks(pair, selected));
106
+ }
107
+ /** A retry of `scheduleRetries` is due: check `pair` again. */
108
+ retryOutrankingPair(pair, remoteGeneration) {
109
+ const selected = this.host.selected();
110
+ if (!selected ||
111
+ this.host.closed() ||
112
+ this.host.remoteGeneration() !== remoteGeneration ||
113
+ !this.host.controlling() ||
114
+ !this.host.replacesSelection() ||
115
+ !this.host.checkList().includes(pair) ||
116
+ !this.retriesOutranking(pair, selected)) {
117
+ return;
118
+ }
119
+ log("retry outranking pair", pair.toJSON());
120
+ // One check per due retry (each pair has its own timer), so it starts
121
+ // at once instead of waiting for the paced `checkOutrankingPairs`.
122
+ this.host.check(pair);
123
+ }
124
+ /** `pair` succeeded: its retries of `scheduleRetries` are over. */
125
+ forgetRetry(pair) {
126
+ const entry = this.retries.get(pair);
127
+ if (!entry)
128
+ return;
129
+ if (entry.timer !== undefined)
130
+ clearTimeout(entry.timer);
131
+ this.retries.delete(pair);
132
+ }
133
+ /** Stop the retries of `scheduleRetries` (restart, close, role change). */
134
+ clearRetries() {
135
+ for (const entry of this.retries.values()) {
136
+ if (entry.timer !== undefined)
137
+ clearTimeout(entry.timer);
138
+ }
139
+ this.retries.clear();
140
+ }
141
+ /**
142
+ * Whether the nominated `pair` replaces the selected pair:
143
+ * - controlled: the peer's larger nomination (the NOMINATION value;
144
+ * USE-CANDIDATE counts as 1, as in libwebrtc), the higher-priority pair
145
+ * between equal ones (RFC 5245 §8.1.1.2: the highest-priority nominated
146
+ * pair is used). A controlling peer nominating once (regular nomination)
147
+ * never causes a switch.
148
+ * - controlling with renomination: the pair renominated last.
149
+ * - controlling, semi-aggressive: a higher-priority pair.
150
+ * - controlling, regular nomination or toward ICE-lite: never.
151
+ */
152
+ replaces(pair) {
153
+ const selected = this.host.selected();
154
+ if (!selected || selected === pair)
155
+ return false;
156
+ if (!this.host.controlling()) {
157
+ if (pair.remoteNomination !== selected.remoteNomination) {
158
+ return pair.remoteNomination > selected.remoteNomination;
159
+ }
160
+ // Equal nominations: the pair the peer last sent data on, as libwebrtc
161
+ // (the peer's own selection may move to a lower-priority pair, e.g.
162
+ // when the selected one stopped working).
163
+ const received = this.lastData.get(pair) ?? 0;
164
+ const selectedReceived = this.lastData.get(selected) ?? 0;
165
+ if (received !== selectedReceived)
166
+ return received > selectedReceived;
167
+ return this.host.outranks(pair, selected);
168
+ }
169
+ if (!this.host.replacesWith(pair))
170
+ return false;
171
+ if (this.host.usesRenomination())
172
+ return pair.nomination > selected.nomination;
173
+ if (this.host.usesSemiAggressiveNomination()) {
174
+ return this.host.outranks(pair, selected);
175
+ }
176
+ return false;
177
+ }
178
+ /**
179
+ * Controlled agent: application data arrived on the live pair of
180
+ * `protocol` and `addr`. Record it (`lastData`) and, when the pair is
181
+ * nominated but not selected, let it replace the selection if the peer
182
+ * now sends on it (`replaces`). ICE-TCP pairs only record:
183
+ * selecting one prunes the other TCP connections.
184
+ */
185
+ controlledDataReceived(protocol, addr) {
186
+ const selected = this.host.selected();
187
+ const sameAddress = (pair) => pair.protocol === protocol &&
188
+ pair.remoteAddr[0] === addr[0] &&
189
+ pair.remoteAddr[1] === addr[1];
190
+ const pair = selected && sameAddress(selected)
191
+ ? selected
192
+ : this.host.checkList().find(sameAddress);
193
+ if (!pair)
194
+ return;
195
+ this.lastData.set(pair, ++this.dataSequence);
196
+ if (pair !== selected &&
197
+ pair.nominated &&
198
+ pair.state === iceBase_1.CandidatePairState.SUCCEEDED &&
199
+ !this.host.isTcpPair(pair) &&
200
+ this.replaces(pair)) {
201
+ this.host.complete(pair);
202
+ }
203
+ }
204
+ /**
205
+ * Semi-aggressive nomination and renomination: nominate the best valid
206
+ * pair with a separate check when it outranks the selection (or nothing is
207
+ * selected yet) and no check of it nominated it already. With renomination
208
+ * ordinary checks never nominate; with semi-aggressive nomination a pair
209
+ * whose check went without USE-CANDIDATE (sent while controlled) still
210
+ * needs one. One such check at a time; its outcome re-evaluates.
211
+ */
212
+ evaluate() {
213
+ if (!this.host.replacesSelection() || this.inFlight || this.host.closed()) {
214
+ return;
215
+ }
216
+ const renomination = this.host.usesRenomination();
217
+ const best = this.host.bestValidPair(this.host.checkList().filter((pair) => this.host.replacesWith(pair)));
218
+ const selected = this.host.selected();
219
+ if (!best || best === selected)
220
+ return;
221
+ if (selected && !this.host.outranks(best, selected))
222
+ return;
223
+ // Semi-aggressive: the USE-CANDIDATE check that succeeded selected it.
224
+ if (!renomination && best.nominated)
225
+ return;
226
+ void this.nominateSelection(best, renomination);
227
+ }
228
+ async nominateSelection(pair, renomination) {
229
+ const current = { pair, abort: new AbortController() };
230
+ this.inFlight = current;
231
+ const value = renomination ? ++this.nominationValue : undefined;
232
+ const remoteGeneration = this.host.remoteGeneration();
233
+ log("nominate selection", pair.toJSON(), { nomination: value });
234
+ const outcome = await this.host.sendNomination(pair, current.abort.signal, value);
235
+ // A role change, restart or close replaced it.
236
+ if (this.inFlight !== current)
237
+ return;
238
+ this.inFlight = undefined;
239
+ if (this.host.isStaleNomination(pair, remoteGeneration))
240
+ return;
241
+ if (outcome === "nominated") {
242
+ if (value !== undefined)
243
+ pair.nomination = value;
244
+ pair.nominated = true;
245
+ this.host.complete(pair);
246
+ return;
247
+ }
248
+ if (outcome === "role-conflict") {
249
+ // 487: the peer is controlling and wins; it nominates.
250
+ this.host.roleConflict();
251
+ return;
252
+ }
253
+ // Not taken (an error, or no answer): the pair is not used and the next
254
+ // best valid pair is considered (checkComplete re-evaluates).
255
+ log("selection nomination failed", pair.toJSON(), outcome);
256
+ pair.updateState(iceBase_1.CandidatePairState.FAILED);
257
+ this.host.complete(pair);
258
+ }
259
+ abort() {
260
+ this.inFlight?.abort.abort();
261
+ this.inFlight = undefined;
262
+ }
263
+ /**
264
+ * After the first selection (semi-aggressive nomination, renomination):
265
+ * check the pairs that outrank the selected one, paced like the ordinary
266
+ * schedule. A pair added later that outranks it (a trickled candidate, a
267
+ * late relay allocation) restarts it (`addPair`).
268
+ */
269
+ async checkOutrankingPairs() {
270
+ if (this.outrankingChecks)
271
+ return;
272
+ this.outrankingChecks = true;
273
+ const remoteGeneration = this.host.remoteGeneration();
274
+ try {
275
+ for (;;) {
276
+ const selected = this.host.selected();
277
+ if (!selected ||
278
+ this.host.closed() ||
279
+ this.host.remoteGeneration() !== remoteGeneration ||
280
+ !this.host.controlling() ||
281
+ !this.host.replacesSelection()) {
282
+ return;
283
+ }
284
+ const pair = this.host
285
+ .checkList()
286
+ .find((pair) => (pair.state === iceBase_1.CandidatePairState.WAITING ||
287
+ pair.state === iceBase_1.CandidatePairState.FROZEN) &&
288
+ this.host.replacesWith(pair) &&
289
+ this.host.outranks(pair, selected));
290
+ if (!pair)
291
+ return;
292
+ this.host.check(pair);
293
+ await timers.setTimeout(20);
294
+ }
295
+ }
296
+ finally {
297
+ this.outrankingChecks = false;
298
+ }
299
+ }
300
+ constructor(host) {
301
+ /**
302
+ * Semi-aggressive nomination or renomination: the separate check that
303
+ * nominates a valid pair which outranks the selection (see
304
+ * `evaluate`). One at a time.
305
+ */
306
+ Object.defineProperty(this, "inFlight", {
307
+ enumerable: true,
308
+ configurable: true,
309
+ writable: true,
310
+ value: void 0
311
+ });
312
+ /** The last NOMINATION value sent (renomination); it only increases. */
313
+ Object.defineProperty(this, "nominationValue", {
314
+ enumerable: true,
315
+ configurable: true,
316
+ writable: true,
317
+ value: 0
318
+ });
319
+ /** `checkOutrankingPairs` is running. */
320
+ Object.defineProperty(this, "outrankingChecks", {
321
+ enumerable: true,
322
+ configurable: true,
323
+ writable: true,
324
+ value: false
325
+ });
326
+ /**
327
+ * Controlled agent: when each live pair last received application data
328
+ * (an increasing sequence, larger is more recent). Between nominated pairs
329
+ * of equal nomination, the one the peer sends on wins (`replaces`).
330
+ */
331
+ Object.defineProperty(this, "lastData", {
332
+ enumerable: true,
333
+ configurable: true,
334
+ writable: true,
335
+ value: new WeakMap()
336
+ });
337
+ Object.defineProperty(this, "dataSequence", {
338
+ enumerable: true,
339
+ configurable: true,
340
+ writable: true,
341
+ value: 0
342
+ });
343
+ /**
344
+ * Retries of failed pairs that outrank the selection
345
+ * (`scheduleRetries`): how many ran, and the pending timer.
346
+ */
347
+ Object.defineProperty(this, "retries", {
348
+ enumerable: true,
349
+ configurable: true,
350
+ writable: true,
351
+ value: new Map()
352
+ });
353
+ Object.defineProperty(this, "host", {
354
+ enumerable: true,
355
+ configurable: true,
356
+ writable: true,
357
+ value: void 0
358
+ });
359
+ this.host = host;
360
+ }
361
+ /** A nominating check of `evaluate` is in flight. */
362
+ get nominating() {
363
+ return this.inFlight !== undefined;
364
+ }
365
+ /** The next NOMINATION value (renomination); it only increases. */
366
+ nextNominationValue() {
367
+ return ++this.nominationValue;
368
+ }
369
+ }
370
+ exports.SelectionReplacement = SelectionReplacement;
371
+ //# sourceMappingURL=selectionReplacement.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"selectionReplacement.js","sourceRoot":"","sources":["../../../../../ice/src/nomination/selectionReplacement.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,6DAA+C;AAC/C,wCAAoE;AACpE,8CAAwD;AAIxD,MAAM,GAAG,GAAG,IAAA,cAAK,EACf,wEAAwE,CACzE,CAAC;AAEF;;;;;GAKG;AACU,QAAA,0BAA0B,GAAG;IACxC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK;CACrB,CAAC;AAoCX;;;;;;;;;;;GAWG;AACH,MAAa,oBAAoB;IA6B/B;;;;;;;;OAQG;IACH,eAAe;QACb,MAAM,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC;QACtC,IACE,CAAC,QAAQ;YACT,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE;YAClB,CAAC,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE;YACxB,CAAC,IAAI,CAAC,IAAI,CAAC,iBAAiB,EAAE,EAC9B,CAAC;YACD,OAAO;QACT,CAAC;QACD,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,EAAE,CAAC;YACzC,IAAI,CAAC,IAAI,CAAC,iBAAiB,CAAC,IAAI,EAAE,QAAQ,CAAC;gBAAE,SAAS;YACtD,IAAI,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YACnC,IAAI,CAAC,KAAK,EAAE,CAAC;gBACX,KAAK,GAAG,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC;gBACvB,IAAI,CAAC,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AAE,CACP,CAAC,IAAI,CAAC,KAAK,KAAK,4BAAkB,CAAC,OAAO;oBACxC,IAAI,CAAC,KAAK,KAAK,4BAAkB,CAAC,MAAM,CAAC;oBAC3C,IAAI,CAAC,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC;oBAC5B,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC,CACrC,CAAC;gBACJ,IAAI,CAAC,IAAI;oBAAE,OAAO;gBAClB,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;gBACtB,MAAM,MAAM,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;YAC9B,CAAC;QACH,CAAC;gBAAS,CAAC;YACT,IAAI,CAAC,gBAAgB,GAAG,KAAK,CAAC;QAChC,CAAC;IACH,CAAC;IAID,YAAY,IAAmB;QA5R/B;;;;WAIG;QACK;;;;;WAGN;QACF,wEAAwE;QAChE;;;;mBAAkB,CAAC;WAAC;QAC5B,yCAAyC;QACjC;;;;mBAAmB,KAAK;WAAC;QACjC;;;;WAIG;QACc;;;;mBAAW,IAAI,OAAO,EAAyB;WAAC;QACzD;;;;mBAAe,CAAC;WAAC;QACzB;;;WAGG;QACc;;;;mBAAU,IAAI,GAAG,EAG/B;WAAC;QA+Pa;;;;;WAAoB;QAGnC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;IAED,qDAAqD;IACrD,IAAI,UAAU;QACZ,OAAO,IAAI,CAAC,QAAQ,KAAK,SAAS,CAAC;IACrC,CAAC;IAED,mEAAmE;IACnE,mBAAmB;QACjB,OAAO,EAAE,IAAI,CAAC,eAAe,CAAC;IAChC,CAAC;CACF;AA1SD,oDA0SC","sourcesContent":["import * as timers from \"node:timers/promises\";\nimport { type CandidatePair, CandidatePairState } from \"../iceBase\";\nimport { type Address, debug } from \"../imports/common\";\nimport type { Protocol } from \"../types/model\";\nimport type { NominationOutcome } from \"./regularNomination\";\n\nconst log = debug(\n \"werift-ice : packages/ice/src/nomination/selectionReplacement.ts : log\",\n);\n\n/**\n * Semi-aggressive nomination and renomination: delays before a failed pair\n * that outranks the selection is checked again (`retryOutrankingPair`), one\n * per retry. As libwebrtc keeps pinging a connection until it has heard\n * nothing for 30 s, the retries end about 31 s after the failure.\n */\nexport const OUTRANKING_RETRY_DELAYS_MS = [\n 1000, 2000, 4000, 8000, 16000,\n] as const;\n\n/** What the agent supplies to its `SelectionReplacement`. */\nexport type SelectionHost = {\n /** The live checklist, highest priority first. */\n checkList: () => readonly CandidatePair[];\n /** The selected pair (`Connection.nominated`). */\n selected: () => CandidatePair | undefined;\n controlling: () => boolean;\n closed: () => boolean;\n remoteGeneration: () => number;\n usesRenomination: () => boolean;\n usesSemiAggressiveNomination: () => boolean;\n /** The controlling agent may move the selection to a higher-priority pair. */\n replacesSelection: () => boolean;\n /** A pair semi-aggressive nomination and renomination may select (not ICE-TCP, checked locally). */\n replacesWith: (pair: CandidatePair) => boolean;\n outranks: (pair: CandidatePair, other: CandidatePair) => boolean;\n bestValidPair: (pairs: CandidatePair[]) => CandidatePair | undefined;\n isTcpPair: (pair: CandidatePair) => boolean;\n /** Start the ordinary check of `pair` (its handle is kept on the pair). */\n check: (pair: CandidatePair) => void;\n /** Apply the outcome of `pair` (`Connection.checkComplete`). */\n complete: (pair: CandidatePair) => void;\n /** Send the nominating check (USE-CANDIDATE, or NOMINATION `value`); never rejects. */\n sendNomination: (\n pair: CandidatePair,\n signal: AbortSignal,\n value?: number,\n ) => Promise<NominationOutcome>;\n /** A nomination of `pair` sent in remote `generation` no longer applies. */\n isStaleNomination: (pair: CandidatePair, generation: number) => boolean;\n /** 487: the peer is controlling and wins; the agent becomes controlled. */\n roleConflict: () => void;\n};\n\n/**\n * Semi-aggressive nomination and renomination (as libwebrtc): how a selection\n * is replaced after the first one.\n *\n * - Controlling: the pairs that outrank the selection go on being checked\n * (`checkOutrankingPairs`), a failed one again after\n * `OUTRANKING_RETRY_DELAYS_MS` (`scheduleRetries`), and the best valid pair\n * is nominated with a separate check when no check of it did\n * (`evaluate`; renomination sends increasing NOMINATION values).\n * - Controlled: between equal nominations the pair the peer sends data on\n * wins (`controlledDataReceived`, `replaces`).\n */\nexport class SelectionReplacement {\n /**\n * Semi-aggressive nomination or renomination: the separate check that\n * nominates a valid pair which outranks the selection (see\n * `evaluate`). One at a time.\n */\n private inFlight?: {\n pair: CandidatePair;\n abort: AbortController;\n };\n /** The last NOMINATION value sent (renomination); it only increases. */\n private nominationValue = 0;\n /** `checkOutrankingPairs` is running. */\n private outrankingChecks = false;\n /**\n * Controlled agent: when each live pair last received application data\n * (an increasing sequence, larger is more recent). Between nominated pairs\n * of equal nomination, the one the peer sends on wins (`replaces`).\n */\n private readonly lastData = new WeakMap<CandidatePair, number>();\n private dataSequence = 0;\n /**\n * Retries of failed pairs that outrank the selection\n * (`scheduleRetries`): how many ran, and the pending timer.\n */\n private readonly retries = new Map<\n CandidatePair,\n { retries: number; timer?: ReturnType<typeof setTimeout> }\n >();\n /**\n * Semi-aggressive nomination and renomination (controlling): a pair that\n * outranks the selection but failed (a check or nomination that went\n * unanswered, e.g. while the path was briefly down) is checked again after\n * `OUTRANKING_RETRY_DELAYS_MS`, as libwebrtc keeps pinging its\n * connections. Without it the agents stay on the lower path when the\n * peer does not trigger a check on that pair either (e.g. its own check\n * failed too). Each pair waits for one retry at a time.\n */\n scheduleRetries() {\n const selected = this.host.selected();\n if (\n !selected ||\n this.host.closed() ||\n !this.host.controlling() ||\n !this.host.replacesSelection()\n ) {\n return;\n }\n for (const pair of this.host.checkList()) {\n if (!this.retriesOutranking(pair, selected)) continue;\n let entry = this.retries.get(pair);\n if (!entry) {\n entry = { retries: 0 };\n this.retries.set(pair, entry);\n }\n if (\n entry.timer !== undefined ||\n entry.retries >= OUTRANKING_RETRY_DELAYS_MS.length\n ) {\n continue;\n }\n const current = entry;\n const remoteGeneration = this.host.remoteGeneration();\n current.timer = setTimeout(() => {\n current.timer = undefined;\n current.retries++;\n this.retryOutrankingPair(pair, remoteGeneration);\n }, OUTRANKING_RETRY_DELAYS_MS[current.retries]);\n }\n }\n\n /** `pair` failed, outranks `selected` and may replace it. */\n private retriesOutranking(pair: CandidatePair, selected: CandidatePair) {\n return (\n pair.state === CandidatePairState.FAILED &&\n this.host.replacesWith(pair) &&\n this.host.outranks(pair, selected)\n );\n }\n\n /** A retry of `scheduleRetries` is due: check `pair` again. */\n private retryOutrankingPair(pair: CandidatePair, remoteGeneration: number) {\n const selected = this.host.selected();\n if (\n !selected ||\n this.host.closed() ||\n this.host.remoteGeneration() !== remoteGeneration ||\n !this.host.controlling() ||\n !this.host.replacesSelection() ||\n !this.host.checkList().includes(pair) ||\n !this.retriesOutranking(pair, selected)\n ) {\n return;\n }\n log(\"retry outranking pair\", pair.toJSON());\n // One check per due retry (each pair has its own timer), so it starts\n // at once instead of waiting for the paced `checkOutrankingPairs`.\n this.host.check(pair);\n }\n\n /** `pair` succeeded: its retries of `scheduleRetries` are over. */\n forgetRetry(pair: CandidatePair) {\n const entry = this.retries.get(pair);\n if (!entry) return;\n if (entry.timer !== undefined) clearTimeout(entry.timer);\n this.retries.delete(pair);\n }\n\n /** Stop the retries of `scheduleRetries` (restart, close, role change). */\n clearRetries() {\n for (const entry of this.retries.values()) {\n if (entry.timer !== undefined) clearTimeout(entry.timer);\n }\n this.retries.clear();\n }\n\n /**\n * Whether the nominated `pair` replaces the selected pair:\n * - controlled: the peer's larger nomination (the NOMINATION value;\n * USE-CANDIDATE counts as 1, as in libwebrtc), the higher-priority pair\n * between equal ones (RFC 5245 §8.1.1.2: the highest-priority nominated\n * pair is used). A controlling peer nominating once (regular nomination)\n * never causes a switch.\n * - controlling with renomination: the pair renominated last.\n * - controlling, semi-aggressive: a higher-priority pair.\n * - controlling, regular nomination or toward ICE-lite: never.\n */\n replaces(pair: CandidatePair) {\n const selected = this.host.selected();\n if (!selected || selected === pair) return false;\n if (!this.host.controlling()) {\n if (pair.remoteNomination !== selected.remoteNomination) {\n return pair.remoteNomination > selected.remoteNomination;\n }\n // Equal nominations: the pair the peer last sent data on, as libwebrtc\n // (the peer's own selection may move to a lower-priority pair, e.g.\n // when the selected one stopped working).\n const received = this.lastData.get(pair) ?? 0;\n const selectedReceived = this.lastData.get(selected) ?? 0;\n if (received !== selectedReceived) return received > selectedReceived;\n return this.host.outranks(pair, selected);\n }\n if (!this.host.replacesWith(pair)) return false;\n if (this.host.usesRenomination())\n return pair.nomination > selected.nomination;\n if (this.host.usesSemiAggressiveNomination()) {\n return this.host.outranks(pair, selected);\n }\n return false;\n }\n\n /**\n * Controlled agent: application data arrived on the live pair of\n * `protocol` and `addr`. Record it (`lastData`) and, when the pair is\n * nominated but not selected, let it replace the selection if the peer\n * now sends on it (`replaces`). ICE-TCP pairs only record:\n * selecting one prunes the other TCP connections.\n */\n controlledDataReceived(protocol: Protocol, addr: Address) {\n const selected = this.host.selected();\n const sameAddress = (pair: CandidatePair) =>\n pair.protocol === protocol &&\n pair.remoteAddr[0] === addr[0] &&\n pair.remoteAddr[1] === addr[1];\n const pair =\n selected && sameAddress(selected)\n ? selected\n : this.host.checkList().find(sameAddress);\n if (!pair) return;\n this.lastData.set(pair, ++this.dataSequence);\n if (\n pair !== selected &&\n pair.nominated &&\n pair.state === CandidatePairState.SUCCEEDED &&\n !this.host.isTcpPair(pair) &&\n this.replaces(pair)\n ) {\n this.host.complete(pair);\n }\n }\n\n /**\n * Semi-aggressive nomination and renomination: nominate the best valid\n * pair with a separate check when it outranks the selection (or nothing is\n * selected yet) and no check of it nominated it already. With renomination\n * ordinary checks never nominate; with semi-aggressive nomination a pair\n * whose check went without USE-CANDIDATE (sent while controlled) still\n * needs one. One such check at a time; its outcome re-evaluates.\n */\n evaluate() {\n if (!this.host.replacesSelection() || this.inFlight || this.host.closed()) {\n return;\n }\n const renomination = this.host.usesRenomination();\n const best = this.host.bestValidPair(\n this.host.checkList().filter((pair) => this.host.replacesWith(pair)),\n );\n const selected = this.host.selected();\n if (!best || best === selected) return;\n if (selected && !this.host.outranks(best, selected)) return;\n // Semi-aggressive: the USE-CANDIDATE check that succeeded selected it.\n if (!renomination && best.nominated) return;\n void this.nominateSelection(best, renomination);\n }\n\n private async nominateSelection(pair: CandidatePair, renomination: boolean) {\n const current = { pair, abort: new AbortController() };\n this.inFlight = current;\n const value = renomination ? ++this.nominationValue : undefined;\n const remoteGeneration = this.host.remoteGeneration();\n log(\"nominate selection\", pair.toJSON(), { nomination: value });\n const outcome = await this.host.sendNomination(\n pair,\n current.abort.signal,\n value,\n );\n // A role change, restart or close replaced it.\n if (this.inFlight !== current) return;\n this.inFlight = undefined;\n if (this.host.isStaleNomination(pair, remoteGeneration)) return;\n if (outcome === \"nominated\") {\n if (value !== undefined) pair.nomination = value;\n pair.nominated = true;\n this.host.complete(pair);\n return;\n }\n if (outcome === \"role-conflict\") {\n // 487: the peer is controlling and wins; it nominates.\n this.host.roleConflict();\n return;\n }\n // Not taken (an error, or no answer): the pair is not used and the next\n // best valid pair is considered (checkComplete re-evaluates).\n log(\"selection nomination failed\", pair.toJSON(), outcome);\n pair.updateState(CandidatePairState.FAILED);\n this.host.complete(pair);\n }\n\n abort() {\n this.inFlight?.abort.abort();\n this.inFlight = undefined;\n }\n\n /**\n * After the first selection (semi-aggressive nomination, renomination):\n * check the pairs that outrank the selected one, paced like the ordinary\n * schedule. A pair added later that outranks it (a trickled candidate, a\n * late relay allocation) restarts it (`addPair`).\n */\n async checkOutrankingPairs() {\n if (this.outrankingChecks) return;\n this.outrankingChecks = true;\n const remoteGeneration = this.host.remoteGeneration();\n try {\n for (;;) {\n const selected = this.host.selected();\n if (\n !selected ||\n this.host.closed() ||\n this.host.remoteGeneration() !== remoteGeneration ||\n !this.host.controlling() ||\n !this.host.replacesSelection()\n ) {\n return;\n }\n const pair = this.host\n .checkList()\n .find(\n (pair) =>\n (pair.state === CandidatePairState.WAITING ||\n pair.state === CandidatePairState.FROZEN) &&\n this.host.replacesWith(pair) &&\n this.host.outranks(pair, selected),\n );\n if (!pair) return;\n this.host.check(pair);\n await timers.setTimeout(20);\n }\n } finally {\n this.outrankingChecks = false;\n }\n }\n\n private readonly host: SelectionHost;\n\n constructor(host: SelectionHost) {\n this.host = host;\n }\n\n /** A nominating check of `evaluate` is in flight. */\n get nominating() {\n return this.inFlight !== undefined;\n }\n\n /** The next NOMINATION value (renomination); it only increases. */\n nextNominationValue() {\n return ++this.nominationValue;\n }\n}\n"]}