@moq/net 0.3.5 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/announce.d.ts +7 -0
- package/announce.d.ts.map +1 -0
- package/announce.js +8 -0
- package/announce.js.map +1 -0
- package/announced.d.ts +49 -91
- package/announced.d.ts.map +1 -1
- package/announced.js +21 -156
- package/announced.js.map +1 -1
- package/bandwidth.d.ts +163 -0
- package/bandwidth.d.ts.map +1 -0
- package/bandwidth.js +304 -0
- package/bandwidth.js.map +1 -0
- package/bandwidth_api.d.ts +7 -0
- package/bandwidth_api.d.ts.map +1 -0
- package/bandwidth_api.js +8 -0
- package/bandwidth_api.js.map +1 -0
- package/broadcast.d.ts +23 -28
- package/broadcast.d.ts.map +1 -1
- package/broadcast.js +61 -40
- package/broadcast.js.map +1 -1
- package/connection/accept.d.ts +16 -1
- package/connection/accept.d.ts.map +1 -1
- package/connection/accept.js +48 -25
- package/connection/accept.js.map +1 -1
- package/connection/browser.d.ts.map +1 -1
- package/connection/browser.js +9 -7
- package/connection/browser.js.map +1 -1
- package/connection/connect.d.ts +30 -6
- package/connection/connect.d.ts.map +1 -1
- package/connection/connect.js +95 -46
- package/connection/connect.js.map +1 -1
- package/connection/established.d.ts +17 -21
- package/connection/established.d.ts.map +1 -1
- package/connection/established.js.map +1 -1
- package/connection/forward.d.ts +2 -0
- package/connection/forward.d.ts.map +1 -0
- package/connection/forward.js +166 -0
- package/connection/forward.js.map +1 -0
- package/connection/handshake.d.ts +1 -0
- package/connection/handshake.d.ts.map +1 -1
- package/connection/handshake.js +5 -2
- package/connection/handshake.js.map +1 -1
- package/connection/index.d.ts +5 -5
- package/connection/index.d.ts.map +1 -1
- package/connection/index.js +4 -5
- package/connection/index.js.map +1 -1
- package/connection/pool.d.ts +186 -0
- package/connection/pool.d.ts.map +1 -0
- package/connection/pool.js +361 -0
- package/connection/pool.js.map +1 -0
- package/connection/reload.d.ts +14 -93
- package/connection/reload.d.ts.map +1 -1
- package/connection/reload.js +217 -81
- package/connection/reload.js.map +1 -1
- package/connection/stats.d.ts +3 -26
- package/connection/stats.d.ts.map +1 -1
- package/connection/stats.js.map +1 -1
- package/connection/transport.d.ts +0 -7
- package/connection/transport.d.ts.map +1 -1
- package/consume.d.ts +1 -43
- package/consume.d.ts.map +1 -1
- package/consume.js +1 -1
- package/consume.js.map +1 -1
- package/error.d.ts +180 -29
- package/error.d.ts.map +1 -1
- package/error.js +331 -16
- package/error.js.map +1 -1
- package/errors.d.ts +7 -0
- package/errors.d.ts.map +1 -0
- package/errors.js +8 -0
- package/errors.js.map +1 -0
- package/group.d.ts +9 -43
- package/group.d.ts.map +1 -1
- package/group.js +283 -69
- package/group.js.map +1 -1
- package/hop.d.ts +107 -0
- package/hop.d.ts.map +1 -0
- package/hop.js +103 -0
- package/hop.js.map +1 -0
- package/ietf/adapter.d.ts +5 -1
- package/ietf/adapter.d.ts.map +1 -1
- package/ietf/adapter.js +27 -9
- package/ietf/adapter.js.map +1 -1
- package/ietf/aliases.d.ts +1 -78
- package/ietf/aliases.d.ts.map +1 -1
- package/ietf/cluster.d.ts +6 -120
- package/ietf/cluster.d.ts.map +1 -1
- package/ietf/cluster.js +81 -41
- package/ietf/cluster.js.map +1 -1
- package/ietf/connection.d.ts +6 -72
- package/ietf/connection.d.ts.map +1 -1
- package/ietf/connection.js +17 -49
- package/ietf/connection.js.map +1 -1
- package/ietf/error.d.ts +11 -0
- package/ietf/error.d.ts.map +1 -0
- package/ietf/error.js +193 -0
- package/ietf/error.js.map +1 -0
- package/ietf/filter.d.ts.map +1 -1
- package/ietf/filter.js +6 -1
- package/ietf/filter.js.map +1 -1
- package/ietf/goaway.d.ts.map +1 -1
- package/ietf/goaway.js +22 -5
- package/ietf/goaway.js.map +1 -1
- package/ietf/hidden.d.ts +2 -0
- package/ietf/hidden.d.ts.map +1 -0
- package/ietf/hidden.js +30 -0
- package/ietf/hidden.js.map +1 -0
- package/ietf/index.d.ts +1 -0
- package/ietf/index.d.ts.map +1 -1
- package/ietf/index.js +1 -0
- package/ietf/index.js.map +1 -1
- package/ietf/object.d.ts +8 -2
- package/ietf/object.d.ts.map +1 -1
- package/ietf/object.js +17 -3
- package/ietf/object.js.map +1 -1
- package/ietf/parameters.d.ts +7 -2
- package/ietf/parameters.d.ts.map +1 -1
- package/ietf/parameters.js +35 -17
- package/ietf/parameters.js.map +1 -1
- package/ietf/properties.d.ts +1 -0
- package/ietf/properties.d.ts.map +1 -1
- package/ietf/properties.js +14 -0
- package/ietf/properties.js.map +1 -1
- package/ietf/publish.d.ts +19 -2
- package/ietf/publish.d.ts.map +1 -1
- package/ietf/publish.js +40 -6
- package/ietf/publish.js.map +1 -1
- package/ietf/publish_namespace.d.ts +25 -0
- package/ietf/publish_namespace.d.ts.map +1 -1
- package/ietf/publish_namespace.js +63 -0
- package/ietf/publish_namespace.js.map +1 -1
- package/ietf/publisher.d.ts +1 -82
- package/ietf/publisher.d.ts.map +1 -1
- package/ietf/publisher.js +302 -181
- package/ietf/publisher.js.map +1 -1
- package/ietf/solicit.d.ts +1 -40
- package/ietf/solicit.d.ts.map +1 -1
- package/ietf/subscribe.d.ts +1 -5
- package/ietf/subscribe.d.ts.map +1 -1
- package/ietf/subscribe.js +11 -13
- package/ietf/subscribe.js.map +1 -1
- package/ietf/subscribe_namespace.d.ts +8 -2
- package/ietf/subscribe_namespace.d.ts.map +1 -1
- package/ietf/subscribe_namespace.js +18 -8
- package/ietf/subscribe_namespace.js.map +1 -1
- package/ietf/subscriber.d.ts +1 -65
- package/ietf/subscriber.d.ts.map +1 -1
- package/ietf/subscriber.js +325 -121
- package/ietf/subscriber.js.map +1 -1
- package/ietf/version.d.ts +12 -1
- package/ietf/version.d.ts.map +1 -1
- package/ietf/version.js +13 -0
- package/ietf/version.js.map +1 -1
- package/index.d.ts +11 -6
- package/index.d.ts.map +1 -1
- package/index.js +10 -5
- package/index.js.map +1 -1
- package/internal.d.ts +104 -1
- package/internal.d.ts.map +1 -1
- package/internal.js +76 -0
- package/internal.js.map +1 -1
- package/lite/announce.d.ts +60 -10
- package/lite/announce.d.ts.map +1 -1
- package/lite/announce.js +173 -31
- package/lite/announce.js.map +1 -1
- package/lite/connection.d.ts +9 -59
- package/lite/connection.d.ts.map +1 -1
- package/lite/connection.js +26 -32
- package/lite/connection.js.map +1 -1
- package/lite/fetch.d.ts +15 -1
- package/lite/fetch.d.ts.map +1 -1
- package/lite/fetch.js +39 -7
- package/lite/fetch.js.map +1 -1
- package/lite/goaway.d.ts.map +1 -1
- package/lite/goaway.js +7 -1
- package/lite/goaway.js.map +1 -1
- package/lite/group.d.ts +18 -4
- package/lite/group.d.ts.map +1 -1
- package/lite/group.js +31 -10
- package/lite/group.js.map +1 -1
- package/lite/priority.d.ts +1 -61
- package/lite/priority.d.ts.map +1 -1
- package/lite/priority.js +4 -5
- package/lite/priority.js.map +1 -1
- package/lite/publisher.d.ts +1 -69
- package/lite/publisher.d.ts.map +1 -1
- package/lite/publisher.js +593 -227
- package/lite/publisher.js.map +1 -1
- package/lite/setup.d.ts +7 -7
- package/lite/setup.d.ts.map +1 -1
- package/lite/setup.js +13 -13
- package/lite/setup.js.map +1 -1
- package/lite/subscribe.d.ts +71 -17
- package/lite/subscribe.d.ts.map +1 -1
- package/lite/subscribe.js +205 -53
- package/lite/subscribe.js.map +1 -1
- package/lite/subscriber.d.ts +15 -56
- package/lite/subscriber.d.ts.map +1 -1
- package/lite/subscriber.js +316 -188
- package/lite/subscriber.js.map +1 -1
- package/lite/track.d.ts +4 -10
- package/lite/track.d.ts.map +1 -1
- package/lite/track.js +34 -29
- package/lite/track.js.map +1 -1
- package/lite/version.d.ts +42 -6
- package/lite/version.d.ts.map +1 -1
- package/lite/version.js +126 -10
- package/lite/version.js.map +1 -1
- package/origin.d.ts +248 -29
- package/origin.d.ts.map +1 -1
- package/origin.js +1232 -37
- package/origin.js.map +1 -1
- package/package.json +8 -3
- package/path.d.ts +25 -7
- package/path.d.ts.map +1 -1
- package/path.js +5 -3
- package/path.js.map +1 -1
- package/stream.d.ts +2 -2
- package/stream.d.ts.map +1 -1
- package/stream.js +39 -25
- package/stream.js.map +1 -1
- package/tail.d.ts +11 -0
- package/tail.d.ts.map +1 -0
- package/tail.js +121 -0
- package/tail.js.map +1 -0
- package/time.d.ts +15 -2
- package/time.d.ts.map +1 -1
- package/time.js +28 -9
- package/time.js.map +1 -1
- package/track.d.ts +195 -84
- package/track.d.ts.map +1 -1
- package/track.js +694 -196
- package/track.js.map +1 -1
- package/wire.d.ts +69 -0
- package/wire.d.ts.map +1 -0
- package/wire.js +32 -0
- package/wire.js.map +1 -0
- package/zod.d.ts +1 -1
- package/zod.d.ts.map +1 -1
- package/zod.js.map +1 -1
- package/mock.d.ts +0 -66
- package/mock.d.ts.map +0 -1
- package/mock.js +0 -243
- package/mock.js.map +0 -1
package/broadcast.js
CHANGED
|
@@ -5,8 +5,12 @@
|
|
|
5
5
|
* @module
|
|
6
6
|
*/
|
|
7
7
|
import { Once, Signal } from "@moq/signals";
|
|
8
|
+
import { NotFound } from "./error.js";
|
|
9
|
+
import { Route } from "./hop.js";
|
|
8
10
|
import { hooks } from "./internal.js";
|
|
9
11
|
import * as track from "./track.js";
|
|
12
|
+
import { registerWire, trackOf } from "./wire.js";
|
|
13
|
+
let attachAnnouncer;
|
|
10
14
|
/** Reactive backing state shared by broadcast producers and consumers. */
|
|
11
15
|
class BroadcastState {
|
|
12
16
|
requested = new Signal([]);
|
|
@@ -96,11 +100,12 @@ async function resolveTrackInfo(state, name) {
|
|
|
96
100
|
// overrides it to fetch over the network (or to reject when the transport has no FETCH).
|
|
97
101
|
async function fetchGroup(state, name, sequence, options = {}) {
|
|
98
102
|
const subscriber = subscribe(state, name, { priority: options.priority });
|
|
103
|
+
hooks.exemptFetch(subscriber);
|
|
99
104
|
try {
|
|
100
105
|
for (;;) {
|
|
101
106
|
const group = await subscriber.recvGroup();
|
|
102
107
|
if (!group)
|
|
103
|
-
throw new
|
|
108
|
+
throw new NotFound(`group ${sequence}`);
|
|
104
109
|
if (group.sequence === sequence) {
|
|
105
110
|
// Close the subscription when the returned group finishes, not now: an
|
|
106
111
|
// in-progress group must keep receiving frames for its lifetime (mirrors
|
|
@@ -110,7 +115,7 @@ async function fetchGroup(state, name, sequence, options = {}) {
|
|
|
110
115
|
}
|
|
111
116
|
group.close();
|
|
112
117
|
if (group.sequence > sequence)
|
|
113
|
-
throw new
|
|
118
|
+
throw new NotFound(`group ${sequence}`);
|
|
114
119
|
}
|
|
115
120
|
}
|
|
116
121
|
catch (err) {
|
|
@@ -125,6 +130,16 @@ async function fetchGroup(state, name, sequence, options = {}) {
|
|
|
125
130
|
*/
|
|
126
131
|
export class Producer {
|
|
127
132
|
#state = new BroadcastState();
|
|
133
|
+
#announcer;
|
|
134
|
+
constructor() {
|
|
135
|
+
registerWire(this, this.#wire(false));
|
|
136
|
+
}
|
|
137
|
+
static {
|
|
138
|
+
attachAnnouncer = (producer, announcer) => {
|
|
139
|
+
producer.#announcer = announcer;
|
|
140
|
+
};
|
|
141
|
+
hooks.attachAnnouncer = attachAnnouncer;
|
|
142
|
+
}
|
|
128
143
|
/**
|
|
129
144
|
* Settles once the broadcast closes: `null` on a clean close, or the abort {@link Error}.
|
|
130
145
|
* Peek it synchronously (`undefined` while open), observe it reactively, or `await` it.
|
|
@@ -136,8 +151,7 @@ export class Producer {
|
|
|
136
151
|
consume() {
|
|
137
152
|
return makeConsumer(this.#state);
|
|
138
153
|
}
|
|
139
|
-
|
|
140
|
-
async requested() {
|
|
154
|
+
async #requested() {
|
|
141
155
|
for (;;) {
|
|
142
156
|
const request = dequeueRequest(this.#state);
|
|
143
157
|
if (request)
|
|
@@ -176,24 +190,45 @@ export class Producer {
|
|
|
176
190
|
removeTrack(name) {
|
|
177
191
|
this.#state.tracks.delete(name);
|
|
178
192
|
}
|
|
179
|
-
/**
|
|
180
|
-
|
|
181
|
-
return
|
|
193
|
+
/** A lazy read handle for a track on this broadcast. */
|
|
194
|
+
track(name) {
|
|
195
|
+
return trackOf(name, this);
|
|
182
196
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
197
|
+
#wire(register) {
|
|
198
|
+
return {
|
|
199
|
+
subscribe: (name, options) => subscribe(this.#state, name, options, register),
|
|
200
|
+
resolveTrackInfo: (name) => resolveTrackInfo(this.#state, name),
|
|
201
|
+
fetchGroup: (name, sequence, options) => fetchGroup(this.#state, name, sequence, options),
|
|
202
|
+
requested: () => this.#requested(),
|
|
203
|
+
};
|
|
186
204
|
}
|
|
187
|
-
/**
|
|
188
|
-
|
|
189
|
-
|
|
205
|
+
/**
|
|
206
|
+
* Advertise this broadcast's exact path, or re-price a standing advertisement in place.
|
|
207
|
+
*
|
|
208
|
+
* Call it once the tracks a subscriber needs first (a catalog) exist. Until then the
|
|
209
|
+
* broadcast exists for nobody, on its own origin or at a peer. Retracts on
|
|
210
|
+
* {@link unannounce} or {@link close}. Throws if this producer was not created through an
|
|
211
|
+
* origin, or if the broadcast is already closed.
|
|
212
|
+
*/
|
|
213
|
+
announce(route = Route.default) {
|
|
214
|
+
if (this.#state.closed.peek() !== undefined) {
|
|
215
|
+
throw new Error(`broadcast is closed: ${this.#state.closed.peek()}`);
|
|
216
|
+
}
|
|
217
|
+
if (!this.#announcer)
|
|
218
|
+
throw new Error("broadcast is not attached to an origin");
|
|
219
|
+
this.#announcer.announce(Route.normalize(route));
|
|
190
220
|
}
|
|
191
|
-
/**
|
|
192
|
-
|
|
193
|
-
|
|
221
|
+
/**
|
|
222
|
+
* Retract the advertisement of this broadcast's path, if any, from local consumers and
|
|
223
|
+
* peers alike. {@link announce} brings it back.
|
|
224
|
+
*/
|
|
225
|
+
unannounce() {
|
|
226
|
+
this.#announcer?.unannounce();
|
|
194
227
|
}
|
|
195
228
|
/** Close the broadcast, optionally with an error to abort waiters. Idempotent. */
|
|
196
229
|
close(abort) {
|
|
230
|
+
this.#announcer?.unannounce();
|
|
231
|
+
this.#announcer = undefined;
|
|
197
232
|
closeState(this.#state, abort);
|
|
198
233
|
}
|
|
199
234
|
}
|
|
@@ -203,8 +238,8 @@ let makeConsumer;
|
|
|
203
238
|
/**
|
|
204
239
|
* The read side of a broadcast.
|
|
205
240
|
*
|
|
206
|
-
* Created internally: obtain one from {@link Producer.consume} or
|
|
207
|
-
*
|
|
241
|
+
* Created internally: obtain one from {@link Producer.consume} or an origin request.
|
|
242
|
+
* The wire layers subclass it to resolve tracks over the network.
|
|
208
243
|
*
|
|
209
244
|
* @public
|
|
210
245
|
*/
|
|
@@ -215,6 +250,12 @@ export class Consumer {
|
|
|
215
250
|
constructor(state) {
|
|
216
251
|
this.#state = state ?? new BroadcastState();
|
|
217
252
|
this.#state.consumers++;
|
|
253
|
+
registerWire(this, {
|
|
254
|
+
subscribe: (name, options) => subscribe(this.#state, name, options, true),
|
|
255
|
+
resolveTrackInfo: (name) => resolveTrackInfo(this.#state, name),
|
|
256
|
+
fetchGroup: (name, sequence, options) => fetchGroup(this.#state, name, sequence, options),
|
|
257
|
+
requested: () => this.#requested(),
|
|
258
|
+
});
|
|
218
259
|
}
|
|
219
260
|
static {
|
|
220
261
|
makeConsumer = (state) => new Consumer(state);
|
|
@@ -247,14 +288,9 @@ export class Consumer {
|
|
|
247
288
|
}
|
|
248
289
|
/** Get a lazy handle for a track on this broadcast. Repeat subscriptions dedupe onto one upstream subscription. */
|
|
249
290
|
track(name) {
|
|
250
|
-
return
|
|
251
|
-
}
|
|
252
|
-
/** Open a live subscription to a track. Used by the subscribing wire layer. Repeat subscriptions to the same track share one upstream subscription. */
|
|
253
|
-
subscribe(name, options) {
|
|
254
|
-
return subscribe(this.#state, name, options, true);
|
|
291
|
+
return trackOf(name, this);
|
|
255
292
|
}
|
|
256
|
-
|
|
257
|
-
async requested() {
|
|
293
|
+
async #requested() {
|
|
258
294
|
for (;;) {
|
|
259
295
|
const request = dequeueRequest(this.#state);
|
|
260
296
|
if (request)
|
|
@@ -267,21 +303,6 @@ export class Consumer {
|
|
|
267
303
|
await Signal.race(this.#state.requested, this.#state.closed);
|
|
268
304
|
}
|
|
269
305
|
}
|
|
270
|
-
/**
|
|
271
|
-
* Resolve a track's immutable info. Used by track handles. This base resolves it from
|
|
272
|
-
* the local producers; the consuming wire layer overrides it to fetch over the wire.
|
|
273
|
-
*/
|
|
274
|
-
resolveTrackInfo(name) {
|
|
275
|
-
return resolveTrackInfo(this.#state, name);
|
|
276
|
-
}
|
|
277
|
-
/**
|
|
278
|
-
* Fetch a single group by sequence. Used by track handles. This base serves from the
|
|
279
|
-
* local retained window; the consuming wire layer overrides it to fetch over the wire
|
|
280
|
-
* (or to reject when the transport has no FETCH).
|
|
281
|
-
*/
|
|
282
|
-
fetchGroup(name, sequence, options) {
|
|
283
|
-
return fetchGroup(this.#state, name, sequence, options);
|
|
284
|
-
}
|
|
285
306
|
/**
|
|
286
307
|
* Release this handle. The broadcast is closed (optionally with an error to abort waiters)
|
|
287
308
|
* once this was the last live handle; while other {@link clone}s remain open it stays live.
|
package/broadcast.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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;AAE7D,OAAO,EAAE,KAAK,EAAsB,MAAM,eAAe,CAAC;AAC1D,OAAO,KAAK,KAAK,MAAM,YAAY,CAAC;AAEpC,0EAA0E;AAC1E,MAAM,cAAc;IACnB,SAAS,GAAG,IAAI,MAAM,CAAkB,EAAE,CAAC,CAAC;IAC5C,OAAO,GAAG,IAAI,GAAG,EAAiB,CAAC;IACnC,MAAM,GAAG,IAAI,IAAI,EAAgB,CAAC;IAClC,MAAM,GAAG,IAAI,GAAG,EAA0B,CAAC;IAC3C,SAAS,GAAG,IAAI,GAAG,EAAyB,CAAC;IAC7C,uFAAuF;IACvF,0FAA0F;IAC1F,SAAS,GAAG,CAAC,CAAC;CACd;AAED,SAA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AAM,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAC;IACzD,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 type { Consumer as GroupConsumer } from \"./group.ts\";\nimport { hooks, type TrackSequence } from \"./internal.ts\";\nimport * as track from \"./track.ts\";\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\ttry {\n\t\tfor (;;) {\n\t\t\tconst group = await subscriber.recvGroup();\n\t\t\tif (!group) throw new Error(`group not found: ${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 Error(`group not found: ${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 implements track.Broadcast {\n\t#state = new BroadcastState();\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\t/** Return the next track requested by a peer. */\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/** Open a live subscription to a track. Used by the publishing wire layer. */\n\tsubscribe(name: string, options?: track.Subscription): track.Subscriber {\n\t\treturn subscribe(this.#state, name, options);\n\t}\n\n\t/** Resolve a track's immutable info. Used by the publishing wire layer. */\n\tresolveTrackInfo(name: string): Promise<track.Info> {\n\t\treturn resolveTrackInfo(this.#state, name);\n\t}\n\n\t/** Fetch a single group from the local retained window. Used by track handles. */\n\tfetchGroup(name: string, sequence: number, options?: track.FetchGroupOptions): Promise<GroupConsumer> {\n\t\treturn fetchGroup(this.#state, name, sequence, options);\n\t}\n\n\t/** A lazy read handle for a track on this broadcast. */\n\ttrack(name: string): track.Consumer {\n\t\treturn new track.Consumer(name, this);\n\t}\n\n\t/** Close the broadcast, optionally with an error to abort waiters. Idempotent. */\n\tclose(abort?: Error) {\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 the connection's\n * `consume(path)`. The wire layers subclass it to resolve tracks over the network.\n *\n * @public\n */\nexport class Consumer implements track.Broadcast {\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}\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 new track.Consumer(name, this);\n\t}\n\n\t/** Open a live subscription to a track. Used by the subscribing wire layer. Repeat subscriptions to the same track share one upstream subscription. */\n\tsubscribe(name: string, options?: track.Subscription): track.Subscriber {\n\t\treturn subscribe(this.#state, name, options, true);\n\t}\n\n\t/** Return the next track requested by the local consumer. Used by the subscribing wire layer. */\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 * Resolve a track's immutable info. Used by track handles. This base resolves it from\n\t * the local producers; the consuming wire layer overrides it to fetch over the wire.\n\t */\n\tresolveTrackInfo(name: string): Promise<track.Info> {\n\t\treturn resolveTrackInfo(this.#state, name);\n\t}\n\n\t/**\n\t * Fetch a single group by sequence. Used by track handles. This base serves from the\n\t * local retained window; the consuming wire layer overrides it to fetch over the wire\n\t * (or to reject when the transport has no FETCH).\n\t */\n\tfetchGroup(name: string, sequence: number, options?: track.FetchGroupOptions): Promise<GroupConsumer> {\n\t\treturn fetchGroup(this.#state, name, sequence, options);\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,KAAK,MAAM,YAAY,CAAC;AACpC,OAAO,EAAE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,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"]}
|
package/connection/accept.d.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
|
+
import type { Consumer as OriginConsumer, Producer as OriginProducer } from "../origin";
|
|
1
2
|
import type { Established } from "./established";
|
|
2
3
|
/** Options for {@link accept}. */
|
|
3
4
|
export interface AcceptProps {
|
|
5
|
+
/** The accepted transport. */
|
|
6
|
+
transport: WebTransport;
|
|
7
|
+
/** The request URL associated with the transport. */
|
|
8
|
+
url: URL;
|
|
4
9
|
/** Version to select during SETUP negotiation (for non-ALPN paths). */
|
|
5
10
|
version?: number;
|
|
6
11
|
/**
|
|
@@ -8,6 +13,16 @@ export interface AcceptProps {
|
|
|
8
13
|
* Defaults to true.
|
|
9
14
|
*/
|
|
10
15
|
discovery?: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* The origin whose broadcasts the session announces and serves to the peer. Omit to
|
|
18
|
+
* publish nothing. Borrowed, not owned: closing the session leaves its broadcasts alone.
|
|
19
|
+
*/
|
|
20
|
+
publish?: OriginConsumer;
|
|
21
|
+
/**
|
|
22
|
+
* The origin the session feeds with the peer's announced broadcasts. Omit to discover
|
|
23
|
+
* nothing. The entries retract when the session dies; see the `consume` connect option.
|
|
24
|
+
*/
|
|
25
|
+
consume?: OriginProducer;
|
|
11
26
|
}
|
|
12
27
|
/**
|
|
13
28
|
* Server-side handshake: accepts a transport and performs the server half of the SETUP exchange.
|
|
@@ -17,5 +32,5 @@ export interface AcceptProps {
|
|
|
17
32
|
* @param props - Optional configuration
|
|
18
33
|
* @returns A promise that resolves to a Connection instance
|
|
19
34
|
*/
|
|
20
|
-
export declare function accept(transport
|
|
35
|
+
export declare function accept({ transport, url, ...props }: AcceptProps): Promise<Established>;
|
|
21
36
|
//# sourceMappingURL=accept.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"accept.d.ts","sourceRoot":"","sources":["../../src/connection/accept.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"accept.d.ts","sourceRoot":"","sources":["../../src/connection/accept.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,QAAQ,IAAI,cAAc,EAAE,QAAQ,IAAI,cAAc,EAAE,MAAM,cAAc,CAAC;AAE3F,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAIpD,kCAAkC;AAClC,MAAM,WAAW,WAAW;IAC3B,8BAA8B;IAC9B,SAAS,EAAE,YAAY,CAAC;IACxB,qDAAqD;IACrD,GAAG,EAAE,GAAG,CAAC;IAET,uEAAuE;IACvE,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB;;;OAGG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;IAEpB;;;OAGG;IACH,OAAO,CAAC,EAAE,cAAc,CAAC;IAEzB;;;OAGG;IACH,OAAO,CAAC,EAAE,cAAc,CAAC;CACzB;AAQD;;;;;;;GAOG;AACH,wBAAsB,MAAM,CAAC,EAAE,SAAS,EAAE,GAAG,EAAE,GAAG,KAAK,EAAE,EAAE,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC,CAI5F"}
|
package/connection/accept.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import * as Ietf from "../ietf/index.js";
|
|
3
3
|
import * as Lite from "../lite/index.js";
|
|
4
4
|
import { Stream } from "../stream.js";
|
|
5
|
+
import { forwardAnnounced } from "./forward.js";
|
|
5
6
|
import { exchangeSetup } from "./handshake.js";
|
|
6
7
|
/**
|
|
7
8
|
* Server-side handshake: accepts a transport and performs the server half of the SETUP exchange.
|
|
@@ -11,42 +12,60 @@ import { exchangeSetup } from "./handshake.js";
|
|
|
11
12
|
* @param props - Optional configuration
|
|
12
13
|
* @returns A promise that resolves to a Connection instance
|
|
13
14
|
*/
|
|
14
|
-
export async function accept(transport, url, props) {
|
|
15
|
+
export async function accept({ transport, url, ...props }) {
|
|
16
|
+
const connection = await acceptInner(transport, url, props);
|
|
17
|
+
if (props.consume)
|
|
18
|
+
forwardAnnounced(connection, props.consume);
|
|
19
|
+
return connection;
|
|
20
|
+
}
|
|
21
|
+
async function acceptInner(transport, url, props) {
|
|
15
22
|
// @ts-expect-error - TODO: add protocol to WebTransport
|
|
16
23
|
const protocol = transport.protocol;
|
|
17
|
-
const
|
|
24
|
+
const wiring = {
|
|
25
|
+
discovery: props.discovery ?? true,
|
|
26
|
+
publish: props.publish,
|
|
27
|
+
};
|
|
28
|
+
if (protocol === Ietf.ALPN.DRAFT_22) {
|
|
29
|
+
return acceptAlpn(transport, url, Ietf.Version.DRAFT_22, wiring);
|
|
30
|
+
}
|
|
31
|
+
if (protocol === Ietf.ALPN.DRAFT_21) {
|
|
32
|
+
return acceptAlpn(transport, url, Ietf.Version.DRAFT_21, wiring);
|
|
33
|
+
}
|
|
18
34
|
if (protocol === Ietf.ALPN.DRAFT_20) {
|
|
19
|
-
return acceptAlpn(transport, url, Ietf.Version.DRAFT_20,
|
|
35
|
+
return acceptAlpn(transport, url, Ietf.Version.DRAFT_20, wiring);
|
|
20
36
|
}
|
|
21
37
|
if (protocol === Ietf.ALPN.DRAFT_19) {
|
|
22
|
-
return acceptAlpn(transport, url, Ietf.Version.DRAFT_19,
|
|
38
|
+
return acceptAlpn(transport, url, Ietf.Version.DRAFT_19, wiring);
|
|
23
39
|
}
|
|
24
40
|
else if (protocol === Ietf.ALPN.DRAFT_18) {
|
|
25
|
-
return acceptAlpn(transport, url, Ietf.Version.DRAFT_18,
|
|
41
|
+
return acceptAlpn(transport, url, Ietf.Version.DRAFT_18, wiring);
|
|
26
42
|
}
|
|
27
43
|
else if (protocol === Ietf.ALPN.DRAFT_17) {
|
|
28
|
-
return acceptAlpn(transport, url, Ietf.Version.DRAFT_17,
|
|
44
|
+
return acceptAlpn(transport, url, Ietf.Version.DRAFT_17, wiring);
|
|
29
45
|
}
|
|
30
46
|
else if (protocol === Ietf.ALPN.DRAFT_16) {
|
|
31
|
-
return acceptSetup(transport, url, Ietf.Version.DRAFT_16,
|
|
47
|
+
return acceptSetup(transport, url, Ietf.Version.DRAFT_16, wiring);
|
|
32
48
|
}
|
|
33
49
|
else if (protocol === Ietf.ALPN.DRAFT_15) {
|
|
34
|
-
return acceptSetup(transport, url, Ietf.Version.DRAFT_15,
|
|
50
|
+
return acceptSetup(transport, url, Ietf.Version.DRAFT_15, wiring);
|
|
51
|
+
}
|
|
52
|
+
else if (protocol === Lite.ALPN_07_WIP) {
|
|
53
|
+
return new Lite.Connection({ url, quic: transport, version: Lite.Version.DRAFT_07, ...wiring });
|
|
35
54
|
}
|
|
36
|
-
else if (protocol === Lite.
|
|
37
|
-
return new Lite.Connection({ url, quic: transport, version: Lite.Version.DRAFT_06,
|
|
55
|
+
else if (protocol === Lite.ALPN_06) {
|
|
56
|
+
return new Lite.Connection({ url, quic: transport, version: Lite.Version.DRAFT_06, ...wiring });
|
|
38
57
|
}
|
|
39
58
|
else if (protocol === Lite.ALPN_05) {
|
|
40
|
-
return new Lite.Connection({ url, quic: transport, version: Lite.Version.DRAFT_05,
|
|
59
|
+
return new Lite.Connection({ url, quic: transport, version: Lite.Version.DRAFT_05, ...wiring });
|
|
41
60
|
}
|
|
42
61
|
else if (protocol === Lite.ALPN_04) {
|
|
43
|
-
return new Lite.Connection({ url, quic: transport, version: Lite.Version.DRAFT_04,
|
|
62
|
+
return new Lite.Connection({ url, quic: transport, version: Lite.Version.DRAFT_04, ...wiring });
|
|
44
63
|
}
|
|
45
64
|
else if (protocol === Lite.ALPN_03) {
|
|
46
|
-
return new Lite.Connection({ url, quic: transport, version: Lite.Version.DRAFT_03,
|
|
65
|
+
return new Lite.Connection({ url, quic: transport, version: Lite.Version.DRAFT_03, ...wiring });
|
|
47
66
|
}
|
|
48
67
|
else if (protocol === Lite.ALPN || protocol === "" || protocol === undefined) {
|
|
49
|
-
return acceptNegotiated(transport, url, props);
|
|
68
|
+
return acceptNegotiated(transport, url, wiring, props.version);
|
|
50
69
|
}
|
|
51
70
|
else {
|
|
52
71
|
throw new Error(`unsupported WebTransport protocol: ${protocol}`);
|
|
@@ -56,15 +75,16 @@ export async function accept(transport, url, props) {
|
|
|
56
75
|
* Draft-17+ accept: ALPN already pinned the version. SETUP is exchanged over
|
|
57
76
|
* a pair of uni streams using stream type 0x2F00.
|
|
58
77
|
*/
|
|
59
|
-
async function acceptAlpn(transport, url, version,
|
|
60
|
-
const { control, solicit, cluster } = await exchangeSetup(transport, version, "moq-lite-js");
|
|
78
|
+
async function acceptAlpn(transport, url, version, wiring) {
|
|
79
|
+
const { control, solicit, hidden, cluster } = await exchangeSetup(transport, version, "moq-lite-js");
|
|
61
80
|
return new Ietf.Connection({
|
|
62
|
-
|
|
81
|
+
...wiring,
|
|
63
82
|
client: false,
|
|
64
83
|
url,
|
|
65
84
|
quic: transport,
|
|
66
85
|
control,
|
|
67
86
|
solicit,
|
|
87
|
+
hidden,
|
|
68
88
|
cluster,
|
|
69
89
|
// v17+ uses NativeSession which manages its own request IDs; maxRequestId is unused.
|
|
70
90
|
maxRequestId: 0n,
|
|
@@ -75,7 +95,7 @@ async function acceptAlpn(transport, url, version, discovery) {
|
|
|
75
95
|
* Legacy accept (draft-15/16): ALPN pinned the version, but the SETUP message
|
|
76
96
|
* is still exchanged over a bidi stream wrapped in the moq-lite compat envelope.
|
|
77
97
|
*/
|
|
78
|
-
async function acceptSetup(transport, url, version,
|
|
98
|
+
async function acceptSetup(transport, url, version, wiring) {
|
|
79
99
|
// Accept bidi, read ClientSetup, write ServerSetup
|
|
80
100
|
const stream = await Stream.accept(transport);
|
|
81
101
|
if (!stream)
|
|
@@ -91,11 +111,12 @@ async function acceptSetup(transport, url, version, discovery) {
|
|
|
91
111
|
params.setVarint(Ietf.SetupOption.MaxRequestId, 42069n);
|
|
92
112
|
params.setBytes(Ietf.SetupOption.Implementation, encoder.encode("moq-lite-js"));
|
|
93
113
|
Ietf.solicitIntoSetup(params);
|
|
114
|
+
Ietf.hiddenIntoSetup(params);
|
|
94
115
|
const server = new Ietf.ServerSetup({ version, parameters: params });
|
|
95
116
|
await server.encode(stream.writer, version);
|
|
96
117
|
const maxRequestId = 42069n;
|
|
97
118
|
return new Ietf.Connection({
|
|
98
|
-
|
|
119
|
+
...wiring,
|
|
99
120
|
client: false,
|
|
100
121
|
url,
|
|
101
122
|
quic: transport,
|
|
@@ -103,10 +124,10 @@ async function acceptSetup(transport, url, version, discovery) {
|
|
|
103
124
|
maxRequestId,
|
|
104
125
|
version,
|
|
105
126
|
solicit: Ietf.solicitFromSetup(client.parameters),
|
|
127
|
+
hidden: Ietf.hiddenFromSetup(client.parameters),
|
|
106
128
|
});
|
|
107
129
|
}
|
|
108
|
-
async function acceptNegotiated(transport, url,
|
|
109
|
-
const discovery = props?.discovery ?? true;
|
|
130
|
+
async function acceptNegotiated(transport, url, wiring, version) {
|
|
110
131
|
const setupVersion = Ietf.Version.DRAFT_14;
|
|
111
132
|
const stream = await Stream.accept(transport);
|
|
112
133
|
if (!stream)
|
|
@@ -119,8 +140,8 @@ async function acceptNegotiated(transport, url, props) {
|
|
|
119
140
|
// Pick the requested version, or first matching version from client's list
|
|
120
141
|
const allVersions = [...Object.values(Lite.Version), ...Object.values(Ietf.Version)];
|
|
121
142
|
let selectedVersion;
|
|
122
|
-
if (
|
|
123
|
-
selectedVersion =
|
|
143
|
+
if (version !== undefined) {
|
|
144
|
+
selectedVersion = version;
|
|
124
145
|
}
|
|
125
146
|
else {
|
|
126
147
|
const match = client.versions.find((v) => allVersions.includes(v));
|
|
@@ -135,6 +156,7 @@ async function acceptNegotiated(transport, url, props) {
|
|
|
135
156
|
params.setVarint(Ietf.SetupOption.MaxRequestId, 42069n);
|
|
136
157
|
params.setBytes(Ietf.SetupOption.Implementation, encoder.encode("moq-lite-js"));
|
|
137
158
|
Ietf.solicitIntoSetup(params);
|
|
159
|
+
Ietf.hiddenIntoSetup(params);
|
|
138
160
|
const server = new Ietf.ServerSetup({ version: selectedVersion, parameters: params });
|
|
139
161
|
await server.encode(stream.writer, setupVersion);
|
|
140
162
|
if (Object.values(Lite.Version).includes(selectedVersion)) {
|
|
@@ -143,13 +165,13 @@ async function acceptNegotiated(transport, url, props) {
|
|
|
143
165
|
quic: transport,
|
|
144
166
|
version: selectedVersion,
|
|
145
167
|
session: stream,
|
|
146
|
-
|
|
168
|
+
...wiring,
|
|
147
169
|
});
|
|
148
170
|
}
|
|
149
171
|
else if (Object.values(Ietf.Version).includes(selectedVersion)) {
|
|
150
172
|
const maxRequestId = client.parameters.getVarint(Ietf.SetupOption.MaxRequestId) ?? 0n;
|
|
151
173
|
return new Ietf.Connection({
|
|
152
|
-
|
|
174
|
+
...wiring,
|
|
153
175
|
client: false,
|
|
154
176
|
url,
|
|
155
177
|
quic: transport,
|
|
@@ -157,6 +179,7 @@ async function acceptNegotiated(transport, url, props) {
|
|
|
157
179
|
maxRequestId,
|
|
158
180
|
version: selectedVersion,
|
|
159
181
|
solicit: Ietf.solicitFromSetup(client.parameters),
|
|
182
|
+
hidden: Ietf.hiddenFromSetup(client.parameters),
|
|
160
183
|
});
|
|
161
184
|
}
|
|
162
185
|
else {
|
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;AACzC,OAAO,EAAE,MAAM,EAA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AAC;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 { Stream } from \"../stream.ts\";\nimport type { Established } from \"./established.ts\";\nimport { exchangeSetup } from \"./handshake.ts\";\n\n/** Options for {@link accept}. */\nexport interface AcceptProps {\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\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: WebTransport, url: URL, props?: AcceptProps): Promise<Established> {\n\t// @ts-expect-error - TODO: add protocol to WebTransport\n\tconst protocol: string | undefined = transport.protocol;\n\n\tconst discovery = props?.discovery ?? true;\n\n\tif (protocol === Ietf.ALPN.DRAFT_20) {\n\t\treturn acceptAlpn(transport, url, Ietf.Version.DRAFT_20, discovery);\n\t}\n\tif (protocol === Ietf.ALPN.DRAFT_19) {\n\t\treturn acceptAlpn(transport, url, Ietf.Version.DRAFT_19, discovery);\n\t} else if (protocol === Ietf.ALPN.DRAFT_18) {\n\t\treturn acceptAlpn(transport, url, Ietf.Version.DRAFT_18, discovery);\n\t} else if (protocol === Ietf.ALPN.DRAFT_17) {\n\t\treturn acceptAlpn(transport, url, Ietf.Version.DRAFT_17, discovery);\n\t} else if (protocol === Ietf.ALPN.DRAFT_16) {\n\t\treturn acceptSetup(transport, url, Ietf.Version.DRAFT_16, discovery);\n\t} else if (protocol === Ietf.ALPN.DRAFT_15) {\n\t\treturn acceptSetup(transport, url, Ietf.Version.DRAFT_15, discovery);\n\t} else if (protocol === Lite.ALPN_06_WIP) {\n\t\treturn new Lite.Connection({ url, quic: transport, version: Lite.Version.DRAFT_06, discovery });\n\t} else if (protocol === Lite.ALPN_05) {\n\t\treturn new Lite.Connection({ url, quic: transport, version: Lite.Version.DRAFT_05, discovery });\n\t} else if (protocol === Lite.ALPN_04) {\n\t\treturn new Lite.Connection({ url, quic: transport, version: Lite.Version.DRAFT_04, discovery });\n\t} else if (protocol === Lite.ALPN_03) {\n\t\treturn new Lite.Connection({ url, quic: transport, version: Lite.Version.DRAFT_03, discovery });\n\t} else if (protocol === Lite.ALPN || protocol === \"\" || protocol === undefined) {\n\t\treturn acceptNegotiated(transport, url, props);\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\tdiscovery: boolean,\n): Promise<Established> {\n\tconst { control, solicit, cluster } = await exchangeSetup(transport, version, \"moq-lite-js\");\n\n\treturn new Ietf.Connection({\n\t\tdiscovery,\n\t\tclient: false,\n\t\turl,\n\t\tquic: transport,\n\t\tcontrol,\n\t\tsolicit,\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\tdiscovery: boolean,\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\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\tdiscovery,\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});\n}\n\nasync function acceptNegotiated(transport: WebTransport, url: URL, props?: AcceptProps): Promise<Established> {\n\tconst discovery = props?.discovery ?? true;\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 (props?.version !== undefined) {\n\t\tselectedVersion = props.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\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\tdiscovery,\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\tdiscovery,\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});\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,MA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;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 +1 @@
|
|
|
1
|
-
{"version":3,"file":"browser.d.ts","sourceRoot":"","sources":["../../src/connection/browser.ts"],"names":[],"mappings":"AAEA,qFAAqF;AACrF,wBAAgB,gCAAgC,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,
|
|
1
|
+
{"version":3,"file":"browser.d.ts","sourceRoot":"","sources":["../../src/connection/browser.ts"],"names":[],"mappings":"AAEA,qFAAqF;AACrF,wBAAgB,gCAAgC,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAsB3E;AAED,kEAAkE;AAClE,wBAAgB,uBAAuB,IAAI,OAAO,CAIjD"}
|
package/connection/browser.js
CHANGED
|
@@ -3,19 +3,21 @@ import Bowser from "bowser";
|
|
|
3
3
|
/** Returns whether a browser user agent has a usable WebTransport implementation. */
|
|
4
4
|
export function isWebTransportUserAgentSupported(userAgent) {
|
|
5
5
|
const browser = Bowser.getParser(userAgent);
|
|
6
|
+
// Every WebKit engine stalls long sessions: the flow-control window never
|
|
7
|
+
// refills (https://bugs.webkit.org/show_bug.cgi?id=319818), and incoming
|
|
8
|
+
// unidirectional streams stop for good after roughly 7,600 of them or 16 MiB
|
|
9
|
+
// on one session (moq-dev/moq#2388); one stream per group reaches that in
|
|
10
|
+
// about two minutes. Chrome, Firefox and Edge on iOS are WebKit under the
|
|
11
|
+
// hood, and iPadOS Safari reports macOS, so gate on the engine and the OS.
|
|
12
|
+
if (browser.getEngine().name === "WebKit" || browser.getOS().name === "iOS")
|
|
13
|
+
return false;
|
|
6
14
|
const supported = browser.satisfies({
|
|
7
15
|
// Fixed with 153.0.0, Firefox only allows two concurrent remote-initiated streams:
|
|
8
16
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=2046262
|
|
9
17
|
firefox: ">=153.0",
|
|
10
|
-
// Safari's flow-control window never refills, which permanently stalls sessions:
|
|
11
|
-
// https://bugs.webkit.org/show_bug.cgi?id=319818
|
|
12
|
-
// It also stops delivering incoming unidirectional streams for good after
|
|
13
|
-
// roughly 7,600 of them or 16 MiB on one session, whichever comes first
|
|
14
|
-
// (moq-dev/moq#2388); one stream per group reaches that in about two minutes.
|
|
15
|
-
safari: "<0",
|
|
16
18
|
});
|
|
17
19
|
if (supported === undefined) {
|
|
18
|
-
//By default, other browsers are considered to support WebTransport.
|
|
20
|
+
// By default, other browsers are considered to support WebTransport.
|
|
19
21
|
return true;
|
|
20
22
|
}
|
|
21
23
|
return supported;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"browser.js","sourceRoot":"","sources":["../../src/connection/browser.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,qFAAqF;AACrF,MAAM,UAAU,gCAAgC,CAAC,SAAiB;IACjE,MAAM,OAAO,GAAG,MAAM,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"browser.js","sourceRoot":"","sources":["../../src/connection/browser.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,qFAAqF;AACrF,MAAM,UAAU,gCAAgC,CAAC,SAAiB;IACjE,MAAM,OAAO,GAAG,MAAM,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC;IAE5C,0EAA0E;IAC1E,yEAAyE;IACzE,6EAA6E;IAC7E,0EAA0E;IAC1E,0EAA0E;IAC1E,2EAA2E;IAC3E,IAAI,OAAO,CAAC,SAAS,EAAE,CAAC,IAAI,KAAK,QAAQ,IAAI,OAAO,CAAC,KAAK,EAAE,CAAC,IAAI,KAAK,KAAK;QAAE,OAAO,KAAK,CAAC;IAE1F,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC;QACnC,mFAAmF;QACnF,uDAAuD;QACvD,OAAO,EAAE,SAAS;KAClB,CAAC,CAAC;IAEH,IAAI,SAAS,KAAK,SAAS,EAAE,CAAC;QAC7B,qEAAqE;QACrE,OAAO,IAAI,CAAC;IACb,CAAC;IACD,OAAO,SAAS,CAAC;AAClB,CAAC;AAED,kEAAkE;AAClE,MAAM,UAAU,uBAAuB;IACtC,IAAI,OAAO,UAAU,CAAC,YAAY,KAAK,WAAW;QAAE,OAAO,KAAK,CAAC;IACjE,IAAI,OAAO,SAAS,KAAK,WAAW;QAAE,OAAO,IAAI,CAAC;IAClD,OAAO,gCAAgC,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC;AAC9D,CAAC","sourcesContent":["import Bowser from \"bowser\";\n\n/** Returns whether a browser user agent has a usable WebTransport implementation. */\nexport function isWebTransportUserAgentSupported(userAgent: string): boolean {\n\tconst browser = Bowser.getParser(userAgent);\n\n\t// Every WebKit engine stalls long sessions: the flow-control window never\n\t// refills (https://bugs.webkit.org/show_bug.cgi?id=319818), and incoming\n\t// unidirectional streams stop for good after roughly 7,600 of them or 16 MiB\n\t// on one session (moq-dev/moq#2388); one stream per group reaches that in\n\t// about two minutes. Chrome, Firefox and Edge on iOS are WebKit under the\n\t// hood, and iPadOS Safari reports macOS, so gate on the engine and the OS.\n\tif (browser.getEngine().name === \"WebKit\" || browser.getOS().name === \"iOS\") return false;\n\n\tconst supported = browser.satisfies({\n\t\t// Fixed with 153.0.0, Firefox only allows two concurrent remote-initiated streams:\n\t\t// https://bugzilla.mozilla.org/show_bug.cgi?id=2046262\n\t\tfirefox: \">=153.0\",\n\t});\n\n\tif (supported === undefined) {\n\t\t// By default, other browsers are considered to support WebTransport.\n\t\treturn true;\n\t}\n\treturn supported;\n}\n\n/** Returns whether this runtime can connect with WebTransport. */\nexport function isWebTransportSupported(): boolean {\n\tif (typeof globalThis.WebTransport === \"undefined\") return false;\n\tif (typeof navigator === \"undefined\") return true;\n\treturn isWebTransportUserAgentSupported(navigator.userAgent);\n}\n"]}
|