@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/origin.js
CHANGED
|
@@ -15,16 +15,183 @@ import { Derived, getter, Once, Signal } from "@moq/signals";
|
|
|
15
15
|
import * as announce from "./announced.js";
|
|
16
16
|
import * as broadcast from "./broadcast.js";
|
|
17
17
|
import { StreamCode, StreamError } from "./error.js";
|
|
18
|
-
import { Route, routesEqual } from "./hop.js";
|
|
19
|
-
import { hooks, scopeCaptures, scopeOverlaps } from "./internal.js";
|
|
18
|
+
import { isAnonymous, Route, routesEqual } from "./hop.js";
|
|
19
|
+
import { hiddenBelow, hooks, scopeCaptures, scopeHead, scopeOverlaps } from "./internal.js";
|
|
20
20
|
import * as Path from "./path.js";
|
|
21
21
|
import { registerWire, wireOf } from "./wire.js";
|
|
22
22
|
export { isAnonymous } from "./hop.js";
|
|
23
|
+
/** The rooted permissions shared by a handle and every route it inserts. */
|
|
24
|
+
class Scope {
|
|
25
|
+
static all = new Scope(Path.empty());
|
|
26
|
+
root;
|
|
27
|
+
allowed;
|
|
28
|
+
constructor(root, allowed) {
|
|
29
|
+
this.root = root;
|
|
30
|
+
this.allowed = allowed;
|
|
31
|
+
}
|
|
32
|
+
narrow(root, patterns) {
|
|
33
|
+
const joined = Path.encode(Path.join(this.root, root));
|
|
34
|
+
const rooted = patterns.rooted(joined);
|
|
35
|
+
const allowed = this.allowed?.intersect(rooted) ?? rooted;
|
|
36
|
+
if (allowed.size === 0)
|
|
37
|
+
throw new Error("origin scopes do not overlap");
|
|
38
|
+
return new Scope(joined, allowed);
|
|
39
|
+
}
|
|
40
|
+
matches(path) {
|
|
41
|
+
return this.allowed?.matches(path) ?? true;
|
|
42
|
+
}
|
|
43
|
+
path(path) {
|
|
44
|
+
const joined = Path.encode(Path.join(this.root, path));
|
|
45
|
+
if (!this.matches(joined))
|
|
46
|
+
throw new Error("path is outside the origin scope");
|
|
47
|
+
return joined;
|
|
48
|
+
}
|
|
49
|
+
prefix(prefix) {
|
|
50
|
+
const joined = Path.encode(Path.join(this.root, prefix));
|
|
51
|
+
if (this.allowed && !this.allowed.overlaps(Path.Pattern.subtree(joined))) {
|
|
52
|
+
throw new Error("prefix is outside the origin scope");
|
|
53
|
+
}
|
|
54
|
+
return joined;
|
|
55
|
+
}
|
|
56
|
+
patterns(pattern = Path.Pattern.all()) {
|
|
57
|
+
const rooted = new Path.Patterns([pattern.rooted(this.root)]);
|
|
58
|
+
return this.allowed?.intersect(rooted) ?? rooted;
|
|
59
|
+
}
|
|
60
|
+
/** Omit nested heads because the outer subscription already carries their routes. */
|
|
61
|
+
heads() {
|
|
62
|
+
if (!this.allowed)
|
|
63
|
+
return [Path.empty()];
|
|
64
|
+
const heads = [...new Set([...this.allowed.rebase(this.root)].map(scopeHead))].sort();
|
|
65
|
+
return heads.filter((head) => !heads.some((other) => other !== head && Path.hasPrefix(other, head)));
|
|
66
|
+
}
|
|
67
|
+
/** The exact paths within this scope, relative to its root. */
|
|
68
|
+
projectPaths(values) {
|
|
69
|
+
if (!values || this === Scope.all)
|
|
70
|
+
return values;
|
|
71
|
+
const out = new Map();
|
|
72
|
+
for (const [path, value] of values) {
|
|
73
|
+
if (!this.matches(path))
|
|
74
|
+
continue;
|
|
75
|
+
const relative = Path.stripPrefix(this.root, path);
|
|
76
|
+
if (relative !== null)
|
|
77
|
+
out.set(relative, value);
|
|
78
|
+
}
|
|
79
|
+
return out;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* The advertisements that may serve this scope, relative to its root. Every prefix at or
|
|
83
|
+
* above the root presents as the empty path, most specific first, since that is the order
|
|
84
|
+
* a request beneath the root resolves in.
|
|
85
|
+
*/
|
|
86
|
+
projectRoutes(values) {
|
|
87
|
+
if (!values || this === Scope.all)
|
|
88
|
+
return values;
|
|
89
|
+
const out = new Map();
|
|
90
|
+
const covering = [];
|
|
91
|
+
const allowed = this.allowed && [...this.allowed];
|
|
92
|
+
for (const [path, candidates] of values) {
|
|
93
|
+
const relative = Path.stripPrefix(this.root, path);
|
|
94
|
+
const above = relative === null || relative === Path.empty();
|
|
95
|
+
if (above && !Path.hasPrefix(path, this.root))
|
|
96
|
+
continue;
|
|
97
|
+
const visible = candidates
|
|
98
|
+
.filter((value) => !allowed || allowed.some((pattern) => advertOverlaps(value, path, pattern)))
|
|
99
|
+
// The claim moves with the key, so it compares against root-relative requests.
|
|
100
|
+
.map((value) => (value.claim ? { ...value, claim: value.claim.rebase(this.root) } : value));
|
|
101
|
+
if (visible.length === 0)
|
|
102
|
+
continue;
|
|
103
|
+
if (!above) {
|
|
104
|
+
out.set(relative, visible);
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
// Hold the empty path's place in the order until every covering prefix is known.
|
|
108
|
+
if (covering.length === 0)
|
|
109
|
+
out.set(Path.empty(), []);
|
|
110
|
+
covering.push([path, visible]);
|
|
111
|
+
}
|
|
112
|
+
if (covering.length > 0) {
|
|
113
|
+
covering.sort(([a], [b]) => b.length - a.length);
|
|
114
|
+
out.set(Path.empty(), covering.flatMap(([, visible]) => visible));
|
|
115
|
+
}
|
|
116
|
+
return out;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/** Whether the route advertised at `prefix` may serve any path `pattern` admits. */
|
|
120
|
+
function advertOverlaps(advert, prefix, pattern) {
|
|
121
|
+
return advert.claim ? advert.claim.overlaps(pattern) : scopeOverlaps(pattern, prefix);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Presents advertised prefixes relative to a root. Every prefix at or above the root
|
|
125
|
+
* collapses to the empty path, where the most specific one wins, since that is the route
|
|
126
|
+
* a request beneath the root resolves through.
|
|
127
|
+
*/
|
|
128
|
+
class CoveringRoot {
|
|
129
|
+
#root;
|
|
130
|
+
#covering;
|
|
131
|
+
constructor(root) {
|
|
132
|
+
this.#root = root;
|
|
133
|
+
}
|
|
134
|
+
/** The presented path for `path`, or undefined when it is outside the root or a broader cover. */
|
|
135
|
+
relative(path) {
|
|
136
|
+
const relative = Path.stripPrefix(this.#root, path);
|
|
137
|
+
if (relative !== null && relative !== Path.empty())
|
|
138
|
+
return relative;
|
|
139
|
+
if (relative === null && !Path.hasPrefix(path, this.#root))
|
|
140
|
+
return undefined;
|
|
141
|
+
if (this.#covering !== undefined && !Path.hasPrefix(this.#covering, path))
|
|
142
|
+
return undefined;
|
|
143
|
+
this.#covering = path;
|
|
144
|
+
return Path.empty();
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
/** Orders advertisements at one prefix: the better route, then a local broadcast on a tie, then fewer hops. */
|
|
148
|
+
function compareCandidates(a, b) {
|
|
149
|
+
return (compareRoutes(a.route, b.route) ||
|
|
150
|
+
Number(b.exact) - Number(a.exact) ||
|
|
151
|
+
a.route.hops.length - b.route.hops.length);
|
|
152
|
+
}
|
|
153
|
+
/** Orders two routes by preference: identified before anonymous, then lower warm cost, then lower cold cost. */
|
|
154
|
+
function compareRoutes(a, b) {
|
|
155
|
+
const anonymous = Number(isAnonymous(a)) - Number(isAnonymous(b));
|
|
156
|
+
if (anonymous !== 0)
|
|
157
|
+
return anonymous;
|
|
158
|
+
if (a.cost.warm !== b.cost.warm)
|
|
159
|
+
return a.cost.warm < b.cost.warm ? -1 : 1;
|
|
160
|
+
if (a.cost.cold !== b.cost.cold)
|
|
161
|
+
return a.cost.cold < b.cost.cold ? -1 : 1;
|
|
162
|
+
return 0;
|
|
163
|
+
}
|
|
164
|
+
/** The preferred of `entries` (newest first) not skipped: the best route, then fewest hops, then newest. */
|
|
165
|
+
function preferredEntry(entries, skip) {
|
|
166
|
+
let best;
|
|
167
|
+
for (const entry of entries) {
|
|
168
|
+
if (skip?.(entry))
|
|
169
|
+
continue;
|
|
170
|
+
if (!best) {
|
|
171
|
+
best = entry;
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
const a = entry.route.peek();
|
|
175
|
+
const b = best.route.peek();
|
|
176
|
+
const order = compareRoutes(a, b) || a.hops.length - b.hops.length;
|
|
177
|
+
if (order < 0)
|
|
178
|
+
best = entry;
|
|
179
|
+
}
|
|
180
|
+
return best;
|
|
181
|
+
}
|
|
182
|
+
/** Whether a session received `entry`, so it is never forwarded to a peer. */
|
|
183
|
+
function received(entry) {
|
|
184
|
+
return !entry.originated;
|
|
185
|
+
}
|
|
23
186
|
function noCapacity() {
|
|
24
187
|
return new StreamError(StreamCode.NoCapacity, { message: "no capacity" });
|
|
25
188
|
}
|
|
26
189
|
/** A served route from {@link Producer.dynamic}: the queue a handler drains. */
|
|
27
190
|
class ServeState {
|
|
191
|
+
root;
|
|
192
|
+
constructor(root) {
|
|
193
|
+
this.root = root;
|
|
194
|
+
}
|
|
28
195
|
queue = new Signal([]);
|
|
29
196
|
pending = new Map();
|
|
30
197
|
served = new Map();
|
|
@@ -46,42 +213,44 @@ class ServeState {
|
|
|
46
213
|
const live = this.served.get(path);
|
|
47
214
|
if (live && live.closed.peek() === undefined)
|
|
48
215
|
return;
|
|
49
|
-
const request = makeRequest(path, this);
|
|
216
|
+
const request = makeRequest(Path.stripPrefix(this.root, path) ?? Path.empty(), this);
|
|
50
217
|
this.pending.set(path, request);
|
|
51
218
|
this.queue.mutate((queue) => {
|
|
52
219
|
queue.push(request);
|
|
53
220
|
});
|
|
54
221
|
}
|
|
55
222
|
accept(request, front) {
|
|
56
|
-
|
|
223
|
+
const path = Path.join(this.root, request.path);
|
|
224
|
+
if (this.closed.peek() !== undefined || this.pending.get(path) !== request) {
|
|
57
225
|
front.close();
|
|
58
226
|
return;
|
|
59
227
|
}
|
|
60
|
-
this.pending.delete(
|
|
61
|
-
const existing = this.served.get(
|
|
228
|
+
this.pending.delete(path);
|
|
229
|
+
const existing = this.served.get(path);
|
|
62
230
|
if (existing && existing.closed.peek() === undefined) {
|
|
63
231
|
front.close();
|
|
64
|
-
this.onChange(
|
|
232
|
+
this.onChange(path);
|
|
65
233
|
this.settled.update((n) => n + 1);
|
|
66
234
|
return;
|
|
67
235
|
}
|
|
68
|
-
this.served.set(
|
|
236
|
+
this.served.set(path, front);
|
|
69
237
|
void front.closed.then(() => {
|
|
70
|
-
if (this.served.get(
|
|
238
|
+
if (this.served.get(path) !== front)
|
|
71
239
|
return;
|
|
72
|
-
this.served.delete(
|
|
73
|
-
this.onChange(
|
|
240
|
+
this.served.delete(path);
|
|
241
|
+
this.onChange(path);
|
|
74
242
|
});
|
|
75
|
-
this.onChange(
|
|
243
|
+
this.onChange(path);
|
|
76
244
|
this.settled.update((n) => n + 1);
|
|
77
245
|
}
|
|
78
246
|
reject(request, err) {
|
|
79
|
-
|
|
247
|
+
const path = Path.join(this.root, request.path);
|
|
248
|
+
if (this.pending.get(path) !== request)
|
|
80
249
|
return;
|
|
81
|
-
this.pending.delete(
|
|
82
|
-
if (this.demanding.has(
|
|
83
|
-
this.rejected.set(
|
|
84
|
-
this.onReject(
|
|
250
|
+
this.pending.delete(path);
|
|
251
|
+
if (this.demanding.has(path))
|
|
252
|
+
this.rejected.set(path, err);
|
|
253
|
+
this.onReject(path, err);
|
|
85
254
|
this.settled.update((n) => n + 1);
|
|
86
255
|
}
|
|
87
256
|
close(abort) {
|
|
@@ -98,7 +267,7 @@ class ServeState {
|
|
|
98
267
|
finishRequest(request, err);
|
|
99
268
|
}
|
|
100
269
|
for (const [path, front] of this.served) {
|
|
101
|
-
front.close(
|
|
270
|
+
front.close();
|
|
102
271
|
this.onChange(path);
|
|
103
272
|
}
|
|
104
273
|
this.served.clear();
|
|
@@ -107,20 +276,86 @@ class ServeState {
|
|
|
107
276
|
this.settled.update((n) => n + 1);
|
|
108
277
|
}
|
|
109
278
|
}
|
|
279
|
+
/** A table mutation invalidates the shared route snapshot before its async notification. */
|
|
280
|
+
class VersionedSignal extends Signal {
|
|
281
|
+
version = 0;
|
|
282
|
+
set(value, notify) {
|
|
283
|
+
this.version++;
|
|
284
|
+
super.set(value, notify);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
110
287
|
/** Reactive backing state shared by origin producers and consumers. */
|
|
111
288
|
class OriginState {
|
|
112
289
|
// Both tables decouple the application producing into the origin from the
|
|
113
290
|
// connections serving or feeding it. Undefined once the origin closes, so late
|
|
114
291
|
// writes fail loudly.
|
|
115
292
|
//
|
|
116
|
-
//
|
|
117
|
-
//
|
|
118
|
-
//
|
|
119
|
-
//
|
|
120
|
-
//
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
293
|
+
// Created is what this endpoint publishes, keyed by exact path, announced or not.
|
|
294
|
+
// Local is the announced subset, with its route in advertisedLocal: a broadcast
|
|
295
|
+
// exists for nobody, here or at a peer, until it announces. Routes is the
|
|
296
|
+
// advertisement table: prefixes a dynamic handle or a received session covers,
|
|
297
|
+
// newest first. Local and routes stay separate so a session can never announce a
|
|
298
|
+
// received entry back to a peer, which is what makes an origin shared by both
|
|
299
|
+
// directions echo-free.
|
|
300
|
+
created = new Map();
|
|
301
|
+
local = new VersionedSignal(new Map());
|
|
302
|
+
advertisedLocal = new VersionedSignal(new Map());
|
|
303
|
+
routes = new VersionedSignal(new Map());
|
|
304
|
+
#snapshotVersion = "";
|
|
305
|
+
#snapshot = { candidates: new Map(), routes: new Map(), visible: new Map() };
|
|
306
|
+
/** The full route table is built once per mutation, regardless of observer count. */
|
|
307
|
+
available = new Derived([this.local, this.advertisedLocal, this.routes], () => this.snapshot().routes);
|
|
308
|
+
/** {@link available} without hidden routes, for unscoped readers that did not opt in. */
|
|
309
|
+
visible = new Derived([this.local, this.advertisedLocal, this.routes], () => this.snapshot().visible);
|
|
310
|
+
snapshot() {
|
|
311
|
+
const version = `${this.local.version}/${this.advertisedLocal.version}/${this.routes.version}`;
|
|
312
|
+
if (version === this.#snapshotVersion)
|
|
313
|
+
return this.#snapshot;
|
|
314
|
+
const candidates = this.candidates();
|
|
315
|
+
const available = new Map();
|
|
316
|
+
const visible = new Map();
|
|
317
|
+
for (const [path, [best]] of candidates) {
|
|
318
|
+
available.set(path, best.route);
|
|
319
|
+
if (!hiddenBelow(Path.empty(), path))
|
|
320
|
+
visible.set(path, best.route);
|
|
321
|
+
}
|
|
322
|
+
this.#snapshot = { candidates, routes: available, visible };
|
|
323
|
+
this.#snapshotVersion = version;
|
|
324
|
+
return this.#snapshot;
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* Every advertisement per prefix, most preferred first, without the `skip`ped entries.
|
|
328
|
+
* Readers select after filtering by their scope, so a cheaper route they cannot see
|
|
329
|
+
* never hides one they can.
|
|
330
|
+
*/
|
|
331
|
+
candidates(skip) {
|
|
332
|
+
const out = new Map();
|
|
333
|
+
for (const [path, entries] of this.routes.peek() ?? []) {
|
|
334
|
+
const list = [];
|
|
335
|
+
for (const entry of entries) {
|
|
336
|
+
if (skip?.(entry))
|
|
337
|
+
continue;
|
|
338
|
+
list.push({ identity: entry.identity, route: entry.route.peek(), claim: entry.claim, exact: false });
|
|
339
|
+
}
|
|
340
|
+
if (list.length > 0)
|
|
341
|
+
out.set(path, list);
|
|
342
|
+
}
|
|
343
|
+
const advertised = this.advertisedLocal.peek();
|
|
344
|
+
for (const [path, front] of this.local.peek() ?? []) {
|
|
345
|
+
const local = { identity: front, route: advertised?.get(path) ?? Route.default, exact: true };
|
|
346
|
+
const list = out.get(path);
|
|
347
|
+
if (list)
|
|
348
|
+
list.push(local);
|
|
349
|
+
else
|
|
350
|
+
out.set(path, [local]);
|
|
351
|
+
}
|
|
352
|
+
for (const list of out.values()) {
|
|
353
|
+
// Stable, so equal routes keep the table's newest-first order.
|
|
354
|
+
if (list.length > 1)
|
|
355
|
+
list.sort(compareCandidates);
|
|
356
|
+
}
|
|
357
|
+
return out;
|
|
358
|
+
}
|
|
124
359
|
// Originated advertisements sessions should forward: exact-path announces plus
|
|
125
360
|
// originated dynamics. Identity is the local front or the route entry, so a
|
|
126
361
|
// republish diffs as retract-then-announce and a re-price as another active.
|
|
@@ -155,56 +390,55 @@ class OriginState {
|
|
|
155
390
|
return;
|
|
156
391
|
slot.route.set(this.route(path, slot));
|
|
157
392
|
}
|
|
158
|
-
/**
|
|
159
|
-
|
|
393
|
+
/**
|
|
394
|
+
* `entry` refused `path` with `err`. A request still serving another source skips the
|
|
395
|
+
* refuser; one with nothing serving ends with `err`.
|
|
396
|
+
*/
|
|
397
|
+
refuse(path, entry, err) {
|
|
160
398
|
const slot = this.requests.peek()?.get(path);
|
|
161
399
|
if (!slot)
|
|
162
400
|
return;
|
|
163
|
-
|
|
164
|
-
|
|
401
|
+
// Only the route the request is waiting on speaks for it; a superseded one's answer is moot.
|
|
402
|
+
if (this.bestEntry(path, (candidate) => slot.refused.has(candidate)) !== entry)
|
|
403
|
+
return;
|
|
404
|
+
const serving = slot.route.peek();
|
|
405
|
+
if (serving && serving.closed.peek() === undefined) {
|
|
406
|
+
slot.refused.add(entry);
|
|
407
|
+
slot.route.set(this.route(path, slot));
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
this.requests.mutate((map) => {
|
|
411
|
+
if (map?.get(path) === slot)
|
|
412
|
+
map.delete(path);
|
|
413
|
+
});
|
|
414
|
+
slot.answer?.close();
|
|
415
|
+
slot.answer = undefined;
|
|
416
|
+
slot.route.set(undefined);
|
|
417
|
+
this.releaseMaterialized(path);
|
|
418
|
+
for (const closed of slot.handles)
|
|
419
|
+
closed.set(err);
|
|
420
|
+
slot.handles.clear();
|
|
165
421
|
}
|
|
166
422
|
/**
|
|
167
423
|
* Recompute every open request covered by `prefix`, after a route was inserted or
|
|
168
424
|
* removed there: a route covers many paths, so a single-path refresh is not enough.
|
|
169
|
-
*
|
|
170
|
-
* retracted route's session subscription
|
|
425
|
+
* Every materialized broadcast belongs to an open request, so rerouting them also
|
|
426
|
+
* releases a retracted route's session subscription even when nothing reads it again.
|
|
171
427
|
*/
|
|
172
428
|
refreshPrefix(prefix) {
|
|
173
|
-
const
|
|
174
|
-
for (const [path, cached] of [...this.materialized]) {
|
|
175
|
-
if (!Path.hasPrefix(prefix, path))
|
|
176
|
-
continue;
|
|
177
|
-
if (cached.entry !== this.bestEntry(path, requests?.get(path)?.refused)) {
|
|
178
|
-
this.materialized.delete(path);
|
|
179
|
-
cached.front.close();
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
for (const [path, slot] of requests ?? []) {
|
|
429
|
+
for (const [path, slot] of this.requests.peek() ?? []) {
|
|
183
430
|
if (Path.hasPrefix(prefix, path))
|
|
184
431
|
slot.route.set(this.route(path, slot));
|
|
185
432
|
}
|
|
186
433
|
}
|
|
187
434
|
/** Rebuild the publisher-facing originated table after an advertisement write. */
|
|
188
435
|
rebuildOriginated() {
|
|
189
|
-
|
|
190
|
-
const advertised = this.advertisedLocal.peek();
|
|
191
|
-
const routes = this.routes.peek();
|
|
192
|
-
if (!local && !advertised && !routes) {
|
|
436
|
+
if (!this.local.peek() && !this.advertisedLocal.peek() && !this.routes.peek()) {
|
|
193
437
|
this.originated.set(undefined);
|
|
194
438
|
return;
|
|
195
439
|
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
const front = local?.get(path);
|
|
199
|
-
if (front)
|
|
200
|
-
next.set(path, { identity: front, route });
|
|
201
|
-
}
|
|
202
|
-
for (const [prefix, entries] of routes ?? []) {
|
|
203
|
-
const mine = entries.find((entry) => entry.originated);
|
|
204
|
-
if (mine)
|
|
205
|
-
next.set(prefix, { identity: mine.identity, route: mine.route.peek() });
|
|
206
|
-
}
|
|
207
|
-
this.originated.set(next);
|
|
440
|
+
// A local broadcast and an originated dynamic at one path compete on cost, as they do for requests.
|
|
441
|
+
this.originated.set(this.candidates(received));
|
|
208
442
|
}
|
|
209
443
|
/**
|
|
210
444
|
* Release the materialized broadcast for `path`, once its last request is gone: the
|
|
@@ -218,16 +452,16 @@ class OriginState {
|
|
|
218
452
|
this.materialized.delete(path);
|
|
219
453
|
cached.front.close();
|
|
220
454
|
}
|
|
221
|
-
/** The
|
|
222
|
-
bestEntry(path,
|
|
455
|
+
/** The preferred entry on the most specific route covering `path`, ignoring skipped entries, if any. */
|
|
456
|
+
bestEntry(path, skip) {
|
|
223
457
|
let bestPrefix;
|
|
224
458
|
let best;
|
|
225
459
|
for (const [prefix, entries] of this.routes.peek() ?? []) {
|
|
226
|
-
const entry = entries.find((candidate) => !refused?.has(candidate));
|
|
227
|
-
if (!entry)
|
|
228
|
-
continue;
|
|
229
460
|
if (!Path.hasPrefix(prefix, path))
|
|
230
461
|
continue;
|
|
462
|
+
const entry = preferredEntry(entries, (candidate) => !candidate.scope.matches(path) || (skip?.(candidate) ?? false));
|
|
463
|
+
if (!entry)
|
|
464
|
+
continue;
|
|
231
465
|
if (bestPrefix === undefined || prefix.length > bestPrefix.length) {
|
|
232
466
|
bestPrefix = prefix;
|
|
233
467
|
best = entry;
|
|
@@ -236,37 +470,56 @@ class OriginState {
|
|
|
236
470
|
return best;
|
|
237
471
|
}
|
|
238
472
|
/**
|
|
239
|
-
*
|
|
240
|
-
*
|
|
473
|
+
* Whether the announced local broadcast at `path` wins over `entry`, the best route a
|
|
474
|
+
* session or dynamic handle announced there. Cost decides, as for any two routes: an
|
|
475
|
+
* identified route strictly cheaper than the local one wins, and the local broadcast
|
|
476
|
+
* wins a tie. A route at a shorter prefix never competes, since the most specific
|
|
477
|
+
* prefix wins outright. False when nothing is announced locally at `path`.
|
|
478
|
+
*/
|
|
479
|
+
localWins(path, entry) {
|
|
480
|
+
const local = this.advertisedLocal.peek()?.get(path);
|
|
481
|
+
if (!local || !this.local.peek()?.has(path))
|
|
482
|
+
return false;
|
|
483
|
+
if (!entry || !this.routes.peek()?.get(path)?.includes(entry))
|
|
484
|
+
return true;
|
|
485
|
+
return compareRoutes(local, entry.route.peek()) <= 0;
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* What `path` resolves to: an announced local publish, a broadcast materialized from
|
|
489
|
+
* the best covering route, or the blind answer.
|
|
241
490
|
*
|
|
242
491
|
* Materialization is lazy and cached per path: the first request under a route opens
|
|
243
|
-
* the providing session's subscription
|
|
244
|
-
*
|
|
492
|
+
* the providing session's subscription and repeats share it. A better route is made
|
|
493
|
+
* before the old one breaks: the current front keeps serving until the new route
|
|
494
|
+
* answers (then swaps) or refuses (then is skipped). A retracted route swaps at once.
|
|
245
495
|
*/
|
|
246
496
|
route(path, slot) {
|
|
497
|
+
const entry = this.bestEntry(path, (candidate) => slot.refused.has(candidate));
|
|
247
498
|
const local = this.local.peek()?.get(path);
|
|
248
|
-
if (local)
|
|
499
|
+
if (local && this.localWins(path, entry)) {
|
|
500
|
+
// Nothing reads a remote front the local broadcast replaced, so close its session subscription.
|
|
501
|
+
this.releaseMaterialized(path);
|
|
249
502
|
return local;
|
|
250
|
-
const entry = this.bestEntry(path, slot.refused);
|
|
251
|
-
const cached = this.materialized.get(path);
|
|
252
|
-
if (cached && cached.entry === entry) {
|
|
253
|
-
if (cached.front.closed.peek() === undefined)
|
|
254
|
-
return cached.front;
|
|
255
|
-
this.materialized.delete(path);
|
|
256
503
|
}
|
|
257
|
-
|
|
504
|
+
let cached = this.materialized.get(path);
|
|
505
|
+
if (cached && cached.front.closed.peek() !== undefined) {
|
|
258
506
|
this.materialized.delete(path);
|
|
259
|
-
cached
|
|
507
|
+
cached = undefined;
|
|
260
508
|
}
|
|
261
|
-
if (
|
|
509
|
+
if (cached && cached.entry === entry)
|
|
510
|
+
return cached.front;
|
|
511
|
+
if (!entry?.server) {
|
|
512
|
+
this.releaseMaterialized(path);
|
|
262
513
|
return slot.answer;
|
|
514
|
+
}
|
|
263
515
|
const served = entry.server.served.get(path);
|
|
264
516
|
if (served && served.closed.peek() === undefined) {
|
|
517
|
+
cached?.front.close();
|
|
265
518
|
this.materialized.set(path, { entry, front: served });
|
|
266
519
|
return served;
|
|
267
520
|
}
|
|
268
521
|
entry.server.enqueue(path);
|
|
269
|
-
return
|
|
522
|
+
return cached?.front;
|
|
270
523
|
}
|
|
271
524
|
}
|
|
272
525
|
/**
|
|
@@ -280,30 +533,47 @@ class OriginState {
|
|
|
280
533
|
*
|
|
281
534
|
* Create, attach {@link dynamic} for tracks served on demand, populate, then
|
|
282
535
|
* {@link broadcast.Producer.announce}: an exact-path subscribe before the tracks exist is
|
|
283
|
-
* refused, and
|
|
536
|
+
* refused, and nobody can see or reach a broadcast until it announces.
|
|
284
537
|
*
|
|
285
538
|
* @public
|
|
286
539
|
*/
|
|
287
540
|
export class Producer {
|
|
288
541
|
#state = new OriginState();
|
|
542
|
+
#scope = Scope.all;
|
|
543
|
+
#requests;
|
|
289
544
|
// The reader backing the passthroughs, so holding a Producer never requires the
|
|
290
545
|
// consume().x() stutter for everyday reads. One instance, so `discovery` keeps its
|
|
291
546
|
// identity across reads.
|
|
292
|
-
#reader = makeConsumer(this.#state);
|
|
547
|
+
#reader = makeConsumer(this.#state, this.#scope);
|
|
293
548
|
constructor() {
|
|
294
549
|
const thisProducer = this;
|
|
295
550
|
registerWire(this, {
|
|
296
551
|
receive: (prefix, route) => this.#receive(prefix, route),
|
|
552
|
+
interests: () => this.#scope.heads(),
|
|
553
|
+
accepts: (prefix) => !this.#scope.allowed ||
|
|
554
|
+
this.#scope.allowed.overlaps(Path.Pattern.subtree(Path.join(this.#scope.root, prefix))),
|
|
297
555
|
attach: (discovery) => this.#attach(discovery),
|
|
298
556
|
expect: () => this.#expect(),
|
|
299
557
|
get requests() {
|
|
300
|
-
|
|
558
|
+
if (thisProducer.#scope === Scope.all)
|
|
559
|
+
return thisProducer.#state.requests;
|
|
560
|
+
thisProducer.#requests ??= new Derived([thisProducer.#state.requests], (requests) => thisProducer.#scope.projectPaths(requests));
|
|
561
|
+
return thisProducer.#requests;
|
|
301
562
|
},
|
|
302
563
|
changed: () => this.#changed(),
|
|
303
|
-
answer: (path, front) => this.#answer(path, front),
|
|
564
|
+
answer: (path, front) => this.#answer(this.#scope.path(path), front),
|
|
304
565
|
routes: (path) => wireOf(this.#reader).routes(path),
|
|
305
566
|
});
|
|
306
567
|
}
|
|
568
|
+
/** Narrow this handle to patterns beneath root, presenting paths relative to that root. */
|
|
569
|
+
scope(root, patterns) {
|
|
570
|
+
const scope = this.#scope.narrow(root, patterns);
|
|
571
|
+
const producer = new Producer();
|
|
572
|
+
producer.#state = this.#state;
|
|
573
|
+
producer.#scope = scope;
|
|
574
|
+
producer.#reader = makeConsumer(this.#state, scope);
|
|
575
|
+
return producer;
|
|
576
|
+
}
|
|
307
577
|
/**
|
|
308
578
|
* Settles once the origin closes: `null` on a clean close, or the abort {@link Error}.
|
|
309
579
|
* Peek it synchronously (`undefined` while open), observe it reactively, or `await` it.
|
|
@@ -314,60 +584,65 @@ export class Producer {
|
|
|
314
584
|
/**
|
|
315
585
|
* Create a broadcast at `path`, returning its producer.
|
|
316
586
|
*
|
|
317
|
-
* The broadcast
|
|
318
|
-
*
|
|
319
|
-
*
|
|
587
|
+
* The broadcast exists for nobody until {@link broadcast.Producer.announce}: announce
|
|
588
|
+
* streams skip it and requests for its path find nothing, on this origin exactly as at
|
|
589
|
+
* a peer. Announce once its tracks exist; {@link broadcast.Producer.unannounce}
|
|
590
|
+
* withdraws it from everyone again.
|
|
320
591
|
*
|
|
321
592
|
* Close the producer to drop it. Creating a path again supersedes the previous
|
|
322
593
|
* broadcast: the origin drops its handle on the old one, which closes it unless the
|
|
323
|
-
* application still holds a consumer clone.
|
|
324
|
-
* remote
|
|
594
|
+
* application still holds a consumer clone. An announced local broadcast competes with
|
|
595
|
+
* a remote route at the same path on cost, winning ties.
|
|
325
596
|
*/
|
|
326
597
|
createBroadcast(path) {
|
|
598
|
+
path = this.#scope.path(path);
|
|
599
|
+
const created = this.#state.created;
|
|
600
|
+
if (!created)
|
|
601
|
+
throw new Error("origin is closed");
|
|
327
602
|
const producer = new broadcast.Producer();
|
|
603
|
+
hooks.stampPath(producer, path);
|
|
328
604
|
const front = producer.consume();
|
|
329
605
|
hooks.attachAnnouncer(producer, {
|
|
330
606
|
announce: (route) => this.#advertiseExact(path, front, route),
|
|
331
607
|
unannounce: () => this.#retractExact(path, front),
|
|
332
608
|
});
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
}
|
|
339
|
-
this.#state.advertisedLocal.mutate((advertised) => {
|
|
340
|
-
advertised?.delete(path);
|
|
341
|
-
});
|
|
342
|
-
this.#state.rebuildOriginated();
|
|
343
|
-
this.#state.refresh(path);
|
|
609
|
+
const previous = created.get(path);
|
|
610
|
+
created.set(path, front);
|
|
611
|
+
if (previous) {
|
|
612
|
+
this.#retractExact(path, previous);
|
|
613
|
+
previous.close();
|
|
614
|
+
}
|
|
344
615
|
// Drop it when the broadcast closes, unless a recreate already replaced it: a
|
|
345
616
|
// stale broadcast closing must not unpublish the live one.
|
|
346
617
|
void front.closed.then(() => {
|
|
347
618
|
this.#retractExact(path, front);
|
|
348
|
-
this.#state.
|
|
349
|
-
|
|
350
|
-
broadcasts.delete(path);
|
|
351
|
-
});
|
|
352
|
-
this.#state.refresh(path);
|
|
619
|
+
if (this.#state.created?.get(path) === front)
|
|
620
|
+
this.#state.created.delete(path);
|
|
353
621
|
});
|
|
354
622
|
return producer;
|
|
355
623
|
}
|
|
356
624
|
#advertiseExact(path, front, route) {
|
|
625
|
+
if (!this.#state.local.peek())
|
|
626
|
+
throw new Error("origin is closed");
|
|
627
|
+
if (this.#state.created?.get(path) !== front)
|
|
628
|
+
throw new Error("broadcast is closed");
|
|
629
|
+
// Both maps move together, so every reader sees the broadcast and its route at once.
|
|
630
|
+
this.#state.local.mutate((broadcasts) => {
|
|
631
|
+
broadcasts?.set(path, front);
|
|
632
|
+
});
|
|
357
633
|
this.#state.advertisedLocal.mutate((advertised) => {
|
|
358
|
-
|
|
359
|
-
throw new Error("origin is closed");
|
|
360
|
-
if (this.#state.local.peek()?.get(path) !== front)
|
|
361
|
-
throw new Error("broadcast is closed");
|
|
362
|
-
advertised.set(path, route);
|
|
634
|
+
advertised?.set(path, route);
|
|
363
635
|
});
|
|
364
636
|
this.#state.rebuildOriginated();
|
|
365
637
|
this.#state.refresh(path);
|
|
366
638
|
}
|
|
367
639
|
#retractExact(path, front) {
|
|
640
|
+
if (this.#state.local.peek()?.get(path) !== front)
|
|
641
|
+
return;
|
|
642
|
+
this.#state.local.mutate((broadcasts) => {
|
|
643
|
+
broadcasts?.delete(path);
|
|
644
|
+
});
|
|
368
645
|
this.#state.advertisedLocal.mutate((advertised) => {
|
|
369
|
-
if (this.#state.local.peek()?.get(path) !== front)
|
|
370
|
-
return;
|
|
371
646
|
advertised?.delete(path);
|
|
372
647
|
});
|
|
373
648
|
this.#state.rebuildOriginated();
|
|
@@ -382,7 +657,7 @@ export class Producer {
|
|
|
382
657
|
* consumers narrow with a {@link Path.Pattern} locally. The advertisement is
|
|
383
658
|
* visible to {@link Consumer.announced} and forwarded by sessions for as long as
|
|
384
659
|
* the returned {@link Dynamic} lives. A consumer resolving a path under it that
|
|
385
|
-
* no local broadcast
|
|
660
|
+
* no announced local broadcast wins is handed to the handle as a {@link Request}.
|
|
386
661
|
*/
|
|
387
662
|
dynamic(prefix, route = Route.default) {
|
|
388
663
|
return this.#insertRoute(prefix, Route.normalize(route), true);
|
|
@@ -397,15 +672,18 @@ export class Producer {
|
|
|
397
672
|
return this.#insertRoute(prefix, Route.normalize(route), false);
|
|
398
673
|
}
|
|
399
674
|
#insertRoute(prefix, route, originated) {
|
|
400
|
-
|
|
675
|
+
prefix = this.#scope.prefix(prefix);
|
|
676
|
+
const server = new ServeState(this.#scope.root);
|
|
401
677
|
server.onChange = (path) => this.#state.refresh(path);
|
|
402
678
|
const entry = {
|
|
403
679
|
identity: {},
|
|
680
|
+
scope: this.#scope,
|
|
681
|
+
claim: this.#scope.allowed?.intersect(new Path.Patterns([Path.Pattern.subtree(prefix)])),
|
|
404
682
|
route: new Signal(route),
|
|
405
683
|
originated,
|
|
406
684
|
server,
|
|
407
685
|
};
|
|
408
|
-
server.onReject = (path) => this.#state.refuse(path, entry);
|
|
686
|
+
server.onReject = (path, err) => this.#state.refuse(path, entry, err);
|
|
409
687
|
let closed = false;
|
|
410
688
|
this.#state.routes.mutate((routes) => {
|
|
411
689
|
if (!routes) {
|
|
@@ -534,7 +812,7 @@ export class Producer {
|
|
|
534
812
|
}
|
|
535
813
|
/** A read handle for this origin, the side a connection's `publish` option borrows. */
|
|
536
814
|
consume() {
|
|
537
|
-
return makeConsumer(this.#state);
|
|
815
|
+
return makeConsumer(this.#state, this.#scope);
|
|
538
816
|
}
|
|
539
817
|
/** Whether every attached session announces into the table; see {@link Consumer.discovery}. */
|
|
540
818
|
get discovery() {
|
|
@@ -544,21 +822,24 @@ export class Producer {
|
|
|
544
822
|
request(path, options) {
|
|
545
823
|
return this.#reader.request(path, options);
|
|
546
824
|
}
|
|
825
|
+
/** The available announcements under `scope`, as a live map; see {@link Consumer.broadcasts}. */
|
|
826
|
+
broadcasts(scope, options) {
|
|
827
|
+
return this.#reader.broadcasts(scope, options);
|
|
828
|
+
}
|
|
547
829
|
/** The available broadcasts under `scope`, as a live stream; see {@link Consumer.announced}. */
|
|
548
|
-
announced(scope) {
|
|
549
|
-
return this.#reader.announced(scope);
|
|
830
|
+
announced(scope, options) {
|
|
831
|
+
return this.#reader.announced(scope, options);
|
|
550
832
|
}
|
|
551
833
|
/** Close the origin, every broadcast it still routes, and its announcement streams. Idempotent. */
|
|
552
834
|
close(abort) {
|
|
553
835
|
if (this.#state.closed.peek() !== undefined)
|
|
554
836
|
return;
|
|
555
837
|
this.#state.closed.set(abort ?? null);
|
|
556
|
-
this.#state.
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
});
|
|
838
|
+
for (const front of this.#state.created?.values() ?? []) {
|
|
839
|
+
front.close();
|
|
840
|
+
}
|
|
841
|
+
this.#state.created = undefined;
|
|
842
|
+
this.#state.local.update(() => undefined);
|
|
562
843
|
this.#state.advertisedLocal.update(() => undefined);
|
|
563
844
|
this.#state.routes.update((routes) => {
|
|
564
845
|
for (const entries of routes?.values() ?? []) {
|
|
@@ -626,25 +907,32 @@ export class Requesting {
|
|
|
626
907
|
* set, and false while a connection is still coming up, so the ordinary page-load window
|
|
627
908
|
* before the first handshake reads as pending rather than as a missing broadcast. Waiting
|
|
628
909
|
* on this is futile by definition; wait for an announcement instead, via the origin's
|
|
629
|
-
* `announced`.
|
|
910
|
+
* `announced`. True once the request is refused.
|
|
630
911
|
*/
|
|
631
912
|
unroutable;
|
|
913
|
+
/**
|
|
914
|
+
* Settles with the error a route's handler refused the path with, or `null` once you
|
|
915
|
+
* {@link close} the request. A refusal is final: no other route is asked, and a fresh
|
|
916
|
+
* request is needed to try again.
|
|
917
|
+
*/
|
|
918
|
+
closed;
|
|
632
919
|
#dispose;
|
|
633
|
-
#
|
|
634
|
-
constructor(path, active, unroutable, dispose) {
|
|
920
|
+
#disposed = false;
|
|
921
|
+
constructor(path, active, unroutable, closed, dispose) {
|
|
635
922
|
this.path = path;
|
|
636
923
|
this.active = active;
|
|
637
924
|
this.unroutable = unroutable;
|
|
925
|
+
this.closed = closed;
|
|
638
926
|
this.#dispose = dispose;
|
|
639
927
|
}
|
|
640
928
|
static {
|
|
641
|
-
makeRequesting = (path, active, unroutable, dispose) => new Requesting(path, active, unroutable, dispose);
|
|
929
|
+
makeRequesting = (path, active, unroutable, closed, dispose) => new Requesting(path, active, unroutable, closed, dispose);
|
|
642
930
|
}
|
|
643
931
|
/** Withdraw the request. The path stays routed for any other open request. Idempotent. */
|
|
644
932
|
close() {
|
|
645
|
-
if (this.#
|
|
933
|
+
if (this.#disposed)
|
|
646
934
|
return;
|
|
647
|
-
this.#
|
|
935
|
+
this.#disposed = true;
|
|
648
936
|
this.#dispose();
|
|
649
937
|
}
|
|
650
938
|
}
|
|
@@ -659,25 +947,26 @@ export class Requesting {
|
|
|
659
947
|
*/
|
|
660
948
|
export class Consumer {
|
|
661
949
|
#state;
|
|
662
|
-
|
|
950
|
+
#scope;
|
|
951
|
+
constructor(state, scope) {
|
|
663
952
|
this.#state = state;
|
|
953
|
+
this.#scope = scope;
|
|
664
954
|
// True only when every attached session announces. One session that cannot means the
|
|
665
955
|
// table is an incomplete picture, so a consumer gated on it has to keep its blind
|
|
666
956
|
// fallback: the paths only that session carries never reach the table at all.
|
|
667
957
|
this.#discovery = new Derived([state.sessions], ({ total, discovery }) => total === 0 ? undefined : discovery === total);
|
|
668
958
|
registerWire(this, {
|
|
669
|
-
routes: (path) => this.#routes(path),
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
demand: (path) => this.#demand(path),
|
|
959
|
+
routes: (path) => this.#routes(scope.path(path)),
|
|
960
|
+
broadcasts: scope === Scope.all ? state.local : new Derived([state.local], (local) => scope.projectPaths(local)),
|
|
961
|
+
advertised: scope === Scope.all
|
|
962
|
+
? state.originated
|
|
963
|
+
: new Derived([state.originated], (routes) => scope.projectRoutes(routes)),
|
|
964
|
+
local: (path) => this.#local(scope.path(path)),
|
|
965
|
+
demand: (path) => this.#demand(scope.path(path)),
|
|
677
966
|
});
|
|
678
967
|
}
|
|
679
968
|
static {
|
|
680
|
-
makeConsumer = (state) => new Consumer(state);
|
|
969
|
+
makeConsumer = (state, scope) => new Consumer(state, scope);
|
|
681
970
|
}
|
|
682
971
|
/** Settles once the origin closes; see {@link Producer.closed}. */
|
|
683
972
|
get closed() {
|
|
@@ -699,8 +988,8 @@ export class Consumer {
|
|
|
699
988
|
// counts, avoiding a Computed's lifecycle.
|
|
700
989
|
#discovery;
|
|
701
990
|
/**
|
|
702
|
-
* Whether the table routes `path`, by
|
|
703
|
-
* covering it.
|
|
991
|
+
* Whether the table routes `path`, by an announced local publish or an announced
|
|
992
|
+
* route covering it.
|
|
704
993
|
*
|
|
705
994
|
* Availability, not a handle: {@link request} is the only way to consume by path. A
|
|
706
995
|
* request on a routed path resolves to that route and never to a blind answer, which is
|
|
@@ -717,9 +1006,10 @@ export class Consumer {
|
|
|
717
1006
|
* Resolve `path`, optionally waiting for an announcement.
|
|
718
1007
|
*
|
|
719
1008
|
* The one way to consume by path. {@link Requesting.active} follows whatever the table
|
|
720
|
-
* routes (
|
|
721
|
-
* republish); when nothing does, the request stands and whichever
|
|
722
|
-
* answers first provides a blind subscription instead, re-answered
|
|
1009
|
+
* routes (an announced local publish, or any feeding session's announcement, swapping
|
|
1010
|
+
* on a republish or a retraction); when nothing does, the request stands and whichever
|
|
1011
|
+
* attached session answers first provides a blind subscription instead, re-answered
|
|
1012
|
+
* across reconnects.
|
|
723
1013
|
* With `announced: true`, an unrouted request waits while discovery is supported and
|
|
724
1014
|
* falls back to that blind behavior only when discovery is unavailable. Close the request
|
|
725
1015
|
* when done. On a closed origin it never resolves.
|
|
@@ -734,10 +1024,13 @@ export class Consumer {
|
|
|
734
1024
|
* feeds from more than one connection.
|
|
735
1025
|
*/
|
|
736
1026
|
request(path, options = {}) {
|
|
1027
|
+
const relative = path;
|
|
1028
|
+
path = this.#scope.path(path);
|
|
737
1029
|
const requests = this.#state.requests.peek();
|
|
738
1030
|
if (!requests) {
|
|
739
1031
|
// Closed origin: a request that can never resolve, and says so.
|
|
740
|
-
|
|
1032
|
+
const closed = new Once();
|
|
1033
|
+
return makeRequesting(relative, new Signal(undefined), getter(true), closed, () => closed.set(null));
|
|
741
1034
|
}
|
|
742
1035
|
let slot = requests.get(path);
|
|
743
1036
|
if (!slot) {
|
|
@@ -748,8 +1041,8 @@ export class Consumer {
|
|
|
748
1041
|
// notify nobody.
|
|
749
1042
|
const refused = new Set();
|
|
750
1043
|
const created = {
|
|
751
|
-
count: 0,
|
|
752
1044
|
blind: 0,
|
|
1045
|
+
handles: new Set(),
|
|
753
1046
|
refused,
|
|
754
1047
|
route: new Signal(this.#state.route(path, { refused })),
|
|
755
1048
|
};
|
|
@@ -758,7 +1051,8 @@ export class Consumer {
|
|
|
758
1051
|
map?.set(path, created);
|
|
759
1052
|
});
|
|
760
1053
|
}
|
|
761
|
-
|
|
1054
|
+
const closed = new Once();
|
|
1055
|
+
slot.handles.add(closed);
|
|
762
1056
|
let blind = !options.announced || this.#discovery.peek() === false;
|
|
763
1057
|
if (blind)
|
|
764
1058
|
slot.blind += 1;
|
|
@@ -794,6 +1088,8 @@ export class Consumer {
|
|
|
794
1088
|
const previous = handle;
|
|
795
1089
|
source = front;
|
|
796
1090
|
handle = front?.clone();
|
|
1091
|
+
if (handle)
|
|
1092
|
+
hooks.stampPath(handle, relative);
|
|
797
1093
|
previous?.close();
|
|
798
1094
|
}
|
|
799
1095
|
return handle;
|
|
@@ -808,8 +1104,8 @@ export class Consumer {
|
|
|
808
1104
|
// Only meaningful while nothing is routed, so it reads the route rather than `active`:
|
|
809
1105
|
// the two cannot disagree, since a routed path always has an answerer-independent
|
|
810
1106
|
// answer.
|
|
811
|
-
const unroutable = new Derived([route, this.#state.answerers], (front, answerers) => !front && answerers === 0);
|
|
812
|
-
return makeRequesting(
|
|
1107
|
+
const unroutable = new Derived([route, this.#state.answerers, closed], (front, answerers, ended) => ended !== undefined || (!front && answerers === 0));
|
|
1108
|
+
return makeRequesting(relative, active, unroutable, closed, () => {
|
|
813
1109
|
// Releases this request's handle; the route itself belongs to the table.
|
|
814
1110
|
released = true;
|
|
815
1111
|
unsubscribeDiscovery();
|
|
@@ -817,21 +1113,25 @@ export class Consumer {
|
|
|
817
1113
|
handle?.close();
|
|
818
1114
|
handle = undefined;
|
|
819
1115
|
source = undefined;
|
|
820
|
-
taken.
|
|
1116
|
+
taken.handles.delete(closed);
|
|
1117
|
+
if (closed.peek() === undefined)
|
|
1118
|
+
closed.set(null);
|
|
821
1119
|
if (blind)
|
|
822
1120
|
taken.blind -= 1;
|
|
823
1121
|
this.#state.requests.mutate(() => { });
|
|
824
|
-
if (taken.
|
|
1122
|
+
if (taken.handles.size > 0)
|
|
825
1123
|
return;
|
|
826
1124
|
// Defer the teardown a microtask: an effect whose rerun was triggered by the
|
|
827
1125
|
// answer resolving closes its old request and takes a new one in the same tick,
|
|
828
1126
|
// and tearing down in between would drop the answer it is about to read.
|
|
829
1127
|
queueMicrotask(() => {
|
|
830
|
-
if (taken.
|
|
1128
|
+
if (taken.handles.size > 0)
|
|
1129
|
+
return;
|
|
1130
|
+
// A refused slot already tore itself down, and the path may hold a newer one.
|
|
1131
|
+
if (this.#state.requests.peek()?.get(path) !== taken)
|
|
831
1132
|
return;
|
|
832
1133
|
this.#state.requests.mutate((map) => {
|
|
833
|
-
|
|
834
|
-
map.delete(path);
|
|
1134
|
+
map?.delete(path);
|
|
835
1135
|
});
|
|
836
1136
|
taken.answer?.close();
|
|
837
1137
|
taken.answer = undefined;
|
|
@@ -840,20 +1140,71 @@ export class Consumer {
|
|
|
840
1140
|
});
|
|
841
1141
|
});
|
|
842
1142
|
}
|
|
1143
|
+
/**
|
|
1144
|
+
* The announced routes matching `scope`, as a live map from covered prefix to route.
|
|
1145
|
+
* Local broadcasts appear once announced; received and dynamic routes retain their
|
|
1146
|
+
* advertised prefixes. Reads are synchronous, and the getter needs no teardown.
|
|
1147
|
+
* Hidden routes are left out unless `options.hidden` opts in (see {@link announce.Options}).
|
|
1148
|
+
* Unscoped readers share one snapshot; each distinct scope filters the table on changes.
|
|
1149
|
+
*/
|
|
1150
|
+
broadcasts(scope, options) {
|
|
1151
|
+
const hidden = options?.hidden ?? false;
|
|
1152
|
+
if (!scope && this.#scope === Scope.all)
|
|
1153
|
+
return hidden ? this.#state.available : this.#state.visible;
|
|
1154
|
+
const patterns = this.#scope.patterns(scope);
|
|
1155
|
+
return new Derived([this.#state.available], () => {
|
|
1156
|
+
const routes = new Map();
|
|
1157
|
+
for (const [path, entry] of this.#listed(patterns, hidden))
|
|
1158
|
+
routes.set(path, entry.route);
|
|
1159
|
+
return routes;
|
|
1160
|
+
});
|
|
1161
|
+
}
|
|
843
1162
|
/**
|
|
844
1163
|
* The announced routes matching `scope`, as a live stream: every currently advertised
|
|
845
1164
|
* route arrives first as active, then additions and retractions as they happen.
|
|
846
|
-
* Any pattern is accepted. A local broadcast appears
|
|
847
|
-
*
|
|
848
|
-
*
|
|
849
|
-
*
|
|
1165
|
+
* Any pattern is accepted. A local broadcast appears once it announces, exactly as a
|
|
1166
|
+
* peer sees it. A dynamic or received route announces the prefix it covers when its
|
|
1167
|
+
* subtree overlaps the scope. The stream ends when the origin closes or the consumer is
|
|
1168
|
+
* closed. Hidden routes are left out unless `options.hidden` opts in (see {@link announce.Options}).
|
|
850
1169
|
*/
|
|
851
|
-
announced(scope = Path.Pattern.all()) {
|
|
1170
|
+
announced(scope = Path.Pattern.all(), options) {
|
|
852
1171
|
const producer = new announce.Producer();
|
|
853
|
-
void this.#runAnnounced(producer, scope);
|
|
1172
|
+
void this.#runAnnounced(producer, this.#scope.patterns(scope), options?.hidden ?? false);
|
|
854
1173
|
return producer.consume();
|
|
855
1174
|
}
|
|
856
|
-
|
|
1175
|
+
/** One snapshot shared by map readers and announcement-stream diffing. */
|
|
1176
|
+
#listed(patterns, hidden) {
|
|
1177
|
+
const next = new Map();
|
|
1178
|
+
const covering = new CoveringRoot(this.#scope.root);
|
|
1179
|
+
const scopes = [...patterns]
|
|
1180
|
+
.sort((a, b) => Path.compareSpecificity(b.specificity(), a.specificity()))
|
|
1181
|
+
.map((pattern) => ({ pattern, head: scopeHead(pattern) }));
|
|
1182
|
+
for (const [path, candidates] of this.#state.snapshot().candidates) {
|
|
1183
|
+
// The first candidate this reader can see wins, since the preferred one overall may not be.
|
|
1184
|
+
let entry;
|
|
1185
|
+
let scope;
|
|
1186
|
+
for (const candidate of candidates) {
|
|
1187
|
+
scope = scopes.find(({ pattern, head }) => (candidate.exact ? pattern.matches(path) : advertOverlaps(candidate, path, pattern)) &&
|
|
1188
|
+
(hidden || !hiddenBelow(head, path)));
|
|
1189
|
+
if (scope) {
|
|
1190
|
+
entry = candidate;
|
|
1191
|
+
break;
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
if (!entry || !scope)
|
|
1195
|
+
continue;
|
|
1196
|
+
const relative = covering.relative(path);
|
|
1197
|
+
if (relative === undefined)
|
|
1198
|
+
continue;
|
|
1199
|
+
next.set(relative, {
|
|
1200
|
+
identity: entry.identity,
|
|
1201
|
+
route: entry.route,
|
|
1202
|
+
captures: scopeCaptures(scope.pattern, path),
|
|
1203
|
+
});
|
|
1204
|
+
}
|
|
1205
|
+
return next;
|
|
1206
|
+
}
|
|
1207
|
+
async #runAnnounced(producer, patterns, hidden) {
|
|
857
1208
|
// Keyed by the presented path (from the origin, not the scope), valued by identity
|
|
858
1209
|
// plus route. Diffing identity rather than mere presence means a republish emits a
|
|
859
1210
|
// retraction then a fresh announcement; a re-price of the same identity emits an
|
|
@@ -866,28 +1217,7 @@ export class Consumer {
|
|
|
866
1217
|
const routes = this.#state.routes.peek();
|
|
867
1218
|
if (local === undefined && advertisedLocal === undefined && routes === undefined)
|
|
868
1219
|
break;
|
|
869
|
-
const next =
|
|
870
|
-
// Routes first, so an advertised local at the same path overwrites it: the
|
|
871
|
-
// announcement points at whatever request() would resolve. A route remains
|
|
872
|
-
// the prefix it advertised; the pattern is a local filter, not a new claim.
|
|
873
|
-
for (const [covered, entries] of routes ?? []) {
|
|
874
|
-
const entry = entries[0];
|
|
875
|
-
if (!entry)
|
|
876
|
-
continue;
|
|
877
|
-
if (!scopeOverlaps(scope, covered))
|
|
878
|
-
continue;
|
|
879
|
-
const snap = {
|
|
880
|
-
identity: entry.identity,
|
|
881
|
-
route: entry.route.peek(),
|
|
882
|
-
captures: scopeCaptures(scope, covered),
|
|
883
|
-
};
|
|
884
|
-
next.set(covered, snap);
|
|
885
|
-
}
|
|
886
|
-
for (const [path, front] of local ?? []) {
|
|
887
|
-
const route = advertisedLocal?.get(path) ?? Route.default;
|
|
888
|
-
if (scope.matches(path))
|
|
889
|
-
next.set(path, { identity: front, route, captures: scopeCaptures(scope, path) });
|
|
890
|
-
}
|
|
1220
|
+
const next = this.#listed(patterns, hidden);
|
|
891
1221
|
for (const [path, snap] of active) {
|
|
892
1222
|
const cur = next.get(path);
|
|
893
1223
|
if (!cur || cur.identity !== snap.identity)
|
|
@@ -939,17 +1269,27 @@ export class Consumer {
|
|
|
939
1269
|
* @internal
|
|
940
1270
|
*/
|
|
941
1271
|
/**
|
|
942
|
-
*
|
|
943
|
-
*
|
|
1272
|
+
* The announced local broadcast at `path`, when it beats the originated routes there.
|
|
1273
|
+
* Resolves through what rebuildOriginated advertised: a peer never sees received routes.
|
|
1274
|
+
*/
|
|
1275
|
+
#local(path) {
|
|
1276
|
+
const local = this.#state.local.peek()?.get(path);
|
|
1277
|
+
if (local && this.#state.localWins(path, this.#state.bestEntry(path, received)))
|
|
1278
|
+
return local;
|
|
1279
|
+
return undefined;
|
|
1280
|
+
}
|
|
1281
|
+
/**
|
|
1282
|
+
* Resolve `path` for serving: an announced local broadcast, or wait for an originated
|
|
1283
|
+
* dynamic to accept it. Undefined when nothing here can serve the path.
|
|
944
1284
|
*
|
|
945
1285
|
* @internal
|
|
946
1286
|
*/
|
|
947
1287
|
async #demand(path) {
|
|
948
|
-
const local = this.#
|
|
1288
|
+
const local = this.#local(path);
|
|
949
1289
|
if (local)
|
|
950
1290
|
return local;
|
|
951
|
-
const entry = this.#state.bestEntry(path);
|
|
952
|
-
if (!entry?.
|
|
1291
|
+
const entry = this.#state.bestEntry(path, received);
|
|
1292
|
+
if (!entry?.server)
|
|
953
1293
|
return undefined;
|
|
954
1294
|
const server = entry.server;
|
|
955
1295
|
const live = server.served.get(path);
|
|
@@ -1004,7 +1344,7 @@ export class Dynamic {
|
|
|
1004
1344
|
#retract;
|
|
1005
1345
|
#closed = false;
|
|
1006
1346
|
constructor(prefix, entry, state, retract) {
|
|
1007
|
-
this.prefix = prefix;
|
|
1347
|
+
this.prefix = Path.stripPrefix(entry.scope.root, prefix) ?? Path.empty();
|
|
1008
1348
|
this.#entry = entry;
|
|
1009
1349
|
this.#state = state;
|
|
1010
1350
|
this.#retract = retract;
|
|
@@ -1018,7 +1358,7 @@ export class Dynamic {
|
|
|
1018
1358
|
throw new Error("dynamic is closed");
|
|
1019
1359
|
this.#entry.route.set(Route.normalize(route));
|
|
1020
1360
|
this.#state.rebuildOriginated();
|
|
1021
|
-
this.#state.refreshPrefix(this.prefix);
|
|
1361
|
+
this.#state.refreshPrefix(Path.join(this.#entry.scope.root, this.prefix));
|
|
1022
1362
|
this.#state.routes.mutate(() => { });
|
|
1023
1363
|
}
|
|
1024
1364
|
/** Retract the route and reject anything still waiting. Idempotent. */
|