@moq/net 0.3.5 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (210) hide show
  1. package/announce.d.ts +7 -0
  2. package/announce.d.ts.map +1 -0
  3. package/announce.js +8 -0
  4. package/announce.js.map +1 -0
  5. package/announced.d.ts +35 -91
  6. package/announced.d.ts.map +1 -1
  7. package/announced.js +21 -156
  8. package/announced.js.map +1 -1
  9. package/bandwidth.d.ts +163 -0
  10. package/bandwidth.d.ts.map +1 -0
  11. package/bandwidth.js +304 -0
  12. package/bandwidth.js.map +1 -0
  13. package/bandwidth_api.d.ts +7 -0
  14. package/bandwidth_api.d.ts.map +1 -0
  15. package/bandwidth_api.js +8 -0
  16. package/bandwidth_api.js.map +1 -0
  17. package/broadcast.d.ts +20 -28
  18. package/broadcast.d.ts.map +1 -1
  19. package/broadcast.js +55 -38
  20. package/broadcast.js.map +1 -1
  21. package/connection/accept.d.ts +16 -1
  22. package/connection/accept.d.ts.map +1 -1
  23. package/connection/accept.js +39 -24
  24. package/connection/accept.js.map +1 -1
  25. package/connection/browser.d.ts.map +1 -1
  26. package/connection/browser.js +9 -7
  27. package/connection/browser.js.map +1 -1
  28. package/connection/connect.d.ts +30 -6
  29. package/connection/connect.d.ts.map +1 -1
  30. package/connection/connect.js +85 -45
  31. package/connection/connect.js.map +1 -1
  32. package/connection/established.d.ts +16 -21
  33. package/connection/established.d.ts.map +1 -1
  34. package/connection/established.js.map +1 -1
  35. package/connection/forward.d.ts +2 -0
  36. package/connection/forward.d.ts.map +1 -0
  37. package/connection/forward.js +159 -0
  38. package/connection/forward.js.map +1 -0
  39. package/connection/handshake.d.ts.map +1 -1
  40. package/connection/handshake.js +2 -2
  41. package/connection/handshake.js.map +1 -1
  42. package/connection/index.d.ts +5 -5
  43. package/connection/index.d.ts.map +1 -1
  44. package/connection/index.js +4 -5
  45. package/connection/index.js.map +1 -1
  46. package/connection/pool.d.ts +188 -0
  47. package/connection/pool.d.ts.map +1 -0
  48. package/connection/pool.js +351 -0
  49. package/connection/pool.js.map +1 -0
  50. package/connection/reload.d.ts +14 -93
  51. package/connection/reload.d.ts.map +1 -1
  52. package/connection/reload.js +216 -80
  53. package/connection/reload.js.map +1 -1
  54. package/connection/stats.d.ts +3 -26
  55. package/connection/stats.d.ts.map +1 -1
  56. package/connection/stats.js.map +1 -1
  57. package/connection/transport.d.ts +0 -7
  58. package/connection/transport.d.ts.map +1 -1
  59. package/consume.d.ts +1 -43
  60. package/consume.d.ts.map +1 -1
  61. package/consume.js +1 -1
  62. package/consume.js.map +1 -1
  63. package/error.d.ts +180 -29
  64. package/error.d.ts.map +1 -1
  65. package/error.js +310 -16
  66. package/error.js.map +1 -1
  67. package/errors.d.ts +7 -0
  68. package/errors.d.ts.map +1 -0
  69. package/errors.js +8 -0
  70. package/errors.js.map +1 -0
  71. package/group.d.ts +9 -43
  72. package/group.d.ts.map +1 -1
  73. package/group.js +283 -69
  74. package/group.js.map +1 -1
  75. package/hop.d.ts +107 -0
  76. package/hop.d.ts.map +1 -0
  77. package/hop.js +103 -0
  78. package/hop.js.map +1 -0
  79. package/ietf/aliases.d.ts +1 -78
  80. package/ietf/aliases.d.ts.map +1 -1
  81. package/ietf/cluster.d.ts +6 -120
  82. package/ietf/cluster.d.ts.map +1 -1
  83. package/ietf/cluster.js +81 -41
  84. package/ietf/cluster.js.map +1 -1
  85. package/ietf/connection.d.ts +6 -72
  86. package/ietf/connection.d.ts.map +1 -1
  87. package/ietf/connection.js +11 -47
  88. package/ietf/connection.js.map +1 -1
  89. package/ietf/error.d.ts +11 -0
  90. package/ietf/error.d.ts.map +1 -0
  91. package/ietf/error.js +168 -0
  92. package/ietf/error.js.map +1 -0
  93. package/ietf/filter.d.ts.map +1 -1
  94. package/ietf/filter.js +6 -1
  95. package/ietf/filter.js.map +1 -1
  96. package/ietf/goaway.d.ts.map +1 -1
  97. package/ietf/goaway.js +22 -5
  98. package/ietf/goaway.js.map +1 -1
  99. package/ietf/parameters.d.ts +2 -2
  100. package/ietf/parameters.d.ts.map +1 -1
  101. package/ietf/parameters.js +15 -17
  102. package/ietf/parameters.js.map +1 -1
  103. package/ietf/publish_namespace.d.ts +25 -0
  104. package/ietf/publish_namespace.d.ts.map +1 -1
  105. package/ietf/publish_namespace.js +63 -0
  106. package/ietf/publish_namespace.js.map +1 -1
  107. package/ietf/publisher.d.ts +1 -82
  108. package/ietf/publisher.d.ts.map +1 -1
  109. package/ietf/publisher.js +203 -136
  110. package/ietf/publisher.js.map +1 -1
  111. package/ietf/solicit.d.ts +1 -40
  112. package/ietf/solicit.d.ts.map +1 -1
  113. package/ietf/subscribe.d.ts +1 -5
  114. package/ietf/subscribe.d.ts.map +1 -1
  115. package/ietf/subscribe.js +8 -9
  116. package/ietf/subscribe.js.map +1 -1
  117. package/ietf/subscriber.d.ts +1 -65
  118. package/ietf/subscriber.d.ts.map +1 -1
  119. package/ietf/subscriber.js +154 -73
  120. package/ietf/subscriber.js.map +1 -1
  121. package/ietf/version.d.ts +12 -1
  122. package/ietf/version.d.ts.map +1 -1
  123. package/ietf/version.js +13 -0
  124. package/ietf/version.js.map +1 -1
  125. package/index.d.ts +11 -6
  126. package/index.d.ts.map +1 -1
  127. package/index.js +10 -5
  128. package/index.js.map +1 -1
  129. package/internal.d.ts +97 -1
  130. package/internal.d.ts.map +1 -1
  131. package/internal.js +65 -0
  132. package/internal.js.map +1 -1
  133. package/lite/announce.d.ts +15 -9
  134. package/lite/announce.d.ts.map +1 -1
  135. package/lite/announce.js +47 -20
  136. package/lite/announce.js.map +1 -1
  137. package/lite/connection.d.ts +9 -59
  138. package/lite/connection.d.ts.map +1 -1
  139. package/lite/connection.js +20 -32
  140. package/lite/connection.js.map +1 -1
  141. package/lite/fetch.d.ts +15 -1
  142. package/lite/fetch.d.ts.map +1 -1
  143. package/lite/fetch.js +39 -7
  144. package/lite/fetch.js.map +1 -1
  145. package/lite/goaway.d.ts.map +1 -1
  146. package/lite/goaway.js +7 -1
  147. package/lite/goaway.js.map +1 -1
  148. package/lite/group.d.ts +18 -4
  149. package/lite/group.d.ts.map +1 -1
  150. package/lite/group.js +31 -10
  151. package/lite/group.js.map +1 -1
  152. package/lite/priority.d.ts +1 -61
  153. package/lite/priority.d.ts.map +1 -1
  154. package/lite/priority.js +4 -5
  155. package/lite/priority.js.map +1 -1
  156. package/lite/publisher.d.ts +1 -69
  157. package/lite/publisher.d.ts.map +1 -1
  158. package/lite/publisher.js +507 -200
  159. package/lite/publisher.js.map +1 -1
  160. package/lite/setup.d.ts +7 -7
  161. package/lite/setup.d.ts.map +1 -1
  162. package/lite/setup.js +13 -13
  163. package/lite/setup.js.map +1 -1
  164. package/lite/subscribe.d.ts +61 -13
  165. package/lite/subscribe.d.ts.map +1 -1
  166. package/lite/subscribe.js +186 -44
  167. package/lite/subscribe.js.map +1 -1
  168. package/lite/subscriber.d.ts +8 -57
  169. package/lite/subscriber.d.ts.map +1 -1
  170. package/lite/subscriber.js +179 -117
  171. package/lite/subscriber.js.map +1 -1
  172. package/lite/track.d.ts +4 -10
  173. package/lite/track.d.ts.map +1 -1
  174. package/lite/track.js +34 -29
  175. package/lite/track.js.map +1 -1
  176. package/lite/version.d.ts +31 -6
  177. package/lite/version.d.ts.map +1 -1
  178. package/lite/version.js +71 -11
  179. package/lite/version.js.map +1 -1
  180. package/origin.d.ts +229 -29
  181. package/origin.d.ts.map +1 -1
  182. package/origin.js +1087 -37
  183. package/origin.js.map +1 -1
  184. package/package.json +3 -2
  185. package/path.d.ts +25 -7
  186. package/path.d.ts.map +1 -1
  187. package/path.js +5 -3
  188. package/path.js.map +1 -1
  189. package/stream.d.ts.map +1 -1
  190. package/stream.js +23 -14
  191. package/stream.js.map +1 -1
  192. package/time.d.ts +15 -2
  193. package/time.d.ts.map +1 -1
  194. package/time.js +28 -9
  195. package/time.js.map +1 -1
  196. package/track.d.ts +166 -83
  197. package/track.d.ts.map +1 -1
  198. package/track.js +617 -187
  199. package/track.js.map +1 -1
  200. package/wire.d.ts +67 -0
  201. package/wire.d.ts.map +1 -0
  202. package/wire.js +32 -0
  203. package/wire.js.map +1 -0
  204. package/zod.d.ts +1 -1
  205. package/zod.d.ts.map +1 -1
  206. package/zod.js.map +1 -1
  207. package/mock.d.ts +0 -66
  208. package/mock.d.ts.map +0 -1
  209. package/mock.js +0 -243
  210. package/mock.js.map +0 -1
