@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.
Files changed (277) hide show
  1. package/README.md +99 -95
  2. package/dist/chunk-4RIXXEII.js +1275 -0
  3. package/dist/chunk-4XYGE53S.cjs +698 -0
  4. package/dist/chunk-4YJANAXU.cjs +1109 -0
  5. package/dist/chunk-6AEHWULA.cjs +641 -0
  6. package/dist/chunk-7DUBLRXC.js +680 -0
  7. package/dist/{chunk-WNTXF3DE.cjs → chunk-7IVGHMKJ.cjs} +164 -62
  8. package/dist/{chunk-YBSEOSSP.js → chunk-A27S7HW7.js} +5 -1
  9. package/dist/chunk-BISI45MN.cjs +680 -0
  10. package/dist/{chunk-3BSZ55L3.cjs → chunk-BPNL5YFQ.cjs} +158 -24
  11. package/dist/chunk-CXDHOMHG.js +1097 -0
  12. package/dist/chunk-DUBCL3WF.cjs +749 -0
  13. package/dist/chunk-DWK5ZQZ4.js +642 -0
  14. package/dist/chunk-E6E3NQYU.js +680 -0
  15. package/dist/chunk-EFM5T7OM.js +698 -0
  16. package/dist/chunk-ENURAVHI.cjs +680 -0
  17. package/dist/{chunk-5WFXFLL4.cjs → chunk-FUFTMAQD.cjs} +96 -63
  18. package/dist/{chunk-2NARXGVA.cjs → chunk-FWISIR26.cjs} +5 -1
  19. package/dist/{chunk-YPXLV5YK.js → chunk-FXZ2MYKJ.js} +376 -38
  20. package/dist/chunk-G26SJ6XS.cjs +1341 -0
  21. package/dist/chunk-G7GI7LJA.js +737 -0
  22. package/dist/chunk-GXEW4COZ.cjs +737 -0
  23. package/dist/chunk-HSVYF6XX.cjs +1361 -0
  24. package/dist/chunk-IBVM4DMJ.cjs +1097 -0
  25. package/dist/chunk-IV2H5CFI.cjs +1275 -0
  26. package/dist/chunk-IV2HRJVT.js +1198 -0
  27. package/dist/chunk-JSQM2MG3.js +680 -0
  28. package/dist/chunk-K4OLITS2.cjs +1055 -0
  29. package/dist/{chunk-UOBWHJA5.js → chunk-KFTCU2P6.js} +2 -3
  30. package/dist/chunk-LH4NTURO.js +1361 -0
  31. package/dist/{chunk-DC4L6ZIT.js → chunk-MFAP7R6L.js} +154 -20
  32. package/dist/chunk-NGVE2RZT.js +1097 -0
  33. package/dist/chunk-NUX5BHWO.js +1341 -0
  34. package/dist/chunk-PJRA2TQ5.js +1055 -0
  35. package/dist/chunk-RVJAGE4S.cjs +1198 -0
  36. package/dist/{chunk-QYG6KGOV.cjs → chunk-RWQ43Z4F.cjs} +2 -3
  37. package/dist/chunk-RZHAPEXO.js +749 -0
  38. package/dist/chunk-ST24APEO.js +1109 -0
  39. package/dist/chunk-SYHW3FLI.cjs +642 -0
  40. package/dist/chunk-TLYNOOQP.cjs +432 -0
  41. package/dist/{chunk-23YG7F46.js → chunk-TMNGRIPL.js} +153 -51
  42. package/dist/{chunk-IQPDRQVC.js → chunk-U2B3B42P.js} +62 -29
  43. package/dist/chunk-UNS34PTA.cjs +680 -0
  44. package/dist/chunk-UR6JKS56.js +432 -0
  45. package/dist/{chunk-GDRGWFEK.cjs → chunk-UYXTY6ZQ.cjs} +376 -38
  46. package/dist/chunk-XUUCOLWU.cjs +1097 -0
  47. package/dist/chunk-YG5KJESI.js +641 -0
  48. package/dist/chunk-ZBKE2QRQ.js +1401 -0
  49. package/dist/chunk-ZSPO2GF2.cjs +1401 -0
  50. package/dist/codec-95k8CAu5.d.cts +35 -0
  51. package/dist/codec-AFuOxfsO.d.ts +60 -0
  52. package/dist/codec-B-UJ5Iow.d.cts +75 -0
  53. package/dist/codec-BC5jfvMb.d.ts +35 -0
  54. package/dist/codec-BECYPfY8.d.ts +35 -0
  55. package/dist/codec-BsPU1vNC.d.ts +39 -0
  56. package/dist/codec-BvpuF-6u.d.cts +39 -0
  57. package/dist/codec-C8jZI5Cx.d.cts +39 -0
  58. package/dist/codec-CAevkgf5.d.cts +33 -0
  59. package/dist/codec-CSUqCrRs.d.ts +39 -0
  60. package/dist/codec-C_HMXNK_.d.ts +33 -0
  61. package/dist/{codec-CTvFtQQI.d.cts → codec-CpuvYTSV.d.cts} +5 -5
  62. package/dist/codec-D0x8-SCw.d.cts +35 -0
  63. package/dist/codec-D7ARhpG1.d.ts +75 -0
  64. package/dist/codec-DNAUGshO.d.cts +60 -0
  65. package/dist/codec-DPx_QNn0.d.ts +31 -0
  66. package/dist/{codec-qPzfmLNu.d.ts → codec-DRhCx_hw.d.ts} +5 -5
  67. package/dist/codec-Db7YPe3l.d.ts +31 -0
  68. package/dist/codec-axkJpb7D.d.cts +31 -0
  69. package/dist/codec-ujAbFaep.d.cts +31 -0
  70. package/dist/draft10-session.cjs +6 -0
  71. package/dist/draft10-session.d.cts +8 -0
  72. package/dist/draft10-session.d.ts +8 -0
  73. package/dist/draft10-session.js +6 -0
  74. package/dist/draft10.cjs +115 -0
  75. package/dist/draft10.d.cts +95 -0
  76. package/dist/draft10.d.ts +95 -0
  77. package/dist/draft10.js +115 -0
  78. package/dist/draft11-session.cjs +6 -0
  79. package/dist/draft11-session.d.cts +8 -0
  80. package/dist/draft11-session.d.ts +8 -0
  81. package/dist/draft11-session.js +6 -0
  82. package/dist/draft11.cjs +109 -0
  83. package/dist/draft11.d.cts +99 -0
  84. package/dist/draft11.d.ts +99 -0
  85. package/dist/draft11.js +109 -0
  86. package/dist/draft12-session.cjs +6 -0
  87. package/dist/draft12-session.d.cts +8 -0
  88. package/dist/draft12-session.d.ts +8 -0
  89. package/dist/draft12-session.js +6 -0
  90. package/dist/draft12.cjs +117 -0
  91. package/dist/draft12.d.cts +106 -0
  92. package/dist/draft12.d.ts +106 -0
  93. package/dist/draft12.js +117 -0
  94. package/dist/draft13-session.cjs +6 -0
  95. package/dist/draft13-session.d.cts +8 -0
  96. package/dist/draft13-session.d.ts +8 -0
  97. package/dist/draft13-session.js +6 -0
  98. package/dist/draft13.cjs +119 -0
  99. package/dist/draft13.d.cts +108 -0
  100. package/dist/draft13.d.ts +108 -0
  101. package/dist/draft13.js +119 -0
  102. package/dist/draft14-session.cjs +2 -2
  103. package/dist/draft14-session.d.cts +4 -4
  104. package/dist/draft14-session.d.ts +4 -4
  105. package/dist/draft14-session.js +1 -1
  106. package/dist/draft14.cjs +4 -4
  107. package/dist/draft14.d.cts +27 -15
  108. package/dist/draft14.d.ts +27 -15
  109. package/dist/draft14.js +3 -3
  110. package/dist/draft15-session.cjs +6 -0
  111. package/dist/draft15-session.d.cts +8 -0
  112. package/dist/draft15-session.d.ts +8 -0
  113. package/dist/draft15-session.js +6 -0
  114. package/dist/draft15.cjs +111 -0
  115. package/dist/draft15.d.cts +93 -0
  116. package/dist/draft15.d.ts +93 -0
  117. package/dist/draft15.js +111 -0
  118. package/dist/draft16-session.cjs +6 -0
  119. package/dist/draft16-session.d.cts +8 -0
  120. package/dist/draft16-session.d.ts +8 -0
  121. package/dist/draft16-session.js +6 -0
  122. package/dist/draft16.cjs +113 -0
  123. package/dist/draft16.d.cts +94 -0
  124. package/dist/draft16.d.ts +94 -0
  125. package/dist/draft16.js +113 -0
  126. package/dist/draft17-session.cjs +8 -0
  127. package/dist/draft17-session.d.cts +51 -0
  128. package/dist/draft17-session.d.ts +51 -0
  129. package/dist/draft17-session.js +8 -0
  130. package/dist/draft17.cjs +99 -0
  131. package/dist/draft17.d.cts +40 -0
  132. package/dist/draft17.d.ts +40 -0
  133. package/dist/draft17.js +99 -0
  134. package/dist/draft7-session.cjs +3 -3
  135. package/dist/draft7-session.d.cts +3 -3
  136. package/dist/draft7-session.d.ts +3 -3
  137. package/dist/draft7-session.js +2 -2
  138. package/dist/draft7.cjs +6 -6
  139. package/dist/draft7.d.cts +10 -10
  140. package/dist/draft7.d.ts +10 -10
  141. package/dist/draft7.js +3 -3
  142. package/dist/draft8-session.cjs +6 -0
  143. package/dist/draft8-session.d.cts +8 -0
  144. package/dist/draft8-session.d.ts +8 -0
  145. package/dist/draft8-session.js +6 -0
  146. package/dist/draft8.cjs +115 -0
  147. package/dist/draft8.d.cts +95 -0
  148. package/dist/draft8.d.ts +95 -0
  149. package/dist/draft8.js +115 -0
  150. package/dist/draft9-session.cjs +6 -0
  151. package/dist/draft9-session.d.cts +8 -0
  152. package/dist/draft9-session.d.ts +8 -0
  153. package/dist/draft9-session.js +6 -0
  154. package/dist/draft9.cjs +115 -0
  155. package/dist/draft9.d.cts +95 -0
  156. package/dist/draft9.d.ts +95 -0
  157. package/dist/draft9.js +115 -0
  158. package/dist/index.cjs +79 -7
  159. package/dist/index.d.cts +71 -8
  160. package/dist/index.d.ts +71 -8
  161. package/dist/index.js +77 -5
  162. package/dist/{session-types-B9NIf7_F.d.ts → session-types-CJIFbTPd.d.ts} +20 -20
  163. package/dist/{session-types-CCo-oA-d.d.cts → session-types-Cbq8IGCP.d.cts} +20 -20
  164. package/dist/session.cjs +5 -5
  165. package/dist/session.d.cts +3 -3
  166. package/dist/session.d.ts +3 -3
  167. package/dist/session.js +5 -5
  168. package/dist/types-4VxSL2Ho.d.cts +261 -0
  169. package/dist/types-4VxSL2Ho.d.ts +261 -0
  170. package/dist/types-B2afJZM-.d.cts +236 -0
  171. package/dist/types-B2afJZM-.d.ts +236 -0
  172. package/dist/{types-CIk5W10V.d.ts → types-BTFeKYCb.d.cts} +37 -37
  173. package/dist/{types-CIk5W10V.d.cts → types-BTFeKYCb.d.ts} +37 -37
  174. package/dist/types-Bg6QYNVt.d.cts +290 -0
  175. package/dist/types-Bg6QYNVt.d.ts +290 -0
  176. package/dist/types-C_1HrqBl.d.cts +306 -0
  177. package/dist/types-C_1HrqBl.d.ts +306 -0
  178. package/dist/types-Cw4WE9dh.d.cts +261 -0
  179. package/dist/types-Cw4WE9dh.d.ts +261 -0
  180. package/dist/types-D5gNQiDj.d.cts +261 -0
  181. package/dist/types-D5gNQiDj.d.ts +261 -0
  182. package/dist/types-DqCDFqgB.d.cts +230 -0
  183. package/dist/types-DqCDFqgB.d.ts +230 -0
  184. package/dist/types-ERexTpT8.d.cts +217 -0
  185. package/dist/types-ERexTpT8.d.ts +217 -0
  186. package/dist/{types-ClXELFGN.d.cts → types-QNXoxC9Y.d.cts} +36 -41
  187. package/dist/{types-ClXELFGN.d.ts → types-QNXoxC9Y.d.ts} +36 -41
  188. package/dist/types-r-CasCf1.d.cts +262 -0
  189. package/dist/types-r-CasCf1.d.ts +262 -0
  190. package/package.json +116 -8
  191. package/src/core/buffer-reader.ts +16 -9
  192. package/src/core/buffer-writer.ts +2 -2
  193. package/src/core/errors.ts +1 -1
  194. package/src/core/session-types.ts +28 -41
  195. package/src/core/types.ts +92 -75
  196. package/src/drafts/draft07/announce-fsm.ts +1 -1
  197. package/src/drafts/draft07/codec.ts +235 -118
  198. package/src/drafts/draft07/index.ts +43 -44
  199. package/src/drafts/draft07/messages.ts +1 -1
  200. package/src/drafts/draft07/parameters.ts +2 -2
  201. package/src/drafts/draft07/rules.ts +67 -38
  202. package/src/drafts/draft07/session-fsm.ts +330 -117
  203. package/src/drafts/draft07/session.ts +10 -10
  204. package/src/drafts/draft07/subscription-fsm.ts +1 -1
  205. package/src/drafts/draft07/varint.ts +4 -4
  206. package/src/drafts/draft08/codec.ts +1254 -0
  207. package/src/drafts/draft08/index.ts +125 -0
  208. package/src/drafts/draft08/messages.ts +72 -0
  209. package/src/drafts/draft08/rules.ts +91 -0
  210. package/src/drafts/draft08/session-fsm.ts +718 -0
  211. package/src/drafts/draft08/session.ts +26 -0
  212. package/src/drafts/draft08/types.ts +377 -0
  213. package/src/drafts/draft09/codec.ts +1235 -0
  214. package/src/drafts/draft09/index.ts +125 -0
  215. package/src/drafts/draft09/messages.ts +72 -0
  216. package/src/drafts/draft09/rules.ts +91 -0
  217. package/src/drafts/draft09/session-fsm.ts +718 -0
  218. package/src/drafts/draft09/session.ts +26 -0
  219. package/src/drafts/draft09/types.ts +376 -0
  220. package/src/drafts/draft10/codec.ts +1235 -0
  221. package/src/drafts/draft10/index.ts +125 -0
  222. package/src/drafts/draft10/messages.ts +72 -0
  223. package/src/drafts/draft10/rules.ts +91 -0
  224. package/src/drafts/draft10/session-fsm.ts +718 -0
  225. package/src/drafts/draft10/session.ts +26 -0
  226. package/src/drafts/draft10/types.ts +376 -0
  227. package/src/drafts/draft11/codec.ts +1198 -0
  228. package/src/drafts/draft11/index.ts +123 -0
  229. package/src/drafts/draft11/messages.ts +71 -0
  230. package/src/drafts/draft11/rules.ts +100 -0
  231. package/src/drafts/draft11/session-fsm.ts +758 -0
  232. package/src/drafts/draft11/session.ts +26 -0
  233. package/src/drafts/draft11/types.ts +375 -0
  234. package/src/drafts/draft12/codec.ts +1354 -0
  235. package/src/drafts/draft12/index.ts +130 -0
  236. package/src/drafts/draft12/messages.ts +84 -0
  237. package/src/drafts/draft12/rules.ts +106 -0
  238. package/src/drafts/draft12/session-fsm.ts +805 -0
  239. package/src/drafts/draft12/session.ts +26 -0
  240. package/src/drafts/draft12/types.ts +414 -0
  241. package/src/drafts/draft13/codec.ts +1438 -0
  242. package/src/drafts/draft13/index.ts +132 -0
  243. package/src/drafts/draft13/messages.ts +86 -0
  244. package/src/drafts/draft13/rules.ts +108 -0
  245. package/src/drafts/draft13/session-fsm.ts +819 -0
  246. package/src/drafts/draft13/session.ts +26 -0
  247. package/src/drafts/draft13/types.ts +433 -0
  248. package/src/drafts/draft14/codec.ts +339 -189
  249. package/src/drafts/draft14/index.ts +103 -108
  250. package/src/drafts/draft14/messages.ts +61 -61
  251. package/src/drafts/draft14/rules.ts +77 -34
  252. package/src/drafts/draft14/session-fsm.ts +640 -147
  253. package/src/drafts/draft14/session.ts +13 -13
  254. package/src/drafts/draft14/types.ts +68 -68
  255. package/src/drafts/draft15/codec.ts +1661 -0
  256. package/src/drafts/draft15/index.ts +121 -0
  257. package/src/drafts/draft15/messages.ts +64 -0
  258. package/src/drafts/draft15/rules.ts +95 -0
  259. package/src/drafts/draft15/session-fsm.ts +687 -0
  260. package/src/drafts/draft15/session.ts +26 -0
  261. package/src/drafts/draft15/types.ts +336 -0
  262. package/src/drafts/draft16/codec.ts +1623 -0
  263. package/src/drafts/draft16/index.ts +123 -0
  264. package/src/drafts/draft16/messages.ts +67 -0
  265. package/src/drafts/draft16/rules.ts +96 -0
  266. package/src/drafts/draft16/session-fsm.ts +682 -0
  267. package/src/drafts/draft16/session.ts +26 -0
  268. package/src/drafts/draft16/types.ts +354 -0
  269. package/src/drafts/draft17/codec.ts +1621 -0
  270. package/src/drafts/draft17/index.ts +105 -0
  271. package/src/drafts/draft17/messages.ts +53 -0
  272. package/src/drafts/draft17/rules.ts +85 -0
  273. package/src/drafts/draft17/session-fsm.ts +437 -0
  274. package/src/drafts/draft17/session.ts +15 -0
  275. package/src/drafts/draft17/types.ts +310 -0
  276. package/src/index.ts +283 -33
  277. package/src/session.ts +20 -20
