Skip to content

Commit a248388

Browse files
committed
refactor(version): 复审两 nit——fetchImpl 兜底免同步抛 + PACKAGE_VERSION 去重
1. fetchLatestVersion 默认参 fetchImpl = fetch:全局 fetch 缺席时会在参数 求值期同步抛 ReferenceError,路由的 .catch 接不住,与「绝不新增故障面」 冲突。改为 typeof 探测 + 恒拒兜底,无 fetch 环境归入常规失败路径静默 返 null;补回归测试(暂置 globalThis.fetch = undefined 断言 resolve null)。 2. PACKAGE_VERSION 读 package.json 逻辑 api.js / version-check.js 各一份, 收敛为 version-check.js 单份导出,/info 与 /version-check 共用;api.js 顺带移除不再使用的 readFileSync 导入。
1 parent c6ebf7b commit a248388

5 files changed

Lines changed: 22 additions & 26 deletions

File tree

‎dsh-mneme/lib/api.js‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { URL } from "node:url";
2-
import { readFileSync } from "node:fs";
32
import { timingSafeEqual, randomBytes } from "node:crypto";
43
import { FEATURE_FLAG_SPEC } from "./settings.js";
54
import { TYPE_FILE, renderMirrorText, parseHumanEdits } from "./mirror.js";
@@ -8,7 +7,7 @@ import { computeHeat } from "./heat.js";
87
import { describeStreamFailure, resolveRoute } from "./dream.js";
98
import { describeLocalRuntime, publicRuntimeStatus } from "./runtime/loader.js";
109
import { hostModulesDir, provisionRuntime } from "./runtime/provision.js";
11-
import { classify, fetchLatestVersion } from "./version-check.js";
10+
import { classify, fetchLatestVersion, PACKAGE_VERSION } from "./version-check.js";
1211

1312
// headers:少数端点(/export 附件下载)需要追加 Content-Disposition 等响应头。
1413
function sendJson(res, status, payload, headers = {}) {
@@ -25,16 +24,8 @@ function sendAttachment(res, status, contentType, filename, body) {
2524
res.end(body);
2625
}
2726

28-
// 导出 JSON 的 version 字段:读插件根的 package.json(src/ 与 lib/ 都在根下
29-
// 一层,相对 import.meta.url 解析一致)。读取失败(打包/受限环境)降级为
30-
// "unknown",导出本身仍然可用。
31-
const PACKAGE_VERSION = (() => {
32-
try {
33-
return JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")).version ?? "unknown";
34-
} catch {
35-
return "unknown";
36-
}
37-
})();
27+
// 版本号统一从 version-check.js 取(同一份读 package.json 逻辑,读取失败
28+
// 同样降级 "unknown")——/info 与 /version-check 两个路由共用,不再各写一份。
3829

3930
// Defaults for the standalone external API — keep in step with the schema
4031
// defaults in config.js (externalApiPort / externalApiHost).

‎dsh-mneme/lib/version-check.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ let cache = { at: -Infinity, latest: null };
9595
* 响应形状不对/URL 校验不过)都静默返回 null——版本提示是锦上添花,绝不
9696
* 让它成为新的故障面。失败不写缓存:TTL 起点保持旧值,下次调用立刻重试。
9797
*/
98-
export async function fetchLatestVersion({ fetchImpl = fetch, now = Date.now } = {}) {
98+
export async function fetchLatestVersion({ fetchImpl = typeof fetch === "function" ? fetch : () => Promise.reject(new Error("no fetch")), now = Date.now } = {}) {
9999
if (now() - cache.at < CACHE_TTL_MS) return cache.latest;
100100
if (!validateRegistryUrl(REGISTRY_URL)) return null;
101101
let manifest;

‎dsh-mneme/src/api.js‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { URL } from "node:url";
2-
import { readFileSync } from "node:fs";
32
import { timingSafeEqual, randomBytes } from "node:crypto";
43
import { FEATURE_FLAG_SPEC } from "./settings.js";
54
import { TYPE_FILE, renderMirrorText, parseHumanEdits } from "./mirror.js";
@@ -8,7 +7,7 @@ import { computeHeat } from "./heat.js";
87
import { describeStreamFailure, resolveRoute } from "./dream.js";
98
import { describeLocalRuntime, publicRuntimeStatus } from "./runtime/loader.js";
109
import { hostModulesDir, provisionRuntime } from "./runtime/provision.js";
11-
import { classify, fetchLatestVersion } from "./version-check.js";
10+
import { classify, fetchLatestVersion, PACKAGE_VERSION } from "./version-check.js";
1211

1312
// headers:少数端点(/export 附件下载)需要追加 Content-Disposition 等响应头。
1413
function sendJson(res, status, payload, headers = {}) {
@@ -25,16 +24,8 @@ function sendAttachment(res, status, contentType, filename, body) {
2524
res.end(body);
2625
}
2726

28-
// 导出 JSON 的 version 字段:读插件根的 package.json(src/ 与 lib/ 都在根下
29-
// 一层,相对 import.meta.url 解析一致)。读取失败(打包/受限环境)降级为
30-
// "unknown",导出本身仍然可用。
31-
const PACKAGE_VERSION = (() => {
32-
try {
33-
return JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")).version ?? "unknown";
34-
} catch {
35-
return "unknown";
36-
}
37-
})();
27+
// 版本号统一从 version-check.js 取(同一份读 package.json 逻辑,读取失败
28+
// 同样降级 "unknown")——/info 与 /version-check 两个路由共用,不再各写一份。
3829

3930
// Defaults for the standalone external API — keep in step with the schema
4031
// defaults in config.js (externalApiPort / externalApiHost).

‎dsh-mneme/src/version-check.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ let cache = { at: -Infinity, latest: null };
9595
* 响应形状不对/URL 校验不过)都静默返回 null——版本提示是锦上添花,绝不
9696
* 让它成为新的故障面。失败不写缓存:TTL 起点保持旧值,下次调用立刻重试。
9797
*/
98-
export async function fetchLatestVersion({ fetchImpl = fetch, now = Date.now } = {}) {
98+
export async function fetchLatestVersion({ fetchImpl = typeof fetch === "function" ? fetch : () => Promise.reject(new Error("no fetch")), now = Date.now } = {}) {
9999
if (now() - cache.at < CACHE_TTL_MS) return cache.latest;
100100
if (!validateRegistryUrl(REGISTRY_URL)) return null;
101101
let manifest;

‎dsh-mneme/test/version-check.test.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,20 @@ describe('version-check.js', () => {
128128
assert.equal(await fetchLatestVersion({ fetchImpl, now: () => 1000 }), '0.8.2');
129129
resetCacheForTest();
130130
});
131+
132+
test('运行环境无全局 fetch 时走默认参兜底:resolve null 而非同步抛', async () => {
133+
resetCacheForTest();
134+
const original = globalThis.fetch;
135+
globalThis.fetch = undefined;
136+
try {
137+
// 修复前:默认参 fetchImpl = fetch 在参数求值期同步抛 ReferenceError,
138+
// 路由的 .catch 接不住;修复后应归入常规失败路径静默返 null。
139+
assert.equal(await fetchLatestVersion({ now: () => 1000 }), null);
140+
} finally {
141+
globalThis.fetch = original;
142+
}
143+
resetCacheForTest();
144+
});
131145
});
132146

133147
// ---- 模块常量:URL 必须过白名单,运行版本可读 ----

0 commit comments

Comments
 (0)