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
View File
@@ -368,6 +368,36 @@ export function apply(ctx: Context) {
},
})
// 更新检查中转:客户端页面来源是 http://127.0.0.1:<随机端口>,直接 fetch
// 自建 Gitea 属跨源请求;Gitea 不返回 Access-Control-Allow-Origin,浏览器会拦截
// (表现为「立即检查」无反应 / 「上次检查未成功」)。改由宿主服务端代取,同源无 CORS。
register({
kind: 'exact',
path: '/api/worktable/update-check',
handler: async (_req: any, res: any) => {
const upstream = 'https://gitea.ququclean.com/api/v1/repos/xiaoqu/dsh-worktable/releases/latest'
const ctrl = new AbortController()
const timer = setTimeout(() => ctrl.abort(), 8000)
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: any = 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: any) {
json(res, 502, { error: 'upstream unreachable: ' + String(err?.message ?? err) })
} finally {
clearTimeout(timer)
}
},
})
// 本地文件读取(资源管理器点击 .html 后浏览器标签内打开)
register({
kind: 'exact',