@moqtap/codec 0.1.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 (71) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +95 -0
  3. package/dist/chunk-23YG7F46.js +764 -0
  4. package/dist/chunk-2NARXGVA.cjs +194 -0
  5. package/dist/chunk-3BSZ55L3.cjs +307 -0
  6. package/dist/chunk-5WFXFLL4.cjs +1185 -0
  7. package/dist/chunk-DC4L6ZIT.js +307 -0
  8. package/dist/chunk-GDRGWFEK.cjs +498 -0
  9. package/dist/chunk-IQPDRQVC.js +1185 -0
  10. package/dist/chunk-QYG6KGOV.cjs +101 -0
  11. package/dist/chunk-UOBWHJA5.js +101 -0
  12. package/dist/chunk-WNTXF3DE.cjs +764 -0
  13. package/dist/chunk-YBSEOSSP.js +194 -0
  14. package/dist/chunk-YPXLV5YK.js +498 -0
  15. package/dist/codec-CTvFtQQI.d.cts +86 -0
  16. package/dist/codec-qPzfmLNu.d.ts +86 -0
  17. package/dist/draft14-session.cjs +6 -0
  18. package/dist/draft14-session.d.cts +8 -0
  19. package/dist/draft14-session.d.ts +8 -0
  20. package/dist/draft14-session.js +6 -0
  21. package/dist/draft14.cjs +121 -0
  22. package/dist/draft14.d.cts +96 -0
  23. package/dist/draft14.d.ts +96 -0
  24. package/dist/draft14.js +121 -0
  25. package/dist/draft7-session.cjs +7 -0
  26. package/dist/draft7-session.d.cts +7 -0
  27. package/dist/draft7-session.d.ts +7 -0
  28. package/dist/draft7-session.js +7 -0
  29. package/dist/draft7.cjs +60 -0
  30. package/dist/draft7.d.cts +72 -0
  31. package/dist/draft7.d.ts +72 -0
  32. package/dist/draft7.js +60 -0
  33. package/dist/index.cjs +40 -0
  34. package/dist/index.d.cts +40 -0
  35. package/dist/index.d.ts +40 -0
  36. package/dist/index.js +40 -0
  37. package/dist/session-types-B9NIf7_F.d.ts +101 -0
  38. package/dist/session-types-CCo-oA-d.d.cts +101 -0
  39. package/dist/session.cjs +27 -0
  40. package/dist/session.d.cts +24 -0
  41. package/dist/session.d.ts +24 -0
  42. package/dist/session.js +27 -0
  43. package/dist/types-CIk5W10V.d.cts +249 -0
  44. package/dist/types-CIk5W10V.d.ts +249 -0
  45. package/dist/types-ClXELFGN.d.cts +241 -0
  46. package/dist/types-ClXELFGN.d.ts +241 -0
  47. package/package.json +84 -0
  48. package/src/core/buffer-reader.ts +107 -0
  49. package/src/core/buffer-writer.ts +91 -0
  50. package/src/core/errors.ts +1 -0
  51. package/src/core/session-types.ts +103 -0
  52. package/src/core/types.ts +363 -0
  53. package/src/drafts/draft07/announce-fsm.ts +2 -0
  54. package/src/drafts/draft07/codec.ts +874 -0
  55. package/src/drafts/draft07/index.ts +70 -0
  56. package/src/drafts/draft07/messages.ts +44 -0
  57. package/src/drafts/draft07/parameters.ts +12 -0
  58. package/src/drafts/draft07/rules.ts +75 -0
  59. package/src/drafts/draft07/session-fsm.ts +353 -0
  60. package/src/drafts/draft07/session.ts +21 -0
  61. package/src/drafts/draft07/subscription-fsm.ts +3 -0
  62. package/src/drafts/draft07/varint.ts +23 -0
  63. package/src/drafts/draft14/codec.ts +1330 -0
  64. package/src/drafts/draft14/index.ts +132 -0
  65. package/src/drafts/draft14/messages.ts +76 -0
  66. package/src/drafts/draft14/rules.ts +70 -0
  67. package/src/drafts/draft14/session-fsm.ts +480 -0
  68. package/src/drafts/draft14/session.ts +26 -0
  69. package/src/drafts/draft14/types.ts +365 -0
  70. package/src/index.ts +85 -0
  71. package/src/session.ts +58 -0
