@caprid/relay 0.4.1 → 0.6.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 +55 -53
- package/dist/client-diagnostics.d.ts +235 -0
- package/dist/client-diagnostics.d.ts.map +1 -0
- package/dist/client-diagnostics.js +582 -0
- package/dist/client-diagnostics.js.map +1 -0
- package/dist/connect-session.d.ts +43 -3
- package/dist/connect-session.d.ts.map +1 -1
- package/dist/connect-session.js +46 -3
- package/dist/connect-session.js.map +1 -1
- package/dist/connect.d.ts +41 -6
- package/dist/connect.d.ts.map +1 -1
- package/dist/connect.js +19 -3
- package/dist/connect.js.map +1 -1
- package/dist/diagnostics-report.d.ts +106 -0
- package/dist/diagnostics-report.d.ts.map +1 -0
- package/dist/diagnostics-report.js +450 -0
- package/dist/diagnostics-report.js.map +1 -0
- package/dist/diagnostics.d.ts +20 -0
- package/dist/diagnostics.d.ts.map +1 -0
- package/dist/diagnostics.js +20 -0
- package/dist/diagnostics.js.map +1 -0
- package/dist/discovery.d.ts +38 -0
- package/dist/discovery.d.ts.map +1 -0
- package/dist/discovery.js +273 -0
- package/dist/discovery.js.map +1 -0
- package/dist/frame-reader.d.ts +12 -0
- package/dist/frame-reader.d.ts.map +1 -1
- package/dist/frame-reader.js +65 -0
- package/dist/frame-reader.js.map +1 -1
- package/dist/frame-writer.d.ts +16 -2
- package/dist/frame-writer.d.ts.map +1 -1
- package/dist/frame-writer.js +73 -1
- package/dist/frame-writer.js.map +1 -1
- package/dist/index.d.ts +8 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/media-track-properties.js +1 -1
- package/dist/media-track-properties.js.map +1 -1
- package/dist/media.d.ts +4 -1
- package/dist/media.d.ts.map +1 -1
- package/dist/media.js +4 -1
- package/dist/media.js.map +1 -1
- package/dist/namespace-events.d.ts +32 -3
- package/dist/namespace-events.d.ts.map +1 -1
- package/dist/namespace-events.js +82 -7
- package/dist/namespace-events.js.map +1 -1
- package/dist/node.d.ts +13 -0
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +17 -0
- package/dist/node.js.map +1 -1
- package/dist/presence.d.ts +155 -0
- package/dist/presence.d.ts.map +1 -0
- package/dist/presence.js +485 -0
- package/dist/presence.js.map +1 -0
- package/dist/publish.d.ts +61 -7
- package/dist/publish.d.ts.map +1 -1
- package/dist/publish.js +208 -87
- package/dist/publish.js.map +1 -1
- package/dist/publishing-roots.d.ts +189 -0
- package/dist/publishing-roots.d.ts.map +1 -0
- package/dist/publishing-roots.js +426 -0
- package/dist/publishing-roots.js.map +1 -0
- package/dist/relay-session.d.ts +31 -0
- package/dist/relay-session.d.ts.map +1 -0
- package/dist/relay-session.js +64 -0
- package/dist/relay-session.js.map +1 -0
- package/dist/subscription-events.d.ts +5 -2
- package/dist/subscription-events.d.ts.map +1 -1
- package/dist/subscription-events.js +20 -6
- package/dist/subscription-events.js.map +1 -1
- package/dist/supervised-publisher.d.ts +7 -0
- package/dist/supervised-publisher.d.ts.map +1 -1
- package/dist/supervised-publisher.js +46 -809
- package/dist/supervised-publisher.js.map +1 -1
- package/dist/supervised-relay.d.ts +120 -0
- package/dist/supervised-relay.d.ts.map +1 -0
- package/dist/supervised-relay.js +83 -0
- package/dist/supervised-relay.js.map +1 -0
- package/dist/supervised-subscriber.d.ts +74 -4
- package/dist/supervised-subscriber.d.ts.map +1 -1
- package/dist/supervised-subscriber.js +259 -561
- package/dist/supervised-subscriber.js.map +1 -1
- package/dist/supervised-track.d.ts +63 -0
- package/dist/supervised-track.d.ts.map +1 -0
- package/dist/supervised-track.js +430 -0
- package/dist/supervised-track.js.map +1 -0
- package/dist/supervisor.d.ts +101 -0
- package/dist/supervisor.d.ts.map +1 -0
- package/dist/supervisor.js +744 -0
- package/dist/supervisor.js.map +1 -0
- package/dist/switchable.d.ts +1 -1
- package/dist/version.d.ts +6 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +6 -0
- package/dist/version.js.map +1 -0
- package/dist/write-slot.d.ts +5 -0
- package/dist/write-slot.d.ts.map +1 -1
- package/dist/write-slot.js +3 -0
- package/dist/write-slot.js.map +1 -1
- package/package.json +13 -9
|
@@ -11,12 +11,15 @@
|
|
|
11
11
|
* make-before-break, and a want that keeps its target sees no break and no repeat. See spec/behavior.md, "Supervised
|
|
12
12
|
* connections".
|
|
13
13
|
*/
|
|
14
|
-
import {
|
|
14
|
+
import { PUBLISH_DONE_STATUS, } from '@caprid/moq-core';
|
|
15
15
|
import { connectRelaySubscriber } from './connect.js';
|
|
16
|
-
import { assertTimeoutMs
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
16
|
+
import { assertTimeoutMs } from './connect-session.js';
|
|
17
|
+
import { diagnosticsOption, wantDiagnostics } from './diagnostics-report.js';
|
|
18
|
+
import { DISCOVERY_LINGER_MS, discoveryEnding, refusedForGood } from './discovery.js';
|
|
19
|
+
import { openPublishingRoots, } from './publishing-roots.js';
|
|
20
|
+
import { SubscriptionRequestError, synchronousCallback } from './subscription.js';
|
|
21
|
+
import { backoffDelay, backoffOptions, isFreshNamespaceRejection, isPermanentRequestError, RECONNECT_BACKOFF, RESUBSCRIBE_BACKOFF, rotateBeforeOption, sleep, whenAborted, } from './supervision.js';
|
|
22
|
+
import { createSupervisor } from './supervisor.js';
|
|
20
23
|
import { assertDrainMs, heldAfter, holdEvent, untilPredicate, } from './switchable.js';
|
|
21
24
|
import { namespaceFields, trackName } from './validation.js';
|
|
22
25
|
/**
|
|
@@ -49,373 +52,84 @@ export async function superviseRelaySubscriber(connect, options) {
|
|
|
49
52
|
if (typeof options !== 'object' || options === null) {
|
|
50
53
|
throw new TypeError('connectSupervisedSubscriber takes an options object');
|
|
51
54
|
}
|
|
52
|
-
const { reconnect, rotateBeforeMs: rotateBefore, onStatus, signal, ...connection } = options;
|
|
55
|
+
const { reconnect, rotateBeforeMs: rotateBefore, onStatus, signal, diagnostics, ...connection } = options;
|
|
53
56
|
const backoff = backoffOptions('reconnect', reconnect, RECONNECT_BACKOFF);
|
|
57
|
+
const reporting = diagnosticsOption(diagnostics)?.scope('subscriber');
|
|
54
58
|
const rotateBeforeMs = rotateBeforeOption(rotateBefore);
|
|
55
59
|
signal?.throwIfAborted();
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
wakeRotation();
|
|
82
|
-
};
|
|
83
|
-
const setStatus = (next, error) => {
|
|
84
|
-
if (status === next && error === undefined)
|
|
85
|
-
return;
|
|
86
|
-
status = next;
|
|
87
|
-
onStatus?.(next, error);
|
|
60
|
+
const supervisor = createSupervisor(async (attempt) => {
|
|
61
|
+
const subscriber = await connect(attempt);
|
|
62
|
+
return {
|
|
63
|
+
subscriber,
|
|
64
|
+
closed: subscriber.closed,
|
|
65
|
+
get expiresAt() {
|
|
66
|
+
return subscriber.expiresAt;
|
|
67
|
+
},
|
|
68
|
+
close: () => subscriber.close(),
|
|
69
|
+
};
|
|
70
|
+
}, { what: 'Supervised subscriber', connection, signal, backoff, rotateBeforeMs, onStatus, diagnostics: reporting });
|
|
71
|
+
const wants = createWantSurface(supervisor, reporting);
|
|
72
|
+
await supervisor.start();
|
|
73
|
+
return {
|
|
74
|
+
get status() {
|
|
75
|
+
return supervisor.status;
|
|
76
|
+
},
|
|
77
|
+
closed: supervisor.closed,
|
|
78
|
+
...wants,
|
|
79
|
+
rotate() {
|
|
80
|
+
supervisor.rotate();
|
|
81
|
+
},
|
|
82
|
+
close() {
|
|
83
|
+
void supervisor.stop({});
|
|
84
|
+
},
|
|
88
85
|
};
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
first?.reject(reason);
|
|
102
|
-
first = undefined;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Keep wants and publishing-roots views on `supervisor`'s connections: each moves with a rotation and comes back on
|
|
89
|
+
* the next connection. Everything is cancelled when the supervisor stops. Given `reporting`, a diagnostics scope, each
|
|
90
|
+
* want's life is reported into it (`want` events and the `wants` section).
|
|
91
|
+
*/
|
|
92
|
+
export function createWantSurface(supervisor, reporting) {
|
|
93
|
+
const wants = new Map();
|
|
94
|
+
const views = new Set();
|
|
95
|
+
const wantReport = reporting && wantDiagnostics(reporting);
|
|
96
|
+
const liveConnection = (scope) => supervisor.liveConnection(scope);
|
|
97
|
+
supervisor.onStop(() => {
|
|
103
98
|
for (const want of wants.values())
|
|
104
99
|
want.controller.abort();
|
|
105
100
|
wants.clear();
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
resolveClosed(result);
|
|
117
|
-
}
|
|
118
|
-
};
|
|
119
|
-
const onAbort = () => {
|
|
120
|
-
try {
|
|
121
|
-
stop({ error: signal?.reason });
|
|
122
|
-
}
|
|
123
|
-
catch {
|
|
124
|
-
// A throwing onStatus while reporting `closed`: `closed` is settled, and an abort listener has no caller to tell.
|
|
125
|
-
}
|
|
126
|
-
};
|
|
127
|
-
signal?.addEventListener('abort', onAbort, { once: true });
|
|
128
|
-
/** The live connection, waiting for one while reconnecting; `undefined` once `scope` aborts. */
|
|
129
|
-
const liveConnection = async (scope) => {
|
|
130
|
-
while (!scope.aborted && !stopped) {
|
|
131
|
-
if (current && !current.signal.aborted)
|
|
132
|
-
return current;
|
|
133
|
-
const woken = new Promise((resolve) => connectionWaiters.push(resolve));
|
|
134
|
-
await Promise.race([woken, whenAborted(scope)]);
|
|
135
|
-
}
|
|
136
|
-
return undefined;
|
|
137
|
-
};
|
|
138
|
-
/** Open one connection, or `undefined` if the supervisor stopped first. Throws the attempt's failure. */
|
|
139
|
-
const connectOnce = async () => {
|
|
140
|
-
let subscriber;
|
|
141
|
-
try {
|
|
142
|
-
attemptInProgress = new AbortController();
|
|
143
|
-
subscriber = await connect({
|
|
144
|
-
...connection,
|
|
145
|
-
signal: signal ? AbortSignal.any([signal, attemptInProgress.signal]) : attemptInProgress.signal,
|
|
146
|
-
[reportEndpoint]: (endpoint) => {
|
|
147
|
-
connection.url ??= endpoint;
|
|
148
|
-
},
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
finally {
|
|
152
|
-
attemptInProgress = undefined;
|
|
153
|
-
}
|
|
154
|
-
if (stopped) {
|
|
155
|
-
subscriber.close();
|
|
156
|
-
return undefined;
|
|
157
|
-
}
|
|
158
|
-
const ended = new AbortController();
|
|
159
|
-
// Aborted the moment the connection ends, so a rotation in progress sees it and falls back to reconnecting.
|
|
160
|
-
void subscriber.closed.then(() => ended.abort());
|
|
161
|
-
const retire = new AbortController();
|
|
162
|
-
const live = {
|
|
163
|
-
subscriber,
|
|
164
|
-
signal: ended.signal,
|
|
165
|
-
retiring: retire.signal,
|
|
166
|
-
roots: new Map(),
|
|
167
|
-
wakes: new Map(),
|
|
168
|
-
holds: 0,
|
|
169
|
-
rotateAt: rotationDeadline(subscriber.expiresAt, rotateBeforeMs),
|
|
170
|
-
};
|
|
171
|
-
return { live, ended, retire };
|
|
172
|
-
};
|
|
173
|
-
/** The connection ended: release what it ran and stop anything still waiting on it. */
|
|
174
|
-
const settleEnded = (opened) => {
|
|
175
|
-
opened.ended.abort();
|
|
176
|
-
for (const watch of opened.live.roots.values())
|
|
177
|
-
watch.close();
|
|
178
|
-
for (const wake of opened.live.wakes.values())
|
|
179
|
-
wake.close();
|
|
180
|
-
};
|
|
181
|
-
/**
|
|
182
|
-
* Open the connection that replaces `opened`, move every want to it, and close the old one once they have moved.
|
|
183
|
-
* Returns the new connection, or `undefined` when the old one ended or the supervisor stopped before a replacement
|
|
184
|
-
* was up, or when an automatic rotation found no fresher credential.
|
|
185
|
-
*/
|
|
186
|
-
const rotateFrom = async (opened, manual) => {
|
|
187
|
-
const old = opened.live;
|
|
188
|
-
let failures = 0;
|
|
189
|
-
let next;
|
|
190
|
-
// The live connection ending while its replacement is still connecting is a reconnect: report it at once.
|
|
191
|
-
void old.subscriber.closed.then(({ error }) => {
|
|
192
|
-
if (stopped || current !== old)
|
|
193
|
-
return;
|
|
194
|
-
current = undefined;
|
|
195
|
-
try {
|
|
196
|
-
setStatus('reconnecting', error);
|
|
197
|
-
}
|
|
198
|
-
catch (thrown) {
|
|
199
|
-
try {
|
|
200
|
-
stop({ error: thrown });
|
|
201
|
-
}
|
|
202
|
-
catch {
|
|
203
|
-
// A throwing onStatus while reporting `closed`: `closed` is settled either way.
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
});
|
|
207
|
-
while (!stopped && !old.signal.aborted) {
|
|
208
|
-
try {
|
|
209
|
-
next = await connectOnce();
|
|
210
|
-
break;
|
|
211
|
-
}
|
|
212
|
-
catch {
|
|
213
|
-
if (stopped)
|
|
214
|
-
return undefined;
|
|
215
|
-
// The live connection keeps serving; try again after the backoff unless it ends first.
|
|
216
|
-
await Promise.race([
|
|
217
|
-
sleep(backoffDelay(failures++, backoff), lifetime.signal).catch(() => { }),
|
|
218
|
-
whenAborted(old.signal),
|
|
219
|
-
]);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
if (!next)
|
|
223
|
-
return undefined;
|
|
224
|
-
// `close()` may have run while the helper's result was on its way here: it could not see this connection.
|
|
225
|
-
if (stopped) {
|
|
226
|
-
next.live.subscriber.close();
|
|
227
|
-
return undefined;
|
|
228
|
-
}
|
|
229
|
-
// Consulted whatever happened to the old connection: a queued `rotate()` is answered by this replacement.
|
|
230
|
-
const asked = manual();
|
|
231
|
-
if (!old.signal.aborted && !asked) {
|
|
232
|
-
// A credential that expires no later than the live one replaces nothing: the relay would end both together.
|
|
233
|
-
const was = old.subscriber.expiresAt?.getTime();
|
|
234
|
-
const now = next.live.subscriber.expiresAt?.getTime();
|
|
235
|
-
if (was !== undefined && (now === undefined || now <= was)) {
|
|
236
|
-
next.live.subscriber.close();
|
|
237
|
-
return undefined;
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
current = next.live;
|
|
241
|
-
retiring = old;
|
|
242
|
-
setStatus('live');
|
|
243
|
-
for (const wake of connectionWaiters.splice(0))
|
|
244
|
-
wake();
|
|
245
|
-
const idle = new Promise((resolve) => {
|
|
246
|
-
old.onIdle = resolve;
|
|
247
|
-
});
|
|
248
|
-
// Every want on the old connection ends its attempt there and subscribes on the new one.
|
|
249
|
-
opened.retire.abort();
|
|
250
|
-
if (old.holds === 0)
|
|
251
|
-
old.onIdle?.();
|
|
252
|
-
// The replacement ending cuts the overlap short too: the wants on it need the next connection now. So does its own
|
|
253
|
-
// rotation falling due, for a credential shorter than the overlap.
|
|
254
|
-
const overlap = new AbortController();
|
|
255
|
-
await Promise.race([
|
|
256
|
-
idle,
|
|
257
|
-
sleep(ROTATION_OVERLAP_MS, lifetime.signal).catch(() => { }),
|
|
258
|
-
whenAborted(old.signal),
|
|
259
|
-
whenAborted(next.live.signal),
|
|
260
|
-
rotationDue(next.live.rotateAt, new Promise(() => { }), AbortSignal.any([lifetime.signal, overlap.signal])),
|
|
261
|
-
]);
|
|
262
|
-
overlap.abort();
|
|
263
|
-
old.onIdle = undefined;
|
|
264
|
-
if (retiring === old)
|
|
265
|
-
retiring = undefined;
|
|
266
|
-
old.subscriber.close();
|
|
267
|
-
settleEnded(opened);
|
|
268
|
-
return next;
|
|
269
|
-
};
|
|
270
|
-
/** Serve `opened` until it ends, or until a rotation replaced it with the connection returned. */
|
|
271
|
-
const serve = async (opened) => {
|
|
272
|
-
const { subscriber } = opened.live;
|
|
273
|
-
const scope = new AbortController();
|
|
274
|
-
/** Cleared once an automatic rotation found no fresher credential: only `rotate()` tries again. */
|
|
275
|
-
let automatic = true;
|
|
276
|
-
try {
|
|
277
|
-
for (;;) {
|
|
278
|
-
const asked = new Promise((resolve) => {
|
|
279
|
-
wakeRotation = resolve;
|
|
280
|
-
});
|
|
281
|
-
if (rotateRequested)
|
|
282
|
-
wakeRotation();
|
|
283
|
-
const due = rotationDue(automatic ? opened.live.rotateAt : undefined, asked, AbortSignal.any([lifetime.signal, scope.signal])).then(() => undefined);
|
|
284
|
-
const outcome = await Promise.race([subscriber.closed, due]);
|
|
285
|
-
if (outcome !== undefined || stopped)
|
|
286
|
-
return outcome ?? {};
|
|
287
|
-
// A `rotate()` before the replacement is current makes this rotation a manual one: it takes the replacement.
|
|
288
|
-
let manual = false;
|
|
289
|
-
const isManual = () => {
|
|
290
|
-
if (rotateRequested) {
|
|
291
|
-
rotateRequested = false;
|
|
292
|
-
manual = true;
|
|
293
|
-
}
|
|
294
|
-
return manual;
|
|
295
|
-
};
|
|
296
|
-
isManual();
|
|
297
|
-
const next = await rotateFrom(opened, isManual);
|
|
298
|
-
if (next)
|
|
299
|
-
return { next };
|
|
300
|
-
if (opened.live.signal.aborted || stopped)
|
|
301
|
-
return await subscriber.closed;
|
|
302
|
-
if (!manual)
|
|
303
|
-
automatic = false;
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
finally {
|
|
307
|
-
scope.abort();
|
|
308
|
-
wakeRotation = () => { };
|
|
309
|
-
}
|
|
101
|
+
wantReport?.forgetAll();
|
|
102
|
+
for (const view of [...views])
|
|
103
|
+
view.close();
|
|
104
|
+
});
|
|
105
|
+
const forget = (key, want) => {
|
|
106
|
+
want.controller.abort();
|
|
107
|
+
if (wants.get(key) !== want)
|
|
108
|
+
return;
|
|
109
|
+
wants.delete(key);
|
|
110
|
+
wantReport?.forget(key);
|
|
310
111
|
};
|
|
311
|
-
const
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
next = undefined;
|
|
318
|
-
if (!opened) {
|
|
319
|
-
try {
|
|
320
|
-
opened = await connectOnce();
|
|
321
|
-
}
|
|
322
|
-
catch (error) {
|
|
323
|
-
if (stopped)
|
|
324
|
-
return;
|
|
325
|
-
if (first && isSetupFailure(error)) {
|
|
326
|
-
first.reject(error);
|
|
327
|
-
first = undefined;
|
|
328
|
-
stop({ error });
|
|
329
|
-
return;
|
|
330
|
-
}
|
|
331
|
-
setStatus('reconnecting', error);
|
|
332
|
-
await sleep(backoffDelay(attempt++, backoff), lifetime.signal).catch(() => { });
|
|
333
|
-
continue;
|
|
334
|
-
}
|
|
335
|
-
if (!opened)
|
|
336
|
-
return;
|
|
337
|
-
// `close()` may have run while the helper's result was on its way here: it could not see this connection.
|
|
338
|
-
if (stopped) {
|
|
339
|
-
opened.live.subscriber.close();
|
|
340
|
-
return;
|
|
341
|
-
}
|
|
342
|
-
current = opened.live;
|
|
343
|
-
setStatus('live');
|
|
344
|
-
first?.resolve();
|
|
345
|
-
first = undefined;
|
|
346
|
-
for (const wake of connectionWaiters.splice(0))
|
|
347
|
-
wake();
|
|
348
|
-
}
|
|
349
|
-
const upAt = performance.now();
|
|
350
|
-
const outcome = await serve(opened);
|
|
351
|
-
if ('next' in outcome) {
|
|
352
|
-
if (stopped)
|
|
353
|
-
return;
|
|
354
|
-
next = outcome.next;
|
|
355
|
-
attempt = 0;
|
|
112
|
+
const guard = (handlers, what) => {
|
|
113
|
+
if (typeof handlers !== 'object' || handlers === null)
|
|
114
|
+
throw new TypeError(`${what} handlers must be an object`);
|
|
115
|
+
const guarded = {};
|
|
116
|
+
for (const [name, callback] of Object.entries(handlers)) {
|
|
117
|
+
if (callback === undefined)
|
|
356
118
|
continue;
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
return;
|
|
361
|
-
current = undefined;
|
|
362
|
-
// Already reported if the connection ended while a rotation's replacement was connecting.
|
|
363
|
-
if (status !== 'reconnecting')
|
|
364
|
-
setStatus('reconnecting', outcome.error);
|
|
365
|
-
if (performance.now() - upAt > backoff.maxDelayMs)
|
|
366
|
-
attempt = 0;
|
|
367
|
-
await sleep(backoffDelay(attempt++, backoff), lifetime.signal).catch(() => { });
|
|
368
|
-
}
|
|
369
|
-
};
|
|
370
|
-
const supervise = async () => {
|
|
371
|
-
try {
|
|
372
|
-
await run();
|
|
119
|
+
if (typeof callback !== 'function')
|
|
120
|
+
throw new TypeError(`${name} must be a function`);
|
|
121
|
+
guarded[name] = synchronousCallback(name, callback, handlers);
|
|
373
122
|
}
|
|
374
|
-
|
|
375
|
-
// An application callback (onStatus) threw: stop rather than leave the supervisor neither reconnecting nor
|
|
376
|
-
// closed, and fail a caller still waiting to connect.
|
|
377
|
-
stop({ error });
|
|
378
|
-
}
|
|
379
|
-
};
|
|
380
|
-
// A second throw, from onStatus reporting `closed`, has nowhere to go: `closed` is settled either way.
|
|
381
|
-
void supervise().catch(() => { });
|
|
382
|
-
try {
|
|
383
|
-
await started;
|
|
384
|
-
}
|
|
385
|
-
catch (error) {
|
|
386
|
-
signal?.removeEventListener('abort', onAbort);
|
|
387
|
-
throw error;
|
|
388
|
-
}
|
|
389
|
-
if (stopped)
|
|
390
|
-
throw signal?.reason ?? new RelayError('CLOSED', 'Supervised subscriber is closed', 'connection');
|
|
391
|
-
const forget = (key, want) => {
|
|
392
|
-
want.controller.abort();
|
|
393
|
-
if (wants.get(key) === want)
|
|
394
|
-
wants.delete(key);
|
|
123
|
+
return guarded;
|
|
395
124
|
};
|
|
396
125
|
return {
|
|
397
|
-
get status() {
|
|
398
|
-
return status;
|
|
399
|
-
},
|
|
400
|
-
closed,
|
|
401
126
|
want(key, wantOptions, handlers = {}) {
|
|
402
127
|
if (typeof key !== 'string')
|
|
403
128
|
throw new TypeError('want key must be a string');
|
|
404
129
|
if (typeof wantOptions !== 'object' || wantOptions === null) {
|
|
405
130
|
throw new TypeError('want takes an options object');
|
|
406
131
|
}
|
|
407
|
-
|
|
408
|
-
throw new TypeError('want handlers must be an object');
|
|
409
|
-
// The primitive's guard: a declared-async callback is refused, a returned promise is observed, never left unhandled.
|
|
410
|
-
const guarded = {};
|
|
411
|
-
for (const [name, callback] of Object.entries(handlers)) {
|
|
412
|
-
if (callback === undefined)
|
|
413
|
-
continue;
|
|
414
|
-
if (typeof callback !== 'function')
|
|
415
|
-
throw new TypeError(`${name} must be a function`);
|
|
416
|
-
guarded[name] = synchronousCallback(name, callback, handlers);
|
|
417
|
-
}
|
|
418
|
-
const safeHandlers = guarded;
|
|
132
|
+
const safeHandlers = guard(handlers, 'want');
|
|
419
133
|
const { parameters, generations = false, staleAfterMs, retry, until, drainMs = 0 } = wantOptions;
|
|
420
134
|
const namespace = namespaceFields(wantOptions.namespace);
|
|
421
135
|
if (generations)
|
|
@@ -426,13 +140,15 @@ export async function superviseRelaySubscriber(connect, options) {
|
|
|
426
140
|
const retryBackoff = backoffOptions('retry', retry, RESUBSCRIBE_BACKOFF);
|
|
427
141
|
const predicate = untilPredicate(until);
|
|
428
142
|
assertDrainMs(drainMs);
|
|
429
|
-
if (stopped)
|
|
430
|
-
throw
|
|
143
|
+
if (supervisor.stopped)
|
|
144
|
+
throw supervisor.closedError();
|
|
431
145
|
const source = JSON.stringify([namespace, generations]);
|
|
432
146
|
const target = targetKey([], track, parameters);
|
|
433
147
|
const existing = wants.get(key);
|
|
434
148
|
if (existing && existing.source === source) {
|
|
435
|
-
existing.handlers =
|
|
149
|
+
existing.handlers = wantReport
|
|
150
|
+
? wantReport.observe(key, track, safeHandlers, () => existing.switchMs)
|
|
151
|
+
: safeHandlers;
|
|
436
152
|
existing.retry = retryBackoff;
|
|
437
153
|
existing.until = predicate;
|
|
438
154
|
existing.drainMs = drainMs;
|
|
@@ -451,7 +167,7 @@ export async function superviseRelaySubscriber(connect, options) {
|
|
|
451
167
|
options: { namespace, track, parameters, generations, staleAfterMs },
|
|
452
168
|
source,
|
|
453
169
|
target,
|
|
454
|
-
handlers: safeHandlers,
|
|
170
|
+
handlers: wantReport ? wantReport.observe(key, track, safeHandlers, () => want.switchMs) : safeHandlers,
|
|
455
171
|
retry: retryBackoff,
|
|
456
172
|
until: predicate,
|
|
457
173
|
drainMs,
|
|
@@ -459,7 +175,7 @@ export async function superviseRelaySubscriber(connect, options) {
|
|
|
459
175
|
controller: new AbortController(),
|
|
460
176
|
};
|
|
461
177
|
wants.set(key, want);
|
|
462
|
-
void runWant(want, liveConnection,
|
|
178
|
+
void runWant(want, liveConnection, supervisor.current, () => forget(key, want));
|
|
463
179
|
},
|
|
464
180
|
unwant(key) {
|
|
465
181
|
const want = wants.get(key);
|
|
@@ -471,26 +187,28 @@ export async function superviseRelaySubscriber(connect, options) {
|
|
|
471
187
|
throw new TypeError('want key must be a string');
|
|
472
188
|
wants.get(key)?.catchUp?.();
|
|
473
189
|
},
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
190
|
+
publishingRoots(rootsOptions, handlers = {}) {
|
|
191
|
+
if (typeof rootsOptions !== 'object' || rootsOptions === null) {
|
|
192
|
+
throw new TypeError('publishingRoots takes an options object');
|
|
193
|
+
}
|
|
194
|
+
const safeHandlers = guard(handlers, 'publishingRoots');
|
|
195
|
+
const prefix = namespaceFields(rootsOptions.prefix);
|
|
196
|
+
namespaceFields([...prefix, 'x', 'x']);
|
|
197
|
+
if (supervisor.stopped)
|
|
198
|
+
throw supervisor.closedError();
|
|
199
|
+
const view = openPublishingRoots({
|
|
200
|
+
liveConnection,
|
|
201
|
+
current: supervisor.current,
|
|
202
|
+
hold,
|
|
203
|
+
onClosed: () => views.delete(view),
|
|
204
|
+
// The connection a rotation replaces sees the next one's announces, and the other way round: hide both.
|
|
205
|
+
hides: (namespace) => supervisor.ownsNamespace(namespace),
|
|
206
|
+
}, prefix, safeHandlers);
|
|
207
|
+
views.add(view);
|
|
208
|
+
return view;
|
|
480
209
|
},
|
|
481
210
|
};
|
|
482
211
|
}
|
|
483
|
-
/**
|
|
484
|
-
* Whether a request refused at the call is the want's own fault, which no connection fixes: an argument, or a root
|
|
485
|
-
* overlapping another followed root. Anything else thrown there (CLOSED, the connection's TRANSPORT_ERROR or GOAWAY)
|
|
486
|
-
* is the connection ending before the supervisor has heard of it.
|
|
487
|
-
*/
|
|
488
|
-
function refusedForGood(error) {
|
|
489
|
-
// The core's encoders refuse a request the wire cannot carry (a field too long to encode) the same way every time.
|
|
490
|
-
if (error instanceof TypeError || error instanceof RangeError || isMoqtProtocolError(error))
|
|
491
|
-
return true;
|
|
492
|
-
return error instanceof RelayError && error.code === 'PREFIX_OVERLAP';
|
|
493
|
-
}
|
|
494
212
|
/** Supervise `want`; a failure its loop did not expect forgets it rather than leave it registered but dead. */
|
|
495
213
|
async function runWant(want, liveConnection, nextConnection, forget) {
|
|
496
214
|
try {
|
|
@@ -527,7 +245,6 @@ async function superviseWant(want, liveConnection, nextConnection, forget) {
|
|
|
527
245
|
return;
|
|
528
246
|
}
|
|
529
247
|
const started = await liveConnection(scope);
|
|
530
|
-
// A want replaced while waiting must not open anything: its replacement may already hold an overlapping root.
|
|
531
248
|
if (!started || scope.aborted)
|
|
532
249
|
return;
|
|
533
250
|
want.settledTarget = undefined;
|
|
@@ -627,7 +344,6 @@ async function backoff(want, connection, delayMs, trackEnded = false) {
|
|
|
627
344
|
// The attempt itself reports a discovery it cannot open.
|
|
628
345
|
}
|
|
629
346
|
}
|
|
630
|
-
// Released in the turn the want is cancelled, as in an attempt, so a replacement can claim an overlapping root.
|
|
631
347
|
if (held)
|
|
632
348
|
want.controller.signal.addEventListener('abort', held.release, { once: true });
|
|
633
349
|
// Only a newer generation wakes the wait. A discovery that fails again does not: the next attempt reports it after
|
|
@@ -685,7 +401,7 @@ function holdWake(want, connection) {
|
|
|
685
401
|
return watch;
|
|
686
402
|
}
|
|
687
403
|
catch {
|
|
688
|
-
//
|
|
404
|
+
// The connection is closing, or its discovery cannot open a request.
|
|
689
405
|
return undefined;
|
|
690
406
|
}
|
|
691
407
|
}
|
|
@@ -706,7 +422,6 @@ async function attempt(want, connection, nextConnection) {
|
|
|
706
422
|
if (options.generations) {
|
|
707
423
|
try {
|
|
708
424
|
({ watch, release } = rootWatch(connection, options.namespace));
|
|
709
|
-
// Released in the turn the want is cancelled, so a replacement want can claim an overlapping root at once.
|
|
710
425
|
scope.addEventListener('abort', release, { once: true });
|
|
711
426
|
}
|
|
712
427
|
catch (error) {
|
|
@@ -752,6 +467,8 @@ function atOrBefore(object, latest) {
|
|
|
752
467
|
return false;
|
|
753
468
|
return object.groupId < latest.groupId || (object.groupId === latest.groupId && object.objectId <= latest.objectId);
|
|
754
469
|
}
|
|
470
|
+
/** How many subgroup ends a slot remembers reporting: far more than a replacement repeats from the latest group. */
|
|
471
|
+
const REMEMBERED_ENDS = 256;
|
|
755
472
|
/** The later of two locations, either of which may be unknown. */
|
|
756
473
|
function later(a, b) {
|
|
757
474
|
if (a === undefined)
|
|
@@ -773,8 +490,9 @@ function targetKey(namespace, track, parameters) {
|
|
|
773
490
|
* The want's subscriptions from the first SUBSCRIBE on `connection` to whatever ends the attempt. A move (a newer
|
|
774
491
|
* generation, or a re-want with another track or parameters) is made before the break: the replacement opens pending
|
|
775
492
|
* while the active subscription keeps delivering, and takes over once one of its objects passes `until`. A rotation is
|
|
776
|
-
* a move to the next connection: with the target unchanged, the replacement takes over at
|
|
777
|
-
* one had not
|
|
493
|
+
* a move to the next connection: with the target unchanged, the replacement takes over at its first object, or at its
|
|
494
|
+
* acceptance when the active one has delivered nothing. It delivers only what the active one had not, and no callback
|
|
495
|
+
* marks it.
|
|
778
496
|
*/
|
|
779
497
|
async function follow(want, connection, namespace, generation, scope, initialWatch, cancelledOrConnection, nextConnection) {
|
|
780
498
|
/** The followed root's discovery on the active subscription's connection. */
|
|
@@ -924,13 +642,18 @@ async function follow(want, connection, namespace, generation, scope, initialWat
|
|
|
924
642
|
if (on.signal.aborted)
|
|
925
643
|
closed();
|
|
926
644
|
};
|
|
927
|
-
|
|
645
|
+
/**
|
|
646
|
+
* `slot` takes over at `object`, which is delivered unless the old subscription delivered it (`repeated`). A
|
|
647
|
+
* rotation's replacement for a track that has delivered nothing takes over at its acceptance, with no object.
|
|
648
|
+
*/
|
|
649
|
+
const cutover = (slot, object, repeated = false) => {
|
|
928
650
|
const from = active;
|
|
929
651
|
// A rotation that keeps the target continues the same stream: nothing to mark, and no drain to repeat objects.
|
|
930
652
|
const silent = rotating(slot);
|
|
931
653
|
// Same stream, same numbering: what the old subscription delivered stays delivered, even arriving late here.
|
|
932
654
|
if (silent && !endedTargets.has(slot.key) && from.ended !== 'publisher-done') {
|
|
933
655
|
slot.skipThrough = later(from.latest, from.skipThrough);
|
|
656
|
+
slot.ends = from.ends;
|
|
934
657
|
}
|
|
935
658
|
endedTargets.clear();
|
|
936
659
|
pending = undefined;
|
|
@@ -957,10 +680,14 @@ async function follow(want, connection, namespace, generation, scope, initialWat
|
|
|
957
680
|
else
|
|
958
681
|
from.subscription.cancel();
|
|
959
682
|
releaseUnused();
|
|
960
|
-
|
|
961
|
-
|
|
683
|
+
if (object) {
|
|
684
|
+
slot.firstGroup = object.groupId;
|
|
685
|
+
// A replacement that starts behind the old subscription still drains through what that one delivered.
|
|
686
|
+
slot.largestGroup = Math.max(object.groupId, slot.skipThrough?.groupId ?? object.groupId);
|
|
687
|
+
}
|
|
962
688
|
lastObjectAt = performance.now();
|
|
963
689
|
if (!silent) {
|
|
690
|
+
want.switchMs = Math.round(lastObjectAt - slot.openedAt);
|
|
964
691
|
try {
|
|
965
692
|
handlers().onSwitched?.({
|
|
966
693
|
from: { namespace: [...from.namespace], track: from.track },
|
|
@@ -977,7 +704,7 @@ async function follow(want, connection, namespace, generation, scope, initialWat
|
|
|
977
704
|
}
|
|
978
705
|
// Later groups that reached the replacement early, out of order, follow the object that cut over. Each delivery
|
|
979
706
|
// is isolated, so one throwing callback loses neither the rest nor the held events.
|
|
980
|
-
const replay = heldAfter(slot.held, object);
|
|
707
|
+
const replay = object ? heldAfter(slot.held, object) : slot.held;
|
|
981
708
|
slot.held = [];
|
|
982
709
|
const deliver = (call) => {
|
|
983
710
|
try {
|
|
@@ -989,9 +716,11 @@ async function follow(want, connection, namespace, generation, scope, initialWat
|
|
|
989
716
|
}
|
|
990
717
|
};
|
|
991
718
|
// A handler may have moved the want again or cancelled it; the object still belongs to this cutover.
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
719
|
+
if (object && !repeated) {
|
|
720
|
+
delivered(slot, object);
|
|
721
|
+
if (!over() && active === slot)
|
|
722
|
+
deliver(() => handlers().onObject?.(object));
|
|
723
|
+
}
|
|
995
724
|
for (const event of replay) {
|
|
996
725
|
if (over() || active !== slot)
|
|
997
726
|
break;
|
|
@@ -1004,11 +733,21 @@ async function follow(want, connection, namespace, generation, scope, initialWat
|
|
|
1004
733
|
deliver(() => handlers().onObject?.(event.value));
|
|
1005
734
|
}
|
|
1006
735
|
else if (event.type === 'subgroupEnd')
|
|
1007
|
-
deliver(() =>
|
|
736
|
+
deliver(() => onSubgroupEnd(slot, event.value));
|
|
1008
737
|
else
|
|
1009
738
|
deliver(() => handlers().onSubgroupReset?.(event.value));
|
|
1010
739
|
}
|
|
1011
740
|
};
|
|
741
|
+
/** A subgroup end of the active subscription, or of one draining: once, across a rotation's cutover. */
|
|
742
|
+
const onSubgroupEnd = (slot, info) => {
|
|
743
|
+
const key = `${info.groupId}:${info.subgroupId}`;
|
|
744
|
+
if (slot.ends.has(key))
|
|
745
|
+
return;
|
|
746
|
+
slot.ends.add(key);
|
|
747
|
+
if (slot.ends.size > REMEMBERED_ENDS)
|
|
748
|
+
slot.ends.delete(slot.ends.values().next().value);
|
|
749
|
+
handlers().onSubgroupEnd?.(info);
|
|
750
|
+
};
|
|
1012
751
|
const onObject = (slot, object) => {
|
|
1013
752
|
if (!slot || over())
|
|
1014
753
|
return;
|
|
@@ -1029,8 +768,9 @@ async function follow(want, connection, namespace, generation, scope, initialWat
|
|
|
1029
768
|
return;
|
|
1030
769
|
}
|
|
1031
770
|
if (draining.has(slot)) {
|
|
1032
|
-
if (object.groupId <= slot.drainThrough)
|
|
771
|
+
if (object.groupId <= slot.drainThrough && !atOrBefore(object, slot.skipThrough)) {
|
|
1033
772
|
handlers().onObject?.(object);
|
|
773
|
+
}
|
|
1034
774
|
return;
|
|
1035
775
|
}
|
|
1036
776
|
if (slot !== pending)
|
|
@@ -1043,16 +783,21 @@ async function follow(want, connection, namespace, generation, scope, initialWat
|
|
|
1043
783
|
if (over() || pending !== slot)
|
|
1044
784
|
return;
|
|
1045
785
|
}
|
|
1046
|
-
// A rotation's replacement starts at the latest group, which the active subscription may have delivered
|
|
1047
|
-
//
|
|
786
|
+
// A rotation's replacement starts at the latest group, which the active subscription may have delivered. Its first
|
|
787
|
+
// object shows the same stream flowing on the new connection, so it cuts over there, and a repeated object is not
|
|
788
|
+
// delivered again. Waiting for a new object instead would hold the old connection for as long as it delivers each
|
|
789
|
+
// one first. Not once the publisher ended or the subscription was ended by it: a restarted publisher numbers from
|
|
790
|
+
// 0 again.
|
|
1048
791
|
const rotation = rotating(slot);
|
|
1049
792
|
// Only PUBLISH_DONE says the publisher may number from 0 again; a stale, terminated or disconnected subscription
|
|
1050
793
|
// does not.
|
|
1051
794
|
const continues = !endedTargets.has(slot.key) && active.ended !== 'publisher-done';
|
|
1052
|
-
if (rotation && continues && atOrBefore(object, active.latest))
|
|
795
|
+
if (rotation && continues && atOrBefore(object, later(active.latest, active.skipThrough))) {
|
|
796
|
+
cutover(slot, object, true);
|
|
1053
797
|
return;
|
|
798
|
+
}
|
|
1054
799
|
// With the active subscription gone there is nothing left to compare with: the next object cuts over. A rotation's
|
|
1055
|
-
// replacement continues the same stream, so its first
|
|
800
|
+
// replacement continues the same stream, so its first object does too.
|
|
1056
801
|
let passed = true;
|
|
1057
802
|
if (!active.ended && !rotation) {
|
|
1058
803
|
try {
|
|
@@ -1106,6 +851,13 @@ async function follow(want, connection, namespace, generation, scope, initialWat
|
|
|
1106
851
|
if (!slot || over())
|
|
1107
852
|
return;
|
|
1108
853
|
slot.accepted = info;
|
|
854
|
+
// A rotation's replacement for a track that has delivered nothing has no first object to wait for, and
|
|
855
|
+
// waiting would hold the old connection for the whole overlap. It takes over once accepted: the relay
|
|
856
|
+
// sends the track's first object on both subscriptions (measured on ord, #524).
|
|
857
|
+
if (slot === pending && rotating(slot) && !active.latest && !active.skipThrough) {
|
|
858
|
+
cutover(slot);
|
|
859
|
+
return;
|
|
860
|
+
}
|
|
1109
861
|
if (slot !== active)
|
|
1110
862
|
return;
|
|
1111
863
|
// Accepted: the watch that waited for the namespace to be announced again is done with.
|
|
@@ -1117,8 +869,8 @@ async function follow(want, connection, namespace, generation, scope, initialWat
|
|
|
1117
869
|
onSubgroupEnd: (info) => {
|
|
1118
870
|
if (slot && slot === pending && !over())
|
|
1119
871
|
holdEvent(slot.held, { type: 'subgroupEnd', value: info });
|
|
1120
|
-
else if (reaches(slot, info.groupId))
|
|
1121
|
-
|
|
872
|
+
else if (slot && reaches(slot, info.groupId))
|
|
873
|
+
onSubgroupEnd(slot, info);
|
|
1122
874
|
},
|
|
1123
875
|
onSubgroupReset: (info) => {
|
|
1124
876
|
if (slot && slot === pending && !over())
|
|
@@ -1150,7 +902,9 @@ async function follow(want, connection, namespace, generation, scope, initialWat
|
|
|
1150
902
|
connection: on,
|
|
1151
903
|
subscription,
|
|
1152
904
|
progressed: false,
|
|
905
|
+
ends: new Set(),
|
|
1153
906
|
held: [],
|
|
907
|
+
openedAt: performance.now(),
|
|
1154
908
|
};
|
|
1155
909
|
slot = opened;
|
|
1156
910
|
holdOn(on);
|
|
@@ -1341,14 +1095,16 @@ async function follow(want, connection, namespace, generation, scope, initialWat
|
|
|
1341
1095
|
* or a REQUEST_ERROR a wait may fix is retried with backoff on a fresh request, and anything else is surfaced.
|
|
1342
1096
|
*/
|
|
1343
1097
|
function discoveryFailed(error, scope, cancelledOrConnection, accepted) {
|
|
1344
|
-
if (scope.aborted
|
|
1098
|
+
if (scope.aborted)
|
|
1345
1099
|
return cancelledOrConnection(accepted);
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1100
|
+
switch (discoveryEnding(error)) {
|
|
1101
|
+
case 'connection':
|
|
1102
|
+
return cancelledOrConnection(accepted);
|
|
1103
|
+
case 'retry':
|
|
1104
|
+
return { kind: 'retry', accepted, progressed: false, reason: 'generation' };
|
|
1105
|
+
case 'fatal':
|
|
1106
|
+
return { kind: 'fatal', error };
|
|
1107
|
+
}
|
|
1352
1108
|
}
|
|
1353
1109
|
/** What a subscription's own ending means for the want. */
|
|
1354
1110
|
function classify(end, accepted, progressed, cancelledOrConnection) {
|
|
@@ -1368,92 +1124,36 @@ function classify(end, accepted, progressed, cancelledOrConnection) {
|
|
|
1368
1124
|
return { kind: 'retry', accepted, progressed, reason: 'terminated' };
|
|
1369
1125
|
}
|
|
1370
1126
|
}
|
|
1371
|
-
/** Whether two namespace prefixes overlap field by field: equal, or one contains the other. */
|
|
1372
|
-
function overlaps(a, b) {
|
|
1373
|
-
const [shorter, longer] = a.length <= b.length ? [a, b] : [b, a];
|
|
1374
|
-
return shorter.every((field, i) => field === longer[i]);
|
|
1375
|
-
}
|
|
1376
|
-
/** The connection's watch for `root`, opened on first use and closed when its last user releases it. */
|
|
1377
1127
|
function rootWatch(connection, root) {
|
|
1378
|
-
const key = JSON.stringify(root);
|
|
1379
|
-
let entry = connection.roots.get(key);
|
|
1380
|
-
if (!entry) {
|
|
1381
|
-
// A watch kept for reuse but unused would hold its prefix and refuse an overlapping root: close it first.
|
|
1382
|
-
for (const [heldKey, held] of connection.roots) {
|
|
1383
|
-
if (held.users === 0 && overlaps(JSON.parse(heldKey), root)) {
|
|
1384
|
-
connection.roots.delete(heldKey);
|
|
1385
|
-
held.close();
|
|
1386
|
-
}
|
|
1387
|
-
}
|
|
1388
|
-
// A fixed namespace's watch only shortens a wait: it gives way to a root's discovery, and its wants back off.
|
|
1389
|
-
for (const [heldKey, held] of connection.wakes) {
|
|
1390
|
-
if (overlaps(JSON.parse(heldKey), root)) {
|
|
1391
|
-
connection.wakes.delete(heldKey);
|
|
1392
|
-
held.close();
|
|
1393
|
-
}
|
|
1394
|
-
}
|
|
1395
|
-
// An ended discovery leaves the map at once, so the next attempt asks the relay again.
|
|
1396
|
-
const forgetEnded = () => {
|
|
1397
|
-
if (connection.roots.get(key) === entry)
|
|
1398
|
-
connection.roots.delete(key);
|
|
1399
|
-
};
|
|
1400
|
-
entry = Object.assign(openRootWatch(connection.subscriber, root, forgetEnded), { users: 0 });
|
|
1401
|
-
connection.roots.set(key, entry);
|
|
1402
|
-
}
|
|
1403
|
-
const watch = entry;
|
|
1404
|
-
watch.users += 1;
|
|
1405
|
-
let released = false;
|
|
1406
|
-
return {
|
|
1407
|
-
watch,
|
|
1408
|
-
release() {
|
|
1409
|
-
if (released)
|
|
1410
|
-
return;
|
|
1411
|
-
released = true;
|
|
1412
|
-
watch.users -= 1;
|
|
1413
|
-
if (watch.users === 0) {
|
|
1414
|
-
// Kept a moment so a want that resubscribes at once reuses the snapshot instead of asking again.
|
|
1415
|
-
setTimeout(() => {
|
|
1416
|
-
if (watch.users === 0 && connection.roots.get(key) === watch) {
|
|
1417
|
-
connection.roots.delete(key);
|
|
1418
|
-
watch.close();
|
|
1419
|
-
}
|
|
1420
|
-
}, 5_000);
|
|
1421
|
-
}
|
|
1422
|
-
},
|
|
1423
|
-
};
|
|
1424
|
-
}
|
|
1425
|
-
function openRootWatch(subscriber, root, onEnd) {
|
|
1426
1128
|
const generations = new Set();
|
|
1427
1129
|
const listeners = new Set();
|
|
1428
|
-
let failure;
|
|
1429
|
-
// Throws PREFIX_OVERLAP synchronously when another followed root overlaps this one.
|
|
1430
|
-
const events = subscriber.namespaceEvents({ prefix: root });
|
|
1431
1130
|
const notify = () => {
|
|
1432
1131
|
for (const listener of [...listeners])
|
|
1433
1132
|
listener();
|
|
1434
1133
|
};
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
}
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1134
|
+
const generationOf = (namespace) => namespace.length === root.length + 1 ? namespace[root.length] : undefined;
|
|
1135
|
+
const watch = connection.discovery.watch(root, {
|
|
1136
|
+
onAnnounced: (namespace) => {
|
|
1137
|
+
const generation = generationOf(namespace);
|
|
1138
|
+
if (generation === undefined)
|
|
1139
|
+
return;
|
|
1140
|
+
generations.add(generation);
|
|
1141
|
+
notify();
|
|
1142
|
+
},
|
|
1143
|
+
onUnannounced: (namespace) => {
|
|
1144
|
+
const generation = generationOf(namespace);
|
|
1145
|
+
if (generation === undefined)
|
|
1146
|
+
return;
|
|
1147
|
+
generations.delete(generation);
|
|
1148
|
+
notify();
|
|
1149
|
+
},
|
|
1150
|
+
onEnded: notify,
|
|
1151
|
+
});
|
|
1152
|
+
for (const namespace of watch.namespaces()) {
|
|
1153
|
+
const generation = generationOf(namespace);
|
|
1154
|
+
if (generation !== undefined)
|
|
1155
|
+
generations.add(generation);
|
|
1156
|
+
}
|
|
1457
1157
|
const current = () => {
|
|
1458
1158
|
let newest;
|
|
1459
1159
|
for (const generation of generations)
|
|
@@ -1461,66 +1161,58 @@ function openRootWatch(subscriber, root, onEnd) {
|
|
|
1461
1161
|
newest = generation;
|
|
1462
1162
|
return newest;
|
|
1463
1163
|
};
|
|
1164
|
+
const failure = () => watch.ended;
|
|
1464
1165
|
return {
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1166
|
+
watch: {
|
|
1167
|
+
current,
|
|
1168
|
+
failure,
|
|
1169
|
+
async newest(scope) {
|
|
1170
|
+
for (;;) {
|
|
1171
|
+
const ended = failure();
|
|
1172
|
+
if (ended)
|
|
1173
|
+
return { kind: 'failed', error: ended.error };
|
|
1174
|
+
const found = current();
|
|
1175
|
+
if (found !== undefined)
|
|
1176
|
+
return { kind: 'found', generation: found };
|
|
1177
|
+
if (scope.aborted)
|
|
1178
|
+
return { kind: 'aborted' };
|
|
1179
|
+
await new Promise((resolve) => {
|
|
1180
|
+
const remove = () => {
|
|
1181
|
+
listeners.delete(wake);
|
|
1182
|
+
scope.removeEventListener('abort', wake);
|
|
1183
|
+
};
|
|
1184
|
+
const wake = () => {
|
|
1185
|
+
remove();
|
|
1186
|
+
resolve();
|
|
1187
|
+
};
|
|
1188
|
+
listeners.add(wake);
|
|
1189
|
+
scope.addEventListener('abort', wake, { once: true });
|
|
1190
|
+
});
|
|
1191
|
+
}
|
|
1192
|
+
},
|
|
1193
|
+
onChange(listener) {
|
|
1194
|
+
listeners.add(listener);
|
|
1195
|
+
return () => listeners.delete(listener);
|
|
1196
|
+
},
|
|
1496
1197
|
},
|
|
1198
|
+
release: () => watch.release(),
|
|
1497
1199
|
};
|
|
1498
1200
|
}
|
|
1499
|
-
/**
|
|
1201
|
+
/**
|
|
1202
|
+
* The connection's watch for the fixed `namespace`, opened on first use and closed a moment after its last user leaves.
|
|
1203
|
+
* It is served by the connection's discovery, so a root's discovery or a publishing-roots view over the namespace
|
|
1204
|
+
* serves it too.
|
|
1205
|
+
*/
|
|
1500
1206
|
function namespaceWake(connection, namespace) {
|
|
1501
1207
|
const key = JSON.stringify(namespace);
|
|
1502
1208
|
let entry = connection.wakes.get(key);
|
|
1503
1209
|
if (!entry) {
|
|
1504
|
-
// A watch or a root's discovery kept for reuse but unused would hold an overlapping prefix and refuse this one.
|
|
1505
|
-
for (const [heldKey, held] of connection.wakes) {
|
|
1506
|
-
if (held.users === 0 && overlaps(JSON.parse(heldKey), namespace)) {
|
|
1507
|
-
connection.wakes.delete(heldKey);
|
|
1508
|
-
clearTimeout(held.linger);
|
|
1509
|
-
held.close();
|
|
1510
|
-
}
|
|
1511
|
-
}
|
|
1512
|
-
for (const [heldKey, held] of connection.roots) {
|
|
1513
|
-
if (held.users === 0 && overlaps(JSON.parse(heldKey), namespace)) {
|
|
1514
|
-
connection.roots.delete(heldKey);
|
|
1515
|
-
held.close();
|
|
1516
|
-
}
|
|
1517
|
-
}
|
|
1518
1210
|
const forgetEnded = () => {
|
|
1519
1211
|
if (connection.wakes.get(key) === entry)
|
|
1520
1212
|
connection.wakes.delete(key);
|
|
1521
1213
|
};
|
|
1522
|
-
// Throws
|
|
1523
|
-
entry = Object.assign(openNamespaceWake(connection.
|
|
1214
|
+
// Throws when the connection's discovery is closed or cannot open a request.
|
|
1215
|
+
entry = Object.assign(openNamespaceWake(connection.discovery, namespace, forgetEnded), { users: 0 });
|
|
1524
1216
|
connection.wakes.set(key, entry);
|
|
1525
1217
|
}
|
|
1526
1218
|
const watch = entry;
|
|
@@ -1544,50 +1236,55 @@ function namespaceWake(connection, namespace) {
|
|
|
1544
1236
|
connection.wakes.delete(key);
|
|
1545
1237
|
watch.close();
|
|
1546
1238
|
}
|
|
1547
|
-
},
|
|
1239
|
+
}, DISCOVERY_LINGER_MS);
|
|
1548
1240
|
}
|
|
1549
1241
|
},
|
|
1550
1242
|
};
|
|
1551
1243
|
}
|
|
1552
|
-
function openNamespaceWake(
|
|
1244
|
+
function openNamespaceWake(discovery, namespace, onEnd) {
|
|
1553
1245
|
const listeners = new Set();
|
|
1554
1246
|
let seen = 0;
|
|
1555
1247
|
let announcedAt = 0;
|
|
1556
1248
|
let returnedAt = 0;
|
|
1557
1249
|
let withdrawn = false;
|
|
1558
1250
|
let ended = false;
|
|
1559
|
-
const events = subscriber.namespaceEvents({ prefix: namespace });
|
|
1560
1251
|
const notify = () => {
|
|
1561
1252
|
for (const listener of [...listeners])
|
|
1562
1253
|
listener();
|
|
1563
1254
|
};
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1255
|
+
// Only the namespace itself; a deeper one is another publisher's.
|
|
1256
|
+
const isNamespace = (shown) => shown.length === namespace.length;
|
|
1257
|
+
const watch = discovery.watch(namespace, {
|
|
1258
|
+
onAnnounced(shown) {
|
|
1259
|
+
if (!isNamespace(shown))
|
|
1260
|
+
return;
|
|
1261
|
+
seen += 1;
|
|
1262
|
+
announcedAt = seen;
|
|
1263
|
+
if (withdrawn)
|
|
1264
|
+
returnedAt = seen;
|
|
1265
|
+
notify();
|
|
1266
|
+
},
|
|
1267
|
+
onUnannounced(shown) {
|
|
1268
|
+
if (!isNamespace(shown))
|
|
1269
|
+
return;
|
|
1270
|
+
seen += 1;
|
|
1271
|
+
announcedAt = 0;
|
|
1272
|
+
returnedAt = 0;
|
|
1273
|
+
withdrawn = true;
|
|
1274
|
+
notify();
|
|
1275
|
+
},
|
|
1276
|
+
onEnded() {
|
|
1585
1277
|
// Refused or ended with the connection: the wants waiting on it back off.
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
})
|
|
1278
|
+
ended = true;
|
|
1279
|
+
onEnd();
|
|
1280
|
+
notify();
|
|
1281
|
+
},
|
|
1282
|
+
});
|
|
1283
|
+
// A request already open over the namespace shows it without an event: count that as a fresh request's first one.
|
|
1284
|
+
if (watch.namespaces().some(isNamespace)) {
|
|
1285
|
+
seen = 1;
|
|
1286
|
+
announcedAt = 1;
|
|
1287
|
+
}
|
|
1591
1288
|
return {
|
|
1592
1289
|
seen: () => seen,
|
|
1593
1290
|
announcedAt: () => announcedAt,
|
|
@@ -1598,7 +1295,8 @@ function openNamespaceWake(subscriber, namespace, onEnd) {
|
|
|
1598
1295
|
return () => listeners.delete(listener);
|
|
1599
1296
|
},
|
|
1600
1297
|
close() {
|
|
1601
|
-
|
|
1298
|
+
ended = true;
|
|
1299
|
+
watch.release();
|
|
1602
1300
|
},
|
|
1603
1301
|
};
|
|
1604
1302
|
}
|