Skip to content

Commit 87791fd

Browse files
fix(producer): keep large local fonts file-backed (#2864)
* fix(producer): keep large local fonts file-backed * fix(producer): avoid local font file races * fix(producer): bound local font stream reads * fix(producer): cache large font file-backed decisions
1 parent b7160f6 commit 87791fd

2 files changed

Lines changed: 90 additions & 5 deletions

File tree

‎packages/producer/src/services/htmlCompiler.test.ts‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,57 @@ describe("local font embedding", () => {
892892

893893
expect(embeddedMessages).toHaveLength(1);
894894
});
895+
896+
it("keeps large local font collections file-backed instead of expanding them into HTML", async () => {
897+
const projectDir = mkdtempSync(join(tmpdir(), "hf-large-local-font-"));
898+
const assetsDir = join(projectDir, "assets");
899+
mkdirSync(assetsDir, { recursive: true });
900+
writeFileSync(join(assetsDir, "large.ttc"), Buffer.alloc(6 * 1024 * 1024, 0x41));
901+
writeFileSync(
902+
join(projectDir, "index.html"),
903+
`<!DOCTYPE html>
904+
<html><head><style>
905+
@font-face {
906+
font-family: "LargeLocal";
907+
src: url("assets/large.ttc") format("collection");
908+
}
909+
@font-face {
910+
font-family: "LargeLocalAlias";
911+
src: url("./assets/large.ttc") format("collection");
912+
}
913+
@font-face {
914+
font-family: "LargeLocalNormalized";
915+
src: url("assets/../assets/large.ttc") format("collection");
916+
}
917+
</style></head><body>
918+
<div data-composition-id="root" data-width="640" data-height="360" data-duration="1">
919+
Text
920+
</div>
921+
</body></html>`,
922+
);
923+
924+
const originalInfo = defaultLogger.info;
925+
const fileBackedMessages: string[] = [];
926+
defaultLogger.info = (message) => {
927+
if (message.includes("Kept large local font file-backed")) {
928+
fileBackedMessages.push(message);
929+
}
930+
};
931+
932+
let compiled: Awaited<ReturnType<typeof compileForRender>>;
933+
try {
934+
compiled = await compileForRender(projectDir, join(projectDir, "index.html"), projectDir);
935+
} finally {
936+
defaultLogger.info = originalInfo;
937+
}
938+
939+
expect(compiled.html).toContain('url("assets/large.ttc")');
940+
expect(compiled.html).toContain('url("./assets/large.ttc")');
941+
expect(compiled.html).toContain('url("assets/../assets/large.ttc")');
942+
expect(compiled.html).not.toContain("data:font/collection;base64,");
943+
expect(Buffer.byteLength(compiled.html)).toBeLessThan(1024 * 1024);
944+
expect(fileBackedMessages).toHaveLength(1);
945+
});
895946
});
896947

897948
describe("template-wrapped sub-composition media offsets", () => {

‎packages/producer/src/services/htmlCompiler.ts‎

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* recursively extracting nested media from sub-sub-compositions.
1111
*/
1212

13-
import { readFileSync, existsSync, mkdirSync } from "fs";
13+
import { createReadStream, existsSync, mkdirSync, readFileSync } from "fs";
1414
import { join, dirname, resolve, basename } from "path";
1515
import { parseHTML } from "linkedom";
1616
import {
@@ -1655,6 +1655,28 @@ export async function localizeRemoteFontFaces(
16551655
}
16561656

16571657
const LOCAL_FONTFACE_URL_RE = /url\(["']?(?!data:|https?:\/\/)([^"')]+)["']?\)/gi;
1658+
// Base64 expands bytes by ~33%, then immutable HTML replacements retain more
1659+
// string copies while compiling. Files up to and including 5 MiB remain inline;
1660+
// the first byte above that stays file-backed. This conservative ceiling keeps
1661+
// verified 19 MiB+ TTC collections out of the V8 heap while both local and
1662+
// distributed file servers continue serving project assets at authored paths.
1663+
const MAX_LOCAL_FONT_DATA_URI_BYTES = 5 * 1024 * 1024;
1664+
1665+
type LocalFontRead = { kind: "file-backed" } | { kind: "inline"; buffer: Buffer };
1666+
1667+
async function readLocalFont(absPath: string): Promise<LocalFontRead> {
1668+
const chunks: Buffer[] = [];
1669+
let totalBytes = 0;
1670+
for await (const chunk of createReadStream(absPath)) {
1671+
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
1672+
totalBytes += buffer.length;
1673+
if (totalBytes > MAX_LOCAL_FONT_DATA_URI_BYTES) {
1674+
return { kind: "file-backed" };
1675+
}
1676+
chunks.push(buffer);
1677+
}
1678+
return { kind: "inline", buffer: Buffer.concat(chunks, totalBytes) };
1679+
}
16581680

16591681
// fallow-ignore-next-line complexity
16601682
async function embedLocalFontFaces(html: string, projectDir: string): Promise<string> {
@@ -1664,6 +1686,7 @@ async function embedLocalFontFaces(html: string, projectDir: string): Promise<st
16641686
let result = html;
16651687
const embeddedPaths = new Set<string>();
16661688
const dataUriByAbsolutePath = new Map<string, string>();
1689+
const fileBackedAbsolutePaths = new Set<string>();
16671690

16681691
let styleMatch: RegExpExecArray | null;
16691692
while ((styleMatch = styleBlockRe.exec(html)) !== null) {
@@ -1679,16 +1702,27 @@ async function embedLocalFontFaces(html: string, projectDir: string): Promise<st
16791702
if (!localPath || embeddedPaths.has(localPath)) continue;
16801703
const absPath = localPath.startsWith("/") ? localPath : resolve(projectDir, localPath);
16811704
if (!isPathInside(absPath, projectDir)) continue;
1682-
if (!existsSync(absPath)) continue;
16831705
const ext = absPath.match(/\.(woff2?|ttf|otf|ttc)$/i)?.[1]?.toLowerCase() ?? "ttf";
16841706
try {
1707+
if (fileBackedAbsolutePaths.has(absPath)) {
1708+
embeddedPaths.add(localPath);
1709+
continue;
1710+
}
16851711
let dataUri = dataUriByAbsolutePath.get(absPath);
16861712
if (!dataUri) {
1687-
const buffer = readFileSync(absPath);
1688-
dataUri = await toDataUri(buffer, ext);
1713+
const font = await readLocalFont(absPath);
1714+
if (font.kind === "file-backed") {
1715+
fileBackedAbsolutePaths.add(absPath);
1716+
defaultLogger.info(
1717+
`[Compiler] Kept large local font file-backed: ${localPath} (> ${(MAX_LOCAL_FONT_DATA_URI_BYTES / 1024 / 1024).toFixed(1)} MB)`,
1718+
);
1719+
embeddedPaths.add(localPath);
1720+
continue;
1721+
}
1722+
dataUri = await toDataUri(font.buffer, ext);
16891723
dataUriByAbsolutePath.set(absPath, dataUri);
16901724
defaultLogger.info(
1691-
`[Compiler] Embedded local font file: ${localPath} (${(buffer.length / 1024).toFixed(0)} KB → data URI)`,
1725+
`[Compiler] Embedded local font file: ${localPath} (${(font.buffer.length / 1024).toFixed(0)} KB → data URI)`,
16921726
);
16931727
}
16941728
result = result.replaceAll(localPath, dataUri);

0 commit comments

Comments
 (0)