Use mime-types for file to content type mapping

This commit is contained in:
Douglas Christopher Wilson 2022-02-06 10:26:18 -05:00
parent 3d05e85b0c
commit a0276c6c91
4 changed files with 13 additions and 16 deletions

View File

@ -5,6 +5,7 @@ This incorporates all changes after 4.17.1 up to 4.17.2.
* change:
- query parser setting defaults to `'simple'`
- Use `mime-types` for file to content type mapping
* deps: array-flatten@3.0.0
* deps: body-parser@2.0.0-beta.1
- `req.body` is no longer always initialized to `{}`

View File

@ -18,6 +18,7 @@ var encodeUrl = require('encodeurl');
var escapeHtml = require('escape-html');
var http = require('http');
var onFinished = require('on-finished');
var mime = require('mime-types')
var path = require('path');
var pathIsAbsolute = require('path-is-absolute');
var statuses = require('statuses')
@ -29,7 +30,6 @@ var setCharset = require('./utils').setCharset;
var cookie = require('cookie');
var send = require('send');
var extname = path.extname;
var mime = send.mime;
var resolve = path.resolve;
var vary = require('vary');
@ -47,13 +47,6 @@ var res = Object.create(http.ServerResponse.prototype)
module.exports = res
/**
* Module variables.
* @private
*/
var charsetRegExp = /;\s*charset\s*=/;
/**
* Set status `code`.
*
@ -451,8 +444,10 @@ res.download = function download (path, filename, options, callback) {
};
/**
* Set _Content-Type_ response header with `type` through `mime.lookup()`
* Set _Content-Type_ response header with `type` through `mime.contentType()`
* when it does not contain "/", or set the Content-Type to `type` otherwise.
* When no mapping is found though `mime.contentType()`, the type is set to
* "application/octet-stream".
*
* Examples:
*
@ -470,7 +465,7 @@ res.download = function download (path, filename, options, callback) {
res.contentType =
res.type = function contentType(type) {
var ct = type.indexOf('/') === -1
? mime.lookup(type)
? (mime.contentType(type) || 'application/octet-stream')
: type;
return this.set('Content-Type', ct);
@ -621,6 +616,9 @@ res.append = function append(field, val) {
*
* Aliased as `res.header()`.
*
* When the set header is "Content-Type", the type is expanded to include
* the charset if not present using `mime.contentType()`.
*
* @param {String|Object} field
* @param {String|Array} val
* @return {ServerResponse} for chaining
@ -639,10 +637,7 @@ res.header = function header(field, val) {
if (Array.isArray(value)) {
throw new TypeError('Content-Type cannot be set to an Array');
}
if (!charsetRegExp.test(value)) {
var charset = mime.charsets.lookup(value.split(';')[0]);
if (charset) value += '; charset=' + charset.toLowerCase();
}
value = mime.contentType(value)
}
this.setHeader(field, value);

View File

@ -14,8 +14,8 @@
var Buffer = require('safe-buffer').Buffer
var contentType = require('content-type');
var mime = require('send').mime;
var etag = require('etag');
var mime = require('mime-types')
var proxyaddr = require('proxy-addr');
var qs = require('qs');
var querystring = require('querystring');
@ -53,7 +53,7 @@ exports.wetag = createETagGenerator({ weak: true })
exports.normalizeType = function(type){
return ~type.indexOf('/')
? acceptParams(type)
: { value: mime.lookup(type), params: {} };
: { value: (mime.lookup(type) || 'application/octet-stream'), params: {} }
};
/**

View File

@ -44,6 +44,7 @@
"fresh": "0.5.2",
"merge-descriptors": "1.0.1",
"methods": "~1.1.2",
"mime-types": "~2.1.34",
"on-finished": "~2.3.0",
"parseurl": "~1.3.3",
"path-is-absolute": "1.0.1",