fix(updater): refresh changed update offers before installation

This commit is contained in:
Tunglies
2026-11-03 18:57:05 +08:00
parent 40fd1d5012
commit 2b7b7cc0e7
9 changed files with 87 additions and 148 deletions
+6 -1
View File
@@ -24,7 +24,10 @@ export const commands = {
getSystemHostname: () => __TAURI_INVOKE<string>('get_system_hostname'),
restartApp: () => __TAURI_INVOKE<null>('restart_app'),
installUpdate: (version: string, onEvent: Channel<DownloadEvent>) =>
__TAURI_INVOKE<boolean>('install_update', { version, onEvent }),
__TAURI_INVOKE<InstallUpdateOutcome>('install_update', {
version,
onEvent,
}),
cancelUpdateDownload: () => __TAURI_INVOKE<void>('cancel_update_download'),
restartCore: () => __TAURI_INVOKE<null>('restart_core'),
/** Replaces the managed core binary atomically instead of letting mihomo overwrite itself. */
@@ -536,6 +539,8 @@ export type IconInfo = {
current_t: string
}
export type InstallUpdateOutcome = 'installed' | 'cancelled' | 'update_changed'
export type InterfaceAddress =
| ({ V4: IpAddress } & { V6?: never })
| ({ V6: IpAddress } & { V4?: never })
+1 -45
View File
@@ -7,11 +7,6 @@ import { compareVersions as compareSemver } from 'compare-versions'
import { version as appVersion } from '@root/package.json'
const SEMVER_FULL_REGEX =
/^\d+(?:\.\d+){1,2}(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/
const SEMVER_SEARCH_REGEX =
/v?\d+(?:\.\d+){1,2}(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?/i
const normalizeVersion = (input: string | null | undefined): string | null => {
if (typeof input !== 'string') return null
const trimmed = input.trim()
@@ -19,19 +14,6 @@ const normalizeVersion = (input: string | null | undefined): string | null => {
return trimmed.replace(/^v/i, '')
}
const ensureSemver = (input: string | null | undefined): string | null => {
const normalized = normalizeVersion(input)
if (!normalized) return null
return SEMVER_FULL_REGEX.test(normalized) ? normalized : null
}
const extractSemver = (input: string | null | undefined): string | null => {
if (typeof input !== 'string') return null
const match = input.match(SEMVER_SEARCH_REGEX)
if (!match) return null
return normalizeVersion(match[0])
}
const compareVersions = (a: string | null, b: string | null): number | null => {
if (!a || !b) return null
try {
@@ -41,32 +23,6 @@ const compareVersions = (a: string | null, b: string | null): number | null => {
}
}
const resolveRemoteVersion = (update: Update): string | null => {
const primary = ensureSemver(update.version)
if (primary) return primary
const fallbackPrimary = extractSemver(update.version)
if (fallbackPrimary) return fallbackPrimary
const raw = update.rawJson ?? {}
const rawVersion = ensureSemver(
typeof raw.version === 'string' ? raw.version : null,
)
if (rawVersion) return rawVersion
const tagVersion = extractSemver(
typeof raw.tag_name === 'string' ? raw.tag_name : null,
)
if (tagVersion) return tagVersion
const nameVersion = extractSemver(
typeof raw.name === 'string' ? raw.name : null,
)
if (nameVersion) return nameVersion
return null
}
const localVersionNormalized = normalizeVersion(appVersion)
export const checkUpdateSafe = async (
@@ -75,7 +31,7 @@ export const checkUpdateSafe = async (
const result = await check(options ?? {})
if (!result) return null
const remoteVersion = resolveRemoteVersion(result)
const remoteVersion = result.version
const comparison = compareVersions(remoteVersion, localVersionNormalized)
const isBuildToStable =