Skip to content

Commit

Permalink
Fix/348 multi root and pre compression infinite loop (#360)
Browse files Browse the repository at this point in the history
* fix: keep track of rootPathOffset to avoid infinite loop

* fix: add tests
  • Loading branch information
davideroffo committed Feb 10, 2023
1 parent c26d54c commit 0394bf8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ async function fastifyStatic (fastify, opts) {
reply,
pathname,
rootPath,
undefined,
rootPathOffset,
undefined,
checkedEncodings
)
Expand Down
57 changes: 57 additions & 0 deletions test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
)

0 comments on commit 0394bf8

Please sign in to comment.