@moq/net 0.4.1 → 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/broadcast.d.ts +21 -7
- package/broadcast.d.ts.map +1 -1
- package/broadcast.js +38 -16
- package/broadcast.js.map +1 -1
- package/connection/accept.js +6 -4
- package/connection/accept.js.map +1 -1
- package/connection/connect.d.ts.map +1 -1
- package/connection/connect.js +5 -4
- package/connection/connect.js.map +1 -1
- package/connection/forward.js +54 -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 +4 -2
- package/connection/handshake.d.ts.map +1 -1
- package/connection/handshake.js +42 -10
- package/connection/handshake.js.map +1 -1
- package/connection/pool.d.ts +16 -1
- package/connection/pool.d.ts.map +1 -1
- package/connection/pool.js +32 -10
- package/connection/pool.js.map +1 -1
- package/connection/reload.d.ts.map +1 -1
- package/connection/reload.js +184 -30
- package/connection/reload.js.map +1 -1
- package/error.d.ts +11 -0
- package/error.d.ts.map +1 -1
- package/error.js +14 -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 +4 -0
- package/ietf/adapter.d.ts.map +1 -1
- package/ietf/adapter.js +19 -8
- 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.map +1 -1
- package/ietf/connection.js +110 -37
- package/ietf/connection.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.js +1 -1
- package/ietf/hidden.js.map +1 -1
- package/ietf/index.d.ts +1 -1
- package/ietf/index.d.ts.map +1 -1
- package/ietf/index.js +1 -1
- package/ietf/index.js.map +1 -1
- package/ietf/object.d.ts +7 -6
- package/ietf/object.d.ts.map +1 -1
- package/ietf/object.js +37 -33
- package/ietf/object.js.map +1 -1
- package/ietf/parameters.d.ts +7 -0
- package/ietf/parameters.d.ts.map +1 -1
- package/ietf/parameters.js +62 -12
- package/ietf/parameters.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 +287 -172
- 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 +22 -14
- package/ietf/subscribe.js.map +1 -1
- package/ietf/subscribe_namespace.d.ts.map +1 -1
- package/ietf/subscribe_namespace.js +2 -3
- package/ietf/subscribe_namespace.js.map +1 -1
- package/ietf/subscriber.js +57 -42
- 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 +12 -3
- package/internal.d.ts.map +1 -1
- package/internal.js +32 -0
- package/internal.js.map +1 -1
- package/lite/announce.d.ts.map +1 -1
- package/lite/announce.js +7 -4
- package/lite/announce.js.map +1 -1
- package/lite/connection.d.ts.map +1 -1
- package/lite/connection.js +22 -7
- 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 +19 -37
- 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/subscriber.d.ts.map +1 -1
- package/lite/subscriber.js +206 -127
- package/lite/subscriber.js.map +1 -1
- package/origin.d.ts +9 -1
- package/origin.d.ts.map +1 -1
- package/origin.js +341 -146
- package/origin.js.map +1 -1
- package/package.json +2 -2
- package/stream.d.ts +77 -12
- package/stream.d.ts.map +1 -1
- package/stream.js +359 -122
- package/stream.js.map +1 -1
- package/tail.d.ts +7 -0
- package/tail.d.ts.map +1 -1
- package/tail.js +68 -22
- package/tail.js.map +1 -1
- package/track.d.ts +14 -3
- package/track.d.ts.map +1 -1
- package/track.js +170 -76
- 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 +17 -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/lite/subscriber.js
CHANGED
|
@@ -5,34 +5,32 @@ import * as broadcast from "../broadcast.js";
|
|
|
5
5
|
import { BroadcastCache } from "../consume.js";
|
|
6
6
|
import { controlTimeout, error, ProtocolViolation, reason, StreamCode, StreamError, sessionCause } from "../error.js";
|
|
7
7
|
import * as netGroup from "../group.js";
|
|
8
|
-
import { Cost, MAX_HOPS, routesEqual, UNKNOWN_HOP } from "../hop.js";
|
|
8
|
+
import { Cost, MAX_HOPS, randomHop, routesEqual, stampHops, UNKNOWN_HOP } from "../hop.js";
|
|
9
9
|
import { groupBounds, hiddenBelow, scopeCaptures, scopeHead, scopeOverlaps } from "../internal.js";
|
|
10
10
|
import * as Path from "../path.js";
|
|
11
11
|
import { Stream } from "../stream.js";
|
|
12
12
|
import { TAIL_GRACE_MS, Tail } from "../tail.js";
|
|
13
13
|
import * as Time from "../time.js";
|
|
14
|
+
import { untilAborted } from "../util/abort.js";
|
|
14
15
|
import { TimeoutError, withTimeout } from "../util/timeout.js";
|
|
15
16
|
import { overrideBroadcastWire, wireOf } from "../wire.js";
|
|
16
17
|
import { AnnounceHistory, AnnounceInit, AnnounceOk, AnnounceRequest, decodeAnnounceBroadcastMaybe, } from "./announce.js";
|
|
17
18
|
import { Datagram as DatagramMessage } from "./datagram.js";
|
|
18
19
|
import * as DatagramStream from "./datagram_stream.js";
|
|
19
20
|
import { Fetch as FetchMessage } from "./fetch.js";
|
|
21
|
+
import { frameDecoder, readFrames } from "./group.js";
|
|
20
22
|
import { sendOrder } from "./priority.js";
|
|
21
23
|
import { Probe } from "./probe.js";
|
|
22
24
|
import { ProbeLevel } from "./setup.js";
|
|
23
25
|
import { StreamId } from "./stream.js";
|
|
24
26
|
import { decodeSubscribeResponse, decodeSubscribeResponseMaybe, EMPTY_RANGE, emptyRange, exclusiveGroupEnd, inclusiveGroupEnd, Subscribe, SubscribeUpdate, } from "./subscribe.js";
|
|
25
27
|
import { TrackInfo, Track as TrackMessage } from "./track.js";
|
|
26
|
-
import { hasAnnounceId, hasAnnounceOk, hasDatagrams, hasProbeRtt, restartSupported, Version } from "./version.js";
|
|
28
|
+
import { hasAnnounceId, hasAnnounceOk, hasDatagrams, hasProbeRtt, hasStreamCount, restartSupported, Version, } from "./version.js";
|
|
27
29
|
// Bound on how long stream-open plus the first response (SUBSCRIBE_OK on older
|
|
28
30
|
// drafts, or TRACK_INFO on lite-05+) may take. Browsers cap concurrent QUIC streams
|
|
29
31
|
// (Chrome ~100) and we open with waitUntilAvailable, so past the cap the open blocks
|
|
30
32
|
// until the peer frees a slot. The timeout turns a stall into a clear error.
|
|
31
33
|
const SUBSCRIBE_SETUP_TIMEOUT_MS = 10_000;
|
|
32
|
-
/** Decode an unsigned zigzag varint back to a signed delta (mirrors Rust `VarInt::to_zigzag`). */
|
|
33
|
-
function unzigzag(v) {
|
|
34
|
-
return (v >> 1n) ^ -(v & 1n);
|
|
35
|
-
}
|
|
36
34
|
// The TRACK stream and implicit SUBSCRIBE acceptance are lite-05+.
|
|
37
35
|
function supportsTrackStream(version) {
|
|
38
36
|
switch (version) {
|
|
@@ -81,6 +79,9 @@ export class Subscriber {
|
|
|
81
79
|
#subscribeNext = 0n;
|
|
82
80
|
// Dedup consumed broadcasts per path: repeat consume() calls share one subscription.
|
|
83
81
|
#consumes = new BroadcastCache();
|
|
82
|
+
// A random Hop ID of this connection's own, written as the first hop of any chain that
|
|
83
|
+
// names no publisher, so a publisher that reconnects reads as a new one.
|
|
84
|
+
#stamp;
|
|
84
85
|
// Dedup in-flight one-shot fetches, keyed by [broadcast, track, sequence]. Concurrent (or
|
|
85
86
|
// repeat, while still open) fetchGroup() calls for the same group share one FETCH stream and
|
|
86
87
|
// each get an independent mirror; the entry is evicted once the group closes.
|
|
@@ -106,6 +107,7 @@ export class Subscriber {
|
|
|
106
107
|
this.#quic = quic;
|
|
107
108
|
this.version = version;
|
|
108
109
|
this.hop = hop;
|
|
110
|
+
this.#stamp = randomHop();
|
|
109
111
|
this.#probe = probe;
|
|
110
112
|
this.#peerSetup = peerSetup;
|
|
111
113
|
}
|
|
@@ -139,7 +141,7 @@ export class Subscriber {
|
|
|
139
141
|
// to reset the stream, not just close our side of it.
|
|
140
142
|
let stream;
|
|
141
143
|
try {
|
|
142
|
-
stream = await Stream.open(this.#quic);
|
|
144
|
+
stream = await Stream.open(this.#quic, { version: this.version });
|
|
143
145
|
}
|
|
144
146
|
catch (err) {
|
|
145
147
|
announced.close(error(err));
|
|
@@ -170,16 +172,16 @@ export class Subscriber {
|
|
|
170
172
|
// they go on record and obey the same one-per-path rule: the initial set
|
|
171
173
|
// naming a path twice is the same violation as two ANNOUNCE_STARTs for it,
|
|
172
174
|
// and the record is what catches either. Draft01/02 carry no hop ids and no
|
|
173
|
-
// ANNOUNCE_OK, so
|
|
175
|
+
// ANNOUNCE_OK, so this connection's stamp names the publisher.
|
|
174
176
|
for (const suffix of init.suffixes) {
|
|
175
177
|
const path = Path.join(prefix, suffix);
|
|
176
178
|
if (advertised.has(path)) {
|
|
177
179
|
throw new ProtocolViolation(`duplicate announce for ${path}`);
|
|
178
180
|
}
|
|
179
|
-
const route = { hops: [UNKNOWN_HOP], cost: Cost.zero };
|
|
181
|
+
const route = { hops: [this.#stamp, UNKNOWN_HOP], cost: Cost.zero };
|
|
180
182
|
const live = visible(path);
|
|
181
183
|
const captures = scopeCaptures(scope, path);
|
|
182
|
-
advertised.set(path, { publisher:
|
|
184
|
+
advertised.set(path, { publisher: this.#stamp, live, route, captures });
|
|
183
185
|
if (!live)
|
|
184
186
|
continue;
|
|
185
187
|
console.debug(`announced: broadcast=${path} active=true`);
|
|
@@ -301,22 +303,14 @@ export class Subscriber {
|
|
|
301
303
|
continue;
|
|
302
304
|
}
|
|
303
305
|
// The first hop identifies the original publisher; an empty chain means the
|
|
304
|
-
// peer itself originated it.
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
// own id, or a lite-03 UNKNOWN placeholder) never proves continuity: two such
|
|
308
|
-
// advertisements can be unrelated publishers. Mirrors the
|
|
309
|
-
// `publisher == Hop::UNKNOWN` arm of the Rust `restart_announce`.
|
|
310
|
-
const identified = publisher !== undefined && publisher !== UNKNOWN_HOP;
|
|
311
|
-
const fullHops = hops !== undefined && responderOrigin !== undefined
|
|
306
|
+
// peer itself originated it. One that names nobody (lite-01..03, or a peer
|
|
307
|
+
// reporting 0) gets this connection's stamp in front of its 0.
|
|
308
|
+
const fullHops = stampHops(hops !== undefined && responderOrigin !== undefined
|
|
312
309
|
? [...hops, responderOrigin]
|
|
313
|
-
: [...(hops ?? [])];
|
|
314
|
-
// A received empty list is the anonymous mark, not a local announcement.
|
|
315
|
-
if (fullHops.length === 0)
|
|
316
|
-
fullHops.push(UNKNOWN_HOP);
|
|
310
|
+
: [...(hops ?? [])], this.#stamp);
|
|
317
311
|
// Appending a withheld AnnounceOk(0) onto a 32-entry list is the same
|
|
318
312
|
// drop Rust's Hops::push makes: do not expose an overlong chain.
|
|
319
|
-
if (fullHops.length > MAX_HOPS) {
|
|
313
|
+
if (fullHops === undefined || fullHops.length > MAX_HOPS) {
|
|
320
314
|
console.debug(`announced: broadcast=${path} dropped (hop chain at MAX_HOPS)`);
|
|
321
315
|
advertised.set(path, {
|
|
322
316
|
publisher: undefined,
|
|
@@ -326,6 +320,7 @@ export class Subscriber {
|
|
|
326
320
|
});
|
|
327
321
|
continue;
|
|
328
322
|
}
|
|
323
|
+
const publisher = fullHops[0];
|
|
329
324
|
const route = { hops: fullHops, cost: cost ?? Cost.zero };
|
|
330
325
|
const captures = scopeCaptures(scope, path);
|
|
331
326
|
if (!visible(path)) {
|
|
@@ -333,29 +328,22 @@ export class Subscriber {
|
|
|
333
328
|
continue;
|
|
334
329
|
}
|
|
335
330
|
// A second advertisement for a path we already carry is a restart: either an
|
|
336
|
-
// explicit ANNOUNCE_UPDATE, or (lite-05) a duplicate ANNOUNCE.
|
|
331
|
+
// explicit ANNOUNCE_UPDATE, or (lite-05) a duplicate ANNOUNCE. It updates the
|
|
332
|
+
// route in place, so a forwarder re-prices without retracting.
|
|
337
333
|
const previous = advertised.get(path);
|
|
338
334
|
if (previous?.live) {
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
console.debug(`announced: broadcast=${path} rerouted`);
|
|
349
|
-
}
|
|
350
|
-
continue;
|
|
335
|
+
// A different publisher took the path. Subscriptions already open drain
|
|
336
|
+
// the old copy, but the next consume starts fresh rather than reusing the
|
|
337
|
+
// old publisher's cached track info.
|
|
338
|
+
if (previous.publisher !== publisher)
|
|
339
|
+
this.#consumes.evict(path);
|
|
340
|
+
advertised.set(path, { publisher, live: true, route, captures });
|
|
341
|
+
console.debug(`announced: broadcast=${path} rerouted`);
|
|
342
|
+
if (!routesEqual(previous.route, route)) {
|
|
343
|
+
announced.append({ prefix: path, captures, kind: "updated", route });
|
|
351
344
|
}
|
|
352
|
-
|
|
353
|
-
// subscriptions must not carry over. Surface a real end before the start.
|
|
354
|
-
retract();
|
|
345
|
+
continue;
|
|
355
346
|
}
|
|
356
|
-
// After `retract()`, which clears the entry: the path is advertised again, by
|
|
357
|
-
// whoever just took it over. Recording it before would leave nothing behind, so
|
|
358
|
-
// the *next* takeover would read as a first announcement and skip its own end.
|
|
359
347
|
advertised.set(path, { publisher, live: true, route, captures });
|
|
360
348
|
console.debug(`announced: broadcast=${path} active=true`);
|
|
361
349
|
announced.append({ prefix: path, captures, kind: "announced", route });
|
|
@@ -469,7 +457,7 @@ export class Subscriber {
|
|
|
469
457
|
closed.catch(() => { });
|
|
470
458
|
const subscriptionUpdates = this.version === Version.DRAFT_01 || this.version === Version.DRAFT_02
|
|
471
459
|
? undefined
|
|
472
|
-
: this.#runSubscriptionUpdates(id, broadcast,
|
|
460
|
+
: this.#runSubscriptionUpdates(id, broadcast, entry, msg, stream);
|
|
473
461
|
// Terminal conditions (stream end, track close, a failed subscription update) settle at most
|
|
474
462
|
// once; race them into one stable promise so the demand loop doesn't re-subscribe each pass.
|
|
475
463
|
// Updates stop quietly at the FIN, which can land before the responses ahead of it are
|
|
@@ -527,9 +515,22 @@ export class Subscriber {
|
|
|
527
515
|
drainOk = true;
|
|
528
516
|
}
|
|
529
517
|
// Register before opening SUBSCRIBE so a racing GROUP stream finds the entry.
|
|
530
|
-
const entry = {
|
|
518
|
+
const entry = {
|
|
519
|
+
track: producer,
|
|
520
|
+
timescale,
|
|
521
|
+
// The effective max age is the stopgap grace: the wrong clock (it bounds
|
|
522
|
+
// presentation-time drift), but it is how long the subscriber was willing to wait
|
|
523
|
+
// for a late group anyway. Already the smaller of the subscriber's and the track's.
|
|
524
|
+
tail: new Tail({
|
|
525
|
+
grace: () => {
|
|
526
|
+
const maxAge = producer.subscription.peek()?.maxAge ?? Time.Milli.zero;
|
|
527
|
+
return maxAge > 0 ? maxAge : TAIL_GRACE_MS;
|
|
528
|
+
},
|
|
529
|
+
}),
|
|
530
|
+
requested: msg.startGroup,
|
|
531
|
+
};
|
|
531
532
|
this.#subscribes.set(id, entry);
|
|
532
|
-
state.stream = await Stream.open(this.#quic);
|
|
533
|
+
state.stream = await Stream.open(this.#quic, { version: this.version });
|
|
533
534
|
await state.stream.writer.u53(StreamId.Subscribe);
|
|
534
535
|
await msg.encode(state.stream.writer, this.version);
|
|
535
536
|
if (drainOk) {
|
|
@@ -543,19 +544,35 @@ export class Subscriber {
|
|
|
543
544
|
}
|
|
544
545
|
// Opens a TRACK stream, reads the single TRACK_INFO, and FINs. Lite-05+ only.
|
|
545
546
|
async #trackInfo(broadcast, track) {
|
|
546
|
-
|
|
547
|
-
try {
|
|
547
|
+
return this.#exchange({ version: this.version }, async (stream) => {
|
|
548
548
|
await stream.writer.u53(StreamId.Track);
|
|
549
549
|
await new TrackMessage(broadcast, track).encode(stream.writer, this.version);
|
|
550
550
|
const info = await TrackInfo.decode(stream.reader, this.version);
|
|
551
551
|
// The publisher FINs after TRACK_INFO; FIN our side too.
|
|
552
552
|
stream.close();
|
|
553
553
|
return info;
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
// Opens a stream and runs a request/response exchange on it, resetting the stream if `run`
|
|
557
|
+
// fails. Subscriber.close() also resets it while `run` is pending, so a peer that never
|
|
558
|
+
// answers cannot hold it open, and a stream that opens after the close is reset at once.
|
|
559
|
+
async #exchange(options, run) {
|
|
560
|
+
const closed = this.#closed.signal;
|
|
561
|
+
closed.throwIfAborted();
|
|
562
|
+
const stream = await Stream.open(this.#quic, options);
|
|
563
|
+
const abort = () => stream.abort(error(closed.reason));
|
|
564
|
+
closed.addEventListener("abort", abort);
|
|
565
|
+
try {
|
|
566
|
+
closed.throwIfAborted();
|
|
567
|
+
return await run(stream);
|
|
554
568
|
}
|
|
555
569
|
catch (err) {
|
|
556
570
|
stream.abort(error(err));
|
|
557
571
|
throw err;
|
|
558
572
|
}
|
|
573
|
+
finally {
|
|
574
|
+
closed.removeEventListener("abort", abort);
|
|
575
|
+
}
|
|
559
576
|
}
|
|
560
577
|
// Map the wire TRACK_INFO onto the model track.Info a producer/consumer holds.
|
|
561
578
|
#toModelInfo(info) {
|
|
@@ -579,24 +596,32 @@ export class Subscriber {
|
|
|
579
596
|
// Open a FETCH stream for one group and stream its bare frames into a group, for the
|
|
580
597
|
// ConsumeBroadcast backing track.Consumer.fetchGroup() (lite-05+).
|
|
581
598
|
async fetchGroup(broadcast, track, sequence, options = {}) {
|
|
599
|
+
options.signal?.throwIfAborted();
|
|
582
600
|
// Coalesce onto a still-open fetch of the same group so we don't open a second FETCH
|
|
583
601
|
// stream (and re-download it); each caller reads an independent mirror.
|
|
602
|
+
//
|
|
603
|
+
// Reserve each caller's mirror before the fetch starts or is awaited: the fetch watches
|
|
604
|
+
// demand from the start, and a fast FIN cannot discard frames before these callers
|
|
605
|
+
// receive their handles. An abort closes only this caller's mirror, so the stream is
|
|
606
|
+
// cancelled once the last one leaves.
|
|
584
607
|
const key = JSON.stringify([broadcast, track, sequence]);
|
|
585
608
|
let entry = this.#fetches.get(key);
|
|
586
|
-
|
|
609
|
+
let consumer;
|
|
610
|
+
if (entry && !entry.group.isClosed) {
|
|
611
|
+
consumer = entry.group.mirror();
|
|
612
|
+
}
|
|
613
|
+
else {
|
|
587
614
|
const group = new netGroup.Producer(sequence);
|
|
588
|
-
|
|
615
|
+
consumer = group.mirror();
|
|
616
|
+
entry = { group, accepted: this.#runFetch(broadcast, track, sequence, options.priority ?? 0, group) };
|
|
589
617
|
this.#fetches.set(key, entry);
|
|
590
618
|
void group.closed.then(() => {
|
|
591
619
|
if (this.#fetches.get(key)?.group === group)
|
|
592
620
|
this.#fetches.delete(key);
|
|
593
621
|
});
|
|
594
622
|
}
|
|
595
|
-
// Reserve each caller's mirror before awaiting acceptance so the pump sees demand,
|
|
596
|
-
// and a fast FIN cannot discard frames before these callers receive their handles.
|
|
597
|
-
const consumer = entry.group.mirror();
|
|
598
623
|
try {
|
|
599
|
-
await entry.accepted;
|
|
624
|
+
await untilAborted(entry.accepted, options.signal);
|
|
600
625
|
return consumer;
|
|
601
626
|
}
|
|
602
627
|
catch (err) {
|
|
@@ -605,38 +630,51 @@ export class Subscriber {
|
|
|
605
630
|
}
|
|
606
631
|
}
|
|
607
632
|
// Open the FETCH stream and pump the response into the shared group. Setup errors close the
|
|
608
|
-
// group, evict the entry, and reject every caller waiting for acceptance.
|
|
609
|
-
|
|
633
|
+
// group, evict the entry, and reject every caller waiting for acceptance. A setup every caller
|
|
634
|
+
// has abandoned is cancelled the same way.
|
|
635
|
+
async #runFetch(broadcast, track, sequence, priority, group) {
|
|
610
636
|
try {
|
|
611
637
|
if (!supportsTrackStream(this.version)) {
|
|
612
638
|
throw new Error("fetch group requires moq-lite-05 or newer");
|
|
613
639
|
}
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
640
|
+
// Lite has no FETCH_OK, so a publisher that never answers would hold the setup forever.
|
|
641
|
+
// Subscriber.close() closing the group releases every caller at any stage, and resets
|
|
642
|
+
// the streams the setup opened.
|
|
643
|
+
const setup = this.#fetchSetup(broadcast, track, sequence, priority, group);
|
|
644
|
+
let accepted;
|
|
617
645
|
try {
|
|
618
|
-
await
|
|
619
|
-
await new FetchMessage({ broadcast, track, priority, group: sequence }).encode(stream.writer, this.version);
|
|
620
|
-
// A byte or an empty-group FIN accepts the fetch; a reset rejects it.
|
|
621
|
-
// done() buffers that byte so the response pump can decode it normally.
|
|
622
|
-
await stream.reader.done();
|
|
646
|
+
accepted = await untilAbandoned(group, setup);
|
|
623
647
|
}
|
|
624
648
|
catch (err) {
|
|
625
|
-
stream.
|
|
649
|
+
// A setup that finishes just after the close hands back a stream nobody will read.
|
|
650
|
+
void setup.then(({ stream }) => stream.abort(error(err)), () => void 0);
|
|
626
651
|
throw err;
|
|
627
652
|
}
|
|
628
|
-
void this.#runFetchResponse(stream, group, Time.Timescale(info.timescale));
|
|
653
|
+
void this.#runFetchResponse(accepted.stream, group, Time.Timescale(accepted.info.timescale));
|
|
629
654
|
}
|
|
630
655
|
catch (err) {
|
|
631
656
|
group.close(error(err));
|
|
632
657
|
throw err;
|
|
633
658
|
}
|
|
634
659
|
}
|
|
660
|
+
// Resolve the track's timescale, then open the FETCH stream and wait for it to be accepted.
|
|
661
|
+
// Closing the group during that wait resets the stream.
|
|
662
|
+
async #fetchSetup(broadcast, track, sequence, priority, group) {
|
|
663
|
+
const info = await untilClosed(group, this.#trackInfo(broadcast, track));
|
|
664
|
+
return this.#exchange({ sendOrder: sendOrder({ priority }), version: this.version }, async (stream) => {
|
|
665
|
+
await stream.writer.u53(StreamId.Fetch);
|
|
666
|
+
await new FetchMessage({ broadcast, track, priority, group: sequence }).encode(stream.writer, this.version);
|
|
667
|
+
// A byte or an empty-group FIN accepts the fetch; a reset rejects it.
|
|
668
|
+
// done() buffers that byte so the response pump can decode it normally.
|
|
669
|
+
await untilClosed(group, stream.reader.done());
|
|
670
|
+
return { stream, info };
|
|
671
|
+
});
|
|
672
|
+
}
|
|
635
673
|
// Read the FETCH response (bare zigzag-delta-timestamped frames) into the group, then
|
|
636
674
|
// FIN. A stream-level failure aborts the group so its reader observes the gap.
|
|
637
675
|
async #runFetchResponse(stream, group, timescale) {
|
|
638
676
|
try {
|
|
639
|
-
|
|
677
|
+
const decode = frameDecoder(timescale);
|
|
640
678
|
// Serve until the stream FINs, the group closes, or every reader leaves. A group can
|
|
641
679
|
// stay open indefinitely (a catalog or JSON stream), so an abandoned fetch is stopped by
|
|
642
680
|
// demand, not by the stream ending. `unused` is watched across frames as one stable
|
|
@@ -644,24 +682,31 @@ export class Subscriber {
|
|
|
644
682
|
// cancel re-arms and resumes.
|
|
645
683
|
const idle = Symbol("idle");
|
|
646
684
|
let unused = group.unused().then(() => idle);
|
|
685
|
+
// A decode consumes its frame whenever it lands, so one outstanding across a re-arm is
|
|
686
|
+
// kept and awaited again rather than abandoned with its frame.
|
|
687
|
+
let pending;
|
|
647
688
|
for (;;) {
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
689
|
+
// Buffered frames are written without an await, as in a group stream.
|
|
690
|
+
let frame = pending === undefined ? stream.reader.tryDecode(decode) : undefined;
|
|
691
|
+
if (!frame) {
|
|
692
|
+
pending ??= stream.reader.decodeMaybe(decode);
|
|
693
|
+
const next = await race([pending, group.closed, unused]);
|
|
694
|
+
if (next === idle) {
|
|
695
|
+
if (group.isClosed)
|
|
696
|
+
break;
|
|
697
|
+
if (group.used.peek()) {
|
|
698
|
+
unused = group.unused().then(() => idle);
|
|
699
|
+
continue;
|
|
700
|
+
}
|
|
701
|
+
// Abandoned mid-group: the truncated group must never end clean.
|
|
702
|
+
throw new StreamError(StreamCode.Cancel, { message: "cancel" });
|
|
653
703
|
}
|
|
654
|
-
|
|
704
|
+
pending = undefined;
|
|
705
|
+
if (!next || next instanceof Error)
|
|
706
|
+
break;
|
|
707
|
+
frame = next;
|
|
655
708
|
}
|
|
656
|
-
|
|
657
|
-
break;
|
|
658
|
-
prevTs += unzigzag(await stream.reader.u62());
|
|
659
|
-
const timestamp = new Time.Timestamp(Number(prevTs), timescale);
|
|
660
|
-
const size = await stream.reader.u53();
|
|
661
|
-
const payload = await stream.reader.read(size);
|
|
662
|
-
if (!payload)
|
|
663
|
-
break;
|
|
664
|
-
group.writeFrame({ payload, timestamp });
|
|
709
|
+
group.writeFrame(frame);
|
|
665
710
|
}
|
|
666
711
|
group.close();
|
|
667
712
|
stream.close();
|
|
@@ -674,20 +719,29 @@ export class Subscriber {
|
|
|
674
719
|
}
|
|
675
720
|
// Reads SUBSCRIBE_START/END/DROP on the subscribe stream until FIN (lite-05+), recording
|
|
676
721
|
// the range the tail is accounted against. SUBSCRIBE_END declares the track's end right
|
|
677
|
-
// away, so a consumer learns it before the last groups arrive.
|
|
678
|
-
//
|
|
722
|
+
// away, so a consumer learns it before the last groups arrive. The publisher must declare
|
|
723
|
+
// the end before FIN; resets and malformed responses reject with their failure.
|
|
679
724
|
async #runResponses(stream, entry) {
|
|
680
725
|
for (;;) {
|
|
681
726
|
const resp = await decodeSubscribeResponseMaybe(stream.reader, this.version);
|
|
682
|
-
if (!resp)
|
|
727
|
+
if (!resp) {
|
|
728
|
+
if (entry.end === undefined)
|
|
729
|
+
throw new ProtocolViolation("subscribe stream ended without SUBSCRIBE_END");
|
|
683
730
|
return;
|
|
731
|
+
}
|
|
684
732
|
if ("start" in resp) {
|
|
685
733
|
entry.start = resp.start.group;
|
|
734
|
+
// The groups the SUBSCRIBE asked for below it are unavailable, whatever the
|
|
735
|
+
// demand asks later.
|
|
736
|
+
if (entry.requested !== undefined)
|
|
737
|
+
entry.tail.account(entry.requested, entry.start);
|
|
686
738
|
}
|
|
687
739
|
else if ("end" in resp) {
|
|
688
740
|
if (entry.end !== undefined)
|
|
689
741
|
throw new ProtocolViolation("duplicate SUBSCRIBE_END");
|
|
690
742
|
entry.end = resp.end.group;
|
|
743
|
+
if (hasStreamCount(this.version))
|
|
744
|
+
entry.streams = resp.end.streams;
|
|
691
745
|
// A local close can win the race with the response; there is nothing left to end.
|
|
692
746
|
if (entry.track.closed.peek() !== undefined)
|
|
693
747
|
continue;
|
|
@@ -695,7 +749,13 @@ export class Subscriber {
|
|
|
695
749
|
entry.track.finishAt(entry.end);
|
|
696
750
|
}
|
|
697
751
|
catch (err) {
|
|
698
|
-
|
|
752
|
+
// lite-05 specified an inclusive end, and @moq/net 0.1.3 to 0.1.9 sent one, so
|
|
753
|
+
// there an end below a received group only costs the early boundary: the FIN
|
|
754
|
+
// still finishes the track. Later drafts made it exclusive.
|
|
755
|
+
if (this.version !== Version.DRAFT_05) {
|
|
756
|
+
throw new ProtocolViolation(`invalid SUBSCRIBE_END: ${reason(error(err))}`);
|
|
757
|
+
}
|
|
758
|
+
console.warn(`invalid SUBSCRIBE_END: ${reason(error(err))}`);
|
|
699
759
|
}
|
|
700
760
|
}
|
|
701
761
|
else if ("drop" in resp) {
|
|
@@ -705,31 +765,31 @@ export class Subscriber {
|
|
|
705
765
|
}
|
|
706
766
|
// Wait for the group streams the publisher still owes once it has ended the subscription.
|
|
707
767
|
//
|
|
708
|
-
//
|
|
709
|
-
//
|
|
710
|
-
//
|
|
711
|
-
//
|
|
712
|
-
// then end cleanly with them skipped like any stale group. That is a wall-clock stopgap for
|
|
713
|
-
// a presentation-time budget; a publisher sending SUBSCRIBE_DROP for every group it reset
|
|
714
|
-
// would account for them with no timer at all.
|
|
768
|
+
// lite-07 counts streams, so skipped sequences owe nothing. Older drafts account for
|
|
769
|
+
// the range using received headers and SUBSCRIBE_DROP. A counted stream reset before
|
|
770
|
+
// its header leaves no trace, so the grace still bounds that wait. Streams whose
|
|
771
|
+
// headers arrived keep reading until their own FIN or reset.
|
|
715
772
|
#settleTail(entry) {
|
|
716
773
|
const { tail, track } = entry;
|
|
717
|
-
// Already the smaller of the subscriber's and the track's max age.
|
|
718
|
-
const maxAge = track.subscription.peek()?.maxAge ?? Time.Milli.zero;
|
|
719
|
-
const grace = maxAge > 0 ? maxAge : TAIL_GRACE_MS;
|
|
720
774
|
const complete = () => {
|
|
775
|
+
if (entry.streams !== undefined)
|
|
776
|
+
return tail.streams >= entry.streams;
|
|
721
777
|
// Without SUBSCRIBE_END (older drafts) nothing says which groups are owed.
|
|
722
778
|
if (entry.end === undefined)
|
|
723
779
|
return false;
|
|
724
780
|
// Without SUBSCRIBE_START the publisher served no group at all.
|
|
725
781
|
if (entry.start === undefined)
|
|
726
782
|
return true;
|
|
727
|
-
|
|
728
|
-
|
|
783
|
+
// Owed from the floor the demand last asked for, which an update can move either
|
|
784
|
+
// way, or where SUBSCRIBE_START resolved a live-edge one. The groups the SUBSCRIBE
|
|
785
|
+
// asked for below its SUBSCRIBE_START were accounted for when it arrived.
|
|
786
|
+
const groups = track.subscription.peek()?.groups;
|
|
787
|
+
const bounds = groupBounds(groups ?? {});
|
|
788
|
+
const start = groups?.start === undefined ? entry.start : bounds.start;
|
|
729
789
|
const end = bounds.end === undefined ? entry.end : Math.min(entry.end, bounds.end);
|
|
730
790
|
return tail.covers(start, end);
|
|
731
791
|
};
|
|
732
|
-
return tail.settle(complete,
|
|
792
|
+
return tail.settle(complete, track.closed);
|
|
733
793
|
}
|
|
734
794
|
/**
|
|
735
795
|
* Send SUBSCRIBE_UPDATE messages whenever the track's aggregate subscription changes.
|
|
@@ -742,7 +802,8 @@ export class Subscriber {
|
|
|
742
802
|
* before SubscribeOk arrived (or between iterations, before .next() registered
|
|
743
803
|
* its listener) aren't lost.
|
|
744
804
|
*/
|
|
745
|
-
async #runSubscriptionUpdates(id, broadcast,
|
|
805
|
+
async #runSubscriptionUpdates(id, broadcast, entry, msg, stream) {
|
|
806
|
+
const track = entry.track;
|
|
746
807
|
const stopped = race([track.closed, stream.reader.closed]).then(() => null);
|
|
747
808
|
let lastSent = {
|
|
748
809
|
priority: msg.priority,
|
|
@@ -766,6 +827,11 @@ export class Subscriber {
|
|
|
766
827
|
const bounds = groupBounds(current.groups);
|
|
767
828
|
if (emptyRange({ startGroup: bounds.start, endGroup: bounds.end }))
|
|
768
829
|
throw new Error(EMPTY_RANGE);
|
|
830
|
+
// A lowered floor owes groups nobody asked for until now.
|
|
831
|
+
if (current.groups?.start !== undefined) {
|
|
832
|
+
const floor = lastSent.groups?.start === undefined ? entry.start : groupBounds(lastSent.groups).start;
|
|
833
|
+
entry.tail.demand(bounds.start, floor ?? Number.POSITIVE_INFINITY);
|
|
834
|
+
}
|
|
769
835
|
// Round-trip the other Subscribe parameters so the publisher doesn't
|
|
770
836
|
// interpret SUBSCRIBE_UPDATE as a reset of ordered/maxAge/etc.
|
|
771
837
|
const update = new SubscribeUpdate({
|
|
@@ -806,6 +872,14 @@ export class Subscriber {
|
|
|
806
872
|
const producer = new netGroup.Producer(group.sequence);
|
|
807
873
|
const read = tail.open(group.sequence);
|
|
808
874
|
try {
|
|
875
|
+
// The publisher contradicted its own end, which no later group can repair. lite-05
|
|
876
|
+
// specified an inclusive end, so its last group lands on it: the write below drops
|
|
877
|
+
// only that group there.
|
|
878
|
+
if (entry.end !== undefined && group.sequence >= entry.end && this.version !== Version.DRAFT_05) {
|
|
879
|
+
const violation = new ProtocolViolation(`group ${group.sequence} is at or past the declared end ${entry.end}`);
|
|
880
|
+
track.close(violation);
|
|
881
|
+
throw violation;
|
|
882
|
+
}
|
|
809
883
|
track.writeGroup(producer);
|
|
810
884
|
// Block until the timescale is known; the group's stream can arrive before
|
|
811
885
|
// TRACK_INFO (or implicit defaults) resolves it on the subscribe stream.
|
|
@@ -820,35 +894,12 @@ export class Subscriber {
|
|
|
820
894
|
await Signal.race(timescale, track.closed);
|
|
821
895
|
scale = timescale.peek();
|
|
822
896
|
}
|
|
823
|
-
|
|
824
|
-
// (the lite-05 FRAME format), which we decode into a Timestamp at that scale.
|
|
825
|
-
// Scale 0 (pre-lite-05) carries no timestamp, so we wall-clock-stamp.
|
|
826
|
-
let prevTs = 0n;
|
|
827
|
-
for (;;) {
|
|
828
|
-
// Only the group's own stream ends it: a track that closes first has already
|
|
829
|
-
// closed (or aborted) this group through its cache.
|
|
830
|
-
const done = await race([stream.done(), producer.closed]);
|
|
831
|
-
if (done !== false)
|
|
832
|
-
break;
|
|
833
|
-
let timestamp;
|
|
834
|
-
if (scale !== 0) {
|
|
835
|
-
prevTs += unzigzag(await stream.u62());
|
|
836
|
-
timestamp = new Time.Timestamp(Number(prevTs), Time.Timescale(scale));
|
|
837
|
-
}
|
|
838
|
-
else {
|
|
839
|
-
timestamp = Time.Timestamp.now();
|
|
840
|
-
}
|
|
841
|
-
const size = await stream.u53();
|
|
842
|
-
const payload = await stream.read(size);
|
|
843
|
-
if (!payload)
|
|
844
|
-
break;
|
|
845
|
-
producer.writeFrame({ payload, timestamp });
|
|
846
|
-
}
|
|
897
|
+
await readFrames(stream, producer, scale);
|
|
847
898
|
producer.close();
|
|
848
899
|
stream.stop(new StreamError(StreamCode.Cancel, { message: "cancel" }));
|
|
849
900
|
}
|
|
850
901
|
catch (err) {
|
|
851
|
-
const e =
|
|
902
|
+
const e = await sessionCause(this.#quic, err);
|
|
852
903
|
producer.close(e);
|
|
853
904
|
stream.stop(e);
|
|
854
905
|
}
|
|
@@ -907,7 +958,7 @@ export class Subscriber {
|
|
|
907
958
|
// Decode one datagram body and hand it to the matching subscription's producer. Drops the
|
|
908
959
|
// datagram (best-effort) if the subscription is unknown/closed or its timescale isn't resolved.
|
|
909
960
|
async #routeDatagram(payload) {
|
|
910
|
-
const dg = await DatagramMessage.decode(payload);
|
|
961
|
+
const dg = await DatagramMessage.decode(payload, this.version);
|
|
911
962
|
const entry = this.#subscribes.get(dg.subscribe);
|
|
912
963
|
if (!entry)
|
|
913
964
|
return; // Unknown or already-closed subscription.
|
|
@@ -958,7 +1009,7 @@ export class Subscriber {
|
|
|
958
1009
|
// transport hiccup) MUST NOT tear down the connection. On error, drop the
|
|
959
1010
|
// estimates so consumers know they're stale.
|
|
960
1011
|
try {
|
|
961
|
-
const stream = await Stream.open(this.#quic);
|
|
1012
|
+
const stream = await Stream.open(this.#quic, { version: this.version });
|
|
962
1013
|
await stream.writer.u53(StreamId.Probe);
|
|
963
1014
|
for (;;) {
|
|
964
1015
|
const probe = await Probe.decodeMaybe(stream.reader, this.version);
|
|
@@ -992,11 +1043,39 @@ export class Subscriber {
|
|
|
992
1043
|
* session died, since those tracks were cut off rather than ended.
|
|
993
1044
|
*/
|
|
994
1045
|
close(err) {
|
|
995
|
-
|
|
1046
|
+
// A fetch or setup exchange cut off by the session is incomplete even on a deliberate
|
|
1047
|
+
// close, so it always ends with an error.
|
|
1048
|
+
const cut = err ?? new StreamError(StreamCode.SessionClosed, { message: "session closed" });
|
|
1049
|
+
this.#closed.abort(cut);
|
|
996
1050
|
for (const { track } of this.#subscribes.values()) {
|
|
997
1051
|
track.close(err);
|
|
998
1052
|
}
|
|
999
1053
|
this.#subscribes.clear();
|
|
1054
|
+
// This also releases callers still awaiting acceptance.
|
|
1055
|
+
for (const { group } of this.#fetches.values()) {
|
|
1056
|
+
group.close(cut);
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
// Settles with `step`, or rejects with the group's error once it closes first. A publisher
|
|
1061
|
+
// may never answer a FETCH, so Subscriber.close() closing the group is what releases it.
|
|
1062
|
+
async function untilClosed(group, step) {
|
|
1063
|
+
const value = await race([step, group.closed]);
|
|
1064
|
+
const closed = group.closed.peek();
|
|
1065
|
+
if (closed !== undefined)
|
|
1066
|
+
throw closed ?? new Error("fetch closed before it was accepted");
|
|
1067
|
+
return value;
|
|
1068
|
+
}
|
|
1069
|
+
// Like untilClosed, but also cancels once every reader has left. Demand is level-triggered, so a
|
|
1070
|
+
// caller that coalesces onto the group before the check re-arms it.
|
|
1071
|
+
async function untilAbandoned(group, step) {
|
|
1072
|
+
const idle = Symbol("idle");
|
|
1073
|
+
for (;;) {
|
|
1074
|
+
const value = await untilClosed(group, race([step, group.unused().then(() => idle)]));
|
|
1075
|
+
if (value !== idle)
|
|
1076
|
+
return value;
|
|
1077
|
+
if (!group.used.peek())
|
|
1078
|
+
throw new StreamError(StreamCode.Cancel, { message: "cancel" });
|
|
1000
1079
|
}
|
|
1001
1080
|
}
|
|
1002
1081
|
/**
|