werift 0.25.2 → 0.27.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 (568) hide show
  1. package/README.md +63 -0
  2. package/THIRD_PARTY_LICENSES_LIBWEBRTC.txt +58 -0
  3. package/lib/common/src/asn1.d.ts +42 -0
  4. package/lib/common/src/asn1.js +163 -0
  5. package/lib/common/src/asn1.js.map +1 -0
  6. package/lib/common/src/binary.d.ts +1 -3
  7. package/lib/common/src/binary.js +8 -6
  8. package/lib/common/src/binary.js.map +1 -1
  9. package/lib/common/src/binaryData.js +2 -1
  10. package/lib/common/src/binaryData.js.map +1 -1
  11. package/lib/common/src/index.d.ts +6 -5
  12. package/lib/common/src/index.js +6 -5
  13. package/lib/common/src/index.js.map +1 -1
  14. package/lib/common/src/log.d.ts +12 -1
  15. package/lib/common/src/log.js +142 -12
  16. package/lib/common/src/log.js.map +1 -1
  17. package/lib/common/src/network.js.map +1 -1
  18. package/lib/common/src/queue.d.ts +41 -0
  19. package/lib/common/src/queue.js +146 -0
  20. package/lib/common/src/queue.js.map +1 -0
  21. package/lib/common/src/transport.d.ts +15 -2
  22. package/lib/common/src/transport.js +108 -19
  23. package/lib/common/src/transport.js.map +1 -1
  24. package/lib/dtls/src/cipher/create.js.map +1 -1
  25. package/lib/dtls/src/cipher/ec.js +8 -8
  26. package/lib/dtls/src/cipher/ec.js.map +1 -1
  27. package/lib/dtls/src/cipher/namedCurve.js +6 -7
  28. package/lib/dtls/src/cipher/namedCurve.js.map +1 -1
  29. package/lib/dtls/src/cipher/prf.d.ts +2 -2
  30. package/lib/dtls/src/cipher/prf.js +29 -8
  31. package/lib/dtls/src/cipher/prf.js.map +1 -1
  32. package/lib/dtls/src/cipher/suites/aead.d.ts +1 -1
  33. package/lib/dtls/src/context/certificate.d.ts +32 -0
  34. package/lib/dtls/src/context/certificate.js +54 -0
  35. package/lib/dtls/src/context/certificate.js.map +1 -0
  36. package/lib/dtls/src/context/cipher.d.ts +13 -7
  37. package/lib/dtls/src/context/cipher.js +58 -106
  38. package/lib/dtls/src/context/cipher.js.map +1 -1
  39. package/lib/dtls/src/context/dtls.js +4 -2
  40. package/lib/dtls/src/context/dtls.js.map +1 -1
  41. package/lib/dtls/src/context/transport.d.ts +4 -0
  42. package/lib/dtls/src/context/transport.js +16 -1
  43. package/lib/dtls/src/context/transport.js.map +1 -1
  44. package/lib/dtls/src/flight/client/flight1.js +2 -1
  45. package/lib/dtls/src/flight/client/flight1.js.map +1 -1
  46. package/lib/dtls/src/flight/client/flight5.js +4 -2
  47. package/lib/dtls/src/flight/client/flight5.js.map +1 -1
  48. package/lib/dtls/src/flight/flight.d.ts +1 -1
  49. package/lib/dtls/src/flight/flight.js +16 -5
  50. package/lib/dtls/src/flight/flight.js.map +1 -1
  51. package/lib/dtls/src/flight/server/flight2.js.map +1 -1
  52. package/lib/dtls/src/flight/server/flight4.d.ts +2 -1
  53. package/lib/dtls/src/flight/server/flight4.js +12 -4
  54. package/lib/dtls/src/flight/server/flight4.js.map +1 -1
  55. package/lib/dtls/src/flight/server/flight6.js +2 -1
  56. package/lib/dtls/src/flight/server/flight6.js.map +1 -1
  57. package/lib/dtls/src/handshake/const.d.ts +17 -13
  58. package/lib/dtls/src/handshake/const.js +16 -14
  59. package/lib/dtls/src/handshake/const.js.map +1 -1
  60. package/lib/dtls/src/handshake/extensions/signature.d.ts +1 -1
  61. package/lib/dtls/src/handshake/extensions/signature.js.map +1 -1
  62. package/lib/dtls/src/handshake/message/alert.js +5 -3
  63. package/lib/dtls/src/handshake/message/alert.js.map +1 -1
  64. package/lib/dtls/src/handshake/message/certificate.js +3 -2
  65. package/lib/dtls/src/handshake/message/certificate.js.map +1 -1
  66. package/lib/dtls/src/handshake/message/changeCipherSpec.js +4 -4
  67. package/lib/dtls/src/handshake/message/changeCipherSpec.js.map +1 -1
  68. package/lib/dtls/src/handshake/message/client/certificateVerify.d.ts +1 -1
  69. package/lib/dtls/src/handshake/message/client/certificateVerify.js +5 -3
  70. package/lib/dtls/src/handshake/message/client/certificateVerify.js.map +1 -1
  71. package/lib/dtls/src/handshake/message/client/hello.js +15 -8
  72. package/lib/dtls/src/handshake/message/client/hello.js.map +1 -1
  73. package/lib/dtls/src/handshake/message/client/keyExchange.js +3 -2
  74. package/lib/dtls/src/handshake/message/client/keyExchange.js.map +1 -1
  75. package/lib/dtls/src/handshake/message/finished.js +2 -1
  76. package/lib/dtls/src/handshake/message/finished.js.map +1 -1
  77. package/lib/dtls/src/handshake/message/server/certificateRequest.d.ts +1 -1
  78. package/lib/dtls/src/handshake/message/server/certificateRequest.js +7 -4
  79. package/lib/dtls/src/handshake/message/server/certificateRequest.js.map +1 -1
  80. package/lib/dtls/src/handshake/message/server/hello.d.ts +1 -1
  81. package/lib/dtls/src/handshake/message/server/hello.js +14 -8
  82. package/lib/dtls/src/handshake/message/server/hello.js.map +1 -1
  83. package/lib/dtls/src/handshake/message/server/helloDone.js +1 -1
  84. package/lib/dtls/src/handshake/message/server/helloDone.js.map +1 -1
  85. package/lib/dtls/src/handshake/message/server/helloVerifyRequest.js +5 -3
  86. package/lib/dtls/src/handshake/message/server/helloVerifyRequest.js.map +1 -1
  87. package/lib/dtls/src/handshake/message/server/keyExchange.d.ts +1 -1
  88. package/lib/dtls/src/handshake/message/server/keyExchange.js +17 -9
  89. package/lib/dtls/src/handshake/message/server/keyExchange.js.map +1 -1
  90. package/lib/dtls/src/handshake/random.d.ts +1 -1
  91. package/lib/dtls/src/handshake/random.js +7 -4
  92. package/lib/dtls/src/handshake/random.js.map +1 -1
  93. package/lib/dtls/src/imports/common.d.ts +1 -0
  94. package/lib/dtls/src/imports/common.js +1 -0
  95. package/lib/dtls/src/imports/common.js.map +1 -1
  96. package/lib/dtls/src/index.d.ts +2 -2
  97. package/lib/dtls/src/index.js +2 -2
  98. package/lib/dtls/src/index.js.map +1 -1
  99. package/lib/dtls/src/record/const.d.ts +43 -33
  100. package/lib/dtls/src/record/const.js +43 -36
  101. package/lib/dtls/src/record/const.js.map +1 -1
  102. package/lib/dtls/src/record/message/fragment.d.ts +1 -1
  103. package/lib/dtls/src/record/message/fragment.js +15 -9
  104. package/lib/dtls/src/record/message/fragment.js.map +1 -1
  105. package/lib/dtls/src/record/message/header.js +12 -7
  106. package/lib/dtls/src/record/message/header.js.map +1 -1
  107. package/lib/dtls/src/record/message/plaintext.js +4 -2
  108. package/lib/dtls/src/record/message/plaintext.js.map +1 -1
  109. package/lib/dtls/src/record/receive.js +2 -2
  110. package/lib/dtls/src/record/receive.js.map +1 -1
  111. package/lib/dtls/src/server.js +1 -1
  112. package/lib/dtls/src/server.js.map +1 -1
  113. package/lib/dtls/src/socket.d.ts +2 -2
  114. package/lib/dtls/src/socket.js +6 -4
  115. package/lib/dtls/src/socket.js.map +1 -1
  116. package/lib/ice/src/candidate.d.ts +1 -1
  117. package/lib/ice/src/candidate.js +24 -12
  118. package/lib/ice/src/candidate.js.map +1 -1
  119. package/lib/ice/src/dns/lookup.d.ts +10 -1
  120. package/lib/ice/src/dns/lookup.js +42 -7
  121. package/lib/ice/src/dns/lookup.js.map +1 -1
  122. package/lib/ice/src/exceptions.d.ts +46 -0
  123. package/lib/ice/src/exceptions.js +92 -5
  124. package/lib/ice/src/exceptions.js.map +1 -1
  125. package/lib/ice/src/helper.d.ts +1 -1
  126. package/lib/ice/src/ice.d.ts +191 -5
  127. package/lib/ice/src/ice.js +1048 -156
  128. package/lib/ice/src/ice.js.map +1 -1
  129. package/lib/ice/src/iceBase.d.ts +174 -10
  130. package/lib/ice/src/iceBase.js +165 -12
  131. package/lib/ice/src/iceBase.js.map +1 -1
  132. package/lib/ice/src/index.d.ts +5 -5
  133. package/lib/ice/src/index.js +5 -5
  134. package/lib/ice/src/index.js.map +1 -1
  135. package/lib/ice/src/internal/resolveHostAddresses.d.ts +26 -0
  136. package/lib/ice/src/internal/resolveHostAddresses.js +45 -0
  137. package/lib/ice/src/internal/resolveHostAddresses.js.map +1 -0
  138. package/lib/ice/src/stun/protocol.d.ts +3 -1
  139. package/lib/ice/src/stun/protocol.js +16 -0
  140. package/lib/ice/src/stun/protocol.js.map +1 -1
  141. package/lib/ice/src/stun/tcpProtocol.d.ts +2 -0
  142. package/lib/ice/src/stun/tcpProtocol.js +16 -1
  143. package/lib/ice/src/stun/tcpProtocol.js.map +1 -1
  144. package/lib/ice/src/stun/transaction.d.ts +35 -1
  145. package/lib/ice/src/stun/transaction.js +93 -18
  146. package/lib/ice/src/stun/transaction.js.map +1 -1
  147. package/lib/ice/src/turn/credential.d.ts +49 -0
  148. package/lib/ice/src/turn/credential.js +78 -0
  149. package/lib/ice/src/turn/credential.js.map +1 -0
  150. package/lib/ice/src/turn/protocol.d.ts +97 -7
  151. package/lib/ice/src/turn/protocol.js +357 -55
  152. package/lib/ice/src/turn/protocol.js.map +1 -1
  153. package/lib/ice/src/utils.js.map +1 -1
  154. package/lib/ice-server/src/stun/attributes.d.ts +2 -0
  155. package/lib/ice-server/src/stun/attributes.js +7 -1
  156. package/lib/ice-server/src/stun/attributes.js.map +1 -1
  157. package/lib/ice-server/src/stun/const.d.ts +24 -16
  158. package/lib/ice-server/src/stun/const.js +22 -18
  159. package/lib/ice-server/src/stun/const.js.map +1 -1
  160. package/lib/ice-server/src/stun/message.d.ts +3 -1
  161. package/lib/ice-server/src/stun/message.js +41 -6
  162. package/lib/ice-server/src/stun/message.js.map +1 -1
  163. package/lib/ice-server/src/turn/auth.d.ts +1 -1
  164. package/lib/index.mjs +25252 -12664
  165. package/lib/nonstandard/index.mjs +11015 -8906
  166. package/lib/polyfill/dom.mjs +30779 -18386
  167. package/lib/polyfill/index.mjs +30779 -18386
  168. package/lib/rtp/src/codec/assertPayload.d.ts +4 -0
  169. package/lib/rtp/src/codec/assertPayload.js +12 -0
  170. package/lib/rtp/src/codec/assertPayload.js.map +1 -0
  171. package/lib/rtp/src/codec/av1.js +2 -0
  172. package/lib/rtp/src/codec/av1.js.map +1 -1
  173. package/lib/rtp/src/codec/h264.d.ts +1 -1
  174. package/lib/rtp/src/codec/h264.js +35 -43
  175. package/lib/rtp/src/codec/h264.js.map +1 -1
  176. package/lib/rtp/src/codec/vp8.js +11 -5
  177. package/lib/rtp/src/codec/vp8.js.map +1 -1
  178. package/lib/rtp/src/codec/vp9.js +12 -0
  179. package/lib/rtp/src/codec/vp9.js.map +1 -1
  180. package/lib/rtp/src/extra/container/mp4/container.js +2 -1
  181. package/lib/rtp/src/extra/container/mp4/container.js.map +1 -1
  182. package/lib/rtp/src/extra/container/mp4/exp-golomb.js +2 -1
  183. package/lib/rtp/src/extra/container/mp4/exp-golomb.js.map +1 -1
  184. package/lib/rtp/src/extra/container/mp4/h264.d.ts +21 -17
  185. package/lib/rtp/src/extra/container/mp4/h264.js +26 -24
  186. package/lib/rtp/src/extra/container/mp4/h264.js.map +1 -1
  187. package/lib/rtp/src/extra/container/ogg/index.d.ts +1 -0
  188. package/lib/rtp/src/extra/container/ogg/index.js +1 -0
  189. package/lib/rtp/src/extra/container/ogg/index.js.map +1 -1
  190. package/lib/rtp/src/extra/container/ogg/writer.d.ts +90 -0
  191. package/lib/rtp/src/extra/container/ogg/writer.js +300 -0
  192. package/lib/rtp/src/extra/container/ogg/writer.js.map +1 -0
  193. package/lib/rtp/src/extra/container/webm/container.d.ts +1 -1
  194. package/lib/rtp/src/extra/container/webm/ebml/ebml.d.ts +6 -0
  195. package/lib/rtp/src/extra/container/webm/ebml/ebml.js +26 -4
  196. package/lib/rtp/src/extra/container/webm/ebml/ebml.js.map +1 -1
  197. package/lib/rtp/src/extra/processor/depacketizer.d.ts +6 -1
  198. package/lib/rtp/src/extra/processor/depacketizer.js +29 -3
  199. package/lib/rtp/src/extra/processor/depacketizer.js.map +1 -1
  200. package/lib/rtp/src/extra/processor/depacketizerCallback.d.ts +1 -11
  201. package/lib/rtp/src/extra/processor/depacketizerTransformer.d.ts +1 -1
  202. package/lib/rtp/src/extra/processor/dtx.js +4 -2
  203. package/lib/rtp/src/extra/processor/dtx.js.map +1 -1
  204. package/lib/rtp/src/extra/processor/dtxCallback.d.ts +1 -11
  205. package/lib/rtp/src/extra/processor/index.d.ts +2 -0
  206. package/lib/rtp/src/extra/processor/index.js +2 -0
  207. package/lib/rtp/src/extra/processor/index.js.map +1 -1
  208. package/lib/rtp/src/extra/processor/interface.d.ts +16 -11
  209. package/lib/rtp/src/extra/processor/interface.js.map +1 -1
  210. package/lib/rtp/src/extra/processor/jitterBuffer.d.ts +29 -6
  211. package/lib/rtp/src/extra/processor/jitterBuffer.js +101 -111
  212. package/lib/rtp/src/extra/processor/jitterBuffer.js.map +1 -1
  213. package/lib/rtp/src/extra/processor/jitterBufferCallback.d.ts +1 -11
  214. package/lib/rtp/src/extra/processor/jitterBufferTransformer.d.ts +1 -1
  215. package/lib/rtp/src/extra/processor/jitterBufferTransformer.js +2 -1
  216. package/lib/rtp/src/extra/processor/jitterBufferTransformer.js.map +1 -1
  217. package/lib/rtp/src/extra/processor/lipsync.d.ts +63 -0
  218. package/lib/rtp/src/extra/processor/lipsync.js +202 -51
  219. package/lib/rtp/src/extra/processor/lipsync.js.map +1 -1
  220. package/lib/rtp/src/extra/processor/mp4.d.ts +0 -6
  221. package/lib/rtp/src/extra/processor/mp4.js +7 -4
  222. package/lib/rtp/src/extra/processor/mp4.js.map +1 -1
  223. package/lib/rtp/src/extra/processor/mute.js +4 -2
  224. package/lib/rtp/src/extra/processor/mute.js.map +1 -1
  225. package/lib/rtp/src/extra/processor/nack.d.ts +1 -1
  226. package/lib/rtp/src/extra/processor/nack.js +5 -3
  227. package/lib/rtp/src/extra/processor/nack.js.map +1 -1
  228. package/lib/rtp/src/extra/processor/nackHandlerCallback.d.ts +1 -11
  229. package/lib/rtp/src/extra/processor/ntpTime.js +4 -6
  230. package/lib/rtp/src/extra/processor/ntpTime.js.map +1 -1
  231. package/lib/rtp/src/extra/processor/ntpTimeCallback.d.ts +1 -11
  232. package/lib/rtp/src/extra/processor/ogg.d.ts +72 -0
  233. package/lib/rtp/src/extra/processor/ogg.js +221 -0
  234. package/lib/rtp/src/extra/processor/ogg.js.map +1 -0
  235. package/lib/rtp/src/extra/processor/oggCallback.d.ts +21 -0
  236. package/lib/rtp/src/extra/processor/oggCallback.js +97 -0
  237. package/lib/rtp/src/extra/processor/oggCallback.js.map +1 -0
  238. package/lib/rtp/src/extra/processor/rtcpCallback.d.ts +1 -1
  239. package/lib/rtp/src/extra/processor/rtcpCallback.js.map +1 -1
  240. package/lib/rtp/src/extra/processor/rtpCallback.d.ts +1 -1
  241. package/lib/rtp/src/extra/processor/rtpCallback.js +3 -2
  242. package/lib/rtp/src/extra/processor/rtpCallback.js.map +1 -1
  243. package/lib/rtp/src/extra/processor/rtpStream.d.ts +2 -2
  244. package/lib/rtp/src/extra/processor/rtpStream.js +2 -1
  245. package/lib/rtp/src/extra/processor/rtpStream.js.map +1 -1
  246. package/lib/rtp/src/extra/processor/rtpTime.js +2 -1
  247. package/lib/rtp/src/extra/processor/rtpTime.js.map +1 -1
  248. package/lib/rtp/src/extra/processor/rtpTimeCallback.d.ts +1 -11
  249. package/lib/rtp/src/extra/processor/webm.d.ts +27 -0
  250. package/lib/rtp/src/extra/processor/webm.js +57 -23
  251. package/lib/rtp/src/extra/processor/webm.js.map +1 -1
  252. package/lib/rtp/src/extra/processor/webmCallback.js +6 -1
  253. package/lib/rtp/src/extra/processor/webmCallback.js.map +1 -1
  254. package/lib/rtp/src/extra/processor/webmStream.js.map +1 -1
  255. package/lib/rtp/src/helper.d.ts +2 -2
  256. package/lib/rtp/src/helper.js +2 -6
  257. package/lib/rtp/src/helper.js.map +1 -1
  258. package/lib/rtp/src/index.d.ts +10 -2
  259. package/lib/rtp/src/index.js +10 -2
  260. package/lib/rtp/src/index.js.map +1 -1
  261. package/lib/rtp/src/rtcp/psfb/fullIntraRequest.d.ts +4 -0
  262. package/lib/rtp/src/rtcp/psfb/fullIntraRequest.js +6 -1
  263. package/lib/rtp/src/rtcp/psfb/fullIntraRequest.js.map +1 -1
  264. package/lib/rtp/src/rtcp/rtcp.js +9 -4
  265. package/lib/rtp/src/rtcp/rtcp.js.map +1 -1
  266. package/lib/rtp/src/rtcp/rtpfb/transportWideSequence.d.ts +58 -0
  267. package/lib/rtp/src/rtcp/rtpfb/transportWideSequence.js +143 -0
  268. package/lib/rtp/src/rtcp/rtpfb/transportWideSequence.js.map +1 -0
  269. package/lib/rtp/src/rtcp/rtpfb/twcc.d.ts +35 -13
  270. package/lib/rtp/src/rtcp/rtpfb/twcc.js +132 -54
  271. package/lib/rtp/src/rtcp/rtpfb/twcc.js.map +1 -1
  272. package/lib/rtp/src/rtcp/rtpfb/twccReceiveTiming.d.ts +13 -0
  273. package/lib/rtp/src/rtcp/rtpfb/twccReceiveTiming.js +29 -0
  274. package/lib/rtp/src/rtcp/rtpfb/twccReceiveTiming.js.map +1 -0
  275. package/lib/rtp/src/rtcp/rtpfb/twccReferenceTime.d.ts +41 -0
  276. package/lib/rtp/src/rtcp/rtpfb/twccReferenceTime.js +89 -0
  277. package/lib/rtp/src/rtcp/rtpfb/twccReferenceTime.js.map +1 -0
  278. package/lib/rtp/src/rtcp/sr.d.ts +4 -3
  279. package/lib/rtp/src/rtcp/sr.js +4 -2
  280. package/lib/rtp/src/rtcp/sr.js.map +1 -1
  281. package/lib/rtp/src/rtp/continuity.d.ts +36 -0
  282. package/lib/rtp/src/rtp/continuity.js +100 -0
  283. package/lib/rtp/src/rtp/continuity.js.map +1 -0
  284. package/lib/rtp/src/rtp/headerExtension.js +41 -21
  285. package/lib/rtp/src/rtp/headerExtension.js.map +1 -1
  286. package/lib/rtp/src/rtp/padding.d.ts +17 -0
  287. package/lib/rtp/src/rtp/padding.js +37 -0
  288. package/lib/rtp/src/rtp/padding.js.map +1 -0
  289. package/lib/rtp/src/rtp/red/encoder.d.ts +10 -0
  290. package/lib/rtp/src/rtp/red/encoder.js +14 -1
  291. package/lib/rtp/src/rtp/red/encoder.js.map +1 -1
  292. package/lib/rtp/src/rtp/red/packet.js +24 -8
  293. package/lib/rtp/src/rtp/red/packet.js.map +1 -1
  294. package/lib/rtp/src/rtp/rtp.d.ts +24 -1
  295. package/lib/rtp/src/rtp/rtp.js +70 -29
  296. package/lib/rtp/src/rtp/rtp.js.map +1 -1
  297. package/lib/rtp/src/rtp/rtx.js +3 -0
  298. package/lib/rtp/src/rtp/rtx.js.map +1 -1
  299. package/lib/rtp/src/rtp/sendHeaderExtensions.d.ts +36 -0
  300. package/lib/rtp/src/rtp/sendHeaderExtensions.js +85 -0
  301. package/lib/rtp/src/rtp/sendHeaderExtensions.js.map +1 -0
  302. package/lib/rtp/src/srtp/cipher/ctr.d.ts +2 -2
  303. package/lib/rtp/src/srtp/cipher/ctr.js +5 -3
  304. package/lib/rtp/src/srtp/cipher/ctr.js.map +1 -1
  305. package/lib/rtp/src/srtp/cipher/gcm.d.ts +1 -1
  306. package/lib/rtp/src/srtp/cipher/gcm.js +1 -1
  307. package/lib/rtp/src/srtp/cipher/gcm.js.map +1 -1
  308. package/lib/rtp/src/srtp/cipher/index.js +8 -4
  309. package/lib/rtp/src/srtp/cipher/index.js.map +1 -1
  310. package/lib/rtp/src/srtp/context/context.d.ts +29 -1
  311. package/lib/rtp/src/srtp/context/context.js +84 -27
  312. package/lib/rtp/src/srtp/context/context.js.map +1 -1
  313. package/lib/rtp/src/srtp/context/srtcp.d.ts +1 -1
  314. package/lib/rtp/src/srtp/context/srtcp.js +33 -0
  315. package/lib/rtp/src/srtp/context/srtcp.js.map +1 -1
  316. package/lib/rtp/src/srtp/context/srtp.d.ts +1 -1
  317. package/lib/rtp/src/srtp/context/srtp.js +36 -4
  318. package/lib/rtp/src/srtp/context/srtp.js.map +1 -1
  319. package/lib/rtp/src/srtp/error.d.ts +8 -0
  320. package/lib/rtp/src/srtp/error.js +13 -1
  321. package/lib/rtp/src/srtp/error.js.map +1 -1
  322. package/lib/rtp/src/srtp/packet.js +4 -1
  323. package/lib/rtp/src/srtp/packet.js.map +1 -1
  324. package/lib/rtp/src/srtp/replay.d.ts +24 -0
  325. package/lib/rtp/src/srtp/replay.js +75 -0
  326. package/lib/rtp/src/srtp/replay.js.map +1 -0
  327. package/lib/rtp/src/srtp/session.js +2 -1
  328. package/lib/rtp/src/srtp/session.js.map +1 -1
  329. package/lib/rtp/src/srtp/srtcp.js +2 -1
  330. package/lib/rtp/src/srtp/srtcp.js.map +1 -1
  331. package/lib/rtp/src/srtp/srtp.js +2 -1
  332. package/lib/rtp/src/srtp/srtp.js.map +1 -1
  333. package/lib/rtp/src/util.js +2 -1
  334. package/lib/rtp/src/util.js.map +1 -1
  335. package/lib/sctp/src/chunk.d.ts +5 -0
  336. package/lib/sctp/src/chunk.js +31 -8
  337. package/lib/sctp/src/chunk.js.map +1 -1
  338. package/lib/sctp/src/const.d.ts +25 -17
  339. package/lib/sctp/src/const.js +23 -19
  340. package/lib/sctp/src/const.js.map +1 -1
  341. package/lib/sctp/src/index.d.ts +4 -3
  342. package/lib/sctp/src/index.js +10 -8
  343. package/lib/sctp/src/index.js.map +1 -1
  344. package/lib/sctp/src/param.d.ts +6 -2
  345. package/lib/sctp/src/param.js +22 -8
  346. package/lib/sctp/src/param.js.map +1 -1
  347. package/lib/sctp/src/sctp.d.ts +91 -2
  348. package/lib/sctp/src/sctp.js +700 -134
  349. package/lib/sctp/src/sctp.js.map +1 -1
  350. package/lib/sctp/src/transport.js +4 -2
  351. package/lib/sctp/src/transport.js.map +1 -1
  352. package/lib/webrtc/src/api/peerConfig.d.ts +307 -0
  353. package/lib/webrtc/src/api/peerConfig.js +291 -0
  354. package/lib/webrtc/src/api/peerConfig.js.map +1 -0
  355. package/lib/webrtc/src/api/peerConnectionEvents.d.ts +36 -0
  356. package/lib/webrtc/src/api/peerConnectionEvents.js +44 -0
  357. package/lib/webrtc/src/api/peerConnectionEvents.js.map +1 -0
  358. package/lib/webrtc/src/dataChannel.d.ts +11 -1
  359. package/lib/webrtc/src/dataChannel.js +25 -5
  360. package/lib/webrtc/src/dataChannel.js.map +1 -1
  361. package/lib/webrtc/src/index.d.ts +2 -2
  362. package/lib/webrtc/src/index.js +2 -2
  363. package/lib/webrtc/src/index.js.map +1 -1
  364. package/lib/webrtc/src/media/index.d.ts +11 -1
  365. package/lib/webrtc/src/media/index.js +71 -1
  366. package/lib/webrtc/src/media/index.js.map +1 -1
  367. package/lib/webrtc/src/media/receiver/nack.js +5 -5
  368. package/lib/webrtc/src/media/receiver/nack.js.map +1 -1
  369. package/lib/webrtc/src/media/receiver/receiverTwcc.d.ts +61 -1
  370. package/lib/webrtc/src/media/receiver/receiverTwcc.js +218 -63
  371. package/lib/webrtc/src/media/receiver/receiverTwcc.js.map +1 -1
  372. package/lib/webrtc/src/media/router.d.ts +130 -6
  373. package/lib/webrtc/src/media/router.js +325 -29
  374. package/lib/webrtc/src/media/router.js.map +1 -1
  375. package/lib/webrtc/src/media/rtpReceiver.d.ts +137 -14
  376. package/lib/webrtc/src/media/rtpReceiver.js +420 -44
  377. package/lib/webrtc/src/media/rtpReceiver.js.map +1 -1
  378. package/lib/webrtc/src/media/rtpSender.d.ts +232 -65
  379. package/lib/webrtc/src/media/rtpSender.js +784 -448
  380. package/lib/webrtc/src/media/rtpSender.js.map +1 -1
  381. package/lib/webrtc/src/media/rtpTransceiver.d.ts +95 -4
  382. package/lib/webrtc/src/media/rtpTransceiver.js +121 -9
  383. package/lib/webrtc/src/media/rtpTransceiver.js.map +1 -1
  384. package/lib/webrtc/src/media/sender/bandwidthEstimator.d.ts +249 -0
  385. package/lib/webrtc/src/media/sender/bandwidthEstimator.js +144 -0
  386. package/lib/webrtc/src/media/sender/bandwidthEstimator.js.map +1 -0
  387. package/lib/webrtc/src/media/sender/createBandwidthEstimator.d.ts +20 -0
  388. package/lib/webrtc/src/media/sender/createBandwidthEstimator.js +26 -0
  389. package/lib/webrtc/src/media/sender/createBandwidthEstimator.js.map +1 -0
  390. package/lib/webrtc/src/media/sender/estimatorBinding.d.ts +70 -0
  391. package/lib/webrtc/src/media/sender/estimatorBinding.js +175 -0
  392. package/lib/webrtc/src/media/sender/estimatorBinding.js.map +1 -0
  393. package/lib/webrtc/src/media/sender/estimators/disabledBwe.d.ts +18 -0
  394. package/lib/webrtc/src/media/sender/estimators/disabledBwe.js +44 -0
  395. package/lib/webrtc/src/media/sender/estimators/disabledBwe.js.map +1 -0
  396. package/lib/webrtc/src/media/sender/estimators/gcc/acknowledgedBitrateEstimator.d.ts +79 -0
  397. package/lib/webrtc/src/media/sender/estimators/gcc/acknowledgedBitrateEstimator.js +289 -0
  398. package/lib/webrtc/src/media/sender/estimators/gcc/acknowledgedBitrateEstimator.js.map +1 -0
  399. package/lib/webrtc/src/media/sender/estimators/gcc/aimdRateControl.d.ts +87 -0
  400. package/lib/webrtc/src/media/sender/estimators/gcc/aimdRateControl.js +429 -0
  401. package/lib/webrtc/src/media/sender/estimators/gcc/aimdRateControl.js.map +1 -0
  402. package/lib/webrtc/src/media/sender/estimators/gcc/alrDetector.d.ts +21 -0
  403. package/lib/webrtc/src/media/sender/estimators/gcc/alrDetector.js +84 -0
  404. package/lib/webrtc/src/media/sender/estimators/gcc/alrDetector.js.map +1 -0
  405. package/lib/webrtc/src/media/sender/estimators/gcc/bandwidthLimitedCause.d.ts +37 -0
  406. package/lib/webrtc/src/media/sender/estimators/gcc/bandwidthLimitedCause.js +74 -0
  407. package/lib/webrtc/src/media/sender/estimators/gcc/bandwidthLimitedCause.js.map +1 -0
  408. package/lib/webrtc/src/media/sender/estimators/gcc/constants.d.ts +383 -0
  409. package/lib/webrtc/src/media/sender/estimators/gcc/constants.js +408 -0
  410. package/lib/webrtc/src/media/sender/estimators/gcc/constants.js.map +1 -0
  411. package/lib/webrtc/src/media/sender/estimators/gcc/gccBwe.d.ts +406 -0
  412. package/lib/webrtc/src/media/sender/estimators/gcc/gccBwe.js +1391 -0
  413. package/lib/webrtc/src/media/sender/estimators/gcc/gccBwe.js.map +1 -0
  414. package/lib/webrtc/src/media/sender/estimators/gcc/index.d.ts +19 -0
  415. package/lib/webrtc/src/media/sender/estimators/gcc/index.js +75 -0
  416. package/lib/webrtc/src/media/sender/estimators/gcc/index.js.map +1 -0
  417. package/lib/webrtc/src/media/sender/estimators/gcc/interArrivalDelta.d.ts +68 -0
  418. package/lib/webrtc/src/media/sender/estimators/gcc/interArrivalDelta.js +201 -0
  419. package/lib/webrtc/src/media/sender/estimators/gcc/interArrivalDelta.js.map +1 -0
  420. package/lib/webrtc/src/media/sender/estimators/gcc/intervalBudget.d.ts +26 -0
  421. package/lib/webrtc/src/media/sender/estimators/gcc/intervalBudget.js +72 -0
  422. package/lib/webrtc/src/media/sender/estimators/gcc/intervalBudget.js.map +1 -0
  423. package/lib/webrtc/src/media/sender/estimators/gcc/linkCapacityEstimator.d.ts +41 -0
  424. package/lib/webrtc/src/media/sender/estimators/gcc/linkCapacityEstimator.js +99 -0
  425. package/lib/webrtc/src/media/sender/estimators/gcc/linkCapacityEstimator.js.map +1 -0
  426. package/lib/webrtc/src/media/sender/estimators/gcc/lossBasedBwe.d.ts +247 -0
  427. package/lib/webrtc/src/media/sender/estimators/gcc/lossBasedBwe.js +1042 -0
  428. package/lib/webrtc/src/media/sender/estimators/gcc/lossBasedBwe.js.map +1 -0
  429. package/lib/webrtc/src/media/sender/estimators/gcc/overuseDetector.d.ts +26 -0
  430. package/lib/webrtc/src/media/sender/estimators/gcc/overuseDetector.js +132 -0
  431. package/lib/webrtc/src/media/sender/estimators/gcc/overuseDetector.js.map +1 -0
  432. package/lib/webrtc/src/media/sender/estimators/gcc/probeController.d.ts +305 -0
  433. package/lib/webrtc/src/media/sender/estimators/gcc/probeController.js +1073 -0
  434. package/lib/webrtc/src/media/sender/estimators/gcc/probeController.js.map +1 -0
  435. package/lib/webrtc/src/media/sender/estimators/gcc/rttBasedBackoff.d.ts +60 -0
  436. package/lib/webrtc/src/media/sender/estimators/gcc/rttBasedBackoff.js +151 -0
  437. package/lib/webrtc/src/media/sender/estimators/gcc/rttBasedBackoff.js.map +1 -0
  438. package/lib/webrtc/src/media/sender/estimators/gcc/trendlineEstimator.d.ts +44 -0
  439. package/lib/webrtc/src/media/sender/estimators/gcc/trendlineEstimator.js +246 -0
  440. package/lib/webrtc/src/media/sender/estimators/gcc/trendlineEstimator.js.map +1 -0
  441. package/lib/webrtc/src/media/sender/estimators/index.d.ts +7 -0
  442. package/lib/webrtc/src/media/sender/estimators/index.js +19 -0
  443. package/lib/webrtc/src/media/sender/estimators/index.js.map +1 -0
  444. package/lib/webrtc/src/media/sender/estimators/lightweightBwe.d.ts +64 -0
  445. package/lib/webrtc/src/media/sender/estimators/lightweightBwe.js +216 -0
  446. package/lib/webrtc/src/media/sender/estimators/lightweightBwe.js.map +1 -0
  447. package/lib/webrtc/src/media/sender/estimators/twccFeedbackAdapter.d.ts +117 -0
  448. package/lib/webrtc/src/media/sender/estimators/twccFeedbackAdapter.js +250 -0
  449. package/lib/webrtc/src/media/sender/estimators/twccFeedbackAdapter.js.map +1 -0
  450. package/lib/webrtc/src/media/sender/outgoingQueue.d.ts +40 -0
  451. package/lib/webrtc/src/media/sender/outgoingQueue.js +153 -0
  452. package/lib/webrtc/src/media/sender/outgoingQueue.js.map +1 -0
  453. package/lib/webrtc/src/media/sender/padding.d.ts +45 -0
  454. package/lib/webrtc/src/media/sender/padding.js +134 -0
  455. package/lib/webrtc/src/media/sender/padding.js.map +1 -0
  456. package/lib/webrtc/src/media/sender/pendingRtp.d.ts +35 -0
  457. package/lib/webrtc/src/media/sender/pendingRtp.js +120 -0
  458. package/lib/webrtc/src/media/sender/pendingRtp.js.map +1 -0
  459. package/lib/webrtc/src/media/sender/rtpWire.d.ts +13 -0
  460. package/lib/webrtc/src/media/sender/rtpWire.js +38 -0
  461. package/lib/webrtc/src/media/sender/rtpWire.js.map +1 -0
  462. package/lib/webrtc/src/media/sender/sendPacer.d.ts +27 -0
  463. package/lib/webrtc/src/media/sender/sendPacer.js +110 -0
  464. package/lib/webrtc/src/media/sender/sendPacer.js.map +1 -0
  465. package/lib/webrtc/src/media/sender/senderBWE.d.ts +8 -32
  466. package/lib/webrtc/src/media/sender/senderBWE.js +5 -142
  467. package/lib/webrtc/src/media/sender/senderBWE.js.map +1 -1
  468. package/lib/webrtc/src/media/sender/senderStats.d.ts +100 -0
  469. package/lib/webrtc/src/media/sender/senderStats.js +322 -0
  470. package/lib/webrtc/src/media/sender/senderStats.js.map +1 -0
  471. package/lib/webrtc/src/media/track.d.ts +21 -3
  472. package/lib/webrtc/src/media/track.js +24 -6
  473. package/lib/webrtc/src/media/track.js.map +1 -1
  474. package/lib/webrtc/src/negotiation/bundleTopology.d.ts +103 -0
  475. package/lib/webrtc/src/negotiation/bundleTopology.js +384 -0
  476. package/lib/webrtc/src/negotiation/bundleTopology.js.map +1 -0
  477. package/lib/webrtc/src/negotiation/descriptionValidation.d.ts +51 -0
  478. package/lib/webrtc/src/negotiation/descriptionValidation.js +267 -0
  479. package/lib/webrtc/src/negotiation/descriptionValidation.js.map +1 -0
  480. package/lib/webrtc/src/negotiation/iceRestartRequest.d.ts +21 -0
  481. package/lib/webrtc/src/negotiation/iceRestartRequest.js +61 -0
  482. package/lib/webrtc/src/negotiation/iceRestartRequest.js.map +1 -0
  483. package/lib/webrtc/src/negotiation/internalState.d.ts +34 -0
  484. package/lib/webrtc/src/negotiation/internalState.js +21 -0
  485. package/lib/webrtc/src/negotiation/internalState.js.map +1 -0
  486. package/lib/webrtc/src/negotiation/negotiationNeeded.d.ts +44 -0
  487. package/lib/webrtc/src/negotiation/negotiationNeeded.js +119 -0
  488. package/lib/webrtc/src/negotiation/negotiationNeeded.js.map +1 -0
  489. package/lib/webrtc/src/negotiation/remoteCandidates.d.ts +86 -0
  490. package/lib/webrtc/src/negotiation/remoteCandidates.js +349 -0
  491. package/lib/webrtc/src/negotiation/remoteCandidates.js.map +1 -0
  492. package/lib/webrtc/src/negotiation/remoteMediaApplication.d.ts +71 -0
  493. package/lib/webrtc/src/negotiation/remoteMediaApplication.js +414 -0
  494. package/lib/webrtc/src/negotiation/remoteMediaApplication.js.map +1 -0
  495. package/lib/webrtc/src/negotiation/transportActivation.d.ts +59 -0
  496. package/lib/webrtc/src/negotiation/transportActivation.js +356 -0
  497. package/lib/webrtc/src/negotiation/transportActivation.js.map +1 -0
  498. package/lib/webrtc/src/negotiationTransaction.d.ts +240 -0
  499. package/lib/webrtc/src/negotiationTransaction.js +604 -0
  500. package/lib/webrtc/src/negotiationTransaction.js.map +1 -0
  501. package/lib/webrtc/src/nonstandard/dummyMedia.js +2 -1
  502. package/lib/webrtc/src/nonstandard/dummyMedia.js.map +1 -1
  503. package/lib/webrtc/src/nonstandard/index.d.ts +1 -1
  504. package/lib/webrtc/src/nonstandard/index.js +1 -1
  505. package/lib/webrtc/src/nonstandard/index.js.map +1 -1
  506. package/lib/webrtc/src/nonstandard/navigator.js +4 -3
  507. package/lib/webrtc/src/nonstandard/navigator.js.map +1 -1
  508. package/lib/webrtc/src/nonstandard/recorder/index.d.ts +32 -8
  509. package/lib/webrtc/src/nonstandard/recorder/index.js +81 -20
  510. package/lib/webrtc/src/nonstandard/recorder/index.js.map +1 -1
  511. package/lib/webrtc/src/nonstandard/recorder/writer/index.d.ts +10 -8
  512. package/lib/webrtc/src/nonstandard/recorder/writer/index.js +2 -1
  513. package/lib/webrtc/src/nonstandard/recorder/writer/index.js.map +1 -1
  514. package/lib/webrtc/src/nonstandard/recorder/writer/ogg.d.ts +32 -0
  515. package/lib/webrtc/src/nonstandard/recorder/writer/ogg.js +185 -0
  516. package/lib/webrtc/src/nonstandard/recorder/writer/ogg.js.map +1 -0
  517. package/lib/webrtc/src/nonstandard/recorder/writer/webm.d.ts +2 -2
  518. package/lib/webrtc/src/nonstandard/recorder/writer/webm.js +10 -3
  519. package/lib/webrtc/src/nonstandard/recorder/writer/webm.js.map +1 -1
  520. package/lib/webrtc/src/nonstandard/userMedia/packetizer.d.ts +2 -2
  521. package/lib/webrtc/src/nonstandard/userMedia/packetizer.js.map +1 -1
  522. package/lib/webrtc/src/nonstandard/userMedia.d.ts +2 -2
  523. package/lib/webrtc/src/nonstandard/userMedia.js +4 -3
  524. package/lib/webrtc/src/nonstandard/userMedia.js.map +1 -1
  525. package/lib/webrtc/src/peerConnection.d.ts +176 -238
  526. package/lib/webrtc/src/peerConnection.js +1042 -913
  527. package/lib/webrtc/src/peerConnection.js.map +1 -1
  528. package/lib/webrtc/src/polyfill/api.d.ts +5 -5
  529. package/lib/webrtc/src/polyfill/api.js.map +1 -1
  530. package/lib/webrtc/src/polyfill/index.js.map +1 -1
  531. package/lib/webrtc/src/polyfill/install.js +1 -1
  532. package/lib/webrtc/src/polyfill/install.js.map +1 -1
  533. package/lib/webrtc/src/polyfill/mediaDevices.js +2 -1
  534. package/lib/webrtc/src/polyfill/mediaDevices.js.map +1 -1
  535. package/lib/webrtc/src/polyfill/registers/mp4Webm.js.map +1 -1
  536. package/lib/webrtc/src/polyfill/registers/rtpRtcp.js.map +1 -1
  537. package/lib/webrtc/src/polyfill/selectSettings.js.map +1 -1
  538. package/lib/webrtc/src/polyfill/sourceIo.js.map +1 -1
  539. package/lib/webrtc/src/sctpManager.d.ts +97 -1
  540. package/lib/webrtc/src/sctpManager.js +189 -9
  541. package/lib/webrtc/src/sctpManager.js.map +1 -1
  542. package/lib/webrtc/src/sdp.js +12 -6
  543. package/lib/webrtc/src/sdp.js.map +1 -1
  544. package/lib/webrtc/src/sdpManager.d.ts +55 -11
  545. package/lib/webrtc/src/sdpManager.js +364 -87
  546. package/lib/webrtc/src/sdpManager.js.map +1 -1
  547. package/lib/webrtc/src/secureTransportManager.d.ts +89 -28
  548. package/lib/webrtc/src/secureTransportManager.js +232 -53
  549. package/lib/webrtc/src/secureTransportManager.js.map +1 -1
  550. package/lib/webrtc/src/transceiverManager.d.ts +62 -18
  551. package/lib/webrtc/src/transceiverManager.js +272 -107
  552. package/lib/webrtc/src/transceiverManager.js.map +1 -1
  553. package/lib/webrtc/src/transport/dtls.d.ts +64 -3
  554. package/lib/webrtc/src/transport/dtls.js +166 -42
  555. package/lib/webrtc/src/transport/dtls.js.map +1 -1
  556. package/lib/webrtc/src/transport/ice.d.ts +199 -28
  557. package/lib/webrtc/src/transport/ice.js +666 -71
  558. package/lib/webrtc/src/transport/ice.js.map +1 -1
  559. package/lib/webrtc/src/transport/iceCandidateAttribute.d.ts +98 -0
  560. package/lib/webrtc/src/transport/iceCandidateAttribute.js +129 -0
  561. package/lib/webrtc/src/transport/iceCandidateAttribute.js.map +1 -0
  562. package/lib/webrtc/src/transport/sctp.d.ts +6 -1
  563. package/lib/webrtc/src/transport/sctp.js +149 -37
  564. package/lib/webrtc/src/transport/sctp.js.map +1 -1
  565. package/lib/webrtc/src/utils.d.ts +39 -6
  566. package/lib/webrtc/src/utils.js +57 -17
  567. package/lib/webrtc/src/utils.js.map +1 -1
  568. package/package.json +16 -15
