@moqtap/codec 0.5.0 → 0.6.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 (113) hide show
  1. package/dist/chunk-62LYLROG.js +2095 -0
  2. package/dist/chunk-CQ65LKPQ.cjs +446 -0
  3. package/dist/chunk-MSG2FXJ2.cjs +2095 -0
  4. package/dist/chunk-O5DQ44TM.js +446 -0
  5. package/dist/{codec-yJ_axwtp.d.cts → codec-B1snaM9L.d.cts} +1 -1
  6. package/dist/{codec-Tdey03Bo.d.ts → codec-BLYAy2OP.d.ts} +1 -1
  7. package/dist/{codec-DqHwYWM-.d.ts → codec-BNS3gS77.d.ts} +1 -1
  8. package/dist/{codec-CBLLytyP.d.cts → codec-BOtv3R5j.d.cts} +1 -1
  9. package/dist/{codec-C17JvsEU.d.cts → codec-BOyrk_C9.d.cts} +1 -1
  10. package/dist/{codec-BsSNjvB3.d.cts → codec-BqqoYdh_.d.cts} +1 -1
  11. package/dist/{codec-BhW5CGZq.d.ts → codec-CI0BjmHc.d.ts} +1 -1
  12. package/dist/{codec-BvzKRqiP.d.ts → codec-CWj5iTxA.d.ts} +1 -1
  13. package/dist/{codec-DbpLHKXN.d.cts → codec-CyApDKwf.d.cts} +1 -1
  14. package/dist/{codec-6s9d5yZm.d.ts → codec-D2JbI2tp.d.ts} +1 -1
  15. package/dist/{codec-BzRIVIzS.d.ts → codec-D47r_-Rl.d.ts} +1 -1
  16. package/dist/{codec-GayA_uN0.d.ts → codec-D49OofBr.d.ts} +1 -1
  17. package/dist/{codec-MxIhLDR5.d.cts → codec-D89DWXbv.d.cts} +1 -1
  18. package/dist/{codec-CzBmYisK.d.cts → codec-D95gT98n.d.cts} +1 -1
  19. package/dist/codec-DAePx2NL.d.ts +30 -0
  20. package/dist/{codec-D_9X0i31.d.ts → codec-DJmV04i0.d.ts} +1 -1
  21. package/dist/{codec-Cbv3sG2y.d.ts → codec-DL0njGkL.d.ts} +1 -1
  22. package/dist/{codec-D2YMCU11.d.cts → codec-DQWQSam3.d.cts} +1 -1
  23. package/dist/{codec-Bp4HPezr.d.ts → codec-DplRgIKq.d.ts} +1 -1
  24. package/dist/{codec-3ZgEK-Yj.d.ts → codec-DujHf55Z.d.ts} +1 -1
  25. package/dist/{codec-DS-As0Pp.d.cts → codec-DvcJgYKq.d.cts} +1 -1
  26. package/dist/{codec-D5Ss123f.d.cts → codec-NMsTK0GX.d.cts} +1 -1
  27. package/dist/{codec-BvSmr-Jp.d.ts → codec-WwwUipaU.d.ts} +1 -1
  28. package/dist/{codec-B6ouUJrm.d.cts → codec-cou9XBMF.d.cts} +1 -1
  29. package/dist/{codec-R28KwiGM.d.cts → codec-hvkJfCF5.d.cts} +1 -1
  30. package/dist/codec-xeuGZY77.d.cts +30 -0
  31. package/dist/draft07-session.d.cts +3 -3
  32. package/dist/draft07-session.d.ts +3 -3
  33. package/dist/draft07.d.cts +3 -3
  34. package/dist/draft07.d.ts +3 -3
  35. package/dist/draft08-session.d.cts +3 -3
  36. package/dist/draft08-session.d.ts +3 -3
  37. package/dist/draft08.d.cts +4 -4
  38. package/dist/draft08.d.ts +4 -4
  39. package/dist/draft09-session.d.cts +3 -3
  40. package/dist/draft09-session.d.ts +3 -3
  41. package/dist/draft09.d.cts +4 -4
  42. package/dist/draft09.d.ts +4 -4
  43. package/dist/draft10-session.d.cts +3 -3
  44. package/dist/draft10-session.d.ts +3 -3
  45. package/dist/draft10.d.cts +4 -4
  46. package/dist/draft10.d.ts +4 -4
  47. package/dist/draft11-session.d.cts +3 -3
  48. package/dist/draft11-session.d.ts +3 -3
  49. package/dist/draft11.d.cts +4 -4
  50. package/dist/draft11.d.ts +4 -4
  51. package/dist/draft12-session.d.cts +3 -3
  52. package/dist/draft12-session.d.ts +3 -3
  53. package/dist/draft12.d.cts +4 -4
  54. package/dist/draft12.d.ts +4 -4
  55. package/dist/draft13-session.d.cts +3 -3
  56. package/dist/draft13-session.d.ts +3 -3
  57. package/dist/draft13.d.cts +4 -4
  58. package/dist/draft13.d.ts +4 -4
  59. package/dist/draft14-session.d.cts +3 -3
  60. package/dist/draft14-session.d.ts +3 -3
  61. package/dist/draft14.d.cts +4 -4
  62. package/dist/draft14.d.ts +4 -4
  63. package/dist/draft15-session.d.cts +3 -3
  64. package/dist/draft15-session.d.ts +3 -3
  65. package/dist/draft15.d.cts +4 -4
  66. package/dist/draft15.d.ts +4 -4
  67. package/dist/draft16-session.d.cts +3 -3
  68. package/dist/draft16-session.d.ts +3 -3
  69. package/dist/draft16.d.cts +4 -4
  70. package/dist/draft16.d.ts +4 -4
  71. package/dist/draft17-session.d.cts +3 -3
  72. package/dist/draft17-session.d.ts +3 -3
  73. package/dist/draft17.d.cts +3 -3
  74. package/dist/draft17.d.ts +3 -3
  75. package/dist/draft18-session.d.cts +3 -3
  76. package/dist/draft18-session.d.ts +3 -3
  77. package/dist/draft18.d.cts +3 -3
  78. package/dist/draft18.d.ts +3 -3
  79. package/dist/draft19-session.cjs +8 -0
  80. package/dist/draft19-session.d.cts +50 -0
  81. package/dist/draft19-session.d.ts +50 -0
  82. package/dist/draft19-session.js +8 -0
  83. package/dist/draft19.cjs +189 -0
  84. package/dist/draft19.d.cts +130 -0
  85. package/dist/draft19.d.ts +130 -0
  86. package/dist/draft19.js +189 -0
  87. package/dist/index.cjs +7 -1
  88. package/dist/index.d.cts +20 -15
  89. package/dist/index.d.ts +20 -15
  90. package/dist/index.js +7 -1
  91. package/dist/{session-types-DBpCRtrL.d.ts → session-types-C0l_-Ups.d.ts} +1 -1
  92. package/dist/{session-types-sBaTRwDh.d.cts → session-types-KsqyVp6y.d.cts} +1 -1
  93. package/dist/session.cjs +5 -1
  94. package/dist/session.d.cts +5 -3
  95. package/dist/session.d.ts +5 -3
  96. package/dist/session.js +5 -1
  97. package/dist/{types-CnBuq68e.d.cts → types-CdCSIfvI.d.cts} +1 -1
  98. package/dist/{types-CnBuq68e.d.ts → types-CdCSIfvI.d.ts} +1 -1
  99. package/dist/types-zHgCS6o5.d.cts +265 -0
  100. package/dist/types-zHgCS6o5.d.ts +265 -0
  101. package/package.json +14 -2
  102. package/src/core/types.ts +1 -0
  103. package/src/drafts/draft19/codec.ts +1425 -0
  104. package/src/drafts/draft19/data-streams.ts +996 -0
  105. package/src/drafts/draft19/error-codes.ts +78 -0
  106. package/src/drafts/draft19/index.ts +126 -0
  107. package/src/drafts/draft19/messages.ts +58 -0
  108. package/src/drafts/draft19/rules.ts +85 -0
  109. package/src/drafts/draft19/session-fsm.ts +451 -0
  110. package/src/drafts/draft19/session.ts +15 -0
  111. package/src/drafts/draft19/types.ts +368 -0
  112. package/src/index.ts +9 -1
  113. package/src/session.ts +4 -0
