fix(v0.5.1): 适配rc.5并修正控制室与更新提醒

This commit is contained in:
Aisland
2026-10-31 17:05:17 +08:00
parent 56ffe0cd1c
commit f98246ef0c
25 changed files with 703 additions and 237 deletions
+50 -16
View File
@@ -11,14 +11,45 @@ const K_LAST_CHECK = 'dsh.worktable.lastUpdateCheck.v1'
const K_SKIP = 'dsh.worktable.skipVersion.v1'
const K_CACHE = 'dsh.worktable.updateCache.v1'
export function cmpVer(a: string, b: string): number {
const pa = String(a).split('.').map((x) => parseInt(x, 10) || 0)
const pb = String(b).split('.').map((x) => parseInt(x, 10) || 0)
for (let i = 0; i < Math.max(pa.length, pb.length); i++) {
const d = (pa[i] || 0) - (pb[i] || 0)
if (d !== 0) return d > 0 ? 1 : -1
/** Strict version precedence; invalid input is incomparable, never a potential update. */
function versionParts(value: string): { core: string[]; pre: string[] } | null {
if (typeof value !== 'string') return null
const match = /^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/.exec(value.trim())
if (!match) return null
const pre = match[4]?.split('.') ?? []
if (pre.some((part) => /^\d+$/.test(part) && part.length > 1 && part[0] === '0')) return null
return { core: match.slice(1, 4), pre }
}
function numericOrder(a: string, b: string): number {
if (a.length !== b.length) return a.length > b.length ? 1 : -1
return a === b ? 0 : a > b ? 1 : -1
}
export function cmpVer(a: string, b: string): number | null {
const pa = versionParts(a), pb = versionParts(b)
if (!pa || !pb) return null
for (let i = 0; i < 3; i++) {
const order = numericOrder(pa.core[i], pb.core[i])
if (order !== 0) return order
}
return 0
if (!pa.pre.length || !pb.pre.length) return pa.pre.length ? -1 : pb.pre.length ? 1 : 0
for (let i = 0; i < Math.max(pa.pre.length, pb.pre.length); i++) {
const x = pa.pre[i], y = pb.pre[i]
if (x === undefined || y === undefined) return x === undefined ? -1 : 1
if (x === y) continue
const nx = /^\d+$/.test(x), ny = /^\d+$/.test(y)
if (nx && ny) return numericOrder(x, y)
if (nx !== ny) return nx ? -1 : 1
return x > y ? 1 : -1
}
return 0 // Build metadata has no effect on version precedence.
}
/** Shared by cache reads and both UIs. Preserve identity to avoid render-effect churn. */
export function newerUpdateInfo(value: unknown): UpdateInfo | null {
const info = value as UpdateInfo | null
if (!info || typeof info.latest !== 'string' || typeof info.notes !== 'string' || typeof info.url !== 'string') return null
if (cmpVer(info.latest, LOCAL_VERSION) !== 1 || cmpVer(info.latest, getSkipVersion()) === 0) return null
return info
}
export function getSkipVersion(): string {
@@ -38,21 +69,23 @@ export function readCache(): { status: UpdateStatus; info: UpdateInfo | null } {
const raw = localStorage.getItem(K_CACHE)
if (raw) {
const c = JSON.parse(raw)
if (c && typeof c.status === 'string') {
return { status: c.status === 'checking' ? 'idle' : c.status, info: c.info ?? null }
if (c?.status === 'uptodate' || c?.status === 'failed' || c?.status === 'idle') {
return { status: c.status, info: c.status === 'uptodate' ? newerUpdateInfo(c.info) : null }
}
}
} catch {}
return { status: 'idle', info: null }
}
export function writeCache(status: UpdateStatus, info: UpdateInfo | null) {
try { localStorage.setItem(K_CACHE, JSON.stringify({ status, info })) } catch {}
try { localStorage.setItem(K_CACHE, JSON.stringify({ status, info: status === 'uptodate' ? newerUpdateInfo(info) : null })) } catch {}
}
/** 检查更新:节流一天一次(force 绕过);8s 超时 × 3 重试;防重入由调用方保证 */
export async function checkUpdate(force = false): Promise<{ status: UpdateStatus; info: UpdateInfo | null }> {
const last = Number(localStorage.getItem(K_LAST_CHECK) ?? '0')
if (!force && Date.now() - last < 24 * 3600 * 1000) return readCache()
let last = 0
try { last = Number(localStorage.getItem(K_LAST_CHECK) ?? '0') } catch {}
const age = Date.now() - last
if (!force && Number.isFinite(last) && last > 0 && age >= 0 && age < 24 * 3600 * 1000) return readCache()
try { localStorage.setItem(K_LAST_CHECK, String(Date.now())) } catch {}
let d: { tag_name?: string; body?: string; html_url?: string } | null = null
for (let attempt = 0; attempt < 3 && !d; attempt++) {
@@ -66,10 +99,11 @@ export async function checkUpdate(force = false): Promise<{ status: UpdateStatus
finally { clearTimeout(timer) }
}
if (!d) { writeCache('failed', null); return { status: 'failed', info: null } }
const tag = (d.tag_name ?? '').replace(/^v/, '')
if (!tag || cmpVer(tag, LOCAL_VERSION) <= 0) { writeCache('uptodate', null); return { status: 'uptodate', info: null } }
if (getSkipVersion() === tag) { writeCache('uptodate', null); return { status: 'uptodate', info: null } }
const info: UpdateInfo = { latest: tag, notes: (d.body ?? '').slice(0, 800), url: d.html_url ?? '' }
const tag = typeof d.tag_name === 'string' ? d.tag_name.trim() : ''
const order = cmpVer(tag, LOCAL_VERSION)
if (order == null) { writeCache('failed', null); return { status: 'failed', info: null } }
if (order <= 0) { writeCache('uptodate', null); return { status: 'uptodate', info: null } }
const info = newerUpdateInfo({ latest: tag.replace(/^v/, ''), notes: typeof d.body === 'string' ? d.body.slice(0, 800) : '', url: typeof d.html_url === 'string' ? d.html_url : '' })
writeCache('uptodate', info)
return { status: 'uptodate', info }
}