@moq/net 0.4.0 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/announced.d.ts +14 -0
- package/announced.d.ts.map +1 -1
- package/announced.js.map +1 -1
- package/broadcast.d.ts +27 -10
- package/broadcast.d.ts.map +1 -1
- package/broadcast.js +47 -21
- package/broadcast.js.map +1 -1
- package/connection/accept.js +14 -4
- package/connection/accept.js.map +1 -1
- package/connection/connect.d.ts.map +1 -1
- package/connection/connect.js +14 -4
- package/connection/connect.js.map +1 -1
- package/connection/established.d.ts +2 -1
- package/connection/established.d.ts.map +1 -1
- package/connection/established.js.map +1 -1
- package/connection/forward.js +61 -47
- package/connection/forward.js.map +1 -1
- package/connection/goaway.d.ts +34 -0
- package/connection/goaway.d.ts.map +1 -0
- package/connection/goaway.js +176 -0
- package/connection/goaway.js.map +1 -0
- package/connection/handshake.d.ts +5 -2
- package/connection/handshake.d.ts.map +1 -1
- package/connection/handshake.js +45 -10
- package/connection/handshake.js.map +1 -1
- package/connection/pool.d.ts +24 -11
- package/connection/pool.d.ts.map +1 -1
- package/connection/pool.js +50 -18
- package/connection/pool.js.map +1 -1
- package/connection/reload.d.ts.map +1 -1
- package/connection/reload.js +189 -35
- package/connection/reload.js.map +1 -1
- package/error.d.ts +11 -0
- package/error.d.ts.map +1 -1
- package/error.js +35 -0
- package/error.js.map +1 -1
- package/errors.d.ts +1 -1
- package/errors.d.ts.map +1 -1
- package/errors.js +1 -1
- package/errors.js.map +1 -1
- package/group.d.ts.map +1 -1
- package/group.js +5 -4
- package/group.js.map +1 -1
- package/hop.d.ts +8 -0
- package/hop.d.ts.map +1 -1
- package/hop.js +16 -0
- package/hop.js.map +1 -1
- package/ietf/adapter.d.ts +9 -1
- package/ietf/adapter.d.ts.map +1 -1
- package/ietf/adapter.js +46 -17
- package/ietf/adapter.js.map +1 -1
- package/ietf/cluster.d.ts +3 -3
- package/ietf/cluster.js +3 -3
- package/ietf/cluster.js.map +1 -1
- package/ietf/connection.d.ts +1 -1
- package/ietf/connection.d.ts.map +1 -1
- package/ietf/connection.js +118 -41
- package/ietf/connection.js.map +1 -1
- package/ietf/error.js +27 -2
- package/ietf/error.js.map +1 -1
- package/ietf/fetch.d.ts +7 -20
- package/ietf/fetch.d.ts.map +1 -1
- package/ietf/fetch.js +52 -22
- package/ietf/fetch.js.map +1 -1
- package/ietf/filter.d.ts +2 -0
- package/ietf/filter.d.ts.map +1 -1
- package/ietf/filter.js +4 -0
- package/ietf/filter.js.map +1 -1
- package/ietf/goaway.d.ts +9 -1
- package/ietf/goaway.d.ts.map +1 -1
- package/ietf/goaway.js +20 -0
- package/ietf/goaway.js.map +1 -1
- package/ietf/hidden.d.ts +2 -0
- package/ietf/hidden.d.ts.map +1 -0
- package/ietf/hidden.js +30 -0
- package/ietf/hidden.js.map +1 -0
- package/ietf/index.d.ts +2 -1
- package/ietf/index.d.ts.map +1 -1
- package/ietf/index.js +2 -1
- package/ietf/index.js.map +1 -1
- package/ietf/object.d.ts +15 -8
- package/ietf/object.d.ts.map +1 -1
- package/ietf/object.js +54 -36
- package/ietf/object.js.map +1 -1
- package/ietf/parameters.d.ts +12 -0
- package/ietf/parameters.d.ts.map +1 -1
- package/ietf/parameters.js +82 -12
- package/ietf/parameters.js.map +1 -1
- package/ietf/properties.d.ts +1 -0
- package/ietf/properties.d.ts.map +1 -1
- package/ietf/properties.js +14 -0
- package/ietf/properties.js.map +1 -1
- package/ietf/publish.d.ts +19 -2
- package/ietf/publish.d.ts.map +1 -1
- package/ietf/publish.js +40 -6
- package/ietf/publish.js.map +1 -1
- package/ietf/publish_namespace.d.ts +2 -2
- package/ietf/publish_namespace.js +2 -2
- package/ietf/publish_namespace.js.map +1 -1
- package/ietf/publisher.js +383 -214
- package/ietf/publisher.js.map +1 -1
- package/ietf/subscribe.d.ts +7 -1
- package/ietf/subscribe.d.ts.map +1 -1
- package/ietf/subscribe.js +25 -18
- package/ietf/subscribe.js.map +1 -1
- package/ietf/subscribe_namespace.d.ts +8 -2
- package/ietf/subscribe_namespace.d.ts.map +1 -1
- package/ietf/subscribe_namespace.js +20 -11
- package/ietf/subscribe_namespace.js.map +1 -1
- package/ietf/subscriber.js +226 -88
- package/ietf/subscriber.js.map +1 -1
- package/ietf/token.d.ts +2 -0
- package/ietf/token.d.ts.map +1 -0
- package/ietf/token.js +99 -0
- package/ietf/token.js.map +1 -0
- package/ietf/track.d.ts +4 -0
- package/ietf/track.d.ts.map +1 -1
- package/ietf/track.js +6 -20
- package/ietf/track.js.map +1 -1
- package/index.d.ts +1 -1
- package/index.d.ts.map +1 -1
- package/index.js.map +1 -1
- package/internal.d.ts +19 -3
- package/internal.d.ts.map +1 -1
- package/internal.js +43 -0
- package/internal.js.map +1 -1
- package/lite/announce.d.ts +46 -2
- package/lite/announce.d.ts.map +1 -1
- package/lite/announce.js +133 -15
- package/lite/announce.js.map +1 -1
- package/lite/connection.d.ts +1 -1
- package/lite/connection.d.ts.map +1 -1
- package/lite/connection.js +30 -9
- package/lite/connection.js.map +1 -1
- package/lite/datagram.d.ts +3 -2
- package/lite/datagram.d.ts.map +1 -1
- package/lite/datagram.js +7 -8
- package/lite/datagram.js.map +1 -1
- package/lite/group.d.ts +12 -8
- package/lite/group.d.ts.map +1 -1
- package/lite/group.js +37 -16
- package/lite/group.js.map +1 -1
- package/lite/message.d.ts +2 -2
- package/lite/message.d.ts.map +1 -1
- package/lite/message.js +14 -5
- package/lite/message.js.map +1 -1
- package/lite/publisher.js +174 -133
- package/lite/publisher.js.map +1 -1
- package/lite/setup.d.ts +1 -1
- package/lite/setup.d.ts.map +1 -1
- package/lite/setup.js +22 -19
- package/lite/setup.js.map +1 -1
- package/lite/subscribe.d.ts +10 -4
- package/lite/subscribe.d.ts.map +1 -1
- package/lite/subscribe.js +20 -10
- package/lite/subscribe.js.map +1 -1
- package/lite/subscriber.d.ts +10 -2
- package/lite/subscriber.d.ts.map +1 -1
- package/lite/subscriber.js +333 -188
- package/lite/subscriber.js.map +1 -1
- package/lite/version.d.ts +11 -0
- package/lite/version.d.ts.map +1 -1
- package/lite/version.js +56 -0
- package/lite/version.js.map +1 -1
- package/origin.d.ts +46 -19
- package/origin.d.ts.map +1 -1
- package/origin.js +529 -189
- package/origin.js.map +1 -1
- package/package.json +8 -4
- package/stream.d.ts +79 -14
- package/stream.d.ts.map +1 -1
- package/stream.js +375 -133
- package/stream.js.map +1 -1
- package/tail.d.ts +18 -0
- package/tail.d.ts.map +1 -0
- package/tail.js +167 -0
- package/tail.js.map +1 -0
- package/track.d.ts +46 -7
- package/track.d.ts.map +1 -1
- package/track.js +263 -101
- package/track.js.map +1 -1
- package/util/abort.d.ts +2 -0
- package/util/abort.d.ts.map +1 -0
- package/util/abort.js +20 -0
- package/util/abort.js.map +1 -0
- package/util/u64.d.ts +39 -0
- package/util/u64.d.ts.map +1 -0
- package/util/u64.js +83 -0
- package/util/u64.js.map +1 -0
- package/util/varint.d.ts +29 -0
- package/util/varint.d.ts.map +1 -0
- package/util/varint.js +198 -0
- package/util/varint.js.map +1 -0
- package/varint.d.ts +10 -6
- package/varint.d.ts.map +1 -1
- package/varint.js +40 -237
- package/varint.js.map +1 -1
- package/wire.d.ts +19 -3
- package/wire.d.ts.map +1 -1
- package/wire.js.map +1 -1
- package/ietf/control.d.ts +0 -135
- package/ietf/control.d.ts.map +0 -1
- package/ietf/control.js +0 -242
- package/ietf/control.js.map +0 -1
package/track.js
CHANGED
|
@@ -13,8 +13,13 @@ import { registerTrackConsumer } from "./wire.js";
|
|
|
13
13
|
// The largest delay setTimeout keeps: anything more truncates to a signed 32-bit int
|
|
14
14
|
// and fires right away.
|
|
15
15
|
const MAX_TIMEOUT_MS = 2 ** 31 - 1;
|
|
16
|
+
// The cache scans at most this many times per retention window.
|
|
17
|
+
const PRUNE_SLICES = 8;
|
|
16
18
|
/** Default {@link Info.maxAge} window (milliseconds) when the publisher does not set one. */
|
|
17
19
|
export const DEFAULT_MAX_AGE_MS = Milli(5000);
|
|
20
|
+
// The higher-first midpoint. IETF flips priority (lower first), so this goes out as 128, the
|
|
21
|
+
// draft's usual publisher priority, while moq-lite carries 127 as written: one urgency on both.
|
|
22
|
+
const DEFAULT_PRIORITY = 127;
|
|
18
23
|
/** Maximum buffered datagrams per subscriber; mirrors Rust's bounded send buffer. */
|
|
19
24
|
const MAX_DATAGRAMS = 64;
|
|
20
25
|
/**
|
|
@@ -50,7 +55,7 @@ export function infoDefaults(info = {}) {
|
|
|
50
55
|
return {
|
|
51
56
|
timescale: Timescale(info.timescale ?? Timescale.MILLI),
|
|
52
57
|
maxAge: maxAgeMillis(info.maxAge ?? DEFAULT_MAX_AGE_MS),
|
|
53
|
-
priority: priorityByte(info.priority ??
|
|
58
|
+
priority: priorityByte(info.priority ?? DEFAULT_PRIORITY),
|
|
54
59
|
};
|
|
55
60
|
}
|
|
56
61
|
// Materialize the defaults at the model boundary so every layer observes a complete
|
|
@@ -115,6 +120,7 @@ export class Request {
|
|
|
115
120
|
}
|
|
116
121
|
static {
|
|
117
122
|
hooks.makeRequest = (options) => new Request(options);
|
|
123
|
+
hooks.pendingTrackProducer = (request) => request.#producer;
|
|
118
124
|
}
|
|
119
125
|
/** The aggregate subscription requested for this track. */
|
|
120
126
|
get subscription() {
|
|
@@ -172,6 +178,33 @@ export class Consumer {
|
|
|
172
178
|
return this.#broadcast.fetchGroup(this.name, sequence, options);
|
|
173
179
|
}
|
|
174
180
|
}
|
|
181
|
+
// The index of the first timeline group at or after `sequence`.
|
|
182
|
+
function timelineIndex(timeline, sequence) {
|
|
183
|
+
let lo = 0;
|
|
184
|
+
let hi = timeline.length;
|
|
185
|
+
while (lo < hi) {
|
|
186
|
+
const mid = (lo + hi) >>> 1;
|
|
187
|
+
if (timeline[mid].sequence < sequence)
|
|
188
|
+
lo = mid + 1;
|
|
189
|
+
else
|
|
190
|
+
hi = mid;
|
|
191
|
+
}
|
|
192
|
+
return lo;
|
|
193
|
+
}
|
|
194
|
+
// Add a group to a sorted timeline, replacing any group with the same sequence. Groups
|
|
195
|
+
// usually arrive in order, so the append is checked first.
|
|
196
|
+
function timelineInsert(timeline, group) {
|
|
197
|
+
const last = timeline.at(-1);
|
|
198
|
+
if (!last || last.sequence < group.sequence) {
|
|
199
|
+
timeline.push(group);
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
const index = timelineIndex(timeline, group.sequence);
|
|
203
|
+
if (timeline[index]?.sequence === group.sequence)
|
|
204
|
+
timeline[index] = group;
|
|
205
|
+
else
|
|
206
|
+
timeline.splice(index, 0, group);
|
|
207
|
+
}
|
|
175
208
|
// The shared state behind a Producer / Subscriber pair. Package-internal
|
|
176
209
|
// wiring, unexported so it never appears in the published type declarations.
|
|
177
210
|
class TrackState {
|
|
@@ -179,9 +212,10 @@ class TrackState {
|
|
|
179
212
|
producer;
|
|
180
213
|
groups = new Signal([]);
|
|
181
214
|
// Every group still in the producer's replay cache, including groups this
|
|
182
|
-
// subscriber already consumed,
|
|
183
|
-
//
|
|
184
|
-
|
|
215
|
+
// subscriber already consumed, sorted by sequence. Drift anchors have the same
|
|
216
|
+
// lifetime as content. Sorted so the latency guard, evaluated per group and per
|
|
217
|
+
// arrival, searches it instead of scanning the whole retained window.
|
|
218
|
+
timeline = [];
|
|
185
219
|
// First timestamps mutate group state rather than track state, so held groups
|
|
186
220
|
// watch this revision as well as arrivals when enforcing latency after handoff.
|
|
187
221
|
timelineChanged = new Signal(0);
|
|
@@ -189,11 +223,12 @@ class TrackState {
|
|
|
189
223
|
datagrams = new Signal([]);
|
|
190
224
|
latest;
|
|
191
225
|
/**
|
|
192
|
-
* The exclusive final boundary,
|
|
193
|
-
* highest sequence produced. Groups and datagrams share the
|
|
194
|
-
* exceed `latest + 1` (which only tracks groups). Mirrors the
|
|
226
|
+
* The exclusive final boundary, declared by {@link Producer.finishAt} or stamped by a
|
|
227
|
+
* clean close as one past the highest sequence produced. Groups and datagrams share the
|
|
228
|
+
* namespace, so this can exceed `latest + 1` (which only tracks groups). Mirrors the
|
|
229
|
+
* Rust `final_sequence`.
|
|
195
230
|
*/
|
|
196
|
-
final;
|
|
231
|
+
final = new Signal(undefined);
|
|
197
232
|
closed = new Once();
|
|
198
233
|
update;
|
|
199
234
|
/** Resolved once the producer commits the immutable properties. */
|
|
@@ -265,15 +300,24 @@ export class Producer {
|
|
|
265
300
|
// read mirrored sinks, never this state directly.
|
|
266
301
|
#state = new TrackState();
|
|
267
302
|
#sequence = { next: 0 };
|
|
303
|
+
// One past the highest group or datagram this producer received, like the Rust
|
|
304
|
+
// `max_sequence`. The shared counter above can run ahead of it: sibling producers of
|
|
305
|
+
// the same track advance it too.
|
|
306
|
+
#received = 0;
|
|
268
307
|
// Recently written source groups, retained for replay to late subscribers and
|
|
269
308
|
// pruned once idle for longer than the cache window. Each entry tracks the mirror
|
|
270
309
|
// it handed to every sink so eviction can drop them too: otherwise a slow consumer
|
|
271
310
|
// that never reads would pin old groups (and their frame bytes) forever.
|
|
272
311
|
#cache = [];
|
|
312
|
+
// The same entries by sequence, so a write finds a duplicate without a scan.
|
|
313
|
+
#cached = new Map();
|
|
314
|
+
// When the cache was last scanned. See #prune.
|
|
315
|
+
#pruned = Number.NEGATIVE_INFINITY;
|
|
273
316
|
// Wakeup for the next entry due to age out. Writes settle retention inline, but a
|
|
274
317
|
// publisher that stalls stops writing, so without this an abandoned group (and any
|
|
275
318
|
// reader parked in it) would wait for a write that never comes.
|
|
276
319
|
#pruneTimer;
|
|
320
|
+
#pruneTimerAt = 0;
|
|
277
321
|
// One independent downstream state per live subscriber.
|
|
278
322
|
#sinks = new Set();
|
|
279
323
|
// Whether any subscriber is currently attached. Exposed as {@link used}; the consumer wire
|
|
@@ -295,13 +339,13 @@ export class Producer {
|
|
|
295
339
|
return resolveInfo(this.#state);
|
|
296
340
|
}
|
|
297
341
|
/**
|
|
298
|
-
* Publisher priority from the committed {@link Info}, or
|
|
342
|
+
* Publisher priority from the committed {@link Info}, or the default before {@link accept}.
|
|
299
343
|
*
|
|
300
344
|
* Higher is served first. Hang publishers set this from `Catalog.PRIORITY` so
|
|
301
345
|
* audio outranks video on the wire and in the bandwidth allocator.
|
|
302
346
|
*/
|
|
303
347
|
get priority() {
|
|
304
|
-
return this.#state.info.peek()?.priority ??
|
|
348
|
+
return this.#state.info.peek()?.priority ?? DEFAULT_PRIORITY;
|
|
305
349
|
}
|
|
306
350
|
/**
|
|
307
351
|
* Settles once the track closes: `null` on a clean close, or the abort {@link Error}.
|
|
@@ -385,6 +429,15 @@ export class Producer {
|
|
|
385
429
|
forward();
|
|
386
430
|
this.#sinks.delete(sink);
|
|
387
431
|
this.#updateSubscription();
|
|
432
|
+
// Update demand: once the last subscriber leaves, the consumer wire (watching
|
|
433
|
+
// {@link unused}) tears the upstream down instead of downloading to nobody.
|
|
434
|
+
this.#used.set(this.#sinks.size > 0);
|
|
435
|
+
// The producer closing every sink keeps its mirrors tracked, so what the sink
|
|
436
|
+
// still buffers ages out with the cache instead of staying pinned.
|
|
437
|
+
if (this.#state.closed.peek() !== undefined) {
|
|
438
|
+
dispose();
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
388
441
|
for (const entry of this.#cache) {
|
|
389
442
|
const mirror = entry.mirrors.get(sink);
|
|
390
443
|
if (mirror) {
|
|
@@ -395,18 +448,14 @@ export class Producer {
|
|
|
395
448
|
for (const group of sink.groups.peek())
|
|
396
449
|
group.close(abort);
|
|
397
450
|
dispose();
|
|
398
|
-
// Update demand: once the last subscriber leaves, the consumer wire (watching
|
|
399
|
-
// {@link unused}) tears the upstream down instead of downloading to nobody.
|
|
400
|
-
this.#used.set(this.#sinks.size > 0);
|
|
401
451
|
});
|
|
402
452
|
}
|
|
403
453
|
this.#prune();
|
|
404
454
|
for (const entry of this.#cache)
|
|
405
455
|
this.#mirror(entry, sink);
|
|
406
|
-
|
|
407
|
-
|
|
456
|
+
sink.final.set(this.#state.final.peek());
|
|
457
|
+
if (closed !== undefined)
|
|
408
458
|
closeTrackState(sink, closed instanceof Error ? closed : undefined);
|
|
409
|
-
}
|
|
410
459
|
}
|
|
411
460
|
// Recompute from every live sink because an update or close can narrow as well as widen
|
|
412
461
|
// the aggregate. The wire layer observes this signal and emits SUBSCRIBE_UPDATE.
|
|
@@ -423,7 +472,7 @@ export class Producer {
|
|
|
423
472
|
#mirror(entry, sink) {
|
|
424
473
|
const dst = entry.group.mirror();
|
|
425
474
|
entry.mirrors.set(sink, dst);
|
|
426
|
-
sink.timeline
|
|
475
|
+
timelineInsert(sink.timeline, dst);
|
|
427
476
|
void dst.readable().then(() => sink.timelineChanged.update((revision) => revision + 1));
|
|
428
477
|
sink.latest = Math.max(sink.latest ?? 0, dst.sequence);
|
|
429
478
|
sink.groups.mutate((groups) => {
|
|
@@ -439,19 +488,33 @@ export class Producer {
|
|
|
439
488
|
// terminal state.
|
|
440
489
|
if (!entry.group.isClosed)
|
|
441
490
|
entry.group.close(new TooFarBehind());
|
|
442
|
-
|
|
491
|
+
const mirrors = [...entry.mirrors.values()];
|
|
492
|
+
for (const mirror of mirrors)
|
|
443
493
|
hooks.evictGroup(mirror);
|
|
494
|
+
this.#unlink(entry);
|
|
495
|
+
for (const mirror of mirrors)
|
|
496
|
+
mirror.close();
|
|
497
|
+
}
|
|
498
|
+
// Take a cached group's mirrors out of every sink, so a subscriber can no longer
|
|
499
|
+
// receive them. A reader already holding one keeps it as is.
|
|
500
|
+
#unlink(entry) {
|
|
501
|
+
for (const [sink, mirror] of entry.mirrors) {
|
|
444
502
|
sink.groups.mutate((groups) => {
|
|
445
503
|
const i = groups.indexOf(mirror);
|
|
446
504
|
if (i >= 0)
|
|
447
505
|
groups.splice(i, 1);
|
|
448
506
|
});
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
507
|
+
const index = timelineIndex(sink.timeline, mirror.sequence);
|
|
508
|
+
if (sink.timeline[index] === mirror)
|
|
509
|
+
sink.timeline.splice(index, 1);
|
|
452
510
|
}
|
|
453
511
|
entry.mirrors.clear();
|
|
454
512
|
}
|
|
513
|
+
// Take a cached group out of the cache lookups.
|
|
514
|
+
#uncache(entry) {
|
|
515
|
+
this.#cache.splice(this.#cache.indexOf(entry), 1);
|
|
516
|
+
this.#cached.delete(entry.group.sequence);
|
|
517
|
+
}
|
|
455
518
|
// The one group retention never takes: the newest, while it is still open. That is
|
|
456
519
|
// the live edge a publisher is appending to, and a track may legitimately keep it
|
|
457
520
|
// open across a long quiet stretch (a catalog snapshot, a JSON stream). Every other
|
|
@@ -463,46 +526,57 @@ export class Producer {
|
|
|
463
526
|
// Evict cached groups idle for longer than the cache window. Idle means nothing
|
|
464
527
|
// written, so an abandoned open group ages out instead of pinning its buffer (and
|
|
465
528
|
// any reader parked in it) forever.
|
|
529
|
+
//
|
|
530
|
+
// Scans at most once per slice of the window, so a track publishing faster than that
|
|
531
|
+
// evicts a run of groups per scan instead of scanning everything to evict one per
|
|
532
|
+
// write. A group can outlive the window by up to one slice.
|
|
466
533
|
#prune() {
|
|
467
534
|
const maxAgeMs = this.#state.info.peek()?.maxAge ?? DEFAULT_MAX_AGE_MS;
|
|
468
|
-
const
|
|
535
|
+
const now = performance.now();
|
|
536
|
+
const slice = maxAgeMs / PRUNE_SLICES;
|
|
537
|
+
if (now < this.#pruned + slice) {
|
|
538
|
+
// Something may have come due since the last scan, so make sure another follows.
|
|
539
|
+
this.#wake(this.#pruned + slice);
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
this.#pruned = now;
|
|
543
|
+
const cutoff = now - maxAgeMs;
|
|
469
544
|
const live = this.#liveEdge();
|
|
545
|
+
let oldest;
|
|
470
546
|
const retained = [];
|
|
471
547
|
for (const entry of this.#cache) {
|
|
472
|
-
if (entry.group === live
|
|
548
|
+
if (entry.group === live) {
|
|
473
549
|
retained.push(entry);
|
|
474
|
-
continue;
|
|
475
550
|
}
|
|
476
|
-
|
|
551
|
+
else if (entry.group.activity >= cutoff) {
|
|
552
|
+
retained.push(entry);
|
|
553
|
+
if (oldest === undefined || entry.group.activity < oldest)
|
|
554
|
+
oldest = entry.group.activity;
|
|
555
|
+
}
|
|
556
|
+
else {
|
|
557
|
+
this.#cached.delete(entry.group.sequence);
|
|
558
|
+
this.#evict(entry);
|
|
559
|
+
}
|
|
477
560
|
}
|
|
478
561
|
this.#cache = retained;
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
// Writes settle retention inline, so this only has to cover the case no write
|
|
483
|
-
// follows. Cheap to over-arm: an entry written since is retained and re-armed.
|
|
484
|
-
#schedulePrune() {
|
|
562
|
+
// Replace the wakeup with one for the next entry due to age out. Writes settle
|
|
563
|
+
// retention inline, so this only has to cover the case no write follows. Cheap to
|
|
564
|
+
// over-arm: an entry written since is retained and re-armed.
|
|
485
565
|
clearTimeout(this.#pruneTimer);
|
|
486
566
|
this.#pruneTimer = undefined;
|
|
487
|
-
if (
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
if (entry.group === live)
|
|
495
|
-
continue;
|
|
496
|
-
if (oldest === undefined || entry.group.activity < oldest)
|
|
497
|
-
oldest = entry.group.activity;
|
|
498
|
-
}
|
|
499
|
-
if (oldest === undefined)
|
|
567
|
+
if (oldest !== undefined)
|
|
568
|
+
this.#wake(Math.max(oldest + maxAgeMs, now + slice));
|
|
569
|
+
}
|
|
570
|
+
// Arm the prune wakeup for `at`, unless one is already armed sooner. Kept after a close,
|
|
571
|
+
// until the cache empties, so what the track left behind still ages out.
|
|
572
|
+
#wake(at) {
|
|
573
|
+
if (this.#pruneTimer !== undefined && this.#pruneTimerAt <= at)
|
|
500
574
|
return;
|
|
501
|
-
|
|
575
|
+
clearTimeout(this.#pruneTimer);
|
|
502
576
|
// setTimeout truncates its delay to a signed 32-bit int, so a longer window
|
|
503
577
|
// would fire immediately and spin. Wake at the cap instead and re-arm: #prune
|
|
504
578
|
// retains anything still fresh, so the extra wakeups are the only cost.
|
|
505
|
-
const delay = Math.min(MAX_TIMEOUT_MS, Math.max(0,
|
|
579
|
+
const delay = Math.min(MAX_TIMEOUT_MS, Math.max(0, at - performance.now()));
|
|
506
580
|
const timer = setTimeout(() => {
|
|
507
581
|
this.#pruneTimer = undefined;
|
|
508
582
|
this.#prune();
|
|
@@ -510,22 +584,33 @@ export class Producer {
|
|
|
510
584
|
// A cache prune is never a reason to hold a Node/Bun process open.
|
|
511
585
|
timer.unref?.();
|
|
512
586
|
this.#pruneTimer = timer;
|
|
587
|
+
this.#pruneTimerAt = at;
|
|
513
588
|
}
|
|
514
589
|
// Retain a source group and fan it out to every live sink.
|
|
515
590
|
#publish(group) {
|
|
516
|
-
const entry = { group,
|
|
591
|
+
const entry = { group, mirrors: new Map() };
|
|
517
592
|
this.#cache.push(entry);
|
|
593
|
+
this.#cached.set(group.sequence, entry);
|
|
594
|
+
this.#received = Math.max(this.#received, group.sequence + 1);
|
|
518
595
|
for (const sink of this.#sinks)
|
|
519
596
|
this.#mirror(entry, sink);
|
|
520
597
|
// Give held mirrors the new live edge before pruning their timeline entry,
|
|
521
598
|
// so their latency guard can preserve a terminal expiry verdict.
|
|
522
599
|
this.#prune();
|
|
523
600
|
}
|
|
524
|
-
|
|
525
|
-
|
|
601
|
+
// Refuse a write once the track is closed, or at or past its declared end.
|
|
602
|
+
#writable(sequence) {
|
|
526
603
|
if (this.#state.closed.peek() !== undefined)
|
|
527
604
|
throw new Error("track is closed");
|
|
605
|
+
const final = this.#state.final.peek();
|
|
606
|
+
if (final !== undefined && sequence >= final) {
|
|
607
|
+
throw new Error(`sequence ${sequence} is at or past the track's end ${final}`);
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
/** Append a new group with the next sequence number. */
|
|
611
|
+
appendGroup() {
|
|
528
612
|
const sequence = this.#sequence;
|
|
613
|
+
this.#writable(sequence.next);
|
|
529
614
|
const group = new GroupProducer(sequence.next);
|
|
530
615
|
sequence.next = group.sequence + 1;
|
|
531
616
|
this.#publish(group);
|
|
@@ -540,16 +625,14 @@ export class Producer {
|
|
|
540
625
|
* entry is already gone, so a long-evicted sequence is accepted as new.
|
|
541
626
|
*/
|
|
542
627
|
writeGroup(group) {
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
const entry = this.#cache[existing];
|
|
548
|
-
if (!(entry.group.closed.peek() instanceof Error)) {
|
|
628
|
+
this.#writable(group.sequence);
|
|
629
|
+
const existing = this.#cached.get(group.sequence);
|
|
630
|
+
if (existing) {
|
|
631
|
+
if (!(existing.group.closed.peek() instanceof Error)) {
|
|
549
632
|
throw new Error(`duplicate group: sequence=${group.sequence}`);
|
|
550
633
|
}
|
|
551
|
-
this.#evict(
|
|
552
|
-
this.#
|
|
634
|
+
this.#evict(existing);
|
|
635
|
+
this.#uncache(existing);
|
|
553
636
|
}
|
|
554
637
|
// Only advance the shared counter upward (for appendGroup auto-increment).
|
|
555
638
|
const sequence = this.#sequence;
|
|
@@ -561,6 +644,7 @@ export class Producer {
|
|
|
561
644
|
// Fan a datagram out to every live subscriber, dropping the oldest once the ring is full.
|
|
562
645
|
// Late subscribers do NOT replay old datagrams (best-effort, unlike the group cache).
|
|
563
646
|
#publishDatagram(datagram) {
|
|
647
|
+
this.#received = Math.max(this.#received, datagram.sequence + 1);
|
|
564
648
|
for (const sink of this.#sinks) {
|
|
565
649
|
sink.datagrams.mutate((list) => {
|
|
566
650
|
if (list.length === MAX_DATAGRAMS)
|
|
@@ -582,12 +666,11 @@ export class Producer {
|
|
|
582
666
|
* relay preserving upstream numbering uses {@link insertDatagram}.
|
|
583
667
|
*/
|
|
584
668
|
appendDatagram(timestamp, payload) {
|
|
585
|
-
if (this.#state.closed.peek() !== undefined)
|
|
586
|
-
throw new Error("track is closed");
|
|
587
|
-
if (payload.byteLength > MAX_DATAGRAM_BYTES)
|
|
588
|
-
throw new Error("datagram payload too large");
|
|
589
669
|
const counter = this.#sequence;
|
|
590
670
|
const sequence = counter.next;
|
|
671
|
+
this.#writable(sequence);
|
|
672
|
+
if (payload.byteLength > MAX_DATAGRAM_BYTES)
|
|
673
|
+
throw new Error("datagram payload too large");
|
|
591
674
|
counter.next = sequence + 1;
|
|
592
675
|
this.#publishDatagram({ sequence, timestamp, payload });
|
|
593
676
|
return sequence;
|
|
@@ -600,8 +683,7 @@ export class Producer {
|
|
|
600
683
|
* apply. Most origin publishers want {@link appendDatagram} instead.
|
|
601
684
|
*/
|
|
602
685
|
insertDatagram(sequence, timestamp, payload) {
|
|
603
|
-
|
|
604
|
-
throw new Error("track is closed");
|
|
686
|
+
this.#writable(sequence);
|
|
605
687
|
if (payload.byteLength > MAX_DATAGRAM_BYTES)
|
|
606
688
|
throw new Error("datagram payload too large");
|
|
607
689
|
const counter = this.#sequence;
|
|
@@ -610,25 +692,77 @@ export class Producer {
|
|
|
610
692
|
}
|
|
611
693
|
this.#publishDatagram({ sequence, timestamp, payload });
|
|
612
694
|
}
|
|
613
|
-
/**
|
|
695
|
+
/**
|
|
696
|
+
* Declare the track's exclusive end, possibly ahead of the live edge, mirroring the Rust
|
|
697
|
+
* `finish_at`.
|
|
698
|
+
*
|
|
699
|
+
* `final` is the first sequence that will never be produced, so a track whose last group
|
|
700
|
+
* is 89 finishes at 90. Groups and datagrams below it are still accepted; anything at or
|
|
701
|
+
* above it is refused. Unlike {@link close} it is not terminal: call `close()` once the
|
|
702
|
+
* remaining groups are written. Throws if the track is closed, already has an end, or
|
|
703
|
+
* `final` is at or below a sequence already produced.
|
|
704
|
+
*/
|
|
705
|
+
finishAt(final) {
|
|
706
|
+
if (this.#state.closed.peek() !== undefined)
|
|
707
|
+
throw new Error("track is closed");
|
|
708
|
+
if (!Number.isSafeInteger(final) || final < 0)
|
|
709
|
+
throw new RangeError(`invalid track end: ${final}`);
|
|
710
|
+
const declared = this.#state.final.peek();
|
|
711
|
+
if (declared !== undefined)
|
|
712
|
+
throw new Error(`track already ends at ${declared}`);
|
|
713
|
+
if (final < this.#received) {
|
|
714
|
+
throw new Error(`track end ${final} is below the next sequence ${this.#received}`);
|
|
715
|
+
}
|
|
716
|
+
this.#declareFinal(final);
|
|
717
|
+
}
|
|
718
|
+
#declareFinal(final) {
|
|
719
|
+
this.#state.final.set(final);
|
|
720
|
+
for (const sink of this.#sinks)
|
|
721
|
+
sink.final.set(final);
|
|
722
|
+
}
|
|
723
|
+
/**
|
|
724
|
+
* Close the track and every subscriber, mirroring the abort to their groups. Idempotent.
|
|
725
|
+
*
|
|
726
|
+
* A clean close keeps the end {@link finishAt} declared, or declares one past the highest
|
|
727
|
+
* sequence produced; an abort ends without one. Subscribers still draining get the
|
|
728
|
+
* finished groups first, then the end or the abort. An abort after the declared end
|
|
729
|
+
* settled (reached, with every group below it finished) is a clean close. The groups
|
|
730
|
+
* left behind still age out after the track's `maxAge`, so a stale subscriber can't pin
|
|
731
|
+
* them.
|
|
732
|
+
*/
|
|
614
733
|
close(abort) {
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
734
|
+
if (this.#state.closed.peek() !== undefined)
|
|
735
|
+
return;
|
|
736
|
+
if (abort && this.#settled())
|
|
737
|
+
abort = undefined;
|
|
738
|
+
if (abort === undefined && this.#state.final.peek() === undefined) {
|
|
739
|
+
this.#declareFinal(this.#received);
|
|
620
740
|
}
|
|
741
|
+
// Nobody will finish these, so a subscriber that has not taken one yet never sees it.
|
|
742
|
+
// Not evicted: a reader already holding one keeps its frames and sees the abort.
|
|
743
|
+
const open = abort ? this.#cache.filter((entry) => entry.group.closed.peek() === undefined) : [];
|
|
621
744
|
closeTrackState(this.#state, abort);
|
|
622
|
-
clearTimeout(this.#pruneTimer);
|
|
623
|
-
this.#pruneTimer = undefined;
|
|
624
745
|
for (const { group } of this.#cache)
|
|
625
746
|
group.close(abort);
|
|
747
|
+
for (const entry of open) {
|
|
748
|
+
this.#unlink(entry);
|
|
749
|
+
this.#uncache(entry);
|
|
750
|
+
}
|
|
626
751
|
for (const sink of this.#sinks) {
|
|
627
752
|
for (const group of sink.groups.peek())
|
|
628
753
|
group.close(abort);
|
|
629
754
|
closeTrackState(sink, abort);
|
|
630
755
|
}
|
|
631
756
|
this.#sinks.clear();
|
|
757
|
+
this.#prune();
|
|
758
|
+
}
|
|
759
|
+
// Whether the declared end was reached and every cached group below it finished, so
|
|
760
|
+
// the track already holds everything it promised. Mirrors the Rust `is_settled`.
|
|
761
|
+
#settled() {
|
|
762
|
+
const final = this.#state.final.peek();
|
|
763
|
+
if (final === undefined || this.#received < final)
|
|
764
|
+
return false;
|
|
765
|
+
return this.#cache.every(({ group }) => group.sequence >= final || group.closed.peek() === null);
|
|
632
766
|
}
|
|
633
767
|
/** Append a frame as its own single-frame group. */
|
|
634
768
|
writeFrame(frame) {
|
|
@@ -679,18 +813,19 @@ export class Subscriber {
|
|
|
679
813
|
#frameGroup;
|
|
680
814
|
#drift() {
|
|
681
815
|
const { end } = this.#cursor.peek();
|
|
816
|
+
const timeline = this.#state.timeline;
|
|
682
817
|
let presentation;
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
818
|
+
// The edge wants the newest content that exists, so it takes the newest
|
|
819
|
+
// stamped group's latest frame: walk back from the cap to the first one.
|
|
820
|
+
for (let i = (end === undefined ? timeline.length : timelineIndex(timeline, end)) - 1; i >= 0; i--) {
|
|
821
|
+
const group = timeline[i];
|
|
686
822
|
if (group.closed.peek() instanceof Error)
|
|
687
823
|
continue;
|
|
688
|
-
// The edge wants the newest content that exists, so it takes the newest
|
|
689
|
-
// stamped group's latest frame.
|
|
690
824
|
const timestamp = hooks.groupTimestamp(group);
|
|
691
|
-
if (timestamp
|
|
692
|
-
|
|
693
|
-
}
|
|
825
|
+
if (timestamp === undefined)
|
|
826
|
+
continue;
|
|
827
|
+
presentation = { sequence: group.sequence, timestamp: hooks.groupLatest(group) ?? timestamp };
|
|
828
|
+
break;
|
|
694
829
|
}
|
|
695
830
|
const requested = this.#state.update.peek()?.maxAge ?? 0;
|
|
696
831
|
const retained = this.#state.info.peek()?.maxAge;
|
|
@@ -713,20 +848,16 @@ export class Subscriber {
|
|
|
713
848
|
// unstamped successor will begin, and shrinking the bound is the unsafe direction.
|
|
714
849
|
// An unstamped successor leaves the reach unbounded until it presents a frame.
|
|
715
850
|
#reach(sequence, end) {
|
|
716
|
-
|
|
717
|
-
for (
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
if (group.closed.peek() instanceof Error)
|
|
851
|
+
const timeline = this.#state.timeline;
|
|
852
|
+
for (let i = timelineIndex(timeline, sequence + 1); i < timeline.length; i++) {
|
|
853
|
+
const successor = timeline[i];
|
|
854
|
+
if (end !== undefined && successor.sequence >= end)
|
|
855
|
+
break;
|
|
856
|
+
if (successor.closed.peek() instanceof Error)
|
|
723
857
|
continue;
|
|
724
|
-
|
|
725
|
-
successor = group;
|
|
858
|
+
return hooks.groupTimestamp(successor)?.asMillis();
|
|
726
859
|
}
|
|
727
|
-
|
|
728
|
-
return undefined;
|
|
729
|
-
return hooks.groupTimestamp(successor)?.asMillis();
|
|
860
|
+
return undefined;
|
|
730
861
|
}
|
|
731
862
|
// Whether the drift budget says to give up on `group`.
|
|
732
863
|
//
|
|
@@ -745,8 +876,7 @@ export class Subscriber {
|
|
|
745
876
|
// falls out for free. Only timestamps drive expiry; wall-clock reclamation of idle
|
|
746
877
|
// content is the cache's own policy, not the budget's.
|
|
747
878
|
#isStale(group, drift) {
|
|
748
|
-
|
|
749
|
-
if (candidate?.group !== group)
|
|
879
|
+
if (this.#state.timeline[timelineIndex(this.#state.timeline, group.sequence)] !== group)
|
|
750
880
|
return false;
|
|
751
881
|
const reach = this.#reach(group.sequence, drift.end);
|
|
752
882
|
return (drift.presentation !== undefined &&
|
|
@@ -826,13 +956,35 @@ export class Subscriber {
|
|
|
826
956
|
return this.#state.latest;
|
|
827
957
|
}
|
|
828
958
|
/**
|
|
829
|
-
* The track's exclusive final boundary
|
|
830
|
-
*
|
|
831
|
-
*
|
|
832
|
-
* `latest() + 1`. Undefined
|
|
959
|
+
* The track's exclusive final boundary: the end {@link Producer.finishAt} declared, which
|
|
960
|
+
* can be ahead of the live edge, or one past the highest sequence produced once the
|
|
961
|
+
* producer closes cleanly (0 for a track that produced none). Groups and datagrams share
|
|
962
|
+
* the sequence namespace, so this can exceed `latest() + 1`. Undefined until declared,
|
|
963
|
+
* and after an abort that declared none.
|
|
833
964
|
*/
|
|
834
965
|
final() {
|
|
835
|
-
return this.#state.final;
|
|
966
|
+
return this.#state.final.peek();
|
|
967
|
+
}
|
|
968
|
+
/**
|
|
969
|
+
* Resolve with the track's exclusive final boundary once it is known, mirroring the Rust
|
|
970
|
+
* `finished`.
|
|
971
|
+
*
|
|
972
|
+
* Resolves as soon as the end is declared, which may be ahead of the live edge, so it
|
|
973
|
+
* says nothing about every group having arrived: read until the cursor returns
|
|
974
|
+
* `undefined` for that. Rejects with the abort, or if the track closes without an end.
|
|
975
|
+
*/
|
|
976
|
+
async finished() {
|
|
977
|
+
for (;;) {
|
|
978
|
+
const final = this.#state.final.peek();
|
|
979
|
+
if (final !== undefined)
|
|
980
|
+
return final;
|
|
981
|
+
const closed = this.#state.closed.peek();
|
|
982
|
+
if (closed instanceof Error)
|
|
983
|
+
throw closed;
|
|
984
|
+
if (closed !== undefined)
|
|
985
|
+
throw new Error("track closed before its end was known");
|
|
986
|
+
await Signal.race(this.#state.final, this.#state.closed);
|
|
987
|
+
}
|
|
836
988
|
}
|
|
837
989
|
/**
|
|
838
990
|
* The newest frame this track has produced, or `undefined` while it has none.
|
|
@@ -911,7 +1063,7 @@ export class Subscriber {
|
|
|
911
1063
|
});
|
|
912
1064
|
this.#frameGroup?.close(abort);
|
|
913
1065
|
this.#frameGroup = undefined;
|
|
914
|
-
this.#state.timeline.
|
|
1066
|
+
this.#state.timeline.length = 0;
|
|
915
1067
|
}
|
|
916
1068
|
/**
|
|
917
1069
|
* Receive every group on this track exactly once, as it becomes available.
|
|
@@ -980,9 +1132,15 @@ export class Subscriber {
|
|
|
980
1132
|
return group ? { kind: "boundary" } : { kind: "done" };
|
|
981
1133
|
}
|
|
982
1134
|
// Package-internal readiness half of recvGroup. Each registration fires at most once, and
|
|
983
|
-
// the caller disposes the losers after whichever source wakes it.
|
|
1135
|
+
// the caller disposes the losers after whichever source wakes it. A declared end wakes it
|
|
1136
|
+
// too, so a publisher can forward the end before the live edge reaches it.
|
|
984
1137
|
#groupChanged(fn) {
|
|
985
|
-
const dispose = [
|
|
1138
|
+
const dispose = [
|
|
1139
|
+
this.#state.groups.changed(fn),
|
|
1140
|
+
this.#cursor.changed(fn),
|
|
1141
|
+
this.#state.closed.changed(fn),
|
|
1142
|
+
this.#state.final.changed(fn),
|
|
1143
|
+
];
|
|
986
1144
|
return () => {
|
|
987
1145
|
for (const close of dispose)
|
|
988
1146
|
close();
|
|
@@ -1222,6 +1380,10 @@ export class Ordered {
|
|
|
1222
1380
|
final() {
|
|
1223
1381
|
return this.#subscriber.final();
|
|
1224
1382
|
}
|
|
1383
|
+
/** Resolve with the track's exclusive final boundary once known; see {@link Subscriber.finished}. */
|
|
1384
|
+
finished() {
|
|
1385
|
+
return this.#subscriber.finished();
|
|
1386
|
+
}
|
|
1225
1387
|
/** Limit subsequent reads to these groups and return this reader for chaining. */
|
|
1226
1388
|
withGroups(groups) {
|
|
1227
1389
|
this.setGroups(groups);
|