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
This commit is contained in:
Miguel Ángel
2026-11-18 08:37:17 +00:00
committed by GitHub
parent e623031f3a
commit 3b79b1858e
4 changed files with 153 additions and 111 deletions
@@ -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;
@@ -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"),
`<!doctype html><html><head><style>/* see img/logo.ttf */\n${faces}</style></head><body>
<div data-composition-id="root" data-width="640" data-height="360" data-duration="1">Text</div>
</body></html>`,
);
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/<data-uri>")`.
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 */");
});
});
@@ -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(
"<html>",
`<html><head><style>${faces(8)}\n.mark { background: url("big.ttf"); }</style></head>`,
),
"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"); }');
});
});
+90 -72
View File
@@ -2043,15 +2043,6 @@ function localizeFontFaceUrls(
);
}
/**
* Match one `url(<path>)` 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<LocalFontRead> {
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<string, string | undefined>,
log: ProducerLogger,
): Promise<string | undefined> {
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 `<style>` `@font-face` sources name as data URIs. Every copy is
* charged to the render's budget, so a font named by many rules stops being inlined once the
* budget is spent and the rest keep loading it from its file.
*/
async function embedLocalFontFaces(
html: string,
projectDir: string,
assetRoot: string | undefined,
budget: CompositionExpansionBudget,
log: ProducerLogger = defaultLogger,
): Promise<string> {
const { fontToDataUri: toDataUri } = await import("@hyperframes/core/fonts/embed");
let result = html;
const embeddedPaths = new Set<string>();
const dataUriByAbsolutePath = new Map<string, string>();
const fileBackedAbsolutePaths = new Set<string>();
const dataUriByPath = new Map<string, string | undefined>();
const dataUriByAbsolutePath = new Map<string, string | undefined>();
for (const { parts: style } of styleBlockMatches(html)) {
for (const { parts: face } of fontFaceBlockMatches(style[1])) {
const block = face[1];
for (const { parts } of localFontFaceUrlMatches(block)) {
for (const { parts } of localFontFaceUrlMatches(face[1])) {
const localPath = parts[1];
if (!localPath || embeddedPaths.has(localPath)) continue;
const absPath = localPath.startsWith("/") ? localPath : resolve(projectDir, localPath);
if (!isPathInside(absPath, projectDir) || escapesAssetRoot(absPath, assetRoot)) continue;
// 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}`))) {
embeddedPaths.add(localPath);
continue;
}
if (isPrivateCompositionFile(projectDir, absPath)) continue;
const ext = absPath.match(/\.(woff2?|ttf|otf|ttc)$/i)?.[1]?.toLowerCase() ?? "ttf";
try {
if (fileBackedAbsolutePaths.has(absPath)) {
embeddedPaths.add(localPath);
continue;
}
let dataUri = dataUriByAbsolutePath.get(absPath);
if (!dataUri) {
const font = await readLocalFont(absPath);
if (font.kind === "file-backed") {
fileBackedAbsolutePaths.add(absPath);
defaultLogger.debug(
`[Compiler] Kept large local font file-backed: ${localPath} (> ${(MAX_LOCAL_FONT_DATA_URI_BYTES / 1024 / 1024).toFixed(1)} MB)`,
);
embeddedPaths.add(localPath);
continue;
}
dataUri = await toDataUri(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)`,
);
}
// Anchored on the `url(...)` occurrence, not a bare substring. A
// plain replaceAll of `localPath` also rewrites that text anywhere
// else it appears -- including inside a LONGER url whose tail
// happens to match, e.g. embedding `fonts/x.ttf` would corrupt an
// untouched `url("file:///abs/fonts/x.ttf")` into
// `url("file:///abs/<data-uri>")`. Any two paths where one is a
// suffix of the other collide the same way. Every sibling rewrite
// in this file already anchors like this.
result = result.replace(urlOccurrenceRe(localPath), `url("${dataUri}")`);
embeddedPaths.add(localPath);
} catch (error) {
// Keep the original path: a font that cannot be read must not fail
// the render. Logged rather than silently swallowed -- a silent skip
// here means the composition renders in a fallback typeface and
// nothing says why.
defaultLogger.warn(
`[Compiler] Could not embed local font ${localPath}: ${
error instanceof Error ? error.message : String(error)
}`,
);
}
if (!localPath || dataUriByPath.has(localPath)) continue;
dataUriByPath.set(
localPath,
await localFontDataUri(localPath, projectDir, assetRoot, dataUriByAbsolutePath, log),
);
}
}
}
return result;
if (![...dataUriByPath.values()].some(Boolean)) return html;
let warned = false;
const embed = (source: string, localPath: string) => {
const dataUri = dataUriByPath.get(localPath);
if (!dataUri) return source;
const inlined = `url("${dataUri}")`;
if (budget.chargeGeneratedIfItFits(inlined.length - source.length)) return inlined;
if (!warned) {
warned = true;
log.warn(
`[Compiler] Kept local font ${localPath} file-backed: the page reached its inline size limit`,
);
}
return source;
};
return replaceMatches(html, styleBlockMatches(html), (style, css) =>
replaceInner(
style,
css,
"</style>".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,
);