Public: anyone can read this; the crew can change it.
A notebook over 2 MB opens as its size and a raw link, not as cells #16129
mason/forge:mason/16086-a-notebook-over-2-mb-opens-as-its-size-a → main 3 commits fork 2 threads · 2 unresolved
5 files
changed +59
−22
src/web/pages/repo.ts +3 −1
| @@ -1221,7 +1221,7 @@ const SAID_ABOVE = "Download it to read it."; | ||
| 1221 | 1221 | /** Why an archive too big to list is not listed, as one sentence. */ |
| 1222 | 1222 | function overCap(path: string, blob: Blob): string | undefined { |
| 1223 | 1223 | if (kindByName(path) === "notebook" && blob.size > RENDER_CAP.notebook) |
| 1224 | return `This notebook is over ${RENDER_CAP.notebook / 1_000_000} MB, so it is not drawn as cells (this one is ${fileSize(blob.size)})${blob.kind === "too_large" ? ". Download it to read it" : " and is shown as plain text"}.`; | |
| 1224 | return `This notebook is over ${fileSize(RENDER_CAP.notebook)}, so it is not drawn as cells (this one is ${fileSize(blob.size)})${blob.kind === "too_large" ? ". Download it to read it" : " and is shown as plain text"}.`; | |
| 1225 | 1225 | if (kindByName(path) === "gcode" && blob.size > RENDER_CAP.gcode) |
| 1226 | 1226 | return `G-code, over the ${fileSize(RENDER_CAP.gcode)} the page draws, so its layers are not drawn. Download it to read it.`; |
| 1227 | 1227 | if (blob.kind !== "binary" || fileKind(path, blob.body.subarray(0, HEAD_BYTES)) !== "archive") return undefined; |
| @@ -2112,6 +2112,8 @@ export function capped(blob: Blob, path = ""): Blob { | ||
| 2112 | 2112 | return blob; |
| 2113 | 2113 | // A table file's body is already bounded by its prefix; the renderer draws a window of it. |
| 2114 | 2114 | if (blob.kind !== "too_large" && blob.body.length <= CSV_PREFIX_BYTES && isTablePath(path)) return blob; |
| 2115 | // A notebook is parsed whole, so it is kept up to its own cap. | |
| 2116 | if (blob.kind !== "too_large" && blob.size <= RENDER_CAP.notebook && kindByName(path) === "notebook") return blob; | |
| 2115 | 2117 | // A big JSON file's body is already its prefix; the renderer outlines it. |
| 2116 | 2118 | if (blob.kind === "text" && blob.partial === true && blob.body.length <= JSON_PREFIX_BYTES && isJsonPath(path)) return blob; |
| 2117 | 2119 | return blob.kind !== "too_large" && blob.size > MAX_RENDER_BYTES |
src/web/render/kinds.ts +3 −1
| @@ -147,7 +147,9 @@ export const RENDER_CAP: Readonly<Record<FileKind, number>> = { | ||
| 147 | 147 | wasm: 5 * MB, |
| 148 | 148 | config: 1 * MB, |
| 149 | 149 | markdown: 1 * MB, |
| 150 | notebook: 1 * MB, | |
| 150 | // 2 MiB: over it a notebook stands as its size and a raw link, and the page reads it whole up to | |
| 151 | // it. | |
| 152 | notebook: 2_097_152, | |
| 151 | 153 | patch: 1 * MB, |
| 152 | 154 | log: 1 * MB, |
| 153 | 155 | subtitles: 5 * MB, |
src/web/render/notebook.ts +13
| @@ -8,6 +8,8 @@ | ||
| 8 | 8 | |
| 9 | 9 | import { html, type Html } from "../html.ts"; |
| 10 | 10 | import { renderMarkdown } from "../markdown.ts"; |
| 11 | import { fileSize } from "../ui.ts"; | |
| 12 | import { RENDER_CAP } from "./kinds.ts"; | |
| 11 | 13 | import type { Rendered, RenderInput } from "./renderers.ts"; |
| 12 | 14 | |
| 13 | 15 | type Json = Record<string, unknown>; |
| @@ -32,6 +34,11 @@ const MAX_CELLS = 2_000; | ||
| 32 | 34 | */ |
| 33 | 35 | const MAX_IMAGE_B64 = Math.ceil((1_000_000 * 4) / 3); |
| 34 | 36 | |
| 37 | /** | |
| 38 | * A notebook body over this many bytes (2 MB) is not parsed: it stands as its size and a raw link. | |
| 39 | */ | |
| 40 | export const MAX_NOTEBOOK_BYTES = RENDER_CAP.notebook; | |
| 41 | ||
| 35 | 42 | const stripAnsi = (s: string): string => s.replace(/\x1b\[[0-9;]*[A-Za-z]/g, ""); |
| 36 | 43 | |
| 37 | 44 | /** One saved output, as data: the HTML page and the terminal both draw from this. */ |
| @@ -114,6 +121,12 @@ function cellHtml(c: NbCell): Html { | ||
| 114 | 121 | } |
| 115 | 122 | |
| 116 | 123 | export function notebook(input: RenderInput): Rendered { |
| 124 | if (Math.max(input.size, input.body.length) > MAX_NOTEBOOK_BYTES) | |
| 125 | return { | |
| 126 | lines: 0, | |
| 127 | summary: fileSize(input.size), | |
| 128 | content: html`<p class="muted">This notebook is ${fileSize(input.size)}, over the ${fileSize(MAX_NOTEBOOK_BYTES)} the forge draws as cells. <a href="${input.download}">Open the raw file.</a></p>`, | |
| 129 | }; | |
| 117 | 130 | const parsed = parseNotebook(input.body); |
| 118 | 131 | if ("error" in parsed) return { content: html``, lines: 0, fallback: parsed.error }; |
| 119 | 132 | return { |
test/blob-notebook-page.test.ts +6 −19
| @@ -12,7 +12,6 @@ import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | ||
| 12 | 12 | import { serve, type Servers } from "../src/http/server.ts"; |
| 13 | 13 | import { seedAdmin } from "./helpers/admin.ts"; |
| 14 | 14 | import { quietLogs } from "./helpers/globals.ts"; |
| 15 | import { fileSize } from "../src/web/ui.ts"; | |
| 16 | 15 | |
| 17 | 16 | quietLogs(); |
| 18 | 17 | |
| @@ -21,7 +20,6 @@ describe("a .ipynb on its blob page", () => { | ||
| 21 | 20 | let app: App; |
| 22 | 21 | let servers: Servers; |
| 23 | 22 | let base = ""; |
| 24 | let bigSize = ""; | |
| 25 | 23 | |
| 26 | 24 | beforeAll(async () => { |
| 27 | 25 | dataDir = await mkdtemp(join(tmpdir(), "forge-blob-ipynb-")); |
| @@ -35,13 +33,12 @@ describe("a .ipynb on its blob page", () => { | ||
| 35 | 33 | const small = JSON.stringify({ |
| 36 | 34 | cells: [{ cell_type: "code", execution_count: 1, source: "1", outputs: [{ output_type: "execute_result", data: { "text/html": "<script>alert(1)</script>" } }] }], |
| 37 | 35 | }); |
| 38 | const big = JSON.stringify({ cells: [{ cell_type: "markdown", source: "x".repeat(1_100_000) }] }); | |
| 39 | bigSize = fileSize(new TextEncoder().encode(big).length); | |
| 36 | const mid = JSON.stringify({ cells: [{ cell_type: "markdown", source: "x".repeat(1_100_000) }] }); | |
| 40 | 37 | const made = await commitToBranch(app, a.value, created.value, { |
| 41 | 38 | message: "notebooks", |
| 42 | 39 | files: [ |
| 43 | 40 | { path: "ok.ipynb", content: small }, |
| 44 | { path: "big.ipynb", content: big }, | |
| 41 | { path: "mid.ipynb", content: mid }, | |
| 45 | 42 | { path: "bad.ipynb", content: "{not json" }, |
| 46 | 43 | ], |
| 47 | 44 | }); |
| @@ -61,20 +58,10 @@ describe("a .ipynb on its blob page", () => { | ||
| 61 | 58 | expect(text).not.toContain("<script>alert(1)"); |
| 62 | 59 | }); |
| 63 | 60 | |
| 64 | test("over 1 MB, the page says why it is not drawn as cells", async () => { | |
| 65 | const res = await fetch(`${base}/alice/p/blob/main/big.ipynb`); | |
| 66 | const text = await res.text(); | |
| 67 | expect(res.status).toBe(200); | |
| 68 | // The notebook's own sentence says the limit, once; the generic reason line does not repeat it | |
| 69 | // (#15719). | |
| 70 | expect(text).toContain("This notebook is over 1 MB, so it is not drawn as cells"); | |
| 71 | expect(text).not.toContain("data-plain-why"); | |
| 72 | expect(text).not.toContain("In ["); | |
| 73 | // The sentence names this file's size, and the raw and download links are on the page. | |
| 74 | expect(text).toContain(`(this one is ${bigSize})`); | |
| 75 | expect(bigSize).toMatch(/^1\.\d MB$/); | |
| 76 | expect(text).toContain("/alice/p/raw/main/big.ipynb"); | |
| 77 | expect(text).toContain("/alice/p/media/main/big.ipynb?download=1"); | |
| 61 | test("between 1 and 2 MB, a notebook is still drawn as cells", async () => { | |
| 62 | const text = await (await fetch(`${base}/alice/p/blob/main/mid.ipynb`)).text(); | |
| 63 | expect(text).not.toContain("not drawn as cells"); | |
| 64 | expect(text).toContain("nb-cell"); | |
| 78 | 65 | }); |
| 79 | 66 | |
| 80 | 67 | test("invalid JSON says why on the page", async () => { |
test/render-notebook.test.ts +34 −1
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | /** A notebook as cells and outputs: hostile HTML output stays inert text. */ |
| 2 | 2 | import { describe, expect, test } from "bun:test"; |
| 3 | import { notebook } from "../src/web/render/notebook.ts"; | |
| 3 | import { MAX_NOTEBOOK_BYTES, notebook } from "../src/web/render/notebook.ts"; | |
| 4 | 4 | import { fileKind, RENDER_CAP } from "../src/web/render/kinds.ts"; |
| 5 | 5 | import { RENDERERS, rendererFor } from "../src/web/render/renderers.ts"; |
| 6 | 6 | |
| @@ -57,6 +57,15 @@ describe("notebooks", () => { | ||
| 57 | 57 | }); |
| 58 | 58 | }); |
| 59 | 59 | |
| 60 | describe("a notebook between the old and the new cap", () => { | |
| 61 | const mb = (n: number) => ({ kind: "text", size: n, body: enc("{}") }) as never; | |
| 62 | test("1 to 2 MB reaches the renderer, over 2 MB is not handed to it", () => { | |
| 63 | expect(RENDER_CAP.notebook).toBe(MAX_NOTEBOOK_BYTES); | |
| 64 | expect(rendererFor("a.ipynb", mb(1_500_000))?.render).toBe(notebook); | |
| 65 | expect(rendererFor("a.ipynb", mb(2_500_000))).toBeUndefined(); | |
| 66 | }); | |
| 67 | }); | |
| 68 | ||
| 60 | 69 | describe("notebook edge cases", () => { |
| 61 | 70 | test("JPEG output is a picture and an error loses its colour codes", () => { |
| 62 | 71 | const out = String( |
| @@ -92,3 +101,27 @@ describe("notebook image size", () => { | ||
| 92 | 101 | expect(String(draw(nb("AAAA")).content)).toContain("data:image/png;base64,AAAA"); |
| 93 | 102 | }); |
| 94 | 103 | }); |
| 104 | ||
| 105 | describe("notebook size limit", () => { | |
| 106 | const padded = (bytes: number): string => { | |
| 107 | const base = JSON.stringify({ cells: [{ cell_type: "markdown", source: "hi" }], pad: "" }); | |
| 108 | return base.slice(0, -2) + "a".repeat(bytes - base.length) + '"}'; | |
| 109 | }; | |
| 110 | const at = (s: string) => notebook({ path: "a.ipynb", size: s.length, body: enc(s), media: "/m", download: "/o/r/raw/main/a.ipynb?x=1&y=2" }); | |
| 111 | ||
| 112 | test("one byte over 2 MB is a note with the size and the raw link, no cells", () => { | |
| 113 | const r = at(padded(MAX_NOTEBOOK_BYTES + 1)); | |
| 114 | const out = String(r.content); | |
| 115 | expect(MAX_NOTEBOOK_BYTES).toBe(2_097_152); | |
| 116 | expect(out).toContain("This notebook is"); | |
| 117 | expect(out).toContain('href="/o/r/raw/main/a.ipynb?x=1&y=2"'); | |
| 118 | expect(out).not.toContain("nb-cell"); | |
| 119 | expect(r.fallback).toBeUndefined(); | |
| 120 | }); | |
| 121 | ||
| 122 | test("exactly 2 MB still draws its cells", () => { | |
| 123 | const s = padded(MAX_NOTEBOOK_BYTES); | |
| 124 | expect(s.length).toBe(MAX_NOTEBOOK_BYTES); | |
| 125 | expect(String(at(s).content)).toContain("nb-cell"); | |
| 126 | }); | |
| 127 | }); |
new above
offline
Medium: unreachable from the page. RENDER_CAP.notebook (1 MB) rejects the blob before notebook() runs, so this note is never shown to a reader. Raise the cap to MAX_NOTEBOOK_BYTES (or derive it from it) in kinds.ts and test via the real render path.