116 lines
3.3 KiB
TypeScript
116 lines
3.3 KiB
TypeScript
import { existsSync, mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
|
|
import { homedir } from "node:os";
|
|
import { dirname, join } from "node:path";
|
|
import { spawn } from "node:child_process";
|
|
import type { LauncherConfig } from "./runtime.js";
|
|
|
|
export const UPDATE_CACHE_FILE_ENV = "SEAL_USE_UPDATE_CACHE_FILE";
|
|
export const DEFAULT_UPDATE_CACHE_TTL_MS = 24 * 60 * 60 * 1000; // 24 hours
|
|
|
|
const DEFAULT_UPDATE_CACHE_FILE = join(homedir(), ".config", "seal-use", "update-cache.json");
|
|
|
|
function normalizeTimestampToSeconds(timestamp: string): string {
|
|
return timestamp.replace(/\.\d+Z$/, "Z");
|
|
}
|
|
|
|
export interface UpdateCacheData {
|
|
checkedAt: string;
|
|
latestVersion: string;
|
|
publishedAt?: string;
|
|
releaseUrl?: string;
|
|
}
|
|
|
|
export function resolveUpdateCacheFilePath(): string {
|
|
const custom = process.env[UPDATE_CACHE_FILE_ENV]?.trim();
|
|
if (custom) return custom;
|
|
return DEFAULT_UPDATE_CACHE_FILE;
|
|
}
|
|
|
|
export function loadUpdateCache(filePath = resolveUpdateCacheFilePath()): UpdateCacheData | null {
|
|
if (!existsSync(filePath)) {
|
|
return null;
|
|
}
|
|
try {
|
|
const raw = readFileSync(filePath, "utf-8");
|
|
const parsed = JSON.parse(raw);
|
|
if (
|
|
parsed &&
|
|
typeof parsed === "object" &&
|
|
typeof parsed.latestVersion === "string" &&
|
|
typeof parsed.checkedAt === "string"
|
|
) {
|
|
return {
|
|
...parsed,
|
|
checkedAt: normalizeTimestampToSeconds(parsed.checkedAt)
|
|
} as UpdateCacheData;
|
|
}
|
|
return null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function saveUpdateCache(
|
|
data: Omit<UpdateCacheData, "checkedAt"> & { checkedAt?: string },
|
|
filePath = resolveUpdateCacheFilePath()
|
|
): void {
|
|
try {
|
|
const dir = dirname(filePath);
|
|
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
|
|
const payload: UpdateCacheData = {
|
|
checkedAt: normalizeTimestampToSeconds(data.checkedAt ?? new Date().toISOString()),
|
|
latestVersion: data.latestVersion,
|
|
publishedAt: data.publishedAt,
|
|
releaseUrl: data.releaseUrl
|
|
};
|
|
|
|
const tmpPath = `${filePath}.tmp.${Date.now()}.${Math.random().toString(36).slice(2, 8)}`;
|
|
writeFileSync(tmpPath, JSON.stringify(payload, null, 2), { mode: 0o600 });
|
|
renameSync(tmpPath, filePath);
|
|
} catch {
|
|
// Ignore cache persistence errors to guarantee non-blocking execution
|
|
}
|
|
}
|
|
|
|
export function isUpdateCacheExpired(
|
|
cache: UpdateCacheData | null | undefined,
|
|
ttlMs = DEFAULT_UPDATE_CACHE_TTL_MS
|
|
): boolean {
|
|
if (!cache || !cache.checkedAt) return true;
|
|
const checkedTime = new Date(cache.checkedAt).getTime();
|
|
if (Number.isNaN(checkedTime)) return true;
|
|
return Date.now() - checkedTime > ttlMs;
|
|
}
|
|
|
|
export function triggerBackgroundUpdateCheck(launcher?: LauncherConfig): boolean {
|
|
if (
|
|
process.env.SEAL_DISABLE_BG_UPDATE === "1" ||
|
|
process.env.NODE_ENV === "test" ||
|
|
process.env.CI
|
|
) {
|
|
return false;
|
|
}
|
|
|
|
if (!launcher || !launcher.command) {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
const cmd = launcher.command;
|
|
const args = [...(launcher.args ?? []), "update", "check", "--bg-save"];
|
|
const child = spawn(cmd, args, {
|
|
detached: true,
|
|
stdio: "ignore",
|
|
env: {
|
|
...process.env,
|
|
SEAL_DISABLE_BG_UPDATE: "1" // Prevent nested spawns
|
|
}
|
|
});
|
|
child.unref();
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|