@moqtap/codec 0.9.0 → 0.11.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 (232) hide show
  1. package/CHANGELOG.md +173 -0
  2. package/dist/auth-redaction-DsdLq4Qs.d.cts +102 -0
  3. package/dist/auth-redaction-DsdLq4Qs.d.ts +102 -0
  4. package/dist/{chunk-PU2KV7VP.cjs → chunk-27K5BUAG.cjs} +485 -515
  5. package/dist/{chunk-BV6MITLO.js → chunk-3RNQMGKA.js} +365 -390
  6. package/dist/{chunk-2XH6PHXR.js → chunk-5AWIXOLB.js} +442 -467
  7. package/dist/{chunk-4XLYCT5Q.cjs → chunk-5IMHJIAZ.cjs} +52 -2
  8. package/dist/{chunk-TVSFDQYE.cjs → chunk-5JGZCKJO.cjs} +513 -531
  9. package/dist/{chunk-IADVBKEJ.cjs → chunk-73K4OSAN.cjs} +407 -432
  10. package/dist/{chunk-N2V7Z37J.js → chunk-7VQKRGRD.js} +19 -3
  11. package/dist/{chunk-UZO5Z2XT.cjs → chunk-A3UCQOIB.cjs} +490 -515
  12. package/dist/{chunk-JKMYAUGY.js → chunk-A7PDMVXM.js} +35 -11
  13. package/dist/{chunk-VZHL2U2N.cjs → chunk-A7VQO6YC.cjs} +54 -38
  14. package/dist/{chunk-KCH4BQXK.cjs → chunk-ACQHZLRZ.cjs} +73 -47
  15. package/dist/{chunk-H6DTAIWM.js → chunk-BBICOMW6.js} +47 -19
  16. package/dist/{chunk-MAXEHYVB.cjs → chunk-BNIUVOCJ.cjs} +554 -612
  17. package/dist/{chunk-QGIHC6BA.cjs → chunk-CNC6ANS4.cjs} +70 -46
  18. package/dist/{chunk-NH4L6NIY.js → chunk-FDLQSWHK.js} +437 -467
  19. package/dist/{chunk-IJAF6PAY.js → chunk-FFNOE4UR.js} +19 -3
  20. package/dist/{chunk-CZNW3R5E.cjs → chunk-GMENT63T.cjs} +54 -38
  21. package/dist/{chunk-7ENEWAMZ.cjs → chunk-HM7BLRVP.cjs} +305 -305
  22. package/dist/{chunk-XHAP2YST.js → chunk-HZONHFNO.js} +19 -3
  23. package/dist/{chunk-EVCPBFSR.cjs → chunk-ILYABWDK.cjs} +83 -55
  24. package/dist/{chunk-IDV2JYJ6.cjs → chunk-KRSFTZVO.cjs} +56 -40
  25. package/dist/{chunk-RIIA4ALQ.cjs → chunk-KSPH6KMH.cjs} +54 -38
  26. package/dist/{chunk-UA5XFP4O.cjs → chunk-KTY4LAUV.cjs} +485 -494
  27. package/dist/{chunk-RZTLQAKF.js → chunk-OJNLJT5D.js} +0 -1
  28. package/dist/{chunk-YJGOD3Y4.js → chunk-PJK2ZS3I.js} +492 -550
  29. package/dist/{chunk-TMXRYOBS.js → chunk-PSMZKHOC.js} +444 -453
  30. package/dist/{chunk-7RE6KSIY.cjs → chunk-Q5JQGHWY.cjs} +0 -1
  31. package/dist/{chunk-23KRITJ6.js → chunk-SQTUQY5E.js} +37 -11
  32. package/dist/{chunk-XQGEOFGE.js → chunk-WCRLAKDW.js} +253 -253
  33. package/dist/{chunk-KYVTM5VI.js → chunk-WL2YUEXH.js} +19 -3
  34. package/dist/{chunk-Q7NSUXXB.js → chunk-YCNIVZS7.js} +464 -482
  35. package/dist/{chunk-KLPWJU6W.js → chunk-YRZQVNZO.js} +50 -0
  36. package/dist/{codec-Bym-K2q2.d.cts → codec-76xzrLTL.d.cts} +21 -2
  37. package/dist/{codec-BKsVu3Fr.d.ts → codec-8p40WdLg.d.ts} +21 -2
  38. package/dist/{codec-Dl_QIXMz.d.ts → codec-BN2Bu0KI.d.ts} +20 -1
  39. package/dist/{codec-D4RvFQmu.d.ts → codec-Bb6GKTrv.d.ts} +21 -2
  40. package/dist/{codec-DAyiF5sq.d.cts → codec-BgpfHoAd.d.cts} +21 -2
  41. package/dist/{codec-CA6nbbEk.d.ts → codec-BkheTXO7.d.ts} +21 -2
  42. package/dist/{codec-DchsPfr3.d.cts → codec-BwVDkokh.d.cts} +21 -2
  43. package/dist/{codec-CgZdadPt.d.cts → codec-C5s1eBJX.d.cts} +21 -2
  44. package/dist/codec-CM5yd6n3.d.ts +38 -0
  45. package/dist/{codec-DJmqoIsp.d.cts → codec-Cb8HC3XV.d.cts} +21 -2
  46. package/dist/codec-Clu_skx8.d.cts +38 -0
  47. package/dist/{codec-DzkdW4kO.d.cts → codec-CpOpemR8.d.cts} +20 -1
  48. package/dist/{codec-CXCjya_G.d.cts → codec-Cshdl9Tt.d.cts} +20 -1
  49. package/dist/{codec-CSBFdPgP.d.cts → codec-DELe00w_.d.cts} +21 -2
  50. package/dist/{codec-C_xxsbKy.d.cts → codec-DHzKAUCa.d.cts} +21 -2
  51. package/dist/{codec--CVeqGQv.d.ts → codec-DdVBtvx7.d.ts} +20 -1
  52. package/dist/{codec-CviAa4lp.d.ts → codec-DlxGuYMv.d.ts} +21 -2
  53. package/dist/{codec-BNy5syCU.d.ts → codec-DxtgPxJS.d.ts} +20 -1
  54. package/dist/{codec-CtvFK4aV.d.cts → codec-GAY4lybi.d.cts} +21 -2
  55. package/dist/{codec-Cfo_z5ET.d.cts → codec-I8u2pfS4.d.cts} +20 -1
  56. package/dist/{codec-vddqy0KY.d.cts → codec-XLrnreeu.d.cts} +21 -2
  57. package/dist/{codec-O-1Js7h4.d.ts → codec-c0YEwwWn.d.ts} +21 -2
  58. package/dist/{codec-Cl73EfRW.d.ts → codec-ejDu-va6.d.ts} +21 -2
  59. package/dist/{codec-C006F75E.d.ts → codec-kaBUzdpV.d.ts} +21 -2
  60. package/dist/{codec-C5evFHL-.d.ts → codec-mVYqkIUF.d.ts} +21 -2
  61. package/dist/{codec-BMwgZIgG.d.ts → codec-qSbO5nOa.d.ts} +21 -2
  62. package/dist/{codec-BQa6Qsav.d.ts → codec-yYKGOipj.d.ts} +21 -2
  63. package/dist/{codec-DzLvG8Wb.d.cts → codec-zfzg9uiy.d.cts} +21 -2
  64. package/dist/draft07.cjs +48 -4
  65. package/dist/draft07.d.cts +42 -2
  66. package/dist/draft07.d.ts +42 -2
  67. package/dist/draft07.js +49 -5
  68. package/dist/draft08.cjs +76 -3
  69. package/dist/draft08.d.cts +70 -2
  70. package/dist/draft08.d.ts +70 -2
  71. package/dist/draft08.js +77 -4
  72. package/dist/draft09.cjs +76 -3
  73. package/dist/draft09.d.cts +70 -2
  74. package/dist/draft09.d.ts +70 -2
  75. package/dist/draft09.js +77 -4
  76. package/dist/draft10.cjs +76 -3
  77. package/dist/draft10.d.cts +70 -2
  78. package/dist/draft10.d.ts +70 -2
  79. package/dist/draft10.js +77 -4
  80. package/dist/draft11-session.d.cts +1 -1
  81. package/dist/draft11-session.d.ts +1 -1
  82. package/dist/draft11.cjs +104 -3
  83. package/dist/draft11.d.cts +100 -4
  84. package/dist/draft11.d.ts +100 -4
  85. package/dist/draft11.js +105 -4
  86. package/dist/draft12-session.d.cts +1 -1
  87. package/dist/draft12-session.d.ts +1 -1
  88. package/dist/draft12.cjs +113 -3
  89. package/dist/draft12.d.cts +109 -4
  90. package/dist/draft12.d.ts +109 -4
  91. package/dist/draft12.js +114 -4
  92. package/dist/draft13-session.d.cts +1 -1
  93. package/dist/draft13-session.d.ts +1 -1
  94. package/dist/draft13.cjs +105 -3
  95. package/dist/draft13.d.cts +101 -4
  96. package/dist/draft13.d.ts +101 -4
  97. package/dist/draft13.js +106 -4
  98. package/dist/draft14-session.d.cts +1 -1
  99. package/dist/draft14-session.d.ts +1 -1
  100. package/dist/draft14.cjs +107 -3
  101. package/dist/draft14.d.cts +103 -30
  102. package/dist/draft14.d.ts +103 -30
  103. package/dist/draft14.js +108 -4
  104. package/dist/draft15-session.d.cts +1 -1
  105. package/dist/draft15-session.d.ts +1 -1
  106. package/dist/draft15.cjs +69 -3
  107. package/dist/draft15.d.cts +65 -22
  108. package/dist/draft15.d.ts +65 -22
  109. package/dist/draft15.js +70 -4
  110. package/dist/draft16-session.cjs +2 -2
  111. package/dist/draft16-session.d.cts +1 -1
  112. package/dist/draft16-session.d.ts +1 -1
  113. package/dist/draft16-session.js +1 -1
  114. package/dist/draft16.cjs +76 -8
  115. package/dist/draft16.d.cts +67 -13
  116. package/dist/draft16.d.ts +67 -13
  117. package/dist/draft16.js +73 -5
  118. package/dist/draft17-session.d.cts +1 -1
  119. package/dist/draft17-session.d.ts +1 -1
  120. package/dist/draft17.cjs +77 -3
  121. package/dist/draft17.d.cts +73 -4
  122. package/dist/draft17.d.ts +73 -4
  123. package/dist/draft17.js +78 -4
  124. package/dist/draft18-session.d.cts +1 -1
  125. package/dist/draft18-session.d.ts +1 -1
  126. package/dist/draft18.cjs +8 -3
  127. package/dist/draft18.d.cts +4 -3
  128. package/dist/draft18.d.ts +4 -3
  129. package/dist/draft18.js +9 -4
  130. package/dist/draft19-session.d.cts +1 -1
  131. package/dist/draft19-session.d.ts +1 -1
  132. package/dist/draft19.cjs +11 -4
  133. package/dist/draft19.d.cts +4 -3
  134. package/dist/draft19.d.ts +4 -3
  135. package/dist/draft19.js +12 -5
  136. package/dist/draft20-session.d.cts +1 -1
  137. package/dist/draft20-session.d.ts +1 -1
  138. package/dist/draft20.cjs +8 -3
  139. package/dist/draft20.d.cts +4 -3
  140. package/dist/draft20.d.ts +4 -3
  141. package/dist/draft20.js +9 -4
  142. package/dist/index.cjs +30 -30
  143. package/dist/index.d.cts +25 -24
  144. package/dist/index.d.ts +25 -24
  145. package/dist/index.js +15 -15
  146. package/dist/session.cjs +2 -2
  147. package/dist/session.d.cts +10 -10
  148. package/dist/session.d.ts +10 -10
  149. package/dist/session.js +1 -1
  150. package/dist/{types-zHgCS6o5.d.cts → types-Btp416jt.d.cts} +15 -7
  151. package/dist/{types-zHgCS6o5.d.ts → types-Btp416jt.d.ts} +15 -7
  152. package/dist/{types-Cm9fjeYC.d.cts → types-BxTL1NXw.d.cts} +9 -2
  153. package/dist/{types-Cm9fjeYC.d.ts → types-BxTL1NXw.d.ts} +9 -2
  154. package/dist/{types-Bio-91Rt.d.cts → types-CI_SX4Xe.d.cts} +2 -2
  155. package/dist/{types-Bio-91Rt.d.ts → types-CI_SX4Xe.d.ts} +2 -2
  156. package/dist/{types-HI_bbXka.d.cts → types-CmqT0jUf.d.cts} +1 -1
  157. package/dist/{types-HI_bbXka.d.ts → types-CmqT0jUf.d.ts} +1 -1
  158. package/dist/{types-CPaYBsMG.d.cts → types-CuibHYcf.d.cts} +9 -2
  159. package/dist/{types-CPaYBsMG.d.ts → types-CuibHYcf.d.ts} +9 -2
  160. package/dist/{types-CWCQwuvy.d.cts → types-CwN7AEoL.d.cts} +24 -12
  161. package/dist/{types-CWCQwuvy.d.ts → types-CwN7AEoL.d.ts} +24 -12
  162. package/dist/{types-Ck45Ex5S.d.cts → types-D0kTgUuL.d.cts} +1 -1
  163. package/dist/{types-Ck45Ex5S.d.ts → types-D0kTgUuL.d.ts} +1 -1
  164. package/dist/{types-D9eiB_9A.d.cts → types-DAnalt5K.d.cts} +9 -2
  165. package/dist/{types-D9eiB_9A.d.ts → types-DAnalt5K.d.ts} +9 -2
  166. package/dist/{types-CXlSn0I9.d.cts → types-DjxexghA.d.cts} +2 -2
  167. package/dist/{types-CXlSn0I9.d.ts → types-DjxexghA.d.ts} +2 -2
  168. package/dist/{types-Cqp10JtB.d.cts → types-DxXNP32l.d.cts} +10 -2
  169. package/dist/{types-Cqp10JtB.d.ts → types-DxXNP32l.d.ts} +10 -2
  170. package/package.json +3 -3
  171. package/src/core/auth-redaction.ts +192 -0
  172. package/src/drafts/draft07/codec.ts +40 -0
  173. package/src/drafts/draft07/error-codes.ts +44 -0
  174. package/src/drafts/draft07/index.ts +15 -0
  175. package/src/drafts/draft08/codec.ts +40 -0
  176. package/src/drafts/draft08/error-codes.ts +76 -0
  177. package/src/drafts/draft08/index.ts +21 -0
  178. package/src/drafts/draft09/codec.ts +40 -0
  179. package/src/drafts/draft09/error-codes.ts +76 -0
  180. package/src/drafts/draft09/index.ts +21 -0
  181. package/src/drafts/draft10/codec.ts +40 -0
  182. package/src/drafts/draft10/error-codes.ts +76 -0
  183. package/src/drafts/draft10/index.ts +21 -0
  184. package/src/drafts/draft11/codec.ts +71 -8
  185. package/src/drafts/draft11/error-codes.ts +106 -0
  186. package/src/drafts/draft11/index.ts +23 -0
  187. package/src/drafts/draft11/types.ts +1 -1
  188. package/src/drafts/draft12/codec.ts +83 -16
  189. package/src/drafts/draft12/error-codes.ts +116 -0
  190. package/src/drafts/draft12/index.ts +25 -0
  191. package/src/drafts/draft12/types.ts +2 -2
  192. package/src/drafts/draft13/codec.ts +75 -11
  193. package/src/drafts/draft13/error-codes.ts +107 -0
  194. package/src/drafts/draft13/index.ts +23 -0
  195. package/src/drafts/draft13/types.ts +3 -3
  196. package/src/drafts/draft14/codec.ts +79 -12
  197. package/src/drafts/draft14/data-streams.ts +331 -259
  198. package/src/drafts/draft14/error-codes.ts +109 -0
  199. package/src/drafts/draft14/index.ts +23 -0
  200. package/src/drafts/draft14/types.ts +1 -1
  201. package/src/drafts/draft15/codec.ts +71 -8
  202. package/src/drafts/draft15/data-streams.ts +489 -444
  203. package/src/drafts/draft15/error-codes.ts +66 -0
  204. package/src/drafts/draft15/index.ts +15 -0
  205. package/src/drafts/draft15/types.ts +9 -2
  206. package/src/drafts/draft16/codec.ts +149 -49
  207. package/src/drafts/draft16/data-streams.ts +513 -442
  208. package/src/drafts/draft16/error-codes.ts +68 -0
  209. package/src/drafts/draft16/index.ts +15 -0
  210. package/src/drafts/draft16/messages.ts +4 -4
  211. package/src/drafts/draft16/session-fsm.ts +0 -1
  212. package/src/drafts/draft16/types.ts +24 -10
  213. package/src/drafts/draft17/codec.ts +77 -10
  214. package/src/drafts/draft17/data-streams.ts +567 -488
  215. package/src/drafts/draft17/index.ts +15 -0
  216. package/src/drafts/draft17/types.ts +9 -2
  217. package/src/drafts/draft18/codec.ts +70 -7
  218. package/src/drafts/draft18/data-streams.ts +570 -482
  219. package/src/drafts/draft18/index.ts +3 -0
  220. package/src/drafts/draft18/types.ts +18 -10
  221. package/src/drafts/draft19/codec.ts +95 -23
  222. package/src/drafts/draft19/data-streams.ts +570 -482
  223. package/src/drafts/draft19/error-codes.ts +3 -1
  224. package/src/drafts/draft19/index.ts +3 -0
  225. package/src/drafts/draft19/messages.ts +3 -1
  226. package/src/drafts/draft19/types.ts +32 -18
  227. package/src/drafts/draft20/codec.ts +115 -36
  228. package/src/drafts/draft20/data-streams.ts +610 -563
  229. package/src/drafts/draft20/index.ts +3 -0
  230. package/src/drafts/draft20/types.ts +31 -13
  231. package/dist/codec-BJ7BHuiZ.d.ts +0 -18
  232. package/dist/codec-C6Mx4bwz.d.cts +0 -18
