@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
package/README.md ADDED
@@ -0,0 +1,70 @@
1
+ <p align="center">
2
+ <img height="128px" src="https://github.com/moq-dev/moq/blob/main/.github/logo.svg" alt="Media over QUIC">
3
+ </p>
4
+
5
+ # @moq/net
6
+
7
+ [![npm version](https://img.shields.io/npm/v/@moq/net)](https://www.npmjs.com/package/@moq/net)
8
+ [![TypeScript](https://img.shields.io/badge/TypeScript-ready-blue.svg)](https://www.typescriptlang.org/)
9
+
10
+ A TypeScript [Media over QUIC](https://moq.dev/) (MoQ) client for both browsers and server JS/TS environments.
11
+ `@moq/net` is the **networking layer**: it negotiates one of two wire protocols at session setup, either the simplified [moq-lite](https://doc.moq.dev/concept/layer/moq-lite) protocol or the full IETF [moq-transport](https://datatracker.ietf.org/doc/draft-ietf-moq-transport/) draft, and handles real-time data delivery to/from moq relays.
12
+
13
+ Check out [hang](../hang) for a higher-level media library that uses this package.
14
+
15
+ > **Note:** moq-lite is a subset of moq-transport and is forwards compatible with it, so this client works with any moq-transport CDN (ex. [Cloudflare](https://moq.dev/blog/first-cdn/)). See the [compatibility docs](https://doc.moq.dev/concept/layer/moq-lite#compatibility) for details.
16
+
17
+ ## Quick Start
18
+
19
+ ```bash
20
+ npm add @moq/net
21
+ # or
22
+ pnpm add @moq/net
23
+ bun add @moq/net
24
+ yarn add @moq/net
25
+ # etc
26
+ ```
27
+
28
+ ## Server-side usage
29
+
30
+ `@moq/net` works on both browsers and servers, however in JS/TS server environments (Node, Bun) WebTransport is not yet available, so `@moq/net` will default to WebSockets communication with the relay.
31
+
32
+ Bun and Node v21+ have `WebSockets` built in, but older versions of Node do not, so for older versions of Node you will need the WebSockets polyfill to use `@moq/net`
33
+
34
+ ```javascript
35
+ import WebSocket from 'ws';
36
+ import * as Moq from '@moq/net';
37
+ // Polyfill WebSocket for MoQ
38
+ globalThis.WebSocket = WebSocket;
39
+ ```
40
+
41
+ You can optionally enable `WebTransport` and full HTTP3/Quic on server environments with the following (experimental) [polyfill](https://github.com/fails-components/webtransport)
42
+
43
+ ```bash
44
+ npm install @fails-components/webtransport
45
+ npm install @fails-components/webtransport-transport-http3-quiche
46
+ ```
47
+
48
+ Which you would load as follows
49
+
50
+ ```javascript
51
+ import { WebTransport, quicheLoaded } from '@fails-components/webtransport';
52
+ global.WebTransport = WebTransport;
53
+ import * as Moq from '@moq/net'
54
+ await quicheLoaded; //This is a promise, connect after it resolves
55
+ ```
56
+
57
+ ## Examples
58
+
59
+ - **[Connection](examples/connection.ts)** - Connect to a MoQ relay server
60
+ - **[Publishing](examples/publish.ts)** - Publish data to a broadcast
61
+ - **[Subscribing](examples/subscribe.ts)** - Subscribe to and receive broadcast data
62
+ - **[Discovery](examples/discovery.ts)** - Discover broadcasts announced by the server
63
+ - **[Server side usage](https://github.com/sb2702/webcodecs-examples/tree/main/src/moq-server)** - Publish from browser to a server
64
+
65
+ ## License
66
+
67
+ Licensed under either:
68
+
69
+ - Apache License, Version 2.0 ([LICENSE-APACHE](../../LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
70
+ - MIT license ([LICENSE-MIT](../../LICENSE-MIT) or http://opensource.org/licenses/MIT)
package/announced.d.ts ADDED
@@ -0,0 +1,41 @@
1
+ import { Signal } from "@moq/signals";
2
+ import * as Path from "./path.js";
3
+ /**
4
+ * The availability of a broadcast.
5
+ *
6
+ * @public
7
+ */
8
+ export interface AnnouncedEntry {
9
+ path: Path.Valid;
10
+ active: boolean;
11
+ }
12
+ export declare class AnnouncedState {
13
+ queue: Signal<AnnouncedEntry[]>;
14
+ closed: Signal<boolean | Error>;
15
+ }
16
+ /**
17
+ * Handles writing announcements to the announcement queue.
18
+ *
19
+ * @public
20
+ */
21
+ export declare class Announced {
22
+ state: AnnouncedState;
23
+ prefix: Path.Valid;
24
+ readonly closed: Promise<Error | undefined>;
25
+ constructor(prefix?: Path.Valid);
26
+ /**
27
+ * Writes an announcement to the queue.
28
+ * @param announcement - The announcement to write
29
+ */
30
+ append(announcement: AnnouncedEntry): void;
31
+ /**
32
+ * Closes the writer.
33
+ * @param abort - If provided, throw this exception instead of returning undefined.
34
+ */
35
+ close(abort?: Error): void;
36
+ /**
37
+ * Returns the next announcement.
38
+ */
39
+ next(): Promise<AnnouncedEntry | undefined>;
40
+ }
41
+ //# sourceMappingURL=announced.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"announced.d.ts","sourceRoot":"","sources":["../src/announced.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AACtC,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAElC;;;;GAIG;AACH,MAAM,WAAW,cAAc;IAC9B,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC;IACjB,MAAM,EAAE,OAAO,CAAC;CAChB;AAED,qBAAa,cAAc;IAC1B,KAAK,2BAAoC;IACzC,MAAM,0BAAsC;CAC5C;AAED;;;;GAIG;AACH,qBAAa,SAAS;IACrB,KAAK,iBAAwB;IAC7B,MAAM,EAAE,IAAI,CAAC,KAAK,CAAC;IAEnB,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,GAAG,SAAS,CAAC,CAAC;gBAEhC,MAAM,aAAe;IAWjC;;;OAGG;IACH,MAAM,CAAC,YAAY,EAAE,cAAc;IAOnC;;;OAGG;IACH,KAAK,CAAC,KAAK,CAAC,EAAE,KAAK;IAOnB;;OAEG;IACG,IAAI,IAAI,OAAO,CAAC,cAAc,GAAG,SAAS,CAAC;CAYjD"}
package/announced.js ADDED
@@ -0,0 +1,65 @@
1
+ import { Signal } from "@moq/signals";
2
+ import * as Path from "./path.js";
3
+ export class AnnouncedState {
4
+ queue = new Signal([]);
5
+ closed = new Signal(false);
6
+ }
7
+ /**
8
+ * Handles writing announcements to the announcement queue.
9
+ *
10
+ * @public
11
+ */
12
+ export class Announced {
13
+ state = new AnnouncedState();
14
+ prefix;
15
+ closed;
16
+ constructor(prefix = Path.empty()) {
17
+ this.prefix = prefix;
18
+ this.closed = new Promise((resolve) => {
19
+ const dispose = this.state.closed.subscribe((closed) => {
20
+ if (!closed)
21
+ return;
22
+ resolve(closed instanceof Error ? closed : undefined);
23
+ dispose();
24
+ });
25
+ });
26
+ }
27
+ /**
28
+ * Writes an announcement to the queue.
29
+ * @param announcement - The announcement to write
30
+ */
31
+ append(announcement) {
32
+ if (this.state.closed.peek())
33
+ throw new Error("announced is closed");
34
+ this.state.queue.mutate((queue) => {
35
+ queue.push(announcement);
36
+ });
37
+ }
38
+ /**
39
+ * Closes the writer.
40
+ * @param abort - If provided, throw this exception instead of returning undefined.
41
+ */
42
+ close(abort) {
43
+ this.state.closed.set(abort ?? true);
44
+ this.state.queue.mutate((queue) => {
45
+ queue.length = 0;
46
+ });
47
+ }
48
+ /**
49
+ * Returns the next announcement.
50
+ */
51
+ async next() {
52
+ for (;;) {
53
+ const announce = this.state.queue.peek().shift();
54
+ if (announce)
55
+ return announce;
56
+ const closed = this.state.closed.peek();
57
+ if (closed instanceof Error)
58
+ throw closed;
59
+ if (closed)
60
+ return undefined;
61
+ await Signal.race(this.state.queue, this.state.closed);
62
+ }
63
+ }
64
+ }
65
+ //# sourceMappingURL=announced.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"announced.js","sourceRoot":"","sources":["../src/announced.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AACtC,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAYlC,MAAM,OAAO,cAAc;IAC1B,KAAK,GAAG,IAAI,MAAM,CAAmB,EAAE,CAAC,CAAC;IACzC,MAAM,GAAG,IAAI,MAAM,CAAkB,KAAK,CAAC,CAAC;CAC5C;AAED;;;;GAIG;AACH,MAAM,OAAO,SAAS;IACrB,KAAK,GAA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,KAAK,CAAC,IAAI,EAAE,CAAC,KAAK,EAAE,CAAC;YACjD,IAAI,QAAQ;gBAAE,OAAO,QAAQ,CAAC;YAE9B,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;YACxC,IAAI,MAAM,YAAY,KAAK;gBAAE,MAAM,MAAM,CAAC;YAC1C,IAAI,MAAM;gBAAE,OAAO,SAAS,CAAC;YAE7B,MAAM,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;QACxD,CAAC;IACF,CAAC;CACD","sourcesContent":["import { Signal } from \"@moq/signals\";\nimport * as Path from \"./path.js\";\n\n/**\n * The availability of a broadcast.\n *\n * @public\n */\nexport interface AnnouncedEntry {\n\tpath: Path.Valid;\n\tactive: boolean;\n}\n\nexport class AnnouncedState {\n\tqueue = new Signal<AnnouncedEntry[]>([]);\n\tclosed = new Signal<boolean | Error>(false);\n}\n\n/**\n * Handles writing announcements to the announcement queue.\n *\n * @public\n */\nexport class Announced {\n\tstate = new AnnouncedState();\n\tprefix: Path.Valid;\n\n\treadonly closed: Promise<Error | undefined>;\n\n\tconstructor(prefix = Path.empty()) {\n\t\tthis.prefix = prefix;\n\t\tthis.closed = new Promise((resolve) => {\n\t\t\tconst dispose = this.state.closed.subscribe((closed) => {\n\t\t\t\tif (!closed) return;\n\t\t\t\tresolve(closed instanceof Error ? closed : undefined);\n\t\t\t\tdispose();\n\t\t\t});\n\t\t});\n\t}\n\n\t/**\n\t * Writes an announcement to the queue.\n\t * @param announcement - The announcement to write\n\t */\n\tappend(announcement: AnnouncedEntry) {\n\t\tif (this.state.closed.peek()) throw new Error(\"announced is closed\");\n\t\tthis.state.queue.mutate((queue) => {\n\t\t\tqueue.push(announcement);\n\t\t});\n\t}\n\n\t/**\n\t * Closes the writer.\n\t * @param abort - If provided, throw this exception instead of returning undefined.\n\t */\n\tclose(abort?: Error) {\n\t\tthis.state.closed.set(abort ?? true);\n\t\tthis.state.queue.mutate((queue) => {\n\t\t\tqueue.length = 0;\n\t\t});\n\t}\n\n\t/**\n\t * Returns the next announcement.\n\t */\n\tasync next(): Promise<AnnouncedEntry | undefined> {\n\t\tfor (;;) {\n\t\t\tconst announce = this.state.queue.peek().shift();\n\t\t\tif (announce) return announce;\n\n\t\t\tconst closed = this.state.closed.peek();\n\t\t\tif (closed instanceof Error) throw closed;\n\t\t\tif (closed) return undefined;\n\n\t\t\tawait Signal.race(this.state.queue, this.state.closed);\n\t\t}\n\t}\n}\n"]}
package/bandwidth.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ import { Signal } from "@moq/signals";
2
+ /**
3
+ * A bandwidth estimate in bits per second, or undefined if unknown.
4
+ *
5
+ * This is a Signal that can be read synchronously via `peek()`,
6
+ * observed reactively via `effect.get()`, or updated via `set()`.
7
+ */
8
+ export type Bandwidth = Signal<number | undefined>;
9
+ /** Create a new bandwidth signal. */
10
+ export declare function createBandwidth(): Bandwidth;
11
+ //# sourceMappingURL=bandwidth.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bandwidth.d.ts","sourceRoot":"","sources":["../src/bandwidth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAEtC;;;;;GAKG;AACH,MAAM,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;AAEnD,qCAAqC;AACrC,wBAAgB,eAAe,IAAI,SAAS,CAE3C"}
package/bandwidth.js ADDED
@@ -0,0 +1,6 @@
1
+ import { Signal } from "@moq/signals";
2
+ /** Create a new bandwidth signal. */
3
+ export function createBandwidth() {
4
+ return new Signal(undefined);
5
+ }
6
+ //# sourceMappingURL=bandwidth.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bandwidth.js","sourceRoot":"","sources":["../src/bandwidth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAUtC,qCAAqC;AACrC,MAAM,UAAU,eAAe;IAC9B,OAAO,IAAI,MAAM,CAAqB,SAAS,CAAC,CAAC;AAClD,CAAC","sourcesContent":["import { Signal } from \"@moq/signals\";\n\n/**\n * A bandwidth estimate in bits per second, or undefined if unknown.\n *\n * This is a Signal that can be read synchronously via `peek()`,\n * observed reactively via `effect.get()`, or updated via `set()`.\n */\nexport type Bandwidth = Signal<number | undefined>;\n\n/** Create a new bandwidth signal. */\nexport function createBandwidth(): Bandwidth {\n\treturn new Signal<number | undefined>(undefined);\n}\n"]}
package/broadcast.d.ts ADDED
@@ -0,0 +1,35 @@
1
+ import { Signal } from "@moq/signals";
2
+ import { Track } from "./track.ts";
3
+ export interface TrackRequest {
4
+ track: Track;
5
+ priority: number;
6
+ }
7
+ export declare class BroadcastState {
8
+ requested: Signal<TrackRequest[]>;
9
+ closed: Signal<boolean | Error>;
10
+ }
11
+ /**
12
+ * Handles writing and managing tracks in a broadcast.
13
+ *
14
+ * @public
15
+ */
16
+ export declare class Broadcast {
17
+ state: BroadcastState;
18
+ readonly closed: Promise<Error | undefined>;
19
+ constructor();
20
+ /**
21
+ * A track requested over the network.
22
+ */
23
+ requested(): Promise<TrackRequest | undefined>;
24
+ /**
25
+ * Populates the provided track over the network.
26
+ */
27
+ subscribe(name: string, priority: number): Track;
28
+ /**
29
+ * Closes the writer and all associated tracks.
30
+ *
31
+ * @param abort - If provided, throw this exception instead of returning undefined.
32
+ */
33
+ close(abort?: Error): void;
34
+ }
35
+ //# sourceMappingURL=broadcast.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"broadcast.d.ts","sourceRoot":"","sources":["../src/broadcast.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AACtC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC,MAAM,WAAW,YAAY;IAC5B,KAAK,EAAE,KAAK,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;CACjB;AAED,qBAAa,cAAc;IAC1B,SAAS,yBAAkC;IAC3C,MAAM,0BAAsC;CAC5C;AAED;;;;GAIG;AACH,qBAAa,SAAS;IACrB,KAAK,iBAAwB;IAE7B,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,GAAG,SAAS,CAAC,CAAC;;IAY5C;;OAEG;IACG,SAAS,IAAI,OAAO,CAAC,YAAY,GAAG,SAAS,CAAC;IAcpD;;OAEG;IACH,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,KAAK;IAehD;;;;OAIG;IACH,KAAK,CAAC,KAAK,CAAC,EAAE,KAAK;CASnB"}
package/broadcast.js ADDED
@@ -0,0 +1,72 @@
1
+ import { Signal } from "@moq/signals";
2
+ import { Track } from "./track.js";
3
+ export class BroadcastState {
4
+ requested = new Signal([]);
5
+ closed = new Signal(false);
6
+ }
7
+ /**
8
+ * Handles writing and managing tracks in a broadcast.
9
+ *
10
+ * @public
11
+ */
12
+ export class Broadcast {
13
+ state = new BroadcastState();
14
+ closed;
15
+ constructor() {
16
+ this.closed = new Promise((resolve) => {
17
+ const dispose = this.state.closed.subscribe((closed) => {
18
+ if (!closed)
19
+ return;
20
+ resolve(closed instanceof Error ? closed : undefined);
21
+ dispose();
22
+ });
23
+ });
24
+ }
25
+ /**
26
+ * A track requested over the network.
27
+ */
28
+ async requested() {
29
+ for (;;) {
30
+ // We use pop instead of shift because it's slightly more efficient.
31
+ const track = this.state.requested.peek().pop();
32
+ if (track)
33
+ return track;
34
+ const closed = this.state.closed.peek();
35
+ if (closed instanceof Error)
36
+ throw closed;
37
+ if (closed)
38
+ return undefined;
39
+ await Signal.race(this.state.requested, this.state.closed);
40
+ }
41
+ }
42
+ /**
43
+ * Populates the provided track over the network.
44
+ */
45
+ subscribe(name, priority) {
46
+ const track = new Track(name);
47
+ if (this.state.closed.peek()) {
48
+ throw new Error(`broadcast is closed: ${this.state.closed.peek()}`);
49
+ }
50
+ this.state.requested.mutate((requested) => {
51
+ requested.push({ track, priority });
52
+ // Sort the tracks by priority in ascending order (we will pop)
53
+ requested.sort((a, b) => a.priority - b.priority);
54
+ });
55
+ return track;
56
+ }
57
+ /**
58
+ * Closes the writer and all associated tracks.
59
+ *
60
+ * @param abort - If provided, throw this exception instead of returning undefined.
61
+ */
62
+ close(abort) {
63
+ this.state.closed.set(abort ?? true);
64
+ for (const { track } of this.state.requested.peek()) {
65
+ track.close(abort);
66
+ }
67
+ this.state.requested.mutate((requested) => {
68
+ requested.length = 0;
69
+ });
70
+ }
71
+ }
72
+ //# sourceMappingURL=broadcast.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"broadcast.js","sourceRoot":"","sources":["../src/broadcast.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AACtC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAOnC,MAAM,OAAO,cAAc;IAC1B,SAAS,GAAG,IAAI,MAAM,CAAiB,EAAE,CAAC,CAAC;IAC3C,MAAM,GAAG,IAAI,MAAM,CAAkB,KAAK,CAAC,CAAC;CAC5C;AAED;;;;GAIG;AACH,MAAM,OAAO,SAAS;IACrB,KAAK,GAAG,IAAI,cAAc,EAAE,CAAC;IAEpB,MAAM,CAA6B;IAE5C;QACC,IAAI,CAAC,MAAM,GAAG,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;YACrC,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,MAAM,EAAE,EAAE;gBACtD,IAAI,CAAC,MAAM;oBAAE,OAAO;gBACpB,OAAO,CAAC,MAAM,YAAY,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;gBACtD,OAAO,EAAE,CAAC;YACX,CAAC,CAAC,CAAC;QACJ,CAAC,CAAC,CAA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,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,IAAI,IAAI,CAAC,CAAC;QACrC,KAAK,MAAM,EAAE,KAAK,EAAE,IAAI,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,IAAI,EAAE,EAAE,CAAC;YACrD,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACpB,CAAC;QACD,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,SAAS,EAAE,EAAE;YACzC,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC;QACtB,CAAC,CAAC,CAAC;IACJ,CAAC;CACD","sourcesContent":["import { Signal } from \"@moq/signals\";\nimport { Track } from \"./track.ts\";\n\nexport interface TrackRequest {\n\ttrack: Track;\n\tpriority: number;\n}\n\nexport class BroadcastState {\n\trequested = new Signal<TrackRequest[]>([]);\n\tclosed = new Signal<boolean | Error>(false);\n}\n\n/**\n * Handles writing and managing tracks in a broadcast.\n *\n * @public\n */\nexport class Broadcast {\n\tstate = new BroadcastState();\n\n\treadonly closed: Promise<Error | undefined>;\n\n\tconstructor() {\n\t\tthis.closed = new Promise((resolve) => {\n\t\t\tconst dispose = this.state.closed.subscribe((closed) => {\n\t\t\t\tif (!closed) return;\n\t\t\t\tresolve(closed instanceof Error ? closed : undefined);\n\t\t\t\tdispose();\n\t\t\t});\n\t\t});\n\t}\n\n\t/**\n\t * A track requested over the network.\n\t */\n\tasync requested(): Promise<TrackRequest | undefined> {\n\t\tfor (;;) {\n\t\t\t// We use pop instead of shift because it's slightly more efficient.\n\t\t\tconst track = this.state.requested.peek().pop();\n\t\t\tif (track) return track;\n\n\t\t\tconst closed = this.state.closed.peek();\n\t\t\tif (closed instanceof Error) throw closed;\n\t\t\tif (closed) return undefined;\n\n\t\t\tawait Signal.race(this.state.requested, this.state.closed);\n\t\t}\n\t}\n\n\t/**\n\t * Populates the provided track over the network.\n\t */\n\tsubscribe(name: string, priority: number): Track {\n\t\tconst track = new Track(name);\n\n\t\tif (this.state.closed.peek()) {\n\t\t\tthrow new Error(`broadcast is closed: ${this.state.closed.peek()}`);\n\t\t}\n\t\tthis.state.requested.mutate((requested) => {\n\t\t\trequested.push({ track, priority });\n\t\t\t// Sort the tracks by priority in ascending order (we will pop)\n\t\t\trequested.sort((a, b) => a.priority - b.priority);\n\t\t});\n\n\t\treturn track;\n\t}\n\n\t/**\n\t * Closes the writer and all associated tracks.\n\t *\n\t * @param abort - If provided, throw this exception instead of returning undefined.\n\t */\n\tclose(abort?: Error) {\n\t\tthis.state.closed.set(abort ?? true);\n\t\tfor (const { track } of this.state.requested.peek()) {\n\t\t\ttrack.close(abort);\n\t\t}\n\t\tthis.state.requested.mutate((requested) => {\n\t\t\trequested.length = 0;\n\t\t});\n\t}\n}\n"]}
@@ -0,0 +1,14 @@
1
+ import type { Established } from "./established.ts";
2
+ export interface AcceptProps {
3
+ version?: number;
4
+ }
5
+ /**
6
+ * Server-side handshake: accepts a transport and performs the server half of the SETUP exchange.
7
+ *
8
+ * @param transport - The WebTransport session to accept
9
+ * @param url - The URL of the connection
10
+ * @param props - Optional configuration
11
+ * @returns A promise that resolves to a Connection instance
12
+ */
13
+ export declare function accept(transport: WebTransport, url: URL, props?: AcceptProps): Promise<Established>;
14
+ //# sourceMappingURL=accept.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"accept.d.ts","sourceRoot":"","sources":["../../src/connection/accept.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAGpD,MAAM,WAAW,WAAW;IAE3B,OAAO,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;;;;;;GAOG;AACH,wBAAsB,MAAM,CAAC,SAAS,EAAE,YAAY,EAAE,GAAG,EAAE,GAAG,EAAE,KAAK,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC,CAqBzG"}
@@ -0,0 +1,133 @@
1
+ import * as Ietf from "../ietf/index.js";
2
+ import * as Lite from "../lite/index.js";
3
+ import { Stream } from "../stream.js";
4
+ import { exchangeSetup } from "./handshake.js";
5
+ /**
6
+ * Server-side handshake: accepts a transport and performs the server half of the SETUP exchange.
7
+ *
8
+ * @param transport - The WebTransport session to accept
9
+ * @param url - The URL of the connection
10
+ * @param props - Optional configuration
11
+ * @returns A promise that resolves to a Connection instance
12
+ */
13
+ export async function accept(transport, url, props) {
14
+ // @ts-expect-error - TODO: add protocol to WebTransport
15
+ const protocol = transport.protocol;
16
+ if (protocol === Ietf.ALPN.DRAFT_18) {
17
+ return acceptAlpn(transport, url, Ietf.Version.DRAFT_18);
18
+ }
19
+ else if (protocol === Ietf.ALPN.DRAFT_17) {
20
+ return acceptAlpn(transport, url, Ietf.Version.DRAFT_17);
21
+ }
22
+ else if (protocol === Ietf.ALPN.DRAFT_16) {
23
+ return acceptSetup(transport, url, Ietf.Version.DRAFT_16);
24
+ }
25
+ else if (protocol === Ietf.ALPN.DRAFT_15) {
26
+ return acceptSetup(transport, url, Ietf.Version.DRAFT_15);
27
+ }
28
+ else if (protocol === Lite.ALPN_04) {
29
+ return new Lite.Connection(url, transport, Lite.Version.DRAFT_04, undefined);
30
+ }
31
+ else if (protocol === Lite.ALPN_03) {
32
+ return new Lite.Connection(url, transport, Lite.Version.DRAFT_03, undefined);
33
+ }
34
+ else if (protocol === Lite.ALPN || protocol === "" || protocol === undefined) {
35
+ return acceptNegotiated(transport, url, props);
36
+ }
37
+ else {
38
+ throw new Error(`unsupported WebTransport protocol: ${protocol}`);
39
+ }
40
+ }
41
+ /**
42
+ * Draft-17+ accept: ALPN already pinned the version. SETUP is exchanged over
43
+ * a pair of uni streams using stream type 0x2F00.
44
+ */
45
+ async function acceptAlpn(transport, url, version) {
46
+ const controlStream = await exchangeSetup(transport, version, "moq-lite-js");
47
+ return new Ietf.Connection({
48
+ url,
49
+ quic: transport,
50
+ control: controlStream,
51
+ // v17+ uses NativeSession which manages its own request IDs; maxRequestId is unused.
52
+ maxRequestId: 0n,
53
+ version,
54
+ });
55
+ }
56
+ /**
57
+ * Legacy accept (draft-15/16): ALPN pinned the version, but the SETUP message
58
+ * is still exchanged over a bidi stream wrapped in the moq-lite compat envelope.
59
+ */
60
+ async function acceptSetup(transport, url, version) {
61
+ // Accept bidi, read ClientSetup, write ServerSetup
62
+ const stream = await Stream.accept(transport);
63
+ if (!stream)
64
+ throw new Error("no incoming bidi stream for SETUP");
65
+ const clientCompat = await stream.reader.u53();
66
+ if (clientCompat !== Lite.StreamId.ClientCompat) {
67
+ throw new Error(`unexpected client message type: 0x${clientCompat.toString(16)}`);
68
+ }
69
+ await Ietf.ClientSetup.decode(stream.reader, version);
70
+ await stream.writer.u53(Lite.StreamId.ServerCompat);
71
+ const encoder = new TextEncoder();
72
+ const params = new Ietf.SetupOptions();
73
+ params.setVarint(Ietf.SetupOption.MaxRequestId, 42069n);
74
+ params.setBytes(Ietf.SetupOption.Implementation, encoder.encode("moq-lite-js"));
75
+ const server = new Ietf.ServerSetup({ version, parameters: params });
76
+ await server.encode(stream.writer, version);
77
+ const maxRequestId = 42069n;
78
+ return new Ietf.Connection({
79
+ url,
80
+ quic: transport,
81
+ control: stream,
82
+ maxRequestId,
83
+ version,
84
+ });
85
+ }
86
+ async function acceptNegotiated(transport, url, props) {
87
+ const setupVersion = Ietf.Version.DRAFT_14;
88
+ const stream = await Stream.accept(transport);
89
+ if (!stream)
90
+ throw new Error("no incoming bidi stream for SETUP");
91
+ const clientCompat = await stream.reader.u53();
92
+ if (clientCompat !== Lite.StreamId.ClientCompat) {
93
+ throw new Error(`unexpected client message type: 0x${clientCompat.toString(16)}`);
94
+ }
95
+ const client = await Ietf.ClientSetup.decode(stream.reader, setupVersion);
96
+ // Pick the requested version, or first matching version from client's list
97
+ const allVersions = [...Object.values(Lite.Version), ...Object.values(Ietf.Version)];
98
+ let selectedVersion;
99
+ if (props?.version !== undefined) {
100
+ selectedVersion = props.version;
101
+ }
102
+ else {
103
+ const match = client.versions.find((v) => allVersions.includes(v));
104
+ if (match === undefined) {
105
+ throw new Error(`no common version found; client offered: ${client.versions.map((v) => v.toString(16)).join(", ")}`);
106
+ }
107
+ selectedVersion = match;
108
+ }
109
+ await stream.writer.u53(Lite.StreamId.ServerCompat);
110
+ const encoder = new TextEncoder();
111
+ const params = new Ietf.SetupOptions();
112
+ params.setVarint(Ietf.SetupOption.MaxRequestId, 42069n);
113
+ params.setBytes(Ietf.SetupOption.Implementation, encoder.encode("moq-lite-js"));
114
+ const server = new Ietf.ServerSetup({ version: selectedVersion, parameters: params });
115
+ await server.encode(stream.writer, setupVersion);
116
+ if (Object.values(Lite.Version).includes(selectedVersion)) {
117
+ return new Lite.Connection(url, transport, selectedVersion, stream);
118
+ }
119
+ else if (Object.values(Ietf.Version).includes(selectedVersion)) {
120
+ const maxRequestId = client.parameters.getVarint(Ietf.SetupOption.MaxRequestId) ?? 0n;
121
+ return new Ietf.Connection({
122
+ url,
123
+ quic: transport,
124
+ control: stream,
125
+ maxRequestId,
126
+ version: selectedVersion,
127
+ });
128
+ }
129
+ else {
130
+ throw new Error(`unsupported version: ${selectedVersion.toString(16)}`);
131
+ }
132
+ }
133
+ //# sourceMappingURL=accept.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"accept.js","sourceRoot":"","sources":["../../src/connection/accept.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,kBAAkB,CAAC;AACzC,OAAO,KAAK,IAAI,MAAM,kBAAkB,CAAC;AACzC,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAEtC,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAO/C;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,MAAM,CAAC,SAAuB,EAAE,GAAQ,EAAE,KAAmB;IAClF,wDAAwD;IACxD,MAAM,QAAQ,GAAuB,SAAS,CAAC,QAAQ,CAAC;IAExD,IAAI,QAAQ,KAAK,IAAI,CAAC,IAAI,CAAC,QAAQ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AAM,CAAC,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;IAEjD,IAAI,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,eAA+B,CAAC,EAAE,CAAC;QAC3E,OAAO,IAAI,IAAI,CAAC,UAAU,CAAC,GAAG,EAAE,SAAS,EAAE,eAA+B,EAAE,MAAM,CAAC,CAAC;IACrF,CAAC;SAAM,IAAI,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,eAA+B,CAAC,EAAE,CAAC;QAClF,MAAM,YAAY,GAAG,MAAM,CAAC,UAAU,CAAC,SAAS,CAAC,IAAI,CAAC,WAAW,CAAC,YAAY,CAAC,IAAI,EAAE,CAAC;QACtF,OAAO,IAAI,IAAI,CAAC,UAAU,CAAC;YAC1B,GAAG;YACH,IAAI,EAAE,SAAS;YACf,OAAO,EAAE,MAAM;YACf,YAAY;YACZ,OAAO,EAAE,eAAmC;SAC5C,CAAC,CAAC;IACJ,CAAC;SAAM,CAAC;QACP,MAAM,IAAI,KAAK,CAAC,wBAAwB,eAAe,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;IACzE,CAAC;AACF,CAAC","sourcesContent":["import * as Ietf from \"../ietf/index.ts\";\nimport * as Lite from \"../lite/index.ts\";\nimport { Stream } from \"../stream.ts\";\nimport type { Established } from \"./established.ts\";\nimport { exchangeSetup } from \"./handshake.ts\";\n\nexport interface AcceptProps {\n\t// Version to select during SETUP negotiation (for non-ALPN paths).\n\tversion?: number;\n}\n\n/**\n * Server-side handshake: accepts a transport and performs the server half of the SETUP exchange.\n *\n * @param transport - The WebTransport session to accept\n * @param url - The URL of the connection\n * @param props - Optional configuration\n * @returns A promise that resolves to a Connection instance\n */\nexport async function accept(transport: WebTransport, url: URL, props?: AcceptProps): Promise<Established> {\n\t// @ts-expect-error - TODO: add protocol to WebTransport\n\tconst protocol: string | undefined = transport.protocol;\n\n\tif (protocol === Ietf.ALPN.DRAFT_18) {\n\t\treturn acceptAlpn(transport, url, Ietf.Version.DRAFT_18);\n\t} else if (protocol === Ietf.ALPN.DRAFT_17) {\n\t\treturn acceptAlpn(transport, url, Ietf.Version.DRAFT_17);\n\t} else if (protocol === Ietf.ALPN.DRAFT_16) {\n\t\treturn acceptSetup(transport, url, Ietf.Version.DRAFT_16);\n\t} else if (protocol === Ietf.ALPN.DRAFT_15) {\n\t\treturn acceptSetup(transport, url, Ietf.Version.DRAFT_15);\n\t} else if (protocol === Lite.ALPN_04) {\n\t\treturn new Lite.Connection(url, transport, Lite.Version.DRAFT_04, undefined);\n\t} else if (protocol === Lite.ALPN_03) {\n\t\treturn new Lite.Connection(url, transport, Lite.Version.DRAFT_03, undefined);\n\t} else if (protocol === Lite.ALPN || protocol === \"\" || protocol === undefined) {\n\t\treturn acceptNegotiated(transport, url, props);\n\t} else {\n\t\tthrow new Error(`unsupported WebTransport protocol: ${protocol}`);\n\t}\n}\n\n/**\n * Draft-17+ accept: ALPN already pinned the version. SETUP is exchanged over\n * a pair of uni streams using stream type 0x2F00.\n */\nasync function acceptAlpn(transport: WebTransport, url: URL, version: Ietf.IetfVersion): Promise<Established> {\n\tconst controlStream = await exchangeSetup(transport, version, \"moq-lite-js\");\n\n\treturn new Ietf.Connection({\n\t\turl,\n\t\tquic: transport,\n\t\tcontrol: controlStream,\n\t\t// v17+ uses NativeSession which manages its own request IDs; maxRequestId is unused.\n\t\tmaxRequestId: 0n,\n\t\tversion,\n\t});\n}\n\n/**\n * Legacy accept (draft-15/16): ALPN pinned the version, but the SETUP message\n * is still exchanged over a bidi stream wrapped in the moq-lite compat envelope.\n */\nasync function acceptSetup(transport: WebTransport, url: URL, version: Ietf.IetfVersion): Promise<Established> {\n\t// Accept bidi, read ClientSetup, write ServerSetup\n\tconst stream = await Stream.accept(transport);\n\tif (!stream) throw new Error(\"no incoming bidi stream for SETUP\");\n\n\tconst clientCompat = await stream.reader.u53();\n\tif (clientCompat !== Lite.StreamId.ClientCompat) {\n\t\tthrow new Error(`unexpected client message type: 0x${clientCompat.toString(16)}`);\n\t}\n\n\tawait Ietf.ClientSetup.decode(stream.reader, version);\n\n\tawait stream.writer.u53(Lite.StreamId.ServerCompat);\n\n\tconst encoder = new TextEncoder();\n\tconst params = new Ietf.SetupOptions();\n\tparams.setVarint(Ietf.SetupOption.MaxRequestId, 42069n);\n\tparams.setBytes(Ietf.SetupOption.Implementation, encoder.encode(\"moq-lite-js\"));\n\n\tconst server = new Ietf.ServerSetup({ version, parameters: params });\n\tawait server.encode(stream.writer, version);\n\n\tconst maxRequestId = 42069n;\n\n\treturn new Ietf.Connection({\n\t\turl,\n\t\tquic: transport,\n\t\tcontrol: stream,\n\t\tmaxRequestId,\n\t\tversion,\n\t});\n}\n\nasync function acceptNegotiated(transport: WebTransport, url: URL, props?: AcceptProps): Promise<Established> {\n\tconst setupVersion = Ietf.Version.DRAFT_14;\n\n\tconst stream = await Stream.accept(transport);\n\tif (!stream) throw new Error(\"no incoming bidi stream for SETUP\");\n\n\tconst clientCompat = await stream.reader.u53();\n\tif (clientCompat !== Lite.StreamId.ClientCompat) {\n\t\tthrow new Error(`unexpected client message type: 0x${clientCompat.toString(16)}`);\n\t}\n\n\tconst client = await Ietf.ClientSetup.decode(stream.reader, setupVersion);\n\n\t// Pick the requested version, or first matching version from client's list\n\tconst allVersions = [...Object.values(Lite.Version), ...Object.values(Ietf.Version)] as number[];\n\tlet selectedVersion: number;\n\tif (props?.version !== undefined) {\n\t\tselectedVersion = props.version;\n\t} else {\n\t\tconst match = client.versions.find((v) => allVersions.includes(v));\n\t\tif (match === undefined) {\n\t\t\tthrow new Error(\n\t\t\t\t`no common version found; client offered: ${client.versions.map((v) => v.toString(16)).join(\", \")}`,\n\t\t\t);\n\t\t}\n\t\tselectedVersion = match;\n\t}\n\n\tawait stream.writer.u53(Lite.StreamId.ServerCompat);\n\n\tconst encoder = new TextEncoder();\n\tconst params = new Ietf.SetupOptions();\n\tparams.setVarint(Ietf.SetupOption.MaxRequestId, 42069n);\n\tparams.setBytes(Ietf.SetupOption.Implementation, encoder.encode(\"moq-lite-js\"));\n\n\tconst server = new Ietf.ServerSetup({ version: selectedVersion, parameters: params });\n\tawait server.encode(stream.writer, setupVersion);\n\n\tif (Object.values(Lite.Version).includes(selectedVersion as Lite.Version)) {\n\t\treturn new Lite.Connection(url, transport, selectedVersion as Lite.Version, stream);\n\t} else if (Object.values(Ietf.Version).includes(selectedVersion as Ietf.Version)) {\n\t\tconst maxRequestId = client.parameters.getVarint(Ietf.SetupOption.MaxRequestId) ?? 0n;\n\t\treturn new Ietf.Connection({\n\t\t\turl,\n\t\t\tquic: transport,\n\t\t\tcontrol: stream,\n\t\t\tmaxRequestId,\n\t\t\tversion: selectedVersion as Ietf.IetfVersion,\n\t\t});\n\t} else {\n\t\tthrow new Error(`unsupported version: ${selectedVersion.toString(16)}`);\n\t}\n}\n"]}
@@ -0,0 +1,19 @@
1
+ import type { Established } from "./established.ts";
2
+ export interface WebSocketOptions {
3
+ enabled?: boolean;
4
+ url?: URL;
5
+ delay?: DOMHighResTimeStamp;
6
+ }
7
+ export interface ConnectProps {
8
+ webtransport?: WebTransportOptions;
9
+ websocket?: WebSocketOptions;
10
+ transport?: WebTransport;
11
+ }
12
+ /**
13
+ * Establishes a connection to a MOQ server.
14
+ *
15
+ * @param url - The URL of the server to connect to
16
+ * @returns A promise that resolves to a Connection instance
17
+ */
18
+ export declare function connect(url: URL, props?: ConnectProps): Promise<Established>;
19
+ //# sourceMappingURL=connect.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"connect.d.ts","sourceRoot":"","sources":["../../src/connection/connect.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAMpD,MAAM,WAAW,gBAAgB;IAEhC,OAAO,CAAC,EAAE,OAAO,CAAC;IAIlB,GAAG,CAAC,EAAE,GAAG,CAAC;IAIV,KAAK,CAAC,EAAE,mBAAmB,CAAC;CAC5B;AAED,MAAM,WAAW,YAAY;IAE5B,YAAY,CAAC,EAAE,mBAAmB,CAAC;IAGnC,SAAS,CAAC,EAAE,gBAAgB,CAAC;IAI7B,SAAS,CAAC,EAAE,YAAY,CAAC;CACzB;AAUD;;;;;GAKG;AACH,wBAAsB,OAAO,CAAC,GAAG,EAAE,GAAG,EAAE,KAAK,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,WAAW,CAAC,CA6HlF"}