@moq/net 0.2.2 → 0.2.4
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.
- package/README.md +1 -0
- package/announced.d.ts +75 -1
- package/announced.d.ts.map +1 -1
- package/announced.js +140 -1
- package/announced.js.map +1 -1
- package/announced.test.js +3 -3
- package/announced.test.js.map +1 -1
- package/connection/established.d.ts +14 -1
- package/connection/established.d.ts.map +1 -1
- package/connection/established.js.map +1 -1
- package/connection/reload.d.ts +26 -5
- package/connection/reload.d.ts.map +1 -1
- package/connection/reload.js +55 -26
- package/connection/reload.js.map +1 -1
- package/connection/reload.test.js +56 -0
- package/connection/reload.test.js.map +1 -1
- package/consume.d.ts +18 -0
- package/consume.d.ts.map +1 -1
- package/consume.js +20 -0
- package/consume.js.map +1 -1
- package/ietf/connection.d.ts +8 -1
- package/ietf/connection.d.ts.map +1 -1
- package/ietf/connection.js +11 -0
- package/ietf/connection.js.map +1 -1
- package/ietf/ietf.test.js +63 -10
- package/ietf/ietf.test.js.map +1 -1
- package/ietf/object.d.ts +3 -3
- package/ietf/object.d.ts.map +1 -1
- package/ietf/object.js +30 -16
- package/ietf/object.js.map +1 -1
- package/ietf/properties.d.ts +8 -2
- package/ietf/properties.d.ts.map +1 -1
- package/ietf/properties.js +60 -8
- package/ietf/properties.js.map +1 -1
- package/ietf/publish.d.ts.map +1 -1
- package/ietf/publish.js +13 -3
- package/ietf/publish.js.map +1 -1
- package/ietf/publisher.d.ts +14 -2
- package/ietf/publisher.d.ts.map +1 -1
- package/ietf/publisher.js +155 -107
- package/ietf/publisher.js.map +1 -1
- package/ietf/request.js +1 -1
- package/ietf/request.js.map +1 -1
- package/ietf/subscribe.d.ts +9 -1
- package/ietf/subscribe.d.ts.map +1 -1
- package/ietf/subscribe.js +20 -4
- package/ietf/subscribe.js.map +1 -1
- package/ietf/subscriber.d.ts.map +1 -1
- package/ietf/subscriber.js +18 -1
- package/ietf/subscriber.js.map +1 -1
- package/integration.test.js +206 -0
- package/integration.test.js.map +1 -1
- package/lite/connection.d.ts +8 -1
- package/lite/connection.d.ts.map +1 -1
- package/lite/connection.js +10 -0
- package/lite/connection.js.map +1 -1
- package/lite/setup.d.ts +4 -3
- package/lite/setup.d.ts.map +1 -1
- package/lite/setup.js +4 -3
- package/lite/setup.js.map +1 -1
- package/lite/subscriber.d.ts.map +1 -1
- package/lite/subscriber.js +48 -3
- package/lite/subscriber.js.map +1 -1
- package/lite/subscriber.test.js +78 -0
- package/lite/subscriber.test.js.map +1 -1
- package/package.json +2 -2
package/connection/reload.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"reload.js","sourceRoot":"","sources":["../../src/connection/reload.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAe,MAAM,EAAE,MAAM,cAAc,CAAC;AAC3D,OAAO,KAAK,QAAQ,MAAM,iBAAiB,CAAC;AAE5C,OAAO,EAAE,KAAK,IAAI,SAAS,EAAE,MAAM,YAAY,CAAC;AAChD,OAAO,EAAqB,OAAO,EAAiD,MAAM,cAAc,CAAC;AAsCzG,uFAAuF;AACvF,MAAM,OAAO,MAAM;IAClB,iEAAiE;IACjE,GAAG,CAA0B;IAE7B,sCAAsC;IACtC,OAAO,CAAkB;IAEzB,iCAAiC;IACjC,MAAM,GAAG,IAAI,MAAM,CAAe,cAAc,CAAC,CAAC;IAElD,0EAA0E;IAC1E,WAAW,GAAG,IAAI,MAAM,CAA0B,SAAS,CAAC,CAAC;IAE7D;;;;;OAKG;IACM,KAAK,CAA4B;IAE1C,8EAA8E;IAC9E,YAAY,CAAqB;IAEjC,oFAAoF;IACpF,SAAS,CAA+B;IAExC;;;OAGG;IACH,SAAS,CAAW;IAEpB,+CAA+C;IAC/C,KAAK,CAAc;IAEnB,0FAA0F;IAC1F,QAAQ,GAAG,IAAI,MAAM,EAAE,CAAC;IAExB,4FAA4F;IAC5F,MAAM,CAAgB;IACtB,cAAc,CAAc;IAC5B,aA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C5D,IAAI,CAAC,MAAM,EAAE,CAAC;wBACb,KAAK,MAAM,IAAI,IAAI,MAAM,EAAE,CAAC;4BAC3B,QAAQ,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC;wBAC1C,CAAC;oBACF,CAAC;gBACF,CAAC;YACF,CAAC,CAAC,CAAC;QACJ,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC;QAC1C,KAAK,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC;QAE9C,OAAO,QAAQ,CAAC;IACjB,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,KAAK;QACV,OAAO,IAAI,CAAC,WAAW,CAAC,IAAI,EAAE,EAAE,KAAK,EAAE,CAAC;IACzC,CAAC;IAED,0FAA0F;IAC1F,KAAK;QACJ,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC;QACtB,IAAI,CAAC,cAAc,EAAE,CAAC;IACvB,CAAC;CACD","sourcesContent":["import { Effect, type Getter, Signal } from \"@moq/signals\";\nimport * as Announce from \"../announced.ts\";\nimport type * as Path from \"../path.ts\";\nimport { empty as emptyPath } from \"../path.ts\";\nimport { type ConnectProps, connect, type WebSocketOptions, type WebTransportProps } from \"./connect.ts\";\nimport type { Established } from \"./established.ts\";\nimport type { Probe, Stats } from \"./stats.ts\";\n\n/** Exponential backoff settings for {@link Reload}'s reconnect loop. */\nexport type ReloadDelay = {\n\t/** The delay in milliseconds before reconnecting (default: 1000). */\n\tinitial: DOMHighResTimeStamp;\n\n\t/** The multiplier for the delay (default: 2). */\n\tmultiplier: number;\n\n\t/** The maximum delay in milliseconds (default: 30000). */\n\tmax: DOMHighResTimeStamp;\n\n\t/**\n\t * Maximum total time in milliseconds to spend retrying before giving up (default:\n\t * 300000, 5 minutes). Resets after each successful connection. Set to 0 for\n\t * unlimited retries.\n\t */\n\ttimeout?: DOMHighResTimeStamp;\n};\n\n/** Connection and retry options for {@link Reload}. */\nexport type ReloadProps = Omit<ConnectProps, \"signal\"> & {\n\t/** Whether to reload the connection when it disconnects (default: true). */\n\tenabled?: boolean | Signal<boolean>;\n\n\t/** The URL of the relay server. */\n\turl?: URL | Signal<URL | undefined>;\n\n\t/** Backoff settings for the reconnect loop. */\n\tdelay?: ReloadDelay;\n};\n\n/** Current state of a {@link Reload} connection. */\nexport type ReloadStatus = \"connecting\" | \"connected\" | \"disconnected\";\n\n/** Maintains a MoQ connection, reconnecting with exponential backoff when it drops. */\nexport class Reload {\n\t/** Relay URL to connect to; updating it triggers a reconnect. */\n\turl: Signal<URL | undefined>;\n\n\t/** Whether reconnecting is active. */\n\tenabled: Signal<boolean>;\n\n\t/** Current connection status. */\n\tstatus = new Signal<ReloadStatus>(\"disconnected\");\n\n\t/** The currently established session, or undefined while disconnected. */\n\testablished = new Signal<Established | undefined>(undefined);\n\n\t/**\n\t * The current connection's PROBE estimates, spanning reconnects.\n\t *\n\t * Undefined while disconnected: the estimates belong to a single connection.\n\t * See {@link Established.probe}.\n\t */\n\treadonly probe: Getter<Probe | undefined>;\n\n\t/** WebTransport options applied to each connection attempt (not reactive). */\n\twebtransport?: WebTransportProps;\n\n\t/** WebSocket fallback options applied to each connection attempt (not reactive). */\n\twebsocket: WebSocketOptions | undefined;\n\n\t/**\n\t * Whether the relay supports broadcast discovery, applied to each connection attempt (not\n\t * reactive). Undefined defers to the default for the URL. See {@link Established.discovery}.\n\t */\n\tdiscovery?: boolean;\n\n\t/** Backoff settings for the reconnect loop. */\n\tdelay: ReloadDelay;\n\n\t/** The reactive effect scope driving the connect loop; closed by {@link Reload.close}. */\n\t#signals = new Effect();\n\n\t/** Resolves when the reconnect loop stops via {@link Reload.close} or the retry timeout. */\n\tclosed: Promise<void>;\n\t#closedResolve!: () => void;\n\t#closedReject!: (err: Error) => void;\n\n\t#delay: DOMHighResTimeStamp;\n\n\t// Timestamp when the current retry sequence started (for timeout).\n\t#retryStart: DOMHighResTimeStamp | undefined;\n\n\t// Increased by 1 each time to trigger a reload.\n\t#tick = new Signal(0);\n\n\t// True after the browser freezes or hides the page until it visibly resumes.\n\t#suspended = new Signal(false);\n\n\t// Use the serialized URL as the reactive connection key. URL objects use identity\n\t// equality, but replacing one with an equivalent instance should not reconnect.\n\t#url: Getter<string | undefined>;\n\tconstructor(props?: ReloadProps) {\n\t\tthis.url = Signal.from(props?.url);\n\t\tthis.enabled = Signal.from(props?.enabled ?? false);\n\t\tthis.delay = props?.delay ?? { initial: 1000, multiplier: 2, max: 30000 };\n\t\tthis.webtransport = props?.webtransport;\n\t\tthis.websocket = props?.websocket;\n\t\tthis.discovery = props?.discovery;\n\n\t\tthis.#delay = this.delay.initial;\n\n\t\tthis.closed = new Promise((resolve, reject) => {\n\t\t\tthis.#closedResolve = resolve;\n\t\t\tthis.#closedReject = reject;\n\t\t});\n\n\t\tif (typeof window !== \"undefined\" && typeof document !== \"undefined\") {\n\t\t\tthis.#signals.event(window, \"pagehide\", () => this.#suspended.set(true));\n\t\t\tthis.#signals.event(window, \"pageshow\", () => this.#suspended.set(false));\n\t\t\tthis.#signals.event(window, \"unload\", () => this.#suspended.set(true));\n\t\t\tthis.#signals.event(document, \"visibilitychange\", () => {\n\t\t\t\tif (!document.hidden) this.#suspended.set(false);\n\t\t\t});\n\t\t}\n\n\t\tthis.probe = this.#signals.computed((effect) => {\n\t\t\tconst connection = effect.get(this.established);\n\t\t\treturn connection && effect.get(connection.probe);\n\t\t});\n\n\t\tthis.#url = this.#signals.computed((effect) => effect.get(this.url)?.href);\n\t\t// Create a reactive root so cleanup is easier.\n\t\tthis.#signals.run(this.#connect.bind(this));\n\t}\n\n\t#connect(effect: Effect): void {\n\t\t// Will retry when the tick changes.\n\t\teffect.get(this.#tick);\n\n\t\tconst suspended = effect.get(this.#suspended);\n\t\tconst enabled = effect.get(this.enabled);\n\t\tif (!enabled || suspended) return;\n\n\t\tconst href = effect.get(this.#url);\n\t\tif (!href) return;\n\t\tconst url = new URL(href);\n\n\t\teffect.set(this.status, \"connecting\", \"disconnected\");\n\n\t\t// This run's teardown, handed to connect() so a rerun cancels the attempt in flight.\n\t\tconst signal = effect.abort;\n\n\t\teffect.spawn(async () => {\n\t\t\t// Set once the session is live, so #retry can tell a healthy session that\n\t\t\t// later dropped from a connect failure or a peer that flaps immediately.\n\t\t\tlet connected: DOMHighResTimeStamp | undefined;\n\n\t\t\ttry {\n\t\t\t\tconst connection = await connect(url, {\n\t\t\t\t\twebsocket: this.websocket,\n\t\t\t\t\twebtransport: this.webtransport,\n\t\t\t\t\tdiscovery: this.discovery,\n\t\t\t\t\tsignal,\n\t\t\t\t});\n\n\t\t\t\t// Hand the connection to the effect, which closes it now if this run is already over.\n\t\t\t\teffect.cleanup(() => connection.close());\n\t\t\t\tif (signal.aborted) return;\n\n\t\t\t\teffect.set(this.established, connection);\n\t\t\t\teffect.set(this.status, \"connected\", \"disconnected\");\n\n\t\t\t\tconnected = performance.now();\n\n\t\t\t\t// A cancelled effect resolves undefined, so the sentinel tells the session\n\t\t\t\t// closing apart from this run being torn down.\n\t\t\t\tconst closed = await Promise.race([effect.cancel, connection.closed.then(() => true)]);\n\t\t\t\tif (!closed) return;\n\n\t\t\t\tconsole.warn(\"connection closed, reconnecting\");\n\t\t\t\tthis.#retry(effect, connected);\n\t\t\t} catch (err) {\n\t\t\t\t// Treat teardown as cancellation, not a connection failure.\n\t\t\t\tif (signal.aborted) return;\n\n\t\t\t\tconsole.warn(\"connection error:\", err);\n\t\t\t\tthis.#retry(effect, connected, err);\n\t\t\t}\n\t\t});\n\t}\n\n\t/**\n\t * Schedule the next connect attempt after the current backoff, or give up when the\n\t * retry window has expired. `connected` is when the dead session was established, if\n\t * it ever was, and `cause` the error that killed it, if it died with one.\n\t */\n\t#retry(effect: Effect, connected: DOMHighResTimeStamp | undefined, cause?: unknown): void {\n\t\t// Any session is dead now: report disconnected during the backoff rather than\n\t\t// when the retry reruns the effect.\n\t\tthis.established.set(undefined);\n\t\tthis.status.set(\"disconnected\");\n\n\t\t// A session that outlived the initial delay was healthy, so clear the backoff and\n\t\t// start a fresh retry window: a one-off drop should reconnect promptly. Anything\n\t\t// shorter is a peer that accepts and immediately severs, which has to keep\n\t\t// escalating or we hammer it forever at the initial delay.\n\t\tif (connected !== undefined && performance.now() - connected >= this.delay.initial) {\n\t\t\tthis.#delay = this.delay.initial;\n\t\t\tthis.#retryStart = undefined;\n\t\t}\n\n\t\t// Track retry start for timeout.\n\t\tthis.#retryStart ??= performance.now();\n\n\t\tconst timeout = this.delay.timeout ?? 300000;\n\t\tif (timeout > 0) {\n\t\t\tconst elapsed = performance.now() - this.#retryStart;\n\t\t\tif (elapsed >= timeout) {\n\t\t\t\tconsole.warn(\"reconnect timed out\");\n\t\t\t\t// A graceful close has no error, so report the timeout itself.\n\t\t\t\tif (cause === undefined) this.#closedReject(new Error(\"reconnect timed out\"));\n\t\t\t\telse this.#closedReject(cause instanceof Error ? cause : new Error(String(cause)));\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\n\t\tconst tick = this.#tick.peek() + 1;\n\t\teffect.timer(() => this.#tick.update((prev) => Math.max(prev, tick)), this.#delay);\n\n\t\tthis.#delay = Math.min(this.#delay * this.delay.multiplier, this.delay.max);\n\t}\n\n\t/**\n\t * Subscribe to broadcast announcements under an optional prefix, spanning reconnects.\n\t *\n\t * The same {@link Announce.Consumer} stream as {@link Established.announced}, but everything active\n\t * is retracted (an `active: false` update) whenever the connection drops and re-announced on\n\t * reconnect, so a consumer draining `next()` never clings to a dead route across a reconnect.\n\t *\n\t * Stays empty while the relay lacks {@link Established.discovery}.\n\t */\n\tannounced(prefix: Path.Valid = emptyPath()): Announce.Consumer {\n\t\tconst producer = new Announce.Producer(prefix);\n\t\tconst consumer = producer.consume();\n\n\t\t// Closing the consumer closes the shared state, so stop appending after that.\n\t\tlet closed = false;\n\t\tvoid consumer.closed.then(() => {\n\t\t\tclosed = true;\n\t\t});\n\n\t\tconst pump = new Effect();\n\t\tpump.run((effect) => {\n\t\t\tconst conn = effect.get(this.established);\n\t\t\tif (!conn) return;\n\n\t\t\t// Without discovery the upstream announce stream never yields, so leave the\n\t\t\t// consumer empty rather than opening a subscription that can't be answered.\n\t\t\tif (!conn.discovery) return;\n\n\t\t\tconst upstream = conn.announced(prefix);\n\t\t\teffect.cleanup(() => upstream.close());\n\n\t\t\t// Track what this connection announced so we can retract it if the connection drops.\n\t\t\tconst active = new Set<Path.Valid>();\n\n\t\t\teffect.spawn(async () => {\n\t\t\t\ttry {\n\t\t\t\t\tfor (;;) {\n\t\t\t\t\t\tconst entry = await Promise.race([effect.cancel, upstream.next()]);\n\t\t\t\t\t\tif (!entry) break;\n\t\t\t\t\t\tif (entry.active) active.add(entry.path);\n\t\t\t\t\t\telse active.delete(entry.path);\n\t\t\t\t\t\tproducer.append(entry);\n\t\t\t\t\t}\n\t\t\t\t} catch {\n\t\t\t\t\t// A dropped connection resets the announce stream; the retractions below cover it.\n\t\t\t\t} finally {\n\t\t\t\t\t// Retract everything from the connection that just went away, so a per-broadcast\n\t\t\t\t\t// watcher tears down instead of clinging to the dead route.\n\t\t\t\t\tif (!closed) {\n\t\t\t\t\t\tfor (const path of active) {\n\t\t\t\t\t\t\tproducer.append({ path, active: false });\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t});\n\t\t});\n\n\t\tthis.#signals.cleanup(() => pump.close());\n\t\tvoid consumer.closed.then(() => pump.close());\n\n\t\treturn consumer;\n\t}\n\n\t/**\n\t * Snapshot the live connection's transport counters, or undefined while disconnected.\n\t * See {@link Established.stats}.\n\t */\n\tasync stats(): Promise<Stats | undefined> {\n\t\treturn this.established.peek()?.stats();\n\t}\n\n\t/** Stop reconnecting, close the current connection, and resolve {@link Reload.closed}. */\n\tclose() {\n\t\tthis.#signals.close();\n\t\t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,MAAM,EAAE,CAAC;4BAC3B,QAAQ,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC;wBAC1C,CAAC;oBACF,CAAC;gBACF,CAAC;YACF,CAAC,CAAC,CAAC;QACJ,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC;QAC1C,KAAK,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC;QAE9C,OAAO,QAAQ,CAAC;IACjB,CAAC;IAED;;;;;;;;;;OAUG;IACH,kBAAkB,CAAC,IAAgB;QAClC,OAAO,IAAI,QAAQ,CAAC,SAAS,CAAC,EAAE,UAAU,EAAE,IAAI,CAAC,WAAW,EAAE,IAAI,EAAE,CAAC,CAAC;IACvE,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,KAAK;QACV,OAAO,IAAI,CAAC,WAAW,CAAC,IAAI,EAAE,EAAE,KAAK,EAAE,CAAC;IACzC,CAAC;IAED,0FAA0F;IAC1F,KAAK;QACJ,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC;QACtB,IAAI,CAAC,cAAc,EAAE,CAAC;IACvB,CAAC;CACD","sourcesContent":["import { Effect, type Getter, Signal } from \"@moq/signals\";\nimport * as Announce from \"../announced.ts\";\nimport { error } from \"../error.ts\";\nimport type * as Path from \"../path.ts\";\nimport { empty as emptyPath } from \"../path.ts\";\nimport { type ConnectProps, connect, type WebSocketOptions, type WebTransportProps } from \"./connect.ts\";\nimport type { Established } from \"./established.ts\";\nimport type { Probe, Stats } from \"./stats.ts\";\n\n/**\n * Exponential backoff settings for {@link Reload}'s reconnect loop.\n *\n * The delays carry jitter, so a fleet of tabs knocked offline together doesn't reconnect in\n * lockstep. Every failure is retried; {@link ReloadDelay.timeout} is what stops the loop.\n */\nexport type ReloadDelay = {\n\t/** The delay in milliseconds before reconnecting (default: 1000). */\n\tinitial: DOMHighResTimeStamp;\n\n\t/** The multiplier for the delay (default: 2). */\n\tmultiplier: number;\n\n\t/** The maximum delay in milliseconds (default: 5000). */\n\tmax: DOMHighResTimeStamp;\n\n\t/**\n\t * Maximum total time in milliseconds to spend retrying before giving up (default:\n\t * 10000). Resets after each successful connection. Set to 0 for unlimited retries.\n\t */\n\ttimeout?: DOMHighResTimeStamp;\n};\n\n/** Connection and retry options for {@link Reload}. */\nexport type ReloadProps = Omit<ConnectProps, \"signal\"> & {\n\t/** Whether to reload the connection when it disconnects (default: true). */\n\tenabled?: boolean | Signal<boolean>;\n\n\t/** The URL of the relay server. */\n\turl?: URL | Signal<URL | undefined>;\n\n\t/** Backoff settings for the reconnect loop. */\n\tdelay?: ReloadDelay;\n};\n\n/**\n * How long to keep retrying before giving up, when {@link ReloadDelay.timeout} is unset.\n *\n * Short on purpose: a failure that clears within it was transient, and one that doesn't should\n * surface as an error rather than leave the page silently reconnecting for minutes.\n */\nconst DEFAULT_TIMEOUT = 10000;\n\n/** Current state of a {@link Reload} connection. */\nexport type ReloadStatus = \"connecting\" | \"connected\" | \"disconnected\";\n\n/** Maintains a MoQ connection, reconnecting with exponential backoff when it drops. */\nexport class Reload {\n\t/** Relay URL to connect to; updating it triggers a reconnect. */\n\turl: Signal<URL | undefined>;\n\n\t/** Whether reconnecting is active. */\n\tenabled: Signal<boolean>;\n\n\t/** Current connection status. */\n\tstatus = new Signal<ReloadStatus>(\"disconnected\");\n\n\t/** The currently established session, or undefined while disconnected. */\n\testablished = new Signal<Established | undefined>(undefined);\n\n\t/**\n\t * The current connection's PROBE estimates, spanning reconnects.\n\t *\n\t * Undefined while disconnected: the estimates belong to a single connection.\n\t * See {@link Established.probe}.\n\t */\n\treadonly probe: Getter<Probe | undefined>;\n\n\t/** WebTransport options applied to each connection attempt (not reactive). */\n\twebtransport?: WebTransportProps;\n\n\t/** WebSocket fallback options applied to each connection attempt (not reactive). */\n\twebsocket: WebSocketOptions | undefined;\n\n\t/**\n\t * Whether the relay supports broadcast discovery, applied to each connection attempt (not\n\t * reactive). Undefined defers to the default for the URL. See {@link Established.discovery}.\n\t */\n\tdiscovery?: boolean;\n\n\t/** Backoff settings for the reconnect loop. */\n\tdelay: ReloadDelay;\n\n\t/** The reactive effect scope driving the connect loop; closed by {@link Reload.close}. */\n\t#signals = new Effect();\n\n\t/**\n\t * Resolves when the reconnect loop stops via {@link Reload.close}.\n\t *\n\t * Rejects when the loop gives up instead, carrying the failure that was in flight when the\n\t * retry window expired.\n\t */\n\tclosed: Promise<void>;\n\t#closedResolve!: () => void;\n\t#closedReject!: (err: Error) => void;\n\n\t// The current wait between attempts, doubling per failure, and when the retry window expires.\n\t// Both are undefined between sequences, so a later edit to `delay` applies to the next one.\n\t#delay: DOMHighResTimeStamp | undefined;\n\t#deadline: DOMHighResTimeStamp | undefined;\n\n\t// Increased by 1 each time to trigger a reload.\n\t#tick = new Signal(0);\n\n\t// True after the browser freezes or hides the page until it visibly resumes.\n\t#suspended = new Signal(false);\n\n\t// Use the serialized URL as the reactive connection key. URL objects use identity\n\t// equality, but replacing one with an equivalent instance should not reconnect.\n\t#url: Getter<string | undefined>;\n\tconstructor(props?: ReloadProps) {\n\t\tthis.url = Signal.from(props?.url);\n\t\tthis.enabled = Signal.from(props?.enabled ?? false);\n\t\tthis.delay = props?.delay ?? { initial: 1000, multiplier: 2, max: 5000 };\n\t\tthis.webtransport = props?.webtransport;\n\t\tthis.websocket = props?.websocket;\n\t\tthis.discovery = props?.discovery;\n\n\t\tthis.closed = new Promise((resolve, reject) => {\n\t\t\tthis.#closedResolve = resolve;\n\t\t\tthis.#closedReject = reject;\n\t\t});\n\n\t\t// A caller is free to never await `closed`, and giving up rejects it unprompted. Marking the\n\t\t// rejection handled here keeps that from surfacing as an `unhandledrejection`; a consumer\n\t\t// awaiting the same promise still receives it.\n\t\tthis.closed.catch(() => {});\n\n\t\tif (typeof window !== \"undefined\" && typeof document !== \"undefined\") {\n\t\t\tthis.#signals.event(window, \"pagehide\", () => this.#suspended.set(true));\n\t\t\tthis.#signals.event(window, \"pageshow\", () => this.#suspended.set(false));\n\t\t\tthis.#signals.event(window, \"unload\", () => this.#suspended.set(true));\n\t\t\tthis.#signals.event(document, \"visibilitychange\", () => {\n\t\t\t\tif (!document.hidden) this.#suspended.set(false);\n\t\t\t});\n\t\t}\n\n\t\tthis.probe = this.#signals.computed((effect) => {\n\t\t\tconst connection = effect.get(this.established);\n\t\t\treturn connection && effect.get(connection.probe);\n\t\t});\n\n\t\tthis.#url = this.#signals.computed((effect) => effect.get(this.url)?.href);\n\t\t// Create a reactive root so cleanup is easier.\n\t\tthis.#signals.run(this.#connect.bind(this));\n\t}\n\n\t#connect(effect: Effect): void {\n\t\t// Will retry when the tick changes.\n\t\teffect.get(this.#tick);\n\n\t\tconst suspended = effect.get(this.#suspended);\n\t\tconst enabled = effect.get(this.enabled);\n\t\tif (!enabled || suspended) return;\n\n\t\tconst href = effect.get(this.#url);\n\t\tif (!href) return;\n\t\tconst url = new URL(href);\n\n\t\teffect.set(this.status, \"connecting\", \"disconnected\");\n\n\t\t// This run's teardown, handed to connect() so a rerun cancels the attempt in flight.\n\t\tconst signal = effect.abort;\n\n\t\teffect.spawn(async () => {\n\t\t\t// Set once the session is live, so #retry can tell a healthy session that\n\t\t\t// later dropped from a connect failure or a peer that flaps immediately.\n\t\t\tlet connected: DOMHighResTimeStamp | undefined;\n\n\t\t\ttry {\n\t\t\t\tconst connection = await connect(url, {\n\t\t\t\t\twebsocket: this.websocket,\n\t\t\t\t\twebtransport: this.webtransport,\n\t\t\t\t\tdiscovery: this.discovery,\n\t\t\t\t\tsignal,\n\t\t\t\t});\n\n\t\t\t\t// Hand the connection to the effect, which closes it now if this run is already over.\n\t\t\t\teffect.cleanup(() => connection.close());\n\t\t\t\tif (signal.aborted) return;\n\n\t\t\t\teffect.set(this.established, connection);\n\t\t\t\teffect.set(this.status, \"connected\", \"disconnected\");\n\n\t\t\t\tconnected = performance.now();\n\n\t\t\t\t// A cancelled effect resolves undefined, so the sentinel tells the session\n\t\t\t\t// closing apart from this run being torn down.\n\t\t\t\tconst closed = await Promise.race([effect.cancel, connection.closed.then(() => true)]);\n\t\t\t\tif (!closed) return;\n\n\t\t\t\tconsole.warn(\"connection closed, reconnecting\");\n\t\t\t\tthis.#retry(effect, connected);\n\t\t\t} catch (err) {\n\t\t\t\t// Treat teardown as cancellation, not a connection failure.\n\t\t\t\tif (signal.aborted) return;\n\n\t\t\t\tconsole.warn(\"connection error:\", err);\n\t\t\t\tthis.#retry(effect, connected, err);\n\t\t\t}\n\t\t});\n\t}\n\n\t/**\n\t * Schedule the next connect attempt after the current backoff, or stop once the retry window\n\t * has expired. `connected` is when the dead session was established, if it ever was, and\n\t * `cause` the error that killed it, if it died with one.\n\t */\n\t#retry(effect: Effect, connected: DOMHighResTimeStamp | undefined, cause?: unknown): void {\n\t\t// Any session is dead now: report disconnected during the backoff rather than\n\t\t// when the retry reruns the effect.\n\t\tthis.established.set(undefined);\n\t\tthis.status.set(\"disconnected\");\n\n\t\t// A session that outlived the initial delay was healthy, so clear the backoff and\n\t\t// start a fresh retry window: a one-off drop should reconnect promptly. Anything\n\t\t// shorter is a peer that accepts and immediately severs, which has to keep\n\t\t// escalating or we hammer it forever at the initial delay.\n\t\tif (connected !== undefined && performance.now() - connected >= this.delay.initial) {\n\t\t\tthis.#delay = undefined;\n\t\t\tthis.#deadline = undefined;\n\t\t}\n\n\t\tconst now = performance.now();\n\t\tconst timeout = this.delay.timeout ?? DEFAULT_TIMEOUT;\n\t\tthis.#delay ??= this.delay.initial;\n\t\tthis.#deadline ??= timeout > 0 ? now + timeout : Number.POSITIVE_INFINITY;\n\n\t\tif (now >= this.#deadline) {\n\t\t\tconsole.warn(\"reconnect timed out\");\n\t\t\t// A graceful close has no error, so report the timeout itself.\n\t\t\tthis.#closedReject(cause === undefined ? new Error(\"reconnect timed out\") : error(cause));\n\t\t\treturn;\n\t\t}\n\n\t\t// Equal jitter, so a fleet of tabs knocked offline together doesn't reconnect on the same\n\t\t// tick, and never past the deadline the retry window promised.\n\t\tconst wait = Math.min(this.#delay * (0.5 + Math.random() / 2), this.#deadline - now);\n\t\tthis.#delay = Math.min(this.#delay * this.delay.multiplier, this.delay.max);\n\n\t\tconst tick = this.#tick.peek() + 1;\n\t\teffect.timer(() => this.#tick.update((prev) => Math.max(prev, tick)), wait);\n\t}\n\n\t/**\n\t * Subscribe to broadcast announcements under an optional prefix, spanning reconnects.\n\t *\n\t * The same {@link Announce.Consumer} stream as {@link Established.announced}, but everything active\n\t * is retracted (an `active: false` update) whenever the connection drops and re-announced on\n\t * reconnect, so a consumer draining `next()` never clings to a dead route across a reconnect.\n\t *\n\t * Stays empty while the relay lacks {@link Established.discovery}.\n\t */\n\tannounced(prefix: Path.Valid = emptyPath()): Announce.Consumer {\n\t\tconst producer = new Announce.Producer(prefix);\n\t\tconst consumer = producer.consume();\n\n\t\t// Closing the consumer closes the shared state, so stop appending after that.\n\t\tlet closed = false;\n\t\tvoid consumer.closed.then(() => {\n\t\t\tclosed = true;\n\t\t});\n\n\t\tconst pump = new Effect();\n\t\tpump.run((effect) => {\n\t\t\tconst conn = effect.get(this.established);\n\t\t\tif (!conn) return;\n\n\t\t\t// Without discovery the upstream announce stream never yields, so leave the\n\t\t\t// consumer empty rather than opening a subscription that can't be answered.\n\t\t\tif (!conn.discovery) return;\n\n\t\t\tconst upstream = conn.announced(prefix);\n\t\t\teffect.cleanup(() => upstream.close());\n\n\t\t\t// Track what this connection announced so we can retract it if the connection drops.\n\t\t\tconst active = new Set<Path.Valid>();\n\n\t\t\teffect.spawn(async () => {\n\t\t\t\ttry {\n\t\t\t\t\tfor (;;) {\n\t\t\t\t\t\tconst entry = await Promise.race([effect.cancel, upstream.next()]);\n\t\t\t\t\t\tif (!entry) break;\n\t\t\t\t\t\tif (entry.active) active.add(entry.path);\n\t\t\t\t\t\telse active.delete(entry.path);\n\t\t\t\t\t\tproducer.append(entry);\n\t\t\t\t\t}\n\t\t\t\t} catch {\n\t\t\t\t\t// A dropped connection resets the announce stream; the retractions below cover it.\n\t\t\t\t} finally {\n\t\t\t\t\t// Retract everything from the connection that just went away, so a per-broadcast\n\t\t\t\t\t// watcher tears down instead of clinging to the dead route.\n\t\t\t\t\tif (!closed) {\n\t\t\t\t\t\tfor (const path of active) {\n\t\t\t\t\t\t\tproducer.append({ path, active: false });\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t});\n\t\t});\n\n\t\tthis.#signals.cleanup(() => pump.close());\n\t\tvoid consumer.closed.then(() => pump.close());\n\n\t\treturn consumer;\n\t}\n\n\t/**\n\t * A reactive handle to one broadcast, spanning reconnects.\n\t *\n\t * The same {@link Announce.Broadcast} as {@link Established.announcedBroadcast}, but it\n\t * follows the reconnect loop: the broadcast drops to `undefined` when the connection dies\n\t * and resolves again once the new connection announces the path. Use it instead of\n\t * consuming off {@link Reload.established} whenever the broadcast may come online after you\n\t * do, which is exactly the case a blind `consume` loses.\n\t *\n\t * Close the handle when done; {@link Reload.close} only drops it to `undefined`.\n\t */\n\tannouncedBroadcast(path: Path.Valid): Announce.Broadcast {\n\t\treturn new Announce.Broadcast({ connection: this.established, path });\n\t}\n\n\t/**\n\t * Snapshot the live connection's transport counters, or undefined while disconnected.\n\t * See {@link Established.stats}.\n\t */\n\tasync stats(): Promise<Stats | undefined> {\n\t\treturn this.established.peek()?.stats();\n\t}\n\n\t/** Stop reconnecting, close the current connection, and resolve {@link Reload.closed}. */\n\tclose() {\n\t\tthis.#signals.close();\n\t\tthis.#closedResolve();\n\t}\n}\n"]}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
/* @ts-self-types="./reload.test.d.ts" */
|
|
2
2
|
import { expect, test } from "bun:test";
|
|
3
|
+
import { Producer as BroadcastProducer } from "../broadcast.js";
|
|
3
4
|
import * as Lite from "../lite/index.js";
|
|
4
5
|
import { createMockTransportPair } from "../mock.js";
|
|
6
|
+
import * as Path from "../path.js";
|
|
5
7
|
import { accept } from "./index.js";
|
|
6
8
|
import { Reload } from "./reload.js";
|
|
7
9
|
async function settle() {
|
|
@@ -101,4 +103,58 @@ test("a peer that severs immediately keeps escalating the backoff", async () =>
|
|
|
101
103
|
globalThis.WebTransport = original;
|
|
102
104
|
}
|
|
103
105
|
});
|
|
106
|
+
// Polls until `pred` holds, so a regression fails the test instead of hanging it.
|
|
107
|
+
async function waitUntil(pred) {
|
|
108
|
+
for (let i = 0; i < 500; i++) {
|
|
109
|
+
if (pred())
|
|
110
|
+
return;
|
|
111
|
+
await settle();
|
|
112
|
+
}
|
|
113
|
+
throw new Error("timed out waiting for condition");
|
|
114
|
+
}
|
|
115
|
+
test("announcedBroadcast follows the reconnect loop", async () => {
|
|
116
|
+
const original = globalThis.WebTransport;
|
|
117
|
+
const url = new URL("https://example.com/");
|
|
118
|
+
// Every connect attempt gets a fresh session, whose server publishes the path once the
|
|
119
|
+
// handshake finishes. The client therefore always asks before the broadcast exists.
|
|
120
|
+
const sessions = [];
|
|
121
|
+
const published = [];
|
|
122
|
+
const stub = function StubWebTransport() {
|
|
123
|
+
const pair = createMockTransportPair(Lite.ALPN_06_WIP);
|
|
124
|
+
void accept(pair.server, url).then((server) => {
|
|
125
|
+
sessions.push(server);
|
|
126
|
+
const broadcast = new BroadcastProducer();
|
|
127
|
+
published.push(broadcast);
|
|
128
|
+
server.publish(Path.from("late"), broadcast);
|
|
129
|
+
});
|
|
130
|
+
return pair.client;
|
|
131
|
+
};
|
|
132
|
+
globalThis.WebTransport = stub;
|
|
133
|
+
const reload = new Reload({
|
|
134
|
+
enabled: true,
|
|
135
|
+
url,
|
|
136
|
+
websocket: { enabled: false },
|
|
137
|
+
delay: { initial: 10, multiplier: 1, max: 10 },
|
|
138
|
+
});
|
|
139
|
+
const watched = reload.announcedBroadcast(Path.from("late"));
|
|
140
|
+
try {
|
|
141
|
+
await waitUntil(() => watched.active.peek() !== undefined);
|
|
142
|
+
const first = watched.active.peek();
|
|
143
|
+
// The session dies: the handle drops the broadcast rather than clinging to a dead one.
|
|
144
|
+
sessions[0]?.close();
|
|
145
|
+
await waitUntil(() => watched.active.peek() === undefined);
|
|
146
|
+
// The reconnect re-announces it, and the handle re-consumes on the new session.
|
|
147
|
+
await waitUntil(() => watched.active.peek() !== undefined);
|
|
148
|
+
expect(watched.active.peek()).not.toBe(first);
|
|
149
|
+
}
|
|
150
|
+
finally {
|
|
151
|
+
watched.close();
|
|
152
|
+
reload.close();
|
|
153
|
+
for (const broadcast of published)
|
|
154
|
+
broadcast.close();
|
|
155
|
+
for (const session of sessions)
|
|
156
|
+
session.close();
|
|
157
|
+
globalThis.WebTransport = original;
|
|
158
|
+
}
|
|
159
|
+
});
|
|
104
160
|
//# sourceMappingURL=reload.test.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"reload.test.js","sourceRoot":"","sources":["../../src/connection/reload.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AACxC,OAAO,KAAK,IAAI,MAAM,kBAAkB,CAAC;AACzC,OAAO,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAC;AACrD,OAAO,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AACpC,OAAO,EAAE,MAAM,EAAoB,MAAM,aAAa,CAAC;AAEvD,KAAK,UAAU,MAAM;IACpB,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC;AACxD,CAAC;AAED,IAAI,CAAC,8DAA8D,EAAE,KAAK,IAAI,EAAE;IAC/E,MAAM,QAAQ,GAAG,UAAU,CAAC,YAAY,CAAC;IACzC,IAAI,QAAQ,GAAG,CAAC,CAAC;IAEjB,MAAM,mBAAmB;QACxB,KAAK,GAAG,IAAI,OAAO,CAAO,GAAG,EA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ACvE,KAAK,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC;QAC/D,OAAO,IAAI,CAAC,MAAM,CAAC;IACpB,CAAC,CAAC;IACF,UAAU,CAAC,YAAY,GAAG,IAAsC,CAAC;IAEjE,kFAAkF;IAClF,kFAAkF;IAClF,8DAA8D;IAC9D,EAAE;IACF,oFAAoF;IACpF,yEAAyE;IACzE,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC;QACzB,OAAO,EAAE,IAAI;QACb,GAAG;QACH,SAAS,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE;QAC7B,KAAK,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC,EAAE,GAAG,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,EAAE;KAC9D,CAAC,CAAC;IACH,IAAI,CAAC;QACJ,MAAM,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC;IAC/C,CAAC;YAAS,CAAC;QACV,MAAM,CAAC,KAAK,EAAE,CAAC;QACf,UAAU,CAAC,YAAY,GAAG,QAAQ,CAAC;IACpC,CAAC;AACF,CAAC,CAAC,CAAC","sourcesContent":["import { expect, test } from \"bun:test\";\nimport * as Lite from \"../lite/index.ts\";\nimport { createMockTransportPair } from \"../mock.ts\";\nimport { accept } from \"./index.ts\";\nimport { Reload, type ReloadProps } from \"./reload.ts\";\n\nasync function settle() {\n\tawait new Promise((resolve) => setTimeout(resolve, 0));\n}\n\ntest(\"equivalent URL instances do not restart a pending connection\", async () => {\n\tconst original = globalThis.WebTransport;\n\tlet connects = 0;\n\n\tclass PendingWebTransport {\n\t\tready = new Promise<void>(() => {});\n\t\tclosed = new Promise<void>(() => {});\n\n\t\tconstructor() {\n\t\t\tconnects++;\n\t\t}\n\n\t\tclose() {}\n\t}\n\n\tglobalThis.WebTransport = PendingWebTransport as unknown as typeof WebTransport;\n\tconst reload = new Reload({\n\t\tenabled: true,\n\t\turl: new URL(\"https://example.com/broadcast\"),\n\t\twebsocket: { enabled: false },\n\t});\n\n\ttry {\n\t\tawait settle();\n\t\texpect(connects).toBe(1);\n\n\t\treload.url.set(new URL(\"https://example.com/broadcast\"));\n\t\tawait settle();\n\t\texpect(connects).toBe(1);\n\n\t\treload.url.set(new URL(\"https://example.com/other\"));\n\t\tawait settle();\n\t\texpect(connects).toBe(2);\n\t} finally {\n\t\treload.close();\n\t\tglobalThis.WebTransport = original;\n\t}\n});\n\ntest(\"ReloadProps excludes signal\", () => {\n\t// @ts-expect-error signal is not part of ReloadProps\n\tconst props: ReloadProps = { signal: new AbortController().signal };\n\texpect(props.enabled).toBeUndefined();\n});\n\ntest(\"closing mid-connect aborts the pending attempt\", async () => {\n\tconst original = globalThis.WebTransport;\n\tlet closes = 0;\n\n\tclass PendingWebTransport {\n\t\tready = new Promise<void>(() => {});\n\t\tclosed = new Promise<void>(() => {});\n\n\t\tclose() {\n\t\t\tcloses++;\n\t\t}\n\t}\n\n\tglobalThis.WebTransport = PendingWebTransport as unknown as typeof WebTransport;\n\tconst reload = new Reload({\n\t\tenabled: true,\n\t\turl: new URL(\"https://example.com/broadcast\"),\n\t\twebsocket: { enabled: false },\n\t});\n\n\ttry {\n\t\tawait settle();\n\t\texpect(closes).toBe(0);\n\n\t\treload.close();\n\t\tawait settle();\n\t\texpect(closes).toBe(1);\n\t} finally {\n\t\tglobalThis.WebTransport = original;\n\t}\n});\n\ntest(\"a peer that severs immediately keeps escalating the backoff\", async () => {\n\tconst original = globalThis.WebTransport;\n\tconst url = new URL(\"https://example.com/\");\n\tconst stub = function StubWebTransport() {\n\t\tconst pair = createMockTransportPair(Lite.ALPN_06_WIP);\n\t\t// Sever the session as soon as the server side finishes the handshake.\n\t\tvoid accept(pair.server, url).then((server) => server.close());\n\t\treturn pair.client;\n\t};\n\tglobalThis.WebTransport = stub as unknown as typeof WebTransport;\n\n\t// Every session dies well within `initial`, so the backoff has to keep escalating\n\t// and the retry window has to expire. Resetting either on each successful connect\n\t// reconnects forever at the initial delay and never gives up.\n\t//\n\t// `initial` sits far above the in-process handshake so a loaded runner can't make a\n\t// session look healthy, and the tiny timeout gives up after one backoff.\n\tconst reload = new Reload({\n\t\tenabled: true,\n\t\turl,\n\t\twebsocket: { enabled: false },\n\t\tdelay: { initial: 1000, multiplier: 2, max: 1000, timeout: 1 },\n\t});\n\ttry {\n\t\tawait expect(reload.closed).rejects.toThrow();\n\t} finally {\n\t\treload.close();\n\t\tglobalThis.WebTransport = original;\n\t}\n});\n"]}
|
|
1
|
+
{"version":3,"file":"reload.test.js","sourceRoot":"","sources":["../../src/connection/reload.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AACxC,OAAO,EAAE,QAAQ,IAAI,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAChE,OAAO,KAAK,IAAI,MAAM,kBAAkB,CAAC;AACzC,OAAO,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAC;AACrD,OAAO,KAAK,IAAI,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AACpC,OAAO,EAAE,MAAM,EAAoB,MAAM,aAAa,CAAC;AAEvD,KAAK,UAAU,MAAM;IACpB,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC;AACxD,CAAC;AAED,IAAI,CAAC,8DAA8D,EAAE,KAAK,IAAI,EAAE;IAC/E,MAAM,QAAQ,GAAG,UAAU,CAAC,YAAY,CAAC;IACzC,IAAI,QAAQ,GAAG,CAAC,CAAC;IAEjB,MAAM,mBAAmB;QACxB,KAAK,GAAG,IAAI,OAAO,CAAO,GAAG,EAAE,GAAE,CAAC,CAAC,CA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,uFAAuF;QACvF,QAAQ,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC;QACrB,MAAM,SAAS,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,SAAS,CAAC,CAAC;QAE3D,gFAAgF;QAChF,MAAM,SAAS,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,SAAS,CAAC,CAAC;QAC3D,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC/C,CAAC;YAAS,CAAC;QACV,OAAO,CAAC,KAAK,EAAE,CAAC;QAChB,MAAM,CAAC,KAAK,EAAE,CAAC;QACf,KAAK,MAAM,SAAS,IAAI,SAAS;YAAE,SAAS,CAAC,KAAK,EAAE,CAAC;QACrD,KAAK,MAAM,OAAO,IAAI,QAAQ;YAAE,OAAO,CAAC,KAAK,EAAE,CAAC;QAChD,UAAU,CAAC,YAAY,GAAG,QAAQ,CAAC;IACpC,CAAC;AACF,CAAC,CAAC,CAAC","sourcesContent":["import { expect, test } from \"bun:test\";\nimport { Producer as BroadcastProducer } from \"../broadcast.ts\";\nimport * as Lite from \"../lite/index.ts\";\nimport { createMockTransportPair } from \"../mock.ts\";\nimport * as Path from \"../path.ts\";\nimport { accept } from \"./index.ts\";\nimport { Reload, type ReloadProps } from \"./reload.ts\";\n\nasync function settle() {\n\tawait new Promise((resolve) => setTimeout(resolve, 0));\n}\n\ntest(\"equivalent URL instances do not restart a pending connection\", async () => {\n\tconst original = globalThis.WebTransport;\n\tlet connects = 0;\n\n\tclass PendingWebTransport {\n\t\tready = new Promise<void>(() => {});\n\t\tclosed = new Promise<void>(() => {});\n\n\t\tconstructor() {\n\t\t\tconnects++;\n\t\t}\n\n\t\tclose() {}\n\t}\n\n\tglobalThis.WebTransport = PendingWebTransport as unknown as typeof WebTransport;\n\tconst reload = new Reload({\n\t\tenabled: true,\n\t\turl: new URL(\"https://example.com/broadcast\"),\n\t\twebsocket: { enabled: false },\n\t});\n\n\ttry {\n\t\tawait settle();\n\t\texpect(connects).toBe(1);\n\n\t\treload.url.set(new URL(\"https://example.com/broadcast\"));\n\t\tawait settle();\n\t\texpect(connects).toBe(1);\n\n\t\treload.url.set(new URL(\"https://example.com/other\"));\n\t\tawait settle();\n\t\texpect(connects).toBe(2);\n\t} finally {\n\t\treload.close();\n\t\tglobalThis.WebTransport = original;\n\t}\n});\n\ntest(\"ReloadProps excludes signal\", () => {\n\t// @ts-expect-error signal is not part of ReloadProps\n\tconst props: ReloadProps = { signal: new AbortController().signal };\n\texpect(props.enabled).toBeUndefined();\n});\n\ntest(\"closing mid-connect aborts the pending attempt\", async () => {\n\tconst original = globalThis.WebTransport;\n\tlet closes = 0;\n\n\tclass PendingWebTransport {\n\t\tready = new Promise<void>(() => {});\n\t\tclosed = new Promise<void>(() => {});\n\n\t\tclose() {\n\t\t\tcloses++;\n\t\t}\n\t}\n\n\tglobalThis.WebTransport = PendingWebTransport as unknown as typeof WebTransport;\n\tconst reload = new Reload({\n\t\tenabled: true,\n\t\turl: new URL(\"https://example.com/broadcast\"),\n\t\twebsocket: { enabled: false },\n\t});\n\n\ttry {\n\t\tawait settle();\n\t\texpect(closes).toBe(0);\n\n\t\treload.close();\n\t\tawait settle();\n\t\texpect(closes).toBe(1);\n\t} finally {\n\t\tglobalThis.WebTransport = original;\n\t}\n});\n\ntest(\"a peer that severs immediately keeps escalating the backoff\", async () => {\n\tconst original = globalThis.WebTransport;\n\tconst url = new URL(\"https://example.com/\");\n\tconst stub = function StubWebTransport() {\n\t\tconst pair = createMockTransportPair(Lite.ALPN_06_WIP);\n\t\t// Sever the session as soon as the server side finishes the handshake.\n\t\tvoid accept(pair.server, url).then((server) => server.close());\n\t\treturn pair.client;\n\t};\n\tglobalThis.WebTransport = stub as unknown as typeof WebTransport;\n\n\t// Every session dies well within `initial`, so the backoff has to keep escalating\n\t// and the retry window has to expire. Resetting either on each successful connect\n\t// reconnects forever at the initial delay and never gives up.\n\t//\n\t// `initial` sits far above the in-process handshake so a loaded runner can't make a\n\t// session look healthy, and the tiny timeout gives up after one backoff.\n\tconst reload = new Reload({\n\t\tenabled: true,\n\t\turl,\n\t\twebsocket: { enabled: false },\n\t\tdelay: { initial: 1000, multiplier: 2, max: 1000, timeout: 1 },\n\t});\n\ttry {\n\t\tawait expect(reload.closed).rejects.toThrow();\n\t} finally {\n\t\treload.close();\n\t\tglobalThis.WebTransport = original;\n\t}\n});\n\n// Polls until `pred` holds, so a regression fails the test instead of hanging it.\nasync function waitUntil(pred: () => boolean): Promise<void> {\n\tfor (let i = 0; i < 500; i++) {\n\t\tif (pred()) return;\n\t\tawait settle();\n\t}\n\tthrow new Error(\"timed out waiting for condition\");\n}\n\ntest(\"announcedBroadcast follows the reconnect loop\", async () => {\n\tconst original = globalThis.WebTransport;\n\tconst url = new URL(\"https://example.com/\");\n\n\t// Every connect attempt gets a fresh session, whose server publishes the path once the\n\t// handshake finishes. The client therefore always asks before the broadcast exists.\n\tconst sessions: { close: () => void }[] = [];\n\tconst published: BroadcastProducer[] = [];\n\tconst stub = function StubWebTransport() {\n\t\tconst pair = createMockTransportPair(Lite.ALPN_06_WIP);\n\t\tvoid accept(pair.server, url).then((server) => {\n\t\t\tsessions.push(server);\n\t\t\tconst broadcast = new BroadcastProducer();\n\t\t\tpublished.push(broadcast);\n\t\t\tserver.publish(Path.from(\"late\"), broadcast);\n\t\t});\n\t\treturn pair.client;\n\t};\n\tglobalThis.WebTransport = stub as unknown as typeof WebTransport;\n\n\tconst reload = new Reload({\n\t\tenabled: true,\n\t\turl,\n\t\twebsocket: { enabled: false },\n\t\tdelay: { initial: 10, multiplier: 1, max: 10 },\n\t});\n\tconst watched = reload.announcedBroadcast(Path.from(\"late\"));\n\n\ttry {\n\t\tawait waitUntil(() => watched.active.peek() !== undefined);\n\t\tconst first = watched.active.peek();\n\n\t\t// The session dies: the handle drops the broadcast rather than clinging to a dead one.\n\t\tsessions[0]?.close();\n\t\tawait waitUntil(() => watched.active.peek() === undefined);\n\n\t\t// The reconnect re-announces it, and the handle re-consumes on the new session.\n\t\tawait waitUntil(() => watched.active.peek() !== undefined);\n\t\texpect(watched.active.peek()).not.toBe(first);\n\t} finally {\n\t\twatched.close();\n\t\treload.close();\n\t\tfor (const broadcast of published) broadcast.close();\n\t\tfor (const session of sessions) session.close();\n\t\tglobalThis.WebTransport = original;\n\t}\n});\n"]}
|
package/consume.d.ts
CHANGED
|
@@ -22,5 +22,23 @@ export declare class BroadcastCache {
|
|
|
22
22
|
* Call on a {@link get} miss, after wiring up the fresh consumer's subscribe loop.
|
|
23
23
|
*/
|
|
24
24
|
insert(path: Path.Valid, consumer: broadcast.Consumer): broadcast.Consumer;
|
|
25
|
+
/**
|
|
26
|
+
* Stop sharing the broadcast cached for `path`, so the next request subscribes fresh.
|
|
27
|
+
*
|
|
28
|
+
* Call when the path's advertisement goes away. A handle only leaves the cache on its own
|
|
29
|
+
* once *every* holder has closed it, so one holder outliving the publisher (a second
|
|
30
|
+
* watcher, or a caller consuming the path directly) would otherwise keep the dead
|
|
31
|
+
* generation's cached tracks alive and hand them to whoever consumes the path next.
|
|
32
|
+
* Existing handles are left alone: they belong to their holders, and the wire resets
|
|
33
|
+
* whatever they still have open.
|
|
34
|
+
*
|
|
35
|
+
* Eviction is unconditional, which costs a dedup miss when two announcement streams watch
|
|
36
|
+
* one path: the second stream's retraction can arrive after the first has already seen the
|
|
37
|
+
* replacement, dropping the fresh entry so the next request subscribes again instead of
|
|
38
|
+
* sharing. Telling that stale retraction from a live one needs a generation id on the
|
|
39
|
+
* advertisement (moq-lite's `Epoch`, not yet on the wire), so until then this errs toward a
|
|
40
|
+
* duplicate subscription rather than risk handing out a dead one.
|
|
41
|
+
*/
|
|
42
|
+
evict(path: Path.Valid): void;
|
|
25
43
|
}
|
|
26
44
|
//# sourceMappingURL=consume.d.ts.map
|
package/consume.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"consume.d.ts","sourceRoot":"","sources":["../src/consume.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,SAAS,MAAM,gBAAgB,CAAC;AACjD,OAAO,KAAK,KAAK,IAAI,MAAM,WAAW,CAAC;AAEvC;;;;;;;;;;;;GAYG;AACH,qBAAa,cAAc;;IAI1B,yFAAyF;IACzF,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,KAAK,GAAG,SAAS,CAAC,QAAQ,GAAG,SAAS,CAIpD;IAED;;;OAGG;IACH,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,KAAK,EAAE,QAAQ,EAAE,SAAS,CAAC,QAAQ,GAAG,SAAS,CAAC,QAAQ,CAUzE;CACD"}
|
|
1
|
+
{"version":3,"file":"consume.d.ts","sourceRoot":"","sources":["../src/consume.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,SAAS,MAAM,gBAAgB,CAAC;AACjD,OAAO,KAAK,KAAK,IAAI,MAAM,WAAW,CAAC;AAEvC;;;;;;;;;;;;GAYG;AACH,qBAAa,cAAc;;IAI1B,yFAAyF;IACzF,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,KAAK,GAAG,SAAS,CAAC,QAAQ,GAAG,SAAS,CAIpD;IAED;;;OAGG;IACH,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,KAAK,EAAE,QAAQ,EAAE,SAAS,CAAC,QAAQ,GAAG,SAAS,CAAC,QAAQ,CAUzE;IAED;;;;;;;;;;;;;;;;OAgBG;IACH,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,KAAK,GAAG,IAAI,CAE5B;CACD"}
|
package/consume.js
CHANGED
|
@@ -36,5 +36,25 @@ export class BroadcastCache {
|
|
|
36
36
|
});
|
|
37
37
|
return consumer;
|
|
38
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Stop sharing the broadcast cached for `path`, so the next request subscribes fresh.
|
|
41
|
+
*
|
|
42
|
+
* Call when the path's advertisement goes away. A handle only leaves the cache on its own
|
|
43
|
+
* once *every* holder has closed it, so one holder outliving the publisher (a second
|
|
44
|
+
* watcher, or a caller consuming the path directly) would otherwise keep the dead
|
|
45
|
+
* generation's cached tracks alive and hand them to whoever consumes the path next.
|
|
46
|
+
* Existing handles are left alone: they belong to their holders, and the wire resets
|
|
47
|
+
* whatever they still have open.
|
|
48
|
+
*
|
|
49
|
+
* Eviction is unconditional, which costs a dedup miss when two announcement streams watch
|
|
50
|
+
* one path: the second stream's retraction can arrive after the first has already seen the
|
|
51
|
+
* replacement, dropping the fresh entry so the next request subscribes again instead of
|
|
52
|
+
* sharing. Telling that stale retraction from a live one needs a generation id on the
|
|
53
|
+
* advertisement (moq-lite's `Epoch`, not yet on the wire), so until then this errs toward a
|
|
54
|
+
* duplicate subscription rather than risk handing out a dead one.
|
|
55
|
+
*/
|
|
56
|
+
evict(path) {
|
|
57
|
+
this.#cache.delete(path);
|
|
58
|
+
}
|
|
39
59
|
}
|
|
40
60
|
//# sourceMappingURL=consume.js.map
|
package/consume.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"consume.js","sourceRoot":"","sources":["../src/consume.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;GAYG;AACH,MAAM,OAAO,cAAc;IAC1B,wEAAwE;IACxE,MAAM,GAAG,IAAI,GAAG,EAAkC,CAAC;IAEnD,yFAAyF;IACzF,GAAG,CAAC,IAAgB;QACnB,MAAM,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QACnC,IAAI,IAAI,IAAI,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,SAAS;YAAE,OAAO,IAAI,CAAC,KAAK,EAAE,CAAC;QAClE,OAAO,SAAS,CAAC;IAClB,CAAC;IAED;;;OAGG;IACH,MAAM,CAAC,IAAgB,EAAE,QAA4B;QACpD,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;QAEhC,wFAAwF;QACxF,2FAA2F;QAC3F,KAAK,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE;YAC9B,IAAI,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,QAAQ;gBAAE,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QAClE,CAAC,CAAC,CAAC;QAEH,OAAO,QAAQ,CAAC;IACjB,CAAC;CACD","sourcesContent":["import type * as broadcast from \"./broadcast.ts\";\nimport type * as Path from \"./path.ts\";\n\n/**\n * Per-path dedup cache for consumed broadcasts, shared by the moq-lite and moq-ietf\n * subscribers.\n *\n * `Connection.consume(path)` must not mint a fresh subscription per call: repeat requests\n * for the same path (e.g. several renditions referencing one `broadcast: \"../source\"`) should\n * share a single upstream subscription. This mirrors the Rust `origin::Consumer` weak-cache:\n * a still-live path resolves to a shared {@link broadcast.Consumer.clone}, a closed one is\n * re-consumed on the next request. Each handle is reference-counted, so the shared broadcast\n * closes once every caller has closed its handle.\n *\n * @internal\n */\nexport class BroadcastCache {\n\t// The base handle per path; callers get reference-counted clones of it.\n\t#cache = new Map<Path.Valid, broadcast.Consumer>();\n\n\t/** A shared handle to the live broadcast cached for `path`, or `undefined` on a miss. */\n\tget(path: Path.Valid): broadcast.Consumer | undefined {\n\t\tconst base = this.#cache.get(path);\n\t\tif (base && base.closed.peek() === undefined) return base.clone();\n\t\treturn undefined;\n\t}\n\n\t/**\n\t * Cache `consumer` as the base handle for `path` (evicting it once it closes) and return it.\n\t * Call on a {@link get} miss, after wiring up the fresh consumer's subscribe loop.\n\t */\n\tinsert(path: Path.Valid, consumer: broadcast.Consumer): broadcast.Consumer {\n\t\tthis.#cache.set(path, consumer);\n\n\t\t// Drop the entry once the broadcast closes (every handle released), so the next request\n\t\t// re-consumes rather than cloning a dead handle. Guard against a newer entry for the path.\n\t\tvoid consumer.closed.then(() => {\n\t\t\tif (this.#cache.get(path) === consumer) this.#cache.delete(path);\n\t\t});\n\n\t\treturn consumer;\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"consume.js","sourceRoot":"","sources":["../src/consume.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;GAYG;AACH,MAAM,OAAO,cAAc;IAC1B,wEAAwE;IACxE,MAAM,GAAG,IAAI,GAAG,EAAkC,CAAC;IAEnD,yFAAyF;IACzF,GAAG,CAAC,IAAgB;QACnB,MAAM,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QACnC,IAAI,IAAI,IAAI,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,SAAS;YAAE,OAAO,IAAI,CAAC,KAAK,EAAE,CAAC;QAClE,OAAO,SAAS,CAAC;IAClB,CAAC;IAED;;;OAGG;IACH,MAAM,CAAC,IAAgB,EAAE,QAA4B;QACpD,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;QAEhC,wFAAwF;QACxF,2FAA2F;QAC3F,KAAK,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE;YAC9B,IAAI,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,QAAQ;gBAAE,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QAClE,CAAC,CAAC,CAAC;QAEH,OAAO,QAAQ,CAAC;IACjB,CAAC;IAED;;;;;;;;;;;;;;;;OAgBG;IACH,KAAK,CAAC,IAAgB;QACrB,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC1B,CAAC;CACD","sourcesContent":["import type * as broadcast from \"./broadcast.ts\";\nimport type * as Path from \"./path.ts\";\n\n/**\n * Per-path dedup cache for consumed broadcasts, shared by the moq-lite and moq-ietf\n * subscribers.\n *\n * `Connection.consume(path)` must not mint a fresh subscription per call: repeat requests\n * for the same path (e.g. several renditions referencing one `broadcast: \"../source\"`) should\n * share a single upstream subscription. This mirrors the Rust `origin::Consumer` weak-cache:\n * a still-live path resolves to a shared {@link broadcast.Consumer.clone}, a closed one is\n * re-consumed on the next request. Each handle is reference-counted, so the shared broadcast\n * closes once every caller has closed its handle.\n *\n * @internal\n */\nexport class BroadcastCache {\n\t// The base handle per path; callers get reference-counted clones of it.\n\t#cache = new Map<Path.Valid, broadcast.Consumer>();\n\n\t/** A shared handle to the live broadcast cached for `path`, or `undefined` on a miss. */\n\tget(path: Path.Valid): broadcast.Consumer | undefined {\n\t\tconst base = this.#cache.get(path);\n\t\tif (base && base.closed.peek() === undefined) return base.clone();\n\t\treturn undefined;\n\t}\n\n\t/**\n\t * Cache `consumer` as the base handle for `path` (evicting it once it closes) and return it.\n\t * Call on a {@link get} miss, after wiring up the fresh consumer's subscribe loop.\n\t */\n\tinsert(path: Path.Valid, consumer: broadcast.Consumer): broadcast.Consumer {\n\t\tthis.#cache.set(path, consumer);\n\n\t\t// Drop the entry once the broadcast closes (every handle released), so the next request\n\t\t// re-consumes rather than cloning a dead handle. Guard against a newer entry for the path.\n\t\tvoid consumer.closed.then(() => {\n\t\t\tif (this.#cache.get(path) === consumer) this.#cache.delete(path);\n\t\t});\n\n\t\treturn consumer;\n\t}\n\n\t/**\n\t * Stop sharing the broadcast cached for `path`, so the next request subscribes fresh.\n\t *\n\t * Call when the path's advertisement goes away. A handle only leaves the cache on its own\n\t * once *every* holder has closed it, so one holder outliving the publisher (a second\n\t * watcher, or a caller consuming the path directly) would otherwise keep the dead\n\t * generation's cached tracks alive and hand them to whoever consumes the path next.\n\t * Existing handles are left alone: they belong to their holders, and the wire resets\n\t * whatever they still have open.\n\t *\n\t * Eviction is unconditional, which costs a dedup miss when two announcement streams watch\n\t * one path: the second stream's retraction can arrive after the first has already seen the\n\t * replacement, dropping the fresh entry so the next request subscribes again instead of\n\t * sharing. Telling that stale retraction from a live one needs a generation id on the\n\t * advertisement (moq-lite's `Epoch`, not yet on the wire), so until then this errs toward a\n\t * duplicate subscription rather than risk handing out a dead one.\n\t */\n\tevict(path: Path.Valid): void {\n\t\tthis.#cache.delete(path);\n\t}\n}\n"]}
|
package/ietf/connection.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type Getter } from "@moq/signals";
|
|
2
|
-
import
|
|
2
|
+
import * as announce from "../announced";
|
|
3
3
|
import type * as broadcast from "../broadcast";
|
|
4
4
|
import type { Established } from "../connection/established";
|
|
5
5
|
import { type Probe, type Stats } from "../connection/stats";
|
|
@@ -69,6 +69,13 @@ export declare class Connection implements Established {
|
|
|
69
69
|
* @returns A Broadcast instance
|
|
70
70
|
*/
|
|
71
71
|
consume(path: Path.Valid): broadcast.Consumer;
|
|
72
|
+
/**
|
|
73
|
+
* Watches a broadcast, live only while it is announced.
|
|
74
|
+
*
|
|
75
|
+
* @param path - The path of the broadcast to watch
|
|
76
|
+
* @returns A reactive handle to the broadcast
|
|
77
|
+
*/
|
|
78
|
+
announcedBroadcast(path: Path.Valid): announce.Broadcast;
|
|
72
79
|
/**
|
|
73
80
|
* Returns a promise that resolves when the connection is closed.
|
|
74
81
|
* @returns A promise that resolves when closed
|
package/ietf/connection.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../../src/ietf/connection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,MAAM,EAAU,MAAM,cAAc,CAAC;AACnD,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../../src/ietf/connection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,MAAM,EAAU,MAAM,cAAc,CAAC;AACnD,OAAO,KAAK,QAAQ,MAAM,iBAAiB,CAAC;AAC5C,OAAO,KAAK,KAAK,SAAS,MAAM,iBAAiB,CAAC;AAClD,OAAO,KAAK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AAS,CAAC,MAAM,aAAe,GAAG,QAAQ,CAAC,QAAQ,CAElD;IAED;;;;;;;;OAQG;IACH,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,KAAK,GAAG,SAAS,CAAC,QAAQ,CAE5C;IAED;;;;;OAKG;IACH,kBAAkB,CAAC,IAAI,EAAE,IAAI,CAAC,KAAK,GAAG,QAAQ,CAAC,SAAS,CAEvD;IA2HD;;;OAGG;IACH,IAAI,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC,CAE1B;CACD"}
|
package/ietf/connection.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/* @ts-self-types="./connection.d.ts" */
|
|
2
2
|
import { Signal } from "@moq/signals";
|
|
3
|
+
import * as announce from "../announced.js";
|
|
3
4
|
import { transportStats } from "../connection/stats.js";
|
|
4
5
|
import { transportOf } from "../connection/transport.js";
|
|
5
6
|
import * as Path from "../path.js";
|
|
@@ -88,6 +89,7 @@ export class Connection {
|
|
|
88
89
|
if (this.#closed)
|
|
89
90
|
return;
|
|
90
91
|
this.#closed = true;
|
|
92
|
+
this.#publisher.close();
|
|
91
93
|
this.#session.close();
|
|
92
94
|
try {
|
|
93
95
|
this.#quic.close();
|
|
@@ -137,6 +139,15 @@ export class Connection {
|
|
|
137
139
|
consume(path) {
|
|
138
140
|
return this.#subscriber.consume(path);
|
|
139
141
|
}
|
|
142
|
+
/**
|
|
143
|
+
* Watches a broadcast, live only while it is announced.
|
|
144
|
+
*
|
|
145
|
+
* @param path - The path of the broadcast to watch
|
|
146
|
+
* @returns A reactive handle to the broadcast
|
|
147
|
+
*/
|
|
148
|
+
announcedBroadcast(path) {
|
|
149
|
+
return new announce.Broadcast({ connection: this, path });
|
|
150
|
+
}
|
|
140
151
|
/**
|
|
141
152
|
* Accepts bidi streams (virtual for v14-v16, real for v17) and dispatches.
|
|
142
153
|
*/
|
package/ietf/connection.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connection.js","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IAAI,CAAC,WAAW,CAAC,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACpD,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,UAAU,CAAC,aAAqB,EAAE,OAAoB;QAC3D,IAAI,CAAC;YACJ,MAAM,IAAI,GAAG,MAAM,aAAa,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;YAC/C,IAAI,IAAI;gBAAE,OAAO;YAEjB,MAAM,MAAM,GAAG,MAAM,aAAa,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC;YAChD,IAAI,MAAM,KAAK,MAAM,CAAC,EAAE,EAAE,CAAC;gBAC1B,MAAM,GAAG,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,aAAa,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;gBAC/D,OAAO,CAAC,IAAI,CAAC,sCAAsC,GAAG,CAAC,aAAa,EAAE,CAAC,CAAC;YACzE,CAAC;iBAAM,CAAC;gBACP,OAAO,CAAC,IAAI,CAAC,yCAAyC,MAAM,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;YAC9E,CAAC;QACF,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACd,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;gBACnB,OAAO,CAAC,KAAK,CAAC,4BAA4B,EAAE,GAAG,CAAC,CAAC;YAClD,CAAC;QACF,CAAC;gBAAS,CAAC;YACV,IAAI,CAAC,KAAK,EAAE,CAAC;QACd,CAAC;IACF,CAAC;IAED;;;OAGG;IACH,IAAI,MAAM;QACT,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC;IAChD,CAAC;CACD","sourcesContent":["import { type Getter, Signal } from \"@moq/signals\";\nimport type * as announce from \"../announced.ts\";\nimport type * as broadcast from \"../broadcast.ts\";\nimport type { Established } from \"../connection/established.ts\";\nimport { type Probe, type Stats, transportStats } from \"../connection/stats.ts\";\nimport { type Transport, transportOf } from \"../connection/transport.ts\";\nimport * as Path from \"../path.ts\";\nimport { type Reader, Readers, type Stream } from \"../stream.ts\";\nimport { ControlStreamAdapter, NativeSession, type Session } from \"./adapter.ts\";\nimport { GoAway } from \"./goaway.ts\";\nimport { Group } from \"./object.ts\";\nimport { Publish } from \"./publish.ts\";\nimport { PublishNamespace } from \"./publish_namespace.ts\";\nimport { Publisher } from \"./publisher.ts\";\nimport { Subscribe, SubscribeUpdate } from \"./subscribe.ts\";\nimport { SubscribeNamespace, SubscribeNamespaceLegacy } from \"./subscribe_namespace.ts\";\nimport { Subscriber } from \"./subscriber.ts\";\nimport { TrackStatusRequest } from \"./track.ts\";\nimport { type IetfVersion, Version, versionName } from \"./version.ts\";\n\n/**\n * Represents a connection to a MoQ server using moq-transport protocol.\n *\n * @public\n */\nexport class Connection implements Established {\n\t// The URL of the connection.\n\treadonly url: URL;\n\n\t// The negotiated protocol version.\n\treadonly version: string;\n\n\t// The wire transport this session runs over.\n\treadonly transport: Transport;\n\n\t/** Whether the relay supports broadcast discovery; see {@link Established.discovery}. */\n\treadonly discovery: boolean;\n\n\t/** moq-transport has no PROBE, so this stays empty; see {@link Established.probe}. */\n\treadonly probe: Getter<Probe> = new Signal<Probe>({});\n\n\t// The established WebTransport session.\n\t#quic: WebTransport;\n\n\t// Session abstraction: adapter for v14-v16, native for v17.\n\t#session: Session;\n\n\t// Module for contributing tracks.\n\t#publisher: Publisher;\n\n\t// Module for distributing tracks.\n\t#subscriber: Subscriber;\n\n\t// Just to avoid logging when `close()` is called.\n\t#closed = false;\n\n\t/**\n\t * Creates a new Connection instance.\n\t * @param url - The URL of the connection\n\t * @param quic - The WebTransport session\n\t * @param control - The control/setup stream\n\t * @param maxRequestId - The initial max request ID\n\t * @param version - The negotiated protocol version\n\t *\n\t * @internal\n\t */\n\tconstructor({\n\t\turl,\n\t\tquic,\n\t\tcontrol,\n\t\tmaxRequestId,\n\t\tversion,\n\t\tclient,\n\t\tdiscovery = true,\n\t}: {\n\t\turl: URL;\n\t\tquic: WebTransport;\n\t\tcontrol: Stream;\n\t\tmaxRequestId: bigint;\n\t\tversion: IetfVersion;\n\t\t/** Whether this peer initiated the session, selecting the even request-ID space. */\n\t\tclient: boolean;\n\t\tdiscovery?: boolean;\n\t}) {\n\t\tthis.url = url;\n\t\tthis.discovery = discovery;\n\t\tthis.version = versionName(version);\n\t\tthis.transport = transportOf(quic);\n\t\tthis.#quic = quic;\n\n\t\t// Two-path dispatch: v14-v16 uses adapter, v17+ uses native bidi streams\n\t\tif (version >= Version.DRAFT_17) {\n\t\t\tthis.#session = new NativeSession(quic, version, client);\n\t\t\t// v17+: control/setup stream only carries GoAway\n\t\t\tvoid this.#runGoAway(control, version);\n\t\t} else {\n\t\t\tconst adapter = new ControlStreamAdapter(quic, control, version, maxRequestId, client);\n\t\t\tthis.#session = adapter;\n\t\t\t// Start the adapter read loop (routes control messages to virtual streams)\n\t\t\tvoid adapter.run().catch((err: unknown) => {\n\t\t\t\tif (!this.#closed) console.error(\"adapter error\", err);\n\t\t\t\tthis.close();\n\t\t\t});\n\t\t}\n\n\t\tthis.#publisher = new Publisher(this.#quic, this.#session);\n\t\tthis.#subscriber = new Subscriber(this.#session);\n\n\t\tvoid this.#run();\n\t}\n\n\t/** Snapshot the transport's counters; see {@link Established.stats}. */\n\tasync stats(): Promise<Stats> {\n\t\treturn transportStats(this.#quic);\n\t}\n\n\t/**\n\t * Closes the connection.\n\t */\n\tclose() {\n\t\tif (this.#closed) return;\n\n\t\tthis.#closed = true;\n\n\t\tthis.#session.close();\n\n\t\ttry {\n\t\t\tthis.#quic.close();\n\t\t} catch {\n\t\t\t// ignore\n\t\t}\n\t}\n\n\tasync #run(): Promise<void> {\n\t\ttry {\n\t\t\tawait Promise.all([this.#runBidis(), this.#runUnis()]);\n\t\t} catch (err) {\n\t\t\tif (!this.#closed) {\n\t\t\t\tconsole.error(\"fatal error running connection\", err);\n\t\t\t}\n\t\t} finally {\n\t\t\tthis.close();\n\t\t}\n\t}\n\n\t/**\n\t * Publishes a broadcast to the connection.\n\t * @param name - The broadcast path to publish\n\t * @param broadcast - The broadcast to publish\n\t */\n\tpublish(path: Path.Valid, producer: broadcast.Producer) {\n\t\tthis.#publisher.publish(path, producer);\n\t}\n\n\t/**\n\t * Gets an announced reader for the specified prefix.\n\t * @param prefix - The prefix for announcements\n\t * @returns An Announced instance\n\t */\n\tannounced(prefix = Path.empty()): announce.Consumer {\n\t\treturn this.#subscriber.announced(prefix);\n\t}\n\n\t/**\n\t * Consumes a broadcast from the connection.\n\t *\n\t * @remarks\n\t * If the broadcast is not found, a \"not found\" error will be thrown when requesting any tracks.\n\t *\n\t * @param broadcast - The path of the broadcast to consume\n\t * @returns A Broadcast instance\n\t */\n\tconsume(path: Path.Valid): broadcast.Consumer {\n\t\treturn this.#subscriber.consume(path);\n\t}\n\n\t/**\n\t * Accepts bidi streams (virtual for v14-v16, real for v17) and dispatches.\n\t */\n\tasync #runBidis() {\n\t\tfor (;;) {\n\t\t\tconst stream = await this.#session.acceptBi();\n\t\t\tif (!stream) break;\n\n\t\t\tvoid this.#runBidi(stream).catch((err: unknown) => {\n\t\t\t\tconsole.error(\"error processing bidi stream\", err);\n\t\t\t\tstream.abort(new Error(\"bidi stream error\"));\n\t\t\t});\n\t\t}\n\t}\n\n\t/**\n\t * Unified bidi stream dispatch. Reads typeId and routes to handler.\n\t * Matches the lite module's runBidi pattern.\n\t */\n\tasync #runBidi(stream: Stream) {\n\t\tconst typeId = await stream.reader.u53();\n\n\t\tswitch (typeId) {\n\t\t\t// Draft-18 SUBSCRIBE_NAMESPACE (0x50) and the legacy 0x11 message decode\n\t\t\t// to the same request_id + namespace; the legacy options field is ignored.\n\t\t\tcase SubscribeNamespace.id: {\n\t\t\t\tconst msg = await SubscribeNamespace.decode(stream.reader, this.#session.version);\n\t\t\t\tawait this.#publisher.runSubscribeNamespace(msg, stream);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase SubscribeNamespaceLegacy.id: {\n\t\t\t\tconst legacy = await SubscribeNamespaceLegacy.decode(stream.reader, this.#session.version);\n\t\t\t\tconst msg = new SubscribeNamespace({ requestId: legacy.requestId, namespace: legacy.namespace });\n\t\t\t\tawait this.#publisher.runSubscribeNamespace(msg, stream);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase SubscribeUpdate.id: {\n\t\t\t\t// REQUEST_UPDATE (0x02) is a follow-up, not a valid initial message\n\t\t\t\tstream.abort(new Error(\"unexpected REQUEST_UPDATE as initial message\"));\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\t// Publisher handles incoming requests\n\t\t\tcase Subscribe.id: {\n\t\t\t\tconst msg = await Subscribe.decode(stream.reader, this.#session.version);\n\t\t\t\tawait this.#publisher.runSubscribe(msg, stream);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase TrackStatusRequest.id: {\n\t\t\t\tconst msg = await TrackStatusRequest.decode(stream.reader, this.#session.version);\n\t\t\t\tawait this.#publisher.runTrackStatusRequest(msg, stream);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\t// Subscriber handles incoming notifications\n\t\t\tcase PublishNamespace.id: {\n\t\t\t\tconst msg = await PublishNamespace.decode(stream.reader, this.#session.version);\n\t\t\t\tawait this.#subscriber.runPublishNamespace(msg, stream);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase Publish.id: {\n\t\t\t\tconst msg = await Publish.decode(stream.reader, this.#session.version);\n\t\t\t\tawait this.#subscriber.runPublish(msg, stream);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tdefault:\n\t\t\t\tconsole.warn(`unexpected bidi stream type: 0x${typeId.toString(16)}`);\n\t\t\t\tstream.abort(new Error(\"unexpected stream type\"));\n\t\t}\n\t}\n\n\t/**\n\t * Handles unidirectional streams for media delivery (groups).\n\t */\n\tasync #runUnis() {\n\t\tconst readers = new Readers(this.#quic, this.#session.version);\n\n\t\tfor (;;) {\n\t\t\tconst stream = await readers.next();\n\t\t\tif (!stream) break;\n\n\t\t\tthis.#runUni(stream)\n\t\t\t\t.then(() => {\n\t\t\t\t\tstream.stop(new Error(\"cancel\"));\n\t\t\t\t})\n\t\t\t\t.catch((err: unknown) => {\n\t\t\t\t\tconsole.error(\"error processing object stream\", err);\n\t\t\t\t\tstream.stop(err);\n\t\t\t\t});\n\t\t}\n\t}\n\n\tasync #runUni(stream: Reader) {\n\t\tconst header = await Group.decode(stream, this.#session.version);\n\t\tawait this.#subscriber.handleGroup(header, stream);\n\t}\n\n\t/**\n\t * v17+ only: reads GoAway from the setup/control stream.\n\t */\n\tasync #runGoAway(controlStream: Stream, version: IetfVersion) {\n\t\ttry {\n\t\t\tconst done = await controlStream.reader.done();\n\t\t\tif (done) return;\n\n\t\t\tconst typeId = await controlStream.reader.u53();\n\t\t\tif (typeId === GoAway.id) {\n\t\t\t\tconst msg = await GoAway.decode(controlStream.reader, version);\n\t\t\t\tconsole.warn(`received GOAWAY with redirect URI: ${msg.newSessionUri}`);\n\t\t\t} else {\n\t\t\t\tconsole.warn(`unexpected message on setup stream: 0x${typeId.toString(16)}`);\n\t\t\t}\n\t\t} catch (err) {\n\t\t\tif (!this.#closed) {\n\t\t\t\tconsole.error(\"error reading setup stream\", err);\n\t\t\t}\n\t\t} finally {\n\t\t\tthis.close();\n\t\t}\n\t}\n\n\t/**\n\t * Returns a promise that resolves when the connection is closed.\n\t * @returns A promise that resolves when closed\n\t */\n\tget closed(): Promise<void> {\n\t\treturn this.#quic.closed.then(() => undefined);\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"connection.js","sourceRoot":"","sources":["../../src/ietf/connection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,MAAM,EAAE,MAAM,cAAc,CAAC;AACnD,OAAO,KAAK,QAAQ,MAAM,iBAAiB,CAAC;AAG5C,OAAO,EAA0B,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAChF,OAAO,EAAkB,WAAW,EAAE,MAAM,4BAA4B,CA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MAAM,IAAI,CAAC,WAAW,CAAC,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACpD,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,UAAU,CAAC,aAAqB,EAAE,OAAoB;QAC3D,IAAI,CAAC;YACJ,MAAM,IAAI,GAAG,MAAM,aAAa,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;YAC/C,IAAI,IAAI;gBAAE,OAAO;YAEjB,MAAM,MAAM,GAAG,MAAM,aAAa,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC;YAChD,IAAI,MAAM,KAAK,MAAM,CAAC,EAAE,EAAE,CAAC;gBAC1B,MAAM,GAAG,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,aAAa,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;gBAC/D,OAAO,CAAC,IAAI,CAAC,sCAAsC,GAAG,CAAC,aAAa,EAAE,CAAC,CAAC;YACzE,CAAC;iBAAM,CAAC;gBACP,OAAO,CAAC,IAAI,CAAC,yCAAyC,MAAM,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;YAC9E,CAAC;QACF,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACd,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;gBACnB,OAAO,CAAC,KAAK,CAAC,4BAA4B,EAAE,GAAG,CAAC,CAAC;YAClD,CAAC;QACF,CAAC;gBAAS,CAAC;YACV,IAAI,CAAC,KAAK,EAAE,CAAC;QACd,CAAC;IACF,CAAC;IAED;;;OAGG;IACH,IAAI,MAAM;QACT,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC;IAChD,CAAC;CACD","sourcesContent":["import { type Getter, Signal } from \"@moq/signals\";\nimport * as announce from \"../announced.ts\";\nimport type * as broadcast from \"../broadcast.ts\";\nimport type { Established } from \"../connection/established.ts\";\nimport { type Probe, type Stats, transportStats } from \"../connection/stats.ts\";\nimport { type Transport, transportOf } from \"../connection/transport.ts\";\nimport * as Path from \"../path.ts\";\nimport { type Reader, Readers, type Stream } from \"../stream.ts\";\nimport { ControlStreamAdapter, NativeSession, type Session } from \"./adapter.ts\";\nimport { GoAway } from \"./goaway.ts\";\nimport { Group } from \"./object.ts\";\nimport { Publish } from \"./publish.ts\";\nimport { PublishNamespace } from \"./publish_namespace.ts\";\nimport { Publisher } from \"./publisher.ts\";\nimport { Subscribe, SubscribeUpdate } from \"./subscribe.ts\";\nimport { SubscribeNamespace, SubscribeNamespaceLegacy } from \"./subscribe_namespace.ts\";\nimport { Subscriber } from \"./subscriber.ts\";\nimport { TrackStatusRequest } from \"./track.ts\";\nimport { type IetfVersion, Version, versionName } from \"./version.ts\";\n\n/**\n * Represents a connection to a MoQ server using moq-transport protocol.\n *\n * @public\n */\nexport class Connection implements Established {\n\t// The URL of the connection.\n\treadonly url: URL;\n\n\t// The negotiated protocol version.\n\treadonly version: string;\n\n\t// The wire transport this session runs over.\n\treadonly transport: Transport;\n\n\t/** Whether the relay supports broadcast discovery; see {@link Established.discovery}. */\n\treadonly discovery: boolean;\n\n\t/** moq-transport has no PROBE, so this stays empty; see {@link Established.probe}. */\n\treadonly probe: Getter<Probe> = new Signal<Probe>({});\n\n\t// The established WebTransport session.\n\t#quic: WebTransport;\n\n\t// Session abstraction: adapter for v14-v16, native for v17.\n\t#session: Session;\n\n\t// Module for contributing tracks.\n\t#publisher: Publisher;\n\n\t// Module for distributing tracks.\n\t#subscriber: Subscriber;\n\n\t// Just to avoid logging when `close()` is called.\n\t#closed = false;\n\n\t/**\n\t * Creates a new Connection instance.\n\t * @param url - The URL of the connection\n\t * @param quic - The WebTransport session\n\t * @param control - The control/setup stream\n\t * @param maxRequestId - The initial max request ID\n\t * @param version - The negotiated protocol version\n\t *\n\t * @internal\n\t */\n\tconstructor({\n\t\turl,\n\t\tquic,\n\t\tcontrol,\n\t\tmaxRequestId,\n\t\tversion,\n\t\tclient,\n\t\tdiscovery = true,\n\t}: {\n\t\turl: URL;\n\t\tquic: WebTransport;\n\t\tcontrol: Stream;\n\t\tmaxRequestId: bigint;\n\t\tversion: IetfVersion;\n\t\t/** Whether this peer initiated the session, selecting the even request-ID space. */\n\t\tclient: boolean;\n\t\tdiscovery?: boolean;\n\t}) {\n\t\tthis.url = url;\n\t\tthis.discovery = discovery;\n\t\tthis.version = versionName(version);\n\t\tthis.transport = transportOf(quic);\n\t\tthis.#quic = quic;\n\n\t\t// Two-path dispatch: v14-v16 uses adapter, v17+ uses native bidi streams\n\t\tif (version >= Version.DRAFT_17) {\n\t\t\tthis.#session = new NativeSession(quic, version, client);\n\t\t\t// v17+: control/setup stream only carries GoAway\n\t\t\tvoid this.#runGoAway(control, version);\n\t\t} else {\n\t\t\tconst adapter = new ControlStreamAdapter(quic, control, version, maxRequestId, client);\n\t\t\tthis.#session = adapter;\n\t\t\t// Start the adapter read loop (routes control messages to virtual streams)\n\t\t\tvoid adapter.run().catch((err: unknown) => {\n\t\t\t\tif (!this.#closed) console.error(\"adapter error\", err);\n\t\t\t\tthis.close();\n\t\t\t});\n\t\t}\n\n\t\tthis.#publisher = new Publisher(this.#quic, this.#session);\n\t\tthis.#subscriber = new Subscriber(this.#session);\n\n\t\tvoid this.#run();\n\t}\n\n\t/** Snapshot the transport's counters; see {@link Established.stats}. */\n\tasync stats(): Promise<Stats> {\n\t\treturn transportStats(this.#quic);\n\t}\n\n\t/**\n\t * Closes the connection.\n\t */\n\tclose() {\n\t\tif (this.#closed) return;\n\n\t\tthis.#closed = true;\n\n\t\tthis.#publisher.close();\n\t\tthis.#session.close();\n\n\t\ttry {\n\t\t\tthis.#quic.close();\n\t\t} catch {\n\t\t\t// ignore\n\t\t}\n\t}\n\n\tasync #run(): Promise<void> {\n\t\ttry {\n\t\t\tawait Promise.all([this.#runBidis(), this.#runUnis()]);\n\t\t} catch (err) {\n\t\t\tif (!this.#closed) {\n\t\t\t\tconsole.error(\"fatal error running connection\", err);\n\t\t\t}\n\t\t} finally {\n\t\t\tthis.close();\n\t\t}\n\t}\n\n\t/**\n\t * Publishes a broadcast to the connection.\n\t * @param name - The broadcast path to publish\n\t * @param broadcast - The broadcast to publish\n\t */\n\tpublish(path: Path.Valid, producer: broadcast.Producer) {\n\t\tthis.#publisher.publish(path, producer);\n\t}\n\n\t/**\n\t * Gets an announced reader for the specified prefix.\n\t * @param prefix - The prefix for announcements\n\t * @returns An Announced instance\n\t */\n\tannounced(prefix = Path.empty()): announce.Consumer {\n\t\treturn this.#subscriber.announced(prefix);\n\t}\n\n\t/**\n\t * Consumes a broadcast from the connection.\n\t *\n\t * @remarks\n\t * If the broadcast is not found, a \"not found\" error will be thrown when requesting any tracks.\n\t *\n\t * @param broadcast - The path of the broadcast to consume\n\t * @returns A Broadcast instance\n\t */\n\tconsume(path: Path.Valid): broadcast.Consumer {\n\t\treturn this.#subscriber.consume(path);\n\t}\n\n\t/**\n\t * Watches a broadcast, live only while it is announced.\n\t *\n\t * @param path - The path of the broadcast to watch\n\t * @returns A reactive handle to the broadcast\n\t */\n\tannouncedBroadcast(path: Path.Valid): announce.Broadcast {\n\t\treturn new announce.Broadcast({ connection: this, path });\n\t}\n\n\t/**\n\t * Accepts bidi streams (virtual for v14-v16, real for v17) and dispatches.\n\t */\n\tasync #runBidis() {\n\t\tfor (;;) {\n\t\t\tconst stream = await this.#session.acceptBi();\n\t\t\tif (!stream) break;\n\n\t\t\tvoid this.#runBidi(stream).catch((err: unknown) => {\n\t\t\t\tconsole.error(\"error processing bidi stream\", err);\n\t\t\t\tstream.abort(new Error(\"bidi stream error\"));\n\t\t\t});\n\t\t}\n\t}\n\n\t/**\n\t * Unified bidi stream dispatch. Reads typeId and routes to handler.\n\t * Matches the lite module's runBidi pattern.\n\t */\n\tasync #runBidi(stream: Stream) {\n\t\tconst typeId = await stream.reader.u53();\n\n\t\tswitch (typeId) {\n\t\t\t// Draft-18 SUBSCRIBE_NAMESPACE (0x50) and the legacy 0x11 message decode\n\t\t\t// to the same request_id + namespace; the legacy options field is ignored.\n\t\t\tcase SubscribeNamespace.id: {\n\t\t\t\tconst msg = await SubscribeNamespace.decode(stream.reader, this.#session.version);\n\t\t\t\tawait this.#publisher.runSubscribeNamespace(msg, stream);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase SubscribeNamespaceLegacy.id: {\n\t\t\t\tconst legacy = await SubscribeNamespaceLegacy.decode(stream.reader, this.#session.version);\n\t\t\t\tconst msg = new SubscribeNamespace({ requestId: legacy.requestId, namespace: legacy.namespace });\n\t\t\t\tawait this.#publisher.runSubscribeNamespace(msg, stream);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase SubscribeUpdate.id: {\n\t\t\t\t// REQUEST_UPDATE (0x02) is a follow-up, not a valid initial message\n\t\t\t\tstream.abort(new Error(\"unexpected REQUEST_UPDATE as initial message\"));\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\t// Publisher handles incoming requests\n\t\t\tcase Subscribe.id: {\n\t\t\t\tconst msg = await Subscribe.decode(stream.reader, this.#session.version);\n\t\t\t\tawait this.#publisher.runSubscribe(msg, stream);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase TrackStatusRequest.id: {\n\t\t\t\tconst msg = await TrackStatusRequest.decode(stream.reader, this.#session.version);\n\t\t\t\tawait this.#publisher.runTrackStatusRequest(msg, stream);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\t// Subscriber handles incoming notifications\n\t\t\tcase PublishNamespace.id: {\n\t\t\t\tconst msg = await PublishNamespace.decode(stream.reader, this.#session.version);\n\t\t\t\tawait this.#subscriber.runPublishNamespace(msg, stream);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase Publish.id: {\n\t\t\t\tconst msg = await Publish.decode(stream.reader, this.#session.version);\n\t\t\t\tawait this.#subscriber.runPublish(msg, stream);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tdefault:\n\t\t\t\tconsole.warn(`unexpected bidi stream type: 0x${typeId.toString(16)}`);\n\t\t\t\tstream.abort(new Error(\"unexpected stream type\"));\n\t\t}\n\t}\n\n\t/**\n\t * Handles unidirectional streams for media delivery (groups).\n\t */\n\tasync #runUnis() {\n\t\tconst readers = new Readers(this.#quic, this.#session.version);\n\n\t\tfor (;;) {\n\t\t\tconst stream = await readers.next();\n\t\t\tif (!stream) break;\n\n\t\t\tthis.#runUni(stream)\n\t\t\t\t.then(() => {\n\t\t\t\t\tstream.stop(new Error(\"cancel\"));\n\t\t\t\t})\n\t\t\t\t.catch((err: unknown) => {\n\t\t\t\t\tconsole.error(\"error processing object stream\", err);\n\t\t\t\t\tstream.stop(err);\n\t\t\t\t});\n\t\t}\n\t}\n\n\tasync #runUni(stream: Reader) {\n\t\tconst header = await Group.decode(stream, this.#session.version);\n\t\tawait this.#subscriber.handleGroup(header, stream);\n\t}\n\n\t/**\n\t * v17+ only: reads GoAway from the setup/control stream.\n\t */\n\tasync #runGoAway(controlStream: Stream, version: IetfVersion) {\n\t\ttry {\n\t\t\tconst done = await controlStream.reader.done();\n\t\t\tif (done) return;\n\n\t\t\tconst typeId = await controlStream.reader.u53();\n\t\t\tif (typeId === GoAway.id) {\n\t\t\t\tconst msg = await GoAway.decode(controlStream.reader, version);\n\t\t\t\tconsole.warn(`received GOAWAY with redirect URI: ${msg.newSessionUri}`);\n\t\t\t} else {\n\t\t\t\tconsole.warn(`unexpected message on setup stream: 0x${typeId.toString(16)}`);\n\t\t\t}\n\t\t} catch (err) {\n\t\t\tif (!this.#closed) {\n\t\t\t\tconsole.error(\"error reading setup stream\", err);\n\t\t\t}\n\t\t} finally {\n\t\t\tthis.close();\n\t\t}\n\t}\n\n\t/**\n\t * Returns a promise that resolves when the connection is closed.\n\t * @returns A promise that resolves when closed\n\t */\n\tget closed(): Promise<void> {\n\t\treturn this.#quic.closed.then(() => undefined);\n\t}\n}\n"]}
|
package/ietf/ietf.test.js
CHANGED
|
@@ -51,10 +51,10 @@ async function decodeVersioned(bytes, decoder, version) {
|
|
|
51
51
|
const reader = new Reader(undefined, bytes, version);
|
|
52
52
|
return await decoder(reader, version);
|
|
53
53
|
}
|
|
54
|
-
async function encodeFrameVersioned(frame, flags, version) {
|
|
54
|
+
async function encodeFrameVersioned(frame, flags, version, timescale = Timescale.MILLI) {
|
|
55
55
|
const { stream, written } = createTestWritableStream();
|
|
56
56
|
const writer = new Writer(stream, version);
|
|
57
|
-
await frame.encode(writer, flags, version);
|
|
57
|
+
await frame.encode(writer, flags, timescale, version);
|
|
58
58
|
writer.close();
|
|
59
59
|
await writer.closed;
|
|
60
60
|
return concatChunks(written);
|
|
@@ -711,6 +711,63 @@ test("SubscribeOk v18: no requestId", async () => {
|
|
|
711
711
|
expect(decoded.requestId).toBe(undefined);
|
|
712
712
|
expect(decoded.trackAlias).toBe(42n);
|
|
713
713
|
});
|
|
714
|
+
// GROUP_ORDER (0x22) is only a legal SUBSCRIBE_OK *message parameter* through draft-15; a
|
|
715
|
+
// draft-16+ peer closes the session with PROTOCOL_VIOLATION when it sees one. The publisher's
|
|
716
|
+
// preference belongs in the DEFAULT_PUBLISHER_GROUP_ORDER track property, which shares the
|
|
717
|
+
// number 0x22 in the separate property registry.
|
|
718
|
+
test("SubscribeOk v18: group order is a track property, not a parameter", async () => {
|
|
719
|
+
const msg = new Subscribe.SubscribeOk({ trackAlias: 42n });
|
|
720
|
+
const encoded = await encodeVersioned(msg, Version.DRAFT_18);
|
|
721
|
+
expect(Array.from(encoded)).toEqual([
|
|
722
|
+
0x00,
|
|
723
|
+
0x04, // u16 message length
|
|
724
|
+
42, // track alias
|
|
725
|
+
0x00, // zero message parameters
|
|
726
|
+
0x22, // DEFAULT_PUBLISHER_GROUP_ORDER, the first (and only) track property
|
|
727
|
+
0x02, // descending
|
|
728
|
+
]);
|
|
729
|
+
});
|
|
730
|
+
// Draft-16 has the block too, under the name Track Extensions. We don't write one there, but
|
|
731
|
+
// a peer that does used to fail the whole message on trailing bytes.
|
|
732
|
+
test("SubscribeOk v16: reads track extensions", async () => {
|
|
733
|
+
const body = [
|
|
734
|
+
7, // request id
|
|
735
|
+
42, // track alias
|
|
736
|
+
0x00, // zero message parameters
|
|
737
|
+
0x22, // DEFAULT_PUBLISHER_GROUP_ORDER
|
|
738
|
+
0x02, // descending
|
|
739
|
+
];
|
|
740
|
+
const bytes = new Uint8Array([0x00, body.length, ...body]);
|
|
741
|
+
const decoded = await decodeVersioned(bytes, Subscribe.SubscribeOk.decode, Version.DRAFT_16);
|
|
742
|
+
expect(decoded.trackAlias).toBe(42n);
|
|
743
|
+
});
|
|
744
|
+
// Only Ascending and Descending are defined, so 0x0 is a malformed message rather than the
|
|
745
|
+
// "publisher decides" it means in the draft-14 fields.
|
|
746
|
+
test("SubscribeOk v18: rejects a zero group order property", async () => {
|
|
747
|
+
const body = [
|
|
748
|
+
42, // track alias
|
|
749
|
+
0x00, // zero message parameters
|
|
750
|
+
0x22, // DEFAULT_PUBLISHER_GROUP_ORDER
|
|
751
|
+
0x00, // invalid
|
|
752
|
+
];
|
|
753
|
+
const bytes = new Uint8Array([0x00, body.length, ...body]);
|
|
754
|
+
await expect(decodeVersioned(bytes, Subscribe.SubscribeOk.decode, Version.DRAFT_18)).rejects.toThrow("unknown group order");
|
|
755
|
+
});
|
|
756
|
+
// Draft-15 is the one version that takes it as a message parameter, and has no track
|
|
757
|
+
// properties to put it in.
|
|
758
|
+
test("SubscribeOk v15: group order is a message parameter", async () => {
|
|
759
|
+
const msg = new Subscribe.SubscribeOk({ requestId: 7n, trackAlias: 42n });
|
|
760
|
+
const encoded = await encodeVersioned(msg, Version.DRAFT_15);
|
|
761
|
+
expect(Array.from(encoded)).toEqual([
|
|
762
|
+
0x00,
|
|
763
|
+
0x05, // u16 message length
|
|
764
|
+
7, // request id
|
|
765
|
+
42, // track alias
|
|
766
|
+
0x01, // one message parameter
|
|
767
|
+
0x22, // GROUP_ORDER
|
|
768
|
+
0x02, // descending
|
|
769
|
+
]);
|
|
770
|
+
});
|
|
714
771
|
test("SubscribeUpdate v18: 1 byte shorter than v17 (no required_request_id_delta)", async () => {
|
|
715
772
|
const msg = new Subscribe.SubscribeUpdate({ requestId: 10n });
|
|
716
773
|
const v17 = await encodeVersioned(msg, Version.DRAFT_17);
|
|
@@ -894,12 +951,10 @@ test("Frame object time: draft-15 uses absolute property types", async () => {
|
|
|
894
951
|
expect(await reader.u53()).toBe(0);
|
|
895
952
|
const extensions = await reader.read(await reader.u53());
|
|
896
953
|
const props = new Reader(undefined, extensions, Version.DRAFT_15);
|
|
897
|
-
expect(await props.u62()).toBe(
|
|
954
|
+
expect(await props.u62()).toBe(0x10n);
|
|
898
955
|
expect(await props.u62()).toBe(96000n);
|
|
899
|
-
expect(await props.u62()).toBe(0x08n);
|
|
900
|
-
expect(await props.u62()).toBe(1000n);
|
|
901
956
|
expect(await props.done()).toBe(true);
|
|
902
|
-
const decoded = await Frame.decode(new Reader(undefined, encoded, Version.DRAFT_15), flags, Version.DRAFT_15);
|
|
957
|
+
const decoded = await Frame.decode(new Reader(undefined, encoded, Version.DRAFT_15), flags, Timescale.MILLI, Version.DRAFT_15);
|
|
903
958
|
expect(decoded.timestamp?.value).toBe(96_000);
|
|
904
959
|
expect(decoded.timestamp?.scale).toBe(Timescale.MILLI);
|
|
905
960
|
});
|
|
@@ -918,12 +973,10 @@ test("Frame object time: draft-16 starts delta property types", async () => {
|
|
|
918
973
|
expect(await reader.u53()).toBe(0);
|
|
919
974
|
const extensions = await reader.read(await reader.u53());
|
|
920
975
|
const props = new Reader(undefined, extensions, Version.DRAFT_16);
|
|
921
|
-
expect(await props.u62()).toBe(
|
|
976
|
+
expect(await props.u62()).toBe(0x10n);
|
|
922
977
|
expect(await props.u62()).toBe(96000n);
|
|
923
|
-
expect(await props.u62()).toBe(0x02n);
|
|
924
|
-
expect(await props.u62()).toBe(1000n);
|
|
925
978
|
expect(await props.done()).toBe(true);
|
|
926
|
-
const decoded = await Frame.decode(new Reader(undefined, encoded, Version.DRAFT_16), flags, Version.DRAFT_16);
|
|
979
|
+
const decoded = await Frame.decode(new Reader(undefined, encoded, Version.DRAFT_16), flags, Timescale.MILLI, Version.DRAFT_16);
|
|
927
980
|
expect(decoded.timestamp?.value).toBe(96_000);
|
|
928
981
|
expect(decoded.timestamp?.scale).toBe(Timescale.MILLI);
|
|
929
982
|
});
|