@moq/net 0.1.8 → 0.2.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/announced.d.ts +40 -24
- package/announced.d.ts.map +1 -1
- package/announced.js +74 -40
- package/announced.js.map +1 -1
- package/announced.test.d.ts +2 -0
- package/announced.test.d.ts.map +1 -0
- package/announced.test.js +37 -0
- package/announced.test.js.map +1 -0
- package/broadcast.d.ts +80 -25
- package/broadcast.d.ts.map +1 -1
- package/broadcast.js +266 -49
- package/broadcast.js.map +1 -1
- package/broadcast.test.d.ts +2 -0
- package/broadcast.test.d.ts.map +1 -0
- package/broadcast.test.js +219 -0
- package/broadcast.test.js.map +1 -0
- package/connection/accept.d.ts +6 -0
- package/connection/accept.d.ts.map +1 -1
- package/connection/accept.js +30 -10
- package/connection/accept.js.map +1 -1
- package/connection/browser.d.ts +5 -0
- package/connection/browser.d.ts.map +1 -0
- package/connection/browser.js +25 -0
- package/connection/browser.js.map +1 -0
- package/connection/browser.test.d.ts +2 -0
- package/connection/browser.test.d.ts.map +1 -0
- package/connection/browser.test.js +29 -0
- package/connection/browser.test.js.map +1 -0
- package/connection/connect.d.ts +43 -1
- package/connection/connect.d.ts.map +1 -1
- package/connection/connect.js +96 -107
- package/connection/connect.js.map +1 -1
- package/connection/connect.test.d.ts +2 -0
- package/connection/connect.test.d.ts.map +1 -0
- package/connection/connect.test.js +118 -0
- package/connection/connect.test.js.map +1 -0
- package/connection/established.d.ts +29 -15
- package/connection/established.d.ts.map +1 -1
- package/connection/established.js.map +1 -1
- package/connection/index.d.ts +8 -0
- package/connection/index.d.ts.map +1 -1
- package/connection/index.js +6 -0
- package/connection/index.js.map +1 -1
- package/connection/reload.d.ts +43 -7
- package/connection/reload.d.ts.map +1 -1
- package/connection/reload.js +165 -63
- package/connection/reload.js.map +1 -1
- package/connection/reload.test.d.ts +2 -0
- package/connection/reload.test.d.ts.map +1 -0
- package/connection/reload.test.js +104 -0
- package/connection/reload.test.js.map +1 -0
- package/connection/stats.d.ts +92 -0
- package/connection/stats.d.ts.map +1 -0
- package/connection/stats.js +35 -0
- package/connection/stats.js.map +1 -0
- package/connection/stats.test.d.ts +2 -0
- package/connection/stats.test.d.ts.map +1 -0
- package/connection/stats.test.js +120 -0
- package/connection/stats.test.js.map +1 -0
- package/connection/transport.d.ts +10 -0
- package/connection/transport.d.ts.map +1 -0
- package/connection/transport.js +12 -0
- package/connection/transport.js.map +1 -0
- package/consume.d.ts +26 -0
- package/consume.d.ts.map +1 -0
- package/consume.js +40 -0
- package/consume.js.map +1 -0
- package/datagram.d.ts +23 -0
- package/datagram.d.ts.map +1 -0
- package/datagram.js +3 -0
- package/datagram.js.map +1 -0
- package/group.d.ts +125 -36
- package/group.d.ts.map +1 -1
- package/group.js +244 -67
- package/group.js.map +1 -1
- package/group.test.js +121 -35
- package/group.test.js.map +1 -1
- package/ietf/adapter.d.ts +5 -3
- package/ietf/adapter.d.ts.map +1 -1
- package/ietf/adapter.js +19 -5
- package/ietf/adapter.js.map +1 -1
- package/ietf/aliases.d.ts +11 -0
- package/ietf/aliases.d.ts.map +1 -0
- package/ietf/aliases.js +52 -0
- package/ietf/aliases.js.map +1 -0
- package/ietf/aliases.test.d.ts +2 -0
- package/ietf/aliases.test.d.ts.map +1 -0
- package/ietf/aliases.test.js +30 -0
- package/ietf/aliases.test.js.map +1 -0
- package/ietf/connection.d.ts +19 -6
- package/ietf/connection.d.ts.map +1 -1
- package/ietf/connection.js +24 -9
- package/ietf/connection.js.map +1 -1
- package/ietf/ietf.test.js +58 -1
- package/ietf/ietf.test.js.map +1 -1
- package/ietf/object.d.ts +12 -4
- package/ietf/object.d.ts.map +1 -1
- package/ietf/object.js +92 -9
- package/ietf/object.js.map +1 -1
- package/ietf/publisher.d.ts +2 -2
- package/ietf/publisher.d.ts.map +1 -1
- package/ietf/publisher.js +12 -12
- package/ietf/publisher.js.map +1 -1
- package/ietf/subscribe_namespace.d.ts +0 -4
- package/ietf/subscribe_namespace.d.ts.map +1 -1
- package/ietf/subscribe_namespace.js +0 -5
- package/ietf/subscribe_namespace.js.map +1 -1
- package/ietf/subscriber.d.ts +8 -4
- package/ietf/subscriber.d.ts.map +1 -1
- package/ietf/subscriber.js +176 -105
- package/ietf/subscriber.js.map +1 -1
- package/index.d.ts +8 -5
- package/index.d.ts.map +1 -1
- package/index.js +8 -5
- package/index.js.map +1 -1
- package/integration.test.js +598 -12
- package/integration.test.js.map +1 -1
- package/internal.d.ts +14 -0
- package/internal.d.ts.map +1 -0
- package/internal.js +8 -0
- package/internal.js.map +1 -0
- package/lite/announce.d.ts +55 -13
- package/lite/announce.d.ts.map +1 -1
- package/lite/announce.js +221 -68
- package/lite/announce.js.map +1 -1
- package/lite/announce.test.d.ts +2 -0
- package/lite/announce.test.d.ts.map +1 -0
- package/lite/announce.test.js +71 -0
- package/lite/announce.test.js.map +1 -0
- package/lite/connection.d.ts +45 -19
- package/lite/connection.d.ts.map +1 -1
- package/lite/connection.js +85 -80
- package/lite/connection.js.map +1 -1
- package/lite/datagram.d.ts +23 -0
- package/lite/datagram.d.ts.map +1 -0
- package/lite/datagram.js +57 -0
- package/lite/datagram.js.map +1 -0
- package/lite/datagram.test.d.ts +2 -0
- package/lite/datagram.test.d.ts.map +1 -0
- package/lite/datagram.test.js +26 -0
- package/lite/datagram.test.js.map +1 -0
- package/lite/datagram_stream.d.ts +8 -0
- package/lite/datagram_stream.d.ts.map +1 -0
- package/lite/datagram_stream.js +44 -0
- package/lite/datagram_stream.js.map +1 -0
- package/lite/datagram_stream.test.d.ts +2 -0
- package/lite/datagram_stream.test.d.ts.map +1 -0
- package/lite/datagram_stream.test.js +15 -0
- package/lite/datagram_stream.test.js.map +1 -0
- package/lite/fetch.d.ts.map +1 -1
- package/lite/fetch.js +2 -2
- package/lite/fetch.js.map +1 -1
- package/lite/fetch.test.d.ts +2 -0
- package/lite/fetch.test.d.ts.map +1 -0
- package/lite/fetch.test.js +41 -0
- package/lite/fetch.test.js.map +1 -0
- package/lite/index.d.ts +3 -0
- package/lite/index.d.ts.map +1 -1
- package/lite/index.js +3 -0
- package/lite/index.js.map +1 -1
- package/lite/publisher.d.ts +13 -6
- package/lite/publisher.d.ts.map +1 -1
- package/lite/publisher.js +274 -64
- package/lite/publisher.js.map +1 -1
- package/lite/publisher.test.d.ts +2 -0
- package/lite/publisher.test.d.ts.map +1 -0
- package/lite/publisher.test.js +65 -0
- package/lite/publisher.test.js.map +1 -0
- package/lite/setup.d.ts +72 -6
- package/lite/setup.d.ts.map +1 -1
- package/lite/setup.js +184 -31
- package/lite/setup.js.map +1 -1
- package/lite/setup.test.d.ts +2 -0
- package/lite/setup.test.d.ts.map +1 -0
- package/lite/setup.test.js +122 -0
- package/lite/setup.test.js.map +1 -0
- package/lite/stream.d.ts +12 -3
- package/lite/stream.d.ts.map +1 -1
- package/lite/stream.js +7 -4
- package/lite/stream.js.map +1 -1
- package/lite/subscribe.d.ts +37 -23
- package/lite/subscribe.d.ts.map +1 -1
- package/lite/subscribe.js +70 -55
- package/lite/subscribe.js.map +1 -1
- package/lite/subscribe.test.d.ts +2 -0
- package/lite/subscribe.test.d.ts.map +1 -0
- package/lite/subscribe.test.js +68 -0
- package/lite/subscribe.test.js.map +1 -0
- package/lite/subscriber.d.ts +24 -17
- package/lite/subscriber.d.ts.map +1 -1
- package/lite/subscriber.js +521 -91
- package/lite/subscriber.js.map +1 -1
- package/lite/subscriber.test.d.ts +2 -0
- package/lite/subscriber.test.d.ts.map +1 -0
- package/lite/subscriber.test.js +30 -0
- package/lite/subscriber.test.js.map +1 -0
- package/lite/track.d.ts +25 -13
- package/lite/track.d.ts.map +1 -1
- package/lite/track.js +60 -35
- package/lite/track.js.map +1 -1
- package/lite/track.test.js +12 -10
- package/lite/track.test.js.map +1 -1
- package/lite/version.d.ts +17 -4
- package/lite/version.d.ts.map +1 -1
- package/lite/version.js +88 -17
- package/lite/version.js.map +1 -1
- package/mock.d.ts +42 -3
- package/mock.d.ts.map +1 -1
- package/mock.js +88 -17
- package/mock.js.map +1 -1
- package/package.json +3 -3
- package/path.d.ts +30 -0
- package/path.d.ts.map +1 -1
- package/path.js +49 -0
- package/path.js.map +1 -1
- package/path.test.js +41 -0
- package/path.test.js.map +1 -1
- package/stream.test.js +10 -0
- package/stream.test.js.map +1 -1
- package/time.d.ts +109 -86
- package/time.d.ts.map +1 -1
- package/time.js +80 -30
- package/time.js.map +1 -1
- package/time.test.d.ts +2 -0
- package/time.test.d.ts.map +1 -0
- package/time.test.js +12 -0
- package/time.test.js.map +1 -0
- package/track.d.ts +225 -45
- package/track.d.ts.map +1 -1
- package/track.js +505 -93
- package/track.js.map +1 -1
- package/track.test.js +138 -29
- package/track.test.js.map +1 -1
- package/util/error.d.ts +9 -0
- package/util/error.d.ts.map +1 -1
- package/util/error.js +21 -0
- package/util/error.js.map +1 -1
- package/util/error.test.d.ts +2 -0
- package/util/error.test.d.ts.map +1 -0
- package/util/error.test.js +45 -0
- package/util/error.test.js.map +1 -0
- package/util/index.d.ts +1 -0
- package/util/index.d.ts.map +1 -1
- package/util/index.js +1 -0
- package/util/index.js.map +1 -1
- package/util/timeout.d.ts +5 -0
- package/util/timeout.d.ts.map +1 -0
- package/util/timeout.js +18 -0
- package/util/timeout.js.map +1 -0
- package/util/timeout.test.d.ts +2 -0
- package/util/timeout.test.d.ts.map +1 -0
- package/util/timeout.test.js +18 -0
- package/util/timeout.test.js.map +1 -0
- package/varint.d.ts +6 -0
- package/varint.d.ts.map +1 -1
- package/varint.js +6 -2
- package/varint.js.map +1 -1
- package/zod.d.ts +4 -4
- package/zod.d.ts.map +1 -1
- package/zod.js.map +1 -1
- package/bandwidth.d.ts +0 -11
- package/bandwidth.d.ts.map +0 -1
- package/bandwidth.js +0 -7
- package/bandwidth.js.map +0 -1
package/lite/subscriber.js
CHANGED
|
@@ -1,15 +1,45 @@
|
|
|
1
1
|
/* @ts-self-types="./subscriber.d.ts" */
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import
|
|
2
|
+
import { Signal } from "@moq/signals";
|
|
3
|
+
import * as announce from "../announced.js";
|
|
4
|
+
import * as broadcast from "../broadcast.js";
|
|
5
|
+
import { BroadcastCache } from "../consume.js";
|
|
6
|
+
import * as netGroup from "../group.js";
|
|
5
7
|
import * as Path from "../path.js";
|
|
6
8
|
import { Stream } from "../stream.js";
|
|
7
|
-
import
|
|
8
|
-
import {
|
|
9
|
+
import * as Time from "../time.js";
|
|
10
|
+
import { error, reason } from "../util/error.js";
|
|
11
|
+
import { withTimeout } from "../util/timeout.js";
|
|
12
|
+
import { AnnounceInit, AnnounceOk, AnnounceRequest, decodeAnnounceBroadcastMaybe } from "./announce.js";
|
|
13
|
+
import { Datagram as DatagramMessage } from "./datagram.js";
|
|
14
|
+
import * as DatagramStream from "./datagram_stream.js";
|
|
15
|
+
import { Fetch as FetchMessage } from "./fetch.js";
|
|
9
16
|
import { Probe } from "./probe.js";
|
|
17
|
+
import { ProbeLevel } from "./setup.js";
|
|
10
18
|
import { StreamId } from "./stream.js";
|
|
11
19
|
import { decodeSubscribeResponse, decodeSubscribeResponseMaybe, Subscribe, SubscribeUpdate } from "./subscribe.js";
|
|
12
|
-
import {
|
|
20
|
+
import { TrackInfo, Track as TrackMessage } from "./track.js";
|
|
21
|
+
import { hasAnnounceId, hasAnnounceOk, hasDatagrams, Version } from "./version.js";
|
|
22
|
+
// Bound on how long stream-open plus the first response (SUBSCRIBE_OK on older
|
|
23
|
+
// drafts, or TRACK_INFO on lite-05+) may take. Browsers cap concurrent QUIC
|
|
24
|
+
// streams (Chrome ~100); past the cap createBidirectionalStream silently blocks.
|
|
25
|
+
// The timeout turns that into a clear error.
|
|
26
|
+
const SUBSCRIBE_SETUP_TIMEOUT_MS = 10_000;
|
|
27
|
+
/** Decode an unsigned zigzag varint back to a signed delta (mirrors Rust `VarInt::to_zigzag`). */
|
|
28
|
+
function unzigzag(v) {
|
|
29
|
+
return (v >> 1n) ^ -(v & 1n);
|
|
30
|
+
}
|
|
31
|
+
// The TRACK stream and implicit SUBSCRIBE acceptance are lite-05+.
|
|
32
|
+
function supportsTrackStream(version) {
|
|
33
|
+
switch (version) {
|
|
34
|
+
case Version.DRAFT_01:
|
|
35
|
+
case Version.DRAFT_02:
|
|
36
|
+
case Version.DRAFT_03:
|
|
37
|
+
case Version.DRAFT_04:
|
|
38
|
+
return false;
|
|
39
|
+
default:
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
13
43
|
/**
|
|
14
44
|
* Handles subscribing to broadcasts and managing their lifecycle.
|
|
15
45
|
*
|
|
@@ -22,29 +52,40 @@ export class Subscriber {
|
|
|
22
52
|
// Shared with the Publisher so callers can optionally filter out their
|
|
23
53
|
// own announcements on a per-call basis (see {@link AnnouncedOptions}).
|
|
24
54
|
origin;
|
|
25
|
-
// Our subscribed tracks.
|
|
55
|
+
// Our subscribed tracks. `timescale` resolves once known (from TRACK_INFO on
|
|
56
|
+
// lite-05+, or implicit defaults on older drafts); group streams block on it
|
|
57
|
+
// before decoding any frame, since a group's QUIC stream can race ahead.
|
|
26
58
|
#subscribes = new Map();
|
|
27
59
|
#subscribeNext = 0n;
|
|
28
|
-
//
|
|
29
|
-
#
|
|
30
|
-
//
|
|
31
|
-
|
|
60
|
+
// Dedup consumed broadcasts per path: repeat consume() calls share one subscription.
|
|
61
|
+
#consumes = new BroadcastCache();
|
|
62
|
+
// Dedup in-flight one-shot fetches, keyed by [broadcast, track, sequence]. Concurrent (or
|
|
63
|
+
// repeat, while still open) fetchGroup() calls for the same group share one FETCH stream and
|
|
64
|
+
// each get an independent mirror; the entry is evicted once the group closes.
|
|
65
|
+
#fetches = new Map();
|
|
66
|
+
// The peer's PROBE estimates, written as they arrive (Lite03+ only).
|
|
67
|
+
#probe;
|
|
68
|
+
// The peer's SETUP (lite-05+), undefined until it arrives. Gates opening the PROBE
|
|
69
|
+
// stream on the peer having advertised Probe >= Report.
|
|
70
|
+
#peerSetup;
|
|
71
|
+
// Distinguishes failures from streams torn down by Subscriber.close().
|
|
72
|
+
#closed = new AbortController();
|
|
32
73
|
/**
|
|
33
74
|
* Creates a new Subscriber instance.
|
|
34
75
|
* @param quic - The WebTransport session to use
|
|
35
76
|
* @param version - The protocol version
|
|
36
77
|
* @param origin - Origin id shared with the Publisher
|
|
37
|
-
* @param
|
|
38
|
-
* @param
|
|
78
|
+
* @param probe - Optional sink for the peer's PROBE estimates
|
|
79
|
+
* @param peerSetup - Optional peer SETUP slot for capability gating (lite-05+)
|
|
39
80
|
*
|
|
40
81
|
* @internal
|
|
41
82
|
*/
|
|
42
|
-
constructor(quic, version, origin,
|
|
83
|
+
constructor(quic, version, origin, probe, peerSetup) {
|
|
43
84
|
this.#quic = quic;
|
|
44
85
|
this.version = version;
|
|
45
86
|
this.origin = origin;
|
|
46
|
-
this.#
|
|
47
|
-
this.#
|
|
87
|
+
this.#probe = probe;
|
|
88
|
+
this.#peerSetup = peerSetup;
|
|
48
89
|
}
|
|
49
90
|
/**
|
|
50
91
|
* Subscribe to broadcast announcements under `prefix`.
|
|
@@ -53,21 +94,29 @@ export class Subscriber {
|
|
|
53
94
|
* this connection's {@link origin}.
|
|
54
95
|
*/
|
|
55
96
|
announced(prefix = Path.empty(), options = {}) {
|
|
56
|
-
const announced = new
|
|
97
|
+
const announced = new announce.Producer(prefix);
|
|
57
98
|
void this.#runAnnounced(announced, prefix, options);
|
|
58
|
-
return announced;
|
|
99
|
+
return announced.consume();
|
|
59
100
|
}
|
|
60
101
|
async #runAnnounced(announced, prefix, options) {
|
|
61
102
|
console.debug(`announced: prefix=${prefix}`);
|
|
62
103
|
// Send our own session-level origin id so the peer can skip announces
|
|
63
104
|
// whose hop chain already passed through us. Matches the Rust subscriber's
|
|
64
105
|
// `exclude_hop: self.self_origin.id` in `run_announce_prefix`.
|
|
65
|
-
const msg = new
|
|
106
|
+
const msg = new AnnounceRequest(prefix, this.origin);
|
|
66
107
|
try {
|
|
67
108
|
// Open a stream and send the announce interest.
|
|
68
109
|
const stream = await Stream.open(this.#quic);
|
|
69
110
|
await stream.writer.u53(StreamId.Announce);
|
|
70
111
|
await msg.encode(stream.writer, this.version);
|
|
112
|
+
// Lite05+: the publisher reports its own origin id before any announces.
|
|
113
|
+
// It no longer stamps itself onto each hop chain, so we append it here to
|
|
114
|
+
// keep the ignoreSelf loop check seeing the full chain.
|
|
115
|
+
let responderOrigin;
|
|
116
|
+
if (hasAnnounceOk(this.version)) {
|
|
117
|
+
const ok = await AnnounceOk.decode(stream.reader, this.version);
|
|
118
|
+
responderOrigin = ok.origin;
|
|
119
|
+
}
|
|
71
120
|
switch (this.version) {
|
|
72
121
|
case Version.DRAFT_01:
|
|
73
122
|
case Version.DRAFT_02: {
|
|
@@ -77,7 +126,7 @@ export class Subscriber {
|
|
|
77
126
|
for (const suffix of init.suffixes) {
|
|
78
127
|
const path = Path.join(prefix, suffix);
|
|
79
128
|
console.debug(`announced: broadcast=${path} active=true`);
|
|
80
|
-
announced.append({ path, active: true });
|
|
129
|
+
announced.append({ path: suffix, active: true });
|
|
81
130
|
}
|
|
82
131
|
break;
|
|
83
132
|
}
|
|
@@ -85,24 +134,73 @@ export class Subscriber {
|
|
|
85
134
|
// Draft03+: no AnnounceInit, initial state comes via Announce messages.
|
|
86
135
|
break;
|
|
87
136
|
}
|
|
137
|
+
// Lite06+: announce ids. Each received `active` implicitly assigns the next
|
|
138
|
+
// per-stream ordinal; `endedId`/`restart` reference it. Tracked even for
|
|
139
|
+
// announces we skip via ignoreSelf, since the sender doesn't know we skipped.
|
|
140
|
+
let nextAnnounceId = 0n;
|
|
141
|
+
const announcedById = new Map();
|
|
88
142
|
// Receive announce updates (for Draft03, this includes initial state)
|
|
89
143
|
for (;;) {
|
|
90
144
|
const announce = await Promise.race([
|
|
91
|
-
|
|
145
|
+
decodeAnnounceBroadcastMaybe(stream.reader, this.version),
|
|
92
146
|
announced.closed,
|
|
93
147
|
]);
|
|
148
|
+
// undefined: the stream ended. null: the consumer closed cleanly.
|
|
94
149
|
if (!announce)
|
|
95
150
|
break;
|
|
96
151
|
if (announce instanceof Error)
|
|
97
152
|
throw announce;
|
|
153
|
+
let suffix;
|
|
154
|
+
let active;
|
|
155
|
+
// Present on active/restart; ended messages never carry hops worth checking.
|
|
156
|
+
let hops;
|
|
157
|
+
switch (announce.status) {
|
|
158
|
+
case "active":
|
|
159
|
+
suffix = announce.suffix;
|
|
160
|
+
active = true;
|
|
161
|
+
hops = announce.hops;
|
|
162
|
+
if (hasAnnounceId(this.version)) {
|
|
163
|
+
announcedById.set(nextAnnounceId++, announce.suffix);
|
|
164
|
+
}
|
|
165
|
+
break;
|
|
166
|
+
case "ended":
|
|
167
|
+
suffix = announce.suffix;
|
|
168
|
+
active = false;
|
|
169
|
+
break;
|
|
170
|
+
case "endedId": {
|
|
171
|
+
// Resolve and retire the id; an unknown or retired id is a protocol violation.
|
|
172
|
+
const path = announcedById.get(announce.id);
|
|
173
|
+
if (path === undefined)
|
|
174
|
+
throw new Error(`unknown announce id: ${announce.id}`);
|
|
175
|
+
announcedById.delete(announce.id);
|
|
176
|
+
suffix = path;
|
|
177
|
+
active = false;
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
case "restart": {
|
|
181
|
+
// Resolve the id; it stays live (the replacement reuses it).
|
|
182
|
+
const path = announcedById.get(announce.id);
|
|
183
|
+
if (path === undefined)
|
|
184
|
+
throw new Error(`unknown announce id: ${announce.id}`);
|
|
185
|
+
suffix = path;
|
|
186
|
+
active = true;
|
|
187
|
+
hops = announce.hops;
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
98
191
|
// Optionally drop reflected announces so callers asking for
|
|
99
|
-
// "someone else's broadcasts" don't re-see their own publishes.
|
|
100
|
-
|
|
101
|
-
|
|
192
|
+
// "someone else's broadcasts" don't re-see their own publishes. In
|
|
193
|
+
// Lite05+ the sender's origin arrives via AnnounceOk, not in each hop
|
|
194
|
+
// list, so fold it back in before checking.
|
|
195
|
+
if (hops !== undefined && options.ignoreSelf) {
|
|
196
|
+
const full = responderOrigin !== undefined ? [...hops, responderOrigin] : hops;
|
|
197
|
+
if (full.includes(this.origin)) {
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
102
200
|
}
|
|
103
|
-
const path = Path.join(prefix,
|
|
104
|
-
console.debug(`announced: broadcast=${path} active=${
|
|
105
|
-
announced.append({ path, active
|
|
201
|
+
const path = Path.join(prefix, suffix);
|
|
202
|
+
console.debug(`announced: broadcast=${path} active=${active}`);
|
|
203
|
+
announced.append({ path: suffix, active });
|
|
106
204
|
}
|
|
107
205
|
announced.close();
|
|
108
206
|
}
|
|
@@ -113,78 +211,278 @@ export class Subscriber {
|
|
|
113
211
|
/**
|
|
114
212
|
* Consumes a broadcast from the connection.
|
|
115
213
|
*
|
|
214
|
+
* Deduplicated per path: repeat calls for the same still-live path share one reference-counted
|
|
215
|
+
* broadcast (and one upstream subscription). The shared broadcast closes once every caller has
|
|
216
|
+
* closed its handle, so callers close normally.
|
|
217
|
+
*
|
|
116
218
|
* @param name - The name of the broadcast to consume
|
|
117
219
|
* @returns A Broadcast instance
|
|
118
220
|
*/
|
|
119
221
|
consume(path) {
|
|
120
|
-
|
|
222
|
+
return this.#consumes.get(path) ?? this.#consumes.insert(path, this.#createConsume(path));
|
|
223
|
+
}
|
|
224
|
+
#createConsume(path) {
|
|
225
|
+
// A consumed broadcast resolves info() and fetchGroup() over the wire by reaching
|
|
226
|
+
// back into this Subscriber (see ConsumeBroadcast below), rather than the wire
|
|
227
|
+
// installing callbacks on the broadcast.
|
|
228
|
+
const consumer = new ConsumeBroadcast(this, path);
|
|
121
229
|
void (async () => {
|
|
122
230
|
for (;;) {
|
|
123
|
-
const request = await
|
|
231
|
+
const request = await consumer.requested();
|
|
124
232
|
if (!request)
|
|
125
233
|
break;
|
|
126
234
|
void this.#runSubscribe(path, request);
|
|
127
235
|
}
|
|
128
236
|
})();
|
|
129
|
-
return
|
|
237
|
+
return consumer;
|
|
130
238
|
}
|
|
131
239
|
async #runSubscribe(broadcast, request) {
|
|
132
240
|
const id = this.#subscribeNext++;
|
|
133
|
-
//
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
const
|
|
138
|
-
|
|
139
|
-
|
|
241
|
+
// `timescale` stays undefined until TRACK_INFO (or, on older drafts,
|
|
242
|
+
// implicit defaults) resolves it; runGroup blocks on it before decoding.
|
|
243
|
+
const timescale = new Signal(undefined);
|
|
244
|
+
console.debug(`subscribe start: id=${id} broadcast=${broadcast} track=${request.name}`);
|
|
245
|
+
const msg = new Subscribe({ id, broadcast, track: request.name, priority: request.priority });
|
|
246
|
+
// Open the stream under a timeout. The stream handle flows back via `state`
|
|
247
|
+
// so the timeout path can abort it if it finishes opening after the deadline.
|
|
248
|
+
const state = {};
|
|
249
|
+
const setup = this.#openSubscribe(state, msg, request, id, timescale);
|
|
250
|
+
let opened;
|
|
251
|
+
try {
|
|
252
|
+
opened = await withTimeout(setup, SUBSCRIBE_SETUP_TIMEOUT_MS, `subscribe timed out after ${SUBSCRIBE_SETUP_TIMEOUT_MS}ms waiting for the first response (browser stream limit reached?)`);
|
|
253
|
+
console.debug(`subscribe ok: id=${id} broadcast=${broadcast} track=${request.name}`);
|
|
254
|
+
}
|
|
255
|
+
catch (err) {
|
|
256
|
+
const e = error(err);
|
|
257
|
+
request.reject(e);
|
|
258
|
+
this.#subscribes.delete(id);
|
|
259
|
+
console.warn(`subscribe error: id=${id} broadcast=${broadcast} track=${request.name} error=${reason(e)}`);
|
|
260
|
+
// If the stream eventually opens after the timeout, abort it so we
|
|
261
|
+
// don't leak it. Cover both branches: setup may resolve late, or it
|
|
262
|
+
// may reject (e.g. encode/decode failure) after the stream is open.
|
|
263
|
+
setup.then(() => state.stream?.abort(e), () => state.stream?.abort(e));
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
const { stream, producer } = opened;
|
|
140
267
|
try {
|
|
141
|
-
// The first response MUST be a SUBSCRIBE_OK.
|
|
142
|
-
const resp = await decodeSubscribeResponse(stream.reader, this.version);
|
|
143
|
-
if (!("ok" in resp)) {
|
|
144
|
-
throw new Error("first subscribe response must be SUBSCRIBE_OK");
|
|
145
|
-
}
|
|
146
|
-
console.debug(`subscribe ok: id=${id} broadcast=${broadcast} track=${request.track.name}`);
|
|
147
268
|
// Watch for priority changes and send SUBSCRIBE_UPDATE. Lite01/Lite02
|
|
148
269
|
// don't carry SUBSCRIBE_UPDATE on the wire, so skip the watcher there
|
|
149
270
|
// and just wait on the stream/track like before.
|
|
150
|
-
//
|
|
151
|
-
//
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
271
|
+
//
|
|
272
|
+
// On lite-05+ the publisher sends SUBSCRIBE_START/END/DROP on this stream;
|
|
273
|
+
// drain them (we don't drive delivery off the resolved range) so the FIN is
|
|
274
|
+
// observed. Older drafts just wait for the stream to close.
|
|
275
|
+
const closed = supportsTrackStream(this.version) ? this.#drainResponses(stream) : stream.reader.closed;
|
|
276
|
+
const priorityUpdates = this.version === Version.DRAFT_01 || this.version === Version.DRAFT_02
|
|
277
|
+
? undefined
|
|
278
|
+
: this.#runPriorityUpdates(id, broadcast, producer, msg, stream);
|
|
279
|
+
// Terminal conditions (stream end, track close, a failed priority update) settle at most
|
|
280
|
+
// once; race them into one stable promise so the demand loop doesn't re-subscribe each pass.
|
|
281
|
+
const terminal = [closed, producer.closed];
|
|
282
|
+
if (priorityUpdates !== undefined)
|
|
283
|
+
terminal.push(priorityUpdates);
|
|
284
|
+
const done = Promise.race(terminal);
|
|
285
|
+
// Serve until a terminal condition fires or the last local subscriber leaves. The unused
|
|
286
|
+
// wake is level-triggered: re-check demand so a subscriber that returns before we tear
|
|
287
|
+
// down (e.g. a quickly unmuted tile) resumes on the same subscription.
|
|
288
|
+
const idle = Symbol("idle");
|
|
289
|
+
for (;;) {
|
|
290
|
+
const reason = await Promise.race([done, producer.unused().then(() => idle)]);
|
|
291
|
+
if (reason === idle && producer.closed.peek() === undefined && producer.used.peek())
|
|
292
|
+
continue;
|
|
293
|
+
break;
|
|
160
294
|
}
|
|
161
|
-
|
|
162
|
-
request.track.close();
|
|
295
|
+
producer.close();
|
|
163
296
|
stream.close();
|
|
164
|
-
console.debug(`subscribe close: id=${id} broadcast=${broadcast} track=${request.
|
|
297
|
+
console.debug(`subscribe close: id=${id} broadcast=${broadcast} track=${request.name}`);
|
|
165
298
|
}
|
|
166
299
|
catch (err) {
|
|
167
300
|
const e = error(err);
|
|
168
|
-
|
|
169
|
-
console.warn(`subscribe error: id=${id} broadcast=${broadcast} track=${request.
|
|
301
|
+
producer.close(e);
|
|
302
|
+
console.warn(`subscribe error: id=${id} broadcast=${broadcast} track=${request.name} error=${reason(e)}`);
|
|
170
303
|
stream.abort(e);
|
|
171
304
|
}
|
|
172
305
|
finally {
|
|
173
306
|
this.#subscribes.delete(id);
|
|
174
307
|
}
|
|
175
308
|
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
309
|
+
// Determine the track's immutable properties, accept the request (so the
|
|
310
|
+
// application's track.Subscriber resolves and incoming groups have a producer to
|
|
311
|
+
// write into), register it, then open the subscribe stream. `state.stream` is
|
|
312
|
+
// populated as soon as the subscribe stream opens so the caller can clean it up
|
|
313
|
+
// on timeout even before this promise settles.
|
|
314
|
+
//
|
|
315
|
+
// On lite-05+ the properties come from a TRACK stream opened first, and the
|
|
316
|
+
// SUBSCRIBE is accepted implicitly (no SUBSCRIBE_OK). Older drafts carry no
|
|
317
|
+
// per-track properties, so they resolve to defaults and just drain SUBSCRIBE_OK.
|
|
318
|
+
async #openSubscribe(state, msg, request, id, timescale) {
|
|
319
|
+
let producer;
|
|
320
|
+
let drainOk = false;
|
|
321
|
+
if (supportsTrackStream(this.version)) {
|
|
322
|
+
// Fetch the immutable properties once via the TRACK stream.
|
|
323
|
+
const info = await this.#trackInfo(msg.broadcast, msg.track);
|
|
324
|
+
producer = request.accept(this.#toModelInfo(info));
|
|
325
|
+
timescale.set(info.timescale);
|
|
326
|
+
}
|
|
327
|
+
else {
|
|
328
|
+
// Older drafts negotiate nothing per-track: verbatim frames, no timescale.
|
|
329
|
+
producer = request.accept();
|
|
330
|
+
timescale.set(0);
|
|
331
|
+
drainOk = true;
|
|
332
|
+
}
|
|
333
|
+
// Register before opening SUBSCRIBE so a racing GROUP stream finds the entry.
|
|
334
|
+
this.#subscribes.set(id, { track: producer, timescale });
|
|
335
|
+
state.stream = await Stream.open(this.#quic);
|
|
336
|
+
await state.stream.writer.u53(StreamId.Subscribe);
|
|
337
|
+
await msg.encode(state.stream.writer, this.version);
|
|
338
|
+
if (drainOk) {
|
|
339
|
+
// The first response MUST be a SUBSCRIBE_OK (older drafts only).
|
|
340
|
+
const resp = await decodeSubscribeResponse(state.stream.reader, this.version);
|
|
341
|
+
if (!("ok" in resp)) {
|
|
342
|
+
throw new Error("first subscribe response must be SUBSCRIBE_OK");
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
return { stream: state.stream, producer };
|
|
346
|
+
}
|
|
347
|
+
// Opens a TRACK stream, reads the single TRACK_INFO, and FINs. Lite-05+ only.
|
|
348
|
+
async #trackInfo(broadcast, track) {
|
|
349
|
+
const stream = await Stream.open(this.#quic);
|
|
350
|
+
try {
|
|
351
|
+
await stream.writer.u53(StreamId.Track);
|
|
352
|
+
await new TrackMessage(broadcast, track).encode(stream.writer, this.version);
|
|
353
|
+
const info = await TrackInfo.decode(stream.reader, this.version);
|
|
354
|
+
// The publisher FINs after TRACK_INFO; FIN our side too.
|
|
355
|
+
stream.close();
|
|
356
|
+
return info;
|
|
357
|
+
}
|
|
358
|
+
catch (err) {
|
|
359
|
+
stream.abort(error(err));
|
|
360
|
+
throw err;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
// Map the wire TRACK_INFO onto the model track.Info a producer/consumer holds.
|
|
364
|
+
#toModelInfo(info) {
|
|
365
|
+
return {
|
|
366
|
+
timescale: Time.Timescale(info.timescale),
|
|
367
|
+
// Publisher Max Latency rides on the wire, so the local retention window
|
|
368
|
+
// matches what the upstream advertises (relays re-serve with the same bound).
|
|
369
|
+
latencyMax: info.latencyMax,
|
|
370
|
+
priority: info.priority,
|
|
371
|
+
ordered: info.ordered,
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
// Resolve a track's immutable model info via a TRACK stream (lite-05+), for the
|
|
375
|
+
// ConsumeBroadcast backing track.Consumer.info(). On older drafts there's no TRACK
|
|
376
|
+
// stream, so this rejects rather than fabricating defaults.
|
|
377
|
+
async resolveTrackInfo(broadcast, track) {
|
|
378
|
+
if (!supportsTrackStream(this.version)) {
|
|
379
|
+
throw new Error("track info requires moq-lite-05 or newer");
|
|
380
|
+
}
|
|
381
|
+
return this.#toModelInfo(await this.#trackInfo(broadcast, track));
|
|
382
|
+
}
|
|
383
|
+
// Open a FETCH stream for one group and stream its bare frames into a group, for the
|
|
384
|
+
// ConsumeBroadcast backing track.Consumer.fetchGroup() (lite-05+).
|
|
385
|
+
fetchGroup(broadcast, track, sequence, options = {}) {
|
|
386
|
+
// Coalesce onto a still-open fetch of the same group so we don't open a second FETCH
|
|
387
|
+
// stream (and re-download it); each caller reads an independent mirror.
|
|
388
|
+
const key = JSON.stringify([broadcast, track, sequence]);
|
|
389
|
+
const existing = this.#fetches.get(key);
|
|
390
|
+
if (existing && !existing.isClosed)
|
|
391
|
+
return Promise.resolve(existing.mirror());
|
|
392
|
+
// Create and cache the group synchronously (before any await) so a concurrent fetch for
|
|
393
|
+
// the same group finds it and coalesces rather than racing to open its own stream.
|
|
394
|
+
const group = new netGroup.Producer(sequence);
|
|
395
|
+
this.#fetches.set(key, group);
|
|
396
|
+
void group.closed.then(() => {
|
|
397
|
+
if (this.#fetches.get(key) === group)
|
|
398
|
+
this.#fetches.delete(key);
|
|
399
|
+
});
|
|
400
|
+
return this.#runFetch(broadcast, track, sequence, options, group);
|
|
401
|
+
}
|
|
402
|
+
// Open the FETCH stream and pump the response into the shared group. Setup errors close the
|
|
403
|
+
// group (so coalesced mirrors observe them and the entry evicts) and reject this caller.
|
|
404
|
+
async #runFetch(broadcast, track, sequence, options, group) {
|
|
405
|
+
try {
|
|
406
|
+
if (!supportsTrackStream(this.version)) {
|
|
407
|
+
throw new Error("fetch group requires moq-lite-05 or newer");
|
|
408
|
+
}
|
|
409
|
+
const info = await this.#trackInfo(broadcast, track);
|
|
410
|
+
const priority = options.priority ?? 0;
|
|
411
|
+
const stream = await Stream.open(this.#quic, undefined, priority);
|
|
412
|
+
try {
|
|
413
|
+
await stream.writer.u53(StreamId.Fetch);
|
|
414
|
+
await new FetchMessage(broadcast, track, priority, sequence).encode(stream.writer, this.version);
|
|
415
|
+
}
|
|
416
|
+
catch (err) {
|
|
417
|
+
stream.abort(error(err));
|
|
418
|
+
throw err;
|
|
419
|
+
}
|
|
420
|
+
// Mint this caller's reader before starting the pump, so the group has demand when the
|
|
421
|
+
// pump begins watching it (an abandoned fetch cancels once every reader has left).
|
|
422
|
+
const consumer = group.mirror();
|
|
423
|
+
void this.#runFetchResponse(stream, group, Time.Timescale(info.timescale));
|
|
424
|
+
return consumer;
|
|
425
|
+
}
|
|
426
|
+
catch (err) {
|
|
427
|
+
group.close(error(err));
|
|
428
|
+
throw err;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
// Read the FETCH response (bare zigzag-delta-timestamped frames) into the group, then
|
|
432
|
+
// FIN. A stream-level failure aborts the group so its reader observes the gap.
|
|
433
|
+
async #runFetchResponse(stream, group, timescale) {
|
|
434
|
+
try {
|
|
435
|
+
let prevTs = 0n;
|
|
436
|
+
// Serve until the stream FINs, the group closes, or every reader leaves. A group can
|
|
437
|
+
// stay open indefinitely (a catalog or JSON stream), so an abandoned fetch is stopped by
|
|
438
|
+
// demand, not by the stream ending. `closed` and `unused` are watched across frames as
|
|
439
|
+
// stable promises (not re-subscribed to the signals each frame); the unused check is
|
|
440
|
+
// level-triggered, so a coalesced fetch that arrives before we cancel re-arms and resumes.
|
|
441
|
+
const idle = Symbol("idle");
|
|
442
|
+
const closed = Promise.resolve(group.closed);
|
|
443
|
+
let unused = group.unused().then(() => idle);
|
|
444
|
+
for (;;) {
|
|
445
|
+
const done = await Promise.race([stream.reader.done(), closed, unused]);
|
|
446
|
+
if (done === idle) {
|
|
447
|
+
if (!group.isClosed && group.used.peek()) {
|
|
448
|
+
unused = group.unused().then(() => idle);
|
|
449
|
+
continue;
|
|
450
|
+
}
|
|
451
|
+
break;
|
|
452
|
+
}
|
|
453
|
+
if (done !== false)
|
|
454
|
+
break;
|
|
455
|
+
prevTs += unzigzag(await stream.reader.u62());
|
|
456
|
+
const timestamp = new Time.Timestamp(Number(prevTs), timescale);
|
|
457
|
+
const size = await stream.reader.u53();
|
|
458
|
+
const payload = await stream.reader.read(size);
|
|
459
|
+
if (!payload)
|
|
460
|
+
break;
|
|
461
|
+
group.writeFrame({ payload, timestamp });
|
|
462
|
+
}
|
|
463
|
+
group.close();
|
|
464
|
+
stream.close();
|
|
465
|
+
}
|
|
466
|
+
catch (err) {
|
|
467
|
+
const e = error(err);
|
|
468
|
+
group.close(e);
|
|
469
|
+
stream.abort(e);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
// Drains SUBSCRIBE_START/END/DROP on the subscribe stream until FIN (lite-05+).
|
|
473
|
+
// The resolved range is informational here; the producer already orders groups.
|
|
474
|
+
// Resolves (never rejects) on FIN or on the stream being reset out from under it,
|
|
475
|
+
// so it's safe to drop from a Promise.race without an unhandled rejection.
|
|
183
476
|
async #drainResponses(stream) {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
477
|
+
try {
|
|
478
|
+
for (;;) {
|
|
479
|
+
const resp = await decodeSubscribeResponseMaybe(stream.reader, this.version);
|
|
480
|
+
if (!resp)
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
catch {
|
|
485
|
+
// Stream closed or reset; nothing more to drain.
|
|
188
486
|
}
|
|
189
487
|
}
|
|
190
488
|
/**
|
|
@@ -202,10 +500,10 @@ export class Subscriber {
|
|
|
202
500
|
const stopped = Promise.race([track.closed, stream.reader.closed]).then(() => null);
|
|
203
501
|
let lastSent;
|
|
204
502
|
for (;;) {
|
|
205
|
-
const current = track.
|
|
503
|
+
const current = track.subscription.peek()?.priority;
|
|
206
504
|
if (current === undefined || current === lastSent) {
|
|
207
505
|
// Nothing new to send; wait for a change or termination.
|
|
208
|
-
const next = await Promise.race([track.
|
|
506
|
+
const next = await Promise.race([track.subscription.changed(), stopped]);
|
|
209
507
|
if (next === null)
|
|
210
508
|
return;
|
|
211
509
|
continue;
|
|
@@ -232,30 +530,51 @@ export class Subscriber {
|
|
|
232
530
|
* @internal
|
|
233
531
|
*/
|
|
234
532
|
async runGroup(group, stream) {
|
|
235
|
-
const
|
|
236
|
-
if (!
|
|
533
|
+
const entry = this.#subscribes.get(group.subscribe);
|
|
534
|
+
if (!entry) {
|
|
237
535
|
if (group.subscribe >= this.#subscribeNext) {
|
|
238
536
|
throw new Error(`unknown subscription: id=${group.subscribe}`);
|
|
239
537
|
}
|
|
240
538
|
return;
|
|
241
539
|
}
|
|
242
|
-
const
|
|
243
|
-
|
|
540
|
+
const { track, timescale } = entry;
|
|
541
|
+
const producer = new netGroup.Producer(group.sequence);
|
|
542
|
+
track.writeGroup(producer);
|
|
244
543
|
try {
|
|
544
|
+
// Block until the timescale is known; the group's stream can arrive before
|
|
545
|
+
// TRACK_INFO (or implicit defaults) resolves it on the subscribe stream.
|
|
546
|
+
let scale = timescale.peek();
|
|
547
|
+
while (scale === undefined) {
|
|
548
|
+
if (track.closed.peek() !== undefined) {
|
|
549
|
+
// Subscription ended before the scale resolved; nothing to decode.
|
|
550
|
+
producer.close();
|
|
551
|
+
stream.stop(new Error("cancel"));
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
554
|
+
await Signal.race(timescale, track.closed);
|
|
555
|
+
scale = timescale.peek();
|
|
556
|
+
}
|
|
557
|
+
// A non-zero scale means every frame is prefixed with a zigzag-delta timestamp
|
|
558
|
+
// (the lite-05 FRAME format), which we decode into a Timestamp at that scale.
|
|
559
|
+
// Scale 0 (pre-lite-05) carries no timestamp, so we wall-clock-stamp.
|
|
560
|
+
let prevTs = 0n;
|
|
245
561
|
for (;;) {
|
|
246
|
-
const done = await Promise.race([stream.done(),
|
|
562
|
+
const done = await Promise.race([stream.done(), track.closed, producer.closed]);
|
|
247
563
|
if (done !== false)
|
|
248
564
|
break;
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
565
|
+
let timestamp;
|
|
566
|
+
if (scale !== 0) {
|
|
567
|
+
prevTs += unzigzag(await stream.u62());
|
|
568
|
+
timestamp = new Time.Timestamp(Number(prevTs), Time.Timescale(scale));
|
|
569
|
+
}
|
|
570
|
+
else {
|
|
571
|
+
timestamp = Time.Timestamp.now();
|
|
253
572
|
}
|
|
254
573
|
const size = await stream.u53();
|
|
255
574
|
const payload = await stream.read(size);
|
|
256
575
|
if (!payload)
|
|
257
576
|
break;
|
|
258
|
-
producer.writeFrame(payload);
|
|
577
|
+
producer.writeFrame({ payload, timestamp });
|
|
259
578
|
}
|
|
260
579
|
producer.close();
|
|
261
580
|
stream.stop(new Error("cancel"));
|
|
@@ -266,6 +585,69 @@ export class Subscriber {
|
|
|
266
585
|
stream.stop(e);
|
|
267
586
|
}
|
|
268
587
|
}
|
|
588
|
+
/**
|
|
589
|
+
* Receives QUIC datagrams and routes each to its subscription's track producer (lite-05 §6.4).
|
|
590
|
+
*
|
|
591
|
+
* Returns immediately on a non-datagram transport or pre-lite-05 version. A decode error or an
|
|
592
|
+
* unknown subscribe id drops that datagram without tearing down the session (best-effort); the
|
|
593
|
+
* loop ends only when the datagram stream closes.
|
|
594
|
+
*
|
|
595
|
+
* @internal
|
|
596
|
+
*/
|
|
597
|
+
async runDatagrams() {
|
|
598
|
+
if (!hasDatagrams(this.version) || DatagramStream.maxDatagramSize(this.#quic) === 0) {
|
|
599
|
+
return;
|
|
600
|
+
}
|
|
601
|
+
// Never reject: this loop is awaited alongside the connection's other tasks, so a
|
|
602
|
+
// datagram-stream failure must not tear the whole session down (it's best-effort).
|
|
603
|
+
const reader = DatagramStream.datagramReader(this.#quic);
|
|
604
|
+
if (!reader)
|
|
605
|
+
return;
|
|
606
|
+
try {
|
|
607
|
+
try {
|
|
608
|
+
for (;;) {
|
|
609
|
+
const { value, done } = await reader.read();
|
|
610
|
+
if (done)
|
|
611
|
+
break;
|
|
612
|
+
if (!value)
|
|
613
|
+
continue;
|
|
614
|
+
try {
|
|
615
|
+
await this.#routeDatagram(value);
|
|
616
|
+
}
|
|
617
|
+
catch (err) {
|
|
618
|
+
console.debug(`dropping datagram: ${reason(err)}`);
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
finally {
|
|
623
|
+
reader.releaseLock();
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
catch (err) {
|
|
627
|
+
const e = error(err);
|
|
628
|
+
if (e.message === "The session is closed.") {
|
|
629
|
+
console.debug(`datagram receive stopped: ${e.message}`);
|
|
630
|
+
}
|
|
631
|
+
else {
|
|
632
|
+
console.warn("datagram stream error", err);
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
// Decode one datagram body and hand it to the matching subscription's producer. Drops the
|
|
637
|
+
// datagram (best-effort) if the subscription is unknown/closed or its timescale isn't resolved.
|
|
638
|
+
async #routeDatagram(payload) {
|
|
639
|
+
const dg = await DatagramMessage.decode(payload);
|
|
640
|
+
const entry = this.#subscribes.get(dg.subscribe);
|
|
641
|
+
if (!entry)
|
|
642
|
+
return; // Unknown or already-closed subscription.
|
|
643
|
+
// Datagrams are lite-05+, which always negotiates a timescale; if it hasn't resolved
|
|
644
|
+
// yet (the datagram raced ahead of TRACK_INFO), drop rather than guess.
|
|
645
|
+
const scale = entry.timescale.peek();
|
|
646
|
+
if (!scale)
|
|
647
|
+
return;
|
|
648
|
+
const timestamp = new Time.Timestamp(dg.timestamp, Time.Timescale(scale));
|
|
649
|
+
entry.track.writeDatagram({ sequence: dg.sequence, timestamp, payload: dg.payload });
|
|
650
|
+
}
|
|
269
651
|
/**
|
|
270
652
|
* Opens a PROBE bidi stream to receive bandwidth estimates from the publisher.
|
|
271
653
|
* Returns immediately if recv bandwidth is not supported.
|
|
@@ -277,14 +659,31 @@ export class Subscriber {
|
|
|
277
659
|
*
|
|
278
660
|
* @internal
|
|
279
661
|
*/
|
|
662
|
+
// Await the peer's advertised probe level, blocking until its SETUP arrives. The peer
|
|
663
|
+
// MUST send exactly one SETUP, so this resolves once that stream is read.
|
|
664
|
+
async #peerProbeLevel(peerSetup) {
|
|
665
|
+
let setup = peerSetup.peek();
|
|
666
|
+
while (setup === undefined) {
|
|
667
|
+
setup = await peerSetup.changed();
|
|
668
|
+
}
|
|
669
|
+
return setup.probe;
|
|
670
|
+
}
|
|
280
671
|
async runProbe() {
|
|
281
|
-
if (!this.#
|
|
672
|
+
if (!this.#probe)
|
|
282
673
|
return;
|
|
283
674
|
if (this.version === Version.DRAFT_01 || this.version === Version.DRAFT_02)
|
|
284
675
|
return;
|
|
676
|
+
// Lite-05+ gates the PROBE stream on the peer advertising Probe >= Report in its
|
|
677
|
+
// SETUP. Wait for the SETUP, then bail if the peer can't report bitrate. Older
|
|
678
|
+
// drafts have no SETUP, so they keep probing unconditionally.
|
|
679
|
+
if (this.#peerSetup) {
|
|
680
|
+
const probe = await this.#peerProbeLevel(this.#peerSetup);
|
|
681
|
+
if (probe < ProbeLevel.Report)
|
|
682
|
+
return;
|
|
683
|
+
}
|
|
285
684
|
// Probe is best-effort: any failure (stream reset by peer, missing peer support,
|
|
286
685
|
// transport hiccup) MUST NOT tear down the connection. On error, drop the
|
|
287
|
-
//
|
|
686
|
+
// estimates so consumers know they're stale.
|
|
288
687
|
try {
|
|
289
688
|
const stream = await Stream.open(this.#quic);
|
|
290
689
|
await stream.writer.u53(StreamId.Probe);
|
|
@@ -292,25 +691,56 @@ export class Subscriber {
|
|
|
292
691
|
const probe = await Probe.decodeMaybe(stream.reader, this.version);
|
|
293
692
|
if (!probe)
|
|
294
693
|
break;
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
694
|
+
// A message that omits the RTT leaves the last one standing, so a
|
|
695
|
+
// bitrate-only report doesn't blank it.
|
|
696
|
+
const prev = this.#probe.peek();
|
|
697
|
+
this.#probe.set({
|
|
698
|
+
estimatedRecvRate: probe.bitrate ?? undefined,
|
|
699
|
+
rtt: probe.rtt !== undefined ? Time.Milli(probe.rtt) : prev.rtt,
|
|
700
|
+
});
|
|
299
701
|
}
|
|
300
702
|
}
|
|
301
703
|
catch (err) {
|
|
302
|
-
|
|
704
|
+
if (!this.#closed.signal.aborted) {
|
|
705
|
+
console.warn("probe stream error", err);
|
|
706
|
+
}
|
|
303
707
|
}
|
|
304
708
|
finally {
|
|
305
|
-
this.#
|
|
306
|
-
this.#rtt?.set(undefined);
|
|
709
|
+
this.#probe.set({});
|
|
307
710
|
}
|
|
308
711
|
}
|
|
309
712
|
close() {
|
|
310
|
-
|
|
713
|
+
this.#closed.abort();
|
|
714
|
+
for (const { track } of this.#subscribes.values()) {
|
|
311
715
|
track.close();
|
|
312
716
|
}
|
|
313
717
|
this.#subscribes.clear();
|
|
314
718
|
}
|
|
315
719
|
}
|
|
720
|
+
/**
|
|
721
|
+
* A broadcast consumed from a lite session. It resolves `track.Consumer.info()` and
|
|
722
|
+
* `.fetchGroup()` over the wire (lite-05+ TRACK / FETCH streams) by reaching into the
|
|
723
|
+
* {@link Subscriber} it was opened from, the way the Rust `BroadcastConsumer` holds its
|
|
724
|
+
* session. Live subscribes still flow through the inherited requested() queue.
|
|
725
|
+
*/
|
|
726
|
+
class ConsumeBroadcast extends broadcast.Consumer {
|
|
727
|
+
#subscriber;
|
|
728
|
+
#path;
|
|
729
|
+
constructor(subscriber, path, state) {
|
|
730
|
+
super(state);
|
|
731
|
+
this.#subscriber = subscriber;
|
|
732
|
+
this.#path = path;
|
|
733
|
+
}
|
|
734
|
+
// Preserve the subclass (and its wire-backed info/fetchGroup) when the consume cache shares
|
|
735
|
+
// this broadcast across callers.
|
|
736
|
+
clone() {
|
|
737
|
+
return new ConsumeBroadcast(this.#subscriber, this.#path, this.shareState());
|
|
738
|
+
}
|
|
739
|
+
resolveTrackInfo(name) {
|
|
740
|
+
return this.#subscriber.resolveTrackInfo(this.#path, name);
|
|
741
|
+
}
|
|
742
|
+
fetchGroup(name, sequence, options) {
|
|
743
|
+
return this.#subscriber.fetchGroup(this.#path, name, sequence, options);
|
|
744
|
+
}
|
|
745
|
+
}
|
|
316
746
|
//# sourceMappingURL=subscriber.js.map
|