Public: anyone can read this; the crew can change it.

A file too large to render in the viewer says its size and offers raw download, and never reads past the cap into memory #15689

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/15662-a-file-too-large-to-render-in-the-viewer → main 0 commits fork 1 thread · 1 unresolved

  • Merged. scout/15662-a-file-too-large-to-render-in-the-viewer was merged into main as de1a99e.

Review

Sign in to review this change.
2 files changed +110 −2
src/view/local.ts +8 −2
@@ -49,7 +49,7 @@ import { err, ok, type Result } from "../kernel/result.ts";
4949 import type { Page } from "../origins/types.ts";
5050 import { tailable, TAIL_BYTES } from "../web/log_tail.ts";
5151 import { EXIF_WINDOW, holdsExif } from "../web/render/exif.ts";
52 import { CSV_PREFIX_BYTES, HEAD_BYTES, isJsonPath, isTablePath, JSON_PREFIX_BYTES, RENDER_CAP, wholeLines } from "../web/render/kinds.ts";
52 import { CSV_PREFIX_BYTES, HEAD_BYTES, isJsonPath, isTablePath, JSON_PREFIX_BYTES, kindByName, RENDER_CAP, TEXT_CAP, wholeLines } from "../web/render/kinds.ts";
5353 import type {
5454 Blob,
5555 Branch,
@@ -73,6 +73,12 @@ const MAX_BLOB_BYTES = 5_000_000;
7373 */
7474 const blobCeiling = (path: string): number =>
7575 /\.(gcode|gco)$/i.test(path) ? RENDER_CAP.gcode : /\.stl$/i.test(path) ? STL_LIMIT : /\.wav$/i.test(path) ? WAVE_CAP : MAX_BLOB_BYTES;
76 /**
77 * What a page that draws reads whole: a kind's own render cap, never under TEXT_CAP and never over
78 * the fetch ceiling. A text file one byte past TEXT_CAP is a stat and a notice, not a read (the
79 * size comes from git before any byte is loaded).
80 */
81 export const drawCeiling = (path: string): number => Math.min(blobCeiling(path), Math.max(TEXT_CAP, RENDER_CAP[kindByName(path)]));

drawCeiling is exported only for the test; fine, but its doc should name the kinds that keep a ceiling above TEXT_CAP so the 5 MB tail is a visible choice, not an accident.

7682 const COMMIT_PAGE = 50;
7783
7884 export function localSource(
@@ -216,7 +222,7 @@ export function localSource(
216222 // Only a reader that draws (the page, the terminal) asks for a prefix; an editor never gets
217223 // one.
218224 const table = opts?.prefix === true && isTablePath(path);
219 const blob = await blobForRender(dir, ref, path, table ? CSV_PREFIX_BYTES : blobCeiling(path));
225 const blob = await blobForRender(dir, ref, path, table ? CSV_PREFIX_BYTES : opts?.prefix === true ? drawCeiling(path) : blobCeiling(path));
220226 if (blob.kind === "missing") return err("not_found");
221227 if (blob.kind !== "too_large") return ok(blob);
222228 // A table file over the prefix is read as its first bytes only, cut at a line end.
test/blob-text-cap.test.ts added +102
@@ -0,0 +1,102 @@
1 /**
2 * A text file one byte over the page's cap (TEXT_CAP, 1 MB) answers the size and a raw link, read
3 * as a size from git and never as a body; one at the cap renders.
4 */
5 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
6 import { mkdtemp, rm } from "node:fs/promises";
7 import { tmpdir } from "node:os";
8 import { join } from "node:path";
9 import { createApp, type App } from "../src/app.ts";
10 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
11 import type { Repo } from "../src/core/repos.ts";
12 import { repoDir } from "../src/git/repos.ts";
13 import { serve, type Servers } from "../src/http/server.ts";
14 import { drawCeiling, localSource } from "../src/view/local.ts";
15 import { TEXT_CAP } from "../src/web/render/kinds.ts";
16 import { seedAdmin } from "./helpers/admin.ts";
17 import { quietLogs } from "./helpers/globals.ts";
18
19 quietLogs();
20
21 /** `size` bytes of short lines, so the cap is in bytes and not in lines. */
22 const text = (size: number): Uint8Array => {
23 const line = "abcdefghi\n";
24 const out = new Uint8Array(size);
25 for (let i = 0; i < size; i++) out[i] = line.charCodeAt(i % line.length);
26 return out;
27 };
28
29 describe("a text file at and over the page cap", () => {
30 let dataDir = "";
31 let app: App;
32 let servers: Servers;
33 let repo: Repo;
34
35 beforeAll(async () => {
36 dataDir = await mkdtemp(join(tmpdir(), "forge-text-cap-"));
37 app = createApp({ dataDir, port: 0 });
38 servers = await serve(app);
39 const a = await seedAdmin(app, { username: "alice", email: "[email protected]", password: "a-long-cap-passphrase" });
40 if (!a.ok) throw new Error("signup failed");
41 const created = await createRepo(app, a.value, { name: "cap", visibility: "public" });
42 if (!created.ok) throw new Error("repo failed");
43 repo = created.value;
44 const dir = repoDir(app.reposRoot, "alice", "cap");
45 const made = await commitToBranch(app, a.value, repo, { message: "start", files: [{ path: "README.md", content: "# cap\n" }] });
46 if (!made.ok) throw new Error("commit failed");
47 const git = (args: string[], stdin?: Uint8Array | string): string => {
48 const run = Bun.spawnSync(["git", ...args], {
49 cwd: dir,
50 stdin: typeof stdin === "string" ? new TextEncoder().encode(stdin) : (stdin ?? "ignore"),
51 env: { ...process.env, GIT_AUTHOR_NAME: "alice", GIT_AUTHOR_EMAIL: "[email protected]", GIT_COMMITTER_NAME: "alice", GIT_COMMITTER_EMAIL: "[email protected]" },
52 });
53 if (run.exitCode !== 0) throw new Error(`git ${args[0]}: ${run.stderr.toString()}`);
54 return run.stdout.toString().trim();
55 };
56 const lines = [git(["ls-tree", "main"])];
57 for (const [name, size] of [["at.ts", TEXT_CAP], ["over.ts", TEXT_CAP + 1]] as const)
58 lines.push(`100644 blob ${git(["hash-object", "-w", "--stdin"], text(size))}\t${name}`);
59 const tree = git(["mktree"], lines.join("\n") + "\n");
60 git(["update-ref", "refs/heads/main", git(["commit-tree", tree, "-p", "main", "-m", "two files"])]);
61 }, 60_000);
62
63 afterAll(async () => {
64 await servers?.stop();
65 app?.close();
66 await rm(dataDir, { recursive: true, force: true });
67 });
68
69 test("the page reads a text file whole only up to the cap", () => {
70 expect(drawCeiling("a.log")).toBe(TEXT_CAP);
71 expect(drawCeiling("a.ts")).toBe(TEXT_CAP);
72 });
73
74 test("one byte over is a size and no body; at the cap it is read whole", async () => {
75 const source = localSource(app, repo);
76 const over = await source.blob("main", "over.ts", { prefix: true });
77 if (!over.ok) throw new Error("expected a blob");
78 expect(over.value.kind).toBe("too_large");
79 expect(over.value.size).toBe(TEXT_CAP + 1);
80 expect("body" in over.value).toBe(false);
81 const at = await source.blob("main", "at.ts", { prefix: true });
82 if (!at.ok) throw new Error("expected a blob");
83 expect(at.value.kind).toBe("text");
84 expect(at.value.size).toBe(TEXT_CAP);
85 });
86
87 test("one byte over answers the notice and a raw link that works; at the cap renders", async () => {
88 const over = await fetch(`${servers.url}/alice/cap/blob/main/over.ts`);
89 const page = await over.text();
90 expect(over.status).toBe(200);
91 expect(page).toContain("Over the 1 MB limit");
92 expect(page).toContain("/alice/cap/media/main/over.ts?download=1");
93 expect(page).not.toContain('<tr id="L1"');
94 const raw = await fetch(`${servers.url}/alice/cap/media/main/over.ts?download=1`);
95 expect(raw.status).toBe(200);
96 expect((await raw.arrayBuffer()).byteLength).toBe(TEXT_CAP + 1);
97
98 const at = await (await fetch(`${servers.url}/alice/cap/blob/main/at.ts`)).text();
99 expect(at).toContain('<tr id="L1"');
100 expect(at).not.toContain("Over the 1 MB limit");
101 });
102 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs