|
| 1 | +const fs = require("node:fs"); |
| 2 | +const https = require("node:https"); |
| 3 | +const http = require("node:http"); |
| 4 | +const os = require("node:os"); |
| 5 | +const path = require("node:path"); |
| 6 | +const { spawn } = require("node:child_process"); |
| 7 | +const { URL } = require("node:url"); |
| 8 | + |
| 9 | +const DEFAULT_DOWNLOAD_BASE = "https://downloads.clovapi.com/desktop/latest"; |
| 10 | +const DEFAULT_LATEST_URL = "https://downloads.clovapi.com/desktop/latest.txt"; |
| 11 | + |
| 12 | +const INSTALLER_BY_PLATFORM = { |
| 13 | + darwin: "clovapi-desktop-darwin-universal.dmg", |
| 14 | + win32: "clovapi-desktop-windows-x64.exe", |
| 15 | +}; |
| 16 | + |
| 17 | +function normalizeVersion(value) { |
| 18 | + return String(value || "") |
| 19 | + .trim() |
| 20 | + .replace(/^v/i, ""); |
| 21 | +} |
| 22 | + |
| 23 | +function compareVersions(left, right) { |
| 24 | + const a = normalizeVersion(left).split(".").map((part) => Number.parseInt(part, 10) || 0); |
| 25 | + const b = normalizeVersion(right).split(".").map((part) => Number.parseInt(part, 10) || 0); |
| 26 | + const length = Math.max(a.length, b.length); |
| 27 | + for (let index = 0; index < length; index += 1) { |
| 28 | + const av = a[index] || 0; |
| 29 | + const bv = b[index] || 0; |
| 30 | + if (av > bv) return 1; |
| 31 | + if (av < bv) return -1; |
| 32 | + } |
| 33 | + return 0; |
| 34 | +} |
| 35 | + |
| 36 | +function isNewerVersion(latest, current) { |
| 37 | + if (!normalizeVersion(latest) || !normalizeVersion(current)) return false; |
| 38 | + return compareVersions(latest, current) > 0; |
| 39 | +} |
| 40 | + |
| 41 | +function latestDesktopUrl() { |
| 42 | + return String(process.env.CLOVAPI_DESKTOP_LATEST_URL || DEFAULT_LATEST_URL).trim(); |
| 43 | +} |
| 44 | + |
| 45 | +function downloadBaseUrl() { |
| 46 | + return String(process.env.CLOVAPI_DESKTOP_DOWNLOAD_BASE || DEFAULT_DOWNLOAD_BASE).replace(/\/+$/, ""); |
| 47 | +} |
| 48 | + |
| 49 | +function installerFileName(platform = process.platform) { |
| 50 | + const name = INSTALLER_BY_PLATFORM[platform]; |
| 51 | + if (!name) { |
| 52 | + throw new Error(`Desktop updates are not supported on ${platform}.`); |
| 53 | + } |
| 54 | + return name; |
| 55 | +} |
| 56 | + |
| 57 | +function installerDownloadUrl(platform = process.platform) { |
| 58 | + return `${downloadBaseUrl()}/${installerFileName(platform)}`; |
| 59 | +} |
| 60 | + |
| 61 | +function fetchText(url, timeoutMs = 15_000) { |
| 62 | + return new Promise((resolve, reject) => { |
| 63 | + const requestUrl = new URL(url); |
| 64 | + const transport = requestUrl.protocol === "http:" ? http : https; |
| 65 | + const request = transport.get( |
| 66 | + requestUrl, |
| 67 | + { |
| 68 | + headers: { "User-Agent": "ClovAPI-Switcher-Desktop-Update" }, |
| 69 | + }, |
| 70 | + (response) => { |
| 71 | + if (response.statusCode && response.statusCode >= 300 && response.statusCode < 400 && response.headers.location) { |
| 72 | + response.resume(); |
| 73 | + fetchText(new URL(response.headers.location, requestUrl).toString(), timeoutMs) |
| 74 | + .then(resolve) |
| 75 | + .catch(reject); |
| 76 | + return; |
| 77 | + } |
| 78 | + if (response.statusCode !== 200) { |
| 79 | + response.resume(); |
| 80 | + reject(new Error(`HTTP ${response.statusCode || 0} fetching ${url}`)); |
| 81 | + return; |
| 82 | + } |
| 83 | + const chunks = []; |
| 84 | + response.on("data", (chunk) => chunks.push(chunk)); |
| 85 | + response.on("end", () => resolve(Buffer.concat(chunks).toString("utf8"))); |
| 86 | + }, |
| 87 | + ); |
| 88 | + request.setTimeout(timeoutMs, () => { |
| 89 | + request.destroy(new Error(`Timed out fetching ${url}`)); |
| 90 | + }); |
| 91 | + request.on("error", reject); |
| 92 | + }); |
| 93 | +} |
| 94 | + |
| 95 | +function downloadFile(url, outPath, timeoutMs = 30 * 60_000) { |
| 96 | + return new Promise((resolve, reject) => { |
| 97 | + const requestUrl = new URL(url); |
| 98 | + const transport = requestUrl.protocol === "http:" ? http : https; |
| 99 | + const request = transport.get( |
| 100 | + requestUrl, |
| 101 | + { |
| 102 | + headers: { "User-Agent": "ClovAPI-Switcher-Desktop-Update" }, |
| 103 | + }, |
| 104 | + (response) => { |
| 105 | + if (response.statusCode && response.statusCode >= 300 && response.statusCode < 400 && response.headers.location) { |
| 106 | + response.resume(); |
| 107 | + downloadFile(new URL(response.headers.location, requestUrl).toString(), outPath, timeoutMs) |
| 108 | + .then(resolve) |
| 109 | + .catch(reject); |
| 110 | + return; |
| 111 | + } |
| 112 | + if (response.statusCode !== 200) { |
| 113 | + response.resume(); |
| 114 | + reject(new Error(`HTTP ${response.statusCode || 0} downloading ${url}`)); |
| 115 | + return; |
| 116 | + } |
| 117 | + const file = fs.createWriteStream(outPath); |
| 118 | + response.pipe(file); |
| 119 | + file.on("finish", () => { |
| 120 | + file.close(() => resolve(outPath)); |
| 121 | + }); |
| 122 | + file.on("error", (error) => { |
| 123 | + file.close(() => { |
| 124 | + fs.rm(outPath, { force: true }, () => reject(error)); |
| 125 | + }); |
| 126 | + }); |
| 127 | + }, |
| 128 | + ); |
| 129 | + request.setTimeout(timeoutMs, () => { |
| 130 | + request.destroy(new Error(`Timed out downloading ${url}`)); |
| 131 | + }); |
| 132 | + request.on("error", reject); |
| 133 | + }); |
| 134 | +} |
| 135 | + |
| 136 | +async function fetchLatestDesktopVersion() { |
| 137 | + const latest = (await fetchText(latestDesktopUrl())).trim(); |
| 138 | + if (!latest) throw new Error("Desktop latest version response was empty."); |
| 139 | + return latest.startsWith("v") ? latest : `v${latest}`; |
| 140 | +} |
| 141 | + |
| 142 | +async function checkDesktopUpdate(currentVersion) { |
| 143 | + const current = normalizeVersion(currentVersion); |
| 144 | + if (!current) { |
| 145 | + return { ok: false, error: "Current desktop version is unavailable." }; |
| 146 | + } |
| 147 | + if (!INSTALLER_BY_PLATFORM[process.platform]) { |
| 148 | + return { ok: false, error: `Desktop updates are not supported on ${process.platform}.` }; |
| 149 | + } |
| 150 | + |
| 151 | + const latestTag = await fetchLatestDesktopVersion(); |
| 152 | + const latest = normalizeVersion(latestTag); |
| 153 | + const upToDate = !isNewerVersion(latest, current); |
| 154 | + |
| 155 | + return { |
| 156 | + ok: true, |
| 157 | + current_version: current, |
| 158 | + latest_version: latest, |
| 159 | + latest_tag: latestTag, |
| 160 | + up_to_date: upToDate, |
| 161 | + download_url: upToDate ? "" : installerDownloadUrl(), |
| 162 | + installer_name: installerFileName(), |
| 163 | + }; |
| 164 | +} |
| 165 | + |
| 166 | +function launchInstaller(installerPath) { |
| 167 | + if (process.platform === "win32") { |
| 168 | + spawn(installerPath, [], { |
| 169 | + detached: true, |
| 170 | + stdio: "ignore", |
| 171 | + windowsHide: false, |
| 172 | + }).unref(); |
| 173 | + return; |
| 174 | + } |
| 175 | + if (process.platform === "darwin") { |
| 176 | + spawn("open", [installerPath], { |
| 177 | + detached: true, |
| 178 | + stdio: "ignore", |
| 179 | + }).unref(); |
| 180 | + return; |
| 181 | + } |
| 182 | + throw new Error(`Desktop updates are not supported on ${process.platform}.`); |
| 183 | +} |
| 184 | + |
| 185 | +async function downloadAndLaunchDesktopUpdate() { |
| 186 | + const fileName = installerFileName(); |
| 187 | + const url = installerDownloadUrl(); |
| 188 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "clovapi-desktop-update-")); |
| 189 | + const installerPath = path.join(tmpDir, fileName); |
| 190 | + await downloadFile(url, installerPath); |
| 191 | + launchInstaller(installerPath); |
| 192 | + return { ok: true, path: installerPath, url }; |
| 193 | +} |
| 194 | + |
| 195 | +module.exports = { |
| 196 | + compareVersions, |
| 197 | + isNewerVersion, |
| 198 | + normalizeVersion, |
| 199 | + installerDownloadUrl, |
| 200 | + fetchLatestDesktopVersion, |
| 201 | + checkDesktopUpdate, |
| 202 | + downloadAndLaunchDesktopUpdate, |
| 203 | +}; |
0 commit comments