diff --git a/src/artifacts/N64RomArtifactReader.test.ts b/src/artifacts/N64RomArtifactReader.test.ts index 2e75171e075..35a72cdf2a2 100644 --- a/src/artifacts/N64RomArtifactReader.test.ts +++ b/src/artifacts/N64RomArtifactReader.test.ts @@ -205,3 +205,40 @@ describe("Nintendo 64 ROM reader", () => { expect(result.metadata?.crc.status).toBe("match"); }); }); + +it.each(["v64", "n64"] as const)( + "streams the %s program in bounded chunks and honors cancellation", + async (order) => { + const program = Buffer.from("12345678".repeat(32 * 1024 + 1)); + const image = n64RomImage({ order, program }); + const reader = new N64RomArtifactReader(await write(image.encoded)); + try { + const entries: ArtifactEntry[] = []; + for await (const entry of reader.entries()) entries.push(entry); + const programEntry = entries.find( + (entry) => entry.path === "program.z64", + )!; + const hash = createHash("sha256"); + let count = 0; + for await (const chunk of await reader.open(programEntry)) { + hash.update(chunk); + count++; + } + expect(count).toBeGreaterThan(1); + expect(hash.digest("hex")).toBe(digest(image.normalized)); + + const controller = new AbortController(); + const stream = await reader.open(programEntry, controller.signal); + const iterator = stream[Symbol.asyncIterator](); + const first = await iterator.next(); + expect(first.done).toBe(false); + expect(first.value.length).toBeLessThan(image.normalized.length); + controller.abort("stop after the first chunk"); + await expect(iterator.next()).rejects.toMatchObject({ + reason: "cancelled", + }); + } finally { + await reader.close(); + } + }, +); diff --git a/src/artifacts/N64RomArtifactReader.ts b/src/artifacts/N64RomArtifactReader.ts index b6db3d742ef..4002775f013 100644 --- a/src/artifacts/N64RomArtifactReader.ts +++ b/src/artifacts/N64RomArtifactReader.ts @@ -35,11 +35,14 @@ import { type PositionalArtifactEntry, } from "./PositionalArtifactReader.js"; +// A 64 KiB read holds whole v64 pairs and n64 words. +const NORMALIZED_CHUNK_BYTES = 64 * 1024; + /** * Nintendo 64 cartridge ROM. Header facts come from the domain parser. * `header.bin`, `ipl3.bin` and `program.z64` are stored ranges when the dump - * is already big-endian. v64 and n64 dumps serve those same ranges after one - * byte-order swap so every entry is big-endian. Nothing is executed or + * is already big-endian. v64 and n64 dumps serve those same ranges after a + * chunked byte-order swap so every entry is big-endian. Nothing is executed or * patched. A CRC mismatch stays a limitation. */ export class N64RomArtifactReader extends PositionalArtifactReader { @@ -149,8 +152,8 @@ export class N64RomArtifactReader extends PositionalArtifactReader void, ): AsyncIterable { const { signal, cancellationFailure } = member; - onStateChange("pending"); - let normalized: Uint8Array; - try { + const end = entry.byteOffset + entry.declaredSize; + for (let position = entry.byteOffset; position < end;) { + const length = Math.min(NORMALIZED_CHUNK_BYTES, end - position); + onStateChange("pending"); + let normalized: Uint8Array; + try { + if (signal.aborted) throw cancellationFailure(signal.reason); + const stored = await readArtifactFileBytes( + N64_FORMAT, + handle, + position, + length, + initial.size, + signal, + ); + normalized = swapN64Bytes(stored, this.#order); + onStateChange("idle"); + } catch (cause: unknown) { + onStateChange("failed"); + if (artifactReadCancelledBy(cause, signal)) + throw cancellationFailure(signal.reason); + if (cause instanceof ParseFailureError) + throw artifactParseFailure(cause, signal); + throw cause; + } if (signal.aborted) throw cancellationFailure(signal.reason); - const stored = await readArtifactFileBytes( - N64_FORMAT, - handle, - entry.byteOffset, - entry.declaredSize, - initial.size, - signal, + position += length; + yield Buffer.from( + normalized.buffer, + normalized.byteOffset, + normalized.length, ); - normalized = swapN64Bytes(stored, this.#order); - onStateChange("idle"); - } catch (cause) { - onStateChange("failed"); - if (artifactReadCancelledBy(cause, signal)) - throw cancellationFailure(signal.reason); - if (cause instanceof ParseFailureError) - throw artifactParseFailure(cause, signal); - throw cause; } - if (signal.aborted) throw cancellationFailure(signal.reason); - yield Buffer.from( - normalized.buffer, - normalized.byteOffset, - normalized.length, - ); } }