refactor(api): implement caching for tunnel and version status endpoints
This commit is contained in:
@@ -2,6 +2,10 @@ import https from "https";
|
||||
import pkg from "../../../../package.json" with { type: "json" };
|
||||
|
||||
const NPM_PACKAGE_NAME = "9router";
|
||||
const VERSION_CACHE_TTL_MS = 3600000; // cache npm latest lookup for 1h
|
||||
|
||||
// Survive hot reload; one cache per process
|
||||
const versionCache = (global.__npmVersionCache ??= { value: null, fetchedAt: 0 });
|
||||
|
||||
// Fetch latest version from npm registry
|
||||
function fetchLatestVersion() {
|
||||
@@ -36,8 +40,20 @@ function compareVersions(a, b) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
async function getLatestVersionCached() {
|
||||
if (versionCache.value && Date.now() - versionCache.fetchedAt < VERSION_CACHE_TTL_MS) {
|
||||
return versionCache.value;
|
||||
}
|
||||
const latest = await fetchLatestVersion();
|
||||
if (latest) {
|
||||
versionCache.value = latest;
|
||||
versionCache.fetchedAt = Date.now();
|
||||
}
|
||||
return latest;
|
||||
}
|
||||
|
||||
export async function GET() {
|
||||
const latestVersion = await fetchLatestVersion();
|
||||
const latestVersion = await getLatestVersionCached();
|
||||
const currentVersion = pkg.version;
|
||||
const hasUpdate = latestVersion ? compareVersions(latestVersion, currentVersion) > 0 : false;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user