@moqtap/codec 0.1.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 (71) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +95 -0
  3. package/dist/chunk-23YG7F46.js +764 -0
  4. package/dist/chunk-2NARXGVA.cjs +194 -0
  5. package/dist/chunk-3BSZ55L3.cjs +307 -0
  6. package/dist/chunk-5WFXFLL4.cjs +1185 -0
  7. package/dist/chunk-DC4L6ZIT.js +307 -0
  8. package/dist/chunk-GDRGWFEK.cjs +498 -0
  9. package/dist/chunk-IQPDRQVC.js +1185 -0
  10. package/dist/chunk-QYG6KGOV.cjs +101 -0
  11. package/dist/chunk-UOBWHJA5.js +101 -0
  12. package/dist/chunk-WNTXF3DE.cjs +764 -0
  13. package/dist/chunk-YBSEOSSP.js +194 -0
  14. package/dist/chunk-YPXLV5YK.js +498 -0
  15. package/dist/codec-CTvFtQQI.d.cts +86 -0
  16. package/dist/codec-qPzfmLNu.d.ts +86 -0
  17. package/dist/draft14-session.cjs +6 -0
  18. package/dist/draft14-session.d.cts +8 -0
  19. package/dist/draft14-session.d.ts +8 -0
  20. package/dist/draft14-session.js +6 -0
  21. package/dist/draft14.cjs +121 -0
  22. package/dist/draft14.d.cts +96 -0
  23. package/dist/draft14.d.ts +96 -0
  24. package/dist/draft14.js +121 -0
  25. package/dist/draft7-session.cjs +7 -0
  26. package/dist/draft7-session.d.cts +7 -0
  27. package/dist/draft7-session.d.ts +7 -0
  28. package/dist/draft7-session.js +7 -0
  29. package/dist/draft7.cjs +60 -0
  30. package/dist/draft7.d.cts +72 -0
  31. package/dist/draft7.d.ts +72 -0
  32. package/dist/draft7.js +60 -0
  33. package/dist/index.cjs +40 -0
  34. package/dist/index.d.cts +40 -0
  35. package/dist/index.d.ts +40 -0
  36. package/dist/index.js +40 -0
  37. package/dist/session-types-B9NIf7_F.d.ts +101 -0
  38. package/dist/session-types-CCo-oA-d.d.cts +101 -0
  39. package/dist/session.cjs +27 -0
  40. package/dist/session.d.cts +24 -0
  41. package/dist/session.d.ts +24 -0
  42. package/dist/session.js +27 -0
  43. package/dist/types-CIk5W10V.d.cts +249 -0
  44. package/dist/types-CIk5W10V.d.ts +249 -0
  45. package/dist/types-ClXELFGN.d.cts +241 -0
  46. package/dist/types-ClXELFGN.d.ts +241 -0
  47. package/package.json +84 -0
  48. package/src/core/buffer-reader.ts +107 -0
  49. package/src/core/buffer-writer.ts +91 -0
  50. package/src/core/errors.ts +1 -0
  51. package/src/core/session-types.ts +103 -0
  52. package/src/core/types.ts +363 -0
  53. package/src/drafts/draft07/announce-fsm.ts +2 -0
  54. package/src/drafts/draft07/codec.ts +874 -0
  55. package/src/drafts/draft07/index.ts +70 -0
  56. package/src/drafts/draft07/messages.ts +44 -0
  57. package/src/drafts/draft07/parameters.ts +12 -0
  58. package/src/drafts/draft07/rules.ts +75 -0
  59. package/src/drafts/draft07/session-fsm.ts +353 -0
  60. package/src/drafts/draft07/session.ts +21 -0
  61. package/src/drafts/draft07/subscription-fsm.ts +3 -0
  62. package/src/drafts/draft07/varint.ts +23 -0
  63. package/src/drafts/draft14/codec.ts +1330 -0
  64. package/src/drafts/draft14/index.ts +132 -0
  65. package/src/drafts/draft14/messages.ts +76 -0
  66. package/src/drafts/draft14/rules.ts +70 -0
  67. package/src/drafts/draft14/session-fsm.ts +480 -0
  68. package/src/drafts/draft14/session.ts +26 -0
  69. package/src/drafts/draft14/types.ts +365 -0
  70. package/src/index.ts +85 -0
  71. package/src/session.ts +58 -0
