import type { Gateway } from './context.ts'; import { handleGit } from './git.ts'; import { NSID as NPM_CLOSURE } from '@nibling/npm-gateway/protocol'; import { handleNpm } from './npm.ts'; import { handleXrpc } from './xrpc.ts'; export type { Gateway } from './context.ts'; export { memoryCache, type ResponseCache } from './cache.ts'; /** Request headers a cross-origin caller may send: XRPC reads, git's, and the cache bypass. */ const PREFLIGHT = { 'access-control-allow-origin': '*', 'access-control-allow-methods': 'GET, POST, OPTIONS', 'access-control-allow-headers': 'content-type, git-protocol, x-nib-fresh', 'access-control-max-age': '86400', }; export function preflight(): Response { return new Response(null, { status: 204, headers: PREFLIGHT }); } /** * The read gateway (D22): repo reads, identity, backlinks, git fetches, and npm closures, each answered from a * per-data-center cache or from upstream. Undefined when the path is not one of its routes. * `x-nib-fresh: 1` skips the cache read, so a writer sees their own write. */ export async function handleGateway(request: Request, g: Gateway): Promise { const url = new URL(request.url); const bypass = request.headers.get('x-nib-fresh') === '1'; if (url.pathname.startsWith('/git/')) return handleGit(g, request, bypass); if (request.method !== 'GET') return undefined; // This worker's did:web names it as an npm gateway (D23), so a client build names it as any other. if (url.pathname === `/xrpc/${NPM_CLOSURE}` || url.pathname === '/.well-known/did.json') return handleNpm(g, request); if (!url.pathname.startsWith('/xrpc/')) return undefined; return handleXrpc(g, url.pathname.slice('/xrpc/'.length), url.searchParams, bypass); }