|
1 | 1 | import { createHash } from "node:crypto"; |
2 | | -import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; |
| 2 | +import { existsSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs"; |
3 | 3 | import { homedir, tmpdir } from "node:os"; |
4 | 4 | import { join } from "node:path"; |
5 | 5 | import { defaultLogger } from "../logger.js"; |
@@ -581,17 +581,21 @@ function warnUnresolvedFonts(unresolved: string[]): void { |
581 | 581 | // Google Fonts on-demand fetch + local cache |
582 | 582 | // --------------------------------------------------------------------------- |
583 | 583 |
|
584 | | -// On AWS Lambda `$HOME` resolves to a `/home/sbx_*` tree that's |
585 | | -// read-only; only `/tmp` is writable. Route the cache there when |
586 | | -// running inside Lambda, and honor `HYPERFRAMES_FONT_CACHE_DIR` as |
587 | | -// an explicit override for any environment. |
| 584 | +let lambdaFontCacheRoot: string | undefined; |
| 585 | + |
| 586 | +// On AWS Lambda `$HOME` resolves to a `/home/sbx_*` tree that's read-only; |
| 587 | +// only `/tmp` is writable. Create one private, unguessable cache directory per |
| 588 | +// warm process and reuse it across invocations. Honor HYPERFRAMES_FONT_CACHE_DIR |
| 589 | +// as an explicit override for any environment. |
588 | 590 | function resolveFontCacheRoot(): string { |
589 | | - return ( |
590 | | - process.env.HYPERFRAMES_FONT_CACHE_DIR ?? |
591 | | - (process.env.AWS_LAMBDA_FUNCTION_NAME |
592 | | - ? join(tmpdir(), "hyperframes", "fonts") |
593 | | - : join(homedir(), ".cache", "hyperframes", "fonts")) |
594 | | - ); |
| 591 | + if (process.env.HYPERFRAMES_FONT_CACHE_DIR) { |
| 592 | + return process.env.HYPERFRAMES_FONT_CACHE_DIR; |
| 593 | + } |
| 594 | + if (process.env.AWS_LAMBDA_FUNCTION_NAME) { |
| 595 | + lambdaFontCacheRoot ??= mkdtempSync(join(tmpdir(), "hyperframes-fonts-")); |
| 596 | + return lambdaFontCacheRoot; |
| 597 | + } |
| 598 | + return join(homedir(), ".cache", "hyperframes", "fonts"); |
595 | 599 | } |
596 | 600 |
|
597 | 601 | // Chrome UA triggers woff2 responses from Google Fonts CSS API |
|
0 commit comments