@moq/net 0.3.4 → 0.4.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/README.md +2 -2
- package/announce.d.ts +7 -0
- package/announce.d.ts.map +1 -0
- package/announce.js +8 -0
- package/announce.js.map +1 -0
- package/announced.d.ts +35 -91
- package/announced.d.ts.map +1 -1
- package/announced.js +21 -156
- package/announced.js.map +1 -1
- package/bandwidth.d.ts +163 -0
- package/bandwidth.d.ts.map +1 -0
- package/bandwidth.js +304 -0
- package/bandwidth.js.map +1 -0
- package/bandwidth_api.d.ts +7 -0
- package/bandwidth_api.d.ts.map +1 -0
- package/bandwidth_api.js +8 -0
- package/bandwidth_api.js.map +1 -0
- package/broadcast.d.ts +20 -28
- package/broadcast.d.ts.map +1 -1
- package/broadcast.js +60 -42
- package/broadcast.js.map +1 -1
- package/connection/accept.d.ts +16 -1
- package/connection/accept.d.ts.map +1 -1
- package/connection/accept.js +39 -24
- package/connection/accept.js.map +1 -1
- package/connection/browser.d.ts.map +1 -1
- package/connection/browser.js +9 -7
- package/connection/browser.js.map +1 -1
- package/connection/connect.d.ts +30 -6
- package/connection/connect.d.ts.map +1 -1
- package/connection/connect.js +97 -50
- package/connection/connect.js.map +1 -1
- package/connection/established.d.ts +16 -21
- package/connection/established.d.ts.map +1 -1
- package/connection/established.js.map +1 -1
- package/connection/forward.d.ts +2 -0
- package/connection/forward.d.ts.map +1 -0
- package/connection/forward.js +159 -0
- package/connection/forward.js.map +1 -0
- package/connection/handshake.d.ts.map +1 -1
- package/connection/handshake.js +2 -2
- package/connection/handshake.js.map +1 -1
- package/connection/index.d.ts +5 -5
- package/connection/index.d.ts.map +1 -1
- package/connection/index.js +4 -5
- package/connection/index.js.map +1 -1
- package/connection/pool.d.ts +188 -0
- package/connection/pool.d.ts.map +1 -0
- package/connection/pool.js +351 -0
- package/connection/pool.js.map +1 -0
- package/connection/reload.d.ts +14 -93
- package/connection/reload.d.ts.map +1 -1
- package/connection/reload.js +216 -80
- package/connection/reload.js.map +1 -1
- package/connection/stats.d.ts +3 -26
- package/connection/stats.d.ts.map +1 -1
- package/connection/stats.js.map +1 -1
- package/connection/transport.d.ts +0 -7
- package/connection/transport.d.ts.map +1 -1
- package/consume.d.ts +1 -43
- package/consume.d.ts.map +1 -1
- package/consume.js +1 -1
- package/consume.js.map +1 -1
- package/error.d.ts +180 -29
- package/error.d.ts.map +1 -1
- package/error.js +310 -16
- package/error.js.map +1 -1
- package/errors.d.ts +7 -0
- package/errors.d.ts.map +1 -0
- package/errors.js +8 -0
- package/errors.js.map +1 -0
- package/group.d.ts +9 -43
- package/group.d.ts.map +1 -1
- package/group.js +283 -69
- package/group.js.map +1 -1
- package/hop.d.ts +107 -0
- package/hop.d.ts.map +1 -0
- package/hop.js +103 -0
- package/hop.js.map +1 -0
- package/ietf/aliases.d.ts +1 -78
- package/ietf/aliases.d.ts.map +1 -1
- package/ietf/cluster.d.ts +6 -120
- package/ietf/cluster.d.ts.map +1 -1
- package/ietf/cluster.js +81 -41
- package/ietf/cluster.js.map +1 -1
- package/ietf/connection.d.ts +6 -72
- package/ietf/connection.d.ts.map +1 -1
- package/ietf/connection.js +11 -47
- package/ietf/connection.js.map +1 -1
- package/ietf/error.d.ts +11 -0
- package/ietf/error.d.ts.map +1 -0
- package/ietf/error.js +168 -0
- package/ietf/error.js.map +1 -0
- package/ietf/filter.d.ts.map +1 -1
- package/ietf/filter.js +6 -1
- package/ietf/filter.js.map +1 -1
- package/ietf/goaway.d.ts.map +1 -1
- package/ietf/goaway.js +22 -5
- package/ietf/goaway.js.map +1 -1
- package/ietf/parameters.d.ts +2 -2
- package/ietf/parameters.d.ts.map +1 -1
- package/ietf/parameters.js +15 -17
- package/ietf/parameters.js.map +1 -1
- package/ietf/publish_namespace.d.ts +25 -0
- package/ietf/publish_namespace.d.ts.map +1 -1
- package/ietf/publish_namespace.js +63 -0
- package/ietf/publish_namespace.js.map +1 -1
- package/ietf/publisher.d.ts +1 -82
- package/ietf/publisher.d.ts.map +1 -1
- package/ietf/publisher.js +219 -125
- package/ietf/publisher.js.map +1 -1
- package/ietf/solicit.d.ts +1 -40
- package/ietf/solicit.d.ts.map +1 -1
- package/ietf/subscribe.d.ts +1 -5
- package/ietf/subscribe.d.ts.map +1 -1
- package/ietf/subscribe.js +8 -9
- package/ietf/subscribe.js.map +1 -1
- package/ietf/subscriber.d.ts +1 -65
- package/ietf/subscriber.d.ts.map +1 -1
- package/ietf/subscriber.js +154 -73
- package/ietf/subscriber.js.map +1 -1
- package/ietf/version.d.ts +12 -1
- package/ietf/version.d.ts.map +1 -1
- package/ietf/version.js +13 -0
- package/ietf/version.js.map +1 -1
- package/index.d.ts +11 -6
- package/index.d.ts.map +1 -1
- package/index.js +10 -5
- package/index.js.map +1 -1
- package/internal.d.ts +99 -1
- package/internal.d.ts.map +1 -1
- package/internal.js +65 -0
- package/internal.js.map +1 -1
- package/lite/announce.d.ts +15 -9
- package/lite/announce.d.ts.map +1 -1
- package/lite/announce.js +47 -20
- package/lite/announce.js.map +1 -1
- package/lite/connection.d.ts +9 -59
- package/lite/connection.d.ts.map +1 -1
- package/lite/connection.js +20 -32
- package/lite/connection.js.map +1 -1
- package/lite/fetch.d.ts +15 -1
- package/lite/fetch.d.ts.map +1 -1
- package/lite/fetch.js +39 -7
- package/lite/fetch.js.map +1 -1
- package/lite/goaway.d.ts.map +1 -1
- package/lite/goaway.js +7 -1
- package/lite/goaway.js.map +1 -1
- package/lite/group.d.ts +18 -4
- package/lite/group.d.ts.map +1 -1
- package/lite/group.js +31 -10
- package/lite/group.js.map +1 -1
- package/lite/priority.d.ts +1 -61
- package/lite/priority.d.ts.map +1 -1
- package/lite/priority.js +4 -5
- package/lite/priority.js.map +1 -1
- package/lite/publisher.d.ts +1 -69
- package/lite/publisher.d.ts.map +1 -1
- package/lite/publisher.js +559 -232
- package/lite/publisher.js.map +1 -1
- package/lite/setup.d.ts +7 -7
- package/lite/setup.d.ts.map +1 -1
- package/lite/setup.js +13 -13
- package/lite/setup.js.map +1 -1
- package/lite/subscribe.d.ts +61 -13
- package/lite/subscribe.d.ts.map +1 -1
- package/lite/subscribe.js +186 -44
- package/lite/subscribe.js.map +1 -1
- package/lite/subscriber.d.ts +8 -57
- package/lite/subscriber.d.ts.map +1 -1
- package/lite/subscriber.js +179 -117
- package/lite/subscriber.js.map +1 -1
- package/lite/track.d.ts +4 -10
- package/lite/track.d.ts.map +1 -1
- package/lite/track.js +34 -29
- package/lite/track.js.map +1 -1
- package/lite/version.d.ts +31 -6
- package/lite/version.d.ts.map +1 -1
- package/lite/version.js +71 -11
- package/lite/version.js.map +1 -1
- package/origin.d.ts +229 -29
- package/origin.d.ts.map +1 -1
- package/origin.js +1087 -37
- package/origin.js.map +1 -1
- package/package.json +3 -2
- package/path.d.ts +25 -7
- package/path.d.ts.map +1 -1
- package/path.js +5 -3
- package/path.js.map +1 -1
- package/stream.d.ts.map +1 -1
- package/stream.js +25 -16
- package/stream.js.map +1 -1
- package/time.d.ts +15 -2
- package/time.d.ts.map +1 -1
- package/time.js +28 -9
- package/time.js.map +1 -1
- package/track.d.ts +171 -82
- package/track.d.ts.map +1 -1
- package/track.js +633 -184
- package/track.js.map +1 -1
- package/util/log.d.ts +5 -0
- package/util/log.d.ts.map +1 -0
- package/util/log.js +17 -0
- package/util/log.js.map +1 -0
- package/wire.d.ts +67 -0
- package/wire.d.ts.map +1 -0
- package/wire.js +32 -0
- package/wire.js.map +1 -0
- package/zod.d.ts +1 -1
- package/zod.d.ts.map +1 -1
- package/zod.js.map +1 -1
- package/mock.d.ts +0 -66
- package/mock.d.ts.map +0 -1
- package/mock.js +0 -243
- package/mock.js.map +0 -1
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
/* @ts-self-types="./pool.d.ts" */
|
|
2
|
+
/**
|
|
3
|
+
* A reconnecting, shareable connection: one origin and one reconnect loop per relay URL.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import { Effect, Once, Signal } from "@moq/signals";
|
|
8
|
+
import * as Announce from "../announced.js";
|
|
9
|
+
import * as Origin from "../origin.js";
|
|
10
|
+
import * as Path from "../path.js";
|
|
11
|
+
import * as Time from "../time.js";
|
|
12
|
+
import { accept } from "./accept.js";
|
|
13
|
+
import { isWebTransportSupported } from "./browser.js";
|
|
14
|
+
import { certificateHash, connect, } from "./connect.js";
|
|
15
|
+
import { Reload } from "./reload.js";
|
|
16
|
+
/** How long an unreferenced shared connection lingers before it actually closes. */
|
|
17
|
+
const LINGER_MS = Time.Milli(2000);
|
|
18
|
+
/**
|
|
19
|
+
* A cloneable handle on a MoQ connection: `new Connection({ url })` reconnects and, by
|
|
20
|
+
* default, shares one origin and one reconnect loop with every other handle on that URL.
|
|
21
|
+
*
|
|
22
|
+
* The shared {@link origin} is wired to both directions. Everything the relay announces
|
|
23
|
+
* lands in it, a publish into it is announced to the relay, and a page that publishes and
|
|
24
|
+
* watches the same path resolves it locally with no round trip.
|
|
25
|
+
*
|
|
26
|
+
* {@link close} releases this handle; the connection survives its last handle by a short
|
|
27
|
+
* linger window (see {@link ConnectionProps.linger}), so a component torn down and rebuilt
|
|
28
|
+
* reuses the warm connection instead of redialing.
|
|
29
|
+
*
|
|
30
|
+
* The loop reconnects for as long as a handle holds it, so an outage of any length recovers
|
|
31
|
+
* on its own. An auth rejection stops retrying those credentials and retires the shared
|
|
32
|
+
* connection so the next handle dials fresh; a new URL on this handle starts another
|
|
33
|
+
* sequence. {@link closed} settles only when this handle is released.
|
|
34
|
+
*
|
|
35
|
+
* Options the pool cannot honor (transport options, discovery, delay, a pinned
|
|
36
|
+
* certificate, caller-owned origins) take `share: false` and get a private loop. A
|
|
37
|
+
* supplied transport cannot reconnect at all; pass it to {@link Connection.connect}
|
|
38
|
+
* instead.
|
|
39
|
+
*
|
|
40
|
+
* @public
|
|
41
|
+
*/
|
|
42
|
+
export class Connection {
|
|
43
|
+
/** Establish a one-shot session; a supplied transport belongs here, not in the reconnect loop. */
|
|
44
|
+
static connect = connect;
|
|
45
|
+
/** Accept a one-shot session on an already-open transport. */
|
|
46
|
+
static accept = accept;
|
|
47
|
+
/** SHA-256 of a certificate, for pinning via `webtransport.serverCertificateHashes`. */
|
|
48
|
+
static certificateHash = certificateHash;
|
|
49
|
+
/** Whether this runtime can connect with WebTransport. */
|
|
50
|
+
static isWebTransportSupported = isWebTransportSupported;
|
|
51
|
+
/** Relay URL to connect to; updating it reconnects to that URL. */
|
|
52
|
+
url;
|
|
53
|
+
/** Whether to hold a connection at all; clearing it releases this handle's share. */
|
|
54
|
+
enabled;
|
|
55
|
+
/** Current status of the connection. */
|
|
56
|
+
status;
|
|
57
|
+
/**
|
|
58
|
+
* The failure that stopped retrying the current URL, or undefined while the loop is live.
|
|
59
|
+
*
|
|
60
|
+
* Set on an auth rejection or when a private retry window expires. Cleared when a new URL
|
|
61
|
+
* or a disable/re-enable starts another sequence, not on a page hide/show.
|
|
62
|
+
*/
|
|
63
|
+
error;
|
|
64
|
+
/**
|
|
65
|
+
* The wire transport the current session runs over, or undefined while disconnected.
|
|
66
|
+
*
|
|
67
|
+
* The session itself is deliberately not exposed: it is shared, so no handle may close
|
|
68
|
+
* or reconfigure it, and everything else it offers is reachable through {@link origin}.
|
|
69
|
+
*/
|
|
70
|
+
transport;
|
|
71
|
+
/** The current connection's PROBE estimates, or undefined while disconnected. */
|
|
72
|
+
probe;
|
|
73
|
+
/**
|
|
74
|
+
* The send-side bandwidth allocator for the current URL, or undefined
|
|
75
|
+
* while disabled or URL-less.
|
|
76
|
+
*
|
|
77
|
+
* Every handle on this URL shares the same instance, so publishers reserve
|
|
78
|
+
* against one registry. Borrowed, not owned: the type has no close, since
|
|
79
|
+
* closing it would starve every other handle.
|
|
80
|
+
*/
|
|
81
|
+
bandwidth;
|
|
82
|
+
/**
|
|
83
|
+
* The origin for the current URL, or undefined while disabled or URL-less.
|
|
84
|
+
*
|
|
85
|
+
* Publish into it or consume from it; a shared handle's origin is the same one every
|
|
86
|
+
* other handle on this URL uses, and it spans the connection's reconnects. Borrowed,
|
|
87
|
+
* not owned: the type has no close, since closing it would tear the origin down under
|
|
88
|
+
* every other handle.
|
|
89
|
+
*/
|
|
90
|
+
origin;
|
|
91
|
+
/**
|
|
92
|
+
* Settles once this handle is released via {@link close}: `null` on a clean close, or
|
|
93
|
+
* the abort {@link Error}. Peek it synchronously (`undefined` while open), observe it
|
|
94
|
+
* reactively, or `await` it. Attempt failures live on {@link error} instead.
|
|
95
|
+
*/
|
|
96
|
+
get closed() {
|
|
97
|
+
return this.#closed;
|
|
98
|
+
}
|
|
99
|
+
#closed = new Once();
|
|
100
|
+
#status = new Signal("disconnected");
|
|
101
|
+
#established = new Signal(undefined);
|
|
102
|
+
#probe = new Signal(undefined);
|
|
103
|
+
#origin = new Signal(undefined);
|
|
104
|
+
#bandwidth = new Signal(undefined);
|
|
105
|
+
#error = new Signal(undefined);
|
|
106
|
+
#signals = new Effect();
|
|
107
|
+
/**
|
|
108
|
+
* Take a handle on the connection for {@link ConnectionProps.url}.
|
|
109
|
+
*
|
|
110
|
+
* Dials immediately when a URL is given and `enabled` is not false; otherwise waits for
|
|
111
|
+
* the signals to say go. The handle owns nothing but its own share: {@link close}
|
|
112
|
+
* releases it, and the underlying connection and origin live for as long as any handle
|
|
113
|
+
* (plus the linger window) wants them.
|
|
114
|
+
*/
|
|
115
|
+
constructor(props) {
|
|
116
|
+
refuse(props);
|
|
117
|
+
this.url = Signal.from(props?.url);
|
|
118
|
+
this.enabled = Signal.from(props?.enabled ?? true);
|
|
119
|
+
this.status = this.#status;
|
|
120
|
+
this.error = this.#error;
|
|
121
|
+
this.probe = this.#probe;
|
|
122
|
+
this.origin = this.#origin;
|
|
123
|
+
this.bandwidth = this.#bandwidth;
|
|
124
|
+
this.transport = this.#signals.computed((effect) => effect.get(this.#established)?.transport);
|
|
125
|
+
const linger = props?.linger;
|
|
126
|
+
// Key on the serialized URL: URL objects use identity equality, and an equivalent
|
|
127
|
+
// instance must not release and redial.
|
|
128
|
+
const href = this.#signals.computed((effect) => effect.get(this.url)?.href);
|
|
129
|
+
if (props?.share === false) {
|
|
130
|
+
this.#runPrivate(props, href);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
this.#signals.run((effect) => {
|
|
134
|
+
if (!effect.get(this.enabled))
|
|
135
|
+
return;
|
|
136
|
+
const key = effect.get(href);
|
|
137
|
+
if (!key)
|
|
138
|
+
return;
|
|
139
|
+
const lease = acquire(key, linger);
|
|
140
|
+
effect.cleanup(lease.release);
|
|
141
|
+
effect.set(this.#origin, lease.origin, undefined);
|
|
142
|
+
effect.set(this.#bandwidth, lease.connection.bandwidth, undefined);
|
|
143
|
+
// Proxy the shared loop's outputs, so this handle reads like its own connection.
|
|
144
|
+
effect.run((nested) => nested.set(this.#status, nested.get(lease.connection.status), "disconnected"));
|
|
145
|
+
effect.run((nested) => nested.set(this.#established, nested.get(lease.connection.established), undefined));
|
|
146
|
+
effect.run((nested) => nested.set(this.#probe, nested.get(lease.connection.probe), undefined));
|
|
147
|
+
effect.run((nested) => nested.set(this.#error, nested.get(lease.connection.error), undefined));
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
#runPrivate(props, href) {
|
|
151
|
+
const owned = props.consume === undefined;
|
|
152
|
+
const origin = props.consume ?? new Origin.Producer();
|
|
153
|
+
if (owned)
|
|
154
|
+
this.#signals.cleanup(() => origin.close());
|
|
155
|
+
const loop = new Reload({
|
|
156
|
+
url: this.url,
|
|
157
|
+
enabled: this.enabled,
|
|
158
|
+
publish: props.publish ?? (owned ? origin.consume() : undefined),
|
|
159
|
+
consume: origin,
|
|
160
|
+
webtransport: props.webtransport,
|
|
161
|
+
websocket: props.websocket,
|
|
162
|
+
discovery: props.discovery,
|
|
163
|
+
// A handle nobody watches wants unlimited retries; an auth rejection still
|
|
164
|
+
// stops this URL, and a new one starts another sequence.
|
|
165
|
+
delay: { timeout: Time.Milli(0), ...props.delay },
|
|
166
|
+
});
|
|
167
|
+
this.#signals.cleanup(() => loop.close());
|
|
168
|
+
this.#signals.run((effect) => {
|
|
169
|
+
if (!effect.get(this.enabled) || !effect.get(href))
|
|
170
|
+
return;
|
|
171
|
+
effect.set(this.#origin, origin, undefined);
|
|
172
|
+
effect.set(this.#bandwidth, loop.bandwidth, undefined);
|
|
173
|
+
});
|
|
174
|
+
this.#signals.run((effect) => effect.set(this.#status, effect.get(loop.status), "disconnected"));
|
|
175
|
+
this.#signals.run((effect) => effect.set(this.#established, effect.get(loop.established), undefined));
|
|
176
|
+
this.#signals.run((effect) => effect.set(this.#probe, effect.get(loop.probe), undefined));
|
|
177
|
+
this.#signals.run((effect) => effect.set(this.#error, effect.get(loop.error), undefined));
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Subscribe to broadcast announcements matching `scope`, spanning reconnects
|
|
181
|
+
* and URL switches: a switch retracts everything from
|
|
182
|
+
* the old relay's origin, then the new one's arrivals stream in.
|
|
183
|
+
*/
|
|
184
|
+
announced(scope = Path.Pattern.all()) {
|
|
185
|
+
const producer = new Announce.Producer();
|
|
186
|
+
const consumer = producer.consume();
|
|
187
|
+
// Closing the consumer closes the shared state, so stop appending after that.
|
|
188
|
+
let closed = false;
|
|
189
|
+
void consumer.closed.then(() => {
|
|
190
|
+
closed = true;
|
|
191
|
+
});
|
|
192
|
+
// A child of this handle's scope rather than a standalone Effect it merely cleans up
|
|
193
|
+
// after: the disposer run() hands back also drops itself from the parent, so opening
|
|
194
|
+
// and closing announcement streams repeatedly does not pile up dead pumps that live
|
|
195
|
+
// until the whole handle closes.
|
|
196
|
+
const stop = this.#signals.run((effect) => {
|
|
197
|
+
const origin = effect.get(this.#origin);
|
|
198
|
+
if (!origin)
|
|
199
|
+
return;
|
|
200
|
+
const upstream = origin.announced(scope);
|
|
201
|
+
effect.cleanup(() => upstream.close());
|
|
202
|
+
// Track what this origin announced so a URL switch retracts it; the last
|
|
203
|
+
// event rides along for the retraction.
|
|
204
|
+
const active = new Map();
|
|
205
|
+
effect.spawn(async () => {
|
|
206
|
+
try {
|
|
207
|
+
for (;;) {
|
|
208
|
+
const entry = await Promise.race([effect.cancel, upstream.next()]);
|
|
209
|
+
if (!entry)
|
|
210
|
+
break;
|
|
211
|
+
if (Announce.isActive(entry.kind))
|
|
212
|
+
active.set(entry.prefix, entry);
|
|
213
|
+
else
|
|
214
|
+
active.delete(entry.prefix);
|
|
215
|
+
producer.append(entry);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
finally {
|
|
219
|
+
if (!closed) {
|
|
220
|
+
for (const entry of active.values()) {
|
|
221
|
+
producer.append({ ...entry, kind: "retracted" });
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
void consumer.closed.then(stop);
|
|
228
|
+
return consumer;
|
|
229
|
+
}
|
|
230
|
+
/** Snapshot the live connection's transport counters, or undefined while disconnected. */
|
|
231
|
+
async stats() {
|
|
232
|
+
return this.#established.peek()?.stats();
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Release this handle. The shared connection closes once its last handle is gone and
|
|
236
|
+
* the linger window passes; other handles on the URL are unaffected. Idempotent.
|
|
237
|
+
*/
|
|
238
|
+
close(abort) {
|
|
239
|
+
this.#signals.close();
|
|
240
|
+
if (this.#closed.peek() === undefined)
|
|
241
|
+
this.#closed.set(abort ?? null);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
/** Throw if `props` cannot be honored, rather than silently dropping them. */
|
|
245
|
+
function refuse(props) {
|
|
246
|
+
if (!props)
|
|
247
|
+
return;
|
|
248
|
+
const extra = props;
|
|
249
|
+
if (extra.transport) {
|
|
250
|
+
throw new Error("a supplied transport cannot reconnect; call Connection.connect() instead");
|
|
251
|
+
}
|
|
252
|
+
if (extra.signal) {
|
|
253
|
+
throw new Error("a Connection owns its abort signal; do not pass one");
|
|
254
|
+
}
|
|
255
|
+
if (props.share === false)
|
|
256
|
+
return;
|
|
257
|
+
if (props.publish || props.consume) {
|
|
258
|
+
throw new Error("caller-owned origins cannot be shared; pass share: false");
|
|
259
|
+
}
|
|
260
|
+
const hashes = props.webtransport?.serverCertificateHashes?.length ?? 0;
|
|
261
|
+
if (props.webtransport?.serverCertificate !== undefined || hashes > 0) {
|
|
262
|
+
throw new Error("a pinned certificate cannot be shared; pass share: false");
|
|
263
|
+
}
|
|
264
|
+
if (props.webtransport !== undefined) {
|
|
265
|
+
throw new Error("webtransport options cannot be shared; pass share: false");
|
|
266
|
+
}
|
|
267
|
+
if (props.websocket !== undefined) {
|
|
268
|
+
throw new Error("websocket options cannot be shared; pass share: false");
|
|
269
|
+
}
|
|
270
|
+
if (props.discovery !== undefined) {
|
|
271
|
+
throw new Error("discovery cannot be shared; pass share: false");
|
|
272
|
+
}
|
|
273
|
+
if (props.delay !== undefined) {
|
|
274
|
+
throw new Error("delay cannot be shared; pass share: false");
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
/** The process-wide pool backing {@link Connection}. */
|
|
278
|
+
const pool = new Map();
|
|
279
|
+
/** Take a reference on the shared entry for `key`, creating it on first use. */
|
|
280
|
+
function acquire(key, linger) {
|
|
281
|
+
let entry = pool.get(key);
|
|
282
|
+
if (!entry) {
|
|
283
|
+
const origin = new Origin.Producer();
|
|
284
|
+
const connection = new Reload({
|
|
285
|
+
url: new URL(key),
|
|
286
|
+
enabled: true,
|
|
287
|
+
publish: origin.consume(),
|
|
288
|
+
consume: origin,
|
|
289
|
+
// Nobody observes a shared loop's `closed`, so giving up would strand every handle
|
|
290
|
+
// on this URL offline until the page reloads. Retry for as long as the entry lives
|
|
291
|
+
// instead; an auth rejection still stops this URL, and evicts below.
|
|
292
|
+
delay: { timeout: Time.Milli(0) },
|
|
293
|
+
});
|
|
294
|
+
const created = { origin, connection, refs: 0, linger: linger ?? LINGER_MS };
|
|
295
|
+
// The loop only stops on a peer saying these credentials will never work. Drop the
|
|
296
|
+
// entry so a later handle dials fresh rather than joining a loop that has stopped;
|
|
297
|
+
// handles already on it keep it until they release, since a redial would be refused
|
|
298
|
+
// the same way.
|
|
299
|
+
connection.error.subscribe((err) => {
|
|
300
|
+
if (err !== undefined && pool.get(key) === created)
|
|
301
|
+
pool.delete(key);
|
|
302
|
+
});
|
|
303
|
+
entry = created;
|
|
304
|
+
pool.set(key, created);
|
|
305
|
+
}
|
|
306
|
+
const taken = entry;
|
|
307
|
+
taken.linger = Time.Milli(Math.max(taken.linger, linger ?? LINGER_MS));
|
|
308
|
+
taken.refs += 1;
|
|
309
|
+
if (taken.timer !== undefined) {
|
|
310
|
+
clearTimeout(taken.timer);
|
|
311
|
+
taken.timer = undefined;
|
|
312
|
+
}
|
|
313
|
+
let released = false;
|
|
314
|
+
return {
|
|
315
|
+
...taken,
|
|
316
|
+
release: () => {
|
|
317
|
+
if (released)
|
|
318
|
+
return;
|
|
319
|
+
released = true;
|
|
320
|
+
taken.refs -= 1;
|
|
321
|
+
if (taken.refs > 0)
|
|
322
|
+
return;
|
|
323
|
+
taken.timer = setTimeout(() => {
|
|
324
|
+
if (pool.get(key) === taken)
|
|
325
|
+
pool.delete(key);
|
|
326
|
+
taken.connection.close();
|
|
327
|
+
taken.origin.close();
|
|
328
|
+
}, taken.linger);
|
|
329
|
+
// Don't hold a Node process open for a connection nobody is using.
|
|
330
|
+
taken.timer.unref?.();
|
|
331
|
+
},
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Close every shared connection immediately, so the next handle dials fresh.
|
|
336
|
+
*
|
|
337
|
+
* Exists for tests, which otherwise share connections across cases.
|
|
338
|
+
*
|
|
339
|
+
* @internal
|
|
340
|
+
*/
|
|
341
|
+
export function resetShared() {
|
|
342
|
+
const entries = [...pool.values()];
|
|
343
|
+
pool.clear();
|
|
344
|
+
for (const entry of entries) {
|
|
345
|
+
if (entry.timer !== undefined)
|
|
346
|
+
clearTimeout(entry.timer);
|
|
347
|
+
entry.connection.close();
|
|
348
|
+
entry.origin.close();
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
//# sourceMappingURL=pool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pool.js","sourceRoot":"","sources":["../../src/connection/pool.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,MAAM,EAAgC,IAAI,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAClF,OAAO,KAAK,QAAQ,MAAM,iBAAiB,CAAC;AAE5C,OAAO,KAAK,MAAM,MAAM,cAAc,CAAC;AACvC,OAAO,KAAK,IAAI,MAAM,YAAY,CAAC;AACnC,OAAO,KAAK,IAAI,MAAM,YAAY,CAAC;AACnC,OAAO,EAAuC,MAAM,EAAE,MAAM,aAAa,CAAC;AAC1E,OAAO,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAC;AACvD,OAAO,EAGN,eAAe,EACf,OAAO,GAGP,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,MAAM,EAAuC,MAAM,aAAa,CAAC;AAI1E,oFAAoF;AACpF,MAAM,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;AAqDnC;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,OAAO,UAAU;IACtB,kGAAkG;IAClG,MAAM,CAAU,OAAO,GAAG,OAAO,CAAC;IAElC,8DAA8D;IAC9D,MAAM,CAAU,MAAM,GAAG,MAAM,CAAC;IAEhC,wFAAwF;IACxF,MAAM,CAAU,eAAe,GAAG,eAAe,CAAC;IAElD,0DAA0D;IAC1D,MAAM,CAAU,uBAAuB,GAAG,uBAAuB,CAAC;IAElE,mEAAmE;IACnE,GAAG,CAA0B;IAE7B,qFAAqF;IACrF,OAAO,CAAkB;IAEzB,wCAAwC;IAC/B,MAAM,CAAuB;IAEtC;;;;;OAKG;IACM,KAAK,CAA4B;IAE1C;;;;;OAKG;IACM,SAAS,CAAoC;IAEtD,iFAAiF;IACxE,KAAK,CAAgC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U,CAAC,KAAK,EAAE,CAAC;QACzB,KAAK,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;IACtB,CAAC;AACF,CAAC","sourcesContent":["/**\n * A reconnecting, shareable connection: one origin and one reconnect loop per relay URL.\n *\n * @module\n */\nimport { Effect, type GetPromise, type Getter, Once, Signal } from \"@moq/signals\";\nimport * as Announce from \"../announced.ts\";\nimport type { Handle } from \"../bandwidth.ts\";\nimport * as Origin from \"../origin.ts\";\nimport * as Path from \"../path.ts\";\nimport * as Time from \"../time.ts\";\nimport { type AcceptProps as AcceptPropsType, accept } from \"./accept.ts\";\nimport { isWebTransportSupported } from \"./browser.ts\";\nimport {\n\ttype CertificateHash as CertificateHashType,\n\ttype ConnectProps as ConnectPropsType,\n\tcertificateHash,\n\tconnect,\n\ttype WebSocketProps as WebSocketPropsType,\n\ttype WebTransportProps as WebTransportPropsType,\n} from \"./connect.ts\";\nimport type { Established as EstablishedType } from \"./established.ts\";\nimport { Reload, type ReloadDelay, type ReloadStatus } from \"./reload.ts\";\nimport type { Probe as ProbeType, Stats as StatsType } from \"./stats.ts\";\nimport type { Transport as TransportType } from \"./transport.ts\";\n\n/** How long an unreferenced shared connection lingers before it actually closes. */\nconst LINGER_MS = Time.Milli(2000);\n\n/** Options for {@link Connection}. */\nexport interface ConnectionProps {\n\t/** The relay to connect to; pass a `Signal` to switch relays live. */\n\turl?: URL | Signal<URL | undefined>;\n\n\t/** Whether to connect at all (default: true); pass a `Signal` to toggle live. */\n\tenabled?: boolean | Signal<boolean>;\n\n\t/**\n\t * How long the underlying connection outlives its last handle (default: 2000ms).\n\t *\n\t * The window is what makes moving an element around the DOM free: the connection and\n\t * everything it discovered are still warm when the new owner asks for them. The longest\n\t * linger requested by any handle sharing the connection wins.\n\t */\n\tlinger?: Time.Milli;\n\n\t/**\n\t * Share a pooled transport keyed on the URL (default: true).\n\t *\n\t * Options the pool cannot honor (transport options, discovery, delay, a pinned\n\t * certificate, caller-owned origins) require `share: false` and get a private\n\t * reconnect loop with the same handle semantics.\n\t */\n\tshare?: boolean;\n\n\t/** WebTransport options applied to each connection attempt (not reactive). */\n\twebtransport?: WebTransportPropsType;\n\n\t/** WebSocket fallback options applied to each connection attempt (not reactive). */\n\twebsocket?: WebSocketPropsType;\n\n\t/** Whether the relay supports broadcast discovery. */\n\tdiscovery?: boolean;\n\n\t/** The origin whose broadcasts are served, spanning reconnects. Requires {@link ConnectionProps.share} `false`. */\n\tpublish?: Origin.Consumer;\n\n\t/** The origin fed with the peer's announced broadcasts, spanning reconnects. Requires {@link ConnectionProps.share} `false`. */\n\tconsume?: Origin.Producer;\n\n\t/** Backoff settings for the reconnect loop; an unset field uses its default. */\n\tdelay?: ReloadDelay;\n\n\t/** A Connection owns the abort signal for each connection attempt. */\n\tsignal?: never;\n\n\t/** A one-shot transport cannot be reused by the reconnect loop; call {@link Connection.connect} instead. */\n\ttransport?: never;\n}\n\n/**\n * A cloneable handle on a MoQ connection: `new Connection({ url })` reconnects and, by\n * default, shares one origin and one reconnect loop with every other handle on that URL.\n *\n * The shared {@link origin} is wired to both directions. Everything the relay announces\n * lands in it, a publish into it is announced to the relay, and a page that publishes and\n * watches the same path resolves it locally with no round trip.\n *\n * {@link close} releases this handle; the connection survives its last handle by a short\n * linger window (see {@link ConnectionProps.linger}), so a component torn down and rebuilt\n * reuses the warm connection instead of redialing.\n *\n * The loop reconnects for as long as a handle holds it, so an outage of any length recovers\n * on its own. An auth rejection stops retrying those credentials and retires the shared\n * connection so the next handle dials fresh; a new URL on this handle starts another\n * sequence. {@link closed} settles only when this handle is released.\n *\n * Options the pool cannot honor (transport options, discovery, delay, a pinned\n * certificate, caller-owned origins) take `share: false` and get a private loop. A\n * supplied transport cannot reconnect at all; pass it to {@link Connection.connect}\n * instead.\n *\n * @public\n */\nexport class Connection {\n\t/** Establish a one-shot session; a supplied transport belongs here, not in the reconnect loop. */\n\tstatic readonly connect = connect;\n\n\t/** Accept a one-shot session on an already-open transport. */\n\tstatic readonly accept = accept;\n\n\t/** SHA-256 of a certificate, for pinning via `webtransport.serverCertificateHashes`. */\n\tstatic readonly certificateHash = certificateHash;\n\n\t/** Whether this runtime can connect with WebTransport. */\n\tstatic readonly isWebTransportSupported = isWebTransportSupported;\n\n\t/** Relay URL to connect to; updating it reconnects to that URL. */\n\turl: Signal<URL | undefined>;\n\n\t/** Whether to hold a connection at all; clearing it releases this handle's share. */\n\tenabled: Signal<boolean>;\n\n\t/** Current status of the connection. */\n\treadonly status: Getter<ReloadStatus>;\n\n\t/**\n\t * The failure that stopped retrying the current URL, or undefined while the loop is live.\n\t *\n\t * Set on an auth rejection or when a private retry window expires. Cleared when a new URL\n\t * or a disable/re-enable starts another sequence, not on a page hide/show.\n\t */\n\treadonly error: Getter<Error | undefined>;\n\n\t/**\n\t * The wire transport the current session runs over, or undefined while disconnected.\n\t *\n\t * The session itself is deliberately not exposed: it is shared, so no handle may close\n\t * or reconfigure it, and everything else it offers is reachable through {@link origin}.\n\t */\n\treadonly transport: Getter<TransportType | undefined>;\n\n\t/** The current connection's PROBE estimates, or undefined while disconnected. */\n\treadonly probe: Getter<ProbeType | undefined>;\n\n\t/**\n\t * The send-side bandwidth allocator for the current URL, or undefined\n\t * while disabled or URL-less.\n\t *\n\t * Every handle on this URL shares the same instance, so publishers reserve\n\t * against one registry. Borrowed, not owned: the type has no close, since\n\t * closing it would starve every other handle.\n\t */\n\treadonly bandwidth: Getter<Handle | undefined>;\n\n\t/**\n\t * The origin for the current URL, or undefined while disabled or URL-less.\n\t *\n\t * Publish into it or consume from it; a shared handle's origin is the same one every\n\t * other handle on this URL uses, and it spans the connection's reconnects. Borrowed,\n\t * not owned: the type has no close, since closing it would tear the origin down under\n\t * every other handle.\n\t */\n\treadonly origin: Getter<Origin.Table | undefined>;\n\n\t/**\n\t * Settles once this handle is released via {@link close}: `null` on a clean close, or\n\t * the abort {@link Error}. Peek it synchronously (`undefined` while open), observe it\n\t * reactively, or `await` it. Attempt failures live on {@link error} instead.\n\t */\n\tget closed(): GetPromise<Error | null> {\n\t\treturn this.#closed;\n\t}\n\n\t#closed = new Once<Error | null>();\n\treadonly #status = new Signal<ReloadStatus>(\"disconnected\");\n\treadonly #established = new Signal<EstablishedType | undefined>(undefined);\n\treadonly #probe = new Signal<ProbeType | undefined>(undefined);\n\treadonly #origin = new Signal<Origin.Producer | undefined>(undefined);\n\treadonly #bandwidth = new Signal<Handle | undefined>(undefined);\n\treadonly #error = new Signal<Error | undefined>(undefined);\n\t#signals = new Effect();\n\n\t/**\n\t * Take a handle on the connection for {@link ConnectionProps.url}.\n\t *\n\t * Dials immediately when a URL is given and `enabled` is not false; otherwise waits for\n\t * the signals to say go. The handle owns nothing but its own share: {@link close}\n\t * releases it, and the underlying connection and origin live for as long as any handle\n\t * (plus the linger window) wants them.\n\t */\n\tconstructor(props?: ConnectionProps) {\n\t\trefuse(props);\n\n\t\tthis.url = Signal.from(props?.url);\n\t\tthis.enabled = Signal.from(props?.enabled ?? true);\n\t\tthis.status = this.#status;\n\t\tthis.error = this.#error;\n\t\tthis.probe = this.#probe;\n\t\tthis.origin = this.#origin;\n\t\tthis.bandwidth = this.#bandwidth;\n\t\tthis.transport = this.#signals.computed((effect) => effect.get(this.#established)?.transport);\n\n\t\tconst linger = props?.linger;\n\n\t\t// Key on the serialized URL: URL objects use identity equality, and an equivalent\n\t\t// instance must not release and redial.\n\t\tconst href = this.#signals.computed((effect) => effect.get(this.url)?.href);\n\n\t\tif (props?.share === false) {\n\t\t\tthis.#runPrivate(props, href);\n\t\t\treturn;\n\t\t}\n\n\t\tthis.#signals.run((effect) => {\n\t\t\tif (!effect.get(this.enabled)) return;\n\t\t\tconst key = effect.get(href);\n\t\t\tif (!key) return;\n\n\t\t\tconst lease = acquire(key, linger);\n\t\t\teffect.cleanup(lease.release);\n\n\t\t\teffect.set(this.#origin, lease.origin, undefined);\n\t\t\teffect.set(this.#bandwidth, lease.connection.bandwidth, undefined);\n\n\t\t\t// Proxy the shared loop's outputs, so this handle reads like its own connection.\n\t\t\teffect.run((nested) => nested.set(this.#status, nested.get(lease.connection.status), \"disconnected\"));\n\t\t\teffect.run((nested) => nested.set(this.#established, nested.get(lease.connection.established), undefined));\n\t\t\teffect.run((nested) => nested.set(this.#probe, nested.get(lease.connection.probe), undefined));\n\t\t\teffect.run((nested) => nested.set(this.#error, nested.get(lease.connection.error), undefined));\n\t\t});\n\t}\n\n\t#runPrivate(props: ConnectionProps, href: Getter<string | undefined>): void {\n\t\tconst owned = props.consume === undefined;\n\t\tconst origin = props.consume ?? new Origin.Producer();\n\t\tif (owned) this.#signals.cleanup(() => origin.close());\n\n\t\tconst loop = new Reload({\n\t\t\turl: this.url,\n\t\t\tenabled: this.enabled,\n\t\t\tpublish: props.publish ?? (owned ? origin.consume() : undefined),\n\t\t\tconsume: origin,\n\t\t\twebtransport: props.webtransport,\n\t\t\twebsocket: props.websocket,\n\t\t\tdiscovery: props.discovery,\n\t\t\t// A handle nobody watches wants unlimited retries; an auth rejection still\n\t\t\t// stops this URL, and a new one starts another sequence.\n\t\t\tdelay: { timeout: Time.Milli(0), ...props.delay },\n\t\t});\n\t\tthis.#signals.cleanup(() => loop.close());\n\n\t\tthis.#signals.run((effect) => {\n\t\t\tif (!effect.get(this.enabled) || !effect.get(href)) return;\n\t\t\teffect.set(this.#origin, origin, undefined);\n\t\t\teffect.set(this.#bandwidth, loop.bandwidth, undefined);\n\t\t});\n\t\tthis.#signals.run((effect) => effect.set(this.#status, effect.get(loop.status), \"disconnected\"));\n\t\tthis.#signals.run((effect) => effect.set(this.#established, effect.get(loop.established), undefined));\n\t\tthis.#signals.run((effect) => effect.set(this.#probe, effect.get(loop.probe), undefined));\n\t\tthis.#signals.run((effect) => effect.set(this.#error, effect.get(loop.error), undefined));\n\t}\n\n\t/**\n\t * Subscribe to broadcast announcements matching `scope`, spanning reconnects\n\t * and URL switches: a switch retracts everything from\n\t * the old relay's origin, then the new one's arrivals stream in.\n\t */\n\tannounced(scope: Path.Pattern = Path.Pattern.all()): Announce.Consumer {\n\t\tconst producer = new Announce.Producer();\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\t// A child of this handle's scope rather than a standalone Effect it merely cleans up\n\t\t// after: the disposer run() hands back also drops itself from the parent, so opening\n\t\t// and closing announcement streams repeatedly does not pile up dead pumps that live\n\t\t// until the whole handle closes.\n\t\tconst stop = this.#signals.run((effect) => {\n\t\t\tconst origin = effect.get(this.#origin);\n\t\t\tif (!origin) return;\n\n\t\t\tconst upstream = origin.announced(scope);\n\t\t\teffect.cleanup(() => upstream.close());\n\n\t\t\t// Track what this origin announced so a URL switch retracts it; the last\n\t\t\t// event rides along for the retraction.\n\t\t\tconst active = new Map<Path.Valid, Announce.Update>();\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 (Announce.isActive(entry.kind)) active.set(entry.prefix, entry);\n\t\t\t\t\t\telse active.delete(entry.prefix);\n\t\t\t\t\t\tproducer.append(entry);\n\t\t\t\t\t}\n\t\t\t\t} finally {\n\t\t\t\t\tif (!closed) {\n\t\t\t\t\t\tfor (const entry of active.values()) {\n\t\t\t\t\t\t\tproducer.append({ ...entry, kind: \"retracted\" });\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\tvoid consumer.closed.then(stop);\n\n\t\treturn consumer;\n\t}\n\n\t/** Snapshot the live connection's transport counters, or undefined while disconnected. */\n\tasync stats(): Promise<StatsType | undefined> {\n\t\treturn this.#established.peek()?.stats();\n\t}\n\n\t/**\n\t * Release this handle. The shared connection closes once its last handle is gone and\n\t * the linger window passes; other handles on the URL are unaffected. Idempotent.\n\t */\n\tclose(abort?: Error): void {\n\t\tthis.#signals.close();\n\t\tif (this.#closed.peek() === undefined) this.#closed.set(abort ?? null);\n\t}\n}\n\n/** Types on {@link Connection}: the handle is the class, these are its associated types. */\nexport namespace Connection {\n\t/** Options for {@link Connection}. */\n\texport type Props = ConnectionProps;\n\t/** Options for {@link Connection.connect}. */\n\texport type ConnectProps = ConnectPropsType;\n\t/** Options for {@link Connection.accept}. */\n\texport type AcceptProps = AcceptPropsType;\n\t/** Backoff settings for a private reconnect loop. */\n\texport type Backoff = ReloadDelay;\n\t/** Current state of a {@link Connection}. */\n\texport type Status = ReloadStatus;\n\t/** The current connection's PROBE estimates. */\n\texport type Probe = ProbeType;\n\t/** A point-in-time snapshot of the transport's counters. */\n\texport type Stats = StatsType;\n\t/** The wire transport a session runs over. */\n\texport type Transport = TransportType;\n\t/** Tuning for the WebSocket fallback. */\n\texport type WebSocketProps = WebSocketPropsType;\n\t/** WebTransport options, including friendlier certificate pinning. */\n\texport type WebTransportProps = WebTransportPropsType;\n\t/** A server certificate hash used to pin a self-signed server. */\n\texport type CertificateHash = CertificateHashType;\n\t/** An established one-shot session. */\n\texport type Established = EstablishedType;\n}\n\n/** Throw if `props` cannot be honored, rather than silently dropping them. */\nfunction refuse(props?: ConnectionProps): void {\n\tif (!props) return;\n\n\tconst extra = props as ConnectionProps & { transport?: unknown; signal?: unknown };\n\tif (extra.transport) {\n\t\tthrow new Error(\"a supplied transport cannot reconnect; call Connection.connect() instead\");\n\t}\n\tif (extra.signal) {\n\t\tthrow new Error(\"a Connection owns its abort signal; do not pass one\");\n\t}\n\tif (props.share === false) return;\n\n\tif (props.publish || props.consume) {\n\t\tthrow new Error(\"caller-owned origins cannot be shared; pass share: false\");\n\t}\n\tconst hashes = props.webtransport?.serverCertificateHashes?.length ?? 0;\n\tif (props.webtransport?.serverCertificate !== undefined || hashes > 0) {\n\t\tthrow new Error(\"a pinned certificate cannot be shared; pass share: false\");\n\t}\n\tif (props.webtransport !== undefined) {\n\t\tthrow new Error(\"webtransport options cannot be shared; pass share: false\");\n\t}\n\tif (props.websocket !== undefined) {\n\t\tthrow new Error(\"websocket options cannot be shared; pass share: false\");\n\t}\n\tif (props.discovery !== undefined) {\n\t\tthrow new Error(\"discovery cannot be shared; pass share: false\");\n\t}\n\tif (props.delay !== undefined) {\n\t\tthrow new Error(\"delay cannot be shared; pass share: false\");\n\t}\n}\n\n/** One shared connection and the handles keeping it alive. */\ninterface Entry {\n\torigin: Origin.Producer;\n\tconnection: Reload;\n\trefs: number;\n\tlinger: Time.Milli;\n\ttimer?: ReturnType<typeof setTimeout>;\n}\n\n/** The process-wide pool backing {@link Connection}. */\nconst pool = new Map<string, Entry>();\n\n/** Take a reference on the shared entry for `key`, creating it on first use. */\nfunction acquire(key: string, linger?: Time.Milli): Entry & { release: () => void } {\n\tlet entry = pool.get(key);\n\tif (!entry) {\n\t\tconst origin = new Origin.Producer();\n\t\tconst connection = new Reload({\n\t\t\turl: new URL(key),\n\t\t\tenabled: true,\n\t\t\tpublish: origin.consume(),\n\t\t\tconsume: origin,\n\t\t\t// Nobody observes a shared loop's `closed`, so giving up would strand every handle\n\t\t\t// on this URL offline until the page reloads. Retry for as long as the entry lives\n\t\t\t// instead; an auth rejection still stops this URL, and evicts below.\n\t\t\tdelay: { timeout: Time.Milli(0) },\n\t\t});\n\n\t\tconst created: Entry = { origin, connection, refs: 0, linger: linger ?? LINGER_MS };\n\n\t\t// The loop only stops on a peer saying these credentials will never work. Drop the\n\t\t// entry so a later handle dials fresh rather than joining a loop that has stopped;\n\t\t// handles already on it keep it until they release, since a redial would be refused\n\t\t// the same way.\n\t\tconnection.error.subscribe((err) => {\n\t\t\tif (err !== undefined && pool.get(key) === created) pool.delete(key);\n\t\t});\n\n\t\tentry = created;\n\t\tpool.set(key, created);\n\t}\n\n\tconst taken = entry;\n\ttaken.linger = Time.Milli(Math.max(taken.linger, linger ?? LINGER_MS));\n\ttaken.refs += 1;\n\tif (taken.timer !== undefined) {\n\t\tclearTimeout(taken.timer);\n\t\ttaken.timer = undefined;\n\t}\n\n\tlet released = false;\n\treturn {\n\t\t...taken,\n\t\trelease: () => {\n\t\t\tif (released) return;\n\t\t\treleased = true;\n\n\t\t\ttaken.refs -= 1;\n\t\t\tif (taken.refs > 0) return;\n\n\t\t\ttaken.timer = setTimeout(() => {\n\t\t\t\tif (pool.get(key) === taken) pool.delete(key);\n\t\t\t\ttaken.connection.close();\n\t\t\t\ttaken.origin.close();\n\t\t\t}, taken.linger);\n\n\t\t\t// Don't hold a Node process open for a connection nobody is using.\n\t\t\t(taken.timer as { unref?: () => void }).unref?.();\n\t\t},\n\t};\n}\n\n/**\n * Close every shared connection immediately, so the next handle dials fresh.\n *\n * Exists for tests, which otherwise share connections across cases.\n *\n * @internal\n */\nexport function resetShared(): void {\n\tconst entries = [...pool.values()];\n\tpool.clear();\n\tfor (const entry of entries) {\n\t\tif (entry.timer !== undefined) clearTimeout(entry.timer);\n\t\tentry.connection.close();\n\t\tentry.origin.close();\n\t}\n}\n"]}
|
package/connection/reload.d.ts
CHANGED
|
@@ -1,104 +1,25 @@
|
|
|
1
|
-
import
|
|
2
|
-
import * as Announce from "../announced";
|
|
3
|
-
import type * as Path from "../path";
|
|
4
|
-
import { type ConnectProps, type WebSocketOptions, type WebTransportProps } from "./connect";
|
|
5
|
-
import type { Established } from "./established";
|
|
6
|
-
import type { Probe, Stats } from "./stats";
|
|
1
|
+
import * as Time from "../time";
|
|
7
2
|
/**
|
|
8
|
-
* Exponential backoff settings for
|
|
3
|
+
* Exponential backoff settings for the reconnect loop.
|
|
9
4
|
*
|
|
10
5
|
* The delays carry jitter, so a fleet of tabs knocked offline together doesn't reconnect in
|
|
11
|
-
* lockstep. Every failure is retried; {@link ReloadDelay.timeout} is what stops the
|
|
6
|
+
* lockstep. Every failure is retried; {@link ReloadDelay.timeout} is what stops the current
|
|
7
|
+
* URL. A new URL or a disable/re-enable starts another sequence.
|
|
12
8
|
*/
|
|
13
9
|
export type ReloadDelay = {
|
|
14
|
-
/** The delay
|
|
15
|
-
initial
|
|
10
|
+
/** The delay before reconnecting (default: 1000ms). */
|
|
11
|
+
initial?: Time.Milli;
|
|
16
12
|
/** The multiplier for the delay (default: 2). */
|
|
17
|
-
multiplier
|
|
18
|
-
/** The maximum delay
|
|
19
|
-
max
|
|
13
|
+
multiplier?: number;
|
|
14
|
+
/** The maximum delay (default: 5000ms). */
|
|
15
|
+
max?: Time.Milli;
|
|
20
16
|
/**
|
|
21
|
-
* Maximum total time
|
|
22
|
-
*
|
|
17
|
+
* Maximum total time to spend retrying the current URL before giving up
|
|
18
|
+
* (default: 10000ms). Resets after each successful connection, a URL change, or a
|
|
19
|
+
* disable/re-enable. Set to 0 for unlimited retries.
|
|
23
20
|
*/
|
|
24
|
-
timeout?:
|
|
21
|
+
timeout?: Time.Milli;
|
|
25
22
|
};
|
|
26
|
-
/**
|
|
27
|
-
export type ReloadProps = Omit<ConnectProps, "signal"> & {
|
|
28
|
-
/** Whether to reload the connection when it disconnects (default: true). */
|
|
29
|
-
enabled?: boolean | Signal<boolean>;
|
|
30
|
-
/** The URL of the relay server. */
|
|
31
|
-
url?: URL | Signal<URL | undefined>;
|
|
32
|
-
/** Backoff settings for the reconnect loop. */
|
|
33
|
-
delay?: ReloadDelay;
|
|
34
|
-
};
|
|
35
|
-
/** Current state of a {@link Reload} connection. */
|
|
23
|
+
/** Current state of a reconnecting connection. */
|
|
36
24
|
export type ReloadStatus = "connecting" | "connected" | "disconnected";
|
|
37
|
-
/** Maintains a MoQ connection, reconnecting with exponential backoff when it drops. */
|
|
38
|
-
export declare class Reload {
|
|
39
|
-
#private;
|
|
40
|
-
/** Relay URL to connect to; updating it triggers a reconnect. */
|
|
41
|
-
url: Signal<URL | undefined>;
|
|
42
|
-
/** Whether reconnecting is active. */
|
|
43
|
-
enabled: Signal<boolean>;
|
|
44
|
-
/** Current connection status. */
|
|
45
|
-
status: Signal<ReloadStatus>;
|
|
46
|
-
/** The currently established session, or undefined while disconnected. */
|
|
47
|
-
established: Signal<Established | undefined>;
|
|
48
|
-
/**
|
|
49
|
-
* The current connection's PROBE estimates, spanning reconnects.
|
|
50
|
-
*
|
|
51
|
-
* Undefined while disconnected: the estimates belong to a single connection.
|
|
52
|
-
* See {@link Established.probe}.
|
|
53
|
-
*/
|
|
54
|
-
readonly probe: Getter<Probe | undefined>;
|
|
55
|
-
/** WebTransport options applied to each connection attempt (not reactive). */
|
|
56
|
-
webtransport?: WebTransportProps;
|
|
57
|
-
/** WebSocket fallback options applied to each connection attempt (not reactive). */
|
|
58
|
-
websocket: WebSocketOptions | undefined;
|
|
59
|
-
/**
|
|
60
|
-
* Whether the relay supports broadcast discovery, applied to each connection attempt (not
|
|
61
|
-
* reactive). Undefined defers to the default for the URL. See {@link Established.discovery}.
|
|
62
|
-
*/
|
|
63
|
-
discovery?: boolean;
|
|
64
|
-
/** Backoff settings for the reconnect loop. */
|
|
65
|
-
delay: ReloadDelay;
|
|
66
|
-
/**
|
|
67
|
-
* Resolves when the reconnect loop stops via {@link Reload.close}.
|
|
68
|
-
*
|
|
69
|
-
* Rejects when the loop gives up instead, carrying the failure that was in flight when the
|
|
70
|
-
* retry window expired.
|
|
71
|
-
*/
|
|
72
|
-
closed: Promise<void>;
|
|
73
|
-
constructor(props?: ReloadProps);
|
|
74
|
-
/**
|
|
75
|
-
* Subscribe to broadcast announcements under an optional prefix, spanning reconnects.
|
|
76
|
-
*
|
|
77
|
-
* The same {@link Announce.Consumer} stream as {@link Established.announced}, but everything active
|
|
78
|
-
* is retracted (an `active: false` update) whenever the connection drops and re-announced on
|
|
79
|
-
* reconnect, so a consumer draining `next()` never clings to a dead route across a reconnect.
|
|
80
|
-
*
|
|
81
|
-
* Stays empty while the relay lacks {@link Established.discovery}.
|
|
82
|
-
*/
|
|
83
|
-
announced(prefix?: Path.Valid): Announce.Consumer;
|
|
84
|
-
/**
|
|
85
|
-
* A reactive handle to one broadcast, spanning reconnects.
|
|
86
|
-
*
|
|
87
|
-
* The same {@link Announce.Broadcast} as {@link Established.announcedBroadcast}, but it
|
|
88
|
-
* follows the reconnect loop: the broadcast drops to `undefined` when the connection dies
|
|
89
|
-
* and resolves again once the new connection announces the path. Use it instead of
|
|
90
|
-
* consuming off {@link Reload.established} whenever the broadcast may come online after you
|
|
91
|
-
* do, which is exactly the case a blind `consume` loses.
|
|
92
|
-
*
|
|
93
|
-
* Close the handle when done; {@link Reload.close} only drops it to `undefined`.
|
|
94
|
-
*/
|
|
95
|
-
announcedBroadcast(path: Path.Valid): Announce.Broadcast;
|
|
96
|
-
/**
|
|
97
|
-
* Snapshot the live connection's transport counters, or undefined while disconnected.
|
|
98
|
-
* See {@link Established.stats}.
|
|
99
|
-
*/
|
|
100
|
-
stats(): Promise<Stats | undefined>;
|
|
101
|
-
/** Stop reconnecting, close the current connection, and resolve {@link Reload.closed}. */
|
|
102
|
-
close(): void;
|
|
103
|
-
}
|
|
104
25
|
//# sourceMappingURL=reload.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"reload.d.ts","sourceRoot":"","sources":["../../src/connection/reload.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"reload.d.ts","sourceRoot":"","sources":["../../src/connection/reload.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,IAAI,MAAM,YAAY,CAAC;AAMnC;;;;;;GAMG;AACH,MAAM,MAAM,WAAW,GAAG;IACzB,uDAAuD;IACvD,OAAO,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC;IAErB,iDAAiD;IACjD,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB,2CAA2C;IAC3C,GAAG,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC;IAEjB;;;;OAIG;IACH,OAAO,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC;CACrB,CAAC;AA+CF,kDAAkD;AAClD,MAAM,MAAM,YAAY,GAAG,YAAY,GAAG,WAAW,GAAG,cAAc,CAAC"}
|