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 content-encoding response header for gzip compression #206

Merged
merged 1 commit into from May 21, 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
43 changes: 19 additions & 24 deletions index.js
Expand Up @@ -50,7 +50,7 @@ async function fastifyStatic (fastify, opts) {
rootPath,
rootPathOffset = 0,
pumpOptions = {},
checkedExtensions
checkedEncodings
) {
const options = Object.assign({}, sendOptions, pumpOptions)

Expand All @@ -66,28 +66,28 @@ async function fastifyStatic (fastify, opts) {
return reply.callNotFound()
}

let encodingExt
let encoding
let pathnameForSend = pathname

if (opts.preCompressed) {
/**
* We conditionally create this structure to track our attempts
* at sending pre-compressed assets
*/
if (!checkedExtensions) {
checkedExtensions = new Set()
if (!checkedEncodings) {
checkedEncodings = new Set()
}

encodingExt = checkEncodingHeaders(request.headers, checkedExtensions)
encoding = getEncodingHeader(request.headers, checkedEncodings)

if (encodingExt) {
if (encoding) {
if (pathname.endsWith('/')) {
pathname = findIndexFile(pathname, options.root, options.index)
if (!pathname) {
return reply.callNotFound()
}
}
pathnameForSend = pathname + '.' + encodingExt
pathnameForSend = pathname + '.' + getEncodingExtension(encoding)
}
}

Expand Down Expand Up @@ -129,9 +129,9 @@ async function fastifyStatic (fastify, opts) {
wrap.on('finish', reply.send.bind(reply))
} else {
wrap.on('pipe', function () {
if (encodingExt) {
if (encoding) {
reply.header('content-type', getContentType(pathname))
reply.header('content-encoding', encodingExt)
reply.header('content-encoding', encoding)
}
reply.send(wrap)
})
Expand Down Expand Up @@ -172,16 +172,16 @@ async function fastifyStatic (fastify, opts) {
return pumpSendToReply(request, reply, pathname, rootPath, rootPathOffset + 1)
}

if (opts.preCompressed && !checkedExtensions.has(encodingExt)) {
checkedExtensions.add(encodingExt)
if (opts.preCompressed && !checkedEncodings.has(encoding)) {
checkedEncodings.add(encoding)
return pumpSendToReply(
request,
reply,
pathname,
rootPath,
undefined,
undefined,
checkedExtensions
checkedEncodings
)
}

Expand Down Expand Up @@ -405,29 +405,24 @@ function findIndexFile (pathname, root, indexFiles = ['index.html']) {
}

// Adapted from https://github.com/fastify/fastify-compress/blob/fa5c12a5394285c86d9f438cb39ff44f3d5cde79/index.js#L442
function checkEncodingHeaders (headers, checked) {
function getEncodingHeader (headers, checked) {
if (!('accept-encoding' in headers)) return

let ext
const header = headers['accept-encoding'].toLowerCase().replace('*', 'gzip')
const accepted = encodingNegotiator.negotiate(
return encodingNegotiator.negotiate(
header,
supportedEncodings.filter((enc) => !checked.has(enc))
)
}

switch (accepted) {
function getEncodingExtension (encoding) {
switch (encoding) {
case 'br':
ext = 'br'
break
return 'br'

case 'gzip':
if (!checked.has('gz')) {
ext = 'gz'
break
}
return 'gz'
}

return ext
}

module.exports = fp(fastifyStatic, {
Expand Down
4 changes: 2 additions & 2 deletions test/static.test.js
Expand Up @@ -2945,7 +2945,7 @@ t.test(
})

genericResponseChecks(t, response)
t.equal(response.headers['content-encoding'], 'gz')
t.equal(response.headers['content-encoding'], 'gzip')
t.equal(response.statusCode, 200)
t.same(response.rawPayload, gzipOnly)
t.end()
Expand Down Expand Up @@ -3037,7 +3037,7 @@ t.test(
})

genericResponseChecks(t, response)
t.equal(response.headers['content-encoding'], 'gz')
t.equal(response.headers['content-encoding'], 'gzip')
t.equal(response.statusCode, 200)
t.same(response.rawPayload, gzipOnly)
t.end()
Expand Down