werift 0.28.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 (492) hide show
  1. package/README.md +182 -2
  2. package/lib/common/src/transport.d.ts +39 -5
  3. package/lib/common/src/transport.js +32 -7
  4. package/lib/common/src/transport.js.map +1 -1
  5. package/lib/dtls/src/carrier/direct.d.ts +67 -0
  6. package/lib/dtls/src/carrier/direct.js +226 -0
  7. package/lib/dtls/src/carrier/direct.js.map +1 -0
  8. package/lib/dtls/src/carrier/types.d.ts +81 -0
  9. package/lib/dtls/src/carrier/types.js +7 -0
  10. package/lib/dtls/src/carrier/types.js.map +1 -0
  11. package/lib/dtls/src/cipher/const.d.ts +6 -0
  12. package/lib/dtls/src/cipher/const.js +13 -2
  13. package/lib/dtls/src/cipher/const.js.map +1 -1
  14. package/lib/dtls/src/cipher/prf.js +11 -2
  15. package/lib/dtls/src/cipher/prf.js.map +1 -1
  16. package/lib/dtls/src/cipher/tls13/aead.d.ts +41 -0
  17. package/lib/dtls/src/cipher/tls13/aead.js +128 -0
  18. package/lib/dtls/src/cipher/tls13/aead.js.map +1 -0
  19. package/lib/dtls/src/cipher/tls13/hkdf.d.ts +30 -0
  20. package/lib/dtls/src/cipher/tls13/hkdf.js +76 -0
  21. package/lib/dtls/src/cipher/tls13/hkdf.js.map +1 -0
  22. package/lib/dtls/src/cipher/tls13/keySchedule.d.ts +70 -0
  23. package/lib/dtls/src/cipher/tls13/keySchedule.js +114 -0
  24. package/lib/dtls/src/cipher/tls13/keySchedule.js.map +1 -0
  25. package/lib/dtls/src/cipher/tls13/signature.d.ts +30 -0
  26. package/lib/dtls/src/cipher/tls13/signature.js +149 -0
  27. package/lib/dtls/src/cipher/tls13/signature.js.map +1 -0
  28. package/lib/dtls/src/client.d.ts +253 -0
  29. package/lib/dtls/src/client.js +1257 -16
  30. package/lib/dtls/src/client.js.map +1 -1
  31. package/lib/dtls/src/context/dtls.d.ts +45 -0
  32. package/lib/dtls/src/context/dtls.js +114 -0
  33. package/lib/dtls/src/context/dtls.js.map +1 -1
  34. package/lib/dtls/src/context/transport.d.ts +13 -0
  35. package/lib/dtls/src/context/transport.js +29 -1
  36. package/lib/dtls/src/context/transport.js.map +1 -1
  37. package/lib/dtls/src/engine/v1_3/connection-base.d.ts +520 -0
  38. package/lib/dtls/src/engine/v1_3/connection-base.js +1644 -0
  39. package/lib/dtls/src/engine/v1_3/connection-base.js.map +1 -0
  40. package/lib/dtls/src/engine/v1_3/connection.d.ts +181 -0
  41. package/lib/dtls/src/engine/v1_3/connection.js +680 -0
  42. package/lib/dtls/src/engine/v1_3/connection.js.map +1 -0
  43. package/lib/dtls/src/engine/v1_3/early-data-buffer.d.ts +38 -0
  44. package/lib/dtls/src/engine/v1_3/early-data-buffer.js +166 -0
  45. package/lib/dtls/src/engine/v1_3/early-data-buffer.js.map +1 -0
  46. package/lib/dtls/src/engine/v1_3/flight/certificate.d.ts +4 -0
  47. package/lib/dtls/src/engine/v1_3/flight/certificate.js +62 -0
  48. package/lib/dtls/src/engine/v1_3/flight/certificate.js.map +1 -0
  49. package/lib/dtls/src/engine/v1_3/flight/client/flight1.d.ts +8 -0
  50. package/lib/dtls/src/engine/v1_3/flight/client/flight1.js +119 -0
  51. package/lib/dtls/src/engine/v1_3/flight/client/flight1.js.map +1 -0
  52. package/lib/dtls/src/engine/v1_3/flight/client/flight4.d.ts +8 -0
  53. package/lib/dtls/src/engine/v1_3/flight/client/flight4.js +251 -0
  54. package/lib/dtls/src/engine/v1_3/flight/client/flight4.js.map +1 -0
  55. package/lib/dtls/src/engine/v1_3/flight/client/flight5.d.ts +5 -0
  56. package/lib/dtls/src/engine/v1_3/flight/client/flight5.js +99 -0
  57. package/lib/dtls/src/engine/v1_3/flight/client/flight5.js.map +1 -0
  58. package/lib/dtls/src/engine/v1_3/flight/dispatch.d.ts +12 -0
  59. package/lib/dtls/src/engine/v1_3/flight/dispatch.js +138 -0
  60. package/lib/dtls/src/engine/v1_3/flight/dispatch.js.map +1 -0
  61. package/lib/dtls/src/engine/v1_3/flight/extensions.d.ts +13 -0
  62. package/lib/dtls/src/engine/v1_3/flight/extensions.js +46 -0
  63. package/lib/dtls/src/engine/v1_3/flight/extensions.js.map +1 -0
  64. package/lib/dtls/src/engine/v1_3/flight/finished.d.ts +6 -0
  65. package/lib/dtls/src/engine/v1_3/flight/finished.js +16 -0
  66. package/lib/dtls/src/engine/v1_3/flight/finished.js.map +1 -0
  67. package/lib/dtls/src/engine/v1_3/flight/post-hs.d.ts +12 -0
  68. package/lib/dtls/src/engine/v1_3/flight/post-hs.js +94 -0
  69. package/lib/dtls/src/engine/v1_3/flight/post-hs.js.map +1 -0
  70. package/lib/dtls/src/engine/v1_3/flight/server/flight2.d.ts +21 -0
  71. package/lib/dtls/src/engine/v1_3/flight/server/flight2.js +227 -0
  72. package/lib/dtls/src/engine/v1_3/flight/server/flight2.js.map +1 -0
  73. package/lib/dtls/src/engine/v1_3/flight/server/flight4.d.ts +6 -0
  74. package/lib/dtls/src/engine/v1_3/flight/server/flight4.js +455 -0
  75. package/lib/dtls/src/engine/v1_3/flight/server/flight4.js.map +1 -0
  76. package/lib/dtls/src/engine/v1_3/flight/server/flight5.d.ts +5 -0
  77. package/lib/dtls/src/engine/v1_3/flight/server/flight5.js +48 -0
  78. package/lib/dtls/src/engine/v1_3/flight/server/flight5.js.map +1 -0
  79. package/lib/dtls/src/engine/v1_3/flight-tx.d.ts +119 -0
  80. package/lib/dtls/src/engine/v1_3/flight-tx.js +782 -0
  81. package/lib/dtls/src/engine/v1_3/flight-tx.js.map +1 -0
  82. package/lib/dtls/src/engine/v1_3/host.d.ts +86 -0
  83. package/lib/dtls/src/engine/v1_3/host.js +11 -0
  84. package/lib/dtls/src/engine/v1_3/host.js.map +1 -0
  85. package/lib/dtls/src/engine/v1_3/record-rx.d.ts +77 -0
  86. package/lib/dtls/src/engine/v1_3/record-rx.js +818 -0
  87. package/lib/dtls/src/engine/v1_3/record-rx.js.map +1 -0
  88. package/lib/dtls/src/engine/v1_3/transcript.d.ts +22 -0
  89. package/lib/dtls/src/engine/v1_3/transcript.js +65 -0
  90. package/lib/dtls/src/engine/v1_3/transcript.js.map +1 -0
  91. package/lib/dtls/src/engine/v1_3/types.d.ts +169 -0
  92. package/lib/dtls/src/engine/v1_3/types.js +113 -0
  93. package/lib/dtls/src/engine/v1_3/types.js.map +1 -0
  94. package/lib/dtls/src/flight/client/flight1.d.ts +7 -1
  95. package/lib/dtls/src/flight/client/flight1.js +9 -3
  96. package/lib/dtls/src/flight/client/flight1.js.map +1 -1
  97. package/lib/dtls/src/flight/client/flight3.d.ts +8 -0
  98. package/lib/dtls/src/flight/client/flight3.js +24 -4
  99. package/lib/dtls/src/flight/client/flight3.js.map +1 -1
  100. package/lib/dtls/src/flight/client/flight5.js +13 -1
  101. package/lib/dtls/src/flight/client/flight5.js.map +1 -1
  102. package/lib/dtls/src/flight/flight.d.ts +16 -0
  103. package/lib/dtls/src/flight/flight.js +109 -8
  104. package/lib/dtls/src/flight/flight.js.map +1 -1
  105. package/lib/dtls/src/flight/server/commitClientHello.d.ts +35 -0
  106. package/lib/dtls/src/flight/server/commitClientHello.js +149 -0
  107. package/lib/dtls/src/flight/server/commitClientHello.js.map +1 -0
  108. package/lib/dtls/src/flight/server/flight2.d.ts +12 -3
  109. package/lib/dtls/src/flight/server/flight2.js +29 -105
  110. package/lib/dtls/src/flight/server/flight2.js.map +1 -1
  111. package/lib/dtls/src/flight/server/flight4.js +14 -2
  112. package/lib/dtls/src/flight/server/flight4.js.map +1 -1
  113. package/lib/dtls/src/flight/server/flight6.js +8 -0
  114. package/lib/dtls/src/flight/server/flight6.js.map +1 -1
  115. package/lib/dtls/src/handshake/const.d.ts +10 -0
  116. package/lib/dtls/src/handshake/const.js +10 -0
  117. package/lib/dtls/src/handshake/const.js.map +1 -1
  118. package/lib/dtls/src/handshake/extensions/cookie.d.ts +113 -0
  119. package/lib/dtls/src/handshake/extensions/cookie.js +326 -0
  120. package/lib/dtls/src/handshake/extensions/cookie.js.map +1 -0
  121. package/lib/dtls/src/handshake/extensions/ids.d.ts +7 -0
  122. package/lib/dtls/src/handshake/extensions/ids.js +11 -0
  123. package/lib/dtls/src/handshake/extensions/ids.js.map +1 -0
  124. package/lib/dtls/src/handshake/extensions/keyShare.d.ts +28 -0
  125. package/lib/dtls/src/handshake/extensions/keyShare.js +156 -0
  126. package/lib/dtls/src/handshake/extensions/keyShare.js.map +1 -0
  127. package/lib/dtls/src/handshake/extensions/signatureAlgorithms.d.ts +27 -0
  128. package/lib/dtls/src/handshake/extensions/signatureAlgorithms.js +67 -0
  129. package/lib/dtls/src/handshake/extensions/signatureAlgorithms.js.map +1 -0
  130. package/lib/dtls/src/handshake/extensions/supportedVersions.d.ts +20 -0
  131. package/lib/dtls/src/handshake/extensions/supportedVersions.js +90 -0
  132. package/lib/dtls/src/handshake/extensions/supportedVersions.js.map +1 -0
  133. package/lib/dtls/src/handshake/extensions/unique.d.ts +7 -0
  134. package/lib/dtls/src/handshake/extensions/unique.js +19 -0
  135. package/lib/dtls/src/handshake/extensions/unique.js.map +1 -0
  136. package/lib/dtls/src/handshake/extensions/useSrtp.d.ts +26 -3
  137. package/lib/dtls/src/handshake/extensions/useSrtp.js +68 -12
  138. package/lib/dtls/src/handshake/extensions/useSrtp.js.map +1 -1
  139. package/lib/dtls/src/handshake/message/tls13/ack.d.ts +50 -0
  140. package/lib/dtls/src/handshake/message/tls13/ack.js +107 -0
  141. package/lib/dtls/src/handshake/message/tls13/ack.js.map +1 -0
  142. package/lib/dtls/src/handshake/message/tls13/certificate.d.ts +17 -0
  143. package/lib/dtls/src/handshake/message/tls13/certificate.js +112 -0
  144. package/lib/dtls/src/handshake/message/tls13/certificate.js.map +1 -0
  145. package/lib/dtls/src/handshake/message/tls13/certificateRequest.d.ts +17 -0
  146. package/lib/dtls/src/handshake/message/tls13/certificateRequest.js +99 -0
  147. package/lib/dtls/src/handshake/message/tls13/certificateRequest.js.map +1 -0
  148. package/lib/dtls/src/handshake/message/tls13/certificateVerify.d.ts +21 -0
  149. package/lib/dtls/src/handshake/message/tls13/certificateVerify.js +77 -0
  150. package/lib/dtls/src/handshake/message/tls13/certificateVerify.js.map +1 -0
  151. package/lib/dtls/src/handshake/message/tls13/encryptedExtensions.d.ts +19 -0
  152. package/lib/dtls/src/handshake/message/tls13/encryptedExtensions.js +56 -0
  153. package/lib/dtls/src/handshake/message/tls13/encryptedExtensions.js.map +1 -0
  154. package/lib/dtls/src/handshake/message/tls13/keyUpdate.d.ts +13 -0
  155. package/lib/dtls/src/handshake/message/tls13/keyUpdate.js +54 -0
  156. package/lib/dtls/src/handshake/message/tls13/keyUpdate.js.map +1 -0
  157. package/lib/dtls/src/handshake/message/tls13/newSessionTicket.d.ts +33 -0
  158. package/lib/dtls/src/handshake/message/tls13/newSessionTicket.js +158 -0
  159. package/lib/dtls/src/handshake/message/tls13/newSessionTicket.js.map +1 -0
  160. package/lib/dtls/src/handshake/random.d.ts +14 -0
  161. package/lib/dtls/src/handshake/random.js +24 -0
  162. package/lib/dtls/src/handshake/random.js.map +1 -1
  163. package/lib/dtls/src/handshake/validation.d.ts +26 -0
  164. package/lib/dtls/src/handshake/validation.js +32 -0
  165. package/lib/dtls/src/handshake/validation.js.map +1 -0
  166. package/lib/dtls/src/index.d.ts +6 -1
  167. package/lib/dtls/src/index.js +116 -44
  168. package/lib/dtls/src/index.js.map +1 -1
  169. package/lib/dtls/src/internal.d.ts +7 -0
  170. package/lib/dtls/src/internal.js +57 -0
  171. package/lib/dtls/src/internal.js.map +1 -0
  172. package/lib/dtls/src/peer.d.ts +49 -0
  173. package/lib/dtls/src/peer.js +54 -0
  174. package/lib/dtls/src/peer.js.map +1 -0
  175. package/lib/dtls/src/record/antiReplayWindow.d.ts +33 -0
  176. package/lib/dtls/src/record/antiReplayWindow.js +119 -0
  177. package/lib/dtls/src/record/antiReplayWindow.js.map +1 -0
  178. package/lib/dtls/src/record/const.d.ts +6 -0
  179. package/lib/dtls/src/record/const.js +6 -0
  180. package/lib/dtls/src/record/const.js.map +1 -1
  181. package/lib/dtls/src/record/message/fragment.d.ts +0 -2
  182. package/lib/dtls/src/record/message/fragment.js +36 -17
  183. package/lib/dtls/src/record/message/fragment.js.map +1 -1
  184. package/lib/dtls/src/record/message/reassembler.d.ts +62 -0
  185. package/lib/dtls/src/record/message/reassembler.js +249 -0
  186. package/lib/dtls/src/record/message/reassembler.js.map +1 -0
  187. package/lib/dtls/src/record/receive.js +24 -16
  188. package/lib/dtls/src/record/receive.js.map +1 -1
  189. package/lib/dtls/src/record/v1_3/header.d.ts +34 -0
  190. package/lib/dtls/src/record/v1_3/header.js +95 -0
  191. package/lib/dtls/src/record/v1_3/header.js.map +1 -0
  192. package/lib/dtls/src/record/v1_3/record.d.ts +133 -0
  193. package/lib/dtls/src/record/v1_3/record.js +370 -0
  194. package/lib/dtls/src/record/v1_3/record.js.map +1 -0
  195. package/lib/dtls/src/retransmission.d.ts +26 -0
  196. package/lib/dtls/src/retransmission.js +39 -0
  197. package/lib/dtls/src/retransmission.js.map +1 -0
  198. package/lib/dtls/src/server.d.ts +38 -0
  199. package/lib/dtls/src/server.js +367 -20
  200. package/lib/dtls/src/server.js.map +1 -1
  201. package/lib/dtls/src/socket.d.ts +379 -35
  202. package/lib/dtls/src/socket.js +1105 -132
  203. package/lib/dtls/src/socket.js.map +1 -1
  204. package/lib/dtls/src/version.d.ts +89 -0
  205. package/lib/dtls/src/version.js +204 -0
  206. package/lib/dtls/src/version.js.map +1 -0
  207. package/lib/ice/src/consent.d.ts +78 -1
  208. package/lib/ice/src/consent.js +236 -24
  209. package/lib/ice/src/consent.js.map +1 -1
  210. package/lib/ice/src/dns/lookup.d.ts +10 -0
  211. package/lib/ice/src/dns/lookup.js +130 -3
  212. package/lib/ice/src/dns/lookup.js.map +1 -1
  213. package/lib/ice/src/exceptions.d.ts +9 -0
  214. package/lib/ice/src/exceptions.js +14 -1
  215. package/lib/ice/src/exceptions.js.map +1 -1
  216. package/lib/ice/src/failover.d.ts +270 -0
  217. package/lib/ice/src/failover.js +674 -0
  218. package/lib/ice/src/failover.js.map +1 -0
  219. package/lib/ice/src/gatheringError.d.ts +28 -0
  220. package/lib/ice/src/gatheringError.js +17 -0
  221. package/lib/ice/src/gatheringError.js.map +1 -0
  222. package/lib/ice/src/ice.d.ts +265 -9
  223. package/lib/ice/src/ice.js +1624 -236
  224. package/lib/ice/src/ice.js.map +1 -1
  225. package/lib/ice/src/iceBase.d.ts +197 -6
  226. package/lib/ice/src/iceBase.js +157 -7
  227. package/lib/ice/src/iceBase.js.map +1 -1
  228. package/lib/ice/src/index.d.ts +2 -0
  229. package/lib/ice/src/index.js +4 -1
  230. package/lib/ice/src/index.js.map +1 -1
  231. package/lib/ice/src/internal/datagram.d.ts +37 -0
  232. package/lib/ice/src/internal/datagram.js +45 -0
  233. package/lib/ice/src/internal/datagram.js.map +1 -0
  234. package/lib/ice/src/internal/hostInterfaces.d.ts +13 -2
  235. package/lib/ice/src/internal/hostInterfaces.js +16 -4
  236. package/lib/ice/src/internal/hostInterfaces.js.map +1 -1
  237. package/lib/ice/src/internal/sped-bind.d.ts +44 -0
  238. package/lib/ice/src/internal/sped-bind.js +53 -0
  239. package/lib/ice/src/internal/sped-bind.js.map +1 -0
  240. package/lib/ice/src/internal/sped-binding-cache.d.ts +27 -0
  241. package/lib/ice/src/internal/sped-binding-cache.js +102 -0
  242. package/lib/ice/src/internal/sped-binding-cache.js.map +1 -0
  243. package/lib/ice/src/internal/sped.d.ts +15 -0
  244. package/lib/ice/src/internal/sped.js +62 -0
  245. package/lib/ice/src/internal/sped.js.map +1 -0
  246. package/lib/ice/src/nomination/selectionController.d.ts +111 -0
  247. package/lib/ice/src/nomination/selectionController.js +172 -0
  248. package/lib/ice/src/nomination/selectionController.js.map +1 -0
  249. package/lib/ice/src/nomination/selectionReplacement.d.ts +40 -0
  250. package/lib/ice/src/nomination/selectionReplacement.js +65 -7
  251. package/lib/ice/src/nomination/selectionReplacement.js.map +1 -1
  252. package/lib/ice/src/sped/draft00/codec.d.ts +20 -0
  253. package/lib/ice/src/sped/draft00/codec.js +59 -0
  254. package/lib/ice/src/sped/draft00/codec.js.map +1 -0
  255. package/lib/ice/src/sped/draft00/constants.d.ts +14 -0
  256. package/lib/ice/src/sped/draft00/constants.js +18 -0
  257. package/lib/ice/src/sped/draft00/constants.js.map +1 -0
  258. package/lib/ice/src/sped/draft00/index.d.ts +6 -0
  259. package/lib/ice/src/sped/draft00/index.js +26 -0
  260. package/lib/ice/src/sped/draft00/index.js.map +1 -0
  261. package/lib/ice/src/sped/draft00/mtu.d.ts +38 -0
  262. package/lib/ice/src/sped/draft00/mtu.js +96 -0
  263. package/lib/ice/src/sped/draft00/mtu.js.map +1 -0
  264. package/lib/ice/src/sped/draft00/session.d.ts +86 -0
  265. package/lib/ice/src/sped/draft00/session.js +353 -0
  266. package/lib/ice/src/sped/draft00/session.js.map +1 -0
  267. package/lib/ice/src/sped/draft00/types.d.ts +16 -0
  268. package/lib/ice/src/sped/draft00/types.js +3 -0
  269. package/lib/ice/src/sped/draft00/types.js.map +1 -0
  270. package/lib/ice/src/sped/runtime.d.ts +188 -0
  271. package/lib/ice/src/sped/runtime.js +643 -0
  272. package/lib/ice/src/sped/runtime.js.map +1 -0
  273. package/lib/ice/src/stun/protocol.d.ts +29 -0
  274. package/lib/ice/src/stun/protocol.js +91 -4
  275. package/lib/ice/src/stun/protocol.js.map +1 -1
  276. package/lib/ice/src/stun/rawAttributeValue.d.ts +1 -0
  277. package/lib/ice/src/stun/rawAttributeValue.js +6 -0
  278. package/lib/ice/src/stun/rawAttributeValue.js.map +1 -0
  279. package/lib/ice/src/stun/tcpProtocol.d.ts +1 -0
  280. package/lib/ice/src/stun/tcpProtocol.js +14 -2
  281. package/lib/ice/src/stun/tcpProtocol.js.map +1 -1
  282. package/lib/ice/src/stun/transaction.d.ts +4 -0
  283. package/lib/ice/src/stun/transaction.js +23 -0
  284. package/lib/ice/src/stun/transaction.js.map +1 -1
  285. package/lib/ice/src/turn/protocol.d.ts +21 -2
  286. package/lib/ice/src/turn/protocol.js +93 -8
  287. package/lib/ice/src/turn/protocol.js.map +1 -1
  288. package/lib/ice/src/types/model.d.ts +9 -0
  289. package/lib/ice/src/types/model.js.map +1 -1
  290. package/lib/ice/src/utils.d.ts +13 -0
  291. package/lib/ice/src/utils.js +13 -0
  292. package/lib/ice/src/utils.js.map +1 -1
  293. package/lib/ice-server/src/stun/message.d.ts +15 -2
  294. package/lib/ice-server/src/stun/message.js +156 -25
  295. package/lib/ice-server/src/stun/message.js.map +1 -1
  296. package/lib/ice-server/src/stun/rawAttributeValue.d.ts +6 -0
  297. package/lib/ice-server/src/stun/rawAttributeValue.js +12 -0
  298. package/lib/ice-server/src/stun/rawAttributeValue.js.map +1 -0
  299. package/lib/index.mjs +19568 -3106
  300. package/lib/nonstandard/index.mjs +2539 -1175
  301. package/lib/polyfill/dom.mjs +33210 -16872
  302. package/lib/polyfill/index.mjs +33345 -17013
  303. package/lib/rtp/src/extra/processor/fsRetry.d.ts +20 -0
  304. package/lib/rtp/src/extra/processor/fsRetry.js +73 -0
  305. package/lib/rtp/src/extra/processor/fsRetry.js.map +1 -0
  306. package/lib/rtp/src/extra/processor/webm.d.ts +6 -0
  307. package/lib/rtp/src/extra/processor/webm.js +6 -2
  308. package/lib/rtp/src/extra/processor/webm.js.map +1 -1
  309. package/lib/rtp/src/extra/processor/webmCallback.d.ts +11 -0
  310. package/lib/rtp/src/extra/processor/webmCallback.js +23 -31
  311. package/lib/rtp/src/extra/processor/webmCallback.js.map +1 -1
  312. package/lib/rtp/src/extra/processor/webmFileWriter.d.ts +24 -0
  313. package/lib/rtp/src/extra/processor/webmFileWriter.js +94 -0
  314. package/lib/rtp/src/extra/processor/webmFileWriter.js.map +1 -0
  315. package/lib/sctp/src/chunk.d.ts +5 -0
  316. package/lib/sctp/src/chunk.js +10 -0
  317. package/lib/sctp/src/chunk.js.map +1 -1
  318. package/lib/sctp/src/index.d.ts +1 -1
  319. package/lib/sctp/src/index.js +4 -1
  320. package/lib/sctp/src/index.js.map +1 -1
  321. package/lib/sctp/src/sctp.d.ts +81 -5
  322. package/lib/sctp/src/sctp.js +273 -28
  323. package/lib/sctp/src/sctp.js.map +1 -1
  324. package/lib/webrtc/src/api/compatibilityOptions.d.ts +11 -1
  325. package/lib/webrtc/src/api/compatibilityOptions.js +11 -1
  326. package/lib/webrtc/src/api/compatibilityOptions.js.map +1 -1
  327. package/lib/webrtc/src/api/peerConfig.d.ts +210 -13
  328. package/lib/webrtc/src/api/peerConfig.js +100 -5
  329. package/lib/webrtc/src/api/peerConfig.js.map +1 -1
  330. package/lib/webrtc/src/api/peerConnectionEvents.d.ts +10 -1
  331. package/lib/webrtc/src/api/peerConnectionEvents.js.map +1 -1
  332. package/lib/webrtc/src/dataChannel.d.ts +122 -10
  333. package/lib/webrtc/src/dataChannel.js +214 -20
  334. package/lib/webrtc/src/dataChannel.js.map +1 -1
  335. package/lib/webrtc/src/media/domEventTarget.d.ts +55 -0
  336. package/lib/webrtc/src/media/domEventTarget.js +216 -0
  337. package/lib/webrtc/src/media/domEventTarget.js.map +1 -0
  338. package/lib/webrtc/src/media/index.d.ts +2 -0
  339. package/lib/webrtc/src/media/index.js +1 -0
  340. package/lib/webrtc/src/media/index.js.map +1 -1
  341. package/lib/webrtc/src/media/parameters.d.ts +5 -0
  342. package/lib/webrtc/src/media/parameters.js +10 -0
  343. package/lib/webrtc/src/media/parameters.js.map +1 -1
  344. package/lib/webrtc/src/media/receiver/receiverTwcc.d.ts +10 -0
  345. package/lib/webrtc/src/media/receiver/receiverTwcc.js +4 -10
  346. package/lib/webrtc/src/media/receiver/receiverTwcc.js.map +1 -1
  347. package/lib/webrtc/src/media/router.d.ts +6 -0
  348. package/lib/webrtc/src/media/router.js +39 -1
  349. package/lib/webrtc/src/media/router.js.map +1 -1
  350. package/lib/webrtc/src/media/rtpReceiver.d.ts +56 -6
  351. package/lib/webrtc/src/media/rtpReceiver.js +165 -15
  352. package/lib/webrtc/src/media/rtpReceiver.js.map +1 -1
  353. package/lib/webrtc/src/media/rtpSender.d.ts +1 -1
  354. package/lib/webrtc/src/media/rtpSender.js +4 -3
  355. package/lib/webrtc/src/media/rtpSender.js.map +1 -1
  356. package/lib/webrtc/src/media/rtpTransceiver.d.ts +29 -11
  357. package/lib/webrtc/src/media/rtpTransceiver.js +31 -9
  358. package/lib/webrtc/src/media/rtpTransceiver.js.map +1 -1
  359. package/lib/webrtc/src/media/stats.d.ts +13 -0
  360. package/lib/webrtc/src/media/stats.js.map +1 -1
  361. package/lib/webrtc/src/media/track.d.ts +103 -11
  362. package/lib/webrtc/src/media/track.js +241 -19
  363. package/lib/webrtc/src/media/track.js.map +1 -1
  364. package/lib/webrtc/src/media/trackConstraints.d.ts +80 -0
  365. package/lib/webrtc/src/media/trackConstraints.js +141 -0
  366. package/lib/webrtc/src/media/trackConstraints.js.map +1 -0
  367. package/lib/webrtc/src/negotiation/internalState.d.ts +0 -4
  368. package/lib/webrtc/src/negotiation/internalState.js +0 -11
  369. package/lib/webrtc/src/negotiation/internalState.js.map +1 -1
  370. package/lib/webrtc/src/negotiation/remoteCandidates.d.ts +20 -1
  371. package/lib/webrtc/src/negotiation/remoteCandidates.js +29 -1
  372. package/lib/webrtc/src/negotiation/remoteCandidates.js.map +1 -1
  373. package/lib/webrtc/src/negotiation/transportActivation.d.ts +4 -0
  374. package/lib/webrtc/src/negotiation/transportActivation.js +112 -11
  375. package/lib/webrtc/src/negotiation/transportActivation.js.map +1 -1
  376. package/lib/webrtc/src/negotiationTransaction.d.ts +25 -4
  377. package/lib/webrtc/src/negotiationTransaction.js +27 -6
  378. package/lib/webrtc/src/negotiationTransaction.js.map +1 -1
  379. package/lib/webrtc/src/nonstandard/dummyMedia.d.ts +6 -0
  380. package/lib/webrtc/src/nonstandard/dummyMedia.js +14 -19
  381. package/lib/webrtc/src/nonstandard/dummyMedia.js.map +1 -1
  382. package/lib/webrtc/src/nonstandard/navigator.d.ts +1 -1
  383. package/lib/webrtc/src/nonstandard/recorder/index.js +5 -1
  384. package/lib/webrtc/src/nonstandard/recorder/index.js.map +1 -1
  385. package/lib/webrtc/src/nonstandard/recorder/writer/webm.d.ts +6 -1
  386. package/lib/webrtc/src/nonstandard/recorder/writer/webm.js +31 -2
  387. package/lib/webrtc/src/nonstandard/recorder/writer/webm.js.map +1 -1
  388. package/lib/webrtc/src/nonstandard/userMedia/packetizer.d.ts +16 -0
  389. package/lib/webrtc/src/nonstandard/userMedia/packetizer.js +25 -0
  390. package/lib/webrtc/src/nonstandard/userMedia/packetizer.js.map +1 -1
  391. package/lib/webrtc/src/nonstandard/userMedia.js +6 -13
  392. package/lib/webrtc/src/nonstandard/userMedia.js.map +1 -1
  393. package/lib/webrtc/src/peerConnection.d.ts +44 -8
  394. package/lib/webrtc/src/peerConnection.js +88 -9
  395. package/lib/webrtc/src/peerConnection.js.map +1 -1
  396. package/lib/webrtc/src/polyfill/api.d.ts +2 -1
  397. package/lib/webrtc/src/polyfill/api.js +3 -3
  398. package/lib/webrtc/src/polyfill/api.js.map +1 -1
  399. package/lib/webrtc/src/polyfill/dom.d.ts +21 -7
  400. package/lib/webrtc/src/polyfill/dom.js +15 -0
  401. package/lib/webrtc/src/polyfill/dom.js.map +1 -1
  402. package/lib/webrtc/src/polyfill/globals.d.ts +6 -2
  403. package/lib/webrtc/src/polyfill/globals.js.map +1 -1
  404. package/lib/webrtc/src/polyfill/index.d.ts +1 -1
  405. package/lib/webrtc/src/polyfill/index.js +1 -1
  406. package/lib/webrtc/src/polyfill/index.js.map +1 -1
  407. package/lib/webrtc/src/polyfill/install.d.ts +5 -1
  408. package/lib/webrtc/src/polyfill/install.js +2 -2
  409. package/lib/webrtc/src/polyfill/install.js.map +1 -1
  410. package/lib/webrtc/src/polyfill/mediaDevices.js +28 -4
  411. package/lib/webrtc/src/polyfill/mediaDevices.js.map +1 -1
  412. package/lib/webrtc/src/polyfill/peerConnectionConfig.d.ts +6 -0
  413. package/lib/webrtc/src/polyfill/peerConnectionConfig.js +15 -4
  414. package/lib/webrtc/src/polyfill/peerConnectionConfig.js.map +1 -1
  415. package/lib/webrtc/src/polyfill/selectSettings.d.ts +4 -14
  416. package/lib/webrtc/src/polyfill/selectSettings.js +10 -106
  417. package/lib/webrtc/src/polyfill/selectSettings.js.map +1 -1
  418. package/lib/webrtc/src/polyfill/sourceIo.d.ts +1 -2
  419. package/lib/webrtc/src/polyfill/sourceIo.js +6 -6
  420. package/lib/webrtc/src/polyfill/sourceIo.js.map +1 -1
  421. package/lib/webrtc/src/sctpManager.d.ts +31 -18
  422. package/lib/webrtc/src/sctpManager.js +158 -38
  423. package/lib/webrtc/src/sctpManager.js.map +1 -1
  424. package/lib/webrtc/src/sdp.d.ts +38 -2
  425. package/lib/webrtc/src/sdp.js +84 -3
  426. package/lib/webrtc/src/sdp.js.map +1 -1
  427. package/lib/webrtc/src/sdpManager.d.ts +19 -9
  428. package/lib/webrtc/src/sdpManager.js +26 -4
  429. package/lib/webrtc/src/sdpManager.js.map +1 -1
  430. package/lib/webrtc/src/secureTransportManager.d.ts +11 -2
  431. package/lib/webrtc/src/secureTransportManager.js +84 -5
  432. package/lib/webrtc/src/secureTransportManager.js.map +1 -1
  433. package/lib/webrtc/src/transceiverManager.d.ts +5 -2
  434. package/lib/webrtc/src/transceiverManager.js +42 -14
  435. package/lib/webrtc/src/transceiverManager.js.map +1 -1
  436. package/lib/webrtc/src/transport/dtls-application-gate.d.ts +1 -0
  437. package/lib/webrtc/src/transport/dtls-application-gate.js +143 -0
  438. package/lib/webrtc/src/transport/dtls-application-gate.js.map +1 -0
  439. package/lib/webrtc/src/transport/dtls-attempt.d.ts +1 -0
  440. package/lib/webrtc/src/transport/dtls-attempt.js +79 -0
  441. package/lib/webrtc/src/transport/dtls-attempt.js.map +1 -0
  442. package/lib/webrtc/src/transport/dtls-certificate.d.ts +24 -0
  443. package/lib/webrtc/src/transport/dtls-certificate.js +98 -0
  444. package/lib/webrtc/src/transport/dtls-certificate.js.map +1 -0
  445. package/lib/webrtc/src/transport/dtls-fingerprint.d.ts +7 -0
  446. package/lib/webrtc/src/transport/dtls-fingerprint.js +66 -0
  447. package/lib/webrtc/src/transport/dtls-fingerprint.js.map +1 -0
  448. package/lib/webrtc/src/transport/dtls-ice-transport.d.ts +1 -0
  449. package/lib/webrtc/src/transport/dtls-ice-transport.js +123 -0
  450. package/lib/webrtc/src/transport/dtls-ice-transport.js.map +1 -0
  451. package/lib/webrtc/src/transport/dtls-media-queue.d.ts +1 -0
  452. package/lib/webrtc/src/transport/dtls-media-queue.js +95 -0
  453. package/lib/webrtc/src/transport/dtls-media-queue.js.map +1 -0
  454. package/lib/webrtc/src/transport/dtls-readiness.d.ts +1 -0
  455. package/lib/webrtc/src/transport/dtls-readiness.js +173 -0
  456. package/lib/webrtc/src/transport/dtls-readiness.js.map +1 -0
  457. package/lib/webrtc/src/transport/dtls-sped-handshake.d.ts +1 -0
  458. package/lib/webrtc/src/transport/dtls-sped-handshake.js +146 -0
  459. package/lib/webrtc/src/transport/dtls-sped-handshake.js.map +1 -0
  460. package/lib/webrtc/src/transport/dtls-srtp.d.ts +1 -0
  461. package/lib/webrtc/src/transport/dtls-srtp.js +133 -0
  462. package/lib/webrtc/src/transport/dtls-srtp.js.map +1 -0
  463. package/lib/webrtc/src/transport/dtls-stats.d.ts +1 -0
  464. package/lib/webrtc/src/transport/dtls-stats.js +134 -0
  465. package/lib/webrtc/src/transport/dtls-stats.js.map +1 -0
  466. package/lib/webrtc/src/transport/dtls-types.d.ts +23 -0
  467. package/lib/webrtc/src/transport/dtls-types.js +11 -0
  468. package/lib/webrtc/src/transport/dtls-types.js.map +1 -0
  469. package/lib/webrtc/src/transport/dtls.d.ts +150 -55
  470. package/lib/webrtc/src/transport/dtls.js +1017 -556
  471. package/lib/webrtc/src/transport/dtls.js.map +1 -1
  472. package/lib/webrtc/src/transport/ice.d.ts +35 -2
  473. package/lib/webrtc/src/transport/ice.js +147 -14
  474. package/lib/webrtc/src/transport/ice.js.map +1 -1
  475. package/lib/webrtc/src/transport/iceCandidateAttribute.d.ts +4 -2
  476. package/lib/webrtc/src/transport/iceCandidateAttribute.js.map +1 -1
  477. package/lib/webrtc/src/transport/sctp.d.ts +126 -2
  478. package/lib/webrtc/src/transport/sctp.js +747 -134
  479. package/lib/webrtc/src/transport/sctp.js.map +1 -1
  480. package/lib/webrtc/src/transport/sped.d.ts +139 -0
  481. package/lib/webrtc/src/transport/sped.js +757 -0
  482. package/lib/webrtc/src/transport/sped.js.map +1 -0
  483. package/lib/webrtc/src/types/util.d.ts +6 -0
  484. package/lib/webrtc/src/types/util.js.map +1 -1
  485. package/lib/webrtc/src/utils.d.ts +1 -1
  486. package/lib/webrtc/src/webidl.d.ts +15 -0
  487. package/lib/webrtc/src/webidl.js +34 -0
  488. package/lib/webrtc/src/webidl.js.map +1 -0
  489. package/package.json +7 -3
  490. package/lib/webrtc/src/polyfill/rtcSessionDescription.d.ts +0 -18
  491. package/lib/webrtc/src/polyfill/rtcSessionDescription.js +0 -36
  492. package/lib/webrtc/src/polyfill/rtcSessionDescription.js.map +0 -1
