@moq/net 0.4.0 → 0.4.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 (121) hide show
  1. package/announced.d.ts +14 -0
  2. package/announced.d.ts.map +1 -1
  3. package/announced.js.map +1 -1
  4. package/broadcast.d.ts +6 -3
  5. package/broadcast.d.ts.map +1 -1
  6. package/broadcast.js +9 -5
  7. package/broadcast.js.map +1 -1
  8. package/connection/accept.js +9 -1
  9. package/connection/accept.js.map +1 -1
  10. package/connection/connect.d.ts.map +1 -1
  11. package/connection/connect.js +10 -1
  12. package/connection/connect.js.map +1 -1
  13. package/connection/established.d.ts +2 -1
  14. package/connection/established.d.ts.map +1 -1
  15. package/connection/established.js.map +1 -1
  16. package/connection/forward.js +9 -2
  17. package/connection/forward.js.map +1 -1
  18. package/connection/handshake.d.ts +1 -0
  19. package/connection/handshake.d.ts.map +1 -1
  20. package/connection/handshake.js +3 -0
  21. package/connection/handshake.js.map +1 -1
  22. package/connection/pool.d.ts +8 -10
  23. package/connection/pool.d.ts.map +1 -1
  24. package/connection/pool.js +18 -8
  25. package/connection/pool.js.map +1 -1
  26. package/connection/reload.js +6 -6
  27. package/connection/reload.js.map +1 -1
  28. package/error.d.ts.map +1 -1
  29. package/error.js +21 -0
  30. package/error.js.map +1 -1
  31. package/ietf/adapter.d.ts +5 -1
  32. package/ietf/adapter.d.ts.map +1 -1
  33. package/ietf/adapter.js +27 -9
  34. package/ietf/adapter.js.map +1 -1
  35. package/ietf/connection.d.ts +1 -1
  36. package/ietf/connection.d.ts.map +1 -1
  37. package/ietf/connection.js +9 -5
  38. package/ietf/connection.js.map +1 -1
  39. package/ietf/error.js +27 -2
  40. package/ietf/error.js.map +1 -1
  41. package/ietf/hidden.d.ts +2 -0
  42. package/ietf/hidden.d.ts.map +1 -0
  43. package/ietf/hidden.js +30 -0
  44. package/ietf/hidden.js.map +1 -0
  45. package/ietf/index.d.ts +1 -0
  46. package/ietf/index.d.ts.map +1 -1
  47. package/ietf/index.js +1 -0
  48. package/ietf/index.js.map +1 -1
  49. package/ietf/object.d.ts +8 -2
  50. package/ietf/object.d.ts.map +1 -1
  51. package/ietf/object.js +17 -3
  52. package/ietf/object.js.map +1 -1
  53. package/ietf/parameters.d.ts +5 -0
  54. package/ietf/parameters.d.ts.map +1 -1
  55. package/ietf/parameters.js +20 -0
  56. package/ietf/parameters.js.map +1 -1
  57. package/ietf/properties.d.ts +1 -0
  58. package/ietf/properties.d.ts.map +1 -1
  59. package/ietf/properties.js +14 -0
  60. package/ietf/properties.js.map +1 -1
  61. package/ietf/publish.d.ts +19 -2
  62. package/ietf/publish.d.ts.map +1 -1
  63. package/ietf/publish.js +40 -6
  64. package/ietf/publish.js.map +1 -1
  65. package/ietf/publisher.js +114 -60
  66. package/ietf/publisher.js.map +1 -1
  67. package/ietf/subscribe.d.ts.map +1 -1
  68. package/ietf/subscribe.js +3 -4
  69. package/ietf/subscribe.js.map +1 -1
  70. package/ietf/subscribe_namespace.d.ts +8 -2
  71. package/ietf/subscribe_namespace.d.ts.map +1 -1
  72. package/ietf/subscribe_namespace.js +18 -8
  73. package/ietf/subscribe_namespace.js.map +1 -1
  74. package/ietf/subscriber.js +189 -66
  75. package/ietf/subscriber.js.map +1 -1
  76. package/internal.d.ts +7 -0
  77. package/internal.d.ts.map +1 -1
  78. package/internal.js +11 -0
  79. package/internal.js.map +1 -1
  80. package/lite/announce.d.ts +46 -2
  81. package/lite/announce.d.ts.map +1 -1
  82. package/lite/announce.js +126 -11
  83. package/lite/announce.js.map +1 -1
  84. package/lite/connection.d.ts +1 -1
  85. package/lite/connection.d.ts.map +1 -1
  86. package/lite/connection.js +9 -3
  87. package/lite/connection.js.map +1 -1
  88. package/lite/publisher.js +168 -109
  89. package/lite/publisher.js.map +1 -1
  90. package/lite/subscribe.d.ts +10 -4
  91. package/lite/subscribe.d.ts.map +1 -1
  92. package/lite/subscribe.js +20 -10
  93. package/lite/subscribe.js.map +1 -1
  94. package/lite/subscriber.d.ts +10 -2
  95. package/lite/subscriber.d.ts.map +1 -1
  96. package/lite/subscriber.js +159 -93
  97. package/lite/subscriber.js.map +1 -1
  98. package/lite/version.d.ts +11 -0
  99. package/lite/version.d.ts.map +1 -1
  100. package/lite/version.js +56 -0
  101. package/lite/version.js.map +1 -1
  102. package/origin.d.ts +37 -18
  103. package/origin.d.ts.map +1 -1
  104. package/origin.js +248 -103
  105. package/origin.js.map +1 -1
  106. package/package.json +7 -3
  107. package/stream.d.ts +2 -2
  108. package/stream.d.ts.map +1 -1
  109. package/stream.js +16 -11
  110. package/stream.js.map +1 -1
  111. package/tail.d.ts +11 -0
  112. package/tail.d.ts.map +1 -0
  113. package/tail.js +121 -0
  114. package/tail.js.map +1 -0
  115. package/track.d.ts +33 -5
  116. package/track.d.ts.map +1 -1
  117. package/track.js +98 -30
  118. package/track.js.map +1 -1
  119. package/wire.d.ts +2 -0
  120. package/wire.d.ts.map +1 -1
  121. package/wire.js.map +1 -1
