From 5d547bbccd04263fd3d668602f349f3364205348 Mon Sep 17 00:00:00 2001 From: dawn Date: Sat, 31 Oct 2026 03:16:07 +0300 Subject: [PATCH] web/blob: ship a rendered file's text once, in its markup Signed-off-by: dawn --- web/src/hooks.ts | 6 ++ web/src/lib/api/blob.test.ts | 68 ++++++++++++++++++- web/src/lib/api/blob.ts | 35 ++++++++++ web/src/lib/components/repo/BlobHeader.svelte | 2 +- web/src/lib/components/repo/BlobView.svelte | 27 +++++++- .../components/repo/PierreFile.stories.svelte | 63 +++++++++++++++++ web/src/lib/components/repo/PierreFile.svelte | 26 ++++--- web/src/lib/components/repo/pierre.ts | 22 ++++++ .../blob/[ref]/[...path]/+page.server.ts | 8 ++- 9 files changed, 240 insertions(+), 17 deletions(-) diff --git a/web/src/hooks.ts b/web/src/hooks.ts index fba1bd3b4..34996de89 100644 --- a/web/src/hooks.ts +++ b/web/src/hooks.ts @@ -1 +1,7 @@ import "#lib/twMergeConfig.ts"; +import type { Transport } from "@sveltejs/kit/hooks"; +import { blobTransport } from "#lib/api/blob.ts"; + +export const transport: Transport = { + RepoBlobView: blobTransport +}; diff --git a/web/src/lib/api/blob.test.ts b/web/src/lib/api/blob.test.ts index 65ad22757..88000b66a 100644 --- a/web/src/lib/api/blob.test.ts +++ b/web/src/lib/api/blob.test.ts @@ -1,5 +1,13 @@ import { describe, expect, it } from "vitest"; -import { contentStats, formatBytes, MAX_INLINE_SIZE } from "#lib/api/blob.ts"; +import { + blobTransport, + contentStats, + formatBytes, + MAX_INLINE_SIZE, + renderedInPage, + textCheck, + type RepoBlobView +} from "#lib/api/blob.ts"; describe("contentStats", () => { it("counts an empty string as no lines and no bytes", () => { @@ -34,3 +42,61 @@ describe("formatBytes", () => { expect(formatBytes(999_999)).toBe("1.0 MB"); }); }); + +describe("blobTransport", () => { + const blob = (overrides: Partial = {}): RepoBlobView => ({ + ref: "main", + path: "go.mod", + kind: "code", + sizeLabel: "12 B", + lines: 1, + contents: "module x\n", + renderedHtml: null, + fileTooLarge: false, + defaultView: "code", + submodule: null, + lastCommit: null, + ...overrides + }); + + it("leaves the text of a server rendered code view out of the data", () => { + expect(blobTransport.encode(renderedInPage(blob()))).toEqual([ + blob({ contents: null, contentsCheck: textCheck("module x\n") }) + ]); + }); + + it("keeps the text of a blob nobody marked", () => { + expect(blobTransport.encode(blob())).toBe(false); + }); + + it("keeps the text when the page renders markup instead", () => { + const markup = blob({ kind: "markup", defaultView: "rendered", renderedHtml: "

x

