werift 0.27.0 → 0.29.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 (582) hide show
  1. package/README.md +299 -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 +51 -10
  9. package/lib/common/src/transport.js +35 -9
  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/carrier/direct.d.ts +67 -0
  15. package/lib/dtls/src/carrier/direct.js +226 -0
  16. package/lib/dtls/src/carrier/direct.js.map +1 -0
  17. package/lib/dtls/src/carrier/types.d.ts +81 -0
  18. package/lib/dtls/src/carrier/types.js +7 -0
  19. package/lib/dtls/src/carrier/types.js.map +1 -0
  20. package/lib/dtls/src/cipher/const.d.ts +6 -0
  21. package/lib/dtls/src/cipher/const.js +13 -2
  22. package/lib/dtls/src/cipher/const.js.map +1 -1
  23. package/lib/dtls/src/cipher/prf.js +11 -2
  24. package/lib/dtls/src/cipher/prf.js.map +1 -1
  25. package/lib/dtls/src/cipher/tls13/aead.d.ts +41 -0
  26. package/lib/dtls/src/cipher/tls13/aead.js +128 -0
  27. package/lib/dtls/src/cipher/tls13/aead.js.map +1 -0
  28. package/lib/dtls/src/cipher/tls13/hkdf.d.ts +30 -0
  29. package/lib/dtls/src/cipher/tls13/hkdf.js +76 -0
  30. package/lib/dtls/src/cipher/tls13/hkdf.js.map +1 -0
  31. package/lib/dtls/src/cipher/tls13/keySchedule.d.ts +70 -0
  32. package/lib/dtls/src/cipher/tls13/keySchedule.js +114 -0
  33. package/lib/dtls/src/cipher/tls13/keySchedule.js.map +1 -0
  34. package/lib/dtls/src/cipher/tls13/signature.d.ts +30 -0
  35. package/lib/dtls/src/cipher/tls13/signature.js +149 -0
  36. package/lib/dtls/src/cipher/tls13/signature.js.map +1 -0
  37. package/lib/dtls/src/client.d.ts +253 -0
  38. package/lib/dtls/src/client.js +1269 -14
  39. package/lib/dtls/src/client.js.map +1 -1
  40. package/lib/dtls/src/context/dtls.d.ts +47 -0
  41. package/lib/dtls/src/context/dtls.js +121 -0
  42. package/lib/dtls/src/context/dtls.js.map +1 -1
  43. package/lib/dtls/src/context/transport.d.ts +30 -1
  44. package/lib/dtls/src/context/transport.js +67 -3
  45. package/lib/dtls/src/context/transport.js.map +1 -1
  46. package/lib/dtls/src/engine/v1_3/connection-base.d.ts +520 -0
  47. package/lib/dtls/src/engine/v1_3/connection-base.js +1644 -0
  48. package/lib/dtls/src/engine/v1_3/connection-base.js.map +1 -0
  49. package/lib/dtls/src/engine/v1_3/connection.d.ts +181 -0
  50. package/lib/dtls/src/engine/v1_3/connection.js +680 -0
  51. package/lib/dtls/src/engine/v1_3/connection.js.map +1 -0
  52. package/lib/dtls/src/engine/v1_3/early-data-buffer.d.ts +38 -0
  53. package/lib/dtls/src/engine/v1_3/early-data-buffer.js +166 -0
  54. package/lib/dtls/src/engine/v1_3/early-data-buffer.js.map +1 -0
  55. package/lib/dtls/src/engine/v1_3/flight/certificate.d.ts +4 -0
  56. package/lib/dtls/src/engine/v1_3/flight/certificate.js +62 -0
  57. package/lib/dtls/src/engine/v1_3/flight/certificate.js.map +1 -0
  58. package/lib/dtls/src/engine/v1_3/flight/client/flight1.d.ts +8 -0
  59. package/lib/dtls/src/engine/v1_3/flight/client/flight1.js +119 -0
  60. package/lib/dtls/src/engine/v1_3/flight/client/flight1.js.map +1 -0
  61. package/lib/dtls/src/engine/v1_3/flight/client/flight4.d.ts +8 -0
  62. package/lib/dtls/src/engine/v1_3/flight/client/flight4.js +251 -0
  63. package/lib/dtls/src/engine/v1_3/flight/client/flight4.js.map +1 -0
  64. package/lib/dtls/src/engine/v1_3/flight/client/flight5.d.ts +5 -0
  65. package/lib/dtls/src/engine/v1_3/flight/client/flight5.js +99 -0
  66. package/lib/dtls/src/engine/v1_3/flight/client/flight5.js.map +1 -0
  67. package/lib/dtls/src/engine/v1_3/flight/dispatch.d.ts +12 -0
  68. package/lib/dtls/src/engine/v1_3/flight/dispatch.js +138 -0
  69. package/lib/dtls/src/engine/v1_3/flight/dispatch.js.map +1 -0
  70. package/lib/dtls/src/engine/v1_3/flight/extensions.d.ts +13 -0
  71. package/lib/dtls/src/engine/v1_3/flight/extensions.js +46 -0
  72. package/lib/dtls/src/engine/v1_3/flight/extensions.js.map +1 -0
  73. package/lib/dtls/src/engine/v1_3/flight/finished.d.ts +6 -0
  74. package/lib/dtls/src/engine/v1_3/flight/finished.js +16 -0
  75. package/lib/dtls/src/engine/v1_3/flight/finished.js.map +1 -0
  76. package/lib/dtls/src/engine/v1_3/flight/post-hs.d.ts +12 -0
  77. package/lib/dtls/src/engine/v1_3/flight/post-hs.js +94 -0
  78. package/lib/dtls/src/engine/v1_3/flight/post-hs.js.map +1 -0
  79. package/lib/dtls/src/engine/v1_3/flight/server/flight2.d.ts +21 -0
  80. package/lib/dtls/src/engine/v1_3/flight/server/flight2.js +227 -0
  81. package/lib/dtls/src/engine/v1_3/flight/server/flight2.js.map +1 -0
  82. package/lib/dtls/src/engine/v1_3/flight/server/flight4.d.ts +6 -0
  83. package/lib/dtls/src/engine/v1_3/flight/server/flight4.js +455 -0
  84. package/lib/dtls/src/engine/v1_3/flight/server/flight4.js.map +1 -0
  85. package/lib/dtls/src/engine/v1_3/flight/server/flight5.d.ts +5 -0
  86. package/lib/dtls/src/engine/v1_3/flight/server/flight5.js +48 -0
  87. package/lib/dtls/src/engine/v1_3/flight/server/flight5.js.map +1 -0
  88. package/lib/dtls/src/engine/v1_3/flight-tx.d.ts +119 -0
  89. package/lib/dtls/src/engine/v1_3/flight-tx.js +782 -0
  90. package/lib/dtls/src/engine/v1_3/flight-tx.js.map +1 -0
  91. package/lib/dtls/src/engine/v1_3/host.d.ts +86 -0
  92. package/lib/dtls/src/engine/v1_3/host.js +11 -0
  93. package/lib/dtls/src/engine/v1_3/host.js.map +1 -0
  94. package/lib/dtls/src/engine/v1_3/record-rx.d.ts +77 -0
  95. package/lib/dtls/src/engine/v1_3/record-rx.js +818 -0
  96. package/lib/dtls/src/engine/v1_3/record-rx.js.map +1 -0
  97. package/lib/dtls/src/engine/v1_3/transcript.d.ts +22 -0
  98. package/lib/dtls/src/engine/v1_3/transcript.js +65 -0
  99. package/lib/dtls/src/engine/v1_3/transcript.js.map +1 -0
  100. package/lib/dtls/src/engine/v1_3/types.d.ts +169 -0
  101. package/lib/dtls/src/engine/v1_3/types.js +113 -0
  102. package/lib/dtls/src/engine/v1_3/types.js.map +1 -0
  103. package/lib/dtls/src/flight/client/flight1.d.ts +7 -1
  104. package/lib/dtls/src/flight/client/flight1.js +9 -3
  105. package/lib/dtls/src/flight/client/flight1.js.map +1 -1
  106. package/lib/dtls/src/flight/client/flight3.d.ts +8 -0
  107. package/lib/dtls/src/flight/client/flight3.js +24 -4
  108. package/lib/dtls/src/flight/client/flight3.js.map +1 -1
  109. package/lib/dtls/src/flight/client/flight5.js +17 -2
  110. package/lib/dtls/src/flight/client/flight5.js.map +1 -1
  111. package/lib/dtls/src/flight/flight.d.ts +21 -0
  112. package/lib/dtls/src/flight/flight.js +119 -9
  113. package/lib/dtls/src/flight/flight.js.map +1 -1
  114. package/lib/dtls/src/flight/server/commitClientHello.d.ts +35 -0
  115. package/lib/dtls/src/flight/server/commitClientHello.js +149 -0
  116. package/lib/dtls/src/flight/server/commitClientHello.js.map +1 -0
  117. package/lib/dtls/src/flight/server/flight2.d.ts +12 -3
  118. package/lib/dtls/src/flight/server/flight2.js +29 -105
  119. package/lib/dtls/src/flight/server/flight2.js.map +1 -1
  120. package/lib/dtls/src/flight/server/flight4.js +14 -2
  121. package/lib/dtls/src/flight/server/flight4.js.map +1 -1
  122. package/lib/dtls/src/flight/server/flight6.js +12 -1
  123. package/lib/dtls/src/flight/server/flight6.js.map +1 -1
  124. package/lib/dtls/src/handshake/const.d.ts +10 -0
  125. package/lib/dtls/src/handshake/const.js +10 -0
  126. package/lib/dtls/src/handshake/const.js.map +1 -1
  127. package/lib/dtls/src/handshake/extensions/cookie.d.ts +113 -0
  128. package/lib/dtls/src/handshake/extensions/cookie.js +326 -0
  129. package/lib/dtls/src/handshake/extensions/cookie.js.map +1 -0
  130. package/lib/dtls/src/handshake/extensions/ids.d.ts +7 -0
  131. package/lib/dtls/src/handshake/extensions/ids.js +11 -0
  132. package/lib/dtls/src/handshake/extensions/ids.js.map +1 -0
  133. package/lib/dtls/src/handshake/extensions/keyShare.d.ts +28 -0
  134. package/lib/dtls/src/handshake/extensions/keyShare.js +156 -0
  135. package/lib/dtls/src/handshake/extensions/keyShare.js.map +1 -0
  136. package/lib/dtls/src/handshake/extensions/signatureAlgorithms.d.ts +27 -0
  137. package/lib/dtls/src/handshake/extensions/signatureAlgorithms.js +67 -0
  138. package/lib/dtls/src/handshake/extensions/signatureAlgorithms.js.map +1 -0
  139. package/lib/dtls/src/handshake/extensions/supportedVersions.d.ts +20 -0
  140. package/lib/dtls/src/handshake/extensions/supportedVersions.js +90 -0
  141. package/lib/dtls/src/handshake/extensions/supportedVersions.js.map +1 -0
  142. package/lib/dtls/src/handshake/extensions/unique.d.ts +7 -0
  143. package/lib/dtls/src/handshake/extensions/unique.js +19 -0
  144. package/lib/dtls/src/handshake/extensions/unique.js.map +1 -0
  145. package/lib/dtls/src/handshake/extensions/useSrtp.d.ts +26 -3
  146. package/lib/dtls/src/handshake/extensions/useSrtp.js +68 -12
  147. package/lib/dtls/src/handshake/extensions/useSrtp.js.map +1 -1
  148. package/lib/dtls/src/handshake/message/tls13/ack.d.ts +50 -0
  149. package/lib/dtls/src/handshake/message/tls13/ack.js +107 -0
  150. package/lib/dtls/src/handshake/message/tls13/ack.js.map +1 -0
  151. package/lib/dtls/src/handshake/message/tls13/certificate.d.ts +17 -0
  152. package/lib/dtls/src/handshake/message/tls13/certificate.js +112 -0
  153. package/lib/dtls/src/handshake/message/tls13/certificate.js.map +1 -0
  154. package/lib/dtls/src/handshake/message/tls13/certificateRequest.d.ts +17 -0
  155. package/lib/dtls/src/handshake/message/tls13/certificateRequest.js +99 -0
  156. package/lib/dtls/src/handshake/message/tls13/certificateRequest.js.map +1 -0
  157. package/lib/dtls/src/handshake/message/tls13/certificateVerify.d.ts +21 -0
  158. package/lib/dtls/src/handshake/message/tls13/certificateVerify.js +77 -0
  159. package/lib/dtls/src/handshake/message/tls13/certificateVerify.js.map +1 -0
  160. package/lib/dtls/src/handshake/message/tls13/encryptedExtensions.d.ts +19 -0
  161. package/lib/dtls/src/handshake/message/tls13/encryptedExtensions.js +56 -0
  162. package/lib/dtls/src/handshake/message/tls13/encryptedExtensions.js.map +1 -0
  163. package/lib/dtls/src/handshake/message/tls13/keyUpdate.d.ts +13 -0
  164. package/lib/dtls/src/handshake/message/tls13/keyUpdate.js +54 -0
  165. package/lib/dtls/src/handshake/message/tls13/keyUpdate.js.map +1 -0
  166. package/lib/dtls/src/handshake/message/tls13/newSessionTicket.d.ts +33 -0
  167. package/lib/dtls/src/handshake/message/tls13/newSessionTicket.js +158 -0
  168. package/lib/dtls/src/handshake/message/tls13/newSessionTicket.js.map +1 -0
  169. package/lib/dtls/src/handshake/random.d.ts +14 -0
  170. package/lib/dtls/src/handshake/random.js +24 -0
  171. package/lib/dtls/src/handshake/random.js.map +1 -1
  172. package/lib/dtls/src/handshake/validation.d.ts +26 -0
  173. package/lib/dtls/src/handshake/validation.js +32 -0
  174. package/lib/dtls/src/handshake/validation.js.map +1 -0
  175. package/lib/dtls/src/index.d.ts +8 -1
  176. package/lib/dtls/src/index.js +125 -44
  177. package/lib/dtls/src/index.js.map +1 -1
  178. package/lib/dtls/src/internal.d.ts +7 -0
  179. package/lib/dtls/src/internal.js +57 -0
  180. package/lib/dtls/src/internal.js.map +1 -0
  181. package/lib/dtls/src/peer.d.ts +49 -0
  182. package/lib/dtls/src/peer.js +54 -0
  183. package/lib/dtls/src/peer.js.map +1 -0
  184. package/lib/dtls/src/record/antiReplayWindow.d.ts +33 -0
  185. package/lib/dtls/src/record/antiReplayWindow.js +119 -0
  186. package/lib/dtls/src/record/antiReplayWindow.js.map +1 -0
  187. package/lib/dtls/src/record/const.d.ts +15 -0
  188. package/lib/dtls/src/record/const.js +15 -1
  189. package/lib/dtls/src/record/const.js.map +1 -1
  190. package/lib/dtls/src/record/message/fragment.d.ts +0 -2
  191. package/lib/dtls/src/record/message/fragment.js +36 -17
  192. package/lib/dtls/src/record/message/fragment.js.map +1 -1
  193. package/lib/dtls/src/record/message/reassembler.d.ts +62 -0
  194. package/lib/dtls/src/record/message/reassembler.js +249 -0
  195. package/lib/dtls/src/record/message/reassembler.js.map +1 -0
  196. package/lib/dtls/src/record/receive.d.ts +6 -0
  197. package/lib/dtls/src/record/receive.js +58 -19
  198. package/lib/dtls/src/record/receive.js.map +1 -1
  199. package/lib/dtls/src/record/v1_3/header.d.ts +34 -0
  200. package/lib/dtls/src/record/v1_3/header.js +95 -0
  201. package/lib/dtls/src/record/v1_3/header.js.map +1 -0
  202. package/lib/dtls/src/record/v1_3/record.d.ts +133 -0
  203. package/lib/dtls/src/record/v1_3/record.js +370 -0
  204. package/lib/dtls/src/record/v1_3/record.js.map +1 -0
  205. package/lib/dtls/src/retransmission.d.ts +26 -0
  206. package/lib/dtls/src/retransmission.js +39 -0
  207. package/lib/dtls/src/retransmission.js.map +1 -0
  208. package/lib/dtls/src/server.d.ts +38 -0
  209. package/lib/dtls/src/server.js +382 -17
  210. package/lib/dtls/src/server.js.map +1 -1
  211. package/lib/dtls/src/socket.d.ts +487 -11
  212. package/lib/dtls/src/socket.js +1359 -70
  213. package/lib/dtls/src/socket.js.map +1 -1
  214. package/lib/dtls/src/version.d.ts +89 -0
  215. package/lib/dtls/src/version.js +204 -0
  216. package/lib/dtls/src/version.js.map +1 -0
  217. package/lib/ice/src/candidateError.d.ts +155 -0
  218. package/lib/ice/src/candidateError.js +246 -0
  219. package/lib/ice/src/candidateError.js.map +1 -0
  220. package/lib/ice/src/candidateGatherer.d.ts +127 -0
  221. package/lib/ice/src/candidateGatherer.js +745 -0
  222. package/lib/ice/src/candidateGatherer.js.map +1 -0
  223. package/lib/ice/src/consent.d.ts +126 -0
  224. package/lib/ice/src/consent.js +470 -0
  225. package/lib/ice/src/consent.js.map +1 -0
  226. package/lib/ice/src/dns/lookup.d.ts +10 -0
  227. package/lib/ice/src/dns/lookup.js +130 -3
  228. package/lib/ice/src/dns/lookup.js.map +1 -1
  229. package/lib/ice/src/exceptions.d.ts +9 -0
  230. package/lib/ice/src/exceptions.js +14 -1
  231. package/lib/ice/src/exceptions.js.map +1 -1
  232. package/lib/ice/src/failover.d.ts +270 -0
  233. package/lib/ice/src/failover.js +674 -0
  234. package/lib/ice/src/failover.js.map +1 -0
  235. package/lib/ice/src/gatheringError.d.ts +28 -0
  236. package/lib/ice/src/gatheringError.js +17 -0
  237. package/lib/ice/src/gatheringError.js.map +1 -0
  238. package/lib/ice/src/ice.d.ts +486 -78
  239. package/lib/ice/src/ice.js +2415 -1129
  240. package/lib/ice/src/ice.js.map +1 -1
  241. package/lib/ice/src/iceBase.d.ts +404 -14
  242. package/lib/ice/src/iceBase.js +251 -38
  243. package/lib/ice/src/iceBase.js.map +1 -1
  244. package/lib/ice/src/index.d.ts +3 -0
  245. package/lib/ice/src/index.js +5 -1
  246. package/lib/ice/src/index.js.map +1 -1
  247. package/lib/ice/src/internal/checkMessages.d.ts +41 -0
  248. package/lib/ice/src/internal/checkMessages.js +63 -0
  249. package/lib/ice/src/internal/checkMessages.js.map +1 -0
  250. package/lib/ice/src/internal/datagram.d.ts +37 -0
  251. package/lib/ice/src/internal/datagram.js +45 -0
  252. package/lib/ice/src/internal/datagram.js.map +1 -0
  253. package/lib/ice/src/internal/defaultRoute.d.ts +20 -0
  254. package/lib/ice/src/internal/defaultRoute.js +134 -0
  255. package/lib/ice/src/internal/defaultRoute.js.map +1 -0
  256. package/lib/ice/src/internal/hostInterfaces.d.ts +28 -0
  257. package/lib/ice/src/internal/hostInterfaces.js +89 -0
  258. package/lib/ice/src/internal/hostInterfaces.js.map +1 -0
  259. package/lib/ice/src/internal/hostPreference.d.ts +24 -0
  260. package/lib/ice/src/internal/hostPreference.js +38 -0
  261. package/lib/ice/src/internal/hostPreference.js.map +1 -0
  262. package/lib/ice/src/internal/resolveHostAddresses.d.ts +56 -8
  263. package/lib/ice/src/internal/resolveHostAddresses.js +127 -24
  264. package/lib/ice/src/internal/resolveHostAddresses.js.map +1 -1
  265. package/lib/ice/src/internal/selectAddresses.d.ts +27 -0
  266. package/lib/ice/src/internal/selectAddresses.js +37 -18
  267. package/lib/ice/src/internal/selectAddresses.js.map +1 -1
  268. package/lib/ice/src/internal/sped-bind.d.ts +44 -0
  269. package/lib/ice/src/internal/sped-bind.js +53 -0
  270. package/lib/ice/src/internal/sped-bind.js.map +1 -0
  271. package/lib/ice/src/internal/sped-binding-cache.d.ts +27 -0
  272. package/lib/ice/src/internal/sped-binding-cache.js +102 -0
  273. package/lib/ice/src/internal/sped-binding-cache.js.map +1 -0
  274. package/lib/ice/src/internal/sped.d.ts +15 -0
  275. package/lib/ice/src/internal/sped.js +62 -0
  276. package/lib/ice/src/internal/sped.js.map +1 -0
  277. package/lib/ice/src/nomination/regularNomination.d.ts +200 -0
  278. package/lib/ice/src/nomination/regularNomination.js +311 -0
  279. package/lib/ice/src/nomination/regularNomination.js.map +1 -0
  280. package/lib/ice/src/nomination/selectionController.d.ts +111 -0
  281. package/lib/ice/src/nomination/selectionController.js +172 -0
  282. package/lib/ice/src/nomination/selectionController.js.map +1 -0
  283. package/lib/ice/src/nomination/selectionReplacement.d.ts +178 -0
  284. package/lib/ice/src/nomination/selectionReplacement.js +429 -0
  285. package/lib/ice/src/nomination/selectionReplacement.js.map +1 -0
  286. package/lib/ice/src/provisional.d.ts +139 -0
  287. package/lib/ice/src/provisional.js +468 -0
  288. package/lib/ice/src/provisional.js.map +1 -0
  289. package/lib/ice/src/sped/draft00/codec.d.ts +20 -0
  290. package/lib/ice/src/sped/draft00/codec.js +59 -0
  291. package/lib/ice/src/sped/draft00/codec.js.map +1 -0
  292. package/lib/ice/src/sped/draft00/constants.d.ts +14 -0
  293. package/lib/ice/src/sped/draft00/constants.js +18 -0
  294. package/lib/ice/src/sped/draft00/constants.js.map +1 -0
  295. package/lib/ice/src/sped/draft00/index.d.ts +6 -0
  296. package/lib/ice/src/sped/draft00/index.js +26 -0
  297. package/lib/ice/src/sped/draft00/index.js.map +1 -0
  298. package/lib/ice/src/sped/draft00/mtu.d.ts +38 -0
  299. package/lib/ice/src/sped/draft00/mtu.js +96 -0
  300. package/lib/ice/src/sped/draft00/mtu.js.map +1 -0
  301. package/lib/ice/src/sped/draft00/session.d.ts +86 -0
  302. package/lib/ice/src/sped/draft00/session.js +353 -0
  303. package/lib/ice/src/sped/draft00/session.js.map +1 -0
  304. package/lib/ice/src/sped/draft00/types.d.ts +16 -0
  305. package/lib/ice/src/sped/draft00/types.js +3 -0
  306. package/lib/ice/src/sped/draft00/types.js.map +1 -0
  307. package/lib/ice/src/sped/runtime.d.ts +188 -0
  308. package/lib/ice/src/sped/runtime.js +643 -0
  309. package/lib/ice/src/sped/runtime.js.map +1 -0
  310. package/lib/ice/src/stun/protocol.d.ts +38 -2
  311. package/lib/ice/src/stun/protocol.js +107 -21
  312. package/lib/ice/src/stun/protocol.js.map +1 -1
  313. package/lib/ice/src/stun/rawAttributeValue.d.ts +1 -0
  314. package/lib/ice/src/stun/rawAttributeValue.js +6 -0
  315. package/lib/ice/src/stun/rawAttributeValue.js.map +1 -0
  316. package/lib/ice/src/stun/tcpProtocol.d.ts +2 -1
  317. package/lib/ice/src/stun/tcpProtocol.js +21 -9
  318. package/lib/ice/src/stun/tcpProtocol.js.map +1 -1
  319. package/lib/ice/src/stun/transaction.d.ts +4 -0
  320. package/lib/ice/src/stun/transaction.js +27 -2
  321. package/lib/ice/src/stun/transaction.js.map +1 -1
  322. package/lib/ice/src/turn/protocol.d.ts +23 -4
  323. package/lib/ice/src/turn/protocol.js +99 -11
  324. package/lib/ice/src/turn/protocol.js.map +1 -1
  325. package/lib/ice/src/types/model.d.ts +25 -5
  326. package/lib/ice/src/types/model.js.map +1 -1
  327. package/lib/ice/src/utils.d.ts +13 -0
  328. package/lib/ice/src/utils.js +13 -0
  329. package/lib/ice/src/utils.js.map +1 -1
  330. package/lib/ice-server/src/stun/attributes.js +4 -0
  331. package/lib/ice-server/src/stun/attributes.js.map +1 -1
  332. package/lib/ice-server/src/stun/const.d.ts +20 -1
  333. package/lib/ice-server/src/stun/const.js +9 -1
  334. package/lib/ice-server/src/stun/const.js.map +1 -1
  335. package/lib/ice-server/src/stun/message.d.ts +43 -3
  336. package/lib/ice-server/src/stun/message.js +258 -40
  337. package/lib/ice-server/src/stun/message.js.map +1 -1
  338. package/lib/ice-server/src/stun/rawAttributeValue.d.ts +6 -0
  339. package/lib/ice-server/src/stun/rawAttributeValue.js +12 -0
  340. package/lib/ice-server/src/stun/rawAttributeValue.js.map +1 -0
  341. package/lib/index.mjs +47427 -25303
  342. package/lib/nonstandard/index.mjs +3076 -1318
  343. package/lib/polyfill/dom.mjs +40430 -18730
  344. package/lib/polyfill/index.mjs +40333 -18639
  345. package/lib/rtp/src/extra/processor/fsRetry.d.ts +20 -0
  346. package/lib/rtp/src/extra/processor/fsRetry.js +73 -0
  347. package/lib/rtp/src/extra/processor/fsRetry.js.map +1 -0
  348. package/lib/rtp/src/extra/processor/webm.d.ts +6 -0
  349. package/lib/rtp/src/extra/processor/webm.js +6 -2
  350. package/lib/rtp/src/extra/processor/webm.js.map +1 -1
  351. package/lib/rtp/src/extra/processor/webmCallback.d.ts +11 -0
  352. package/lib/rtp/src/extra/processor/webmCallback.js +23 -31
  353. package/lib/rtp/src/extra/processor/webmCallback.js.map +1 -1
  354. package/lib/rtp/src/extra/processor/webmFileWriter.d.ts +24 -0
  355. package/lib/rtp/src/extra/processor/webmFileWriter.js +94 -0
  356. package/lib/rtp/src/extra/processor/webmFileWriter.js.map +1 -0
  357. package/lib/rtp/src/index.d.ts +1 -0
  358. package/lib/rtp/src/index.js +1 -0
  359. package/lib/rtp/src/index.js.map +1 -1
  360. package/lib/rtp/src/rtp/continuity.d.ts +14 -1
  361. package/lib/rtp/src/rtp/continuity.js +24 -4
  362. package/lib/rtp/src/rtp/continuity.js.map +1 -1
  363. package/lib/rtp/src/rtp/timelineRewriter.d.ts +178 -0
  364. package/lib/rtp/src/rtp/timelineRewriter.js +417 -0
  365. package/lib/rtp/src/rtp/timelineRewriter.js.map +1 -0
  366. package/lib/sctp/src/chunk.d.ts +5 -0
  367. package/lib/sctp/src/chunk.js +10 -0
  368. package/lib/sctp/src/chunk.js.map +1 -1
  369. package/lib/sctp/src/index.d.ts +1 -1
  370. package/lib/sctp/src/index.js +4 -1
  371. package/lib/sctp/src/index.js.map +1 -1
  372. package/lib/sctp/src/sctp.d.ts +86 -5
  373. package/lib/sctp/src/sctp.js +354 -75
  374. package/lib/sctp/src/sctp.js.map +1 -1
  375. package/lib/webrtc/src/api/compatibilityOptions.d.ts +43 -0
  376. package/lib/webrtc/src/api/compatibilityOptions.js +43 -0
  377. package/lib/webrtc/src/api/compatibilityOptions.js.map +1 -0
  378. package/lib/webrtc/src/api/peerConfig.d.ts +547 -36
  379. package/lib/webrtc/src/api/peerConfig.js +187 -26
  380. package/lib/webrtc/src/api/peerConfig.js.map +1 -1
  381. package/lib/webrtc/src/api/peerConnectionEvents.d.ts +87 -2
  382. package/lib/webrtc/src/api/peerConnectionEvents.js +105 -1
  383. package/lib/webrtc/src/api/peerConnectionEvents.js.map +1 -1
  384. package/lib/webrtc/src/dataChannel.d.ts +122 -10
  385. package/lib/webrtc/src/dataChannel.js +214 -20
  386. package/lib/webrtc/src/dataChannel.js.map +1 -1
  387. package/lib/webrtc/src/helper.d.ts +8 -0
  388. package/lib/webrtc/src/helper.js +40 -4
  389. package/lib/webrtc/src/helper.js.map +1 -1
  390. package/lib/webrtc/src/media/codecCompatibility.d.ts +87 -1
  391. package/lib/webrtc/src/media/codecCompatibility.js +338 -12
  392. package/lib/webrtc/src/media/codecCompatibility.js.map +1 -1
  393. package/lib/webrtc/src/media/domEventTarget.d.ts +55 -0
  394. package/lib/webrtc/src/media/domEventTarget.js +216 -0
  395. package/lib/webrtc/src/media/domEventTarget.js.map +1 -0
  396. package/lib/webrtc/src/media/index.d.ts +2 -0
  397. package/lib/webrtc/src/media/index.js +1 -0
  398. package/lib/webrtc/src/media/index.js.map +1 -1
  399. package/lib/webrtc/src/media/parameters.d.ts +5 -0
  400. package/lib/webrtc/src/media/parameters.js +10 -0
  401. package/lib/webrtc/src/media/parameters.js.map +1 -1
  402. package/lib/webrtc/src/media/receiver/nack.d.ts +43 -10
  403. package/lib/webrtc/src/media/receiver/nack.js +245 -88
  404. package/lib/webrtc/src/media/receiver/nack.js.map +1 -1
  405. package/lib/webrtc/src/media/receiver/receiverTwcc.d.ts +10 -0
  406. package/lib/webrtc/src/media/receiver/receiverTwcc.js +4 -10
  407. package/lib/webrtc/src/media/receiver/receiverTwcc.js.map +1 -1
  408. package/lib/webrtc/src/media/router.d.ts +245 -51
  409. package/lib/webrtc/src/media/router.js +702 -237
  410. package/lib/webrtc/src/media/router.js.map +1 -1
  411. package/lib/webrtc/src/media/routing/pendingRoutes.d.ts +26 -0
  412. package/lib/webrtc/src/media/routing/pendingRoutes.js +48 -0
  413. package/lib/webrtc/src/media/routing/pendingRoutes.js.map +1 -0
  414. package/lib/webrtc/src/media/routing/ridRouting.d.ts +37 -0
  415. package/lib/webrtc/src/media/routing/ridRouting.js +74 -0
  416. package/lib/webrtc/src/media/routing/ridRouting.js.map +1 -0
  417. package/lib/webrtc/src/media/routing/routeTable.d.ts +60 -0
  418. package/lib/webrtc/src/media/routing/routeTable.js +95 -0
  419. package/lib/webrtc/src/media/routing/routeTable.js.map +1 -0
  420. package/lib/webrtc/src/media/routing/rtcpRouting.d.ts +34 -0
  421. package/lib/webrtc/src/media/routing/rtcpRouting.js +165 -0
  422. package/lib/webrtc/src/media/routing/rtcpRouting.js.map +1 -0
  423. package/lib/webrtc/src/media/routing/unknownSsrc.d.ts +55 -0
  424. package/lib/webrtc/src/media/routing/unknownSsrc.js +117 -0
  425. package/lib/webrtc/src/media/routing/unknownSsrc.js.map +1 -0
  426. package/lib/webrtc/src/media/rtpReceiver.d.ts +277 -5
  427. package/lib/webrtc/src/media/rtpReceiver.js +826 -36
  428. package/lib/webrtc/src/media/rtpReceiver.js.map +1 -1
  429. package/lib/webrtc/src/media/rtpSender.d.ts +8 -1
  430. package/lib/webrtc/src/media/rtpSender.js +13 -6
  431. package/lib/webrtc/src/media/rtpSender.js.map +1 -1
  432. package/lib/webrtc/src/media/rtpTransceiver.d.ts +47 -11
  433. package/lib/webrtc/src/media/rtpTransceiver.js +95 -11
  434. package/lib/webrtc/src/media/rtpTransceiver.js.map +1 -1
  435. package/lib/webrtc/src/media/sender/senderReportHistory.d.ts +51 -0
  436. package/lib/webrtc/src/media/sender/senderReportHistory.js +111 -0
  437. package/lib/webrtc/src/media/sender/senderReportHistory.js.map +1 -0
  438. package/lib/webrtc/src/media/sender/senderStats.d.ts +6 -5
  439. package/lib/webrtc/src/media/sender/senderStats.js +14 -19
  440. package/lib/webrtc/src/media/sender/senderStats.js.map +1 -1
  441. package/lib/webrtc/src/media/stats.d.ts +13 -0
  442. package/lib/webrtc/src/media/stats.js.map +1 -1
  443. package/lib/webrtc/src/media/track.d.ts +112 -11
  444. package/lib/webrtc/src/media/track.js +250 -19
  445. package/lib/webrtc/src/media/track.js.map +1 -1
  446. package/lib/webrtc/src/media/trackConstraints.d.ts +80 -0
  447. package/lib/webrtc/src/media/trackConstraints.js +141 -0
  448. package/lib/webrtc/src/media/trackConstraints.js.map +1 -0
  449. package/lib/webrtc/src/negotiation/internalState.d.ts +0 -4
  450. package/lib/webrtc/src/negotiation/internalState.js +0 -11
  451. package/lib/webrtc/src/negotiation/internalState.js.map +1 -1
  452. package/lib/webrtc/src/negotiation/remoteCandidates.d.ts +20 -1
  453. package/lib/webrtc/src/negotiation/remoteCandidates.js +29 -1
  454. package/lib/webrtc/src/negotiation/remoteCandidates.js.map +1 -1
  455. package/lib/webrtc/src/negotiation/remoteMediaApplication.d.ts +23 -6
  456. package/lib/webrtc/src/negotiation/remoteMediaApplication.js +46 -22
  457. package/lib/webrtc/src/negotiation/remoteMediaApplication.js.map +1 -1
  458. package/lib/webrtc/src/negotiation/transceiverPairing.d.ts +1 -0
  459. package/lib/webrtc/src/negotiation/transceiverPairing.js +62 -0
  460. package/lib/webrtc/src/negotiation/transceiverPairing.js.map +1 -0
  461. package/lib/webrtc/src/negotiation/transportActivation.d.ts +12 -1
  462. package/lib/webrtc/src/negotiation/transportActivation.js +133 -11
  463. package/lib/webrtc/src/negotiation/transportActivation.js.map +1 -1
  464. package/lib/webrtc/src/negotiationTransaction.d.ts +57 -5
  465. package/lib/webrtc/src/negotiationTransaction.js +134 -10
  466. package/lib/webrtc/src/negotiationTransaction.js.map +1 -1
  467. package/lib/webrtc/src/nonstandard/dummyMedia.d.ts +6 -0
  468. package/lib/webrtc/src/nonstandard/dummyMedia.js +14 -19
  469. package/lib/webrtc/src/nonstandard/dummyMedia.js.map +1 -1
  470. package/lib/webrtc/src/nonstandard/navigator.d.ts +1 -1
  471. package/lib/webrtc/src/nonstandard/recorder/index.js +5 -1
  472. package/lib/webrtc/src/nonstandard/recorder/index.js.map +1 -1
  473. package/lib/webrtc/src/nonstandard/recorder/writer/webm.d.ts +6 -1
  474. package/lib/webrtc/src/nonstandard/recorder/writer/webm.js +31 -2
  475. package/lib/webrtc/src/nonstandard/recorder/writer/webm.js.map +1 -1
  476. package/lib/webrtc/src/nonstandard/userMedia/packetizer.d.ts +16 -0
  477. package/lib/webrtc/src/nonstandard/userMedia/packetizer.js +25 -0
  478. package/lib/webrtc/src/nonstandard/userMedia/packetizer.js.map +1 -1
  479. package/lib/webrtc/src/nonstandard/userMedia.js +6 -13
  480. package/lib/webrtc/src/nonstandard/userMedia.js.map +1 -1
  481. package/lib/webrtc/src/peerConnection.d.ts +106 -11
  482. package/lib/webrtc/src/peerConnection.js +370 -36
  483. package/lib/webrtc/src/peerConnection.js.map +1 -1
  484. package/lib/webrtc/src/polyfill/api.d.ts +2 -1
  485. package/lib/webrtc/src/polyfill/api.js +3 -3
  486. package/lib/webrtc/src/polyfill/api.js.map +1 -1
  487. package/lib/webrtc/src/polyfill/dom.d.ts +21 -7
  488. package/lib/webrtc/src/polyfill/dom.js +15 -0
  489. package/lib/webrtc/src/polyfill/dom.js.map +1 -1
  490. package/lib/webrtc/src/polyfill/globals.d.ts +8 -3
  491. package/lib/webrtc/src/polyfill/globals.js.map +1 -1
  492. package/lib/webrtc/src/polyfill/index.d.ts +1 -1
  493. package/lib/webrtc/src/polyfill/index.js +1 -1
  494. package/lib/webrtc/src/polyfill/index.js.map +1 -1
  495. package/lib/webrtc/src/polyfill/install.d.ts +5 -1
  496. package/lib/webrtc/src/polyfill/install.js +4 -2
  497. package/lib/webrtc/src/polyfill/install.js.map +1 -1
  498. package/lib/webrtc/src/polyfill/mediaDevices.js +28 -4
  499. package/lib/webrtc/src/polyfill/mediaDevices.js.map +1 -1
  500. package/lib/webrtc/src/polyfill/peerConnectionConfig.d.ts +6 -0
  501. package/lib/webrtc/src/polyfill/peerConnectionConfig.js +15 -4
  502. package/lib/webrtc/src/polyfill/peerConnectionConfig.js.map +1 -1
  503. package/lib/webrtc/src/polyfill/selectSettings.d.ts +4 -14
  504. package/lib/webrtc/src/polyfill/selectSettings.js +10 -106
  505. package/lib/webrtc/src/polyfill/selectSettings.js.map +1 -1
  506. package/lib/webrtc/src/polyfill/sourceIo.d.ts +1 -2
  507. package/lib/webrtc/src/polyfill/sourceIo.js +6 -6
  508. package/lib/webrtc/src/polyfill/sourceIo.js.map +1 -1
  509. package/lib/webrtc/src/sctpManager.d.ts +31 -18
  510. package/lib/webrtc/src/sctpManager.js +162 -38
  511. package/lib/webrtc/src/sctpManager.js.map +1 -1
  512. package/lib/webrtc/src/sdp.d.ts +107 -2
  513. package/lib/webrtc/src/sdp.js +223 -17
  514. package/lib/webrtc/src/sdp.js.map +1 -1
  515. package/lib/webrtc/src/sdpManager.d.ts +101 -10
  516. package/lib/webrtc/src/sdpManager.js +236 -14
  517. package/lib/webrtc/src/sdpManager.js.map +1 -1
  518. package/lib/webrtc/src/secureTransportManager.d.ts +49 -3
  519. package/lib/webrtc/src/secureTransportManager.js +166 -12
  520. package/lib/webrtc/src/secureTransportManager.js.map +1 -1
  521. package/lib/webrtc/src/transceiverManager.d.ts +87 -6
  522. package/lib/webrtc/src/transceiverManager.js +272 -51
  523. package/lib/webrtc/src/transceiverManager.js.map +1 -1
  524. package/lib/webrtc/src/transport/dtls-application-gate.d.ts +1 -0
  525. package/lib/webrtc/src/transport/dtls-application-gate.js +143 -0
  526. package/lib/webrtc/src/transport/dtls-application-gate.js.map +1 -0
  527. package/lib/webrtc/src/transport/dtls-attempt.d.ts +1 -0
  528. package/lib/webrtc/src/transport/dtls-attempt.js +79 -0
  529. package/lib/webrtc/src/transport/dtls-attempt.js.map +1 -0
  530. package/lib/webrtc/src/transport/dtls-certificate.d.ts +24 -0
  531. package/lib/webrtc/src/transport/dtls-certificate.js +98 -0
  532. package/lib/webrtc/src/transport/dtls-certificate.js.map +1 -0
  533. package/lib/webrtc/src/transport/dtls-fingerprint.d.ts +7 -0
  534. package/lib/webrtc/src/transport/dtls-fingerprint.js +66 -0
  535. package/lib/webrtc/src/transport/dtls-fingerprint.js.map +1 -0
  536. package/lib/webrtc/src/transport/dtls-ice-transport.d.ts +1 -0
  537. package/lib/webrtc/src/transport/dtls-ice-transport.js +123 -0
  538. package/lib/webrtc/src/transport/dtls-ice-transport.js.map +1 -0
  539. package/lib/webrtc/src/transport/dtls-media-queue.d.ts +1 -0
  540. package/lib/webrtc/src/transport/dtls-media-queue.js +95 -0
  541. package/lib/webrtc/src/transport/dtls-media-queue.js.map +1 -0
  542. package/lib/webrtc/src/transport/dtls-readiness.d.ts +1 -0
  543. package/lib/webrtc/src/transport/dtls-readiness.js +173 -0
  544. package/lib/webrtc/src/transport/dtls-readiness.js.map +1 -0
  545. package/lib/webrtc/src/transport/dtls-sped-handshake.d.ts +1 -0
  546. package/lib/webrtc/src/transport/dtls-sped-handshake.js +146 -0
  547. package/lib/webrtc/src/transport/dtls-sped-handshake.js.map +1 -0
  548. package/lib/webrtc/src/transport/dtls-srtp.d.ts +1 -0
  549. package/lib/webrtc/src/transport/dtls-srtp.js +133 -0
  550. package/lib/webrtc/src/transport/dtls-srtp.js.map +1 -0
  551. package/lib/webrtc/src/transport/dtls-stats.d.ts +1 -0
  552. package/lib/webrtc/src/transport/dtls-stats.js +134 -0
  553. package/lib/webrtc/src/transport/dtls-stats.js.map +1 -0
  554. package/lib/webrtc/src/transport/dtls-types.d.ts +23 -0
  555. package/lib/webrtc/src/transport/dtls-types.js +11 -0
  556. package/lib/webrtc/src/transport/dtls-types.js.map +1 -0
  557. package/lib/webrtc/src/transport/dtls.d.ts +236 -47
  558. package/lib/webrtc/src/transport/dtls.js +1198 -441
  559. package/lib/webrtc/src/transport/dtls.js.map +1 -1
  560. package/lib/webrtc/src/transport/ice.d.ts +95 -5
  561. package/lib/webrtc/src/transport/ice.js +323 -21
  562. package/lib/webrtc/src/transport/ice.js.map +1 -1
  563. package/lib/webrtc/src/transport/iceCandidateAttribute.d.ts +4 -2
  564. package/lib/webrtc/src/transport/iceCandidateAttribute.js.map +1 -1
  565. package/lib/webrtc/src/transport/sctp.d.ts +149 -2
  566. package/lib/webrtc/src/transport/sctp.js +826 -128
  567. package/lib/webrtc/src/transport/sctp.js.map +1 -1
  568. package/lib/webrtc/src/transport/sped.d.ts +139 -0
  569. package/lib/webrtc/src/transport/sped.js +757 -0
  570. package/lib/webrtc/src/transport/sped.js.map +1 -0
  571. package/lib/webrtc/src/types/util.d.ts +6 -0
  572. package/lib/webrtc/src/types/util.js.map +1 -1
  573. package/lib/webrtc/src/utils.d.ts +34 -1
  574. package/lib/webrtc/src/utils.js +52 -3
  575. package/lib/webrtc/src/utils.js.map +1 -1
  576. package/lib/webrtc/src/webidl.d.ts +15 -0
  577. package/lib/webrtc/src/webidl.js +34 -0
  578. package/lib/webrtc/src/webidl.js.map +1 -0
  579. package/package.json +8 -3
  580. package/lib/webrtc/src/polyfill/rtcSessionDescription.d.ts +0 -18
  581. package/lib/webrtc/src/polyfill/rtcSessionDescription.js +0 -36
  582. package/lib/webrtc/src/polyfill/rtcSessionDescription.js.map +0 -1