@@ -0,0 +1,718 @@
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 { Draft10Message, Draft10MessageType } from "./types.js";
18
+
19
+ function violation(
20
+ code: ProtocolViolation<Draft10MessageType>["code"],
21
+ message: string,
22
+ currentPhase: SessionPhase,
23
+ offendingMessage: Draft10MessageType,
24
+ ): ProtocolViolation<Draft10MessageType> {
25
+ return { code, message, currentPhase, offendingMessage };
26
+ }
27
+
28
+ export class Draft10SessionFSM {
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 _subscribeIds = 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<Draft10MessageType> {
57
+ return getLegalOutgoing(this._phase, this._role);
58
+ }
59
+
60
+ get legalIncoming(): ReadonlySet<Draft10MessageType> {
61
+ return getLegalIncoming(this._phase, this._role);
62
+ }
63
+
64
+ private checkRole(
65
+ message: Draft10Message,
66
+ direction: "inbound" | "outbound",
67
+ ): ProtocolViolation<Draft10MessageType> | 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 checkDuplicateSubscribeId(
91
+ subscribeId: bigint,
92
+ msgType: Draft10MessageType,
93
+ ): ProtocolViolation<Draft10MessageType> | null {
94
+ if (this._subscribeIds.has(subscribeId)) {
95
+ return violation(
96
+ "DUPLICATE_REQUEST_ID",
97
+ `Subscribe ID ${subscribeId} already in use`,
98
+ this._phase,
99
+ msgType,
100
+ );
101
+ }
102
+ return null;
103
+ }
104
+
105
+ private checkKnownSubscribeId(
106
+ subscribeId: bigint,
107
+ msgType: Draft10MessageType,
108
+ ): ProtocolViolation<Draft10MessageType> | null {
109
+ if (!this._subscribeIds.has(subscribeId)) {
110
+ return violation(
111
+ "UNKNOWN_REQUEST_ID",
112
+ `No request with subscribe ID ${subscribeId}`,
113
+ this._phase,
114
+ msgType,
115
+ );
116
+ }
117
+ return null;
118
+ }
119
+
120
+ validateOutgoing(message: Draft10Message): ValidationResult<Draft10MessageType> {
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: Draft10Message): TransitionResult<Draft10MessageType> {
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: Draft10Message): TransitionResult<Draft10MessageType> {
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: Draft10Message,
154
+ direction: "inbound" | "outbound",
155
+ ): TransitionResult<Draft10MessageType> {
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
+ // Fetch lifecycle
193
+ case "fetch":
194
+ return this.handleFetch(message, sideEffects);
195
+ case "fetch_ok":
196
+ return this.handleFetchOk(message, sideEffects);
197
+ case "fetch_error":
198
+ return this.handleFetchError(message, sideEffects);
199
+ case "fetch_cancel":
200
+ return this.handleFetchCancel(message, sideEffects);
201
+ default:
202
+ return this.handleReadyPhaseMessage(message);
203
+ }
204
+ }
205
+
206
+ private handleClientSetup(
207
+ direction: "inbound" | "outbound",
208
+ ): TransitionResult<Draft10MessageType> {
209
+ if (this._phase !== "idle") {
210
+ return {
211
+ ok: false,
212
+ violation: violation(
213
+ "SETUP_VIOLATION",
214
+ "CLIENT_SETUP already sent/received",
215
+ this._phase,
216
+ "client_setup",
217
+ ),
218
+ };
219
+ }
220
+ if (direction === "outbound" && this._role !== "client") {
221
+ return {
222
+ ok: false,
223
+ violation: violation(
224
+ "ROLE_VIOLATION",
225
+ "Only client can send CLIENT_SETUP",
226
+ this._phase,
227
+ "client_setup",
228
+ ),
229
+ };
230
+ }
231
+ this._phase = "setup";
232
+ return { ok: true, phase: this._phase, sideEffects: [] };
233
+ }
234
+
235
+ private handleServerSetup(
236
+ direction: "inbound" | "outbound",
237
+ ): TransitionResult<Draft10MessageType> {
238
+ if (this._phase !== "setup") {
239
+ return {
240
+ ok: false,
241
+ violation: violation(
242
+ "SETUP_VIOLATION",
243
+ "SERVER_SETUP before CLIENT_SETUP",
244
+ this._phase,
245
+ "server_setup",
246
+ ),
247
+ };
248
+ }
249
+ if (direction === "outbound" && this._role !== "server") {
250
+ return {
251
+ ok: false,
252
+ violation: violation(
253
+ "ROLE_VIOLATION",
254
+ "Only server can send SERVER_SETUP",
255
+ this._phase,
256
+ "server_setup",
257
+ ),
258
+ };
259
+ }
260
+ this._phase = "ready";
261
+ return { ok: true, phase: this._phase, sideEffects: [{ type: "session-ready" }] };
262
+ }
263
+
264
+ private handleGoAway(
265
+ message: Draft10Message,
266
+ sideEffects: SideEffect[],
267
+ ): TransitionResult<Draft10MessageType> {
268
+ if (this._phase !== "ready" && this._phase !== "draining") {
269
+ return {
270
+ ok: false,
271
+ violation: violation(
272
+ "UNEXPECTED_MESSAGE",
273
+ `GOAWAY not valid in phase ${this._phase}`,
274
+ this._phase,
275
+ "goaway",
276
+ ),
277
+ };
278
+ }
279
+ this._phase = "draining";
280
+ const goaway = message as import("./types.js").Draft10GoAway;
281
+ sideEffects.push({ type: "session-draining", goAwayUri: goaway.new_session_uri });
282
+ return { ok: true, phase: this._phase, sideEffects };
283
+ }
284
+
285
+ private requireReady(msgType: Draft10MessageType): ProtocolViolation<Draft10MessageType> | null {
286
+ if (this._phase !== "ready" && this._phase !== "draining") {
287
+ return violation(
288
+ this._phase === "idle" || this._phase === "setup"
289
+ ? "MESSAGE_BEFORE_SETUP"
290
+ : "UNEXPECTED_MESSAGE",
291
+ `${msgType} requires ready phase, current: ${this._phase}`,
292
+ this._phase,
293
+ msgType,
294
+ );
295
+ }
296
+ return null;
297
+ }
298
+
299
+ // ─── Subscribe lifecycle ───
300
+
301
+ private handleSubscribe(
302
+ message: Draft10Message,
303
+ sideEffects: SideEffect[],
304
+ ): TransitionResult<Draft10MessageType> {
305
+ const err = this.requireReady(message.type);
306
+ if (err) return { ok: false, violation: err };
307
+ const sub = message as import("./types.js").Draft10Subscribe;
308
+ const dupErr = this.checkDuplicateSubscribeId(sub.subscribe_id, message.type);
309
+ if (dupErr) return { ok: false, violation: dupErr };
310
+ this._subscribeIds.add(sub.subscribe_id);
311
+ this._subscriptions.set(sub.subscribe_id, {
312
+ subscribeId: sub.subscribe_id,
313
+ phase: "pending",
314
+ trackNamespace: sub.track_namespace,
315
+ trackName: sub.track_name,
316
+ });
317
+ return { ok: true, phase: this._phase, sideEffects };
318
+ }
319
+
320
+ private handleSubscribeOk(
321
+ message: Draft10Message,
322
+ sideEffects: SideEffect[],
323
+ ): TransitionResult<Draft10MessageType> {
324
+ const err = this.requireReady(message.type);
325
+ if (err) return { ok: false, violation: err };
326
+ const ok = message as import("./types.js").Draft10SubscribeOk;
327
+ const idErr = this.checkKnownSubscribeId(ok.subscribe_id, message.type);
328
+ if (idErr) return { ok: false, violation: idErr };
329
+ const existing = this._subscriptions.get(ok.subscribe_id);
330
+ if (!existing)
331
+ return {
332
+ ok: false,
333
+ violation: violation(
334
+ "UNKNOWN_REQUEST_ID",
335
+ `No subscription with subscribe ID ${ok.subscribe_id}`,
336
+ this._phase,
337
+ message.type,
338
+ ),
339
+ };
340
+ if (existing.phase !== "pending")
341
+ return {
342
+ ok: false,
343
+ violation: violation(
344
+ "STATE_VIOLATION",
345
+ `Subscription ${ok.subscribe_id} is ${existing.phase}, not pending`,
346
+ this._phase,
347
+ message.type,
348
+ ),
349
+ };
350
+ this._subscriptions.set(ok.subscribe_id, { ...existing, phase: "active" });
351
+ sideEffects.push({ type: "subscription-activated", subscribeId: ok.subscribe_id });
352
+ return { ok: true, phase: this._phase, sideEffects };
353
+ }
354
+
355
+ private handleSubscribeError(
356
+ message: Draft10Message,
357
+ sideEffects: SideEffect[],
358
+ ): TransitionResult<Draft10MessageType> {
359
+ const err = this.requireReady(message.type);
360
+ if (err) return { ok: false, violation: err };
361
+ const subErr = message as import("./types.js").Draft10SubscribeError;
362
+ const idErr = this.checkKnownSubscribeId(subErr.subscribe_id, message.type);
363
+ if (idErr) return { ok: false, violation: idErr };
364
+ const existing = this._subscriptions.get(subErr.subscribe_id);
365
+ if (!existing)
366
+ return {
367
+ ok: false,
368
+ violation: violation(
369
+ "UNKNOWN_REQUEST_ID",
370
+ `No subscription with subscribe ID ${subErr.subscribe_id}`,
371
+ this._phase,
372
+ message.type,
373
+ ),
374
+ };
375
+ if (existing.phase !== "pending")
376
+ return {
377
+ ok: false,
378
+ violation: violation(
379
+ "STATE_VIOLATION",
380
+ `Subscription ${subErr.subscribe_id} is ${existing.phase}, not pending`,
381
+ this._phase,
382
+ message.type,
383
+ ),
384
+ };
385
+ this._subscriptions.set(subErr.subscribe_id, { ...existing, phase: "error" });
386
+ sideEffects.push({
387
+ type: "subscription-ended",
388
+ subscribeId: subErr.subscribe_id,
389
+ reason: subErr.reason_phrase,
390
+ });
391
+ return { ok: true, phase: this._phase, sideEffects };
392
+ }
393
+
394
+ private handleSubscribeUpdate(
395
+ message: Draft10Message,
396
+ sideEffects: SideEffect[],
397
+ ): TransitionResult<Draft10MessageType> {
398
+ const err = this.requireReady(message.type);
399
+ if (err) return { ok: false, violation: err };
400
+ const update = message as import("./types.js").Draft10SubscribeUpdate;
401
+ const idErr = this.checkKnownSubscribeId(update.subscribe_id, message.type);
402
+ if (idErr) return { ok: false, violation: idErr };
403
+ const existing = this._subscriptions.get(update.subscribe_id);
404
+ if (!existing)
405
+ return {
406
+ ok: false,
407
+ violation: violation(
408
+ "UNKNOWN_REQUEST_ID",
409
+ `No subscription with subscribe ID ${update.subscribe_id}`,
410
+ this._phase,
411
+ message.type,
412
+ ),
413
+ };
414
+ if (existing.phase !== "active")
415
+ return {
416
+ ok: false,
417
+ violation: violation(
418
+ "STATE_VIOLATION",
419
+ `Subscription ${update.subscribe_id} is ${existing.phase}, not active`,
420
+ this._phase,
421
+ message.type,
422
+ ),
423
+ };
424
+ return { ok: true, phase: this._phase, sideEffects };
425
+ }
426
+
427
+ private handleSubscribeDone(
428
+ message: Draft10Message,
429
+ sideEffects: SideEffect[],
430
+ ): TransitionResult<Draft10MessageType> {
431
+ const err = this.requireReady(message.type);
432
+ if (err) return { ok: false, violation: err };
433
+ const done = message as import("./types.js").Draft10SubscribeDone;
434
+ const idErr = this.checkKnownSubscribeId(done.subscribe_id, message.type);
435
+ if (idErr) return { ok: false, violation: idErr };
436
+ const existing = this._subscriptions.get(done.subscribe_id);
437
+ if (!existing)
438
+ return {
439
+ ok: false,
440
+ violation: violation(
441
+ "UNKNOWN_REQUEST_ID",
442
+ `No subscription with subscribe ID ${done.subscribe_id}`,
443
+ this._phase,
444
+ message.type,
445
+ ),
446
+ };
447
+ this._subscriptions.set(done.subscribe_id, { ...existing, phase: "done" });
448
+ sideEffects.push({
449
+ type: "subscription-ended",
450
+ subscribeId: done.subscribe_id,
451
+ reason: done.reason_phrase,
452
+ });
453
+ return { ok: true, phase: this._phase, sideEffects };
454
+ }
455
+
456
+ private handleUnsubscribe(
457
+ message: Draft10Message,
458
+ sideEffects: SideEffect[],
459
+ ): TransitionResult<Draft10MessageType> {
460
+ const err = this.requireReady(message.type);
461
+ if (err) return { ok: false, violation: err };
462
+ const unsub = message as import("./types.js").Draft10Unsubscribe;
463
+ const idErr = this.checkKnownSubscribeId(unsub.subscribe_id, message.type);
464
+ if (idErr) return { ok: false, violation: idErr };
465
+ const existing = this._subscriptions.get(unsub.subscribe_id);
466
+ if (!existing)
467
+ return {
468
+ ok: false,
469
+ violation: violation(
470
+ "UNKNOWN_REQUEST_ID",
471
+ `No subscription with subscribe ID ${unsub.subscribe_id}`,
472
+ this._phase,
473
+ message.type,
474
+ ),
475
+ };
476
+ this._subscriptions.set(unsub.subscribe_id, { ...existing, phase: "done" });
477
+ sideEffects.push({
478
+ type: "subscription-ended",
479
+ subscribeId: unsub.subscribe_id,
480
+ reason: "unsubscribed",
481
+ });
482
+ return { ok: true, phase: this._phase, sideEffects };
483
+ }
484
+
485
+ // ─── Announce lifecycle ───
486
+
487
+ private handleAnnounce(
488
+ message: Draft10Message,
489
+ sideEffects: SideEffect[],
490
+ ): TransitionResult<Draft10MessageType> {
491
+ const err = this.requireReady(message.type);
492
+ if (err) return { ok: false, violation: err };
493
+ const ann = message as import("./types.js").Draft10Announce;
494
+ const nsKey = ann.track_namespace.join("/");
495
+ this._announces.set(nsKey, { namespace: ann.track_namespace, phase: "pending" });
496
+ return { ok: true, phase: this._phase, sideEffects };
497
+ }
498
+
499
+ private handleAnnounceOk(
500
+ message: Draft10Message,
501
+ sideEffects: SideEffect[],
502
+ ): TransitionResult<Draft10MessageType> {
503
+ const err = this.requireReady(message.type);
504
+ if (err) return { ok: false, violation: err };
505
+ const ok = message as import("./types.js").Draft10AnnounceOk;
506
+ const nsKey = ok.track_namespace.join("/");
507
+ const existing = this._announces.get(nsKey);
508
+ if (!existing)
509
+ return {
510
+ ok: false,
511
+ violation: violation(
512
+ "UNKNOWN_REQUEST_ID",
513
+ `No announce for namespace ${nsKey}`,
514
+ this._phase,
515
+ message.type,
516
+ ),
517
+ };
518
+ if (existing.phase !== "pending")
519
+ return {
520
+ ok: false,
521
+ violation: violation(
522
+ "STATE_VIOLATION",
523
+ `Announce ${nsKey} is ${existing.phase}, not pending`,
524
+ this._phase,
525
+ message.type,
526
+ ),
527
+ };
528
+ this._announces.set(nsKey, { ...existing, phase: "active" });
529
+ return { ok: true, phase: this._phase, sideEffects };
530
+ }
531
+
532
+ private handleAnnounceError(
533
+ message: Draft10Message,
534
+ sideEffects: SideEffect[],
535
+ ): TransitionResult<Draft10MessageType> {
536
+ const err = this.requireReady(message.type);
537
+ if (err) return { ok: false, violation: err };
538
+ const annErr = message as import("./types.js").Draft10AnnounceError;
539
+ const nsKey = annErr.track_namespace.join("/");
540
+ const existing = this._announces.get(nsKey);
541
+ if (!existing)
542
+ return {
543
+ ok: false,
544
+ violation: violation(
545
+ "UNKNOWN_REQUEST_ID",
546
+ `No announce for namespace ${nsKey}`,
547
+ this._phase,
548
+ message.type,
549
+ ),
550
+ };
551
+ if (existing.phase !== "pending")
552
+ return {
553
+ ok: false,
554
+ violation: violation(
555
+ "STATE_VIOLATION",
556
+ `Announce ${nsKey} is ${existing.phase}, not pending`,
557
+ this._phase,
558
+ message.type,
559
+ ),
560
+ };
561
+ this._announces.set(nsKey, { ...existing, phase: "error" });
562
+ sideEffects.push({ type: "announce-ended", namespace: annErr.track_namespace });
563
+ return { ok: true, phase: this._phase, sideEffects };
564
+ }
565
+
566
+ private handleUnannounce(
567
+ message: Draft10Message,
568
+ sideEffects: SideEffect[],
569
+ ): TransitionResult<Draft10MessageType> {
570
+ const err = this.requireReady(message.type);
571
+ if (err) return { ok: false, violation: err };
572
+ const unann = message as import("./types.js").Draft10Unannounce;
573
+ const nsKey = unann.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: unann.track_namespace });
578
+ }
579
+ return { ok: true, phase: this._phase, sideEffects };
580
+ }
581
+
582
+ private handleAnnounceCancel(
583
+ message: Draft10Message,
584
+ sideEffects: SideEffect[],
585
+ ): TransitionResult<Draft10MessageType> {
586
+ const err = this.requireReady(message.type);
587
+ if (err) return { ok: false, violation: err };
588
+ const cancel = message as import("./types.js").Draft10AnnounceCancel;
589
+ const nsKey = cancel.track_namespace.join("/");
590
+ const existing = this._announces.get(nsKey);
591
+ if (existing) {
592
+ this._announces.delete(nsKey);
593
+ sideEffects.push({ type: "announce-ended", namespace: cancel.track_namespace });
594
+ }
595
+ return { ok: true, phase: this._phase, sideEffects };
596
+ }
597
+
598
+ // ─── Fetch lifecycle ───
599
+
600
+ private handleFetch(
601
+ message: Draft10Message,
602
+ sideEffects: SideEffect[],
603
+ ): TransitionResult<Draft10MessageType> {
604
+ const err = this.requireReady(message.type);
605
+ if (err) return { ok: false, violation: err };
606
+ const fetch = message as import("./types.js").Draft10Fetch;
607
+ const dupErr = this.checkDuplicateSubscribeId(fetch.subscribe_id, message.type);
608
+ if (dupErr) return { ok: false, violation: dupErr };
609
+ this._subscribeIds.add(fetch.subscribe_id);
610
+ this._fetches.set(fetch.subscribe_id, { requestId: fetch.subscribe_id, phase: "pending" });
611
+ return { ok: true, phase: this._phase, sideEffects };
612
+ }
613
+
614
+ private handleFetchOk(
615
+ message: Draft10Message,
616
+ sideEffects: SideEffect[],
617
+ ): TransitionResult<Draft10MessageType> {
618
+ const err = this.requireReady(message.type);
619
+ if (err) return { ok: false, violation: err };
620
+ const ok = message as import("./types.js").Draft10FetchOk;
621
+ const idErr = this.checkKnownSubscribeId(ok.subscribe_id, message.type);
622
+ if (idErr) return { ok: false, violation: idErr };
623
+ const existing = this._fetches.get(ok.subscribe_id);
624
+ if (!existing)
625
+ return {
626
+ ok: false,
627
+ violation: violation(
628
+ "UNKNOWN_REQUEST_ID",
629
+ `No fetch with subscribe ID ${ok.subscribe_id}`,
630
+ this._phase,
631
+ message.type,
632
+ ),
633
+ };
634
+ if (existing.phase !== "pending")
635
+ return {
636
+ ok: false,
637
+ violation: violation(
638
+ "STATE_VIOLATION",
639
+ `Fetch ${ok.subscribe_id} is ${existing.phase}, not pending`,
640
+ this._phase,
641
+ message.type,
642
+ ),
643
+ };
644
+ this._fetches.set(ok.subscribe_id, { ...existing, phase: "active" });
645
+ sideEffects.push({ type: "fetch-activated", requestId: ok.subscribe_id });
646
+ return { ok: true, phase: this._phase, sideEffects };
647
+ }
648
+
649
+ private handleFetchError(
650
+ message: Draft10Message,
651
+ sideEffects: SideEffect[],
652
+ ): TransitionResult<Draft10MessageType> {
653
+ const err = this.requireReady(message.type);
654
+ if (err) return { ok: false, violation: err };
655
+ const fetchErr = message as import("./types.js").Draft10FetchError;
656
+ const idErr = this.checkKnownSubscribeId(fetchErr.subscribe_id, message.type);
657
+ if (idErr) return { ok: false, violation: idErr };
658
+ const existing = this._fetches.get(fetchErr.subscribe_id);
659
+ if (!existing)
660
+ return {
661
+ ok: false,
662
+ violation: violation(
663
+ "UNKNOWN_REQUEST_ID",
664
+ `No fetch with subscribe ID ${fetchErr.subscribe_id}`,
665
+ this._phase,
666
+ message.type,
667
+ ),
668
+ };
669
+ this._fetches.set(fetchErr.subscribe_id, { ...existing, phase: "error" });
670
+ sideEffects.push({
671
+ type: "fetch-ended",
672
+ requestId: fetchErr.subscribe_id,
673
+ reason: fetchErr.reason_phrase,
674
+ });
675
+ return { ok: true, phase: this._phase, sideEffects };
676
+ }
677
+
678
+ private handleFetchCancel(
679
+ message: Draft10Message,
680
+ sideEffects: SideEffect[],
681
+ ): TransitionResult<Draft10MessageType> {
682
+ const err = this.requireReady(message.type);
683
+ if (err) return { ok: false, violation: err };
684
+ const cancel = message as import("./types.js").Draft10FetchCancel;
685
+ const idErr = this.checkKnownSubscribeId(cancel.subscribe_id, message.type);
686
+ if (idErr) return { ok: false, violation: idErr };
687
+ const existing = this._fetches.get(cancel.subscribe_id);
688
+ if (!existing)
689
+ return {
690
+ ok: false,
691
+ violation: violation(
692
+ "UNKNOWN_REQUEST_ID",
693
+ `No fetch with subscribe ID ${cancel.subscribe_id}`,
694
+ this._phase,
695
+ message.type,
696
+ ),
697
+ };
698
+ this._fetches.set(cancel.subscribe_id, { ...existing, phase: "cancelled" });
699
+ sideEffects.push({ type: "fetch-ended", requestId: cancel.subscribe_id, reason: "cancelled" });
700
+ return { ok: true, phase: this._phase, sideEffects };
701
+ }
702
+
703
+ // ─── Generic ready-phase handler ───
704
+
705
+ private handleReadyPhaseMessage(message: Draft10Message): TransitionResult<Draft10MessageType> {
706
+ const err = this.requireReady(message.type);
707
+ if (err) return { ok: false, violation: err };
708
+ return { ok: true, phase: this._phase, sideEffects: [] };
709
+ }
710
+
711
+ reset(): void {
712
+ this._phase = "idle";
713
+ this._subscriptions.clear();
714
+ this._announces.clear();
715
+ this._fetches.clear();
716
+ this._subscribeIds.clear();
717
+ }
718
+ }