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
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
const path = require('node:path')
const { fileURLToPath } = require('node:url')
const { statSync } = require('node:fs')
const { basename } = require('node:path')
const { glob } = require('glob')
const fp = require('fastify-plugin')
const send = require('@fastify/send')
const encodingNegotiator = require('@fastify/accept-negotiator')
const contentDisposition = require('content-disposition')
const { create } = require('content-disposition')

const dirList = require('./lib/dirList')

Expand All @@ -21,6 +22,10 @@ const encodingExtensionMap = {
gzip: '.gz'
}

function contentDisposition (filename) {
return create(basename(filename))
}

/** @type {import("fastify").FastifyPluginAsync<import("./types").FastifyStaticOptions>} */
async function fastifyStatic (fastify, opts) {
if (opts.serve !== false || opts.root !== undefined) {
Expand Down Expand Up @@ -327,7 +332,7 @@ async function fastifyStatic (fastify, opts) {
}

if (metadata.error.code === 'ENOENT') {
// when preCompress is enabled and the path is a directory without a trailing slash
// when preCompress is enabled and the path is a directory without a trailing slash
if (opts.preCompressed && encoding) {
if (opts.redirect !== true) {
const indexPathname = findIndexFile(pathname, options.root, options.index)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"dependencies": {
"@fastify/accept-negotiator": "^2.0.0",
"@fastify/send": "^4.0.0",
"content-disposition": "^1.0.1",
"content-disposition": "^2.0.1",
"fastify-plugin": "^6.0.0",
"fastq": "^1.17.1",
"glob": "^13.0.0"
Expand Down
12 changes: 6 additions & 6 deletions test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,7 @@ test('download', async (t) => {

const response = await fetch('http://localhost:' + fastify.server.address().port + '/foo/bar')
t.assert.ok(response.ok)
t.assert.deepStrictEqual(response.headers.get('content-disposition'), 'attachment; filename="index.html"')
t.assert.deepStrictEqual(response.headers.get('content-disposition'), 'attachment; filename=index.html')
t.assert.deepStrictEqual(response.status, 200)
t.assert.deepStrictEqual(await response.text(), indexContent)
genericResponseChecks(t, response)
Expand All @@ -1129,7 +1129,7 @@ test('download', async (t) => {
const response = await fetch('http://localhost:' + fastify.server.address().port + '/foo/bar/change')
t.assert.ok(response.ok)
t.assert.deepStrictEqual(response.status, 200)
t.assert.deepStrictEqual(response.headers.get('content-disposition'), 'attachment; filename="hello-world.html"')
t.assert.deepStrictEqual(response.headers.get('content-disposition'), 'attachment; filename=hello-world.html')
t.assert.deepStrictEqual(await response.text(), indexContent)
genericResponseChecks(t, response)
})
Expand All @@ -1140,7 +1140,7 @@ test('download', async (t) => {
const response = await fetch('http://localhost:' + fastify.server.address().port + '/root/path/override/test')
t.assert.ok(response.ok)
t.assert.deepStrictEqual(response.status, 200)
t.assert.deepStrictEqual(response.headers.get('content-disposition'), 'attachment; filename="foo.html"')
t.assert.deepStrictEqual(response.headers.get('content-disposition'), 'attachment; filename=foo.html')
t.assert.deepStrictEqual(await response.text(), deepContent)
genericResponseChecks(t, response)
})
Expand All @@ -1151,7 +1151,7 @@ test('download', async (t) => {
const response = await fetch('http://localhost:' + fastify.server.address().port + '/foo/bar/override')
t.assert.ok(response.ok)
t.assert.deepStrictEqual(response.status, 200)
t.assert.deepStrictEqual(response.headers.get('content-disposition'), 'attachment; filename="hello-world.html"')
t.assert.deepStrictEqual(response.headers.get('content-disposition'), 'attachment; filename=hello-world.html')
t.assert.deepStrictEqual(response.headers.get('cache-control'), 'public, max-age=7200, immutable')
t.assert.deepStrictEqual(await response.text(), indexContent)
genericResponseChecks(t, response)
Expand All @@ -1163,7 +1163,7 @@ test('download', async (t) => {
const response = await fetch('http://localhost:' + fastify.server.address().port + '/foo/bar/override/2')
t.assert.ok(response.ok)
t.assert.deepStrictEqual(response.status, 200)
t.assert.deepStrictEqual(response.headers.get('content-disposition'), 'attachment; filename="index.html"')
t.assert.deepStrictEqual(response.headers.get('content-disposition'), 'attachment; filename=index.html')
t.assert.deepStrictEqual(response.headers.get('accept-ranges'), null)
t.assert.deepStrictEqual(await response.text(), indexContent)
genericResponseChecks(t, response)
Expand All @@ -1175,7 +1175,7 @@ test('download', async (t) => {
const response = await fetch('http://localhost:' + fastify.server.address().port + '/root/path/override/test/change')
t.assert.ok(response.ok)
t.assert.deepStrictEqual(response.status, 200)
t.assert.deepStrictEqual(response.headers.get('content-disposition'), 'attachment; filename="hello-world.html"')
t.assert.deepStrictEqual(response.headers.get('content-disposition'), 'attachment; filename=hello-world.html')
t.assert.deepStrictEqual(await response.text(), deepContent)
genericResponseChecks(t, response)
})
Expand Down
Loading