diff --git a/js/net/src/ietf/fetch.ts b/js/net/src/ietf/fetch.ts index e0a5159873f..8d054423767 100644 --- a/js/net/src/ietf/fetch.ts +++ b/js/net/src/ietf/fetch.ts @@ -1,4 +1,4 @@ -import type { Reader, Writer } from "../stream.ts"; +import { type Reader, Writer } from "../stream.ts"; import { hasRangeFilters, isDraft20 } from "./filter.ts"; import * as Message from "./message.ts"; import * as Namespace from "./namespace.ts"; @@ -20,9 +20,14 @@ export class FetchHeader { this.requestId = requestId; } - /** Write the header, which the stream type must already precede. */ - async encode(w: Writer, _version: IetfVersion): Promise { - await w.u62(this.requestId); + /** Write the stream type and the header in one write, like a subgroup header. */ + async encode(w: Writer, version: IetfVersion): Promise { + await w.write( + await Writer.collect(version, async (h) => { + await h.u53(FetchHeader.type); + await h.u62(this.requestId); + }), + ); } /** Read the header, with the stream type already consumed. */ diff --git a/js/net/src/ietf/object.test.ts b/js/net/src/ietf/object.test.ts new file mode 100644 index 00000000000..dd8e21e1ce3 --- /dev/null +++ b/js/net/src/ietf/object.test.ts @@ -0,0 +1,42 @@ +import { expect, test } from "bun:test"; +import { Reader, Writer } from "../stream.ts"; +import { FetchHeader } from "./fetch.ts"; +import { Group } from "./object.ts"; +import { Version } from "./version.ts"; + +test("a data stream's header goes out in one write, stream type included", async () => { + for (const version of [Version.DRAFT_14, Version.DRAFT_22]) { + const chunks: Uint8Array[] = []; + const writer = new Writer( + new WritableStream({ write: (chunk) => void chunks.push(new Uint8Array(chunk)) }), + version, + ); + + // Every optional field present, with values past a one-byte varint. + const group = new Group({ + trackAlias: 1_000_000n, + groupId: 70_000, + subGroupId: 300, + publisherPriority: 200, + flags: { + hasExtensions: true, + hasSubgroup: true, + hasSubgroupObject: false, + hasEnd: true, + hasPriority: true, + firstObject: true, + }, + }); + await group.encode(writer, version); + await new FetchHeader({ requestId: 1_000_000n }).encode(writer, version); + expect(chunks.length).toBe(2); + + const decoded = await Group.decode(new Reader(undefined, chunks[0], version), version); + expect(decoded).toEqual(group); + + const fetch = new Reader(undefined, chunks[1], version); + expect(await fetch.u53()).toBe(FetchHeader.type); + expect((await FetchHeader.decode(fetch, version)).requestId).toBe(1_000_000n); + expect(await fetch.done()).toBe(true); + } +}); diff --git a/js/net/src/ietf/object.ts b/js/net/src/ietf/object.ts index 00f7c7eb261..40a0b5fd930 100644 --- a/js/net/src/ietf/object.ts +++ b/js/net/src/ietf/object.ts @@ -181,7 +181,15 @@ export class Group { this.publisherPriority = publisherPriority; } + /** + * Write the header, stream type first, in one write: a peer that classifies a stream from + * its first read (imquic) drops a header split across several. + */ async encode(w: Writer, version: IetfVersion): Promise { + await w.write(await Writer.collect(version, (h) => this.#encode(h, version))); + } + + async #encode(w: Writer, version: IetfVersion): Promise { if (!this.flags.hasSubgroup && this.subGroupId !== 0) { throw new Error(`Subgroup ID must be 0 if hasSubgroup is false: ${this.subGroupId}`); } diff --git a/js/net/src/ietf/publisher.ts b/js/net/src/ietf/publisher.ts index 337e97cece5..45aed3e1609 100644 --- a/js/net/src/ietf/publisher.ts +++ b/js/net/src/ietf/publisher.ts @@ -802,7 +802,6 @@ export class Publisher { } streams.opened += 1; - await stream.u53(FetchHeader.type); await new FetchHeader({ requestId }).encode(stream, version); if (fill.kind !== "group") { diff --git a/js/net/src/stream.ts b/js/net/src/stream.ts index 44b0aaea61c..a3655730e7a 100644 --- a/js/net/src/stream.ts +++ b/js/net/src/stream.ts @@ -117,6 +117,17 @@ export function encodeVarint(v: number | bigint, version: StreamVersion): Uint8A return encodeTo(new ArrayBuffer(9), parts.hi, lo, version); } +/** The parts joined into one buffer, so they can go out in one write. */ +export function concat(parts: Uint8Array[]): Uint8Array { + const out = new Uint8Array(parts.reduce((n, p) => n + p.byteLength, 0)); + let offset = 0; + for (const part of parts) { + out.set(part, offset); + offset += part.byteLength; + } + return out; +} + /** * The `WebTransportSendStream` that every outgoing stream is, narrowed to the one attribute * this package uses. The DOM types available here still describe them as plain @@ -774,6 +785,23 @@ export class Writer { this.#writer.abort(withCode(reason, this.version)).catch(() => void 0); } + /** Run `f` against a Writer that buffers in memory, returning everything it wrote as one chunk. */ + static async collect(version: StreamVersion, f: (w: Writer) => Promise): Promise { + const chunks: Uint8Array[] = []; + // Copied, since each primitive reuses the Writer's scratch buffer. + const w = new Writer( + new WritableStream({ write: (chunk) => void chunks.push(chunk.slice()) }), + version, + ); + try { + await f(w); + } finally { + w.close(); + } + await w.closed; + return concat(chunks); + } + /** * Open an outgoing unidirectional stream. * @param quic - The session to open it on