@moq/net 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 (254) hide show
  1. package/README.md +70 -0
  2. package/announced.d.ts +41 -0
  3. package/announced.d.ts.map +1 -0
  4. package/announced.js +65 -0
  5. package/announced.js.map +1 -0
  6. package/bandwidth.d.ts +11 -0
  7. package/bandwidth.d.ts.map +1 -0
  8. package/bandwidth.js +6 -0
  9. package/bandwidth.js.map +1 -0
  10. package/broadcast.d.ts +35 -0
  11. package/broadcast.d.ts.map +1 -0
  12. package/broadcast.js +72 -0
  13. package/broadcast.js.map +1 -0
  14. package/connection/accept.d.ts +14 -0
  15. package/connection/accept.d.ts.map +1 -0
  16. package/connection/accept.js +133 -0
  17. package/connection/accept.js.map +1 -0
  18. package/connection/connect.d.ts +19 -0
  19. package/connection/connect.d.ts.map +1 -0
  20. package/connection/connect.js +284 -0
  21. package/connection/connect.js.map +1 -0
  22. package/connection/established.d.ts +22 -0
  23. package/connection/established.d.ts.map +1 -0
  24. package/connection/established.js +2 -0
  25. package/connection/established.js.map +1 -0
  26. package/connection/handshake.d.ts +10 -0
  27. package/connection/handshake.d.ts.map +1 -0
  28. package/connection/handshake.js +47 -0
  29. package/connection/handshake.js.map +1 -0
  30. package/connection/index.d.ts +5 -0
  31. package/connection/index.d.ts.map +1 -0
  32. package/connection/index.js +5 -0
  33. package/connection/index.js.map +1 -0
  34. package/connection/reload.d.ts +32 -0
  35. package/connection/reload.d.ts.map +1 -0
  36. package/connection/reload.js +127 -0
  37. package/connection/reload.js.map +1 -0
  38. package/group.d.ts +34 -0
  39. package/group.d.ts.map +1 -0
  40. package/group.js +92 -0
  41. package/group.js.map +1 -0
  42. package/ietf/adapter.d.ts +65 -0
  43. package/ietf/adapter.d.ts.map +1 -0
  44. package/ietf/adapter.js +625 -0
  45. package/ietf/adapter.js.map +1 -0
  46. package/ietf/connection.d.ts +65 -0
  47. package/ietf/connection.d.ts.map +1 -0
  48. package/ietf/connection.js +239 -0
  49. package/ietf/connection.js.map +1 -0
  50. package/ietf/control.d.ts +135 -0
  51. package/ietf/control.d.ts.map +1 -0
  52. package/ietf/control.js +237 -0
  53. package/ietf/control.js.map +1 -0
  54. package/ietf/fetch.d.ts +64 -0
  55. package/ietf/fetch.d.ts.map +1 -0
  56. package/ietf/fetch.js +99 -0
  57. package/ietf/fetch.js.map +1 -0
  58. package/ietf/goaway.d.ts +15 -0
  59. package/ietf/goaway.d.ts.map +1 -0
  60. package/ietf/goaway.js +33 -0
  61. package/ietf/goaway.js.map +1 -0
  62. package/ietf/ietf.test.d.ts +2 -0
  63. package/ietf/ietf.test.d.ts.map +1 -0
  64. package/ietf/ietf.test.js +800 -0
  65. package/ietf/ietf.test.js.map +1 -0
  66. package/ietf/index.d.ts +18 -0
  67. package/ietf/index.d.ts.map +1 -0
  68. package/ietf/index.js +18 -0
  69. package/ietf/index.js.map +1 -0
  70. package/ietf/message.d.ts +4 -0
  71. package/ietf/message.d.ts.map +1 -0
  72. package/ietf/message.js +53 -0
  73. package/ietf/message.js.map +1 -0
  74. package/ietf/namespace.d.ts +5 -0
  75. package/ietf/namespace.d.ts.map +1 -0
  76. package/ietf/namespace.js +21 -0
  77. package/ietf/namespace.js.map +1 -0
  78. package/ietf/object.d.ts +37 -0
  79. package/ietf/object.d.ts.map +1 -0
  80. package/ietf/object.js +129 -0
  81. package/ietf/object.js.map +1 -0
  82. package/ietf/parameters.d.ts +56 -0
  83. package/ietf/parameters.d.ts.map +1 -0
  84. package/ietf/parameters.js +336 -0
  85. package/ietf/parameters.js.map +1 -0
  86. package/ietf/properties.d.ts +4 -0
  87. package/ietf/properties.d.ts.map +1 -0
  88. package/ietf/properties.js +32 -0
  89. package/ietf/properties.js.map +1 -0
  90. package/ietf/publish.d.ts +68 -0
  91. package/ietf/publish.d.ts.map +1 -0
  92. package/ietf/publish.js +191 -0
  93. package/ietf/publish.js.map +1 -0
  94. package/ietf/publish_namespace.d.ts +68 -0
  95. package/ietf/publish_namespace.d.ts.map +1 -0
  96. package/ietf/publish_namespace.js +175 -0
  97. package/ietf/publish_namespace.js.map +1 -0
  98. package/ietf/publisher.d.ts +50 -0
  99. package/ietf/publisher.d.ts.map +1 -0
  100. package/ietf/publisher.js +329 -0
  101. package/ietf/publisher.js.map +1 -0
  102. package/ietf/request.d.ts +52 -0
  103. package/ietf/request.d.ts.map +1 -0
  104. package/ietf/request.js +118 -0
  105. package/ietf/request.js.map +1 -0
  106. package/ietf/setup.d.ts +38 -0
  107. package/ietf/setup.d.ts.map +1 -0
  108. package/ietf/setup.js +128 -0
  109. package/ietf/setup.js.map +1 -0
  110. package/ietf/subscribe.d.ts +66 -0
  111. package/ietf/subscribe.d.ts.map +1 -0
  112. package/ietf/subscribe.js +275 -0
  113. package/ietf/subscribe.js.map +1 -0
  114. package/ietf/subscribe_namespace.d.ts +89 -0
  115. package/ietf/subscribe_namespace.d.ts.map +1 -0
  116. package/ietf/subscribe_namespace.js +195 -0
  117. package/ietf/subscribe_namespace.js.map +1 -0
  118. package/ietf/subscriber.d.ts +53 -0
  119. package/ietf/subscriber.d.ts.map +1 -0
  120. package/ietf/subscriber.js +395 -0
  121. package/ietf/subscriber.js.map +1 -0
  122. package/ietf/track.d.ts +40 -0
  123. package/ietf/track.d.ts.map +1 -0
  124. package/ietf/track.js +107 -0
  125. package/ietf/track.js.map +1 -0
  126. package/ietf/version.d.ts +50 -0
  127. package/ietf/version.d.ts.map +1 -0
  128. package/ietf/version.js +55 -0
  129. package/ietf/version.js.map +1 -0
  130. package/index.d.ts +11 -0
  131. package/index.d.ts.map +1 -0
  132. package/index.js +16 -0
  133. package/index.js.map +1 -0
  134. package/integration.test.d.ts +2 -0
  135. package/integration.test.d.ts.map +1 -0
  136. package/integration.test.js +83 -0
  137. package/integration.test.js.map +1 -0
  138. package/lite/announce.d.ts +35 -0
  139. package/lite/announce.d.ts.map +1 -0
  140. package/lite/announce.js +165 -0
  141. package/lite/announce.js.map +1 -0
  142. package/lite/connection.d.ts +47 -0
  143. package/lite/connection.d.ts.map +1 -0
  144. package/lite/connection.js +218 -0
  145. package/lite/connection.js.map +1 -0
  146. package/lite/fetch.d.ts +14 -0
  147. package/lite/fetch.d.ts.map +1 -0
  148. package/lite/fetch.js +46 -0
  149. package/lite/fetch.js.map +1 -0
  150. package/lite/goaway.d.ts +10 -0
  151. package/lite/goaway.d.ts.map +1 -0
  152. package/lite/goaway.js +36 -0
  153. package/lite/goaway.js.map +1 -0
  154. package/lite/group.d.ts +18 -0
  155. package/lite/group.d.ts.map +1 -0
  156. package/lite/group.js +45 -0
  157. package/lite/group.js.map +1 -0
  158. package/lite/index.d.ts +11 -0
  159. package/lite/index.d.ts.map +1 -0
  160. package/lite/index.js +11 -0
  161. package/lite/index.js.map +1 -0
  162. package/lite/message.d.ts +5 -0
  163. package/lite/message.d.ts.map +1 -0
  164. package/lite/message.js +50 -0
  165. package/lite/message.js.map +1 -0
  166. package/lite/origin.d.ts +19 -0
  167. package/lite/origin.d.ts.map +1 -0
  168. package/lite/origin.js +28 -0
  169. package/lite/origin.js.map +1 -0
  170. package/lite/probe.d.ts +12 -0
  171. package/lite/probe.d.ts.map +1 -0
  172. package/lite/probe.js +59 -0
  173. package/lite/probe.js.map +1 -0
  174. package/lite/publisher.d.ts +56 -0
  175. package/lite/publisher.d.ts.map +1 -0
  176. package/lite/publisher.js +305 -0
  177. package/lite/publisher.js.map +1 -0
  178. package/lite/session.d.ts +36 -0
  179. package/lite/session.d.ts.map +1 -0
  180. package/lite/session.js +129 -0
  181. package/lite/session.js.map +1 -0
  182. package/lite/stream.d.ts +18 -0
  183. package/lite/stream.d.ts.map +1 -0
  184. package/lite/stream.js +11 -0
  185. package/lite/stream.js.map +1 -0
  186. package/lite/subscribe.d.ts +91 -0
  187. package/lite/subscribe.d.ts.map +1 -0
  188. package/lite/subscribe.js +267 -0
  189. package/lite/subscribe.js.map +1 -0
  190. package/lite/subscriber.d.ts +80 -0
  191. package/lite/subscriber.d.ts.map +1 -0
  192. package/lite/subscriber.js +245 -0
  193. package/lite/subscriber.js.map +1 -0
  194. package/lite/version.d.ts +12 -0
  195. package/lite/version.d.ts.map +1 -0
  196. package/lite/version.js +23 -0
  197. package/lite/version.js.map +1 -0
  198. package/mock.d.ts +16 -0
  199. package/mock.d.ts.map +1 -0
  200. package/mock.js +153 -0
  201. package/mock.js.map +1 -0
  202. package/package.json +27 -0
  203. package/path.d.ts +87 -0
  204. package/path.d.ts.map +1 -0
  205. package/path.js +98 -0
  206. package/path.js.map +1 -0
  207. package/path.test.d.ts +2 -0
  208. package/path.test.d.ts.map +1 -0
  209. package/path.test.js +153 -0
  210. package/path.test.js.map +1 -0
  211. package/stream.d.ts +57 -0
  212. package/stream.d.ts.map +1 -0
  213. package/stream.js +325 -0
  214. package/stream.js.map +1 -0
  215. package/stream.test.d.ts +2 -0
  216. package/stream.test.d.ts.map +1 -0
  217. package/stream.test.js +209 -0
  218. package/stream.test.js.map +1 -0
  219. package/time.d.ts +77 -0
  220. package/time.d.ts.map +1 -0
  221. package/time.js +65 -0
  222. package/time.js.map +1 -0
  223. package/track.d.ts +66 -0
  224. package/track.d.ts.map +1 -0
  225. package/track.js +203 -0
  226. package/track.js.map +1 -0
  227. package/track.test.d.ts +2 -0
  228. package/track.test.d.ts.map +1 -0
  229. package/track.test.js +39 -0
  230. package/track.test.js.map +1 -0
  231. package/util/error.d.ts +3 -0
  232. package/util/error.d.ts.map +1 -0
  233. package/util/error.js +8 -0
  234. package/util/error.js.map +1 -0
  235. package/util/hex.d.ts +2 -0
  236. package/util/hex.d.ts.map +1 -0
  237. package/util/hex.js +12 -0
  238. package/util/hex.js.map +1 -0
  239. package/util/index.d.ts +2 -0
  240. package/util/index.d.ts.map +1 -0
  241. package/util/index.js +2 -0
  242. package/util/index.js.map +1 -0
  243. package/varint.d.ts +28 -0
  244. package/varint.d.ts.map +1 -0
  245. package/varint.js +279 -0
  246. package/varint.js.map +1 -0
  247. package/varint.test.d.ts +2 -0
  248. package/varint.test.d.ts.map +1 -0
  249. package/varint.test.js +105 -0
  250. package/varint.test.js.map +1 -0
  251. package/zod.d.ts +6 -0
  252. package/zod.d.ts.map +1 -0
  253. package/zod.js +12 -0
  254. package/zod.js.map +1 -0
