forked from jfhbrook/node-ecstatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opts.js
144 lines (128 loc) · 2.74 KB
/
opts.js
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// This is so you can have options aliasing and defaults in one place.
module.exports = function (opts) {
var autoIndex = true,
showDir = true,
humanReadable = true,
si = false,
cache = 'max-age=3600',
gzip = false,
defaultExt = '.html',
handleError = true,
serverHeader = true,
contentType = 'application/octet-stream',
mimeTypes;
function isDeclared(k) {
return typeof opts[k] !== 'undefined' && opts[k] !== null;
}
if (opts) {
[
'autoIndex',
'autoindex'
].some(function (k) {
if (isDeclared(k)) {
autoIndex = opts[k];
return true;
}
});
[
'showDir',
'showdir'
].some(function (k) {
if (isDeclared(k)) {
showDir = opts[k];
return true;
}
});
[
'humanReadable',
'humanreadable',
'human-readable'
].some(function (k) {
if (isDeclared(k)) {
humanReadable = opts[k];
return true;
}
});
[
'si',
'index'
].some(function (k) {
if (isDeclared(k)) {
si = opts[k];
return true;
}
});
if (opts.defaultExt && typeof opts.defaultExt === 'string') {
defaultExt = opts.defaultExt;
}
if (typeof opts.cache !== 'undefined' && opts.cache !== null) {
if (typeof opts.cache === 'string') {
cache = opts.cache;
}
else if(typeof opts.cache === 'number') {
cache = 'max-age=' + opts.cache;
}
}
if (typeof opts.gzip !== 'undefined' && opts.gzip !== null) {
gzip = opts.gzip;
}
[
'handleError',
'handleerror'
].some(function (k) {
if (isDeclared(k)) {
handleError = opts[k];
return true;
}
});
[
'serverHeader',
'serverheader',
'server-header'
].some(function (k) {
if (isDeclared(k)) {
serverHeader = opts[k];
return true;
}
});
[
'contentType',
'contenttype',
'content-type'
].some(function (k) {
if (isDeclared(k)) {
contentType = opts[k];
return true;
}
});
[
'mimetype',
'mimetypes',
'mimeType',
'mimeTypes',
'mime-type',
'mime-types',
'mime-Type',
'mime-Types'
].some(function (k) {
if (isDeclared(k)) {
mimeTypes = opts[k];
return true;
}
});
}
return {
cache: cache,
autoIndex: autoIndex,
showDir: showDir,
humanReadable: humanReadable,
si: si,
defaultExt: defaultExt,
baseDir: (opts && opts.baseDir) || '/',
gzip: gzip,
handleError: handleError,
serverHeader: serverHeader,
contentType: contentType,
mimeTypes: mimeTypes
};
};