@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
@@ -1 +1 @@
1
- {"version":3,"file":"publisher.js","sourceRoot":"","sources":["../../src/lite/publisher.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,MAAM,EAAE,MAAM,cAAc,CAAC;AAGpD,OAAO,KAAK,IAAI,MAAM,YAAY,CAAC;AACnC,OAAO,EAAe,MAAM,EAAE,MAAM,cAAc,CAAC;AAEnD,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACzC,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAyB,MAAM,eAAe,CAAC;AAC9E,OAAO,EAAE,KAAK,IAAI,YAAY,EAAE,MAAM,YAAY,CAAC;AAEnD,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,uBAAuB,EAAkB,YAAY,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAErH,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACvC,OAAO,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAEvD,MAAM,cAAc,GAAG,GAAG,CAAC,CAAC,KAAK;AACjC,MAAM,aAAa,GAAG,MAAM,CAAC,CAAC,KAAK;AACnC,MAAM,eAAe,GAAG,IAAI,CAAC;AAE7B;;;;GAIG;AACH,MAAM,OAAO,SAAS;IACrB,iCAAiC;IACxB,OAAO,CAAU;IAE1B,wEAAwE;IACxE,uEAAuE;IACvE,wEAAwE;IACxE,qBAAqB;IACZ,MAAM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KAAK,GAAG,aAAa,GAAG,cAAc,CAAC;oBAC7C,MAAM,SAAS,GAAG,CAAC,eAAe,GAAG,CAAC,aAAa,GAAG,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC;oBAClE,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,GAAG,eAAe,CAAC,GAAG,eAAe,CAAC;oBACrE,UAAU,GAAG,MAAM,IAAI,SAAS,CAAC;gBAClC,CAAC;gBAED,IAAI,UAAU,EAAE,CAAC;oBAChB,MAAM,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;oBAC7D,eAAe,GAAG,OAAO,CAAC;oBAC1B,YAAY,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC;gBAClC,CAAC;YACF,CAAC;QACF,CAAC;QAAC,OAAO,GAAY,EAAE,CAAC;YACvB,OAAO,CAAC,IAAI,CAAC,oBAAoB,EAAE,GAAG,CAAC,CAAC;YACxC,MAAM,CAAC,KAAK,EAAE,CAAC;QAChB,CAAC;IACF,CAAC;IAED,KAAK;QACJ,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,UAAU,EAAE,EAAE;YACtC,KAAK,MAAM,SAAS,IAAI,UAAU,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE,CAAC;gBACpD,SAAS,CAAC,KAAK,EAAE,CAAC;YACnB,CAAC;YACD,OAAO,SAAS,CAAC;QAClB,CAAC,CAAC,CAAC;IACJ,CAAC;CACD","sourcesContent":["import { type Dispose, Signal } from \"@moq/signals\";\nimport type { Broadcast } from \"../broadcast.ts\";\nimport type { Group } from \"../group.ts\";\nimport * as Path from \"../path.ts\";\nimport { type Stream, Writer } from \"../stream.ts\";\nimport type { Track } from \"../track.ts\";\nimport { error } from \"../util/error.ts\";\nimport { Announce, AnnounceInit, type AnnounceInterest } from \"./announce.ts\";\nimport { Group as GroupMessage } from \"./group.ts\";\nimport type { Origin } from \"./origin.ts\";\nimport { Probe } from \"./probe.ts\";\nimport { encodeSubscribeResponse, type Subscribe, SubscribeEnd, SubscribeOk, SubscribeUpdate } from \"./subscribe.ts\";\nimport type { Track as TrackMessage } from \"./track.ts\";\nimport { TrackInfo } from \"./track.ts\";\nimport { hasTrackStream, Version } from \"./version.ts\";\n\nconst PROBE_INTERVAL = 100; // ms\nconst PROBE_MAX_AGE = 10_000; // ms\nconst PROBE_MAX_DELTA = 0.25;\n\n/**\n * Handles publishing broadcasts and managing their lifecycle.\n *\n * @internal\n */\nexport class Publisher {\n\t// The version of the connection.\n\treadonly version: Version;\n\n\t// Per-connection origin appended to outbound Announce hops, so the peer\n\t// can detect loops and prefer shorter paths. Created by Connection and\n\t// shared with Subscriber, which can optionally use it to filter out its\n\t// own announcements.\n\treadonly origin: Origin;\n\n\t#quic: WebTransport;\n\n\t// Our published broadcasts.\n\t// It's a signal so we can live update any announce streams.\n\t#broadcasts = new Signal<Map<Path.Valid, Broadcast> | undefined>(new Map());\n\n\t/**\n\t * Creates a new Publisher instance.\n\t * @param quic - The WebTransport session to use\n\t * @param version - Negotiated protocol version\n\t * @param origin - Origin id shared with the Subscriber\n\t *\n\t * @internal\n\t */\n\tconstructor(quic: WebTransport, version: Version, origin: Origin) {\n\t\tthis.#quic = quic;\n\t\tthis.version = version;\n\t\tthis.origin = origin;\n\t}\n\n\t/**\n\t * Publishes a broadcast with any associated tracks.\n\t * @param name - The broadcast to publish\n\t */\n\tpublish(path: Path.Valid, broadcast: Broadcast) {\n\t\tthis.#broadcasts.mutate((broadcasts) => {\n\t\t\tif (!broadcasts) throw new Error(\"closed\");\n\t\t\tbroadcasts.set(path, broadcast);\n\t\t});\n\n\t\t// Remove the broadcast from the lookup when it's closed.\n\t\tvoid broadcast.closed.finally(() => {\n\t\t\tthis.#broadcasts.mutate((broadcasts) => {\n\t\t\t\tbroadcasts?.delete(path);\n\t\t\t});\n\t\t});\n\t}\n\n\t/**\n\t * Handles an announce interest message.\n\t * @param msg - The announce interest message\n\t * @param stream - The stream to write announcements to\n\t *\n\t * @internal\n\t */\n\tasync runAnnounce(msg: AnnounceInterest, stream: Stream) {\n\t\tconsole.debug(`announce: prefix=${msg.prefix}`);\n\n\t\t// Send initial announcements\n\t\tlet active = new Set<Path.Valid>();\n\n\t\tconst broadcasts = this.#broadcasts.peek();\n\t\tif (!broadcasts) return; // closed\n\n\t\tfor (const name of broadcasts.keys()) {\n\t\t\tconst suffix = Path.stripPrefix(msg.prefix, name);\n\t\t\tif (suffix === null) continue;\n\t\t\tconsole.debug(`announce: broadcast=${name} active=true`);\n\t\t\tactive.add(suffix);\n\t\t}\n\n\t\tswitch (this.version) {\n\t\t\tcase Version.DRAFT_01:\n\t\t\tcase Version.DRAFT_02: {\n\t\t\t\tconst init = new AnnounceInit([...active]);\n\t\t\t\tawait init.encode(stream.writer, this.version);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tdefault:\n\t\t\t\t// Draft03+: send individual Announce messages for initial state.\n\t\t\t\tfor (const suffix of active) {\n\t\t\t\t\tconst wire = new Announce({ suffix, active: true, hops: [this.origin] });\n\t\t\t\t\tawait wire.encode(stream.writer, this.version);\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t}\n\n\t\t// Wait for updates to the broadcasts.\n\t\tfor (;;) {\n\t\t\t// TODO Make a better helper within Signals.\n\t\t\tlet dispose!: Dispose;\n\t\t\tconst changed = new Promise<Map<Path.Valid, Broadcast> | undefined>((resolve) => {\n\t\t\t\tdispose = this.#broadcasts.changed(resolve);\n\t\t\t});\n\n\t\t\t// Wait until the map of broadcasts changes.\n\t\t\tconst broadcasts = await Promise.race([changed, stream.reader.closed]);\n\t\t\tdispose();\n\t\t\tif (!broadcasts) break;\n\n\t\t\t// Create a new set of active broadcasts.\n\t\t\t// This is SLOW, but it's not worth optimizing because we often have just 1 broadcast anyway.\n\t\t\tconst newActive = new Set<Path.Valid>();\n\t\t\tfor (const name of broadcasts.keys()) {\n\t\t\t\tconst suffix = Path.stripPrefix(msg.prefix, name);\n\t\t\t\tif (suffix === null) continue; // Not our prefix.\n\t\t\t\tnewActive.add(suffix);\n\t\t\t}\n\n\t\t\t// Announce any new broadcasts.\n\t\t\tfor (const added of newActive.difference(active)) {\n\t\t\t\tconsole.debug(`announce: broadcast=${added} active=true`);\n\t\t\t\tconst wire = new Announce({ suffix: added, active: true, hops: [this.origin] });\n\t\t\t\tawait wire.encode(stream.writer, this.version);\n\t\t\t}\n\n\t\t\t// Announce any removed broadcasts.\n\t\t\t// Ended announces don't need hops — the peer matches on path only.\n\t\t\tfor (const removed of active.difference(newActive)) {\n\t\t\t\tconsole.debug(`announce: broadcast=${removed} active=false`);\n\t\t\t\tconst wire = new Announce({ suffix: removed, active: false });\n\t\t\t\tawait wire.encode(stream.writer, this.version);\n\t\t\t}\n\n\t\t\t// NOTE: This is kind of a hack that won't work with a rapid UNANNOUNCE/ANNOUNCE cycle.\n\t\t\t// However, our client doesn't do that anyway.\n\n\t\t\tactive = newActive;\n\t\t}\n\t}\n\n\t/**\n\t * Handles a subscribe message.\n\t * @param msg - The subscribe message\n\t * @param stream - The stream to write track data to\n\t *\n\t * @internal\n\t */\n\tasync runSubscribe(msg: Subscribe, stream: Stream) {\n\t\tconst broadcast = this.#broadcasts.peek()?.get(msg.broadcast);\n\t\tif (!broadcast) {\n\t\t\tconsole.debug(`publish unknown: broadcast=${msg.broadcast}`);\n\t\t\tstream.writer.reset(new Error(\"not found\"));\n\t\t\treturn;\n\t\t}\n\n\t\tconst track = broadcast.subscribe(msg.track, msg.priority);\n\n\t\ttry {\n\t\t\t// moq-lite-05+ trims SUBSCRIBE_OK to the resolved start group; the publisher\n\t\t\t// properties move to TRACK_INFO. Earlier versions echo the priority inline.\n\t\t\tconst info = hasTrackStream(this.version)\n\t\t\t\t? new SubscribeOk({ startGroup: msg.startGroup ?? 0 })\n\t\t\t\t: new SubscribeOk({ priority: msg.priority });\n\t\t\tawait encodeSubscribeResponse(stream.writer, { ok: info }, this.version);\n\n\t\t\tconsole.debug(`publish ok: broadcast=${msg.broadcast} track=${track.name}`);\n\n\t\t\tconst serving = this.#runTrack(msg.id, msg.broadcast, track, stream.writer);\n\n\t\t\tfor (;;) {\n\t\t\t\tconst decode = SubscribeUpdate.decodeMaybe(stream.reader, this.version);\n\n\t\t\t\tconst result = await Promise.any([serving, decode]);\n\t\t\t\tif (!result) break;\n\n\t\t\t\tif (result instanceof SubscribeUpdate) {\n\t\t\t\t\tconsole.debug(\n\t\t\t\t\t\t`subscribe update: broadcast=${msg.broadcast} track=${track.name} priority=${result.priority}`,\n\t\t\t\t\t);\n\t\t\t\t\ttrack.updatePriority(result.priority);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconsole.debug(`publish done: broadcast=${msg.broadcast} track=${track.name}`);\n\t\t\tstream.close();\n\t\t\ttrack.close();\n\t\t} catch (err: unknown) {\n\t\t\tconst e = error(err);\n\t\t\tconsole.warn(`publish error: broadcast=${msg.broadcast} track=${track.name} error=${e.message}`);\n\t\t\ttrack.close(e);\n\t\t\tstream.abort(e);\n\t\t}\n\t}\n\n\t/**\n\t * Serves a Track stream (moq-lite-05+): replies with the track's immutable TRACK_INFO.\n\t *\n\t * @internal\n\t */\n\tasync runTrackInfo(msg: TrackMessage, stream: Stream) {\n\t\tconst broadcast = this.#broadcasts.peek()?.get(msg.broadcast);\n\t\tif (!broadcast) {\n\t\t\tthrow new Error(\"not found\");\n\t\t}\n\n\t\t// The publisher properties aren't tracked in the browser model yet; report\n\t\t// defaults with a millisecond timescale matching the wall-clock frame timestamps.\n\t\tconst info = new TrackInfo({ timescale: 1000 });\n\t\tawait info.encode(stream.writer, this.version);\n\t}\n\n\t/**\n\t * Runs a track and sends its data to the stream.\n\t * @param sub - The subscription ID\n\t * @param broadcast - The broadcast name\n\t * @param track - The track to run\n\t * @param stream - The stream to write to\n\t *\n\t * @internal\n\t */\n\tasync #runTrack(sub: bigint, broadcast: Path.Valid, track: Track, stream: Writer) {\n\t\ttry {\n\t\t\tlet lastSequence: number | undefined;\n\t\t\tlet trackEnded = false;\n\n\t\t\tfor (;;) {\n\t\t\t\tconst next = track.recvGroup();\n\t\t\t\tconst res = await Promise.race([\n\t\t\t\t\tnext.then((group) => ({ group })),\n\t\t\t\t\tstream.closed.then(() => \"closed\" as const),\n\t\t\t\t]);\n\t\t\t\tif (res === \"closed\") {\n\t\t\t\t\tnext.then((group) => group?.close()).catch(() => {});\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tif (!res.group) {\n\t\t\t\t\ttrackEnded = true;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\tlastSequence = res.group.sequence;\n\t\t\t\tvoid this.#runGroup(sub, res.group);\n\t\t\t}\n\n\t\t\t// moq-lite-05+: signal end-of-track before the subscribe stream FINs.\n\t\t\tif (trackEnded && hasTrackStream(this.version)) {\n\t\t\t\tconst end = new SubscribeEnd({ group: lastSequence ?? 0 });\n\t\t\t\tawait encodeSubscribeResponse(stream, { end }, this.version);\n\t\t\t}\n\n\t\t\tconsole.debug(`publish close: broadcast=${broadcast} track=${track.name}`);\n\t\t\ttrack.close();\n\t\t\tstream.close();\n\t\t} catch (err: unknown) {\n\t\t\tconst e = error(err);\n\t\t\tconsole.warn(`publish error: broadcast=${broadcast} track=${track.name} error=${e.message}`);\n\t\t\ttrack.close(e);\n\t\t\tstream.reset(e);\n\t\t}\n\t}\n\n\t/**\n\t * Runs a group and sends its frames to the stream.\n\t * @param sub - The subscription ID\n\t * @param group - The group to run\n\t *\n\t * @internal\n\t */\n\tasync #runGroup(sub: bigint, group: Group) {\n\t\tconst msg = new GroupMessage(sub, group.sequence);\n\t\ttry {\n\t\t\tconst stream = await Writer.open(this.#quic);\n\t\t\tawait stream.u8(0); // stream type\n\t\t\tawait msg.encode(stream);\n\n\t\t\ttry {\n\t\t\t\t// moq-lite-05+ stamps each frame with a wall-clock millisecond timestamp, sent\n\t\t\t\t// as a zigzag delta from the previous frame (the first frame is a delta from 0).\n\t\t\t\tlet prevTimestamp = 0n;\n\n\t\t\t\tfor (;;) {\n\t\t\t\t\tconst frame = await Promise.race([group.readFrame(), stream.closed]);\n\t\t\t\t\tif (!frame) break;\n\n\t\t\t\t\tif (hasTrackStream(this.version)) {\n\t\t\t\t\t\tconst now = BigInt(Date.now());\n\t\t\t\t\t\tconst delta = now - prevTimestamp;\n\t\t\t\t\t\tprevTimestamp = now;\n\t\t\t\t\t\tawait stream.u62((delta << 1n) ^ (delta >> 63n));\n\t\t\t\t\t}\n\n\t\t\t\t\tawait stream.u53(frame.byteLength);\n\t\t\t\t\tawait stream.write(frame);\n\t\t\t\t}\n\n\t\t\t\tstream.close();\n\t\t\t\tgroup.close();\n\t\t\t} catch (err: unknown) {\n\t\t\t\tconst e = error(err);\n\t\t\t\tstream.reset(e);\n\t\t\t\tgroup.close(e);\n\t\t\t}\n\t\t} catch (err: unknown) {\n\t\t\tconst e = error(err);\n\t\t\tgroup.close(e);\n\t\t}\n\t}\n\n\t/**\n\t * Handles a probe stream by periodically reporting estimated bitrate.\n\t * @param stream - The probe bidi stream\n\t *\n\t * @internal\n\t */\n\tasync runProbe(stream: Stream) {\n\t\t// getStats is not yet in the TypeScript WebTransport type definitions.\n\t\tconst quic = this.#quic as unknown as {\n\t\t\tgetStats?: () => Promise<{ estimatedSendRate: number | null }>;\n\t\t};\n\t\tif (!quic.getStats) {\n\t\t\t// Best-effort: we can't supply bandwidth estimates, so close the\n\t\t\t// whole bidi (FIN + STOP_SENDING) to let the peer release its end.\n\t\t\tstream.close();\n\t\t\treturn;\n\t\t}\n\n\t\tlet lastSentBitrate: number | undefined;\n\t\tlet lastSentTime: number | undefined;\n\n\t\ttry {\n\t\t\tfor (;;) {\n\t\t\t\tconst timeout = new Promise<\"timeout\">((resolve) =>\n\t\t\t\t\tsetTimeout(() => resolve(\"timeout\"), PROBE_INTERVAL),\n\t\t\t\t);\n\t\t\t\tconst result = await Promise.race([timeout, stream.reader.closed]);\n\t\t\t\tif (result !== \"timeout\") break;\n\n\t\t\t\tconst stats = await quic.getStats();\n\t\t\t\tconst bitrate = stats.estimatedSendRate;\n\t\t\t\tif (bitrate == null) continue;\n\n\t\t\t\tlet shouldSend: boolean;\n\t\t\t\tif (lastSentBitrate === undefined || lastSentTime === undefined) {\n\t\t\t\t\tshouldSend = true;\n\t\t\t\t} else if (lastSentBitrate === 0) {\n\t\t\t\t\tshouldSend = bitrate > 0;\n\t\t\t\t} else {\n\t\t\t\t\tconst elapsed = performance.now() - lastSentTime;\n\t\t\t\t\tconst t = Math.max(PROBE_INTERVAL, Math.min(PROBE_MAX_AGE, elapsed));\n\t\t\t\t\tconst range = PROBE_MAX_AGE - PROBE_INTERVAL;\n\t\t\t\t\tconst threshold = (PROBE_MAX_DELTA * (PROBE_MAX_AGE - t)) / range;\n\t\t\t\t\tconst change = Math.abs(bitrate - lastSentBitrate) / lastSentBitrate;\n\t\t\t\t\tshouldSend = change >= threshold;\n\t\t\t\t}\n\n\t\t\t\tif (shouldSend) {\n\t\t\t\t\tawait new Probe(bitrate).encode(stream.writer, this.version);\n\t\t\t\t\tlastSentBitrate = bitrate;\n\t\t\t\t\tlastSentTime = performance.now();\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (err: unknown) {\n\t\t\tconsole.warn(\"probe stream error\", err);\n\t\t\tstream.close();\n\t\t}\n\t}\n\n\tclose() {\n\t\tthis.#broadcasts.update((broadcasts) => {\n\t\t\tfor (const broadcast of broadcasts?.values() ?? []) {\n\t\t\t\tbroadcast.close();\n\t\t\t}\n\t\t\treturn undefined;\n\t\t});\n\t}\n}\n"]}
1
+ {"version":3,"file":"publisher.js","sourceRoot":"","sources":["../../src/lite/publisher.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,MAAM,EAAE,MAAM,cAAc,CAAC;AAEpD,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAE5C,OAAO,KAAK,IAAI,MAAM,YAAY,CAAC;AACnC,OAAO,EA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,GAAG,CAAC,eAAe,GAAG,CAAC,aAAa,GAAG,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC;oBAClE,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,GAAG,eAAe,CAAC,GAAG,eAAe,CAAC;oBACrE,UAAU,GAAG,MAAM,IAAI,SAAS,CAAC;gBAClC,CAAC;gBAED,IAAI,UAAU,EAAE,CAAC;oBAChB,MAAM,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;oBAC7D,eAAe,GAAG,OAAO,CAAC;oBAC1B,YAAY,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC;gBAClC,CAAC;YACF,CAAC;QACF,CAAC;QAAC,OAAO,GAAY,EAAE,CAAC;YACvB,OAAO,CAAC,IAAI,CAAC,oBAAoB,EAAE,GAAG,CAAC,CAAC;YACxC,MAAM,CAAC,KAAK,EAAE,CAAC;QAChB,CAAC;IACF,CAAC;IAED,KAAK;QACJ,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,UAAU,EAAE,EAAE;YACtC,KAAK,MAAM,SAAS,IAAI,UAAU,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE,CAAC;gBACpD,SAAS,CAAC,KAAK,EAAE,CAAC;YACnB,CAAC;YACD,OAAO,SAAS,CAAC;QAClB,CAAC,CAAC,CAAC;QAEH,qEAAqE;QACrE,IAAI,CAAC,eAAe,EAAE,WAAW,EAAE,CAAC;QACpC,IAAI,CAAC,eAAe,GAAG,SAAS,CAAC;IAClC,CAAC;CACD","sourcesContent":["import { type Dispose, Signal } from \"@moq/signals\";\nimport type * as broadcast from \"../broadcast.ts\";\nimport { error, reason } from \"../error.ts\";\nimport type * as group from \"../group.ts\";\nimport * as Path from \"../path.ts\";\nimport { type Stream, Writer } from \"../stream.ts\";\nimport { Timescale } from \"../time.ts\";\nimport type * as track from \"../track.ts\";\nimport { AnnounceInit, AnnounceOk, type AnnounceRequest, encodeAnnounceBroadcast } from \"./announce.ts\";\nimport { Datagram as DatagramMessage } from \"./datagram.ts\";\nimport * as DatagramStream from \"./datagram_stream.ts\";\nimport type { Fetch } from \"./fetch.ts\";\nimport { Group as GroupMessage } from \"./group.ts\";\nimport type { Origin } from \"./origin.ts\";\nimport { Probe } from \"./probe.ts\";\nimport {\n\tencodeSubscribeResponse,\n\ttype Subscribe,\n\tSubscribeEnd,\n\tSubscribeOk,\n\tSubscribeStart,\n\tSubscribeUpdate,\n} from \"./subscribe.ts\";\nimport { TrackInfo as TrackInfoMessage, type Track as TrackMessage } from \"./track.ts\";\nimport { hasAnnounceId, hasAnnounceOk, hasDatagrams, Version } from \"./version.ts\";\n\nconst PROBE_INTERVAL = 100; // ms\nconst PROBE_MAX_AGE = 10_000; // ms\nconst PROBE_MAX_DELTA = 0.25;\n\n/** Map a signed delta to an unsigned zigzag varint value (mirrors Rust `VarInt::from_zigzag`). */\nfunction zigzag(delta: bigint): bigint {\n\treturn delta >= 0n ? delta << 1n : (-delta << 1n) - 1n;\n}\n\n// The TRACK stream, implicit SUBSCRIBE acceptance, and SUBSCRIBE_START/END are\n// all lite-05+.\nfunction supportsTrackStream(version: Version): boolean {\n\tswitch (version) {\n\t\tcase Version.DRAFT_01:\n\t\tcase Version.DRAFT_02:\n\t\tcase Version.DRAFT_03:\n\t\tcase Version.DRAFT_04:\n\t\t\treturn false;\n\t\tdefault:\n\t\t\treturn true;\n\t}\n}\n\n/**\n * Handles publishing broadcasts and managing their lifecycle.\n *\n * @internal\n */\nexport class Publisher {\n\t// The version of the connection.\n\treadonly version: Version;\n\n\t// Per-connection origin appended to outbound Announce hops, so the peer\n\t// can detect loops and prefer shorter paths. Created by Connection and\n\t// shared with Subscriber, which can optionally use it to filter out its\n\t// own announcements.\n\treadonly origin: Origin;\n\n\t#quic: WebTransport;\n\n\t// The one writer for the outbound datagram stream (getWriter locks it), acquired once at\n\t// construction when this version + transport carry datagrams, released in close(). Its\n\t// presence is the gate: undefined means datagrams aren't served on this connection. All\n\t// subscriptions share it, since a second getWriter on the same stream would throw.\n\t#datagramWriter?: WritableStreamDefaultWriter<Uint8Array>;\n\n\t// Our published broadcasts.\n\t// It's a signal so we can live update any announce streams.\n\t#broadcasts = new Signal<Map<Path.Valid, broadcast.Producer> | undefined>(new Map());\n\n\t// TRACK_INFO is immutable per track, so resolve it from the application once\n\t// (via a throwaway subscribe whose info() resolves when the app calls accept)\n\t// and reuse it for every later TRACK request of the same track. Keyed by\n\t// `broadcast\\0track`. A rejected lookup is evicted so a retry can re-probe.\n\t#trackInfo = new Map<string, Promise<TrackInfoMessage>>();\n\n\t/**\n\t * Creates a new Publisher instance.\n\t * @param quic - The WebTransport session to use\n\t * @param version - Negotiated protocol version\n\t * @param origin - Origin id shared with the Subscriber\n\t *\n\t * @internal\n\t */\n\tconstructor(quic: WebTransport, version: Version, origin: Origin) {\n\t\tthis.#quic = quic;\n\t\tthis.version = version;\n\t\tthis.origin = origin;\n\n\t\t// Grab the datagram writer up front when the transport carries datagrams (no group\n\t\t// fallback, so it stays undefined otherwise). One writer for all subscriptions.\n\t\tif (hasDatagrams(version)) {\n\t\t\tthis.#datagramWriter = DatagramStream.datagramWriter(quic);\n\t\t}\n\t}\n\n\t/**\n\t * Publishes a broadcast with any associated tracks.\n\t * @param name - The broadcast to publish\n\t */\n\tpublish(path: Path.Valid, broadcast: broadcast.Producer) {\n\t\tthis.#broadcasts.mutate((broadcasts) => {\n\t\t\tif (!broadcasts) throw new Error(\"closed\");\n\t\t\tbroadcasts.set(path, broadcast);\n\t\t});\n\n\t\t// Remove the broadcast from the lookup when it's closed.\n\t\tvoid broadcast.closed.then(() => {\n\t\t\tthis.#broadcasts.mutate((broadcasts) => {\n\t\t\t\tbroadcasts?.delete(path);\n\t\t\t});\n\t\t});\n\t}\n\n\t/**\n\t * Handles an announce interest message.\n\t * @param msg - The announce interest message\n\t * @param stream - The stream to write announcements to\n\t *\n\t * @internal\n\t */\n\tasync runAnnounce(msg: AnnounceRequest, stream: Stream) {\n\t\tconsole.debug(`announce: prefix=${msg.prefix}`);\n\n\t\t// Send initial announcements\n\t\tlet active = new Set<Path.Valid>();\n\n\t\tconst broadcasts = this.#broadcasts.peek();\n\t\tif (!broadcasts) return; // closed\n\n\t\tfor (const name of broadcasts.keys()) {\n\t\t\tconst suffix = Path.stripPrefix(msg.prefix, name);\n\t\t\tif (suffix === null) continue;\n\t\t\tconsole.debug(`announce: broadcast=${name} active=true`);\n\t\t\tactive.add(suffix);\n\t\t}\n\n\t\t// Lite06+: announce ids. Every active we send implicitly assigns the next\n\t\t// per-stream ordinal; ended references the id instead of repeating the path.\n\t\tlet nextAnnounceId = 0n;\n\t\tconst announceIds = new Map<Path.Valid, bigint>();\n\n\t\tswitch (this.version) {\n\t\t\tcase Version.DRAFT_01:\n\t\t\tcase Version.DRAFT_02: {\n\t\t\t\tconst init = new AnnounceInit([...active]);\n\t\t\t\tawait init.encode(stream.writer, this.version);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tdefault: {\n\t\t\t\tif (!hasAnnounceOk(this.version)) {\n\t\t\t\t\t// Draft03/04: send individual Announce messages, stamping our origin as a hop.\n\t\t\t\t\tfor (const suffix of active) {\n\t\t\t\t\t\tawait encodeAnnounceBroadcast(\n\t\t\t\t\t\t\tstream.writer,\n\t\t\t\t\t\t\t{ status: \"active\", suffix, hops: [this.origin] },\n\t\t\t\t\t\t\tthis.version,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\t// Report our origin id once via AnnounceOk and the count of initial announces\n\t\t\t\t// that follow; the subscriber stamps our origin onto each hop chain, so we omit it.\n\t\t\t\tconst ok = new AnnounceOk(this.origin, active.size);\n\t\t\t\tawait ok.encode(stream.writer, this.version);\n\t\t\t\tfor (const suffix of active) {\n\t\t\t\t\tif (hasAnnounceId(this.version)) {\n\t\t\t\t\t\tannounceIds.set(suffix, nextAnnounceId++);\n\t\t\t\t\t}\n\t\t\t\t\tawait encodeAnnounceBroadcast(stream.writer, { status: \"active\", suffix, hops: [] }, this.version);\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\t// Wait for updates to the broadcasts.\n\t\tfor (;;) {\n\t\t\t// TODO Make a better helper within Signals.\n\t\t\tlet dispose!: Dispose;\n\t\t\tconst changed = new Promise<Map<Path.Valid, broadcast.Producer> | undefined>((resolve) => {\n\t\t\t\tdispose = this.#broadcasts.changed(resolve);\n\t\t\t});\n\n\t\t\t// Wait until the map of broadcasts changes.\n\t\t\tconst broadcasts = await Promise.race([changed, stream.reader.closed]);\n\t\t\tdispose();\n\t\t\tif (!broadcasts) break;\n\n\t\t\t// Create a new set of active broadcasts.\n\t\t\t// This is SLOW, but it's not worth optimizing because we often have just 1 broadcast anyway.\n\t\t\tconst newActive = new Set<Path.Valid>();\n\t\t\tfor (const name of broadcasts.keys()) {\n\t\t\t\tconst suffix = Path.stripPrefix(msg.prefix, name);\n\t\t\t\tif (suffix === null) continue; // Not our prefix.\n\t\t\t\tnewActive.add(suffix);\n\t\t\t}\n\n\t\t\t// Announce any new broadcasts. Lite05+ reports our origin once via AnnounceOk, so\n\t\t\t// the subscriber stamps it onto each hop chain; older versions stamp it here.\n\t\t\tfor (const added of newActive.difference(active)) {\n\t\t\t\tconsole.debug(`announce: broadcast=${added} active=true`);\n\t\t\t\tconst hops = hasAnnounceOk(this.version) ? [] : [this.origin];\n\t\t\t\tif (hasAnnounceId(this.version)) {\n\t\t\t\t\tannounceIds.set(added, nextAnnounceId++);\n\t\t\t\t}\n\t\t\t\tawait encodeAnnounceBroadcast(stream.writer, { status: \"active\", suffix: added, hops }, this.version);\n\t\t\t}\n\n\t\t\t// Announce any removed broadcasts. Lite06+ retracts by announce id;\n\t\t\t// older versions repeat the path (ended announces don't need hops).\n\t\t\tfor (const removed of active.difference(newActive)) {\n\t\t\t\tconsole.debug(`announce: broadcast=${removed} active=false`);\n\t\t\t\tif (hasAnnounceId(this.version)) {\n\t\t\t\t\tconst id = announceIds.get(removed);\n\t\t\t\t\tannounceIds.delete(removed);\n\t\t\t\t\tif (id === undefined) continue; // never announced\n\t\t\t\t\tawait encodeAnnounceBroadcast(stream.writer, { status: \"endedId\", id }, this.version);\n\t\t\t\t} else {\n\t\t\t\t\tawait encodeAnnounceBroadcast(stream.writer, { status: \"ended\", suffix: removed }, this.version);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// NOTE: This is kind of a hack that won't work with a rapid UNANNOUNCE/ANNOUNCE cycle.\n\t\t\t// However, our client doesn't do that anyway.\n\n\t\t\tactive = newActive;\n\t\t}\n\t}\n\n\t/**\n\t * Handles a subscribe message.\n\t * @param msg - The subscribe message\n\t * @param stream - The stream to write track data to\n\t *\n\t * @internal\n\t */\n\tasync runSubscribe(msg: Subscribe, stream: Stream) {\n\t\tconst broadcast = this.#broadcasts.peek()?.get(msg.broadcast);\n\t\tif (!broadcast) {\n\t\t\tconsole.debug(`publish unknown: broadcast=${msg.broadcast}`);\n\t\t\tstream.writer.reset(new Error(\"not found\"));\n\t\t\treturn;\n\t\t}\n\n\t\tconst track = broadcast.subscribe(msg.track, { priority: msg.priority });\n\n\t\t// The best-effort datagram loop, started once serving begins. It parks when the\n\t\t// track finishes (recvDatagram returns undefined), so #runTrack alone ends the\n\t\t// subscription; awaited during teardown so it doesn't outlive the subscription.\n\t\tlet datagrams: Promise<void> | undefined;\n\n\t\ttry {\n\t\t\tlet timescale: Timescale = Timescale.MILLI;\n\n\t\t\tif (supportsTrackStream(this.version)) {\n\t\t\t\t// Lite-05+ accepts implicitly: no SUBSCRIBE_OK (the immutable\n\t\t\t\t// properties live in TRACK_INFO), and the resolved range arrives as\n\t\t\t\t// SUBSCRIBE_START / SUBSCRIBE_END emitted from #runTrack.\n\t\t\t\t//\n\t\t\t\t// The timescale is an immutable property, so serving MUST use exactly\n\t\t\t\t// what TRACK_INFO advertised. It comes from the producer's accept(), so\n\t\t\t\t// they always agree. Awaiting info() also surfaces a rejected track\n\t\t\t\t// (accept never called, track closed) as an error here, which resets the\n\t\t\t\t// stream.\n\t\t\t\tconst info = await track.info();\n\t\t\t\ttimescale = info.timescale;\n\t\t\t} else {\n\t\t\t\t// Older drafts acknowledge with SUBSCRIBE_OK and stream frames verbatim.\n\t\t\t\tconst ok = new SubscribeOk({ priority: msg.priority });\n\t\t\t\tawait encodeSubscribeResponse(stream.writer, { ok }, this.version);\n\t\t\t}\n\n\t\t\tconsole.debug(`publish ok: broadcast=${msg.broadcast} track=${track.name}`);\n\n\t\t\tconst serving = this.#runTrack(msg.id, msg.broadcast, track, stream.writer, timescale);\n\n\t\t\t// Serve datagrams concurrently with groups whenever the transport carries them\n\t\t\t// (the writer exists iff so). No group fallback: otherwise they simply aren't sent.\n\t\t\tif (this.#datagramWriter) {\n\t\t\t\tdatagrams = this.#runDatagrams(msg.id, track, timescale);\n\t\t\t}\n\n\t\t\tfor (;;) {\n\t\t\t\tconst decode = SubscribeUpdate.decodeMaybe(stream.reader, this.version);\n\n\t\t\t\tconst result = await Promise.any([serving, decode]);\n\t\t\t\tif (!result) break;\n\n\t\t\t\tif (result instanceof SubscribeUpdate) {\n\t\t\t\t\tconsole.debug(\n\t\t\t\t\t\t`subscribe update: broadcast=${msg.broadcast} track=${track.name} priority=${result.priority}`,\n\t\t\t\t\t);\n\t\t\t\t\ttrack.update({ priority: result.priority });\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconsole.debug(`publish done: broadcast=${msg.broadcast} track=${track.name}`);\n\t\t\tstream.close();\n\t\t\ttrack.close();\n\t\t\t// track.close ends the datagram loop; wait so it doesn't leak past teardown.\n\t\t\tawait datagrams;\n\t\t} catch (err: unknown) {\n\t\t\tconst e = error(err);\n\t\t\tconsole.warn(`publish error: broadcast=${msg.broadcast} track=${track.name} error=${reason(e)}`);\n\t\t\ttrack.close(e);\n\t\t\tstream.abort(e);\n\t\t\tawait datagrams;\n\t\t}\n\t}\n\n\t/**\n\t * Handles a FETCH stream by serving one group as bare frame records (lite-05+).\n\t *\n\t * @internal\n\t */\n\tasync runFetch(msg: Fetch, stream: Stream) {\n\t\tif (!supportsTrackStream(this.version)) {\n\t\t\tstream.writer.reset(new Error(\"fetch requires moq-lite-05 or newer\"));\n\t\t\treturn;\n\t\t}\n\n\t\tconst broadcast = this.#broadcasts.peek()?.get(msg.broadcast);\n\t\tif (!broadcast) {\n\t\t\tconsole.debug(`fetch unknown: broadcast=${msg.broadcast}`);\n\t\t\tstream.writer.reset(new Error(\"not found\"));\n\t\t\treturn;\n\t\t}\n\n\t\tlet group: group.Consumer | undefined;\n\t\ttry {\n\t\t\t// The timescale is immutable, so serve exactly what TRACK_INFO advertised.\n\t\t\tconst info = await this.#resolveTrackInfo(msg.broadcast, msg.track);\n\t\t\tgroup = await broadcast.track(msg.track).fetchGroup(msg.group, { priority: msg.priority });\n\t\t\tawait this.#runFetchGroup(group, stream.writer, Timescale(info.timescale));\n\t\t\tconsole.debug(`fetch done: broadcast=${msg.broadcast} track=${msg.track} group=${msg.group}`);\n\t\t\tstream.close();\n\t\t\tgroup.close();\n\t\t} catch (err: unknown) {\n\t\t\tconst e = error(err);\n\t\t\tconsole.warn(\n\t\t\t\t`fetch error: broadcast=${msg.broadcast} track=${msg.track} group=${msg.group} error=${reason(e)}`,\n\t\t\t);\n\t\t\tgroup?.close(e);\n\t\t\tstream.abort(e);\n\t\t}\n\t}\n\n\t/**\n\t * Runs a track and sends its data to the stream.\n\t * @param sub - The subscription ID\n\t * @param broadcast - The broadcast name\n\t * @param track - The track to run\n\t * @param stream - The stream to write to\n\t *\n\t * @internal\n\t */\n\tasync #runTrack(sub: bigint, broadcast: Path.Valid, track: track.Subscriber, stream: Writer, timescale: Timescale) {\n\t\t// Lite-05+ resolves the range on the subscribe stream: SUBSCRIBE_START once the\n\t\t// first group is known, SUBSCRIBE_END when the track finishes.\n\t\tconst emitRange = supportsTrackStream(this.version);\n\t\tlet startSent = false;\n\n\t\t// The exclusive end of the delivered range. recvGroup is arrival-ordered rather than\n\t\t// sequence-ordered, so this tracks the max and not the last group seen. 0 is already\n\t\t// the encoding for a track that produced no groups.\n\t\tlet end = 0;\n\n\t\ttry {\n\t\t\tfor (;;) {\n\t\t\t\tconst next = track.recvGroup();\n\t\t\t\tconst group = await Promise.race([next, stream.closed]);\n\t\t\t\tif (!group) {\n\t\t\t\t\tnext.then((group) => group?.close()).catch(() => {});\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\tif (emitRange && !startSent) {\n\t\t\t\t\tstartSent = true;\n\t\t\t\t\tawait encodeSubscribeResponse(stream, { start: new SubscribeStart(group.sequence) }, this.version);\n\t\t\t\t}\n\t\t\t\tend = Math.max(end, group.sequence + 1);\n\n\t\t\t\tvoid this.#runGroup(sub, group, timescale);\n\t\t\t}\n\n\t\t\tif (emitRange) {\n\t\t\t\tawait encodeSubscribeResponse(stream, { end: new SubscribeEnd(end) }, this.version);\n\t\t\t}\n\n\t\t\tconsole.debug(`publish close: broadcast=${broadcast} track=${track.name}`);\n\t\t\ttrack.close();\n\t\t\tstream.close();\n\t\t} catch (err: unknown) {\n\t\t\tconst e = error(err);\n\t\t\tconsole.warn(`publish error: broadcast=${broadcast} track=${track.name} error=${reason(e)}`);\n\t\t\ttrack.close(e);\n\t\t\tstream.reset(e);\n\t\t}\n\t}\n\n\t/**\n\t * Answers a TRACK stream (0x6) with a single TRACK_INFO, then FINs.\n\t *\n\t * @internal\n\t */\n\tasync runTrackInfo(msg: TrackMessage, stream: Stream) {\n\t\ttry {\n\t\t\tconst info = await this.#resolveTrackInfo(msg.broadcast, msg.track);\n\t\t\tawait info.encode(stream.writer, this.version);\n\t\t\tconsole.debug(`track info: broadcast=${msg.broadcast} track=${msg.track}`);\n\t\t\tstream.close();\n\t\t} catch (err) {\n\t\t\tconsole.debug(`track unknown: broadcast=${msg.broadcast} track=${msg.track}`);\n\t\t\tstream.writer.reset(error(err));\n\t\t}\n\t}\n\n\t// Resolve (and cache) a track's immutable TRACK_INFO by asking the application.\n\t// `broadcast.track(name).info()` triggers a TrackRequest the app answers with\n\t// accept(TrackInfo); only the immutable properties are needed (not the groups).\n\t// Cached because they're fixed for the track's lifetime. Rejects if the broadcast\n\t// or track is unavailable.\n\t#resolveTrackInfo(broadcast: Path.Valid, track: string): Promise<TrackInfoMessage> {\n\t\tconst key = `${broadcast}\\0${track}`;\n\t\tconst cached = this.#trackInfo.get(key);\n\t\tif (cached) return cached;\n\n\t\tconst pending = (async () => {\n\t\t\tconst published = this.#broadcasts.peek()?.get(broadcast);\n\t\t\tif (!published) throw new Error(\"not found\");\n\n\t\t\tconst info = await published.track(track).info();\n\t\t\treturn new TrackInfoMessage({\n\t\t\t\tpriority: info.priority,\n\t\t\t\tordered: info.ordered,\n\t\t\t\t// Publisher Max Latency: the publisher's retention bound, advertised so\n\t\t\t\t// relays re-serve with the same window.\n\t\t\t\tlatencyMax: info.latencyMax,\n\t\t\t\t// Lite05 mandates per-frame timestamps. Advertise the track's timescale;\n\t\t\t\t// `#runGroup` emits each frame converted to it.\n\t\t\t\ttimescale: info.timescale,\n\t\t\t});\n\t\t})();\n\n\t\t// Don't poison the cache on failure: a later request may succeed.\n\t\tpending.catch(() => this.#trackInfo.delete(key));\n\t\tthis.#trackInfo.set(key, pending);\n\t\treturn pending;\n\t}\n\n\t/**\n\t * Forwards a track's datagrams best-effort over QUIC datagrams (lite-05 §6.4), parallel to\n\t * its groups. Each datagram is dropped (there is no group fallback) if the encoded body\n\t * doesn't fit the transport's datagram limit or the send fails. Returns once the track\n\t * finishes; a failure never tears down the subscription.\n\t *\n\t * @internal\n\t */\n\tasync #runDatagrams(sub: bigint, track: track.Subscriber, timescale: Timescale) {\n\t\tconst writer = this.#datagramWriter;\n\t\tif (!writer) return; // Only reached with a writer (see the #datagramWriter gate).\n\t\tconst maxSize = DatagramStream.maxDatagramSize(this.#quic);\n\n\t\ttry {\n\t\t\tfor (;;) {\n\t\t\t\tconst datagram = await track.recvDatagram();\n\t\t\t\tif (!datagram) return; // Track finished; #runTrack tears the subscription down.\n\n\t\t\t\t// Convert the timestamp to the track's advertised timescale, matching #runGroup.\n\t\t\t\tconst ts = Math.round(datagram.timestamp.as(timescale));\n\t\t\t\tconst body = new DatagramMessage(sub, datagram.sequence, ts, datagram.payload).encode();\n\n\t\t\t\t// No group fallback: drop anything that doesn't fit a single datagram.\n\t\t\t\tif (body.byteLength > maxSize) {\n\t\t\t\t\tconsole.debug(`dropping oversize datagram: sub=${sub} size=${body.byteLength} max=${maxSize}`);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tawait writer.ready;\n\t\t\t\tawait writer.write(body);\n\t\t\t}\n\t\t} catch (err: unknown) {\n\t\t\t// Best-effort: a datagram send failure stops sending but never fails the subscription.\n\t\t\tconsole.debug(`datagram send stopped: sub=${sub} error=${reason(err)}`);\n\t\t}\n\t}\n\n\t/**\n\t * Runs a group and sends its frames to the stream.\n\t * @param sub - The subscription ID\n\t * @param group - The group to run\n\t *\n\t * @internal\n\t */\n\t// Serialize a fetched group's frames onto the FETCH stream as bare records: each a\n\t// zigzag-delta timestamp (at the track's advertised timescale) followed by size + bytes.\n\tasync #runFetchGroup(group: group.Consumer, stream: Writer, timescale: Timescale) {\n\t\tlet prevTs = 0n;\n\t\tfor (;;) {\n\t\t\tconst frame = await Promise.race([group.readFrame(), stream.closed]);\n\t\t\tif (!frame) break;\n\n\t\t\tconst ts = BigInt(Math.round(frame.timestamp.as(timescale)));\n\t\t\tawait stream.u62(zigzag(ts - prevTs));\n\t\t\tprevTs = ts;\n\n\t\t\tawait stream.u53(frame.payload.byteLength);\n\t\t\tawait stream.write(frame.payload);\n\t\t}\n\t}\n\n\tasync #runGroup(sub: bigint, group: group.Consumer, timescale: Timescale) {\n\t\tconst msg = new GroupMessage(sub, group.sequence);\n\t\ttry {\n\t\t\tconst stream = await Writer.open(this.#quic);\n\t\t\tawait stream.u53(0); // stream type\n\t\t\tawait msg.encode(stream);\n\n\t\t\t// Lite05+ prefixes every frame with a zigzag-delta timestamp at the track's\n\t\t\t// advertised timescale; older drafts omit it.\n\t\t\tconst timestamps = supportsTrackStream(this.version);\n\t\t\tlet prevTs = 0n;\n\n\t\t\ttry {\n\t\t\t\tfor (;;) {\n\t\t\t\t\tconst frame = await Promise.race([group.readFrame(), stream.closed]);\n\t\t\t\t\tif (!frame) break;\n\n\t\t\t\t\tif (timestamps) {\n\t\t\t\t\t\t// Convert each frame to the track's advertised timescale.\n\t\t\t\t\t\tconst ts = BigInt(Math.round(frame.timestamp.as(timescale)));\n\t\t\t\t\t\tawait stream.u62(zigzag(ts - prevTs));\n\t\t\t\t\t\tprevTs = ts;\n\t\t\t\t\t}\n\n\t\t\t\t\tawait stream.u53(frame.payload.byteLength);\n\t\t\t\t\tawait stream.write(frame.payload);\n\t\t\t\t}\n\n\t\t\t\tstream.close();\n\t\t\t\tgroup.close();\n\t\t\t} catch (err: unknown) {\n\t\t\t\tconst e = error(err);\n\t\t\t\tstream.reset(e);\n\t\t\t\tgroup.close(e);\n\t\t\t}\n\t\t} catch (err: unknown) {\n\t\t\tconst e = error(err);\n\t\t\tgroup.close(e);\n\t\t}\n\t}\n\n\t/**\n\t * Handles a probe stream by periodically reporting estimated bitrate.\n\t * @param stream - The probe bidi stream\n\t *\n\t * @internal\n\t */\n\tasync runProbe(stream: Stream) {\n\t\t// getStats is not yet in the TypeScript WebTransport type definitions.\n\t\tconst quic = this.#quic as unknown as {\n\t\t\tgetStats?: () => Promise<{ estimatedSendRate: number | null }>;\n\t\t};\n\t\tif (!quic.getStats) {\n\t\t\t// Best-effort: we can't supply bandwidth estimates, so close the\n\t\t\t// whole bidi (FIN + STOP_SENDING) to let the peer release its end.\n\t\t\tstream.close();\n\t\t\treturn;\n\t\t}\n\n\t\tlet lastSentBitrate: number | undefined;\n\t\tlet lastSentTime: number | undefined;\n\n\t\ttry {\n\t\t\tfor (;;) {\n\t\t\t\tconst timeout = new Promise<\"timeout\">((resolve) =>\n\t\t\t\t\tsetTimeout(() => resolve(\"timeout\"), PROBE_INTERVAL),\n\t\t\t\t);\n\t\t\t\tconst result = await Promise.race([timeout, stream.reader.closed]);\n\t\t\t\tif (result !== \"timeout\") break;\n\n\t\t\t\tconst stats = await quic.getStats();\n\t\t\t\tconst bitrate = stats.estimatedSendRate;\n\t\t\t\tif (bitrate == null) continue;\n\n\t\t\t\tlet shouldSend: boolean;\n\t\t\t\tif (lastSentBitrate === undefined || lastSentTime === undefined) {\n\t\t\t\t\tshouldSend = true;\n\t\t\t\t} else if (lastSentBitrate === 0) {\n\t\t\t\t\tshouldSend = bitrate > 0;\n\t\t\t\t} else {\n\t\t\t\t\tconst elapsed = performance.now() - lastSentTime;\n\t\t\t\t\tconst t = Math.max(PROBE_INTERVAL, Math.min(PROBE_MAX_AGE, elapsed));\n\t\t\t\t\tconst range = PROBE_MAX_AGE - PROBE_INTERVAL;\n\t\t\t\t\tconst threshold = (PROBE_MAX_DELTA * (PROBE_MAX_AGE - t)) / range;\n\t\t\t\t\tconst change = Math.abs(bitrate - lastSentBitrate) / lastSentBitrate;\n\t\t\t\t\tshouldSend = change >= threshold;\n\t\t\t\t}\n\n\t\t\t\tif (shouldSend) {\n\t\t\t\t\tawait new Probe(bitrate).encode(stream.writer, this.version);\n\t\t\t\t\tlastSentBitrate = bitrate;\n\t\t\t\t\tlastSentTime = performance.now();\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (err: unknown) {\n\t\t\tconsole.warn(\"probe stream error\", err);\n\t\t\tstream.close();\n\t\t}\n\t}\n\n\tclose() {\n\t\tthis.#broadcasts.update((broadcasts) => {\n\t\t\tfor (const broadcast of broadcasts?.values() ?? []) {\n\t\t\t\tbroadcast.close();\n\t\t\t}\n\t\t\treturn undefined;\n\t\t});\n\n\t\t// Release the datagram writer's lock so the stream can be torn down.\n\t\tthis.#datagramWriter?.releaseLock();\n\t\tthis.#datagramWriter = undefined;\n\t}\n}\n"]}
@@ -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","sourceRoot":"","sources":["../../src/lite/publisher.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AACxC,OAAO,EAAE,QAAQ,IAAI,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAChE,OAAO,EAAE,QAAQ,IAAI,aAAa,EAAE,MAAM,aAAa,CAAC;AACxD,OAAO,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAC;AACrD,OAAO,KAAK,IAAI,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AACtC,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,uBAAuB,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC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,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,EAExC;IA6BK,MAAM,CAAC,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAGvD;IAED,OAAa,MAAM,CAAC,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,KAAK,CAAC,CAG/D;CACD"}
1
+ {"version":3,"file":"setup.d.ts","sourceRoot":"","sources":["../../src/lite/setup.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAGnD,OAAO,EAAkB,KAAK,OAAO,EAAE,MAAM,cAAc,CAAC;AAY5D;;;;;;GAMG;AACH,eAAO,MAAM,UAAU;IACtB,wDAAwD;aACxD,IAAI,EAAE,CAAC;IACP,+EAA+E;aAC/E,MAAM,EAA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ertises 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
  }