@moq/net 0.3.5 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (244) hide show
  1. package/announce.d.ts +7 -0
  2. package/announce.d.ts.map +1 -0
  3. package/announce.js +8 -0
  4. package/announce.js.map +1 -0
  5. package/announced.d.ts +49 -91
  6. package/announced.d.ts.map +1 -1
  7. package/announced.js +21 -156
  8. package/announced.js.map +1 -1
  9. package/bandwidth.d.ts +163 -0
  10. package/bandwidth.d.ts.map +1 -0
  11. package/bandwidth.js +304 -0
  12. package/bandwidth.js.map +1 -0
  13. package/bandwidth_api.d.ts +7 -0
  14. package/bandwidth_api.d.ts.map +1 -0
  15. package/bandwidth_api.js +8 -0
  16. package/bandwidth_api.js.map +1 -0
  17. package/broadcast.d.ts +23 -28
  18. package/broadcast.d.ts.map +1 -1
  19. package/broadcast.js +61 -40
  20. package/broadcast.js.map +1 -1
  21. package/connection/accept.d.ts +16 -1
  22. package/connection/accept.d.ts.map +1 -1
  23. package/connection/accept.js +48 -25
  24. package/connection/accept.js.map +1 -1
  25. package/connection/browser.d.ts.map +1 -1
  26. package/connection/browser.js +9 -7
  27. package/connection/browser.js.map +1 -1
  28. package/connection/connect.d.ts +30 -6
  29. package/connection/connect.d.ts.map +1 -1
  30. package/connection/connect.js +95 -46
  31. package/connection/connect.js.map +1 -1
  32. package/connection/established.d.ts +17 -21
  33. package/connection/established.d.ts.map +1 -1
  34. package/connection/established.js.map +1 -1
  35. package/connection/forward.d.ts +2 -0
  36. package/connection/forward.d.ts.map +1 -0
  37. package/connection/forward.js +166 -0
  38. package/connection/forward.js.map +1 -0
  39. package/connection/handshake.d.ts +1 -0
  40. package/connection/handshake.d.ts.map +1 -1
  41. package/connection/handshake.js +5 -2
  42. package/connection/handshake.js.map +1 -1
  43. package/connection/index.d.ts +5 -5
  44. package/connection/index.d.ts.map +1 -1
  45. package/connection/index.js +4 -5
  46. package/connection/index.js.map +1 -1
  47. package/connection/pool.d.ts +186 -0
  48. package/connection/pool.d.ts.map +1 -0
  49. package/connection/pool.js +361 -0
  50. package/connection/pool.js.map +1 -0
  51. package/connection/reload.d.ts +14 -93
  52. package/connection/reload.d.ts.map +1 -1
  53. package/connection/reload.js +217 -81
  54. package/connection/reload.js.map +1 -1
  55. package/connection/stats.d.ts +3 -26
  56. package/connection/stats.d.ts.map +1 -1
  57. package/connection/stats.js.map +1 -1
  58. package/connection/transport.d.ts +0 -7
  59. package/connection/transport.d.ts.map +1 -1
  60. package/consume.d.ts +1 -43
  61. package/consume.d.ts.map +1 -1
  62. package/consume.js +1 -1
  63. package/consume.js.map +1 -1
  64. package/error.d.ts +180 -29
  65. package/error.d.ts.map +1 -1
  66. package/error.js +331 -16
  67. package/error.js.map +1 -1
  68. package/errors.d.ts +7 -0
  69. package/errors.d.ts.map +1 -0
  70. package/errors.js +8 -0
  71. package/errors.js.map +1 -0
  72. package/group.d.ts +9 -43
  73. package/group.d.ts.map +1 -1
  74. package/group.js +283 -69
  75. package/group.js.map +1 -1
  76. package/hop.d.ts +107 -0
  77. package/hop.d.ts.map +1 -0
  78. package/hop.js +103 -0
  79. package/hop.js.map +1 -0
  80. package/ietf/adapter.d.ts +5 -1
  81. package/ietf/adapter.d.ts.map +1 -1
  82. package/ietf/adapter.js +27 -9
  83. package/ietf/adapter.js.map +1 -1
  84. package/ietf/aliases.d.ts +1 -78
  85. package/ietf/aliases.d.ts.map +1 -1
  86. package/ietf/cluster.d.ts +6 -120
  87. package/ietf/cluster.d.ts.map +1 -1
  88. package/ietf/cluster.js +81 -41
  89. package/ietf/cluster.js.map +1 -1
  90. package/ietf/connection.d.ts +6 -72
  91. package/ietf/connection.d.ts.map +1 -1
  92. package/ietf/connection.js +17 -49
  93. package/ietf/connection.js.map +1 -1
  94. package/ietf/error.d.ts +11 -0
  95. package/ietf/error.d.ts.map +1 -0
  96. package/ietf/error.js +193 -0
  97. package/ietf/error.js.map +1 -0
  98. package/ietf/filter.d.ts.map +1 -1
  99. package/ietf/filter.js +6 -1
  100. package/ietf/filter.js.map +1 -1
  101. package/ietf/goaway.d.ts.map +1 -1
  102. package/ietf/goaway.js +22 -5
  103. package/ietf/goaway.js.map +1 -1
  104. package/ietf/hidden.d.ts +2 -0
  105. package/ietf/hidden.d.ts.map +1 -0
  106. package/ietf/hidden.js +30 -0
  107. package/ietf/hidden.js.map +1 -0
  108. package/ietf/index.d.ts +1 -0
  109. package/ietf/index.d.ts.map +1 -1
  110. package/ietf/index.js +1 -0
  111. package/ietf/index.js.map +1 -1
  112. package/ietf/object.d.ts +8 -2
  113. package/ietf/object.d.ts.map +1 -1
  114. package/ietf/object.js +17 -3
  115. package/ietf/object.js.map +1 -1
  116. package/ietf/parameters.d.ts +7 -2
  117. package/ietf/parameters.d.ts.map +1 -1
  118. package/ietf/parameters.js +35 -17
  119. package/ietf/parameters.js.map +1 -1
  120. package/ietf/properties.d.ts +1 -0
  121. package/ietf/properties.d.ts.map +1 -1
  122. package/ietf/properties.js +14 -0
  123. package/ietf/properties.js.map +1 -1
  124. package/ietf/publish.d.ts +19 -2
  125. package/ietf/publish.d.ts.map +1 -1
  126. package/ietf/publish.js +40 -6
  127. package/ietf/publish.js.map +1 -1
  128. package/ietf/publish_namespace.d.ts +25 -0
  129. package/ietf/publish_namespace.d.ts.map +1 -1
  130. package/ietf/publish_namespace.js +63 -0
  131. package/ietf/publish_namespace.js.map +1 -1
  132. package/ietf/publisher.d.ts +1 -82
  133. package/ietf/publisher.d.ts.map +1 -1
  134. package/ietf/publisher.js +302 -181
  135. package/ietf/publisher.js.map +1 -1
  136. package/ietf/solicit.d.ts +1 -40
  137. package/ietf/solicit.d.ts.map +1 -1
  138. package/ietf/subscribe.d.ts +1 -5
  139. package/ietf/subscribe.d.ts.map +1 -1
  140. package/ietf/subscribe.js +11 -13
  141. package/ietf/subscribe.js.map +1 -1
  142. package/ietf/subscribe_namespace.d.ts +8 -2
  143. package/ietf/subscribe_namespace.d.ts.map +1 -1
  144. package/ietf/subscribe_namespace.js +18 -8
  145. package/ietf/subscribe_namespace.js.map +1 -1
  146. package/ietf/subscriber.d.ts +1 -65
  147. package/ietf/subscriber.d.ts.map +1 -1
  148. package/ietf/subscriber.js +325 -121
  149. package/ietf/subscriber.js.map +1 -1
  150. package/ietf/version.d.ts +12 -1
  151. package/ietf/version.d.ts.map +1 -1
  152. package/ietf/version.js +13 -0
  153. package/ietf/version.js.map +1 -1
  154. package/index.d.ts +11 -6
  155. package/index.d.ts.map +1 -1
  156. package/index.js +10 -5
  157. package/index.js.map +1 -1
  158. package/internal.d.ts +104 -1
  159. package/internal.d.ts.map +1 -1
  160. package/internal.js +76 -0
  161. package/internal.js.map +1 -1
  162. package/lite/announce.d.ts +60 -10
  163. package/lite/announce.d.ts.map +1 -1
  164. package/lite/announce.js +173 -31
  165. package/lite/announce.js.map +1 -1
  166. package/lite/connection.d.ts +9 -59
  167. package/lite/connection.d.ts.map +1 -1
  168. package/lite/connection.js +26 -32
  169. package/lite/connection.js.map +1 -1
  170. package/lite/fetch.d.ts +15 -1
  171. package/lite/fetch.d.ts.map +1 -1
  172. package/lite/fetch.js +39 -7
  173. package/lite/fetch.js.map +1 -1
  174. package/lite/goaway.d.ts.map +1 -1
  175. package/lite/goaway.js +7 -1
  176. package/lite/goaway.js.map +1 -1
  177. package/lite/group.d.ts +18 -4
  178. package/lite/group.d.ts.map +1 -1
  179. package/lite/group.js +31 -10
  180. package/lite/group.js.map +1 -1
  181. package/lite/priority.d.ts +1 -61
  182. package/lite/priority.d.ts.map +1 -1
  183. package/lite/priority.js +4 -5
  184. package/lite/priority.js.map +1 -1
  185. package/lite/publisher.d.ts +1 -69
  186. package/lite/publisher.d.ts.map +1 -1
  187. package/lite/publisher.js +593 -227
  188. package/lite/publisher.js.map +1 -1
  189. package/lite/setup.d.ts +7 -7
  190. package/lite/setup.d.ts.map +1 -1
  191. package/lite/setup.js +13 -13
  192. package/lite/setup.js.map +1 -1
  193. package/lite/subscribe.d.ts +71 -17
  194. package/lite/subscribe.d.ts.map +1 -1
  195. package/lite/subscribe.js +205 -53
  196. package/lite/subscribe.js.map +1 -1
  197. package/lite/subscriber.d.ts +15 -56
  198. package/lite/subscriber.d.ts.map +1 -1
  199. package/lite/subscriber.js +316 -188
  200. package/lite/subscriber.js.map +1 -1
  201. package/lite/track.d.ts +4 -10
  202. package/lite/track.d.ts.map +1 -1
  203. package/lite/track.js +34 -29
  204. package/lite/track.js.map +1 -1
  205. package/lite/version.d.ts +42 -6
  206. package/lite/version.d.ts.map +1 -1
  207. package/lite/version.js +126 -10
  208. package/lite/version.js.map +1 -1
  209. package/origin.d.ts +248 -29
  210. package/origin.d.ts.map +1 -1
  211. package/origin.js +1232 -37
  212. package/origin.js.map +1 -1
  213. package/package.json +8 -3
  214. package/path.d.ts +25 -7
  215. package/path.d.ts.map +1 -1
  216. package/path.js +5 -3
  217. package/path.js.map +1 -1
  218. package/stream.d.ts +2 -2
  219. package/stream.d.ts.map +1 -1
  220. package/stream.js +39 -25
  221. package/stream.js.map +1 -1
  222. package/tail.d.ts +11 -0
  223. package/tail.d.ts.map +1 -0
  224. package/tail.js +121 -0
  225. package/tail.js.map +1 -0
  226. package/time.d.ts +15 -2
  227. package/time.d.ts.map +1 -1
  228. package/time.js +28 -9
  229. package/time.js.map +1 -1
  230. package/track.d.ts +195 -84
  231. package/track.d.ts.map +1 -1
  232. package/track.js +694 -196
  233. package/track.js.map +1 -1
  234. package/wire.d.ts +69 -0
  235. package/wire.d.ts.map +1 -0
  236. package/wire.js +32 -0
  237. package/wire.js.map +1 -0
  238. package/zod.d.ts +1 -1
  239. package/zod.d.ts.map +1 -1
  240. package/zod.js.map +1 -1
  241. package/mock.d.ts +0 -66
  242. package/mock.d.ts.map +0 -1
  243. package/mock.js +0 -243
  244. package/mock.js.map +0 -1
