refactor mime-types
This commit is contained in:
parent
38a715b3a2
commit
782aa2672e
2 changed files with 33 additions and 64 deletions
|
|
@ -3,7 +3,7 @@ import CSS from './css.js';
|
||||||
import JS from './js.js';
|
import JS from './js.js';
|
||||||
import setCookie from 'set-cookie-parser';
|
import setCookie from 'set-cookie-parser';
|
||||||
import { xor, base64, plain } from './codecs.js';
|
import { xor, base64, plain } from './codecs.js';
|
||||||
import mimeTypes from 'mime-types';
|
import * as mimeTypes from './mime.js';
|
||||||
import {
|
import {
|
||||||
validateCookie,
|
validateCookie,
|
||||||
db,
|
db,
|
||||||
|
|
|
||||||
|
|
@ -1,51 +1,13 @@
|
||||||
/*!
|
|
||||||
* mime-types
|
|
||||||
* Copyright(c) 2014 Jonathan Ong
|
|
||||||
* Copyright(c) 2015 Douglas Christopher Wilson
|
|
||||||
* MIT Licensed
|
|
||||||
*/
|
|
||||||
|
|
||||||
'use strict';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module dependencies.
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
|
|
||||||
var $exports = {};
|
|
||||||
|
|
||||||
import db from 'mime-db';
|
import db from 'mime-db';
|
||||||
|
|
||||||
var extname = function (path = '') {
|
const EXTRACT_TYPE_REGEXP = /^\s*([^;\s]*)(?:;|\s|$)/;
|
||||||
if (!path.includes('.')) return '';
|
const TEXT_TYPE_REGEXP = /^text\//i;
|
||||||
const map = path.split('.');
|
const extensions = Object.create(null);
|
||||||
|
const types = Object.create(null);
|
||||||
return '.' + map[map.length - 1];
|
const charsets = { lookup: charset };
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module variables.
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
|
|
||||||
var EXTRACT_TYPE_REGEXP = /^\s*([^;\s]*)(?:;|\s|$)/;
|
|
||||||
var TEXT_TYPE_REGEXP = /^text\//i;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module exports.
|
|
||||||
* @public
|
|
||||||
*/
|
|
||||||
|
|
||||||
$exports.charset = charset;
|
|
||||||
$exports.charsets = { lookup: charset };
|
|
||||||
$exports.contentType = contentType;
|
|
||||||
$exports.extension = extension;
|
|
||||||
$exports.extensions = Object.create(null);
|
|
||||||
$exports.lookup = lookup;
|
|
||||||
$exports.types = Object.create(null);
|
|
||||||
|
|
||||||
// Populate the extensions/types maps
|
// Populate the extensions/types maps
|
||||||
populateMaps($exports.extensions, $exports.types);
|
populateMaps(extensions, types);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the default charset for a MIME type.
|
* Get the default charset for a MIME type.
|
||||||
|
|
@ -60,8 +22,8 @@ function charset(type) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: use media-typer
|
// TODO: use media-typer
|
||||||
var match = EXTRACT_TYPE_REGEXP.exec(type);
|
const match = EXTRACT_TYPE_REGEXP.exec(type);
|
||||||
var mime = match && db[match[1].toLowerCase()];
|
const mime = match && db[match[1].toLowerCase()];
|
||||||
|
|
||||||
if (mime && mime.charset) {
|
if (mime && mime.charset) {
|
||||||
return mime.charset;
|
return mime.charset;
|
||||||
|
|
@ -88,7 +50,7 @@ function contentType(str) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
var mime = str.indexOf('/') === -1 ? $exports.lookup(str) : str;
|
let mime = str.indexOf('/') === -1 ? lookup(str) : str;
|
||||||
|
|
||||||
if (!mime) {
|
if (!mime) {
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -96,7 +58,7 @@ function contentType(str) {
|
||||||
|
|
||||||
// TODO: use content-type or other module
|
// TODO: use content-type or other module
|
||||||
if (mime.indexOf('charset') === -1) {
|
if (mime.indexOf('charset') === -1) {
|
||||||
var charset = $exports.charset(mime);
|
const charset = charset(mime);
|
||||||
if (charset) mime += '; charset=' + charset.toLowerCase();
|
if (charset) mime += '; charset=' + charset.toLowerCase();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -116,10 +78,10 @@ function extension(type) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: use media-typer
|
// TODO: use media-typer
|
||||||
var match = EXTRACT_TYPE_REGEXP.exec(type);
|
const match = EXTRACT_TYPE_REGEXP.exec(type);
|
||||||
|
|
||||||
// get extensions
|
// get extensions
|
||||||
var exts = match && $exports.extensions[match[1].toLowerCase()];
|
const exts = match && extensions[match[1].toLowerCase()];
|
||||||
|
|
||||||
if (!exts || !exts.length) {
|
if (!exts || !exts.length) {
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -141,15 +103,15 @@ function lookup(path) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the extension ("ext" or ".ext" or full path)
|
// get the extension ("ext" or ".ext" or full path)
|
||||||
var extension = extname('x.' + path)
|
const extension = extname('x.' + path)
|
||||||
.toLowerCase()
|
.toLowerCase()
|
||||||
.substr(1);
|
.slice(1);
|
||||||
|
|
||||||
if (!extension) {
|
if (!extension) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $exports.types[extension] || false;
|
return types[extension] || false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -159,11 +121,11 @@ function lookup(path) {
|
||||||
|
|
||||||
function populateMaps(extensions, types) {
|
function populateMaps(extensions, types) {
|
||||||
// source preference (least -> most)
|
// source preference (least -> most)
|
||||||
var preference = ['nginx', 'apache', undefined, 'iana'];
|
const preference = ['nginx', 'apache', undefined, 'iana'];
|
||||||
|
|
||||||
Object.keys(db).forEach(function forEachMimeType(type) {
|
for (const type in db) {
|
||||||
var mime = db[type];
|
const mime = db[type];
|
||||||
var exts = mime.extensions;
|
const exts = mime.extensions;
|
||||||
|
|
||||||
if (!exts || !exts.length) {
|
if (!exts || !exts.length) {
|
||||||
return;
|
return;
|
||||||
|
|
@ -173,12 +135,12 @@ function populateMaps(extensions, types) {
|
||||||
extensions[type] = exts;
|
extensions[type] = exts;
|
||||||
|
|
||||||
// extension -> mime
|
// extension -> mime
|
||||||
for (var i = 0; i < exts.length; i++) {
|
for (let i = 0; i < exts.length; i++) {
|
||||||
var extension = exts[i];
|
const extension = exts[i];
|
||||||
|
|
||||||
if (types[extension]) {
|
if (types[extension]) {
|
||||||
var from = preference.indexOf(db[types[extension]].source);
|
const from = preference.indexOf(db[types[extension]].source);
|
||||||
var to = preference.indexOf(mime.source);
|
const to = preference.indexOf(mime.source);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
types[extension] !== 'application/octet-stream' &&
|
types[extension] !== 'application/octet-stream' &&
|
||||||
|
|
@ -194,7 +156,14 @@ function populateMaps(extensions, types) {
|
||||||
// set the extension -> mime
|
// set the extension -> mime
|
||||||
types[extension] = type;
|
types[extension] = type;
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default $exports;
|
function extname(path = '') {
|
||||||
|
if (!path.includes('.')) return '';
|
||||||
|
const map = path.split('.');
|
||||||
|
|
||||||
|
return '.' + map[map.length - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
export { charset, charsets, contentType, extension, lookup };
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue