@@ -6,7 +6,7 @@ const path = require("node:path");
66const { spawn } = require ( "node:child_process" ) ;
77const { URL } = require ( "node:url" ) ;
88
9- const DEFAULT_DOWNLOAD_BASE = "https://downloads.clovapi.com/desktop/latest " ;
9+ const DEFAULT_DOWNLOAD_ROOT = "https://downloads.clovapi.com/desktop" ;
1010const DEFAULT_LATEST_URL = "https://downloads.clovapi.com/desktop/latest.txt" ;
1111
1212const INSTALLER_BY_PLATFORM = {
@@ -42,8 +42,20 @@ function latestDesktopUrl() {
4242 return String ( process . env . CLOVAPI_DESKTOP_LATEST_URL || DEFAULT_LATEST_URL ) . trim ( ) ;
4343}
4444
45- function downloadBaseUrl ( ) {
46- return String ( process . env . CLOVAPI_DESKTOP_DOWNLOAD_BASE || DEFAULT_DOWNLOAD_BASE ) . replace ( / \/ + $ / , "" ) ;
45+ function desktopDownloadRoot ( ) {
46+ const override = String ( process . env . CLOVAPI_DESKTOP_DOWNLOAD_ROOT || "" ) . trim ( ) ;
47+ if ( override ) return override . replace ( / \/ + $ / , "" ) ;
48+ const legacyBase = String ( process . env . CLOVAPI_DESKTOP_DOWNLOAD_BASE || "" ) . trim ( ) . replace ( / \/ + $ / , "" ) ;
49+ if ( legacyBase . endsWith ( "/latest" ) ) {
50+ return legacyBase . slice ( 0 , - "/latest" . length ) ;
51+ }
52+ return DEFAULT_DOWNLOAD_ROOT ;
53+ }
54+
55+ function normalizeTag ( value ) {
56+ const trimmed = String ( value || "" ) . trim ( ) ;
57+ if ( ! trimmed ) return "" ;
58+ return trimmed . startsWith ( "v" ) ? trimmed : `v${ trimmed } ` ;
4759}
4860
4961function installerFileName ( platform = process . platform ) {
@@ -54,8 +66,12 @@ function installerFileName(platform = process.platform) {
5466 return name ;
5567}
5668
57- function installerDownloadUrl ( platform = process . platform ) {
58- return `${ downloadBaseUrl ( ) } /${ installerFileName ( platform ) } ` ;
69+ function installerDownloadUrl ( versionTag , platform = process . platform ) {
70+ const tag = normalizeTag ( versionTag ) ;
71+ if ( ! tag ) {
72+ throw new Error ( "Desktop version tag is required." ) ;
73+ }
74+ return `${ desktopDownloadRoot ( ) } /${ tag } /${ installerFileName ( platform ) } ` ;
5975}
6076
6177function fetchText ( url , timeoutMs = 15_000 ) {
@@ -158,7 +174,7 @@ async function checkDesktopUpdate(currentVersion) {
158174 latest_version : latest ,
159175 latest_tag : latestTag ,
160176 up_to_date : upToDate ,
161- download_url : upToDate ? "" : installerDownloadUrl ( ) ,
177+ download_url : upToDate ? "" : installerDownloadUrl ( latestTag ) ,
162178 installer_name : installerFileName ( ) ,
163179 } ;
164180}
@@ -183,19 +199,22 @@ function launchInstaller(installerPath) {
183199}
184200
185201async function downloadAndLaunchDesktopUpdate ( ) {
202+ const latestTag = await fetchLatestDesktopVersion ( ) ;
186203 const fileName = installerFileName ( ) ;
187- const url = installerDownloadUrl ( ) ;
204+ const url = installerDownloadUrl ( latestTag ) ;
188205 const tmpDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "clovapi-desktop-update-" ) ) ;
189206 const installerPath = path . join ( tmpDir , fileName ) ;
190207 await downloadFile ( url , installerPath ) ;
191208 launchInstaller ( installerPath ) ;
192- return { ok : true , path : installerPath , url } ;
209+ return { ok : true , path : installerPath , url, latest_tag : latestTag } ;
193210}
194211
195212module . exports = {
196213 compareVersions,
197214 isNewerVersion,
198215 normalizeVersion,
216+ normalizeTag,
217+ desktopDownloadRoot,
199218 installerDownloadUrl,
200219 fetchLatestDesktopVersion,
201220 checkDesktopUpdate,
0 commit comments