Skip to content

Commit

Permalink
Switch to mime-types from mime
Browse files Browse the repository at this point in the history
  • Loading branch information
LinusU authored and dougwilson committed Feb 3, 2022
1 parent 0b7bd4b commit 9238dae
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 65 deletions.
27 changes: 10 additions & 17 deletions README.md
Expand Up @@ -133,15 +133,6 @@ The `SendStream` is an event emitter and will emit the following events:
The `pipe` method is used to pipe the response into the Node.js HTTP response
object, typically `send(req, path, options).pipe(res)`.

### .mime

The `mime` export is the global instance of of the
[`mime` npm module](https://www.npmjs.com/package/mime).

This is used to configure the MIME types that are associated with file extensions
as well as other options for how to resolve the MIME type of a file (like the
default type to use for an unknown file extension).

## Error-handling

By default when no `error` listeners are present an automatic response will be
Expand Down Expand Up @@ -210,20 +201,22 @@ server.listen(3000)
### Custom file types

```js
var extname = require('path').extname
var http = require('http')
var parseUrl = require('parseurl')
var send = require('send')

// Default unknown types to text/plain
send.mime.default_type = 'text/plain'

// Add a custom type
send.mime.define({
'application/x-my-type': ['x-mt', 'x-mtt']
})

var server = http.createServer(function onRequest (req, res) {
send(req, parseUrl(req).pathname, { root: '/www/public' })
.on('headers', function (res, path) {
switch (extname(path)) {
case '.x-mt':
case '.x-mtt':
// custom type for these extensions
res.setHeader('Content-Type', 'application/x-my-type')
break
}
})
.pipe(res)
})

Expand Down
15 changes: 4 additions & 11 deletions index.js
Expand Up @@ -21,7 +21,7 @@ var escapeHtml = require('escape-html')
var etag = require('etag')
var fresh = require('fresh')
var fs = require('fs')
var mime = require('mime')
var mime = require('mime-types')
var ms = require('ms')
var onFinished = require('on-finished')
var parseRange = require('range-parser')
Expand Down Expand Up @@ -68,7 +68,6 @@ var UP_PATH_REGEXP = /(?:^|[\\/])\.\.(?:[\\/]|$)/
*/

module.exports = send
module.exports.mime = mime

/**
* Return a `SendStream` for `req` and `path`.
Expand Down Expand Up @@ -835,17 +834,11 @@ SendStream.prototype.type = function type (path) {

if (res.getHeader('Content-Type')) return

var type = mime.lookup(path)

if (!type) {
debug('no content-type')
return
}

var charset = mime.charsets.lookup(type)
var ext = extname(path)
var type = mime.contentType(ext) || 'application/octet-stream'

debug('content-type %s', type)
res.setHeader('Content-Type', type + (charset ? '; charset=' + charset : ''))
res.setHeader('Content-Type', type)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -24,7 +24,7 @@
"etag": "~1.8.1",
"fresh": "0.5.2",
"http-errors": "1.8.1",
"mime": "1.6.0",
"mime-types": "~2.1.34",
"ms": "2.1.3",
"on-finished": "~2.3.0",
"range-parser": "~1.2.1",
Expand Down
45 changes: 9 additions & 36 deletions test/send.js
Expand Up @@ -147,16 +147,23 @@ describe('send(file).pipe(res)', function () {
it('should set Content-Type via mime map', function (done) {
request(app)
.get('/name.txt')
.expect('Content-Type', 'text/plain; charset=UTF-8')
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect(200, function (err) {
if (err) return done(err)
request(app)
.get('/tobi.html')
.expect('Content-Type', 'text/html; charset=UTF-8')
.expect('Content-Type', 'text/html; charset=utf-8')
.expect(200, done)
})
})

it('should default Content-Type to octet-stream', function (done) {
request(app)
.get('/no_ext')
.expect('Content-Type', 'application/octet-stream')
.expect(200, done)
})

it('should 404 if file disappears after stat, before open', function (done) {
var app = http.createServer(function (req, res) {
send(req, req.url, { root: 'test/fixtures' })
Expand Down Expand Up @@ -1420,40 +1427,6 @@ describe('send(file, options)', function () {
})
})

describe('send.mime', function () {
it('should be exposed', function () {
assert.ok(send.mime)
})

describe('.default_type', function () {
before(function () {
this.default_type = send.mime.default_type
})

afterEach(function () {
send.mime.default_type = this.default_type
})

it('should change the default type', function (done) {
send.mime.default_type = 'text/plain'

request(createServer({ root: fixtures }))
.get('/no_ext')
.expect('Content-Type', 'text/plain; charset=UTF-8')
.expect(200, done)
})

it('should not add Content-Type for undefined default', function (done) {
send.mime.default_type = undefined

request(createServer({ root: fixtures }))
.get('/no_ext')
.expect(shouldNotHaveHeader('Content-Type'))
.expect(200, done)
})
})
})

function createServer (opts, fn) {
return http.createServer(function onRequest (req, res) {
try {
Expand Down

0 comments on commit 9238dae

Please sign in to comment.