Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions js/net/src/ietf/publisher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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"]);
Expand Down
15 changes: 9 additions & 6 deletions js/net/src/ietf/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,9 @@ export class Publisher {
const fillAbort = new AbortController();
const unsubscribed = new Promise<void>((resolve) => {
unsubscribe = () => {
fillAbort.abort(new Error("unsubscribed before the fill finished"));
fillAbort.abort(
new StreamError(StreamCode.Cancel, { message: "unsubscribed before the fill finished" }),
);
resolve();
};
});
Expand Down Expand Up @@ -1157,7 +1159,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();
}
Expand Down Expand Up @@ -1218,9 +1220,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
Expand All @@ -1232,7 +1235,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<unknown>) => {
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;
};

Expand Down Expand Up @@ -1260,7 +1263,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;
Expand Down
55 changes: 55 additions & 0 deletions js/net/src/mock.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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 };
}

/** What a read sees once the stream fails. */
function readError(readable: ReadableStream<Uint8Array>): Promise<unknown> {
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 readError(remote.readable);
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 readError(remote.readable);
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));
// 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 });
});
90 changes: 76 additions & 14 deletions js/net/src/mock.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -32,26 +32,88 @@ const WRITABLE_STRATEGY: QueuingStrategy<Uint8Array> = { highWaterMark: 256 };
const READABLE_STRATEGY: QueuingStrategy<Uint8Array> = { highWaterMark: 256 };

/** Every stream of a transport pair, so a session close can fail the ones still open. */
type StreamSet = Set<TransformStreamDefaultController<Uint8Array>>;
type StreamSet = Set<(err: Error) => void>;

function newStream(streams: StreamSet): TransformStream<Uint8Array, Uint8Array> {
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<Uint8Array>; writable: WritableStream<Uint8Array> } {
let reader!: ReadableStreamDefaultController<Uint8Array>;
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<Uint8Array>(
{
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<Uint8Array>(
{
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<void>((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<undefined>;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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();

Expand Down
1 change: 0 additions & 1 deletion quest/m1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 0 additions & 23 deletions quest/m1/mock-reset-codes.md

This file was deleted.

Loading