From 3b79b1858e2feb1ebc6b549d7e418f697ba1eabc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Wed, 18 Nov 2026 08:37:17 +0000 Subject: [PATCH] fix(producer): inline a local font only while the page stays within its size limit (#8152) * test(producer): a font many rules name stays within the page's size limit * fix(producer): inline a local font only while the page stays within its size limit Only url() sources inside @font-face rules are replaced, and every inlined copy is charged to the render's expansion budget. Once the budget is spent, the remaining references keep loading the font from its file. * fix(producer): keep the font anchoring test and name the fit-or-skip charge --- .../src/services/compositionSizeLimit.ts | 6 + .../services/htmlCompiler.fontEmbed.test.ts | 70 ++++---- .../services/htmlCompiler.sizeLimit.test.ts | 26 +++ .../producer/src/services/htmlCompiler.ts | 162 ++++++++++-------- 4 files changed, 153 insertions(+), 111 deletions(-) diff --git a/packages/producer/src/services/compositionSizeLimit.ts b/packages/producer/src/services/compositionSizeLimit.ts index b3ffa71f93..a121961d98 100644 --- a/packages/producer/src/services/compositionSizeLimit.ts +++ b/packages/producer/src/services/compositionSizeLimit.ts @@ -87,6 +87,12 @@ export class CompositionExpansionBudget { }); } + chargeGeneratedIfItFits(bytes: number): boolean { + if (this.generated + bytes > MAX_EXPANDED_COMPOSITION_BYTES) return false; + this.generated += bytes; + return true; + } + charge(file: string, bytes: number): void { this.bytes += bytes; this.mounts += 1; diff --git a/packages/producer/src/services/htmlCompiler.fontEmbed.test.ts b/packages/producer/src/services/htmlCompiler.fontEmbed.test.ts index 494f355259..9b65bcbd0a 100644 --- a/packages/producer/src/services/htmlCompiler.fontEmbed.test.ts +++ b/packages/producer/src/services/htmlCompiler.fontEmbed.test.ts @@ -1,49 +1,41 @@ +import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { dirname, join } from "node:path"; import { describe, expect, it } from "vitest"; -import { urlOccurrenceRe } from "./htmlCompiler.js"; +import { compileForRender } from "./htmlCompiler.js"; /** - * The embed step used `result.replaceAll(localPath, dataUri)` — a bare - * substring rewrite over the whole compiled document. These pin the collision - * that made unsafe: it is invisible in ordinary projects and easy to - * reintroduce, because the naive form passes every test that uses one font. + * A whole-document substring rewrite of a font path also rewrote the tail of a longer path and the + * same text in a comment. These are invisible in a project with one font. */ describe("font embed url() anchoring", () => { - const DATA_URI = "data:font/woff2;base64,AAAA"; - const replace = (html: string, path: string) => - html.replace(urlOccurrenceRe(path), `url("${DATA_URI}")`); + const fonts = { + "img/logo.ttf": "short", + "assets/img/logo.ttf": "long", + "fonts/x+y[1].ttf": "meta", + }; + const dataUri = (text: string) => + `url("data:font/ttf;base64,${Buffer.from(text).toString("base64")}")`; - it("rewrites the exact occurrence, quoted or bare", () => { - expect(replace(`src: url("fonts/x.ttf")`, "fonts/x.ttf")).toBe(`src: url("${DATA_URI}")`); - expect(replace(`src: url(fonts/x.ttf)`, "fonts/x.ttf")).toBe(`src: url("${DATA_URI}")`); - expect(replace(`src: url('fonts/x.ttf')`, "fonts/x.ttf")).toBe(`src: url("${DATA_URI}")`); - }); + it("replaces each @font-face source with its own file and nothing else", async () => { + const dir = mkdtempSync(join(tmpdir(), "hf-font-anchor-")); + for (const [path, text] of Object.entries(fonts)) { + mkdirSync(dirname(join(dir, path)), { recursive: true }); + writeFileSync(join(dir, path), text); + } + const faces = Object.keys(fonts) + .map((path, i) => `@font-face { font-family: "F${i}"; src: url("${path}"); }`) + .join("\n"); + writeFileSync( + join(dir, "index.html"), + ` +
Text
+`, + ); - it("does not corrupt a longer url whose tail matches the embedded path", () => { - // The reported corruption: embedding `fonts/x.ttf` rewrote the tail of an - // untouched absolute rule, producing `url("file:///abs/")`. - const html = `a { src: url("fonts/x.ttf") } b { src: url("file:///abs/fonts/x.ttf") }`; - const out = replace(html, "fonts/x.ttf"); - expect(out).toContain(`a { src: url("${DATA_URI}") }`); - expect(out).toContain(`b { src: url("file:///abs/fonts/x.ttf") }`); - }); + const { html } = await compileForRender(dir, join(dir, "index.html"), dir); - it("does not corrupt a sibling whose path is a suffix of another project path", () => { - // No file:// needed. Any two paths where one is a suffix of the other - // collide under a bare substring replace. - const html = `a { src: url("img/logo.ttf") } b { src: url("assets/img/logo.ttf") }`; - const out = replace(html, "img/logo.ttf"); - expect(out).toContain(`a { src: url("${DATA_URI}") }`); - expect(out).toContain(`b { src: url("assets/img/logo.ttf") }`); - }); - - it("leaves the path alone outside a url() wrapper", () => { - const html = `/* see fonts/x.ttf for the source */ a { src: url("fonts/x.ttf") }`; - const out = replace(html, "fonts/x.ttf"); - expect(out).toContain("/* see fonts/x.ttf for the source */"); - }); - - it("treats regex metacharacters in a path literally", () => { - const html = `a { src: url("fonts/x+y(1).ttf") }`; - expect(replace(html, "fonts/x+y(1).ttf")).toBe(`a { src: url("${DATA_URI}") }`); + for (const text of Object.values(fonts)) expect(html).toContain(dataUri(text)); + expect(html).toContain("/* see img/logo.ttf */"); }); }); diff --git a/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts b/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts index f3fe3c569d..db73bd01ca 100644 --- a/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts +++ b/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts @@ -319,3 +319,29 @@ describe("variable-driven composition styles and media sources", () => { expect(await srcFor("https://example.com/x.mp4\n")).toBe("https://example.com/x.mp4"); }); }); + +describe("local font embedding", () => { + const LARGEST_INLINED_FONT_BYTES = 5 * 1024 * 1024; + const faces = (count: number) => + Array.from( + { length: count }, + (_, i) => `@font-face { font-family: "F${i}"; src: url("big.ttf"); }`, + ).join("\n"); + + it("inlines a font that many rules name only while the page stays within the limit", async () => { + const dir = project({ + "index.html": ROOT.replace( + "", + ``, + ), + "scene.html": SCENE, + }); + writeFileSync(join(dir, "big.ttf"), Buffer.alloc(LARGEST_INLINED_FONT_BYTES, 1)); + const { html } = await compileForRender(dir, join(dir, "index.html"), dir); + const inlined = html.split("data:font/ttf;base64,").length - 1; + expect(inlined).toBeGreaterThan(0); + expect(html.length).toBeLessThan(MAX_EXPANDED_COMPOSITION_BYTES + 64 * 1024); + expect(html.split('url("big.ttf")').length - 1).toBe(8 - inlined + 1); + expect(html).toContain('.mark { background: url("big.ttf"); }'); + }); +}); diff --git a/packages/producer/src/services/htmlCompiler.ts b/packages/producer/src/services/htmlCompiler.ts index ff90566ac1..73fff0bf65 100644 --- a/packages/producer/src/services/htmlCompiler.ts +++ b/packages/producer/src/services/htmlCompiler.ts @@ -2043,15 +2043,6 @@ function localizeFontFaceUrls( ); } -/** - * Match one `url()` occurrence, with or without quotes, for a literal - * path. Exported for tests: the suffix-collision it prevents is invisible in - * ordinary projects and easy to reintroduce. - */ -export function urlOccurrenceRe(localPath: string): RegExp { - const escaped = localPath.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); - return new RegExp(`url\\((["']?)${escaped}\\1\\)`, "g"); -} // Base64 expands bytes by ~33%, then immutable HTML replacements retain more // string copies while compiling. Files up to and including 5 MiB remain inline; // the first byte above that stays file-backed. This conservative ceiling keeps @@ -2075,81 +2066,107 @@ async function readLocalFont(absPath: string): Promise { return { kind: "inline", buffer: Buffer.concat(chunks, totalBytes) }; } -// fallow-ignore-next-line complexity +/** The data URI a project font path embeds as, or undefined when it stays a file. */ +async function localFontDataUri( + localPath: string, + projectDir: string, + assetRoot: string | undefined, + dataUriByAbsolutePath: Map, + log: ProducerLogger, +): Promise { + const absPath = localPath.startsWith("/") ? localPath : resolve(projectDir, localPath); + if (!isPathInside(absPath, projectDir) || escapesAssetRoot(absPath, assetRoot)) return undefined; + // A project font that is a link leading outside obeys the same policy as the file server. + if (refusesOutsideLink(projectDir, absPath, (line) => log.warn(`[Compiler] ${line}`))) { + return undefined; + } + if (isPrivateCompositionFile(projectDir, absPath)) return undefined; + if (dataUriByAbsolutePath.has(absPath)) return dataUriByAbsolutePath.get(absPath); + const ext = absPath.match(/\.(woff2?|ttf|otf|ttc)$/i)?.[1]?.toLowerCase() ?? "ttf"; + try { + const font = await readLocalFont(absPath); + if (font.kind === "file-backed") { + dataUriByAbsolutePath.set(absPath, undefined); + defaultLogger.debug( + `[Compiler] Kept large local font file-backed: ${localPath} (> ${(MAX_LOCAL_FONT_DATA_URI_BYTES / 1024 / 1024).toFixed(1)} MB)`, + ); + return undefined; + } + const { fontToDataUri } = await import("@hyperframes/core/fonts/embed"); + const dataUri = await fontToDataUri(font.buffer, ext, { compressToWoff2: false }); + dataUriByAbsolutePath.set(absPath, dataUri); + defaultLogger.debug( + `[Compiler] Embedded local font file: ${localPath} (${(font.buffer.length / 1024).toFixed(0)} KB → data URI)`, + ); + return dataUri; + } catch (error) { + // Keep the original path: a font that cannot be read must not fail the render, but say why + // it renders in a fallback typeface. + defaultLogger.warn( + `[Compiler] Could not embed local font ${localPath}: ${ + error instanceof Error ? error.message : String(error) + }`, + ); + return undefined; + } +} + +/** `whole` with its `inner` text, which ends `tail` characters before its end, replaced. */ +const replaceInner = (whole: string, inner: string, tail: number, next: string) => + whole.slice(0, whole.length - tail - inner.length) + next + whole.slice(whole.length - tail); + +/** + * Inline the project fonts that `".length, + replaceMatches(css, fontFaceBlockMatches(css), (face, body) => + replaceInner(face, body, 1, replaceMatches(body, localFontFaceUrlMatches(body), embed)), + ), + ), + ); } /** @@ -2518,6 +2535,7 @@ export async function compileForRender( htmlWithPreparedGifs, projectDir, assetRoot, + budget, options.log, );