@moqtap/codec 0.10.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 (213) hide show
  1. package/CHANGELOG.md +147 -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-RYKQXBG6.cjs → chunk-27K5BUAG.cjs} +481 -517
  5. package/dist/{chunk-WCPENYNE.js → chunk-3RNQMGKA.js} +359 -390
  6. package/dist/{chunk-TSUZ6GJV.js → chunk-5AWIXOLB.js} +438 -469
  7. package/dist/{chunk-4XLYCT5Q.cjs → chunk-5IMHJIAZ.cjs} +52 -2
  8. package/dist/{chunk-KTNERCTZ.cjs → chunk-5JGZCKJO.cjs} +488 -518
  9. package/dist/{chunk-WCTMPDUX.cjs → chunk-73K4OSAN.cjs} +401 -432
  10. package/dist/{chunk-N2V7Z37J.js → chunk-7VQKRGRD.js} +19 -3
  11. package/dist/{chunk-BIBMDTU5.cjs → chunk-A3UCQOIB.cjs} +486 -517
  12. package/dist/{chunk-K2B3OVFK.js → chunk-A7PDMVXM.js} +24 -3
  13. package/dist/{chunk-VZHL2U2N.cjs → chunk-A7VQO6YC.cjs} +54 -38
  14. package/dist/{chunk-AQ445VV7.cjs → chunk-ACQHZLRZ.cjs} +66 -46
  15. package/dist/{chunk-L5DRACXS.js → chunk-BBICOMW6.js} +25 -3
  16. package/dist/{chunk-T4VWPKDG.cjs → chunk-BNIUVOCJ.cjs} +529 -599
  17. package/dist/{chunk-UKKDOOUO.cjs → chunk-CNC6ANS4.cjs} +58 -37
  18. package/dist/{chunk-OZRK73NQ.js → chunk-FDLQSWHK.js} +433 -469
  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-XCRCAUGI.cjs → chunk-HM7BLRVP.cjs} +291 -297
  22. package/dist/{chunk-XHAP2YST.js → chunk-HZONHFNO.js} +19 -3
  23. package/dist/{chunk-SDJTSMOW.cjs → chunk-ILYABWDK.cjs} +60 -38
  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-OUNN4IQB.cjs → chunk-KTY4LAUV.cjs} +482 -497
  27. package/dist/{chunk-RZTLQAKF.js → chunk-OJNLJT5D.js} +0 -1
  28. package/dist/{chunk-FT3HDNIB.js → chunk-PJK2ZS3I.js} +467 -537
  29. package/dist/{chunk-G53WYUXL.js → chunk-PSMZKHOC.js} +441 -456
  30. package/dist/{chunk-7RE6KSIY.cjs → chunk-Q5JQGHWY.cjs} +0 -1
  31. package/dist/{chunk-BX2N7TZM.js → chunk-SQTUQY5E.js} +31 -11
  32. package/dist/{chunk-CF5DDD3Q.js → chunk-WCRLAKDW.js} +239 -245
  33. package/dist/{chunk-KYVTM5VI.js → chunk-WL2YUEXH.js} +19 -3
  34. package/dist/{chunk-KGDVC4O6.js → chunk-YCNIVZS7.js} +439 -469
  35. package/dist/{chunk-KLPWJU6W.js → chunk-YRZQVNZO.js} +50 -0
  36. package/dist/{codec-CFWu5KVT.d.cts → codec-76xzrLTL.d.cts} +21 -2
  37. package/dist/{codec-gl4_QEva.d.ts → codec-8p40WdLg.d.ts} +20 -1
  38. package/dist/{codec-Dl_QIXMz.d.ts → codec-BN2Bu0KI.d.ts} +20 -1
  39. package/dist/{codec-Bux_Q40X.d.ts → codec-Bb6GKTrv.d.ts} +21 -2
  40. package/dist/{codec-EvybrBuY.d.cts → codec-BgpfHoAd.d.cts} +20 -1
  41. package/dist/{codec-alVA27Uc.d.ts → codec-BkheTXO7.d.ts} +20 -1
  42. package/dist/{codec-Czejcmxr.d.cts → codec-BwVDkokh.d.cts} +21 -2
  43. package/dist/{codec-BrTi4K7r.d.cts → codec-C5s1eBJX.d.cts} +21 -2
  44. package/dist/codec-CM5yd6n3.d.ts +38 -0
  45. package/dist/{codec-CWnf8mn_.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-f7vxeVIS.d.cts → codec-DELe00w_.d.cts} +21 -2
  50. package/dist/{codec-DNmiG2e0.d.cts → codec-DHzKAUCa.d.cts} +20 -1
  51. package/dist/{codec--CVeqGQv.d.ts → codec-DdVBtvx7.d.ts} +20 -1
  52. package/dist/{codec-CBdOIfMY.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-guzqtfMk.d.cts → codec-GAY4lybi.d.cts} +20 -1
  55. package/dist/{codec-Cfo_z5ET.d.cts → codec-I8u2pfS4.d.cts} +20 -1
  56. package/dist/{codec-BxQTybUX.d.cts → codec-XLrnreeu.d.cts} +21 -2
  57. package/dist/{codec-DOmAmFkI.d.ts → codec-c0YEwwWn.d.ts} +20 -1
  58. package/dist/{codec-DaXjRrOV.d.ts → codec-ejDu-va6.d.ts} +21 -2
  59. package/dist/{codec-C3mdsGx3.d.ts → codec-kaBUzdpV.d.ts} +21 -2
  60. package/dist/{codec-CwZ6OCPP.d.ts → codec-mVYqkIUF.d.ts} +21 -2
  61. package/dist/{codec-08ZOr5wI.d.ts → codec-qSbO5nOa.d.ts} +21 -2
  62. package/dist/{codec-Cm3D261p.d.ts → codec-yYKGOipj.d.ts} +20 -1
  63. package/dist/{codec-Bd60SGKH.d.cts → codec-zfzg9uiy.d.cts} +20 -1
  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.cjs +104 -3
  81. package/dist/draft11.d.cts +98 -2
  82. package/dist/draft11.d.ts +98 -2
  83. package/dist/draft11.js +105 -4
  84. package/dist/draft12.cjs +113 -3
  85. package/dist/draft12.d.cts +107 -2
  86. package/dist/draft12.d.ts +107 -2
  87. package/dist/draft12.js +114 -4
  88. package/dist/draft13.cjs +105 -3
  89. package/dist/draft13.d.cts +99 -2
  90. package/dist/draft13.d.ts +99 -2
  91. package/dist/draft13.js +106 -4
  92. package/dist/draft14.cjs +107 -3
  93. package/dist/draft14.d.cts +101 -28
  94. package/dist/draft14.d.ts +101 -28
  95. package/dist/draft14.js +108 -4
  96. package/dist/draft15-session.d.cts +1 -1
  97. package/dist/draft15-session.d.ts +1 -1
  98. package/dist/draft15.cjs +69 -3
  99. package/dist/draft15.d.cts +65 -22
  100. package/dist/draft15.d.ts +65 -22
  101. package/dist/draft15.js +70 -4
  102. package/dist/draft16-session.cjs +2 -2
  103. package/dist/draft16-session.d.cts +1 -1
  104. package/dist/draft16-session.d.ts +1 -1
  105. package/dist/draft16-session.js +1 -1
  106. package/dist/draft16.cjs +76 -8
  107. package/dist/draft16.d.cts +67 -13
  108. package/dist/draft16.d.ts +67 -13
  109. package/dist/draft16.js +73 -5
  110. package/dist/draft17-session.d.cts +1 -1
  111. package/dist/draft17-session.d.ts +1 -1
  112. package/dist/draft17.cjs +77 -3
  113. package/dist/draft17.d.cts +73 -4
  114. package/dist/draft17.d.ts +73 -4
  115. package/dist/draft17.js +78 -4
  116. package/dist/draft18-session.d.cts +1 -1
  117. package/dist/draft18-session.d.ts +1 -1
  118. package/dist/draft18.cjs +8 -3
  119. package/dist/draft18.d.cts +4 -3
  120. package/dist/draft18.d.ts +4 -3
  121. package/dist/draft18.js +9 -4
  122. package/dist/draft19-session.d.cts +1 -1
  123. package/dist/draft19-session.d.ts +1 -1
  124. package/dist/draft19.cjs +11 -4
  125. package/dist/draft19.d.cts +4 -3
  126. package/dist/draft19.d.ts +4 -3
  127. package/dist/draft19.js +12 -5
  128. package/dist/draft20-session.d.cts +1 -1
  129. package/dist/draft20-session.d.ts +1 -1
  130. package/dist/draft20.cjs +8 -3
  131. package/dist/draft20.d.cts +4 -3
  132. package/dist/draft20.d.ts +4 -3
  133. package/dist/draft20.js +9 -4
  134. package/dist/index.cjs +30 -30
  135. package/dist/index.d.cts +21 -20
  136. package/dist/index.d.ts +21 -20
  137. package/dist/index.js +15 -15
  138. package/dist/session.cjs +2 -2
  139. package/dist/session.d.cts +6 -6
  140. package/dist/session.d.ts +6 -6
  141. package/dist/session.js +1 -1
  142. package/dist/{types-C1rxh-id.d.cts → types-Btp416jt.d.cts} +8 -0
  143. package/dist/{types-C1rxh-id.d.ts → types-Btp416jt.d.ts} +8 -0
  144. package/dist/{types-CZTzbIzQ.d.cts → types-BxTL1NXw.d.cts} +7 -0
  145. package/dist/{types-CZTzbIzQ.d.ts → types-BxTL1NXw.d.ts} +7 -0
  146. package/dist/{types-pShrs3fN.d.cts → types-CuibHYcf.d.cts} +7 -0
  147. package/dist/{types-pShrs3fN.d.ts → types-CuibHYcf.d.ts} +7 -0
  148. package/dist/{types-CY-QWxd-.d.cts → types-CwN7AEoL.d.cts} +13 -1
  149. package/dist/{types-CY-QWxd-.d.ts → types-CwN7AEoL.d.ts} +13 -1
  150. package/dist/{types-D4bmW8rY.d.cts → types-DAnalt5K.d.cts} +7 -0
  151. package/dist/{types-D4bmW8rY.d.ts → types-DAnalt5K.d.ts} +7 -0
  152. package/dist/{types-Bg86GGVZ.d.cts → types-DxXNP32l.d.cts} +8 -0
  153. package/dist/{types-Bg86GGVZ.d.ts → types-DxXNP32l.d.ts} +8 -0
  154. package/package.json +3 -3
  155. package/src/core/auth-redaction.ts +192 -0
  156. package/src/drafts/draft07/codec.ts +40 -0
  157. package/src/drafts/draft07/error-codes.ts +44 -0
  158. package/src/drafts/draft07/index.ts +15 -0
  159. package/src/drafts/draft08/codec.ts +40 -0
  160. package/src/drafts/draft08/error-codes.ts +76 -0
  161. package/src/drafts/draft08/index.ts +21 -0
  162. package/src/drafts/draft09/codec.ts +40 -0
  163. package/src/drafts/draft09/error-codes.ts +76 -0
  164. package/src/drafts/draft09/index.ts +21 -0
  165. package/src/drafts/draft10/codec.ts +40 -0
  166. package/src/drafts/draft10/error-codes.ts +76 -0
  167. package/src/drafts/draft10/index.ts +21 -0
  168. package/src/drafts/draft11/codec.ts +60 -0
  169. package/src/drafts/draft11/error-codes.ts +106 -0
  170. package/src/drafts/draft11/index.ts +23 -0
  171. package/src/drafts/draft12/codec.ts +61 -0
  172. package/src/drafts/draft12/error-codes.ts +116 -0
  173. package/src/drafts/draft12/index.ts +25 -0
  174. package/src/drafts/draft13/codec.ts +69 -11
  175. package/src/drafts/draft13/error-codes.ts +107 -0
  176. package/src/drafts/draft13/index.ts +23 -0
  177. package/src/drafts/draft13/types.ts +1 -1
  178. package/src/drafts/draft14/codec.ts +65 -4
  179. package/src/drafts/draft14/data-streams.ts +331 -259
  180. package/src/drafts/draft14/error-codes.ts +109 -0
  181. package/src/drafts/draft14/index.ts +23 -0
  182. package/src/drafts/draft15/codec.ts +65 -8
  183. package/src/drafts/draft15/data-streams.ts +489 -444
  184. package/src/drafts/draft15/error-codes.ts +66 -0
  185. package/src/drafts/draft15/index.ts +15 -0
  186. package/src/drafts/draft15/types.ts +7 -0
  187. package/src/drafts/draft16/codec.ts +146 -52
  188. package/src/drafts/draft16/data-streams.ts +513 -442
  189. package/src/drafts/draft16/error-codes.ts +68 -0
  190. package/src/drafts/draft16/index.ts +15 -0
  191. package/src/drafts/draft16/messages.ts +4 -4
  192. package/src/drafts/draft16/session-fsm.ts +0 -1
  193. package/src/drafts/draft16/types.ts +22 -8
  194. package/src/drafts/draft17/codec.ts +73 -12
  195. package/src/drafts/draft17/data-streams.ts +567 -488
  196. package/src/drafts/draft17/index.ts +15 -0
  197. package/src/drafts/draft17/types.ts +7 -0
  198. package/src/drafts/draft18/codec.ts +66 -9
  199. package/src/drafts/draft18/data-streams.ts +570 -482
  200. package/src/drafts/draft18/index.ts +3 -0
  201. package/src/drafts/draft18/types.ts +16 -8
  202. package/src/drafts/draft19/codec.ts +70 -10
  203. package/src/drafts/draft19/data-streams.ts +570 -482
  204. package/src/drafts/draft19/error-codes.ts +3 -1
  205. package/src/drafts/draft19/index.ts +3 -0
  206. package/src/drafts/draft19/messages.ts +3 -1
  207. package/src/drafts/draft19/types.ts +22 -11
  208. package/src/drafts/draft20/codec.ts +90 -23
  209. package/src/drafts/draft20/data-streams.ts +610 -563
  210. package/src/drafts/draft20/index.ts +3 -0
  211. package/src/drafts/draft20/types.ts +14 -2
  212. package/dist/codec-BJ7BHuiZ.d.ts +0 -18
  213. package/dist/codec-C6Mx4bwz.d.cts +0 -18
