@moq/net 0.1.9 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/announced.d.ts +40 -24
- package/announced.d.ts.map +1 -1
- package/announced.js +74 -40
- package/announced.js.map +1 -1
- package/announced.test.d.ts +2 -0
- package/announced.test.d.ts.map +1 -0
- package/announced.test.js +37 -0
- package/announced.test.js.map +1 -0
- package/broadcast.d.ts +80 -25
- package/broadcast.d.ts.map +1 -1
- package/broadcast.js +266 -49
- package/broadcast.js.map +1 -1
- package/broadcast.test.d.ts +2 -0
- package/broadcast.test.d.ts.map +1 -0
- package/broadcast.test.js +219 -0
- package/broadcast.test.js.map +1 -0
- package/connection/accept.d.ts +6 -0
- package/connection/accept.d.ts.map +1 -1
- package/connection/accept.js +30 -10
- package/connection/accept.js.map +1 -1
- package/connection/browser.d.ts +5 -0
- package/connection/browser.d.ts.map +1 -0
- package/connection/browser.js +25 -0
- package/connection/browser.js.map +1 -0
- package/connection/browser.test.d.ts +2 -0
- package/connection/browser.test.d.ts.map +1 -0
- package/connection/browser.test.js +29 -0
- package/connection/browser.test.js.map +1 -0
- package/connection/connect.d.ts +43 -1
- package/connection/connect.d.ts.map +1 -1
- package/connection/connect.js +96 -107
- package/connection/connect.js.map +1 -1
- package/connection/connect.test.d.ts +2 -0
- package/connection/connect.test.d.ts.map +1 -0
- package/connection/connect.test.js +118 -0
- package/connection/connect.test.js.map +1 -0
- package/connection/established.d.ts +29 -15
- package/connection/established.d.ts.map +1 -1
- package/connection/established.js.map +1 -1
- package/connection/index.d.ts +8 -0
- package/connection/index.d.ts.map +1 -1
- package/connection/index.js +6 -0
- package/connection/index.js.map +1 -1
- package/connection/reload.d.ts +43 -7
- package/connection/reload.d.ts.map +1 -1
- package/connection/reload.js +159 -62
- package/connection/reload.js.map +1 -1
- package/connection/reload.test.js +65 -0
- package/connection/reload.test.js.map +1 -1
- package/connection/stats.d.ts +92 -0
- package/connection/stats.d.ts.map +1 -0
- package/connection/stats.js +35 -0
- package/connection/stats.js.map +1 -0
- package/connection/stats.test.d.ts +2 -0
- package/connection/stats.test.d.ts.map +1 -0
- package/connection/stats.test.js +120 -0
- package/connection/stats.test.js.map +1 -0
- package/connection/transport.d.ts +10 -0
- package/connection/transport.d.ts.map +1 -0
- package/connection/transport.js +12 -0
- package/connection/transport.js.map +1 -0
- package/consume.d.ts +26 -0
- package/consume.d.ts.map +1 -0
- package/consume.js +40 -0
- package/consume.js.map +1 -0
- package/datagram.d.ts +23 -0
- package/datagram.d.ts.map +1 -0
- package/datagram.js +3 -0
- package/datagram.js.map +1 -0
- package/group.d.ts +125 -36
- package/group.d.ts.map +1 -1
- package/group.js +244 -67
- package/group.js.map +1 -1
- package/group.test.js +121 -35
- package/group.test.js.map +1 -1
- package/ietf/adapter.d.ts +2 -2
- package/ietf/adapter.d.ts.map +1 -1
- package/ietf/adapter.js +15 -5
- package/ietf/adapter.js.map +1 -1
- package/ietf/connection.d.ts +19 -6
- package/ietf/connection.d.ts.map +1 -1
- package/ietf/connection.js +23 -8
- package/ietf/connection.js.map +1 -1
- package/ietf/ietf.test.js +58 -1
- package/ietf/ietf.test.js.map +1 -1
- package/ietf/object.d.ts +12 -4
- package/ietf/object.d.ts.map +1 -1
- package/ietf/object.js +92 -9
- package/ietf/object.js.map +1 -1
- package/ietf/publisher.d.ts +2 -2
- package/ietf/publisher.d.ts.map +1 -1
- package/ietf/publisher.js +12 -12
- package/ietf/publisher.js.map +1 -1
- package/ietf/subscribe_namespace.d.ts +0 -4
- package/ietf/subscribe_namespace.d.ts.map +1 -1
- package/ietf/subscribe_namespace.js +0 -5
- package/ietf/subscribe_namespace.js.map +1 -1
- package/ietf/subscriber.d.ts +8 -4
- package/ietf/subscriber.d.ts.map +1 -1
- package/ietf/subscriber.js +171 -93
- package/ietf/subscriber.js.map +1 -1
- package/index.d.ts +8 -5
- package/index.d.ts.map +1 -1
- package/index.js +8 -5
- package/index.js.map +1 -1
- package/integration.test.js +598 -12
- package/integration.test.js.map +1 -1
- package/internal.d.ts +14 -0
- package/internal.d.ts.map +1 -0
- package/internal.js +8 -0
- package/internal.js.map +1 -0
- package/lite/announce.d.ts +55 -13
- package/lite/announce.d.ts.map +1 -1
- package/lite/announce.js +221 -68
- package/lite/announce.js.map +1 -1
- package/lite/announce.test.d.ts +2 -0
- package/lite/announce.test.d.ts.map +1 -0
- package/lite/announce.test.js +71 -0
- package/lite/announce.test.js.map +1 -0
- package/lite/connection.d.ts +45 -19
- package/lite/connection.d.ts.map +1 -1
- package/lite/connection.js +85 -80
- package/lite/connection.js.map +1 -1
- package/lite/datagram.d.ts +23 -0
- package/lite/datagram.d.ts.map +1 -0
- package/lite/datagram.js +57 -0
- package/lite/datagram.js.map +1 -0
- package/lite/datagram.test.d.ts +2 -0
- package/lite/datagram.test.d.ts.map +1 -0
- package/lite/datagram.test.js +26 -0
- package/lite/datagram.test.js.map +1 -0
- package/lite/datagram_stream.d.ts +8 -0
- package/lite/datagram_stream.d.ts.map +1 -0
- package/lite/datagram_stream.js +44 -0
- package/lite/datagram_stream.js.map +1 -0
- package/lite/datagram_stream.test.d.ts +2 -0
- package/lite/datagram_stream.test.d.ts.map +1 -0
- package/lite/datagram_stream.test.js +15 -0
- package/lite/datagram_stream.test.js.map +1 -0
- package/lite/fetch.d.ts.map +1 -1
- package/lite/fetch.js +2 -2
- package/lite/fetch.js.map +1 -1
- package/lite/fetch.test.d.ts +2 -0
- package/lite/fetch.test.d.ts.map +1 -0
- package/lite/fetch.test.js +41 -0
- package/lite/fetch.test.js.map +1 -0
- package/lite/index.d.ts +3 -0
- package/lite/index.d.ts.map +1 -1
- package/lite/index.js +3 -0
- package/lite/index.js.map +1 -1
- package/lite/publisher.d.ts +13 -6
- package/lite/publisher.d.ts.map +1 -1
- package/lite/publisher.js +274 -64
- package/lite/publisher.js.map +1 -1
- package/lite/publisher.test.d.ts +2 -0
- package/lite/publisher.test.d.ts.map +1 -0
- package/lite/publisher.test.js +65 -0
- package/lite/publisher.test.js.map +1 -0
- package/lite/setup.d.ts +72 -6
- package/lite/setup.d.ts.map +1 -1
- package/lite/setup.js +184 -31
- package/lite/setup.js.map +1 -1
- package/lite/setup.test.d.ts +2 -0
- package/lite/setup.test.d.ts.map +1 -0
- package/lite/setup.test.js +122 -0
- package/lite/setup.test.js.map +1 -0
- package/lite/stream.d.ts +12 -3
- package/lite/stream.d.ts.map +1 -1
- package/lite/stream.js +7 -4
- package/lite/stream.js.map +1 -1
- package/lite/subscribe.d.ts +37 -23
- package/lite/subscribe.d.ts.map +1 -1
- package/lite/subscribe.js +70 -55
- package/lite/subscribe.js.map +1 -1
- package/lite/subscribe.test.d.ts +2 -0
- package/lite/subscribe.test.d.ts.map +1 -0
- package/lite/subscribe.test.js +68 -0
- package/lite/subscribe.test.js.map +1 -0
- package/lite/subscriber.d.ts +24 -17
- package/lite/subscriber.d.ts.map +1 -1
- package/lite/subscriber.js +515 -90
- package/lite/subscriber.js.map +1 -1
- package/lite/subscriber.test.js +2 -2
- package/lite/subscriber.test.js.map +1 -1
- package/lite/track.d.ts +25 -13
- package/lite/track.d.ts.map +1 -1
- package/lite/track.js +60 -35
- package/lite/track.js.map +1 -1
- package/lite/track.test.js +12 -10
- package/lite/track.test.js.map +1 -1
- package/lite/version.d.ts +17 -4
- package/lite/version.d.ts.map +1 -1
- package/lite/version.js +88 -17
- package/lite/version.js.map +1 -1
- package/mock.d.ts +42 -3
- package/mock.d.ts.map +1 -1
- package/mock.js +88 -17
- package/mock.js.map +1 -1
- package/package.json +3 -3
- package/path.d.ts +30 -0
- package/path.d.ts.map +1 -1
- package/path.js +49 -0
- package/path.js.map +1 -1
- package/path.test.js +41 -0
- package/path.test.js.map +1 -1
- package/stream.test.js +10 -0
- package/stream.test.js.map +1 -1
- package/time.d.ts +109 -86
- package/time.d.ts.map +1 -1
- package/time.js +80 -30
- package/time.js.map +1 -1
- package/time.test.d.ts +2 -0
- package/time.test.d.ts.map +1 -0
- package/time.test.js +12 -0
- package/time.test.js.map +1 -0
- package/track.d.ts +225 -45
- package/track.d.ts.map +1 -1
- package/track.js +505 -93
- package/track.js.map +1 -1
- package/track.test.js +138 -29
- package/track.test.js.map +1 -1
- package/util/error.d.ts +9 -0
- package/util/error.d.ts.map +1 -1
- package/util/error.js +21 -0
- package/util/error.js.map +1 -1
- package/util/error.test.d.ts +2 -0
- package/util/error.test.d.ts.map +1 -0
- package/util/error.test.js +45 -0
- package/util/error.test.js.map +1 -0
- package/util/index.d.ts +1 -0
- package/util/index.d.ts.map +1 -1
- package/util/index.js +1 -0
- package/util/index.js.map +1 -1
- package/util/timeout.d.ts +5 -0
- package/util/timeout.d.ts.map +1 -0
- package/util/timeout.js +18 -0
- package/util/timeout.js.map +1 -0
- package/util/timeout.test.d.ts +2 -0
- package/util/timeout.test.d.ts.map +1 -0
- package/util/timeout.test.js +18 -0
- package/util/timeout.test.js.map +1 -0
- package/varint.d.ts +6 -0
- package/varint.d.ts.map +1 -1
- package/varint.js +6 -2
- package/varint.js.map +1 -1
- package/zod.d.ts +4 -4
- package/zod.d.ts.map +1 -1
- package/zod.js.map +1 -1
- package/bandwidth.d.ts +0 -11
- package/bandwidth.d.ts.map +0 -1
- package/bandwidth.js +0 -7
- package/bandwidth.js.map +0 -1
|
@@ -1,27 +1,41 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type
|
|
3
|
-
import type
|
|
4
|
-
import type { Broadcast } from "../broadcast";
|
|
1
|
+
import type { Getter } from "@moq/signals";
|
|
2
|
+
import type * as announce from "../announced";
|
|
3
|
+
import type * as broadcast from "../broadcast";
|
|
5
4
|
import type * as Path from "../path";
|
|
6
|
-
import type
|
|
5
|
+
import type { Probe, Stats } from "./stats";
|
|
6
|
+
import type { Transport } from "./transport";
|
|
7
7
|
/** An established MoQ session, implemented by both the moq-lite and moq-ietf protocols. */
|
|
8
8
|
export interface Established {
|
|
9
9
|
/** URL of the connected server. */
|
|
10
10
|
readonly url: URL;
|
|
11
11
|
/** Negotiated wire protocol version. */
|
|
12
12
|
readonly version: string;
|
|
13
|
-
/**
|
|
14
|
-
readonly
|
|
15
|
-
/**
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
13
|
+
/** The wire transport this session runs over. */
|
|
14
|
+
readonly transport: Transport;
|
|
15
|
+
/**
|
|
16
|
+
* Estimates measured by the peer, updated as PROBE messages arrive. Stays empty on
|
|
17
|
+
* versions without PROBE. See {@link Stats} for what the local transport counts.
|
|
18
|
+
*/
|
|
19
|
+
readonly probe: Getter<Probe>;
|
|
20
|
+
/**
|
|
21
|
+
* Whether the relay supports broadcast discovery: announcing which broadcasts exist under a
|
|
22
|
+
* prefix. When false, {@link announced} never yields, so a consumer must subscribe blind
|
|
23
|
+
* rather than wait for an announcement. Set via `discovery` on the connect options.
|
|
24
|
+
*/
|
|
25
|
+
readonly discovery: boolean;
|
|
26
|
+
/** Subscribe to broadcast announcements under an optional path prefix, returning paths relative to that prefix. */
|
|
27
|
+
announced(prefix?: Path.Valid): announce.Consumer;
|
|
21
28
|
/** Publish a broadcast at the given path. */
|
|
22
|
-
publish(path: Path.Valid, broadcast:
|
|
29
|
+
publish(path: Path.Valid, broadcast: broadcast.Producer): void;
|
|
23
30
|
/** Consume the broadcast at the given path. */
|
|
24
|
-
consume(
|
|
31
|
+
consume(path: Path.Valid): broadcast.Consumer;
|
|
32
|
+
/**
|
|
33
|
+
* Snapshot the transport's counters, querying it fresh on each call.
|
|
34
|
+
*
|
|
35
|
+
* Resolves to an empty snapshot on a transport without `getStats()`. Sample it on
|
|
36
|
+
* whatever schedule you need rather than expecting the library to poll for you.
|
|
37
|
+
*/
|
|
38
|
+
stats(): Promise<Stats>;
|
|
25
39
|
/** Close the session. */
|
|
26
40
|
close(): void;
|
|
27
41
|
/** Resolves when the session closes. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"established.d.ts","sourceRoot":"","sources":["../../src/connection/established.ts"],"names":[],"mappings":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ACzB,KAAK,IAAI,IAAI,CAAC;IAEd,wCAAwC;IACxC,MAAM,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;CACtB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"established.js","sourceRoot":"","sources":["../../src/connection/established.ts"],"names":[],"mappings":"","sourcesContent":["import type {
|
|
1
|
+
{"version":3,"file":"established.js","sourceRoot":"","sources":["../../src/connection/established.ts"],"names":[],"mappings":"","sourcesContent":["import type { Getter } from \"@moq/signals\";\nimport type * as announce from \"../announced.ts\";\nimport type * as broadcast from \"../broadcast.ts\";\nimport type * as Path from \"../path.ts\";\nimport type { Probe, Stats } from \"./stats.ts\";\nimport type { Transport } from \"./transport.ts\";\n\n/** An established MoQ session, implemented by both the moq-lite and moq-ietf protocols. */\nexport interface Established {\n\t/** URL of the connected server. */\n\treadonly url: URL;\n\n\t/** Negotiated wire protocol version. */\n\treadonly version: string;\n\n\t/** The wire transport this session runs over. */\n\treadonly transport: Transport;\n\n\t/**\n\t * Estimates measured by the peer, updated as PROBE messages arrive. Stays empty on\n\t * versions without PROBE. See {@link Stats} for what the local transport counts.\n\t */\n\treadonly probe: Getter<Probe>;\n\n\t/**\n\t * Whether the relay supports broadcast discovery: announcing which broadcasts exist under a\n\t * prefix. When false, {@link announced} never yields, so a consumer must subscribe blind\n\t * rather than wait for an announcement. Set via `discovery` on the connect options.\n\t */\n\treadonly discovery: boolean;\n\n\t/** Subscribe to broadcast announcements under an optional path prefix, returning paths relative to that prefix. */\n\tannounced(prefix?: Path.Valid): announce.Consumer;\n\n\t/** Publish a broadcast at the given path. */\n\tpublish(path: Path.Valid, broadcast: broadcast.Producer): void;\n\n\t/** Consume the broadcast at the given path. */\n\tconsume(path: Path.Valid): broadcast.Consumer;\n\n\t/**\n\t * Snapshot the transport's counters, querying it fresh on each call.\n\t *\n\t * Resolves to an empty snapshot on a transport without `getStats()`. Sample it on\n\t * whatever schedule you need rather than expecting the library to poll for you.\n\t */\n\tstats(): Promise<Stats>;\n\n\t/** Close the session. */\n\tclose(): void;\n\n\t/** Resolves when the session closes. */\n\tclosed: Promise<void>;\n}\n"]}
|
package/connection/index.d.ts
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Connection helpers: connect to or accept a MoQ session and reconnect on failure.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
1
6
|
export * from "./accept";
|
|
7
|
+
export { isWebTransportSupported } from "./browser";
|
|
2
8
|
export * from "./connect";
|
|
3
9
|
export * from "./established";
|
|
4
10
|
export * from "./reload";
|
|
11
|
+
export type { Probe, Stats } from "./stats";
|
|
12
|
+
export type { Transport } from "./transport";
|
|
5
13
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/connection/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,kBAAkB,CAAC;AACjC,cAAc,aAAa,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/connection/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,cAAc,aAAa,CAAC;AAC5B,OAAO,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAC;AACvD,cAAc,cAAc,CAAC;AAC7B,cAAc,kBAAkB,CAAC;AACjC,cAAc,aAAa,CAAC;AAC5B,YAAY,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAC/C,YAAY,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC"}
|
package/connection/index.js
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
/* @ts-self-types="./index.d.ts" */
|
|
2
|
+
/**
|
|
3
|
+
* Connection helpers: connect to or accept a MoQ session and reconnect on failure.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
2
7
|
export * from "./accept.js";
|
|
8
|
+
export { isWebTransportSupported } from "./browser.js";
|
|
3
9
|
export * from "./connect.js";
|
|
4
10
|
export * from "./established.js";
|
|
5
11
|
export * from "./reload.js";
|
package/connection/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/connection/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,kBAAkB,CAAC;AACjC,cAAc,aAAa,CAAC","sourcesContent":["
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/connection/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,cAAc,aAAa,CAAC;AAC5B,OAAO,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAC;AACvD,cAAc,cAAc,CAAC;AAC7B,cAAc,kBAAkB,CAAC;AACjC,cAAc,aAAa,CAAC","sourcesContent":["/**\n * Connection helpers: connect to or accept a MoQ session and reconnect on failure.\n *\n * @module\n */\nexport * from \"./accept.ts\";\nexport { isWebTransportSupported } from \"./browser.ts\";\nexport * from \"./connect.ts\";\nexport * from \"./established.ts\";\nexport * from \"./reload.ts\";\nexport type { Probe, Stats } from \"./stats.ts\";\nexport type { Transport } from \"./transport.ts\";\n"]}
|
package/connection/reload.d.ts
CHANGED
|
@@ -1,18 +1,31 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type Getter, Signal } from "@moq/signals";
|
|
2
|
+
import * as Announce from "../announced";
|
|
2
3
|
import type * as Path from "../path";
|
|
3
4
|
import { type ConnectProps, type WebSocketOptions, type WebTransportProps } from "./connect";
|
|
4
5
|
import type { Established } from "./established";
|
|
6
|
+
import type { Probe, Stats } from "./stats";
|
|
5
7
|
/** Exponential backoff settings for {@link Reload}'s reconnect loop. */
|
|
6
8
|
export type ReloadDelay = {
|
|
9
|
+
/** The delay in milliseconds before reconnecting (default: 1000). */
|
|
7
10
|
initial: DOMHighResTimeStamp;
|
|
11
|
+
/** The multiplier for the delay (default: 2). */
|
|
8
12
|
multiplier: number;
|
|
13
|
+
/** The maximum delay in milliseconds (default: 30000). */
|
|
9
14
|
max: DOMHighResTimeStamp;
|
|
15
|
+
/**
|
|
16
|
+
* Maximum total time in milliseconds to spend retrying before giving up (default:
|
|
17
|
+
* 300000, 5 minutes). Resets after each successful connection. Set to 0 for
|
|
18
|
+
* unlimited retries.
|
|
19
|
+
*/
|
|
10
20
|
timeout?: DOMHighResTimeStamp;
|
|
11
21
|
};
|
|
12
|
-
/**
|
|
13
|
-
export type ReloadProps = ConnectProps & {
|
|
22
|
+
/** Connection and retry options for {@link Reload}. */
|
|
23
|
+
export type ReloadProps = Omit<ConnectProps, "signal"> & {
|
|
24
|
+
/** Whether to reload the connection when it disconnects (default: true). */
|
|
14
25
|
enabled?: boolean | Signal<boolean>;
|
|
26
|
+
/** The URL of the relay server. */
|
|
15
27
|
url?: URL | Signal<URL | undefined>;
|
|
28
|
+
/** Backoff settings for the reconnect loop. */
|
|
16
29
|
delay?: ReloadDelay;
|
|
17
30
|
};
|
|
18
31
|
/** Current state of a {@link Reload} connection. */
|
|
@@ -28,19 +41,42 @@ export declare class Reload {
|
|
|
28
41
|
status: Signal<ReloadStatus>;
|
|
29
42
|
/** The currently established session, or undefined while disconnected. */
|
|
30
43
|
established: Signal<Established | undefined>;
|
|
31
|
-
/**
|
|
32
|
-
|
|
44
|
+
/**
|
|
45
|
+
* The current connection's PROBE estimates, spanning reconnects.
|
|
46
|
+
*
|
|
47
|
+
* Undefined while disconnected: the estimates belong to a single connection.
|
|
48
|
+
* See {@link Established.probe}.
|
|
49
|
+
*/
|
|
50
|
+
readonly probe: Getter<Probe | undefined>;
|
|
33
51
|
/** WebTransport options applied to each connection attempt (not reactive). */
|
|
34
52
|
webtransport?: WebTransportProps;
|
|
35
53
|
/** WebSocket fallback options applied to each connection attempt (not reactive). */
|
|
36
54
|
websocket: WebSocketOptions | undefined;
|
|
55
|
+
/**
|
|
56
|
+
* Whether the relay supports broadcast discovery, applied to each connection attempt (not
|
|
57
|
+
* reactive). Undefined defers to the default for the URL. See {@link Established.discovery}.
|
|
58
|
+
*/
|
|
59
|
+
discovery?: boolean;
|
|
37
60
|
/** Backoff settings for the reconnect loop. */
|
|
38
61
|
delay: ReloadDelay;
|
|
39
|
-
/** The reactive effect scope driving the connect loop; closed by {@link Reload.close}. */
|
|
40
|
-
signals: Effect;
|
|
41
62
|
/** Resolves when the reconnect loop stops via {@link Reload.close} or the retry timeout. */
|
|
42
63
|
closed: Promise<void>;
|
|
43
64
|
constructor(props?: ReloadProps);
|
|
65
|
+
/**
|
|
66
|
+
* Subscribe to broadcast announcements under an optional prefix, spanning reconnects.
|
|
67
|
+
*
|
|
68
|
+
* The same {@link Announce.Consumer} stream as {@link Established.announced}, but everything active
|
|
69
|
+
* is retracted (an `active: false` update) whenever the connection drops and re-announced on
|
|
70
|
+
* reconnect, so a consumer draining `next()` never clings to a dead route across a reconnect.
|
|
71
|
+
*
|
|
72
|
+
* Stays empty while the relay lacks {@link Established.discovery}.
|
|
73
|
+
*/
|
|
74
|
+
announced(prefix?: Path.Valid): Announce.Consumer;
|
|
75
|
+
/**
|
|
76
|
+
* Snapshot the live connection's transport counters, or undefined while disconnected.
|
|
77
|
+
* See {@link Established.stats}.
|
|
78
|
+
*/
|
|
79
|
+
stats(): Promise<Stats | undefined>;
|
|
44
80
|
/** Stop reconnecting, close the current connection, and resolve {@link Reload.closed}. */
|
|
45
81
|
close(): void;
|
|
46
82
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"reload.d.ts","sourceRoot":"","sources":["../../src/connection/reload.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"reload.d.ts","sourceRoot":"","sources":["../../src/connection/reload.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,KAAK,MAAM,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAC3D,OA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;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC,KAAK,GAAG,SAAS,CAAC,CAAC;IAE1C,8EAA8E;IAC9E,YAAY,CAAC,EAAE,iBAAiB,CAAC;IAEjC,oFAAoF;IACpF,SAAS,EAAE,gBAAgB,GAAG,SAAS,CAAC;IAExC;;;OAGG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;IAEpB,+CAA+C;IAC/C,KAAK,EAAE,WAAW,CAAC;IAKnB,4FAA4F;IAC5F,MAAM,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IAkBtB,YAAY,KAAK,CAAC,EAAE,WAAW,EAgC9B;IAmGD;;;;;;;;OAQG;IACH,SAAS,CAAC,MAAM,GAAE,IAAI,CAAC,KAAmB,GAAG,QAAQ,CAAC,QAAQ,CAoD7D;IAED;;;OAGG;IACG,KAAK,IAAI,OAAO,CAAC,KAAK,GAAG,SAAS,CAAC,CAExC;IAED,0FAA0F;IAC1F,KAAK,SAGJ;CACD"}
|
package/connection/reload.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/* @ts-self-types="./reload.d.ts" */
|
|
2
2
|
import { Effect, Signal } from "@moq/signals";
|
|
3
|
+
import * as Announce from "../announced.js";
|
|
3
4
|
import { empty as emptyPath } from "../path.js";
|
|
4
5
|
import { connect } from "./connect.js";
|
|
5
6
|
/** Maintains a MoQ connection, reconnecting with exponential backoff when it drops. */
|
|
@@ -12,18 +13,26 @@ export class Reload {
|
|
|
12
13
|
status = new Signal("disconnected");
|
|
13
14
|
/** The currently established session, or undefined while disconnected. */
|
|
14
15
|
established = new Signal(undefined);
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
/**
|
|
17
|
+
* The current connection's PROBE estimates, spanning reconnects.
|
|
18
|
+
*
|
|
19
|
+
* Undefined while disconnected: the estimates belong to a single connection.
|
|
20
|
+
* See {@link Established.probe}.
|
|
21
|
+
*/
|
|
22
|
+
probe;
|
|
19
23
|
/** WebTransport options applied to each connection attempt (not reactive). */
|
|
20
24
|
webtransport;
|
|
21
25
|
/** WebSocket fallback options applied to each connection attempt (not reactive). */
|
|
22
26
|
websocket;
|
|
27
|
+
/**
|
|
28
|
+
* Whether the relay supports broadcast discovery, applied to each connection attempt (not
|
|
29
|
+
* reactive). Undefined defers to the default for the URL. See {@link Established.discovery}.
|
|
30
|
+
*/
|
|
31
|
+
discovery;
|
|
23
32
|
/** Backoff settings for the reconnect loop. */
|
|
24
33
|
delay;
|
|
25
34
|
/** The reactive effect scope driving the connect loop; closed by {@link Reload.close}. */
|
|
26
|
-
signals = new Effect();
|
|
35
|
+
#signals = new Effect();
|
|
27
36
|
/** Resolves when the reconnect loop stops via {@link Reload.close} or the retry timeout. */
|
|
28
37
|
closed;
|
|
29
38
|
#closedResolve;
|
|
@@ -33,6 +42,8 @@ export class Reload {
|
|
|
33
42
|
#retryStart;
|
|
34
43
|
// Increased by 1 each time to trigger a reload.
|
|
35
44
|
#tick = new Signal(0);
|
|
45
|
+
// True after the browser freezes or hides the page until it visibly resumes.
|
|
46
|
+
#suspended = new Signal(false);
|
|
36
47
|
// Use the serialized URL as the reactive connection key. URL objects use identity
|
|
37
48
|
// equality, but replacing one with an equivalent instance should not reconnect.
|
|
38
49
|
#url;
|
|
@@ -42,100 +53,186 @@ export class Reload {
|
|
|
42
53
|
this.delay = props?.delay ?? { initial: 1000, multiplier: 2, max: 30000 };
|
|
43
54
|
this.webtransport = props?.webtransport;
|
|
44
55
|
this.websocket = props?.websocket;
|
|
56
|
+
this.discovery = props?.discovery;
|
|
45
57
|
this.#delay = this.delay.initial;
|
|
46
58
|
this.closed = new Promise((resolve, reject) => {
|
|
47
59
|
this.#closedResolve = resolve;
|
|
48
60
|
this.#closedReject = reject;
|
|
49
61
|
});
|
|
50
|
-
|
|
62
|
+
if (typeof window !== "undefined" && typeof document !== "undefined") {
|
|
63
|
+
this.#signals.event(window, "pagehide", () => this.#suspended.set(true));
|
|
64
|
+
this.#signals.event(window, "pageshow", () => this.#suspended.set(false));
|
|
65
|
+
this.#signals.event(window, "unload", () => this.#suspended.set(true));
|
|
66
|
+
this.#signals.event(document, "visibilitychange", () => {
|
|
67
|
+
if (!document.hidden)
|
|
68
|
+
this.#suspended.set(false);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
this.probe = this.#signals.computed((effect) => {
|
|
72
|
+
const connection = effect.get(this.established);
|
|
73
|
+
return connection && effect.get(connection.probe);
|
|
74
|
+
});
|
|
75
|
+
this.#url = this.#signals.computed((effect) => effect.get(this.url)?.href);
|
|
51
76
|
// Create a reactive root so cleanup is easier.
|
|
52
|
-
this
|
|
53
|
-
this.signals.run(this.#runAnnounced.bind(this));
|
|
77
|
+
this.#signals.run(this.#connect.bind(this));
|
|
54
78
|
}
|
|
55
79
|
#connect(effect) {
|
|
56
80
|
// Will retry when the tick changes.
|
|
57
81
|
effect.get(this.#tick);
|
|
82
|
+
const suspended = effect.get(this.#suspended);
|
|
58
83
|
const enabled = effect.get(this.enabled);
|
|
59
|
-
if (!enabled)
|
|
84
|
+
if (!enabled || suspended)
|
|
60
85
|
return;
|
|
61
86
|
const href = effect.get(this.#url);
|
|
62
87
|
if (!href)
|
|
63
88
|
return;
|
|
64
89
|
const url = new URL(href);
|
|
65
90
|
effect.set(this.status, "connecting", "disconnected");
|
|
91
|
+
// This run's teardown, handed to connect() so a rerun cancels the attempt in flight.
|
|
92
|
+
const signal = effect.abort;
|
|
66
93
|
effect.spawn(async () => {
|
|
94
|
+
// Set once the session is live, so #retry can tell a healthy session that
|
|
95
|
+
// later dropped from a connect failure or a peer that flaps immediately.
|
|
96
|
+
let connected;
|
|
67
97
|
try {
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
98
|
+
const connection = await connect(url, {
|
|
99
|
+
websocket: this.websocket,
|
|
100
|
+
webtransport: this.webtransport,
|
|
101
|
+
discovery: this.discovery,
|
|
102
|
+
signal,
|
|
103
|
+
});
|
|
104
|
+
// Hand the connection to the effect, which closes it now if this run is already over.
|
|
105
|
+
effect.cleanup(() => connection.close());
|
|
106
|
+
if (signal.aborted)
|
|
72
107
|
return;
|
|
73
|
-
}
|
|
74
108
|
effect.set(this.established, connection);
|
|
75
|
-
effect.cleanup(() => connection.close());
|
|
76
109
|
effect.set(this.status, "connected", "disconnected");
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
this
|
|
80
|
-
await Promise.race([effect.cancel, connection.closed]);
|
|
110
|
+
connected = performance.now();
|
|
111
|
+
// A cancelled effect resolves undefined, so the sentinel tells the session
|
|
112
|
+
// closing apart from this run being torn down.
|
|
113
|
+
const closed = await Promise.race([effect.cancel, connection.closed.then(() => true)]);
|
|
114
|
+
if (!closed)
|
|
115
|
+
return;
|
|
116
|
+
console.warn("connection closed, reconnecting");
|
|
117
|
+
this.#retry(effect, connected);
|
|
81
118
|
}
|
|
82
119
|
catch (err) {
|
|
120
|
+
// Treat teardown as cancellation, not a connection failure.
|
|
121
|
+
if (signal.aborted)
|
|
122
|
+
return;
|
|
83
123
|
console.warn("connection error:", err);
|
|
84
|
-
|
|
85
|
-
this.#retryStart ??= performance.now();
|
|
86
|
-
const timeout = this.delay.timeout ?? 300000;
|
|
87
|
-
if (timeout > 0) {
|
|
88
|
-
const elapsed = performance.now() - this.#retryStart;
|
|
89
|
-
if (elapsed >= timeout) {
|
|
90
|
-
console.warn("reconnect timed out");
|
|
91
|
-
this.#closedReject(err instanceof Error ? err : new Error(String(err)));
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
const tick = this.#tick.peek() + 1;
|
|
96
|
-
effect.timer(() => this.#tick.update((prev) => Math.max(prev, tick)), this.#delay);
|
|
97
|
-
this.#delay = Math.min(this.#delay * this.delay.multiplier, this.delay.max);
|
|
124
|
+
this.#retry(effect, connected, err);
|
|
98
125
|
}
|
|
99
126
|
});
|
|
100
127
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
//
|
|
108
|
-
//
|
|
109
|
-
|
|
110
|
-
|
|
128
|
+
/**
|
|
129
|
+
* Schedule the next connect attempt after the current backoff, or give up when the
|
|
130
|
+
* retry window has expired. `connected` is when the dead session was established, if
|
|
131
|
+
* it ever was, and `cause` the error that killed it, if it died with one.
|
|
132
|
+
*/
|
|
133
|
+
#retry(effect, connected, cause) {
|
|
134
|
+
// Any session is dead now: report disconnected during the backoff rather than
|
|
135
|
+
// when the retry reruns the effect.
|
|
136
|
+
this.established.set(undefined);
|
|
137
|
+
this.status.set("disconnected");
|
|
138
|
+
// A session that outlived the initial delay was healthy, so clear the backoff and
|
|
139
|
+
// start a fresh retry window: a one-off drop should reconnect promptly. Anything
|
|
140
|
+
// shorter is a peer that accepts and immediately severs, which has to keep
|
|
141
|
+
// escalating or we hammer it forever at the initial delay.
|
|
142
|
+
if (connected !== undefined && performance.now() - connected >= this.delay.initial) {
|
|
143
|
+
this.#delay = this.delay.initial;
|
|
144
|
+
this.#retryStart = undefined;
|
|
111
145
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
146
|
+
// Track retry start for timeout.
|
|
147
|
+
this.#retryStart ??= performance.now();
|
|
148
|
+
const timeout = this.delay.timeout ?? 300000;
|
|
149
|
+
if (timeout > 0) {
|
|
150
|
+
const elapsed = performance.now() - this.#retryStart;
|
|
151
|
+
if (elapsed >= timeout) {
|
|
152
|
+
console.warn("reconnect timed out");
|
|
153
|
+
// A graceful close has no error, so report the timeout itself.
|
|
154
|
+
if (cause === undefined)
|
|
155
|
+
this.#closedReject(new Error("reconnect timed out"));
|
|
156
|
+
else
|
|
157
|
+
this.#closedReject(cause instanceof Error ? cause : new Error(String(cause)));
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
const tick = this.#tick.peek() + 1;
|
|
162
|
+
effect.timer(() => this.#tick.update((prev) => Math.max(prev, tick)), this.#delay);
|
|
163
|
+
this.#delay = Math.min(this.#delay * this.delay.multiplier, this.delay.max);
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Subscribe to broadcast announcements under an optional prefix, spanning reconnects.
|
|
167
|
+
*
|
|
168
|
+
* The same {@link Announce.Consumer} stream as {@link Established.announced}, but everything active
|
|
169
|
+
* is retracted (an `active: false` update) whenever the connection drops and re-announced on
|
|
170
|
+
* reconnect, so a consumer draining `next()` never clings to a dead route across a reconnect.
|
|
171
|
+
*
|
|
172
|
+
* Stays empty while the relay lacks {@link Established.discovery}.
|
|
173
|
+
*/
|
|
174
|
+
announced(prefix = emptyPath()) {
|
|
175
|
+
const producer = new Announce.Producer(prefix);
|
|
176
|
+
const consumer = producer.consume();
|
|
177
|
+
// Closing the consumer closes the shared state, so stop appending after that.
|
|
178
|
+
let closed = false;
|
|
179
|
+
void consumer.closed.then(() => {
|
|
180
|
+
closed = true;
|
|
181
|
+
});
|
|
182
|
+
const pump = new Effect();
|
|
183
|
+
pump.run((effect) => {
|
|
184
|
+
const conn = effect.get(this.established);
|
|
185
|
+
if (!conn)
|
|
186
|
+
return;
|
|
187
|
+
// Without discovery the upstream announce stream never yields, so leave the
|
|
188
|
+
// consumer empty rather than opening a subscription that can't be answered.
|
|
189
|
+
if (!conn.discovery)
|
|
190
|
+
return;
|
|
191
|
+
const upstream = conn.announced(prefix);
|
|
192
|
+
effect.cleanup(() => upstream.close());
|
|
193
|
+
// Track what this connection announced so we can retract it if the connection drops.
|
|
194
|
+
const active = new Set();
|
|
195
|
+
effect.spawn(async () => {
|
|
196
|
+
try {
|
|
197
|
+
for (;;) {
|
|
198
|
+
const entry = await Promise.race([effect.cancel, upstream.next()]);
|
|
199
|
+
if (!entry)
|
|
200
|
+
break;
|
|
201
|
+
if (entry.active)
|
|
122
202
|
active.add(entry.path);
|
|
123
|
-
|
|
124
|
-
else {
|
|
203
|
+
else
|
|
125
204
|
active.delete(entry.path);
|
|
205
|
+
producer.append(entry);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
catch {
|
|
209
|
+
// A dropped connection resets the announce stream; the retractions below cover it.
|
|
210
|
+
}
|
|
211
|
+
finally {
|
|
212
|
+
// Retract everything from the connection that just went away, so a per-broadcast
|
|
213
|
+
// watcher tears down instead of clinging to the dead route.
|
|
214
|
+
if (!closed) {
|
|
215
|
+
for (const path of active) {
|
|
216
|
+
producer.append({ path, active: false });
|
|
126
217
|
}
|
|
127
|
-
}
|
|
218
|
+
}
|
|
128
219
|
}
|
|
129
|
-
}
|
|
130
|
-
catch (err) {
|
|
131
|
-
this.#announced.set(new Set());
|
|
132
|
-
throw err;
|
|
133
|
-
}
|
|
220
|
+
});
|
|
134
221
|
});
|
|
222
|
+
this.#signals.cleanup(() => pump.close());
|
|
223
|
+
void consumer.closed.then(() => pump.close());
|
|
224
|
+
return consumer;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Snapshot the live connection's transport counters, or undefined while disconnected.
|
|
228
|
+
* See {@link Established.stats}.
|
|
229
|
+
*/
|
|
230
|
+
async stats() {
|
|
231
|
+
return this.established.peek()?.stats();
|
|
135
232
|
}
|
|
136
233
|
/** Stop reconnecting, close the current connection, and resolve {@link Reload.closed}. */
|
|
137
234
|
close() {
|
|
138
|
-
this
|
|
235
|
+
this.#signals.close();
|
|
139
236
|
this.#closedResolve();
|
|
140
237
|
}
|
|
141
238
|
}
|
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;AAE3D,OAAO,EAAE,KAAK,IAAI,SAAS,EAAE,MAAM,YAAY,CAAC;AAChD,OAAO,EAAqB,OAAO,EAAiD,MAAM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wD;QACxD,IAAI,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,QAAQ,CAAC,mBAAmB,CAAC,EAAE,CAAC;YACrD,OAAO;QACR,CAAC;QAED,MAAM,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC,SAAS,EAAE,CAAC,CAAC;QAC9C,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC,CAAC;QAExC,MAAM,CAAC,KAAK,CAAC,KAAK,IAAI,EAAE;YACvB,IAAI,CAAC;gBACJ,SAAS,CAAC;oBACT,MAAM,KAAK,GAAG,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;oBACpE,IAAI,CAAC,KAAK;wBAAE,MAAM;oBAElB,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,EAAE;wBACjC,IAAI,KAAK,CAAC,MAAM,EAAE,CAAC;4BAClB,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;wBACxB,CAAC;6BAAM,CAAC;4BACP,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;wBAC3B,CAAC;oBACF,CAAC,CAAC,CAAC;gBACJ,CAAC;YACF,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACd,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,GAAG,EAAE,CAAC,CAAC;gBAC/B,MAAM,GAAG,CAAC;YACX,CAAC;QACF,CAAC,CAAC,CAAC;IACJ,CAAC;IAED,0FAA0F;IAC1F,KAAK;QACJ,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;QACrB,IAAI,CAAC,cAAc,EAAE,CAAC;IACvB,CAAC;CACD","sourcesContent":["import { Effect, type Getter, Signal } from \"@moq/signals\";\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\";\n\n/** Exponential backoff settings for {@link Reload}'s reconnect loop. */\nexport type ReloadDelay = {\n\t// The delay in milliseconds before reconnecting.\n\t// default: 1000\n\tinitial: DOMHighResTimeStamp;\n\n\t// The multiplier for the delay.\n\t// default: 2\n\tmultiplier: number;\n\n\t// The maximum delay in milliseconds.\n\t// default: 30000\n\tmax: DOMHighResTimeStamp;\n\n\t// Maximum total time in milliseconds to spend retrying before giving up.\n\t// Resets after each successful connection. Set to 0 for unlimited retries.\n\t// default: 300000 (5 minutes)\n\ttimeout?: DOMHighResTimeStamp;\n};\n\n/** Options for {@link Reload}: connect options plus reactive URL/enabled signals and backoff tuning. */\nexport type ReloadProps = ConnectProps & {\n\t// Whether to reload the connection when it disconnects.\n\t// 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// The delay for the reload.\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// All actively announced broadcast paths, updated reactively.\n\t#announced = new Signal<Set<Path.Valid>>(new Set());\n\n\t/** The set of broadcast paths currently announced by the server, updated reactively. */\n\treadonly announced: Getter<Set<Path.Valid>> = this.#announced;\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/** 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\tsignals = 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// 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\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\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\tthis.#url = this.signals.computed((effect) => effect.get(this.url)?.href);\n\n\t\t// Create a reactive root so cleanup is easier.\n\t\tthis.signals.run(this.#connect.bind(this));\n\t\tthis.signals.run(this.#runAnnounced.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 enabled = effect.get(this.enabled);\n\t\tif (!enabled) 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\teffect.spawn(async () => {\n\t\t\ttry {\n\t\t\t\tconst pending = connect(url, { websocket: this.websocket, webtransport: this.webtransport });\n\n\t\t\t\tconst connection = await Promise.race([effect.cancel, pending]);\n\t\t\t\tif (!connection) {\n\t\t\t\t\tpending.then((conn) => conn.close()).catch(() => {});\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\teffect.set(this.established, connection);\n\t\t\t\teffect.cleanup(() => connection.close());\n\n\t\t\t\teffect.set(this.status, \"connected\", \"disconnected\");\n\n\t\t\t\t// Reset the exponential backoff and timeout on success.\n\t\t\t\tthis.#delay = this.delay.initial;\n\t\t\t\tthis.#retryStart = undefined;\n\n\t\t\t\tawait Promise.race([effect.cancel, connection.closed]);\n\t\t\t} catch (err) {\n\t\t\t\tconsole.warn(\"connection error:\", err);\n\n\t\t\t\t// Track retry start for timeout.\n\t\t\t\tthis.#retryStart ??= performance.now();\n\n\t\t\t\tconst timeout = this.delay.timeout ?? 300000;\n\t\t\t\tif (timeout > 0) {\n\t\t\t\t\tconst elapsed = performance.now() - this.#retryStart;\n\t\t\t\t\tif (elapsed >= timeout) {\n\t\t\t\t\t\tconsole.warn(\"reconnect timed out\");\n\t\t\t\t\t\tthis.#closedReject(err instanceof Error ? err : new Error(String(err)));\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tconst tick = this.#tick.peek() + 1;\n\t\t\t\teffect.timer(() => this.#tick.update((prev) => Math.max(prev, tick)), this.#delay);\n\n\t\t\t\tthis.#delay = Math.min(this.#delay * this.delay.multiplier, this.delay.max);\n\t\t\t}\n\t\t});\n\t}\n\n\t#runAnnounced(effect: Effect): void {\n\t\tthis.#announced.set(new Set());\n\n\t\tconst conn = effect.get(this.established);\n\t\tif (!conn) return;\n\n\t\teffect.cleanup(() => this.#announced.set(new Set()));\n\n\t\t// Cloudflare's relay does not yet support SUBSCRIBE_NAMESPACE, so\n\t\t// skip announce subscriptions entirely for those hosts.\n\t\tif (conn.url.hostname.endsWith(\"mediaoverquic.com\")) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst announced = conn.announced(emptyPath());\n\t\teffect.cleanup(() => announced.close());\n\n\t\teffect.spawn(async () => {\n\t\t\ttry {\n\t\t\t\tfor (;;) {\n\t\t\t\t\tconst entry = await Promise.race([effect.cancel, announced.next()]);\n\t\t\t\t\tif (!entry) break;\n\n\t\t\t\t\tthis.#announced.mutate((active) => {\n\t\t\t\t\t\tif (entry.active) {\n\t\t\t\t\t\t\tactive.add(entry.path);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tactive.delete(entry.path);\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t} catch (err) {\n\t\t\t\tthis.#announced.set(new Set());\n\t\t\t\tthrow err;\n\t\t\t}\n\t\t});\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
|
+
{"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,SAA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/B,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;oBACxB,CAAC;gBACF,CAAC;gBAAC,MAAM,CAAC;oBACR,mFAAmF;gBACpF,CAAC;wBAAS,CAAC;oBACV,iFAAiF;oBACjF,4DAA4D;oBAC5D,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\tthis.#closedResolve();\n\t}\n}\n"]}
|