From 0394bf8cd50307c3afe53563b27ef285d5c51f2b Mon Sep 17 00:00:00 2001 From: Davide Roffo Date: Fri, 10 Feb 2023 16:25:12 +0100 Subject: [PATCH] Fix/348 multi root and pre compression infinite loop (#360) * fix: keep track of rootPathOffset to avoid infinite loop * fix: add tests --- index.js | 2 +- test/static.test.js | 57 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 17f38ba..73f1400 100644 --- a/index.js +++ b/index.js @@ -240,7 +240,7 @@ async function fastifyStatic (fastify, opts) { reply, pathname, rootPath, - undefined, + rootPathOffset, undefined, checkedEncodings ) diff --git a/test/static.test.js b/test/static.test.js index 2609039..79bdcc7 100644 --- a/test/static.test.js +++ b/test/static.test.js @@ -3646,3 +3646,60 @@ t.test('should serve files into hidden dir without wildcard option', (t) => { }) }) }) + +t.test( + 'will serve pre-compressed files with .gzip if multi-root', + async (t) => { + const pluginOptions = { + root: [path.join(__dirname, '/static-pre-compressed'), path.join(__dirname, '/static')], + preCompressed: true + } + + const fastify = Fastify() + + fastify.register(fastifyStatic, pluginOptions) + t.teardown(fastify.close.bind(fastify)) + + const response = await fastify.inject({ + method: 'GET', + url: '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 still serve un-compressed files with multi-root and preCompressed as true', + async (t) => { + const pluginOptions = { + root: [path.join(__dirname, '/static-pre-compressed'), path.join(__dirname, '/static')], + preCompressed: true + } + + const fastify = Fastify() + + fastify.register(fastifyStatic, pluginOptions) + t.teardown(fastify.close.bind(fastify)) + + const response = await fastify.inject({ + method: 'GET', + url: 'foobar.html', + headers: { + 'accept-encoding': '*, *' + } + }) + + genericResponseChecks(t, response) + t.equal(response.statusCode, 200) + t.same(response.body, foobarContent) + t.end() + } +)