@moq/net 0.4.1 → 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/broadcast.d.ts +21 -7
- package/broadcast.d.ts.map +1 -1
- package/broadcast.js +38 -16
- package/broadcast.js.map +1 -1
- package/connection/accept.js +6 -4
- package/connection/accept.js.map +1 -1
- package/connection/connect.d.ts.map +1 -1
- package/connection/connect.js +5 -4
- package/connection/connect.js.map +1 -1
- package/connection/forward.js +54 -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 +4 -2
- package/connection/handshake.d.ts.map +1 -1
- package/connection/handshake.js +42 -10
- package/connection/handshake.js.map +1 -1
- package/connection/pool.d.ts +16 -1
- package/connection/pool.d.ts.map +1 -1
- package/connection/pool.js +32 -10
- package/connection/pool.js.map +1 -1
- package/connection/reload.d.ts.map +1 -1
- package/connection/reload.js +184 -30
- package/connection/reload.js.map +1 -1
- package/error.d.ts +11 -0
- package/error.d.ts.map +1 -1
- package/error.js +14 -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 +4 -0
- package/ietf/adapter.d.ts.map +1 -1
- package/ietf/adapter.js +19 -8
- 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.map +1 -1
- package/ietf/connection.js +110 -37
- package/ietf/connection.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.js +1 -1
- package/ietf/hidden.js.map +1 -1
- package/ietf/index.d.ts +1 -1
- package/ietf/index.d.ts.map +1 -1
- package/ietf/index.js +1 -1
- package/ietf/index.js.map +1 -1
- package/ietf/object.d.ts +7 -6
- package/ietf/object.d.ts.map +1 -1
- package/ietf/object.js +37 -33
- package/ietf/object.js.map +1 -1
- package/ietf/parameters.d.ts +7 -0
- package/ietf/parameters.d.ts.map +1 -1
- package/ietf/parameters.js +62 -12
- package/ietf/parameters.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 +287 -172
- 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 +22 -14
- package/ietf/subscribe.js.map +1 -1
- package/ietf/subscribe_namespace.d.ts.map +1 -1
- package/ietf/subscribe_namespace.js +2 -3
- package/ietf/subscribe_namespace.js.map +1 -1
- package/ietf/subscriber.js +57 -42
- 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 +12 -3
- package/internal.d.ts.map +1 -1
- package/internal.js +32 -0
- package/internal.js.map +1 -1
- package/lite/announce.d.ts.map +1 -1
- package/lite/announce.js +7 -4
- package/lite/announce.js.map +1 -1
- package/lite/connection.d.ts.map +1 -1
- package/lite/connection.js +22 -7
- 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 +19 -37
- 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/subscriber.d.ts.map +1 -1
- package/lite/subscriber.js +206 -127
- package/lite/subscriber.js.map +1 -1
- package/origin.d.ts +9 -1
- package/origin.d.ts.map +1 -1
- package/origin.js +341 -146
- package/origin.js.map +1 -1
- package/package.json +2 -2
- package/stream.d.ts +77 -12
- package/stream.d.ts.map +1 -1
- package/stream.js +359 -122
- package/stream.js.map +1 -1
- package/tail.d.ts +7 -0
- package/tail.d.ts.map +1 -1
- package/tail.js +68 -22
- package/tail.js.map +1 -1
- package/track.d.ts +14 -3
- package/track.d.ts.map +1 -1
- package/track.js +170 -76
- 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 +17 -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/publisher.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
/* @ts-self-types="./publisher.d.ts" */
|
|
2
2
|
import { race, Signal } from "@moq/signals";
|
|
3
3
|
import { controlTimeout, error, reason, StreamCode, StreamError } from "../error.js";
|
|
4
|
-
import {
|
|
5
|
-
import { hiddenBelow, hooks } from "../internal.js";
|
|
4
|
+
import { hiddenBelow, hooks, presented } from "../internal.js";
|
|
6
5
|
import * as Path from "../path.js";
|
|
7
6
|
import { Writer } from "../stream.js";
|
|
8
7
|
import { Milli, Timescale } from "../time.js";
|
|
@@ -11,13 +10,13 @@ import * as Varint from "../varint.js";
|
|
|
11
10
|
import { wireOf } from "../wire.js";
|
|
12
11
|
import * as Cluster from "./cluster.js";
|
|
13
12
|
import { requestReason, toRequestCode } from "./error.js";
|
|
14
|
-
import { FetchHeader } from "./fetch.js";
|
|
13
|
+
import { FetchError, FetchHeader } from "./fetch.js";
|
|
15
14
|
import * as Filter from "./filter.js";
|
|
16
15
|
import { FetchFrame, Frame, Group as GroupMessage } from "./object.js";
|
|
17
16
|
import { fromWire, toWire } from "./priority.js";
|
|
18
17
|
import * as Properties from "./properties.js";
|
|
19
18
|
import { PublishDone, PublishDoneStatus } from "./publish.js";
|
|
20
|
-
import { PublishNamespace, PublishNamespaceDone, PublishNamespaceOk } from "./publish_namespace.js";
|
|
19
|
+
import { PublishNamespace, PublishNamespaceDone, PublishNamespaceOk, PublishNamespaceUpdate, } from "./publish_namespace.js";
|
|
21
20
|
import { RequestError, RequestOk } from "./request.js";
|
|
22
21
|
import { SubscribeError, SubscribeOk } from "./subscribe.js";
|
|
23
22
|
import { SubscribeNamespaceEntry, SubscribeNamespaceEntryDone, SubscribeNamespaceOk, } from "./subscribe_namespace.js";
|
|
@@ -36,8 +35,30 @@ function clusterFor(base, route) {
|
|
|
36
35
|
const hops = route.hops.at(-1) === self ? [...route.hops] : [...route.hops, self];
|
|
37
36
|
return { hops, cost: route.cost.warm };
|
|
38
37
|
}
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
/**
|
|
39
|
+
* What it takes to move the peer from `held` to `next`.
|
|
40
|
+
*
|
|
41
|
+
* - `"same"`: the peer would decode the same advertisement. The cold cost, and the whole
|
|
42
|
+
* route without Cluster, never reach the wire, so a change there sends nothing.
|
|
43
|
+
* - `"restart"`: nothing is held, nothing is wanted, or a different broadcast, which is
|
|
44
|
+
* withdrawn and advertised again.
|
|
45
|
+
* - Otherwise the same broadcast at a new hop chain or warm cost: the cluster parameters
|
|
46
|
+
* the peer holds and the ones it should, for the target to update.
|
|
47
|
+
*/
|
|
48
|
+
function change(base, held, next) {
|
|
49
|
+
if (held === undefined || next === undefined || held.identity !== next.identity)
|
|
50
|
+
return "restart";
|
|
51
|
+
const from = clusterFor(base, held.route);
|
|
52
|
+
const to = clusterFor(base, next.route);
|
|
53
|
+
if (from === undefined || to === undefined)
|
|
54
|
+
return from === to ? "same" : "restart";
|
|
55
|
+
if (from.cost === to.cost && sameHops(from.hops, to.hops))
|
|
56
|
+
return "same";
|
|
57
|
+
return { from, to };
|
|
58
|
+
}
|
|
59
|
+
/** Whether two hop paths list the same hops in the same order. */
|
|
60
|
+
function sameHops(a, b) {
|
|
61
|
+
return a.length === b.length && a.every((hop, i) => hop === b[i]);
|
|
41
62
|
}
|
|
42
63
|
/**
|
|
43
64
|
* How long one advertisement may take to be answered. Matches the Rust publisher, and the
|
|
@@ -59,6 +80,7 @@ export class Publisher {
|
|
|
59
80
|
#quic;
|
|
60
81
|
#session;
|
|
61
82
|
#requiresSolicitation;
|
|
83
|
+
#hidden;
|
|
62
84
|
// The origin this session serves, borrowed: it outlives the session, and closing the
|
|
63
85
|
// session leaves its broadcasts alone. The namespaces are advertised with an unsolicited
|
|
64
86
|
// PUBLISH_NAMESPACE (see {@link runPublishNamespaces}), or on request if the peer asked
|
|
@@ -74,13 +96,14 @@ export class Publisher {
|
|
|
74
96
|
*
|
|
75
97
|
* @internal
|
|
76
98
|
*/
|
|
77
|
-
constructor({ quic, session, publish, requiresSolicitation, cluster, }) {
|
|
99
|
+
constructor({ quic, session, publish, requiresSolicitation, hidden = false, cluster, }) {
|
|
78
100
|
this.#quic = quic;
|
|
79
101
|
this.#session = session;
|
|
80
102
|
const origin = publish && wireOf(publish);
|
|
81
103
|
this.#advertised = origin?.advertised ?? new Signal(new Map());
|
|
82
104
|
this.#publish = publish;
|
|
83
105
|
this.#requiresSolicitation = requiresSolicitation;
|
|
106
|
+
this.#hidden = hidden;
|
|
84
107
|
this.#advert = Cluster.advertise(cluster);
|
|
85
108
|
}
|
|
86
109
|
/**
|
|
@@ -94,20 +117,33 @@ export class Publisher {
|
|
|
94
117
|
const name = msg.trackNamespace;
|
|
95
118
|
let broadcast;
|
|
96
119
|
let refusal;
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
120
|
+
// Legal requests we can't honor are refused one at a time. A subscription that
|
|
121
|
+
// forwards nothing is only useful to a subscriber that later turns forwarding on,
|
|
122
|
+
// and serving a Range Filter unfiltered would deliver objects it excluded.
|
|
123
|
+
const unsupported = !msg.forward
|
|
124
|
+
? "FORWARD=0 not supported"
|
|
125
|
+
: msg.rangeFilters
|
|
126
|
+
? "range filters not supported"
|
|
127
|
+
: undefined;
|
|
128
|
+
if (unsupported) {
|
|
129
|
+
refusal = { errorCode: toRequestCode("not_supported", "subscribe", version), reasonPhrase: unsupported };
|
|
106
130
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
131
|
+
else {
|
|
132
|
+
try {
|
|
133
|
+
broadcast =
|
|
134
|
+
this.#publish && (wireOf(this.#publish).local(name) ?? (await wireOf(this.#publish).demand(name)));
|
|
135
|
+
if (!broadcast) {
|
|
136
|
+
refusal = {
|
|
137
|
+
errorCode: toRequestCode("does_not_exist", "subscribe", version),
|
|
138
|
+
reasonPhrase: "broadcast not found",
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
catch (err) {
|
|
143
|
+
const e = error(err);
|
|
144
|
+
const condition = e instanceof StreamError && e.code === StreamCode.NotFound ? "does_not_exist" : "internal";
|
|
145
|
+
refusal = { errorCode: toRequestCode(condition, "subscribe", version), reasonPhrase: reason(e) };
|
|
146
|
+
}
|
|
111
147
|
}
|
|
112
148
|
if (refusal) {
|
|
113
149
|
if (version === Version.DRAFT_14) {
|
|
@@ -374,7 +410,7 @@ export class Publisher {
|
|
|
374
410
|
},
|
|
375
411
|
});
|
|
376
412
|
try {
|
|
377
|
-
await hooks.guardGroup(group, header.encode(stream, this.#session.version));
|
|
413
|
+
await hooks.guardGroup(group, () => header.encode(stream, this.#session.version));
|
|
378
414
|
// The first written object goes on the wire as its absolute id, so a trimmed
|
|
379
415
|
// head shows the true numbering rather than a silently renumbered group.
|
|
380
416
|
let first = true;
|
|
@@ -402,7 +438,7 @@ export class Publisher {
|
|
|
402
438
|
const obj = new Frame({ payload: read.frame.payload, timestamp: read.frame.timestamp });
|
|
403
439
|
const delta = first ? read.sequence : 0;
|
|
404
440
|
first = false;
|
|
405
|
-
await hooks.guardGroup(group, obj.encode(stream, header.flags, timescale, this.#session.version, delta));
|
|
441
|
+
await hooks.guardGroup(group, () => obj.encode(stream, header.flags, timescale, this.#session.version, delta));
|
|
406
442
|
}
|
|
407
443
|
finally {
|
|
408
444
|
read.complete();
|
|
@@ -546,8 +582,9 @@ export class Publisher {
|
|
|
546
582
|
* This carries the advertisements when the peer asked to be told on request (MoQ
|
|
547
583
|
* Solicit); otherwise {@link runPublishNamespaces} has already announced everything
|
|
548
584
|
* visible and repeating it here would leave the peer holding two sources for one
|
|
549
|
-
* broadcast, so only the hidden namespaces it may see ride here (MoQ Hidden).
|
|
550
|
-
*
|
|
585
|
+
* broadcast, so only the hidden namespaces it may see ride here (MoQ Hidden).
|
|
586
|
+
* Draft-16+ streams Namespace entries inline; draft-14/15 predate those messages, so
|
|
587
|
+
* each advertisement is a PUBLISH_NAMESPACE request of its own.
|
|
551
588
|
*
|
|
552
589
|
* @internal
|
|
553
590
|
*/
|
|
@@ -574,36 +611,35 @@ export class Publisher {
|
|
|
574
611
|
// Hidden namespaces are left out unless the peer opted in (MoQ Hidden). Unless the
|
|
575
612
|
// peer asked to be told only on request, it has already heard everything visible
|
|
576
613
|
// from the empty prefix unasked, so this stream carries only what that hid.
|
|
577
|
-
const carries = (covered) => (msg.hidden || !hiddenBelow(prefix, covered)) &&
|
|
578
|
-
(this.#requiresSolicitation || hiddenBelow(Path.empty(), covered));
|
|
579
|
-
//
|
|
580
|
-
//
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
return await this.#advertise(Path.join(prefix, suffix), requests, refused, cluster);
|
|
585
|
-
}
|
|
614
|
+
const carries = (covered) => (!this.#hidden || msg.hidden || !hiddenBelow(prefix, covered)) &&
|
|
615
|
+
(this.#requiresSolicitation || (this.#hidden && hiddenBelow(Path.empty(), covered)));
|
|
616
|
+
// Inline entries always land, and the receiver treats a repeated NAMESPACE as a
|
|
617
|
+
// replacement, so repricing one is just sending it again, a new original
|
|
618
|
+
// publisher included, as Rust does. Only a legacy PUBLISH_NAMESPACE request can
|
|
619
|
+
// be declined.
|
|
620
|
+
const namespace = async (suffix, snap) => {
|
|
586
621
|
await stream.writer.u53(SubscribeNamespaceEntry.id);
|
|
622
|
+
const cluster = clusterFor(this.#advert, snap.route);
|
|
587
623
|
await new SubscribeNamespaceEntry({ suffix, cluster }).encode(stream.writer, version);
|
|
588
|
-
return
|
|
624
|
+
return "held";
|
|
589
625
|
};
|
|
590
|
-
const
|
|
591
|
-
|
|
592
|
-
|
|
626
|
+
const target = legacy
|
|
627
|
+
? {
|
|
628
|
+
advertise: (suffix, snap) => this.#advertise(Path.join(prefix, suffix), requests, clusterFor(this.#advert, snap.route)),
|
|
629
|
+
update: (suffix, _snap, reprice) => this.#update(Path.join(prefix, suffix), requests, reprice),
|
|
630
|
+
withdraw: (suffix) => this.#withdraw(Path.join(prefix, suffix), requests),
|
|
593
631
|
}
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
632
|
+
: {
|
|
633
|
+
advertise: namespace,
|
|
634
|
+
update: namespace,
|
|
635
|
+
withdraw: async (suffix) => {
|
|
636
|
+
await stream.writer.u53(SubscribeNamespaceEntryDone.id);
|
|
637
|
+
await new SubscribeNamespaceEntryDone({ suffix }).encode(stream.writer, version);
|
|
638
|
+
},
|
|
639
|
+
};
|
|
640
|
+
// Keyed by suffix, as the entries name them.
|
|
641
|
+
const ns = { active: new Map(), refused: new Map(), offered: new Map() };
|
|
602
642
|
let retry = 0;
|
|
603
|
-
// What the peer refused, and whether coming back is worth anything.
|
|
604
|
-
const refused = new Map();
|
|
605
|
-
// The identity each refusal was about, so a republish at the same path clears it.
|
|
606
|
-
const offered = new Map();
|
|
607
643
|
for (;;) {
|
|
608
644
|
// Subscribe BEFORE reconciling, for the same reason as
|
|
609
645
|
// {@link runPublishNamespaces}: a publish landing while an advertisement
|
|
@@ -618,52 +654,7 @@ export class Publisher {
|
|
|
618
654
|
dispose();
|
|
619
655
|
break;
|
|
620
656
|
}
|
|
621
|
-
const
|
|
622
|
-
for (const [covered, snap] of advertised) {
|
|
623
|
-
const suffix = Path.stripPrefix(prefix, covered);
|
|
624
|
-
if (suffix === null || !carries(covered))
|
|
625
|
-
continue;
|
|
626
|
-
updated.set(suffix, snap);
|
|
627
|
-
}
|
|
628
|
-
// A namespace that is gone, or that a republish replaced, takes its refusal with
|
|
629
|
-
// it: the peer refused a broadcast, not a path forever, so a different one at
|
|
630
|
-
// the same path is offered again. The origin swaps the front in one mutation,
|
|
631
|
-
// so the path never leaves `updated` and only the front says they differ.
|
|
632
|
-
for (const path of [...refused.keys()]) {
|
|
633
|
-
const suffix = Path.stripPrefix(prefix, path);
|
|
634
|
-
const snap = suffix === null ? undefined : updated.get(suffix);
|
|
635
|
-
if (snap === undefined || offered.get(path) !== snap.identity) {
|
|
636
|
-
refused.delete(path);
|
|
637
|
-
offered.delete(path);
|
|
638
|
-
}
|
|
639
|
-
}
|
|
640
|
-
// Track what the peer holds rather than what we attempted: a declined
|
|
641
|
-
// advertisement stays out of `held`, so the next turn retries it instead of
|
|
642
|
-
// believing the namespace is already up.
|
|
643
|
-
const held = new Map(active);
|
|
644
|
-
// Withdraw first so a republish or re-price reads as withdraw-then-advertise
|
|
645
|
-
// (a restart). Identity change is a new broadcast; a route change is the
|
|
646
|
-
// same one at a new cost or hop chain.
|
|
647
|
-
for (const [removed, snap] of active) {
|
|
648
|
-
if (sameAdvert(updated.get(removed), snap))
|
|
649
|
-
continue;
|
|
650
|
-
await withdraw(removed);
|
|
651
|
-
held.delete(removed);
|
|
652
|
-
}
|
|
653
|
-
for (const [added, snap] of updated) {
|
|
654
|
-
if (sameAdvert(held.get(added), snap))
|
|
655
|
-
continue;
|
|
656
|
-
if (!this.#offerable(Path.join(prefix, added), refused))
|
|
657
|
-
continue;
|
|
658
|
-
offered.set(Path.join(prefix, added), snap.identity);
|
|
659
|
-
if (await advertise(added, snap))
|
|
660
|
-
held.set(added, snap);
|
|
661
|
-
}
|
|
662
|
-
active = held;
|
|
663
|
-
// Whatever we wanted up and could not get up, as {@link runPublishNamespaces}
|
|
664
|
-
// does: only a legacy request can be declined, and nothing about the peer
|
|
665
|
-
// starting to answer raises a signal this loop is watching.
|
|
666
|
-
const outstanding = [...updated].some(([suffix, snap]) => !sameAdvert(active.get(suffix), snap) && this.#pending(Path.join(prefix, suffix), refused));
|
|
657
|
+
const outstanding = await this.#reconcile(ns, presented(prefix, advertised, carries), target);
|
|
667
658
|
retry = outstanding ? Math.min(retry ? retry * 2 : RETRY_BASE, RETRY_MAX) : 0;
|
|
668
659
|
// Wait for the next change, or for the peer to unsubscribe.
|
|
669
660
|
const next = await (retry
|
|
@@ -706,6 +697,11 @@ export class Publisher {
|
|
|
706
697
|
}
|
|
707
698
|
// The open PUBLISH_NAMESPACE request per advertised path.
|
|
708
699
|
const requests = new Map();
|
|
700
|
+
const target = {
|
|
701
|
+
advertise: (path, snap) => this.#advertise(path, requests, clusterFor(this.#advert, snap.route)),
|
|
702
|
+
update: (path, _snap, reprice) => this.#update(path, requests, reprice),
|
|
703
|
+
withdraw: (path) => this.#withdraw(path, requests),
|
|
704
|
+
};
|
|
709
705
|
// The origin outlives the session and its signal never ends, so this loop needs the
|
|
710
706
|
// session's own ending to stop: without it a closed connection leaves the loop parked
|
|
711
707
|
// on a shared origin forever, waking on someone else's publish to fail on a dead
|
|
@@ -714,14 +710,9 @@ export class Publisher {
|
|
|
714
710
|
const closed = this.#quic.closed.then(() => undefined, () => undefined);
|
|
715
711
|
let dispose;
|
|
716
712
|
try {
|
|
717
|
-
//
|
|
718
|
-
|
|
719
|
-
let active = new Map();
|
|
713
|
+
// Keyed by path.
|
|
714
|
+
const ns = { active: new Map(), refused: new Map(), offered: new Map() };
|
|
720
715
|
let retry = 0;
|
|
721
|
-
// What the peer refused, and whether coming back is worth anything.
|
|
722
|
-
const refused = new Map();
|
|
723
|
-
// The identity each refusal was about, so a republish at the same path clears it.
|
|
724
|
-
const offered = new Map();
|
|
725
716
|
for (;;) {
|
|
726
717
|
// Subscribe BEFORE reconciling. Each advertisement below waits a round trip
|
|
727
718
|
// for the peer's reply, and a publish that lands in that window notifies
|
|
@@ -738,47 +729,13 @@ export class Publisher {
|
|
|
738
729
|
break;
|
|
739
730
|
}
|
|
740
731
|
const updated = new Map();
|
|
741
|
-
for (const [covered,
|
|
742
|
-
//
|
|
743
|
-
if (hiddenBelow(Path.empty(), covered))
|
|
744
|
-
continue;
|
|
745
|
-
updated.set(covered, snap);
|
|
746
|
-
}
|
|
747
|
-
// A namespace that is gone, or that a republish replaced, takes its refusal with
|
|
748
|
-
// it: the peer refused a broadcast, not a path forever, so a different one at
|
|
749
|
-
// the same path is offered again. Rust gets this by rebuilding the watched entry.
|
|
750
|
-
for (const path of [...refused.keys()]) {
|
|
751
|
-
const snap = updated.get(path);
|
|
752
|
-
if (snap === undefined || offered.get(path) !== snap.identity) {
|
|
753
|
-
refused.delete(path);
|
|
754
|
-
offered.delete(path);
|
|
755
|
-
}
|
|
756
|
-
}
|
|
757
|
-
// Withdraw first so a republish or re-price reads as withdraw-then-advertise
|
|
758
|
-
// (a restart) rather than nothing.
|
|
759
|
-
for (const [removed, snap] of active) {
|
|
760
|
-
if (sameAdvert(updated.get(removed), snap))
|
|
732
|
+
for (const [covered, candidates] of advertised) {
|
|
733
|
+
// Only peers that declared MoQ Hidden filter unsolicited discovery.
|
|
734
|
+
if ((this.#hidden && hiddenBelow(Path.empty(), covered)) || candidates.length === 0)
|
|
761
735
|
continue;
|
|
762
|
-
|
|
736
|
+
updated.set(covered, candidates[0]);
|
|
763
737
|
}
|
|
764
|
-
|
|
765
|
-
if (sameAdvert(active.get(added), snap))
|
|
766
|
-
continue;
|
|
767
|
-
if (!this.#offerable(added, refused))
|
|
768
|
-
continue;
|
|
769
|
-
offered.set(added, snap.identity);
|
|
770
|
-
await this.#advertise(added, requests, refused, clusterFor(this.#advert, snap.route));
|
|
771
|
-
}
|
|
772
|
-
// What the peer holds, not what we attempted: a declined PUBLISH_NAMESPACE
|
|
773
|
-
// leaves no request behind, so it stays outstanding below.
|
|
774
|
-
active = new Map([...requests.keys()].flatMap((path) => {
|
|
775
|
-
const snap = updated.get(path);
|
|
776
|
-
return snap ? [[path, snap]] : [];
|
|
777
|
-
}));
|
|
778
|
-
// Whatever we wanted up and could not get up. Stream credit freeing, a
|
|
779
|
-
// transient failure clearing, or the peer starting to answer raises no
|
|
780
|
-
// signal of its own, so the only way back is to ask again on a timer.
|
|
781
|
-
const outstanding = [...updated].some(([path, snap]) => !sameAdvert(active.get(path), snap) && this.#pending(path, refused));
|
|
738
|
+
const outstanding = await this.#reconcile(ns, updated, target);
|
|
782
739
|
retry = outstanding ? Math.min(retry ? retry * 2 : RETRY_BASE, RETRY_MAX) : 0;
|
|
783
740
|
// Wait for the next change, which has already fired if one landed above.
|
|
784
741
|
const next = await (retry
|
|
@@ -802,14 +759,79 @@ export class Publisher {
|
|
|
802
759
|
}
|
|
803
760
|
}
|
|
804
761
|
}
|
|
762
|
+
/**
|
|
763
|
+
* Bring the peer's view in line with `updated`, one namespace at a time, and report
|
|
764
|
+
* whether anything it should hold is still missing and worth coming back for.
|
|
765
|
+
*
|
|
766
|
+
* Nothing the peer cannot see is sent, a reprice updates the namespace in place, and
|
|
767
|
+
* anything else is withdrawn first so a republish reads as withdraw-then-advertise.
|
|
768
|
+
* `ns` records what the peer holds rather than what we attempted: a declined
|
|
769
|
+
* advertisement or update stays out of it, so the next turn offers it fresh instead
|
|
770
|
+
* of believing it is already up.
|
|
771
|
+
*/
|
|
772
|
+
async #reconcile(ns, updated, target) {
|
|
773
|
+
// A namespace that is gone, or that a republish replaced, takes its refusal with
|
|
774
|
+
// it: the peer refused a broadcast, not a path forever, so a different one at the
|
|
775
|
+
// same path is offered again. The origin swaps the front in one mutation, so the
|
|
776
|
+
// path never leaves `updated` and only the front says they differ. Rust gets this
|
|
777
|
+
// by rebuilding the watched entry.
|
|
778
|
+
for (const key of [...ns.refused.keys()]) {
|
|
779
|
+
const snap = updated.get(key);
|
|
780
|
+
if (snap === undefined || ns.offered.get(key) !== snap.identity) {
|
|
781
|
+
ns.refused.delete(key);
|
|
782
|
+
ns.offered.delete(key);
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
const held = new Map(ns.active);
|
|
786
|
+
for (const [key, snap] of ns.active) {
|
|
787
|
+
if (change(this.#advert, snap, updated.get(key)) !== "restart")
|
|
788
|
+
continue;
|
|
789
|
+
await target.withdraw(key);
|
|
790
|
+
held.delete(key);
|
|
791
|
+
}
|
|
792
|
+
for (const [key, snap] of updated) {
|
|
793
|
+
const prev = held.get(key);
|
|
794
|
+
const diff = change(this.#advert, prev, snap);
|
|
795
|
+
if (diff === "same")
|
|
796
|
+
continue;
|
|
797
|
+
let answer;
|
|
798
|
+
if (prev !== undefined && diff !== "restart") {
|
|
799
|
+
answer = await target.update(key, snap, diff);
|
|
800
|
+
}
|
|
801
|
+
else {
|
|
802
|
+
// A fresh offer waits for what a refusal asked for, whatever brought us back.
|
|
803
|
+
if (!this.#offerable(key, ns.refused))
|
|
804
|
+
continue;
|
|
805
|
+
answer = await target.advertise(key, snap);
|
|
806
|
+
}
|
|
807
|
+
// `offered` pairs with `refused`, so a held or withdrawn path leaves nothing behind.
|
|
808
|
+
if (answer === "held") {
|
|
809
|
+
held.set(key, snap);
|
|
810
|
+
ns.refused.delete(key);
|
|
811
|
+
ns.offered.delete(key);
|
|
812
|
+
}
|
|
813
|
+
else {
|
|
814
|
+
held.delete(key);
|
|
815
|
+
if (answer !== "dropped") {
|
|
816
|
+
ns.refused.set(key, answer);
|
|
817
|
+
ns.offered.set(key, snap.identity);
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
ns.active = held;
|
|
822
|
+
// Whatever we wanted up and could not get up. Stream credit freeing, a transient
|
|
823
|
+
// failure clearing, or the peer starting to answer raises no signal of its own, so
|
|
824
|
+
// the only way back is to ask again on a timer.
|
|
825
|
+
return [...updated].some(([key, snap]) => change(this.#advert, held.get(key), snap) !== "same" && this.#pending(key, ns.refused));
|
|
826
|
+
}
|
|
805
827
|
/**
|
|
806
828
|
* Whether a namespace may be offered to the peer right now.
|
|
807
829
|
*
|
|
808
830
|
* A peer that asked never to be offered it again means it, whatever brought us back;
|
|
809
831
|
* one that named a minimum wait gets it, even when our own backoff comes round sooner.
|
|
810
832
|
*/
|
|
811
|
-
#offerable(
|
|
812
|
-
const entry = refused.get(
|
|
833
|
+
#offerable(key, refused) {
|
|
834
|
+
const entry = refused.get(key);
|
|
813
835
|
if (entry === undefined)
|
|
814
836
|
return true;
|
|
815
837
|
return entry !== "never" && Date.now() >= entry;
|
|
@@ -822,21 +844,31 @@ export class Publisher {
|
|
|
822
844
|
* gating the timer on offerable instead would disarm it for exactly the wait it is
|
|
823
845
|
* supposed to be counting. Only a refusal that forbids retrying ends it.
|
|
824
846
|
*/
|
|
825
|
-
#pending(
|
|
826
|
-
return refused.get(
|
|
847
|
+
#pending(key, refused) {
|
|
848
|
+
return refused.get(key) !== "never";
|
|
849
|
+
}
|
|
850
|
+
/**
|
|
851
|
+
* What a REQUEST_ERROR's retry interval, in milliseconds, says about re-offering the
|
|
852
|
+
* namespace.
|
|
853
|
+
*
|
|
854
|
+
* Draft-14/15 carry no such field, so a 0 there says nothing and our own backoff
|
|
855
|
+
* stands; everywhere else 0 means the peer does not want this again.
|
|
856
|
+
*/
|
|
857
|
+
#refusal(err) {
|
|
858
|
+
const version = this.#session.version;
|
|
859
|
+
if (version === Version.DRAFT_14 || version === Version.DRAFT_15)
|
|
860
|
+
return "dropped";
|
|
861
|
+
return err.retryInterval === 0n ? "never" : Date.now() + Number(err.retryInterval);
|
|
827
862
|
}
|
|
828
863
|
/**
|
|
829
864
|
* Advertise one namespace on its own PUBLISH_NAMESPACE request. A declined request
|
|
830
865
|
* is logged and skipped: a peer that wants none of this rejects each one and stays
|
|
831
866
|
* connected.
|
|
832
|
-
*
|
|
833
|
-
* `refused` records what a refusal said about coming back, so a peer that asked not to
|
|
834
|
-
* be offered a namespace again is not re-offered it by the retry above.
|
|
835
867
|
*/
|
|
836
|
-
async #advertise(path, requests,
|
|
868
|
+
async #advertise(path, requests, cluster = this.#advert) {
|
|
837
869
|
const requestId = await this.#session.nextRequestId();
|
|
838
870
|
if (requestId === undefined)
|
|
839
|
-
return
|
|
871
|
+
return "dropped";
|
|
840
872
|
// Opening is inside the try because it fails like any other advertisement does:
|
|
841
873
|
// a peer out of stream credit rejects here, and letting that escape would unwind
|
|
842
874
|
// the announce loop and take discovery down for the whole session, rather than
|
|
@@ -849,22 +881,18 @@ export class Publisher {
|
|
|
849
881
|
// advertisements, so a peer that takes the stream and answers nothing strands
|
|
850
882
|
// every publish and withdrawal behind it. Timing out costs this namespace a
|
|
851
883
|
// turn and leaves it outstanding, which the retry above re-offers.
|
|
852
|
-
await withTimeout((async () => {
|
|
884
|
+
const answer = await withTimeout((async () => {
|
|
853
885
|
await stream.writer.u53(PublishNamespace.id);
|
|
854
886
|
const msg = new PublishNamespace({ requestId, trackNamespace: path, cluster });
|
|
855
887
|
await msg.encode(stream.writer, this.#session.version);
|
|
856
888
|
// Read response (RequestOk and PublishNamespaceOk share 0x07)
|
|
857
889
|
const respTypeId = await stream.reader.u53();
|
|
858
890
|
if (respTypeId === RequestError.id) {
|
|
859
|
-
// The peer named how long to stay away, in milliseconds. Draft-14/15
|
|
860
|
-
// carry no such field, so a 0 there says nothing and our own backoff
|
|
861
|
-
// stands; everywhere else 0 means it does not want this again.
|
|
862
891
|
const err = await RequestError.decode(stream.reader, this.#session.version);
|
|
863
|
-
const
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
throw new Error(`PublishNamespace rejected: ${requestReason(err.errorCode, err.reasonPhrase, "publish_namespace", this.#session.version)}`);
|
|
892
|
+
const why = requestReason(err.errorCode, err.reasonPhrase, "publish_namespace", this.#session.version);
|
|
893
|
+
console.warn(`announce refused: broadcast=${path} error=${why}`);
|
|
894
|
+
stream.abort(new Error(`PublishNamespace rejected: ${why}`));
|
|
895
|
+
return this.#refusal(err);
|
|
868
896
|
}
|
|
869
897
|
if (respTypeId !== RequestOk.id) {
|
|
870
898
|
throw new Error(`PublishNamespace rejected: typeId=0x${respTypeId.toString(16)}`);
|
|
@@ -876,18 +904,79 @@ export class Publisher {
|
|
|
876
904
|
else {
|
|
877
905
|
await RequestOk.decode(stream.reader, this.#session.version);
|
|
878
906
|
}
|
|
907
|
+
return "held";
|
|
879
908
|
})(), ADVERTISE_TIMEOUT_MS, `advertisement timed out after ${ADVERTISE_TIMEOUT_MS}ms waiting for the peer's answer`);
|
|
880
|
-
|
|
881
|
-
|
|
909
|
+
if (answer === "held")
|
|
910
|
+
requests.set(path, { path, requestId, stream });
|
|
911
|
+
return answer;
|
|
882
912
|
}
|
|
883
913
|
catch (err) {
|
|
884
914
|
// The peer never answered the advertisement: a control timeout, not late content.
|
|
885
915
|
const e = err instanceof TimeoutError ? controlTimeout(err) : error(err);
|
|
886
916
|
console.warn(`announce failed: broadcast=${path} error=${reason(e)}`);
|
|
887
917
|
request?.abort(e);
|
|
888
|
-
return
|
|
918
|
+
return "dropped";
|
|
889
919
|
}
|
|
890
920
|
}
|
|
921
|
+
/**
|
|
922
|
+
* Update a namespace the peer holds: REQUEST_UPDATE on the request that carries it,
|
|
923
|
+
* with only the parameters that changed, then its answer.
|
|
924
|
+
*
|
|
925
|
+
* Waiting for the answer keeps one update outstanding per stream, which satisfies any
|
|
926
|
+
* MAX_REQUEST_UPDATES the peer set without reading it; a change landing meanwhile waits
|
|
927
|
+
* for the next turn. The answer decides whether the peer still holds the namespace: a
|
|
928
|
+
* REQUEST_ERROR closes the peer's side and withdraws the advertisement, so ours is
|
|
929
|
+
* finished too, and an unanswered update is dropped abruptly, since a peer that ignored
|
|
930
|
+
* it cannot be assumed to hold either price. Either way the retry re-offers it fresh.
|
|
931
|
+
*
|
|
932
|
+
* A different original publisher is an update like any other: the receiver drains what
|
|
933
|
+
* it already serves from the old one and never splices the two.
|
|
934
|
+
*/
|
|
935
|
+
async #update(path, requests, { from, to }) {
|
|
936
|
+
const request = requests.get(path);
|
|
937
|
+
if (!request)
|
|
938
|
+
return "dropped";
|
|
939
|
+
const update = {
|
|
940
|
+
hops: sameHops(from.hops, to.hops) ? undefined : to.hops,
|
|
941
|
+
cost: from.cost === to.cost ? undefined : to.cost,
|
|
942
|
+
};
|
|
943
|
+
const version = this.#session.version;
|
|
944
|
+
let answer;
|
|
945
|
+
try {
|
|
946
|
+
const requestId = await this.#session.nextRequestId();
|
|
947
|
+
if (requestId === undefined)
|
|
948
|
+
throw new Error("no request ID for the update");
|
|
949
|
+
answer = await withTimeout((async () => {
|
|
950
|
+
const { writer, reader } = request.stream;
|
|
951
|
+
await writer.u53(PublishNamespaceUpdate.id);
|
|
952
|
+
await new PublishNamespaceUpdate({ requestId, update }).encode(writer, version);
|
|
953
|
+
const typeId = await reader.u53();
|
|
954
|
+
if (typeId === RequestError.id) {
|
|
955
|
+
const err = await RequestError.decode(reader, version);
|
|
956
|
+
const why = requestReason(err.errorCode, err.reasonPhrase, "publish_namespace", version);
|
|
957
|
+
console.warn(`publish_namespace update refused: broadcast=${path} error=${why}`);
|
|
958
|
+
return this.#refusal(err);
|
|
959
|
+
}
|
|
960
|
+
if (typeId !== RequestOk.id) {
|
|
961
|
+
throw new Error(`unexpected answer to the update: typeId=0x${typeId.toString(16)}`);
|
|
962
|
+
}
|
|
963
|
+
await RequestOk.decode(reader, version);
|
|
964
|
+
return "held";
|
|
965
|
+
})(), ADVERTISE_TIMEOUT_MS, `update timed out after ${ADVERTISE_TIMEOUT_MS}ms waiting for the peer's answer`);
|
|
966
|
+
}
|
|
967
|
+
catch (err) {
|
|
968
|
+
const e = err instanceof TimeoutError ? controlTimeout(err) : error(err);
|
|
969
|
+
console.warn(`publish_namespace update failed: broadcast=${path} error=${reason(e)}`);
|
|
970
|
+
requests.delete(path);
|
|
971
|
+
request.stream.abort(e);
|
|
972
|
+
return "dropped";
|
|
973
|
+
}
|
|
974
|
+
// The peer already closed out a refused request, and its STOP_SENDING can reject the
|
|
975
|
+
// wait on our FIN. That is the withdrawal finishing, not a reason to end the loop.
|
|
976
|
+
if (answer !== "held")
|
|
977
|
+
await this.#withdraw(path, requests).catch(() => undefined);
|
|
978
|
+
return answer;
|
|
979
|
+
}
|
|
891
980
|
/**
|
|
892
981
|
* Close out a namespace's PUBLISH_NAMESPACE request with PUBLISH_NAMESPACE_DONE.
|
|
893
982
|
*/
|
|
@@ -925,23 +1014,49 @@ export class Publisher {
|
|
|
925
1014
|
* @internal
|
|
926
1015
|
*/
|
|
927
1016
|
async runTrackStatusRequest(msg, stream) {
|
|
1017
|
+
// TRACK_STATUS_ERROR is 0x0f on draft-14.
|
|
1018
|
+
await this.#refuseUnsupported(stream, msg.requestId, "track_status", 0x0f, "TRACK_STATUS is not supported");
|
|
1019
|
+
}
|
|
1020
|
+
/**
|
|
1021
|
+
* Refuses an incoming SUBSCRIBE_TRACKS (draft-18+): we never send the PUBLISH it asks for.
|
|
1022
|
+
* The reply carries no Request ID, so the message body is left unread.
|
|
1023
|
+
*
|
|
1024
|
+
* @internal
|
|
1025
|
+
*/
|
|
1026
|
+
async runSubscribeTracks(stream) {
|
|
928
1027
|
const version = this.#session.version;
|
|
929
|
-
|
|
1028
|
+
await stream.writer.u53(RequestError.id);
|
|
1029
|
+
await new RequestError({
|
|
1030
|
+
errorCode: toRequestCode("not_supported", "subscribe_namespace", version),
|
|
1031
|
+
reasonPhrase: "SUBSCRIBE_TRACKS is not supported",
|
|
1032
|
+
}).encode(stream.writer, version);
|
|
1033
|
+
stream.close();
|
|
1034
|
+
}
|
|
1035
|
+
/**
|
|
1036
|
+
* Handles an incoming FETCH on a bidi stream. We serve none.
|
|
1037
|
+
*
|
|
1038
|
+
* @internal
|
|
1039
|
+
*/
|
|
1040
|
+
async runFetch(msg, stream) {
|
|
1041
|
+
await this.#refuseUnsupported(stream, msg.requestId, "fetch", FetchError.id, "FETCH is not supported");
|
|
1042
|
+
}
|
|
1043
|
+
/**
|
|
1044
|
+
* Refuse a request NOT_SUPPORTED. Draft-14 gives each request its own error message,
|
|
1045
|
+
* `errorId14`, with the SUBSCRIBE_ERROR body; later drafts use REQUEST_ERROR.
|
|
1046
|
+
*/
|
|
1047
|
+
async #refuseUnsupported(stream, requestId, kind, errorId14, reasonPhrase) {
|
|
1048
|
+
const version = this.#session.version;
|
|
1049
|
+
const errorCode = toRequestCode("not_supported", kind, version);
|
|
930
1050
|
if (version === Version.DRAFT_14) {
|
|
931
|
-
|
|
932
|
-
await stream.writer
|
|
933
|
-
await new SubscribeError({
|
|
934
|
-
requestId: msg.requestId,
|
|
935
|
-
errorCode,
|
|
936
|
-
reasonPhrase: "TRACK_STATUS is not supported",
|
|
937
|
-
}).encode(stream.writer, version);
|
|
1051
|
+
await stream.writer.u53(errorId14);
|
|
1052
|
+
await new SubscribeError({ requestId, errorCode, reasonPhrase }).encode(stream.writer, version);
|
|
938
1053
|
}
|
|
939
1054
|
else {
|
|
940
1055
|
await stream.writer.u53(RequestError.id);
|
|
941
1056
|
await new RequestError({
|
|
942
|
-
requestId: version === Version.DRAFT_15 || version === Version.DRAFT_16 ?
|
|
1057
|
+
requestId: version === Version.DRAFT_15 || version === Version.DRAFT_16 ? requestId : undefined,
|
|
943
1058
|
errorCode,
|
|
944
|
-
reasonPhrase
|
|
1059
|
+
reasonPhrase,
|
|
945
1060
|
}).encode(stream.writer, version);
|
|
946
1061
|
}
|
|
947
1062
|
stream.close();
|