@moqtap/codec 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +99 -95
- package/dist/chunk-4RIXXEII.js +1275 -0
- package/dist/chunk-4XYGE53S.cjs +698 -0
- package/dist/chunk-4YJANAXU.cjs +1109 -0
- package/dist/chunk-6AEHWULA.cjs +641 -0
- package/dist/chunk-7DUBLRXC.js +680 -0
- package/dist/{chunk-YBZD3DU5.cjs → chunk-7IVGHMKJ.cjs} +44 -42
- package/dist/chunk-BISI45MN.cjs +680 -0
- package/dist/{chunk-NLYTRGXA.cjs → chunk-BPNL5YFQ.cjs} +5 -5
- package/dist/chunk-CXDHOMHG.js +1097 -0
- package/dist/chunk-DUBCL3WF.cjs +749 -0
- package/dist/chunk-DWK5ZQZ4.js +642 -0
- package/dist/chunk-E6E3NQYU.js +680 -0
- package/dist/chunk-EFM5T7OM.js +698 -0
- package/dist/chunk-ENURAVHI.cjs +680 -0
- package/dist/{chunk-Z66WDWHI.js → chunk-FXZ2MYKJ.js} +127 -1
- package/dist/chunk-G26SJ6XS.cjs +1341 -0
- package/dist/chunk-G7GI7LJA.js +737 -0
- package/dist/chunk-GXEW4COZ.cjs +737 -0
- package/dist/chunk-HSVYF6XX.cjs +1361 -0
- package/dist/chunk-IBVM4DMJ.cjs +1097 -0
- package/dist/chunk-IV2H5CFI.cjs +1275 -0
- package/dist/chunk-IV2HRJVT.js +1198 -0
- package/dist/chunk-JSQM2MG3.js +680 -0
- package/dist/chunk-K4OLITS2.cjs +1055 -0
- package/dist/{chunk-UOBWHJA5.js → chunk-KFTCU2P6.js} +2 -3
- package/dist/chunk-LH4NTURO.js +1361 -0
- package/dist/{chunk-YTXLWKOR.js → chunk-MFAP7R6L.js} +1 -1
- package/dist/chunk-NGVE2RZT.js +1097 -0
- package/dist/chunk-NUX5BHWO.js +1341 -0
- package/dist/chunk-PJRA2TQ5.js +1055 -0
- package/dist/chunk-RVJAGE4S.cjs +1198 -0
- package/dist/{chunk-QYG6KGOV.cjs → chunk-RWQ43Z4F.cjs} +2 -3
- package/dist/chunk-RZHAPEXO.js +749 -0
- package/dist/chunk-ST24APEO.js +1109 -0
- package/dist/chunk-SYHW3FLI.cjs +642 -0
- package/dist/chunk-TLYNOOQP.cjs +432 -0
- package/dist/{chunk-IXHOBNXA.js → chunk-TMNGRIPL.js} +43 -41
- package/dist/chunk-UNS34PTA.cjs +680 -0
- package/dist/chunk-UR6JKS56.js +432 -0
- package/dist/{chunk-NPWHHWXT.cjs → chunk-UYXTY6ZQ.cjs} +127 -1
- package/dist/chunk-XUUCOLWU.cjs +1097 -0
- package/dist/chunk-YG5KJESI.js +641 -0
- package/dist/chunk-ZBKE2QRQ.js +1401 -0
- package/dist/chunk-ZSPO2GF2.cjs +1401 -0
- package/dist/codec-95k8CAu5.d.cts +35 -0
- package/dist/codec-AFuOxfsO.d.ts +60 -0
- package/dist/codec-B-UJ5Iow.d.cts +75 -0
- package/dist/codec-BC5jfvMb.d.ts +35 -0
- package/dist/codec-BECYPfY8.d.ts +35 -0
- package/dist/codec-BsPU1vNC.d.ts +39 -0
- package/dist/codec-BvpuF-6u.d.cts +39 -0
- package/dist/codec-C8jZI5Cx.d.cts +39 -0
- package/dist/codec-CAevkgf5.d.cts +33 -0
- package/dist/codec-CSUqCrRs.d.ts +39 -0
- package/dist/codec-C_HMXNK_.d.ts +33 -0
- package/dist/{codec-Bvr7rFtj.d.cts → codec-CpuvYTSV.d.cts} +1 -1
- package/dist/codec-D0x8-SCw.d.cts +35 -0
- package/dist/codec-D7ARhpG1.d.ts +75 -0
- package/dist/codec-DNAUGshO.d.cts +60 -0
- package/dist/codec-DPx_QNn0.d.ts +31 -0
- package/dist/{codec-B2mc2g3i.d.ts → codec-DRhCx_hw.d.ts} +1 -1
- package/dist/codec-Db7YPe3l.d.ts +31 -0
- package/dist/codec-axkJpb7D.d.cts +31 -0
- package/dist/codec-ujAbFaep.d.cts +31 -0
- package/dist/draft10-session.cjs +6 -0
- package/dist/draft10-session.d.cts +8 -0
- package/dist/draft10-session.d.ts +8 -0
- package/dist/draft10-session.js +6 -0
- package/dist/draft10.cjs +115 -0
- package/dist/draft10.d.cts +95 -0
- package/dist/draft10.d.ts +95 -0
- package/dist/draft10.js +115 -0
- package/dist/draft11-session.cjs +6 -0
- package/dist/draft11-session.d.cts +8 -0
- package/dist/draft11-session.d.ts +8 -0
- package/dist/draft11-session.js +6 -0
- package/dist/draft11.cjs +109 -0
- package/dist/draft11.d.cts +99 -0
- package/dist/draft11.d.ts +99 -0
- package/dist/draft11.js +109 -0
- package/dist/draft12-session.cjs +6 -0
- package/dist/draft12-session.d.cts +8 -0
- package/dist/draft12-session.d.ts +8 -0
- package/dist/draft12-session.js +6 -0
- package/dist/draft12.cjs +117 -0
- package/dist/draft12.d.cts +106 -0
- package/dist/draft12.d.ts +106 -0
- package/dist/draft12.js +117 -0
- package/dist/draft13-session.cjs +6 -0
- package/dist/draft13-session.d.cts +8 -0
- package/dist/draft13-session.d.ts +8 -0
- package/dist/draft13-session.js +6 -0
- package/dist/draft13.cjs +119 -0
- package/dist/draft13.d.cts +108 -0
- package/dist/draft13.d.ts +108 -0
- package/dist/draft13.js +119 -0
- package/dist/draft14-session.cjs +2 -2
- package/dist/draft14-session.d.cts +3 -3
- package/dist/draft14-session.d.ts +3 -3
- package/dist/draft14-session.js +1 -1
- package/dist/draft14.cjs +3 -3
- package/dist/draft14.d.cts +16 -4
- package/dist/draft14.d.ts +16 -4
- package/dist/draft14.js +2 -2
- package/dist/draft15-session.cjs +6 -0
- package/dist/draft15-session.d.cts +8 -0
- package/dist/draft15-session.d.ts +8 -0
- package/dist/draft15-session.js +6 -0
- package/dist/draft15.cjs +111 -0
- package/dist/draft15.d.cts +93 -0
- package/dist/draft15.d.ts +93 -0
- package/dist/draft15.js +111 -0
- package/dist/draft16-session.cjs +6 -0
- package/dist/draft16-session.d.cts +8 -0
- package/dist/draft16-session.d.ts +8 -0
- package/dist/draft16-session.js +6 -0
- package/dist/draft16.cjs +113 -0
- package/dist/draft16.d.cts +94 -0
- package/dist/draft16.d.ts +94 -0
- package/dist/draft16.js +113 -0
- package/dist/draft17-session.cjs +8 -0
- package/dist/draft17-session.d.cts +51 -0
- package/dist/draft17-session.d.ts +51 -0
- package/dist/draft17-session.js +8 -0
- package/dist/draft17.cjs +99 -0
- package/dist/draft17.d.cts +40 -0
- package/dist/draft17.d.ts +40 -0
- package/dist/draft17.js +99 -0
- package/dist/draft7-session.cjs +3 -3
- package/dist/draft7-session.d.cts +3 -3
- package/dist/draft7-session.d.ts +3 -3
- package/dist/draft7-session.js +2 -2
- package/dist/draft7.cjs +4 -4
- package/dist/draft7.d.cts +2 -2
- package/dist/draft7.d.ts +2 -2
- package/dist/draft7.js +2 -2
- package/dist/draft8-session.cjs +6 -0
- package/dist/draft8-session.d.cts +8 -0
- package/dist/draft8-session.d.ts +8 -0
- package/dist/draft8-session.js +6 -0
- package/dist/draft8.cjs +115 -0
- package/dist/draft8.d.cts +95 -0
- package/dist/draft8.d.ts +95 -0
- package/dist/draft8.js +115 -0
- package/dist/draft9-session.cjs +6 -0
- package/dist/draft9-session.d.cts +8 -0
- package/dist/draft9-session.d.ts +8 -0
- package/dist/draft9-session.js +6 -0
- package/dist/draft9.cjs +115 -0
- package/dist/draft9.d.cts +95 -0
- package/dist/draft9.d.ts +95 -0
- package/dist/draft9.js +115 -0
- package/dist/index.cjs +76 -4
- package/dist/index.d.cts +68 -5
- package/dist/index.d.ts +68 -5
- package/dist/index.js +76 -4
- package/dist/{session-types-DFjMk4HH.d.ts → session-types-CJIFbTPd.d.ts} +1 -1
- package/dist/{session-types-DW1RSZX_.d.cts → session-types-Cbq8IGCP.d.cts} +1 -1
- package/dist/session.cjs +5 -5
- package/dist/session.d.cts +3 -3
- package/dist/session.d.ts +3 -3
- package/dist/session.js +5 -5
- package/dist/types-4VxSL2Ho.d.cts +261 -0
- package/dist/types-4VxSL2Ho.d.ts +261 -0
- package/dist/types-B2afJZM-.d.cts +236 -0
- package/dist/types-B2afJZM-.d.ts +236 -0
- package/dist/types-Bg6QYNVt.d.cts +290 -0
- package/dist/types-Bg6QYNVt.d.ts +290 -0
- package/dist/types-C_1HrqBl.d.cts +306 -0
- package/dist/types-C_1HrqBl.d.ts +306 -0
- package/dist/types-Cw4WE9dh.d.cts +261 -0
- package/dist/types-Cw4WE9dh.d.ts +261 -0
- package/dist/types-D5gNQiDj.d.cts +261 -0
- package/dist/types-D5gNQiDj.d.ts +261 -0
- package/dist/types-DqCDFqgB.d.cts +230 -0
- package/dist/types-DqCDFqgB.d.ts +230 -0
- package/dist/types-ERexTpT8.d.cts +217 -0
- package/dist/types-ERexTpT8.d.ts +217 -0
- package/dist/{types-DPYE49t0.d.cts → types-QNXoxC9Y.d.cts} +2 -7
- package/dist/{types-DPYE49t0.d.ts → types-QNXoxC9Y.d.ts} +2 -7
- package/dist/types-r-CasCf1.d.cts +262 -0
- package/dist/types-r-CasCf1.d.ts +262 -0
- package/package.json +110 -2
- package/src/core/types.ts +24 -7
- package/src/drafts/draft07/codec.ts +49 -41
- package/src/drafts/draft07/rules.ts +1 -3
- package/src/drafts/draft08/codec.ts +1254 -0
- package/src/drafts/draft08/index.ts +125 -0
- package/src/drafts/draft08/messages.ts +72 -0
- package/src/drafts/draft08/rules.ts +91 -0
- package/src/drafts/draft08/session-fsm.ts +718 -0
- package/src/drafts/draft08/session.ts +26 -0
- package/src/drafts/draft08/types.ts +377 -0
- package/src/drafts/draft09/codec.ts +1235 -0
- package/src/drafts/draft09/index.ts +125 -0
- package/src/drafts/draft09/messages.ts +72 -0
- package/src/drafts/draft09/rules.ts +91 -0
- package/src/drafts/draft09/session-fsm.ts +718 -0
- package/src/drafts/draft09/session.ts +26 -0
- package/src/drafts/draft09/types.ts +376 -0
- package/src/drafts/draft10/codec.ts +1235 -0
- package/src/drafts/draft10/index.ts +125 -0
- package/src/drafts/draft10/messages.ts +72 -0
- package/src/drafts/draft10/rules.ts +91 -0
- package/src/drafts/draft10/session-fsm.ts +718 -0
- package/src/drafts/draft10/session.ts +26 -0
- package/src/drafts/draft10/types.ts +376 -0
- package/src/drafts/draft11/codec.ts +1198 -0
- package/src/drafts/draft11/index.ts +123 -0
- package/src/drafts/draft11/messages.ts +71 -0
- package/src/drafts/draft11/rules.ts +100 -0
- package/src/drafts/draft11/session-fsm.ts +758 -0
- package/src/drafts/draft11/session.ts +26 -0
- package/src/drafts/draft11/types.ts +375 -0
- package/src/drafts/draft12/codec.ts +1354 -0
- package/src/drafts/draft12/index.ts +130 -0
- package/src/drafts/draft12/messages.ts +84 -0
- package/src/drafts/draft12/rules.ts +106 -0
- package/src/drafts/draft12/session-fsm.ts +805 -0
- package/src/drafts/draft12/session.ts +26 -0
- package/src/drafts/draft12/types.ts +414 -0
- package/src/drafts/draft13/codec.ts +1438 -0
- package/src/drafts/draft13/index.ts +132 -0
- package/src/drafts/draft13/messages.ts +86 -0
- package/src/drafts/draft13/rules.ts +108 -0
- package/src/drafts/draft13/session-fsm.ts +819 -0
- package/src/drafts/draft13/session.ts +26 -0
- package/src/drafts/draft13/types.ts +433 -0
- package/src/drafts/draft14/session-fsm.ts +182 -1
- package/src/drafts/draft15/codec.ts +1661 -0
- package/src/drafts/draft15/index.ts +121 -0
- package/src/drafts/draft15/messages.ts +64 -0
- package/src/drafts/draft15/rules.ts +95 -0
- package/src/drafts/draft15/session-fsm.ts +687 -0
- package/src/drafts/draft15/session.ts +26 -0
- package/src/drafts/draft15/types.ts +336 -0
- package/src/drafts/draft16/codec.ts +1623 -0
- package/src/drafts/draft16/index.ts +123 -0
- package/src/drafts/draft16/messages.ts +67 -0
- package/src/drafts/draft16/rules.ts +96 -0
- package/src/drafts/draft16/session-fsm.ts +682 -0
- package/src/drafts/draft16/session.ts +26 -0
- package/src/drafts/draft16/types.ts +354 -0
- package/src/drafts/draft17/codec.ts +1621 -0
- package/src/drafts/draft17/index.ts +105 -0
- package/src/drafts/draft17/messages.ts +53 -0
- package/src/drafts/draft17/rules.ts +85 -0
- package/src/drafts/draft17/session-fsm.ts +437 -0
- package/src/drafts/draft17/session.ts +15 -0
- package/src/drafts/draft17/types.ts +310 -0
- package/src/index.ts +217 -2
|
@@ -0,0 +1,819 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AnnounceState,
|
|
3
|
+
FetchState,
|
|
4
|
+
ProtocolViolation,
|
|
5
|
+
SessionPhase,
|
|
6
|
+
SideEffect,
|
|
7
|
+
SubscriptionState,
|
|
8
|
+
TransitionResult,
|
|
9
|
+
ValidationResult,
|
|
10
|
+
} from "../../core/session-types.js";
|
|
11
|
+
import {
|
|
12
|
+
CLIENT_ONLY_MESSAGES,
|
|
13
|
+
getLegalIncoming,
|
|
14
|
+
getLegalOutgoing,
|
|
15
|
+
SERVER_ONLY_MESSAGES,
|
|
16
|
+
} from "./rules.js";
|
|
17
|
+
import type { Draft13Message, Draft13MessageType } from "./types.js";
|
|
18
|
+
|
|
19
|
+
function violation(
|
|
20
|
+
code: ProtocolViolation<Draft13MessageType>["code"],
|
|
21
|
+
message: string,
|
|
22
|
+
currentPhase: SessionPhase,
|
|
23
|
+
offendingMessage: Draft13MessageType,
|
|
24
|
+
): ProtocolViolation<Draft13MessageType> {
|
|
25
|
+
return { code, message, currentPhase, offendingMessage };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export class Draft13SessionFSM {
|
|
29
|
+
private _phase: SessionPhase = "idle";
|
|
30
|
+
private _role: "client" | "server";
|
|
31
|
+
private _subscriptions = new Map<bigint, SubscriptionState>();
|
|
32
|
+
private _announces = new Map<string, AnnounceState>();
|
|
33
|
+
private _fetches = new Map<bigint, FetchState>();
|
|
34
|
+
private _requestIds = new Set<bigint>();
|
|
35
|
+
|
|
36
|
+
constructor(role: "client" | "server") {
|
|
37
|
+
this._role = role;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
get phase(): SessionPhase {
|
|
41
|
+
return this._phase;
|
|
42
|
+
}
|
|
43
|
+
get role(): "client" | "server" {
|
|
44
|
+
return this._role;
|
|
45
|
+
}
|
|
46
|
+
get subscriptions(): ReadonlyMap<bigint, SubscriptionState> {
|
|
47
|
+
return this._subscriptions;
|
|
48
|
+
}
|
|
49
|
+
get announces(): ReadonlyMap<string, AnnounceState> {
|
|
50
|
+
return this._announces;
|
|
51
|
+
}
|
|
52
|
+
get fetches(): ReadonlyMap<bigint, FetchState> {
|
|
53
|
+
return this._fetches;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
get legalOutgoing(): ReadonlySet<Draft13MessageType> {
|
|
57
|
+
return getLegalOutgoing(this._phase, this._role);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
get legalIncoming(): ReadonlySet<Draft13MessageType> {
|
|
61
|
+
return getLegalIncoming(this._phase, this._role);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
private checkRole(
|
|
65
|
+
message: Draft13Message,
|
|
66
|
+
direction: "inbound" | "outbound",
|
|
67
|
+
): ProtocolViolation<Draft13MessageType> | null {
|
|
68
|
+
const senderRole =
|
|
69
|
+
direction === "outbound" ? this._role : this._role === "client" ? "server" : "client";
|
|
70
|
+
|
|
71
|
+
if (CLIENT_ONLY_MESSAGES.has(message.type) && senderRole !== "client") {
|
|
72
|
+
return violation(
|
|
73
|
+
"ROLE_VIOLATION",
|
|
74
|
+
`${message.type} can only be sent by client`,
|
|
75
|
+
this._phase,
|
|
76
|
+
message.type,
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
if (SERVER_ONLY_MESSAGES.has(message.type) && senderRole !== "server") {
|
|
80
|
+
return violation(
|
|
81
|
+
"ROLE_VIOLATION",
|
|
82
|
+
`${message.type} can only be sent by server`,
|
|
83
|
+
this._phase,
|
|
84
|
+
message.type,
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
private checkDuplicateRequestId(
|
|
91
|
+
requestId: bigint,
|
|
92
|
+
msgType: Draft13MessageType,
|
|
93
|
+
): ProtocolViolation<Draft13MessageType> | null {
|
|
94
|
+
if (this._requestIds.has(requestId)) {
|
|
95
|
+
return violation(
|
|
96
|
+
"DUPLICATE_REQUEST_ID",
|
|
97
|
+
`Request ID ${requestId} already in use`,
|
|
98
|
+
this._phase,
|
|
99
|
+
msgType,
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
private checkKnownRequestId(
|
|
106
|
+
requestId: bigint,
|
|
107
|
+
msgType: Draft13MessageType,
|
|
108
|
+
): ProtocolViolation<Draft13MessageType> | null {
|
|
109
|
+
if (!this._requestIds.has(requestId)) {
|
|
110
|
+
return violation(
|
|
111
|
+
"UNKNOWN_REQUEST_ID",
|
|
112
|
+
`No request with ID ${requestId}`,
|
|
113
|
+
this._phase,
|
|
114
|
+
msgType,
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
validateOutgoing(message: Draft13Message): ValidationResult<Draft13MessageType> {
|
|
121
|
+
const roleViolation = this.checkRole(message, "outbound");
|
|
122
|
+
if (roleViolation) return { ok: false, violation: roleViolation };
|
|
123
|
+
|
|
124
|
+
if (!this.legalOutgoing.has(message.type)) {
|
|
125
|
+
return {
|
|
126
|
+
ok: false,
|
|
127
|
+
violation: violation(
|
|
128
|
+
this._phase === "idle" || this._phase === "setup"
|
|
129
|
+
? "MESSAGE_BEFORE_SETUP"
|
|
130
|
+
: "UNEXPECTED_MESSAGE",
|
|
131
|
+
`Cannot send ${message.type} in phase ${this._phase}`,
|
|
132
|
+
this._phase,
|
|
133
|
+
message.type,
|
|
134
|
+
),
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
return { ok: true };
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
receive(message: Draft13Message): TransitionResult<Draft13MessageType> {
|
|
141
|
+
const roleViolation = this.checkRole(message, "inbound");
|
|
142
|
+
if (roleViolation) return { ok: false, violation: roleViolation };
|
|
143
|
+
return this.applyTransition(message, "inbound");
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
send(message: Draft13Message): TransitionResult<Draft13MessageType> {
|
|
147
|
+
const roleViolation = this.checkRole(message, "outbound");
|
|
148
|
+
if (roleViolation) return { ok: false, violation: roleViolation };
|
|
149
|
+
return this.applyTransition(message, "outbound");
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
private applyTransition(
|
|
153
|
+
message: Draft13Message,
|
|
154
|
+
direction: "inbound" | "outbound",
|
|
155
|
+
): TransitionResult<Draft13MessageType> {
|
|
156
|
+
const sideEffects: SideEffect[] = [];
|
|
157
|
+
|
|
158
|
+
switch (message.type) {
|
|
159
|
+
case "client_setup":
|
|
160
|
+
return this.handleClientSetup(direction);
|
|
161
|
+
case "server_setup":
|
|
162
|
+
return this.handleServerSetup(direction);
|
|
163
|
+
case "goaway":
|
|
164
|
+
return this.handleGoAway(message, sideEffects);
|
|
165
|
+
|
|
166
|
+
// Subscribe lifecycle
|
|
167
|
+
case "subscribe":
|
|
168
|
+
return this.handleSubscribe(message, sideEffects);
|
|
169
|
+
case "subscribe_ok":
|
|
170
|
+
return this.handleSubscribeOk(message, sideEffects);
|
|
171
|
+
case "subscribe_error":
|
|
172
|
+
return this.handleSubscribeError(message, sideEffects);
|
|
173
|
+
case "subscribe_update":
|
|
174
|
+
return this.handleSubscribeUpdate(message, sideEffects);
|
|
175
|
+
case "subscribe_done":
|
|
176
|
+
return this.handleSubscribeDone(message, sideEffects);
|
|
177
|
+
case "unsubscribe":
|
|
178
|
+
return this.handleUnsubscribe(message, sideEffects);
|
|
179
|
+
|
|
180
|
+
// Announce lifecycle
|
|
181
|
+
case "announce":
|
|
182
|
+
return this.handleAnnounce(message, sideEffects);
|
|
183
|
+
case "announce_ok":
|
|
184
|
+
return this.handleAnnounceOk(message, sideEffects);
|
|
185
|
+
case "announce_error":
|
|
186
|
+
return this.handleAnnounceError(message, sideEffects);
|
|
187
|
+
case "unannounce":
|
|
188
|
+
return this.handleUnannounce(message, sideEffects);
|
|
189
|
+
case "announce_cancel":
|
|
190
|
+
return this.handleAnnounceCancel(message, sideEffects);
|
|
191
|
+
|
|
192
|
+
// Publish lifecycle
|
|
193
|
+
case "publish":
|
|
194
|
+
return this.handlePublish(message, sideEffects);
|
|
195
|
+
case "publish_ok":
|
|
196
|
+
return this.handlePublishOk(message, sideEffects);
|
|
197
|
+
case "publish_error":
|
|
198
|
+
return this.handlePublishError(message, sideEffects);
|
|
199
|
+
|
|
200
|
+
// Fetch lifecycle
|
|
201
|
+
case "fetch":
|
|
202
|
+
return this.handleFetch(message, sideEffects);
|
|
203
|
+
case "fetch_ok":
|
|
204
|
+
return this.handleFetchOk(message, sideEffects);
|
|
205
|
+
case "fetch_error":
|
|
206
|
+
return this.handleFetchError(message, sideEffects);
|
|
207
|
+
case "fetch_cancel":
|
|
208
|
+
return this.handleFetchCancel(message, sideEffects);
|
|
209
|
+
|
|
210
|
+
// Subscribe namespace lifecycle
|
|
211
|
+
case "subscribe_namespace":
|
|
212
|
+
return this.handleSubscribeNamespace(message, sideEffects);
|
|
213
|
+
case "subscribe_namespace_ok":
|
|
214
|
+
return this.handleSubscribeNamespaceOk(message, sideEffects);
|
|
215
|
+
case "subscribe_namespace_error":
|
|
216
|
+
return this.handleSubscribeNamespaceError(message, sideEffects);
|
|
217
|
+
|
|
218
|
+
// Track status lifecycle
|
|
219
|
+
case "track_status":
|
|
220
|
+
return this.handleTrackStatus(message, sideEffects);
|
|
221
|
+
case "track_status_ok":
|
|
222
|
+
return this.handleTrackStatusOk(message, sideEffects);
|
|
223
|
+
case "track_status_error":
|
|
224
|
+
return this.handleTrackStatusError(message, sideEffects);
|
|
225
|
+
|
|
226
|
+
// Other ready-phase messages
|
|
227
|
+
default:
|
|
228
|
+
return this.handleReadyPhaseMessage(message);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
private handleClientSetup(
|
|
233
|
+
direction: "inbound" | "outbound",
|
|
234
|
+
): TransitionResult<Draft13MessageType> {
|
|
235
|
+
if (this._phase !== "idle") {
|
|
236
|
+
return {
|
|
237
|
+
ok: false,
|
|
238
|
+
violation: violation(
|
|
239
|
+
"SETUP_VIOLATION",
|
|
240
|
+
"CLIENT_SETUP already sent/received",
|
|
241
|
+
this._phase,
|
|
242
|
+
"client_setup",
|
|
243
|
+
),
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
if (direction === "outbound" && this._role !== "client") {
|
|
247
|
+
return {
|
|
248
|
+
ok: false,
|
|
249
|
+
violation: violation(
|
|
250
|
+
"ROLE_VIOLATION",
|
|
251
|
+
"Only client can send CLIENT_SETUP",
|
|
252
|
+
this._phase,
|
|
253
|
+
"client_setup",
|
|
254
|
+
),
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
this._phase = "setup";
|
|
258
|
+
return { ok: true, phase: this._phase, sideEffects: [] };
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
private handleServerSetup(
|
|
262
|
+
direction: "inbound" | "outbound",
|
|
263
|
+
): TransitionResult<Draft13MessageType> {
|
|
264
|
+
if (this._phase !== "setup") {
|
|
265
|
+
return {
|
|
266
|
+
ok: false,
|
|
267
|
+
violation: violation(
|
|
268
|
+
"SETUP_VIOLATION",
|
|
269
|
+
"SERVER_SETUP before CLIENT_SETUP",
|
|
270
|
+
this._phase,
|
|
271
|
+
"server_setup",
|
|
272
|
+
),
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
if (direction === "outbound" && this._role !== "server") {
|
|
276
|
+
return {
|
|
277
|
+
ok: false,
|
|
278
|
+
violation: violation(
|
|
279
|
+
"ROLE_VIOLATION",
|
|
280
|
+
"Only server can send SERVER_SETUP",
|
|
281
|
+
this._phase,
|
|
282
|
+
"server_setup",
|
|
283
|
+
),
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
this._phase = "ready";
|
|
287
|
+
return { ok: true, phase: this._phase, sideEffects: [{ type: "session-ready" }] };
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
private handleGoAway(
|
|
291
|
+
message: Draft13Message,
|
|
292
|
+
sideEffects: SideEffect[],
|
|
293
|
+
): TransitionResult<Draft13MessageType> {
|
|
294
|
+
if (this._phase !== "ready" && this._phase !== "draining") {
|
|
295
|
+
return {
|
|
296
|
+
ok: false,
|
|
297
|
+
violation: violation(
|
|
298
|
+
"UNEXPECTED_MESSAGE",
|
|
299
|
+
`GOAWAY not valid in phase ${this._phase}`,
|
|
300
|
+
this._phase,
|
|
301
|
+
"goaway",
|
|
302
|
+
),
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
this._phase = "draining";
|
|
306
|
+
const goaway = message as import("./types.js").Draft13GoAway;
|
|
307
|
+
sideEffects.push({ type: "session-draining", goAwayUri: goaway.new_session_uri });
|
|
308
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
private requireReady(msgType: Draft13MessageType): ProtocolViolation<Draft13MessageType> | null {
|
|
312
|
+
if (this._phase !== "ready" && this._phase !== "draining") {
|
|
313
|
+
return violation(
|
|
314
|
+
this._phase === "idle" || this._phase === "setup"
|
|
315
|
+
? "MESSAGE_BEFORE_SETUP"
|
|
316
|
+
: "UNEXPECTED_MESSAGE",
|
|
317
|
+
`${msgType} requires ready phase, current: ${this._phase}`,
|
|
318
|
+
this._phase,
|
|
319
|
+
msgType,
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
return null;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// ─── Subscribe lifecycle ───
|
|
326
|
+
|
|
327
|
+
private handleSubscribe(
|
|
328
|
+
message: Draft13Message,
|
|
329
|
+
sideEffects: SideEffect[],
|
|
330
|
+
): TransitionResult<Draft13MessageType> {
|
|
331
|
+
const err = this.requireReady(message.type);
|
|
332
|
+
if (err) return { ok: false, violation: err };
|
|
333
|
+
const sub = message as import("./types.js").Draft13Subscribe;
|
|
334
|
+
const dupErr = this.checkDuplicateRequestId(sub.request_id, message.type);
|
|
335
|
+
if (dupErr) return { ok: false, violation: dupErr };
|
|
336
|
+
this._requestIds.add(sub.request_id);
|
|
337
|
+
this._subscriptions.set(sub.request_id, {
|
|
338
|
+
subscribeId: sub.request_id,
|
|
339
|
+
phase: "pending",
|
|
340
|
+
trackNamespace: sub.track_namespace,
|
|
341
|
+
trackName: sub.track_name,
|
|
342
|
+
});
|
|
343
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
private handleSubscribeOk(
|
|
347
|
+
message: Draft13Message,
|
|
348
|
+
sideEffects: SideEffect[],
|
|
349
|
+
): TransitionResult<Draft13MessageType> {
|
|
350
|
+
const err = this.requireReady(message.type);
|
|
351
|
+
if (err) return { ok: false, violation: err };
|
|
352
|
+
const ok = message as import("./types.js").Draft13SubscribeOk;
|
|
353
|
+
const idErr = this.checkKnownRequestId(ok.request_id, message.type);
|
|
354
|
+
if (idErr) return { ok: false, violation: idErr };
|
|
355
|
+
const existing = this._subscriptions.get(ok.request_id);
|
|
356
|
+
if (!existing)
|
|
357
|
+
return {
|
|
358
|
+
ok: false,
|
|
359
|
+
violation: violation(
|
|
360
|
+
"UNKNOWN_REQUEST_ID",
|
|
361
|
+
`No subscription with request ID ${ok.request_id}`,
|
|
362
|
+
this._phase,
|
|
363
|
+
message.type,
|
|
364
|
+
),
|
|
365
|
+
};
|
|
366
|
+
if (existing.phase !== "pending")
|
|
367
|
+
return {
|
|
368
|
+
ok: false,
|
|
369
|
+
violation: violation(
|
|
370
|
+
"STATE_VIOLATION",
|
|
371
|
+
`Subscription ${ok.request_id} is ${existing.phase}, not pending`,
|
|
372
|
+
this._phase,
|
|
373
|
+
message.type,
|
|
374
|
+
),
|
|
375
|
+
};
|
|
376
|
+
this._subscriptions.set(ok.request_id, { ...existing, phase: "active" });
|
|
377
|
+
sideEffects.push({ type: "subscription-activated", subscribeId: ok.request_id });
|
|
378
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
private handleSubscribeError(
|
|
382
|
+
message: Draft13Message,
|
|
383
|
+
sideEffects: SideEffect[],
|
|
384
|
+
): TransitionResult<Draft13MessageType> {
|
|
385
|
+
const err = this.requireReady(message.type);
|
|
386
|
+
if (err) return { ok: false, violation: err };
|
|
387
|
+
const subErr = message as import("./types.js").Draft13SubscribeError;
|
|
388
|
+
const idErr = this.checkKnownRequestId(subErr.request_id, message.type);
|
|
389
|
+
if (idErr) return { ok: false, violation: idErr };
|
|
390
|
+
const existing = this._subscriptions.get(subErr.request_id);
|
|
391
|
+
if (!existing)
|
|
392
|
+
return {
|
|
393
|
+
ok: false,
|
|
394
|
+
violation: violation(
|
|
395
|
+
"UNKNOWN_REQUEST_ID",
|
|
396
|
+
`No subscription with request ID ${subErr.request_id}`,
|
|
397
|
+
this._phase,
|
|
398
|
+
message.type,
|
|
399
|
+
),
|
|
400
|
+
};
|
|
401
|
+
if (existing.phase !== "pending")
|
|
402
|
+
return {
|
|
403
|
+
ok: false,
|
|
404
|
+
violation: violation(
|
|
405
|
+
"STATE_VIOLATION",
|
|
406
|
+
`Subscription ${subErr.request_id} is ${existing.phase}, not pending`,
|
|
407
|
+
this._phase,
|
|
408
|
+
message.type,
|
|
409
|
+
),
|
|
410
|
+
};
|
|
411
|
+
this._subscriptions.set(subErr.request_id, { ...existing, phase: "error" });
|
|
412
|
+
sideEffects.push({
|
|
413
|
+
type: "subscription-ended",
|
|
414
|
+
subscribeId: subErr.request_id,
|
|
415
|
+
reason: subErr.reason_phrase,
|
|
416
|
+
});
|
|
417
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
private handleSubscribeUpdate(
|
|
421
|
+
message: Draft13Message,
|
|
422
|
+
sideEffects: SideEffect[],
|
|
423
|
+
): TransitionResult<Draft13MessageType> {
|
|
424
|
+
const err = this.requireReady(message.type);
|
|
425
|
+
if (err) return { ok: false, violation: err };
|
|
426
|
+
const update = message as import("./types.js").Draft13SubscribeUpdate;
|
|
427
|
+
const idErr = this.checkKnownRequestId(update.request_id, message.type);
|
|
428
|
+
if (idErr) return { ok: false, violation: idErr };
|
|
429
|
+
const existing = this._subscriptions.get(update.request_id);
|
|
430
|
+
if (!existing)
|
|
431
|
+
return {
|
|
432
|
+
ok: false,
|
|
433
|
+
violation: violation(
|
|
434
|
+
"UNKNOWN_REQUEST_ID",
|
|
435
|
+
`No subscription with request ID ${update.request_id}`,
|
|
436
|
+
this._phase,
|
|
437
|
+
message.type,
|
|
438
|
+
),
|
|
439
|
+
};
|
|
440
|
+
if (existing.phase !== "active")
|
|
441
|
+
return {
|
|
442
|
+
ok: false,
|
|
443
|
+
violation: violation(
|
|
444
|
+
"STATE_VIOLATION",
|
|
445
|
+
`Subscription ${update.request_id} is ${existing.phase}, not active`,
|
|
446
|
+
this._phase,
|
|
447
|
+
message.type,
|
|
448
|
+
),
|
|
449
|
+
};
|
|
450
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
private handleSubscribeDone(
|
|
454
|
+
message: Draft13Message,
|
|
455
|
+
sideEffects: SideEffect[],
|
|
456
|
+
): TransitionResult<Draft13MessageType> {
|
|
457
|
+
const err = this.requireReady(message.type);
|
|
458
|
+
if (err) return { ok: false, violation: err };
|
|
459
|
+
const done = message as import("./types.js").Draft13SubscribeDone;
|
|
460
|
+
const idErr = this.checkKnownRequestId(done.request_id, message.type);
|
|
461
|
+
if (idErr) return { ok: false, violation: idErr };
|
|
462
|
+
const existing = this._subscriptions.get(done.request_id);
|
|
463
|
+
if (!existing)
|
|
464
|
+
return {
|
|
465
|
+
ok: false,
|
|
466
|
+
violation: violation(
|
|
467
|
+
"UNKNOWN_REQUEST_ID",
|
|
468
|
+
`No subscription with request ID ${done.request_id}`,
|
|
469
|
+
this._phase,
|
|
470
|
+
message.type,
|
|
471
|
+
),
|
|
472
|
+
};
|
|
473
|
+
this._subscriptions.set(done.request_id, { ...existing, phase: "done" });
|
|
474
|
+
sideEffects.push({
|
|
475
|
+
type: "subscription-ended",
|
|
476
|
+
subscribeId: done.request_id,
|
|
477
|
+
reason: done.reason_phrase,
|
|
478
|
+
});
|
|
479
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
private handleUnsubscribe(
|
|
483
|
+
message: Draft13Message,
|
|
484
|
+
sideEffects: SideEffect[],
|
|
485
|
+
): TransitionResult<Draft13MessageType> {
|
|
486
|
+
const err = this.requireReady(message.type);
|
|
487
|
+
if (err) return { ok: false, violation: err };
|
|
488
|
+
const unsub = message as import("./types.js").Draft13Unsubscribe;
|
|
489
|
+
const idErr = this.checkKnownRequestId(unsub.request_id, message.type);
|
|
490
|
+
if (idErr) return { ok: false, violation: idErr };
|
|
491
|
+
const existing = this._subscriptions.get(unsub.request_id);
|
|
492
|
+
if (!existing)
|
|
493
|
+
return {
|
|
494
|
+
ok: false,
|
|
495
|
+
violation: violation(
|
|
496
|
+
"UNKNOWN_REQUEST_ID",
|
|
497
|
+
`No subscription with request ID ${unsub.request_id}`,
|
|
498
|
+
this._phase,
|
|
499
|
+
message.type,
|
|
500
|
+
),
|
|
501
|
+
};
|
|
502
|
+
this._subscriptions.set(unsub.request_id, { ...existing, phase: "done" });
|
|
503
|
+
sideEffects.push({
|
|
504
|
+
type: "subscription-ended",
|
|
505
|
+
subscribeId: unsub.request_id,
|
|
506
|
+
reason: "unsubscribed",
|
|
507
|
+
});
|
|
508
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
// ─── Announce lifecycle ───
|
|
512
|
+
|
|
513
|
+
private handleAnnounce(
|
|
514
|
+
message: Draft13Message,
|
|
515
|
+
sideEffects: SideEffect[],
|
|
516
|
+
): TransitionResult<Draft13MessageType> {
|
|
517
|
+
const err = this.requireReady(message.type);
|
|
518
|
+
if (err) return { ok: false, violation: err };
|
|
519
|
+
const ann = message as import("./types.js").Draft13Announce;
|
|
520
|
+
const nsKey = ann.track_namespace.join("/");
|
|
521
|
+
this._requestIds.add(ann.request_id);
|
|
522
|
+
this._announces.set(nsKey, { namespace: ann.track_namespace, phase: "pending" });
|
|
523
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
private handleAnnounceOk(
|
|
527
|
+
message: Draft13Message,
|
|
528
|
+
sideEffects: SideEffect[],
|
|
529
|
+
): TransitionResult<Draft13MessageType> {
|
|
530
|
+
const err = this.requireReady(message.type);
|
|
531
|
+
if (err) return { ok: false, violation: err };
|
|
532
|
+
const ok = message as import("./types.js").Draft13AnnounceOk;
|
|
533
|
+
const idErr = this.checkKnownRequestId(ok.request_id, message.type);
|
|
534
|
+
if (idErr) return { ok: false, violation: idErr };
|
|
535
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
private handleAnnounceError(
|
|
539
|
+
message: Draft13Message,
|
|
540
|
+
sideEffects: SideEffect[],
|
|
541
|
+
): TransitionResult<Draft13MessageType> {
|
|
542
|
+
const err = this.requireReady(message.type);
|
|
543
|
+
if (err) return { ok: false, violation: err };
|
|
544
|
+
const annErr = message as import("./types.js").Draft13AnnounceError;
|
|
545
|
+
const idErr = this.checkKnownRequestId(annErr.request_id, message.type);
|
|
546
|
+
if (idErr) return { ok: false, violation: idErr };
|
|
547
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
private handleUnannounce(
|
|
551
|
+
message: Draft13Message,
|
|
552
|
+
sideEffects: SideEffect[],
|
|
553
|
+
): TransitionResult<Draft13MessageType> {
|
|
554
|
+
const err = this.requireReady(message.type);
|
|
555
|
+
if (err) return { ok: false, violation: err };
|
|
556
|
+
const unann = message as import("./types.js").Draft13Unannounce;
|
|
557
|
+
const nsKey = unann.track_namespace.join("/");
|
|
558
|
+
const existing = this._announces.get(nsKey);
|
|
559
|
+
if (existing) {
|
|
560
|
+
this._announces.delete(nsKey);
|
|
561
|
+
sideEffects.push({ type: "announce-ended", namespace: unann.track_namespace });
|
|
562
|
+
}
|
|
563
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
private handleAnnounceCancel(
|
|
567
|
+
message: Draft13Message,
|
|
568
|
+
sideEffects: SideEffect[],
|
|
569
|
+
): TransitionResult<Draft13MessageType> {
|
|
570
|
+
const err = this.requireReady(message.type);
|
|
571
|
+
if (err) return { ok: false, violation: err };
|
|
572
|
+
const cancel = message as import("./types.js").Draft13AnnounceCancel;
|
|
573
|
+
const nsKey = cancel.track_namespace.join("/");
|
|
574
|
+
const existing = this._announces.get(nsKey);
|
|
575
|
+
if (existing) {
|
|
576
|
+
this._announces.delete(nsKey);
|
|
577
|
+
sideEffects.push({ type: "announce-ended", namespace: cancel.track_namespace });
|
|
578
|
+
}
|
|
579
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
// ─── Publish lifecycle ───
|
|
583
|
+
|
|
584
|
+
private handlePublish(
|
|
585
|
+
message: Draft13Message,
|
|
586
|
+
sideEffects: SideEffect[],
|
|
587
|
+
): TransitionResult<Draft13MessageType> {
|
|
588
|
+
const err = this.requireReady(message.type);
|
|
589
|
+
if (err) return { ok: false, violation: err };
|
|
590
|
+
const pub = message as import("./types.js").Draft13Publish;
|
|
591
|
+
const dupErr = this.checkDuplicateRequestId(pub.request_id, message.type);
|
|
592
|
+
if (dupErr) return { ok: false, violation: dupErr };
|
|
593
|
+
this._requestIds.add(pub.request_id);
|
|
594
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
private handlePublishOk(
|
|
598
|
+
message: Draft13Message,
|
|
599
|
+
sideEffects: SideEffect[],
|
|
600
|
+
): TransitionResult<Draft13MessageType> {
|
|
601
|
+
const err = this.requireReady(message.type);
|
|
602
|
+
if (err) return { ok: false, violation: err };
|
|
603
|
+
const ok = message as import("./types.js").Draft13PublishOk;
|
|
604
|
+
const idErr = this.checkKnownRequestId(ok.request_id, message.type);
|
|
605
|
+
if (idErr) return { ok: false, violation: idErr };
|
|
606
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
private handlePublishError(
|
|
610
|
+
message: Draft13Message,
|
|
611
|
+
sideEffects: SideEffect[],
|
|
612
|
+
): TransitionResult<Draft13MessageType> {
|
|
613
|
+
const err = this.requireReady(message.type);
|
|
614
|
+
if (err) return { ok: false, violation: err };
|
|
615
|
+
const pubErr = message as import("./types.js").Draft13PublishError;
|
|
616
|
+
const idErr = this.checkKnownRequestId(pubErr.request_id, message.type);
|
|
617
|
+
if (idErr) return { ok: false, violation: idErr };
|
|
618
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
// ─── Fetch lifecycle ───
|
|
622
|
+
|
|
623
|
+
private handleFetch(
|
|
624
|
+
message: Draft13Message,
|
|
625
|
+
sideEffects: SideEffect[],
|
|
626
|
+
): TransitionResult<Draft13MessageType> {
|
|
627
|
+
const err = this.requireReady(message.type);
|
|
628
|
+
if (err) return { ok: false, violation: err };
|
|
629
|
+
const fetch = message as import("./types.js").Draft13Fetch;
|
|
630
|
+
const dupErr = this.checkDuplicateRequestId(fetch.request_id, message.type);
|
|
631
|
+
if (dupErr) return { ok: false, violation: dupErr };
|
|
632
|
+
this._requestIds.add(fetch.request_id);
|
|
633
|
+
this._fetches.set(fetch.request_id, { requestId: fetch.request_id, phase: "pending" });
|
|
634
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
private handleFetchOk(
|
|
638
|
+
message: Draft13Message,
|
|
639
|
+
sideEffects: SideEffect[],
|
|
640
|
+
): TransitionResult<Draft13MessageType> {
|
|
641
|
+
const err = this.requireReady(message.type);
|
|
642
|
+
if (err) return { ok: false, violation: err };
|
|
643
|
+
const ok = message as import("./types.js").Draft13FetchOk;
|
|
644
|
+
const idErr = this.checkKnownRequestId(ok.request_id, message.type);
|
|
645
|
+
if (idErr) return { ok: false, violation: idErr };
|
|
646
|
+
const existing = this._fetches.get(ok.request_id);
|
|
647
|
+
if (!existing)
|
|
648
|
+
return {
|
|
649
|
+
ok: false,
|
|
650
|
+
violation: violation(
|
|
651
|
+
"UNKNOWN_REQUEST_ID",
|
|
652
|
+
`No fetch with request ID ${ok.request_id}`,
|
|
653
|
+
this._phase,
|
|
654
|
+
message.type,
|
|
655
|
+
),
|
|
656
|
+
};
|
|
657
|
+
if (existing.phase !== "pending")
|
|
658
|
+
return {
|
|
659
|
+
ok: false,
|
|
660
|
+
violation: violation(
|
|
661
|
+
"STATE_VIOLATION",
|
|
662
|
+
`Fetch ${ok.request_id} is ${existing.phase}, not pending`,
|
|
663
|
+
this._phase,
|
|
664
|
+
message.type,
|
|
665
|
+
),
|
|
666
|
+
};
|
|
667
|
+
this._fetches.set(ok.request_id, { ...existing, phase: "active" });
|
|
668
|
+
sideEffects.push({ type: "fetch-activated", requestId: ok.request_id });
|
|
669
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
private handleFetchError(
|
|
673
|
+
message: Draft13Message,
|
|
674
|
+
sideEffects: SideEffect[],
|
|
675
|
+
): TransitionResult<Draft13MessageType> {
|
|
676
|
+
const err = this.requireReady(message.type);
|
|
677
|
+
if (err) return { ok: false, violation: err };
|
|
678
|
+
const fetchErr = message as import("./types.js").Draft13FetchError;
|
|
679
|
+
const idErr = this.checkKnownRequestId(fetchErr.request_id, message.type);
|
|
680
|
+
if (idErr) return { ok: false, violation: idErr };
|
|
681
|
+
const existing = this._fetches.get(fetchErr.request_id);
|
|
682
|
+
if (!existing)
|
|
683
|
+
return {
|
|
684
|
+
ok: false,
|
|
685
|
+
violation: violation(
|
|
686
|
+
"UNKNOWN_REQUEST_ID",
|
|
687
|
+
`No fetch with request ID ${fetchErr.request_id}`,
|
|
688
|
+
this._phase,
|
|
689
|
+
message.type,
|
|
690
|
+
),
|
|
691
|
+
};
|
|
692
|
+
this._fetches.set(fetchErr.request_id, { ...existing, phase: "error" });
|
|
693
|
+
sideEffects.push({
|
|
694
|
+
type: "fetch-ended",
|
|
695
|
+
requestId: fetchErr.request_id,
|
|
696
|
+
reason: fetchErr.reason_phrase,
|
|
697
|
+
});
|
|
698
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
private handleFetchCancel(
|
|
702
|
+
message: Draft13Message,
|
|
703
|
+
sideEffects: SideEffect[],
|
|
704
|
+
): TransitionResult<Draft13MessageType> {
|
|
705
|
+
const err = this.requireReady(message.type);
|
|
706
|
+
if (err) return { ok: false, violation: err };
|
|
707
|
+
const cancel = message as import("./types.js").Draft13FetchCancel;
|
|
708
|
+
const idErr = this.checkKnownRequestId(cancel.request_id, message.type);
|
|
709
|
+
if (idErr) return { ok: false, violation: idErr };
|
|
710
|
+
const existing = this._fetches.get(cancel.request_id);
|
|
711
|
+
if (!existing)
|
|
712
|
+
return {
|
|
713
|
+
ok: false,
|
|
714
|
+
violation: violation(
|
|
715
|
+
"UNKNOWN_REQUEST_ID",
|
|
716
|
+
`No fetch with request ID ${cancel.request_id}`,
|
|
717
|
+
this._phase,
|
|
718
|
+
message.type,
|
|
719
|
+
),
|
|
720
|
+
};
|
|
721
|
+
this._fetches.set(cancel.request_id, { ...existing, phase: "cancelled" });
|
|
722
|
+
sideEffects.push({ type: "fetch-ended", requestId: cancel.request_id, reason: "cancelled" });
|
|
723
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
// ─── Track status lifecycle ───
|
|
727
|
+
|
|
728
|
+
private handleTrackStatus(
|
|
729
|
+
message: Draft13Message,
|
|
730
|
+
sideEffects: SideEffect[],
|
|
731
|
+
): TransitionResult<Draft13MessageType> {
|
|
732
|
+
const err = this.requireReady(message.type);
|
|
733
|
+
if (err) return { ok: false, violation: err };
|
|
734
|
+
const ts = message as import("./types.js").Draft13TrackStatus;
|
|
735
|
+
const dupErr = this.checkDuplicateRequestId(ts.request_id, message.type);
|
|
736
|
+
if (dupErr) return { ok: false, violation: dupErr };
|
|
737
|
+
this._requestIds.add(ts.request_id);
|
|
738
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
private handleTrackStatusOk(
|
|
742
|
+
message: Draft13Message,
|
|
743
|
+
sideEffects: SideEffect[],
|
|
744
|
+
): TransitionResult<Draft13MessageType> {
|
|
745
|
+
const err = this.requireReady(message.type);
|
|
746
|
+
if (err) return { ok: false, violation: err };
|
|
747
|
+
const ok = message as import("./types.js").Draft13TrackStatusOk;
|
|
748
|
+
const idErr = this.checkKnownRequestId(ok.request_id, message.type);
|
|
749
|
+
if (idErr) return { ok: false, violation: idErr };
|
|
750
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
private handleTrackStatusError(
|
|
754
|
+
message: Draft13Message,
|
|
755
|
+
sideEffects: SideEffect[],
|
|
756
|
+
): TransitionResult<Draft13MessageType> {
|
|
757
|
+
const err = this.requireReady(message.type);
|
|
758
|
+
if (err) return { ok: false, violation: err };
|
|
759
|
+
const tsErr = message as import("./types.js").Draft13TrackStatusError;
|
|
760
|
+
const idErr = this.checkKnownRequestId(tsErr.request_id, message.type);
|
|
761
|
+
if (idErr) return { ok: false, violation: idErr };
|
|
762
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
// ─── Subscribe namespace lifecycle ───
|
|
766
|
+
|
|
767
|
+
private handleSubscribeNamespace(
|
|
768
|
+
message: Draft13Message,
|
|
769
|
+
sideEffects: SideEffect[],
|
|
770
|
+
): TransitionResult<Draft13MessageType> {
|
|
771
|
+
const err = this.requireReady(message.type);
|
|
772
|
+
if (err) return { ok: false, violation: err };
|
|
773
|
+
const sn = message as import("./types.js").Draft13SubscribeNamespace;
|
|
774
|
+
const dupErr = this.checkDuplicateRequestId(sn.request_id, message.type);
|
|
775
|
+
if (dupErr) return { ok: false, violation: dupErr };
|
|
776
|
+
this._requestIds.add(sn.request_id);
|
|
777
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
private handleSubscribeNamespaceOk(
|
|
781
|
+
message: Draft13Message,
|
|
782
|
+
sideEffects: SideEffect[],
|
|
783
|
+
): TransitionResult<Draft13MessageType> {
|
|
784
|
+
const err = this.requireReady(message.type);
|
|
785
|
+
if (err) return { ok: false, violation: err };
|
|
786
|
+
const ok = message as import("./types.js").Draft13SubscribeNamespaceOk;
|
|
787
|
+
const idErr = this.checkKnownRequestId(ok.request_id, message.type);
|
|
788
|
+
if (idErr) return { ok: false, violation: idErr };
|
|
789
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
private handleSubscribeNamespaceError(
|
|
793
|
+
message: Draft13Message,
|
|
794
|
+
sideEffects: SideEffect[],
|
|
795
|
+
): TransitionResult<Draft13MessageType> {
|
|
796
|
+
const err = this.requireReady(message.type);
|
|
797
|
+
if (err) return { ok: false, violation: err };
|
|
798
|
+
const snErr = message as import("./types.js").Draft13SubscribeNamespaceError;
|
|
799
|
+
const idErr = this.checkKnownRequestId(snErr.request_id, message.type);
|
|
800
|
+
if (idErr) return { ok: false, violation: idErr };
|
|
801
|
+
return { ok: true, phase: this._phase, sideEffects };
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
// ─── Generic ready-phase handler ───
|
|
805
|
+
|
|
806
|
+
private handleReadyPhaseMessage(message: Draft13Message): TransitionResult<Draft13MessageType> {
|
|
807
|
+
const err = this.requireReady(message.type);
|
|
808
|
+
if (err) return { ok: false, violation: err };
|
|
809
|
+
return { ok: true, phase: this._phase, sideEffects: [] };
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
reset(): void {
|
|
813
|
+
this._phase = "idle";
|
|
814
|
+
this._subscriptions.clear();
|
|
815
|
+
this._announces.clear();
|
|
816
|
+
this._fetches.clear();
|
|
817
|
+
this._requestIds.clear();
|
|
818
|
+
}
|
|
819
|
+
}
|