@@ -0,0 +1,307 @@
1
+ import {
2
+ CLIENT_ONLY_MESSAGES,
3
+ SERVER_ONLY_MESSAGES,
4
+ getLegalIncoming,
5
+ getLegalOutgoing
6
+ } from "./chunk-UOBWHJA5.js";
7
+
8
+ // src/drafts/draft07/session-fsm.ts
9
+ function violation(code, message, currentPhase, offendingMessage) {
10
+ return { code, message, currentPhase, offendingMessage };
11
+ }
12
+ var SessionFSM = class {
13
+ _phase = "idle";
14
+ _role;
15
+ _subscriptions = /* @__PURE__ */ new Map();
16
+ _announces = /* @__PURE__ */ new Map();
17
+ constructor(role) {
18
+ this._role = role;
19
+ }
20
+ get phase() {
21
+ return this._phase;
22
+ }
23
+ get role() {
24
+ return this._role;
25
+ }
26
+ get subscriptions() {
27
+ return this._subscriptions;
28
+ }
29
+ get announces() {
30
+ return this._announces;
31
+ }
32
+ get legalOutgoing() {
33
+ return getLegalOutgoing(this._phase, this._role);
34
+ }
35
+ get legalIncoming() {
36
+ return getLegalIncoming(this._phase, this._role);
37
+ }
38
+ // Validate role constraints
39
+ checkRole(message, direction) {
40
+ const senderRole = direction === "outbound" ? this._role : this._role === "client" ? "server" : "client";
41
+ if (CLIENT_ONLY_MESSAGES.has(message.type) && senderRole !== "client") {
42
+ return violation("ROLE_VIOLATION", `${message.type} can only be sent by client`, this._phase, message.type);
43
+ }
44
+ if (SERVER_ONLY_MESSAGES.has(message.type) && senderRole !== "server") {
45
+ return violation("ROLE_VIOLATION", `${message.type} can only be sent by server`, this._phase, message.type);
46
+ }
47
+ return null;
48
+ }
49
+ validateOutgoing(message) {
50
+ const roleViolation = this.checkRole(message, "outbound");
51
+ if (roleViolation) return { ok: false, violation: roleViolation };
52
+ if (!this.legalOutgoing.has(message.type)) {
53
+ return {
54
+ ok: false,
55
+ violation: violation(
56
+ this._phase === "idle" || this._phase === "setup" ? "MESSAGE_BEFORE_SETUP" : "UNEXPECTED_MESSAGE",
57
+ `Cannot send ${message.type} in phase ${this._phase}`,
58
+ this._phase,
59
+ message.type
60
+ )
61
+ };
62
+ }
63
+ return { ok: true };
64
+ }
65
+ receive(message) {
66
+ const roleViolation = this.checkRole(message, "inbound");
67
+ if (roleViolation) return { ok: false, violation: roleViolation };
68
+ return this.applyTransition(message, "inbound");
69
+ }
70
+ send(message) {
71
+ const roleViolation = this.checkRole(message, "outbound");
72
+ if (roleViolation) return { ok: false, violation: roleViolation };
73
+ return this.applyTransition(message, "outbound");
74
+ }
75
+ applyTransition(message, direction) {
76
+ const sideEffects = [];
77
+ switch (message.type) {
78
+ case "client_setup":
79
+ return this.handleClientSetup(message, direction);
80
+ case "server_setup":
81
+ return this.handleServerSetup(message, direction);
82
+ case "goaway":
83
+ return this.handleGoAway(message, direction, sideEffects);
84
+ // Subscription lifecycle
85
+ case "subscribe":
86
+ return this.handleSubscribe(message, direction, sideEffects);
87
+ case "subscribe_ok":
88
+ return this.handleSubscribeOk(message, direction, sideEffects);
89
+ case "subscribe_error":
90
+ return this.handleSubscribeError(message, direction, sideEffects);
91
+ case "subscribe_done":
92
+ return this.handleSubscribeDone(message, direction, sideEffects);
93
+ case "unsubscribe":
94
+ return this.handleUnsubscribe(message, direction, sideEffects);
95
+ // Announce lifecycle
96
+ case "announce":
97
+ return this.handleAnnounce(message, direction, sideEffects);
98
+ case "announce_ok":
99
+ return this.handleAnnounceOk(message, direction, sideEffects);
100
+ case "announce_error":
101
+ return this.handleAnnounceError(message, direction, sideEffects);
102
+ case "announce_cancel":
103
+ return this.handleAnnounceCancel(message, direction, sideEffects);
104
+ case "unannounce":
105
+ return this.handleUnannounce(message, direction, sideEffects);
106
+ // Fetch lifecycle
107
+ case "fetch":
108
+ case "fetch_ok":
109
+ case "fetch_error":
110
+ case "fetch_cancel":
111
+ return this.handleReadyPhaseMessage(message);
112
+ // Other ready-phase messages
113
+ default:
114
+ return this.handleReadyPhaseMessage(message);
115
+ }
116
+ }
117
+ handleClientSetup(_message, direction) {
118
+ if (this._phase !== "idle") {
119
+ return { ok: false, violation: violation("SETUP_VIOLATION", "CLIENT_SETUP already sent/received", this._phase, "client_setup") };
120
+ }
121
+ if (direction === "outbound" && this._role !== "client") {
122
+ return { ok: false, violation: violation("ROLE_VIOLATION", "Only client can send CLIENT_SETUP", this._phase, "client_setup") };
123
+ }
124
+ this._phase = "setup";
125
+ return { ok: true, phase: this._phase, sideEffects: [] };
126
+ }
127
+ handleServerSetup(_message, direction) {
128
+ if (this._phase !== "setup") {
129
+ return { ok: false, violation: violation("SETUP_VIOLATION", "SERVER_SETUP before CLIENT_SETUP", this._phase, "server_setup") };
130
+ }
131
+ if (direction === "outbound" && this._role !== "server") {
132
+ return { ok: false, violation: violation("ROLE_VIOLATION", "Only server can send SERVER_SETUP", this._phase, "server_setup") };
133
+ }
134
+ this._phase = "ready";
135
+ return { ok: true, phase: this._phase, sideEffects: [{ type: "session-ready" }] };
136
+ }
137
+ handleGoAway(message, _direction, sideEffects) {
138
+ if (this._phase !== "ready" && this._phase !== "draining") {
139
+ return { ok: false, violation: violation("UNEXPECTED_MESSAGE", `GOAWAY not valid in phase ${this._phase}`, this._phase, "goaway") };
140
+ }
141
+ this._phase = "draining";
142
+ const goaway = message;
143
+ sideEffects.push({ type: "session-draining", goAwayUri: goaway.newSessionUri });
144
+ return { ok: true, phase: this._phase, sideEffects };
145
+ }
146
+ requireReady(msgType) {
147
+ if (this._phase !== "ready" && this._phase !== "draining") {
148
+ return violation(
149
+ this._phase === "idle" || this._phase === "setup" ? "MESSAGE_BEFORE_SETUP" : "UNEXPECTED_MESSAGE",
150
+ `${msgType} requires ready phase, current: ${this._phase}`,
151
+ this._phase,
152
+ msgType
153
+ );
154
+ }
155
+ return null;
156
+ }
157
+ handleSubscribe(message, _direction, sideEffects) {
158
+ const err = this.requireReady(message.type);
159
+ if (err) return { ok: false, violation: err };
160
+ const sub = message;
161
+ if (this._subscriptions.has(sub.subscribeId)) {
162
+ return { ok: false, violation: violation("DUPLICATE_SUBSCRIBE_ID", `Subscribe ID ${sub.subscribeId} already exists`, this._phase, message.type) };
163
+ }
164
+ this._subscriptions.set(sub.subscribeId, {
165
+ subscribeId: sub.subscribeId,
166
+ phase: "pending",
167
+ trackNamespace: sub.trackNamespace,
168
+ trackName: sub.trackName
169
+ });
170
+ return { ok: true, phase: this._phase, sideEffects };
171
+ }
172
+ handleSubscribeOk(message, _direction, sideEffects) {
173
+ const err = this.requireReady(message.type);
174
+ if (err) return { ok: false, violation: err };
175
+ const ok = message;
176
+ const existing = this._subscriptions.get(ok.subscribeId);
177
+ if (!existing) {
178
+ return { ok: false, violation: violation("UNKNOWN_SUBSCRIBE_ID", `No subscription with ID ${ok.subscribeId}`, this._phase, message.type) };
179
+ }
180
+ if (existing.phase !== "pending") {
181
+ return { ok: false, violation: violation("STATE_VIOLATION", `Subscription ${ok.subscribeId} is ${existing.phase}, not pending`, this._phase, message.type) };
182
+ }
183
+ this._subscriptions.set(ok.subscribeId, { ...existing, phase: "active" });
184
+ sideEffects.push({ type: "subscription-activated", subscribeId: ok.subscribeId });
185
+ return { ok: true, phase: this._phase, sideEffects };
186
+ }
187
+ handleSubscribeError(message, _direction, sideEffects) {
188
+ const err = this.requireReady(message.type);
189
+ if (err) return { ok: false, violation: err };
190
+ const subErr = message;
191
+ const existing = this._subscriptions.get(subErr.subscribeId);
192
+ if (!existing) {
193
+ return { ok: false, violation: violation("UNKNOWN_SUBSCRIBE_ID", `No subscription with ID ${subErr.subscribeId}`, this._phase, message.type) };
194
+ }
195
+ if (existing.phase !== "pending") {
196
+ return { ok: false, violation: violation("STATE_VIOLATION", `Subscription ${subErr.subscribeId} is ${existing.phase}, not pending`, this._phase, message.type) };
197
+ }
198
+ this._subscriptions.set(subErr.subscribeId, { ...existing, phase: "error" });
199
+ sideEffects.push({ type: "subscription-ended", subscribeId: subErr.subscribeId, reason: subErr.reasonPhrase });
200
+ return { ok: true, phase: this._phase, sideEffects };
201
+ }
202
+ handleSubscribeDone(message, _direction, sideEffects) {
203
+ const err = this.requireReady(message.type);
204
+ if (err) return { ok: false, violation: err };
205
+ const done = message;
206
+ const existing = this._subscriptions.get(done.subscribeId);
207
+ if (!existing) {
208
+ return { ok: false, violation: violation("UNKNOWN_SUBSCRIBE_ID", `No subscription with ID ${done.subscribeId}`, this._phase, message.type) };
209
+ }
210
+ this._subscriptions.set(done.subscribeId, { ...existing, phase: "done" });
211
+ sideEffects.push({ type: "subscription-ended", subscribeId: done.subscribeId, reason: done.reasonPhrase });
212
+ return { ok: true, phase: this._phase, sideEffects };
213
+ }
214
+ handleUnsubscribe(message, _direction, sideEffects) {
215
+ const err = this.requireReady(message.type);
216
+ if (err) return { ok: false, violation: err };
217
+ const unsub = message;
218
+ const existing = this._subscriptions.get(unsub.subscribeId);
219
+ if (!existing) {
220
+ return { ok: false, violation: violation("UNKNOWN_SUBSCRIBE_ID", `No subscription with ID ${unsub.subscribeId}`, this._phase, message.type) };
221
+ }
222
+ this._subscriptions.set(unsub.subscribeId, { ...existing, phase: "done" });
223
+ sideEffects.push({ type: "subscription-ended", subscribeId: unsub.subscribeId, reason: "unsubscribed" });
224
+ return { ok: true, phase: this._phase, sideEffects };
225
+ }
226
+ // Announce handlers
227
+ namespaceKey(ns) {
228
+ return ns.join("/");
229
+ }
230
+ handleAnnounce(message, _direction, sideEffects) {
231
+ const err = this.requireReady(message.type);
232
+ if (err) return { ok: false, violation: err };
233
+ const ann = message;
234
+ const key = this.namespaceKey(ann.trackNamespace);
235
+ this._announces.set(key, { namespace: ann.trackNamespace, phase: "pending" });
236
+ return { ok: true, phase: this._phase, sideEffects };
237
+ }
238
+ handleAnnounceOk(message, _direction, sideEffects) {
239
+ const err = this.requireReady(message.type);
240
+ if (err) return { ok: false, violation: err };
241
+ const ok = message;
242
+ const key = this.namespaceKey(ok.trackNamespace);
243
+ const existing = this._announces.get(key);
244
+ if (!existing) {
245
+ return { ok: false, violation: violation("UNEXPECTED_MESSAGE", `No announce for namespace ${key}`, this._phase, message.type) };
246
+ }
247
+ this._announces.set(key, { ...existing, phase: "active" });
248
+ sideEffects.push({ type: "announce-activated", namespace: ok.trackNamespace });
249
+ return { ok: true, phase: this._phase, sideEffects };
250
+ }
251
+ handleAnnounceError(message, _direction, sideEffects) {
252
+ const err = this.requireReady(message.type);
253
+ if (err) return { ok: false, violation: err };
254
+ const annErr = message;
255
+ const key = this.namespaceKey(annErr.trackNamespace);
256
+ const existing = this._announces.get(key);
257
+ if (!existing) {
258
+ return { ok: false, violation: violation("UNEXPECTED_MESSAGE", `No announce for namespace ${key}`, this._phase, message.type) };
259
+ }
260
+ this._announces.set(key, { ...existing, phase: "error" });
261
+ sideEffects.push({ type: "announce-ended", namespace: annErr.trackNamespace });
262
+ return { ok: true, phase: this._phase, sideEffects };
263
+ }
264
+ handleAnnounceCancel(message, _direction, sideEffects) {
265
+ const err = this.requireReady(message.type);
266
+ if (err) return { ok: false, violation: err };
267
+ const cancel = message;
268
+ const key = this.namespaceKey(cancel.trackNamespace);
269
+ const existing = this._announces.get(key);
270
+ if (existing) {
271
+ this._announces.delete(key);
272
+ sideEffects.push({ type: "announce-ended", namespace: cancel.trackNamespace });
273
+ }
274
+ return { ok: true, phase: this._phase, sideEffects };
275
+ }
276
+ handleUnannounce(message, _direction, sideEffects) {
277
+ const err = this.requireReady(message.type);
278
+ if (err) return { ok: false, violation: err };
279
+ const unann = message;
280
+ const key = this.namespaceKey(unann.trackNamespace);
281
+ const existing = this._announces.get(key);
282
+ if (existing) {
283
+ this._announces.delete(key);
284
+ sideEffects.push({ type: "announce-ended", namespace: unann.trackNamespace });
285
+ }
286
+ return { ok: true, phase: this._phase, sideEffects };
287
+ }
288
+ handleReadyPhaseMessage(message) {
289
+ const err = this.requireReady(message.type);
290
+ if (err) return { ok: false, violation: err };
291
+ return { ok: true, phase: this._phase, sideEffects: [] };
292
+ }
293
+ reset() {
294
+ this._phase = "idle";
295
+ this._subscriptions.clear();
296
+ this._announces.clear();
297
+ }
298
+ };
299
+
300
+ // src/drafts/draft07/session.ts
301
+ function createDraft07SessionState(options) {
302
+ return new SessionFSM(options.role);
303
+ }
304
+
305
+ export {
306
+ createDraft07SessionState
307
+ };