@moq/net 0.1.9 → 0.2.1

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 (259) 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 +159 -62
  47. package/connection/reload.js.map +1 -1
  48. package/connection/reload.test.js +65 -0
  49. package/connection/reload.test.js.map +1 -1
  50. package/connection/stats.d.ts +92 -0
  51. package/connection/stats.d.ts.map +1 -0
  52. package/connection/stats.js +35 -0
  53. package/connection/stats.js.map +1 -0
  54. package/connection/stats.test.d.ts +2 -0
  55. package/connection/stats.test.d.ts.map +1 -0
  56. package/connection/stats.test.js +120 -0
  57. package/connection/stats.test.js.map +1 -0
  58. package/connection/transport.d.ts +10 -0
  59. package/connection/transport.d.ts.map +1 -0
  60. package/connection/transport.js +12 -0
  61. package/connection/transport.js.map +1 -0
  62. package/consume.d.ts +26 -0
  63. package/consume.d.ts.map +1 -0
  64. package/consume.js +40 -0
  65. package/consume.js.map +1 -0
  66. package/datagram.d.ts +23 -0
  67. package/datagram.d.ts.map +1 -0
  68. package/datagram.js +3 -0
  69. package/datagram.js.map +1 -0
  70. package/error.d.ts +62 -0
  71. package/error.d.ts.map +1 -0
  72. package/error.js +92 -0
  73. package/error.js.map +1 -0
  74. package/error.test.d.ts +2 -0
  75. package/error.test.d.ts.map +1 -0
  76. package/error.test.js +89 -0
  77. package/error.test.js.map +1 -0
  78. package/group.d.ts +125 -36
  79. package/group.d.ts.map +1 -1
  80. package/group.js +244 -67
  81. package/group.js.map +1 -1
  82. package/group.test.js +121 -35
  83. package/group.test.js.map +1 -1
  84. package/ietf/adapter.d.ts +2 -2
  85. package/ietf/adapter.d.ts.map +1 -1
  86. package/ietf/adapter.js +15 -5
  87. package/ietf/adapter.js.map +1 -1
  88. package/ietf/connection.d.ts +19 -6
  89. package/ietf/connection.d.ts.map +1 -1
  90. package/ietf/connection.js +23 -8
  91. package/ietf/connection.js.map +1 -1
  92. package/ietf/ietf.test.js +58 -1
  93. package/ietf/ietf.test.js.map +1 -1
  94. package/ietf/object.d.ts +12 -4
  95. package/ietf/object.d.ts.map +1 -1
  96. package/ietf/object.js +92 -9
  97. package/ietf/object.js.map +1 -1
  98. package/ietf/publisher.d.ts +2 -2
  99. package/ietf/publisher.d.ts.map +1 -1
  100. package/ietf/publisher.js +12 -12
  101. package/ietf/publisher.js.map +1 -1
  102. package/ietf/subscribe_namespace.d.ts +0 -4
  103. package/ietf/subscribe_namespace.d.ts.map +1 -1
  104. package/ietf/subscribe_namespace.js +0 -5
  105. package/ietf/subscribe_namespace.js.map +1 -1
  106. package/ietf/subscriber.d.ts +8 -4
  107. package/ietf/subscriber.d.ts.map +1 -1
  108. package/ietf/subscriber.js +171 -93
  109. package/ietf/subscriber.js.map +1 -1
  110. package/index.d.ts +10 -5
  111. package/index.d.ts.map +1 -1
  112. package/index.js +10 -5
  113. package/index.js.map +1 -1
  114. package/integration.test.js +634 -12
  115. package/integration.test.js.map +1 -1
  116. package/internal.d.ts +14 -0
  117. package/internal.d.ts.map +1 -0
  118. package/internal.js +8 -0
  119. package/internal.js.map +1 -0
  120. package/lite/announce.d.ts +55 -13
  121. package/lite/announce.d.ts.map +1 -1
  122. package/lite/announce.js +221 -68
  123. package/lite/announce.js.map +1 -1
  124. package/lite/announce.test.d.ts +2 -0
  125. package/lite/announce.test.d.ts.map +1 -0
  126. package/lite/announce.test.js +71 -0
  127. package/lite/announce.test.js.map +1 -0
  128. package/lite/connection.d.ts +45 -19
  129. package/lite/connection.d.ts.map +1 -1
  130. package/lite/connection.js +85 -80
  131. package/lite/connection.js.map +1 -1
  132. package/lite/datagram.d.ts +23 -0
  133. package/lite/datagram.d.ts.map +1 -0
  134. package/lite/datagram.js +57 -0
  135. package/lite/datagram.js.map +1 -0
  136. package/lite/datagram.test.d.ts +2 -0
  137. package/lite/datagram.test.d.ts.map +1 -0
  138. package/lite/datagram.test.js +26 -0
  139. package/lite/datagram.test.js.map +1 -0
  140. package/lite/datagram_stream.d.ts +8 -0
  141. package/lite/datagram_stream.d.ts.map +1 -0
  142. package/lite/datagram_stream.js +44 -0
  143. package/lite/datagram_stream.js.map +1 -0
  144. package/lite/datagram_stream.test.d.ts +2 -0
  145. package/lite/datagram_stream.test.d.ts.map +1 -0
  146. package/lite/datagram_stream.test.js +15 -0
  147. package/lite/datagram_stream.test.js.map +1 -0
  148. package/lite/fetch.d.ts.map +1 -1
  149. package/lite/fetch.js +2 -2
  150. package/lite/fetch.js.map +1 -1
  151. package/lite/fetch.test.d.ts +2 -0
  152. package/lite/fetch.test.d.ts.map +1 -0
  153. package/lite/fetch.test.js +41 -0
  154. package/lite/fetch.test.js.map +1 -0
  155. package/lite/index.d.ts +3 -0
  156. package/lite/index.d.ts.map +1 -1
  157. package/lite/index.js +3 -0
  158. package/lite/index.js.map +1 -1
  159. package/lite/publisher.d.ts +13 -6
  160. package/lite/publisher.d.ts.map +1 -1
  161. package/lite/publisher.js +274 -64
  162. package/lite/publisher.js.map +1 -1
  163. package/lite/publisher.test.d.ts +2 -0
  164. package/lite/publisher.test.d.ts.map +1 -0
  165. package/lite/publisher.test.js +65 -0
  166. package/lite/publisher.test.js.map +1 -0
  167. package/lite/setup.d.ts +72 -6
  168. package/lite/setup.d.ts.map +1 -1
  169. package/lite/setup.js +184 -31
  170. package/lite/setup.js.map +1 -1
  171. package/lite/setup.test.d.ts +2 -0
  172. package/lite/setup.test.d.ts.map +1 -0
  173. package/lite/setup.test.js +122 -0
  174. package/lite/setup.test.js.map +1 -0
  175. package/lite/stream.d.ts +12 -3
  176. package/lite/stream.d.ts.map +1 -1
  177. package/lite/stream.js +7 -4
  178. package/lite/stream.js.map +1 -1
  179. package/lite/subscribe.d.ts +37 -23
  180. package/lite/subscribe.d.ts.map +1 -1
  181. package/lite/subscribe.js +70 -55
  182. package/lite/subscribe.js.map +1 -1
  183. package/lite/subscribe.test.d.ts +2 -0
  184. package/lite/subscribe.test.d.ts.map +1 -0
  185. package/lite/subscribe.test.js +68 -0
  186. package/lite/subscribe.test.js.map +1 -0
  187. package/lite/subscriber.d.ts +24 -17
  188. package/lite/subscriber.d.ts.map +1 -1
  189. package/lite/subscriber.js +515 -90
  190. package/lite/subscriber.js.map +1 -1
  191. package/lite/subscriber.test.js +2 -2
  192. package/lite/subscriber.test.js.map +1 -1
  193. package/lite/track.d.ts +25 -13
  194. package/lite/track.d.ts.map +1 -1
  195. package/lite/track.js +60 -35
  196. package/lite/track.js.map +1 -1
  197. package/lite/track.test.js +12 -10
  198. package/lite/track.test.js.map +1 -1
  199. package/lite/version.d.ts +17 -4
  200. package/lite/version.d.ts.map +1 -1
  201. package/lite/version.js +88 -17
  202. package/lite/version.js.map +1 -1
  203. package/mock.d.ts +42 -3
  204. package/mock.d.ts.map +1 -1
  205. package/mock.js +88 -17
  206. package/mock.js.map +1 -1
  207. package/package.json +3 -3
  208. package/path.d.ts +30 -0
  209. package/path.d.ts.map +1 -1
  210. package/path.js +49 -0
  211. package/path.js.map +1 -1
  212. package/path.test.js +41 -0
  213. package/path.test.js.map +1 -1
  214. package/stream.d.ts.map +1 -1
  215. package/stream.js +25 -4
  216. package/stream.js.map +1 -1
  217. package/stream.test.js +42 -0
  218. package/stream.test.js.map +1 -1
  219. package/time.d.ts +109 -86
  220. package/time.d.ts.map +1 -1
  221. package/time.js +80 -30
  222. package/time.js.map +1 -1
  223. package/time.test.d.ts +2 -0
  224. package/time.test.d.ts.map +1 -0
  225. package/time.test.js +12 -0
  226. package/time.test.js.map +1 -0
  227. package/track.d.ts +225 -45
  228. package/track.d.ts.map +1 -1
  229. package/track.js +505 -93
  230. package/track.js.map +1 -1
  231. package/track.test.js +138 -29
  232. package/track.test.js.map +1 -1
  233. package/util/timeout.d.ts +5 -0
  234. package/util/timeout.d.ts.map +1 -0
  235. package/util/timeout.js +18 -0
  236. package/util/timeout.js.map +1 -0
  237. package/util/timeout.test.d.ts +2 -0
  238. package/util/timeout.test.d.ts.map +1 -0
  239. package/util/timeout.test.js +18 -0
  240. package/util/timeout.test.js.map +1 -0
  241. package/varint.d.ts +6 -0
  242. package/varint.d.ts.map +1 -1
  243. package/varint.js +6 -2
  244. package/varint.js.map +1 -1
  245. package/zod.d.ts +4 -4
  246. package/zod.d.ts.map +1 -1
  247. package/zod.js.map +1 -1
  248. package/bandwidth.d.ts +0 -11
  249. package/bandwidth.d.ts.map +0 -1
  250. package/bandwidth.js +0 -7
  251. package/bandwidth.js.map +0 -1
  252. package/util/error.d.ts +0 -3
  253. package/util/error.d.ts.map +0 -1
  254. package/util/error.js +0 -9
  255. package/util/error.js.map +0 -1
  256. package/util/index.d.ts +0 -2
  257. package/util/index.d.ts.map +0 -1
  258. package/util/index.js +0 -3
  259. package/util/index.js.map +0 -1
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetch.test.d.ts","sourceRoot":"","sources":["../../src/lite/fetch.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,41 @@
1
+ /* @ts-self-types="./fetch.test.d.ts" */
2
+ import { expect, test } from "bun:test";
3
+ import * as Path from "../path.js";
4
+ import { Reader, Writer } from "../stream.js";
5
+ import { Fetch } from "./fetch.js";
6
+ import { Version } from "./version.js";
7
+ function concat(chunks) {
8
+ const total = chunks.reduce((sum, c) => sum + c.byteLength, 0);
9
+ const out = new Uint8Array(total);
10
+ let offset = 0;
11
+ for (const c of chunks) {
12
+ out.set(c, offset);
13
+ offset += c.byteLength;
14
+ }
15
+ return out;
16
+ }
17
+ async function encode(version, fetch) {
18
+ const written = [];
19
+ const writer = new Writer(new WritableStream({ write: (chunk) => void written.push(new Uint8Array(chunk)) }));
20
+ await fetch.encode(writer, version);
21
+ writer.close();
22
+ await writer.closed;
23
+ return concat(written);
24
+ }
25
+ async function roundtrip(version, fetch) {
26
+ const reader = new Reader(undefined, await encode(version, fetch));
27
+ return Fetch.decode(reader, version);
28
+ }
29
+ function sample() {
30
+ return new Fetch(Path.from("room/1"), "video", 3, 42);
31
+ }
32
+ test("Fetch round-trips on draft-03/04/05", async () => {
33
+ for (const version of [Version.DRAFT_03, Version.DRAFT_04, Version.DRAFT_05]) {
34
+ const got = await roundtrip(version, sample());
35
+ expect(got.broadcast).toBe(Path.from("room/1"));
36
+ expect(got.track).toBe("video");
37
+ expect(got.priority).toBe(3);
38
+ expect(got.group).toBe(42);
39
+ }
40
+ });
41
+ //# sourceMappingURL=fetch.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetch.test.js","sourceRoot":"","sources":["../../src/lite/fetch.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AACxC,OAAO,KAAK,IAAI,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAEvC,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,MAAM,CAAC,OAAgB,EAAE,KAAY;IACnD,MAAM,OAAO,GAAiB,EAAE,CAAC;IACjC,MAAM,MAAM,GAAG,IAAI,MAAM,CACxB,IAAI,cAAc,CAAa,EAAE,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,IAAI,UAAU,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAC9F,CAAC;IACF,MAAM,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACpC,MAAM,CAAC,KAAK,EAAE,CAAC;IACf,MAAM,MAAM,CAAC,MAAM,CAAC;IACpB,OAAO,MAAM,CAAC,OAAO,CAAC,CAAC;AACxB,CAAC;AAED,KAAK,UAAU,SAAS,CAAC,OAAgB,EAAE,KAAY;IACtD,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,SAAS,EAAE,MAAM,MAAM,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,CAAC;IACnE,OAAO,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AACtC,CAAC;AAED,SAAS,MAAM;IACd,OAAO,IAAI,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC;AACvD,CAAC;AAED,IAAI,CAAC,qCAAqC,EAAE,KAAK,IAAI,EAAE;IACtD,KAAK,MAAM,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,OAAO,CAAC,QAAQ,EAAE,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC;QAC9E,MAAM,GAAG,GAAG,MAAM,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,CAAC,CAAC;QAC/C,MAAM,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC;QAChD,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAChC,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QAC7B,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAC5B,CAAC;AACF,CAAC,CAAC,CAAC","sourcesContent":["import { expect, test } from \"bun:test\";\nimport * as Path from \"../path.ts\";\nimport { Reader, Writer } from \"../stream.ts\";\nimport { Fetch } from \"./fetch.ts\";\nimport { Version } from \"./version.ts\";\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 encode(version: Version, fetch: Fetch): 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 fetch.encode(writer, version);\n\twriter.close();\n\tawait writer.closed;\n\treturn concat(written);\n}\n\nasync function roundtrip(version: Version, fetch: Fetch): Promise<Fetch> {\n\tconst reader = new Reader(undefined, await encode(version, fetch));\n\treturn Fetch.decode(reader, version);\n}\n\nfunction sample(): Fetch {\n\treturn new Fetch(Path.from(\"room/1\"), \"video\", 3, 42);\n}\n\ntest(\"Fetch round-trips on draft-03/04/05\", async () => {\n\tfor (const version of [Version.DRAFT_03, Version.DRAFT_04, Version.DRAFT_05]) {\n\t\tconst got = await roundtrip(version, sample());\n\t\texpect(got.broadcast).toBe(Path.from(\"room/1\"));\n\t\texpect(got.track).toBe(\"video\");\n\t\texpect(got.priority).toBe(3);\n\t\texpect(got.group).toBe(42);\n\t}\n});\n"]}
package/lite/index.d.ts CHANGED
@@ -1,11 +1,14 @@
1
1
  export * from "./announce";