@@ -0,0 +1,2095 @@
1
+ import {
2
+ BufferReader,
3
+ BufferWriter,
4
+ DecodeError,
5
+ bytesToHex,
6
+ hexToBytes
7
+ } from "./chunk-VOU7PNHI.js";
8
+
9
+ // src/drafts/draft19/messages.ts
10
+ var MSG_REQUEST_UPDATE = 0x02n;
11
+ var MSG_SUBSCRIBE = 0x03n;
12
+ var MSG_SUBSCRIBE_OK = 0x04n;
13
+ var MSG_REQUEST_ERROR = 0x05n;
14
+ var MSG_PUBLISH_NAMESPACE = 0x06n;
15
+ var MSG_REQUEST_OK = 0x07n;
16
+ var MSG_NAMESPACE = 0x08n;
17
+ var MSG_PUBLISH_DONE = 0x0bn;
18
+ var MSG_TRACK_STATUS = 0x0dn;
19
+ var MSG_NAMESPACE_DONE = 0x0en;
20
+ var MSG_PUBLISH_SKIPPED = 0x0fn;
21
+ var MSG_GOAWAY = 0x10n;
22
+ var MSG_FETCH = 0x16n;
23
+ var MSG_FETCH_OK = 0x18n;
24
+ var MSG_PUBLISH = 0x1dn;
25
+ var MSG_PUBLISH_OK = MSG_REQUEST_OK;
26
+ var MSG_SUBSCRIBE_NAMESPACE = 0x50n;
27
+ var MSG_SUBSCRIBE_TRACKS = 0x51n;
28
+ var MSG_SETUP = 0x2f00n;
29
+ var SETUP_OPT_PATH = 0x01n;
30
+ var SETUP_OPT_AUTHORIZATION_TOKEN = 0x03n;
31
+ var SETUP_OPT_MAX_AUTH_TOKEN_CACHE_SIZE = 0x04n;
32
+ var SETUP_OPT_AUTHORITY = 0x05n;
33
+ var SETUP_OPT_MAX_FILTER_RANGES = 0x06n;
34
+ var SETUP_OPT_MOQT_IMPLEMENTATION = 0x07n;
35
+ var SETUP_OPT_MAX_REQUEST_UPDATES = 0x08n;
36
+ var MESSAGE_TYPE_MAP = /* @__PURE__ */ new Map([
37
+ [MSG_SETUP, "setup"],
38
+ [MSG_SUBSCRIBE, "subscribe"],
39
+ [MSG_SUBSCRIBE_OK, "subscribe_ok"],
40
+ [MSG_REQUEST_UPDATE, "request_update"],
41
+ [MSG_PUBLISH, "publish"],
42
+ [MSG_PUBLISH_DONE, "publish_done"],
43
+ [MSG_PUBLISH_NAMESPACE, "publish_namespace"],
44
+ [MSG_NAMESPACE, "namespace"],
45
+ [MSG_NAMESPACE_DONE, "namespace_done"],
46
+ [MSG_SUBSCRIBE_NAMESPACE, "subscribe_namespace"],
47
+ [MSG_SUBSCRIBE_TRACKS, "subscribe_tracks"],
48
+ [MSG_PUBLISH_SKIPPED, "publish_skipped"],
49
+ [MSG_FETCH, "fetch"],
50
+ [MSG_FETCH_OK, "fetch_ok"],
51
+ [MSG_TRACK_STATUS, "track_status"],
52
+ [MSG_REQUEST_OK, "request_ok"],
53
+ [MSG_REQUEST_ERROR, "request_error"],
54
+ [MSG_GOAWAY, "goaway"]
55
+ ]);
56
+ var MESSAGE_ID_MAP = new Map(
57
+ [...MESSAGE_TYPE_MAP.entries()].map(([id, name]) => [name, id])
58
+ );
59
+
60
+ // src/drafts/draft19/data-streams.ts
61
+ var FETCH_STREAM_TYPE = 0x05n;
62
+ var OBJPROP_PRIOR_GROUP_ID_GAP = 0x3cn;
63
+ var OBJPROP_PRIOR_OBJECT_ID_GAP = 0x3en;
64
+ var KNOWN_OBJ_PROPS = /* @__PURE__ */ new Map([
65
+ [OBJPROP_PRIOR_OBJECT_ID_GAP, "prior_object_id_gap"],
66
+ [OBJPROP_PRIOR_GROUP_ID_GAP, "prior_group_id_gap"]
67
+ ]);
68
+ function decodeObjectProperties(r, propsLength) {
69
+ const endOff = r.offset + propsLength;
70
+ const props = {};
71
+ let prevType = 0n;
72
+ while (r.offset < endOff) {
73
+ const delta = r.readVarInt();
74
+ const propType = prevType + delta;
75
+ prevType = propType;
76
+ if (propType % 2n === 0n) {
77
+ const value = r.readVarInt();
78
+ const name = KNOWN_OBJ_PROPS.get(propType) ?? `0x${propType.toString(16)}`;
79
+ props[name] = value;
80
+ } else {
81
+ const length = Number(r.readVarInt());
82
+ r.readBytes(length);
83
+ }
84
+ }
85
+ return props;
86
+ }
87
+ function encodeObjectProperties(props, w) {
88
+ const reverseMap = /* @__PURE__ */ new Map();
89
+ for (const [id, name] of KNOWN_OBJ_PROPS) {
90
+ reverseMap.set(name, id);
91
+ }
92
+ const entries = [];
93
+ for (const [name, value] of Object.entries(props)) {
94
+ const typeId = reverseMap.get(name);
95
+ if (typeId !== void 0) {
96
+ entries.push({ type: typeId, value });
97
+ }
98
+ }
99
+ entries.sort((a, b) => a.type < b.type ? -1 : a.type > b.type ? 1 : 0);
100
+ let prevType = 0n;
101
+ for (const entry of entries) {
102
+ w.writeVarInt(entry.type - prevType);
103
+ w.writeVarInt(entry.value);
104
+ prevType = entry.type;
105
+ }
106
+ }
107
+ function isValidSubgroupType(streamType) {
108
+ const inRange = streamType >= 16 && streamType <= 31 || streamType >= 48 && streamType <= 63 || streamType >= 80 && streamType <= 95 || streamType >= 112 && streamType <= 127;
109
+ if (!inRange) return false;
110
+ return (streamType & 6) !== 6;
111
+ }
112
+ function encodeSubgroupStream(stream) {
113
+ const w = new BufferWriter();
114
+ const streamType = stream.headerType;
115
+ w.writeUint8(streamType);
116
+ const propertiesPresent = (streamType & 1) !== 0;
117
+ const subgroupMode = (streamType & 6) >> 1;
118
+ const hasSubgroupField = subgroupMode === 2;
119
+ const hasPriority = (streamType & 32) === 0;
120
+ w.writeVarInt(stream.trackAlias);
121
+ w.writeVarInt(stream.groupId);
122
+ if (hasSubgroupField) {
123
+ w.writeVarInt(stream.subgroupId);
124
+ }
125
+ if (hasPriority) {
126
+ w.writeUint8(stream.publisherPriority);
127
+ }
128
+ let prevObjectId = -1n;
129
+ for (const obj of stream.objects) {
130
+ const delta = prevObjectId < 0n ? obj.objectId : obj.objectId - prevObjectId - 1n;
131
+ w.writeVarInt(delta);
132
+ if (propertiesPresent) {
133
+ if (obj.objectProperties && Object.keys(obj.objectProperties).length > 0) {
134
+ const tmpW = new BufferWriter(32);
135
+ encodeObjectProperties(obj.objectProperties, tmpW);
136
+ const raw = tmpW.finish();
137
+ w.writeVarInt(BigInt(raw.byteLength));
138
+ w.writeBytes(raw);
139
+ } else {
140
+ w.writeVarInt(0n);
141
+ }
142
+ }
143
+ w.writeVarInt(BigInt(obj.payloadLength));
144
+ if (obj.payloadLength === 0) {
145
+ w.writeVarInt(obj.status ?? 0n);
146
+ } else {
147
+ w.writeBytes(obj.payload);
148
+ }
149
+ prevObjectId = obj.objectId;
150
+ }
151
+ return w.finish();
152
+ }
153
+ function encodeDatagram(dg) {
154
+ const w = new BufferWriter();
155
+ const dgType = dg.datagramType;
156
+ w.writeUint8(dgType);
157
+ w.writeVarInt(dg.trackAlias);
158
+ w.writeVarInt(dg.groupId);
159
+ const objectIdAbsent = (dgType & 4) !== 0;
160
+ const isStatus = (dgType & 32) !== 0;
161
+ const defaultPriority = (dgType & 8) !== 0;
162
+ const propertiesPresent = (dgType & 1) !== 0;
163
+ if (!objectIdAbsent) {
164
+ w.writeVarInt(dg.objectId);
165
+ }
166
+ if (!defaultPriority) {
167
+ w.writeUint8(dg.publisherPriority);
168
+ }
169
+ if (propertiesPresent) {
170
+ if (dg.objectProperties && Object.keys(dg.objectProperties).length > 0) {
171
+ const tmpW = new BufferWriter(32);
172
+ encodeObjectProperties(dg.objectProperties, tmpW);
173
+ const raw = tmpW.finish();
174
+ w.writeVarInt(BigInt(raw.byteLength));
175
+ w.writeBytes(raw);
176
+ } else {
177
+ w.writeVarInt(0n);
178
+ }
179
+ }
180
+ if (isStatus) {
181
+ w.writeVarInt(dg.objectStatus ?? 0n);
182
+ } else {
183
+ w.writeBytes(dg.payload);
184
+ }
185
+ return w.finish();
186
+ }
187
+ function encodeFetchStream(stream) {
188
+ const w = new BufferWriter();
189
+ w.writeVarInt(FETCH_STREAM_TYPE);
190
+ w.writeVarInt(stream.requestId);
191
+ let prevGroupId = 0n;
192
+ let prevObjectId = 0n;
193
+ let first = true;
194
+ for (const obj of stream.objects) {
195
+ w.writeVarInt(BigInt(obj.serializationFlags));
196
+ const flags = obj.serializationFlags;
197
+ if (flags >= 128) {
198
+ w.writeVarInt(obj.groupId);
199
+ w.writeVarInt(obj.objectId);
200
+ w.writeVarInt(BigInt(obj.payloadLength));
201
+ } else if (flags & 64) {
202
+ if (flags & 8) {
203
+ if (first) w.writeVarInt(obj.groupId);
204
+ else w.writeVarInt(obj.groupId - prevGroupId - 1n);
205
+ }
206
+ if (flags & 4) w.writeVarInt(obj.objectId);
207
+ if (flags & 16) w.writeUint8(obj.publisherPriority);
208
+ if (flags & 32) {
209
+ if (obj.objectProperties && Object.keys(obj.objectProperties).length > 0) {
210
+ const tmpW = new BufferWriter(32);
211
+ encodeObjectProperties(obj.objectProperties, tmpW);
212
+ const raw = tmpW.finish();
213
+ w.writeVarInt(BigInt(raw.byteLength));
214
+ w.writeBytes(raw);
215
+ } else {
216
+ w.writeVarInt(0n);
217
+ }
218
+ }
219
+ w.writeVarInt(BigInt(obj.payloadLength));
220
+ if (obj.payloadLength > 0) {
221
+ w.writeBytes(obj.payload);
222
+ }
223
+ } else {
224
+ if (flags & 8) {
225
+ if (first) w.writeVarInt(obj.groupId);
226
+ else w.writeVarInt(obj.groupId - prevGroupId - 1n);
227
+ }
228
+ const subgroupEncoding = flags & 3;
229
+ if (subgroupEncoding === 3) w.writeVarInt(obj.subgroupId);
230
+ if (flags & 4) w.writeVarInt(obj.objectId);
231
+ if (flags & 16) w.writeUint8(obj.publisherPriority);
232
+ if (flags & 32) {
233
+ if (obj.objectProperties && Object.keys(obj.objectProperties).length > 0) {
234
+ const tmpW = new BufferWriter(32);
235
+ encodeObjectProperties(obj.objectProperties, tmpW);
236
+ const raw = tmpW.finish();
237
+ w.writeVarInt(BigInt(raw.byteLength));
238
+ w.writeBytes(raw);
239
+ } else {
240
+ w.writeVarInt(0n);
241
+ }
242
+ }
243
+ w.writeVarInt(BigInt(obj.payloadLength));
244
+ if (obj.payloadLength > 0) {
245
+ w.writeBytes(obj.payload);
246
+ }
247
+ }
248
+ prevGroupId = obj.groupId;
249
+ prevObjectId = obj.objectId;
250
+ first = false;
251
+ }
252
+ void prevObjectId;
253
+ return w.finish();
254
+ }
255
+ function decodeSubgroupStream(bytes) {
256
+ try {
257
+ const r = new BufferReader(bytes);
258
+ const streamType = r.readUint8();
259
+ if (!isValidSubgroupType(streamType)) {
260
+ return {
261
+ ok: false,
262
+ error: new DecodeError(
263
+ "CONSTRAINT_VIOLATION",
264
+ `Invalid subgroup stream type 0x${streamType.toString(16)}`,
265
+ 0
266
+ )
267
+ };
268
+ }
269
+ const propertiesPresent = (streamType & 1) !== 0;
270
+ const subgroupMode = (streamType & 6) >> 1;
271
+ const hasSubgroupField = subgroupMode === 2;
272
+ const subgroupIsFirstObjId = subgroupMode === 1;
273
+ const endOfGroup = (streamType & 8) !== 0;
274
+ const hasPriority = (streamType & 32) === 0;
275
+ const firstObject = (streamType & 64) !== 0;
276
+ const trackAlias = r.readVarInt();
277
+ const groupId = r.readVarInt();
278
+ let subgroupId = 0n;
279
+ if (hasSubgroupField) {
280
+ subgroupId = r.readVarInt();
281
+ }
282
+ let publisherPriority = 128;
283
+ if (hasPriority) {
284
+ publisherPriority = r.readUint8();
285
+ }
286
+ const objects = [];
287
+ let prevObjectId = -1n;
288
+ let isFirst = true;
289
+ while (r.remaining > 0) {
290
+ const byteOffset = r.offset;
291
+ const delta = r.readVarInt();
292
+ let objectId;
293
+ if (isFirst) {
294
+ objectId = delta;
295
+ if (subgroupIsFirstObjId) {
296
+ subgroupId = objectId;
297
+ }
298
+ isFirst = false;
299
+ } else {
300
+ objectId = prevObjectId + 1n + delta;
301
+ }
302
+ const extensionData = new Uint8Array(0);
303
+ let objectProperties;
304
+ if (propertiesPresent) {
305
+ const propsLen = Number(r.readVarInt());
306
+ if (propsLen > 0) {
307
+ objectProperties = decodeObjectProperties(r, propsLen);
308
+ }
309
+ }
310
+ const payloadLength = Number(r.readVarInt());
311
+ let payload;
312
+ let status;
313
+ let payloadByteOffset;
314
+ if (payloadLength === 0) {
315
+ status = r.readVarInt();
316
+ payloadByteOffset = r.offset;
317
+ payload = new Uint8Array(0);
318
+ } else {
319
+ payloadByteOffset = r.offset;
320
+ payload = r.readBytesView(payloadLength);
321
+ }
322
+ const obj = {
323
+ type: "object",
324
+ byteOffset,
325
+ payloadByteOffset,
326
+ objectId,
327
+ payloadLength,
328
+ payload,
329
+ extensionData
330
+ };
331
+ if (status !== void 0) obj.status = status;
332
+ if (objectProperties !== void 0)
333
+ obj.objectProperties = objectProperties;
334
+ objects.push(obj);
335
+ prevObjectId = objectId;
336
+ }
337
+ const result = {
338
+ type: "subgroup",
339
+ headerType: streamType,
340
+ trackAlias,
341
+ groupId,
342
+ subgroupId,
343
+ publisherPriority,
344
+ objects
345
+ };
346
+ if (endOfGroup) result.endOfGroup = true;
347
+ if (firstObject) result.firstObject = true;
348
+ return {
349
+ ok: true,
350
+ value: result,
351
+ bytesRead: r.offset
352
+ };
353
+ } catch (e) {
354
+ if (e instanceof DecodeError) return { ok: false, error: e };
355
+ throw e;
356
+ }
357
+ }
358
+ function decodeDatagram(bytes) {
359
+ try {
360
+ const r = new BufferReader(bytes);
361
+ const dgType = r.readUint8();
362
+ const objectIdAbsent = (dgType & 4) !== 0;
363
+ const endOfGroup = (dgType & 2) !== 0;
364
+ const isStatus = (dgType & 32) !== 0;
365
+ const defaultPriority = (dgType & 8) !== 0;
366
+ const propertiesPresent = (dgType & 1) !== 0;
367
+ const trackAlias = r.readVarInt();
368
+ const groupId = r.readVarInt();
369
+ let objectId = 0n;
370
+ if (!objectIdAbsent) {
371
+ objectId = r.readVarInt();
372
+ }
373
+ let publisherPriority = 128;
374
+ if (!defaultPriority) {
375
+ publisherPriority = r.readUint8();
376
+ }
377
+ let objectProperties;
378
+ if (propertiesPresent) {
379
+ const propsLen = Number(r.readVarInt());
380
+ if (propsLen > 0) {
381
+ objectProperties = decodeObjectProperties(r, propsLen);
382
+ }
383
+ }
384
+ let objectStatus;
385
+ let payload;
386
+ if (isStatus) {
387
+ objectStatus = r.readVarInt();
388
+ payload = new Uint8Array(0);
389
+ } else {
390
+ payload = r.readBytesView(r.remaining);
391
+ }
392
+ const payloadLength = payload.byteLength;
393
+ const result = {
394
+ type: "datagram",
395
+ datagramType: dgType,
396
+ trackAlias,
397
+ groupId,
398
+ objectId,
399
+ publisherPriority,
400
+ payloadLength,
401
+ payload
402
+ };
403
+ if (endOfGroup) result.endOfGroup = true;
404
+ if (objectStatus !== void 0)
405
+ result.objectStatus = objectStatus;
406
+ if (objectProperties !== void 0)
407
+ result.objectProperties = objectProperties;
408
+ return { ok: true, value: result, bytesRead: r.offset };
409
+ } catch (e) {
410
+ if (e instanceof DecodeError) return { ok: false, error: e };
411
+ throw e;
412
+ }
413
+ }
414
+ function decodeFetchStream(bytes) {
415
+ try {
416
+ const r = new BufferReader(bytes);
417
+ const streamType = r.readVarInt();
418
+ if (streamType !== FETCH_STREAM_TYPE) {
419
+ return {
420
+ ok: false,
421
+ error: new DecodeError(
422
+ "CONSTRAINT_VIOLATION",
423
+ `Expected fetch stream type 0x05, got 0x${streamType.toString(16)}`,
424
+ 0
425
+ )
426
+ };
427
+ }
428
+ const requestId = r.readVarInt();
429
+ const objects = [];
430
+ let prevGroupId = 0n;
431
+ let prevSubgroupId = 0n;
432
+ let prevObjectId = 0n;
433
+ let prevPriority = 128;
434
+ let first = true;
435
+ while (r.remaining > 0) {
436
+ const byteOffset = r.offset;
437
+ const flags = Number(r.readVarInt());
438
+ let groupId = prevGroupId;
439
+ let subgroupId = prevSubgroupId;
440
+ let objectId = prevObjectId + 1n;
441
+ let payloadLength;
442
+ let payload;
443
+ let payloadByteOffset;
444
+ const extensionData = new Uint8Array(0);
445
+ if (flags >= 128) {
446
+ if (flags !== 140 && flags !== 268) {
447
+ return {
448
+ ok: false,
449
+ error: new DecodeError(
450
+ "CONSTRAINT_VIOLATION",
451
+ `Unknown serialization flags value: 0x${flags.toString(16)}`,
452
+ r.offset
453
+ )
454
+ };
455
+ }
456
+ groupId = r.readVarInt();
457
+ objectId = r.readVarInt();
458
+ payloadLength = Number(r.readVarInt());
459
+ payloadByteOffset = r.offset;
460
+ payload = payloadLength > 0 ? r.readBytesView(payloadLength) : new Uint8Array(0);
461
+ } else if (flags & 64) {
462
+ const objectIdPresent = (flags & 4) !== 0;
463
+ const groupIdPresent = (flags & 8) !== 0;
464
+ const priorityPresent = (flags & 16) !== 0;
465
+ const propsPresent = (flags & 32) !== 0;
466
+ if (groupIdPresent) {
467
+ const groupDelta = r.readVarInt();
468
+ groupId = first ? groupDelta : prevGroupId + groupDelta + 1n;
469
+ }
470
+ if (objectIdPresent) {
471
+ objectId = r.readVarInt();
472
+ }
473
+ if (priorityPresent) {
474
+ prevPriority = r.readUint8();
475
+ }
476
+ let objectProperties;
477
+ if (propsPresent) {
478
+ const propsLen = Number(r.readVarInt());
479
+ if (propsLen > 0) {
480
+ objectProperties = decodeObjectProperties(r, propsLen);
481
+ }
482
+ }
483
+ payloadLength = Number(r.readVarInt());
484
+ payloadByteOffset = r.offset;
485
+ payload = payloadLength > 0 ? r.readBytesView(payloadLength) : new Uint8Array(0);
486
+ const obj2 = {
487
+ type: "object",
488
+ byteOffset,
489
+ payloadByteOffset,
490
+ serializationFlags: flags,
491
+ groupId,
492
+ subgroupId: 0n,
493
+ objectId,
494
+ publisherPriority: prevPriority,
495
+ payloadLength,
496
+ payload,
497
+ extensionData
498
+ };
499
+ if (objectProperties)
500
+ obj2.objectProperties = objectProperties;
501
+ objects.push(obj2);
502
+ prevGroupId = groupId;
503
+ prevObjectId = objectId;
504
+ first = false;
505
+ continue;
506
+ } else {
507
+ const subgroupEncoding = flags & 3;
508
+ const objectIdPresent = (flags & 4) !== 0;
509
+ const groupIdPresent = (flags & 8) !== 0;
510
+ const priorityPresent = (flags & 16) !== 0;
511
+ const propsPresent = (flags & 32) !== 0;
512
+ if (groupIdPresent) {
513
+ const groupDelta = r.readVarInt();
514
+ groupId = first ? groupDelta : prevGroupId + groupDelta + 1n;
515
+ } else if (first) {
516
+ return {
517
+ ok: false,
518
+ error: new DecodeError(
519
+ "CONSTRAINT_VIOLATION",
520
+ "First fetch object must include groupId",
521
+ r.offset
522
+ )
523
+ };
524
+ }
525
+ if (subgroupEncoding === 0) {
526
+ subgroupId = 0n;
527
+ } else if (subgroupEncoding === 1) {
528
+ if (first) {
529
+ return {
530
+ ok: false,
531
+ error: new DecodeError(
532
+ "CONSTRAINT_VIOLATION",
533
+ "First fetch object cannot reference prior subgroupId",
534
+ r.offset
535
+ )
536
+ };
537
+ }
538
+ subgroupId = prevSubgroupId;
539
+ } else if (subgroupEncoding === 2) {
540
+ if (first) {
541
+ return {
542
+ ok: false,
543
+ error: new DecodeError(
544
+ "CONSTRAINT_VIOLATION",
545
+ "First fetch object cannot reference prior subgroupId",
546
+ r.offset
547
+ )
548
+ };
549
+ }
550
+ subgroupId = prevSubgroupId + 1n;
551
+ } else if (subgroupEncoding === 3) {
552
+ subgroupId = r.readVarInt();
553
+ }
554
+ if (objectIdPresent) {
555
+ objectId = r.readVarInt();
556
+ } else if (first) {
557
+ return {
558
+ ok: false,
559
+ error: new DecodeError(
560
+ "CONSTRAINT_VIOLATION",
561
+ "First fetch object must include objectId",
562
+ r.offset
563
+ )
564
+ };
565
+ }
566
+ if (priorityPresent) {
567
+ prevPriority = r.readUint8();
568
+ }
569
+ let objectProperties;
570
+ if (propsPresent) {
571
+ const propsLen = Number(r.readVarInt());
572
+ if (propsLen > 0) {
573
+ objectProperties = decodeObjectProperties(r, propsLen);
574
+ }
575
+ }
576
+ payloadLength = Number(r.readVarInt());
577
+ payloadByteOffset = r.offset;
578
+ payload = payloadLength > 0 ? r.readBytesView(payloadLength) : new Uint8Array(0);
579
+ if (objectProperties) {
580
+ const obj2 = {
581
+ type: "object",
582
+ byteOffset,
583
+ payloadByteOffset,
584
+ serializationFlags: flags,
585
+ groupId,
586
+ subgroupId,
587
+ objectId,
588
+ publisherPriority: prevPriority,
589
+ payloadLength,
590
+ payload,
591
+ extensionData
592
+ };
593
+ obj2.objectProperties = objectProperties;
594
+ objects.push(obj2);
595
+ prevGroupId = groupId;
596
+ prevSubgroupId = subgroupId;
597
+ prevObjectId = objectId;
598
+ first = false;
599
+ continue;
600
+ }
601
+ }
602
+ const obj = {
603
+ type: "object",
604
+ byteOffset,
605
+ payloadByteOffset,
606
+ serializationFlags: flags,
607
+ groupId,
608
+ subgroupId,
609
+ objectId,
610
+ publisherPriority: prevPriority,
611
+ payloadLength,
612
+ payload,
613
+ extensionData
614
+ };
615
+ objects.push(obj);
616
+ prevGroupId = groupId;
617
+ prevSubgroupId = subgroupId;
618
+ prevObjectId = objectId;
619
+ first = false;
620
+ }
621
+ return {
622
+ ok: true,
623
+ value: { type: "fetch", requestId, objects },
624
+ bytesRead: r.offset
625
+ };
626
+ } catch (e) {
627
+ if (e instanceof DecodeError) return { ok: false, error: e };
628
+ throw e;
629
+ }
630
+ }
631
+ function decodeDataStream(streamType, bytes) {
632
+ switch (streamType) {
633
+ case "subgroup":
634
+ return decodeSubgroupStream(bytes);
635
+ case "datagram":
636
+ return decodeDatagram(bytes);
637
+ case "fetch":
638
+ return decodeFetchStream(bytes);
639
+ default: {
640
+ const _exhaustive = streamType;
641
+ throw new Error(`Unknown stream type: ${_exhaustive}`);
642
+ }
643
+ }
644
+ }
645
+ function createSubgroupStreamDecoder() {
646
+ let buffer = new Uint8Array(0);
647
+ let offset = 0;
648
+ let headerEmitted = false;
649
+ let prevObjectId = -1n;
650
+ let firstObject = true;
651
+ let _propertiesPresent = false;
652
+ return new TransformStream({
653
+ transform(chunk, controller) {
654
+ if (offset > 0) {
655
+ buffer = buffer.subarray(offset);
656
+ offset = 0;
657
+ }
658
+ const newBuffer = new Uint8Array(buffer.length + chunk.length);
659
+ newBuffer.set(buffer, 0);
660
+ newBuffer.set(chunk, buffer.length);
661
+ buffer = newBuffer;
662
+ if (!headerEmitted) {
663
+ try {
664
+ const r = new BufferReader(buffer.subarray(offset));
665
+ const streamType = r.readUint8();
666
+ if (!isValidSubgroupType(streamType)) {
667
+ controller.error(
668
+ new DecodeError(
669
+ "CONSTRAINT_VIOLATION",
670
+ `Invalid subgroup stream type 0x${streamType.toString(16)}`,
671
+ 0
672
+ )
673
+ );
674
+ return;
675
+ }
676
+ _propertiesPresent = (streamType & 1) !== 0;
677
+ const subgroupMode = (streamType & 6) >> 1;
678
+ const hasSubgroupField = subgroupMode === 2;
679
+ const hasPriority = (streamType & 32) === 0;
680
+ const trackAlias = r.readVarInt();
681
+ const groupId = r.readVarInt();
682
+ let subgroupId = 0n;
683
+ if (hasSubgroupField) {
684
+ subgroupId = r.readVarInt();
685
+ }
686
+ let publisherPriority = 128;
687
+ if (hasPriority) {
688
+ publisherPriority = r.readUint8();
689
+ }
690
+ controller.enqueue({
691
+ type: "subgroup_header",
692
+ trackAlias,
693
+ groupId,
694
+ subgroupId,
695
+ publisherPriority
696
+ });
697
+ headerEmitted = true;
698
+ offset += r.offset;
699
+ } catch (e) {
700
+ if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
701
+ return;
702
+ }
703
+ controller.error(e);
704
+ return;
705
+ }
706
+ }
707
+ while (offset < buffer.length) {
708
+ try {
709
+ const r = new BufferReader(buffer.subarray(offset));
710
+ const delta = r.readVarInt();
711
+ let objectId;
712
+ if (firstObject) {
713
+ objectId = delta;
714
+ firstObject = false;
715
+ } else {
716
+ objectId = prevObjectId + 1n + delta;
717
+ }
718
+ let extensionData = new Uint8Array(0);
719
+ if (_propertiesPresent) {
720
+ const extLen = Number(r.readVarInt());
721
+ extensionData = extLen > 0 ? r.readBytesView(extLen) : new Uint8Array(0);
722
+ }
723
+ const payloadLength = Number(r.readVarInt());
724
+ const payloadByteOffset = r.offset;
725
+ const payload = payloadLength > 0 ? r.readBytesView(payloadLength) : new Uint8Array(0);
726
+ controller.enqueue({
727
+ type: "object",
728
+ objectId,
729
+ payloadLength,
730
+ payload,
731
+ extensionData,
732
+ byteOffset: 0,
733
+ payloadByteOffset
734
+ });
735
+ offset += r.offset;
736
+ prevObjectId = objectId;
737
+ } catch (e) {
738
+ if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
739
+ break;
740
+ }
741
+ controller.error(e);
742
+ return;
743
+ }
744
+ }
745
+ },
746
+ flush(controller) {
747
+ if (offset < buffer.length) {
748
+ controller.error(new DecodeError("UNEXPECTED_END", "Stream ended with incomplete data", 0));
749
+ }
750
+ }
751
+ });
752
+ }
753
+ function createFetchStreamDecoder() {
754
+ let buffer = new Uint8Array(0);
755
+ let offset = 0;
756
+ let headerEmitted = false;
757
+ return new TransformStream({
758
+ transform(chunk, controller) {
759
+ if (offset > 0) {
760
+ buffer = buffer.subarray(offset);
761
+ offset = 0;
762
+ }
763
+ const newBuffer = new Uint8Array(buffer.length + chunk.length);
764
+ newBuffer.set(buffer, 0);
765
+ newBuffer.set(chunk, buffer.length);
766
+ buffer = newBuffer;
767
+ if (!headerEmitted) {
768
+ try {
769
+ const r = new BufferReader(buffer.subarray(offset));
770
+ const streamType = r.readVarInt();
771
+ if (streamType !== FETCH_STREAM_TYPE) {
772
+ controller.error(
773
+ new DecodeError(
774
+ "CONSTRAINT_VIOLATION",
775
+ `Expected fetch stream type 0x05, got 0x${streamType.toString(16)}`,
776
+ 0
777
+ )
778
+ );
779
+ return;
780
+ }
781
+ const requestId = r.readVarInt();
782
+ controller.enqueue({ type: "fetch_header", requestId });
783
+ headerEmitted = true;
784
+ offset += r.offset;
785
+ } catch (e) {
786
+ if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
787
+ return;
788
+ }
789
+ controller.error(e);
790
+ return;
791
+ }
792
+ }
793
+ while (offset < buffer.length) {
794
+ try {
795
+ const r = new BufferReader(buffer.subarray(offset));
796
+ const flags = Number(r.readVarInt());
797
+ const objectIdPresent = (flags & 4) !== 0;
798
+ const groupIdPresent = (flags & 8) !== 0;
799
+ const priorityPresent = (flags & 16) !== 0;
800
+ const extensionsPresent = (flags & 32) !== 0;
801
+ const subgroupEncoding = flags & 3;
802
+ if (groupIdPresent) r.readVarInt();
803
+ if (subgroupEncoding === 3) r.readVarInt();
804
+ let objectId = 0n;
805
+ if (objectIdPresent) objectId = r.readVarInt();
806
+ if (priorityPresent) r.readUint8();
807
+ let extensionData = new Uint8Array(0);
808
+ if (extensionsPresent) {
809
+ const extLen = Number(r.readVarInt());
810
+ extensionData = extLen > 0 ? r.readBytesView(extLen) : new Uint8Array(0);
811
+ }
812
+ const payloadLength = Number(r.readVarInt());
813
+ const payloadByteOffset = r.offset;
814
+ const payload = payloadLength > 0 ? r.readBytesView(payloadLength) : new Uint8Array(0);
815
+ controller.enqueue({
816
+ type: "object",
817
+ objectId,
818
+ payloadLength,
819
+ payload,
820
+ extensionData,
821
+ byteOffset: 0,
822
+ payloadByteOffset
823
+ });
824
+ offset += r.offset;
825
+ } catch (e) {
826
+ if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
827
+ break;
828
+ }
829
+ controller.error(e);
830
+ return;
831
+ }
832
+ }
833
+ },
834
+ flush(controller) {
835
+ if (offset < buffer.length) {
836
+ controller.error(new DecodeError("UNEXPECTED_END", "Stream ended with incomplete data", 0));
837
+ }
838
+ }
839
+ });
840
+ }
841
+ function createDataStreamDecoder() {
842
+ let buffer = new Uint8Array(0);
843
+ let offset = 0;
844
+ let inner = null;
845
+ return new TransformStream({
846
+ transform(chunk, controller) {
847
+ if (offset > 0) {
848
+ buffer = buffer.subarray(offset);
849
+ offset = 0;
850
+ }
851
+ const newBuffer = new Uint8Array(buffer.length + chunk.length);
852
+ newBuffer.set(buffer, 0);
853
+ newBuffer.set(chunk, buffer.length);
854
+ buffer = newBuffer;
855
+ if (inner === null) {
856
+ if (offset >= buffer.length) return;
857
+ const firstByte = buffer[offset];
858
+ if (isValidSubgroupType(firstByte)) {
859
+ const decoder = createSubgroupStreamDecoder();
860
+ inner = decoder;
861
+ } else if (firstByte === 5) {
862
+ const decoder = createFetchStreamDecoder();
863
+ inner = decoder;
864
+ } else {
865
+ controller.error(
866
+ new DecodeError(
867
+ "CONSTRAINT_VIOLATION",
868
+ `Unknown data stream type: 0x${firstByte.toString(16)}`,
869
+ 0
870
+ )
871
+ );
872
+ return;
873
+ }
874
+ }
875
+ },
876
+ flush(controller) {
877
+ if (offset >= buffer.length) return;
878
+ const view = buffer.subarray(offset);
879
+ const firstByte = view[0];
880
+ let result;
881
+ if (isValidSubgroupType(firstByte)) {
882
+ result = decodeSubgroupStream(view);
883
+ } else if (firstByte === 5) {
884
+ result = decodeFetchStream(view);
885
+ } else {
886
+ controller.error(
887
+ new DecodeError(
888
+ "CONSTRAINT_VIOLATION",
889
+ `Unknown data stream type: 0x${firstByte.toString(16)}`,
890
+ 0
891
+ )
892
+ );
893
+ return;
894
+ }
895
+ if (!result.ok) {
896
+ controller.error(result.error);
897
+ return;
898
+ }
899
+ const stream = result.value;
900
+ if (stream.type === "subgroup") {
901
+ controller.enqueue({
902
+ type: "subgroup_header",
903
+ trackAlias: stream.trackAlias,
904
+ groupId: stream.groupId,
905
+ subgroupId: stream.subgroupId,
906
+ publisherPriority: stream.publisherPriority
907
+ });
908
+ for (const obj of stream.objects) {
909
+ controller.enqueue(obj);
910
+ }
911
+ } else if (stream.type === "fetch") {
912
+ controller.enqueue({
913
+ type: "fetch_header",
914
+ requestId: stream.requestId
915
+ });
916
+ for (const obj of stream.objects) {
917
+ controller.enqueue(obj);
918
+ }
919
+ }
920
+ }
921
+ });
922
+ }
923
+
924
+ // src/drafts/draft19/codec.ts
925
+ var textEncoder = /* @__PURE__ */ new TextEncoder();
926
+ var textDecoder = /* @__PURE__ */ new TextDecoder();
927
+ var REQUEST_ERROR_REDIRECT = 0x34n;
928
+ function encodeSetupOptions(opts, writer) {
929
+ const entries = [];
930
+ if (opts.path !== void 0) {
931
+ entries.push({
932
+ type: SETUP_OPT_PATH,
933
+ encode: (w) => {
934
+ const encoded = textEncoder.encode(opts.path);
935
+ w.writeVarInt(BigInt(encoded.byteLength));
936
+ w.writeBytes(encoded);
937
+ }
938
+ });
939
+ }
940
+ if (opts.authorization_token !== void 0) {
941
+ entries.push({
942
+ type: SETUP_OPT_AUTHORIZATION_TOKEN,
943
+ encode: (w) => {
944
+ const tmpW = new BufferWriter(64);
945
+ encodeAuthorizationToken(opts.authorization_token, tmpW);
946
+ const raw = tmpW.finish();
947
+ w.writeVarInt(BigInt(raw.byteLength));
948
+ w.writeBytes(raw);
949
+ }
950
+ });
951
+ }
952
+ if (opts.max_auth_token_cache_size !== void 0) {
953
+ entries.push({
954
+ type: SETUP_OPT_MAX_AUTH_TOKEN_CACHE_SIZE,
955
+ encode: (w) => w.writeVarInt(opts.max_auth_token_cache_size)
956
+ });
957
+ }
958
+ if (opts.authority !== void 0) {
959
+ entries.push({
960
+ type: SETUP_OPT_AUTHORITY,
961
+ encode: (w) => {
962
+ const encoded = textEncoder.encode(opts.authority);
963
+ w.writeVarInt(BigInt(encoded.byteLength));
964
+ w.writeBytes(encoded);
965
+ }
966
+ });
967
+ }
968
+ if (opts.max_filter_ranges !== void 0) {
969
+ entries.push({
970
+ type: SETUP_OPT_MAX_FILTER_RANGES,
971
+ encode: (w) => w.writeVarInt(opts.max_filter_ranges)
972
+ });
973
+ }
974
+ if (opts.moqt_implementation !== void 0) {
975
+ entries.push({
976
+ type: SETUP_OPT_MOQT_IMPLEMENTATION,
977
+ encode: (w) => {
978
+ const encoded = textEncoder.encode(opts.moqt_implementation);
979
+ w.writeVarInt(BigInt(encoded.byteLength));
980
+ w.writeBytes(encoded);
981
+ }
982
+ });
983
+ }
984
+ if (opts.max_request_updates !== void 0) {
985
+ entries.push({
986
+ type: SETUP_OPT_MAX_REQUEST_UPDATES,
987
+ encode: (w) => w.writeVarInt(opts.max_request_updates)
988
+ });
989
+ }
990
+ if (opts.unknown) {
991
+ for (const u of opts.unknown) {
992
+ const id = BigInt(u.id);
993
+ entries.push({
994
+ type: id,
995
+ encode: (w) => {
996
+ if (id % 2n === 0n) {
997
+ const raw = hexToBytes(u.raw_hex);
998
+ const tmpReader = new BufferReader(raw);
999
+ w.writeVarInt(tmpReader.readVarInt());
1000
+ } else {
1001
+ const raw = hexToBytes(u.raw_hex);
1002
+ w.writeVarInt(BigInt(raw.byteLength));
1003
+ w.writeBytes(raw);
1004
+ }
1005
+ }
1006
+ });
1007
+ }
1008
+ }
1009
+ entries.sort((a, b) => a.type < b.type ? -1 : a.type > b.type ? 1 : 0);
1010
+ let prevType = 0n;
1011
+ for (const entry of entries) {
1012
+ writer.writeVarInt(entry.type - prevType);
1013
+ entry.encode(writer);
1014
+ prevType = entry.type;
1015
+ }
1016
+ }
1017
+ function decodeSetupOptions(reader, payloadEnd) {
1018
+ const result = {};
1019
+ const unknown = [];
1020
+ let prevType = 0n;
1021
+ while (reader.offset < payloadEnd) {
1022
+ const delta = reader.readVarInt();
1023
+ const optType = prevType + delta;
1024
+ prevType = optType;
1025
+ if (optType % 2n === 0n) {
1026
+ const value = reader.readVarInt();
1027
+ if (optType === SETUP_OPT_MAX_AUTH_TOKEN_CACHE_SIZE) {
1028
+ result.max_auth_token_cache_size = value;
1029
+ } else if (optType === SETUP_OPT_MAX_FILTER_RANGES) {
1030
+ result.max_filter_ranges = value;
1031
+ } else if (optType === SETUP_OPT_MAX_REQUEST_UPDATES) {
1032
+ result.max_request_updates = value;
1033
+ } else {
1034
+ const tmpWriter = new BufferWriter(16);
1035
+ tmpWriter.writeVarInt(value);
1036
+ const raw = tmpWriter.finish();
1037
+ unknown.push({
1038
+ id: `0x${optType.toString(16)}`,
1039
+ length: raw.byteLength,
1040
+ raw_hex: bytesToHex(raw)
1041
+ });
1042
+ }
1043
+ } else {
1044
+ const length = Number(reader.readVarInt());
1045
+ const bytes = reader.readBytes(length);
1046
+ if (optType === SETUP_OPT_PATH) {
1047
+ result.path = textDecoder.decode(bytes);
1048
+ } else if (optType === SETUP_OPT_AUTHORIZATION_TOKEN) {
1049
+ result.authorization_token = decodeAuthorizationToken(new BufferReader(bytes));
1050
+ } else if (optType === SETUP_OPT_AUTHORITY) {
1051
+ result.authority = textDecoder.decode(bytes);
1052
+ } else if (optType === SETUP_OPT_MOQT_IMPLEMENTATION) {
1053
+ result.moqt_implementation = textDecoder.decode(bytes);
1054
+ } else {
1055
+ unknown.push({
1056
+ id: `0x${optType.toString(16)}`,
1057
+ length,
1058
+ raw_hex: bytesToHex(bytes)
1059
+ });
1060
+ }
1061
+ }
1062
+ }
1063
+ if (unknown.length > 0) result.unknown = unknown;
1064
+ return result;
1065
+ }
1066
+ function encodeAuthorizationToken(token, w) {
1067
+ const aliasType = Number(token.alias_type);
1068
+ w.writeVarInt(token.alias_type);
1069
+ if (aliasType === 0 || aliasType === 1 || aliasType === 2) {
1070
+ w.writeVarInt(token.token_alias ?? 0n);
1071
+ }
1072
+ if (aliasType === 1 || aliasType === 3) {
1073
+ w.writeVarInt(token.token_type ?? 0n);
1074
+ const tv = token.token_value ?? new Uint8Array(0);
1075
+ w.writeBytes(tv);
1076
+ }
1077
+ }
1078
+ function decodeAuthorizationToken(r) {
1079
+ const alias_type = r.readVarInt();
1080
+ const aliasType = Number(alias_type);
1081
+ const result = { alias_type };
1082
+ if (aliasType === 0 || aliasType === 1 || aliasType === 2) {
1083
+ ;
1084
+ result.token_alias = r.readVarInt();
1085
+ }
1086
+ if (aliasType === 1 || aliasType === 3) {
1087
+ ;
1088
+ result.token_type = r.readVarInt();
1089
+ const bytes = r.readBytes(r.remaining);
1090
+ result.token_value = bytes;
1091
+ }
1092
+ return result;
1093
+ }
1094
+ function encodeNamespaceTuple(ns, w) {
1095
+ w.writeVarInt(BigInt(ns.length));
1096
+ for (const part of ns) {
1097
+ const encoded = textEncoder.encode(part);
1098
+ w.writeVarInt(BigInt(encoded.byteLength));
1099
+ w.writeBytes(encoded);
1100
+ }
1101
+ }
1102
+ function decodeNamespaceTuple(r) {
1103
+ const count = Number(r.readVarInt());
1104
+ const parts = [];
1105
+ for (let i = 0; i < count; i++) {
1106
+ const len = Number(r.readVarInt());
1107
+ parts.push(textDecoder.decode(r.readBytes(len)));
1108
+ }
1109
+ return parts;
1110
+ }
1111
+ var PARAM_OBJECT_DELIVERY_TIMEOUT = 0x02n;
1112
+ var PARAM_AUTHORIZATION_TOKEN = 0x03n;
1113
+ var PARAM_RENDEZVOUS_TIMEOUT = 0x04n;
1114
+ var PARAM_SUBGROUP_DELIVERY_TIMEOUT = 0x06n;
1115
+ var PARAM_EXPIRES = 0x08n;
1116
+ var PARAM_LARGEST_OBJECT = 0x09n;
1117
+ var PARAM_FILL_TIMEOUT = 0x0an;
1118
+ var PARAM_FORWARD = 0x10n;
1119
+ var PARAM_SUBSCRIBER_PRIORITY = 0x20n;
1120
+ var PARAM_LOCATION_FILTER = 0x21n;
1121
+ var PARAM_GROUP_ORDER = 0x22n;
1122
+ var PARAM_SUBGROUP_FILTER = 0x25n;
1123
+ var PARAM_OBJECTID_FILTER = 0x26n;
1124
+ var PARAM_PRIORITY_FILTER = 0x27n;
1125
+ var PARAM_OBJECT_PROPERTY_FILTER = 0x28n;
1126
+ var PARAM_TRACK_PROPERTY_FILTER = 0x29n;
1127
+ var PARAM_NEW_GROUP_REQUEST = 0x32n;
1128
+ var PARAM_TRACK_NAMESPACE_PREFIX = 0x34n;
1129
+ function encodeRangeFilter(filter, hasProperty, w) {
1130
+ if (filter.removed) {
1131
+ w.writeVarInt(0n);
1132
+ return;
1133
+ }
1134
+ const tmpW = new BufferWriter(32);
1135
+ tmpW.writeUint8(Number(filter.set_id ?? 0n));
1136
+ if (hasProperty) {
1137
+ tmpW.writeVarInt(filter.property_type ?? 0n);
1138
+ }
1139
+ let prevEnd = 0n;
1140
+ for (const range of filter.ranges ?? []) {
1141
+ tmpW.writeVarInt(range.start - prevEnd);
1142
+ if (range.end !== void 0) {
1143
+ tmpW.writeVarInt(range.end - range.start);
1144
+ prevEnd = range.end;
1145
+ }
1146
+ }
1147
+ const raw = tmpW.finish();
1148
+ w.writeVarInt(BigInt(raw.byteLength));
1149
+ w.writeBytes(raw);
1150
+ }
1151
+ function decodeRangeFilter(reader, hasProperty) {
1152
+ const length = Number(reader.readVarInt());
1153
+ if (length === 0) {
1154
+ return { removed: true };
1155
+ }
1156
+ const end = reader.offset + length;
1157
+ const set_id = BigInt(reader.readUint8());
1158
+ let property_type;
1159
+ if (hasProperty) {
1160
+ property_type = reader.readVarInt();
1161
+ }
1162
+ const ranges = [];
1163
+ let prevEnd = 0n;
1164
+ while (reader.offset < end) {
1165
+ const start = reader.readVarInt() + prevEnd;
1166
+ if (reader.offset < end) {
1167
+ const rangeEnd = reader.readVarInt() + start;
1168
+ ranges.push({ start, end: rangeEnd });
1169
+ prevEnd = rangeEnd;
1170
+ } else {
1171
+ ranges.push({ start });
1172
+ }
1173
+ }
1174
+ return property_type !== void 0 ? { set_id, property_type, ranges } : { set_id, ranges };
1175
+ }
1176
+ function encodeParams(params, writer) {
1177
+ const entries = [];
1178
+ if (params.object_delivery_timeout !== void 0) {
1179
+ entries.push({
1180
+ type: PARAM_OBJECT_DELIVERY_TIMEOUT,
1181
+ encode: (w) => w.writeVarInt(params.object_delivery_timeout)
1182
+ });
1183
+ }
1184
+ if (params.authorization_token !== void 0) {
1185
+ entries.push({
1186
+ type: PARAM_AUTHORIZATION_TOKEN,
1187
+ encode: (w) => {
1188
+ const tmpW = new BufferWriter(64);
1189
+ encodeAuthorizationToken(params.authorization_token, tmpW);
1190
+ const raw = tmpW.finish();
1191
+ w.writeVarInt(BigInt(raw.byteLength));
1192
+ w.writeBytes(raw);
1193
+ }
1194
+ });
1195
+ }
1196
+ if (params.rendezvous_timeout !== void 0) {
1197
+ entries.push({
1198
+ type: PARAM_RENDEZVOUS_TIMEOUT,
1199
+ encode: (w) => w.writeVarInt(params.rendezvous_timeout)
1200
+ });
1201
+ }
1202
+ if (params.subgroup_delivery_timeout !== void 0) {
1203
+ entries.push({
1204
+ type: PARAM_SUBGROUP_DELIVERY_TIMEOUT,
1205
+ encode: (w) => w.writeVarInt(params.subgroup_delivery_timeout)
1206
+ });
1207
+ }
1208
+ if (params.expires !== void 0) {
1209
+ entries.push({
1210
+ type: PARAM_EXPIRES,
1211
+ encode: (w) => w.writeVarInt(params.expires)
1212
+ });
1213
+ }
1214
+ if (params.largest_object !== void 0) {
1215
+ entries.push({
1216
+ type: PARAM_LARGEST_OBJECT,
1217
+ encode: (w) => {
1218
+ const tmpW = new BufferWriter(16);
1219
+ tmpW.writeVarInt(params.largest_object.group);
1220
+ tmpW.writeVarInt(params.largest_object.object);
1221
+ const raw = tmpW.finish();
1222
+ w.writeVarInt(BigInt(raw.byteLength));
1223
+ w.writeBytes(raw);
1224
+ }
1225
+ });
1226
+ }
1227
+ if (params.fill_timeout !== void 0) {
1228
+ entries.push({
1229
+ type: PARAM_FILL_TIMEOUT,
1230
+ encode: (w) => w.writeVarInt(params.fill_timeout)
1231
+ });
1232
+ }
1233
+ if (params.forward !== void 0) {
1234
+ entries.push({
1235
+ type: PARAM_FORWARD,
1236
+ encode: (w) => w.writeUint8(Number(params.forward))
1237
+ });
1238
+ }
1239
+ if (params.subscriber_priority !== void 0) {
1240
+ entries.push({
1241
+ type: PARAM_SUBSCRIBER_PRIORITY,
1242
+ encode: (w) => w.writeUint8(Number(params.subscriber_priority))
1243
+ });
1244
+ }
1245
+ if (params.location_filter !== void 0) {
1246
+ entries.push({
1247
+ type: PARAM_LOCATION_FILTER,
1248
+ encode: (w) => {
1249
+ const f = params.location_filter;
1250
+ const tmpW = new BufferWriter(32);
1251
+ tmpW.writeVarInt(f.filter_type);
1252
+ if (f.filter_type === 3n || f.filter_type === 4n) {
1253
+ tmpW.writeVarInt(f.start_group);
1254
+ tmpW.writeVarInt(f.start_object);
1255
+ }
1256
+ if (f.filter_type === 4n) {
1257
+ tmpW.writeVarInt(f.end_group);
1258
+ }
1259
+ const raw = tmpW.finish();
1260
+ w.writeVarInt(BigInt(raw.byteLength));
1261
+ w.writeBytes(raw);
1262
+ }
1263
+ });
1264
+ }
1265
+ if (params.group_order !== void 0) {
1266
+ entries.push({
1267
+ type: PARAM_GROUP_ORDER,
1268
+ encode: (w) => w.writeUint8(Number(params.group_order))
1269
+ });
1270
+ }
1271
+ if (params.subgroup_filter !== void 0) {
1272
+ entries.push({
1273
+ type: PARAM_SUBGROUP_FILTER,
1274
+ encode: (w) => encodeRangeFilter(params.subgroup_filter, false, w)
1275
+ });
1276
+ }
1277
+ if (params.objectid_filter !== void 0) {
1278
+ entries.push({
1279
+ type: PARAM_OBJECTID_FILTER,
1280
+ encode: (w) => encodeRangeFilter(params.objectid_filter, false, w)
1281
+ });
1282
+ }
1283
+ if (params.priority_filter !== void 0) {
1284
+ entries.push({
1285
+ type: PARAM_PRIORITY_FILTER,
1286
+ encode: (w) => encodeRangeFilter(params.priority_filter, false, w)
1287
+ });
1288
+ }
1289
+ if (params.object_property_filter !== void 0) {
1290
+ entries.push({
1291
+ type: PARAM_OBJECT_PROPERTY_FILTER,
1292
+ encode: (w) => encodeRangeFilter(params.object_property_filter, true, w)
1293
+ });
1294
+ }
1295
+ if (params.track_property_filter !== void 0) {
1296
+ entries.push({
1297
+ type: PARAM_TRACK_PROPERTY_FILTER,
1298
+ encode: (w) => encodeRangeFilter(params.track_property_filter, true, w)
1299
+ });
1300
+ }
1301
+ if (params.new_group_request !== void 0) {
1302
+ entries.push({
1303
+ type: PARAM_NEW_GROUP_REQUEST,
1304
+ encode: (w) => w.writeVarInt(params.new_group_request)
1305
+ });
1306
+ }
1307
+ if (params.track_namespace_prefix !== void 0) {
1308
+ entries.push({
1309
+ type: PARAM_TRACK_NAMESPACE_PREFIX,
1310
+ encode: (w) => {
1311
+ const tmpW = new BufferWriter(32);
1312
+ encodeNamespaceTuple(params.track_namespace_prefix, tmpW);
1313
+ const raw = tmpW.finish();
1314
+ w.writeVarInt(BigInt(raw.byteLength));
1315
+ w.writeBytes(raw);
1316
+ }
1317
+ });
1318
+ }
1319
+ if (params.unknown) {
1320
+ for (const u of params.unknown) {
1321
+ const id = BigInt(u.id);
1322
+ entries.push({
1323
+ type: id,
1324
+ encode: (w) => {
1325
+ const raw = hexToBytes(u.raw_hex);
1326
+ w.writeBytes(raw);
1327
+ }
1328
+ });
1329
+ }
1330
+ }
1331
+ entries.sort((a, b) => a.type < b.type ? -1 : a.type > b.type ? 1 : 0);
1332
+ writer.writeVarInt(BigInt(entries.length));
1333
+ let prevType = 0n;
1334
+ for (const entry of entries) {
1335
+ writer.writeVarInt(entry.type - prevType);
1336
+ entry.encode(writer);
1337
+ prevType = entry.type;
1338
+ }
1339
+ }
1340
+ function decodeParams(reader) {
1341
+ const count = Number(reader.readVarInt());
1342
+ const result = {};
1343
+ const unknown = [];
1344
+ let prevType = 0n;
1345
+ for (let i = 0; i < count; i++) {
1346
+ const delta = reader.readVarInt();
1347
+ const paramType = prevType + delta;
1348
+ prevType = paramType;
1349
+ if (paramType === PARAM_OBJECT_DELIVERY_TIMEOUT) {
1350
+ result.object_delivery_timeout = reader.readVarInt();
1351
+ } else if (paramType === PARAM_AUTHORIZATION_TOKEN) {
1352
+ const length = Number(reader.readVarInt());
1353
+ const bytes = reader.readBytes(length);
1354
+ result.authorization_token = decodeAuthorizationToken(new BufferReader(bytes));
1355
+ } else if (paramType === PARAM_RENDEZVOUS_TIMEOUT) {
1356
+ result.rendezvous_timeout = reader.readVarInt();
1357
+ } else if (paramType === PARAM_SUBGROUP_DELIVERY_TIMEOUT) {
1358
+ result.subgroup_delivery_timeout = reader.readVarInt();
1359
+ } else if (paramType === PARAM_EXPIRES) {
1360
+ result.expires = reader.readVarInt();
1361
+ } else if (paramType === PARAM_LARGEST_OBJECT) {
1362
+ const length = Number(reader.readVarInt());
1363
+ const startOff = reader.offset;
1364
+ const group = reader.readVarInt();
1365
+ const object = reader.readVarInt();
1366
+ const consumed = reader.offset - startOff;
1367
+ if (consumed < length) reader.readBytes(length - consumed);
1368
+ result.largest_object = { group, object };
1369
+ } else if (paramType === PARAM_FILL_TIMEOUT) {
1370
+ result.fill_timeout = reader.readVarInt();
1371
+ } else if (paramType === PARAM_FORWARD) {
1372
+ result.forward = BigInt(reader.readUint8());
1373
+ } else if (paramType === PARAM_SUBSCRIBER_PRIORITY) {
1374
+ result.subscriber_priority = BigInt(reader.readUint8());
1375
+ } else if (paramType === PARAM_LOCATION_FILTER) {
1376
+ const length = Number(reader.readVarInt());
1377
+ const startOff = reader.offset;
1378
+ const filter_type = reader.readVarInt();
1379
+ const filter = { filter_type };
1380
+ if (filter_type === 3n || filter_type === 4n) {
1381
+ filter.start_group = reader.readVarInt();
1382
+ filter.start_object = reader.readVarInt();
1383
+ }
1384
+ if (filter_type === 4n) {
1385
+ filter.end_group = reader.readVarInt();
1386
+ }
1387
+ const consumed = reader.offset - startOff;
1388
+ if (consumed < length) reader.readBytes(length - consumed);
1389
+ result.location_filter = filter;
1390
+ } else if (paramType === PARAM_GROUP_ORDER) {
1391
+ result.group_order = BigInt(reader.readUint8());
1392
+ } else if (paramType === PARAM_SUBGROUP_FILTER) {
1393
+ result.subgroup_filter = decodeRangeFilter(reader, false);
1394
+ } else if (paramType === PARAM_OBJECTID_FILTER) {
1395
+ result.objectid_filter = decodeRangeFilter(reader, false);
1396
+ } else if (paramType === PARAM_PRIORITY_FILTER) {
1397
+ result.priority_filter = decodeRangeFilter(reader, false);
1398
+ } else if (paramType === PARAM_OBJECT_PROPERTY_FILTER) {
1399
+ result.object_property_filter = decodeRangeFilter(reader, true);
1400
+ } else if (paramType === PARAM_TRACK_PROPERTY_FILTER) {
1401
+ result.track_property_filter = decodeRangeFilter(reader, true);
1402
+ } else if (paramType === PARAM_NEW_GROUP_REQUEST) {
1403
+ result.new_group_request = reader.readVarInt();
1404
+ } else if (paramType === PARAM_TRACK_NAMESPACE_PREFIX) {
1405
+ const length = Number(reader.readVarInt());
1406
+ const bytes = reader.readBytes(length);
1407
+ result.track_namespace_prefix = decodeNamespaceTuple(new BufferReader(bytes));
1408
+ } else {
1409
+ if (paramType % 2n === 0n) {
1410
+ const value = reader.readVarInt();
1411
+ const tmpWriter = new BufferWriter(16);
1412
+ tmpWriter.writeVarInt(value);
1413
+ const raw = tmpWriter.finish();
1414
+ unknown.push({
1415
+ id: `0x${paramType.toString(16)}`,
1416
+ length: raw.byteLength,
1417
+ raw_hex: bytesToHex(raw)
1418
+ });
1419
+ } else {
1420
+ const length = Number(reader.readVarInt());
1421
+ const bytes = reader.readBytes(length);
1422
+ unknown.push({
1423
+ id: `0x${paramType.toString(16)}`,
1424
+ length,
1425
+ raw_hex: bytesToHex(bytes)
1426
+ });
1427
+ }
1428
+ }
1429
+ }
1430
+ if (unknown.length > 0) result.unknown = unknown;
1431
+ return result;
1432
+ }
1433
+ var TPROP_OBJECT_DELIVERY_TIMEOUT = 0x02n;
1434
+ var TPROP_MAX_CACHE_DURATION = 0x04n;
1435
+ var TPROP_SUBGROUP_DELIVERY_TIMEOUT = 0x06n;
1436
+ var TPROP_IMMUTABLE_PROPERTIES = 0x0bn;
1437
+ var TPROP_DEFAULT_PUBLISHER_PRIORITY = 0x0en;
1438
+ var TPROP_DEFAULT_PUBLISHER_GROUP_ORDER = 0x22n;
1439
+ var TPROP_DYNAMIC_GROUPS = 0x30n;
1440
+ function encodeTrackProperties(props, writer) {
1441
+ const entries = [];
1442
+ if (props.object_delivery_timeout !== void 0) {
1443
+ entries.push({
1444
+ type: TPROP_OBJECT_DELIVERY_TIMEOUT,
1445
+ encode: (w) => w.writeVarInt(props.object_delivery_timeout)
1446
+ });
1447
+ }
1448
+ if (props.max_cache_duration !== void 0) {
1449
+ entries.push({
1450
+ type: TPROP_MAX_CACHE_DURATION,
1451
+ encode: (w) => w.writeVarInt(props.max_cache_duration)
1452
+ });
1453
+ }
1454
+ if (props.subgroup_delivery_timeout !== void 0) {
1455
+ entries.push({
1456
+ type: TPROP_SUBGROUP_DELIVERY_TIMEOUT,
1457
+ encode: (w) => w.writeVarInt(props.subgroup_delivery_timeout)
1458
+ });
1459
+ }
1460
+ if (props.immutable_properties !== void 0) {
1461
+ entries.push({
1462
+ type: TPROP_IMMUTABLE_PROPERTIES,
1463
+ encode: (w) => {
1464
+ const raw = props.immutable_properties;
1465
+ w.writeVarInt(BigInt(raw.byteLength));
1466
+ w.writeBytes(raw);
1467
+ }
1468
+ });
1469
+ }
1470
+ if (props.default_publisher_priority !== void 0) {
1471
+ entries.push({
1472
+ type: TPROP_DEFAULT_PUBLISHER_PRIORITY,
1473
+ encode: (w) => w.writeVarInt(props.default_publisher_priority)
1474
+ });
1475
+ }
1476
+ if (props.default_publisher_group_order !== void 0) {
1477
+ entries.push({
1478
+ type: TPROP_DEFAULT_PUBLISHER_GROUP_ORDER,
1479
+ encode: (w) => w.writeVarInt(props.default_publisher_group_order)
1480
+ });
1481
+ }
1482
+ if (props.dynamic_groups !== void 0) {
1483
+ entries.push({
1484
+ type: TPROP_DYNAMIC_GROUPS,
1485
+ encode: (w) => w.writeVarInt(props.dynamic_groups)
1486
+ });
1487
+ }
1488
+ if (props.unknown) {
1489
+ for (const u of props.unknown) {
1490
+ const id = BigInt(u.id);
1491
+ entries.push({
1492
+ type: id,
1493
+ encode: (w) => {
1494
+ if (id % 2n === 0n) {
1495
+ const raw = hexToBytes(u.raw_hex);
1496
+ const tmpReader = new BufferReader(raw);
1497
+ w.writeVarInt(tmpReader.readVarInt());
1498
+ } else {
1499
+ const raw = hexToBytes(u.raw_hex);
1500
+ w.writeVarInt(BigInt(raw.byteLength));
1501
+ w.writeBytes(raw);
1502
+ }
1503
+ }
1504
+ });
1505
+ }
1506
+ }
1507
+ entries.sort((a, b) => a.type < b.type ? -1 : a.type > b.type ? 1 : 0);
1508
+ let prevType = 0n;
1509
+ for (const entry of entries) {
1510
+ writer.writeVarInt(entry.type - prevType);
1511
+ entry.encode(writer);
1512
+ prevType = entry.type;
1513
+ }
1514
+ }
1515
+ function decodeTrackProperties(reader, payloadEnd) {
1516
+ const result = {};
1517
+ const unknown = [];
1518
+ let prevType = 0n;
1519
+ while (reader.offset < payloadEnd) {
1520
+ const delta = reader.readVarInt();
1521
+ const propType = prevType + delta;
1522
+ prevType = propType;
1523
+ if (propType % 2n === 0n) {
1524
+ const value = reader.readVarInt();
1525
+ if (propType === TPROP_OBJECT_DELIVERY_TIMEOUT) {
1526
+ result.object_delivery_timeout = value;
1527
+ } else if (propType === TPROP_MAX_CACHE_DURATION) {
1528
+ result.max_cache_duration = value;
1529
+ } else if (propType === TPROP_SUBGROUP_DELIVERY_TIMEOUT) {
1530
+ result.subgroup_delivery_timeout = value;
1531
+ } else if (propType === TPROP_DEFAULT_PUBLISHER_PRIORITY) {
1532
+ result.default_publisher_priority = value;
1533
+ } else if (propType === TPROP_DEFAULT_PUBLISHER_GROUP_ORDER) {
1534
+ result.default_publisher_group_order = value;
1535
+ } else if (propType === TPROP_DYNAMIC_GROUPS) {
1536
+ result.dynamic_groups = value;
1537
+ } else {
1538
+ const tmpWriter = new BufferWriter(16);
1539
+ tmpWriter.writeVarInt(value);
1540
+ const raw = tmpWriter.finish();
1541
+ unknown.push({
1542
+ id: `0x${propType.toString(16)}`,
1543
+ length: raw.byteLength,
1544
+ raw_hex: bytesToHex(raw)
1545
+ });
1546
+ }
1547
+ } else {
1548
+ const length = Number(reader.readVarInt());
1549
+ const bytes = reader.readBytes(length);
1550
+ if (propType === TPROP_IMMUTABLE_PROPERTIES) {
1551
+ result.immutable_properties = bytes;
1552
+ } else {
1553
+ unknown.push({
1554
+ id: `0x${propType.toString(16)}`,
1555
+ length,
1556
+ raw_hex: bytesToHex(bytes)
1557
+ });
1558
+ }
1559
+ }
1560
+ }
1561
+ if (unknown.length > 0) result.unknown = unknown;
1562
+ return result;
1563
+ }
1564
+ function encodeRedirect(redirect, w) {
1565
+ const uri = textEncoder.encode(redirect.connect_uri);
1566
+ w.writeVarInt(BigInt(uri.byteLength));
1567
+ w.writeBytes(uri);
1568
+ w.writeTuple(redirect.track_namespace);
1569
+ w.writeString(redirect.track_name);
1570
+ }
1571
+ function decodeRedirect(r) {
1572
+ const uriLen = Number(r.readVarInt());
1573
+ const uriBytes = r.readBytes(uriLen);
1574
+ const connect_uri = textDecoder.decode(uriBytes);
1575
+ const track_namespace = r.readTuple();
1576
+ const track_name = r.readString();
1577
+ return { connect_uri, track_namespace, track_name };
1578
+ }
1579
+ function encodeSetupPayload(msg, w) {
1580
+ encodeSetupOptions(msg.options, w);
1581
+ }
1582
+ function encodeSubscribePayload(msg, w) {
1583
+ w.writeVarInt(msg.request_id);
1584
+ w.writeTuple(msg.track_namespace);
1585
+ w.writeString(msg.track_name);
1586
+ encodeParams(msg.parameters, w);
1587
+ }
1588
+ function encodeSubscribeOkPayload(msg, w) {
1589
+ w.writeVarInt(msg.track_alias);
1590
+ encodeParams(msg.parameters, w);
1591
+ encodeTrackProperties(msg.track_properties, w);
1592
+ }
1593
+ function encodeRequestUpdatePayload(msg, w) {
1594
+ w.writeVarInt(msg.request_id);
1595
+ encodeParams(msg.parameters, w);
1596
+ }
1597
+ function encodePublishPayload(msg, w) {
1598
+ w.writeVarInt(msg.request_id);
1599
+ w.writeTuple(msg.track_namespace);
1600
+ w.writeString(msg.track_name);
1601
+ w.writeVarInt(msg.track_alias);
1602
+ encodeParams(msg.parameters, w);
1603
+ encodeTrackProperties(msg.track_properties, w);
1604
+ }
1605
+ function encodePublishDonePayload(msg, w) {
1606
+ w.writeVarInt(msg.status_code);
1607
+ w.writeVarInt(msg.stream_count);
1608
+ w.writeString(msg.reason_phrase);
1609
+ }
1610
+ function encodePublishNamespacePayload(msg, w) {
1611
+ w.writeVarInt(msg.request_id);
1612
+ w.writeTuple(msg.track_namespace);
1613
+ encodeParams(msg.parameters, w);
1614
+ }
1615
+ function encodeNamespacePayload(msg, w) {
1616
+ w.writeTuple(msg.namespace_suffix);
1617
+ }
1618
+ function encodeNamespaceDonePayload(msg, w) {
1619
+ w.writeTuple(msg.namespace_suffix);
1620
+ }
1621
+ function encodeSubscribeNamespacePayload(msg, w) {
1622
+ w.writeVarInt(msg.request_id);
1623
+ w.writeTuple(msg.namespace_prefix);
1624
+ encodeParams(msg.parameters, w);
1625
+ }
1626
+ function encodeSubscribeTracksPayload(msg, w) {
1627
+ w.writeVarInt(msg.request_id);
1628
+ w.writeTuple(msg.namespace_prefix);
1629
+ encodeParams(msg.parameters, w);
1630
+ }
1631
+ function encodePublishSkippedPayload(msg, w) {
1632
+ w.writeTuple(msg.namespace_suffix);
1633
+ w.writeString(msg.track_name);
1634
+ }
1635
+ function encodeFetchPayload(msg, w) {
1636
+ w.writeVarInt(msg.request_id);
1637
+ w.writeVarInt(msg.fetch_type);
1638
+ const ft = Number(msg.fetch_type);
1639
+ if (ft === 1 && msg.standalone) {
1640
+ w.writeTuple(msg.standalone.track_namespace);
1641
+ w.writeString(msg.standalone.track_name);
1642
+ w.writeVarInt(msg.standalone.start_group);
1643
+ w.writeVarInt(msg.standalone.start_object);
1644
+ w.writeVarInt(msg.standalone.end_group);
1645
+ w.writeVarInt(msg.standalone.end_object);
1646
+ } else if ((ft === 2 || ft === 3) && msg.joining) {
1647
+ w.writeVarInt(msg.joining.joining_request_id);
1648
+ w.writeVarInt(msg.joining.joining_start);
1649
+ }
1650
+ encodeParams(msg.parameters, w);
1651
+ }
1652
+ function encodeFetchOkPayload(msg, w) {
1653
+ w.writeUint8(msg.end_of_track);
1654
+ w.writeVarInt(msg.end_group);
1655
+ w.writeVarInt(msg.end_object);
1656
+ encodeParams(msg.parameters, w);
1657
+ encodeTrackProperties(msg.track_properties, w);
1658
+ }
1659
+ function encodeTrackStatusPayload(msg, w) {
1660
+ w.writeVarInt(msg.request_id);
1661
+ w.writeTuple(msg.track_namespace);
1662
+ w.writeString(msg.track_name);
1663
+ encodeParams(msg.parameters, w);
1664
+ }
1665
+ function encodeRequestOkPayload(msg, w) {
1666
+ encodeParams(msg.parameters, w);
1667
+ encodeTrackProperties(msg.track_properties, w);
1668
+ }
1669
+ function encodeRequestErrorPayload(msg, w) {
1670
+ w.writeVarInt(msg.error_code);
1671
+ w.writeVarInt(msg.retry_interval);
1672
+ w.writeString(msg.reason_phrase);
1673
+ if (msg.error_code === REQUEST_ERROR_REDIRECT && msg.redirect) {
1674
+ encodeRedirect(msg.redirect, w);
1675
+ }
1676
+ }
1677
+ function encodeGoAwayPayload(msg, w) {
1678
+ w.writeString(msg.new_session_uri);
1679
+ w.writeVarInt(msg.timeout);
1680
+ }
1681
+ function decodeSetupPayload(r, payloadEnd) {
1682
+ const options = decodeSetupOptions(r, payloadEnd);
1683
+ return { type: "setup", options };
1684
+ }
1685
+ function decodeSubscribePayload(r) {
1686
+ const request_id = r.readVarInt();
1687
+ const track_namespace = r.readTuple();
1688
+ const track_name = r.readString();
1689
+ const parameters = decodeParams(r);
1690
+ return {
1691
+ type: "subscribe",
1692
+ request_id,
1693
+ track_namespace,
1694
+ track_name,
1695
+ parameters
1696
+ };
1697
+ }
1698
+ function decodeSubscribeOkPayload(r, payloadEnd) {
1699
+ const track_alias = r.readVarInt();
1700
+ const parameters = decodeParams(r);
1701
+ const track_properties = decodeTrackProperties(r, payloadEnd);
1702
+ return { type: "subscribe_ok", track_alias, parameters, track_properties };
1703
+ }
1704
+ function decodeRequestUpdatePayload(r) {
1705
+ const request_id = r.readVarInt();
1706
+ const parameters = decodeParams(r);
1707
+ return {
1708
+ type: "request_update",
1709
+ request_id,
1710
+ parameters
1711
+ };
1712
+ }
1713
+ function decodePublishPayload(r, payloadEnd) {
1714
+ const request_id = r.readVarInt();
1715
+ const track_namespace = r.readTuple();
1716
+ const track_name = r.readString();
1717
+ const track_alias = r.readVarInt();
1718
+ const parameters = decodeParams(r);
1719
+ const track_properties = decodeTrackProperties(r, payloadEnd);
1720
+ return {
1721
+ type: "publish",
1722
+ request_id,
1723
+ track_namespace,
1724
+ track_name,
1725
+ track_alias,
1726
+ parameters,
1727
+ track_properties
1728
+ };
1729
+ }
1730
+ function decodePublishDonePayload(r) {
1731
+ const status_code = r.readVarInt();
1732
+ const stream_count = r.readVarInt();
1733
+ const reason_phrase = r.readString();
1734
+ return { type: "publish_done", status_code, stream_count, reason_phrase };
1735
+ }
1736
+ function decodePublishNamespacePayload(r) {
1737
+ const request_id = r.readVarInt();
1738
+ const track_namespace = r.readTuple();
1739
+ const parameters = decodeParams(r);
1740
+ return {
1741
+ type: "publish_namespace",
1742
+ request_id,
1743
+ track_namespace,
1744
+ parameters
1745
+ };
1746
+ }
1747
+ function decodeNamespacePayload(r) {
1748
+ const namespace_suffix = r.readTuple();
1749
+ return { type: "namespace", namespace_suffix };
1750
+ }
1751
+ function decodeNamespaceDonePayload(r) {
1752
+ const namespace_suffix = r.readTuple();
1753
+ return { type: "namespace_done", namespace_suffix };
1754
+ }
1755
+ function decodeSubscribeNamespacePayload(r) {
1756
+ const request_id = r.readVarInt();
1757
+ const namespace_prefix = r.readTuple();
1758
+ const parameters = decodeParams(r);
1759
+ return {
1760
+ type: "subscribe_namespace",
1761
+ request_id,
1762
+ namespace_prefix,
1763
+ parameters
1764
+ };
1765
+ }
1766
+ function decodeSubscribeTracksPayload(r) {
1767
+ const request_id = r.readVarInt();
1768
+ const namespace_prefix = r.readTuple();
1769
+ const parameters = decodeParams(r);
1770
+ return {
1771
+ type: "subscribe_tracks",
1772
+ request_id,
1773
+ namespace_prefix,
1774
+ parameters
1775
+ };
1776
+ }
1777
+ function decodePublishSkippedPayload(r) {
1778
+ const namespace_suffix = r.readTuple();
1779
+ const track_name = r.readString();
1780
+ return { type: "publish_skipped", namespace_suffix, track_name };
1781
+ }
1782
+ function decodeFetchPayload(r) {
1783
+ const request_id = r.readVarInt();
1784
+ const fetch_type = r.readVarInt();
1785
+ const ft = Number(fetch_type);
1786
+ if (ft < 1 || ft > 3) {
1787
+ throw new DecodeError("CONSTRAINT_VIOLATION", `Invalid fetch_type: ${ft}`, r.offset);
1788
+ }
1789
+ let standalone;
1790
+ let joining;
1791
+ if (ft === 1) {
1792
+ const track_namespace = r.readTuple();
1793
+ const track_name = r.readString();
1794
+ const start_group = r.readVarInt();
1795
+ const start_object = r.readVarInt();
1796
+ const end_group = r.readVarInt();
1797
+ const end_object = r.readVarInt();
1798
+ standalone = {
1799
+ track_namespace,
1800
+ track_name,
1801
+ start_group,
1802
+ start_object,
1803
+ end_group,
1804
+ end_object
1805
+ };
1806
+ } else {
1807
+ const joining_request_id = r.readVarInt();
1808
+ const joining_start = r.readVarInt();
1809
+ joining = { joining_request_id, joining_start };
1810
+ }
1811
+ const parameters = decodeParams(r);
1812
+ return {
1813
+ type: "fetch",
1814
+ request_id,
1815
+ fetch_type,
1816
+ standalone,
1817
+ joining,
1818
+ parameters
1819
+ };
1820
+ }
1821
+ function decodeFetchOkPayload(r, payloadEnd) {
1822
+ const end_of_track = r.readUint8();
1823
+ const end_group = r.readVarInt();
1824
+ const end_object = r.readVarInt();
1825
+ const parameters = decodeParams(r);
1826
+ const track_properties = decodeTrackProperties(r, payloadEnd);
1827
+ return {
1828
+ type: "fetch_ok",
1829
+ end_of_track,
1830
+ end_group,
1831
+ end_object,
1832
+ parameters,
1833
+ track_properties
1834
+ };
1835
+ }
1836
+ function decodeTrackStatusPayload(r) {
1837
+ const request_id = r.readVarInt();
1838
+ const track_namespace = r.readTuple();
1839
+ const track_name = r.readString();
1840
+ const parameters = decodeParams(r);
1841
+ return {
1842
+ type: "track_status",
1843
+ request_id,
1844
+ track_namespace,
1845
+ track_name,
1846
+ parameters
1847
+ };
1848
+ }
1849
+ function decodeRequestOkPayload(r, payloadEnd) {
1850
+ const parameters = decodeParams(r);
1851
+ const track_properties = decodeTrackProperties(r, payloadEnd);
1852
+ return { type: "request_ok", parameters, track_properties };
1853
+ }
1854
+ function decodeRequestErrorPayload(r, payloadEnd) {
1855
+ const error_code = r.readVarInt();
1856
+ const retry_interval = r.readVarInt();
1857
+ const reason_phrase = r.readString();
1858
+ let redirect;
1859
+ if (error_code === REQUEST_ERROR_REDIRECT && r.offset < payloadEnd) {
1860
+ redirect = decodeRedirect(r);
1861
+ }
1862
+ if (redirect !== void 0) {
1863
+ return { type: "request_error", error_code, retry_interval, reason_phrase, redirect };
1864
+ }
1865
+ return { type: "request_error", error_code, retry_interval, reason_phrase };
1866
+ }
1867
+ function decodeGoAwayPayload(r) {
1868
+ const new_session_uri = r.readString();
1869
+ const timeout = r.readVarInt();
1870
+ return { type: "goaway", new_session_uri, timeout };
1871
+ }
1872
+ function encodeMessage(message) {
1873
+ const typeId = MESSAGE_ID_MAP.get(message.type);
1874
+ if (typeId === void 0) {
1875
+ throw new Error(`Unknown message type: ${message.type}`);
1876
+ }
1877
+ const payloadWriter = new BufferWriter();
1878
+ encodePayload(message, payloadWriter);
1879
+ const payload = payloadWriter.finishView();
1880
+ if (payload.byteLength > 65535) {
1881
+ throw new Error(`Payload too large for 16-bit length: ${payload.byteLength}`);
1882
+ }
1883
+ const writer = new BufferWriter(payload.byteLength + 16);
1884
+ writer.writeVarInt(typeId);
1885
+ writer.writeUint8(payload.byteLength >> 8 & 255);
1886
+ writer.writeUint8(payload.byteLength & 255);
1887
+ writer.writeBytes(payload);
1888
+ return writer.finish();
1889
+ }
1890
+ function encodePayload(msg, w) {
1891
+ switch (msg.type) {
1892
+ case "setup":
1893
+ return encodeSetupPayload(msg, w);
1894
+ case "subscribe":
1895
+ return encodeSubscribePayload(msg, w);
1896
+ case "subscribe_ok":
1897
+ return encodeSubscribeOkPayload(msg, w);
1898
+ case "request_update":
1899
+ return encodeRequestUpdatePayload(msg, w);
1900
+ case "publish":
1901
+ return encodePublishPayload(msg, w);
1902
+ case "publish_done":
1903
+ return encodePublishDonePayload(msg, w);
1904
+ case "publish_namespace":
1905
+ return encodePublishNamespacePayload(msg, w);
1906
+ case "namespace":
1907
+ return encodeNamespacePayload(msg, w);
1908
+ case "namespace_done":
1909
+ return encodeNamespaceDonePayload(msg, w);
1910
+ case "subscribe_namespace":
1911
+ return encodeSubscribeNamespacePayload(msg, w);
1912
+ case "subscribe_tracks":
1913
+ return encodeSubscribeTracksPayload(msg, w);
1914
+ case "publish_skipped":
1915
+ return encodePublishSkippedPayload(msg, w);
1916
+ case "fetch":
1917
+ return encodeFetchPayload(msg, w);
1918
+ case "fetch_ok":
1919
+ return encodeFetchOkPayload(msg, w);
1920
+ case "track_status":
1921
+ return encodeTrackStatusPayload(msg, w);
1922
+ case "request_ok":
1923
+ return encodeRequestOkPayload(msg, w);
1924
+ case "request_error":
1925
+ return encodeRequestErrorPayload(msg, w);
1926
+ case "goaway":
1927
+ return encodeGoAwayPayload(msg, w);
1928
+ default: {
1929
+ const _exhaustive = msg;
1930
+ throw new Error(`Unhandled message type: ${_exhaustive.type}`);
1931
+ }
1932
+ }
1933
+ }
1934
+ function decodeMessage(bytes) {
1935
+ try {
1936
+ const reader = new BufferReader(bytes);
1937
+ const typeId = reader.readVarInt();
1938
+ const lenHi = reader.readUint8();
1939
+ const lenLo = reader.readUint8();
1940
+ const payloadLength = lenHi << 8 | lenLo;
1941
+ const payloadBytes = reader.readBytes(payloadLength);
1942
+ const payloadReader = new BufferReader(payloadBytes);
1943
+ let message;
1944
+ if (typeId === MSG_SETUP) {
1945
+ message = decodeSetupPayload(payloadReader, payloadLength);
1946
+ } else if (typeId === MSG_SUBSCRIBE) {
1947
+ message = decodeSubscribePayload(payloadReader);
1948
+ } else if (typeId === MSG_SUBSCRIBE_OK) {
1949
+ message = decodeSubscribeOkPayload(payloadReader, payloadLength);
1950
+ } else if (typeId === MSG_REQUEST_UPDATE) {
1951
+ message = decodeRequestUpdatePayload(payloadReader);
1952
+ } else if (typeId === MSG_PUBLISH) {
1953
+ message = decodePublishPayload(payloadReader, payloadLength);
1954
+ } else if (typeId === MSG_PUBLISH_DONE) {
1955
+ message = decodePublishDonePayload(payloadReader);
1956
+ } else if (typeId === MSG_PUBLISH_NAMESPACE) {
1957
+ message = decodePublishNamespacePayload(payloadReader);
1958
+ } else if (typeId === MSG_NAMESPACE) {
1959
+ message = decodeNamespacePayload(payloadReader);
1960
+ } else if (typeId === MSG_NAMESPACE_DONE) {
1961
+ message = decodeNamespaceDonePayload(payloadReader);
1962
+ } else if (typeId === MSG_SUBSCRIBE_NAMESPACE) {
1963
+ message = decodeSubscribeNamespacePayload(payloadReader);
1964
+ } else if (typeId === MSG_SUBSCRIBE_TRACKS) {
1965
+ message = decodeSubscribeTracksPayload(payloadReader);
1966
+ } else if (typeId === MSG_PUBLISH_SKIPPED) {
1967
+ message = decodePublishSkippedPayload(payloadReader);
1968
+ } else if (typeId === MSG_FETCH) {
1969
+ message = decodeFetchPayload(payloadReader);
1970
+ } else if (typeId === MSG_FETCH_OK) {
1971
+ message = decodeFetchOkPayload(payloadReader, payloadLength);
1972
+ } else if (typeId === MSG_TRACK_STATUS) {
1973
+ message = decodeTrackStatusPayload(payloadReader);
1974
+ } else if (typeId === MSG_REQUEST_OK) {
1975
+ message = decodeRequestOkPayload(payloadReader, payloadLength);
1976
+ } else if (typeId === MSG_REQUEST_ERROR) {
1977
+ message = decodeRequestErrorPayload(payloadReader, payloadLength);
1978
+ } else if (typeId === MSG_GOAWAY) {
1979
+ message = decodeGoAwayPayload(payloadReader);
1980
+ } else {
1981
+ return {
1982
+ ok: false,
1983
+ error: new DecodeError(
1984
+ "UNKNOWN_MESSAGE_TYPE",
1985
+ `Unknown message type ID: 0x${typeId.toString(16)}`,
1986
+ 0
1987
+ )
1988
+ };
1989
+ }
1990
+ return { ok: true, value: message, bytesRead: reader.offset };
1991
+ } catch (e) {
1992
+ if (e instanceof DecodeError) {
1993
+ return { ok: false, error: e };
1994
+ }
1995
+ throw e;
1996
+ }
1997
+ }
1998
+ function createStreamDecoder() {
1999
+ let buffer = new Uint8Array(0);
2000
+ let offset = 0;
2001
+ return new TransformStream({
2002
+ transform(chunk, controller) {
2003
+ if (offset > 0) {
2004
+ buffer = buffer.subarray(offset);
2005
+ offset = 0;
2006
+ }
2007
+ const newBuffer = new Uint8Array(buffer.length + chunk.length);
2008
+ newBuffer.set(buffer, 0);
2009
+ newBuffer.set(chunk, buffer.length);
2010
+ buffer = newBuffer;
2011
+ while (offset < buffer.length) {
2012
+ const result = decodeMessage(buffer.subarray(offset));
2013
+ if (!result.ok) {
2014
+ if (result.error.code === "UNEXPECTED_END") {
2015
+ break;
2016
+ }
2017
+ controller.error(result.error);
2018
+ return;
2019
+ }
2020
+ controller.enqueue(result.value);
2021
+ offset += result.bytesRead;
2022
+ }
2023
+ },
2024
+ flush(controller) {
2025
+ if (offset < buffer.length) {
2026
+ controller.error(
2027
+ new DecodeError("UNEXPECTED_END", "Stream ended with incomplete message data", 0)
2028
+ );
2029
+ }
2030
+ }
2031
+ });
2032
+ }
2033
+ function createDraft19Codec() {
2034
+ return {
2035
+ draft: "18",
2036
+ encodeMessage,
2037
+ decodeMessage,
2038
+ encodeSubgroupStream,
2039
+ encodeDatagram,
2040
+ encodeFetchStream,
2041
+ decodeSubgroupStream,
2042
+ decodeDatagram,
2043
+ decodeFetchStream,
2044
+ decodeDataStream,
2045
+ createStreamDecoder,
2046
+ createSubgroupStreamDecoder,
2047
+ createFetchStreamDecoder,
2048
+ createDataStreamDecoder
2049
+ };
2050
+ }
2051
+
2052
+ export {
2053
+ MSG_REQUEST_UPDATE,
2054
+ MSG_SUBSCRIBE,
2055
+ MSG_SUBSCRIBE_OK,
2056
+ MSG_REQUEST_ERROR,
2057
+ MSG_PUBLISH_NAMESPACE,
2058
+ MSG_REQUEST_OK,
2059
+ MSG_NAMESPACE,
2060
+ MSG_PUBLISH_DONE,
2061
+ MSG_TRACK_STATUS,
2062
+ MSG_NAMESPACE_DONE,
2063
+ MSG_PUBLISH_SKIPPED,
2064
+ MSG_GOAWAY,
2065
+ MSG_FETCH,
2066
+ MSG_FETCH_OK,
2067
+ MSG_PUBLISH,
2068
+ MSG_PUBLISH_OK,
2069
+ MSG_SUBSCRIBE_NAMESPACE,
2070
+ MSG_SUBSCRIBE_TRACKS,
2071
+ MSG_SETUP,
2072
+ SETUP_OPT_PATH,
2073
+ SETUP_OPT_AUTHORIZATION_TOKEN,
2074
+ SETUP_OPT_MAX_AUTH_TOKEN_CACHE_SIZE,
2075
+ SETUP_OPT_AUTHORITY,
2076
+ SETUP_OPT_MAX_FILTER_RANGES,
2077
+ SETUP_OPT_MOQT_IMPLEMENTATION,
2078
+ SETUP_OPT_MAX_REQUEST_UPDATES,
2079
+ MESSAGE_TYPE_MAP,
2080
+ MESSAGE_ID_MAP,
2081
+ encodeSubgroupStream,
2082
+ encodeDatagram,
2083
+ encodeFetchStream,
2084
+ decodeSubgroupStream,
2085
+ decodeDatagram,
2086
+ decodeFetchStream,
2087
+ decodeDataStream,
2088
+ createSubgroupStreamDecoder,
2089
+ createFetchStreamDecoder,
2090
+ createDataStreamDecoder,
2091
+ encodeMessage,
2092
+ decodeMessage,
2093
+ createStreamDecoder,
2094
+ createDraft19Codec
2095
+ };