$ show elvisdragonmao/emfont
emfont is a free, open-source CJK web font service that provides on-demand, dynamically subsetted fonts and pre-generated static font packs. The backend is built with Node.js 22 and the Fastify framework, using PostgreSQL for metadata, Redis for caching, and MinIO (or optionally Cloudflare R2) for font file storage. The service exposes a REST API for font generation (`/g/:font`), CSS integration (`/css/:font`), and an admin panel for font upload, editing, and user management. A client-side JavaScript library (`emfont.js`) scans DOM elements with `emfont-*` classes and loads the correct subset fonts, with local caching via localStorage. The system supports Unicode script classification for language coverage analysis and uses worker threads for parallel static font regeneration. Deployment is Dockerized with a multi-stage build, and monitoring includes Prometheus metrics and optional Pyroscope profiling. The codebase is well-structured with clean separation of concerns (routes, utilities, fonts), but lacks automated tests and has minor security concerns (admin session secret fallback), and some hardcoded URLs that reduce portability.
The LLM identified 3 files worth highlighting for reuse:
Implements a context-aware logger using AsyncLocalStorage to propagate request-specific metadata (e.g., request ID) through async calls. Wraps the pino logger to provide a high-performance, structured logging interface that can be injected into any module. This pattern is self-contained and reusable across any Node.js application requiring contextual logging.
Classifies Unicode codepoints into script categories using a compact JSON representation derived from the official Unicode Scripts.txt. Uses a binary search over sorted ranges for O(log n) lookups. The module is entirely self-contained (embedded Scripts.json) and can be extracted to any text-processing project needing script identification, font fallback decisions, or language detection.
A UMD JavaScript library that scans the DOM for elements with `emfont-*` attributes (e.g., `emfont-css`, `emfont-text`) and dynamically loads the corresponding subset web fonts. Implements an in-memory and localStorage cache with TTL to avoid redundant network requests. The library is independent of the server's internal architecture and can be reused in any frontend project that needs to load custom subset fonts from a CDN endpoint.