@moqtap/codec 0.1.1 → 0.2.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.
- package/README.md +99 -95
- package/dist/chunk-4RIXXEII.js +1275 -0
- package/dist/chunk-4XYGE53S.cjs +698 -0
- package/dist/chunk-4YJANAXU.cjs +1109 -0
- package/dist/chunk-6AEHWULA.cjs +641 -0
- package/dist/chunk-7DUBLRXC.js +680 -0
- package/dist/{chunk-YBZD3DU5.cjs → chunk-7IVGHMKJ.cjs} +44 -42
- package/dist/chunk-BISI45MN.cjs +680 -0
- package/dist/{chunk-NLYTRGXA.cjs → chunk-BPNL5YFQ.cjs} +5 -5
- package/dist/chunk-CXDHOMHG.js +1097 -0
- package/dist/chunk-DUBCL3WF.cjs +749 -0
- package/dist/chunk-DWK5ZQZ4.js +642 -0
- package/dist/chunk-E6E3NQYU.js +680 -0
- package/dist/chunk-EFM5T7OM.js +698 -0
- package/dist/chunk-ENURAVHI.cjs +680 -0
- package/dist/{chunk-Z66WDWHI.js → chunk-FXZ2MYKJ.js} +127 -1
- package/dist/chunk-G26SJ6XS.cjs +1341 -0
- package/dist/chunk-G7GI7LJA.js +737 -0
- package/dist/chunk-GXEW4COZ.cjs +737 -0
- package/dist/chunk-HSVYF6XX.cjs +1361 -0
- package/dist/chunk-IBVM4DMJ.cjs +1097 -0
- package/dist/chunk-IV2H5CFI.cjs +1275 -0
- package/dist/chunk-IV2HRJVT.js +1198 -0
- package/dist/chunk-JSQM2MG3.js +680 -0
- package/dist/chunk-K4OLITS2.cjs +1055 -0
- package/dist/{chunk-UOBWHJA5.js → chunk-KFTCU2P6.js} +2 -3
- package/dist/chunk-LH4NTURO.js +1361 -0
- package/dist/{chunk-YTXLWKOR.js → chunk-MFAP7R6L.js} +1 -1
- package/dist/chunk-NGVE2RZT.js +1097 -0
- package/dist/chunk-NUX5BHWO.js +1341 -0
- package/dist/chunk-PJRA2TQ5.js +1055 -0
- package/dist/chunk-RVJAGE4S.cjs +1198 -0
- package/dist/{chunk-QYG6KGOV.cjs → chunk-RWQ43Z4F.cjs} +2 -3
- package/dist/chunk-RZHAPEXO.js +749 -0
- package/dist/chunk-ST24APEO.js +1109 -0
- package/dist/chunk-SYHW3FLI.cjs +642 -0
- package/dist/chunk-TLYNOOQP.cjs +432 -0
- package/dist/{chunk-IXHOBNXA.js → chunk-TMNGRIPL.js} +43 -41
- package/dist/chunk-UNS34PTA.cjs +680 -0
- package/dist/chunk-UR6JKS56.js +432 -0
- package/dist/{chunk-NPWHHWXT.cjs → chunk-UYXTY6ZQ.cjs} +127 -1
- package/dist/chunk-XUUCOLWU.cjs +1097 -0
- package/dist/chunk-YG5KJESI.js +641 -0
- package/dist/chunk-ZBKE2QRQ.js +1401 -0
- package/dist/chunk-ZSPO2GF2.cjs +1401 -0
- package/dist/codec-95k8CAu5.d.cts +35 -0
- package/dist/codec-AFuOxfsO.d.ts +60 -0
- package/dist/codec-B-UJ5Iow.d.cts +75 -0
- package/dist/codec-BC5jfvMb.d.ts +35 -0
- package/dist/codec-BECYPfY8.d.ts +35 -0
- package/dist/codec-BsPU1vNC.d.ts +39 -0
- package/dist/codec-BvpuF-6u.d.cts +39 -0
- package/dist/codec-C8jZI5Cx.d.cts +39 -0
- package/dist/codec-CAevkgf5.d.cts +33 -0
- package/dist/codec-CSUqCrRs.d.ts +39 -0
- package/dist/codec-C_HMXNK_.d.ts +33 -0
- package/dist/{codec-Bvr7rFtj.d.cts → codec-CpuvYTSV.d.cts} +1 -1
- package/dist/codec-D0x8-SCw.d.cts +35 -0
- package/dist/codec-D7ARhpG1.d.ts +75 -0
- package/dist/codec-DNAUGshO.d.cts +60 -0
- package/dist/codec-DPx_QNn0.d.ts +31 -0
- package/dist/{codec-B2mc2g3i.d.ts → codec-DRhCx_hw.d.ts} +1 -1
- package/dist/codec-Db7YPe3l.d.ts +31 -0
- package/dist/codec-axkJpb7D.d.cts +31 -0
- package/dist/codec-ujAbFaep.d.cts +31 -0
- package/dist/draft10-session.cjs +6 -0
- package/dist/draft10-session.d.cts +8 -0
- package/dist/draft10-session.d.ts +8 -0
- package/dist/draft10-session.js +6 -0
- package/dist/draft10.cjs +115 -0
- package/dist/draft10.d.cts +95 -0
- package/dist/draft10.d.ts +95 -0
- package/dist/draft10.js +115 -0
- package/dist/draft11-session.cjs +6 -0
- package/dist/draft11-session.d.cts +8 -0
- package/dist/draft11-session.d.ts +8 -0
- package/dist/draft11-session.js +6 -0
- package/dist/draft11.cjs +109 -0
- package/dist/draft11.d.cts +99 -0
- package/dist/draft11.d.ts +99 -0
- package/dist/draft11.js +109 -0
- package/dist/draft12-session.cjs +6 -0
- package/dist/draft12-session.d.cts +8 -0
- package/dist/draft12-session.d.ts +8 -0
- package/dist/draft12-session.js +6 -0
- package/dist/draft12.cjs +117 -0
- package/dist/draft12.d.cts +106 -0
- package/dist/draft12.d.ts +106 -0
- package/dist/draft12.js +117 -0
- package/dist/draft13-session.cjs +6 -0
- package/dist/draft13-session.d.cts +8 -0
- package/dist/draft13-session.d.ts +8 -0
- package/dist/draft13-session.js +6 -0
- package/dist/draft13.cjs +119 -0
- package/dist/draft13.d.cts +108 -0
- package/dist/draft13.d.ts +108 -0
- package/dist/draft13.js +119 -0
- package/dist/draft14-session.cjs +2 -2
- package/dist/draft14-session.d.cts +3 -3
- package/dist/draft14-session.d.ts +3 -3
- package/dist/draft14-session.js +1 -1
- package/dist/draft14.cjs +3 -3
- package/dist/draft14.d.cts +16 -4
- package/dist/draft14.d.ts +16 -4
- package/dist/draft14.js +2 -2
- package/dist/draft15-session.cjs +6 -0
- package/dist/draft15-session.d.cts +8 -0
- package/dist/draft15-session.d.ts +8 -0
- package/dist/draft15-session.js +6 -0
- package/dist/draft15.cjs +111 -0
- package/dist/draft15.d.cts +93 -0
- package/dist/draft15.d.ts +93 -0
- package/dist/draft15.js +111 -0
- package/dist/draft16-session.cjs +6 -0
- package/dist/draft16-session.d.cts +8 -0
- package/dist/draft16-session.d.ts +8 -0
- package/dist/draft16-session.js +6 -0
- package/dist/draft16.cjs +113 -0
- package/dist/draft16.d.cts +94 -0
- package/dist/draft16.d.ts +94 -0
- package/dist/draft16.js +113 -0
- package/dist/draft17-session.cjs +8 -0
- package/dist/draft17-session.d.cts +51 -0
- package/dist/draft17-session.d.ts +51 -0
- package/dist/draft17-session.js +8 -0
- package/dist/draft17.cjs +99 -0
- package/dist/draft17.d.cts +40 -0
- package/dist/draft17.d.ts +40 -0
- package/dist/draft17.js +99 -0
- package/dist/draft7-session.cjs +3 -3
- package/dist/draft7-session.d.cts +3 -3
- package/dist/draft7-session.d.ts +3 -3
- package/dist/draft7-session.js +2 -2
- package/dist/draft7.cjs +4 -4
- package/dist/draft7.d.cts +2 -2
- package/dist/draft7.d.ts +2 -2
- package/dist/draft7.js +2 -2
- package/dist/draft8-session.cjs +6 -0
- package/dist/draft8-session.d.cts +8 -0
- package/dist/draft8-session.d.ts +8 -0
- package/dist/draft8-session.js +6 -0
- package/dist/draft8.cjs +115 -0
- package/dist/draft8.d.cts +95 -0
- package/dist/draft8.d.ts +95 -0
- package/dist/draft8.js +115 -0
- package/dist/draft9-session.cjs +6 -0
- package/dist/draft9-session.d.cts +8 -0
- package/dist/draft9-session.d.ts +8 -0
- package/dist/draft9-session.js +6 -0
- package/dist/draft9.cjs +115 -0
- package/dist/draft9.d.cts +95 -0
- package/dist/draft9.d.ts +95 -0
- package/dist/draft9.js +115 -0
- package/dist/index.cjs +76 -4
- package/dist/index.d.cts +68 -5
- package/dist/index.d.ts +68 -5
- package/dist/index.js +76 -4
- package/dist/{session-types-DFjMk4HH.d.ts → session-types-CJIFbTPd.d.ts} +1 -1
- package/dist/{session-types-DW1RSZX_.d.cts → session-types-Cbq8IGCP.d.cts} +1 -1
- package/dist/session.cjs +5 -5
- package/dist/session.d.cts +3 -3
- package/dist/session.d.ts +3 -3
- package/dist/session.js +5 -5
- package/dist/types-4VxSL2Ho.d.cts +261 -0
- package/dist/types-4VxSL2Ho.d.ts +261 -0
- package/dist/types-B2afJZM-.d.cts +236 -0
- package/dist/types-B2afJZM-.d.ts +236 -0
- package/dist/types-Bg6QYNVt.d.cts +290 -0
- package/dist/types-Bg6QYNVt.d.ts +290 -0
- package/dist/types-C_1HrqBl.d.cts +306 -0
- package/dist/types-C_1HrqBl.d.ts +306 -0
- package/dist/types-Cw4WE9dh.d.cts +261 -0
- package/dist/types-Cw4WE9dh.d.ts +261 -0
- package/dist/types-D5gNQiDj.d.cts +261 -0
- package/dist/types-D5gNQiDj.d.ts +261 -0
- package/dist/types-DqCDFqgB.d.cts +230 -0
- package/dist/types-DqCDFqgB.d.ts +230 -0
- package/dist/types-ERexTpT8.d.cts +217 -0
- package/dist/types-ERexTpT8.d.ts +217 -0
- package/dist/{types-DPYE49t0.d.cts → types-QNXoxC9Y.d.cts} +2 -7
- package/dist/{types-DPYE49t0.d.ts → types-QNXoxC9Y.d.ts} +2 -7
- package/dist/types-r-CasCf1.d.cts +262 -0
- package/dist/types-r-CasCf1.d.ts +262 -0
- package/package.json +110 -2
- package/src/core/types.ts +24 -7
- package/src/drafts/draft07/codec.ts +49 -41
- package/src/drafts/draft07/rules.ts +1 -3
- package/src/drafts/draft08/codec.ts +1254 -0
- package/src/drafts/draft08/index.ts +125 -0
- package/src/drafts/draft08/messages.ts +72 -0
- package/src/drafts/draft08/rules.ts +91 -0
- package/src/drafts/draft08/session-fsm.ts +718 -0
- package/src/drafts/draft08/session.ts +26 -0
- package/src/drafts/draft08/types.ts +377 -0
- package/src/drafts/draft09/codec.ts +1235 -0
- package/src/drafts/draft09/index.ts +125 -0
- package/src/drafts/draft09/messages.ts +72 -0
- package/src/drafts/draft09/rules.ts +91 -0
- package/src/drafts/draft09/session-fsm.ts +718 -0
- package/src/drafts/draft09/session.ts +26 -0
- package/src/drafts/draft09/types.ts +376 -0
- package/src/drafts/draft10/codec.ts +1235 -0
- package/src/drafts/draft10/index.ts +125 -0
- package/src/drafts/draft10/messages.ts +72 -0
- package/src/drafts/draft10/rules.ts +91 -0
- package/src/drafts/draft10/session-fsm.ts +718 -0
- package/src/drafts/draft10/session.ts +26 -0
- package/src/drafts/draft10/types.ts +376 -0
- package/src/drafts/draft11/codec.ts +1198 -0
- package/src/drafts/draft11/index.ts +123 -0
- package/src/drafts/draft11/messages.ts +71 -0
- package/src/drafts/draft11/rules.ts +100 -0
- package/src/drafts/draft11/session-fsm.ts +758 -0
- package/src/drafts/draft11/session.ts +26 -0
- package/src/drafts/draft11/types.ts +375 -0
- package/src/drafts/draft12/codec.ts +1354 -0
- package/src/drafts/draft12/index.ts +130 -0
- package/src/drafts/draft12/messages.ts +84 -0
- package/src/drafts/draft12/rules.ts +106 -0
- package/src/drafts/draft12/session-fsm.ts +805 -0
- package/src/drafts/draft12/session.ts +26 -0
- package/src/drafts/draft12/types.ts +414 -0
- package/src/drafts/draft13/codec.ts +1438 -0
- package/src/drafts/draft13/index.ts +132 -0
- package/src/drafts/draft13/messages.ts +86 -0
- package/src/drafts/draft13/rules.ts +108 -0
- package/src/drafts/draft13/session-fsm.ts +819 -0
- package/src/drafts/draft13/session.ts +26 -0
- package/src/drafts/draft13/types.ts +433 -0
- package/src/drafts/draft14/session-fsm.ts +182 -1
- package/src/drafts/draft15/codec.ts +1661 -0
- package/src/drafts/draft15/index.ts +121 -0
- package/src/drafts/draft15/messages.ts +64 -0
- package/src/drafts/draft15/rules.ts +95 -0
- package/src/drafts/draft15/session-fsm.ts +687 -0
- package/src/drafts/draft15/session.ts +26 -0
- package/src/drafts/draft15/types.ts +336 -0
- package/src/drafts/draft16/codec.ts +1623 -0
- package/src/drafts/draft16/index.ts +123 -0
- package/src/drafts/draft16/messages.ts +67 -0
- package/src/drafts/draft16/rules.ts +96 -0
- package/src/drafts/draft16/session-fsm.ts +682 -0
- package/src/drafts/draft16/session.ts +26 -0
- package/src/drafts/draft16/types.ts +354 -0
- package/src/drafts/draft17/codec.ts +1621 -0
- package/src/drafts/draft17/index.ts +105 -0
- package/src/drafts/draft17/messages.ts +53 -0
- package/src/drafts/draft17/rules.ts +85 -0
- package/src/drafts/draft17/session-fsm.ts +437 -0
- package/src/drafts/draft17/session.ts +15 -0
- package/src/drafts/draft17/types.ts +310 -0
- package/src/index.ts +217 -2
|
@@ -0,0 +1,1623 @@
|
|
|
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";
|
|
5
|
+
import {
|
|
6
|
+
MESSAGE_ID_MAP,
|
|
7
|
+
MSG_CLIENT_SETUP,
|
|
8
|
+
MSG_FETCH,
|
|
9
|
+
MSG_FETCH_CANCEL,
|
|
10
|
+
MSG_FETCH_OK,
|
|
11
|
+
MSG_GOAWAY,
|
|
12
|
+
MSG_MAX_REQUEST_ID,
|
|
13
|
+
MSG_NAMESPACE,
|
|
14
|
+
MSG_NAMESPACE_DONE,
|
|
15
|
+
MSG_PUBLISH,
|
|
16
|
+
MSG_PUBLISH_DONE,
|
|
17
|
+
MSG_PUBLISH_NAMESPACE,
|
|
18
|
+
MSG_PUBLISH_NAMESPACE_CANCEL,
|
|
19
|
+
MSG_PUBLISH_NAMESPACE_DONE,
|
|
20
|
+
MSG_PUBLISH_OK,
|
|
21
|
+
MSG_REQUEST_ERROR,
|
|
22
|
+
MSG_REQUEST_OK,
|
|
23
|
+
MSG_REQUEST_UPDATE,
|
|
24
|
+
MSG_REQUESTS_BLOCKED,
|
|
25
|
+
MSG_SERVER_SETUP,
|
|
26
|
+
MSG_SUBSCRIBE,
|
|
27
|
+
MSG_SUBSCRIBE_NAMESPACE,
|
|
28
|
+
MSG_SUBSCRIBE_OK,
|
|
29
|
+
MSG_TRACK_STATUS,
|
|
30
|
+
MSG_UNSUBSCRIBE,
|
|
31
|
+
SETUP_PARAM_AUTHORITY,
|
|
32
|
+
SETUP_PARAM_MAX_AUTH_TOKEN_CACHE_SIZE,
|
|
33
|
+
SETUP_PARAM_MAX_REQUEST_ID,
|
|
34
|
+
SETUP_PARAM_MOQT_IMPLEMENTATION,
|
|
35
|
+
SETUP_PARAM_PATH,
|
|
36
|
+
} from "./messages.js";
|
|
37
|
+
import type {
|
|
38
|
+
DatagramObject,
|
|
39
|
+
DataStreamEvent,
|
|
40
|
+
Draft16DataStream,
|
|
41
|
+
Draft16Fetch,
|
|
42
|
+
Draft16Message,
|
|
43
|
+
Draft16Params,
|
|
44
|
+
Draft16SetupParams,
|
|
45
|
+
FetchObjectPayload,
|
|
46
|
+
FetchStream,
|
|
47
|
+
FetchStreamHeader,
|
|
48
|
+
JoiningFetch,
|
|
49
|
+
ObjectPayload,
|
|
50
|
+
StandaloneFetch,
|
|
51
|
+
SubgroupStream,
|
|
52
|
+
SubgroupStreamHeader,
|
|
53
|
+
UnknownParam,
|
|
54
|
+
} from "./types.js";
|
|
55
|
+
|
|
56
|
+
// ─── Helpers ───────────────────────────────────────────────────────────────────
|
|
57
|
+
|
|
58
|
+
function bytesToHex(bytes: Uint8Array): string {
|
|
59
|
+
let hex = "";
|
|
60
|
+
for (let i = 0; i < bytes.byteLength; i++) {
|
|
61
|
+
hex += (bytes[i] as number).toString(16).padStart(2, "0");
|
|
62
|
+
}
|
|
63
|
+
return hex;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function hexToBytes(hex: string): Uint8Array {
|
|
67
|
+
const bytes = new Uint8Array(hex.length / 2);
|
|
68
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
69
|
+
bytes[i / 2] = parseInt(hex.substring(i, i + 2), 16);
|
|
70
|
+
}
|
|
71
|
+
return bytes;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// ─── Setup Parameter Encoding/Decoding ──────────────────────────────────────────
|
|
75
|
+
|
|
76
|
+
function encodeSetupParams(params: Draft16SetupParams, writer: BufferWriter): void {
|
|
77
|
+
let count = 0;
|
|
78
|
+
if (params.path !== undefined) count++;
|
|
79
|
+
if (params.max_request_id !== undefined) count++;
|
|
80
|
+
if (params.max_auth_token_cache_size !== undefined) count++;
|
|
81
|
+
if (params.authority !== undefined) count++;
|
|
82
|
+
if (params.moqt_implementation !== undefined) count++;
|
|
83
|
+
if (params.unknown) count += params.unknown.length;
|
|
84
|
+
|
|
85
|
+
writer.writeVarInt(count);
|
|
86
|
+
|
|
87
|
+
// PATH (0x01) - odd, length-prefixed bytes
|
|
88
|
+
if (params.path !== undefined) {
|
|
89
|
+
writer.writeVarInt(SETUP_PARAM_PATH);
|
|
90
|
+
const encoded = new TextEncoder().encode(params.path);
|
|
91
|
+
writer.writeVarInt(encoded.byteLength);
|
|
92
|
+
writer.writeBytes(encoded);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// MAX_REQUEST_ID (0x02) - even, varint value
|
|
96
|
+
if (params.max_request_id !== undefined) {
|
|
97
|
+
writer.writeVarInt(SETUP_PARAM_MAX_REQUEST_ID);
|
|
98
|
+
writer.writeVarInt(params.max_request_id);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// MAX_AUTH_TOKEN_CACHE_SIZE (0x04) - even, varint value
|
|
102
|
+
if (params.max_auth_token_cache_size !== undefined) {
|
|
103
|
+
writer.writeVarInt(SETUP_PARAM_MAX_AUTH_TOKEN_CACHE_SIZE);
|
|
104
|
+
writer.writeVarInt(params.max_auth_token_cache_size);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// AUTHORITY (0x05) - odd, length-prefixed bytes
|
|
108
|
+
if (params.authority !== undefined) {
|
|
109
|
+
writer.writeVarInt(SETUP_PARAM_AUTHORITY);
|
|
110
|
+
const encoded = new TextEncoder().encode(params.authority);
|
|
111
|
+
writer.writeVarInt(encoded.byteLength);
|
|
112
|
+
writer.writeBytes(encoded);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// MOQT_IMPLEMENTATION (0x07) - odd, length-prefixed bytes
|
|
116
|
+
if (params.moqt_implementation !== undefined) {
|
|
117
|
+
writer.writeVarInt(SETUP_PARAM_MOQT_IMPLEMENTATION);
|
|
118
|
+
const encoded = new TextEncoder().encode(params.moqt_implementation);
|
|
119
|
+
writer.writeVarInt(encoded.byteLength);
|
|
120
|
+
writer.writeBytes(encoded);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Unknown params
|
|
124
|
+
if (params.unknown) {
|
|
125
|
+
for (const u of params.unknown) {
|
|
126
|
+
const id = BigInt(u.id);
|
|
127
|
+
writer.writeVarInt(id);
|
|
128
|
+
if (id % 2n === 0n) {
|
|
129
|
+
const raw = hexToBytes(u.raw_hex);
|
|
130
|
+
const tmpReader = new BufferReader(raw);
|
|
131
|
+
const value = tmpReader.readVarInt();
|
|
132
|
+
writer.writeVarInt(value);
|
|
133
|
+
} else {
|
|
134
|
+
const raw = hexToBytes(u.raw_hex);
|
|
135
|
+
writer.writeVarInt(raw.byteLength);
|
|
136
|
+
writer.writeBytes(raw);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function decodeSetupParams(reader: BufferReader): Draft16SetupParams {
|
|
143
|
+
const count = Number(reader.readVarInt());
|
|
144
|
+
const result: Draft16SetupParams = {};
|
|
145
|
+
const unknown: UnknownParam[] = [];
|
|
146
|
+
|
|
147
|
+
for (let i = 0; i < count; i++) {
|
|
148
|
+
const paramType = reader.readVarInt();
|
|
149
|
+
|
|
150
|
+
if (paramType % 2n === 0n) {
|
|
151
|
+
const value = reader.readVarInt();
|
|
152
|
+
if (paramType === SETUP_PARAM_MAX_REQUEST_ID) {
|
|
153
|
+
result.max_request_id = value;
|
|
154
|
+
} else if (paramType === SETUP_PARAM_MAX_AUTH_TOKEN_CACHE_SIZE) {
|
|
155
|
+
result.max_auth_token_cache_size = value;
|
|
156
|
+
} else {
|
|
157
|
+
const tmpWriter = new BufferWriter(16);
|
|
158
|
+
tmpWriter.writeVarInt(value);
|
|
159
|
+
const raw = tmpWriter.finish();
|
|
160
|
+
unknown.push({
|
|
161
|
+
id: `0x${paramType.toString(16)}`,
|
|
162
|
+
length: raw.byteLength,
|
|
163
|
+
raw_hex: bytesToHex(raw),
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
} else {
|
|
167
|
+
const length = Number(reader.readVarInt());
|
|
168
|
+
const bytes = reader.readBytes(length);
|
|
169
|
+
if (paramType === SETUP_PARAM_PATH) {
|
|
170
|
+
result.path = new TextDecoder().decode(bytes);
|
|
171
|
+
} else if (paramType === SETUP_PARAM_AUTHORITY) {
|
|
172
|
+
result.authority = new TextDecoder().decode(bytes);
|
|
173
|
+
} else if (paramType === SETUP_PARAM_MOQT_IMPLEMENTATION) {
|
|
174
|
+
result.moqt_implementation = new TextDecoder().decode(bytes);
|
|
175
|
+
} else {
|
|
176
|
+
unknown.push({
|
|
177
|
+
id: `0x${paramType.toString(16)}`,
|
|
178
|
+
length,
|
|
179
|
+
raw_hex: bytesToHex(bytes),
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (unknown.length > 0) {
|
|
186
|
+
result.unknown = unknown;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return result;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// ─── Version-Specific Parameter Encoding/Decoding ───────────────────────────────
|
|
193
|
+
|
|
194
|
+
const PARAM_EXPIRES = 0x08n;
|
|
195
|
+
const PARAM_LARGEST_OBJECT = 0x09n;
|
|
196
|
+
const PARAM_SUBSCRIBER_PRIORITY = 0x20n;
|
|
197
|
+
const PARAM_SUBSCRIPTION_FILTER = 0x21n;
|
|
198
|
+
const PARAM_GROUP_ORDER = 0x22n;
|
|
199
|
+
|
|
200
|
+
function encodeParams(params: Draft16Params, writer: BufferWriter): void {
|
|
201
|
+
let count = params.unknown ? params.unknown.length : 0;
|
|
202
|
+
if (params.expires !== undefined) count++;
|
|
203
|
+
if (params.largest_object !== undefined) count++;
|
|
204
|
+
if (params.subscriber_priority !== undefined) count++;
|
|
205
|
+
if (params.subscription_filter !== undefined) count++;
|
|
206
|
+
if (params.group_order !== undefined) count++;
|
|
207
|
+
writer.writeVarInt(count);
|
|
208
|
+
|
|
209
|
+
if (params.expires !== undefined) {
|
|
210
|
+
writer.writeVarInt(PARAM_EXPIRES);
|
|
211
|
+
writer.writeVarInt(params.expires);
|
|
212
|
+
}
|
|
213
|
+
if (params.largest_object !== undefined) {
|
|
214
|
+
writer.writeVarInt(PARAM_LARGEST_OBJECT);
|
|
215
|
+
const tmpW = new BufferWriter(16);
|
|
216
|
+
tmpW.writeVarInt(params.largest_object.group);
|
|
217
|
+
tmpW.writeVarInt(params.largest_object.object);
|
|
218
|
+
const raw = tmpW.finish();
|
|
219
|
+
writer.writeVarInt(raw.byteLength);
|
|
220
|
+
writer.writeBytes(raw);
|
|
221
|
+
}
|
|
222
|
+
if (params.subscriber_priority !== undefined) {
|
|
223
|
+
writer.writeVarInt(PARAM_SUBSCRIBER_PRIORITY);
|
|
224
|
+
writer.writeVarInt(params.subscriber_priority);
|
|
225
|
+
}
|
|
226
|
+
if (params.subscription_filter !== undefined) {
|
|
227
|
+
writer.writeVarInt(PARAM_SUBSCRIPTION_FILTER);
|
|
228
|
+
const tmpW = new BufferWriter(32);
|
|
229
|
+
const f = params.subscription_filter;
|
|
230
|
+
tmpW.writeVarInt(f.filter_type);
|
|
231
|
+
if (f.filter_type === 3n || f.filter_type === 4n) {
|
|
232
|
+
tmpW.writeVarInt(f.start_group!);
|
|
233
|
+
tmpW.writeVarInt(f.start_object!);
|
|
234
|
+
}
|
|
235
|
+
if (f.filter_type === 4n) {
|
|
236
|
+
tmpW.writeVarInt(f.end_group!);
|
|
237
|
+
}
|
|
238
|
+
const raw = tmpW.finish();
|
|
239
|
+
writer.writeVarInt(raw.byteLength);
|
|
240
|
+
writer.writeBytes(raw);
|
|
241
|
+
}
|
|
242
|
+
if (params.group_order !== undefined) {
|
|
243
|
+
writer.writeVarInt(PARAM_GROUP_ORDER);
|
|
244
|
+
writer.writeVarInt(params.group_order);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (params.unknown) {
|
|
248
|
+
for (const u of params.unknown) {
|
|
249
|
+
const id = BigInt(u.id);
|
|
250
|
+
writer.writeVarInt(id);
|
|
251
|
+
if (id % 2n === 0n) {
|
|
252
|
+
const raw = hexToBytes(u.raw_hex);
|
|
253
|
+
const tmpReader = new BufferReader(raw);
|
|
254
|
+
const value = tmpReader.readVarInt();
|
|
255
|
+
writer.writeVarInt(value);
|
|
256
|
+
} else {
|
|
257
|
+
const raw = hexToBytes(u.raw_hex);
|
|
258
|
+
writer.writeVarInt(raw.byteLength);
|
|
259
|
+
writer.writeBytes(raw);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function decodeParams(reader: BufferReader): Draft16Params {
|
|
266
|
+
const count = Number(reader.readVarInt());
|
|
267
|
+
const result: Draft16Params = {};
|
|
268
|
+
const unknown: UnknownParam[] = [];
|
|
269
|
+
|
|
270
|
+
for (let i = 0; i < count; i++) {
|
|
271
|
+
const paramType = reader.readVarInt();
|
|
272
|
+
|
|
273
|
+
if (paramType === PARAM_EXPIRES) {
|
|
274
|
+
result.expires = reader.readVarInt();
|
|
275
|
+
} else if (paramType === PARAM_SUBSCRIBER_PRIORITY) {
|
|
276
|
+
result.subscriber_priority = reader.readVarInt();
|
|
277
|
+
} else if (paramType === PARAM_GROUP_ORDER) {
|
|
278
|
+
result.group_order = reader.readVarInt();
|
|
279
|
+
} else if (paramType === PARAM_LARGEST_OBJECT) {
|
|
280
|
+
const length = Number(reader.readVarInt());
|
|
281
|
+
const startOff = reader.offset;
|
|
282
|
+
const group = reader.readVarInt();
|
|
283
|
+
const object = reader.readVarInt();
|
|
284
|
+
const consumed = reader.offset - startOff;
|
|
285
|
+
if (consumed < length) reader.readBytes(length - consumed);
|
|
286
|
+
result.largest_object = { group, object };
|
|
287
|
+
} else if (paramType === PARAM_SUBSCRIPTION_FILTER) {
|
|
288
|
+
const length = Number(reader.readVarInt());
|
|
289
|
+
const startOff = reader.offset;
|
|
290
|
+
const filter_type = reader.readVarInt();
|
|
291
|
+
const filter: {
|
|
292
|
+
filter_type: bigint;
|
|
293
|
+
start_group?: bigint;
|
|
294
|
+
start_object?: bigint;
|
|
295
|
+
end_group?: bigint;
|
|
296
|
+
} = { filter_type };
|
|
297
|
+
if (filter_type === 3n || filter_type === 4n) {
|
|
298
|
+
filter.start_group = reader.readVarInt();
|
|
299
|
+
filter.start_object = reader.readVarInt();
|
|
300
|
+
}
|
|
301
|
+
if (filter_type === 4n) {
|
|
302
|
+
filter.end_group = reader.readVarInt();
|
|
303
|
+
}
|
|
304
|
+
const consumed = reader.offset - startOff;
|
|
305
|
+
if (consumed < length) reader.readBytes(length - consumed);
|
|
306
|
+
result.subscription_filter = filter;
|
|
307
|
+
} else if (paramType % 2n === 0n) {
|
|
308
|
+
const value = reader.readVarInt();
|
|
309
|
+
const tmpWriter = new BufferWriter(16);
|
|
310
|
+
tmpWriter.writeVarInt(value);
|
|
311
|
+
const raw = tmpWriter.finish();
|
|
312
|
+
unknown.push({
|
|
313
|
+
id: `0x${paramType.toString(16)}`,
|
|
314
|
+
length: raw.byteLength,
|
|
315
|
+
raw_hex: bytesToHex(raw),
|
|
316
|
+
});
|
|
317
|
+
} else {
|
|
318
|
+
const length = Number(reader.readVarInt());
|
|
319
|
+
const bytes = reader.readBytes(length);
|
|
320
|
+
unknown.push({
|
|
321
|
+
id: `0x${paramType.toString(16)}`,
|
|
322
|
+
length,
|
|
323
|
+
raw_hex: bytesToHex(bytes),
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
if (unknown.length > 0) {
|
|
329
|
+
result.unknown = unknown;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
return result;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// ─── Payload Encoders ──────────────────────────────────────────────────────────
|
|
336
|
+
|
|
337
|
+
function encodeClientSetupPayload(
|
|
338
|
+
msg: Draft16Message & { type: "client_setup" },
|
|
339
|
+
w: BufferWriter,
|
|
340
|
+
): void {
|
|
341
|
+
encodeSetupParams(msg.parameters, w);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
function encodeServerSetupPayload(
|
|
345
|
+
msg: Draft16Message & { type: "server_setup" },
|
|
346
|
+
w: BufferWriter,
|
|
347
|
+
): void {
|
|
348
|
+
encodeSetupParams(msg.parameters, w);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function encodeSubscribePayload(
|
|
352
|
+
msg: Draft16Message & { type: "subscribe" },
|
|
353
|
+
w: BufferWriter,
|
|
354
|
+
): void {
|
|
355
|
+
w.writeVarInt(msg.request_id);
|
|
356
|
+
w.writeTuple(msg.track_namespace);
|
|
357
|
+
w.writeString(msg.track_name);
|
|
358
|
+
encodeParams(msg.parameters, w);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
function encodeSubscribeOkPayload(
|
|
362
|
+
msg: Draft16Message & { type: "subscribe_ok" },
|
|
363
|
+
w: BufferWriter,
|
|
364
|
+
): void {
|
|
365
|
+
w.writeVarInt(msg.request_id);
|
|
366
|
+
w.writeVarInt(msg.track_alias);
|
|
367
|
+
encodeParams(msg.parameters, w);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
function encodeRequestUpdatePayload(
|
|
371
|
+
msg: Draft16Message & { type: "request_update" },
|
|
372
|
+
w: BufferWriter,
|
|
373
|
+
): void {
|
|
374
|
+
w.writeVarInt(msg.request_id);
|
|
375
|
+
w.writeVarInt(msg.existing_request_id);
|
|
376
|
+
encodeParams(msg.parameters, w);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
function encodeUnsubscribePayload(
|
|
380
|
+
msg: Draft16Message & { type: "unsubscribe" },
|
|
381
|
+
w: BufferWriter,
|
|
382
|
+
): void {
|
|
383
|
+
w.writeVarInt(msg.request_id);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
function encodePublishPayload(msg: Draft16Message & { type: "publish" }, w: BufferWriter): void {
|
|
387
|
+
w.writeVarInt(msg.request_id);
|
|
388
|
+
w.writeTuple(msg.track_namespace);
|
|
389
|
+
w.writeString(msg.track_name);
|
|
390
|
+
w.writeVarInt(msg.track_alias);
|
|
391
|
+
encodeParams(msg.parameters, w);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
function encodePublishOkPayload(
|
|
395
|
+
msg: Draft16Message & { type: "publish_ok" },
|
|
396
|
+
w: BufferWriter,
|
|
397
|
+
): void {
|
|
398
|
+
w.writeVarInt(msg.request_id);
|
|
399
|
+
encodeParams(msg.parameters, w);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
function encodePublishDonePayload(
|
|
403
|
+
msg: Draft16Message & { type: "publish_done" },
|
|
404
|
+
w: BufferWriter,
|
|
405
|
+
): void {
|
|
406
|
+
w.writeVarInt(msg.request_id);
|
|
407
|
+
w.writeVarInt(msg.status_code);
|
|
408
|
+
w.writeVarInt(msg.stream_count);
|
|
409
|
+
w.writeString(msg.reason_phrase);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
function encodePublishNamespacePayload(
|
|
413
|
+
msg: Draft16Message & { type: "publish_namespace" },
|
|
414
|
+
w: BufferWriter,
|
|
415
|
+
): void {
|
|
416
|
+
w.writeVarInt(msg.request_id);
|
|
417
|
+
w.writeTuple(msg.track_namespace);
|
|
418
|
+
encodeParams(msg.parameters, w);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
// Draft-16: simplified to just request_id
|
|
422
|
+
function encodePublishNamespaceDonePayload(
|
|
423
|
+
msg: Draft16Message & { type: "publish_namespace_done" },
|
|
424
|
+
w: BufferWriter,
|
|
425
|
+
): void {
|
|
426
|
+
w.writeVarInt(msg.request_id);
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// Draft-16: changed to request_id + error_code + reason_phrase
|
|
430
|
+
function encodePublishNamespaceCancelPayload(
|
|
431
|
+
msg: Draft16Message & { type: "publish_namespace_cancel" },
|
|
432
|
+
w: BufferWriter,
|
|
433
|
+
): void {
|
|
434
|
+
w.writeVarInt(msg.request_id);
|
|
435
|
+
w.writeVarInt(msg.error_code);
|
|
436
|
+
w.writeString(msg.reason_phrase);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
// Draft-16: added subscribe_options
|
|
440
|
+
function encodeSubscribeNamespacePayload(
|
|
441
|
+
msg: Draft16Message & { type: "subscribe_namespace" },
|
|
442
|
+
w: BufferWriter,
|
|
443
|
+
): void {
|
|
444
|
+
w.writeVarInt(msg.request_id);
|
|
445
|
+
w.writeTuple(msg.namespace_prefix);
|
|
446
|
+
w.writeVarInt(msg.subscribe_options);
|
|
447
|
+
encodeParams(msg.parameters, w);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
// New in draft-16
|
|
451
|
+
function encodeNamespacePayload(
|
|
452
|
+
msg: Draft16Message & { type: "namespace" },
|
|
453
|
+
w: BufferWriter,
|
|
454
|
+
): void {
|
|
455
|
+
w.writeTuple(msg.namespace_suffix);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
// New in draft-16
|
|
459
|
+
function encodeNamespaceDonePayload(
|
|
460
|
+
msg: Draft16Message & { type: "namespace_done" },
|
|
461
|
+
w: BufferWriter,
|
|
462
|
+
): void {
|
|
463
|
+
w.writeTuple(msg.namespace_suffix);
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
function encodeFetchPayload(msg: Draft16Message & { type: "fetch" }, w: BufferWriter): void {
|
|
467
|
+
w.writeVarInt(msg.request_id);
|
|
468
|
+
w.writeVarInt(msg.fetch_type);
|
|
469
|
+
const ft = Number(msg.fetch_type);
|
|
470
|
+
if (ft === 1 && msg.standalone) {
|
|
471
|
+
w.writeTuple(msg.standalone.track_namespace);
|
|
472
|
+
w.writeString(msg.standalone.track_name);
|
|
473
|
+
w.writeVarInt(msg.standalone.start_group);
|
|
474
|
+
w.writeVarInt(msg.standalone.start_object);
|
|
475
|
+
w.writeVarInt(msg.standalone.end_group);
|
|
476
|
+
w.writeVarInt(msg.standalone.end_object);
|
|
477
|
+
} else if ((ft === 2 || ft === 3) && msg.joining) {
|
|
478
|
+
w.writeVarInt(msg.joining.joining_request_id);
|
|
479
|
+
w.writeVarInt(msg.joining.joining_start);
|
|
480
|
+
}
|
|
481
|
+
encodeParams(msg.parameters, w);
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
function encodeFetchOkPayload(msg: Draft16Message & { type: "fetch_ok" }, w: BufferWriter): void {
|
|
485
|
+
w.writeVarInt(msg.request_id);
|
|
486
|
+
w.writeUint8(msg.end_of_track);
|
|
487
|
+
w.writeVarInt(msg.end_group);
|
|
488
|
+
w.writeVarInt(msg.end_object);
|
|
489
|
+
encodeParams(msg.parameters, w);
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
function encodeFetchCancelPayload(
|
|
493
|
+
msg: Draft16Message & { type: "fetch_cancel" },
|
|
494
|
+
w: BufferWriter,
|
|
495
|
+
): void {
|
|
496
|
+
w.writeVarInt(msg.request_id);
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
function encodeTrackStatusPayload(
|
|
500
|
+
msg: Draft16Message & { type: "track_status" },
|
|
501
|
+
w: BufferWriter,
|
|
502
|
+
): void {
|
|
503
|
+
w.writeVarInt(msg.request_id);
|
|
504
|
+
w.writeTuple(msg.track_namespace);
|
|
505
|
+
w.writeString(msg.track_name);
|
|
506
|
+
encodeParams(msg.parameters, w);
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
function encodeRequestOkPayload(
|
|
510
|
+
msg: Draft16Message & { type: "request_ok" },
|
|
511
|
+
w: BufferWriter,
|
|
512
|
+
): void {
|
|
513
|
+
w.writeVarInt(msg.request_id);
|
|
514
|
+
encodeParams(msg.parameters, w);
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
// Draft-16: added retry_interval
|
|
518
|
+
function encodeRequestErrorPayload(
|
|
519
|
+
msg: Draft16Message & { type: "request_error" },
|
|
520
|
+
w: BufferWriter,
|
|
521
|
+
): void {
|
|
522
|
+
w.writeVarInt(msg.request_id);
|
|
523
|
+
w.writeVarInt(msg.error_code);
|
|
524
|
+
w.writeVarInt(msg.retry_interval);
|
|
525
|
+
w.writeString(msg.reason_phrase);
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
function encodeGoAwayPayload(msg: Draft16Message & { type: "goaway" }, w: BufferWriter): void {
|
|
529
|
+
w.writeString(msg.new_session_uri);
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
function encodeMaxRequestIdPayload(
|
|
533
|
+
msg: Draft16Message & { type: "max_request_id" },
|
|
534
|
+
w: BufferWriter,
|
|
535
|
+
): void {
|
|
536
|
+
w.writeVarInt(msg.max_request_id);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
function encodeRequestsBlockedPayload(
|
|
540
|
+
msg: Draft16Message & { type: "requests_blocked" },
|
|
541
|
+
w: BufferWriter,
|
|
542
|
+
): void {
|
|
543
|
+
w.writeVarInt(msg.maximum_request_id);
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
// ─── Payload Decoders ──────────────────────────────────────────────────────────
|
|
547
|
+
|
|
548
|
+
function decodeClientSetupPayload(r: BufferReader): Draft16Message {
|
|
549
|
+
const parameters = decodeSetupParams(r);
|
|
550
|
+
return { type: "client_setup", parameters };
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
function decodeServerSetupPayload(r: BufferReader): Draft16Message {
|
|
554
|
+
const parameters = decodeSetupParams(r);
|
|
555
|
+
return { type: "server_setup", parameters };
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
function decodeSubscribePayload(r: BufferReader): Draft16Message {
|
|
559
|
+
const request_id = r.readVarInt();
|
|
560
|
+
const track_namespace = r.readTuple();
|
|
561
|
+
const track_name = r.readString();
|
|
562
|
+
const parameters = decodeParams(r);
|
|
563
|
+
return { type: "subscribe", request_id, track_namespace, track_name, parameters };
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
function decodeSubscribeOkPayload(r: BufferReader): Draft16Message {
|
|
567
|
+
const request_id = r.readVarInt();
|
|
568
|
+
const track_alias = r.readVarInt();
|
|
569
|
+
const parameters = decodeParams(r);
|
|
570
|
+
return { type: "subscribe_ok", request_id, track_alias, parameters };
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
function decodeRequestUpdatePayload(r: BufferReader): Draft16Message {
|
|
574
|
+
const request_id = r.readVarInt();
|
|
575
|
+
const existing_request_id = r.readVarInt();
|
|
576
|
+
const parameters = decodeParams(r);
|
|
577
|
+
return { type: "request_update", request_id, existing_request_id, parameters };
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
function decodeUnsubscribePayload(r: BufferReader): Draft16Message {
|
|
581
|
+
const request_id = r.readVarInt();
|
|
582
|
+
return { type: "unsubscribe", request_id };
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
function decodePublishPayload(r: BufferReader): Draft16Message {
|
|
586
|
+
const request_id = r.readVarInt();
|
|
587
|
+
const track_namespace = r.readTuple();
|
|
588
|
+
const track_name = r.readString();
|
|
589
|
+
const track_alias = r.readVarInt();
|
|
590
|
+
const parameters = decodeParams(r);
|
|
591
|
+
return { type: "publish", request_id, track_namespace, track_name, track_alias, parameters };
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
function decodePublishOkPayload(r: BufferReader): Draft16Message {
|
|
595
|
+
const request_id = r.readVarInt();
|
|
596
|
+
const parameters = decodeParams(r);
|
|
597
|
+
return { type: "publish_ok", request_id, parameters };
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
function decodePublishDonePayload(r: BufferReader): Draft16Message {
|
|
601
|
+
const request_id = r.readVarInt();
|
|
602
|
+
const status_code = r.readVarInt();
|
|
603
|
+
const stream_count = r.readVarInt();
|
|
604
|
+
const reason_phrase = r.readString();
|
|
605
|
+
return { type: "publish_done", request_id, status_code, stream_count, reason_phrase };
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
function decodePublishNamespacePayload(r: BufferReader): Draft16Message {
|
|
609
|
+
const request_id = r.readVarInt();
|
|
610
|
+
const track_namespace = r.readTuple();
|
|
611
|
+
const parameters = decodeParams(r);
|
|
612
|
+
return { type: "publish_namespace", request_id, track_namespace, parameters };
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
// Draft-16: simplified to just request_id
|
|
616
|
+
function decodePublishNamespaceDonePayload(r: BufferReader): Draft16Message {
|
|
617
|
+
const request_id = r.readVarInt();
|
|
618
|
+
return { type: "publish_namespace_done", request_id };
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
// Draft-16: changed to request_id + error_code + reason_phrase
|
|
622
|
+
function decodePublishNamespaceCancelPayload(r: BufferReader): Draft16Message {
|
|
623
|
+
const request_id = r.readVarInt();
|
|
624
|
+
const error_code = r.readVarInt();
|
|
625
|
+
const reason_phrase = r.readString();
|
|
626
|
+
return { type: "publish_namespace_cancel", request_id, error_code, reason_phrase };
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
// Draft-16: added subscribe_options
|
|
630
|
+
function decodeSubscribeNamespacePayload(r: BufferReader): Draft16Message {
|
|
631
|
+
const request_id = r.readVarInt();
|
|
632
|
+
const namespace_prefix = r.readTuple();
|
|
633
|
+
const subscribe_options = r.readVarInt();
|
|
634
|
+
const parameters = decodeParams(r);
|
|
635
|
+
return {
|
|
636
|
+
type: "subscribe_namespace",
|
|
637
|
+
request_id,
|
|
638
|
+
namespace_prefix,
|
|
639
|
+
subscribe_options,
|
|
640
|
+
parameters,
|
|
641
|
+
};
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
// New in draft-16
|
|
645
|
+
function decodeNamespacePayload(r: BufferReader): Draft16Message {
|
|
646
|
+
const namespace_suffix = r.readTuple();
|
|
647
|
+
return { type: "namespace", namespace_suffix };
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
// New in draft-16
|
|
651
|
+
function decodeNamespaceDonePayload(r: BufferReader): Draft16Message {
|
|
652
|
+
const namespace_suffix = r.readTuple();
|
|
653
|
+
return { type: "namespace_done", namespace_suffix };
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
function decodeFetchPayload(r: BufferReader): Draft16Message {
|
|
657
|
+
const request_id = r.readVarInt();
|
|
658
|
+
const fetch_type = r.readVarInt();
|
|
659
|
+
const ft = Number(fetch_type);
|
|
660
|
+
|
|
661
|
+
if (ft < 1 || ft > 3) {
|
|
662
|
+
throw new DecodeError("CONSTRAINT_VIOLATION", `Invalid fetch_type: ${ft}`, r.offset);
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
let standalone: StandaloneFetch | undefined;
|
|
666
|
+
let joining: JoiningFetch | undefined;
|
|
667
|
+
|
|
668
|
+
if (ft === 1) {
|
|
669
|
+
const track_namespace = r.readTuple();
|
|
670
|
+
const track_name = r.readString();
|
|
671
|
+
const start_group = r.readVarInt();
|
|
672
|
+
const start_object = r.readVarInt();
|
|
673
|
+
const end_group = r.readVarInt();
|
|
674
|
+
const end_object = r.readVarInt();
|
|
675
|
+
standalone = { track_namespace, track_name, start_group, start_object, end_group, end_object };
|
|
676
|
+
} else {
|
|
677
|
+
const joining_request_id = r.readVarInt();
|
|
678
|
+
const joining_start = r.readVarInt();
|
|
679
|
+
joining = { joining_request_id, joining_start };
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
const parameters = decodeParams(r);
|
|
683
|
+
|
|
684
|
+
return {
|
|
685
|
+
type: "fetch",
|
|
686
|
+
request_id,
|
|
687
|
+
fetch_type,
|
|
688
|
+
standalone,
|
|
689
|
+
joining,
|
|
690
|
+
parameters,
|
|
691
|
+
} as Draft16Fetch;
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
function decodeFetchOkPayload(r: BufferReader): Draft16Message {
|
|
695
|
+
const request_id = r.readVarInt();
|
|
696
|
+
const end_of_track = r.readUint8();
|
|
697
|
+
const end_group = r.readVarInt();
|
|
698
|
+
const end_object = r.readVarInt();
|
|
699
|
+
const parameters = decodeParams(r);
|
|
700
|
+
return { type: "fetch_ok", request_id, end_of_track, end_group, end_object, parameters };
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
function decodeFetchCancelPayload(r: BufferReader): Draft16Message {
|
|
704
|
+
const request_id = r.readVarInt();
|
|
705
|
+
return { type: "fetch_cancel", request_id };
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
function decodeTrackStatusPayload(r: BufferReader): Draft16Message {
|
|
709
|
+
const request_id = r.readVarInt();
|
|
710
|
+
const track_namespace = r.readTuple();
|
|
711
|
+
const track_name = r.readString();
|
|
712
|
+
const parameters = decodeParams(r);
|
|
713
|
+
return { type: "track_status", request_id, track_namespace, track_name, parameters };
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
function decodeRequestOkPayload(r: BufferReader): Draft16Message {
|
|
717
|
+
const request_id = r.readVarInt();
|
|
718
|
+
const parameters = decodeParams(r);
|
|
719
|
+
return { type: "request_ok", request_id, parameters };
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
// Draft-16: added retry_interval
|
|
723
|
+
function decodeRequestErrorPayload(r: BufferReader): Draft16Message {
|
|
724
|
+
const request_id = r.readVarInt();
|
|
725
|
+
const error_code = r.readVarInt();
|
|
726
|
+
const retry_interval = r.readVarInt();
|
|
727
|
+
const reason_phrase = r.readString();
|
|
728
|
+
return { type: "request_error", request_id, error_code, retry_interval, reason_phrase };
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
function decodeGoAwayPayload(r: BufferReader): Draft16Message {
|
|
732
|
+
const new_session_uri = r.readString();
|
|
733
|
+
return { type: "goaway", new_session_uri };
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
function decodeMaxRequestIdPayload(r: BufferReader): Draft16Message {
|
|
737
|
+
const max_request_id = r.readVarInt();
|
|
738
|
+
return { type: "max_request_id", max_request_id };
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
function decodeRequestsBlockedPayload(r: BufferReader): Draft16Message {
|
|
742
|
+
const maximum_request_id = r.readVarInt();
|
|
743
|
+
return { type: "requests_blocked", maximum_request_id };
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
// ─── Payload dispatch tables ───────────────────────────────────────────────────
|
|
747
|
+
|
|
748
|
+
const payloadDecoders: ReadonlyMap<bigint, (r: BufferReader) => Draft16Message> = new Map([
|
|
749
|
+
[MSG_CLIENT_SETUP, decodeClientSetupPayload],
|
|
750
|
+
[MSG_SERVER_SETUP, decodeServerSetupPayload],
|
|
751
|
+
[MSG_SUBSCRIBE, decodeSubscribePayload],
|
|
752
|
+
[MSG_SUBSCRIBE_OK, decodeSubscribeOkPayload],
|
|
753
|
+
[MSG_REQUEST_UPDATE, decodeRequestUpdatePayload],
|
|
754
|
+
[MSG_UNSUBSCRIBE, decodeUnsubscribePayload],
|
|
755
|
+
[MSG_PUBLISH, decodePublishPayload],
|
|
756
|
+
[MSG_PUBLISH_OK, decodePublishOkPayload],
|
|
757
|
+
[MSG_PUBLISH_DONE, decodePublishDonePayload],
|
|
758
|
+
[MSG_PUBLISH_NAMESPACE, decodePublishNamespacePayload],
|
|
759
|
+
[MSG_PUBLISH_NAMESPACE_DONE, decodePublishNamespaceDonePayload],
|
|
760
|
+
[MSG_PUBLISH_NAMESPACE_CANCEL, decodePublishNamespaceCancelPayload],
|
|
761
|
+
[MSG_SUBSCRIBE_NAMESPACE, decodeSubscribeNamespacePayload],
|
|
762
|
+
[MSG_NAMESPACE, decodeNamespacePayload],
|
|
763
|
+
[MSG_NAMESPACE_DONE, decodeNamespaceDonePayload],
|
|
764
|
+
[MSG_FETCH, decodeFetchPayload],
|
|
765
|
+
[MSG_FETCH_OK, decodeFetchOkPayload],
|
|
766
|
+
[MSG_FETCH_CANCEL, decodeFetchCancelPayload],
|
|
767
|
+
[MSG_TRACK_STATUS, decodeTrackStatusPayload],
|
|
768
|
+
[MSG_REQUEST_OK, decodeRequestOkPayload],
|
|
769
|
+
[MSG_REQUEST_ERROR, decodeRequestErrorPayload],
|
|
770
|
+
[MSG_GOAWAY, decodeGoAwayPayload],
|
|
771
|
+
[MSG_MAX_REQUEST_ID, decodeMaxRequestIdPayload],
|
|
772
|
+
[MSG_REQUESTS_BLOCKED, decodeRequestsBlockedPayload],
|
|
773
|
+
]);
|
|
774
|
+
|
|
775
|
+
// ─── Public API ────────────────────────────────────────────────────────────────
|
|
776
|
+
|
|
777
|
+
/**
|
|
778
|
+
* Encode a draft-16 control message with type(varint) + length(uint16 BE) + payload.
|
|
779
|
+
*/
|
|
780
|
+
export function encodeMessage(message: Draft16Message): Uint8Array {
|
|
781
|
+
const typeId = MESSAGE_ID_MAP.get(message.type);
|
|
782
|
+
if (typeId === undefined) {
|
|
783
|
+
throw new Error(`Unknown message type: ${message.type}`);
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
const payloadWriter = new BufferWriter();
|
|
787
|
+
encodePayload(message, payloadWriter);
|
|
788
|
+
const payload = payloadWriter.finish();
|
|
789
|
+
|
|
790
|
+
if (payload.byteLength > 0xffff) {
|
|
791
|
+
throw new Error(`Payload too large for 16-bit length: ${payload.byteLength}`);
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
const writer = new BufferWriter();
|
|
795
|
+
writer.writeVarInt(typeId);
|
|
796
|
+
writer.writeUint8((payload.byteLength >> 8) & 0xff);
|
|
797
|
+
writer.writeUint8(payload.byteLength & 0xff);
|
|
798
|
+
writer.writeBytes(payload);
|
|
799
|
+
|
|
800
|
+
return writer.finish();
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
function encodePayload(msg: Draft16Message, w: BufferWriter): void {
|
|
804
|
+
switch (msg.type) {
|
|
805
|
+
case "client_setup":
|
|
806
|
+
return encodeClientSetupPayload(msg, w);
|
|
807
|
+
case "server_setup":
|
|
808
|
+
return encodeServerSetupPayload(msg, w);
|
|
809
|
+
case "subscribe":
|
|
810
|
+
return encodeSubscribePayload(msg, w);
|
|
811
|
+
case "subscribe_ok":
|
|
812
|
+
return encodeSubscribeOkPayload(msg, w);
|
|
813
|
+
case "request_update":
|
|
814
|
+
return encodeRequestUpdatePayload(msg, w);
|
|
815
|
+
case "unsubscribe":
|
|
816
|
+
return encodeUnsubscribePayload(msg, w);
|
|
817
|
+
case "publish":
|
|
818
|
+
return encodePublishPayload(msg, w);
|
|
819
|
+
case "publish_ok":
|
|
820
|
+
return encodePublishOkPayload(msg, w);
|
|
821
|
+
case "publish_done":
|
|
822
|
+
return encodePublishDonePayload(msg, w);
|
|
823
|
+
case "publish_namespace":
|
|
824
|
+
return encodePublishNamespacePayload(msg, w);
|
|
825
|
+
case "publish_namespace_done":
|
|
826
|
+
return encodePublishNamespaceDonePayload(msg, w);
|
|
827
|
+
case "publish_namespace_cancel":
|
|
828
|
+
return encodePublishNamespaceCancelPayload(msg, w);
|
|
829
|
+
case "subscribe_namespace":
|
|
830
|
+
return encodeSubscribeNamespacePayload(msg, w);
|
|
831
|
+
case "namespace":
|
|
832
|
+
return encodeNamespacePayload(msg, w);
|
|
833
|
+
case "namespace_done":
|
|
834
|
+
return encodeNamespaceDonePayload(msg, w);
|
|
835
|
+
case "fetch":
|
|
836
|
+
return encodeFetchPayload(msg, w);
|
|
837
|
+
case "fetch_ok":
|
|
838
|
+
return encodeFetchOkPayload(msg, w);
|
|
839
|
+
case "fetch_cancel":
|
|
840
|
+
return encodeFetchCancelPayload(msg, w);
|
|
841
|
+
case "track_status":
|
|
842
|
+
return encodeTrackStatusPayload(msg, w);
|
|
843
|
+
case "request_ok":
|
|
844
|
+
return encodeRequestOkPayload(msg, w);
|
|
845
|
+
case "request_error":
|
|
846
|
+
return encodeRequestErrorPayload(msg, w);
|
|
847
|
+
case "goaway":
|
|
848
|
+
return encodeGoAwayPayload(msg, w);
|
|
849
|
+
case "max_request_id":
|
|
850
|
+
return encodeMaxRequestIdPayload(msg, w);
|
|
851
|
+
case "requests_blocked":
|
|
852
|
+
return encodeRequestsBlockedPayload(msg, w);
|
|
853
|
+
default: {
|
|
854
|
+
const _exhaustive: never = msg;
|
|
855
|
+
throw new Error(`Unhandled message type: ${(_exhaustive as Draft16Message).type}`);
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
/**
|
|
861
|
+
* Decode a draft-16 control message from bytes (type + uint16 length + payload).
|
|
862
|
+
*/
|
|
863
|
+
export function decodeMessage(bytes: Uint8Array): DecodeResult<Draft16Message> {
|
|
864
|
+
try {
|
|
865
|
+
const reader = new BufferReader(bytes);
|
|
866
|
+
const typeId = reader.readVarInt();
|
|
867
|
+
|
|
868
|
+
const lenHi = reader.readUint8();
|
|
869
|
+
const lenLo = reader.readUint8();
|
|
870
|
+
const payloadLength = (lenHi << 8) | lenLo;
|
|
871
|
+
|
|
872
|
+
const payloadBytes = reader.readBytes(payloadLength);
|
|
873
|
+
const payloadReader = new BufferReader(payloadBytes);
|
|
874
|
+
|
|
875
|
+
const decoder = payloadDecoders.get(typeId);
|
|
876
|
+
if (!decoder) {
|
|
877
|
+
return {
|
|
878
|
+
ok: false,
|
|
879
|
+
error: new DecodeError(
|
|
880
|
+
"UNKNOWN_MESSAGE_TYPE",
|
|
881
|
+
`Unknown message type ID: 0x${typeId.toString(16)}`,
|
|
882
|
+
0,
|
|
883
|
+
),
|
|
884
|
+
};
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
const message = decoder(payloadReader);
|
|
888
|
+
return { ok: true, value: message, bytesRead: reader.offset };
|
|
889
|
+
} catch (e) {
|
|
890
|
+
if (e instanceof DecodeError) {
|
|
891
|
+
return { ok: false, error: e };
|
|
892
|
+
}
|
|
893
|
+
throw e;
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
// ─── Data Stream Encoding/Decoding ─────────────────────────────────────────────
|
|
898
|
+
|
|
899
|
+
const FETCH_STREAM_TYPE = 0x05n;
|
|
900
|
+
|
|
901
|
+
/**
|
|
902
|
+
* Encode a subgroup stream header + objects.
|
|
903
|
+
*/
|
|
904
|
+
export function encodeSubgroupStream(stream: SubgroupStream): Uint8Array {
|
|
905
|
+
const w = new BufferWriter();
|
|
906
|
+
const streamType = stream.headerType;
|
|
907
|
+
w.writeVarInt(BigInt(streamType));
|
|
908
|
+
|
|
909
|
+
const hasSubgroupField = (streamType & 0x04) !== 0;
|
|
910
|
+
const hasPriority = streamType < 0x30;
|
|
911
|
+
|
|
912
|
+
w.writeVarInt(stream.trackAlias);
|
|
913
|
+
w.writeVarInt(stream.groupId);
|
|
914
|
+
if (hasSubgroupField) {
|
|
915
|
+
w.writeVarInt(stream.subgroupId);
|
|
916
|
+
}
|
|
917
|
+
if (hasPriority) {
|
|
918
|
+
w.writeUint8(stream.publisherPriority);
|
|
919
|
+
}
|
|
920
|
+
let prevObjectId = -1n;
|
|
921
|
+
for (const obj of stream.objects) {
|
|
922
|
+
const delta = prevObjectId < 0n ? obj.objectId : obj.objectId - prevObjectId - 1n;
|
|
923
|
+
w.writeVarInt(delta);
|
|
924
|
+
w.writeVarInt(obj.payloadLength);
|
|
925
|
+
if (obj.payloadLength === 0) {
|
|
926
|
+
w.writeVarInt(obj.status ?? 0n);
|
|
927
|
+
} else {
|
|
928
|
+
w.writeBytes(obj.payload);
|
|
929
|
+
}
|
|
930
|
+
prevObjectId = obj.objectId;
|
|
931
|
+
}
|
|
932
|
+
return w.finish();
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
/**
|
|
936
|
+
* Encode a datagram object.
|
|
937
|
+
*/
|
|
938
|
+
export function encodeDatagram(dg: DatagramObject): Uint8Array {
|
|
939
|
+
const w = new BufferWriter();
|
|
940
|
+
const dgType = dg.datagramType;
|
|
941
|
+
w.writeVarInt(BigInt(dgType));
|
|
942
|
+
w.writeVarInt(dg.trackAlias);
|
|
943
|
+
w.writeVarInt(dg.groupId);
|
|
944
|
+
|
|
945
|
+
const objectIdAbsent = (dgType & 0x04) !== 0;
|
|
946
|
+
const isStatus = (dgType & 0x20) !== 0;
|
|
947
|
+
|
|
948
|
+
if (!objectIdAbsent) {
|
|
949
|
+
w.writeVarInt(dg.objectId);
|
|
950
|
+
}
|
|
951
|
+
w.writeUint8(dg.publisherPriority);
|
|
952
|
+
|
|
953
|
+
if (isStatus) {
|
|
954
|
+
w.writeVarInt(dg.objectStatus ?? 0n);
|
|
955
|
+
} else {
|
|
956
|
+
w.writeBytes(dg.payload);
|
|
957
|
+
}
|
|
958
|
+
return w.finish();
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
/**
|
|
962
|
+
* Encode a fetch stream header + objects.
|
|
963
|
+
*/
|
|
964
|
+
export function encodeFetchStream(stream: FetchStream): Uint8Array {
|
|
965
|
+
const w = new BufferWriter();
|
|
966
|
+
w.writeVarInt(FETCH_STREAM_TYPE);
|
|
967
|
+
w.writeVarInt(stream.requestId);
|
|
968
|
+
for (const obj of stream.objects) {
|
|
969
|
+
w.writeUint8(obj.serializationFlags);
|
|
970
|
+
const flags = obj.serializationFlags;
|
|
971
|
+
if (flags & 0x08) w.writeVarInt(obj.groupId);
|
|
972
|
+
const subgroupEncoding = flags & 0x03;
|
|
973
|
+
if (subgroupEncoding === 0x03) w.writeVarInt(obj.subgroupId);
|
|
974
|
+
if (flags & 0x04) w.writeVarInt(obj.objectId);
|
|
975
|
+
if (flags & 0x10) w.writeUint8(obj.publisherPriority);
|
|
976
|
+
w.writeVarInt(obj.payloadLength);
|
|
977
|
+
if (obj.payloadLength === 0) {
|
|
978
|
+
w.writeVarInt(obj.status ?? 0n);
|
|
979
|
+
} else {
|
|
980
|
+
w.writeBytes(obj.payload);
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
return w.finish();
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
/**
|
|
987
|
+
* Decode a subgroup data stream from raw bytes.
|
|
988
|
+
*/
|
|
989
|
+
export function decodeSubgroupStream(bytes: Uint8Array): DecodeResult<SubgroupStream> {
|
|
990
|
+
try {
|
|
991
|
+
const r = new BufferReader(bytes);
|
|
992
|
+
const streamType = Number(r.readVarInt());
|
|
993
|
+
|
|
994
|
+
if (
|
|
995
|
+
!((streamType >= 0x10 && streamType <= 0x1d) || (streamType >= 0x30 && streamType <= 0x3d))
|
|
996
|
+
) {
|
|
997
|
+
return {
|
|
998
|
+
ok: false,
|
|
999
|
+
error: new DecodeError(
|
|
1000
|
+
"CONSTRAINT_VIOLATION",
|
|
1001
|
+
`Expected subgroup stream type 0x10-0x1D or 0x30-0x3D, got 0x${streamType.toString(16)}`,
|
|
1002
|
+
0,
|
|
1003
|
+
),
|
|
1004
|
+
};
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
const hasSubgroupField = (streamType & 0x04) !== 0;
|
|
1008
|
+
const subgroupIsFirstObjId = (streamType & 0x02) !== 0 && !hasSubgroupField;
|
|
1009
|
+
const hasPriority = streamType < 0x30;
|
|
1010
|
+
|
|
1011
|
+
const trackAlias = r.readVarInt();
|
|
1012
|
+
const groupId = r.readVarInt();
|
|
1013
|
+
|
|
1014
|
+
let subgroupId = 0n;
|
|
1015
|
+
if (hasSubgroupField) {
|
|
1016
|
+
subgroupId = r.readVarInt();
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
let publisherPriority = 128;
|
|
1020
|
+
if (hasPriority) {
|
|
1021
|
+
publisherPriority = r.readUint8();
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
const objects: ObjectPayload[] = [];
|
|
1025
|
+
let prevObjectId = -1n;
|
|
1026
|
+
let firstObject = true;
|
|
1027
|
+
|
|
1028
|
+
while (r.remaining > 0) {
|
|
1029
|
+
const delta = r.readVarInt();
|
|
1030
|
+
let objectId: bigint;
|
|
1031
|
+
if (firstObject) {
|
|
1032
|
+
objectId = delta;
|
|
1033
|
+
if (subgroupIsFirstObjId && firstObject) {
|
|
1034
|
+
subgroupId = objectId;
|
|
1035
|
+
}
|
|
1036
|
+
firstObject = false;
|
|
1037
|
+
} else {
|
|
1038
|
+
objectId = prevObjectId + 1n + delta;
|
|
1039
|
+
}
|
|
1040
|
+
const payloadLength = Number(r.readVarInt());
|
|
1041
|
+
let payload: Uint8Array;
|
|
1042
|
+
let status: bigint | undefined;
|
|
1043
|
+
if (payloadLength === 0) {
|
|
1044
|
+
status = r.readVarInt();
|
|
1045
|
+
payload = new Uint8Array(0);
|
|
1046
|
+
} else {
|
|
1047
|
+
payload = r.readBytes(payloadLength);
|
|
1048
|
+
}
|
|
1049
|
+
const obj: ObjectPayload = { type: "object", objectId, payloadLength, payload };
|
|
1050
|
+
if (status !== undefined) (obj as unknown as Record<string, unknown>).status = status;
|
|
1051
|
+
objects.push(obj);
|
|
1052
|
+
prevObjectId = objectId;
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
return {
|
|
1056
|
+
ok: true,
|
|
1057
|
+
value: {
|
|
1058
|
+
type: "subgroup",
|
|
1059
|
+
headerType: streamType,
|
|
1060
|
+
trackAlias,
|
|
1061
|
+
groupId,
|
|
1062
|
+
subgroupId,
|
|
1063
|
+
publisherPriority,
|
|
1064
|
+
objects,
|
|
1065
|
+
},
|
|
1066
|
+
bytesRead: r.offset,
|
|
1067
|
+
};
|
|
1068
|
+
} catch (e) {
|
|
1069
|
+
if (e instanceof DecodeError) return { ok: false, error: e };
|
|
1070
|
+
throw e;
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
/**
|
|
1075
|
+
* Decode a datagram object from raw bytes.
|
|
1076
|
+
*/
|
|
1077
|
+
export function decodeDatagram(bytes: Uint8Array): DecodeResult<DatagramObject> {
|
|
1078
|
+
try {
|
|
1079
|
+
const r = new BufferReader(bytes);
|
|
1080
|
+
const dgType = Number(r.readVarInt());
|
|
1081
|
+
|
|
1082
|
+
const objectIdAbsent = (dgType & 0x04) !== 0;
|
|
1083
|
+
const endOfGroup = (dgType & 0x02) !== 0;
|
|
1084
|
+
const isStatus = (dgType & 0x20) !== 0;
|
|
1085
|
+
|
|
1086
|
+
const trackAlias = r.readVarInt();
|
|
1087
|
+
const groupId = r.readVarInt();
|
|
1088
|
+
let objectId = 0n;
|
|
1089
|
+
if (!objectIdAbsent) {
|
|
1090
|
+
objectId = r.readVarInt();
|
|
1091
|
+
}
|
|
1092
|
+
const publisherPriority = r.readUint8();
|
|
1093
|
+
|
|
1094
|
+
let objectStatus: bigint | undefined;
|
|
1095
|
+
let payload: Uint8Array;
|
|
1096
|
+
if (isStatus) {
|
|
1097
|
+
objectStatus = r.readVarInt();
|
|
1098
|
+
payload = new Uint8Array(0);
|
|
1099
|
+
} else {
|
|
1100
|
+
payload = r.readBytes(r.remaining);
|
|
1101
|
+
}
|
|
1102
|
+
const payloadLength = payload.byteLength;
|
|
1103
|
+
|
|
1104
|
+
const result: DatagramObject = {
|
|
1105
|
+
type: "datagram",
|
|
1106
|
+
datagramType: dgType,
|
|
1107
|
+
trackAlias,
|
|
1108
|
+
groupId,
|
|
1109
|
+
objectId,
|
|
1110
|
+
publisherPriority,
|
|
1111
|
+
payloadLength,
|
|
1112
|
+
payload,
|
|
1113
|
+
};
|
|
1114
|
+
|
|
1115
|
+
if (endOfGroup) (result as unknown as Record<string, unknown>).endOfGroup = true;
|
|
1116
|
+
if (objectStatus !== undefined)
|
|
1117
|
+
(result as unknown as Record<string, unknown>).objectStatus = objectStatus;
|
|
1118
|
+
|
|
1119
|
+
return { ok: true, value: result, bytesRead: r.offset };
|
|
1120
|
+
} catch (e) {
|
|
1121
|
+
if (e instanceof DecodeError) return { ok: false, error: e };
|
|
1122
|
+
throw e;
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
|
|
1126
|
+
/**
|
|
1127
|
+
* Decode a fetch data stream from raw bytes.
|
|
1128
|
+
*/
|
|
1129
|
+
export function decodeFetchStream(bytes: Uint8Array): DecodeResult<FetchStream> {
|
|
1130
|
+
try {
|
|
1131
|
+
const r = new BufferReader(bytes);
|
|
1132
|
+
const streamType = r.readVarInt();
|
|
1133
|
+
if (streamType !== FETCH_STREAM_TYPE) {
|
|
1134
|
+
return {
|
|
1135
|
+
ok: false,
|
|
1136
|
+
error: new DecodeError(
|
|
1137
|
+
"CONSTRAINT_VIOLATION",
|
|
1138
|
+
`Expected fetch stream type 0x05, got 0x${streamType.toString(16)}`,
|
|
1139
|
+
0,
|
|
1140
|
+
),
|
|
1141
|
+
};
|
|
1142
|
+
}
|
|
1143
|
+
const requestId = r.readVarInt();
|
|
1144
|
+
const objects: FetchObjectPayload[] = [];
|
|
1145
|
+
|
|
1146
|
+
let prevGroupId = 0n;
|
|
1147
|
+
let prevSubgroupId = 0n;
|
|
1148
|
+
let prevObjectId = 0n;
|
|
1149
|
+
let prevPriority = 128;
|
|
1150
|
+
let first = true;
|
|
1151
|
+
|
|
1152
|
+
while (r.remaining > 0) {
|
|
1153
|
+
const flags = r.readUint8();
|
|
1154
|
+
const subgroupEncoding = flags & 0x03;
|
|
1155
|
+
const objectIdPresent = (flags & 0x04) !== 0;
|
|
1156
|
+
const groupIdPresent = (flags & 0x08) !== 0;
|
|
1157
|
+
const priorityPresent = (flags & 0x10) !== 0;
|
|
1158
|
+
const extensionsPresent = (flags & 0x20) !== 0;
|
|
1159
|
+
|
|
1160
|
+
if (flags & 0xc0) {
|
|
1161
|
+
return {
|
|
1162
|
+
ok: false,
|
|
1163
|
+
error: new DecodeError(
|
|
1164
|
+
"CONSTRAINT_VIOLATION",
|
|
1165
|
+
"Reserved bits set in fetch object flags",
|
|
1166
|
+
r.offset,
|
|
1167
|
+
),
|
|
1168
|
+
};
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
let groupId = prevGroupId;
|
|
1172
|
+
if (groupIdPresent) {
|
|
1173
|
+
groupId = r.readVarInt();
|
|
1174
|
+
} else if (first) {
|
|
1175
|
+
return {
|
|
1176
|
+
ok: false,
|
|
1177
|
+
error: new DecodeError(
|
|
1178
|
+
"CONSTRAINT_VIOLATION",
|
|
1179
|
+
"First fetch object must include groupId",
|
|
1180
|
+
r.offset,
|
|
1181
|
+
),
|
|
1182
|
+
};
|
|
1183
|
+
}
|
|
1184
|
+
|
|
1185
|
+
let subgroupId = prevSubgroupId;
|
|
1186
|
+
if (subgroupEncoding === 0x03) {
|
|
1187
|
+
subgroupId = r.readVarInt();
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
let objectId = prevObjectId + 1n;
|
|
1191
|
+
if (objectIdPresent) {
|
|
1192
|
+
objectId = r.readVarInt();
|
|
1193
|
+
} else if (first) {
|
|
1194
|
+
return {
|
|
1195
|
+
ok: false,
|
|
1196
|
+
error: new DecodeError(
|
|
1197
|
+
"CONSTRAINT_VIOLATION",
|
|
1198
|
+
"First fetch object must include objectId",
|
|
1199
|
+
r.offset,
|
|
1200
|
+
),
|
|
1201
|
+
};
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
if (priorityPresent) {
|
|
1205
|
+
prevPriority = r.readUint8();
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
if (extensionsPresent) {
|
|
1209
|
+
const extLen = Number(r.readVarInt());
|
|
1210
|
+
if (extLen > 0) {
|
|
1211
|
+
r.readBytes(extLen);
|
|
1212
|
+
}
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
const payloadLength = Number(r.readVarInt());
|
|
1216
|
+
let payload: Uint8Array;
|
|
1217
|
+
let status: bigint | undefined;
|
|
1218
|
+
if (payloadLength > 0) {
|
|
1219
|
+
payload = r.readBytes(payloadLength);
|
|
1220
|
+
} else {
|
|
1221
|
+
status = r.readVarInt();
|
|
1222
|
+
payload = new Uint8Array(0);
|
|
1223
|
+
}
|
|
1224
|
+
|
|
1225
|
+
const obj: FetchObjectPayload = {
|
|
1226
|
+
type: "object",
|
|
1227
|
+
serializationFlags: flags,
|
|
1228
|
+
groupId,
|
|
1229
|
+
subgroupId,
|
|
1230
|
+
objectId,
|
|
1231
|
+
publisherPriority: prevPriority,
|
|
1232
|
+
payloadLength,
|
|
1233
|
+
payload,
|
|
1234
|
+
};
|
|
1235
|
+
if (status !== undefined) (obj as unknown as Record<string, unknown>).status = status;
|
|
1236
|
+
objects.push(obj);
|
|
1237
|
+
|
|
1238
|
+
prevGroupId = groupId;
|
|
1239
|
+
prevSubgroupId = subgroupId;
|
|
1240
|
+
prevObjectId = objectId;
|
|
1241
|
+
first = false;
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1244
|
+
return {
|
|
1245
|
+
ok: true,
|
|
1246
|
+
value: { type: "fetch", requestId, objects },
|
|
1247
|
+
bytesRead: r.offset,
|
|
1248
|
+
};
|
|
1249
|
+
} catch (e) {
|
|
1250
|
+
if (e instanceof DecodeError) return { ok: false, error: e };
|
|
1251
|
+
throw e;
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
|
|
1255
|
+
/**
|
|
1256
|
+
* Decode a data stream, dispatching by stream type.
|
|
1257
|
+
*/
|
|
1258
|
+
export function decodeDataStream(
|
|
1259
|
+
streamType: "subgroup" | "datagram" | "fetch",
|
|
1260
|
+
bytes: Uint8Array,
|
|
1261
|
+
): DecodeResult<Draft16DataStream> {
|
|
1262
|
+
switch (streamType) {
|
|
1263
|
+
case "subgroup":
|
|
1264
|
+
return decodeSubgroupStream(bytes);
|
|
1265
|
+
case "datagram":
|
|
1266
|
+
return decodeDatagram(bytes);
|
|
1267
|
+
case "fetch":
|
|
1268
|
+
return decodeFetchStream(bytes);
|
|
1269
|
+
default: {
|
|
1270
|
+
const _exhaustive: never = streamType;
|
|
1271
|
+
throw new Error(`Unknown stream type: ${_exhaustive}`);
|
|
1272
|
+
}
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
|
|
1276
|
+
// ─── Stream Decoders ───────────────────────────────────────────────────────────
|
|
1277
|
+
|
|
1278
|
+
export function createStreamDecoder(): TransformStream<Uint8Array, Draft16Message> {
|
|
1279
|
+
let buffer = new Uint8Array(0);
|
|
1280
|
+
|
|
1281
|
+
return new TransformStream<Uint8Array, Draft16Message>({
|
|
1282
|
+
transform(chunk, controller) {
|
|
1283
|
+
const newBuffer = new Uint8Array(buffer.length + chunk.length);
|
|
1284
|
+
newBuffer.set(buffer, 0);
|
|
1285
|
+
newBuffer.set(chunk, buffer.length);
|
|
1286
|
+
buffer = newBuffer;
|
|
1287
|
+
|
|
1288
|
+
while (buffer.length > 0) {
|
|
1289
|
+
const result = decodeMessage(buffer);
|
|
1290
|
+
if (!result.ok) {
|
|
1291
|
+
if (result.error.code === "UNEXPECTED_END") {
|
|
1292
|
+
break;
|
|
1293
|
+
}
|
|
1294
|
+
controller.error(result.error);
|
|
1295
|
+
return;
|
|
1296
|
+
}
|
|
1297
|
+
controller.enqueue(result.value);
|
|
1298
|
+
buffer = buffer.slice(result.bytesRead);
|
|
1299
|
+
}
|
|
1300
|
+
},
|
|
1301
|
+
|
|
1302
|
+
flush(controller) {
|
|
1303
|
+
if (buffer.length > 0) {
|
|
1304
|
+
controller.error(
|
|
1305
|
+
new DecodeError("UNEXPECTED_END", "Stream ended with incomplete message data", 0),
|
|
1306
|
+
);
|
|
1307
|
+
}
|
|
1308
|
+
},
|
|
1309
|
+
});
|
|
1310
|
+
}
|
|
1311
|
+
|
|
1312
|
+
export function createSubgroupStreamDecoder(): TransformStream<
|
|
1313
|
+
Uint8Array,
|
|
1314
|
+
SubgroupStreamHeader | ObjectPayload
|
|
1315
|
+
> {
|
|
1316
|
+
let buffer = new Uint8Array(0);
|
|
1317
|
+
let headerEmitted = false;
|
|
1318
|
+
let prevObjectId = -1n;
|
|
1319
|
+
let firstObject = true;
|
|
1320
|
+
let _subgroupIsFirstObjId = false;
|
|
1321
|
+
|
|
1322
|
+
return new TransformStream<Uint8Array, SubgroupStreamHeader | ObjectPayload>({
|
|
1323
|
+
transform(chunk, controller) {
|
|
1324
|
+
const newBuffer = new Uint8Array(buffer.length + chunk.length);
|
|
1325
|
+
newBuffer.set(buffer, 0);
|
|
1326
|
+
newBuffer.set(chunk, buffer.length);
|
|
1327
|
+
buffer = newBuffer;
|
|
1328
|
+
|
|
1329
|
+
if (!headerEmitted) {
|
|
1330
|
+
try {
|
|
1331
|
+
const r = new BufferReader(buffer);
|
|
1332
|
+
const streamType = Number(r.readVarInt());
|
|
1333
|
+
|
|
1334
|
+
if (
|
|
1335
|
+
!(
|
|
1336
|
+
(streamType >= 0x10 && streamType <= 0x1d) ||
|
|
1337
|
+
(streamType >= 0x30 && streamType <= 0x3d)
|
|
1338
|
+
)
|
|
1339
|
+
) {
|
|
1340
|
+
controller.error(
|
|
1341
|
+
new DecodeError(
|
|
1342
|
+
"CONSTRAINT_VIOLATION",
|
|
1343
|
+
`Expected subgroup stream type, got 0x${streamType.toString(16)}`,
|
|
1344
|
+
0,
|
|
1345
|
+
),
|
|
1346
|
+
);
|
|
1347
|
+
return;
|
|
1348
|
+
}
|
|
1349
|
+
|
|
1350
|
+
const hasSubgroupField = (streamType & 0x04) !== 0;
|
|
1351
|
+
_subgroupIsFirstObjId = (streamType & 0x02) !== 0 && !hasSubgroupField;
|
|
1352
|
+
const hasPriority = streamType < 0x30;
|
|
1353
|
+
|
|
1354
|
+
const trackAlias = r.readVarInt();
|
|
1355
|
+
const groupId = r.readVarInt();
|
|
1356
|
+
|
|
1357
|
+
let subgroupId = 0n;
|
|
1358
|
+
if (hasSubgroupField) {
|
|
1359
|
+
subgroupId = r.readVarInt();
|
|
1360
|
+
}
|
|
1361
|
+
|
|
1362
|
+
let publisherPriority = 128;
|
|
1363
|
+
if (hasPriority) {
|
|
1364
|
+
publisherPriority = r.readUint8();
|
|
1365
|
+
}
|
|
1366
|
+
|
|
1367
|
+
controller.enqueue({
|
|
1368
|
+
type: "subgroup_header",
|
|
1369
|
+
trackAlias,
|
|
1370
|
+
groupId,
|
|
1371
|
+
subgroupId,
|
|
1372
|
+
publisherPriority,
|
|
1373
|
+
});
|
|
1374
|
+
headerEmitted = true;
|
|
1375
|
+
buffer = buffer.slice(r.offset);
|
|
1376
|
+
} catch (e) {
|
|
1377
|
+
if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
|
|
1378
|
+
return;
|
|
1379
|
+
}
|
|
1380
|
+
controller.error(e);
|
|
1381
|
+
return;
|
|
1382
|
+
}
|
|
1383
|
+
}
|
|
1384
|
+
|
|
1385
|
+
while (buffer.length > 0) {
|
|
1386
|
+
try {
|
|
1387
|
+
const r = new BufferReader(buffer);
|
|
1388
|
+
const delta = r.readVarInt();
|
|
1389
|
+
let objectId: bigint;
|
|
1390
|
+
if (firstObject) {
|
|
1391
|
+
objectId = delta;
|
|
1392
|
+
firstObject = false;
|
|
1393
|
+
} else {
|
|
1394
|
+
objectId = prevObjectId + 1n + delta;
|
|
1395
|
+
}
|
|
1396
|
+
const payloadLength = Number(r.readVarInt());
|
|
1397
|
+
const payload = payloadLength > 0 ? r.readBytes(payloadLength) : new Uint8Array(0);
|
|
1398
|
+
controller.enqueue({ type: "object", objectId, payloadLength, payload });
|
|
1399
|
+
buffer = buffer.slice(r.offset);
|
|
1400
|
+
prevObjectId = objectId;
|
|
1401
|
+
} catch (e) {
|
|
1402
|
+
if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
|
|
1403
|
+
break;
|
|
1404
|
+
}
|
|
1405
|
+
controller.error(e);
|
|
1406
|
+
return;
|
|
1407
|
+
}
|
|
1408
|
+
}
|
|
1409
|
+
},
|
|
1410
|
+
|
|
1411
|
+
flush(controller) {
|
|
1412
|
+
if (buffer.length > 0) {
|
|
1413
|
+
controller.error(new DecodeError("UNEXPECTED_END", "Stream ended with incomplete data", 0));
|
|
1414
|
+
}
|
|
1415
|
+
},
|
|
1416
|
+
});
|
|
1417
|
+
}
|
|
1418
|
+
|
|
1419
|
+
export function createFetchStreamDecoder(): TransformStream<
|
|
1420
|
+
Uint8Array,
|
|
1421
|
+
FetchStreamHeader | ObjectPayload
|
|
1422
|
+
> {
|
|
1423
|
+
let buffer = new Uint8Array(0);
|
|
1424
|
+
let headerEmitted = false;
|
|
1425
|
+
|
|
1426
|
+
return new TransformStream<Uint8Array, FetchStreamHeader | ObjectPayload>({
|
|
1427
|
+
transform(chunk, controller) {
|
|
1428
|
+
const newBuffer = new Uint8Array(buffer.length + chunk.length);
|
|
1429
|
+
newBuffer.set(buffer, 0);
|
|
1430
|
+
newBuffer.set(chunk, buffer.length);
|
|
1431
|
+
buffer = newBuffer;
|
|
1432
|
+
|
|
1433
|
+
if (!headerEmitted) {
|
|
1434
|
+
try {
|
|
1435
|
+
const r = new BufferReader(buffer);
|
|
1436
|
+
const streamType = r.readVarInt();
|
|
1437
|
+
if (streamType !== FETCH_STREAM_TYPE) {
|
|
1438
|
+
controller.error(
|
|
1439
|
+
new DecodeError(
|
|
1440
|
+
"CONSTRAINT_VIOLATION",
|
|
1441
|
+
`Expected fetch stream type 0x05, got 0x${streamType.toString(16)}`,
|
|
1442
|
+
0,
|
|
1443
|
+
),
|
|
1444
|
+
);
|
|
1445
|
+
return;
|
|
1446
|
+
}
|
|
1447
|
+
const requestId = r.readVarInt();
|
|
1448
|
+
controller.enqueue({ type: "fetch_header", requestId });
|
|
1449
|
+
headerEmitted = true;
|
|
1450
|
+
buffer = buffer.slice(r.offset);
|
|
1451
|
+
} catch (e) {
|
|
1452
|
+
if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
|
|
1453
|
+
return;
|
|
1454
|
+
}
|
|
1455
|
+
controller.error(e);
|
|
1456
|
+
return;
|
|
1457
|
+
}
|
|
1458
|
+
}
|
|
1459
|
+
|
|
1460
|
+
while (buffer.length > 0) {
|
|
1461
|
+
try {
|
|
1462
|
+
const r = new BufferReader(buffer);
|
|
1463
|
+
const flags = r.readUint8();
|
|
1464
|
+
const objectIdPresent = (flags & 0x04) !== 0;
|
|
1465
|
+
const groupIdPresent = (flags & 0x08) !== 0;
|
|
1466
|
+
const priorityPresent = (flags & 0x10) !== 0;
|
|
1467
|
+
const extensionsPresent = (flags & 0x20) !== 0;
|
|
1468
|
+
const subgroupEncoding = flags & 0x03;
|
|
1469
|
+
|
|
1470
|
+
if (groupIdPresent) r.readVarInt();
|
|
1471
|
+
if (subgroupEncoding === 0x03) r.readVarInt();
|
|
1472
|
+
let objectId = 0n;
|
|
1473
|
+
if (objectIdPresent) objectId = r.readVarInt();
|
|
1474
|
+
if (priorityPresent) r.readUint8();
|
|
1475
|
+
if (extensionsPresent) {
|
|
1476
|
+
const extLen = Number(r.readVarInt());
|
|
1477
|
+
if (extLen > 0) r.readBytes(extLen);
|
|
1478
|
+
}
|
|
1479
|
+
const payloadLength = Number(r.readVarInt());
|
|
1480
|
+
const payload = payloadLength > 0 ? r.readBytes(payloadLength) : new Uint8Array(0);
|
|
1481
|
+
controller.enqueue({ type: "object", objectId, payloadLength, payload });
|
|
1482
|
+
buffer = buffer.slice(r.offset);
|
|
1483
|
+
} catch (e) {
|
|
1484
|
+
if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
|
|
1485
|
+
break;
|
|
1486
|
+
}
|
|
1487
|
+
controller.error(e);
|
|
1488
|
+
return;
|
|
1489
|
+
}
|
|
1490
|
+
}
|
|
1491
|
+
},
|
|
1492
|
+
|
|
1493
|
+
flush(controller) {
|
|
1494
|
+
if (buffer.length > 0) {
|
|
1495
|
+
controller.error(new DecodeError("UNEXPECTED_END", "Stream ended with incomplete data", 0));
|
|
1496
|
+
}
|
|
1497
|
+
},
|
|
1498
|
+
});
|
|
1499
|
+
}
|
|
1500
|
+
|
|
1501
|
+
export function createDataStreamDecoder(): TransformStream<Uint8Array, DataStreamEvent> {
|
|
1502
|
+
let buffer = new Uint8Array(0);
|
|
1503
|
+
let inner: TransformStream<Uint8Array, DataStreamEvent> | null = null;
|
|
1504
|
+
|
|
1505
|
+
return new TransformStream<Uint8Array, DataStreamEvent>({
|
|
1506
|
+
transform(chunk, controller) {
|
|
1507
|
+
const newBuffer = new Uint8Array(buffer.length + chunk.length);
|
|
1508
|
+
newBuffer.set(buffer, 0);
|
|
1509
|
+
newBuffer.set(chunk, buffer.length);
|
|
1510
|
+
buffer = newBuffer;
|
|
1511
|
+
|
|
1512
|
+
if (inner === null) {
|
|
1513
|
+
if (buffer.length === 0) return;
|
|
1514
|
+
const firstByte = buffer[0]!;
|
|
1515
|
+
|
|
1516
|
+
if ((firstByte >= 0x10 && firstByte <= 0x1d) || (firstByte >= 0x30 && firstByte <= 0x3d)) {
|
|
1517
|
+
const decoder = createSubgroupStreamDecoder();
|
|
1518
|
+
inner = decoder as unknown as TransformStream<Uint8Array, DataStreamEvent>;
|
|
1519
|
+
} else if (firstByte === 0x05) {
|
|
1520
|
+
const decoder = createFetchStreamDecoder();
|
|
1521
|
+
inner = decoder as unknown as TransformStream<Uint8Array, DataStreamEvent>;
|
|
1522
|
+
} else {
|
|
1523
|
+
controller.error(
|
|
1524
|
+
new DecodeError(
|
|
1525
|
+
"CONSTRAINT_VIOLATION",
|
|
1526
|
+
`Unknown data stream type: 0x${firstByte.toString(16)}`,
|
|
1527
|
+
0,
|
|
1528
|
+
),
|
|
1529
|
+
);
|
|
1530
|
+
return;
|
|
1531
|
+
}
|
|
1532
|
+
}
|
|
1533
|
+
},
|
|
1534
|
+
|
|
1535
|
+
flush(controller) {
|
|
1536
|
+
if (buffer.length === 0) return;
|
|
1537
|
+
|
|
1538
|
+
const firstByte = buffer[0]!;
|
|
1539
|
+
let result: DecodeResult<Draft16DataStream>;
|
|
1540
|
+
|
|
1541
|
+
if ((firstByte >= 0x10 && firstByte <= 0x1d) || (firstByte >= 0x30 && firstByte <= 0x3d)) {
|
|
1542
|
+
result = decodeSubgroupStream(buffer);
|
|
1543
|
+
} else if (firstByte === 0x05) {
|
|
1544
|
+
result = decodeFetchStream(buffer);
|
|
1545
|
+
} else {
|
|
1546
|
+
controller.error(
|
|
1547
|
+
new DecodeError(
|
|
1548
|
+
"CONSTRAINT_VIOLATION",
|
|
1549
|
+
`Unknown data stream type: 0x${firstByte.toString(16)}`,
|
|
1550
|
+
0,
|
|
1551
|
+
),
|
|
1552
|
+
);
|
|
1553
|
+
return;
|
|
1554
|
+
}
|
|
1555
|
+
|
|
1556
|
+
if (!result.ok) {
|
|
1557
|
+
controller.error(result.error);
|
|
1558
|
+
return;
|
|
1559
|
+
}
|
|
1560
|
+
|
|
1561
|
+
const stream = result.value;
|
|
1562
|
+
if (stream.type === "subgroup") {
|
|
1563
|
+
controller.enqueue({
|
|
1564
|
+
type: "subgroup_header",
|
|
1565
|
+
trackAlias: stream.trackAlias,
|
|
1566
|
+
groupId: stream.groupId,
|
|
1567
|
+
subgroupId: stream.subgroupId,
|
|
1568
|
+
publisherPriority: stream.publisherPriority,
|
|
1569
|
+
});
|
|
1570
|
+
for (const obj of stream.objects) {
|
|
1571
|
+
controller.enqueue(obj);
|
|
1572
|
+
}
|
|
1573
|
+
} else if (stream.type === "fetch") {
|
|
1574
|
+
controller.enqueue({
|
|
1575
|
+
type: "fetch_header",
|
|
1576
|
+
requestId: stream.requestId,
|
|
1577
|
+
});
|
|
1578
|
+
for (const obj of stream.objects) {
|
|
1579
|
+
controller.enqueue(obj);
|
|
1580
|
+
}
|
|
1581
|
+
}
|
|
1582
|
+
},
|
|
1583
|
+
});
|
|
1584
|
+
}
|
|
1585
|
+
|
|
1586
|
+
// ─── Codec Factory ─────────────────────────────────────────────────────────────
|
|
1587
|
+
|
|
1588
|
+
export interface Draft16Codec extends BaseCodec<Draft16Message> {
|
|
1589
|
+
readonly draft: "draft-ietf-moq-transport-16";
|
|
1590
|
+
encodeSubgroupStream(stream: SubgroupStream): Uint8Array;
|
|
1591
|
+
encodeDatagram(dg: DatagramObject): Uint8Array;
|
|
1592
|
+
encodeFetchStream(stream: FetchStream): Uint8Array;
|
|
1593
|
+
decodeSubgroupStream(bytes: Uint8Array): DecodeResult<SubgroupStream>;
|
|
1594
|
+
decodeDatagram(bytes: Uint8Array): DecodeResult<DatagramObject>;
|
|
1595
|
+
decodeFetchStream(bytes: Uint8Array): DecodeResult<FetchStream>;
|
|
1596
|
+
decodeDataStream(
|
|
1597
|
+
streamType: "subgroup" | "datagram" | "fetch",
|
|
1598
|
+
bytes: Uint8Array,
|
|
1599
|
+
): DecodeResult<Draft16DataStream>;
|
|
1600
|
+
createStreamDecoder(): TransformStream<Uint8Array, Draft16Message>;
|
|
1601
|
+
createSubgroupStreamDecoder(): TransformStream<Uint8Array, SubgroupStreamHeader | ObjectPayload>;
|
|
1602
|
+
createFetchStreamDecoder(): TransformStream<Uint8Array, FetchStreamHeader | ObjectPayload>;
|
|
1603
|
+
createDataStreamDecoder(): TransformStream<Uint8Array, DataStreamEvent>;
|
|
1604
|
+
}
|
|
1605
|
+
|
|
1606
|
+
export function createDraft16Codec(): Draft16Codec {
|
|
1607
|
+
return {
|
|
1608
|
+
draft: "draft-ietf-moq-transport-16",
|
|
1609
|
+
encodeMessage,
|
|
1610
|
+
decodeMessage,
|
|
1611
|
+
encodeSubgroupStream,
|
|
1612
|
+
encodeDatagram,
|
|
1613
|
+
encodeFetchStream,
|
|
1614
|
+
decodeSubgroupStream,
|
|
1615
|
+
decodeDatagram,
|
|
1616
|
+
decodeFetchStream,
|
|
1617
|
+
decodeDataStream,
|
|
1618
|
+
createStreamDecoder,
|
|
1619
|
+
createSubgroupStreamDecoder,
|
|
1620
|
+
createFetchStreamDecoder,
|
|
1621
|
+
createDataStreamDecoder,
|
|
1622
|
+
};
|
|
1623
|
+
}
|