@moq/net 0.3.1 → 0.3.3

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 (100) hide show
  1. package/broadcast.d.ts.map +1 -1
  2. package/broadcast.js +3 -2
  3. package/broadcast.js.map +1 -1
  4. package/connection/accept.js +2 -1
  5. package/connection/accept.js.map +1 -1
  6. package/connection/browser.d.ts.map +1 -1
  7. package/connection/browser.js +15 -12
  8. package/connection/browser.js.map +1 -1
  9. package/connection/connect.d.ts.map +1 -1
  10. package/connection/connect.js +2 -1
  11. package/connection/connect.js.map +1 -1
  12. package/connection/handshake.d.ts +7 -4
  13. package/connection/handshake.d.ts.map +1 -1
  14. package/connection/handshake.js +17 -5
  15. package/connection/handshake.js.map +1 -1
  16. package/error.d.ts +14 -0
  17. package/error.d.ts.map +1 -1
  18. package/error.js +15 -0
  19. package/error.js.map +1 -1
  20. package/ietf/aliases.d.ts +73 -5
  21. package/ietf/aliases.d.ts.map +1 -1
  22. package/ietf/aliases.js +121 -14
  23. package/ietf/aliases.js.map +1 -1
  24. package/ietf/cluster.d.ts +141 -0
  25. package/ietf/cluster.d.ts.map +1 -0
  26. package/ietf/cluster.js +204 -0
  27. package/ietf/cluster.js.map +1 -0
  28. package/ietf/connection.d.ts +8 -1
  29. package/ietf/connection.d.ts.map +1 -1
  30. package/ietf/connection.js +19 -4
  31. package/ietf/connection.js.map +1 -1
  32. package/ietf/index.d.ts +1 -0
  33. package/ietf/index.d.ts.map +1 -1
  34. package/ietf/index.js +2 -0
  35. package/ietf/index.js.map +1 -1
  36. package/ietf/parameters.d.ts +11 -0
  37. package/ietf/parameters.d.ts.map +1 -1
  38. package/ietf/parameters.js +25 -1
  39. package/ietf/parameters.js.map +1 -1
  40. package/ietf/publish_namespace.d.ts +16 -2
  41. package/ietf/publish_namespace.d.ts.map +1 -1
  42. package/ietf/publish_namespace.js +25 -6
  43. package/ietf/publish_namespace.js.map +1 -1
  44. package/ietf/publisher.d.ts +11 -4
  45. package/ietf/publisher.d.ts.map +1 -1
  46. package/ietf/publisher.js +13 -8
  47. package/ietf/publisher.js.map +1 -1
  48. package/ietf/subscribe_namespace.d.ts +21 -3
  49. package/ietf/subscribe_namespace.d.ts.map +1 -1
  50. package/ietf/subscribe_namespace.js +31 -9
  51. package/ietf/subscribe_namespace.js.map +1 -1
  52. package/ietf/subscriber.d.ts +8 -3
  53. package/ietf/subscriber.d.ts.map +1 -1
  54. package/ietf/subscriber.js +219 -40
  55. package/ietf/subscriber.js.map +1 -1
  56. package/internal.d.ts +16 -1
  57. package/internal.d.ts.map +1 -1
  58. package/internal.js.map +1 -1
  59. package/lite/announce.d.ts +1 -2
  60. package/lite/announce.d.ts.map +1 -1
  61. package/lite/announce.js +4 -9
  62. package/lite/announce.js.map +1 -1
  63. package/lite/connection.d.ts +18 -2
  64. package/lite/connection.d.ts.map +1 -1
  65. package/lite/connection.js +36 -5
  66. package/lite/connection.js.map +1 -1
  67. package/lite/probe.d.ts +18 -2
  68. package/lite/probe.d.ts.map +1 -1
  69. package/lite/probe.js +24 -22
  70. package/lite/probe.js.map +1 -1
  71. package/lite/publisher.d.ts +1 -1
  72. package/lite/publisher.d.ts.map +1 -1
  73. package/lite/publisher.js +47 -15
  74. package/lite/publisher.js.map +1 -1
  75. package/lite/setup.d.ts +1 -1
  76. package/lite/setup.d.ts.map +1 -1
  77. package/lite/setup.js +1 -1
  78. package/lite/setup.js.map +1 -1
  79. package/lite/subscriber.d.ts +1 -1
  80. package/lite/subscriber.d.ts.map +1 -1
  81. package/lite/subscriber.js +10 -5
  82. package/lite/subscriber.js.map +1 -1
  83. package/lite/version.d.ts +7 -0
  84. package/lite/version.d.ts.map +1 -1
  85. package/lite/version.js +17 -0
  86. package/lite/version.js.map +1 -1
  87. package/{lite/origin.d.ts → origin.d.ts} +24 -0
  88. package/origin.d.ts.map +1 -0
  89. package/{lite/origin.js → origin.js} +24 -0
  90. package/origin.js.map +1 -0
  91. package/package.json +3 -2
  92. package/path.d.ts +29 -0
  93. package/path.d.ts.map +1 -1
  94. package/path.js +51 -0
  95. package/path.js.map +1 -1
  96. package/track.d.ts.map +1 -1
  97. package/track.js +35 -14
  98. package/track.js.map +1 -1
  99. package/lite/origin.d.ts.map +0 -1
  100. package/lite/origin.js.map +0 -1