@@ -60,6 +60,8 @@ const SCTPConnectionStates = [
60
60
  "connected",
61
61
  "connecting",
62
62
  ];
63
+ /** Shared empty payload of abandoned DATA chunks. */
64
+ const EMPTY_PAYLOAD = Buffer.alloc(0);
63
65
  class SCTP {
64
66
  get peerRwnd() {
65
67
  // RFC 9260 §6.2.1(D-ii): available receiver window = a_rwnd - outstanding DATA.
@@ -70,13 +72,13 @@ class SCTP {
70
72
  enumerable: true,
71
73
  configurable: true,
72
74
  writable: true,
73
- value: transport
75
+ value: void 0
74
76
  });
75
77
  Object.defineProperty(this, "port", {
76
78
  enumerable: true,
77
79
  configurable: true,
78
80
  writable: true,
79
- value: port
81
+ value: void 0
80
82
  });
81
83
  Object.defineProperty(this, "flush", {
82
84
  enumerable: true,
@@ -145,6 +147,13 @@ class SCTP {
145
147
  writable: true,
146
148
  value: false
147
149
  });
150
+ /** An INIT from the peer was accepted (answered with INIT_ACK). */
151
+ Object.defineProperty(this, "receivedInit", {
152
+ enumerable: true,
153
+ configurable: true,
154
+ writable: true,
155
+ value: false
156
+ });
148
157
  Object.defineProperty(this, "hmacKey", {
149
158
  enumerable: true,
150
159
  configurable: true,
@@ -303,23 +312,41 @@ class SCTP {
303
312
  writable: true,
304
313
  value: false
305
314
  });
315
+ /** FORWARD-TSN kept until the peer's SACK covers it, so a lost one can be resent. */
306
316
  Object.defineProperty(this, "forwardTsnChunk", {
307
317
  enumerable: true,
308
318
  configurable: true,
309
319
  writable: true,
310
320
  value: void 0
311
321
  });
322
+ Object.defineProperty(this, "forwardTsnPending", {
323
+ enumerable: true,
324
+ configurable: true,
325
+ writable: true,
326
+ value: false
327
+ });
312
328
  Object.defineProperty(this, "flightSize", {
313
329
  enumerable: true,
314
330
  configurable: true,
315
331
  writable: true,
316
332
  value: 0
317
333
  });
334
+ /** DATA chunks waiting to be transmitted for the first time. */
318
335
  Object.defineProperty(this, "outboundQueue", {
319
336
  enumerable: true,
320
337
  configurable: true,
321
338
  writable: true,
322
- value: []
339
+ value: new common_1.FifoQueue()
340
+ });
341
+ /**
342
+ * Pending send() calls in queue order, each waiting for its last chunk to
343
+ * leave outboundQueue. Resolved head-first, so completion is O(1) per call.
344
+ */
345
+ Object.defineProperty(this, "sendWaiters", {
346
+ enumerable: true,
347
+ configurable: true,
348
+ writable: true,
349
+ value: new common_1.FifoQueue()
323
350
  });
324
351
  Object.defineProperty(this, "outboundStreamSeq", {
325
352
  enumerable: true,
@@ -362,7 +389,7 @@ class SCTP {
362
389
  enumerable: true,
363
390
  configurable: true,
364
391
  writable: true,
365
- value: []
392
+ value: new common_1.FifoQueue()
366
393
  });
367
394
  Object.defineProperty(this, "transmitting", {
368
395
  enumerable: true,
@@ -403,6 +430,31 @@ class SCTP {
403
430
  writable: true,
404
431
  value: []
405
432
  });
433
+ /** Result returned for the incoming request reconfigResponseSeq; resent to a retransmitted request. */
434
+ Object.defineProperty(this, "lastReconfigResult", {
435
+ enumerable: true,
436
+ configurable: true,
437
+ writable: true,
438
+ value: void 0
439
+ });
440
+ /** The peer answered our outstanding reset request with In progress (RFC 6525 §5.2.7). */
441
+ Object.defineProperty(this, "reconfigRequestInProgress", {
442
+ enumerable: true,
443
+ configurable: true,
444
+ writable: true,
445
+ value: false
446
+ });
447
+ /**
448
+ * RFC 6525 §5.2.2 deferred reset: an incoming Outgoing SSN Reset Request whose
449
+ * Sender's Last Assigned TSN has not been received yet. DATA for the reset
450
+ * streams beyond lastTsn belongs to the new streams and is held until the reset.
451
+ */
452
+ Object.defineProperty(this, "deferredReset", {
453
+ enumerable: true,
454
+ configurable: true,
455
+ writable: true,
456
+ value: void 0
457
+ });
406
458
  // rtt calculation
407
459
  Object.defineProperty(this, "srtt", {
408
460
  enumerable: true,
@@ -468,6 +520,23 @@ class SCTP {
468
520
  writable: true,
469
521
  value: void 0
470
522
  });
523
+ /**
524
+ * Fires at the earliest expiry of a message in outboundQueue, so expired
525
+ * messages are abandoned even if nothing else (SACK, window update, send)
526
+ * runs transmit.
527
+ */
528
+ Object.defineProperty(this, "outboundExpiryHandle", {
529
+ enumerable: true,
530
+ configurable: true,
531
+ writable: true,
532
+ value: void 0
533
+ });
534
+ Object.defineProperty(this, "outboundExpiryAt", {
535
+ enumerable: true,
536
+ configurable: true,
537
+ writable: true,
538
+ value: void 0
539
+ });
471
540
  /**Re-configuration Timer */
472
541
  Object.defineProperty(this, "timerReconfigHandle", {
473
542
  enumerable: true,
@@ -533,16 +602,25 @@ class SCTP {
533
602
  this.localTsn = tsnPlusOne(this.localTsn);
534
603
  chunks.push(chunk);
535
604
  }
536
- chunks.forEach((chunk) => {
605
+ for (const chunk of chunks) {
537
606
  this.outboundQueue.push(chunk);
538
- });
607
+ }
608
+ if (expiry !== undefined)
609
+ this.armOutboundExpiry(expiry);
539
610
  if (ordered) {
540
611
  this.outboundStreamSeq[streamId] = (0, common_1.uint16Add)(streamSeqNum, 1);
541
612
  }
613
+ // Resolves once this call's last chunk is handed to the network (moved to
614
+ // sentQueue), not when the whole outboundQueue drains, so N queued sends
615
+ // do not wake N waiters on every transmit.
616
+ const lastChunk = chunks[chunks.length - 1];
617
+ const sent = lastChunk && !this.isStopped
618
+ ? new Promise((resolve) => {
619
+ this.sendWaiters.push({ chunk: lastChunk, resolve });
620
+ })
621
+ : undefined;
542
622
  await this.transmit();
543
- while (this.outboundQueue.length) {
544
- await this.flush.asPromise();
545
- }
623
+ await sent;
546
624
  }
547
625
  });
548
626
  Object.defineProperty(this, "timer1Expired", {
@@ -606,21 +684,63 @@ class SCTP {
606
684
  return;
607
685
  this.timer3Handle = undefined;
608
686
  // # mark retransmit or abandoned chunks
687
+ // RFC 9260 §6.3.3 (E3): only outstanding chunks; gap-acked ones are kept
688
+ // for reneging but not retransmitted.
609
689
  this.sentQueue.forEach((chunk) => {
690
+ if (chunk.acked || chunk.abandoned)
691
+ return;
610
692
  if (!this.maybeAbandon(chunk)) {
611
693
  chunk.retransmit = true;
612
694
  }
695
+ this.flightSizeDecrease(chunk);
613
696
  });
614
697
  this.updateAdvancedPeerAckPoint();
698
+ // An unacknowledged FORWARD-TSN may have been lost; resend it.
699
+ if (this.forwardTsnChunk) {
700
+ this.forwardTsnPending = true;
701
+ }
615
702
  // # adjust congestion window
616
703
  this.fastRecoveryExit = undefined;
617
- this.flightSize = 0;
618
704
  this.partialBytesAcked = 0;
619
705
  this.ssthresh = Math.max(Math.floor(this.cwnd / 2), 4 * this.maxPayloadSize);
620
706
  this.cwnd = this.maxPayloadSize;
621
707
  this.transmit();
622
708
  }
623
709
  });
710
+ /**
711
+ * Abandons every expired message still in outboundQueue, wherever it is.
712
+ * Chunks stay in place so TSN order is kept: transmit() moves them to
713
+ * sentQueue unsent when they reach the head, and FORWARD-TSN skips them.
714
+ * Their send() resolves now, without waiting for earlier messages.
715
+ */
716
+ Object.defineProperty(this, "outboundExpiryExpired", {
717
+ enumerable: true,
718
+ configurable: true,
719
+ writable: true,
720
+ value: () => {
721
+ this.outboundExpiryHandle = undefined;
722
+ this.outboundExpiryAt = undefined;
723
+ if (this.isStopped)
724
+ return;
725
+ let nextExpiry;
726
+ for (const chunk of this.outboundQueue) {
727
+ if (chunk.abandoned || chunk.expiry === undefined)
728
+ continue;
729
+ if (this.isExpired(chunk)) {
730
+ this.markAbandoned(chunk);
731
+ this.resolveSendWaiterOutOfOrder(chunk);
732
+ }
733
+ else if (nextExpiry === undefined || chunk.expiry < nextExpiry) {
734
+ nextExpiry = chunk.expiry;
735
+ }
736
+ }
737
+ if (nextExpiry !== undefined)
738
+ this.armOutboundExpiry(nextExpiry);
739
+ this.transmit().catch((err) => {
740
+ log("transmit on outbound expiry failed", err.message);
741
+ });
742
+ }
743
+ });
624
744
  Object.defineProperty(this, "timerReconfigHandleExpired", {
625
745
  enumerable: true,
626
746
  configurable: true,
@@ -628,9 +748,13 @@ class SCTP {
628
748
  value: async () => {
629
749
  if (this.isStopped)
630
750
  return;
631
- this.timerReconfigFailures++;
751
+ // RFC 6525 §5.2.7: no error count while the peer reports In progress.
752
+ if (this.reconfigRequestInProgress)
753
+ this.timerReconfigFailures = 0;
754
+ else
755
+ this.timerReconfigFailures++;
632
756
  // back off
633
- this.rto = Math.ceil(this.rto * 1.5);
757
+ this.rto = Math.min(Math.ceil(this.rto * 1.5), SCTP_RTO_MAX);
634
758
  if (this.timerReconfigFailures > SCTP_MAX_ASSOCIATION_RETRANS) {
635
759
  log("timerReconfigFailures", this.timerReconfigFailures);
636
760
  this.setState(const_1.SCTP_STATE.CLOSED);
@@ -665,6 +789,8 @@ class SCTP {
665
789
  this.heartbeatStart();
666
790
  }
667
791
  });
792
+ this.transport = transport;
793
+ this.port = port;
668
794
  this.mtu = options.mtu ?? exports.DEFAULT_SCTP_MTU;
669
795
  validateSctpMtu(this.mtu);
670
796
  this.maxPayloadSize = maxPayloadSizeForMtu(this.mtu);
@@ -704,7 +830,14 @@ class SCTP {
704
830
  if (chunks.length != 1) {
705
831
  throw new Error();
706
832
  }
707
- expectedTag = 0;
833
+ // RFC 9260 §8.5.1 (A): an INIT carries verification tag 0. An older
834
+ // werift (<= 0.26) that answered our INIT before its own start() then
835
+ // sends its INIT with the tag it learned from ours; only a peer that
836
+ // received our INIT knows that tag, so accept it as well.
837
+ expectedTag =
838
+ verificationTag === this.localVerificationTag
839
+ ? this.localVerificationTag
840
+ : 0;
708
841
  }
709
842
  else {
710
843
  expectedTag = this.localVerificationTag;
@@ -716,6 +849,9 @@ class SCTP {
716
849
  for (const chunk of chunks) {
717
850
  await this.receiveChunk(chunk);
718
851
  }
852
+ // Messages of this packet are delivered by now, so the last message before
853
+ // the reset boundary reaches onReceive before onReconfigStreams.
854
+ await this.maybePerformDeferredReset();
719
855
  if (this.sackNeeded) {
720
856
  // RFC 9260 delayed ACK guidance is per packet ("every second packet").
721
857
  if (this.sackHasNewDataInPacket) {
@@ -757,8 +893,8 @@ class SCTP {
757
893
  return;
758
894
  const gaps = [];
759
895
  let gapNext;
760
- [...this.sackMisOrdered].sort().forEach((tsn) => {
761
- const pos = (tsn - this.lastReceivedTsn) % SCTP_TSN_MODULO;
896
+ sortTsns(this.sackMisOrdered, this.lastReceivedTsn).forEach((tsn) => {
897
+ const pos = tsnDistance(this.lastReceivedTsn, tsn);
762
898
  if (tsn === gapNext) {
763
899
  gaps[gaps.length - 1][1] = pos;
764
900
  }
@@ -789,12 +925,28 @@ class SCTP {
789
925
  break;
790
926
  case chunk_1.InitChunk.type:
791
927
  {
792
- if (!this.isServer)
928
+ // RFC 9260 §5.2.1: an initiator that receives the peer's INIT in
929
+ // COOKIE-WAIT / COOKIE-ECHOED (both sides sent INIT) answers it
930
+ // with an INIT_ACK.
931
+ if (!this.isServer && !this.isInitiating)
793
932
  return;
794
933
  const init = chunk;
934
+ this.receivedInit = true;
795
935
  log("receive init", init);
796
- this.lastReceivedTsn = tsnMinusOne(init.initialTsn);
797
- this.reconfigResponseSeq = tsnMinusOne(init.initialTsn);
936
+ // Take the peer's initial TSN only before the handshake got past
937
+ // it (CLOSED / COOKIE-WAIT). In COOKIE-ECHOED our INIT_ACK handling
938
+ // already took it, and once ESTABLISHED a delayed duplicate INIT
939
+ // must not rewind the cumulative TSN of DATA received since.
940
+ // A different initiate tag is a restarted peer: start its TSNs over.
941
+ if (this.associationState === const_1.SCTP_STATE.CLOSED ||
942
+ this.associationState === const_1.SCTP_STATE.COOKIE_WAIT ||
943
+ init.initiateTag !== this.remoteVerificationTag) {
944
+ this.lastReceivedTsn = tsnMinusOne(init.initialTsn);
945
+ this.reconfigResponseSeq = tsnMinusOne(init.initialTsn);
946
+ // Reset requests of the previous TSN space no longer apply.
947
+ this.lastReconfigResult = undefined;
948
+ this.deferredReset = undefined;
949
+ }
798
950
  this.remoteVerificationTag = init.initiateTag;
799
951
  this.ssthresh = init.advertisedRwnd;
800
952
  this.peerAdvertisedRwnd = init.advertisedRwnd;
@@ -845,11 +997,14 @@ class SCTP {
845
997
  break;
846
998
  }
847
999
  }
848
- await this.sendChunk(echo).catch((err) => {
849
- log("send echo failed", err.message);
850
- });
1000
+ // Enter COOKIE_ECHOED and arm T1 before handing the chunk off: a
1001
+ // duplicate INIT_ACK or an early COOKIE_ACK during the send must see
1002
+ // the new state, and a failed send is packet loss that T1 recovers.
851
1003
  this.timer1Start(echo);
852
1004
  this.setState(const_1.SCTP_STATE.COOKIE_ECHOED);
1005
+ await this.sendChunk(echo).catch((err) => {
1006
+ log("send echo failed; T1 retransmits", err.message);
1007
+ });
853
1008
  }
854
1009
  break;
855
1010
  case chunk_1.SackChunk.type:
@@ -894,8 +1049,14 @@ class SCTP {
894
1049
  break;
895
1050
  case chunk_1.CookieEchoChunk.type:
896
1051
  {
897
- if (!this.isServer)
1052
+ // An initiator accepts the COOKIE_ECHO that follows its INIT_ACK
1053
+ // to a colliding INIT (RFC 9260 §5.2.1).
1054
+ if (!this.isServer &&
1055
+ !(this.receivedInit &&
1056
+ (this.isInitiating ||
1057
+ this.associationState === const_1.SCTP_STATE.ESTABLISHED))) {
898
1058
  return;
1059
+ }
899
1060
  const data = chunk;
900
1061
  const cookie = data.body;
901
1062
  const digest = (0, crypto_1.createHmac)("sha1", this.hmacKey)
@@ -920,10 +1081,20 @@ class SCTP {
920
1081
  return;
921
1082
  }
922
1083
  const ack = new chunk_1.CookieAckChunk();
923
- await this.sendChunk(ack).catch((err) => {
924
- log("send cookieAck failed", err.message);
1084
+ // Our own INIT / COOKIE_ECHO (collision) is superseded by the
1085
+ // association the peer's cookie established.
1086
+ this.timer1Cancel();
1087
+ // RFC 9260 §5.1 (D): move to ESTABLISHED when replying. Hand off the
1088
+ // COOKIE_ACK first so it precedes DATA flushed by the connected
1089
+ // event. A failed send is packet loss: the initiator retransmits
1090
+ // COOKIE_ECHO and this association (same cookie key) answers again.
1091
+ const sending = this.sendChunk(ack).catch((err) => {
1092
+ log("send cookieAck failed; peer retransmits", err.message);
925
1093
  });
926
- this.setState(const_1.SCTP_STATE.ESTABLISHED);
1094
+ if (this.associationState !== const_1.SCTP_STATE.ESTABLISHED) {
1095
+ this.setState(const_1.SCTP_STATE.ESTABLISHED);
1096
+ }
1097
+ await sending;
927
1098
  }
928
1099
  break;
929
1100
  case chunk_1.CookieAckChunk.type:
@@ -978,47 +1149,30 @@ class SCTP {
978
1149
  switch (param.type) {
979
1150
  case param_1.OutgoingSSNResetRequestParam.type:
980
1151
  {
981
- const p = param;
982
- // # send response
983
- const response = new param_1.ReconfigResponseParam(p.requestSequence, param_1.reconfigResult.ReconfigResultSuccessPerformed);
984
- this.reconfigResponseSeq = p.requestSequence;
985
- await this.sendReconfigParam(response);
986
- // # mark closed inbound streams
987
- await Promise.all(p.streams.map(async (streamId) => {
988
- delete this.inboundStreams[streamId];
989
- if (this.outboundStreamSeq[streamId]) {
990
- // Only add to reconfigQueue if not already present
991
- if (!this.reconfigQueue.includes(streamId)) {
992
- this.reconfigQueue.push(streamId);
993
- }
994
- // await this.sendResetRequest(streamId);
995
- }
996
- }));
997
- await this.transmitReconfigRequest();
998
- // # close data channel
999
- this.onReconfigStreams.execute(p.streams);
1152
+ await this.receiveOutgoingSSNResetRequest(param);
1000
1153
  }
1001
1154
  break;
1002
1155
  case param_1.ReconfigResponseParam.type:
1003
1156
  {
1004
1157
  const reset = param;
1005
- if (reset.result !== param_1.reconfigResult.ReconfigResultSuccessPerformed) {
1158
+ const isCurrentRequest = reset.responseSequence === this.reconfigRequest?.requestSequence;
1159
+ if (reset.result === param_1.reconfigResult.InProgress) {
1160
+ if (isCurrentRequest) {
1161
+ // The peer deferred the reset (RFC 6525 §5.2.2). Keep the request so the
1162
+ // Re-configuration timer retransmits it; the peer answered, so this is
1163
+ // not a reachability failure.
1164
+ log("OutgoingSSNResetRequestParam in progress");
1165
+ this.reconfigRequestInProgress = true;
1166
+ // RFC 6525 §5.2.7 H2: restart the timer for this request.
1167
+ this.timerReconfigCancel();
1168
+ this.timerReconfigHandleStart();
1169
+ }
1170
+ }
1171
+ else if (reset.result !== param_1.reconfigResult.ReconfigResultSuccessPerformed) {
1006
1172
  log("OutgoingSSNResetRequestParam failed", Object.keys(param_1.reconfigResult).find((key) => param_1.reconfigResult[key] === reset.result));
1007
1173
  }
1008
- else if (reset.responseSequence === this.reconfigRequest?.requestSequence) {
1009
- const streamIds = this.reconfigRequest.streams.map((streamId) => {
1010
- delete this.outboundStreamSeq[streamId];
1011
- return streamId;
1012
- });
1013
- this.onReconfigStreams.execute(streamIds);
1014
- this.reconfigRequest = undefined;
1015
- this.timerReconfigCancel();
1016
- // Reset RTO backoff on successful reconfiguration
1017
- this.rto = SCTP_RTO_INITIAL;
1018
- this.timerReconfigFailures = 0;
1019
- if (this.reconfigQueue.length > 0) {
1020
- await this.transmitReconfigRequest();
1021
- }
1174
+ else if (isCurrentRequest) {
1175
+ await this.completeOutgoingReset();
1022
1176
  }
1023
1177
  }
1024
1178
  break;
@@ -1026,13 +1180,143 @@ class SCTP {
1026
1180
  {
1027
1181
  const add = param;
1028
1182
  this._inboundStreamsCount += add.newStreams;
1029
- const res = new param_1.ReconfigResponseParam(add.requestSequence, 1);
1183
+ const res = new param_1.ReconfigResponseParam(add.requestSequence, param_1.reconfigResult.ReconfigResultSuccessPerformed);
1030
1184
  this.reconfigResponseSeq = add.requestSequence;
1185
+ this.lastReconfigResult = res.result;
1031
1186
  await this.sendReconfigParam(res);
1032
1187
  }
1033
1188
  break;
1034
1189
  }
1035
1190
  }
1191
+ /** The peer answered our outstanding reset request with Success - Performed. */
1192
+ async completeOutgoingReset() {
1193
+ const streamIds = this.reconfigRequest.streams.map((streamId) => {
1194
+ delete this.outboundStreamSeq[streamId];
1195
+ return streamId;
1196
+ });
1197
+ this.onReconfigStreams.execute(streamIds);
1198
+ this.reconfigRequest = undefined;
1199
+ this.reconfigRequestInProgress = false;
1200
+ this.timerReconfigCancel();
1201
+ // Reset RTO backoff on successful reconfiguration
1202
+ this.rto = SCTP_RTO_INITIAL;
1203
+ this.timerReconfigFailures = 0;
1204
+ if (this.reconfigQueue.length > 0) {
1205
+ await this.transmitReconfigRequest();
1206
+ }
1207
+ }
1208
+ /** RFC 6525 §4.1: an empty stream list means every stream. */
1209
+ resetTargetStreams(request) {
1210
+ if (request.streams.length > 0)
1211
+ return request.streams;
1212
+ return [
1213
+ ...new Set([
1214
+ ...Object.keys(this.inboundStreams).map(Number),
1215
+ ...Object.keys(this.outboundStreamSeq).map(Number),
1216
+ ]),
1217
+ ];
1218
+ }
1219
+ /** RFC 6525 §5.2.1 sequence check, then §5.2.2 immediate or deferred reset. */
1220
+ async receiveOutgoingSSNResetRequest(request) {
1221
+ const respond = (result) => this.sendReconfigParam(new param_1.ReconfigResponseParam(request.requestSequence, result));
1222
+ if (request.requestSequence === this.reconfigResponseSeq) {
1223
+ // Retransmission of an already processed request: resend the same result.
1224
+ await respond(this.lastReconfigResult ?? param_1.reconfigResult.BadSequenceNumber);
1225
+ return;
1226
+ }
1227
+ // Only one request is deferred at a time, so it is the expected one.
1228
+ const pending = this.deferredReset;
1229
+ const expected = pending
1230
+ ? pending.request.requestSequence
1231
+ : tsnPlusOne(this.reconfigResponseSeq);
1232
+ if (request.requestSequence !== expected) {
1233
+ await respond(param_1.reconfigResult.BadSequenceNumber);
1234
+ return;
1235
+ }
1236
+ // RFC 6525 §5.2.2 E1: the peer has received our outstanding request. The
1237
+ // reset itself completes only with the peer's response, and that response
1238
+ // may be lost, so keep retransmitting the request (the peer answers a
1239
+ // retransmission with the same result) instead of stopping the timer. The
1240
+ // peer is reachable, so like In progress the expiries are not failures.
1241
+ if (this.reconfigRequest &&
1242
+ request.responseSequence === this.reconfigRequest.requestSequence) {
1243
+ this.reconfigRequestInProgress = true;
1244
+ this.timerReconfigCancel();
1245
+ this.timerReconfigHandleStart();
1246
+ }
1247
+ const target = pending?.request ?? request;
1248
+ if (!this.reachedResetBoundary(target)) {
1249
+ // Some DATA up to Sender's Last Assigned TSN is still missing. A
1250
+ // retransmitted request is evaluated again, so reconfigResponseSeq stays.
1251
+ this.deferredReset ?? (this.deferredReset = { request: target, heldChunks: [] });
1252
+ await respond(param_1.reconfigResult.InProgress);
1253
+ return;
1254
+ }
1255
+ const { transmitReverseReset } = this.performIncomingStreamReset(target);
1256
+ await respond(param_1.reconfigResult.ReconfigResultSuccessPerformed);
1257
+ await transmitReverseReset();
1258
+ }
1259
+ reachedResetBoundary(request) {
1260
+ return (this.lastReceivedTsn !== undefined &&
1261
+ (0, common_1.uint32Gte)(this.lastReceivedTsn, request.lastTsn));
1262
+ }
1263
+ async maybePerformDeferredReset() {
1264
+ const pending = this.deferredReset;
1265
+ if (!pending || !this.reachedResetBoundary(pending.request))
1266
+ return;
1267
+ // RFC 6525 §5.2.2: answer Success - Performed once the deferred reset is done.
1268
+ const { transmitReverseReset } = this.performIncomingStreamReset(pending.request);
1269
+ await this.sendReconfigParam(new param_1.ReconfigResponseParam(pending.request.requestSequence, param_1.reconfigResult.ReconfigResultSuccessPerformed));
1270
+ await transmitReverseReset();
1271
+ }
1272
+ /**
1273
+ * Resets the inbound streams of `request` synchronously, so no DATA received
1274
+ * while awaiting can reach the old streams. Returns the asynchronous part,
1275
+ * the reset of our own outgoing streams.
1276
+ */
1277
+ performIncomingStreamReset(request) {
1278
+ const heldChunks = this.deferredReset?.request === request
1279
+ ? this.deferredReset.heldChunks
1280
+ : [];
1281
+ this.deferredReset = undefined;
1282
+ this.reconfigResponseSeq = request.requestSequence;
1283
+ this.lastReconfigResult = param_1.reconfigResult.ReconfigResultSuccessPerformed;
1284
+ const resetStreams = this.resetTargetStreams(request);
1285
+ for (const streamId of resetStreams) {
1286
+ const inboundStream = this.inboundStreams[streamId];
1287
+ if (inboundStream) {
1288
+ // Incomplete messages of the old stream are discarded.
1289
+ for (const chunk of inboundStream.reassembly) {
1290
+ this.advertisedRwnd += chunk.userData.length;
1291
+ }
1292
+ delete this.inboundStreams[streamId];
1293
+ }
1294
+ if (this.outboundStreamSeq[streamId]) {
1295
+ // Only add to reconfigQueue if not already present
1296
+ if (!this.reconfigQueue.includes(streamId)) {
1297
+ this.reconfigQueue.push(streamId);
1298
+ }
1299
+ }
1300
+ }
1301
+ // # close data channel
1302
+ this.onReconfigStreams.execute(resetStreams);
1303
+ // DATA beyond the boundary belongs to the reset streams (SSN starts at 0).
1304
+ const streamIds = new Set();
1305
+ for (const chunk of heldChunks) {
1306
+ this.getInboundStream(chunk.streamId).addChunk(chunk);
1307
+ streamIds.add(chunk.streamId);
1308
+ }
1309
+ for (const streamId of streamIds) {
1310
+ this.deliverMessages(this.getInboundStream(streamId));
1311
+ }
1312
+ return { transmitReverseReset: () => this.transmitReconfigRequest() };
1313
+ }
1314
+ deliverMessages(inboundStream) {
1315
+ for (const message of inboundStream.popMessages()) {
1316
+ this.advertisedRwnd += message[2].length;
1317
+ this.receive(...message);
1318
+ }
1319
+ }
1036
1320
  receiveDataChunk(chunk) {
1037
1321
  this.sackNeeded = true;
1038
1322
  if (this.markReceived(chunk.tsn)) {
@@ -1050,13 +1334,19 @@ class SCTP {
1050
1334
  // RFC 9260 permits immediate SACK on gap/loss indicators.
1051
1335
  this.sackImmediate = true;
1052
1336
  }
1053
- const inboundStream = this.getInboundStream(chunk.streamId);
1054
- inboundStream.addChunk(chunk);
1055
1337
  this.advertisedRwnd -= chunk.userData.length;
1056
- for (const message of inboundStream.popMessages()) {
1057
- this.advertisedRwnd += message[2].length;
1058
- this.receive(...message);
1338
+ const pending = this.deferredReset;
1339
+ if (pending &&
1340
+ (pending.request.streams.length === 0 ||
1341
+ pending.request.streams.includes(chunk.streamId)) &&
1342
+ (0, common_1.uint32Gt)(chunk.tsn, pending.request.lastTsn)) {
1343
+ // RFC 6525 §5.2.2: hold DATA for the new stream until the reset is performed.
1344
+ pending.heldChunks.push(chunk);
1345
+ return;
1059
1346
  }
1347
+ const inboundStream = this.getInboundStream(chunk.streamId);
1348
+ inboundStream.addChunk(chunk);
1349
+ this.deliverMessages(inboundStream);
1060
1350
  }
1061
1351
  async receiveSackChunk(chunk) {
1062
1352
  // """
@@ -1070,25 +1360,22 @@ class SCTP {
1070
1360
  let done = 0, doneBytes = 0;
1071
1361
  // # handle acknowledged data
1072
1362
  while (this.sentQueue.length > 0 &&
1073
- (0, common_1.uint32Gte)(this.lastSackedTsn, this.sentQueue[0].tsn)) {
1363
+ (0, common_1.uint32Gte)(this.lastSackedTsn, this.sentQueue.peek().tsn)) {
1074
1364
  const sChunk = this.sentQueue.shift();
1075
1365
  done++;
1076
- if (!sChunk?.acked) {
1366
+ if (!sChunk.acked) {
1077
1367
  doneBytes += sChunk.bookSize;
1078
- this.flightSizeDecrease(sChunk);
1079
1368
  }
1369
+ this.flightSizeDecrease(sChunk);
1080
1370
  if (done === 1 && sChunk.sentCount === 1) {
1081
1371
  this.updateRto(receivedTime - sChunk.sentTime);
1082
1372
  }
1083
1373
  }
1084
- // Furthermore, this exposed an issue I've seen in v8 in other projects: using an array as a queue seems to result in long calls to shift (it does an array copy under the hood?). The problem seems to get worse the more times it shifts. Resetting the queue to empty array mitigates this.
1085
- if (!this.sentQueue.length) {
1086
- this.sentQueue = [];
1087
- }
1088
1374
  // # handle gap blocks
1089
1375
  let loss = false;
1376
+ const seen = new Set();
1377
+ let highestNewlyAcked = chunk.cumulativeTsn;
1090
1378
  if (chunk.gaps.length > 0) {
1091
- const seen = new Set();
1092
1379
  let highestSeenTsn;
1093
1380
  chunk.gaps.forEach((gap) => {
1094
1381
  for (let pos = gap[0]; pos < gap[1] + 1; pos++) {
@@ -1096,7 +1383,6 @@ class SCTP {
1096
1383
  seen.add(highestSeenTsn);
1097
1384
  }
1098
1385
  });
1099
- let highestNewlyAcked = chunk.cumulativeTsn;
1100
1386
  for (const sChunk of this.sentQueue) {
1101
1387
  if ((0, common_1.uint32Gt)(sChunk.tsn, highestSeenTsn)) {
1102
1388
  break;
@@ -1104,26 +1390,46 @@ class SCTP {
1104
1390
  if (seen.has(sChunk.tsn) && !sChunk.acked) {
1105
1391
  doneBytes += sChunk.bookSize;
1106
1392
  sChunk.acked = true;
1393
+ sChunk.reneged = false;
1394
+ // A pending retransmission is moot once the peer holds the chunk.
1395
+ sChunk.retransmit = false;
1107
1396
  this.flightSizeDecrease(sChunk);
1108
1397
  highestNewlyAcked = sChunk.tsn;
1109
1398
  }
1110
1399
  }
1111
- // # strike missing chunks prior to HTNA
1400
+ }
1401
+ // # detect reneging (RFC 9260 6.2.1 D-iii)
1402
+ // A chunk gap-acked before but missing from this SACK was dropped by the
1403
+ // peer: it is outstanding again, and every SACK still missing it counts one
1404
+ // miss indication towards Fast Retransmit. Chunks up to HTNA are struck by
1405
+ // the loop below, so only the ones beyond it are struck here.
1406
+ let reneged = false;
1407
+ for (const sChunk of this.sentQueue) {
1408
+ // Abandoned chunks are never retransmitted nor counted in flight again.
1409
+ if (seen.has(sChunk.tsn) || sChunk.abandoned)
1410
+ continue;
1411
+ if (sChunk.acked) {
1412
+ sChunk.acked = false;
1413
+ sChunk.reneged = true;
1414
+ // Outstanding again, so it is counted in flight until resent or acked.
1415
+ this.flightSizeIncrease(sChunk);
1416
+ }
1417
+ if (!sChunk.reneged)
1418
+ continue;
1419
+ reneged = true;
1420
+ if (!sChunk.retransmit &&
1421
+ (chunk.gaps.length === 0 || (0, common_1.uint32Gt)(sChunk.tsn, highestNewlyAcked))) {
1422
+ loss = this.strikeChunk(sChunk) || loss;
1423
+ }
1424
+ }
1425
+ // # strike missing chunks prior to HTNA
1426
+ if (chunk.gaps.length > 0) {
1112
1427
  for (const sChunk of this.sentQueue) {
1113
1428
  if ((0, common_1.uint32Gt)(sChunk.tsn, highestNewlyAcked)) {
1114
1429
  break;
1115
1430
  }
1116
1431
  if (!seen.has(sChunk.tsn)) {
1117
- sChunk.misses++;
1118
- if (sChunk.misses === 3) {
1119
- sChunk.misses = 0;
1120
- if (!this.maybeAbandon(sChunk)) {
1121
- sChunk.retransmit = true;
1122
- }
1123
- sChunk.acked = false;
1124
- this.flightSizeDecrease(sChunk);
1125
- loss = true;
1126
- }
1432
+ loss = this.strikeChunk(sChunk) || loss;
1127
1433
  }
1128
1434
  }
1129
1435
  }
@@ -1145,7 +1451,7 @@ class SCTP {
1145
1451
  this.ssthresh = Math.max(Math.floor(this.cwnd / 2), 4 * this.maxPayloadSize);
1146
1452
  this.cwnd = this.ssthresh;
1147
1453
  this.partialBytesAcked = 0;
1148
- this.fastRecoveryExit = this.sentQueue[this.sentQueue.length - 1].tsn;
1454
+ this.fastRecoveryExit = this.sentQueue.last().tsn;
1149
1455
  this.fastRecoveryTransmit = true;
1150
1456
  }
1151
1457
  }
@@ -1158,9 +1464,13 @@ class SCTP {
1158
1464
  else if (done > 0) {
1159
1465
  this.timer3Restart();
1160
1466
  }
1467
+ else if (reneged && !this.timer3Handle) {
1468
+ this.timer3Start();
1469
+ }
1161
1470
  // RFC 9260 §6.2.1(D-ii): keep latest peer a_rwnd; available window is derived via flightSize.
1162
1471
  this.peerAdvertisedRwnd = chunk.advertisedRwnd;
1163
1472
  this.updateAdvancedPeerAckPoint();
1473
+ this.settleForwardTsn(chunk.cumulativeTsn);
1164
1474
  await this.onSackReceived();
1165
1475
  await this.transmit();
1166
1476
  }
@@ -1174,7 +1484,7 @@ class SCTP {
1174
1484
  // # advance cumulative TSN
1175
1485
  this.lastReceivedTsn = chunk.cumulativeTsn;
1176
1486
  this.sackMisOrdered = new Set([...this.sackMisOrdered].filter(isObsolete));
1177
- for (const tsn of [...this.sackMisOrdered].sort()) {
1487
+ for (const tsn of sortTsns(this.sackMisOrdered, this.lastReceivedTsn)) {
1178
1488
  if (tsn === tsnPlusOne(this.lastReceivedTsn)) {
1179
1489
  this.lastReceivedTsn = tsn;
1180
1490
  }
@@ -1185,20 +1495,31 @@ class SCTP {
1185
1495
  // # filter out obsolete entries
1186
1496
  this.sackDuplicates = this.sackDuplicates.filter(isObsolete);
1187
1497
  this.sackMisOrdered = new Set([...this.sackMisOrdered].filter(isObsolete));
1188
- // # update reassembly
1498
+ // # update reassembly (RFC 3758 3.6)
1189
1499
  for (const [streamId, streamSeqNum] of chunk.streams) {
1190
1500
  const inboundStream = this.getInboundStream(streamId);
1191
- // # advance sequence number and perform delivery
1192
- inboundStream.streamSequenceNumber = (0, common_1.uint16Add)(streamSeqNum, 1);
1193
- for (const message of inboundStream.popMessages()) {
1501
+ // # deliver stranded complete messages, drop broken ones, then advance
1502
+ const { messages, prunedSize } = inboundStream.forwardSequenceNumber(streamSeqNum);
1503
+ this.advertisedRwnd += prunedSize;
1504
+ for (const message of [...messages, ...inboundStream.popMessages()]) {
1194
1505
  this.advertisedRwnd += message[2].length;
1195
1506
  this.receive(...message);
1196
1507
  }
1197
1508
  }
1198
- // # prune obsolete chunks
1199
- Object.values(this.inboundStreams).forEach((inboundStream) => {
1200
- this.advertisedRwnd += inboundStream.pruneChunks(this.lastReceivedTsn);
1201
- });
1509
+ // # unordered chunks covered by the cumulative TSN will never complete
1510
+ for (const inboundStream of Object.values(this.inboundStreams)) {
1511
+ this.advertisedRwnd += inboundStream.pruneUnorderedChunks(chunk.cumulativeTsn);
1512
+ }
1513
+ const pending = this.deferredReset;
1514
+ if (pending) {
1515
+ pending.heldChunks = pending.heldChunks.filter((held) => {
1516
+ // Only DATA the FORWARD-TSN skipped is dropped; later received DATA stays.
1517
+ if ((0, common_1.uint32Gt)(held.tsn, chunk.cumulativeTsn))
1518
+ return true;
1519
+ this.advertisedRwnd += held.userData.length;
1520
+ return false;
1521
+ });
1522
+ }
1202
1523
  }
1203
1524
  updateRto(R) {
1204
1525
  if (!this.srtt) {
@@ -1228,7 +1549,7 @@ class SCTP {
1228
1549
  return true;
1229
1550
  }
1230
1551
  this.sackMisOrdered.add(tsn);
1231
- for (const tsn of [...this.sackMisOrdered].sort()) {
1552
+ for (const tsn of sortTsns(this.sackMisOrdered, this.lastReceivedTsn)) {
1232
1553
  if (tsn === tsnPlusOne(this.lastReceivedTsn)) {
1233
1554
  this.lastReceivedTsn = tsn;
1234
1555
  }
@@ -1241,6 +1562,35 @@ class SCTP {
1241
1562
  this.sackMisOrdered = new Set([...this.sackMisOrdered].filter(isObsolete));
1242
1563
  return false;
1243
1564
  }
1565
+ resolveSendWaiters(sentChunk) {
1566
+ this.dropDoneSendWaiters();
1567
+ if (this.sendWaiters.peek()?.chunk === sentChunk) {
1568
+ this.sendWaiters.shift().resolve();
1569
+ this.dropDoneSendWaiters();
1570
+ }
1571
+ }
1572
+ dropDoneSendWaiters() {
1573
+ while (this.sendWaiters.peek()?.done)
1574
+ this.sendWaiters.shift();
1575
+ }
1576
+ /** Resolves the send() of an expired queued message ahead of earlier ones (rare, O(n)). */
1577
+ resolveSendWaiterOutOfOrder(chunk) {
1578
+ for (const waiter of this.sendWaiters) {
1579
+ if (waiter.chunk === chunk && !waiter.done) {
1580
+ waiter.done = true;
1581
+ waiter.resolve();
1582
+ return;
1583
+ }
1584
+ }
1585
+ }
1586
+ /** Settles every pending send() so no caller is left waiting after close. */
1587
+ releaseSendWaiters() {
1588
+ const waiters = this.sendWaiters;
1589
+ this.sendWaiters = new common_1.FifoQueue();
1590
+ for (const waiter of waiters) {
1591
+ waiter.resolve();
1592
+ }
1593
+ }
1244
1594
  async transmit() {
1245
1595
  if (this.isStopped)
1246
1596
  return;
@@ -1266,11 +1616,11 @@ class SCTP {
1266
1616
  // Transmit outbound data.
1267
1617
  // """
1268
1618
  // # send FORWARD TSN
1269
- if (this.forwardTsnChunk) {
1619
+ if (this.forwardTsnChunk && this.forwardTsnPending) {
1620
+ this.forwardTsnPending = false;
1270
1621
  await this.sendChunk(this.forwardTsnChunk).catch((err) => {
1271
1622
  log("send forwardTsn failed", err.message);
1272
1623
  });
1273
- this.forwardTsnChunk = undefined;
1274
1624
  if (!this.timer3Handle) {
1275
1625
  this.timer3Start();
1276
1626
  }
@@ -1279,9 +1629,20 @@ class SCTP {
1279
1629
  ? 2 * this.maxPayloadSize
1280
1630
  : 4 * this.maxPayloadSize;
1281
1631
  const cwnd = Math.min(this.flightSize + burstSize, this.cwnd);
1632
+ // Set when expired data is abandoned here, so the FORWARD-TSN goes out.
1633
+ let abandoned = false;
1282
1634
  let retransmitEarliest = true;
1283
1635
  for (const dataChunk of this.sentQueue) {
1636
+ if (dataChunk.retransmit && (dataChunk.acked || dataChunk.abandoned)) {
1637
+ // Acked or abandoned after being marked; nothing to resend.
1638
+ dataChunk.retransmit = false;
1639
+ }
1284
1640
  if (dataChunk.retransmit) {
1641
+ // The lifetime may have run out while the chunk waited for cwnd.
1642
+ if (this.isExpired(dataChunk) && this.maybeAbandon(dataChunk)) {
1643
+ abandoned = true;
1644
+ continue;
1645
+ }
1285
1646
  if (this.fastRecoveryTransmit) {
1286
1647
  this.fastRecoveryTransmit = false;
1287
1648
  }
@@ -1291,6 +1652,7 @@ class SCTP {
1291
1652
  this.flightSizeIncrease(dataChunk);
1292
1653
  dataChunk.misses = 0;
1293
1654
  dataChunk.retransmit = false;
1655
+ dataChunk.reneged = false;
1294
1656
  dataChunk.sentCount++;
1295
1657
  await this.sendChunk(dataChunk).catch((err) => {
1296
1658
  log("send data failed", err.message);
@@ -1302,17 +1664,28 @@ class SCTP {
1302
1664
  retransmitEarliest = false;
1303
1665
  }
1304
1666
  // Drain queued outbound DATA; retransmission pacing is controlled by RTO/T3.
1305
- while (this.outboundQueue.length > 0 &&
1306
- this.flightSize < cwnd &&
1307
- this.peerRwnd > 0) {
1308
- const chunk = this.outboundQueue.shift();
1309
- if (!chunk)
1667
+ while (this.outboundQueue.length > 0) {
1668
+ const chunk = this.outboundQueue.peek();
1669
+ // A message that expired while waiting for cwnd/rwnd is never sent; its
1670
+ // TSNs are already assigned, so it is abandoned and skipped by FORWARD-TSN.
1671
+ // A message the expiry timer already abandoned in place goes the same way.
1672
+ if (chunk.abandoned || this.isExpired(chunk)) {
1673
+ this.outboundQueue.shift();
1674
+ this.sentQueue.push(chunk);
1675
+ this.resolveSendWaiters(chunk);
1676
+ this.abandonMessage(this.sentQueue.length - 1);
1677
+ abandoned = true;
1678
+ continue;
1679
+ }
1680
+ if (this.flightSize >= cwnd || this.peerRwnd <= 0) {
1310
1681
  break;
1682
+ }
1311
1683
  if (chunk.bookSize > this.peerRwnd && this.flightSize > 0) {
1312
- this.outboundQueue.unshift(chunk);
1313
1684
  break;
1314
1685
  }
1686
+ this.outboundQueue.shift();
1315
1687
  this.sentQueue.push(chunk);
1688
+ this.resolveSendWaiters(chunk);
1316
1689
  this.flightSizeIncrease(chunk);
1317
1690
  // # update counters
1318
1691
  chunk.sentCount++;
@@ -1324,10 +1697,14 @@ class SCTP {
1324
1697
  this.timer3Start();
1325
1698
  }
1326
1699
  }
1327
- if (!this.outboundQueue.length) {
1328
- // Resetting the queue to empty array mitigates this.
1329
- this.outboundQueue = [];
1700
+ if (abandoned) {
1701
+ this.updateAdvancedPeerAckPoint();
1702
+ // Send the FORWARD-TSN in the next round of transmit().
1703
+ if (this.forwardTsnPending)
1704
+ this.transmitRequested = true;
1330
1705
  }
1706
+ if (this.outboundQueue.length === 0)
1707
+ this.outboundExpiryCancel();
1331
1708
  this.flush.execute();
1332
1709
  }
1333
1710
  async transmitReconfigRequest() {
@@ -1341,6 +1718,7 @@ class SCTP {
1341
1718
  const param = new param_1.OutgoingSSNResetRequestParam(this.reconfigRequestSeq, this.reconfigResponseSeq, tsnMinusOne(this.localTsn), streams);
1342
1719
  this.reconfigRequestSeq = tsnPlusOne(this.reconfigRequestSeq);
1343
1720
  this.reconfigRequest = param;
1721
+ this.reconfigRequestInProgress = false;
1344
1722
  await this.sendReconfigParam(param);
1345
1723
  this.timerReconfigHandleStart();
1346
1724
  }
@@ -1363,11 +1741,20 @@ class SCTP {
1363
1741
  await this.transmit();
1364
1742
  }
1365
1743
  }
1744
+ // flightSize always equals the bookSize sum of inFlight chunks in sentQueue,
1745
+ // so both helpers are idempotent per chunk.
1366
1746
  flightSizeIncrease(chunk) {
1747
+ if (chunk.inFlight)
1748
+ return;
1749
+ chunk.inFlight = true;
1367
1750
  this.flightSize += chunk.bookSize;
1368
1751
  }
1752
+ /** No-op for chunks not counted in flightSize, so every exit path can call it. */
1369
1753
  flightSizeDecrease(chunk) {
1370
- this.flightSize = Math.max(0, this.flightSize - chunk.bookSize);
1754
+ if (!chunk.inFlight)
1755
+ return;
1756
+ chunk.inFlight = false;
1757
+ this.flightSize -= chunk.bookSize;
1371
1758
  }
1372
1759
  // # timers
1373
1760
  /**t1 is wait for initAck or cookieAck */
@@ -1416,6 +1803,25 @@ class SCTP {
1416
1803
  // RFC 9260 requires restarting T3-rtx with current RTO (seconds -> JS ms).
1417
1804
  this.timer3Handle = setTimeout(this.timer3Expired, this.rto * 1000);
1418
1805
  }
1806
+ /** Arms the outbound expiry timer for `expiry` unless an earlier one is armed. */
1807
+ armOutboundExpiry(expiry) {
1808
+ if (this.isStopped)
1809
+ return;
1810
+ if (this.outboundExpiryHandle && this.outboundExpiryAt <= expiry)
1811
+ return;
1812
+ this.outboundExpiryCancel();
1813
+ this.outboundExpiryAt = expiry;
1814
+ this.outboundExpiryHandle = setTimeout(this.outboundExpiryExpired,
1815
+ // setTimeout overflows above 2^31 - 1 ms; a later expiry re-arms on fire.
1816
+ Math.min(Math.max(0, expiry - Date.now()), 0x7fffffff));
1817
+ }
1818
+ outboundExpiryCancel() {
1819
+ if (this.outboundExpiryHandle) {
1820
+ clearTimeout(this.outboundExpiryHandle);
1821
+ this.outboundExpiryHandle = undefined;
1822
+ }
1823
+ this.outboundExpiryAt = undefined;
1824
+ }
1419
1825
  timer3Cancel() {
1420
1826
  if (this.timer3Handle) {
1421
1827
  clearTimeout(this.timer3Handle);
@@ -1466,25 +1872,38 @@ class SCTP {
1466
1872
  this.heartbeatInterval = interval;
1467
1873
  this.heartbeatRestart();
1468
1874
  }
1875
+ /** Drops the FORWARD-TSN once acknowledged; otherwise keeps T3 running to resend it. */
1876
+ settleForwardTsn(sackedTsn) {
1877
+ if (!this.forwardTsnChunk)
1878
+ return;
1879
+ if ((0, common_1.uint32Gte)(sackedTsn, this.forwardTsnChunk.cumulativeTsn)) {
1880
+ this.forwardTsnChunk = undefined;
1881
+ this.forwardTsnPending = false;
1882
+ if (this.sentQueue.length === 0)
1883
+ this.timer3Cancel();
1884
+ }
1885
+ else if (!this.timer3Handle) {
1886
+ this.timer3Start();
1887
+ }
1888
+ }
1469
1889
  updateAdvancedPeerAckPoint() {
1470
1890
  if ((0, common_1.uint32Gt)(this.lastSackedTsn, this.advancedPeerAckTsn)) {
1471
1891
  this.advancedPeerAckTsn = this.lastSackedTsn;
1472
1892
  }
1473
1893
  let done = 0;
1474
- const streams = {};
1475
- while (this.sentQueue.length > 0 && this.sentQueue[0].abandoned) {
1894
+ // Carry over streams of an unacknowledged FORWARD-TSN that this one replaces.
1895
+ const streams = Object.fromEntries(this.forwardTsnChunk?.streams ?? []);
1896
+ while (this.sentQueue.length > 0 && this.sentQueue.peek().abandoned) {
1476
1897
  const chunk = this.sentQueue.shift();
1898
+ this.flightSizeDecrease(chunk);
1477
1899
  this.advancedPeerAckTsn = chunk.tsn;
1478
1900
  done++;
1479
1901
  if (!(chunk.flags & SCTP_DATA_UNORDERED)) {
1480
1902
  streams[chunk.streamId] = chunk.streamSeqNum;
1481
1903
  }
1482
1904
  }
1483
- // Resetting the queue to empty array mitigates this.
1484
- if (!this.sentQueue.length) {
1485
- this.sentQueue = [];
1486
- }
1487
1905
  if (done) {
1906
+ this.forwardTsnPending = true;
1488
1907
  this.forwardTsnChunk = new chunk_1.ForwardTsnChunk(0, undefined);
1489
1908
  this.forwardTsnChunk.cumulativeTsn = this.advancedPeerAckTsn;
1490
1909
  this.forwardTsnChunk.streams = Object.entries(streams).map(([k, v]) => [
@@ -1496,28 +1915,80 @@ class SCTP {
1496
1915
  maybeAbandon(chunk) {
1497
1916
  if (chunk.abandoned)
1498
1917
  return true;
1918
+ // The peer already holds an acked chunk (gap SACK); abandoning it would
1919
+ // make the FORWARD-TSN discard data the receiver could deliver.
1920
+ if (chunk.acked)
1921
+ return false;
1499
1922
  const abandon = (!!chunk.maxRetransmits && chunk.maxRetransmits < chunk.sentCount) ||
1500
- (!!chunk.expiry && chunk.expiry < Date.now() / 1000);
1923
+ this.isExpired(chunk);
1501
1924
  if (!abandon)
1502
1925
  return false;
1503
- const chunkPos = this.sentQueue.findIndex((v) => v.type === chunk.type);
1926
+ const chunkPos = this.sentQueue.findIndex((v) => v === chunk);
1927
+ if (chunkPos < 0)
1928
+ return false;
1929
+ this.abandonMessage(chunkPos);
1930
+ return true;
1931
+ }
1932
+ /** Counts a miss indication; on the third, marks the chunk for Fast Retransmit (or abandons it). */
1933
+ strikeChunk(chunk) {
1934
+ chunk.misses++;
1935
+ if (chunk.misses < 3)
1936
+ return false;
1937
+ chunk.misses = 0;
1938
+ if (!this.maybeAbandon(chunk)) {
1939
+ chunk.retransmit = true;
1940
+ }
1941
+ chunk.acked = false;
1942
+ this.flightSizeDecrease(chunk);
1943
+ return true;
1944
+ }
1945
+ isExpired(chunk) {
1946
+ return chunk.expiry !== undefined && chunk.expiry <= Date.now();
1947
+ }
1948
+ /**
1949
+ * An abandoned chunk is never sent or retransmitted again, so its payload is
1950
+ * released; bookSize and the TSN / stream metadata FORWARD-TSN needs stay.
1951
+ */
1952
+ markAbandoned(chunk) {
1953
+ chunk.abandoned = true;
1954
+ chunk.retransmit = false;
1955
+ chunk.userData = EMPTY_PAYLOAD;
1956
+ }
1957
+ /** Marks every fragment of the message at sentQueue[chunkPos] abandoned. */
1958
+ abandonMessage(chunkPos) {
1959
+ // Abandon the whole message: walk back to its first fragment and forward
1960
+ // to its last fragment (RFC 3758 3.5 A3).
1504
1961
  for (let pos = chunkPos; pos >= 0; pos--) {
1505
- const oChunk = this.sentQueue[pos];
1506
- oChunk.abandoned = true;
1507
- oChunk.retransmit = false;
1508
- if (oChunk.flags & SCTP_DATA_LAST_FRAG) {
1962
+ const oChunk = this.sentQueue.at(pos);
1963
+ this.markAbandoned(oChunk);
1964
+ this.flightSizeDecrease(oChunk);
1965
+ if (oChunk.flags & SCTP_DATA_FIRST_FRAG) {
1509
1966
  break;
1510
1967
  }
1511
1968
  }
1969
+ let lastFragReached = false;
1512
1970
  for (let pos = chunkPos; pos < this.sentQueue.length; pos++) {
1513
- const oChunk = this.sentQueue[pos];
1514
- oChunk.abandoned = true;
1515
- oChunk.retransmit = false;
1971
+ const oChunk = this.sentQueue.at(pos);
1972
+ this.markAbandoned(oChunk);
1973
+ this.flightSizeDecrease(oChunk);
1516
1974
  if (oChunk.flags & SCTP_DATA_LAST_FRAG) {
1975
+ lastFragReached = true;
1517
1976
  break;
1518
1977
  }
1519
1978
  }
1520
- return true;
1979
+ // The remaining fragments may still be waiting in outboundQueue (cwnd/rwnd
1980
+ // limited). Sending them would leave the receiver with a headless message,
1981
+ // so move them to sentQueue as abandoned without transmitting; they are then
1982
+ // covered by the FORWARD-TSN like the rest of the message.
1983
+ while (!lastFragReached && this.outboundQueue.length > 0) {
1984
+ const oChunk = this.outboundQueue.shift();
1985
+ this.markAbandoned(oChunk);
1986
+ this.sentQueue.push(oChunk);
1987
+ this.resolveSendWaiters(oChunk);
1988
+ if (oChunk.flags & SCTP_DATA_LAST_FRAG) {
1989
+ lastFragReached = true;
1990
+ }
1991
+ }
1521
1992
  }
1522
1993
  static getCapabilities() {
1523
1994
  return new RTCSctpCapabilities(65536);
@@ -1528,15 +1999,25 @@ class SCTP {
1528
1999
  async start(remotePort) {
1529
2000
  if (!this.started) {
1530
2001
  this.started = true;
1531
- this.setConnectionState("connecting");
2002
+ // Keep "connected" when the peer's INIT already established it.
2003
+ if (this.associationState !== const_1.SCTP_STATE.ESTABLISHED) {
2004
+ this.setConnectionState("connecting");
2005
+ }
1532
2006
  if (remotePort) {
1533
2007
  this.setRemotePort(remotePort);
1534
2008
  }
1535
- if (!this.isServer) {
2009
+ // The peer's INIT may already have established the association
2010
+ // before start(); a new INIT would reset it.
2011
+ if (!this.isServer && this.associationState === const_1.SCTP_STATE.CLOSED) {
1536
2012
  await this.init();
1537
2013
  }
1538
2014
  }
1539
2015
  }
2016
+ /** Waiting for INIT_ACK / COOKIE_ACK of our own INIT. */
2017
+ get isInitiating() {
2018
+ return (this.associationState === const_1.SCTP_STATE.COOKIE_WAIT ||
2019
+ this.associationState === const_1.SCTP_STATE.COOKIE_ECHOED);
2020
+ }
1540
2021
  async init() {
1541
2022
  const init = new chunk_1.InitChunk();
1542
2023
  init.initiateTag = this.localVerificationTag;
@@ -1546,14 +2027,17 @@ class SCTP {
1546
2027
  init.initialTsn = this.localTsn;
1547
2028
  this.setExtensions(init.params);
1548
2029
  log("send init", init);
2030
+ // # start T1 timer and enter COOKIE-WAIT state
2031
+ // Before handing INIT off, so an INIT_ACK that arrives while the send is
2032
+ // still pending is not dropped by the COOKIE_WAIT guard, and a failed
2033
+ // send is packet loss that T1 retransmits (or closes after max retries).
2034
+ this.timer1Start(init);
2035
+ this.setState(const_1.SCTP_STATE.COOKIE_WAIT);
1549
2036
  try {
1550
2037
  await this.sendChunk(init);
1551
- // # start T1 timer and enter COOKIE-WAIT state
1552
- this.timer1Start(init);
1553
- this.setState(const_1.SCTP_STATE.COOKIE_WAIT);
1554
2038
  }
1555
2039
  catch (error) {
1556
- log("send init failed", error.message);
2040
+ log("send init failed; T1 retransmits", error?.message);
1557
2041
  }
1558
2042
  }
1559
2043
  setExtensions(params) {
@@ -1571,7 +2055,10 @@ class SCTP {
1571
2055
  if (this.remotePort === undefined) {
1572
2056
  throw new Error("invalid remote port");
1573
2057
  }
1574
- const packet = (0, chunk_1.serializePacket)(this.localPort, this.remotePort, this.remoteVerificationTag, chunk);
2058
+ const packet = (0, chunk_1.serializePacket)(this.localPort, this.remotePort,
2059
+ // RFC 9260 §8.5.1: INIT is always sent with verification tag 0, also
2060
+ // when an INIT answered before start() taught us the peer's tag.
2061
+ chunk.type === chunk_1.InitChunk.type ? 0 : this.remoteVerificationTag, chunk);
1575
2062
  await this.transport.send(packet);
1576
2063
  }
1577
2064
  setState(state) {
@@ -1591,12 +2078,16 @@ class SCTP {
1591
2078
  this.timer3Cancel();
1592
2079
  this.timerReconfigCancel();
1593
2080
  this.heartbeatCancel();
2081
+ this.outboundExpiryCancel();
1594
2082
  if (this.sackTimeout) {
1595
2083
  clearTimeout(this.sackTimeout);
1596
2084
  this.sackTimeout = undefined;
1597
2085
  }
2086
+ // Drop a deferred incoming stream reset and the DATA held for it.
2087
+ this.deferredReset = undefined;
1598
2088
  this.setConnectionState("closed");
1599
2089
  this.removeAllListeners();
2090
+ this.releaseSendWaiters();
1600
2091
  }
1601
2092
  }
1602
2093
  setConnectionState(state) {
@@ -1622,6 +2113,7 @@ class SCTP {
1622
2113
  clearTimeout(this.timer1Handle);
1623
2114
  clearTimeout(this.timer2Handle);
1624
2115
  clearTimeout(this.timer3Handle);
2116
+ this.outboundExpiryCancel();
1625
2117
  clearTimeout(this.timerReconfigHandle);
1626
2118
  clearTimeout(this.timerHeartbeatHandle);
1627
2119
  clearTimeout(this.sackTimeout);
@@ -1697,8 +2189,8 @@ class InboundStream {
1697
2189
  break;
1698
2190
  }
1699
2191
  else {
2192
+ // The partial message is broken; this chunk may start the next one.
1700
2193
  startPos = undefined;
1701
- pos++;
1702
2194
  continue;
1703
2195
  }
1704
2196
  }
@@ -1720,8 +2212,12 @@ class InboundStream {
1720
2212
  if (ordered && chunk.streamSeqNum === this.streamSequenceNumber) {
1721
2213
  this.streamSequenceNumber = (0, common_1.uint16Add)(this.streamSequenceNumber, 1);
1722
2214
  }
2215
+ // The next message starts at the removed one's position and need not
2216
+ // be TSN-contiguous with it, so restart the search there.
1723
2217
  pos = startPos;
2218
+ startPos = undefined;
1724
2219
  yield [chunk.streamId, chunk.protocol, userData];
2220
+ continue;
1725
2221
  }
1726
2222
  else {
1727
2223
  pos++;
@@ -1729,6 +2225,67 @@ class InboundStream {
1729
2225
  expectedTsn = tsnPlusOne(expectedTsn);
1730
2226
  }
1731
2227
  }
2228
+ /**
2229
+ * Applies a FORWARD-TSN entry (RFC 3758 3.6): ordered messages with SSN up to
2230
+ * `ssn` are no longer waited for. Complete ones are returned for delivery in
2231
+ * order, incomplete ones are dropped, and the expected SSN moves past `ssn`
2232
+ * (never backwards, so a stale/duplicated entry is harmless).
2233
+ */
2234
+ forwardSequenceNumber(ssn) {
2235
+ const messages = [];
2236
+ let prunedSize = 0;
2237
+ let fragments = [];
2238
+ const dropFragments = () => {
2239
+ for (const c of fragments)
2240
+ prunedSize += c.userData.length;
2241
+ fragments = [];
2242
+ };
2243
+ this.reassembly = this.reassembly.filter((chunk) => {
2244
+ if (chunk.flags & SCTP_DATA_UNORDERED ||
2245
+ (0, common_1.uint16Gt)(chunk.streamSeqNum, ssn)) {
2246
+ return true;
2247
+ }
2248
+ const prev = fragments[fragments.length - 1];
2249
+ if (chunk.flags & SCTP_DATA_FIRST_FRAG) {
2250
+ dropFragments();
2251
+ }
2252
+ else if (!prev ||
2253
+ chunk.tsn !== tsnPlusOne(prev.tsn) ||
2254
+ chunk.streamSeqNum !== prev.streamSeqNum) {
2255
+ dropFragments();
2256
+ prunedSize += chunk.userData.length;
2257
+ return false;
2258
+ }
2259
+ fragments.push(chunk);
2260
+ if (chunk.flags & SCTP_DATA_LAST_FRAG) {
2261
+ messages.push([
2262
+ chunk.streamId,
2263
+ chunk.protocol,
2264
+ Buffer.concat(fragments.map((c) => c.userData)),
2265
+ ]);
2266
+ fragments = [];
2267
+ }
2268
+ return false;
2269
+ });
2270
+ dropFragments();
2271
+ const nextSsn = (0, common_1.uint16Add)(ssn, 1);
2272
+ if ((0, common_1.uint16Gt)(nextSsn, this.streamSequenceNumber)) {
2273
+ this.streamSequenceNumber = nextSsn;
2274
+ }
2275
+ return { messages, prunedSize };
2276
+ }
2277
+ /** Removes unordered chunks with TSN up to `tsn`. */
2278
+ pruneUnorderedChunks(tsn) {
2279
+ let size = 0;
2280
+ this.reassembly = this.reassembly.filter((chunk) => {
2281
+ if (chunk.flags & SCTP_DATA_UNORDERED && (0, common_1.uint32Gte)(tsn, chunk.tsn)) {
2282
+ size += chunk.userData.length;
2283
+ return false;
2284
+ }
2285
+ return true;
2286
+ });
2287
+ return size;
2288
+ }
1732
2289
  pruneChunks(tsn) {
1733
2290
  // """
1734
2291
  // Prune chunks up to the given TSN.
@@ -1754,13 +2311,22 @@ class RTCSctpCapabilities {
1754
2311
  enumerable: true,
1755
2312
  configurable: true,
1756
2313
  writable: true,
1757
- value: maxMessageSize
2314
+ value: void 0
1758
2315
  });
2316
+ this.maxMessageSize = maxMessageSize;
1759
2317
  }
1760
2318
  }
1761
2319
  exports.RTCSctpCapabilities = RTCSctpCapabilities;
1762
2320
  function tsnMinusOne(a) {
1763
- return (a - 1) % SCTP_TSN_MODULO;
2321
+ return (a - 1 + SCTP_TSN_MODULO) % SCTP_TSN_MODULO;
2322
+ }
2323
+ /** Serial-number distance from `base` to `tsn` (RFC 1982), 0..2^32-1. */
2324
+ function tsnDistance(base, tsn) {
2325
+ return (tsn - base + SCTP_TSN_MODULO) % SCTP_TSN_MODULO;
2326
+ }
2327
+ /** Sorts TSNs in serial-number order after `base`, so the order holds across a wrap. */
2328
+ function sortTsns(tsns, base) {
2329
+ return [...tsns].sort((a, b) => tsnDistance(base, a) - tsnDistance(base, b));
1764
2330
  }
1765
2331
  function tsnPlusOne(a) {
1766
2332
  return (a + 1) % SCTP_TSN_MODULO;