Skip to content

Commit

Permalink
feat: add contentType option (#93)
Browse files Browse the repository at this point in the history
* feat: add  `contentType` option

* test: add type tests

* Update README.md

Signed-off-by: Frazer Smith <[email protected]>

---------

Signed-off-by: Frazer Smith <[email protected]>
  • Loading branch information
Fdawgs authored Nov 22, 2024
1 parent 6f078bb commit 277a32f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ of the `Range` request header.
Enable or disable setting `Cache-Control` response header, defaults to
true. Disabling this will ignore the `immutable` and `maxAge` options.

##### contentType

By default, this library uses the `mime` module to set the `Content-Type`
of the response based on the file extension of the requested file.

To disable this functionality, set `contentType` to `false`.
The `Content-Type` header will need to be set manually if disabled.

##### dotfiles

Set how "dotfiles" are treated when encountered. A dotfile is a file
Expand Down
21 changes: 14 additions & 7 deletions lib/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ function normalizeOptions (options) {
? Boolean(options.cacheControl)
: true

const contentType = options.contentType !== undefined
? Boolean(options.contentType)
: true

const etag = options.etag !== undefined
? Boolean(options.etag)
: true
Expand Down Expand Up @@ -137,6 +141,7 @@ function normalizeOptions (options) {
return {
acceptRanges,
cacheControl,
contentType,
etag,
dotfiles,
extensions,
Expand Down Expand Up @@ -489,13 +494,15 @@ function sendFileDirectly (request, path, stat, options) {
}

// set content-type
let type = mime.getType(path) || mime.default_type
debug('content-type %s', type)
if (type && isUtf8MimeType(type)) {
type += '; charset=utf-8'
}
if (type) {
headers['Content-Type'] = type
if (options.contentType) {
let type = mime.getType(path) || mime.default_type
debug('content-type %s', type)
if (type && isUtf8MimeType(type)) {
type += '; charset=utf-8'
}
if (type) {
headers['Content-Type'] = type
}
}

// conditional GET support
Expand Down
15 changes: 14 additions & 1 deletion test/send.1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const { shouldNotHaveHeader, createServer } = require('./utils')
const fixtures = path.join(__dirname, 'fixtures')

test('send(file, options)', function (t) {
t.plan(10)
t.plan(11)

t.test('acceptRanges', function (t) {
t.plan(2)
Expand Down Expand Up @@ -60,6 +60,19 @@ test('send(file, options)', function (t) {
})
})

t.test('contentType', function (t) {
t.plan(1)

t.test('should support disabling content-type', function (t) {
t.plan(2)

request(createServer({ contentType: false, root: fixtures }))
.get('/name.txt')
.expect(shouldNotHaveHeader('Content-Type', t))
.expect(200, err => t.error(err))
})
})

t.test('etag', function (t) {
t.plan(1)

Expand Down
5 changes: 5 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ declare namespace send {
*/
cacheControl?: boolean | undefined;

/**
* Enable or disable setting Content-Type response header, defaults to true.
*/
contentType?: boolean | undefined;

/**
* Set how "dotfiles" are treated when encountered.
* A dotfile is a file or directory that begins with a dot (".").
Expand Down
10 changes: 9 additions & 1 deletion types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,22 @@ const req: any = {}
}

{
const result = await send(req, '/test.html', { maxAge: 0, root: __dirname + '/wwwroot' })
const result = await send(req, '/test.html', { contentType: true, maxAge: 0, root: __dirname + '/wwwroot' })

expectType<SendResult>(result)
expectType<number>(result.statusCode)
expectType<Record<string, string>>(result.headers)
expectType<Readable>(result.stream)
}

{
const result = await send(req, '/test.html', { contentType: false, root: __dirname + '/wwwroot' })

expectType<SendResult>(result)
expectType<number>(result.statusCode)
expectType<Record<string, string>>(result.headers)
expectType<Readable>(result.stream)
}

const result = await send(req, '/test.html')
switch (result.type) {
Expand Down

0 comments on commit 277a32f

Please sign in to comment.