@@ -2,16 +2,17 @@
2
2
  import * as announce from "../announced.js";
3
3
  import * as broadcast from "../broadcast.js";
4
4
  import { BroadcastCache } from "../consume.js";
5
- import { error, reason } from "../error.js";
5
+ import { error, ProtocolViolation, reason } from "../error.js";
6
6
  import * as netGroup from "../group.js";
7
7
  import * as Path from "../path.js";
8
8
  import { Timestamp } from "../time.js";
9
9
  import { withTimeout } from "../util/timeout.js";
10
- import { TrackAliases } from "./aliases.js";
10
+ import { DuplicateTrackAlias, RetiredTrackAlias, TrackAliases } from "./aliases.js";
11
+ import * as Cluster from "./cluster.js";
11
12
  import { Frame } from "./object.js";
12
13
  import { toWire } from "./priority.js";
13
14
  import { PublishError } from "./publish.js";
14
- import { PublishNamespaceError, PublishNamespaceOk } from "./publish_namespace.js";
15
+ import { PublishNamespace, PublishNamespaceDone, PublishNamespaceError, PublishNamespaceOk, } from "./publish_namespace.js";
15
16
  import { RequestError, RequestOk } from "./request.js";
16
17
  import { Subscribe, SubscribeError, SubscribeOk, Unsubscribe } from "./subscribe.js";
17
18
  import { PublishBlocked, SubscribeNamespace, SubscribeNamespaceEntry, SubscribeNamespaceEntryDone, SubscribeNamespaceLegacy, SubscribeNamespaceOk, UnsubscribeNamespace, } from "./subscribe_namespace.js";
@@ -28,6 +29,10 @@ const SUBSCRIBE_OK_TIMEOUT_MS = 10_000;
28
29
  */
