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