Something went wrong. Try again.
MIRROR: javascript for 🐜's, a tiny runtime with big ambitions ant.themackabu.dev
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849import { cacheTtl, GITHUB_REPOSITORY } from './config';import type { Env } from './types';
export async function cachedJson( request: Request, ctx: WaitUntilContext, env: Env, key: string, producer: () => Promise<unknown>,): Promise<Response> { const cache = defaultCache(); const cacheKey = new Request(`https://ant-api-cache.local/${GITHUB_REPOSITORY}/${encodeURIComponent(key)}`, { method: 'GET', }); const cached = await cache.match(cacheKey); if (cached) return addCors(cached);
const body = await producer(); const response = Response.json(body, { headers: { 'Cache-Control': `public, max-age=${cacheTtl(env)}`, Vary: 'Accept', 'Access-Control-Allow-Origin': '*', 'X-Content-Type-Options': 'nosniff', }, });
if (request.method === 'GET') ctx.waitUntil(cache.put(cacheKey, response.clone())); return response;}
type WaitUntilContext = { waitUntil(promise: Promise<unknown>): void;};
function defaultCache(): Cache { return (caches as CacheStorage & { default: Cache }).default;}
function addCors(response: Response): Response { const headers = new Headers(response.headers); headers.set('Access-Control-Allow-Origin', '*'); return new Response(response.body, { status: response.status, statusText: response.statusText, headers, });}