Skip to content

Switch to mime-types from mime #192

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

Merged
merged 1 commit into from
Feb 3, 2022
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 HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Remove `send.index()` -- use `index` in `options`
* Remove `send.maxage()` -- use `maxAge` in `options`
* Remove `send.root()` -- use `root` in `options`
* Use `mime-types` for file to content type mapping -- removed `send.mime`

0.17.2 / 2021-12-11
===================
Expand Down
27 changes: 10 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 @@ -762,17 +761,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 @@ -1281,40 +1288,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