diff --git a/js/net/MIGRATE.md b/js/net/MIGRATE.md index 6a1e774a4d4..e12e417b78f 100644 --- a/js/net/MIGRATE.md +++ b/js/net/MIGRATE.md @@ -4,6 +4,10 @@ How to update your code for each breaking change, newest first. ## Unreleased +### Past group ranges work over moq-transport ([#7793](https://github.com/moq-dev/moq/pull/7793)) + +`track.Consumer.fetchGroup()` and subscriptions to past group ranges now use standalone FETCH on drafts 18, 20, and 22. No API migration is needed. First-party publishers serve one group per FETCH; multi-group ranges require a publisher that serves them. + ### IETF FETCH serves on-demand history ([#7794](https://github.com/moq-dev/moq/pull/7794)) A single-group IETF FETCH now asks `track.requests()` for groups the publisher's cache no longer holds, or never held, using the same `Track.Fill` responder as moq-lite. Existing responders need no changes. Multi-group IETF FETCH remains unsupported. diff --git a/js/net/src/fill.ts b/js/net/src/fill.ts index 8350b652dbe..3a329599436 100644 --- a/js/net/src/fill.ts +++ b/js/net/src/fill.ts @@ -339,6 +339,11 @@ function subtract(gap: Run, run: Run): Run[] { ].filter((part) => part.start < part.end); } +/** The still-wanted runs of a fill, in ascending order. @internal */ +export let wantedRuns: (fill: Fill) => Run[]; +/** Wait for a change to the groups or readers of a fill. @internal */ +export let fillChanged: (fill: Fill) => Promise; + // Constructs the public handles from within this module; assigned in their static blocks. let makeFill: (state: FillState, pending: Pending) => Fill; let makeRequests: (state: FillState) => Requests; @@ -443,6 +448,8 @@ export class Fill { static { makeFill = (state, pending) => new Fill(state, pending); + wantedRuns = (fill) => fill.#pending.owed(); + fillChanged = (fill) => Promise.resolve(fill.#pending.revision.changed()); } /** The most urgent delivery priority among the readers waiting on it now. */ diff --git a/js/net/src/goaway-requests.test.ts b/js/net/src/goaway-requests.test.ts index 7bebb5f4ef9..b781fe6c4b4 100644 --- a/js/net/src/goaway-requests.test.ts +++ b/js/net/src/goaway-requests.test.ts @@ -181,8 +181,9 @@ async function handover(kind: "lite" | "ietf"): Promise { // A fresh consumer caches nothing, so the publisher's answer proves the FETCH reached the wire. const fetched = wireOf(client).consume(Path.from("room")).track("video").fetchGroup(0n); - if (kind === "lite") expect(await (await fetched).readString()).toBe("one"); - else await expect(fetched).rejects.toThrow(/not supported/); + const group = await fetched; + expect(await group.readString()).toBe("one"); + group.close(); // A new announce-interest opens too, and sees the route at the drain cost. const interest = client.announced(); diff --git a/js/net/src/ietf/join.test.ts b/js/net/src/ietf/join.test.ts index 9cb0a6563bf..c3569c78f3e 100644 --- a/js/net/src/ietf/join.test.ts +++ b/js/net/src/ietf/join.test.ts @@ -1,7 +1,8 @@ import { expect, jest, onTestFinished, test } from "bun:test"; -import { Elapsed, Instant, Milli, Nano, now, timeout } from "@moq/clock"; +import { Elapsed, Instant, Manual, Milli, Nano, now, timeout } from "@moq/clock"; import * as Connection from "../connection/index.ts"; import { StreamCode } from "../error.ts"; +import { hooks } from "../internal.ts"; import { GRACE } from "../ledger.ts"; import { createMockTransportPair, textFrame } from "../mock.ts"; import { Producer as OriginProducer } from "../origin.ts"; @@ -61,7 +62,10 @@ async function subscribed(version: IetfVersion = VERSION) { const pair = createMockTransportPair(ALPNS[version]); const session = new NativeSession(pair.server, version, true); const subscriber = new Subscriber({ session }); - const consumer = subscriber.consume(Path.from("room")).track("video").subscribe(); + const consumer = subscriber + .consume(Path.from("room")) + .track("video") + .subscribe({ ...track.Subscription.default(), ranges: [{ start: { relative: 0 } }] }); onTestFinished(() => { consumer.close(); session.close(); @@ -617,3 +621,37 @@ for (const version of VERSIONS) { expect(sent.mock.calls).toHaveLength(version === VERSION ? 2 : 0); }); } + +// A completed join must not remain a gate on the group's later cache misses. +test("an evicted joined group is fetched again as standalone history", async () => { + const clock = new Manual(); + const installed = clock.install(); + onTestFinished(() => installed.close()); + const { subscriber, consumer, fetching, fetch, client } = await join(); + if (!fetch) throw new Error("no joining FETCH"); + await accept(fetching); + await subscriber.handleFetch(await head(fetch.requestId, ["h0", "h1"])); + await subscriber.handleGroup(...liveStream(GROUP, 2, ["t2"])); + const first = await consumer.recvGroup(); + expect(await frames(first)).toEqual(["h0", "h1", "t2"]); + first?.close(); + await subscriber.handleGroup(...liveStream(GROUP + 1n, 0, ["new"])); + // Twice the model's 30-second idle cache window, with deterministic time. + await clock.advance(Nano.fromMilli(Milli(60_000))); + expect(hooks.cachedGroup(consumer, GROUP)).toBeUndefined(); + const broadcast = subscriber.consume(Path.from("room")); + onTestFinished(() => broadcast.close()); + const pending = broadcast.track("video").fetchGroup(GROUP); + const stream = await nextStream(client); + if (!stream) throw new Error("no standalone FETCH"); + expect(await stream.reader.u53()).toBe(Fetch.id); + const standalone = await Fetch.decode(stream.reader, VERSION); + expect(standalone.type.kind).toBe("standalone"); + await stream.writer.u53(FetchOk.id); + await new FetchOk({ endOfTrack: false, endLocation: { group: GROUP, object: 3n } }).encode(stream.writer, VERSION); + await subscriber.handleFetch(await head(standalone.requestId, ["h0", "h1", "t2"])); + const group = await pending; + expect(await frames(group)).toEqual(["h0", "h1", "t2"]); + group.close(); + consumer.close(); +}); diff --git a/js/net/src/ietf/range.test.ts b/js/net/src/ietf/range.test.ts new file mode 100644 index 00000000000..d3caba8d7e2 --- /dev/null +++ b/js/net/src/ietf/range.test.ts @@ -0,0 +1,439 @@ +import { expect, onTestFinished, test } from "bun:test"; +import { StreamCode, StreamError } from "../error.ts"; +import { hooks } from "../internal.ts"; +import { createMockTransportPair } from "../mock.ts"; +import * as Path from "../path.ts"; +import { Reader, Stream, Writer } from "../stream.ts"; +import { type Subscription, Producer as TrackProducer } from "../track.ts"; +import { Fetch, FetchOk } from "./fetch.ts"; +import { EndOfRange, FetchEndOfRange, FetchFrame, Group } from "./object.ts"; +import { RequestError } from "./request.ts"; +import { NativeSession } from "./session.ts"; +import { Subscribe, SubscribeOk } from "./subscribe.ts"; +import { Subscriber } from "./subscriber.ts"; +import { TrackStatusOk, TrackStatusRequest } from "./track.ts"; +import { ALPN, type IetfVersion, Version } from "./version.ts"; + +const VERSIONS = [Version.DRAFT_18, Version.DRAFT_20, Version.DRAFT_22] as const; +const ALPNS = { + [Version.DRAFT_18]: ALPN.DRAFT_18, + [Version.DRAFT_20]: ALPN.DRAFT_20, + [Version.DRAFT_22]: ALPN.DRAFT_22, +}; +const ranges = (start: bigint, end: bigint): Subscription => ({ + ranges: [{ start: { absolute: start }, end: { absolute: end } }], + delay: { max: null }, + order: "ascending", +}); + +function fixture(version: IetfVersion) { + const pair = createMockTransportPair(ALPNS[version]); + const session = new NativeSession(pair.server, version, true); + const subscriber = new Subscriber({ session }); + const broadcast = subscriber.consume(Path.from("room")); + const incoming = pair.client.incomingBidirectionalStreams.getReader(); + const readers: { close(): void }[] = []; + onTestFinished(() => { + for (const reader of readers) reader.close(); + broadcast.close(); + session.close(); + incoming.releaseLock(); + }); + const next = async () => { + const stream = await incoming.read(); + if (stream.done) throw new Error("missing request"); + return new Stream({ ...stream.value, version }); + }; + const status = async (largest: bigint | null = 5n, provided?: Stream, typed = false) => { + const stream = provided ?? (await next()); + if (!typed) expect(await stream.reader.u53()).toBe(TrackStatusRequest.id); + const request = await TrackStatusRequest.decode(stream.reader, version); + expect([request.trackNamespace, request.trackName]).toEqual([Path.from("room"), "video"]); + await stream.writer.u53(TrackStatusOk.id); + await new TrackStatusOk({ largest: largest === null ? undefined : { groupId: largest, objectId: 2n } }).encode( + stream.writer, + version, + ); + stream.writer.close(); + }; + const fetch = async (allowStatus = false) => { + let stream = await next(); + let type = await stream.reader.u53(); + if (type === TrackStatusRequest.id && allowStatus) { + await status(5n, stream, true); + stream = await next(); + type = await stream.reader.u53(); + } + expect(type).toBe(Fetch.id); + return { stream, request: await Fetch.decode(stream.reader, version) }; + }; + const answer = async (stream: Stream, group: bigint, object = 2n, ended = false) => { + await stream.writer.u53(FetchOk.id); + await new FetchOk({ + endOfTrack: ended, + endLocation: { group, object: version === Version.DRAFT_18 ? object + 1n : object }, + }).encode(stream.writer, version); + }; + const data = async (id: bigint, groups: bigint[], objects = ["a", "", "c"]) => { + const bytes = await Writer.collect(version, async (writer) => { + await writer.u62(id); + let previousGroup: bigint | undefined; + for (const group of groups) { + for (const [object, payload] of objects.entries()) + await new FetchFrame({ + payload: new TextEncoder().encode(`${payload && group}${payload}`), + priority: 0, + }).encode( + writer, + { + group, + object, + first: previousGroup === undefined && object === 0, + previousGroup: object === 0 ? previousGroup : undefined, + }, + undefined, + version, + ); + previousGroup = group; + } + }); + return subscriber.handleFetch(new Reader(undefined, bytes, version)); + }; + return { subscriber, broadcast, next, status, fetch, answer, data, readers }; +} + +async function payloads(group: { readFrame(): Promise<{ payload: Uint8Array } | undefined> }) { + const out: string[] = []; + for (let frame = await group.readFrame(); frame; frame = await group.readFrame()) + out.push(new TextDecoder().decode(frame.payload)); + return out; +} + +for (const version of VERSIONS) { + test(`${version}: a one-group FETCH waits for FETCH_OK even when its stream overtakes it`, async () => { + const fx = fixture(version); + const pending = fx.broadcast.track("video").fetchGroup(1n); + await fx.status(null); + const { stream, request } = await fx.fetch(); + expect(request.groupOrder).toBe("ascending"); + expect(request.type).toEqual( + version === Version.DRAFT_18 + ? { + kind: "standalone", + trackNamespace: Path.from("room"), + trackName: "video", + start: { group: 1n, object: 0n }, + end: { group: 1n, object: 0n }, + } + : { + kind: "filtered", + trackNamespace: Path.from("room"), + trackName: "video", + filter: { + kind: "absolute", + startGroup: 1n, + startObject: 0n, + endGroup: 1n, + endObject: undefined, + }, + }, + ); + const receiving = fx.data(request.requestId, [1n]); + await fx.answer(stream, 1n); + stream.writer.close(); + const group = await pending; + fx.readers.push(group); + expect(await payloads(group)).toEqual(["1a", "", "1c"]); + await receiving; + }); + + test(`${version}: one standalone FETCH answers a past range, including a missing group`, async () => { + const fx = fixture(version); + const reader = fx.broadcast.track("video").subscribe(ranges(1n, 5n)); + fx.readers.push(reader); + const ordered = reader.ordered(); + const read = ordered.nextGroup(); + await fx.status(); + const { stream, request } = await fx.fetch(); + await fx.answer(stream, 4n); + await fx.data(request.requestId, [1n, 3n, 4n]); + const first = await read; + expect(first?.sequence).toBe(1n); + if (!first) throw new Error("missing first group"); + expect(await payloads(first)).toEqual(["1a", "", "1c"]); + first.close(); + for (const sequence of [3n, 4n]) { + const group = await ordered.nextGroup(); + expect(group?.sequence).toBe(sequence); + if (!group) throw new Error("missing group"); + expect(await payloads(group)).toEqual([`${sequence}a`, "", `${sequence}c`]); + group.close(); + } + expect(await ordered.nextGroup()).toBeUndefined(); + const overlapping = fx.broadcast.track("video").subscribe(ranges(0n, 5n)); + fx.readers.push(overlapping); + const overlap = overlapping.ordered(); + const missing = overlap.nextGroup(); + const backfill = await fx.fetch(); + await fx.answer(backfill.stream, 0n); + await fx.data(backfill.request.requestId, [0n]); + const zero = await missing; + expect(zero?.sequence).toBe(0n); + zero?.close(); + for (const sequence of [1n, 3n, 4n]) { + const group = await overlap.nextGroup(); + expect(group?.sequence).toBe(sequence); + group?.close(); + } + expect(await overlap.nextGroup()).toBeUndefined(); + overlapping.close(); + reader.update(ranges(5n, 6n)); + const more = ordered.nextGroup(); + const second = await fx.fetch(); + await fx.answer(second.stream, 5n); + await fx.data(second.request.requestId, [5n]); + const group = await more; + expect(group?.sequence).toBe(5n); + group?.close(); + }); + + test(`${version}: a refusal fails its reader and a later reader can ask again`, async () => { + const fx = fixture(version); + const pending = fx.broadcast.track("video").fetchGroup(1n); + const failed = pending.catch((error) => error); + await fx.status(); + const { stream } = await fx.fetch(); + await stream.writer.u53(RequestError.id); + await new RequestError({ errorCode: 0x10, reasonPhrase: "missing" }).encode(stream.writer, version); + expect(await failed).toBeInstanceOf(Error); + expect((await failed).message).toContain("missing"); + const retry = fx.broadcast.track("video").fetchGroup(1n); + const second = await fx.fetch(true); + await fx.answer(second.stream, 1n); + await fx.data(second.request.requestId, [1n]); + const group = await retry; + group.close(); + }); + + test(`${version}: a request reset after FETCH_OK refuses the still-missing data stream`, async () => { + const fx = fixture(version); + const pending = fx.broadcast.track("video").fetchGroup(1n); + const failed = pending.catch((error) => error); + await fx.status(); + const { stream } = await fx.fetch(); + await fx.answer(stream, 1n); + stream.abort(new StreamError(StreamCode.Cancel)); + expect(await failed).toBeInstanceOf(Error); + }); + test(`${version}: abandoning a partial group stops its FETCH data stream`, async () => { + const fx = fixture(version); + const pending = fx.broadcast.track("video").fetchGroup(1n); + await fx.status(); + const { stream, request } = await fx.fetch(); + await fx.answer(stream, 1n); + const prefix = await Writer.collect(version, async (writer) => { + await writer.u62(request.requestId); + await new FetchFrame({ payload: new Uint8Array([42]), priority: 0 }).encode( + writer, + { group: 1n, object: 0, first: true }, + undefined, + version, + ); + }); + const cancelled = Promise.withResolvers(); + const readable = new ReadableStream({ + start(controller) { + controller.enqueue(prefix); + }, + cancel: cancelled.resolve, + }); + const receiving = fx.subscriber.handleFetch(new Reader(readable, new Uint8Array(), version)); + const group = await pending; + expect((await group.readFrame())?.payload).toEqual(new Uint8Array([42])); + group.close(); + await cancelled.promise; + await receiving; + }); + + test(`${version}: cancellation before FETCH_OK ends the outstanding request`, async () => { + const fx = fixture(version); + const controller = new AbortController(); + const pending = fx.broadcast + .track("video") + .fetchGroup(1n, { signal: controller.signal }) + .catch((error) => error); + await fx.status(); + const { stream } = await fx.fetch(); + controller.abort(new Error("caller left")); + expect(await pending).toBeInstanceOf(Error); + await stream.reader.closed.catch(() => undefined); + }); + + test(`${version}: a FETCH object outside the accepted range fails the request`, async () => { + const fx = fixture(version); + const pending = fx.broadcast + .track("video") + .fetchGroup(1n) + .catch((error) => error); + await fx.status(); + const { stream, request } = await fx.fetch(); + await fx.answer(stream, 1n); + await fx.data(request.requestId, [2n]); + expect(await pending).toBeInstanceOf(Error); + }); + test(`${version}: a refused live widening leaves history FETCHes and cached groups readable`, async () => { + const fx = fixture(version); + const history = fx.broadcast.track("video").subscribe(ranges(1n, 3n)); + fx.readers.push(history); + const ordered = history.ordered(); + const first = ordered.nextGroup(); + await fx.status(); + const fetching = await fx.fetch(); + await fx.answer(fetching.stream, 2n); + const live = fx.broadcast + .track("video") + .subscribe({ ranges: [{ start: { absolute: 6n } }], delay: { max: null } }); + fx.readers.push(live); + const failed = live.recvGroup().catch((error) => error); + const widening = await fx.next(); + expect(await widening.reader.u53()).toBe(Subscribe.id); + await Subscribe.decode(widening.reader, version); + await widening.writer.u53(RequestError.id); + await new RequestError({ errorCode: 0x10, reasonPhrase: "finished" }).encode(widening.writer, version); + expect(await failed).toBeInstanceOf(Error); + await fx.data(fetching.request.requestId, [1n, 2n]); + const group = await first; + expect(group?.sequence).toBe(1n); + if (!group) throw new Error("missing history group"); + expect(await payloads(group)).toEqual(["1a", "", "1c"]); + group?.close(); + const second = await ordered.nextGroup(); + expect(second?.sequence).toBe(2n); + if (!second) throw new Error("missing history group"); + expect(await payloads(second)).toEqual(["2a", "", "2c"]); + second?.close(); + expect(await ordered.nextGroup()).toBeUndefined(); + const cached = await fx.broadcast.track("video").fetchGroup(1n); + expect(await payloads(cached)).toEqual(["1a", "", "1c"]); + cached.close(); + }); + + test(`${version}: narrowing a widened read back to history ends its live SUBSCRIBE`, async () => { + const fx = fixture(version); + const history = fx.broadcast.track("video").subscribe(ranges(1n, 3n)); + fx.readers.push(history); + const ordered = history.ordered(); + const first = ordered.nextGroup(); + await fx.status(); + const fetching = await fx.fetch(); + await fx.answer(fetching.stream, 2n); + const live = fx.broadcast + .track("video") + .subscribe({ ranges: [{ start: { absolute: 6n } }], delay: { max: null } }); + const widening = await fx.next(); + expect(await widening.reader.u53()).toBe(Subscribe.id); + await Subscribe.decode(widening.reader, version); + await widening.writer.u53(SubscribeOk.id); + await new SubscribeOk({ trackAlias: 7n }).encode(widening.writer, version); + live.close(); + // The serve loop must stop and close the request rather than spin on its settled history wake. + expect(await widening.reader.done()).toBe(true); + await fx.data(fetching.request.requestId, [1n, 2n]); + const group = await first; + if (!group) throw new Error("missing history group"); + expect(await payloads(group)).toEqual(["1a", "", "1c"]); + group.close(); + const second = await ordered.nextGroup(); + if (!second) throw new Error("missing history group"); + expect(await payloads(second)).toEqual(["2a", "", "2c"]); + second.close(); + expect(await ordered.nextGroup()).toBeUndefined(); + }); + + test(`${version}: a relative end on an empty track subscribes for the first group`, async () => { + const fx = fixture(version); + const reader = fx.broadcast.track("video").subscribe({ + ranges: [{ start: { absolute: 0n }, end: { relative: 0 } }], + delay: { max: null }, + order: "ascending", + }); + fx.readers.push(reader); + const ordered = reader.ordered(); + const first = ordered.nextGroup(); + await fx.status(null); + const live = await fx.next(); + expect(await live.reader.u53()).toBe(Subscribe.id); + await Subscribe.decode(live.reader, version); + await live.writer.u53(SubscribeOk.id); + await new SubscribeOk({ trackAlias: 7n }).encode(live.writer, version); + const payload = new TextEncoder().encode("0a"); + const header = new Group({ + trackAlias: 7n, + groupId: 0n, + subGroupId: 0, + publisherPriority: 0, + flags: { + hasExtensions: false, + hasSubgroup: false, + hasSubgroupObject: false, + hasEnd: true, + hasPriority: true, + firstObject: true, + }, + }); + await fx.subscriber.handleGroup( + header, + new Reader(undefined, new Uint8Array([0, payload.byteLength, ...payload]), version), + ); + const group = await first; + expect(group?.sequence).toBe(0n); + if (!group) throw new Error("missing first group"); + expect(await payloads(group)).toEqual(["0a"]); + group.close(); + expect(await ordered.nextGroup()).toBeUndefined(); + }); + + test(`${version}: a nonexistent trailing object range preserves the group's payloads`, async () => { + const fx = fixture(version); + const pending = fx.broadcast.track("video").fetchGroup(1n); + await fx.status(); + const { stream, request } = await fx.fetch(); + await fx.answer(stream, 1n, 5n); + const bytes = await Writer.collect(version, async (writer) => { + await writer.u62(request.requestId); + for (const [object, value] of [1, 2, 3].entries()) + await new FetchFrame({ payload: new Uint8Array([value]), priority: 0 }).encode( + writer, + { group: 1n, object, first: object === 0 }, + undefined, + version, + ); + await new FetchEndOfRange({ reason: EndOfRange.NonExistent, group: 1n, object: 5n }).encode( + writer, + version, + ); + }); + await fx.subscriber.handleFetch(new Reader(undefined, bytes, version)); + const group = await pending; + for (const value of [1, 2, 3]) expect((await group.readFrame())?.payload).toEqual(new Uint8Array([value])); + expect(await group.readFrame()).toBeUndefined(); + group.close(); + }); +} + +for (const ietf of [undefined, Version.DRAFT_18]) { + test(`live metadata alone does not turn its pending data into a cache miss (${ietf ?? "lite"})`, async () => { + const producer = new TrackProducer("video").accept({ ietf }); + hooks.receiveTrack(producer); + hooks.reportedGroup(producer, 5n); + const requests = producer.requests(); + const reader = producer.subscribe(ranges(5n, 6n)); + const reading = reader.recvGroup(); + const missing = requests.requestedFill(); + requests.close(); + expect(await missing).toBeUndefined(); + reader.close(); + expect(await reading).toBeUndefined(); + hooks.releaseTrack(producer); + }); +} diff --git a/js/net/src/ietf/range.ts b/js/net/src/ietf/range.ts new file mode 100644 index 00000000000..79ecadb7187 --- /dev/null +++ b/js/net/src/ietf/range.ts @@ -0,0 +1,212 @@ +import { race, Signal } from "@moq/signals"; +import { error, NotFound, ProtocolViolation, StreamCode, StreamError } from "../error.ts"; +import { type Fill, fillChanged, wantedRuns } from "../fill.ts"; +import type * as group from "../group.ts"; +import { receiveFrame, skipPayload } from "../payload.ts"; +import type { Cursor, Reader } from "../stream.ts"; +import type { Timescale } from "../time.ts"; +import type * as track from "../track.ts"; +import { untilAborted } from "../util/abort.ts"; +import { decodeFetchObject, EndOfRange } from "./object.ts"; + +/** The groups one standalone FETCH writes, sharing the joining FETCH's object and payload decoder. @internal */ +export class RangeFetch { + readonly fill: Fill; + readonly first: bigint; + readonly last: bigint; + readonly bound?: number; + readonly #track: track.Producer; + readonly #timescale?: Timescale; + readonly #legacy: boolean; + readonly #changed = new Signal(0); + readonly #stop = new AbortController(); + readonly #done = Promise.withResolvers(); + #next: bigint; + #current?: { sequence: bigint; producer?: group.Producer }; + #taken = false; + #finished = false; + + constructor(options: { + fill: Fill; + first: bigint; + track: track.Producer; + timescale?: Timescale; + last: bigint; + bound?: number; + legacy: boolean; + }) { + this.fill = options.fill; + this.first = this.#next = options.first; + this.last = options.last; + this.bound = options.bound; + this.#track = options.track; + this.#timescale = options.timescale; + this.#legacy = options.legacy; + } + + /** Settles when the stream finishes or the request cancels it. */ + get done(): Promise { + return this.#done.promise; + } + + /** Stop once no reader wants the remaining groups or the unfinished group already handed out. */ + async unwanted(): Promise { + for (;;) { + const demand = this.#current?.producer?.demand(); + if ( + this.#finished || + (!wantedRuns(this.fill).some((run) => run.start <= this.last && run.end > this.first) && + !demand?.wanted.peek()) + ) + return; + await race([fillChanged(this.fill), Signal.race(this.#changed, ...(demand ? [demand.wanted] : []))]); + } + } + + /** Abort only this stream's unfinished group and refuse what it still owes. Complete history stays cached. */ + cancel(err: Error): void { + if (this.#finished) return; + this.#current?.producer?.abort(err); + this.fill.refuseGroups(this.#next, this.last + 1n, err); + this.#finish(err); + } + + #finish(err?: Error): void { + this.#finished = true; + this.#stop.abort(err ?? new StreamError(StreamCode.Cancel, { message: "fetch complete" })); + this.#changed.update((n) => n + 1); + this.#done.resolve(); + } + + #closeGroup(): void { + this.#current?.producer?.finish(); + this.#current = undefined; + this.#changed.update((n) => n + 1); + } + + #open(sequence: bigint): void { + if (sequence < this.#next || sequence > this.last) + throw new ProtocolViolation("FETCH group is out of order or past FETCH_OK's End Location"); + this.#closeGroup(); + this.fill.dropGroups(this.#next, sequence); + // A live stream may have answered this sequence while the FETCH was in flight. + const producer = this.fill.isWanted(sequence) ? this.fill.accept(sequence) : undefined; + this.#current = { sequence, producer }; + this.#next = sequence + 1n; + this.#changed.update((n) => n + 1); + } + + /** Read one fetch stream after its header, bounded by FETCH_OK, accepting each group at its first object. */ + async receive(stream: Reader): Promise { + if (this.#taken || this.#finished) { + stream.stop(new StreamError(StreamCode.Cancel, { message: "FETCH stream no longer wanted" })); + return; + } + this.#taken = true; + let prior: bigint | undefined; + let next = 0; + let first = true; + let trailing = false; + try { + const decode = (c: Cursor) => decodeFetchObject(c, this.#timescale); + for (;;) { + const object = await untilAborted( + Promise.resolve(stream.tryDecode(decode) ?? stream.decodeMaybe(decode)), + this.#stop.signal, + ); + if (object === undefined) break; + if (object.kind === "endOfRange") { + if ( + object.group < (prior ?? this.first) || + object.group > this.last || + (this.bound !== undefined && object.group === this.last && object.object >= this.bound) + ) + throw new ProtocolViolation("End of Range is outside FETCH_OK's range"); + if (this.#current?.sequence === object.group) { + if (object.reason !== EndOfRange.NonExistent) + throw new NotFound("FETCH group suffix unavailable"); + trailing = true; + } else { + this.#closeGroup(); + if (object.reason === EndOfRange.NonExistent) this.fill.dropGroups(this.#next, object.group); + else this.fill.refuseGroups(this.#next, object.group, new NotFound("FETCH range unavailable")); + this.fill.refuseGroups( + object.group, + object.group + 1n, + new NotFound("FETCH omitted a group prefix"), + ); + this.#current = { sequence: object.group }; + this.#next = object.group + 1n; + } + prior = object.group; + next = object.object + 1; + first = true; + continue; + } + if ( + first && + (object.group === undefined || + object.object === undefined || + object.priority === undefined || + object.subgroup === "prior" || + object.subgroup === "next") + ) + throw new ProtocolViolation("the first fetch object refers to a prior object"); + if ( + object.subgroup === "datagram" || + object.subgroup === "next" || + (object.subgroup === "explicit" && object.subgroupId !== 0) + ) + throw new Error("FETCH subgroups and datagrams are unsupported"); + const sequence = + object.group === undefined ? prior : prior === undefined ? object.group : prior + object.group + 1n; + if (sequence === undefined || sequence > 0xffff_ffff_ffff_ffffn) + throw new ProtocolViolation("invalid FETCH Group ID"); + if (this.#current?.sequence !== sequence) { + this.#open(sequence); + next = 0; + trailing = false; + } + if (trailing) throw new Error("FETCH object gap is unsupported"); + const id = + object.group === undefined + ? object.object === undefined + ? next + : next - 1 + object.object + : object.object; + if (id === undefined || id !== next || !Number.isSafeInteger(id)) + throw new Error("FETCH object gap is unsupported"); + if (sequence === this.last && this.bound !== undefined && id >= this.bound) + throw new ProtocolViolation("FETCH continued past FETCH_OK's End Location"); + if (this.#timescale !== undefined && object.timestamp === undefined) + throw new StreamError(StreamCode.MalformedTrack, { + message: `object without a Timestamp on a track with TIMESCALE: group=${sequence}`, + }); + const producer = this.#current?.producer; + if (producer) { + if (object.payload) producer.writeFrame({ payload: object.payload, timestamp: object.timestamp }); + else if (!(await untilAborted(receiveFrame(stream, producer, object), this.#stop.signal))) + throw new Error("FETCH group was abandoned"); + } else if (!object.payload) await untilAborted(skipPayload(stream, object.size), this.#stop.signal); + prior = sequence; + next = id + 1; + first = false; + } + if ( + this.#legacy && + this.bound !== undefined && + (this.#current?.sequence !== this.last || next < this.bound) + ) + throw new ProtocolViolation("FETCH ended before FETCH_OK's End Location"); + this.#closeGroup(); + this.fill.dropGroups(this.#next, this.last + 1n); + this.#finish(); + } catch (cause) { + const err = error(cause); + this.cancel(err); + if (err instanceof StreamError && err.code === StreamCode.MalformedTrack) this.#track.abort(err); + stream.stop(err); + throw err; + } + } +} diff --git a/js/net/src/ietf/subscriber.test.ts b/js/net/src/ietf/subscriber.test.ts index a649a2dccbb..c1333899c8e 100644 --- a/js/net/src/ietf/subscriber.test.ts +++ b/js/net/src/ietf/subscriber.test.ts @@ -10,7 +10,7 @@ import * as Path from "../path.ts"; import { Reader, Stream } from "../stream.ts"; import { Tail } from "../tail.ts"; import { Timescale, Timestamp } from "../time.ts"; -import type * as track from "../track.ts"; +import * as track from "../track.ts"; import * as demandUtil from "../util/demand.ts"; import type * as Cluster from "./cluster.ts"; import { Connection } from "./connection.ts"; @@ -24,6 +24,7 @@ import { NativeSession } from "./session.ts"; import { Subscribe, SubscribeOk, SubscribeUpdate } from "./subscribe.ts"; import { SubscribeNamespace, SubscribeNamespaceEntry, SubscribeNamespaceEntryDone } from "./subscribe_namespace.ts"; import { Subscriber } from "./subscriber.ts"; +import { TrackStatusOk, TrackStatusRequest } from "./track.ts"; import { ALPN, type IetfVersion, Version } from "./version.ts"; const VERSION = Version.DRAFT_18; @@ -768,7 +769,10 @@ async function subscribeTrack({ const session = new NativeSession(pair.server, version, true); const subscriber = new Subscriber({ session }); - const track = subscriber.consume(Path.from("room")).track("video").subscribe(subscription); + const consumer = subscriber + .consume(Path.from("room")) + .track("video") + .subscribe(subscription ?? { ...track.Subscription.default(), ranges: [{ start: { relative: 0 } }] }); const peer = await nextStream(pair.client); if (!peer) throw new Error("the subscriber never opened a subscribe stream"); @@ -780,7 +784,7 @@ async function subscribeTrack({ await peer.writer.u53(SubscribeOk.id); await new SubscribeOk({ trackAlias: ALIAS, properties }).encode(peer.writer, version); - return { subscriber, track }; + return { subscriber, track: consumer }; } // A refusal already ended the request: a reset back would tell the publisher we failed. @@ -903,19 +907,18 @@ test("older peer without priority property inherits the default publisher priori track.close(); }); -test("an info-only lookup waits for SUBSCRIBE_OK instead of abandoning", async () => { +test("an info-only lookup waits for TRACK_STATUS_OK instead of abandoning", async () => { const pair = createMockTransportPair(ALPN.DRAFT_18); const session = new NativeSession(pair.server, VERSION, true); const subscriber = new Subscriber({ session }); const info = subscriber.consume(Path.from("room")).track("video").info(); const peer = await nextStream(pair.client); - if (!peer) throw new Error("missing SUBSCRIBE stream"); - expect(await peer.reader.u53()).toBe(Subscribe.id); - await Subscribe.decode(peer.reader, VERSION); + if (!peer) throw new Error("missing TRACK_STATUS stream"); + expect(await peer.reader.u53()).toBe(TrackStatusRequest.id); + await TrackStatusRequest.decode(peer.reader, VERSION); - await peer.writer.u53(SubscribeOk.id); - await new SubscribeOk({ - trackAlias: ALIAS, + await peer.writer.u53(TrackStatusOk.id); + await new TrackStatusOk({ properties: { priority: 37 }, }).encode(peer.writer, VERSION); expect((await info).priority).toBe(127 - 37); diff --git a/js/net/src/ietf/subscriber.ts b/js/net/src/ietf/subscriber.ts index c181b66a7f0..14a9dc6f2bb 100644 --- a/js/net/src/ietf/subscriber.ts +++ b/js/net/src/ietf/subscriber.ts @@ -10,6 +10,7 @@ import * as DatagramStream from "../datagram_stream.ts"; import { closeError, error, + NotFound, ProtocolViolation, reason, SessionCode, @@ -18,6 +19,7 @@ import { sessionCause, unauthorized, } from "../error.ts"; +import { type Fill, fillChanged, type Requests, wantedRuns } from "../fill.ts"; import type * as netGroup from "../group.ts"; import { Cost, type Route, routesEqual, UNKNOWN_HOP } from "../hop.ts"; import * as Instance from "../instance.ts"; @@ -34,8 +36,9 @@ import { closeUnwanted } from "../util/announce.ts"; import { Deadline, requestOptions } from "../util/deadline.ts"; import { abandoned as demandAbandoned, isAbandoned, unwanted } from "../util/demand.ts"; import { LIMITS, type Limits, SlotMap, Slots } from "../util/limits.ts"; -import { wireSpan } from "../window.ts"; -import { overrideBroadcastWire, wireOf } from "../wire.ts"; +import { min } from "../util/sequence.ts"; +import { bounded, wireSpan } from "../window.ts"; +import { wireOf } from "../wire.ts"; import { DuplicateTrackAlias, RetiredTrackAlias, TrackAliases } from "./aliases.ts"; import * as Cluster from "./cluster.ts"; import { ObjectDatagram } from "./datagram.ts"; @@ -57,12 +60,14 @@ import { Parameters } from "./parameters.ts"; import { DEFAULT_WIRE, fromWire, toWire } from "./priority.ts"; import { type Publish, PublishDone, PublishDoneStatus, publishDoneClean } from "./publish.ts"; import { type AdvertParams, type PublishNamespace, PublishNamespaceUpdate } from "./publish_namespace.ts"; +import { RangeFetch } from "./range.ts"; import { RequestError, RequestOk } from "./request.ts"; -import { type Framed, RequestStream, refuseReserved, refuseUpdate } from "./request_stream.ts"; +import { type Framed, malformed, RequestStream, refuseReserved, refuseUpdate } from "./request_stream.ts"; import type { Session } from "./session.ts"; import { joinFilter, Subscribe, SubscribeOk, SubscribeUpdate } from "./subscribe.ts"; import { SubscribeNamespace, SubscribeNamespaceEntry, SubscribeNamespaceEntryDone } from "./subscribe_namespace.ts"; import { MalformedAuthToken } from "./token.ts"; +import { TrackStatusOk, TrackStatusRequest } from "./track.ts"; // Wire ceiling (2^62-1). A draining session stamps it on every live route so any other // candidate outranks it, while the route stays selectable as the last path. Matches Rust @@ -103,6 +108,7 @@ type SubscribeSetupState = { // The accepted subscription, which a registered alias names. subscription?: Subscription; registeredAlias?: bigint; + requests?: Requests; /** * Whether the caller has given up. Setup checks this once the stream exists so a request * opened after the timeout still unwinds: nothing else settles setup if the peer has @@ -165,6 +171,8 @@ export class Subscriber { // a draft-18 joining FETCH's own request ID, or a draft-22 SUBSCRIBE's, whose fill can // overtake SUBSCRIBE_OK and so waits on it. A fetch stream needs no track alias. #fetches = new Map>(); + #ranges = new Map>(); + #joins = new WeakMap(); // Dedup consumed broadcasts per path: repeat consume() calls share one subscription. #consumes = new BroadcastCache(); @@ -684,7 +692,6 @@ export class Subscriber { } #createConsume(path: Path.Valid, instance: ReturnType): broadcast.Consumer { - // moq-transport resolves metadata through SUBSCRIBE and has no one-shot group fetch here. const consumer = new ConsumeBroadcast(); // Registered before anyone can list the broadcast, so no reader syncs on an empty answer. void this.#refuseListing(wireOf(consumer).lister()); @@ -727,7 +734,12 @@ export class Subscriber { ]); } - async #runSubscribe(broadcast: Path.Valid, request: track.Request, valid?: AbortSignal) { + async #runSubscribe( + broadcast: Path.Valid, + request: track.Request, + valid?: AbortSignal, + known?: { producer: track.Producer; edge: bigint }, + ) { const refused = unauthorized(broadcast); try { for (;;) { @@ -750,6 +762,11 @@ export class Subscriber { return; } + if (!known && (bounded(request.subscription.ranges ?? [{}]) || request.demand().wanted.peek() === undefined)) { + await this.#runHistory(broadcast, request, valid); + return; + } + let requestId: bigint | undefined; try { valid?.throwIfAborted(); @@ -773,7 +790,7 @@ export class Subscriber { // and any registration if setup eventually finishes. const state: SubscribeSetupState = {}; const deadline = new Deadline(); - const setup = this.#openSubscribe(state, broadcast, request, requestId, deadline); + const setup = this.#openSubscribe(state, broadcast, request, requestId, deadline, known?.producer); // The publisher can be serving before it answers, so waiting only on the response // would miss the local side going away and leave it serving a track nobody reads. @@ -813,7 +830,8 @@ export class Subscriber { // A control request that timed out is not late content, so it carries its own code. // Local abandonment must commit without yielding after the demand check. const e = err === abandoned ? abandoned : await sessionCause(this.#quic, err); - request.reject(e); + if (known) hooks.refuseLive(known.producer, known.edge, e); + else request.reject(e); console.warn( `subscribe error: id=${requestId} broadcast=${broadcast} track=${request.name} error=${reason(e)}`, ); @@ -828,6 +846,7 @@ export class Subscriber { state.fill?.(undefined); this.#fetches.delete(requestId); state.subscription?.join?.cancel(e); + state.requests?.close(); if ( state.registeredAlias !== undefined && @@ -853,6 +872,7 @@ export class Subscriber { } const producer = subscription.track; + if (state.requests) void this.#runFills(broadcast, producer, state.requests, valid); let disposeGrant: Dispose | undefined; // Ends the joining FETCH with the subscription. const joining = new AbortController(); @@ -867,6 +887,12 @@ export class Subscriber { const localEnded = Symbol("local"); const revokedEnded = Symbol("revoked"); const idle = Symbol("idle"); + // History readers may still want the track once its live part is no longer read, so + // this wake stops serving without the demand re-check `idle` takes. + const historyEnded = Symbol("history"); + const historical = known + ? this.#historyOnly(producer, known.edge).then(() => historyEnded) + : new Promise(() => {}); // Losing the grant ends the subscription, leaving the session alone. let revoke!: () => void; @@ -888,7 +914,16 @@ export class Subscriber { hooks.trackReleased(producer).then(() => localEnded), revoked, ]); - const updates = this.#runUpdates(stream, requestId, producer, priority, maxAge, unanswered, done); + const stopped = Promise.withResolvers(); + const updates = this.#runUpdates( + stream, + requestId, + producer, + priority, + maxAge, + unanswered, + race([done, stopped.promise]), + ); // Serve until a terminal condition fires or the last local subscriber leaves. The unused // wake is level-triggered: re-check demand so a subscriber that returns before we tear @@ -896,7 +931,7 @@ export class Subscriber { let terminal = localEnded; const demand = producer.demand(); for (;;) { - const reason = await race([done, unwanted(demand).then(() => idle)]); + const reason = await race([done, unwanted(demand).then(() => idle), historical]); if ( reason === idle && hooks.demandReleased(demand).peek() === undefined && @@ -911,16 +946,21 @@ export class Subscriber { // reopens the window the demand re-check above just closed, and a subscriber that // returned during it would be closed by this line. The lite subscriber closes // straight out of its loop for the same reason. + if (known && terminal === publisherEnded && !hooks.trackFinished(producer)) { + const largest = producer.largest(); + hooks.endTrack(producer, { group: largest ? largest.group + 1n : 0n, frame: 0 }); + } if (terminal === revokedEnded) { console.info(`subscription no longer authorized: broadcast=${broadcast} track=${request.name}`); producer.abort(refused); } else { // Only a received end declares one; a copy nobody wants anymore is released, not finished. - hooks.releaseTrack(producer); + if (!known) hooks.releaseTrack(producer); } // Closing settled `done`, so an update still being written finishes and nothing starts // another. Its bytes must not interleave with the close. + stopped.resolve(); await updates; stream.close(); @@ -928,12 +968,14 @@ export class Subscriber { } catch (err) { stream.abort(err); const e = await sessionCause(this.#quic, err); - producer.abort(e); + if (known) hooks.refuseLive(producer, known.edge, e); + else producer.abort(e); console.warn( `subscribe error: id=${requestId} broadcast=${broadcast} track=${request.name} error=${reason(e)}`, ); } finally { joining.abort(); + if (known) hooks.cacheTrack(producer); // A track end already settled the head; anything else leaves it for a later join. subscription.join?.cancel(new Error("subscription ended")); for (const id of subscription.fetchIds) this.#fetches.delete(id); @@ -944,6 +986,301 @@ export class Subscriber { } } + #needsLive(producer: track.Producer, edge: bigint): boolean { + if (hooks.trackFinished(producer) || !hooks.subscribing(producer).peek()) return false; + const ranges = producer.subscription.peek()?.ranges ?? []; + if (!bounded(ranges)) return true; + const end = wireSpan(ranges).end; + // A relative end resolves against Largest, or against the first group when none exists yet, + // which only the live feed delivers. + return end === undefined ? edge === 0n : end > edge; + } + + async #historyOnly(producer: track.Producer, edge: bigint): Promise { + while (this.#needsLive(producer, edge) && hooks.trackReleased(producer).peek() === undefined) { + await Signal.race(producer.subscription, hooks.subscribing(producer), hooks.trackReleased(producer)); + } + } + + // A bounded read can be entirely below Largest, including on a finished track that refuses + // SUBSCRIBE. Learn its properties without subscribing, then let its misses ask FETCHes. + async #runHistory(broadcast: Path.Valid, request: track.Request, valid?: AbortSignal) { + const stop = new AbortController(); + const signal = valid ? AbortSignal.any([valid, stop.signal]) : stop.signal; + const revoked = this.#grant?.subscribe(() => { + if (this.#denied(broadcast)) stop.abort(unauthorized(broadcast)); + }); + let stream: RequestStream | undefined; + let producer: track.Producer | undefined; + let fills: Promise | undefined; + const deadline = new Deadline(); + try { + const setup = async () => { + const id = await this.#session.nextRequestId(signal); + if (id === undefined) throw await this.#closedSession(); + stream = await RequestStream.open(this.#session, deadline, this.#quic); + if (signal.aborted) { + stream.abort(signal.reason); + signal.throwIfAborted(); + } + await Message.write( + stream.writer, + TrackStatusRequest.id, + new TrackStatusRequest({ + requestId: id, + trackNamespace: broadcast, + trackName: request.name, + }), + this.#session.version, + ); + const response = await stream.next(); + if (response?.type === RequestError.id) { + const err = await RequestError.decode(response.reader, this.#session.version); + throw new Error( + requestReason(err.errorCode, err.reasonPhrase, "track_status", this.#session.version), + ); + } + if (response?.type !== TrackStatusOk.id) throw new ProtocolViolation("expected TRACK_STATUS_OK"); + try { + return await TrackStatusOk.decode(response.reader, this.#session.version); + } catch (err) { + throw malformed(err, "TRACK_STATUS_OK"); + } + }; + const ok = await untilAborted( + race([ + deadline.wait(setup()), + demandAbandoned(request.demand()).then((): never => { + throw new Error("history read abandoned"); + }), + ]), + signal, + ); + stream?.close(); + if (this.#denied(broadcast)) throw unauthorized(broadcast); + const cache = ok.properties.maxCacheDuration; + if (cache !== undefined && cache > BigInt(Number.MAX_SAFE_INTEGER)) + throw new RangeError("max cache duration exceeds safe milliseconds"); + const staged = hooks.stageRequest(request, { + timescale: ok.properties.timescale, + priority: fromWire(ok.properties.priority ?? DEFAULT_WIRE), + maxAge: cache === undefined ? undefined : Milli(Number(cache)), + ietf: this.#session.version, + }); + producer = staged.producer; + // Only FETCH feeds this copy. Mark it live if a later widening opens SUBSCRIBE. + if (ok.largest) hooks.reportedGroup(producer, ok.largest.groupId); + const requests = producer.requests(); + fills = this.#runFills(broadcast, producer, requests, signal); + staged.commit(); + const subscribing = hooks.subscribing(producer); + for (;;) { + if (hooks.trackReleased(producer).peek() !== undefined) return; + const subscription = producer.subscription.peek(); + if (!subscription) { + hooks.releaseTrack(producer); + return; + } + const edge = (producer.largest()?.group ?? ok.largest?.groupId ?? -1n) + 1n; + if (this.#needsLive(producer, edge)) { + await this.#runSubscribe(broadcast, request, signal, { producer, edge }); + continue; + } + await untilAborted( + Promise.resolve(Signal.race(producer.subscription, subscribing, hooks.trackReleased(producer))), + signal, + ); + } + } catch (cause) { + const err = await sessionCause(this.#quic, cause); + stream?.abort(err); + request.reject(err); + } finally { + stop.abort(); + revoked?.(); + if (producer) hooks.releaseTrack(producer); + await fills; + } + } + + // One handler per received track. Each fill owns its network request, and cancellation of + // the track stops and joins every job before releasing the handler. + async #runFills(broadcast: Path.Valid, producer: track.Producer, requests: Requests, valid?: AbortSignal) { + const stop = new AbortController(); + const signal = valid ? AbortSignal.any([valid, stop.signal]) : stop.signal; + const released = hooks + .trackReleased(producer) + .subscribe(() => stop.abort(hooks.trackReleased(producer).peek())); + const jobs = new Set>(); + try { + for (;;) { + const fill = await untilAborted(requests.requestedFill(), signal); + if (!fill) break; + const run = async () => { + try { + // The join may already bring a miss in this run. Let it answer first instead + // of fetching the same head twice. + const join = this.#joins.get(producer); + if (join && fill.start <= join.sequence && fill.end > join.sequence) { + await untilAborted(join.settled, signal); + // A failed join costs that head. Retrying it as standalone would hide a publisher bug. + if (fill.isWanted(join.sequence)) + fill.refuseGroups( + join.sequence, + join.sequence + 1n, + new NotFound("joining FETCH did not provide the head"), + ); + } + for (let run = wantedRuns(fill)[0]; run; run = wantedRuns(fill)[0]) { + await this.#runRangeFetch(broadcast, producer, { fill, ...run, signal }); + } + } catch (cause) { + fill.refuseGroups(fill.start, fill.end, error(cause)); + } finally { + fill.close(); + } + }; + const job = run(); + jobs.add(job); + void job.then(() => jobs.delete(job)); + } + } catch (cause) { + if (!signal.aborted) producer.abort(error(cause)); + } finally { + stop.abort(); + released(); + requests.close(); + await Promise.all(jobs); + } + } + + async #runRangeFetch( + broadcast: Path.Valid, + producer: track.Producer, + options: { fill: Fill; start: bigint; end: bigint; signal: AbortSignal }, + ) { + const { fill, start, end: until, signal } = options; + const version = this.#session.version; + let stream: RequestStream | undefined; + let id: bigint | undefined; + let receiving: RangeFetch | undefined; + const accepted = Promise.withResolvers(); + const stop = new AbortController(); + const active = AbortSignal.any([signal, stop.signal]); + const abandoned = async (): Promise => { + while (!receiving) { + if (!wantedRuns(fill).some((run) => run.start < until && run.end > start)) + throw new StreamError(StreamCode.Cancel); + await untilAborted(fillChanged(fill), active); + } + await untilAborted(receiving.unwanted(), active); + throw new StreamError(StreamCode.Cancel); + }; + const run = async () => { + id = await this.#session.nextRequestId(active); + active.throwIfAborted(); + if (id === undefined) throw await this.#closedSession(); + this.#ranges.set(id, accepted.promise); + stream = new RequestStream(await this.#session.openBi(requestOptions), this.#session, this.#quic); + if (active.aborted) { + stream.abort(active.reason); + active.throwIfAborted(); + } + const last = until - 1n; + const namespace = { trackNamespace: broadcast, trackName: producer.name }; + await Message.write( + stream.writer, + Fetch.id, + new Fetch({ + requestId: id, + subscriberPriority: toWire(fill.priority), + groupOrder: "ascending", + type: isDraft20(version) + ? { + kind: "filtered", + ...namespace, + filter: { kind: "absolute", startGroup: start, startObject: 0n, endGroup: last }, + } + : { + kind: "standalone", + ...namespace, + start: { group: start, object: 0n }, + end: { group: last, object: 0n }, + }, + }), + version, + ); + const response = await stream.next(); + if (response?.type === RequestError.id) { + const err = await RequestError.decode(response.reader, version); + throw new Error(requestReason(err.errorCode, err.reasonPhrase, "fetch", version)); + } + if (response?.type !== FetchOk.id) throw new ProtocolViolation("expected FETCH_OK"); + let ok: FetchOk; + try { + ok = await FetchOk.decode(response.reader, version); + } catch (err) { + throw new ProtocolViolation(`malformed FETCH_OK: ${reason(error(err))}`); + } + active.throwIfAborted(); + const end = ok.endLocation; + const whole = !isDraft20(version) && end.group === last && end.object === 0n; + if (isDraft20(version) && (end.group < start || end.group > last)) + throw new ProtocolViolation("FETCH_OK's End Location is outside the requested range"); + if (!whole && (end.group < start || (end.group === start && end.object === 0n && !isDraft20(version)))) + throw new NotFound("empty FETCH"); + const covered = whole + ? last + : end.object === 0n && !isDraft20(version) + ? min(end.group - 1n, last) + : min(end.group, last); + const bound = + whole || end.group > last || (end.object === 0n && !isDraft20(version)) + ? undefined + : end.object + (isDraft20(version) ? 1n : 0n); + if (bound !== undefined && bound > BigInt(Number.MAX_SAFE_INTEGER)) + throw new ProtocolViolation("FETCH_OK Object ID exceeds the model's safe range"); + if (ok.endOfTrack) + hooks.endTrack(producer, { + group: end.group + (whole || isDraft20(version) || end.object > 0n ? 1n : 0n), + frame: 0, + }); + if (covered < last) + fill.refuseGroups(covered + 1n, until, new NotFound("FETCH ended before the requested range")); + const info = await producer.info(); + active.throwIfAborted(); + receiving = new RangeFetch({ + fill, + first: start, + track: producer, + timescale: info.timescale ?? undefined, + last: covered, + bound: bound === undefined ? undefined : Number(bound), + legacy: !isDraft20(version), + }); + accepted.resolve(receiving); + const ended = stream.next().then((message) => { + if (message !== undefined) throw new ProtocolViolation("unexpected message after FETCH_OK"); + return receiving?.done; + }); + await race([receiving.done, ended]); + stream.close(); + }; + try { + await untilAborted(race([run(), abandoned()]), signal); + } catch (cause) { + const err = await sessionCause(this.#quic, cause); + receiving?.cancel(err); + fill.refuseGroups(start, until, err); + stream?.abort(err); + if (err instanceof ProtocolViolation) this.#fail(err.code, err.message); + } finally { + stop.abort(); + accepted.resolve(undefined); + if (id !== undefined) this.#ranges.delete(id); + } + } + /** * Read the PUBLISH_DONE that ends a subscription, then wait for the data streams it counts. * @@ -1180,6 +1517,7 @@ export class Subscriber { request: track.Request, requestId: bigint, deadline: Deadline, + known?: track.Producer, ): Promise<{ stream: RequestStream; subscription: Subscription; @@ -1248,16 +1586,20 @@ export class Subscriber { } // Staged until the answer is written in below, so no reader sees the track before its // Largest and start. - const staged = hooks.stageRequest(request, { - // No TIMESCALE means no timeline, and the track must not claim one when served onward. - timescale: ok.properties.timescale, - priority: fromWire(ok.properties.priority ?? DEFAULT_WIRE), - maxAge: maxCacheDuration === undefined ? undefined : Milli(Number(maxCacheDuration)), - // A moq-transport peer may stamp arrival times, and nothing on the wire says it didn't. - ietf: version, - }); - const producer = staged.producer; + const staged = known + ? undefined + : hooks.stageRequest(request, { + // No TIMESCALE means no timeline, and the track must not claim one when served onward. + timescale: ok.properties.timescale, + priority: fromWire(ok.properties.priority ?? DEFAULT_WIRE), + maxAge: maxCacheDuration === undefined ? undefined : Milli(Number(maxCacheDuration)), + // A moq-transport peer may stamp arrival times, and nothing on the wire says it didn't. + ietf: version, + }); + const producer = known ?? staged?.producer; + if (!producer) throw new Error("subscription has no track"); hooks.receiveTrack(producer); + if (!known) state.requests = producer.requests(); const tail = new Tail(); const subscription: Subscription = { track: producer, tail, timescale: ok.properties.timescale, fetchIds: [] }; // The subscription starts after Largest, so the joining FETCH or the fill brings its @@ -1265,6 +1607,11 @@ export class Subscriber { // is one of the subscription's own streams, so the Stream Count already waits for it. if (ok.largest) { subscription.join = new Join(ok.largest.groupId, tail, { counted: msg.fill !== undefined }); + const join = subscription.join; + this.#joins.set(producer, join); + void join.settled.then(() => { + if (this.#joins.get(producer) === join) this.#joins.delete(producer); + }); } if (msg.fill) subscription.fetchIds.push(requestId); state.subscription = subscription; @@ -1281,7 +1628,7 @@ export class Subscriber { start: ok.largest ? ok.largest.groupId : 0n, requested: wireSpan(request.subscription.ranges ?? [{}]).start, }); - staged.commit(); + staged?.commit(); state.fill?.(subscription); try { @@ -1659,10 +2006,10 @@ export class Subscriber { } /** - * Handles a fetch stream, with its type already read: the head of a join, answering a - * draft-18 joining FETCH or a draft-22 fill. + * Handles a fetch stream, with its type already read: a standalone range, a draft-18 + * joining FETCH, or a draft-22 fill. * - * Either asked for one group from object 0, so the first object must spell + * A joining FETCH or fill asked for one group from object 0, so its first object must spell * `{G, 0}` and every later one follow it in that group. The head is created as soon as its * first object arrives and written as the objects do, so readers see it at once; the live * tail takes the group over through the {@link Join}. Anything the head cannot stitch, @@ -1673,6 +2020,20 @@ export class Subscriber { async handleFetch(stream: Reader) { const version = this.#session.version; const { requestId } = await FetchHeader.decode(stream, version); + const range = this.#ranges.get(requestId); + if (range) { + const receiving = await range; + if (!receiving) { + stream.stop(new StreamError(StreamCode.Cancel)); + return; + } + try { + await receiving.receive(stream); + } catch (err) { + if (err instanceof ProtocolViolation) this.#fail(err.code, err.message); + } + return; + } const subscription = await this.#fetches.get(requestId); // Nothing of ours asked: the publisher is answering a FETCH or fill we never sent. if (!subscription?.join) throw new Error(`unexpected fetch stream: request=${requestId}`); @@ -1854,18 +2215,11 @@ export class Subscriber { } } -/** - * A broadcast consumed from a moq-transport session. Track info is resolved by the - * subscribe path (the inherited `resolveTrackInfo`), but this client has no one-shot - * group fetch, so the wire offers none and `track.Consumer.fetchGroup()` is rejected. - */ +// The protected broadcast constructor keeps network-backed consumers package-private. class ConsumeBroadcast extends broadcast.Consumer { constructor(state?: never) { super(state); - overrideBroadcastWire(this, { fetchGroup: undefined }); } - - // Preserve the subclass when the consume cache shares this broadcast across callers. override clone(): ConsumeBroadcast { return new ConsumeBroadcast(this.shareState()); } diff --git a/js/net/src/ietf/tail.test.ts b/js/net/src/ietf/tail.test.ts index 03638dbe7c7..31d69abe76d 100644 --- a/js/net/src/ietf/tail.test.ts +++ b/js/net/src/ietf/tail.test.ts @@ -61,7 +61,7 @@ async function subscribed(largest?: bigint) { const reader = subscriber .consume(Path.from("room")) .track("video") - .subscribe({ delay: { max: Milli(60_000) } }); + .subscribe({ ranges: [{ start: { relative: 0 } }], delay: { max: Milli(60_000) } }); const peer = await Stream.accept(pair.client, VERSION); if (!peer) throw new Error("the subscriber never opened a subscribe stream"); diff --git a/js/net/src/integration.test.ts b/js/net/src/integration.test.ts index 063438c8dc2..281d522de18 100644 --- a/js/net/src/integration.test.ts +++ b/js/net/src/integration.test.ts @@ -1448,23 +1448,39 @@ test("integration: lite draft-07 fetch uses the republished track's timescale", server.abort(); }); -test("integration: ietf fetch group is unsupported", async () => { - const pair = createMockTransportPair(Ietf.ALPN.DRAFT_18); - const origin = new OriginProducer(); - - const [client, server] = await Promise.all([ - connect(url, { transport: pair.client }), - accept(pair.server, url, { publish: origin.consume() }), - ]); - - const remote = wireOf(client).consume(Path.from("test")); - await expect(remote.track("video").fetchGroup(0n)).rejects.toThrow( - "fetch group is not supported by this session's version", - ); - - remote.close(); - client.abort(); - server.abort(); +test("integration: ietf fetches a past group of a finished track", async () => { + for (const protocol of [Ietf.ALPN.DRAFT_18, Ietf.ALPN.DRAFT_20, Ietf.ALPN.DRAFT_22]) { + const pair = createMockTransportPair(protocol); + const origin = new OriginProducer(); + const publication = origin.createBroadcast(Path.from("test")); + publication.announce(); + const published = publication.broadcast.createTrack("video", { timescale: Timescale.MILLI }); + for (let sequence = 0; sequence < 3; sequence++) { + const group = published.appendGroup(); + group.writeFrame({ + payload: new Uint8Array([sequence]), + timestamp: new Timestamp(sequence * 1000, Timescale.MILLI), + }); + group.finish(); + } + published.finish(); + const [client, server] = await Promise.all([ + connect(url, { transport: pair.client }), + accept(pair.server, url, { publish: origin.consume() }), + ]); + const remote = wireOf(client).consume(Path.from("test")); + const group = await remote.track("video").fetchGroup(1n); + expect(group.sequence).toBe(1n); + const frame = await group.readFrame(); + expect(frame?.payload).toEqual(new Uint8Array([1])); + expect(frame?.timestamp).toEqual(new Timestamp(1000, Timescale.MILLI)); + expect(await group.readFrame()).toBeUndefined(); + group.close(); + remote.close(); + client.abort(); + server.abort(); + origin.close(); + } }); // We list no tracks by PUBLISH, so a SUBSCRIBE_TRACKS is refused NOT_SUPPORTED on its own stream, and diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index 0c6408ec830..f3a4526ad2c 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -615,6 +615,12 @@ export const hooks: { groupChanged: (subscriber: Subscriber, fn: () => void) => Dispose; /** Apply a FETCH reader budget without inheriting SUBSCRIBE's zero default. */ fetchBudget: (subscriber: Subscriber, maxDelay: Milli | undefined) => void; + /** Whether a reader beyond one-shot fetches subscribes to this copy. */ + subscribing: (producer: Producer) => Getter; + /** Leave a wire copy serving its cache and fills without a live feed. */ + cacheTrack: (producer: Producer) => void; + /** Fail subscriptions reaching beyond `edge`, keeping history readers and one-shot fetches. */ + refuseLive: (producer: Producer, edge: bigint, error: Error) => void; /** * A fetch's group `sequence`, before it reads: `true` once it is cached or asked of a fill, * written yet or not, as the Rust `fetch_group` queues one; `false` with no handler to ask; @@ -862,6 +868,15 @@ export const hooks: { groupChanged: () => { throw new Error("track.ts not loaded"); }, + cacheTrack: () => { + throw new Error("track.ts not loaded"); + }, + refuseLive: () => { + throw new Error("track.ts not loaded"); + }, + subscribing: () => { + throw new Error("track module not loaded"); + }, fetchBudget: () => { throw new Error("track.ts not loaded"); }, diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 74c246c66f7..ac1237ceaa2 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -47,6 +47,7 @@ import { rescale } from "./rescale.ts"; import { Timescale, type Timestamp } from "./time.ts"; import { checkSequence, max, min, UNBOUNDED } from "./util/sequence.ts"; import { + bounded, type Holdings, hasTime, isWire, @@ -653,6 +654,8 @@ class TrackState { dropsAnchors = new Signal(false); // Frames wake the sequence cursor only while an owed hole holds it. waitingOrdered = false; + // One-shot group reads need FETCH, even when metadata has not reported a live edge. + fetch = false; // Bumped when a wire subscription declares or resolves where its live feed starts, so a // publisher holding its first group for that answer wakes. startChanged = new Signal(0); @@ -761,7 +764,9 @@ let reofferCached: ( let reofferSequences: (producer: Producer, sink: TrackState, sequences: readonly bigint[]) => void; // Whether a group may still land on the producer's live feed; assigned in its static block. let mayLand: (producer: Producer, land: Land) => boolean; +let owedRuns: (producer: Producer, start: bigint, end: bigint) => Iterable; let owedGroup: (producer: Producer, start: bigint, end: bigint) => { sequence: bigint; folds?: number } | undefined; +let fillEdge: (producer: Producer) => bigint | undefined; let sourceStart: (producer: Producer) => bigint | "pending" | undefined; // The producer's checked group starts, sorted by sequence; assigned in its static block. let startsOf: (producer: Producer) => readonly Start[]; @@ -869,6 +874,7 @@ export class Producer { // One past the highest group or datagram this producer received, like the Rust // `max_sequence`, and where the next append lands. #received = 0n; + #subscribing = new Signal(false); // Recently written source groups, retained for replay to late subscribers and // pruned once idle for longer than the cache window. Each entry tracks the mirror @@ -951,6 +957,27 @@ export class Producer { } static { + hooks.subscribing = (producer) => producer.#subscribing; + hooks.cacheTrack = (producer) => { + producer.#subscribed = false; + producer.#served = undefined; + producer.#tailPending = false; + for (const state of [producer.#state, ...producer.#sinks]) state.owed.set(false); + producer.#settle(); + }; + hooks.refuseLive = (producer, edge, error) => { + for (const sink of producer.#sinks) { + if (sink.fetch) continue; + const ranges = sink.update.peek()?.ranges ?? [{}]; + const span = wireSpan(ranges); + if (bounded(ranges) && span.end !== undefined && span.end <= edge) continue; + closeTrackState(sink, error); + } + }; + fillEdge = (producer) => + !producer.#subscribed && producer.#state.info.peek()?.ietf !== undefined + ? producer.#state.largest.peek()?.group + : undefined; hooks.receiveTrack = (producer) => { producer.#subscribed = true; }; @@ -1019,27 +1046,38 @@ export class Producer { } }; mayLand = (producer, land) => producer.#landable(land) !== undefined; - owedGroup = (producer, start, end) => { - if (!producer.#subscribed) return undefined; + owedRuns = (producer, start, end) => { + if (!producer.#subscribed) return []; const feed = producer.#feedStart(); const floor = feed === "pending" ? producer.#served?.requested : feed; - return producer.#landable({ + return producer.#landing({ start: max(start, floor ?? 0n), end, floor: floor !== undefined, grace: producer.#grace(), }); }; + owedGroup = (producer, start, end) => { + for (const run of owedRuns(producer, start, end)) return { sequence: run.start, folds: run.folds }; + return undefined; + }; } // The first group that may still land from the live feed, by the rule a pending tail is judged // by; see {@link Land}. Mirrors the Rust `TrackState::may_land`. #landable(land: Land): { sequence: bigint; folds?: number } | undefined { - if (this.#state.closed.peek() !== undefined) return undefined; + for (const run of this.#landing(land)) { + if (!land.delivered?.covers(run.start, run.end)) return { sequence: run.start, folds: run.folds }; + } + return undefined; + } + + *#landing(land: Land): Generator { + if (this.#state.closed.peek() !== undefined) return; let start = land.start; if (!land.floor) { const first = this.#arrivals.first(); - if (first === undefined) return undefined; + if (first === undefined) return; start = max(start, first); } start = max(start, this.#served?.start ?? 0n); @@ -1052,10 +1090,9 @@ export class Producer { const folds = gap.since === undefined ? undefined : gap.since + limit; if (folds !== undefined && at >= folds) continue; for (const open of runGaps(drops, gap.start, gap.end)) { - if (!land.delivered?.covers(open.start, open.end)) return { sequence: open.start, folds }; + yield { ...open, folds }; } } - return undefined; } // Record `[start, end)` given up with `error`. An upstream drop (`open`) also ends the @@ -1300,6 +1337,7 @@ export class Producer { // applies its own, as each reader's budget does, so the demand asked upstream is the same // before and after the info lands. #updateSubscription(): void { + this.#subscribing.set([...this.#sinks].some((sink) => !sink.fetch && sink.closed.peek() === undefined)); const combined = combineSubscriptions(this.#sinks); this.#settle(); this.#state.update.set(combined); @@ -2581,8 +2619,16 @@ export class Subscriber { // the Rust judge skips its track's datagram buffer; the Rust fetch names it // `NotFetchable`. const gone = [...this.#state.drops.peek()]; + // Reordered streams still owed by the live feed are not cache misses. + for (const run of owedRuns(producer, start, end)) { + insertRun(gone, run.start, run.end); + if (run.folds !== undefined) this.#wakeAt(run.folds); + } for (const sequence of heldDatagrams(producer)) insertRun(gone, sequence, sequence + 1n); - for (const run of this.#wants(start, min(end, this.#state.received.peek()))) { + for (const run of this.#wants( + start, + min(end, max(this.#state.received.peek(), (fillEdge(producer) ?? -1n) + 1n)), + )) { for (const open of runGaps(gone, run.start, run.end)) { let next = open.start; for (let i = timelineIndex(timeline, open.start); i < timeline.length; i++) { @@ -2710,9 +2756,9 @@ export class Subscriber { // A fill still owes a group it reads. if (this.#askFills(this.#window.floor() ?? 0n, end) !== undefined) return false; const held = this.#state.groups.peek(); - const received = this.#state.received.peek(); - const drops = this.#state.drops.peek(); const producer = this.#state.producer; + const received = max(this.#state.received.peek(), ((producer && fillEdge(producer)) ?? -1n) + 1n); + const drops = this.#state.drops.peek(); for (const run of this.#window.runs(this.#window.floor() ?? 0n, end)) { let gaps = [run]; for (const delivered of taken) gaps = gaps.flatMap((gap) => delivered.gaps(gap.start, gap.end)); @@ -2925,6 +2971,9 @@ export class Subscriber { hooks.askFetch = (subscriber, sequence) => subscriber.#askFetch(sequence); hooks.fetchBudget = (subscriber, maxDelay) => { subscriber.#fetchBudget = maxDelay ?? Number.POSITIVE_INFINITY; + subscriber.#state.fetch = true; + const update = subscriber.#state.update.peek(); + if (update) subscriber.#state.update.set(update, true); }; // The sequence cursor lives here (it shares the buffer and the drift anchor with // the arrival cursor); `Ordered` is the handle that reaches it. diff --git a/quest/m1/README.md b/quest/m1/README.md index 4743bb5f2f9..23202535123 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -171,7 +171,6 @@ their end report, so an embedder's drain alert pages on real cuts. - [crates.io trusts the workflow](/quest/m1/crates-trusted-publisher.md) - maintainer action: every published crate gets a trusted publisher for `release-rs.yml` - [Rust releases publish through OIDC](/quest/m1/release-rs-oidc.md) - release-plz publishes with trusted publishing and `CARGO_REGISTRY_TOKEN` is revoked - [Main is no slower than release](/quest/m1/release-perf/README.md) - every benchmark where main is slower than release is fixed or accepted with a reason, checked by a final paired run before the cut -- [Interop coverage](/quest/m1/interop-coverage/README.md) - the riskiest moq-transport features moq-net exposes get cross-implementation tests over drafts 18, 20, and 22 - [Calibrate the paired nightly benchmarks](/quest/m1/bench-ci.md) - A/A and deliberately slowed Nightly runs set the paired comparison's threshold, so its regression alerts can be trusted - [Unbounded groups](/quest/m1/unbounded-groups/README.md) - groups and frames lose their size caps; a group keeps a 32 MB tail, and readers behind it replay through paced flights or refresh - [Poll-first async](/quest/m1/poll-first/README.md) - library crates compile no coroutines by default: async wrappers are generated from `poll_*` and gated behind an `async` feature diff --git a/quest/m1/ietf-drop/README.md b/quest/m1/ietf-drop/README.md index 3f91d98eb92..55b65ae06b8 100644 --- a/quest/m1/ietf-drop/README.md +++ b/quest/m1/ietf-drop/README.md @@ -44,7 +44,3 @@ protocol implementation or release work remains here. ## Waiting - englishm/moq-interop-runner: review and merge [englishm/moq-interop-runner#183](https://github.com/englishm/moq-interop-runner/pull/183); verify both registry entries and README rows list drafts 18, 20, and 22 before deleting this quest - -## Related - -- [Interop coverage](/quest/m1/interop-coverage/README.md) - cross-implementation cases target drafts 18, 20, and 22 diff --git a/quest/m1/interop-coverage/README.md b/quest/m1/interop-coverage/README.md deleted file mode 100644 index f5a07d7c1d9..00000000000 --- a/quest/m1/interop-coverage/README.md +++ /dev/null @@ -1,58 +0,0 @@ -# Interop coverage for moq-transport - -## Goal - -The moq-transport features moq-net exposes with the most interop bug -history have cross-implementation tests: Rust, JS, and the bindings exchange -hang media and exercise subgroup data, namespace discovery, late joins, -FETCH, GOAWAY, TRACK_STATUS, and datagrams over drafts 18, 20, and 22. Our -upstream runner clients implement the runner's existing cases for those -features. - -Boundaries: - -- Only what moq-net can do. Subgroups other than 0, rendezvous, and - per-request authorization tokens are not exposed, so they are out of - scope, and runner cells that need them stay red by design. -- PUBLISH (push) and outbound forward=0 are out of scope (decided - 2026-10-25, maintainer, reversing the 2026-10-22 decision): no child - quest owned them, and the cases above carry the interop bug history. -- Media stays hang. No MSF catalog in our runner clients. -- New cases live in-tree (`test/interop`), not in the runner's spec. -- Not covered: mid-subscription updates (`Subscription::update` in Rust and - JS, sent as SUBSCRIBE_UPDATE or REQUEST_UPDATE) for priority, max age, - NEW_GROUP_REQUEST, and group order. The maintainer did not pick them on - 2026-10-21; plan them separately if their bug history grows. - -## Plan - -Decided 2026-11-02 (maintainer, follow-up planning after the 2026-11-02 /quest-complete run): draft 20 joins the covered drafts, since it came back in -[moq-dev/moq#7190](https://github.com/moq-dev/moq/pull/7190) and every published moq-dev client and relay already offers it (verified on -[moq-dev/moq#6972](https://github.com/moq-dev/moq/pull/6972)). - - -Decided with the maintainer on 2026-10-21, from an inventory of the -coverage on `main` at `3ab6cbf81d`. - -- `just test interop --versions ietf` runs the matrix on each supported draft; - every new case runs on it. Beyond it, moq-transport is covered by - in-process codec tests (`rs/moq-net/src/test_interop.rs`), the public - relays page (published clients), and the Cloudflare and Moxygen smoke - lanes. -- Drafts 18, 20, and 22 only, nightly. Protocol removal and draft-20 - restoration have landed; only the - [runner registry correction](/quest/m1/ietf-drop/README.md) remains. - The request-limits case went with drafts 14 and 16, the only ones with - MAX_REQUEST_ID. -- Case ranking follows interop bug history: subgroup data and namespace - discovery, then late join and FETCH, then GOAWAY, TRACK_STATUS, - and datagrams. - -## Required - -- [JS subscribers FETCH](/quest/m1/interop-coverage/js-ietf-fetch.md) - `@moq/net` fetches past groups over moq-transport, and joins the CLI FETCH round - -## Related - -- [Runner client bump](/quest/m2/release/runner-client-bump.md) - after the cut publishes #5083, the runner's Rust client stops stamping every object -- [Interop audit](/quest/m2/audit-interop.md) - classifies the red cells these cases produce diff --git a/quest/m1/interop-coverage/js-ietf-fetch.md b/quest/m1/interop-coverage/js-ietf-fetch.md deleted file mode 100644 index dcaf9307ffe..00000000000 --- a/quest/m1/interop-coverage/js-ietf-fetch.md +++ /dev/null @@ -1,27 +0,0 @@ -# [M] JS subscribers FETCH over moq-transport - -## Goal - -`@moq/net`'s moq-transport subscriber fetches a past group with a standalone -FETCH, so `track.Consumer.fetchGroup()` works on moq-transport. JS subscribers -join the CLI FETCH interop round beside Rust. - -## Plan - -Found by [moq-dev/moq#7015](https://github.com/moq-dev/moq/pull/7015). Today -`track.Consumer.fetchGroup()` is rejected on moq-transport. The mid-group -join already recovers the current group's head, with a joining FETCH on -draft 18 or a fill on draft 22 -([moq-dev/moq#7089](https://github.com/moq-dev/moq/pull/7089)), and its -fetch-stream decoder and head stitching are what a standalone FETCH reuses. - -Decided 2026-10-28 (maintainer): one [M] quest in m1, after -JS ranges ([moq-dev/moq#7696](https://github.com/moq-dev/moq/pull/7696)), which turns `fetchGroup` into -a one-range subscription. Map a past range to a standalone FETCH, the way -Rust's subscriber does. Then add the native JS runtimes as subscribers to the -`run_fetch_round` cells in the tail suite. Our publishers answer only one -group ([moq-dev/moq#7204](https://github.com/moq-dev/moq/pull/7204), 2026-10-28), so a -past-range FETCH against them is refused. Test ranges against a third-party -publisher, and one group against ours. - -Public API: none beyond JS ranges' surface. Wire: none. diff --git a/quest/m2/audit-interop.md b/quest/m2/audit-interop.md index 3fd93f7f433..d16ddae4d68 100644 --- a/quest/m2/audit-interop.md +++ b/quest/m2/audit-interop.md @@ -191,4 +191,3 @@ cells of the 2026-10-31 nightly. Classify it. - [Runner client bump](/quest/m2/release/runner-client-bump.md) - the runner's Rust client moves to the cut and its API - [Runner teardown](/quest/m3/interop-runner-approval.md) - the runner's moq.dev clients move to the withdrawing `close()` once it ships -- [Interop coverage](/quest/m1/interop-coverage/README.md) - the runner cases whose red cells this audit classifies diff --git a/quest/m2/ietf-hole-marker.md b/quest/m2/ietf-hole-marker.md index 8a81a710bee..337da3f190f 100644 --- a/quest/m2/ietf-hole-marker.md +++ b/quest/m2/ietf-hole-marker.md @@ -65,7 +65,7 @@ Open, for this quest's interview: - How a publisher answering a range walks the drop set's runs in group order, and where FETCH_OK's End Location falls. -Test across Rust and JS on drafts 18, 20, and 22, with and without the extension. Draft 20 added 2026-11-03 (maintainer, /quest-plan of the follow-ups from [moq-dev/moq#7704](https://github.com/moq-dev/moq/pull/7704)), since draft 20 joined the covered drafts in [interop coverage](/quest/m1/interop-coverage/README.md). +Test across Rust and JS on drafts 18, 20, and 22, with and without the extension. Draft 20 added 2026-11-03 (maintainer, /quest-plan of the follow-ups from [moq-dev/moq#7704](https://github.com/moq-dev/moq/pull/7704)), since draft 20 joined the covered drafts in [interop coverage](https://github.com/moq-dev/moq/blob/acef90cf0425d9f0e4e699759cede07adfb2ecfb/quest/m1/interop-coverage/README.md). A range over a copy whose holes are all proven is answered with `0x8C` runs. A range with an unknown hole is still refused. Regression: an upstream reports G stale for a narrow subscriber while still holding G; a wider diff --git a/quest/m2/publish-done-codes.md b/quest/m2/publish-done-codes.md index 9b20315ae71..a884eecc519 100644 --- a/quest/m2/publish-done-codes.md +++ b/quest/m2/publish-done-codes.md @@ -8,6 +8,6 @@ A moq-transport PUBLISH_DONE carries GOING_AWAY (0x4), TOO_FAR_BEHIND (0x5), and Follow-up of [moq-dev/moq#7396](https://github.com/moq-dev/moq/pull/7396), which did this for MALFORMED_TRACK (0x12). The stream-reset codes already map both ways in `rs/moq-net/src/ietf/error.rs:30-75`; only PUBLISH_DONE doesn't use them. -Decided 2026-10-30 (maintainer, follow-up planning after the /quest-afk run). Reuse #7396's path: the publisher picks the PUBLISH_DONE code from the same error-to-code mapping that resets the track's streams, and the subscriber reads it back as the peer's stream code. Cover drafts 18, 20, and 22, both bindings, and JS, each test failing without its fix. Draft 20 added 2026-11-03 (maintainer, /quest-plan of the follow-ups from [moq-dev/moq#7704](https://github.com/moq-dev/moq/pull/7704)), since draft 20 joined the covered drafts in [interop coverage](/quest/m1/interop-coverage/README.md). +Decided 2026-10-30 (maintainer, follow-up planning after the /quest-afk run). Reuse #7396's path: the publisher picks the PUBLISH_DONE code from the same error-to-code mapping that resets the track's streams, and the subscriber reads it back as the peer's stream code. Cover drafts 18, 20, and 22, both bindings, and JS, each test failing without its fix. Draft 20 added 2026-11-03 (maintainer, /quest-plan of the follow-ups from [moq-dev/moq#7704](https://github.com/moq-dev/moq/pull/7704)), since draft 20 joined the covered drafts in [interop coverage](https://github.com/moq-dev/moq/blob/acef90cf0425d9f0e4e699759cede07adfb2ecfb/quest/m1/interop-coverage/README.md). Public API: none. Wire: existing registered codes only. diff --git a/test/ietf-conformance/README.md b/test/ietf-conformance/README.md index a2e81df6593..1fb68017941 100644 --- a/test/ietf-conformance/README.md +++ b/test/ietf-conformance/README.md @@ -35,6 +35,19 @@ reviewing the selected cases, rebasing the profile patch, and rerunning the full matrix. The build recipe adds the client's missing Folly ManualExecutor link dependency and patches dependency script interpreters for Nix. +## Native JS FETCH ranges + +The same run also has Node and Bun read multi-group past ranges directly from the pinned +Moxygen publisher on draft 18. A separate live connection advances the publisher beyond +the requested groups, so the FETCH connection shares none of its cache. Both readers check +the group sequences, every object and its payload, overlapping ranges, missing groups, and +bounded EOF. First-party publishers serve only one group per FETCH and are covered by the +interop tail matrix instead. + +Run only this lane with `just test ietf-conformance --fetch`. It also runs in the full +nightly conformance recipe. The fixture pins draft 18 at SETUP because this Moxygen revision's +HTTP/3 listener predates WebTransport subprotocol negotiation. + ## Conformance profile The client runs under a relay conformance profile, applied by diff --git a/test/ietf-conformance/run.sh b/test/ietf-conformance/run.sh index 6a8023d7429..a5fc3b3e5d2 100755 --- a/test/ietf-conformance/run.sh +++ b/test/ietf-conformance/run.sh @@ -7,6 +7,16 @@ if [[ "${GITHUB_ACTIONS:-}" != true && "${LOCAL_SUPERVISED:-}" != 1 ]]; then exec bash "$(dirname "$0")/../../sh/local.sh" bash "$0" "$@" fi +fetch_only=0 +case "${1:-}" in + "") ;; + --fetch) fetch_only=1 ;; + *) + echo "usage: run.sh [--fetch]" >&2 + exit 2 + ;; +esac + CONFORMANCE_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) WORKSPACE=$(cd "$CONFORMANCE_DIR/../.." && pwd) # shellcheck source-path=SCRIPTDIR source=../lib/harness.sh @@ -20,7 +30,8 @@ relay="$WORKSPACE/${CARGO_TARGET_DIR:-target}/debug/moq-relay" # Cargo accepts an absolute target directory too. [[ "${CARGO_TARGET_DIR:-}" != /* ]] || relay="$CARGO_TARGET_DIR/debug/moq-relay" -harness_begin ietf-conformance "$(harness_env HARNESS_RESULTS)just test ietf-conformance" +bun install --frozen-lockfile +harness_begin ietf-conformance "$(harness_env HARNESS_RESULTS)just test ietf-conformance$(harness_argv "$@")" python3 "$CONFORMANCE_DIR/prepare.py" "$package/share/moxygen/conformance_test.sh" "$HARNESS_RUN/conformance.sh" # LibreSSL in the dev shell has no system-wide openssl.cnf. cat >"$HARNESS_RUN/openssl.cnf" <<'EOF' @@ -112,10 +123,34 @@ run_suite() { fi } +# Fetch directly from Moxygen: first-party publishers intentionally serve one group only. +run_js_ranges() { + local runtime status + for runtime in node bun; do + status=0 + local command=(bun) + [[ "$runtime" != node ]] || command=(node --import tsx) + harness_spawn "fetch-$runtime" "$HARNESS_RUN/fetch-$runtime.log" \ + timeout -k 3 30 "${command[@]}" "$WORKSPACE/test/interop/clients/js-native/ranges.ts" \ + "https://127.0.0.1:$publisher_port/test" "$HARNESS_RUN/cert.pem" + harness_wait "$HARNESS_PID" || status=$? + cat "$HARNESS_RUN/fetch-$runtime.log" + local cell=(suite=ietf-conformance case="JS FETCH ranges" version=moq-transport-18 transport=quic publisher=moxygen subscriber="js-native-$runtime" log="$HARNESS_RUN/fetch-$runtime.log") + if ((status == 0)) && publisher_healthy; then + harness_result pass "${cell[@]}" + else + harness_result fail "${cell[@]}" + failed=1 + fi + done +} + # Moxygen's MoQVersions.h uses moqt-20-meta-02 for draft 20; use shared moqt-18. failed=0 version=18 -for topology in single cluster; do +topologies=(single cluster) +((fetch_only == 0)) || topologies=(single) +for topology in "${topologies[@]}"; do label="$topology-$version" start_relay "$label-origin" "$version" ingress="$HARNESS_PORT" @@ -137,9 +172,10 @@ for topology in single cluster; do namespace_ready # A direct-origin control distinguishes suite/pin failures from relay behavior. if [[ "$topology" == single ]]; then - run_suite "reference-$version" "https://127.0.0.1:$publisher_port/test" + run_js_ranges + ((fetch_only == 1)) || run_suite "reference-$version" "https://127.0.0.1:$publisher_port/test" fi - run_suite "$label" "https://127.0.0.1:$egress/" + ((fetch_only == 1)) || run_suite "$label" "https://127.0.0.1:$egress/" harness_reap "$publisher_pid" [[ -z "$egress_pid" ]] || harness_reap "$egress_pid" harness_reap "$ingress_pid" diff --git a/test/interop/README.md b/test/interop/README.md index 0546de5126f..713a51cf5c7 100644 --- a/test/interop/README.md +++ b/test/interop/README.md @@ -51,10 +51,10 @@ The normal run also checks a finite raw track through the relay. Its publisher announces end 4 before writing four 256 KiB groups; the reader verifies every byte, exactly groups 0 through 3, and a clean end at 4. Rust-to-Rust always runs. Selecting native JS adds both Rust-to-JS and JS-to-Rust under that runtime, so `--all` covers -Node and Bun. The current CLI also FETCHes group 1 of a finished track from the Rust -publisher and each selected native JS runtime, requires exactly its three objects, each -with its own bytes, and closes under the relay idle-out check. Each native JS runtime also serves -that FETCH from history (`CLI FETCH history`): its publisher writes only group 3 live and answers +Node and Bun. The FETCH round crosses Rust and each selected native JS runtime as both +publisher and subscriber. Each reader fetches group 1 of a finished track, requires exactly +its three objects, each with its own bytes, and closes under the relay idle-out check. Each native JS runtime also serves +that FETCH to all three subscribers from history (`CLI FETCH history`): its publisher writes only group 3 live and answers groups 0 through 2, which it never cached, from an in-memory responder on `track.requests()`. This runs on moq-lite and every supported IETF draft. diff --git a/test/interop/clients/js-native/ranges.ts b/test/interop/clients/js-native/ranges.ts new file mode 100644 index 00000000000..8b7363f3572 --- /dev/null +++ b/test/interop/clients/js-native/ranges.ts @@ -0,0 +1,95 @@ +/** Standalone multi-group FETCH against the pinned Moxygen publisher. @module */ +import assert from "node:assert/strict"; +import { readFileSync } from "node:fs"; +import * as Moq from "@moq/net"; +import WebTransport from "@moq/web-transport"; +import { offered } from "../../../../js/net/src/connection/extensions.ts"; +import { exchangeSetup } from "../../../../js/net/src/connection/handshake.ts"; +import { Connection } from "../../../../js/net/src/ietf/connection.ts"; +import { Version } from "../../../../js/net/src/ietf/version.ts"; +import { wireOf } from "../../../../js/net/src/wire.ts"; + +console.debug = console.error; +console.info = console.error; +const [address, certificate] = process.argv.slice(2); +assert(address && certificate, "usage: ranges.ts URL CERTIFICATE"); +const url = new URL(address); +const cert = readFileSync(certificate, "utf8"); +// MoQTest's 16 namespace fields: subgroup zero, groups 0,2,...,1000, three objects per +// group, 1024/513-byte payloads, 10 ms between objects, and no status markers or properties. +const path = Moq.Path.from("moq-test-00/0/0/0/1000/2/3/1024/513/10/2/1/0/-1/-1/0"); +async function connect() { + const transport = new WebTransport(url, { + protocols: ["moqt-18"], + serverCertificateHashes: [{ algorithm: "sha-256", value: await Moq.Connection.certificateHash(cert) }], + }); + await transport.ready; + // Moxygen's pinned HTTP/3 server predates subprotocol negotiation. This fixture's + // server offers draft 18 alone, so exchange that SETUP directly on its WebTransport. + const extensions = offered({ auth: false, solicit: false }); + const setup = await exchangeSetup(transport, Version.DRAFT_18, "interop-ranges", { client: true, extensions }); + return new Connection({ url, quic: transport, version: Version.DRAFT_18, client: true, extensions, ...setup }); +} + +function payload(bytes: Uint8Array, size: number) { + assert.equal(bytes.byteLength, size, "wrong object size"); + for (const [offset, byte] of bytes.entries()) + assert.equal(byte, offset % 256 === 0 ? (offset >> 8) & 255 : 0x74, `corrupt object at ${offset}`); +} + +const live = await connect(); +const source = wireOf(live).consume(path); +const holder = source.track("test").subscribe({ delay: { max: null } }); +try { + // Give TRACK_STATUS a real live edge without sharing this connection's cache with FETCH. + for (;;) { + const group = await holder.recvGroup(); + assert(group, "publisher ended before its past range existed"); + while (await group.readFrame()) {} + group.close(); + if (group.sequence >= 8n) break; + } + const history = await connect(); + const broadcast = wireOf(history).consume(path); + try { + for (const [start, end, expected] of [ + [2n, 7n, [2n, 4n, 6n]], + [0n, 5n, [0n, 2n, 4n]], + ] as const) { + const reader = broadcast.track("test").subscribe({ + ranges: [{ ...(start === 0n ? {} : { start: { absolute: start } }), end: { absolute: end } }], + delay: { max: null }, + order: "ascending", + }); + try { + const seen: bigint[] = []; + const ordered = reader.ordered(); + for (let group = await ordered.nextGroup(); group; group = await ordered.nextGroup()) { + seen.push(group.sequence); + try { + for (const size of [1024, 513, 513]) { + const frame = await group.readFrame(); + assert(frame, "FETCH omitted an object"); + payload(frame.payload, size); + } + assert.equal(await group.readFrame(), undefined, "FETCH repeated an object"); + } finally { + group.close(); + } + } + assert.deepEqual(seen, expected, "FETCH omitted, repeated, or renumbered a group"); + } finally { + reader.close(); + } + } + console.error("moxygen ranges clean groups=2,4,6;0,2,4 frames=18"); + } finally { + broadcast.close(); + await history.close(); + } +} finally { + holder.close(); + source.close(); + await live.close(); +} +process.exit(0); diff --git a/test/interop/clients/js-native/tail.ts b/test/interop/clients/js-native/tail.ts index 2219454d63b..a4dccd60e8b 100644 --- a/test/interop/clients/js-native/tail.ts +++ b/test/interop/clients/js-native/tail.ts @@ -1,7 +1,7 @@ /** * Finite raw-track interop: publish until the harness acknowledges, or read through clean EOF. * - * `drop` cuts group 1. `join` reads from whichever group the subscription starts at: a + * `fetch` reads one past group. `drop` cuts group 1. `join` reads from whichever group the subscription starts at: a * moq-transport subscription starts at the largest group rather than at the range asked for, so * it must read a contiguous run of groups through the last one. `compress` publishes a compressed * track with several frames, one empty, in each group. `cached` publishes every group before @@ -34,26 +34,27 @@ const status = mode === "status"; const cached = mode === "cached"; const history = mode === "history"; const late = mode === "late"; +const fetch = mode === "fetch"; if ( !url || !path || !["publish", "subscribe"].includes(role) || - ![undefined, "drop", "join", "compress", "status", "cached", "history", "late"].includes(mode) + ![undefined, "drop", "join", "compress", "status", "cached", "history", "late", "fetch"].includes(mode) ) { throw new Error( - "usage: tail.ts publish|subscribe URL BROADCAST [VERSION [drop|join|compress|status|cached|history|late]]", + "usage: tail.ts publish|subscribe URL BROADCAST [VERSION [drop|join|compress|status|cached|history|late|fetch]]", ); } /** * The frames of each group. A compressed track spans several frames, one empty, per window. - * `cached`, `history`, and `late` give every frame its own bytes, so a FETCH or join that drops, repeats, or + * `cached`, `history`, `fetch`, and `late` give every frame its own bytes, so a FETCH or join that drops, repeats, or * reorders one fails. */ function frames(sequence: number): Uint8Array[] { if (compress) return [new Uint8Array(BYTES / 2).fill(sequence), new Uint8Array(0), new Uint8Array(BYTES / 2).fill(sequence)]; - if (cached || history || late) + if (cached || history || late || fetch) return [0, 1, 2].map((index) => new Uint8Array(BYTES / 4).fill(sequence * 16 + index)); return [new Uint8Array(BYTES).fill(sequence)]; } @@ -162,52 +163,62 @@ try { await request.active.changed(); broadcast = request.active.peek(); } - // `late` names no start, joining the latest group as a player does: group 0, whose - // head predates the subscription. - const track = broadcast.track("tail").subscribe({ - delay: { max: Milli(drop ? 60_000 : 5000) }, - ...(late ? {} : { ranges: [{ start: { absolute: 0n } }] }), - }); - if (compress) assert.equal((await track.info()).compress, version === "moq-lite-07-wip"); - const seen: number[] = []; - try { - if (late) { - for (let sequence = 0; sequence < LATE_GROUPS; sequence++) { - const group = await track.recvGroup(); - assert(group, "missing group"); - try { - assert.equal(group.sequence, BigInt(sequence), "joined the wrong group"); - await read(group); - } finally { - group.close(); + if (fetch) { + const group = await broadcast.track("tail").fetchGroup(1n); + try { + await read(group); + } finally { + group.close(); + } + console.error("fetch clean group=1 frames=3 bytes=196608"); + } else { + // `late` names no start, joining the latest group as a player does: group 0, whose + // head predates the subscription. + const track = broadcast.track("tail").subscribe({ + delay: { max: Milli(drop ? 60_000 : 5000) }, + ...(late ? {} : { ranges: [{ start: { absolute: 0n } }] }), + }); + if (compress) assert.equal((await track.info()).compress, version === "moq-lite-07-wip"); + const seen: number[] = []; + try { + if (late) { + for (let sequence = 0; sequence < LATE_GROUPS; sequence++) { + const group = await track.recvGroup(); + assert(group, "missing group"); + try { + assert.equal(group.sequence, BigInt(sequence), "joined the wrong group"); + await read(group); + } finally { + group.close(); + } } - } - assert.equal(await track.recvGroup(), undefined, "extra group"); - assert.deepEqual(await track.finished(), { group: BigInt(LATE_GROUPS), frame: 0 }); - console.error("late join clean groups=0,1 frames=6"); - } else { - for (;;) { - const group = await track.recvGroup(); - if (!group) break; - try { - assert(group.sequence < GROUPS, `unexpected group ${group.sequence}`); - await read(group); - seen.push(Number(group.sequence)); - } finally { - group.close(); + assert.equal(await track.recvGroup(), undefined, "extra group"); + assert.deepEqual(await track.finished(), { group: BigInt(LATE_GROUPS), frame: 0 }); + console.error("late join clean groups=0,1 frames=6"); + } else { + for (;;) { + const group = await track.recvGroup(); + if (!group) break; + try { + assert(group.sequence < GROUPS, `unexpected group ${group.sequence}`); + await read(group); + seen.push(Number(group.sequence)); + } finally { + group.close(); + } } + seen.sort((a, b) => a - b); + const first = join ? (seen[0] ?? 0) : 0; + const all = Array.from({ length: GROUPS - first }, (_, i) => first + i); + assert.deepEqual(seen, drop ? [0, 2, 3] : all); + assert.deepEqual(await track.finished(), { group: BigInt(GROUPS), frame: 0 }); + if (join) console.error(`tail join clean end=4 groups=${seen.join(",")}`); + else if (drop) console.error("tail drop clean end=4 groups=0,2,3 bytes=786432"); + else console.error("tail clean end=4 groups=0,1,2,3 bytes=1048576"); } - seen.sort((a, b) => a - b); - const first = join ? (seen[0] ?? 0) : 0; - const all = Array.from({ length: GROUPS - first }, (_, i) => first + i); - assert.deepEqual(seen, drop ? [0, 2, 3] : all); - assert.deepEqual(await track.finished(), { group: BigInt(GROUPS), frame: 0 }); - if (join) console.error(`tail join clean end=4 groups=${seen.join(",")}`); - else if (drop) console.error("tail drop clean end=4 groups=0,2,3 bytes=786432"); - else console.error("tail clean end=4 groups=0,1,2,3 bytes=1048576"); + } finally { + track.close(); } - } finally { - track.close(); } } finally { request.close(); diff --git a/test/interop/interop.sh b/test/interop/interop.sh index 8dc01b1080f..b523183f21c 100755 --- a/test/interop/interop.sh +++ b/test/interop/interop.sh @@ -498,13 +498,21 @@ plan_pass() { done fi [[ "$FINISH_ONLY" -eq 0 ]] || return 0 - record "not run" case="CLI FETCH" publisher=rust subscriber=rust + for pub in rust js-native-node js-native-bun; do + [[ "$pub" == rust ]] || needs "$pub" || continue + for sub in rust js-native-node js-native-bun; do + [[ "$sub" == rust ]] || needs "$sub" || continue + record "not run" case="CLI FETCH" publisher="$pub" subscriber="$sub" + done + done record "not run" case="late join" publisher=rust subscriber=rust record "not run" case=tail publisher=rust subscriber=rust for runtime in js-native-node js-native-bun; do needs "$runtime" || continue - record "not run" case="CLI FETCH" publisher="$runtime" subscriber=rust - record "not run" case="CLI FETCH history" publisher="$runtime" subscriber=rust + for sub in rust js-native-node js-native-bun; do + [[ "$sub" == rust ]] || needs "$sub" || continue + record "not run" case="CLI FETCH history" publisher="$runtime" subscriber="$sub" + done record "not run" case="late join" publisher=rust subscriber="$runtime" record "not run" case="late join" publisher="$runtime" subscriber=rust record "not run" case=tail publisher=rust subscriber="$runtime" @@ -2030,18 +2038,18 @@ run_late_pair() { rm "$fifo" "$heads" } -# This checkout's CLI FETCHes a past group of a finished track, under the same relay close check as +# Rust and native JS FETCH a past group of a finished track, under the same relay close check as # live readers. Every object has its own bytes, so a FETCH that drops, repeats, or reorders one fails. # The `history` mode (native JS only) serves the group from the publisher's request handler: it # was never in its cache. run_fetch_round() { - local pub="$1" mode="${2:-cached}" pid from version - local name="fetch-$1${2:+-$2}" fifo="$LOGS/fetch-ack" broadcast="fetch-$1-$$-$RANDOM" - local label="CLI FETCH${2:+ $2}" + local pub="$1" sub="$2" mode="${3:-cached}" pid from version + local name="fetch-$1-$2${3:+-$3}" fifo="$LOGS/fetch-ack" broadcast="fetch-$1-$$-$RANDOM" + local label="CLI FETCH${3:+ $3}" local started=$SECONDS why="" - local cell=(case="$label" publisher="$pub" subscriber=rust log="$LOGS/$name-pub.log") - if is_broken "$pub"; then - echo " FAIL $label $pub (client unavailable)" + local cell=(case="$label" publisher="$pub" subscriber="$sub" log="$LOGS/$name-sub.log") + if is_broken "$pub" || is_broken "$sub"; then + echo " FAIL $label $pub -> $sub (client unavailable)" record fail "${cell[@]}" detail="client unavailable" overall=1 return @@ -2052,30 +2060,22 @@ run_fetch_round() { exec 9<>"$fifo" harness_spawn "$name-pub" "$LOGS/$name-pub.log" run_tail_publisher "$fifo" "$pub" publish "$broadcast" "$(token_url --publish "$(publish_grant "$broadcast")")" "$version" "$mode" pid="$HARNESS_PID" - if timeout -k 3 "$TIMEOUT" "$MOQ" --connect "$(token_url --subscribe "$(subscribe_grant "$broadcast")")" ${version:+--connect-version "$version"} \ - --broadcast "$broadcast" fetch tail --group 1 --json >"$LOGS/$name.json" && - python3 - "$LOGS/$name.json" <<'PYCODE' -import base64, json, sys -frames = [json.loads(line) for line in open(sys.argv[1])] -assert [(frame["group"], frame["frame"], frame["size"]) for frame in frames] == [(1, index, 64 * 1024) for index in range(3)], frames -assert [base64.b64decode(frame["payload"], validate=True) for frame in frames] == [bytes([16 + index]) * (64 * 1024) for index in range(3)], "incorrect FETCH payload" -PYCODE - then + if run_fetch_subscriber "$sub" "$broadcast" "$version" "$name"; then printf 'clean end\n' >&9 harness_wait "$pid" || true if grep -qx 'tail acknowledged' "$LOGS/$name-pub.log"; then - echo " PASS $label $pub (group 1; 3 objects)" + echo " PASS $label $pub -> $sub (group 1; 3 objects)" else - echo " FAIL $label $pub (publisher did not acknowledge)" + echo " FAIL $label $pub -> $sub (publisher did not acknowledge)" why="publisher did not acknowledge" overall=1 cat "$LOGS/$name-pub.log" fi else - echo " FAIL $label $pub" + echo " FAIL $label $pub -> $sub" why="incorrect or missing FETCH payload" overall=1 - cat "$LOGS/$name-pub.log" + cat "$LOGS/$name-pub.log" "$LOGS/$name-sub.log" harness_reap "$pid" fi exec 9>&- @@ -2089,6 +2089,23 @@ PYCODE if [[ -z "$why" ]]; then record pass "${cell[@]}"; else record fail "${cell[@]}" detail="$why"; fi } +run_fetch_subscriber() { + local sub="$1" broadcast="$2" version="$3" name="$4" + if [[ "$sub" != rust ]]; then + run_tail_client "$sub" subscribe "$broadcast" "$(token_url --subscribe "$(subscribe_grant "$broadcast")")" "$version" fetch >"$LOGS/$name-sub.log" 2>&1 && + grep -qx 'fetch clean group=1 frames=3 bytes=196608' "$LOGS/$name-sub.log" + return + fi + timeout -k 3 "$TIMEOUT" "$MOQ" --connect "$(token_url --subscribe "$(subscribe_grant "$broadcast")")" ${version:+--connect-version "$version"} \ + --broadcast "$broadcast" fetch tail --group 1 --json >"$LOGS/$name.json" 2>"$LOGS/$name-sub.log" && + python3 - "$LOGS/$name.json" <<'PYCODE' +import base64, json, sys +frames = [json.loads(line) for line in open(sys.argv[1])] +assert [(frame["group"], frame["frame"], frame["size"]) for frame in frames] == [(1, index, 64 * 1024) for index in range(3)], frames +assert [base64.b64decode(frame["payload"], validate=True) for frame in frames] == [bytes([16 + index]) * (64 * 1024) for index in range(3)], "incorrect FETCH payload" +PYCODE +} + # One media.ts invocation. It reports its own verdict (a negative control passes by failing on the # assertion it names), so the exit code is the whole answer. MEDIA_RUNS=0 @@ -2416,11 +2433,20 @@ run_hls_grid() { run_tails() { local runtime version echo "=== CLI FETCH close ===" - run_fetch_round rust + local pub sub + for pub in rust js-native-node js-native-bun; do + [[ "$pub" == rust ]] || needs "$pub" || continue + for sub in rust js-native-node js-native-bun; do + [[ "$sub" == rust ]] || needs "$sub" || continue + run_fetch_round "$pub" "$sub" + done + done for runtime in js-native-node js-native-bun; do needs "$runtime" || continue - run_fetch_round "$runtime" - run_fetch_round "$runtime" history + for sub in rust js-native-node js-native-bun; do + [[ "$sub" == rust ]] || needs "$sub" || continue + run_fetch_round "$runtime" "$sub" history + done done echo "=== late join ===" run_late_pair rust rust diff --git a/test/justfile b/test/justfile index 467497e2ef5..e480dce1cb4 100644 --- a/test/justfile +++ b/test/justfile @@ -95,8 +95,9 @@ wire-compat *args: ./interop/compat.sh "$@" # Pinned Moxygen sections 1-7, subgroup zero, through one relay under the conformance profile. -ietf-conformance: - ./ietf-conformance/run.sh +[positional-arguments] +ietf-conformance *args: + ./ietf-conformance/run.sh "$@" # Installs each client from its public registry, inside test/smoke's own Nix # shell rather than this repo's toolchain. The first argument picks a smoke