@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/ietf/subscriber.js
CHANGED
|
@@ -1,18 +1,24 @@
|
|
|
1
1
|
/* @ts-self-types="./subscriber.d.ts" */
|
|
2
|
+
import { race, Signal } from "@moq/signals";
|
|
2
3
|
import * as announce from "../announced.js";
|
|
3
4
|
import * as broadcast from "../broadcast.js";
|
|
4
5
|
import { BroadcastCache } from "../consume.js";
|
|
5
|
-
import { error, ProtocolViolation, reason } from "../error.js";
|
|
6
|
+
import { closeError, controlTimeout, error, ProtocolViolation, reason, sessionCause } from "../error.js";
|
|
6
7
|
import * as netGroup from "../group.js";
|
|
8
|
+
import { Cost, routesEqual, UNKNOWN_HOP } from "../hop.js";
|
|
9
|
+
import { hiddenBelow, hooks, scopeCaptures, scopeHead, scopeOverlaps } from "../internal.js";
|
|
7
10
|
import * as Path from "../path.js";
|
|
11
|
+
import { TAIL_GRACE_MS, Tail } from "../tail.js";
|
|
8
12
|
import { Timestamp } from "../time.js";
|
|
9
|
-
import { withTimeout } from "../util/timeout.js";
|
|
13
|
+
import { TimeoutError, withTimeout } from "../util/timeout.js";
|
|
14
|
+
import { overrideBroadcastWire, wireOf } from "../wire.js";
|
|
10
15
|
import { DuplicateTrackAlias, RetiredTrackAlias, TrackAliases } from "./aliases.js";
|
|
11
16
|
import * as Cluster from "./cluster.js";
|
|
17
|
+
import { requestReason, toRequestCode } from "./error.js";
|
|
12
18
|
import { Frame } from "./object.js";
|
|
13
|
-
import { toWire } from "./priority.js";
|
|
14
|
-
import { PublishError } from "./publish.js";
|
|
15
|
-
import {
|
|
19
|
+
import { fromWire, toWire } from "./priority.js";
|
|
20
|
+
import { PublishDone, PublishError, publishDoneClean } from "./publish.js";
|
|
21
|
+
import { PublishNamespaceDone, PublishNamespaceError, PublishNamespaceOk, PublishNamespaceUpdate, } from "./publish_namespace.js";
|
|
16
22
|
import { RequestError, RequestOk } from "./request.js";
|
|
17
23
|
import { joinFilter, Subscribe, SubscribeError, SubscribeOk, Unsubscribe } from "./subscribe.js";
|
|
18
24
|
import { PublishBlocked, SubscribeNamespace, SubscribeNamespaceEntry, SubscribeNamespaceEntryDone, SubscribeNamespaceLegacy, SubscribeNamespaceOk, UnsubscribeNamespace, } from "./subscribe_namespace.js";
|
|
@@ -21,6 +27,10 @@ import { Version } from "./version.js";
|
|
|
21
27
|
// concurrent QUIC streams (Chrome ~100); past the cap openBi() silently
|
|
22
28
|
// blocks. The timeout turns that into a clear error.
|
|
23
29
|
const SUBSCRIBE_OK_TIMEOUT_MS = 10_000;
|
|
30
|
+
/** Whether a reader with `filter` sees an announcement at `path`. */
|
|
31
|
+
function sees(filter, path) {
|
|
32
|
+
return scopeOverlaps(filter.scope, path) && (filter.hidden || !hiddenBelow(filter.prefix, path));
|
|
33
|
+
}
|
|
24
34
|
/**
|
|
25
35
|
* Handles subscribing to broadcasts using moq-transport protocol.
|
|
26
36
|
* Uses the stream-per-request pattern (real bidi streams for v17, virtual for v14-v16).
|
|
@@ -29,6 +39,9 @@ const SUBSCRIBE_OK_TIMEOUT_MS = 10_000;
|
|
|
29
39
|
*/
|
|
30
40
|
export class Subscriber {
|
|
31
41
|
#session;
|
|
42
|
+
// The transport, so a request cut off by the session's close ends with the session's
|
|
43
|
+
// error. Optional for tests that drive a bare session.
|
|
44
|
+
#quic;
|
|
32
45
|
// The Hop IDs this session declared; see {@link Cluster}. What the peer declared is what
|
|
33
46
|
// says whether an advertisement carries a hop path, and ours is what a path looping back
|
|
34
47
|
// to us contains.
|
|
@@ -53,16 +66,20 @@ export class Subscriber {
|
|
|
53
66
|
// Counting them is what keeps the second from duplicating the announce and the first
|
|
54
67
|
// to end from retracting what the other still holds.
|
|
55
68
|
#announced = new Map();
|
|
56
|
-
// Any consumers that want each new announcement.
|
|
57
|
-
#announcedConsumers = new
|
|
69
|
+
// Any consumers that want each new announcement, keyed by their local filter.
|
|
70
|
+
#announcedConsumers = new Map();
|
|
71
|
+
// Whether the peer understands the HIDDEN parameter (MoQ Hidden).
|
|
72
|
+
#hidden;
|
|
58
73
|
/**
|
|
59
74
|
* Creates a new Subscriber instance.
|
|
60
75
|
*
|
|
61
76
|
* @internal
|
|
62
77
|
*/
|
|
63
|
-
constructor({ session, cluster, }) {
|
|
78
|
+
constructor({ session, quic, cluster, hidden = false, }) {
|
|
64
79
|
this.#session = session;
|
|
80
|
+
this.#quic = quic;
|
|
65
81
|
this.#cluster = cluster;
|
|
82
|
+
this.#hidden = hidden;
|
|
66
83
|
}
|
|
67
84
|
/**
|
|
68
85
|
* Whether an advertisement is ours coming back: its hop path already ran through us, so
|
|
@@ -75,23 +92,42 @@ export class Subscriber {
|
|
|
75
92
|
#reflected(advert) {
|
|
76
93
|
return advert !== undefined && this.#cluster !== undefined && Cluster.loops(advert, this.#cluster.self);
|
|
77
94
|
}
|
|
95
|
+
/** The route an advertisement carries; one without a path is anonymous and free. */
|
|
96
|
+
#route(advert) {
|
|
97
|
+
if (advert === undefined)
|
|
98
|
+
return { hops: [UNKNOWN_HOP], cost: Cost.zero };
|
|
99
|
+
return { hops: advert.hops, cost: { warm: advert.cost, cold: advert.cost } };
|
|
100
|
+
}
|
|
78
101
|
/**
|
|
79
|
-
* Gets an announced reader
|
|
102
|
+
* Gets an announced reader matching `scope`. Paths are relative to the session,
|
|
103
|
+
* not the scope.
|
|
80
104
|
*
|
|
81
105
|
* The peer is asked with SUBSCRIBE_NAMESPACE regardless of what it declared, and an
|
|
82
106
|
* unsolicited PUBLISH_NAMESPACE lands here too, so a peer that only tells and one
|
|
83
107
|
* that only answers are both discovered.
|
|
108
|
+
*
|
|
109
|
+
* Hidden routes (a `.`-prefixed segment below the scope's head) are left out unless
|
|
110
|
+
* `options.hidden` opts in. The opt-in rides the SUBSCRIBE_NAMESPACE when the peer
|
|
111
|
+
* understands it (MoQ Hidden); the rule is also applied here, since an unsolicited
|
|
112
|
+
* PUBLISH_NAMESPACE or a peer that never heard of it hides nothing.
|
|
84
113
|
*/
|
|
85
|
-
announced(
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
114
|
+
announced(scope = Path.Pattern.all(), options) {
|
|
115
|
+
// The wire speaks announce interest by prefix.
|
|
116
|
+
const prefix = scopeHead(scope);
|
|
117
|
+
const filter = { scope, prefix, hidden: options?.hidden ?? false };
|
|
118
|
+
const announced = new announce.Producer();
|
|
119
|
+
for (const [active, info] of this.#announced) {
|
|
120
|
+
if (!sees(filter, active))
|
|
90
121
|
continue;
|
|
91
|
-
announced.append({
|
|
122
|
+
announced.append({
|
|
123
|
+
prefix: active,
|
|
124
|
+
captures: scopeCaptures(scope, active),
|
|
125
|
+
kind: "announced",
|
|
126
|
+
route: info.route,
|
|
127
|
+
});
|
|
92
128
|
}
|
|
93
|
-
this.#announcedConsumers.
|
|
94
|
-
void this.#runAnnounced(announced, prefix).finally(() => {
|
|
129
|
+
this.#announcedConsumers.set(announced, filter);
|
|
130
|
+
void this.#runAnnounced(announced, prefix, filter.hidden && this.#hidden).finally(() => {
|
|
95
131
|
this.#announcedConsumers.delete(announced);
|
|
96
132
|
announced.close();
|
|
97
133
|
});
|
|
@@ -101,28 +137,48 @@ export class Subscriber {
|
|
|
101
137
|
* Record one more advertisement for a path, telling consumers only when it is the
|
|
102
138
|
* first. A second one is the same namespace said twice, not news.
|
|
103
139
|
*/
|
|
104
|
-
#attachAnnounce(path) {
|
|
105
|
-
const
|
|
106
|
-
|
|
107
|
-
|
|
140
|
+
#attachAnnounce(path, route) {
|
|
141
|
+
const existing = this.#announced.get(path);
|
|
142
|
+
if (existing) {
|
|
143
|
+
existing.count += 1;
|
|
108
144
|
return;
|
|
145
|
+
}
|
|
146
|
+
this.#announced.set(path, { count: 1, route });
|
|
109
147
|
console.debug(`announced: broadcast=${path} active=true`);
|
|
110
|
-
for (const consumer of this.#announcedConsumers) {
|
|
111
|
-
|
|
112
|
-
if (suffix === null)
|
|
148
|
+
for (const [consumer, filter] of this.#announcedConsumers) {
|
|
149
|
+
if (!sees(filter, path))
|
|
113
150
|
continue;
|
|
114
|
-
|
|
151
|
+
const scope = filter.scope;
|
|
152
|
+
consumer.append({ prefix: path, captures: scopeCaptures(scope, path), kind: "announced", route });
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Replace the stored route for a path that is already announced. A no-op when the
|
|
157
|
+
* hops and cost did not change; otherwise consumers hear `updated` so a forwarder
|
|
158
|
+
* can reprice without retracting.
|
|
159
|
+
*/
|
|
160
|
+
#updateAnnounce(path, route) {
|
|
161
|
+
const existing = this.#announced.get(path);
|
|
162
|
+
if (existing === undefined || routesEqual(existing.route, route))
|
|
163
|
+
return;
|
|
164
|
+
existing.route = route;
|
|
165
|
+
console.debug(`announced: broadcast=${path} rerouted`);
|
|
166
|
+
for (const [consumer, filter] of this.#announcedConsumers) {
|
|
167
|
+
if (!sees(filter, path))
|
|
168
|
+
continue;
|
|
169
|
+
const scope = filter.scope;
|
|
170
|
+
consumer.append({ prefix: path, captures: scopeCaptures(scope, path), kind: "updated", route });
|
|
115
171
|
}
|
|
116
172
|
}
|
|
117
173
|
/**
|
|
118
174
|
* Drop one advertisement for a path, retracting it only once the last one goes.
|
|
119
175
|
*/
|
|
120
176
|
#detachAnnounce(path) {
|
|
121
|
-
const
|
|
122
|
-
if (
|
|
177
|
+
const existing = this.#announced.get(path);
|
|
178
|
+
if (existing === undefined)
|
|
123
179
|
return;
|
|
124
|
-
if (count > 1) {
|
|
125
|
-
|
|
180
|
+
if (existing.count > 1) {
|
|
181
|
+
existing.count -= 1;
|
|
126
182
|
return;
|
|
127
183
|
}
|
|
128
184
|
this.#announced.delete(path);
|
|
@@ -130,19 +186,24 @@ export class Subscriber {
|
|
|
130
186
|
// would otherwise hand the dead generation to whoever consumes the path next.
|
|
131
187
|
this.#consumes.evict(path);
|
|
132
188
|
console.debug(`announced: broadcast=${path} active=false`);
|
|
133
|
-
for (const consumer of this.#announcedConsumers) {
|
|
134
|
-
|
|
135
|
-
if (suffix === null)
|
|
189
|
+
for (const [consumer, filter] of this.#announcedConsumers) {
|
|
190
|
+
if (!sees(filter, path))
|
|
136
191
|
continue;
|
|
192
|
+
const scope = filter.scope;
|
|
137
193
|
try {
|
|
138
|
-
consumer.append({
|
|
194
|
+
consumer.append({
|
|
195
|
+
prefix: path,
|
|
196
|
+
captures: scopeCaptures(scope, path),
|
|
197
|
+
kind: "retracted",
|
|
198
|
+
route: existing.route,
|
|
199
|
+
});
|
|
139
200
|
}
|
|
140
201
|
catch {
|
|
141
202
|
// Consumer already closed, will be cleaned up
|
|
142
203
|
}
|
|
143
204
|
}
|
|
144
205
|
}
|
|
145
|
-
async #runAnnounced(announced, prefix) {
|
|
206
|
+
async #runAnnounced(announced, prefix, hidden) {
|
|
146
207
|
const version = this.#session.version;
|
|
147
208
|
// Suffixes live on this stream, so a repeat is recognized as an update to the
|
|
148
209
|
// advertisement rather than a second one, which would leak the count.
|
|
@@ -171,11 +232,11 @@ export class Subscriber {
|
|
|
171
232
|
version === Version.DRAFT_16 ||
|
|
172
233
|
version === Version.DRAFT_17) {
|
|
173
234
|
await stream.writer.u53(SubscribeNamespaceLegacy.id);
|
|
174
|
-
await new SubscribeNamespaceLegacy({ namespace: prefix, requestId }).encode(stream.writer, version);
|
|
235
|
+
await new SubscribeNamespaceLegacy({ namespace: prefix, requestId, hidden }).encode(stream.writer, version);
|
|
175
236
|
}
|
|
176
237
|
else {
|
|
177
238
|
await stream.writer.u53(SubscribeNamespace.id);
|
|
178
|
-
await new SubscribeNamespace({ namespace: prefix, requestId }).encode(stream.writer, version);
|
|
239
|
+
await new SubscribeNamespace({ namespace: prefix, requestId, hidden }).encode(stream.writer, version);
|
|
179
240
|
}
|
|
180
241
|
console.debug(`subscribe_namespace written: requestId=${requestId}`);
|
|
181
242
|
// Read response
|
|
@@ -212,10 +273,15 @@ export class Subscriber {
|
|
|
212
273
|
this.#detachAnnounce(path);
|
|
213
274
|
continue;
|
|
214
275
|
}
|
|
215
|
-
// A repeat
|
|
216
|
-
|
|
276
|
+
// A repeat replaces the advertisement in place: HOP_PATH / ROUTE_COST
|
|
277
|
+
// can change without a NAMESPACE_DONE. Only the first is a new path.
|
|
278
|
+
const route = this.#route(entry.cluster);
|
|
279
|
+
if (live.has(path)) {
|
|
280
|
+
this.#updateAnnounce(path, route);
|
|
281
|
+
}
|
|
282
|
+
else {
|
|
217
283
|
live.add(path);
|
|
218
|
-
this.#attachAnnounce(path);
|
|
284
|
+
this.#attachAnnounce(path, route);
|
|
219
285
|
}
|
|
220
286
|
}
|
|
221
287
|
else if (msgType === SubscribeNamespaceEntryDone.id) {
|
|
@@ -246,7 +312,7 @@ export class Subscriber {
|
|
|
246
312
|
this.#session.close();
|
|
247
313
|
});
|
|
248
314
|
// Wait for either the read loop or the announced to close
|
|
249
|
-
await
|
|
315
|
+
await race([readLoop, announced.closed]);
|
|
250
316
|
// For v14/v15: send UnsubscribeNamespace before closing
|
|
251
317
|
if (version === Version.DRAFT_14 || version === Version.DRAFT_15) {
|
|
252
318
|
try {
|
|
@@ -308,7 +374,7 @@ export class Subscriber {
|
|
|
308
374
|
const consumer = new ConsumeBroadcast();
|
|
309
375
|
void (async () => {
|
|
310
376
|
for (;;) {
|
|
311
|
-
const request = await consumer.requested();
|
|
377
|
+
const request = await wireOf(consumer).requested();
|
|
312
378
|
if (!request)
|
|
313
379
|
break;
|
|
314
380
|
void this.#runSubscribe(path, request);
|
|
@@ -316,28 +382,43 @@ export class Subscriber {
|
|
|
316
382
|
})();
|
|
317
383
|
return consumer;
|
|
318
384
|
}
|
|
385
|
+
// The adapter is gone. If the transport has already closed, that close is the
|
|
386
|
+
// error. A still-open transport, such as a GOAWAY drain, has no peer code yet,
|
|
387
|
+
// so this does not wait for it.
|
|
388
|
+
async #closedSession() {
|
|
389
|
+
const quic = this.#quic;
|
|
390
|
+
if (!quic)
|
|
391
|
+
return new Error("session closed");
|
|
392
|
+
return Promise.race([
|
|
393
|
+
closeError(quic),
|
|
394
|
+
new Promise((resolve) => queueMicrotask(() => resolve(new Error("session closed")))),
|
|
395
|
+
]);
|
|
396
|
+
}
|
|
319
397
|
async #runSubscribe(broadcast, request) {
|
|
320
398
|
const requestId = await this.#session.nextRequestId();
|
|
321
399
|
if (requestId === undefined) {
|
|
322
|
-
request.reject(
|
|
400
|
+
request.reject(await this.#closedSession());
|
|
323
401
|
return;
|
|
324
402
|
}
|
|
325
403
|
console.debug(`subscribe start: id=${requestId} broadcast=${broadcast} track=${request.name}`);
|
|
326
|
-
//
|
|
327
|
-
//
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
// incoming object streams are routed into.
|
|
331
|
-
const producer = request.accept({ ordered: false });
|
|
404
|
+
// Keep the request pending until SUBSCRIBE_OK supplies immutable track metadata.
|
|
405
|
+
// Group streams already wait on the alias, so early data stays behind this response.
|
|
406
|
+
const producer = hooks.pendingTrackProducer(request);
|
|
407
|
+
const subscription = { track: producer, tail: new Tail() };
|
|
332
408
|
// Open the stream and wait for SUBSCRIBE_OK under a timeout. State
|
|
333
409
|
// flows back via `state` so the timeout path can clean up the stream
|
|
334
410
|
// and any registration if setup eventually finishes.
|
|
335
411
|
const state = {};
|
|
336
|
-
const setup = this.#openSubscribe(state, broadcast, request,
|
|
412
|
+
const setup = this.#openSubscribe(state, broadcast, request, subscription, requestId);
|
|
337
413
|
// The publisher can be serving before it answers, so waiting only on the response
|
|
338
414
|
// would miss the local side going away and leave it serving a track nobody reads.
|
|
339
415
|
// Demand returning before we commit is not abandonment, matching the serving loop.
|
|
340
416
|
const waitAbandoned = async () => {
|
|
417
|
+
// An info-only lookup attaches no subscriber yet still waits on SUBSCRIBE_OK for
|
|
418
|
+
// the track info, so only demand that arrived and then left is abandonment.
|
|
419
|
+
while (!producer.used.peek() && producer.closed.peek() === undefined) {
|
|
420
|
+
await Signal.race(producer.used, producer.closed);
|
|
421
|
+
}
|
|
341
422
|
for (;;) {
|
|
342
423
|
await producer.unused();
|
|
343
424
|
if (producer.closed.peek() !== undefined || !producer.used.peek())
|
|
@@ -347,7 +428,7 @@ export class Subscriber {
|
|
|
347
428
|
let stream;
|
|
348
429
|
let trackAlias;
|
|
349
430
|
try {
|
|
350
|
-
const result = await
|
|
431
|
+
const result = await race([
|
|
351
432
|
withTimeout(setup, SUBSCRIBE_OK_TIMEOUT_MS, `subscribe timed out after ${SUBSCRIBE_OK_TIMEOUT_MS}ms waiting for SUBSCRIBE_OK (browser stream limit reached?)`),
|
|
352
433
|
waitAbandoned(),
|
|
353
434
|
]);
|
|
@@ -358,8 +439,9 @@ export class Subscriber {
|
|
|
358
439
|
console.debug(`subscribe ok: id=${requestId} broadcast=${broadcast} track=${request.name}`);
|
|
359
440
|
}
|
|
360
441
|
catch (err) {
|
|
361
|
-
|
|
362
|
-
|
|
442
|
+
// A control request that timed out is not late content, so it carries its own code.
|
|
443
|
+
const e = err instanceof TimeoutError ? controlTimeout(err) : await sessionCause(this.#quic, err);
|
|
444
|
+
request.reject(e);
|
|
363
445
|
console.warn(`subscribe error: id=${requestId} broadcast=${broadcast} track=${request.name} error=${reason(e)}`);
|
|
364
446
|
// Runs now for whatever is already open, and again if `setup` settles late.
|
|
365
447
|
// Retirement is repeated because a late SUBSCRIBE_OK can register an alias after
|
|
@@ -367,7 +449,7 @@ export class Subscriber {
|
|
|
367
449
|
let torn = false;
|
|
368
450
|
const cleanup = async (afterSetup) => {
|
|
369
451
|
state.cancelled = true;
|
|
370
|
-
if (state.registeredAlias !== undefined && this.#aliases.retire(state.registeredAlias,
|
|
452
|
+
if (state.registeredAlias !== undefined && this.#aliases.retire(state.registeredAlias, subscription)) {
|
|
371
453
|
this.#timescales.delete(state.registeredAlias);
|
|
372
454
|
}
|
|
373
455
|
if (!state.stream || torn)
|
|
@@ -402,10 +484,10 @@ export class Subscriber {
|
|
|
402
484
|
const publisherEnded = Symbol("publisher");
|
|
403
485
|
const localEnded = Symbol("local");
|
|
404
486
|
const idle = Symbol("idle");
|
|
405
|
-
// Terminal conditions settle at most once (
|
|
406
|
-
//
|
|
407
|
-
const done =
|
|
408
|
-
stream.
|
|
487
|
+
// Terminal conditions settle at most once (PublishDone, track close = local
|
|
488
|
+
// unsubscribe); race them once so the demand loop doesn't re-subscribe each pass.
|
|
489
|
+
const done = race([
|
|
490
|
+
this.#runPublishDone(stream, subscription).then(() => publisherEnded),
|
|
409
491
|
producer.closed.then(() => localEnded),
|
|
410
492
|
]);
|
|
411
493
|
// Serve until a terminal condition fires or the last local subscriber leaves. The unused
|
|
@@ -413,22 +495,26 @@ export class Subscriber {
|
|
|
413
495
|
// down resumes on the same stream.
|
|
414
496
|
let terminal = localEnded;
|
|
415
497
|
for (;;) {
|
|
416
|
-
const reason = await
|
|
498
|
+
const reason = await race([done, producer.unused().then(() => idle)]);
|
|
417
499
|
if (reason === idle && producer.closed.peek() === undefined && producer.used.peek())
|
|
418
500
|
continue;
|
|
419
501
|
terminal = reason;
|
|
420
502
|
break;
|
|
421
503
|
}
|
|
504
|
+
// Close before the cancellation is written, not after: awaiting the write first
|
|
505
|
+
// reopens the window the demand re-check above just closed, and a subscriber that
|
|
506
|
+
// returned during it would be closed by this line. The lite subscriber closes
|
|
507
|
+
// straight out of its loop for the same reason.
|
|
508
|
+
producer.close();
|
|
422
509
|
// The publisher already ended the request, so there is nothing to cancel. Sending
|
|
423
510
|
// UNSUBSCRIBE here would name a request it has already torn down.
|
|
424
511
|
if (terminal !== publisherEnded)
|
|
425
512
|
await this.#cancelSubscribe(stream, requestId);
|
|
426
|
-
producer.close();
|
|
427
513
|
stream.close();
|
|
428
514
|
console.debug(`subscribe close: id=${requestId} broadcast=${broadcast} track=${request.name}`);
|
|
429
515
|
}
|
|
430
516
|
catch (err) {
|
|
431
|
-
const e =
|
|
517
|
+
const e = await sessionCause(this.#quic, err);
|
|
432
518
|
producer.close(e);
|
|
433
519
|
stream.abort(e);
|
|
434
520
|
console.warn(`subscribe error: id=${requestId} broadcast=${broadcast} track=${request.name} error=${reason(e)}`);
|
|
@@ -436,10 +522,37 @@ export class Subscriber {
|
|
|
436
522
|
finally {
|
|
437
523
|
// Only the owner tears down the alias metadata: a later subscription may have
|
|
438
524
|
// reclaimed the alias and installed its own timescale.
|
|
439
|
-
if (this.#aliases.retire(trackAlias,
|
|
525
|
+
if (this.#aliases.retire(trackAlias, subscription))
|
|
440
526
|
this.#timescales.delete(trackAlias);
|
|
441
527
|
}
|
|
442
528
|
}
|
|
529
|
+
/**
|
|
530
|
+
* Read the PUBLISH_DONE that ends a subscription, then wait for the data streams it counts.
|
|
531
|
+
*
|
|
532
|
+
* An error status aborts the track with it. A clean one leaves streams in flight, since
|
|
533
|
+
* QUIC does not order them, so wait until the Stream Count many have been read, or a
|
|
534
|
+
* bounded grace for the ones that never arrive (the draft says to use a timeout). The count
|
|
535
|
+
* is only a hint: a peer may send 0 regardless, so 0 waits out the grace. A request stream
|
|
536
|
+
* that ends without one ends the track the same way.
|
|
537
|
+
*/
|
|
538
|
+
async #runPublishDone(stream, subscription) {
|
|
539
|
+
const version = this.#session.version;
|
|
540
|
+
let count;
|
|
541
|
+
if (!(await stream.reader.done())) {
|
|
542
|
+
const typeId = await stream.reader.u53();
|
|
543
|
+
if (typeId !== PublishDone.id) {
|
|
544
|
+
throw new ProtocolViolation(`unexpected message on a subscription: 0x${typeId.toString(16)}`);
|
|
545
|
+
}
|
|
546
|
+
const done = await PublishDone.decode(stream.reader, version);
|
|
547
|
+
if (!publishDoneClean(done.statusCode, version)) {
|
|
548
|
+
throw new Error(`publish done: status=0x${done.statusCode.toString(16)} reason=${done.reasonPhrase}`);
|
|
549
|
+
}
|
|
550
|
+
count = done.streamCount;
|
|
551
|
+
}
|
|
552
|
+
const { tail, track } = subscription;
|
|
553
|
+
const complete = () => count !== undefined && count > 0n && BigInt(tail.streams) >= count;
|
|
554
|
+
await tail.settle(complete, TAIL_GRACE_MS, track.closed);
|
|
555
|
+
}
|
|
443
556
|
/**
|
|
444
557
|
* Tell the publisher to stop serving a subscription we are walking away from.
|
|
445
558
|
*
|
|
@@ -465,7 +578,7 @@ export class Subscriber {
|
|
|
465
578
|
// `state` is populated as soon as the stream opens and again when the
|
|
466
579
|
// trackAlias is registered, so the caller can clean both up on timeout
|
|
467
580
|
// even before this promise settles.
|
|
468
|
-
async #openSubscribe(state, broadcast, request,
|
|
581
|
+
async #openSubscribe(state, broadcast, request, subscription, requestId) {
|
|
469
582
|
const version = this.#session.version;
|
|
470
583
|
state.stream = await this.#session.openBi();
|
|
471
584
|
// The timeout can fire while the open is still in flight, in which case cleanup ran
|
|
@@ -503,7 +616,7 @@ export class Subscriber {
|
|
|
503
616
|
const err = version === Version.DRAFT_14
|
|
504
617
|
? await SubscribeError.decode(state.stream.reader, version)
|
|
505
618
|
: await RequestError.decode(state.stream.reader, version);
|
|
506
|
-
reasonPhrase =
|
|
619
|
+
reasonPhrase = requestReason(err.errorCode, err.reasonPhrase, "subscribe", version);
|
|
507
620
|
}
|
|
508
621
|
}
|
|
509
622
|
catch {
|
|
@@ -513,8 +626,11 @@ export class Subscriber {
|
|
|
513
626
|
throw new Error(`SUBSCRIBE error: ${reasonPhrase}`);
|
|
514
627
|
}
|
|
515
628
|
const ok = await SubscribeOk.decode(state.stream.reader, version);
|
|
629
|
+
if (state.cancelled)
|
|
630
|
+
throw new Error("subscribe cancelled before acceptance");
|
|
631
|
+
request.accept({ priority: fromWire(ok.properties.priority ?? 128) });
|
|
516
632
|
try {
|
|
517
|
-
this.#aliases.set(ok.trackAlias,
|
|
633
|
+
this.#aliases.set(ok.trackAlias, subscription, { broadcast, name: request.name });
|
|
518
634
|
const timescale = ok.properties.timescale;
|
|
519
635
|
if (timescale !== undefined) {
|
|
520
636
|
this.#timescales.set(ok.trackAlias, timescale);
|
|
@@ -548,7 +664,7 @@ export class Subscriber {
|
|
|
548
664
|
await stream.writer.u53(RequestError.id);
|
|
549
665
|
await new RequestError({
|
|
550
666
|
requestId: msg.requestId,
|
|
551
|
-
errorCode:
|
|
667
|
+
errorCode: toRequestCode("uninterested", "publish_namespace", version),
|
|
552
668
|
reasonPhrase: "route loops back",
|
|
553
669
|
}).encode(stream.writer, version);
|
|
554
670
|
stream.close();
|
|
@@ -563,11 +679,14 @@ export class Subscriber {
|
|
|
563
679
|
const legacy = version === Version.DRAFT_14 || version === Version.DRAFT_15;
|
|
564
680
|
if (legacy && (this.#announced.has(path) || this.#legacyRequests.has(path))) {
|
|
565
681
|
console.warn("duplicate PublishNamespace");
|
|
682
|
+
// No draft registers a code for a duplicate, and this refusal is a draft-14/15
|
|
683
|
+
// implementation limit, which is what INTERNAL_ERROR describes.
|
|
684
|
+
const errorCode = toRequestCode("internal", "publish_namespace", version);
|
|
566
685
|
if (version === Version.DRAFT_14) {
|
|
567
686
|
await stream.writer.u53(PublishNamespaceError.id);
|
|
568
687
|
await new PublishNamespaceError({
|
|
569
688
|
requestId: msg.requestId,
|
|
570
|
-
errorCode
|
|
689
|
+
errorCode,
|
|
571
690
|
reasonPhrase: "duplicate namespace",
|
|
572
691
|
}).encode(stream.writer, version);
|
|
573
692
|
}
|
|
@@ -575,7 +694,7 @@ export class Subscriber {
|
|
|
575
694
|
await stream.writer.u53(RequestError.id);
|
|
576
695
|
await new RequestError({
|
|
577
696
|
requestId: msg.requestId,
|
|
578
|
-
errorCode
|
|
697
|
+
errorCode,
|
|
579
698
|
reasonPhrase: "duplicate namespace",
|
|
580
699
|
}).encode(stream.writer, version);
|
|
581
700
|
}
|
|
@@ -608,10 +727,12 @@ export class Subscriber {
|
|
|
608
727
|
// Only now is the advertisement ours to announce, for the reason above: a
|
|
609
728
|
// consumer reacting with a SUBSCRIBE must not interleave with that OK.
|
|
610
729
|
attached = true;
|
|
611
|
-
this.#attachAnnounce(path);
|
|
612
|
-
// An advertisement is updated in place
|
|
613
|
-
//
|
|
614
|
-
//
|
|
730
|
+
this.#attachAnnounce(path, this.#route(msg.cluster));
|
|
731
|
+
// An advertisement is updated in place with REQUEST_UPDATE on the stream that
|
|
732
|
+
// already carries it, so read until the stream ends rather than waiting on the
|
|
733
|
+
// close. Nothing else would deliver a re-parented route. What the peer holds
|
|
734
|
+
// is kept current, since an update carries only what changed.
|
|
735
|
+
let held = msg.cluster;
|
|
615
736
|
const done = version === Version.DRAFT_16 || legacy;
|
|
616
737
|
for (;;) {
|
|
617
738
|
if (await stream.reader.done())
|
|
@@ -621,32 +742,60 @@ export class Subscriber {
|
|
|
621
742
|
await PublishNamespaceDone.decode(stream.reader, version);
|
|
622
743
|
break;
|
|
623
744
|
}
|
|
624
|
-
|
|
745
|
+
// A repeated PUBLISH_NAMESPACE lands here too: a second request on the
|
|
746
|
+
// stream is the base draft's duplicate request ID.
|
|
747
|
+
if (typeId !== PublishNamespaceUpdate.id) {
|
|
625
748
|
throw new ProtocolViolation(`unexpected message on publish_namespace stream: 0x${typeId.toString(16)}`);
|
|
626
749
|
}
|
|
627
|
-
const update = await
|
|
628
|
-
// The
|
|
629
|
-
//
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
750
|
+
const update = await PublishNamespaceUpdate.decode(stream.reader, version);
|
|
751
|
+
// The parameters exist only on a session that negotiated the extension;
|
|
752
|
+
// anywhere else they are the peer's violation.
|
|
753
|
+
if (held === undefined) {
|
|
754
|
+
if (update.update.hops !== undefined || update.update.cost !== undefined) {
|
|
755
|
+
throw new ProtocolViolation("cluster parameters on a session that negotiated none");
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
else {
|
|
759
|
+
// A different original publisher is a different advertisement, which the
|
|
760
|
+
// draft has withdrawn and made again: its content is not continuous with
|
|
761
|
+
// what is held. Refusing the update closes the stream, which is that
|
|
762
|
+
// withdrawal.
|
|
763
|
+
if (update.update.hops !== undefined && update.update.hops[0] !== held.hops[0]) {
|
|
764
|
+
console.warn(`publish_namespace update changes the publisher: broadcast=${path}`);
|
|
765
|
+
await stream.writer.u53(RequestError.id);
|
|
766
|
+
await new RequestError({
|
|
767
|
+
errorCode: toRequestCode("not_supported", "publish_namespace", version),
|
|
768
|
+
reasonPhrase: "a new publisher is a new advertisement",
|
|
769
|
+
}).encode(stream.writer, version);
|
|
770
|
+
stream.close();
|
|
771
|
+
return;
|
|
772
|
+
}
|
|
773
|
+
held = Cluster.apply(held, update.update);
|
|
633
774
|
}
|
|
634
|
-
// A path that now runs through us is unusable, so give it back.
|
|
635
|
-
// reading: this stream is the
|
|
636
|
-
// clean path arrives here or
|
|
637
|
-
|
|
775
|
+
// A path that now runs through us is unusable, so give it back. The update
|
|
776
|
+
// itself is accepted, and reading continues: this stream is the
|
|
777
|
+
// advertisement's only channel, so a later clean path arrives here or
|
|
778
|
+
// nowhere.
|
|
779
|
+
if (this.#reflected(held)) {
|
|
638
780
|
if (attached) {
|
|
639
781
|
attached = false;
|
|
640
782
|
console.debug(`publish_namespace now loops back, detaching: broadcast=${path}`);
|
|
641
783
|
this.#detachAnnounce(path);
|
|
642
784
|
}
|
|
643
|
-
continue;
|
|
644
785
|
}
|
|
645
|
-
|
|
646
|
-
|
|
786
|
+
else if (!attached) {
|
|
787
|
+
// Re-attach: a clean path replaced the reflected one we detached from.
|
|
647
788
|
attached = true;
|
|
648
|
-
this.#attachAnnounce(path);
|
|
789
|
+
this.#attachAnnounce(path, this.#route(held));
|
|
790
|
+
}
|
|
791
|
+
else {
|
|
792
|
+
this.#updateAnnounce(path, this.#route(held));
|
|
649
793
|
}
|
|
794
|
+
// Nothing here can fail to apply, so every update is acknowledged. A leaf
|
|
795
|
+
// routes nothing, so a repricing changes nothing it holds; consumers still
|
|
796
|
+
// hear the new route so a forwarder can reprice.
|
|
797
|
+
await stream.writer.u53(RequestOk.id);
|
|
798
|
+
await new RequestOk({}).encode(stream.writer, version);
|
|
650
799
|
}
|
|
651
800
|
}
|
|
652
801
|
finally {
|
|
@@ -667,17 +816,17 @@ export class Subscriber {
|
|
|
667
816
|
*/
|
|
668
817
|
async runPublish(msg, stream) {
|
|
669
818
|
const version = this.#session.version;
|
|
670
|
-
//
|
|
671
|
-
//
|
|
819
|
+
// We decline the method itself rather than this particular track, which would be
|
|
820
|
+
// UNINTERESTED.
|
|
672
821
|
//
|
|
673
822
|
// The alias the message carries is deliberately not recorded. Nothing will ever bind
|
|
674
823
|
// it, and a rejected request has no lifetime of ours to hang the cleanup on.
|
|
675
|
-
const
|
|
824
|
+
const errorCode = toRequestCode("not_supported", "publish", version);
|
|
676
825
|
if (version === Version.DRAFT_14) {
|
|
677
826
|
await stream.writer.u53(PublishError.id);
|
|
678
827
|
const err = new PublishError({
|
|
679
828
|
requestId: msg.requestId,
|
|
680
|
-
errorCode
|
|
829
|
+
errorCode,
|
|
681
830
|
reasonPhrase: "publish not supported",
|
|
682
831
|
});
|
|
683
832
|
await err.encode(stream.writer, version);
|
|
@@ -686,7 +835,7 @@ export class Subscriber {
|
|
|
686
835
|
await stream.writer.u53(RequestError.id);
|
|
687
836
|
const err = new RequestError({
|
|
688
837
|
requestId: version === Version.DRAFT_15 || version === Version.DRAFT_16 ? msg.requestId : undefined,
|
|
689
|
-
errorCode
|
|
838
|
+
errorCode,
|
|
690
839
|
reasonPhrase: "publish not supported",
|
|
691
840
|
});
|
|
692
841
|
await err.encode(stream.writer, version);
|
|
@@ -702,48 +851,104 @@ export class Subscriber {
|
|
|
702
851
|
if (group.subGroupId !== 0) {
|
|
703
852
|
throw new Error("subgroups are not supported");
|
|
704
853
|
}
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
854
|
+
let subscription;
|
|
855
|
+
try {
|
|
856
|
+
// The control message establishing this alias can arrive after the data stream.
|
|
857
|
+
subscription = await this.#aliases.get(group.trackAlias);
|
|
858
|
+
}
|
|
859
|
+
catch (err) {
|
|
860
|
+
const e = error(err);
|
|
861
|
+
// Ours: we cancelled the subscription and the publisher has not stopped yet.
|
|
862
|
+
// Anything else on this alias is the publisher sending data for a track it never
|
|
863
|
+
// acknowledged, which is worth seeing.
|
|
864
|
+
if (e instanceof RetiredTrackAlias) {
|
|
865
|
+
console.debug(`dropping group for a cancelled subscription: alias=${group.trackAlias}`);
|
|
866
|
+
}
|
|
867
|
+
stream.stop(e);
|
|
718
868
|
return;
|
|
719
869
|
}
|
|
720
|
-
const
|
|
870
|
+
const { track, tail } = subscription;
|
|
871
|
+
// Every data stream counts toward PUBLISH_DONE's Stream Count, even one dropped below.
|
|
872
|
+
const read = tail.open(group.groupId);
|
|
873
|
+
// Created on the first object rather than the header: an END_OF_TRACK at object 0
|
|
874
|
+
// means the group does not exist at all.
|
|
875
|
+
let producer;
|
|
876
|
+
const open = () => {
|
|
877
|
+
if (!producer) {
|
|
878
|
+
producer = new netGroup.Producer(group.groupId);
|
|
879
|
+
track.writeGroup(producer);
|
|
880
|
+
}
|
|
881
|
+
return producer;
|
|
882
|
+
};
|
|
721
883
|
try {
|
|
722
|
-
//
|
|
723
|
-
|
|
724
|
-
|
|
884
|
+
// FIRST_OBJECT clear says this stream starts partway through the group, which the
|
|
885
|
+
// draft lets a publisher do to answer a filter. Nothing above here can use it: the
|
|
886
|
+
// objects that would arrive are not decodable without the missing head, and a group
|
|
887
|
+
// is the unit an application resyncs on. Drop it and pick up at the next group, the
|
|
888
|
+
// same degradation as a publisher that no longer holds the head.
|
|
889
|
+
//
|
|
890
|
+
// This only saves reading a stream we would throw away. The bit is the publisher's
|
|
891
|
+
// claim, so what is enforced is the object ids themselves: `Frame.decode` holds every
|
|
892
|
+
// object to starting at 0 and incrementing by 1, whatever the header said and on the
|
|
893
|
+
// drafts that have no such bit to read.
|
|
894
|
+
if (!group.flags.firstObject) {
|
|
895
|
+
console.debug(`dropping a group with no head: alias=${group.trackAlias} group=${group.groupId}`);
|
|
896
|
+
stream.stop(new Error("a group must start at object 0"));
|
|
897
|
+
return;
|
|
898
|
+
}
|
|
899
|
+
// The alias binds after SUBSCRIBE_OK commits the track property; an omitted
|
|
900
|
+
// header priority inherits it (draft-21 section 10.4).
|
|
901
|
+
if (!group.flags.hasPriority)
|
|
902
|
+
group.publisherPriority = toWire((await track.info()).priority);
|
|
725
903
|
for (;;) {
|
|
726
|
-
|
|
904
|
+
// Only the group's own stream ends it: a track that closes first has already
|
|
905
|
+
// closed (or aborted) this group through its cache.
|
|
906
|
+
const done = await (producer ? race([stream.done(), producer.closed]) : stream.done());
|
|
727
907
|
if (done !== false)
|
|
728
908
|
break;
|
|
729
909
|
const frame = await Frame.decode(stream, group.flags, this.#timescales.get(group.trackAlias), this.#session.version);
|
|
910
|
+
if (frame.endOfTrack) {
|
|
911
|
+
// No object at or past this location exists: after the group's last object
|
|
912
|
+
// the track ends with it, and at object 0 it ends before it.
|
|
913
|
+
const end = producer ? group.groupId + 1 : group.groupId;
|
|
914
|
+
producer?.close();
|
|
915
|
+
try {
|
|
916
|
+
track.finishAt(end);
|
|
917
|
+
}
|
|
918
|
+
catch (err) {
|
|
919
|
+
throw new ProtocolViolation(`invalid END_OF_TRACK: ${reason(error(err))}`);
|
|
920
|
+
}
|
|
921
|
+
return;
|
|
922
|
+
}
|
|
730
923
|
if (frame.payload === undefined)
|
|
731
924
|
break;
|
|
732
|
-
|
|
925
|
+
open().writeFrame({ payload: frame.payload, timestamp: frame.timestamp ?? Timestamp.now() });
|
|
733
926
|
}
|
|
734
|
-
|
|
927
|
+
// A group with no objects still exists.
|
|
928
|
+
open().close();
|
|
735
929
|
}
|
|
736
930
|
catch (err) {
|
|
737
931
|
const e = error(err);
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
932
|
+
if (e instanceof ProtocolViolation) {
|
|
933
|
+
// The publisher broke the track's end, which no later group can repair.
|
|
934
|
+
producer?.close(e);
|
|
935
|
+
track.close(e);
|
|
936
|
+
}
|
|
937
|
+
else {
|
|
938
|
+
// A stream that fails before its first object still names a group, which the
|
|
939
|
+
// reader sees fail rather than silently go missing.
|
|
940
|
+
try {
|
|
941
|
+
open().close(e);
|
|
942
|
+
}
|
|
943
|
+
catch {
|
|
944
|
+
// The track has already closed or ended below this group.
|
|
945
|
+
}
|
|
743
946
|
}
|
|
744
|
-
producer.close(e);
|
|
745
947
|
stream.stop(e);
|
|
746
948
|
}
|
|
949
|
+
finally {
|
|
950
|
+
read();
|
|
951
|
+
}
|
|
747
952
|
}
|
|
748
953
|
}
|
|
749
954
|
/**
|
|
@@ -752,16 +957,15 @@ export class Subscriber {
|
|
|
752
957
|
* group fetch, so `track.Consumer.fetchGroup()` is rejected.
|
|
753
958
|
*/
|
|
754
959
|
class ConsumeBroadcast extends broadcast.Consumer {
|
|
755
|
-
// biome-ignore lint/complexity/noUselessConstructor: widens the protected base constructor to public
|
|
756
960
|
constructor(state) {
|
|
757
961
|
super(state);
|
|
962
|
+
overrideBroadcastWire(this, {
|
|
963
|
+
fetchGroup: () => Promise.reject(new Error("fetch group is not supported for moq-transport")),
|
|
964
|
+
});
|
|
758
965
|
}
|
|
759
966
|
// Preserve the subclass when the consume cache shares this broadcast across callers.
|
|
760
967
|
clone() {
|
|
761
968
|
return new ConsumeBroadcast(this.shareState());
|
|
762
969
|
}
|
|
763
|
-
fetchGroup() {
|
|
764
|
-
return Promise.reject(new Error("fetch group is not supported for moq-transport"));
|
|
765
|
-
}
|
|
766
970
|
}
|
|
767
971
|
//# sourceMappingURL=subscriber.js.map
|