Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/flo-bit/atmo-watch. atmo.watch
Something went wrong. Try again.
985 B · 27 lines
TypeScript
at main
12345678910111213141516171819202122232425262728import { vi } from 'vitest';
/** Small Cache API stand-in with expiring entries and independently readable bodies. */export function installTestCache() { const entries = new Map< string, { body: ArrayBuffer; headers: [string, string][]; status: number; expiresAt: number } >(); const cache = { match: vi.fn(async (request: Request) => { const entry = entries.get(request.url); if (!entry || entry.expiresAt <= Date.now()) return undefined; return new Response(entry.body, { headers: entry.headers, status: entry.status }); }), put: vi.fn(async (request: Request, response: Response) => { const maxAge = Number(response.headers.get('cache-control')?.match(/max-age=(\d+)/)?.[1]); entries.set(request.url, { body: await response.arrayBuffer(), headers: [...response.headers.entries()], status: response.status, expiresAt: Date.now() + maxAge * 1000 }); }) }; vi.stubGlobal('caches', { default: cache }); return { cache, entries };}