Reverse engineer anything with agents, from app behavior down to native binaries.
TypeScript agent-skillsai-agentsbinary-analysisclaude-codeclicodexcordisdecompilerdeveloper-toolsdshdsh-pluginghidrahopperllmmcpmodel-context-protocolreareverse-engineeringstatic-analysistouhou
refactor(files): allow reader-owned cancellation failures
morluto5e13e0a8765d
diff --git a/src/process/BoundedFileBytes.ts b/src/process/BoundedFileBytes.tsindex a9d6f33a5..fec1c5d98 100644--- a/src/process/BoundedFileBytes.ts+++ b/src/process/BoundedFileBytes.ts@@ -6,7 +6,13 @@ export const readBoundedFileBytes = async (maxBytes: number,signal?: AbortSignal,expectedSize?: number,+ cancellationFailure?: (reason: unknown) => Error,): Promise<Buffer | undefined> => {+ const assertActive = (): void => {+ if (signal?.aborted && cancellationFailure !== undefined)+ throw cancellationFailure(signal.reason);+ signal?.throwIfAborted();+ };if (!Number.isSafeInteger(maxBytes) || maxBytes < 0)throw new RangeError("File byte limit must be a nonnegative safe integer");if (@@ -21,7 +27,7 @@ export const readBoundedFileBytes = async (const buffer = Buffer.allocUnsafe(Math.min(64 * 1_024, maxBytes + 1));let total = 0;for (;;) {- signal?.throwIfAborted();+ assertActive();const { bytesRead } = await handle.read(buffer,0,@@ -38,7 +44,7 @@ export const readBoundedFileBytes = async (if (expectedSize === 0) return buffer;let total = 0;for (;;) {- signal?.throwIfAborted();+ assertActive();const { bytesRead } = await handle.read(buffer,total,diff --git a/tests/boundary/filesystem/boundedFileBytes.test.ts b/tests/boundary/filesystem/boundedFileBytes.test.tsindex a0c358ae6..02f34b841 100644--- a/tests/boundary/filesystem/boundedFileBytes.test.ts+++ b/tests/boundary/filesystem/boundedFileBytes.test.ts@@ -23,6 +23,40 @@ describe("bounded file byte reads", () => {},);+ it.each([undefined, 5])(+ "preserves raw reasons or caller-owned cancellation with expected size %s",+ async (expectedSize) => {+ const root = await createTestTempDirectory("rea-bounded-cancel-");+ const path = join(root, "input");+ await writeFile(path, "hello");+ const handle = await open(path, "r");+ try {+ for (const reason of [null, "stopped", new Error("stopped")]) {+ const controller = new AbortController();+ controller.abort(reason);+ await expect(+ readBoundedFileBytes(handle, 5, controller.signal, expectedSize),+ ).rejects.toBe(reason);+ const typed = new Error("typed cancellation", { cause: reason });+ await expect(+ readBoundedFileBytes(+ handle,+ 5,+ controller.signal,+ expectedSize,+ (observed) => {+ expect(observed).toBe(reason);+ return typed;+ },+ ),+ ).rejects.toBe(typed);+ }+ } finally {+ await handle.close();+ }+ },+ );+it("stops after one overflow byte when a file grows beyond its admitted size", async () => {const root = await createTestTempDirectory("rea-growing-bytes-");const path = join(root, "input");