Changeset e29cc2e for trip-planner-front/node_modules/mime/Mime.js
- Timestamp:
- 11/25/21 22:08:24 (3 years ago)
- Branches:
- master
- Children:
- 8d391a1
- Parents:
- 59329aa
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trip-planner-front/node_modules/mime/Mime.js
r59329aa re29cc2e 1 'use strict'; 1 var path = require('path'); 2 var fs = require('fs'); 2 3 3 /**4 * @param typeMap [Object] Map of MIME type -> Array[extensions]5 * @param ...6 */7 4 function Mime() { 8 this._types = Object.create(null);9 this. _extensions = Object.create(null);5 // Map of extension -> mime type 6 this.types = Object.create(null); 10 7 11 for (let i = 0; i < arguments.length; i++) { 12 this.define(arguments[i]); 13 } 14 15 this.define = this.define.bind(this); 16 this.getType = this.getType.bind(this); 17 this.getExtension = this.getExtension.bind(this); 8 // Map of mime type -> extension 9 this.extensions = Object.create(null); 18 10 } 19 11 … … 25 17 * e.g. mime.define({'audio/ogg', ['oga', 'ogg', 'spx']}); 26 18 * 27 * If a type declares an extension that has already been defined, an error will28 * be thrown. To suppress this error and force the extension to be associated29 * with the new type, pass `force`=true. Alternatively, you may prefix the30 * extension with "*" to map the type to extension, without mapping the31 * extension to the type.32 *33 * e.g. mime.define({'audio/wav', ['wav']}, {'audio/x-wav', ['*wav']});34 *35 *36 19 * @param map (Object) type definitions 37 * @param force (Boolean) if true, force overriding of existing definitions38 20 */ 39 Mime.prototype.define = function(typeMap, force) { 40 for (let type in typeMap) { 41 let extensions = typeMap[type].map(function(t) { 42 return t.toLowerCase(); 43 }); 44 type = type.toLowerCase(); 45 46 for (let i = 0; i < extensions.length; i++) { 47 const ext = extensions[i]; 48 49 // '*' prefix = not the preferred type for this extension. So fixup the 50 // extension, and skip it. 51 if (ext[0] === '*') { 52 continue; 21 Mime.prototype.define = function (map) { 22 for (var type in map) { 23 var exts = map[type]; 24 for (var i = 0; i < exts.length; i++) { 25 if (process.env.DEBUG_MIME && this.types[exts[i]]) { 26 console.warn((this._loading || "define()").replace(/.*\//, ''), 'changes "' + exts[i] + '" extension type from ' + 27 this.types[exts[i]] + ' to ' + type); 53 28 } 54 29 55 if (!force && (ext in this._types)) { 56 throw new Error( 57 'Attempt to change mapping for "' + ext + 58 '" extension from "' + this._types[ext] + '" to "' + type + 59 '". Pass `force=true` to allow this, otherwise remove "' + ext + 60 '" from the list of extensions for "' + type + '".' 61 ); 62 } 63 64 this._types[ext] = type; 30 this.types[exts[i]] = type; 65 31 } 66 32 67 // Use first extension as default 68 if (force || !this._extensions[type]) { 69 const ext = extensions[0]; 70 this._extensions[type] = (ext[0] !== '*') ? ext : ext.substr(1); 33 // Default extension is the first one we encounter 34 if (!this.extensions[type]) { 35 this.extensions[type] = exts[0]; 71 36 } 72 37 } … … 74 39 75 40 /** 41 * Load an Apache2-style ".types" file 42 * 43 * This may be called multiple times (it's expected). Where files declare 44 * overlapping types/extensions, the last file wins. 45 * 46 * @param file (String) path of file to load. 47 */ 48 Mime.prototype.load = function(file) { 49 this._loading = file; 50 // Read file and split into lines 51 var map = {}, 52 content = fs.readFileSync(file, 'ascii'), 53 lines = content.split(/[\r\n]+/); 54 55 lines.forEach(function(line) { 56 // Clean up whitespace/comments, and split into fields 57 var fields = line.replace(/\s*#.*|^\s*|\s*$/g, '').split(/\s+/); 58 map[fields.shift()] = fields; 59 }); 60 61 this.define(map); 62 63 this._loading = null; 64 }; 65 66 /** 76 67 * Lookup a mime type based on extension 77 68 */ 78 Mime.prototype.getType = function(path) { 79 path = String(path); 80 let last = path.replace(/^.*[/\\]/, '').toLowerCase(); 81 let ext = last.replace(/^.*\./, '').toLowerCase(); 69 Mime.prototype.lookup = function(path, fallback) { 70 var ext = path.replace(/^.*[\.\/\\]/, '').toLowerCase(); 82 71 83 let hasPath = last.length < path.length; 84 let hasDot = ext.length < last.length - 1; 85 86 return (hasDot || !hasPath) && this._types[ext] || null; 72 return this.types[ext] || fallback || this.default_type; 87 73 }; 88 74 … … 90 76 * Return file extension associated with a mime type 91 77 */ 92 Mime.prototype. getExtension = function(type) {93 type = /^\s*([^;\s]*)/.test(type) && RegExp.$1;94 return t ype && this._extensions[type.toLowerCase()] || null;78 Mime.prototype.extension = function(mimeType) { 79 var type = mimeType.match(/^\s*([^;\s]*)(?:;|\s|$)/)[1].toLowerCase(); 80 return this.extensions[type]; 95 81 }; 96 82 97 module.exports = Mime; 83 // Default instance 84 var mime = new Mime(); 85 86 // Define built-in types 87 mime.define(require('./types.json')); 88 89 // Default type 90 mime.default_type = mime.lookup('bin'); 91 92 // 93 // Additional API specific to the default instance 94 // 95 96 mime.Mime = Mime; 97 98 /** 99 * Lookup a charset based on mime type. 100 */ 101 mime.charsets = { 102 lookup: function(mimeType, fallback) { 103 // Assume text types are utf8 104 return (/^text\/|^application\/(javascript|json)/).test(mimeType) ? 'UTF-8' : fallback; 105 } 106 }; 107 108 module.exports = mime;
Note:
See TracChangeset
for help on using the changeset viewer.