web-font/backend/shared.ts
崮生(子虚) e15f96294b perf(双端): 字体目录清单 TTL 缓存——find_font_path 模糊匹配与 /api/fonts 共享
- 原 find 模糊匹配每请求 readdir+逐项 stat,成本随字体数线性增长(线上 find 4ms)
- rust: misc::font_dir_listing 5s TTL + Arc 共享;精确匹配不缓存(新上传字体立即可见);
  上传成功主动失效;collect_gids/seed_gids 的 O(n²) contains 改定长位图
- ts: shared.fontDirListing 同设计;findFontPath/routes fonts/upload 失效同步
- 回归:51 测试过;3 字体×3 文本新旧产物 sha256 一致;/api/fonts 输出 diff 为空;
  线上二次模糊匹配 find 4ms→0ms
2026-11-04 21:05:42 +08:00

186 lines
6.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { fontDirs, subsetCacheMaxSize } from "./config";
import { LruCache } from "./lru_cache";
import type { SubsetTtfObject } from "./font_util/glyph_bitmap";
import { path_join, readFile, stat, readdir } from "./interface";
import { loadPersistedStats, scheduleStatsFlush, type PersistedStats } from "./stats_store";
/** 解析请求 URL(req.url 只有路径,需要补全协议和主机才能用 URL API) */
export function parseUrl(req: Request): URL {
return new URL(req.url, "http://localhost");
}
/** JSON 响应工具 */
export function jsonResponse(data: unknown, status = 200) {
return new Response(JSON.stringify(data), {
status,
headers: { "Content-Type": "application/json" },
});
}
/** 运行时统计(累计计数跨重启持久化,startTime 为本次进程启动时刻) */
export const stats = {
/** 服务启动时间戳(进程级,不持久化) */
startTime: Date.now(),
/** 总请求数(持久化累计) */
totalRequests: 0,
/** 字体裁剪请求次数(含缓存命中;持久化累计) */
subsetRequests: 0,
/** 字体裁剪缓存命中次数(持久化累计) */
subsetCacheHits: 0,
/** 累计裁剪文字字符数(持久化累计) */
totalChars: 0,
/** 临时文件上传次数(持久化累计) */
tempUploads: 0,
/** 离线裁剪完成次数(纯浏览器端裁剪的匿名上报;持久化累计) */
offlineSubsets: 0,
/** 离线裁剪字体下载次数(匿名上报;持久化累计) */
offlineDownloads: 0,
};
/**
* 从持久化文件恢复累计计数
*
* 在 main() 最早期 await 一次:让计数器从历史值起步,实现跨重启持久化。
* startTime 始终重置为当前时刻——uptime 度量的是本次进程。
*/
export async function initStats(): Promise<void> {
const persisted = await loadPersistedStats();
stats.startTime = persisted.startTime;
stats.totalRequests = persisted.totalRequests;
stats.subsetRequests = persisted.subsetRequests;
stats.subsetCacheHits = persisted.subsetCacheHits;
stats.totalChars = persisted.totalChars;
stats.tempUploads = persisted.tempUploads;
stats.offlineSubsets = persisted.offlineSubsets;
stats.offlineDownloads = persisted.offlineDownloads;
}
/** 取出需要持久化的累计计数字段 */
export function snapshotStats(): PersistedStats {
return {
totalRequests: stats.totalRequests,
subsetRequests: stats.subsetRequests,
subsetCacheHits: stats.subsetCacheHits,
totalChars: stats.totalChars,
tempUploads: stats.tempUploads,
offlineSubsets: stats.offlineSubsets,
offlineDownloads: stats.offlineDownloads,
};
}
/**
* 标记统计已变更,触发防抖落盘
*
* 请求热路径调用:仅做一次对象赋值 + 幂等的定时器检查,绝不阻塞。
* 真正的磁盘 IO 由 stats_store 内部按 FLUSH_INTERVAL 批量执行。
*/
export function markStatsDirty(): void {
scheduleStatsFlush(snapshotStats());
}
/** 字体文件 LRU 缓存,最多保留 3 个最近使用的字体 buffer(按条目数淘汰) */
export const fontBufferCache = new LruCache<ArrayBuffer>({ maxSize: 3 });
/**
* 字体裁剪产物缓存条目:bytes 供 HTTP 下载/前端预览;
* ttf 为内存子集对象(cmap 已重建,可直接光栅化评分),OTF 输入时为 null。
* 不为序列化已产出的对象单独留副本,两处消费方共享同一份产物。
*/
export interface SubsetCacheEntry {
/** 序列化裁剪字体(供 HTTP 下载/前端预览) */
bytes: Uint8Array;
/** 评分用内存子集对象(cmap 已重建,可直接光栅化),OTF 输入时为 null */
ttf: SubsetTtfObject | null;
}
/**
* 字体裁剪结果共享 LRU 缓存(按内存容量淘汰)。
* key 由消费方自定(版本指纹:字体路径:outType:文本);
* 容量计费 = 序列化 bytes + ttf 对象估算开销(16 倍),避免缓存对象本身把容器排薄。
*/
export const subsetCache = new LruCache<SubsetCacheEntry>({
maxBytes: subsetCacheMaxSize,
sizeFn: (v) => v.bytes.byteLength * (v.ttf ? 17 : 1),
});
/** 字体目录清单条目:模糊匹配键 + 列表输出共享 */
interface FontDirEntry {
/** 去扩展名小写 stem(前缀/包含匹配键) */
stem: string;
/** 原始文件名(/api/fonts 输出) */
name: string;
/** 所在目录 */
dir: string;
}
/** 目录清单缓存(5s TTL):消除每请求 readdir + 逐项判定的成本(随字体数线性增长)。
* 上传/删除后主动失效(invalidateFontDirCache);精确匹配不走此缓存,新上传字体立即可见 */
const FONT_DIR_CACHE_TTL = 5_000;
let fontDirCache: { at: number; entries: FontDirEntry[] } | null = null;
/** 上传/删除字体后主动失效(下一次任何查找立即重建清单) */
export function invalidateFontDirCache(): void {
fontDirCache = null;
}
/** 取字体目录清单(TTL 内复用),findFontPath 模糊匹配与 /api/fonts 共享 */
export async function fontDirListing(): Promise<FontDirEntry[]> {
if (fontDirCache && Date.now() - fontDirCache.at < FONT_DIR_CACHE_TTL) {
return fontDirCache.entries;
}
const entries: FontDirEntry[] = [];
for (const dir of fontDirs) {
try {
const list = await readdir(dir);
for (const entry of list) {
if (entry.isFile() && /\.(ttf|otf|woff|woff2)$/i.test(entry.name)) {
entries.push({
stem: entry.name.replace(/\.[^.]+$/, "").toLowerCase(),
name: entry.name,
dir,
});
}
}
} catch {}
}
fontDirCache = { at: Date.now(), entries };
return entries;
}
/**
* 在所有字体目录中查找字体文件
* 匹配优先级:精确匹配 > 前缀匹配 > 包含匹配
*/
export async function findFontPath(filename: string): Promise<string | null> {
for (const dir of fontDirs) {
const filePath = path_join(dir, filename);
try {
const s = await stat(filePath);
if (s.isFile()) return filePath;
} catch {}
}
const allFonts = await fontDirListing();
const query = filename.replace(/\.[^.]+$/, "").toLowerCase();
for (const f of allFonts) {
if (f.stem.startsWith(query)) return path_join(f.dir, f.name);
}
for (const f of allFonts) {
if (f.stem.includes(query)) return path_join(f.dir, f.name);
}
return null;
}
/** 从缓存或磁盘读取字体 buffer */
export async function readFontBuffer(fontPath: string): Promise<ArrayBuffer> {
const cached = fontBufferCache.get(fontPath);
if (cached) return cached;
const buffer = new Uint8Array(await readFile(fontPath)).buffer;
fontBufferCache.set(fontPath, buffer);
return buffer;
}