import { match, pathToRegexp,} from "https://deno.land/x/path_to_regexp@v6.2.0/index.ts";import { Status, STATUS_TEXT,} from "https://deno.land/std@0.100.0/http/http_status.ts";import { inMemoryCache } from "https://deno.land/x/httpcache@0.1.2/in_memory.ts";import { render } from "https://x.lcas.dev/preact@10.5.12/ssr.js";import { contentType as getContentType, lookup,} from "https://raw.githubusercontent.com/usesift/media_types/34656bf398c81f2687fa5010e56844dac4e7a2e9/mod.ts";import type { VNode } from "https://x.lcas.dev/preact@10.5.12/mod.d.ts";
export * from "https://x.lcas.dev/preact@10.5.12/mod.js";
const globalCache = inMemoryCache(20);
let routes: Routes = { 404: defaultNotFoundPage };
export interface PathParams { [key: string]: string | string[];}
export type Handler = ( request: Request, params: PathParams,) => Response | Promise<Response>;
export interface Routes { [path: string]: Handler;}
export function serve(userRoutes: Routes): void { routes = { ...routes, ...userRoutes }; addEventListener("fetch", (event: any) => { event.respondWith(handleRequest(event.request, routes)); });}
function newResponse( response: Response, headers: HeadersInit,): Response { response = new Response(response.body, response); const newHeaders = headers instanceof Headers ? headers : new Headers(headers);
if (response.headers.has("x-github-request-id")) { response.headers.delete("content-security-policy"); }
for (const [key, value] of newHeaders) { response.headers.set(key, value); }
return response;}
async function handleRequest( request: Request, routes: Routes,): Promise<Response> { const { search, pathname } = new URL(request.url);
try { const startTime = Date.now(); let response = await globalCache.match(request); if (typeof response === "undefined") { for (const route of Object.keys(routes)) { if (pathToRegexp(route).test(pathname)) { const getParams = match(route); const { params = {} } = getParams(pathname) as { params: { [key: string]: string }; }; try { response = await routes[route](request, params); } catch (error) { console.error("Error serving request:", error); response = json({ error: error.message }, { status: 500 }); } if (!(response instanceof Response)) { response = jsx(response); } break; } } } else { response = newResponse(response, { "x-function-cache-hit": "true", }); }
if (response === undefined) { response = await routes["404"](request, {}); }
console.log( `${request.method} ${pathname + search} ${ response.headers.has("x-function-cache-hit") ? String.fromCodePoint(0x26a1) : "" }${Date.now() - startTime}ms ${response.status}`, );
return response; } catch (error) { console.error("Error serving request:", error); return json({ error: error.message }, { status: 500 }); }}
function defaultNotFoundPage() { return new Response("<h1 align=center>page not found</h1>", { status: 404, headers: { "Content-Type": "text/html; charset=utf-8" }, });}
export interface ServeStaticOptions { baseUrl: string; intervene?: ( request: Request, response: Response, ) => Promise<Response> | Response; cache?: boolean;}
export function serveStatic( relativePath: string, { baseUrl, intervene, cache = true }: ServeStaticOptions,): Handler { return async (request: Request, params: PathParams): Promise<Response> => { let filePath = relativePath; if (params && params.filename) { if (Array.isArray(params.filename)) { params.filename = params.filename.join("/"); } filePath = relativePath.endsWith("/") ? relativePath + params.filename : relativePath + "/" + params.filename; } const fileUrl = new URL(filePath, baseUrl).toString();
let response: Response | undefined; if (cache) { response = await globalCache.match(request); }
if (typeof response === "undefined") { response = await fetch(new Request(fileUrl, request)); response = new Response(response.body, response);
const contentType = getContentType(String(lookup(filePath))); if (contentType) { response.headers.set("content-type", contentType); } if (typeof intervene === "function") { response = await intervene(request, response); }
if (cache) { const TEN_MB = 1024 * 1024 * 10; if (Number(response.headers.get("content-length")) < TEN_MB) { await globalCache.put(request, response); } } }
if (response.status == 404) { return routes[404](request, {}); } return response; };}
export function json( jsobj: Parameters<typeof JSON.stringify>[0], init?: ResponseInit,): Response { const headers = init?.headers instanceof Headers ? init.headers : new Headers(init?.headers);
if (!headers.has("Content-Type")) { headers.set("Content-Type", "application/json; charset=utf-8"); } return new Response(JSON.stringify(jsobj) + "\n", { statusText: init?.statusText ?? STATUS_TEXT.get(init?.status ?? Status.OK), status: init?.status ?? Status.OK, headers, });}
export function jsx(jsx: VNode, init?: ResponseInit): Response { const headers = init?.headers instanceof Headers ? init.headers : new Headers(init?.headers);
if (!headers.has("Content-Type")) { headers.set("Content-Type", "text/html; charset=utf-8"); }
return new Response(render(jsx), { statusText: init?.statusText ?? STATUS_TEXT.get(init?.status ?? Status.OK), status: init?.status ?? Status.OK, headers, });}
export interface RequestTerms { [key: string]: { headers?: string[]; body?: string[]; params?: string[]; };}
export async function validateRequest( request: Request, terms: RequestTerms,): Promise<{ error?: { message: string; status: number }; body?: { [key: string]: unknown };}> { let body = {};
if (!terms[request.method]) { return { error: { message: `method ${request.method} is not allowed for the URL`, status: Status.MethodNotAllowed, }, }; }
if ( terms[request.method]?.params && terms[request.method].params!.length > 0 ) { const { searchParams } = new URL(request.url); const requestParams = []; for (const param of searchParams.keys()) { requestParams.push(param); }
for (const param of terms[request.method].params!) { if (!requestParams.includes(param)) { return { error: { message: `param '${param}' is required to process the request`, status: Status.BadRequest, }, }; } } }
if ( terms[request.method].headers && terms[request.method].headers!.length > 0 ) { const requestHeaderKeys = []; for (const header of request.headers.keys()) { requestHeaderKeys.push(header); }
for (const header of terms[request.method].headers!) { if (!requestHeaderKeys.includes(header.toLowerCase())) { return { error: { message: `header '${header}' not available`, status: Status.BadRequest, }, }; } } }
if (terms[request.method].body && terms[request.method].body!.length > 0) { const requestBody = await request.json(); const bodyKeys = Object.keys(requestBody); for (const key of terms[request.method].body!) { if (!bodyKeys.includes(key)) { return { error: { message: `field '${key}' is not available in the body`, status: Status.BadRequest, }, }; } }
body = requestBody; }
return { body };}