@moq/net 0.4.1 → 0.4.3
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 +20 -4
- package/ietf/filter.d.ts.map +1 -1
- package/ietf/filter.js +165 -17
- 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 +9 -6
- package/ietf/object.d.ts.map +1 -1
- package/ietf/object.js +47 -38
- package/ietf/object.js.map +1 -1
- package/ietf/parameters.d.ts +13 -8
- package/ietf/parameters.d.ts.map +1 -1
- package/ietf/parameters.js +177 -30
- package/ietf/parameters.js.map +1 -1
- package/ietf/publish.d.ts +1 -0
- package/ietf/publish.d.ts.map +1 -1
- package/ietf/publish.js +1 -0
- 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 +404 -202
- package/ietf/publisher.js.map +1 -1
- package/ietf/request_stream.d.ts +7 -0
- package/ietf/request_stream.d.ts.map +1 -0
- package/ietf/request_stream.js +20 -0
- package/ietf/request_stream.js.map +1 -0
- package/ietf/subscribe.d.ts +7 -1
- package/ietf/subscribe.d.ts.map +1 -1
- package/ietf/subscribe.js +24 -17
- package/ietf/subscribe.js.map +1 -1
- package/ietf/subscribe_namespace.d.ts.map +1 -1
- package/ietf/subscribe_namespace.js +7 -3
- package/ietf/subscribe_namespace.js.map +1 -1
- package/ietf/subscriber.js +83 -56
- 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 +39 -45
- 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.map +1 -1
- package/lite/subscribe.js +4 -5
- package/lite/subscribe.js.map +1 -1
- package/lite/subscriber.d.ts.map +1 -1
- package/lite/subscriber.js +210 -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,14 +10,17 @@ 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";
|
|
15
|
+
import * as Message from "./message.js";
|
|
16
16
|
import { FetchFrame, Frame, Group as GroupMessage } from "./object.js";
|
|
17
|
+
import { Parameters } from "./parameters.js";
|
|
17
18
|
import { fromWire, toWire } from "./priority.js";
|
|
18
19
|
import * as Properties from "./properties.js";
|
|
19
20
|
import { PublishDone, PublishDoneStatus } from "./publish.js";
|
|
20
|
-
import { PublishNamespace, PublishNamespaceDone, PublishNamespaceOk } from "./publish_namespace.js";
|
|
21
|
+
import { PublishNamespace, PublishNamespaceDone, PublishNamespaceOk, PublishNamespaceUpdate, } from "./publish_namespace.js";
|
|
21
22
|
import { RequestError, RequestOk } from "./request.js";
|
|
23
|
+
import { cancelled, finCancels } from "./request_stream.js";
|
|
22
24
|
import { SubscribeError, SubscribeOk } from "./subscribe.js";
|
|
23
25
|
import { SubscribeNamespaceEntry, SubscribeNamespaceEntryDone, SubscribeNamespaceOk, } from "./subscribe_namespace.js";
|
|
24
26
|
import { Version } from "./version.js";
|
|
@@ -36,8 +38,30 @@ function clusterFor(base, route) {
|
|
|
36
38
|
const hops = route.hops.at(-1) === self ? [...route.hops] : [...route.hops, self];
|
|
37
39
|
return { hops, cost: route.cost.warm };
|
|
38
40
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
+
/**
|
|
42
|
+
* What it takes to move the peer from `held` to `next`.
|
|
43
|
+
*
|
|
44
|
+
* - `"same"`: the peer would decode the same advertisement. The cold cost, and the whole
|
|
45
|
+
* route without Cluster, never reach the wire, so a change there sends nothing.
|
|
46
|
+
* - `"restart"`: nothing is held, nothing is wanted, or a different broadcast, which is
|
|
47
|
+
* withdrawn and advertised again.
|
|
48
|
+
* - Otherwise the same broadcast at a new hop chain or warm cost: the cluster parameters
|
|
49
|
+
* the peer holds and the ones it should, for the target to update.
|
|
50
|
+
*/
|
|
51
|
+
function change(base, held, next) {
|
|
52
|
+
if (held === undefined || next === undefined || held.identity !== next.identity)
|
|
53
|
+
return "restart";
|
|
54
|
+
const from = clusterFor(base, held.route);
|
|
55
|
+
const to = clusterFor(base, next.route);
|
|
56
|
+
if (from === undefined || to === undefined)
|
|
57
|
+
return from === to ? "same" : "restart";
|
|
58
|
+
if (from.cost === to.cost && sameHops(from.hops, to.hops))
|
|
59
|
+
return "same";
|
|
60
|
+
return { from, to };
|
|
61
|
+
}
|
|
62
|
+
/** Whether two hop paths list the same hops in the same order. */
|
|
63
|
+
function sameHops(a, b) {
|
|
64
|
+
return a.length === b.length && a.every((hop, i) => hop === b[i]);
|
|
41
65
|
}
|
|
42
66
|
/**
|
|
43
67
|
* How long one advertisement may take to be answered. Matches the Rust publisher, and the
|
|
@@ -59,6 +83,7 @@ export class Publisher {
|
|
|
59
83
|
#quic;
|
|
60
84
|
#session;
|
|
61
85
|
#requiresSolicitation;
|
|
86
|
+
#hidden;
|
|
62
87
|
// The origin this session serves, borrowed: it outlives the session, and closing the
|
|
63
88
|
// session leaves its broadcasts alone. The namespaces are advertised with an unsolicited
|
|
64
89
|
// PUBLISH_NAMESPACE (see {@link runPublishNamespaces}), or on request if the peer asked
|
|
@@ -74,13 +99,14 @@ export class Publisher {
|
|
|
74
99
|
*
|
|
75
100
|
* @internal
|
|
76
101
|
*/
|
|
77
|
-
constructor({ quic, session, publish, requiresSolicitation, cluster, }) {
|
|
102
|
+
constructor({ quic, session, publish, requiresSolicitation, hidden = false, cluster, }) {
|
|
78
103
|
this.#quic = quic;
|
|
79
104
|
this.#session = session;
|
|
80
105
|
const origin = publish && wireOf(publish);
|
|
81
106
|
this.#advertised = origin?.advertised ?? new Signal(new Map());
|
|
82
107
|
this.#publish = publish;
|
|
83
108
|
this.#requiresSolicitation = requiresSolicitation;
|
|
109
|
+
this.#hidden = hidden;
|
|
84
110
|
this.#advert = Cluster.advertise(cluster);
|
|
85
111
|
}
|
|
86
112
|
/**
|
|
@@ -94,20 +120,33 @@ export class Publisher {
|
|
|
94
120
|
const name = msg.trackNamespace;
|
|
95
121
|
let broadcast;
|
|
96
122
|
let refusal;
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
123
|
+
// Legal requests we can't honor are refused one at a time. A subscription that
|
|
124
|
+
// forwards nothing is only useful to a subscriber that later turns forwarding on,
|
|
125
|
+
// and serving a Range Filter unfiltered would deliver objects it excluded.
|
|
126
|
+
const unsupported = !msg.forward
|
|
127
|
+
? "FORWARD=0 not supported"
|
|
128
|
+
: msg.rangeFilters
|
|
129
|
+
? "range filters not supported"
|
|
130
|
+
: undefined;
|
|
131
|
+
if (unsupported) {
|
|
132
|
+
refusal = { errorCode: toRequestCode("not_supported", "subscribe", version), reasonPhrase: unsupported };
|
|
106
133
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
134
|
+
else {
|
|
135
|
+
try {
|
|
136
|
+
broadcast =
|
|
137
|
+
this.#publish && (wireOf(this.#publish).local(name) ?? (await wireOf(this.#publish).demand(name)));
|
|
138
|
+
if (!broadcast) {
|
|
139
|
+
refusal = {
|
|
140
|
+
errorCode: toRequestCode("does_not_exist", "subscribe", version),
|
|
141
|
+
reasonPhrase: "broadcast not found",
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
catch (err) {
|
|
146
|
+
const e = error(err);
|
|
147
|
+
const condition = e instanceof StreamError && e.code === StreamCode.NotFound ? "does_not_exist" : "internal";
|
|
148
|
+
refusal = { errorCode: toRequestCode(condition, "subscribe", version), reasonPhrase: reason(e) };
|
|
149
|
+
}
|
|
111
150
|
}
|
|
112
151
|
if (refusal) {
|
|
113
152
|
if (version === Version.DRAFT_14) {
|
|
@@ -217,17 +256,82 @@ export class Publisher {
|
|
|
217
256
|
const unsubscribed = new Promise((resolve) => {
|
|
218
257
|
unsubscribe = resolve;
|
|
219
258
|
});
|
|
220
|
-
|
|
259
|
+
// Update replies and PUBLISH_DONE come from separate tasks but share this stream, and a
|
|
260
|
+
// message is written in several pieces. Chain them so each goes out whole, in order,
|
|
261
|
+
// and nothing follows PUBLISH_DONE.
|
|
262
|
+
let replies = Promise.resolve();
|
|
263
|
+
let replied = false;
|
|
264
|
+
const reply = (write) => {
|
|
265
|
+
const sent = replies.then(() => (replied ? undefined : write()));
|
|
266
|
+
replies = sent.catch(() => undefined);
|
|
267
|
+
return sent;
|
|
268
|
+
};
|
|
269
|
+
let publishError;
|
|
270
|
+
const updates = (async () => {
|
|
271
|
+
if (version === Version.DRAFT_14 || version === Version.DRAFT_15 || version === Version.DRAFT_16)
|
|
272
|
+
return stream.reader.closed;
|
|
273
|
+
for (;;) {
|
|
274
|
+
if (await stream.reader.done()) {
|
|
275
|
+
if (finCancels(version))
|
|
276
|
+
return;
|
|
277
|
+
return stream.writer.closed;
|
|
278
|
+
}
|
|
279
|
+
if ((await stream.reader.u53()) !== 0x02)
|
|
280
|
+
throw new Error("unexpected message on subscribe stream");
|
|
281
|
+
const params = await Message.decode(stream.reader, async (r) => {
|
|
282
|
+
await r.u62();
|
|
283
|
+
if (version === Version.DRAFT_17)
|
|
284
|
+
await r.u62();
|
|
285
|
+
return Parameters.decode(r, version);
|
|
286
|
+
});
|
|
287
|
+
const unsupported = params.forward === false ||
|
|
288
|
+
params.bytes.size > 0 ||
|
|
289
|
+
params.subscriptionFilter !== undefined ||
|
|
290
|
+
params.largest !== undefined ||
|
|
291
|
+
params.rangeFilters ||
|
|
292
|
+
params.trackPropertyFilter ||
|
|
293
|
+
params.authorizationToken ||
|
|
294
|
+
[...params.vars.keys()].some((key) => key !== 0x10n && key !== 0x20n);
|
|
295
|
+
if (unsupported) {
|
|
296
|
+
// Recorded before the reply, so a PUBLISH_DONE queued behind it reports the failure.
|
|
297
|
+
const failed = new UpdateFailed();
|
|
298
|
+
publishError = failed;
|
|
299
|
+
await reply(async () => {
|
|
300
|
+
await stream.writer.u53(RequestError.id);
|
|
301
|
+
await new RequestError({
|
|
302
|
+
requestId: undefined,
|
|
303
|
+
errorCode: toRequestCode("not_supported", "subscribe", version),
|
|
304
|
+
reasonPhrase: "REQUEST_UPDATE parameters not supported",
|
|
305
|
+
}).encode(stream.writer, version);
|
|
306
|
+
});
|
|
307
|
+
throw failed;
|
|
308
|
+
}
|
|
309
|
+
// update() replaces every option, so carry the rest over as Rust does.
|
|
310
|
+
if (params.subscriberPriority !== undefined)
|
|
311
|
+
track.update({ ...track.subscription.peek(), priority: fromWire(params.subscriberPriority) });
|
|
312
|
+
await reply(async () => {
|
|
313
|
+
await stream.writer.u53(RequestOk.id);
|
|
314
|
+
await new RequestOk({ requestId: undefined }).encode(stream.writer, version);
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
})();
|
|
318
|
+
const requestEnded = version === Version.DRAFT_14 || version === Version.DRAFT_15 || version === Version.DRAFT_16
|
|
319
|
+
? updates
|
|
320
|
+
: race([updates, stream.writer.closed]);
|
|
321
|
+
void requestEnded.then(() => {
|
|
221
322
|
if (!finished)
|
|
222
323
|
unsubscribe();
|
|
223
324
|
},
|
|
224
|
-
//
|
|
225
|
-
() =>
|
|
325
|
+
// Updates can fail after the serving loop ends while its last groups drain.
|
|
326
|
+
(err) => {
|
|
327
|
+
publishError = error(err);
|
|
328
|
+
unsubscribe();
|
|
329
|
+
});
|
|
226
330
|
// Every group started, until its stream finishes or resets, and the data streams
|
|
227
331
|
// opened for PUBLISH_DONE to report.
|
|
228
332
|
const groups = new Set();
|
|
229
333
|
const streams = { opened: 0 };
|
|
230
|
-
// Serve
|
|
334
|
+
// Serve groups until the track ends or the requester cancels.
|
|
231
335
|
const serving = (async () => {
|
|
232
336
|
for (;;) {
|
|
233
337
|
const group = await track.recvGroup();
|
|
@@ -270,12 +374,10 @@ export class Publisher {
|
|
|
270
374
|
streams,
|
|
271
375
|
})
|
|
272
376
|
: Promise.resolve();
|
|
273
|
-
let publishError;
|
|
274
377
|
let ended = false;
|
|
275
378
|
try {
|
|
276
379
|
const served = Symbol("served");
|
|
277
|
-
ended =
|
|
278
|
-
(await race([Promise.all([serving, filling]).then(() => served), stream.reader.closed])) === served;
|
|
380
|
+
ended = (await race([Promise.all([serving, filling]).then(() => served), requestEnded])) === served;
|
|
279
381
|
}
|
|
280
382
|
catch (err) {
|
|
281
383
|
publishError = error(err);
|
|
@@ -284,6 +386,14 @@ export class Publisher {
|
|
|
284
386
|
// the draft requires, so its count is final. The subscriber leaving cancels the
|
|
285
387
|
// ones still queued instead.
|
|
286
388
|
await race([Promise.all(groups), unsubscribed]);
|
|
389
|
+
// An error of our own (a refused or malformed update, a failed track) ends the
|
|
390
|
+
// subscription while the requester may still listen, so its open streams reset
|
|
391
|
+
// rather than finish as if complete, and PUBLISH_DONE still follows them. A stream
|
|
392
|
+
// error is the requester cancelling, which the shortcut above already covers.
|
|
393
|
+
if (publishError && !(publishError instanceof StreamError)) {
|
|
394
|
+
track.close(publishError);
|
|
395
|
+
await Promise.all([...groups, filling]);
|
|
396
|
+
}
|
|
287
397
|
// Draft 14 on has no end location in PUBLISH_DONE: an END_OF_TRACK object is what
|
|
288
398
|
// tells the subscriber where the track ended.
|
|
289
399
|
const final = track.final();
|
|
@@ -303,23 +413,32 @@ export class Publisher {
|
|
|
303
413
|
// PUBLISH_DONE is required for every supported draft. The peer may have already
|
|
304
414
|
// closed its side to unsubscribe, in which case there is nowhere to send it.
|
|
305
415
|
try {
|
|
306
|
-
await
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
:
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
416
|
+
await reply(async () => {
|
|
417
|
+
replied = true;
|
|
418
|
+
await stream.writer.u53(PublishDone.id);
|
|
419
|
+
const done = new PublishDone({
|
|
420
|
+
requestId: version === Version.DRAFT_14 || version === Version.DRAFT_15 || version === Version.DRAFT_16
|
|
421
|
+
? msg.requestId
|
|
422
|
+
: undefined,
|
|
423
|
+
statusCode: publishError instanceof UpdateFailed
|
|
424
|
+
? PublishDoneStatus.UPDATE_FAILED
|
|
425
|
+
: publishError
|
|
426
|
+
? PublishDoneStatus.INTERNAL_ERROR
|
|
427
|
+
: PublishDoneStatus.TRACK_ENDED,
|
|
428
|
+
streamCount: BigInt(streams.opened),
|
|
429
|
+
reasonPhrase: publishError instanceof UpdateFailed
|
|
430
|
+
? "update failed"
|
|
431
|
+
: publishError
|
|
432
|
+
? "internal error"
|
|
433
|
+
: "track ended",
|
|
434
|
+
});
|
|
435
|
+
await done.encode(stream.writer, version);
|
|
314
436
|
});
|
|
315
|
-
await done.encode(stream.writer, version);
|
|
316
437
|
}
|
|
317
438
|
catch {
|
|
318
439
|
// Stream might already be closed by peer.
|
|
319
440
|
}
|
|
320
|
-
//
|
|
321
|
-
// that lands while PublishDone is still going out as our own completion, leaving
|
|
322
|
-
// queued groups to open for a subscriber that has already left.
|
|
441
|
+
// Our close follows PUBLISH_DONE; it must not cancel data still queued for delivery.
|
|
323
442
|
finished = true;
|
|
324
443
|
stream.close();
|
|
325
444
|
}
|
|
@@ -366,7 +485,9 @@ export class Publisher {
|
|
|
366
485
|
hasExtensions: stamped,
|
|
367
486
|
hasSubgroup: false,
|
|
368
487
|
hasSubgroupObject: false,
|
|
369
|
-
|
|
488
|
+
// A stream capped by the range stops before the group may end, so its FIN
|
|
489
|
+
// cannot claim END_OF_GROUP.
|
|
490
|
+
hasEnd: slice.until === undefined,
|
|
370
491
|
hasPriority: true,
|
|
371
492
|
// Only honest when the stream really starts at the group's first object;
|
|
372
493
|
// a trimmed head starts partway through.
|
|
@@ -374,7 +495,7 @@ export class Publisher {
|
|
|
374
495
|
},
|
|
375
496
|
});
|
|
376
497
|
try {
|
|
377
|
-
await hooks.guardGroup(group, header.encode(stream, this.#session.version));
|
|
498
|
+
await hooks.guardGroup(group, () => header.encode(stream, this.#session.version));
|
|
378
499
|
// The first written object goes on the wire as its absolute id, so a trimmed
|
|
379
500
|
// head shows the true numbering rather than a silently renumbered group.
|
|
380
501
|
let first = true;
|
|
@@ -402,7 +523,7 @@ export class Publisher {
|
|
|
402
523
|
const obj = new Frame({ payload: read.frame.payload, timestamp: read.frame.timestamp });
|
|
403
524
|
const delta = first ? read.sequence : 0;
|
|
404
525
|
first = false;
|
|
405
|
-
await hooks.guardGroup(group, obj.encode(stream, header.flags, timescale, this.#session.version, delta));
|
|
526
|
+
await hooks.guardGroup(group, () => obj.encode(stream, header.flags, timescale, this.#session.version, delta));
|
|
406
527
|
}
|
|
407
528
|
finally {
|
|
408
529
|
read.complete();
|
|
@@ -543,11 +664,9 @@ export class Publisher {
|
|
|
543
664
|
/**
|
|
544
665
|
* Handles an incoming SUBSCRIBE_NAMESPACE on a bidi stream.
|
|
545
666
|
*
|
|
546
|
-
*
|
|
547
|
-
*
|
|
548
|
-
*
|
|
549
|
-
* broadcast, so only the hidden namespaces it may see ride here (MoQ Hidden). Draft-16+ streams Namespace entries inline; draft-14/15 predate those
|
|
550
|
-
* messages, so each advertisement is a PUBLISH_NAMESPACE request of its own.
|
|
667
|
+
* Draft-16+ fills the stream with every visible match, independently of unsolicited
|
|
668
|
+
* discovery. Draft-14/15 use PUBLISH_NAMESPACE requests instead and suppress matches
|
|
669
|
+
* already announced on the unsolicited path. Hidden visibility applies to both.
|
|
551
670
|
*
|
|
552
671
|
* @internal
|
|
553
672
|
*/
|
|
@@ -557,6 +676,7 @@ export class Publisher {
|
|
|
557
676
|
const legacy = version === Version.DRAFT_14 || version === Version.DRAFT_15;
|
|
558
677
|
// Draft-14/15: the open PUBLISH_NAMESPACE request per advertised suffix.
|
|
559
678
|
const requests = new Map();
|
|
679
|
+
const ending = cancelled(stream, version);
|
|
560
680
|
try {
|
|
561
681
|
// Send OK response
|
|
562
682
|
if (version === Version.DRAFT_14) {
|
|
@@ -571,39 +691,37 @@ export class Publisher {
|
|
|
571
691
|
});
|
|
572
692
|
await ok.encode(stream.writer, version);
|
|
573
693
|
}
|
|
574
|
-
//
|
|
575
|
-
//
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
//
|
|
580
|
-
//
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
if (legacy) {
|
|
584
|
-
return await this.#advertise(Path.join(prefix, suffix), requests, refused, cluster);
|
|
585
|
-
}
|
|
694
|
+
// Only legacy requests create another route; inline entries always fill the query.
|
|
695
|
+
// Hidden namespaces still require opt-in unless the prefix names them directly.
|
|
696
|
+
const carries = (covered) => (!this.#hidden || msg.hidden || !hiddenBelow(prefix, covered)) &&
|
|
697
|
+
(!legacy || this.#requiresSolicitation || (this.#hidden && hiddenBelow(Path.empty(), covered)));
|
|
698
|
+
// Inline entries always land, and the receiver treats a repeated NAMESPACE as a
|
|
699
|
+
// replacement, so repricing one is just sending it again, a new original
|
|
700
|
+
// publisher included, as Rust does. Only a legacy PUBLISH_NAMESPACE request can
|
|
701
|
+
// be declined.
|
|
702
|
+
const namespace = async (suffix, snap) => {
|
|
586
703
|
await stream.writer.u53(SubscribeNamespaceEntry.id);
|
|
704
|
+
const cluster = clusterFor(this.#advert, snap.route);
|
|
587
705
|
await new SubscribeNamespaceEntry({ suffix, cluster }).encode(stream.writer, version);
|
|
588
|
-
return
|
|
706
|
+
return "held";
|
|
589
707
|
};
|
|
590
|
-
const
|
|
591
|
-
|
|
592
|
-
|
|
708
|
+
const target = legacy
|
|
709
|
+
? {
|
|
710
|
+
advertise: (suffix, snap) => this.#advertise(Path.join(prefix, suffix), requests, clusterFor(this.#advert, snap.route)),
|
|
711
|
+
update: (suffix, _snap, reprice) => this.#update(Path.join(prefix, suffix), requests, reprice),
|
|
712
|
+
withdraw: (suffix) => this.#withdraw(Path.join(prefix, suffix), requests),
|
|
593
713
|
}
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
714
|
+
: {
|
|
715
|
+
advertise: namespace,
|
|
716
|
+
update: namespace,
|
|
717
|
+
withdraw: async (suffix) => {
|
|
718
|
+
await stream.writer.u53(SubscribeNamespaceEntryDone.id);
|
|
719
|
+
await new SubscribeNamespaceEntryDone({ suffix }).encode(stream.writer, version);
|
|
720
|
+
},
|
|
721
|
+
};
|
|
722
|
+
// Keyed by suffix, as the entries name them.
|
|
723
|
+
const ns = { active: new Map(), refused: new Map(), offered: new Map() };
|
|
602
724
|
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
725
|
for (;;) {
|
|
608
726
|
// Subscribe BEFORE reconciling, for the same reason as
|
|
609
727
|
// {@link runPublishNamespaces}: a publish landing while an advertisement
|
|
@@ -618,57 +736,12 @@ export class Publisher {
|
|
|
618
736
|
dispose();
|
|
619
737
|
break;
|
|
620
738
|
}
|
|
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));
|
|
739
|
+
const outstanding = await this.#reconcile(ns, presented(prefix, advertised, carries), target);
|
|
667
740
|
retry = outstanding ? Math.min(retry ? retry * 2 : RETRY_BASE, RETRY_MAX) : 0;
|
|
668
741
|
// Wait for the next change, or for the peer to unsubscribe.
|
|
669
742
|
const next = await (retry
|
|
670
|
-
? race([changed,
|
|
671
|
-
: race([changed,
|
|
743
|
+
? race([changed, ending, retryAfter(retry).then(() => advertised)])
|
|
744
|
+
: race([changed, ending]));
|
|
672
745
|
dispose();
|
|
673
746
|
if (!next)
|
|
674
747
|
break;
|
|
@@ -694,8 +767,8 @@ export class Publisher {
|
|
|
694
767
|
* The peers that never send SUBSCRIBE_NAMESPACE are exactly the ones expecting a
|
|
695
768
|
* publisher to announce itself, so announcing is the default. A peer that would
|
|
696
769
|
* rather ask says so in its SETUP (MoQ Solicit) and this does nothing, leaving
|
|
697
|
-
* {@link runSubscribeNamespace} to carry the advertisements instead.
|
|
698
|
-
*
|
|
770
|
+
* {@link runSubscribeNamespace} to carry the advertisements instead. Modern peers
|
|
771
|
+
* may also discover the namespace through an inline subscription fill.
|
|
699
772
|
*
|
|
700
773
|
* @internal
|
|
701
774
|
*/
|
|
@@ -706,6 +779,11 @@ export class Publisher {
|
|
|
706
779
|
}
|
|
707
780
|
// The open PUBLISH_NAMESPACE request per advertised path.
|
|
708
781
|
const requests = new Map();
|
|
782
|
+
const target = {
|
|
783
|
+
advertise: (path, snap) => this.#advertise(path, requests, clusterFor(this.#advert, snap.route)),
|
|
784
|
+
update: (path, _snap, reprice) => this.#update(path, requests, reprice),
|
|
785
|
+
withdraw: (path) => this.#withdraw(path, requests),
|
|
786
|
+
};
|
|
709
787
|
// The origin outlives the session and its signal never ends, so this loop needs the
|
|
710
788
|
// session's own ending to stop: without it a closed connection leaves the loop parked
|
|
711
789
|
// on a shared origin forever, waking on someone else's publish to fail on a dead
|
|
@@ -714,14 +792,9 @@ export class Publisher {
|
|
|
714
792
|
const closed = this.#quic.closed.then(() => undefined, () => undefined);
|
|
715
793
|
let dispose;
|
|
716
794
|
try {
|
|
717
|
-
//
|
|
718
|
-
|
|
719
|
-
let active = new Map();
|
|
795
|
+
// Keyed by path.
|
|
796
|
+
const ns = { active: new Map(), refused: new Map(), offered: new Map() };
|
|
720
797
|
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
798
|
for (;;) {
|
|
726
799
|
// Subscribe BEFORE reconciling. Each advertisement below waits a round trip
|
|
727
800
|
// for the peer's reply, and a publish that lands in that window notifies
|
|
@@ -738,47 +811,13 @@ export class Publisher {
|
|
|
738
811
|
break;
|
|
739
812
|
}
|
|
740
813
|
const updated = new Map();
|
|
741
|
-
for (const [covered,
|
|
742
|
-
//
|
|
743
|
-
if (hiddenBelow(Path.empty(), covered))
|
|
814
|
+
for (const [covered, candidates] of advertised) {
|
|
815
|
+
// Only peers that declared MoQ Hidden filter unsolicited discovery.
|
|
816
|
+
if ((this.#hidden && hiddenBelow(Path.empty(), covered)) || candidates.length === 0)
|
|
744
817
|
continue;
|
|
745
|
-
updated.set(covered,
|
|
818
|
+
updated.set(covered, candidates[0]);
|
|
746
819
|
}
|
|
747
|
-
|
|
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))
|
|
761
|
-
continue;
|
|
762
|
-
await this.#withdraw(removed, requests);
|
|
763
|
-
}
|
|
764
|
-
for (const [added, snap] of updated) {
|
|
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));
|
|
820
|
+
const outstanding = await this.#reconcile(ns, updated, target);
|
|
782
821
|
retry = outstanding ? Math.min(retry ? retry * 2 : RETRY_BASE, RETRY_MAX) : 0;
|
|
783
822
|
// Wait for the next change, which has already fired if one landed above.
|
|
784
823
|
const next = await (retry
|
|
@@ -802,14 +841,79 @@ export class Publisher {
|
|
|
802
841
|
}
|
|
803
842
|
}
|
|
804
843
|
}
|
|
844
|
+
/**
|
|
845
|
+
* Bring the peer's view in line with `updated`, one namespace at a time, and report
|
|
846
|
+
* whether anything it should hold is still missing and worth coming back for.
|
|
847
|
+
*
|
|
848
|
+
* Nothing the peer cannot see is sent, a reprice updates the namespace in place, and
|
|
849
|
+
* anything else is withdrawn first so a republish reads as withdraw-then-advertise.
|
|
850
|
+
* `ns` records what the peer holds rather than what we attempted: a declined
|
|
851
|
+
* advertisement or update stays out of it, so the next turn offers it fresh instead
|
|
852
|
+
* of believing it is already up.
|
|
853
|
+
*/
|
|
854
|
+
async #reconcile(ns, updated, target) {
|
|
855
|
+
// A namespace that is gone, or that a republish replaced, takes its refusal with
|
|
856
|
+
// it: the peer refused a broadcast, not a path forever, so a different one at the
|
|
857
|
+
// same path is offered again. The origin swaps the front in one mutation, so the
|
|
858
|
+
// path never leaves `updated` and only the front says they differ. Rust gets this
|
|
859
|
+
// by rebuilding the watched entry.
|
|
860
|
+
for (const key of [...ns.refused.keys()]) {
|
|
861
|
+
const snap = updated.get(key);
|
|
862
|
+
if (snap === undefined || ns.offered.get(key) !== snap.identity) {
|
|
863
|
+
ns.refused.delete(key);
|
|
864
|
+
ns.offered.delete(key);
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
const held = new Map(ns.active);
|
|
868
|
+
for (const [key, snap] of ns.active) {
|
|
869
|
+
if (change(this.#advert, snap, updated.get(key)) !== "restart")
|
|
870
|
+
continue;
|
|
871
|
+
await target.withdraw(key);
|
|
872
|
+
held.delete(key);
|
|
873
|
+
}
|
|
874
|
+
for (const [key, snap] of updated) {
|
|
875
|
+
const prev = held.get(key);
|
|
876
|
+
const diff = change(this.#advert, prev, snap);
|
|
877
|
+
if (diff === "same")
|
|
878
|
+
continue;
|
|
879
|
+
let answer;
|
|
880
|
+
if (prev !== undefined && diff !== "restart") {
|
|
881
|
+
answer = await target.update(key, snap, diff);
|
|
882
|
+
}
|
|
883
|
+
else {
|
|
884
|
+
// A fresh offer waits for what a refusal asked for, whatever brought us back.
|
|
885
|
+
if (!this.#offerable(key, ns.refused))
|
|
886
|
+
continue;
|
|
887
|
+
answer = await target.advertise(key, snap);
|
|
888
|
+
}
|
|
889
|
+
// `offered` pairs with `refused`, so a held or withdrawn path leaves nothing behind.
|
|
890
|
+
if (answer === "held") {
|
|
891
|
+
held.set(key, snap);
|
|
892
|
+
ns.refused.delete(key);
|
|
893
|
+
ns.offered.delete(key);
|
|
894
|
+
}
|
|
895
|
+
else {
|
|
896
|
+
held.delete(key);
|
|
897
|
+
if (answer !== "dropped") {
|
|
898
|
+
ns.refused.set(key, answer);
|
|
899
|
+
ns.offered.set(key, snap.identity);
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
ns.active = held;
|
|
904
|
+
// Whatever we wanted up and could not get up. Stream credit freeing, a transient
|
|
905
|
+
// failure clearing, or the peer starting to answer raises no signal of its own, so
|
|
906
|
+
// the only way back is to ask again on a timer.
|
|
907
|
+
return [...updated].some(([key, snap]) => change(this.#advert, held.get(key), snap) !== "same" && this.#pending(key, ns.refused));
|
|
908
|
+
}
|
|
805
909
|
/**
|
|
806
910
|
* Whether a namespace may be offered to the peer right now.
|
|
807
911
|
*
|
|
808
912
|
* A peer that asked never to be offered it again means it, whatever brought us back;
|
|
809
913
|
* one that named a minimum wait gets it, even when our own backoff comes round sooner.
|
|
810
914
|
*/
|
|
811
|
-
#offerable(
|
|
812
|
-
const entry = refused.get(
|
|
915
|
+
#offerable(key, refused) {
|
|
916
|
+
const entry = refused.get(key);
|
|
813
917
|
if (entry === undefined)
|
|
814
918
|
return true;
|
|
815
919
|
return entry !== "never" && Date.now() >= entry;
|
|
@@ -822,21 +926,31 @@ export class Publisher {
|
|
|
822
926
|
* gating the timer on offerable instead would disarm it for exactly the wait it is
|
|
823
927
|
* supposed to be counting. Only a refusal that forbids retrying ends it.
|
|
824
928
|
*/
|
|
825
|
-
#pending(
|
|
826
|
-
return refused.get(
|
|
929
|
+
#pending(key, refused) {
|
|
930
|
+
return refused.get(key) !== "never";
|
|
931
|
+
}
|
|
932
|
+
/**
|
|
933
|
+
* What a REQUEST_ERROR's retry interval, in milliseconds, says about re-offering the
|
|
934
|
+
* namespace.
|
|
935
|
+
*
|
|
936
|
+
* Draft-14/15 carry no such field, so a 0 there says nothing and our own backoff
|
|
937
|
+
* stands; everywhere else 0 means the peer does not want this again.
|
|
938
|
+
*/
|
|
939
|
+
#refusal(err) {
|
|
940
|
+
const version = this.#session.version;
|
|
941
|
+
if (version === Version.DRAFT_14 || version === Version.DRAFT_15)
|
|
942
|
+
return "dropped";
|
|
943
|
+
return err.retryInterval === 0n ? "never" : Date.now() + Number(err.retryInterval);
|
|
827
944
|
}
|
|
828
945
|
/**
|
|
829
946
|
* Advertise one namespace on its own PUBLISH_NAMESPACE request. A declined request
|
|
830
947
|
* is logged and skipped: a peer that wants none of this rejects each one and stays
|
|
831
948
|
* 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
949
|
*/
|
|
836
|
-
async #advertise(path, requests,
|
|
950
|
+
async #advertise(path, requests, cluster = this.#advert) {
|
|
837
951
|
const requestId = await this.#session.nextRequestId();
|
|
838
952
|
if (requestId === undefined)
|
|
839
|
-
return
|
|
953
|
+
return "dropped";
|
|
840
954
|
// Opening is inside the try because it fails like any other advertisement does:
|
|
841
955
|
// a peer out of stream credit rejects here, and letting that escape would unwind
|
|
842
956
|
// the announce loop and take discovery down for the whole session, rather than
|
|
@@ -849,22 +963,18 @@ export class Publisher {
|
|
|
849
963
|
// advertisements, so a peer that takes the stream and answers nothing strands
|
|
850
964
|
// every publish and withdrawal behind it. Timing out costs this namespace a
|
|
851
965
|
// turn and leaves it outstanding, which the retry above re-offers.
|
|
852
|
-
await withTimeout((async () => {
|
|
966
|
+
const answer = await withTimeout((async () => {
|
|
853
967
|
await stream.writer.u53(PublishNamespace.id);
|
|
854
968
|
const msg = new PublishNamespace({ requestId, trackNamespace: path, cluster });
|
|
855
969
|
await msg.encode(stream.writer, this.#session.version);
|
|
856
970
|
// Read response (RequestOk and PublishNamespaceOk share 0x07)
|
|
857
971
|
const respTypeId = await stream.reader.u53();
|
|
858
972
|
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
973
|
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)}`);
|
|
974
|
+
const why = requestReason(err.errorCode, err.reasonPhrase, "publish_namespace", this.#session.version);
|
|
975
|
+
console.warn(`announce refused: broadcast=${path} error=${why}`);
|
|
976
|
+
stream.abort(new Error(`PublishNamespace rejected: ${why}`));
|
|
977
|
+
return this.#refusal(err);
|
|
868
978
|
}
|
|
869
979
|
if (respTypeId !== RequestOk.id) {
|
|
870
980
|
throw new Error(`PublishNamespace rejected: typeId=0x${respTypeId.toString(16)}`);
|
|
@@ -876,18 +986,79 @@ export class Publisher {
|
|
|
876
986
|
else {
|
|
877
987
|
await RequestOk.decode(stream.reader, this.#session.version);
|
|
878
988
|
}
|
|
989
|
+
return "held";
|
|
879
990
|
})(), ADVERTISE_TIMEOUT_MS, `advertisement timed out after ${ADVERTISE_TIMEOUT_MS}ms waiting for the peer's answer`);
|
|
880
|
-
|
|
881
|
-
|
|
991
|
+
if (answer === "held")
|
|
992
|
+
requests.set(path, { path, requestId, stream });
|
|
993
|
+
return answer;
|
|
882
994
|
}
|
|
883
995
|
catch (err) {
|
|
884
996
|
// The peer never answered the advertisement: a control timeout, not late content.
|
|
885
997
|
const e = err instanceof TimeoutError ? controlTimeout(err) : error(err);
|
|
886
998
|
console.warn(`announce failed: broadcast=${path} error=${reason(e)}`);
|
|
887
999
|
request?.abort(e);
|
|
888
|
-
return
|
|
1000
|
+
return "dropped";
|
|
889
1001
|
}
|
|
890
1002
|
}
|
|
1003
|
+
/**
|
|
1004
|
+
* Update a namespace the peer holds: REQUEST_UPDATE on the request that carries it,
|
|
1005
|
+
* with only the parameters that changed, then its answer.
|
|
1006
|
+
*
|
|
1007
|
+
* Waiting for the answer keeps one update outstanding per stream, which satisfies any
|
|
1008
|
+
* MAX_REQUEST_UPDATES the peer set without reading it; a change landing meanwhile waits
|
|
1009
|
+
* for the next turn. The answer decides whether the peer still holds the namespace: a
|
|
1010
|
+
* REQUEST_ERROR closes the peer's side and withdraws the advertisement, so ours is
|
|
1011
|
+
* finished too, and an unanswered update is dropped abruptly, since a peer that ignored
|
|
1012
|
+
* it cannot be assumed to hold either price. Either way the retry re-offers it fresh.
|
|
1013
|
+
*
|
|
1014
|
+
* A different original publisher is an update like any other: the receiver drains what
|
|
1015
|
+
* it already serves from the old one and never splices the two.
|
|
1016
|
+
*/
|
|
1017
|
+
async #update(path, requests, { from, to }) {
|
|
1018
|
+
const request = requests.get(path);
|
|
1019
|
+
if (!request)
|
|
1020
|
+
return "dropped";
|
|
1021
|
+
const update = {
|
|
1022
|
+
hops: sameHops(from.hops, to.hops) ? undefined : to.hops,
|
|
1023
|
+
cost: from.cost === to.cost ? undefined : to.cost,
|
|
1024
|
+
};
|
|
1025
|
+
const version = this.#session.version;
|
|
1026
|
+
let answer;
|
|
1027
|
+
try {
|
|
1028
|
+
const requestId = await this.#session.nextRequestId();
|
|
1029
|
+
if (requestId === undefined)
|
|
1030
|
+
throw new Error("no request ID for the update");
|
|
1031
|
+
answer = await withTimeout((async () => {
|
|
1032
|
+
const { writer, reader } = request.stream;
|
|
1033
|
+
await writer.u53(PublishNamespaceUpdate.id);
|
|
1034
|
+
await new PublishNamespaceUpdate({ requestId, update }).encode(writer, version);
|
|
1035
|
+
const typeId = await reader.u53();
|
|
1036
|
+
if (typeId === RequestError.id) {
|
|
1037
|
+
const err = await RequestError.decode(reader, version);
|
|
1038
|
+
const why = requestReason(err.errorCode, err.reasonPhrase, "publish_namespace", version);
|
|
1039
|
+
console.warn(`publish_namespace update refused: broadcast=${path} error=${why}`);
|
|
1040
|
+
return this.#refusal(err);
|
|
1041
|
+
}
|
|
1042
|
+
if (typeId !== RequestOk.id) {
|
|
1043
|
+
throw new Error(`unexpected answer to the update: typeId=0x${typeId.toString(16)}`);
|
|
1044
|
+
}
|
|
1045
|
+
await RequestOk.decode(reader, version);
|
|
1046
|
+
return "held";
|
|
1047
|
+
})(), ADVERTISE_TIMEOUT_MS, `update timed out after ${ADVERTISE_TIMEOUT_MS}ms waiting for the peer's answer`);
|
|
1048
|
+
}
|
|
1049
|
+
catch (err) {
|
|
1050
|
+
const e = err instanceof TimeoutError ? controlTimeout(err) : error(err);
|
|
1051
|
+
console.warn(`publish_namespace update failed: broadcast=${path} error=${reason(e)}`);
|
|
1052
|
+
requests.delete(path);
|
|
1053
|
+
request.stream.abort(e);
|
|
1054
|
+
return "dropped";
|
|
1055
|
+
}
|
|
1056
|
+
// The peer already closed out a refused request, and its STOP_SENDING can reject the
|
|
1057
|
+
// wait on our FIN. That is the withdrawal finishing, not a reason to end the loop.
|
|
1058
|
+
if (answer !== "held")
|
|
1059
|
+
await this.#withdraw(path, requests).catch(() => undefined);
|
|
1060
|
+
return answer;
|
|
1061
|
+
}
|
|
891
1062
|
/**
|
|
892
1063
|
* Close out a namespace's PUBLISH_NAMESPACE request with PUBLISH_NAMESPACE_DONE.
|
|
893
1064
|
*/
|
|
@@ -925,23 +1096,49 @@ export class Publisher {
|
|
|
925
1096
|
* @internal
|
|
926
1097
|
*/
|
|
927
1098
|
async runTrackStatusRequest(msg, stream) {
|
|
1099
|
+
// TRACK_STATUS_ERROR is 0x0f on draft-14.
|
|
1100
|
+
await this.#refuseUnsupported(stream, msg.requestId, "track_status", 0x0f, "TRACK_STATUS is not supported");
|
|
1101
|
+
}
|
|
1102
|
+
/**
|
|
1103
|
+
* Refuses an incoming SUBSCRIBE_TRACKS (draft-18+): we never send the PUBLISH it asks for.
|
|
1104
|
+
* The reply carries no Request ID, so the message body is left unread.
|
|
1105
|
+
*
|
|
1106
|
+
* @internal
|
|
1107
|
+
*/
|
|
1108
|
+
async runSubscribeTracks(stream) {
|
|
928
1109
|
const version = this.#session.version;
|
|
929
|
-
|
|
1110
|
+
await stream.writer.u53(RequestError.id);
|
|
1111
|
+
await new RequestError({
|
|
1112
|
+
errorCode: toRequestCode("not_supported", "subscribe_namespace", version),
|
|
1113
|
+
reasonPhrase: "SUBSCRIBE_TRACKS is not supported",
|
|
1114
|
+
}).encode(stream.writer, version);
|
|
1115
|
+
stream.close();
|
|
1116
|
+
}
|
|
1117
|
+
/**
|
|
1118
|
+
* Handles an incoming FETCH on a bidi stream. We serve none.
|
|
1119
|
+
*
|
|
1120
|
+
* @internal
|
|
1121
|
+
*/
|
|
1122
|
+
async runFetch(msg, stream) {
|
|
1123
|
+
await this.#refuseUnsupported(stream, msg.requestId, "fetch", FetchError.id, "FETCH is not supported");
|
|
1124
|
+
}
|
|
1125
|
+
/**
|
|
1126
|
+
* Refuse a request NOT_SUPPORTED. Draft-14 gives each request its own error message,
|
|
1127
|
+
* `errorId14`, with the SUBSCRIBE_ERROR body; later drafts use REQUEST_ERROR.
|
|
1128
|
+
*/
|
|
1129
|
+
async #refuseUnsupported(stream, requestId, kind, errorId14, reasonPhrase) {
|
|
1130
|
+
const version = this.#session.version;
|
|
1131
|
+
const errorCode = toRequestCode("not_supported", kind, version);
|
|
930
1132
|
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);
|
|
1133
|
+
await stream.writer.u53(errorId14);
|
|
1134
|
+
await new SubscribeError({ requestId, errorCode, reasonPhrase }).encode(stream.writer, version);
|
|
938
1135
|
}
|
|
939
1136
|
else {
|
|
940
1137
|
await stream.writer.u53(RequestError.id);
|
|
941
1138
|
await new RequestError({
|
|
942
|
-
requestId: version === Version.DRAFT_15 || version === Version.DRAFT_16 ?
|
|
1139
|
+
requestId: version === Version.DRAFT_15 || version === Version.DRAFT_16 ? requestId : undefined,
|
|
943
1140
|
errorCode,
|
|
944
|
-
reasonPhrase
|
|
1141
|
+
reasonPhrase,
|
|
945
1142
|
}).encode(stream.writer, version);
|
|
946
1143
|
}
|
|
947
1144
|
stream.close();
|
|
@@ -1092,4 +1289,9 @@ function fillRange(fill, subscription, largest) {
|
|
|
1092
1289
|
until: end.object === undefined ? undefined : end.object + 1n,
|
|
1093
1290
|
};
|
|
1094
1291
|
}
|
|
1292
|
+
class UpdateFailed extends Error {
|
|
1293
|
+
constructor() {
|
|
1294
|
+
super("subscription update failed");
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1095
1297
|
//# sourceMappingURL=publisher.js.map
|