@@ -1,14 +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");
8
10
  const helper_1 = require("../helper");
9
11
  const common_1 = require("../imports/common");
10
12
  const log = (0, common_1.debug)("werift:packages/webrtc/src/transport/sctp.ts");
11
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;
12
26
  class RTCSctpTransport {
13
27
  constructor(port = 5000, maxMessageSize = exports.DEFAULT_MAX_MESSAGE_SIZE, sctpOptions = {}) {
14
28
  Object.defineProperty(this, "port", {
@@ -127,11 +141,26 @@ class RTCSctpTransport {
127
141
  writable: true,
128
142
  value: new common_1.FifoQueue()
129
143
  });
130
- 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", {
131
148
  enumerable: true,
132
149
  configurable: true,
133
150
  writable: true,
134
- 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()
135
164
  });
136
165
  Object.defineProperty(this, "eventDisposer", {
137
166
  enumerable: true,
@@ -139,6 +168,64 @@ class RTCSctpTransport {
139
168
  writable: true,
140
169
  value: []
141
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
+ });
142
229
  Object.defineProperty(this, "addEventListener", {
143
230
  enumerable: true,
144
231
  configurable: true,
@@ -173,7 +260,27 @@ class RTCSctpTransport {
173
260
  log("DATA_CHANNEL_OPEN data.length not enough");
174
261
  return;
175
262
  }
176
- 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) {
177
284
  // !BBHLHH — 12 byte DCEP OPEN header (big-endian)
178
285
  const channelType = data.readUInt8(1);
179
286
  const reliability = data.readUInt32BE(4);
@@ -203,20 +310,27 @@ class RTCSctpTransport {
203
310
  protocol,
204
311
  id: streamId,
205
312
  });
206
- const channel = new dataChannel_1.RTCDataChannel(this, parameters, false);
313
+ const channel = new dataChannel_1.RTCDataChannel(this, parameters, false, this.defaultBinaryType());
207
314
  channel.isCreatedByRemote = true;
208
315
  this.dataChannels[streamId] = channel;
209
- this.onDataChannel.execute(channel);
210
- 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;
211
329
  }
212
330
  else {
213
331
  log("datachannel already opened", "retransmit ack");
214
332
  }
215
- const channel = this.dataChannels[streamId];
216
- const ack = Buffer.allocUnsafe(1);
217
- ack.writeUInt8(const_1.DATA_CHANNEL_ACK, 0);
218
- this.dataChannelQueue.push([channel, const_1.WEBRTC_DCEP, ack]);
219
- this.dataChannelFlush();
333
+ this.sendDataChannelAck(existing);
220
334
  }
221
335
  break;
222
336
  case const_1.DATA_CHANNEL_ACK:
@@ -224,9 +338,13 @@ class RTCSctpTransport {
224
338
  log("DATA_CHANNEL_ACK", streamId, ppId);
225
339
  const channel = this.dataChannels[streamId];
226
340
  if (!channel) {
227
- 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");
228
347
  }
229
- channel.setReadyState("open");
230
348
  }
231
349
  break;
232
350
  }