@@ -368,7 +368,6 @@ var Draft16SessionFSM = (_class = class {
368
368
  });
369
369
  return { ok: true, phase: this._phase, sideEffects };
370
370
  }
371
- // Renamed from handleSubscribeUpdate in draft-15
372
371
  handleRequestUpdate(message, sideEffects) {
373
372
  const err = this.requireReady(message.type);
374
373
  if (err) return { ok: false, violation: err };
@@ -3,8 +3,10 @@ import {
3
3
  BufferWriter,
4
4
  DecodeError,
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/draft13/data-streams.ts
10
12
  var FETCH_STREAM_TYPE = 0x05n;
@@ -477,10 +479,10 @@ function decodeSetupParams(r) {
477
479
  const bytes = r.readBytes(length);
478
480
  result.path = textDecoder.decode(bytes);
479
481
  } else if (paramType === SETUP_PARAM_AUTHORIZATION_TOKEN) {
480
- result.authorization_token = [
481
- ...result.authorization_token ?? [],
482
- decodeAuthorizationToken(r, length)
483
- ];
482
+ const valueStart = r.offset;
483
+ const token = decodeAuthorizationToken(r, length);
484
+ noteAuthTokenValue(valueStart, length, token.token_value);
485
+ result.authorization_token = [...result.authorization_token ?? [], token];
484
486
  } else {
485
487
  const bytes = r.readBytes(length);
486
488
  unknown.push({
@@ -553,10 +555,10 @@ function decodeParams(r) {
553
555
  } else {
554
556
  const length = Number(r.readVarInt());
555
557
  if (paramType === PARAM_AUTHORIZATION_TOKEN) {
556
- result.authorization_token = [
557
- ...result.authorization_token ?? [],
558
- decodeAuthorizationToken(r, length)
559
- ];
558
+ const valueStart = r.offset;
559
+ const token = decodeAuthorizationToken(r, length);
560
+ noteAuthTokenValue(valueStart, length, token.token_value);
561
+ result.authorization_token = [...result.authorization_token ?? [], token];
560
562
  } else {
561
563
  const bytes = r.readBytes(length);
562
564
  unknown.push({
@@ -1366,6 +1368,23 @@ function createDraft13Codec() {
1366
1368
  createStreamDecoder
1367
1369
  };
1368
1370
  }
1371
+ function noteAuthTokenValue(valueStart, valueLength, value) {
1372
+ if (value === void 0 || value.byteLength === 0) return;
1373
+ recordAuthValueSpan(valueStart + valueLength - value.byteLength, value.byteLength);
1374
+ }
1375
+ function redactAuthTokens(bytes) {
1376
+ let payloadStart;
1377
+ try {
1378
+ const probe = new BufferReader(bytes);
1379
+ probe.readVarInt();
1380
+ probe.readUint8();
1381
+ probe.readUint8();
1382
+ payloadStart = probe.offset;
1383
+ } catch {
1384
+ return { bytes, redacted: 0, incomplete: false, decoded: false };
1385
+ }
1386
+ return redactWith(bytes, payloadStart, () => decodeMessage(bytes).ok);
1387
+ }
1369
1388
 
1370
1389
  export {
1371
1390
  encodeSubgroupStream,
@@ -1415,5 +1434,6 @@ export {
1415
1434
  encodeMessage,
1416
1435
  decodeMessage,
1417
1436
  createStreamDecoder,
1418
- createDraft13Codec
1437
+ createDraft13Codec,
1438
+ redactAuthTokens
1419
1439
  };
@@ -3,8 +3,10 @@ import {
3
3
  BufferWriter,
4
4
  DecodeError,
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/draft14/messages.ts
10
12
  var MSG_SUBSCRIBE_UPDATE = 0x02n;
@@ -177,87 +179,95 @@ function encodeFetchStream(stream) {
177
179
  }
178
180
  return w.finish();
179
181
  }
182
+ function readSubgroupHeader(r) {
183
+ const streamType = Number(r.readVarInt());
184
+ if (!isValidSubgroupType(streamType)) {
185
+ throw new DecodeError(
186
+ "CONSTRAINT_VIOLATION",
187
+ `Expected subgroup stream type 0x10-0x1D, got 0x${streamType.toString(16)}`,
188
+ 0
189
+ );
190
+ }
191
+ const extensionsPresent = (streamType & 1) !== 0;
192
+ const hasSubgroupField = (streamType & 4) !== 0;
193
+ const subgroupIsFirstObjId = (streamType & 2) !== 0 && !hasSubgroupField;
194
+ const trackAlias = r.readVarInt();
195
+ const groupId = r.readVarInt();
196
+ let subgroupId = 0n;
197
+ if (hasSubgroupField) {
198
+ subgroupId = r.readVarInt();
199
+ }
200
+ const publisherPriority = r.readUint8();
201
+ return {
202
+ streamType,
203
+ extensionsPresent,
204
+ subgroupIsFirstObjId,
205
+ trackAlias,
206
+ groupId,
207
+ subgroupId,
208
+ publisherPriority
209
+ };
210
+ }
211
+ function readSubgroupObject(r, extensionsPresent, isFirst, prevObjectId, base) {
212
+ const byteOffset = base + r.offset;
213
+ const delta = r.readVarInt();
214
+ const objectId = isFirst ? delta : prevObjectId + 1n + delta;
215
+ let extensionData = new Uint8Array(0);
216
+ if (extensionsPresent) {
217
+ const extLen = Number(r.readVarInt());
218
+ extensionData = extLen > 0 ? r.readBytesView(extLen) : new Uint8Array(0);
219
+ }
220
+ const payloadLength = Number(r.readVarInt());
221
+ let payload;
222
+ let status;
223
+ let payloadByteOffset;
224
+ if (payloadLength === 0) {
225
+ status = r.readVarInt();
226
+ payloadByteOffset = base + r.offset;
227
+ payload = new Uint8Array(0);
228
+ } else {
229
+ payloadByteOffset = base + r.offset;
230
+ payload = r.readBytesView(payloadLength);
231
+ }
232
+ const obj = {
233
+ type: "object",
234
+ byteOffset,
235
+ payloadByteOffset,
236
+ objectId,
237
+ payloadLength,
238
+ payload,
239
+ extensionData
240
+ };
241
+ if (status !== void 0) obj.status = status;
242
+ return obj;
243
+ }
180
244
  function decodeSubgroupStream(bytes) {
181
245
  try {
182
246
  const r = new BufferReader(bytes);
183
- const streamType = Number(r.readVarInt());
184
- if (!isValidSubgroupType(streamType)) {
185
- return {
186
- ok: false,
187
- error: new DecodeError(
188
- "CONSTRAINT_VIOLATION",
189
- `Expected subgroup stream type 0x10-0x1D, got 0x${streamType.toString(16)}`,
190
- 0
191
- )
192
- };
193
- }
194
- const extensionsPresent = (streamType & 1) !== 0;
195
- const hasSubgroupField = (streamType & 4) !== 0;
196
- const subgroupIsFirstObjId = (streamType & 2) !== 0 && !hasSubgroupField;
197
- const trackAlias = r.readVarInt();
198
- const groupId = r.readVarInt();
199
- let subgroupId = 0n;
200
- if (hasSubgroupField) {
201
- subgroupId = r.readVarInt();
202
- }
203
- const publisherPriority = r.readUint8();
247
+ const h = readSubgroupHeader(r);
204
248
  const objects = [];
205
249
  let prevObjectId = -1n;
206
- let firstObject = true;
250
+ let isFirst = true;
251
+ let subgroupId = h.subgroupId;
207
252
  while (r.remaining > 0) {
208
- const byteOffset = r.offset;
209
- const delta = r.readVarInt();
210
- let objectId;
211
- if (firstObject) {
212
- objectId = delta;
213
- if (subgroupIsFirstObjId) {
214
- subgroupId = objectId;
215
- }
216
- firstObject = false;
217
- } else {
218
- objectId = prevObjectId + 1n + delta;
219
- }
220
- let extensionData = new Uint8Array(0);
221
- if (extensionsPresent) {
222
- const extLen = Number(r.readVarInt());
223
- extensionData = extLen > 0 ? r.readBytesView(extLen) : new Uint8Array(0);
224
- }
225
- const payloadLength = Number(r.readVarInt());
226
- let payload;
227
- let status;
228
- let payloadByteOffset;
229
- if (payloadLength === 0) {
230
- status = r.readVarInt();
231
- payloadByteOffset = r.offset;
232
- payload = new Uint8Array(0);
233
- } else {
234
- payloadByteOffset = r.offset;
235
- payload = r.readBytesView(payloadLength);
236
- }
237
- const obj = {
238
- type: "object",
239
- byteOffset,
240
- payloadByteOffset,
241
- objectId,
242
- payloadLength,
243
- payload,
244
- extensionData
245
- };
246
- if (status !== void 0) obj.status = status;
253
+ const obj = readSubgroupObject(r, h.extensionsPresent, isFirst, prevObjectId, 0);
254
+ if (isFirst && h.subgroupIsFirstObjId) subgroupId = obj.objectId;
255
+ isFirst = false;
247
256
  objects.push(obj);
248
- prevObjectId = objectId;
257
+ prevObjectId = obj.objectId;
249
258
  }
259
+ const result = {
260
+ type: "subgroup",
261
+ headerType: h.streamType,
262
+ trackAlias: h.trackAlias,
263
+ groupId: h.groupId,
264
+ subgroupId,
265
+ publisherPriority: h.publisherPriority,
266
+ objects
267
+ };
250
268
  return {
251
269
  ok: true,
252
- value: {
253
- type: "subgroup",
254
- headerType: streamType,
255
- trackAlias,
256
- groupId,
257
- subgroupId,
258
- publisherPriority,
259
- objects
260
- },
270
+ value: result,
261
271
  bytesRead: r.offset
262
272
  };
263
273
  } catch (e) {
@@ -326,6 +336,44 @@ function decodeDatagram(bytes) {
326
336
  throw e;
327
337
  }
328
338
  }
339
+ function newFetchObjectState() {
340
+ return {};
341
+ }
342
+ function readFetchObject(r, base, _st) {
343
+ const byteOffset = base + r.offset;
344
+ const groupId = r.readVarInt();
345
+ const subgroupId = r.readVarInt();
346
+ const objectId = r.readVarInt();
347
+ const publisherPriority = r.readUint8();
348
+ const extLen = Number(r.readVarInt());
349
+ const extensionData = extLen > 0 ? r.readBytesView(extLen) : new Uint8Array(0);
350
+ const payloadLength = Number(r.readVarInt());
351
+ let payload;
352
+ let status;
353
+ let payloadByteOffset;
354
+ if (payloadLength === 0) {
355
+ status = r.readVarInt();
356
+ payloadByteOffset = base + r.offset;
357
+ payload = new Uint8Array(0);
358
+ } else {
359
+ payloadByteOffset = base + r.offset;
360
+ payload = r.readBytesView(payloadLength);
361
+ }
362
+ const obj = {
363
+ type: "object",
364
+ byteOffset,
365
+ payloadByteOffset,
366
+ groupId,
367
+ subgroupId,
368
+ objectId,
369
+ publisherPriority,
370
+ payloadLength,
371
+ payload,
372
+ extensionData
373
+ };
374
+ if (status !== void 0) obj.status = status;
375
+ return obj;
376
+ }
329
377
  function decodeFetchStream(bytes) {
330
378
  try {
331
379
  const r = new BufferReader(bytes);
@@ -342,40 +390,9 @@ function decodeFetchStream(bytes) {
342
390
  }
343
391
  const requestId = r.readVarInt();
344
392
  const objects = [];
393
+ const st = newFetchObjectState();
345
394
  while (r.remaining > 0) {
346
- const byteOffset = r.offset;
347
- const groupId = r.readVarInt();
348
- const subgroupId = r.readVarInt();
349
- const objectId = r.readVarInt();
350
- const publisherPriority = r.readUint8();
351
- const extLen = Number(r.readVarInt());
352
- const extensionData = extLen > 0 ? r.readBytesView(extLen) : new Uint8Array(0);
353
- const payloadLength = Number(r.readVarInt());
354
- let payload;
355
- let status;
356
- let payloadByteOffset;
357
- if (payloadLength === 0) {
358
- status = r.readVarInt();
359
- payloadByteOffset = r.offset;
360
- payload = new Uint8Array(0);
361
- } else {
362
- payloadByteOffset = r.offset;
363
- payload = r.readBytesView(payloadLength);
364
- }
365
- const obj = {
366
- type: "object",
367
- byteOffset,
368
- payloadByteOffset,
369
- groupId,
370
- subgroupId,
371
- objectId,
372
- publisherPriority,
373
- payloadLength,
374
- payload,
375
- extensionData
376
- };
377
- if (status !== void 0) obj.status = status;
378
- objects.push(obj);
395
+ objects.push(readFetchObject(r, 0, st));
379
396
  }
380
397
  return {
381
398
  ok: true,
@@ -401,56 +418,68 @@ function decodeDataStream(streamType, bytes) {
401
418
  }
402
419
  }
403
420
  }
421
+ var StreamBuffer = class {
422
+ buf = new Uint8Array(0);
423
+ /** Bytes written into `buf`. */
424
+ len = 0;
425
+ /** Consumed prefix of `buf`; `base + offset` is the position in the stream. */
426
+ offset = 0;
427
+ /** Absolute position of `buf[0]` within the stream. */
428
+ base = 0;
429
+ get unread() {
430
+ return this.len - this.offset;
431
+ }
432
+ append(chunk) {
433
+ if (this.len + chunk.length > this.buf.length) {
434
+ const live = this.len - this.offset;
435
+ const needed = live + chunk.length;
436
+ let cap = Math.max(4096, this.buf.length);
437
+ while (cap < needed * 2) cap *= 2;
438
+ const next = new Uint8Array(cap);
439
+ next.set(this.buf.subarray(this.offset, this.len), 0);
440
+ this.buf = next;
441
+ this.base += this.offset;
442
+ this.len = live;
443
+ this.offset = 0;
444
+ }
445
+ this.buf.set(chunk, this.len);
446
+ this.len += chunk.length;
447
+ }
448
+ /**
449
+ * The written bytes, for a reader positioned at `offset`. Bounded by `len`
450
+ * rather than by capacity, so `BufferReader.remaining` counts real bytes and
451
+ * not the spare room after them.
452
+ */
453
+ written() {
454
+ return this.buf.subarray(0, this.len);
455
+ }
456
+ };
404
457
  function createSubgroupStreamDecoder() {
405
- let buffer = new Uint8Array(0);
406
- let offset = 0;
458
+ const b = new StreamBuffer();
459
+ let header = null;
407
460
  let headerEmitted = false;
408
- let extensionsPresent = false;
409
461
  let prevObjectId = -1n;
410
- let firstObject = true;
462
+ let isFirst = true;
463
+ function emitHeader(controller, h, subgroupId) {
464
+ controller.enqueue({
465
+ type: "subgroup_header",
466
+ headerType: h.streamType,
467
+ trackAlias: h.trackAlias,
468
+ groupId: h.groupId,
469
+ subgroupId,
470
+ publisherPriority: h.publisherPriority
471
+ });
472
+ headerEmitted = true;
473
+ }
411
474
  return new TransformStream({
412
475
  transform(chunk, controller) {
413
- if (offset > 0) {
414
- buffer = buffer.subarray(offset);
415
- offset = 0;
416
- }
417
- const newBuffer = new Uint8Array(buffer.length + chunk.length);
418
- newBuffer.set(buffer, 0);
419
- newBuffer.set(chunk, buffer.length);
420
- buffer = newBuffer;
421
- if (!headerEmitted) {
476
+ b.append(chunk);
477
+ const view = b.written();
478
+ if (header === null) {
422
479
  try {
423
- const r = new BufferReader(buffer.subarray(offset));
424
- const streamType = Number(r.readVarInt());
425
- if (!isValidSubgroupType(streamType)) {
426
- controller.error(
427
- new DecodeError(
428
- "CONSTRAINT_VIOLATION",
429
- `Expected subgroup stream type 0x10-0x1D, got 0x${streamType.toString(16)}`,
430
- 0
431
- )
432
- );
433
- return;
434
- }
435
- extensionsPresent = (streamType & 1) !== 0;
436
- const hasSubgroupField = (streamType & 4) !== 0;
437
- const trackAlias = r.readVarInt();
438
- const groupId = r.readVarInt();
439
- let subgroupId = 0n;
440
- if (hasSubgroupField) {
441
- subgroupId = r.readVarInt();
442
- }
443
- const publisherPriority = r.readUint8();
444
- controller.enqueue({
445
- type: "subgroup_header",
446
- headerType: streamType,
447
- trackAlias,
448
- groupId,
449
- subgroupId,
450
- publisherPriority
451
- });
452
- headerEmitted = true;
453
- offset += r.offset;
480
+ const hr = new BufferReader(view, b.offset);
481
+ header = readSubgroupHeader(hr);
482
+ b.offset = hr.offset;
454
483
  } catch (e) {
455
484
  if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
456
485
  return;
@@ -458,78 +487,51 @@ function createSubgroupStreamDecoder() {
458
487
  controller.error(e);
459
488
  return;
460
489
  }
490
+ if (!header.subgroupIsFirstObjId) emitHeader(controller, header, header.subgroupId);
461
491
  }
462
- while (offset < buffer.length) {
492
+ const h = header;
493
+ if (h === null) return;
494
+ const r = new BufferReader(view, b.offset);
495
+ while (r.remaining > 0) {
496
+ const start = r.offset;
497
+ let obj;
463
498
  try {
464
- const r = new BufferReader(buffer.subarray(offset));
465
- const byteOffset = r.offset;
466
- const delta = r.readVarInt();
467
- const objectId = firstObject ? delta : prevObjectId + 1n + delta;
468
- firstObject = false;
469
- let extensionData = new Uint8Array(0);
470
- if (extensionsPresent) {
471
- const extLen = Number(r.readVarInt());
472
- extensionData = extLen > 0 ? r.readBytesView(extLen) : new Uint8Array(0);
473
- }
474
- const payloadLength = Number(r.readVarInt());
475
- let payload;
476
- let status;
477
- let payloadByteOffset;
478
- if (payloadLength === 0) {
479
- status = r.readVarInt();
480
- payloadByteOffset = r.offset;
481
- payload = new Uint8Array(0);
482
- } else {
483
- payloadByteOffset = r.offset;
484
- payload = r.readBytesView(payloadLength);
485
- }
486
- const obj = {
487
- type: "object",
488
- byteOffset,
489
- payloadByteOffset,
490
- objectId,
491
- payloadLength,
492
- payload,
493
- extensionData
494
- };
495
- if (status !== void 0) obj.status = status;
496
- controller.enqueue(obj);
497
- prevObjectId = objectId;
498
- offset += r.offset;
499
+ obj = readSubgroupObject(r, h.extensionsPresent, isFirst, prevObjectId, b.base);
499
500
  } catch (e) {
500
501
  if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
501
- break;
502
+ b.offset = start;
503
+ return;
502
504
  }
503
505
  controller.error(e);
504
506
  return;
505
507
  }
508
+ if (!headerEmitted) emitHeader(controller, h, obj.objectId);
509
+ isFirst = false;
510
+ b.offset = r.offset;
511
+ prevObjectId = obj.objectId;
512
+ controller.enqueue(obj);
506
513
  }
507
514
  },
508
515
  flush(controller) {
509
- if (offset < buffer.length) {
516
+ if (header !== null && !headerEmitted) emitHeader(controller, header, 0n);
517
+ if (b.unread > 0) {
510
518
  controller.error(new DecodeError("UNEXPECTED_END", "Stream ended with incomplete data", 0));
511
519
  }
512
520
  }
513
521
  });
514
522
  }
515
523
  function createFetchStreamDecoder() {
516
- let buffer = new Uint8Array(0);
517
- let offset = 0;
524
+ const b = new StreamBuffer();
518
525
  let headerEmitted = false;
526
+ const st = newFetchObjectState();
519
527
  return new TransformStream({
520
528
  transform(chunk, controller) {
521
- if (offset > 0) {
522
- buffer = buffer.subarray(offset);
523
- offset = 0;
524
- }
525
- const newBuffer = new Uint8Array(buffer.length + chunk.length);
526
- newBuffer.set(buffer, 0);
527
- newBuffer.set(chunk, buffer.length);
528
- buffer = newBuffer;
529
+ b.append(chunk);
530
+ const view = b.written();
529
531
  if (!headerEmitted) {
530
532
  try {
531
- const r = new BufferReader(buffer.subarray(offset));
532
- const streamType = r.readVarInt();
533
+ const r2 = new BufferReader(view, b.offset);
534
+ const streamType = r2.readVarInt();
533
535
  if (streamType !== FETCH_STREAM_TYPE) {
534
536
  controller.error(
535
537
  new DecodeError(
@@ -540,10 +542,10 @@ function createFetchStreamDecoder() {
540
542
  );
541
543
  return;
542
544
  }
543
- const requestId = r.readVarInt();
545
+ const requestId = r2.readVarInt();
544
546
  controller.enqueue({ type: "fetch_header", requestId });
545
547
  headerEmitted = true;
546
- offset += r.offset;
548
+ b.offset = r2.offset;
547
549
  } catch (e) {
548
550
  if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
549
551
  return;
@@ -552,54 +554,26 @@ function createFetchStreamDecoder() {
552
554
  return;
553
555
  }
554
556
  }
555
- while (offset < buffer.length) {
557
+ const r = new BufferReader(view, b.offset);
558
+ while (r.remaining > 0) {
559
+ const start = r.offset;
560
+ let obj;
556
561
  try {
557
- const r = new BufferReader(buffer.subarray(offset));
558
- const byteOffset = r.offset;
559
- const groupId = r.readVarInt();
560
- const subgroupId = r.readVarInt();
561
- const objectId = r.readVarInt();
562
- const publisherPriority = r.readUint8();
563
- const extLen = Number(r.readVarInt());
564
- const extensionData = extLen > 0 ? r.readBytesView(extLen) : new Uint8Array(0);
565
- const payloadLength = Number(r.readVarInt());
566
- let payload;
567
- let status;
568
- let payloadByteOffset;
569
- if (payloadLength === 0) {
570
- status = r.readVarInt();
571
- payloadByteOffset = r.offset;
572
- payload = new Uint8Array(0);
573
- } else {
574
- payloadByteOffset = r.offset;
575
- payload = r.readBytesView(payloadLength);
576
- }
577
- const obj = {
578
- type: "object",
579
- byteOffset,
580
- payloadByteOffset,
581
- groupId,
582
- subgroupId,
583
- objectId,
584
- publisherPriority,
585
- payloadLength,
586
- payload,
587
- extensionData
588
- };
589
- if (status !== void 0) obj.status = status;
590
- controller.enqueue(obj);
591
- offset += r.offset;
562
+ obj = readFetchObject(r, b.base, st);
592
563
  } catch (e) {
593
564
  if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
594
- break;
565
+ b.offset = start;
566
+ return;
595
567
  }
596
568
  controller.error(e);
597
569
  return;
598
570
  }
571
+ b.offset = r.offset;
572
+ controller.enqueue(obj);
599
573
  }
600
574
  },
601
575
  flush(controller) {
602
- if (offset < buffer.length) {
576
+ if (b.unread > 0) {
603
577
  controller.error(new DecodeError("UNEXPECTED_END", "Stream ended with incomplete data", 0));
604
578
  }
605
579
  }
@@ -901,8 +875,9 @@ function decodeSetupParams(reader) {
901
875
  if (tokenReader.remaining > 0) {
902
876
  tok.token_type = tokenReader.readVarInt();
903
877
  if (tokenReader.remaining > 0) {
904
- const tokenValue = tokenReader.readBytesView(tokenReader.remaining);
905
- tok.token_value = new Uint8Array(tokenValue);
878
+ const value = new Uint8Array(tokenReader.readBytesView(tokenReader.remaining));
879
+ tok.token_value = value;
880
+ noteAuthTokenValue(reader.offset - length, length, value);
906
881
  }
907
882
  }
908
883
  result.authorization_token = [
@@ -1000,8 +975,9 @@ function decodeMessageParams(reader) {
1000
975
  if (tokenReader.remaining > 0) {
1001
976
  tok.token_type = tokenReader.readVarInt();
1002
977
  if (tokenReader.remaining > 0) {
1003
- const tokenValue = tokenReader.readBytesView(tokenReader.remaining);
1004
- tok.token_value = new Uint8Array(tokenValue);
978
+ const value = new Uint8Array(tokenReader.readBytesView(tokenReader.remaining));
979
+ tok.token_value = value;
980
+ noteAuthTokenValue(reader.offset - length, length, value);
1005
981
  }
1006
982
  }
1007
983
  result.authorization_token = [
@@ -1838,6 +1814,23 @@ function createDraft14Codec() {
1838
1814
  createDataStreamDecoder
1839
1815
  };
1840
1816
  }
1817
+ function noteAuthTokenValue(valueStart, valueLength, value) {
1818
+ if (value === void 0 || value.byteLength === 0) return;
1819
+ recordAuthValueSpan(valueStart + valueLength - value.byteLength, value.byteLength);
1820
+ }
1821
+ function redactAuthTokens(bytes) {
1822
+ let payloadStart;
1823
+ try {
1824
+ const probe = new BufferReader(bytes);
1825
+ probe.readVarInt();
1826
+ probe.readUint8();
1827
+ probe.readUint8();
1828
+ payloadStart = probe.offset;
1829
+ } catch {
1830
+ return { bytes, redacted: 0, incomplete: false, decoded: false };
1831
+ }
1832
+ return redactWith(bytes, payloadStart, () => decodeMessage(bytes).ok);
1833
+ }
1841
1834
 
1842
1835
  export {
1843
1836
  MSG_SUBSCRIBE_UPDATE,
@@ -1892,5 +1885,6 @@ export {
1892
1885
  encodeMessage,
1893
1886
  decodeMessage,
1894
1887
  createStreamDecoder,
1895
- createDraft14Codec
1888
+ createDraft14Codec,
1889
+ redactAuthTokens
1896
1890
  };