From b277ab6625aaf8d5aea6f22d9fa5327f6253f3d0 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Fri, 10 Nov 2023 12:12:28 +0100 Subject: De-duplicate content-encoding handling --- src/app.ts | 51 ++++++++++++++------------------------------------- 1 file changed, 14 insertions(+), 37 deletions(-) (limited to 'src/app.ts') 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 { -- cgit 1.4.1