2
2
  export * from "./connection";
3
+ export * from "./datagram";
3
4
  export * from "./fetch";
4
5
  export * from "./goaway";
5
6
  export * from "./group";
6
7
  export * from "./probe";
7
8
  export * from "./session";
9
+ export * from "./setup";
8
10
  export * from "./stream";
9
11
  export * from "./subscribe";
12
+ export * from "./track";
10
13
  export * from "./version";
11
14
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/lite/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,cAAc,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/lite/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC"}
package/lite/index.js CHANGED
@@ -1,12 +1,15 @@
1
1
  /* @ts-self-types="./index.d.ts" */
2
2
  export * from "./announce.js";
3
3
  export * from "./connection.js";
4
+ export * from "./datagram.js";
4
5
  export * from "./fetch.js";
5
6
  export * from "./goaway.js";
6
7
  export * from "./group.js";
7
8
  export * from "./probe.js";
8
9
  export * from "./session.js";
10
+ export * from "./setup.js";
9
11
  export * from "./stream.js";
10
12
  export * from "./subscribe.js";
13
+ export * from "./track.js";
11
14
  export * from "./version.js";
12
15
  //# sourceMappingURL=index.js.map
package/lite/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/lite/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,cAAc,CAAC","sourcesContent":["export * from \"./announce.ts\";\nexport * from \"./connection.ts\";\nexport * from \"./fetch.ts\";\nexport * from \"./goaway.ts\";\nexport * from \"./group.ts\";\nexport * from \"./probe.ts\";\nexport * from \"./session.ts\";\nexport * from \"./stream.ts\";\nexport * from \"./subscribe.ts\";\nexport * from \"./version.ts\";\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/lite/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC","sourcesContent":["export * from \"./announce.ts\";\nexport * from \"./connection.ts\";\nexport * from \"./datagram.ts\";\nexport * from \"./fetch.ts\";\nexport * from \"./goaway.ts\";\nexport * from \"./group.ts\";\nexport * from \"./probe.ts\";\nexport * from \"./session.ts\";\nexport * from \"./setup.ts\";\nexport * from \"./stream.ts\";\nexport * from \"./subscribe.ts\";\nexport * from \"./track.ts\";\nexport * from \"./version.ts\";\n"]}
@@ -1,10 +1,11 @@
1
- import type { Broadcast } from "../broadcast";
1
+ import type * as broadcast from "../broadcast";
2
2
  import * as Path from "../path";
