import type { CacheAdapter } from "@authhero/adapter-interfaces"; import type { HostResolverCache } from "./cache"; export interface CacheAdapterHostCacheOptions { upstream: HostResolverCache; cache: CacheAdapter; freshTtlMs: number; staleTtlMs?: number; staleIfErrorTtlMs?: number; negativeTtlMs?: number; keyPrefix?: string; waitUntil?: (promise: Promise) => void; cacheReadTimeoutMs?: number; cacheWriteTimeoutMs?: number; upstreamTimeoutMs?: number; } /** * Wraps an upstream `HostResolverCache` with a generic `CacheAdapter` layer. * Adds stale-while-revalidate and stale-if-error semantics on top of any * cache implementation — Cloudflare Cache API (via `CloudflareCache` from * `@authhero/cloudflare-adapter`), Redis, in-memory, or anything else * satisfying the `CacheAdapter` interface. * * Typical use on Cloudflare Workers: * * ```ts * import { createCloudflareCache } from "@authhero/cloudflare-adapter"; * import { createCacheAdapterHostCache, createInMemoryHostCache } from "@authhero/proxy"; * * const cache = createCloudflareCache({ cacheName: "authhero-proxy-hosts" }); * const resolver = createCacheAdapterHostCache({ * upstream: createInMemoryHostCache(data, { freshTtlMs: 60_000 }), * cache, * freshTtlMs: 60 * 60_000, // 1 hour fresh * staleTtlMs: 23 * 60 * 60_000, // SWR for 23 more hours (24h total) * staleIfErrorTtlMs: 24 * 60 * 60_000, // serve last-known-good for one more day on upstream errors * waitUntil: (p) => ctx.waitUntil(p), * }); * ``` * * Invalidation is the caller's responsibility — when a host's routes change, * call `cache.delete(buildCacheAdapterKey(keyPrefix, host))` from the control * plane. Otherwise entries expire on their own TTL. */ export declare function createCacheAdapterHostCache(options: CacheAdapterHostCacheOptions): HostResolverCache; export declare function buildCacheAdapterKey(keyPrefix: string, host: string): string;