var SPLITER = /[ ,]+/; function Mime(){ this.types = {}; this.extensions = {}; this.defaultType = "text/plain"; this.defaultExtension = "txt"; this.register("*/*", "*"); this.register("text/html", "html htm xhtml"); this.register("text/plain", "txt"); this.register("application/javascript", "js"); this.register("text/css", "css"); this.register("text/calendar", "ics"); this.register("text/csv", "csv"); this.register("image/png", "png"); this.register("image/jpeg", "jpeg jpg"); this.register("image/gif", "gif"); this.register("image/bmp", "bmp"); this.register("image/x-icon", "ico"); this.register("image/tiff", "tiff tif"); this.register("image/svg+xml", "svg"); this.register("video/mpeg", "mpg mpeg mpe"); this.register("application/xml", "xml"); this.register("application/rss+xml", "rss"); this.register("application/atom+xml", "atom"); this.register("application/x-yaml", "yaml"); this.register("multipart/form-data", "multipart_form"); this.register("application/x-www-form-urlencoded", "url_encoded_form"); this.register("application/x-font-ttf", "ttf"); this.register("application/x-font-truetype", "ttf"); this.register("application/x-font-opentype", "otf"); this.register("application/font-woff", "woff"); this.register("application/vnd.ms-fontobject", "eot"); this.register("application/json", "map"); this.register("application/json", "json"); this.register("application/pdf", "pdf"); this.register("application/zip", "zip"); } Mime.prototype.register = function(type, exts){ var types = this.types, extensions = this.extensions, ext, i; exts = exts.split(SPLITER); for (i = exts.length; i--;) { ext = exts[i]; if (!ext.length) continue; if (!extensions[ext]) extensions[ext] = type; } if (!types[type] && exts[0]) types[type] = exts[0]; return this; }; Mime.prototype.unregister = function(exts){ var types = this.types, extensions = this.extensions, key, i; exts = exts.split(SPLITER); for (i = exts.length; i--;) { ext = exts[i]; if (!ext.length) continue; for (key in types) { if (types[key] === ext) types[key] = undefined; } extensions[ext] = undefined; } return this; }; Mime.prototype.unregisterType = function(type){ var types = this.types, extensions = this.extensions, key, i; for (key in extensions) { if (extensions[key] === type) extensions[key] = undefined; } types[type] = undefined; return this; }; Mime.prototype.lookUpType = function(ext, fallback){ var type = this.extensions[ext]; fallback = fallback === undefined ? true : !!fallback; return type ? type : fallback ? this.defaultType : undefined; }; Mime.prototype.lookUpExt = function(type, fallback){ var ext = this.types[type]; fallback = fallback === undefined ? true : !!fallback; return ext ? ext : fallback ? this.defaultExtension : undefined; }; module.exports = new Mime;