Skip to content

Commit d8da75b

Browse files
committed
fix: harden release update detection
1 parent 0548cbc commit d8da75b

6 files changed

Lines changed: 58 additions & 14 deletions

File tree

CLAUDE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ bin/
8484
- `tsx` — TypeScript 直接运行
8585

8686
## 变更日志
87+
- v1.2.14 更新检测修复:Release 检查优先走 GitHub latest 页面跳转并回退 API,避开匿名 API 403 限流;同时把更新时间与可用版本写回本地缓存,减少重复请求
8788
- v1.2.13 粘贴根因修复:移除对 ink-text-input 的关键依赖路径,改用本地稳定输入组件;多段 paste 不再因 React 受控输入闭包滞后而丢失前缀
8889
- v1.2.12 密钥完整性修复:表单为长密钥显示首尾摘要与长度,拦截空格/换行损坏,并要求二次确认后再保存,避免 WSL/终端粘贴异常时把截断密钥静默写盘
8990
- v1.2.11 更新体验修复:更新确认页展示准备/下载/替换阶段与下载进度,完成后短暂停留再退出;Linux/macOS 前台下载后原地替换,避免“确认后直接弹出”却看不见过程

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cc-switch-cli",
3-
"version": "1.2.13",
3+
"version": "1.2.14",
44
"description": "TUI for switching models in Claude Code, Codex, Gemini, and OpenClaw",
55
"type": "module",
66
"bin": {

src/app.tsx

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,22 @@ function App() {
151151
useEffect(() => {
152152
let cancelled = false
153153

154-
void checkForUpdates(store).then(({ version }) => {
154+
void checkForUpdates(store).then(({ version, checkedAt }) => {
155155
if (cancelled) return
156156
setUpdateChecking(false)
157+
158+
setStore(prev => {
159+
const next = {
160+
...prev,
161+
lastUpdateCheck: checkedAt,
162+
updateAvailable: version ?? undefined,
163+
}
164+
saveStore(next)
165+
return next
166+
})
167+
157168
if (version) {
158169
setUpdateAvailable(version)
159-
} else {
160-
// 无更新时清除缓存
161-
setStore(prev => {
162-
const next = { ...prev, updateAvailable: undefined }
163-
saveStore(next)
164-
return next
165-
})
166170
}
167171
})
168172

src/updater.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ const MAX_REDIRECTS = 5
5151
const DOWNLOAD_PROGRESS_STEP = 512 * 1024
5252
const DOWNLOAD_PROGRESS_INTERVAL_MS = 120
5353

54-
// ---------------------- 请求 GitHub API ----------------------
55-
function fetchLatest(): Promise<string | null> {
54+
// ---------------------- 请求 GitHub Release ----------------------
55+
function fetchLatestViaApi(): Promise<string | null> {
5656
return new Promise(resolve => {
5757
const req = https.get(
5858
`https://api.github.com/repos/${REPO}/releases/latest`,
@@ -83,6 +83,45 @@ function fetchLatest(): Promise<string | null> {
8383
})
8484
}
8585

86+
function extractVersionFromLocation(location: string | undefined): string | null {
87+
if (!location) return null
88+
89+
try {
90+
const pathname = new URL(location, `https://github.com/${REPO}/releases/latest`).pathname
91+
const match = pathname.match(/\/releases\/(?:tag|download)\/v?(\d+\.\d+\.\d+)(?:\/|$)/)
92+
return normalizeVersion(match?.[1] ?? null)
93+
} catch {
94+
return null
95+
}
96+
}
97+
98+
function fetchLatestViaRedirect(): Promise<string | null> {
99+
return new Promise(resolve => {
100+
const req = https.get(
101+
`https://github.com/${REPO}/releases/latest`,
102+
{ headers: { 'User-Agent': 'cc-switch-cli' } },
103+
res => {
104+
const version = extractVersionFromLocation(res.headers.location)
105+
res.resume()
106+
resolve(version)
107+
},
108+
)
109+
110+
req.on('error', () => resolve(null))
111+
req.setTimeout(5000, () => {
112+
req.destroy()
113+
resolve(null)
114+
})
115+
})
116+
}
117+
118+
async function fetchLatest(): Promise<string | null> {
119+
const redirectVersion = await fetchLatestViaRedirect()
120+
if (redirectVersion) return redirectVersion
121+
122+
return fetchLatestViaApi()
123+
}
124+
86125
// ---------------------- 公开 API ----------------------
87126
export interface UpdateResult {
88127
version: string | null

src/version.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
* [PROTOCOL]: 变更时更新此头部,然后检查 CLAUDE.md
66
*/
77

8-
export const VERSION = '1.2.13'
8+
export const VERSION = '1.2.14'

0 commit comments

Comments
 (0)