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: add contentType send option #484

Merged
merged 3 commits into from
Nov 22, 2024
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ The following options are also supported and will be passed directly to the
[`@fastify/send`](https://www.npmjs.com/package/@fastify/send) module:

- [`acceptRanges`](https://www.npmjs.com/package/@fastify/send#acceptranges)
- [`contentType`](https://www.npmjs.com/package/@fastify/send#contenttype)
- [`cacheControl`](https://www.npmjs.com/package/@fastify/send#cachecontrol) (Enable or disable setting Cache-Control response header, defaults to true. **Important:** If you want to provide a custom Cache-Control response header, this option must be false.)
- [`dotfiles`](https://www.npmjs.com/package/@fastify/send#dotfiles)
- [`etag`](https://www.npmjs.com/package/@fastify/send#etag)
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ async function fastifyStatic (fastify, opts) {
const sendOptions = {
root: opts.root,
acceptRanges: opts.acceptRanges,
contentType: opts.contentType,
cacheControl: opts.cacheControl,
dotfiles: opts.dotfiles,
etag: opts.etag,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"homepage": "https://github.com/fastify/fastify-static",
"dependencies": {
"@fastify/accept-negotiator": "^2.0.0",
"@fastify/send": "^3.1.0",
"@fastify/send": "^3.2.0",
"content-disposition": "^0.5.4",
"fastify-plugin": "^5.0.0",
"fastq": "^1.17.1",
Expand Down
43 changes: 42 additions & 1 deletion test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1377,10 +1377,11 @@ t.test('root not found warning', (t) => {
})

t.test('send options', (t) => {
t.plan(11)
t.plan(12)
const pluginOptions = {
root: path.join(__dirname, '/static'),
acceptRanges: 'acceptRanges',
contentType: 'contentType',
cacheControl: 'cacheControl',
dotfiles: 'dotfiles',
etag: 'etag',
Expand All @@ -1396,6 +1397,7 @@ t.test('send options', (t) => {
t.equal(pathName, '/index.html')
t.equal(options.root, path.join(__dirname, '/static'))
t.equal(options.acceptRanges, 'acceptRanges')
t.equal(options.contentType, 'contentType')
t.equal(options.cacheControl, 'cacheControl')
t.equal(options.dotfiles, 'dotfiles')
t.equal(options.etag, 'etag')
Expand Down Expand Up @@ -4061,3 +4063,42 @@ t.test('respect the .code when using with sendFile', t => {
})
})
})

t.test('respect the .type when using with sendFile with contentType disabled', t => {
t.plan(6)

const pluginOptions = {
root: path.join(__dirname, '/static'),
contentType: false
}
const fastify = Fastify()

fastify.register(fastifyStatic, pluginOptions)

fastify.get('/custom', (_, reply) => {
return reply.type('text/html; charset=windows-1252').sendFile('foo.html')
})

t.teardown(fastify.close.bind(fastify))

fastify.listen({ port: 0 }, err => {
t.error(err)

fastify.server.unref()

const file = fs.readFileSync(path.join(__dirname, '/static/foo.html'))
const contentLength = Buffer.byteLength(file).toString()

simple.concat({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/custom',
followRedirect: false
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.equal(response.headers['content-type'], 'text/html; charset=windows-1252')
t.equal(response.headers['content-length'], contentLength)
t.equal(body.toString(), fooContent)
})
})
})
2 changes: 2 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ declare namespace fastifyStatic {
// Passed on to `send`
export interface SendOptions {
acceptRanges?: boolean;
contentType?: boolean;
cacheControl?: boolean;
dotfiles?: 'allow' | 'deny' | 'ignore';
etag?: boolean;
Expand Down Expand Up @@ -103,6 +104,7 @@ declare namespace fastifyStatic {

// Passed on to `send`
acceptRanges?: boolean;
contentType?: boolean;
cacheControl?: boolean;
dotfiles?: 'allow' | 'deny' | 'ignore';
etag?: boolean;
Expand Down
13 changes: 13 additions & 0 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ expectType<any>(fastifyStaticCjs)
const appWithImplicitHttp = fastify()
const options: FastifyStaticOptions = {
acceptRanges: true,
contentType: true,
cacheControl: true,
decorateReply: true,
dotfiles: 'allow',
Expand Down Expand Up @@ -145,6 +146,10 @@ appWithHttp2
appWithHttp2.get('/download/2', (request, reply) => {
reply.download('some-file-name', 'some-filename', { cacheControl: false, acceptRanges: true })
})

appWithHttp2.get('/download/3', (request, reply) => {
reply.download('some-file-name', 'some-filename', { contentType: false })
})
})

const multiRootAppWithImplicitHttp = fastify()
Expand All @@ -165,6 +170,10 @@ multiRootAppWithImplicitHttp
reply.sendFile('some-file-name', 'some-root-name', { cacheControl: false, acceptRanges: true })
})

multiRootAppWithImplicitHttp.get('/', (request, reply) => {
reply.sendFile('some-file-name', 'some-root-name-2', { contentType: false })
})

multiRootAppWithImplicitHttp.get('/download', (request, reply) => {
reply.download('some-file-name')
})
Expand All @@ -176,6 +185,10 @@ multiRootAppWithImplicitHttp
multiRootAppWithImplicitHttp.get('/download/2', (request, reply) => {
reply.download('some-file-name', 'some-filename', { cacheControl: false, acceptRanges: true })
})

multiRootAppWithImplicitHttp.get('/download/3', (request, reply) => {
reply.download('some-file-name', 'some-filename', { contentType: false })
})
})

const noIndexApp = fastify()
Expand Down