mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
fix(fonts): stop failing cloud renders on var() shorthands, font-X-400 names and linked font sheets (#7166)
* fix(fonts): stop failing cloud renders on var() shorthands, font-X-400 names and linked font sheets * style(producer): shorten the project font sheet comment * style(core): shorten the field font families test header * fix(fonts): split font size parsing and fit the CLI size budget * fix(producer): read project font sheets for family names only, never fetch from them
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
// @vitest-environment node
|
||||
// Family spellings that failed distributed renders closed although the browser draws them.
|
||||
|
||||
import { afterAll, beforeEach, describe, expect, it } from "vitest";
|
||||
import { mkdtempSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { EMBEDDED_FONT_DATA } from "./fontData.generated.js";
|
||||
import {
|
||||
_clearGoogleFontCssCacheForTests,
|
||||
injectDeterministicFontFaces,
|
||||
} from "./deterministicFonts.js";
|
||||
|
||||
const FONT_CACHE_ENV = "HYPERFRAMES_FONT_CACHE_DIR";
|
||||
const originalFontCache = process.env[FONT_CACHE_ENV];
|
||||
const fontCache = mkdtempSync(join(tmpdir(), "hf-font-field-families-"));
|
||||
|
||||
beforeEach(() => {
|
||||
process.env[FONT_CACHE_ENV] = fontCache;
|
||||
_clearGoogleFontCssCacheForTests();
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
rmSync(fontCache, { recursive: true, force: true });
|
||||
if (originalFontCache !== undefined) process.env[FONT_CACHE_ENV] = originalFontCache;
|
||||
else delete process.env[FONT_CACHE_ENV];
|
||||
});
|
||||
|
||||
// Google Fonts answers 400 for a name it does not know; here it knows none.
|
||||
const noGoogleFonts = (async () => new Response("", { status: 400 })) as unknown as typeof fetch;
|
||||
|
||||
const inject = (html: string) =>
|
||||
injectDeterministicFontFaces(html, {
|
||||
failClosedFontFetch: true,
|
||||
allowSystemFontCapture: false,
|
||||
fetchImpl: noGoogleFonts,
|
||||
});
|
||||
|
||||
const MONTSERRAT_400 = EMBEDDED_FONT_DATA.get("@fontsource/montserrat:400:normal");
|
||||
|
||||
describe("field font families under fail-closed", () => {
|
||||
it("reads a font shorthand whose weight is a var() ahead of the size", async () => {
|
||||
const html = `<html><head><style>
|
||||
:root { --font-display: Montserrat, sans-serif; --weight: 700; }
|
||||
h1 { font: var(--weight) 1000px/1000px var(--font-display); }
|
||||
</style></head><body><h1>hi</h1></body></html>`;
|
||||
|
||||
const out = await inject(html);
|
||||
|
||||
expect(out).toContain('font-family: "Montserrat"');
|
||||
});
|
||||
|
||||
it("still takes a lone var() as the size when no literal size follows", async () => {
|
||||
const html = `<html><head><style>
|
||||
:root { --size: 40px; }
|
||||
h1 { font: var(--size) Montserrat; }
|
||||
</style></head><body><h1>hi</h1></body></html>`;
|
||||
|
||||
expect(await inject(html)).toContain('font-family: "Montserrat"');
|
||||
});
|
||||
|
||||
it("serves a class-style family name from the family it names", async () => {
|
||||
const html = `<html><head></head><body>
|
||||
<div style="font-family: font-Montserrat-400">hi</div>
|
||||
</body></html>`;
|
||||
|
||||
const out = await inject(html);
|
||||
|
||||
expect(MONTSERRAT_400).toBeDefined();
|
||||
expect(out).toContain('font-family: "font-Montserrat-400"');
|
||||
expect(out).toContain(`url("${MONTSERRAT_400}")`);
|
||||
});
|
||||
});
|
||||
@@ -990,6 +990,27 @@ function findDeclaredFamilyAlias(
|
||||
return match;
|
||||
}
|
||||
|
||||
// Design exports name a family by class, `font-<Family>-<weight>`; serve all <Family> faces under that name.
|
||||
const WEIGHTED_CLASS_FAMILY = /^font-(.+)-[1-9]00$/i;
|
||||
|
||||
async function resolveWeightedClassFamily(
|
||||
authoredFamily: string,
|
||||
optional: boolean,
|
||||
options: InternalFontFetchOptions,
|
||||
fontText?: string,
|
||||
): Promise<string[] | null> {
|
||||
const family = WEIGHTED_CLASS_FAMILY.exec(authoredFamily)?.[1];
|
||||
if (!family) return null;
|
||||
return resolveFamilyFaceRules(
|
||||
normalizeFamilyName(family),
|
||||
family,
|
||||
authoredFamily,
|
||||
optional,
|
||||
options,
|
||||
fontText,
|
||||
);
|
||||
}
|
||||
|
||||
// Emits under the authored name instead of rewriting usages, as FONT_ALIASES does,
|
||||
// so script-assigned and var()-indirected usages match too.
|
||||
async function resolveDeclaredFamilyAlias(
|
||||
@@ -1065,7 +1086,8 @@ async function buildFontFaceCss(
|
||||
optional,
|
||||
options,
|
||||
fontText,
|
||||
));
|
||||
)) ??
|
||||
(await resolveWeightedClassFamily(originalCaseFamily, optional, options, fontText));
|
||||
options.diag.endFamily(Boolean(familyRules));
|
||||
if (familyRules) {
|
||||
rules.push(...familyRules);
|
||||
@@ -1823,6 +1845,7 @@ export interface InjectDeterministicFontFacesOptions {
|
||||
* planning; tests may lower delays and timeouts without replacing timers.
|
||||
*/
|
||||
fontFetchRetryPolicy?: Partial<FontFetchRetryPolicy>;
|
||||
projectFontFamilies?: readonly string[];
|
||||
/**
|
||||
* When `true` (default for local renders), fonts that aren't resolved by
|
||||
* the bundled alias map or Google Fonts are located on the local filesystem,
|
||||
@@ -2029,6 +2052,8 @@ export async function injectDeterministicFontFaces(
|
||||
};
|
||||
|
||||
const existingFaces = extractExistingFontFaces(html);
|
||||
for (const family of options.projectFontFamilies ?? [])
|
||||
existingFaces.add(normalizeFamilyName(family));
|
||||
const requestedFamilies = extractRequestedFontFamilies(html);
|
||||
const pendingFamilies = new Map<string, ResolvedFamily>();
|
||||
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
// @vitest-environment node
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { copyFileSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { compileForRender } from "./htmlCompiler.js";
|
||||
|
||||
vi.mock("../utils/urlDownloader.js", async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import("../utils/urlDownloader.js")>()),
|
||||
downloadToTemp: async () => {
|
||||
throw new Error("offline test");
|
||||
},
|
||||
fetchPublicHttpsText: async () =>
|
||||
`@font-face { font-family: "Project Display"; src: url("https://cdn.example.com/display.woff2"); }`,
|
||||
}));
|
||||
|
||||
// A distributed render must never ask the network for a font the project itself ships.
|
||||
const FONT_FILE = join(
|
||||
__dirname,
|
||||
"../../tests/heygen-promo-preview-assets/src/TT_Norms_Pro_Medium.woff2",
|
||||
);
|
||||
|
||||
const page = (head: string, body = "") => `<!doctype html>
|
||||
<html><head>${head}</head>
|
||||
<body><div id="root" data-composition-id="root" data-width="320" data-height="180" data-duration="1">
|
||||
<h1 style="font-family: 'Project Display', sans-serif">hello</h1>${body}
|
||||
</div></body></html>`;
|
||||
|
||||
const FACE = `@font-face { font-family: "Project Display"; src: url("fonts/display.woff2") format("woff2"); }`;
|
||||
|
||||
describe("compileForRender with fonts shipped in the project", () => {
|
||||
let projectDir: string;
|
||||
let fetchSpy: ReturnType<typeof vi.fn>;
|
||||
|
||||
beforeEach(() => {
|
||||
projectDir = mkdtempSync(join(tmpdir(), "hf-project-fonts-"));
|
||||
mkdirSync(join(projectDir, "fonts"));
|
||||
copyFileSync(FONT_FILE, join(projectDir, "fonts/display.woff2"));
|
||||
fetchSpy = vi.fn(async () => new Response("", { status: 400 }));
|
||||
vi.stubGlobal("fetch", fetchSpy);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
rmSync(projectDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
const compile = (html: string) => {
|
||||
writeFileSync(join(projectDir, "index.html"), html);
|
||||
return compileForRender(projectDir, join(projectDir, "index.html"), join(projectDir, ".dl"), {
|
||||
failClosedFontFetch: true,
|
||||
allowSystemFontCapture: false,
|
||||
});
|
||||
};
|
||||
|
||||
it("accepts a family whose @font-face in the page points at a project file", async () => {
|
||||
const { html } = await compile(page(`<style>${FACE}</style>`));
|
||||
expect(html).toContain("Project Display");
|
||||
expect(html).not.toContain('url("fonts/display.woff2")');
|
||||
expect(fetchSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("accepts a family whose @font-face lives in a project stylesheet", async () => {
|
||||
writeFileSync(join(projectDir, "fonts.css"), FACE);
|
||||
const { html } = await compile(page(`<link rel="stylesheet" href="fonts.css">`));
|
||||
expect(html).toContain('<link rel="stylesheet" href="fonts.css">');
|
||||
expect(fetchSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("resolves a project stylesheet's font urls against the stylesheet's folder", async () => {
|
||||
mkdirSync(join(projectDir, "css"));
|
||||
writeFileSync(join(projectDir, "css/fonts.css"), FACE.replace("fonts/", "../fonts/"));
|
||||
const { html } = await compile(page(`<link rel="stylesheet" href="./css/fonts.css">`));
|
||||
expect(html).toContain('<link rel="stylesheet" href="./css/fonts.css">');
|
||||
expect(fetchSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("still fails closed when a project stylesheet names a font file the project lacks", async () => {
|
||||
writeFileSync(
|
||||
join(projectDir, "fonts.css"),
|
||||
FACE.replace("fonts/display.woff2", "fonts/missing.woff2"),
|
||||
);
|
||||
await expect(compile(page(`<link rel="stylesheet" href="fonts.css">`))).rejects.toThrow(
|
||||
/Unresolved fonts in fail-closed mode: Project Display/,
|
||||
);
|
||||
});
|
||||
|
||||
it("accepts a family whose @font-face lives in a remote stylesheet", async () => {
|
||||
const { html } = await compile(
|
||||
page(`<link rel="stylesheet" href="https://cdn.example.com/fonts.css">`),
|
||||
);
|
||||
expect(html).toContain("Project Display");
|
||||
});
|
||||
});
|
||||
@@ -12,7 +12,7 @@ import { decodedUrlPath } from "@hyperframes/parsers/asset-paths";
|
||||
*/
|
||||
|
||||
import { createHash } from "node:crypto";
|
||||
import { createReadStream, existsSync, mkdirSync } from "fs";
|
||||
import { createReadStream, existsSync, mkdirSync, readFileSync } from "fs";
|
||||
import { join, dirname, resolve, basename, relative } from "path";
|
||||
import { parseHTML } from "linkedom";
|
||||
import {
|
||||
@@ -1747,6 +1747,32 @@ async function inlineExternalFontStylesheets(html: string): Promise<string> {
|
||||
return result;
|
||||
}
|
||||
|
||||
// Families a linked project stylesheet declares with an @font-face file the project ships.
|
||||
function projectStylesheetFontFamilies(html: string, projectDir: string): string[] {
|
||||
const families: string[] = [];
|
||||
for (const [tag] of html.matchAll(/<link\b[^>]*\brel=["']stylesheet["'][^>]*>/gi)) {
|
||||
const href = /\bhref=["']([^"']+)["']/i.exec(tag)?.[1];
|
||||
if (!href || /^(?:[a-z][a-z\d+.-]*:|\/\/)/i.test(href)) continue;
|
||||
const sheet = resolve(projectDir, decodedUrlPath(href).replace(/^(?:\.?\/)+/, ""));
|
||||
if (!isPathInside(sheet, projectDir) || !existsSync(sheet)) continue;
|
||||
for (const face of extractFontFaceBlocks(readFileSync(sheet, "utf8"))) {
|
||||
const family = /font-family\s*:\s*([^;}]+)/i
|
||||
.exec(face)?.[1]
|
||||
?.trim()
|
||||
.replace(/^['"]|['"]$/g, "");
|
||||
const shipsFile = [...face.matchAll(LOCAL_FONTFACE_URL_RE)].some(([, url = ""]) => {
|
||||
const path = decodedUrlPath(url);
|
||||
const file = path.startsWith("/")
|
||||
? resolve(projectDir, `.${path}`)
|
||||
: resolve(dirname(sheet), path);
|
||||
return isPathInside(file, projectDir) && existsSync(file);
|
||||
});
|
||||
if (family && shipsFile) families.push(family);
|
||||
}
|
||||
}
|
||||
return families;
|
||||
}
|
||||
|
||||
/**
|
||||
* Collect all remote `url(https://...)` references inside `@font-face` blocks
|
||||
* found in `<style>` tags.
|
||||
@@ -1791,16 +1817,17 @@ export async function localizeRemoteFontFaces(
|
||||
html: string,
|
||||
downloadDir: string,
|
||||
): Promise<{ html: string; remoteMediaAssets: Map<string, string> }> {
|
||||
// Phase 1: Inline @font-face rules from external <link rel="stylesheet"> tags.
|
||||
const processed = await inlineExternalFontStylesheets(html);
|
||||
|
||||
// Phase 2: Download font file URLs from all @font-face blocks (both
|
||||
// pre-existing inline blocks and the ones just inlined from external sheets).
|
||||
const urlSet = collectFontFaceUrls(processed);
|
||||
return localizeFontFaceUrls(await inlineExternalFontStylesheets(html), downloadDir);
|
||||
}
|
||||
|
||||
/** Download the remote font files of the page's inline @font-face rules and point the rules at them. */
|
||||
function localizeFontFaceUrls(
|
||||
html: string,
|
||||
downloadDir: string,
|
||||
): Promise<{ html: string; remoteMediaAssets: Map<string, string> }> {
|
||||
return downloadAndRewriteUrls(
|
||||
urlSet,
|
||||
processed,
|
||||
collectFontFaceUrls(html),
|
||||
html,
|
||||
join(downloadDir, REMOTE_MEDIA_SUBDIR),
|
||||
"Remote font download failed",
|
||||
"Localized remote font face(s)",
|
||||
@@ -2102,16 +2129,19 @@ export async function compileForRender(
|
||||
const usesThreeDTransforms = detectThreeDTransformUsage(sanitizedHtml);
|
||||
const hasAncestorBackgroundImage = detectAncestorBackgroundImage(sanitizedHtml);
|
||||
|
||||
// Remote font stylesheets join the page first, or the font check below cannot see them.
|
||||
const fontSheetHtml = await inlineExternalFontStylesheets(
|
||||
promoteCssImportsToLinkTags(sanitizedHtml),
|
||||
);
|
||||
const normalizedFontHtml = normalizeSystemFontPrimaryFamilies(
|
||||
injectTextRenderingRule(
|
||||
coalesceHeadStylesAndBodyScripts(promoteCssImportsToLinkTags(sanitizedHtml)),
|
||||
),
|
||||
injectTextRenderingRule(coalesceHeadStylesAndBodyScripts(fontSheetHtml)),
|
||||
);
|
||||
|
||||
const coalescedHtml = await injectDeterministicFontFaces(normalizedFontHtml, {
|
||||
logger: defaultLogger,
|
||||
failClosedFontFetch: options.failClosedFontFetch === true,
|
||||
allowSystemFontCapture: options.allowSystemFontCapture,
|
||||
projectFontFamilies: projectStylesheetFontFamilies(fontSheetHtml, projectDir),
|
||||
abortSignal: options.abortSignal,
|
||||
});
|
||||
|
||||
@@ -2168,7 +2198,7 @@ export async function compileForRender(
|
||||
// allow http://localhost:PORT as origin), causing Chrome to silently fall
|
||||
// back to the next font in the stack.
|
||||
const { html: htmlWithLocalizedFonts, remoteMediaAssets: remoteFontAssets } =
|
||||
await localizeRemoteFontFaces(htmlWithLocalBg, downloadDir);
|
||||
await localizeFontFaceUrls(htmlWithLocalBg, downloadDir);
|
||||
|
||||
// Chrome cannot decode HEIC, and ffprobe reads a tiled one as video streams,
|
||||
// so convert to JPEG before the media-type preflight sees it.
|
||||
|
||||
Reference in New Issue
Block a user