diff --git a/doc/concept/standard.md b/doc/concept/standard.md index 55d606a8432..83844db2e67 100644 --- a/doc/concept/standard.md +++ b/doc/concept/standard.md @@ -26,11 +26,11 @@ maps everything else to "not supported" or a harmless equivalent. The subset drops. What a peer actually observes against this implementation: - **Pull.** Subscribers ask. Single-track `PUBLISH` offers are declined; announce a namespace and serve the resulting subscriptions. Announcements go out unsolicited, and we also ask for every prefix we may discover. The solicit `SETUP` option makes us wait to be asked. Without it, a draft 16+ peer also gets each match as a `NAMESPACE` on its `SUBSCRIBE_NAMESPACE` stream, so it hears the namespace twice. We never send `PUBLISH`: on drafts 16 and 17 a `SUBSCRIBE_NAMESPACE` asking only for `PUBLISH` is refused, one asking for both gets only `NAMESPACE`, and any other Subscribe Options value closes the session. -- **History is one group.** A `FETCH` returns one group from the cache, or on drafts 14 through 19 the saved prefix of the group a new subscription just joined. A range of groups is refused, as is a draft 20+ `LOCATION_FILTER` that ends at Largest Object rather than an absolute End Location. On draft 20 and later, `FETCH_OK`'s End Location is the requested end, capped at Largest Object: a range holding no objects gets an empty fetch stream, and only a start past Largest Object is refused, with `INVALID_RANGE`. A relay's copy with no upstream subscription can't know Largest Object short of the track's end, so it echoes the requested end instead. JavaScript publishing refuses every `FETCH`. Datagrams are never fetchable. A Rust reader that only fetches learns the track from `TRACK_STATUS` instead of subscribing, so a finished track stays fetchable; draft 17 still subscribes, since its `TRACK_STATUS` answer cannot say whether the track is timed, and so does a reader whose publisher refuses `TRACK_STATUS` as `NOT_SUPPORTED`, as JavaScript does. +- **History is one group.** A `FETCH` returns one group from the cache, or on drafts 14 through 19 the saved prefix of the group a new subscription just joined. A range of groups is refused, as is a draft 20+ `LOCATION_FILTER` that ends at Largest Object rather than an absolute End Location. On draft 20 and later, `FETCH_OK`'s End Location is the requested end, capped at Largest Object: a range holding no objects gets an empty fetch stream, and only a start past Largest Object is refused, with `INVALID_RANGE`. A relay's copy with no upstream subscription can't know Largest Object short of the track's end, so it echoes the requested end instead. JavaScript publishing refuses every `FETCH`. Datagrams are never fetchable. A Rust reader that only fetches learns the track from `TRACK_STATUS` instead of subscribing, so a finished track stays fetchable; draft 17 still subscribes, since its `TRACK_STATUS` answer cannot say whether the track is timed, and so does a reader whose publisher refuses `TRACK_STATUS` as `NOT_SUPPORTED`. - **Timing.** A track whose `SUBSCRIBE_OK` declares no `TIMESCALE` is untimed, as is every track on drafts 14 through 16. A Rust reader that never subscribed takes the units from `TRACK_STATUS` on draft 18 and later. - **Strict SETUP and parameters.** Repeated unknown `SETUP` options, GREASE included, are accepted if well-formed; a repeated known option is rejected. On draft 17 and later, a `GROUP_ORDER` other than Ascending (1) or Descending (2) closes the session. Message parameters follow the negotiated draft's lists: drafts 14 and 15 ignore an unknown or misplaced parameter, draft 16 ignores one defined only for another message but closes the session on an unknown one, and draft 17 and later close it on both. - **One `SETUP` credential**, forwarded to the [auth server](/bin/relay/auth#the-contract) unverified. A second `SETUP` token closes the session. We advertise no token cache, so an alias reference in `SETUP` closes it with `PROTOCOL_VIOLATION`, one on a request with `UNKNOWN_AUTH_TOKEN_ALIAS`, and a registration on a request with `AUTH_TOKEN_CACHE_OVERFLOW`. A token attached to an individual request is otherwise decoded and grants nothing, and a request repeating one is refused with `MALFORMED_AUTH_TOKEN`. `SUBSCRIBE_TRACKS` is refused unread, so its token is not decoded. On draft 17 and later, the negotiated [MoQ Auth extension](/concept/moq-lite#authorization) accepts additional session tokens in band. -- **Refused, not fatal.** A legal request this stack does not serve is rejected on its own and the session stays up: `FORWARD=0`, range filters, `TRACK_STATUS` to a JavaScript publisher, `SUBSCRIBE_TRACKS`, and the fetch forms above. On draft 19 and later, and in Rust on drafts 14 through 16, a subscription update may change only priority; any other update ends that subscription. +- **Refused, not fatal.** A legal request this stack does not serve is rejected on its own and the session stays up: `FORWARD=0`, range filters, `SUBSCRIBE_TRACKS`, and the fetch forms above. On draft 19 and later, and in Rust on drafts 14 through 16, a subscription update may change only priority; any other update ends that subscription. - **Datagrams** are a single normal object at object 0, forwarded without renumbering. Anything else is dropped, and a malformed one closes the session. Rust and JavaScript both carry them on every draft. - **Priority.** Higher is served first. The IETF default of 128 is this stack's 127, and a track that never sets one is 127. - **Size.** An object extension block larger than 64 KiB ends that subgroup stream. The session stays up. This cap is ours, not the draft's. diff --git a/js/net/src/broadcast.ts b/js/net/src/broadcast.ts index 34acea2ae12..c83f7459b84 100644 --- a/js/net/src/broadcast.ts +++ b/js/net/src/broadcast.ts @@ -247,6 +247,12 @@ async function resolveTrackInfo(state: BroadcastState, name: string, hold?: Abor return untilAborted(info, hold); } +// The newest frame cached for `name`, without subscribing: undefined when nobody serves it yet. +function largest(state: BroadcastState, name: string): track.Location | undefined { + const producer = lookup(state, name); + return producer && hooks.cachedLargest(producer); +} + // Serve a group from the local retained window by subscribing and scanning to the // requested sequence. The default for a produced broadcast; the consuming wire layer // overrides it to fetch over the network (or to reject when the transport has no FETCH). @@ -350,6 +356,7 @@ export class Producer { registerWire(this, { subscribe: (name, options) => subscribe(this.#state, name, options), resolveTrackInfo: (name, hold) => resolveTrackInfo(this.#state, name, hold), + largest: (name) => largest(this.#state, name), fetchGroup: (name, sequence, options) => fetchGroup(this.#state, name, sequence, options), requested: () => requested(this.#state), }); @@ -519,6 +526,7 @@ export class Consumer { registerWire(this, { subscribe: (name, options) => subscribe(this.#state, name, options), resolveTrackInfo: (name, hold) => resolveTrackInfo(this.#state, name, hold), + largest: (name) => largest(this.#state, name), fetchGroup: (name, sequence, options) => fetchGroup(this.#state, name, sequence, options), requested: () => requested(this.#state), }); diff --git a/js/net/src/ietf/connection.ts b/js/net/src/ietf/connection.ts index e7f1f81599a..0fb840dffbe 100644 --- a/js/net/src/ietf/connection.ts +++ b/js/net/src/ietf/connection.ts @@ -34,7 +34,7 @@ import { } from "./subscribe_namespace.ts"; import { Subscriber } from "./subscriber.ts"; import { MalformedAuthToken } from "./token.ts"; -import { TrackStatusRequest } from "./track.ts"; +import { TRACK_STATUS_ERROR_14, TrackStatusRequest } from "./track.ts"; import { type IetfVersion, Version, versionName } from "./version.ts"; // The PADDING stream type (draft-18+): bytes a peer sends to probe for bandwidth. @@ -519,8 +519,7 @@ export class Connection implements Established { publish: PublishError.id, publish_namespace: PublishNamespaceError.id, subscribe_namespace: SubscribeNamespaceError.id, - // TRACK_STATUS_ERROR - track_status: 0x0f, + track_status: TRACK_STATUS_ERROR_14, }[kind]; const legacy = version === Version.DRAFT_14 || version === Version.DRAFT_15 || version === Version.DRAFT_16; await stream.writer.u53(version === Version.DRAFT_14 ? id14 : RequestError.id); diff --git a/js/net/src/ietf/ietf.test.ts b/js/net/src/ietf/ietf.test.ts index 01fe7791066..32a625d4511 100644 --- a/js/net/src/ietf/ietf.test.ts +++ b/js/net/src/ietf/ietf.test.ts @@ -504,23 +504,31 @@ test("TrackStatusRequest: round trip", async () => { expect(decoded.trackName).toBe("main"); }); -test("TrackStatus v14: round trip", async () => { - const msg = new Track.TrackStatus({ - trackNamespace: Path.from("test"), - trackName: "status", - statusCode: 200, - lastGroupId: 42n, - lastObjectId: 100n, - }); - - const encoded = await encodeVersioned(msg, Version.DRAFT_14); - const decoded = await decodeVersioned(encoded, Track.TrackStatus.decode, Version.DRAFT_14); - - expect(decoded.trackNamespace).toBe("test" as Path.Valid); - expect(decoded.trackName).toBe("status"); - expect(decoded.statusCode).toBe(200); - expect(decoded.lastGroupId).toBe(42n); - expect(decoded.lastObjectId).toBe(100n); +// TRACK_STATUS_OK carries what SUBSCRIBE_OK would, as far as each draft's answer has room. +test("TrackStatusOk: round trip on every draft", async () => { + const properties = { timescale: Timescale.MILLI, priority: 7, groupOrder: 0x02, maxCacheDuration: 1000n }; + for (const version of Object.values(Version).filter((v): v is IetfVersion => v !== Version.DRAFT_07)) { + const legacy = version === Version.DRAFT_14 || version === Version.DRAFT_15 || version === Version.DRAFT_16; + const msg = new Track.TrackStatusOk({ + requestId: legacy ? 3n : undefined, + largest: { groupId: 4n, objectId: 1n }, + properties, + }); + const encoded = await encodeVersioned(msg, version); + const decoded = await decodeVersioned(encoded, Track.TrackStatusOk.decode, version); + + expect(decoded.requestId).toBe(legacy ? 3n : undefined); + expect(decoded.largest).toEqual({ groupId: 4n, objectId: 1n }); + // GROUP_ORDER is a field on draft-14 and a parameter on draft-15; REQUEST_OK has no + // properties block until draft-18. + const expected = + version === Version.DRAFT_14 || version === Version.DRAFT_15 + ? { groupOrder: 0x02 } + : version >= Version.DRAFT_18 + ? properties + : {}; + expect(decoded.properties).toEqual(expected); + } }); // Validation tests @@ -2058,6 +2066,7 @@ test("TrackStatusRequest: carries SUBSCRIBE fields", async () => { for (const [version, rest] of cases) { const msg = await decodeVersioned(framed([...TRACK_HEAD, ...rest]), Track.TrackStatusRequest.decode, version); expect(msg.trackName).toBe("video"); + expect(msg.propertiesWanted).toBe(version !== Version.DRAFT_20); } }); diff --git a/js/net/src/ietf/publisher.test.ts b/js/net/src/ietf/publisher.test.ts index b2c9c9fb284..221b92ea147 100644 --- a/js/net/src/ietf/publisher.test.ts +++ b/js/net/src/ietf/publisher.test.ts @@ -1,7 +1,7 @@ import { expect, jest, mock, spyOn, test } from "bun:test"; import { Signal } from "@moq/signals"; import type { Grant } from "../auth.ts"; -import type { Producer as BroadcastProducer } from "../broadcast.ts"; +import { Producer as BroadcastProducer } from "../broadcast.ts"; import * as Epoch from "../epoch.ts"; import { error } from "../error.ts"; import { Producer as GroupProducer, MAX_GROUP_FRAMES } from "../group.ts"; @@ -18,13 +18,14 @@ import type * as Cluster from "./cluster.ts"; import { toRequestCode } from "./error.ts"; import { Fetch, FetchHeader } from "./fetch.ts"; import { Frame, Group as GroupMessage } from "./object.ts"; +import { toWire } from "./priority.ts"; import { PublishDone } from "./publish.ts"; import { PublishNamespace, PublishNamespaceUpdate } from "./publish_namespace.ts"; import { Publisher } from "./publisher.ts"; import { RequestError, RequestOk } from "./request.ts"; -import { GroupOrder, Subscribe, SubscribeOk, SubscribeUpdate, Unsubscribe } from "./subscribe.ts"; +import { GroupOrder, Subscribe, SubscribeError, SubscribeOk, SubscribeUpdate, Unsubscribe } from "./subscribe.ts"; import { SubscribeNamespace, SubscribeNamespaceEntry, SubscribeNamespaceEntryDone } from "./subscribe_namespace.ts"; -import { TrackStatusRequest } from "./track.ts"; +import { TRACK_STATUS_ERROR_14, TrackStatusOk, TrackStatusRequest } from "./track.ts"; import { ALPN, type IetfVersion, Version } from "./version.ts"; function publish(origin: OriginProducer, path: Path.Valid) { @@ -140,46 +141,217 @@ function publisher( }; } -test("TRACK_STATUS gets exact NOT_SUPPORTED refusal bytes on every draft", async () => { - const phrase = new TextEncoder().encode("TRACK_STATUS is not supported"); - for (const version of [ - Version.DRAFT_14, - Version.DRAFT_15, - Version.DRAFT_16, - Version.DRAFT_17, - Version.DRAFT_18, - Version.DRAFT_19, - Version.DRAFT_20, - Version.DRAFT_21, - Version.DRAFT_22, - ] as const) { - const pair = createMockTransportPair(ALPN.DRAFT_19); +const IETF_VERSIONS = [ + Version.DRAFT_14, + Version.DRAFT_15, + Version.DRAFT_16, + Version.DRAFT_17, + Version.DRAFT_18, + Version.DRAFT_19, + Version.DRAFT_20, + Version.DRAFT_21, + Version.DRAFT_22, +] as const; + +/** A request stream recording what the publisher writes, with the requester's side under test control. */ +function requestStream(version: IetfVersion) { + let input!: ReadableStreamDefaultController; + const written: number[] = []; + const stream = new Stream({ + readable: new ReadableStream({ + start(c) { + input = c; + }, + }), + writable: new WritableStream({ + write: (chunk) => { + written.push(...chunk); + }, + }), + version, + }); + return { + stream, + written, + wire: () => new Reader(undefined, Uint8Array.from(written), version), + cancel: () => input.error(new Error("request cancelled")), + }; +} + +function trackStatus(namespace: string, propertiesWanted = true) { + return new TrackStatusRequest({ + requestId: 7n, + trackNamespace: Path.from(namespace), + trackName: "video", + propertiesWanted, + }); +} + +/** The error code of the refusal on `wire`. */ +async function refusalCode(wire: Reader, version: IetfVersion): Promise { + if (version === Version.DRAFT_14) { + expect(await wire.u53()).toBe(TRACK_STATUS_ERROR_14); + return (await SubscribeError.decode(wire, version)).errorCode; + } + expect(await wire.u53()).toBe(RequestError.id); + return (await RequestError.decode(wire, version)).errorCode; +} + +// TRACK_STATUS gets what a SUBSCRIBE_OK would carry, as far as each draft's answer has room, +// without subscribing to the track. INCLUDE_PROPERTIES=0 empties the properties. +test("TRACK_STATUS is answered on every draft without subscribing", async () => { + for (const version of IETF_VERSIONS) { + for (const wanted of [true, false]) { + const pair = createMockTransportPair(ALPNS[version]); + const session = new NativeSession(pair.server, version, true); + const { pub, origin } = publisher(pair.server, { session }); + const broadcast = publish(origin, Path.from("room")); + const track = broadcast.createTrack("video", { + timescale: Timescale.MILLI, + maxAge: Milli(5000), + priority: 2, + }); + const group = new GroupProducer(4); + group.writeFrame({ payload: new Uint8Array([1]), timestamp: Timestamp.fromMillis(0) }); + group.writeFrame({ payload: new Uint8Array([2]), timestamp: Timestamp.fromMillis(1) }); + track.writeGroup(group); + const subscribed = spyOn(track, "subscribe"); + + const { stream, wire } = requestStream(version); + // Only draft-20 can carry the opt-out. + await pub.runTrackStatusRequest(trackStatus("room", wanted || version < Version.DRAFT_20), stream); + expect(subscribed).not.toHaveBeenCalled(); + + const r = wire(); + expect(await r.u53()).toBe(TrackStatusOk.id(version)); + const ok = await TrackStatusOk.decode(r, version); + expect(await r.done()).toBe(true); + + const legacy = version === Version.DRAFT_14 || version === Version.DRAFT_15 || version === Version.DRAFT_16; + expect(ok.requestId).toBe(legacy ? 7n : undefined); + expect(ok.largest).toEqual({ groupId: 4n, objectId: 1n }); + const expected = + version === Version.DRAFT_14 || version === Version.DRAFT_15 + ? { groupOrder: GroupOrder.Descending } + : version < Version.DRAFT_18 || (!wanted && version >= Version.DRAFT_20) + ? {} + : { + timescale: Timescale.MILLI, + priority: toWire(2), + groupOrder: GroupOrder.Descending, + maxCacheDuration: 5000n, + }; + expect(ok.properties).toEqual(expected); + origin.close(); + } + } +}); + +// A track that has published nothing answers without LARGEST_OBJECT. +test("TRACK_STATUS for an empty track names no largest", async () => { + const version = Version.DRAFT_22; + const pair = createMockTransportPair(ALPNS[version]); + const session = new NativeSession(pair.server, version, true); + const { pub, origin } = publisher(pair.server, { session }); + publish(origin, Path.from("room")).createTrack("video"); + + const { stream, wire } = requestStream(version); + await pub.runTrackStatusRequest(trackStatus("room"), stream); + const r = wire(); + expect(await r.u53()).toBe(TrackStatusOk.id(version)); + expect((await TrackStatusOk.decode(r, version)).largest).toBeUndefined(); + origin.close(); +}); + +// Refused before anything resolves when the grant excludes the broadcast, and refused when +// the grant narrows while the broadcast resolves. +test("TRACK_STATUS outside the grant is refused on every draft", async () => { + const nothing = (): Grant => ({ publish: new Path.Patterns([]), subscribe: new Path.Patterns([]) }); + for (const version of IETF_VERSIONS) { + const unauthorized = toRequestCode("unauthorized", "track_status", version); + const pair = createMockTransportPair(ALPNS[version]); + const session = new NativeSession(pair.server, version, true); + const grant = new Signal(nothing()); + const { pub, origin } = publisher(pair.server, { session, grant }); + const requests = origin.dynamic(Path.from("room")).requested(); + + const outside = requestStream(version); + await pub.runTrackStatusRequest(trackStatus("room"), outside.stream); + expect(await refusalCode(outside.wire(), version)).toBe(unauthorized); + + // Granted, so it reaches the handler, which has not answered when the grant narrows. + grant.set(undefined); + const narrowed = requestStream(version); + const task = pub.runTrackStatusRequest(trackStatus("room"), narrowed.stream); + expect((await requests.next()).value?.path).toBe(Path.from("room")); + grant.set(nothing()); + await task; + expect(await refusalCode(narrowed.wire(), version)).toBe(unauthorized); + + await requests.return?.(); + origin.close(); + } +}); + +test("TRACK_STATUS for a missing broadcast or track is refused DOES_NOT_EXIST", async () => { + for (const version of IETF_VERSIONS) { + const missing = toRequestCode("does_not_exist", "track_status", version); + const pair = createMockTransportPair(ALPNS[version]); const session = new NativeSession(pair.server, version, true); const { pub, origin } = publisher(pair.server, { session }); - const written: Uint8Array[] = []; - const stream = new Stream({ - readable: new ReadableStream(), - writable: new WritableStream({ - write: (chunk) => { - written.push(new Uint8Array(chunk)); - }, - }), - version, - }); - await pub.runTrackStatusRequest( - new TrackStatusRequest({ requestId: 7n, trackNamespace: Path.from("test"), trackName: "video" }), - stream, - ); - await stream.writer.closed; - const body = [ - ...(version <= Version.DRAFT_16 ? [7] : []), - 3, - ...(version >= Version.DRAFT_16 ? [0] : []), - phrase.length, - ...phrase, - ]; - const expected = [version === Version.DRAFT_14 ? 0x0f : 0x05, 0, body.length, ...body]; - expect(written.flatMap((chunk) => Array.from(chunk))).toEqual(expected); + + const absent = requestStream(version); + await pub.runTrackStatusRequest(trackStatus("absent"), absent.stream); + expect(await refusalCode(absent.wire(), version)).toBe(missing); + + // A produced broadcast serves only the tracks it created. + publish(origin, Path.from("room")); + const untracked = requestStream(version); + await pub.runTrackStatusRequest(trackStatus("room"), untracked.stream); + expect(await refusalCode(untracked.wire(), version)).toBe(missing); + origin.close(); + } +}); + +// The requester leaving is owed nothing, and gives back the origin demand or the track +// lookup it held, whichever was still resolving. +test("a cancelled TRACK_STATUS releases its demand on every draft", async () => { + for (const version of IETF_VERSIONS) { + const pair = createMockTransportPair(ALPNS[version]); + const session = new NativeSession(pair.server, version, true); + const { pub, origin } = publisher(pair.server, { session }); + const requests = origin.dynamic(Path.from("room")).requested(); + + // The broadcast is still resolving: its handler never answers. + const early = requestStream(version); + const resolving = pub.runTrackStatusRequest(trackStatus("room/early"), early.stream); + const pending = (await requests.next()).value; + expect(pending?.path).toBe(Path.from("room/early")); + early.cancel(); + await resolving; + expect(early.written).toEqual([]); + // Withdrawn, so a late answer is closed rather than served. + const answer = new BroadcastProducer().consume(); + pending?.accept(answer); + expect(answer.closed.peek()).toBeNull(); + + // The broadcast resolves, but its track is never answered. + const late = requestStream(version); + const looking = pub.runTrackStatusRequest(trackStatus("room/late"), late.stream); + const request = (await requests.next()).value; + if (!request) throw new Error("no request for room/late"); + const served = new BroadcastProducer(); + const trackRequest = wireOf(served).requested(); + request.accept(served); + expect((await trackRequest)?.name).toBe("video"); + expect(served.demand().used.peek()).toBe(true); + late.cancel(); + await looking; + expect(late.written).toEqual([]); + await served.demand().unused(); + + await requests.return?.(); + served.close(); origin.close(); } }); diff --git a/js/net/src/ietf/publisher.ts b/js/net/src/ietf/publisher.ts index 40e8e6b875f..cb0dac26865 100644 --- a/js/net/src/ietf/publisher.ts +++ b/js/net/src/ietf/publisher.ts @@ -4,7 +4,16 @@ import { enforceGrant } from "../auth_session.ts"; import type * as broadcast from "../broadcast.ts"; import { Withdrawal } from "../connection/withdrawal.ts"; import * as DatagramStream from "../datagram_stream.ts"; -import { controlTimeout, error, ProtocolViolation, reason, StreamCode, StreamError } from "../error.ts"; +import { + controlTimeout, + error, + NotFound, + ProtocolViolation, + reason, + StreamCode, + StreamError, + unauthorized, +} from "../error.ts"; import type * as group from "../group.ts"; import type { Hop, Route } from "../hop.ts"; import { hiddenBelow, hooks, presented } from "../internal.ts"; @@ -48,7 +57,7 @@ import { SubscribeNamespaceOk, } from "./subscribe_namespace.ts"; import { MalformedAuthToken } from "./token.ts"; -import type { TrackStatusRequest } from "./track.ts"; +import { TRACK_STATUS_ERROR_14, TrackStatusOk, type TrackStatusRequest } from "./track.ts"; import { type IetfVersion, Version } from "./version.ts"; /** First wait before re-offering a namespace the peer refused or we couldn't open for. */ @@ -358,7 +367,7 @@ export class Publisher { const version = this.#session.version; const name = msg.trackNamespace; let broadcast: broadcast.Consumer | undefined; - let refusal: { errorCode: number; reasonPhrase: string } | undefined; + let refusal: Refusal | undefined; // The requester leaving while the broadcast and track resolve ends the request, so a // cancelled subscription gives back its slot instead of holding it until the track @@ -384,50 +393,19 @@ export class Publisher { refusal = { errorCode: toRequestCode("unauthorized", "subscribe", version), reasonPhrase: "not granted" }; } else { try { - broadcast = - this.#publish && - (wireOf(this.#publish).local(name) ?? - (await wireOf(this.#publish).demand(name, undefined, left.signal))); - if (!broadcast) { - refusal = { - errorCode: toRequestCode("does_not_exist", "subscribe", version), - reasonPhrase: "broadcast not found", - }; - } + broadcast = await this.#resolve(name, left.signal); } catch (err: unknown) { // Nobody is left to refuse. if (left.signal.aborted) { stream.abort(cancel); return; } - const e = error(err); - const condition = - e instanceof StreamError && (e.code === StreamCode.NotFound || e.code === StreamCode.Unroutable) - ? "does_not_exist" - : "internal"; - refusal = { errorCode: toRequestCode(condition, "subscribe", version), reasonPhrase: reason(e) }; + refusal = this.#failed(err, "subscribe"); } } if (refusal) { - if (version === Version.DRAFT_14) { - await stream.writer.u53(SubscribeError.id); - const err = new SubscribeError({ - requestId: msg.requestId, - errorCode: refusal.errorCode, - reasonPhrase: refusal.reasonPhrase, - }); - await err.encode(stream.writer, version); - } else { - await stream.writer.u53(RequestError.id); - const err = new RequestError({ - requestId: version === Version.DRAFT_15 || version === Version.DRAFT_16 ? msg.requestId : undefined, - errorCode: refusal.errorCode, - reasonPhrase: refusal.reasonPhrase, - }); - await err.encode(stream.writer, version); - } - stream.close(); + await this.#refuse(stream, msg.requestId, SubscribeError.id, refusal); return; } if (!broadcast) return; @@ -1630,13 +1608,85 @@ export class Publisher { } /** - * Handles an incoming TRACK_STATUS_REQUEST on a bidi stream. + * Answers an incoming TRACK_STATUS with what a SUBSCRIBE_OK for the track would carry, + * without subscribing. + * + * The track resolves as it would for a SUBSCRIBE, dynamic handlers included, and the + * answer waits on its info. The Largest Location is whatever the cache holds now. * * @internal */ async runTrackStatusRequest(msg: TrackStatusRequest, stream: Stream) { - // TRACK_STATUS_ERROR is 0x0f on draft-14. - await this.#refuseUnsupported(stream, msg.requestId, "track_status", 0x0f, "TRACK_STATUS is not supported"); + const version = this.#session.version; + const name = msg.trackNamespace; + const denied: Refusal = { + errorCode: toRequestCode("unauthorized", "track_status", version), + reasonPhrase: "not granted", + }; + + // Answer only for what our grant lets us publish. Checked before the track resolves, + // so a denied request never reaches the origin. + if (this.#denied(name)) { + await this.#refuse(stream, msg.requestId, TRACK_STATUS_ERROR_14, denied); + return; + } + + // Holds the track wanted until the answer is out. The requester leaving is owed + // nothing, and lets go of the broadcast and track lookups. + const hold = new AbortController(); + const cancel = new StreamError(StreamCode.Cancel, { message: "cancel" }); + void cancelled(stream, version).then(() => hold.abort(cancel)); + + // A grant that narrows abandons the lookup, and resets an answer the transport has + // not taken yet rather than delivering it. + let answering = false; + const disposeGrant = this.#grant.subscribe(() => { + if (!this.#denied(name)) return; + hold.abort(); + if (answering) stream.abort(unauthorized(name)); + }); + + try { + let answer: TrackStatusOk; + try { + const broadcast = await this.#resolve(name, hold.signal); + const info = await wireOf(broadcast).resolveTrackInfo(msg.trackName, hold.signal); + const largest = wireOf(broadcast).largest(msg.trackName); + answer = new TrackStatusOk({ + requestId: + version === Version.DRAFT_14 || version === Version.DRAFT_15 || version === Version.DRAFT_16 + ? msg.requestId + : undefined, + largest: largest && { groupId: BigInt(largest.group), objectId: BigInt(largest.frame) }, + properties: msg.propertiesWanted + ? { + timescale: info.timescale ?? undefined, + priority: toWire(info.priority), + groupOrder: Properties.DESCENDING, + maxCacheDuration: info.maxAge === undefined ? undefined : BigInt(info.maxAge), + } + : {}, + }); + } catch (err: unknown) { + if (hold.signal.reason === cancel) { + stream.abort(cancel); + return; + } + const refusal = this.#denied(name) ? denied : this.#failed(err, "track_status"); + await this.#refuse(stream, msg.requestId, TRACK_STATUS_ERROR_14, refusal); + return; + } + + answering = true; + await stream.writer.u53(TrackStatusOk.id(version)); + await answer.encode(stream.writer, version); + // The answer is all this stream carries. + stream.close(); + await stream.writer.closed.catch(() => undefined); + } finally { + disposeGrant(); + hold.abort(); + } } /** @@ -1679,10 +1729,7 @@ export class Publisher { await this.#refuseUnsupported(stream, msg.requestId, "fetch", FetchError.id, "FETCH is not supported"); } - /** - * Refuse a request NOT_SUPPORTED. Draft-14 gives each request its own error message, - * `errorId14`, with the SUBSCRIBE_ERROR body; later drafts use REQUEST_ERROR. - */ + /** Refuse a request NOT_SUPPORTED. */ async #refuseUnsupported( stream: Stream, requestId: bigint, @@ -1690,8 +1737,16 @@ export class Publisher { errorId14: number, reasonPhrase: string, ) { + const errorCode = toRequestCode("not_supported", kind, this.#session.version); + await this.#refuse(stream, requestId, errorId14, { errorCode, reasonPhrase }); + } + + /** + * Refuse a request and end its stream. Draft-14 gives each request its own error + * message, `errorId14`, with the SUBSCRIBE_ERROR body; later drafts use REQUEST_ERROR. + */ + async #refuse(stream: Stream, requestId: bigint, errorId14: number, { errorCode, reasonPhrase }: Refusal) { const version = this.#session.version; - const errorCode = toRequestCode("not_supported", kind, version); if (version === Version.DRAFT_14) { await stream.writer.u53(errorId14); await new SubscribeError({ requestId, errorCode, reasonPhrase }).encode(stream.writer, version); @@ -1705,6 +1760,34 @@ export class Publisher { } stream.close(); } + + /** What a request that failed to resolve is refused with. */ + #failed(err: unknown, kind: RequestKind): Refusal { + const e = error(err); + const condition = + e instanceof StreamError && (e.code === StreamCode.NotFound || e.code === StreamCode.Unroutable) + ? "does_not_exist" + : "internal"; + return { errorCode: toRequestCode(condition, kind, this.#session.version), reasonPhrase: reason(e) }; + } + + /** + * Resolve the broadcast a request names for serving: a local publish, or else the + * origin's routes and dynamic handlers. Rejects with the abort once `signal` aborts. + */ + async #resolve(name: Path.Valid, signal: AbortSignal): Promise { + const broadcast = + this.#publish && + (wireOf(this.#publish).local(name) ?? (await wireOf(this.#publish).demand(name, undefined, signal))); + if (!broadcast) throw new NotFound(`broadcast ${name}`); + return broadcast; + } +} + +/** A request's refusal: its code on this draft, and why. */ +interface Refusal { + errorCode: number; + reasonPhrase: string; } /** A `{group, object}` position, in the Location Filter's own units. */ diff --git a/js/net/src/ietf/track.ts b/js/net/src/ietf/track.ts index 3dbeb118ac9..779c2551021 100644 --- a/js/net/src/ietf/track.ts +++ b/js/net/src/ietf/track.ts @@ -1,29 +1,39 @@ import type * as Path from "../path.ts"; import type { Reader, Writer } from "../stream.ts"; +import * as Filter from "./filter.ts"; import * as Message from "./message.ts"; import * as Namespace from "./namespace.ts"; -import { Parameters } from "./parameters.ts"; -import { Subscribe } from "./subscribe.ts"; +import { type MessageLocation, Parameters } from "./parameters.ts"; +import * as Properties from "./properties.ts"; +import { RequestOk } from "./request.ts"; +import { Subscribe, SubscribeOk } from "./subscribe.ts"; import { type IetfVersion, Version } from "./version.ts"; // we only support Group Order descending const GROUP_ORDER = 0x02; +/** TRACK_STATUS_ERROR on draft-14, with the SUBSCRIBE_ERROR body. Later drafts refuse with REQUEST_ERROR. */ +export const TRACK_STATUS_ERROR_14 = 0x0f; + export class TrackStatusRequest { static id = 0x0d; requestId: bigint; trackNamespace: Path.Valid; trackName: string; + /** Whether the requester wants Track Properties on the answer (INCLUDE_PROPERTIES, draft-20). */ + propertiesWanted: boolean; constructor({ requestId, trackNamespace, trackName, - }: { requestId: bigint; trackNamespace: Path.Valid; trackName: string }) { + propertiesWanted = true, + }: { requestId: bigint; trackNamespace: Path.Valid; trackName: string; propertiesWanted?: boolean }) { this.requestId = requestId; this.trackNamespace = trackNamespace; this.trackName = trackName; + this.propertiesWanted = propertiesWanted; } async #encode(w: Writer, version: IetfVersion): Promise { @@ -41,8 +51,10 @@ export class TrackStatusRequest { await w.u53(0x2); // filter_type = LargestObject await w.u53(0); // no parameters } else { - // v15+: just parameters + // v15+: just parameters. Only the opt-out is worth bytes, and an older peer would + // read INCLUDE_PROPERTIES as unknown, which is a protocol violation. const params = new Parameters(); + if (!this.propertiesWanted && Filter.isDraft20(version)) params.includeProperties = false; await params.encode(w, version); } } @@ -53,74 +65,97 @@ export class TrackStatusRequest { /** * Every draft defines TRACK_STATUS as identical to SUBSCRIBE, so it decodes as one and - * keeps only what names the track. We refuse the request, so the rest goes unread. + * keeps what names the track and shapes the answer. The rest goes unread: there is no + * subscription for it to configure. */ static async decode(r: Reader, version: IetfVersion): Promise { - const { requestId, trackNamespace, trackName } = await Subscribe.decode(r, version); - return new TrackStatusRequest({ requestId, trackNamespace, trackName }); + const { requestId, trackNamespace, trackName, propertiesWanted } = await Subscribe.decode(r, version); + return new TrackStatusRequest({ requestId, trackNamespace, trackName, propertiesWanted }); } } -// Track status response (0x0E) — v14 only (TRACK_STATUS_OK) -// In v15+, the response to TrackStatusRequest is RequestOk (0x07) -export class TrackStatus { - static id = 0x0e; +/** + * TRACK_STATUS_OK: what a SUBSCRIBE_OK would say about the track, with no subscription. + * + * Draft-14 gives it its own type and the SUBSCRIBE_OK body, with a Track Alias of 0. Later + * drafts answer with a REQUEST_OK, which gained Track Properties in draft-18; before that + * only the message parameters carry anything. + */ +export class TrackStatusOk { + /** The draft-14 message type. */ + static readonly ID_14 = 0x0e; + + /** The message type on `version`. */ + static id(version: IetfVersion): number { + return version === Version.DRAFT_14 ? TrackStatusOk.ID_14 : RequestOk.id; + } - trackNamespace: Path.Valid; - trackName: string; - statusCode: number; - lastGroupId: bigint; - lastObjectId: bigint; + /** Present on drafts 14-16 only. */ + requestId: bigint | undefined; + + /** The largest Location in the track (LARGEST_OBJECT), absent while it has no content. */ + largest: MessageLocation | undefined; + + /** The Track Properties, on the drafts whose answer has room for them. */ + properties: Properties.Properties; constructor({ - trackNamespace, - trackName, - statusCode, - lastGroupId, - lastObjectId, - }: { - trackNamespace: Path.Valid; - trackName: string; - statusCode: number; - lastGroupId: bigint; - lastObjectId: bigint; - }) { - this.trackNamespace = trackNamespace; - this.trackName = trackName; - this.statusCode = statusCode; - this.lastGroupId = lastGroupId; - this.lastObjectId = lastObjectId; + requestId, + largest, + properties = {}, + }: { requestId?: bigint; largest?: MessageLocation; properties?: Properties.Properties }) { + this.requestId = requestId; + this.largest = largest; + this.properties = properties; } - async #encode(w: Writer): Promise { - await Namespace.encode(w, this.trackNamespace); - await w.string(this.trackName); - await w.u62(BigInt(this.statusCode)); - await w.u62(this.lastGroupId); - await w.u62(this.lastObjectId); + /** Whether the answer carries a Track Properties block on `version`. */ + static #hasProperties(version: IetfVersion): boolean { + return version >= Version.DRAFT_18; } - async encode(w: Writer, _version: IetfVersion): Promise { - return Message.encode(w, this.#encode.bind(this)); - } + async #encode(w: Writer, version: IetfVersion): Promise { + if (version === Version.DRAFT_15 || version === Version.DRAFT_16) { + if (this.requestId === undefined) throw new Error("requestId required for draft14-16"); + await w.u62(this.requestId); + } - static async decode(r: Reader, _version: IetfVersion): Promise { - return Message.decode(r, TrackStatus.#decode); + // The parameters SUBSCRIBE_OK carries: GROUP_ORDER is one only on draft-15. + const params = new Parameters(); + if (this.largest !== undefined) params.largest = this.largest; + if (version === Version.DRAFT_15 && this.properties.groupOrder !== undefined) { + params.groupOrder = this.properties.groupOrder; + } + await params.encode(w, version); + + // Track Properties are the final field, so nothing may follow. + if (TrackStatusOk.#hasProperties(version)) await Properties.encode(w, this.properties, version); } - static async #decode(r: Reader): Promise { - const trackNamespace = await Namespace.decode(r); - const trackName = await r.string(); - const statusCode = Number(await r.u62()); - const lastGroupId = await r.u62(); - const lastObjectId = await r.u62(); + async encode(w: Writer, version: IetfVersion): Promise { + if (version === Version.DRAFT_14) { + const { requestId, largest, properties } = this; + return new SubscribeOk({ requestId, trackAlias: 0n, largest, properties }).encode(w, version); + } + return Message.encode(w, (mw) => this.#encode(mw, version)); + } - return new TrackStatus({ trackNamespace, trackName, statusCode, lastGroupId, lastObjectId }); + static async decode(r: Reader, version: IetfVersion): Promise { + if (version === Version.DRAFT_14) { + const { requestId, largest, properties } = await SubscribeOk.decode(r, version); + return new TrackStatusOk({ requestId, largest, properties }); + } + return Message.decode(r, (mr) => TrackStatusOk.#decode(mr, version)); } - // Track status codes - static readonly STATUS_IN_PROGRESS = 0x00; - static readonly STATUS_NOT_FOUND = 0x01; - static readonly STATUS_NOT_AUTHORIZED = 0x02; - static readonly STATUS_ENDED = 0x03; + static async #decode(r: Reader, version: IetfVersion): Promise { + const requestId = version === Version.DRAFT_15 || version === Version.DRAFT_16 ? await r.u62() : undefined; + const params = await Parameters.decode(r, version, "subscribe-ok"); + const properties = TrackStatusOk.#hasProperties(version) ? await Properties.decode(r, version) : {}; + if (version === Version.DRAFT_15) { + properties.groupOrder = params.groupOrder; + properties.maxCacheDuration = params.maxCacheDuration; + } + return new TrackStatusOk({ requestId, largest: params.largest, properties }); + } } diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index 94c83af6fa8..25452369c4f 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -187,6 +187,8 @@ export const hooks: { pendingTrackProducer: (request: Request) => Producer; /** Have a producer write through a broadcast's sequence namespace for its name. */ bindSequence: (producer: Producer, sequence: TrackSequence) => void; + /** The newest frame a producer's cache holds, read without subscribing; assigned by `track.ts`. */ + cachedLargest: (producer: Producer) => Location | undefined; /** * Take the next group the subscriber's cursor allows, without waiting; assigned by `track.ts`. * @@ -254,6 +256,9 @@ export const hooks: { bindSequence: () => { throw new Error("track.ts not loaded"); }, + cachedLargest: () => { + throw new Error("track.ts not loaded"); + }, tryRecvGroup: () => { throw new Error("track.ts not loaded"); }, diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 1a3040842a6..8d7c04e080d 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -599,6 +599,7 @@ export class Producer { sequence.holders++; }; hooks.bindSequence = bindProducerSequence; + hooks.cachedLargest = (producer) => producer.#largest(); } /** @@ -811,6 +812,22 @@ export class Producer { return group?.closed.peek() instanceof Error ? undefined : group; } + // The newest frame in the cache, as {@link Subscriber.largest} reads it from a sink: + // unknown once the newest group is evicted, since what is left cannot say where the edge is. + #largest(): Location | undefined { + const latest = this.#state.latest; + if (latest === undefined || !this.#cached.has(latest)) return undefined; + + let largest: Location | undefined; + for (const { group } of this.#cache) { + const count = group.consume().frameCount; + if (count > 0 && (largest === undefined || group.sequence > largest.group)) { + largest = { group: group.sequence, frame: count - 1 }; + } + } + return largest; + } + // Evict cached groups idle for longer than the cache window. Idle means nothing // written, so an abandoned open group ages out instead of pinning its buffer (and // any reader parked in it) forever. diff --git a/js/net/src/wire.ts b/js/net/src/wire.ts index 13bc5fd6feb..5526356cd07 100644 --- a/js/net/src/wire.ts +++ b/js/net/src/wire.ts @@ -22,6 +22,8 @@ export interface Broadcast { subscribe(name: string, options?: track.Subscription): track.Subscriber; /** The track's properties; `hold` keeps the track wanted until it aborts, the way a held TRACK stream does, and aborting it before the answer abandons the lookup. */ resolveTrackInfo(name: string, hold?: AbortSignal): Promise; + /** The newest frame the track's cache holds, read without subscribing; `undefined` while it holds none. */ + largest(name: string): track.Location | undefined; fetchGroup(name: string, sequence: number, options?: track.FetchGroupOptions): Promise; requested(): Promise; /** diff --git a/quest/m1/README.md b/quest/m1/README.md index 0074331c067..5800db1a9ff 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -104,7 +104,6 @@ above them. - [Data consumer timestamps](/quest/m1/data-consumer-timestamps.md) - json and binary consumers return each value's timestamp, in Rust and every binding; snapshots add `latest()` beside an in-order `next()` - [JSON window timestamps](/quest/m1/json-window-timestamps.md) - Rust and JS window consumers return each event's frame timestamp like the snapshot and stream consumers - [Exact catalog wall clock](/quest/m1/catalog-wall-clock-precision.md) - `hang::catalog::Clock::wall_clock` keeps sub-millisecond precision -- [JavaScript answers TRACK_STATUS](/quest/m1/js-track-status.md) - a browser publisher answers IETF TRACK_STATUS like Rust, so a native fetch-only request needs no SUBSCRIBE fallback - [JavaScript FETCH](/quest/m1/js-fetch.md) - browser publishers answer IETF FETCH through the JS ranges request surface - [IETF fill timeout](/quest/m1/ietf-fill-timeout.md) - native FILL_TIMEOUT bounds upstream waiting on drafts 18-22, alongside local content-age enforcement - [IETF group ingest refuses loudly](/quest/m1/ietf-ingest-refusal.md) - a subgroup other than 0 or a datagram past object 0 aborts the track, and a peer's malformed group is refused rather than dropped at debug diff --git a/quest/m1/js-fetch.md b/quest/m1/js-fetch.md index 622d506ec9d..9745b145f93 100644 --- a/quest/m1/js-fetch.md +++ b/quest/m1/js-fetch.md @@ -25,9 +25,13 @@ no `NotFetchable` code, since nothing would send it. Only lite-07 has the code, and lite-07 drops FETCH for [ranges](/quest/m1/subscribe-ranges/lite.md); lite-05 and lite-06 answer `NotFound`. -Answering TRACK_STATUS, which a native fetch-only request asks before its -first FETCH, split out to [JavaScript answers TRACK_STATUS](/quest/m1/js-track-status.md) -(2026-10-13): it does not need FETCH. +The JS publisher already answers the TRACK_STATUS a native fetch-only request +asks before its first FETCH, so after that answer the FETCH is all that is +refused today. + +Decided 2026-10-13: once this lands, check whether the Rust IETF subscriber's +fallback to SUBSCRIBE on a NOT_SUPPORTED TRACK_STATUS is still needed, and +delete it if no in-tree publisher refuses anymore. Verify with an in-memory application responder: a browser publisher serves a native IETF subscriber after a group is evicted or was never cached. Run the @@ -43,7 +47,6 @@ claims that change. ## Related -- [JavaScript answers TRACK_STATUS](/quest/m1/js-track-status.md) - answers the TRACK_STATUS a native fetch-only request sends first - [Browser archive](/quest/m3/archive-browser.md) - supplies memory or OPFS archive data through the same request surface - [IETF fill timeout](/quest/m1/ietf-fill-timeout.md) - native timeout budgets and gap dispositions build on this responder - [FETCH max-delay](/quest/m1/fetch-max-delay.md) - local per-reader age limits also apply to historical data diff --git a/quest/m1/js-track-status.md b/quest/m1/js-track-status.md deleted file mode 100644 index 98f188247bc..00000000000 --- a/quest/m1/js-track-status.md +++ /dev/null @@ -1,42 +0,0 @@ -# [S] JavaScript answers TRACK_STATUS - -## Goal - -A `@moq/net` publisher answers IETF TRACK_STATUS on every draft instead of -refusing every one with NOT_SUPPORTED, so a native fetch-only request on -drafts 14-16 and 18-22 learns the track in one round trip rather than falling -back to SUBSCRIBE. Draft 17 still subscribes, since its answer cannot say -whether the track is timed. - -## Plan - -Since #4974, a native subscriber asks TRACK_STATUS for demand without a -subscription. #5409 found that the relay's IETF publisher can make a reader's -first request look fetch-only, and that against a JS publisher the refusal -failed the track; the Rust subscriber now falls back to SUBSCRIBE on -NOT_SUPPORTED, at the cost of an extra round trip. - -Decided 2026-10-13: mirror the Rust publisher -(`rs/moq-net/src/ietf/publisher.rs`, `run_track_status_stream`). Check the -grant before resolving, resolve the broadcast through the serving origin like -a SUBSCRIBE (dynamic handlers included), wait for the track's info, and answer -TRACK_STATUS_OK. A requester that cancels is owed nothing and releases its -demand through the path #5411 added. Errors map to the same refusal codes as -Rust. - -Split out of [JavaScript FETCH](/quest/m1/js-fetch.md), which is blocked on -JS ranges; answering TRACK_STATUS does not need FETCH. - -Verify with a native IETF subscriber against a browser publisher: on drafts -14-16 and 18-22 a fetch-only request gets TRACK_STATUS_OK with no SUBSCRIBE -fallback, while draft 17 keeps subscribing (`TrackStatusOk::describes_track`). -On every draft, a cancelled request releases origin demand and a request -outside the grant is refused. Run `just test interop --all`. - -Public API: none. Wire: implement the existing TRACK_STATUS and -TRACK_STATUS_OK formats in JS; drop JavaScript from the TRACK_STATUS refusals -in `doc/concept/standard.md`. - -## Related - -- [JavaScript FETCH](/quest/m1/js-fetch.md) - serves the FETCH that follows this answer diff --git a/rs/moq-net/src/test_interop.rs b/rs/moq-net/src/test_interop.rs index a3b9dbe47e2..d4e512cb133 100644 --- a/rs/moq-net/src/test_interop.rs +++ b/rs/moq-net/src/test_interop.rs @@ -312,3 +312,104 @@ fn lite_auth_unsupported_interop() { ); assert!(reader.is_empty()); } + +/// TRACK_STATUS on every draft: the JS publisher decodes what Rust encodes and answers with a +/// TRACK_STATUS_OK the Rust subscriber reads, carrying what a SUBSCRIBE_OK would as far as the +/// draft has room, or refuses a missing broadcast DOES_NOT_EXIST. +#[test] +#[ignore = "requires Bun; run by just test interop"] +fn ietf_track_status_interop() { + use crate::coding::Decode; + use crate::ietf::error::request; + + let all = [ + ietf::Version::Draft14, + ietf::Version::Draft15, + ietf::Version::Draft16, + ietf::Version::Draft17, + ietf::Version::Draft18, + ietf::Version::Draft19, + ietf::Version::Draft20, + ietf::Version::Draft21, + ietf::Version::Draft22, + ]; + for (draft, version) in (14..).zip(all) { + let legacy = matches!( + version, + ietf::Version::Draft14 | ietf::Version::Draft15 | ietf::Version::Draft16 + ); + let request = |id: u64, namespace: &str, wanted: bool| { + let mut buf = Vec::new(); + let mut w = Encoder::new(&mut buf, version.into()); + w.varint(::ID).unwrap(); + ietf::TrackStatus { + request_id: ietf::RequestId(id), + track_namespace: crate::Path::new(namespace), + track_name: "video".into(), + properties_wanted: wanted, + } + .encode(&mut w, version) + .unwrap(); + buf + }; + let input = serde_json::json!({ + "draft": draft, + "requests": [request(1, "room", true), request(3, "room", false), request(5, "absent", true)], + }); + let responses: Vec> = + serde_json::from_slice(&bun("track-status.ts", input)).expect("JS returned its responses"); + assert_eq!(responses.len(), 3, "{version}"); + + for (index, wanted) in [true, false].into_iter().enumerate() { + let mut r = Decoder::new(&responses[index], version.into()); + assert_eq!(r.varint().unwrap(), ietf::TrackStatusOk::id(version), "{version}"); + let ok = ietf::TrackStatusOk::decode(&mut r, version).unwrap(); + assert!(r.is_empty(), "{version}: trailing bytes"); + + let expected = match version { + // GROUP_ORDER is a field on draft-14 and a parameter on draft-15. + ietf::Version::Draft14 | ietf::Version::Draft15 => ietf::Properties { + group_order: Some(ietf::GroupOrder::Descending), + ..Default::default() + }, + ietf::Version::Draft16 | ietf::Version::Draft17 => ietf::Properties::default(), + // Only draft-20 can carry the opt-out. + _ if !wanted && ietf::Filter::is_draft20(version) => ietf::Properties::default(), + _ => ietf::Properties { + max_cache_duration: Some(std::time::Duration::from_millis(5000)), + timescale: Some(crate::Timescale::MILLI), + // The JS track's priority 2, lower-first on the wire. + priority: Some(u8::MAX - 2), + group_order: Some(ietf::GroupOrder::Descending), + }, + }; + assert_eq!( + ok, + ietf::TrackStatusOk { + request_id: legacy.then_some(ietf::RequestId(1 + 2 * index as u64)), + largest: Some(ietf::Location { group: 4, object: 1 }), + properties: expected, + }, + "{version} wanted={wanted}" + ); + } + + let mut r = Decoder::new(&responses[2], version.into()); + let code = match r.varint().unwrap() { + ietf::TRACK_STATUS_ERROR_14 if version == ietf::Version::Draft14 => { + ietf::SubscribeError::decode(&mut r, version).unwrap().error_code + } + id if id == ::ID && version != ietf::Version::Draft14 => { + ietf::RequestError::decode(&mut r, version).unwrap().error_code + } + id => panic!("{version}: unexpected refusal type {id}"), + }; + assert!( + matches!( + request::from_code(code, request::Kind::TrackStatus, version), + crate::Error::NotFound + ), + "{version}: missing broadcast refused with {code}" + ); + } +} diff --git a/test/interop/README.md b/test/interop/README.md index e34fd31496a..a09e261b72b 100644 --- a/test/interop/README.md +++ b/test/interop/README.md @@ -354,3 +354,8 @@ and JS refuses them on lite-06. drafts 14 through 22: `ietf-datagram.ts` decodes Rust's datagrams and their Timestamps and re-encodes them byte for byte, and Rust decodes the datagram the JS publisher sends. + +`ietf_track_status_interop` sends Rust's `TRACK_STATUS` to the JS publisher on +drafts 14 through 22 through `track-status.ts`, and Rust decodes each +`TRACK_STATUS_OK` and the refusal for a missing broadcast the way its +subscriber reads them. diff --git a/test/interop/track-status.ts b/test/interop/track-status.ts new file mode 100644 index 00000000000..af856355991 --- /dev/null +++ b/test/interop/track-status.ts @@ -0,0 +1,62 @@ +// Answer Rust-encoded TRACK_STATUS requests with the JS publisher, returning each response. +import assert from "node:assert/strict"; +import { Producer as GroupProducer } from "../../js/net/src/group.ts"; +import { NativeSession } from "../../js/net/src/ietf/adapter.ts"; +import { Publisher } from "../../js/net/src/ietf/publisher.ts"; +import { TrackStatusRequest } from "../../js/net/src/ietf/track.ts"; +import type { IetfVersion } from "../../js/net/src/ietf/version.ts"; +import { createMockTransportPair } from "../../js/net/src/mock.ts"; +import { Producer as OriginProducer } from "../../js/net/src/origin.ts"; +import * as Path from "../../js/net/src/path.ts"; +import { Stream } from "../../js/net/src/stream.ts"; +import { Milli, Timescale, Timestamp } from "../../js/net/src/time.ts"; + +// Stdout is the response-byte channel back to Rust. +console.debug = console.error; +console.info = console.error; +const input: { draft: number; requests: number[][] } = JSON.parse(process.argv[2]); +const version = (0xff000000 + input.draft) as IetfVersion; + +const pair = createMockTransportPair(); +const origin = new OriginProducer(); +const broadcast = origin.createBroadcast(Path.from("room")); +broadcast.announce(); +const track = broadcast.createTrack("video", { timescale: Timescale.MILLI, maxAge: Milli(5000), priority: 2 }); +const group = new GroupProducer(4); +group.writeFrame({ payload: new Uint8Array([1]), timestamp: Timestamp.fromMillis(0) }); +group.writeFrame({ payload: new Uint8Array([2]), timestamp: Timestamp.fromMillis(1) }); +track.writeGroup(group); + +const publisher = new Publisher({ + quic: pair.server, + session: new NativeSession(pair.server, version, true), + publish: origin.consume(), + requiresSolicitation: false, +}); + +const responses: number[][] = []; +for (const request of input.requests) { + const written: number[] = []; + const stream = new Stream({ + // The requester holds its side open until answered: a FIN cancels on drafts 14-18. + readable: new ReadableStream({ + start(controller) { + controller.enqueue(Uint8Array.from(request)); + }, + }), + writable: new WritableStream({ + write(chunk) { + written.push(...chunk); + }, + }), + version, + }); + assert.equal(await stream.reader.u62(), BigInt(TrackStatusRequest.id)); + const msg = await TrackStatusRequest.decode(stream.reader, version); + await publisher.runTrackStatusRequest(msg, stream); + responses.push(written); +} + +publisher.close(); +origin.close(); +process.stdout.write(JSON.stringify(responses)); diff --git a/test/justfile b/test/justfile index fdacef8cd45..88db4dc699f 100644 --- a/test/justfile +++ b/test/justfile @@ -37,11 +37,11 @@ harness: # Finite track tails run too; `--tail` selects just the CLI FETCH close and Rust/Node/Bun tail checks. # Both runs first check js/net's varints against moq-net's encoder at every size boundary, # and on lite-06/07 messages (the filter also matches `lite_varint_interop`), then -# moq-transport OBJECT_DATAGRAMs both ways on every draft, and Lite AUTH_ERROR -# NOT_SUPPORTED after a grant in both directions. +# moq-transport OBJECT_DATAGRAMs both ways and the JS answer to TRACK_STATUS on every +# draft, and Lite AUTH_ERROR NOT_SUPPORTED after a grant in both directions. [positional-arguments] interop *args: - just rs test -p moq-net --lib varint_interop ietf_datagram_interop auth_unsupported_interop --run-ignored all + just rs test -p moq-net --lib varint_interop ietf_datagram_interop ietf_track_status_interop auth_unsupported_interop --run-ignored all ./interop/interop.sh "$@" # Nightly main vs newest installable registry releases: tokens, moq-lite session