@moqtap/codec 0.1.0 → 0.1.1
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.
- package/dist/{chunk-YBSEOSSP.js → chunk-A27S7HW7.js} +5 -1
- package/dist/{chunk-5WFXFLL4.cjs → chunk-FUFTMAQD.cjs} +96 -63
- package/dist/{chunk-2NARXGVA.cjs → chunk-FWISIR26.cjs} +5 -1
- package/dist/{chunk-23YG7F46.js → chunk-IXHOBNXA.js} +117 -17
- package/dist/{chunk-3BSZ55L3.cjs → chunk-NLYTRGXA.cjs} +153 -19
- package/dist/{chunk-GDRGWFEK.cjs → chunk-NPWHHWXT.cjs} +249 -37
- package/dist/{chunk-IQPDRQVC.js → chunk-U2B3B42P.js} +62 -29
- package/dist/{chunk-WNTXF3DE.cjs → chunk-YBZD3DU5.cjs} +127 -27
- package/dist/{chunk-DC4L6ZIT.js → chunk-YTXLWKOR.js} +153 -19
- package/dist/{chunk-YPXLV5YK.js → chunk-Z66WDWHI.js} +249 -37
- package/dist/{codec-qPzfmLNu.d.ts → codec-B2mc2g3i.d.ts} +5 -5
- package/dist/{codec-CTvFtQQI.d.cts → codec-Bvr7rFtj.d.cts} +5 -5
- package/dist/draft14-session.cjs +2 -2
- package/dist/draft14-session.d.cts +4 -4
- package/dist/draft14-session.d.ts +4 -4
- package/dist/draft14-session.js +1 -1
- package/dist/draft14.cjs +4 -4
- package/dist/draft14.d.cts +15 -15
- package/dist/draft14.d.ts +15 -15
- package/dist/draft14.js +3 -3
- package/dist/draft7-session.cjs +2 -2
- package/dist/draft7-session.d.cts +3 -3
- package/dist/draft7-session.d.ts +3 -3
- package/dist/draft7-session.js +1 -1
- package/dist/draft7.cjs +5 -5
- package/dist/draft7.d.cts +10 -10
- package/dist/draft7.d.ts +10 -10
- package/dist/draft7.js +2 -2
- package/dist/index.cjs +6 -6
- package/dist/index.d.cts +6 -6
- package/dist/index.d.ts +6 -6
- package/dist/index.js +3 -3
- package/dist/{session-types-B9NIf7_F.d.ts → session-types-DFjMk4HH.d.ts} +20 -20
- package/dist/{session-types-CCo-oA-d.d.cts → session-types-DW1RSZX_.d.cts} +20 -20
- package/dist/session.cjs +4 -4
- package/dist/session.d.cts +3 -3
- package/dist/session.d.ts +3 -3
- package/dist/session.js +2 -2
- package/dist/{types-CIk5W10V.d.ts → types-BTFeKYCb.d.cts} +37 -37
- package/dist/{types-CIk5W10V.d.cts → types-BTFeKYCb.d.ts} +37 -37
- package/dist/{types-ClXELFGN.d.cts → types-DPYE49t0.d.cts} +36 -36
- package/dist/{types-ClXELFGN.d.ts → types-DPYE49t0.d.ts} +36 -36
- package/package.json +7 -7
- package/src/core/buffer-reader.ts +16 -9
- package/src/core/buffer-writer.ts +2 -2
- package/src/core/errors.ts +1 -1
- package/src/core/session-types.ts +28 -41
- package/src/core/types.ts +70 -70
- package/src/drafts/draft07/announce-fsm.ts +1 -1
- package/src/drafts/draft07/codec.ts +195 -86
- package/src/drafts/draft07/index.ts +43 -44
- package/src/drafts/draft07/messages.ts +1 -1
- package/src/drafts/draft07/parameters.ts +2 -2
- package/src/drafts/draft07/rules.ts +68 -37
- package/src/drafts/draft07/session-fsm.ts +330 -117
- package/src/drafts/draft07/session.ts +10 -10
- package/src/drafts/draft07/subscription-fsm.ts +1 -1
- package/src/drafts/draft07/varint.ts +4 -4
- package/src/drafts/draft14/codec.ts +339 -189
- package/src/drafts/draft14/index.ts +103 -108
- package/src/drafts/draft14/messages.ts +61 -61
- package/src/drafts/draft14/rules.ts +77 -34
- package/src/drafts/draft14/session-fsm.ts +458 -146
- package/src/drafts/draft14/session.ts +13 -13
- package/src/drafts/draft14/types.ts +68 -68
- package/src/index.ts +66 -31
- package/src/session.ts +20 -20
|
@@ -1,63 +1,63 @@
|
|
|
1
|
-
import { BufferReader } from
|
|
2
|
-
import { BufferWriter } from
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
import type {
|
|
6
|
-
Draft14Message,
|
|
7
|
-
Draft14Params,
|
|
8
|
-
UnknownParam,
|
|
9
|
-
Draft14DataStream,
|
|
10
|
-
ObjectPayload,
|
|
11
|
-
SubgroupStream,
|
|
12
|
-
DatagramObject,
|
|
13
|
-
FetchStream,
|
|
14
|
-
SubgroupStreamHeader,
|
|
15
|
-
FetchStreamHeader,
|
|
16
|
-
DataStreamEvent,
|
|
17
|
-
} from './types.js';
|
|
1
|
+
import { BufferReader } from "../../core/buffer-reader.js";
|
|
2
|
+
import { BufferWriter } from "../../core/buffer-writer.js";
|
|
3
|
+
import type { BaseCodec, DecodeResult } from "../../core/types.js";
|
|
4
|
+
import { DecodeError } from "../../core/types.js";
|
|
18
5
|
import {
|
|
6
|
+
MESSAGE_ID_MAP,
|
|
19
7
|
MSG_CLIENT_SETUP,
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
8
|
+
MSG_FETCH,
|
|
9
|
+
MSG_FETCH_CANCEL,
|
|
10
|
+
MSG_FETCH_ERROR,
|
|
11
|
+
MSG_FETCH_OK,
|
|
12
|
+
MSG_GOAWAY,
|
|
13
|
+
MSG_MAX_REQUEST_ID,
|
|
26
14
|
MSG_PUBLISH,
|
|
27
|
-
MSG_PUBLISH_OK,
|
|
28
|
-
MSG_PUBLISH_ERROR,
|
|
29
15
|
MSG_PUBLISH_DONE,
|
|
16
|
+
MSG_PUBLISH_ERROR,
|
|
30
17
|
MSG_PUBLISH_NAMESPACE,
|
|
31
|
-
MSG_PUBLISH_NAMESPACE_OK,
|
|
32
|
-
MSG_PUBLISH_NAMESPACE_ERROR,
|
|
33
|
-
MSG_PUBLISH_NAMESPACE_DONE,
|
|
34
18
|
MSG_PUBLISH_NAMESPACE_CANCEL,
|
|
19
|
+
MSG_PUBLISH_NAMESPACE_DONE,
|
|
20
|
+
MSG_PUBLISH_NAMESPACE_ERROR,
|
|
21
|
+
MSG_PUBLISH_NAMESPACE_OK,
|
|
22
|
+
MSG_PUBLISH_OK,
|
|
23
|
+
MSG_REQUESTS_BLOCKED,
|
|
24
|
+
MSG_SERVER_SETUP,
|
|
25
|
+
MSG_SUBSCRIBE,
|
|
26
|
+
MSG_SUBSCRIBE_ERROR,
|
|
35
27
|
MSG_SUBSCRIBE_NAMESPACE,
|
|
36
|
-
MSG_SUBSCRIBE_NAMESPACE_OK,
|
|
37
28
|
MSG_SUBSCRIBE_NAMESPACE_ERROR,
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
MSG_FETCH_ERROR,
|
|
42
|
-
MSG_FETCH_CANCEL,
|
|
29
|
+
MSG_SUBSCRIBE_NAMESPACE_OK,
|
|
30
|
+
MSG_SUBSCRIBE_OK,
|
|
31
|
+
MSG_SUBSCRIBE_UPDATE,
|
|
43
32
|
MSG_TRACK_STATUS,
|
|
44
|
-
MSG_TRACK_STATUS_OK,
|
|
45
33
|
MSG_TRACK_STATUS_ERROR,
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
MESSAGE_ID_MAP,
|
|
50
|
-
PARAM_ROLE,
|
|
51
|
-
PARAM_PATH,
|
|
34
|
+
MSG_TRACK_STATUS_OK,
|
|
35
|
+
MSG_UNSUBSCRIBE,
|
|
36
|
+
MSG_UNSUBSCRIBE_NAMESPACE,
|
|
52
37
|
PARAM_MAX_REQUEST_ID,
|
|
53
|
-
|
|
38
|
+
PARAM_PATH,
|
|
39
|
+
PARAM_ROLE,
|
|
40
|
+
} from "./messages.js";
|
|
41
|
+
import type {
|
|
42
|
+
DatagramObject,
|
|
43
|
+
DataStreamEvent,
|
|
44
|
+
Draft14DataStream,
|
|
45
|
+
Draft14Message,
|
|
46
|
+
Draft14Params,
|
|
47
|
+
FetchStream,
|
|
48
|
+
FetchStreamHeader,
|
|
49
|
+
ObjectPayload,
|
|
50
|
+
SubgroupStream,
|
|
51
|
+
SubgroupStreamHeader,
|
|
52
|
+
UnknownParam,
|
|
53
|
+
} from "./types.js";
|
|
54
54
|
|
|
55
55
|
// ─── Helpers ───────────────────────────────────────────────────────────────────
|
|
56
56
|
|
|
57
57
|
function bytesToHex(bytes: Uint8Array): string {
|
|
58
|
-
let hex =
|
|
58
|
+
let hex = "";
|
|
59
59
|
for (let i = 0; i < bytes.byteLength; i++) {
|
|
60
|
-
hex += (bytes[i] as number).toString(16).padStart(2,
|
|
60
|
+
hex += (bytes[i] as number).toString(16).padStart(2, "0");
|
|
61
61
|
}
|
|
62
62
|
return hex;
|
|
63
63
|
}
|
|
@@ -135,7 +135,7 @@ function decodeParams(reader: BufferReader): Draft14Params {
|
|
|
135
135
|
tmpWriter.writeVarInt(value);
|
|
136
136
|
const raw = tmpWriter.finish();
|
|
137
137
|
unknown.push({
|
|
138
|
-
id:
|
|
138
|
+
id: `0x${paramType.toString(16)}`,
|
|
139
139
|
length: raw.byteLength,
|
|
140
140
|
raw_hex: bytesToHex(raw),
|
|
141
141
|
});
|
|
@@ -148,7 +148,7 @@ function decodeParams(reader: BufferReader): Draft14Params {
|
|
|
148
148
|
result.path = new TextDecoder().decode(bytes);
|
|
149
149
|
} else {
|
|
150
150
|
unknown.push({
|
|
151
|
-
id:
|
|
151
|
+
id: `0x${paramType.toString(16)}`,
|
|
152
152
|
length,
|
|
153
153
|
raw_hex: bytesToHex(bytes),
|
|
154
154
|
});
|
|
@@ -165,7 +165,10 @@ function decodeParams(reader: BufferReader): Draft14Params {
|
|
|
165
165
|
|
|
166
166
|
// ─── Payload Encoders ──────────────────────────────────────────────────────────
|
|
167
167
|
|
|
168
|
-
function encodeClientSetupPayload(
|
|
168
|
+
function encodeClientSetupPayload(
|
|
169
|
+
msg: Draft14Message & { type: "client_setup" },
|
|
170
|
+
w: BufferWriter,
|
|
171
|
+
): void {
|
|
169
172
|
w.writeVarInt(msg.supported_versions.length);
|
|
170
173
|
for (const v of msg.supported_versions) {
|
|
171
174
|
w.writeVarInt(v);
|
|
@@ -173,12 +176,18 @@ function encodeClientSetupPayload(msg: Draft14Message & { type: 'client_setup' }
|
|
|
173
176
|
encodeParams(msg.parameters, w);
|
|
174
177
|
}
|
|
175
178
|
|
|
176
|
-
function encodeServerSetupPayload(
|
|
179
|
+
function encodeServerSetupPayload(
|
|
180
|
+
msg: Draft14Message & { type: "server_setup" },
|
|
181
|
+
w: BufferWriter,
|
|
182
|
+
): void {
|
|
177
183
|
w.writeVarInt(msg.selected_version);
|
|
178
184
|
encodeParams(msg.parameters, w);
|
|
179
185
|
}
|
|
180
186
|
|
|
181
|
-
function encodeSubscribePayload(
|
|
187
|
+
function encodeSubscribePayload(
|
|
188
|
+
msg: Draft14Message & { type: "subscribe" },
|
|
189
|
+
w: BufferWriter,
|
|
190
|
+
): void {
|
|
182
191
|
w.writeVarInt(msg.request_id);
|
|
183
192
|
w.writeTuple(msg.track_namespace);
|
|
184
193
|
w.writeString(msg.track_name);
|
|
@@ -197,7 +206,10 @@ function encodeSubscribePayload(msg: Draft14Message & { type: 'subscribe' }, w:
|
|
|
197
206
|
encodeParams(msg.parameters, w);
|
|
198
207
|
}
|
|
199
208
|
|
|
200
|
-
function encodeSubscribeOkPayload(
|
|
209
|
+
function encodeSubscribeOkPayload(
|
|
210
|
+
msg: Draft14Message & { type: "subscribe_ok" },
|
|
211
|
+
w: BufferWriter,
|
|
212
|
+
): void {
|
|
201
213
|
w.writeVarInt(msg.request_id);
|
|
202
214
|
w.writeVarInt(msg.track_alias);
|
|
203
215
|
w.writeVarInt(msg.expires);
|
|
@@ -210,7 +222,10 @@ function encodeSubscribeOkPayload(msg: Draft14Message & { type: 'subscribe_ok' }
|
|
|
210
222
|
encodeParams(msg.parameters, w);
|
|
211
223
|
}
|
|
212
224
|
|
|
213
|
-
function encodeSubscribeUpdatePayload(
|
|
225
|
+
function encodeSubscribeUpdatePayload(
|
|
226
|
+
msg: Draft14Message & { type: "subscribe_update" },
|
|
227
|
+
w: BufferWriter,
|
|
228
|
+
): void {
|
|
214
229
|
w.writeVarInt(msg.request_id);
|
|
215
230
|
w.writeVarInt(msg.start_group);
|
|
216
231
|
w.writeVarInt(msg.start_object);
|
|
@@ -220,17 +235,23 @@ function encodeSubscribeUpdatePayload(msg: Draft14Message & { type: 'subscribe_u
|
|
|
220
235
|
encodeParams(msg.parameters, w);
|
|
221
236
|
}
|
|
222
237
|
|
|
223
|
-
function encodeSubscribeErrorPayload(
|
|
238
|
+
function encodeSubscribeErrorPayload(
|
|
239
|
+
msg: Draft14Message & { type: "subscribe_error" },
|
|
240
|
+
w: BufferWriter,
|
|
241
|
+
): void {
|
|
224
242
|
w.writeVarInt(msg.request_id);
|
|
225
243
|
w.writeVarInt(msg.error_code);
|
|
226
244
|
w.writeString(msg.reason_phrase);
|
|
227
245
|
}
|
|
228
246
|
|
|
229
|
-
function encodeUnsubscribePayload(
|
|
247
|
+
function encodeUnsubscribePayload(
|
|
248
|
+
msg: Draft14Message & { type: "unsubscribe" },
|
|
249
|
+
w: BufferWriter,
|
|
250
|
+
): void {
|
|
230
251
|
w.writeVarInt(msg.request_id);
|
|
231
252
|
}
|
|
232
253
|
|
|
233
|
-
function encodePublishPayload(msg: Draft14Message & { type:
|
|
254
|
+
function encodePublishPayload(msg: Draft14Message & { type: "publish" }, w: BufferWriter): void {
|
|
234
255
|
w.writeVarInt(msg.request_id);
|
|
235
256
|
w.writeTuple(msg.track_namespace);
|
|
236
257
|
w.writeString(msg.track_name);
|
|
@@ -238,74 +259,110 @@ function encodePublishPayload(msg: Draft14Message & { type: 'publish' }, w: Buff
|
|
|
238
259
|
encodeParams(msg.parameters, w);
|
|
239
260
|
}
|
|
240
261
|
|
|
241
|
-
function encodePublishOkPayload(
|
|
262
|
+
function encodePublishOkPayload(
|
|
263
|
+
msg: Draft14Message & { type: "publish_ok" },
|
|
264
|
+
w: BufferWriter,
|
|
265
|
+
): void {
|
|
242
266
|
w.writeVarInt(msg.request_id);
|
|
243
267
|
w.writeVarInt(msg.track_alias);
|
|
244
268
|
w.writeVarInt(msg.forward);
|
|
245
269
|
encodeParams(msg.parameters, w);
|
|
246
270
|
}
|
|
247
271
|
|
|
248
|
-
function encodePublishErrorPayload(
|
|
272
|
+
function encodePublishErrorPayload(
|
|
273
|
+
msg: Draft14Message & { type: "publish_error" },
|
|
274
|
+
w: BufferWriter,
|
|
275
|
+
): void {
|
|
249
276
|
w.writeVarInt(msg.request_id);
|
|
250
277
|
w.writeVarInt(msg.error_code);
|
|
251
278
|
w.writeString(msg.reason_phrase);
|
|
252
279
|
}
|
|
253
280
|
|
|
254
|
-
function encodePublishDonePayload(
|
|
281
|
+
function encodePublishDonePayload(
|
|
282
|
+
msg: Draft14Message & { type: "publish_done" },
|
|
283
|
+
w: BufferWriter,
|
|
284
|
+
): void {
|
|
255
285
|
w.writeVarInt(msg.request_id);
|
|
256
286
|
w.writeVarInt(msg.status_code);
|
|
257
287
|
w.writeString(msg.reason_phrase);
|
|
258
288
|
}
|
|
259
289
|
|
|
260
|
-
function encodePublishNamespacePayload(
|
|
290
|
+
function encodePublishNamespacePayload(
|
|
291
|
+
msg: Draft14Message & { type: "publish_namespace" },
|
|
292
|
+
w: BufferWriter,
|
|
293
|
+
): void {
|
|
261
294
|
w.writeVarInt(msg.request_id);
|
|
262
295
|
w.writeTuple(msg.track_namespace);
|
|
263
296
|
encodeParams(msg.parameters, w);
|
|
264
297
|
}
|
|
265
298
|
|
|
266
|
-
function encodePublishNamespaceOkPayload(
|
|
299
|
+
function encodePublishNamespaceOkPayload(
|
|
300
|
+
msg: Draft14Message & { type: "publish_namespace_ok" },
|
|
301
|
+
w: BufferWriter,
|
|
302
|
+
): void {
|
|
267
303
|
w.writeVarInt(msg.request_id);
|
|
268
304
|
encodeParams(msg.parameters, w);
|
|
269
305
|
}
|
|
270
306
|
|
|
271
|
-
function encodePublishNamespaceErrorPayload(
|
|
307
|
+
function encodePublishNamespaceErrorPayload(
|
|
308
|
+
msg: Draft14Message & { type: "publish_namespace_error" },
|
|
309
|
+
w: BufferWriter,
|
|
310
|
+
): void {
|
|
272
311
|
w.writeVarInt(msg.request_id);
|
|
273
312
|
w.writeVarInt(msg.error_code);
|
|
274
313
|
w.writeString(msg.reason_phrase);
|
|
275
314
|
}
|
|
276
315
|
|
|
277
|
-
function encodePublishNamespaceDonePayload(
|
|
316
|
+
function encodePublishNamespaceDonePayload(
|
|
317
|
+
msg: Draft14Message & { type: "publish_namespace_done" },
|
|
318
|
+
w: BufferWriter,
|
|
319
|
+
): void {
|
|
278
320
|
w.writeVarInt(msg.request_id);
|
|
279
321
|
w.writeVarInt(msg.status_code);
|
|
280
322
|
w.writeString(msg.reason_phrase);
|
|
281
323
|
}
|
|
282
324
|
|
|
283
|
-
function encodePublishNamespaceCancelPayload(
|
|
325
|
+
function encodePublishNamespaceCancelPayload(
|
|
326
|
+
msg: Draft14Message & { type: "publish_namespace_cancel" },
|
|
327
|
+
w: BufferWriter,
|
|
328
|
+
): void {
|
|
284
329
|
w.writeVarInt(msg.request_id);
|
|
285
330
|
}
|
|
286
331
|
|
|
287
|
-
function encodeSubscribeNamespacePayload(
|
|
332
|
+
function encodeSubscribeNamespacePayload(
|
|
333
|
+
msg: Draft14Message & { type: "subscribe_namespace" },
|
|
334
|
+
w: BufferWriter,
|
|
335
|
+
): void {
|
|
288
336
|
w.writeVarInt(msg.request_id);
|
|
289
337
|
w.writeTuple(msg.namespace_prefix);
|
|
290
338
|
encodeParams(msg.parameters, w);
|
|
291
339
|
}
|
|
292
340
|
|
|
293
|
-
function encodeSubscribeNamespaceOkPayload(
|
|
341
|
+
function encodeSubscribeNamespaceOkPayload(
|
|
342
|
+
msg: Draft14Message & { type: "subscribe_namespace_ok" },
|
|
343
|
+
w: BufferWriter,
|
|
344
|
+
): void {
|
|
294
345
|
w.writeVarInt(msg.request_id);
|
|
295
346
|
encodeParams(msg.parameters, w);
|
|
296
347
|
}
|
|
297
348
|
|
|
298
|
-
function encodeSubscribeNamespaceErrorPayload(
|
|
349
|
+
function encodeSubscribeNamespaceErrorPayload(
|
|
350
|
+
msg: Draft14Message & { type: "subscribe_namespace_error" },
|
|
351
|
+
w: BufferWriter,
|
|
352
|
+
): void {
|
|
299
353
|
w.writeVarInt(msg.request_id);
|
|
300
354
|
w.writeVarInt(msg.error_code);
|
|
301
355
|
w.writeString(msg.reason_phrase);
|
|
302
356
|
}
|
|
303
357
|
|
|
304
|
-
function encodeUnsubscribeNamespacePayload(
|
|
358
|
+
function encodeUnsubscribeNamespacePayload(
|
|
359
|
+
msg: Draft14Message & { type: "unsubscribe_namespace" },
|
|
360
|
+
w: BufferWriter,
|
|
361
|
+
): void {
|
|
305
362
|
w.writeVarInt(msg.request_id);
|
|
306
363
|
}
|
|
307
364
|
|
|
308
|
-
function encodeFetchPayload(msg: Draft14Message & { type:
|
|
365
|
+
function encodeFetchPayload(msg: Draft14Message & { type: "fetch" }, w: BufferWriter): void {
|
|
309
366
|
w.writeVarInt(msg.request_id);
|
|
310
367
|
w.writeTuple(msg.track_namespace);
|
|
311
368
|
w.writeString(msg.track_name);
|
|
@@ -315,31 +372,43 @@ function encodeFetchPayload(msg: Draft14Message & { type: 'fetch' }, w: BufferWr
|
|
|
315
372
|
encodeParams(msg.parameters, w);
|
|
316
373
|
}
|
|
317
374
|
|
|
318
|
-
function encodeFetchOkPayload(msg: Draft14Message & { type:
|
|
375
|
+
function encodeFetchOkPayload(msg: Draft14Message & { type: "fetch_ok" }, w: BufferWriter): void {
|
|
319
376
|
w.writeVarInt(msg.request_id);
|
|
320
377
|
w.writeVarInt(msg.track_alias);
|
|
321
378
|
w.writeVarInt(msg.end_of_track);
|
|
322
379
|
encodeParams(msg.parameters, w);
|
|
323
380
|
}
|
|
324
381
|
|
|
325
|
-
function encodeFetchErrorPayload(
|
|
382
|
+
function encodeFetchErrorPayload(
|
|
383
|
+
msg: Draft14Message & { type: "fetch_error" },
|
|
384
|
+
w: BufferWriter,
|
|
385
|
+
): void {
|
|
326
386
|
w.writeVarInt(msg.request_id);
|
|
327
387
|
w.writeVarInt(msg.error_code);
|
|
328
388
|
w.writeString(msg.reason_phrase);
|
|
329
389
|
}
|
|
330
390
|
|
|
331
|
-
function encodeFetchCancelPayload(
|
|
391
|
+
function encodeFetchCancelPayload(
|
|
392
|
+
msg: Draft14Message & { type: "fetch_cancel" },
|
|
393
|
+
w: BufferWriter,
|
|
394
|
+
): void {
|
|
332
395
|
w.writeVarInt(msg.request_id);
|
|
333
396
|
}
|
|
334
397
|
|
|
335
|
-
function encodeTrackStatusPayload(
|
|
398
|
+
function encodeTrackStatusPayload(
|
|
399
|
+
msg: Draft14Message & { type: "track_status" },
|
|
400
|
+
w: BufferWriter,
|
|
401
|
+
): void {
|
|
336
402
|
w.writeVarInt(msg.request_id);
|
|
337
403
|
w.writeTuple(msg.track_namespace);
|
|
338
404
|
w.writeString(msg.track_name);
|
|
339
405
|
encodeParams(msg.parameters, w);
|
|
340
406
|
}
|
|
341
407
|
|
|
342
|
-
function encodeTrackStatusOkPayload(
|
|
408
|
+
function encodeTrackStatusOkPayload(
|
|
409
|
+
msg: Draft14Message & { type: "track_status_ok" },
|
|
410
|
+
w: BufferWriter,
|
|
411
|
+
): void {
|
|
343
412
|
w.writeVarInt(msg.request_id);
|
|
344
413
|
w.writeVarInt(msg.status_code);
|
|
345
414
|
const sc = Number(msg.status_code);
|
|
@@ -350,21 +419,30 @@ function encodeTrackStatusOkPayload(msg: Draft14Message & { type: 'track_status_
|
|
|
350
419
|
encodeParams(msg.parameters, w);
|
|
351
420
|
}
|
|
352
421
|
|
|
353
|
-
function encodeTrackStatusErrorPayload(
|
|
422
|
+
function encodeTrackStatusErrorPayload(
|
|
423
|
+
msg: Draft14Message & { type: "track_status_error" },
|
|
424
|
+
w: BufferWriter,
|
|
425
|
+
): void {
|
|
354
426
|
w.writeVarInt(msg.request_id);
|
|
355
427
|
w.writeVarInt(msg.error_code);
|
|
356
428
|
w.writeString(msg.reason_phrase);
|
|
357
429
|
}
|
|
358
430
|
|
|
359
|
-
function encodeGoAwayPayload(msg: Draft14Message & { type:
|
|
431
|
+
function encodeGoAwayPayload(msg: Draft14Message & { type: "goaway" }, w: BufferWriter): void {
|
|
360
432
|
w.writeString(msg.new_session_uri);
|
|
361
433
|
}
|
|
362
434
|
|
|
363
|
-
function encodeMaxRequestIdPayload(
|
|
435
|
+
function encodeMaxRequestIdPayload(
|
|
436
|
+
msg: Draft14Message & { type: "max_request_id" },
|
|
437
|
+
w: BufferWriter,
|
|
438
|
+
): void {
|
|
364
439
|
w.writeVarInt(msg.request_id);
|
|
365
440
|
}
|
|
366
441
|
|
|
367
|
-
function encodeRequestsBlockedPayload(
|
|
442
|
+
function encodeRequestsBlockedPayload(
|
|
443
|
+
msg: Draft14Message & { type: "requests_blocked" },
|
|
444
|
+
w: BufferWriter,
|
|
445
|
+
): void {
|
|
368
446
|
w.writeVarInt(msg.request_id);
|
|
369
447
|
}
|
|
370
448
|
|
|
@@ -373,20 +451,24 @@ function encodeRequestsBlockedPayload(msg: Draft14Message & { type: 'requests_bl
|
|
|
373
451
|
function decodeClientSetupPayload(r: BufferReader): Draft14Message {
|
|
374
452
|
const numVersions = Number(r.readVarInt());
|
|
375
453
|
if (numVersions === 0) {
|
|
376
|
-
throw new DecodeError(
|
|
454
|
+
throw new DecodeError(
|
|
455
|
+
"CONSTRAINT_VIOLATION",
|
|
456
|
+
"CLIENT_SETUP must offer at least one version",
|
|
457
|
+
r.offset,
|
|
458
|
+
);
|
|
377
459
|
}
|
|
378
460
|
const supported_versions: bigint[] = [];
|
|
379
461
|
for (let i = 0; i < numVersions; i++) {
|
|
380
462
|
supported_versions.push(r.readVarInt());
|
|
381
463
|
}
|
|
382
464
|
const parameters = decodeParams(r);
|
|
383
|
-
return { type:
|
|
465
|
+
return { type: "client_setup", supported_versions, parameters };
|
|
384
466
|
}
|
|
385
467
|
|
|
386
468
|
function decodeServerSetupPayload(r: BufferReader): Draft14Message {
|
|
387
469
|
const selected_version = r.readVarInt();
|
|
388
470
|
const parameters = decodeParams(r);
|
|
389
|
-
return { type:
|
|
471
|
+
return { type: "server_setup", selected_version, parameters };
|
|
390
472
|
}
|
|
391
473
|
|
|
392
474
|
function decodeSubscribePayload(r: BufferReader): Draft14Message {
|
|
@@ -400,7 +482,7 @@ function decodeSubscribePayload(r: BufferReader): Draft14Message {
|
|
|
400
482
|
const ft = Number(filter_type);
|
|
401
483
|
|
|
402
484
|
if (ft < 1 || ft > 4) {
|
|
403
|
-
throw new DecodeError(
|
|
485
|
+
throw new DecodeError("CONSTRAINT_VIOLATION", `Invalid filter_type: ${ft}`, r.offset);
|
|
404
486
|
}
|
|
405
487
|
|
|
406
488
|
let start_group: bigint | undefined;
|
|
@@ -417,8 +499,8 @@ function decodeSubscribePayload(r: BufferReader): Draft14Message {
|
|
|
417
499
|
|
|
418
500
|
const parameters = decodeParams(r);
|
|
419
501
|
|
|
420
|
-
const msg: Draft14Message & { type:
|
|
421
|
-
type:
|
|
502
|
+
const msg: Draft14Message & { type: "subscribe" } = {
|
|
503
|
+
type: "subscribe",
|
|
422
504
|
request_id,
|
|
423
505
|
track_namespace,
|
|
424
506
|
track_name,
|
|
@@ -454,7 +536,7 @@ function decodeSubscribeOkPayload(r: BufferReader): Draft14Message {
|
|
|
454
536
|
const parameters = decodeParams(r);
|
|
455
537
|
|
|
456
538
|
const msg: Draft14Message = {
|
|
457
|
-
type:
|
|
539
|
+
type: "subscribe_ok",
|
|
458
540
|
request_id,
|
|
459
541
|
track_alias,
|
|
460
542
|
expires,
|
|
@@ -476,7 +558,7 @@ function decodeSubscribeUpdatePayload(r: BufferReader): Draft14Message {
|
|
|
476
558
|
const forward = r.readVarInt();
|
|
477
559
|
const parameters = decodeParams(r);
|
|
478
560
|
return {
|
|
479
|
-
type:
|
|
561
|
+
type: "subscribe_update",
|
|
480
562
|
request_id,
|
|
481
563
|
start_group,
|
|
482
564
|
start_object,
|
|
@@ -491,12 +573,12 @@ function decodeSubscribeErrorPayload(r: BufferReader): Draft14Message {
|
|
|
491
573
|
const request_id = r.readVarInt();
|
|
492
574
|
const error_code = r.readVarInt();
|
|
493
575
|
const reason_phrase = r.readString();
|
|
494
|
-
return { type:
|
|
576
|
+
return { type: "subscribe_error", request_id, error_code, reason_phrase };
|
|
495
577
|
}
|
|
496
578
|
|
|
497
579
|
function decodeUnsubscribePayload(r: BufferReader): Draft14Message {
|
|
498
580
|
const request_id = r.readVarInt();
|
|
499
|
-
return { type:
|
|
581
|
+
return { type: "unsubscribe", request_id };
|
|
500
582
|
}
|
|
501
583
|
|
|
502
584
|
function decodePublishPayload(r: BufferReader): Draft14Message {
|
|
@@ -505,7 +587,7 @@ function decodePublishPayload(r: BufferReader): Draft14Message {
|
|
|
505
587
|
const track_name = r.readString();
|
|
506
588
|
const forward = r.readVarInt();
|
|
507
589
|
const parameters = decodeParams(r);
|
|
508
|
-
return { type:
|
|
590
|
+
return { type: "publish", request_id, track_namespace, track_name, forward, parameters };
|
|
509
591
|
}
|
|
510
592
|
|
|
511
593
|
function decodePublishOkPayload(r: BufferReader): Draft14Message {
|
|
@@ -513,78 +595,78 @@ function decodePublishOkPayload(r: BufferReader): Draft14Message {
|
|
|
513
595
|
const track_alias = r.readVarInt();
|
|
514
596
|
const forward = r.readVarInt();
|
|
515
597
|
const parameters = decodeParams(r);
|
|
516
|
-
return { type:
|
|
598
|
+
return { type: "publish_ok", request_id, track_alias, forward, parameters };
|
|
517
599
|
}
|
|
518
600
|
|
|
519
601
|
function decodePublishErrorPayload(r: BufferReader): Draft14Message {
|
|
520
602
|
const request_id = r.readVarInt();
|
|
521
603
|
const error_code = r.readVarInt();
|
|
522
604
|
const reason_phrase = r.readString();
|
|
523
|
-
return { type:
|
|
605
|
+
return { type: "publish_error", request_id, error_code, reason_phrase };
|
|
524
606
|
}
|
|
525
607
|
|
|
526
608
|
function decodePublishDonePayload(r: BufferReader): Draft14Message {
|
|
527
609
|
const request_id = r.readVarInt();
|
|
528
610
|
const status_code = r.readVarInt();
|
|
529
611
|
const reason_phrase = r.readString();
|
|
530
|
-
return { type:
|
|
612
|
+
return { type: "publish_done", request_id, status_code, reason_phrase };
|
|
531
613
|
}
|
|
532
614
|
|
|
533
615
|
function decodePublishNamespacePayload(r: BufferReader): Draft14Message {
|
|
534
616
|
const request_id = r.readVarInt();
|
|
535
617
|
const track_namespace = r.readTuple();
|
|
536
618
|
const parameters = decodeParams(r);
|
|
537
|
-
return { type:
|
|
619
|
+
return { type: "publish_namespace", request_id, track_namespace, parameters };
|
|
538
620
|
}
|
|
539
621
|
|
|
540
622
|
function decodePublishNamespaceOkPayload(r: BufferReader): Draft14Message {
|
|
541
623
|
const request_id = r.readVarInt();
|
|
542
624
|
const parameters = decodeParams(r);
|
|
543
|
-
return { type:
|
|
625
|
+
return { type: "publish_namespace_ok", request_id, parameters };
|
|
544
626
|
}
|
|
545
627
|
|
|
546
628
|
function decodePublishNamespaceErrorPayload(r: BufferReader): Draft14Message {
|
|
547
629
|
const request_id = r.readVarInt();
|
|
548
630
|
const error_code = r.readVarInt();
|
|
549
631
|
const reason_phrase = r.readString();
|
|
550
|
-
return { type:
|
|
632
|
+
return { type: "publish_namespace_error", request_id, error_code, reason_phrase };
|
|
551
633
|
}
|
|
552
634
|
|
|
553
635
|
function decodePublishNamespaceDonePayload(r: BufferReader): Draft14Message {
|
|
554
636
|
const request_id = r.readVarInt();
|
|
555
637
|
const status_code = r.readVarInt();
|
|
556
638
|
const reason_phrase = r.readString();
|
|
557
|
-
return { type:
|
|
639
|
+
return { type: "publish_namespace_done", request_id, status_code, reason_phrase };
|
|
558
640
|
}
|
|
559
641
|
|
|
560
642
|
function decodePublishNamespaceCancelPayload(r: BufferReader): Draft14Message {
|
|
561
643
|
const request_id = r.readVarInt();
|
|
562
|
-
return { type:
|
|
644
|
+
return { type: "publish_namespace_cancel", request_id };
|
|
563
645
|
}
|
|
564
646
|
|
|
565
647
|
function decodeSubscribeNamespacePayload(r: BufferReader): Draft14Message {
|
|
566
648
|
const request_id = r.readVarInt();
|
|
567
649
|
const namespace_prefix = r.readTuple();
|
|
568
650
|
const parameters = decodeParams(r);
|
|
569
|
-
return { type:
|
|
651
|
+
return { type: "subscribe_namespace", request_id, namespace_prefix, parameters };
|
|
570
652
|
}
|
|
571
653
|
|
|
572
654
|
function decodeSubscribeNamespaceOkPayload(r: BufferReader): Draft14Message {
|
|
573
655
|
const request_id = r.readVarInt();
|
|
574
656
|
const parameters = decodeParams(r);
|
|
575
|
-
return { type:
|
|
657
|
+
return { type: "subscribe_namespace_ok", request_id, parameters };
|
|
576
658
|
}
|
|
577
659
|
|
|
578
660
|
function decodeSubscribeNamespaceErrorPayload(r: BufferReader): Draft14Message {
|
|
579
661
|
const request_id = r.readVarInt();
|
|
580
662
|
const error_code = r.readVarInt();
|
|
581
663
|
const reason_phrase = r.readString();
|
|
582
|
-
return { type:
|
|
664
|
+
return { type: "subscribe_namespace_error", request_id, error_code, reason_phrase };
|
|
583
665
|
}
|
|
584
666
|
|
|
585
667
|
function decodeUnsubscribeNamespacePayload(r: BufferReader): Draft14Message {
|
|
586
668
|
const request_id = r.readVarInt();
|
|
587
|
-
return { type:
|
|
669
|
+
return { type: "unsubscribe_namespace", request_id };
|
|
588
670
|
}
|
|
589
671
|
|
|
590
672
|
function decodeFetchPayload(r: BufferReader): Draft14Message {
|
|
@@ -595,7 +677,16 @@ function decodeFetchPayload(r: BufferReader): Draft14Message {
|
|
|
595
677
|
const start_object = r.readVarInt();
|
|
596
678
|
const end_group = r.readVarInt();
|
|
597
679
|
const parameters = decodeParams(r);
|
|
598
|
-
return {
|
|
680
|
+
return {
|
|
681
|
+
type: "fetch",
|
|
682
|
+
request_id,
|
|
683
|
+
track_namespace,
|
|
684
|
+
track_name,
|
|
685
|
+
start_group,
|
|
686
|
+
start_object,
|
|
687
|
+
end_group,
|
|
688
|
+
parameters,
|
|
689
|
+
};
|
|
599
690
|
}
|
|
600
691
|
|
|
601
692
|
function decodeFetchOkPayload(r: BufferReader): Draft14Message {
|
|
@@ -603,19 +694,19 @@ function decodeFetchOkPayload(r: BufferReader): Draft14Message {
|
|
|
603
694
|
const track_alias = r.readVarInt();
|
|
604
695
|
const end_of_track = r.readVarInt();
|
|
605
696
|
const parameters = decodeParams(r);
|
|
606
|
-
return { type:
|
|
697
|
+
return { type: "fetch_ok", request_id, track_alias, end_of_track, parameters };
|
|
607
698
|
}
|
|
608
699
|
|
|
609
700
|
function decodeFetchErrorPayload(r: BufferReader): Draft14Message {
|
|
610
701
|
const request_id = r.readVarInt();
|
|
611
702
|
const error_code = r.readVarInt();
|
|
612
703
|
const reason_phrase = r.readString();
|
|
613
|
-
return { type:
|
|
704
|
+
return { type: "fetch_error", request_id, error_code, reason_phrase };
|
|
614
705
|
}
|
|
615
706
|
|
|
616
707
|
function decodeFetchCancelPayload(r: BufferReader): Draft14Message {
|
|
617
708
|
const request_id = r.readVarInt();
|
|
618
|
-
return { type:
|
|
709
|
+
return { type: "fetch_cancel", request_id };
|
|
619
710
|
}
|
|
620
711
|
|
|
621
712
|
function decodeTrackStatusPayload(r: BufferReader): Draft14Message {
|
|
@@ -623,7 +714,7 @@ function decodeTrackStatusPayload(r: BufferReader): Draft14Message {
|
|
|
623
714
|
const track_namespace = r.readTuple();
|
|
624
715
|
const track_name = r.readString();
|
|
625
716
|
const parameters = decodeParams(r);
|
|
626
|
-
return { type:
|
|
717
|
+
return { type: "track_status", request_id, track_namespace, track_name, parameters };
|
|
627
718
|
}
|
|
628
719
|
|
|
629
720
|
function decodeTrackStatusOkPayload(r: BufferReader): Draft14Message {
|
|
@@ -642,7 +733,7 @@ function decodeTrackStatusOkPayload(r: BufferReader): Draft14Message {
|
|
|
642
733
|
const parameters = decodeParams(r);
|
|
643
734
|
|
|
644
735
|
return {
|
|
645
|
-
type:
|
|
736
|
+
type: "track_status_ok",
|
|
646
737
|
request_id,
|
|
647
738
|
status_code,
|
|
648
739
|
parameters,
|
|
@@ -654,22 +745,22 @@ function decodeTrackStatusErrorPayload(r: BufferReader): Draft14Message {
|
|
|
654
745
|
const request_id = r.readVarInt();
|
|
655
746
|
const error_code = r.readVarInt();
|
|
656
747
|
const reason_phrase = r.readString();
|
|
657
|
-
return { type:
|
|
748
|
+
return { type: "track_status_error", request_id, error_code, reason_phrase };
|
|
658
749
|
}
|
|
659
750
|
|
|
660
751
|
function decodeGoAwayPayload(r: BufferReader): Draft14Message {
|
|
661
752
|
const new_session_uri = r.readString();
|
|
662
|
-
return { type:
|
|
753
|
+
return { type: "goaway", new_session_uri };
|
|
663
754
|
}
|
|
664
755
|
|
|
665
756
|
function decodeMaxRequestIdPayload(r: BufferReader): Draft14Message {
|
|
666
757
|
const request_id = r.readVarInt();
|
|
667
|
-
return { type:
|
|
758
|
+
return { type: "max_request_id", request_id };
|
|
668
759
|
}
|
|
669
760
|
|
|
670
761
|
function decodeRequestsBlockedPayload(r: BufferReader): Draft14Message {
|
|
671
762
|
const request_id = r.readVarInt();
|
|
672
|
-
return { type:
|
|
763
|
+
return { type: "requests_blocked", request_id };
|
|
673
764
|
}
|
|
674
765
|
|
|
675
766
|
// ─── Payload dispatch tables ───────────────────────────────────────────────────
|
|
@@ -739,36 +830,66 @@ export function encodeMessage(message: Draft14Message): Uint8Array {
|
|
|
739
830
|
|
|
740
831
|
function encodePayload(msg: Draft14Message, w: BufferWriter): void {
|
|
741
832
|
switch (msg.type) {
|
|
742
|
-
case
|
|
743
|
-
|
|
744
|
-
case
|
|
745
|
-
|
|
746
|
-
case
|
|
747
|
-
|
|
748
|
-
case
|
|
749
|
-
|
|
750
|
-
case
|
|
751
|
-
|
|
752
|
-
case
|
|
753
|
-
|
|
754
|
-
case
|
|
755
|
-
|
|
756
|
-
case
|
|
757
|
-
|
|
758
|
-
case
|
|
759
|
-
|
|
760
|
-
case
|
|
761
|
-
|
|
762
|
-
case
|
|
763
|
-
|
|
764
|
-
case
|
|
765
|
-
|
|
766
|
-
case
|
|
767
|
-
|
|
768
|
-
case
|
|
769
|
-
|
|
770
|
-
case
|
|
771
|
-
|
|
833
|
+
case "client_setup":
|
|
834
|
+
return encodeClientSetupPayload(msg, w);
|
|
835
|
+
case "server_setup":
|
|
836
|
+
return encodeServerSetupPayload(msg, w);
|
|
837
|
+
case "subscribe":
|
|
838
|
+
return encodeSubscribePayload(msg, w);
|
|
839
|
+
case "subscribe_ok":
|
|
840
|
+
return encodeSubscribeOkPayload(msg, w);
|
|
841
|
+
case "subscribe_update":
|
|
842
|
+
return encodeSubscribeUpdatePayload(msg, w);
|
|
843
|
+
case "subscribe_error":
|
|
844
|
+
return encodeSubscribeErrorPayload(msg, w);
|
|
845
|
+
case "unsubscribe":
|
|
846
|
+
return encodeUnsubscribePayload(msg, w);
|
|
847
|
+
case "publish":
|
|
848
|
+
return encodePublishPayload(msg, w);
|
|
849
|
+
case "publish_ok":
|
|
850
|
+
return encodePublishOkPayload(msg, w);
|
|
851
|
+
case "publish_error":
|
|
852
|
+
return encodePublishErrorPayload(msg, w);
|
|
853
|
+
case "publish_done":
|
|
854
|
+
return encodePublishDonePayload(msg, w);
|
|
855
|
+
case "publish_namespace":
|
|
856
|
+
return encodePublishNamespacePayload(msg, w);
|
|
857
|
+
case "publish_namespace_ok":
|
|
858
|
+
return encodePublishNamespaceOkPayload(msg, w);
|
|
859
|
+
case "publish_namespace_error":
|
|
860
|
+
return encodePublishNamespaceErrorPayload(msg, w);
|
|
861
|
+
case "publish_namespace_done":
|
|
862
|
+
return encodePublishNamespaceDonePayload(msg, w);
|
|
863
|
+
case "publish_namespace_cancel":
|
|
864
|
+
return encodePublishNamespaceCancelPayload(msg, w);
|
|
865
|
+
case "subscribe_namespace":
|
|
866
|
+
return encodeSubscribeNamespacePayload(msg, w);
|
|
867
|
+
case "subscribe_namespace_ok":
|
|
868
|
+
return encodeSubscribeNamespaceOkPayload(msg, w);
|
|
869
|
+
case "subscribe_namespace_error":
|
|
870
|
+
return encodeSubscribeNamespaceErrorPayload(msg, w);
|
|
871
|
+
case "unsubscribe_namespace":
|
|
872
|
+
return encodeUnsubscribeNamespacePayload(msg, w);
|
|
873
|
+
case "fetch":
|
|
874
|
+
return encodeFetchPayload(msg, w);
|
|
875
|
+
case "fetch_ok":
|
|
876
|
+
return encodeFetchOkPayload(msg, w);
|
|
877
|
+
case "fetch_error":
|
|
878
|
+
return encodeFetchErrorPayload(msg, w);
|
|
879
|
+
case "fetch_cancel":
|
|
880
|
+
return encodeFetchCancelPayload(msg, w);
|
|
881
|
+
case "track_status":
|
|
882
|
+
return encodeTrackStatusPayload(msg, w);
|
|
883
|
+
case "track_status_ok":
|
|
884
|
+
return encodeTrackStatusOkPayload(msg, w);
|
|
885
|
+
case "track_status_error":
|
|
886
|
+
return encodeTrackStatusErrorPayload(msg, w);
|
|
887
|
+
case "goaway":
|
|
888
|
+
return encodeGoAwayPayload(msg, w);
|
|
889
|
+
case "max_request_id":
|
|
890
|
+
return encodeMaxRequestIdPayload(msg, w);
|
|
891
|
+
case "requests_blocked":
|
|
892
|
+
return encodeRequestsBlockedPayload(msg, w);
|
|
772
893
|
default: {
|
|
773
894
|
const _exhaustive: never = msg;
|
|
774
895
|
throw new Error(`Unhandled message type: ${(_exhaustive as Draft14Message).type}`);
|
|
@@ -797,7 +918,11 @@ export function decodeMessage(bytes: Uint8Array): DecodeResult<Draft14Message> {
|
|
|
797
918
|
if (!decoder) {
|
|
798
919
|
return {
|
|
799
920
|
ok: false,
|
|
800
|
-
error: new DecodeError(
|
|
921
|
+
error: new DecodeError(
|
|
922
|
+
"UNKNOWN_MESSAGE_TYPE",
|
|
923
|
+
`Unknown message type ID: 0x${typeId.toString(16)}`,
|
|
924
|
+
0,
|
|
925
|
+
),
|
|
801
926
|
};
|
|
802
927
|
}
|
|
803
928
|
|
|
@@ -867,7 +992,14 @@ export function decodeSubgroupStream(bytes: Uint8Array): DecodeResult<SubgroupSt
|
|
|
867
992
|
const r = new BufferReader(bytes);
|
|
868
993
|
const streamType = r.readVarInt();
|
|
869
994
|
if (streamType !== 0n) {
|
|
870
|
-
return {
|
|
995
|
+
return {
|
|
996
|
+
ok: false,
|
|
997
|
+
error: new DecodeError(
|
|
998
|
+
"CONSTRAINT_VIOLATION",
|
|
999
|
+
`Expected subgroup stream type 0, got ${streamType}`,
|
|
1000
|
+
0,
|
|
1001
|
+
),
|
|
1002
|
+
};
|
|
871
1003
|
}
|
|
872
1004
|
const trackAlias = r.readVarInt();
|
|
873
1005
|
const groupId = r.readVarInt();
|
|
@@ -878,11 +1010,11 @@ export function decodeSubgroupStream(bytes: Uint8Array): DecodeResult<SubgroupSt
|
|
|
878
1010
|
const objectId = r.readVarInt();
|
|
879
1011
|
const payloadLength = Number(r.readVarInt());
|
|
880
1012
|
const payload = r.readBytes(payloadLength);
|
|
881
|
-
objects.push({ type:
|
|
1013
|
+
objects.push({ type: "object", objectId, payloadLength, payload });
|
|
882
1014
|
}
|
|
883
1015
|
return {
|
|
884
1016
|
ok: true,
|
|
885
|
-
value: { type:
|
|
1017
|
+
value: { type: "subgroup", trackAlias, groupId, subgroupId, publisherPriority, objects },
|
|
886
1018
|
bytesRead: r.offset,
|
|
887
1019
|
};
|
|
888
1020
|
} catch (e) {
|
|
@@ -904,7 +1036,7 @@ export function decodeDatagram(bytes: Uint8Array): DecodeResult<DatagramObject>
|
|
|
904
1036
|
const payload = r.readBytes(payloadLength);
|
|
905
1037
|
return {
|
|
906
1038
|
ok: true,
|
|
907
|
-
value: { type:
|
|
1039
|
+
value: { type: "datagram", trackAlias, groupId, objectId, payloadLength, payload },
|
|
908
1040
|
bytesRead: r.offset,
|
|
909
1041
|
};
|
|
910
1042
|
} catch (e) {
|
|
@@ -921,7 +1053,14 @@ export function decodeFetchStream(bytes: Uint8Array): DecodeResult<FetchStream>
|
|
|
921
1053
|
const r = new BufferReader(bytes);
|
|
922
1054
|
const streamType = r.readVarInt();
|
|
923
1055
|
if (streamType !== 2n) {
|
|
924
|
-
return {
|
|
1056
|
+
return {
|
|
1057
|
+
ok: false,
|
|
1058
|
+
error: new DecodeError(
|
|
1059
|
+
"CONSTRAINT_VIOLATION",
|
|
1060
|
+
`Expected fetch stream type 2, got ${streamType}`,
|
|
1061
|
+
0,
|
|
1062
|
+
),
|
|
1063
|
+
};
|
|
925
1064
|
}
|
|
926
1065
|
const subscribeRequestId = r.readVarInt();
|
|
927
1066
|
const objects: ObjectPayload[] = [];
|
|
@@ -929,11 +1068,11 @@ export function decodeFetchStream(bytes: Uint8Array): DecodeResult<FetchStream>
|
|
|
929
1068
|
const objectId = r.readVarInt();
|
|
930
1069
|
const payloadLength = Number(r.readVarInt());
|
|
931
1070
|
const payload = r.readBytes(payloadLength);
|
|
932
|
-
objects.push({ type:
|
|
1071
|
+
objects.push({ type: "object", objectId, payloadLength, payload });
|
|
933
1072
|
}
|
|
934
1073
|
return {
|
|
935
1074
|
ok: true,
|
|
936
|
-
value: { type:
|
|
1075
|
+
value: { type: "fetch", subscribeRequestId, objects },
|
|
937
1076
|
bytesRead: r.offset,
|
|
938
1077
|
};
|
|
939
1078
|
} catch (e) {
|
|
@@ -946,15 +1085,15 @@ export function decodeFetchStream(bytes: Uint8Array): DecodeResult<FetchStream>
|
|
|
946
1085
|
* Decode a data stream, dispatching by stream type.
|
|
947
1086
|
*/
|
|
948
1087
|
export function decodeDataStream(
|
|
949
|
-
streamType:
|
|
1088
|
+
streamType: "subgroup" | "datagram" | "fetch",
|
|
950
1089
|
bytes: Uint8Array,
|
|
951
1090
|
): DecodeResult<Draft14DataStream> {
|
|
952
1091
|
switch (streamType) {
|
|
953
|
-
case
|
|
1092
|
+
case "subgroup":
|
|
954
1093
|
return decodeSubgroupStream(bytes);
|
|
955
|
-
case
|
|
1094
|
+
case "datagram":
|
|
956
1095
|
return decodeDatagram(bytes);
|
|
957
|
-
case
|
|
1096
|
+
case "fetch":
|
|
958
1097
|
return decodeFetchStream(bytes);
|
|
959
1098
|
default: {
|
|
960
1099
|
const _exhaustive: never = streamType;
|
|
@@ -985,7 +1124,7 @@ export function createStreamDecoder(): TransformStream<Uint8Array, Draft14Messag
|
|
|
985
1124
|
while (buffer.length > 0) {
|
|
986
1125
|
const result = decodeMessage(buffer);
|
|
987
1126
|
if (!result.ok) {
|
|
988
|
-
if (result.error.code ===
|
|
1127
|
+
if (result.error.code === "UNEXPECTED_END") {
|
|
989
1128
|
// Need more data -- wait for next chunk
|
|
990
1129
|
break;
|
|
991
1130
|
}
|
|
@@ -1003,7 +1142,7 @@ export function createStreamDecoder(): TransformStream<Uint8Array, Draft14Messag
|
|
|
1003
1142
|
// If there is remaining data in the buffer, it is a truncated message
|
|
1004
1143
|
if (buffer.length > 0) {
|
|
1005
1144
|
controller.error(
|
|
1006
|
-
new DecodeError(
|
|
1145
|
+
new DecodeError("UNEXPECTED_END", "Stream ended with incomplete message data", 0),
|
|
1007
1146
|
);
|
|
1008
1147
|
}
|
|
1009
1148
|
},
|
|
@@ -1018,7 +1157,10 @@ export function createStreamDecoder(): TransformStream<Uint8Array, Draft14Messag
|
|
|
1018
1157
|
* varint(0x00) + varint(trackAlias) + varint(groupId) + varint(subgroupId)
|
|
1019
1158
|
* + uint8(publisherPriority) + [varint(objectId) + varint(payloadLength) + bytes(payload)]*
|
|
1020
1159
|
*/
|
|
1021
|
-
export function createSubgroupStreamDecoder(): TransformStream<
|
|
1160
|
+
export function createSubgroupStreamDecoder(): TransformStream<
|
|
1161
|
+
Uint8Array,
|
|
1162
|
+
SubgroupStreamHeader | ObjectPayload
|
|
1163
|
+
> {
|
|
1022
1164
|
let buffer = new Uint8Array(0);
|
|
1023
1165
|
let headerEmitted = false;
|
|
1024
1166
|
|
|
@@ -1036,7 +1178,11 @@ export function createSubgroupStreamDecoder(): TransformStream<Uint8Array, Subgr
|
|
|
1036
1178
|
const streamType = r.readVarInt();
|
|
1037
1179
|
if (streamType !== 0n) {
|
|
1038
1180
|
controller.error(
|
|
1039
|
-
new DecodeError(
|
|
1181
|
+
new DecodeError(
|
|
1182
|
+
"CONSTRAINT_VIOLATION",
|
|
1183
|
+
`Expected subgroup stream type 0, got ${streamType}`,
|
|
1184
|
+
0,
|
|
1185
|
+
),
|
|
1040
1186
|
);
|
|
1041
1187
|
return;
|
|
1042
1188
|
}
|
|
@@ -1046,7 +1192,7 @@ export function createSubgroupStreamDecoder(): TransformStream<Uint8Array, Subgr
|
|
|
1046
1192
|
const publisherPriority = r.readUint8();
|
|
1047
1193
|
|
|
1048
1194
|
controller.enqueue({
|
|
1049
|
-
type:
|
|
1195
|
+
type: "subgroup_header",
|
|
1050
1196
|
trackAlias,
|
|
1051
1197
|
groupId,
|
|
1052
1198
|
subgroupId,
|
|
@@ -1055,7 +1201,7 @@ export function createSubgroupStreamDecoder(): TransformStream<Uint8Array, Subgr
|
|
|
1055
1201
|
headerEmitted = true;
|
|
1056
1202
|
buffer = buffer.slice(r.offset);
|
|
1057
1203
|
} catch (e) {
|
|
1058
|
-
if (e instanceof DecodeError && e.code ===
|
|
1204
|
+
if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
|
|
1059
1205
|
return; // Need more data
|
|
1060
1206
|
}
|
|
1061
1207
|
controller.error(e);
|
|
@@ -1070,10 +1216,10 @@ export function createSubgroupStreamDecoder(): TransformStream<Uint8Array, Subgr
|
|
|
1070
1216
|
const objectId = r.readVarInt();
|
|
1071
1217
|
const payloadLength = Number(r.readVarInt());
|
|
1072
1218
|
const payload = r.readBytes(payloadLength);
|
|
1073
|
-
controller.enqueue({ type:
|
|
1219
|
+
controller.enqueue({ type: "object", objectId, payloadLength, payload });
|
|
1074
1220
|
buffer = buffer.slice(r.offset);
|
|
1075
1221
|
} catch (e) {
|
|
1076
|
-
if (e instanceof DecodeError && e.code ===
|
|
1222
|
+
if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
|
|
1077
1223
|
break; // Need more data
|
|
1078
1224
|
}
|
|
1079
1225
|
controller.error(e);
|
|
@@ -1084,9 +1230,7 @@ export function createSubgroupStreamDecoder(): TransformStream<Uint8Array, Subgr
|
|
|
1084
1230
|
|
|
1085
1231
|
flush(controller) {
|
|
1086
1232
|
if (buffer.length > 0) {
|
|
1087
|
-
controller.error(
|
|
1088
|
-
new DecodeError('UNEXPECTED_END', 'Stream ended with incomplete data', 0),
|
|
1089
|
-
);
|
|
1233
|
+
controller.error(new DecodeError("UNEXPECTED_END", "Stream ended with incomplete data", 0));
|
|
1090
1234
|
}
|
|
1091
1235
|
},
|
|
1092
1236
|
});
|
|
@@ -1100,7 +1244,10 @@ export function createSubgroupStreamDecoder(): TransformStream<Uint8Array, Subgr
|
|
|
1100
1244
|
* varint(0x02) + varint(subscribeRequestId)
|
|
1101
1245
|
* + [varint(objectId) + varint(payloadLength) + bytes(payload)]*
|
|
1102
1246
|
*/
|
|
1103
|
-
export function createFetchStreamDecoder(): TransformStream<
|
|
1247
|
+
export function createFetchStreamDecoder(): TransformStream<
|
|
1248
|
+
Uint8Array,
|
|
1249
|
+
FetchStreamHeader | ObjectPayload
|
|
1250
|
+
> {
|
|
1104
1251
|
let buffer = new Uint8Array(0);
|
|
1105
1252
|
let headerEmitted = false;
|
|
1106
1253
|
|
|
@@ -1118,20 +1265,24 @@ export function createFetchStreamDecoder(): TransformStream<Uint8Array, FetchStr
|
|
|
1118
1265
|
const streamType = r.readVarInt();
|
|
1119
1266
|
if (streamType !== 2n) {
|
|
1120
1267
|
controller.error(
|
|
1121
|
-
new DecodeError(
|
|
1268
|
+
new DecodeError(
|
|
1269
|
+
"CONSTRAINT_VIOLATION",
|
|
1270
|
+
`Expected fetch stream type 2, got ${streamType}`,
|
|
1271
|
+
0,
|
|
1272
|
+
),
|
|
1122
1273
|
);
|
|
1123
1274
|
return;
|
|
1124
1275
|
}
|
|
1125
1276
|
const subscribeRequestId = r.readVarInt();
|
|
1126
1277
|
|
|
1127
1278
|
controller.enqueue({
|
|
1128
|
-
type:
|
|
1279
|
+
type: "fetch_header",
|
|
1129
1280
|
subscribeRequestId,
|
|
1130
1281
|
});
|
|
1131
1282
|
headerEmitted = true;
|
|
1132
1283
|
buffer = buffer.slice(r.offset);
|
|
1133
1284
|
} catch (e) {
|
|
1134
|
-
if (e instanceof DecodeError && e.code ===
|
|
1285
|
+
if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
|
|
1135
1286
|
return; // Need more data
|
|
1136
1287
|
}
|
|
1137
1288
|
controller.error(e);
|
|
@@ -1146,10 +1297,10 @@ export function createFetchStreamDecoder(): TransformStream<Uint8Array, FetchStr
|
|
|
1146
1297
|
const objectId = r.readVarInt();
|
|
1147
1298
|
const payloadLength = Number(r.readVarInt());
|
|
1148
1299
|
const payload = r.readBytes(payloadLength);
|
|
1149
|
-
controller.enqueue({ type:
|
|
1300
|
+
controller.enqueue({ type: "object", objectId, payloadLength, payload });
|
|
1150
1301
|
buffer = buffer.slice(r.offset);
|
|
1151
1302
|
} catch (e) {
|
|
1152
|
-
if (e instanceof DecodeError && e.code ===
|
|
1303
|
+
if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
|
|
1153
1304
|
break; // Need more data
|
|
1154
1305
|
}
|
|
1155
1306
|
controller.error(e);
|
|
@@ -1160,9 +1311,7 @@ export function createFetchStreamDecoder(): TransformStream<Uint8Array, FetchStr
|
|
|
1160
1311
|
|
|
1161
1312
|
flush(controller) {
|
|
1162
1313
|
if (buffer.length > 0) {
|
|
1163
|
-
controller.error(
|
|
1164
|
-
new DecodeError('UNEXPECTED_END', 'Stream ended with incomplete data', 0),
|
|
1165
|
-
);
|
|
1314
|
+
controller.error(new DecodeError("UNEXPECTED_END", "Stream ended with incomplete data", 0));
|
|
1166
1315
|
}
|
|
1167
1316
|
},
|
|
1168
1317
|
});
|
|
@@ -1175,7 +1324,7 @@ export function createFetchStreamDecoder(): TransformStream<Uint8Array, FetchStr
|
|
|
1175
1324
|
*/
|
|
1176
1325
|
export function createDataStreamDecoder(): TransformStream<Uint8Array, DataStreamEvent> {
|
|
1177
1326
|
let buffer = new Uint8Array(0);
|
|
1178
|
-
let detectedType:
|
|
1327
|
+
let detectedType: "subgroup" | "fetch" | null = null;
|
|
1179
1328
|
let headerEmitted = false;
|
|
1180
1329
|
|
|
1181
1330
|
// We peek the stream type then create the appropriate inner decoder,
|
|
@@ -1195,18 +1344,18 @@ export function createDataStreamDecoder(): TransformStream<Uint8Array, DataStrea
|
|
|
1195
1344
|
const r = new BufferReader(buffer);
|
|
1196
1345
|
const streamType = r.readVarInt();
|
|
1197
1346
|
if (streamType === 0n) {
|
|
1198
|
-
detectedType =
|
|
1347
|
+
detectedType = "subgroup";
|
|
1199
1348
|
} else if (streamType === 2n) {
|
|
1200
|
-
detectedType =
|
|
1349
|
+
detectedType = "fetch";
|
|
1201
1350
|
} else {
|
|
1202
1351
|
controller.error(
|
|
1203
|
-
new DecodeError(
|
|
1352
|
+
new DecodeError("CONSTRAINT_VIOLATION", `Unknown data stream type: ${streamType}`, 0),
|
|
1204
1353
|
);
|
|
1205
1354
|
return;
|
|
1206
1355
|
}
|
|
1207
1356
|
// Do NOT advance buffer - the inner header parser expects the stream type
|
|
1208
1357
|
} catch (e) {
|
|
1209
|
-
if (e instanceof DecodeError && e.code ===
|
|
1358
|
+
if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
|
|
1210
1359
|
return; // Need more data
|
|
1211
1360
|
}
|
|
1212
1361
|
controller.error(e);
|
|
@@ -1214,7 +1363,7 @@ export function createDataStreamDecoder(): TransformStream<Uint8Array, DataStrea
|
|
|
1214
1363
|
}
|
|
1215
1364
|
}
|
|
1216
1365
|
|
|
1217
|
-
if (detectedType ===
|
|
1366
|
+
if (detectedType === "subgroup") {
|
|
1218
1367
|
// Parse subgroup header if not yet emitted
|
|
1219
1368
|
if (!headerEmitted) {
|
|
1220
1369
|
try {
|
|
@@ -1225,7 +1374,7 @@ export function createDataStreamDecoder(): TransformStream<Uint8Array, DataStrea
|
|
|
1225
1374
|
const subgroupId = r.readVarInt();
|
|
1226
1375
|
const publisherPriority = r.readUint8();
|
|
1227
1376
|
controller.enqueue({
|
|
1228
|
-
type:
|
|
1377
|
+
type: "subgroup_header",
|
|
1229
1378
|
trackAlias,
|
|
1230
1379
|
groupId,
|
|
1231
1380
|
subgroupId,
|
|
@@ -1234,7 +1383,7 @@ export function createDataStreamDecoder(): TransformStream<Uint8Array, DataStrea
|
|
|
1234
1383
|
headerEmitted = true;
|
|
1235
1384
|
buffer = buffer.slice(r.offset);
|
|
1236
1385
|
} catch (e) {
|
|
1237
|
-
if (e instanceof DecodeError && e.code ===
|
|
1386
|
+
if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
|
|
1238
1387
|
return;
|
|
1239
1388
|
}
|
|
1240
1389
|
controller.error(e);
|
|
@@ -1249,13 +1398,13 @@ export function createDataStreamDecoder(): TransformStream<Uint8Array, DataStrea
|
|
|
1249
1398
|
r.readVarInt(); // stream type (0x02)
|
|
1250
1399
|
const subscribeRequestId = r.readVarInt();
|
|
1251
1400
|
controller.enqueue({
|
|
1252
|
-
type:
|
|
1401
|
+
type: "fetch_header",
|
|
1253
1402
|
subscribeRequestId,
|
|
1254
1403
|
});
|
|
1255
1404
|
headerEmitted = true;
|
|
1256
1405
|
buffer = buffer.slice(r.offset);
|
|
1257
1406
|
} catch (e) {
|
|
1258
|
-
if (e instanceof DecodeError && e.code ===
|
|
1407
|
+
if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
|
|
1259
1408
|
return;
|
|
1260
1409
|
}
|
|
1261
1410
|
controller.error(e);
|
|
@@ -1271,10 +1420,10 @@ export function createDataStreamDecoder(): TransformStream<Uint8Array, DataStrea
|
|
|
1271
1420
|
const objectId = r.readVarInt();
|
|
1272
1421
|
const payloadLength = Number(r.readVarInt());
|
|
1273
1422
|
const payload = r.readBytes(payloadLength);
|
|
1274
|
-
controller.enqueue({ type:
|
|
1423
|
+
controller.enqueue({ type: "object", objectId, payloadLength, payload });
|
|
1275
1424
|
buffer = buffer.slice(r.offset);
|
|
1276
1425
|
} catch (e) {
|
|
1277
|
-
if (e instanceof DecodeError && e.code ===
|
|
1426
|
+
if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
|
|
1278
1427
|
break;
|
|
1279
1428
|
}
|
|
1280
1429
|
controller.error(e);
|
|
@@ -1285,9 +1434,7 @@ export function createDataStreamDecoder(): TransformStream<Uint8Array, DataStrea
|
|
|
1285
1434
|
|
|
1286
1435
|
flush(controller) {
|
|
1287
1436
|
if (buffer.length > 0) {
|
|
1288
|
-
controller.error(
|
|
1289
|
-
new DecodeError('UNEXPECTED_END', 'Stream ended with incomplete data', 0),
|
|
1290
|
-
);
|
|
1437
|
+
controller.error(new DecodeError("UNEXPECTED_END", "Stream ended with incomplete data", 0));
|
|
1291
1438
|
}
|
|
1292
1439
|
},
|
|
1293
1440
|
});
|
|
@@ -1296,14 +1443,17 @@ export function createDataStreamDecoder(): TransformStream<Uint8Array, DataStrea
|
|
|
1296
1443
|
// ─── Codec Factory ─────────────────────────────────────────────────────────────
|
|
1297
1444
|
|
|
1298
1445
|
export interface Draft14Codec extends BaseCodec<Draft14Message> {
|
|
1299
|
-
readonly draft:
|
|
1446
|
+
readonly draft: "draft-ietf-moq-transport-14";
|
|
1300
1447
|
encodeSubgroupStream(stream: SubgroupStream): Uint8Array;
|
|
1301
1448
|
encodeDatagram(dg: DatagramObject): Uint8Array;
|
|
1302
1449
|
encodeFetchStream(stream: FetchStream): Uint8Array;
|
|
1303
1450
|
decodeSubgroupStream(bytes: Uint8Array): DecodeResult<SubgroupStream>;
|
|
1304
1451
|
decodeDatagram(bytes: Uint8Array): DecodeResult<DatagramObject>;
|
|
1305
1452
|
decodeFetchStream(bytes: Uint8Array): DecodeResult<FetchStream>;
|
|
1306
|
-
decodeDataStream(
|
|
1453
|
+
decodeDataStream(
|
|
1454
|
+
streamType: "subgroup" | "datagram" | "fetch",
|
|
1455
|
+
bytes: Uint8Array,
|
|
1456
|
+
): DecodeResult<Draft14DataStream>;
|
|
1307
1457
|
createStreamDecoder(): TransformStream<Uint8Array, Draft14Message>;
|
|
1308
1458
|
createSubgroupStreamDecoder(): TransformStream<Uint8Array, SubgroupStreamHeader | ObjectPayload>;
|
|
1309
1459
|
createFetchStreamDecoder(): TransformStream<Uint8Array, FetchStreamHeader | ObjectPayload>;
|
|
@@ -1312,7 +1462,7 @@ export interface Draft14Codec extends BaseCodec<Draft14Message> {
|
|
|
1312
1462
|
|
|
1313
1463
|
export function createDraft14Codec(): Draft14Codec {
|
|
1314
1464
|
return {
|
|
1315
|
-
draft:
|
|
1465
|
+
draft: "draft-ietf-moq-transport-14",
|
|
1316
1466
|
encodeMessage,
|
|
1317
1467
|
decodeMessage,
|
|
1318
1468
|
encodeSubgroupStream,
|