Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(scripts): expire the Weblate language-stats cache
Elian Doran72cdf3b59764
diff --git a/scripts/translation/utils.spec.ts b/scripts/translation/utils.spec.tsnew file mode 100644index 0000000000..aa702b84a2--- /dev/null+++ b/scripts/translation/utils.spec.ts@@ -0,0 +1,23 @@+import { describe, expect, it } from "vitest";++import { CACHE_MAX_AGE_MS, isCacheFresh } from "./utils";++describe("isCacheFresh", () => {+ const now = new Date("2026-08-20T12:00:00Z").getTime();++ it("accepts a cache younger than the maximum age", () => {+ expect(isCacheFresh(now, now)).toBe(true);+ expect(isCacheFresh(now - 1000, now)).toBe(true);+ expect(isCacheFresh(now - CACHE_MAX_AGE_MS + 1, now)).toBe(true);+ });++ it("rejects a cache at or past the maximum age", () => {+ expect(isCacheFresh(now - CACHE_MAX_AGE_MS, now)).toBe(false);+ expect(isCacheFresh(now - 7 * CACHE_MAX_AGE_MS, now)).toBe(false);+ });++ it("rejects a cache stamped in the future", () => {+ expect(isCacheFresh(now + 1, now)).toBe(false);+ expect(isCacheFresh(now + CACHE_MAX_AGE_MS, now)).toBe(false);+ });+});diff --git a/scripts/translation/utils.ts b/scripts/translation/utils.tsindex 6683edc080..2b2fab50a3 100644--- a/scripts/translation/utils.ts+++ b/scripts/translation/utils.ts@@ -3,15 +3,16 @@ import { join } from "path";const scriptDir = __dirname;+/** How long a `.language-stats-*.json` cache file is reused before Weblate is queried again. */+export const CACHE_MAX_AGE_MS = 24 * 60 * 60 * 1000;+export async function getLanguageStats(project: "readme" | "client") {const cacheFile = join(scriptDir, `.language-stats-${project}.json`);// Try to read from the cache.try {const cacheStats = await stat(cacheFile);- const now = new Date();- const oneDay = 24 * 60 * 60 * 1000; // milliseconds- if (cacheStats.mtimeMs < now.getTime() + oneDay) {+ if (isCacheFresh(cacheStats.mtimeMs, Date.now())) {console.log("Reading language stats from cache.");return JSON.parse(await readFile(cacheFile, "utf-8"));}@@ -31,3 +32,14 @@ export async function getLanguageStats(project: "readme" | "client") {return stats;}++/**+ * Determines whether a cache file written at `mtimeMs` can still be used at `nowMs`.+ *+ * A file stamped in the future is treated as stale, so a bad clock or a copied timestamp+ * cannot pin the cache to a snapshot that never expires.+ */+export function isCacheFresh(mtimeMs: number, nowMs: number) {+ const age = nowMs - mtimeMs;+ return age >= 0 && age < CACHE_MAX_AGE_MS;+}diff --git a/scripts/vitest.config.ts b/scripts/vitest.config.tsindex cf7337dc03..4263abbd6a 100644--- a/scripts/vitest.config.ts+++ b/scripts/vitest.config.ts@@ -6,6 +6,6 @@ export default defineConfig({test: {name: "scripts",environment: "node",- include: [ "*.spec.ts" ]+ include: [ "**/*.spec.ts" ]}});