about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorAlan Pearce2023-09-16 07:35:08 +0200
committerAlan Pearce2023-09-16 07:35:08 +0200
commit03cbe07a15514f3ad300c310741a6b7769101455 (patch)
tree0a9f03aaebfb983458206234701cfc3cc3a5fe34 /src
parentb906f4f6d2b90cc3a25c6b6d12956971f144ab52 (diff)
downloadwebsite-03cbe07a15514f3ad300c310741a6b7769101455.tar.lz
website-03cbe07a15514f3ad300c310741a6b7769101455.tar.zst
website-03cbe07a15514f3ad300c310741a6b7769101455.zip
Simplify file handling
Diffstat (limited to 'src')
-rw-r--r--src/index.ts34
1 files changed, 9 insertions, 25 deletions
diff --git a/src/index.ts b/src/index.ts
index f4279c8..57c612a 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -108,31 +108,15 @@ async function serveFile(
   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 {