Simplify file handling
Alan Pearce alan@alanpearce.eu
Sat, 16 Sep 2023 07:35:08 +0200
1 files changed, 9 insertions(+), 25 deletions(-)
jump to
M src/index.ts → src/index.ts
@@ -108,31 +108,15 @@ file: File | undefined, statusCode: number = 200, extraHeaders: Record<string, string> = {}, ): Promise<Response> { - if (file && (await file.handle.exists())) { - metrics.requestsByStatus.inc({ status_code: statusCode }); - return new Response(file.handle, { - headers: { - "last-modified": file.mtime.toUTCString(), - ...extraHeaders, - ...(file.headers || defaultHeaders), - }, - status: statusCode, - }); - } else { - metrics.requestsByStatus.inc({ status_code: 404 }); - // TODO return encoded - return serveFile(files.get("/404.html"), 404); - } -} - -async function serveEncodedFile( - file: File | undefined, - statusCode: number = 200, - extraHeaders: Record<string, string> = {}, -): Promise<Response> { - const res = await serveFile(file, statusCode, extraHeaders); - res.headers.delete("content-disposition"); - return res; + metrics.requestsByStatus.inc({ status_code: statusCode }); + return new Response(await file.handle.arrayBuffer(), { + headers: { + "last-modified": file.mtime.toUTCString(), + ...extraHeaders, + ...(file.headers || defaultHeaders), + }, + status: statusCode, + }); } function parseIfModifiedSinceHeader(header: string | null): number {