Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 70 additions & 2 deletions packages/producer/src/services/fontCompression.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { describe, expect, it } from "bun:test";
import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs";
import { describe, expect, it, spyOn } from "bun:test";
import {
existsSync,
mkdirSync,
mkdtempSync,
readFileSync,
readdirSync,
rmSync,
writeFileSync,
} from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { compressToWoff2, fontToDataUri } from "./fontCompression.js";
Expand Down Expand Up @@ -43,6 +51,66 @@ describe("compressToWoff2", () => {
});

describe("fontToDataUri", () => {
it("preserves a pre-existing temporary file and still populates the cache", async () => {
const cacheDir = mkdtempSync(join(tmpdir(), "hf-local-font-cache-"));
const raw = Buffer.from("stable-font-content");
const compressed = Buffer.from("compressed-font-content");
const compressImpl = async () => compressed;
const clock = spyOn(Date, "now").mockReturnValue(1234567890);
try {
const first = await fontToDataUri(raw, "ttf", { cacheDir, compressImpl });
const [cacheName] = readdirSync(cacheDir);
if (!cacheName) throw new Error("Expected a cached font");
rmSync(join(cacheDir, cacheName));
const existingName = `${cacheName}.tmp-${process.pid}-${Date.now()}`;
writeFileSync(join(cacheDir, existingName), "another writer's file");

expect(await fontToDataUri(raw, "ttf", { cacheDir, compressImpl })).toBe(first);
expect(readFileSync(join(cacheDir, existingName), "utf8")).toBe("another writer's file");
expect(readFileSync(join(cacheDir, cacheName))).toEqual(compressed);
expect(readdirSync(cacheDir).sort()).toEqual([cacheName, existingName].sort());
} finally {
clock.mockRestore();
rmSync(cacheDir, { recursive: true, force: true });
}
});

it("cleans up its temporary files when publishing the cache fails", async () => {
const cacheDir = mkdtempSync(join(tmpdir(), "hf-local-font-cache-"));
const raw = Buffer.from("stable-font-content");
const compressImpl = async () => Buffer.from("compressed-font-content");
try {
const first = await fontToDataUri(raw, "ttf", { cacheDir, compressImpl });
const [cacheName] = readdirSync(cacheDir);
if (!cacheName) throw new Error("Expected a cached font");
rmSync(join(cacheDir, cacheName));
mkdirSync(join(cacheDir, cacheName));

expect(await fontToDataUri(raw, "ttf", { cacheDir, compressImpl })).toBe(first);
expect(readdirSync(cacheDir)).toEqual([cacheName]);
} finally {
rmSync(cacheDir, { recursive: true, force: true });
}
});

it("returns compressed data when the cache directory cannot be created", async () => {
const root = mkdtempSync(join(tmpdir(), "hf-local-font-cache-"));
const cacheDir = join(root, "not-a-directory");
writeFileSync(cacheDir, "keep");
try {
const compressed = Buffer.from("compressed-font-content");
const uri = await fontToDataUri(Buffer.from("font"), "ttf", {
cacheDir,
compressImpl: async () => compressed,
});
expect(uri).toBe(`data:font/woff2;base64,${compressed.toString("base64")}`);
expect(readFileSync(cacheDir, "utf8")).toBe("keep");
expect(readdirSync(root)).toEqual(["not-a-directory"]);
} finally {
rmSync(root, { recursive: true, force: true });
}
});

it("reuses cached compression across calls", async () => {
const cacheDir = mkdtempSync(join(tmpdir(), "hf-local-font-cache-"));
const raw = Buffer.from("stable-font-content");
Expand Down
18 changes: 15 additions & 3 deletions packages/producer/src/services/fontCompression.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
// @ts-expect-error -- wawoff2 ships no type declarations; ambient .d.ts only visible to producer's own tsconfig
import wawoff2 from "wawoff2";
import { createHash } from "node:crypto";
import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
import {
existsSync,
mkdirSync,
mkdtempSync,
readFileSync,
renameSync,
rmSync,
writeFileSync,
} from "node:fs";
import { homedir, tmpdir } from "node:os";
import { dirname, join } from "node:path";

Expand Down Expand Up @@ -57,17 +65,21 @@ function readCachedCompression(path: string): Buffer | null {
}

function cacheCompression(path: string, compressed: Buffer): void {
const tmpPath = `${path}.tmp-${process.pid}-${Date.now()}`;
let tempDir: string | undefined;
try {
mkdirSync(dirname(path), { recursive: true });
// Keep the write on the cache filesystem for an atomic rename, inside a
// private directory that this invocation owns.
tempDir = mkdtempSync(join(dirname(path), ".compression-"));
const tmpPath = join(tempDir, "font.woff2");
writeFileSync(tmpPath, compressed, { flag: "wx", mode: 0o644 });
renameSync(tmpPath, path);
} catch {
// A concurrent process may have populated the cache, or the cache may be
// read-only. Compression still succeeded, so rendering can continue.
} finally {
try {
rmSync(tmpPath, { force: true });
if (tempDir) rmSync(tempDir, { recursive: true, force: true });
} catch {
// Best-effort cleanup only.
}
Expand Down
Loading