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
+1 -1
View File
@@ -38,7 +38,7 @@ export function hostPreviewUrl(
/** Keep update instructions on the active profile; Desktop must use its bundled CLI. */
// 本仓发布在自建 Gitea:它不支持 GitHub 的 /releases/latest/download 简写,
// 安装命令必须带具体 tag。这里给固定 tag(随发布更新),避免给出 404 的命令。
const UPDATE_TGZ = 'https://gitea.ququclean.com/xiaoqu/dsh-worktable/releases/download/v0.5.3-local.7/dsh-worktable.tgz'
const UPDATE_TGZ = 'https://gitea.ququclean.com/xiaoqu/dsh-worktable/releases/download/v0.5.3-local.8/dsh-worktable.tgz'
export function worktableUpgrade(protocol = typeof location === 'undefined' ? 'http:' : location.protocol) {
const desktop = protocol === 'dsh-app:'
+3 -3
View File
@@ -33,8 +33,8 @@ type DockMode = 'footer' | 'float'
// ── 更新检查(客户端直连本仓 Gitea Releases API,只读 GET;失败静默)──
// 本仓是内部补丁分发仓:上游只发官方版(无本地补丁),
// 查上游既看不到本仓版本,国内网络也常失败。
const UPDATE_HOST = 'https://gitea.ququclean.com'
const UPDATE_REPO = 'xiaoqu/dsh-worktable'
// 走宿主中转:页面来源是随机端口 127.0.0.1,直连 Gitea 跨源且无 CORS 头会被拦截
const UPDATE_API = '/api/worktable/update-check'
const { command: UPGRADE_CMD, prompt: UPGRADE_AI, desktop: DESKTOP_HOST } = worktableUpgrade()
// 更新提示图标(手绘 SVG,避免 emoji 跨平台渲染差异)
const ICON_SYNC = (
@@ -1097,7 +1097,7 @@ function WorktableSection(props: any) {
const ctrl = new AbortController()
const timer = setTimeout(() => ctrl.abort(), 8000) // 单次 8s 超时,计入下一轮重试
try {
const r = await fetch(UPDATE_HOST + '/api/v1/repos/' + UPDATE_REPO + '/releases/latest', { headers: { Accept: 'application/json' }, cache: 'no-store', signal: ctrl.signal })
const r = await fetch(UPDATE_API, { headers: { Accept: 'application/json' }, cache: 'no-store', signal: ctrl.signal })
if (r.ok) d = await r.json() as { tag_name?: string; body?: string; html_url?: string }
else if (r.status === 403 || r.status === 404) break // 限流/不存在:不再重试
} catch { /* 网络抖动/超时:下一轮重试 */ }
+4 -3
View File
@@ -3,8 +3,9 @@ declare const __WT_VERSION__: string
export const LOCAL_VERSION = typeof __WT_VERSION__ === 'undefined' ? 'dev' : __WT_VERSION__
// 本仓是内部补丁分发仓,更新源必须是自己的 Gitea,而不是上游 GitHub:
// 上游只发布官方版(无本地补丁),查它既看不到本仓版本,国内网络也常失败。
const UPDATE_HOST = 'https://gitea.ququclean.com'
const UPDATE_REPO = 'xiaoqu/dsh-worktable'
// 走宿主中转(/api/worktable/update-check):页面来源是随机端口的 127.0.0.1,
// 直连 Gitea 属跨源且 Gitea 无 CORS 头,浏览器会拦截请求。
const UPDATE_API = '/api/worktable/update-check'
export type UpdateStatus = 'idle' | 'checking' | 'uptodate' | 'failed'
export type UpdateInfo = { latest: string; notes: string; url: string }
@@ -95,7 +96,7 @@ export async function checkUpdate(force = false): Promise<{ status: UpdateStatus
const ctrl = new AbortController()
const timer = setTimeout(() => ctrl.abort(), 8000)
try {
const r = await fetch(UPDATE_HOST + '/api/v1/repos/' + UPDATE_REPO + '/releases/latest', { headers: { Accept: 'application/json' }, cache: 'no-store', signal: ctrl.signal })
const r = await fetch(UPDATE_API, { headers: { Accept: 'application/json' }, cache: 'no-store', signal: ctrl.signal })
if (r.ok) d = (await r.json()) as { tag_name?: string; body?: string; html_url?: string }
else if (r.status === 403 || r.status === 404) break
} catch { /* 网络抖动/超时:下一轮重试 */ }