@moqtap/codec 0.1.0 → 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-WNTXF3DE.cjs → chunk-7IVGHMKJ.cjs} +164 -62
- package/dist/{chunk-YBSEOSSP.js → chunk-A27S7HW7.js} +5 -1
- package/dist/chunk-BISI45MN.cjs +680 -0
- package/dist/{chunk-3BSZ55L3.cjs → chunk-BPNL5YFQ.cjs} +158 -24
- 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-5WFXFLL4.cjs → chunk-FUFTMAQD.cjs} +96 -63
- package/dist/{chunk-2NARXGVA.cjs → chunk-FWISIR26.cjs} +5 -1
- package/dist/{chunk-YPXLV5YK.js → chunk-FXZ2MYKJ.js} +376 -38
- 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-DC4L6ZIT.js → chunk-MFAP7R6L.js} +154 -20
- 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-23YG7F46.js → chunk-TMNGRIPL.js} +153 -51
- package/dist/{chunk-IQPDRQVC.js → chunk-U2B3B42P.js} +62 -29
- package/dist/chunk-UNS34PTA.cjs +680 -0
- package/dist/chunk-UR6JKS56.js +432 -0
- package/dist/{chunk-GDRGWFEK.cjs → chunk-UYXTY6ZQ.cjs} +376 -38
- 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-CTvFtQQI.d.cts → codec-CpuvYTSV.d.cts} +5 -5
- 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-qPzfmLNu.d.ts → codec-DRhCx_hw.d.ts} +5 -5
- 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 +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 +27 -15
- package/dist/draft14.d.ts +27 -15
- package/dist/draft14.js +3 -3
- 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 +6 -6
- package/dist/draft7.d.cts +10 -10
- package/dist/draft7.d.ts +10 -10
- package/dist/draft7.js +3 -3
- 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 +79 -7
- package/dist/index.d.cts +71 -8
- package/dist/index.d.ts +71 -8
- package/dist/index.js +77 -5
- package/dist/{session-types-B9NIf7_F.d.ts → session-types-CJIFbTPd.d.ts} +20 -20
- package/dist/{session-types-CCo-oA-d.d.cts → session-types-Cbq8IGCP.d.cts} +20 -20
- 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-CIk5W10V.d.ts → types-BTFeKYCb.d.cts} +37 -37
- package/dist/{types-CIk5W10V.d.cts → types-BTFeKYCb.d.ts} +37 -37
- 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-ClXELFGN.d.cts → types-QNXoxC9Y.d.cts} +36 -41
- package/dist/{types-ClXELFGN.d.ts → types-QNXoxC9Y.d.ts} +36 -41
- package/dist/types-r-CasCf1.d.cts +262 -0
- package/dist/types-r-CasCf1.d.ts +262 -0
- package/package.json +116 -8
- 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 +92 -75
- package/src/drafts/draft07/announce-fsm.ts +1 -1
- package/src/drafts/draft07/codec.ts +235 -118
- 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 +67 -38
- 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/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/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 +640 -147
- package/src/drafts/draft14/session.ts +13 -13
- package/src/drafts/draft14/types.ts +68 -68
- 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 +283 -33
- package/src/session.ts +20 -20
|
@@ -0,0 +1,1401 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BufferReader,
|
|
3
|
+
BufferWriter,
|
|
4
|
+
DecodeError
|
|
5
|
+
} from "./chunk-A27S7HW7.js";
|
|
6
|
+
|
|
7
|
+
// src/drafts/draft17/messages.ts
|
|
8
|
+
var MSG_REQUEST_UPDATE = 0x02n;
|
|
9
|
+
var MSG_SUBSCRIBE = 0x03n;
|
|
10
|
+
var MSG_SUBSCRIBE_OK = 0x04n;
|
|
11
|
+
var MSG_REQUEST_ERROR = 0x05n;
|
|
12
|
+
var MSG_PUBLISH_NAMESPACE = 0x06n;
|
|
13
|
+
var MSG_REQUEST_OK = 0x07n;
|
|
14
|
+
var MSG_NAMESPACE = 0x08n;
|
|
15
|
+
var MSG_PUBLISH_DONE = 0x0bn;
|
|
16
|
+
var MSG_TRACK_STATUS = 0x0dn;
|
|
17
|
+
var MSG_NAMESPACE_DONE = 0x0en;
|
|
18
|
+
var MSG_PUBLISH_BLOCKED = 0x0fn;
|
|
19
|
+
var MSG_GOAWAY = 0x10n;
|
|
20
|
+
var MSG_SUBSCRIBE_NAMESPACE = 0x11n;
|
|
21
|
+
var MSG_FETCH = 0x16n;
|
|
22
|
+
var MSG_FETCH_OK = 0x18n;
|
|
23
|
+
var MSG_PUBLISH = 0x1dn;
|
|
24
|
+
var MSG_PUBLISH_OK = 0x1en;
|
|
25
|
+
var MSG_SETUP = 0x2f00n;
|
|
26
|
+
var SETUP_OPT_PATH = 0x01n;
|
|
27
|
+
var SETUP_OPT_MAX_AUTH_TOKEN_CACHE_SIZE = 0x04n;
|
|
28
|
+
var SETUP_OPT_AUTHORITY = 0x05n;
|
|
29
|
+
var SETUP_OPT_MOQT_IMPLEMENTATION = 0x07n;
|
|
30
|
+
var MESSAGE_TYPE_MAP = /* @__PURE__ */ new Map([
|
|
31
|
+
[MSG_SETUP, "setup"],
|
|
32
|
+
[MSG_SUBSCRIBE, "subscribe"],
|
|
33
|
+
[MSG_SUBSCRIBE_OK, "subscribe_ok"],
|
|
34
|
+
[MSG_REQUEST_UPDATE, "request_update"],
|
|
35
|
+
[MSG_PUBLISH, "publish"],
|
|
36
|
+
[MSG_PUBLISH_OK, "publish_ok"],
|
|
37
|
+
[MSG_PUBLISH_DONE, "publish_done"],
|
|
38
|
+
[MSG_PUBLISH_NAMESPACE, "publish_namespace"],
|
|
39
|
+
[MSG_NAMESPACE, "namespace"],
|
|
40
|
+
[MSG_NAMESPACE_DONE, "namespace_done"],
|
|
41
|
+
[MSG_SUBSCRIBE_NAMESPACE, "subscribe_namespace"],
|
|
42
|
+
[MSG_PUBLISH_BLOCKED, "publish_blocked"],
|
|
43
|
+
[MSG_FETCH, "fetch"],
|
|
44
|
+
[MSG_FETCH_OK, "fetch_ok"],
|
|
45
|
+
[MSG_TRACK_STATUS, "track_status"],
|
|
46
|
+
[MSG_REQUEST_OK, "request_ok"],
|
|
47
|
+
[MSG_REQUEST_ERROR, "request_error"],
|
|
48
|
+
[MSG_GOAWAY, "goaway"]
|
|
49
|
+
]);
|
|
50
|
+
var MESSAGE_ID_MAP = new Map(
|
|
51
|
+
[...MESSAGE_TYPE_MAP.entries()].map(([id, name]) => [name, id])
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
// src/drafts/draft17/codec.ts
|
|
55
|
+
function bytesToHex(bytes) {
|
|
56
|
+
let hex = "";
|
|
57
|
+
for (let i = 0; i < bytes.byteLength; i++) {
|
|
58
|
+
hex += bytes[i].toString(16).padStart(2, "0");
|
|
59
|
+
}
|
|
60
|
+
return hex;
|
|
61
|
+
}
|
|
62
|
+
function hexToBytes(hex) {
|
|
63
|
+
const bytes = new Uint8Array(hex.length / 2);
|
|
64
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
65
|
+
bytes[i / 2] = parseInt(hex.substring(i, i + 2), 16);
|
|
66
|
+
}
|
|
67
|
+
return bytes;
|
|
68
|
+
}
|
|
69
|
+
function encodeSetupOptions(opts, writer) {
|
|
70
|
+
const entries = [];
|
|
71
|
+
if (opts.path !== void 0) {
|
|
72
|
+
entries.push({
|
|
73
|
+
type: SETUP_OPT_PATH,
|
|
74
|
+
encode: (w) => {
|
|
75
|
+
const encoded = new TextEncoder().encode(opts.path);
|
|
76
|
+
w.writeVarInt(BigInt(encoded.byteLength));
|
|
77
|
+
w.writeBytes(encoded);
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
if (opts.max_auth_token_cache_size !== void 0) {
|
|
82
|
+
entries.push({
|
|
83
|
+
type: SETUP_OPT_MAX_AUTH_TOKEN_CACHE_SIZE,
|
|
84
|
+
encode: (w) => w.writeVarInt(opts.max_auth_token_cache_size)
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
if (opts.authority !== void 0) {
|
|
88
|
+
entries.push({
|
|
89
|
+
type: SETUP_OPT_AUTHORITY,
|
|
90
|
+
encode: (w) => {
|
|
91
|
+
const encoded = new TextEncoder().encode(opts.authority);
|
|
92
|
+
w.writeVarInt(BigInt(encoded.byteLength));
|
|
93
|
+
w.writeBytes(encoded);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
if (opts.moqt_implementation !== void 0) {
|
|
98
|
+
entries.push({
|
|
99
|
+
type: SETUP_OPT_MOQT_IMPLEMENTATION,
|
|
100
|
+
encode: (w) => {
|
|
101
|
+
const encoded = new TextEncoder().encode(opts.moqt_implementation);
|
|
102
|
+
w.writeVarInt(BigInt(encoded.byteLength));
|
|
103
|
+
w.writeBytes(encoded);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
if (opts.unknown) {
|
|
108
|
+
for (const u of opts.unknown) {
|
|
109
|
+
const id = BigInt(u.id);
|
|
110
|
+
entries.push({
|
|
111
|
+
type: id,
|
|
112
|
+
encode: (w) => {
|
|
113
|
+
if (id % 2n === 0n) {
|
|
114
|
+
const raw = hexToBytes(u.raw_hex);
|
|
115
|
+
const tmpReader = new BufferReader(raw);
|
|
116
|
+
w.writeVarInt(tmpReader.readVarInt());
|
|
117
|
+
} else {
|
|
118
|
+
const raw = hexToBytes(u.raw_hex);
|
|
119
|
+
w.writeVarInt(BigInt(raw.byteLength));
|
|
120
|
+
w.writeBytes(raw);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
entries.sort((a, b) => a.type < b.type ? -1 : a.type > b.type ? 1 : 0);
|
|
127
|
+
let prevType = 0n;
|
|
128
|
+
for (const entry of entries) {
|
|
129
|
+
writer.writeVarInt(entry.type - prevType);
|
|
130
|
+
entry.encode(writer);
|
|
131
|
+
prevType = entry.type;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
function decodeSetupOptions(reader, payloadEnd) {
|
|
135
|
+
const result = {};
|
|
136
|
+
const unknown = [];
|
|
137
|
+
let prevType = 0n;
|
|
138
|
+
while (reader.offset < payloadEnd) {
|
|
139
|
+
const delta = reader.readVarInt();
|
|
140
|
+
const optType = prevType + delta;
|
|
141
|
+
prevType = optType;
|
|
142
|
+
if (optType % 2n === 0n) {
|
|
143
|
+
const value = reader.readVarInt();
|
|
144
|
+
if (optType === SETUP_OPT_MAX_AUTH_TOKEN_CACHE_SIZE) {
|
|
145
|
+
result.max_auth_token_cache_size = value;
|
|
146
|
+
} else {
|
|
147
|
+
const tmpWriter = new BufferWriter(16);
|
|
148
|
+
tmpWriter.writeVarInt(value);
|
|
149
|
+
const raw = tmpWriter.finish();
|
|
150
|
+
unknown.push({
|
|
151
|
+
id: `0x${optType.toString(16)}`,
|
|
152
|
+
length: raw.byteLength,
|
|
153
|
+
raw_hex: bytesToHex(raw)
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
} else {
|
|
157
|
+
const length = Number(reader.readVarInt());
|
|
158
|
+
const bytes = reader.readBytes(length);
|
|
159
|
+
if (optType === SETUP_OPT_PATH) {
|
|
160
|
+
result.path = new TextDecoder().decode(bytes);
|
|
161
|
+
} else if (optType === SETUP_OPT_AUTHORITY) {
|
|
162
|
+
result.authority = new TextDecoder().decode(bytes);
|
|
163
|
+
} else if (optType === SETUP_OPT_MOQT_IMPLEMENTATION) {
|
|
164
|
+
result.moqt_implementation = new TextDecoder().decode(bytes);
|
|
165
|
+
} else {
|
|
166
|
+
unknown.push({ id: `0x${optType.toString(16)}`, length, raw_hex: bytesToHex(bytes) });
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (unknown.length > 0) result.unknown = unknown;
|
|
171
|
+
return result;
|
|
172
|
+
}
|
|
173
|
+
var PARAM_EXPIRES = 0x08n;
|
|
174
|
+
var PARAM_LARGEST_OBJECT = 0x09n;
|
|
175
|
+
var PARAM_SUBSCRIBER_PRIORITY = 0x20n;
|
|
176
|
+
var PARAM_SUBSCRIPTION_FILTER = 0x21n;
|
|
177
|
+
var PARAM_GROUP_ORDER = 0x22n;
|
|
178
|
+
function encodeParams(params, writer) {
|
|
179
|
+
const entries = [];
|
|
180
|
+
if (params.expires !== void 0) {
|
|
181
|
+
entries.push({ type: PARAM_EXPIRES, encode: (w) => w.writeVarInt(params.expires) });
|
|
182
|
+
}
|
|
183
|
+
if (params.largest_object !== void 0) {
|
|
184
|
+
entries.push({
|
|
185
|
+
type: PARAM_LARGEST_OBJECT,
|
|
186
|
+
encode: (w) => {
|
|
187
|
+
w.writeVarInt(params.largest_object.group);
|
|
188
|
+
w.writeVarInt(params.largest_object.object);
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
if (params.subscriber_priority !== void 0) {
|
|
193
|
+
entries.push({
|
|
194
|
+
type: PARAM_SUBSCRIBER_PRIORITY,
|
|
195
|
+
encode: (w) => w.writeUint8(Number(params.subscriber_priority))
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
if (params.subscription_filter !== void 0) {
|
|
199
|
+
entries.push({
|
|
200
|
+
type: PARAM_SUBSCRIPTION_FILTER,
|
|
201
|
+
encode: (w) => {
|
|
202
|
+
const f = params.subscription_filter;
|
|
203
|
+
const tmpW = new BufferWriter(32);
|
|
204
|
+
tmpW.writeVarInt(f.filter_type);
|
|
205
|
+
if (f.filter_type === 3n || f.filter_type === 4n) {
|
|
206
|
+
tmpW.writeVarInt(f.start_group);
|
|
207
|
+
tmpW.writeVarInt(f.start_object);
|
|
208
|
+
}
|
|
209
|
+
if (f.filter_type === 4n) {
|
|
210
|
+
tmpW.writeVarInt(f.end_group);
|
|
211
|
+
}
|
|
212
|
+
const raw = tmpW.finish();
|
|
213
|
+
w.writeVarInt(BigInt(raw.byteLength));
|
|
214
|
+
w.writeBytes(raw);
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
if (params.group_order !== void 0) {
|
|
219
|
+
entries.push({
|
|
220
|
+
type: PARAM_GROUP_ORDER,
|
|
221
|
+
encode: (w) => w.writeUint8(Number(params.group_order))
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
if (params.unknown) {
|
|
225
|
+
for (const u of params.unknown) {
|
|
226
|
+
const id = BigInt(u.id);
|
|
227
|
+
entries.push({
|
|
228
|
+
type: id,
|
|
229
|
+
encode: (w) => {
|
|
230
|
+
const raw = hexToBytes(u.raw_hex);
|
|
231
|
+
w.writeBytes(raw);
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
entries.sort((a, b) => a.type < b.type ? -1 : a.type > b.type ? 1 : 0);
|
|
237
|
+
writer.writeVarInt(BigInt(entries.length));
|
|
238
|
+
let prevType = 0n;
|
|
239
|
+
for (const entry of entries) {
|
|
240
|
+
writer.writeVarInt(entry.type - prevType);
|
|
241
|
+
entry.encode(writer);
|
|
242
|
+
prevType = entry.type;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
function decodeParams(reader) {
|
|
246
|
+
const count = Number(reader.readVarInt());
|
|
247
|
+
const result = {};
|
|
248
|
+
const unknown = [];
|
|
249
|
+
let prevType = 0n;
|
|
250
|
+
for (let i = 0; i < count; i++) {
|
|
251
|
+
const delta = reader.readVarInt();
|
|
252
|
+
const paramType = prevType + delta;
|
|
253
|
+
prevType = paramType;
|
|
254
|
+
if (paramType === PARAM_EXPIRES) {
|
|
255
|
+
result.expires = reader.readVarInt();
|
|
256
|
+
} else if (paramType === PARAM_LARGEST_OBJECT) {
|
|
257
|
+
const group = reader.readVarInt();
|
|
258
|
+
const object = reader.readVarInt();
|
|
259
|
+
result.largest_object = { group, object };
|
|
260
|
+
} else if (paramType === PARAM_SUBSCRIBER_PRIORITY) {
|
|
261
|
+
result.subscriber_priority = BigInt(reader.readUint8());
|
|
262
|
+
} else if (paramType === PARAM_SUBSCRIPTION_FILTER) {
|
|
263
|
+
const length = Number(reader.readVarInt());
|
|
264
|
+
const startOff = reader.offset;
|
|
265
|
+
const filter_type = reader.readVarInt();
|
|
266
|
+
const filter = { filter_type };
|
|
267
|
+
if (filter_type === 3n || filter_type === 4n) {
|
|
268
|
+
filter.start_group = reader.readVarInt();
|
|
269
|
+
filter.start_object = reader.readVarInt();
|
|
270
|
+
}
|
|
271
|
+
if (filter_type === 4n) {
|
|
272
|
+
filter.end_group = reader.readVarInt();
|
|
273
|
+
}
|
|
274
|
+
const consumed = reader.offset - startOff;
|
|
275
|
+
if (consumed < length) reader.readBytes(length - consumed);
|
|
276
|
+
result.subscription_filter = filter;
|
|
277
|
+
} else if (paramType === PARAM_GROUP_ORDER) {
|
|
278
|
+
result.group_order = BigInt(reader.readUint8());
|
|
279
|
+
} else {
|
|
280
|
+
if (paramType % 2n === 0n) {
|
|
281
|
+
const value = reader.readVarInt();
|
|
282
|
+
const tmpWriter = new BufferWriter(16);
|
|
283
|
+
tmpWriter.writeVarInt(value);
|
|
284
|
+
const raw = tmpWriter.finish();
|
|
285
|
+
unknown.push({
|
|
286
|
+
id: `0x${paramType.toString(16)}`,
|
|
287
|
+
length: raw.byteLength,
|
|
288
|
+
raw_hex: bytesToHex(raw)
|
|
289
|
+
});
|
|
290
|
+
} else {
|
|
291
|
+
const length = Number(reader.readVarInt());
|
|
292
|
+
const bytes = reader.readBytes(length);
|
|
293
|
+
unknown.push({ id: `0x${paramType.toString(16)}`, length, raw_hex: bytesToHex(bytes) });
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
if (unknown.length > 0) result.unknown = unknown;
|
|
298
|
+
return result;
|
|
299
|
+
}
|
|
300
|
+
function encodeTrackProperties(props, writer) {
|
|
301
|
+
if (!props.unknown || props.unknown.length === 0) return;
|
|
302
|
+
const entries = props.unknown.map((u) => ({ type: BigInt(u.id), raw: u }));
|
|
303
|
+
entries.sort((a, b) => a.type < b.type ? -1 : a.type > b.type ? 1 : 0);
|
|
304
|
+
let prevType = 0n;
|
|
305
|
+
for (const entry of entries) {
|
|
306
|
+
writer.writeVarInt(entry.type - prevType);
|
|
307
|
+
if (entry.type % 2n === 0n) {
|
|
308
|
+
const raw = hexToBytes(entry.raw.raw_hex);
|
|
309
|
+
const tmpReader = new BufferReader(raw);
|
|
310
|
+
writer.writeVarInt(tmpReader.readVarInt());
|
|
311
|
+
} else {
|
|
312
|
+
const raw = hexToBytes(entry.raw.raw_hex);
|
|
313
|
+
writer.writeVarInt(BigInt(raw.byteLength));
|
|
314
|
+
writer.writeBytes(raw);
|
|
315
|
+
}
|
|
316
|
+
prevType = entry.type;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
function decodeTrackProperties(reader, payloadEnd) {
|
|
320
|
+
const result = {};
|
|
321
|
+
const unknown = [];
|
|
322
|
+
let prevType = 0n;
|
|
323
|
+
while (reader.offset < payloadEnd) {
|
|
324
|
+
const delta = reader.readVarInt();
|
|
325
|
+
const propType = prevType + delta;
|
|
326
|
+
prevType = propType;
|
|
327
|
+
if (propType % 2n === 0n) {
|
|
328
|
+
const value = reader.readVarInt();
|
|
329
|
+
const tmpWriter = new BufferWriter(16);
|
|
330
|
+
tmpWriter.writeVarInt(value);
|
|
331
|
+
const raw = tmpWriter.finish();
|
|
332
|
+
unknown.push({
|
|
333
|
+
id: `0x${propType.toString(16)}`,
|
|
334
|
+
length: raw.byteLength,
|
|
335
|
+
raw_hex: bytesToHex(raw)
|
|
336
|
+
});
|
|
337
|
+
} else {
|
|
338
|
+
const length = Number(reader.readVarInt());
|
|
339
|
+
const bytes = reader.readBytes(length);
|
|
340
|
+
unknown.push({ id: `0x${propType.toString(16)}`, length, raw_hex: bytesToHex(bytes) });
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
if (unknown.length > 0) result.unknown = unknown;
|
|
344
|
+
return result;
|
|
345
|
+
}
|
|
346
|
+
function encodeSetupPayload(msg, w) {
|
|
347
|
+
encodeSetupOptions(msg.options, w);
|
|
348
|
+
}
|
|
349
|
+
function encodeSubscribePayload(msg, w) {
|
|
350
|
+
w.writeVarInt(msg.request_id);
|
|
351
|
+
w.writeVarInt(msg.required_request_id_delta);
|
|
352
|
+
w.writeTuple(msg.track_namespace);
|
|
353
|
+
w.writeString(msg.track_name);
|
|
354
|
+
encodeParams(msg.parameters, w);
|
|
355
|
+
}
|
|
356
|
+
function encodeSubscribeOkPayload(msg, w) {
|
|
357
|
+
w.writeVarInt(msg.track_alias);
|
|
358
|
+
encodeParams(msg.parameters, w);
|
|
359
|
+
encodeTrackProperties(msg.track_properties, w);
|
|
360
|
+
}
|
|
361
|
+
function encodeRequestUpdatePayload(msg, w) {
|
|
362
|
+
w.writeVarInt(msg.request_id);
|
|
363
|
+
w.writeVarInt(msg.required_request_id_delta);
|
|
364
|
+
encodeParams(msg.parameters, w);
|
|
365
|
+
}
|
|
366
|
+
function encodePublishPayload(msg, w) {
|
|
367
|
+
w.writeVarInt(msg.request_id);
|
|
368
|
+
w.writeVarInt(msg.required_request_id_delta);
|
|
369
|
+
w.writeTuple(msg.track_namespace);
|
|
370
|
+
w.writeString(msg.track_name);
|
|
371
|
+
w.writeVarInt(msg.track_alias);
|
|
372
|
+
encodeParams(msg.parameters, w);
|
|
373
|
+
encodeTrackProperties(msg.track_properties, w);
|
|
374
|
+
}
|
|
375
|
+
function encodePublishOkPayload(msg, w) {
|
|
376
|
+
encodeParams(msg.parameters, w);
|
|
377
|
+
}
|
|
378
|
+
function encodePublishDonePayload(msg, w) {
|
|
379
|
+
w.writeVarInt(msg.status_code);
|
|
380
|
+
w.writeVarInt(msg.stream_count);
|
|
381
|
+
w.writeString(msg.reason_phrase);
|
|
382
|
+
}
|
|
383
|
+
function encodePublishNamespacePayload(msg, w) {
|
|
384
|
+
w.writeVarInt(msg.request_id);
|
|
385
|
+
w.writeVarInt(msg.required_request_id_delta);
|
|
386
|
+
w.writeTuple(msg.track_namespace);
|
|
387
|
+
encodeParams(msg.parameters, w);
|
|
388
|
+
}
|
|
389
|
+
function encodeNamespacePayload(msg, w) {
|
|
390
|
+
w.writeTuple(msg.namespace_suffix);
|
|
391
|
+
}
|
|
392
|
+
function encodeNamespaceDonePayload(msg, w) {
|
|
393
|
+
w.writeTuple(msg.namespace_suffix);
|
|
394
|
+
}
|
|
395
|
+
function encodeSubscribeNamespacePayload(msg, w) {
|
|
396
|
+
w.writeVarInt(msg.request_id);
|
|
397
|
+
w.writeVarInt(msg.required_request_id_delta);
|
|
398
|
+
w.writeTuple(msg.namespace_prefix);
|
|
399
|
+
w.writeVarInt(msg.subscribe_options);
|
|
400
|
+
encodeParams(msg.parameters, w);
|
|
401
|
+
}
|
|
402
|
+
function encodePublishBlockedPayload(msg, w) {
|
|
403
|
+
w.writeTuple(msg.namespace_suffix);
|
|
404
|
+
w.writeString(msg.track_name);
|
|
405
|
+
}
|
|
406
|
+
function encodeFetchPayload(msg, w) {
|
|
407
|
+
w.writeVarInt(msg.request_id);
|
|
408
|
+
w.writeVarInt(msg.required_request_id_delta);
|
|
409
|
+
w.writeVarInt(msg.fetch_type);
|
|
410
|
+
const ft = Number(msg.fetch_type);
|
|
411
|
+
if (ft === 1 && msg.standalone) {
|
|
412
|
+
w.writeTuple(msg.standalone.track_namespace);
|
|
413
|
+
w.writeString(msg.standalone.track_name);
|
|
414
|
+
w.writeVarInt(msg.standalone.start_group);
|
|
415
|
+
w.writeVarInt(msg.standalone.start_object);
|
|
416
|
+
w.writeVarInt(msg.standalone.end_group);
|
|
417
|
+
w.writeVarInt(msg.standalone.end_object);
|
|
418
|
+
} else if ((ft === 2 || ft === 3) && msg.joining) {
|
|
419
|
+
w.writeVarInt(msg.joining.joining_request_id);
|
|
420
|
+
w.writeVarInt(msg.joining.joining_start);
|
|
421
|
+
}
|
|
422
|
+
encodeParams(msg.parameters, w);
|
|
423
|
+
}
|
|
424
|
+
function encodeFetchOkPayload(msg, w) {
|
|
425
|
+
w.writeUint8(msg.end_of_track);
|
|
426
|
+
w.writeVarInt(msg.end_group);
|
|
427
|
+
w.writeVarInt(msg.end_object);
|
|
428
|
+
encodeParams(msg.parameters, w);
|
|
429
|
+
encodeTrackProperties(msg.track_properties, w);
|
|
430
|
+
}
|
|
431
|
+
function encodeTrackStatusPayload(msg, w) {
|
|
432
|
+
w.writeVarInt(msg.request_id);
|
|
433
|
+
w.writeVarInt(msg.required_request_id_delta);
|
|
434
|
+
w.writeTuple(msg.track_namespace);
|
|
435
|
+
w.writeString(msg.track_name);
|
|
436
|
+
encodeParams(msg.parameters, w);
|
|
437
|
+
}
|
|
438
|
+
function encodeRequestOkPayload(msg, w) {
|
|
439
|
+
encodeParams(msg.parameters, w);
|
|
440
|
+
}
|
|
441
|
+
function encodeRequestErrorPayload(msg, w) {
|
|
442
|
+
w.writeVarInt(msg.error_code);
|
|
443
|
+
w.writeVarInt(msg.retry_interval);
|
|
444
|
+
w.writeString(msg.reason_phrase);
|
|
445
|
+
}
|
|
446
|
+
function encodeGoAwayPayload(msg, w) {
|
|
447
|
+
w.writeString(msg.new_session_uri);
|
|
448
|
+
w.writeVarInt(msg.timeout);
|
|
449
|
+
}
|
|
450
|
+
function decodeSetupPayload(r, payloadEnd) {
|
|
451
|
+
const options = decodeSetupOptions(r, payloadEnd);
|
|
452
|
+
return { type: "setup", options };
|
|
453
|
+
}
|
|
454
|
+
function decodeSubscribePayload(r) {
|
|
455
|
+
const request_id = r.readVarInt();
|
|
456
|
+
const required_request_id_delta = r.readVarInt();
|
|
457
|
+
const track_namespace = r.readTuple();
|
|
458
|
+
const track_name = r.readString();
|
|
459
|
+
const parameters = decodeParams(r);
|
|
460
|
+
return {
|
|
461
|
+
type: "subscribe",
|
|
462
|
+
request_id,
|
|
463
|
+
required_request_id_delta,
|
|
464
|
+
track_namespace,
|
|
465
|
+
track_name,
|
|
466
|
+
parameters
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
function decodeSubscribeOkPayload(r, payloadEnd) {
|
|
470
|
+
const track_alias = r.readVarInt();
|
|
471
|
+
const parameters = decodeParams(r);
|
|
472
|
+
const track_properties = decodeTrackProperties(r, payloadEnd);
|
|
473
|
+
return { type: "subscribe_ok", track_alias, parameters, track_properties };
|
|
474
|
+
}
|
|
475
|
+
function decodeRequestUpdatePayload(r) {
|
|
476
|
+
const request_id = r.readVarInt();
|
|
477
|
+
const required_request_id_delta = r.readVarInt();
|
|
478
|
+
const parameters = decodeParams(r);
|
|
479
|
+
return { type: "request_update", request_id, required_request_id_delta, parameters };
|
|
480
|
+
}
|
|
481
|
+
function decodePublishPayload(r, payloadEnd) {
|
|
482
|
+
const request_id = r.readVarInt();
|
|
483
|
+
const required_request_id_delta = r.readVarInt();
|
|
484
|
+
const track_namespace = r.readTuple();
|
|
485
|
+
const track_name = r.readString();
|
|
486
|
+
const track_alias = r.readVarInt();
|
|
487
|
+
const parameters = decodeParams(r);
|
|
488
|
+
const track_properties = decodeTrackProperties(r, payloadEnd);
|
|
489
|
+
return {
|
|
490
|
+
type: "publish",
|
|
491
|
+
request_id,
|
|
492
|
+
required_request_id_delta,
|
|
493
|
+
track_namespace,
|
|
494
|
+
track_name,
|
|
495
|
+
track_alias,
|
|
496
|
+
parameters,
|
|
497
|
+
track_properties
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
function decodePublishOkPayload(r) {
|
|
501
|
+
const parameters = decodeParams(r);
|
|
502
|
+
return { type: "publish_ok", parameters };
|
|
503
|
+
}
|
|
504
|
+
function decodePublishDonePayload(r) {
|
|
505
|
+
const status_code = r.readVarInt();
|
|
506
|
+
const stream_count = r.readVarInt();
|
|
507
|
+
const reason_phrase = r.readString();
|
|
508
|
+
return { type: "publish_done", status_code, stream_count, reason_phrase };
|
|
509
|
+
}
|
|
510
|
+
function decodePublishNamespacePayload(r) {
|
|
511
|
+
const request_id = r.readVarInt();
|
|
512
|
+
const required_request_id_delta = r.readVarInt();
|
|
513
|
+
const track_namespace = r.readTuple();
|
|
514
|
+
const parameters = decodeParams(r);
|
|
515
|
+
return {
|
|
516
|
+
type: "publish_namespace",
|
|
517
|
+
request_id,
|
|
518
|
+
required_request_id_delta,
|
|
519
|
+
track_namespace,
|
|
520
|
+
parameters
|
|
521
|
+
};
|
|
522
|
+
}
|
|
523
|
+
function decodeNamespacePayload(r) {
|
|
524
|
+
const namespace_suffix = r.readTuple();
|
|
525
|
+
return { type: "namespace", namespace_suffix };
|
|
526
|
+
}
|
|
527
|
+
function decodeNamespaceDonePayload(r) {
|
|
528
|
+
const namespace_suffix = r.readTuple();
|
|
529
|
+
return { type: "namespace_done", namespace_suffix };
|
|
530
|
+
}
|
|
531
|
+
function decodeSubscribeNamespacePayload(r) {
|
|
532
|
+
const request_id = r.readVarInt();
|
|
533
|
+
const required_request_id_delta = r.readVarInt();
|
|
534
|
+
const namespace_prefix = r.readTuple();
|
|
535
|
+
const subscribe_options = r.readVarInt();
|
|
536
|
+
const parameters = decodeParams(r);
|
|
537
|
+
return {
|
|
538
|
+
type: "subscribe_namespace",
|
|
539
|
+
request_id,
|
|
540
|
+
required_request_id_delta,
|
|
541
|
+
namespace_prefix,
|
|
542
|
+
subscribe_options,
|
|
543
|
+
parameters
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
function decodePublishBlockedPayload(r) {
|
|
547
|
+
const namespace_suffix = r.readTuple();
|
|
548
|
+
const track_name = r.readString();
|
|
549
|
+
return { type: "publish_blocked", namespace_suffix, track_name };
|
|
550
|
+
}
|
|
551
|
+
function decodeFetchPayload(r) {
|
|
552
|
+
const request_id = r.readVarInt();
|
|
553
|
+
const required_request_id_delta = r.readVarInt();
|
|
554
|
+
const fetch_type = r.readVarInt();
|
|
555
|
+
const ft = Number(fetch_type);
|
|
556
|
+
if (ft < 1 || ft > 3) {
|
|
557
|
+
throw new DecodeError("CONSTRAINT_VIOLATION", `Invalid fetch_type: ${ft}`, r.offset);
|
|
558
|
+
}
|
|
559
|
+
let standalone;
|
|
560
|
+
let joining;
|
|
561
|
+
if (ft === 1) {
|
|
562
|
+
const track_namespace = r.readTuple();
|
|
563
|
+
const track_name = r.readString();
|
|
564
|
+
const start_group = r.readVarInt();
|
|
565
|
+
const start_object = r.readVarInt();
|
|
566
|
+
const end_group = r.readVarInt();
|
|
567
|
+
const end_object = r.readVarInt();
|
|
568
|
+
standalone = { track_namespace, track_name, start_group, start_object, end_group, end_object };
|
|
569
|
+
} else {
|
|
570
|
+
const joining_request_id = r.readVarInt();
|
|
571
|
+
const joining_start = r.readVarInt();
|
|
572
|
+
joining = { joining_request_id, joining_start };
|
|
573
|
+
}
|
|
574
|
+
const parameters = decodeParams(r);
|
|
575
|
+
return {
|
|
576
|
+
type: "fetch",
|
|
577
|
+
request_id,
|
|
578
|
+
required_request_id_delta,
|
|
579
|
+
fetch_type,
|
|
580
|
+
standalone,
|
|
581
|
+
joining,
|
|
582
|
+
parameters
|
|
583
|
+
};
|
|
584
|
+
}
|
|
585
|
+
function decodeFetchOkPayload(r, payloadEnd) {
|
|
586
|
+
const end_of_track = r.readUint8();
|
|
587
|
+
const end_group = r.readVarInt();
|
|
588
|
+
const end_object = r.readVarInt();
|
|
589
|
+
const parameters = decodeParams(r);
|
|
590
|
+
const track_properties = decodeTrackProperties(r, payloadEnd);
|
|
591
|
+
return { type: "fetch_ok", end_of_track, end_group, end_object, parameters, track_properties };
|
|
592
|
+
}
|
|
593
|
+
function decodeTrackStatusPayload(r) {
|
|
594
|
+
const request_id = r.readVarInt();
|
|
595
|
+
const required_request_id_delta = r.readVarInt();
|
|
596
|
+
const track_namespace = r.readTuple();
|
|
597
|
+
const track_name = r.readString();
|
|
598
|
+
const parameters = decodeParams(r);
|
|
599
|
+
return {
|
|
600
|
+
type: "track_status",
|
|
601
|
+
request_id,
|
|
602
|
+
required_request_id_delta,
|
|
603
|
+
track_namespace,
|
|
604
|
+
track_name,
|
|
605
|
+
parameters
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
function decodeRequestOkPayload(r) {
|
|
609
|
+
const parameters = decodeParams(r);
|
|
610
|
+
return { type: "request_ok", parameters };
|
|
611
|
+
}
|
|
612
|
+
function decodeRequestErrorPayload(r) {
|
|
613
|
+
const error_code = r.readVarInt();
|
|
614
|
+
const retry_interval = r.readVarInt();
|
|
615
|
+
const reason_phrase = r.readString();
|
|
616
|
+
return { type: "request_error", error_code, retry_interval, reason_phrase };
|
|
617
|
+
}
|
|
618
|
+
function decodeGoAwayPayload(r) {
|
|
619
|
+
const new_session_uri = r.readString();
|
|
620
|
+
const timeout = r.readVarInt();
|
|
621
|
+
return { type: "goaway", new_session_uri, timeout };
|
|
622
|
+
}
|
|
623
|
+
function encodeMessage(message) {
|
|
624
|
+
const typeId = MESSAGE_ID_MAP.get(message.type);
|
|
625
|
+
if (typeId === void 0) {
|
|
626
|
+
throw new Error(`Unknown message type: ${message.type}`);
|
|
627
|
+
}
|
|
628
|
+
const payloadWriter = new BufferWriter();
|
|
629
|
+
encodePayload(message, payloadWriter);
|
|
630
|
+
const payload = payloadWriter.finish();
|
|
631
|
+
if (payload.byteLength > 65535) {
|
|
632
|
+
throw new Error(`Payload too large for 16-bit length: ${payload.byteLength}`);
|
|
633
|
+
}
|
|
634
|
+
const writer = new BufferWriter();
|
|
635
|
+
writer.writeVarInt(typeId);
|
|
636
|
+
writer.writeUint8(payload.byteLength >> 8 & 255);
|
|
637
|
+
writer.writeUint8(payload.byteLength & 255);
|
|
638
|
+
writer.writeBytes(payload);
|
|
639
|
+
return writer.finish();
|
|
640
|
+
}
|
|
641
|
+
function encodePayload(msg, w) {
|
|
642
|
+
switch (msg.type) {
|
|
643
|
+
case "setup":
|
|
644
|
+
return encodeSetupPayload(msg, w);
|
|
645
|
+
case "subscribe":
|
|
646
|
+
return encodeSubscribePayload(msg, w);
|
|
647
|
+
case "subscribe_ok":
|
|
648
|
+
return encodeSubscribeOkPayload(msg, w);
|
|
649
|
+
case "request_update":
|
|
650
|
+
return encodeRequestUpdatePayload(msg, w);
|
|
651
|
+
case "publish":
|
|
652
|
+
return encodePublishPayload(msg, w);
|
|
653
|
+
case "publish_ok":
|
|
654
|
+
return encodePublishOkPayload(msg, w);
|
|
655
|
+
case "publish_done":
|
|
656
|
+
return encodePublishDonePayload(msg, w);
|
|
657
|
+
case "publish_namespace":
|
|
658
|
+
return encodePublishNamespacePayload(msg, w);
|
|
659
|
+
case "namespace":
|
|
660
|
+
return encodeNamespacePayload(msg, w);
|
|
661
|
+
case "namespace_done":
|
|
662
|
+
return encodeNamespaceDonePayload(msg, w);
|
|
663
|
+
case "subscribe_namespace":
|
|
664
|
+
return encodeSubscribeNamespacePayload(msg, w);
|
|
665
|
+
case "publish_blocked":
|
|
666
|
+
return encodePublishBlockedPayload(msg, w);
|
|
667
|
+
case "fetch":
|
|
668
|
+
return encodeFetchPayload(msg, w);
|
|
669
|
+
case "fetch_ok":
|
|
670
|
+
return encodeFetchOkPayload(msg, w);
|
|
671
|
+
case "track_status":
|
|
672
|
+
return encodeTrackStatusPayload(msg, w);
|
|
673
|
+
case "request_ok":
|
|
674
|
+
return encodeRequestOkPayload(msg, w);
|
|
675
|
+
case "request_error":
|
|
676
|
+
return encodeRequestErrorPayload(msg, w);
|
|
677
|
+
case "goaway":
|
|
678
|
+
return encodeGoAwayPayload(msg, w);
|
|
679
|
+
default: {
|
|
680
|
+
const _exhaustive = msg;
|
|
681
|
+
throw new Error(`Unhandled message type: ${_exhaustive.type}`);
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
function decodeMessage(bytes) {
|
|
686
|
+
try {
|
|
687
|
+
const reader = new BufferReader(bytes);
|
|
688
|
+
const typeId = reader.readVarInt();
|
|
689
|
+
const lenHi = reader.readUint8();
|
|
690
|
+
const lenLo = reader.readUint8();
|
|
691
|
+
const payloadLength = lenHi << 8 | lenLo;
|
|
692
|
+
const payloadStart = reader.offset;
|
|
693
|
+
const _payloadEnd = payloadStart + payloadLength;
|
|
694
|
+
const payloadBytes = reader.readBytes(payloadLength);
|
|
695
|
+
const payloadReader = new BufferReader(payloadBytes);
|
|
696
|
+
let message;
|
|
697
|
+
if (typeId === MSG_SETUP) {
|
|
698
|
+
message = decodeSetupPayload(payloadReader, payloadLength);
|
|
699
|
+
} else if (typeId === MSG_SUBSCRIBE) {
|
|
700
|
+
message = decodeSubscribePayload(payloadReader);
|
|
701
|
+
} else if (typeId === MSG_SUBSCRIBE_OK) {
|
|
702
|
+
message = decodeSubscribeOkPayload(payloadReader, payloadLength);
|
|
703
|
+
} else if (typeId === MSG_REQUEST_UPDATE) {
|
|
704
|
+
message = decodeRequestUpdatePayload(payloadReader);
|
|
705
|
+
} else if (typeId === MSG_PUBLISH) {
|
|
706
|
+
message = decodePublishPayload(payloadReader, payloadLength);
|
|
707
|
+
} else if (typeId === MSG_PUBLISH_OK) {
|
|
708
|
+
message = decodePublishOkPayload(payloadReader);
|
|
709
|
+
} else if (typeId === MSG_PUBLISH_DONE) {
|
|
710
|
+
message = decodePublishDonePayload(payloadReader);
|
|
711
|
+
} else if (typeId === MSG_PUBLISH_NAMESPACE) {
|
|
712
|
+
message = decodePublishNamespacePayload(payloadReader);
|
|
713
|
+
} else if (typeId === MSG_NAMESPACE) {
|
|
714
|
+
message = decodeNamespacePayload(payloadReader);
|
|
715
|
+
} else if (typeId === MSG_NAMESPACE_DONE) {
|
|
716
|
+
message = decodeNamespaceDonePayload(payloadReader);
|
|
717
|
+
} else if (typeId === MSG_SUBSCRIBE_NAMESPACE) {
|
|
718
|
+
message = decodeSubscribeNamespacePayload(payloadReader);
|
|
719
|
+
} else if (typeId === MSG_PUBLISH_BLOCKED) {
|
|
720
|
+
message = decodePublishBlockedPayload(payloadReader);
|
|
721
|
+
} else if (typeId === MSG_FETCH) {
|
|
722
|
+
message = decodeFetchPayload(payloadReader);
|
|
723
|
+
} else if (typeId === MSG_FETCH_OK) {
|
|
724
|
+
message = decodeFetchOkPayload(payloadReader, payloadLength);
|
|
725
|
+
} else if (typeId === MSG_TRACK_STATUS) {
|
|
726
|
+
message = decodeTrackStatusPayload(payloadReader);
|
|
727
|
+
} else if (typeId === MSG_REQUEST_OK) {
|
|
728
|
+
message = decodeRequestOkPayload(payloadReader);
|
|
729
|
+
} else if (typeId === MSG_REQUEST_ERROR) {
|
|
730
|
+
message = decodeRequestErrorPayload(payloadReader);
|
|
731
|
+
} else if (typeId === MSG_GOAWAY) {
|
|
732
|
+
message = decodeGoAwayPayload(payloadReader);
|
|
733
|
+
} else {
|
|
734
|
+
return {
|
|
735
|
+
ok: false,
|
|
736
|
+
error: new DecodeError(
|
|
737
|
+
"UNKNOWN_MESSAGE_TYPE",
|
|
738
|
+
`Unknown message type ID: 0x${typeId.toString(16)}`,
|
|
739
|
+
0
|
|
740
|
+
)
|
|
741
|
+
};
|
|
742
|
+
}
|
|
743
|
+
return { ok: true, value: message, bytesRead: reader.offset };
|
|
744
|
+
} catch (e) {
|
|
745
|
+
if (e instanceof DecodeError) {
|
|
746
|
+
return { ok: false, error: e };
|
|
747
|
+
}
|
|
748
|
+
throw e;
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
var FETCH_STREAM_TYPE = 0x05n;
|
|
752
|
+
function encodeSubgroupStream(stream) {
|
|
753
|
+
const w = new BufferWriter();
|
|
754
|
+
const streamType = stream.headerType;
|
|
755
|
+
w.writeVarInt(BigInt(streamType));
|
|
756
|
+
const hasSubgroupField = (streamType & 4) !== 0;
|
|
757
|
+
const hasPriority = streamType < 48;
|
|
758
|
+
w.writeVarInt(stream.trackAlias);
|
|
759
|
+
w.writeVarInt(stream.groupId);
|
|
760
|
+
if (hasSubgroupField) {
|
|
761
|
+
w.writeVarInt(stream.subgroupId);
|
|
762
|
+
}
|
|
763
|
+
if (hasPriority) {
|
|
764
|
+
w.writeUint8(stream.publisherPriority);
|
|
765
|
+
}
|
|
766
|
+
let prevObjectId = -1n;
|
|
767
|
+
for (const obj of stream.objects) {
|
|
768
|
+
const delta = prevObjectId < 0n ? obj.objectId : obj.objectId - prevObjectId - 1n;
|
|
769
|
+
w.writeVarInt(delta);
|
|
770
|
+
w.writeVarInt(BigInt(obj.payloadLength));
|
|
771
|
+
if (obj.payloadLength === 0) {
|
|
772
|
+
w.writeVarInt(obj.status ?? 0n);
|
|
773
|
+
} else {
|
|
774
|
+
w.writeBytes(obj.payload);
|
|
775
|
+
}
|
|
776
|
+
prevObjectId = obj.objectId;
|
|
777
|
+
}
|
|
778
|
+
return w.finish();
|
|
779
|
+
}
|
|
780
|
+
function encodeDatagram(dg) {
|
|
781
|
+
const w = new BufferWriter();
|
|
782
|
+
const dgType = dg.datagramType;
|
|
783
|
+
w.writeVarInt(BigInt(dgType));
|
|
784
|
+
w.writeVarInt(dg.trackAlias);
|
|
785
|
+
w.writeVarInt(dg.groupId);
|
|
786
|
+
const objectIdAbsent = (dgType & 4) !== 0;
|
|
787
|
+
const isStatus = (dgType & 32) !== 0;
|
|
788
|
+
const defaultPriority = (dgType & 8) !== 0;
|
|
789
|
+
if (!objectIdAbsent) {
|
|
790
|
+
w.writeVarInt(dg.objectId);
|
|
791
|
+
}
|
|
792
|
+
if (!defaultPriority) {
|
|
793
|
+
w.writeUint8(dg.publisherPriority);
|
|
794
|
+
}
|
|
795
|
+
if (isStatus) {
|
|
796
|
+
w.writeVarInt(dg.objectStatus ?? 0n);
|
|
797
|
+
} else {
|
|
798
|
+
w.writeBytes(dg.payload);
|
|
799
|
+
}
|
|
800
|
+
return w.finish();
|
|
801
|
+
}
|
|
802
|
+
function encodeFetchStream(stream) {
|
|
803
|
+
const w = new BufferWriter();
|
|
804
|
+
w.writeVarInt(FETCH_STREAM_TYPE);
|
|
805
|
+
w.writeVarInt(stream.requestId);
|
|
806
|
+
for (const obj of stream.objects) {
|
|
807
|
+
w.writeUint8(obj.serializationFlags);
|
|
808
|
+
const flags = obj.serializationFlags;
|
|
809
|
+
if (flags & 8) w.writeVarInt(obj.groupId);
|
|
810
|
+
const subgroupEncoding = flags & 3;
|
|
811
|
+
if (subgroupEncoding === 3) w.writeVarInt(obj.subgroupId);
|
|
812
|
+
if (flags & 4) w.writeVarInt(obj.objectId);
|
|
813
|
+
if (flags & 16) w.writeUint8(obj.publisherPriority);
|
|
814
|
+
w.writeVarInt(BigInt(obj.payloadLength));
|
|
815
|
+
if (obj.payloadLength === 0) {
|
|
816
|
+
w.writeVarInt(obj.status ?? 0n);
|
|
817
|
+
} else {
|
|
818
|
+
w.writeBytes(obj.payload);
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
return w.finish();
|
|
822
|
+
}
|
|
823
|
+
function decodeSubgroupStream(bytes) {
|
|
824
|
+
try {
|
|
825
|
+
const r = new BufferReader(bytes);
|
|
826
|
+
const streamType = Number(r.readVarInt());
|
|
827
|
+
if (!(streamType >= 16 && streamType <= 29 || streamType >= 48 && streamType <= 61)) {
|
|
828
|
+
return {
|
|
829
|
+
ok: false,
|
|
830
|
+
error: new DecodeError(
|
|
831
|
+
"CONSTRAINT_VIOLATION",
|
|
832
|
+
`Expected subgroup stream type 0x10-0x1D or 0x30-0x3D, got 0x${streamType.toString(16)}`,
|
|
833
|
+
0
|
|
834
|
+
)
|
|
835
|
+
};
|
|
836
|
+
}
|
|
837
|
+
const hasSubgroupField = (streamType & 4) !== 0;
|
|
838
|
+
const subgroupIsFirstObjId = (streamType & 2) !== 0 && !hasSubgroupField;
|
|
839
|
+
const hasPriority = streamType < 48;
|
|
840
|
+
const trackAlias = r.readVarInt();
|
|
841
|
+
const groupId = r.readVarInt();
|
|
842
|
+
let subgroupId = 0n;
|
|
843
|
+
if (hasSubgroupField) {
|
|
844
|
+
subgroupId = r.readVarInt();
|
|
845
|
+
}
|
|
846
|
+
let publisherPriority = 128;
|
|
847
|
+
if (hasPriority) {
|
|
848
|
+
publisherPriority = r.readUint8();
|
|
849
|
+
}
|
|
850
|
+
const objects = [];
|
|
851
|
+
let prevObjectId = -1n;
|
|
852
|
+
let firstObject = true;
|
|
853
|
+
while (r.remaining > 0) {
|
|
854
|
+
const delta = r.readVarInt();
|
|
855
|
+
let objectId;
|
|
856
|
+
if (firstObject) {
|
|
857
|
+
objectId = delta;
|
|
858
|
+
if (subgroupIsFirstObjId && firstObject) {
|
|
859
|
+
subgroupId = objectId;
|
|
860
|
+
}
|
|
861
|
+
firstObject = false;
|
|
862
|
+
} else {
|
|
863
|
+
objectId = prevObjectId + 1n + delta;
|
|
864
|
+
}
|
|
865
|
+
const payloadLength = Number(r.readVarInt());
|
|
866
|
+
let payload;
|
|
867
|
+
let status;
|
|
868
|
+
if (payloadLength === 0) {
|
|
869
|
+
status = r.readVarInt();
|
|
870
|
+
payload = new Uint8Array(0);
|
|
871
|
+
} else {
|
|
872
|
+
payload = r.readBytes(payloadLength);
|
|
873
|
+
}
|
|
874
|
+
const obj = { type: "object", objectId, payloadLength, payload };
|
|
875
|
+
if (status !== void 0) obj.status = status;
|
|
876
|
+
objects.push(obj);
|
|
877
|
+
prevObjectId = objectId;
|
|
878
|
+
}
|
|
879
|
+
return {
|
|
880
|
+
ok: true,
|
|
881
|
+
value: {
|
|
882
|
+
type: "subgroup",
|
|
883
|
+
headerType: streamType,
|
|
884
|
+
trackAlias,
|
|
885
|
+
groupId,
|
|
886
|
+
subgroupId,
|
|
887
|
+
publisherPriority,
|
|
888
|
+
objects
|
|
889
|
+
},
|
|
890
|
+
bytesRead: r.offset
|
|
891
|
+
};
|
|
892
|
+
} catch (e) {
|
|
893
|
+
if (e instanceof DecodeError) return { ok: false, error: e };
|
|
894
|
+
throw e;
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
function decodeDatagram(bytes) {
|
|
898
|
+
try {
|
|
899
|
+
const r = new BufferReader(bytes);
|
|
900
|
+
const dgType = Number(r.readVarInt());
|
|
901
|
+
const objectIdAbsent = (dgType & 4) !== 0;
|
|
902
|
+
const endOfGroup = (dgType & 2) !== 0;
|
|
903
|
+
const isStatus = (dgType & 32) !== 0;
|
|
904
|
+
const defaultPriority = (dgType & 8) !== 0;
|
|
905
|
+
const trackAlias = r.readVarInt();
|
|
906
|
+
const groupId = r.readVarInt();
|
|
907
|
+
let objectId = 0n;
|
|
908
|
+
if (!objectIdAbsent) {
|
|
909
|
+
objectId = r.readVarInt();
|
|
910
|
+
}
|
|
911
|
+
let publisherPriority = 128;
|
|
912
|
+
if (!defaultPriority) {
|
|
913
|
+
publisherPriority = r.readUint8();
|
|
914
|
+
}
|
|
915
|
+
let objectStatus;
|
|
916
|
+
let payload;
|
|
917
|
+
if (isStatus) {
|
|
918
|
+
objectStatus = r.readVarInt();
|
|
919
|
+
payload = new Uint8Array(0);
|
|
920
|
+
} else {
|
|
921
|
+
payload = r.readBytes(r.remaining);
|
|
922
|
+
}
|
|
923
|
+
const payloadLength = payload.byteLength;
|
|
924
|
+
const result = {
|
|
925
|
+
type: "datagram",
|
|
926
|
+
datagramType: dgType,
|
|
927
|
+
trackAlias,
|
|
928
|
+
groupId,
|
|
929
|
+
objectId,
|
|
930
|
+
publisherPriority,
|
|
931
|
+
payloadLength,
|
|
932
|
+
payload
|
|
933
|
+
};
|
|
934
|
+
if (endOfGroup) result.endOfGroup = true;
|
|
935
|
+
if (objectStatus !== void 0)
|
|
936
|
+
result.objectStatus = objectStatus;
|
|
937
|
+
return { ok: true, value: result, bytesRead: r.offset };
|
|
938
|
+
} catch (e) {
|
|
939
|
+
if (e instanceof DecodeError) return { ok: false, error: e };
|
|
940
|
+
throw e;
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
function decodeFetchStream(bytes) {
|
|
944
|
+
try {
|
|
945
|
+
const r = new BufferReader(bytes);
|
|
946
|
+
const streamType = r.readVarInt();
|
|
947
|
+
if (streamType !== FETCH_STREAM_TYPE) {
|
|
948
|
+
return {
|
|
949
|
+
ok: false,
|
|
950
|
+
error: new DecodeError(
|
|
951
|
+
"CONSTRAINT_VIOLATION",
|
|
952
|
+
`Expected fetch stream type 0x05, got 0x${streamType.toString(16)}`,
|
|
953
|
+
0
|
|
954
|
+
)
|
|
955
|
+
};
|
|
956
|
+
}
|
|
957
|
+
const requestId = r.readVarInt();
|
|
958
|
+
const objects = [];
|
|
959
|
+
let prevGroupId = 0n;
|
|
960
|
+
let prevSubgroupId = 0n;
|
|
961
|
+
let prevObjectId = 0n;
|
|
962
|
+
let prevPriority = 128;
|
|
963
|
+
let first = true;
|
|
964
|
+
while (r.remaining > 0) {
|
|
965
|
+
const flags = r.readUint8();
|
|
966
|
+
const subgroupEncoding = flags & 3;
|
|
967
|
+
const objectIdPresent = (flags & 4) !== 0;
|
|
968
|
+
const groupIdPresent = (flags & 8) !== 0;
|
|
969
|
+
const priorityPresent = (flags & 16) !== 0;
|
|
970
|
+
const extensionsPresent = (flags & 32) !== 0;
|
|
971
|
+
if (flags & 192) {
|
|
972
|
+
return {
|
|
973
|
+
ok: false,
|
|
974
|
+
error: new DecodeError(
|
|
975
|
+
"CONSTRAINT_VIOLATION",
|
|
976
|
+
"Reserved bits set in fetch object flags",
|
|
977
|
+
r.offset
|
|
978
|
+
)
|
|
979
|
+
};
|
|
980
|
+
}
|
|
981
|
+
let groupId = prevGroupId;
|
|
982
|
+
if (groupIdPresent) {
|
|
983
|
+
groupId = r.readVarInt();
|
|
984
|
+
} else if (first) {
|
|
985
|
+
return {
|
|
986
|
+
ok: false,
|
|
987
|
+
error: new DecodeError(
|
|
988
|
+
"CONSTRAINT_VIOLATION",
|
|
989
|
+
"First fetch object must include groupId",
|
|
990
|
+
r.offset
|
|
991
|
+
)
|
|
992
|
+
};
|
|
993
|
+
}
|
|
994
|
+
let subgroupId = prevSubgroupId;
|
|
995
|
+
if (subgroupEncoding === 0) {
|
|
996
|
+
subgroupId = 0n;
|
|
997
|
+
} else if (subgroupEncoding === 1) {
|
|
998
|
+
subgroupId = prevSubgroupId;
|
|
999
|
+
} else if (subgroupEncoding === 2) {
|
|
1000
|
+
subgroupId = prevSubgroupId + 1n;
|
|
1001
|
+
} else if (subgroupEncoding === 3) {
|
|
1002
|
+
subgroupId = r.readVarInt();
|
|
1003
|
+
}
|
|
1004
|
+
let objectId = prevObjectId + 1n;
|
|
1005
|
+
if (objectIdPresent) {
|
|
1006
|
+
objectId = r.readVarInt();
|
|
1007
|
+
} else if (first) {
|
|
1008
|
+
return {
|
|
1009
|
+
ok: false,
|
|
1010
|
+
error: new DecodeError(
|
|
1011
|
+
"CONSTRAINT_VIOLATION",
|
|
1012
|
+
"First fetch object must include objectId",
|
|
1013
|
+
r.offset
|
|
1014
|
+
)
|
|
1015
|
+
};
|
|
1016
|
+
}
|
|
1017
|
+
if (priorityPresent) {
|
|
1018
|
+
prevPriority = r.readUint8();
|
|
1019
|
+
}
|
|
1020
|
+
if (extensionsPresent) {
|
|
1021
|
+
const extLen = Number(r.readVarInt());
|
|
1022
|
+
if (extLen > 0) {
|
|
1023
|
+
r.readBytes(extLen);
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
const payloadLength = Number(r.readVarInt());
|
|
1027
|
+
let payload;
|
|
1028
|
+
let status;
|
|
1029
|
+
if (payloadLength > 0) {
|
|
1030
|
+
payload = r.readBytes(payloadLength);
|
|
1031
|
+
} else {
|
|
1032
|
+
status = r.readVarInt();
|
|
1033
|
+
payload = new Uint8Array(0);
|
|
1034
|
+
}
|
|
1035
|
+
const obj = {
|
|
1036
|
+
type: "object",
|
|
1037
|
+
serializationFlags: flags,
|
|
1038
|
+
groupId,
|
|
1039
|
+
subgroupId,
|
|
1040
|
+
objectId,
|
|
1041
|
+
publisherPriority: prevPriority,
|
|
1042
|
+
payloadLength,
|
|
1043
|
+
payload
|
|
1044
|
+
};
|
|
1045
|
+
if (status !== void 0) obj.status = status;
|
|
1046
|
+
objects.push(obj);
|
|
1047
|
+
prevGroupId = groupId;
|
|
1048
|
+
prevSubgroupId = subgroupId;
|
|
1049
|
+
prevObjectId = objectId;
|
|
1050
|
+
first = false;
|
|
1051
|
+
}
|
|
1052
|
+
return {
|
|
1053
|
+
ok: true,
|
|
1054
|
+
value: { type: "fetch", requestId, objects },
|
|
1055
|
+
bytesRead: r.offset
|
|
1056
|
+
};
|
|
1057
|
+
} catch (e) {
|
|
1058
|
+
if (e instanceof DecodeError) return { ok: false, error: e };
|
|
1059
|
+
throw e;
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
function decodeDataStream(streamType, bytes) {
|
|
1063
|
+
switch (streamType) {
|
|
1064
|
+
case "subgroup":
|
|
1065
|
+
return decodeSubgroupStream(bytes);
|
|
1066
|
+
case "datagram":
|
|
1067
|
+
return decodeDatagram(bytes);
|
|
1068
|
+
case "fetch":
|
|
1069
|
+
return decodeFetchStream(bytes);
|
|
1070
|
+
default: {
|
|
1071
|
+
const _exhaustive = streamType;
|
|
1072
|
+
throw new Error(`Unknown stream type: ${_exhaustive}`);
|
|
1073
|
+
}
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
function createStreamDecoder() {
|
|
1077
|
+
let buffer = new Uint8Array(0);
|
|
1078
|
+
return new TransformStream({
|
|
1079
|
+
transform(chunk, controller) {
|
|
1080
|
+
const newBuffer = new Uint8Array(buffer.length + chunk.length);
|
|
1081
|
+
newBuffer.set(buffer, 0);
|
|
1082
|
+
newBuffer.set(chunk, buffer.length);
|
|
1083
|
+
buffer = newBuffer;
|
|
1084
|
+
while (buffer.length > 0) {
|
|
1085
|
+
const result = decodeMessage(buffer);
|
|
1086
|
+
if (!result.ok) {
|
|
1087
|
+
if (result.error.code === "UNEXPECTED_END") {
|
|
1088
|
+
break;
|
|
1089
|
+
}
|
|
1090
|
+
controller.error(result.error);
|
|
1091
|
+
return;
|
|
1092
|
+
}
|
|
1093
|
+
controller.enqueue(result.value);
|
|
1094
|
+
buffer = buffer.slice(result.bytesRead);
|
|
1095
|
+
}
|
|
1096
|
+
},
|
|
1097
|
+
flush(controller) {
|
|
1098
|
+
if (buffer.length > 0) {
|
|
1099
|
+
controller.error(
|
|
1100
|
+
new DecodeError("UNEXPECTED_END", "Stream ended with incomplete message data", 0)
|
|
1101
|
+
);
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
});
|
|
1105
|
+
}
|
|
1106
|
+
function createSubgroupStreamDecoder() {
|
|
1107
|
+
let buffer = new Uint8Array(0);
|
|
1108
|
+
let headerEmitted = false;
|
|
1109
|
+
let prevObjectId = -1n;
|
|
1110
|
+
let firstObject = true;
|
|
1111
|
+
let _subgroupIsFirstObjId = false;
|
|
1112
|
+
return new TransformStream({
|
|
1113
|
+
transform(chunk, controller) {
|
|
1114
|
+
const newBuffer = new Uint8Array(buffer.length + chunk.length);
|
|
1115
|
+
newBuffer.set(buffer, 0);
|
|
1116
|
+
newBuffer.set(chunk, buffer.length);
|
|
1117
|
+
buffer = newBuffer;
|
|
1118
|
+
if (!headerEmitted) {
|
|
1119
|
+
try {
|
|
1120
|
+
const r = new BufferReader(buffer);
|
|
1121
|
+
const streamType = Number(r.readVarInt());
|
|
1122
|
+
if (!(streamType >= 16 && streamType <= 29 || streamType >= 48 && streamType <= 61)) {
|
|
1123
|
+
controller.error(
|
|
1124
|
+
new DecodeError(
|
|
1125
|
+
"CONSTRAINT_VIOLATION",
|
|
1126
|
+
`Expected subgroup stream type, got 0x${streamType.toString(16)}`,
|
|
1127
|
+
0
|
|
1128
|
+
)
|
|
1129
|
+
);
|
|
1130
|
+
return;
|
|
1131
|
+
}
|
|
1132
|
+
const hasSubgroupField = (streamType & 4) !== 0;
|
|
1133
|
+
_subgroupIsFirstObjId = (streamType & 2) !== 0 && !hasSubgroupField;
|
|
1134
|
+
const hasPriority = streamType < 48;
|
|
1135
|
+
const trackAlias = r.readVarInt();
|
|
1136
|
+
const groupId = r.readVarInt();
|
|
1137
|
+
let subgroupId = 0n;
|
|
1138
|
+
if (hasSubgroupField) {
|
|
1139
|
+
subgroupId = r.readVarInt();
|
|
1140
|
+
}
|
|
1141
|
+
let publisherPriority = 128;
|
|
1142
|
+
if (hasPriority) {
|
|
1143
|
+
publisherPriority = r.readUint8();
|
|
1144
|
+
}
|
|
1145
|
+
controller.enqueue({
|
|
1146
|
+
type: "subgroup_header",
|
|
1147
|
+
trackAlias,
|
|
1148
|
+
groupId,
|
|
1149
|
+
subgroupId,
|
|
1150
|
+
publisherPriority
|
|
1151
|
+
});
|
|
1152
|
+
headerEmitted = true;
|
|
1153
|
+
buffer = buffer.slice(r.offset);
|
|
1154
|
+
} catch (e) {
|
|
1155
|
+
if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
|
|
1156
|
+
return;
|
|
1157
|
+
}
|
|
1158
|
+
controller.error(e);
|
|
1159
|
+
return;
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
while (buffer.length > 0) {
|
|
1163
|
+
try {
|
|
1164
|
+
const r = new BufferReader(buffer);
|
|
1165
|
+
const delta = r.readVarInt();
|
|
1166
|
+
let objectId;
|
|
1167
|
+
if (firstObject) {
|
|
1168
|
+
objectId = delta;
|
|
1169
|
+
firstObject = false;
|
|
1170
|
+
} else {
|
|
1171
|
+
objectId = prevObjectId + 1n + delta;
|
|
1172
|
+
}
|
|
1173
|
+
const payloadLength = Number(r.readVarInt());
|
|
1174
|
+
const payload = payloadLength > 0 ? r.readBytes(payloadLength) : new Uint8Array(0);
|
|
1175
|
+
controller.enqueue({ type: "object", objectId, payloadLength, payload });
|
|
1176
|
+
buffer = buffer.slice(r.offset);
|
|
1177
|
+
prevObjectId = objectId;
|
|
1178
|
+
} catch (e) {
|
|
1179
|
+
if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
|
|
1180
|
+
break;
|
|
1181
|
+
}
|
|
1182
|
+
controller.error(e);
|
|
1183
|
+
return;
|
|
1184
|
+
}
|
|
1185
|
+
}
|
|
1186
|
+
},
|
|
1187
|
+
flush(controller) {
|
|
1188
|
+
if (buffer.length > 0) {
|
|
1189
|
+
controller.error(new DecodeError("UNEXPECTED_END", "Stream ended with incomplete data", 0));
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
1192
|
+
});
|
|
1193
|
+
}
|
|
1194
|
+
function createFetchStreamDecoder() {
|
|
1195
|
+
let buffer = new Uint8Array(0);
|
|
1196
|
+
let headerEmitted = false;
|
|
1197
|
+
return new TransformStream({
|
|
1198
|
+
transform(chunk, controller) {
|
|
1199
|
+
const newBuffer = new Uint8Array(buffer.length + chunk.length);
|
|
1200
|
+
newBuffer.set(buffer, 0);
|
|
1201
|
+
newBuffer.set(chunk, buffer.length);
|
|
1202
|
+
buffer = newBuffer;
|
|
1203
|
+
if (!headerEmitted) {
|
|
1204
|
+
try {
|
|
1205
|
+
const r = new BufferReader(buffer);
|
|
1206
|
+
const streamType = r.readVarInt();
|
|
1207
|
+
if (streamType !== FETCH_STREAM_TYPE) {
|
|
1208
|
+
controller.error(
|
|
1209
|
+
new DecodeError(
|
|
1210
|
+
"CONSTRAINT_VIOLATION",
|
|
1211
|
+
`Expected fetch stream type 0x05, got 0x${streamType.toString(16)}`,
|
|
1212
|
+
0
|
|
1213
|
+
)
|
|
1214
|
+
);
|
|
1215
|
+
return;
|
|
1216
|
+
}
|
|
1217
|
+
const requestId = r.readVarInt();
|
|
1218
|
+
controller.enqueue({ type: "fetch_header", requestId });
|
|
1219
|
+
headerEmitted = true;
|
|
1220
|
+
buffer = buffer.slice(r.offset);
|
|
1221
|
+
} catch (e) {
|
|
1222
|
+
if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
|
|
1223
|
+
return;
|
|
1224
|
+
}
|
|
1225
|
+
controller.error(e);
|
|
1226
|
+
return;
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
while (buffer.length > 0) {
|
|
1230
|
+
try {
|
|
1231
|
+
const r = new BufferReader(buffer);
|
|
1232
|
+
const flags = r.readUint8();
|
|
1233
|
+
const objectIdPresent = (flags & 4) !== 0;
|
|
1234
|
+
const groupIdPresent = (flags & 8) !== 0;
|
|
1235
|
+
const priorityPresent = (flags & 16) !== 0;
|
|
1236
|
+
const extensionsPresent = (flags & 32) !== 0;
|
|
1237
|
+
const subgroupEncoding = flags & 3;
|
|
1238
|
+
if (groupIdPresent) r.readVarInt();
|
|
1239
|
+
if (subgroupEncoding === 3) r.readVarInt();
|
|
1240
|
+
let objectId = 0n;
|
|
1241
|
+
if (objectIdPresent) objectId = r.readVarInt();
|
|
1242
|
+
if (priorityPresent) r.readUint8();
|
|
1243
|
+
if (extensionsPresent) {
|
|
1244
|
+
const extLen = Number(r.readVarInt());
|
|
1245
|
+
if (extLen > 0) r.readBytes(extLen);
|
|
1246
|
+
}
|
|
1247
|
+
const payloadLength = Number(r.readVarInt());
|
|
1248
|
+
const payload = payloadLength > 0 ? r.readBytes(payloadLength) : new Uint8Array(0);
|
|
1249
|
+
controller.enqueue({ type: "object", objectId, payloadLength, payload });
|
|
1250
|
+
buffer = buffer.slice(r.offset);
|
|
1251
|
+
} catch (e) {
|
|
1252
|
+
if (e instanceof DecodeError && e.code === "UNEXPECTED_END") {
|
|
1253
|
+
break;
|
|
1254
|
+
}
|
|
1255
|
+
controller.error(e);
|
|
1256
|
+
return;
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1259
|
+
},
|
|
1260
|
+
flush(controller) {
|
|
1261
|
+
if (buffer.length > 0) {
|
|
1262
|
+
controller.error(new DecodeError("UNEXPECTED_END", "Stream ended with incomplete data", 0));
|
|
1263
|
+
}
|
|
1264
|
+
}
|
|
1265
|
+
});
|
|
1266
|
+
}
|
|
1267
|
+
function createDataStreamDecoder() {
|
|
1268
|
+
let buffer = new Uint8Array(0);
|
|
1269
|
+
let inner = null;
|
|
1270
|
+
return new TransformStream({
|
|
1271
|
+
transform(chunk, controller) {
|
|
1272
|
+
const newBuffer = new Uint8Array(buffer.length + chunk.length);
|
|
1273
|
+
newBuffer.set(buffer, 0);
|
|
1274
|
+
newBuffer.set(chunk, buffer.length);
|
|
1275
|
+
buffer = newBuffer;
|
|
1276
|
+
if (inner === null) {
|
|
1277
|
+
if (buffer.length === 0) return;
|
|
1278
|
+
const firstByte = buffer[0];
|
|
1279
|
+
if (firstByte >= 16 && firstByte <= 29 || firstByte >= 48 && firstByte <= 61) {
|
|
1280
|
+
const decoder = createSubgroupStreamDecoder();
|
|
1281
|
+
inner = decoder;
|
|
1282
|
+
} else if (firstByte === 5) {
|
|
1283
|
+
const decoder = createFetchStreamDecoder();
|
|
1284
|
+
inner = decoder;
|
|
1285
|
+
} else {
|
|
1286
|
+
controller.error(
|
|
1287
|
+
new DecodeError(
|
|
1288
|
+
"CONSTRAINT_VIOLATION",
|
|
1289
|
+
`Unknown data stream type: 0x${firstByte.toString(16)}`,
|
|
1290
|
+
0
|
|
1291
|
+
)
|
|
1292
|
+
);
|
|
1293
|
+
return;
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
},
|
|
1297
|
+
flush(controller) {
|
|
1298
|
+
if (buffer.length === 0) return;
|
|
1299
|
+
const firstByte = buffer[0];
|
|
1300
|
+
let result;
|
|
1301
|
+
if (firstByte >= 16 && firstByte <= 29 || firstByte >= 48 && firstByte <= 61) {
|
|
1302
|
+
result = decodeSubgroupStream(buffer);
|
|
1303
|
+
} else if (firstByte === 5) {
|
|
1304
|
+
result = decodeFetchStream(buffer);
|
|
1305
|
+
} else {
|
|
1306
|
+
controller.error(
|
|
1307
|
+
new DecodeError(
|
|
1308
|
+
"CONSTRAINT_VIOLATION",
|
|
1309
|
+
`Unknown data stream type: 0x${firstByte.toString(16)}`,
|
|
1310
|
+
0
|
|
1311
|
+
)
|
|
1312
|
+
);
|
|
1313
|
+
return;
|
|
1314
|
+
}
|
|
1315
|
+
if (!result.ok) {
|
|
1316
|
+
controller.error(result.error);
|
|
1317
|
+
return;
|
|
1318
|
+
}
|
|
1319
|
+
const stream = result.value;
|
|
1320
|
+
if (stream.type === "subgroup") {
|
|
1321
|
+
controller.enqueue({
|
|
1322
|
+
type: "subgroup_header",
|
|
1323
|
+
trackAlias: stream.trackAlias,
|
|
1324
|
+
groupId: stream.groupId,
|
|
1325
|
+
subgroupId: stream.subgroupId,
|
|
1326
|
+
publisherPriority: stream.publisherPriority
|
|
1327
|
+
});
|
|
1328
|
+
for (const obj of stream.objects) {
|
|
1329
|
+
controller.enqueue(obj);
|
|
1330
|
+
}
|
|
1331
|
+
} else if (stream.type === "fetch") {
|
|
1332
|
+
controller.enqueue({
|
|
1333
|
+
type: "fetch_header",
|
|
1334
|
+
requestId: stream.requestId
|
|
1335
|
+
});
|
|
1336
|
+
for (const obj of stream.objects) {
|
|
1337
|
+
controller.enqueue(obj);
|
|
1338
|
+
}
|
|
1339
|
+
}
|
|
1340
|
+
}
|
|
1341
|
+
});
|
|
1342
|
+
}
|
|
1343
|
+
function createDraft17Codec() {
|
|
1344
|
+
return {
|
|
1345
|
+
draft: "draft-ietf-moq-transport-17",
|
|
1346
|
+
encodeMessage,
|
|
1347
|
+
decodeMessage,
|
|
1348
|
+
encodeSubgroupStream,
|
|
1349
|
+
encodeDatagram,
|
|
1350
|
+
encodeFetchStream,
|
|
1351
|
+
decodeSubgroupStream,
|
|
1352
|
+
decodeDatagram,
|
|
1353
|
+
decodeFetchStream,
|
|
1354
|
+
decodeDataStream,
|
|
1355
|
+
createStreamDecoder,
|
|
1356
|
+
createSubgroupStreamDecoder,
|
|
1357
|
+
createFetchStreamDecoder,
|
|
1358
|
+
createDataStreamDecoder
|
|
1359
|
+
};
|
|
1360
|
+
}
|
|
1361
|
+
|
|
1362
|
+
export {
|
|
1363
|
+
MSG_REQUEST_UPDATE,
|
|
1364
|
+
MSG_SUBSCRIBE,
|
|
1365
|
+
MSG_SUBSCRIBE_OK,
|
|
1366
|
+
MSG_REQUEST_ERROR,
|
|
1367
|
+
MSG_PUBLISH_NAMESPACE,
|
|
1368
|
+
MSG_REQUEST_OK,
|
|
1369
|
+
MSG_NAMESPACE,
|
|
1370
|
+
MSG_PUBLISH_DONE,
|
|
1371
|
+
MSG_TRACK_STATUS,
|
|
1372
|
+
MSG_NAMESPACE_DONE,
|
|
1373
|
+
MSG_PUBLISH_BLOCKED,
|
|
1374
|
+
MSG_GOAWAY,
|
|
1375
|
+
MSG_SUBSCRIBE_NAMESPACE,
|
|
1376
|
+
MSG_FETCH,
|
|
1377
|
+
MSG_FETCH_OK,
|
|
1378
|
+
MSG_PUBLISH,
|
|
1379
|
+
MSG_PUBLISH_OK,
|
|
1380
|
+
MSG_SETUP,
|
|
1381
|
+
SETUP_OPT_PATH,
|
|
1382
|
+
SETUP_OPT_MAX_AUTH_TOKEN_CACHE_SIZE,
|
|
1383
|
+
SETUP_OPT_AUTHORITY,
|
|
1384
|
+
SETUP_OPT_MOQT_IMPLEMENTATION,
|
|
1385
|
+
MESSAGE_TYPE_MAP,
|
|
1386
|
+
MESSAGE_ID_MAP,
|
|
1387
|
+
encodeMessage,
|
|
1388
|
+
decodeMessage,
|
|
1389
|
+
encodeSubgroupStream,
|
|
1390
|
+
encodeDatagram,
|
|
1391
|
+
encodeFetchStream,
|
|
1392
|
+
decodeSubgroupStream,
|
|
1393
|
+
decodeDatagram,
|
|
1394
|
+
decodeFetchStream,
|
|
1395
|
+
decodeDataStream,
|
|
1396
|
+
createStreamDecoder,
|
|
1397
|
+
createSubgroupStreamDecoder,
|
|
1398
|
+
createFetchStreamDecoder,
|
|
1399
|
+
createDataStreamDecoder,
|
|
1400
|
+
createDraft17Codec
|
|
1401
|
+
};
|