aboutsummaryrefslogtreecommitdiff
path: root/node_modules/node-mime/src/index.js
blob: c472624eb1a97e6b3f399f460e96c3c1bd128555 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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;