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

A file kind the forge cannot draw says so in one line and offers the download, never a blank page #14074

Merged opened this

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

Merged

scout/forge:scout/14045-a-file-kind-the-forge-cannot-draw-says-s → main 10 commits fork 6 threads · 6 unresolved

  • Merged. scout/14045-a-file-kind-the-forge-cannot-draw-says-s was merged into main as 0b3c6b5.

Review

Sign in to review this change.
12 files changed +165 −16
docs/features/README.md +1
@@ -289,6 +289,7 @@ Read with the guide page [Your apps](../guide-your-apps.md).
289289 ### Files in the browser
290290
291291 - [Every kind of file the viewer opens, and how large](file-kinds.md)
292 - [A file too large to draw says so in one line](undrawable-file.md)
292293 - [An .obj or .ply file opens in the 3D viewer](mesh-files.md)
293294 - [An .stl file as a 3D model](file-stl.md)
294295 - [A .dxf file as a picture](file-dxf.md)
docs/features/undrawable-file.md added +17
@@ -0,0 +1,17 @@
1 # A file too large to draw says so in one line
2
3 Open the blob page (or the terminal's file view) of a file past what the forge reads
4 whole. Instead of a blank page it shows `name · size` as the title, the reason as the note under
5 it (so the title holds one line on a phone), and a Download button; the terminal puts name, size
6 and reason on a line each:
7
8 - a kind the forge knows says its limit and why: `Over the 1 MB limit for markdown files,
9 because the page reads a file whole to draw it.` (images, PDF and the like say the browser
10 would have to load them whole);
11 - bytes of no kind it knows say `The forge cannot draw this yet.`
12
13 A binary within the read ceiling says `The forge cannot draw this yet.` too, beside its type word,
14 the Download and its hex dump (`binary-file.md`). The wording is `src/web/render/undrawable.ts`,
15 shared by the page and the terminal: it returns the parts, and each door joins them, so a file
16 name holding ` · ` never splits wrong.
17 Tests: `test/render-undrawable.test.ts`, `test/log-tail-doors.test.ts` (both doors).
src/tui/screens/workspace.ts +8 −4
@@ -124,6 +124,7 @@ import { renderMarkdown, type Line } from "../ui/markdown.ts";
124124 import { archiveNote } from "../ui/archive-note.ts";
125125 import { psdNote } from "../ui/psd-note.ts";
126126 import { mediaNote } from "../ui/media-note.ts";
127 import { undrawable, UNDRAWN } from "../../web/render/undrawable.ts";
127128 import { tokColor, type Tokens } from "../ui/theme.ts";
128129 import { card, emptyState, input, noteLines, scrollbar } from "../ui/widgets.ts";
129130 import type { Session } from "../session.ts";
@@ -1485,9 +1486,12 @@ export function workspaceScreen(
14851486 else if (media !== undefined) note = media;
14861487 else if (tail !== undefined) {
14871488 raw = new TextEncoder().encode(`${tailSentence(tail)}\n${downloadAt(t.path)}\n\n${tail.lines.join("\n")}\n`);
1488 } else if (read.value.kind === "too_large")
1489 note = `${formatSize(read.value.size)} — too large to display.`;
1490 else if (read.value.kind === "binary") {
1489 } else if (read.value.kind === "too_large") {
1490 // One line a part, so the pane keeps the whole reason and the way to the download.
outdated

Long line with a fallback ?? ... too large to display that is unreachable for too_large blobs only if undrawable always returns; keep one wording. The terminal binary case has no test.

1491 const said = undrawable(t.path, read.value, formatSize);
outdated

Parsing the formatted string back apart with split(' · ') breaks on a file name containing ' · '. Return the parts from undrawable() instead.

1492 const lines = said === undefined ? [`${formatSize(read.value.size)} — over the render cap.`] : [said.name, said.size, said.reason];
1493 note = `${lines.join("\n")}\n\n${downloadAt(t.path)}`;
1494 } else if (read.value.kind === "binary") {
14911495 // A binary blob whose *name* says picture is the one binary we do
14921496 // something with. Everything else keeps the honest note: showing
14931497 // bytes as text is how a terminal ends up beeping at you.
@@ -1507,7 +1511,7 @@ export function workspaceScreen(
15071511 // safe to draw).
15081512 const head = read.value.body.subarray(0, HEX_BYTES);
15091513 raw = new TextEncoder().encode(
1510 `Binary file, ${formatSize(read.value.size)}, ${sniffType(head)}. First ${formatSize(head.length)} as hex.\n\n${hexdump(head)}\n`,
1514 `Binary file, ${formatSize(read.value.size)}, ${sniffType(head)}. ${UNDRAWN}\n${downloadAt(t.path)}\nFirst ${formatSize(head.length)} as hex.\n\n${hexdump(head)}\n`,
15111515 );
15121516 }
15131517 }
src/web/pages/repo.ts +14 −3
@@ -99,6 +99,7 @@ import { gcodeOffThread, rowsOffThread } from "../code_rows_pool.ts";
9999 import { flashTo, fragment, page, redirect } from "../layout.ts";
100100 import { renderMarkdown, type MarkdownRepo } from "../markdown.ts";
101101 import { overSize } from "../render/archive.ts";
102 import { undrawable, UNDRAWN, type Undrawn } from "../render/undrawable.ts";
102103 import { csvJs, isCsvPath, sortColumn } from "../render/csv.ts";
103104 import { fontJs } from "../render/font.ts";
104105 import { INLINE_MAX, keepGcode, keptGcode, layerCount } from "../render/gcode.ts";
@@ -741,6 +742,13 @@ export function blobView(value: string | null): BlobView {
741742 return value === "source" ? "source" : "rendered";
742743 }
743744
745 /** What the page says over a too-large file: the forge's own reason, or the one the read gave. */
746 function tooLargeParts(path: string, size: number, head: Uint8Array | undefined, why: string | undefined): Undrawn {
outdated

Principles 1 and 8: the whole name · size · reason line becomes the heading, which wraps badly on a phone. Put the reason in the note and keep the title short.

747 const fallback = { name: path.slice(path.lastIndexOf("/") + 1), size: fileSize(size) };
748 if (why !== undefined) return { ...fallback, reason: why };
749 return undrawable(path, { size, ...(head !== undefined ? { head } : {}) }, fileSize) ?? { ...fallback, reason: "Over the render cap. Download it instead." };
750 }
751
744752 /** One file, in each of the three states a source can hand one back in. */
745753 export function blobBody(
746754 base: string,
@@ -758,7 +766,7 @@ export function blobBody(
758766 const shown = Math.min(head.length, blob.size);
759767 return {
760768 lines: 0,
761 content: html`<p class="nb-cap">Binary file · ${fileSize(blob.size)} · ${sniffType(head)} — first ${fileSize(shown)} as hex. <a href="${downloadHref(base, ref, path)}">Download</a> · <a href="${mediaHref(base, ref, path)}">Raw</a></p>
769 content: html`<p class="nb-cap">Binary file · ${fileSize(blob.size)} · ${sniffType(head)}. ${UNDRAWN} First ${fileSize(shown)} as hex. <a href="${downloadHref(base, ref, path)}">Download</a> · <a href="${mediaHref(base, ref, path)}">Raw</a></p>
762770 <pre class="card hexdump" tabindex="0" role="region" aria-label="Hex dump of the first bytes">${hexdump(head)}</pre>`,
763771 };
764772 }
@@ -790,12 +798,15 @@ export function blobBody(
790798 };
791799 }
792800 if (blob.kind === "too_large") {
outdated

Principle 1 and 8: the title 'Too large to render' and the sentence both say the file is too big, and the name repeats the page heading. Keep one confident sentence; the nested ternary here is also hard to read, pull it into a small function.

801 // The title stays short (name and size) so it holds one line on a phone; the reason is the
802 // note.
803 const said = tooLargeParts(path, blob.size, blob.head, why);
793804 return {
794805 lines: 0,
795806 content: empty({
796807 icon: "alert",
797 title: "Too large to render",
798 note: why !== undefined ? `${fileSize(blob.size)} — ${why}` : `${fileSize(blob.size)} — over the render cap. Download it instead.`,
808 title: `${said.name} · ${said.size}`,
809 note: said.reason,
799810 action: btn("Download", {
800811 href: downloadHref(base, ref, path),
801812 variant: "primary",
src/web/render/undrawable.ts added +47
@@ -0,0 +1,47 @@
1 /**

The feature has no docs/features/<name>.md how-to; add one (not README). Also this web module is imported by src/tui: consider placing the shared wording in a neutral module.

2 * What the forge says for a file too large to read: one plain sentence, the same on the blob page
3 * and in the terminal, never a blank page. Bytes of no kind the forge knows say it cannot draw
4 * them yet; a known kind past the size its renderer takes (`RENDER_CAP`) says its limit and why.
5 * A binary within the read ceiling says `UNDRAWN` too, beside its hex dump and Download.
6 *
7 * It returns parts, not a sentence: each door joins them its own way, so a file name holding the
8 * page's separator never splits wrong.
9 */
10
11 import { fileKind, kindByName, RENDER_CAP, TEXT_CAP, type FileKind } from "./kinds.ts";
12
13 export const UNDRAWN = "The forge cannot draw this yet.";
14
15 /** A blob too large to read whole: its size and the head of its bytes, when read. */
16 export interface UndrawnBlob {
17 readonly size: number;
18 readonly head?: Uint8Array;
19 }
20
21 const MB = 1_000_000;
22 const limit = (bytes: number): string => (bytes >= MB ? `${+(bytes / MB).toFixed(1)} MB` : `${Math.round(bytes / 1000)} kB`);
23 const MEDIA: ReadonlySet<FileKind> = new Set<FileKind>(["image", "svg", "pdf", "html", "audio", "video", "model3d"]);
24
25 const baseName = (path: string): string => path.slice(path.lastIndexOf("/") + 1);
26
27 /** Why a file of this kind is over what the forge draws. */
28 function reason(kind: FileKind): string {
29 if (kind === "binary") return UNDRAWN;
30 if (kind === "text") return `Over the ${limit(TEXT_CAP)} limit for text, because the page reads a file whole to show it.`;
31 const why = MEDIA.has(kind) ? "the browser would have to load it whole" : "the page reads a file whole to draw it";
32 return `Over the ${limit(RENDER_CAP[kind])} limit for ${kind} files, because ${why}.`;
outdated

Dead branch for real input: blobs of kind 'binary' now get the hex dump (#13995), so this only fires for too_large. Remove it or wire it to a door, and cover it with a door test.

33 }
34
35 /** What the forge says of a file it does not draw: its name, its size and why. */
36 export interface Undrawn {
37 readonly name: string;
38 readonly size: string;
39 readonly reason: string;
40 }
41
42 /** The parts of the one line for a too-large file; undefined when the file is within its limit. */
43 export function undrawable(path: string, blob: UndrawnBlob, size: (bytes: number) => string): Undrawn | undefined {
44 const kind = blob.head !== undefined ? fileKind(path, blob.head) : kindByName(path);
45 if (kind !== "binary" && kind !== "text" && blob.size <= RENDER_CAP[kind]) return undefined;
46 return { name: baseName(path), size: size(blob.size), reason: reason(kind) };
47 }
test/blob-binary-page.test.ts +1 −1
@@ -50,7 +50,7 @@ describe("a binary on its blob page", () => {
5050 const res = await fetch(`${base}/alice/p/blob/main/mod.bin`);
5151 const text = await res.text();
5252 expect(res.status).toBe(200);
53 expect(text).toContain(`${body.length} B · WebAssembly module — first ${body.length} B as hex.`);
53 expect(text).toContain(`${body.length} B · WebAssembly module. The forge cannot draw this yet. First ${body.length} B as hex.`);
5454 expect(text).toContain("download=1");
5555 expect(text).not.toContain("<script>");
5656 expect(text).toContain("|.asm....&lt;script&gt;|");
test/blob-media-ceiling.test.ts +3 −3
@@ -74,7 +74,7 @@ describe("the page ceiling, by kind", () => {
7474 expect(rendererFor("notes.md", cut, { markdown: md.render })).toBeUndefined();
7575 expect(md.seen).toHaveLength(0);
7676 expect(String(blobBody("/a/r", "main", "notes.md", cut).content)).toContain(
77 "over the render cap",
77 "Over the 1 MB limit",
7878 );
7979 });
8080
@@ -166,7 +166,7 @@ describe("a large blob on the page", () => {
166166 expect(res.status).toBe(200);
167167 expect(text).toContain("<video");
168168 expect(text).toContain('src="/alice/big/media/main/big.mp4"');
169 expect(text).not.toContain("over the render cap");
169 expect(text).not.toContain("Over the 1 MB limit");
170170 expect(text.length).toBeLessThan(200_000);
171171 });
172172
@@ -185,7 +185,7 @@ describe("a large blob on the page", () => {
185185 test("a 3 MB .mp4 of words, and a 3 MB Markdown file, keep the over-cap note", async () => {
186186 for (const path of ["words.mp4", "notes.md"]) {
187187 const text = await (await fetch(`${base}/alice/big/blob/main/${path}`)).text();
188 expect(text).toContain("over the render cap");
188 expect(text).toContain("Over the 1 MB limit");
189189 expect(text).not.toContain("<video");
190190 }
191191 });
test/fuzz-renderers.test.ts +1 −1
@@ -467,7 +467,7 @@ describe("a file nothing can render", () => {
467467 const name = "weird<name>.blob";
468468 const { content } = blobBody("/o/r", "main", name, { kind: "binary", body, size: body.length });
469469 const page = content.text;
470 expect(page).toContain("3.9 KB · binary data — first 3.9 KB as hex.");
470 expect(page).toContain("3.9 KB · binary data. The forge cannot draw this yet. First 3.9 KB as hex.");
471471 expect(page).not.toContain("weird&lt;name&gt;.blob ·");
472472 // The escaped name reaches the page through the download href only.
473473 expect(page).toContain('href="/o/r/media/main/weird&lt;name&gt;.blob?download=1"');
test/hardening.test.ts +1 −1
@@ -265,7 +265,7 @@ describe("running-forge attacks", () => {
265265
266266 test("oversized blob is refused by stat, not read into memory (C-2)", async () => {
267267 const html = await (await fetch(`${base}/t/r/blob/main/huge.txt`)).text();
268 expect(html).toContain("Too large to render");
268 expect(html).toContain("Over the 1 MB limit for text");
269269 expect(html.length).toBeLessThan(200_000); // the 1.1MB body never rendered
270270 });
271271
test/log-tail-doors.test.ts +20 −2
@@ -52,7 +52,9 @@ beforeAll(async () => {
5252 };
5353 const body = new TextEncoder().encode(big);
5454 const oid = git(["hash-object", "-w", "--stdin"], body);
55 const tree = git(["mktree"], `${git(["ls-tree", "main"])}\n100644 blob ${oid}\tbig.log\n100644 blob ${oid}\tbig.ts\n`);
55 const binOid = git(["hash-object", "-w", "--stdin"], Uint8Array.from({ length: 6_000_000 }, (_, i) => (i * 7 + 1) % 256));
56 const smallOid = git(["hash-object", "-w", "--stdin"], Uint8Array.from({ length: 300 }, (_, i) => (i * 7 + 1) % 256));
57 const tree = git(["mktree"], `${git(["ls-tree", "main"])}\n100644 blob ${oid}\tbig.log\n100644 blob ${oid}\tbig.ts\n100644 blob ${binOid}\tbig.xyz\n100644 blob ${smallOid}\tsmall.bin\n`);
5658 git(["update-ref", "refs/heads/main", git(["commit-tree", tree, "-p", "main", "-m", "big"])]);
5759 }, 60_000);
5860 afterAll(async () => {
@@ -116,7 +118,23 @@ describe("the terminal", () => {
116118
117119 test("a too-large .ts draws the too-large note and no tail", async () => {
118120 const text = await view("big.ts");
119 expect(text).toContain("too large to display");
121 expect(text).toContain("Over the 1 MB limit for text, because the page reads a file whole to show it.");
122 expect(text).toContain("Download");
120123 expect(text).not.toContain("Showing the last");
121124 });
125
126 test("a too-large file of no known kind says the forge cannot draw it and offers the download", async () => {
127 const text = await view("big.xyz");
128 expect(text).toContain("big.xyz");
129 expect(text).toContain("The forge cannot draw this yet.");
130 expect(text).toContain("Download");
131 });
132
133 test("a small binary says the sentence and the download beside its hex dump", async () => {
134 const text = await view("small.bin");
135 expect(text).toContain("Binary file");
136 expect(text).toContain("00000000");
137 expect(text).toContain("cannot draw");
138 expect(text).toContain("Download");
139 });
122140 });
test/log-tail-view.test.ts +1 −1
@@ -93,7 +93,7 @@ describe("log tail", () => {
9393
9494 test("a file with no tail keeps the too-large note; only .log and .txt tail", () => {
9595 const page = String(blobBody("/a/r", "main", "big.log", { kind: "too_large", size: 9_000_000 }).content);
96 expect(page).toContain("Too large to render");
96 expect(page).toContain("Over the 1 MB limit for log files");
9797 expect(tailable("a/x.LOG")).toBe(true);
9898 expect(tailable("n.txt")).toBe(true);
9999 expect(tailable("x.ts")).toBe(false);
test/render-undrawable.test.ts added +51
@@ -0,0 +1,51 @@
1 /** A file the forge cannot draw says so in one sentence and offers the download (#14045). */
2
3 import { describe, expect, test } from "bun:test";
4 import { blobBody } from "../src/web/pages/repo.ts";
5 import { undrawable, UNDRAWN } from "../src/web/render/undrawable.ts";
6
7 const size = (n: number): string => `${n} B`;
8
9 describe("an undrawable file", () => {
10 test("a too-large file of unknown kind says name, size, the sentence and offers the download", () => {
11 const head = Uint8Array.from({ length: 300 }, (_, i) => (i * 7 + 1) % 256);
12 const page = blobBody("/o/r", "main", "dir/mystery.xyz", { kind: "too_large", head, size: 90_000_000 }).content.text;
13 expect(page).toContain('<p class="empty-title">mystery.xyz · 85.8 MB</p>');
14 expect(page).toContain(`${UNDRAWN}`);
15 expect(page).toContain("mystery.xyz?download=1");
16 });
17
18 test("an over-size file of a known kind says its limit and why", () => {
19 const md = blobBody("/o/r", "main", "notes.md", { kind: "too_large", size: 3_000_000 }).content.text;
20 expect(md).toContain("Over the 1 MB limit for markdown files, because the page reads a file whole to draw it.");
21 expect(md).toContain("notes.md?download=1");
22 const img = undrawable("big.png", { size: 60_000_000, head: Uint8Array.of(0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a) }, size);
23 expect(img?.reason).toBe("Over the 50 MB limit for image files, because the browser would have to load it whole.");
24 });
25
26 test("the reason is the note, not the title, so the title stays short", () => {
27 const md = blobBody("/o/r", "main", "notes.md", { kind: "too_large", size: 3_000_000 }).content.text;
28 expect(md).toContain('<p class="empty-title">notes.md · 2.9 MB</p>');
29 expect(md).not.toMatch(/empty-title">[^<]*Over the/);
30 });
31
32 test("it returns parts, so a name holding the separator stays whole", () => {
33 expect(undrawable("d/a · b.xyz", { size: 90_000_000, head: Uint8Array.of(0, 1, 2, 3) }, size)).toEqual({
34 name: "a · b.xyz",
35 size: "90000000 B",
36 reason: UNDRAWN,
37 });
38 });
39
40 test("a binary within the read ceiling says the sentence beside its hex dump and Download", () => {
41 const body = Uint8Array.from({ length: 64 }, (_, i) => (i * 13 + 1) % 256);
42 const page = blobBody("/o/r", "main", "blob.xyz", { kind: "binary", body, size: 64 }).content.text;
43 expect(page).toContain(UNDRAWN);
44 expect(page).toContain("blob.xyz?download=1");
45 expect(page).toContain("hexdump");
46 });
47
48 test("a too-large file within its kind's limit is not called undrawable", () => {
49 expect(undrawable("a.png", { size: 10, head: Uint8Array.of(0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a) }, size)).toBeUndefined();
50 });
51 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs