@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,680 @@
|
|
|
1
|
+
// src/drafts/draft10/rules.ts
|
|
2
|
+
var CONTROL_MESSAGES = /* @__PURE__ */ new Set([
|
|
3
|
+
"client_setup",
|
|
4
|
+
"server_setup",
|
|
5
|
+
"subscribe",
|
|
6
|
+
"subscribe_ok",
|
|
7
|
+
"subscribe_error",
|
|
8
|
+
"subscribe_update",
|
|
9
|
+
"subscribe_done",
|
|
10
|
+
"unsubscribe",
|
|
11
|
+
"announce",
|
|
12
|
+
"announce_ok",
|
|
13
|
+
"announce_error",
|
|
14
|
+
"unannounce",
|
|
15
|
+
"announce_cancel",
|
|
16
|
+
"subscribe_announces",
|
|
17
|
+
"subscribe_announces_ok",
|
|
18
|
+
"subscribe_announces_error",
|
|
19
|
+
"unsubscribe_announces",
|
|
20
|
+
"fetch",
|
|
21
|
+
"fetch_ok",
|
|
22
|
+
"fetch_error",
|
|
23
|
+
"fetch_cancel",
|
|
24
|
+
"track_status_request",
|
|
25
|
+
"track_status",
|
|
26
|
+
"goaway",
|
|
27
|
+
"max_subscribe_id",
|
|
28
|
+
"subscribes_blocked"
|
|
29
|
+
]);
|
|
30
|
+
var CLIENT_ONLY_MESSAGES = /* @__PURE__ */ new Set(["client_setup"]);
|
|
31
|
+
var SERVER_ONLY_MESSAGES = /* @__PURE__ */ new Set(["server_setup"]);
|
|
32
|
+
var BIDIRECTIONAL_MESSAGES = /* @__PURE__ */ new Set([
|
|
33
|
+
"subscribe",
|
|
34
|
+
"subscribe_ok",
|
|
35
|
+
"subscribe_error",
|
|
36
|
+
"subscribe_update",
|
|
37
|
+
"subscribe_done",
|
|
38
|
+
"unsubscribe",
|
|
39
|
+
"announce",
|
|
40
|
+
"announce_ok",
|
|
41
|
+
"announce_error",
|
|
42
|
+
"unannounce",
|
|
43
|
+
"announce_cancel",
|
|
44
|
+
"subscribe_announces",
|
|
45
|
+
"subscribe_announces_ok",
|
|
46
|
+
"subscribe_announces_error",
|
|
47
|
+
"unsubscribe_announces",
|
|
48
|
+
"fetch",
|
|
49
|
+
"fetch_ok",
|
|
50
|
+
"fetch_error",
|
|
51
|
+
"fetch_cancel",
|
|
52
|
+
"track_status_request",
|
|
53
|
+
"track_status",
|
|
54
|
+
"goaway",
|
|
55
|
+
"max_subscribe_id",
|
|
56
|
+
"subscribes_blocked"
|
|
57
|
+
]);
|
|
58
|
+
function getLegalOutgoing(phase, role) {
|
|
59
|
+
const legal = /* @__PURE__ */ new Set();
|
|
60
|
+
switch (phase) {
|
|
61
|
+
case "idle":
|
|
62
|
+
if (role === "client") legal.add("client_setup");
|
|
63
|
+
break;
|
|
64
|
+
case "setup":
|
|
65
|
+
if (role === "server") legal.add("server_setup");
|
|
66
|
+
break;
|
|
67
|
+
case "ready":
|
|
68
|
+
legal.add("goaway");
|
|
69
|
+
for (const msg of BIDIRECTIONAL_MESSAGES) legal.add(msg);
|
|
70
|
+
break;
|
|
71
|
+
case "draining":
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
return legal;
|
|
75
|
+
}
|
|
76
|
+
function getLegalIncoming(phase, role) {
|
|
77
|
+
const remoteRole = role === "client" ? "server" : "client";
|
|
78
|
+
return getLegalOutgoing(phase, remoteRole);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// src/drafts/draft10/session-fsm.ts
|
|
82
|
+
function violation(code, message, currentPhase, offendingMessage) {
|
|
83
|
+
return { code, message, currentPhase, offendingMessage };
|
|
84
|
+
}
|
|
85
|
+
var Draft10SessionFSM = class {
|
|
86
|
+
_phase = "idle";
|
|
87
|
+
_role;
|
|
88
|
+
_subscriptions = /* @__PURE__ */ new Map();
|
|
89
|
+
_announces = /* @__PURE__ */ new Map();
|
|
90
|
+
_fetches = /* @__PURE__ */ new Map();
|
|
91
|
+
_subscribeIds = /* @__PURE__ */ new Set();
|
|
92
|
+
constructor(role) {
|
|
93
|
+
this._role = role;
|
|
94
|
+
}
|
|
95
|
+
get phase() {
|
|
96
|
+
return this._phase;
|
|
97
|
+
}
|
|
98
|
+
get role() {
|
|
99
|
+
return this._role;
|
|
100
|
+
}
|
|
101
|
+
get subscriptions() {
|
|
102
|
+
return this._subscriptions;
|
|
103
|
+
}
|
|
104
|
+
get announces() {
|
|
105
|
+
return this._announces;
|
|
106
|
+
}
|
|
107
|
+
get fetches() {
|
|
108
|
+
return this._fetches;
|
|
109
|
+
}
|
|
110
|
+
get legalOutgoing() {
|
|
111
|
+
return getLegalOutgoing(this._phase, this._role);
|
|
112
|
+
}
|
|
113
|
+
get legalIncoming() {
|
|
114
|
+
return getLegalIncoming(this._phase, this._role);
|
|
115
|
+
}
|
|
116
|
+
checkRole(message, direction) {
|
|
117
|
+
const senderRole = direction === "outbound" ? this._role : this._role === "client" ? "server" : "client";
|
|
118
|
+
if (CLIENT_ONLY_MESSAGES.has(message.type) && senderRole !== "client") {
|
|
119
|
+
return violation(
|
|
120
|
+
"ROLE_VIOLATION",
|
|
121
|
+
`${message.type} can only be sent by client`,
|
|
122
|
+
this._phase,
|
|
123
|
+
message.type
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
if (SERVER_ONLY_MESSAGES.has(message.type) && senderRole !== "server") {
|
|
127
|
+
return violation(
|
|
128
|
+
"ROLE_VIOLATION",
|
|
129
|
+
`${message.type} can only be sent by server`,
|
|
130
|
+
this._phase,
|
|
131
|
+
message.type
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
checkDuplicateSubscribeId(subscribeId, msgType) {
|
|
137
|
+
if (this._subscribeIds.has(subscribeId)) {
|
|
138
|
+
return violation(
|
|
139
|
+
"DUPLICATE_REQUEST_ID",
|
|
140
|
+
`Subscribe ID ${subscribeId} already in use`,
|
|
141
|
+
this._phase,
|
|
142
|
+
msgType
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
checkKnownSubscribeId(subscribeId, msgType) {
|
|
148
|
+
if (!this._subscribeIds.has(subscribeId)) {
|
|
149
|
+
return violation(
|
|
150
|
+
"UNKNOWN_REQUEST_ID",
|
|
151
|
+
`No request with subscribe ID ${subscribeId}`,
|
|
152
|
+
this._phase,
|
|
153
|
+
msgType
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
validateOutgoing(message) {
|
|
159
|
+
const roleViolation = this.checkRole(message, "outbound");
|
|
160
|
+
if (roleViolation) return { ok: false, violation: roleViolation };
|
|
161
|
+
if (!this.legalOutgoing.has(message.type)) {
|
|
162
|
+
return {
|
|
163
|
+
ok: false,
|
|
164
|
+
violation: violation(
|
|
165
|
+
this._phase === "idle" || this._phase === "setup" ? "MESSAGE_BEFORE_SETUP" : "UNEXPECTED_MESSAGE",
|
|
166
|
+
`Cannot send ${message.type} in phase ${this._phase}`,
|
|
167
|
+
this._phase,
|
|
168
|
+
message.type
|
|
169
|
+
)
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
return { ok: true };
|
|
173
|
+
}
|
|
174
|
+
receive(message) {
|
|
175
|
+
const roleViolation = this.checkRole(message, "inbound");
|
|
176
|
+
if (roleViolation) return { ok: false, violation: roleViolation };
|
|
177
|
+
return this.applyTransition(message, "inbound");
|
|
178
|
+
}
|
|
179
|
+
send(message) {
|
|
180
|
+
const roleViolation = this.checkRole(message, "outbound");
|
|
181
|
+
if (roleViolation) return { ok: false, violation: roleViolation };
|
|
182
|
+
return this.applyTransition(message, "outbound");
|
|
183
|
+
}
|
|
184
|
+
applyTransition(message, direction) {
|
|
185
|
+
const sideEffects = [];
|
|
186
|
+
switch (message.type) {
|
|
187
|
+
case "client_setup":
|
|
188
|
+
return this.handleClientSetup(direction);
|
|
189
|
+
case "server_setup":
|
|
190
|
+
return this.handleServerSetup(direction);
|
|
191
|
+
case "goaway":
|
|
192
|
+
return this.handleGoAway(message, sideEffects);
|
|
193
|
+
// Subscribe lifecycle
|
|
194
|
+
case "subscribe":
|
|
195
|
+
return this.handleSubscribe(message, sideEffects);
|
|
196
|
+
case "subscribe_ok":
|
|
197
|
+
return this.handleSubscribeOk(message, sideEffects);
|
|
198
|
+
case "subscribe_error":
|
|
199
|
+
return this.handleSubscribeError(message, sideEffects);
|
|
200
|
+
case "subscribe_update":
|
|
201
|
+
return this.handleSubscribeUpdate(message, sideEffects);
|
|
202
|
+
case "subscribe_done":
|
|
203
|
+
return this.handleSubscribeDone(message, sideEffects);
|
|
204
|
+
case "unsubscribe":
|
|
205
|
+
return this.handleUnsubscribe(message, sideEffects);
|
|
206
|
+
// Announce lifecycle
|
|
207
|
+
case "announce":
|
|
208
|
+
return this.handleAnnounce(message, sideEffects);
|
|
209
|
+
case "announce_ok":
|
|
210
|
+
return this.handleAnnounceOk(message, sideEffects);
|
|
211
|
+
case "announce_error":
|
|
212
|
+
return this.handleAnnounceError(message, sideEffects);
|
|
213
|
+
case "unannounce":
|
|
214
|
+
return this.handleUnannounce(message, sideEffects);
|
|
215
|
+
case "announce_cancel":
|
|
216
|
+
return this.handleAnnounceCancel(message, sideEffects);
|
|
217
|
+
// Fetch lifecycle
|
|
218
|
+
case "fetch":
|
|
219
|
+
return this.handleFetch(message, sideEffects);
|
|
220
|
+
case "fetch_ok":
|
|
221
|
+
return this.handleFetchOk(message, sideEffects);
|
|
222
|
+
case "fetch_error":
|
|
223
|
+
return this.handleFetchError(message, sideEffects);
|
|
224
|
+
case "fetch_cancel":
|
|
225
|
+
return this.handleFetchCancel(message, sideEffects);
|
|
226
|
+
default:
|
|
227
|
+
return this.handleReadyPhaseMessage(message);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
handleClientSetup(direction) {
|
|
231
|
+
if (this._phase !== "idle") {
|
|
232
|
+
return {
|
|
233
|
+
ok: false,
|
|
234
|
+
violation: violation(
|
|
235
|
+
"SETUP_VIOLATION",
|
|
236
|
+
"CLIENT_SETUP already sent/received",
|
|
237
|
+
this._phase,
|
|
238
|
+
"client_setup"
|
|
239
|
+
)
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
if (direction === "outbound" && this._role !== "client") {
|
|
243
|
+
return {
|
|
244
|
+
ok: false,
|
|
245
|
+
violation: violation(
|
|
246
|
+
"ROLE_VIOLATION",
|
|
247
|
+
"Only client can send CLIENT_SETUP",
|
|
248
|
+
this._phase,
|
|
249
|
+
"client_setup"
|
|
250
|
+
)
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
this._phase = "setup";
|
|
254
|
+
return { ok: true, phase: this._phase, sideEffects: [] };
|
|
255
|
+
}
|
|
256
|
+
handleServerSetup(direction) {
|
|
257
|
+
if (this._phase !== "setup") {
|
|
258
|
+
return {
|
|
259
|
+
ok: false,
|
|
260
|
+
violation: violation(
|
|
261
|
+
"SETUP_VIOLATION",
|
|
262
|
+
"SERVER_SETUP before CLIENT_SETUP",
|
|
263
|
+
this._phase,
|
|
264
|
+
"server_setup"
|
|
265
|
+
)
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
if (direction === "outbound" && this._role !== "server") {
|
|
269
|
+
return {
|
|
270
|
+
ok: false,
|
|
271
|
+
violation: violation(
|
|
272
|
+
"ROLE_VIOLATION",
|
|
273
|
+
"Only server can send SERVER_SETUP",
|
|
274
|
+
this._phase,
|
|
275
|
+
"server_setup"
|
|
276
|
+
)
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
this._phase = "ready";
|
|
280
|
+
return { ok: true, phase: this._phase, sideEffects: [{ type: "session-ready" }] };
|
|
281
|
+
}
|
|
282
|
+
handleGoAway(message, sideEffects) {
|
|
283
|
+
if (this._phase !== "ready" && this._phase !== "draining") {
|
|
284
|
+
return {
|
|
285
|
+
ok: false,
|
|
286
|
+
violation: violation(
|
|
287
|
+
"UNEXPECTED_MESSAGE",
|
|
288
|
+
`GOAWAY not valid in phase ${this._phase}`,
|
|
289
|
+
this._phase,
|
|
290
|
+
"goaway"
|
|
291
|
+
)
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
this._phase = "draining";
|
|
295
|
+
const goaway = message;
|
|
296
|
+
sideEffects.push({ type: "session-draining", goAwayUri: goaway.new_session_uri });
|
|
297
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
298
|
+
}
|
|
299
|
+
requireReady(msgType) {
|
|
300
|
+
if (this._phase !== "ready" && this._phase !== "draining") {
|
|
301
|
+
return violation(
|
|
302
|
+
this._phase === "idle" || this._phase === "setup" ? "MESSAGE_BEFORE_SETUP" : "UNEXPECTED_MESSAGE",
|
|
303
|
+
`${msgType} requires ready phase, current: ${this._phase}`,
|
|
304
|
+
this._phase,
|
|
305
|
+
msgType
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
return null;
|
|
309
|
+
}
|
|
310
|
+
// ─── Subscribe lifecycle ───
|
|
311
|
+
handleSubscribe(message, sideEffects) {
|
|
312
|
+
const err = this.requireReady(message.type);
|
|
313
|
+
if (err) return { ok: false, violation: err };
|
|
314
|
+
const sub = message;
|
|
315
|
+
const dupErr = this.checkDuplicateSubscribeId(sub.subscribe_id, message.type);
|
|
316
|
+
if (dupErr) return { ok: false, violation: dupErr };
|
|
317
|
+
this._subscribeIds.add(sub.subscribe_id);
|
|
318
|
+
this._subscriptions.set(sub.subscribe_id, {
|
|
319
|
+
subscribeId: sub.subscribe_id,
|
|
320
|
+
phase: "pending",
|
|
321
|
+
trackNamespace: sub.track_namespace,
|
|
322
|
+
trackName: sub.track_name
|
|
323
|
+
});
|
|
324
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
325
|
+
}
|
|
326
|
+
handleSubscribeOk(message, sideEffects) {
|
|
327
|
+
const err = this.requireReady(message.type);
|
|
328
|
+
if (err) return { ok: false, violation: err };
|
|
329
|
+
const ok = message;
|
|
330
|
+
const idErr = this.checkKnownSubscribeId(ok.subscribe_id, message.type);
|
|
331
|
+
if (idErr) return { ok: false, violation: idErr };
|
|
332
|
+
const existing = this._subscriptions.get(ok.subscribe_id);
|
|
333
|
+
if (!existing)
|
|
334
|
+
return {
|
|
335
|
+
ok: false,
|
|
336
|
+
violation: violation(
|
|
337
|
+
"UNKNOWN_REQUEST_ID",
|
|
338
|
+
`No subscription with subscribe ID ${ok.subscribe_id}`,
|
|
339
|
+
this._phase,
|
|
340
|
+
message.type
|
|
341
|
+
)
|
|
342
|
+
};
|
|
343
|
+
if (existing.phase !== "pending")
|
|
344
|
+
return {
|
|
345
|
+
ok: false,
|
|
346
|
+
violation: violation(
|
|
347
|
+
"STATE_VIOLATION",
|
|
348
|
+
`Subscription ${ok.subscribe_id} is ${existing.phase}, not pending`,
|
|
349
|
+
this._phase,
|
|
350
|
+
message.type
|
|
351
|
+
)
|
|
352
|
+
};
|
|
353
|
+
this._subscriptions.set(ok.subscribe_id, { ...existing, phase: "active" });
|
|
354
|
+
sideEffects.push({ type: "subscription-activated", subscribeId: ok.subscribe_id });
|
|
355
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
356
|
+
}
|
|
357
|
+
handleSubscribeError(message, sideEffects) {
|
|
358
|
+
const err = this.requireReady(message.type);
|
|
359
|
+
if (err) return { ok: false, violation: err };
|
|
360
|
+
const subErr = message;
|
|
361
|
+
const idErr = this.checkKnownSubscribeId(subErr.subscribe_id, message.type);
|
|
362
|
+
if (idErr) return { ok: false, violation: idErr };
|
|
363
|
+
const existing = this._subscriptions.get(subErr.subscribe_id);
|
|
364
|
+
if (!existing)
|
|
365
|
+
return {
|
|
366
|
+
ok: false,
|
|
367
|
+
violation: violation(
|
|
368
|
+
"UNKNOWN_REQUEST_ID",
|
|
369
|
+
`No subscription with subscribe ID ${subErr.subscribe_id}`,
|
|
370
|
+
this._phase,
|
|
371
|
+
message.type
|
|
372
|
+
)
|
|
373
|
+
};
|
|
374
|
+
if (existing.phase !== "pending")
|
|
375
|
+
return {
|
|
376
|
+
ok: false,
|
|
377
|
+
violation: violation(
|
|
378
|
+
"STATE_VIOLATION",
|
|
379
|
+
`Subscription ${subErr.subscribe_id} is ${existing.phase}, not pending`,
|
|
380
|
+
this._phase,
|
|
381
|
+
message.type
|
|
382
|
+
)
|
|
383
|
+
};
|
|
384
|
+
this._subscriptions.set(subErr.subscribe_id, { ...existing, phase: "error" });
|
|
385
|
+
sideEffects.push({
|
|
386
|
+
type: "subscription-ended",
|
|
387
|
+
subscribeId: subErr.subscribe_id,
|
|
388
|
+
reason: subErr.reason_phrase
|
|
389
|
+
});
|
|
390
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
391
|
+
}
|
|
392
|
+
handleSubscribeUpdate(message, sideEffects) {
|
|
393
|
+
const err = this.requireReady(message.type);
|
|
394
|
+
if (err) return { ok: false, violation: err };
|
|
395
|
+
const update = message;
|
|
396
|
+
const idErr = this.checkKnownSubscribeId(update.subscribe_id, message.type);
|
|
397
|
+
if (idErr) return { ok: false, violation: idErr };
|
|
398
|
+
const existing = this._subscriptions.get(update.subscribe_id);
|
|
399
|
+
if (!existing)
|
|
400
|
+
return {
|
|
401
|
+
ok: false,
|
|
402
|
+
violation: violation(
|
|
403
|
+
"UNKNOWN_REQUEST_ID",
|
|
404
|
+
`No subscription with subscribe ID ${update.subscribe_id}`,
|
|
405
|
+
this._phase,
|
|
406
|
+
message.type
|
|
407
|
+
)
|
|
408
|
+
};
|
|
409
|
+
if (existing.phase !== "active")
|
|
410
|
+
return {
|
|
411
|
+
ok: false,
|
|
412
|
+
violation: violation(
|
|
413
|
+
"STATE_VIOLATION",
|
|
414
|
+
`Subscription ${update.subscribe_id} is ${existing.phase}, not active`,
|
|
415
|
+
this._phase,
|
|
416
|
+
message.type
|
|
417
|
+
)
|
|
418
|
+
};
|
|
419
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
420
|
+
}
|
|
421
|
+
handleSubscribeDone(message, sideEffects) {
|
|
422
|
+
const err = this.requireReady(message.type);
|
|
423
|
+
if (err) return { ok: false, violation: err };
|
|
424
|
+
const done = message;
|
|
425
|
+
const idErr = this.checkKnownSubscribeId(done.subscribe_id, message.type);
|
|
426
|
+
if (idErr) return { ok: false, violation: idErr };
|
|
427
|
+
const existing = this._subscriptions.get(done.subscribe_id);
|
|
428
|
+
if (!existing)
|
|
429
|
+
return {
|
|
430
|
+
ok: false,
|
|
431
|
+
violation: violation(
|
|
432
|
+
"UNKNOWN_REQUEST_ID",
|
|
433
|
+
`No subscription with subscribe ID ${done.subscribe_id}`,
|
|
434
|
+
this._phase,
|
|
435
|
+
message.type
|
|
436
|
+
)
|
|
437
|
+
};
|
|
438
|
+
this._subscriptions.set(done.subscribe_id, { ...existing, phase: "done" });
|
|
439
|
+
sideEffects.push({
|
|
440
|
+
type: "subscription-ended",
|
|
441
|
+
subscribeId: done.subscribe_id,
|
|
442
|
+
reason: done.reason_phrase
|
|
443
|
+
});
|
|
444
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
445
|
+
}
|
|
446
|
+
handleUnsubscribe(message, sideEffects) {
|
|
447
|
+
const err = this.requireReady(message.type);
|
|
448
|
+
if (err) return { ok: false, violation: err };
|
|
449
|
+
const unsub = message;
|
|
450
|
+
const idErr = this.checkKnownSubscribeId(unsub.subscribe_id, message.type);
|
|
451
|
+
if (idErr) return { ok: false, violation: idErr };
|
|
452
|
+
const existing = this._subscriptions.get(unsub.subscribe_id);
|
|
453
|
+
if (!existing)
|
|
454
|
+
return {
|
|
455
|
+
ok: false,
|
|
456
|
+
violation: violation(
|
|
457
|
+
"UNKNOWN_REQUEST_ID",
|
|
458
|
+
`No subscription with subscribe ID ${unsub.subscribe_id}`,
|
|
459
|
+
this._phase,
|
|
460
|
+
message.type
|
|
461
|
+
)
|
|
462
|
+
};
|
|
463
|
+
this._subscriptions.set(unsub.subscribe_id, { ...existing, phase: "done" });
|
|
464
|
+
sideEffects.push({
|
|
465
|
+
type: "subscription-ended",
|
|
466
|
+
subscribeId: unsub.subscribe_id,
|
|
467
|
+
reason: "unsubscribed"
|
|
468
|
+
});
|
|
469
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
470
|
+
}
|
|
471
|
+
// ─── Announce lifecycle ───
|
|
472
|
+
handleAnnounce(message, sideEffects) {
|
|
473
|
+
const err = this.requireReady(message.type);
|
|
474
|
+
if (err) return { ok: false, violation: err };
|
|
475
|
+
const ann = message;
|
|
476
|
+
const nsKey = ann.track_namespace.join("/");
|
|
477
|
+
this._announces.set(nsKey, { namespace: ann.track_namespace, phase: "pending" });
|
|
478
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
479
|
+
}
|
|
480
|
+
handleAnnounceOk(message, sideEffects) {
|
|
481
|
+
const err = this.requireReady(message.type);
|
|
482
|
+
if (err) return { ok: false, violation: err };
|
|
483
|
+
const ok = message;
|
|
484
|
+
const nsKey = ok.track_namespace.join("/");
|
|
485
|
+
const existing = this._announces.get(nsKey);
|
|
486
|
+
if (!existing)
|
|
487
|
+
return {
|
|
488
|
+
ok: false,
|
|
489
|
+
violation: violation(
|
|
490
|
+
"UNKNOWN_REQUEST_ID",
|
|
491
|
+
`No announce for namespace ${nsKey}`,
|
|
492
|
+
this._phase,
|
|
493
|
+
message.type
|
|
494
|
+
)
|
|
495
|
+
};
|
|
496
|
+
if (existing.phase !== "pending")
|
|
497
|
+
return {
|
|
498
|
+
ok: false,
|
|
499
|
+
violation: violation(
|
|
500
|
+
"STATE_VIOLATION",
|
|
501
|
+
`Announce ${nsKey} is ${existing.phase}, not pending`,
|
|
502
|
+
this._phase,
|
|
503
|
+
message.type
|
|
504
|
+
)
|
|
505
|
+
};
|
|
506
|
+
this._announces.set(nsKey, { ...existing, phase: "active" });
|
|
507
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
508
|
+
}
|
|
509
|
+
handleAnnounceError(message, sideEffects) {
|
|
510
|
+
const err = this.requireReady(message.type);
|
|
511
|
+
if (err) return { ok: false, violation: err };
|
|
512
|
+
const annErr = message;
|
|
513
|
+
const nsKey = annErr.track_namespace.join("/");
|
|
514
|
+
const existing = this._announces.get(nsKey);
|
|
515
|
+
if (!existing)
|
|
516
|
+
return {
|
|
517
|
+
ok: false,
|
|
518
|
+
violation: violation(
|
|
519
|
+
"UNKNOWN_REQUEST_ID",
|
|
520
|
+
`No announce for namespace ${nsKey}`,
|
|
521
|
+
this._phase,
|
|
522
|
+
message.type
|
|
523
|
+
)
|
|
524
|
+
};
|
|
525
|
+
if (existing.phase !== "pending")
|
|
526
|
+
return {
|
|
527
|
+
ok: false,
|
|
528
|
+
violation: violation(
|
|
529
|
+
"STATE_VIOLATION",
|
|
530
|
+
`Announce ${nsKey} is ${existing.phase}, not pending`,
|
|
531
|
+
this._phase,
|
|
532
|
+
message.type
|
|
533
|
+
)
|
|
534
|
+
};
|
|
535
|
+
this._announces.set(nsKey, { ...existing, phase: "error" });
|
|
536
|
+
sideEffects.push({ type: "announce-ended", namespace: annErr.track_namespace });
|
|
537
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
538
|
+
}
|
|
539
|
+
handleUnannounce(message, sideEffects) {
|
|
540
|
+
const err = this.requireReady(message.type);
|
|
541
|
+
if (err) return { ok: false, violation: err };
|
|
542
|
+
const unann = message;
|
|
543
|
+
const nsKey = unann.track_namespace.join("/");
|
|
544
|
+
const existing = this._announces.get(nsKey);
|
|
545
|
+
if (existing) {
|
|
546
|
+
this._announces.delete(nsKey);
|
|
547
|
+
sideEffects.push({ type: "announce-ended", namespace: unann.track_namespace });
|
|
548
|
+
}
|
|
549
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
550
|
+
}
|
|
551
|
+
handleAnnounceCancel(message, sideEffects) {
|
|
552
|
+
const err = this.requireReady(message.type);
|
|
553
|
+
if (err) return { ok: false, violation: err };
|
|
554
|
+
const cancel = message;
|
|
555
|
+
const nsKey = cancel.track_namespace.join("/");
|
|
556
|
+
const existing = this._announces.get(nsKey);
|
|
557
|
+
if (existing) {
|
|
558
|
+
this._announces.delete(nsKey);
|
|
559
|
+
sideEffects.push({ type: "announce-ended", namespace: cancel.track_namespace });
|
|
560
|
+
}
|
|
561
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
562
|
+
}
|
|
563
|
+
// ─── Fetch lifecycle ───
|
|
564
|
+
handleFetch(message, sideEffects) {
|
|
565
|
+
const err = this.requireReady(message.type);
|
|
566
|
+
if (err) return { ok: false, violation: err };
|
|
567
|
+
const fetch = message;
|
|
568
|
+
const dupErr = this.checkDuplicateSubscribeId(fetch.subscribe_id, message.type);
|
|
569
|
+
if (dupErr) return { ok: false, violation: dupErr };
|
|
570
|
+
this._subscribeIds.add(fetch.subscribe_id);
|
|
571
|
+
this._fetches.set(fetch.subscribe_id, { requestId: fetch.subscribe_id, phase: "pending" });
|
|
572
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
573
|
+
}
|
|
574
|
+
handleFetchOk(message, sideEffects) {
|
|
575
|
+
const err = this.requireReady(message.type);
|
|
576
|
+
if (err) return { ok: false, violation: err };
|
|
577
|
+
const ok = message;
|
|
578
|
+
const idErr = this.checkKnownSubscribeId(ok.subscribe_id, message.type);
|
|
579
|
+
if (idErr) return { ok: false, violation: idErr };
|
|
580
|
+
const existing = this._fetches.get(ok.subscribe_id);
|
|
581
|
+
if (!existing)
|
|
582
|
+
return {
|
|
583
|
+
ok: false,
|
|
584
|
+
violation: violation(
|
|
585
|
+
"UNKNOWN_REQUEST_ID",
|
|
586
|
+
`No fetch with subscribe ID ${ok.subscribe_id}`,
|
|
587
|
+
this._phase,
|
|
588
|
+
message.type
|
|
589
|
+
)
|
|
590
|
+
};
|
|
591
|
+
if (existing.phase !== "pending")
|
|
592
|
+
return {
|
|
593
|
+
ok: false,
|
|
594
|
+
violation: violation(
|
|
595
|
+
"STATE_VIOLATION",
|
|
596
|
+
`Fetch ${ok.subscribe_id} is ${existing.phase}, not pending`,
|
|
597
|
+
this._phase,
|
|
598
|
+
message.type
|
|
599
|
+
)
|
|
600
|
+
};
|
|
601
|
+
this._fetches.set(ok.subscribe_id, { ...existing, phase: "active" });
|
|
602
|
+
sideEffects.push({ type: "fetch-activated", requestId: ok.subscribe_id });
|
|
603
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
604
|
+
}
|
|
605
|
+
handleFetchError(message, sideEffects) {
|
|
606
|
+
const err = this.requireReady(message.type);
|
|
607
|
+
if (err) return { ok: false, violation: err };
|
|
608
|
+
const fetchErr = message;
|
|
609
|
+
const idErr = this.checkKnownSubscribeId(fetchErr.subscribe_id, message.type);
|
|
610
|
+
if (idErr) return { ok: false, violation: idErr };
|
|
611
|
+
const existing = this._fetches.get(fetchErr.subscribe_id);
|
|
612
|
+
if (!existing)
|
|
613
|
+
return {
|
|
614
|
+
ok: false,
|
|
615
|
+
violation: violation(
|
|
616
|
+
"UNKNOWN_REQUEST_ID",
|
|
617
|
+
`No fetch with subscribe ID ${fetchErr.subscribe_id}`,
|
|
618
|
+
this._phase,
|
|
619
|
+
message.type
|
|
620
|
+
)
|
|
621
|
+
};
|
|
622
|
+
this._fetches.set(fetchErr.subscribe_id, { ...existing, phase: "error" });
|
|
623
|
+
sideEffects.push({
|
|
624
|
+
type: "fetch-ended",
|
|
625
|
+
requestId: fetchErr.subscribe_id,
|
|
626
|
+
reason: fetchErr.reason_phrase
|
|
627
|
+
});
|
|
628
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
629
|
+
}
|
|
630
|
+
handleFetchCancel(message, sideEffects) {
|
|
631
|
+
const err = this.requireReady(message.type);
|
|
632
|
+
if (err) return { ok: false, violation: err };
|
|
633
|
+
const cancel = message;
|
|
634
|
+
const idErr = this.checkKnownSubscribeId(cancel.subscribe_id, message.type);
|
|
635
|
+
if (idErr) return { ok: false, violation: idErr };
|
|
636
|
+
const existing = this._fetches.get(cancel.subscribe_id);
|
|
637
|
+
if (!existing)
|
|
638
|
+
return {
|
|
639
|
+
ok: false,
|
|
640
|
+
violation: violation(
|
|
641
|
+
"UNKNOWN_REQUEST_ID",
|
|
642
|
+
`No fetch with subscribe ID ${cancel.subscribe_id}`,
|
|
643
|
+
this._phase,
|
|
644
|
+
message.type
|
|
645
|
+
)
|
|
646
|
+
};
|
|
647
|
+
this._fetches.set(cancel.subscribe_id, { ...existing, phase: "cancelled" });
|
|
648
|
+
sideEffects.push({ type: "fetch-ended", requestId: cancel.subscribe_id, reason: "cancelled" });
|
|
649
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
650
|
+
}
|
|
651
|
+
// ─── Generic ready-phase handler ───
|
|
652
|
+
handleReadyPhaseMessage(message) {
|
|
653
|
+
const err = this.requireReady(message.type);
|
|
654
|
+
if (err) return { ok: false, violation: err };
|
|
655
|
+
return { ok: true, phase: this._phase, sideEffects: [] };
|
|
656
|
+
}
|
|
657
|
+
reset() {
|
|
658
|
+
this._phase = "idle";
|
|
659
|
+
this._subscriptions.clear();
|
|
660
|
+
this._announces.clear();
|
|
661
|
+
this._fetches.clear();
|
|
662
|
+
this._subscribeIds.clear();
|
|
663
|
+
}
|
|
664
|
+
};
|
|
665
|
+
|
|
666
|
+
// src/drafts/draft10/session.ts
|
|
667
|
+
function createDraft10SessionState(options) {
|
|
668
|
+
return new Draft10SessionFSM(options.role);
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
export {
|
|
672
|
+
CONTROL_MESSAGES,
|
|
673
|
+
CLIENT_ONLY_MESSAGES,
|
|
674
|
+
SERVER_ONLY_MESSAGES,
|
|
675
|
+
BIDIRECTIONAL_MESSAGES,
|
|
676
|
+
getLegalOutgoing,
|
|
677
|
+
getLegalIncoming,
|
|
678
|
+
Draft10SessionFSM,
|
|
679
|
+
createDraft10SessionState
|
|
680
|
+
};
|