@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.
Files changed (252) 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-YBZD3DU5.cjs → chunk-7IVGHMKJ.cjs} +44 -42
  8. package/dist/chunk-BISI45MN.cjs +680 -0
  9. package/dist/{chunk-NLYTRGXA.cjs → chunk-BPNL5YFQ.cjs} +5 -5
  10. package/dist/chunk-CXDHOMHG.js +1097 -0
  11. package/dist/chunk-DUBCL3WF.cjs +749 -0
  12. package/dist/chunk-DWK5ZQZ4.js +642 -0
  13. package/dist/chunk-E6E3NQYU.js +680 -0
  14. package/dist/chunk-EFM5T7OM.js +698 -0
  15. package/dist/chunk-ENURAVHI.cjs +680 -0
  16. package/dist/{chunk-Z66WDWHI.js → chunk-FXZ2MYKJ.js} +127 -1
  17. package/dist/chunk-G26SJ6XS.cjs +1341 -0
  18. package/dist/chunk-G7GI7LJA.js +737 -0
  19. package/dist/chunk-GXEW4COZ.cjs +737 -0
  20. package/dist/chunk-HSVYF6XX.cjs +1361 -0
  21. package/dist/chunk-IBVM4DMJ.cjs +1097 -0
  22. package/dist/chunk-IV2H5CFI.cjs +1275 -0
  23. package/dist/chunk-IV2HRJVT.js +1198 -0
  24. package/dist/chunk-JSQM2MG3.js +680 -0
  25. package/dist/chunk-K4OLITS2.cjs +1055 -0
  26. package/dist/{chunk-UOBWHJA5.js → chunk-KFTCU2P6.js} +2 -3
  27. package/dist/chunk-LH4NTURO.js +1361 -0
  28. package/dist/{chunk-YTXLWKOR.js → chunk-MFAP7R6L.js} +1 -1
  29. package/dist/chunk-NGVE2RZT.js +1097 -0
  30. package/dist/chunk-NUX5BHWO.js +1341 -0
  31. package/dist/chunk-PJRA2TQ5.js +1055 -0
  32. package/dist/chunk-RVJAGE4S.cjs +1198 -0
  33. package/dist/{chunk-QYG6KGOV.cjs → chunk-RWQ43Z4F.cjs} +2 -3
  34. package/dist/chunk-RZHAPEXO.js +749 -0
  35. package/dist/chunk-ST24APEO.js +1109 -0
  36. package/dist/chunk-SYHW3FLI.cjs +642 -0
  37. package/dist/chunk-TLYNOOQP.cjs +432 -0
  38. package/dist/{chunk-IXHOBNXA.js → chunk-TMNGRIPL.js} +43 -41
  39. package/dist/chunk-UNS34PTA.cjs +680 -0
  40. package/dist/chunk-UR6JKS56.js +432 -0
  41. package/dist/{chunk-NPWHHWXT.cjs → chunk-UYXTY6ZQ.cjs} +127 -1
  42. package/dist/chunk-XUUCOLWU.cjs +1097 -0
  43. package/dist/chunk-YG5KJESI.js +641 -0
  44. package/dist/chunk-ZBKE2QRQ.js +1401 -0
  45. package/dist/chunk-ZSPO2GF2.cjs +1401 -0
  46. package/dist/codec-95k8CAu5.d.cts +35 -0
  47. package/dist/codec-AFuOxfsO.d.ts +60 -0
  48. package/dist/codec-B-UJ5Iow.d.cts +75 -0
  49. package/dist/codec-BC5jfvMb.d.ts +35 -0
  50. package/dist/codec-BECYPfY8.d.ts +35 -0
  51. package/dist/codec-BsPU1vNC.d.ts +39 -0
  52. package/dist/codec-BvpuF-6u.d.cts +39 -0
  53. package/dist/codec-C8jZI5Cx.d.cts +39 -0
  54. package/dist/codec-CAevkgf5.d.cts +33 -0
  55. package/dist/codec-CSUqCrRs.d.ts +39 -0
  56. package/dist/codec-C_HMXNK_.d.ts +33 -0
  57. package/dist/{codec-Bvr7rFtj.d.cts → codec-CpuvYTSV.d.cts} +1 -1
  58. package/dist/codec-D0x8-SCw.d.cts +35 -0
  59. package/dist/codec-D7ARhpG1.d.ts +75 -0
  60. package/dist/codec-DNAUGshO.d.cts +60 -0
  61. package/dist/codec-DPx_QNn0.d.ts +31 -0
  62. package/dist/{codec-B2mc2g3i.d.ts → codec-DRhCx_hw.d.ts} +1 -1
  63. package/dist/codec-Db7YPe3l.d.ts +31 -0
  64. package/dist/codec-axkJpb7D.d.cts +31 -0
  65. package/dist/codec-ujAbFaep.d.cts +31 -0
  66. package/dist/draft10-session.cjs +6 -0
  67. package/dist/draft10-session.d.cts +8 -0
  68. package/dist/draft10-session.d.ts +8 -0
  69. package/dist/draft10-session.js +6 -0
  70. package/dist/draft10.cjs +115 -0
  71. package/dist/draft10.d.cts +95 -0
  72. package/dist/draft10.d.ts +95 -0
  73. package/dist/draft10.js +115 -0
  74. package/dist/draft11-session.cjs +6 -0
  75. package/dist/draft11-session.d.cts +8 -0
  76. package/dist/draft11-session.d.ts +8 -0
  77. package/dist/draft11-session.js +6 -0
  78. package/dist/draft11.cjs +109 -0
  79. package/dist/draft11.d.cts +99 -0
  80. package/dist/draft11.d.ts +99 -0
  81. package/dist/draft11.js +109 -0
  82. package/dist/draft12-session.cjs +6 -0
  83. package/dist/draft12-session.d.cts +8 -0
  84. package/dist/draft12-session.d.ts +8 -0
  85. package/dist/draft12-session.js +6 -0
  86. package/dist/draft12.cjs +117 -0
  87. package/dist/draft12.d.cts +106 -0
  88. package/dist/draft12.d.ts +106 -0
  89. package/dist/draft12.js +117 -0
  90. package/dist/draft13-session.cjs +6 -0
  91. package/dist/draft13-session.d.cts +8 -0
  92. package/dist/draft13-session.d.ts +8 -0
  93. package/dist/draft13-session.js +6 -0
  94. package/dist/draft13.cjs +119 -0
  95. package/dist/draft13.d.cts +108 -0
  96. package/dist/draft13.d.ts +108 -0
  97. package/dist/draft13.js +119 -0
  98. package/dist/draft14-session.cjs +2 -2
  99. package/dist/draft14-session.d.cts +3 -3
  100. package/dist/draft14-session.d.ts +3 -3
  101. package/dist/draft14-session.js +1 -1
  102. package/dist/draft14.cjs +3 -3
  103. package/dist/draft14.d.cts +16 -4
  104. package/dist/draft14.d.ts +16 -4
  105. package/dist/draft14.js +2 -2
  106. package/dist/draft15-session.cjs +6 -0
  107. package/dist/draft15-session.d.cts +8 -0
  108. package/dist/draft15-session.d.ts +8 -0
  109. package/dist/draft15-session.js +6 -0
  110. package/dist/draft15.cjs +111 -0
  111. package/dist/draft15.d.cts +93 -0
  112. package/dist/draft15.d.ts +93 -0
  113. package/dist/draft15.js +111 -0
  114. package/dist/draft16-session.cjs +6 -0
  115. package/dist/draft16-session.d.cts +8 -0
  116. package/dist/draft16-session.d.ts +8 -0
  117. package/dist/draft16-session.js +6 -0
  118. package/dist/draft16.cjs +113 -0
  119. package/dist/draft16.d.cts +94 -0
  120. package/dist/draft16.d.ts +94 -0
  121. package/dist/draft16.js +113 -0
  122. package/dist/draft17-session.cjs +8 -0
  123. package/dist/draft17-session.d.cts +51 -0
  124. package/dist/draft17-session.d.ts +51 -0
  125. package/dist/draft17-session.js +8 -0
  126. package/dist/draft17.cjs +99 -0
  127. package/dist/draft17.d.cts +40 -0
  128. package/dist/draft17.d.ts +40 -0
  129. package/dist/draft17.js +99 -0
  130. package/dist/draft7-session.cjs +3 -3
  131. package/dist/draft7-session.d.cts +3 -3
  132. package/dist/draft7-session.d.ts +3 -3
  133. package/dist/draft7-session.js +2 -2
  134. package/dist/draft7.cjs +4 -4
  135. package/dist/draft7.d.cts +2 -2
  136. package/dist/draft7.d.ts +2 -2
  137. package/dist/draft7.js +2 -2
  138. package/dist/draft8-session.cjs +6 -0
  139. package/dist/draft8-session.d.cts +8 -0
  140. package/dist/draft8-session.d.ts +8 -0
  141. package/dist/draft8-session.js +6 -0
  142. package/dist/draft8.cjs +115 -0
  143. package/dist/draft8.d.cts +95 -0
  144. package/dist/draft8.d.ts +95 -0
  145. package/dist/draft8.js +115 -0
  146. package/dist/draft9-session.cjs +6 -0
  147. package/dist/draft9-session.d.cts +8 -0
  148. package/dist/draft9-session.d.ts +8 -0
  149. package/dist/draft9-session.js +6 -0
  150. package/dist/draft9.cjs +115 -0
  151. package/dist/draft9.d.cts +95 -0
  152. package/dist/draft9.d.ts +95 -0
  153. package/dist/draft9.js +115 -0
  154. package/dist/index.cjs +76 -4
  155. package/dist/index.d.cts +68 -5
  156. package/dist/index.d.ts +68 -5
  157. package/dist/index.js +76 -4
  158. package/dist/{session-types-DFjMk4HH.d.ts → session-types-CJIFbTPd.d.ts} +1 -1
  159. package/dist/{session-types-DW1RSZX_.d.cts → session-types-Cbq8IGCP.d.cts} +1 -1
  160. package/dist/session.cjs +5 -5
  161. package/dist/session.d.cts +3 -3
  162. package/dist/session.d.ts +3 -3
  163. package/dist/session.js +5 -5
  164. package/dist/types-4VxSL2Ho.d.cts +261 -0
  165. package/dist/types-4VxSL2Ho.d.ts +261 -0
  166. package/dist/types-B2afJZM-.d.cts +236 -0
  167. package/dist/types-B2afJZM-.d.ts +236 -0
  168. package/dist/types-Bg6QYNVt.d.cts +290 -0
  169. package/dist/types-Bg6QYNVt.d.ts +290 -0
  170. package/dist/types-C_1HrqBl.d.cts +306 -0
  171. package/dist/types-C_1HrqBl.d.ts +306 -0
  172. package/dist/types-Cw4WE9dh.d.cts +261 -0
  173. package/dist/types-Cw4WE9dh.d.ts +261 -0
  174. package/dist/types-D5gNQiDj.d.cts +261 -0
  175. package/dist/types-D5gNQiDj.d.ts +261 -0
  176. package/dist/types-DqCDFqgB.d.cts +230 -0
  177. package/dist/types-DqCDFqgB.d.ts +230 -0
  178. package/dist/types-ERexTpT8.d.cts +217 -0
  179. package/dist/types-ERexTpT8.d.ts +217 -0
  180. package/dist/{types-DPYE49t0.d.cts → types-QNXoxC9Y.d.cts} +2 -7
  181. package/dist/{types-DPYE49t0.d.ts → types-QNXoxC9Y.d.ts} +2 -7
  182. package/dist/types-r-CasCf1.d.cts +262 -0
  183. package/dist/types-r-CasCf1.d.ts +262 -0
  184. package/package.json +110 -2
  185. package/src/core/types.ts +24 -7
  186. package/src/drafts/draft07/codec.ts +49 -41
  187. package/src/drafts/draft07/rules.ts +1 -3
  188. package/src/drafts/draft08/codec.ts +1254 -0
  189. package/src/drafts/draft08/index.ts +125 -0
  190. package/src/drafts/draft08/messages.ts +72 -0
  191. package/src/drafts/draft08/rules.ts +91 -0
  192. package/src/drafts/draft08/session-fsm.ts +718 -0
  193. package/src/drafts/draft08/session.ts +26 -0
  194. package/src/drafts/draft08/types.ts +377 -0
  195. package/src/drafts/draft09/codec.ts +1235 -0
  196. package/src/drafts/draft09/index.ts +125 -0
  197. package/src/drafts/draft09/messages.ts +72 -0
  198. package/src/drafts/draft09/rules.ts +91 -0
  199. package/src/drafts/draft09/session-fsm.ts +718 -0
  200. package/src/drafts/draft09/session.ts +26 -0
  201. package/src/drafts/draft09/types.ts +376 -0
  202. package/src/drafts/draft10/codec.ts +1235 -0
  203. package/src/drafts/draft10/index.ts +125 -0
  204. package/src/drafts/draft10/messages.ts +72 -0
  205. package/src/drafts/draft10/rules.ts +91 -0
  206. package/src/drafts/draft10/session-fsm.ts +718 -0
  207. package/src/drafts/draft10/session.ts +26 -0
  208. package/src/drafts/draft10/types.ts +376 -0
  209. package/src/drafts/draft11/codec.ts +1198 -0
  210. package/src/drafts/draft11/index.ts +123 -0
  211. package/src/drafts/draft11/messages.ts +71 -0
  212. package/src/drafts/draft11/rules.ts +100 -0
  213. package/src/drafts/draft11/session-fsm.ts +758 -0
  214. package/src/drafts/draft11/session.ts +26 -0
  215. package/src/drafts/draft11/types.ts +375 -0
  216. package/src/drafts/draft12/codec.ts +1354 -0
  217. package/src/drafts/draft12/index.ts +130 -0
  218. package/src/drafts/draft12/messages.ts +84 -0
  219. package/src/drafts/draft12/rules.ts +106 -0
  220. package/src/drafts/draft12/session-fsm.ts +805 -0
  221. package/src/drafts/draft12/session.ts +26 -0
  222. package/src/drafts/draft12/types.ts +414 -0
  223. package/src/drafts/draft13/codec.ts +1438 -0
  224. package/src/drafts/draft13/index.ts +132 -0
  225. package/src/drafts/draft13/messages.ts +86 -0
  226. package/src/drafts/draft13/rules.ts +108 -0
  227. package/src/drafts/draft13/session-fsm.ts +819 -0
  228. package/src/drafts/draft13/session.ts +26 -0
  229. package/src/drafts/draft13/types.ts +433 -0
  230. package/src/drafts/draft14/session-fsm.ts +182 -1
  231. package/src/drafts/draft15/codec.ts +1661 -0
  232. package/src/drafts/draft15/index.ts +121 -0
  233. package/src/drafts/draft15/messages.ts +64 -0
  234. package/src/drafts/draft15/rules.ts +95 -0
  235. package/src/drafts/draft15/session-fsm.ts +687 -0
  236. package/src/drafts/draft15/session.ts +26 -0
  237. package/src/drafts/draft15/types.ts +336 -0
  238. package/src/drafts/draft16/codec.ts +1623 -0
  239. package/src/drafts/draft16/index.ts +123 -0
  240. package/src/drafts/draft16/messages.ts +67 -0
  241. package/src/drafts/draft16/rules.ts +96 -0
  242. package/src/drafts/draft16/session-fsm.ts +682 -0
  243. package/src/drafts/draft16/session.ts +26 -0
  244. package/src/drafts/draft16/types.ts +354 -0
  245. package/src/drafts/draft17/codec.ts +1621 -0
  246. package/src/drafts/draft17/index.ts +105 -0
  247. package/src/drafts/draft17/messages.ts +53 -0
  248. package/src/drafts/draft17/rules.ts +85 -0
  249. package/src/drafts/draft17/session-fsm.ts +437 -0
  250. package/src/drafts/draft17/session.ts +15 -0
  251. package/src/drafts/draft17/types.ts +310 -0
  252. package/src/index.ts +217 -2
