@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/track.js
CHANGED
|
@@ -5,19 +5,18 @@
|
|
|
5
5
|
* @module
|
|
6
6
|
*/
|
|
7
7
|
import { Once, Signal } from "@moq/signals";
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const MAX_DATAGRAM_AGE_MS = 50;
|
|
8
|
+
import { GroupTooLarge, TooFarBehind } from "./error.js";
|
|
9
|
+
import { Producer as GroupProducer } from "./group.js";
|
|
10
|
+
import { groupBounds, hooks, } from "./internal.js";
|
|
11
|
+
import { Milli, Timescale } from "./time.js";
|
|
12
|
+
import { registerTrackConsumer } from "./wire.js";
|
|
13
|
+
// The largest delay setTimeout keeps: anything more truncates to a signed 32-bit int
|
|
14
|
+
// and fires right away.
|
|
15
|
+
const MAX_TIMEOUT_MS = 2 ** 31 - 1;
|
|
16
|
+
/** Default {@link Info.maxAge} window (milliseconds) when the publisher does not set one. */
|
|
17
|
+
export const DEFAULT_MAX_AGE_MS = Milli(5000);
|
|
18
|
+
/** Maximum buffered datagrams per subscriber; mirrors Rust's bounded send buffer. */
|
|
19
|
+
const MAX_DATAGRAMS = 64;
|
|
21
20
|
/**
|
|
22
21
|
* Sanity cap on a datagram payload: the QUIC DATAGRAM frame ceiling. The real limit is
|
|
23
22
|
* per-hop (the negotiated transport datagram size minus a small header) and oversize
|
|
@@ -30,30 +29,41 @@ const MAX_DATAGRAM_BYTES = 65535;
|
|
|
30
29
|
// millisecond is expected; ceil rather than round, because a budget shortened by rounding
|
|
31
30
|
// skips a group the subscriber still wants. Anything that is not a duration is refused
|
|
32
31
|
// here, where the field is named, rather than deep in the encoder.
|
|
33
|
-
function
|
|
32
|
+
function maxAgeMillis(value) {
|
|
34
33
|
if (!Number.isFinite(value) || value < 0) {
|
|
35
|
-
throw new RangeError(`
|
|
34
|
+
throw new RangeError(`maxAge must be a non-negative number of milliseconds: ${value}`);
|
|
35
|
+
}
|
|
36
|
+
const millis = Math.ceil(value);
|
|
37
|
+
if (!Number.isSafeInteger(millis)) {
|
|
38
|
+
throw new RangeError(`maxAge exceeds the safe integer millisecond range: ${value}`);
|
|
36
39
|
}
|
|
37
|
-
return
|
|
40
|
+
return Milli(millis);
|
|
41
|
+
}
|
|
42
|
+
function priorityByte(value) {
|
|
43
|
+
if (!Number.isInteger(value) || value < 0 || value > 255) {
|
|
44
|
+
throw new RangeError(`priority must be an integer in 0..=255: ${value}`);
|
|
45
|
+
}
|
|
46
|
+
return value;
|
|
38
47
|
}
|
|
39
48
|
/** Fill in any unset {@link Info} fields with their defaults. */
|
|
40
49
|
export function infoDefaults(info = {}) {
|
|
41
50
|
return {
|
|
42
|
-
timescale: info.timescale ?? Timescale.MILLI,
|
|
43
|
-
|
|
44
|
-
priority: info.priority ?? 0,
|
|
45
|
-
ordered: info.ordered ?? false,
|
|
51
|
+
timescale: Timescale(info.timescale ?? Timescale.MILLI),
|
|
52
|
+
maxAge: maxAgeMillis(info.maxAge ?? DEFAULT_MAX_AGE_MS),
|
|
53
|
+
priority: priorityByte(info.priority ?? 0),
|
|
46
54
|
};
|
|
47
55
|
}
|
|
48
56
|
// Materialize the defaults at the model boundary so every layer observes a complete
|
|
49
57
|
// subscription rather than interpreting an omitted field differently.
|
|
50
58
|
function subscriptionDefaults(subscription = {}) {
|
|
59
|
+
const bounds = groupBounds(subscription.groups ?? {});
|
|
51
60
|
return {
|
|
52
|
-
priority: subscription.priority ?? 0,
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
61
|
+
priority: priorityByte(subscription.priority ?? 0),
|
|
62
|
+
maxAge: maxAgeMillis(subscription.maxAge ?? Milli.zero),
|
|
63
|
+
groups: {
|
|
64
|
+
start: subscription.groups?.start === undefined ? undefined : { included: bounds.start },
|
|
65
|
+
end: bounds.end === undefined ? undefined : { excluded: bounds.end },
|
|
66
|
+
},
|
|
57
67
|
};
|
|
58
68
|
}
|
|
59
69
|
// Aggregate the preferences of every live subscriber, matching Rust's Subscription::poll_combined.
|
|
@@ -68,25 +78,22 @@ function combineSubscriptions(states) {
|
|
|
68
78
|
continue;
|
|
69
79
|
}
|
|
70
80
|
combined.priority = Math.max(combined.priority ?? 0, subscription.priority ?? 0);
|
|
71
|
-
combined.
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
else {
|
|
83
|
-
combined.endGroup = Math.max(combined.endGroup, subscription.endGroup);
|
|
84
|
-
}
|
|
81
|
+
combined.maxAge = Milli(Math.max(combined.maxAge ?? Milli.zero, subscription.maxAge ?? Milli.zero));
|
|
82
|
+
// A floor only restricts, so a subscriber without one clears the aggregate:
|
|
83
|
+
// its budget may reach below any floor the others set.
|
|
84
|
+
const a = groupBounds(combined.groups ?? {});
|
|
85
|
+
const b = groupBounds(subscription.groups ?? {});
|
|
86
|
+
combined.groups = {
|
|
87
|
+
start: combined.groups?.start === undefined || subscription.groups?.start === undefined
|
|
88
|
+
? undefined
|
|
89
|
+
: { included: Math.min(a.start, b.start) },
|
|
90
|
+
end: a.end === undefined || b.end === undefined ? undefined : { excluded: Math.max(a.end, b.end) },
|
|
91
|
+
};
|
|
85
92
|
}
|
|
86
93
|
return combined;
|
|
87
94
|
}
|
|
88
95
|
/**
|
|
89
|
-
* A request for a track the peer wants,
|
|
96
|
+
* A request for a track the peer wants, delivered to the publishing wire layer.
|
|
90
97
|
*
|
|
91
98
|
* Created internally by the broadcast when a subscription (or info lookup) needs a track
|
|
92
99
|
* served; answer it with {@link accept} or {@link reject}.
|
|
@@ -108,6 +115,7 @@ export class Request {
|
|
|
108
115
|
}
|
|
109
116
|
static {
|
|
110
117
|
hooks.makeRequest = (options) => new Request(options);
|
|
118
|
+
hooks.pendingTrackProducer = (request) => request.#producer;
|
|
111
119
|
}
|
|
112
120
|
/** The aggregate subscription requested for this track. */
|
|
113
121
|
get subscription() {
|
|
@@ -142,7 +150,17 @@ export class Consumer {
|
|
|
142
150
|
this.name = name;
|
|
143
151
|
this.#broadcast = broadcast;
|
|
144
152
|
}
|
|
145
|
-
|
|
153
|
+
static {
|
|
154
|
+
registerTrackConsumer((name, broadcast) => new Consumer(name, broadcast));
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Open a live subscription to the track.
|
|
158
|
+
*
|
|
159
|
+
* The cursor starts at the group the subscription named (its floor), or 0.
|
|
160
|
+
* {@link Subscription.maxAge} is what asks for data: delivery skips everything above
|
|
161
|
+
* the floor that the budget convicts, so the default budget of zero delivers only the
|
|
162
|
+
* latest group and a larger one reaches back over what it can still use.
|
|
163
|
+
*/
|
|
146
164
|
subscribe(options) {
|
|
147
165
|
return this.#broadcast.subscribe(this.name, options);
|
|
148
166
|
}
|
|
@@ -161,9 +179,23 @@ class TrackState {
|
|
|
161
179
|
/** The producer fanning into this sink, so a subscriber can mint a sibling of itself. */
|
|
162
180
|
producer;
|
|
163
181
|
groups = new Signal([]);
|
|
164
|
-
|
|
182
|
+
// Every group still in the producer's replay cache, including groups this
|
|
183
|
+
// subscriber already consumed, paired with its source queue time. Drift anchors
|
|
184
|
+
// have the same lifetime as content.
|
|
185
|
+
timeline = new Map();
|
|
186
|
+
// First timestamps mutate group state rather than track state, so held groups
|
|
187
|
+
// watch this revision as well as arrivals when enforcing latency after handoff.
|
|
188
|
+
timelineChanged = new Signal(0);
|
|
189
|
+
/** Best-effort datagram channel, parallel to {@link groups}; a bounded send buffer per subscriber. */
|
|
165
190
|
datagrams = new Signal([]);
|
|
166
191
|
latest;
|
|
192
|
+
/**
|
|
193
|
+
* The exclusive final boundary, declared by {@link Producer.finishAt} or stamped by a
|
|
194
|
+
* clean close as one past the highest sequence produced. Groups and datagrams share the
|
|
195
|
+
* namespace, so this can exceed `latest + 1` (which only tracks groups). Mirrors the
|
|
196
|
+
* Rust `final_sequence`.
|
|
197
|
+
*/
|
|
198
|
+
final = new Signal(undefined);
|
|
167
199
|
closed = new Once();
|
|
168
200
|
update;
|
|
169
201
|
/** Resolved once the producer commits the immutable properties. */
|
|
@@ -207,6 +239,11 @@ function bindProducer(name, producer, sequences) {
|
|
|
207
239
|
bindProducerSequence(producer, shared);
|
|
208
240
|
}
|
|
209
241
|
let bindProducerSequence;
|
|
242
|
+
// The sequence-order cursor lives inside `Subscriber` (it shares the group buffer and the
|
|
243
|
+
// drift anchor with the arrival cursor), so `Ordered` reaches it through this bridge,
|
|
244
|
+
// assigned in the class's static block.
|
|
245
|
+
let makeOrdered;
|
|
246
|
+
let ordered_;
|
|
210
247
|
// Constructs a Subscriber from within this module without exposing a public
|
|
211
248
|
// constructor that would leak the unexported TrackState. Assigned in the class's
|
|
212
249
|
// static block.
|
|
@@ -218,7 +255,7 @@ let makeSubscriber;
|
|
|
218
255
|
* subscription the publisher serves from it) gets an independent
|
|
219
256
|
* {@link Subscriber} that receives a full copy of the groups, each with its own
|
|
220
257
|
* read cursor. Groups are mirrored into every live subscriber and retained for the
|
|
221
|
-
* track's `
|
|
258
|
+
* track's `maxAge` window so a late subscriber replays the recent groups.
|
|
222
259
|
*
|
|
223
260
|
* Obtained from {@link Request.accept} (the wire asks the application for a track to
|
|
224
261
|
* serve) or constructed directly for an in-process track.
|
|
@@ -231,10 +268,14 @@ export class Producer {
|
|
|
231
268
|
#state = new TrackState();
|
|
232
269
|
#sequence = { next: 0 };
|
|
233
270
|
// Recently written source groups, retained for replay to late subscribers and
|
|
234
|
-
// pruned once
|
|
271
|
+
// pruned once idle for longer than the cache window. Each entry tracks the mirror
|
|
235
272
|
// it handed to every sink so eviction can drop them too: otherwise a slow consumer
|
|
236
273
|
// that never reads would pin old groups (and their frame bytes) forever.
|
|
237
274
|
#cache = [];
|
|
275
|
+
// Wakeup for the next entry due to age out. Writes settle retention inline, but a
|
|
276
|
+
// publisher that stalls stops writing, so without this an abandoned group (and any
|
|
277
|
+
// reader parked in it) would wait for a write that never comes.
|
|
278
|
+
#pruneTimer;
|
|
238
279
|
// One independent downstream state per live subscriber.
|
|
239
280
|
#sinks = new Set();
|
|
240
281
|
// Whether any subscriber is currently attached. Exposed as {@link used}; the consumer wire
|
|
@@ -255,6 +296,15 @@ export class Producer {
|
|
|
255
296
|
info() {
|
|
256
297
|
return resolveInfo(this.#state);
|
|
257
298
|
}
|
|
299
|
+
/**
|
|
300
|
+
* Publisher priority from the committed {@link Info}, or 0 before {@link accept}.
|
|
301
|
+
*
|
|
302
|
+
* Higher is served first. Hang publishers set this from `Catalog.PRIORITY` so
|
|
303
|
+
* audio outranks video on the wire and in the bandwidth allocator.
|
|
304
|
+
*/
|
|
305
|
+
get priority() {
|
|
306
|
+
return this.#state.info.peek()?.priority ?? 0;
|
|
307
|
+
}
|
|
258
308
|
/**
|
|
259
309
|
* Settles once the track closes: `null` on a clean close, or the abort {@link Error}.
|
|
260
310
|
* Peek it synchronously (`undefined` while open), observe it reactively, or `await` it.
|
|
@@ -278,9 +328,17 @@ export class Producer {
|
|
|
278
328
|
// Propagate to any sink handed out before accept (the on-demand path).
|
|
279
329
|
for (const sink of this.#sinks)
|
|
280
330
|
sink.info.set(resolved);
|
|
331
|
+
this.#updateSubscription();
|
|
281
332
|
return this;
|
|
282
333
|
}
|
|
283
|
-
/**
|
|
334
|
+
/**
|
|
335
|
+
* An independent {@link Subscriber} reading this track's groups.
|
|
336
|
+
*
|
|
337
|
+
* Its cursor starts at the group the subscription named (its floor), or 0.
|
|
338
|
+
* {@link Subscription.maxAge} is what asks for data: delivery skips everything above
|
|
339
|
+
* the floor that the budget convicts, so the default budget of zero delivers only the
|
|
340
|
+
* latest group and a larger one reaches back over what it can still use.
|
|
341
|
+
*/
|
|
284
342
|
subscribe(options = {}) {
|
|
285
343
|
const sink = new TrackState(options);
|
|
286
344
|
this.#addSink(sink);
|
|
@@ -347,13 +405,18 @@ export class Producer {
|
|
|
347
405
|
this.#prune();
|
|
348
406
|
for (const entry of this.#cache)
|
|
349
407
|
this.#mirror(entry, sink);
|
|
408
|
+
sink.final.set(this.#state.final.peek());
|
|
350
409
|
if (closed !== undefined)
|
|
351
410
|
closeTrackState(sink, closed instanceof Error ? closed : undefined);
|
|
352
411
|
}
|
|
353
412
|
// Recompute from every live sink because an update or close can narrow as well as widen
|
|
354
413
|
// the aggregate. The wire layer observes this signal and emits SUBSCRIBE_UPDATE.
|
|
355
414
|
#updateSubscription() {
|
|
356
|
-
|
|
415
|
+
const combined = combineSubscriptions(this.#sinks);
|
|
416
|
+
const retained = this.#state.info.peek()?.maxAge;
|
|
417
|
+
if (combined && retained !== undefined)
|
|
418
|
+
combined.maxAge = Milli.min(combined.maxAge ?? Milli.zero, retained);
|
|
419
|
+
this.#state.update.set(combined);
|
|
357
420
|
}
|
|
358
421
|
// Mirror a cached source group into a sink. The mirror fills synchronously as the
|
|
359
422
|
// source is written and keeps its own read cursor; frame bytes are shared by
|
|
@@ -361,6 +424,8 @@ export class Producer {
|
|
|
361
424
|
#mirror(entry, sink) {
|
|
362
425
|
const dst = entry.group.mirror();
|
|
363
426
|
entry.mirrors.set(sink, dst);
|
|
427
|
+
sink.timeline.set(dst.sequence, { group: dst, time: entry.time });
|
|
428
|
+
void dst.readable().then(() => sink.timelineChanged.update((revision) => revision + 1));
|
|
364
429
|
sink.latest = Math.max(sink.latest ?? 0, dst.sequence);
|
|
365
430
|
sink.groups.mutate((groups) => {
|
|
366
431
|
groups.push(dst);
|
|
@@ -369,43 +434,107 @@ export class Producer {
|
|
|
369
434
|
}
|
|
370
435
|
// Drop a cached group's mirror from every sink so no consumer can pin it.
|
|
371
436
|
#evict(entry) {
|
|
437
|
+
// Reclaiming a group the publisher never closed ends it as the gap it is, before
|
|
438
|
+
// the mirrors below would otherwise report a clean finish to a reader that had
|
|
439
|
+
// drained it. The usual case, an already-closed group aging out, keeps its own
|
|
440
|
+
// terminal state.
|
|
441
|
+
if (!entry.group.isClosed)
|
|
442
|
+
entry.group.close(new TooFarBehind());
|
|
372
443
|
for (const [sink, mirror] of entry.mirrors) {
|
|
444
|
+
hooks.evictGroup(mirror);
|
|
373
445
|
sink.groups.mutate((groups) => {
|
|
374
446
|
const i = groups.indexOf(mirror);
|
|
375
447
|
if (i >= 0)
|
|
376
448
|
groups.splice(i, 1);
|
|
377
449
|
});
|
|
450
|
+
if (sink.timeline.get(mirror.sequence)?.group === mirror)
|
|
451
|
+
sink.timeline.delete(mirror.sequence);
|
|
378
452
|
mirror.close();
|
|
379
453
|
}
|
|
380
454
|
entry.mirrors.clear();
|
|
381
455
|
}
|
|
382
|
-
//
|
|
456
|
+
// The one group retention never takes: the newest, while it is still open. That is
|
|
457
|
+
// the live edge a publisher is appending to, and a track may legitimately keep it
|
|
458
|
+
// open across a long quiet stretch (a catalog snapshot, a JSON stream). Every other
|
|
459
|
+
// open group is an abandoned one, and ages out like a closed one.
|
|
460
|
+
#liveEdge() {
|
|
461
|
+
const latest = this.#cache.at(-1)?.group;
|
|
462
|
+
return latest?.closed.peek() === undefined ? latest : undefined;
|
|
463
|
+
}
|
|
464
|
+
// Evict cached groups idle for longer than the cache window. Idle means nothing
|
|
465
|
+
// written, so an abandoned open group ages out instead of pinning its buffer (and
|
|
466
|
+
// any reader parked in it) forever.
|
|
383
467
|
#prune() {
|
|
384
|
-
const
|
|
385
|
-
const cutoff =
|
|
468
|
+
const maxAgeMs = this.#state.info.peek()?.maxAge ?? DEFAULT_MAX_AGE_MS;
|
|
469
|
+
const cutoff = performance.now() - maxAgeMs;
|
|
470
|
+
const live = this.#liveEdge();
|
|
386
471
|
const retained = [];
|
|
387
472
|
for (const entry of this.#cache) {
|
|
388
|
-
if (entry.
|
|
473
|
+
if (entry.group === live || entry.group.activity >= cutoff) {
|
|
389
474
|
retained.push(entry);
|
|
390
475
|
continue;
|
|
391
476
|
}
|
|
392
477
|
this.#evict(entry);
|
|
393
478
|
}
|
|
394
479
|
this.#cache = retained;
|
|
480
|
+
this.#schedulePrune();
|
|
481
|
+
}
|
|
482
|
+
// Arm the wakeup for the next entry due to age out, replacing any pending one.
|
|
483
|
+
// Writes settle retention inline, so this only has to cover the case no write
|
|
484
|
+
// follows. Cheap to over-arm: an entry written since is retained and re-armed.
|
|
485
|
+
#schedulePrune() {
|
|
486
|
+
clearTimeout(this.#pruneTimer);
|
|
487
|
+
this.#pruneTimer = undefined;
|
|
488
|
+
if (this.#state.closed.peek() !== undefined)
|
|
489
|
+
return;
|
|
490
|
+
// One pass, no intermediate arrays: this runs on every publish, and a spread
|
|
491
|
+
// over the cache would also cap how many groups a track can hold.
|
|
492
|
+
const live = this.#liveEdge();
|
|
493
|
+
let oldest;
|
|
494
|
+
for (const entry of this.#cache) {
|
|
495
|
+
if (entry.group === live)
|
|
496
|
+
continue;
|
|
497
|
+
if (oldest === undefined || entry.group.activity < oldest)
|
|
498
|
+
oldest = entry.group.activity;
|
|
499
|
+
}
|
|
500
|
+
if (oldest === undefined)
|
|
501
|
+
return;
|
|
502
|
+
const maxAgeMs = this.#state.info.peek()?.maxAge ?? DEFAULT_MAX_AGE_MS;
|
|
503
|
+
// setTimeout truncates its delay to a signed 32-bit int, so a longer window
|
|
504
|
+
// would fire immediately and spin. Wake at the cap instead and re-arm: #prune
|
|
505
|
+
// retains anything still fresh, so the extra wakeups are the only cost.
|
|
506
|
+
const delay = Math.min(MAX_TIMEOUT_MS, Math.max(0, oldest + maxAgeMs - performance.now()));
|
|
507
|
+
const timer = setTimeout(() => {
|
|
508
|
+
this.#pruneTimer = undefined;
|
|
509
|
+
this.#prune();
|
|
510
|
+
}, delay);
|
|
511
|
+
// A cache prune is never a reason to hold a Node/Bun process open.
|
|
512
|
+
timer.unref?.();
|
|
513
|
+
this.#pruneTimer = timer;
|
|
395
514
|
}
|
|
396
515
|
// Retain a source group and fan it out to every live sink.
|
|
397
516
|
#publish(group) {
|
|
398
|
-
const entry = { group, time:
|
|
517
|
+
const entry = { group, time: performance.now(), mirrors: new Map() };
|
|
399
518
|
this.#cache.push(entry);
|
|
400
|
-
this.#prune();
|
|
401
519
|
for (const sink of this.#sinks)
|
|
402
520
|
this.#mirror(entry, sink);
|
|
521
|
+
// Give held mirrors the new live edge before pruning their timeline entry,
|
|
522
|
+
// so their latency guard can preserve a terminal expiry verdict.
|
|
523
|
+
this.#prune();
|
|
403
524
|
}
|
|
404
|
-
|
|
405
|
-
|
|
525
|
+
// Refuse a write once the track is closed, or at or past its declared end.
|
|
526
|
+
#writable(sequence) {
|
|
406
527
|
if (this.#state.closed.peek() !== undefined)
|
|
407
528
|
throw new Error("track is closed");
|
|
529
|
+
const final = this.#state.final.peek();
|
|
530
|
+
if (final !== undefined && sequence >= final) {
|
|
531
|
+
throw new Error(`sequence ${sequence} is at or past the track's end ${final}`);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
/** Append a new group with the next sequence number. */
|
|
535
|
+
appendGroup() {
|
|
408
536
|
const sequence = this.#sequence;
|
|
537
|
+
this.#writable(sequence.next);
|
|
409
538
|
const group = new GroupProducer(sequence.next);
|
|
410
539
|
sequence.next = group.sequence + 1;
|
|
411
540
|
this.#publish(group);
|
|
@@ -420,8 +549,7 @@ export class Producer {
|
|
|
420
549
|
* entry is already gone, so a long-evicted sequence is accepted as new.
|
|
421
550
|
*/
|
|
422
551
|
writeGroup(group) {
|
|
423
|
-
|
|
424
|
-
throw new Error("track is closed");
|
|
552
|
+
this.#writable(group.sequence);
|
|
425
553
|
const existing = this.#cache.findIndex((entry) => entry.group.sequence === group.sequence);
|
|
426
554
|
if (existing >= 0) {
|
|
427
555
|
const entry = this.#cache[existing];
|
|
@@ -441,13 +569,11 @@ export class Producer {
|
|
|
441
569
|
// Fan a datagram out to every live subscriber, dropping the oldest once the ring is full.
|
|
442
570
|
// Late subscribers do NOT replay old datagrams (best-effort, unlike the group cache).
|
|
443
571
|
#publishDatagram(datagram) {
|
|
444
|
-
const now = performance.now();
|
|
445
572
|
for (const sink of this.#sinks) {
|
|
446
573
|
sink.datagrams.mutate((list) => {
|
|
447
|
-
list.
|
|
448
|
-
// Drop anything older than the send-buffer window.
|
|
449
|
-
while (list.length > 0 && now - list[0].time > MAX_DATAGRAM_AGE_MS)
|
|
574
|
+
if (list.length === MAX_DATAGRAMS)
|
|
450
575
|
list.shift();
|
|
576
|
+
list.push(datagram);
|
|
451
577
|
});
|
|
452
578
|
}
|
|
453
579
|
}
|
|
@@ -461,40 +587,76 @@ export class Producer {
|
|
|
461
587
|
* keep datagram payloads small (e.g. a single audio frame). Datagrams are never delivered
|
|
462
588
|
* over IETF moq-transport or stream-only transports (the WebSocket fallback). A payload over
|
|
463
589
|
* 65535 bytes (the QUIC datagram frame ceiling) throws. An origin publisher uses this; a
|
|
464
|
-
* relay preserving upstream numbering uses {@link
|
|
590
|
+
* relay preserving upstream numbering uses {@link insertDatagram}.
|
|
465
591
|
*/
|
|
466
592
|
appendDatagram(timestamp, payload) {
|
|
467
|
-
if (this.#state.closed.peek() !== undefined)
|
|
468
|
-
throw new Error("track is closed");
|
|
469
|
-
if (payload.byteLength > MAX_DATAGRAM_BYTES)
|
|
470
|
-
throw new Error("datagram payload too large");
|
|
471
593
|
const counter = this.#sequence;
|
|
472
594
|
const sequence = counter.next;
|
|
595
|
+
this.#writable(sequence);
|
|
596
|
+
if (payload.byteLength > MAX_DATAGRAM_BYTES)
|
|
597
|
+
throw new Error("datagram payload too large");
|
|
473
598
|
counter.next = sequence + 1;
|
|
474
599
|
this.#publishDatagram({ sequence, timestamp, payload });
|
|
475
600
|
return sequence;
|
|
476
601
|
}
|
|
477
602
|
/**
|
|
478
|
-
*
|
|
603
|
+
* Insert a datagram with an explicit sequence number.
|
|
479
604
|
*
|
|
480
605
|
* Preserves the supplied sequence (advancing the shared counter if needed) so a relay can
|
|
481
606
|
* forward a datagram without renumbering it. The size limits of {@link appendDatagram}
|
|
482
607
|
* apply. Most origin publishers want {@link appendDatagram} instead.
|
|
483
608
|
*/
|
|
484
|
-
|
|
609
|
+
insertDatagram(sequence, timestamp, payload) {
|
|
610
|
+
this.#writable(sequence);
|
|
611
|
+
if (payload.byteLength > MAX_DATAGRAM_BYTES)
|
|
612
|
+
throw new Error("datagram payload too large");
|
|
613
|
+
const counter = this.#sequence;
|
|
614
|
+
if (sequence >= counter.next) {
|
|
615
|
+
counter.next = sequence + 1;
|
|
616
|
+
}
|
|
617
|
+
this.#publishDatagram({ sequence, timestamp, payload });
|
|
618
|
+
}
|
|
619
|
+
/**
|
|
620
|
+
* Declare the track's exclusive end, possibly ahead of the live edge, mirroring the Rust
|
|
621
|
+
* `finish_at`.
|
|
622
|
+
*
|
|
623
|
+
* `final` is the first sequence that will never be produced, so a track whose last group
|
|
624
|
+
* is 89 finishes at 90. Groups and datagrams below it are still accepted; anything at or
|
|
625
|
+
* above it is refused. Unlike {@link close} it is not terminal: call `close()` once the
|
|
626
|
+
* remaining groups are written. Throws if the track is closed, already has an end, or
|
|
627
|
+
* `final` is at or below a sequence already produced.
|
|
628
|
+
*/
|
|
629
|
+
finishAt(final) {
|
|
485
630
|
if (this.#state.closed.peek() !== undefined)
|
|
486
631
|
throw new Error("track is closed");
|
|
487
|
-
if (
|
|
488
|
-
throw new
|
|
489
|
-
const
|
|
490
|
-
if (
|
|
491
|
-
|
|
632
|
+
if (!Number.isSafeInteger(final) || final < 0)
|
|
633
|
+
throw new RangeError(`invalid track end: ${final}`);
|
|
634
|
+
const declared = this.#state.final.peek();
|
|
635
|
+
if (declared !== undefined)
|
|
636
|
+
throw new Error(`track already ends at ${declared}`);
|
|
637
|
+
if (final < this.#sequence.next) {
|
|
638
|
+
throw new Error(`track end ${final} is below the next sequence ${this.#sequence.next}`);
|
|
492
639
|
}
|
|
493
|
-
this.#
|
|
640
|
+
this.#declareFinal(final);
|
|
494
641
|
}
|
|
495
|
-
|
|
642
|
+
#declareFinal(final) {
|
|
643
|
+
this.#state.final.set(final);
|
|
644
|
+
for (const sink of this.#sinks)
|
|
645
|
+
sink.final.set(final);
|
|
646
|
+
}
|
|
647
|
+
/**
|
|
648
|
+
* Close the track and every subscriber, mirroring the abort to their groups. Idempotent.
|
|
649
|
+
*
|
|
650
|
+
* A clean close keeps the end {@link finishAt} declared, or declares one past the highest
|
|
651
|
+
* sequence produced; an abort ends without one.
|
|
652
|
+
*/
|
|
496
653
|
close(abort) {
|
|
654
|
+
if (abort === undefined && this.#state.closed.peek() === undefined && this.#state.final.peek() === undefined) {
|
|
655
|
+
this.#declareFinal(this.#sequence.next);
|
|
656
|
+
}
|
|
497
657
|
closeTrackState(this.#state, abort);
|
|
658
|
+
clearTimeout(this.#pruneTimer);
|
|
659
|
+
this.#pruneTimer = undefined;
|
|
498
660
|
for (const { group } of this.#cache)
|
|
499
661
|
group.close(abort);
|
|
500
662
|
for (const sink of this.#sinks) {
|
|
@@ -542,12 +704,140 @@ export class Subscriber {
|
|
|
542
704
|
#state;
|
|
543
705
|
#nextSequence = 0;
|
|
544
706
|
#cursor = new Signal({ start: 0 });
|
|
707
|
+
#enforceLatency = true;
|
|
708
|
+
// Which cursor owns this subscription. Both cursors draw from one buffer, so the
|
|
709
|
+
// first group read commits the track to arrival order and {@link ordered} commits
|
|
710
|
+
// it to sequence order; whichever wins is the only one allowed from here on.
|
|
711
|
+
// Datagrams are a separate cursor and never commit.
|
|
712
|
+
#mode;
|
|
713
|
+
// The group the frame-level helpers are currently draining, acquired through the
|
|
714
|
+
// sequence cursor so frame reads and {@link Ordered.nextGroup} share one floor.
|
|
715
|
+
#frameGroup;
|
|
716
|
+
#drift() {
|
|
717
|
+
const { end } = this.#cursor.peek();
|
|
718
|
+
let presentation;
|
|
719
|
+
for (const { group } of this.#state.timeline.values()) {
|
|
720
|
+
if (end !== undefined && group.sequence >= end)
|
|
721
|
+
continue;
|
|
722
|
+
if (group.closed.peek() instanceof Error)
|
|
723
|
+
continue;
|
|
724
|
+
// The edge wants the newest content that exists, so it takes the newest
|
|
725
|
+
// stamped group's latest frame.
|
|
726
|
+
const timestamp = hooks.groupTimestamp(group);
|
|
727
|
+
if (timestamp !== undefined && (!presentation || group.sequence > presentation.sequence)) {
|
|
728
|
+
presentation = { sequence: group.sequence, timestamp: hooks.groupLatest(group) ?? timestamp };
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
const requested = this.#state.update.peek()?.maxAge ?? 0;
|
|
732
|
+
const retained = this.#state.info.peek()?.maxAge;
|
|
733
|
+
return {
|
|
734
|
+
budget: this.#enforceLatency
|
|
735
|
+
? retained === undefined
|
|
736
|
+
? requested
|
|
737
|
+
: Math.min(requested, retained)
|
|
738
|
+
: Number.POSITIVE_INFINITY,
|
|
739
|
+
presentation,
|
|
740
|
+
end,
|
|
741
|
+
};
|
|
742
|
+
}
|
|
743
|
+
// The furthest presentation time the group at `sequence` could still reach: where its
|
|
744
|
+
// immediate servable successor begins, or undefined when nothing proves where it
|
|
745
|
+
// stops. An upper bound, deliberately: a frame's duration is not on the wire, so a
|
|
746
|
+
// group's own last timestamp says where it starts presenting, not where it ends.
|
|
747
|
+
// Only the *immediate* successor counts: timestamps need not rise with sequence (a
|
|
748
|
+
// rewind reorders them), so a later stamped group proves nothing about where an
|
|
749
|
+
// unstamped successor will begin, and shrinking the bound is the unsafe direction.
|
|
750
|
+
// An unstamped successor leaves the reach unbounded until it presents a frame.
|
|
751
|
+
#reach(sequence, end) {
|
|
752
|
+
let successor;
|
|
753
|
+
for (const { group } of this.#state.timeline.values()) {
|
|
754
|
+
if (group.sequence <= sequence)
|
|
755
|
+
continue;
|
|
756
|
+
if (end !== undefined && group.sequence >= end)
|
|
757
|
+
continue;
|
|
758
|
+
if (group.closed.peek() instanceof Error)
|
|
759
|
+
continue;
|
|
760
|
+
if (!successor || group.sequence < successor.sequence)
|
|
761
|
+
successor = group;
|
|
762
|
+
}
|
|
763
|
+
if (!successor)
|
|
764
|
+
return undefined;
|
|
765
|
+
return hooks.groupTimestamp(successor)?.asMillis();
|
|
766
|
+
}
|
|
767
|
+
// Whether the drift budget says to give up on `group`.
|
|
768
|
+
//
|
|
769
|
+
// Presentation time measures a group by how far it could still reach, not by how far
|
|
770
|
+
// behind it started. Being behind is survivable: priority transmits newer groups first,
|
|
771
|
+
// so a backlog bursts at whatever rate is left over and closes the gap faster than the
|
|
772
|
+
// live edge advances. A group is abandoned only once everything it could still present
|
|
773
|
+
// falls outside the budget.
|
|
774
|
+
//
|
|
775
|
+
// A group's reach is bounded by its nearest successor: it cannot present past where the
|
|
776
|
+
// next group begins. Its own frames say nothing, since a frame's duration is not on the
|
|
777
|
+
// wire, and the candidate needs no timestamp of its own: an empty group is bounded by
|
|
778
|
+
// its stamped successor the same way. The bound is exclusive, so the comparison is `>=`:
|
|
779
|
+
// the freshest frame a group could still hold sits just below its reach, so an age equal
|
|
780
|
+
// to the budget already puts every frame in it strictly past the budget. A zero budget
|
|
781
|
+
// falls out for free. Only timestamps drive expiry; wall-clock reclamation of idle
|
|
782
|
+
// content is the cache's own policy, not the budget's.
|
|
783
|
+
#isStale(group, drift) {
|
|
784
|
+
const candidate = this.#state.timeline.get(group.sequence);
|
|
785
|
+
if (candidate?.group !== group)
|
|
786
|
+
return false;
|
|
787
|
+
const reach = this.#reach(group.sequence, drift.end);
|
|
788
|
+
return (drift.presentation !== undefined &&
|
|
789
|
+
drift.presentation.sequence > group.sequence &&
|
|
790
|
+
reach !== undefined &&
|
|
791
|
+
drift.presentation.timestamp.asMillis() - reach >= drift.budget);
|
|
792
|
+
}
|
|
793
|
+
#guard(group) {
|
|
794
|
+
if (!this.#enforceLatency)
|
|
795
|
+
return group;
|
|
796
|
+
hooks.expireGroup(group, {
|
|
797
|
+
expired: () => this.#isStale(group, this.#drift()),
|
|
798
|
+
changed: [
|
|
799
|
+
this.#state.groups,
|
|
800
|
+
this.#state.timelineChanged,
|
|
801
|
+
this.#state.update,
|
|
802
|
+
this.#state.info,
|
|
803
|
+
this.#cursor,
|
|
804
|
+
this.#state.closed,
|
|
805
|
+
],
|
|
806
|
+
});
|
|
807
|
+
return group;
|
|
808
|
+
}
|
|
545
809
|
constructor(name, state) {
|
|
546
810
|
this.name = name;
|
|
547
811
|
this.#state = state;
|
|
812
|
+
// The cursor's floor is the group the subscription named, or 0. A floor is the
|
|
813
|
+
// only thing a start contributes; {@link Subscription.maxAge} is what asks for
|
|
814
|
+
// data, and delivery skips everything above the floor that the budget convicts.
|
|
815
|
+
this.#cursor.set({ start: groupBounds(state.update.peek()?.groups ?? {}).start });
|
|
548
816
|
}
|
|
549
817
|
static {
|
|
550
818
|
makeSubscriber = (name, state) => new Subscriber(name, state);
|
|
819
|
+
hooks.tryRecvGroup = (subscriber) => subscriber.#tryRecvGroup();
|
|
820
|
+
hooks.groupChanged = (subscriber, fn) => subscriber.#groupChanged(fn);
|
|
821
|
+
hooks.exemptFetch = (subscriber) => {
|
|
822
|
+
subscriber.#enforceLatency = false;
|
|
823
|
+
};
|
|
824
|
+
hooks.replaceGroups = (subscriber, groups) => subscriber.#replaceGroups(groups);
|
|
825
|
+
// The sequence cursor lives here (it shares the buffer and the drift anchor with
|
|
826
|
+
// the arrival cursor); `Ordered` is the handle that reaches it.
|
|
827
|
+
ordered_ = {
|
|
828
|
+
nextGroup: (subscriber) => subscriber.#nextGroup(),
|
|
829
|
+
readFrame: (subscriber) => subscriber.#readFrame(),
|
|
830
|
+
readString: (subscriber) => subscriber.#readString(),
|
|
831
|
+
readJson: (subscriber) => subscriber.#readJson(),
|
|
832
|
+
readBool: (subscriber) => subscriber.#readBool(),
|
|
833
|
+
// Unordered either way, so both handles reach the same cursor.
|
|
834
|
+
recvDatagram: (subscriber) => subscriber.#recvDatagram(),
|
|
835
|
+
};
|
|
836
|
+
}
|
|
837
|
+
// Refuse a read on this handle once `ordered()` has taken the subscription.
|
|
838
|
+
#live() {
|
|
839
|
+
if (this.#mode === "ordered")
|
|
840
|
+
throw new Error("track is read in sequence order; use the Ordered handle");
|
|
551
841
|
}
|
|
552
842
|
/**
|
|
553
843
|
* Resolve this track's immutable publisher properties.
|
|
@@ -571,6 +861,37 @@ export class Subscriber {
|
|
|
571
861
|
latest() {
|
|
572
862
|
return this.#state.latest;
|
|
573
863
|
}
|
|
864
|
+
/**
|
|
865
|
+
* The track's exclusive final boundary: the end {@link Producer.finishAt} declared, which
|
|
866
|
+
* can be ahead of the live edge, or one past the highest sequence produced once the
|
|
867
|
+
* producer closes cleanly (0 for a track that produced none). Groups and datagrams share
|
|
868
|
+
* the sequence namespace, so this can exceed `latest() + 1`. Undefined until declared,
|
|
869
|
+
* and after an abort that declared none.
|
|
870
|
+
*/
|
|
871
|
+
final() {
|
|
872
|
+
return this.#state.final.peek();
|
|
873
|
+
}
|
|
874
|
+
/**
|
|
875
|
+
* Resolve with the track's exclusive final boundary once it is known, mirroring the Rust
|
|
876
|
+
* `finished`.
|
|
877
|
+
*
|
|
878
|
+
* Resolves as soon as the end is declared, which may be ahead of the live edge, so it
|
|
879
|
+
* says nothing about every group having arrived: read until the cursor returns
|
|
880
|
+
* `undefined` for that. Rejects with the abort, or if the track closes without an end.
|
|
881
|
+
*/
|
|
882
|
+
async finished() {
|
|
883
|
+
for (;;) {
|
|
884
|
+
const final = this.#state.final.peek();
|
|
885
|
+
if (final !== undefined)
|
|
886
|
+
return final;
|
|
887
|
+
const closed = this.#state.closed.peek();
|
|
888
|
+
if (closed instanceof Error)
|
|
889
|
+
throw closed;
|
|
890
|
+
if (closed !== undefined)
|
|
891
|
+
throw new Error("track closed before its end was known");
|
|
892
|
+
await Signal.race(this.#state.final, this.#state.closed);
|
|
893
|
+
}
|
|
894
|
+
}
|
|
574
895
|
/**
|
|
575
896
|
* The newest frame this track has produced, or `undefined` while it has none.
|
|
576
897
|
*
|
|
@@ -611,22 +932,33 @@ export class Subscriber {
|
|
|
611
932
|
throw new Error("track has no producer to fork from");
|
|
612
933
|
return producer.subscribe(options);
|
|
613
934
|
}
|
|
614
|
-
/**
|
|
615
|
-
|
|
616
|
-
this
|
|
935
|
+
/** Limit subsequent reads to these groups and return this reader for chaining. */
|
|
936
|
+
withGroups(groups) {
|
|
937
|
+
this.setGroups(groups);
|
|
938
|
+
return this;
|
|
617
939
|
}
|
|
618
940
|
/**
|
|
619
|
-
*
|
|
620
|
-
*
|
|
621
|
-
*
|
|
941
|
+
* Limit subsequent reads without rewinding read progress or changing the wire request.
|
|
942
|
+
* An omitted start preserves the current floor; an omitted end removes the cap.
|
|
943
|
+
* Raising the end makes unread buffered groups available again.
|
|
622
944
|
*/
|
|
623
|
-
|
|
624
|
-
|
|
945
|
+
setGroups(groups) {
|
|
946
|
+
const { start, end } = groupBounds(groups);
|
|
947
|
+
this.#cursor.update((cursor) => ({ start: Math.max(cursor.start, start), end }));
|
|
948
|
+
}
|
|
949
|
+
// Serving counterpart of setGroups: a named start replaces the floor, matching
|
|
950
|
+
// Rust `start_at`. Local readers stay monotonic; only the wire publisher lowers.
|
|
951
|
+
#replaceGroups(groups) {
|
|
952
|
+
const { start, end } = groupBounds(groups);
|
|
953
|
+
this.#cursor.update((cursor) => ({
|
|
954
|
+
start: groups.start === undefined ? cursor.start : start,
|
|
955
|
+
end,
|
|
956
|
+
}));
|
|
625
957
|
}
|
|
626
958
|
/** Close the track (optionally with an error), closing any pending groups. Idempotent. */
|
|
627
959
|
close(abort) {
|
|
628
960
|
// Settle if we're first (the producer may already have); either way drop anything
|
|
629
|
-
// still buffered. Groups parked at the
|
|
961
|
+
// still buffered. Groups parked at the setGroups cap deliberately outlive a clean
|
|
630
962
|
// producer close, so the subscriber leaving is what must release them: closing
|
|
631
963
|
// and clearing wakes a pending read to observe the end instead of hanging.
|
|
632
964
|
closeTrackState(this.#state, abort);
|
|
@@ -635,55 +967,108 @@ export class Subscriber {
|
|
|
635
967
|
group.close(abort);
|
|
636
968
|
groups.length = 0;
|
|
637
969
|
});
|
|
970
|
+
this.#frameGroup?.close(abort);
|
|
971
|
+
this.#frameGroup = undefined;
|
|
972
|
+
this.#state.timeline.clear();
|
|
638
973
|
}
|
|
639
974
|
/**
|
|
640
975
|
* Receive every group on this track exactly once, as it becomes available.
|
|
641
976
|
*
|
|
642
977
|
* Groups may arrive out of order or with gaps due to network conditions; unlike
|
|
643
|
-
* {@link nextGroup}, one that arrives after a newer group was already returned
|
|
644
|
-
* still delivered. When several groups are buffered, the lowest sequence is
|
|
978
|
+
* {@link Ordered.nextGroup}, one that arrives after a newer group was already returned
|
|
979
|
+
* is still delivered. When several groups are buffered, the lowest sequence is
|
|
645
980
|
* returned first.
|
|
646
981
|
*
|
|
647
|
-
* Honors the
|
|
982
|
+
* Honors the range set by {@link setGroups}: a group
|
|
648
983
|
* beyond the cap stays buffered (not dropped) and is offered once the cap rises, even
|
|
649
984
|
* after a clean close, without blocking in-range groups that arrive behind it.
|
|
985
|
+
* A group whose presentation time is further behind the live edge than this
|
|
986
|
+
* subscriber's `maxAge` is skipped. The default of zero takes the live edge.
|
|
987
|
+
* The budget remains attached after return, so a pending frame read rejects if a stalled
|
|
988
|
+
* group becomes stale while newer data advances.
|
|
989
|
+
*
|
|
990
|
+
* The first call commits this track to arrival order: {@link ordered} throws afterwards.
|
|
650
991
|
*/
|
|
651
992
|
async recvGroup() {
|
|
993
|
+
this.#live();
|
|
994
|
+
this.#mode = "arrival";
|
|
652
995
|
for (;;) {
|
|
653
|
-
const
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
996
|
+
const recv = this.#tryRecvGroup();
|
|
997
|
+
switch (recv.kind) {
|
|
998
|
+
case "group":
|
|
999
|
+
return recv.group;
|
|
1000
|
+
case "done":
|
|
1001
|
+
return undefined;
|
|
1002
|
+
case "error":
|
|
1003
|
+
throw recv.error;
|
|
1004
|
+
}
|
|
1005
|
+
// Idle, or parked at the boundary waiting for the cap to rise.
|
|
663
1006
|
await Signal.race(this.#state.groups, this.#cursor, this.#state.closed);
|
|
664
1007
|
}
|
|
665
1008
|
}
|
|
1009
|
+
// Package-internal synchronous half of recvGroup. The lite publisher uses this so applying
|
|
1010
|
+
// control state, popping the group, and positioning its frames are one JavaScript turn.
|
|
1011
|
+
#tryRecvGroup() {
|
|
1012
|
+
const groups = this.#state.groups.peek();
|
|
1013
|
+
const { start, end } = this.#cursor.peek();
|
|
1014
|
+
while (groups.length > 0 && groups[0].sequence < start)
|
|
1015
|
+
groups.shift()?.close();
|
|
1016
|
+
const drift = this.#drift();
|
|
1017
|
+
for (;;) {
|
|
1018
|
+
// The buffer is sequence-sorted, so an in-range group that arrives behind a
|
|
1019
|
+
// beyond-cap one sorts in front of it and is never blocked by it.
|
|
1020
|
+
const group = groups[0];
|
|
1021
|
+
if (!group || (end !== undefined && group.sequence >= end))
|
|
1022
|
+
break;
|
|
1023
|
+
groups.shift();
|
|
1024
|
+
if (this.#isStale(group, drift)) {
|
|
1025
|
+
group.close();
|
|
1026
|
+
continue;
|
|
1027
|
+
}
|
|
1028
|
+
return { kind: "group", group: this.#guard(group) };
|
|
1029
|
+
}
|
|
1030
|
+
const group = groups[0];
|
|
1031
|
+
const closed = this.#state.closed.peek();
|
|
1032
|
+
if (closed instanceof Error)
|
|
1033
|
+
return { kind: "error", error: closed };
|
|
1034
|
+
if (closed === undefined)
|
|
1035
|
+
return { kind: "idle" };
|
|
1036
|
+
// A group beyond the cap outlives a clean close: it becomes deliverable if
|
|
1037
|
+
// the cap rises, so the track isn't over while any are held.
|
|
1038
|
+
return group ? { kind: "boundary" } : { kind: "done" };
|
|
1039
|
+
}
|
|
1040
|
+
// Package-internal readiness half of recvGroup. Each registration fires at most once, and
|
|
1041
|
+
// the caller disposes the losers after whichever source wakes it. A declared end wakes it
|
|
1042
|
+
// too, so a publisher can forward the end before the live edge reaches it.
|
|
1043
|
+
#groupChanged(fn) {
|
|
1044
|
+
const dispose = [
|
|
1045
|
+
this.#state.groups.changed(fn),
|
|
1046
|
+
this.#cursor.changed(fn),
|
|
1047
|
+
this.#state.closed.changed(fn),
|
|
1048
|
+
this.#state.final.changed(fn),
|
|
1049
|
+
];
|
|
1050
|
+
return () => {
|
|
1051
|
+
for (const close of dispose)
|
|
1052
|
+
close();
|
|
1053
|
+
};
|
|
1054
|
+
}
|
|
666
1055
|
/**
|
|
667
1056
|
* Take the next buffered group without blocking, honoring the same cursor bounds as
|
|
668
1057
|
* {@link recvGroup}.
|
|
669
1058
|
*
|
|
670
1059
|
* Returns `undefined` when nothing is deliverable right now, which is not by itself the
|
|
671
1060
|
* end of the track: a group may still arrive, or one may be parked beyond the
|
|
672
|
-
* {@link
|
|
1061
|
+
* {@link setGroups} cap. Use it to drain what the retained window already holds, where
|
|
673
1062
|
* waiting for a sequence nothing will republish would park forever.
|
|
674
1063
|
*/
|
|
675
1064
|
tryRecvGroup() {
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
if (group && (end === undefined || group.sequence <= end)) {
|
|
684
|
-
groups.shift();
|
|
685
|
-
return group;
|
|
686
|
-
}
|
|
1065
|
+
this.#live();
|
|
1066
|
+
this.#mode = "arrival";
|
|
1067
|
+
const recv = this.#tryRecvGroup();
|
|
1068
|
+
if (recv.kind === "group")
|
|
1069
|
+
return recv.group;
|
|
1070
|
+
if (recv.kind === "error")
|
|
1071
|
+
throw recv.error;
|
|
687
1072
|
return undefined;
|
|
688
1073
|
}
|
|
689
1074
|
/**
|
|
@@ -692,23 +1077,20 @@ export class Subscriber {
|
|
|
692
1077
|
* Datagrams are a separate best-effort channel from groups (see
|
|
693
1078
|
* {@link Producer.appendDatagram}); they share only the sequence namespace. A consumer
|
|
694
1079
|
* that falls too far behind silently loses the oldest datagrams. Read this alongside
|
|
695
|
-
* {@link recvGroup} (e.g. in a separate loop) to receive both channels concurrently.
|
|
696
|
-
* a datagram
|
|
1080
|
+
* {@link recvGroup} (e.g. in a separate loop) to receive both channels concurrently.
|
|
1081
|
+
* The two cursors are independent: a datagram never moves the group cursor.
|
|
697
1082
|
*/
|
|
698
1083
|
async recvDatagram() {
|
|
1084
|
+
this.#live();
|
|
1085
|
+
return this.#recvDatagram();
|
|
1086
|
+
}
|
|
1087
|
+
// The datagram cursor, reachable from either handle: unordered by construction, so the
|
|
1088
|
+
// choice of group order says nothing about it.
|
|
1089
|
+
async #recvDatagram() {
|
|
699
1090
|
for (;;) {
|
|
700
1091
|
const datagrams = this.#state.datagrams.peek();
|
|
701
|
-
// Evict datagrams older than the send-buffer window (also enforced on write), so a
|
|
702
|
-
// reader that stalled skips stale datagrams instead of replaying them.
|
|
703
|
-
const cutoff = performance.now() - MAX_DATAGRAM_AGE_MS;
|
|
704
|
-
while (datagrams.length > 0 && datagrams[0].time < cutoff)
|
|
705
|
-
datagrams.shift();
|
|
706
1092
|
if (datagrams.length > 0) {
|
|
707
|
-
|
|
708
|
-
if (datagram) {
|
|
709
|
-
this.#nextSequence = Math.max(this.#nextSequence, datagram.sequence + 1);
|
|
710
|
-
}
|
|
711
|
-
return datagram;
|
|
1093
|
+
return datagrams.shift();
|
|
712
1094
|
}
|
|
713
1095
|
const closed = this.#state.closed.peek();
|
|
714
1096
|
if (closed instanceof Error)
|
|
@@ -718,128 +1100,111 @@ export class Subscriber {
|
|
|
718
1100
|
await Signal.race(this.#state.datagrams, this.#state.closed);
|
|
719
1101
|
}
|
|
720
1102
|
}
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
*
|
|
724
|
-
* Late arrivals (sequence at or below the last returned) are silently skipped.
|
|
725
|
-
* Use {@link recvGroup} to see every group in arrival order instead.
|
|
726
|
-
*/
|
|
727
|
-
async nextGroup() {
|
|
1103
|
+
// The sequence cursor behind {@link Ordered}, which owns the only public door to it.
|
|
1104
|
+
async #nextGroup() {
|
|
728
1105
|
for (;;) {
|
|
729
1106
|
const groups = this.#state.groups.peek();
|
|
730
1107
|
const cursor = this.#cursor.peek();
|
|
731
1108
|
const start = Math.max(cursor.start, this.#nextSequence);
|
|
732
1109
|
while (groups.length > 0 && groups[0].sequence < start)
|
|
733
1110
|
groups.shift()?.close();
|
|
734
|
-
|
|
735
|
-
|
|
1111
|
+
// One anchor for the whole pass, so walking a backlog off stays linear.
|
|
1112
|
+
const drift = this.#drift();
|
|
1113
|
+
for (;;) {
|
|
1114
|
+
const group = groups[0];
|
|
1115
|
+
if (!group || (cursor.end !== undefined && group.sequence >= cursor.end))
|
|
1116
|
+
break;
|
|
736
1117
|
groups.shift();
|
|
737
1118
|
this.#nextSequence = group.sequence + 1;
|
|
738
|
-
|
|
1119
|
+
// Every frame this group could still hold is past the budget, so keep
|
|
1120
|
+
// scanning: one pass walks a whole backlog off rather than replaying it.
|
|
1121
|
+
if (this.#isStale(group, drift)) {
|
|
1122
|
+
group.close();
|
|
1123
|
+
continue;
|
|
1124
|
+
}
|
|
1125
|
+
// One cursor: the frame helpers must not keep draining a group this
|
|
1126
|
+
// read just moved past, or interleaved reads would run backwards.
|
|
1127
|
+
if (this.#frameGroup && this.#frameGroup.sequence < group.sequence) {
|
|
1128
|
+
this.#frameGroup.close();
|
|
1129
|
+
this.#frameGroup = undefined;
|
|
1130
|
+
}
|
|
1131
|
+
return this.#guard(group);
|
|
739
1132
|
}
|
|
740
1133
|
const closed = this.#state.closed.peek();
|
|
741
1134
|
if (closed instanceof Error)
|
|
742
1135
|
throw closed;
|
|
743
|
-
|
|
1136
|
+
// A group parked above the cap stays deliverable even after a clean close
|
|
1137
|
+
// (its frames remain buffered), so keep waiting for a cap raise. Only a
|
|
1138
|
+
// drained track reports finished.
|
|
1139
|
+
if (closed !== undefined && !groups[0])
|
|
744
1140
|
return undefined;
|
|
745
1141
|
await Signal.race(this.#state.groups, this.#cursor, this.#state.closed);
|
|
746
1142
|
}
|
|
747
1143
|
}
|
|
748
1144
|
/**
|
|
749
|
-
* Reads the next frame across
|
|
750
|
-
* Treat the returned frame bytes as read-only; they are shared with other consumers.
|
|
751
|
-
*/
|
|
752
|
-
async readFrame() {
|
|
753
|
-
const next = await this.readFrameSequence();
|
|
754
|
-
return next ? { payload: next.payload, timestamp: next.timestamp } : undefined;
|
|
755
|
-
}
|
|
756
|
-
/**
|
|
757
|
-
* Reads the next frame along with its group and frame sequence numbers.
|
|
1145
|
+
* Reads the next frame across groups, in sequence order, with its group and frame numbers.
|
|
758
1146
|
* Treat the returned frame bytes as read-only; they are shared with other consumers.
|
|
1147
|
+
*
|
|
1148
|
+
* Groups are acquired through the same sequence cursor as {@link Ordered.nextGroup},
|
|
1149
|
+
* so frames never run backwards: a late lower-sequence group is skipped, and so is
|
|
1150
|
+
* one every frame of which `maxAge` proves is too old. A group the budget abandons
|
|
1151
|
+
* mid-stall ends cleanly and the cursor resyncs from the next group; a gap inside a
|
|
1152
|
+
* group still surfaces as {@link TooFarBehind} or {@link GroupTooLarge}.
|
|
759
1153
|
*/
|
|
760
|
-
async
|
|
1154
|
+
async #readFrame() {
|
|
761
1155
|
for (;;) {
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
groups.shift()?.close();
|
|
772
|
-
throw new Lagged();
|
|
773
|
-
}
|
|
774
|
-
const next = groups[0].tryReadFrameSequence();
|
|
775
|
-
if (next) {
|
|
776
|
-
return {
|
|
777
|
-
group: groups[0].sequence,
|
|
778
|
-
frame: next.sequence,
|
|
779
|
-
payload: next.payload,
|
|
780
|
-
timestamp: next.timestamp,
|
|
781
|
-
};
|
|
782
|
-
}
|
|
783
|
-
groups.shift()?.close();
|
|
1156
|
+
if (!this.#frameGroup) {
|
|
1157
|
+
this.#frameGroup = await this.#nextGroup();
|
|
1158
|
+
if (!this.#frameGroup)
|
|
1159
|
+
return undefined;
|
|
1160
|
+
}
|
|
1161
|
+
const group = this.#frameGroup;
|
|
1162
|
+
let next;
|
|
1163
|
+
try {
|
|
1164
|
+
next = await group.readFrameSequence();
|
|
784
1165
|
}
|
|
785
|
-
|
|
1166
|
+
catch (err) {
|
|
1167
|
+
// The group failed underneath us: resync from the next one, surfacing
|
|
1168
|
+
// only what the caller can act on (a gap, or the track's own abort).
|
|
1169
|
+
this.#frameGroup = undefined;
|
|
1170
|
+
group.close();
|
|
1171
|
+
if (err instanceof TooFarBehind || err instanceof GroupTooLarge)
|
|
1172
|
+
throw err;
|
|
786
1173
|
const closed = this.#state.closed.peek();
|
|
787
1174
|
if (closed instanceof Error)
|
|
788
1175
|
throw closed;
|
|
789
|
-
if (closed !== undefined)
|
|
790
|
-
return undefined;
|
|
791
|
-
await Signal.race(this.#state.groups, this.#cursor, this.#state.closed);
|
|
792
1176
|
continue;
|
|
793
1177
|
}
|
|
794
|
-
|
|
795
|
-
if (group.skipped) {
|
|
796
|
-
// Fell behind this group's eviction window. Drop it and signal the gap;
|
|
797
|
-
// the next read resyncs from the following group.
|
|
798
|
-
groups.shift()?.close();
|
|
799
|
-
throw new Lagged();
|
|
800
|
-
}
|
|
801
|
-
const next = group.tryReadFrameSequence();
|
|
802
|
-
if (next)
|
|
1178
|
+
if (next) {
|
|
803
1179
|
return {
|
|
804
1180
|
group: group.sequence,
|
|
805
1181
|
frame: next.sequence,
|
|
806
1182
|
payload: next.payload,
|
|
807
1183
|
timestamp: next.timestamp,
|
|
808
1184
|
};
|
|
809
|
-
const closed = this.#state.closed.peek();
|
|
810
|
-
if (closed instanceof Error)
|
|
811
|
-
throw closed;
|
|
812
|
-
if (closed !== undefined)
|
|
813
|
-
return undefined;
|
|
814
|
-
// A finished (drained + closed) group has nothing left: drop it and loop, rather than
|
|
815
|
-
// busy-waiting on its already-resolved readable() (which would livelock and starve the
|
|
816
|
-
// macrotask that delivers the next group).
|
|
817
|
-
if (group.done) {
|
|
818
|
-
groups.shift()?.close();
|
|
819
|
-
continue;
|
|
820
1185
|
}
|
|
821
|
-
//
|
|
822
|
-
|
|
823
|
-
|
|
1186
|
+
// The group is exhausted (or the budget abandoned its stall); move on.
|
|
1187
|
+
this.#frameGroup = undefined;
|
|
1188
|
+
group.close();
|
|
824
1189
|
}
|
|
825
1190
|
}
|
|
826
1191
|
/** Reads the next frame and decodes it as a UTF-8 string. */
|
|
827
|
-
async readString() {
|
|
828
|
-
const next = await this
|
|
1192
|
+
async #readString() {
|
|
1193
|
+
const next = await this.#readFrame();
|
|
829
1194
|
if (!next)
|
|
830
1195
|
return undefined;
|
|
831
1196
|
return new TextDecoder().decode(next.payload);
|
|
832
1197
|
}
|
|
833
1198
|
/** Reads the next frame and parses it as JSON. */
|
|
834
|
-
async readJson() {
|
|
835
|
-
const next = await this
|
|
1199
|
+
async #readJson() {
|
|
1200
|
+
const next = await this.#readString();
|
|
836
1201
|
if (!next)
|
|
837
1202
|
return undefined;
|
|
838
1203
|
return JSON.parse(next);
|
|
839
1204
|
}
|
|
840
1205
|
/** Reads the next frame and decodes it as a one-byte boolean, throwing on a malformed frame. */
|
|
841
|
-
async readBool() {
|
|
842
|
-
const next = await this
|
|
1206
|
+
async #readBool() {
|
|
1207
|
+
const next = await this.#readFrame();
|
|
843
1208
|
if (!next)
|
|
844
1209
|
return undefined;
|
|
845
1210
|
const payload = next.payload;
|
|
@@ -847,6 +1212,25 @@ export class Subscriber {
|
|
|
847
1212
|
throw new Error("invalid bool frame");
|
|
848
1213
|
return payload[0] === 1;
|
|
849
1214
|
}
|
|
1215
|
+
/**
|
|
1216
|
+
* Read this track's groups in sequence order instead of arrival order.
|
|
1217
|
+
*
|
|
1218
|
+
* Both cursors draw from the same buffer, so a track is read one way or the other: this
|
|
1219
|
+
* hands the subscription to the returned {@link Ordered} and leaves this handle inert.
|
|
1220
|
+
* {@link recvGroup} and {@link recvDatagram} throw afterwards. Throws once a
|
|
1221
|
+
* {@link recvGroup} call has already committed the track to arrival order.
|
|
1222
|
+
*
|
|
1223
|
+
* Datagrams come along: they are a separate cursor either way, so the choice of group
|
|
1224
|
+
* order says nothing about them, and reading them commits nothing.
|
|
1225
|
+
*/
|
|
1226
|
+
ordered() {
|
|
1227
|
+
if (this.#mode === "ordered")
|
|
1228
|
+
throw new Error("track is already read in sequence order");
|
|
1229
|
+
if (this.#mode === "arrival")
|
|
1230
|
+
throw new Error("track is already read in arrival order");
|
|
1231
|
+
this.#mode = "ordered";
|
|
1232
|
+
return makeOrdered(this);
|
|
1233
|
+
}
|
|
850
1234
|
/**
|
|
851
1235
|
* Update this subscription's options (e.g. priority), triggering a SUBSCRIBE_UPDATE to the
|
|
852
1236
|
* publisher. Mirrors the Rust `Subscriber::update`.
|
|
@@ -855,4 +1239,118 @@ export class Subscriber {
|
|
|
855
1239
|
this.#state.update.set(subscriptionDefaults(options));
|
|
856
1240
|
}
|
|
857
1241
|
}
|
|
1242
|
+
/**
|
|
1243
|
+
* A {@link Subscriber} that reads groups in sequence order.
|
|
1244
|
+
*
|
|
1245
|
+
* Created by {@link Subscriber.ordered}, which takes the subscription over: the two
|
|
1246
|
+
* cursors draw from one buffer, so a track is read one way or the other and never both.
|
|
1247
|
+
* Every group this returns has a higher sequence than the last, so a late arrival is
|
|
1248
|
+
* skipped rather than delivered out of turn.
|
|
1249
|
+
*
|
|
1250
|
+
* `maxAge` applies as this cursor reads, exactly as it does on the arrival cursor: a
|
|
1251
|
+
* group is skipped once its reach, where its immediate successor begins, is that far
|
|
1252
|
+
* behind the newest frame on the track. Nothing weaker convicts it, since the reach is
|
|
1253
|
+
* the only proof that every frame it could still hold is past the budget, so a backlog
|
|
1254
|
+
* inside the budget is still delivered whole, as a burst in order. The budget follows a
|
|
1255
|
+
* group already handed out too: a stalled group's pending frame read rejects once newer
|
|
1256
|
+
* content has pulled that far ahead.
|
|
1257
|
+
*/
|
|
1258
|
+
export class Ordered {
|
|
1259
|
+
/** The track name. */
|
|
1260
|
+
name;
|
|
1261
|
+
#subscriber;
|
|
1262
|
+
constructor(subscriber) {
|
|
1263
|
+
this.name = subscriber.name;
|
|
1264
|
+
this.#subscriber = subscriber;
|
|
1265
|
+
}
|
|
1266
|
+
static {
|
|
1267
|
+
makeOrdered = (subscriber) => new Ordered(subscriber);
|
|
1268
|
+
}
|
|
1269
|
+
/** Resolve this track's immutable publisher properties; see {@link Subscriber.info}. */
|
|
1270
|
+
info() {
|
|
1271
|
+
return this.#subscriber.info();
|
|
1272
|
+
}
|
|
1273
|
+
/** Settles once the track closes; see {@link Producer.closed}. */
|
|
1274
|
+
get closed() {
|
|
1275
|
+
return this.#subscriber.closed;
|
|
1276
|
+
}
|
|
1277
|
+
/** This subscriber's current options, including defaults and the last {@link update}. */
|
|
1278
|
+
get subscription() {
|
|
1279
|
+
return this.#subscriber.subscription;
|
|
1280
|
+
}
|
|
1281
|
+
/** The latest group sequence observed on this track, if any. */
|
|
1282
|
+
latest() {
|
|
1283
|
+
return this.#subscriber.latest();
|
|
1284
|
+
}
|
|
1285
|
+
/** The track's exclusive final boundary; see {@link Subscriber.final}. */
|
|
1286
|
+
final() {
|
|
1287
|
+
return this.#subscriber.final();
|
|
1288
|
+
}
|
|
1289
|
+
/** Resolve with the track's exclusive final boundary once known; see {@link Subscriber.finished}. */
|
|
1290
|
+
finished() {
|
|
1291
|
+
return this.#subscriber.finished();
|
|
1292
|
+
}
|
|
1293
|
+
/** Limit subsequent reads to these groups and return this reader for chaining. */
|
|
1294
|
+
withGroups(groups) {
|
|
1295
|
+
this.setGroups(groups);
|
|
1296
|
+
return this;
|
|
1297
|
+
}
|
|
1298
|
+
/** Limit subsequent reads to these groups; see {@link Subscriber.setGroups}. */
|
|
1299
|
+
setGroups(groups) {
|
|
1300
|
+
this.#subscriber.setGroups(groups);
|
|
1301
|
+
}
|
|
1302
|
+
/** Update this subscription's options; see {@link Subscriber.update}. */
|
|
1303
|
+
update(options) {
|
|
1304
|
+
this.#subscriber.update(options);
|
|
1305
|
+
}
|
|
1306
|
+
/** Close the track (optionally with an error), closing any pending groups. Idempotent. */
|
|
1307
|
+
close(abort) {
|
|
1308
|
+
this.#subscriber.close(abort);
|
|
1309
|
+
}
|
|
1310
|
+
/**
|
|
1311
|
+
* Return the next group with a strictly-greater sequence number than the last returned.
|
|
1312
|
+
*
|
|
1313
|
+
* Late arrivals (sequence at or below the last returned) are silently skipped, as is a
|
|
1314
|
+
* group whose every frame is further behind the live edge than `maxAge` (the default of
|
|
1315
|
+
* zero keeps only what nothing newer has superseded). Honors the bounds set by
|
|
1316
|
+
* {@link setGroups}.
|
|
1317
|
+
*/
|
|
1318
|
+
nextGroup() {
|
|
1319
|
+
return ordered_.nextGroup(this.#subscriber);
|
|
1320
|
+
}
|
|
1321
|
+
/**
|
|
1322
|
+
* Read the next frame across groups, in sequence order, with its group and frame numbers.
|
|
1323
|
+
*
|
|
1324
|
+
* Rides the same cursor as {@link nextGroup} and shares this handle's contract: a
|
|
1325
|
+
* buffered backlog is drained in full up to the point `maxAge` proves it useless.
|
|
1326
|
+
* Treat the returned frame bytes as read-only; they are shared with other consumers.
|
|
1327
|
+
*/
|
|
1328
|
+
readFrame() {
|
|
1329
|
+
return ordered_.readFrame(this.#subscriber);
|
|
1330
|
+
}
|
|
1331
|
+
/** Read the next frame and decode it as a UTF-8 string. */
|
|
1332
|
+
readString() {
|
|
1333
|
+
return ordered_.readString(this.#subscriber);
|
|
1334
|
+
}
|
|
1335
|
+
/** Read the next frame and parse it as JSON. */
|
|
1336
|
+
readJson() {
|
|
1337
|
+
return ordered_.readJson(this.#subscriber);
|
|
1338
|
+
}
|
|
1339
|
+
/** Read the next frame and decode it as a one-byte boolean, throwing on a malformed frame. */
|
|
1340
|
+
readBool() {
|
|
1341
|
+
return ordered_.readBool(this.#subscriber);
|
|
1342
|
+
}
|
|
1343
|
+
/**
|
|
1344
|
+
* Receive the next datagram in arrival order.
|
|
1345
|
+
*
|
|
1346
|
+
* Datagrams are a separate best-effort channel from groups (see
|
|
1347
|
+
* {@link Producer.appendDatagram}); they share only the sequence namespace, and
|
|
1348
|
+
* neither cursor moves the other. Unordered by construction, so this behaves
|
|
1349
|
+
* identically on either handle; it is here so a track carrying both channels needs
|
|
1350
|
+
* one subscription rather than two.
|
|
1351
|
+
*/
|
|
1352
|
+
recvDatagram() {
|
|
1353
|
+
return ordered_.recvDatagram(this.#subscriber);
|
|
1354
|
+
}
|
|
1355
|
+
}
|
|
858
1356
|
//# sourceMappingURL=track.js.map
|