Skip to content

Commit 889d310

Browse files
LinusUdougwilson
authored andcommitted
Use mime-types for file to content type mapping
closes pillarjs#192
1 parent e9ff7d9 commit 889d310

File tree

5 files changed

+25
-65
lines changed

5 files changed

+25
-65
lines changed

HISTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Remove `send.index()` -- use `index` in `options`
88
* Remove `send.maxage()` -- use `maxAge` in `options`
99
* Remove `send.root()` -- use `root` in `options`
10+
* Use `mime-types` for file to content type mapping -- removed `send.mime`
1011

1112
0.17.2 / 2021-12-11
1213
===================

README.md

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,6 @@ The `SendStream` is an event emitter and will emit the following events:
133133
The `pipe` method is used to pipe the response into the Node.js HTTP response
134134
object, typically `send(req, path, options).pipe(res)`.
135135

136-
### .mime
137-
138-
The `mime` export is the global instance of of the
139-
[`mime` npm module](https://www.npmjs.com/package/mime).
140-
141-
This is used to configure the MIME types that are associated with file extensions
142-
as well as other options for how to resolve the MIME type of a file (like the
143-
default type to use for an unknown file extension).
144-
145136
## Error-handling
146137

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

212203
```js
204+
var extname = require('path').extname
213205
var http = require('http')
214206
var parseUrl = require('parseurl')
215207
var send = require('send')
216208

217-
// Default unknown types to text/plain
218-
send.mime.default_type = 'text/plain'
219-
220-
// Add a custom type
221-
send.mime.define({
222-
'application/x-my-type': ['x-mt', 'x-mtt']
223-
})
224-
225209
var server = http.createServer(function onRequest (req, res) {
226210
send(req, parseUrl(req).pathname, { root: '/www/public' })
211+
.on('headers', function (res, path) {
212+
switch (extname(path)) {
213+
case '.x-mt':
214+
case '.x-mtt':
215+
// custom type for these extensions
216+
res.setHeader('Content-Type', 'application/x-my-type')
217+
break
218+
}
219+
})
227220
.pipe(res)
228221
})
229222

index.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var escapeHtml = require('escape-html')
2121
var etag = require('etag')
2222
var fresh = require('fresh')
2323
var fs = require('fs')
24-
var mime = require('mime')
24+
var mime = require('mime-types')
2525
var ms = require('ms')
2626
var onFinished = require('on-finished')
2727
var parseRange = require('range-parser')
@@ -68,7 +68,6 @@ var UP_PATH_REGEXP = /(?:^|[\\/])\.\.(?:[\\/]|$)/
6868
*/
6969

7070
module.exports = send
71-
module.exports.mime = mime
7271

7372
/**
7473
* Return a `SendStream` for `req` and `path`.
@@ -762,17 +761,11 @@ SendStream.prototype.type = function type (path) {
762761

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

765-
var type = mime.lookup(path)
766-
767-
if (!type) {
768-
debug('no content-type')
769-
return
770-
}
771-
772-
var charset = mime.charsets.lookup(type)
764+
var ext = extname(path)
765+
var type = mime.contentType(ext) || 'application/octet-stream'
773766

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

778771
/**

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"etag": "~1.8.1",
2525
"fresh": "0.5.2",
2626
"http-errors": "1.8.1",
27-
"mime": "1.6.0",
27+
"mime-types": "~2.1.34",
2828
"ms": "2.1.3",
2929
"on-finished": "~2.3.0",
3030
"range-parser": "~1.2.1",

test/send.js

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -147,16 +147,23 @@ describe('send(file).pipe(res)', function () {
147147
it('should set Content-Type via mime map', function (done) {
148148
request(app)
149149
.get('/name.txt')
150-
.expect('Content-Type', 'text/plain; charset=UTF-8')
150+
.expect('Content-Type', 'text/plain; charset=utf-8')
151151
.expect(200, function (err) {
152152
if (err) return done(err)
153153
request(app)
154154
.get('/tobi.html')
155-
.expect('Content-Type', 'text/html; charset=UTF-8')
155+
.expect('Content-Type', 'text/html; charset=utf-8')
156156
.expect(200, done)
157157
})
158158
})
159159

160+
it('should default Content-Type to octet-stream', function (done) {
161+
request(app)
162+
.get('/no_ext')
163+
.expect('Content-Type', 'application/octet-stream')
164+
.expect(200, done)
165+
})
166+
160167
it('should 404 if file disappears after stat, before open', function (done) {
161168
var app = http.createServer(function (req, res) {
162169
send(req, req.url, { root: 'test/fixtures' })
@@ -1281,40 +1288,6 @@ describe('send(file, options)', function () {
12811288
})
12821289
})
12831290

1284-
describe('send.mime', function () {
1285-
it('should be exposed', function () {
1286-
assert.ok(send.mime)
1287-
})
1288-
1289-
describe('.default_type', function () {
1290-
before(function () {
1291-
this.default_type = send.mime.default_type
1292-
})
1293-
1294-
afterEach(function () {
1295-
send.mime.default_type = this.default_type
1296-
})
1297-
1298-
it('should change the default type', function (done) {
1299-
send.mime.default_type = 'text/plain'
1300-
1301-
request(createServer({ root: fixtures }))
1302-
.get('/no_ext')
1303-
.expect('Content-Type', 'text/plain; charset=UTF-8')
1304-
.expect(200, done)
1305-
})
1306-
1307-
it('should not add Content-Type for undefined default', function (done) {
1308-
send.mime.default_type = undefined
1309-
1310-
request(createServer({ root: fixtures }))
1311-
.get('/no_ext')
1312-
.expect(shouldNotHaveHeader('Content-Type'))
1313-
.expect(200, done)
1314-
})
1315-
})
1316-
})
1317-
13181291
function createServer (opts, fn) {
13191292
return http.createServer(function onRequest (req, res) {
13201293
try {

0 commit comments

Comments
 (0)