opts.js
4.36 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// This is so you can have options aliasing and defaults in one place.
var defaults = require('./defaults.json');
var aliases = require('./aliases.json')
module.exports = function (opts) {
var autoIndex = defaults.autoIndex,
showDir = defaults.showDir,
showDotfiles = defaults.showDotfiles,
humanReadable = defaults.humanReadable,
si = defaults.si,
cache = defaults.cache,
gzip = defaults.gzip,
defaultExt = defaults.defaultExt,
handleError = defaults.handleError,
headers = {},
serverHeader = defaults.serverHeader,
contentType = defaults.contentType,
mimeTypes,
weakEtags = defaults.weakEtags,
weakCompare = defaults.weakCompare,
handleOptionsMethod = defaults.handleOptionsMethod;
function isDeclared(k) {
return typeof opts[k] !== 'undefined' && opts[k] !== null;
}
if (opts) {
aliases.autoIndex.some(function (k) {
if (isDeclared(k)) {
autoIndex = opts[k];
return true;
}
});
aliases.showDir.some(function (k) {
if (isDeclared(k)) {
showDir = opts[k];
return true;
}
});
aliases.showDotfiles.some(function (k) {
if (isDeclared(k)) {
showDotfiles = opts[k];
return true;
}
});
aliases.humanReadable.some(function (k) {
if (isDeclared(k)) {
humanReadable = opts[k];
return true;
}
});
aliases.si.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;
}
else if (typeof opts.cache === 'function') {
cache = opts.cache
}
}
if (typeof opts.gzip !== 'undefined' && opts.gzip !== null) {
gzip = opts.gzip;
}
aliases.handleError.some(function (k) {
if (isDeclared(k)) {
handleError = opts[k];
return true;
}
});
aliases.cors.forEach(function(k) {
if (isDeclared(k) && k) {
handleOptionsMethod = true;
headers['Access-Control-Allow-Origin'] = '*';
headers['Access-Control-Allow-Headers'] = 'Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since';
}
});
aliases.headers.forEach(function (k) {
if (!isDeclared(k)) return;
if (Array.isArray(opts[k])) {
opts[k].forEach(setHeader);
}
else if (opts[k] && typeof opts[k] === 'object') {
Object.keys(opts[k]).forEach(function (key) {
headers[key] = opts[k][key];
});
}
else setHeader(opts[k]);
function setHeader (str) {
var m = /^(.+?)\s*:\s*(.*)$/.exec(str)
if (!m) headers[str] = true
else headers[m[1]] = m[2]
}
});
aliases.serverHeader.some(function (k) {
if (isDeclared(k)) {
serverHeader = opts[k];
return true;
}
});
aliases.contentType.some(function (k) {
if (isDeclared(k)) {
contentType = opts[k];
return true;
}
});
aliases.mimeType.some(function (k) {
if (isDeclared(k)) {
mimeTypes = opts[k];
return true;
}
});
aliases.weakEtags.some(function (k) {
if (isDeclared(k)) {
weakEtags = opts[k];
return true;
}
});
aliases.weakCompare.some(function (k) {
if (isDeclared(k)) {
weakCompare = opts[k];
return true;
}
});
aliases.handleOptionsMethod.some(function (k) {
if (isDeclared(k)) {
handleOptionsMethod = handleOptionsMethod || opts[k];
return true;
}
});
}
return {
cache: cache,
autoIndex: autoIndex,
showDir: showDir,
showDotfiles: showDotfiles,
humanReadable: humanReadable,
si: si,
defaultExt: defaultExt,
baseDir: (opts && opts.baseDir) || '/',
gzip: gzip,
handleError: handleError,
headers: headers,
serverHeader: serverHeader,
contentType: contentType,
mimeTypes: mimeTypes,
weakEtags: weakEtags,
weakCompare: weakCompare,
handleOptionsMethod: handleOptionsMethod
};
};