@@ -51,8 +51,8 @@ const MAX_REDIRECTS = 5
5151const DOWNLOAD_PROGRESS_STEP = 512 * 1024
5252const 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 ( / \/ r e l e a s e s \/ (?: t a g | d o w n l o a d ) \/ 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 ----------------------
87126export interface UpdateResult {
88127 version : string | null
0 commit comments