mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-09-07 01:56:04 +00:00
Compositions are now self-contained: the compiler captures font files and embeds them as woff2 data URIs, eliminating silent render-time fallback when the render environment lacks the author's fonts. Resolution order (each tier falls through to the next): 1. Existing @font-face → use as-is 2. Bundled alias (38 cross-platform mappings) → embed data URI 3. Google Fonts → fetch, cache, embed 4. Local system font → locate on OS, compress to woff2, embed 5. Local @font-face paths → read file, compress, inline as data URI 6. External CDN stylesheets → fetch CSS, extract @font-face, inline 7. Alias map fallback → closest bundled equivalent 8. Actionable error with guidance Key changes: - System font locator (macOS/Windows/Linux) with path-bounding and symlink defense (realpathSync + O_NOFOLLOW) - woff2 compression via wawoff2 (WASM, cross-platform) - Multi-weight/style variant capture with length-sorted token matching - External stylesheet inlining with SSRF defense (assertPublicHttpsUrl, HTTPS-only, private-host blocking, 2MB cap, 4-concurrent limit) - Studio auto-import via GET /fonts/file API + renderAliasFor() derived from shared FONT_ALIAS_MAP (no more hand-curated drift) - failClosedFontFetch throws on unresolved fonts in distributed renders - Single source of truth: @hyperframes/core/fonts/aliases - system_font_will_alias lint rule (escalates to warning for distributed) - Default to Inter + JetBrains Mono in templates and CSS reset
81 lines
2.9 KiB
TypeScript
81 lines
2.9 KiB
TypeScript
import { describe, expect, it } from "bun:test";
|
|
import { existsSync, readFileSync } from "node:fs";
|
|
import { compressToWoff2, fontToDataUri } from "./fontCompression.js";
|
|
|
|
/**
|
|
* Locate a system TTF font for real compression tests. macOS ships plenty
|
|
* of .ttf files in /System/Library/Fonts/Supplemental; we pick the first
|
|
* one found from a short list. Returns null on Linux CI or any environment
|
|
* without the expected fonts — those tests are skipped gracefully.
|
|
*/
|
|
function findSystemTtf(): Buffer | null {
|
|
const candidates = [
|
|
"/System/Library/Fonts/Supplemental/Andale Mono.ttf",
|
|
"/System/Library/Fonts/Supplemental/Courier New.ttf",
|
|
"/System/Library/Fonts/Supplemental/Arial.ttf",
|
|
"/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
|
|
];
|
|
for (const path of candidates) {
|
|
if (existsSync(path)) return readFileSync(path);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
describe("compressToWoff2", () => {
|
|
it("compresses a TTF buffer to a smaller woff2 buffer", async () => {
|
|
const ttf = findSystemTtf();
|
|
if (!ttf) {
|
|
console.warn("Skipping: no system TTF font available");
|
|
return;
|
|
}
|
|
const woff2 = await compressToWoff2(ttf);
|
|
expect(woff2).toBeInstanceOf(Buffer);
|
|
expect(woff2.length).toBeGreaterThan(0);
|
|
expect(woff2.length).toBeLessThan(ttf.length);
|
|
});
|
|
|
|
it("throws on invalid input", async () => {
|
|
const garbage = Buffer.from("this is not a font file");
|
|
await expect(compressToWoff2(garbage)).rejects.toThrow();
|
|
});
|
|
});
|
|
|
|
describe("fontToDataUri", () => {
|
|
it("skips compression for woff2 input and returns a data URI", async () => {
|
|
const raw = Buffer.from("fake-woff2-bytes");
|
|
const uri = await fontToDataUri(raw, "woff2");
|
|
const expectedBase64 = raw.toString("base64");
|
|
expect(uri).toBe(`data:font/woff2;base64,${expectedBase64}`);
|
|
});
|
|
|
|
it("compresses a TTF and returns a woff2 data URI", async () => {
|
|
const ttf = findSystemTtf();
|
|
if (!ttf) {
|
|
console.warn("Skipping: no system TTF font available");
|
|
return;
|
|
}
|
|
const uri = await fontToDataUri(ttf, "ttf");
|
|
expect(uri).toMatch(/^data:font\/woff2;base64,/);
|
|
const naiveLength = `data:font/ttf;base64,${ttf.toString("base64")}`.length;
|
|
expect(uri.length).toBeLessThan(naiveLength);
|
|
});
|
|
|
|
it("falls back to raw ttf on compression failure", async () => {
|
|
const garbage = Buffer.from("this is not a font file");
|
|
const uri = await fontToDataUri(garbage, "ttf");
|
|
expect(uri).toMatch(/^data:font\/ttf;base64,/);
|
|
});
|
|
|
|
it("falls back with correct MIME type for otf", async () => {
|
|
const garbage = Buffer.from("not a font");
|
|
const uri = await fontToDataUri(garbage, "otf");
|
|
expect(uri).toMatch(/^data:font\/otf;base64,/);
|
|
});
|
|
|
|
it("falls back with correct MIME type for ttc", async () => {
|
|
const garbage = Buffer.from("not a font");
|
|
const uri = await fontToDataUri(garbage, "ttc");
|
|
expect(uri).toMatch(/^data:font\/collection;base64,/);
|
|
});
|
|
});
|