fix(update): 更新检查改走宿主中转,修复 Gitea 无 CORS 导致请求被拦 (v0.5.3-local.8)

0.5.3-local.6 把更新检查从上游 GitHub 改到自建 Gitea API,但仍不通:
页面来源是 http://127.0.0.1:<随机端口>,请求 https://gitea.ququclean.com/api/...
属跨源,而 Gitea 不返回 Access-Control-Allow-Origin,浏览器直接拦截 fetch。
表现为「立即检查」无反应、长期显示「上次检查未成功」。

改为宿主中转:
- src/index.ts 新增 /api/worktable/update-check 路由,服务端(Node 无 CORS
  约束)代取 Gitea releases/latest,客户端只请求同源路径
- updateCheck.ts / index.tsx 的 fetch 改指向该同源路由
- 该路由沿既有约定需要鉴权(request-auth.test.mjs 要求除 health/preview-static
  外所有路由都必须拒绝未鉴权请求),未传 publicAsset

测试:sidebar-popup.test.mjs 的 releaseUrl 改为 /api/worktable/update-check。

验证:npm run pack 全绿,双资产同源 sha256 225e8568…。
This commit is contained in:
wangheng
2026-12-02 17:30:41 +08:00
parent 6b8349073d
commit 1b053e5577
11 changed files with 96 additions and 28 deletions
+30 -1
View File
@@ -382,7 +382,7 @@ function resolveDshHomeSafe() {
cachedDshHome = baseDshHome();
return cachedDshHome;
}
var PLUGIN_VERSION = false ? "dev" : "0.5.3-local.7";
var PLUGIN_VERSION = false ? "dev" : "0.5.3-local.8";
var name = "dsh-worktable";
var inject = ["webServer", "sessions"];
var HEALTH_PATH = "/api/worktable/health";
@@ -743,6 +743,35 @@ function apply(ctx) {
json(res, 200, { plugin: "dsh-worktable", version: PLUGIN_VERSION, ok: true });
}
});
register({
kind: "exact",
path: "/api/worktable/update-check",
handler: async (_req, res) => {
const upstream = "https://gitea.ququclean.com/api/v1/repos/xiaoqu/dsh-worktable/releases/latest";
const ctrl = new AbortController();
const timer = setTimeout(() => ctrl.abort(), 8e3);
try {
const r = await fetch(upstream, {
headers: { Accept: "application/json", "user-agent": "dsh-worktable/" + PLUGIN_VERSION },
signal: ctrl.signal
});
if (!r.ok) {
json(res, 502, { error: "upstream status " + r.status });
return;
}
const d = await r.json();
json(res, 200, {
tag_name: typeof d?.tag_name === "string" ? d.tag_name : "",
body: typeof d?.body === "string" ? d.body : "",
html_url: typeof d?.html_url === "string" ? d.html_url : ""
});
} catch (err) {
json(res, 502, { error: "upstream unreachable: " + String(err?.message ?? err) });
} finally {
clearTimeout(timer);
}
}
});
register({
kind: "exact",
path: "/api/worktable/file",