@@ -0,0 +1,361 @@
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) use a private loop. An explicit `share: true`
37
+ * refuses them. A 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;
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.#signals = new Effect();
118
+ this.url = Signal.from(props?.url);
119
+ this.enabled = Signal.from(props?.enabled ?? true);
120
+ this.status = this.#status;
121
+ this.error = this.#error;
122
+ this.probe = this.#probe;
123
+ this.origin = this.#origin;
124
+ this.bandwidth = this.#bandwidth;
125
+ this.transport = this.#signals.computed((effect) => effect.get(this.#established)?.transport);
126
+ const linger = props?.linger;
127
+ // Key on the serialized URL: URL objects use identity equality, and an equivalent
128
+ // instance must not release and redial.
129
+ const href = this.#signals.computed((effect) => effect.get(this.url)?.href);
130
+ if (props && (props.share === false || requiresPrivate(props))) {
131
+ this.#runPrivate(props, href);
132
+ return;
133
+ }
134
+ this.#signals.run((effect) => {
135
+ if (!effect.get(this.enabled))
136
+ return;
137
+ const key = effect.get(href);
138
+ if (!key)
139
+ return;
140
+ const lease = acquire(key, linger);
141
+ effect.cleanup(lease.release);
142
+ effect.set(this.#origin, lease.origin, undefined);
143
+ effect.set(this.#bandwidth, lease.connection.bandwidth, undefined);
144
+ // Proxy the shared loop's outputs, so this handle reads like its own connection.
145
+ effect.run((nested) => nested.set(this.#status, nested.get(lease.connection.status), "disconnected"));
146
+ effect.run((nested) => nested.set(this.#established, nested.get(lease.connection.established), undefined));
147
+ effect.run((nested) => nested.set(this.#probe, nested.get(lease.connection.probe), undefined));
148
+ effect.run((nested) => nested.set(this.#error, nested.get(lease.connection.error), undefined));
149
+ });
150
+ }
151
+ #runPrivate(props, href) {
152
+ const owned = props.consume === undefined;
153
+ const origin = props.consume ?? new Origin.Producer();
154
+ if (owned)
155
+ this.#signals.cleanup(() => origin.close());
156
+ const loop = new Reload({
157
+ url: this.url,
158
+ enabled: this.enabled,
159
+ publish: props.publish ?? (owned ? origin.consume() : undefined),
160
+ consume: origin,
161
+ webtransport: props.webtransport,
162
+ websocket: props.websocket,
163
+ discovery: props.discovery,
164
+ // A handle nobody watches wants unlimited retries; an auth rejection still
165
+ // stops this URL, and a new one starts another sequence.
166
+ delay: { timeout: Time.Milli(0), ...props.delay },
167
+ });
168
+ this.#signals.cleanup(() => loop.close());
169
+ this.#signals.run((effect) => {
170
+ if (!effect.get(this.enabled) || !effect.get(href))
171
+ return;
172
+ effect.set(this.#origin, origin, undefined);
173
+ effect.set(this.#bandwidth, loop.bandwidth, undefined);
174
+ });
175
+ this.#signals.run((effect) => effect.set(this.#status, effect.get(loop.status), "disconnected"));
176
+ this.#signals.run((effect) => effect.set(this.#established, effect.get(loop.established), undefined));
177
+ this.#signals.run((effect) => effect.set(this.#probe, effect.get(loop.probe), undefined));
178
+ this.#signals.run((effect) => effect.set(this.#error, effect.get(loop.error), undefined));
179
+ }
180
+ /**
181
+ * Subscribe to broadcast announcements matching `scope`, spanning reconnects
182
+ * and URL switches: a switch retracts everything from
183
+ * the old relay's origin, then the new one's arrivals stream in.
184
+ */
185
+ announced(scope = Path.Pattern.all(), options) {
186
+ const producer = new Announce.Producer();
187
+ const consumer = producer.consume();
188
+ // Closing the consumer closes the shared state, so stop appending after that.
189
+ let closed = false;
190
+ void consumer.closed.then(() => {
191
+ closed = true;
192
+ });
193
+ // A child of this handle's scope rather than a standalone Effect it merely cleans up
194
+ // after: the disposer run() hands back also drops itself from the parent, so opening
195
+ // and closing announcement streams repeatedly does not pile up dead pumps that live
196
+ // until the whole handle closes.
197
+ const stop = this.#signals.run((effect) => {
198
+ const origin = effect.get(this.#origin);
199
+ if (!origin)
200
+ return;
201
+ const upstream = origin.announced(scope, options);
202
+ effect.cleanup(() => upstream.close());
203
+ // Track what this origin announced so a URL switch retracts it; the last
204
+ // event rides along for the retraction.
205
+ const active = new Map();
206
+ effect.spawn(async () => {
207
+ try {
208
+ for (;;) {
209
+ const entry = await effect.race(upstream.next());
210
+ if (!entry)
211
+ break;
212
+ if (Announce.isActive(entry.kind))
213
+ active.set(entry.prefix, entry);
214
+ else
215
+ active.delete(entry.prefix);
216
+ producer.append(entry);
217
+ }
218
+ }
219
+ finally {
220
+ if (!closed) {
221
+ for (const entry of active.values()) {
222
+ producer.append({ ...entry, kind: "retracted" });
223
+ }
224
+ }
225
+ }
226
+ });
227
+ });
228
+ void consumer.closed.then(stop);
229
+ return consumer;
230
+ }
231
+ /** Snapshot the live connection's transport counters, or undefined while disconnected. */
232
+ async stats() {
233
+ return this.#established.peek()?.stats();
234
+ }
235
+ /**
236
+ * Release this handle. The shared connection closes once its last handle is gone and
237
+ * the linger window passes; other handles on the URL are unaffected. Idempotent.
238
+ */
239
+ close(abort) {
240
+ this.#signals.close();
241
+ if (this.#closed.peek() === undefined)
242
+ this.#closed.set(abort ?? null);
243
+ }
244
+ }
245
+ /** Throw if `props` cannot be honored, rather than silently dropping them. */
246
+ function refuse(props) {
247
+ if (!props)
248
+ return;
249
+ const extra = props;
250
+ if (extra.transport) {
251
+ throw new Error("a supplied transport cannot reconnect; call Connection.connect() instead");
252
+ }
253
+ if (extra.signal) {
254
+ throw new Error("a Connection owns its abort signal; do not pass one");
255
+ }
256
+ if (props.share !== true || !requiresPrivate(props))
257
+ return;
258
+ if (props.publish || props.consume) {
259
+ throw new Error("caller-owned origins cannot be shared; pass share: false");
260
+ }
261
+ const hashes = props.webtransport?.serverCertificateHashes?.length ?? 0;
262
+ if (props.webtransport?.serverCertificate !== undefined || hashes > 0) {
263
+ throw new Error("a pinned certificate cannot be shared; pass share: false");
264
+ }
265
+ if (props.webtransport !== undefined) {
266
+ throw new Error("webtransport options cannot be shared; pass share: false");
267
+ }
268
+ if (props.websocket !== undefined) {
269
+ throw new Error("websocket options cannot be shared; pass share: false");
270
+ }
271
+ if (props.discovery !== undefined) {
272
+ throw new Error("discovery cannot be shared; pass share: false");
273
+ }
274
+ if (props.delay !== undefined) {
275
+ throw new Error("delay cannot be shared; pass share: false");
276
+ }
277
+ }
278
+ /** Options tied to one handle cannot be represented by a URL-keyed shared entry. */
279
+ function requiresPrivate(props) {
280
+ return (props.webtransport !== undefined ||
281
+ props.websocket !== undefined ||
282
+ props.discovery !== undefined ||
283
+ props.delay !== undefined ||
284
+ props.publish !== undefined ||
285
+ props.consume !== undefined);
286
+ }
287
+ /** The process-wide pool backing {@link Connection}. */
288
+ const pool = new Map();
289
+ /** Take a reference on the shared entry for `key`, creating it on first use. */
290
+ function acquire(key, linger) {
291
+ let entry = pool.get(key);
292
+ if (!entry) {
293
+ const origin = new Origin.Producer();
294
+ const connection = new Reload({
295
+ url: new URL(key),
296
+ enabled: true,
297
+ publish: origin.consume(),
298
+ consume: origin,
299
+ // Nobody observes a shared loop's `closed`, so giving up would strand every handle
300
+ // on this URL offline until the page reloads. Retry for as long as the entry lives
301
+ // instead; an auth rejection still stops this URL, and evicts below.
302
+ delay: { timeout: Time.Milli(0) },
303
+ });
304
+ const created = { origin, connection, refs: 0, linger: linger ?? LINGER_MS };
305
+ // The loop only stops on a peer saying these credentials will never work. Drop the
306
+ // entry so a later handle dials fresh rather than joining a loop that has stopped;
307
+ // handles already on it keep it until they release, since a redial would be refused
308
+ // the same way.
309
+ connection.error.subscribe((err) => {
310
+ if (err !== undefined && pool.get(key) === created)
311
+ pool.delete(key);
312
+ });
313
+ entry = created;
314
+ pool.set(key, created);
315
+ }
316
+ const taken = entry;
317
+ taken.linger = Time.Milli(Math.max(taken.linger, linger ?? LINGER_MS));
318
+ taken.refs += 1;
319
+ if (taken.timer !== undefined) {
320
+ clearTimeout(taken.timer);
321
+ taken.timer = undefined;
322
+ }
323
+ let released = false;
324
+ return {
325
+ ...taken,
326
+ release: () => {
327
+ if (released)
328
+ return;
329
+ released = true;
330
+ taken.refs -= 1;
331
+ if (taken.refs > 0)
332
+ return;
333
+ taken.timer = setTimeout(() => {
334
+ if (pool.get(key) === taken)
335
+ pool.delete(key);
336
+ taken.connection.close();
337
+ taken.origin.close();
338
+ }, taken.linger);
339
+ // Don't hold a Node process open for a connection nobody is using.
340
+ taken.timer.unref?.();
341
+ },
342
+ };
343
+ }
344
+ /**
345
+ * Close every shared connection immediately, so the next handle dials fresh.
346
+ *
347
+ * Exists for tests, which otherwise share connections across cases.
348
+ *
349
+ * @internal
350
+ */
351
+ export function resetShared() {
352
+ const entries = [...pool.values()];
353
+ pool.clear();
354
+ for (const entry of entries) {
355
+ if (entry.timer !== undefined)
356
+ clearTimeout(entry.timer);
357
+ entry.connection.close();
358
+ entry.origin.close();
359
+ }
360
+ }
361
+ //# 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;AAmDnC;;;;;;;;;;;;;;;;;;;;;;;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,CAA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AC,KAAK,KAAK;oBAAE,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;gBAC9C,KAAK,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC;gBACzB,KAAK,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;YACtB,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;YAEjB,mEAAmE;YAClE,KAAK,CAAC,KAAgC,CAAC,KAAK,EAAE,EAAE,CAAC;QACnD,CAAC;KACD,CAAC;AACH,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,WAAW;IAC1B,MAAM,OAAO,GAAG,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;IACnC,IAAI,CAAC,KAAK,EAAE,CAAC;IACb,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE,CAAC;QAC7B,IAAI,KAAK,CAAC,KAAK,KAAK,SAAS;YAAE,YAAY,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACzD,KAAK,CAAC,UAAU,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. By default, options specific to this\n\t * handle use a private reconnect loop; otherwise connections share by URL.\n\t * Explicit `true` refuses options a pool cannot honor.\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. Implies a private loop. */\n\tpublish?: Origin.Consumer;\n\n\t/** The origin fed with the peer's announced broadcasts, spanning reconnects. Implies a private loop. */\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) use a private loop. An explicit `share: true`\n * refuses them. A 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: 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\t\tthis.#signals = new Effect();\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 && (props.share === false || requiresPrivate(props))) {\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(), options?: Announce.Options): 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, options);\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 effect.race(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 !== true || !requiresPrivate(props)) 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/** Options tied to one handle cannot be represented by a URL-keyed shared entry. */\nfunction requiresPrivate(props: ConnectionProps): boolean {\n\treturn (\n\t\tprops.webtransport !== undefined ||\n\t\tprops.websocket !== undefined ||\n\t\tprops.discovery !== undefined ||\n\t\tprops.delay !== undefined ||\n\t\tprops.publish !== undefined ||\n\t\tprops.consume !== undefined\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"]}
@@ -1,104 +1,25 @@
1
- import { type Getter, Signal } from "@moq/signals";
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 {@link Reload}'s reconnect loop.
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 loop.
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 in milliseconds before reconnecting (default: 1000). */
15
- initial: DOMHighResTimeStamp;
10
+ /** The delay before reconnecting (default: 1000ms). */
11
+ initial?: Time.Milli;
16
12
  /** The multiplier for the delay (default: 2). */
17
- multiplier: number;
18
- /** The maximum delay in milliseconds (default: 5000). */
19
- max: DOMHighResTimeStamp;
13
+ multiplier?: number;
14
+ /** The maximum delay (default: 5000ms). */
15
+ max?: Time.Milli;
20
16
  /**
21
- * Maximum total time in milliseconds to spend retrying before giving up (default:
22
- * 10000). Resets after each successful connection. Set to 0 for unlimited retries.
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?: DOMHighResTimeStamp;
21
+ timeout?: Time.Milli;
25
22
  };
26
- /** Connection and retry options for {@link Reload}. */
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":"AAAA,OAAO,EAAU,KAAK,MAAM,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAC3D,OAAO,KAAK,QAAQ,MAAM,iBAAiB,CAAC;AAE5C,OAAO,KAAK,KAAK,IAAI,MAAM,YAAY,CAAC;AAExC,OAAO,EAAE,KAAK,YAAY,EAAW,KAAK,gBAAgB,EAAE,KAAK,iBAAiB,EAAE,MAAM,cAAc,CAAC;AACzG,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AACpD,OAAO,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAE/C;;;;;GAKG;AACH,MAAM,MAAM,WAAW,GAAG;IACzB,qEAAqE;IACrE,OAAO,EAAE,mBAAmB,CAAC;IAE7B,iDA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