From 2686f5b85156507cdb35806c7c1485d0abd2c7f6 Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Thu, 28 Oct 2021 12:03:20 +0100 Subject: [PATCH 1/2] fix(index): replace all `*` directives with `gzip` --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 7249882..8dec5d9 100644 --- a/index.js +++ b/index.js @@ -429,11 +429,11 @@ function findIndexFile (pathname, root, indexFiles = ['index.html']) { }) } -// Adapted from https://github.com/fastify/fastify-compress/blob/fa5c12a5394285c86d9f438cb39ff44f3d5cde79/index.js#L442 +// Adapted from https://github.com/fastify/fastify-compress/blob/665e132fa63d3bf05ad37df3c20346660b71a857/index.js#L451 function getEncodingHeader (headers, checked) { if (!('accept-encoding' in headers)) return - const header = headers['accept-encoding'].toLowerCase().replace('*', 'gzip') + const header = headers['accept-encoding'].toLowerCase().replace(/\*/g, 'gzip') return encodingNegotiator.negotiate( header, supportedEncodings.filter((enc) => !checked.has(enc)) From a616f2db0cfca92a8aa15eb6bf7417784c8f8509 Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Thu, 28 Oct 2021 12:04:46 +0100 Subject: [PATCH 2/2] test(static): check response for `*` directives --- test/static.test.js | 129 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 127 insertions(+), 2 deletions(-) diff --git a/test/static.test.js b/test/static.test.js index 67335ed..078fc1f 100644 --- a/test/static.test.js +++ b/test/static.test.js @@ -35,6 +35,9 @@ const innerIndex = fs const allThreeBr = fs.readFileSync( './test/static-pre-compressed/all-three.html.br' ) +const allThreeGzip = fs.readFileSync( + './test/static-pre-compressed/all-three.html.gz' +) const gzipOnly = fs.readFileSync( './test/static-pre-compressed/gzip-only.html.gz' ) @@ -3015,6 +3018,66 @@ t.test( } ) +t.test( + 'will serve pre-compressed files with .gzip if * directive used', + async (t) => { + const pluginOptions = { + root: path.join(__dirname, '/static-pre-compressed'), + prefix: '/static-pre-compressed/', + preCompressed: true + } + + const fastify = Fastify() + + fastify.register(fastifyStatic, pluginOptions) + t.teardown(fastify.close.bind(fastify)) + + const response = await fastify.inject({ + method: 'GET', + url: '/static-pre-compressed/all-three.html', + headers: { + 'accept-encoding': '*' + } + }) + + genericResponseChecks(t, response) + t.equal(response.headers['content-encoding'], 'gzip') + t.equal(response.statusCode, 200) + t.same(response.rawPayload, allThreeGzip) + t.end() + } +) + +t.test( + 'will serve pre-compressed files with .gzip if multiple * directives used', + async (t) => { + const pluginOptions = { + root: path.join(__dirname, '/static-pre-compressed'), + prefix: '/static-pre-compressed/', + preCompressed: true + } + + const fastify = Fastify() + + fastify.register(fastifyStatic, pluginOptions) + t.teardown(fastify.close.bind(fastify)) + + const response = await fastify.inject({ + method: 'GET', + url: '/static-pre-compressed/all-three.html', + headers: { + 'accept-encoding': '*, *' + } + }) + + genericResponseChecks(t, response) + t.equal(response.headers['content-encoding'], 'gzip') + t.equal(response.statusCode, 200) + t.same(response.rawPayload, allThreeGzip) + t.end() + } +) + t.test( 'will serve uncompressed files if there are no compressed variants on disk', async (t) => { @@ -3077,7 +3140,7 @@ t.test( ) t.test( - 'will serve pre-compressed files and fallback to .gz if .br is not on disk (with wildcard: false) ', + 'will serve pre-compressed files and fallback to .gz if .br is not on disk (with wildcard: false)', async (t) => { const pluginOptions = { root: path.join(__dirname, '/static-pre-compressed'), @@ -3108,7 +3171,69 @@ t.test( ) t.test( - 'will serve uncompressed files if there are no compressed variants on disk (with wildcard: false)', + 'will serve pre-compressed files with .gzip if * directive used (with wildcard: false)', + async (t) => { + const pluginOptions = { + root: path.join(__dirname, '/static-pre-compressed'), + prefix: '/static-pre-compressed/', + preCompressed: true, + wildcard: false + } + + const fastify = Fastify() + + fastify.register(fastifyStatic, pluginOptions) + t.teardown(fastify.close.bind(fastify)) + + const response = await fastify.inject({ + method: 'GET', + url: '/static-pre-compressed/all-three.html', + headers: { + 'accept-encoding': '*' + } + }) + + genericResponseChecks(t, response) + t.equal(response.headers['content-encoding'], 'gzip') + t.equal(response.statusCode, 200) + t.same(response.rawPayload, allThreeGzip) + t.end() + } +) + +t.test( + 'will serve pre-compressed files with .gzip if multiple * directives used (with wildcard: false)', + async (t) => { + const pluginOptions = { + root: path.join(__dirname, '/static-pre-compressed'), + prefix: '/static-pre-compressed/', + preCompressed: true, + wildcard: false + } + + const fastify = Fastify() + + fastify.register(fastifyStatic, pluginOptions) + t.teardown(fastify.close.bind(fastify)) + + const response = await fastify.inject({ + method: 'GET', + url: '/static-pre-compressed/all-three.html', + headers: { + 'accept-encoding': '*, *' + } + }) + + genericResponseChecks(t, response) + t.equal(response.headers['content-encoding'], 'gzip') + t.equal(response.statusCode, 200) + t.same(response.rawPayload, allThreeGzip) + t.end() + } +) + +t.test( + 'will serve uncompressed files if there are no compressed variants on disk (with wildcard: false)', async (t) => { const pluginOptions = { root: path.join(__dirname, '/static-pre-compressed'),