29
30
  export class Subscriber {
30
31
  #session;
32
+ // The Hop IDs this session declared; see {@link Cluster}. What the peer declared is what
33
+ // says whether an advertisement carries a hop path, and ours is what a path looping back
34
+ // to us contains.
35
+ #cluster;
31
36
  // Publisher-chosen aliases used by incoming group streams.
32
37
  #aliases = new TrackAliases();
33
38
  // Units for each track's object Timestamps, from the TIMESCALE Track Property in
@@ -52,12 +57,23 @@ export class Subscriber {
52
57
  #announcedConsumers = new Set();
53
58
  /**
54
59
  * Creates a new Subscriber instance.
55
- * @param session - The session abstraction for bidi streams and request IDs
56
60
  *
57
61
  * @internal
58
62
  */
59
- constructor(session) {
63
+ constructor({ session, cluster, }) {
60
64
  this.#session = session;
65
+ this.#cluster = cluster;
66
+ }
67
+ /**
68
+ * Whether an advertisement is ours coming back: its hop path already ran through us, so
69
+ * subscribing via it would route us back to ourselves.
70
+ *
71
+ * A conforming peer withholds these (it knows our Hop ID), so this is the backstop that
72
+ * keeps a mesh working when one member does not. A session that negotiated nothing
73
+ * carries no path, and there is nothing to check.
74
+ */
75
+ #reflected(advert) {
76
+ return advert !== undefined && this.#cluster !== undefined && Cluster.loops(advert, this.#cluster.self);
61
77
  }
62
78
  /**
63
79
  * Gets an announced reader for the specified prefix.
@@ -183,10 +199,19 @@ export class Subscriber {
183
199
  break;
184
200
  const msgType = await stream.reader.u53();
185
201
  if (msgType === SubscribeNamespaceEntry.id) {
186
- const entry = await SubscribeNamespaceEntry.decode(stream.reader, version);
202
+ const entry = await SubscribeNamespaceEntry.decode(stream.reader, version, Cluster.negotiated(this.#cluster));
187
203
  if (released)
188
204
  break;
189
205
  const path = Path.join(prefix, entry.suffix);
206
+ // A repeat updates the advertisement in place, so one that now
207
+ // loops back through us has taken a route we can't subscribe
208
+ // over: the path is gone even though the message says active.
209
+ if (this.#reflected(entry.cluster)) {
210
+ console.debug(`dropping reflected namespace: broadcast=${path}`);
211
+ if (live.delete(path))
212
+ this.#detachAnnounce(path);
213
+ continue;
214
+ }
190
215
  // A repeat updates the advertisement; only the first is news.
191
216
  if (!live.has(path)) {
192
217
  live.add(path);
@@ -211,6 +236,15 @@ export class Subscriber {
211
236
  }
212
237
  }
213
238
  })();
239
+ // The race below can end through the consumer's close while a message that
240
+ // already arrived is still decoding, which leaves the loop with nothing
241
+ // awaiting it: a violation decoded after that would land nowhere, and its
242
+ // rejection would go unhandled. Idempotent with the catch below, which is
243
+ // what runs when the loop is the one that ends the race.
244
+ readLoop.catch((err) => {
245
+ if (err instanceof ProtocolViolation)
246
+ this.#session.close();
247
+ });
214
248
  // Wait for either the read loop or the announced to close
215
249
  await Promise.race([readLoop, announced.closed]);
216
250
  // For v14/v15: send UnsubscribeNamespace before closing
@@ -234,6 +268,12 @@ export class Subscriber {
234
268
  catch (err) {
235
269
  const e = error(err);
236
270
  console.warn(`subscribe_namespace error: ${reason(e)}`);
271
+ // An advertisement is decoded here rather than in the session dispatch, so this
272
+ // is the only place a malformed one surfaces. The cluster draft requires closing
273
+ // the session over those, and the stream alone is not enough: the peer would
274
+ // just repeat it on the next SUBSCRIBE_NAMESPACE.
275
+ if (e instanceof ProtocolViolation)
276
+ this.#session.close();
237
277
  // Abort the stream rather than letting the caller's `finally` close it cleanly.
238
278
  // A rejected namespace subscription is a failure, and a consumer that can't tell
239
279
  // it from "nothing is published under this prefix" waits forever on a broadcast
@@ -277,7 +317,6 @@ export class Subscriber {
277
317
  return consumer;
278
318
  }
279
319
  async #runSubscribe(broadcast, request) {
280
- const version = this.#session.version;
281
320
  const requestId = await this.#session.nextRequestId();
282
321
  if (requestId === undefined) {
283
322
  request.reject(new Error("session closed"));
@@ -295,10 +334,25 @@ export class Subscriber {
295
334
  // and any registration if setup eventually finishes.
296
335
  const state = {};
297
336
  const setup = this.#openSubscribe(state, broadcast, request, producer, requestId);
337
+ // The publisher can be serving before it answers, so waiting only on the response
338
+ // would miss the local side going away and leave it serving a track nobody reads.
339
+ // Demand returning before we commit is not abandonment, matching the serving loop.
340
+ const waitAbandoned = async () => {
341
+ for (;;) {
342
+ await producer.unused();
343
+ if (producer.closed.peek() !== undefined || !producer.used.peek())
344
+ return null;
345
+ }
346
+ };
298
347
  let stream;
299
348
  let trackAlias;
300
349
  try {
301
- const result = await withTimeout(setup, SUBSCRIBE_OK_TIMEOUT_MS, `subscribe timed out after ${SUBSCRIBE_OK_TIMEOUT_MS}ms waiting for SUBSCRIBE_OK (browser stream limit reached?)`);
350
+ const result = await Promise.race([
351
+ withTimeout(setup, SUBSCRIBE_OK_TIMEOUT_MS, `subscribe timed out after ${SUBSCRIBE_OK_TIMEOUT_MS}ms waiting for SUBSCRIBE_OK (browser stream limit reached?)`),
352
+ waitAbandoned(),
353
+ ]);
354
+ if (result === null)
355
+ throw new Error("subscribe abandoned before it was accepted");
302
356
  stream = result.stream;
303
357
  trackAlias = result.alias;
304
358
  console.debug(`subscribe ok: id=${requestId} broadcast=${broadcast} track=${request.name}`);
@@ -307,43 +361,68 @@ export class Subscriber {
307
361
  const e = error(err);
308
362
  producer.close(e);
309
363
  console.warn(`subscribe error: id=${requestId} broadcast=${broadcast} track=${request.name} error=${reason(e)}`);
310
- // If setup eventually settles after the timeout, abort the stream
311
- // and drop any registration so we don't leak. Cover both branches:
312
- // setup may resolve late, or reject (e.g. SUBSCRIBE error) after the
313
- // stream is already open.
314
- const cleanup = () => {
315
- if (state.registeredAlias !== undefined)
316
- this.#aliases.delete(state.registeredAlias, producer);
317
- state.stream?.abort(e);
364
+ // Runs now for whatever is already open, and again if `setup` settles late.
365
+ // Retirement is repeated because a late SUBSCRIBE_OK can register an alias after
366
+ // the first pass; the stream is torn down once.
367
+ let torn = false;
368
+ const cleanup = async (afterSetup) => {
369
+ state.cancelled = true;
370
+ if (state.registeredAlias !== undefined && this.#aliases.retire(state.registeredAlias, producer)) {
371
+ this.#timescales.delete(state.registeredAlias);
372
+ }
373
+ if (!state.stream || torn)
374
+ return;
375
+ // A SUBSCRIBE still being written must not be torn down from here. Aborting a
376
+ // Web Stream waits for the in-flight write, so the request can still reach the
377
+ // peer while the abort destroys the stream we would have cancelled on. Setup
378
+ // unwinds on the cancelled flag once that write lands, and its pass below has
379
+ // the finished picture.
380
+ if (!state.sent && !afterSetup)
381
+ return;
382
+ torn = true;
383
+ // Once the SUBSCRIBE is out the publisher may be serving, whether or not it has
384
+ // answered yet: data streams are independent of the request stream. Aborting says
385
+ // nothing on v14-16, where the request rides a virtual stream whose reset is
386
+ // local, so the UNSUBSCRIBE has to go out explicitly.
387
+ if (state.sent && !state.rejected)
388
+ await this.#cancelSubscribe(state.stream, requestId);
389
+ state.stream.abort(e);
318
390
  };
319
- setup.then(cleanup, cleanup);
391
+ // Tear down what is already open rather than waiting on `setup`. A peer that
392
+ // opened the stream and then went quiet never settles it, and deferring would
393
+ // leave the request outstanding for the life of the session -- which is the
394
+ // timeout case, not a rare one.
395
+ void cleanup(false);
396
+ setup.then(() => cleanup(true), () => cleanup(true));
320
397
  return;
321
398
  }
322
399
  try {
400
+ // Which terminal fired decides whether we owe the publisher a cancellation, so
401
+ // tag them rather than racing bare promises.
402
+ const publisherEnded = Symbol("publisher");
403
+ const localEnded = Symbol("local");
404
+ const idle = Symbol("idle");
323
405
  // Terminal conditions settle at most once (stream close = PublishDone, track close =
324
406
  // local unsubscribe); race them once so the demand loop doesn't re-subscribe each pass.
325
- const done = Promise.race([stream.reader.closed, producer.closed]);
407
+ const done = Promise.race([
408
+ stream.reader.closed.then(() => publisherEnded),
409
+ producer.closed.then(() => localEnded),
410
+ ]);
326
411
  // Serve until a terminal condition fires or the last local subscriber leaves. The unused
327
412
  // wake is level-triggered: re-check demand so a subscriber that returns before we tear
328
413
  // down resumes on the same stream.
329
- const idle = Symbol("idle");
414
+ let terminal = localEnded;
330
415
  for (;;) {
331
416
  const reason = await Promise.race([done, producer.unused().then(() => idle)]);
332
417
  if (reason === idle && producer.closed.peek() === undefined && producer.used.peek())
333
418
  continue;
419
+ terminal = reason;
334
420
  break;
335
421
  }
336
- // For v14-v16: send Unsubscribe before closing (removed in v17+)
337
- if (version === Version.DRAFT_14 || version === Version.DRAFT_15 || version === Version.DRAFT_16) {
338
- try {
339
- await stream.writer.u53(Unsubscribe.id);
340
- const unsub = new Unsubscribe({ requestId });
341
- await unsub.encode(stream.writer, version);
342
- }
343
- catch {
344
- // Stream might already be closed
345
- }
346
- }
422
+ // The publisher already ended the request, so there is nothing to cancel. Sending
423
+ // UNSUBSCRIBE here would name a request it has already torn down.
424
+ if (terminal !== publisherEnded)
425
+ await this.#cancelSubscribe(stream, requestId);
347
426
  producer.close();
348
427
  stream.close();
349
428
  console.debug(`subscribe close: id=${requestId} broadcast=${broadcast} track=${request.name}`);
@@ -355,8 +434,31 @@ export class Subscriber {
355
434
  console.warn(`subscribe error: id=${requestId} broadcast=${broadcast} track=${request.name} error=${reason(e)}`);
356
435
  }
357
436
  finally {
358
- this.#aliases.delete(trackAlias, producer);
359
- this.#timescales.delete(trackAlias);
437
+ // Only the owner tears down the alias metadata: a later subscription may have
438
+ // reclaimed the alias and installed its own timescale.
439
+ if (this.#aliases.retire(trackAlias, producer))
440
+ this.#timescales.delete(trackAlias);
441
+ }
442
+ }
443
+ /**
444
+ * Tell the publisher to stop serving a subscription we are walking away from.
445
+ *
446
+ * v14-16 cancel with UNSUBSCRIBE (draft-16 section 9.12), which is what lets the
447
+ * publisher destroy the subscription (section 5.1.1); v17+ removed the message and
448
+ * rely on the stream reset instead. Every path that abandons an Established
449
+ * subscription goes through here, because those versions carry the request over a
450
+ * virtual stream whose reset never reaches the peer.
451
+ */
452
+ async #cancelSubscribe(stream, requestId) {
453
+ const version = this.#session.version;
454
+ if (version !== Version.DRAFT_14 && version !== Version.DRAFT_15 && version !== Version.DRAFT_16)
455
+ return;
456
+ try {
457
+ await stream.writer.u53(Unsubscribe.id);
458
+ await new Unsubscribe({ requestId }).encode(stream.writer, version);
459
+ }
460
+ catch {
461
+ // The stream may already be gone; there is nothing further to tell the peer.
360
462
  }
361
463
  }
362
464
  // Opens the subscribe stream, sends SUBSCRIBE, and reads the response.
@@ -366,6 +468,11 @@ export class Subscriber {
366
468
  async #openSubscribe(state, broadcast, request, producer, requestId) {
367
469
  const version = this.#session.version;
368
470
  state.stream = await this.#session.openBi();
471
+ // The timeout can fire while the open is still in flight, in which case cleanup ran
472
+ // with nothing to release. Bail now that the stream is recorded, so settling this
473
+ // promise hands it back to be torn down.
474
+ if (state.cancelled)
475
+ throw new Error("subscribe cancelled before it was sent");
369
476
  await state.stream.writer.u53(Subscribe.id);
370
477
  const msg = new Subscribe({
371
478
  requestId,
@@ -374,6 +481,12 @@ export class Subscriber {
374
481
  subscriberPriority: toWire(request.priority),
375
482
  });
376
483
  await msg.encode(state.stream.writer, version);
484
+ state.sent = true;
485
+ // The caller may have given up while that write was in flight. It deliberately left
486
+ // the stream alone, so unwind here and let its cleanup send the cancellation now that
487
+ // the SUBSCRIBE has actually reached the peer.
488
+ if (state.cancelled)
489
+ throw new Error("subscribe cancelled while it was being sent");
377
490
  console.debug(`subscribe written: id=${requestId} broadcast=${broadcast} track=${request.name}`);
378
491
  const respTypeId = await state.stream.reader.u53();
379
492
  if (respTypeId !== SubscribeOk.id) {
@@ -389,17 +502,22 @@ export class Subscriber {
389
502
  catch {
390
503
  // Decoding error response failed, use default message
391
504
  }
505
+ state.rejected = true;
392
506
  throw new Error(`SUBSCRIBE error: ${reasonPhrase}`);
393
507
  }
394
508
  const ok = await SubscribeOk.decode(state.stream.reader, version);
395
509
  try {
396
- this.#aliases.set(ok.trackAlias, producer);
510
+ this.#aliases.set(ok.trackAlias, producer, { broadcast, name: request.name });
397
511
  if (ok.timescale !== undefined) {
398
512
  this.#timescales.set(ok.trackAlias, ok.timescale);
399
513
  }
400
514
  }
401
515
  catch (err) {
402
- this.#session.close();
516
+ // Only one alias naming two different tracks is the session's problem. A publisher
517
+ // sharing an alias between subscriptions to one track is allowed to do that, and
518
+ // disconnecting over it would drop every other broadcast on the session.
519
+ if (err instanceof DuplicateTrackAlias)
520
+ this.#session.close();
403
521
  throw err;
404
522
  }
405
523
  state.registeredAlias = ok.trackAlias;
@@ -414,6 +532,20 @@ export class Subscriber {
414
532
  async runPublishNamespace(msg, stream) {
415
533
  const version = this.#session.version;
416
534
  const path = msg.trackNamespace;
535
+ // A path that already ran through us looped back. Refuse it rather than holding an
536
+ // advertisement we could never subscribe through. The peer knows our Hop ID, so a
537
+ // conforming one never offers it; 0 as the retry interval says not to come back.
538
+ if (this.#reflected(msg.cluster)) {
539
+ console.debug(`dropping reflected publish_namespace: broadcast=${path}`);
540
+ await stream.writer.u53(RequestError.id);
541
+ await new RequestError({
542
+ requestId: msg.requestId,
543
+ errorCode: 400,
544
+ reasonPhrase: "route loops back",
545
+ }).encode(stream.writer, version);
546
+ stream.close();
547
+ return;
548
+ }
417
549
  // Draft-14/15 key their namespace-scoped messages by name, not request ID, so the
418
550
  // adapter can hold only one request per namespace: a second would overwrite the
419
551
  // first, and the withdrawals would then close the wrong stream and fail to find
@@ -469,10 +601,45 @@ export class Subscriber {
469
601
  // consumer reacting with a SUBSCRIBE must not interleave with that OK.
470
602
  attached = true;
471
603
  this.#attachAnnounce(path);
472
- // Wait for stream close (= PublishNamespaceDone)
473
- console.debug(`runPublishNamespace: awaiting stream.reader.closed for ${path}`);
474
- await stream.reader.closed;
475
- console.debug(`runPublishNamespace: stream.reader.closed resolved for ${path}`);
604
+ // An advertisement is updated in place, by repeating PUBLISH_NAMESPACE on the
605
+ // stream that already carries it, so read until the stream ends rather than
606
+ // waiting on the close. Nothing else would deliver a re-parented route.
607
+ const done = version === Version.DRAFT_16 || legacy;
608
+ for (;;) {
609
+ if (await stream.reader.done())
610
+ break;
611
+ const typeId = await stream.reader.u53();
612
+ if (done && typeId === PublishNamespaceDone.id) {
613
+ await PublishNamespaceDone.decode(stream.reader, version);
614
+ break;
615
+ }
616
+ if (typeId !== PublishNamespace.id) {
617
+ throw new ProtocolViolation(`unexpected message on publish_namespace stream: 0x${typeId.toString(16)}`);
618
+ }
619
+ const update = await PublishNamespace.decode(stream.reader, version, Cluster.negotiated(this.#cluster));
620
+ // The stream is the advertisement, so an update on it must name the same
621
+ // one. Applying a mismatched update would retarget this path with metadata
622
+ // meant for a different request.
623
+ if (update.requestId !== msg.requestId || update.trackNamespace !== path) {
624
+ throw new ProtocolViolation("publish_namespace update does not match its stream");
625
+ }
626
+ // A path that now runs through us is unusable, so give it back. Keep
627
+ // reading: this stream is the advertisement's only channel, so a later
628
+ // clean path arrives here or nowhere.
629
+ if (this.#reflected(update.cluster)) {
630
+ if (attached) {
631
+ attached = false;
632
+ console.debug(`publish_namespace now loops back, detaching: broadcast=${path}`);
633
+ this.#detachAnnounce(path);
634
+ }
635
+ continue;
636
+ }
637
+ // Re-attach: a clean path replaced the reflected one we detached from.
638
+ if (!attached) {
639
+ attached = true;
640
+ this.#attachAnnounce(path);
641
+ }
642
+ }
476
643
  }
477
644
  finally {
478
645
  if (legacy)
@@ -492,11 +659,17 @@ export class Subscriber {
492
659
  */
493
660
  async runPublish(msg, stream) {
494
661
  const version = this.#session.version;
662
+ // NOT_SUPPORTED, from the PUBLISH error codes in draft-19 section 10.10. We decline
663
+ // the method itself rather than this particular track, which is UNINTERESTED (0x4).
664
+ //
665
+ // The alias the message carries is deliberately not recorded. Nothing will ever bind
666
+ // it, and a rejected request has no lifetime of ours to hang the cleanup on.
667
+ const NOT_SUPPORTED = 0x3;
495
668
  if (version === Version.DRAFT_14) {
496
669
  await stream.writer.u53(PublishError.id);
497
670
  const err = new PublishError({
498
671
  requestId: msg.requestId,
499
- errorCode: 500,
672
+ errorCode: NOT_SUPPORTED,
500
673
  reasonPhrase: "publish not supported",
501
674
  });
502
675
  await err.encode(stream.writer, version);
@@ -505,7 +678,7 @@ export class Subscriber {
505
678
  await stream.writer.u53(RequestError.id);
506
679
  const err = new RequestError({
507
680
  requestId: version === Version.DRAFT_15 || version === Version.DRAFT_16 ? msg.requestId : undefined,
508
- errorCode: 500,
681
+ errorCode: NOT_SUPPORTED,
509
682
  reasonPhrase: "publish not supported",
510
683
  });
511
684
  await err.encode(stream.writer, version);
@@ -539,6 +712,12 @@ export class Subscriber {
539
712
  }
540
713
  catch (err) {
541
714
  const e = error(err);
715
+ // Ours: we cancelled the subscription and the publisher has not stopped yet.
716
+ // Anything else on this alias is the publisher sending data for a track it never
717
+ // acknowledged, which is worth seeing.
718
+ if (e instanceof RetiredTrackAlias) {
719
+ console.debug(`dropping group for a cancelled subscription: alias=${group.trackAlias}`);
720
+ }
542
721
  producer.close(e);
543
722
  stream.stop(e);
544
723
  }