@@ -3,13 +3,15 @@ import { Signal } from "@moq/signals";
3
3
  import * as announce from "../announced.js";
4
4
  import * as broadcast from "../broadcast.js";
5
5
  import { BroadcastCache } from "../consume.js";
6
- import { error, ProtocolViolation, reason } from "../error.js";
6
+ import { controlTimeout, error, ProtocolViolation, reason, StreamCode, StreamError } from "../error.js";
7
7
  import * as netGroup from "../group.js";
8
- import { UNKNOWN_ORIGIN } from "../origin.js";
8
+ import { Cost, MAX_HOPS, routesEqual, UNKNOWN_HOP } from "../hop.js";
9
+ import { groupBounds, scopeCaptures, scopeHead, scopeOverlaps } from "../internal.js";
9
10
  import * as Path from "../path.js";
10
11
  import { Stream } from "../stream.js";
11
12
  import * as Time from "../time.js";
12
- import { withTimeout } from "../util/timeout.js";
13
+ import { TimeoutError, withTimeout } from "../util/timeout.js";
14
+ import { overrideBroadcastWire, wireOf } from "../wire.js";
13
15
  import { AnnounceInit, AnnounceOk, AnnounceRequest, decodeAnnounceBroadcastMaybe } from "./announce.js";
14
16
  import { Datagram as DatagramMessage } from "./datagram.js";