@@ -1,13 +1,28 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RTCSctpCapabilities = exports.RTCSctpTransport = exports.DEFAULT_MAX_MESSAGE_SIZE = void 0;
3
+ exports.RTCSctpCapabilities = exports.RTCSctpTransport = exports.PASSIVE_INIT_FALLBACK_MS = exports.DEFAULT_MAX_MESSAGE_SIZE = void 0;
4
4
  const crypto_1 = require("crypto");
5
5
  const src_1 = require("../../../sctp/src");
6
+ const sctp_1 = require("../../../sctp/src/sctp");
6
7
  const const_1 = require("../const");
7
8
  const dataChannel_1 = require("../dataChannel");
9
+ const errors_1 = require("../errors");
10
+ const helper_1 = require("../helper");
8
11
  const common_1 = require("../imports/common");
9
12
  const log = (0, common_1.debug)("werift:packages/webrtc/src/transport/sctp.ts");
10
13
  exports.DEFAULT_MAX_MESSAGE_SIZE = 65536;
14
+ /**
15
+ * How long the DTLS client (passive SCTP side) waits for the peer's INIT
16
+ * after DTLS authentication before sending INIT itself. A peer that picks
17
+ * the SCTP initiator by the ICE role (older werift) may wait as well; well
18
+ * below the SCTP initial RTO (3 s), so a lost INIT also recovers sooner.
19
+ */
20
+ exports.PASSIVE_INIT_FALLBACK_MS = 1000;
21
+ /**
22
+ * Stream IDs a data channel may use: 0..65534 (W3C webrtc-pc, RFC 8831 section 6.5).
23
+ * @see tests/issue/815.test.ts "a reservation beyond the negotiated stream limit is given back and fails"
24
+ */
25
+ const DATA_CHANNEL_ID_LIMIT = 65535;
11
26
  class RTCSctpTransport {
12
27
  constructor(port = 5000, maxMessageSize = exports.DEFAULT_MAX_MESSAGE_SIZE, sctpOptions = {}) {
13
28
  Object.defineProperty(this, "port", {
@@ -52,6 +67,37 @@ class RTCSctpTransport {
52
67
  writable: true,
53
68
  value: (0, crypto_1.randomUUID)().toString()
54
69
  });
70
+ /**
71
+ * `connecting` until the association is established, then `connected`;
72
+ * `closed` once the association closed (the peer ended it, or the DTLS
73
+ * transport closed / failed). {@link RTCSctpTransport.stop} (e.g.
74
+ * `RTCPeerConnection.close()`) moves to `closed` without statechange.
75
+ * A new association (renegotiation) starts again from `connecting`.
76
+ */
77
+ Object.defineProperty(this, "state", {
78
+ enumerable: true,
79
+ configurable: true,
80
+ writable: true,
81
+ value: "connecting"
82
+ });
83
+ Object.defineProperty(this, "onStateChange", {
84
+ enumerable: true,
85
+ configurable: true,
86
+ writable: true,
87
+ value: new common_1.Event()
88
+ });
89
+ Object.defineProperty(this, "onstatechange", {
90
+ enumerable: true,
91
+ configurable: true,
92
+ writable: true,
93
+ value: void 0
94
+ });
95
+ Object.defineProperty(this, "events", {
96
+ enumerable: true,
97
+ configurable: true,
98
+ writable: true,
99
+ value: new helper_1.EventTarget()
100
+ });
55
101
  Object.defineProperty(this, "mid", {
56
102
  enumerable: true,
57
103
  configurable: true,
@@ -95,11 +141,26 @@ class RTCSctpTransport {
95
141
  writable: true,
96
142
  value: new common_1.FifoQueue()
97
143
  });
98
- Object.defineProperty(this, "dataChannelId", {
144
+ /**
145
+ * @see tests/datachannel/closeDelivery.test.ts "messages sent before close() while connecting reach the peer, then both ends close"
146
+ */
147
+ Object.defineProperty(this, "closeAfterFlush", {
99
148
  enumerable: true,
100
149
  configurable: true,
101
150
  writable: true,
102
- value: void 0
151
+ value: new Set()
152
+ });
153
+ /**
154
+ * In-band channels created here whose DATA_CHANNEL_OPEN is not sent yet;
155
+ * their stream ID, if any, is a reservation.
156
+ * @see tests/issue/815.test.ts "a rollback gives back the IDs a pranswer reserved"
157
+ * @see tests/issue/815.test.ts "a move to a DTLS transport with a decided role assigns its parity; a reset association waits for the next negotiation"
158
+ */
159
+ Object.defineProperty(this, "unannounced", {
160
+ enumerable: true,
161
+ configurable: true,
162
+ writable: true,
163
+ value: new Set()
103
164
  });
104
165
  Object.defineProperty(this, "eventDisposer", {
105
166
  enumerable: true,
@@ -107,6 +168,80 @@ class RTCSctpTransport {
107
168
  writable: true,
108
169
  value: []
109
170
  });
171
+ Object.defineProperty(this, "stopping", {
172
+ enumerable: true,
173
+ configurable: true,
174
+ writable: true,
175
+ value: false
176
+ });
177
+ /** Associations whose start began before DTLS peer authentication. */
178
+ Object.defineProperty(this, "preAuthenticationStarts", {
179
+ enumerable: true,
180
+ configurable: true,
181
+ writable: true,
182
+ value: new WeakSet()
183
+ });
184
+ /**
185
+ * Closing channels' streams and which reset direction is done.
186
+ * @see tests/issue/815.reset.test.ts "close fires on neither end before both directions of the stream are reset"
187
+ */
188
+ Object.defineProperty(this, "closingStreams", {
189
+ enumerable: true,
190
+ configurable: true,
191
+ writable: true,
192
+ value: new Map()
193
+ });
194
+ /**
195
+ * @internal
196
+ * How long a channel waits for the peer to reset its side. 5 s covers one
197
+ * retransmission of a lost reset request (SCTP RTO.Initial 3 s, RFC 9260
198
+ * section 16).
199
+ * @see tests/issue/815.reset.test.ts "a legacy peer that does not reset its side (ordered=%s, no data from it): close fires after reverseResetTimeout and the ID stays in use"
200
+ */
201
+ Object.defineProperty(this, "reverseResetTimeout", {
202
+ enumerable: true,
203
+ configurable: true,
204
+ writable: true,
205
+ value: 5000
206
+ });
207
+ /**
208
+ * @internal
209
+ * The initial `binaryType` of a channel the remote peer announces
210
+ * (`PeerConfig.dataChannelBinaryType`, read when the channel is created).
211
+ */
212
+ Object.defineProperty(this, "defaultBinaryType", {
213
+ enumerable: true,
214
+ configurable: true,
215
+ writable: true,
216
+ value: () => "nodebuffer"
217
+ });
218
+ /**
219
+ * Streams closed by {@link RTCSctpTransport.reverseResetTimeout}: in use
220
+ * until the peer's reset arrives or the association ends.
221
+ * @see tests/issue/815.reset.test.ts "a peer reset arriving after reverseResetTimeout closes nothing and frees the ID"
222
+ */
223
+ Object.defineProperty(this, "awaitingPeerReset", {
224
+ enumerable: true,
225
+ configurable: true,
226
+ writable: true,
227
+ value: new Set()
228
+ });
229
+ Object.defineProperty(this, "addEventListener", {
230
+ enumerable: true,
231
+ configurable: true,
232
+ writable: true,
233
+ value: (type, listener, options) => {
234
+ this.events.addEventListener(type, listener, options);
235
+ }
236
+ });
237
+ Object.defineProperty(this, "removeEventListener", {
238
+ enumerable: true,
239
+ configurable: true,
240
+ writable: true,
241
+ value: (type, listener) => {
242
+ this.events.removeEventListener(type, listener);
243
+ }
244
+ });
110
245
  Object.defineProperty(this, "datachannelReceive", {
111
246
  enumerable: true,
112
247
  configurable: true,
@@ -125,7 +260,27 @@ class RTCSctpTransport {
125
260
  log("DATA_CHANNEL_OPEN data.length not enough");
126
261
  return;
127
262
  }
128
- if (!Object.keys(this.dataChannels).includes(streamId.toString())) {
263
+ const existing = this.dataChannels[streamId];
264
+ if (existing && !existing.isCreatedByRemote) {
265
+ // Test: tests/issue/815.test.ts "is not acknowledged and the stream is reset, so neither channel stays connecting"
266
+ log("DATA_CHANNEL_OPEN on a stream ID in local use, rejected", {
267
+ streamId,
268
+ label: existing.label,
269
+ });
270
+ this.rejectOpenOnStreamInUse(existing);
271
+ return;
272
+ }
273
+ if (!existing &&
274
+ streamId % 2 === this.idParity &&
275
+ !this.peerMayUseIceRoleParity) {
276
+ // Test: tests/issue/815.test.ts "issue 815: a DATA_CHANNEL_OPEN violating the odd/even rule on an unused stream"
277
+ log("DATA_CHANNEL_OPEN violating the odd/even rule, rejected", {
278
+ streamId,
279
+ });
280
+ this.resetStream(streamId);
281
+ return;
282
+ }
283
+ if (!existing) {
129
284
  // !BBHLHH — 12 byte DCEP OPEN header (big-endian)
130
285
  const channelType = data.readUInt8(1);
131
286
  const reliability = data.readUInt32BE(4);
@@ -155,20 +310,27 @@ class RTCSctpTransport {
155
310
  protocol,
156
311
  id: streamId,
157
312
  });
158
- const channel = new dataChannel_1.RTCDataChannel(this, parameters, false);
313
+ const channel = new dataChannel_1.RTCDataChannel(this, parameters, false, this.defaultBinaryType());
159
314
  channel.isCreatedByRemote = true;
160
315
  this.dataChannels[streamId] = channel;
161
- this.onDataChannel.execute(channel);
162
- channel.setReadyState("open");
316
+ // Test: tests/datachannel/closeDelivery.test.ts "close() in the datachannel handler: the opener gets the ACK first, no error, both ends close"
317
+ this.sendDataChannelAck(channel);
318
+ // Test: tests/issue/815.notification.test.ts "datachannel: later listeners run and the channel opens on both ends"
319
+ this.onDataChannel.executeIsolated((error) => log("onDataChannel listener failed", error), channel);
320
+ if (channel.readyState === "connecting") {
321
+ channel.setReadyState("open");
322
+ }
323
+ return;
324
+ }
325
+ else if (existing.readyState !== "open") {
326
+ // Test: tests/issue/815.reset.test.ts "a DATA_CHANNEL_OPEN on the stream of a closing remote channel is not acknowledged"
327
+ log("DATA_CHANNEL_OPEN on a closing stream, ignored", streamId);
328
+ return;
163
329
  }
164
330
  else {
165
331
  log("datachannel already opened", "retransmit ack");
166
332
  }
167
- const channel = this.dataChannels[streamId];
168
- const ack = Buffer.allocUnsafe(1);
169
- ack.writeUInt8(const_1.DATA_CHANNEL_ACK, 0);
170
- this.dataChannelQueue.push([channel, const_1.WEBRTC_DCEP, ack]);
171
- this.dataChannelFlush();
333
+ this.sendDataChannelAck(existing);
172
334
  }
173
335
  break;
174
336
  case const_1.DATA_CHANNEL_ACK:
@@ -176,9 +338,13 @@ class RTCSctpTransport {
176
338
  log("DATA_CHANNEL_ACK", streamId, ppId);
177
339
  const channel = this.dataChannels[streamId];
178
340
  if (!channel) {
179
- throw new Error("channel not found");
341
+ // Test: tests/issue/815.notification.test.ts "the receive callback ignores an ACK of an unknown stream and an unknown PPID"
342
+ log("DATA_CHANNEL_ACK for an unknown stream ID, ignored", streamId);
343
+ return;
344
+ }
345
+ if (channel.readyState === "connecting") {
346
+ channel.setReadyState("open");
180
347
  }
181
- channel.setReadyState("open");
182
348
  }
183
349
  break;
184
350
  }
@@ -197,33 +363,74 @@ class RTCSctpTransport {
197
363
  case const_1.WEBRTC_BINARY_EMPTY:
198
364
  return Buffer.from([]);
199
365
  default:
200
- throw new Error();
366
+ return undefined;
201
367
  }
202
368
  })();
203
- // Update reception statistics
204
- channel.messagesReceived++;
205
- channel.bytesReceived += data.length;
206
- channel.onMessage.execute(msg);
207
- channel.emit("message", { data: msg });
208
- if (channel.onmessage) {
209
- channel.onmessage({ data: msg });
369
+ // Test: tests/issue/815.notification.test.ts "the receive callback ignores an ACK of an unknown stream and an unknown PPID"
370
+ if (msg === undefined) {
371
+ log("message with an unknown PPID, ignored", { streamId, ppId });
372
+ return;
373
+ }
374
+ // W3C webrtc-pc "Receiving messages on a data channel" step 3.
375
+ // Test: tests/issue/815.reset.test.ts "a message arriving while closing is discarded (W3C: not open discards it)"
376
+ if (channel.readyState === "closing" ||
377
+ channel.readyState === "closed") {
378
+ log("message on a closing data channel, discarded", streamId);
379
+ return;
210
380
  }
381
+ // Test: tests/datachannel/bufferedAmount.test.ts "empty string and empty Buffer arrive in order, bufferedAmount stays 0, stats count them"
382
+ channel.messagesReceived++;
383
+ channel.bytesReceived +=
384
+ ppId === const_1.WEBRTC_STRING_EMPTY || ppId === const_1.WEBRTC_BINARY_EMPTY
385
+ ? 0
386
+ : data.length;
387
+ // Test: tests/issue/815.notification.test.ts "message: later listeners and later messages are delivered"
388
+ const onError = (error) => log("message listener failed", error);
389
+ channel.onMessage.executeIsolated(onError, msg);
390
+ // Test: tests/datachannel/bufferedAmount.test.ts "onmessage receives the same message event object as the message listeners"
391
+ // binaryType: converted once, the same object for both surfaces.
392
+ // Test: tests/datachannel/binaryType.test.ts "$binaryType: $name arrives byte for byte"
393
+ const event = {
394
+ type: "message",
395
+ data: (0, dataChannel_1.convertDataChannelMessage)(msg, channel.binaryType),
396
+ };
397
+ channel.emitIsolated(onError, "message", event);
398
+ (0, helper_1.notifySafely)(log, "onmessage", () => channel.onmessage?.(event));
211
399
  }
212
400
  }
213
401
  }
214
402
  });
403
+ /**
404
+ * What {@link RTCDataChannel.send} calls; use that instead.
405
+ * @see tests/datachannel/bufferedAmount.test.ts "empty string and empty Buffer arrive in order, bufferedAmount stays 0, stats count them"
406
+ */
215
407
  Object.defineProperty(this, "datachannelSend", {
216
408
  enumerable: true,
217
409
  configurable: true,
218
410
  writable: true,
219
411
  value: (channel, data) => {
220
- const userData = Buffer.isBuffer(data) ? data : Buffer.from(data);
221
412
  const size = (0, dataChannel_1.getDataChannelMessageSize)(data);
222
413
  this.assertSendableMessageSize(size);
223
- channel.addBufferedAmount(size);
224
- this.dataChannelQueue.push(typeof data === "string"
225
- ? [channel, const_1.WEBRTC_STRING, userData]
226
- : [channel, const_1.WEBRTC_BINARY, userData]);
414
+ channel.changeBufferedAmount(size);
415
+ const isString = typeof data === "string";
416
+ if (size === 0) {
417
+ // Test: tests/datachannel/bufferedAmount.test.ts "empty string and empty Buffer arrive in order, bufferedAmount stays 0, stats count them"
418
+ this.dataChannelQueue.push([
419
+ channel,
420
+ isString ? const_1.WEBRTC_STRING_EMPTY : const_1.WEBRTC_BINARY_EMPTY,
421
+ Buffer.from([0]),
422
+ 0,
423
+ ]);
424
+ }
425
+ else {
426
+ const userData = Buffer.isBuffer(data) ? data : Buffer.from(data);
427
+ this.dataChannelQueue.push([
428
+ channel,
429
+ isString ? const_1.WEBRTC_STRING : const_1.WEBRTC_BINARY,
430
+ userData,
431
+ size,
432
+ ]);
433
+ }
227
434
  if (this.sctp.associationState !== src_1.SCTP_STATE.ESTABLISHED) {
228
435
  log("sctp not established", this.sctp.associationState);
229
436
  }
@@ -238,6 +445,22 @@ class RTCSctpTransport {
238
445
  get transport() {
239
446
  return this.dtlsTransport;
240
447
  }
448
+ /**
449
+ * Change the state and notify. Each notification is isolated: an
450
+ * exception a listener throws is logged and never interrupts the caller
451
+ * (stop() and its ABORT, closing the data channels).
452
+ */
453
+ setState(state, emitEvent = true) {
454
+ if (state === this.state)
455
+ return;
456
+ this.state = state;
457
+ // Per subscriber, so a throwing subscriber cannot skip a later one.
458
+ this.onStateChange.executeIsolated((error) => log("onStateChange listener failed", error), state);
459
+ if (emitEvent) {
460
+ (0, helper_1.notifySafely)(log, "onstatechange", () => this.onstatechange?.());
461
+ this.events.emitIsolated((error) => log("statechange listener failed", error), "statechange");
462
+ }
463
+ }
241
464
  setDtlsTransport(dtlsTransport) {
242
465
  if (this.dtlsTransport && this.dtlsTransport.id === dtlsTransport.id) {
243
466
  return;
@@ -248,14 +471,14 @@ class RTCSctpTransport {
248
471
  * @internal
249
472
  * Discard the current association (ABORT, attached channels close) and
250
473
  * bind a new, unstarted one (on the same DTLS transport by default).
251
- * Channels still waiting for a stream ID stay queued for the next
252
- * association.
474
+ * Channels whose DATA_CHANNEL_OPEN was not sent stay, without stream ID.
475
+ * @see tests/issue/815.test.ts "a move to a DTLS transport with a decided role assigns its parity; a reset association waits for the next negotiation"
253
476
  */
254
477
  async resetAssociation(dtlsTransport = this.dtlsTransport) {
478
+ this.releaseUnannouncedStreamIds();
255
479
  await this.stop();
256
- this.dataChannelQueue = this.dataChannelQueue.filter(([channel]) => channel.id === undefined);
257
- this.dataChannelId = undefined;
258
- this.bindAssociation(dtlsTransport);
480
+ this.dataChannelQueue = this.dataChannelQueue.filter(([channel]) => this.unannounced.has(channel));
481
+ this.bindAssociation(dtlsTransport, { assignIds: false });
259
482
  }
260
483
  /**
261
484
  * @internal
@@ -266,59 +489,361 @@ class RTCSctpTransport {
266
489
  this.sctp.state !== "new" ||
267
490
  this.sctp.associationState !== src_1.SCTP_STATE.CLOSED);
268
491
  }
269
- bindAssociation(dtlsTransport) {
270
- this.eventDisposer.forEach((dispose) => dispose());
492
+ /**
493
+ * `assignIds`: unannounced channels take stream IDs from the bound DTLS
494
+ * transport's role at once.
495
+ * @see tests/issue/815.test.ts "a move to a DTLS transport with a decided role assigns its parity; a reset association waits for the next negotiation"
496
+ */
497
+ bindAssociation(dtlsTransport, { assignIds = true } = {}) {
498
+ this.disposeSctpListeners();
499
+ this.clearClosingStreams();
500
+ this.closeAfterFlush.clear();
271
501
  this.dtlsTransport = dtlsTransport;
272
- // A new association: the transport delivers again.
502
+ this.stopping = false;
503
+ this.stopped = false;
504
+ this.createSctpAssociation();
505
+ if (assignIds) {
506
+ this.releaseUnannouncedStreamIds();
507
+ this.assignPendingStreamIds();
508
+ }
509
+ }
510
+ disposeSctpListeners() {
511
+ this.eventDisposer.forEach((dispose) => dispose());
512
+ this.eventDisposer = [];
513
+ }
514
+ createSctpAssociation() {
515
+ this.disposeSctpListeners();
516
+ this.clearClosingStreams();
273
517
  this.stopped = false;
518
+ this.setState("connecting", false);
274
519
  this.sctp = new src_1.SCTP(new BridgeDtls(this.dtlsTransport), this.port, this.sctpOptions);
520
+ const association = this.sctp;
275
521
  this.eventDisposer = [
276
522
  ...[
277
- this.sctp.onReceive.subscribe(this.datachannelReceive),
278
- this.sctp.onReconfigStreams.subscribe((ids) => {
279
- ids.forEach((id) => {
280
- const dc = this.dataChannels[id];
281
- if (!dc)
282
- return;
283
- // todo fix
284
- dc.setReadyState("closing");
285
- dc.setReadyState("closed");
286
- delete this.dataChannels[id];
287
- });
288
- }),
289
- this.sctp.stateChanged.connected.subscribe(() => {
523
+ association.onReceive.subscribe(this.datachannelReceive),
524
+ association.onReconfigStreams.subscribe((ids, direction) => this.onStreamsReset(ids, direction)),
525
+ association.stateChanged.connected.subscribe(() => {
526
+ this.setState("connected");
290
527
  Object.values(this.dataChannels).forEach((channel) => {
291
- if (channel.negotiated && channel.readyState !== "open") {
528
+ // Test: tests/datachannel/closeDelivery.test.ts "messages sent before close() while connecting reach the peer, then both ends close"
529
+ if (channel.negotiated && channel.readyState === "connecting") {
292
530
  channel.setReadyState("open");
293
531
  }
294
532
  });
295
533
  this.dataChannelFlush();
296
534
  }),
297
- this.sctp.stateChanged.closed.subscribe(() => {
298
- Object.values(this.dataChannels).forEach((dc) => {
299
- dc.setReadyState("closed");
300
- });
301
- this.dataChannels = {};
535
+ association.stateChanged.closed.subscribe(() => {
536
+ if (!this.stopped)
537
+ this.setState("closed");
538
+ this.clearClosingStreams();
539
+ this.closeAfterFlush.clear();
540
+ // An explicit stop() closes the channels registered on this
541
+ // association only; channels whose OPEN is still queued outlive it
542
+ // (rollback re-announces them on the next association).
543
+ if (this.stopping) {
544
+ this.closeDataChannels({ clearQueue: false, includeQueued: false });
545
+ return;
546
+ }
547
+ // Keep pre-establishment channels and queued DCEP available for a
548
+ // retry. Once an association was established, closing it closes them.
549
+ if (!association.hadEstablished)
550
+ return;
551
+ this.closeDataChannels();
302
552
  }),
553
+ ...(() => {
554
+ // A pre-established early association cannot make progress after
555
+ // permission is revoked. Cancel it immediately instead of waiting
556
+ // for SCTP T1 retransmissions to expire; authenticated retry will
557
+ // create a fresh association through SctpTransportManager.
558
+ // SCTP refuses once COOKIE_ECHO / COOKIE_ACK was handed off (the
559
+ // peer may be ESTABLISHED and never re-INIT). Such a start is kept;
560
+ // a cancelled attempt may have dropped the peer's in-flight reply,
561
+ // so retransmit now rather than at the next T1 expiry.
562
+ //
563
+ // The cancel applies only to the early attempt itself:
564
+ // - a start made after DTLS peer authentication is not early, and
565
+ // an early start becomes ordinary once DTLS authenticates; both
566
+ // must survive SPED resets / ICE restarts;
567
+ // - only the DTLS server (the active INIT sender) sends SCTP under
568
+ // the early permission. The DTLS client's passive association
569
+ // is merely armed early; cancelling it would discard the cookie
570
+ // key of an INIT_ACK it already sent, so the peer's COOKIE_ECHO
571
+ // could never be accepted.
572
+ const cancelEarlyStart = () => {
573
+ if (this.sctp !== association)
574
+ return;
575
+ const earlyAttempt = this.preAuthenticationStarts.has(association) &&
576
+ this.dtlsTransport.role === "server" &&
577
+ // An early start still pending once DTLS authenticated no
578
+ // longer depends on the early permission: its retransmissions
579
+ // are ordinary authenticated sends.
580
+ this.dtlsTransport.state !== "connected";
581
+ if (earlyAttempt &&
582
+ association.cancelStart(new sctp_1.SCTPStartCancelledError())) {
583
+ return;
584
+ }
585
+ // The start is kept (not early, or already committed). The reset
586
+ // may have dropped an in-flight reply, so retransmit the pending
587
+ // INIT / COOKIE_ECHO now instead of at the next T1 expiry
588
+ // (no-op when nothing is pending, e.g. the passive side).
589
+ association.retransmitStartNow();
590
+ };
591
+ return [
592
+ this.dtlsTransport.onEarlyApplicationSendRevoked?.subscribe(cancelEarlyStart),
593
+ this.dtlsTransport.onEarlyApplicationAttemptCancelled?.subscribe(cancelEarlyStart),
594
+ ].flatMap((subscription) => (subscription ? [subscription] : []));
595
+ })(),
303
596
  this.dtlsTransport.onStateChange.subscribe((state) => {
304
- if (state === "closed") {
305
- this.sctp.setState(src_1.SCTP_STATE.CLOSED);
597
+ if (state === "connected") {
598
+ // A start kept across a cancel/revoke can proceed now; don't wait
599
+ // for the next T1 expiry.
600
+ if (this.sctp === association)
601
+ association.retransmitStartNow();
602
+ return;
603
+ }
604
+ if (state === "failed" || state === "closed") {
605
+ // A stopped transport is left to stop():
606
+ // RTCPeerConnection.close() marks DTLS closed synchronously,
607
+ // before stop() sends the SCTP ABORT, and closing the association
608
+ // here would skip that ABORT.
609
+ if (this.stopped)
610
+ return;
611
+ // DTLS failure is terminal for this SCTP association. Do not
612
+ // preserve queued DCEP/data for a retry after authentication has
613
+ // failed, and close every channel immediately.
614
+ this.stopping = true;
615
+ // The channels' close notification sees the transport closed.
616
+ this.setState("closed");
617
+ // Close queued pre-establishment channels before SCTP removes its
618
+ // state listeners; otherwise their DCEP entries are lost without
619
+ // a DataChannel close event.
620
+ this.closeDataChannels();
621
+ association.setState(src_1.SCTP_STATE.CLOSED);
622
+ this.disposeSctpListeners();
306
623
  }
307
624
  }),
625
+ // Test: tests/issue/815.test.ts "issue 815: data channel stream IDs follow the DTLS role"
626
+ // Test: tests/issue/815.test.ts "the DTLS start resolving auto assigns the IDs of the role it took"
627
+ this.dtlsTransport.onRoleChange.subscribe(() => {
628
+ this.releaseUnannouncedStreamIds();
629
+ this.assignPendingStreamIds();
630
+ }),
308
631
  ].map((e) => e.unSubscribe),
309
- () => (this.sctp.onSackReceived = async () => { }),
632
+ () => (association.onSackReceived = async () => { }),
310
633
  ];
311
- this.sctp.onSackReceived = async () => {
312
- this.dataChannelFlush();
634
+ association.onSackReceived = async () => {
635
+ await this.dataChannelFlush();
313
636
  };
314
637
  }
638
+ /** @internal Prepare a fresh association after INIT/T1 failure. */
639
+ prepareForStart() {
640
+ if (this.stopping) {
641
+ throw new Error("SCTP transport is stopped");
642
+ }
643
+ if (this.sctp.state === "closed") {
644
+ this.createSctpAssociation();
645
+ }
646
+ return this.sctp;
647
+ }
648
+ /**
649
+ * RFC 8831 section 6.7: the first reset of a stream makes its channel
650
+ * `closing`; it is `closed`, its ID released just before, once both
651
+ * directions are reset.
652
+ * @see tests/issue/815.reset.test.ts "issue 815: a stream ID reused after a close (RFC 8831 section 6.7 reset in both directions)"
653
+ * @see tests/issue/815.notification.test.ts "closed by $closedBy: a close listener re-creates a channel on the only free ID"
654
+ */
655
+ onStreamsReset(ids, direction) {
656
+ for (const id of ids) {
657
+ if (direction === "incoming" && this.awaitingPeerReset.delete(id)) {
658
+ // Test: tests/issue/815.reset.test.ts "a peer reset arriving after reverseResetTimeout closes nothing and frees the ID"
659
+ this.sctp.reconfigQueue = this.sctp.reconfigQueue.filter((streamId) => streamId !== id);
660
+ continue;
661
+ }
662
+ // Test: tests/issue/815.test.ts "issue 815: a DATA_CHANNEL_OPEN violating the odd/even rule on an unused stream"
663
+ const dc = this.dataChannels[id];
664
+ if (!dc)
665
+ continue;
666
+ let reset = this.closingStreams.get(id);
667
+ if (!reset) {
668
+ reset = { outgoing: false, incoming: false };
669
+ this.closingStreams.set(id, reset);
670
+ }
671
+ reset[direction] = true;
672
+ if (dc.readyState !== "closing") {
673
+ dc.setReadyState("closing");
674
+ // Test: tests/issue/815.notification.test.ts "a closing listener closing the PeerConnection ends the channel once"
675
+ if (this.dataChannels[id] !== dc || dc.readyState === "closed") {
676
+ continue;
677
+ }
678
+ }
679
+ if (direction === "incoming")
680
+ this.answerIncomingReset(id, reset);
681
+ this.completeClose(id, dc, reset);
682
+ }
683
+ }
684
+ /**
685
+ * Reset our side of a stream the peer reset, exactly once.
686
+ * @see tests/issue/815.reset.test.ts "issue 815: a stream ID reused after a close (RFC 8831 section 6.7 reset in both directions)"
687
+ */
688
+ answerIncomingReset(id, reset) {
689
+ const inFlight = !!this.sctp.reconfigRequest?.streams.includes(id);
690
+ if (reset.outgoing || inFlight) {
691
+ // Test: tests/issue/815.notification.test.ts "closed by both ends at once: a close listener re-creates a channel that opens"
692
+ this.sctp.reconfigQueue = this.sctp.reconfigQueue.filter((streamId) => streamId !== id);
693
+ return;
694
+ }
695
+ this.resetStream(id);
696
+ }
697
+ completeClose(id, dc, reset) {
698
+ if (reset.outgoing && reset.incoming) {
699
+ clearTimeout(reset.fallback);
700
+ this.closingStreams.delete(id);
701
+ delete this.dataChannels[id];
702
+ this.unannounced.delete(dc);
703
+ dc.setReadyState("closed");
704
+ }
705
+ else if (reset.outgoing && !reset.fallback) {
706
+ reset.fallback = setTimeout(() => {
707
+ if (this.closingStreams.get(id) !== reset)
708
+ return;
709
+ log("the peer did not reset its side of the stream", id);
710
+ // Test: tests/issue/815.reset.test.ts "a peer reset arriving after reverseResetTimeout closes nothing and frees the ID"
711
+ this.awaitingPeerReset.add(id);
712
+ reset.incoming = true;
713
+ this.completeClose(id, dc, reset);
714
+ }, this.reverseResetTimeout);
715
+ reset.fallback.unref?.();
716
+ }
717
+ }
718
+ clearClosingStreams() {
719
+ for (const { fallback } of this.closingStreams.values()) {
720
+ clearTimeout(fallback);
721
+ }
722
+ this.closingStreams.clear();
723
+ // Test: tests/issue/815.reset.test.ts "pc.close() while waiting for the peer's reset closes the channel once and leaves no timer"
724
+ this.awaitingPeerReset.clear();
725
+ }
726
+ /**
727
+ * The SCTP INIT side: the DTLS server, not the ICE role.
728
+ * @see tests/issue/815.test.ts "#815 report: reversed ICE / DTLS roles with channels from both ends do not collide"
729
+ */
315
730
  get isServer() {
316
- const role = this.dtlsTransport.iceRoleAtStart ?? this.dtlsTransport.iceTransport.role;
317
- return role !== "controlling";
731
+ return this.dtlsTransport.role === "server";
732
+ }
733
+ /**
734
+ * RFC 8832 section 6: DTLS client even, server odd; none while `auto`.
735
+ * @see tests/issue/815.test.ts "issue 815: data channel stream IDs follow the DTLS role"
736
+ */
737
+ /**
738
+ * werift up to 0.28.0 picks its stream ID parity by the ICE role (controlling
739
+ * odd), not by the DTLS role. With the ICE and DTLS roles reversed, such a
740
+ * peer opens its channels on this side's parity, so an OPEN on an unused
741
+ * stream of this parity is accepted instead of reset. An RFC 8832 peer never
742
+ * uses this parity, so this changes nothing for it.
743
+ * @see tests/integrate/legacySctpRole.test.ts "$name: 旧 werift が開いた DataChannel を受け入れ、label どおりに届く"
744
+ */
745
+ get peerMayUseIceRoleParity() {
746
+ const parity = this.idParity;
747
+ if (parity === undefined)
748
+ return false;
749
+ const iceRole = this.dtlsTransport.iceRoleAtStart ?? this.dtlsTransport.iceTransport.role;
750
+ return (parity === 1) !== (iceRole === "controlling");
751
+ }
752
+ get idParity() {
753
+ switch (this.dtlsTransport?.role) {
754
+ case "client":
755
+ return 0;
756
+ case "server":
757
+ return 1;
758
+ default:
759
+ return undefined;
760
+ }
761
+ }
762
+ /**
763
+ * Exclusive upper bound of a stream ID: 65535, or what SCTP negotiated.
764
+ * @see tests/issue/815.test.ts "a reservation beyond the negotiated stream limit is given back and fails"
765
+ */
766
+ get streamIdLimit() {
767
+ const negotiated = this.sctp?.maxChannels;
768
+ return negotiated === undefined
769
+ ? DATA_CHANNEL_ID_LIMIT
770
+ : Math.min(negotiated, DATA_CHANNEL_ID_LIMIT);
771
+ }
772
+ /**
773
+ * The lowest free stream ID of `parity`.
774
+ * @see tests/issue/815.test.ts "auto IDs skip negotiated channels of either parity and reuse closed IDs"
775
+ */
776
+ allocateStreamId(parity) {
777
+ const limit = this.streamIdLimit;
778
+ const free = (id) => !this.dataChannels[id] && !this.awaitingPeerReset.has(id);
779
+ for (let id = parity; id < limit; id += 2) {
780
+ if (free(id))
781
+ return id;
782
+ }
783
+ return undefined;
784
+ }
785
+ /**
786
+ * @internal
787
+ * Reserve a stream ID for every unannounced channel without one, from the
788
+ * DTLS role; none left is an `OperationError` (W3C 4.4.1.6 step 2.2.6).
789
+ * @see tests/issue/815.test.ts "a channel waiting for an ID closes with OperationError when none is left"
790
+ */
791
+ assignPendingStreamIds() {
792
+ const parity = this.idParity;
793
+ if (parity === undefined)
794
+ return;
795
+ const limit = this.streamIdLimit;
796
+ for (const channel of [...this.unannounced]) {
797
+ // Test: tests/issue/815.test.ts "a channel closed by an error handler is not given an ID"
798
+ if (!this.unannounced.has(channel) ||
799
+ channel.readyState === "closed" ||
800
+ channel.readyState === "closing") {
801
+ continue;
802
+ }
803
+ if (channel.id != undefined) {
804
+ // Test: tests/issue/815.test.ts "a reservation beyond the negotiated stream limit is given back and fails"
805
+ if (channel.id < limit)
806
+ continue;
807
+ if (this.dataChannels[channel.id] === channel) {
808
+ delete this.dataChannels[channel.id];
809
+ }
810
+ channel.setId(undefined);
811
+ }
812
+ const id = this.allocateStreamId(parity);
813
+ if (id === undefined) {
814
+ this.unannounced.delete(channel);
815
+ this.dataChannelQueue = this.dataChannelQueue.filter(([queued]) => queued !== channel);
816
+ channel.failWithError((0, errors_1.createWebRtcDomException)("OperationError", "No data channel stream ID is available"));
817
+ continue;
818
+ }
819
+ this.dataChannels[id] = channel;
820
+ channel.setId(id);
821
+ }
822
+ }
823
+ /**
824
+ * @internal
825
+ * Give back the reservations of unannounced channels.
826
+ * @see tests/issue/815.test.ts "a %s gives the IDs back and the next answer, keeping the DTLS role, assigns them again"
827
+ */
828
+ releaseUnannouncedStreamIds() {
829
+ for (const channel of this.unannounced) {
830
+ if (channel.id == undefined)
831
+ continue;
832
+ if (this.dataChannels[channel.id] === channel) {
833
+ delete this.dataChannels[channel.id];
834
+ }
835
+ channel.setId(undefined);
836
+ }
318
837
  }
319
838
  channelByLabel(label) {
320
839
  return Object.values(this.dataChannels).find((d) => d.label === label);
321
840
  }
841
+ sendDataChannelAck(channel) {
842
+ const ack = Buffer.allocUnsafe(1);
843
+ ack.writeUInt8(const_1.DATA_CHANNEL_ACK, 0);
844
+ this.dataChannelQueue.push([channel, const_1.WEBRTC_DCEP, ack]);
845
+ this.dataChannelFlush();
846
+ }
322
847
  dataChannelAddNegotiated(channel) {
323
848
  if (channel.id == undefined) {
324
849
  throw new Error();
@@ -326,6 +851,10 @@ class RTCSctpTransport {
326
851
  if (this.dataChannels[channel.id]) {
327
852
  throw new Error();
328
853
  }
854
+ if (this.awaitingPeerReset.has(channel.id)) {
855
+ // Test: tests/issue/815.reset.test.ts "a legacy peer that does not reset its side (ordered=%s, no data from it): close fires after reverseResetTimeout and the ID stays in use"
856
+ throw (0, errors_1.createWebRtcDomException)("OperationError", `Stream ${channel.id} waits for the peer to reset it`);
857
+ }
329
858
  this.dataChannels[channel.id] = channel;
330
859
  if (this.sctp.associationState === src_1.SCTP_STATE.ESTABLISHED) {
331
860
  channel.setReadyState("open");
@@ -337,11 +866,21 @@ class RTCSctpTransport {
337
866
  // Their 16-bit limit is enforced by the RTCDataChannel constructor.
338
867
  const label = Buffer.from(channel.label, "utf8");
339
868
  const protocol = Buffer.from(channel.protocol, "utf8");
340
- if (channel.id !== undefined) {
341
- if (this.dataChannels[channel.id])
342
- throw new Error(`Data channel with ID ${channel.id} already registered`);
343
- this.dataChannels[channel.id] = channel;
869
+ // Test: tests/issue/815.test.ts "an in-band channel reaching the transport with an id is an internal error"
870
+ if (channel.id != undefined) {
871
+ throw new Error("internal: an in-band data channel gets its stream ID from the DTLS role");
872
+ }
873
+ // Test: tests/issue/815.test.ts "a channel waiting for an ID closes with OperationError when none is left"
874
+ const parity = this.idParity;
875
+ if (parity !== undefined) {
876
+ const id = this.allocateStreamId(parity);
877
+ if (id === undefined) {
878
+ throw (0, errors_1.createWebRtcDomException)("OperationError", "No data channel stream ID is available");
879
+ }
880
+ this.dataChannels[id] = channel;
881
+ channel.setId(id);
344
882
  }
883
+ this.unannounced.add(channel);
345
884
  const priority = 0;
346
885
  // RFC 8832 5.1: the reliability mode and the unordered bit (0x80) are
347
886
  // independent, so a partially reliable channel keeps its unordered bit.
@@ -377,43 +916,33 @@ class RTCSctpTransport {
377
916
  this.dataChannelQueue.push([channel, const_1.WEBRTC_DCEP, send]);
378
917
  this.dataChannelFlush();
379
918
  }
919
+ /**
920
+ * Hand the queued DCEP and user messages to SCTP once the association is
921
+ * established; a channel still without stream ID stays queued.
922
+ * @see tests/issue/815.test.ts "issue 815: data channel stream IDs follow the DTLS role"
923
+ * @see tests/datachannel/closeDelivery.test.ts "messages sent before close() while connecting reach the peer, then both ends close"
924
+ */
380
925
  dataChannelFlush() {
381
- // """
382
- // Try to flush buffered data to the SCTP layer.
383
- // We wait until the association is established, as we need to know
384
- // whether we are a client or a server to correctly assign an odd/even ID
385
- // to the data channels.
386
- // """
387
926
  if (this.sctp.associationState != src_1.SCTP_STATE.ESTABLISHED)
388
927
  return;
389
- // The peer's INIT can establish an association that joined a connected
390
- // DTLS transport before start() ran here; the same side decides the IDs.
391
- this.dataChannelId ?? (this.dataChannelId = this.isServer ? 0 : 1);
928
+ this.assignPendingStreamIds();
929
+ const waiting = new common_1.FifoQueue();
392
930
  // Hands every queued message to SCTP synchronously (SCTP.send enqueues
393
931
  // before its first await) and tracks completion per message, so repeated
394
932
  // datachannelSend() calls never spawn concurrent flush loops.
395
933
  while (this.dataChannelQueue.length > 0) {
396
- const [channel, protocol, userData] = this.dataChannelQueue.shift();
397
- // A channel closed while its message was queued (e.g. the association
398
- // closed and was re-established) is no longer managed; never announce it.
399
- if (channel.readyState === "closed" || channel.readyState === "closing") {
400
- if (protocol !== const_1.WEBRTC_DCEP)
401
- channel.addBufferedAmount(-userData.length);
934
+ const item = this.dataChannelQueue.shift();
935
+ const [channel, protocol, userData] = item;
936
+ if (channel.readyState === "closed")
937
+ continue;
938
+ const streamId = channel.id;
939
+ if (streamId == undefined) {
940
+ waiting.push(item);
402
941
  continue;
403
- }
404
- let streamId = channel.id;
405
- if (streamId === undefined) {
406
- // An association the remote established before start() decides the
407
- // parity here (RFC 8832: DTLS client even; ICE controlling is server).
408
- this.dataChannelId ?? (this.dataChannelId = this.isServer ? 0 : 1);
409
- streamId = this.dataChannelId;
410
- while (Object.keys(this.dataChannels).includes(streamId.toString())) {
411
- streamId += 2;
412
- }
413
- this.dataChannels[streamId] = channel;
414
- channel.setId(streamId);
415
942
  }
416
943
  if (protocol === const_1.WEBRTC_DCEP) {
944
+ if (userData[0] === const_1.DATA_CHANNEL_OPEN)
945
+ this.unannounced.delete(channel);
417
946
  this.sctp
418
947
  .send(streamId, protocol, userData, {
419
948
  ordered: true,
@@ -426,21 +955,67 @@ class RTCSctpTransport {
426
955
  const expiry = channel.maxPacketLifeTime !== null
427
956
  ? Date.now() + channel.maxPacketLifeTime
428
957
  : undefined;
958
+ const bufferedBytes = item[3] ?? userData.length;
429
959
  this.sctp
430
960
  .send(streamId, protocol, userData, {
431
961
  expiry,
432
962
  maxRetransmits: channel.maxRetransmits ?? undefined,
433
963
  ordered: channel.ordered,
434
964
  })
435
- .catch((err) => {
965
+ .then((result) => {
966
+ // Test: tests/datachannel/bufferedAmount.test.ts "messages abandoned by maxPacketLifeTime before they were sent are subtracted and notified"
967
+ // Test: tests/datachannel/bufferedAmount.test.ts "pc.close() keeps bufferedAmount and fires no bufferedamountlow"
968
+ if (result !== src_1.SctpSendResult.discarded && bufferedBytes > 0) {
969
+ channel.changeBufferedAmount(-bufferedBytes);
970
+ }
971
+ }, (err) => {
436
972
  log("send data failed", err.message);
437
973
  })
438
- .then(() => channel.addBufferedAmount(-userData.length))
439
974
  .catch((err) => {
440
975
  log("update bufferedAmount failed", err.message);
441
976
  });
442
977
  }
443
978
  }
979
+ if (waiting.length > 0)
980
+ this.dataChannelQueue = waiting;
981
+ for (const channel of this.closeAfterFlush) {
982
+ if (channel.id == undefined)
983
+ continue;
984
+ this.closeAfterFlush.delete(channel);
985
+ if (channel.readyState !== "closing")
986
+ continue;
987
+ this.resetStream(channel.id);
988
+ }
989
+ }
990
+ /**
991
+ * Close retained channels even when SCTP removed its state listeners.
992
+ * @see tests/issue/815.test.ts "a throwing close listener of a registered channel skips no listener, channel or teardown"
993
+ * @see tests/issue/815.notification.test.ts "association closed by the peer: a channel created in a close callback stays registered until pc.close()"
994
+ * @see tests/issue/815.test.ts "pc.close() notifies close of a reserved, unannounced channel after the transports closed"
995
+ */
996
+ closeDataChannels({ clearQueue = true, includeQueued = true, } = {}) {
997
+ const channels = new Set([
998
+ ...Object.values(this.dataChannels),
999
+ ...(includeQueued
1000
+ ? this.dataChannelQueue.toArray().map(([channel]) => channel)
1001
+ : []),
1002
+ ]);
1003
+ if (clearQueue)
1004
+ this.dataChannelQueue.clear();
1005
+ else
1006
+ this.dataChannelQueue = this.dataChannelQueue.filter(([queued]) => !channels.has(queued));
1007
+ this.dataChannels = {};
1008
+ for (const channel of channels) {
1009
+ this.unannounced.delete(channel);
1010
+ if (channel.readyState === "closed")
1011
+ continue;
1012
+ try {
1013
+ channel.setReadyState("closed");
1014
+ }
1015
+ catch (error) {
1016
+ log("datachannel close callback failed", error);
1017
+ }
1018
+ }
444
1019
  }
445
1020
  assertSendableMessageSize(size) {
446
1021
  if (this.remoteMaxMessageSize !== 0 && size > this.remoteMaxMessageSize) {
@@ -460,70 +1035,193 @@ class RTCSctpTransport {
460
1035
  this.sctp.setRemotePort(port);
461
1036
  }
462
1037
  async start(remotePort) {
463
- this.assignRole();
1038
+ this.prepareForStart();
1039
+ // RTCDtlsTransport.state is "connected" only after SDP fingerprint
1040
+ // authentication, so anything earlier is an early (pre-auth) start.
1041
+ if (this.dtlsTransport.state !== "connected") {
1042
+ this.preAuthenticationStarts.add(this.sctp);
1043
+ }
1044
+ // SCTP's `isServer` flag means passive COOKIE-WAIT peer. WARP assigns the
1045
+ // active INIT sender to the DTLS server, independently of the ICE role.
1046
+ this.sctp.isServer = !this.isServer;
1047
+ if (!this.isServer) {
1048
+ this.schedulePassiveInitFallback(this.sctp);
1049
+ }
464
1050
  await this.sctp.start(remotePort);
465
1051
  }
466
1052
  /**
467
- * @internal
468
- * Idempotent start: an association that is established (also passively,
469
- * from the remote INIT), handshaking or closed is not started again, so
470
- * no second INIT is sent and the state never moves back.
1053
+ * DTLS client: if the peer's INIT has not arrived
1054
+ * {@link PASSIVE_INIT_FALLBACK_MS} after DTLS authentication, send INIT
1055
+ * ourselves (SCTP answers a colliding INIT, RFC 9260 §5.2.1). Never before
1056
+ * authentication: the DTLS client has no early send permission.
471
1057
  */
1058
+ schedulePassiveInitFallback(association) {
1059
+ let timer;
1060
+ const arm = () => {
1061
+ if (timer)
1062
+ return;
1063
+ timer = setTimeout(() => {
1064
+ if (this.sctp !== association || this.stopping)
1065
+ return;
1066
+ association.startPassiveFallbackInit().catch((error) => {
1067
+ log("passive fallback INIT failed", error.message);
1068
+ });
1069
+ }, exports.PASSIVE_INIT_FALLBACK_MS);
1070
+ };
1071
+ if (this.dtlsTransport.state === "connected") {
1072
+ arm();
1073
+ }
1074
+ else {
1075
+ const subscription = this.dtlsTransport.onStateChange.subscribe((state) => {
1076
+ if (state !== "connected")
1077
+ return;
1078
+ subscription.unSubscribe();
1079
+ arm();
1080
+ });
1081
+ this.eventDisposer.push(subscription.unSubscribe);
1082
+ }
1083
+ // The association is replaced or the transport stops: drop the timer.
1084
+ this.eventDisposer.push(() => clearTimeout(timer));
1085
+ }
1086
+ /** @internal Start once; an established or already started association stays intact. */
472
1087
  async ensureStarted(remotePort) {
473
- if (this.sctp.started || this.sctp.state === "closed")
1088
+ if (this.sctp.started ||
1089
+ this.sctp.state === "connected" ||
1090
+ this.sctp.state === "closed") {
474
1091
  return;
475
- this.assignRole();
476
- if (this.sctp.state === "connected")
477
- return;
478
- await this.sctp.start(remotePort);
1092
+ }
1093
+ await this.start(remotePort);
479
1094
  }
480
- assignRole() {
481
- this.dataChannelId = this.isServer ? 0 : 1;
482
- this.sctp.isServer = this.isServer;
1095
+ /**
1096
+ * @internal
1097
+ * The synchronous part of {@link RTCSctpTransport.stop}: `closed` without
1098
+ * statechange, and nothing received afterwards reaches the application.
1099
+ * `RTCPeerConnection.close()` calls it before its first await.
1100
+ */
1101
+ markStopped() {
1102
+ this.stopping = true;
1103
+ this.stopped = true;
1104
+ this.setState("closed", false);
1105
+ }
1106
+ /**
1107
+ * @internal
1108
+ * `RTCPeerConnection.close()`, after stop(): close the channels whose
1109
+ * DATA_CHANNEL_OPEN was not sent (stop() keeps them for a rollback).
1110
+ * @see tests/issue/815.test.ts "pc.close() notifies close of a channel without an ID after SCTP / DTLS / the PeerConnection closed"
1111
+ * @see tests/issue/815.test.ts "pc.close() notifies close of a reserved, unannounced channel after the transports closed"
1112
+ */
1113
+ closeUnannouncedChannels() {
1114
+ const channels = [...this.unannounced];
1115
+ this.unannounced.clear();
1116
+ this.dataChannelQueue = this.dataChannelQueue.filter(([queued]) => !channels.includes(queued));
1117
+ for (const channel of channels) {
1118
+ if (channel.id != undefined &&
1119
+ this.dataChannels[channel.id] === channel) {
1120
+ delete this.dataChannels[channel.id];
1121
+ }
1122
+ // Test: tests/issue/815.test.ts "pc.close() closes every pending channel even if a close listener throws"
1123
+ channel.setReadyState("closed");
1124
+ }
483
1125
  }
484
1126
  async stop() {
485
- this.stopped = true;
1127
+ const closedAlready = this.sctp.state === "closed";
1128
+ this.markStopped();
486
1129
  this.dtlsTransport.dataReceiver = () => { };
487
1130
  this.discardUnsentMessages();
488
- await this.sctp.stop();
1131
+ try {
1132
+ await this.sctp.stop();
1133
+ }
1134
+ finally {
1135
+ // An association already CLOSED emits no closed event again.
1136
+ if (closedAlready) {
1137
+ this.closeDataChannels({ clearQueue: false, includeQueued: false });
1138
+ }
1139
+ this.disposeSctpListeners();
1140
+ }
489
1141
  }
490
1142
  // Messages not yet handed to SCTP belong to the stopped association and must
491
- // not leak into a later one. User messages are dropped and removed from
492
- // bufferedAmount; DATA_CHANNEL_OPEN is kept only for channels still alive, so
493
- // a channel that outlives the transport (e.g. rollback) can be announced,
494
- // while closed channels and stale ACKs go.
1143
+ // not leak into a later one. User messages are dropped; DATA_CHANNEL_OPEN is
1144
+ // kept only for channels still alive, so a channel that outlives the
1145
+ // transport (e.g. rollback) can be announced, while closed channels and
1146
+ // stale ACKs go.
1147
+ // Test: tests/transport/sctp.test.ts "stop discards unsent messages and keeps them in bufferedAmount"
495
1148
  discardUnsentMessages() {
496
1149
  const kept = new common_1.FifoQueue();
497
1150
  for (const item of this.dataChannelQueue.toArray()) {
498
1151
  const [channel, protocol, userData] = item;
499
- if (protocol !== const_1.WEBRTC_DCEP) {
500
- channel.addBufferedAmount(-userData.length);
501
- }
502
- else if (userData[0] === const_1.DATA_CHANNEL_OPEN &&
1152
+ if (protocol === const_1.WEBRTC_DCEP &&
1153
+ userData[0] === const_1.DATA_CHANNEL_OPEN &&
503
1154
  channel.readyState !== "closed" &&
504
1155
  channel.readyState !== "closing") {
505
1156
  kept.push(item);
506
1157
  }
507
1158
  }
508
1159
  this.dataChannelQueue = kept;
1160
+ this.closeAfterFlush.clear();
1161
+ for (const channel of this.unannounced) {
1162
+ if (channel.readyState === "closed" || channel.readyState === "closing") {
1163
+ this.unannounced.delete(channel);
1164
+ }
1165
+ }
509
1166
  }
1167
+ /**
1168
+ * Reset our outgoing stream (RFC 8831 section 6.7), which closes the
1169
+ * peer's channel on it.
1170
+ * @see tests/issue/815.test.ts "issue 815: a DATA_CHANNEL_OPEN violating the odd/even rule on an unused stream"
1171
+ */
1172
+ resetStream(streamId) {
1173
+ if (!this.sctp.reconfigQueue.includes(streamId)) {
1174
+ this.sctp.reconfigQueue.push(streamId);
1175
+ }
1176
+ if (this.sctp.reconfigQueue.length === 1) {
1177
+ this.sctp.transmitReconfigRequest();
1178
+ }
1179
+ }
1180
+ /**
1181
+ * RFC 8832 section 6: an OPEN on a stream in use is not acknowledged and
1182
+ * the stream's channel closes (both ends claimed it).
1183
+ * @see tests/issue/815.test.ts "is not acknowledged and the stream is reset, so neither channel stays connecting"
1184
+ */
1185
+ rejectOpenOnStreamInUse(channel) {
1186
+ if (this.unannounced.delete(channel)) {
1187
+ this.dataChannelQueue = this.dataChannelQueue.filter(([queued]) => queued !== channel);
1188
+ }
1189
+ this.dataChannelClose(channel);
1190
+ }
1191
+ /**
1192
+ * What {@link RTCDataChannel.close} calls; use that instead.
1193
+ * @see tests/datachannel/closeDelivery.test.ts "close() right after a burst (%o): every message arrives in order before close fires on the sender"
1194
+ * @see tests/datachannel/closeDelivery.test.ts "messages sent before close() while connecting reach the peer, then both ends close"
1195
+ * @see tests/datachannel/closeDelivery.test.ts "pc.close() before the association is established closes the channel and drops its messages"
1196
+ * @see tests/datachannel/closeDelivery.test.ts "an in-band channel closed before its DATA_CHANNEL_OPEN is sent closes at once and sends nothing"
1197
+ */
510
1198
  dataChannelClose(channel) {
511
1199
  if (!["closing", "closed"].includes(channel.readyState)) {
512
- channel.setReadyState("closing");
513
- if (this.sctp.associationState === src_1.SCTP_STATE.ESTABLISHED) {
514
- // Check if channel.id is already in reconfigQueue to prevent duplicates
515
- if (!this.sctp.reconfigQueue.includes(channel.id)) {
516
- this.sctp.reconfigQueue.push(channel.id);
517
- }
518
- if (this.sctp.reconfigQueue.length === 1) {
519
- this.sctp.transmitReconfigRequest();
520
- }
1200
+ channel.setReadyState("closing", { notifyClosing: false });
1201
+ // Test: tests/datachannel/closeDelivery.test.ts "pc.close() inside a stateChange subscriber of close() leaves the channel closed without error"
1202
+ if (channel.readyState !== "closing")
1203
+ return;
1204
+ const associationEnded = this.stopped || this.sctp.state === "closed";
1205
+ if (this.sctp.associationState === src_1.SCTP_STATE.ESTABLISHED &&
1206
+ !this.unannounced.has(channel)) {
1207
+ this.dataChannelFlush();
1208
+ this.resetStream(channel.id);
1209
+ }
1210
+ else if (!associationEnded &&
1211
+ !this.unannounced.has(channel) &&
1212
+ channel.id != undefined &&
1213
+ this.dataChannels[channel.id] === channel) {
1214
+ // Test: tests/datachannel/closeDelivery.test.ts "messages sent before close() while connecting reach the peer, then both ends close"
1215
+ this.closeAfterFlush.add(channel);
521
1216
  }
522
1217
  else {
523
- this.dataChannelQueue = this.dataChannelQueue.filter((queueItem) => queueItem[0].id !== channel.id);
1218
+ // Test: tests/issue/815.test.ts "closing one channel before negotiation keeps the OPEN of another (queue filtered by identity)"
1219
+ this.dataChannelQueue = this.dataChannelQueue.filter((queueItem) => queueItem[0] !== channel);
1220
+ this.unannounced.delete(channel);
524
1221
  // Remove any pending reconfiguration requests for this channel
525
1222
  this.sctp.reconfigQueue = this.sctp.reconfigQueue.filter((streamId) => streamId !== channel.id);
526
- if (channel.id !== undefined) {
1223
+ if (channel.id != undefined &&
1224
+ this.dataChannels[channel.id] === channel) {
527
1225
  delete this.dataChannels[channel.id];
528
1226
  }
529
1227
  channel.setReadyState("closed");