3
3
  import { type Stream } from "../stream";
4
- import { type AnnounceInterest } from "./announce";
4
+ import { type AnnounceRequest } from "./announce";
5
+ import type { Fetch } from "./fetch";
5
6
  import type { Origin } from "./origin";
6
7
  import { type Subscribe } from "./subscribe";
7
- import type { Track as TrackMessage } from "./track";
8
+ import { type Track as TrackMessage } from "./track";
8
9
  import { Version } from "./version";
9
10
  /**
10
11
  * Handles publishing broadcasts and managing their lifecycle.
@@ -28,7 +29,7 @@ export declare class Publisher {
28
29
  * Publishes a broadcast with any associated tracks.
29
30
  * @param name - The broadcast to publish
30
31
  */
31
- publish(path: Path.Valid, broadcast: Broadcast): void;
32
+ publish(path: Path.Valid, broadcast: broadcast.Producer): void;
32
33
  /**
33
34
  * Handles an announce interest message.
34
35
  * @param msg - The announce interest message
@@ -36,7 +37,7 @@ export declare class Publisher {
36
37
  *
37
38
  * @internal
38
39
  */
39
- runAnnounce(msg: AnnounceInterest, stream: Stream): Promise<void>;
40
+ runAnnounce(msg: AnnounceRequest, stream: Stream): Promise<void>;
40
41
  /**
41
42
  * Handles a subscribe message.
42
43
  * @param msg - The subscribe message
@@ -46,7 +47,13 @@ export declare class Publisher {
46
47
  */