@@ -0,0 +1,1185 @@
1
+ import {
2
+ BufferReader,
3
+ BufferWriter,
4
+ DecodeError
5
+ } from "./chunk-YBSEOSSP.js";
6
+
7
+ // src/drafts/draft14/messages.ts
8
+ var MSG_SUBSCRIBE_UPDATE = 0x02n;
9
+ var MSG_SUBSCRIBE = 0x03n;
10
+ var MSG_SUBSCRIBE_OK = 0x04n;
11
+ var MSG_SUBSCRIBE_ERROR = 0x05n;
12
+ var MSG_PUBLISH_NAMESPACE = 0x06n;
13
+ var MSG_PUBLISH_NAMESPACE_OK = 0x07n;
14
+ var MSG_PUBLISH_NAMESPACE_ERROR = 0x08n;
15
+ var MSG_PUBLISH_NAMESPACE_DONE = 0x09n;
16
+ var MSG_UNSUBSCRIBE = 0x0An;
17
+ var MSG_PUBLISH_DONE = 0x0Bn;
18
+ var MSG_PUBLISH_NAMESPACE_CANCEL = 0x0Cn;
19
+ var MSG_TRACK_STATUS = 0x0Dn;
20
+ var MSG_TRACK_STATUS_OK = 0x0En;
21
+ var MSG_TRACK_STATUS_ERROR = 0x0Fn;
22
+ var MSG_GOAWAY = 0x10n;
23
+ var MSG_SUBSCRIBE_NAMESPACE = 0x11n;
24
+ var MSG_SUBSCRIBE_NAMESPACE_OK = 0x12n;
25
+ var MSG_SUBSCRIBE_NAMESPACE_ERROR = 0x13n;
26
+ var MSG_UNSUBSCRIBE_NAMESPACE = 0x14n;
27
+ var MSG_MAX_REQUEST_ID = 0x15n;
28
+ var MSG_FETCH = 0x16n;
29
+ var MSG_FETCH_CANCEL = 0x17n;
30
+ var MSG_FETCH_OK = 0x18n;
31
+ var MSG_FETCH_ERROR = 0x19n;
32
+ var MSG_REQUESTS_BLOCKED = 0x1An;
33
+ var MSG_PUBLISH = 0x1Dn;
34
+ var MSG_PUBLISH_OK = 0x1En;
35
+ var MSG_PUBLISH_ERROR = 0x1Fn;
36
+ var MSG_CLIENT_SETUP = 0x20n;
37
+ var MSG_SERVER_SETUP = 0x21n;
38
+ var PARAM_ROLE = 0x00n;
39
+ var PARAM_PATH = 0x01n;
40
+ var PARAM_MAX_REQUEST_ID = 0x02n;
41
+ var MESSAGE_TYPE_MAP = /* @__PURE__ */ new Map([
42
+ [MSG_CLIENT_SETUP, "client_setup"],
43
+ [MSG_SERVER_SETUP, "server_setup"],
44
+ [MSG_SUBSCRIBE, "subscribe"],
45
+ [MSG_SUBSCRIBE_OK, "subscribe_ok"],
46
+ [MSG_SUBSCRIBE_UPDATE, "subscribe_update"],
47
+ [MSG_SUBSCRIBE_ERROR, "subscribe_error"],
48
+ [MSG_UNSUBSCRIBE, "unsubscribe"],
49
+ [MSG_PUBLISH, "publish"],
50
+ [MSG_PUBLISH_OK, "publish_ok"],
51
+ [MSG_PUBLISH_ERROR, "publish_error"],
52
+ [MSG_PUBLISH_DONE, "publish_done"],
53
+ [MSG_PUBLISH_NAMESPACE, "publish_namespace"],
54
+ [MSG_PUBLISH_NAMESPACE_OK, "publish_namespace_ok"],
55
+ [MSG_PUBLISH_NAMESPACE_ERROR, "publish_namespace_error"],
56
+ [MSG_PUBLISH_NAMESPACE_DONE, "publish_namespace_done"],
57
+ [MSG_PUBLISH_NAMESPACE_CANCEL, "publish_namespace_cancel"],
58
+ [MSG_SUBSCRIBE_NAMESPACE, "subscribe_namespace"],
59
+ [MSG_SUBSCRIBE_NAMESPACE_OK, "subscribe_namespace_ok"],
60
+ [MSG_SUBSCRIBE_NAMESPACE_ERROR, "subscribe_namespace_error"],
61
+ [MSG_UNSUBSCRIBE_NAMESPACE, "unsubscribe_namespace"],
62
+ [MSG_FETCH, "fetch"],
63
+ [MSG_FETCH_OK, "fetch_ok"],
64
+ [MSG_FETCH_ERROR, "fetch_error"],
65
+ [MSG_FETCH_CANCEL, "fetch_cancel"],
66
+ [MSG_TRACK_STATUS, "track_status"],
67
+ [MSG_TRACK_STATUS_OK, "track_status_ok"],
68
+ [MSG_TRACK_STATUS_ERROR, "track_status_error"],
69
+ [MSG_GOAWAY, "goaway"],
70
+ [MSG_MAX_REQUEST_ID, "max_request_id"],
71
+ [MSG_REQUESTS_BLOCKED, "requests_blocked"]
72
+ ]);
73
+ var MESSAGE_ID_MAP = new Map(
74
+ [...MESSAGE_TYPE_MAP.entries()].map(([id, name]) => [name, id])
75
+ );
76
+
77
+ // src/drafts/draft14/codec.ts
78
+ function bytesToHex(bytes) {
79
+ let hex = "";
80
+ for (let i = 0; i < bytes.byteLength; i++) {
81
+ hex += bytes[i].toString(16).padStart(2, "0");
82
+ }
83
+ return hex;
84
+ }
85
+ function hexToBytes(hex) {
86
+ const bytes = new Uint8Array(hex.length / 2);
87
+ for (let i = 0; i < hex.length; i += 2) {
88
+ bytes[i / 2] = parseInt(hex.substring(i, i + 2), 16);
89
+ }
90
+ return bytes;
91
+ }
92
+ function encodeParams(params, writer) {
93
+ let count = 0;
94
+ if (params.role !== void 0) count++;
95
+ if (params.path !== void 0) count++;
96
+ if (params.max_request_id !== void 0) count++;
97
+ if (params.unknown) count += params.unknown.length;
98
+ writer.writeVarInt(count);
99
+ if (params.role !== void 0) {
100
+ writer.writeVarInt(PARAM_ROLE);
101
+ writer.writeVarInt(params.role);
102
+ }
103
+ if (params.path !== void 0) {
104
+ writer.writeVarInt(PARAM_PATH);
105
+ const encoded = new TextEncoder().encode(params.path);
106
+ writer.writeVarInt(encoded.byteLength);
107
+ writer.writeBytes(encoded);
108
+ }
109
+ if (params.max_request_id !== void 0) {
110
+ writer.writeVarInt(PARAM_MAX_REQUEST_ID);
111
+ writer.writeVarInt(params.max_request_id);
112
+ }
113
+ if (params.unknown) {
114
+ for (const u of params.unknown) {
115
+ const id = BigInt(u.id);
116
+ writer.writeVarInt(id);
117
+ const raw = hexToBytes(u.raw_hex);
118
+ writer.writeVarInt(raw.byteLength);
119
+ writer.writeBytes(raw);
120
+ }
121
+ }
122
+ }
123
+ function decodeParams(reader) {
124
+ const count = Number(reader.readVarInt());
125
+ const result = {};
126
+ const unknown = [];
127
+ for (let i = 0; i < count; i++) {
128
+ const paramType = reader.readVarInt();
129
+ if (paramType % 2n === 0n) {
130
+ const value = reader.readVarInt();
131
+ if (paramType === PARAM_ROLE) {
132
+ result.role = value;
133
+ } else if (paramType === PARAM_MAX_REQUEST_ID) {
134
+ result.max_request_id = value;
135
+ } else {
136
+ const tmpWriter = new BufferWriter(16);
137
+ tmpWriter.writeVarInt(value);
138
+ const raw = tmpWriter.finish();
139
+ unknown.push({
140
+ id: "0x" + paramType.toString(16),
141
+ length: raw.byteLength,
142
+ raw_hex: bytesToHex(raw)
143
+ });
144
+ }
145
+ } else {
146
+ const length = Number(reader.readVarInt());
147
+ const bytes = reader.readBytes(length);
148
+ if (paramType === PARAM_PATH) {
149
+ result.path = new TextDecoder().decode(bytes);
150
+ } else {
151
+ unknown.push({
152
+ id: "0x" + paramType.toString(16),
153
+ length,
154
+ raw_hex: bytesToHex(bytes)
155
+ });
156
+ }
157
+ }
158
+ }
159
+ if (unknown.length > 0) {
160
+ result.unknown = unknown;
161
+ }
162
+ return result;
163
+ }
164
+ function encodeClientSetupPayload(msg, w) {
165
+ w.writeVarInt(msg.supported_versions.length);
166
+ for (const v of msg.supported_versions) {
167
+ w.writeVarInt(v);
168
+ }
169
+ encodeParams(msg.parameters, w);
170
+ }
171
+ function encodeServerSetupPayload(msg, w) {
172
+ w.writeVarInt(msg.selected_version);
173
+ encodeParams(msg.parameters, w);
174
+ }
175
+ function encodeSubscribePayload(msg, w) {
176
+ w.writeVarInt(msg.request_id);
177
+ w.writeTuple(msg.track_namespace);
178
+ w.writeString(msg.track_name);
179
+ w.writeUint8(Number(msg.subscriber_priority));
180
+ w.writeVarInt(msg.group_order);
181
+ w.writeVarInt(msg.forward);
182
+ w.writeVarInt(msg.filter_type);
183
+ const ft = Number(msg.filter_type);
184
+ if (ft >= 3) {
185
+ w.writeVarInt(msg.start_group);
186
+ w.writeVarInt(msg.start_object);
187
+ }
188
+ if (ft === 4) {
189
+ w.writeVarInt(msg.end_group);
190
+ }
191
+ encodeParams(msg.parameters, w);
192
+ }
193
+ function encodeSubscribeOkPayload(msg, w) {
194
+ w.writeVarInt(msg.request_id);
195
+ w.writeVarInt(msg.track_alias);
196
+ w.writeVarInt(msg.expires);
197
+ w.writeVarInt(msg.group_order);
198
+ w.writeVarInt(msg.content_exists);
199
+ if (Number(msg.content_exists) === 1) {
200
+ w.writeVarInt(msg.largest_group);
201
+ w.writeVarInt(msg.largest_object);
202
+ }
203
+ encodeParams(msg.parameters, w);
204
+ }
205
+ function encodeSubscribeUpdatePayload(msg, w) {
206
+ w.writeVarInt(msg.request_id);
207
+ w.writeVarInt(msg.start_group);
208
+ w.writeVarInt(msg.start_object);
209
+ w.writeVarInt(msg.end_group);
210
+ w.writeUint8(Number(msg.subscriber_priority));
211
+ w.writeVarInt(msg.forward);
212
+ encodeParams(msg.parameters, w);
213
+ }
214
+ function encodeSubscribeErrorPayload(msg, w) {
215
+ w.writeVarInt(msg.request_id);
216
+ w.writeVarInt(msg.error_code);
217
+ w.writeString(msg.reason_phrase);
218
+ }
219
+ function encodeUnsubscribePayload(msg, w) {
220
+ w.writeVarInt(msg.request_id);
221
+ }
222
+ function encodePublishPayload(msg, w) {
223
+ w.writeVarInt(msg.request_id);
224
+ w.writeTuple(msg.track_namespace);
225
+ w.writeString(msg.track_name);
226
+ w.writeVarInt(msg.forward);
227
+ encodeParams(msg.parameters, w);
228
+ }
229
+ function encodePublishOkPayload(msg, w) {
230
+ w.writeVarInt(msg.request_id);
231
+ w.writeVarInt(msg.track_alias);
232
+ w.writeVarInt(msg.forward);
233
+ encodeParams(msg.parameters, w);
234
+ }
235
+ function encodePublishErrorPayload(msg, w) {
236
+ w.writeVarInt(msg.request_id);
237
+ w.writeVarInt(msg.error_code);
238
+ w.writeString(msg.reason_phrase);
239
+ }
240
+ function encodePublishDonePayload(msg, w) {
241
+ w.writeVarInt(msg.request_id);
242
+ w.writeVarInt(msg.status_code);
243
+ w.writeString(msg.reason_phrase);
244
+ }
245
+ function encodePublishNamespacePayload(msg, w) {
246
+ w.writeVarInt(msg.request_id);
247
+ w.writeTuple(msg.track_namespace);
248
+ encodeParams(msg.parameters, w);
249
+ }
250
+ function encodePublishNamespaceOkPayload(msg, w) {
251
+ w.writeVarInt(msg.request_id);
252
+ encodeParams(msg.parameters, w);
253
+ }
254
+ function encodePublishNamespaceErrorPayload(msg, w) {
255
+ w.writeVarInt(msg.request_id);
256
+ w.writeVarInt(msg.error_code);
257
+ w.writeString(msg.reason_phrase);
258
+ }
259
+ function encodePublishNamespaceDonePayload(msg, w) {
260
+ w.writeVarInt(msg.request_id);
261
+ w.writeVarInt(msg.status_code);
262
+ w.writeString(msg.reason_phrase);
263
+ }
264
+ function encodePublishNamespaceCancelPayload(msg, w) {
265
+ w.writeVarInt(msg.request_id);
266
+ }
267
+ function encodeSubscribeNamespacePayload(msg, w) {
268
+ w.writeVarInt(msg.request_id);
269
+ w.writeTuple(msg.namespace_prefix);
270
+ encodeParams(msg.parameters, w);
271
+ }
272
+ function encodeSubscribeNamespaceOkPayload(msg, w) {
273
+ w.writeVarInt(msg.request_id);
274
+ encodeParams(msg.parameters, w);
275
+ }
276
+ function encodeSubscribeNamespaceErrorPayload(msg, w) {
277
+ w.writeVarInt(msg.request_id);
278
+ w.writeVarInt(msg.error_code);
279
+ w.writeString(msg.reason_phrase);
280
+ }
281
+ function encodeUnsubscribeNamespacePayload(msg, w) {
282
+ w.writeVarInt(msg.request_id);
283
+ }
284
+ function encodeFetchPayload(msg, w) {
285
+ w.writeVarInt(msg.request_id);
286
+ w.writeTuple(msg.track_namespace);
287
+ w.writeString(msg.track_name);
288
+ w.writeVarInt(msg.start_group);
289
+ w.writeVarInt(msg.start_object);
290
+ w.writeVarInt(msg.end_group);
291
+ encodeParams(msg.parameters, w);
292
+ }
293
+ function encodeFetchOkPayload(msg, w) {
294
+ w.writeVarInt(msg.request_id);
295
+ w.writeVarInt(msg.track_alias);
296
+ w.writeVarInt(msg.end_of_track);
297
+ encodeParams(msg.parameters, w);
298
+ }
299
+ function encodeFetchErrorPayload(msg, w) {
300
+ w.writeVarInt(msg.request_id);
301
+ w.writeVarInt(msg.error_code);
302
+ w.writeString(msg.reason_phrase);
303
+ }
304
+ function encodeFetchCancelPayload(msg, w) {
305
+ w.writeVarInt(msg.request_id);
306
+ }
307
+ function encodeTrackStatusPayload(msg, w) {
308
+ w.writeVarInt(msg.request_id);
309
+ w.writeTuple(msg.track_namespace);
310
+ w.writeString(msg.track_name);
311
+ encodeParams(msg.parameters, w);
312
+ }
313
+ function encodeTrackStatusOkPayload(msg, w) {
314
+ w.writeVarInt(msg.request_id);
315
+ w.writeVarInt(msg.status_code);
316
+ const sc = Number(msg.status_code);
317
+ if (sc === 0 || sc === 3) {
318
+ w.writeVarInt(msg.largest_group);
319
+ w.writeVarInt(msg.largest_object);
320
+ }
321
+ encodeParams(msg.parameters, w);
322
+ }
323
+ function encodeTrackStatusErrorPayload(msg, w) {
324
+ w.writeVarInt(msg.request_id);
325
+ w.writeVarInt(msg.error_code);
326
+ w.writeString(msg.reason_phrase);
327
+ }
328
+ function encodeGoAwayPayload(msg, w) {
329
+ w.writeString(msg.new_session_uri);
330
+ }
331
+ function encodeMaxRequestIdPayload(msg, w) {
332
+ w.writeVarInt(msg.request_id);
333
+ }
334
+ function encodeRequestsBlockedPayload(msg, w) {
335
+ w.writeVarInt(msg.request_id);
336
+ }
337
+ function decodeClientSetupPayload(r) {
338
+ const numVersions = Number(r.readVarInt());
339
+ if (numVersions === 0) {
340
+ throw new DecodeError("CONSTRAINT_VIOLATION", "CLIENT_SETUP must offer at least one version", r.offset);
341
+ }
342
+ const supported_versions = [];
343
+ for (let i = 0; i < numVersions; i++) {
344
+ supported_versions.push(r.readVarInt());
345
+ }
346
+ const parameters = decodeParams(r);
347
+ return { type: "client_setup", supported_versions, parameters };
348
+ }
349
+ function decodeServerSetupPayload(r) {
350
+ const selected_version = r.readVarInt();
351
+ const parameters = decodeParams(r);
352
+ return { type: "server_setup", selected_version, parameters };
353
+ }
354
+ function decodeSubscribePayload(r) {
355
+ const request_id = r.readVarInt();
356
+ const track_namespace = r.readTuple();
357
+ const track_name = r.readString();
358
+ const subscriber_priority = BigInt(r.readUint8());
359
+ const group_order = r.readVarInt();
360
+ const forward = r.readVarInt();
361
+ const filter_type = r.readVarInt();
362
+ const ft = Number(filter_type);
363
+ if (ft < 1 || ft > 4) {
364
+ throw new DecodeError("CONSTRAINT_VIOLATION", `Invalid filter_type: ${ft}`, r.offset);
365
+ }
366
+ let start_group;
367
+ let start_object;
368
+ let end_group;
369
+ if (ft >= 3) {
370
+ start_group = r.readVarInt();
371
+ start_object = r.readVarInt();
372
+ }
373
+ if (ft === 4) {
374
+ end_group = r.readVarInt();
375
+ }
376
+ const parameters = decodeParams(r);
377
+ const msg = {
378
+ type: "subscribe",
379
+ request_id,
380
+ track_namespace,
381
+ track_name,
382
+ subscriber_priority,
383
+ group_order,
384
+ forward,
385
+ filter_type,
386
+ parameters
387
+ };
388
+ if (start_group !== void 0) {
389
+ return { ...msg, start_group, start_object, end_group };
390
+ }
391
+ return msg;
392
+ }
393
+ function decodeSubscribeOkPayload(r) {
394
+ const request_id = r.readVarInt();
395
+ const track_alias = r.readVarInt();
396
+ const expires = r.readVarInt();
397
+ const group_order = r.readVarInt();
398
+ const content_exists = r.readVarInt();
399
+ let largest_group;
400
+ let largest_object;
401
+ if (Number(content_exists) === 1) {
402
+ largest_group = r.readVarInt();
403
+ largest_object = r.readVarInt();
404
+ }
405
+ const parameters = decodeParams(r);
406
+ const msg = {
407
+ type: "subscribe_ok",
408
+ request_id,
409
+ track_alias,
410
+ expires,
411
+ group_order,
412
+ content_exists,
413
+ parameters,
414
+ ...largest_group !== void 0 ? { largest_group, largest_object } : {}
415
+ };
416
+ return msg;
417
+ }
418
+ function decodeSubscribeUpdatePayload(r) {
419
+ const request_id = r.readVarInt();
420
+ const start_group = r.readVarInt();
421
+ const start_object = r.readVarInt();
422
+ const end_group = r.readVarInt();
423
+ const subscriber_priority = BigInt(r.readUint8());
424
+ const forward = r.readVarInt();
425
+ const parameters = decodeParams(r);
426
+ return {
427
+ type: "subscribe_update",
428
+ request_id,
429
+ start_group,
430
+ start_object,
431
+ end_group,
432
+ subscriber_priority,
433
+ forward,
434
+ parameters
435
+ };
436
+ }
437
+ function decodeSubscribeErrorPayload(r) {
438
+ const request_id = r.readVarInt();
439
+ const error_code = r.readVarInt();
440
+ const reason_phrase = r.readString();
441
+ return { type: "subscribe_error", request_id, error_code, reason_phrase };
442
+ }
443
+ function decodeUnsubscribePayload(r) {
444
+ const request_id = r.readVarInt();
445
+ return { type: "unsubscribe", request_id };
446
+ }
447
+ function decodePublishPayload(r) {
448
+ const request_id = r.readVarInt();
449
+ const track_namespace = r.readTuple();
450
+ const track_name = r.readString();
451
+ const forward = r.readVarInt();
452
+ const parameters = decodeParams(r);
453
+ return { type: "publish", request_id, track_namespace, track_name, forward, parameters };
454
+ }
455
+ function decodePublishOkPayload(r) {
456
+ const request_id = r.readVarInt();
457
+ const track_alias = r.readVarInt();
458
+ const forward = r.readVarInt();
459
+ const parameters = decodeParams(r);
460
+ return { type: "publish_ok", request_id, track_alias, forward, parameters };
461
+ }
462
+ function decodePublishErrorPayload(r) {
463
+ const request_id = r.readVarInt();
464
+ const error_code = r.readVarInt();
465
+ const reason_phrase = r.readString();
466
+ return { type: "publish_error", request_id, error_code, reason_phrase };
467
+ }
468
+ function decodePublishDonePayload(r) {
469
+ const request_id = r.readVarInt();
470
+ const status_code = r.readVarInt();
471
+ const reason_phrase = r.readString();
472
+ return { type: "publish_done", request_id, status_code, reason_phrase };
473
+ }
474
+ function decodePublishNamespacePayload(r) {
475
+ const request_id = r.readVarInt();
476
+ const track_namespace = r.readTuple();
477
+ const parameters = decodeParams(r);
478
+ return { type: "publish_namespace", request_id, track_namespace, parameters };
479
+ }
480
+ function decodePublishNamespaceOkPayload(r) {
481
+ const request_id = r.readVarInt();
482
+ const parameters = decodeParams(r);
483
+ return { type: "publish_namespace_ok", request_id, parameters };
484
+ }
485
+ function decodePublishNamespaceErrorPayload(r) {
486
+ const request_id = r.readVarInt();
487
+ const error_code = r.readVarInt();
488
+ const reason_phrase = r.readString();
489
+ return { type: "publish_namespace_error", request_id, error_code, reason_phrase };
490
+ }
491
+ function decodePublishNamespaceDonePayload(r) {
492
+ const request_id = r.readVarInt();
493
+ const status_code = r.readVarInt();
494
+ const reason_phrase = r.readString();
495
+ return { type: "publish_namespace_done", request_id, status_code, reason_phrase };
496
+ }
497
+ function decodePublishNamespaceCancelPayload(r) {
498
+ const request_id = r.readVarInt();
499
+ return { type: "publish_namespace_cancel", request_id };
500
+ }
501
+ function decodeSubscribeNamespacePayload(r) {
502
+ const request_id = r.readVarInt();
503
+ const namespace_prefix = r.readTuple();
504
+ const parameters = decodeParams(r);
505
+ return { type: "subscribe_namespace", request_id, namespace_prefix, parameters };
506
+ }
507
+ function decodeSubscribeNamespaceOkPayload(r) {
508
+ const request_id = r.readVarInt();
509
+ const parameters = decodeParams(r);
510
+ return { type: "subscribe_namespace_ok", request_id, parameters };
511
+ }
512
+ function decodeSubscribeNamespaceErrorPayload(r) {
513
+ const request_id = r.readVarInt();
514
+ const error_code = r.readVarInt();
515
+ const reason_phrase = r.readString();
516
+ return { type: "subscribe_namespace_error", request_id, error_code, reason_phrase };
517
+ }
518
+ function decodeUnsubscribeNamespacePayload(r) {
519
+ const request_id = r.readVarInt();
520
+ return { type: "unsubscribe_namespace", request_id };
521
+ }
522
+ function decodeFetchPayload(r) {
523
+ const request_id = r.readVarInt();
524
+ const track_namespace = r.readTuple();
525
+ const track_name = r.readString();
526
+ const start_group = r.readVarInt();
527
+ const start_object = r.readVarInt();
528
+ const end_group = r.readVarInt();
529
+ const parameters = decodeParams(r);
530
+ return { type: "fetch", request_id, track_namespace, track_name, start_group, start_object, end_group, parameters };
531
+ }
532
+ function decodeFetchOkPayload(r) {
533
+ const request_id = r.readVarInt();
534
+ const track_alias = r.readVarInt();
535
+ const end_of_track = r.readVarInt();
536
+ const parameters = decodeParams(r);
537
+ return { type: "fetch_ok", request_id, track_alias, end_of_track, parameters };
538
+ }
539
+ function decodeFetchErrorPayload(r) {
540
+ const request_id = r.readVarInt();
541
+ const error_code = r.readVarInt();
542
+ const reason_phrase = r.readString();
543
+ return { type: "fetch_error", request_id, error_code, reason_phrase };
544
+ }
545
+ function decodeFetchCancelPayload(r) {
546
+ const request_id = r.readVarInt();
547
+ return { type: "fetch_cancel", request_id };
548
+ }
549
+ function decodeTrackStatusPayload(r) {
550
+ const request_id = r.readVarInt();
551
+ const track_namespace = r.readTuple();
552
+ const track_name = r.readString();
553
+ const parameters = decodeParams(r);
554
+ return { type: "track_status", request_id, track_namespace, track_name, parameters };
555
+ }
556
+ function decodeTrackStatusOkPayload(r) {
557
+ const request_id = r.readVarInt();
558
+ const status_code = r.readVarInt();
559
+ const sc = Number(status_code);
560
+ let largest_group;
561
+ let largest_object;
562
+ if (sc === 0 || sc === 3) {
563
+ largest_group = r.readVarInt();
564
+ largest_object = r.readVarInt();
565
+ }
566
+ const parameters = decodeParams(r);
567
+ return {
568
+ type: "track_status_ok",
569
+ request_id,
570
+ status_code,
571
+ parameters,
572
+ ...largest_group !== void 0 ? { largest_group, largest_object } : {}
573
+ };
574
+ }
575
+ function decodeTrackStatusErrorPayload(r) {
576
+ const request_id = r.readVarInt();
577
+ const error_code = r.readVarInt();
578
+ const reason_phrase = r.readString();
579
+ return { type: "track_status_error", request_id, error_code, reason_phrase };
580
+ }
581
+ function decodeGoAwayPayload(r) {
582
+ const new_session_uri = r.readString();
583
+ return { type: "goaway", new_session_uri };
584
+ }
585
+ function decodeMaxRequestIdPayload(r) {
586
+ const request_id = r.readVarInt();
587
+ return { type: "max_request_id", request_id };
588
+ }
589
+ function decodeRequestsBlockedPayload(r) {
590
+ const request_id = r.readVarInt();
591
+ return { type: "requests_blocked", request_id };
592
+ }
593
+ var payloadDecoders = /* @__PURE__ */ new Map([
594
+ [MSG_CLIENT_SETUP, decodeClientSetupPayload],
595
+ [MSG_SERVER_SETUP, decodeServerSetupPayload],
596
+ [MSG_SUBSCRIBE, decodeSubscribePayload],
597
+ [MSG_SUBSCRIBE_OK, decodeSubscribeOkPayload],
598
+ [MSG_SUBSCRIBE_UPDATE, decodeSubscribeUpdatePayload],
599
+ [MSG_SUBSCRIBE_ERROR, decodeSubscribeErrorPayload],
600
+ [MSG_UNSUBSCRIBE, decodeUnsubscribePayload],
601
+ [MSG_PUBLISH, decodePublishPayload],
602
+ [MSG_PUBLISH_OK, decodePublishOkPayload],
603
+ [MSG_PUBLISH_ERROR, decodePublishErrorPayload],
604
+ [MSG_PUBLISH_DONE, decodePublishDonePayload],
605
+ [MSG_PUBLISH_NAMESPACE, decodePublishNamespacePayload],
606
+ [MSG_PUBLISH_NAMESPACE_OK, decodePublishNamespaceOkPayload],
607
+ [MSG_PUBLISH_NAMESPACE_ERROR, decodePublishNamespaceErrorPayload],
608
+ [MSG_PUBLISH_NAMESPACE_DONE, decodePublishNamespaceDonePayload],
609
+ [MSG_PUBLISH_NAMESPACE_CANCEL, decodePublishNamespaceCancelPayload],
610
+ [MSG_SUBSCRIBE_NAMESPACE, decodeSubscribeNamespacePayload],
611
+ [MSG_SUBSCRIBE_NAMESPACE_OK, decodeSubscribeNamespaceOkPayload],
612
+ [MSG_SUBSCRIBE_NAMESPACE_ERROR, decodeSubscribeNamespaceErrorPayload],
613
+ [MSG_UNSUBSCRIBE_NAMESPACE, decodeUnsubscribeNamespacePayload],
614
+ [MSG_FETCH, decodeFetchPayload],
615
+ [MSG_FETCH_OK, decodeFetchOkPayload],
616
+ [MSG_FETCH_ERROR, decodeFetchErrorPayload],
617
+ [MSG_FETCH_CANCEL, decodeFetchCancelPayload],
618
+ [MSG_TRACK_STATUS, decodeTrackStatusPayload],
619
+ [MSG_TRACK_STATUS_OK, decodeTrackStatusOkPayload],
620
+ [MSG_TRACK_STATUS_ERROR, decodeTrackStatusErrorPayload],
621
+ [MSG_GOAWAY, decodeGoAwayPayload],
622
+ [MSG_MAX_REQUEST_ID, decodeMaxRequestIdPayload],
623
+ [MSG_REQUESTS_BLOCKED, decodeRequestsBlockedPayload]
624
+ ]);
625
+ function encodeMessage(message) {
626
+ const typeId = MESSAGE_ID_MAP.get(message.type);
627
+ if (typeId === void 0) {
628
+ throw new Error(`Unknown message type: ${message.type}`);
629
+ }
630
+ const payloadWriter = new BufferWriter();
631
+ encodePayload(message, payloadWriter);
632
+ const payload = payloadWriter.finish();
633
+ if (payload.byteLength > 65535) {
634
+ throw new Error(`Payload too large for 16-bit length: ${payload.byteLength}`);
635
+ }
636
+ const writer = new BufferWriter();
637
+ writer.writeVarInt(typeId);
638
+ writer.writeUint8(payload.byteLength >> 8 & 255);
639
+ writer.writeUint8(payload.byteLength & 255);
640
+ writer.writeBytes(payload);
641
+ return writer.finish();
642
+ }
643
+ function encodePayload(msg, w) {
644
+ switch (msg.type) {
645
+ case "client_setup":
646
+ return encodeClientSetupPayload(msg, w);
647
+ case "server_setup":
648
+ return encodeServerSetupPayload(msg, w);
649
+ case "subscribe":
650
+ return encodeSubscribePayload(msg, w);
651
+ case "subscribe_ok":
652
+ return encodeSubscribeOkPayload(msg, w);
653
+ case "subscribe_update":
654
+ return encodeSubscribeUpdatePayload(msg, w);
655
+ case "subscribe_error":
656
+ return encodeSubscribeErrorPayload(msg, w);
657
+ case "unsubscribe":
658
+ return encodeUnsubscribePayload(msg, w);
659
+ case "publish":
660
+ return encodePublishPayload(msg, w);
661
+ case "publish_ok":
662
+ return encodePublishOkPayload(msg, w);
663
+ case "publish_error":
664
+ return encodePublishErrorPayload(msg, w);
665
+ case "publish_done":
666
+ return encodePublishDonePayload(msg, w);
667
+ case "publish_namespace":
668
+ return encodePublishNamespacePayload(msg, w);
669
+ case "publish_namespace_ok":
670
+ return encodePublishNamespaceOkPayload(msg, w);
671
+ case "publish_namespace_error":
672
+ return encodePublishNamespaceErrorPayload(msg, w);
673
+ case "publish_namespace_done":
674
+ return encodePublishNamespaceDonePayload(msg, w);
675
+ case "publish_namespace_cancel":
676
+ return encodePublishNamespaceCancelPayload(msg, w);
677
+ case "subscribe_namespace":
678
+ return encodeSubscribeNamespacePayload(msg, w);
679
+ case "subscribe_namespace_ok":
680
+ return encodeSubscribeNamespaceOkPayload(msg, w);
681
+ case "subscribe_namespace_error":
682
+ return encodeSubscribeNamespaceErrorPayload(msg, w);
683
+ case "unsubscribe_namespace":
684
+ return encodeUnsubscribeNamespacePayload(msg, w);
685
+ case "fetch":
686
+ return encodeFetchPayload(msg, w);
687
+ case "fetch_ok":
688
+ return encodeFetchOkPayload(msg, w);
689
+ case "fetch_error":
690
+ return encodeFetchErrorPayload(msg, w);
691
+ case "fetch_cancel":
692
+ return encodeFetchCancelPayload(msg, w);
693
+ case "track_status":
694
+ return encodeTrackStatusPayload(msg, w);
695
+ case "track_status_ok":
696
+ return encodeTrackStatusOkPayload(msg, w);
697
+ case "track_status_error":
698
+ return encodeTrackStatusErrorPayload(msg, w);
699
+ case "goaway":
700
+ return encodeGoAwayPayload(msg, w);
701
+ case "max_request_id":
702
+ return encodeMaxRequestIdPayload(msg, w);
703
+ case "requests_blocked":
704
+ return encodeRequestsBlockedPayload(msg, w);
705
+ default: {
706
+ const _exhaustive = msg;
707
+ throw new Error(`Unhandled message type: ${_exhaustive.type}`);
708
+ }
709
+ }
710
+ }
711
+ function decodeMessage(bytes) {
712
+ try {
713
+ const reader = new BufferReader(bytes);
714
+ const typeId = reader.readVarInt();
715
+ const lenHi = reader.readUint8();
716
+ const lenLo = reader.readUint8();
717
+ const payloadLength = lenHi << 8 | lenLo;
718
+ const payloadBytes = reader.readBytes(payloadLength);
719
+ const payloadReader = new BufferReader(payloadBytes);
720
+ const decoder = payloadDecoders.get(typeId);
721
+ if (!decoder) {
722
+ return {
723
+ ok: false,
724
+ error: new DecodeError("UNKNOWN_MESSAGE_TYPE", `Unknown message type ID: 0x${typeId.toString(16)}`, 0)
725
+ };
726
+ }
727
+ const message = decoder(payloadReader);
728
+ return { ok: true, value: message, bytesRead: reader.offset };
729
+ } catch (e) {
730
+ if (e instanceof DecodeError) {
731
+ return { ok: false, error: e };
732
+ }
733
+ throw e;
734
+ }
735
+ }
736
+ function encodeSubgroupStream(stream) {
737
+ const w = new BufferWriter();
738
+ w.writeVarInt(0n);
739
+ w.writeVarInt(stream.trackAlias);
740
+ w.writeVarInt(stream.groupId);
741
+ w.writeVarInt(stream.subgroupId);
742
+ w.writeUint8(stream.publisherPriority);
743
+ for (const obj of stream.objects) {
744
+ w.writeVarInt(obj.objectId);
745
+ w.writeVarInt(obj.payloadLength);
746
+ w.writeBytes(obj.payload);
747
+ }
748
+ return w.finish();
749
+ }
750
+ function encodeDatagram(dg) {
751
+ const w = new BufferWriter();
752
+ w.writeVarInt(dg.trackAlias);
753
+ w.writeVarInt(dg.groupId);
754
+ w.writeVarInt(dg.objectId);
755
+ w.writeVarInt(dg.payloadLength);
756
+ w.writeBytes(dg.payload);
757
+ return w.finish();
758
+ }
759
+ function encodeFetchStream(stream) {
760
+ const w = new BufferWriter();
761
+ w.writeVarInt(2n);
762
+ w.writeVarInt(stream.subscribeRequestId);
763
+ for (const obj of stream.objects) {
764
+ w.writeVarInt(obj.objectId);
765
+ w.writeVarInt(obj.payloadLength);
766
+ w.writeBytes(obj.payload);
767
+ }
768
+ return w.finish();
769
+ }
770
+ function decodeSubgroupStream(bytes) {
771
+ try {
772
+ const r = new BufferReader(bytes);
773
+ const streamType = r.readVarInt();
774
+ if (streamType !== 0n) {
775
+ return { ok: false, error: new DecodeError("CONSTRAINT_VIOLATION", `Expected subgroup stream type 0, got ${streamType}`, 0) };
776
+ }
777
+ const trackAlias = r.readVarInt();
778
+ const groupId = r.readVarInt();
779
+ const subgroupId = r.readVarInt();
780
+ const publisherPriority = r.readUint8();
781
+ const objects = [];
782
+ while (r.remaining > 0) {
783
+ const objectId = r.readVarInt();
784
+ const payloadLength = Number(r.readVarInt());
785
+ const payload = r.readBytes(payloadLength);
786
+ objects.push({ type: "object", objectId, payloadLength, payload });
787
+ }
788
+ return {
789
+ ok: true,
790
+ value: { type: "subgroup", trackAlias, groupId, subgroupId, publisherPriority, objects },
791
+ bytesRead: r.offset
792
+ };
793
+ } catch (e) {
794
+ if (e instanceof DecodeError) return { ok: false, error: e };
795
+ throw e;
796
+ }
797
+ }
798
+ function decodeDatagram(bytes) {
799
+ try {
800
+ const r = new BufferReader(bytes);
801
+ const trackAlias = r.readVarInt();
802
+ const groupId = r.readVarInt();
803
+ const objectId = r.readVarInt();
804
+ const payloadLength = Number(r.readVarInt());
805
+ const payload = r.readBytes(payloadLength);
806
+ return {
807
+ ok: true,
808
+ value: { type: "datagram", trackAlias, groupId, objectId, payloadLength, payload },
809
+ bytesRead: r.offset
810
+ };
811
+ } catch (e) {
812
+ if (e instanceof DecodeError) return { ok: false, error: e };
813
+ throw e;
814
+ }
815
+ }
816
+ function decodeFetchStream(bytes) {
817
+ try {
818
+ const r = new BufferReader(bytes);
819
+ const streamType = r.readVarInt();
820
+ if (streamType !== 2n) {
821
+ return { ok: false, error: new DecodeError("CONSTRAINT_VIOLATION", `Expected fetch stream type 2, got ${streamType}`, 0) };
822
+ }
823
+ const subscribeRequestId = r.readVarInt();
824
+ const objects = [];
825
+ while (r.remaining > 0) {
826
+ const objectId = r.readVarInt();
827
+ const payloadLength = Number(r.readVarInt());
828
+ const payload = r.readBytes(payloadLength);
829
+ objects.push({ type: "object", objectId, payloadLength, payload });
830
+ }
831
+ return {
832
+ ok: true,
833
+ value: { type: "fetch", subscribeRequestId, objects },
834
+ bytesRead: r.offset
835
+ };
836
+ } catch (e) {
837
+ if (e instanceof DecodeError) return { ok: false, error: e };
838
+ throw e;
839
+ }
840
+ }
841
+ function decodeDataStream(streamType, bytes) {
842
+ switch (streamType) {
843
+ case "subgroup":
844
+ return decodeSubgroupStream(bytes);
845
+ case "datagram":
846
+ return decodeDatagram(bytes);
847
+ case "fetch":
848
+ return decodeFetchStream(bytes);
849
+ default: {
850
+ const _exhaustive = streamType;
851
+ throw new Error(`Unknown stream type: ${_exhaustive}`);
852
+ }
853
+ }
854
+ }
855
+ function createStreamDecoder() {
856
+ let buffer = new Uint8Array(0);
857
+ return new TransformStream({
858
+ transform(chunk, controller) {
859
+ const newBuffer = new Uint8Array(buffer.length + chunk.length);
860
+ newBuffer.set(buffer, 0);
861
+ newBuffer.set(chunk, buffer.length);
862
+ buffer = newBuffer;
863
+ while (buffer.length > 0) {
864
+ const result = decodeMessage(buffer);
865
+ if (!result.ok) {
866
+ if (result.error.code === "UNEXPECTED_END") {
867
+ break;
868
+ }
869
+ controller.error(result.error);
870
+ return;
871
+ }
872
+ controller.enqueue(result.value);
873
+ buffer = buffer.slice(result.bytesRead);
874
+ }
875
+ },
876
+ flush(controller) {
877
+ if (buffer.length > 0) {
878
+ controller.error(
879
+ new DecodeError("UNEXPECTED_END", "Stream ended with incomplete message data", 0)
880
+ );
881
+ }
882
+ }
883
+ });
884
+ }
885
+ function createSubgroupStreamDecoder() {
886
+ let buffer = new Uint8Array(0);
887
+ let headerEmitted = false;
888
+ return new TransformStream({
889
+ transform(chunk, controller) {
890
+ const newBuffer = new Uint8Array(buffer.length + chunk.length);
891
+ newBuffer.set(buffer, 0);
892
+ newBuffer.set(chunk, buffer.length);
893
+ buffer = newBuffer;
894
+ if (!headerEmitted) {
895
+ try {
896
+ const r = new BufferReader(buffer);
897
+ const streamType = r.readVarInt();
898
+ if (streamType !== 0n) {
899
+ controller.error(
900
+ new DecodeError("CONSTRAINT_VIOLATION", `Expected subgroup stream type 0, got ${streamType}`, 0)
901
+ );
902
+ return;
903
+ }
904
+ const trackAlias = r.readVarInt();
905
+ const groupId = r.readVarInt();
906
+ const subgroupId = r.readVarInt();
907
+ const publisherPriority = r.readUint8();
908
+ controller.enqueue({
909
+ type: "subgroup_header",
910
+ trackAlias,
911
+ groupId,
912
+ subgroupId,
913
+ publisherPriority
914
+ });
915
+ headerEmitted = true;
916
+ buffer = buffer.slice(r.offset);
917
+ } catch (e) {
918
+ if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
919
+ return;
920
+ }
921
+ controller.error(e);
922
+ return;
923
+ }
924
+ }
925
+ while (buffer.length > 0) {
926
+ try {
927
+ const r = new BufferReader(buffer);
928
+ const objectId = r.readVarInt();
929
+ const payloadLength = Number(r.readVarInt());
930
+ const payload = r.readBytes(payloadLength);
931
+ controller.enqueue({ type: "object", objectId, payloadLength, payload });
932
+ buffer = buffer.slice(r.offset);
933
+ } catch (e) {
934
+ if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
935
+ break;
936
+ }
937
+ controller.error(e);
938
+ return;
939
+ }
940
+ }
941
+ },
942
+ flush(controller) {
943
+ if (buffer.length > 0) {
944
+ controller.error(
945
+ new DecodeError("UNEXPECTED_END", "Stream ended with incomplete data", 0)
946
+ );
947
+ }
948
+ }
949
+ });
950
+ }
951
+ function createFetchStreamDecoder() {
952
+ let buffer = new Uint8Array(0);
953
+ let headerEmitted = false;
954
+ return new TransformStream({
955
+ transform(chunk, controller) {
956
+ const newBuffer = new Uint8Array(buffer.length + chunk.length);
957
+ newBuffer.set(buffer, 0);
958
+ newBuffer.set(chunk, buffer.length);
959
+ buffer = newBuffer;
960
+ if (!headerEmitted) {
961
+ try {
962
+ const r = new BufferReader(buffer);
963
+ const streamType = r.readVarInt();
964
+ if (streamType !== 2n) {
965
+ controller.error(
966
+ new DecodeError("CONSTRAINT_VIOLATION", `Expected fetch stream type 2, got ${streamType}`, 0)
967
+ );
968
+ return;
969
+ }
970
+ const subscribeRequestId = r.readVarInt();
971
+ controller.enqueue({
972
+ type: "fetch_header",
973
+ subscribeRequestId
974
+ });
975
+ headerEmitted = true;
976
+ buffer = buffer.slice(r.offset);
977
+ } catch (e) {
978
+ if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
979
+ return;
980
+ }
981
+ controller.error(e);
982
+ return;
983
+ }
984
+ }
985
+ while (buffer.length > 0) {
986
+ try {
987
+ const r = new BufferReader(buffer);
988
+ const objectId = r.readVarInt();
989
+ const payloadLength = Number(r.readVarInt());
990
+ const payload = r.readBytes(payloadLength);
991
+ controller.enqueue({ type: "object", objectId, payloadLength, payload });
992
+ buffer = buffer.slice(r.offset);
993
+ } catch (e) {
994
+ if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
995
+ break;
996
+ }
997
+ controller.error(e);
998
+ return;
999
+ }
1000
+ }
1001
+ },
1002
+ flush(controller) {
1003
+ if (buffer.length > 0) {
1004
+ controller.error(
1005
+ new DecodeError("UNEXPECTED_END", "Stream ended with incomplete data", 0)
1006
+ );
1007
+ }
1008
+ }
1009
+ });
1010
+ }
1011
+ function createDataStreamDecoder() {
1012
+ let buffer = new Uint8Array(0);
1013
+ let detectedType = null;
1014
+ let headerEmitted = false;
1015
+ return new TransformStream({
1016
+ transform(chunk, controller) {
1017
+ const newBuffer = new Uint8Array(buffer.length + chunk.length);
1018
+ newBuffer.set(buffer, 0);
1019
+ newBuffer.set(chunk, buffer.length);
1020
+ buffer = newBuffer;
1021
+ if (detectedType === null) {
1022
+ try {
1023
+ const r = new BufferReader(buffer);
1024
+ const streamType = r.readVarInt();
1025
+ if (streamType === 0n) {
1026
+ detectedType = "subgroup";
1027
+ } else if (streamType === 2n) {
1028
+ detectedType = "fetch";
1029
+ } else {
1030
+ controller.error(
1031
+ new DecodeError("CONSTRAINT_VIOLATION", `Unknown data stream type: ${streamType}`, 0)
1032
+ );
1033
+ return;
1034
+ }
1035
+ } catch (e) {
1036
+ if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
1037
+ return;
1038
+ }
1039
+ controller.error(e);
1040
+ return;
1041
+ }
1042
+ }
1043
+ if (detectedType === "subgroup") {
1044
+ if (!headerEmitted) {
1045
+ try {
1046
+ const r = new BufferReader(buffer);
1047
+ r.readVarInt();
1048
+ const trackAlias = r.readVarInt();
1049
+ const groupId = r.readVarInt();
1050
+ const subgroupId = r.readVarInt();
1051
+ const publisherPriority = r.readUint8();
1052
+ controller.enqueue({
1053
+ type: "subgroup_header",
1054
+ trackAlias,
1055
+ groupId,
1056
+ subgroupId,
1057
+ publisherPriority
1058
+ });
1059
+ headerEmitted = true;
1060
+ buffer = buffer.slice(r.offset);
1061
+ } catch (e) {
1062
+ if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
1063
+ return;
1064
+ }
1065
+ controller.error(e);
1066
+ return;
1067
+ }
1068
+ }
1069
+ } else {
1070
+ if (!headerEmitted) {
1071
+ try {
1072
+ const r = new BufferReader(buffer);
1073
+ r.readVarInt();
1074
+ const subscribeRequestId = r.readVarInt();
1075
+ controller.enqueue({
1076
+ type: "fetch_header",
1077
+ subscribeRequestId
1078
+ });
1079
+ headerEmitted = true;
1080
+ buffer = buffer.slice(r.offset);
1081
+ } catch (e) {
1082
+ if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
1083
+ return;
1084
+ }
1085
+ controller.error(e);
1086
+ return;
1087
+ }
1088
+ }
1089
+ }
1090
+ while (buffer.length > 0) {
1091
+ try {
1092
+ const r = new BufferReader(buffer);
1093
+ const objectId = r.readVarInt();
1094
+ const payloadLength = Number(r.readVarInt());
1095
+ const payload = r.readBytes(payloadLength);
1096
+ controller.enqueue({ type: "object", objectId, payloadLength, payload });
1097
+ buffer = buffer.slice(r.offset);
1098
+ } catch (e) {
1099
+ if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
1100
+ break;
1101
+ }
1102
+ controller.error(e);
1103
+ return;
1104
+ }
1105
+ }
1106
+ },
1107
+ flush(controller) {
1108
+ if (buffer.length > 0) {
1109
+ controller.error(
1110
+ new DecodeError("UNEXPECTED_END", "Stream ended with incomplete data", 0)
1111
+ );
1112
+ }
1113
+ }
1114
+ });
1115
+ }
1116
+ function createDraft14Codec() {
1117
+ return {
1118
+ draft: "draft-ietf-moq-transport-14",
1119
+ encodeMessage,
1120
+ decodeMessage,
1121
+ encodeSubgroupStream,
1122
+ encodeDatagram,
1123
+ encodeFetchStream,
1124
+ decodeSubgroupStream,
1125
+ decodeDatagram,
1126
+ decodeFetchStream,
1127
+ decodeDataStream,
1128
+ createStreamDecoder,
1129
+ createSubgroupStreamDecoder,
1130
+ createFetchStreamDecoder,
1131
+ createDataStreamDecoder
1132
+ };
1133
+ }
1134
+
1135
+ export {
1136
+ MSG_SUBSCRIBE_UPDATE,
1137
+ MSG_SUBSCRIBE,
1138
+ MSG_SUBSCRIBE_OK,
1139
+ MSG_SUBSCRIBE_ERROR,
1140
+ MSG_PUBLISH_NAMESPACE,
1141
+ MSG_PUBLISH_NAMESPACE_OK,
1142
+ MSG_PUBLISH_NAMESPACE_ERROR,
1143
+ MSG_PUBLISH_NAMESPACE_DONE,
1144
+ MSG_UNSUBSCRIBE,
1145
+ MSG_PUBLISH_DONE,
1146
+ MSG_PUBLISH_NAMESPACE_CANCEL,
1147
+ MSG_TRACK_STATUS,
1148
+ MSG_TRACK_STATUS_OK,
1149
+ MSG_TRACK_STATUS_ERROR,
1150
+ MSG_GOAWAY,
1151
+ MSG_SUBSCRIBE_NAMESPACE,
1152
+ MSG_SUBSCRIBE_NAMESPACE_OK,
1153
+ MSG_SUBSCRIBE_NAMESPACE_ERROR,
1154
+ MSG_UNSUBSCRIBE_NAMESPACE,
1155
+ MSG_MAX_REQUEST_ID,
1156
+ MSG_FETCH,
1157
+ MSG_FETCH_CANCEL,
1158
+ MSG_FETCH_OK,
1159
+ MSG_FETCH_ERROR,
1160
+ MSG_REQUESTS_BLOCKED,
1161
+ MSG_PUBLISH,
1162
+ MSG_PUBLISH_OK,
1163
+ MSG_PUBLISH_ERROR,
1164
+ MSG_CLIENT_SETUP,
1165
+ MSG_SERVER_SETUP,
1166
+ PARAM_ROLE,
1167
+ PARAM_PATH,
1168
+ PARAM_MAX_REQUEST_ID,
1169
+ MESSAGE_TYPE_MAP,
1170
+ MESSAGE_ID_MAP,
1171
+ encodeMessage,
1172
+ decodeMessage,
1173
+ encodeSubgroupStream,
1174
+ encodeDatagram,
1175
+ encodeFetchStream,
1176
+ decodeSubgroupStream,
1177
+ decodeDatagram,
1178
+ decodeFetchStream,
1179
+ decodeDataStream,
1180
+ createStreamDecoder,
1181
+ createSubgroupStreamDecoder,
1182
+ createFetchStreamDecoder,
1183
+ createDataStreamDecoder,
1184
+ createDraft14Codec
1185
+ };