@@ -3,8 +3,10 @@ import {
3
3
  Draft17BufferReader,
4
4
  Draft17BufferWriter,
5
5
  bytesToHex,
6
- hexToBytes
7
- } from "./chunk-KLPWJU6W.js";
6
+ hexToBytes,
7
+ recordAuthValueSpan,
8
+ redactWith
9
+ } from "./chunk-YRZQVNZO.js";
8
10
 
9
11
  // src/drafts/draft17/messages.ts
10
12
  var MSG_REQUEST_UPDATE = 0x02n;
@@ -230,96 +232,104 @@ function encodeFetchStream(stream) {
230
232
  }
231
233
  return w.finish();
232
234
  }
235
+ function readSubgroupHeader(r) {
236
+ const streamType = Number(r.readVarInt());
237
+ if (!(streamType >= 16 && streamType <= 29 || streamType >= 48 && streamType <= 61) || (streamType & 6) === 6) {
238
+ throw new DecodeError(
239
+ "CONSTRAINT_VIOLATION",
240
+ `Expected subgroup stream type 0x10-0x15/0x18-0x1D/0x30-0x35/0x38-0x3D, got 0x${streamType.toString(16)}`,
241
+ 0
242
+ );
243
+ }
244
+ const propertiesPresent = (streamType & 1) !== 0;
245
+ const hasSubgroupField = (streamType & 4) !== 0;
246
+ const subgroupIsFirstObjId = (streamType & 2) !== 0 && !hasSubgroupField;
247
+ const hasPriority = streamType < 48;
248
+ const endOfGroup = (streamType & 8) !== 0;
249
+ const trackAlias = r.readVarInt();
250
+ const groupId = r.readVarInt();
251
+ let subgroupId = 0n;
252
+ if (hasSubgroupField) {
253
+ subgroupId = r.readVarInt();
254
+ }
255
+ let publisherPriority = 128;
256
+ if (hasPriority) {
257
+ publisherPriority = r.readUint8();
258
+ }
259
+ return {
260
+ streamType,
261
+ propertiesPresent,
262
+ subgroupIsFirstObjId,
263
+ endOfGroup,
264
+ trackAlias,
265
+ groupId,
266
+ subgroupId,
267
+ publisherPriority
268
+ };
269
+ }
270
+ function readSubgroupObject(r, propertiesPresent, isFirst, prevObjectId, base) {
271
+ const byteOffset = base + r.offset;
272
+ const delta = r.readVarInt();
273
+ const objectId = isFirst ? delta : prevObjectId + 1n + delta;
274
+ const extensionData = new Uint8Array(0);
275
+ let objectProperties;
276
+ if (propertiesPresent) {
277
+ const propsLen = Number(r.readVarInt());
278
+ if (propsLen > 0) {
279
+ objectProperties = decodeObjectProperties(r, propsLen);
280
+ }
281
+ }
282
+ const payloadLength = Number(r.readVarInt());
283
+ let payload;
284
+ let status;
285
+ let payloadByteOffset;
286
+ if (payloadLength === 0) {
287
+ status = r.readVarInt();
288
+ payloadByteOffset = base + r.offset;
289
+ payload = new Uint8Array(0);
290
+ } else {
291
+ payloadByteOffset = base + r.offset;
292
+ payload = r.readBytesView(payloadLength);
293
+ }
294
+ const obj = {
295
+ type: "object",
296
+ byteOffset,
297
+ payloadByteOffset,
298
+ objectId,
299
+ payloadLength,
300
+ payload,
301
+ extensionData
302
+ };
303
+ if (status !== void 0) obj.status = status;
304
+ if (objectProperties !== void 0)
305
+ obj.objectProperties = objectProperties;
306
+ return obj;
307
+ }
233
308
  function decodeSubgroupStream(bytes) {
234
309
  try {
235
310
  const r = new Draft17BufferReader(bytes);
236
- const streamType = Number(r.readVarInt());
237
- if (!(streamType >= 16 && streamType <= 29 || streamType >= 48 && streamType <= 61) || (streamType & 6) === 6) {
238
- return {
239
- ok: false,
240
- error: new DecodeError(
241
- "CONSTRAINT_VIOLATION",
242
- `Expected subgroup stream type 0x10-0x15/0x18-0x1D/0x30-0x35/0x38-0x3D, got 0x${streamType.toString(16)}`,
243
- 0
244
- )
245
- };
246
- }
247
- const propertiesPresent = (streamType & 1) !== 0;
248
- const hasSubgroupField = (streamType & 4) !== 0;
249
- const subgroupIsFirstObjId = (streamType & 2) !== 0 && !hasSubgroupField;
250
- const hasPriority = streamType < 48;
251
- const endOfGroup = (streamType & 8) !== 0;
252
- const trackAlias = r.readVarInt();
253
- const groupId = r.readVarInt();
254
- let subgroupId = 0n;
255
- if (hasSubgroupField) {
256
- subgroupId = r.readVarInt();
257
- }
258
- let publisherPriority = 128;
259
- if (hasPriority) {
260
- publisherPriority = r.readUint8();
261
- }
311
+ const h = readSubgroupHeader(r);
262
312
  const objects = [];
263
313
  let prevObjectId = -1n;
264
- let firstObject = true;
314
+ let isFirst = true;
315
+ let subgroupId = h.subgroupId;
265
316
  while (r.remaining > 0) {
266
- const byteOffset = r.offset;
267
- const delta = r.readVarInt();
268
- let objectId;
269
- if (firstObject) {
270
- objectId = delta;
271
- if (subgroupIsFirstObjId && firstObject) {
272
- subgroupId = objectId;
273
- }
274
- firstObject = false;
275
- } else {
276
- objectId = prevObjectId + 1n + delta;
277
- }
278
- const extensionData = new Uint8Array(0);
279
- let objectProperties;
280
- if (propertiesPresent) {
281
- const propsLen = Number(r.readVarInt());
282
- if (propsLen > 0) {
283
- objectProperties = decodeObjectProperties(r, propsLen);
284
- }
285
- }
286
- const payloadLength = Number(r.readVarInt());
287
- let payload;
288
- let status;
289
- let payloadByteOffset;
290
- if (payloadLength === 0) {
291
- status = r.readVarInt();
292
- payloadByteOffset = r.offset;
293
- payload = new Uint8Array(0);
294
- } else {
295
- payloadByteOffset = r.offset;
296
- payload = r.readBytesView(payloadLength);
297
- }
298
- const obj = {
299
- type: "object",
300
- byteOffset,
301
- payloadByteOffset,
302
- objectId,
303
- payloadLength,
304
- payload,
305
- extensionData
306
- };
307
- if (status !== void 0) obj.status = status;
308
- if (objectProperties !== void 0)
309
- obj.objectProperties = objectProperties;
317
+ const obj = readSubgroupObject(r, h.propertiesPresent, isFirst, prevObjectId, 0);
318
+ if (isFirst && h.subgroupIsFirstObjId) subgroupId = obj.objectId;
319
+ isFirst = false;
310
320
  objects.push(obj);
311
- prevObjectId = objectId;
321
+ prevObjectId = obj.objectId;
312
322
  }
313
323
  const result = {
314
324
  type: "subgroup",
315
- headerType: streamType,
316
- trackAlias,
317
- groupId,
325
+ headerType: h.streamType,
326
+ trackAlias: h.trackAlias,
327
+ groupId: h.groupId,
318
328
  subgroupId,
319
- publisherPriority,
320
- objects
329
+ publisherPriority: h.publisherPriority,
330
+ objects,
331
+ ...h.endOfGroup ? { endOfGroup: true } : {}
321
332
  };
322
- if (endOfGroup) result.endOfGroup = true;
323
333
  return {
324
334
  ok: true,
325
335
  value: result,
@@ -386,6 +396,176 @@ function decodeDatagram(bytes) {
386
396
  throw e;
387
397
  }
388
398
  }
399
+ function newFetchObjectState() {
400
+ return { prevGroupId: 0n, prevSubgroupId: 0n, prevObjectId: 0n, prevPriority: 128, first: true };
401
+ }
402
+ function readFetchObject(r, base, st) {
403
+ const byteOffset = base + r.offset;
404
+ const flags = Number(r.readVarInt());
405
+ let groupId = st.prevGroupId;
406
+ let subgroupId = st.prevSubgroupId;
407
+ let objectId = st.prevObjectId + 1n;
408
+ let payloadLength;
409
+ let payload;
410
+ let payloadByteOffset;
411
+ const extensionData = new Uint8Array(0);
412
+ if (flags >= 128) {
413
+ if (flags !== 140 && flags !== 268) {
414
+ throw new DecodeError(
415
+ "CONSTRAINT_VIOLATION",
416
+ `Unknown serialization flags value: 0x${flags.toString(16)}`,
417
+ r.offset
418
+ );
419
+ }
420
+ groupId = r.readVarInt();
421
+ objectId = r.readVarInt();
422
+ payloadLength = Number(r.readVarInt());
423
+ payloadByteOffset = base + r.offset;
424
+ payload = payloadLength > 0 ? r.readBytesView(payloadLength) : new Uint8Array(0);
425
+ } else if (flags & 64) {
426
+ const objectIdPresent = (flags & 4) !== 0;
427
+ const groupIdPresent = (flags & 8) !== 0;
428
+ const priorityPresent = (flags & 16) !== 0;
429
+ const propsPresent = (flags & 32) !== 0;
430
+ if (groupIdPresent) {
431
+ groupId = r.readVarInt();
432
+ }
433
+ if (objectIdPresent) {
434
+ objectId = r.readVarInt();
435
+ }
436
+ if (priorityPresent) {
437
+ st.prevPriority = r.readUint8();
438
+ }
439
+ let objectProperties;
440
+ if (propsPresent) {
441
+ const propsLen = Number(r.readVarInt());
442
+ if (propsLen > 0) {
443
+ objectProperties = decodeObjectProperties(r, propsLen);
444
+ }
445
+ }
446
+ payloadLength = Number(r.readVarInt());
447
+ payloadByteOffset = base + r.offset;
448
+ payload = payloadLength > 0 ? r.readBytesView(payloadLength) : new Uint8Array(0);
449
+ const obj2 = {
450
+ type: "object",
451
+ byteOffset,
452
+ payloadByteOffset,
453
+ serializationFlags: flags,
454
+ groupId,
455
+ subgroupId: 0n,
456
+ objectId,
457
+ publisherPriority: st.prevPriority,
458
+ payloadLength,
459
+ payload,
460
+ extensionData
461
+ };
462
+ if (objectProperties)
463
+ obj2.objectProperties = objectProperties;
464
+ st.prevGroupId = groupId;
465
+ st.prevObjectId = objectId;
466
+ st.first = false;
467
+ return obj2;
468
+ } else {
469
+ const subgroupEncoding = flags & 3;
470
+ const objectIdPresent = (flags & 4) !== 0;
471
+ const groupIdPresent = (flags & 8) !== 0;
472
+ const priorityPresent = (flags & 16) !== 0;
473
+ const propsPresent = (flags & 32) !== 0;
474
+ if (groupIdPresent) {
475
+ groupId = r.readVarInt();
476
+ } else if (st.first) {
477
+ throw new DecodeError(
478
+ "CONSTRAINT_VIOLATION",
479
+ "First fetch object must include groupId",
480
+ r.offset
481
+ );
482
+ }
483
+ if (subgroupEncoding === 0) {
484
+ subgroupId = 0n;
485
+ } else if (subgroupEncoding === 1) {
486
+ if (st.first) {
487
+ throw new DecodeError(
488
+ "CONSTRAINT_VIOLATION",
489
+ "First fetch object cannot reference prior subgroupId",
490
+ r.offset
491
+ );
492
+ }
493
+ subgroupId = st.prevSubgroupId;
494
+ } else if (subgroupEncoding === 2) {
495
+ if (st.first) {
496
+ throw new DecodeError(
497
+ "CONSTRAINT_VIOLATION",
498
+ "First fetch object cannot reference prior subgroupId",
499
+ r.offset
500
+ );
501
+ }
502
+ subgroupId = st.prevSubgroupId + 1n;
503
+ } else if (subgroupEncoding === 3) {
504
+ subgroupId = r.readVarInt();
505
+ }
506
+ if (objectIdPresent) {
507
+ objectId = r.readVarInt();
508
+ } else if (st.first) {
509
+ throw new DecodeError(
510
+ "CONSTRAINT_VIOLATION",
511
+ "First fetch object must include objectId",
512
+ r.offset
513
+ );
514
+ }
515
+ if (priorityPresent) {
516
+ st.prevPriority = r.readUint8();
517
+ }
518
+ let objectProperties;
519
+ if (propsPresent) {
520
+ const propsLen = Number(r.readVarInt());
521
+ if (propsLen > 0) {
522
+ objectProperties = decodeObjectProperties(r, propsLen);
523
+ }
524
+ }
525
+ payloadLength = Number(r.readVarInt());
526
+ payloadByteOffset = base + r.offset;
527
+ payload = payloadLength > 0 ? r.readBytesView(payloadLength) : new Uint8Array(0);
528
+ if (objectProperties) {
529
+ const obj2 = {
530
+ type: "object",
531
+ byteOffset,
532
+ payloadByteOffset,
533
+ serializationFlags: flags,
534
+ groupId,
535
+ subgroupId,
536
+ objectId,
537
+ publisherPriority: st.prevPriority,
538
+ payloadLength,
539
+ payload,
540
+ extensionData
541
+ };
542
+ obj2.objectProperties = objectProperties;
543
+ st.prevGroupId = groupId;
544
+ st.prevSubgroupId = subgroupId;
545
+ st.prevObjectId = objectId;
546
+ st.first = false;
547
+ return obj2;
548
+ }
549
+ }
550
+ const obj = {
551
+ type: "object",
552
+ byteOffset,
553
+ payloadByteOffset,
554
+ serializationFlags: flags,
555
+ groupId,
556
+ subgroupId,
557
+ objectId,
558
+ publisherPriority: st.prevPriority,
559
+ payloadLength,
560
+ payload,
561
+ extensionData
562
+ };
563
+ st.prevGroupId = groupId;
564
+ st.prevSubgroupId = subgroupId;
565
+ st.prevObjectId = objectId;
566
+ st.first = false;
567
+ return obj;
568
+ }
389
569
  function decodeFetchStream(bytes) {
390
570
  try {
391
571
  const r = new Draft17BufferReader(bytes);
@@ -402,194 +582,9 @@ function decodeFetchStream(bytes) {
402
582
  }
403
583
  const requestId = r.readVarInt();
404
584
  const objects = [];
405
- let prevGroupId = 0n;
406
- let prevSubgroupId = 0n;
407
- let prevObjectId = 0n;
408
- let prevPriority = 128;
409
- let first = true;
585
+ const st = newFetchObjectState();
410
586
  while (r.remaining > 0) {
411
- const byteOffset = r.offset;
412
- const flags = Number(r.readVarInt());
413
- let groupId = prevGroupId;
414
- let subgroupId = prevSubgroupId;
415
- let objectId = prevObjectId + 1n;
416
- let payloadLength;
417
- let payload;
418
- let payloadByteOffset;
419
- const extensionData = new Uint8Array(0);
420
- if (flags >= 128) {
421
- if (flags !== 140 && flags !== 268) {
422
- return {
423
- ok: false,
424
- error: new DecodeError(
425
- "CONSTRAINT_VIOLATION",
426
- `Unknown serialization flags value: 0x${flags.toString(16)}`,
427
- r.offset
428
- )
429
- };
430
- }
431
- groupId = r.readVarInt();
432
- objectId = r.readVarInt();
433
- payloadLength = Number(r.readVarInt());
434
- payloadByteOffset = r.offset;
435
- payload = payloadLength > 0 ? r.readBytesView(payloadLength) : new Uint8Array(0);
436
- } else if (flags & 64) {
437
- const objectIdPresent = (flags & 4) !== 0;
438
- const groupIdPresent = (flags & 8) !== 0;
439
- const priorityPresent = (flags & 16) !== 0;
440
- const propsPresent = (flags & 32) !== 0;
441
- if (groupIdPresent) {
442
- groupId = r.readVarInt();
443
- }
444
- if (objectIdPresent) {
445
- objectId = r.readVarInt();
446
- }
447
- if (priorityPresent) {
448
- prevPriority = r.readUint8();
449
- }
450
- let objectProperties;
451
- if (propsPresent) {
452
- const propsLen = Number(r.readVarInt());
453
- if (propsLen > 0) {
454
- objectProperties = decodeObjectProperties(r, propsLen);
455
- }
456
- }
457
- payloadLength = Number(r.readVarInt());
458
- payloadByteOffset = r.offset;
459
- payload = payloadLength > 0 ? r.readBytesView(payloadLength) : new Uint8Array(0);
460
- const obj2 = {
461
- type: "object",
462
- byteOffset,
463
- payloadByteOffset,
464
- serializationFlags: flags,
465
- groupId,
466
- subgroupId: 0n,
467
- objectId,
468
- publisherPriority: prevPriority,
469
- payloadLength,
470
- payload,
471
- extensionData
472
- };
473
- if (objectProperties)
474
- obj2.objectProperties = objectProperties;
475
- objects.push(obj2);
476
- prevGroupId = groupId;
477
- prevObjectId = objectId;
478
- first = false;
479
- continue;
480
- } else {
481
- const subgroupEncoding = flags & 3;
482
- const objectIdPresent = (flags & 4) !== 0;
483
- const groupIdPresent = (flags & 8) !== 0;
484
- const priorityPresent = (flags & 16) !== 0;
485
- const propsPresent = (flags & 32) !== 0;
486
- if (groupIdPresent) {
487
- groupId = r.readVarInt();
488
- } else if (first) {
489
- return {
490
- ok: false,
491
- error: new DecodeError(
492
- "CONSTRAINT_VIOLATION",
493
- "First fetch object must include groupId",
494
- r.offset
495
- )
496
- };
497
- }
498
- if (subgroupEncoding === 0) {
499
- subgroupId = 0n;
500
- } else if (subgroupEncoding === 1) {
501
- if (first) {
502
- return {
503
- ok: false,
504
- error: new DecodeError(
505
- "CONSTRAINT_VIOLATION",
506
- "First fetch object cannot reference prior subgroupId",
507
- r.offset
508
- )
509
- };
510
- }
511
- subgroupId = prevSubgroupId;
512
- } else if (subgroupEncoding === 2) {
513
- if (first) {
514
- return {
515
- ok: false,
516
- error: new DecodeError(
517
- "CONSTRAINT_VIOLATION",
518
- "First fetch object cannot reference prior subgroupId",
519
- r.offset
520
- )
521
- };
522
- }
523
- subgroupId = prevSubgroupId + 1n;
524
- } else if (subgroupEncoding === 3) {
525
- subgroupId = r.readVarInt();
526
- }
527
- if (objectIdPresent) {
528
- objectId = r.readVarInt();
529
- } else if (first) {
530
- return {
531
- ok: false,
532
- error: new DecodeError(
533
- "CONSTRAINT_VIOLATION",
534
- "First fetch object must include objectId",
535
- r.offset
536
- )
537
- };
538
- }
539
- if (priorityPresent) {
540
- prevPriority = r.readUint8();
541
- }
542
- let objectProperties;
543
- if (propsPresent) {
544
- const propsLen = Number(r.readVarInt());
545
- if (propsLen > 0) {
546
- objectProperties = decodeObjectProperties(r, propsLen);
547
- }
548
- }
549
- payloadLength = Number(r.readVarInt());
550
- payloadByteOffset = r.offset;
551
- payload = payloadLength > 0 ? r.readBytesView(payloadLength) : new Uint8Array(0);
552
- if (objectProperties) {
553
- const obj2 = {
554
- type: "object",
555
- byteOffset,
556
- payloadByteOffset,
557
- serializationFlags: flags,
558
- groupId,
559
- subgroupId,
560
- objectId,
561
- publisherPriority: prevPriority,
562
- payloadLength,
563
- payload,
564
- extensionData
565
- };
566
- obj2.objectProperties = objectProperties;
567
- objects.push(obj2);
568
- prevGroupId = groupId;
569
- prevSubgroupId = subgroupId;
570
- prevObjectId = objectId;
571
- first = false;
572
- continue;
573
- }
574
- }
575
- const obj = {
576
- type: "object",
577
- byteOffset,
578
- payloadByteOffset,
579
- serializationFlags: flags,
580
- groupId,
581
- subgroupId,
582
- objectId,
583
- publisherPriority: prevPriority,
584
- payloadLength,
585
- payload,
586
- extensionData
587
- };
588
- objects.push(obj);
589
- prevGroupId = groupId;
590
- prevSubgroupId = subgroupId;
591
- prevObjectId = objectId;
592
- first = false;
587
+ objects.push(readFetchObject(r, 0, st));
593
588
  }
594
589
  return {
595
590
  ok: true,
@@ -615,61 +610,69 @@ function decodeDataStream(streamType, bytes) {
615
610
  }
616
611
  }
617
612
  }
613
+ var StreamBuffer = class {
614
+ buf = new Uint8Array(0);
615
+ /** Bytes written into `buf`. */
616
+ len = 0;
617
+ /** Consumed prefix of `buf`; `base + offset` is the position in the stream. */
618
+ offset = 0;
619
+ /** Absolute position of `buf[0]` within the stream. */
620
+ base = 0;
621
+ get unread() {
622
+ return this.len - this.offset;
623
+ }
624
+ append(chunk) {
625
+ if (this.len + chunk.length > this.buf.length) {
626
+ const live = this.len - this.offset;
627
+ const needed = live + chunk.length;
628
+ let cap = Math.max(4096, this.buf.length);
629
+ while (cap < needed * 2) cap *= 2;
630
+ const next = new Uint8Array(cap);
631
+ next.set(this.buf.subarray(this.offset, this.len), 0);
632
+ this.buf = next;
633
+ this.base += this.offset;
634
+ this.len = live;
635
+ this.offset = 0;
636
+ }
637
+ this.buf.set(chunk, this.len);
638
+ this.len += chunk.length;
639
+ }
640
+ /**
641
+ * The written bytes, for a reader positioned at `offset`. Bounded by `len`
642
+ * rather than by capacity, so `BufferReader.remaining` counts real bytes and
643
+ * not the spare room after them.
644
+ */
645
+ written() {
646
+ return this.buf.subarray(0, this.len);
647
+ }
648
+ };
618
649
  function createSubgroupStreamDecoder() {
619
- let buffer = new Uint8Array(0);
620
- let offset = 0;
650
+ const b = new StreamBuffer();
651
+ let header = null;
621
652
  let headerEmitted = false;
622
653
  let prevObjectId = -1n;
623
- let firstObject = true;
624
- let _subgroupIsFirstObjId = false;
625
- let _extensionsPresent = false;
654
+ let isFirst = true;
655
+ function emitHeader(controller, h, subgroupId) {
656
+ controller.enqueue({
657
+ type: "subgroup_header",
658
+ headerType: h.streamType,
659
+ trackAlias: h.trackAlias,
660
+ groupId: h.groupId,
661
+ subgroupId,
662
+ publisherPriority: h.publisherPriority,
663
+ ...h.endOfGroup ? { endOfGroup: true } : {}
664
+ });
665
+ headerEmitted = true;
666
+ }
626
667
  return new TransformStream({
627
668
  transform(chunk, controller) {
628
- if (offset > 0) {
629
- buffer = buffer.subarray(offset);
630
- offset = 0;
631
- }
632
- const newBuffer = new Uint8Array(buffer.length + chunk.length);
633
- newBuffer.set(buffer, 0);
634
- newBuffer.set(chunk, buffer.length);
635
- buffer = newBuffer;
636
- if (!headerEmitted) {
669
+ b.append(chunk);
670
+ const view = b.written();
671
+ if (header === null) {
637
672
  try {
638
- const r = new Draft17BufferReader(buffer.subarray(offset));
639
- const streamType = Number(r.readVarInt());
640
- if (!(streamType >= 16 && streamType <= 29 || streamType >= 48 && streamType <= 61)) {
641
- controller.error(
642
- new DecodeError(
643
- "CONSTRAINT_VIOLATION",
644
- `Expected subgroup stream type, got 0x${streamType.toString(16)}`,
645
- 0
646
- )
647
- );
648
- return;
649
- }
650
- _extensionsPresent = (streamType & 1) !== 0;
651
- const hasSubgroupField = (streamType & 4) !== 0;
652
- _subgroupIsFirstObjId = (streamType & 2) !== 0 && !hasSubgroupField;
653
- const hasPriority = streamType < 48;
654
- const trackAlias = r.readVarInt();
655
- const groupId = r.readVarInt();
656
- let subgroupId = 0n;
657
- if (hasSubgroupField) {
658
- subgroupId = r.readVarInt();
659
- }
660
- let publisherPriority = 128;
661
- if (hasPriority) {
662
- publisherPriority = r.readUint8();
663
- }
664
- controller.enqueue({
665
- type: "subgroup_header",
666
- trackAlias,
667
- groupId,
668
- subgroupId,
669
- publisherPriority
670
- });
671
- headerEmitted = true;
672
- offset += r.offset;
673
+ const hr = new Draft17BufferReader(view, b.offset);
674
+ header = readSubgroupHeader(hr);
675
+ b.offset = hr.offset;
673
676
  } catch (e) {
674
677
  if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
675
678
  return;
@@ -677,71 +680,51 @@ function createSubgroupStreamDecoder() {
677
680
  controller.error(e);
678
681
  return;
679
682
  }
683
+ if (!header.subgroupIsFirstObjId) emitHeader(controller, header, header.subgroupId);
680
684
  }
681
- while (offset < buffer.length) {
685
+ const h = header;
686
+ if (h === null) return;
687
+ const r = new Draft17BufferReader(view, b.offset);
688
+ while (r.remaining > 0) {
689
+ const start = r.offset;
690
+ let obj;
682
691
  try {
683
- const r = new Draft17BufferReader(buffer.subarray(offset));
684
- const delta = r.readVarInt();
685
- let objectId;
686
- if (firstObject) {
687
- objectId = delta;
688
- firstObject = false;
689
- } else {
690
- objectId = prevObjectId + 1n + delta;
691
- }
692
- let extensionData = new Uint8Array(0);
693
- if (_extensionsPresent) {
694
- const extLen = Number(r.readVarInt());
695
- extensionData = extLen > 0 ? r.readBytesView(extLen) : new Uint8Array(0);
696
- }
697
- const payloadLength = Number(r.readVarInt());
698
- const payloadByteOffset = r.offset;
699
- const payload = payloadLength > 0 ? r.readBytesView(payloadLength) : new Uint8Array(0);
700
- controller.enqueue({
701
- type: "object",
702
- objectId,
703
- payloadLength,
704
- payload,
705
- extensionData,
706
- byteOffset: 0,
707
- payloadByteOffset
708
- });
709
- offset += r.offset;
710
- prevObjectId = objectId;
692
+ obj = readSubgroupObject(r, h.propertiesPresent, isFirst, prevObjectId, b.base);
711
693
  } catch (e) {
712
694
  if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
713
- break;
695
+ b.offset = start;
696
+ return;
714
697
  }
715
698
  controller.error(e);
716
699
  return;
717
700
  }
701
+ if (!headerEmitted) emitHeader(controller, h, obj.objectId);
702
+ isFirst = false;
703
+ b.offset = r.offset;
704
+ prevObjectId = obj.objectId;
705
+ controller.enqueue(obj);
718
706
  }
719
707
  },
720
708
  flush(controller) {
721
- if (offset < buffer.length) {
709
+ if (header !== null && !headerEmitted) emitHeader(controller, header, 0n);
710
+ if (b.unread > 0) {
722
711
  controller.error(new DecodeError("UNEXPECTED_END", "Stream ended with incomplete data", 0));
723
712
  }
724
713
  }
725
714
  });
726
715
  }
727
716
  function createFetchStreamDecoder() {
728
- let buffer = new Uint8Array(0);
729
- let offset = 0;
717
+ const b = new StreamBuffer();
730
718
  let headerEmitted = false;
719
+ const st = newFetchObjectState();
731
720
  return new TransformStream({
732
721
  transform(chunk, controller) {
733
- if (offset > 0) {
734
- buffer = buffer.subarray(offset);
735
- offset = 0;
736
- }
737
- const newBuffer = new Uint8Array(buffer.length + chunk.length);
738
- newBuffer.set(buffer, 0);
739
- newBuffer.set(chunk, buffer.length);
740
- buffer = newBuffer;
722
+ b.append(chunk);
723
+ const view = b.written();
741
724
  if (!headerEmitted) {
742
725
  try {
743
- const r = new Draft17BufferReader(buffer.subarray(offset));
744
- const streamType = r.readVarInt();
726
+ const r2 = new Draft17BufferReader(view, b.offset);
727
+ const streamType = r2.readVarInt();
745
728
  if (streamType !== FETCH_STREAM_TYPE) {
746
729
  controller.error(
747
730
  new DecodeError(
@@ -752,10 +735,10 @@ function createFetchStreamDecoder() {
752
735
  );
753
736
  return;
754
737
  }
755
- const requestId = r.readVarInt();
738
+ const requestId = r2.readVarInt();
756
739
  controller.enqueue({ type: "fetch_header", requestId });
757
740
  headerEmitted = true;
758
- offset += r.offset;
741
+ b.offset = r2.offset;
759
742
  } catch (e) {
760
743
  if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
761
744
  return;
@@ -764,99 +747,82 @@ function createFetchStreamDecoder() {
764
747
  return;
765
748
  }
766
749
  }
767
- while (offset < buffer.length) {
750
+ const r = new Draft17BufferReader(view, b.offset);
751
+ while (r.remaining > 0) {
752
+ const start = r.offset;
753
+ let obj;
768
754
  try {
769
- const r = new Draft17BufferReader(buffer.subarray(offset));
770
- const flags = Number(r.readVarInt());
771
- const objectIdPresent = (flags & 4) !== 0;
772
- const groupIdPresent = (flags & 8) !== 0;
773
- const priorityPresent = (flags & 16) !== 0;
774
- const extensionsPresent = (flags & 32) !== 0;
775
- const subgroupEncoding = flags & 3;
776
- if (groupIdPresent) r.readVarInt();
777
- if (subgroupEncoding === 3) r.readVarInt();
778
- let objectId = 0n;
779
- if (objectIdPresent) objectId = r.readVarInt();
780
- if (priorityPresent) r.readUint8();
781
- let extensionData = new Uint8Array(0);
782
- if (extensionsPresent) {
783
- const extLen = Number(r.readVarInt());
784
- extensionData = extLen > 0 ? r.readBytesView(extLen) : new Uint8Array(0);
785
- }
786
- const payloadLength = Number(r.readVarInt());
787
- const payloadByteOffset = r.offset;
788
- const payload = payloadLength > 0 ? r.readBytesView(payloadLength) : new Uint8Array(0);
789
- controller.enqueue({
790
- type: "object",
791
- objectId,
792
- payloadLength,
793
- payload,
794
- extensionData,
795
- byteOffset: 0,
796
- payloadByteOffset
797
- });
798
- offset += r.offset;
755
+ obj = readFetchObject(r, b.base, st);
799
756
  } catch (e) {
800
757
  if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
801
- break;
758
+ b.offset = start;
759
+ return;
802
760
  }
803
761
  controller.error(e);
804
762
  return;
805
763
  }
764
+ b.offset = r.offset;
765
+ controller.enqueue(obj);
806
766
  }
807
767
  },
808
768
  flush(controller) {
809
- if (offset < buffer.length) {
769
+ if (b.unread > 0) {
810
770
  controller.error(new DecodeError("UNEXPECTED_END", "Stream ended with incomplete data", 0));
811
771
  }
812
772
  }
813
773
  });
814
774
  }
815
775
  function createDataStreamDecoder() {
816
- let buffer = new Uint8Array(0);
817
- let offset = 0;
818
- let inner = null;
819
- return new TransformStream({
820
- transform(chunk, controller) {
821
- if (offset > 0) {
822
- buffer = buffer.subarray(offset);
823
- offset = 0;
824
- }
825
- const newBuffer = new Uint8Array(buffer.length + chunk.length);
826
- newBuffer.set(buffer, 0);
827
- newBuffer.set(chunk, buffer.length);
828
- buffer = newBuffer;
829
- if (inner === null) {
830
- if (offset >= buffer.length) return;
831
- const firstByte = buffer[offset];
832
- if (firstByte >= 16 && firstByte <= 29 || firstByte >= 48 && firstByte <= 61) {
833
- const decoder = createSubgroupStreamDecoder();
834
- inner = decoder;
835
- } else if (firstByte === 5) {
836
- const decoder = createFetchStreamDecoder();
837
- inner = decoder;
838
- } else {
839
- controller.error(
840
- new DecodeError(
841
- "CONSTRAINT_VIOLATION",
842
- `Unknown data stream type: 0x${firstByte.toString(16)}`,
843
- 0
844
- )
845
- );
846
- return;
776
+ let writer = null;
777
+ let pump = Promise.resolve();
778
+ let broken = false;
779
+ function attach(inner, controller) {
780
+ writer = inner.writable.getWriter();
781
+ const reader = inner.readable.getReader();
782
+ pump = (async () => {
783
+ try {
784
+ for (; ; ) {
785
+ const { done, value } = await reader.read();
786
+ if (done) break;
787
+ controller.enqueue(value);
788
+ }
789
+ } catch (e) {
790
+ if (!broken) {
791
+ broken = true;
792
+ controller.error(e);
847
793
  }
848
794
  }
849
- },
850
- flush(controller) {
851
- if (offset >= buffer.length) return;
852
- const view = buffer.subarray(offset);
853
- const firstByte = view[0];
854
- let result;
795
+ })();
796
+ }
797
+ async function feed(bytes) {
798
+ if (writer === null || broken || bytes.length === 0) return;
799
+ try {
800
+ await writer.write(bytes);
801
+ } catch {
802
+ broken = true;
803
+ }
804
+ }
805
+ return new TransformStream({
806
+ async transform(chunk, controller) {
807
+ if (broken) return;
808
+ if (writer !== null) {
809
+ await feed(chunk);
810
+ return;
811
+ }
812
+ if (chunk.length === 0) return;
813
+ const firstByte = chunk[0];
855
814
  if (firstByte >= 16 && firstByte <= 29 || firstByte >= 48 && firstByte <= 61) {
856
- result = decodeSubgroupStream(view);
815
+ attach(
816
+ createSubgroupStreamDecoder(),
817
+ controller
818
+ );
857
819
  } else if (firstByte === 5) {
858
- result = decodeFetchStream(view);
820
+ attach(
821
+ createFetchStreamDecoder(),
822
+ controller
823
+ );
859
824
  } else {
825
+ broken = true;
860
826
  controller.error(
861
827
  new DecodeError(
862
828
  "CONSTRAINT_VIOLATION",
@@ -866,31 +832,15 @@ function createDataStreamDecoder() {
866
832
  );
867
833
  return;
868
834
  }
869
- if (!result.ok) {
870
- controller.error(result.error);
871
- return;
872
- }
873
- const stream = result.value;
874
- if (stream.type === "subgroup") {
875
- controller.enqueue({
876
- type: "subgroup_header",
877
- trackAlias: stream.trackAlias,
878
- groupId: stream.groupId,
879
- subgroupId: stream.subgroupId,
880
- publisherPriority: stream.publisherPriority
881
- });
882
- for (const obj of stream.objects) {
883
- controller.enqueue(obj);
884
- }
885
- } else if (stream.type === "fetch") {
886
- controller.enqueue({
887
- type: "fetch_header",
888
- requestId: stream.requestId
889
- });
890
- for (const obj of stream.objects) {
891
- controller.enqueue(obj);
892
- }
835
+ await feed(chunk);
836
+ },
837
+ async flush() {
838
+ if (writer === null) return;
839
+ try {
840
+ await writer.close();
841
+ } catch {
893
842
  }
843
+ await pump;
894
844
  }
895
845
  });
896
846
  }
@@ -910,12 +860,12 @@ function encodeSetupOptions(opts, writer) {
910
860
  }
911
861
  });
912
862
  }
913
- if (opts.authorization_token !== void 0) {
863
+ for (const token of opts.authorization_token ?? []) {
914
864
  entries.push({
915
865
  type: SETUP_OPT_AUTHORIZATION_TOKEN,
916
866
  encode: (w) => {
917
867
  const tmpW = new Draft17BufferWriter(64);
918
- encodeAuthorizationToken(opts.authorization_token, tmpW);
868
+ encodeAuthorizationToken(token, tmpW);
919
869
  const raw = tmpW.finish();
920
870
  w.writeVarInt(BigInt(raw.byteLength));
921
871
  w.writeBytes(raw);
@@ -1003,7 +953,9 @@ function decodeSetupOptions(reader, payloadEnd) {
1003
953
  if (optType === SETUP_OPT_PATH) {
1004
954
  result.path = textDecoder.decode(bytes);
1005
955
  } else if (optType === SETUP_OPT_AUTHORIZATION_TOKEN) {
1006
- result.authorization_token = decodeAuthorizationToken(new Draft17BufferReader(bytes));
956
+ const token = decodeAuthorizationToken(new Draft17BufferReader(bytes));
957
+ noteAuthTokenValue(reader.offset - length, length, token.token_value);
958
+ result.authorization_token = [...result.authorization_token ?? [], token];
1007
959
  } else if (optType === SETUP_OPT_AUTHORITY) {
1008
960
  result.authority = textDecoder.decode(bytes);
1009
961
  } else if (optType === SETUP_OPT_MOQT_IMPLEMENTATION) {
@@ -1029,7 +981,6 @@ function encodeAuthorizationToken(token, w) {
1029
981
  if (aliasType === 1 || aliasType === 3) {
1030
982
  w.writeVarInt(token.token_type ?? 0n);
1031
983
  const tv = token.token_value ?? new Uint8Array(0);
1032
- w.writeVarInt(BigInt(tv.byteLength));
1033
984
  w.writeBytes(tv);
1034
985
  }
1035
986
  }
@@ -1044,8 +995,7 @@ function decodeAuthorizationToken(r) {
1044
995
  if (aliasType === 1 || aliasType === 3) {
1045
996
  ;
1046
997
  result.token_type = r.readVarInt();
1047
- const len = Number(r.readVarInt());
1048
- const bytes = r.readBytes(len);
998
+ const bytes = r.readBytes(r.remaining);
1049
999
  result.token_value = bytes;
1050
1000
  }
1051
1001
  return result;
@@ -1091,12 +1041,12 @@ function encodeParams(params, writer) {
1091
1041
  encode: (w) => w.writeVarInt(params.delivery_timeout)
1092
1042
  });
1093
1043
  }
1094
- if (params.authorization_token !== void 0) {
1044
+ for (const token of params.authorization_token ?? []) {
1095
1045
  entries.push({
1096
1046
  type: PARAM_AUTHORIZATION_TOKEN,
1097
1047
  encode: (w) => {
1098
1048
  const tmpW = new Draft17BufferWriter(64);
1099
- encodeAuthorizationToken(params.authorization_token, tmpW);
1049
+ encodeAuthorizationToken(token, tmpW);
1100
1050
  const raw = tmpW.finish();
1101
1051
  w.writeVarInt(BigInt(raw.byteLength));
1102
1052
  w.writeBytes(raw);
@@ -1210,7 +1160,9 @@ function decodeParams(reader, messageType) {
1210
1160
  } else if (paramType === PARAM_AUTHORIZATION_TOKEN) {
1211
1161
  const length = Number(reader.readVarInt());
1212
1162
  const bytes = reader.readBytes(length);
1213
- result.authorization_token = decodeAuthorizationToken(new Draft17BufferReader(bytes));
1163
+ const token = decodeAuthorizationToken(new Draft17BufferReader(bytes));
1164
+ noteAuthTokenValue(reader.offset - length, length, token.token_value);
1165
+ result.authorization_token = [...result.authorization_token ?? [], token];
1214
1166
  } else if (paramType === PARAM_RENDEZVOUS_TIMEOUT) {
1215
1167
  result.rendezvous_timeout = reader.readVarInt();
1216
1168
  } else if (paramType === PARAM_EXPIRES) {
@@ -1851,6 +1803,23 @@ function createDraft17Codec() {
1851
1803
  createDataStreamDecoder
1852
1804
  };
1853
1805
  }
1806
+ function noteAuthTokenValue(valueStart, valueLength, value) {
1807
+ if (value === void 0 || value.byteLength === 0) return;
1808
+ recordAuthValueSpan(valueStart + valueLength - value.byteLength, value.byteLength);
1809
+ }
1810
+ function redactAuthTokens(bytes) {
1811
+ let payloadStart;
1812
+ try {
1813
+ const probe = new Draft17BufferReader(bytes);
1814
+ probe.readVarInt();
1815
+ probe.readUint8();
1816
+ probe.readUint8();
1817
+ payloadStart = probe.offset;
1818
+ } catch {
1819
+ return { bytes, redacted: 0, incomplete: false, decoded: false };
1820
+ }
1821
+ return redactWith(bytes, payloadStart, () => decodeMessage(bytes).ok);
1822
+ }
1854
1823
 
1855
1824
  export {
1856
1825
  MSG_REQUEST_UPDATE,
@@ -1890,5 +1859,6 @@ export {
1890
1859
  encodeMessage,
1891
1860
  decodeMessage,
1892
1861
  createStreamDecoder,
1893
- createDraft17Codec
1862
+ createDraft17Codec,
1863
+ redactAuthTokens
1894
1864
  };