import { getGenKvNamespace } from '@/src/lib/env-exporter' import { getAllRegions, getBoundsForRegion } from '@/src/dbService' import type { scaffoldEssentialConsts } from '@/src/trpc/apis/common-apis/common-trpc-index' export const GEN_KV_KEY = 'essentialConsts' export const genKvKeyForBound = (boundId: number): string => `${GEN_KV_KEY}_${boundId}` export type EssentialConstsPayload = Awaited> // Dynamic import: gen-store must not statically import common-trpc-index, because // that module imports readEssentialConstsFromKv from here (cycle). async function buildEssentialConsts(boundId: number): Promise { const { scaffoldEssentialConsts } = await import( '../trpc/apis/common-apis/common-trpc-index' ) return scaffoldEssentialConsts(boundId) } async function allBoundIds(): Promise { const regions = await getAllRegions() const ids: number[] = [] for (const region of regions) { const bounds = await getBoundsForRegion(region.id) for (const bound of bounds) ids.push(bound.id) } return ids } async function putBound(kv: any, boundId: number): Promise { // The scaffold payload carries `essentialUrls` alongside the version fields, // so the KV mirror serves exactly the URLs the cache writer published. const payload = await buildEssentialConsts(boundId) await kv.put(genKvKeyForBound(boundId), JSON.stringify(payload)) } /** * Rebuilds the flat essentialConsts payload for one bound (or every bound of * every region when boundId is omitted) and mirrors it to KV under * `essentialConsts_{boundId}`. No-op when KV is absent. */ export async function refreshGenKv(boundId?: number): Promise { try { const kv = getGenKvNamespace() if (!kv) return const boundIds = boundId != null ? [boundId] : await allBoundIds() for (const id of boundIds) { try { await putBound(kv, id) } catch (error) { console.error(`refreshGenKv failed for bound ${id}:`, error) } } } catch (error) { console.error('refreshGenKv failed:', error) } } /** Rebuilds the KV mirror for every bound of one region. */ export async function refreshGenKvForRegion(regionId: number): Promise { try { const kv = getGenKvNamespace() if (!kv) return const bounds = await getBoundsForRegion(regionId) for (const bound of bounds) { try { await putBound(kv, bound.id) } catch (error) { console.error(`refreshGenKv failed for bound ${bound.id}:`, error) } } } catch (error) { console.error('refreshGenKvForRegion failed:', error) } } /** * Drops a bound's cached payload. Called when a bound is deleted so the stale blob cannot be * served if that id is ever reused by a new bound. */ export async function deleteGenKvForBound(boundId: number): Promise { try { const kv = getGenKvNamespace() if (!kv) return await kv.delete(genKvKeyForBound(boundId)) } catch (error) { console.error(`deleteGenKvForBound failed for bound ${boundId}:`, error) } } /** * Client read path: serve the precomputed payload for a bound from KV. On a * miss, build it from D1/env, backfill KV, and return. */ export async function readEssentialConstsFromKv(boundId: number): Promise { const key = genKvKeyForBound(boundId) try { const kv = getGenKvNamespace() if (kv) { const cached = (await kv.get(key, 'json')) as EssentialConstsPayload | null if (cached) return cached } } catch (error) { console.error('readEssentialConstsFromKv failed:', error) } console.log('building consts') const payload = await buildEssentialConsts(boundId) try { const kv = getGenKvNamespace() if (kv) await kv.put(key, JSON.stringify(payload)) } catch (error) { console.error('backfill essentialConsts failed:', error) } return payload }