@@ -0,0 +1,641 @@
1
+ // src/drafts/draft15/rules.ts
2
+ var CONTROL_MESSAGES = /* @__PURE__ */ new Set([
3
+ "client_setup",
4
+ "server_setup",
5
+ "subscribe",
6
+ "subscribe_ok",
7
+ "subscribe_update",
8
+ "unsubscribe",
9
+ "publish",
10
+ "publish_ok",
11
+ "publish_done",
12
+ "publish_namespace",
13
+ "publish_namespace_done",
14
+ "publish_namespace_cancel",
15
+ "subscribe_namespace",
16
+ "unsubscribe_namespace",
17
+ "fetch",
18
+ "fetch_ok",
19
+ "fetch_cancel",
20
+ "track_status",
21
+ "request_ok",
22
+ "request_error",
23
+ "goaway",
24
+ "max_request_id",
25
+ "requests_blocked"
26
+ ]);
27
+ var CLIENT_ONLY_MESSAGES = /* @__PURE__ */ new Set(["client_setup"]);
28
+ var SERVER_ONLY_MESSAGES = /* @__PURE__ */ new Set(["server_setup"]);
29
+ var BIDIRECTIONAL_MESSAGES = /* @__PURE__ */ new Set([
30
+ "subscribe",
31
+ "subscribe_ok",
32
+ "subscribe_update",
33
+ "unsubscribe",
34
+ "publish",
35
+ "publish_ok",
36
+ "publish_done",
37
+ "publish_namespace",
38
+ "publish_namespace_done",
39
+ "publish_namespace_cancel",
40
+ "subscribe_namespace",
41
+ "unsubscribe_namespace",
42
+ "fetch",
43
+ "fetch_ok",
44
+ "fetch_cancel",
45
+ "track_status",
46
+ "request_ok",
47
+ "request_error",
48
+ "goaway",
49
+ "max_request_id",
50
+ "requests_blocked"
51
+ ]);
52
+ function getLegalOutgoing(phase, role) {
53
+ const legal = /* @__PURE__ */ new Set();
54
+ switch (phase) {
55
+ case "idle":
56
+ if (role === "client") legal.add("client_setup");
57
+ break;
58
+ case "setup":
59
+ if (role === "server") legal.add("server_setup");
60
+ break;
61
+ case "ready": {
62
+ legal.add("goaway");
63
+ for (const msg of BIDIRECTIONAL_MESSAGES) {
64
+ legal.add(msg);
65
+ }
66
+ break;
67
+ }
68
+ case "draining":
69
+ break;
70
+ }
71
+ return legal;
72
+ }
73
+ function getLegalIncoming(phase, role) {
74
+ const remoteRole = role === "client" ? "server" : "client";
75
+ return getLegalOutgoing(phase, remoteRole);
76
+ }
77
+
78
+ // src/drafts/draft15/session-fsm.ts
79
+ function violation(code, message, currentPhase, offendingMessage) {
80
+ return { code, message, currentPhase, offendingMessage };
81
+ }
82
+ var Draft15SessionFSM = class {
83
+ _phase = "idle";
84
+ _role;
85
+ _subscriptions = /* @__PURE__ */ new Map();
86
+ _publishes = /* @__PURE__ */ new Map();
87
+ _fetches = /* @__PURE__ */ new Map();
88
+ _requestIds = /* @__PURE__ */ new Set();
89
+ constructor(role) {
90
+ this._role = role;
91
+ }
92
+ get phase() {
93
+ return this._phase;
94
+ }
95
+ get role() {
96
+ return this._role;
97
+ }
98
+ get subscriptions() {
99
+ return this._subscriptions;
100
+ }
101
+ get announces() {
102
+ return /* @__PURE__ */ new Map();
103
+ }
104
+ get publishes() {
105
+ return this._publishes;
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
+ checkDuplicateRequestId(requestId, msgType) {
137
+ if (this._requestIds.has(requestId)) {
138
+ return violation(
139
+ "DUPLICATE_REQUEST_ID",
140
+ `Request ID ${requestId} already in use`,
141
+ this._phase,
142
+ msgType
143
+ );
144
+ }
145
+ return null;
146
+ }
147
+ checkKnownRequestId(requestId, msgType) {
148
+ if (!this._requestIds.has(requestId)) {
149
+ return violation(
150
+ "UNKNOWN_REQUEST_ID",
151
+ `No request with ID ${requestId}`,
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_update":
199
+ return this.handleSubscribeUpdate(message, sideEffects);
200
+ case "unsubscribe":
201
+ return this.handleUnsubscribe(message, sideEffects);
202
+ // Publish lifecycle
203
+ case "publish":
204
+ return this.handlePublish(message, sideEffects);
205
+ case "publish_ok":
206
+ return this.handlePublishOk(message, sideEffects);
207
+ case "publish_done":
208
+ return this.handlePublishDone(message, sideEffects);
209
+ // Fetch lifecycle
210
+ case "fetch":
211
+ return this.handleFetch(message, sideEffects);
212
+ case "fetch_ok":
213
+ return this.handleFetchOk(message, sideEffects);
214
+ case "fetch_cancel":
215
+ return this.handleFetchCancel(message, sideEffects);
216
+ // Consolidated responses — REQUEST_ERROR can end any pending request
217
+ case "request_ok":
218
+ return this.handleRequestOk(message, sideEffects);
219
+ case "request_error":
220
+ return this.handleRequestError(message, sideEffects);
221
+ // Namespace and track status — register request IDs
222
+ case "publish_namespace":
223
+ return this.handlePublishNamespace(message, sideEffects);
224
+ case "subscribe_namespace":
225
+ return this.handleSubscribeNamespace(message, sideEffects);
226
+ case "track_status":
227
+ return this.handleTrackStatus(message, sideEffects);
228
+ // Other ready-phase messages
229
+ default:
230
+ return this.handleReadyPhaseMessage(message);
231
+ }
232
+ }
233
+ handleClientSetup(direction) {
234
+ if (this._phase !== "idle") {
235
+ return {
236
+ ok: false,
237
+ violation: violation(
238
+ "SETUP_VIOLATION",
239
+ "CLIENT_SETUP already sent/received",
240
+ this._phase,
241
+ "client_setup"
242
+ )
243
+ };
244
+ }
245
+ if (direction === "outbound" && this._role !== "client") {
246
+ return {
247
+ ok: false,
248
+ violation: violation(
249
+ "ROLE_VIOLATION",
250
+ "Only client can send CLIENT_SETUP",
251
+ this._phase,
252
+ "client_setup"
253
+ )
254
+ };
255
+ }
256
+ this._phase = "setup";
257
+ return { ok: true, phase: this._phase, sideEffects: [] };
258
+ }
259
+ handleServerSetup(direction) {
260
+ if (this._phase !== "setup") {
261
+ return {
262
+ ok: false,
263
+ violation: violation(
264
+ "SETUP_VIOLATION",
265
+ "SERVER_SETUP before CLIENT_SETUP",
266
+ this._phase,
267
+ "server_setup"
268
+ )
269
+ };
270
+ }
271
+ if (direction === "outbound" && this._role !== "server") {
272
+ return {
273
+ ok: false,
274
+ violation: violation(
275
+ "ROLE_VIOLATION",
276
+ "Only server can send SERVER_SETUP",
277
+ this._phase,
278
+ "server_setup"
279
+ )
280
+ };
281
+ }
282
+ this._phase = "ready";
283
+ return { ok: true, phase: this._phase, sideEffects: [{ type: "session-ready" }] };
284
+ }
285
+ handleGoAway(message, sideEffects) {
286
+ if (this._phase !== "ready" && this._phase !== "draining") {
287
+ return {
288
+ ok: false,
289
+ violation: violation(
290
+ "UNEXPECTED_MESSAGE",
291
+ `GOAWAY not valid in phase ${this._phase}`,
292
+ this._phase,
293
+ "goaway"
294
+ )
295
+ };
296
+ }
297
+ this._phase = "draining";
298
+ const goaway = message;
299
+ sideEffects.push({ type: "session-draining", goAwayUri: goaway.new_session_uri });
300
+ return { ok: true, phase: this._phase, sideEffects };
301
+ }
302
+ requireReady(msgType) {
303
+ if (this._phase !== "ready" && this._phase !== "draining") {
304
+ return violation(
305
+ this._phase === "idle" || this._phase === "setup" ? "MESSAGE_BEFORE_SETUP" : "UNEXPECTED_MESSAGE",
306
+ `${msgType} requires ready phase, current: ${this._phase}`,
307
+ this._phase,
308
+ msgType
309
+ );
310
+ }
311
+ return null;
312
+ }
313
+ handleSubscribe(message, sideEffects) {
314
+ const err = this.requireReady(message.type);
315
+ if (err) return { ok: false, violation: err };
316
+ const sub = message;
317
+ const dupErr = this.checkDuplicateRequestId(sub.request_id, message.type);
318
+ if (dupErr) return { ok: false, violation: dupErr };
319
+ this._requestIds.add(sub.request_id);
320
+ this._subscriptions.set(sub.request_id, {
321
+ subscribeId: sub.request_id,
322
+ phase: "pending",
323
+ trackNamespace: sub.track_namespace,
324
+ trackName: sub.track_name
325
+ });
326
+ return { ok: true, phase: this._phase, sideEffects };
327
+ }
328
+ handleSubscribeOk(message, sideEffects) {
329
+ const err = this.requireReady(message.type);
330
+ if (err) return { ok: false, violation: err };
331
+ const ok = message;
332
+ const idErr = this.checkKnownRequestId(ok.request_id, message.type);
333
+ if (idErr) return { ok: false, violation: idErr };
334
+ const existing = this._subscriptions.get(ok.request_id);
335
+ if (!existing)
336
+ return {
337
+ ok: false,
338
+ violation: violation(
339
+ "UNKNOWN_REQUEST_ID",
340
+ `No subscription with request ID ${ok.request_id}`,
341
+ this._phase,
342
+ message.type
343
+ )
344
+ };
345
+ if (existing.phase !== "pending")
346
+ return {
347
+ ok: false,
348
+ violation: violation(
349
+ "STATE_VIOLATION",
350
+ `Subscription ${ok.request_id} is ${existing.phase}, not pending`,
351
+ this._phase,
352
+ message.type
353
+ )
354
+ };
355
+ this._subscriptions.set(ok.request_id, { ...existing, phase: "active" });
356
+ sideEffects.push({ type: "subscription-activated", subscribeId: ok.request_id });
357
+ return { ok: true, phase: this._phase, sideEffects };
358
+ }
359
+ handleSubscribeUpdate(message, sideEffects) {
360
+ const err = this.requireReady(message.type);
361
+ if (err) return { ok: false, violation: err };
362
+ const update = message;
363
+ const dupErr = this.checkDuplicateRequestId(update.request_id, message.type);
364
+ if (dupErr) return { ok: false, violation: dupErr };
365
+ this._requestIds.add(update.request_id);
366
+ const existing = this._subscriptions.get(update.subscription_request_id);
367
+ if (!existing)
368
+ return {
369
+ ok: false,
370
+ violation: violation(
371
+ "UNKNOWN_REQUEST_ID",
372
+ `No subscription with request ID ${update.subscription_request_id}`,
373
+ this._phase,
374
+ message.type
375
+ )
376
+ };
377
+ if (existing.phase !== "active")
378
+ return {
379
+ ok: false,
380
+ violation: violation(
381
+ "STATE_VIOLATION",
382
+ `Subscription ${update.subscription_request_id} is ${existing.phase}, not active`,
383
+ this._phase,
384
+ message.type
385
+ )
386
+ };
387
+ return { ok: true, phase: this._phase, sideEffects };
388
+ }
389
+ handleUnsubscribe(message, sideEffects) {
390
+ const err = this.requireReady(message.type);
391
+ if (err) return { ok: false, violation: err };
392
+ const unsub = message;
393
+ const idErr = this.checkKnownRequestId(unsub.request_id, message.type);
394
+ if (idErr) return { ok: false, violation: idErr };
395
+ const existing = this._subscriptions.get(unsub.request_id);
396
+ if (!existing)
397
+ return {
398
+ ok: false,
399
+ violation: violation(
400
+ "UNKNOWN_REQUEST_ID",
401
+ `No subscription with request ID ${unsub.request_id}`,
402
+ this._phase,
403
+ message.type
404
+ )
405
+ };
406
+ this._subscriptions.set(unsub.request_id, { ...existing, phase: "done" });
407
+ sideEffects.push({
408
+ type: "subscription-ended",
409
+ subscribeId: unsub.request_id,
410
+ reason: "unsubscribed"
411
+ });
412
+ return { ok: true, phase: this._phase, sideEffects };
413
+ }
414
+ handlePublish(message, sideEffects) {
415
+ const err = this.requireReady(message.type);
416
+ if (err) return { ok: false, violation: err };
417
+ const pub = message;
418
+ const dupErr = this.checkDuplicateRequestId(pub.request_id, message.type);
419
+ if (dupErr) return { ok: false, violation: dupErr };
420
+ this._requestIds.add(pub.request_id);
421
+ this._publishes.set(pub.request_id, { requestId: pub.request_id, phase: "pending" });
422
+ return { ok: true, phase: this._phase, sideEffects };
423
+ }
424
+ handlePublishOk(message, sideEffects) {
425
+ const err = this.requireReady(message.type);
426
+ if (err) return { ok: false, violation: err };
427
+ const ok = message;
428
+ const idErr = this.checkKnownRequestId(ok.request_id, message.type);
429
+ if (idErr) return { ok: false, violation: idErr };
430
+ const existing = this._publishes.get(ok.request_id);
431
+ if (!existing)
432
+ return {
433
+ ok: false,
434
+ violation: violation(
435
+ "UNKNOWN_REQUEST_ID",
436
+ `No publish with request ID ${ok.request_id}`,
437
+ this._phase,
438
+ message.type
439
+ )
440
+ };
441
+ if (existing.phase !== "pending")
442
+ return {
443
+ ok: false,
444
+ violation: violation(
445
+ "STATE_VIOLATION",
446
+ `Publish ${ok.request_id} is ${existing.phase}, not pending`,
447
+ this._phase,
448
+ message.type
449
+ )
450
+ };
451
+ this._publishes.set(ok.request_id, { ...existing, phase: "active" });
452
+ sideEffects.push({ type: "publish-activated", requestId: ok.request_id });
453
+ return { ok: true, phase: this._phase, sideEffects };
454
+ }
455
+ handlePublishDone(message, sideEffects) {
456
+ const err = this.requireReady(message.type);
457
+ if (err) return { ok: false, violation: err };
458
+ const done = message;
459
+ const idErr = this.checkKnownRequestId(done.request_id, message.type);
460
+ if (idErr) return { ok: false, violation: idErr };
461
+ const existing = this._publishes.get(done.request_id);
462
+ if (!existing)
463
+ return {
464
+ ok: false,
465
+ violation: violation(
466
+ "UNKNOWN_REQUEST_ID",
467
+ `No publish with request ID ${done.request_id}`,
468
+ this._phase,
469
+ message.type
470
+ )
471
+ };
472
+ this._publishes.set(done.request_id, { ...existing, phase: "done" });
473
+ sideEffects.push({
474
+ type: "publish-ended",
475
+ requestId: done.request_id,
476
+ reason: done.reason_phrase
477
+ });
478
+ return { ok: true, phase: this._phase, sideEffects };
479
+ }
480
+ handleFetch(message, sideEffects) {
481
+ const err = this.requireReady(message.type);
482
+ if (err) return { ok: false, violation: err };
483
+ const fetch = message;
484
+ const dupErr = this.checkDuplicateRequestId(fetch.request_id, message.type);
485
+ if (dupErr) return { ok: false, violation: dupErr };
486
+ this._requestIds.add(fetch.request_id);
487
+ this._fetches.set(fetch.request_id, { requestId: fetch.request_id, phase: "pending" });
488
+ return { ok: true, phase: this._phase, sideEffects };
489
+ }
490
+ handleFetchOk(message, sideEffects) {
491
+ const err = this.requireReady(message.type);
492
+ if (err) return { ok: false, violation: err };
493
+ const ok = message;
494
+ const idErr = this.checkKnownRequestId(ok.request_id, message.type);
495
+ if (idErr) return { ok: false, violation: idErr };
496
+ const existing = this._fetches.get(ok.request_id);
497
+ if (!existing)
498
+ return {
499
+ ok: false,
500
+ violation: violation(
501
+ "UNKNOWN_REQUEST_ID",
502
+ `No fetch with request ID ${ok.request_id}`,
503
+ this._phase,
504
+ message.type
505
+ )
506
+ };
507
+ if (existing.phase !== "pending")
508
+ return {
509
+ ok: false,
510
+ violation: violation(
511
+ "STATE_VIOLATION",
512
+ `Fetch ${ok.request_id} is ${existing.phase}, not pending`,
513
+ this._phase,
514
+ message.type
515
+ )
516
+ };
517
+ this._fetches.set(ok.request_id, { ...existing, phase: "active" });
518
+ sideEffects.push({ type: "fetch-activated", requestId: ok.request_id });
519
+ return { ok: true, phase: this._phase, sideEffects };
520
+ }
521
+ handleFetchCancel(message, sideEffects) {
522
+ const err = this.requireReady(message.type);
523
+ if (err) return { ok: false, violation: err };
524
+ const cancel = message;
525
+ const idErr = this.checkKnownRequestId(cancel.request_id, message.type);
526
+ if (idErr) return { ok: false, violation: idErr };
527
+ const existing = this._fetches.get(cancel.request_id);
528
+ if (!existing)
529
+ return {
530
+ ok: false,
531
+ violation: violation(
532
+ "UNKNOWN_REQUEST_ID",
533
+ `No fetch with request ID ${cancel.request_id}`,
534
+ this._phase,
535
+ message.type
536
+ )
537
+ };
538
+ this._fetches.set(cancel.request_id, { ...existing, phase: "cancelled" });
539
+ sideEffects.push({ type: "fetch-ended", requestId: cancel.request_id, reason: "cancelled" });
540
+ return { ok: true, phase: this._phase, sideEffects };
541
+ }
542
+ // REQUEST_ERROR replaces subscribe_error, publish_error, fetch_error, etc.
543
+ handleRequestError(message, sideEffects) {
544
+ const err = this.requireReady(message.type);
545
+ if (err) return { ok: false, violation: err };
546
+ const reqErr = message;
547
+ const sub = this._subscriptions.get(reqErr.request_id);
548
+ if (sub && sub.phase === "pending") {
549
+ this._subscriptions.set(reqErr.request_id, { ...sub, phase: "error" });
550
+ sideEffects.push({
551
+ type: "subscription-ended",
552
+ subscribeId: reqErr.request_id,
553
+ reason: reqErr.reason_phrase
554
+ });
555
+ return { ok: true, phase: this._phase, sideEffects };
556
+ }
557
+ const pub = this._publishes.get(reqErr.request_id);
558
+ if (pub && pub.phase === "pending") {
559
+ this._publishes.set(reqErr.request_id, { ...pub, phase: "error" });
560
+ sideEffects.push({
561
+ type: "publish-ended",
562
+ requestId: reqErr.request_id,
563
+ reason: reqErr.reason_phrase
564
+ });
565
+ return { ok: true, phase: this._phase, sideEffects };
566
+ }
567
+ const fetch = this._fetches.get(reqErr.request_id);
568
+ if (fetch && fetch.phase === "pending") {
569
+ this._fetches.set(reqErr.request_id, { ...fetch, phase: "error" });
570
+ sideEffects.push({
571
+ type: "fetch-ended",
572
+ requestId: reqErr.request_id,
573
+ reason: reqErr.reason_phrase
574
+ });
575
+ return { ok: true, phase: this._phase, sideEffects };
576
+ }
577
+ return { ok: true, phase: this._phase, sideEffects };
578
+ }
579
+ // REQUEST_OK replaces subscribe_namespace_ok, publish_namespace_ok, track_status_ok
580
+ handleRequestOk(message, sideEffects) {
581
+ const err = this.requireReady(message.type);
582
+ if (err) return { ok: false, violation: err };
583
+ return { ok: true, phase: this._phase, sideEffects };
584
+ }
585
+ // ─── Namespace and track status request ID tracking ───
586
+ handlePublishNamespace(message, sideEffects) {
587
+ const err = this.requireReady(message.type);
588
+ if (err) return { ok: false, violation: err };
589
+ const pn = message;
590
+ const dupErr = this.checkDuplicateRequestId(pn.request_id, message.type);
591
+ if (dupErr) return { ok: false, violation: dupErr };
592
+ this._requestIds.add(pn.request_id);
593
+ return { ok: true, phase: this._phase, sideEffects };
594
+ }
595
+ handleSubscribeNamespace(message, sideEffects) {
596
+ const err = this.requireReady(message.type);
597
+ if (err) return { ok: false, violation: err };
598
+ const sn = message;
599
+ const dupErr = this.checkDuplicateRequestId(sn.request_id, message.type);
600
+ if (dupErr) return { ok: false, violation: dupErr };
601
+ this._requestIds.add(sn.request_id);
602
+ return { ok: true, phase: this._phase, sideEffects };
603
+ }
604
+ handleTrackStatus(message, sideEffects) {
605
+ const err = this.requireReady(message.type);
606
+ if (err) return { ok: false, violation: err };
607
+ const ts = message;
608
+ const dupErr = this.checkDuplicateRequestId(ts.request_id, message.type);
609
+ if (dupErr) return { ok: false, violation: dupErr };
610
+ this._requestIds.add(ts.request_id);
611
+ return { ok: true, phase: this._phase, sideEffects };
612
+ }
613
+ handleReadyPhaseMessage(message) {
614
+ const err = this.requireReady(message.type);
615
+ if (err) return { ok: false, violation: err };
616
+ return { ok: true, phase: this._phase, sideEffects: [] };
617
+ }
618
+ reset() {
619
+ this._phase = "idle";
620
+ this._subscriptions.clear();
621
+ this._publishes.clear();
622
+ this._fetches.clear();
623
+ this._requestIds.clear();
624
+ }
625
+ };
626
+
627
+ // src/drafts/draft15/session.ts
628
+ function createDraft15SessionState(options) {
629
+ return new Draft15SessionFSM(options.role);
630
+ }
631
+
632
+ export {
633
+ CONTROL_MESSAGES,
634
+ CLIENT_ONLY_MESSAGES,
635
+ SERVER_ONLY_MESSAGES,
636
+ BIDIRECTIONAL_MESSAGES,
637
+ getLegalOutgoing,
638
+ getLegalIncoming,
639
+ Draft15SessionFSM,
640
+ createDraft15SessionState
641
+ };