15
17
  import * as DatagramStream from "./datagram_stream.js";
@@ -18,9 +20,9 @@ import { sendOrder } from "./priority.js";
18
20
  import { Probe } from "./probe.js";
19
21
  import { ProbeLevel } from "./setup.js";
20
22
  import { StreamId } from "./stream.js";
21
- import { decodeSubscribeResponse, decodeSubscribeResponseMaybe, Subscribe, SubscribeUpdate } from "./subscribe.js";
23
+ import { decodeSubscribeResponse, decodeSubscribeResponseMaybe, EMPTY_RANGE, emptyRange, exclusiveGroupEnd, inclusiveGroupEnd, Subscribe, SubscribeUpdate, } from "./subscribe.js";
22
24
  import { TrackInfo, Track as TrackMessage } from "./track.js";
23
- import { hasAnnounceId, hasAnnounceOk, hasDatagrams, hasExcludeHop, hasProbeRtt, restartSupported, Version, } from "./version.js";
25
+ import { hasAnnounceId, hasAnnounceOk, hasDatagrams, hasProbeRtt, restartSupported, Version } from "./version.js";
24
26
  // Bound on how long stream-open plus the first response (SUBSCRIBE_OK on older
25
27
  // drafts, or TRACK_INFO on lite-05+) may take. Browsers cap concurrent QUIC streams
26
28
  // (Chrome ~100) and we open with waitUntilAvailable, so past the cap the open blocks
