@caprid/moq-core 0.4.1 → 0.6.0
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/README.md +2 -2
- package/UPSTREAM.json +2 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/protocol/control-messages.d.ts +24 -7
- package/dist/protocol/control-messages.d.ts.map +1 -1
- package/dist/protocol/control-messages.js +28 -8
- package/dist/protocol/control-messages.js.map +1 -1
- package/dist/protocol/duplex-session.d.ts +68 -0
- package/dist/protocol/duplex-session.d.ts.map +1 -0
- package/dist/protocol/duplex-session.js +297 -0
- package/dist/protocol/duplex-session.js.map +1 -0
- package/dist/protocol/publish-session.d.ts +35 -10
- package/dist/protocol/publish-session.d.ts.map +1 -1
- package/dist/protocol/publish-session.js +247 -99
- package/dist/protocol/publish-session.js.map +1 -1
- package/dist/protocol/request-stream.d.ts +12 -0
- package/dist/protocol/request-stream.d.ts.map +1 -1
- package/dist/protocol/request-stream.js +40 -4
- package/dist/protocol/request-stream.js.map +1 -1
- package/dist/protocol/session.d.ts +28 -6
- package/dist/protocol/session.d.ts.map +1 -1
- package/dist/protocol/session.js +225 -53
- package/dist/protocol/session.js.map +1 -1
- package/package.json +1 -1
package/dist/protocol/session.js
CHANGED
|
@@ -21,7 +21,8 @@ import { StreamReader, utf8Encode } from './bytes.js';
|
|
|
21
21
|
import { decodeControlMessage, encodeFetch, encodeRequestError, encodeRequestOk, encodeRequestUpdate, encodeSetup, encodeSubscribe, encodeSubscribeNamespace, encodeTrackStatus, MESSAGE_TYPE, REQUEST_ERROR_CODE, resolveMoqtProtocol, SETUP_OPTION, } from './control-messages.js';
|
|
22
22
|
import { isMoqtProtocolError, MoqtProtocolError, SESSION_ERROR } from './errors.js';
|
|
23
23
|
import { isSubgroupHeaderType, readFetchEntries, readFetchHeader, readSubgroupHeader, readSubgroupObjects, STREAM_TYPE, } from './object-stream.js';
|
|
24
|
-
import { openRequestStream } from './request-stream.js';
|
|
24
|
+
import { openRequestStream, readRequest } from './request-stream.js';
|
|
25
|
+
import { isSessionError } from './transport-error.js';
|
|
25
26
|
/** Validate once; returned promises are diagnosed and observed, never awaited by delivery. */
|
|
26
27
|
function synchronousSubscriptionHandlers(handlers) {
|
|
27
28
|
const result = {};
|
|
@@ -87,6 +88,46 @@ const streamFailures = new WeakSet();
|
|
|
87
88
|
export function isStreamFailure(error) {
|
|
88
89
|
return streamFailures.has(error);
|
|
89
90
|
}
|
|
91
|
+
function declares(options, type) {
|
|
92
|
+
return options.some((option) => option.type === type);
|
|
93
|
+
}
|
|
94
|
+
const PENDING = Symbol('pending');
|
|
95
|
+
/** Whether `promise` had already settled when this was called: one microtask turn, whichever handler runs first. */
|
|
96
|
+
async function settled(promise) {
|
|
97
|
+
try {
|
|
98
|
+
return (await Promise.race([promise, Promise.resolve(PENDING)])) !== PENDING;
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
function activeCountViolation(answers, negotiated, parameters) {
|
|
105
|
+
const carried = parameters.activeCount !== undefined;
|
|
106
|
+
if (answers !== 'subscribe-namespace') {
|
|
107
|
+
return carried ? `ACTIVE_COUNT on a REQUEST_OK answering ${answers}` : undefined;
|
|
108
|
+
}
|
|
109
|
+
if (negotiated && !carried)
|
|
110
|
+
return 'REQUEST_OK omits ACTIVE_COUNT, which the session negotiated';
|
|
111
|
+
if (!negotiated && carried)
|
|
112
|
+
return 'REQUEST_OK carries ACTIVE_COUNT, which the session did not negotiate';
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
function countedStep(owed, input) {
|
|
116
|
+
if (owed === 0)
|
|
117
|
+
return { owed, synced: false };
|
|
118
|
+
switch (input) {
|
|
119
|
+
case 'namespace':
|
|
120
|
+
return { owed: owed - 1, synced: owed === 1 };
|
|
121
|
+
case 'namespace-done':
|
|
122
|
+
return { violation: 'NAMESPACE_DONE inside the ACTIVE_COUNT set' };
|
|
123
|
+
case 'goaway':
|
|
124
|
+
return { violation: 'GOAWAY inside the ACTIVE_COUNT set' };
|
|
125
|
+
case 'fin':
|
|
126
|
+
return { violation: 'subscribe-namespace stream finished inside the ACTIVE_COUNT set' };
|
|
127
|
+
case 'stream-failed':
|
|
128
|
+
return { owed, synced: false };
|
|
129
|
+
}
|
|
130
|
+
}
|
|
90
131
|
// ============================================================================
|
|
91
132
|
// Session driver
|
|
92
133
|
// ============================================================================
|
|
@@ -100,25 +141,38 @@ class MoqtSessionImpl {
|
|
|
100
141
|
/** Client request IDs are even, starting at 0; a server's are odd, starting at 1 (§6.4.2.1). */
|
|
101
142
|
#nextRequestId;
|
|
102
143
|
/** Every inbound request ID ever seen. Reuse, or this endpoint's own parity, closes the session (§6.4.2.1). */
|
|
103
|
-
#peerRequestIds
|
|
144
|
+
#peerRequestIds;
|
|
104
145
|
#subscriptions = new Map();
|
|
105
146
|
#fetches = new Map();
|
|
106
147
|
#trackStatuses = new Map();
|
|
107
148
|
#namespaceSubscriptions = new Map();
|
|
108
149
|
#aliasRoutes = new Map();
|
|
109
150
|
#aliasWaiters = new Map();
|
|
151
|
+
/**
|
|
152
|
+
* Every request stream with a direction still open, answered or not: teardown cancels them all, whatever the maps
|
|
153
|
+
* still hold.
|
|
154
|
+
* Held weakly, so a request the session has forgotten (#419) keeps nothing alive here; one the transport still
|
|
155
|
+
* holds is still reached.
|
|
156
|
+
*/
|
|
157
|
+
#requestStreams = new Set();
|
|
158
|
+
/** Every incoming data stream still being read. The peer's control stream is not one: stopping it ends the session. */
|
|
159
|
+
#dataReaders = new Set();
|
|
160
|
+
/** Drops each incoming request the application has yet to answer. */
|
|
161
|
+
#incomingRequests = new Set();
|
|
110
162
|
#resolveReady;
|
|
111
163
|
#rejectReady;
|
|
112
164
|
#controlWriter;
|
|
113
165
|
#receivedServerSetup = false;
|
|
114
166
|
/** The server's SETUP declared SETUP_OPTION.MAX_AGE. */
|
|
115
167
|
#maxAgeSupported = false;
|
|
168
|
+
#activeCountNegotiated = false;
|
|
116
169
|
#receivedControlGoaway = false;
|
|
117
170
|
#destroyed = false;
|
|
118
171
|
constructor(transport, config = {}) {
|
|
119
172
|
this.#transport = transport;
|
|
120
173
|
this.#protocol = resolveMoqtProtocol(transport.protocol);
|
|
121
174
|
this.#config = config;
|
|
175
|
+
this.#peerRequestIds = config.peerRequestIds ?? new Set();
|
|
122
176
|
this.#callbacks = config.callbacks ?? {};
|
|
123
177
|
this.#nextRequestId = config.perspective === 'server' ? 1 : 0;
|
|
124
178
|
this.ready = new Promise((resolve, reject) => {
|
|
@@ -156,6 +210,15 @@ class MoqtSessionImpl {
|
|
|
156
210
|
try {
|
|
157
211
|
const control = await this.#transport.createUnidirectionalStream();
|
|
158
212
|
this.#controlWriter = control.getWriter();
|
|
213
|
+
// A control stream MUST stay open for the session's lifetime (§6.3): a peer stopping it has ended the session, as
|
|
214
|
+
// the publish driver also treats it. A clean local close resolves the writer instead, and #fatal no-ops once
|
|
215
|
+
// #handleClosed has run. A session-wide failure (a peer's clean close fails every stream as it settles `closed`)
|
|
216
|
+
// is the transport's to report: `closed` says whether it was clean.
|
|
217
|
+
void this.#controlWriter.closed.catch(async (error) => {
|
|
218
|
+
if (isSessionError(error) || (await settled(this.#transport.closed)))
|
|
219
|
+
return;
|
|
220
|
+
this.#fatal(error);
|
|
221
|
+
});
|
|
159
222
|
const options = [
|
|
160
223
|
{
|
|
161
224
|
type: SETUP_OPTION.MOQT_IMPLEMENTATION,
|
|
@@ -209,10 +272,17 @@ class MoqtSessionImpl {
|
|
|
209
272
|
...this.#namespaceSubscriptions.values(),
|
|
210
273
|
];
|
|
211
274
|
const pending = expected ? [] : records.filter((r) => !r.settled && !r.cancelled);
|
|
212
|
-
for (const record of records)
|
|
275
|
+
for (const record of records)
|
|
213
276
|
this.#settleRequest(record);
|
|
214
|
-
|
|
215
|
-
|
|
277
|
+
for (const stream of this.#requestStreams)
|
|
278
|
+
stream.deref()?.cancel(error);
|
|
279
|
+
for (const drop of this.#incomingRequests)
|
|
280
|
+
drop();
|
|
281
|
+
this.#incomingRequests.clear();
|
|
282
|
+
for (const reader of this.#dataReaders)
|
|
283
|
+
void reader.cancel(error).catch(() => { });
|
|
284
|
+
this.#requestStreams.clear();
|
|
285
|
+
this.#dataReaders.clear();
|
|
216
286
|
for (const record of this.#subscriptions.values())
|
|
217
287
|
this.#removeSubscription(record);
|
|
218
288
|
this.#subscriptions.clear();
|
|
@@ -277,6 +347,8 @@ class MoqtSessionImpl {
|
|
|
277
347
|
subscribe(options, callbacks = {}) {
|
|
278
348
|
const handlers = synchronousSubscriptionHandlers(callbacks);
|
|
279
349
|
const requestId = this.#allocateRequestId();
|
|
350
|
+
if (this.#destroyed)
|
|
351
|
+
return { requestId, update: () => { }, cancel: () => { } };
|
|
280
352
|
const record = { requestId, handlers, cancelled: false, settled: false, readers: new Set() };
|
|
281
353
|
this.#subscriptions.set(requestId, record);
|
|
282
354
|
const message = encodeSubscribe({
|
|
@@ -285,7 +357,7 @@ class MoqtSessionImpl {
|
|
|
285
357
|
trackName: options.trackName,
|
|
286
358
|
parameters: this.#outgoingParameters(options.parameters, true),
|
|
287
359
|
}, this.#protocol);
|
|
288
|
-
void this.#openRequest(message, record, (msg) => this.#handleSubscriptionMessage(record, msg), (error) => {
|
|
360
|
+
void this.#openRequest('subscribe', message, record, (msg) => this.#handleSubscriptionMessage(record, msg), (error) => {
|
|
289
361
|
this.#removeSubscription(record);
|
|
290
362
|
handlers.onError?.(error);
|
|
291
363
|
}, () => {
|
|
@@ -309,6 +381,8 @@ class MoqtSessionImpl {
|
|
|
309
381
|
}
|
|
310
382
|
fetch(options, handlers = {}) {
|
|
311
383
|
const requestId = this.#allocateRequestId();
|
|
384
|
+
if (this.#destroyed)
|
|
385
|
+
return { requestId, cancel: () => { } };
|
|
312
386
|
const record = {
|
|
313
387
|
requestId,
|
|
314
388
|
handlers,
|
|
@@ -322,9 +396,13 @@ class MoqtSessionImpl {
|
|
|
322
396
|
...options,
|
|
323
397
|
parameters: this.#outgoingParameters(options.parameters, false),
|
|
324
398
|
};
|
|
325
|
-
void this.#openRequest(encodeFetch(request, this.#protocol), record, (msg) => this.#handleFetchMessage(record, msg), (error) => {
|
|
399
|
+
void this.#openRequest('fetch', encodeFetch(request, this.#protocol), record, (msg) => this.#handleFetchMessage(record, msg), (error) => {
|
|
326
400
|
this.#fetches.delete(requestId);
|
|
327
401
|
handlers.onError?.(error);
|
|
402
|
+
}, () => {
|
|
403
|
+
// §6.4.2.2: the requester SHOULD FIN once the peer's FIN completes the request. Left open, the stream would
|
|
404
|
+
// outlive a fetch the session has already forgotten.
|
|
405
|
+
record.stream?.finWrite().catch(() => { });
|
|
328
406
|
});
|
|
329
407
|
return {
|
|
330
408
|
requestId,
|
|
@@ -332,6 +410,8 @@ class MoqtSessionImpl {
|
|
|
332
410
|
};
|
|
333
411
|
}
|
|
334
412
|
trackStatus(options, handlers = {}) {
|
|
413
|
+
if (this.#destroyed)
|
|
414
|
+
return;
|
|
335
415
|
const requestId = this.#allocateRequestId();
|
|
336
416
|
const record = { requestId, handlers, cancelled: false, settled: false };
|
|
337
417
|
// Tracked so session teardown fails a still-pending TRACK_STATUS the
|
|
@@ -344,7 +424,7 @@ class MoqtSessionImpl {
|
|
|
344
424
|
trackName: options.trackName,
|
|
345
425
|
parameters: this.#outgoingParameters(options.parameters, false),
|
|
346
426
|
}, this.#protocol);
|
|
347
|
-
void this.#openRequest(message, record, (msg) => {
|
|
427
|
+
void this.#openRequest('track-status', message, record, (msg) => {
|
|
348
428
|
if (msg.kind === 'request-ok') {
|
|
349
429
|
this.#settleRequest(record);
|
|
350
430
|
this.#trackStatuses.delete(requestId);
|
|
@@ -364,12 +444,15 @@ class MoqtSessionImpl {
|
|
|
364
444
|
}
|
|
365
445
|
subscribeNamespace(options, handlers = {}) {
|
|
366
446
|
const requestId = this.#allocateRequestId();
|
|
447
|
+
if (this.#destroyed)
|
|
448
|
+
return { requestId, cancel: () => { } };
|
|
367
449
|
const record = {
|
|
368
450
|
requestId,
|
|
369
451
|
handlers,
|
|
370
452
|
cancelled: false,
|
|
371
453
|
settled: false,
|
|
372
454
|
active: new Map(),
|
|
455
|
+
owed: 0,
|
|
373
456
|
};
|
|
374
457
|
const message = encodeSubscribeNamespace({
|
|
375
458
|
requestId,
|
|
@@ -379,6 +462,8 @@ class MoqtSessionImpl {
|
|
|
379
462
|
this.#namespaceSubscriptions.set(requestId, record);
|
|
380
463
|
// Once only, and never after REQUEST_ERROR answered the request: that removes the record first.
|
|
381
464
|
const end = (error) => {
|
|
465
|
+
if (this.#count(record, error === undefined ? 'fin' : 'stream-failed') === undefined)
|
|
466
|
+
return;
|
|
382
467
|
if (!this.#namespaceSubscriptions.delete(requestId))
|
|
383
468
|
return;
|
|
384
469
|
// A failed stream is dropped here: session teardown only reaches the records it still holds.
|
|
@@ -387,11 +472,15 @@ class MoqtSessionImpl {
|
|
|
387
472
|
// §9.15: a FIN or reset on the response stream SHOULD read as NAMESPACE_DONE for each active namespace.
|
|
388
473
|
const active = [...record.active.values()];
|
|
389
474
|
record.active.clear();
|
|
390
|
-
for (const suffix of active)
|
|
391
|
-
|
|
392
|
-
|
|
475
|
+
for (const suffix of active) {
|
|
476
|
+
if (!this.#live(record))
|
|
477
|
+
return;
|
|
478
|
+
handlers.onNamespaceDone?.(suffix, { requestEnded: true });
|
|
479
|
+
}
|
|
480
|
+
if (this.#live(record))
|
|
481
|
+
handlers.onEnd?.(error === undefined ? {} : { error });
|
|
393
482
|
};
|
|
394
|
-
void this.#openRequest(message, record, (msg) => this.#handleNamespaceMessage(record, msg), (error) => {
|
|
483
|
+
void this.#openRequest('subscribe-namespace', message, record, (msg) => this.#handleNamespaceMessage(record, msg), (error) => {
|
|
395
484
|
// Removing the record puts the stream out of teardown's reach, so drop it now.
|
|
396
485
|
record.stream?.cancel(error);
|
|
397
486
|
this.#namespaceSubscriptions.delete(requestId);
|
|
@@ -423,11 +512,13 @@ class MoqtSessionImpl {
|
|
|
423
512
|
* still kills the whole session. Once `record.settled` is set the same events are ordinary end-of-request signals and
|
|
424
513
|
* are ignored, except that a FIN calls `onEnd` and a stream error calls `onSettledError` when given.
|
|
425
514
|
*/
|
|
426
|
-
async #openRequest(message, record, onMessage, onFailure, onEnd, onSettledError) {
|
|
515
|
+
async #openRequest(kind, message, record, onMessage, onFailure, onEnd, onSettledError) {
|
|
427
516
|
const fail = (error) => {
|
|
428
517
|
if (record.settled || record.cancelled || this.#destroyed)
|
|
429
518
|
return;
|
|
430
519
|
this.#settleRequest(record);
|
|
520
|
+
// A request that died unanswered keeps no stream: its sending direction would otherwise stay open.
|
|
521
|
+
record.stream?.cancel(error);
|
|
431
522
|
onFailure(error);
|
|
432
523
|
};
|
|
433
524
|
let stream;
|
|
@@ -442,6 +533,12 @@ class MoqtSessionImpl {
|
|
|
442
533
|
this.#fatal(error);
|
|
443
534
|
return;
|
|
444
535
|
}
|
|
536
|
+
// The session ended while the stream was opening: nothing is sent on it, and nothing it carries is reported.
|
|
537
|
+
if (this.#destroyed) {
|
|
538
|
+
void stream.readable.cancel().catch(() => { });
|
|
539
|
+
void stream.writable.abort().catch(() => { });
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
445
542
|
const timeout = this.#config.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS;
|
|
446
543
|
if (timeout > 0 && !record.settled && !record.cancelled) {
|
|
447
544
|
record.responseTimer = setTimeout(() => {
|
|
@@ -453,8 +550,19 @@ class MoqtSessionImpl {
|
|
|
453
550
|
fail(failure);
|
|
454
551
|
}, timeout);
|
|
455
552
|
}
|
|
456
|
-
|
|
457
|
-
onMessage
|
|
553
|
+
const requestStream = openRequestStream(stream, message, {
|
|
554
|
+
onMessage: (msg) => {
|
|
555
|
+
// Frames decoded in one read keep arriving after a frame among them, or its handler, ended the session.
|
|
556
|
+
if (this.#destroyed)
|
|
557
|
+
return;
|
|
558
|
+
const violation = msg.kind === 'request-ok'
|
|
559
|
+
? activeCountViolation(kind, this.#activeCountNegotiated, msg.parameters)
|
|
560
|
+
: undefined;
|
|
561
|
+
if (violation === undefined)
|
|
562
|
+
onMessage(msg);
|
|
563
|
+
else
|
|
564
|
+
this.#fatal(new MoqtProtocolError(violation));
|
|
565
|
+
},
|
|
458
566
|
onFin: () => {
|
|
459
567
|
if (!record.settled) {
|
|
460
568
|
fail(streamFailure('request stream closed before a response'));
|
|
@@ -468,6 +576,8 @@ class MoqtSessionImpl {
|
|
|
468
576
|
this.#fatal(error);
|
|
469
577
|
return;
|
|
470
578
|
}
|
|
579
|
+
// The response direction failed, so the request is over: its sending direction goes with it.
|
|
580
|
+
record.stream?.cancel(error);
|
|
471
581
|
if (record.settled) {
|
|
472
582
|
if (!record.cancelled && !this.#destroyed)
|
|
473
583
|
onSettledError?.(error);
|
|
@@ -475,11 +585,19 @@ class MoqtSessionImpl {
|
|
|
475
585
|
}
|
|
476
586
|
fail(streamFailure(error instanceof Error ? error.message : 'request stream failed'));
|
|
477
587
|
},
|
|
588
|
+
onClosed: () => this.#requestStreams.delete(tracked),
|
|
478
589
|
}, this.#protocol);
|
|
590
|
+
const tracked = new WeakRef(requestStream);
|
|
591
|
+
record.stream = requestStream;
|
|
592
|
+
this.#requestStreams.add(tracked);
|
|
479
593
|
// A cancel that raced the stream opening lands here.
|
|
480
594
|
if (record.cancelled)
|
|
481
595
|
record.stream.cancel(record.pendingCancelReason);
|
|
482
596
|
}
|
|
597
|
+
/** Whether `record`'s handlers may still be called: not after its cancel, and not after the session's `onClosed`. */
|
|
598
|
+
#live(record) {
|
|
599
|
+
return !record.cancelled && !this.#destroyed;
|
|
600
|
+
}
|
|
483
601
|
/** Mark a request answered (or dead) and disarm its response timeout. */
|
|
484
602
|
#settleRequest(record) {
|
|
485
603
|
record.settled = true;
|
|
@@ -573,6 +691,7 @@ class MoqtSessionImpl {
|
|
|
573
691
|
this.#settleRequest(record);
|
|
574
692
|
this.#fetches.delete(record.requestId);
|
|
575
693
|
record.handlers.onError?.(message);
|
|
694
|
+
record.stream?.finWrite().catch(() => { });
|
|
576
695
|
break;
|
|
577
696
|
case 'goaway':
|
|
578
697
|
record.handlers.onGoaway?.(message);
|
|
@@ -582,8 +701,7 @@ class MoqtSessionImpl {
|
|
|
582
701
|
}
|
|
583
702
|
}
|
|
584
703
|
#handleNamespaceMessage(record, message) {
|
|
585
|
-
|
|
586
|
-
if (record.cancelled || this.#destroyed)
|
|
704
|
+
if (record.cancelled)
|
|
587
705
|
return;
|
|
588
706
|
switch (message.kind) {
|
|
589
707
|
case 'request-ok':
|
|
@@ -593,7 +711,10 @@ class MoqtSessionImpl {
|
|
|
593
711
|
}
|
|
594
712
|
this.#settleRequest(record);
|
|
595
713
|
record.accepted = true;
|
|
596
|
-
record.
|
|
714
|
+
record.owed = message.parameters.activeCount ?? 0;
|
|
715
|
+
record.handlers.onOk?.(message.parameters.activeCount === undefined ? {} : { activeCount: message.parameters.activeCount });
|
|
716
|
+
if (message.parameters.activeCount === 0 && this.#live(record))
|
|
717
|
+
record.handlers.onSynced?.();
|
|
597
718
|
break;
|
|
598
719
|
case 'request-error':
|
|
599
720
|
// After REQUEST_OK the response is spent: an error then is outside the stream's grammar.
|
|
@@ -607,32 +728,49 @@ class MoqtSessionImpl {
|
|
|
607
728
|
record.stream?.finWrite().catch(() => { });
|
|
608
729
|
break;
|
|
609
730
|
case 'namespace':
|
|
610
|
-
case 'namespace-done':
|
|
731
|
+
case 'namespace-done': {
|
|
611
732
|
// Entries follow REQUEST_OK (§9.15); one before it, or after REQUEST_ERROR, is outside the
|
|
612
733
|
// stream's grammar.
|
|
613
734
|
if (!record.accepted) {
|
|
614
735
|
this.#fatal(new MoqtProtocolError(`${message.kind} before REQUEST_OK on a subscribe-namespace request stream`));
|
|
615
736
|
return;
|
|
616
737
|
}
|
|
738
|
+
const synced = this.#count(record, message.kind);
|
|
739
|
+
if (synced === undefined)
|
|
740
|
+
return;
|
|
617
741
|
if (message.kind === 'namespace') {
|
|
618
742
|
record.active.set(JSON.stringify(message.trackNamespaceSuffix), message.trackNamespaceSuffix);
|
|
619
743
|
record.handlers.onNamespace?.(message.trackNamespaceSuffix);
|
|
744
|
+
if (synced && this.#live(record))
|
|
745
|
+
record.handlers.onSynced?.();
|
|
620
746
|
}
|
|
621
747
|
else if (record.active.delete(JSON.stringify(message.trackNamespaceSuffix))) {
|
|
622
|
-
record.handlers.onNamespaceDone?.(message.trackNamespaceSuffix);
|
|
748
|
+
record.handlers.onNamespaceDone?.(message.trackNamespaceSuffix, { requestEnded: false });
|
|
623
749
|
}
|
|
624
750
|
else {
|
|
625
751
|
// §9.15: NAMESPACE_DONE before the corresponding NAMESPACE MUST close the session.
|
|
626
752
|
this.#fatal(new MoqtProtocolError('NAMESPACE_DONE before its NAMESPACE on a subscribe-namespace request stream'));
|
|
627
753
|
}
|
|
628
754
|
break;
|
|
755
|
+
}
|
|
629
756
|
case 'goaway':
|
|
757
|
+
if (this.#count(record, 'goaway') === undefined)
|
|
758
|
+
return;
|
|
630
759
|
record.handlers.onGoaway?.(message);
|
|
631
760
|
break;
|
|
632
761
|
default:
|
|
633
762
|
this.#fatal(new MoqtProtocolError(`unexpected ${message.kind} on a subscribe-namespace request stream`));
|
|
634
763
|
}
|
|
635
764
|
}
|
|
765
|
+
#count(record, input) {
|
|
766
|
+
const step = countedStep(record.owed, input);
|
|
767
|
+
if ('violation' in step) {
|
|
768
|
+
this.#fatal(new MoqtProtocolError(step.violation));
|
|
769
|
+
return undefined;
|
|
770
|
+
}
|
|
771
|
+
record.owed = step.owed;
|
|
772
|
+
return step.synced;
|
|
773
|
+
}
|
|
636
774
|
#cancelSubscription(record, reason) {
|
|
637
775
|
if (record.cancelled)
|
|
638
776
|
return;
|
|
@@ -666,6 +804,7 @@ class MoqtSessionImpl {
|
|
|
666
804
|
record.pendingCancelReason = reason;
|
|
667
805
|
this.#settleRequest(record);
|
|
668
806
|
record.stream?.cancel(reason);
|
|
807
|
+
void record.reader?.cancel(reason).catch(() => { });
|
|
669
808
|
this.#fetches.delete(record.requestId);
|
|
670
809
|
}
|
|
671
810
|
// --------------------------------------------------------------------------
|
|
@@ -698,22 +837,32 @@ class MoqtSessionImpl {
|
|
|
698
837
|
await this.#runControlStream(reader);
|
|
699
838
|
return;
|
|
700
839
|
}
|
|
701
|
-
if (
|
|
702
|
-
|
|
703
|
-
await this.#runSubgroupStream(reader, header);
|
|
704
|
-
return;
|
|
705
|
-
}
|
|
706
|
-
if (streamType === STREAM_TYPE.FETCH_HEADER) {
|
|
707
|
-
const { requestId } = await readFetchHeader(reader);
|
|
708
|
-
await this.#runFetchStream(reader, requestId);
|
|
840
|
+
if (this.#destroyed) {
|
|
841
|
+
await reader.cancel();
|
|
709
842
|
return;
|
|
710
843
|
}
|
|
711
|
-
|
|
844
|
+
this.#dataReaders.add(reader);
|
|
845
|
+
try {
|
|
846
|
+
if (isSubgroupHeaderType(streamType)) {
|
|
847
|
+
const header = await readSubgroupHeader(reader, streamType);
|
|
848
|
+
await this.#runSubgroupStream(reader, header);
|
|
849
|
+
return;
|
|
850
|
+
}
|
|
851
|
+
if (streamType === STREAM_TYPE.FETCH_HEADER) {
|
|
852
|
+
const { requestId } = await readFetchHeader(reader);
|
|
853
|
+
await this.#runFetchStream(reader, requestId);
|
|
854
|
+
return;
|
|
855
|
+
}
|
|
856
|
+
if (streamType === STREAM_TYPE.PADDING) {
|
|
857
|
+
await reader.cancel();
|
|
858
|
+
return;
|
|
859
|
+
}
|
|
712
860
|
await reader.cancel();
|
|
713
|
-
|
|
861
|
+
throw new MoqtProtocolError(`unknown unidirectional stream type 0x${streamType.toString(16)}`);
|
|
862
|
+
}
|
|
863
|
+
finally {
|
|
864
|
+
this.#dataReaders.delete(reader);
|
|
714
865
|
}
|
|
715
|
-
await reader.cancel();
|
|
716
|
-
throw new MoqtProtocolError(`unknown unidirectional stream type 0x${streamType.toString(16)}`);
|
|
717
866
|
}
|
|
718
867
|
/** Read one control-message frame using the pull-based stream reader. */
|
|
719
868
|
async #readControlFrame(reader, type) {
|
|
@@ -725,6 +874,9 @@ class MoqtSessionImpl {
|
|
|
725
874
|
async #runControlStream(reader) {
|
|
726
875
|
// The stream-type varint (0x2F00) doubles as the first message's type.
|
|
727
876
|
const setup = await this.#readControlFrame(reader, MESSAGE_TYPE.SETUP);
|
|
877
|
+
// A duplex session's transport outlives its subscriber, so the control stream can still deliver after `onClosed`.
|
|
878
|
+
if (this.#destroyed)
|
|
879
|
+
return;
|
|
728
880
|
if (setup.kind !== 'setup')
|
|
729
881
|
throw new MoqtProtocolError('control stream did not begin with SETUP');
|
|
730
882
|
if (this.#receivedServerSetup) {
|
|
@@ -732,13 +884,18 @@ class MoqtSessionImpl {
|
|
|
732
884
|
}
|
|
733
885
|
this.#receivedServerSetup = true;
|
|
734
886
|
// The option's value is reserved and ignored on receipt: its presence is the declaration.
|
|
735
|
-
this.#maxAgeSupported = setup.options
|
|
887
|
+
this.#maxAgeSupported = declares(setup.options, SETUP_OPTION.MAX_AGE);
|
|
888
|
+
this.#activeCountNegotiated =
|
|
889
|
+
declares(this.#config.setupOptions ?? [], SETUP_OPTION.ACTIVE_COUNT) &&
|
|
890
|
+
declares(setup.options, SETUP_OPTION.ACTIVE_COUNT);
|
|
736
891
|
this.#resolveReady();
|
|
737
892
|
this.#callbacks.onReady?.(setup.options);
|
|
738
893
|
// Subsequent control-stream messages: GOAWAY (at most once).
|
|
739
894
|
while (!(await reader.atEnd())) {
|
|
740
895
|
const type = await reader.readVarint();
|
|
741
896
|
const message = await this.#readControlFrame(reader, type);
|
|
897
|
+
if (this.#destroyed)
|
|
898
|
+
return;
|
|
742
899
|
if (message.kind !== 'goaway')
|
|
743
900
|
throw new MoqtProtocolError(`unexpected ${message.kind} on the control stream`);
|
|
744
901
|
if (this.#receivedControlGoaway) {
|
|
@@ -822,28 +979,36 @@ class MoqtSessionImpl {
|
|
|
822
979
|
await reader.cancel();
|
|
823
980
|
return;
|
|
824
981
|
}
|
|
982
|
+
record.reader = reader;
|
|
825
983
|
try {
|
|
826
984
|
for await (const entry of readFetchEntries(reader, record.groupOrder)) {
|
|
827
|
-
if (record.cancelled) {
|
|
985
|
+
if (record.cancelled || this.#destroyed) {
|
|
828
986
|
await reader.cancel();
|
|
829
987
|
return;
|
|
830
988
|
}
|
|
831
989
|
record.handlers.onEntry?.(entry);
|
|
832
990
|
}
|
|
991
|
+
// Teardown's or the fetch's own cancel of this reader reads as a clean end.
|
|
992
|
+
if (this.#destroyed || record.cancelled)
|
|
993
|
+
return;
|
|
833
994
|
record.handlers.onEnd?.();
|
|
834
995
|
}
|
|
835
996
|
catch (error) {
|
|
997
|
+
if (record.cancelled)
|
|
998
|
+
return;
|
|
836
999
|
if (isMoqtProtocolError(error))
|
|
837
1000
|
throw error;
|
|
838
1001
|
// A reset mid-replay means entries may be missing — never report it
|
|
839
1002
|
// as the clean FIN `onEnd` promises.
|
|
840
|
-
|
|
1003
|
+
if (!this.#destroyed)
|
|
1004
|
+
record.handlers.onReset?.({ error });
|
|
841
1005
|
}
|
|
842
1006
|
finally {
|
|
843
1007
|
// Nothing more arrives for the fetch once its data stream and its response have, so the session forgets it
|
|
844
1008
|
// then (#419): kept, the record's handlers held everything they had collected for the session's life. Data can
|
|
845
1009
|
// beat the response; until the response, or the request timeout, the record stays for teardown to fail.
|
|
846
1010
|
record.dataEnded = true;
|
|
1011
|
+
record.reader = undefined;
|
|
847
1012
|
if (record.settled)
|
|
848
1013
|
this.#fetches.delete(requestId);
|
|
849
1014
|
}
|
|
@@ -873,8 +1038,14 @@ class MoqtSessionImpl {
|
|
|
873
1038
|
}
|
|
874
1039
|
async #handleIncomingRequest(stream) {
|
|
875
1040
|
const reader = new StreamReader(stream.readable);
|
|
876
|
-
const
|
|
877
|
-
|
|
1041
|
+
const message = await readRequest(stream, reader, (type) => this.#readControlFrame(reader, type));
|
|
1042
|
+
// A request still arriving when the session ended is dropped: nothing reaches a callback after `onClosed`. (A duplex
|
|
1043
|
+
// session's transport outlives its subscriber, for the publisher's drain.)
|
|
1044
|
+
if (this.#destroyed) {
|
|
1045
|
+
await reader.cancel().catch(() => { });
|
|
1046
|
+
void stream.writable.abort().catch(() => { });
|
|
1047
|
+
return;
|
|
1048
|
+
}
|
|
878
1049
|
const initiatingRequestId = message.kind === 'fetch'
|
|
879
1050
|
? message.request.requestId
|
|
880
1051
|
: message.kind === 'subscribe' ||
|
|
@@ -887,34 +1058,35 @@ class MoqtSessionImpl {
|
|
|
887
1058
|
// Before answering: a request this client rejects still consumes its id.
|
|
888
1059
|
if (initiatingRequestId !== undefined)
|
|
889
1060
|
this.#claimPeerRequestId(initiatingRequestId);
|
|
1061
|
+
const writer = stream.writable.getWriter();
|
|
1062
|
+
let responded = false;
|
|
1063
|
+
// Until its response has been written, the request is teardown's to drop: nothing is answered after `onClosed`.
|
|
1064
|
+
const drop = () => {
|
|
1065
|
+
responded = true;
|
|
1066
|
+
void reader.cancel().catch(() => { });
|
|
1067
|
+
void writer.abort().catch(() => { });
|
|
1068
|
+
};
|
|
890
1069
|
const respond = async (bytes) => {
|
|
891
|
-
|
|
1070
|
+
if (responded)
|
|
1071
|
+
return;
|
|
1072
|
+
responded = true;
|
|
892
1073
|
try {
|
|
893
1074
|
await writer.write(bytes);
|
|
894
1075
|
await writer.close();
|
|
895
1076
|
}
|
|
896
1077
|
catch {
|
|
897
|
-
// Peer cancelled
|
|
1078
|
+
// Peer cancelled, or teardown dropped the request.
|
|
898
1079
|
}
|
|
899
|
-
|
|
1080
|
+
this.#incomingRequests.delete(drop);
|
|
1081
|
+
await reader.cancel().catch(() => { });
|
|
900
1082
|
};
|
|
1083
|
+
this.#incomingRequests.add(drop);
|
|
901
1084
|
if (message.kind === 'publish') {
|
|
902
1085
|
const callbacks = this.#callbacks;
|
|
903
1086
|
if (callbacks.onIncomingPublish) {
|
|
904
|
-
let responded = false;
|
|
905
1087
|
callbacks.onIncomingPublish(message, {
|
|
906
|
-
accept: () => {
|
|
907
|
-
|
|
908
|
-
return;
|
|
909
|
-
responded = true;
|
|
910
|
-
void respond(encodeRequestOk({}, this.#protocol));
|
|
911
|
-
},
|
|
912
|
-
reject: (errorCode = REQUEST_ERROR_CODE.UNINTERESTED, reason = '') => {
|
|
913
|
-
if (responded)
|
|
914
|
-
return;
|
|
915
|
-
responded = true;
|
|
916
|
-
void respond(encodeRequestError(errorCode, reason));
|
|
917
|
-
},
|
|
1088
|
+
accept: () => void respond(encodeRequestOk({}, this.#protocol)),
|
|
1089
|
+
reject: (errorCode = REQUEST_ERROR_CODE.UNINTERESTED, reason = '') => void respond(encodeRequestError(errorCode, reason)),
|
|
918
1090
|
});
|
|
919
1091
|
return;
|
|
920
1092
|
}
|