about summary refs log tree commit diff stats
path: root/src/app.ts
diff options
context:
space:
mode:
authorAlan Pearce2023-11-10 12:12:28 +0100
committerAlan Pearce2023-11-10 12:12:28 +0100
commitb277ab6625aaf8d5aea6f22d9fa5327f6253f3d0 (patch)
treeaea1163c026d4bcaff957df90f75f97cbd4f64e9 /src/app.ts
parent2492547ee00b8acc58e5d22f93a5a5433676f981 (diff)
downloadwebsite-b277ab6625aaf8d5aea6f22d9fa5327f6253f3d0.tar.lz
website-b277ab6625aaf8d5aea6f22d9fa5327f6253f3d0.tar.zst
website-b277ab6625aaf8d5aea6f22d9fa5327f6253f3d0.zip
De-duplicate content-encoding handling
Diffstat (limited to 'src/app.ts')
-rw-r--r--src/app.ts51
1 files changed, 14 insertions, 37 deletions
diff --git a/src/app.ts b/src/app.ts
index 6843ccd..b2957aa 100644
--- a/src/app.ts
+++ b/src/app.ts
@@ -160,6 +160,8 @@ export const server = {
         });
       }
       const file = files.get(pathname);
+      let contentEncoding = "identity";
+      let suffix = "";
       if (file && (await file.handle.exists())) {
         if (
           parseIfModifiedSinceHeader(
@@ -178,50 +180,25 @@ export const server = {
           .split(",")
           .map((x) => x.trim().toLowerCase());
         if (encodings.includes("br") && files.has(pathname + ".br")) {
-          transaction.setHttpStatus(200);
-          metrics.requests.inc({
-            method: request.method,
-            path: pathname,
-            status_code: 200,
-            content_encoding: "br",
-          });
-          return serveFile(files.get(pathname + ".br"), 200, {
-            "content-encoding": "br",
-            "content-type": file.type,
-          });
+          contentEncoding = "br";
+          suffix = ".br";
         } else if (encodings.includes("zstd") && files.has(pathname + ".zst")) {
-          transaction.setHttpStatus(200);
-          metrics.requests.inc({
-            method: request.method,
-            path: pathname,
-            status_code: 200,
-            content_encoding: "zst",
-          });
-          return serveFile(files.get(pathname + ".zst"), 200, {
-            "content-encoding": "zstd",
-            "content-type": file.type,
-          });
+          contentEncoding = "zstd";
+          suffix = ".zst";
         } else if (encodings.includes("gzip") && files.has(pathname + ".gz")) {
-          transaction.setHttpStatus(200);
-          metrics.requests.inc({
-            method: request.method,
-            path: pathname,
-            status_code: 200,
-            content_encoding: "gzip",
-          });
-          return serveFile(files.get(pathname + ".gz"), 200, {
-            "content-encoding": "gzip",
-            "content-type": file.type,
-          });
+          contentEncoding = "gzip";
+          suffix = ".gz";
         }
-        transaction.setHttpStatus(200);
+        const status = 200;
+        transaction.setHttpStatus(status);
         metrics.requests.inc({
           method: request.method,
           path: pathname,
-          status_code: 200,
-          content_encoding: "identity",
+          status_code: status,
+          content_encoding: contentEncoding,
         });
-        return serveFile(file, 200, {
+        return serveFile(files.get(pathname + suffix), status, {
+          "content-encoding": contentEncoding,
           "content-type": file.type,
         });
       } else {