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