From 68ad12a9af2f74ea3acd4ad1b962bd07ab8662be Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 28 Oct 2026 06:16:34 -0700 Subject: [PATCH 1/4] chore(quest): claim quest/m1/mock-reset-codes.md Quest-Claim: babad187d02fab29 From 0cb6066a615fc3aa5b8a13900f4e207cffc5bfb1 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 28 Oct 2026 06:22:23 -0700 Subject: [PATCH 2/4] fix(net): mock resets carry only codes; ietf publisher cancels with CANCELLED The mock transport handed the peer the local reset/stop reason object, which real WebTransport cannot carry. Build each stream direction from a WritableStream/ReadableStream pair whose abort and cancel hooks deliver only the stream error code (0 for a plain Error). That exposed the ietf publisher resetting group and fill streams with plain Errors (INTERNAL_ERROR on the wire) when the subscriber left or a REQUEST_UPDATE excluded the group. Reset those as CANCELLED, as the draft and the Rust publisher do. Co-Authored-By: Claude Opus 5.5 --- js/net/src/ietf/publisher.test.ts | 9 ++-- js/net/src/ietf/publisher.ts | 13 ++--- js/net/src/mock.test.ts | 46 ++++++++++++++++ js/net/src/mock.ts | 90 ++++++++++++++++++++++++++----- 4 files changed, 133 insertions(+), 25 deletions(-) create mode 100644 js/net/src/mock.test.ts diff --git a/js/net/src/ietf/publisher.test.ts b/js/net/src/ietf/publisher.test.ts index a9fe499c6c9..71d21569600 100644 --- a/js/net/src/ietf/publisher.test.ts +++ b/js/net/src/ietf/publisher.test.ts @@ -3279,10 +3279,9 @@ test("draft-22: the subscriber leaving ends a fill still reading its group", asy client.close(); // A reset discards what the peer has not acknowledged, so the objects already written - // may or may not survive it. That it ends at all, with the cancellation as its reason, - // is the whole point. + // may or may not survive it. That it ends at all, reset as CANCELLED, is the whole point. const served = await reading; - expect(served.reset?.message).toContain("unsubscribed"); + expect(served.reset).toMatchObject({ code: StreamCode.Cancel }); } finally { open.finish(); fx.close(); @@ -4368,7 +4367,7 @@ test("draft-22: narrowing gives up an excluded group's open stream", async () => await new SubscribeUpdate({ requestId: 2n, parameters }).encode(client.writer, fx.version); expect(await client.reader.u53()).toBe(RequestOk.id); await RequestOk.decode(client.reader, fx.version); - expect((await reading)?.message).toContain("range update"); + expect(await reading).toMatchObject({ code: StreamCode.Cancel }); // The publisher remains usable; only this subscription stopped its old group. open.writeFrame({ payload: new Uint8Array([2]), timestamp: Timestamp.now() }); writeGroup(track, 1); @@ -4603,7 +4602,7 @@ test.each([ expect(await client.reader.u53()).toBe(RequestOk.id); await RequestOk.decode(client.reader, fx.version); } - expect((await reading)?.message).toContain("range update"); + expect(await reading).toMatchObject({ code: StreamCode.Cancel }); open.writeFrame({ payload: new TextEncoder().encode("1.1"), timestamp: Timestamp.now() }); open.finish(); expect((await readServed(fx.uni, 2)).sort()).toEqual(["0.0", "1.0", "1.1"]); diff --git a/js/net/src/ietf/publisher.ts b/js/net/src/ietf/publisher.ts index 7cdc2a3bf8a..cbe3bcf1102 100644 --- a/js/net/src/ietf/publisher.ts +++ b/js/net/src/ietf/publisher.ts @@ -854,7 +854,7 @@ export class Publisher { const fillAbort = new AbortController(); const unsubscribed = new Promise((resolve) => { unsubscribe = () => { - fillAbort.abort(new Error("unsubscribed before the fill finished")); + fillAbort.abort(new StreamError(StreamCode.Cancel, { message: "unsubscribed before the fill finished" })); resolve(); }; }); @@ -1157,7 +1157,7 @@ export class Publisher { const narrowed = () => { if (excludes(bounds.peek(), group.sequence, slice)) { if (!finished) live.retired = true; - rangeError = new Error("group excluded by range update"); + rangeError = new StreamError(StreamCode.Cancel, { message: "group excluded by range update" }); hooks.releaseGroupReader(group, rangeError); excluded.resolve(); } @@ -1218,9 +1218,10 @@ export class Publisher { // A pause or the subscriber leaving resets the stream, even mid-write: a write // parked on flow control would otherwise hold the pause's answer indefinitely. - let left = false; + // CANCELLED, as the draft asks of a reset the subscriber caused: 0 would read as our failure. + let left: StreamError | undefined; const cancelled = unsubscribed.then(() => { - left = true; + left = new StreamError(StreamCode.Cancel, { message: "unsubscribed while serving the group" }); return undefined; }); // The subscription's budget, or an eviction of what is left to read, may give up on the @@ -1232,7 +1233,7 @@ export class Publisher { // Writing the group stops for the pause too; the drain below answers it without a reset. const send = async (op: () => Promise) => { await race([write(op), cancelled, excluded.promise]); - if (left) throw new Error("unsubscribed while serving the group"); + if (left) throw left; if (rangeError) throw rangeError; }; @@ -1260,7 +1261,7 @@ export class Publisher { cancelled, excluded.promise, ]); - if (left) throw new Error("unsubscribed while serving the group"); + if (left) throw left; if (rangeError) throw rangeError; if (!read) break; next = read.sequence + 1; diff --git a/js/net/src/mock.test.ts b/js/net/src/mock.test.ts new file mode 100644 index 00000000000..528796e3dec --- /dev/null +++ b/js/net/src/mock.test.ts @@ -0,0 +1,46 @@ +import { expect, test } from "bun:test"; +import { StreamCode, toTransport } from "./error.ts"; +import { createMockTransportPair } from "./mock.ts"; + +async function pair() { + const { client, server } = createMockTransportPair(); + const local = await client.createBidirectionalStream(); + const { value: remote } = await server.incomingBidirectionalStreams.getReader().read(); + if (!remote) throw new Error("no incoming stream"); + return { local, remote }; +} + +test("a reset reaches the peer as its code alone, like a real wire", async () => { + const { local, remote } = await pair(); + await local.writable.abort(new Error("local detail")); + const err = await remote.readable.getReader().read().catch((e: unknown) => e); + expect(err).toMatchObject({ source: "stream", streamErrorCode: 0 }); + expect((err as Error).message).not.toContain("local detail"); +}); + +test("a reset keeps the code its reason carries", async () => { + const { local, remote } = await pair(); + await local.writable.abort(toTransport(StreamCode.Cancel, "cancel")); + const err = await remote.readable.getReader().read().catch((e: unknown) => e); + expect(err).toMatchObject({ source: "stream", streamErrorCode: StreamCode.Cancel }); +}); + +test("a stop reaches the writer as its code alone", async () => { + const { local, remote } = await pair(); + await remote.readable.cancel(new Error("local detail")); + const err = await local.writable.getWriter().closed.catch((e: unknown) => e); + expect(err).toMatchObject({ source: "stream", streamErrorCode: 0 }); + expect((err as Error).message).not.toContain("local detail"); +}); + +test("a write parked on a full peer gives up when the peer stops", async () => { + const { local, remote } = await pair(); + const writer = local.writable.getWriter(); + // Past both queues, so the last write parks on backpressure. + const writes = Array.from({ length: 600 }, () => writer.write(new Uint8Array(1)).catch((e: unknown) => e)); + // A host task lets the writes run until one parks in flight on the full peer. + await new Promise((resolve) => setTimeout(resolve, 0)); + await remote.readable.cancel(toTransport(StreamCode.Cancel, "cancel")); + const last = await writes[writes.length - 1]; + expect(last).toMatchObject({ source: "stream", streamErrorCode: StreamCode.Cancel }); +}); diff --git a/js/net/src/mock.ts b/js/net/src/mock.ts index a10cac22910..40c27246419 100644 --- a/js/net/src/mock.ts +++ b/js/net/src/mock.ts @@ -1,7 +1,7 @@ /** * Test doubles for in-process testing. * - * Creates paired client/server transports connected via TransformStreams. + * Creates paired client/server transports connected by in-memory streams. */ import { Consumer as BroadcastConsumer } from "./broadcast.ts"; @@ -32,26 +32,88 @@ const WRITABLE_STRATEGY: QueuingStrategy = { highWaterMark: 256 }; const READABLE_STRATEGY: QueuingStrategy = { highWaterMark: 256 }; /** Every stream of a transport pair, so a session close can fail the ones still open. */ -type StreamSet = Set>; +type StreamSet = Set<(err: Error) => void>; -function newStream(streams: StreamSet): TransformStream { - return new TransformStream( +/** + * What the peer sees of a local reset or stop reason: only its code, as on a real wire. + * WebTransport sends a `WebTransportError`'s `streamErrorCode` and 0 for anything else. + */ +function onWire(reason: unknown, message: string): Error { + const { source, streamErrorCode } = (reason ?? {}) as { source?: unknown; streamErrorCode?: unknown }; + const code = source === "stream" && typeof streamErrorCode === "number" ? streamErrorCode : 0; + return Object.assign(new Error(`${message}: ${code}`), { source: "stream" as const, streamErrorCode: code }); +} + +/** + * One direction of a stream. A reset (`abort`) reaches the reader, and a stop (`cancel`) the + * writer, carrying only the code. A TransformStream would hand the peer the local reason. + */ +function newStream(streams: StreamSet): { readable: ReadableStream; writable: WritableStream } { + let reader!: ReadableStreamDefaultController; + let writer!: WritableStreamDefaultController; + // Set once either side ends early, so a write waiting on a full reader gives up. + let ended = false; + let wake: (() => void) | undefined; + const resume = () => { + wake?.(); + wake = undefined; + }; + const end = () => { + ended = true; + resume(); + }; + + const readable = new ReadableStream( { start(controller) { - streams.add(controller); + reader = controller; }, - // Copy each chunk to simulate real WebTransport's kernel-boundary copy. - // Without this, Writer's scratch buffer reuse corrupts queued data. - transform(chunk, controller) { - controller.enqueue(new Uint8Array(chunk)); + pull: resume, + cancel(reason) { + writer.error(onWire(reason, "stop sending")); + end(); }, }, - WRITABLE_STRATEGY, READABLE_STRATEGY, ); + + const writable = new WritableStream( + { + start(controller) { + writer = controller; + controller.signal.addEventListener("abort", end); + }, + async write(chunk) { + // Copy each chunk to simulate real WebTransport's kernel-boundary copy. + // Without this, Writer's scratch buffer reuse corrupts queued data. + reader.enqueue(new Uint8Array(chunk)); + // Backpressure: wait for the reader to drain, as a TransformStream would. + while (!ended && (reader.desiredSize ?? 0) <= 0) { + await new Promise((resolve) => { + wake = resolve; + }); + } + }, + close() { + reader.close(); + }, + abort(reason) { + reader.error(onWire(reason, "reset stream")); + }, + }, + WRITABLE_STRATEGY, + ); + + streams.add((err) => { + reader.error(err); + writer.error(err); + end(); + }); + + return { readable, writable }; } -/** A fake {@link WebTransport} backed by TransformStreams, paired with a peer. */ +/** A fake {@link WebTransport} backed by in-memory streams, paired with a peer. */ export class MockTransport implements WebTransport { readonly protocol: string; readonly ready: Promise; @@ -184,7 +246,7 @@ export class MockTransport implements WebTransport { const peer = this.#peer; if (!peer) throw new Error("no peer"); - // Create two TransformStreams for the two directions + // One stream for each direction const c2s = newStream(this.#streams); const s2c = newStream(this.#streams); @@ -247,8 +309,8 @@ export class MockTransport implements WebTransport { // Shaped like the WebTransportError a real session close fails its streams with. const closed = Object.assign(new Error("session closed"), { source: "session", streamErrorCode: null }); - for (const stream of this.#streams) { - stream.error(closed); + for (const fail of this.#streams) { + fail(closed); } this.#streams.clear(); From 6975a78c198a6a790b912f7c3fc65a30ac0c400b Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 28 Oct 2026 06:25:54 -0700 Subject: [PATCH 3/4] fix(net): format, and finish quest/m1/mock-reset-codes.md Co-Authored-By: Claude Opus 5.5 --- js/net/src/ietf/publisher.ts | 4 +++- js/net/src/mock.test.ts | 12 ++++++++++-- quest/m1/README.md | 1 - quest/m1/mock-reset-codes.md | 23 ----------------------- 4 files changed, 13 insertions(+), 27 deletions(-) delete mode 100644 quest/m1/mock-reset-codes.md diff --git a/js/net/src/ietf/publisher.ts b/js/net/src/ietf/publisher.ts index cbe3bcf1102..bf44499e053 100644 --- a/js/net/src/ietf/publisher.ts +++ b/js/net/src/ietf/publisher.ts @@ -854,7 +854,9 @@ export class Publisher { const fillAbort = new AbortController(); const unsubscribed = new Promise((resolve) => { unsubscribe = () => { - fillAbort.abort(new StreamError(StreamCode.Cancel, { message: "unsubscribed before the fill finished" })); + fillAbort.abort( + new StreamError(StreamCode.Cancel, { message: "unsubscribed before the fill finished" }), + ); resolve(); }; }); diff --git a/js/net/src/mock.test.ts b/js/net/src/mock.test.ts index 528796e3dec..d393576f67e 100644 --- a/js/net/src/mock.test.ts +++ b/js/net/src/mock.test.ts @@ -10,10 +10,18 @@ async function pair() { return { local, remote }; } +/** What a read sees once the stream fails. */ +function readError(readable: ReadableStream): Promise { + return readable + .getReader() + .read() + .catch((e: unknown) => e); +} + test("a reset reaches the peer as its code alone, like a real wire", async () => { const { local, remote } = await pair(); await local.writable.abort(new Error("local detail")); - const err = await remote.readable.getReader().read().catch((e: unknown) => e); + const err = await readError(remote.readable); expect(err).toMatchObject({ source: "stream", streamErrorCode: 0 }); expect((err as Error).message).not.toContain("local detail"); }); @@ -21,7 +29,7 @@ test("a reset reaches the peer as its code alone, like a real wire", async () => test("a reset keeps the code its reason carries", async () => { const { local, remote } = await pair(); await local.writable.abort(toTransport(StreamCode.Cancel, "cancel")); - const err = await remote.readable.getReader().read().catch((e: unknown) => e); + const err = await readError(remote.readable); expect(err).toMatchObject({ source: "stream", streamErrorCode: StreamCode.Cancel }); }); diff --git a/quest/m1/README.md b/quest/m1/README.md index 3f93058e5c5..ce392cc7f72 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -151,7 +151,6 @@ removed). - [Rust FETCH parity](/quest/m1/ietf-fetch-parity.md) - Rust finishes an answered joining FETCH's head, refuses a lost head with DOES_NOT_EXIST, and sends no timestamps without TIMESCALE, as the JS publisher does - [SUBSCRIBE honors the properties opt-out](/quest/m1/subscribe-timestamps.md) - the Rust publisher sends untimed objects to a SUBSCRIBE that declines Track Properties, and the Rust subscriber asks for them when it needs timestamps - [JS FETCH timeout test](/quest/m1/js-fetch-timeout-test.md) - a JS joining FETCH still waiting on its SUBSCRIBE at the deadline is refused with TIMEOUT -- [Mock reset codes](/quest/m1/mock-reset-codes.md) - `js/net`'s mock transport carries only reset codes, and any reset found sending the wrong code is fixed - [IETF fill timeout](/quest/m1/ietf-fill-timeout.md) - Rust honors native FILL_TIMEOUT to bound upstream waiting on drafts 18 and 22, alongside local content-age enforcement - [JS FETCH fill timeout](/quest/m1/ietf-fill-timeout-js.md) - the JS FETCH responder honors FILL_TIMEOUT as Rust does - [Rust identity enforcement](/quest/m1/catalog-identity-rs.md) - `moq-mux` refuses a redefined rendition, and every Rust publisher mints a new rendition name for a decoder identity change and updates everything else in place diff --git a/quest/m1/mock-reset-codes.md b/quest/m1/mock-reset-codes.md deleted file mode 100644 index e3d7f65675f..00000000000 --- a/quest/m1/mock-reset-codes.md +++ /dev/null @@ -1,23 +0,0 @@ -# [M] The JS mock transport carries only reset codes - -## Goal - -`js/net`'s mock transport delivers a stream reset to the peer as a code, as -real WebTransport does (0 for a plain `Error`), instead of the local reason -object. Every test that relied on a reason a real wire cannot carry is -fixed, and so is any reset that would go out with the wrong code. - -## Plan - -Found by [moq-dev/moq#6988](https://github.com/moq-dev/moq/pull/6988). A -cancel hook on the mock's TransformStream works in bun and node but makes -`abort()` reject; wrapping the read side does not. Either breaks 9 tests: - -- 4 ietf publisher draft-22 tests expect "unsubscribed" or "range update", - which suggests those resets go out as INTERNAL_ERROR on a real wire and - should be CANCELLED. Reproduce against a real peer and fix the code at the - publisher. -- 2 lite publisher tests and 3 reload/connect setup tests check reasons a - real wire cannot carry; assert on what the peer can observe. - -Public API: none. Wire: any reset found sending the wrong code is corrected. From e39f2a5b3d203b8b28753a92deb507f171ae4231 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 28 Oct 2026 06:32:12 -0700 Subject: [PATCH 4/4] test(net): park the mock write without a real timer Co-Authored-By: Claude Opus 5.5 --- js/net/src/mock.test.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/js/net/src/mock.test.ts b/js/net/src/mock.test.ts index d393576f67e..2d143fe3be1 100644 --- a/js/net/src/mock.test.ts +++ b/js/net/src/mock.test.ts @@ -46,8 +46,9 @@ test("a write parked on a full peer gives up when the peer stops", async () => { const writer = local.writable.getWriter(); // Past both queues, so the last write parks on backpressure. const writes = Array.from({ length: 600 }, () => writer.write(new Uint8Array(1)).catch((e: unknown) => e)); - // A host task lets the writes run until one parks in flight on the full peer. - await new Promise((resolve) => setTimeout(resolve, 0)); + // The peer queues 256 chunks. The write before the 256th settles just as the 256th starts, + // which fills the queue and parks in flight. + await writes[254]; await remote.readable.cancel(toTransport(StreamCode.Cancel, "cancel")); const last = await writes[writes.length - 1]; expect(last).toMatchObject({ source: "stream", streamErrorCode: StreamCode.Cancel });