@moq/net 0.3.2 → 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.
@@ -1 +1 @@
1
- {"version":3,"file":"broadcast.d.ts","sourceRoot":"","sources":["../src/broadcast.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,KAAK,UAAU,EAAgB,MAAM,cAAc,CAAC;AAC7D,OAAO,KAAK,EAAE,QAAQ,IAAI,aAAa,EAAE,MAAM,YAAY,CAAC;AAE5D,OAAO,KAAK,KAAK,MAAM,YAAY,CAAC;AAgIpC;;;;GAIG;AACH,qBAAa,QAAS,YAAW,KAAK,CAAC,SAAS;;IAG/C;;;OAGG;IACH,IAAI,MAAM,IAAI,UAAU,CAAC,KAAK,GAAG,IAAI,CAAC,CAErC;IAED,wCAAwC;IACxC,OAAO,IAAI,QAAQ,CAElB;IAED,iDAAiD;IAC3C,SAAS,IAAI,OAAO,CAAC,KAAK,CAAC,OAAO,GAAG,SAAS,CAAC,CAWpD;IAED,uFAAuF;IACvF,WAAW,CAAC,KAAK,EAAE,KAAK,CAAC,QAAQ,GAAG,IAAI,CAiBvC;IAED,6EAA6E;IAC7E,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,GAAE,OAAO,CAAC,KAAK,CAAC,IAAI,CAAM,GAAG,KAAK,CAAC,QAAQ,CAIxE;IAED,kDAAkD;IAClD,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAE9B;IAED,8EAA8E;IAC9E,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,KAAK,CAAC,YAAY,GAAG,KAAK,CAAC,UAAU,CAEtE;IAED,2EAA2E;IAC3E,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAElD;IAED,kFAAkF;IAClF,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,M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E,KAAK,QAElB;CACD;AAMD;;;;;;;GAOG;AACH,qBAAa,QAAS,YAAW,KAAK,CAAC,SAAS;;IAM/C,SAAS,aAAa,KAAK,CAAC,EAAE,KAAK,EAAE;IAUrC;;;;;;OAMG;IACH,IAAI,MAAM,IAAI,UAAU,CAAC,KAAK,GAAG,IAAI,CAAC,CAErC;IAED;;;;;;;OAOG;IACH,KAAK,IAAI,QAAQ,CAEhB;IAID,SAAS,CAAC,UAAU,IAAI,KAAK,CAE5B;IAED,mHAAmH;IACnH,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,KAAK,CAAC,QAAQ,CAElC;IAED,uJAAuJ;IACvJ,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,KAAK,CAAC,YAAY,GAAG,KAAK,CAAC,UAAU,CAEtE;IAED,iGAAiG;IAC3F,SAAS,IAAI,OAAO,CAAC,KAAK,CAAC,OAAO,GAAG,SAAS,CAAC,CAWpD;IAED;;;OAGG;IACH,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAElD;IAED;;;;OAIG;IACH,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,KAAK,CAAC,iBAAiB,GAAG,OAAO,CAAC,aAAa,CAAC,CAEpG;IAED;;;OAGG;IACH,KAAK,CAAC,KAAK,CAAC,EAAE,KAAK,QAKlB;CACD"}
package/broadcast.js CHANGED
@@ -12,6 +12,7 @@ class BroadcastState {
12
12
  requested = new Signal([]);
13
13
  closed = new Once();
14
14
  tracks = new Map();
15
+ sequences = new Map();
15
16
  // Live consumer handles sharing this state (see {@link Consumer.clone}). The broadcast
16
17
  // closes once the last one closes, so a shared consumer can be handed to several callers.
17
18
  consumers = 0;
@@ -66,7 +67,7 @@ function subscribe(state, name, options = {}, register = false) {
66
67
  });
67
68
  }
68
69
  state.requested.mutate((requested) => {
69
- requested.push(hooks.makeRequest(name, producer));
70
+ requested.push(hooks.makeRequest({ name, producer, sequences: state.sequences }));
70
71
  });
71
72
  return subscriber;
72
73
  }
@@ -80,7 +81,7 @@ async function resolveTrackInfo(state, name) {
80
81
  }
81
82
  const producer = new track.Producer(name);
82
83
  state.requested.mutate((requested) => {
83
- requested.push(hooks.makeRequest(name, producer));
84
+ requested.push(hooks.makeRequest({ name, producer, sequences: state.sequences }));
84
85
  });
