@moq/net 0.3.4 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/announce.d.ts +7 -0
- package/announce.d.ts.map +1 -0
- package/announce.js +8 -0
- package/announce.js.map +1 -0
- package/announced.d.ts +35 -91
- package/announced.d.ts.map +1 -1
- package/announced.js +21 -156
- package/announced.js.map +1 -1
- package/bandwidth.d.ts +163 -0
- package/bandwidth.d.ts.map +1 -0
- package/bandwidth.js +304 -0
- package/bandwidth.js.map +1 -0
- package/bandwidth_api.d.ts +7 -0
- package/bandwidth_api.d.ts.map +1 -0
- package/bandwidth_api.js +8 -0
- package/bandwidth_api.js.map +1 -0
- package/broadcast.d.ts +20 -28
- package/broadcast.d.ts.map +1 -1
- package/broadcast.js +60 -42
- package/broadcast.js.map +1 -1
- package/connection/accept.d.ts +16 -1
- package/connection/accept.d.ts.map +1 -1
- package/connection/accept.js +39 -24
- package/connection/accept.js.map +1 -1
- package/connection/browser.d.ts.map +1 -1
- package/connection/browser.js +9 -7
- package/connection/browser.js.map +1 -1
- package/connection/connect.d.ts +30 -6
- package/connection/connect.d.ts.map +1 -1
- package/connection/connect.js +97 -50
- package/connection/connect.js.map +1 -1
- package/connection/established.d.ts +16 -21
- package/connection/established.d.ts.map +1 -1
- package/connection/established.js.map +1 -1
- package/connection/forward.d.ts +2 -0
- package/connection/forward.d.ts.map +1 -0
- package/connection/forward.js +159 -0
- package/connection/forward.js.map +1 -0
- package/connection/handshake.d.ts.map +1 -1
- package/connection/handshake.js +2 -2
- package/connection/handshake.js.map +1 -1
- package/connection/index.d.ts +5 -5
- package/connection/index.d.ts.map +1 -1
- package/connection/index.js +4 -5
- package/connection/index.js.map +1 -1
- package/connection/pool.d.ts +188 -0
- package/connection/pool.d.ts.map +1 -0
- package/connection/pool.js +351 -0
- package/connection/pool.js.map +1 -0
- package/connection/reload.d.ts +14 -93
- package/connection/reload.d.ts.map +1 -1
- package/connection/reload.js +216 -80
- package/connection/reload.js.map +1 -1
- package/connection/stats.d.ts +3 -26
- package/connection/stats.d.ts.map +1 -1
- package/connection/stats.js.map +1 -1
- package/connection/transport.d.ts +0 -7
- package/connection/transport.d.ts.map +1 -1
- package/consume.d.ts +1 -43
- package/consume.d.ts.map +1 -1
- package/consume.js +1 -1
- package/consume.js.map +1 -1
- package/error.d.ts +180 -29
- package/error.d.ts.map +1 -1
- package/error.js +310 -16
- package/error.js.map +1 -1
- package/errors.d.ts +7 -0
- package/errors.d.ts.map +1 -0
- package/errors.js +8 -0
- package/errors.js.map +1 -0
- package/group.d.ts +9 -43
- package/group.d.ts.map +1 -1
- package/group.js +283 -69
- package/group.js.map +1 -1
- package/hop.d.ts +107 -0
- package/hop.d.ts.map +1 -0
- package/hop.js +103 -0
- package/hop.js.map +1 -0
- package/ietf/aliases.d.ts +1 -78
- package/ietf/aliases.d.ts.map +1 -1
- package/ietf/cluster.d.ts +6 -120
- package/ietf/cluster.d.ts.map +1 -1
- package/ietf/cluster.js +81 -41
- package/ietf/cluster.js.map +1 -1
- package/ietf/connection.d.ts +6 -72
- package/ietf/connection.d.ts.map +1 -1
- package/ietf/connection.js +11 -47
- package/ietf/connection.js.map +1 -1
- package/ietf/error.d.ts +11 -0
- package/ietf/error.d.ts.map +1 -0
- package/ietf/error.js +168 -0
- package/ietf/error.js.map +1 -0
- package/ietf/filter.d.ts.map +1 -1
- package/ietf/filter.js +6 -1
- package/ietf/filter.js.map +1 -1
- package/ietf/goaway.d.ts.map +1 -1
- package/ietf/goaway.js +22 -5
- package/ietf/goaway.js.map +1 -1
- package/ietf/parameters.d.ts +2 -2
- package/ietf/parameters.d.ts.map +1 -1
- package/ietf/parameters.js +15 -17
- package/ietf/parameters.js.map +1 -1
- package/ietf/publish_namespace.d.ts +25 -0
- package/ietf/publish_namespace.d.ts.map +1 -1
- package/ietf/publish_namespace.js +63 -0
- package/ietf/publish_namespace.js.map +1 -1
- package/ietf/publisher.d.ts +1 -82
- package/ietf/publisher.d.ts.map +1 -1
- package/ietf/publisher.js +219 -125
- package/ietf/publisher.js.map +1 -1
- package/ietf/solicit.d.ts +1 -40
- package/ietf/solicit.d.ts.map +1 -1
- package/ietf/subscribe.d.ts +1 -5
- package/ietf/subscribe.d.ts.map +1 -1
- package/ietf/subscribe.js +8 -9
- package/ietf/subscribe.js.map +1 -1
- package/ietf/subscriber.d.ts +1 -65
- package/ietf/subscriber.d.ts.map +1 -1
- package/ietf/subscriber.js +154 -73
- package/ietf/subscriber.js.map +1 -1
- package/ietf/version.d.ts +12 -1
- package/ietf/version.d.ts.map +1 -1
- package/ietf/version.js +13 -0
- package/ietf/version.js.map +1 -1
- package/index.d.ts +11 -6
- package/index.d.ts.map +1 -1
- package/index.js +10 -5
- package/index.js.map +1 -1
- package/internal.d.ts +99 -1
- package/internal.d.ts.map +1 -1
- package/internal.js +65 -0
- package/internal.js.map +1 -1
- package/lite/announce.d.ts +15 -9
- package/lite/announce.d.ts.map +1 -1
- package/lite/announce.js +47 -20
- package/lite/announce.js.map +1 -1
- package/lite/connection.d.ts +9 -59
- package/lite/connection.d.ts.map +1 -1
- package/lite/connection.js +20 -32
- package/lite/connection.js.map +1 -1
- package/lite/fetch.d.ts +15 -1
- package/lite/fetch.d.ts.map +1 -1
- package/lite/fetch.js +39 -7
- package/lite/fetch.js.map +1 -1
- package/lite/goaway.d.ts.map +1 -1
- package/lite/goaway.js +7 -1
- package/lite/goaway.js.map +1 -1
- package/lite/group.d.ts +18 -4
- package/lite/group.d.ts.map +1 -1
- package/lite/group.js +31 -10
- package/lite/group.js.map +1 -1
- package/lite/priority.d.ts +1 -61
- package/lite/priority.d.ts.map +1 -1
- package/lite/priority.js +4 -5
- package/lite/priority.js.map +1 -1
- package/lite/publisher.d.ts +1 -69
- package/lite/publisher.d.ts.map +1 -1
- package/lite/publisher.js +559 -232
- package/lite/publisher.js.map +1 -1
- package/lite/setup.d.ts +7 -7
- package/lite/setup.d.ts.map +1 -1
- package/lite/setup.js +13 -13
- package/lite/setup.js.map +1 -1
- package/lite/subscribe.d.ts +61 -13
- package/lite/subscribe.d.ts.map +1 -1
- package/lite/subscribe.js +186 -44
- package/lite/subscribe.js.map +1 -1
- package/lite/subscriber.d.ts +8 -57
- package/lite/subscriber.d.ts.map +1 -1
- package/lite/subscriber.js +179 -117
- package/lite/subscriber.js.map +1 -1
- package/lite/track.d.ts +4 -10
- package/lite/track.d.ts.map +1 -1
- package/lite/track.js +34 -29
- package/lite/track.js.map +1 -1
- package/lite/version.d.ts +31 -6
- package/lite/version.d.ts.map +1 -1
- package/lite/version.js +71 -11
- package/lite/version.js.map +1 -1
- package/origin.d.ts +229 -29
- package/origin.d.ts.map +1 -1
- package/origin.js +1087 -37
- package/origin.js.map +1 -1
- package/package.json +3 -2
- package/path.d.ts +25 -7
- package/path.d.ts.map +1 -1
- package/path.js +5 -3
- package/path.js.map +1 -1
- package/stream.d.ts.map +1 -1
- package/stream.js +25 -16
- package/stream.js.map +1 -1
- package/time.d.ts +15 -2
- package/time.d.ts.map +1 -1
- package/time.js +28 -9
- package/time.js.map +1 -1
- package/track.d.ts +171 -82
- package/track.d.ts.map +1 -1
- package/track.js +633 -184
- package/track.js.map +1 -1
- package/util/log.d.ts +5 -0
- package/util/log.d.ts.map +1 -0
- package/util/log.js +17 -0
- package/util/log.js.map +1 -0
- package/wire.d.ts +67 -0
- package/wire.d.ts.map +1 -0
- package/wire.js +32 -0
- package/wire.js.map +1 -0
- package/zod.d.ts +1 -1
- package/zod.d.ts.map +1 -1
- package/zod.js.map +1 -1
- package/mock.d.ts +0 -66
- package/mock.d.ts.map +0 -1
- package/mock.js +0 -243
- package/mock.js.map +0 -1
package/lite/publisher.js
CHANGED
|
@@ -1,18 +1,41 @@
|
|
|
1
1
|
/* @ts-self-types="./publisher.d.ts" */
|
|
2
2
|
import { Signal } from "@moq/signals";
|
|
3
|
-
import { error, reason } from "../error.js";
|
|
3
|
+
import { error, NotFound, reason, StreamCode, StreamError } from "../error.js";
|
|
4
|
+
import { routesEqual } from "../hop.js";
|
|
5
|
+
import { hooks } from "../internal.js";
|
|
4
6
|
import * as Path from "../path.js";
|
|
5
7
|
import { Writer } from "../stream.js";
|
|
6
|
-
import { Timescale } from "../time.js";
|
|
8
|
+
import { Milli, Timescale } from "../time.js";
|
|
9
|
+
import { wireOf } from "../wire.js";
|
|
7
10
|
import { AnnounceInit, AnnounceOk, encodeAnnounceBroadcast } from "./announce.js";
|
|
8
11
|
import { Datagram as DatagramMessage } from "./datagram.js";
|
|
9
12
|
import * as DatagramStream from "./datagram_stream.js";
|
|
10
13
|
import { Group as GroupMessage } from "./group.js";
|
|
11
14
|
import { Priority, sendOrder } from "./priority.js";
|
|
12
15
|
import { Probe } from "./probe.js";
|
|
13
|
-
import { encodeSubscribeResponse, SubscribeEnd, SubscribeOk, SubscribeStart, SubscribeUpdate, } from "./subscribe.js";
|
|
16
|
+
import { encodeSubscribeResponse, exclusiveGroupEnd, SubscribeEnd, SubscribeOk, SubscribeStart, SubscribeUpdate, } from "./subscribe.js";
|
|
14
17
|
import { TrackInfo as TrackInfoMessage } from "./track.js";
|
|
15
|
-
import { hasAnnounceId, hasAnnounceOk, hasDatagrams, hasProbeRtt, Version } from "./version.js";
|
|
18
|
+
import { hasAnnounceId, hasAnnounceOk, hasDatagrams, hasProbeRtt, resolvesStart, Version } from "./version.js";
|
|
19
|
+
// Where each originated route lands under the requested prefix: its suffix beneath
|
|
20
|
+
// the prefix, or the empty suffix for a route above it, where the most specific
|
|
21
|
+
// such route wins the way a request through the prefix would resolve.
|
|
22
|
+
function presented(prefix, table) {
|
|
23
|
+
const out = new Map();
|
|
24
|
+
let rootLen = -1;
|
|
25
|
+
for (const [covered, snap] of table) {
|
|
26
|
+
if (Path.hasPrefix(covered, prefix)) {
|
|
27
|
+
if (covered.length < rootLen)
|
|
28
|
+
continue;
|
|
29
|
+
rootLen = covered.length;
|
|
30
|
+
out.set(Path.empty(), snap);
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
const suffix = Path.stripPrefix(prefix, covered);
|
|
34
|
+
if (suffix !== null)
|
|
35
|
+
out.set(suffix, snap);
|
|
36
|
+
}
|
|
37
|
+
return out;
|
|
38
|
+
}
|
|
16
39
|
const PROBE_INTERVAL = 100; // ms
|
|
17
40
|
const PROBE_MAX_AGE = 10_000; // ms
|
|
18
41
|
const PROBE_MAX_DELTA = 0.25;
|
|
@@ -34,6 +57,169 @@ function supportsTrackStream(version) {
|
|
|
34
57
|
return true;
|
|
35
58
|
}
|
|
36
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* The frames of `sequence` a subscription asked for, as a start index and an inclusive end.
|
|
62
|
+
*
|
|
63
|
+
* The frame bounds qualify the start and end group only; every other group is served whole.
|
|
64
|
+
* Which groups are served at all is the subscriber's read cursor (`replaceGroups`),
|
|
65
|
+
* applied when a group is popped rather than re-checked here.
|
|
66
|
+
*
|
|
67
|
+
* The serving loop calls this synchronously after the pop, before any SUBSCRIBE_UPDATE can
|
|
68
|
+
* change `bounds`. Nothing downstream trims the frame range: it is a wire request,
|
|
69
|
+
* deliberately decoupled from the receiver's local read cursor.
|
|
70
|
+
*/
|
|
71
|
+
function frameRange(bounds, sequence) {
|
|
72
|
+
return {
|
|
73
|
+
start: bounds.startGroup === sequence ? bounds.startFrame : 0,
|
|
74
|
+
end: bounds.endGroup === sequence ? bounds.endFrame : undefined,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* The subscribe stream's control half, decoded ahead of the serving loop.
|
|
79
|
+
*
|
|
80
|
+
* Decoding runs on its own and publishes each full subscription update immediately, so a
|
|
81
|
+
* blocked response write cannot delay re-ranking streams already in flight. It separately
|
|
82
|
+
* stores only the latest range state for the serving loop, which owns the local track cursor
|
|
83
|
+
* and frame bounds. That keeps a group pop and its frame-range snapshot one indivisible step.
|
|
84
|
+
*
|
|
85
|
+
* Reading ahead makes control-first ordering hold for a burst. Coalescing bounds memory while
|
|
86
|
+
* preserving the newest state decoded before the next group pop. The Rust publisher gets the
|
|
87
|
+
* same ordering from `poll_decode_maybe`, which decodes straight out of the reader's buffer;
|
|
88
|
+
* nothing here can decode synchronously, so it reads ahead instead.
|
|
89
|
+
*/
|
|
90
|
+
class SubscriptionControls {
|
|
91
|
+
#writer;
|
|
92
|
+
#update;
|
|
93
|
+
// Sticky, first one wins: null once the stream is over, an Error once it failed.
|
|
94
|
+
#end;
|
|
95
|
+
#ended;
|
|
96
|
+
#resolveEnd;
|
|
97
|
+
#changed = new Signal(0);
|
|
98
|
+
/** Settles once decoding stops, so teardown can wait for it rather than leaving it running. */
|
|
99
|
+
decoding;
|
|
100
|
+
constructor({ reader, writer, version, apply }) {
|
|
101
|
+
this.#writer = writer;
|
|
102
|
+
this.#ended = new Promise((resolve) => {
|
|
103
|
+
this.#resolveEnd = resolve;
|
|
104
|
+
});
|
|
105
|
+
this.decoding = this.#decode(reader, version, apply);
|
|
106
|
+
// Our own half going away ends the loop too, and has to reach it the same way: the
|
|
107
|
+
// loop looks at nothing else.
|
|
108
|
+
void writer.closed.then(() => this.#finish(null), (err) => this.#finish(error(err)));
|
|
109
|
+
}
|
|
110
|
+
/** The next control to apply, or undefined while the peer is quiet. */
|
|
111
|
+
take() {
|
|
112
|
+
const update = this.#update;
|
|
113
|
+
this.#update = undefined;
|
|
114
|
+
// An update decoded before the stream ended still applies before the sticky end.
|
|
115
|
+
if (update)
|
|
116
|
+
return { kind: "update", update };
|
|
117
|
+
if (this.#end === undefined)
|
|
118
|
+
return undefined;
|
|
119
|
+
return this.#end === null ? { kind: "done" } : { kind: "error", error: this.#end };
|
|
120
|
+
}
|
|
121
|
+
/** Calls `fn` once {@link take} may answer differently. */
|
|
122
|
+
changed(fn) {
|
|
123
|
+
return this.#changed.changed(fn);
|
|
124
|
+
}
|
|
125
|
+
/** Returns false when peer departure supersedes a blocked response write. */
|
|
126
|
+
async response(pending) {
|
|
127
|
+
const result = await Promise.race([
|
|
128
|
+
pending.then(() => ({ kind: "sent" }), (err) => ({ kind: "error", error: error(err) })),
|
|
129
|
+
this.#ended.then((end) => ({ kind: "ended", end })),
|
|
130
|
+
]);
|
|
131
|
+
if (result.kind === "sent")
|
|
132
|
+
return true;
|
|
133
|
+
if (result.kind === "error")
|
|
134
|
+
throw result.error;
|
|
135
|
+
// Promise.race leaves the blocked encode running, so reset the writable half too.
|
|
136
|
+
this.#writer.reset(result.end ?? new StreamError(StreamCode.Cancel, { message: "cancel" }));
|
|
137
|
+
if (result.end)
|
|
138
|
+
throw result.end;
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
#finish(end) {
|
|
142
|
+
if (this.#end !== undefined)
|
|
143
|
+
return;
|
|
144
|
+
this.#end = end;
|
|
145
|
+
this.#resolveEnd(end);
|
|
146
|
+
this.#changed.update((value) => value + 1);
|
|
147
|
+
}
|
|
148
|
+
async #decode(reader, version, apply) {
|
|
149
|
+
try {
|
|
150
|
+
while (this.#end === undefined) {
|
|
151
|
+
const update = await SubscribeUpdate.decodeMaybe(reader, version);
|
|
152
|
+
if (!update)
|
|
153
|
+
break;
|
|
154
|
+
apply(update);
|
|
155
|
+
this.#update = update;
|
|
156
|
+
this.#changed.update((value) => value + 1);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
catch (err) {
|
|
160
|
+
this.#finish(error(err));
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
this.#finish(null);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
// A microtask is too short: decoding one framed update crosses several awaits, each of which
|
|
167
|
+
// can requeue behind the serving continuation. A task boundary lets the decoder finish whatever
|
|
168
|
+
// the transport already delivered before the next group pop. Updates are rare, so groups do not
|
|
169
|
+
// pay this scheduling cost on the normal path.
|
|
170
|
+
const yieldToControls = () => new Promise((resolve) => setTimeout(resolve, 0));
|
|
171
|
+
// Register both readiness sources in the same turn after the caller observed neither ready.
|
|
172
|
+
// The winner disposes both registrations, so an idle subscription accumulates nothing no
|
|
173
|
+
// matter how many times it wakes.
|
|
174
|
+
function waitForSubscription(controls, subscriber) {
|
|
175
|
+
return new Promise((resolve) => {
|
|
176
|
+
let settled = false;
|
|
177
|
+
const dispose = [];
|
|
178
|
+
const wake = () => {
|
|
179
|
+
if (settled)
|
|
180
|
+
return;
|
|
181
|
+
settled = true;
|
|
182
|
+
for (const close of dispose)
|
|
183
|
+
close();
|
|
184
|
+
resolve();
|
|
185
|
+
};
|
|
186
|
+
dispose.push(controls.changed(wake), hooks.groupChanged(subscriber, wake));
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* The budget to serve a peer with, given what its wire could tell us.
|
|
191
|
+
*
|
|
192
|
+
* A version without the field decodes as `0`, which is indistinguishable from a peer
|
|
193
|
+
* genuinely asking for the live edge. Serving that as real time would discard backlog
|
|
194
|
+
* a legacy subscriber never declined, so fall back to a window wide enough not to drop
|
|
195
|
+
* and leave enforcement to the receiver, as the IETF path does for the same reason.
|
|
196
|
+
*/
|
|
197
|
+
function servingMaxAge(version, requested) {
|
|
198
|
+
return carriesMaxAge(version) ? (requested ?? 0) : Number.MAX_SAFE_INTEGER;
|
|
199
|
+
}
|
|
200
|
+
/** Whether this version's SUBSCRIBE carries Subscriber Max Age at all. */
|
|
201
|
+
function carriesMaxAge(version) {
|
|
202
|
+
return version !== Version.DRAFT_01 && version !== Version.DRAFT_02;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Position a subscription's read cursor for the wire serving it.
|
|
206
|
+
*
|
|
207
|
+
* On lite-06 there is nothing to do: the cursor is floored at the group the subscription
|
|
208
|
+
* named (or 0), and its Max Age decides what above the floor is worth delivering.
|
|
209
|
+
*
|
|
210
|
+
* Pre-06 wires are the exception: their drafts define an absent `Group Start` as the
|
|
211
|
+
* latest group, so say so explicitly rather than letting the budget reach back. Lite-03/04/05
|
|
212
|
+
* carry a Max Age, but there it is a staleness tolerance only; lite-01/02 additionally get
|
|
213
|
+
* an unbounded budget so nothing is dropped under them (see {@link servingMaxAge}), which
|
|
214
|
+
* must not read as a request to replay the whole cache on join.
|
|
215
|
+
*/
|
|
216
|
+
function positionCursor(track, version, startGroup) {
|
|
217
|
+
if (resolvesStart(version) || startGroup !== undefined)
|
|
218
|
+
return;
|
|
219
|
+
const latest = track.latest();
|
|
220
|
+
if (latest !== undefined)
|
|
221
|
+
hooks.replaceGroups(track, { start: { included: latest } });
|
|
222
|
+
}
|
|
37
223
|
/**
|
|
38
224
|
* Handles publishing broadcasts and managing their lifecycle.
|
|
39
225
|
*
|
|
@@ -46,58 +232,51 @@ export class Publisher {
|
|
|
46
232
|
// can detect loops and prefer shorter paths. Created by Connection and
|
|
47
233
|
// shared with Subscriber, which can optionally use it to filter out its
|
|
48
234
|
// own announcements.
|
|
49
|
-
|
|
235
|
+
hop;
|
|
50
236
|
#quic;
|
|
51
237
|
// The one writer for the outbound datagram stream (getWriter locks it), acquired once at
|
|
52
238
|
// construction when this version + transport carry datagrams, released in close(). Its
|
|
53
239
|
// presence is the gate: undefined means datagrams aren't served on this connection. All
|
|
54
240
|
// subscriptions share it, since a second getWriter on the same stream would throw.
|
|
55
241
|
#datagramWriter;
|
|
56
|
-
//
|
|
57
|
-
//
|
|
58
|
-
|
|
242
|
+
// The published broadcasts, borrowed from the origin this session serves. The origin
|
|
243
|
+
// outlives the session, so this is read-only here: subscribe/fetch look it up, and
|
|
244
|
+
// closing the session leaves the broadcasts alone.
|
|
245
|
+
#broadcasts;
|
|
246
|
+
// Originated advertisements this session forwards. Unadvertised local broadcasts
|
|
247
|
+
// stay reachable by exact path without appearing here.
|
|
248
|
+
#advertised;
|
|
249
|
+
#publish;
|
|
59
250
|
// TRACK_INFO is immutable per track, so resolve it from the application once
|
|
60
251
|
// (via a throwaway subscribe whose info() resolves when the app calls accept)
|
|
61
|
-
// and reuse it for every later TRACK request of the same track. Keyed by
|
|
62
|
-
//
|
|
63
|
-
|
|
252
|
+
// and reuse it for every later TRACK request of the same track. Keyed by the
|
|
253
|
+
// routing front rather than the path: immutability holds for one broadcast, and a
|
|
254
|
+
// republish puts a different one on the path, so its entries must not be reused.
|
|
255
|
+
// A rejected lookup is evicted so a retry can re-probe.
|
|
256
|
+
#trackInfo = new WeakMap();
|
|
64
257
|
/**
|
|
65
258
|
* Creates a new Publisher instance.
|
|
66
259
|
* @param quic - The WebTransport session to use
|
|
67
260
|
* @param version - Negotiated protocol version
|
|
68
|
-
* @param origin -
|
|
261
|
+
* @param origin - Hop id shared with the Subscriber
|
|
262
|
+
* @param publish - The origin whose broadcasts this session serves; omit to publish nothing
|
|
69
263
|
*
|
|
70
264
|
* @internal
|
|
71
265
|
*/
|
|
72
|
-
constructor(quic, version,
|
|
266
|
+
constructor(quic, version, hop, publish) {
|
|
73
267
|
this.#quic = quic;
|
|
74
268
|
this.version = version;
|
|
75
|
-
this.
|
|
269
|
+
this.hop = hop;
|
|
270
|
+
const origin = publish && wireOf(publish);
|
|
271
|
+
this.#broadcasts = origin?.broadcasts ?? new Signal(new Map());
|
|
272
|
+
this.#advertised = origin?.advertised ?? new Signal(new Map());
|
|
273
|
+
this.#publish = publish;
|
|
76
274
|
// Grab the datagram writer up front when the transport carries datagrams (no group
|
|
77
275
|
// fallback, so it stays undefined otherwise). One writer for all subscriptions.
|
|
78
276
|
if (hasDatagrams(version)) {
|
|
79
277
|
this.#datagramWriter = DatagramStream.datagramWriter(quic);
|
|
80
278
|
}
|
|
81
279
|
}
|
|
82
|
-
/**
|
|
83
|
-
* Publishes a broadcast with any associated tracks.
|
|
84
|
-
* @param name - The broadcast to publish
|
|
85
|
-
*/
|
|
86
|
-
publish(path, broadcast) {
|
|
87
|
-
this.#broadcasts.mutate((broadcasts) => {
|
|
88
|
-
if (!broadcasts)
|
|
89
|
-
throw new Error("closed");
|
|
90
|
-
broadcasts.set(path, broadcast);
|
|
91
|
-
});
|
|
92
|
-
// Remove the broadcast from the lookup when it's closed, unless the path was republished.
|
|
93
|
-
void broadcast.closed.then(() => {
|
|
94
|
-
this.#broadcasts.mutate((broadcasts) => {
|
|
95
|
-
if (broadcasts?.get(path) === broadcast) {
|
|
96
|
-
broadcasts.delete(path);
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
280
|
/**
|
|
102
281
|
* Handles an announce interest message.
|
|
103
282
|
* @param msg - The announce interest message
|
|
@@ -107,99 +286,128 @@ export class Publisher {
|
|
|
107
286
|
*/
|
|
108
287
|
async runAnnounce(msg, stream) {
|
|
109
288
|
console.debug(`announce: prefix=${msg.prefix}`);
|
|
110
|
-
//
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
if (!broadcasts)
|
|
114
|
-
return; // closed
|
|
115
|
-
for (const name of broadcasts.keys()) {
|
|
116
|
-
const suffix = Path.stripPrefix(msg.prefix, name);
|
|
117
|
-
if (suffix === null)
|
|
118
|
-
continue;
|
|
119
|
-
console.debug(`announce: broadcast=${name} active=true`);
|
|
120
|
-
active.add(suffix);
|
|
121
|
-
}
|
|
289
|
+
// Keyed by suffix, valued by identity plus route, so a republish diffs as
|
|
290
|
+
// ended-then-active and a re-price as a restart.
|
|
291
|
+
let active = new Map();
|
|
122
292
|
// Lite06+: announce ids. Every active we send implicitly assigns the next
|
|
123
|
-
// per-stream ordinal; ended
|
|
293
|
+
// per-stream ordinal; ended/restart reference the id instead of repeating the path.
|
|
124
294
|
let nextAnnounceId = 0n;
|
|
125
295
|
const announceIds = new Map();
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
296
|
+
const wireHops = (route) => {
|
|
297
|
+
if (hasAnnounceOk(this.version))
|
|
298
|
+
return route.hops;
|
|
299
|
+
return [...route.hops, this.hop];
|
|
300
|
+
};
|
|
301
|
+
const announce = async (suffix, route) => {
|
|
302
|
+
console.debug(`announce: broadcast=${suffix} active=true`);
|
|
303
|
+
if (hasAnnounceId(this.version))
|
|
304
|
+
announceIds.set(suffix, nextAnnounceId++);
|
|
305
|
+
await encodeAnnounceBroadcast(stream.writer, { status: "active", suffix, hops: wireHops(route), cost: route.cost }, this.version);
|
|
306
|
+
};
|
|
307
|
+
const restart = async (suffix, route) => {
|
|
308
|
+
if (!hasAnnounceId(this.version)) {
|
|
309
|
+
await retract(suffix);
|
|
310
|
+
await announce(suffix, route);
|
|
311
|
+
return;
|
|
132
312
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
313
|
+
const id = announceIds.get(suffix);
|
|
314
|
+
if (id === undefined) {
|
|
315
|
+
await announce(suffix, route);
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
console.debug(`announce: broadcast=${suffix} restart=true`);
|
|
319
|
+
await encodeAnnounceBroadcast(stream.writer, { status: "restart", id, hops: wireHops(route), cost: route.cost }, this.version);
|
|
320
|
+
};
|
|
321
|
+
// Lite06+ retracts by announce id; older versions repeat the path (ended announces
|
|
322
|
+
// don't need hops).
|
|
323
|
+
const retract = async (suffix) => {
|
|
324
|
+
console.debug(`announce: broadcast=${suffix} active=false`);
|
|
325
|
+
if (!hasAnnounceId(this.version)) {
|
|
326
|
+
await encodeAnnounceBroadcast(stream.writer, { status: "ended", suffix }, this.version);
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
const id = announceIds.get(suffix);
|
|
330
|
+
announceIds.delete(suffix);
|
|
331
|
+
if (id === undefined)
|
|
332
|
+
return; // never announced
|
|
333
|
+
await encodeAnnounceBroadcast(stream.writer, { status: "endedId", id }, this.version);
|
|
334
|
+
};
|
|
335
|
+
// Subscribe BEFORE writing anything: every encode below awaits the wire, and a publish
|
|
336
|
+
// landing in that window only notifies the listeners already registered. One created
|
|
337
|
+
// afterwards would sleep through it, leaving the change unannounced until something
|
|
338
|
+
// unrelated moved.
|
|
339
|
+
// TODO Make a better helper within Signals.
|
|
340
|
+
let dispose;
|
|
341
|
+
let changed = new Promise((resolve) => {
|
|
342
|
+
dispose = this.#advertised.changed(resolve);
|
|
343
|
+
});
|
|
344
|
+
try {
|
|
345
|
+
const initial = this.#advertised.peek();
|
|
346
|
+
if (!initial)
|
|
347
|
+
return; // closed
|
|
348
|
+
for (const [name, snap] of presented(msg.prefix, initial)) {
|
|
349
|
+
active.set(name, snap);
|
|
350
|
+
}
|
|
351
|
+
switch (this.version) {
|
|
352
|
+
case Version.DRAFT_01:
|
|
353
|
+
case Version.DRAFT_02: {
|
|
354
|
+
for (const suffix of active.keys()) {
|
|
355
|
+
console.debug(`announce: broadcast=${suffix} active=true`);
|
|
138
356
|
}
|
|
357
|
+
const init = new AnnounceInit([...active.keys()]);
|
|
358
|
+
await init.encode(stream.writer, this.version);
|
|
139
359
|
break;
|
|
140
360
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
announceIds.set(suffix, nextAnnounceId++);
|
|
361
|
+
default: {
|
|
362
|
+
if (!hasAnnounceOk(this.version)) {
|
|
363
|
+
for (const [suffix, snap] of active) {
|
|
364
|
+
await announce(suffix, snap.route);
|
|
365
|
+
}
|
|
366
|
+
break;
|
|
148
367
|
}
|
|
149
|
-
|
|
368
|
+
const ok = new AnnounceOk(this.hop, active.size);
|
|
369
|
+
await ok.encode(stream.writer, this.version);
|
|
370
|
+
for (const [suffix, snap] of active) {
|
|
371
|
+
await announce(suffix, snap.route);
|
|
372
|
+
}
|
|
373
|
+
break;
|
|
150
374
|
}
|
|
151
|
-
break;
|
|
152
375
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
const newActive = new Set();
|
|
169
|
-
for (const name of broadcasts.keys()) {
|
|
170
|
-
const suffix = Path.stripPrefix(msg.prefix, name);
|
|
171
|
-
if (suffix === null)
|
|
172
|
-
continue; // Not our prefix.
|
|
173
|
-
newActive.add(suffix);
|
|
174
|
-
}
|
|
175
|
-
// Announce any new broadcasts. Lite05+ reports our origin once via AnnounceOk, so
|
|
176
|
-
// the subscriber stamps it onto each hop chain; older versions stamp it here.
|
|
177
|
-
for (const added of newActive.difference(active)) {
|
|
178
|
-
console.debug(`announce: broadcast=${added} active=true`);
|
|
179
|
-
const hops = hasAnnounceOk(this.version) ? [] : [this.origin];
|
|
180
|
-
if (hasAnnounceId(this.version)) {
|
|
181
|
-
announceIds.set(added, nextAnnounceId++);
|
|
376
|
+
for (;;) {
|
|
377
|
+
const advertised = await Promise.race([changed, stream.reader.closed]);
|
|
378
|
+
dispose();
|
|
379
|
+
if (!advertised)
|
|
380
|
+
break;
|
|
381
|
+
// Re-arm before reading, so an advertise that lands while we write is not lost.
|
|
382
|
+
changed = new Promise((resolve) => {
|
|
383
|
+
dispose = this.#advertised.changed(resolve);
|
|
384
|
+
});
|
|
385
|
+
const latest = this.#advertised.peek();
|
|
386
|
+
if (!latest)
|
|
387
|
+
break;
|
|
388
|
+
const updated = new Map();
|
|
389
|
+
for (const [name, snap] of presented(msg.prefix, latest)) {
|
|
390
|
+
updated.set(name, snap);
|
|
182
391
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
for (const removed of active.difference(newActive)) {
|
|
188
|
-
console.debug(`announce: broadcast=${removed} active=false`);
|
|
189
|
-
if (hasAnnounceId(this.version)) {
|
|
190
|
-
const id = announceIds.get(removed);
|
|
191
|
-
announceIds.delete(removed);
|
|
192
|
-
if (id === undefined)
|
|
193
|
-
continue; // never announced
|
|
194
|
-
await encodeAnnounceBroadcast(stream.writer, { status: "endedId", id }, this.version);
|
|
392
|
+
for (const [suffix, snap] of active) {
|
|
393
|
+
const cur = updated.get(suffix);
|
|
394
|
+
if (!cur || cur.identity !== snap.identity)
|
|
395
|
+
await retract(suffix);
|
|
195
396
|
}
|
|
196
|
-
|
|
197
|
-
|
|
397
|
+
for (const [suffix, snap] of updated) {
|
|
398
|
+
const prev = active.get(suffix);
|
|
399
|
+
if (!prev || prev.identity !== snap.identity) {
|
|
400
|
+
await announce(suffix, snap.route);
|
|
401
|
+
}
|
|
402
|
+
else if (!routesEqual(prev.route, snap.route)) {
|
|
403
|
+
await restart(suffix, snap.route);
|
|
404
|
+
}
|
|
198
405
|
}
|
|
406
|
+
active = updated;
|
|
199
407
|
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
408
|
+
}
|
|
409
|
+
finally {
|
|
410
|
+
dispose();
|
|
203
411
|
}
|
|
204
412
|
}
|
|
205
413
|
/**
|
|
@@ -210,27 +418,37 @@ export class Publisher {
|
|
|
210
418
|
* @internal
|
|
211
419
|
*/
|
|
212
420
|
async runSubscribe(msg, stream) {
|
|
213
|
-
|
|
214
|
-
|
|
421
|
+
let front;
|
|
422
|
+
try {
|
|
423
|
+
front =
|
|
424
|
+
this.#broadcasts.peek()?.get(msg.broadcast) ??
|
|
425
|
+
(this.#publish && (await wireOf(this.#publish).demand(msg.broadcast)));
|
|
426
|
+
}
|
|
427
|
+
catch (err) {
|
|
428
|
+
stream.writer.reset(error(err));
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
if (!front) {
|
|
215
432
|
console.debug(`publish unknown: broadcast=${msg.broadcast}`);
|
|
216
|
-
stream.writer.reset(new
|
|
433
|
+
stream.writer.reset(new NotFound(`broadcast ${msg.broadcast}`));
|
|
217
434
|
return;
|
|
218
435
|
}
|
|
219
|
-
const
|
|
436
|
+
const endGroup = exclusiveGroupEnd(msg.endGroup);
|
|
437
|
+
const track = wireOf(front).subscribe(msg.track, {
|
|
220
438
|
priority: msg.priority,
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
439
|
+
maxAge: Milli(servingMaxAge(this.version, msg.maxAge)),
|
|
440
|
+
groups: {
|
|
441
|
+
start: msg.startGroup === undefined ? undefined : { included: msg.startGroup },
|
|
442
|
+
end: endGroup === undefined ? undefined : { excluded: endGroup },
|
|
443
|
+
},
|
|
225
444
|
});
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
track.startAt(startGroup);
|
|
229
|
-
track.endAt(msg.endGroup);
|
|
445
|
+
positionCursor(track, this.version, msg.startGroup);
|
|
446
|
+
hooks.replaceGroups(track, { end: endGroup === undefined ? undefined : { excluded: endGroup } });
|
|
230
447
|
// The best-effort datagram loop, started once serving begins. It parks when the
|
|
231
448
|
// track finishes (recvDatagram returns undefined), so #runTrack alone ends the
|
|
232
449
|
// subscription; awaited during teardown so it doesn't outlive the subscription.
|
|
233
450
|
let datagrams = Promise.resolve();
|
|
451
|
+
let controls;
|
|
234
452
|
try {
|
|
235
453
|
let timescale = Timescale.MILLI;
|
|
236
454
|
if (supportsTrackStream(this.version)) {
|
|
@@ -250,51 +468,57 @@ export class Publisher {
|
|
|
250
468
|
// Older drafts acknowledge with SUBSCRIBE_OK and stream frames verbatim.
|
|
251
469
|
const ok = new SubscribeOk({
|
|
252
470
|
priority: msg.priority,
|
|
253
|
-
|
|
254
|
-
maxLatency: msg.maxLatency,
|
|
471
|
+
maxAge: msg.maxAge,
|
|
255
472
|
startGroup: msg.startGroup,
|
|
256
473
|
endGroup: msg.endGroup,
|
|
257
474
|
});
|
|
258
475
|
await encodeSubscribeResponse(stream.writer, { ok }, this.version);
|
|
259
476
|
}
|
|
260
477
|
console.debug(`publish ok: broadcast=${msg.broadcast} track=${track.name}`);
|
|
261
|
-
const serving = this.#runTrack(msg.id, msg.broadcast, track, stream.writer, timescale);
|
|
262
478
|
// Serve datagrams concurrently with groups whenever the transport carries them
|
|
263
479
|
// (the writer exists iff so). No group fallback: otherwise they simply aren't sent.
|
|
264
480
|
if (this.#datagramWriter) {
|
|
265
481
|
datagrams = this.#runDatagrams(msg.id, track, timescale);
|
|
266
482
|
}
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
console.debug(`subscribe update: broadcast=${msg.broadcast} track=${track.name}`);
|
|
483
|
+
controls = new SubscriptionControls({
|
|
484
|
+
reader: stream.reader,
|
|
485
|
+
writer: stream.writer,
|
|
486
|
+
version: this.version,
|
|
487
|
+
apply: (update) => {
|
|
488
|
+
const end = exclusiveGroupEnd(update.endGroup);
|
|
274
489
|
track.update({
|
|
275
|
-
priority:
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
490
|
+
priority: update.priority,
|
|
491
|
+
maxAge: Milli(servingMaxAge(this.version, update.maxAge)),
|
|
492
|
+
groups: {
|
|
493
|
+
start: update.startGroup === undefined ? undefined : { included: update.startGroup },
|
|
494
|
+
end: end === undefined ? undefined : { excluded: end },
|
|
495
|
+
},
|
|
280
496
|
});
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
497
|
+
},
|
|
498
|
+
});
|
|
499
|
+
await this.#runTrack(track, stream.writer, controls, {
|
|
500
|
+
sub: msg.id,
|
|
501
|
+
broadcast: msg.broadcast,
|
|
502
|
+
timescale,
|
|
503
|
+
bounds: {
|
|
504
|
+
startGroup: msg.startGroup,
|
|
505
|
+
startFrame: msg.startFrame,
|
|
506
|
+
endGroup: msg.endGroup,
|
|
507
|
+
endFrame: msg.endFrame,
|
|
508
|
+
},
|
|
509
|
+
});
|
|
286
510
|
console.debug(`publish done: broadcast=${msg.broadcast} track=${track.name}`);
|
|
287
511
|
stream.close();
|
|
288
512
|
track.close();
|
|
289
|
-
// track.close ends the datagram loop
|
|
290
|
-
await datagrams;
|
|
513
|
+
// Closing the stream ends the decoder and track.close ends the datagram loop.
|
|
514
|
+
await Promise.all([datagrams, controls.decoding]);
|
|
291
515
|
}
|
|
292
516
|
catch (err) {
|
|
293
517
|
const e = error(err);
|
|
294
518
|
console.warn(`publish error: broadcast=${msg.broadcast} track=${track.name} error=${reason(e)}`);
|
|
295
519
|
track.close(e);
|
|
296
520
|
stream.abort(e);
|
|
297
|
-
await datagrams;
|
|
521
|
+
await Promise.all([datagrams, controls?.decoding]);
|
|
298
522
|
}
|
|
299
523
|
}
|
|
300
524
|
/**
|
|
@@ -307,10 +531,19 @@ export class Publisher {
|
|
|
307
531
|
stream.writer.reset(new Error("fetch requires moq-lite-05 or newer"));
|
|
308
532
|
return;
|
|
309
533
|
}
|
|
310
|
-
|
|
311
|
-
|
|
534
|
+
let front;
|
|
535
|
+
try {
|
|
536
|
+
front =
|
|
537
|
+
this.#broadcasts.peek()?.get(msg.broadcast) ??
|
|
538
|
+
(this.#publish && (await wireOf(this.#publish).demand(msg.broadcast)));
|
|
539
|
+
}
|
|
540
|
+
catch (err) {
|
|
541
|
+
stream.writer.reset(error(err));
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
if (!front) {
|
|
312
545
|
console.debug(`fetch unknown: broadcast=${msg.broadcast}`);
|
|
313
|
-
stream.writer.reset(new
|
|
546
|
+
stream.writer.reset(new NotFound(`broadcast ${msg.broadcast}`));
|
|
314
547
|
return;
|
|
315
548
|
}
|
|
316
549
|
// The subscriber opened this stream, so its send order only ranked the request. Rank the
|
|
@@ -318,10 +551,15 @@ export class Publisher {
|
|
|
318
551
|
stream.writer.setPriority(sendOrder({ priority: msg.priority }));
|
|
319
552
|
let group;
|
|
320
553
|
try {
|
|
321
|
-
// The timescale is immutable, so serve exactly what TRACK_INFO advertised.
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
await
|
|
554
|
+
// The timescale is immutable, so serve exactly what TRACK_INFO advertised. Both
|
|
555
|
+
// come off the same front, so the metadata and the frames are one generation.
|
|
556
|
+
const info = await this.#resolveTrackInfo(front, msg.track);
|
|
557
|
+
group = await wireOf(front).fetchGroup(msg.track, msg.group, { priority: msg.priority });
|
|
558
|
+
await this.#runFetchGroup(group, stream.writer, {
|
|
559
|
+
timescale: Timescale(info.timescale),
|
|
560
|
+
start: msg.startFrame,
|
|
561
|
+
end: msg.endFrame,
|
|
562
|
+
});
|
|
325
563
|
console.debug(`fetch done: broadcast=${msg.broadcast} track=${msg.track} group=${msg.group}`);
|
|
326
564
|
stream.close();
|
|
327
565
|
group.close();
|
|
@@ -342,70 +580,126 @@ export class Publisher {
|
|
|
342
580
|
*
|
|
343
581
|
* @internal
|
|
344
582
|
*/
|
|
345
|
-
async #runTrack(
|
|
583
|
+
async #runTrack(track, stream, controls, serving) {
|
|
584
|
+
const { sub, broadcast, timescale, bounds } = serving;
|
|
346
585
|
// Lite-05+ resolves the range on the subscribe stream: SUBSCRIBE_START once the
|
|
347
586
|
// first group is known, SUBSCRIBE_END when the track finishes.
|
|
348
587
|
const emitRange = supportsTrackStream(this.version);
|
|
349
588
|
let startSent = false;
|
|
350
|
-
|
|
351
|
-
//
|
|
352
|
-
//
|
|
353
|
-
|
|
589
|
+
let endSent = false;
|
|
590
|
+
// The track's exclusive final boundary. A Rust subscriber feeds SUBSCRIBE_END
|
|
591
|
+
// straight into finish_at, so it must name the track's boundary (which counts
|
|
592
|
+
// datagram sequences too), not the delivered range: a subscription cap can hold
|
|
593
|
+
// produced groups back. The latest() fallback covers a subscription torn down
|
|
594
|
+
// before the producer declared it.
|
|
595
|
+
const boundary = () => track.final() ?? (track.latest() ?? -1) + 1;
|
|
596
|
+
// SUBSCRIBE_END names that boundary, which a cap can hold groups back from, so it goes
|
|
597
|
+
// out as soon as the producer finishes and the subscription keeps serving whatever a
|
|
598
|
+
// later cap raise releases (see the Rust publisher's Recv::Boundary).
|
|
599
|
+
const sendEnd = async () => {
|
|
600
|
+
endSent = true;
|
|
601
|
+
if (emitRange) {
|
|
602
|
+
return controls.response(encodeSubscribeResponse(stream, { end: new SubscribeEnd(boundary()) }, this.version));
|
|
603
|
+
}
|
|
604
|
+
return true;
|
|
605
|
+
};
|
|
354
606
|
// One ranking for the whole subscription, shared by every group it serves.
|
|
355
607
|
const priority = new Priority(track);
|
|
356
608
|
// Cancels groups still queued for a stream slot. Only the subscriber leaving counts:
|
|
357
|
-
// a track that ran out of groups still has to flush the ones already queued, and
|
|
358
|
-
//
|
|
609
|
+
// a track that ran out of groups still has to flush the ones already queued, and the
|
|
610
|
+
// caller FINs the subscribe stream to say so.
|
|
359
611
|
let finished = false;
|
|
360
612
|
let unsubscribe;
|
|
361
613
|
const unsubscribed = new Promise((resolve) => {
|
|
362
614
|
unsubscribe = resolve;
|
|
363
615
|
});
|
|
364
|
-
void stream.closed.then(() => {
|
|
365
|
-
if (!finished)
|
|
366
|
-
unsubscribe();
|
|
367
|
-
},
|
|
368
|
-
// A reset is always the peer.
|
|
369
|
-
() => unsubscribe());
|
|
370
616
|
try {
|
|
371
617
|
for (;;) {
|
|
372
|
-
//
|
|
373
|
-
//
|
|
374
|
-
//
|
|
375
|
-
//
|
|
376
|
-
const
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
618
|
+
// Control before data, matching the Rust publisher: every control decoded while
|
|
619
|
+
// the loop was parked applies to the next pop, never to one already made. This
|
|
620
|
+
// drain is synchronous, so nothing the decoder holds can land between the pop
|
|
621
|
+
// below and its frame range.
|
|
622
|
+
const control = controls.take();
|
|
623
|
+
if (control) {
|
|
624
|
+
switch (control.kind) {
|
|
625
|
+
case "done":
|
|
626
|
+
// The subscriber left. Its queued groups are pointless now, which
|
|
627
|
+
// the finally below acts on since `finished` stays false.
|
|
628
|
+
return;
|
|
629
|
+
case "error":
|
|
630
|
+
throw control.error;
|
|
631
|
+
case "update": {
|
|
632
|
+
const update = control.update;
|
|
633
|
+
console.debug(`subscribe update: broadcast=${broadcast} track=${track.name}`);
|
|
634
|
+
hooks.replaceGroups(track, {
|
|
635
|
+
start: update.startGroup === undefined ? undefined : { included: update.startGroup },
|
|
636
|
+
end: update.endGroup === undefined ? undefined : { included: update.endGroup },
|
|
637
|
+
});
|
|
638
|
+
bounds.startGroup = update.startGroup;
|
|
639
|
+
bounds.startFrame = update.startFrame;
|
|
640
|
+
bounds.endGroup = update.endGroup;
|
|
641
|
+
bounds.endFrame = update.endFrame;
|
|
642
|
+
await yieldToControls();
|
|
643
|
+
continue;
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
// Exactly-once arrival-order serving. This synchronous package-internal pop
|
|
648
|
+
// and frameRange call are the operation's linearization point.
|
|
649
|
+
const recv = hooks.tryRecvGroup(track);
|
|
650
|
+
switch (recv.kind) {
|
|
651
|
+
case "error":
|
|
652
|
+
throw recv.error;
|
|
653
|
+
case "idle":
|
|
654
|
+
await waitForSubscription(controls, track);
|
|
655
|
+
continue;
|
|
656
|
+
case "boundary":
|
|
657
|
+
// The producer finished but is still holding groups above the cap.
|
|
658
|
+
// Declare the boundary, then wait for an update to release them.
|
|
659
|
+
if (!endSent) {
|
|
660
|
+
if (!(await sendEnd()))
|
|
661
|
+
return;
|
|
662
|
+
continue;
|
|
663
|
+
}
|
|
664
|
+
await waitForSubscription(controls, track);
|
|
665
|
+
continue;
|
|
666
|
+
case "done":
|
|
667
|
+
if (!endSent) {
|
|
668
|
+
if (!(await sendEnd()))
|
|
669
|
+
return;
|
|
670
|
+
continue;
|
|
671
|
+
}
|
|
672
|
+
finished = true;
|
|
673
|
+
return;
|
|
381
674
|
}
|
|
675
|
+
const group = recv.group;
|
|
676
|
+
const range = frameRange(bounds, group.sequence);
|
|
382
677
|
if (emitRange && !startSent) {
|
|
383
678
|
startSent = true;
|
|
384
679
|
// SUBSCRIBE_START promises nothing below this sequence will be delivered.
|
|
385
680
|
// Arrival-order serving could later surface a straggler below the first
|
|
386
681
|
// group, so pin the floor to what was announced.
|
|
387
|
-
|
|
388
|
-
|
|
682
|
+
hooks.replaceGroups(track, {
|
|
683
|
+
start: { included: group.sequence },
|
|
684
|
+
end: bounds.endGroup === undefined ? undefined : { included: bounds.endGroup },
|
|
685
|
+
});
|
|
686
|
+
if (!(await controls.response(encodeSubscribeResponse(stream, { start: new SubscribeStart(group.sequence) }, this.version))))
|
|
687
|
+
return;
|
|
389
688
|
}
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
689
|
+
void this.#runGroup({
|
|
690
|
+
sub,
|
|
691
|
+
group,
|
|
692
|
+
timescale,
|
|
693
|
+
priority,
|
|
694
|
+
unsubscribed,
|
|
695
|
+
start: range.start,
|
|
696
|
+
end: range.end,
|
|
697
|
+
});
|
|
395
698
|
}
|
|
396
|
-
console.debug(`publish close: broadcast=${broadcast} track=${track.name}`);
|
|
397
|
-
finished = true;
|
|
398
|
-
track.close();
|
|
399
|
-
stream.close();
|
|
400
|
-
}
|
|
401
|
-
catch (err) {
|
|
402
|
-
const e = error(err);
|
|
403
|
-
console.warn(`publish error: broadcast=${broadcast} track=${track.name} error=${reason(e)}`);
|
|
404
|
-
unsubscribe();
|
|
405
|
-
track.close(e);
|
|
406
|
-
stream.reset(e);
|
|
407
699
|
}
|
|
408
700
|
finally {
|
|
701
|
+
if (!finished)
|
|
702
|
+
unsubscribe();
|
|
409
703
|
priority.close();
|
|
410
704
|
}
|
|
411
705
|
}
|
|
@@ -416,7 +710,11 @@ export class Publisher {
|
|
|
416
710
|
*/
|
|
417
711
|
async runTrackInfo(msg, stream) {
|
|
418
712
|
try {
|
|
419
|
-
const
|
|
713
|
+
const front = this.#broadcasts.peek()?.get(msg.broadcast) ??
|
|
714
|
+
(this.#publish && (await wireOf(this.#publish).demand(msg.broadcast)));
|
|
715
|
+
if (!front)
|
|
716
|
+
throw new NotFound(`broadcast ${msg.broadcast}`);
|
|
717
|
+
const info = await this.#resolveTrackInfo(front, msg.track);
|
|
420
718
|
await info.encode(stream.writer, this.version);
|
|
421
719
|
console.debug(`track info: broadcast=${msg.broadcast} track=${msg.track}`);
|
|
422
720
|
stream.close();
|
|
@@ -427,34 +725,33 @@ export class Publisher {
|
|
|
427
725
|
}
|
|
428
726
|
}
|
|
429
727
|
// Resolve (and cache) a track's immutable TRACK_INFO by asking the application.
|
|
430
|
-
// `
|
|
431
|
-
//
|
|
432
|
-
//
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
728
|
+
// `resolveTrackInfo` triggers a TrackRequest the app answers with accept(TrackInfo);
|
|
729
|
+
// only the immutable properties are needed (not the groups). Cached because they're
|
|
730
|
+
// fixed for the track's lifetime. Rejects if the track is unavailable.
|
|
731
|
+
#resolveTrackInfo(front, track) {
|
|
732
|
+
let tracks = this.#trackInfo.get(front);
|
|
733
|
+
if (!tracks) {
|
|
734
|
+
tracks = new Map();
|
|
735
|
+
this.#trackInfo.set(front, tracks);
|
|
736
|
+
}
|
|
737
|
+
const cached = tracks.get(track);
|
|
437
738
|
if (cached)
|
|
438
739
|
return cached;
|
|
439
740
|
const pending = (async () => {
|
|
440
|
-
const
|
|
441
|
-
if (!published)
|
|
442
|
-
throw new Error("not found");
|
|
443
|
-
const info = await published.track(track).info();
|
|
741
|
+
const info = await wireOf(front).resolveTrackInfo(track);
|
|
444
742
|
return new TrackInfoMessage({
|
|
445
743
|
priority: info.priority,
|
|
446
|
-
|
|
447
|
-
// Publisher Max Latency: the publisher's retention bound, advertised so
|
|
744
|
+
// Publisher Max Age: the publisher's retention bound, advertised so
|
|
448
745
|
// relays re-serve with the same window.
|
|
449
|
-
|
|
746
|
+
maxAge: info.maxAge,
|
|
450
747
|
// Lite05 mandates per-frame timestamps. Advertise the track's timescale;
|
|
451
748
|
// `#runGroup` emits each frame converted to it.
|
|
452
749
|
timescale: info.timescale,
|
|
453
750
|
});
|
|
454
751
|
})();
|
|
455
752
|
// Don't poison the cache on failure: a later request may succeed.
|
|
456
|
-
pending.catch(() =>
|
|
457
|
-
|
|
753
|
+
pending.catch(() => tracks.delete(track));
|
|
754
|
+
tracks.set(track, pending);
|
|
458
755
|
return pending;
|
|
459
756
|
}
|
|
460
757
|
/**
|
|
@@ -494,9 +791,17 @@ export class Publisher {
|
|
|
494
791
|
}
|
|
495
792
|
// Serialize a fetched group's frames onto the FETCH stream as bare records: each a
|
|
496
793
|
// zigzag-delta timestamp (at the track's advertised timescale) followed by size + bytes.
|
|
497
|
-
async #runFetchGroup(group, stream, timescale) {
|
|
794
|
+
async #runFetchGroup(group, stream, { timescale, start: startFrame, end: endFrame }) {
|
|
795
|
+
// The response carries no header, so the receiver numbers the first frame it gets
|
|
796
|
+
// as `startFrame`. Skipping the head here is the only thing keeping those numbers
|
|
797
|
+
// honest; a group that ends before we reach it can't be served at all.
|
|
798
|
+
for (let i = 0; i < startFrame; i++) {
|
|
799
|
+
if (!(await Promise.race([group.readFrame(), stream.closed]))) {
|
|
800
|
+
throw new Error(`fetch group ended at frame ${i}, before the requested start ${startFrame}`);
|
|
801
|
+
}
|
|
802
|
+
}
|
|
498
803
|
let prevTs = 0n;
|
|
499
|
-
for (
|
|
804
|
+
for (let index = startFrame; endFrame === undefined || index <= endFrame; index++) {
|
|
500
805
|
const frame = await Promise.race([group.readFrame(), stream.closed]);
|
|
501
806
|
if (!frame)
|
|
502
807
|
break;
|
|
@@ -513,8 +818,10 @@ export class Publisher {
|
|
|
513
818
|
* @internal
|
|
514
819
|
*/
|
|
515
820
|
async #runGroup(options) {
|
|
516
|
-
const { sub, group, timescale, priority, unsubscribed } = options;
|
|
517
|
-
|
|
821
|
+
const { sub, group, timescale, priority, unsubscribed, start: startFrame, end: endFrame } = options;
|
|
822
|
+
// This model holds whole groups, so frame `startFrame` is always reachable unless
|
|
823
|
+
// the group ends first. Declaring it up front keeps the stream self-describing.
|
|
824
|
+
const msg = new GroupMessage({ subscribe: sub, sequence: group.sequence, frameStart: startFrame });
|
|
518
825
|
try {
|
|
519
826
|
// The transport drains streams by send order, so this is what makes a high-priority
|
|
520
827
|
// track (and a newer group within it) win the link when there isn't room for both.
|
|
@@ -538,24 +845,48 @@ export class Publisher {
|
|
|
538
845
|
// A SUBSCRIBE_UPDATE re-ranks the subscription, so a group already on the wire
|
|
539
846
|
// follows it too rather than keeping a stale rank until it finishes.
|
|
540
847
|
priority.add(stream, group.sequence);
|
|
541
|
-
await
|
|
542
|
-
|
|
848
|
+
await hooks.guardGroup(group, (async () => {
|
|
849
|
+
await stream.u53(0); // stream type
|
|
850
|
+
await msg.encode(stream, this.version);
|
|
851
|
+
})());
|
|
543
852
|
// Lite05+ prefixes every frame with a zigzag-delta timestamp at the track's
|
|
544
853
|
// advertised timescale; older drafts omit it.
|
|
545
854
|
const timestamps = supportsTrackStream(this.version);
|
|
546
855
|
let prevTs = 0n;
|
|
856
|
+
// Whether the cursor ever reached the requested start, which decides how the
|
|
857
|
+
// end of the group is read below.
|
|
858
|
+
let reached = startFrame === 0;
|
|
547
859
|
for (;;) {
|
|
548
|
-
const
|
|
549
|
-
if (!
|
|
860
|
+
const read = await Promise.race([hooks.readGroupFrame(group), stream.closed]);
|
|
861
|
+
if (!read) {
|
|
862
|
+
// The group ended before the frame the subscriber asked to start
|
|
863
|
+
// at, so this publisher can't serve the range at all. FINning here
|
|
864
|
+
// would claim an empty group under that index; reset so it reads
|
|
865
|
+
// as the gap it is.
|
|
866
|
+
if (!reached)
|
|
867
|
+
throw new Error(`group ended before frame ${startFrame}`);
|
|
550
868
|
break;
|
|
551
|
-
if (timestamps) {
|
|
552
|
-
// Convert each frame to the track's advertised timescale.
|
|
553
|
-
const ts = BigInt(Math.round(frame.timestamp.as(timescale)));
|
|
554
|
-
await stream.u62(zigzag(ts - prevTs));
|
|
555
|
-
prevTs = ts;
|
|
556
869
|
}
|
|
557
|
-
|
|
558
|
-
|
|
870
|
+
try {
|
|
871
|
+
// Frames below the requested start were excluded, and the receiver
|
|
872
|
+
// numbers what it gets from `startFrame`.
|
|
873
|
+
if (read.sequence < startFrame)
|
|
874
|
+
continue;
|
|
875
|
+
if (endFrame !== undefined && read.sequence > endFrame)
|
|
876
|
+
break;
|
|
877
|
+
reached = true;
|
|
878
|
+
if (timestamps) {
|
|
879
|
+
// Convert each frame to the track's advertised timescale.
|
|
880
|
+
const ts = BigInt(Math.round(read.frame.timestamp.as(timescale)));
|
|
881
|
+
await hooks.guardGroup(group, stream.u62(zigzag(ts - prevTs)));
|
|
882
|
+
prevTs = ts;
|
|
883
|
+
}
|
|
884
|
+
await hooks.guardGroup(group, stream.u53(read.frame.payload.byteLength));
|
|
885
|
+
await hooks.guardGroup(group, stream.write(read.frame.payload));
|
|
886
|
+
}
|
|
887
|
+
finally {
|
|
888
|
+
read.complete();
|
|
889
|
+
}
|
|
559
890
|
}
|
|
560
891
|
stream.close();
|
|
561
892
|
group.close();
|
|
@@ -658,12 +989,8 @@ export class Publisher {
|
|
|
658
989
|
}
|
|
659
990
|
}
|
|
660
991
|
close() {
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
broadcast.close();
|
|
664
|
-
}
|
|
665
|
-
return undefined;
|
|
666
|
-
});
|
|
992
|
+
// The broadcasts belong to the origin, which outlives this session; closing here
|
|
993
|
+
// only drops the borrow. The peer sees the unannounce when the streams die.
|
|
667
994
|
// Release the datagram writer's lock so the stream can be torn down.
|
|
668
995
|
this.#datagramWriter?.releaseLock();
|
|
669
996
|
this.#datagramWriter = undefined;
|