@caprid/relay 0.4.1 → 0.5.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 +4 -3
- package/dist/connect.d.ts +40 -5
- package/dist/connect.d.ts.map +1 -1
- package/dist/connect.js +19 -3
- package/dist/connect.js.map +1 -1
- 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-writer.d.ts +1 -2
- package/dist/frame-writer.d.ts.map +1 -1
- package/dist/frame-writer.js.map +1 -1
- package/dist/index.d.ts +7 -2
- 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.map +1 -1
- package/dist/supervised-publisher.js +42 -808
- package/dist/supervised-publisher.js.map +1 -1
- package/dist/supervised-relay.d.ts +112 -0
- package/dist/supervised-relay.d.ts.map +1 -0
- package/dist/supervised-relay.js +80 -0
- package/dist/supervised-relay.js.map +1 -0
- package/dist/supervised-subscriber.d.ts +66 -4
- package/dist/supervised-subscriber.d.ts.map +1 -1
- package/dist/supervised-subscriber.js +244 -557
- 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 +92 -0
- package/dist/supervisor.d.ts.map +1 -0
- package/dist/supervisor.js +720 -0
- package/dist/supervisor.js.map +1 -0
- package/dist/switchable.d.ts +1 -1
- 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 +7 -7
|
@@ -11,12 +11,14 @@
|
|
|
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 { DISCOVERY_LINGER_MS, discoveryEnding, refusedForGood } from './discovery.js';
|
|
18
|
+
import { openPublishingRoots, } from './publishing-roots.js';
|
|
19
|
+
import { SubscriptionRequestError, synchronousCallback } from './subscription.js';
|
|
20
|
+
import { backoffDelay, backoffOptions, isFreshNamespaceRejection, isPermanentRequestError, RECONNECT_BACKOFF, RESUBSCRIBE_BACKOFF, rotateBeforeOption, sleep, whenAborted, } from './supervision.js';
|
|
21
|
+
import { createSupervisor } from './supervisor.js';
|
|
20
22
|
import { assertDrainMs, heldAfter, holdEvent, untilPredicate, } from './switchable.js';
|
|
21
23
|
import { namespaceFields, trackName } from './validation.js';
|
|
22
24
|
/**
|
|
@@ -53,369 +55,74 @@ export async function superviseRelaySubscriber(connect, options) {
|
|
|
53
55
|
const backoff = backoffOptions('reconnect', reconnect, RECONNECT_BACKOFF);
|
|
54
56
|
const rotateBeforeMs = rotateBeforeOption(rotateBefore);
|
|
55
57
|
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);
|
|
58
|
+
const supervisor = createSupervisor(async (attempt) => {
|
|
59
|
+
const subscriber = await connect(attempt);
|
|
60
|
+
return {
|
|
61
|
+
subscriber,
|
|
62
|
+
closed: subscriber.closed,
|
|
63
|
+
get expiresAt() {
|
|
64
|
+
return subscriber.expiresAt;
|
|
65
|
+
},
|
|
66
|
+
close: () => subscriber.close(),
|
|
67
|
+
};
|
|
68
|
+
}, { what: 'Supervised subscriber', connection, signal, backoff, rotateBeforeMs, onStatus });
|
|
69
|
+
const wants = createWantSurface(supervisor);
|
|
70
|
+
await supervisor.start();
|
|
71
|
+
return {
|
|
72
|
+
get status() {
|
|
73
|
+
return supervisor.status;
|
|
74
|
+
},
|
|
75
|
+
closed: supervisor.closed,
|
|
76
|
+
...wants,
|
|
77
|
+
rotate() {
|
|
78
|
+
supervisor.rotate();
|
|
79
|
+
},
|
|
80
|
+
close() {
|
|
81
|
+
void supervisor.stop({});
|
|
82
|
+
},
|
|
88
83
|
};
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
attemptInProgress?.abort(reason);
|
|
100
|
-
// An abort while the first connection is still being made rejects the call that is waiting for it.
|
|
101
|
-
first?.reject(reason);
|
|
102
|
-
first = undefined;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Keep wants and publishing-roots views on `supervisor`'s connections: each moves with a rotation and comes back on
|
|
87
|
+
* the next connection. Everything is cancelled when the supervisor stops.
|
|
88
|
+
*/
|
|
89
|
+
export function createWantSurface(supervisor) {
|
|
90
|
+
const wants = new Map();
|
|
91
|
+
const views = new Set();
|
|
92
|
+
const liveConnection = (scope) => supervisor.liveConnection(scope);
|
|
93
|
+
supervisor.onStop(() => {
|
|
103
94
|
for (const want of wants.values())
|
|
104
95
|
want.controller.abort();
|
|
105
96
|
wants.clear();
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
retiring = undefined;
|
|
110
|
-
for (const wake of connectionWaiters.splice(0))
|
|
111
|
-
wake();
|
|
112
|
-
try {
|
|
113
|
-
setStatus('closed');
|
|
114
|
-
}
|
|
115
|
-
finally {
|
|
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
|
-
}
|
|
310
|
-
};
|
|
311
|
-
const run = async () => {
|
|
312
|
-
let attempt = 0;
|
|
313
|
-
/** A connection a rotation already opened and moved the wants to. */
|
|
314
|
-
let next;
|
|
315
|
-
while (!stopped) {
|
|
316
|
-
let opened = next;
|
|
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;
|
|
356
|
-
continue;
|
|
357
|
-
}
|
|
358
|
-
settleEnded(opened);
|
|
359
|
-
if (stopped)
|
|
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();
|
|
373
|
-
}
|
|
374
|
-
catch (error) {
|
|
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');
|
|
97
|
+
for (const view of [...views])
|
|
98
|
+
view.close();
|
|
99
|
+
});
|
|
391
100
|
const forget = (key, want) => {
|
|
392
101
|
want.controller.abort();
|
|
393
102
|
if (wants.get(key) === want)
|
|
394
103
|
wants.delete(key);
|
|
395
104
|
};
|
|
105
|
+
const guard = (handlers, what) => {
|
|
106
|
+
if (typeof handlers !== 'object' || handlers === null)
|
|
107
|
+
throw new TypeError(`${what} handlers must be an object`);
|
|
108
|
+
const guarded = {};
|
|
109
|
+
for (const [name, callback] of Object.entries(handlers)) {
|
|
110
|
+
if (callback === undefined)
|
|
111
|
+
continue;
|
|
112
|
+
if (typeof callback !== 'function')
|
|
113
|
+
throw new TypeError(`${name} must be a function`);
|
|
114
|
+
guarded[name] = synchronousCallback(name, callback, handlers);
|
|
115
|
+
}
|
|
116
|
+
return guarded;
|
|
117
|
+
};
|
|
396
118
|
return {
|
|
397
|
-
get status() {
|
|
398
|
-
return status;
|
|
399
|
-
},
|
|
400
|
-
closed,
|
|
401
119
|
want(key, wantOptions, handlers = {}) {
|
|
402
120
|
if (typeof key !== 'string')
|
|
403
121
|
throw new TypeError('want key must be a string');
|
|
404
122
|
if (typeof wantOptions !== 'object' || wantOptions === null) {
|
|
405
123
|
throw new TypeError('want takes an options object');
|
|
406
124
|
}
|
|
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;
|
|
125
|
+
const safeHandlers = guard(handlers, 'want');
|
|
419
126
|
const { parameters, generations = false, staleAfterMs, retry, until, drainMs = 0 } = wantOptions;
|
|
420
127
|
const namespace = namespaceFields(wantOptions.namespace);
|
|
421
128
|
if (generations)
|
|
@@ -426,8 +133,8 @@ export async function superviseRelaySubscriber(connect, options) {
|
|
|
426
133
|
const retryBackoff = backoffOptions('retry', retry, RESUBSCRIBE_BACKOFF);
|
|
427
134
|
const predicate = untilPredicate(until);
|
|
428
135
|
assertDrainMs(drainMs);
|
|
429
|
-
if (stopped)
|
|
430
|
-
throw
|
|
136
|
+
if (supervisor.stopped)
|
|
137
|
+
throw supervisor.closedError();
|
|
431
138
|
const source = JSON.stringify([namespace, generations]);
|
|
432
139
|
const target = targetKey([], track, parameters);
|
|
433
140
|
const existing = wants.get(key);
|
|
@@ -459,7 +166,7 @@ export async function superviseRelaySubscriber(connect, options) {
|
|
|
459
166
|
controller: new AbortController(),
|
|
460
167
|
};
|
|
461
168
|
wants.set(key, want);
|
|
462
|
-
void runWant(want, liveConnection,
|
|
169
|
+
void runWant(want, liveConnection, supervisor.current, () => forget(key, want));
|
|
463
170
|
},
|
|
464
171
|
unwant(key) {
|
|
465
172
|
const want = wants.get(key);
|
|
@@ -471,26 +178,28 @@ export async function superviseRelaySubscriber(connect, options) {
|
|
|
471
178
|
throw new TypeError('want key must be a string');
|
|
472
179
|
wants.get(key)?.catchUp?.();
|
|
473
180
|
},
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
181
|
+
publishingRoots(rootsOptions, handlers = {}) {
|
|
182
|
+
if (typeof rootsOptions !== 'object' || rootsOptions === null) {
|
|
183
|
+
throw new TypeError('publishingRoots takes an options object');
|
|
184
|
+
}
|
|
185
|
+
const safeHandlers = guard(handlers, 'publishingRoots');
|
|
186
|
+
const prefix = namespaceFields(rootsOptions.prefix);
|
|
187
|
+
namespaceFields([...prefix, 'x', 'x']);
|
|
188
|
+
if (supervisor.stopped)
|
|
189
|
+
throw supervisor.closedError();
|
|
190
|
+
const view = openPublishingRoots({
|
|
191
|
+
liveConnection,
|
|
192
|
+
current: supervisor.current,
|
|
193
|
+
hold,
|
|
194
|
+
onClosed: () => views.delete(view),
|
|
195
|
+
// The connection a rotation replaces sees the next one's announces, and the other way round: hide both.
|
|
196
|
+
hides: (namespace) => supervisor.ownsNamespace(namespace),
|
|
197
|
+
}, prefix, safeHandlers);
|
|
198
|
+
views.add(view);
|
|
199
|
+
return view;
|
|
480
200
|
},
|
|
481
201
|
};
|
|
482
202
|
}
|
|
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
203
|
/** Supervise `want`; a failure its loop did not expect forgets it rather than leave it registered but dead. */
|
|
495
204
|
async function runWant(want, liveConnection, nextConnection, forget) {
|
|
496
205
|
try {
|
|
@@ -527,7 +236,6 @@ async function superviseWant(want, liveConnection, nextConnection, forget) {
|
|
|
527
236
|
return;
|
|
528
237
|
}
|
|
529
238
|
const started = await liveConnection(scope);
|
|
530
|
-
// A want replaced while waiting must not open anything: its replacement may already hold an overlapping root.
|
|
531
239
|
if (!started || scope.aborted)
|
|
532
240
|
return;
|
|
533
241
|
want.settledTarget = undefined;
|
|
@@ -627,7 +335,6 @@ async function backoff(want, connection, delayMs, trackEnded = false) {
|
|
|
627
335
|
// The attempt itself reports a discovery it cannot open.
|
|
628
336
|
}
|
|
629
337
|
}
|
|
630
|
-
// Released in the turn the want is cancelled, as in an attempt, so a replacement can claim an overlapping root.
|
|
631
338
|
if (held)
|
|
632
339
|
want.controller.signal.addEventListener('abort', held.release, { once: true });
|
|
633
340
|
// Only a newer generation wakes the wait. A discovery that fails again does not: the next attempt reports it after
|
|
@@ -685,7 +392,7 @@ function holdWake(want, connection) {
|
|
|
685
392
|
return watch;
|
|
686
393
|
}
|
|
687
394
|
catch {
|
|
688
|
-
//
|
|
395
|
+
// The connection is closing, or its discovery cannot open a request.
|
|
689
396
|
return undefined;
|
|
690
397
|
}
|
|
691
398
|
}
|
|
@@ -706,7 +413,6 @@ async function attempt(want, connection, nextConnection) {
|
|
|
706
413
|
if (options.generations) {
|
|
707
414
|
try {
|
|
708
415
|
({ 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
416
|
scope.addEventListener('abort', release, { once: true });
|
|
711
417
|
}
|
|
712
418
|
catch (error) {
|
|
@@ -752,6 +458,8 @@ function atOrBefore(object, latest) {
|
|
|
752
458
|
return false;
|
|
753
459
|
return object.groupId < latest.groupId || (object.groupId === latest.groupId && object.objectId <= latest.objectId);
|
|
754
460
|
}
|
|
461
|
+
/** How many subgroup ends a slot remembers reporting: far more than a replacement repeats from the latest group. */
|
|
462
|
+
const REMEMBERED_ENDS = 256;
|
|
755
463
|
/** The later of two locations, either of which may be unknown. */
|
|
756
464
|
function later(a, b) {
|
|
757
465
|
if (a === undefined)
|
|
@@ -773,8 +481,9 @@ function targetKey(namespace, track, parameters) {
|
|
|
773
481
|
* The want's subscriptions from the first SUBSCRIBE on `connection` to whatever ends the attempt. A move (a newer
|
|
774
482
|
* generation, or a re-want with another track or parameters) is made before the break: the replacement opens pending
|
|
775
483
|
* 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
|
|
484
|
+
* a move to the next connection: with the target unchanged, the replacement takes over at its first object, or at its
|
|
485
|
+
* acceptance when the active one has delivered nothing. It delivers only what the active one had not, and no callback
|
|
486
|
+
* marks it.
|
|
778
487
|
*/
|
|
779
488
|
async function follow(want, connection, namespace, generation, scope, initialWatch, cancelledOrConnection, nextConnection) {
|
|
780
489
|
/** The followed root's discovery on the active subscription's connection. */
|
|
@@ -924,13 +633,18 @@ async function follow(want, connection, namespace, generation, scope, initialWat
|
|
|
924
633
|
if (on.signal.aborted)
|
|
925
634
|
closed();
|
|
926
635
|
};
|
|
927
|
-
|
|
636
|
+
/**
|
|
637
|
+
* `slot` takes over at `object`, which is delivered unless the old subscription delivered it (`repeated`). A
|
|
638
|
+
* rotation's replacement for a track that has delivered nothing takes over at its acceptance, with no object.
|
|
639
|
+
*/
|
|
640
|
+
const cutover = (slot, object, repeated = false) => {
|
|
928
641
|
const from = active;
|
|
929
642
|
// A rotation that keeps the target continues the same stream: nothing to mark, and no drain to repeat objects.
|
|
930
643
|
const silent = rotating(slot);
|
|
931
644
|
// Same stream, same numbering: what the old subscription delivered stays delivered, even arriving late here.
|
|
932
645
|
if (silent && !endedTargets.has(slot.key) && from.ended !== 'publisher-done') {
|
|
933
646
|
slot.skipThrough = later(from.latest, from.skipThrough);
|
|
647
|
+
slot.ends = from.ends;
|
|
934
648
|
}
|
|
935
649
|
endedTargets.clear();
|
|
936
650
|
pending = undefined;
|
|
@@ -957,8 +671,11 @@ async function follow(want, connection, namespace, generation, scope, initialWat
|
|
|
957
671
|
else
|
|
958
672
|
from.subscription.cancel();
|
|
959
673
|
releaseUnused();
|
|
960
|
-
|
|
961
|
-
|
|
674
|
+
if (object) {
|
|
675
|
+
slot.firstGroup = object.groupId;
|
|
676
|
+
// A replacement that starts behind the old subscription still drains through what that one delivered.
|
|
677
|
+
slot.largestGroup = Math.max(object.groupId, slot.skipThrough?.groupId ?? object.groupId);
|
|
678
|
+
}
|
|
962
679
|
lastObjectAt = performance.now();
|
|
963
680
|
if (!silent) {
|
|
964
681
|
try {
|
|
@@ -977,7 +694,7 @@ async function follow(want, connection, namespace, generation, scope, initialWat
|
|
|
977
694
|
}
|
|
978
695
|
// Later groups that reached the replacement early, out of order, follow the object that cut over. Each delivery
|
|
979
696
|
// is isolated, so one throwing callback loses neither the rest nor the held events.
|
|
980
|
-
const replay = heldAfter(slot.held, object);
|
|
697
|
+
const replay = object ? heldAfter(slot.held, object) : slot.held;
|
|
981
698
|
slot.held = [];
|
|
982
699
|
const deliver = (call) => {
|
|
983
700
|
try {
|
|
@@ -989,9 +706,11 @@ async function follow(want, connection, namespace, generation, scope, initialWat
|
|
|
989
706
|
}
|
|
990
707
|
};
|
|
991
708
|
// A handler may have moved the want again or cancelled it; the object still belongs to this cutover.
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
709
|
+
if (object && !repeated) {
|
|
710
|
+
delivered(slot, object);
|
|
711
|
+
if (!over() && active === slot)
|
|
712
|
+
deliver(() => handlers().onObject?.(object));
|
|
713
|
+
}
|
|
995
714
|
for (const event of replay) {
|
|
996
715
|
if (over() || active !== slot)
|
|
997
716
|
break;
|
|
@@ -1004,11 +723,21 @@ async function follow(want, connection, namespace, generation, scope, initialWat
|
|
|
1004
723
|
deliver(() => handlers().onObject?.(event.value));
|
|
1005
724
|
}
|
|
1006
725
|
else if (event.type === 'subgroupEnd')
|
|
1007
|
-
deliver(() =>
|
|
726
|
+
deliver(() => onSubgroupEnd(slot, event.value));
|
|
1008
727
|
else
|
|
1009
728
|
deliver(() => handlers().onSubgroupReset?.(event.value));
|
|
1010
729
|
}
|
|
1011
730
|
};
|
|
731
|
+
/** A subgroup end of the active subscription, or of one draining: once, across a rotation's cutover. */
|
|
732
|
+
const onSubgroupEnd = (slot, info) => {
|
|
733
|
+
const key = `${info.groupId}:${info.subgroupId}`;
|
|
734
|
+
if (slot.ends.has(key))
|
|
735
|
+
return;
|
|
736
|
+
slot.ends.add(key);
|
|
737
|
+
if (slot.ends.size > REMEMBERED_ENDS)
|
|
738
|
+
slot.ends.delete(slot.ends.values().next().value);
|
|
739
|
+
handlers().onSubgroupEnd?.(info);
|
|
740
|
+
};
|
|
1012
741
|
const onObject = (slot, object) => {
|
|
1013
742
|
if (!slot || over())
|
|
1014
743
|
return;
|
|
@@ -1029,8 +758,9 @@ async function follow(want, connection, namespace, generation, scope, initialWat
|
|
|
1029
758
|
return;
|
|
1030
759
|
}
|
|
1031
760
|
if (draining.has(slot)) {
|
|
1032
|
-
if (object.groupId <= slot.drainThrough)
|
|
761
|
+
if (object.groupId <= slot.drainThrough && !atOrBefore(object, slot.skipThrough)) {
|
|
1033
762
|
handlers().onObject?.(object);
|
|
763
|
+
}
|
|
1034
764
|
return;
|
|
1035
765
|
}
|
|
1036
766
|
if (slot !== pending)
|
|
@@ -1043,16 +773,21 @@ async function follow(want, connection, namespace, generation, scope, initialWat
|
|
|
1043
773
|
if (over() || pending !== slot)
|
|
1044
774
|
return;
|
|
1045
775
|
}
|
|
1046
|
-
// A rotation's replacement starts at the latest group, which the active subscription may have delivered
|
|
1047
|
-
//
|
|
776
|
+
// A rotation's replacement starts at the latest group, which the active subscription may have delivered. Its first
|
|
777
|
+
// object shows the same stream flowing on the new connection, so it cuts over there, and a repeated object is not
|
|
778
|
+
// delivered again. Waiting for a new object instead would hold the old connection for as long as it delivers each
|
|
779
|
+
// one first. Not once the publisher ended or the subscription was ended by it: a restarted publisher numbers from
|
|
780
|
+
// 0 again.
|
|
1048
781
|
const rotation = rotating(slot);
|
|
1049
782
|
// Only PUBLISH_DONE says the publisher may number from 0 again; a stale, terminated or disconnected subscription
|
|
1050
783
|
// does not.
|
|
1051
784
|
const continues = !endedTargets.has(slot.key) && active.ended !== 'publisher-done';
|
|
1052
|
-
if (rotation && continues && atOrBefore(object, active.latest))
|
|
785
|
+
if (rotation && continues && atOrBefore(object, later(active.latest, active.skipThrough))) {
|
|
786
|
+
cutover(slot, object, true);
|
|
1053
787
|
return;
|
|
788
|
+
}
|
|
1054
789
|
// 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
|
|
790
|
+
// replacement continues the same stream, so its first object does too.
|
|
1056
791
|
let passed = true;
|
|
1057
792
|
if (!active.ended && !rotation) {
|
|
1058
793
|
try {
|
|
@@ -1106,6 +841,13 @@ async function follow(want, connection, namespace, generation, scope, initialWat
|
|
|
1106
841
|
if (!slot || over())
|
|
1107
842
|
return;
|
|
1108
843
|
slot.accepted = info;
|
|
844
|
+
// A rotation's replacement for a track that has delivered nothing has no first object to wait for, and
|
|
845
|
+
// waiting would hold the old connection for the whole overlap. It takes over once accepted: the relay
|
|
846
|
+
// sends the track's first object on both subscriptions (measured on ord, #524).
|
|
847
|
+
if (slot === pending && rotating(slot) && !active.latest && !active.skipThrough) {
|
|
848
|
+
cutover(slot);
|
|
849
|
+
return;
|
|
850
|
+
}
|
|
1109
851
|
if (slot !== active)
|
|
1110
852
|
return;
|
|
1111
853
|
// Accepted: the watch that waited for the namespace to be announced again is done with.
|
|
@@ -1117,8 +859,8 @@ async function follow(want, connection, namespace, generation, scope, initialWat
|
|
|
1117
859
|
onSubgroupEnd: (info) => {
|
|
1118
860
|
if (slot && slot === pending && !over())
|
|
1119
861
|
holdEvent(slot.held, { type: 'subgroupEnd', value: info });
|
|
1120
|
-
else if (reaches(slot, info.groupId))
|
|
1121
|
-
|
|
862
|
+
else if (slot && reaches(slot, info.groupId))
|
|
863
|
+
onSubgroupEnd(slot, info);
|
|
1122
864
|
},
|
|
1123
865
|
onSubgroupReset: (info) => {
|
|
1124
866
|
if (slot && slot === pending && !over())
|
|
@@ -1150,6 +892,7 @@ async function follow(want, connection, namespace, generation, scope, initialWat
|
|
|
1150
892
|
connection: on,
|
|
1151
893
|
subscription,
|
|
1152
894
|
progressed: false,
|
|
895
|
+
ends: new Set(),
|
|
1153
896
|
held: [],
|
|
1154
897
|
};
|
|
1155
898
|
slot = opened;
|
|
@@ -1341,14 +1084,16 @@ async function follow(want, connection, namespace, generation, scope, initialWat
|
|
|
1341
1084
|
* or a REQUEST_ERROR a wait may fix is retried with backoff on a fresh request, and anything else is surfaced.
|
|
1342
1085
|
*/
|
|
1343
1086
|
function discoveryFailed(error, scope, cancelledOrConnection, accepted) {
|
|
1344
|
-
if (scope.aborted
|
|
1087
|
+
if (scope.aborted)
|
|
1345
1088
|
return cancelledOrConnection(accepted);
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1089
|
+
switch (discoveryEnding(error)) {
|
|
1090
|
+
case 'connection':
|
|
1091
|
+
return cancelledOrConnection(accepted);
|
|
1092
|
+
case 'retry':
|
|
1093
|
+
return { kind: 'retry', accepted, progressed: false, reason: 'generation' };
|
|
1094
|
+
case 'fatal':
|
|
1095
|
+
return { kind: 'fatal', error };
|
|
1096
|
+
}
|
|
1352
1097
|
}
|
|
1353
1098
|
/** What a subscription's own ending means for the want. */
|
|
1354
1099
|
function classify(end, accepted, progressed, cancelledOrConnection) {
|
|
@@ -1368,92 +1113,36 @@ function classify(end, accepted, progressed, cancelledOrConnection) {
|
|
|
1368
1113
|
return { kind: 'retry', accepted, progressed, reason: 'terminated' };
|
|
1369
1114
|
}
|
|
1370
1115
|
}
|
|
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
1116
|
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
1117
|
const generations = new Set();
|
|
1427
1118
|
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
1119
|
const notify = () => {
|
|
1432
1120
|
for (const listener of [...listeners])
|
|
1433
1121
|
listener();
|
|
1434
1122
|
};
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
}
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1123
|
+
const generationOf = (namespace) => namespace.length === root.length + 1 ? namespace[root.length] : undefined;
|
|
1124
|
+
const watch = connection.discovery.watch(root, {
|
|
1125
|
+
onAnnounced: (namespace) => {
|
|
1126
|
+
const generation = generationOf(namespace);
|
|
1127
|
+
if (generation === undefined)
|
|
1128
|
+
return;
|
|
1129
|
+
generations.add(generation);
|
|
1130
|
+
notify();
|
|
1131
|
+
},
|
|
1132
|
+
onUnannounced: (namespace) => {
|
|
1133
|
+
const generation = generationOf(namespace);
|
|
1134
|
+
if (generation === undefined)
|
|
1135
|
+
return;
|
|
1136
|
+
generations.delete(generation);
|
|
1137
|
+
notify();
|
|
1138
|
+
},
|
|
1139
|
+
onEnded: notify,
|
|
1140
|
+
});
|
|
1141
|
+
for (const namespace of watch.namespaces()) {
|
|
1142
|
+
const generation = generationOf(namespace);
|
|
1143
|
+
if (generation !== undefined)
|
|
1144
|
+
generations.add(generation);
|
|
1145
|
+
}
|
|
1457
1146
|
const current = () => {
|
|
1458
1147
|
let newest;
|
|
1459
1148
|
for (const generation of generations)
|
|
@@ -1461,66 +1150,58 @@ function openRootWatch(subscriber, root, onEnd) {
|
|
|
1461
1150
|
newest = generation;
|
|
1462
1151
|
return newest;
|
|
1463
1152
|
};
|
|
1153
|
+
const failure = () => watch.ended;
|
|
1464
1154
|
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
|
-
|
|
1155
|
+
watch: {
|
|
1156
|
+
current,
|
|
1157
|
+
failure,
|
|
1158
|
+
async newest(scope) {
|
|
1159
|
+
for (;;) {
|
|
1160
|
+
const ended = failure();
|
|
1161
|
+
if (ended)
|
|
1162
|
+
return { kind: 'failed', error: ended.error };
|
|
1163
|
+
const found = current();
|
|
1164
|
+
if (found !== undefined)
|
|
1165
|
+
return { kind: 'found', generation: found };
|
|
1166
|
+
if (scope.aborted)
|
|
1167
|
+
return { kind: 'aborted' };
|
|
1168
|
+
await new Promise((resolve) => {
|
|
1169
|
+
const remove = () => {
|
|
1170
|
+
listeners.delete(wake);
|
|
1171
|
+
scope.removeEventListener('abort', wake);
|
|
1172
|
+
};
|
|
1173
|
+
const wake = () => {
|
|
1174
|
+
remove();
|
|
1175
|
+
resolve();
|
|
1176
|
+
};
|
|
1177
|
+
listeners.add(wake);
|
|
1178
|
+
scope.addEventListener('abort', wake, { once: true });
|
|
1179
|
+
});
|
|
1180
|
+
}
|
|
1181
|
+
},
|
|
1182
|
+
onChange(listener) {
|
|
1183
|
+
listeners.add(listener);
|
|
1184
|
+
return () => listeners.delete(listener);
|
|
1185
|
+
},
|
|
1496
1186
|
},
|
|
1187
|
+
release: () => watch.release(),
|
|
1497
1188
|
};
|
|
1498
1189
|
}
|
|
1499
|
-
/**
|
|
1190
|
+
/**
|
|
1191
|
+
* The connection's watch for the fixed `namespace`, opened on first use and closed a moment after its last user leaves.
|
|
1192
|
+
* It is served by the connection's discovery, so a root's discovery or a publishing-roots view over the namespace
|
|
1193
|
+
* serves it too.
|
|
1194
|
+
*/
|
|
1500
1195
|
function namespaceWake(connection, namespace) {
|
|
1501
1196
|
const key = JSON.stringify(namespace);
|
|
1502
1197
|
let entry = connection.wakes.get(key);
|
|
1503
1198
|
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
1199
|
const forgetEnded = () => {
|
|
1519
1200
|
if (connection.wakes.get(key) === entry)
|
|
1520
1201
|
connection.wakes.delete(key);
|
|
1521
1202
|
};
|
|
1522
|
-
// Throws
|
|
1523
|
-
entry = Object.assign(openNamespaceWake(connection.
|
|
1203
|
+
// Throws when the connection's discovery is closed or cannot open a request.
|
|
1204
|
+
entry = Object.assign(openNamespaceWake(connection.discovery, namespace, forgetEnded), { users: 0 });
|
|
1524
1205
|
connection.wakes.set(key, entry);
|
|
1525
1206
|
}
|
|
1526
1207
|
const watch = entry;
|
|
@@ -1544,50 +1225,55 @@ function namespaceWake(connection, namespace) {
|
|
|
1544
1225
|
connection.wakes.delete(key);
|
|
1545
1226
|
watch.close();
|
|
1546
1227
|
}
|
|
1547
|
-
},
|
|
1228
|
+
}, DISCOVERY_LINGER_MS);
|
|
1548
1229
|
}
|
|
1549
1230
|
},
|
|
1550
1231
|
};
|
|
1551
1232
|
}
|
|
1552
|
-
function openNamespaceWake(
|
|
1233
|
+
function openNamespaceWake(discovery, namespace, onEnd) {
|
|
1553
1234
|
const listeners = new Set();
|
|
1554
1235
|
let seen = 0;
|
|
1555
1236
|
let announcedAt = 0;
|
|
1556
1237
|
let returnedAt = 0;
|
|
1557
1238
|
let withdrawn = false;
|
|
1558
1239
|
let ended = false;
|
|
1559
|
-
const events = subscriber.namespaceEvents({ prefix: namespace });
|
|
1560
1240
|
const notify = () => {
|
|
1561
1241
|
for (const listener of [...listeners])
|
|
1562
1242
|
listener();
|
|
1563
1243
|
};
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1244
|
+
// Only the namespace itself; a deeper one is another publisher's.
|
|
1245
|
+
const isNamespace = (shown) => shown.length === namespace.length;
|
|
1246
|
+
const watch = discovery.watch(namespace, {
|
|
1247
|
+
onAnnounced(shown) {
|
|
1248
|
+
if (!isNamespace(shown))
|
|
1249
|
+
return;
|
|
1250
|
+
seen += 1;
|
|
1251
|
+
announcedAt = seen;
|
|
1252
|
+
if (withdrawn)
|
|
1253
|
+
returnedAt = seen;
|
|
1254
|
+
notify();
|
|
1255
|
+
},
|
|
1256
|
+
onUnannounced(shown) {
|
|
1257
|
+
if (!isNamespace(shown))
|
|
1258
|
+
return;
|
|
1259
|
+
seen += 1;
|
|
1260
|
+
announcedAt = 0;
|
|
1261
|
+
returnedAt = 0;
|
|
1262
|
+
withdrawn = true;
|
|
1263
|
+
notify();
|
|
1264
|
+
},
|
|
1265
|
+
onEnded() {
|
|
1585
1266
|
// Refused or ended with the connection: the wants waiting on it back off.
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
})
|
|
1267
|
+
ended = true;
|
|
1268
|
+
onEnd();
|
|
1269
|
+
notify();
|
|
1270
|
+
},
|
|
1271
|
+
});
|
|
1272
|
+
// A request already open over the namespace shows it without an event: count that as a fresh request's first one.
|
|
1273
|
+
if (watch.namespaces().some(isNamespace)) {
|
|
1274
|
+
seen = 1;
|
|
1275
|
+
announcedAt = 1;
|
|
1276
|
+
}
|
|
1591
1277
|
return {
|
|
1592
1278
|
seen: () => seen,
|
|
1593
1279
|
announcedAt: () => announcedAt,
|
|
@@ -1598,7 +1284,8 @@ function openNamespaceWake(subscriber, namespace, onEnd) {
|
|
|
1598
1284
|
return () => listeners.delete(listener);
|
|
1599
1285
|
},
|
|
1600
1286
|
close() {
|
|
1601
|
-
|
|
1287
|
+
ended = true;
|
|
1288
|
+
watch.release();
|
|
1602
1289
|
},
|
|
1603
1290
|
};
|
|
1604
1291
|
}
|