import { LRUCache } from 'lru-cache'; import { config } from './config.ts'; import { withPage } from './browser/pool.ts'; import { withTimeout } from './browser/queue.ts'; import { ResolveError, type Post } from './types.ts'; import type { PlatformSpec } from './platforms/types.ts'; /** * Resolved posts, keyed by canonical URL. Reloading, hitting back, or * opening the same link twice must not drive Chromium again. */ const cache = new LRUCache({ max: config.cacheMax, ttl: config.cacheTtlMs, }); export function cacheSize(): number { return cache.size; } export async function resolvePost( spec: PlatformSpec, originalUrl: string, pathRest: string, ): Promise { const hit = cache.get(originalUrl); if (hit) return hit; const post = await withTimeout( withPage(({ page, detach }) => spec.resolve({ page, detach, spec, originalUrl, pathRest })), config.resolveTimeoutMs, `${spec.label} resolve`, ).catch((error: unknown) => { if (error instanceof ResolveError) throw error; throw new ResolveError( error instanceof Error ? error.message : String(error), spec.id, originalUrl, { cause: error }, ); }); cache.set(originalUrl, post); return post; }