|
| 1 | +const NPM_REGISTRY_URL = "https://registry.npmjs.org"; |
| 2 | +const CHECK_TIMEOUT_MS = 3000; |
| 3 | + |
| 4 | +export interface UpdateInfo { |
| 5 | + currentVersion: string |
| 6 | + latestVersion: string |
| 7 | + updateCommand: string |
| 8 | +} |
| 9 | + |
| 10 | +function parseVersion( |
| 11 | + version: string, |
| 12 | +): { parts: number[]; prerelease: string | null } | null { |
| 13 | + const normalized = version.trim().replace(/^v/i, "") |
| 14 | + const match = normalized.match(/^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?/) |
| 15 | + if (!match) return null |
| 16 | + |
| 17 | + return { |
| 18 | + parts: [Number(match[1]), Number(match[2]), Number(match[3])], |
| 19 | + prerelease: match[4] ?? null, |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +function isVersionNewer(latestVersion: string, currentVersion: string): boolean { |
| 24 | + const latest = parseVersion(latestVersion) |
| 25 | + const current = parseVersion(currentVersion) |
| 26 | + if (!latest || !current) return latestVersion !== currentVersion |
| 27 | + |
| 28 | + for (let i = 0; i < 3; i++) { |
| 29 | + const latestPart = latest.parts[i] ?? 0 |
| 30 | + const currentPart = current.parts[i] ?? 0 |
| 31 | + if (latestPart > currentPart) return true |
| 32 | + if (latestPart < currentPart) return false |
| 33 | + } |
| 34 | + |
| 35 | + if (!latest.prerelease && current.prerelease) return true |
| 36 | + if (latest.prerelease && !current.prerelease) return false |
| 37 | + return latest.prerelease !== current.prerelease && latest.prerelease !== null |
| 38 | +} |
| 39 | + |
| 40 | +export async function checkNpmUpdate( |
| 41 | + packageName: string, |
| 42 | + currentVersion: string, |
| 43 | + updateCommand: string, |
| 44 | +): Promise<UpdateInfo | null> { |
| 45 | + const controller = new AbortController() |
| 46 | + const timeout = setTimeout(() => controller.abort(), CHECK_TIMEOUT_MS) |
| 47 | + |
| 48 | + try { |
| 49 | + const encodedPackage = packageName.startsWith("@") |
| 50 | + ? packageName.replace("/", "%2F") |
| 51 | + : encodeURIComponent(packageName) |
| 52 | + const response = await fetch(`${NPM_REGISTRY_URL}/${encodedPackage}/latest`, { |
| 53 | + signal: controller.signal, |
| 54 | + }) |
| 55 | + if (!response.ok) return null |
| 56 | + |
| 57 | + const data = (await response.json()) as { version?: unknown } |
| 58 | + const latestVersion = typeof data.version === "string" ? data.version : null |
| 59 | + if (!latestVersion || !isVersionNewer(latestVersion, currentVersion)) { |
| 60 | + return null |
| 61 | + } |
| 62 | + |
| 63 | + return { currentVersion, latestVersion, updateCommand } |
| 64 | + } catch { |
| 65 | + return null |
| 66 | + } finally { |
| 67 | + clearTimeout(timeout) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +export function formatUpdateNotice(info: UpdateInfo): string { |
| 72 | + return [ |
| 73 | + "supermemory: update available", |
| 74 | + `current: v${info.currentVersion}`, |
| 75 | + `latest: v${info.latestVersion}`, |
| 76 | + `run: ${info.updateCommand}`, |
| 77 | + ].join(" | ") |
| 78 | +} |
0 commit comments