Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(index): replace all * directives with gzip #250

Merged
merged 2 commits into from Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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