47
48
  runSubscribe(msg: Subscribe, stream: Stream): Promise<void>;
48
49
  /**
49
- * Serves a Track stream (moq-lite-05+): replies with the track's immutable TRACK_INFO.
50
+ * Handles a FETCH stream by serving one group as bare frame records (lite-05+).
51
+ *
52
+ * @internal
53
+ */
54
+ runFetch(msg: Fetch, stream: Stream): Promise<void>;
55
+ /**
56
+ * Answers a TRACK stream (0x6) with a single TRACK_INFO, then FINs.
50
57
  *
51
58
  * @internal
52
59
  */
@@ -1 +1 @@
1
- {"version":3,"file":"publisher.d.ts","sourceRoot":"","sources":["../../src/lite/publisher.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAEjD,OAAO,KAAK,IAAI,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,KAAK,MAAM,EAAU,MAAM,cAAc,CAAC;AAGnD,OAAO,EAA0B,KAAK,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAE9E,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAE1C,OAAO,EAA2B,KAAK,SAAS,EAA8C,MAAM,gBAAgB,CAAC;AACrH,OAAO,KAAK,EAAE,KAAK,IAAI,YAAY,EAAE,MAAM,YAAY,CAAC;AAExD,OAAO,EAAkB,OAAO,EAAE,MAAM,cAAc,CAAC;AAMvD;;;;GAIG;AACH,qBAAa,SAAS;;IAErB,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAM1B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAQxB;;;;;;;OAOG;IACH,YAAY,IAAI,EAAE,YAAY,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAI/D;IAED;;;OAGG;IACH,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,KAAK,EAAE,SAAS,EAAE,SAAS,QAY7C;IAED;;;;;;OAMG;IACG,WAAW,CAAC,GAAG,EAAE,gBAAgB,EAAE,MAAM,EAAE,MAAM,iBA0EtD;IAED;;;;;;OAMG;IACG,YAAY,CAAC,GAAG,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,iBA6ChD;IAED;;;;OAIG;IACG,YAAY,CAAC,GAAG,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,iBAUnD;IAmGD;;;;;OAKG;IACG,QAAQ,CAAC,MAAM,EAAE,MAAM,iBAmD5B;IAED,KAAK,SAOJ;CACD"}
1
+ {"version":3,"file":"publisher.d.ts","sourceRoot":"","sources":["../../src/lite/publisher.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,KAAK,SAAS,MAAM,iBAAiB,CAAC;AAGlD,OAAO,KAAK,IAAI,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,KAAK,MAAM,EAAU,MAAM,cAAc,CAAC;AAGnD,OAAO,EAA4B,KAAK,eAAe,EAA2B,MAAM,eAAe,CAAC;AAGxG,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAExC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAE1C,OAAO,EAEN,KAAK,SAAS,EAKd,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAiC,KAAK,KAAK,IAAI,YAAY,EAAE,MAAM,YAAY,CAAC;AACvF,OAAO,EAA8C,OAAO,EAAE,MAAM,cAAc,CAAC;AAyBnF;;;;GAIG;AACH,qBAAa,SAAS;;IAErB,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAM1B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAoBxB;;;;;;;OAOG;IACH,YAAY,IAAI,EAAE,YAAY,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAU/D;IAED;;;OAGG;IACH,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,KAAK,EAAE,SAAS,EAAE,SAAS,CAAC,QAAQ,QAYtD;IAED;;;;;;OAMG;IACG,WAAW,CAAC,GAAG,EAAE,eAAe,EAAE,MAAM,EAAE,MAAM,iBA2GrD;IAED;;;;;;OAMG;IACG,YAAY,CAAC,GAAG,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,iBAwEhD;IAED;;;;OAIG;IACG,QAAQ,CAAC,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,iBA8BxC;IAuDD;;;;OAIG;IACG,YAAY,CAAC,GAAG,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,iBAUnD;IAyID;;;;;OAKG;IACG,QAAQ,CAAC,MAAM,EAAE,MAAM,iBAmD5B;IAED,KAAK,SAWJ;CACD"}
package/lite/publisher.js CHANGED
@@ -1,17 +1,37 @@
1
1
  /* @ts-self-types="./publisher.d.ts" */
2
2
  import { Signal } from "@moq/signals";
3
+ import { error, reason } from "../error.js";
3
4
  import * as Path from "../path.js";
4
5
  import { Writer } from "../stream.js";
5
- import { error } from "../util/error.js";
6
- import { Announce, AnnounceInit } from "./announce.js";
6
+ import { Timescale } from "../time.js";
7
+ import { AnnounceInit, AnnounceOk, encodeAnnounceBroadcast } from "./announce.js";
8
+ import { Datagram as DatagramMessage } from "./datagram.js";
9
+ import * as DatagramStream from "./datagram_stream.js";
7
10
  import { Group as GroupMessage } from "./group.js";
8
11
  import { Probe } from "./probe.js";
9
- import { encodeSubscribeResponse, SubscribeEnd, SubscribeOk, SubscribeUpdate } from "./subscribe.js";
10
- import { TrackInfo } from "./track.js";
11
- import { hasTrackStream, Version } from "./version.js";
12
+ import { encodeSubscribeResponse, SubscribeEnd, SubscribeOk, SubscribeStart, SubscribeUpdate, } from "./subscribe.js";
13
+ import { TrackInfo as TrackInfoMessage } from "./track.js";
14
+ import { hasAnnounceId, hasAnnounceOk, hasDatagrams, Version } from "./version.js";
12
15
  const PROBE_INTERVAL = 100; // ms
13
16
  const PROBE_MAX_AGE = 10_000; // ms
14
17
  const PROBE_MAX_DELTA = 0.25;
18
+ /** Map a signed delta to an unsigned zigzag varint value (mirrors Rust `VarInt::from_zigzag`). */
19
+ function zigzag(delta) {
20
+ return delta >= 0n ? delta << 1n : (-delta << 1n) - 1n;
21
+ }
22
+ // The TRACK stream, implicit SUBSCRIBE acceptance, and SUBSCRIBE_START/END are
23
+ // all lite-05+.
24
+ function supportsTrackStream(version) {
25
+ switch (version) {
26
+ case Version.DRAFT_01:
27
+ case Version.DRAFT_02:
28
+ case Version.DRAFT_03:
29
+ case Version.DRAFT_04:
30
+ return false;
31
+ default:
32
+ return true;
33
+ }
34
+ }
15
35
  /**
16
36
  * Handles publishing broadcasts and managing their lifecycle.
17
37
  *
@@ -26,9 +46,19 @@ export class Publisher {
26
46
  // own announcements.
27
47
  origin;
28
48
  #quic;
49
+ // The one writer for the outbound datagram stream (getWriter locks it), acquired once at
50
+ // construction when this version + transport carry datagrams, released in close(). Its
51
+ // presence is the gate: undefined means datagrams aren't served on this connection. All
52
+ // subscriptions share it, since a second getWriter on the same stream would throw.
53
+ #datagramWriter;
29
54
  // Our published broadcasts.
30
55
  // It's a signal so we can live update any announce streams.
31
56
  #broadcasts = new Signal(new Map());
57
+ // TRACK_INFO is immutable per track, so resolve it from the application once
58
+ // (via a throwaway subscribe whose info() resolves when the app calls accept)
59
+ // and reuse it for every later TRACK request of the same track. Keyed by
60
+ // `broadcast\0track`. A rejected lookup is evicted so a retry can re-probe.
61
+ #trackInfo = new Map();
32
62
  /**
33
63
  * Creates a new Publisher instance.
34
64
  * @param quic - The WebTransport session to use
@@ -41,6 +71,11 @@ export class Publisher {
41
71
  this.#quic = quic;
42
72
  this.version = version;
43
73
  this.origin = origin;
74
+ // Grab the datagram writer up front when the transport carries datagrams (no group
75
+ // fallback, so it stays undefined otherwise). One writer for all subscriptions.
76
+ if (hasDatagrams(version)) {
77
+ this.#datagramWriter = DatagramStream.datagramWriter(quic);
78
+ }
44
79
  }
45
80
  /**
46
81
  * Publishes a broadcast with any associated tracks.
@@ -53,7 +88,7 @@ export class Publisher {
53
88
  broadcasts.set(path, broadcast);
54
89
  });
55
90
  // Remove the broadcast from the lookup when it's closed.
56
- void broadcast.closed.finally(() => {
91
+ void broadcast.closed.then(() => {
57
92
  this.#broadcasts.mutate((broadcasts) => {
58
93
  broadcasts?.delete(path);
59
94
  });
@@ -80,6 +115,10 @@ export class Publisher {
80
115
  console.debug(`announce: broadcast=${name} active=true`);
81
116
  active.add(suffix);
82
117
  }
118
+ // Lite06+: announce ids. Every active we send implicitly assigns the next
119
+ // per-stream ordinal; ended references the id instead of repeating the path.
120
+ let nextAnnounceId = 0n;
121
+ const announceIds = new Map();
83
122
  switch (this.version) {
84
123
  case Version.DRAFT_01:
85
124
  case Version.DRAFT_02: {
@@ -87,13 +126,26 @@ export class Publisher {
87
126
  await init.encode(stream.writer, this.version);
88
127
  break;
89
128
  }
90
- default:
91
- // Draft03+: send individual Announce messages for initial state.
129
+ default: {
130
+ if (!hasAnnounceOk(this.version)) {
131
+ // Draft03/04: send individual Announce messages, stamping our origin as a hop.
132
+ for (const suffix of active) {
133
+ await encodeAnnounceBroadcast(stream.writer, { status: "active", suffix, hops: [this.origin] }, this.version);
134
+ }
135
+ break;
136
+ }
137
+ // Report our origin id once via AnnounceOk and the count of initial announces
138
+ // that follow; the subscriber stamps our origin onto each hop chain, so we omit it.
139
+ const ok = new AnnounceOk(this.origin, active.size);
140
+ await ok.encode(stream.writer, this.version);
92
141
  for (const suffix of active) {
93
- const wire = new Announce({ suffix, active: true, hops: [this.origin] });
94
- await wire.encode(stream.writer, this.version);
142
+ if (hasAnnounceId(this.version)) {
143
+ announceIds.set(suffix, nextAnnounceId++);
144
+ }
145
+ await encodeAnnounceBroadcast(stream.writer, { status: "active", suffix, hops: [] }, this.version);
95
146
  }
96
147
  break;
148
+ }
97
149
  }
98
150
  // Wait for updates to the broadcasts.
99
151
  for (;;) {
@@ -116,18 +168,30 @@ export class Publisher {
116
168
  continue; // Not our prefix.
117
169
  newActive.add(suffix);
118
170
  }
119
- // Announce any new broadcasts.
171
+ // Announce any new broadcasts. Lite05+ reports our origin once via AnnounceOk, so
172
+ // the subscriber stamps it onto each hop chain; older versions stamp it here.
120
173
  for (const added of newActive.difference(active)) {
121
174
  console.debug(`announce: broadcast=${added} active=true`);
122
- const wire = new Announce({ suffix: added, active: true, hops: [this.origin] });
123
- await wire.encode(stream.writer, this.version);
175
+ const hops = hasAnnounceOk(this.version) ? [] : [this.origin];
176
+ if (hasAnnounceId(this.version)) {
177
+ announceIds.set(added, nextAnnounceId++);
178
+ }
179
+ await encodeAnnounceBroadcast(stream.writer, { status: "active", suffix: added, hops }, this.version);
124
180
  }
125
- // Announce any removed broadcasts.
126
- // Ended announces don't need hops — the peer matches on path only.
181
+ // Announce any removed broadcasts. Lite06+ retracts by announce id;
182
+ // older versions repeat the path (ended announces don't need hops).
127
183
  for (const removed of active.difference(newActive)) {
128
184
  console.debug(`announce: broadcast=${removed} active=false`);
129
- const wire = new Announce({ suffix: removed, active: false });
130
- await wire.encode(stream.writer, this.version);
185
+ if (hasAnnounceId(this.version)) {
186
+ const id = announceIds.get(removed);
187
+ announceIds.delete(removed);
188
+ if (id === undefined)
189
+ continue; // never announced
190
+ await encodeAnnounceBroadcast(stream.writer, { status: "endedId", id }, this.version);
191
+ }
192
+ else {
193
+ await encodeAnnounceBroadcast(stream.writer, { status: "ended", suffix: removed }, this.version);
194
+ }
131
195
  }
132
196
  // NOTE: This is kind of a hack that won't work with a rapid UNANNOUNCE/ANNOUNCE cycle.
133
197
  // However, our client doesn't do that anyway.
@@ -148,16 +212,38 @@ export class Publisher {
148
212
  stream.writer.reset(new Error("not found"));
149
213
  return;
150
214
  }
151
- const track = broadcast.subscribe(msg.track, msg.priority);
215
+ const track = broadcast.subscribe(msg.track, { priority: msg.priority });
216
+ // The best-effort datagram loop, started once serving begins. It parks when the
217
+ // track finishes (recvDatagram returns undefined), so #runTrack alone ends the
218
+ // subscription; awaited during teardown so it doesn't outlive the subscription.
219
+ let datagrams;
152
220
  try {
153
- // moq-lite-05+ trims SUBSCRIBE_OK to the resolved start group; the publisher
154
- // properties move to TRACK_INFO. Earlier versions echo the priority inline.
155
- const info = hasTrackStream(this.version)
156
- ? new SubscribeOk({ startGroup: msg.startGroup ?? 0 })
157
- : new SubscribeOk({ priority: msg.priority });
158
- await encodeSubscribeResponse(stream.writer, { ok: info }, this.version);
221
+ let timescale = Timescale.MILLI;
222
+ if (supportsTrackStream(this.version)) {
223
+ // Lite-05+ accepts implicitly: no SUBSCRIBE_OK (the immutable
224
+ // properties live in TRACK_INFO), and the resolved range arrives as
225
+ // SUBSCRIBE_START / SUBSCRIBE_END emitted from #runTrack.
226
+ //
227
+ // The timescale is an immutable property, so serving MUST use exactly
228
+ // what TRACK_INFO advertised. It comes from the producer's accept(), so
229
+ // they always agree. Awaiting info() also surfaces a rejected track
230
+ // (accept never called, track closed) as an error here, which resets the
231
+ // stream.
232
+ const info = await track.info();
233
+ timescale = info.timescale;
234
+ }
235
+ else {
236
+ // Older drafts acknowledge with SUBSCRIBE_OK and stream frames verbatim.
237
+ const ok = new SubscribeOk({ priority: msg.priority });
238
+ await encodeSubscribeResponse(stream.writer, { ok }, this.version);
239
+ }
159
240
  console.debug(`publish ok: broadcast=${msg.broadcast} track=${track.name}`);
160
- const serving = this.#runTrack(msg.id, msg.broadcast, track, stream.writer);
241
+ const serving = this.#runTrack(msg.id, msg.broadcast, track, stream.writer, timescale);
242
+ // Serve datagrams concurrently with groups whenever the transport carries them
243
+ // (the writer exists iff so). No group fallback: otherwise they simply aren't sent.
244
+ if (this.#datagramWriter) {
245
+ datagrams = this.#runDatagrams(msg.id, track, timescale);
246
+ }
161
247
  for (;;) {
162
248
  const decode = SubscribeUpdate.decodeMaybe(stream.reader, this.version);
163
249
  const result = await Promise.any([serving, decode]);
@@ -165,34 +251,55 @@ export class Publisher {
165
251
  break;
166
252
  if (result instanceof SubscribeUpdate) {
167
253
  console.debug(`subscribe update: broadcast=${msg.broadcast} track=${track.name} priority=${result.priority}`);
168
- track.updatePriority(result.priority);
254
+ track.update({ priority: result.priority });
169
255
  }
170
256
  }
171
257
  console.debug(`publish done: broadcast=${msg.broadcast} track=${track.name}`);
172
258
  stream.close();
173
259
  track.close();
260
+ // track.close ends the datagram loop; wait so it doesn't leak past teardown.
261
+ await datagrams;
174
262
  }
175
263
  catch (err) {
176
264
  const e = error(err);
177
- console.warn(`publish error: broadcast=${msg.broadcast} track=${track.name} error=${e.message}`);
265
+ console.warn(`publish error: broadcast=${msg.broadcast} track=${track.name} error=${reason(e)}`);
178
266
  track.close(e);
179
267
  stream.abort(e);
268
+ await datagrams;
180
269
  }
181
270
  }
182
271
  /**
183
- * Serves a Track stream (moq-lite-05+): replies with the track's immutable TRACK_INFO.
272
+ * Handles a FETCH stream by serving one group as bare frame records (lite-05+).
184
273
  *
185
274
  * @internal
186
275
  */
187
- async runTrackInfo(msg, stream) {
276
+ async runFetch(msg, stream) {
277
+ if (!supportsTrackStream(this.version)) {
278
+ stream.writer.reset(new Error("fetch requires moq-lite-05 or newer"));
279
+ return;
280
+ }
188
281
  const broadcast = this.#broadcasts.peek()?.get(msg.broadcast);
189
282
  if (!broadcast) {
190
- throw new Error("not found");
283
+ console.debug(`fetch unknown: broadcast=${msg.broadcast}`);
284
+ stream.writer.reset(new Error("not found"));
285
+ return;
286
+ }
287
+ let group;
288
+ try {
289
+ // The timescale is immutable, so serve exactly what TRACK_INFO advertised.
290
+ const info = await this.#resolveTrackInfo(msg.broadcast, msg.track);
291
+ group = await broadcast.track(msg.track).fetchGroup(msg.group, { priority: msg.priority });
292
+ await this.#runFetchGroup(group, stream.writer, Timescale(info.timescale));
293
+ console.debug(`fetch done: broadcast=${msg.broadcast} track=${msg.track} group=${msg.group}`);
294
+ stream.close();
295
+ group.close();
296
+ }
297
+ catch (err) {
298
+ const e = error(err);
299
+ console.warn(`fetch error: broadcast=${msg.broadcast} track=${msg.track} group=${msg.group} error=${reason(e)}`);
300
+ group?.close(e);
301
+ stream.abort(e);
191
302
  }
192
- // The publisher properties aren't tracked in the browser model yet; report
193
- // defaults with a millisecond timescale matching the wall-clock frame timestamps.
194
- const info = new TrackInfo({ timescale: 1000 });
195
- await info.encode(stream.writer, this.version);
196
303
  }
197
304
  /**
198
305
  * Runs a track and sends its data to the stream.
@@ -203,31 +310,32 @@ export class Publisher {
203
310
  *
204
311
  * @internal
205
312
  */
206
- async #runTrack(sub, broadcast, track, stream) {
313
+ async #runTrack(sub, broadcast, track, stream, timescale) {
314
+ // Lite-05+ resolves the range on the subscribe stream: SUBSCRIBE_START once the
315
+ // first group is known, SUBSCRIBE_END when the track finishes.
316
+ const emitRange = supportsTrackStream(this.version);
317
+ let startSent = false;
318
+ // The exclusive end of the delivered range. recvGroup is arrival-ordered rather than
319
+ // sequence-ordered, so this tracks the max and not the last group seen. 0 is already
320
+ // the encoding for a track that produced no groups.
321
+ let end = 0;
207
322
  try {
208
- let lastSequence;
209
- let trackEnded = false;
210
323
  for (;;) {
211
324
  const next = track.recvGroup();
212
- const res = await Promise.race([
213
- next.then((group) => ({ group })),
214
- stream.closed.then(() => "closed"),
215
- ]);
216
- if (res === "closed") {
325
+ const group = await Promise.race([next, stream.closed]);
326
+ if (!group) {
217
327
  next.then((group) => group?.close()).catch(() => { });
218
328
  break;
219
329
  }
220
- if (!res.group) {
221
- trackEnded = true;
222
- break;
330
+ if (emitRange && !startSent) {
331
+ startSent = true;
332
+ await encodeSubscribeResponse(stream, { start: new SubscribeStart(group.sequence) }, this.version);
223
333
  }
224
- lastSequence = res.group.sequence;
225
- void this.#runGroup(sub, res.group);
334
+ end = Math.max(end, group.sequence + 1);
335
+ void this.#runGroup(sub, group, timescale);
226
336
  }
227
- // moq-lite-05+: signal end-of-track before the subscribe stream FINs.
228
- if (trackEnded && hasTrackStream(this.version)) {
229
- const end = new SubscribeEnd({ group: lastSequence ?? 0 });
230
- await encodeSubscribeResponse(stream, { end }, this.version);
337
+ if (emitRange) {
338
+ await encodeSubscribeResponse(stream, { end: new SubscribeEnd(end) }, this.version);
231
339
  }
232
340
  console.debug(`publish close: broadcast=${broadcast} track=${track.name}`);
233
341
  track.close();
@@ -235,11 +343,94 @@ export class Publisher {
235
343
  }
236
344
  catch (err) {
237
345
  const e = error(err);
238
- console.warn(`publish error: broadcast=${broadcast} track=${track.name} error=${e.message}`);
346
+ console.warn(`publish error: broadcast=${broadcast} track=${track.name} error=${reason(e)}`);
239
347
  track.close(e);
240
348
  stream.reset(e);
241
349
  }
242
350
  }
351
+ /**
352
+ * Answers a TRACK stream (0x6) with a single TRACK_INFO, then FINs.
353
+ *
354
+ * @internal
355
+ */
356
+ async runTrackInfo(msg, stream) {
357
+ try {
358
+ const info = await this.#resolveTrackInfo(msg.broadcast, msg.track);
359
+ await info.encode(stream.writer, this.version);
360
+ console.debug(`track info: broadcast=${msg.broadcast} track=${msg.track}`);
361
+ stream.close();
362
+ }
363
+ catch (err) {
364
+ console.debug(`track unknown: broadcast=${msg.broadcast} track=${msg.track}`);
365
+ stream.writer.reset(error(err));
366
+ }
367
+ }
368
+ // Resolve (and cache) a track's immutable TRACK_INFO by asking the application.
369
+ // `broadcast.track(name).info()` triggers a TrackRequest the app answers with
370
+ // accept(TrackInfo); only the immutable properties are needed (not the groups).
371
+ // Cached because they're fixed for the track's lifetime. Rejects if the broadcast
372
+ // or track is unavailable.
373
+ #resolveTrackInfo(broadcast, track) {
374
+ const key = `${broadcast}\0${track}`;
375
+ const cached = this.#trackInfo.get(key);
376
+ if (cached)
377
+ return cached;
378
+ const pending = (async () => {
379
+ const published = this.#broadcasts.peek()?.get(broadcast);
380
+ if (!published)
381
+ throw new Error("not found");
382
+ const info = await published.track(track).info();
383
+ return new TrackInfoMessage({
384
+ priority: info.priority,
385
+ ordered: info.ordered,
386
+ // Publisher Max Latency: the publisher's retention bound, advertised so
387
+ // relays re-serve with the same window.
388
+ latencyMax: info.latencyMax,
389
+ // Lite05 mandates per-frame timestamps. Advertise the track's timescale;
390
+ // `#runGroup` emits each frame converted to it.
391
+ timescale: info.timescale,
392
+ });
393
+ })();
394
+ // Don't poison the cache on failure: a later request may succeed.
395
+ pending.catch(() => this.#trackInfo.delete(key));
396
+ this.#trackInfo.set(key, pending);
397
+ return pending;
398
+ }
399
+ /**
400
+ * Forwards a track's datagrams best-effort over QUIC datagrams (lite-05 §6.4), parallel to
401
+ * its groups. Each datagram is dropped (there is no group fallback) if the encoded body
402
+ * doesn't fit the transport's datagram limit or the send fails. Returns once the track
403
+ * finishes; a failure never tears down the subscription.
404
+ *
405
+ * @internal
406
+ */
407
+ async #runDatagrams(sub, track, timescale) {
408
+ const writer = this.#datagramWriter;
409
+ if (!writer)
410
+ return; // Only reached with a writer (see the #datagramWriter gate).
411
+ const maxSize = DatagramStream.maxDatagramSize(this.#quic);
412
+ try {
413
+ for (;;) {
414
+ const datagram = await track.recvDatagram();
415
+ if (!datagram)
416
+ return; // Track finished; #runTrack tears the subscription down.
417
+ // Convert the timestamp to the track's advertised timescale, matching #runGroup.
418
+ const ts = Math.round(datagram.timestamp.as(timescale));
419
+ const body = new DatagramMessage(sub, datagram.sequence, ts, datagram.payload).encode();
420
+ // No group fallback: drop anything that doesn't fit a single datagram.
421
+ if (body.byteLength > maxSize) {
422
+ console.debug(`dropping oversize datagram: sub=${sub} size=${body.byteLength} max=${maxSize}`);
423
+ continue;
424
+ }
425
+ await writer.ready;
426
+ await writer.write(body);
427
+ }
428
+ }
429
+ catch (err) {
430
+ // Best-effort: a datagram send failure stops sending but never fails the subscription.
431
+ console.debug(`datagram send stopped: sub=${sub} error=${reason(err)}`);
432
+ }
433
+ }
243
434
  /**
244
435
  * Runs a group and sends its frames to the stream.
245
436
  * @param sub - The subscription ID
@@ -247,28 +438,44 @@ export class Publisher {
247
438
  *
248
439
  * @internal
249
440
  */
250
- async #runGroup(sub, group) {
441
+ // Serialize a fetched group's frames onto the FETCH stream as bare records: each a
442
+ // zigzag-delta timestamp (at the track's advertised timescale) followed by size + bytes.
443
+ async #runFetchGroup(group, stream, timescale) {
444
+ let prevTs = 0n;
445
+ for (;;) {
446
+ const frame = await Promise.race([group.readFrame(), stream.closed]);
447
+ if (!frame)
448
+ break;
449
+ const ts = BigInt(Math.round(frame.timestamp.as(timescale)));
450
+ await stream.u62(zigzag(ts - prevTs));
451
+ prevTs = ts;
452
+ await stream.u53(frame.payload.byteLength);
453
+ await stream.write(frame.payload);
454
+ }
455
+ }
456
+ async #runGroup(sub, group, timescale) {
251
457
  const msg = new GroupMessage(sub, group.sequence);
252
458
  try {
253
459
  const stream = await Writer.open(this.#quic);
254
- await stream.u8(0); // stream type
460
+ await stream.u53(0); // stream type
255
461
  await msg.encode(stream);
462
+ // Lite05+ prefixes every frame with a zigzag-delta timestamp at the track's
463
+ // advertised timescale; older drafts omit it.
464
+ const timestamps = supportsTrackStream(this.version);
465
+ let prevTs = 0n;
256
466
  try {
257
- // moq-lite-05+ stamps each frame with a wall-clock millisecond timestamp, sent
258
- // as a zigzag delta from the previous frame (the first frame is a delta from 0).
259
- let prevTimestamp = 0n;
260
467
  for (;;) {
261
468
  const frame = await Promise.race([group.readFrame(), stream.closed]);
262
469
  if (!frame)
263
470
  break;
264
- if (hasTrackStream(this.version)) {
265
- const now = BigInt(Date.now());
266
- const delta = now - prevTimestamp;
267
- prevTimestamp = now;
268
- await stream.u62((delta << 1n) ^ (delta >> 63n));
471
+ if (timestamps) {
472
+ // Convert each frame to the track's advertised timescale.
473
+ const ts = BigInt(Math.round(frame.timestamp.as(timescale)));
474
+ await stream.u62(zigzag(ts - prevTs));
475
+ prevTs = ts;
269
476
  }
270
- await stream.u53(frame.byteLength);
271
- await stream.write(frame);
477
+ await stream.u53(frame.payload.byteLength);
478
+ await stream.write(frame.payload);
272
479
  }
273
480
  stream.close();
274
481
  group.close();
@@ -345,6 +552,9 @@ export class Publisher {
345
552
  }
346
553
  return undefined;
347
554
  });
555
+ // Release the datagram writer's lock so the stream can be torn down.
556
+ this.#datagramWriter?.releaseLock();
557
+ this.#datagramWriter = undefined;
348
558
  }
349
559
  }
350
560
  //# sourceMappingURL=publisher.js.map