Skip to content

Commit 3ff0f39

Browse files
authored
fix: sendFile ignoring option overrides in some cases (#559)
1 parent 663baa7 commit 3ff0f39

2 files changed

Lines changed: 89 additions & 4 deletions

File tree

index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ async function fastifyStatic (fastify, opts) {
274274
pathname + '/',
275275
rootPath,
276276
undefined,
277-
undefined,
277+
pumpOptions,
278278
checkedEncodings
279279
)
280280
}
@@ -314,7 +314,7 @@ async function fastifyStatic (fastify, opts) {
314314
pathname + '/',
315315
rootPath,
316316
undefined,
317-
undefined,
317+
pumpOptions,
318318
checkedEncodings
319319
)
320320
}
@@ -336,7 +336,15 @@ async function fastifyStatic (fastify, opts) {
336336

337337
// root paths left to try?
338338
if (Array.isArray(rootPath) && rootPathOffset < (rootPath.length - 1)) {
339-
return pumpSendToReply(request, reply, pathname, rootPath, rootPathOffset + 1)
339+
return pumpSendToReply(
340+
request,
341+
reply,
342+
pathname,
343+
rootPath,
344+
rootPathOffset + 1,
345+
pumpOptions,
346+
undefined
347+
)
340348
}
341349

342350
if (opts.preCompressed && !checkedEncodings.has(encoding)) {
@@ -347,7 +355,7 @@ async function fastifyStatic (fastify, opts) {
347355
pathnameOrig,
348356
rootPath,
349357
rootPathOffset,
350-
undefined,
358+
pumpOptions,
351359
checkedEncodings
352360
)
353361
}

test/static.test.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,83 @@ test('sendFile', async (t) => {
870870
})
871871
})
872872

873+
test('sendFile with multiple roots', async (t) => {
874+
t.plan(4)
875+
876+
const pluginOptions = {
877+
root: [path.join(__dirname, '/static'), path.join(__dirname, '/static2')],
878+
prefix: '/static'
879+
}
880+
const fastify = Fastify()
881+
const maxAge = Math.round(Math.random() * 10) * 10000
882+
fastify.register(fastifyStatic, pluginOptions)
883+
884+
t.after(() => fastify.close())
885+
886+
fastify.get('/foo', function (_req, rep) {
887+
rep.sendFile('foo.html')
888+
})
889+
890+
fastify.get('/foo-with-options', function (_req, rep) {
891+
rep.sendFile('foo.html', { maxAge })
892+
})
893+
894+
fastify.get('/bar', function (_req, rep) {
895+
rep.sendFile('bar.html')
896+
})
897+
898+
fastify.get('/bar-with-options', function (_req, rep) {
899+
rep.sendFile('bar.html', { maxAge })
900+
})
901+
902+
await fastify.listen({ port: 0 })
903+
fastify.server.unref()
904+
905+
await t.test('reply.sendFile() first root', async (t) => {
906+
t.plan(4 + GENERIC_RESPONSE_CHECK_COUNT)
907+
908+
const response = await fetch('http://localhost:' + fastify.server.address().port + '/foo')
909+
t.assert.ok(response.ok)
910+
t.assert.deepStrictEqual(response.status, 200)
911+
t.assert.deepStrictEqual(await response.text(), fooContent)
912+
t.assert.deepStrictEqual(response.headers.get('cache-control'), 'public, max-age=0')
913+
genericResponseChecks(t, response)
914+
})
915+
916+
await t.test('reply.sendFile() first root with options', async (t) => {
917+
t.plan(4 + GENERIC_RESPONSE_CHECK_COUNT)
918+
919+
const response = await fetch('http://localhost:' + fastify.server.address().port + '/foo-with-options')
920+
t.assert.ok(response.ok)
921+
t.assert.deepStrictEqual(response.status, 200)
922+
t.assert.deepStrictEqual(await response.text(), fooContent)
923+
t.assert.deepStrictEqual(response.headers.get('cache-control'), `public, max-age=${maxAge / 1000}`)
924+
genericResponseChecks(t, response)
925+
})
926+
927+
await t.test('reply.sendFile() second root', async (t) => {
928+
t.plan(4 + GENERIC_RESPONSE_CHECK_COUNT)
929+
930+
const response = await fetch('http://localhost:' + fastify.server.address().port + '/bar')
931+
t.assert.ok(response.ok)
932+
t.assert.deepStrictEqual(response.status, 200)
933+
t.assert.deepStrictEqual(await response.text(), barContent)
934+
t.assert.deepStrictEqual(response.headers.get('cache-control'), 'public, max-age=0')
935+
genericResponseChecks(t, response)
936+
})
937+
938+
await t.test('reply.sendFile() second root with options', async (t) => {
939+
t.plan(4 + GENERIC_RESPONSE_CHECK_COUNT)
940+
941+
const response = await fetch('http://localhost:' + fastify.server.address().port + '/bar-with-options')
942+
t.assert.ok(response.ok)
943+
t.assert.deepStrictEqual(response.status, 200)
944+
t.assert.deepStrictEqual(await response.text(), barContent)
945+
t.assert.deepStrictEqual(response.headers.get('cache-control'), `public, max-age=${maxAge / 1000}`)
946+
genericResponseChecks(t, response)
947+
})
948+
})
949+
873950
test('sendFile disabled', async (t) => {
874951
t.plan(1)
875952

0 commit comments

Comments
 (0)