From 7d0243e6f5bbde5c39f770c4b7968e5a9f77ff16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B4=AE=E7=94=9F=EF=BC=88=E5=AD=90=E8=99=9A=EF=BC=89?= <2234839456@qq.com> Date: Sat, 13 Jun 2026 21:32:33 +0800 Subject: [PATCH] =?UTF-8?q?fix(v1.9.2):=20subsetCache=20=E6=94=B9=E7=94=A8?= =?UTF-8?q?=20Date.now()=20=E5=90=AF=E5=8A=A8=E6=97=B6=E6=88=B3=EF=BC=8C?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20LLRT=20process.uptime=20=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit process.uptime() 在 LLRT 运行时不存在(typeof === "undefined"),调用抛 TypeError: not a function,导致容器启动即崩溃(v1.9.1 修完 __filename 后暴露)。 改用模块加载时取一次 Date.now():Node/LLRT 均可用,进程重启即变化, 开发态「重启即重置内存缓存」语义不变,生产态靠 PACKAGE_VERSION。 另增 dev:backend:llrt / serve:llrt 本地命令,用 LLRT 跑后端提前暴露兼容问题。 Co-Authored-By: Claude Opus 4.8 (1M context) --- backend/routes/subset.ts | 18 ++++++++++++------ package.json | 4 +++- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/backend/routes/subset.ts b/backend/routes/subset.ts index e10a7c5..18c061e 100644 --- a/backend/routes/subset.ts +++ b/backend/routes/subset.ts @@ -2,20 +2,26 @@ import { fontSubset } from "../font_util/font"; import type { FontEditor } from "../../vendor/fonteditor-core/lib/ttf/font.js"; import { parseUrl, jsonResponse, stats, subsetCache, findFontPath, readFontBuffer } from "../shared"; +/** + * 进程启动时戳(模块加载时取一次,进程重启即变化) + * + * 用 Date.now() 而非 process.uptime():后者在 LLRT 运行时不存在(typeof === "undefined"), + * 调用会抛 TypeError: not a function,导致容器启动即崩溃。 + * Date.now() 在 Node 与 LLRT 中均可用,且模块只加载一次,取到的就是稳定的启动时戳。 + */ +declare const PACKAGE_VERSION: string; +/** 进程启动时刻,重启进程即变化 —— 用于开发态「重启即重置内存缓存」 */ +const PROCESS_START_TIME = Date.now(); /** * 子集化版本指纹 * * 纳入 subsetCache 的 key,让旧缓存条目在以下两种场景自动失效,无需手动清缓存: * - 生产:发版 bump package.json 的 version(构建期由 tsdown define 注入),旧缓存自然过期 - * - 开发:pnpm dev 重启进程时 process.uptime() 变化,内存缓存整体重置 + * - 开发:pnpm dev 重启进程时 PROCESS_START_TIME 变化,内存缓存整体重置 * * 杜绝「子集化代码已修但缓存返回旧错误结果」的陷阱。 - * - * 注意:不用 createRequire(import.meta.url) 读 package.json —— 该写法经 tsdown 打包为 CJS 后 - * 会引入 __filename,而 LLRT 运行时不提供 __filename,导致 ReferenceError。 */ -declare const PACKAGE_VERSION: string; -const SUBSET_CACHE_KEY = `${PACKAGE_VERSION}:${process.uptime()}`; +const SUBSET_CACHE_KEY = `${PACKAGE_VERSION}:${PROCESS_START_TIME}`; /** GET /api?font=...&text=... — 字体裁剪 */ export async function handleFontSubset(req: Request, res: Response) { diff --git a/package.json b/package.json index 7781616..56c9ed3 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,14 @@ { "name": "webfont", "private": true, - "version": "1.9.1", + "version": "1.9.2", "type": "module", "scripts": { "dev": "pnpx tsx scripts/dev-all.ts", "dev:frontend": "vite", "dev:backend": "pnpx tsx backend/app.ts", + "dev:backend:llrt": "pnpm build_backend && ./llrt ./dist_backend/app.cjs", + "serve:llrt": "./llrt ./dist_backend/app.cjs", "build": "vite build", "build_backend": "pnpx tsx scripts/build-backend.ts", "docker_build": "docker build -t llej0/web-font:${npm_package_version} -t llej0/web-font:latest .",