@@ -245,33 +363,74 @@ class RTCSctpTransport {
245
363
  case const_1.WEBRTC_BINARY_EMPTY:
246
364
  return Buffer.from([]);
247
365
  default:
248
- throw new Error();
366
+ return undefined;
249
367
  }
250
368
  })();
251
- // Update reception statistics
252
- channel.messagesReceived++;
253
- channel.bytesReceived += data.length;
254
- channel.onMessage.execute(msg);
255
- channel.emit("message", { data: msg });
256
- if (channel.onmessage) {
257
- 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;
258
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));
259
399
  }
260
400
  }
261
401
  }
262
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
+ */
263
407
  Object.defineProperty(this, "datachannelSend", {
264
408
  enumerable: true,
265
409
  configurable: true,
266
410
  writable: true,
267
411
  value: (channel, data) => {
268
- const userData = Buffer.isBuffer(data) ? data : Buffer.from(data);
269
412
  const size = (0, dataChannel_1.getDataChannelMessageSize)(data);
270
413
  this.assertSendableMessageSize(size);
271
- channel.addBufferedAmount(size);
272
- this.dataChannelQueue.push(typeof data === "string"
273
- ? [channel, const_1.WEBRTC_STRING, userData]
274
- : [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
+ }
275
434
  if (this.sctp.associationState !== src_1.SCTP_STATE.ESTABLISHED) {
276
435
  log("sctp not established", this.sctp.associationState);
277
436
  }
@@ -312,14 +471,14 @@ class RTCSctpTransport {
312
471
  * @internal
313
472
  * Discard the current association (ABORT, attached channels close) and
314
473
  * bind a new, unstarted one (on the same DTLS transport by default).
315
- * Channels still waiting for a stream ID stay queued for the next
316
- * 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"
317
476
  */
318
477
  async resetAssociation(dtlsTransport = this.dtlsTransport) {
478
+ this.releaseUnannouncedStreamIds();
319
479
  await this.stop();
320
- this.dataChannelQueue = this.dataChannelQueue.filter(([channel]) => channel.id === undefined);
321
- this.dataChannelId = undefined;
322
- this.bindAssociation(dtlsTransport);
480
+ this.dataChannelQueue = this.dataChannelQueue.filter(([channel]) => this.unannounced.has(channel));
481
+ this.bindAssociation(dtlsTransport, { assignIds: false });
323
482
  }
324
483
  /**
325
484
  * @internal
@@ -330,70 +489,361 @@ class RTCSctpTransport {
330
489
  this.sctp.state !== "new" ||
331
490
  this.sctp.associationState !== src_1.SCTP_STATE.CLOSED);
332
491
  }
333
- bindAssociation(dtlsTransport) {
334
- 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();
335
501
  this.dtlsTransport = dtlsTransport;
336
- // 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();
337
517
  this.stopped = false;
338
518
  this.setState("connecting", false);
339
519
  this.sctp = new src_1.SCTP(new BridgeDtls(this.dtlsTransport), this.port, this.sctpOptions);
520
+ const association = this.sctp;
340
521
  this.eventDisposer = [
341
522
  ...[
342
- this.sctp.onReceive.subscribe(this.datachannelReceive),
343
- this.sctp.onReconfigStreams.subscribe((ids) => {
344
- ids.forEach((id) => {
345
- const dc = this.dataChannels[id];
346
- if (!dc)
347
- return;
348
- // todo fix
349
- dc.setReadyState("closing");
350
- dc.setReadyState("closed");
351
- delete this.dataChannels[id];
352
- });
353
- }),
354
- 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(() => {
355
526
  this.setState("connected");
356
527
  Object.values(this.dataChannels).forEach((channel) => {
357
- 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") {
358
530
  channel.setReadyState("open");
359
531
  }
360
532
  });
361
533
  this.dataChannelFlush();
362
534
  }),
363
- this.sctp.stateChanged.closed.subscribe(() => {
535
+ association.stateChanged.closed.subscribe(() => {
364
536
  if (!this.stopped)
365
537
  this.setState("closed");
366
- Object.values(this.dataChannels).forEach((dc) => {
367
- dc.setReadyState("closed");
368
- });
369
- this.dataChannels = {};
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();
370
552
  }),
371
- // DTLS closed (close_notify from the peer) or failed (a fatal
372
- // alert): the association and its data channels close. A stopped
373
- // transport is left to stop(): RTCPeerConnection.close() marks DTLS
374
- // closed synchronously, before stop() sends the SCTP ABORT, and
375
- // closing the association here would skip that ABORT.
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
+ })(),
376
596
  this.dtlsTransport.onStateChange.subscribe((state) => {
377
- if (this.stopped)
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();
378
602
  return;
379
- if (state === "closed" || state === "failed") {
380
- this.sctp.setState(src_1.SCTP_STATE.CLOSED);
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();
381
623
  }
382
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
+ }),
383
631
  ].map((e) => e.unSubscribe),
384
- () => (this.sctp.onSackReceived = async () => { }),
632
+ () => (association.onSackReceived = async () => { }),
385
633
  ];
386
- this.sctp.onSackReceived = async () => {
387
- this.dataChannelFlush();
634
+ association.onSackReceived = async () => {
635
+ await this.dataChannelFlush();
388
636
  };
389
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
+ */
390
730
  get isServer() {
391
- const role = this.dtlsTransport.iceRoleAtStart ?? this.dtlsTransport.iceTransport.role;
392
- 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
+ }
393
837
  }
394
838
  channelByLabel(label) {
395
839
  return Object.values(this.dataChannels).find((d) => d.label === label);
396
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
+ }
397
847
  dataChannelAddNegotiated(channel) {
398
848
  if (channel.id == undefined) {
399
849
  throw new Error();
@@ -401,6 +851,10 @@ class RTCSctpTransport {
401
851
  if (this.dataChannels[channel.id]) {
402
852
  throw new Error();
403
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
+ }
404
858
  this.dataChannels[channel.id] = channel;
405
859
  if (this.sctp.associationState === src_1.SCTP_STATE.ESTABLISHED) {
406
860
  channel.setReadyState("open");
@@ -412,11 +866,21 @@ class RTCSctpTransport {
412
866
  // Their 16-bit limit is enforced by the RTCDataChannel constructor.
413
867
  const label = Buffer.from(channel.label, "utf8");
414
868
  const protocol = Buffer.from(channel.protocol, "utf8");
415
- if (channel.id !== undefined) {
416
- if (this.dataChannels[channel.id])
417
- throw new Error(`Data channel with ID ${channel.id} already registered`);
418
- 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);
419
882
  }
883
+ this.unannounced.add(channel);
420
884
  const priority = 0;
421
885
  // RFC 8832 5.1: the reliability mode and the unordered bit (0x80) are
422
886
  // independent, so a partially reliable channel keeps its unordered bit.
@@ -452,43 +916,33 @@ class RTCSctpTransport {
452
916
  this.dataChannelQueue.push([channel, const_1.WEBRTC_DCEP, send]);
453
917
  this.dataChannelFlush();
454
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
+ */
455
925
  dataChannelFlush() {
456
- // """
457
- // Try to flush buffered data to the SCTP layer.
458
- // We wait until the association is established, as we need to know
459
- // whether we are a client or a server to correctly assign an odd/even ID
460
- // to the data channels.
461
- // """
462
926
  if (this.sctp.associationState != src_1.SCTP_STATE.ESTABLISHED)
463
927
  return;
464
- // The peer's INIT can establish an association that joined a connected
465
- // DTLS transport before start() ran here; the same side decides the IDs.
466
- this.dataChannelId ?? (this.dataChannelId = this.isServer ? 0 : 1);
928
+ this.assignPendingStreamIds();
929
+ const waiting = new common_1.FifoQueue();
467
930
  // Hands every queued message to SCTP synchronously (SCTP.send enqueues
468
931
  // before its first await) and tracks completion per message, so repeated
469
932
  // datachannelSend() calls never spawn concurrent flush loops.
470
933
  while (this.dataChannelQueue.length > 0) {
471
- const [channel, protocol, userData] = this.dataChannelQueue.shift();
472
- // A channel closed while its message was queued (e.g. the association
473
- // closed and was re-established) is no longer managed; never announce it.
474
- if (channel.readyState === "closed" || channel.readyState === "closing") {
475
- if (protocol !== const_1.WEBRTC_DCEP)
476
- 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);
477
941
  continue;
478
- }
479
- let streamId = channel.id;
480
- if (streamId === undefined) {
481
- // An association the remote established before start() decides the
482
- // parity here (RFC 8832: DTLS client even; ICE controlling is server).
483
- this.dataChannelId ?? (this.dataChannelId = this.isServer ? 0 : 1);
484
- streamId = this.dataChannelId;
485
- while (Object.keys(this.dataChannels).includes(streamId.toString())) {
486
- streamId += 2;
487
- }
488
- this.dataChannels[streamId] = channel;
489
- channel.setId(streamId);
490
942
  }
491
943
  if (protocol === const_1.WEBRTC_DCEP) {
944
+ if (userData[0] === const_1.DATA_CHANNEL_OPEN)
945
+ this.unannounced.delete(channel);
492
946
  this.sctp
493
947
  .send(streamId, protocol, userData, {
494
948
  ordered: true,
@@ -501,21 +955,67 @@ class RTCSctpTransport {
501
955
  const expiry = channel.maxPacketLifeTime !== null
502
956
  ? Date.now() + channel.maxPacketLifeTime
503
957
  : undefined;
958
+ const bufferedBytes = item[3] ?? userData.length;
504
959
  this.sctp
505
960
  .send(streamId, protocol, userData, {
506
961
  expiry,
507
962
  maxRetransmits: channel.maxRetransmits ?? undefined,
508
963
  ordered: channel.ordered,
509
964
  })
510
- .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) => {
511
972
  log("send data failed", err.message);
512
973
  })
513
- .then(() => channel.addBufferedAmount(-userData.length))
514
974
  .catch((err) => {
515
975
  log("update bufferedAmount failed", err.message);
516
976
  });
517
977
  }
518
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
+ }
519
1019
  }
520
1020
  assertSendableMessageSize(size) {
521
1021
  if (this.remoteMaxMessageSize !== 0 && size > this.remoteMaxMessageSize) {
@@ -535,26 +1035,62 @@ class RTCSctpTransport {
535
1035
  this.sctp.setRemotePort(port);
536
1036
  }
537
1037
  async start(remotePort) {
538
- 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
+ }
539
1050
  await this.sctp.start(remotePort);
540
1051
  }
541
1052
  /**
542
- * @internal
543
- * Idempotent start: an association that is established (also passively,
544
- * from the remote INIT), handshaking or closed is not started again, so
545
- * 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.
546
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. */
547
1087
  async ensureStarted(remotePort) {
548
- if (this.sctp.started || this.sctp.state === "closed")
549
- return;
550
- this.assignRole();
551
- if (this.sctp.state === "connected")
1088
+ if (this.sctp.started ||
1089
+ this.sctp.state === "connected" ||
1090
+ this.sctp.state === "closed") {
552
1091
  return;
553
- await this.sctp.start(remotePort);
554
- }
555
- assignRole() {
556
- this.dataChannelId = this.isServer ? 0 : 1;
557
- this.sctp.isServer = this.isServer;
1092
+ }
1093
+ await this.start(remotePort);
558
1094
  }
559
1095
  /**
560
1096
  * @internal
@@ -563,52 +1099,129 @@ class RTCSctpTransport {
563
1099
  * `RTCPeerConnection.close()` calls it before its first await.
564
1100
  */
565
1101
  markStopped() {
1102
+ this.stopping = true;
566
1103
  this.stopped = true;
567
1104
  this.setState("closed", false);
568
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
+ }
1125
+ }
569
1126
  async stop() {
1127
+ const closedAlready = this.sctp.state === "closed";
570
1128
  this.markStopped();
571
1129
  this.dtlsTransport.dataReceiver = () => { };
572
1130
  this.discardUnsentMessages();
573
- 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
+ }
574
1141
  }
575
1142
  // Messages not yet handed to SCTP belong to the stopped association and must
576
- // not leak into a later one. User messages are dropped and removed from
577
- // bufferedAmount; DATA_CHANNEL_OPEN is kept only for channels still alive, so
578
- // a channel that outlives the transport (e.g. rollback) can be announced,
579
- // 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"
580
1148
  discardUnsentMessages() {
581
1149
  const kept = new common_1.FifoQueue();
582
1150
  for (const item of this.dataChannelQueue.toArray()) {
583
1151
  const [channel, protocol, userData] = item;
584
- if (protocol !== const_1.WEBRTC_DCEP) {
585
- channel.addBufferedAmount(-userData.length);
586
- }
587
- else if (userData[0] === const_1.DATA_CHANNEL_OPEN &&
1152
+ if (protocol === const_1.WEBRTC_DCEP &&
1153
+ userData[0] === const_1.DATA_CHANNEL_OPEN &&
588
1154
  channel.readyState !== "closed" &&
589
1155
  channel.readyState !== "closing") {
590
1156
  kept.push(item);
591
1157
  }
592
1158
  }
593
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
+ }
594
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
+ */
595
1198
  dataChannelClose(channel) {
596
1199
  if (!["closing", "closed"].includes(channel.readyState)) {
597
- channel.setReadyState("closing");
598
- if (this.sctp.associationState === src_1.SCTP_STATE.ESTABLISHED) {
599
- // Check if channel.id is already in reconfigQueue to prevent duplicates
600
- if (!this.sctp.reconfigQueue.includes(channel.id)) {
601
- this.sctp.reconfigQueue.push(channel.id);
602
- }
603
- if (this.sctp.reconfigQueue.length === 1) {
604
- this.sctp.transmitReconfigRequest();
605
- }
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);
606
1216
  }
607
1217
  else {
608
- 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);
609
1221
  // Remove any pending reconfiguration requests for this channel
610
1222
  this.sctp.reconfigQueue = this.sctp.reconfigQueue.filter((streamId) => streamId !== channel.id);
611
- if (channel.id !== undefined) {
1223
+ if (channel.id != undefined &&
1224
+ this.dataChannels[channel.id] === channel) {
612
1225
  delete this.dataChannels[channel.id];
613
1226
  }
614
1227
  channel.setReadyState("closed");