package/origin.js CHANGED
@@ -15,11 +15,44 @@ import { Derived, getter, Once, Signal } from "@moq/signals";
15
15
  import * as announce from "./announced.js";
16
16
  import * as broadcast from "./broadcast.js";
17
17
  import { StreamCode, StreamError } from "./error.js";
18
- import { Route, routesEqual } from "./hop.js";
19
- import { hooks, scopeCaptures, scopeOverlaps } from "./internal.js";
18
+ import { isAnonymous, Route, routesEqual } from "./hop.js";
19
+ import { hiddenBelow, hooks, scopeCaptures, scopeHead, scopeOverlaps } from "./internal.js";
20
20
  import * as Path from "./path.js";
21
21
  import { registerWire, wireOf } from "./wire.js";
22
22
  export { isAnonymous } from "./hop.js";
23
+ /** Orders two routes by preference: identified before anonymous, then lower warm cost, then lower cold cost. */
24
+ function compareRoutes(a, b) {
25
+ const anonymous = Number(isAnonymous(a)) - Number(isAnonymous(b));
26
+ if (anonymous !== 0)
27
+ return anonymous;
28
+ if (a.cost.warm !== b.cost.warm)
29
+ return a.cost.warm < b.cost.warm ? -1 : 1;
30
+ if (a.cost.cold !== b.cost.cold)
31
+ return a.cost.cold < b.cost.cold ? -1 : 1;
32
+ return 0;
33
+ }
34
+ /** The preferred of `entries` (newest first) not skipped: the best route, then fewest hops, then newest. */
35
+ function preferredEntry(entries, skip) {
36
+ let best;
37
+ for (const entry of entries) {
38
+ if (skip?.(entry))
39
+ continue;
40
+ if (!best) {
41
+ best = entry;
42
+ continue;
43
+ }
44
+ const a = entry.route.peek();
45
+ const b = best.route.peek();
46
+ const order = compareRoutes(a, b) || a.hops.length - b.hops.length;
47
+ if (order < 0)
48
+ best = entry;
49
+ }
50
+ return best;
51
+ }
52
+ /** Whether a session received `entry`, so it is never forwarded to a peer. */
53
+ function received(entry) {
54
+ return !entry.originated;
55
+ }
23
56
  function noCapacity() {
24
57
  return new StreamError(StreamCode.NoCapacity, { message: "no capacity" });
25
58
  }