" }); + expect(blobTransport.encode(renderedInPage(markup))).toBe(false); + }); + + it("keeps text with carriage returns or NUL", () => { + expect(blobTransport.encode(renderedInPage(blob({ contents: "a\r\nb\r\n" })))).toBe(false); + expect(blobTransport.encode(renderedInPage(blob({ contents: "a\u0000b\n" })))).toBe(false); + }); + + it("decodes to the view it encoded", () => { + const encoded = blob({ contents: null, contentsCheck: textCheck("module x\n") }); + expect(blobTransport.decode([encoded])).toEqual(encoded); + }); +}); + +describe("textCheck", () => { + it("matches the same text", () => { + expect(textCheck("a\tb\n")).toBe(textCheck("a\tb\n")); + }); + + it("misses text that differs by one character or one carriage return", () => { + const check = textCheck("a\r\nb\n"); + expect(textCheck("a\nb\n")).not.toBe(check); + expect(textCheck("a\r\nc\n")).not.toBe(check); + }); + + it("misses a same-length swap", () => { + expect(textCheck("ba")).not.toBe(textCheck("ab")); + }); +}); diff --git a/web/src/lib/api/blob.ts b/web/src/lib/api/blob.ts index fb2236b20..90e21894c 100644 --- a/web/src/lib/api/blob.ts +++ b/web/src/lib/api/blob.ts @@ -1,3 +1,4 @@ +import type { Transporter } from "@sveltejs/kit/hooks"; import type { CommitSummary } from "#lib/api/repo.ts"; import { isMarkdownFile } from "#lib/markup/index.ts"; @@ -17,8 +18,42 @@ export interface RepoBlobView { defaultView: BlobViewMode; submodule: { name: string; url: string } | null; lastCommit: CommitSummary | null; + // only set when contents is null. the browser checks its read-back against it + contentsCheck?: string; } +// not crypto, a collision only buys the text that is already on screen +export const textCheck = (text: string): string => { + let h1 = 0xdeadbeef; + let h2 = 0x41c6ce57; + for (let i = 0; i < text.length; i++) { + const c = text.charCodeAt(i); + h1 = Math.imul(h1 ^ c, 2654435761); + h2 = Math.imul(h2 ^ c, 1597334677); + } + h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909); + h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909); + return `${text.length}:${(4294967296 * (2097151 & h2) + (h1 >>> 0)).toString(36)}`; +}; + +const inPage = new WeakSet(); + +// text with \r or NUL never survives pierre's markup +export const renderedInPage = (blob: RepoBlobView): RepoBlobView => { + if (blob.defaultView === "code" && blob.contents !== null && !/[\r\0]/.test(blob.contents)) + inPage.add(blob); + return blob; +}; + +export const blobTransport: Transporter = { + encode: (blob) => + inPage.has(blob) && + blob.contents !== null && [ + { ...blob, contents: null, contentsCheck: textCheck(blob.contents) } + ], + decode: ([blob]) => blob +}; + // same 1MiB inline cap as the appview's maxBlobSize export const MAX_INLINE_SIZE = 1 << 20; diff --git a/web/src/lib/components/repo/BlobHeader.svelte b/web/src/lib/components/repo/BlobHeader.svelte index 04a223a89..8c1acf74e 100644 --- a/web/src/lib/components/repo/BlobHeader.svelte +++ b/web/src/lib/components/repo/BlobHeader.svelte @@ -47,7 +47,7 @@ blame?: boolean; blameLoading?: boolean; blameError?: string | null; - copyText?: string | null; + copyText?: string | (() => string) | null; } let { diff --git a/web/src/lib/components/repo/BlobView.svelte b/web/src/lib/components/repo/BlobView.svelte index b364f6270..b402497cc 100644 --- a/web/src/lib/components/repo/BlobView.svelte +++ b/web/src/lib/components/repo/BlobView.svelte @@ -9,6 +9,7 @@ import { languageForPath } from "#lib/components/repo/languageColors.ts"; import LastCommitPanel from "#lib/components/repo/LastCommitPanel.svelte"; import PierreFile from "#lib/components/repo/PierreFile.svelte"; + import { pageText } from "#lib/components/repo/pierre.ts"; import { lineAnchor, parseLineAnchor } from "#lib/components/repo/lineAnchor.ts"; import { baseName, fileHistoryHref } from "#lib/components/repo/urls.ts"; import { createBlame, type BlameSource } from "#lib/components/repo/blame/request.svelte.ts"; @@ -141,6 +142,26 @@ let codeHost: HTMLElement | undefined = $state(); + // keyed on the blob object so a new file never shows the last one's text + let readBack = $state.raw<{ blob: RepoBlobView; text: string } | null>(null); + const contents = $derived(blob.contents ?? (readBack?.blob === blob ? readBack.text : null)); + + $effect(() => { + const host = codeHost?.querySelector("diffs-container"); + const check = blob.contentsCheck; + if (!host || !check || contents !== null) return; + const read = blob; + pageText(host, check, rawHref).then( + (text) => (readBack = { blob: read, text }), + () => undefined + ); + }); + + const copyChecked = (): string => { + if (contents === null) throw new Error("file text unavailable"); + return contents; + }; + $effect(() => { const host = codeHost; if (!host || wrap) return; @@ -170,7 +191,7 @@ showBlame={blameSource !== undefined} blameLoading={blameRequest.loading} blameError={blameRequest.error} - copyText={blob.contents} + copyText={blob.contentsCheck ? copyChecked : blob.contents} /> {#if blameRequest.error} @@ -231,11 +252,11 @@ {@html blob.renderedHtml} - {:else if blob.contents !== null} + {:else if contents !== null || blob.contentsCheck}
import { defineMeta, type StoryContext } from "@storybook/addon-svelte-csf"; import { expect, waitFor } from "storybook/test"; + import { preloadFile } from "@pierre/diffs/ssr"; import PierreFile from "#lib/components/repo/PierreFile.svelte"; + import { pageText, pierreFileOptions } from "#lib/components/repo/pierre.ts"; + import { textCheck } from "#lib/api/blob.ts"; const contents = `import { serve } from "#lib/components/repo/server"; @@ -25,6 +28,47 @@ export const main = () => { ); }; + const hostile: Record = { + tabs: "\tx\t\ty\t\n", + "trailing spaces": "a \nb \n", + nbsp: "a\u00a0b\u00a0\n", + "zero width": "a\u200bb\u200c\u200d\ufeff\n", + combining: "e\u0301 n\u0303\n", + // svelte flags bidi controls even as escapes + rtl: `\u05e9\u05dc\u05d5\u05dd ${String.fromCharCode(0x202e)}abc${String.fromCharCode(0x202c)} \u0645\u0631\u062d\u0628\u0627\n`, + crlf: "a\r\nb\r\n", + "lone cr": "a\rb\n", + "cr mix": "a\r\nb\rc\n\r", + "no final newline": "abc", + empty: "", + "only newlines": "\n\n\n", + "long line": `${"x".repeat(20000)}\n`, + "html-looking": "