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
13 changes: 9 additions & 4 deletions js/net/src/ietf/fetch.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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<void> {
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<void> {
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. */
Expand Down
42 changes: 42 additions & 0 deletions js/net/src/ietf/object.test.ts
Original file line number Diff line number Diff line change
@@ -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<Uint8Array>({ 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);
}
});
8 changes: 8 additions & 0 deletions js/net/src/ietf/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
await w.write(await Writer.collect(version, (h) => this.#encode(h, version)));
}

async #encode(w: Writer, version: IetfVersion): Promise<void> {
if (!this.flags.hasSubgroup && this.subGroupId !== 0) {
throw new Error(`Subgroup ID must be 0 if hasSubgroup is false: ${this.subGroupId}`);
}
Expand Down
1 change: 0 additions & 1 deletion js/net/src/ietf/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
28 changes: 28 additions & 0 deletions js/net/src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<void>): Promise<Uint8Array> {
const chunks: Uint8Array[] = [];
// Copied, since each primitive reuses the Writer's scratch buffer.
const w = new Writer(
new WritableStream<Uint8Array>({ 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
Expand Down
Loading