{"version":3,"file":"cache-Bcak7Ews.mjs","names":[],"sources":["../src/redirects/cache.ts"],"sourcesContent":["/**\n * Redirect rule cache.\n *\n * Worker-isolate cache for enabled redirect rules. The middleware populates\n * this on first request; route handlers invalidate it on writes. Cached rules\n * expire so writes handled by another isolate become visible here too.\n *\n * Both exact-match and pattern rules are loaded from one query and cached\n * together: exact rules indexed by source path in a Map, pattern rules\n * pre-compiled into an array. A single warm request issues zero database\n * queries; a cold or expired isolate issues one.\n *\n * This module deliberately has NO Astro imports so it can be safely imported\n * from handlers, seed, CLI, and tests without dragging in `astro:middleware`.\n */\n\nimport { after } from \"../after.js\";\nimport type { Redirect } from \"../database/repositories/redirect.js\";\nimport {\n\tcreateSingleFlightCache,\n\ttype SingleFlightCache,\n\tinvalidateSingleFlightCache,\n\tsingleFlightCached,\n} from \"../utils/single-flight-cache.js\";\nimport type { CompiledPattern } from \"./patterns.js\";\nimport { compilePattern, interpolateDestination, matchPattern } from \"./patterns.js\";\n\nexport interface CachedRedirectRule {\n\tredirect: Redirect;\n\tcompiled: CompiledPattern;\n}\n\nexport interface CachedRedirects {\n\t/** Exact-match rules indexed by source path (`source` -> `Redirect`). */\n\texact: Map<string, Redirect>;\n\t/** Pattern rules with their compiled regexes, preserving insertion order. */\n\tpatterns: CachedRedirectRule[];\n}\n\ninterface RedirectCacheState {\n\tredirects: CachedRedirects | null;\n\texpiresAt: number;\n\tgeneration: number;\n\trefresh: SingleFlightCache<CachedRedirects>;\n}\n\nconst REDIRECT_CACHE_TTL_MS = 30_000;\nconst REDIRECT_CACHE_MAX_REFRESH_ATTEMPTS = 3;\nconst REDIRECT_CACHE_KEY = Symbol.for(\"emdash:redirect-cache\");\nconst g = globalThis as Record<symbol, unknown>;\nconst cacheState: RedirectCacheState =\n\t// eslint-disable-next-line typescript/no-unsafe-type-assertion -- globalThis singleton pattern (see request-context.ts)\n\t(g[REDIRECT_CACHE_KEY] as RedirectCacheState | undefined) ??\n\t(() => {\n\t\tconst state: RedirectCacheState = {\n\t\t\tredirects: null,\n\t\t\texpiresAt: 0,\n\t\t\tgeneration: 0,\n\t\t\trefresh: createSingleFlightCache<CachedRedirects>(),\n\t\t};\n\t\tg[REDIRECT_CACHE_KEY] = state;\n\t\treturn state;\n\t})();\n\n/**\n * Invalidate the cached redirects (both exact and pattern).\n * Call when redirects are created, updated, or deleted.\n */\nexport function invalidateRedirectCache(): void {\n\tcacheState.generation++;\n\tcacheState.redirects = null;\n\tcacheState.expiresAt = 0;\n\tinvalidateSingleFlightCache(cacheState.refresh);\n}\n\n/**\n * Get the cached redirects, or null if the cache is cold.\n */\nfunction getCachedRedirects(): CachedRedirects | null {\n\tif (cacheState.redirects && Date.now() >= cacheState.expiresAt) {\n\t\tinvalidateRedirectCache();\n\t}\n\treturn cacheState.redirects;\n}\n\n/** Compile enabled database rows into the in-memory lookup structures. */\nfunction compileRedirects(redirects: Redirect[]): CachedRedirects {\n\tconst exact = new Map<string, Redirect>();\n\tconst patterns: CachedRedirectRule[] = [];\n\tfor (const r of redirects) {\n\t\tif (r.isPattern) {\n\t\t\tpatterns.push({ redirect: r, compiled: compilePattern(r.source) });\n\t\t} else {\n\t\t\texact.set(r.source, r);\n\t\t}\n\t}\n\treturn { exact, patterns };\n}\n\nfunction installCachedRedirects(redirects: CachedRedirects): CachedRedirects {\n\tcacheState.redirects = redirects;\n\tcacheState.expiresAt = Date.now() + REDIRECT_CACHE_TTL_MS;\n\treturn cacheState.redirects;\n}\n\nexport async function loadCachedRedirects(\n\tload: () => Promise<Redirect[]>,\n): Promise<CachedRedirects> {\n\tfor (let attempt = 0; attempt < REDIRECT_CACHE_MAX_REFRESH_ATTEMPTS; attempt++) {\n\t\tconst cached = getCachedRedirects();\n\t\tif (cached) return cached;\n\n\t\tconst generation = cacheState.generation;\n\t\tconst loaded = await singleFlightCached(\n\t\t\tcacheState.refresh,\n\t\t\tasync () => compileRedirects(await load()),\n\t\t\t{ anchor: (promise) => after(() => promise), ownerTimeoutMs: 30_000 },\n\t\t);\n\n\t\tif (generation === cacheState.generation) {\n\t\t\treturn installCachedRedirects(loaded);\n\t\t}\n\n\t\tif (attempt === REDIRECT_CACHE_MAX_REFRESH_ATTEMPTS - 1) {\n\t\t\treturn loaded;\n\t\t}\n\t}\n\n\tthrow new Error(\"Redirect cache refresh exhausted without loading rules\");\n}\n\n/**\n * Match a path against the cached pattern rules.\n * Returns the resolved destination and matching redirect, or null.\n */\nexport function matchCachedPatterns(\n\trules: CachedRedirectRule[],\n\tpathname: string,\n): { redirect: Redirect; destination: string } | null {\n\tfor (const { redirect, compiled } of rules) {\n\t\tconst params = matchPattern(compiled, pathname);\n\t\tif (params) {\n\t\t\tconst dest = interpolateDestination(redirect.destination, params);\n\t\t\treturn { redirect, destination: dest };\n\t\t}\n\t}\n\treturn null;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AA8CA,MAAM,wBAAwB;AAC9B,MAAM,sCAAsC;AAC5C,MAAM,qBAAqB,OAAO,IAAI,wBAAwB;AAC9D,MAAM,IAAI;AACV,MAAM,aAEJ,EAAE,8BACI;CACN,MAAM,QAA4B;EACjC,WAAW;EACX,WAAW;EACX,YAAY;EACZ,SAAS,yBAA0C;EACnD;AACD,GAAE,sBAAsB;AACxB,QAAO;IACJ;;;;;AAML,SAAgB,0BAAgC;AAC/C,YAAW;AACX,YAAW,YAAY;AACvB,YAAW,YAAY;AACvB,6BAA4B,WAAW,QAAQ;;;;;AAMhD,SAAS,qBAA6C;AACrD,KAAI,WAAW,aAAa,KAAK,KAAK,IAAI,WAAW,UACpD,0BAAyB;AAE1B,QAAO,WAAW;;;AAInB,SAAS,iBAAiB,WAAwC;CACjE,MAAM,wBAAQ,IAAI,KAAuB;CACzC,MAAM,WAAiC,EAAE;AACzC,MAAK,MAAM,KAAK,UACf,KAAI,EAAE,UACL,UAAS,KAAK;EAAE,UAAU;EAAG,UAAU,eAAe,EAAE,OAAO;EAAE,CAAC;KAElE,OAAM,IAAI,EAAE,QAAQ,EAAE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