Skip to content

Commit 6605225

Browse files
authored
fix(producer): isolate font cache temporary writes (#3669)
1 parent a93106a commit 6605225

2 files changed

Lines changed: 85 additions & 5 deletions

File tree

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

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
import { describe, expect, it } from "bun:test";
2-
import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs";
1+
import { describe, expect, it, spyOn } from "bun:test";
2+
import {
3+
existsSync,
4+
mkdirSync,
5+
mkdtempSync,
6+
readFileSync,
7+
readdirSync,
8+
rmSync,
9+
writeFileSync,
10+
} from "node:fs";
311
import { tmpdir } from "node:os";
412
import { join } from "node:path";
513
import { compressToWoff2, fontToDataUri } from "./fontCompression.js";
@@ -43,6 +51,66 @@ describe("compressToWoff2", () => {
4351
});
4452

4553
describe("fontToDataUri", () => {
54+
it("preserves a pre-existing temporary file and still populates the cache", async () => {
55+
const cacheDir = mkdtempSync(join(tmpdir(), "hf-local-font-cache-"));
56+
const raw = Buffer.from("stable-font-content");
57+
const compressed = Buffer.from("compressed-font-content");
58+
const compressImpl = async () => compressed;
59+
const clock = spyOn(Date, "now").mockReturnValue(1234567890);
60+
try {
61+
const first = await fontToDataUri(raw, "ttf", { cacheDir, compressImpl });
62+
const [cacheName] = readdirSync(cacheDir);
63+
if (!cacheName) throw new Error("Expected a cached font");
64+
rmSync(join(cacheDir, cacheName));
65+
const existingName = `${cacheName}.tmp-${process.pid}-${Date.now()}`;
66+
writeFileSync(join(cacheDir, existingName), "another writer's file");
67+
68+
expect(await fontToDataUri(raw, "ttf", { cacheDir, compressImpl })).toBe(first);
69+
expect(readFileSync(join(cacheDir, existingName), "utf8")).toBe("another writer's file");
70+
expect(readFileSync(join(cacheDir, cacheName))).toEqual(compressed);
71+
expect(readdirSync(cacheDir).sort()).toEqual([cacheName, existingName].sort());
72+
} finally {
73+
clock.mockRestore();
74+
rmSync(cacheDir, { recursive: true, force: true });
75+
}
76+
});
77+
78+
it("cleans up its temporary files when publishing the cache fails", async () => {
79+
const cacheDir = mkdtempSync(join(tmpdir(), "hf-local-font-cache-"));
80+
const raw = Buffer.from("stable-font-content");
81+
const compressImpl = async () => Buffer.from("compressed-font-content");
82+
try {
83+
const first = await fontToDataUri(raw, "ttf", { cacheDir, compressImpl });
84+
const [cacheName] = readdirSync(cacheDir);
85+
if (!cacheName) throw new Error("Expected a cached font");
86+
rmSync(join(cacheDir, cacheName));
87+
mkdirSync(join(cacheDir, cacheName));
88+
89+
expect(await fontToDataUri(raw, "ttf", { cacheDir, compressImpl })).toBe(first);
90+
expect(readdirSync(cacheDir)).toEqual([cacheName]);
91+
} finally {
92+
rmSync(cacheDir, { recursive: true, force: true });
93+
}
94+
});
95+
96+
it("returns compressed data when the cache directory cannot be created", async () => {
97+
const root = mkdtempSync(join(tmpdir(), "hf-local-font-cache-"));
98+
const cacheDir = join(root, "not-a-directory");
99+
writeFileSync(cacheDir, "keep");
100+
try {
101+
const compressed = Buffer.from("compressed-font-content");
102+
const uri = await fontToDataUri(Buffer.from("font"), "ttf", {
103+
cacheDir,
104+
compressImpl: async () => compressed,
105+
});
106+
expect(uri).toBe(`data:font/woff2;base64,${compressed.toString("base64")}`);
107+
expect(readFileSync(cacheDir, "utf8")).toBe("keep");
108+
expect(readdirSync(root)).toEqual(["not-a-directory"]);
109+
} finally {
110+
rmSync(root, { recursive: true, force: true });
111+
}
112+
});
113+
46114
it("reuses cached compression across calls", async () => {
47115
const cacheDir = mkdtempSync(join(tmpdir(), "hf-local-font-cache-"));
48116
const raw = Buffer.from("stable-font-content");

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
// @ts-expect-error -- wawoff2 ships no type declarations; ambient .d.ts only visible to producer's own tsconfig
22
import wawoff2 from "wawoff2";
33
import { createHash } from "node:crypto";
4-
import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
4+
import {
5+
existsSync,
6+
mkdirSync,
7+
mkdtempSync,
8+
readFileSync,
9+
renameSync,
10+
rmSync,
11+
writeFileSync,
12+
} from "node:fs";
513
import { homedir, tmpdir } from "node:os";
614
import { dirname, join } from "node:path";
715

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

5967
function cacheCompression(path: string, compressed: Buffer): void {
60-
const tmpPath = `${path}.tmp-${process.pid}-${Date.now()}`;
68+
let tempDir: string | undefined;
6169
try {
6270
mkdirSync(dirname(path), { recursive: true });
71+
// Keep the write on the cache filesystem for an atomic rename, inside a
72+
// private directory that this invocation owns.
73+
tempDir = mkdtempSync(join(dirname(path), ".compression-"));
74+
const tmpPath = join(tempDir, "font.woff2");
6375
writeFileSync(tmpPath, compressed, { flag: "wx", mode: 0o644 });
6476
renameSync(tmpPath, path);
6577
} catch {
6678
// A concurrent process may have populated the cache, or the cache may be
6779
// read-only. Compression still succeeded, so rendering can continue.
6880
} finally {
6981
try {
70-
rmSync(tmpPath, { force: true });
82+
if (tempDir) rmSync(tempDir, { recursive: true, force: true });
7183
} catch {
7284
// Best-effort cleanup only.
7385
}

0 commit comments

Comments
 (0)