@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,194 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/core/types.ts
2
+ var DecodeError = class extends Error {
3
+
4
+
5
+ constructor(code, message, offset) {
6
+ super(message);
7
+ this.name = "DecodeError";
8
+ this.code = code;
9
+ this.offset = offset;
10
+ }
11
+ };
12
+
13
+ // src/core/buffer-reader.ts
14
+ var BufferReader = class {
15
+ constructor(buffer, offset = 0) {
16
+ this.buffer = buffer;
17
+ this.view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
18
+ this.pos = offset;
19
+ }
20
+
21
+
22
+ get offset() {
23
+ return this.pos;
24
+ }
25
+ get remaining() {
26
+ return this.buffer.byteLength - this.pos;
27
+ }
28
+ readUint8() {
29
+ if (this.remaining < 1) {
30
+ throw new DecodeError("UNEXPECTED_END", "Not enough bytes to read uint8", this.pos);
31
+ }
32
+ const value = this.view.getUint8(this.pos);
33
+ this.pos += 1;
34
+ return value;
35
+ }
36
+ readBytes(length) {
37
+ if (this.remaining < length) {
38
+ throw new DecodeError("UNEXPECTED_END", `Not enough bytes: need ${length}, have ${this.remaining}`, this.pos);
39
+ }
40
+ const slice = this.buffer.slice(this.pos, this.pos + length);
41
+ this.pos += length;
42
+ return slice;
43
+ }
44
+ readVarInt() {
45
+ if (this.remaining < 1) {
46
+ throw new DecodeError("UNEXPECTED_END", "Not enough bytes for varint", this.pos);
47
+ }
48
+ const first = this.view.getUint8(this.pos);
49
+ const prefix = first >> 6;
50
+ let length;
51
+ let value;
52
+ switch (prefix) {
53
+ case 0:
54
+ length = 1;
55
+ value = BigInt(first & 63);
56
+ break;
57
+ case 1:
58
+ length = 2;
59
+ if (this.remaining < 2) {
60
+ throw new DecodeError("UNEXPECTED_END", "Not enough bytes for 2-byte varint", this.pos);
61
+ }
62
+ value = BigInt(this.view.getUint16(this.pos) & 16383);
63
+ break;
64
+ case 2:
65
+ length = 4;
66
+ if (this.remaining < 4) {
67
+ throw new DecodeError("UNEXPECTED_END", "Not enough bytes for 4-byte varint", this.pos);
68
+ }
69
+ value = BigInt(this.view.getUint32(this.pos)) & 0x3fffffffn;
70
+ break;
71
+ case 3:
72
+ length = 8;
73
+ if (this.remaining < 8) {
74
+ throw new DecodeError("UNEXPECTED_END", "Not enough bytes for 8-byte varint", this.pos);
75
+ }
76
+ value = this.view.getBigUint64(this.pos) & 0x3fffffffffffffffn;
77
+ break;
78
+ default:
79
+ throw new DecodeError("INVALID_VARINT", "Invalid varint prefix", this.pos);
80
+ }
81
+ this.pos += length;
82
+ return value;
83
+ }
84
+ readString() {
85
+ const length = Number(this.readVarInt());
86
+ const bytes = this.readBytes(length);
87
+ return new TextDecoder().decode(bytes);
88
+ }
89
+ readTuple() {
90
+ const count = Number(this.readVarInt());
91
+ const result = [];
92
+ for (let i = 0; i < count; i++) {
93
+ result.push(this.readString());
94
+ }
95
+ return result;
96
+ }
97
+ readParameters() {
98
+ const count = Number(this.readVarInt());
99
+ const params = /* @__PURE__ */ new Map();
100
+ for (let i = 0; i < count; i++) {
101
+ const key = this.readVarInt();
102
+ const length = Number(this.readVarInt());
103
+ const value = this.readBytes(length);
104
+ params.set(key, value);
105
+ }
106
+ return params;
107
+ }
108
+ };
109
+
110
+ // src/core/buffer-writer.ts
111
+ var BufferWriter = class {
112
+
113
+
114
+
115
+ constructor(initialSize = 256) {
116
+ this.buffer = new Uint8Array(initialSize);
117
+ this.view = new DataView(this.buffer.buffer);
118
+ this.pos = 0;
119
+ }
120
+ get offset() {
121
+ return this.pos;
122
+ }
123
+ ensureCapacity(needed) {
124
+ const required = this.pos + needed;
125
+ if (required <= this.buffer.byteLength) return;
126
+ let newSize = this.buffer.byteLength * 2;
127
+ while (newSize < required) newSize *= 2;
128
+ const newBuffer = new Uint8Array(newSize);
129
+ newBuffer.set(this.buffer);
130
+ this.buffer = newBuffer;
131
+ this.view = new DataView(this.buffer.buffer);
132
+ }
133
+ writeUint8(value) {
134
+ this.ensureCapacity(1);
135
+ this.view.setUint8(this.pos, value);
136
+ this.pos += 1;
137
+ }
138
+ writeBytes(bytes) {
139
+ this.ensureCapacity(bytes.byteLength);
140
+ this.buffer.set(bytes, this.pos);
141
+ this.pos += bytes.byteLength;
142
+ }
143
+ writeVarInt(value) {
144
+ const v = BigInt(value);
145
+ if (v < 0n) throw new Error("VarInt value must be non-negative");
146
+ if (v < 0x40n) {
147
+ this.ensureCapacity(1);
148
+ this.view.setUint8(this.pos, Number(v));
149
+ this.pos += 1;
150
+ } else if (v < 0x4000n) {
151
+ this.ensureCapacity(2);
152
+ this.view.setUint16(this.pos, Number(v) | 16384);
153
+ this.pos += 2;
154
+ } else if (v < 0x40000000n) {
155
+ this.ensureCapacity(4);
156
+ this.view.setUint32(this.pos, Number(v) | 2147483648);
157
+ this.pos += 4;
158
+ } else if (v < 0x4000000000000000n) {
159
+ this.ensureCapacity(8);
160
+ this.view.setBigUint64(this.pos, v | 0xc000000000000000n);
161
+ this.pos += 8;
162
+ } else {
163
+ throw new Error("VarInt value exceeds 62-bit range");
164
+ }
165
+ }
166
+ writeString(str) {
167
+ const encoded = new TextEncoder().encode(str);
168
+ this.writeVarInt(encoded.byteLength);
169
+ this.writeBytes(encoded);
170
+ }
171
+ writeTuple(values) {
172
+ this.writeVarInt(values.length);
173
+ for (const v of values) {
174
+ this.writeString(v);
175
+ }
176
+ }
177
+ writeParameters(params) {
178
+ this.writeVarInt(params.size);
179
+ for (const [key, value] of params) {
180
+ this.writeVarInt(key);
181
+ this.writeVarInt(value.byteLength);
182
+ this.writeBytes(value);
183
+ }
184
+ }
185
+ finish() {
186
+ return this.buffer.slice(0, this.pos);
187
+ }
188
+ };
189
+
190
+
191
+
192
+
193
+
194
+ exports.DecodeError = DecodeError; exports.BufferReader = BufferReader; exports.BufferWriter = BufferWriter;
@@ -0,0 +1,307 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); var _class;
2
+
3
+
4
+
5
+
6
+ var _chunkQYG6KGOVcjs = require('./chunk-QYG6KGOV.cjs');
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 = class {
13
+ __init() {this._phase = "idle"}
14
+
15
+ __init2() {this._subscriptions = /* @__PURE__ */ new Map()}
16
+ __init3() {this._announces = /* @__PURE__ */ new Map()}
17
+ constructor(role) {;_class.prototype.__init.call(this);_class.prototype.__init2.call(this);_class.prototype.__init3.call(this);
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 _chunkQYG6KGOVcjs.getLegalOutgoing.call(void 0, this._phase, this._role);
34
+ }
35
+ get legalIncoming() {
36
+ return _chunkQYG6KGOVcjs.getLegalIncoming.call(void 0, 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 (_chunkQYG6KGOVcjs.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 (_chunkQYG6KGOVcjs.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
+ }, _class);
299
+
300
+ // src/drafts/draft07/session.ts
301
+ function createDraft07SessionState(options) {
302
+ return new SessionFSM(options.role);
303
+ }
304
+
305
+
306
+
307
+ exports.createDraft07SessionState = createDraft07SessionState;