|
| 1 | +import { consola } from 'consola'; |
| 2 | + |
| 3 | +const logger = consola.withTag('version-cache'); |
| 4 | + |
| 5 | +export interface FetchResult { |
| 6 | + version: string |
| 7 | + time?: Date |
| 8 | +} |
| 9 | + |
| 10 | +interface StoredVersion { |
| 11 | + version: string | undefined |
| 12 | + firstSeen: string // ISO 8601 — Date doesn't survive JSON round-trip |
| 13 | +} |
| 14 | + |
| 15 | +interface InflightResult { |
| 16 | + result: FetchResult | undefined |
| 17 | + firstSeen: Date |
| 18 | + ttl: number |
| 19 | +} |
| 20 | + |
| 21 | +const TTL_OK = 3600; // 1 hour |
| 22 | +const TTL_ERROR = 60; // 1 minute |
| 23 | + |
| 24 | +// In-flight deduplication — only holds promises for active upstream |
| 25 | +// fetches, so naturally bounded by concurrency, not by key space. |
| 26 | +// The originating caller writes to KV; joiners just read the result. |
| 27 | +const inflight = new Map<string, Promise<InflightResult>>(); |
| 28 | + |
| 29 | +/** Remove a cached version entry, forcing a fresh upstream poll. */ |
| 30 | +export async function deleteCachedVersion(key: string): Promise<void> { |
| 31 | + try { |
| 32 | + await useStorage('versions').removeItem(key); |
| 33 | + } catch (error) { |
| 34 | + logger.warn(`failed to delete ${key}:`, error); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Returns the last known version, polling upstream only when |
| 40 | + * the KV entry has expired. Uses the upstream publish time |
| 41 | + * when available, otherwise tracks first-seen locally. |
| 42 | + * |
| 43 | + * Degrades gracefully — KV failures fall through to an |
| 44 | + * upstream fetch rather than crashing the handler. |
| 45 | + */ |
| 46 | +export async function fetchVersion( |
| 47 | + key: string, |
| 48 | + fetcher: () => Promise<FetchResult | undefined>, |
| 49 | +): Promise<{ version: string | undefined; firstSeen: Date }> { |
| 50 | + const storage = useStorage('versions'); |
| 51 | + |
| 52 | + // KV cache hit — return stored data directly |
| 53 | + try { |
| 54 | + const stored = await storage.getItem<StoredVersion>(key); |
| 55 | + if (stored) { |
| 56 | + return { |
| 57 | + version: stored.version, |
| 58 | + firstSeen: new Date(stored.firstSeen), |
| 59 | + }; |
| 60 | + } |
| 61 | + } catch (error) { |
| 62 | + logger.warn(`KV read failed for ${key}:`, error); |
| 63 | + } |
| 64 | + |
| 65 | + // KV miss or expired — poll upstream with in-flight dedup. |
| 66 | + // The first caller fetches + writes KV; concurrent joiners |
| 67 | + // just await the same result without duplicate writes. |
| 68 | + let pending = inflight.get(key); |
| 69 | + if (!pending) { |
| 70 | + pending = (async () => { |
| 71 | + let result: FetchResult | undefined; |
| 72 | + try { |
| 73 | + result = await fetcher(); |
| 74 | + } catch (error) { |
| 75 | + logger.warn(`upstream fetch failed for ${key}:`, error); |
| 76 | + } |
| 77 | + const version = result?.version; |
| 78 | + const firstSeen = result?.time ?? new Date(); |
| 79 | + const ttl = version === undefined ? TTL_ERROR : TTL_OK; |
| 80 | + |
| 81 | + try { |
| 82 | + await storage.setItem(key, { |
| 83 | + version, |
| 84 | + firstSeen: firstSeen.toISOString(), |
| 85 | + } satisfies StoredVersion, { ttl }); |
| 86 | + } catch (error) { |
| 87 | + logger.warn(`KV write failed for ${key}:`, error); |
| 88 | + } |
| 89 | + |
| 90 | + return { result, firstSeen, ttl }; |
| 91 | + })(); |
| 92 | + inflight.set(key, pending); |
| 93 | + pending.finally(() => inflight.delete(key)); |
| 94 | + } |
| 95 | + |
| 96 | + const { result, firstSeen } = await pending; |
| 97 | + return { version: result?.version, firstSeen }; |
| 98 | +} |
0 commit comments