From 81ec0616c8670c877b89bbc9b1d90e6428302c48 Mon Sep 17 00:00:00 2001 From: De-Panther Date: Tue, 7 Feb 2023 01:22:20 +0200 Subject: [PATCH 1/2] Serve compressed files if already exist --- lib/core/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/core/index.js b/lib/core/index.js index 920e55b9..560b6b5f 100644 --- a/lib/core/index.js +++ b/lib/core/index.js @@ -201,8 +201,8 @@ module.exports = function createMiddleware(_dir, _options) { ) ); // determine compressed forms if they were to exist - gzippedFile = `${file}.gz`; - brotliFile = `${file}.br`; + gzippedFile = file.endsWith('.gz') ? file : `${file}.gz`; + brotliFile = file.endsWith('.br') ? file : `${file}.br`; Object.keys(headers).forEach((key) => { res.setHeader(key, headers[key]); From 4b0a24d4541060471db2363d80005112716416a3 Mon Sep 17 00:00:00 2001 From: De-Panther Date: Tue, 11 Jul 2023 02:39:41 +0300 Subject: [PATCH 2/2] Fix Content-Type issues based on tests --- lib/core/index.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/lib/core/index.js b/lib/core/index.js index 560b6b5f..8e062c90 100644 --- a/lib/core/index.js +++ b/lib/core/index.js @@ -111,6 +111,7 @@ module.exports = function createMiddleware(_dir, _options) { const headers = opts.headers; const weakEtags = opts.weakEtags; const handleOptionsMethod = opts.handleOptionsMethod; + const defaultType = opts.contentType || 'application/octet-stream'; opts.root = dir; if (defaultExt && /^\./.test(defaultExt)) { @@ -185,6 +186,7 @@ module.exports = function createMiddleware(_dir, _options) { let file = null; let gzippedFile = null; let brotliFile = null; + let contentType = null; try { decodeURIComponent(req.url); // check validity of url @@ -204,6 +206,9 @@ module.exports = function createMiddleware(_dir, _options) { gzippedFile = file.endsWith('.gz') ? file : `${file}.gz`; brotliFile = file.endsWith('.br') ? file : `${file}.br`; + // Do MIME lookup, fall back to octet-stream + contentType = mime.lookup(file, defaultType); + Object.keys(headers).forEach((key) => { res.setHeader(key, headers[key]); }); @@ -227,10 +232,6 @@ module.exports = function createMiddleware(_dir, _options) { function serve(stat) { - // Do a MIME lookup, fall back to octet-stream and handle gzip - // and brotli special case. - const defaultType = opts.contentType || 'application/octet-stream'; - let contentType = mime.lookup(file, defaultType); const range = (req.headers && req.headers.range); const lastModified = (new Date(stat.mtime)).toUTCString(); const etag = generateEtag(stat, weakEtags); @@ -252,12 +253,8 @@ module.exports = function createMiddleware(_dir, _options) { if (file === gzippedFile) { // is .gz picked up res.setHeader('Content-Encoding', 'gzip'); - // strip gz ending and lookup mime type - contentType = mime.lookup(path.basename(file, '.gz'), defaultType); } else if (file === brotliFile) { // is .br picked up res.setHeader('Content-Encoding', 'br'); - // strip br ending and lookup mime type - contentType = mime.lookup(path.basename(file, '.br'), defaultType); } if (typeof cacheControl === 'function') {