@moq/net 0.4.0 → 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/announced.d.ts +14 -0
- package/announced.d.ts.map +1 -1
- package/announced.js.map +1 -1
- package/broadcast.d.ts +6 -3
- package/broadcast.d.ts.map +1 -1
- package/broadcast.js +9 -5
- package/broadcast.js.map +1 -1
- package/connection/accept.js +9 -1
- package/connection/accept.js.map +1 -1
- package/connection/connect.d.ts.map +1 -1
- package/connection/connect.js +10 -1
- 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 +9 -2
- package/connection/forward.js.map +1 -1
- package/connection/handshake.d.ts +1 -0
- package/connection/handshake.d.ts.map +1 -1
- package/connection/handshake.js +3 -0
- package/connection/handshake.js.map +1 -1
- package/connection/pool.d.ts +8 -10
- package/connection/pool.d.ts.map +1 -1
- package/connection/pool.js +18 -8
- package/connection/pool.js.map +1 -1
- package/connection/reload.js +6 -6
- package/connection/reload.js.map +1 -1
- package/error.d.ts.map +1 -1
- package/error.js +21 -0
- package/error.js.map +1 -1
- 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/connection.d.ts +1 -1
- package/ietf/connection.d.ts.map +1 -1
- package/ietf/connection.js +9 -5
- package/ietf/connection.js.map +1 -1
- package/ietf/error.js +27 -2
- package/ietf/error.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 +5 -0
- package/ietf/parameters.d.ts.map +1 -1
- package/ietf/parameters.js +20 -0
- 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/publisher.js +114 -60
- package/ietf/publisher.js.map +1 -1
- package/ietf/subscribe.d.ts.map +1 -1
- package/ietf/subscribe.js +3 -4
- 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.js +189 -66
- package/ietf/subscriber.js.map +1 -1
- package/internal.d.ts +7 -0
- package/internal.d.ts.map +1 -1
- package/internal.js +11 -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 +126 -11
- 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 +9 -3
- package/lite/connection.js.map +1 -1
- package/lite/publisher.js +168 -109
- package/lite/publisher.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 +159 -93
- 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 +37 -18
- package/origin.d.ts.map +1 -1
- package/origin.js +248 -103
- package/origin.js.map +1 -1
- package/package.json +7 -3
- package/stream.d.ts +2 -2
- package/stream.d.ts.map +1 -1
- package/stream.js +16 -11
- 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/track.d.ts +33 -5
- package/track.d.ts.map +1 -1
- package/track.js +98 -30
- package/track.js.map +1 -1
- package/wire.d.ts +2 -0
- package/wire.d.ts.map +1 -1
- package/wire.js.map +1 -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
8
|
import { Cost, routesEqual, UNKNOWN_HOP } from "../hop.js";
|
|
8
|
-
import { scopeCaptures, scopeHead, scopeOverlaps } from "../internal.js";
|
|
9
|
+
import { hiddenBelow, hooks, scopeCaptures, scopeHead, scopeOverlaps } from "../internal.js";
|
|
9
10
|
import * as Path from "../path.js";
|
|
11
|
+
import { TAIL_GRACE_MS, 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,9 @@ const SUBSCRIBE_OK_TIMEOUT_MS = 10_000;
|
|
|
33
39
|
*/
|
|
34
40
|
export class Subscriber {
|
|
35
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;
|
|
36
45
|
// The Hop IDs this session declared; see {@link Cluster}. What the peer declared is what
|
|
37
46
|
// says whether an advertisement carries a hop path, and ours is what a path looping back
|
|
38
47
|
// to us contains.
|
|
@@ -59,14 +68,18 @@ export class Subscriber {
|
|
|
59
68
|
#announced = new Map();
|
|
60
69
|
// Any consumers that want each new announcement, keyed by their local filter.
|
|
61
70
|
#announcedConsumers = new Map();
|
|
71
|
+
// Whether the peer understands the HIDDEN parameter (MoQ Hidden).
|
|
72
|
+
#hidden;
|
|
62
73
|
/**
|
|
63
74
|
* Creates a new Subscriber instance.
|
|
64
75
|
*
|
|
65
76
|
* @internal
|
|
66
77
|
*/
|
|
67
|
-
constructor({ session, cluster, }) {
|
|
78
|
+
constructor({ session, quic, cluster, hidden = false, }) {
|
|
68
79
|
this.#session = session;
|
|
80
|
+
this.#quic = quic;
|
|
69
81
|
this.#cluster = cluster;
|
|
82
|
+
this.#hidden = hidden;
|
|
70
83
|
}
|
|
71
84
|
/**
|
|
72
85
|
* Whether an advertisement is ours coming back: its hop path already ran through us, so
|
|
@@ -92,13 +105,19 @@ export class Subscriber {
|
|
|
92
105
|
* The peer is asked with SUBSCRIBE_NAMESPACE regardless of what it declared, and an
|
|
93
106
|
* unsolicited PUBLISH_NAMESPACE lands here too, so a peer that only tells and one
|
|
94
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.
|
|
95
113
|
*/
|
|
96
|
-
announced(scope = Path.Pattern.all()) {
|
|
114
|
+
announced(scope = Path.Pattern.all(), options) {
|
|
97
115
|
// The wire speaks announce interest by prefix.
|
|
98
116
|
const prefix = scopeHead(scope);
|
|
117
|
+
const filter = { scope, prefix, hidden: options?.hidden ?? false };
|
|
99
118
|
const announced = new announce.Producer();
|
|
100
119
|
for (const [active, info] of this.#announced) {
|
|
101
|
-
if (!
|
|
120
|
+
if (!sees(filter, active))
|
|
102
121
|
continue;
|
|
103
122
|
announced.append({
|
|
104
123
|
prefix: active,
|
|
@@ -107,8 +126,8 @@ export class Subscriber {
|
|
|
107
126
|
route: info.route,
|
|
108
127
|
});
|
|
109
128
|
}
|
|
110
|
-
this.#announcedConsumers.set(announced,
|
|
111
|
-
void this.#runAnnounced(announced, prefix).finally(() => {
|
|
129
|
+
this.#announcedConsumers.set(announced, filter);
|
|
130
|
+
void this.#runAnnounced(announced, prefix, filter.hidden && this.#hidden).finally(() => {
|
|
112
131
|
this.#announcedConsumers.delete(announced);
|
|
113
132
|
announced.close();
|
|
114
133
|
});
|
|
@@ -126,9 +145,10 @@ export class Subscriber {
|
|
|
126
145
|
}
|
|
127
146
|
this.#announced.set(path, { count: 1, route });
|
|
128
147
|
console.debug(`announced: broadcast=${path} active=true`);
|
|
129
|
-
for (const [consumer,
|
|
130
|
-
if (!
|
|
148
|
+
for (const [consumer, filter] of this.#announcedConsumers) {
|
|
149
|
+
if (!sees(filter, path))
|
|
131
150
|
continue;
|
|
151
|
+
const scope = filter.scope;
|
|
132
152
|
consumer.append({ prefix: path, captures: scopeCaptures(scope, path), kind: "announced", route });
|
|
133
153
|
}
|
|
134
154
|
}
|
|
@@ -143,9 +163,10 @@ export class Subscriber {
|
|
|
143
163
|
return;
|
|
144
164
|
existing.route = route;
|
|
145
165
|
console.debug(`announced: broadcast=${path} rerouted`);
|
|
146
|
-
for (const [consumer,
|
|
147
|
-
if (!
|
|
166
|
+
for (const [consumer, filter] of this.#announcedConsumers) {
|
|
167
|
+
if (!sees(filter, path))
|
|
148
168
|
continue;
|
|
169
|
+
const scope = filter.scope;
|
|
149
170
|
consumer.append({ prefix: path, captures: scopeCaptures(scope, path), kind: "updated", route });
|
|
150
171
|
}
|
|
151
172
|
}
|
|
@@ -165,9 +186,10 @@ export class Subscriber {
|
|
|
165
186
|
// would otherwise hand the dead generation to whoever consumes the path next.
|
|
166
187
|
this.#consumes.evict(path);
|
|
167
188
|
console.debug(`announced: broadcast=${path} active=false`);
|
|
168
|
-
for (const [consumer,
|
|
169
|
-
if (!
|
|
189
|
+
for (const [consumer, filter] of this.#announcedConsumers) {
|
|
190
|
+
if (!sees(filter, path))
|
|
170
191
|
continue;
|
|
192
|
+
const scope = filter.scope;
|
|
171
193
|
try {
|
|
172
194
|
consumer.append({
|
|
173
195
|
prefix: path,
|
|
@@ -181,7 +203,7 @@ export class Subscriber {
|
|
|
181
203
|
}
|
|
182
204
|
}
|
|
183
205
|
}
|
|
184
|
-
async #runAnnounced(announced, prefix) {
|
|
206
|
+
async #runAnnounced(announced, prefix, hidden) {
|
|
185
207
|
const version = this.#session.version;
|
|
186
208
|
// Suffixes live on this stream, so a repeat is recognized as an update to the
|
|
187
209
|
// advertisement rather than a second one, which would leak the count.
|
|
@@ -210,11 +232,11 @@ export class Subscriber {
|
|
|
210
232
|
version === Version.DRAFT_16 ||
|
|
211
233
|
version === Version.DRAFT_17) {
|
|
212
234
|
await stream.writer.u53(SubscribeNamespaceLegacy.id);
|
|
213
|
-
await new SubscribeNamespaceLegacy({ namespace: prefix, requestId }).encode(stream.writer, version);
|
|
235
|
+
await new SubscribeNamespaceLegacy({ namespace: prefix, requestId, hidden }).encode(stream.writer, version);
|
|
214
236
|
}
|
|
215
237
|
else {
|
|
216
238
|
await stream.writer.u53(SubscribeNamespace.id);
|
|
217
|
-
await new SubscribeNamespace({ namespace: prefix, requestId }).encode(stream.writer, version);
|
|
239
|
+
await new SubscribeNamespace({ namespace: prefix, requestId, hidden }).encode(stream.writer, version);
|
|
218
240
|
}
|
|
219
241
|
console.debug(`subscribe_namespace written: requestId=${requestId}`);
|
|
220
242
|
// Read response
|
|
@@ -290,7 +312,7 @@ export class Subscriber {
|
|
|
290
312
|
this.#session.close();
|
|
291
313
|
});
|
|
292
314
|
// Wait for either the read loop or the announced to close
|
|
293
|
-
await
|
|
315
|
+
await race([readLoop, announced.closed]);
|
|
294
316
|
// For v14/v15: send UnsubscribeNamespace before closing
|
|
295
317
|
if (version === Version.DRAFT_14 || version === Version.DRAFT_15) {
|
|
296
318
|
try {
|
|
@@ -360,28 +382,43 @@ export class Subscriber {
|
|
|
360
382
|
})();
|
|
361
383
|
return consumer;
|
|
362
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
|
+
}
|
|
363
397
|
async #runSubscribe(broadcast, request) {
|
|
364
398
|
const requestId = await this.#session.nextRequestId();
|
|
365
399
|
if (requestId === undefined) {
|
|
366
|
-
request.reject(
|
|
400
|
+
request.reject(await this.#closedSession());
|
|
367
401
|
return;
|
|
368
402
|
}
|
|
369
403
|
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({});
|
|
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() };
|
|
376
408
|
// Open the stream and wait for SUBSCRIBE_OK under a timeout. State
|
|
377
409
|
// flows back via `state` so the timeout path can clean up the stream
|
|
378
410
|
// and any registration if setup eventually finishes.
|
|
379
411
|
const state = {};
|
|
380
|
-
const setup = this.#openSubscribe(state, broadcast, request,
|
|
412
|
+
const setup = this.#openSubscribe(state, broadcast, request, subscription, requestId);
|
|
381
413
|
// The publisher can be serving before it answers, so waiting only on the response
|
|
382
414
|
// would miss the local side going away and leave it serving a track nobody reads.
|
|
383
415
|
// Demand returning before we commit is not abandonment, matching the serving loop.
|
|
384
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
|
+
}
|
|
385
422
|
for (;;) {
|
|
386
423
|
await producer.unused();
|
|
387
424
|
if (producer.closed.peek() !== undefined || !producer.used.peek())
|
|
@@ -391,7 +428,7 @@ export class Subscriber {
|
|
|
391
428
|
let stream;
|
|
392
429
|
let trackAlias;
|
|
393
430
|
try {
|
|
394
|
-
const result = await
|
|
431
|
+
const result = await race([
|
|
395
432
|
withTimeout(setup, SUBSCRIBE_OK_TIMEOUT_MS, `subscribe timed out after ${SUBSCRIBE_OK_TIMEOUT_MS}ms waiting for SUBSCRIBE_OK (browser stream limit reached?)`),
|
|
396
433
|
waitAbandoned(),
|
|
397
434
|
]);
|
|
@@ -403,8 +440,8 @@ export class Subscriber {
|
|
|
403
440
|
}
|
|
404
441
|
catch (err) {
|
|
405
442
|
// 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
|
-
|
|
443
|
+
const e = err instanceof TimeoutError ? controlTimeout(err) : await sessionCause(this.#quic, err);
|
|
444
|
+
request.reject(e);
|
|
408
445
|
console.warn(`subscribe error: id=${requestId} broadcast=${broadcast} track=${request.name} error=${reason(e)}`);
|
|
409
446
|
// Runs now for whatever is already open, and again if `setup` settles late.
|
|
410
447
|
// Retirement is repeated because a late SUBSCRIBE_OK can register an alias after
|
|
@@ -412,7 +449,7 @@ export class Subscriber {
|
|
|
412
449
|
let torn = false;
|
|
413
450
|
const cleanup = async (afterSetup) => {
|
|
414
451
|
state.cancelled = true;
|
|
415
|
-
if (state.registeredAlias !== undefined && this.#aliases.retire(state.registeredAlias,
|
|
452
|
+
if (state.registeredAlias !== undefined && this.#aliases.retire(state.registeredAlias, subscription)) {
|
|
416
453
|
this.#timescales.delete(state.registeredAlias);
|
|
417
454
|
}
|
|
418
455
|
if (!state.stream || torn)
|
|
@@ -447,10 +484,10 @@ export class Subscriber {
|
|
|
447
484
|
const publisherEnded = Symbol("publisher");
|
|
448
485
|
const localEnded = Symbol("local");
|
|
449
486
|
const idle = Symbol("idle");
|
|
450
|
-
// Terminal conditions settle at most once (
|
|
451
|
-
//
|
|
452
|
-
const done =
|
|
453
|
-
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),
|
|
454
491
|
producer.closed.then(() => localEnded),
|
|
455
492
|
]);
|
|
456
493
|
// Serve until a terminal condition fires or the last local subscriber leaves. The unused
|
|
@@ -458,7 +495,7 @@ export class Subscriber {
|
|
|
458
495
|
// down resumes on the same stream.
|
|
459
496
|
let terminal = localEnded;
|
|
460
497
|
for (;;) {
|
|
461
|
-
const reason = await
|
|
498
|
+
const reason = await race([done, producer.unused().then(() => idle)]);
|
|
462
499
|
if (reason === idle && producer.closed.peek() === undefined && producer.used.peek())
|
|
463
500
|
continue;
|
|
464
501
|
terminal = reason;
|
|
@@ -477,7 +514,7 @@ export class Subscriber {
|
|
|
477
514
|
console.debug(`subscribe close: id=${requestId} broadcast=${broadcast} track=${request.name}`);
|
|
478
515
|
}
|
|
479
516
|
catch (err) {
|
|
480
|
-
const e =
|
|
517
|
+
const e = await sessionCause(this.#quic, err);
|
|
481
518
|
producer.close(e);
|
|
482
519
|
stream.abort(e);
|
|
483
520
|
console.warn(`subscribe error: id=${requestId} broadcast=${broadcast} track=${request.name} error=${reason(e)}`);
|
|
@@ -485,10 +522,37 @@ export class Subscriber {
|
|
|
485
522
|
finally {
|
|
486
523
|
// Only the owner tears down the alias metadata: a later subscription may have
|
|
487
524
|
// reclaimed the alias and installed its own timescale.
|
|
488
|
-
if (this.#aliases.retire(trackAlias,
|
|
525
|
+
if (this.#aliases.retire(trackAlias, subscription))
|
|
489
526
|
this.#timescales.delete(trackAlias);
|
|
490
527
|
}
|
|
491
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
|
+
}
|
|
492
556
|
/**
|
|
493
557
|
* Tell the publisher to stop serving a subscription we are walking away from.
|
|
494
558
|
*
|
|
@@ -514,7 +578,7 @@ export class Subscriber {
|
|
|
514
578
|
// `state` is populated as soon as the stream opens and again when the
|
|
515
579
|
// trackAlias is registered, so the caller can clean both up on timeout
|
|
516
580
|
// even before this promise settles.
|
|
517
|
-
async #openSubscribe(state, broadcast, request,
|
|
581
|
+
async #openSubscribe(state, broadcast, request, subscription, requestId) {
|
|
518
582
|
const version = this.#session.version;
|
|
519
583
|
state.stream = await this.#session.openBi();
|
|
520
584
|
// The timeout can fire while the open is still in flight, in which case cleanup ran
|
|
@@ -562,8 +626,11 @@ export class Subscriber {
|
|
|
562
626
|
throw new Error(`SUBSCRIBE error: ${reasonPhrase}`);
|
|
563
627
|
}
|
|
564
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) });
|
|
565
632
|
try {
|
|
566
|
-
this.#aliases.set(ok.trackAlias,
|
|
633
|
+
this.#aliases.set(ok.trackAlias, subscription, { broadcast, name: request.name });
|
|
567
634
|
const timescale = ok.properties.timescale;
|
|
568
635
|
if (timescale !== undefined) {
|
|
569
636
|
this.#timescales.set(ok.trackAlias, timescale);
|
|
@@ -784,48 +851,104 @@ export class Subscriber {
|
|
|
784
851
|
if (group.subGroupId !== 0) {
|
|
785
852
|
throw new Error("subgroups are not supported");
|
|
786
853
|
}
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
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);
|
|
800
868
|
return;
|
|
801
869
|
}
|
|
802
|
-
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
|
+
};
|
|
803
883
|
try {
|
|
804
|
-
//
|
|
805
|
-
|
|
806
|
-
|
|
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);
|
|
807
903
|
for (;;) {
|
|
808
|
-
|
|
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());
|
|
809
907
|
if (done !== false)
|
|
810
908
|
break;
|
|
811
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
|
+
}
|
|
812
923
|
if (frame.payload === undefined)
|
|
813
924
|
break;
|
|
814
|
-
|
|
925
|
+
open().writeFrame({ payload: frame.payload, timestamp: frame.timestamp ?? Timestamp.now() });
|
|
815
926
|
}
|
|
816
|
-
|
|
927
|
+
// A group with no objects still exists.
|
|
928
|
+
open().close();
|
|
817
929
|
}
|
|
818
930
|
catch (err) {
|
|
819
931
|
const e = error(err);
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
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
|
+
}
|
|
825
946
|
}
|
|
826
|
-
producer.close(e);
|
|
827
947
|
stream.stop(e);
|
|
828
948
|
}
|
|
949
|
+
finally {
|
|
950
|
+
read();
|
|
951
|
+
}
|
|
829
952
|
}
|
|
830
953
|
}
|
|
831
954
|
/**
|