@@ -0,0 +1,625 @@
1
+ import { Mutex } from "async-mutex";
2
+ import { Reader, Stream } from "../stream.js";
3
+ import * as Varint from "../varint.js";
4
+ import * as Namespace from "./namespace.js";
5
+ import { Version } from "./version.js";
6
+ /**
7
+ * v17 native session — thin wrapper around WebTransport.
8
+ * Each request gets its own real bidi stream; no control stream multiplexing.
9
+ */
10
+ export class NativeSession {
11
+ #quic;
12
+ #requestId = 0n;
13
+ version;
14
+ constructor(quic, version) {
15
+ this.#quic = quic;
16
+ this.version = version;
17
+ }
18
+ async openBi() {
19
+ return Stream.open(this.#quic, this.version);
20
+ }
21
+ async acceptBi() {
22
+ return Stream.accept(this.#quic, this.version);
23
+ }
24
+ async nextRequestId() {
25
+ const id = this.#requestId;
26
+ this.#requestId += 2n;
27
+ return id;
28
+ }
29
+ }
30
+ // Route classification for control stream messages.
31
+ const Route = {
32
+ NewRequest: 0, // Create virtual bidi stream, push initial message
33
+ Response: 1, // Push message to existing stream (keep open)
34
+ ErrorResponse: 2, // Push message to existing stream, then close
35
+ CloseStream: 3, // Close stream recv (no bytes pushed)
36
+ FollowUp: 4, // Push follow-up message to existing stream
37
+ MaxRequestId: 5, // Update flow control
38
+ Ignore: 6, // Connection-level, no routing
39
+ GoAway: 7, // Terminal
40
+ };
41
+ /**
42
+ * Converts v14-v16 control stream multiplexing into virtual bidi streams.
43
+ *
44
+ * Reads control messages, classifies them, and routes to virtual Stream objects.
45
+ * Each request/response pair gets its own virtual Stream, making all versions
46
+ * look like v17's stream-per-request model.
47
+ */
48
+ export class ControlStreamAdapter {
49
+ // WebTransport session (for opening real bidi streams in v16)
50
+ #quic;
51
+ // Control stream
52
+ #reader;
53
+ #writer;
54
+ #writeMutex = new Mutex();
55
+ version;
56
+ // Virtual streams keyed by requestId
57
+ #streams = new Map();
58
+ // Namespace → requestId reverse lookup (v14/v15 namespace-keyed messages)
59
+ #namespaces = new Map();
60
+ // requestId → namespace reverse lookup (for cleanup in #closeStream)
61
+ #namespacesByRequestId = new Map();
62
+ // SubscribeNamespace requestIds — for routing 0x08/0x0E entries that lack requestId (v14/v15)
63
+ #subscribeNamespaces = new Set();
64
+ // Incoming stream queue (for acceptBi)
65
+ #incomingQueue = [];
66
+ #incomingWaiters = [];
67
+ // Request ID flow control
68
+ #requestId = 0n;
69
+ #maxRequestId;
70
+ #maxRequestIdResolves = [];
71
+ #closed = false;
72
+ constructor(quic, controlStream, version, maxRequestId) {
73
+ this.#quic = quic;
74
+ this.#reader = controlStream.reader;
75
+ this.#reader.version = version;
76
+ this.#writer = controlStream.writer;
77
+ this.#writer.version = version;
78
+ this.version = version;
79
+ this.#maxRequestId = maxRequestId;
80
+ }
81
+ /**
82
+ * Accept the next incoming virtual bidi stream.
83
+ * Blocks until a new request arrives on the control stream.
84
+ */
85
+ async acceptBi() {
86
+ if (this.#closed)
87
+ return undefined;
88
+ const queued = this.#incomingQueue.shift();
89
+ if (queued)
90
+ return queued;
91
+ return new Promise((resolve) => {
92
+ this.#incomingWaiters.push(resolve);
93
+ });
94
+ }
95
+ /**
96
+ * Open an outgoing virtual bidi stream.
97
+ * Buffers writes until the first full message is available, parses the
98
+ * requestId (and namespace for PublishNamespace), self-registers, then
99
+ * flushes. Subsequent writes go directly to the control stream.
100
+ */
101
+ openBi() {
102
+ let controller;
103
+ let registeredRequestId;
104
+ const readable = new ReadableStream({
105
+ start(c) {
106
+ controller = c;
107
+ },
108
+ cancel: () => {
109
+ if (registeredRequestId !== undefined) {
110
+ this.#streams.delete(registeredRequestId);
111
+ }
112
+ },
113
+ });
114
+ let buffer = new Uint8Array(0);
115
+ let registered = false;
116
+ const sendWritable = new WritableStream({
117
+ write: async (chunk) => {
118
+ // Always accumulate bytes and flush only complete messages
119
+ // to prevent interleaving of partial messages from concurrent virtual streams.
120
+ const newBuf = new Uint8Array(buffer.length + chunk.length);
121
+ newBuf.set(buffer);
122
+ newBuf.set(chunk, buffer.length);
123
+ buffer = newBuf;
124
+ // Try to flush complete messages from the buffer
125
+ for (;;) {
126
+ const boundary = this.#messageSize(buffer);
127
+ if (boundary === undefined)
128
+ break;
129
+ const toFlush = buffer.subarray(0, boundary);
130
+ buffer = buffer.subarray(boundary);
131
+ if (!registered) {
132
+ // First message: extract requestId and register before flushing
133
+ const parsed = this.#tryParseOutgoing(toFlush);
134
+ if (parsed) {
135
+ registeredRequestId = parsed.requestId;
136
+ this.#streams.set(parsed.requestId, { controller });
137
+ registered = true;
138
+ }
139
+ }
140
+ await this.#writeMutex.runExclusive(() => this.#writer.write(toFlush));
141
+ }
142
+ },
143
+ });
144
+ const stream = new Stream({ readable, writable: sendWritable });
145
+ stream.reader.version = this.version;
146
+ stream.writer.version = this.version;
147
+ return stream;
148
+ }
149
+ /**
150
+ * Open a real WebTransport bidi stream (for v16 SubscribeNamespace).
151
+ */
152
+ async openNativeBi() {
153
+ return Stream.open(this.#quic, this.version);
154
+ }
155
+ /**
156
+ * Allocate the next request ID, blocking if flow control limit reached.
157
+ */
158
+ async nextRequestId() {
159
+ for (;;) {
160
+ if (this.#closed)
161
+ return undefined;
162
+ const id = this.#requestId;
163
+ if (id < this.#maxRequestId) {
164
+ this.#requestId += 2n;
165
+ return id;
166
+ }
167
+ await new Promise((resolve) => {
168
+ this.#maxRequestIdResolves.push(resolve);
169
+ });
170
+ }
171
+ }
172
+ /**
173
+ * Main run loop — reads control stream messages and routes to virtual streams.
174
+ * Must be called after construction. Runs until the control stream closes.
175
+ */
176
+ async run() {
177
+ try {
178
+ // v16: also accept real bidi streams (for SubscribeNamespace)
179
+ if (this.version === Version.DRAFT_16) {
180
+ void this.#acceptNativeBidis();
181
+ }
182
+ for (;;) {
183
+ const done = await this.#reader.done();
184
+ if (done)
185
+ break;
186
+ const typeId = await this.#reader.u53();
187
+ const size = await this.#reader.u16();
188
+ const body = await this.#reader.read(size);
189
+ const classified = await this.#classify(typeId, body);
190
+ if (classified.route === Route.GoAway) {
191
+ console.warn("received GOAWAY on control stream");
192
+ return;
193
+ }
194
+ const { route, requestId } = classified;
195
+ switch (route) {
196
+ case Route.NewRequest:
197
+ this.#newRequest(typeId, size, body, requestId);
198
+ break;
199
+ case Route.Response:
200
+ this.#pushMessage(requestId, typeId, size, body);
201
+ break;
202
+ case Route.ErrorResponse:
203
+ this.#pushMessage(requestId, typeId, size, body);
204
+ this.#closeStream(requestId);
205
+ break;
206
+ case Route.CloseStream:
207
+ this.#closeStream(requestId);
208
+ break;
209
+ case Route.FollowUp:
210
+ this.#pushMessage(requestId, typeId, size, body);
211
+ break;
212
+ case Route.MaxRequestId:
213
+ this.#maxRequestId = requestId;
214
+ for (const resolve of this.#maxRequestIdResolves)
215
+ resolve();
216
+ this.#maxRequestIdResolves = [];
217
+ break;
218
+ }
219
+ }
220
+ }
221
+ finally {
222
+ this.close();
223
+ }
224
+ }
225
+ /** Accept real WebTransport bidi streams and queue them for acceptBi (v16). */
226
+ async #acceptNativeBidis() {
227
+ try {
228
+ for (;;) {
229
+ const stream = await Stream.accept(this.#quic, this.version);
230
+ if (!stream)
231
+ break;
232
+ const waiter = this.#incomingWaiters.shift();
233
+ if (waiter) {
234
+ waiter(stream);
235
+ }
236
+ else {
237
+ this.#incomingQueue.push(stream);
238
+ }
239
+ }
240
+ }
241
+ catch {
242
+ // Session closed
243
+ }
244
+ }
245
+ #newRequest(typeId, size, body, requestId) {
246
+ let controller;
247
+ const readable = new ReadableStream({
248
+ start(c) {
249
+ controller = c;
250
+ },
251
+ cancel: () => {
252
+ this.#streams.delete(requestId);
253
+ },
254
+ });
255
+ const sendWritable = this.#createSendWritable();
256
+ const stream = new Stream({ readable, writable: sendWritable });
257
+ stream.reader.version = this.version;
258
+ stream.writer.version = this.version;
259
+ this.#streams.set(requestId, { controller });
260
+ // Push initial message bytes so the dispatcher can read typeId + decode
261
+ controller.enqueue(this.#encodeRaw(typeId, size, body));
262
+ // Queue for acceptBi
263
+ const waiter = this.#incomingWaiters.shift();
264
+ if (waiter) {
265
+ waiter(stream);
266
+ }
267
+ else {
268
+ this.#incomingQueue.push(stream);
269
+ }
270
+ }
271
+ #pushMessage(requestId, typeId, size, body) {
272
+ const entry = this.#streams.get(requestId);
273
+ if (!entry) {
274
+ console.warn(`adapter: no stream for requestId=${requestId} typeId=0x${typeId.toString(16)}`);
275
+ return;
276
+ }
277
+ try {
278
+ entry.controller.enqueue(this.#encodeRaw(typeId, size, body));
279
+ }
280
+ catch {
281
+ // Stream already closed
282
+ }
283
+ }
284
+ #closeStream(requestId) {
285
+ const entry = this.#streams.get(requestId);
286
+ if (!entry)
287
+ return;
288
+ console.debug(`adapter: closing stream requestId=${requestId}`);
289
+ this.#streams.delete(requestId);
290
+ this.#subscribeNamespaces.delete(requestId);
291
+ const namespace = this.#namespacesByRequestId.get(requestId);
292
+ if (namespace !== undefined) {
293
+ this.#namespaces.delete(namespace);
294
+ this.#namespacesByRequestId.delete(requestId);
295
+ }
296
+ try {
297
+ entry.controller.close();
298
+ }
299
+ catch {
300
+ // Already closed
301
+ }
302
+ }
303
+ /**
304
+ * Returns the total byte size of the first complete message in buffer,
305
+ * or undefined if the buffer doesn't contain a complete message yet.
306
+ * Message format: [typeId varint][size u16 BE][body of `size` bytes]
307
+ */
308
+ #messageSize(buffer) {
309
+ if (buffer.length === 0)
310
+ return undefined;
311
+ const typeSize = 1 << ((buffer[0] & 0xc0) >> 6);
312
+ if (buffer.length < typeSize)
313
+ return undefined;
314
+ const [, afterType] = Varint.decode(buffer);
315
+ if (afterType.length < 2)
316
+ return undefined;
317
+ const size = (afterType[0] << 8) | afterType[1];
318
+ const totalSize = buffer.length - afterType.length + 2 + size;
319
+ if (buffer.length < totalSize)
320
+ return undefined;
321
+ return totalSize;
322
+ }
323
+ /**
324
+ * Try to parse the first outgoing message from accumulated bytes.
325
+ * Returns the requestId if enough data is available, undefined otherwise.
326
+ */
327
+ #tryParseOutgoing(buffer) {
328
+ if (buffer.length === 0)
329
+ return undefined;
330
+ // Check typeId varint size before decoding
331
+ const typeSize = 1 << ((buffer[0] & 0xc0) >> 6);
332
+ if (buffer.length < typeSize)
333
+ return undefined;
334
+ const [typeId, afterType] = Varint.decode(buffer);
335
+ // Need 2 bytes for u16 size
336
+ if (afterType.length < 2)
337
+ return undefined;
338
+ const size = (afterType[0] << 8) | afterType[1];
339
+ const bodyStart = afterType.subarray(2);
340
+ // Need full body
341
+ if (bodyStart.length < size)
342
+ return undefined;
343
+ const body = bodyStart.subarray(0, size);
344
+ // Decode requestId from body (QUIC varint)
345
+ const [reqId] = Varint.decode(body);
346
+ const requestId = BigInt(reqId);
347
+ // PublishNamespace (0x06): also parse namespace for v14/v15 reverse lookup
348
+ if (typeId === 0x06) {
349
+ try {
350
+ const [, afterReqId] = Varint.decode(body);
351
+ this.#parseAndRegisterNamespace(afterReqId, requestId);
352
+ }
353
+ catch {
354
+ // Non-critical: only needed for v14/v15 PublishNamespaceDone/Cancel
355
+ }
356
+ }
357
+ // SubscribeNamespace (0x11): register for follow-up routing
358
+ if (typeId === 0x11) {
359
+ this.#subscribeNamespaces.add(requestId);
360
+ }
361
+ return { requestId };
362
+ }
363
+ /**
364
+ * Parse a namespace from raw bytes and register it for reverse lookup.
365
+ */
366
+ #parseAndRegisterNamespace(buf, requestId) {
367
+ const decoder = new TextDecoder();
368
+ const [partCount, afterCount] = Varint.decode(buf);
369
+ let cursor = afterCount;
370
+ const parts = [];
371
+ for (let i = 0; i < partCount; i++) {
372
+ const [len, afterLen] = Varint.decode(cursor);
373
+ parts.push(decoder.decode(afterLen.subarray(0, len)));
374
+ cursor = afterLen.subarray(len);
375
+ }
376
+ const namespace = parts.join("/");
377
+ this.#namespaces.set(namespace, requestId);
378
+ this.#namespacesByRequestId.set(requestId, namespace);
379
+ }
380
+ /** Create a WritableStream that buffers and writes complete messages to the control stream under mutex. */
381
+ #createSendWritable() {
382
+ let buffer = new Uint8Array(0);
383
+ return new WritableStream({
384
+ write: async (chunk) => {
385
+ const newBuf = new Uint8Array(buffer.length + chunk.length);
386
+ newBuf.set(buffer);
387
+ newBuf.set(chunk, buffer.length);
388
+ buffer = newBuf;
389
+ for (;;) {
390
+ const boundary = this.#messageSize(buffer);
391
+ if (boundary === undefined)
392
+ break;
393
+ const toFlush = buffer.subarray(0, boundary);
394
+ buffer = buffer.subarray(boundary);
395
+ await this.#writeMutex.runExclusive(() => this.#writer.write(toFlush));
396
+ }
397
+ },
398
+ });
399
+ }
400
+ /** Encode raw message bytes: [typeId varint][size u16 BE][body] */
401
+ #encodeRaw(typeId, size, body) {
402
+ // v14-v16 always use QUIC varint
403
+ const typeIdBytes = Varint.encodeTo(new ArrayBuffer(9), typeId);
404
+ const result = new Uint8Array(typeIdBytes.byteLength + 2 + body.byteLength);
405
+ result.set(typeIdBytes, 0);
406
+ const sizeView = new DataView(result.buffer, typeIdBytes.byteLength, 2);
407
+ sizeView.setUint16(0, size);
408
+ result.set(body, typeIdBytes.byteLength + 2);
409
+ return result;
410
+ }
411
+ /**
412
+ * Classify a control message and extract its requestId for routing.
413
+ */
414
+ async #classify(typeId, body) {
415
+ const readRequestId = async () => {
416
+ const r = new Reader(undefined, body, this.version);
417
+ return await r.u62();
418
+ };
419
+ const readNamespaceRequestId = async () => {
420
+ const r = new Reader(undefined, body, this.version);
421
+ const namespace = await Namespace.decode(r);
422
+ const requestId = this.#namespaces.get(namespace);
423
+ if (requestId === undefined)
424
+ throw new Error(`unknown namespace: ${namespace}`);
425
+ this.#namespaces.delete(namespace);
426
+ return requestId;
427
+ };
428
+ switch (typeId) {
429
+ // === FollowUp: route to existing stream ===
430
+ case 0x02: {
431
+ // SubscribeUpdate / REQUEST_UPDATE
432
+ const requestId = await readRequestId();
433
+ return { route: Route.FollowUp, requestId };
434
+ }
435
+ // === NewRequest: create virtual stream ===
436
+ case 0x03: // Subscribe
437
+ case 0x16: // Fetch
438
+ case 0x1d: // Publish
439
+ case 0x0d: {
440
+ // TrackStatusRequest
441
+ const requestId = await readRequestId();
442
+ return { route: Route.NewRequest, requestId };
443
+ }
444
+ case 0x06: {
445
+ // PublishNamespace — also store namespace for v14/v15 reverse lookup
446
+ const r = new Reader(undefined, body, this.version);
447
+ const requestId = await r.u62();
448
+ const namespace = await Namespace.decode(r);
449
+ this.#namespaces.set(namespace, requestId);
450
+ this.#namespacesByRequestId.set(requestId, namespace);
451
+ return { route: Route.NewRequest, requestId };
452
+ }
453
+ case 0x11: {
454
+ // SubscribeNamespace (v14/v15 only on control stream)
455
+ if (this.version !== Version.DRAFT_14 && this.version !== Version.DRAFT_15) {
456
+ throw new Error("unexpected SubscribeNamespace on control stream");
457
+ }
458
+ const requestId = await readRequestId();
459
+ return { route: Route.NewRequest, requestId };
460
+ }
461
+ // === Response: push bytes, keep stream open ===
462
+ case 0x04: {
463
+ // SubscribeOk
464
+ const requestId = await readRequestId();
465
+ return { route: Route.Response, requestId };
466
+ }
467
+ case 0x18: {
468
+ // FetchOk
469
+ const requestId = await readRequestId();
470
+ return { route: Route.Response, requestId };
471
+ }
472
+ case 0x1e: {
473
+ // PublishOk
474
+ const requestId = await readRequestId();
475
+ return { route: Route.Response, requestId };
476
+ }
477
+ case 0x07: {
478
+ // v14: PublishNamespaceOk, v15+: RequestOk
479
+ const requestId = await readRequestId();
480
+ return { route: Route.Response, requestId };
481
+ }
482
+ case 0x12: {
483
+ // SubscribeNamespaceOk (v14 only)
484
+ if (this.version !== Version.DRAFT_14)
485
+ throw new Error("unexpected SubscribeNamespaceOk");
486
+ const requestId = await readRequestId();
487
+ return { route: Route.Response, requestId };
488
+ }
489
+ // === ErrorResponse: push bytes + close ===
490
+ case 0x05: {
491
+ // SubscribeError (v14) / RequestError (v15+)
492
+ const requestId = await readRequestId();
493
+ return { route: Route.ErrorResponse, requestId };
494
+ }
495
+ case 0x19: {
496
+ // FetchError (v14 only)
497
+ if (this.version !== Version.DRAFT_14)
498
+ throw new Error("unexpected FetchError");
499
+ const requestId = await readRequestId();
500
+ return { route: Route.ErrorResponse, requestId };
501
+ }
502
+ case 0x1f: {
503
+ // PublishError (v14 only)
504
+ if (this.version !== Version.DRAFT_14)
505
+ throw new Error("unexpected PublishError");
506
+ const requestId = await readRequestId();
507
+ return { route: Route.ErrorResponse, requestId };
508
+ }
509
+ case 0x08: {
510
+ if (this.version === Version.DRAFT_14) {
511
+ // PublishNamespaceError
512
+ const requestId = await readRequestId();
513
+ return { route: Route.ErrorResponse, requestId };
514
+ }
515
+ // v15: Namespace entry (no requestId) — route to SubscribeNamespace stream
516
+ const subNs08 = this.#subscribeNamespaces.values().next().value;
517
+ if (subNs08 === undefined)
518
+ throw new Error("unexpected message 0x08: no SubscribeNamespace stream");
519
+ return { route: Route.FollowUp, requestId: subNs08 };
520
+ }
521
+ case 0x0e: {
522
+ // v15: NamespaceDone entry (no requestId) — route to SubscribeNamespace stream
523
+ const subNs0e = this.#subscribeNamespaces.values().next().value;
524
+ if (subNs0e === undefined)
525
+ throw new Error("unexpected message 0x0e: no SubscribeNamespace stream");
526
+ return { route: Route.FollowUp, requestId: subNs0e };
527
+ }
528
+ case 0x13: {
529
+ // SubscribeNamespaceError (v14 only)
530
+ if (this.version !== Version.DRAFT_14)
531
+ throw new Error("unexpected SubscribeNamespaceError");
532
+ const requestId = await readRequestId();
533
+ return { route: Route.ErrorResponse, requestId };
534
+ }
535
+ // === CloseStream: close recv (no bytes pushed) ===
536
+ case 0x0a: {
537
+ // Unsubscribe
538
+ const requestId = await readRequestId();
539
+ return { route: Route.CloseStream, requestId };
540
+ }
541
+ case 0x0b: {
542
+ // PublishDone
543
+ const requestId = await readRequestId();
544
+ return { route: Route.CloseStream, requestId };
545
+ }
546
+ case 0x17: {
547
+ // FetchCancel
548
+ const requestId = await readRequestId();
549
+ return { route: Route.CloseStream, requestId };
550
+ }
551
+ case 0x09: {
552
+ // PublishNamespaceDone: v16 uses requestId, v14/v15 uses namespace
553
+ if (this.version === Version.DRAFT_16) {
554
+ const requestId = await readRequestId();
555
+ return { route: Route.CloseStream, requestId };
556
+ }
557
+ const requestId = await readNamespaceRequestId();
558
+ return { route: Route.CloseStream, requestId };
559
+ }
560
+ case 0x0c: {
561
+ // PublishNamespaceCancel: v16 uses requestId, v14/v15 uses namespace
562
+ if (this.version === Version.DRAFT_16) {
563
+ const requestId = await readRequestId();
564
+ return { route: Route.CloseStream, requestId };
565
+ }
566
+ const requestId = await readNamespaceRequestId();
567
+ return { route: Route.CloseStream, requestId };
568
+ }
569
+ case 0x14: {
570
+ // UnsubscribeNamespace (v14/v15 only)
571
+ if (this.version !== Version.DRAFT_14 && this.version !== Version.DRAFT_15) {
572
+ throw new Error("unexpected UnsubscribeNamespace");
573
+ }
574
+ const requestId = await readRequestId();
575
+ return { route: Route.CloseStream, requestId };
576
+ }
577
+ // === Utility ===
578
+ case 0x15: {
579
+ // MaxRequestId
580
+ const requestId = await readRequestId();
581
+ return { route: Route.MaxRequestId, requestId };
582
+ }
583
+ case 0x1a: {
584
+ // RequestsBlocked — connection-level, consume and ignore
585
+ await readRequestId();
586
+ return { route: Route.Ignore, requestId: 0n };
587
+ }
588
+ // === Terminal ===
589
+ case 0x10: // GoAway
590
+ return { route: Route.GoAway };
591
+ default:
592
+ throw new Error(`unknown control message type: 0x${typeId.toString(16)}`);
593
+ }
594
+ }
595
+ close() {
596
+ if (this.#closed)
597
+ return;
598
+ this.#closed = true;
599
+ console.debug("adapter: close() called");
600
+ // Close all virtual streams
601
+ for (const entry of this.#streams.values()) {
602
+ try {
603
+ entry.controller.close();
604
+ }
605
+ catch {
606
+ // Already closed
607
+ }
608
+ }
609
+ this.#streams.clear();
610
+ // Resolve any waiting acceptBi callers
611
+ for (const waiter of this.#incomingWaiters) {
612
+ waiter(undefined);
613
+ }
614
+ this.#incomingWaiters = [];
615
+ // Clear namespace mappings
616
+ this.#namespaces.clear();
617
+ this.#namespacesByRequestId.clear();
618
+ this.#subscribeNamespaces.clear();
619
+ // Unblock maxRequestId waiters
620
+ for (const resolve of this.#maxRequestIdResolves)
621
+ resolve();
622
+ this.#maxRequestIdResolves = [];
623
+ }
624
+ }
625
+ //# sourceMappingURL=adapter.js.map