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

Closed opened this
Closed without merging

mason/forge:mason/16086-a-notebook-over-2-mb-opens-as-its-size-a → main 3 commits fork 2 threads · 2 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
5 files changed +59 −22
src/web/pages/repo.ts +3 −1
@@ -1221,7 +1221,7 @@ const SAID_ABOVE = "Download it to read it.";
12211221 /** Why an archive too big to list is not listed, as one sentence. */
12221222 function overCap(path: string, blob: Blob): string | undefined {
12231223 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"}.`;
12251225 if (kindByName(path) === "gcode" && blob.size > RENDER_CAP.gcode)
12261226 return `G-code, over the ${fileSize(RENDER_CAP.gcode)} the page draws, so its layers are not drawn. Download it to read it.`;
12271227 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 {
21122112 return blob;
21132113 // A table file's body is already bounded by its prefix; the renderer draws a window of it.
21142114 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;
21152117 // A big JSON file's body is already its prefix; the renderer outlines it.
21162118 if (blob.kind === "text" && blob.partial === true && blob.body.length <= JSON_PREFIX_BYTES && isJsonPath(path)) return blob;
21172119 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>> = {
147147 wasm: 5 * MB,
148148 config: 1 * MB,
149149 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,
151153 patch: 1 * MB,
152154 log: 1 * MB,
153155 subtitles: 5 * MB,
src/web/render/notebook.ts +13
@@ -8,6 +8,8 @@
88
99 import { html, type Html } from "../html.ts";
1010 import { renderMarkdown } from "../markdown.ts";
11 import { fileSize } from "../ui.ts";
12 import { RENDER_CAP } from "./kinds.ts";
1113 import type { Rendered, RenderInput } from "./renderers.ts";
1214
1315 type Json = Record<string, unknown>;
@@ -32,6 +34,11 @@ const MAX_CELLS = 2_000;
3234 */
3335 const MAX_IMAGE_B64 = Math.ceil((1_000_000 * 4) / 3);
3436
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
3542 const stripAnsi = (s: string): string => s.replace(/\x1b\[[0-9;]*[A-Za-z]/g, "");
3643
3744 /** One saved output, as data: the HTML page and the terminal both draw from this. */
@@ -114,6 +121,12 @@ function cellHtml(c: NbCell): Html {
114121 }
115122
116123 export function notebook(input: RenderInput): Rendered {
outdated

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.

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 };
117130 const parsed = parseNotebook(input.body);
118131 if ("error" in parsed) return { content: html``, lines: 0, fallback: parsed.error };
119132 return {
test/blob-notebook-page.test.ts +6 −19
@@ -12,7 +12,6 @@ import { commitToBranch, createRepo } from "../src/actions/repos.ts";
1212 import { serve, type Servers } from "../src/http/server.ts";
1313 import { seedAdmin } from "./helpers/admin.ts";
1414 import { quietLogs } from "./helpers/globals.ts";
15 import { fileSize } from "../src/web/ui.ts";
1615
1716 quietLogs();
1817
@@ -21,7 +20,6 @@ describe("a .ipynb on its blob page", () => {
2120 let app: App;
2221 let servers: Servers;
2322 let base = "";
24 let bigSize = "";
2523
2624 beforeAll(async () => {
2725 dataDir = await mkdtemp(join(tmpdir(), "forge-blob-ipynb-"));
@@ -35,13 +33,12 @@ describe("a .ipynb on its blob page", () => {
3533 const small = JSON.stringify({
3634 cells: [{ cell_type: "code", execution_count: 1, source: "1", outputs: [{ output_type: "execute_result", data: { "text/html": "<script>alert(1)</script>" } }] }],
3735 });
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) }] });
4037 const made = await commitToBranch(app, a.value, created.value, {
4138 message: "notebooks",
4239 files: [
4340 { path: "ok.ipynb", content: small },
44 { path: "big.ipynb", content: big },
41 { path: "mid.ipynb", content: mid },
4542 { path: "bad.ipynb", content: "{not json" },
4643 ],
4744 });
@@ -61,20 +58,10 @@ describe("a .ipynb on its blob page", () => {
6158 expect(text).not.toContain("<script>alert(1)");
6259 });
6360
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");
7865 });
7966
8067 test("invalid JSON says why on the page", async () => {
test/render-notebook.test.ts +34 −1
@@ -1,6 +1,6 @@
11 /** A notebook as cells and outputs: hostile HTML output stays inert text. */
22 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";
44 import { fileKind, RENDER_CAP } from "../src/web/render/kinds.ts";
55 import { RENDERERS, rendererFor } from "../src/web/render/renderers.ts";
66
@@ -57,6 +57,15 @@ describe("notebooks", () => {
5757 });
5858 });
5959
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
6069 describe("notebook edge cases", () => {
6170 test("JPEG output is a picture and an error loses its colour codes", () => {
6271 const out = String(
@@ -92,3 +101,27 @@ describe("notebook image size", () => {
92101 expect(String(draw(nb("AAAA")).content)).toContain("data:image/png;base64,AAAA");
93102 });
94103 });
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&amp;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 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs