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,200 @@
1
+ import { type CandidatePair } from "../iceBase";
2
+ import type { Protocol } from "../types/model";
3
+ /** Default of `IceOptions.nominationDelayMs`. */
4
+ export declare const DEFAULT_NOMINATION_DELAY_MS = 300;
5
+ /**
6
+ * USE-CANDIDATE transactions sent for one pair before an unanswered
7
+ * nomination gives up (each one retransmits as a check does).
8
+ */
9
+ export declare const NOMINATION_ATTEMPTS = 3;
10
+ /**
11
+ * Check-list result: an unanswered nomination was abandoned. Unlike
12
+ * ICE_FAILED (which connect() keeps waiting past, as more candidates may
13
+ * still pair), it ends connect() with an error.
14
+ */
15
+ export declare const ICE_NOMINATION_ABANDONED = 3;
16
+ /**
17
+ * How long, from the start of its checks, the live checklist waits for the pair
18
+ * a provisional nomination may have selected (`NominationHint`) to appear.
19
+ * It can appear after end-of-candidates: a peer-reflexive or passive TCP
20
+ * pair forms only when the peer's check arrives. Covers the peer's check
21
+ * pacing and a TCP check's response timeout (`TCP_CHECK_RESPONSE_TIMEOUT_MS`).
22
+ */
23
+ export declare const PREFERRED_PAIR_WAIT_MS = 3000;
24
+ export type NominationDecision = {
25
+ action: "nominate";
26
+ pair: CandidatePair;
27
+ } | {
28
+ action: "wait";
29
+ deadline: number;
30
+ }
31
+ /** The pair the peer may have selected cannot be nominated: nominate nothing else. */
32
+ | {
33
+ action: "abandon";
34
+ pair?: CandidatePair;
35
+ } | {
36
+ action: "none";
37
+ };
38
+ /**
39
+ * RFC 8445 §8.1.1 regular nomination: when the controlling agent stops
40
+ * checking and nominates, shared by the live and the provisional checklist.
41
+ *
42
+ * - With a `preferred` rule (the peer may have selected that path already,
43
+ * see `NominationHint`), only the matching pair may be nominated: as soon
44
+ * as it is valid, waited for while its check runs. When it fails, or is not
45
+ * in the checklist `PREFERRED_PAIR_WAIT_MS` (at least the delay) after the
46
+ * checklist began, even after end-of-candidates or with no other valid
47
+ * pair, the nomination is
48
+ * abandoned (as an unanswered one is):
49
+ * nominating another pair could leave the agents on different paths.
50
+ * - The candidate is the first valid (Succeeded) pair of `pairs` (priority
51
+ * order, highest first), so the choice is deterministic.
52
+ * - Nominate at once when both sides signalled end-of-candidates and no
53
+ * higher-priority pair can still succeed.
54
+ * - Otherwise wait until `quietSince + delayMs`, then nominate anyway: a
55
+ * higher-priority pair still in progress (an unreachable host pair takes
56
+ * seconds to fail) or a peer that never signals end-of-candidates must not
57
+ * hold the selection back longer than that.
58
+ */
59
+ export declare const decideNomination: ({ pairs, remoteCandidatesEnd, localCandidatesEnd, quietSince, delayMs, now, unchecked, preferred, preferredSince, }: {
60
+ pairs: readonly CandidatePair[];
61
+ remoteCandidatesEnd: boolean;
62
+ localCandidatesEnd: boolean;
63
+ quietSince: number | undefined;
64
+ delayMs: number;
65
+ now: number;
66
+ /** Pairs never checked locally, which cannot be waited for. */
67
+ unchecked: (pair: CandidatePair) => boolean;
68
+ preferred: ((pair: CandidatePair) => boolean) | undefined;
69
+ /** When the checklist began waiting for the `preferred` pair. */
70
+ preferredSince: number | undefined;
71
+ }) => NominationDecision;
72
+ /** What a USE-CANDIDATE check came back with. */
73
+ export type NominationOutcome =
74
+ /** Answered with success. */
75
+ "nominated"
76
+ /** Timed out: the request may have reached the peer, which may have selected the pair. */
77
+ | "unanswered"
78
+ /** An error response, or the request never left (e.g. a refused TCP connect). */
79
+ | "rejected"
80
+ /** 487: the peer is controlling too and wins (RFC 8445 §7.2.5.1). */
81
+ | "role-conflict";
82
+ /** What a checklist (live or provisional) supplies to its `RegularNomination`. */
83
+ export type NominationHost = {
84
+ /** The controlling agent nominates in this checklist now (full ICE on both sides, nothing nominated yet, the checklist still current). */
85
+ active: () => boolean;
86
+ /** The checklist's checks began (connect(), startProvisionalChecks()). */
87
+ checking: () => boolean;
88
+ /** The checklist, highest priority first. */
89
+ pairs: () => readonly CandidatePair[];
90
+ remoteCandidatesEnd: () => boolean;
91
+ localCandidatesEnd: () => boolean;
92
+ delayMs: () => number;
93
+ /** Pairs never checked locally (forceTurn), which cannot be waited for. */
94
+ unchecked: (pair: CandidatePair) => boolean;
95
+ outranks: (pair: CandidatePair, other: CandidatePair) => boolean;
96
+ /** Matches the only pair that may be nominated: the peer may have selected it already (see `NominationHint`). */
97
+ preferred: () => ((pair: CandidatePair) => boolean) | undefined;
98
+ /** A result for `pair` still applies: the pair is in the checklist and the agent open. */
99
+ owns: (pair: CandidatePair) => boolean;
100
+ /** Send the USE-CANDIDATE check; never rejects. */
101
+ send: (pair: CandidatePair, signal: AbortSignal) => Promise<NominationOutcome>;
102
+ nominated: (pair: CandidatePair) => void;
103
+ /** The peer refused the pair: fail it, so the next valid pair is nominated. */
104
+ rejected: (pair: CandidatePair) => void;
105
+ roleConflict: (pair: CandidatePair) => void;
106
+ /**
107
+ * No attempt on a pair the peer may have selected was answered, or that
108
+ * pair (the hint) failed or never appeared (`pair` undefined).
109
+ */
110
+ abandoned: (pair?: CandidatePair) => void;
111
+ /** The delay ended (after the re-evaluation it triggers). */
112
+ deadlinePassed?: () => void;
113
+ };
114
+ /**
115
+ * The regular nomination (RFC 8445 §8.1.1) of one checklist: the single
116
+ * state and rule set the live checklist and the provisional (ICE-restart)
117
+ * checklist share. It holds the delay, the USE-CANDIDATE attempt in flight,
118
+ * its repeats and the abandoned state; the checklist supplies the pairs and
119
+ * applies the outcome (`NominationHost`).
120
+ *
121
+ * - Delay: `quietSince` is the first valid pair, moved forward by every pair
122
+ * added later that outranks the best valid pair and is checked locally
123
+ * (a forceTurn-excluded pair cannot replace it and does not count).
124
+ * - One attempt at a time; a result whose attempt is no longer current (a
125
+ * restart, a replacement pranswer, close or a role change replaced it) is
126
+ * ignored and its transaction aborted.
127
+ * - An unanswered attempt may have been accepted: it is repeated on the same
128
+ * pair (never another), also when a repeat is answered with an error. When
129
+ * all `NOMINATION_ATTEMPTS` go unanswered nothing else is nominated.
130
+ * - An error answering the first attempt: only that pair fails.
131
+ */
132
+ export declare class RegularNomination {
133
+ private quietSince?;
134
+ /** The checklist began waiting for the preferred pair (see `decideNomination`). */
135
+ private preferredSince?;
136
+ private timer?;
137
+ private attempt?;
138
+ /** Every attempt went unanswered: no other pair is nominated until reset. */
139
+ abandoned: boolean;
140
+ /**
141
+ * The pair a USE-CANDIDATE was sent to and the peer did not refuse: the
142
+ * peer may have selected it (the provisional checklist hands it to the
143
+ * live one at the final answer, see `NominationHint`).
144
+ */
145
+ maybeSelected?: CandidatePair;
146
+ private readonly host;
147
+ constructor(host: NominationHost);
148
+ /** A USE-CANDIDATE transaction is in flight. */
149
+ get inFlight(): boolean;
150
+ /** Waiting for the delay to end. */
151
+ get waiting(): boolean;
152
+ /** `pair` joined the checklist; `best` was its best valid pair before. */
153
+ pairAdded(pair: CandidatePair, best: CandidatePair | undefined): void;
154
+ /**
155
+ * Nominate now, or arm the timer that re-evaluates at the deadline. Called
156
+ * whenever the answer may change: a check finished, a pair was added,
157
+ * end-of-candidates, the role changed, the timer fired.
158
+ */
159
+ evaluate(): void;
160
+ /** Stop waiting (a pair was nominated, or nomination is not possible now). */
161
+ stop(): void;
162
+ /** The role changed: the attempt in flight was sent as the controlling agent and changes nothing after it. */
163
+ invalidate(): void;
164
+ /** The checklist starts over (restart, replacement pranswer, renomination, close). */
165
+ reset(): void;
166
+ /**
167
+ * @param maybeAccepted an earlier attempt for `pair` went unanswered: the
168
+ * peer may have selected the pair already, so no later outcome lets the
169
+ * nomination move to another pair.
170
+ */
171
+ private nominate;
172
+ }
173
+ /**
174
+ * The pair the controlling agent may have nominated in the provisional
175
+ * checklist when a final answer replaced it with the live one. The peer
176
+ * may have selected that pair already: a USE-CANDIDATE retransmission can
177
+ * reach its live checklist (same credentials) after it committed the
178
+ * answer, and a peer that keeps the ICE session across pranswer -> answer
179
+ * (JSEP) keeps its provisional selection. The live checklist checks from
180
+ * scratch (`[2.7-T4a]`) but nominates only the pair with the same 5-tuple;
181
+ * when that pair fails or never appears it nominates nothing else and the
182
+ * agent fails (see `decideNomination`). It only applies while the live
183
+ * checklist uses the provisional credentials and the agent keeps the pair's
184
+ * local socket (`protocol`); otherwise the usual rules choose the pair.
185
+ */
186
+ export type NominationHint = {
187
+ localUsername: string;
188
+ remoteUsername: string;
189
+ remotePassword: string;
190
+ key: string;
191
+ /**
192
+ * The local socket (or TURN allocation) of the pair. The hint holds only
193
+ * while the agent keeps it: a commit that closes it (an ICE restart
194
+ * allocates TURN afresh, gather options that exclude the socket) ends the
195
+ * path for both agents, so no selection of the peer is left to match.
196
+ */
197
+ protocol: Protocol;
198
+ };
199
+ /** Local foundation and the 5-tuple: the same path in another checklist. */
200
+ export declare const pairKey: (pair: CandidatePair) => string;
@@ -0,0 +1,311 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.pairKey = exports.RegularNomination = exports.decideNomination = exports.PREFERRED_PAIR_WAIT_MS = exports.ICE_NOMINATION_ABANDONED = exports.NOMINATION_ATTEMPTS = exports.DEFAULT_NOMINATION_DELAY_MS = void 0;
4
+ const iceBase_1 = require("../iceBase");
5
+ const common_1 = require("../imports/common");
6
+ const log = (0, common_1.debug)("werift-ice : packages/ice/src/nomination/regularNomination.ts : log");
7
+ /** Default of `IceOptions.nominationDelayMs`. */
8
+ exports.DEFAULT_NOMINATION_DELAY_MS = 300;
9
+ /**
10
+ * USE-CANDIDATE transactions sent for one pair before an unanswered
11
+ * nomination gives up (each one retransmits as a check does).
12
+ */
13
+ exports.NOMINATION_ATTEMPTS = 3;
14
+ /**
15
+ * Check-list result: an unanswered nomination was abandoned. Unlike
16
+ * ICE_FAILED (which connect() keeps waiting past, as more candidates may
17
+ * still pair), it ends connect() with an error.
18
+ */
19
+ exports.ICE_NOMINATION_ABANDONED = 3;
20
+ /**
21
+ * How long, from the start of its checks, the live checklist waits for the pair
22
+ * a provisional nomination may have selected (`NominationHint`) to appear.
23
+ * It can appear after end-of-candidates: a peer-reflexive or passive TCP
24
+ * pair forms only when the peer's check arrives. Covers the peer's check
25
+ * pacing and a TCP check's response timeout (`TCP_CHECK_RESPONSE_TIMEOUT_MS`).
26
+ */
27
+ exports.PREFERRED_PAIR_WAIT_MS = 3000;
28
+ /**
29
+ * RFC 8445 §8.1.1 regular nomination: when the controlling agent stops
30
+ * checking and nominates, shared by the live and the provisional checklist.
31
+ *
32
+ * - With a `preferred` rule (the peer may have selected that path already,
33
+ * see `NominationHint`), only the matching pair may be nominated: as soon
34
+ * as it is valid, waited for while its check runs. When it fails, or is not
35
+ * in the checklist `PREFERRED_PAIR_WAIT_MS` (at least the delay) after the
36
+ * checklist began, even after end-of-candidates or with no other valid
37
+ * pair, the nomination is
38
+ * abandoned (as an unanswered one is):
39
+ * nominating another pair could leave the agents on different paths.
40
+ * - The candidate is the first valid (Succeeded) pair of `pairs` (priority
41
+ * order, highest first), so the choice is deterministic.
42
+ * - Nominate at once when both sides signalled end-of-candidates and no
43
+ * higher-priority pair can still succeed.
44
+ * - Otherwise wait until `quietSince + delayMs`, then nominate anyway: a
45
+ * higher-priority pair still in progress (an unreachable host pair takes
46
+ * seconds to fail) or a peer that never signals end-of-candidates must not
47
+ * hold the selection back longer than that.
48
+ */
49
+ const decideNomination = ({ pairs, remoteCandidatesEnd, localCandidatesEnd, quietSince, delayMs, now, unchecked, preferred, preferredSince, }) => {
50
+ if (preferred) {
51
+ const hinted = pairs.find(preferred);
52
+ if (hinted?.state === iceBase_1.CandidatePairState.SUCCEEDED) {
53
+ return { action: "nominate", pair: hinted };
54
+ }
55
+ if (hinted) {
56
+ if (hinted.state === iceBase_1.CandidatePairState.FAILED || unchecked(hinted)) {
57
+ return { action: "abandon", pair: hinted };
58
+ }
59
+ // Its check finishing re-evaluates the nomination.
60
+ return { action: "none" };
61
+ }
62
+ // Not (yet) in the checklist. End-of-candidates does not settle it, nor
63
+ // does an empty checklist: a peer-reflexive pair (or a passive TCP one) is
64
+ // only formed when the peer's check arrives, so wait for it a bounded time
65
+ // from when the checklist began.
66
+ const deadline = (preferredSince ?? now) + Math.max(delayMs, exports.PREFERRED_PAIR_WAIT_MS);
67
+ if (now >= deadline)
68
+ return { action: "abandon" };
69
+ return { action: "wait", deadline };
70
+ }
71
+ const index = pairs.findIndex((pair) => pair.state === iceBase_1.CandidatePairState.SUCCEEDED);
72
+ if (index === -1)
73
+ return { action: "none" };
74
+ const pair = pairs[index];
75
+ const pendingAbove = pairs
76
+ .slice(0, index)
77
+ .some((above) => above.state !== iceBase_1.CandidatePairState.FAILED && !unchecked(above));
78
+ if (remoteCandidatesEnd && localCandidatesEnd && !pendingAbove) {
79
+ return { action: "nominate", pair };
80
+ }
81
+ const deadline = (quietSince ?? now) + delayMs;
82
+ if (now >= deadline)
83
+ return { action: "nominate", pair };
84
+ return { action: "wait", deadline };
85
+ };
86
+ exports.decideNomination = decideNomination;
87
+ /**
88
+ * The regular nomination (RFC 8445 §8.1.1) of one checklist: the single
89
+ * state and rule set the live checklist and the provisional (ICE-restart)
90
+ * checklist share. It holds the delay, the USE-CANDIDATE attempt in flight,
91
+ * its repeats and the abandoned state; the checklist supplies the pairs and
92
+ * applies the outcome (`NominationHost`).
93
+ *
94
+ * - Delay: `quietSince` is the first valid pair, moved forward by every pair
95
+ * added later that outranks the best valid pair and is checked locally
96
+ * (a forceTurn-excluded pair cannot replace it and does not count).
97
+ * - One attempt at a time; a result whose attempt is no longer current (a
98
+ * restart, a replacement pranswer, close or a role change replaced it) is
99
+ * ignored and its transaction aborted.
100
+ * - An unanswered attempt may have been accepted: it is repeated on the same
101
+ * pair (never another), also when a repeat is answered with an error. When
102
+ * all `NOMINATION_ATTEMPTS` go unanswered nothing else is nominated.
103
+ * - An error answering the first attempt: only that pair fails.
104
+ */
105
+ class RegularNomination {
106
+ constructor(host) {
107
+ Object.defineProperty(this, "quietSince", {
108
+ enumerable: true,
109
+ configurable: true,
110
+ writable: true,
111
+ value: void 0
112
+ });
113
+ /** The checklist began waiting for the preferred pair (see `decideNomination`). */
114
+ Object.defineProperty(this, "preferredSince", {
115
+ enumerable: true,
116
+ configurable: true,
117
+ writable: true,
118
+ value: void 0
119
+ });
120
+ Object.defineProperty(this, "timer", {
121
+ enumerable: true,
122
+ configurable: true,
123
+ writable: true,
124
+ value: void 0
125
+ });
126
+ Object.defineProperty(this, "attempt", {
127
+ enumerable: true,
128
+ configurable: true,
129
+ writable: true,
130
+ value: void 0
131
+ });
132
+ /** Every attempt went unanswered: no other pair is nominated until reset. */
133
+ Object.defineProperty(this, "abandoned", {
134
+ enumerable: true,
135
+ configurable: true,
136
+ writable: true,
137
+ value: false
138
+ });
139
+ /**
140
+ * The pair a USE-CANDIDATE was sent to and the peer did not refuse: the
141
+ * peer may have selected it (the provisional checklist hands it to the
142
+ * live one at the final answer, see `NominationHint`).
143
+ */
144
+ Object.defineProperty(this, "maybeSelected", {
145
+ enumerable: true,
146
+ configurable: true,
147
+ writable: true,
148
+ value: void 0
149
+ });
150
+ Object.defineProperty(this, "host", {
151
+ enumerable: true,
152
+ configurable: true,
153
+ writable: true,
154
+ value: void 0
155
+ });
156
+ this.host = host;
157
+ }
158
+ /** A USE-CANDIDATE transaction is in flight. */
159
+ get inFlight() {
160
+ return this.attempt !== undefined;
161
+ }
162
+ /** Waiting for the delay to end. */
163
+ get waiting() {
164
+ return this.timer !== undefined;
165
+ }
166
+ /** `pair` joined the checklist; `best` was its best valid pair before. */
167
+ pairAdded(pair, best) {
168
+ // A pair that may replace the best valid pair restarts the delay; a
169
+ // lower-priority one, or one the agent never checks, cannot change the
170
+ // choice and does not.
171
+ if (best && !this.host.unchecked(pair) && this.host.outranks(pair, best)) {
172
+ this.quietSince = Date.now();
173
+ this.evaluate();
174
+ }
175
+ }
176
+ /**
177
+ * Nominate now, or arm the timer that re-evaluates at the deadline. Called
178
+ * whenever the answer may change: a check finished, a pair was added,
179
+ * end-of-candidates, the role changed, the timer fired.
180
+ */
181
+ evaluate() {
182
+ if (!this.host.active() || this.attempt || this.abandoned) {
183
+ this.stop();
184
+ return;
185
+ }
186
+ const pairs = this.host.pairs();
187
+ const now = Date.now();
188
+ if (this.quietSince === undefined &&
189
+ pairs.some((pair) => pair.state === iceBase_1.CandidatePairState.SUCCEEDED)) {
190
+ this.quietSince = now;
191
+ }
192
+ const preferred = this.host.preferred();
193
+ if (preferred &&
194
+ this.preferredSince === undefined &&
195
+ this.host.checking()) {
196
+ this.preferredSince = now;
197
+ }
198
+ const decision = (0, exports.decideNomination)({
199
+ pairs,
200
+ remoteCandidatesEnd: this.host.remoteCandidatesEnd(),
201
+ localCandidatesEnd: this.host.localCandidatesEnd(),
202
+ quietSince: this.quietSince,
203
+ delayMs: this.host.delayMs(),
204
+ now,
205
+ unchecked: this.host.unchecked,
206
+ preferred,
207
+ preferredSince: this.preferredSince,
208
+ });
209
+ this.stop();
210
+ if (decision.action === "wait") {
211
+ this.timer = setTimeout(() => {
212
+ this.timer = undefined;
213
+ this.evaluate();
214
+ this.host.deadlinePassed?.();
215
+ }, Math.max(0, decision.deadline - now));
216
+ }
217
+ else if (decision.action === "nominate") {
218
+ this.nominate(decision.pair, 1, false);
219
+ }
220
+ else if (decision.action === "abandon") {
221
+ log("preferred pair cannot be nominated, giving up");
222
+ this.abandoned = true;
223
+ this.host.abandoned(decision.pair);
224
+ }
225
+ }
226
+ /** Stop waiting (a pair was nominated, or nomination is not possible now). */
227
+ stop() {
228
+ if (this.timer !== undefined) {
229
+ clearTimeout(this.timer);
230
+ this.timer = undefined;
231
+ }
232
+ }
233
+ /** The role changed: the attempt in flight was sent as the controlling agent and changes nothing after it. */
234
+ invalidate() {
235
+ this.attempt?.abort.abort();
236
+ this.attempt = undefined;
237
+ this.abandoned = false;
238
+ }
239
+ /** The checklist starts over (restart, replacement pranswer, renomination, close). */
240
+ reset() {
241
+ this.invalidate();
242
+ this.stop();
243
+ this.quietSince = undefined;
244
+ this.preferredSince = undefined;
245
+ this.maybeSelected = undefined;
246
+ }
247
+ /**
248
+ * @param maybeAccepted an earlier attempt for `pair` went unanswered: the
249
+ * peer may have selected the pair already, so no later outcome lets the
250
+ * nomination move to another pair.
251
+ */
252
+ nominate(pair, attempt, maybeAccepted) {
253
+ log("nominate", pair.toJSON(), { attempt, maybeAccepted });
254
+ const current = { pair, abort: new AbortController() };
255
+ this.attempt = current;
256
+ this.maybeSelected = pair;
257
+ void this.host.send(pair, current.abort.signal).then((outcome) => {
258
+ if (this.attempt !== current)
259
+ return;
260
+ this.attempt = undefined;
261
+ if (!this.host.owns(pair)) {
262
+ // The pair left the checklist (another remote generation dropped).
263
+ if (this.maybeSelected === pair)
264
+ this.maybeSelected = undefined;
265
+ this.evaluate();
266
+ return;
267
+ }
268
+ if (outcome === "nominated") {
269
+ this.stop();
270
+ this.host.nominated(pair);
271
+ return;
272
+ }
273
+ if (outcome === "role-conflict") {
274
+ // The peer did not accept it as controlled; it nominates now.
275
+ log("nomination role conflict", pair.toJSON());
276
+ if (!maybeAccepted)
277
+ this.maybeSelected = undefined;
278
+ this.host.roleConflict(pair);
279
+ return;
280
+ }
281
+ if (outcome === "unanswered" || maybeAccepted) {
282
+ if (attempt < exports.NOMINATION_ATTEMPTS) {
283
+ log("nomination unanswered, repeating", pair.toJSON(), { attempt });
284
+ this.nominate(pair, attempt + 1, true);
285
+ return;
286
+ }
287
+ log("nomination unanswered, giving up", pair.toJSON());
288
+ this.abandoned = true;
289
+ this.stop();
290
+ this.host.abandoned(pair);
291
+ return;
292
+ }
293
+ log("nomination rejected", pair.toJSON());
294
+ this.maybeSelected = undefined;
295
+ this.host.rejected(pair);
296
+ });
297
+ }
298
+ }
299
+ exports.RegularNomination = RegularNomination;
300
+ /** Local foundation and the 5-tuple: the same path in another checklist. */
301
+ const pairKey = (pair) => [
302
+ pair.localCandidate.foundation,
303
+ pair.localCandidate.transport.toLowerCase(),
304
+ pair.localCandidate.host,
305
+ pair.localCandidate.port,
306
+ pair.remoteCandidate.transport.toLowerCase(),
307
+ pair.remoteCandidate.host,
308
+ pair.remoteCandidate.port,
309
+ ].join(" ");
310
+ exports.pairKey = pairKey;
311
+ //# sourceMappingURL=regularNomination.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"regularNomination.js","sourceRoot":"","sources":["../../../../../ice/src/nomination/regularNomination.ts"],"names":[],"mappings":";;;AAAA,wCAAoE;AACpE,8CAA0C;AAG1C,MAAM,GAAG,GAAG,IAAA,cAAK,EACf,qEAAqE,CACtE,CAAC;AAEF,iDAAiD;AACpC,QAAA,2BAA2B,GAAG,GAAG,CAAC;AAC/C;;;GAGG;AACU,QAAA,mBAAmB,GAAG,CAAC,CAAC;AACrC;;;;GAIG;AACU,QAAA,wBAAwB,GAAG,CAAC,CAAC;AAC1C;;;;;;GAMG;AACU,QAAA,sBAAsB,GAAG,IAAI,CAAC;AAS3C;;;;;;;;;;;;;;;;;;;;GAoBG;AACI,MAAM,gBAAgB,GAAG,CAAC,EAC/B,KAAK,EACL,mBAAmB,EACnB,kBAAk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IACE,IAAI,CAAC,cAAc,CAAC,UAAU;IAC9B,IAAI,CAAC,cAAc,CAAC,SAAS,CAAC,WAAW,EAAE;IAC3C,IAAI,CAAC,cAAc,CAAC,IAAI;IACxB,IAAI,CAAC,cAAc,CAAC,IAAI;IACxB,IAAI,CAAC,eAAe,CAAC,SAAS,CAAC,WAAW,EAAE;IAC5C,IAAI,CAAC,eAAe,CAAC,IAAI;IACzB,IAAI,CAAC,eAAe,CAAC,IAAI;CAC1B,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AATD,QAAA,OAAO,WASN","sourcesContent":["import { type CandidatePair, CandidatePairState } from \"../iceBase\";\nimport { debug } from \"../imports/common\";\nimport type { Protocol } from \"../types/model\";\n\nconst log = debug(\n \"werift-ice : packages/ice/src/nomination/regularNomination.ts : log\",\n);\n\n/** Default of `IceOptions.nominationDelayMs`. */\nexport const DEFAULT_NOMINATION_DELAY_MS = 300;\n/**\n * USE-CANDIDATE transactions sent for one pair before an unanswered\n * nomination gives up (each one retransmits as a check does).\n */\nexport const NOMINATION_ATTEMPTS = 3;\n/**\n * Check-list result: an unanswered nomination was abandoned. Unlike\n * ICE_FAILED (which connect() keeps waiting past, as more candidates may\n * still pair), it ends connect() with an error.\n */\nexport const ICE_NOMINATION_ABANDONED = 3;\n/**\n * How long, from the start of its checks, the live checklist waits for the pair\n * a provisional nomination may have selected (`NominationHint`) to appear.\n * It can appear after end-of-candidates: a peer-reflexive or passive TCP\n * pair forms only when the peer's check arrives. Covers the peer's check\n * pacing and a TCP check's response timeout (`TCP_CHECK_RESPONSE_TIMEOUT_MS`).\n */\nexport const PREFERRED_PAIR_WAIT_MS = 3000;\n\nexport type NominationDecision =\n | { action: \"nominate\"; pair: CandidatePair }\n | { action: \"wait\"; deadline: number }\n /** The pair the peer may have selected cannot be nominated: nominate nothing else. */\n | { action: \"abandon\"; pair?: CandidatePair }\n | { action: \"none\" };\n\n/**\n * RFC 8445 §8.1.1 regular nomination: when the controlling agent stops\n * checking and nominates, shared by the live and the provisional checklist.\n *\n * - With a `preferred` rule (the peer may have selected that path already,\n * see `NominationHint`), only the matching pair may be nominated: as soon\n * as it is valid, waited for while its check runs. When it fails, or is not\n * in the checklist `PREFERRED_PAIR_WAIT_MS` (at least the delay) after the\n * checklist began, even after end-of-candidates or with no other valid\n * pair, the nomination is\n * abandoned (as an unanswered one is):\n * nominating another pair could leave the agents on different paths.\n * - The candidate is the first valid (Succeeded) pair of `pairs` (priority\n * order, highest first), so the choice is deterministic.\n * - Nominate at once when both sides signalled end-of-candidates and no\n * higher-priority pair can still succeed.\n * - Otherwise wait until `quietSince + delayMs`, then nominate anyway: a\n * higher-priority pair still in progress (an unreachable host pair takes\n * seconds to fail) or a peer that never signals end-of-candidates must not\n * hold the selection back longer than that.\n */\nexport const decideNomination = ({\n pairs,\n remoteCandidatesEnd,\n localCandidatesEnd,\n quietSince,\n delayMs,\n now,\n unchecked,\n preferred,\n preferredSince,\n}: {\n pairs: readonly CandidatePair[];\n remoteCandidatesEnd: boolean;\n localCandidatesEnd: boolean;\n quietSince: number | undefined;\n delayMs: number;\n now: number;\n /** Pairs never checked locally, which cannot be waited for. */\n unchecked: (pair: CandidatePair) => boolean;\n preferred: ((pair: CandidatePair) => boolean) | undefined;\n /** When the checklist began waiting for the `preferred` pair. */\n preferredSince: number | undefined;\n}): NominationDecision => {\n if (preferred) {\n const hinted = pairs.find(preferred);\n if (hinted?.state === CandidatePairState.SUCCEEDED) {\n return { action: \"nominate\", pair: hinted };\n }\n if (hinted) {\n if (hinted.state === CandidatePairState.FAILED || unchecked(hinted)) {\n return { action: \"abandon\", pair: hinted };\n }\n // Its check finishing re-evaluates the nomination.\n return { action: \"none\" };\n }\n // Not (yet) in the checklist. End-of-candidates does not settle it, nor\n // does an empty checklist: a peer-reflexive pair (or a passive TCP one) is\n // only formed when the peer's check arrives, so wait for it a bounded time\n // from when the checklist began.\n const deadline =\n (preferredSince ?? now) + Math.max(delayMs, PREFERRED_PAIR_WAIT_MS);\n if (now >= deadline) return { action: \"abandon\" };\n return { action: \"wait\", deadline };\n }\n const index = pairs.findIndex(\n (pair) => pair.state === CandidatePairState.SUCCEEDED,\n );\n if (index === -1) return { action: \"none\" };\n const pair = pairs[index];\n const pendingAbove = pairs\n .slice(0, index)\n .some(\n (above) => above.state !== CandidatePairState.FAILED && !unchecked(above),\n );\n if (remoteCandidatesEnd && localCandidatesEnd && !pendingAbove) {\n return { action: \"nominate\", pair };\n }\n const deadline = (quietSince ?? now) + delayMs;\n if (now >= deadline) return { action: \"nominate\", pair };\n return { action: \"wait\", deadline };\n};\n\n/** What a USE-CANDIDATE check came back with. */\nexport type NominationOutcome =\n /** Answered with success. */\n | \"nominated\"\n /** Timed out: the request may have reached the peer, which may have selected the pair. */\n | \"unanswered\"\n /** An error response, or the request never left (e.g. a refused TCP connect). */\n | \"rejected\"\n /** 487: the peer is controlling too and wins (RFC 8445 §7.2.5.1). */\n | \"role-conflict\";\n\n/** What a checklist (live or provisional) supplies to its `RegularNomination`. */\nexport type NominationHost = {\n /** The controlling agent nominates in this checklist now (full ICE on both sides, nothing nominated yet, the checklist still current). */\n active: () => boolean;\n /** The checklist's checks began (connect(), startProvisionalChecks()). */\n checking: () => boolean;\n /** The checklist, highest priority first. */\n pairs: () => readonly CandidatePair[];\n remoteCandidatesEnd: () => boolean;\n localCandidatesEnd: () => boolean;\n delayMs: () => number;\n /** Pairs never checked locally (forceTurn), which cannot be waited for. */\n unchecked: (pair: CandidatePair) => boolean;\n outranks: (pair: CandidatePair, other: CandidatePair) => boolean;\n /** Matches the only pair that may be nominated: the peer may have selected it already (see `NominationHint`). */\n preferred: () => ((pair: CandidatePair) => boolean) | undefined;\n /** A result for `pair` still applies: the pair is in the checklist and the agent open. */\n owns: (pair: CandidatePair) => boolean;\n /** Send the USE-CANDIDATE check; never rejects. */\n send: (\n pair: CandidatePair,\n signal: AbortSignal,\n ) => Promise<NominationOutcome>;\n nominated: (pair: CandidatePair) => void;\n /** The peer refused the pair: fail it, so the next valid pair is nominated. */\n rejected: (pair: CandidatePair) => void;\n roleConflict: (pair: CandidatePair) => void;\n /**\n * No attempt on a pair the peer may have selected was answered, or that\n * pair (the hint) failed or never appeared (`pair` undefined).\n */\n abandoned: (pair?: CandidatePair) => void;\n /** The delay ended (after the re-evaluation it triggers). */\n deadlinePassed?: () => void;\n};\n\n/**\n * The regular nomination (RFC 8445 §8.1.1) of one checklist: the single\n * state and rule set the live checklist and the provisional (ICE-restart)\n * checklist share. It holds the delay, the USE-CANDIDATE attempt in flight,\n * its repeats and the abandoned state; the checklist supplies the pairs and\n * applies the outcome (`NominationHost`).\n *\n * - Delay: `quietSince` is the first valid pair, moved forward by every pair\n * added later that outranks the best valid pair and is checked locally\n * (a forceTurn-excluded pair cannot replace it and does not count).\n * - One attempt at a time; a result whose attempt is no longer current (a\n * restart, a replacement pranswer, close or a role change replaced it) is\n * ignored and its transaction aborted.\n * - An unanswered attempt may have been accepted: it is repeated on the same\n * pair (never another), also when a repeat is answered with an error. When\n * all `NOMINATION_ATTEMPTS` go unanswered nothing else is nominated.\n * - An error answering the first attempt: only that pair fails.\n */\nexport class RegularNomination {\n private quietSince?: number;\n /** The checklist began waiting for the preferred pair (see `decideNomination`). */\n private preferredSince?: number;\n private timer?: ReturnType<typeof setTimeout>;\n private attempt?: { pair: CandidatePair; abort: AbortController };\n /** Every attempt went unanswered: no other pair is nominated until reset. */\n abandoned = false;\n /**\n * The pair a USE-CANDIDATE was sent to and the peer did not refuse: the\n * peer may have selected it (the provisional checklist hands it to the\n * live one at the final answer, see `NominationHint`).\n */\n maybeSelected?: CandidatePair;\n\n private readonly host: NominationHost;\n\n constructor(host: NominationHost) {\n this.host = host;\n }\n\n /** A USE-CANDIDATE transaction is in flight. */\n get inFlight() {\n return this.attempt !== undefined;\n }\n\n /** Waiting for the delay to end. */\n get waiting() {\n return this.timer !== undefined;\n }\n\n /** `pair` joined the checklist; `best` was its best valid pair before. */\n pairAdded(pair: CandidatePair, best: CandidatePair | undefined) {\n // A pair that may replace the best valid pair restarts the delay; a\n // lower-priority one, or one the agent never checks, cannot change the\n // choice and does not.\n if (best && !this.host.unchecked(pair) && this.host.outranks(pair, best)) {\n this.quietSince = Date.now();\n this.evaluate();\n }\n }\n\n /**\n * Nominate now, or arm the timer that re-evaluates at the deadline. Called\n * whenever the answer may change: a check finished, a pair was added,\n * end-of-candidates, the role changed, the timer fired.\n */\n evaluate() {\n if (!this.host.active() || this.attempt || this.abandoned) {\n this.stop();\n return;\n }\n const pairs = this.host.pairs();\n const now = Date.now();\n if (\n this.quietSince === undefined &&\n pairs.some((pair) => pair.state === CandidatePairState.SUCCEEDED)\n ) {\n this.quietSince = now;\n }\n const preferred = this.host.preferred();\n if (\n preferred &&\n this.preferredSince === undefined &&\n this.host.checking()\n ) {\n this.preferredSince = now;\n }\n const decision = decideNomination({\n pairs,\n remoteCandidatesEnd: this.host.remoteCandidatesEnd(),\n localCandidatesEnd: this.host.localCandidatesEnd(),\n quietSince: this.quietSince,\n delayMs: this.host.delayMs(),\n now,\n unchecked: this.host.unchecked,\n preferred,\n preferredSince: this.preferredSince,\n });\n this.stop();\n if (decision.action === \"wait\") {\n this.timer = setTimeout(\n () => {\n this.timer = undefined;\n this.evaluate();\n this.host.deadlinePassed?.();\n },\n Math.max(0, decision.deadline - now),\n );\n } else if (decision.action === \"nominate\") {\n this.nominate(decision.pair, 1, false);\n } else if (decision.action === \"abandon\") {\n log(\"preferred pair cannot be nominated, giving up\");\n this.abandoned = true;\n this.host.abandoned(decision.pair);\n }\n }\n\n /** Stop waiting (a pair was nominated, or nomination is not possible now). */\n stop() {\n if (this.timer !== undefined) {\n clearTimeout(this.timer);\n this.timer = undefined;\n }\n }\n\n /** The role changed: the attempt in flight was sent as the controlling agent and changes nothing after it. */\n invalidate() {\n this.attempt?.abort.abort();\n this.attempt = undefined;\n this.abandoned = false;\n }\n\n /** The checklist starts over (restart, replacement pranswer, renomination, close). */\n reset() {\n this.invalidate();\n this.stop();\n this.quietSince = undefined;\n this.preferredSince = undefined;\n this.maybeSelected = undefined;\n }\n\n /**\n * @param maybeAccepted an earlier attempt for `pair` went unanswered: the\n * peer may have selected the pair already, so no later outcome lets the\n * nomination move to another pair.\n */\n private nominate(\n pair: CandidatePair,\n attempt: number,\n maybeAccepted: boolean,\n ) {\n log(\"nominate\", pair.toJSON(), { attempt, maybeAccepted });\n const current = { pair, abort: new AbortController() };\n this.attempt = current;\n this.maybeSelected = pair;\n void this.host.send(pair, current.abort.signal).then((outcome) => {\n if (this.attempt !== current) return;\n this.attempt = undefined;\n if (!this.host.owns(pair)) {\n // The pair left the checklist (another remote generation dropped).\n if (this.maybeSelected === pair) this.maybeSelected = undefined;\n this.evaluate();\n return;\n }\n if (outcome === \"nominated\") {\n this.stop();\n this.host.nominated(pair);\n return;\n }\n if (outcome === \"role-conflict\") {\n // The peer did not accept it as controlled; it nominates now.\n log(\"nomination role conflict\", pair.toJSON());\n if (!maybeAccepted) this.maybeSelected = undefined;\n this.host.roleConflict(pair);\n return;\n }\n if (outcome === \"unanswered\" || maybeAccepted) {\n if (attempt < NOMINATION_ATTEMPTS) {\n log(\"nomination unanswered, repeating\", pair.toJSON(), { attempt });\n this.nominate(pair, attempt + 1, true);\n return;\n }\n log(\"nomination unanswered, giving up\", pair.toJSON());\n this.abandoned = true;\n this.stop();\n this.host.abandoned(pair);\n return;\n }\n log(\"nomination rejected\", pair.toJSON());\n this.maybeSelected = undefined;\n this.host.rejected(pair);\n });\n }\n}\n\n/**\n * The pair the controlling agent may have nominated in the provisional\n * checklist when a final answer replaced it with the live one. The peer\n * may have selected that pair already: a USE-CANDIDATE retransmission can\n * reach its live checklist (same credentials) after it committed the\n * answer, and a peer that keeps the ICE session across pranswer -> answer\n * (JSEP) keeps its provisional selection. The live checklist checks from\n * scratch (`[2.7-T4a]`) but nominates only the pair with the same 5-tuple;\n * when that pair fails or never appears it nominates nothing else and the\n * agent fails (see `decideNomination`). It only applies while the live\n * checklist uses the provisional credentials and the agent keeps the pair's\n * local socket (`protocol`); otherwise the usual rules choose the pair.\n */\nexport type NominationHint = {\n localUsername: string;\n remoteUsername: string;\n remotePassword: string;\n key: string;\n /**\n * The local socket (or TURN allocation) of the pair. The hint holds only\n * while the agent keeps it: a commit that closes it (an ICE restart\n * allocates TURN afresh, gather options that exclude the socket) ends the\n * path for both agents, so no selection of the peer is left to match.\n */\n protocol: Protocol;\n};\n\n/** Local foundation and the 5-tuple: the same path in another checklist. */\nexport const pairKey = (pair: CandidatePair) =>\n [\n pair.localCandidate.foundation,\n pair.localCandidate.transport.toLowerCase(),\n pair.localCandidate.host,\n pair.localCandidate.port,\n pair.remoteCandidate.transport.toLowerCase(),\n pair.remoteCandidate.host,\n pair.remoteCandidate.port,\n ].join(\" \");\n"]}