Skip to content

Commit fa5fda6

Browse files
only21milclaude
andcommitted
fix(web): rejected PDF responses release their bodies
The PDF.js fallback threw on an error status or an oversize declared Content-Length without cancelling the response body, so the rejected stream stayed open while the error view was shown. Cancel it before throwing, without letting a failed cancel replace the visible error. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 15fe283 commit fa5fda6

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { expect, it, vi } from "vite-plus/test";
2+
import { readBoundedDocument } from "./readBoundedDocument";
3+
4+
it.each([
5+
{ name: "an error status", status: 503, headers: {} },
6+
{ name: "a declared oversize body", status: 200, headers: { "content-length": "11" } },
7+
])("releases the response body it rejects for $name", async ({ status, headers }) => {
8+
const cancel = vi.fn(() => {
9+
throw new Error("cancel failed");
10+
});
11+
const body = new ReadableStream<Uint8Array>({ cancel });
12+
using _fetch = vi
13+
.spyOn(globalThis, "fetch")
14+
.mockResolvedValue(new Response(body, { status, headers }));
15+
16+
await expect(readBoundedDocument("/file.pdf", new AbortController().signal, 10)).rejects.toThrow(
17+
"This file is unavailable or too large to preview.",
18+
);
19+
expect(cancel).toHaveBeenCalledOnce();
20+
});

‎apps/web/src/components/files/readBoundedDocument.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ export async function readBoundedDocument(
55
cache: RequestCache = "default",
66
) {
77
const response = await fetch(src, { signal, cache });
8-
if (!response.ok || Number(response.headers.get("content-length")) > limit)
8+
if (!response.ok || Number(response.headers.get("content-length")) > limit) {
9+
void response.body?.cancel().catch(() => undefined);
910
throw new Error("This file is unavailable or too large to preview.");
11+
}
1012
const reader = response.body?.getReader();
1113
if (!reader) throw new Error("This file is unavailable.");
1214
const chunks: Uint8Array<ArrayBuffer>[] = [];

0 commit comments

Comments
 (0)