85
86
  try {
86
87
  return await producer.info();
package/broadcast.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"broadcast.js","sourceRoot":"","sources":["../src/broadcast.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAmB,IAAI,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAE7D,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,KAAK,KAAK,MAAM,YAAY,CAAC;AAEpC,0EAA0E;AAC1E,MAAM,cAAc;IACnB,SAAS,GAAG,IAAI,MAAM,CAAkB,EAAE,CAAC,CAAC;IAC5C,MAAM,GAAG,IAAI,IAAI,EAAgB,CAAC;IAClC,MAAM,GAAG,IAAI,GAAG,EAA0B,CAAC;IAC3C,uFAAuF;IACvF,0FAA0F;IAC1F,SAAS,GAAG,CAAC,CAAC;CACd;AAED,SAAS,cAAc,CAAC,KAAqB;IAC5C,MAAM,SAAS,GAAG,KAAK,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC;IACzC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,GAAG,CAAC,CAAC,QAAQ,CAAC,CAAC;IAClD,OAAO,SAAS,CAAC,GAAG,EAAE,CAAC;AACxB,CAAC;AAED,+EAA+E;AAC/E,8EAA8E;AAC9E,6DAA6D;AAC7D,EAAE;AACF,sFAAsF;AACtF,+CAA+C;AAC/C,SAAS,UAAU,CAAC,KAAqB,EAAE,KAAa;IACvD,IAA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c,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;YAC5C,IAAI,OAAO;gBAAE,OAAO,OAAO,CAAC;YAE5B,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;YACzC,IAAI,MAAM,YAAY,KAAK;gBAAE,MAAM,MAAM,CAAC;YAC1C,IAAI,MAAM,KAAK,SAAS;gBAAE,OAAO,SAAS,CAAC;YAE3C,MAAM,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QAC9D,CAAC;IACF,CAAC;IAED;;;OAGG;IACH,gBAAgB,CAAC,IAAY;QAC5B,OAAO,gBAAgB,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IAC5C,CAAC;IAED;;;;OAIG;IACH,UAAU,CAAC,IAAY,EAAE,QAAgB,EAAE,OAAiC;QAC3E,OAAO,UAAU,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAC;IACzD,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,KAAa;QAClB,IAAI,IAAI,CAAC,OAAO;YAAE,OAAO;QACzB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QACpB,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC,SAAS,GAAG,CAAC;YAAE,OAAO;QACxC,UAAU,CAAC,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAChC,CAAC;CACD","sourcesContent":["/**\n * Broadcast role handles: a named collection of tracks produced by a publisher.\n *\n * @module\n */\nimport { type GetPromise, Once, Signal } from \"@moq/signals\";\nimport type { Consumer as GroupConsumer } from \"./group.ts\";\nimport { hooks } from \"./internal.ts\";\nimport * as track from \"./track.ts\";\n\n/** Reactive backing state shared by broadcast producers and consumers. */\nclass BroadcastState {\n\trequested = new Signal<track.Request[]>([]);\n\tclosed = new Once<Error | null>();\n\ttracks = new Map<string, track.Producer>();\n\t// Live consumer handles sharing this state (see {@link Consumer.clone}). The broadcast\n\t// closes once the last one closes, so a shared consumer can be handed to several callers.\n\tconsumers = 0;\n}\n\nfunction dequeueRequest(state: BroadcastState): track.Request | undefined {\n\tconst requested = state.requested.peek();\n\trequested.sort((a, b) => a.priority - b.priority);\n\treturn requested.pop();\n}\n\n// Close the broadcast and reject any requests still pending in the queue, so a\n// subscriber blocked on the track's info() or group reads is unblocked rather\n// than left waiting on a producer that will never be served.\n//\n// Once.set throws on a second settle, and the producer and each consumer handle close\n// independently, so this has to be idempotent.\nfunction closeState(state: BroadcastState, abort?: Error) {\n\tif (state.closed.peek() !== undefined) return;\n\tstate.closed.set(abort ?? null);\n\tstate.requested.mutate((requests) => {\n\t\tfor (const request of requests) request.reject(abort);\n\t\trequests.length = 0;\n\t});\n}\n\n// `register` is set on the subscribing (consumer) side: the fresh producer is cached in\n// `state.tracks` so repeat subscriptions to the same track fan out from one upstream subscription\n// instead of opening a new one, mirroring the Rust `broadcast::Consumer::track` weak-dedup. The\n// consumer wire watches the producer's demand ({@link track.Producer.used}) and tears the upstream\n// down once its last subscriber leaves, closing the producer, which evicts the cache entry below.\n// The publishing side leaves `register` false: `state.tracks` there holds only the tracks the app\n// inserted, and a dynamic serve stays one request per peer subscription.\nfunction subscribe(\n\tstate: BroadcastState,\n\tname: string,\n\toptions: track.Subscription = {},\n\tregister = false,\n): track.Subscriber {\n\tif (state.closed.peek() !== undefined) {\n\t\tthrow new Error(`broadcast is closed: ${state.closed.peek()}`);\n\t}\n\n\tconst existing = state.tracks.get(name);\n\tif (existing) {\n\t\tif (existing.closed.peek() === undefined) return existing.subscribe(options);\n\t\tstate.tracks.delete(name);\n\t}\n\n\tconst producer = new track.Producer(name);\n\tconst subscriber = producer.subscribe(options);\n\n\tif (register) {\n\t\tstate.tracks.set(name, producer);\n\t\t// Drop the cache entry once the upstream closes (the wire tears it down when its last\n\t\t// subscriber leaves), so a later subscribe re-opens it.\n\t\tvoid producer.closed.then(() => {\n\t\t\tif (state.tracks.get(name) === producer) state.tracks.delete(name);\n\t\t});\n\t}\n\n\tstate.requested.mutate((requested) => {\n\t\trequested.push(hooks.makeRequest(name, producer));\n\t});\n\n\treturn subscriber;\n}\n\nasync function resolveTrackInfo(state: BroadcastState, name: string): Promise<track.Info> {\n\tconst existing = state.tracks.get(name);\n\tif (existing && existing.closed.peek() === undefined) {\n\t\treturn existing.info();\n\t}\n\n\tif (state.closed.peek() !== undefined) {\n\t\treturn Promise.reject(new Error(`broadcast is closed: ${state.closed.peek()}`));\n\t}\n\n\tconst producer = new track.Producer(name);\n\tstate.requested.mutate((requested) => {\n\t\trequested.push(hooks.makeRequest(name, producer));\n\t});\n\n\ttry {\n\t\treturn await producer.info();\n\t} finally {\n\t\tproducer.close();\n\t}\n}\n\n// Serve a group from the local retained window by subscribing and scanning to the\n// requested sequence. The default for a produced broadcast; the consuming wire layer\n// overrides it to fetch over the network (or to reject when the transport has no FETCH).\nasync function fetchGroup(\n\tstate: BroadcastState,\n\tname: string,\n\tsequence: number,\n\toptions: track.FetchGroupOptions = {},\n): Promise<GroupConsumer> {\n\tconst subscriber = subscribe(state, name, { priority: options.priority });\n\ttry {\n\t\tfor (;;) {\n\t\t\tconst group = await subscriber.recvGroup();\n\t\t\tif (!group) throw new Error(`group not found: ${sequence}`);\n\t\t\tif (group.sequence === sequence) {\n\t\t\t\t// Close the subscription when the returned group finishes, not now: an\n\t\t\t\t// in-progress group must keep receiving frames for its lifetime (mirrors\n\t\t\t\t// Rust poll_fetch). Also fires if the caller closes the group early.\n\t\t\t\tvoid group.closed.then(() => subscriber.close());\n\t\t\t\treturn group;\n\t\t\t}\n\n\t\t\tgroup.close();\n\t\t\tif (group.sequence > sequence) throw new Error(`group not found: ${sequence}`);\n\t\t}\n\t} catch (err) {\n\t\tsubscriber.close();\n\t\tthrow err;\n\t}\n}\n\n/**\n * The write side of a broadcast.\n *\n * @public\n */\nexport class Producer implements track.Broadcast {\n\t#state = new BroadcastState();\n\n\t/**\n\t * Settles once the broadcast closes: `null` on a clean close, or the abort {@link Error}.\n\t * Peek it synchronously (`undefined` while open), observe it reactively, or `await` it.\n\t */\n\tget closed(): GetPromise<Error | null> {\n\t\treturn this.#state.closed;\n\t}\n\n\t/** A read handle for this broadcast. */\n\tconsume(): Consumer {\n\t\treturn makeConsumer(this.#state);\n\t}\n\n\t/** Return the next track requested by a peer. */\n\tasync requested(): Promise<track.Request | undefined> {\n\t\tfor (;;) {\n\t\t\tconst request = dequeueRequest(this.#state);\n\t\t\tif (request) return request;\n\n\t\t\tconst closed = this.#state.closed.peek();\n\t\t\tif (closed instanceof Error) throw closed;\n\t\t\tif (closed !== undefined) return undefined;\n\n\t\t\tawait Signal.race(this.#state.requested, this.#state.closed);\n\t\t}\n\t}\n\n\t/** Insert a track that is served directly, without an on-demand request round-trip. */\n\tinsertTrack(track: track.Producer): void {\n\t\tif (this.#state.closed.peek() !== undefined) {\n\t\t\tthrow new Error(`broadcast is closed: ${this.#state.closed.peek()}`);\n\t\t}\n\n\t\tconst existing = this.#state.tracks.get(track.name);\n\t\tif (existing && existing.closed.peek() === undefined) {\n\t\t\tthrow new Error(`duplicate track: ${track.name}`);\n\t\t}\n\n\t\tthis.#state.tracks.set(track.name, track);\n\n\t\tvoid track.closed.then(() => {\n\t\t\tif (this.#state.tracks.get(track.name) === track) {\n\t\t\t\tthis.#state.tracks.delete(track.name);\n\t\t\t}\n\t\t});\n\t}\n\n\t/** Create a track, insert it into the broadcast, and return its producer. */\n\tcreateTrack(name: string, info: Partial<track.Info> = {}): track.Producer {\n\t\tconst producer = new track.Producer(name).accept(info);\n\t\tthis.insertTrack(producer);\n\t\treturn producer;\n\t}\n\n\t/** Remove a statically inserted track by name. */\n\tremoveTrack(name: string): void {\n\t\tthis.#state.tracks.delete(name);\n\t}\n\n\t/** Open a live subscription to a track. Used by the publishing wire layer. */\n\tsubscribe(name: string, options?: track.Subscription): track.Subscriber {\n\t\treturn subscribe(this.#state, name, options);\n\t}\n\n\t/** Resolve a track's immutable info. Used by the publishing wire layer. */\n\tresolveTrackInfo(name: string): Promise<track.Info> {\n\t\treturn resolveTrackInfo(this.#state, name);\n\t}\n\n\t/** Fetch a single group from the local retained window. Used by track handles. */\n\tfetchGroup(name: string, sequence: number, options?: track.FetchGroupOptions): Promise<GroupConsumer> {\n\t\treturn fetchGroup(this.#state, name, sequence, options);\n\t}\n\n\t/** A lazy read handle for a track on this broadcast. */\n\ttrack(name: string): track.Consumer {\n\t\treturn new track.Consumer(name, this);\n\t}\n\n\t/** Close the broadcast, optionally with an error to abort waiters. Idempotent. */\n\tclose(abort?: Error) {\n\t\tcloseState(this.#state, abort);\n\t}\n}\n\n// Constructs a Consumer from within this module without exposing a public constructor\n// that would leak the unexported BroadcastState. Assigned in the class's static block.\nlet makeConsumer: (state: BroadcastState) => Consumer;\n\n/**\n * The read side of a broadcast.\n *\n * Created internally: obtain one from {@link Producer.consume} or the connection's\n * `consume(path)`. The wire layers subclass it to resolve tracks over the network.\n *\n * @public\n */\nexport class Consumer implements track.Broadcast {\n\t#state: BroadcastState;\n\n\t// Guards against a double close() on this handle over-decrementing the consumer count.\n\t#closed = false;\n\n\tprotected constructor(state?: never);\n\tprotected constructor(state?: BroadcastState) {\n\t\tthis.#state = state ?? new BroadcastState();\n\t\tthis.#state.consumers++;\n\t}\n\n\tstatic {\n\t\tmakeConsumer = (state) => new Consumer(state as never);\n\t}\n\n\t/**\n\t * Settles once the broadcast closes: `null` on a clean close, or the abort {@link Error}.\n\t * Peek it synchronously (`undefined` while open), observe it reactively, or `await` it.\n\t *\n\t * Shared by every {@link clone}: it settles once the last handle closes. The subscribing\n\t * wire layer peeks it to evict a closed entry from its per-path consume cache.\n\t */\n\tget closed(): GetPromise<Error | null> {\n\t\treturn this.#state.closed;\n\t}\n\n\t/**\n\t * Return another handle to the same broadcast, reference-counted with this one.\n\t *\n\t * Both handles read the same tracks and share one {@link closed} state; the broadcast\n\t * closes only once *every* handle has {@link close}d. Used by the connection's per-path\n\t * consume cache to share one subscription across callers. Subclasses that resolve info over\n\t * the wire override this to preserve their type (see the wire layer's consumed broadcast).\n\t */\n\tclone(): Consumer {\n\t\treturn new Consumer(this.shareState());\n\t}\n\n\t// Hand this consumer's backing state to a clone. Opaque (`never`) so the state type stays\n\t// unexported; a subclass passes it straight back into its own `super(...)`.\n\tprotected shareState(): never {\n\t\treturn this.#state as never;\n\t}\n\n\t/** Get a lazy handle for a track on this broadcast. Repeat subscriptions dedupe onto one upstream subscription. */\n\ttrack(name: string): track.Consumer {\n\t\treturn new track.Consumer(name, this);\n\t}\n\n\t/** Open a live subscription to a track. Used by the subscribing wire layer. Repeat subscriptions to the same track share one upstream subscription. */\n\tsubscribe(name: string, options?: track.Subscription): track.Subscriber {\n\t\treturn subscribe(this.#state, name, options, true);\n\t}\n\n\t/** Return the next track requested by the local consumer. Used by the subscribing wire layer. */\n\tasync requested(): Promise<track.Request | undefined> {\n\t\tfor (;;) {\n\t\t\tconst request = dequeueRequest(this.#state);\n\t\t\tif (request) return request;\n\n\t\t\tconst closed = this.#state.closed.peek();\n\t\t\tif (closed instanceof Error) throw closed;\n\t\t\tif (closed !== undefined) return undefined;\n\n\t\t\tawait Signal.race(this.#state.requested, this.#state.closed);\n\t\t}\n\t}\n\n\t/**\n\t * Resolve a track's immutable info. Used by track handles. This base resolves it from\n\t * the local producers; the consuming wire layer overrides it to fetch over the wire.\n\t */\n\tresolveTrackInfo(name: string): Promise<track.Info> {\n\t\treturn resolveTrackInfo(this.#state, name);\n\t}\n\n\t/**\n\t * Fetch a single group by sequence. Used by track handles. This base serves from the\n\t * local retained window; the consuming wire layer overrides it to fetch over the wire\n\t * (or to reject when the transport has no FETCH).\n\t */\n\tfetchGroup(name: string, sequence: number, options?: track.FetchGroupOptions): Promise<GroupConsumer> {\n\t\treturn fetchGroup(this.#state, name, sequence, options);\n\t}\n\n\t/**\n\t * Release this handle. The broadcast is closed (optionally with an error to abort waiters)\n\t * once this was the last live handle; while other {@link clone}s remain open it stays live.\n\t */\n\tclose(abort?: Error) {\n\t\tif (this.#closed) return;\n\t\tthis.#closed = true;\n\t\tif (--this.#state.consumers > 0) return;\n\t\tcloseState(this.#state, abort);\n\t}\n}\n"]}
1
+ {"version":3,"file":"broadcast.js","sourceRoot":"","sources":["../src/broadcast.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAmB,IAAI,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAE7D,OAAO,EAAE,KAAK,EAAsB,MAAM,eAAe,CAAC;AAC1D,OAAO,KAAK,KAAK,MAAM,YAAY,CAAC;AAEpC,0EAA0E;AAC1E,MAAM,cAAc;IACnB,SAAS,GAAG,IAAI,MAAM,CAAkB,EAAE,CAAC,CAAC;IAC5C,MAAM,GAAG,IAAI,IAAI,EAAgB,CAAC;IAClC,MAAM,GAAG,IAAI,GAAG,EAA0B,CAAC;IAC3C,SAAS,GAAG,IAAI,GAAG,EAAyB,CAAC;IAC7C,uFAAuF;IACvF,0FAA0F;IAC1F,SAAS,GAAG,CAAC,CAAC;CACd;AAED,SAAS,cAAc,CAAC,KAAqB;IAC5C,MAAM,SAAS,GAAG,KAAK,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC;IACzC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,GAAG,CAAC,CAAC,QAAQ,CAAC,CAAC;IAClD,OAAO,SAAS,CAAC,GAAG,EAAE,CAAC;AACxB,CAAC;AAED,+EAA+E;AAC/E,8EAA8E;AAC9E,6DAA6D;AAC7D,EAAE;AACF,sFAAsF;AACtF,+CAA+C;AAC/C,SAAS,UAAU,CAAC,KAAqB,EAAE,KAAa;IACvD,IAAI,KAAK,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,SAAS;QAAE,OAAO;IAC9C,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,I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AC,MAAM,CAAC,CAAC;YAC5C,IAAI,OAAO;gBAAE,OAAO,OAAO,CAAC;YAE5B,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;YACzC,IAAI,MAAM,YAAY,KAAK;gBAAE,MAAM,MAAM,CAAC;YAC1C,IAAI,MAAM,KAAK,SAAS;gBAAE,OAAO,SAAS,CAAC;YAE3C,MAAM,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QAC9D,CAAC;IACF,CAAC;IAED;;;OAGG;IACH,gBAAgB,CAAC,IAAY;QAC5B,OAAO,gBAAgB,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IAC5C,CAAC;IAED;;;;OAIG;IACH,UAAU,CAAC,IAAY,EAAE,QAAgB,EAAE,OAAiC;QAC3E,OAAO,UAAU,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAC;IACzD,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,KAAa;QAClB,IAAI,IAAI,CAAC,OAAO;YAAE,OAAO;QACzB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QACpB,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC,SAAS,GAAG,CAAC;YAAE,OAAO;QACxC,UAAU,CAAC,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAChC,CAAC;CACD","sourcesContent":["/**\n * Broadcast role handles: a named collection of tracks produced by a publisher.\n *\n * @module\n */\nimport { type GetPromise, Once, Signal } from \"@moq/signals\";\nimport type { Consumer as GroupConsumer } from \"./group.ts\";\nimport { hooks, type TrackSequence } from \"./internal.ts\";\nimport * as track from \"./track.ts\";\n\n/** Reactive backing state shared by broadcast producers and consumers. */\nclass BroadcastState {\n\trequested = new Signal<track.Request[]>([]);\n\tclosed = new Once<Error | null>();\n\ttracks = new Map<string, track.Producer>();\n\tsequences = new Map<string, TrackSequence>();\n\t// Live consumer handles sharing this state (see {@link Consumer.clone}). The broadcast\n\t// closes once the last one closes, so a shared consumer can be handed to several callers.\n\tconsumers = 0;\n}\n\nfunction dequeueRequest(state: BroadcastState): track.Request | undefined {\n\tconst requested = state.requested.peek();\n\trequested.sort((a, b) => a.priority - b.priority);\n\treturn requested.pop();\n}\n\n// Close the broadcast and reject any requests still pending in the queue, so a\n// subscriber blocked on the track's info() or group reads is unblocked rather\n// than left waiting on a producer that will never be served.\n//\n// Once.set throws on a second settle, and the producer and each consumer handle close\n// independently, so this has to be idempotent.\nfunction closeState(state: BroadcastState, abort?: Error) {\n\tif (state.closed.peek() !== undefined) return;\n\tstate.closed.set(abort ?? null);\n\tstate.requested.mutate((requests) => {\n\t\tfor (const request of requests) request.reject(abort);\n\t\trequests.length = 0;\n\t});\n}\n\n// `register` is set on the subscribing (consumer) side: the fresh producer is cached in\n// `state.tracks` so repeat subscriptions to the same track fan out from one upstream subscription\n// instead of opening a new one, mirroring the Rust `broadcast::Consumer::track` weak-dedup. The\n// consumer wire watches the producer's demand ({@link track.Producer.used}) and tears the upstream\n// down once its last subscriber leaves, closing the producer, which evicts the cache entry below.\n// The publishing side leaves `register` false: `state.tracks` there holds only the tracks the app\n// inserted, and a dynamic serve stays one request per peer subscription.\nfunction subscribe(\n\tstate: BroadcastState,\n\tname: string,\n\toptions: track.Subscription = {},\n\tregister = false,\n): track.Subscriber {\n\tif (state.closed.peek() !== undefined) {\n\t\tthrow new Error(`broadcast is closed: ${state.closed.peek()}`);\n\t}\n\n\tconst existing = state.tracks.get(name);\n\tif (existing) {\n\t\tif (existing.closed.peek() === undefined) return existing.subscribe(options);\n\t\tstate.tracks.delete(name);\n\t}\n\n\tconst producer = new track.Producer(name);\n\tconst subscriber = producer.subscribe(options);\n\n\tif (register) {\n\t\tstate.tracks.set(name, producer);\n\t\t// Drop the cache entry once the upstream closes (the wire tears it down when its last\n\t\t// subscriber leaves), so a later subscribe re-opens it.\n\t\tvoid producer.closed.then(() => {\n\t\t\tif (state.tracks.get(name) === producer) state.tracks.delete(name);\n\t\t});\n\t}\n\n\tstate.requested.mutate((requested) => {\n\t\trequested.push(hooks.makeRequest({ name, producer, sequences: state.sequences }));\n\t});\n\n\treturn subscriber;\n}\n\nasync function resolveTrackInfo(state: BroadcastState, name: string): Promise<track.Info> {\n\tconst existing = state.tracks.get(name);\n\tif (existing && existing.closed.peek() === undefined) {\n\t\treturn existing.info();\n\t}\n\n\tif (state.closed.peek() !== undefined) {\n\t\treturn Promise.reject(new Error(`broadcast is closed: ${state.closed.peek()}`));\n\t}\n\n\tconst producer = new track.Producer(name);\n\tstate.requested.mutate((requested) => {\n\t\trequested.push(hooks.makeRequest({ name, producer, sequences: state.sequences }));\n\t});\n\n\ttry {\n\t\treturn await producer.info();\n\t} finally {\n\t\tproducer.close();\n\t}\n}\n\n// Serve a group from the local retained window by subscribing and scanning to the\n// requested sequence. The default for a produced broadcast; the consuming wire layer\n// overrides it to fetch over the network (or to reject when the transport has no FETCH).\nasync function fetchGroup(\n\tstate: BroadcastState,\n\tname: string,\n\tsequence: number,\n\toptions: track.FetchGroupOptions = {},\n): Promise<GroupConsumer> {\n\tconst subscriber = subscribe(state, name, { priority: options.priority });\n\ttry {\n\t\tfor (;;) {\n\t\t\tconst group = await subscriber.recvGroup();\n\t\t\tif (!group) throw new Error(`group not found: ${sequence}`);\n\t\t\tif (group.sequence === sequence) {\n\t\t\t\t// Close the subscription when the returned group finishes, not now: an\n\t\t\t\t// in-progress group must keep receiving frames for its lifetime (mirrors\n\t\t\t\t// Rust poll_fetch). Also fires if the caller closes the group early.\n\t\t\t\tvoid group.closed.then(() => subscriber.close());\n\t\t\t\treturn group;\n\t\t\t}\n\n\t\t\tgroup.close();\n\t\t\tif (group.sequence > sequence) throw new Error(`group not found: ${sequence}`);\n\t\t}\n\t} catch (err) {\n\t\tsubscriber.close();\n\t\tthrow err;\n\t}\n}\n\n/**\n * The write side of a broadcast.\n *\n * @public\n */\nexport class Producer implements track.Broadcast {\n\t#state = new BroadcastState();\n\n\t/**\n\t * Settles once the broadcast closes: `null` on a clean close, or the abort {@link Error}.\n\t * Peek it synchronously (`undefined` while open), observe it reactively, or `await` it.\n\t */\n\tget closed(): GetPromise<Error | null> {\n\t\treturn this.#state.closed;\n\t}\n\n\t/** A read handle for this broadcast. */\n\tconsume(): Consumer {\n\t\treturn makeConsumer(this.#state);\n\t}\n\n\t/** Return the next track requested by a peer. */\n\tasync requested(): Promise<track.Request | undefined> {\n\t\tfor (;;) {\n\t\t\tconst request = dequeueRequest(this.#state);\n\t\t\tif (request) return request;\n\n\t\t\tconst closed = this.#state.closed.peek();\n\t\t\tif (closed instanceof Error) throw closed;\n\t\t\tif (closed !== undefined) return undefined;\n\n\t\t\tawait Signal.race(this.#state.requested, this.#state.closed);\n\t\t}\n\t}\n\n\t/** Insert a track that is served directly, without an on-demand request round-trip. */\n\tinsertTrack(track: track.Producer): void {\n\t\tif (this.#state.closed.peek() !== undefined) {\n\t\t\tthrow new Error(`broadcast is closed: ${this.#state.closed.peek()}`);\n\t\t}\n\n\t\tconst existing = this.#state.tracks.get(track.name);\n\t\tif (existing && existing.closed.peek() === undefined) {\n\t\t\tthrow new Error(`duplicate track: ${track.name}`);\n\t\t}\n\n\t\tthis.#state.tracks.set(track.name, track);\n\n\t\tvoid track.closed.then(() => {\n\t\t\tif (this.#state.tracks.get(track.name) === track) {\n\t\t\t\tthis.#state.tracks.delete(track.name);\n\t\t\t}\n\t\t});\n\t}\n\n\t/** Create a track, insert it into the broadcast, and return its producer. */\n\tcreateTrack(name: string, info: Partial<track.Info> = {}): track.Producer {\n\t\tconst producer = new track.Producer(name).accept(info);\n\t\tthis.insertTrack(producer);\n\t\treturn producer;\n\t}\n\n\t/** Remove a statically inserted track by name. */\n\tremoveTrack(name: string): void {\n\t\tthis.#state.tracks.delete(name);\n\t}\n\n\t/** Open a live subscription to a track. Used by the publishing wire layer. */\n\tsubscribe(name: string, options?: track.Subscription): track.Subscriber {\n\t\treturn subscribe(this.#state, name, options);\n\t}\n\n\t/** Resolve a track's immutable info. Used by the publishing wire layer. */\n\tresolveTrackInfo(name: string): Promise<track.Info> {\n\t\treturn resolveTrackInfo(this.#state, name);\n\t}\n\n\t/** Fetch a single group from the local retained window. Used by track handles. */\n\tfetchGroup(name: string, sequence: number, options?: track.FetchGroupOptions): Promise<GroupConsumer> {\n\t\treturn fetchGroup(this.#state, name, sequence, options);\n\t}\n\n\t/** A lazy read handle for a track on this broadcast. */\n\ttrack(name: string): track.Consumer {\n\t\treturn new track.Consumer(name, this);\n\t}\n\n\t/** Close the broadcast, optionally with an error to abort waiters. Idempotent. */\n\tclose(abort?: Error) {\n\t\tcloseState(this.#state, abort);\n\t}\n}\n\n// Constructs a Consumer from within this module without exposing a public constructor\n// that would leak the unexported BroadcastState. Assigned in the class's static block.\nlet makeConsumer: (state: BroadcastState) => Consumer;\n\n/**\n * The read side of a broadcast.\n *\n * Created internally: obtain one from {@link Producer.consume} or the connection's\n * `consume(path)`. The wire layers subclass it to resolve tracks over the network.\n *\n * @public\n */\nexport class Consumer implements track.Broadcast {\n\t#state: BroadcastState;\n\n\t// Guards against a double close() on this handle over-decrementing the consumer count.\n\t#closed = false;\n\n\tprotected constructor(state?: never);\n\tprotected constructor(state?: BroadcastState) {\n\t\tthis.#state = state ?? new BroadcastState();\n\t\tthis.#state.consumers++;\n\t}\n\n\tstatic {\n\t\tmakeConsumer = (state) => new Consumer(state as never);\n\t}\n\n\t/**\n\t * Settles once the broadcast closes: `null` on a clean close, or the abort {@link Error}.\n\t * Peek it synchronously (`undefined` while open), observe it reactively, or `await` it.\n\t *\n\t * Shared by every {@link clone}: it settles once the last handle closes. The subscribing\n\t * wire layer peeks it to evict a closed entry from its per-path consume cache.\n\t */\n\tget closed(): GetPromise<Error | null> {\n\t\treturn this.#state.closed;\n\t}\n\n\t/**\n\t * Return another handle to the same broadcast, reference-counted with this one.\n\t *\n\t * Both handles read the same tracks and share one {@link closed} state; the broadcast\n\t * closes only once *every* handle has {@link close}d. Used by the connection's per-path\n\t * consume cache to share one subscription across callers. Subclasses that resolve info over\n\t * the wire override this to preserve their type (see the wire layer's consumed broadcast).\n\t */\n\tclone(): Consumer {\n\t\treturn new Consumer(this.shareState());\n\t}\n\n\t// Hand this consumer's backing state to a clone. Opaque (`never`) so the state type stays\n\t// unexported; a subclass passes it straight back into its own `super(...)`.\n\tprotected shareState(): never {\n\t\treturn this.#state as never;\n\t}\n\n\t/** Get a lazy handle for a track on this broadcast. Repeat subscriptions dedupe onto one upstream subscription. */\n\ttrack(name: string): track.Consumer {\n\t\treturn new track.Consumer(name, this);\n\t}\n\n\t/** Open a live subscription to a track. Used by the subscribing wire layer. Repeat subscriptions to the same track share one upstream subscription. */\n\tsubscribe(name: string, options?: track.Subscription): track.Subscriber {\n\t\treturn subscribe(this.#state, name, options, true);\n\t}\n\n\t/** Return the next track requested by the local consumer. Used by the subscribing wire layer. */\n\tasync requested(): Promise<track.Request | undefined> {\n\t\tfor (;;) {\n\t\t\tconst request = dequeueRequest(this.#state);\n\t\t\tif (request) return request;\n\n\t\t\tconst closed = this.#state.closed.peek();\n\t\t\tif (closed instanceof Error) throw closed;\n\t\t\tif (closed !== undefined) return undefined;\n\n\t\t\tawait Signal.race(this.#state.requested, this.#state.closed);\n\t\t}\n\t}\n\n\t/**\n\t * Resolve a track's immutable info. Used by track handles. This base resolves it from\n\t * the local producers; the consuming wire layer overrides it to fetch over the wire.\n\t */\n\tresolveTrackInfo(name: string): Promise<track.Info> {\n\t\treturn resolveTrackInfo(this.#state, name);\n\t}\n\n\t/**\n\t * Fetch a single group by sequence. Used by track handles. This base serves from the\n\t * local retained window; the consuming wire layer overrides it to fetch over the wire\n\t * (or to reject when the transport has no FETCH).\n\t */\n\tfetchGroup(name: string, sequence: number, options?: track.FetchGroupOptions): Promise<GroupConsumer> {\n\t\treturn fetchGroup(this.#state, name, sequence, options);\n\t}\n\n\t/**\n\t * Release this handle. The broadcast is closed (optionally with an error to abort waiters)\n\t * once this was the last live handle; while other {@link clone}s remain open it stays live.\n\t */\n\tclose(abort?: Error) {\n\t\tif (this.#closed) return;\n\t\tthis.#closed = true;\n\t\tif (--this.#state.consumers > 0) return;\n\t\tcloseState(this.#state, abort);\n\t}\n}\n"]}
package/ietf/aliases.d.ts CHANGED
@@ -1,11 +1,79 @@
1
+ /**
2
+ * The full track name an alias is bound to.
3
+ *
4
+ * Kept as two components rather than one joined string: a track name may contain the same
5
+ * separator a broadcast path uses, so `a` + `b/c` and `a/b` + `c` would otherwise compare
6
+ * equal and turn a genuine collision into apparent sharing.
7
+ * @internal
8
+ */
9
+ export type TrackIdentity = {
10
+ broadcast: string;
11
+ name: string;
12
+ };
13
+ /**
14
+ * Thrown when a group arrives for a subscription we already cancelled.
15
+ *
16
+ * The publisher only stops once our cancellation reaches it, so objects keep arriving for
17
+ * at least a round trip afterwards. Draft-19 section 11.1 asks us to keep enough state to
18
+ * discard them quickly "rather than treating them as belonging to an unknown Track Alias".
19
+ *
20
+ * This makes the window quiet, not safe. Once a publisher reassigns the alias the tombstone
21
+ * is reclaimed, and nothing on a group stream distinguishes the old subscription's objects
22
+ * from the new one's. Cancelling promptly is what bounds the exposure to a round trip.
23
+ * @internal
24
+ */
25
+ export declare class RetiredTrackAlias extends Error {
26
+ constructor(alias: bigint);
27
+ }
28
+ /**
29
+ * Thrown when a publisher gives one alias to two subscriptions of the same track.
30
+ *
31
+ * Draft-19 section 5.1 permits this and expects the subscriber to demux by re-applying
32
+ * each subscription's filter. Ours are all LargestObject, so they are indistinguishable and
33
+ * we cannot, which costs the one subscription. It is not a protocol error, so it must not
34
+ * take the session down with it.
35
+ * @internal
36
+ */
37
+ export declare class SharedTrackAlias extends Error {
38
+ constructor(alias: bigint);
39
+ }
40
+ /**
41
+ * Thrown when a publisher points one alias at two different tracks at once, which
42
+ * draft-19 section 11.1 makes fatal to the session.
43
+ * @internal
44
+ */
45
+ export declare class DuplicateTrackAlias extends Error {
46
+ constructor(alias: bigint);
47
+ }
1
48
  /** Resolves publisher-chosen track aliases after control/data stream reordering. @internal */
2
49
  export declare class TrackAliases<T> {
3
50
  #private;
4
- /** Waits briefly for an alias to be established by SUBSCRIBE_OK or PUBLISH. */
51
+ /**
52
+ * Waits briefly for an alias to be established by SUBSCRIBE_OK or PUBLISH.
53
+ *
54
+ * Throws {@link RetiredTrackAlias} at once for an alias we cancelled, rather than
55
+ * waiting out the timeout for a binding that is never coming.
56
+ */
5
57
  get(alias: bigint): Promise<T>;
6
- /** Establishes an alias and releases any data streams waiting for it. */
7
- set(alias: bigint, value: T): void;
8
- /** Removes an alias only if it still belongs to the supplied value. */
9
- delete(alias: bigint, value: T): void;
58
+ /**
59
+ * Establishes an alias and releases any data streams waiting for it.
60
+ *
61
+ * `track` is the full track name the alias was bound to, which is what decides whether a
62
+ * repeat is the legal sharing of an alias across subscriptions to one track
63
+ * ({@link SharedTrackAlias}) or the collision that must fail the session
64
+ * ({@link DuplicateTrackAlias}).
65
+ */
66
+ set(alias: bigint, value: T, track: TrackIdentity): void;
67
+ /**
68
+ * Retires an alias whose subscription was cancelled, so groups still in flight for it
69
+ * are discarded promptly instead of reported as unknown.
70
+ *
71
+ * Only retires an alias that still belongs to the supplied value: a later subscription
72
+ * may already have reclaimed it, and that binding outranks a departing owner.
73
+ *
74
+ * Returns whether this caller still owned the binding, so metadata keyed by the alias is
75
+ * only torn down by the owner and never out from under whoever reclaimed it.
76
+ */
77
+ retire(alias: bigint, value: T): boolean;
10
78
  }
11
79
  //# sourceMappingURL=aliases.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"aliases.d.ts","sourceRoot":"","sources":["../../src/ietf/aliases.ts"],"names":[],"mappings":"AAIA,8FAA8F;AAC9F,qBAAa,YAAY,CAAC,CAAC;;IAI1B,+EAA+E;IACzE,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,CAuBnC;IAED,yEAAyE;IACzE,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,QAW1B;IAED,uEAAuE;IACvE,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,QAE7B;CACD"}
1
+ {"version":3,"file":"aliases.d.ts","sourceRoot":"","sources":["../../src/ietf/aliases.ts"],"names":[],"mappings":"AAiBA;;;;;;;GAOG;AACH,MAAM,MAAM,aAAa,GAAG;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC;AAMhE;;;;;;;;;;;GAWG;AACH,qBAAa,iBAAkB,SAAQ,KAAK;IAC3C,YAAY,KAAK,EAAE,MAAM,EAGxB;CACD;AAED;;;;;;;;GAQG;AACH,qBAAa,gBAAiB,SAAQ,KAAK;IAC1C,YAAY,KAAK,EAAE,MAAM,EAGxB;CACD;AAED;;;;GAIG;AACH,qBAAa,mBAAoB,SAAQ,KAAK;IAC7C,YAAY,KAAK,EAAE,MAAM,EAGxB;CACD;AAED,8FAA8F;AAC9F,qBAAa,YAAY,CAAC,CAAC;;IAQ1B;;;;;OAKG;IACG,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,CAyBnC;IAED;;;;;;;OAOG;IACH,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,EAAE,KAAK,EAAE,aAAa,QAehD;IAED;;;;;;;;;OASG;IACH,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,OAAO,CAcvC;CAOD"}
package/ietf/aliases.js CHANGED
@@ -1,13 +1,83 @@
1
1
  /* @ts-self-types="./aliases.d.ts" */
2
2
  const TRACK_ALIAS_TIMEOUT_MS = 1000;
3
+ /**
4
+ * How many cancelled aliases to remember. Objects keep arriving for about a round trip
5
+ * after we cancel, so a handful covers the window, while the cap keeps a long session with
6
+ * heavy subscription churn from accumulating tombstones for its whole lifetime.
7
+ *
8
+ * The bound is a count rather than a deadline, so eviction stays synchronous with
9
+ * retirement instead of needing a timer. The trade is that a session cancelling more than
10
+ * this many distinct aliases inside one round trip evicts a tombstone whose objects are
11
+ * still arriving; those fall back to the unknown-alias wait, which is the old behavior
12
+ * rather than a new failure.
13
+ */
14
+ const RETIRED_ALIAS_CAPACITY = 64;
15
+ function sameTrack(a, b) {
16
+ return a.broadcast === b.broadcast && a.name === b.name;
17
+ }
18
+ /**
19
+ * Thrown when a group arrives for a subscription we already cancelled.
20
+ *
21
+ * The publisher only stops once our cancellation reaches it, so objects keep arriving for
22
+ * at least a round trip afterwards. Draft-19 section 11.1 asks us to keep enough state to
23
+ * discard them quickly "rather than treating them as belonging to an unknown Track Alias".
24
+ *
25
+ * This makes the window quiet, not safe. Once a publisher reassigns the alias the tombstone
26
+ * is reclaimed, and nothing on a group stream distinguishes the old subscription's objects
27
+ * from the new one's. Cancelling promptly is what bounds the exposure to a round trip.
28
+ * @internal
29
+ */
30
+ export class RetiredTrackAlias extends Error {
31
+ constructor(alias) {
32
+ super(`track alias retired: ${alias}`);
33
+ this.name = "RetiredTrackAlias";
34
+ }
35
+ }
36
+ /**
37
+ * Thrown when a publisher gives one alias to two subscriptions of the same track.
38
+ *
39
+ * Draft-19 section 5.1 permits this and expects the subscriber to demux by re-applying
40
+ * each subscription's filter. Ours are all LargestObject, so they are indistinguishable and
41
+ * we cannot, which costs the one subscription. It is not a protocol error, so it must not
42
+ * take the session down with it.
43
+ * @internal
44
+ */
45
+ export class SharedTrackAlias extends Error {
46
+ constructor(alias) {
47
+ super(`track alias shared by another subscription: ${alias}`);
48
+ this.name = "SharedTrackAlias";
49
+ }
50
+ }
51
+ /**
52
+ * Thrown when a publisher points one alias at two different tracks at once, which
53
+ * draft-19 section 11.1 makes fatal to the session.
54
+ * @internal
55
+ */
56
+ export class DuplicateTrackAlias extends Error {
57
+ constructor(alias) {
58
+ super(`duplicate track alias: ${alias}`);
59
+ this.name = "DuplicateTrackAlias";
60
+ }
61
+ }
3
62
  /** Resolves publisher-chosen track aliases after control/data stream reordering. @internal */
4
63
  export class TrackAliases {
5
64
  #active = new Map();
6
65
  #pending = new Map();
7
- /** Waits briefly for an alias to be established by SUBSCRIBE_OK or PUBLISH. */
66
+ /** Aliases whose subscription we cancelled, in retirement order so the oldest is forgotten first. */
67
+ #retired = [];
68
+ #retiredSet = new Set();
69
+ /**
70
+ * Waits briefly for an alias to be established by SUBSCRIBE_OK or PUBLISH.
71
+ *
72
+ * Throws {@link RetiredTrackAlias} at once for an alias we cancelled, rather than
73
+ * waiting out the timeout for a binding that is never coming.
74
+ */
8
75
  async get(alias) {
9
- if (this.#active.has(alias))
10
- return this.#active.get(alias);
76
+ const bound = this.#active.get(alias);
77
+ if (bound !== undefined)
78
+ return bound.value;
79
+ if (this.#retiredSet.has(alias))
80
+ throw new RetiredTrackAlias(alias);
11
81
  const { promise, resolve } = Promise.withResolvers();
12
82
  let resolvers = this.#pending.get(alias);
13
83
  if (!resolvers) {
@@ -29,24 +99,61 @@ export class TrackAliases {
29
99
  this.#pending.delete(alias);
30
100
  }
31
101
  }
32
- /** Establishes an alias and releases any data streams waiting for it. */
33
- set(alias, value) {
102
+ /**
103
+ * Establishes an alias and releases any data streams waiting for it.
104
+ *
105
+ * `track` is the full track name the alias was bound to, which is what decides whether a
106
+ * repeat is the legal sharing of an alias across subscriptions to one track
107
+ * ({@link SharedTrackAlias}) or the collision that must fail the session
108
+ * ({@link DuplicateTrackAlias}).
109
+ */
110
+ set(alias, value, track) {
34
111
  const active = this.#active.get(alias);
35
- if (this.#active.has(alias)) {
36
- if (active !== value)
37
- throw new Error(`duplicate track alias: ${alias}`);
38
- return;
112
+ if (active !== undefined) {
113
+ if (active.value === value)
114
+ return;
115
+ throw sameTrack(active.track, track) ? new SharedTrackAlias(alias) : new DuplicateTrackAlias(alias);
39
116
  }
40
- this.#active.set(alias, value);
117
+ // Our subscription is gone, so the publisher is free to point the alias somewhere
118
+ // new. Reclaiming it also drops the tombstone early.
119
+ this.#forget(alias);
120
+ this.#active.set(alias, { value, track });
41
121
  const resolvers = this.#pending.get(alias);
42
122
  this.#pending.delete(alias);
43
123
  for (const resolve of resolvers ?? [])
44
124
  resolve(value);
45
125
  }
46
- /** Removes an alias only if it still belongs to the supplied value. */
47
- delete(alias, value) {
48
- if (this.#active.get(alias) === value)
49
- this.#active.delete(alias);
126
+ /**
127
+ * Retires an alias whose subscription was cancelled, so groups still in flight for it
128
+ * are discarded promptly instead of reported as unknown.
129
+ *
130
+ * Only retires an alias that still belongs to the supplied value: a later subscription
131
+ * may already have reclaimed it, and that binding outranks a departing owner.
132
+ *
133
+ * Returns whether this caller still owned the binding, so metadata keyed by the alias is
134
+ * only torn down by the owner and never out from under whoever reclaimed it.
135
+ */
136
+ retire(alias, value) {
137
+ if (this.#active.get(alias)?.value !== value)
138
+ return false;
139
+ this.#active.delete(alias);
140
+ if (this.#retiredSet.has(alias))
141
+ return true;
142
+ this.#retiredSet.add(alias);
143
+ this.#retired.push(alias);
144
+ while (this.#retired.length > RETIRED_ALIAS_CAPACITY) {
145
+ const oldest = this.#retired.shift();
146
+ if (oldest !== undefined)
147
+ this.#retiredSet.delete(oldest);
148
+ }
149
+ return true;
150
+ }
151
+ #forget(alias) {
152
+ if (!this.#retiredSet.delete(alias))
153
+ return;
154
+ const at = this.#retired.indexOf(alias);
155
+ if (at !== -1)
156
+ this.#retired.splice(at, 1);
50
157
  }
51
158
  }
52
159
  //# sourceMappingURL=aliases.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"aliases.js","sourceRoot":"","sources":["../../src/ietf/aliases.ts"],"names":[],"mappings":"AAAA,MAAM,sBAAsB,GAAG,IAAI,CAAC;AAIpC,8FAA8F;AAC9F,MAAM,OAAO,YAAY;IACxB,OAAO,GAAG,IAAI,GAAG,EAAa,CAAC;IAC/B,QAAQ,GAAG,IAAI,GAAG,EAA4B,CAAC;IAE/C,+EAA+E;IAC/E,KAAK,CAAC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,KAAQ;QAC1B,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QACvC,IAAI,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC;YAC7B,IAAI,MAAM,KAAK,KAAK;gBAAE,MAAM,IAAI,KAAK,CAAC,0BAA0B,KAAK,EAAE,CAAC,CAAC;YACzE,OAAO;QACR,CAAC;QAED,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;QAC/B,MAAM,SAAS,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QAC3C,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAC5B,KAAK,MAAM,OAAO,IAAI,SAAS,IAAI,EAAE;YAAE,OAAO,CAAC,KAAK,CAAC,CAAC;IACvD,CAAC;IAED,uEAAuE;IACvE,MAAM,CAAC,KAAa,EAAE,KAAQ;QAC7B,IAAI,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,KAAK;YAAE,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACnE,CAAC;CACD","sourcesContent":["const TRACK_ALIAS_TIMEOUT_MS = 1000;\n\ntype Resolver<T> = PromiseWithResolvers<T>[\"resolve\"];\n\n/** Resolves publisher-chosen track aliases after control/data stream reordering. @internal */\nexport class TrackAliases<T> {\n\t#active = new Map<bigint, T>();\n\t#pending = new Map<bigint, Set<Resolver<T>>>();\n\n\t/** Waits briefly for an alias to be established by SUBSCRIBE_OK or PUBLISH. */\n\tasync get(alias: bigint): Promise<T> {\n\t\tif (this.#active.has(alias)) return this.#active.get(alias) as T;\n\n\t\tconst { promise, resolve } = Promise.withResolvers<T>();\n\t\tlet resolvers = this.#pending.get(alias);\n\t\tif (!resolvers) {\n\t\t\tresolvers = new Set();\n\t\t\tthis.#pending.set(alias, resolvers);\n\t\t}\n\t\tresolvers.add(resolve);\n\n\t\tlet timer: ReturnType<typeof setTimeout> | undefined;\n\t\tconst timeout = new Promise<never>((_, reject) => {\n\t\t\ttimer = setTimeout(() => reject(new Error(`unknown track alias: ${alias}`)), TRACK_ALIAS_TIMEOUT_MS);\n\t\t});\n\n\t\ttry {\n\t\t\treturn await Promise.race([promise, timeout]);\n\t\t} finally {\n\t\t\tclearTimeout(timer);\n\t\t\tresolvers.delete(resolve);\n\t\t\tif (this.#pending.get(alias) === resolvers && resolvers.size === 0) this.#pending.delete(alias);\n\t\t}\n\t}\n\n\t/** Establishes an alias and releases any data streams waiting for it. */\n\tset(alias: bigint, value: T) {\n\t\tconst active = this.#active.get(alias);\n\t\tif (this.#active.has(alias)) {\n\t\t\tif (active !== value) throw new Error(`duplicate track alias: ${alias}`);\n\t\t\treturn;\n\t\t}\n\n\t\tthis.#active.set(alias, value);\n\t\tconst resolvers = this.#pending.get(alias);\n\t\tthis.#pending.delete(alias);\n\t\tfor (const resolve of resolvers ?? []) resolve(value);\n\t}\n\n\t/** Removes an alias only if it still belongs to the supplied value. */\n\tdelete(alias: bigint, value: T) {\n\t\tif (this.#active.get(alias) === value) this.#active.delete(alias);\n\t}\n}\n"]}
1
+ {"version":3,"file":"aliases.js","sourceRoot":"","sources":["../../src/ietf/aliases.ts"],"names":[],"mappings":"AAAA,MAAM,sBAAsB,GAAG,IAAI,CAAC;AAEpC;;;;;;;;;;GAUG;AACH,MAAM,sBAAsB,GAAG,EAAE,CAAC;AAclC,SAAS,SAAS,CAAC,CAAgB,EAAE,CAAgB;IACpD,OAAO,CAAC,CAAC,SAAS,KAAK,CAAC,CAAC,SAAS,IAAI,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,IAAI,CAAC;AACzD,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,OAAO,iBAAkB,SAAQ,KAAK;IAC3C,YAAY,KAAa;QACxB,KAAK,CAAC,wBAAwB,KAAK,EA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IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;QACxC,IAAI,EAAE,KAAK,CAAC,CAAC;YAAE,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;IAC5C,CAAC;CACD","sourcesContent":["const TRACK_ALIAS_TIMEOUT_MS = 1000;\n\n/**\n * How many cancelled aliases to remember. Objects keep arriving for about a round trip\n * after we cancel, so a handful covers the window, while the cap keeps a long session with\n * heavy subscription churn from accumulating tombstones for its whole lifetime.\n *\n * The bound is a count rather than a deadline, so eviction stays synchronous with\n * retirement instead of needing a timer. The trade is that a session cancelling more than\n * this many distinct aliases inside one round trip evicts a tombstone whose objects are\n * still arriving; those fall back to the unknown-alias wait, which is the old behavior\n * rather than a new failure.\n */\nconst RETIRED_ALIAS_CAPACITY = 64;\n\ntype Resolver<T> = PromiseWithResolvers<T>[\"resolve\"];\n\n/**\n * The full track name an alias is bound to.\n *\n * Kept as two components rather than one joined string: a track name may contain the same\n * separator a broadcast path uses, so `a` + `b/c` and `a/b` + `c` would otherwise compare\n * equal and turn a genuine collision into apparent sharing.\n * @internal\n */\nexport type TrackIdentity = { broadcast: string; name: string };\n\nfunction sameTrack(a: TrackIdentity, b: TrackIdentity): boolean {\n\treturn a.broadcast === b.broadcast && a.name === b.name;\n}\n\n/**\n * Thrown when a group arrives for a subscription we already cancelled.\n *\n * The publisher only stops once our cancellation reaches it, so objects keep arriving for\n * at least a round trip afterwards. Draft-19 section 11.1 asks us to keep enough state to\n * discard them quickly \"rather than treating them as belonging to an unknown Track Alias\".\n *\n * This makes the window quiet, not safe. Once a publisher reassigns the alias the tombstone\n * is reclaimed, and nothing on a group stream distinguishes the old subscription's objects\n * from the new one's. Cancelling promptly is what bounds the exposure to a round trip.\n * @internal\n */\nexport class RetiredTrackAlias extends Error {\n\tconstructor(alias: bigint) {\n\t\tsuper(`track alias retired: ${alias}`);\n\t\tthis.name = \"RetiredTrackAlias\";\n\t}\n}\n\n/**\n * Thrown when a publisher gives one alias to two subscriptions of the same track.\n *\n * Draft-19 section 5.1 permits this and expects the subscriber to demux by re-applying\n * each subscription's filter. Ours are all LargestObject, so they are indistinguishable and\n * we cannot, which costs the one subscription. It is not a protocol error, so it must not\n * take the session down with it.\n * @internal\n */\nexport class SharedTrackAlias extends Error {\n\tconstructor(alias: bigint) {\n\t\tsuper(`track alias shared by another subscription: ${alias}`);\n\t\tthis.name = \"SharedTrackAlias\";\n\t}\n}\n\n/**\n * Thrown when a publisher points one alias at two different tracks at once, which\n * draft-19 section 11.1 makes fatal to the session.\n * @internal\n */\nexport class DuplicateTrackAlias extends Error {\n\tconstructor(alias: bigint) {\n\t\tsuper(`duplicate track alias: ${alias}`);\n\t\tthis.name = \"DuplicateTrackAlias\";\n\t}\n}\n\n/** Resolves publisher-chosen track aliases after control/data stream reordering. @internal */\nexport class TrackAliases<T> {\n\t#active = new Map<bigint, { value: T; track: TrackIdentity }>();\n\t#pending = new Map<bigint, Set<Resolver<T>>>();\n\n\t/** Aliases whose subscription we cancelled, in retirement order so the oldest is forgotten first. */\n\t#retired: bigint[] = [];\n\t#retiredSet = new Set<bigint>();\n\n\t/**\n\t * Waits briefly for an alias to be established by SUBSCRIBE_OK or PUBLISH.\n\t *\n\t * Throws {@link RetiredTrackAlias} at once for an alias we cancelled, rather than\n\t * waiting out the timeout for a binding that is never coming.\n\t */\n\tasync get(alias: bigint): Promise<T> {\n\t\tconst bound = this.#active.get(alias);\n\t\tif (bound !== undefined) return bound.value;\n\t\tif (this.#retiredSet.has(alias)) throw new RetiredTrackAlias(alias);\n\n\t\tconst { promise, resolve } = Promise.withResolvers<T>();\n\t\tlet resolvers = this.#pending.get(alias);\n\t\tif (!resolvers) {\n\t\t\tresolvers = new Set();\n\t\t\tthis.#pending.set(alias, resolvers);\n\t\t}\n\t\tresolvers.add(resolve);\n\n\t\tlet timer: ReturnType<typeof setTimeout> | undefined;\n\t\tconst timeout = new Promise<never>((_, reject) => {\n\t\t\ttimer = setTimeout(() => reject(new Error(`unknown track alias: ${alias}`)), TRACK_ALIAS_TIMEOUT_MS);\n\t\t});\n\n\t\ttry {\n\t\t\treturn await Promise.race([promise, timeout]);\n\t\t} finally {\n\t\t\tclearTimeout(timer);\n\t\t\tresolvers.delete(resolve);\n\t\t\tif (this.#pending.get(alias) === resolvers && resolvers.size === 0) this.#pending.delete(alias);\n\t\t}\n\t}\n\n\t/**\n\t * Establishes an alias and releases any data streams waiting for it.\n\t *\n\t * `track` is the full track name the alias was bound to, which is what decides whether a\n\t * repeat is the legal sharing of an alias across subscriptions to one track\n\t * ({@link SharedTrackAlias}) or the collision that must fail the session\n\t * ({@link DuplicateTrackAlias}).\n\t */\n\tset(alias: bigint, value: T, track: TrackIdentity) {\n\t\tconst active = this.#active.get(alias);\n\t\tif (active !== undefined) {\n\t\t\tif (active.value === value) return;\n\t\t\tthrow sameTrack(active.track, track) ? new SharedTrackAlias(alias) : new DuplicateTrackAlias(alias);\n\t\t}\n\n\t\t// Our subscription is gone, so the publisher is free to point the alias somewhere\n\t\t// new. Reclaiming it also drops the tombstone early.\n\t\tthis.#forget(alias);\n\n\t\tthis.#active.set(alias, { value, track });\n\t\tconst resolvers = this.#pending.get(alias);\n\t\tthis.#pending.delete(alias);\n\t\tfor (const resolve of resolvers ?? []) resolve(value);\n\t}\n\n\t/**\n\t * Retires an alias whose subscription was cancelled, so groups still in flight for it\n\t * are discarded promptly instead of reported as unknown.\n\t *\n\t * Only retires an alias that still belongs to the supplied value: a later subscription\n\t * may already have reclaimed it, and that binding outranks a departing owner.\n\t *\n\t * Returns whether this caller still owned the binding, so metadata keyed by the alias is\n\t * only torn down by the owner and never out from under whoever reclaimed it.\n\t */\n\tretire(alias: bigint, value: T): boolean {\n\t\tif (this.#active.get(alias)?.value !== value) return false;\n\t\tthis.#active.delete(alias);\n\n\t\tif (this.#retiredSet.has(alias)) return true;\n\t\tthis.#retiredSet.add(alias);\n\t\tthis.#retired.push(alias);\n\n\t\twhile (this.#retired.length > RETIRED_ALIAS_CAPACITY) {\n\t\t\tconst oldest = this.#retired.shift();\n\t\t\tif (oldest !== undefined) this.#retiredSet.delete(oldest);\n\t\t}\n\n\t\treturn true;\n\t}\n\n\t#forget(alias: bigint) {\n\t\tif (!this.#retiredSet.delete(alias)) return;\n\t\tconst at = this.#retired.indexOf(alias);\n\t\tif (at !== -1) this.#retired.splice(at, 1);\n\t}\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"publisher.d.ts","sourceRoot":"","sources":["../../src/ietf/publisher.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,KAAK,SAAS,MAAM,iBAAiB,CAAC;AAGlD,OAAO,KAAK,IAAI,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,KAAK,MAAM,EAAU,MAAM,cAAc,CAAC;AAGnD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,KAAK,OAAO,MAAM,cAAc,CAAC;AAMxC,OAAO,EAAE,KAAK,SAAS,EAA+B,MAAM,gBAAgB,CAAC;AAC7E,OAAO,EACN,KAAK,kBAAkB,EAIvB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAe,KAAK,kBAAkB,EAAE,MAAM,YAAY,CAAC;AA8ClE;;;;;GAKG;AACH,qBAAa,SAAS;;IAcrB;;;;OAIG;IACH,YAAY,EACX,IAAI,EACJ,OAAO,EACP,oBAAoB,EACpB,OAAO,GACP,EAAE;QACF,iDAAiD;QACjD,IAAI,EAAE,YAAY,CAAC;QACnB,gEAAgE;QAChE,OAAO,EAAE,OAAO,CAAC;QACjB,0EAA0E;QAC1E,oBAAoB,EAAE,OAAO,CAAC;QAC9B,4DAA4D;QAC5D,OAAO,CAAC,EAAE,OAAO,CAAC,IAAI,CAAC;KACvB,EAKA;IAED;;;;OAIG;IACH,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,KAAK,EAAE,SAAS,EAAE,SAAS,CAAC,QAAQ,QAYtD;IAED;;;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,CAAC,KAAK,EAAE,SAAS,EAAE,SAAS,CAAC,QAAQ,QActD;IAED;;;;;OAKG;IACG,YAAY,CAAC,GAAG,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,iBAwGhD;IAwDD;;;;;;;;;;OAUG;IACG,qBAAqB,CAAC,GAAG,EAAE,kBAAkB,EAAE,MAAM,EAAE,MAAM,iBA+HlE;IAED;;;;;;;;;;;OAWG;IACG,oBAAoB,kBA6EzB;IAkID;;;;OAIG;IACG,qBAAqB,CAAC,GAAG,EAAE,kBAAkB,EAAE,MAAM,EAAE,MAAM,iBAuBlE;IAED;;;;OAIG;IACH,KAAK,SAOJ;CACD"}
package/ietf/publisher.js CHANGED
@@ -67,10 +67,12 @@ export class Publisher {
67
67
  throw new Error("closed");
68
68
  broadcasts.set(path, broadcast);
69
69
  });
70
- // Remove the broadcast from the lookup when it's closed.
70
+ // Remove the broadcast from the lookup when it's closed, unless the path was republished.
71
71
  void broadcast.closed.then(() => {
72
72
  this.#broadcasts.mutate((broadcasts) => {
73
- broadcasts?.delete(path);
73
+ if (broadcasts?.get(path) === broadcast) {
74
+ broadcasts.delete(path);
75
+ }
74
76
  });
75
77
  });
76
78
  }