@moq/net 0.1.8 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (264) hide show
  1. package/announced.d.ts +40 -24
  2. package/announced.d.ts.map +1 -1
  3. package/announced.js +74 -40
  4. package/announced.js.map +1 -1
  5. package/announced.test.d.ts +2 -0
  6. package/announced.test.d.ts.map +1 -0
  7. package/announced.test.js +37 -0
  8. package/announced.test.js.map +1 -0
  9. package/broadcast.d.ts +80 -25
  10. package/broadcast.d.ts.map +1 -1
  11. package/broadcast.js +266 -49
  12. package/broadcast.js.map +1 -1
  13. package/broadcast.test.d.ts +2 -0
  14. package/broadcast.test.d.ts.map +1 -0
  15. package/broadcast.test.js +219 -0
  16. package/broadcast.test.js.map +1 -0
  17. package/connection/accept.d.ts +6 -0
  18. package/connection/accept.d.ts.map +1 -1
  19. package/connection/accept.js +30 -10
  20. package/connection/accept.js.map +1 -1
  21. package/connection/browser.d.ts +5 -0
  22. package/connection/browser.d.ts.map +1 -0
  23. package/connection/browser.js +25 -0
  24. package/connection/browser.js.map +1 -0
  25. package/connection/browser.test.d.ts +2 -0
  26. package/connection/browser.test.d.ts.map +1 -0
  27. package/connection/browser.test.js +29 -0
  28. package/connection/browser.test.js.map +1 -0
  29. package/connection/connect.d.ts +43 -1
  30. package/connection/connect.d.ts.map +1 -1
  31. package/connection/connect.js +96 -107
  32. package/connection/connect.js.map +1 -1
  33. package/connection/connect.test.d.ts +2 -0
  34. package/connection/connect.test.d.ts.map +1 -0
  35. package/connection/connect.test.js +118 -0
  36. package/connection/connect.test.js.map +1 -0
  37. package/connection/established.d.ts +29 -15
  38. package/connection/established.d.ts.map +1 -1
  39. package/connection/established.js.map +1 -1
  40. package/connection/index.d.ts +8 -0
  41. package/connection/index.d.ts.map +1 -1
  42. package/connection/index.js +6 -0
  43. package/connection/index.js.map +1 -1
  44. package/connection/reload.d.ts +43 -7
  45. package/connection/reload.d.ts.map +1 -1
  46. package/connection/reload.js +165 -63
  47. package/connection/reload.js.map +1 -1
  48. package/connection/reload.test.d.ts +2 -0
  49. package/connection/reload.test.d.ts.map +1 -0
  50. package/connection/reload.test.js +104 -0
  51. package/connection/reload.test.js.map +1 -0
  52. package/connection/stats.d.ts +92 -0
  53. package/connection/stats.d.ts.map +1 -0
  54. package/connection/stats.js +35 -0
  55. package/connection/stats.js.map +1 -0
  56. package/connection/stats.test.d.ts +2 -0
  57. package/connection/stats.test.d.ts.map +1 -0
  58. package/connection/stats.test.js +120 -0
  59. package/connection/stats.test.js.map +1 -0
  60. package/connection/transport.d.ts +10 -0
  61. package/connection/transport.d.ts.map +1 -0
  62. package/connection/transport.js +12 -0
  63. package/connection/transport.js.map +1 -0
  64. package/consume.d.ts +26 -0
  65. package/consume.d.ts.map +1 -0
  66. package/consume.js +40 -0
  67. package/consume.js.map +1 -0
  68. package/datagram.d.ts +23 -0
  69. package/datagram.d.ts.map +1 -0
  70. package/datagram.js +3 -0
  71. package/datagram.js.map +1 -0
  72. package/group.d.ts +125 -36
  73. package/group.d.ts.map +1 -1
  74. package/group.js +244 -67
  75. package/group.js.map +1 -1
  76. package/group.test.js +121 -35
  77. package/group.test.js.map +1 -1
  78. package/ietf/adapter.d.ts +5 -3
  79. package/ietf/adapter.d.ts.map +1 -1
  80. package/ietf/adapter.js +19 -5
  81. package/ietf/adapter.js.map +1 -1
  82. package/ietf/aliases.d.ts +11 -0
  83. package/ietf/aliases.d.ts.map +1 -0
  84. package/ietf/aliases.js +52 -0
  85. package/ietf/aliases.js.map +1 -0
  86. package/ietf/aliases.test.d.ts +2 -0
  87. package/ietf/aliases.test.d.ts.map +1 -0
  88. package/ietf/aliases.test.js +30 -0
  89. package/ietf/aliases.test.js.map +1 -0
  90. package/ietf/connection.d.ts +19 -6
  91. package/ietf/connection.d.ts.map +1 -1
  92. package/ietf/connection.js +24 -9
  93. package/ietf/connection.js.map +1 -1
  94. package/ietf/ietf.test.js +58 -1
  95. package/ietf/ietf.test.js.map +1 -1
  96. package/ietf/object.d.ts +12 -4
  97. package/ietf/object.d.ts.map +1 -1
  98. package/ietf/object.js +92 -9
  99. package/ietf/object.js.map +1 -1
  100. package/ietf/publisher.d.ts +2 -2
  101. package/ietf/publisher.d.ts.map +1 -1
  102. package/ietf/publisher.js +12 -12
  103. package/ietf/publisher.js.map +1 -1
  104. package/ietf/subscribe_namespace.d.ts +0 -4
  105. package/ietf/subscribe_namespace.d.ts.map +1 -1
  106. package/ietf/subscribe_namespace.js +0 -5
  107. package/ietf/subscribe_namespace.js.map +1 -1
  108. package/ietf/subscriber.d.ts +8 -4
  109. package/ietf/subscriber.d.ts.map +1 -1
  110. package/ietf/subscriber.js +176 -105
  111. package/ietf/subscriber.js.map +1 -1
  112. package/index.d.ts +8 -5
  113. package/index.d.ts.map +1 -1
  114. package/index.js +8 -5
  115. package/index.js.map +1 -1
  116. package/integration.test.js +598 -12
  117. package/integration.test.js.map +1 -1
  118. package/internal.d.ts +14 -0
  119. package/internal.d.ts.map +1 -0
  120. package/internal.js +8 -0
  121. package/internal.js.map +1 -0
  122. package/lite/announce.d.ts +55 -13
  123. package/lite/announce.d.ts.map +1 -1
  124. package/lite/announce.js +221 -68
  125. package/lite/announce.js.map +1 -1
  126. package/lite/announce.test.d.ts +2 -0
  127. package/lite/announce.test.d.ts.map +1 -0
  128. package/lite/announce.test.js +71 -0
  129. package/lite/announce.test.js.map +1 -0
  130. package/lite/connection.d.ts +45 -19
  131. package/lite/connection.d.ts.map +1 -1
  132. package/lite/connection.js +85 -80
  133. package/lite/connection.js.map +1 -1
  134. package/lite/datagram.d.ts +23 -0
  135. package/lite/datagram.d.ts.map +1 -0
  136. package/lite/datagram.js +57 -0
  137. package/lite/datagram.js.map +1 -0
  138. package/lite/datagram.test.d.ts +2 -0
  139. package/lite/datagram.test.d.ts.map +1 -0
  140. package/lite/datagram.test.js +26 -0
  141. package/lite/datagram.test.js.map +1 -0
  142. package/lite/datagram_stream.d.ts +8 -0
  143. package/lite/datagram_stream.d.ts.map +1 -0
  144. package/lite/datagram_stream.js +44 -0
  145. package/lite/datagram_stream.js.map +1 -0
  146. package/lite/datagram_stream.test.d.ts +2 -0
  147. package/lite/datagram_stream.test.d.ts.map +1 -0
  148. package/lite/datagram_stream.test.js +15 -0
  149. package/lite/datagram_stream.test.js.map +1 -0
  150. package/lite/fetch.d.ts.map +1 -1
  151. package/lite/fetch.js +2 -2
  152. package/lite/fetch.js.map +1 -1
  153. package/lite/fetch.test.d.ts +2 -0
  154. package/lite/fetch.test.d.ts.map +1 -0
  155. package/lite/fetch.test.js +41 -0
  156. package/lite/fetch.test.js.map +1 -0
  157. package/lite/index.d.ts +3 -0
  158. package/lite/index.d.ts.map +1 -1
  159. package/lite/index.js +3 -0
  160. package/lite/index.js.map +1 -1
  161. package/lite/publisher.d.ts +13 -6
  162. package/lite/publisher.d.ts.map +1 -1
  163. package/lite/publisher.js +274 -64
  164. package/lite/publisher.js.map +1 -1
  165. package/lite/publisher.test.d.ts +2 -0
  166. package/lite/publisher.test.d.ts.map +1 -0
  167. package/lite/publisher.test.js +65 -0
  168. package/lite/publisher.test.js.map +1 -0
  169. package/lite/setup.d.ts +72 -6
  170. package/lite/setup.d.ts.map +1 -1
  171. package/lite/setup.js +184 -31
  172. package/lite/setup.js.map +1 -1
  173. package/lite/setup.test.d.ts +2 -0
  174. package/lite/setup.test.d.ts.map +1 -0
  175. package/lite/setup.test.js +122 -0
  176. package/lite/setup.test.js.map +1 -0
  177. package/lite/stream.d.ts +12 -3
  178. package/lite/stream.d.ts.map +1 -1
  179. package/lite/stream.js +7 -4
  180. package/lite/stream.js.map +1 -1
  181. package/lite/subscribe.d.ts +37 -23
  182. package/lite/subscribe.d.ts.map +1 -1
  183. package/lite/subscribe.js +70 -55
  184. package/lite/subscribe.js.map +1 -1
  185. package/lite/subscribe.test.d.ts +2 -0
  186. package/lite/subscribe.test.d.ts.map +1 -0
  187. package/lite/subscribe.test.js +68 -0
  188. package/lite/subscribe.test.js.map +1 -0
  189. package/lite/subscriber.d.ts +24 -17
  190. package/lite/subscriber.d.ts.map +1 -1
  191. package/lite/subscriber.js +521 -91
  192. package/lite/subscriber.js.map +1 -1
  193. package/lite/subscriber.test.d.ts +2 -0
  194. package/lite/subscriber.test.d.ts.map +1 -0
  195. package/lite/subscriber.test.js +30 -0
  196. package/lite/subscriber.test.js.map +1 -0
  197. package/lite/track.d.ts +25 -13
  198. package/lite/track.d.ts.map +1 -1
  199. package/lite/track.js +60 -35
  200. package/lite/track.js.map +1 -1
  201. package/lite/track.test.js +12 -10
  202. package/lite/track.test.js.map +1 -1
  203. package/lite/version.d.ts +17 -4
  204. package/lite/version.d.ts.map +1 -1
  205. package/lite/version.js +88 -17
  206. package/lite/version.js.map +1 -1
  207. package/mock.d.ts +42 -3
  208. package/mock.d.ts.map +1 -1
  209. package/mock.js +88 -17
  210. package/mock.js.map +1 -1
  211. package/package.json +3 -3
  212. package/path.d.ts +30 -0
  213. package/path.d.ts.map +1 -1
  214. package/path.js +49 -0
  215. package/path.js.map +1 -1
  216. package/path.test.js +41 -0
  217. package/path.test.js.map +1 -1
  218. package/stream.test.js +10 -0
  219. package/stream.test.js.map +1 -1
  220. package/time.d.ts +109 -86
  221. package/time.d.ts.map +1 -1
  222. package/time.js +80 -30
  223. package/time.js.map +1 -1
  224. package/time.test.d.ts +2 -0
  225. package/time.test.d.ts.map +1 -0
  226. package/time.test.js +12 -0
  227. package/time.test.js.map +1 -0
  228. package/track.d.ts +225 -45
  229. package/track.d.ts.map +1 -1
  230. package/track.js +505 -93
  231. package/track.js.map +1 -1
  232. package/track.test.js +138 -29
  233. package/track.test.js.map +1 -1
  234. package/util/error.d.ts +9 -0
  235. package/util/error.d.ts.map +1 -1
  236. package/util/error.js +21 -0
  237. package/util/error.js.map +1 -1
  238. package/util/error.test.d.ts +2 -0
  239. package/util/error.test.d.ts.map +1 -0
  240. package/util/error.test.js +45 -0
  241. package/util/error.test.js.map +1 -0
  242. package/util/index.d.ts +1 -0
  243. package/util/index.d.ts.map +1 -1
  244. package/util/index.js +1 -0
  245. package/util/index.js.map +1 -1
  246. package/util/timeout.d.ts +5 -0
  247. package/util/timeout.d.ts.map +1 -0
  248. package/util/timeout.js +18 -0
  249. package/util/timeout.js.map +1 -0
  250. package/util/timeout.test.d.ts +2 -0
  251. package/util/timeout.test.d.ts.map +1 -0
  252. package/util/timeout.test.js +18 -0
  253. package/util/timeout.test.js.map +1 -0
  254. package/varint.d.ts +6 -0
  255. package/varint.d.ts.map +1 -1
  256. package/varint.js +6 -2
  257. package/varint.js.map +1 -1
  258. package/zod.d.ts +4 -4
  259. package/zod.d.ts.map +1 -1
  260. package/zod.js.map +1 -1
  261. package/bandwidth.d.ts +0 -11
  262. package/bandwidth.d.ts.map +0 -1
  263. package/bandwidth.js +0 -7
  264. package/bandwidth.js.map +0 -1
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=publisher.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"publisher.test.d.ts","sourceRoot":"","sources":["../../src/lite/publisher.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,65 @@
1
+ /* @ts-self-types="./publisher.test.d.ts" */
2
+ import { expect, test } from "bun:test";
3
+ import { Producer as BroadcastProducer } from "../broadcast.js";
4
+ import { Producer as GroupProducer } from "../group.js";
5
+ import { createMockTransportPair } from "../mock.js";
6
+ import * as Path from "../path.js";
7
+ import { Stream } from "../stream.js";
8
+ import { randomOrigin } from "./origin.js";
9
+ import { Publisher } from "./publisher.js";
10
+ import { decodeSubscribeResponse, Subscribe } from "./subscribe.js";
11
+ import { ALPN_05, Version } from "./version.js";
12
+ // Delivers `sequences` in the given order, finishes the track, and returns the
13
+ // SUBSCRIBE_END boundary the publisher put on the wire.
14
+ async function subscribeEnd(sequences) {
15
+ const pair = createMockTransportPair(ALPN_05);
16
+ const publisher = new Publisher(pair.server, Version.DRAFT_05, randomOrigin());
17
+ const broadcast = new BroadcastProducer();
18
+ const track = broadcast.createTrack("video");
19
+ publisher.publish(Path.from("test"), broadcast);
20
+ const client = await Stream.open(pair.client);
21
+ const server = await Stream.accept(pair.server);
22
+ if (!server)
23
+ throw new Error("publisher never accepted the subscribe stream");
24
+ const msg = new Subscribe({ id: 0n, broadcast: Path.from("test"), track: "video", priority: 0 });
25
+ void publisher.runSubscribe(msg, server);
26
+ // Finish the track only once it's being served, so the publisher observes a live
27
+ // track ending rather than resolving a subscribe against an already-closed one.
28
+ for (const sequence of sequences) {
29
+ const group = new GroupProducer(sequence);
30
+ group.writeString("hello");
31
+ group.close();
32
+ track.writeGroup(group);
33
+ // Let the publisher drain this group before the next, so arrival order is the
34
+ // order given rather than whatever the cache hands over in one batch.
35
+ await new Promise((resolve) => setTimeout(resolve, 5));
36
+ }
37
+ track.close();
38
+ try {
39
+ for (;;) {
40
+ const resp = await decodeSubscribeResponse(client.reader, Version.DRAFT_05);
41
+ if ("end" in resp)
42
+ return resp.end.group;
43
+ }
44
+ }
45
+ finally {
46
+ broadcast.close();
47
+ client.close();
48
+ }
49
+ }
50
+ // A Rust subscriber feeds this value straight into `track::Producer::finish_at`, which is
51
+ // exclusive, so an inclusive bound here silently truncates the final group across languages.
52
+ test("lite draft-05: subscribe end is the exclusive boundary", async () => {
53
+ expect(await subscribeEnd([0, 1, 2])).toBe(3);
54
+ });
55
+ // recvGroup is arrival-ordered, so the boundary has to clear the max sequence delivered,
56
+ // not the last one seen. Otherwise the boundary lands on a group already on the wire.
57
+ test("lite draft-05: subscribe end clears the max sequence when groups arrive out of order", async () => {
58
+ expect(await subscribeEnd([0, 2, 1])).toBe(3);
59
+ });
60
+ // 0 is the only encoding for "no groups at all"; an inclusive bound cannot express it
61
+ // without colliding with a track whose sole group was sequence 0.
62
+ test("lite draft-05: subscribe end is 0 when no groups were produced", async () => {
63
+ expect(await subscribeEnd([])).toBe(0);
64
+ });
65
+ //# sourceMappingURL=publisher.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"publisher.test.js","sourceR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AAY,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAC/C,CAAC,CAAC,CAAC;AAEH,yFAAyF;AACzF,sFAAsF;AACtF,IAAI,CAAC,sFAAsF,EAAE,KAAK,IAAI,EAAE;IACvG,MAAM,CAAC,MAAM,YAAY,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAC/C,CAAC,CAAC,CAAC;AAEH,sFAAsF;AACtF,kEAAkE;AAClE,IAAI,CAAC,gEAAgE,EAAE,KAAK,IAAI,EAAE;IACjF,MAAM,CAAC,MAAM,YAAY,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AACxC,CAAC,CAAC,CAAC","sourcesContent":["import { expect, test } from \"bun:test\";\nimport { Producer as BroadcastProducer } from \"../broadcast.ts\";\nimport { Producer as GroupProducer } from \"../group.ts\";\nimport { createMockTransportPair } from \"../mock.ts\";\nimport * as Path from \"../path.ts\";\nimport { Stream } from \"../stream.ts\";\nimport { randomOrigin } from \"./origin.ts\";\nimport { Publisher } from \"./publisher.ts\";\nimport { decodeSubscribeResponse, Subscribe } from \"./subscribe.ts\";\nimport { ALPN_05, Version } from \"./version.ts\";\n\n// Delivers `sequences` in the given order, finishes the track, and returns the\n// SUBSCRIBE_END boundary the publisher put on the wire.\nasync function subscribeEnd(sequences: number[]): Promise<number> {\n\tconst pair = createMockTransportPair(ALPN_05);\n\tconst publisher = new Publisher(pair.server, Version.DRAFT_05, randomOrigin());\n\n\tconst broadcast = new BroadcastProducer();\n\tconst track = broadcast.createTrack(\"video\");\n\tpublisher.publish(Path.from(\"test\"), broadcast);\n\n\tconst client = await Stream.open(pair.client);\n\tconst server = await Stream.accept(pair.server);\n\tif (!server) throw new Error(\"publisher never accepted the subscribe stream\");\n\n\tconst msg = new Subscribe({ id: 0n, broadcast: Path.from(\"test\"), track: \"video\", priority: 0 });\n\tvoid publisher.runSubscribe(msg, server);\n\n\t// Finish the track only once it's being served, so the publisher observes a live\n\t// track ending rather than resolving a subscribe against an already-closed one.\n\tfor (const sequence of sequences) {\n\t\tconst group = new GroupProducer(sequence);\n\t\tgroup.writeString(\"hello\");\n\t\tgroup.close();\n\t\ttrack.writeGroup(group);\n\n\t\t// Let the publisher drain this group before the next, so arrival order is the\n\t\t// order given rather than whatever the cache hands over in one batch.\n\t\tawait new Promise((resolve) => setTimeout(resolve, 5));\n\t}\n\ttrack.close();\n\n\ttry {\n\t\tfor (;;) {\n\t\t\tconst resp = await decodeSubscribeResponse(client.reader, Version.DRAFT_05);\n\t\t\tif (\"end\" in resp) return resp.end.group;\n\t\t}\n\t} finally {\n\t\tbroadcast.close();\n\t\tclient.close();\n\t}\n}\n\n// A Rust subscriber feeds this value straight into `track::Producer::finish_at`, which is\n// exclusive, so an inclusive bound here silently truncates the final group across languages.\ntest(\"lite draft-05: subscribe end is the exclusive boundary\", async () => {\n\texpect(await subscribeEnd([0, 1, 2])).toBe(3);\n});\n\n// recvGroup is arrival-ordered, so the boundary has to clear the max sequence delivered,\n// not the last one seen. Otherwise the boundary lands on a group already on the wire.\ntest(\"lite draft-05: subscribe end clears the max sequence when groups arrive out of order\", async () => {\n\texpect(await subscribeEnd([0, 2, 1])).toBe(3);\n});\n\n// 0 is the only encoding for \"no groups at all\"; an inclusive bound cannot express it\n// without colliding with a track whose sole group was sequence 0.\ntest(\"lite draft-05: subscribe end is 0 when no groups were produced\", async () => {\n\texpect(await subscribeEnd([])).toBe(0);\n});\n"]}
package/lite/setup.d.ts CHANGED
@@ -1,18 +1,84 @@
1
+ /**
2
+ * The lite-05 SETUP message: each endpoint advertises its capabilities once, as the
3
+ * sole message on a unidirectional Setup Stream ({@link DataType.Setup}), then closes it.
4
+ *
5
+ * @module
6
+ */
1
7
  import type { Reader, Writer } from "../stream";
2
8
  import { type Version } from "./version";
3
9
  /**
4
- * The moq-lite-05 SETUP message, sent once per endpoint on a unidirectional Setup stream.
10
+ * The probe capability an endpoint advertises in SETUP.
11
+ *
12
+ * Monotonic: a higher level implies every lower one. An unknown (future) value
13
+ * decodes as the highest level we understand, so a peer that gains a new level is
14
+ * treated as at least {@link ProbeLevel.Increase}.
15
+ */
16
+ export declare const ProbeLevel: {
17
+ /** No probing. Equivalent to omitting the parameter. */
18
+ readonly None: 0;
19
+ /** The publisher can measure and periodically report its estimated bitrate. */
20
+ readonly Report: 1;
21
+ /** The publisher can additionally pad the connection (or send redundant data). */
22
+ readonly Increase: 2;
23
+ };
24
+ /** A probe capability level. See {@link ProbeLevel}. */
25
+ export type ProbeLevel = (typeof ProbeLevel)[keyof typeof ProbeLevel];
26
+ /**
27
+ * The direction a client intends to use the session for.
28
+ *
29
+ * A client advertises this in its SETUP so the server can reject a token that lacks the
30
+ * matching scope during the handshake, instead of accepting a session that then silently
31
+ * carries no media (a subscribe-only token used to publish, or vice versa). It only ever
32
+ * narrows what the server grants, so it is not a security boundary: the server still
33
+ * enforces the token's scope regardless.
34
+ */
35
+ export declare const Role: {
36
+ /** The client may do either, or declined to say. Equivalent to omitting the parameter. */
37
+ readonly Both: 0;
38
+ /** The client will publish tracks (ingest); the server must consume. */
39
+ readonly Publisher: 1;
40
+ /** The client will subscribe to tracks (egress); the server must publish. */
41
+ readonly Subscriber: 2;
42
+ };
43
+ /** A session role. See {@link Role}. */
44
+ export type Role = (typeof Role)[keyof typeof Role];
45
+ /** The capabilities a {@link Setup} advertises. Each defaults to the wire default, which is the absence of the parameter. */
46
+ export interface SetupProps {
47
+ /** See {@link Setup.probe}. Defaults to {@link ProbeLevel.None}. */
48
+ probe?: ProbeLevel;
49
+ /** See {@link Setup.path}. Omitted by default. */
50
+ path?: string;
51
+ /** See {@link Setup.role}. Defaults to {@link Role.Both}. */
52
+ role?: Role;
53
+ }
54
+ /**
55
+ * The SETUP message, sent once per endpoint on the unidirectional Setup Stream.
5
56
  *
6
- * The browser conveys its path via the WebTransport URL, so it never sends a Path
7
- * parameter; this class still parses one for completeness when reading a peer's SETUP.
57
+ * lite-05+ only. The two endpoints' SETUP messages are independent: neither side
58
+ * blocks on the peer's before opening other streams, but a stream whose encoding
59
+ * depends on a negotiated capability (e.g. PROBE) must wait for it.
8
60
  */
9
61
  export declare class Setup {
10
62
  #private;
63
+ /** The probe capability this endpoint supports. {@link ProbeLevel.None} when absent. */
64
+ probe: ProbeLevel;
65
+ /**
66
+ * The request path, for transports that carry no request URI (native QUIC, qmux over
67
+ * TCP/TLS, unix sockets). Sent only by the client; a server never sends one and a relay
68
+ * never forwards it. `undefined` on URI-carrying bindings such as WebTransport, where
69
+ * sending one is a protocol violation. An empty string means the same as `undefined`.
70
+ */
11
71
  path?: string;
12
- constructor(props?: {
13
- path?: string;
14
- });
72
+ /**
73
+ * The client's intended {@link Role}. `Both` is sent as the absence of the parameter, so
74
+ * a client that never sets it decodes back to `Both`. Sent only by the client; a server
75
+ * never sends one and a relay never forwards it.
76
+ */
77
+ role: Role;
78
+ constructor({ probe, path, role }?: SetupProps);
79
+ /** Encode the SETUP message with its size prefix. Throws on pre-lite-05 versions. */
15
80
  encode(w: Writer, version: Version): Promise<void>;
81
+ /** Decode a SETUP message with its size prefix. Throws on pre-lite-05 versions. */
16
82
  static decode(r: Reader, version: Version): Promise<Setup>;
17
83
  }
18
84
  //# sourceMappingURL=setup.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"setup.d.ts","sourceRoot":"","sources":["../../src/lite/setup.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAEnD,OAAO,EAAkB,KAAK,OAAO,EAAE,MAAM,cAAc,CAAC;AAK5D;;;;;GAKG;AACH,qBAAa,KAAK;;IAEjB,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd,YAAY,KAAK,GAAE;QAAE,IAAI,CAAC,EAAE,MAAM,CAAA;KAAO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,KAAK,EAAE,UAAU,CAAC;IAElB;;;;;OAKG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd;;;;OAIG;IACH,IAAI,EAAE,IAAI,CAAC;IAEX,YAAY,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,GAAE,UAAe,EAIjD;IAyCD,qFAAqF;IAC/E,MAAM,CAAC,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAGvD;IAED,mFAAmF;IACnF,OAAa,MAAM,CAAC,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,KAAK,CAAC,CAG/D;CACD"}
package/lite/setup.js CHANGED
@@ -1,54 +1,207 @@
1
1
  /* @ts-self-types="./setup.d.ts" */
2
+ /**
3
+ * The lite-05 SETUP message: each endpoint advertises its capabilities once, as the
4
+ * sole message on a unidirectional Setup Stream ({@link DataType.Setup}), then closes it.
5
+ *
6
+ * @module
7
+ */
8
+ import * as Varint from "../varint.js";
2
9
  import * as Message from "./message.js";
3
10
  import { hasSetupStream } from "./version.js";
4
- /// Setup parameter ID for the request Path (client-only, on URI-less transports).
5
- const PARAM_PATH = 0x2;
11
+ /** Setup Parameter id for the Probe capability level. */
12
+ const PARAM_PROBE = 0x1n;
13
+ /** Setup Parameter id for the request Path (client-only, URI-less transports). */
14
+ const PARAM_PATH = 0x2n;
15
+ /** Setup Parameter id for the client's intended {@link Role} (client-only). */
16
+ const PARAM_ROLE = 0x3n;
17
+ /** Cap on the number of parameters in a bag, matching the Rust decoder. */
18
+ const MAX_PARAMS = 64;
6
19
  /**
7
- * The moq-lite-05 SETUP message, sent once per endpoint on a unidirectional Setup stream.
20
+ * The probe capability an endpoint advertises in SETUP.
8
21
  *
9
- * The browser conveys its path via the WebTransport URL, so it never sends a Path
10
- * parameter; this class still parses one for completeness when reading a peer's SETUP.
22
+ * Monotonic: a higher level implies every lower one. An unknown (future) value
23
+ * decodes as the highest level we understand, so a peer that gains a new level is
24
+ * treated as at least {@link ProbeLevel.Increase}.
11
25
  */
12
- export class Setup {
13
- /// The request path, only sent on transport bindings without a request URI.
14
- path;
15
- constructor(props = {}) {
16
- this.path = props.path;
26
+ export const ProbeLevel = {
27
+ /** No probing. Equivalent to omitting the parameter. */
28
+ None: 0,
29
+ /** The publisher can measure and periodically report its estimated bitrate. */
30
+ Report: 1,
31
+ /** The publisher can additionally pad the connection (or send redundant data). */
32
+ Increase: 2,
33
+ };
34
+ /** Map a wire value to a level, saturating unknown values to {@link ProbeLevel.Increase}. */
35
+ function probeFromCode(code) {
36
+ switch (code) {
37
+ case 0n:
38
+ return ProbeLevel.None;
39
+ case 1n:
40
+ return ProbeLevel.Report;
41
+ default:
42
+ return ProbeLevel.Increase;
17
43
  }
18
- async #encode(w) {
19
- if (this.path !== undefined) {
20
- await w.u53(1); // parameter count
21
- const value = new TextEncoder().encode(this.path);
22
- await w.u53(PARAM_PATH);
44
+ }
45
+ /**
46
+ * The direction a client intends to use the session for.
47
+ *
48
+ * A client advertises this in its SETUP so the server can reject a token that lacks the
49
+ * matching scope during the handshake, instead of accepting a session that then silently
50
+ * carries no media (a subscribe-only token used to publish, or vice versa). It only ever
51
+ * narrows what the server grants, so it is not a security boundary: the server still
52
+ * enforces the token's scope regardless.
53
+ */
54
+ export const Role = {
55
+ /** The client may do either, or declined to say. Equivalent to omitting the parameter. */
56
+ Both: 0,
57
+ /** The client will publish tracks (ingest); the server must consume. */
58
+ Publisher: 1,
59
+ /** The client will subscribe to tracks (egress); the server must publish. */
60
+ Subscriber: 2,
61
+ };
62
+ /**
63
+ * Map a wire value to a role, falling back to {@link Role.Both}.
64
+ *
65
+ * The draft requires a receiver that does not recognize the value to treat it as `Both`,
66
+ * so a newer client can't break an older server: it just loses the early reject and defers
67
+ * to the token's scope.
68
+ */
69
+ function roleFromCode(code) {
70
+ switch (code) {
71
+ case 1n:
72
+ return Role.Publisher;
73
+ case 2n:
74
+ return Role.Subscriber;
75
+ default:
76
+ return Role.Both;
77
+ }
78
+ }
79
+ /**
80
+ * A bag of `id -> raw bytes` parameters, the body shared by SETUP. Encoded as a varint
81
+ * count followed by `id, length, value` triples; duplicate ids are rejected on decode.
82
+ */
83
+ class Parameters {
84
+ #entries = new Map();
85
+ /** Set a parameter to a raw byte value, replacing any existing entry. */
86
+ setBytes(id, value) {
87
+ this.#entries.set(id, value);
88
+ }
89
+ /** Return a parameter's raw byte value, if present. */
90
+ getBytes(id) {
91
+ return this.#entries.get(id);
92
+ }
93
+ /** Set a parameter to a varint value, replacing any existing entry. */
94
+ setVarint(id, value) {
95
+ this.#entries.set(id, Varint.encode(Number(value)));
96
+ }
97
+ /** Decode a parameter as a single varint, if present. Throws if trailing bytes remain. */
98
+ getVarint(id) {
99
+ const bytes = this.#entries.get(id);
100
+ if (bytes === undefined)
101
+ return undefined;
102
+ const [value, remain] = Varint.decode(bytes);
103
+ if (remain.byteLength !== 0) {
104
+ throw new Error("trailing bytes after varint parameter");
105
+ }
106
+ return BigInt(value);
107
+ }
108
+ async encode(w) {
109
+ if (this.#entries.size > MAX_PARAMS) {
110
+ throw new Error("too many parameters");
111
+ }
112
+ await w.u53(this.#entries.size);
113
+ for (const [id, value] of this.#entries) {
114
+ await w.u62(id);
23
115
  await w.u53(value.byteLength);
24
116
  await w.write(value);
25
117
  }
26
- else {
27
- await w.u53(0); // no parameters
28
- }
29
118
  }
30
- static async #decode(r) {
119
+ static async decode(r) {
120
+ const params = new Parameters();
31
121
  const count = await r.u53();
32
- let path;
122
+ if (count > MAX_PARAMS) {
123
+ throw new Error("too many parameters");
124
+ }
33
125
  for (let i = 0; i < count; i++) {
34
- const id = await r.u53();
35
- const len = await r.u53();
36
- const value = await r.read(len);
37
- // Unknown parameters are ignored so new ones stay backward compatible.
38
- if (id === PARAM_PATH) {
39
- path = new TextDecoder().decode(value);
126
+ const id = await r.u62();
127
+ if (params.#entries.has(id)) {
128
+ throw new Error(`duplicate parameter id: ${id.toString()}`);
40
129
  }
130
+ const size = await r.u53();
131
+ const value = await r.read(size);
132
+ params.#entries.set(id, value);
133
+ }
134
+ return params;
135
+ }
136
+ }
137
+ /**
138
+ * The SETUP message, sent once per endpoint on the unidirectional Setup Stream.
139
+ *
140
+ * lite-05+ only. The two endpoints' SETUP messages are independent: neither side
141
+ * blocks on the peer's before opening other streams, but a stream whose encoding
142
+ * depends on a negotiated capability (e.g. PROBE) must wait for it.
143
+ */
144
+ export class Setup {
145
+ /** The probe capability this endpoint supports. {@link ProbeLevel.None} when absent. */
146
+ probe;
147
+ /**
148
+ * The request path, for transports that carry no request URI (native QUIC, qmux over
149
+ * TCP/TLS, unix sockets). Sent only by the client; a server never sends one and a relay
150
+ * never forwards it. `undefined` on URI-carrying bindings such as WebTransport, where
151
+ * sending one is a protocol violation. An empty string means the same as `undefined`.
152
+ */
153
+ path;
154
+ /**
155
+ * The client's intended {@link Role}. `Both` is sent as the absence of the parameter, so
156
+ * a client that never sets it decodes back to `Both`. Sent only by the client; a server
157
+ * never sends one and a relay never forwards it.
158
+ */
159
+ role;
160
+ constructor({ probe, path, role } = {}) {
161
+ this.probe = probe ?? ProbeLevel.None;
162
+ this.path = path;
163
+ this.role = role ?? Role.Both;
164
+ }
165
+ static #guard(version) {
166
+ if (!hasSetupStream(version)) {
167
+ throw new Error("setup stream not supported for this version");
168
+ }
169
+ }
170
+ async #encode(w) {
171
+ const params = new Parameters();
172
+ // None is the wire default, so omit it to keep the message empty when nothing is set.
173
+ if (this.probe !== ProbeLevel.None) {
174
+ params.setVarint(PARAM_PROBE, this.probe);
41
175
  }
42
- return new Setup({ path });
176
+ if (this.path !== undefined) {
177
+ params.setBytes(PARAM_PATH, new TextEncoder().encode(this.path));
178
+ }
179
+ // Both is the wire default, sent as the absence of the parameter.
180
+ if (this.role !== Role.Both) {
181
+ params.setVarint(PARAM_ROLE, this.role);
182
+ }
183
+ await params.encode(w);
184
+ }
185
+ static async #decode(r) {
186
+ const params = await Parameters.decode(r);
187
+ const probeCode = params.getVarint(PARAM_PROBE);
188
+ const probe = probeCode === undefined ? ProbeLevel.None : probeFromCode(probeCode);
189
+ // An empty path is valid and means the same as omitting the parameter, so a
190
+ // client that wants the root doesn't have to special-case it.
191
+ const pathBytes = params.getBytes(PARAM_PATH);
192
+ const path = pathBytes === undefined ? undefined : new TextDecoder().decode(pathBytes);
193
+ const roleCode = params.getVarint(PARAM_ROLE);
194
+ const role = roleCode === undefined ? Role.Both : roleFromCode(roleCode);
195
+ return new Setup({ probe, path, role });
43
196
  }
197
+ /** Encode the SETUP message with its size prefix. Throws on pre-lite-05 versions. */
44
198
  async encode(w, version) {
45
- if (!hasSetupStream(version))
46
- throw new Error("SETUP requires moq-lite-05+");
199
+ Setup.#guard(version);
47
200
  return Message.encode(w, this.#encode.bind(this));
48
201
  }
202
+ /** Decode a SETUP message with its size prefix. Throws on pre-lite-05 versions. */
49
203
  static async decode(r, version) {
50
- if (!hasSetupStream(version))
51
- throw new Error("SETUP requires moq-lite-05+");
204
+ Setup.#guard(version);
52
205
  return Message.decode(r, Setup.#decode);
53
206
  }
54
207
  }
package/lite/setup.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"setup.js","sourceRoot":"","sources":["../../src/lite/setup.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,OAAO,MAAM,cAAc,CAAC;AACxC,OAAO,EAAE,cAAc,EAAgB,MAAM,cAAc,CAAC;AAE5D,kFAAkF;AAClF,MAAM,UAAU,GAAG,GAAG,CAAC;AAEvB;;;;;GAKG;AACH,MAAM,OAAO,KAAK;IACjB,4EAA4E;IAC5E,IAAI,CAAU;IAEd,YAAY,KAAK,GAAsB,EAAE;QACxC,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC;IACxB,CAAC;IAED,KAAK,CAAC,OAAO,CAAC,CAAS;QACtB,IAAI,IAAI,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;YAC7B,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC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AAE,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IACnD,CAAC;IAED,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAS,EAAE,OAAgB;QAC9C,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC;YAAE,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;QAC7E,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC;IACzC,CAAC;CACD","sourcesContent":["import type { Reader, Writer } from \"../stream.ts\";\nimport * as Message from \"./message.ts\";\nimport { hasSetupStream, type Version } from \"./version.ts\";\n\n/// Setup parameter ID for the request Path (client-only, on URI-less transports).\nconst PARAM_PATH = 0x2;\n\n/**\n * The moq-lite-05 SETUP message, sent once per endpoint on a unidirectional Setup stream.\n *\n * The browser conveys its path via the WebTransport URL, so it never sends a Path\n * parameter; this class still parses one for completeness when reading a peer's SETUP.\n */\nexport class Setup {\n\t/// The request path, only sent on transport bindings without a request URI.\n\tpath?: string;\n\n\tconstructor(props: { path?: string } = {}) {\n\t\tthis.path = props.path;\n\t}\n\n\tasync #encode(w: Writer) {\n\t\tif (this.path !== undefined) {\n\t\t\tawait w.u53(1); // parameter count\n\t\t\tconst value = new TextEncoder().encode(this.path);\n\t\t\tawait w.u53(PARAM_PATH);\n\t\t\tawait w.u53(value.byteLength);\n\t\t\tawait w.write(value);\n\t\t} else {\n\t\t\tawait w.u53(0); // no parameters\n\t\t}\n\t}\n\n\tstatic async #decode(r: Reader): Promise<Setup> {\n\t\tconst count = await r.u53();\n\t\tlet path: string | undefined;\n\t\tfor (let i = 0; i < count; i++) {\n\t\t\tconst id = await r.u53();\n\t\t\tconst len = await r.u53();\n\t\t\tconst value = await r.read(len);\n\t\t\t// Unknown parameters are ignored so new ones stay backward compatible.\n\t\t\tif (id === PARAM_PATH) {\n\t\t\t\tpath = new TextDecoder().decode(value);\n\t\t\t}\n\t\t}\n\t\treturn new Setup({ path });\n\t}\n\n\tasync encode(w: Writer, version: Version): Promise<void> {\n\t\tif (!hasSetupStream(version)) throw new Error(\"SETUP requires moq-lite-05+\");\n\t\treturn Message.encode(w, this.#encode.bind(this));\n\t}\n\n\tstatic async decode(r: Reader, version: Version): Promise<Setup> {\n\t\tif (!hasSetupStream(version)) throw new Error(\"SETUP requires moq-lite-05+\");\n\t\treturn Message.decode(r, Setup.#decode);\n\t}\n}\n"]}
1
+ {"version":3,"file":"setup.js","sourceRoot":"","sources":["../../src/lite/setup.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,MAAM,MAAM,cAAc,CAAC;AACvC,OAAO,KAAK,OAAO,MAAM,cAAc,CAAC;AACxC,OAAO,EAAE,cAAc,EAAgB,MAAM,cAAc,CAAC;AAE5D,yDAAyD;AACzD,MAAM,WAAW,GAAG,IAAI,CAAC;AACzB,kFAAkF;AAClF,MAAM,UAAU,GAAG,IAAI,CAAC;AACxB,+EAA+E;AAC/E,MAAM,UAAU,GAAG,IAAI,CAAC;AAExB,2EAA2E;AAC3E,MAAM,UAAU,GAAG,EAAE,CAAC;AAEtB;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG;IACzB,wDAAwD;IACxD,IAAI,EAAE,CAAC;IACP,+EAA+E;IAC/E,MAAM,EAAE,CAAC;IACT,kFAAkF;IAClF,QAAQ,EAAE,CAAC;CACF,CAAC;AAKX,6FAA6F;AAC7F,SAAS,aAAa,CAAC,IAAY;IAClC,QAAQ,IAAI,EAAE,CAAC;QACd,KAAK,EAAE;YACN,OAAO,UAAU,CAAC,IAAI,CAAC;QACxB,KAAK,EAAE;YACN,OAAO,UAAU,CAAC,MAAM,CAAC;QAC1B;YACC,OAAO,UAAU,CAAC,QAAQ,CAAC;IAC7B,CAAC;AACF,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,IAAI,GAAG;IACnB,0FAA0F;IAC1F,IAAI,EAAE,CAAC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,CAAC,CAAC,CAAC;IACnD,CAAC;IAED,mFAAmF;IACnF,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAS,EAAE,OAAgB;QAC9C,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QACtB,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC;IACzC,CAAC;CACD","sourcesContent":["/**\n * The lite-05 SETUP message: each endpoint advertises its capabilities once, as the\n * sole message on a unidirectional Setup Stream ({@link DataType.Setup}), then closes it.\n *\n * @module\n */\n\nimport type { Reader, Writer } from \"../stream.ts\";\nimport * as Varint from \"../varint.ts\";\nimport * as Message from \"./message.ts\";\nimport { hasSetupStream, type Version } from \"./version.ts\";\n\n/** Setup Parameter id for the Probe capability level. */\nconst PARAM_PROBE = 0x1n;\n/** Setup Parameter id for the request Path (client-only, URI-less transports). */\nconst PARAM_PATH = 0x2n;\n/** Setup Parameter id for the client's intended {@link Role} (client-only). */\nconst PARAM_ROLE = 0x3n;\n\n/** Cap on the number of parameters in a bag, matching the Rust decoder. */\nconst MAX_PARAMS = 64;\n\n/**\n * The probe capability an endpoint advertises in SETUP.\n *\n * Monotonic: a higher level implies every lower one. An unknown (future) value\n * decodes as the highest level we understand, so a peer that gains a new level is\n * treated as at least {@link ProbeLevel.Increase}.\n */\nexport const ProbeLevel = {\n\t/** No probing. Equivalent to omitting the parameter. */\n\tNone: 0,\n\t/** The publisher can measure and periodically report its estimated bitrate. */\n\tReport: 1,\n\t/** The publisher can additionally pad the connection (or send redundant data). */\n\tIncrease: 2,\n} as const;\n\n/** A probe capability level. See {@link ProbeLevel}. */\nexport type ProbeLevel = (typeof ProbeLevel)[keyof typeof ProbeLevel];\n\n/** Map a wire value to a level, saturating unknown values to {@link ProbeLevel.Increase}. */\nfunction probeFromCode(code: bigint): ProbeLevel {\n\tswitch (code) {\n\t\tcase 0n:\n\t\t\treturn ProbeLevel.None;\n\t\tcase 1n:\n\t\t\treturn ProbeLevel.Report;\n\t\tdefault:\n\t\t\treturn ProbeLevel.Increase;\n\t}\n}\n\n/**\n * The direction a client intends to use the session for.\n *\n * A client advertises this in its SETUP so the server can reject a token that lacks the\n * matching scope during the handshake, instead of accepting a session that then silently\n * carries no media (a subscribe-only token used to publish, or vice versa). It only ever\n * narrows what the server grants, so it is not a security boundary: the server still\n * enforces the token's scope regardless.\n */\nexport const Role = {\n\t/** The client may do either, or declined to say. Equivalent to omitting the parameter. */\n\tBoth: 0,\n\t/** The client will publish tracks (ingest); the server must consume. */\n\tPublisher: 1,\n\t/** The client will subscribe to tracks (egress); the server must publish. */\n\tSubscriber: 2,\n} as const;\n\n/** A session role. See {@link Role}. */\nexport type Role = (typeof Role)[keyof typeof Role];\n\n/**\n * Map a wire value to a role, falling back to {@link Role.Both}.\n *\n * The draft requires a receiver that does not recognize the value to treat it as `Both`,\n * so a newer client can't break an older server: it just loses the early reject and defers\n * to the token's scope.\n */\nfunction roleFromCode(code: bigint): Role {\n\tswitch (code) {\n\t\tcase 1n:\n\t\t\treturn Role.Publisher;\n\t\tcase 2n:\n\t\t\treturn Role.Subscriber;\n\t\tdefault:\n\t\t\treturn Role.Both;\n\t}\n}\n\n/**\n * A bag of `id -> raw bytes` parameters, the body shared by SETUP. Encoded as a varint\n * count followed by `id, length, value` triples; duplicate ids are rejected on decode.\n */\nclass Parameters {\n\t#entries = new Map<bigint, Uint8Array>();\n\n\t/** Set a parameter to a raw byte value, replacing any existing entry. */\n\tsetBytes(id: bigint, value: Uint8Array) {\n\t\tthis.#entries.set(id, value);\n\t}\n\n\t/** Return a parameter's raw byte value, if present. */\n\tgetBytes(id: bigint): Uint8Array | undefined {\n\t\treturn this.#entries.get(id);\n\t}\n\n\t/** Set a parameter to a varint value, replacing any existing entry. */\n\tsetVarint(id: bigint, value: number | bigint) {\n\t\tthis.#entries.set(id, Varint.encode(Number(value)));\n\t}\n\n\t/** Decode a parameter as a single varint, if present. Throws if trailing bytes remain. */\n\tgetVarint(id: bigint): bigint | undefined {\n\t\tconst bytes = this.#entries.get(id);\n\t\tif (bytes === undefined) return undefined;\n\t\tconst [value, remain] = Varint.decode(bytes);\n\t\tif (remain.byteLength !== 0) {\n\t\t\tthrow new Error(\"trailing bytes after varint parameter\");\n\t\t}\n\t\treturn BigInt(value);\n\t}\n\n\tasync encode(w: Writer) {\n\t\tif (this.#entries.size > MAX_PARAMS) {\n\t\t\tthrow new Error(\"too many parameters\");\n\t\t}\n\n\t\tawait w.u53(this.#entries.size);\n\t\tfor (const [id, value] of this.#entries) {\n\t\t\tawait w.u62(id);\n\t\t\tawait w.u53(value.byteLength);\n\t\t\tawait w.write(value);\n\t\t}\n\t}\n\n\tstatic async decode(r: Reader): Promise<Parameters> {\n\t\tconst params = new Parameters();\n\n\t\tconst count = await r.u53();\n\t\tif (count > MAX_PARAMS) {\n\t\t\tthrow new Error(\"too many parameters\");\n\t\t}\n\n\t\tfor (let i = 0; i < count; i++) {\n\t\t\tconst id = await r.u62();\n\t\t\tif (params.#entries.has(id)) {\n\t\t\t\tthrow new Error(`duplicate parameter id: ${id.toString()}`);\n\t\t\t}\n\t\t\tconst size = await r.u53();\n\t\t\tconst value = await r.read(size);\n\t\t\tparams.#entries.set(id, value);\n\t\t}\n\n\t\treturn params;\n\t}\n}\n\n/** The capabilities a {@link Setup} advertises. Each defaults to the wire default, which is the absence of the parameter. */\nexport interface SetupProps {\n\t/** See {@link Setup.probe}. Defaults to {@link ProbeLevel.None}. */\n\tprobe?: ProbeLevel;\n\t/** See {@link Setup.path}. Omitted by default. */\n\tpath?: string;\n\t/** See {@link Setup.role}. Defaults to {@link Role.Both}. */\n\trole?: Role;\n}\n\n/**\n * The SETUP message, sent once per endpoint on the unidirectional Setup Stream.\n *\n * lite-05+ only. The two endpoints' SETUP messages are independent: neither side\n * blocks on the peer's before opening other streams, but a stream whose encoding\n * depends on a negotiated capability (e.g. PROBE) must wait for it.\n */\nexport class Setup {\n\t/** The probe capability this endpoint supports. {@link ProbeLevel.None} when absent. */\n\tprobe: ProbeLevel;\n\n\t/**\n\t * The request path, for transports that carry no request URI (native QUIC, qmux over\n\t * TCP/TLS, unix sockets). Sent only by the client; a server never sends one and a relay\n\t * never forwards it. `undefined` on URI-carrying bindings such as WebTransport, where\n\t * sending one is a protocol violation. An empty string means the same as `undefined`.\n\t */\n\tpath?: string;\n\n\t/**\n\t * The client's intended {@link Role}. `Both` is sent as the absence of the parameter, so\n\t * a client that never sets it decodes back to `Both`. Sent only by the client; a server\n\t * never sends one and a relay never forwards it.\n\t */\n\trole: Role;\n\n\tconstructor({ probe, path, role }: SetupProps = {}) {\n\t\tthis.probe = probe ?? ProbeLevel.None;\n\t\tthis.path = path;\n\t\tthis.role = role ?? Role.Both;\n\t}\n\n\tstatic #guard(version: Version) {\n\t\tif (!hasSetupStream(version)) {\n\t\t\tthrow new Error(\"setup stream not supported for this version\");\n\t\t}\n\t}\n\n\tasync #encode(w: Writer) {\n\t\tconst params = new Parameters();\n\t\t// None is the wire default, so omit it to keep the message empty when nothing is set.\n\t\tif (this.probe !== ProbeLevel.None) {\n\t\t\tparams.setVarint(PARAM_PROBE, this.probe);\n\t\t}\n\t\tif (this.path !== undefined) {\n\t\t\tparams.setBytes(PARAM_PATH, new TextEncoder().encode(this.path));\n\t\t}\n\t\t// Both is the wire default, sent as the absence of the parameter.\n\t\tif (this.role !== Role.Both) {\n\t\t\tparams.setVarint(PARAM_ROLE, this.role);\n\t\t}\n\t\tawait params.encode(w);\n\t}\n\n\tstatic async #decode(r: Reader): Promise<Setup> {\n\t\tconst params = await Parameters.decode(r);\n\n\t\tconst probeCode = params.getVarint(PARAM_PROBE);\n\t\tconst probe = probeCode === undefined ? ProbeLevel.None : probeFromCode(probeCode);\n\n\t\t// An empty path is valid and means the same as omitting the parameter, so a\n\t\t// client that wants the root doesn't have to special-case it.\n\t\tconst pathBytes = params.getBytes(PARAM_PATH);\n\t\tconst path = pathBytes === undefined ? undefined : new TextDecoder().decode(pathBytes);\n\n\t\tconst roleCode = params.getVarint(PARAM_ROLE);\n\t\tconst role = roleCode === undefined ? Role.Both : roleFromCode(roleCode);\n\n\t\treturn new Setup({ probe, path, role });\n\t}\n\n\t/** Encode the SETUP message with its size prefix. Throws on pre-lite-05 versions. */\n\tasync encode(w: Writer, version: Version): Promise<void> {\n\t\tSetup.#guard(version);\n\t\treturn Message.encode(w, this.#encode.bind(this));\n\t}\n\n\t/** Decode a SETUP message with its size prefix. Throws on pre-lite-05 versions. */\n\tstatic async decode(r: Reader, version: Version): Promise<Setup> {\n\t\tSetup.#guard(version);\n\t\treturn Message.decode(r, Setup.#decode);\n\t}\n}\n"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=setup.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"setup.test.d.ts","sourceRoot":"","sources":["../../src/lite/setup.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,122 @@
1
+ /* @ts-self-types="./setup.test.d.ts" */
2
+ import { expect, test } from "bun:test";
3
+ import { Reader, Writer } from "../stream.js";
4
+ import * as Varint from "../varint.js";
5
+ import { ProbeLevel, Role, Setup } from "./setup.js";
6
+ import { Version } from "./version.js";
7
+ // Parameter ids, duplicated from setup.ts so a typo there fails a test rather than
8
+ // silently agreeing with itself.
9
+ const PARAM_PROBE = 0x1n;
10
+ const PARAM_PATH = 0x2n;
11
+ const PARAM_ROLE = 0x3n;
12
+ function concat(chunks) {
13
+ const total = chunks.reduce((sum, c) => sum + c.byteLength, 0);
14
+ const out = new Uint8Array(total);
15
+ let offset = 0;
16
+ for (const c of chunks) {
17
+ out.set(c, offset);
18
+ offset += c.byteLength;
19
+ }
20
+ return out;
21
+ }
22
+ async function bytes(f) {
23
+ const written = [];
24
+ const writer = new Writer(new WritableStream({ write: (chunk) => void written.push(new Uint8Array(chunk)) }));
25
+ await f(writer);
26
+ writer.close();
27
+ await writer.closed;
28
+ return concat(written);
29
+ }
30
+ async function roundTrip(msg) {
31
+ const reader = new Reader(undefined, await bytes((w) => msg.encode(w, Version.DRAFT_05)));
32
+ const got = await Setup.decode(reader, Version.DRAFT_05);
33
+ expect(await reader.done()).toBe(true);
34
+ return got;
35
+ }
36
+ // Hand-frame a SETUP carrying a single raw parameter, so we can feed the decoder values
37
+ // our own encoder would never produce (unknown levels, empty paths).
38
+ async function decodeParam(id, value) {
39
+ const body = await bytes(async (w) => {
40
+ await w.u53(1); // parameter count
41
+ await w.u62(id);
42
+ await w.u53(value.byteLength);
43
+ if (value.byteLength > 0)
44
+ await w.write(value);
45
+ });
46
+ const framed = await bytes(async (w) => {
47
+ await w.u53(body.byteLength); // Message size prefix
48
+ await w.write(body);
49
+ });
50
+ return await Setup.decode(new Reader(undefined, framed), Version.DRAFT_05);
51
+ }
52
+ test("empty SETUP round-trips on draft-05", async () => {
53
+ const got = await roundTrip(new Setup());
54
+ expect(got.probe).toBe(ProbeLevel.None);
55
+ expect(got.path).toBeUndefined();
56
+ expect(got.role).toBe(Role.Both);
57
+ });
58
+ test("each probe level round-trips on draft-05", async () => {
59
+ for (const probe of [ProbeLevel.None, ProbeLevel.Report, ProbeLevel.Increase]) {
60
+ const got = await roundTrip(new Setup({ probe }));
61
+ expect(got.probe).toBe(probe);
62
+ expect(got.path).toBeUndefined();
63
+ }
64
+ });
65
+ test("SETUP with path round-trips on draft-05", async () => {
66
+ const got = await roundTrip(new Setup({ probe: ProbeLevel.Report, path: "/room/123" }));
67
+ expect(got.probe).toBe(ProbeLevel.Report);
68
+ expect(got.path).toBe("/room/123");
69
+ });
70
+ test("each role round-trips on draft-05", async () => {
71
+ for (const role of [Role.Both, Role.Publisher, Role.Subscriber]) {
72
+ const got = await roundTrip(new Setup({ role }));
73
+ expect(got.role).toBe(role);
74
+ }
75
+ });
76
+ test("SETUP carries role alongside probe and path", async () => {
77
+ const got = await roundTrip(new Setup({ probe: ProbeLevel.Increase, path: "/room/123", role: Role.Publisher }));
78
+ expect(got.probe).toBe(ProbeLevel.Increase);
79
+ expect(got.path).toBe("/room/123");
80
+ expect(got.role).toBe(Role.Publisher);
81
+ });
82
+ test("Both is the wire default, so it is omitted rather than encoded", async () => {
83
+ // Both must be the absence of the parameter, so a server that predates it decodes a
84
+ // Both client back to Both. Assert the empty bag directly: comparing against another
85
+ // default-constructed Setup would agree with itself even if we encoded Both.
86
+ const both = await bytes((w) => new Setup({ role: Role.Both }).encode(w, Version.DRAFT_05));
87
+ const emptyBag = await bytes(async (w) => {
88
+ await w.u53(1); // Message size prefix: one byte of body follows
89
+ await w.u53(0); // parameter count
90
+ });
91
+ expect(both).toEqual(emptyBag);
92
+ // A directional role is still encoded, so the check above can actually fail.
93
+ const publisher = await bytes((w) => new Setup({ role: Role.Publisher }).encode(w, Version.DRAFT_05));
94
+ expect(publisher.byteLength).toBeGreaterThan(both.byteLength);
95
+ });
96
+ test("unknown role falls back to Both", async () => {
97
+ // Hand-frame a SETUP carrying an unrecognized role (99). The draft requires a receiver
98
+ // that doesn't know the value to treat it as Both, so a newer client can't break us.
99
+ const got = await decodeParam(PARAM_ROLE, Varint.encode(99));
100
+ expect(got.role).toBe(Role.Both);
101
+ });
102
+ test("role 0 decodes as Both", async () => {
103
+ const got = await decodeParam(PARAM_ROLE, Varint.encode(0));
104
+ expect(got.role).toBe(Role.Both);
105
+ });
106
+ test("unknown probe level saturates to Increase", async () => {
107
+ const got = await decodeParam(PARAM_PROBE, Varint.encode(99));
108
+ expect(got.probe).toBe(ProbeLevel.Increase);
109
+ });
110
+ test("SETUP is rejected before draft-05", async () => {
111
+ await expect(bytes((w) => new Setup().encode(w, Version.DRAFT_04))).rejects.toThrow();
112
+ });
113
+ test("SETUP decode is rejected before draft-05", async () => {
114
+ const framed = await bytes((w) => new Setup().encode(w, Version.DRAFT_05));
115
+ await expect(Setup.decode(new Reader(undefined, framed), Version.DRAFT_04)).rejects.toThrow();
116
+ });
117
+ test("empty path decodes as the root", async () => {
118
+ // Valid on the wire and equivalent to omitting the parameter.
119
+ const got = await decodeParam(PARAM_PATH, new Uint8Array());
120
+ expect(got.path).toBe("");
121
+ });
122
+ //# sourceMappingURL=setup.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"setup.test.js","sourceRoot":"","sources":["../../src/lite/setup.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AACxC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,KAAK,MAAM,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACrD,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAEvC,mFAAmF;AACnF,iCAAiC;AACjC,MAAM,WAAW,GAAG,IAAI,CAAC;AACzB,MAAM,UAAU,GAAG,IAAI,CAAC;AACxB,MAAM,UAAU,GAAG,IAAI,CAAC;AAExB,SAAS,MAAM,CAAC,MAAoB;IACnC,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC,GAAG,GAAG,CAAC,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC;IAC/D,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,KAAK,CAAC,CAAC;IAClC,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,KAAK,MAAM,CAAC,IAAI,MAAM,EAAE,CAAC;QACxB,GAAG,CAAC,GAAG,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;QACnB,MAAM,IAAI,CAAC,CAAC,UAAU,CAAC;IACxB,CAAC;IACD,OAAO,GAAG,CAAC;AACZ,CAAC;AAED,KAAK,UAAU,KAAK,CAAC,CAA+B;IACnD,MAAM,OAAO,GAAiB,EAAE,CAAC;IACjC,MAAM,MAAM,GAAG,IAAI,MAAM,CACxB,IAAI,cAAc,CAAa,EAAE,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,K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;IAC9D,MAAM,GAAG,GAAG,MAAM,WAAW,CAAC,UAAU,EAAE,IAAI,UAAU,EAAE,CAAC,CAAC;IAC5D,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AAC3B,CAAC,CAAC,CAAC","sourcesContent":["import { expect, test } from \"bun:test\";\nimport { Reader, Writer } from \"../stream.ts\";\nimport * as Varint from \"../varint.ts\";\nimport { ProbeLevel, Role, Setup } from \"./setup.ts\";\nimport { Version } from \"./version.ts\";\n\n// Parameter ids, duplicated from setup.ts so a typo there fails a test rather than\n// silently agreeing with itself.\nconst PARAM_PROBE = 0x1n;\nconst PARAM_PATH = 0x2n;\nconst PARAM_ROLE = 0x3n;\n\nfunction concat(chunks: Uint8Array[]): Uint8Array {\n\tconst total = chunks.reduce((sum, c) => sum + c.byteLength, 0);\n\tconst out = new Uint8Array(total);\n\tlet offset = 0;\n\tfor (const c of chunks) {\n\t\tout.set(c, offset);\n\t\toffset += c.byteLength;\n\t}\n\treturn out;\n}\n\nasync function bytes(f: (w: Writer) => Promise<void>): Promise<Uint8Array> {\n\tconst written: Uint8Array[] = [];\n\tconst writer = new Writer(\n\t\tnew WritableStream<Uint8Array>({ write: (chunk) => void written.push(new Uint8Array(chunk)) }),\n\t);\n\tawait f(writer);\n\twriter.close();\n\tawait writer.closed;\n\treturn concat(written);\n}\n\nasync function roundTrip(msg: Setup): Promise<Setup> {\n\tconst reader = new Reader(undefined, await bytes((w) => msg.encode(w, Version.DRAFT_05)));\n\tconst got = await Setup.decode(reader, Version.DRAFT_05);\n\texpect(await reader.done()).toBe(true);\n\treturn got;\n}\n\n// Hand-frame a SETUP carrying a single raw parameter, so we can feed the decoder values\n// our own encoder would never produce (unknown levels, empty paths).\nasync function decodeParam(id: bigint, value: Uint8Array): Promise<Setup> {\n\tconst body = await bytes(async (w) => {\n\t\tawait w.u53(1); // parameter count\n\t\tawait w.u62(id);\n\t\tawait w.u53(value.byteLength);\n\t\tif (value.byteLength > 0) await w.write(value);\n\t});\n\n\tconst framed = await bytes(async (w) => {\n\t\tawait w.u53(body.byteLength); // Message size prefix\n\t\tawait w.write(body);\n\t});\n\n\treturn await Setup.decode(new Reader(undefined, framed), Version.DRAFT_05);\n}\n\ntest(\"empty SETUP round-trips on draft-05\", async () => {\n\tconst got = await roundTrip(new Setup());\n\texpect(got.probe).toBe(ProbeLevel.None);\n\texpect(got.path).toBeUndefined();\n\texpect(got.role).toBe(Role.Both);\n});\n\ntest(\"each probe level round-trips on draft-05\", async () => {\n\tfor (const probe of [ProbeLevel.None, ProbeLevel.Report, ProbeLevel.Increase]) {\n\t\tconst got = await roundTrip(new Setup({ probe }));\n\t\texpect(got.probe).toBe(probe);\n\t\texpect(got.path).toBeUndefined();\n\t}\n});\n\ntest(\"SETUP with path round-trips on draft-05\", async () => {\n\tconst got = await roundTrip(new Setup({ probe: ProbeLevel.Report, path: \"/room/123\" }));\n\texpect(got.probe).toBe(ProbeLevel.Report);\n\texpect(got.path).toBe(\"/room/123\");\n});\n\ntest(\"each role round-trips on draft-05\", async () => {\n\tfor (const role of [Role.Both, Role.Publisher, Role.Subscriber]) {\n\t\tconst got = await roundTrip(new Setup({ role }));\n\t\texpect(got.role).toBe(role);\n\t}\n});\n\ntest(\"SETUP carries role alongside probe and path\", async () => {\n\tconst got = await roundTrip(new Setup({ probe: ProbeLevel.Increase, path: \"/room/123\", role: Role.Publisher }));\n\texpect(got.probe).toBe(ProbeLevel.Increase);\n\texpect(got.path).toBe(\"/room/123\");\n\texpect(got.role).toBe(Role.Publisher);\n});\n\ntest(\"Both is the wire default, so it is omitted rather than encoded\", async () => {\n\t// Both must be the absence of the parameter, so a server that predates it decodes a\n\t// Both client back to Both. Assert the empty bag directly: comparing against another\n\t// default-constructed Setup would agree with itself even if we encoded Both.\n\tconst both = await bytes((w) => new Setup({ role: Role.Both }).encode(w, Version.DRAFT_05));\n\tconst emptyBag = await bytes(async (w) => {\n\t\tawait w.u53(1); // Message size prefix: one byte of body follows\n\t\tawait w.u53(0); // parameter count\n\t});\n\texpect(both).toEqual(emptyBag);\n\n\t// A directional role is still encoded, so the check above can actually fail.\n\tconst publisher = await bytes((w) => new Setup({ role: Role.Publisher }).encode(w, Version.DRAFT_05));\n\texpect(publisher.byteLength).toBeGreaterThan(both.byteLength);\n});\n\ntest(\"unknown role falls back to Both\", async () => {\n\t// Hand-frame a SETUP carrying an unrecognized role (99). The draft requires a receiver\n\t// that doesn't know the value to treat it as Both, so a newer client can't break us.\n\tconst got = await decodeParam(PARAM_ROLE, Varint.encode(99));\n\texpect(got.role).toBe(Role.Both);\n});\n\ntest(\"role 0 decodes as Both\", async () => {\n\tconst got = await decodeParam(PARAM_ROLE, Varint.encode(0));\n\texpect(got.role).toBe(Role.Both);\n});\n\ntest(\"unknown probe level saturates to Increase\", async () => {\n\tconst got = await decodeParam(PARAM_PROBE, Varint.encode(99));\n\texpect(got.probe).toBe(ProbeLevel.Increase);\n});\n\ntest(\"SETUP is rejected before draft-05\", async () => {\n\tawait expect(bytes((w) => new Setup().encode(w, Version.DRAFT_04))).rejects.toThrow();\n});\n\ntest(\"SETUP decode is rejected before draft-05\", async () => {\n\tconst framed = await bytes((w) => new Setup().encode(w, Version.DRAFT_05));\n\tawait expect(Setup.decode(new Reader(undefined, framed), Version.DRAFT_04)).rejects.toThrow();\n});\n\ntest(\"empty path decodes as the root\", async () => {\n\t// Valid on the wire and equivalent to omitting the parameter.\n\tconst got = await decodeParam(PARAM_PATH, new Uint8Array());\n\texpect(got.path).toBe(\"\");\n});\n"]}