Skip to content

Commit

Permalink
fix(index): replace all * directives with gzip (#250)
Browse files Browse the repository at this point in the history
* fix(index): replace all `*` directives with `gzip`

* test(static): check response for `*` directives
  • Loading branch information
Fdawgs committed Nov 3, 2021
1 parent eec47e8 commit 2eb1beb
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 4 deletions.
4 changes: 2 additions & 2 deletions index.js
Expand Up @@ -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))
Expand Down
129 changes: 127 additions & 2 deletions test/static.test.js
Expand Up @@ -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'
)
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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'),
Expand Down

0 comments on commit 2eb1beb

Please sign in to comment.