@moqtap/codec 0.2.3 → 0.4.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 (313) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +100 -99
  3. package/dist/{chunk-RZHAPEXO.js → chunk-2A57DRXV.js} +36 -10
  4. package/dist/{chunk-73MWUKSJ.js → chunk-2UTVXWFF.js} +190 -102
  5. package/dist/{chunk-JTVCOMBI.cjs → chunk-3XKO6PCR.cjs} +290 -183
  6. package/dist/{chunk-ACQ4CUJP.cjs → chunk-4BDEU3D4.cjs} +237 -158
  7. package/dist/{chunk-4N4INRE7.cjs → chunk-4HBKHNU2.cjs} +144 -110
  8. package/dist/{chunk-2ZT6CVSE.js → chunk-4IVZZOY3.js} +269 -162
  9. package/dist/{chunk-GLBQDG4L.cjs → chunk-5O764MBD.cjs} +508 -117
  10. package/dist/chunk-7KBAWOBA.js +1212 -0
  11. package/dist/{chunk-JSQM2MG3.js → chunk-7QOPNSDF.js} +44 -12
  12. package/dist/{chunk-SYHW3FLI.cjs → chunk-7RE6KSIY.cjs} +28 -8
  13. package/dist/{chunk-YG5KJESI.js → chunk-AY6IAHUM.js} +28 -8
  14. package/dist/{chunk-EFM5T7OM.js → chunk-BLQDWOSG.js} +36 -10
  15. package/dist/{chunk-UYXTY6ZQ.cjs → chunk-E65QKRSN.cjs} +20 -12
  16. package/dist/{chunk-BISI45MN.cjs → chunk-EAHE3IOG.cjs} +44 -12
  17. package/dist/{chunk-PZBPH2D7.js → chunk-GCBFHZW6.js} +216 -137
  18. package/dist/{chunk-GXEW4COZ.cjs → chunk-H5NSL4MO.cjs} +36 -10
  19. package/dist/chunk-HZHSO7EW.cjs +1212 -0
  20. package/dist/{chunk-KZCYQ7ED.cjs → chunk-IILNSDEQ.cjs} +69 -41
  21. package/dist/{chunk-4XYGE53S.cjs → chunk-J3XCPBW3.cjs} +36 -10
  22. package/dist/{chunk-6AEHWULA.cjs → chunk-JKOEJZ5B.cjs} +28 -8
  23. package/dist/{chunk-RUPCJTC3.cjs → chunk-JL3NEW3O.cjs} +23 -1
  24. package/dist/{chunk-G7GI7LJA.js → chunk-LORZMJJB.js} +36 -10
  25. package/dist/{chunk-DUBCL3WF.cjs → chunk-LV5C6MEC.cjs} +36 -10
  26. package/dist/{chunk-ENURAVHI.cjs → chunk-MYXAD5JC.cjs} +44 -12
  27. package/dist/{chunk-FXZ2MYKJ.js → chunk-MZ7HUCGJ.js} +20 -12
  28. package/dist/{chunk-S74DWJE2.cjs → chunk-OKG2HT25.cjs} +277 -88
  29. package/dist/{chunk-BGWGVINJ.cjs → chunk-PKEOQSSZ.cjs} +144 -110
  30. package/dist/{chunk-2GODRVT5.js → chunk-Q46ZURKX.js} +325 -48
  31. package/dist/{chunk-UR6JKS56.js → chunk-Q7IGWV3P.js} +41 -9
  32. package/dist/{chunk-XSNX2IUJ.js → chunk-RW4UG7CN.js} +121 -87
  33. package/dist/{chunk-DWK5ZQZ4.js → chunk-RZTLQAKF.js} +28 -8
  34. package/dist/{chunk-UNS34PTA.cjs → chunk-S6IPIN66.cjs} +44 -12
  35. package/dist/{chunk-YIG2C3LF.cjs → chunk-SDWRXXOO.cjs} +214 -126
  36. package/dist/{chunk-CEH7ESNC.js → chunk-SHMA4PBY.js} +121 -87
  37. package/dist/{chunk-MC7W6PMI.js → chunk-U3ZVLT5L.js} +483 -135
  38. package/dist/{chunk-VJUUZ4W2.cjs → chunk-UJU63EFY.cjs} +374 -97
  39. package/dist/{chunk-7L43FE6P.js → chunk-VOU7PNHI.js} +23 -1
  40. package/dist/{chunk-E6E3NQYU.js → chunk-WRHGCVYN.js} +44 -12
  41. package/dist/{chunk-FPISPJ5N.cjs → chunk-XNWYLVGU.cjs} +529 -181
  42. package/dist/{chunk-U3IPZYPX.js → chunk-XS77RBY7.js} +228 -39
  43. package/dist/{chunk-JGG7CMSZ.cjs → chunk-XZE5R57R.cjs} +144 -110
  44. package/dist/{chunk-77MNXX3M.js → chunk-Y6KCSM3V.js} +121 -87
  45. package/dist/{chunk-TLYNOOQP.cjs → chunk-YTPZZI2M.cjs} +41 -9
  46. package/dist/{chunk-7DUBLRXC.js → chunk-YWRCSQBA.js} +44 -12
  47. package/dist/{chunk-MRJWLGYH.js → chunk-Z3VKP3PJ.js} +69 -41
  48. package/dist/{chunk-E6P74GDT.js → chunk-Z6U26IRI.js} +464 -73
  49. package/dist/{codec-FvGCxIhr.d.ts → codec-25u4_1HS.d.ts} +4 -4
  50. package/dist/{codec-DRcsBCsb.d.ts → codec-3Ppy_5Gd.d.ts} +4 -4
  51. package/dist/{codec-BdVMRMOI.d.ts → codec-BB0U8MF2.d.cts} +4 -4
  52. package/dist/{codec-B2cH-f5V.d.cts → codec-BHHfBTnV.d.ts} +4 -4
  53. package/dist/{codec-B5K73Bdj.d.ts → codec-BN6jk0Td.d.cts} +4 -4
  54. package/dist/{codec-R8GrCPt2.d.ts → codec-BU0gaSZm.d.ts} +4 -4
  55. package/dist/{codec-Bq-tLtfP.d.ts → codec-C-Yk2RNa.d.ts} +4 -4
  56. package/dist/codec-CFWQY9Lj.d.cts +18 -0
  57. package/dist/{codec-CPlK3Pi5.d.ts → codec-COy4uuPO.d.cts} +4 -4
  58. package/dist/codec-CewFCsEy.d.ts +18 -0
  59. package/dist/{codec-KXHvXZVb.d.cts → codec-Ck7ZwFF1.d.cts} +4 -4
  60. package/dist/{codec-DZhZSIii.d.cts → codec-D2QVSH9D.d.ts} +4 -4
  61. package/dist/{codec-BwFgpeQu.d.ts → codec-DDUBJGn0.d.cts} +4 -4
  62. package/dist/{codec-BFeliJFc.d.ts → codec-DNfml-se.d.ts} +4 -4
  63. package/dist/{codec-C4n1PS09.d.cts → codec-DoOGjjDq.d.cts} +4 -4
  64. package/dist/{codec-Bap882Fh.d.cts → codec-J7ekOSMc.d.ts} +4 -4
  65. package/dist/{codec-SKBeHQk7.d.ts → codec-R9TJCq-E.d.cts} +4 -4
  66. package/dist/{codec-DNUIgLqc.d.cts → codec-ZwV-Njt6.d.ts} +4 -4
  67. package/dist/{codec-HgBgHkFI.d.cts → codec-j819z_hL.d.cts} +4 -4
  68. package/dist/{codec-DY4QH364.d.cts → codec-w8z2j_uo.d.cts} +4 -4
  69. package/dist/{codec-CPvPq0sb.d.cts → codec-wtt6WF1K.d.ts} +4 -4
  70. package/dist/{codec-BUMjz0J4.d.cts → codec-z6_2PSzi.d.cts} +4 -4
  71. package/dist/{draft7-session.cjs → draft07-session.cjs} +2 -2
  72. package/dist/draft07-session.d.cts +8 -0
  73. package/dist/draft07-session.d.ts +8 -0
  74. package/dist/{draft7-session.js → draft07-session.js} +1 -1
  75. package/dist/draft07.cjs +129 -0
  76. package/dist/draft07.d.cts +67 -0
  77. package/dist/draft07.d.ts +67 -0
  78. package/dist/draft07.js +129 -0
  79. package/dist/draft08-session.cjs +6 -0
  80. package/dist/draft08-session.d.cts +8 -0
  81. package/dist/draft08-session.d.ts +8 -0
  82. package/dist/{draft8-session.js → draft08-session.js} +1 -1
  83. package/dist/draft08.cjs +119 -0
  84. package/dist/{draft8.d.cts → draft08.d.cts} +15 -13
  85. package/dist/{draft8.d.ts → draft08.d.ts} +15 -13
  86. package/dist/{draft8.js → draft08.js} +7 -4
  87. package/dist/draft09-session.cjs +6 -0
  88. package/dist/draft09-session.d.cts +8 -0
  89. package/dist/draft09-session.d.ts +8 -0
  90. package/dist/{draft9-session.js → draft09-session.js} +1 -1
  91. package/dist/draft09.cjs +119 -0
  92. package/dist/{draft9.d.cts → draft09.d.cts} +15 -13
  93. package/dist/{draft9.d.ts → draft09.d.ts} +15 -13
  94. package/dist/{draft9.js → draft09.js} +7 -4
  95. package/dist/draft10-session.cjs +2 -2
  96. package/dist/draft10-session.d.cts +6 -6
  97. package/dist/draft10-session.d.ts +6 -6
  98. package/dist/draft10-session.js +1 -1
  99. package/dist/draft10.cjs +8 -5
  100. package/dist/draft10.d.cts +14 -12
  101. package/dist/draft10.d.ts +14 -12
  102. package/dist/draft10.js +16 -13
  103. package/dist/draft11-session.cjs +2 -2
  104. package/dist/draft11-session.d.cts +6 -6
  105. package/dist/draft11-session.d.ts +6 -6
  106. package/dist/draft11-session.js +1 -1
  107. package/dist/draft11.cjs +8 -5
  108. package/dist/draft11.d.cts +14 -12
  109. package/dist/draft11.d.ts +14 -12
  110. package/dist/draft11.js +16 -13
  111. package/dist/draft12-session.cjs +2 -2
  112. package/dist/draft12-session.d.cts +6 -6
  113. package/dist/draft12-session.d.ts +6 -6
  114. package/dist/draft12-session.js +1 -1
  115. package/dist/draft12.cjs +8 -5
  116. package/dist/draft12.d.cts +16 -14
  117. package/dist/draft12.d.ts +16 -14
  118. package/dist/draft12.js +16 -13
  119. package/dist/draft13-session.cjs +2 -2
  120. package/dist/draft13-session.d.cts +6 -6
  121. package/dist/draft13-session.d.ts +6 -6
  122. package/dist/draft13-session.js +1 -1
  123. package/dist/draft13.cjs +8 -5
  124. package/dist/draft13.d.cts +16 -14
  125. package/dist/draft13.d.ts +16 -14
  126. package/dist/draft13.js +16 -13
  127. package/dist/draft14-session.cjs +2 -2
  128. package/dist/draft14-session.d.cts +6 -6
  129. package/dist/draft14-session.d.ts +6 -6
  130. package/dist/draft14-session.js +1 -1
  131. package/dist/draft14.cjs +14 -5
  132. package/dist/draft14.d.cts +14 -12
  133. package/dist/draft14.d.ts +14 -12
  134. package/dist/draft14.js +13 -4
  135. package/dist/draft15-session.cjs +2 -2
  136. package/dist/draft15-session.d.cts +6 -6
  137. package/dist/draft15-session.d.ts +6 -6
  138. package/dist/draft15-session.js +1 -1
  139. package/dist/draft15.cjs +8 -5
  140. package/dist/draft15.d.cts +14 -12
  141. package/dist/draft15.d.ts +14 -12
  142. package/dist/draft15.js +16 -13
  143. package/dist/draft16-session.cjs +2 -2
  144. package/dist/draft16-session.d.cts +6 -6
  145. package/dist/draft16-session.d.ts +6 -6
  146. package/dist/draft16-session.js +1 -1
  147. package/dist/draft16.cjs +8 -5
  148. package/dist/draft16.d.cts +14 -12
  149. package/dist/draft16.d.ts +14 -12
  150. package/dist/draft16.js +16 -13
  151. package/dist/draft17-session.cjs +2 -2
  152. package/dist/draft17-session.d.cts +7 -7
  153. package/dist/draft17-session.d.ts +7 -7
  154. package/dist/draft17-session.js +1 -1
  155. package/dist/draft17.cjs +8 -5
  156. package/dist/draft17.d.cts +11 -9
  157. package/dist/draft17.d.ts +11 -9
  158. package/dist/draft17.js +16 -13
  159. package/dist/index.cjs +52 -74
  160. package/dist/index.d.cts +41 -60
  161. package/dist/index.d.ts +41 -60
  162. package/dist/index.js +40 -62
  163. package/dist/{session-types-CJIFbTPd.d.ts → session-types-C7UONex5.d.cts} +28 -27
  164. package/dist/{session-types-Cbq8IGCP.d.cts → session-types-X-IYEGoG.d.ts} +28 -27
  165. package/dist/session.cjs +46 -13
  166. package/dist/session.d.cts +29 -11
  167. package/dist/session.d.ts +29 -11
  168. package/dist/session.js +46 -13
  169. package/dist/{types-DxSFvL2b.d.cts → types--Q7_DcvP.d.cts} +43 -39
  170. package/dist/{types-DxSFvL2b.d.ts → types--Q7_DcvP.d.ts} +43 -39
  171. package/dist/{types-B0FT5Qs2.d.cts → types-0IWbU9Z_.d.cts} +36 -36
  172. package/dist/{types-B0FT5Qs2.d.ts → types-0IWbU9Z_.d.ts} +36 -36
  173. package/dist/{types-gQsr0AAA.d.cts → types-B3g5G9KP.d.cts} +36 -36
  174. package/dist/{types-gQsr0AAA.d.ts → types-B3g5G9KP.d.ts} +36 -36
  175. package/dist/{types-Pme7sua0.d.cts → types-BFpFNM_m.d.cts} +57 -32
  176. package/dist/{types-Pme7sua0.d.ts → types-BFpFNM_m.d.ts} +57 -32
  177. package/dist/{types-8gUGeMgs.d.cts → types-Br7fm2mf.d.cts} +48 -26
  178. package/dist/{types-8gUGeMgs.d.ts → types-Br7fm2mf.d.ts} +48 -26
  179. package/dist/{types-BeAVNU-F.d.cts → types-C2qKTRR_.d.cts} +50 -45
  180. package/dist/{types-BeAVNU-F.d.ts → types-C2qKTRR_.d.ts} +50 -45
  181. package/dist/{types-DKpsfD3i.d.cts → types-DEaypVsD.d.cts} +115 -55
  182. package/dist/{types-DKpsfD3i.d.ts → types-DEaypVsD.d.ts} +115 -55
  183. package/dist/types-Dl4IaAHR.d.cts +282 -0
  184. package/dist/types-Dl4IaAHR.d.ts +282 -0
  185. package/dist/{types-CDa7F5ct.d.cts → types-DrQpTW1G.d.cts} +58 -50
  186. package/dist/{types-CDa7F5ct.d.ts → types-DrQpTW1G.d.ts} +58 -50
  187. package/dist/types-DuIO7cWK.d.cts +25 -0
  188. package/dist/types-DuIO7cWK.d.ts +25 -0
  189. package/dist/{types-B9A2g05r.d.cts → types-DuxfOYiO.d.cts} +47 -31
  190. package/dist/{types-B9A2g05r.d.ts → types-DuxfOYiO.d.ts} +47 -31
  191. package/dist/{types-D1dI_oTX.d.cts → types-jV1yDDBA.d.cts} +36 -36
  192. package/dist/{types-D1dI_oTX.d.ts → types-jV1yDDBA.d.ts} +36 -36
  193. package/package.json +192 -192
  194. package/src/core/buffer-reader.ts +130 -130
  195. package/src/core/buffer-writer.ts +100 -100
  196. package/src/core/errors.ts +1 -1
  197. package/src/core/hex.ts +21 -21
  198. package/src/core/session-types.ts +61 -59
  199. package/src/core/types.ts +18 -361
  200. package/src/drafts/draft07/codec.ts +1058 -938
  201. package/src/drafts/draft07/data-streams.ts +240 -240
  202. package/src/drafts/draft07/index.ts +124 -89
  203. package/src/drafts/draft07/messages.ts +76 -42
  204. package/src/drafts/draft07/rules.ts +107 -101
  205. package/src/drafts/draft07/session-fsm.ts +315 -287
  206. package/src/drafts/draft07/session.ts +8 -6
  207. package/src/drafts/draft07/types.ts +406 -72
  208. package/src/drafts/draft07/varint.ts +51 -51
  209. package/src/drafts/draft08/codec.ts +985 -953
  210. package/src/drafts/draft08/data-streams.ts +363 -359
  211. package/src/drafts/draft08/index.ts +10 -9
  212. package/src/drafts/draft08/messages.ts +59 -59
  213. package/src/drafts/draft08/rules.ts +73 -73
  214. package/src/drafts/draft08/session-fsm.ts +335 -303
  215. package/src/drafts/draft08/session.ts +6 -7
  216. package/src/drafts/draft08/types.ts +380 -384
  217. package/src/drafts/draft09/codec.ts +977 -945
  218. package/src/drafts/draft09/data-streams.ts +336 -332
  219. package/src/drafts/draft09/index.ts +10 -9
  220. package/src/drafts/draft09/messages.ts +59 -59
  221. package/src/drafts/draft09/rules.ts +73 -73
  222. package/src/drafts/draft09/session-fsm.ts +335 -303
  223. package/src/drafts/draft09/session.ts +6 -7
  224. package/src/drafts/draft09/types.ts +380 -384
  225. package/src/drafts/draft10/codec.ts +977 -945
  226. package/src/drafts/draft10/data-streams.ts +336 -332
  227. package/src/drafts/draft10/index.ts +10 -9
  228. package/src/drafts/draft10/messages.ts +59 -59
  229. package/src/drafts/draft10/rules.ts +73 -73
  230. package/src/drafts/draft10/session-fsm.ts +335 -303
  231. package/src/drafts/draft10/session.ts +6 -7
  232. package/src/drafts/draft10/types.ts +380 -384
  233. package/src/drafts/draft11/codec.ts +1054 -988
  234. package/src/drafts/draft11/data-streams.ts +316 -269
  235. package/src/drafts/draft11/index.ts +10 -9
  236. package/src/drafts/draft11/messages.ts +60 -59
  237. package/src/drafts/draft11/rules.ts +73 -73
  238. package/src/drafts/draft11/session-fsm.ts +359 -333
  239. package/src/drafts/draft11/session.ts +6 -7
  240. package/src/drafts/draft11/types.ts +385 -381
  241. package/src/drafts/draft12/codec.ts +1178 -1122
  242. package/src/drafts/draft12/data-streams.ts +324 -275
  243. package/src/drafts/draft12/index.ts +10 -9
  244. package/src/drafts/draft12/messages.ts +67 -66
  245. package/src/drafts/draft12/rules.ts +79 -79
  246. package/src/drafts/draft12/session-fsm.ts +384 -358
  247. package/src/drafts/draft12/session.ts +6 -7
  248. package/src/drafts/draft12/types.ts +424 -419
  249. package/src/drafts/draft13/codec.ts +1296 -1206
  250. package/src/drafts/draft13/data-streams.ts +326 -275
  251. package/src/drafts/draft13/index.ts +10 -9
  252. package/src/drafts/draft13/messages.ts +73 -68
  253. package/src/drafts/draft13/rules.ts +81 -81
  254. package/src/drafts/draft13/session-fsm.ts +392 -366
  255. package/src/drafts/draft13/session.ts +6 -7
  256. package/src/drafts/draft13/types.ts +447 -438
  257. package/src/drafts/draft14/codec.ts +1347 -1043
  258. package/src/drafts/draft14/data-streams.ts +831 -813
  259. package/src/drafts/draft14/index.ts +17 -9
  260. package/src/drafts/draft14/messages.ts +65 -65
  261. package/src/drafts/draft14/rules.ts +81 -81
  262. package/src/drafts/draft14/session-fsm.ts +415 -405
  263. package/src/drafts/draft14/session.ts +6 -7
  264. package/src/drafts/draft14/types.ts +451 -381
  265. package/src/drafts/draft15/codec.ts +1138 -978
  266. package/src/drafts/draft15/data-streams.ts +842 -794
  267. package/src/drafts/draft15/index.ts +10 -9
  268. package/src/drafts/draft15/messages.ts +54 -53
  269. package/src/drafts/draft15/rules.ts +67 -67
  270. package/src/drafts/draft15/session-fsm.ts +319 -299
  271. package/src/drafts/draft15/session.ts +6 -7
  272. package/src/drafts/draft15/types.ts +357 -339
  273. package/src/drafts/draft16/codec.ts +1242 -966
  274. package/src/drafts/draft16/data-streams.ts +835 -789
  275. package/src/drafts/draft16/index.ts +10 -9
  276. package/src/drafts/draft16/messages.ts +56 -55
  277. package/src/drafts/draft16/rules.ts +69 -69
  278. package/src/drafts/draft16/session-fsm.ts +319 -299
  279. package/src/drafts/draft16/session.ts +6 -7
  280. package/src/drafts/draft16/types.ts +386 -357
  281. package/src/drafts/draft17/codec.ts +1209 -971
  282. package/src/drafts/draft17/data-streams.ts +969 -758
  283. package/src/drafts/draft17/index.ts +9 -7
  284. package/src/drafts/draft17/messages.ts +43 -42
  285. package/src/drafts/draft17/rules.ts +58 -58
  286. package/src/drafts/draft17/session-fsm.ts +248 -216
  287. package/src/drafts/draft17/session.ts +5 -5
  288. package/src/drafts/draft17/types.ts +337 -313
  289. package/src/index.ts +85 -274
  290. package/src/session.ts +63 -25
  291. package/dist/chunk-FZDIEHQL.cjs +0 -1042
  292. package/dist/chunk-HDWJ2GDE.js +0 -24
  293. package/dist/chunk-KNDYS3DK.cjs +0 -24
  294. package/dist/chunk-N2KBF5Q2.js +0 -1042
  295. package/dist/draft7-session.d.cts +0 -7
  296. package/dist/draft7-session.d.ts +0 -7
  297. package/dist/draft7.cjs +0 -72
  298. package/dist/draft7.d.cts +0 -148
  299. package/dist/draft7.d.ts +0 -148
  300. package/dist/draft7.js +0 -72
  301. package/dist/draft8-session.cjs +0 -6
  302. package/dist/draft8-session.d.cts +0 -8
  303. package/dist/draft8-session.d.ts +0 -8
  304. package/dist/draft8.cjs +0 -116
  305. package/dist/draft9-session.cjs +0 -6
  306. package/dist/draft9-session.d.cts +0 -8
  307. package/dist/draft9-session.d.ts +0 -8
  308. package/dist/draft9.cjs +0 -116
  309. package/dist/types-QNXoxC9Y.d.cts +0 -236
  310. package/dist/types-QNXoxC9Y.d.ts +0 -236
  311. package/src/drafts/draft07/announce-fsm.ts +0 -2
  312. package/src/drafts/draft07/parameters.ts +0 -12
  313. package/src/drafts/draft07/subscription-fsm.ts +0 -3
