source: trip-planner-front/node_modules/mime/Mime.js@ b738035

Last change on this file since b738035 was e29cc2e, checked in by Ema <ema_spirova@…>, 3 years ago

primeNG components

  • Property mode set to 100644
File size: 2.7 KB
RevLine 
[e29cc2e]1var path = require('path');
2var fs = require('fs');
[6a3a178]3
4function Mime() {
[e29cc2e]5 // Map of extension -> mime type
6 this.types = Object.create(null);
[6a3a178]7
[e29cc2e]8 // Map of mime type -> extension
9 this.extensions = Object.create(null);
[6a3a178]10}
11
12/**
13 * Define mimetype -> extension mappings. Each key is a mime-type that maps
14 * to an array of extensions associated with the type. The first extension is
15 * used as the default extension for the type.
16 *
17 * e.g. mime.define({'audio/ogg', ['oga', 'ogg', 'spx']});
18 *
19 * @param map (Object) type definitions
20 */
[e29cc2e]21Mime.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);
[6a3a178]28 }
29
[e29cc2e]30 this.types[exts[i]] = type;
[6a3a178]31 }
32
[e29cc2e]33 // Default extension is the first one we encounter
34 if (!this.extensions[type]) {
35 this.extensions[type] = exts[0];
[6a3a178]36 }
37 }
38};
39
40/**
[e29cc2e]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.
[6a3a178]47 */
[e29cc2e]48Mime.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]+/);
[6a3a178]54
[e29cc2e]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 });
[6a3a178]60
[e29cc2e]61 this.define(map);
62
63 this._loading = null;
64};
65
66/**
67 * Lookup a mime type based on extension
68 */
69Mime.prototype.lookup = function(path, fallback) {
70 var ext = path.replace(/^.*[\.\/\\]/, '').toLowerCase();
71
72 return this.types[ext] || fallback || this.default_type;
[6a3a178]73};
74
75/**
76 * Return file extension associated with a mime type
77 */
[e29cc2e]78Mime.prototype.extension = function(mimeType) {
79 var type = mimeType.match(/^\s*([^;\s]*)(?:;|\s|$)/)[1].toLowerCase();
80 return this.extensions[type];
81};
82
83// Default instance
84var mime = new Mime();
85
86// Define built-in types
87mime.define(require('./types.json'));
88
89// Default type
90mime.default_type = mime.lookup('bin');
91
92//
93// Additional API specific to the default instance
94//
95
96mime.Mime = Mime;
97
98/**
99 * Lookup a charset based on mime type.
100 */
101mime.charsets = {
102 lookup: function(mimeType, fallback) {
103 // Assume text types are utf8
104 return (/^text\/|^application\/(javascript|json)/).test(mimeType) ? 'UTF-8' : fallback;
105 }
[6a3a178]106};
107
[e29cc2e]108module.exports = mime;
Note: See TracBrowser for help on using the repository browser.