Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ async function fastifyStatic (fastify, opts) {
encodingExt = checkEncodingHeaders(request.headers, checkedExtensions)

if (encodingExt) {
if (pathname.endsWith('/')) {
pathname = findIndexFile(pathname, options.root, options.index)
if (!pathname) {
return reply.callNotFound()
}
}
pathnameForSend = pathname + '.' + encodingExt
}
}
Expand Down Expand Up @@ -379,16 +385,25 @@ const supportedEncodings = ['br', 'gzip', 'deflate']

function getContentType (path) {
const type = send.mime.lookup(path)
if (!type) {
return
}
const charset = send.mime.charsets.lookup(type)
if (!charset) {
return type
}
return `${type}; charset=${charset}`
}

function findIndexFile (pathname, root, indexFiles = ['index.html']) {
return indexFiles.find(filename => {
const p = path.join(root, pathname, filename)
try {
const stats = statSync(p)
return !stats.isDirectory()
} catch (e) {
return false
}
})
}

// Adapted from https://github.com/fastify/fastify-compress/blob/fa5c12a5394285c86d9f438cb39ff44f3d5cde79/index.js#L442
function checkEncodingHeaders (headers, checked) {
if (!('accept-encoding' in headers)) return
Expand Down
Empty file.
5 changes: 5 additions & 0 deletions test/static-pre-compressed/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
index
</body>
</html>
Binary file added test/static-pre-compressed/index.html.br
Binary file not shown.
Binary file added test/static-pre-compressed/sample.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/static-pre-compressed/sample.jpg.br
Binary file not shown.
121 changes: 121 additions & 0 deletions test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ const allThreeBr = fs.readFileSync(
const gzipOnly = fs.readFileSync(
'./test/static-pre-compressed/gzip-only.html.gz'
)
const indexBr = fs.readFileSync(
'./test/static-pre-compressed/index.html.br'
)
const uncompressedStatic = fs
.readFileSync('./test/static-pre-compressed/uncompressed.html')
.toString('utf8')
Expand Down Expand Up @@ -3098,3 +3101,121 @@ t.test(
t.end()
}
)

t.test(
'will serve precompressed index',
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/',
headers: {
'accept-encoding': 'gzip, deflate, br'
}
})

genericResponseChecks(t, response)
t.equal(response.headers['content-encoding'], 'br')
t.equal(response.statusCode, 200)
t.same(response.rawPayload, indexBr)
t.end()
}
)

t.test(
'will serve precompressed index with alternative index option',
async (t) => {
const pluginOptions = {
root: path.join(__dirname, '/static-pre-compressed'),
prefix: '/static-pre-compressed/',
preCompressed: true,
index: ['all-three.html']
}

const fastify = Fastify()

fastify.register(fastifyStatic, pluginOptions)
t.teardown(fastify.close.bind(fastify))

const response = await fastify.inject({
method: 'GET',
url: '/static-pre-compressed/',
headers: {
'accept-encoding': 'gzip, deflate, br'
}
})

genericResponseChecks(t, response)
t.equal(response.headers['content-encoding'], 'br')
t.equal(response.statusCode, 200)
t.same(response.rawPayload, allThreeBr)
t.end()
}
)

t.test(
'will serve precompressed file without content-type charset',
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/sample.jpg',
headers: {
'accept-encoding': 'gzip, deflate, br'
}
})

t.equal(response.headers['content-type'], 'image/jpeg')
t.equal(response.headers['content-encoding'], 'br')
t.equal(response.statusCode, 200)
t.end()
}
)

t.test(
'nonexistent index with precompressed option',
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/empty/',
headers: {
'accept-encoding': 'gzip, deflate, br'
}
})

t.equal(response.statusCode, 404)
genericErrorResponseChecks(t, response)
t.end()
}
)