@@ -1,988 +1,1054 @@
1
- import { bytesToHex, hexToBytes } from "../../core/hex.js";
2
- import { BufferReader } from "../../core/buffer-reader.js";
3
- import { BufferWriter } from "../../core/buffer-writer.js";
4
- import type { BaseCodec, DecodeResult } from "../../core/types.js";
5
- import { DecodeError } from "../../core/types.js";
6
- import {
7
- MESSAGE_ID_MAP,
8
- MSG_ANNOUNCE,
9
- MSG_ANNOUNCE_CANCEL,
10
- MSG_ANNOUNCE_ERROR,
11
- MSG_ANNOUNCE_OK,
12
- MSG_CLIENT_SETUP,
13
- MSG_FETCH,
14
- MSG_FETCH_CANCEL,
15
- MSG_FETCH_ERROR,
16
- MSG_FETCH_OK,
17
- MSG_GOAWAY,
18
- MSG_MAX_REQUEST_ID,
19
- MSG_REQUESTS_BLOCKED,
20
- MSG_SERVER_SETUP,
21
- MSG_SUBSCRIBE,
22
- MSG_SUBSCRIBE_ANNOUNCES,
23
- MSG_SUBSCRIBE_ANNOUNCES_ERROR,
24
- MSG_SUBSCRIBE_ANNOUNCES_OK,
25
- MSG_SUBSCRIBE_DONE,
26
- MSG_SUBSCRIBE_ERROR,
27
- MSG_SUBSCRIBE_OK,
28
- MSG_SUBSCRIBE_UPDATE,
29
- MSG_TRACK_STATUS,
30
- MSG_TRACK_STATUS_REQUEST,
31
- MSG_UNANNOUNCE,
32
- MSG_UNSUBSCRIBE,
33
- MSG_UNSUBSCRIBE_ANNOUNCES,
34
- PARAM_AUTHORIZATION_TOKEN,
35
- PARAM_DELIVERY_TIMEOUT,
36
- SETUP_PARAM_MAX_REQUEST_ID,
37
- SETUP_PARAM_PATH,
38
- } from "./messages.js";
39
- import type {
40
- DatagramObject,
41
- Draft11DataStream,
42
- Draft11Fetch,
43
- Draft11Message,
44
- Draft11Params,
45
- Draft11SetupParams,
46
- FetchObjectPayload,
47
- FetchStream,
48
- JoiningFetch,
49
- LargestLocation,
50
- ObjectPayload,
51
- StandaloneFetch,
52
- SubgroupStream,
53
- UnknownParam,
54
- } from "./types.js";
55
-
56
- const textEncoder = /* @__PURE__ */ new TextEncoder();
57
- const textDecoder = /* @__PURE__ */ new TextDecoder();
58
-
59
- // ─── Setup Parameter Encoding/Decoding (even/odd convention) ────────────────────
60
-
61
- function encodeSetupParams(params: Draft11SetupParams, w: BufferWriter): void {
62
- let count = 0;
63
- if (params.path !== undefined) count++;
64
- if (params.max_request_id !== undefined) count++;
65
- if (params.unknown) count += params.unknown.length;
66
-
67
- w.writeVarInt(count);
68
-
69
- if (params.path !== undefined) {
70
- w.writeVarInt(SETUP_PARAM_PATH);
71
- const encoded = textEncoder.encode(params.path);
72
- w.writeVarInt(encoded.byteLength);
73
- w.writeBytes(encoded);
74
- }
75
- if (params.max_request_id !== undefined) {
76
- w.writeVarInt(SETUP_PARAM_MAX_REQUEST_ID);
77
- w.writeVarInt(params.max_request_id);
78
- }
79
- if (params.unknown) {
80
- for (const u of params.unknown) {
81
- const id = BigInt(u.id);
82
- w.writeVarInt(id);
83
- if (id % 2n === 0n) {
84
- const raw = hexToBytes(u.raw_hex);
85
- const tmpReader = new BufferReader(raw);
86
- w.writeVarInt(tmpReader.readVarInt());
87
- } else {
88
- const raw = hexToBytes(u.raw_hex);
89
- w.writeVarInt(raw.byteLength);
90
- w.writeBytes(raw);
91
- }
92
- }
93
- }
94
- }
95
-
96
- function decodeSetupParams(r: BufferReader): Draft11SetupParams {
97
- const count = Number(r.readVarInt());
98
- const result: Draft11SetupParams = {};
99
- const unknown: UnknownParam[] = [];
100
-
101
- for (let i = 0; i < count; i++) {
102
- const paramType = r.readVarInt();
103
- if (paramType % 2n === 0n) {
104
- const value = r.readVarInt();
105
- if (paramType === SETUP_PARAM_MAX_REQUEST_ID) {
106
- result.max_request_id = value;
107
- } else {
108
- const tmpW = new BufferWriter(16);
109
- tmpW.writeVarInt(value);
110
- const raw = tmpW.finish();
111
- unknown.push({
112
- id: `0x${paramType.toString(16)}`,
113
- length: raw.byteLength,
114
- raw_hex: bytesToHex(raw),
115
- });
116
- }
117
- } else {
118
- const length = Number(r.readVarInt());
119
- const bytes = r.readBytes(length);
120
- if (paramType === SETUP_PARAM_PATH) {
121
- result.path = textDecoder.decode(bytes);
122
- } else {
123
- unknown.push({ id: `0x${paramType.toString(16)}`, length, raw_hex: bytesToHex(bytes) });
124
- }
125
- }
126
- }
127
-
128
- if (unknown.length > 0) result.unknown = unknown;
129
- return result;
130
- }
131
-
132
- // ─── Version-Specific Parameter Encoding/Decoding ───────────────────────────────
133
- // Uses even/odd convention: even type → bare varint value, odd type → length-prefixed blob
134
-
135
- function encodeParams(params: Draft11Params, w: BufferWriter): void {
136
- let count = params.unknown ? params.unknown.length : 0;
137
- if (params.authorization_token !== undefined) count++;
138
- if (params.delivery_timeout !== undefined) count++;
139
- w.writeVarInt(count);
140
-
141
- if (params.authorization_token !== undefined) {
142
- // AUTHORIZATION_TOKEN = 0x01 (odd → length-prefixed)
143
- w.writeVarInt(PARAM_AUTHORIZATION_TOKEN);
144
- const at = params.authorization_token;
145
- const tmpW = new BufferWriter(64);
146
- tmpW.writeVarInt(at.alias_type);
147
- tmpW.writeVarInt(at.token_type);
148
- const tokenBytes = textEncoder.encode(at.token_value);
149
- tmpW.writeBytes(tokenBytes);
150
- const raw = tmpW.finish();
151
- w.writeVarInt(raw.byteLength);
152
- w.writeBytes(raw);
153
- }
154
- if (params.delivery_timeout !== undefined) {
155
- // DELIVERY_TIMEOUT = 0x02 (evenbare varint)
156
- w.writeVarInt(PARAM_DELIVERY_TIMEOUT);
157
- w.writeVarInt(params.delivery_timeout);
158
- }
159
- if (params.unknown) {
160
- for (const u of params.unknown) {
161
- const id = BigInt(u.id);
162
- w.writeVarInt(id);
163
- if (id % 2n === 0n) {
164
- // Even → bare varint
165
- const raw = hexToBytes(u.raw_hex);
166
- const tmpReader = new BufferReader(raw);
167
- w.writeVarInt(tmpReader.readVarInt());
168
- } else {
169
- // Odd → length-prefixed
170
- const raw = hexToBytes(u.raw_hex);
171
- w.writeVarInt(raw.byteLength);
172
- w.writeBytes(raw);
173
- }
174
- }
175
- }
176
- }
177
-
178
- function decodeParams(r: BufferReader): Draft11Params {
179
- const count = Number(r.readVarInt());
180
- const result: Draft11Params = {};
181
- const unknown: UnknownParam[] = [];
182
-
183
- for (let i = 0; i < count; i++) {
184
- const paramType = r.readVarInt();
185
-
186
- if (paramType % 2n === 0n) {
187
- // Even type bare varint value
188
- const value = r.readVarInt();
189
- if (paramType === PARAM_DELIVERY_TIMEOUT) {
190
- result.delivery_timeout = value;
191
- } else {
192
- const tmpW = new BufferWriter(16);
193
- tmpW.writeVarInt(value);
194
- const raw = tmpW.finish();
195
- unknown.push({
196
- id: `0x${paramType.toString(16)}`,
197
- length: raw.byteLength,
198
- raw_hex: bytesToHex(raw),
199
- });
200
- }
201
- } else {
202
- // Odd type → length-prefixed blob
203
- const length = Number(r.readVarInt());
204
- const startOff = r.offset;
205
-
206
- if (paramType === PARAM_AUTHORIZATION_TOKEN) {
207
- const alias_type = r.readVarInt();
208
- const token_type = r.readVarInt();
209
- const tokenBytesLen = length - (r.offset - startOff);
210
- const tokenBytes = r.readBytes(tokenBytesLen);
211
- result.authorization_token = {
212
- alias_type,
213
- token_type,
214
- token_value: textDecoder.decode(tokenBytes),
215
- };
216
- } else {
217
- const bytes = r.readBytes(length);
218
- unknown.push({ id: `0x${paramType.toString(16)}`, length, raw_hex: bytesToHex(bytes) });
219
- }
220
- }
221
- }
222
-
223
- if (unknown.length > 0) result.unknown = unknown;
224
- return result;
225
- }
226
-
227
- // ─── Payload Encoders ──────────────────────────────────────────────────────────
228
-
229
- function encodeClientSetupPayload(
230
- msg: Draft11Message & { type: "client_setup" },
231
- w: BufferWriter,
232
- ): void {
233
- w.writeVarInt(msg.supported_versions.length);
234
- for (const v of msg.supported_versions) w.writeVarInt(v);
235
- encodeSetupParams(msg.parameters, w);
236
- }
237
-
238
- function encodeServerSetupPayload(
239
- msg: Draft11Message & { type: "server_setup" },
240
- w: BufferWriter,
241
- ): void {
242
- w.writeVarInt(msg.selected_version);
243
- encodeSetupParams(msg.parameters, w);
244
- }
245
-
246
- function encodeSubscribePayload(
247
- msg: Draft11Message & { type: "subscribe" },
248
- w: BufferWriter,
249
- ): void {
250
- w.writeVarInt(msg.request_id);
251
- w.writeVarInt(msg.track_alias);
252
- w.writeTuple(msg.track_namespace);
253
- w.writeString(msg.track_name);
254
- w.writeUint8(msg.subscriber_priority);
255
- w.writeVarInt(msg.group_order);
256
- w.writeVarInt(msg.forward);
257
- w.writeVarInt(msg.filter_type);
258
- const ft = Number(msg.filter_type);
259
- if (ft === 3 || ft === 4) {
260
- w.writeVarInt(msg.start_group!);
261
- w.writeVarInt(msg.start_object!);
262
- }
263
- if (ft === 4) {
264
- w.writeVarInt(msg.end_group!);
265
- }
266
- encodeParams(msg.parameters, w);
267
- }
268
-
269
- function encodeSubscribeOkPayload(
270
- msg: Draft11Message & { type: "subscribe_ok" },
271
- w: BufferWriter,
272
- ): void {
273
- w.writeVarInt(msg.request_id);
274
- w.writeVarInt(msg.expires);
275
- w.writeVarInt(msg.group_order);
276
- w.writeVarInt(msg.content_exists);
277
- if (msg.content_exists === 1n && msg.largest_location) {
278
- w.writeVarInt(msg.largest_location.group);
279
- w.writeVarInt(msg.largest_location.object);
280
- }
281
- encodeParams(msg.parameters, w);
282
- }
283
-
284
- function encodeSubscribeErrorPayload(
285
- msg: Draft11Message & { type: "subscribe_error" },
286
- w: BufferWriter,
287
- ): void {
288
- w.writeVarInt(msg.request_id);
289
- w.writeVarInt(msg.error_code);
290
- w.writeString(msg.reason_phrase);
291
- w.writeVarInt(msg.track_alias);
292
- }
293
-
294
- function encodeSubscribeUpdatePayload(
295
- msg: Draft11Message & { type: "subscribe_update" },
296
- w: BufferWriter,
297
- ): void {
298
- w.writeVarInt(msg.request_id);
299
- w.writeVarInt(msg.start_group);
300
- w.writeVarInt(msg.start_object);
301
- w.writeVarInt(msg.end_group);
302
- w.writeUint8(msg.subscriber_priority);
303
- w.writeVarInt(msg.forward);
304
- encodeParams(msg.parameters, w);
305
- }
306
-
307
- function encodeSubscribeDonePayload(
308
- msg: Draft11Message & { type: "subscribe_done" },
309
- w: BufferWriter,
310
- ): void {
311
- w.writeVarInt(msg.request_id);
312
- w.writeVarInt(msg.status_code);
313
- w.writeVarInt(msg.stream_count);
314
- w.writeString(msg.reason_phrase);
315
- }
316
-
317
- function encodeUnsubscribePayload(
318
- msg: Draft11Message & { type: "unsubscribe" },
319
- w: BufferWriter,
320
- ): void {
321
- w.writeVarInt(msg.request_id);
322
- }
323
-
324
- function encodeAnnouncePayload(msg: Draft11Message & { type: "announce" }, w: BufferWriter): void {
325
- w.writeVarInt(msg.request_id);
326
- w.writeTuple(msg.track_namespace);
327
- encodeParams(msg.parameters, w);
328
- }
329
-
330
- function encodeAnnounceOkPayload(
331
- msg: Draft11Message & { type: "announce_ok" },
332
- w: BufferWriter,
333
- ): void {
334
- w.writeVarInt(msg.request_id);
335
- }
336
-
337
- function encodeAnnounceErrorPayload(
338
- msg: Draft11Message & { type: "announce_error" },
339
- w: BufferWriter,
340
- ): void {
341
- w.writeVarInt(msg.request_id);
342
- w.writeVarInt(msg.error_code);
343
- w.writeString(msg.reason_phrase);
344
- }
345
-
346
- function encodeUnannouncePayload(
347
- msg: Draft11Message & { type: "unannounce" },
348
- w: BufferWriter,
349
- ): void {
350
- w.writeTuple(msg.track_namespace);
351
- }
352
-
353
- function encodeAnnounceCancelPayload(
354
- msg: Draft11Message & { type: "announce_cancel" },
355
- w: BufferWriter,
356
- ): void {
357
- w.writeTuple(msg.track_namespace);
358
- w.writeVarInt(msg.error_code);
359
- w.writeString(msg.reason_phrase);
360
- }
361
-
362
- function encodeSubscribeAnnouncesPayload(
363
- msg: Draft11Message & { type: "subscribe_announces" },
364
- w: BufferWriter,
365
- ): void {
366
- w.writeVarInt(msg.request_id);
367
- w.writeTuple(msg.track_namespace_prefix);
368
- encodeParams(msg.parameters, w);
369
- }
370
-
371
- function encodeSubscribeAnnouncesOkPayload(
372
- msg: Draft11Message & { type: "subscribe_announces_ok" },
373
- w: BufferWriter,
374
- ): void {
375
- w.writeVarInt(msg.request_id);
376
- }
377
-
378
- function encodeSubscribeAnnouncesErrorPayload(
379
- msg: Draft11Message & { type: "subscribe_announces_error" },
380
- w: BufferWriter,
381
- ): void {
382
- w.writeVarInt(msg.request_id);
383
- w.writeVarInt(msg.error_code);
384
- w.writeString(msg.reason_phrase);
385
- }
386
-
387
- function encodeUnsubscribeAnnouncesPayload(
388
- msg: Draft11Message & { type: "unsubscribe_announces" },
389
- w: BufferWriter,
390
- ): void {
391
- w.writeTuple(msg.track_namespace_prefix);
392
- }
393
-
394
- function encodeFetchPayload(msg: Draft11Message & { type: "fetch" }, w: BufferWriter): void {
395
- w.writeVarInt(msg.request_id);
396
- w.writeUint8(msg.subscriber_priority);
397
- w.writeVarInt(msg.group_order);
398
- w.writeVarInt(msg.fetch_type);
399
- const ft = Number(msg.fetch_type);
400
- if (ft === 1 && msg.standalone) {
401
- w.writeTuple(msg.standalone.track_namespace);
402
- w.writeString(msg.standalone.track_name);
403
- w.writeVarInt(msg.standalone.start_group);
404
- w.writeVarInt(msg.standalone.start_object);
405
- w.writeVarInt(msg.standalone.end_group);
406
- w.writeVarInt(msg.standalone.end_object);
407
- } else if ((ft === 2 || ft === 3) && msg.joining) {
408
- w.writeVarInt(msg.joining.joining_subscribe_id);
409
- w.writeVarInt(msg.joining.joining_start);
410
- }
411
- encodeParams(msg.parameters, w);
412
- }
413
-
414
- function encodeFetchOkPayload(msg: Draft11Message & { type: "fetch_ok" }, w: BufferWriter): void {
415
- w.writeVarInt(msg.request_id);
416
- w.writeVarInt(msg.group_order);
417
- w.writeVarInt(msg.end_of_track);
418
- w.writeVarInt(msg.end_location.group);
419
- w.writeVarInt(msg.end_location.object);
420
- encodeParams(msg.parameters, w);
421
- }
422
-
423
- function encodeFetchErrorPayload(
424
- msg: Draft11Message & { type: "fetch_error" },
425
- w: BufferWriter,
426
- ): void {
427
- w.writeVarInt(msg.request_id);
428
- w.writeVarInt(msg.error_code);
429
- w.writeString(msg.reason_phrase);
430
- }
431
-
432
- function encodeFetchCancelPayload(
433
- msg: Draft11Message & { type: "fetch_cancel" },
434
- w: BufferWriter,
435
- ): void {
436
- w.writeVarInt(msg.request_id);
437
- }
438
-
439
- function encodeTrackStatusRequestPayload(
440
- msg: Draft11Message & { type: "track_status_request" },
441
- w: BufferWriter,
442
- ): void {
443
- w.writeVarInt(msg.request_id);
444
- w.writeTuple(msg.track_namespace);
445
- w.writeString(msg.track_name);
446
- encodeParams(msg.parameters, w);
447
- }
448
-
449
- function encodeTrackStatusPayload(
450
- msg: Draft11Message & { type: "track_status" },
451
- w: BufferWriter,
452
- ): void {
453
- w.writeVarInt(msg.request_id);
454
- w.writeVarInt(msg.status_code);
455
- w.writeVarInt(msg.largest_location.group);
456
- w.writeVarInt(msg.largest_location.object);
457
- encodeParams(msg.parameters, w);
458
- }
459
-
460
- function encodeGoAwayPayload(msg: Draft11Message & { type: "goaway" }, w: BufferWriter): void {
461
- w.writeString(msg.new_session_uri);
462
- }
463
-
464
- function encodeMaxRequestIdPayload(
465
- msg: Draft11Message & { type: "max_request_id" },
466
- w: BufferWriter,
467
- ): void {
468
- w.writeVarInt(msg.request_id);
469
- }
470
-
471
- function encodeRequestsBlockedPayload(
472
- msg: Draft11Message & { type: "requests_blocked" },
473
- w: BufferWriter,
474
- ): void {
475
- w.writeVarInt(msg.maximum_request_id);
476
- }
477
-
478
- // ─── Payload Decoders ──────────────────────────────────────────────────────────
479
-
480
- function decodeClientSetupPayload(r: BufferReader): Draft11Message {
481
- const numVersions = Number(r.readVarInt());
482
- if (numVersions === 0) {
483
- throw new DecodeError("CONSTRAINT_VIOLATION", "supported_versions must not be empty", r.offset);
484
- }
485
- const supported_versions: bigint[] = [];
486
- for (let i = 0; i < numVersions; i++) supported_versions.push(r.readVarInt());
487
- const parameters = decodeSetupParams(r);
488
- return { type: "client_setup", supported_versions, parameters };
489
- }
490
-
491
- function decodeServerSetupPayload(r: BufferReader): Draft11Message {
492
- const selected_version = r.readVarInt();
493
- const parameters = decodeSetupParams(r);
494
- return { type: "server_setup", selected_version, parameters };
495
- }
496
-
497
- function decodeSubscribePayload(r: BufferReader): Draft11Message {
498
- const request_id = r.readVarInt();
499
- const track_alias = r.readVarInt();
500
- const track_namespace = r.readTuple();
501
- const track_name = r.readString();
502
- const subscriber_priority = r.readUint8();
503
- const group_order = r.readVarInt();
504
- const forward = r.readVarInt();
505
- const filter_type = r.readVarInt();
506
- const ft = Number(filter_type);
507
- if (ft < 1 || ft > 4) {
508
- throw new DecodeError("CONSTRAINT_VIOLATION", `Invalid filter_type: ${ft}`, r.offset);
509
- }
510
- let start_group: bigint | undefined;
511
- let start_object: bigint | undefined;
512
- let end_group: bigint | undefined;
513
- if (ft === 3 || ft === 4) {
514
- start_group = r.readVarInt();
515
- start_object = r.readVarInt();
516
- }
517
- if (ft === 4) {
518
- end_group = r.readVarInt();
519
- }
520
- const parameters = decodeParams(r);
521
- const msg: Record<string, unknown> = {
522
- type: "subscribe",
523
- request_id,
524
- track_alias,
525
- track_namespace,
526
- track_name,
527
- subscriber_priority,
528
- group_order,
529
- forward,
530
- filter_type,
531
- parameters,
532
- };
533
- if (start_group !== undefined) msg.start_group = start_group;
534
- if (start_object !== undefined) msg.start_object = start_object;
535
- if (end_group !== undefined) msg.end_group = end_group;
536
- return msg as unknown as Draft11Message;
537
- }
538
-
539
- function decodeSubscribeOkPayload(r: BufferReader): Draft11Message {
540
- const request_id = r.readVarInt();
541
- const expires = r.readVarInt();
542
- const group_order = r.readVarInt();
543
- const content_exists = r.readVarInt();
544
- let largest_location: LargestLocation | undefined;
545
- if (content_exists === 1n) {
546
- const group = r.readVarInt();
547
- const object = r.readVarInt();
548
- largest_location = { group, object };
549
- }
550
- const parameters = decodeParams(r);
551
- const msg: Record<string, unknown> = {
552
- type: "subscribe_ok",
553
- request_id,
554
- expires,
555
- group_order,
556
- content_exists,
557
- parameters,
558
- };
559
- if (largest_location) msg.largest_location = largest_location;
560
- return msg as unknown as Draft11Message;
561
- }
562
-
563
- function decodeSubscribeErrorPayload(r: BufferReader): Draft11Message {
564
- const request_id = r.readVarInt();
565
- const error_code = r.readVarInt();
566
- const reason_phrase = r.readString();
567
- const track_alias = r.readVarInt();
568
- return { type: "subscribe_error", request_id, error_code, reason_phrase, track_alias };
569
- }
570
-
571
- function decodeSubscribeUpdatePayload(r: BufferReader): Draft11Message {
572
- const request_id = r.readVarInt();
573
- const start_group = r.readVarInt();
574
- const start_object = r.readVarInt();
575
- const end_group = r.readVarInt();
576
- const subscriber_priority = r.readUint8();
577
- const forward = r.readVarInt();
578
- const parameters = decodeParams(r);
579
- return {
580
- type: "subscribe_update",
581
- request_id,
582
- start_group,
583
- start_object,
584
- end_group,
585
- subscriber_priority,
586
- forward,
587
- parameters,
588
- };
589
- }
590
-
591
- function decodeSubscribeDonePayload(r: BufferReader): Draft11Message {
592
- const request_id = r.readVarInt();
593
- const status_code = r.readVarInt();
594
- const stream_count = r.readVarInt();
595
- const reason_phrase = r.readString();
596
- return { type: "subscribe_done", request_id, status_code, stream_count, reason_phrase };
597
- }
598
-
599
- function decodeUnsubscribePayload(r: BufferReader): Draft11Message {
600
- const request_id = r.readVarInt();
601
- return { type: "unsubscribe", request_id };
602
- }
603
-
604
- function decodeAnnouncePayload(r: BufferReader): Draft11Message {
605
- const request_id = r.readVarInt();
606
- const track_namespace = r.readTuple();
607
- const parameters = decodeParams(r);
608
- return { type: "announce", request_id, track_namespace, parameters };
609
- }
610
-
611
- function decodeAnnounceOkPayload(r: BufferReader): Draft11Message {
612
- const request_id = r.readVarInt();
613
- return { type: "announce_ok", request_id };
614
- }
615
-
616
- function decodeAnnounceErrorPayload(r: BufferReader): Draft11Message {
617
- const request_id = r.readVarInt();
618
- const error_code = r.readVarInt();
619
- const reason_phrase = r.readString();
620
- return { type: "announce_error", request_id, error_code, reason_phrase };
621
- }
622
-
623
- function decodeUnannouncePayload(r: BufferReader): Draft11Message {
624
- const track_namespace = r.readTuple();
625
- return { type: "unannounce", track_namespace };
626
- }
627
-
628
- function decodeAnnounceCancelPayload(r: BufferReader): Draft11Message {
629
- const track_namespace = r.readTuple();
630
- const error_code = r.readVarInt();
631
- const reason_phrase = r.readString();
632
- return { type: "announce_cancel", track_namespace, error_code, reason_phrase };
633
- }
634
-
635
- function decodeSubscribeAnnouncesPayload(r: BufferReader): Draft11Message {
636
- const request_id = r.readVarInt();
637
- const track_namespace_prefix = r.readTuple();
638
- const parameters = decodeParams(r);
639
- return { type: "subscribe_announces", request_id, track_namespace_prefix, parameters };
640
- }
641
-
642
- function decodeSubscribeAnnouncesOkPayload(r: BufferReader): Draft11Message {
643
- const request_id = r.readVarInt();
644
- return { type: "subscribe_announces_ok", request_id };
645
- }
646
-
647
- function decodeSubscribeAnnouncesErrorPayload(r: BufferReader): Draft11Message {
648
- const request_id = r.readVarInt();
649
- const error_code = r.readVarInt();
650
- const reason_phrase = r.readString();
651
- return { type: "subscribe_announces_error", request_id, error_code, reason_phrase };
652
- }
653
-
654
- function decodeUnsubscribeAnnouncesPayload(r: BufferReader): Draft11Message {
655
- const track_namespace_prefix = r.readTuple();
656
- return { type: "unsubscribe_announces", track_namespace_prefix };
657
- }
658
-
659
- function decodeFetchPayload(r: BufferReader): Draft11Message {
660
- const request_id = r.readVarInt();
661
- const subscriber_priority = r.readUint8();
662
- const group_order = r.readVarInt();
663
- const fetch_type = r.readVarInt();
664
- const ft = Number(fetch_type);
665
-
666
- if (ft < 1 || ft > 3) {
667
- throw new DecodeError("CONSTRAINT_VIOLATION", `Invalid fetch_type: ${ft}`, r.offset);
668
- }
669
-
670
- let standalone: StandaloneFetch | undefined;
671
- let joining: JoiningFetch | undefined;
672
-
673
- if (ft === 1) {
674
- const track_namespace = r.readTuple();
675
- const track_name = r.readString();
676
- const start_group = r.readVarInt();
677
- const start_object = r.readVarInt();
678
- const end_group = r.readVarInt();
679
- const end_object = r.readVarInt();
680
- standalone = { track_namespace, track_name, start_group, start_object, end_group, end_object };
681
- } else {
682
- const joining_subscribe_id = r.readVarInt();
683
- const joining_start = r.readVarInt();
684
- joining = { joining_subscribe_id, joining_start };
685
- }
686
-
687
- const parameters = decodeParams(r);
688
- return {
689
- type: "fetch",
690
- request_id,
691
- subscriber_priority,
692
- group_order,
693
- fetch_type,
694
- standalone,
695
- joining,
696
- parameters,
697
- } as Draft11Fetch;
698
- }
699
-
700
- function decodeFetchOkPayload(r: BufferReader): Draft11Message {
701
- const request_id = r.readVarInt();
702
- const group_order = r.readVarInt();
703
- const end_of_track = r.readVarInt();
704
- const group = r.readVarInt();
705
- const object = r.readVarInt();
706
- const parameters = decodeParams(r);
707
- return {
708
- type: "fetch_ok",
709
- request_id,
710
- group_order,
711
- end_of_track,
712
- end_location: { group, object },
713
- parameters,
714
- };
715
- }
716
-
717
- function decodeFetchErrorPayload(r: BufferReader): Draft11Message {
718
- const request_id = r.readVarInt();
719
- const error_code = r.readVarInt();
720
- const reason_phrase = r.readString();
721
- return { type: "fetch_error", request_id, error_code, reason_phrase };
722
- }
723
-
724
- function decodeFetchCancelPayload(r: BufferReader): Draft11Message {
725
- const request_id = r.readVarInt();
726
- return { type: "fetch_cancel", request_id };
727
- }
728
-
729
- function decodeTrackStatusRequestPayload(r: BufferReader): Draft11Message {
730
- const request_id = r.readVarInt();
731
- const track_namespace = r.readTuple();
732
- const track_name = r.readString();
733
- const parameters = decodeParams(r);
734
- return { type: "track_status_request", request_id, track_namespace, track_name, parameters };
735
- }
736
-
737
- function decodeTrackStatusPayload(r: BufferReader): Draft11Message {
738
- const request_id = r.readVarInt();
739
- const status_code = r.readVarInt();
740
- const group = r.readVarInt();
741
- const object = r.readVarInt();
742
- const parameters = decodeParams(r);
743
- return {
744
- type: "track_status",
745
- request_id,
746
- status_code,
747
- largest_location: { group, object },
748
- parameters,
749
- };
750
- }
751
-
752
- function decodeGoAwayPayload(r: BufferReader): Draft11Message {
753
- const new_session_uri = r.readString();
754
- return { type: "goaway", new_session_uri };
755
- }
756
-
757
- function decodeMaxRequestIdPayload(r: BufferReader): Draft11Message {
758
- const request_id = r.readVarInt();
759
- return { type: "max_request_id", request_id };
760
- }
761
-
762
- function decodeRequestsBlockedPayload(r: BufferReader): Draft11Message {
763
- const maximum_request_id = r.readVarInt();
764
- return { type: "requests_blocked", maximum_request_id };
765
- }
766
-
767
- // ─── Payload dispatch tables ───────────────────────────────────────────────────
768
-
769
- const payloadDecoders: ReadonlyMap<bigint, (r: BufferReader) => Draft11Message> = new Map([
770
- [MSG_CLIENT_SETUP, decodeClientSetupPayload],
771
- [MSG_SERVER_SETUP, decodeServerSetupPayload],
772
- [MSG_SUBSCRIBE, decodeSubscribePayload],
773
- [MSG_SUBSCRIBE_OK, decodeSubscribeOkPayload],
774
- [MSG_SUBSCRIBE_ERROR, decodeSubscribeErrorPayload],
775
- [MSG_SUBSCRIBE_UPDATE, decodeSubscribeUpdatePayload],
776
- [MSG_SUBSCRIBE_DONE, decodeSubscribeDonePayload],
777
- [MSG_UNSUBSCRIBE, decodeUnsubscribePayload],
778
- [MSG_ANNOUNCE, decodeAnnouncePayload],
779
- [MSG_ANNOUNCE_OK, decodeAnnounceOkPayload],
780
- [MSG_ANNOUNCE_ERROR, decodeAnnounceErrorPayload],
781
- [MSG_UNANNOUNCE, decodeUnannouncePayload],
782
- [MSG_ANNOUNCE_CANCEL, decodeAnnounceCancelPayload],
783
- [MSG_SUBSCRIBE_ANNOUNCES, decodeSubscribeAnnouncesPayload],
784
- [MSG_SUBSCRIBE_ANNOUNCES_OK, decodeSubscribeAnnouncesOkPayload],
785
- [MSG_SUBSCRIBE_ANNOUNCES_ERROR, decodeSubscribeAnnouncesErrorPayload],
786
- [MSG_UNSUBSCRIBE_ANNOUNCES, decodeUnsubscribeAnnouncesPayload],
787
- [MSG_FETCH, decodeFetchPayload],
788
- [MSG_FETCH_OK, decodeFetchOkPayload],
789
- [MSG_FETCH_ERROR, decodeFetchErrorPayload],
790
- [MSG_FETCH_CANCEL, decodeFetchCancelPayload],
791
- [MSG_TRACK_STATUS_REQUEST, decodeTrackStatusRequestPayload],
792
- [MSG_TRACK_STATUS, decodeTrackStatusPayload],
793
- [MSG_GOAWAY, decodeGoAwayPayload],
794
- [MSG_MAX_REQUEST_ID, decodeMaxRequestIdPayload],
795
- [MSG_REQUESTS_BLOCKED, decodeRequestsBlockedPayload],
796
- ]);
797
-
798
- // ─── Public API ────────────────────────────────────────────────────────────────
799
-
800
- /**
801
- * Encode a draft-11 control message with type(varint) + length(varint) + payload.
802
- */
803
- export function encodeMessage(message: Draft11Message): Uint8Array {
804
- const typeId = MESSAGE_ID_MAP.get(message.type);
805
- if (typeId === undefined) throw new Error(`Unknown message type: ${message.type}`);
806
-
807
- const payloadWriter = new BufferWriter();
808
- encodePayload(message, payloadWriter);
809
- const payload = payloadWriter.finishView();
810
-
811
- if (payload.byteLength > 0xffff) {
812
- throw new Error(`Payload too large for 16-bit length: ${payload.byteLength}`);
813
- }
814
-
815
- // Write framed message: type(varint) + length(uint16 BE) + payload
816
- const writer = new BufferWriter(payload.byteLength + 16);
817
- writer.writeVarInt(typeId);
818
- writer.writeUint8((payload.byteLength >> 8) & 0xff);
819
- writer.writeUint8(payload.byteLength & 0xff);
820
- writer.writeBytes(payload);
821
- return writer.finish();
822
- }
823
-
824
- function encodePayload(msg: Draft11Message, w: BufferWriter): void {
825
- switch (msg.type) {
826
- case "client_setup":
827
- return encodeClientSetupPayload(msg, w);
828
- case "server_setup":
829
- return encodeServerSetupPayload(msg, w);
830
- case "subscribe":
831
- return encodeSubscribePayload(msg, w);
832
- case "subscribe_ok":
833
- return encodeSubscribeOkPayload(msg, w);
834
- case "subscribe_error":
835
- return encodeSubscribeErrorPayload(msg, w);
836
- case "subscribe_update":
837
- return encodeSubscribeUpdatePayload(msg, w);
838
- case "subscribe_done":
839
- return encodeSubscribeDonePayload(msg, w);
840
- case "unsubscribe":
841
- return encodeUnsubscribePayload(msg, w);
842
- case "announce":
843
- return encodeAnnouncePayload(msg, w);
844
- case "announce_ok":
845
- return encodeAnnounceOkPayload(msg, w);
846
- case "announce_error":
847
- return encodeAnnounceErrorPayload(msg, w);
848
- case "unannounce":
849
- return encodeUnannouncePayload(msg, w);
850
- case "announce_cancel":
851
- return encodeAnnounceCancelPayload(msg, w);
852
- case "subscribe_announces":
853
- return encodeSubscribeAnnouncesPayload(msg, w);
854
- case "subscribe_announces_ok":
855
- return encodeSubscribeAnnouncesOkPayload(msg, w);
856
- case "subscribe_announces_error":
857
- return encodeSubscribeAnnouncesErrorPayload(msg, w);
858
- case "unsubscribe_announces":
859
- return encodeUnsubscribeAnnouncesPayload(msg, w);
860
- case "fetch":
861
- return encodeFetchPayload(msg, w);
862
- case "fetch_ok":
863
- return encodeFetchOkPayload(msg, w);
864
- case "fetch_error":
865
- return encodeFetchErrorPayload(msg, w);
866
- case "fetch_cancel":
867
- return encodeFetchCancelPayload(msg, w);
868
- case "track_status_request":
869
- return encodeTrackStatusRequestPayload(msg, w);
870
- case "track_status":
871
- return encodeTrackStatusPayload(msg, w);
872
- case "goaway":
873
- return encodeGoAwayPayload(msg, w);
874
- case "max_request_id":
875
- return encodeMaxRequestIdPayload(msg, w);
876
- case "requests_blocked":
877
- return encodeRequestsBlockedPayload(msg, w);
878
- default: {
879
- const _exhaustive: never = msg;
880
- throw new Error(`Unhandled message type: ${(_exhaustive as Draft11Message).type}`);
881
- }
882
- }
883
- }
884
-
885
- /**
886
- * Decode a draft-11 control message from bytes (type + uint16 length + payload).
887
- */
888
- export function decodeMessage(bytes: Uint8Array): DecodeResult<Draft11Message> {
889
- try {
890
- const reader = new BufferReader(bytes);
891
- const typeId = reader.readVarInt();
892
-
893
- // Read 16-bit big-endian payload length
894
- const lenHi = reader.readUint8();
895
- const lenLo = reader.readUint8();
896
- const payloadLength = (lenHi << 8) | lenLo;
897
-
898
- const payloadBytes = reader.readBytes(payloadLength);
899
- const payloadReader = new BufferReader(payloadBytes);
900
-
901
- const decoder = payloadDecoders.get(typeId);
902
- if (!decoder) {
903
- return {
904
- ok: false,
905
- error: new DecodeError(
906
- "UNKNOWN_MESSAGE_TYPE",
907
- `Unknown message type ID: 0x${typeId.toString(16)}`,
908
- 0,
909
- ),
910
- };
911
- }
912
-
913
- const message = decoder(payloadReader);
914
- return { ok: true, value: message, bytesRead: reader.offset };
915
- } catch (e) {
916
- if (e instanceof DecodeError) return { ok: false, error: e };
917
- throw e;
918
- }
919
- }
920
-
921
- // ─── Data Stream Encoding/Decoding ─────────────────────────────────────────────
922
-
923
- export { encodeSubgroupStream, encodeDatagram, encodeFetchStream, decodeSubgroupStream, decodeDatagram, decodeFetchStream, decodeDataStream } from "./data-streams.js";
924
- import { encodeSubgroupStream, encodeDatagram, encodeFetchStream, decodeSubgroupStream, decodeDatagram, decodeFetchStream, decodeDataStream } from "./data-streams.js";
925
-
926
- export function createStreamDecoder(): TransformStream<Uint8Array, Draft11Message> {
927
- let buffer = new Uint8Array(0);
928
- let offset = 0;
929
- return new TransformStream<Uint8Array, Draft11Message>({
930
- transform(chunk, controller) {
931
- if (offset > 0) {
932
- buffer = buffer.subarray(offset);
933
- offset = 0;
934
- }
935
- const newBuffer = new Uint8Array(buffer.length + chunk.length);
936
- newBuffer.set(buffer, 0);
937
- newBuffer.set(chunk, buffer.length);
938
- buffer = newBuffer;
939
- while (offset < buffer.length) {
940
- const result = decodeMessage(buffer.subarray(offset));
941
- if (!result.ok) {
942
- if (result.error.code === "UNEXPECTED_END") break;
943
- controller.error(result.error);
944
- return;
945
- }
946
- controller.enqueue(result.value);
947
- offset += result.bytesRead;
948
- }
949
- },
950
- flush(controller) {
951
- if (offset < buffer.length)
952
- controller.error(new DecodeError("UNEXPECTED_END", "Stream ended with incomplete data", 0));
953
- },
954
- });
955
- }
956
-
957
- // ─── Codec Factory ─────────────────────────────────────────────────────────────
958
-
959
- export interface Draft11Codec extends BaseCodec<Draft11Message> {
960
- readonly draft: "draft-ietf-moq-transport-11";
961
- encodeSubgroupStream(stream: SubgroupStream): Uint8Array;
962
- encodeDatagram(dg: DatagramObject): Uint8Array;
963
- encodeFetchStream(stream: FetchStream): Uint8Array;
964
- decodeSubgroupStream(bytes: Uint8Array): DecodeResult<SubgroupStream>;
965
- decodeDatagram(bytes: Uint8Array): DecodeResult<DatagramObject>;
966
- decodeFetchStream(bytes: Uint8Array): DecodeResult<FetchStream>;
967
- decodeDataStream(
968
- streamType: "subgroup" | "datagram" | "fetch",
969
- bytes: Uint8Array,
970
- ): DecodeResult<Draft11DataStream>;
971
- createStreamDecoder(): TransformStream<Uint8Array, Draft11Message>;
972
- }
973
-
974
- export function createDraft11Codec(): Draft11Codec {
975
- return {
976
- draft: "draft-ietf-moq-transport-11",
977
- encodeMessage,
978
- decodeMessage,
979
- encodeSubgroupStream,
980
- encodeDatagram,
981
- encodeFetchStream,
982
- decodeSubgroupStream,
983
- decodeDatagram,
984
- decodeFetchStream,
985
- decodeDataStream,
986
- createStreamDecoder,
987
- };
988
- }
1
+ import { BufferReader } from '../../core/buffer-reader.js'
2
+ import { BufferWriter } from '../../core/buffer-writer.js'
3
+ import { bytesToHex, hexToBytes } from '../../core/hex.js'
4
+ import type { BaseCodec, DecodeResult } from '../../core/types.js'
5
+ import { DecodeError } from '../../core/types.js'
6
+ import {
7
+ decodeDatagram,
8
+ decodeDataStream,
9
+ decodeFetchStream,
10
+ decodeSubgroupStream,
11
+ encodeDatagram,
12
+ encodeFetchStream,
13
+ encodeSubgroupStream,
14
+ } from './data-streams.js'
15
+ import {
16
+ MESSAGE_ID_MAP,
17
+ MSG_ANNOUNCE,
18
+ MSG_ANNOUNCE_CANCEL,
19
+ MSG_ANNOUNCE_ERROR,
20
+ MSG_ANNOUNCE_OK,
21
+ MSG_CLIENT_SETUP,
22
+ MSG_FETCH,
23
+ MSG_FETCH_CANCEL,
24
+ MSG_FETCH_ERROR,
25
+ MSG_FETCH_OK,
26
+ MSG_GOAWAY,
27
+ MSG_MAX_REQUEST_ID,
28
+ MSG_REQUESTS_BLOCKED,
29
+ MSG_SERVER_SETUP,
30
+ MSG_SUBSCRIBE,
31
+ MSG_SUBSCRIBE_ANNOUNCES,
32
+ MSG_SUBSCRIBE_ANNOUNCES_ERROR,
33
+ MSG_SUBSCRIBE_ANNOUNCES_OK,
34
+ MSG_SUBSCRIBE_DONE,
35
+ MSG_SUBSCRIBE_ERROR,
36
+ MSG_SUBSCRIBE_OK,
37
+ MSG_SUBSCRIBE_UPDATE,
38
+ MSG_TRACK_STATUS,
39
+ MSG_TRACK_STATUS_REQUEST,
40
+ MSG_UNANNOUNCE,
41
+ MSG_UNSUBSCRIBE,
42
+ MSG_UNSUBSCRIBE_ANNOUNCES,
43
+ PARAM_AUTHORIZATION_TOKEN,
44
+ PARAM_DELIVERY_TIMEOUT,
45
+ PARAM_MAX_CACHE_DURATION,
46
+ SETUP_PARAM_MAX_REQUEST_ID,
47
+ SETUP_PARAM_PATH,
48
+ } from './messages.js'
49
+ import type {
50
+ DatagramObject,
51
+ Draft11DataStream,
52
+ Draft11Fetch,
53
+ Draft11Message,
54
+ Draft11Params,
55
+ Draft11SetupParams,
56
+ FetchStream,
57
+ JoiningFetch,
58
+ LargestLocation,
59
+ StandaloneFetch,
60
+ SubgroupStream,
61
+ UnknownParam,
62
+ } from './types.js'
63
+
64
+ const textEncoder = /* @__PURE__ */ new TextEncoder()
65
+ const textDecoder = /* @__PURE__ */ new TextDecoder()
66
+
67
+ // ─── Setup Parameter Encoding/Decoding (even/odd convention) ────────────────────
68
+
69
+ function encodeSetupParams(params: Draft11SetupParams, w: BufferWriter): void {
70
+ let count = 0
71
+ if (params.path !== undefined) count++
72
+ if (params.max_request_id !== undefined) count++
73
+ if (params.unknown) count += params.unknown.length
74
+
75
+ w.writeVarInt(count)
76
+
77
+ if (params.path !== undefined) {
78
+ w.writeVarInt(SETUP_PARAM_PATH)
79
+ const encoded = textEncoder.encode(params.path)
80
+ w.writeVarInt(encoded.byteLength)
81
+ w.writeBytes(encoded)
82
+ }
83
+ if (params.max_request_id !== undefined) {
84
+ w.writeVarInt(SETUP_PARAM_MAX_REQUEST_ID)
85
+ w.writeVarInt(params.max_request_id)
86
+ }
87
+ if (params.unknown) {
88
+ for (const u of params.unknown) {
89
+ const id = BigInt(u.id)
90
+ w.writeVarInt(id)
91
+ if (id % 2n === 0n) {
92
+ const raw = hexToBytes(u.raw_hex)
93
+ const tmpReader = new BufferReader(raw)
94
+ w.writeVarInt(tmpReader.readVarInt())
95
+ } else {
96
+ const raw = hexToBytes(u.raw_hex)
97
+ w.writeVarInt(raw.byteLength)
98
+ w.writeBytes(raw)
99
+ }
100
+ }
101
+ }
102
+ }
103
+
104
+ function decodeSetupParams(r: BufferReader): Draft11SetupParams {
105
+ const count = Number(r.readVarInt())
106
+ const result: Draft11SetupParams = {}
107
+ const unknown: UnknownParam[] = []
108
+
109
+ for (let i = 0; i < count; i++) {
110
+ const paramType = r.readVarInt()
111
+ if (paramType % 2n === 0n) {
112
+ const value = r.readVarInt()
113
+ if (paramType === SETUP_PARAM_MAX_REQUEST_ID) {
114
+ result.max_request_id = value
115
+ } else {
116
+ const tmpW = new BufferWriter(16)
117
+ tmpW.writeVarInt(value)
118
+ const raw = tmpW.finish()
119
+ unknown.push({
120
+ id: `0x${paramType.toString(16)}`,
121
+ length: raw.byteLength,
122
+ raw_hex: bytesToHex(raw),
123
+ })
124
+ }
125
+ } else {
126
+ const length = Number(r.readVarInt())
127
+ const bytes = r.readBytes(length)
128
+ if (paramType === SETUP_PARAM_PATH) {
129
+ result.path = textDecoder.decode(bytes)
130
+ } else {
131
+ unknown.push({
132
+ id: `0x${paramType.toString(16)}`,
133
+ length,
134
+ raw_hex: bytesToHex(bytes),
135
+ })
136
+ }
137
+ }
138
+ }
139
+
140
+ if (unknown.length > 0) result.unknown = unknown
141
+ return result
142
+ }
143
+
144
+ // ─── Version-Specific Parameter Encoding/Decoding ───────────────────────────────
145
+ // Uses even/odd convention: even type → bare varint value, odd type → length-prefixed blob
146
+
147
+ function encodeParams(params: Draft11Params, w: BufferWriter): void {
148
+ let count = params.unknown ? params.unknown.length : 0
149
+ if (params.authorization_token !== undefined) count++
150
+ if (params.delivery_timeout !== undefined) count++
151
+ if (params.max_cache_duration !== undefined) count++
152
+ w.writeVarInt(count)
153
+
154
+ if (params.authorization_token !== undefined) {
155
+ // AUTHORIZATION_TOKEN = 0x01 (oddlength-prefixed)
156
+ w.writeVarInt(PARAM_AUTHORIZATION_TOKEN)
157
+ const at = params.authorization_token
158
+ const tmpW = new BufferWriter(64)
159
+ tmpW.writeVarInt(at.alias_type)
160
+ tmpW.writeVarInt(at.token_type)
161
+ const tokenBytes = textEncoder.encode(at.token_value)
162
+ tmpW.writeBytes(tokenBytes)
163
+ const raw = tmpW.finish()
164
+ w.writeVarInt(raw.byteLength)
165
+ w.writeBytes(raw)
166
+ }
167
+ if (params.delivery_timeout !== undefined) {
168
+ // DELIVERY_TIMEOUT = 0x02 (even → bare varint)
169
+ w.writeVarInt(PARAM_DELIVERY_TIMEOUT)
170
+ w.writeVarInt(params.delivery_timeout)
171
+ }
172
+ if (params.max_cache_duration !== undefined) {
173
+ // MAX_CACHE_DURATION = 0x04 (even → bare varint)
174
+ w.writeVarInt(PARAM_MAX_CACHE_DURATION)
175
+ w.writeVarInt(params.max_cache_duration)
176
+ }
177
+ if (params.unknown) {
178
+ for (const u of params.unknown) {
179
+ const id = BigInt(u.id)
180
+ w.writeVarInt(id)
181
+ if (id % 2n === 0n) {
182
+ // Even → bare varint
183
+ const raw = hexToBytes(u.raw_hex)
184
+ const tmpReader = new BufferReader(raw)
185
+ w.writeVarInt(tmpReader.readVarInt())
186
+ } else {
187
+ // Oddlength-prefixed
188
+ const raw = hexToBytes(u.raw_hex)
189
+ w.writeVarInt(raw.byteLength)
190
+ w.writeBytes(raw)
191
+ }
192
+ }
193
+ }
194
+ }
195
+
196
+ function decodeParams(r: BufferReader): Draft11Params {
197
+ const count = Number(r.readVarInt())
198
+ const result: Draft11Params = {}
199
+ const unknown: UnknownParam[] = []
200
+
201
+ for (let i = 0; i < count; i++) {
202
+ const paramType = r.readVarInt()
203
+
204
+ if (paramType % 2n === 0n) {
205
+ // Even type → bare varint value
206
+ const value = r.readVarInt()
207
+ if (paramType === PARAM_DELIVERY_TIMEOUT) {
208
+ result.delivery_timeout = value
209
+ } else if (paramType === PARAM_MAX_CACHE_DURATION) {
210
+ result.max_cache_duration = value
211
+ } else {
212
+ const tmpW = new BufferWriter(16)
213
+ tmpW.writeVarInt(value)
214
+ const raw = tmpW.finish()
215
+ unknown.push({
216
+ id: `0x${paramType.toString(16)}`,
217
+ length: raw.byteLength,
218
+ raw_hex: bytesToHex(raw),
219
+ })
220
+ }
221
+ } else {
222
+ // Odd type → length-prefixed blob
223
+ const length = Number(r.readVarInt())
224
+ const startOff = r.offset
225
+
226
+ if (paramType === PARAM_AUTHORIZATION_TOKEN) {
227
+ const alias_type = r.readVarInt()
228
+ const token_type = r.readVarInt()
229
+ const tokenBytesLen = length - (r.offset - startOff)
230
+ const tokenBytes = r.readBytes(tokenBytesLen)
231
+ result.authorization_token = {
232
+ alias_type,
233
+ token_type,
234
+ token_value: textDecoder.decode(tokenBytes),
235
+ }
236
+ } else {
237
+ const bytes = r.readBytes(length)
238
+ unknown.push({
239
+ id: `0x${paramType.toString(16)}`,
240
+ length,
241
+ raw_hex: bytesToHex(bytes),
242
+ })
243
+ }
244
+ }
245
+ }
246
+
247
+ if (unknown.length > 0) result.unknown = unknown
248
+ return result
249
+ }
250
+
251
+ // ─── Payload Encoders ──────────────────────────────────────────────────────────
252
+
253
+ function encodeClientSetupPayload(
254
+ msg: Draft11Message & { type: 'client_setup' },
255
+ w: BufferWriter,
256
+ ): void {
257
+ w.writeVarInt(msg.supported_versions.length)
258
+ for (const v of msg.supported_versions) w.writeVarInt(v)
259
+ encodeSetupParams(msg.parameters, w)
260
+ }
261
+
262
+ function encodeServerSetupPayload(
263
+ msg: Draft11Message & { type: 'server_setup' },
264
+ w: BufferWriter,
265
+ ): void {
266
+ w.writeVarInt(msg.selected_version)
267
+ encodeSetupParams(msg.parameters, w)
268
+ }
269
+
270
+ function encodeSubscribePayload(
271
+ msg: Draft11Message & { type: 'subscribe' },
272
+ w: BufferWriter,
273
+ ): void {
274
+ w.writeVarInt(msg.request_id)
275
+ w.writeVarInt(msg.track_alias)
276
+ w.writeTuple(msg.track_namespace)
277
+ w.writeString(msg.track_name)
278
+ w.writeUint8(msg.subscriber_priority)
279
+ w.writeUint8(msg.group_order)
280
+ w.writeVarInt(msg.forward)
281
+ w.writeVarInt(msg.filter_type)
282
+ const ft = Number(msg.filter_type)
283
+ if (ft === 3 || ft === 4) {
284
+ w.writeVarInt(msg.start_group!)
285
+ w.writeVarInt(msg.start_object!)
286
+ }
287
+ if (ft === 4) {
288
+ w.writeVarInt(msg.end_group!)
289
+ }
290
+ encodeParams(msg.parameters, w)
291
+ }
292
+
293
+ function encodeSubscribeOkPayload(
294
+ msg: Draft11Message & { type: 'subscribe_ok' },
295
+ w: BufferWriter,
296
+ ): void {
297
+ w.writeVarInt(msg.request_id)
298
+ w.writeVarInt(msg.expires)
299
+ w.writeUint8(msg.group_order)
300
+ w.writeVarInt(msg.content_exists)
301
+ if (msg.content_exists === 1n && msg.largest_location) {
302
+ w.writeVarInt(msg.largest_location.group)
303
+ w.writeVarInt(msg.largest_location.object)
304
+ }
305
+ encodeParams(msg.parameters, w)
306
+ }
307
+
308
+ function encodeSubscribeErrorPayload(
309
+ msg: Draft11Message & { type: 'subscribe_error' },
310
+ w: BufferWriter,
311
+ ): void {
312
+ w.writeVarInt(msg.request_id)
313
+ w.writeVarInt(msg.error_code)
314
+ w.writeString(msg.reason_phrase)
315
+ w.writeVarInt(msg.track_alias)
316
+ }
317
+
318
+ function encodeSubscribeUpdatePayload(
319
+ msg: Draft11Message & { type: 'subscribe_update' },
320
+ w: BufferWriter,
321
+ ): void {
322
+ w.writeVarInt(msg.request_id)
323
+ w.writeVarInt(msg.start_group)
324
+ w.writeVarInt(msg.start_object)
325
+ w.writeVarInt(msg.end_group)
326
+ w.writeUint8(msg.subscriber_priority)
327
+ w.writeVarInt(msg.forward)
328
+ encodeParams(msg.parameters, w)
329
+ }
330
+
331
+ function encodeSubscribeDonePayload(
332
+ msg: Draft11Message & { type: 'subscribe_done' },
333
+ w: BufferWriter,
334
+ ): void {
335
+ w.writeVarInt(msg.request_id)
336
+ w.writeVarInt(msg.status_code)
337
+ w.writeVarInt(msg.stream_count)
338
+ w.writeString(msg.reason_phrase)
339
+ }
340
+
341
+ function encodeUnsubscribePayload(
342
+ msg: Draft11Message & { type: 'unsubscribe' },
343
+ w: BufferWriter,
344
+ ): void {
345
+ w.writeVarInt(msg.request_id)
346
+ }
347
+
348
+ function encodeAnnouncePayload(msg: Draft11Message & { type: 'announce' }, w: BufferWriter): void {
349
+ w.writeVarInt(msg.request_id)
350
+ w.writeTuple(msg.track_namespace)
351
+ encodeParams(msg.parameters, w)
352
+ }
353
+
354
+ function encodeAnnounceOkPayload(
355
+ msg: Draft11Message & { type: 'announce_ok' },
356
+ w: BufferWriter,
357
+ ): void {
358
+ w.writeVarInt(msg.request_id)
359
+ }
360
+
361
+ function encodeAnnounceErrorPayload(
362
+ msg: Draft11Message & { type: 'announce_error' },
363
+ w: BufferWriter,
364
+ ): void {
365
+ w.writeVarInt(msg.request_id)
366
+ w.writeVarInt(msg.error_code)
367
+ w.writeString(msg.reason_phrase)
368
+ }
369
+
370
+ function encodeUnannouncePayload(
371
+ msg: Draft11Message & { type: 'unannounce' },
372
+ w: BufferWriter,
373
+ ): void {
374
+ w.writeTuple(msg.track_namespace)
375
+ }
376
+
377
+ function encodeAnnounceCancelPayload(
378
+ msg: Draft11Message & { type: 'announce_cancel' },
379
+ w: BufferWriter,
380
+ ): void {
381
+ w.writeTuple(msg.track_namespace)
382
+ w.writeVarInt(msg.error_code)
383
+ w.writeString(msg.reason_phrase)
384
+ }
385
+
386
+ function encodeSubscribeAnnouncesPayload(
387
+ msg: Draft11Message & { type: 'subscribe_announces' },
388
+ w: BufferWriter,
389
+ ): void {
390
+ w.writeVarInt(msg.request_id)
391
+ w.writeTuple(msg.track_namespace_prefix)
392
+ encodeParams(msg.parameters, w)
393
+ }
394
+
395
+ function encodeSubscribeAnnouncesOkPayload(
396
+ msg: Draft11Message & { type: 'subscribe_announces_ok' },
397
+ w: BufferWriter,
398
+ ): void {
399
+ w.writeVarInt(msg.request_id)
400
+ }
401
+
402
+ function encodeSubscribeAnnouncesErrorPayload(
403
+ msg: Draft11Message & { type: 'subscribe_announces_error' },
404
+ w: BufferWriter,
405
+ ): void {
406
+ w.writeVarInt(msg.request_id)
407
+ w.writeVarInt(msg.error_code)
408
+ w.writeString(msg.reason_phrase)
409
+ }
410
+
411
+ function encodeUnsubscribeAnnouncesPayload(
412
+ msg: Draft11Message & { type: 'unsubscribe_announces' },
413
+ w: BufferWriter,
414
+ ): void {
415
+ w.writeTuple(msg.track_namespace_prefix)
416
+ }
417
+
418
+ function encodeFetchPayload(msg: Draft11Message & { type: 'fetch' }, w: BufferWriter): void {
419
+ w.writeVarInt(msg.request_id)
420
+ w.writeUint8(msg.subscriber_priority)
421
+ w.writeUint8(msg.group_order)
422
+ w.writeVarInt(msg.fetch_type)
423
+ const ft = Number(msg.fetch_type)
424
+ if (ft === 1 && msg.standalone) {
425
+ w.writeTuple(msg.standalone.track_namespace)
426
+ w.writeString(msg.standalone.track_name)
427
+ w.writeVarInt(msg.standalone.start_group)
428
+ w.writeVarInt(msg.standalone.start_object)
429
+ w.writeVarInt(msg.standalone.end_group)
430
+ w.writeVarInt(msg.standalone.end_object)
431
+ } else if ((ft === 2 || ft === 3) && msg.joining) {
432
+ w.writeVarInt(msg.joining.joining_subscribe_id)
433
+ w.writeVarInt(msg.joining.joining_start)
434
+ }
435
+ encodeParams(msg.parameters, w)
436
+ }
437
+
438
+ function encodeFetchOkPayload(msg: Draft11Message & { type: 'fetch_ok' }, w: BufferWriter): void {
439
+ w.writeVarInt(msg.request_id)
440
+ w.writeUint8(msg.group_order)
441
+ w.writeVarInt(msg.end_of_track)
442
+ w.writeVarInt(msg.end_location.group)
443
+ w.writeVarInt(msg.end_location.object)
444
+ encodeParams(msg.parameters, w)
445
+ }
446
+
447
+ function encodeFetchErrorPayload(
448
+ msg: Draft11Message & { type: 'fetch_error' },
449
+ w: BufferWriter,
450
+ ): void {
451
+ w.writeVarInt(msg.request_id)
452
+ w.writeVarInt(msg.error_code)
453
+ w.writeString(msg.reason_phrase)
454
+ }
455
+
456
+ function encodeFetchCancelPayload(
457
+ msg: Draft11Message & { type: 'fetch_cancel' },
458
+ w: BufferWriter,
459
+ ): void {
460
+ w.writeVarInt(msg.request_id)
461
+ }
462
+
463
+ function encodeTrackStatusRequestPayload(
464
+ msg: Draft11Message & { type: 'track_status_request' },
465
+ w: BufferWriter,
466
+ ): void {
467
+ w.writeVarInt(msg.request_id)
468
+ w.writeTuple(msg.track_namespace)
469
+ w.writeString(msg.track_name)
470
+ encodeParams(msg.parameters, w)
471
+ }
472
+
473
+ function encodeTrackStatusPayload(
474
+ msg: Draft11Message & { type: 'track_status' },
475
+ w: BufferWriter,
476
+ ): void {
477
+ w.writeVarInt(msg.request_id)
478
+ w.writeVarInt(msg.status_code)
479
+ w.writeVarInt(msg.largest_location.group)
480
+ w.writeVarInt(msg.largest_location.object)
481
+ encodeParams(msg.parameters, w)
482
+ }
483
+
484
+ function encodeGoAwayPayload(msg: Draft11Message & { type: 'goaway' }, w: BufferWriter): void {
485
+ w.writeString(msg.new_session_uri)
486
+ }
487
+
488
+ function encodeMaxRequestIdPayload(
489
+ msg: Draft11Message & { type: 'max_request_id' },
490
+ w: BufferWriter,
491
+ ): void {
492
+ w.writeVarInt(msg.request_id)
493
+ }
494
+
495
+ function encodeRequestsBlockedPayload(
496
+ msg: Draft11Message & { type: 'requests_blocked' },
497
+ w: BufferWriter,
498
+ ): void {
499
+ w.writeVarInt(msg.maximum_request_id)
500
+ }
501
+
502
+ // ─── Payload Decoders ──────────────────────────────────────────────────────────
503
+
504
+ function decodeClientSetupPayload(r: BufferReader): Draft11Message {
505
+ const numVersions = Number(r.readVarInt())
506
+ if (numVersions === 0) {
507
+ throw new DecodeError('CONSTRAINT_VIOLATION', 'supported_versions must not be empty', r.offset)
508
+ }
509
+ const supported_versions: bigint[] = []
510
+ for (let i = 0; i < numVersions; i++) supported_versions.push(r.readVarInt())
511
+ const parameters = decodeSetupParams(r)
512
+ return { type: 'client_setup', supported_versions, parameters }
513
+ }
514
+
515
+ function decodeServerSetupPayload(r: BufferReader): Draft11Message {
516
+ const selected_version = r.readVarInt()
517
+ const parameters = decodeSetupParams(r)
518
+ return { type: 'server_setup', selected_version, parameters }
519
+ }
520
+
521
+ function decodeSubscribePayload(r: BufferReader): Draft11Message {
522
+ const request_id = r.readVarInt()
523
+ const track_alias = r.readVarInt()
524
+ const track_namespace = r.readTuple()
525
+ const track_name = r.readString()
526
+ const subscriber_priority = r.readUint8()
527
+ const group_order = r.readUint8()
528
+ const forward = r.readVarInt()
529
+ const filter_type = r.readVarInt()
530
+ const ft = Number(filter_type)
531
+ if (ft < 1 || ft > 4) {
532
+ throw new DecodeError('CONSTRAINT_VIOLATION', `Invalid filter_type: ${ft}`, r.offset)
533
+ }
534
+ let start_group: bigint | undefined
535
+ let start_object: bigint | undefined
536
+ let end_group: bigint | undefined
537
+ if (ft === 3 || ft === 4) {
538
+ start_group = r.readVarInt()
539
+ start_object = r.readVarInt()
540
+ }
541
+ if (ft === 4) {
542
+ end_group = r.readVarInt()
543
+ }
544
+ const parameters = decodeParams(r)
545
+ const msg: Record<string, unknown> = {
546
+ type: 'subscribe',
547
+ request_id,
548
+ track_alias,
549
+ track_namespace,
550
+ track_name,
551
+ subscriber_priority,
552
+ group_order,
553
+ forward,
554
+ filter_type,
555
+ parameters,
556
+ }
557
+ if (start_group !== undefined) msg.start_group = start_group
558
+ if (start_object !== undefined) msg.start_object = start_object
559
+ if (end_group !== undefined) msg.end_group = end_group
560
+ return msg as unknown as Draft11Message
561
+ }
562
+
563
+ function decodeSubscribeOkPayload(r: BufferReader): Draft11Message {
564
+ const request_id = r.readVarInt()
565
+ const expires = r.readVarInt()
566
+ const group_order = r.readUint8()
567
+ const content_exists = r.readVarInt()
568
+ let largest_location: LargestLocation | undefined
569
+ if (content_exists === 1n) {
570
+ const group = r.readVarInt()
571
+ const object = r.readVarInt()
572
+ largest_location = { group, object }
573
+ }
574
+ const parameters = decodeParams(r)
575
+ const msg: Record<string, unknown> = {
576
+ type: 'subscribe_ok',
577
+ request_id,
578
+ expires,
579
+ group_order,
580
+ content_exists,
581
+ parameters,
582
+ }
583
+ if (largest_location) msg.largest_location = largest_location
584
+ return msg as unknown as Draft11Message
585
+ }
586
+
587
+ function decodeSubscribeErrorPayload(r: BufferReader): Draft11Message {
588
+ const request_id = r.readVarInt()
589
+ const error_code = r.readVarInt()
590
+ const reason_phrase = r.readString()
591
+ const track_alias = r.readVarInt()
592
+ return {
593
+ type: 'subscribe_error',
594
+ request_id,
595
+ error_code,
596
+ reason_phrase,
597
+ track_alias,
598
+ }
599
+ }
600
+
601
+ function decodeSubscribeUpdatePayload(r: BufferReader): Draft11Message {
602
+ const request_id = r.readVarInt()
603
+ const start_group = r.readVarInt()
604
+ const start_object = r.readVarInt()
605
+ const end_group = r.readVarInt()
606
+ const subscriber_priority = r.readUint8()
607
+ const forward = r.readVarInt()
608
+ const parameters = decodeParams(r)
609
+ return {
610
+ type: 'subscribe_update',
611
+ request_id,
612
+ start_group,
613
+ start_object,
614
+ end_group,
615
+ subscriber_priority,
616
+ forward,
617
+ parameters,
618
+ }
619
+ }
620
+
621
+ function decodeSubscribeDonePayload(r: BufferReader): Draft11Message {
622
+ const request_id = r.readVarInt()
623
+ const status_code = r.readVarInt()
624
+ const stream_count = r.readVarInt()
625
+ const reason_phrase = r.readString()
626
+ return {
627
+ type: 'subscribe_done',
628
+ request_id,
629
+ status_code,
630
+ stream_count,
631
+ reason_phrase,
632
+ }
633
+ }
634
+
635
+ function decodeUnsubscribePayload(r: BufferReader): Draft11Message {
636
+ const request_id = r.readVarInt()
637
+ return { type: 'unsubscribe', request_id }
638
+ }
639
+
640
+ function decodeAnnouncePayload(r: BufferReader): Draft11Message {
641
+ const request_id = r.readVarInt()
642
+ const track_namespace = r.readTuple()
643
+ const parameters = decodeParams(r)
644
+ return { type: 'announce', request_id, track_namespace, parameters }
645
+ }
646
+
647
+ function decodeAnnounceOkPayload(r: BufferReader): Draft11Message {
648
+ const request_id = r.readVarInt()
649
+ return { type: 'announce_ok', request_id }
650
+ }
651
+
652
+ function decodeAnnounceErrorPayload(r: BufferReader): Draft11Message {
653
+ const request_id = r.readVarInt()
654
+ const error_code = r.readVarInt()
655
+ const reason_phrase = r.readString()
656
+ return { type: 'announce_error', request_id, error_code, reason_phrase }
657
+ }
658
+
659
+ function decodeUnannouncePayload(r: BufferReader): Draft11Message {
660
+ const track_namespace = r.readTuple()
661
+ return { type: 'unannounce', track_namespace }
662
+ }
663
+
664
+ function decodeAnnounceCancelPayload(r: BufferReader): Draft11Message {
665
+ const track_namespace = r.readTuple()
666
+ const error_code = r.readVarInt()
667
+ const reason_phrase = r.readString()
668
+ return { type: 'announce_cancel', track_namespace, error_code, reason_phrase }
669
+ }
670
+
671
+ function decodeSubscribeAnnouncesPayload(r: BufferReader): Draft11Message {
672
+ const request_id = r.readVarInt()
673
+ const track_namespace_prefix = r.readTuple()
674
+ const parameters = decodeParams(r)
675
+ return {
676
+ type: 'subscribe_announces',
677
+ request_id,
678
+ track_namespace_prefix,
679
+ parameters,
680
+ }
681
+ }
682
+
683
+ function decodeSubscribeAnnouncesOkPayload(r: BufferReader): Draft11Message {
684
+ const request_id = r.readVarInt()
685
+ return { type: 'subscribe_announces_ok', request_id }
686
+ }
687
+
688
+ function decodeSubscribeAnnouncesErrorPayload(r: BufferReader): Draft11Message {
689
+ const request_id = r.readVarInt()
690
+ const error_code = r.readVarInt()
691
+ const reason_phrase = r.readString()
692
+ return {
693
+ type: 'subscribe_announces_error',
694
+ request_id,
695
+ error_code,
696
+ reason_phrase,
697
+ }
698
+ }
699
+
700
+ function decodeUnsubscribeAnnouncesPayload(r: BufferReader): Draft11Message {
701
+ const track_namespace_prefix = r.readTuple()
702
+ return { type: 'unsubscribe_announces', track_namespace_prefix }
703
+ }
704
+
705
+ function decodeFetchPayload(r: BufferReader): Draft11Message {
706
+ const request_id = r.readVarInt()
707
+ const subscriber_priority = r.readUint8()
708
+ const group_order = r.readUint8()
709
+ const fetch_type = r.readVarInt()
710
+ const ft = Number(fetch_type)
711
+
712
+ if (ft < 1 || ft > 3) {
713
+ throw new DecodeError('CONSTRAINT_VIOLATION', `Invalid fetch_type: ${ft}`, r.offset)
714
+ }
715
+
716
+ let standalone: StandaloneFetch | undefined
717
+ let joining: JoiningFetch | undefined
718
+
719
+ if (ft === 1) {
720
+ const track_namespace = r.readTuple()
721
+ const track_name = r.readString()
722
+ const start_group = r.readVarInt()
723
+ const start_object = r.readVarInt()
724
+ const end_group = r.readVarInt()
725
+ const end_object = r.readVarInt()
726
+ standalone = {
727
+ track_namespace,
728
+ track_name,
729
+ start_group,
730
+ start_object,
731
+ end_group,
732
+ end_object,
733
+ }
734
+ } else {
735
+ const joining_subscribe_id = r.readVarInt()
736
+ const joining_start = r.readVarInt()
737
+ joining = { joining_subscribe_id, joining_start }
738
+ }
739
+
740
+ const parameters = decodeParams(r)
741
+ return {
742
+ type: 'fetch',
743
+ request_id,
744
+ subscriber_priority,
745
+ group_order,
746
+ fetch_type,
747
+ standalone,
748
+ joining,
749
+ parameters,
750
+ } as Draft11Fetch
751
+ }
752
+
753
+ function decodeFetchOkPayload(r: BufferReader): Draft11Message {
754
+ const request_id = r.readVarInt()
755
+ const group_order = r.readUint8()
756
+ const end_of_track = r.readVarInt()
757
+ const group = r.readVarInt()
758
+ const object = r.readVarInt()
759
+ const parameters = decodeParams(r)
760
+ return {
761
+ type: 'fetch_ok',
762
+ request_id,
763
+ group_order,
764
+ end_of_track,
765
+ end_location: { group, object },
766
+ parameters,
767
+ }
768
+ }
769
+
770
+ function decodeFetchErrorPayload(r: BufferReader): Draft11Message {
771
+ const request_id = r.readVarInt()
772
+ const error_code = r.readVarInt()
773
+ const reason_phrase = r.readString()
774
+ return { type: 'fetch_error', request_id, error_code, reason_phrase }
775
+ }
776
+
777
+ function decodeFetchCancelPayload(r: BufferReader): Draft11Message {
778
+ const request_id = r.readVarInt()
779
+ return { type: 'fetch_cancel', request_id }
780
+ }
781
+
782
+ function decodeTrackStatusRequestPayload(r: BufferReader): Draft11Message {
783
+ const request_id = r.readVarInt()
784
+ const track_namespace = r.readTuple()
785
+ const track_name = r.readString()
786
+ const parameters = decodeParams(r)
787
+ return {
788
+ type: 'track_status_request',
789
+ request_id,
790
+ track_namespace,
791
+ track_name,
792
+ parameters,
793
+ }
794
+ }
795
+
796
+ function decodeTrackStatusPayload(r: BufferReader): Draft11Message {
797
+ const request_id = r.readVarInt()
798
+ const status_code = r.readVarInt()
799
+ const group = r.readVarInt()
800
+ const object = r.readVarInt()
801
+ const parameters = decodeParams(r)
802
+ return {
803
+ type: 'track_status',
804
+ request_id,
805
+ status_code,
806
+ largest_location: { group, object },
807
+ parameters,
808
+ }
809
+ }
810
+
811
+ function decodeGoAwayPayload(r: BufferReader): Draft11Message {
812
+ const new_session_uri = r.readString()
813
+ return { type: 'goaway', new_session_uri }
814
+ }
815
+
816
+ function decodeMaxRequestIdPayload(r: BufferReader): Draft11Message {
817
+ const request_id = r.readVarInt()
818
+ return { type: 'max_request_id', request_id }
819
+ }
820
+
821
+ function decodeRequestsBlockedPayload(r: BufferReader): Draft11Message {
822
+ const maximum_request_id = r.readVarInt()
823
+ return { type: 'requests_blocked', maximum_request_id }
824
+ }
825
+
826
+ // ─── Payload dispatch tables ───────────────────────────────────────────────────
827
+
828
+ const payloadDecoders: ReadonlyMap<bigint, (r: BufferReader) => Draft11Message> = new Map([
829
+ [MSG_CLIENT_SETUP, decodeClientSetupPayload],
830
+ [MSG_SERVER_SETUP, decodeServerSetupPayload],
831
+ [MSG_SUBSCRIBE, decodeSubscribePayload],
832
+ [MSG_SUBSCRIBE_OK, decodeSubscribeOkPayload],
833
+ [MSG_SUBSCRIBE_ERROR, decodeSubscribeErrorPayload],
834
+ [MSG_SUBSCRIBE_UPDATE, decodeSubscribeUpdatePayload],
835
+ [MSG_SUBSCRIBE_DONE, decodeSubscribeDonePayload],
836
+ [MSG_UNSUBSCRIBE, decodeUnsubscribePayload],
837
+ [MSG_ANNOUNCE, decodeAnnouncePayload],
838
+ [MSG_ANNOUNCE_OK, decodeAnnounceOkPayload],
839
+ [MSG_ANNOUNCE_ERROR, decodeAnnounceErrorPayload],
840
+ [MSG_UNANNOUNCE, decodeUnannouncePayload],
841
+ [MSG_ANNOUNCE_CANCEL, decodeAnnounceCancelPayload],
842
+ [MSG_SUBSCRIBE_ANNOUNCES, decodeSubscribeAnnouncesPayload],
843
+ [MSG_SUBSCRIBE_ANNOUNCES_OK, decodeSubscribeAnnouncesOkPayload],
844
+ [MSG_SUBSCRIBE_ANNOUNCES_ERROR, decodeSubscribeAnnouncesErrorPayload],
845
+ [MSG_UNSUBSCRIBE_ANNOUNCES, decodeUnsubscribeAnnouncesPayload],
846
+ [MSG_FETCH, decodeFetchPayload],
847
+ [MSG_FETCH_OK, decodeFetchOkPayload],
848
+ [MSG_FETCH_ERROR, decodeFetchErrorPayload],
849
+ [MSG_FETCH_CANCEL, decodeFetchCancelPayload],
850
+ [MSG_TRACK_STATUS_REQUEST, decodeTrackStatusRequestPayload],
851
+ [MSG_TRACK_STATUS, decodeTrackStatusPayload],
852
+ [MSG_GOAWAY, decodeGoAwayPayload],
853
+ [MSG_MAX_REQUEST_ID, decodeMaxRequestIdPayload],
854
+ [MSG_REQUESTS_BLOCKED, decodeRequestsBlockedPayload],
855
+ ])
856
+
857
+ // ─── Public API ────────────────────────────────────────────────────────────────
858
+
859
+ /**
860
+ * Encode a draft-11 control message with type(varint) + length(varint) + payload.
861
+ */
862
+ export function encodeMessage(message: Draft11Message): Uint8Array {
863
+ const typeId = MESSAGE_ID_MAP.get(message.type)
864
+ if (typeId === undefined) throw new Error(`Unknown message type: ${message.type}`)
865
+
866
+ const payloadWriter = new BufferWriter()
867
+ encodePayload(message, payloadWriter)
868
+ const payload = payloadWriter.finishView()
869
+
870
+ if (payload.byteLength > 0xffff) {
871
+ throw new Error(`Payload too large for 16-bit length: ${payload.byteLength}`)
872
+ }
873
+
874
+ // Write framed message: type(varint) + length(uint16 BE) + payload
875
+ const writer = new BufferWriter(payload.byteLength + 16)
876
+ writer.writeVarInt(typeId)
877
+ writer.writeUint8((payload.byteLength >> 8) & 0xff)
878
+ writer.writeUint8(payload.byteLength & 0xff)
879
+ writer.writeBytes(payload)
880
+ return writer.finish()
881
+ }
882
+
883
+ function encodePayload(msg: Draft11Message, w: BufferWriter): void {
884
+ switch (msg.type) {
885
+ case 'client_setup':
886
+ return encodeClientSetupPayload(msg, w)
887
+ case 'server_setup':
888
+ return encodeServerSetupPayload(msg, w)
889
+ case 'subscribe':
890
+ return encodeSubscribePayload(msg, w)
891
+ case 'subscribe_ok':
892
+ return encodeSubscribeOkPayload(msg, w)
893
+ case 'subscribe_error':
894
+ return encodeSubscribeErrorPayload(msg, w)
895
+ case 'subscribe_update':
896
+ return encodeSubscribeUpdatePayload(msg, w)
897
+ case 'subscribe_done':
898
+ return encodeSubscribeDonePayload(msg, w)
899
+ case 'unsubscribe':
900
+ return encodeUnsubscribePayload(msg, w)
901
+ case 'announce':
902
+ return encodeAnnouncePayload(msg, w)
903
+ case 'announce_ok':
904
+ return encodeAnnounceOkPayload(msg, w)
905
+ case 'announce_error':
906
+ return encodeAnnounceErrorPayload(msg, w)
907
+ case 'unannounce':
908
+ return encodeUnannouncePayload(msg, w)
909
+ case 'announce_cancel':
910
+ return encodeAnnounceCancelPayload(msg, w)
911
+ case 'subscribe_announces':
912
+ return encodeSubscribeAnnouncesPayload(msg, w)
913
+ case 'subscribe_announces_ok':
914
+ return encodeSubscribeAnnouncesOkPayload(msg, w)
915
+ case 'subscribe_announces_error':
916
+ return encodeSubscribeAnnouncesErrorPayload(msg, w)
917
+ case 'unsubscribe_announces':
918
+ return encodeUnsubscribeAnnouncesPayload(msg, w)
919
+ case 'fetch':
920
+ return encodeFetchPayload(msg, w)
921
+ case 'fetch_ok':
922
+ return encodeFetchOkPayload(msg, w)
923
+ case 'fetch_error':
924
+ return encodeFetchErrorPayload(msg, w)
925
+ case 'fetch_cancel':
926
+ return encodeFetchCancelPayload(msg, w)
927
+ case 'track_status_request':
928
+ return encodeTrackStatusRequestPayload(msg, w)
929
+ case 'track_status':
930
+ return encodeTrackStatusPayload(msg, w)
931
+ case 'goaway':
932
+ return encodeGoAwayPayload(msg, w)
933
+ case 'max_request_id':
934
+ return encodeMaxRequestIdPayload(msg, w)
935
+ case 'requests_blocked':
936
+ return encodeRequestsBlockedPayload(msg, w)
937
+ default: {
938
+ const _exhaustive: never = msg
939
+ throw new Error(`Unhandled message type: ${(_exhaustive as Draft11Message).type}`)
940
+ }
941
+ }
942
+ }
943
+
944
+ /**
945
+ * Decode a draft-11 control message from bytes (type + uint16 length + payload).
946
+ */
947
+ export function decodeMessage(bytes: Uint8Array): DecodeResult<Draft11Message> {
948
+ try {
949
+ const reader = new BufferReader(bytes)
950
+ const typeId = reader.readVarInt()
951
+
952
+ // Read 16-bit big-endian payload length
953
+ const lenHi = reader.readUint8()
954
+ const lenLo = reader.readUint8()
955
+ const payloadLength = (lenHi << 8) | lenLo
956
+
957
+ const payloadBytes = reader.readBytes(payloadLength)
958
+ const payloadReader = new BufferReader(payloadBytes)
959
+
960
+ const decoder = payloadDecoders.get(typeId)
961
+ if (!decoder) {
962
+ return {
963
+ ok: false,
964
+ error: new DecodeError(
965
+ 'UNKNOWN_MESSAGE_TYPE',
966
+ `Unknown message type ID: 0x${typeId.toString(16)}`,
967
+ 0,
968
+ ),
969
+ }
970
+ }
971
+
972
+ const message = decoder(payloadReader)
973
+ return { ok: true, value: message, bytesRead: reader.offset }
974
+ } catch (e) {
975
+ if (e instanceof DecodeError) return { ok: false, error: e }
976
+ throw e
977
+ }
978
+ }
979
+
980
+ // ─── Data Stream Encoding/Decoding ─────────────────────────────────────────────
981
+
982
+ export {
983
+ decodeDatagram,
984
+ decodeDataStream,
985
+ decodeFetchStream,
986
+ decodeSubgroupStream,
987
+ encodeDatagram,
988
+ encodeFetchStream,
989
+ encodeSubgroupStream,
990
+ } from './data-streams.js'
991
+
992
+ export function createStreamDecoder(): TransformStream<Uint8Array, Draft11Message> {
993
+ let buffer = new Uint8Array(0)
994
+ let offset = 0
995
+ return new TransformStream<Uint8Array, Draft11Message>({
996
+ transform(chunk, controller) {
997
+ if (offset > 0) {
998
+ buffer = buffer.subarray(offset)
999
+ offset = 0
1000
+ }
1001
+ const newBuffer = new Uint8Array(buffer.length + chunk.length)
1002
+ newBuffer.set(buffer, 0)
1003
+ newBuffer.set(chunk, buffer.length)
1004
+ buffer = newBuffer
1005
+ while (offset < buffer.length) {
1006
+ const result = decodeMessage(buffer.subarray(offset))
1007
+ if (!result.ok) {
1008
+ if (result.error.code === 'UNEXPECTED_END') break
1009
+ controller.error(result.error)
1010
+ return
1011
+ }
1012
+ controller.enqueue(result.value)
1013
+ offset += result.bytesRead
1014
+ }
1015
+ },
1016
+ flush(controller) {
1017
+ if (offset < buffer.length)
1018
+ controller.error(new DecodeError('UNEXPECTED_END', 'Stream ended with incomplete data', 0))
1019
+ },
1020
+ })
1021
+ }
1022
+
1023
+ // ─── Codec Factory ─────────────────────────────────────────────────────────────
1024
+
1025
+ export interface Draft11Codec extends BaseCodec<Draft11Message> {
1026
+ readonly draft: '11'
1027
+ encodeSubgroupStream(stream: SubgroupStream): Uint8Array
1028
+ encodeDatagram(dg: DatagramObject): Uint8Array
1029
+ encodeFetchStream(stream: FetchStream): Uint8Array
1030
+ decodeSubgroupStream(bytes: Uint8Array): DecodeResult<SubgroupStream>
1031
+ decodeDatagram(bytes: Uint8Array): DecodeResult<DatagramObject>
1032
+ decodeFetchStream(bytes: Uint8Array): DecodeResult<FetchStream>
1033
+ decodeDataStream(
1034
+ streamType: 'subgroup' | 'datagram' | 'fetch',
1035
+ bytes: Uint8Array,
1036
+ ): DecodeResult<Draft11DataStream>
1037
+ createStreamDecoder(): TransformStream<Uint8Array, Draft11Message>
1038
+ }
1039
+
1040
+ export function createDraft11Codec(): Draft11Codec {
1041
+ return {
1042
+ draft: '11',
1043
+ encodeMessage,
1044
+ decodeMessage,
1045
+ encodeSubgroupStream,
1046
+ encodeDatagram,
1047
+ encodeFetchStream,
1048
+ decodeSubgroupStream,
1049
+ decodeDatagram,
1050
+ decodeFetchStream,
1051
+ decodeDataStream,
1052
+ createStreamDecoder,
1053
+ }
1054
+ }