Something went wrong. Try again.
Local-first knowledge base made for Typst and the Atmosphere. typbase.at
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980import { HTTPError } from "nitro";import { createMemoryStorage, defineCachedHandler } from "ocache";
import type { Endpoint, UpstreamRequest } from "./endpoints";
import { InvalidRequestError } from "./endpoints";
/** Upstream responses larger than this are passed through without being cached. */const MAX_BODY_SIZE = 8 * 1024 * 1024;
/** Seconds to wait for an upstream response before giving up. */const UPSTREAM_TIMEOUT = 20;
const storage = createMemoryStorage({ maxBytes: 64 * 1024 * 1024, maxSize: 1000 });
export interface ProxyEvent { req: Request; url: URL; params: Record<string, string>;}
export type CachedProxy = (event: ProxyEvent) => Promise<Response>;
async function forward(event: ProxyEvent, endpoint: Endpoint): Promise<Response> { let request: UpstreamRequest; try { request = endpoint.resolve({ params: event.params, query: event.url.searchParams }); } catch (error) { if (error instanceof InvalidRequestError) { throw new HTTPError({ status: 400, message: error.message }); } throw error; }
const { url, headers } = request;
const response = await fetch(url, { headers: { accept: "*/*", ...headers }, signal: event.req.signal, });
if (!response.ok) { throw new HTTPError({ status: response.status === 400 || response.status === 404 ? response.status : 502, message: `Upstream responded with \`${response.status}\`.`, }); }
// Only the content type is passed on, so nothing else the upstream sends is ours to replay. return new Response(await response.arrayBuffer(), { headers: { "content-type": response.headers.get("content-type") ?? "application/octet-stream" }, });}
/** Marks the response as cacheable by the shared cache in front of us. */export function finalizeResponse(response: Response): Response { const cacheControl = response.headers.get("cache-control"); if (!cacheControl || cacheControl.includes("public")) { return response; }
const headers = new Headers(response.headers); headers.set("cache-control", `public, ${cacheControl}`);
return new Response(response.body, { status: response.status, headers });}
export function createCachedProxy(endpoint: Endpoint): CachedProxy { return defineCachedHandler<ProxyEvent>((event) => forward(event, endpoint), { name: endpoint.name, maxAge: endpoint.maxAge, staleMaxAge: endpoint.staleMaxAge, swr: true, allowQuery: endpoint.allowQuery ?? [], maxBodySize: MAX_BODY_SIZE, maxResolveTime: UPSTREAM_TIMEOUT, storage, }) as CachedProxy;}