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