@moq/net 0.4.0 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/announced.d.ts +14 -0
- package/announced.d.ts.map +1 -1
- package/announced.js.map +1 -1
- package/broadcast.d.ts +27 -10
- package/broadcast.d.ts.map +1 -1
- package/broadcast.js +47 -21
- package/broadcast.js.map +1 -1
- package/connection/accept.js +14 -4
- package/connection/accept.js.map +1 -1
- package/connection/connect.d.ts.map +1 -1
- package/connection/connect.js +14 -4
- package/connection/connect.js.map +1 -1
- package/connection/established.d.ts +2 -1
- package/connection/established.d.ts.map +1 -1
- package/connection/established.js.map +1 -1
- package/connection/forward.js +61 -47
- package/connection/forward.js.map +1 -1
- package/connection/goaway.d.ts +34 -0
- package/connection/goaway.d.ts.map +1 -0
- package/connection/goaway.js +176 -0
- package/connection/goaway.js.map +1 -0
- package/connection/handshake.d.ts +5 -2
- package/connection/handshake.d.ts.map +1 -1
- package/connection/handshake.js +45 -10
- package/connection/handshake.js.map +1 -1
- package/connection/pool.d.ts +24 -11
- package/connection/pool.d.ts.map +1 -1
- package/connection/pool.js +50 -18
- package/connection/pool.js.map +1 -1
- package/connection/reload.d.ts.map +1 -1
- package/connection/reload.js +189 -35
- package/connection/reload.js.map +1 -1
- package/error.d.ts +11 -0
- package/error.d.ts.map +1 -1
- package/error.js +35 -0
- package/error.js.map +1 -1
- package/errors.d.ts +1 -1
- package/errors.d.ts.map +1 -1
- package/errors.js +1 -1
- package/errors.js.map +1 -1
- package/group.d.ts.map +1 -1
- package/group.js +5 -4
- package/group.js.map +1 -1
- package/hop.d.ts +8 -0
- package/hop.d.ts.map +1 -1
- package/hop.js +16 -0
- package/hop.js.map +1 -1
- package/ietf/adapter.d.ts +9 -1
- package/ietf/adapter.d.ts.map +1 -1
- package/ietf/adapter.js +46 -17
- package/ietf/adapter.js.map +1 -1
- package/ietf/cluster.d.ts +3 -3
- package/ietf/cluster.js +3 -3
- package/ietf/cluster.js.map +1 -1
- package/ietf/connection.d.ts +1 -1
- package/ietf/connection.d.ts.map +1 -1
- package/ietf/connection.js +118 -41
- package/ietf/connection.js.map +1 -1
- package/ietf/error.js +27 -2
- package/ietf/error.js.map +1 -1
- package/ietf/fetch.d.ts +7 -20
- package/ietf/fetch.d.ts.map +1 -1
- package/ietf/fetch.js +52 -22
- package/ietf/fetch.js.map +1 -1
- package/ietf/filter.d.ts +2 -0
- package/ietf/filter.d.ts.map +1 -1
- package/ietf/filter.js +4 -0
- package/ietf/filter.js.map +1 -1
- package/ietf/goaway.d.ts +9 -1
- package/ietf/goaway.d.ts.map +1 -1
- package/ietf/goaway.js +20 -0
- 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 +2 -1
- package/ietf/index.d.ts.map +1 -1
- package/ietf/index.js +2 -1
- package/ietf/index.js.map +1 -1
- package/ietf/object.d.ts +15 -8
- package/ietf/object.d.ts.map +1 -1
- package/ietf/object.js +54 -36
- package/ietf/object.js.map +1 -1
- package/ietf/parameters.d.ts +12 -0
- package/ietf/parameters.d.ts.map +1 -1
- package/ietf/parameters.js +82 -12
- 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 +2 -2
- package/ietf/publish_namespace.js +2 -2
- package/ietf/publish_namespace.js.map +1 -1
- package/ietf/publisher.js +383 -214
- package/ietf/publisher.js.map +1 -1
- package/ietf/subscribe.d.ts +7 -1
- package/ietf/subscribe.d.ts.map +1 -1
- package/ietf/subscribe.js +25 -18
- 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 +20 -11
- package/ietf/subscribe_namespace.js.map +1 -1
- package/ietf/subscriber.js +226 -88
- package/ietf/subscriber.js.map +1 -1
- package/ietf/token.d.ts +2 -0
- package/ietf/token.d.ts.map +1 -0
- package/ietf/token.js +99 -0
- package/ietf/token.js.map +1 -0
- package/ietf/track.d.ts +4 -0
- package/ietf/track.d.ts.map +1 -1
- package/ietf/track.js +6 -20
- package/ietf/track.js.map +1 -1
- package/index.d.ts +1 -1
- package/index.d.ts.map +1 -1
- package/index.js.map +1 -1
- package/internal.d.ts +19 -3
- package/internal.d.ts.map +1 -1
- package/internal.js +43 -0
- package/internal.js.map +1 -1
- package/lite/announce.d.ts +46 -2
- package/lite/announce.d.ts.map +1 -1
- package/lite/announce.js +133 -15
- package/lite/announce.js.map +1 -1
- package/lite/connection.d.ts +1 -1
- package/lite/connection.d.ts.map +1 -1
- package/lite/connection.js +30 -9
- package/lite/connection.js.map +1 -1
- package/lite/datagram.d.ts +3 -2
- package/lite/datagram.d.ts.map +1 -1
- package/lite/datagram.js +7 -8
- package/lite/datagram.js.map +1 -1
- package/lite/group.d.ts +12 -8
- package/lite/group.d.ts.map +1 -1
- package/lite/group.js +37 -16
- package/lite/group.js.map +1 -1
- package/lite/message.d.ts +2 -2
- package/lite/message.d.ts.map +1 -1
- package/lite/message.js +14 -5
- package/lite/message.js.map +1 -1
- package/lite/publisher.js +174 -133
- package/lite/publisher.js.map +1 -1
- package/lite/setup.d.ts +1 -1
- package/lite/setup.d.ts.map +1 -1
- package/lite/setup.js +22 -19
- package/lite/setup.js.map +1 -1
- package/lite/subscribe.d.ts +10 -4
- package/lite/subscribe.d.ts.map +1 -1
- package/lite/subscribe.js +20 -10
- package/lite/subscribe.js.map +1 -1
- package/lite/subscriber.d.ts +10 -2
- package/lite/subscriber.d.ts.map +1 -1
- package/lite/subscriber.js +333 -188
- package/lite/subscriber.js.map +1 -1
- package/lite/version.d.ts +11 -0
- package/lite/version.d.ts.map +1 -1
- package/lite/version.js +56 -0
- package/lite/version.js.map +1 -1
- package/origin.d.ts +46 -19
- package/origin.d.ts.map +1 -1
- package/origin.js +529 -189
- package/origin.js.map +1 -1
- package/package.json +8 -4
- package/stream.d.ts +79 -14
- package/stream.d.ts.map +1 -1
- package/stream.js +375 -133
- package/stream.js.map +1 -1
- package/tail.d.ts +18 -0
- package/tail.d.ts.map +1 -0
- package/tail.js +167 -0
- package/tail.js.map +1 -0
- package/track.d.ts +46 -7
- package/track.d.ts.map +1 -1
- package/track.js +263 -101
- package/track.js.map +1 -1
- package/util/abort.d.ts +2 -0
- package/util/abort.d.ts.map +1 -0
- package/util/abort.js +20 -0
- package/util/abort.js.map +1 -0
- package/util/u64.d.ts +39 -0
- package/util/u64.d.ts.map +1 -0
- package/util/u64.js +83 -0
- package/util/u64.js.map +1 -0
- package/util/varint.d.ts +29 -0
- package/util/varint.d.ts.map +1 -0
- package/util/varint.js +198 -0
- package/util/varint.js.map +1 -0
- package/varint.d.ts +10 -6
- package/varint.d.ts.map +1 -1
- package/varint.js +40 -237
- package/varint.js.map +1 -1
- package/wire.d.ts +19 -3
- package/wire.d.ts.map +1 -1
- package/wire.js.map +1 -1
- package/ietf/control.d.ts +0 -135
- package/ietf/control.d.ts.map +0 -1
- package/ietf/control.js +0 -242
- package/ietf/control.js.map +0 -1
package/ietf/subscriber.js
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
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 { controlTimeout, 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";
|
|
7
|
-
import { Cost, routesEqual, UNKNOWN_HOP } from "../hop.js";
|
|
8
|
-
import { scopeCaptures, scopeHead, scopeOverlaps } from "../internal.js";
|
|
8
|
+
import { Cost, randomHop, routesEqual, stampHops, UNKNOWN_HOP } from "../hop.js";
|
|
9
|
+
import { hiddenBelow, hooks, scopeCaptures, scopeHead, scopeOverlaps } from "../internal.js";
|
|
9
10
|
import * as Path from "../path.js";
|
|
11
|
+
import { Tail } from "../tail.js";
|
|
10
12
|
import { Timestamp } from "../time.js";
|
|
11
13
|
import { TimeoutError, withTimeout } from "../util/timeout.js";
|
|
12
14
|
import { overrideBroadcastWire, wireOf } from "../wire.js";
|
|
@@ -14,8 +16,8 @@ import { DuplicateTrackAlias, RetiredTrackAlias, TrackAliases } from "./aliases.
|
|
|
14
16
|
import * as Cluster from "./cluster.js";
|
|
15
17
|
import { requestReason, toRequestCode } from "./error.js";
|
|
16
18
|
import { Frame } from "./object.js";
|
|
17
|
-
import { toWire } from "./priority.js";
|
|
18
|
-
import { PublishError } from "./publish.js";
|
|
19
|
+
import { fromWire, toWire } from "./priority.js";
|
|
20
|
+
import { PublishDone, PublishError, publishDoneClean } from "./publish.js";
|
|
19
21
|
import { PublishNamespaceDone, PublishNamespaceError, PublishNamespaceOk, PublishNamespaceUpdate, } from "./publish_namespace.js";
|
|
20
22
|
import { RequestError, RequestOk } from "./request.js";
|
|
21
23
|
import { joinFilter, Subscribe, SubscribeError, SubscribeOk, Unsubscribe } from "./subscribe.js";
|
|
@@ -25,6 +27,10 @@ import { Version } from "./version.js";
|
|
|
25
27
|
// concurrent QUIC streams (Chrome ~100); past the cap openBi() silently
|
|
26
28
|
// blocks. The timeout turns that into a clear error.
|
|
27
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
|
+
}
|
|
28
34
|
/**
|
|
29
35
|
* Handles subscribing to broadcasts using moq-transport protocol.
|
|
30
36
|
* Uses the stream-per-request pattern (real bidi streams for v17, virtual for v14-v16).
|
|
@@ -33,6 +39,10 @@ const SUBSCRIBE_OK_TIMEOUT_MS = 10_000;
|
|
|
33
39
|
*/
|
|
34
40
|
export class Subscriber {
|
|
35
41
|
#session;
|
|
42
|
+
#localClose = false;
|
|
43
|
+
// The transport, so a request cut off by the session's close ends with the session's
|
|
44
|
+
// error. Optional for tests that drive a bare session.
|
|
45
|
+
#quic;
|
|
36
46
|
// The Hop IDs this session declared; see {@link Cluster}. What the peer declared is what
|
|
37
47
|
// says whether an advertisement carries a hop path, and ours is what a path looping back
|
|
38
48
|
// to us contains.
|
|
@@ -45,6 +55,9 @@ export class Subscriber {
|
|
|
45
55
|
#timescales = new Map();
|
|
46
56
|
// Dedup consumed broadcasts per path: repeat consume() calls share one subscription.
|
|
47
57
|
#consumes = new BroadcastCache();
|
|
58
|
+
// A random Hop ID of this connection's own, written as the first hop of any path that
|
|
59
|
+
// names no publisher, so a publisher that reconnects reads as a new one.
|
|
60
|
+
#stamp = randomHop();
|
|
48
61
|
// Paths with a legacy PUBLISH_NAMESPACE request in flight, reserved synchronously.
|
|
49
62
|
// The count below is only taken once the OK is written, and two requests that both
|
|
50
63
|
// got past the duplicate check before either attached would both take one.
|
|
@@ -59,14 +72,22 @@ export class Subscriber {
|
|
|
59
72
|
#announced = new Map();
|
|
60
73
|
// Any consumers that want each new announcement, keyed by their local filter.
|
|
61
74
|
#announcedConsumers = new Map();
|
|
75
|
+
// Whether the peer understands the HIDDEN parameter (MoQ Hidden).
|
|
76
|
+
#hidden;
|
|
77
|
+
/** Marks this subscriber's deliberate local session close. @internal */
|
|
78
|
+
close() {
|
|
79
|
+
this.#localClose = true;
|
|
80
|
+
}
|
|
62
81
|
/**
|
|
63
82
|
* Creates a new Subscriber instance.
|
|
64
83
|
*
|
|
65
84
|
* @internal
|
|
66
85
|
*/
|
|
67
|
-
constructor({ session, cluster, }) {
|
|
86
|
+
constructor({ session, quic, cluster, hidden = false, }) {
|
|
68
87
|
this.#session = session;
|
|
88
|
+
this.#quic = quic;
|
|
69
89
|
this.#cluster = cluster;
|
|
90
|
+
this.#hidden = hidden;
|
|
70
91
|
}
|
|
71
92
|
/**
|
|
72
93
|
* Whether an advertisement is ours coming back: its hop path already ran through us, so
|
|
@@ -79,11 +100,17 @@ export class Subscriber {
|
|
|
79
100
|
#reflected(advert) {
|
|
80
101
|
return advert !== undefined && this.#cluster !== undefined && Cluster.loops(advert, this.#cluster.self);
|
|
81
102
|
}
|
|
82
|
-
/**
|
|
103
|
+
/**
|
|
104
|
+
* The route an advertisement carries; one without a path is free. A path that names no
|
|
105
|
+
* publisher, or none at all, gets this connection's stamp in front of a 0.
|
|
106
|
+
*/
|
|
83
107
|
#route(advert) {
|
|
84
108
|
if (advert === undefined)
|
|
85
|
-
return { hops: [UNKNOWN_HOP], cost: Cost.zero };
|
|
86
|
-
|
|
109
|
+
return { hops: [this.#stamp, UNKNOWN_HOP], cost: Cost.zero };
|
|
110
|
+
// A full chain, or a stamp colliding with an entry (a 1-in-2^53 draw), keeps the
|
|
111
|
+
// path as sent.
|
|
112
|
+
const hops = stampHops(advert.hops, this.#stamp) ?? [...advert.hops];
|
|
113
|
+
return { hops, cost: { warm: advert.cost, cold: advert.cost } };
|
|
87
114
|
}
|
|
88
115
|
/**
|
|
89
116
|
* Gets an announced reader matching `scope`. Paths are relative to the session,
|
|
@@ -92,13 +119,19 @@ export class Subscriber {
|
|
|
92
119
|
* The peer is asked with SUBSCRIBE_NAMESPACE regardless of what it declared, and an
|
|
93
120
|
* unsolicited PUBLISH_NAMESPACE lands here too, so a peer that only tells and one
|
|
94
121
|
* that only answers are both discovered.
|
|
122
|
+
*
|
|
123
|
+
* Hidden routes (a `.`-prefixed segment below the scope's head) are left out unless
|
|
124
|
+
* `options.hidden` opts in. The opt-in rides the SUBSCRIBE_NAMESPACE when the peer
|
|
125
|
+
* understands it (MoQ Hidden); the rule is also applied here, since an unsolicited
|
|
126
|
+
* PUBLISH_NAMESPACE or a peer that never heard of it hides nothing.
|
|
95
127
|
*/
|
|
96
|
-
announced(scope = Path.Pattern.all()) {
|
|
128
|
+
announced(scope = Path.Pattern.all(), options) {
|
|
97
129
|
// The wire speaks announce interest by prefix.
|
|
98
130
|
const prefix = scopeHead(scope);
|
|
131
|
+
const filter = { scope, prefix, hidden: options?.hidden ?? false };
|
|
99
132
|
const announced = new announce.Producer();
|
|
100
133
|
for (const [active, info] of this.#announced) {
|
|
101
|
-
if (!
|
|
134
|
+
if (!sees(filter, active))
|
|
102
135
|
continue;
|
|
103
136
|
announced.append({
|
|
104
137
|
prefix: active,
|
|
@@ -107,8 +140,8 @@ export class Subscriber {
|
|
|
107
140
|
route: info.route,
|
|
108
141
|
});
|
|
109
142
|
}
|
|
110
|
-
this.#announcedConsumers.set(announced,
|
|
111
|
-
void this.#runAnnounced(announced, prefix).finally(() => {
|
|
143
|
+
this.#announcedConsumers.set(announced, filter);
|
|
144
|
+
void this.#runAnnounced(announced, prefix, filter.hidden && this.#hidden).finally(() => {
|
|
112
145
|
this.#announcedConsumers.delete(announced);
|
|
113
146
|
announced.close();
|
|
114
147
|
});
|
|
@@ -126,9 +159,10 @@ export class Subscriber {
|
|
|
126
159
|
}
|
|
127
160
|
this.#announced.set(path, { count: 1, route });
|
|
128
161
|
console.debug(`announced: broadcast=${path} active=true`);
|
|
129
|
-
for (const [consumer,
|
|
130
|
-
if (!
|
|
162
|
+
for (const [consumer, filter] of this.#announcedConsumers) {
|
|
163
|
+
if (!sees(filter, path))
|
|
131
164
|
continue;
|
|
165
|
+
const scope = filter.scope;
|
|
132
166
|
consumer.append({ prefix: path, captures: scopeCaptures(scope, path), kind: "announced", route });
|
|
133
167
|
}
|
|
134
168
|
}
|
|
@@ -136,16 +170,22 @@ export class Subscriber {
|
|
|
136
170
|
* Replace the stored route for a path that is already announced. A no-op when the
|
|
137
171
|
* hops and cost did not change; otherwise consumers hear `updated` so a forwarder
|
|
138
172
|
* can reprice without retracting.
|
|
173
|
+
*
|
|
174
|
+
* A new first hop is a new publisher: holders keep their broadcast to drain, but the
|
|
175
|
+
* next consume starts fresh rather than reusing the old publisher's cached track info.
|
|
139
176
|
*/
|
|
140
177
|
#updateAnnounce(path, route) {
|
|
141
178
|
const existing = this.#announced.get(path);
|
|
142
179
|
if (existing === undefined || routesEqual(existing.route, route))
|
|
143
180
|
return;
|
|
181
|
+
if (existing.route.hops[0] !== route.hops[0])
|
|
182
|
+
this.#consumes.evict(path);
|
|
144
183
|
existing.route = route;
|
|
145
184
|
console.debug(`announced: broadcast=${path} rerouted`);
|
|
146
|
-
for (const [consumer,
|
|
147
|
-
if (!
|
|
185
|
+
for (const [consumer, filter] of this.#announcedConsumers) {
|
|
186
|
+
if (!sees(filter, path))
|
|
148
187
|
continue;
|
|
188
|
+
const scope = filter.scope;
|
|
149
189
|
consumer.append({ prefix: path, captures: scopeCaptures(scope, path), kind: "updated", route });
|
|
150
190
|
}
|
|
151
191
|
}
|
|
@@ -165,9 +205,10 @@ export class Subscriber {
|
|
|
165
205
|
// would otherwise hand the dead generation to whoever consumes the path next.
|
|
166
206
|
this.#consumes.evict(path);
|
|
167
207
|
console.debug(`announced: broadcast=${path} active=false`);
|
|
168
|
-
for (const [consumer,
|
|
169
|
-
if (!
|
|
208
|
+
for (const [consumer, filter] of this.#announcedConsumers) {
|
|
209
|
+
if (!sees(filter, path))
|
|
170
210
|
continue;
|
|
211
|
+
const scope = filter.scope;
|
|
171
212
|
try {
|
|
172
213
|
consumer.append({
|
|
173
214
|
prefix: path,
|
|
@@ -181,7 +222,7 @@ export class Subscriber {
|
|
|
181
222
|
}
|
|
182
223
|
}
|
|
183
224
|
}
|
|
184
|
-
async #runAnnounced(announced, prefix) {
|
|
225
|
+
async #runAnnounced(announced, prefix, hidden) {
|
|
185
226
|
const version = this.#session.version;
|
|
186
227
|
// Suffixes live on this stream, so a repeat is recognized as an update to the
|
|
187
228
|
// advertisement rather than a second one, which would leak the count.
|
|
@@ -210,11 +251,11 @@ export class Subscriber {
|
|
|
210
251
|
version === Version.DRAFT_16 ||
|
|
211
252
|
version === Version.DRAFT_17) {
|
|
212
253
|
await stream.writer.u53(SubscribeNamespaceLegacy.id);
|
|
213
|
-
await new SubscribeNamespaceLegacy({ namespace: prefix, requestId }).encode(stream.writer, version);
|
|
254
|
+
await new SubscribeNamespaceLegacy({ namespace: prefix, requestId, hidden }).encode(stream.writer, version);
|
|
214
255
|
}
|
|
215
256
|
else {
|
|
216
257
|
await stream.writer.u53(SubscribeNamespace.id);
|
|
217
|
-
await new SubscribeNamespace({ namespace: prefix, requestId }).encode(stream.writer, version);
|
|
258
|
+
await new SubscribeNamespace({ namespace: prefix, requestId, hidden }).encode(stream.writer, version);
|
|
218
259
|
}
|
|
219
260
|
console.debug(`subscribe_namespace written: requestId=${requestId}`);
|
|
220
261
|
// Read response
|
|
@@ -290,7 +331,7 @@ export class Subscriber {
|
|
|
290
331
|
this.#session.close();
|
|
291
332
|
});
|
|
292
333
|
// Wait for either the read loop or the announced to close
|
|
293
|
-
await
|
|
334
|
+
await race([readLoop, announced.closed]);
|
|
294
335
|
// For v14/v15: send UnsubscribeNamespace before closing
|
|
295
336
|
if (version === Version.DRAFT_14 || version === Version.DRAFT_15) {
|
|
296
337
|
try {
|
|
@@ -360,28 +401,43 @@ export class Subscriber {
|
|
|
360
401
|
})();
|
|
361
402
|
return consumer;
|
|
362
403
|
}
|
|
404
|
+
// The adapter is gone. If the transport has already closed, that close is the
|
|
405
|
+
// error. A still-open transport, such as a GOAWAY drain, has no peer code yet,
|
|
406
|
+
// so this does not wait for it.
|
|
407
|
+
async #closedSession() {
|
|
408
|
+
const quic = this.#quic;
|
|
409
|
+
if (!quic)
|
|
410
|
+
return new Error("session closed");
|
|
411
|
+
return Promise.race([
|
|
412
|
+
closeError(quic),
|
|
413
|
+
new Promise((resolve) => queueMicrotask(() => resolve(new Error("session closed")))),
|
|
414
|
+
]);
|
|
415
|
+
}
|
|
363
416
|
async #runSubscribe(broadcast, request) {
|
|
364
417
|
const requestId = await this.#session.nextRequestId();
|
|
365
418
|
if (requestId === undefined) {
|
|
366
|
-
request.reject(
|
|
419
|
+
request.reject(await this.#closedSession());
|
|
367
420
|
return;
|
|
368
421
|
}
|
|
369
422
|
console.debug(`subscribe start: id=${requestId} broadcast=${broadcast} track=${request.name}`);
|
|
370
|
-
//
|
|
371
|
-
//
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
// gives us the write side that incoming object streams are routed into.
|
|
375
|
-
const producer = request.accept({});
|
|
423
|
+
// Keep the request pending until SUBSCRIBE_OK supplies immutable track metadata.
|
|
424
|
+
// Group streams already wait on the alias, so early data stays behind this response.
|
|
425
|
+
const producer = hooks.pendingTrackProducer(request);
|
|
426
|
+
const subscription = { track: producer, tail: new Tail() };
|
|
376
427
|
// Open the stream and wait for SUBSCRIBE_OK under a timeout. State
|
|
377
428
|
// flows back via `state` so the timeout path can clean up the stream
|
|
378
429
|
// and any registration if setup eventually finishes.
|
|
379
430
|
const state = {};
|
|
380
|
-
const setup = this.#openSubscribe(state, broadcast, request,
|
|
431
|
+
const setup = this.#openSubscribe(state, broadcast, request, subscription, requestId);
|
|
381
432
|
// The publisher can be serving before it answers, so waiting only on the response
|
|
382
433
|
// would miss the local side going away and leave it serving a track nobody reads.
|
|
383
434
|
// Demand returning before we commit is not abandonment, matching the serving loop.
|
|
384
435
|
const waitAbandoned = async () => {
|
|
436
|
+
// An info-only lookup attaches no subscriber yet still waits on SUBSCRIBE_OK for
|
|
437
|
+
// the track info, so only demand that arrived and then left is abandonment.
|
|
438
|
+
while (!producer.used.peek() && producer.closed.peek() === undefined) {
|
|
439
|
+
await Signal.race(producer.used, producer.closed);
|
|
440
|
+
}
|
|
385
441
|
for (;;) {
|
|
386
442
|
await producer.unused();
|
|
387
443
|
if (producer.closed.peek() !== undefined || !producer.used.peek())
|
|
@@ -391,7 +447,7 @@ export class Subscriber {
|
|
|
391
447
|
let stream;
|
|
392
448
|
let trackAlias;
|
|
393
449
|
try {
|
|
394
|
-
const result = await
|
|
450
|
+
const result = await race([
|
|
395
451
|
withTimeout(setup, SUBSCRIBE_OK_TIMEOUT_MS, `subscribe timed out after ${SUBSCRIBE_OK_TIMEOUT_MS}ms waiting for SUBSCRIBE_OK (browser stream limit reached?)`),
|
|
396
452
|
waitAbandoned(),
|
|
397
453
|
]);
|
|
@@ -403,8 +459,8 @@ export class Subscriber {
|
|
|
403
459
|
}
|
|
404
460
|
catch (err) {
|
|
405
461
|
// A control request that timed out is not late content, so it carries its own code.
|
|
406
|
-
const e = err instanceof TimeoutError ? controlTimeout(err) :
|
|
407
|
-
|
|
462
|
+
const e = err instanceof TimeoutError ? controlTimeout(err) : await sessionCause(this.#quic, err);
|
|
463
|
+
request.reject(e);
|
|
408
464
|
console.warn(`subscribe error: id=${requestId} broadcast=${broadcast} track=${request.name} error=${reason(e)}`);
|
|
409
465
|
// Runs now for whatever is already open, and again if `setup` settles late.
|
|
410
466
|
// Retirement is repeated because a late SUBSCRIBE_OK can register an alias after
|
|
@@ -412,7 +468,7 @@ export class Subscriber {
|
|
|
412
468
|
let torn = false;
|
|
413
469
|
const cleanup = async (afterSetup) => {
|
|
414
470
|
state.cancelled = true;
|
|
415
|
-
if (state.registeredAlias !== undefined && this.#aliases.retire(state.registeredAlias,
|
|
471
|
+
if (state.registeredAlias !== undefined && this.#aliases.retire(state.registeredAlias, subscription)) {
|
|
416
472
|
this.#timescales.delete(state.registeredAlias);
|
|
417
473
|
}
|
|
418
474
|
if (!state.stream || torn)
|
|
@@ -447,10 +503,10 @@ export class Subscriber {
|
|
|
447
503
|
const publisherEnded = Symbol("publisher");
|
|
448
504
|
const localEnded = Symbol("local");
|
|
449
505
|
const idle = Symbol("idle");
|
|
450
|
-
// Terminal conditions settle at most once (
|
|
451
|
-
//
|
|
452
|
-
const done =
|
|
453
|
-
stream.
|
|
506
|
+
// Terminal conditions settle at most once (PublishDone, track close = local
|
|
507
|
+
// unsubscribe); race them once so the demand loop doesn't re-subscribe each pass.
|
|
508
|
+
const done = race([
|
|
509
|
+
this.#runPublishDone(stream, subscription).then(() => publisherEnded),
|
|
454
510
|
producer.closed.then(() => localEnded),
|
|
455
511
|
]);
|
|
456
512
|
// Serve until a terminal condition fires or the last local subscriber leaves. The unused
|
|
@@ -458,7 +514,7 @@ export class Subscriber {
|
|
|
458
514
|
// down resumes on the same stream.
|
|
459
515
|
let terminal = localEnded;
|
|
460
516
|
for (;;) {
|
|
461
|
-
const reason = await
|
|
517
|
+
const reason = await race([done, producer.unused().then(() => idle)]);
|
|
462
518
|
if (reason === idle && producer.closed.peek() === undefined && producer.used.peek())
|
|
463
519
|
continue;
|
|
464
520
|
terminal = reason;
|
|
@@ -477,18 +533,44 @@ export class Subscriber {
|
|
|
477
533
|
console.debug(`subscribe close: id=${requestId} broadcast=${broadcast} track=${request.name}`);
|
|
478
534
|
}
|
|
479
535
|
catch (err) {
|
|
480
|
-
const e =
|
|
481
|
-
producer.close(e);
|
|
536
|
+
const e = await sessionCause(this.#quic, err);
|
|
537
|
+
producer.close(this.#localClose ? undefined : e);
|
|
482
538
|
stream.abort(e);
|
|
483
539
|
console.warn(`subscribe error: id=${requestId} broadcast=${broadcast} track=${request.name} error=${reason(e)}`);
|
|
484
540
|
}
|
|
485
541
|
finally {
|
|
486
542
|
// Only the owner tears down the alias metadata: a later subscription may have
|
|
487
543
|
// reclaimed the alias and installed its own timescale.
|
|
488
|
-
if (this.#aliases.retire(trackAlias,
|
|
544
|
+
if (this.#aliases.retire(trackAlias, subscription))
|
|
489
545
|
this.#timescales.delete(trackAlias);
|
|
490
546
|
}
|
|
491
547
|
}
|
|
548
|
+
/**
|
|
549
|
+
* Read the PUBLISH_DONE that ends a subscription, then wait for the data streams it counts.
|
|
550
|
+
*
|
|
551
|
+
* An error status aborts the track with it. A clean one leaves streams in flight, since
|
|
552
|
+
* QUIC does not order them, so wait until the Stream Count many have been read, or a
|
|
553
|
+
* bounded grace for the ones that never arrive (the draft says to use a timeout). The count
|
|
554
|
+
* is only a hint: a peer may send 0 regardless, so 0 waits out the grace. A request
|
|
555
|
+
* stream that FINs without PUBLISH_DONE is a protocol violation.
|
|
556
|
+
*/
|
|
557
|
+
async #runPublishDone(stream, subscription) {
|
|
558
|
+
const version = this.#session.version;
|
|
559
|
+
if (await stream.reader.done())
|
|
560
|
+
throw new ProtocolViolation("subscribe stream ended without PUBLISH_DONE");
|
|
561
|
+
const typeId = await stream.reader.u53();
|
|
562
|
+
if (typeId !== PublishDone.id) {
|
|
563
|
+
throw new ProtocolViolation(`unexpected message on a subscription: 0x${typeId.toString(16)}`);
|
|
564
|
+
}
|
|
565
|
+
const done = await PublishDone.decode(stream.reader, version);
|
|
566
|
+
if (!publishDoneClean(done.statusCode, version)) {
|
|
567
|
+
throw new Error(`publish done: status=0x${done.statusCode.toString(16)} reason=${done.reasonPhrase}`);
|
|
568
|
+
}
|
|
569
|
+
const count = done.streamCount;
|
|
570
|
+
const { tail, track } = subscription;
|
|
571
|
+
const complete = () => count > 0n && BigInt(tail.streams) >= count;
|
|
572
|
+
await tail.settle(complete, track.closed);
|
|
573
|
+
}
|
|
492
574
|
/**
|
|
493
575
|
* Tell the publisher to stop serving a subscription we are walking away from.
|
|
494
576
|
*
|
|
@@ -514,7 +596,7 @@ export class Subscriber {
|
|
|
514
596
|
// `state` is populated as soon as the stream opens and again when the
|
|
515
597
|
// trackAlias is registered, so the caller can clean both up on timeout
|
|
516
598
|
// even before this promise settles.
|
|
517
|
-
async #openSubscribe(state, broadcast, request,
|
|
599
|
+
async #openSubscribe(state, broadcast, request, subscription, requestId) {
|
|
518
600
|
const version = this.#session.version;
|
|
519
601
|
state.stream = await this.#session.openBi();
|
|
520
602
|
// The timeout can fire while the open is still in flight, in which case cleanup ran
|
|
@@ -562,8 +644,11 @@ export class Subscriber {
|
|
|
562
644
|
throw new Error(`SUBSCRIBE error: ${reasonPhrase}`);
|
|
563
645
|
}
|
|
564
646
|
const ok = await SubscribeOk.decode(state.stream.reader, version);
|
|
647
|
+
if (state.cancelled)
|
|
648
|
+
throw new Error("subscribe cancelled before acceptance");
|
|
649
|
+
request.accept({ priority: fromWire(ok.properties.priority ?? 128) });
|
|
565
650
|
try {
|
|
566
|
-
this.#aliases.set(ok.trackAlias,
|
|
651
|
+
this.#aliases.set(ok.trackAlias, subscription, { broadcast, name: request.name });
|
|
567
652
|
const timescale = ok.properties.timescale;
|
|
568
653
|
if (timescale !== undefined) {
|
|
569
654
|
this.#timescales.set(ok.trackAlias, timescale);
|
|
@@ -689,20 +774,7 @@ export class Subscriber {
|
|
|
689
774
|
}
|
|
690
775
|
}
|
|
691
776
|
else {
|
|
692
|
-
// A different original publisher
|
|
693
|
-
// draft has withdrawn and made again: its content is not continuous with
|
|
694
|
-
// what is held. Refusing the update closes the stream, which is that
|
|
695
|
-
// withdrawal.
|
|
696
|
-
if (update.update.hops !== undefined && update.update.hops[0] !== held.hops[0]) {
|
|
697
|
-
console.warn(`publish_namespace update changes the publisher: broadcast=${path}`);
|
|
698
|
-
await stream.writer.u53(RequestError.id);
|
|
699
|
-
await new RequestError({
|
|
700
|
-
errorCode: toRequestCode("not_supported", "publish_namespace", version),
|
|
701
|
-
reasonPhrase: "a new publisher is a new advertisement",
|
|
702
|
-
}).encode(stream.writer, version);
|
|
703
|
-
stream.close();
|
|
704
|
-
return;
|
|
705
|
-
}
|
|
777
|
+
// A different original publisher applies in place too, as it does inline.
|
|
706
778
|
held = Cluster.apply(held, update.update);
|
|
707
779
|
}
|
|
708
780
|
// A path that now runs through us is unusable, so give it back. The update
|
|
@@ -784,48 +856,114 @@ export class Subscriber {
|
|
|
784
856
|
if (group.subGroupId !== 0) {
|
|
785
857
|
throw new Error("subgroups are not supported");
|
|
786
858
|
}
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
859
|
+
let subscription;
|
|
860
|
+
try {
|
|
861
|
+
// The control message establishing this alias can arrive after the data stream.
|
|
862
|
+
subscription = await this.#aliases.get(group.trackAlias);
|
|
863
|
+
}
|
|
864
|
+
catch (err) {
|
|
865
|
+
const e = await sessionCause(this.#quic, err);
|
|
866
|
+
// Ours: we cancelled the subscription and the publisher has not stopped yet.
|
|
867
|
+
// Anything else on this alias is the publisher sending data for a track it never
|
|
868
|
+
// acknowledged, which is worth seeing.
|
|
869
|
+
if (e instanceof RetiredTrackAlias) {
|
|
870
|
+
console.debug(`dropping group for a cancelled subscription: alias=${group.trackAlias}`);
|
|
871
|
+
}
|
|
872
|
+
stream.stop(e);
|
|
800
873
|
return;
|
|
801
874
|
}
|
|
802
|
-
const
|
|
875
|
+
const { track, tail } = subscription;
|
|
876
|
+
// Every data stream counts toward PUBLISH_DONE's Stream Count, even one dropped below.
|
|
877
|
+
const read = tail.open(group.groupId);
|
|
878
|
+
// Created on the first object rather than the header: an END_OF_TRACK at object 0
|
|
879
|
+
// means the group does not exist at all.
|
|
880
|
+
let producer;
|
|
881
|
+
const open = () => {
|
|
882
|
+
if (!producer) {
|
|
883
|
+
// The publisher contradicted its own end, which no later group can repair.
|
|
884
|
+
if (subscription.end !== undefined && group.groupId >= subscription.end) {
|
|
885
|
+
throw new ProtocolViolation(`group ${group.groupId} is at or past the declared end ${subscription.end}`);
|
|
886
|
+
}
|
|
887
|
+
producer = new netGroup.Producer(group.groupId);
|
|
888
|
+
track.writeGroup(producer);
|
|
889
|
+
}
|
|
890
|
+
return producer;
|
|
891
|
+
};
|
|
803
892
|
try {
|
|
804
|
-
//
|
|
805
|
-
|
|
806
|
-
|
|
893
|
+
// FIRST_OBJECT clear says this stream starts partway through the group, which the
|
|
894
|
+
// draft lets a publisher do to answer a filter. Nothing above here can use it: the
|
|
895
|
+
// objects that would arrive are not decodable without the missing head, and a group
|
|
896
|
+
// is the unit an application resyncs on. Drop it and pick up at the next group, the
|
|
897
|
+
// same degradation as a publisher that no longer holds the head.
|
|
898
|
+
//
|
|
899
|
+
// This only saves reading a stream we would throw away. The bit is the publisher's
|
|
900
|
+
// claim, so what is enforced is the object ids themselves: `Frame.decode` holds every
|
|
901
|
+
// object to starting at 0 and incrementing by 1, whatever the header said and on the
|
|
902
|
+
// drafts that have no such bit to read.
|
|
903
|
+
if (!group.flags.firstObject) {
|
|
904
|
+
console.debug(`dropping a group with no head: alias=${group.trackAlias} group=${group.groupId}`);
|
|
905
|
+
stream.stop(new Error("a group must start at object 0"));
|
|
906
|
+
return;
|
|
907
|
+
}
|
|
908
|
+
// The alias binds after SUBSCRIBE_OK commits the track property; an omitted
|
|
909
|
+
// header priority inherits it (draft-21 section 10.4).
|
|
910
|
+
if (!group.flags.hasPriority)
|
|
911
|
+
group.publisherPriority = toWire((await track.info()).priority);
|
|
912
|
+
const decode = (c) => Frame.decode(c, group.flags, this.#timescales.get(group.trackAlias));
|
|
807
913
|
for (;;) {
|
|
808
|
-
|
|
809
|
-
|
|
914
|
+
// Every object already buffered is written without an await, so the reader wakes
|
|
915
|
+
// once per batch rather than once per object. Only the group's own stream ends it:
|
|
916
|
+
// a track that closes first has already closed (or aborted) this group through its
|
|
917
|
+
// cache.
|
|
918
|
+
const frame = stream.tryDecode(decode) ??
|
|
919
|
+
(await (producer
|
|
920
|
+
? race([stream.decodeMaybe(decode), producer.closed])
|
|
921
|
+
: stream.decodeMaybe(decode)));
|
|
922
|
+
if (!frame || frame instanceof Error)
|
|
810
923
|
break;
|
|
811
|
-
|
|
924
|
+
if (frame.endOfTrack) {
|
|
925
|
+
// No object at or past this location exists: after the group's last object
|
|
926
|
+
// the track ends with it, and at object 0 it ends before it.
|
|
927
|
+
const end = producer ? group.groupId + 1 : group.groupId;
|
|
928
|
+
producer?.close();
|
|
929
|
+
try {
|
|
930
|
+
track.finishAt(end);
|
|
931
|
+
}
|
|
932
|
+
catch (err) {
|
|
933
|
+
throw new ProtocolViolation(`invalid END_OF_TRACK: ${reason(error(err))}`);
|
|
934
|
+
}
|
|
935
|
+
subscription.end ??= end;
|
|
936
|
+
return;
|
|
937
|
+
}
|
|
812
938
|
if (frame.payload === undefined)
|
|
813
939
|
break;
|
|
814
|
-
|
|
940
|
+
open().writeFrame({ payload: frame.payload, timestamp: frame.timestamp ?? Timestamp.now() });
|
|
815
941
|
}
|
|
816
|
-
|
|
942
|
+
// A group with no objects still exists.
|
|
943
|
+
open().close();
|
|
817
944
|
}
|
|
818
945
|
catch (err) {
|
|
819
|
-
const e =
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
946
|
+
const e = await sessionCause(this.#quic, err);
|
|
947
|
+
if (e instanceof ProtocolViolation) {
|
|
948
|
+
// The publisher broke the track's end, which no later group can repair.
|
|
949
|
+
producer?.close(e);
|
|
950
|
+
track.close(e);
|
|
951
|
+
}
|
|
952
|
+
else {
|
|
953
|
+
// A stream that fails before its first object still names a group, which the
|
|
954
|
+
// reader sees fail rather than silently go missing.
|
|
955
|
+
try {
|
|
956
|
+
open().close(e);
|
|
957
|
+
}
|
|
958
|
+
catch {
|
|
959
|
+
// The track has already closed or ended below this group.
|
|
960
|
+
}
|
|
825
961
|
}
|
|
826
|
-
producer.close(e);
|
|
827
962
|
stream.stop(e);
|
|
828
963
|
}
|
|
964
|
+
finally {
|
|
965
|
+
read();
|
|
966
|
+
}
|
|
829
967
|
}
|
|
830
968
|
}
|
|
831
969
|
/**
|