@moq/net 0.4.1 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/broadcast.d.ts +21 -7
- package/broadcast.d.ts.map +1 -1
- package/broadcast.js +38 -16
- package/broadcast.js.map +1 -1
- package/connection/accept.js +6 -4
- package/connection/accept.js.map +1 -1
- package/connection/connect.d.ts.map +1 -1
- package/connection/connect.js +5 -4
- package/connection/connect.js.map +1 -1
- package/connection/forward.js +54 -47
- package/connection/forward.js.map +1 -1
- package/connection/goaway.d.ts +34 -0
- package/connection/goaway.d.ts.map +1 -0
- package/connection/goaway.js +176 -0
- package/connection/goaway.js.map +1 -0
- package/connection/handshake.d.ts +4 -2
- package/connection/handshake.d.ts.map +1 -1
- package/connection/handshake.js +42 -10
- package/connection/handshake.js.map +1 -1
- package/connection/pool.d.ts +16 -1
- package/connection/pool.d.ts.map +1 -1
- package/connection/pool.js +32 -10
- package/connection/pool.js.map +1 -1
- package/connection/reload.d.ts.map +1 -1
- package/connection/reload.js +184 -30
- package/connection/reload.js.map +1 -1
- package/error.d.ts +11 -0
- package/error.d.ts.map +1 -1
- package/error.js +14 -0
- package/error.js.map +1 -1
- package/errors.d.ts +1 -1
- package/errors.d.ts.map +1 -1
- package/errors.js +1 -1
- package/errors.js.map +1 -1
- package/group.d.ts.map +1 -1
- package/group.js +5 -4
- package/group.js.map +1 -1
- package/hop.d.ts +8 -0
- package/hop.d.ts.map +1 -1
- package/hop.js +16 -0
- package/hop.js.map +1 -1
- package/ietf/adapter.d.ts +4 -0
- package/ietf/adapter.d.ts.map +1 -1
- package/ietf/adapter.js +19 -8
- package/ietf/adapter.js.map +1 -1
- package/ietf/cluster.d.ts +3 -3
- package/ietf/cluster.js +3 -3
- package/ietf/cluster.js.map +1 -1
- package/ietf/connection.d.ts.map +1 -1
- package/ietf/connection.js +110 -37
- package/ietf/connection.js.map +1 -1
- package/ietf/fetch.d.ts +7 -20
- package/ietf/fetch.d.ts.map +1 -1
- package/ietf/fetch.js +52 -22
- package/ietf/fetch.js.map +1 -1
- package/ietf/filter.d.ts +2 -0
- package/ietf/filter.d.ts.map +1 -1
- package/ietf/filter.js +4 -0
- package/ietf/filter.js.map +1 -1
- package/ietf/goaway.d.ts +9 -1
- package/ietf/goaway.d.ts.map +1 -1
- package/ietf/goaway.js +20 -0
- package/ietf/goaway.js.map +1 -1
- package/ietf/hidden.js +1 -1
- package/ietf/hidden.js.map +1 -1
- package/ietf/index.d.ts +1 -1
- package/ietf/index.d.ts.map +1 -1
- package/ietf/index.js +1 -1
- package/ietf/index.js.map +1 -1
- package/ietf/object.d.ts +7 -6
- package/ietf/object.d.ts.map +1 -1
- package/ietf/object.js +37 -33
- package/ietf/object.js.map +1 -1
- package/ietf/parameters.d.ts +7 -0
- package/ietf/parameters.d.ts.map +1 -1
- package/ietf/parameters.js +62 -12
- package/ietf/parameters.js.map +1 -1
- package/ietf/publish_namespace.d.ts +2 -2
- package/ietf/publish_namespace.js +2 -2
- package/ietf/publish_namespace.js.map +1 -1
- package/ietf/publisher.js +287 -172
- package/ietf/publisher.js.map +1 -1
- package/ietf/subscribe.d.ts +7 -1
- package/ietf/subscribe.d.ts.map +1 -1
- package/ietf/subscribe.js +22 -14
- package/ietf/subscribe.js.map +1 -1
- package/ietf/subscribe_namespace.d.ts.map +1 -1
- package/ietf/subscribe_namespace.js +2 -3
- package/ietf/subscribe_namespace.js.map +1 -1
- package/ietf/subscriber.js +57 -42
- package/ietf/subscriber.js.map +1 -1
- package/ietf/token.d.ts +2 -0
- package/ietf/token.d.ts.map +1 -0
- package/ietf/token.js +99 -0
- package/ietf/token.js.map +1 -0
- package/ietf/track.d.ts +4 -0
- package/ietf/track.d.ts.map +1 -1
- package/ietf/track.js +6 -20
- package/ietf/track.js.map +1 -1
- package/index.d.ts +1 -1
- package/index.d.ts.map +1 -1
- package/index.js.map +1 -1
- package/internal.d.ts +12 -3
- package/internal.d.ts.map +1 -1
- package/internal.js +32 -0
- package/internal.js.map +1 -1
- package/lite/announce.d.ts.map +1 -1
- package/lite/announce.js +7 -4
- package/lite/announce.js.map +1 -1
- package/lite/connection.d.ts.map +1 -1
- package/lite/connection.js +22 -7
- package/lite/connection.js.map +1 -1
- package/lite/datagram.d.ts +3 -2
- package/lite/datagram.d.ts.map +1 -1
- package/lite/datagram.js +7 -8
- package/lite/datagram.js.map +1 -1
- package/lite/group.d.ts +12 -8
- package/lite/group.d.ts.map +1 -1
- package/lite/group.js +37 -16
- package/lite/group.js.map +1 -1
- package/lite/message.d.ts +2 -2
- package/lite/message.d.ts.map +1 -1
- package/lite/message.js +14 -5
- package/lite/message.js.map +1 -1
- package/lite/publisher.js +19 -37
- package/lite/publisher.js.map +1 -1
- package/lite/setup.d.ts +1 -1
- package/lite/setup.d.ts.map +1 -1
- package/lite/setup.js +22 -19
- package/lite/setup.js.map +1 -1
- package/lite/subscriber.d.ts.map +1 -1
- package/lite/subscriber.js +206 -127
- package/lite/subscriber.js.map +1 -1
- package/origin.d.ts +9 -1
- package/origin.d.ts.map +1 -1
- package/origin.js +341 -146
- package/origin.js.map +1 -1
- package/package.json +2 -2
- package/stream.d.ts +77 -12
- package/stream.d.ts.map +1 -1
- package/stream.js +359 -122
- package/stream.js.map +1 -1
- package/tail.d.ts +7 -0
- package/tail.d.ts.map +1 -1
- package/tail.js +68 -22
- package/tail.js.map +1 -1
- package/track.d.ts +14 -3
- package/track.d.ts.map +1 -1
- package/track.js +170 -76
- package/track.js.map +1 -1
- package/util/abort.d.ts +2 -0
- package/util/abort.d.ts.map +1 -0
- package/util/abort.js +20 -0
- package/util/abort.js.map +1 -0
- package/util/u64.d.ts +39 -0
- package/util/u64.d.ts.map +1 -0
- package/util/u64.js +83 -0
- package/util/u64.js.map +1 -0
- package/util/varint.d.ts +29 -0
- package/util/varint.d.ts.map +1 -0
- package/util/varint.js +198 -0
- package/util/varint.js.map +1 -0
- package/varint.d.ts +10 -6
- package/varint.d.ts.map +1 -1
- package/varint.js +40 -237
- package/varint.js.map +1 -1
- package/wire.d.ts +17 -3
- package/wire.d.ts.map +1 -1
- package/wire.js.map +1 -1
- package/ietf/control.d.ts +0 -135
- package/ietf/control.d.ts.map +0 -1
- package/ietf/control.js +0 -242
- package/ietf/control.js.map +0 -1
package/broadcast.d.ts
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import { type GetPromise } from "@moq/signals";
|
|
7
7
|
import { Route } from "./hop";
|
|
8
|
+
import * as Path from "./path";
|
|
8
9
|
import * as track from "./track";
|
|
9
10
|
/**
|
|
10
11
|
* The write side of a broadcast.
|
|
@@ -19,7 +20,7 @@ export declare class Producer {
|
|
|
19
20
|
* Peek it synchronously (`undefined` while open), observe it reactively, or `await` it.
|
|
20
21
|
*/
|
|
21
22
|
get closed(): GetPromise<Error | null>;
|
|
22
|
-
/** A read handle for this broadcast. */
|
|
23
|
+
/** A read handle for this broadcast, named by the path the origin created it at. */
|
|
23
24
|
consume(): Consumer;
|
|
24
25
|
/** Insert a track that is served directly, without an on-demand request round-trip. */
|
|
25
26
|
insertTrack(track: track.Producer): void;
|
|
@@ -46,7 +47,9 @@ export declare class Producer {
|
|
|
46
47
|
* peers alike. {@link announce} brings it back.
|
|
47
48
|
*/
|
|
48
49
|
unannounce(): void;
|
|
49
|
-
/**
|
|
50
|
+
/** End the broadcast for good: retract it, serve no new tracks, and refuse a later {@link announce}. Idempotent. */
|
|
51
|
+
close(): void;
|
|
52
|
+
/** @deprecated A broadcast end carries no cause; call `close()` without one. */
|
|
50
53
|
close(abort?: Error): void;
|
|
51
54
|
}
|
|
52
55
|
/**
|
|
@@ -59,7 +62,16 @@ export declare class Producer {
|
|
|
59
62
|
*/
|
|
60
63
|
export declare class Consumer {
|
|
61
64
|
#private;
|
|
62
|
-
protected constructor(
|
|
65
|
+
protected constructor(shared?: never);
|
|
66
|
+
/**
|
|
67
|
+
* The path this handle names the broadcast by, which relative references in its catalog
|
|
68
|
+
* (hang's `broadcast` field) resolve against.
|
|
69
|
+
*
|
|
70
|
+
* An origin stamps each handle it hands out with the path it was requested at, relative to
|
|
71
|
+
* that origin handle's scope root, and a broadcast it created with the path it was created at.
|
|
72
|
+
* Empty for a standalone broadcast, which is then its own root: any `..` reference escapes.
|
|
73
|
+
*/
|
|
74
|
+
get path(): Path.Valid;
|
|
63
75
|
/**
|
|
64
76
|
* Settles once the broadcast closes: `null` on a clean close, or the abort {@link Error}.
|
|
65
77
|
* Peek it synchronously (`undefined` while open), observe it reactively, or `await` it.
|
|
@@ -71,8 +83,8 @@ export declare class Consumer {
|
|
|
71
83
|
/**
|
|
72
84
|
* Return another handle to the same broadcast, reference-counted with this one.
|
|
73
85
|
*
|
|
74
|
-
* Both handles read the same tracks and share one {@link closed}
|
|
75
|
-
* closes only once *every* handle has {@link close}d. Used by the connection's per-path
|
|
86
|
+
* Both handles read the same tracks, carry the same {@link path}, and share one {@link closed}
|
|
87
|
+
* state; the broadcast closes only once *every* handle has {@link close}d. Used by the connection's per-path
|
|
76
88
|
* consume cache to share one subscription across callers. Subclasses that resolve info over
|
|
77
89
|
* the wire override this to preserve their type (see the wire layer's consumed broadcast).
|
|
78
90
|
*/
|
|
@@ -81,9 +93,11 @@ export declare class Consumer {
|
|
|
81
93
|
/** Get a lazy handle for a track on this broadcast. Repeat subscriptions dedupe onto one upstream subscription. */
|
|
82
94
|
track(name: string): track.Consumer;
|
|
83
95
|
/**
|
|
84
|
-
* Release this handle. The broadcast is closed
|
|
85
|
-
*
|
|
96
|
+
* Release this handle. The broadcast is closed once this was the last live handle;
|
|
97
|
+
* while other {@link clone}s remain open it stays live.
|
|
86
98
|
*/
|
|
99
|
+
close(): void;
|
|
100
|
+
/** @deprecated A broadcast end carries no cause; call `close()` without one. */
|
|
87
101
|
close(abort?: Error): void;
|
|
88
102
|
}
|
|
89
103
|
//# sourceMappingURL=broadcast.d.ts.map
|
package/broadcast.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"broadcast.d.ts","sourceRoot":"","sources":["../src/broadcast.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,KAAK,UAAU,EAAgB,MAAM,cAAc,CAAC;AAG7D,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAEjC,OAAO,KAAK,KAAK,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"broadcast.d.ts","sourceRoot":"","sources":["../src/broadcast.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,KAAK,UAAU,EAAgB,MAAM,cAAc,CAAC;AAG7D,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAEjC,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,OAAO,KAAK,KAAK,MAAM,YAAY,CAAC;AAiJpC;;;;GAIG;AACH,qBAAa,QAAQ;;IAKpB,cAEC;IAYD;;;OAGG;IACH,IAAI,MAAM,IAAI,UAAU,CAAC,KAAK,GAAG,IAAI,CAAC,CAErC;IAED,oFAAoF;IACpF,OAAO,IAAI,QAAQ,CAElB;IAeD,uFAAuF;IACvF,WAAW,CAAC,KAAK,EAAE,KAAK,CAAC,QAAQ,GAAG,IAAI,CAiBvC;IAED,6EAA6E;IAC7E,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,GAAE,OAAO,CAAC,KAAK,CAAC,IAAI,CAAM,GAAG,KAAK,CAAC,QAAQ,CAIxE;IAED,kDAAkD;IAClD,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAE9B;IAED,wDAAwD;IACxD,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,KAAK,CAAC,QAAQ,CAElC;IAWD;;;;;;;OAOG;IACH,QAAQ,CAAC,KAAK,GAAE,KAAK,GAAG;QAAE,IAAI,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;QAAC,IAAI,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,GAAG,MAAM,CAAA;KAAkB,GAAG,IAAI,CAMrG;IAED;;;OAGG;IACH,UAAU,IAAI,IAAI,CAEjB;IAED,oHAAoH;IACpH,KAAK,IAAI,IAAI,CAAC;IACd,gFAAgF;IAChF,KAAK,CAAC,KAAK,CAAC,EAAE,KAAK,GAAG,IAAI,CAAC;CAM3B;AAYD;;;;;;;GAOG;AACH,qBAAa,QAAQ;;IAOpB,SAAS,aAAa,MAAM,CAAC,EAAE,KAAK,EAAE;IAqBtC;;;;;;;OAOG;IACH,IAAI,IAAI,IAAI,IAAI,CAAC,KAAK,CAErB;IAED;;;;;;OAMG;IACH,IAAI,MAAM,IAAI,UAAU,CAAC,KAAK,GAAG,IAAI,CAAC,CAErC;IAED;;;;;;;OAOG;IACH,KAAK,IAAI,QAAQ,CAEhB;IAID,SAAS,CAAC,UAAU,IAAI,KAAK,CAE5B;IAED,mHAAmH;IACnH,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,KAAK,CAAC,QAAQ,CAElC;IAeD;;;OAGG;IACH,KAAK,IAAI,IAAI,CAAC;IACd,gFAAgF;IAChF,KAAK,CAAC,KAAK,CAAC,EAAE,KAAK,GAAG,IAAI,CAAC;CAO3B"}
|
package/broadcast.js
CHANGED
|
@@ -8,9 +8,12 @@ import { Once, Signal } from "@moq/signals";
|
|
|
8
8
|
import { NotFound } from "./error.js";
|
|
9
9
|
import { Route } from "./hop.js";
|
|
10
10
|
import { hooks } from "./internal.js";
|
|
11
|
+
import * as Path from "./path.js";
|
|
11
12
|
import * as track from "./track.js";
|
|
13
|
+
import { untilAborted } from "./util/abort.js";
|
|
12
14
|
import { registerWire, trackOf } from "./wire.js";
|
|
13
15
|
let attachAnnouncer;
|
|
16
|
+
let stampProducer;
|
|
14
17
|
/** Reactive backing state shared by broadcast producers and consumers. */
|
|
15
18
|
class BroadcastState {
|
|
16
19
|
requested = new Signal([]);
|
|
@@ -99,11 +102,12 @@ async function resolveTrackInfo(state, name) {
|
|
|
99
102
|
// requested sequence. The default for a produced broadcast; the consuming wire layer
|
|
100
103
|
// overrides it to fetch over the network (or to reject when the transport has no FETCH).
|
|
101
104
|
async function fetchGroup(state, name, sequence, options = {}) {
|
|
105
|
+
options.signal?.throwIfAborted();
|
|
102
106
|
const subscriber = subscribe(state, name, { priority: options.priority });
|
|
103
107
|
hooks.exemptFetch(subscriber);
|
|
104
108
|
try {
|
|
105
109
|
for (;;) {
|
|
106
|
-
const group = await subscriber.recvGroup();
|
|
110
|
+
const group = await untilAborted(subscriber.recvGroup(), options.signal);
|
|
107
111
|
if (!group)
|
|
108
112
|
throw new NotFound(`group ${sequence}`);
|
|
109
113
|
if (group.sequence === sequence) {
|
|
@@ -131,6 +135,7 @@ async function fetchGroup(state, name, sequence, options = {}) {
|
|
|
131
135
|
export class Producer {
|
|
132
136
|
#state = new BroadcastState();
|
|
133
137
|
#announcer;
|
|
138
|
+
#path = Path.empty();
|
|
134
139
|
constructor() {
|
|
135
140
|
registerWire(this, this.#wire(false));
|
|
136
141
|
}
|
|
@@ -139,6 +144,9 @@ export class Producer {
|
|
|
139
144
|
producer.#announcer = announcer;
|
|
140
145
|
};
|
|
141
146
|
hooks.attachAnnouncer = attachAnnouncer;
|
|
147
|
+
stampProducer = (producer, path) => {
|
|
148
|
+
producer.#path = path;
|
|
149
|
+
};
|
|
142
150
|
}
|
|
143
151
|
/**
|
|
144
152
|
* Settles once the broadcast closes: `null` on a clean close, or the abort {@link Error}.
|
|
@@ -147,9 +155,9 @@ export class Producer {
|
|
|
147
155
|
get closed() {
|
|
148
156
|
return this.#state.closed;
|
|
149
157
|
}
|
|
150
|
-
/** A read handle for this broadcast. */
|
|
158
|
+
/** A read handle for this broadcast, named by the path the origin created it at. */
|
|
151
159
|
consume() {
|
|
152
|
-
return makeConsumer(this.#state);
|
|
160
|
+
return makeConsumer({ state: this.#state, path: this.#path });
|
|
153
161
|
}
|
|
154
162
|
async #requested() {
|
|
155
163
|
for (;;) {
|
|
@@ -225,7 +233,6 @@ export class Producer {
|
|
|
225
233
|
unannounce() {
|
|
226
234
|
this.#announcer?.unannounce();
|
|
227
235
|
}
|
|
228
|
-
/** Close the broadcast, optionally with an error to abort waiters. Idempotent. */
|
|
229
236
|
close(abort) {
|
|
230
237
|
this.#announcer?.unannounce();
|
|
231
238
|
this.#announcer = undefined;
|
|
@@ -245,10 +252,12 @@ let makeConsumer;
|
|
|
245
252
|
*/
|
|
246
253
|
export class Consumer {
|
|
247
254
|
#state;
|
|
255
|
+
#path;
|
|
248
256
|
// Guards against a double close() on this handle over-decrementing the consumer count.
|
|
249
257
|
#closed = false;
|
|
250
|
-
constructor(
|
|
251
|
-
this.#state = state ?? new BroadcastState();
|
|
258
|
+
constructor(shared) {
|
|
259
|
+
this.#state = shared?.state ?? new BroadcastState();
|
|
260
|
+
this.#path = shared?.path ?? Path.empty();
|
|
252
261
|
this.#state.consumers++;
|
|
253
262
|
registerWire(this, {
|
|
254
263
|
subscribe: (name, options) => subscribe(this.#state, name, options, true),
|
|
@@ -258,7 +267,24 @@ export class Consumer {
|
|
|
258
267
|
});
|
|
259
268
|
}
|
|
260
269
|
static {
|
|
261
|
-
makeConsumer = (
|
|
270
|
+
makeConsumer = (shared) => new Consumer(shared);
|
|
271
|
+
hooks.stampPath = (target, path) => {
|
|
272
|
+
if (target instanceof Consumer)
|
|
273
|
+
target.#path = path;
|
|
274
|
+
else
|
|
275
|
+
stampProducer(target, path);
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* The path this handle names the broadcast by, which relative references in its catalog
|
|
280
|
+
* (hang's `broadcast` field) resolve against.
|
|
281
|
+
*
|
|
282
|
+
* An origin stamps each handle it hands out with the path it was requested at, relative to
|
|
283
|
+
* that origin handle's scope root, and a broadcast it created with the path it was created at.
|
|
284
|
+
* Empty for a standalone broadcast, which is then its own root: any `..` reference escapes.
|
|
285
|
+
*/
|
|
286
|
+
get path() {
|
|
287
|
+
return this.#path;
|
|
262
288
|
}
|
|
263
289
|
/**
|
|
264
290
|
* Settles once the broadcast closes: `null` on a clean close, or the abort {@link Error}.
|
|
@@ -273,18 +299,18 @@ export class Consumer {
|
|
|
273
299
|
/**
|
|
274
300
|
* Return another handle to the same broadcast, reference-counted with this one.
|
|
275
301
|
*
|
|
276
|
-
* Both handles read the same tracks and share one {@link closed}
|
|
277
|
-
* closes only once *every* handle has {@link close}d. Used by the connection's per-path
|
|
302
|
+
* Both handles read the same tracks, carry the same {@link path}, and share one {@link closed}
|
|
303
|
+
* state; the broadcast closes only once *every* handle has {@link close}d. Used by the connection's per-path
|
|
278
304
|
* consume cache to share one subscription across callers. Subclasses that resolve info over
|
|
279
305
|
* the wire override this to preserve their type (see the wire layer's consumed broadcast).
|
|
280
306
|
*/
|
|
281
307
|
clone() {
|
|
282
308
|
return new Consumer(this.shareState());
|
|
283
309
|
}
|
|
284
|
-
// Hand this consumer's backing state to a clone. Opaque (`never`) so the state type
|
|
285
|
-
// unexported; a subclass passes it straight back into its own `super(...)`.
|
|
310
|
+
// Hand this consumer's backing state and path to a clone. Opaque (`never`) so the state type
|
|
311
|
+
// stays unexported; a subclass passes it straight back into its own `super(...)`.
|
|
286
312
|
shareState() {
|
|
287
|
-
return this.#state;
|
|
313
|
+
return { state: this.#state, path: this.#path };
|
|
288
314
|
}
|
|
289
315
|
/** Get a lazy handle for a track on this broadcast. Repeat subscriptions dedupe onto one upstream subscription. */
|
|
290
316
|
track(name) {
|
|
@@ -303,10 +329,6 @@ export class Consumer {
|
|
|
303
329
|
await Signal.race(this.#state.requested, this.#state.closed);
|
|
304
330
|
}
|
|
305
331
|
}
|
|
306
|
-
/**
|
|
307
|
-
* Release this handle. The broadcast is closed (optionally with an error to abort waiters)
|
|
308
|
-
* once this was the last live handle; while other {@link clone}s remain open it stays live.
|
|
309
|
-
*/
|
|
310
332
|
close(abort) {
|
|
311
333
|
if (this.#closed)
|
|
312
334
|
return;
|
package/broadcast.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"broadc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,CAAC;IAED,mHAAmH;IACnH,KAAK,CAAC,IAAY;QACjB,OAAO,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;IAC5B,CAAC;IAED,KAAK,CAAC,UAAU;QACf,SAAS,CAAC;YACT,MAAM,OAAO,GAAG,cAAc,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;YAC5C,IAAI,OAAO;gBAAE,OAAO,OAAO,CAAC;YAE5B,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;YACzC,IAAI,MAAM,YAAY,KAAK;gBAAE,MAAM,MAAM,CAAC;YAC1C,IAAI,MAAM,KAAK,SAAS;gBAAE,OAAO,SAAS,CAAC;YAE3C,MAAM,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QAC9D,CAAC;IACF,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,KAAa;QAClB,IAAI,IAAI,CAAC,OAAO;YAAE,OAAO;QACzB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QACpB,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC,SAAS,GAAG,CAAC;YAAE,OAAO;QACxC,UAAU,CAAC,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAChC,CAAC;CACD","sourcesContent":["/**\n * Broadcast role handles: a named collection of tracks produced by a publisher.\n *\n * @module\n */\nimport { type GetPromise, Once, Signal } from \"@moq/signals\";\nimport { NotFound } from \"./error.ts\";\nimport type { Consumer as GroupConsumer } from \"./group.ts\";\nimport { Route } from \"./hop.ts\";\nimport { hooks, type TrackSequence } from \"./internal.ts\";\nimport * as track from \"./track.ts\";\nimport { registerWire, trackOf, type Broadcast as Wire } from \"./wire.ts\";\n\n/** The origin callback a created broadcast uses to advertise its exact path. @internal */\nexport interface Announcer {\n\t/** Advertise or re-price this broadcast's path. */\n\tannounce(route: Route): void;\n\t/** Retract the advertisement from local consumers and peers alike. */\n\tunannounce(): void;\n}\n\nlet attachAnnouncer: (producer: Producer, announcer: Announcer) => void;\n\n/** Reactive backing state shared by broadcast producers and consumers. */\nclass BroadcastState {\n\trequested = new Signal<track.Request[]>([]);\n\tpending = new Set<track.Request>();\n\tclosed = new Once<Error | null>();\n\ttracks = new Map<string, track.Producer>();\n\tsequences = new Map<string, TrackSequence>();\n\t// Live consumer handles sharing this state (see {@link Consumer.clone}). The broadcast\n\t// closes once the last one closes, so a shared consumer can be handed to several callers.\n\tconsumers = 0;\n}\n\nfunction dequeueRequest(state: BroadcastState): track.Request | undefined {\n\tconst requested = state.requested.peek();\n\trequested.sort((a, b) => a.priority - b.priority);\n\treturn requested.pop();\n}\n\n// Close the broadcast and reject any requests still pending in the queue, so a\n// subscriber blocked on the track's info() or group reads is unblocked rather\n// than left waiting on a producer that will never be served.\n//\n// Once.set throws on a second settle, and the producer and each consumer handle close\n// independently, so this has to be idempotent.\nfunction closeState(state: BroadcastState, abort?: Error) {\n\tif (state.closed.peek() !== undefined) return;\n\tstate.closed.set(abort ?? null);\n\tfor (const request of state.pending) request.reject(abort);\n\tstate.requested.mutate((requests) => {\n\t\trequests.length = 0;\n\t});\n}\n\n// `register` is set on the subscribing (consumer) side: the fresh producer is cached in\n// `state.tracks` so repeat subscriptions to the same track fan out from one upstream subscription\n// instead of opening a new one, mirroring the Rust `broadcast::Consumer::track` weak-dedup. The\n// consumer wire watches the producer's demand ({@link track.Producer.used}) and tears the upstream\n// down once its last subscriber leaves, closing the producer, which evicts the cache entry below.\n// The publishing side leaves `register` false: `state.tracks` there holds only the tracks the app\n// inserted, and a dynamic serve stays one request per peer subscription.\nfunction subscribe(\n\tstate: BroadcastState,\n\tname: string,\n\toptions: track.Subscription = {},\n\tregister = false,\n): track.Subscriber {\n\tif (state.closed.peek() !== undefined) {\n\t\tthrow new Error(`broadcast is closed: ${state.closed.peek()}`);\n\t}\n\n\tconst existing = state.tracks.get(name);\n\tif (existing) {\n\t\tif (existing.closed.peek() === undefined) return existing.subscribe(options);\n\t\tstate.tracks.delete(name);\n\t}\n\n\tconst producer = new track.Producer(name);\n\tconst subscriber = producer.subscribe(options);\n\n\tif (register) {\n\t\tstate.tracks.set(name, producer);\n\t\t// Drop the cache entry once the upstream closes (the wire tears it down when its last\n\t\t// subscriber leaves), so a later subscribe re-opens it.\n\t\tvoid producer.closed.then(() => {\n\t\t\tif (state.tracks.get(name) === producer) state.tracks.delete(name);\n\t\t});\n\t}\n\n\tstate.requested.mutate((requested) => {\n\t\trequested.push(hooks.makeRequest({ name, producer, sequences: state.sequences, pending: state.pending }));\n\t});\n\n\treturn subscriber;\n}\n\nasync function resolveTrackInfo(state: BroadcastState, name: string): Promise<track.Info> {\n\tconst existing = state.tracks.get(name);\n\tif (existing && existing.closed.peek() === undefined) {\n\t\treturn existing.info();\n\t}\n\n\tif (state.closed.peek() !== undefined) {\n\t\treturn Promise.reject(new Error(`broadcast is closed: ${state.closed.peek()}`));\n\t}\n\n\tconst producer = new track.Producer(name);\n\tstate.requested.mutate((requested) => {\n\t\trequested.push(hooks.makeRequest({ name, producer, sequences: state.sequences, pending: state.pending }));\n\t});\n\n\ttry {\n\t\treturn await producer.info();\n\t} finally {\n\t\tproducer.close();\n\t}\n}\n\n// Serve a group from the local retained window by subscribing and scanning to the\n// requested sequence. The default for a produced broadcast; the consuming wire layer\n// overrides it to fetch over the network (or to reject when the transport has no FETCH).\nasync function fetchGroup(\n\tstate: BroadcastState,\n\tname: string,\n\tsequence: number,\n\toptions: track.FetchGroupOptions = {},\n): Promise<GroupConsumer> {\n\tconst subscriber = subscribe(state, name, { priority: options.priority });\n\thooks.exemptFetch(subscriber);\n\ttry {\n\t\tfor (;;) {\n\t\t\tconst group = await subscriber.recvGroup();\n\t\t\tif (!group) throw new NotFound(`group ${sequence}`);\n\t\t\tif (group.sequence === sequence) {\n\t\t\t\t// Close the subscription when the returned group finishes, not now: an\n\t\t\t\t// in-progress group must keep receiving frames for its lifetime (mirrors\n\t\t\t\t// Rust poll_fetch). Also fires if the caller closes the group early.\n\t\t\t\tvoid group.closed.then(() => subscriber.close());\n\t\t\t\treturn group;\n\t\t\t}\n\n\t\t\tgroup.close();\n\t\t\tif (group.sequence > sequence) throw new NotFound(`group ${sequence}`);\n\t\t}\n\t} catch (err) {\n\t\tsubscriber.close();\n\t\tthrow err;\n\t}\n}\n\n/**\n * The write side of a broadcast.\n *\n * @public\n */\nexport class Producer {\n\t#state = new BroadcastState();\n\t#announcer?: Announcer;\n\n\tconstructor() {\n\t\tregisterWire(this, this.#wire(false));\n\t}\n\n\tstatic {\n\t\tattachAnnouncer = (producer, announcer) => {\n\t\t\tproducer.#announcer = announcer;\n\t\t};\n\t\thooks.attachAnnouncer = attachAnnouncer;\n\t}\n\n\t/**\n\t * Settles once the broadcast closes: `null` on a clean close, or the abort {@link Error}.\n\t * Peek it synchronously (`undefined` while open), observe it reactively, or `await` it.\n\t */\n\tget closed(): GetPromise<Error | null> {\n\t\treturn this.#state.closed;\n\t}\n\n\t/** A read handle for this broadcast. */\n\tconsume(): Consumer {\n\t\treturn makeConsumer(this.#state);\n\t}\n\n\tasync #requested(): Promise<track.Request | undefined> {\n\t\tfor (;;) {\n\t\t\tconst request = dequeueRequest(this.#state);\n\t\t\tif (request) return request;\n\n\t\t\tconst closed = this.#state.closed.peek();\n\t\t\tif (closed instanceof Error) throw closed;\n\t\t\tif (closed !== undefined) return undefined;\n\n\t\t\tawait Signal.race(this.#state.requested, this.#state.closed);\n\t\t}\n\t}\n\n\t/** Insert a track that is served directly, without an on-demand request round-trip. */\n\tinsertTrack(track: track.Producer): void {\n\t\tif (this.#state.closed.peek() !== undefined) {\n\t\t\tthrow new Error(`broadcast is closed: ${this.#state.closed.peek()}`);\n\t\t}\n\n\t\tconst existing = this.#state.tracks.get(track.name);\n\t\tif (existing && existing.closed.peek() === undefined) {\n\t\t\tthrow new Error(`duplicate track: ${track.name}`);\n\t\t}\n\n\t\tthis.#state.tracks.set(track.name, track);\n\n\t\tvoid track.closed.then(() => {\n\t\t\tif (this.#state.tracks.get(track.name) === track) {\n\t\t\t\tthis.#state.tracks.delete(track.name);\n\t\t\t}\n\t\t});\n\t}\n\n\t/** Create a track, insert it into the broadcast, and return its producer. */\n\tcreateTrack(name: string, info: Partial<track.Info> = {}): track.Producer {\n\t\tconst producer = new track.Producer(name).accept(info);\n\t\tthis.insertTrack(producer);\n\t\treturn producer;\n\t}\n\n\t/** Remove a statically inserted track by name. */\n\tremoveTrack(name: string): void {\n\t\tthis.#state.tracks.delete(name);\n\t}\n\n\t/** A lazy read handle for a track on this broadcast. */\n\ttrack(name: string): track.Consumer {\n\t\treturn trackOf(name, this);\n\t}\n\n\t#wire(register: boolean): Wire {\n\t\treturn {\n\t\t\tsubscribe: (name, options) => subscribe(this.#state, name, options, register),\n\t\t\tresolveTrackInfo: (name) => resolveTrackInfo(this.#state, name),\n\t\t\tfetchGroup: (name, sequence, options) => fetchGroup(this.#state, name, sequence, options),\n\t\t\trequested: () => this.#requested(),\n\t\t};\n\t}\n\n\t/**\n\t * Advertise this broadcast's exact path, or re-price a standing advertisement in place.\n\t *\n\t * Call it once the tracks a subscriber needs first (a catalog) exist. Until then the\n\t * broadcast exists for nobody, on its own origin or at a peer. Retracts on\n\t * {@link unannounce} or {@link close}. Throws if this producer was not created through an\n\t * origin, or if the broadcast is already closed.\n\t */\n\tannounce(route: Route | { hops?: Route[\"hops\"]; cost?: Route[\"cost\"] | bigint } = Route.default): void {\n\t\tif (this.#state.closed.peek() !== undefined) {\n\t\t\tthrow new Error(`broadcast is closed: ${this.#state.closed.peek()}`);\n\t\t}\n\t\tif (!this.#announcer) throw new Error(\"broadcast is not attached to an origin\");\n\t\tthis.#announcer.announce(Route.normalize(route));\n\t}\n\n\t/**\n\t * Retract the advertisement of this broadcast's path, if any, from local consumers and\n\t * peers alike. {@link announce} brings it back.\n\t */\n\tunannounce(): void {\n\t\tthis.#announcer?.unannounce();\n\t}\n\n\t/** Close the broadcast, optionally with an error to abort waiters. Idempotent. */\n\tclose(abort?: Error) {\n\t\tthis.#announcer?.unannounce();\n\t\tthis.#announcer = undefined;\n\t\tcloseState(this.#state, abort);\n\t}\n}\n\n// Constructs a Consumer from within this module without exposing a public constructor\n// that would leak the unexported BroadcastState. Assigned in the class's static block.\nlet makeConsumer: (state: BroadcastState) => Consumer;\n\n/**\n * The read side of a broadcast.\n *\n * Created internally: obtain one from {@link Producer.consume} or an origin request.\n * The wire layers subclass it to resolve tracks over the network.\n *\n * @public\n */\nexport class Consumer {\n\t#state: BroadcastState;\n\n\t// Guards against a double close() on this handle over-decrementing the consumer count.\n\t#closed = false;\n\n\tprotected constructor(state?: never);\n\tprotected constructor(state?: BroadcastState) {\n\t\tthis.#state = state ?? new BroadcastState();\n\t\tthis.#state.consumers++;\n\t\tregisterWire(this, {\n\t\t\tsubscribe: (name, options) => subscribe(this.#state, name, options, true),\n\t\t\tresolveTrackInfo: (name) => resolveTrackInfo(this.#state, name),\n\t\t\tfetchGroup: (name, sequence, options) => fetchGroup(this.#state, name, sequence, options),\n\t\t\trequested: () => this.#requested(),\n\t\t});\n\t}\n\n\tstatic {\n\t\tmakeConsumer = (state) => new Consumer(state as never);\n\t}\n\n\t/**\n\t * Settles once the broadcast closes: `null` on a clean close, or the abort {@link Error}.\n\t * Peek it synchronously (`undefined` while open), observe it reactively, or `await` it.\n\t *\n\t * Shared by every {@link clone}: it settles once the last handle closes. The subscribing\n\t * wire layer peeks it to evict a closed entry from its per-path consume cache.\n\t */\n\tget closed(): GetPromise<Error | null> {\n\t\treturn this.#state.closed;\n\t}\n\n\t/**\n\t * Return another handle to the same broadcast, reference-counted with this one.\n\t *\n\t * Both handles read the same tracks and share one {@link closed} state; the broadcast\n\t * closes only once *every* handle has {@link close}d. Used by the connection's per-path\n\t * consume cache to share one subscription across callers. Subclasses that resolve info over\n\t * the wire override this to preserve their type (see the wire layer's consumed broadcast).\n\t */\n\tclone(): Consumer {\n\t\treturn new Consumer(this.shareState());\n\t}\n\n\t// Hand this consumer's backing state to a clone. Opaque (`never`) so the state type stays\n\t// unexported; a subclass passes it straight back into its own `super(...)`.\n\tprotected shareState(): never {\n\t\treturn this.#state as never;\n\t}\n\n\t/** Get a lazy handle for a track on this broadcast. Repeat subscriptions dedupe onto one upstream subscription. */\n\ttrack(name: string): track.Consumer {\n\t\treturn trackOf(name, this);\n\t}\n\n\tasync #requested(): Promise<track.Request | undefined> {\n\t\tfor (;;) {\n\t\t\tconst request = dequeueRequest(this.#state);\n\t\t\tif (request) return request;\n\n\t\t\tconst closed = this.#state.closed.peek();\n\t\t\tif (closed instanceof Error) throw closed;\n\t\t\tif (closed !== undefined) return undefined;\n\n\t\t\tawait Signal.race(this.#state.requested, this.#state.closed);\n\t\t}\n\t}\n\n\t/**\n\t * Release this handle. The broadcast is closed (optionally with an error to abort waiters)\n\t * once this was the last live handle; while other {@link clone}s remain open it stays live.\n\t */\n\tclose(abort?: Error) {\n\t\tif (this.#closed) return;\n\t\tthis.#closed = true;\n\t\tif (--this.#state.consumers > 0) return;\n\t\tcloseState(this.#state, abort);\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"broadcast.js","sourceRoot":"","sources":["../src/broadcast.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAmB,IAAI,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAC7D,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEtC,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AACjC,OAAO,EAAE,KAAK,EAAsB,MAAM,eAAe,CAAC;AAC1D,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,OAAO,KAAK,KAAK,MAAM,YAAY,CAAC;AACpC,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAE,YAAY,EAAE,OAAO,EAA0B,MAAM,WAAW,CAAC;AAU1E,IAAI,eAAmE,CAAC;AAC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ourcesContent":["/**\n * Broadcast role handles: a named collection of tracks produced by a publisher.\n *\n * @module\n */\nimport { type GetPromise, Once, Signal } from \"@moq/signals\";\nimport { NotFound } from \"./error.ts\";\nimport type { Consumer as GroupConsumer } from \"./group.ts\";\nimport { Route } from \"./hop.ts\";\nimport { hooks, type TrackSequence } from \"./internal.ts\";\nimport * as Path from \"./path.ts\";\nimport * as track from \"./track.ts\";\nimport { untilAborted } from \"./util/abort.ts\";\nimport { registerWire, trackOf, type Broadcast as Wire } from \"./wire.ts\";\n\n/** The origin callback a created broadcast uses to advertise its exact path. @internal */\nexport interface Announcer {\n\t/** Advertise or re-price this broadcast's path. */\n\tannounce(route: Route): void;\n\t/** Retract the advertisement from local consumers and peers alike. */\n\tunannounce(): void;\n}\n\nlet attachAnnouncer: (producer: Producer, announcer: Announcer) => void;\nlet stampProducer: (producer: Producer, path: Path.Valid) => void;\n\n/** Reactive backing state shared by broadcast producers and consumers. */\nclass BroadcastState {\n\trequested = new Signal<track.Request[]>([]);\n\tpending = new Set<track.Request>();\n\tclosed = new Once<Error | null>();\n\ttracks = new Map<string, track.Producer>();\n\tsequences = new Map<string, TrackSequence>();\n\t// Live consumer handles sharing this state (see {@link Consumer.clone}). The broadcast\n\t// closes once the last one closes, so a shared consumer can be handed to several callers.\n\tconsumers = 0;\n}\n\nfunction dequeueRequest(state: BroadcastState): track.Request | undefined {\n\tconst requested = state.requested.peek();\n\trequested.sort((a, b) => a.priority - b.priority);\n\treturn requested.pop();\n}\n\n// Close the broadcast and reject any requests still pending in the queue, so a\n// subscriber blocked on the track's info() or group reads is unblocked rather\n// than left waiting on a producer that will never be served.\n//\n// Once.set throws on a second settle, and the producer and each consumer handle close\n// independently, so this has to be idempotent.\nfunction closeState(state: BroadcastState, abort?: Error) {\n\tif (state.closed.peek() !== undefined) return;\n\tstate.closed.set(abort ?? null);\n\tfor (const request of state.pending) request.reject(abort);\n\tstate.requested.mutate((requests) => {\n\t\trequests.length = 0;\n\t});\n}\n\n// `register` is set on the subscribing (consumer) side: the fresh producer is cached in\n// `state.tracks` so repeat subscriptions to the same track fan out from one upstream subscription\n// instead of opening a new one, mirroring the Rust `broadcast::Consumer::track` weak-dedup. The\n// consumer wire watches the producer's demand ({@link track.Producer.used}) and tears the upstream\n// down once its last subscriber leaves, closing the producer, which evicts the cache entry below.\n// The publishing side leaves `register` false: `state.tracks` there holds only the tracks the app\n// inserted, and a dynamic serve stays one request per peer subscription.\nfunction subscribe(\n\tstate: BroadcastState,\n\tname: string,\n\toptions: track.Subscription = {},\n\tregister = false,\n): track.Subscriber {\n\tif (state.closed.peek() !== undefined) {\n\t\tthrow new Error(`broadcast is closed: ${state.closed.peek()}`);\n\t}\n\n\tconst existing = state.tracks.get(name);\n\tif (existing) {\n\t\tif (existing.closed.peek() === undefined) return existing.subscribe(options);\n\t\tstate.tracks.delete(name);\n\t}\n\n\tconst producer = new track.Producer(name);\n\tconst subscriber = producer.subscribe(options);\n\n\tif (register) {\n\t\tstate.tracks.set(name, producer);\n\t\t// Drop the cache entry once the upstream closes (the wire tears it down when its last\n\t\t// subscriber leaves), so a later subscribe re-opens it.\n\t\tvoid producer.closed.then(() => {\n\t\t\tif (state.tracks.get(name) === producer) state.tracks.delete(name);\n\t\t});\n\t}\n\n\tstate.requested.mutate((requested) => {\n\t\trequested.push(hooks.makeRequest({ name, producer, sequences: state.sequences, pending: state.pending }));\n\t});\n\n\treturn subscriber;\n}\n\nasync function resolveTrackInfo(state: BroadcastState, name: string): Promise<track.Info> {\n\tconst existing = state.tracks.get(name);\n\tif (existing && existing.closed.peek() === undefined) {\n\t\treturn existing.info();\n\t}\n\n\tif (state.closed.peek() !== undefined) {\n\t\treturn Promise.reject(new Error(`broadcast is closed: ${state.closed.peek()}`));\n\t}\n\n\tconst producer = new track.Producer(name);\n\tstate.requested.mutate((requested) => {\n\t\trequested.push(hooks.makeRequest({ name, producer, sequences: state.sequences, pending: state.pending }));\n\t});\n\n\ttry {\n\t\treturn await producer.info();\n\t} finally {\n\t\tproducer.close();\n\t}\n}\n\n// Serve a group from the local retained window by subscribing and scanning to the\n// requested sequence. The default for a produced broadcast; the consuming wire layer\n// overrides it to fetch over the network (or to reject when the transport has no FETCH).\nasync function fetchGroup(\n\tstate: BroadcastState,\n\tname: string,\n\tsequence: number,\n\toptions: track.FetchGroupOptions = {},\n): Promise<GroupConsumer> {\n\toptions.signal?.throwIfAborted();\n\tconst subscriber = subscribe(state, name, { priority: options.priority });\n\thooks.exemptFetch(subscriber);\n\ttry {\n\t\tfor (;;) {\n\t\t\tconst group = await untilAborted(subscriber.recvGroup(), options.signal);\n\t\t\tif (!group) throw new NotFound(`group ${sequence}`);\n\t\t\tif (group.sequence === sequence) {\n\t\t\t\t// Close the subscription when the returned group finishes, not now: an\n\t\t\t\t// in-progress group must keep receiving frames for its lifetime (mirrors\n\t\t\t\t// Rust poll_fetch). Also fires if the caller closes the group early.\n\t\t\t\tvoid group.closed.then(() => subscriber.close());\n\t\t\t\treturn group;\n\t\t\t}\n\n\t\t\tgroup.close();\n\t\t\tif (group.sequence > sequence) throw new NotFound(`group ${sequence}`);\n\t\t}\n\t} catch (err) {\n\t\tsubscriber.close();\n\t\tthrow err;\n\t}\n}\n\n/**\n * The write side of a broadcast.\n *\n * @public\n */\nexport class Producer {\n\t#state = new BroadcastState();\n\t#announcer?: Announcer;\n\t#path = Path.empty();\n\n\tconstructor() {\n\t\tregisterWire(this, this.#wire(false));\n\t}\n\n\tstatic {\n\t\tattachAnnouncer = (producer, announcer) => {\n\t\t\tproducer.#announcer = announcer;\n\t\t};\n\t\thooks.attachAnnouncer = attachAnnouncer;\n\t\tstampProducer = (producer, path) => {\n\t\t\tproducer.#path = path;\n\t\t};\n\t}\n\n\t/**\n\t * Settles once the broadcast closes: `null` on a clean close, or the abort {@link Error}.\n\t * Peek it synchronously (`undefined` while open), observe it reactively, or `await` it.\n\t */\n\tget closed(): GetPromise<Error | null> {\n\t\treturn this.#state.closed;\n\t}\n\n\t/** A read handle for this broadcast, named by the path the origin created it at. */\n\tconsume(): Consumer {\n\t\treturn makeConsumer({ state: this.#state, path: this.#path });\n\t}\n\n\tasync #requested(): Promise<track.Request | undefined> {\n\t\tfor (;;) {\n\t\t\tconst request = dequeueRequest(this.#state);\n\t\t\tif (request) return request;\n\n\t\t\tconst closed = this.#state.closed.peek();\n\t\t\tif (closed instanceof Error) throw closed;\n\t\t\tif (closed !== undefined) return undefined;\n\n\t\t\tawait Signal.race(this.#state.requested, this.#state.closed);\n\t\t}\n\t}\n\n\t/** Insert a track that is served directly, without an on-demand request round-trip. */\n\tinsertTrack(track: track.Producer): void {\n\t\tif (this.#state.closed.peek() !== undefined) {\n\t\t\tthrow new Error(`broadcast is closed: ${this.#state.closed.peek()}`);\n\t\t}\n\n\t\tconst existing = this.#state.tracks.get(track.name);\n\t\tif (existing && existing.closed.peek() === undefined) {\n\t\t\tthrow new Error(`duplicate track: ${track.name}`);\n\t\t}\n\n\t\tthis.#state.tracks.set(track.name, track);\n\n\t\tvoid track.closed.then(() => {\n\t\t\tif (this.#state.tracks.get(track.name) === track) {\n\t\t\t\tthis.#state.tracks.delete(track.name);\n\t\t\t}\n\t\t});\n\t}\n\n\t/** Create a track, insert it into the broadcast, and return its producer. */\n\tcreateTrack(name: string, info: Partial<track.Info> = {}): track.Producer {\n\t\tconst producer = new track.Producer(name).accept(info);\n\t\tthis.insertTrack(producer);\n\t\treturn producer;\n\t}\n\n\t/** Remove a statically inserted track by name. */\n\tremoveTrack(name: string): void {\n\t\tthis.#state.tracks.delete(name);\n\t}\n\n\t/** A lazy read handle for a track on this broadcast. */\n\ttrack(name: string): track.Consumer {\n\t\treturn trackOf(name, this);\n\t}\n\n\t#wire(register: boolean): Wire {\n\t\treturn {\n\t\t\tsubscribe: (name, options) => subscribe(this.#state, name, options, register),\n\t\t\tresolveTrackInfo: (name) => resolveTrackInfo(this.#state, name),\n\t\t\tfetchGroup: (name, sequence, options) => fetchGroup(this.#state, name, sequence, options),\n\t\t\trequested: () => this.#requested(),\n\t\t};\n\t}\n\n\t/**\n\t * Advertise this broadcast's exact path, or re-price a standing advertisement in place.\n\t *\n\t * Call it once the tracks a subscriber needs first (a catalog) exist. Until then the\n\t * broadcast exists for nobody, on its own origin or at a peer. Retracts on\n\t * {@link unannounce} or {@link close}. Throws if this producer was not created through an\n\t * origin, or if the broadcast is already closed.\n\t */\n\tannounce(route: Route | { hops?: Route[\"hops\"]; cost?: Route[\"cost\"] | bigint } = Route.default): void {\n\t\tif (this.#state.closed.peek() !== undefined) {\n\t\t\tthrow new Error(`broadcast is closed: ${this.#state.closed.peek()}`);\n\t\t}\n\t\tif (!this.#announcer) throw new Error(\"broadcast is not attached to an origin\");\n\t\tthis.#announcer.announce(Route.normalize(route));\n\t}\n\n\t/**\n\t * Retract the advertisement of this broadcast's path, if any, from local consumers and\n\t * peers alike. {@link announce} brings it back.\n\t */\n\tunannounce(): void {\n\t\tthis.#announcer?.unannounce();\n\t}\n\n\t/** End the broadcast for good: retract it, serve no new tracks, and refuse a later {@link announce}. Idempotent. */\n\tclose(): void;\n\t/** @deprecated A broadcast end carries no cause; call `close()` without one. */\n\tclose(abort?: Error): void;\n\tclose(abort?: Error) {\n\t\tthis.#announcer?.unannounce();\n\t\tthis.#announcer = undefined;\n\t\tcloseState(this.#state, abort);\n\t}\n}\n\n// What a new consumer handle inherits: the shared broadcast plus the path naming it.\ninterface Shared {\n\tstate: BroadcastState;\n\tpath: Path.Valid;\n}\n\n// Constructs a Consumer from within this module without exposing a public constructor\n// that would leak the unexported BroadcastState. Assigned in the class's static block.\nlet makeConsumer: (shared: Shared) => Consumer;\n\n/**\n * The read side of a broadcast.\n *\n * Created internally: obtain one from {@link Producer.consume} or an origin request.\n * The wire layers subclass it to resolve tracks over the network.\n *\n * @public\n */\nexport class Consumer {\n\t#state: BroadcastState;\n\t#path: Path.Valid;\n\n\t// Guards against a double close() on this handle over-decrementing the consumer count.\n\t#closed = false;\n\n\tprotected constructor(shared?: never);\n\tprotected constructor(shared?: Shared) {\n\t\tthis.#state = shared?.state ?? new BroadcastState();\n\t\tthis.#path = shared?.path ?? Path.empty();\n\t\tthis.#state.consumers++;\n\t\tregisterWire(this, {\n\t\t\tsubscribe: (name, options) => subscribe(this.#state, name, options, true),\n\t\t\tresolveTrackInfo: (name) => resolveTrackInfo(this.#state, name),\n\t\t\tfetchGroup: (name, sequence, options) => fetchGroup(this.#state, name, sequence, options),\n\t\t\trequested: () => this.#requested(),\n\t\t});\n\t}\n\n\tstatic {\n\t\tmakeConsumer = (shared) => new Consumer(shared as never);\n\t\thooks.stampPath = (target, path) => {\n\t\t\tif (target instanceof Consumer) target.#path = path;\n\t\t\telse stampProducer(target, path);\n\t\t};\n\t}\n\n\t/**\n\t * The path this handle names the broadcast by, which relative references in its catalog\n\t * (hang's `broadcast` field) resolve against.\n\t *\n\t * An origin stamps each handle it hands out with the path it was requested at, relative to\n\t * that origin handle's scope root, and a broadcast it created with the path it was created at.\n\t * Empty for a standalone broadcast, which is then its own root: any `..` reference escapes.\n\t */\n\tget path(): Path.Valid {\n\t\treturn this.#path;\n\t}\n\n\t/**\n\t * Settles once the broadcast closes: `null` on a clean close, or the abort {@link Error}.\n\t * Peek it synchronously (`undefined` while open), observe it reactively, or `await` it.\n\t *\n\t * Shared by every {@link clone}: it settles once the last handle closes. The subscribing\n\t * wire layer peeks it to evict a closed entry from its per-path consume cache.\n\t */\n\tget closed(): GetPromise<Error | null> {\n\t\treturn this.#state.closed;\n\t}\n\n\t/**\n\t * Return another handle to the same broadcast, reference-counted with this one.\n\t *\n\t * Both handles read the same tracks, carry the same {@link path}, and share one {@link closed}\n\t * state; the broadcast closes only once *every* handle has {@link close}d. Used by the connection's per-path\n\t * consume cache to share one subscription across callers. Subclasses that resolve info over\n\t * the wire override this to preserve their type (see the wire layer's consumed broadcast).\n\t */\n\tclone(): Consumer {\n\t\treturn new Consumer(this.shareState());\n\t}\n\n\t// Hand this consumer's backing state and path to a clone. Opaque (`never`) so the state type\n\t// stays unexported; a subclass passes it straight back into its own `super(...)`.\n\tprotected shareState(): never {\n\t\treturn { state: this.#state, path: this.#path } satisfies Shared as never;\n\t}\n\n\t/** Get a lazy handle for a track on this broadcast. Repeat subscriptions dedupe onto one upstream subscription. */\n\ttrack(name: string): track.Consumer {\n\t\treturn trackOf(name, this);\n\t}\n\n\tasync #requested(): Promise<track.Request | undefined> {\n\t\tfor (;;) {\n\t\t\tconst request = dequeueRequest(this.#state);\n\t\t\tif (request) return request;\n\n\t\t\tconst closed = this.#state.closed.peek();\n\t\t\tif (closed instanceof Error) throw closed;\n\t\t\tif (closed !== undefined) return undefined;\n\n\t\t\tawait Signal.race(this.#state.requested, this.#state.closed);\n\t\t}\n\t}\n\n\t/**\n\t * Release this handle. The broadcast is closed once this was the last live handle;\n\t * while other {@link clone}s remain open it stays live.\n\t */\n\tclose(): void;\n\t/** @deprecated A broadcast end carries no cause; call `close()` without one. */\n\tclose(abort?: Error): void;\n\tclose(abort?: Error) {\n\t\tif (this.#closed) return;\n\t\tthis.#closed = true;\n\t\tif (--this.#state.consumers > 0) return;\n\t\tcloseState(this.#state, abort);\n\t}\n}\n"]}
|
package/connection/accept.js
CHANGED
|
@@ -19,7 +19,8 @@ export async function accept({ transport, url, ...props }) {
|
|
|
19
19
|
return connection;
|
|
20
20
|
}
|
|
21
21
|
async function acceptInner(transport, url, props) {
|
|
22
|
-
//
|
|
22
|
+
// The DOM lib has no `protocol` property yet. It is "" when none was negotiated, and
|
|
23
|
+
// undefined in a browser that predates subprotocols (Firefox before 155).
|
|
23
24
|
const protocol = transport.protocol;
|
|
24
25
|
const wiring = {
|
|
25
26
|
discovery: props.discovery ?? true,
|
|
@@ -76,13 +77,14 @@ async function acceptInner(transport, url, props) {
|
|
|
76
77
|
* a pair of uni streams using stream type 0x2F00.
|
|
77
78
|
*/
|
|
78
79
|
async function acceptAlpn(transport, url, version, wiring) {
|
|
79
|
-
const { control, solicit, hidden, cluster } = await exchangeSetup(transport, version, "moq-lite-js");
|
|
80
|
+
const { control, early, solicit, hidden, cluster } = await exchangeSetup(transport, version, "moq-lite-js");
|
|
80
81
|
return new Ietf.Connection({
|
|
81
82
|
...wiring,
|
|
82
83
|
client: false,
|
|
83
84
|
url,
|
|
84
85
|
quic: transport,
|
|
85
86
|
control,
|
|
87
|
+
early,
|
|
86
88
|
solicit,
|
|
87
89
|
hidden,
|
|
88
90
|
cluster,
|
|
@@ -97,7 +99,7 @@ async function acceptAlpn(transport, url, version, wiring) {
|
|
|
97
99
|
*/
|
|
98
100
|
async function acceptSetup(transport, url, version, wiring) {
|
|
99
101
|
// Accept bidi, read ClientSetup, write ServerSetup
|
|
100
|
-
const stream = await Stream.accept(transport);
|
|
102
|
+
const stream = await Stream.accept(transport, version);
|
|
101
103
|
if (!stream)
|
|
102
104
|
throw new Error("no incoming bidi stream for SETUP");
|
|
103
105
|
const clientCompat = await stream.reader.u53();
|
|
@@ -129,7 +131,7 @@ async function acceptSetup(transport, url, version, wiring) {
|
|
|
129
131
|
}
|
|
130
132
|
async function acceptNegotiated(transport, url, wiring, version) {
|
|
131
133
|
const setupVersion = Ietf.Version.DRAFT_14;
|
|
132
|
-
const stream = await Stream.accept(transport);
|
|
134
|
+
const stream = await Stream.accept(transport, setupVersion);
|
|
133
135
|
if (!stream)
|
|
134
136
|
throw new Error("no incoming bidi stream for SETUP");
|
|
135
137
|
const clientCompat = await stream.reader.u53();
|
package/connection/accept.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"accept.js","sourceRoot":"","sources":["../../src/connection/accept.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,kBAAkB,CAAC;AACzC,OAAO,KAAK,IAAI,MAAM,kBAAkB,CAAC;AAEzC,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAEtC,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAqC/C;;;;;;;GA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CAAC,CAAC;IACzE,CAAC;AACF,CAAC","sourcesContent":["import * as Ietf from \"../ietf/index.ts\";\nimport * as Lite from \"../lite/index.ts\";\nimport type { Consumer as OriginConsumer, Producer as OriginProducer } from \"../origin.ts\";\nimport { Stream } from \"../stream.ts\";\nimport type { Established } from \"./established.ts\";\nimport { forwardAnnounced } from \"./forward.ts\";\nimport { exchangeSetup } from \"./handshake.ts\";\n\n/** Options for {@link accept}. */\nexport interface AcceptProps {\n\t/** The accepted transport. */\n\ttransport: WebTransport;\n\t/** The request URL associated with the transport. */\n\turl: URL;\n\n\t/** Version to select during SETUP negotiation (for non-ALPN paths). */\n\tversion?: number;\n\n\t/**\n\t * Whether this server supports broadcast discovery; see {@link Established.discovery}.\n\t * Defaults to true.\n\t */\n\tdiscovery?: boolean;\n\n\t/**\n\t * The origin whose broadcasts the session announces and serves to the peer. Omit to\n\t * publish nothing. Borrowed, not owned: closing the session leaves its broadcasts alone.\n\t */\n\tpublish?: OriginConsumer;\n\n\t/**\n\t * The origin the session feeds with the peer's announced broadcasts. Omit to discover\n\t * nothing. The entries retract when the session dies; see the `consume` connect option.\n\t */\n\tconsume?: OriginProducer;\n}\n\n/** The per-session wiring shared by every negotiated protocol path. */\ntype SessionProps = {\n\tdiscovery: boolean;\n\tpublish?: OriginConsumer;\n};\n\n/**\n * Server-side handshake: accepts a transport and performs the server half of the SETUP exchange.\n *\n * @param transport - The WebTransport session to accept\n * @param url - The URL of the connection\n * @param props - Optional configuration\n * @returns A promise that resolves to a Connection instance\n */\nexport async function accept({ transport, url, ...props }: AcceptProps): Promise<Established> {\n\tconst connection = await acceptInner(transport, url, props);\n\tif (props.consume) forwardAnnounced(connection, props.consume);\n\treturn connection;\n}\n\nasync function acceptInner(\n\ttransport: WebTransport,\n\turl: URL,\n\tprops: Omit<AcceptProps, \"transport\" | \"url\">,\n): Promise<Established> {\n\t// @ts-expect-error - TODO: add protocol to WebTransport\n\tconst protocol: string | undefined = transport.protocol;\n\n\tconst wiring: SessionProps = {\n\t\tdiscovery: props.discovery ?? true,\n\t\tpublish: props.publish,\n\t};\n\n\tif (protocol === Ietf.ALPN.DRAFT_22) {\n\t\treturn acceptAlpn(transport, url, Ietf.Version.DRAFT_22, wiring);\n\t}\n\tif (protocol === Ietf.ALPN.DRAFT_21) {\n\t\treturn acceptAlpn(transport, url, Ietf.Version.DRAFT_21, wiring);\n\t}\n\tif (protocol === Ietf.ALPN.DRAFT_20) {\n\t\treturn acceptAlpn(transport, url, Ietf.Version.DRAFT_20, wiring);\n\t}\n\tif (protocol === Ietf.ALPN.DRAFT_19) {\n\t\treturn acceptAlpn(transport, url, Ietf.Version.DRAFT_19, wiring);\n\t} else if (protocol === Ietf.ALPN.DRAFT_18) {\n\t\treturn acceptAlpn(transport, url, Ietf.Version.DRAFT_18, wiring);\n\t} else if (protocol === Ietf.ALPN.DRAFT_17) {\n\t\treturn acceptAlpn(transport, url, Ietf.Version.DRAFT_17, wiring);\n\t} else if (protocol === Ietf.ALPN.DRAFT_16) {\n\t\treturn acceptSetup(transport, url, Ietf.Version.DRAFT_16, wiring);\n\t} else if (protocol === Ietf.ALPN.DRAFT_15) {\n\t\treturn acceptSetup(transport, url, Ietf.Version.DRAFT_15, wiring);\n\t} else if (protocol === Lite.ALPN_07_WIP) {\n\t\treturn new Lite.Connection({ url, quic: transport, version: Lite.Version.DRAFT_07, ...wiring });\n\t} else if (protocol === Lite.ALPN_06) {\n\t\treturn new Lite.Connection({ url, quic: transport, version: Lite.Version.DRAFT_06, ...wiring });\n\t} else if (protocol === Lite.ALPN_05) {\n\t\treturn new Lite.Connection({ url, quic: transport, version: Lite.Version.DRAFT_05, ...wiring });\n\t} else if (protocol === Lite.ALPN_04) {\n\t\treturn new Lite.Connection({ url, quic: transport, version: Lite.Version.DRAFT_04, ...wiring });\n\t} else if (protocol === Lite.ALPN_03) {\n\t\treturn new Lite.Connection({ url, quic: transport, version: Lite.Version.DRAFT_03, ...wiring });\n\t} else if (protocol === Lite.ALPN || protocol === \"\" || protocol === undefined) {\n\t\treturn acceptNegotiated(transport, url, wiring, props.version);\n\t} else {\n\t\tthrow new Error(`unsupported WebTransport protocol: ${protocol}`);\n\t}\n}\n\n/**\n * Draft-17+ accept: ALPN already pinned the version. SETUP is exchanged over\n * a pair of uni streams using stream type 0x2F00.\n */\nasync function acceptAlpn(\n\ttransport: WebTransport,\n\turl: URL,\n\tversion: Ietf.IetfVersion,\n\twiring: SessionProps,\n): Promise<Established> {\n\tconst { control, solicit, hidden, cluster } = await exchangeSetup(transport, version, \"moq-lite-js\");\n\n\treturn new Ietf.Connection({\n\t\t...wiring,\n\t\tclient: false,\n\t\turl,\n\t\tquic: transport,\n\t\tcontrol,\n\t\tsolicit,\n\t\thidden,\n\t\tcluster,\n\t\t// v17+ uses NativeSession which manages its own request IDs; maxRequestId is unused.\n\t\tmaxRequestId: 0n,\n\t\tversion,\n\t});\n}\n\n/**\n * Legacy accept (draft-15/16): ALPN pinned the version, but the SETUP message\n * is still exchanged over a bidi stream wrapped in the moq-lite compat envelope.\n */\nasync function acceptSetup(\n\ttransport: WebTransport,\n\turl: URL,\n\tversion: Ietf.IetfVersion,\n\twiring: SessionProps,\n): Promise<Established> {\n\t// Accept bidi, read ClientSetup, write ServerSetup\n\tconst stream = await Stream.accept(transport);\n\tif (!stream) throw new Error(\"no incoming bidi stream for SETUP\");\n\n\tconst clientCompat = await stream.reader.u53();\n\tif (clientCompat !== Lite.StreamId.ClientCompat) {\n\t\tthrow new Error(`unexpected client message type: 0x${clientCompat.toString(16)}`);\n\t}\n\n\tconst client = await Ietf.ClientSetup.decode(stream.reader, version);\n\n\tawait stream.writer.u53(Lite.StreamId.ServerCompat);\n\n\tconst encoder = new TextEncoder();\n\tconst params = new Ietf.SetupOptions();\n\tparams.setVarint(Ietf.SetupOption.MaxRequestId, 42069n);\n\tparams.setBytes(Ietf.SetupOption.Implementation, encoder.encode(\"moq-lite-js\"));\n\tIetf.solicitIntoSetup(params);\n\tIetf.hiddenIntoSetup(params);\n\n\tconst server = new Ietf.ServerSetup({ version, parameters: params });\n\tawait server.encode(stream.writer, version);\n\n\tconst maxRequestId = 42069n;\n\n\treturn new Ietf.Connection({\n\t\t...wiring,\n\t\tclient: false,\n\t\turl,\n\t\tquic: transport,\n\t\tcontrol: stream,\n\t\tmaxRequestId,\n\t\tversion,\n\t\tsolicit: Ietf.solicitFromSetup(client.parameters),\n\t\thidden: Ietf.hiddenFromSetup(client.parameters),\n\t});\n}\n\nasync function acceptNegotiated(\n\ttransport: WebTransport,\n\turl: URL,\n\twiring: SessionProps,\n\tversion?: number,\n): Promise<Established> {\n\tconst setupVersion = Ietf.Version.DRAFT_14;\n\n\tconst stream = await Stream.accept(transport);\n\tif (!stream) throw new Error(\"no incoming bidi stream for SETUP\");\n\n\tconst clientCompat = await stream.reader.u53();\n\tif (clientCompat !== Lite.StreamId.ClientCompat) {\n\t\tthrow new Error(`unexpected client message type: 0x${clientCompat.toString(16)}`);\n\t}\n\n\tconst client = await Ietf.ClientSetup.decode(stream.reader, setupVersion);\n\n\t// Pick the requested version, or first matching version from client's list\n\tconst allVersions = [...Object.values(Lite.Version), ...Object.values(Ietf.Version)] as number[];\n\tlet selectedVersion: number;\n\tif (version !== undefined) {\n\t\tselectedVersion = version;\n\t} else {\n\t\tconst match = client.versions.find((v) => allVersions.includes(v));\n\t\tif (match === undefined) {\n\t\t\tthrow new Error(\n\t\t\t\t`no common version found; client offered: ${client.versions.map((v) => v.toString(16)).join(\", \")}`,\n\t\t\t);\n\t\t}\n\t\tselectedVersion = match;\n\t}\n\n\tawait stream.writer.u53(Lite.StreamId.ServerCompat);\n\n\tconst encoder = new TextEncoder();\n\tconst params = new Ietf.SetupOptions();\n\tparams.setVarint(Ietf.SetupOption.MaxRequestId, 42069n);\n\tparams.setBytes(Ietf.SetupOption.Implementation, encoder.encode(\"moq-lite-js\"));\n\tIetf.solicitIntoSetup(params);\n\tIetf.hiddenIntoSetup(params);\n\n\tconst server = new Ietf.ServerSetup({ version: selectedVersion, parameters: params });\n\tawait server.encode(stream.writer, setupVersion);\n\n\tif (Object.values(Lite.Version).includes(selectedVersion as Lite.Version)) {\n\t\treturn new Lite.Connection({\n\t\t\turl,\n\t\t\tquic: transport,\n\t\t\tversion: selectedVersion as Lite.Version,\n\t\t\tsession: stream,\n\t\t\t...wiring,\n\t\t});\n\t} else if (Object.values(Ietf.Version).includes(selectedVersion as Ietf.Version)) {\n\t\tconst maxRequestId = client.parameters.getVarint(Ietf.SetupOption.MaxRequestId) ?? 0n;\n\t\treturn new Ietf.Connection({\n\t\t\t...wiring,\n\t\t\tclient: false,\n\t\t\turl,\n\t\t\tquic: transport,\n\t\t\tcontrol: stream,\n\t\t\tmaxRequestId,\n\t\t\tversion: selectedVersion as Ietf.IetfVersion,\n\t\t\tsolicit: Ietf.solicitFromSetup(client.parameters),\n\t\t\thidden: Ietf.hiddenFromSetup(client.parameters),\n\t\t});\n\t} else {\n\t\tthrow new Error(`unsupported version: ${selectedVersion.toString(16)}`);\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"accept.js","sourceRoot":"","sources":["../../src/connection/accept.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,kBAAkB,CAAC;AACzC,OAAO,KAAK,IAAI,MAAM,kBAAkB,CAAC;AAEzC,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAEtC,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAqC/C;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,MAAM,CAAC,EAAE,SAAS,EAAE,GAAG,EAAE,GAAG,KAAK,EAAe;IACrE,MAAM,UAAU,GAAG,MAAM,WAAW,CAAC,SAAS,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC;IAC5D,IAAI,KAAK,CAAC,OAAO;QAAE,gBAAgB,CAAC,UAAU,EAAE,KAAK,CAAC,OAAO,CAAC,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EAAE,CAAC,CAAC;IACtF,MAAM,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;IAEjD,IAAI,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,eAA+B,CAAC,EAAE,CAAC;QAC3E,OAAO,IAAI,IAAI,CAAC,UAAU,CAAC;YAC1B,GAAG;YACH,IAAI,EAAE,SAAS;YACf,OAAO,EAAE,eAA+B;YACxC,OAAO,EAAE,MAAM;YACf,GAAG,MAAM;SACT,CAAC,CAAC;IACJ,CAAC;SAAM,IAAI,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,eAA+B,CAAC,EAAE,CAAC;QAClF,MAAM,YAAY,GAAG,MAAM,CAAC,UAAU,CAAC,SAAS,CAAC,IAAI,CAAC,WAAW,CAAC,YAAY,CAAC,IAAI,EAAE,CAAC;QACtF,OAAO,IAAI,IAAI,CAAC,UAAU,CAAC;YAC1B,GAAG,MAAM;YACT,MAAM,EAAE,KAAK;YACb,GAAG;YACH,IAAI,EAAE,SAAS;YACf,OAAO,EAAE,MAAM;YACf,YAAY;YACZ,OAAO,EAAE,eAAmC;YAC5C,OAAO,EAAE,IAAI,CAAC,gBAAgB,CAAC,MAAM,CAAC,UAAU,CAAC;YACjD,MAAM,EAAE,IAAI,CAAC,eAAe,CAAC,MAAM,CAAC,UAAU,CAAC;SAC/C,CAAC,CAAC;IACJ,CAAC;SAAM,CAAC;QACP,MAAM,IAAI,KAAK,CAAC,wBAAwB,eAAe,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;IACzE,CAAC;AACF,CAAC","sourcesContent":["import * as Ietf from \"../ietf/index.ts\";\nimport * as Lite from \"../lite/index.ts\";\nimport type { Consumer as OriginConsumer, Producer as OriginProducer } from \"../origin.ts\";\nimport { Stream } from \"../stream.ts\";\nimport type { Established } from \"./established.ts\";\nimport { forwardAnnounced } from \"./forward.ts\";\nimport { exchangeSetup } from \"./handshake.ts\";\n\n/** Options for {@link accept}. */\nexport interface AcceptProps {\n\t/** The accepted transport. */\n\ttransport: WebTransport;\n\t/** The request URL associated with the transport. */\n\turl: URL;\n\n\t/** Version to select during SETUP negotiation (for non-ALPN paths). */\n\tversion?: number;\n\n\t/**\n\t * Whether this server supports broadcast discovery; see {@link Established.discovery}.\n\t * Defaults to true.\n\t */\n\tdiscovery?: boolean;\n\n\t/**\n\t * The origin whose broadcasts the session announces and serves to the peer. Omit to\n\t * publish nothing. Borrowed, not owned: closing the session leaves its broadcasts alone.\n\t */\n\tpublish?: OriginConsumer;\n\n\t/**\n\t * The origin the session feeds with the peer's announced broadcasts. Omit to discover\n\t * nothing. The entries retract when the session dies; see the `consume` connect option.\n\t */\n\tconsume?: OriginProducer;\n}\n\n/** The per-session wiring shared by every negotiated protocol path. */\ntype SessionProps = {\n\tdiscovery: boolean;\n\tpublish?: OriginConsumer;\n};\n\n/**\n * Server-side handshake: accepts a transport and performs the server half of the SETUP exchange.\n *\n * @param transport - The WebTransport session to accept\n * @param url - The URL of the connection\n * @param props - Optional configuration\n * @returns A promise that resolves to a Connection instance\n */\nexport async function accept({ transport, url, ...props }: AcceptProps): Promise<Established> {\n\tconst connection = await acceptInner(transport, url, props);\n\tif (props.consume) forwardAnnounced(connection, props.consume);\n\treturn connection;\n}\n\nasync function acceptInner(\n\ttransport: WebTransport,\n\turl: URL,\n\tprops: Omit<AcceptProps, \"transport\" | \"url\">,\n): Promise<Established> {\n\t// The DOM lib has no `protocol` property yet. It is \"\" when none was negotiated, and\n\t// undefined in a browser that predates subprotocols (Firefox before 155).\n\tconst protocol = (transport as { protocol?: string }).protocol;\n\n\tconst wiring: SessionProps = {\n\t\tdiscovery: props.discovery ?? true,\n\t\tpublish: props.publish,\n\t};\n\n\tif (protocol === Ietf.ALPN.DRAFT_22) {\n\t\treturn acceptAlpn(transport, url, Ietf.Version.DRAFT_22, wiring);\n\t}\n\tif (protocol === Ietf.ALPN.DRAFT_21) {\n\t\treturn acceptAlpn(transport, url, Ietf.Version.DRAFT_21, wiring);\n\t}\n\tif (protocol === Ietf.ALPN.DRAFT_20) {\n\t\treturn acceptAlpn(transport, url, Ietf.Version.DRAFT_20, wiring);\n\t}\n\tif (protocol === Ietf.ALPN.DRAFT_19) {\n\t\treturn acceptAlpn(transport, url, Ietf.Version.DRAFT_19, wiring);\n\t} else if (protocol === Ietf.ALPN.DRAFT_18) {\n\t\treturn acceptAlpn(transport, url, Ietf.Version.DRAFT_18, wiring);\n\t} else if (protocol === Ietf.ALPN.DRAFT_17) {\n\t\treturn acceptAlpn(transport, url, Ietf.Version.DRAFT_17, wiring);\n\t} else if (protocol === Ietf.ALPN.DRAFT_16) {\n\t\treturn acceptSetup(transport, url, Ietf.Version.DRAFT_16, wiring);\n\t} else if (protocol === Ietf.ALPN.DRAFT_15) {\n\t\treturn acceptSetup(transport, url, Ietf.Version.DRAFT_15, wiring);\n\t} else if (protocol === Lite.ALPN_07_WIP) {\n\t\treturn new Lite.Connection({ url, quic: transport, version: Lite.Version.DRAFT_07, ...wiring });\n\t} else if (protocol === Lite.ALPN_06) {\n\t\treturn new Lite.Connection({ url, quic: transport, version: Lite.Version.DRAFT_06, ...wiring });\n\t} else if (protocol === Lite.ALPN_05) {\n\t\treturn new Lite.Connection({ url, quic: transport, version: Lite.Version.DRAFT_05, ...wiring });\n\t} else if (protocol === Lite.ALPN_04) {\n\t\treturn new Lite.Connection({ url, quic: transport, version: Lite.Version.DRAFT_04, ...wiring });\n\t} else if (protocol === Lite.ALPN_03) {\n\t\treturn new Lite.Connection({ url, quic: transport, version: Lite.Version.DRAFT_03, ...wiring });\n\t} else if (protocol === Lite.ALPN || protocol === \"\" || protocol === undefined) {\n\t\treturn acceptNegotiated(transport, url, wiring, props.version);\n\t} else {\n\t\tthrow new Error(`unsupported WebTransport protocol: ${protocol}`);\n\t}\n}\n\n/**\n * Draft-17+ accept: ALPN already pinned the version. SETUP is exchanged over\n * a pair of uni streams using stream type 0x2F00.\n */\nasync function acceptAlpn(\n\ttransport: WebTransport,\n\turl: URL,\n\tversion: Ietf.IetfVersion,\n\twiring: SessionProps,\n): Promise<Established> {\n\tconst { control, early, solicit, hidden, cluster } = await exchangeSetup(transport, version, \"moq-lite-js\");\n\n\treturn new Ietf.Connection({\n\t\t...wiring,\n\t\tclient: false,\n\t\turl,\n\t\tquic: transport,\n\t\tcontrol,\n\t\tearly,\n\t\tsolicit,\n\t\thidden,\n\t\tcluster,\n\t\t// v17+ uses NativeSession which manages its own request IDs; maxRequestId is unused.\n\t\tmaxRequestId: 0n,\n\t\tversion,\n\t});\n}\n\n/**\n * Legacy accept (draft-15/16): ALPN pinned the version, but the SETUP message\n * is still exchanged over a bidi stream wrapped in the moq-lite compat envelope.\n */\nasync function acceptSetup(\n\ttransport: WebTransport,\n\turl: URL,\n\tversion: Ietf.IetfVersion,\n\twiring: SessionProps,\n): Promise<Established> {\n\t// Accept bidi, read ClientSetup, write ServerSetup\n\tconst stream = await Stream.accept(transport, version);\n\tif (!stream) throw new Error(\"no incoming bidi stream for SETUP\");\n\n\tconst clientCompat = await stream.reader.u53();\n\tif (clientCompat !== Lite.StreamId.ClientCompat) {\n\t\tthrow new Error(`unexpected client message type: 0x${clientCompat.toString(16)}`);\n\t}\n\n\tconst client = await Ietf.ClientSetup.decode(stream.reader, version);\n\n\tawait stream.writer.u53(Lite.StreamId.ServerCompat);\n\n\tconst encoder = new TextEncoder();\n\tconst params = new Ietf.SetupOptions();\n\tparams.setVarint(Ietf.SetupOption.MaxRequestId, 42069n);\n\tparams.setBytes(Ietf.SetupOption.Implementation, encoder.encode(\"moq-lite-js\"));\n\tIetf.solicitIntoSetup(params);\n\tIetf.hiddenIntoSetup(params);\n\n\tconst server = new Ietf.ServerSetup({ version, parameters: params });\n\tawait server.encode(stream.writer, version);\n\n\tconst maxRequestId = 42069n;\n\n\treturn new Ietf.Connection({\n\t\t...wiring,\n\t\tclient: false,\n\t\turl,\n\t\tquic: transport,\n\t\tcontrol: stream,\n\t\tmaxRequestId,\n\t\tversion,\n\t\tsolicit: Ietf.solicitFromSetup(client.parameters),\n\t\thidden: Ietf.hiddenFromSetup(client.parameters),\n\t});\n}\n\nasync function acceptNegotiated(\n\ttransport: WebTransport,\n\turl: URL,\n\twiring: SessionProps,\n\tversion?: number,\n): Promise<Established> {\n\tconst setupVersion = Ietf.Version.DRAFT_14;\n\n\tconst stream = await Stream.accept(transport, setupVersion);\n\tif (!stream) throw new Error(\"no incoming bidi stream for SETUP\");\n\n\tconst clientCompat = await stream.reader.u53();\n\tif (clientCompat !== Lite.StreamId.ClientCompat) {\n\t\tthrow new Error(`unexpected client message type: 0x${clientCompat.toString(16)}`);\n\t}\n\n\tconst client = await Ietf.ClientSetup.decode(stream.reader, setupVersion);\n\n\t// Pick the requested version, or first matching version from client's list\n\tconst allVersions = [...Object.values(Lite.Version), ...Object.values(Ietf.Version)] as number[];\n\tlet selectedVersion: number;\n\tif (version !== undefined) {\n\t\tselectedVersion = version;\n\t} else {\n\t\tconst match = client.versions.find((v) => allVersions.includes(v));\n\t\tif (match === undefined) {\n\t\t\tthrow new Error(\n\t\t\t\t`no common version found; client offered: ${client.versions.map((v) => v.toString(16)).join(\", \")}`,\n\t\t\t);\n\t\t}\n\t\tselectedVersion = match;\n\t}\n\n\tawait stream.writer.u53(Lite.StreamId.ServerCompat);\n\n\tconst encoder = new TextEncoder();\n\tconst params = new Ietf.SetupOptions();\n\tparams.setVarint(Ietf.SetupOption.MaxRequestId, 42069n);\n\tparams.setBytes(Ietf.SetupOption.Implementation, encoder.encode(\"moq-lite-js\"));\n\tIetf.solicitIntoSetup(params);\n\tIetf.hiddenIntoSetup(params);\n\n\tconst server = new Ietf.ServerSetup({ version: selectedVersion, parameters: params });\n\tawait server.encode(stream.writer, setupVersion);\n\n\tif (Object.values(Lite.Version).includes(selectedVersion as Lite.Version)) {\n\t\treturn new Lite.Connection({\n\t\t\turl,\n\t\t\tquic: transport,\n\t\t\tversion: selectedVersion as Lite.Version,\n\t\t\tsession: stream,\n\t\t\t...wiring,\n\t\t});\n\t} else if (Object.values(Ietf.Version).includes(selectedVersion as Ietf.Version)) {\n\t\tconst maxRequestId = client.parameters.getVarint(Ietf.SetupOption.MaxRequestId) ?? 0n;\n\t\treturn new Ietf.Connection({\n\t\t\t...wiring,\n\t\t\tclient: false,\n\t\t\turl,\n\t\t\tquic: transport,\n\t\t\tcontrol: stream,\n\t\t\tmaxRequestId,\n\t\t\tversion: selectedVersion as Ietf.IetfVersion,\n\t\t\tsolicit: Ietf.solicitFromSetup(client.parameters),\n\t\t\thidden: Ietf.hiddenFromSetup(client.parameters),\n\t\t});\n\t} else {\n\t\tthrow new Error(`unsupported version: ${selectedVersion.toString(16)}`);\n\t}\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connect.d.ts","sourceRoot":"","sources":["../../src/connection/connect.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,QAAQ,IAAI,cAAc,EAAE,QAAQ,IAAI,cAAc,EAAE,MAAM,cAAc,CAAC;AAE3F,OAAO,KAAK,IAAI,MAAM,YAAY,CAAC;AAInC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAWpD,yGAAyG;AACzG,MAAM,WAAW,cAAc;IAC9B,yDAAyD;IACzD,OAAO,CAAC,EAAE,OAAO,CAAC;IAElB,mGAAmG;IACnG,GAAG,CAAC,EAAE,GAAG,CAAC;IAEV;;;OAGG;IACH,KAAK,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC;CACnB;AAED;;;;GAIG;AACH,MAAM,WAAW,eAAe;IAC/B,4EAA4E;IAC5E,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB,uDAAuD;IACvD,KAAK,EAAE,YAAY,GAAG,MAAM,CAAC;CAC7B;AAED,2GAA2G;AAC3G,MAAM,WAAW,iBAAkB,SAAQ,IAAI,CAAC,mBAAmB,E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;;GAIG;AACH,MAAM,WAAW,eAAe;IAC/B,4EAA4E;IAC5E,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB,uDAAuD;IACvD,KAAK,EAAE,YAAY,GAAG,MAAM,CAAC;CAC7B;AAED,2GAA2G;AAC3G,MAAM,WAAW,iBAAkB,SAAQ,IAAI,CAAC,mBAAmB,EAAE,yBAAyB,CAAC;IAC9F;;;OAGG;IACH,uBAAuB,CAAC,EAAE,eAAe,EAAE,CAAC;IAE5C;;;;OAIG;IACH,iBAAiB,CAAC,EAAE,MAAM,GAAG,YAAY,CAAC;CAC1C;AAED,mCAAmC;AACnC,MAAM,WAAW,YAAY;IAC5B,qBAAqB;IACrB,GAAG,EAAE,GAAG,CAAC;IAET,4BAA4B;IAC5B,YAAY,CAAC,EAAE,iBAAiB,CAAC;IAEjC,oCAAoC;IACpC,SAAS,CAAC,EAAE,cAAc,CAAC;IAE3B;;;;;;OAMG;IACH,SAAS,CAAC,EAAE,YAAY,CAAC;IAEzB;;;OAGG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;IAEpB;;;;;;;OAOG;IACH,OAAO,CAAC,EAAE,cAAc,CAAC;IAEzB;;;;;;;;;OASG;IACH,OAAO,CAAC,EAAE,cAAc,CAAC;IAEzB;;;;OAIG;IACH,MAAM,CAAC,EAAE,WAAW,CAAC;CACrB;AAcD;;;;;;GAMG;AACH,wBAAsB,OAAO,CAAC,EAAE,GAAG,EAAE,GAAG,KAAK,EAAE,EAAE,YAAY,GAAG,OAAO,CAAC,WAAW,CAAC,CAwBnF;AA6OD;;;;GAIG;AACH,wBAAsB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,GAAG,OAAO,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC,CAInG"}
|
package/connection/connect.js
CHANGED
|
@@ -128,8 +128,8 @@ async function connectTransport(url, session, wiring) {
|
|
|
128
128
|
// Negotiate the MoQ protocol over an established transport. The caller races this against
|
|
129
129
|
// the session closing so a close code is not lost behind a failed or stalled SETUP stream.
|
|
130
130
|
async function negotiate(url, session, wiring) {
|
|
131
|
-
//
|
|
132
|
-
//
|
|
131
|
+
// The DOM lib has no `protocol` property yet. It is "" when none was negotiated, and
|
|
132
|
+
// undefined in a browser that predates subprotocols (Firefox before 155).
|
|
133
133
|
const protocol = session.protocol || undefined;
|
|
134
134
|
if (dev())
|
|
135
135
|
console.debug(redact(url), "negotiated ALPN:", protocol ?? "(none)");
|
|
@@ -178,7 +178,7 @@ async function negotiate(url, session, wiring) {
|
|
|
178
178
|
else {
|
|
179
179
|
throw new Error(`unsupported WebTransport protocol: ${protocol}`);
|
|
180
180
|
}
|
|
181
|
-
const stream = await Stream.open(session);
|
|
181
|
+
const stream = await Stream.open(session, { version: setupVersion });
|
|
182
182
|
await stream.writer.u53(Lite.StreamId.ClientCompat);
|
|
183
183
|
const encoder = new TextEncoder();
|
|
184
184
|
const params = new Ietf.SetupOptions();
|
|
@@ -232,13 +232,14 @@ async function negotiate(url, session, wiring) {
|
|
|
232
232
|
* exchanged over a pair of uni streams using stream type 0x2F00.
|
|
233
233
|
*/
|
|
234
234
|
async function handshakeAlpn(url, session, version, wiring) {
|
|
235
|
-
const { control, solicit, hidden, cluster } = await exchangeSetup(session, version, "moq-lite-js");
|
|
235
|
+
const { control, early, solicit, hidden, cluster } = await exchangeSetup(session, version, "moq-lite-js");
|
|
236
236
|
return new Ietf.Connection({
|
|
237
237
|
...wiring,
|
|
238
238
|
client: true,
|
|
239
239
|
url,
|
|
240
240
|
quic: session,
|
|
241
241
|
control,
|
|
242
|
+
early,
|
|
242
243
|
solicit,
|
|
243
244
|
hidden,
|
|
244
245
|
cluster,
|