@@ -69,9 +71,8 @@ export class Subscriber {
69
71
  #quic;
70
72
  // The version of the connection.
71
73
  version;
72
- // Shared with the Publisher so callers can optionally filter out their
73
- // own announcements on a per-call basis (see {@link AnnouncedOptions}).
74
- origin;
74
+ // Shared with the Publisher so reflected announces can be dropped on receipt.
75
+ hop;
75
76
  // Our subscribed tracks. `timescale` resolves once known (from TRACK_INFO on
76
77
  // lite-05+, or implicit defaults on older drafts); group streams block on it
77
78
  // before decoding any frame, since a group's QUIC stream can race ahead.
@@ -94,43 +95,40 @@ export class Subscriber {
94
95
  * Creates a new Subscriber instance.
95
96
  * @param quic - The WebTransport session to use
96
97
  * @param version - The protocol version
97
- * @param origin - Origin id shared with the Publisher
98
+ * @param origin - Hop id shared with the Publisher
98
99
  * @param probe - Optional sink for the peer's PROBE estimates
99
100
  * @param peerSetup - Optional peer SETUP slot for capability gating (lite-05+)
100
101
  *
101
102
  * @internal
102
103
  */
103
- constructor(quic, version, origin, probe, peerSetup) {
104
+ constructor(quic, version, hop, probe, peerSetup) {
104
105
  this.#quic = quic;
105
106
  this.version = version;
106
- this.origin = origin;
107
+ this.hop = hop;
107
108
  this.#probe = probe;
108
109
  this.#peerSetup = peerSetup;
109
110
  }
110
111
  /**
111
- * Subscribe to broadcast announcements under `prefix`.
112
+ * Subscribe to broadcast announcements matching `scope`. Paths are relative
113
+ * to the session, not the scope.
112
114
  *
113
- * Pass `{ ignoreSelf: true }` to skip announces that have already traversed
114
- * this connection's {@link origin}.
115
+ * Reflected announces (those whose hop chain already includes this
116
+ * connection) are always dropped: moq-lite-06 has none to keep, and older
117
+ * versions stay consistent with that.
115
118
  */
116
- announced(prefix = Path.empty(), options = {}) {
117
- const announced = new announce.Producer(prefix);
118
- void this.#runAnnounced(announced, prefix, options);
119
+ announced(scope = Path.Pattern.all()) {
120
+ const announced = new announce.Producer();
121
+ // The wire speaks announce interest by prefix, and echoes suffixes beneath it.
122
+ void this.#runAnnounced(announced, scopeHead(scope), scope);
119
123
  return announced.consume();
120
124
  }
121
- async #runAnnounced(announced, prefix, options) {
125
+ async #runAnnounced(announced, prefix, scope) {
122
126
  console.debug(`announced: prefix=${prefix}`);
123
- // Lite04/05: send our own session-level origin id so the peer can skip announces
127
+ // Lite04/05: send our own session-level Hop ID so the peer can skip announces
124
128
  // whose hop chain already passed through us. Encoding drops it on every other
125
129
  // version, where we drop the reflected announce on receipt instead. Matches the
126
130
  // Rust subscriber's `exclude_hop: self.self_origin.id` in `run_announce_prefix`.
127
- const msg = new AnnounceRequest(prefix, this.origin);
128
- // Drop reflected announces so callers asking for "someone else's broadcasts"
129
- // don't re-see their own publishes. A caller can always ask for this, and it is
130
- // required on versions that don't carry excludeHop above: there the peer isn't
131
- // filtering them out for us, so filtering here keeps what the app sees the same
132
- // as lite-05. Lite01-03 carry no real hop ids, so the check never matches there.
133
- const dropReflected = options.ignoreSelf || !hasExcludeHop(this.version);
131
+ const msg = new AnnounceRequest(prefix, this.hop);
134
132
  // Opened outside the try so the catch can reach it: a protocol violation below has
135
133
  // to reset the stream, not just close our side of it.
136
134
  let stream;
@@ -145,17 +143,16 @@ export class Subscriber {
145
143
  // Send the announce interest.
146
144
  await stream.writer.u53(StreamId.Announce);
147
145
  await msg.encode(stream.writer, this.version);
148
- // Lite05+: the publisher reports its own origin id before any announces.
146
+ // Lite05+: the publisher reports its own Hop ID before any announces.
149
147
  // It no longer stamps itself onto each hop chain, so we append it here to
150
- // keep the ignoreSelf loop check seeing the full chain.
148
+ // keep the reflected-announce loop check seeing the full chain.
151
149
  let responderOrigin;
152
150
  if (hasAnnounceOk(this.version)) {
153
151
  const ok = await AnnounceOk.decode(stream.reader, this.version);
154
- // A responder that withholds its identity sends the reserved 0. It names
155
- // nobody, so folding it into a chain would stamp a placeholder that cannot
156
- // close a loop or tell two publishers apart. Treat it as absent instead,
157
- // which is the loop-blind route the draft describes.
158
- responderOrigin = ok.origin === UNKNOWN_ORIGIN ? undefined : ok.origin;
152
+ // Keep a withheld 0: it names nobody for loop detection, but it is the
153
+ // anonymous mark and must travel the reconstructed chain. Assigned identities
154
+ // stay off this hop and are never forwarded.
155
+ responderOrigin = ok.hop;
159
156
  }
160
157
  const advertised = new Map();
161
158
  switch (this.version) {
@@ -170,12 +167,17 @@ export class Subscriber {
170
167
  // ANNOUNCE_OK, so nothing names the publisher.
171
168
  for (const suffix of init.suffixes) {
172
169
  const path = Path.join(prefix, suffix);
173
- if (advertised.has(suffix)) {
170
+ if (advertised.has(path)) {
174
171
  throw new ProtocolViolation(`duplicate announce for ${path}`);
175
172
  }
176
- advertised.set(suffix, { publisher: undefined, live: true });
173
+ const route = { hops: [UNKNOWN_HOP], cost: Cost.zero };
174
+ const live = scopeOverlaps(scope, path);
175
+ const captures = scopeCaptures(scope, path);
176
+ advertised.set(path, { publisher: undefined, live, route, captures });
177
+ if (!live)
178
+ continue;
177
179
  console.debug(`announced: broadcast=${path} active=true`);
178
- announced.append({ path: suffix, active: true });
180
+ announced.append({ prefix: path, captures, kind: "announced", route });
179
181
  }
180
182
  break;
181
183
  }
@@ -185,7 +187,7 @@ export class Subscriber {
185
187
  }
186
188
  // Lite06+: announce ids. Each received `active` implicitly assigns the next
187
189
  // per-stream ordinal; `endedId`/`restart` reference it. Tracked even for
188
- // announces we skip via ignoreSelf, since the sender doesn't know we skipped.
190
+ // announces we skip as reflected, since the sender doesn't know we skipped.
189
191
  let nextAnnounceId = 0n;
190
192
  const announcedById = new Map();
191
193
  // Receive announce updates (for Draft03, this includes initial state)
@@ -199,45 +201,55 @@ export class Subscriber {
199
201
  break;
200
202
  if (announce instanceof Error)
201
203
  throw announce;
202
- let suffix;
204
+ let path;
203
205
  let active;
204
206
  // Present on active/restart; ended messages never carry hops worth checking.
205
207
  let hops;
208
+ let cost;
206
209
  switch (announce.status) {
207
210
  case "active":
208
- suffix = announce.suffix;
211
+ // The wire names the suffix beneath the interest prefix; the consumer
212
+ // sees the covered path from the session root.
213
+ path = Path.join(prefix, announce.suffix);
209
214
  active = true;
210
215
  hops = announce.hops;
216
+ cost = announce.cost;
211
217
  if (hasAnnounceId(this.version)) {
212
- announcedById.set(nextAnnounceId++, announce.suffix);
218
+ announcedById.set(nextAnnounceId++, path);
213
219
  }
214
220
  break;
215
221
  case "ended":
216
- suffix = announce.suffix;
222
+ path = Path.join(prefix, announce.suffix);
217
223
  active = false;
218
224
  break;
219
225
  case "endedId": {
220
226
  // Resolve and retire the id; an unknown or retired id is a protocol violation.
221
- const path = announcedById.get(announce.id);
222
- if (path === undefined)
227
+ const resolved = announcedById.get(announce.id);
228
+ if (resolved === undefined)
223
229
  throw new ProtocolViolation(`unknown announce id: ${announce.id}`);
224
230
  announcedById.delete(announce.id);
225
- suffix = path;
231
+ if (resolved === null)
232
+ continue;
233
+ path = resolved;
226
234
  active = false;
227
235
  break;
228
236
  }
229
237
  case "restart": {
230
238
  // Resolve the id; it stays live (the replacement reuses it).
231
- const path = announcedById.get(announce.id);
232
- if (path === undefined)
239
+ const resolved = announcedById.get(announce.id);
240
+ if (resolved === undefined)
233
241
  throw new ProtocolViolation(`unknown announce id: ${announce.id}`);
234
- suffix = path;
242
+ if (resolved === null)
243
+ continue;
244
+ path = resolved;
235
245
  active = true;
236
246
  hops = announce.hops;
247
+ cost = announce.cost;
237
248
  break;
238
249
  }
250
+ case "skipped":
251
+ continue;
239
252
  }
240
- const path = Path.join(prefix, suffix);
241
253
  // One current advertisement per path per stream, decided before anything below
242
254
  // can skip this announcement. A second ANNOUNCE_START for a path the peer
243
255
  // already advertised is a violation whether or not its route would be usable
@@ -250,7 +262,7 @@ export class Subscriber {
250
262
  // a duplicate means the same thing on both sides of it. Mirrors the branch the
251
263
  // Rust announce loop takes before `start_announce`.
252
264
  const duplicateIsRestart = restartSupported(this.version) && !hasAnnounceId(this.version);
253
- if (announce.status === "active" && !duplicateIsRestart && advertised.has(suffix)) {
265
+ if (announce.status === "active" && !duplicateIsRestart && advertised.has(path)) {
254
266
  throw new ProtocolViolation(`duplicate announce for ${path}`);
255
267
  }
256
268
  // Retract the path: forget the advertisement, drop the shared consume entry so a
@@ -258,63 +270,101 @@ export class Subscriber {
258
270
  // and tell the consumer. A no-op for an advertisement never surfaced, which is
259
271
  // what an id retiring a skipped announce resolves to.
260
272
  const retract = () => {
261
- const previous = advertised.get(suffix);
262
- advertised.delete(suffix);
273
+ const previous = advertised.get(path);
274
+ advertised.delete(path);
263
275
  if (!previous?.live)
264
276
  return;
265
277
  this.#consumes.evict(path);
266
278
  console.debug(`announced: broadcast=${path} active=false`);
267
- announced.append({ path: suffix, active: false });
279
+ announced.append({
280
+ prefix: path,
281
+ captures: previous.captures,
282
+ kind: "retracted",
283
+ route: previous.route,
284
+ });
268
285
  };
269
286
  // In Lite05+ the sender's origin arrives via AnnounceOk, not in each hop
270
287
  // list, so fold it back in before checking.
271
- if (hops !== undefined && dropReflected) {
288
+ if (hops !== undefined) {
272
289
  const full = responderOrigin !== undefined ? [...hops, responderOrigin] : hops;
273
- if (full.includes(this.origin)) {
290
+ if (full.includes(this.hop)) {
274
291
  // A reflected restart means the peer's remaining route loops back through
275
292
  // us, so the route is gone even though the message says active. The
276
293
  // advertisement stays live: the peer still holds the path and its id still
277
294
  // resolves here.
278
295
  retract();
279
- advertised.set(suffix, { publisher: undefined, live: false });
296
+ advertised.set(path, {
297
+ publisher: undefined,
298
+ live: false,
299
+ route: { hops: full, cost: Cost.zero },
300
+ captures: undefined,
301
+ });
280
302
  continue;
281
303
  }
282
304
  }
283
- if (active) {
284
- // The first hop identifies the original publisher; an empty chain means the
285
- // peer itself originated it. See `restart_announce` in the Rust subscriber.
286
- const publisher = hops?.[0] ?? responderOrigin;
287
- // A publisher with no identity (an empty chain from a peer that withheld its
288
- // own id, or a lite-03 UNKNOWN placeholder) never proves continuity: two such
289
- // advertisements can be unrelated publishers. Mirrors the
290
- // `publisher == Origin::UNKNOWN` arm of the Rust `restart_announce`.
291
- const identified = publisher !== undefined && publisher !== UNKNOWN_ORIGIN;
292
- // A second advertisement for a path we already carry is a restart: either an
293
- // explicit ANNOUNCE_UPDATE, or (lite-05) a duplicate ANNOUNCE.
294
- const previous = advertised.get(suffix);
295
- if (previous?.live) {
296
- if (identified && previous.publisher === publisher) {
297
- // Same publisher, new route. In-flight subscriptions resume across it,
298
- // so there is nothing for a consumer to react to. An unidentified
299
- // publisher falls through to the replacement path below instead.
305
+ if (!active) {
306
+ retract();
307
+ continue;
308
+ }
309
+ // The first hop identifies the original publisher; an empty chain means the
310
+ // peer itself originated it. See `restart_announce` in the Rust subscriber.
311
+ const publisher = hops?.[0] ?? responderOrigin;
312
+ // A publisher with no identity (an empty chain from a peer that withheld its
313
+ // own id, or a lite-03 UNKNOWN placeholder) never proves continuity: two such
314
+ // advertisements can be unrelated publishers. Mirrors the
315
+ // `publisher == Hop::UNKNOWN` arm of the Rust `restart_announce`.
316
+ const identified = publisher !== undefined && publisher !== UNKNOWN_HOP;
317
+ const fullHops = hops !== undefined && responderOrigin !== undefined
318
+ ? [...hops, responderOrigin]
319
+ : [...(hops ?? [])];
320
+ // A received empty list is the anonymous mark, not a local announcement.
321
+ if (fullHops.length === 0)
322
+ fullHops.push(UNKNOWN_HOP);
323
+ // Appending a withheld AnnounceOk(0) onto a 32-entry list is the same
324
+ // drop Rust's Hops::push makes: do not expose an overlong chain.
325
+ if (fullHops.length > MAX_HOPS) {
326
+ console.debug(`announced: broadcast=${path} dropped (hop chain at MAX_HOPS)`);
327
+ advertised.set(path, {
328
+ publisher: undefined,
329
+ live: false,
330
+ route: { hops: [], cost: Cost.zero },
331
+ captures: undefined,
332
+ });
333
+ continue;
334
+ }
335
+ const route = { hops: fullHops, cost: cost ?? Cost.zero };
336
+ const captures = scopeCaptures(scope, path);
337
+ if (!scopeOverlaps(scope, path)) {
338
+ advertised.set(path, { publisher, live: false, route, captures });
339
+ continue;
340
+ }
341
+ // A second advertisement for a path we already carry is a restart: either an
342
+ // explicit ANNOUNCE_UPDATE, or (lite-05) a duplicate ANNOUNCE.
343
+ const previous = advertised.get(path);
344
+ if (previous?.live) {
345
+ if (identified && previous.publisher === publisher) {
346
+ // Same publisher, new route. In-flight subscriptions resume across it.
347
+ // Emit the route so a forwarder can re-price without retracting.
348
+ if (!routesEqual(previous.route, route)) {
349
+ advertised.set(path, { publisher, live: true, route, captures });
300
350
  console.debug(`announced: broadcast=${path} rerouted`);
301
- continue;
351
+ announced.append({ prefix: path, captures, kind: "updated", route });
302
352
  }
303
- // A different publisher took the path, so cached track info and existing
304
- // subscriptions must not carry over. Surface a real end before the start.
305
- retract();
353
+ else {
354
+ console.debug(`announced: broadcast=${path} rerouted`);
355
+ }
356
+ continue;
306
357
  }
307
- // After `retract()`, which clears the entry: the path is advertised again, by
308
- // whoever just took it over. Recording it before would leave nothing behind, so
309
- // the *next* takeover would read as a first announcement and skip its own end.
310
- advertised.set(suffix, { publisher, live: true });
311
- }
312
- else {
358
+ // A different publisher took the path, so cached track info and existing
359
+ // subscriptions must not carry over. Surface a real end before the start.
313
360
  retract();
314
- continue;
315
361
  }
362
+ // After `retract()`, which clears the entry: the path is advertised again, by
363
+ // whoever just took it over. Recording it before would leave nothing behind, so
364
+ // the *next* takeover would read as a first announcement and skip its own end.
365
+ advertised.set(path, { publisher, live: true, route, captures });
316
366
  console.debug(`announced: broadcast=${path} active=true`);
317
- announced.append({ path: suffix, active: true });
367
+ announced.append({ prefix: path, captures, kind: "announced", route });
318
368
  }
319
369
  announced.close();
320
370
  }
@@ -354,7 +404,7 @@ export class Subscriber {
354
404
  const consumer = new ConsumeBroadcast(this, path);
355
405
  void (async () => {
356
406
  for (;;) {
357
- const request = await consumer.requested();
407
+ const request = await wireOf(consumer).requested();
358
408
  if (!request)
359
409
  break;
360
410
  void this.#runSubscribe(path, request);
@@ -365,19 +415,24 @@ export class Subscriber {
365
415
  async #runSubscribe(broadcast, request) {
366
416
  const id = this.#subscribeNext++;
367
417
  const subscription = request.subscription;
418
+ const initialBounds = groupBounds(subscription.groups);
419
+ if (emptyRange({ startGroup: initialBounds.start, endGroup: initialBounds.end })) {
420
+ request.reject(new Error(EMPTY_RANGE));
421
+ return;
422
+ }
368
423
  // `timescale` stays undefined until TRACK_INFO (or, on older drafts,
369
424
  // implicit defaults) resolves it; runGroup blocks on it before decoding.
370
425
  const timescale = new Signal(undefined);
371
426
  console.debug(`subscribe start: id=${id} broadcast=${broadcast} track=${request.name}`);
427
+ const bounds = groupBounds(subscription.groups);
372
428
  const msg = new Subscribe({
373
429
  id,
374
430
  broadcast,
375
431
  track: request.name,
376
432
  priority: subscription.priority ?? 0,
377
- ordered: subscription.ordered,
378
- maxLatency: subscription.latencyMax,
379
- startGroup: subscription.startGroup,
380
- endGroup: subscription.endGroup,
433
+ maxAge: subscription.maxAge,
434
+ startGroup: subscription.groups?.start === undefined ? undefined : bounds.start,
435
+ endGroup: inclusiveGroupEnd(bounds.end),
381
436
  });
382
437
  // Open the stream under a timeout. The stream handle flows back via `state`
383
438
  // so the timeout path can abort it if it finishes opening after the deadline.
@@ -389,7 +444,9 @@ export class Subscriber {
389
444
  console.debug(`subscribe ok: id=${id} broadcast=${broadcast} track=${request.name}`);
390
445
  }
391
446
  catch (err) {
392
- const e = error(err);
447
+ // The setup outlived its deadline waiting for the first response: a control
448
+ // timeout, not content that arrived late.
449
+ const e = err instanceof TimeoutError ? controlTimeout(err) : error(err);
393
450
  request.reject(e);
394
451
  this.#subscribes.delete(id);
395
452
  console.warn(`subscribe error: id=${id} broadcast=${broadcast} track=${request.name} error=${reason(e)}`);
@@ -500,15 +557,14 @@ export class Subscriber {
500
557
  #toModelInfo(info) {
501
558
  return {
502
559
  timescale: Time.Timescale(info.timescale),
503
- // Publisher Max Latency rides on the wire, so the local retention window
560
+ // Publisher Max Age rides on the wire, so the local retention window
504
561
  // matches what the upstream advertises (relays re-serve with the same bound).
505
- latencyMax: info.latencyMax,
562
+ maxAge: Time.Milli(info.maxAge),
506
563
  priority: info.priority,
507
- ordered: info.ordered,
508
564
  };
509
565
  }
510
566
  // Resolve a track's immutable model info via a TRACK stream (lite-05+), for the
511
- // ConsumeBroadcast backing track.Consumer.info(). On older drafts there's no TRACK
567
+ // ConsumeBroadcast backing track.Consumer.query(). On older drafts there's no TRACK
512
568
  // stream, so this rejects rather than fabricating defaults.
513
569
  async resolveTrackInfo(broadcast, track) {
514
570
  if (!supportsTrackStream(this.version)) {
@@ -547,7 +603,7 @@ export class Subscriber {
547
603
  const stream = await Stream.open(this.#quic, { sendOrder: sendOrder({ priority }) });
548
604
  try {
549
605
  await stream.writer.u53(StreamId.Fetch);
550
- await new FetchMessage(broadcast, track, priority, sequence).encode(stream.writer, this.version);
606
+ await new FetchMessage({ broadcast, track, priority, group: sequence }).encode(stream.writer, this.version);
551
607
  }
552
608
  catch (err) {
553
609
  stream.abort(error(err));
@@ -636,10 +692,11 @@ export class Subscriber {
636
692
  const stopped = Promise.race([track.closed, stream.reader.closed]).then(() => null);
637
693
  let lastSent = {
638
694
  priority: msg.priority,
639
- ordered: msg.ordered,
640
- latencyMax: msg.maxLatency,
641
- startGroup: msg.startGroup,
642
- endGroup: msg.endGroup,
695
+ maxAge: Time.Milli(msg.maxAge),
696
+ groups: {
697
+ start: msg.startGroup === undefined ? undefined : { included: msg.startGroup },
698
+ end: msg.endGroup === undefined ? undefined : { excluded: exclusiveGroupEnd(msg.endGroup) ?? 0 },
699
+ },
643
700
  };
644
701
  for (;;) {
645
702
  const current = track.subscription.peek();
@@ -650,12 +707,18 @@ export class Subscriber {
650
707
  return;
651
708
  continue;
652
709
  }
710
+ // Demand collapsing to nothing is refused the same way an initial empty
711
+ // request is: the error closes the track, so every local subscriber sees it.
712
+ const bounds = groupBounds(current.groups);
713
+ if (emptyRange({ startGroup: bounds.start, endGroup: bounds.end }))
714
+ throw new Error(EMPTY_RANGE);
715
+ // Round-trip the other Subscribe parameters so the publisher doesn't
716
+ // interpret SUBSCRIBE_UPDATE as a reset of ordered/maxAge/etc.
653
717
  const update = new SubscribeUpdate({
654
718
  priority: current.priority ?? 0,
655
- ordered: current.ordered,
656
- maxLatency: current.latencyMax,
657
- startGroup: current.startGroup,
658
- endGroup: current.endGroup,
719
+ maxAge: current.maxAge,
720
+ startGroup: current.groups?.start === undefined ? undefined : bounds.start,
721
+ endGroup: inclusiveGroupEnd(bounds.end),
659
722
  });
660
723
  await update.encode(stream.writer, this.version);
661
724
  lastSent = { ...current };
@@ -663,11 +726,12 @@ export class Subscriber {
663
726
  }
664
727
  }
665
728
  #sameSubscription(a, b) {
729
+ const ag = groupBounds(a.groups);
730
+ const bg = groupBounds(b.groups);
666
731
  return ((a.priority ?? 0) === (b.priority ?? 0) &&
667
- (a.ordered ?? false) === (b.ordered ?? false) &&
668
- (a.latencyMax ?? 0) === (b.latencyMax ?? 0) &&
669
- a.startGroup === b.startGroup &&
670
- a.endGroup === b.endGroup);
732
+ (a.maxAge ?? 0) === (b.maxAge ?? 0) &&
733
+ ag.start === bg.start &&
734
+ ag.end === bg.end);
671
735
  }
672
736
  /**
673
737
  * Handles a group message.
@@ -695,7 +759,7 @@ export class Subscriber {
695
759
  if (track.closed.peek() !== undefined) {
696
760
  // Subscription ended before the scale resolved; nothing to decode.
697
761
  producer.close();
698
- stream.stop(new Error("cancel"));
762
+ stream.stop(new StreamError(StreamCode.Cancel, { message: "cancel" }));
699
763
  return;
700
764
  }
701
765
  await Signal.race(timescale, track.closed);
@@ -724,7 +788,7 @@ export class Subscriber {
724
788
  producer.writeFrame({ payload, timestamp });
725
789
  }
726
790
  producer.close();
727
- stream.stop(new Error("cancel"));
791
+ stream.stop(new StreamError(StreamCode.Cancel, { message: "cancel" }));
728
792
  }
729
793
  catch (err) {
730
794
  const e = error(err);
@@ -793,7 +857,7 @@ export class Subscriber {
793
857
  if (!scale)
794
858
  return;
795
859
  const timestamp = new Time.Timestamp(dg.timestamp, Time.Timescale(scale));
796
- entry.track.writeDatagram({ sequence: dg.sequence, timestamp, payload: dg.payload });
860
+ entry.track.insertDatagram(dg.sequence, timestamp, dg.payload);
797
861
  }
798
862
  /**
799
863
  * Opens a PROBE bidi stream to receive bandwidth estimates from the publisher.
@@ -870,7 +934,7 @@ export class Subscriber {
870
934
  }
871
935
  }
872
936
  /**
873
- * A broadcast consumed from a lite session. It resolves `track.Consumer.info()` and
937
+ * A broadcast consumed from a lite session. It resolves `track.Consumer.query()` and
874
938
  * `.fetchGroup()` over the wire (lite-05+ TRACK / FETCH streams) by reaching into the
875
939
  * {@link Subscriber} it was opened from, the way the Rust `BroadcastConsumer` holds its
876
940
  * session. Live subscribes still flow through the inherited requested() queue.
@@ -880,6 +944,10 @@ class ConsumeBroadcast extends broadcast.Consumer {
880
944
  #path;
881
945
  constructor(subscriber, path, state) {
882
946
  super(state);
947
+ overrideBroadcastWire(this, {
948
+ resolveTrackInfo: (name) => subscriber.resolveTrackInfo(path, name),
949
+ fetchGroup: (name, sequence, options) => subscriber.fetchGroup(path, name, sequence, options),
950
+ });
883
951
  this.#subscriber = subscriber;
884
952
  this.#path = path;
885
953
  }
@@ -888,11 +956,5 @@ class ConsumeBroadcast extends broadcast.Consumer {
888
956
  clone() {
889
957
  return new ConsumeBroadcast(this.#subscriber, this.#path, this.shareState());
890
958
  }
891
- resolveTrackInfo(name) {
892
- return this.#subscriber.resolveTrackInfo(this.#path, name);
893
- }
894
- fetchGroup(name, sequence, options) {
895
- return this.#subscriber.fetchGroup(this.#path, name, sequence, options);
896
- }
897
959
  }
898
960
  //# sourceMappingURL=subscriber.js.map