fix: pass some basic metadata with static file PRECHECKs

This commit is contained in:
Andy Burke 2025-08-08 17:28:29 -07:00
parent 7ccb9d7b69
commit 41c7802a36

View file

@ -7,6 +7,7 @@ import * as fs from '@std/fs';
import * as path from '@std/path';
import * as media_types from '@std/media-types';
import { SERVER } from '../server.ts';
import { getCookies } from '@std/http/cookie';
let PUT_PATHS_ALLOWED: string[] | undefined = undefined;
let DELETE_PATHS_ALLOWED: string[] | undefined = undefined;
@ -17,7 +18,7 @@ export type HANDLER_METHOD = (
normalized_path: string,
server: SERVER
) => Promise<Response | undefined> | Response | undefined;
export type PRECHECK = (request: Request) => Promise<Response> | Response | undefined;
export type PRECHECK = (request: Request, meta: Record<string, any>) => Promise<Response> | Response | undefined;
export const PRECHECKS: Partial<Record<HTTP_METHOD, PRECHECK[]>> = {};
export const HANDLERS: Partial<Record<HTTP_METHOD, HANDLER_METHOD>> = {
HEAD: async (_request: Request, normalized_path: string, _server: SERVER): Promise<Response | undefined> => {
@ -295,8 +296,14 @@ export default async function handle_static_files(request: Request, server: SERV
const prechecks: PRECHECK[] = PRECHECKS[method] ?? [];
const cookies: Record<string, string> = getCookies(request.headers);
const query = Object.fromEntries(new URL(request.url).searchParams.entries());
for await (const precheck of prechecks) {
const error_response: Response | undefined = await precheck(request);
const error_response: Response | undefined = await precheck(request, {
cookies,
query
});
if (error_response) {
return error_response;
}