@@ -107,20 +140,74 @@ class ServeState {
107
140
  this.settled.update((n) => n + 1);
108
141
  }
109
142
  }
143
+ /** A table mutation invalidates the shared route snapshot before its async notification. */
144
+ class VersionedSignal extends Signal {
145
+ version = 0;
146
+ set(value, notify) {
147
+ this.version++;
148
+ super.set(value, notify);
149
+ }
150
+ }
110
151
  /** Reactive backing state shared by origin producers and consumers. */
111
152
  class OriginState {
112
153
  // Both tables decouple the application producing into the origin from the
113
154
  // connections serving or feeding it. Undefined once the origin closes, so late
114
155
  // writes fail loudly.
115
156
  //
116
- // Local is what this endpoint creates, keyed by exact path: reachable for
117
- // subscribes whether or not it is advertised. Routes is the advertisement table:
118
- // prefixes a dynamic handle or a received session covers, newest first. They stay
119
- // separate so a session can never announce a received entry back to a peer, which
120
- // is what makes an origin shared by both directions echo-free.
121
- local = new Signal(new Map());
122
- advertisedLocal = new Signal(new Map());
123
- routes = new Signal(new Map());
157
+ // Created is what this endpoint publishes, keyed by exact path, announced or not.
158
+ // Local is the announced subset, with its route in advertisedLocal: a broadcast
159
+ // exists for nobody, here or at a peer, until it announces. Routes is the
160
+ // advertisement table: prefixes a dynamic handle or a received session covers,
161
+ // newest first. Local and routes stay separate so a session can never announce a
162
+ // received entry back to a peer, which is what makes an origin shared by both
163
+ // directions echo-free.
164
+ created = new Map();
165
+ local = new VersionedSignal(new Map());
166
+ advertisedLocal = new VersionedSignal(new Map());
167
+ routes = new VersionedSignal(new Map());
168
+ #snapshotVersion = "";
169
+ #snapshot = {
170
+ remote: new Map(),
171
+ local: new Map(),
172
+ routes: new Map(),
173
+ visible: new Map(),
174
+ };
175
+ /** The full route table is built once per mutation, regardless of observer count. */
176
+ available = new Derived([this.local, this.advertisedLocal, this.routes], () => this.snapshot().routes);
177
+ /** {@link available} without hidden routes, for unscoped readers that did not opt in. */
178
+ visible = new Derived([this.local, this.advertisedLocal, this.routes], () => this.snapshot().visible);
179
+ snapshot() {
180
+ const version = `${this.local.version}/${this.advertisedLocal.version}/${this.routes.version}`;
181
+ if (version === this.#snapshotVersion)
182
+ return this.#snapshot;
183
+ const remote = new Map();
184
+ const local = new Map();
185
+ const available = new Map();
186
+ for (const [path, routes] of this.routes.peek() ?? []) {
187
+ const entry = preferredEntry(routes);
188
+ if (!entry)
189
+ continue;
190
+ const value = { identity: entry.identity, route: entry.route.peek() };
191
+ remote.set(path, value);
192
+ available.set(path, value.route);
193
+ }
194
+ for (const [path, front] of this.local.peek() ?? []) {
195
+ const routes = this.routes.peek()?.get(path);
196
+ if (!this.localWins(path, routes && preferredEntry(routes)))
197
+ continue;
198
+ const value = { identity: front, route: this.advertisedLocal.peek()?.get(path) ?? Route.default };
199
+ local.set(path, value);
200
+ available.set(path, value.route);
201
+ }
202
+ const visible = new Map();
203
+ for (const [path, route] of available) {
204
+ if (!hiddenBelow(Path.empty(), path))
205
+ visible.set(path, route);
206
+ }
207
+ this.#snapshot = { remote, local, routes: available, visible };
208
+ this.#snapshotVersion = version;
209
+ return this.#snapshot;
210
+ }
124
211
  // Originated advertisements sessions should forward: exact-path announces plus
125
212
  // originated dynamics. Identity is the local front or the route entry, so a
126
213
  // republish diffs as retract-then-announce and a re-price as another active.
@@ -174,7 +261,8 @@ class OriginState {
174
261
  for (const [path, cached] of [...this.materialized]) {
175
262
  if (!Path.hasPrefix(prefix, path))
176
263
  continue;
177
- if (cached.entry !== this.bestEntry(path, requests?.get(path)?.refused)) {
264
+ const refused = requests?.get(path)?.refused;
265
+ if (cached.entry !== this.bestEntry(path, (entry) => refused?.has(entry) ?? false)) {
178
266
  this.materialized.delete(path);
179
267
  cached.front.close();
180
268
  }
@@ -194,16 +282,19 @@ class OriginState {
194
282
  return;
195
283
  }
196
284
  const next = new Map();
197
- for (const [path, route] of advertised ?? []) {
198
- const front = local?.get(path);
199
- if (front)
200
- next.set(path, { identity: front, route });
201
- }
202
285
  for (const [prefix, entries] of routes ?? []) {
203
- const mine = entries.find((entry) => entry.originated);
286
+ const mine = preferredEntry(entries, received);
204
287
  if (mine)
205
288
  next.set(prefix, { identity: mine.identity, route: mine.route.peek() });
206
289
  }
290
+ // A local broadcast and an originated dynamic at one path compete on cost, as they do for requests.
291
+ for (const [path, route] of advertised ?? []) {
292
+ const front = local?.get(path);
293
+ const entries = routes?.get(path);
294
+ if (front && this.localWins(path, entries && preferredEntry(entries, received))) {
295
+ next.set(path, { identity: front, route });
296
+ }
297
+ }
207
298
  this.originated.set(next);
208
299
  }
209
300
  /**
@@ -218,16 +309,16 @@ class OriginState {
218
309
  this.materialized.delete(path);
219
310
  cached.front.close();
220
311
  }
221
- /** The newest entry on the most specific route covering `path`, skipping `refused`, if any. */
222
- bestEntry(path, refused) {
312
+ /** The preferred entry on the most specific route covering `path`, ignoring skipped entries, if any. */
313
+ bestEntry(path, skip) {
223
314
  let bestPrefix;
224
315
  let best;
225
316
  for (const [prefix, entries] of this.routes.peek() ?? []) {
226
- const entry = entries.find((candidate) => !refused?.has(candidate));
227
- if (!entry)
228
- continue;
229
317
  if (!Path.hasPrefix(prefix, path))
230
318
  continue;
319
+ const entry = preferredEntry(entries, skip);
320
+ if (!entry)
321
+ continue;
231
322
  if (bestPrefix === undefined || prefix.length > bestPrefix.length) {
232
323
  bestPrefix = prefix;
233
324
  best = entry;
@@ -236,18 +327,36 @@ class OriginState {
236
327
  return best;
237
328
  }
238
329
  /**
239
- * What `path` resolves to: a local publish, a broadcast materialized from the best
240
- * covering route, or the blind answer.
330
+ * Whether the announced local broadcast at `path` wins over `entry`, the best route a
331
+ * session or dynamic handle announced there. Cost decides, as for any two routes: an
332
+ * identified route strictly cheaper than the local one wins, and the local broadcast
333
+ * wins a tie. A route at a shorter prefix never competes, since the most specific
334
+ * prefix wins outright. False when nothing is announced locally at `path`.
335
+ */
336
+ localWins(path, entry) {
337
+ const local = this.advertisedLocal.peek()?.get(path);
338
+ if (!local || !this.local.peek()?.has(path))
339
+ return false;
340
+ if (!entry || !this.routes.peek()?.get(path)?.includes(entry))
341
+ return true;
342
+ return compareRoutes(local, entry.route.peek()) <= 0;
343
+ }
344
+ /**
345
+ * What `path` resolves to: an announced local publish, a broadcast materialized from
346
+ * the best covering route, or the blind answer.
241
347
  *
242
348
  * Materialization is lazy and cached per path: the first request under a route opens
243
349
  * the providing session's subscription, repeats share it, and a provider change (the
244
350
  * route retracting, a better session taking over) swaps it out.
245
351
  */
246
352
  route(path, slot) {
353
+ const entry = this.bestEntry(path, (candidate) => slot.refused.has(candidate));
247
354
  const local = this.local.peek()?.get(path);
248
- if (local)
355
+ if (local && this.localWins(path, entry)) {
356
+ // Nothing reads a remote front the local broadcast replaced, so close its session subscription.
357
+ this.releaseMaterialized(path);
249
358
  return local;
250
- const entry = this.bestEntry(path, slot.refused);
359
+ }
251
360
  const cached = this.materialized.get(path);
252
361
  if (cached && cached.entry === entry) {
253
362
  if (cached.front.closed.peek() === undefined)
@@ -280,7 +389,7 @@ class OriginState {
280
389
  *
281
390
  * Create, attach {@link dynamic} for tracks served on demand, populate, then
282
391
  * {@link broadcast.Producer.announce}: an exact-path subscribe before the tracks exist is
283
- * refused, and announcing advertises the path to peers.
392
+ * refused, and nobody can see or reach a broadcast until it announces.
284
393
  *
285
394
  * @public
286
395
  */
@@ -314,60 +423,63 @@ export class Producer {
314
423
  /**
315
424
  * Create a broadcast at `path`, returning its producer.
316
425
  *
317
- * The broadcast appears on this origin's local announce streams immediately.
318
- * Call {@link broadcast.Producer.announce} to advertise it to peers once its
319
- * tracks exist. Local consumers can discover and request it without that call.
426
+ * The broadcast exists for nobody until {@link broadcast.Producer.announce}: announce
427
+ * streams skip it and requests for its path find nothing, on this origin exactly as at
428
+ * a peer. Announce once its tracks exist; {@link broadcast.Producer.unannounce}
429
+ * withdraws it from everyone again.
320
430
  *
321
431
  * Close the producer to drop it. Creating a path again supersedes the previous
322
432
  * broadcast: the origin drops its handle on the old one, which closes it unless the
323
- * application still holds a consumer clone. A local broadcast also shadows any
324
- * remote broadcast at the same path.
433
+ * application still holds a consumer clone. An announced local broadcast competes with
434
+ * a remote route at the same path on cost, winning ties.
325
435
  */
326
436
  createBroadcast(path) {
437
+ const created = this.#state.created;
438
+ if (!created)
439
+ throw new Error("origin is closed");
327
440
  const producer = new broadcast.Producer();
328
441
  const front = producer.consume();
329
442
  hooks.attachAnnouncer(producer, {
330
443
  announce: (route) => this.#advertiseExact(path, front, route),
331
444
  unannounce: () => this.#retractExact(path, front),
332
445
  });
333
- this.#state.local.mutate((broadcasts) => {
334
- if (!broadcasts)
335
- throw new Error("origin is closed");
336
- broadcasts.get(path)?.close();
337
- broadcasts.set(path, front);
338
- });
339
- this.#state.advertisedLocal.mutate((advertised) => {
340
- advertised?.delete(path);
341
- });
342
- this.#state.rebuildOriginated();
343
- this.#state.refresh(path);
446
+ const previous = created.get(path);
447
+ created.set(path, front);
448
+ if (previous) {
449
+ this.#retractExact(path, previous);
450
+ previous.close();
451
+ }
344
452
  // Drop it when the broadcast closes, unless a recreate already replaced it: a
345
453
  // stale broadcast closing must not unpublish the live one.
346
454
  void front.closed.then(() => {
347
455
  this.#retractExact(path, front);
348
- this.#state.local.mutate((broadcasts) => {
349
- if (broadcasts?.get(path) === front)
350
- broadcasts.delete(path);
351
- });
352
- this.#state.refresh(path);
456
+ if (this.#state.created?.get(path) === front)
457
+ this.#state.created.delete(path);
353
458
  });
354
459
  return producer;
355
460
  }
356
461
  #advertiseExact(path, front, route) {
462
+ if (!this.#state.local.peek())
463
+ throw new Error("origin is closed");
464
+ if (this.#state.created?.get(path) !== front)
465
+ throw new Error("broadcast is closed");
466
+ // Both maps move together, so every reader sees the broadcast and its route at once.
467
+ this.#state.local.mutate((broadcasts) => {
468
+ broadcasts?.set(path, front);
469
+ });
357
470
  this.#state.advertisedLocal.mutate((advertised) => {
358
- if (!advertised)
359
- throw new Error("origin is closed");
360
- if (this.#state.local.peek()?.get(path) !== front)
361
- throw new Error("broadcast is closed");
362
- advertised.set(path, route);
471
+ advertised?.set(path, route);
363
472
  });
364
473
  this.#state.rebuildOriginated();
365
474
  this.#state.refresh(path);
366
475
  }
367
476
  #retractExact(path, front) {
477
+ if (this.#state.local.peek()?.get(path) !== front)
478
+ return;
479
+ this.#state.local.mutate((broadcasts) => {
480
+ broadcasts?.delete(path);
481
+ });
368
482
  this.#state.advertisedLocal.mutate((advertised) => {
369
- if (this.#state.local.peek()?.get(path) !== front)
370
- return;
371
483
  advertised?.delete(path);
372
484
  });
373
485
  this.#state.rebuildOriginated();
@@ -382,7 +494,7 @@ export class Producer {
382
494
  * consumers narrow with a {@link Path.Pattern} locally. The advertisement is
383
495
  * visible to {@link Consumer.announced} and forwarded by sessions for as long as
384
496
  * the returned {@link Dynamic} lives. A consumer resolving a path under it that
385
- * no local broadcast covers is handed to the handle as a {@link Request}.
497
+ * no announced local broadcast wins is handed to the handle as a {@link Request}.
386
498
  */
387
499
  dynamic(prefix, route = Route.default) {
388
500
  return this.#insertRoute(prefix, Route.normalize(route), true);
@@ -544,21 +656,24 @@ export class Producer {
544
656
  request(path, options) {
545
657
  return this.#reader.request(path, options);
546
658
  }
659
+ /** The available announcements under `scope`, as a live map; see {@link Consumer.broadcasts}. */
660
+ broadcasts(scope, options) {
661
+ return this.#reader.broadcasts(scope, options);
662
+ }
547
663
  /** The available broadcasts under `scope`, as a live stream; see {@link Consumer.announced}. */
548
- announced(scope) {
549
- return this.#reader.announced(scope);
664
+ announced(scope, options) {
665
+ return this.#reader.announced(scope, options);
550
666
  }
551
667
  /** Close the origin, every broadcast it still routes, and its announcement streams. Idempotent. */
552
668
  close(abort) {
553
669
  if (this.#state.closed.peek() !== undefined)
554
670
  return;
555
671
  this.#state.closed.set(abort ?? null);
556
- this.#state.local.update((broadcasts) => {
557
- for (const front of broadcasts?.values() ?? []) {
558
- front.close(abort);
559
- }
560
- return undefined;
561
- });
672
+ for (const front of this.#state.created?.values() ?? []) {
673
+ front.close(abort);
674
+ }
675
+ this.#state.created = undefined;
676
+ this.#state.local.update(() => undefined);
562
677
  this.#state.advertisedLocal.update(() => undefined);
563
678
  this.#state.routes.update((routes) => {
564
679
  for (const entries of routes?.values() ?? []) {
@@ -673,6 +788,7 @@ export class Consumer {
673
788
  get advertised() {
674
789
  return state.originated;
675
790
  },
791
+ local: (path) => this.#local(path),
676
792
  demand: (path) => this.#demand(path),
677
793
  });
678
794
  }
@@ -699,8 +815,8 @@ export class Consumer {
699
815
  // counts, avoiding a Computed's lifecycle.
700
816
  #discovery;
701
817
  /**
702
- * Whether the table routes `path`, by a local publish or an announced route
703
- * covering it.
818
+ * Whether the table routes `path`, by an announced local publish or an announced
819
+ * route covering it.
704
820
  *
705
821
  * Availability, not a handle: {@link request} is the only way to consume by path. A
706
822
  * request on a routed path resolves to that route and never to a blind answer, which is
@@ -717,9 +833,10 @@ export class Consumer {
717
833
  * Resolve `path`, optionally waiting for an announcement.
718
834
  *
719
835
  * The one way to consume by path. {@link Requesting.active} follows whatever the table
720
- * routes (a local publish, or any feeding session's announcement, swapping on a
721
- * republish); when nothing does, the request stands and whichever attached session
722
- * answers first provides a blind subscription instead, re-answered across reconnects.
836
+ * routes (an announced local publish, or any feeding session's announcement, swapping
837
+ * on a republish or a retraction); when nothing does, the request stands and whichever
838
+ * attached session answers first provides a blind subscription instead, re-answered
839
+ * across reconnects.
723
840
  * With `announced: true`, an unrouted request waits while discovery is supported and
724
841
  * falls back to that blind behavior only when discovery is unavailable. Close the request
725
842
  * when done. On a closed origin it never resolves.
@@ -840,20 +957,59 @@ export class Consumer {
840
957
  });
841
958
  });
842
959
  }
960
+ /**
961
+ * The announced routes matching `scope`, as a live map from covered prefix to route.
962
+ * Local broadcasts appear once announced; received and dynamic routes retain their
963
+ * advertised prefixes. Reads are synchronous, and the getter needs no teardown.
964
+ * Hidden routes are left out unless `options.hidden` opts in (see {@link announce.Options}).
965
+ * Unscoped readers share one snapshot; each distinct scope filters the table on changes.
966
+ */
967
+ broadcasts(scope, options) {
968
+ const hidden = options?.hidden ?? false;
969
+ if (!scope)
970
+ return hidden ? this.#state.available : this.#state.visible;
971
+ return new Derived([this.#state.available], () => {
972
+ const routes = new Map();
973
+ for (const [path, entry] of this.#listed(scope, hidden))
974
+ routes.set(path, entry.route);
975
+ return routes;
976
+ });
977
+ }
843
978
  /**
844
979
  * The announced routes matching `scope`, as a live stream: every currently advertised
845
980
  * route arrives first as active, then additions and retractions as they happen.
846
- * Any pattern is accepted. A local broadcast appears when it is created;
847
- * {@link broadcast.Producer.announce} only forwards it to peers. A dynamic or
848
- * received route announces the prefix it covers when its subtree overlaps the scope. The
849
- * stream ends when the origin closes or the consumer is closed.
981
+ * Any pattern is accepted. A local broadcast appears once it announces, exactly as a
982
+ * peer sees it. A dynamic or received route announces the prefix it covers when its
983
+ * subtree overlaps the scope. The stream ends when the origin closes or the consumer is
984
+ * closed. Hidden routes are left out unless `options.hidden` opts in (see {@link announce.Options}).
850
985
  */
851
- announced(scope = Path.Pattern.all()) {
986
+ announced(scope = Path.Pattern.all(), options) {
852
987
  const producer = new announce.Producer();
853
- void this.#runAnnounced(producer, scope);
988
+ void this.#runAnnounced(producer, scope, options?.hidden ?? false);
854
989
  return producer.consume();
855
990
  }
856
- async #runAnnounced(producer, scope) {
991
+ /** One snapshot shared by map readers and announcement-stream diffing. */
992
+ #listed(scope, hidden) {
993
+ const next = new Map();
994
+ const { remote, local } = this.#state.snapshot();
995
+ const head = scopeHead(scope);
996
+ for (const [path, entry] of remote) {
997
+ if (!scopeOverlaps(scope, path))
998
+ continue;
999
+ if (!hidden && hiddenBelow(head, path))
1000
+ continue;
1001
+ next.set(path, { identity: entry.identity, route: entry.route, captures: scopeCaptures(scope, path) });
1002
+ }
1003
+ for (const [path, entry] of local) {
1004
+ if (!scope.matches(path))
1005
+ continue;
1006
+ if (!hidden && hiddenBelow(head, path))
1007
+ continue;
1008
+ next.set(path, { identity: entry.identity, route: entry.route, captures: scopeCaptures(scope, path) });
1009
+ }
1010
+ return next;
1011
+ }
1012
+ async #runAnnounced(producer, scope, hidden) {
857
1013
  // Keyed by the presented path (from the origin, not the scope), valued by identity
858
1014
  // plus route. Diffing identity rather than mere presence means a republish emits a
859
1015
  // retraction then a fresh announcement; a re-price of the same identity emits an
@@ -866,28 +1022,7 @@ export class Consumer {
866
1022
  const routes = this.#state.routes.peek();
867
1023
  if (local === undefined && advertisedLocal === undefined && routes === undefined)
868
1024
  break;
869
- const next = new Map();
870
- // Routes first, so an advertised local at the same path overwrites it: the
871
- // announcement points at whatever request() would resolve. A route remains
872
- // the prefix it advertised; the pattern is a local filter, not a new claim.
873
- for (const [covered, entries] of routes ?? []) {
874
- const entry = entries[0];
875
- if (!entry)
876
- continue;
877
- if (!scopeOverlaps(scope, covered))
878
- continue;
879
- const snap = {
880
- identity: entry.identity,
881
- route: entry.route.peek(),
882
- captures: scopeCaptures(scope, covered),
883
- };
884
- next.set(covered, snap);
885
- }
886
- for (const [path, front] of local ?? []) {
887
- const route = advertisedLocal?.get(path) ?? Route.default;
888
- if (scope.matches(path))
889
- next.set(path, { identity: front, route, captures: scopeCaptures(scope, path) });
890
- }
1025
+ const next = this.#listed(scope, hidden);
891
1026
  for (const [path, snap] of active) {
892
1027
  const cur = next.get(path);
893
1028
  if (!cur || cur.identity !== snap.identity)
@@ -939,17 +1074,27 @@ export class Consumer {
939
1074
  * @internal
940
1075
  */
941
1076
  /**
942
- * Resolve `path` for serving: a local broadcast, or wait for an originated dynamic
943
- * to accept it. Undefined when nothing here can serve the path.
1077
+ * The announced local broadcast at `path`, when it beats the originated routes there.
1078
+ * Resolves through what rebuildOriginated advertised: a peer never sees received routes.
1079
+ */
1080
+ #local(path) {
1081
+ const local = this.#state.local.peek()?.get(path);
1082
+ if (local && this.#state.localWins(path, this.#state.bestEntry(path, received)))
1083
+ return local;
1084
+ return undefined;
1085
+ }
1086
+ /**
1087
+ * Resolve `path` for serving: an announced local broadcast, or wait for an originated
1088
+ * dynamic to accept it. Undefined when nothing here can serve the path.
944
1089
  *
945
1090
  * @internal
946
1091
  */
947
1092
  async #demand(path) {
948
- const local = this.#state.local.peek()?.get(path);
1093
+ const local = this.#local(path);
949
1094
  if (local)
950
1095
  return local;
951
- const entry = this.#state.bestEntry(path);
952
- if (!entry?.originated || !entry.server)
1096
+ const entry = this.#state.bestEntry(path, received);
1097
+ if (!entry?.server)
953
1098
  return undefined;
954
1099
  const server = entry.server;
955
1100
  const live = server.served.get(path);