Skip to content

Commit 8f800f5

Browse files
committed
clean up parseOpts function and add jsdoc for parsed options object
1 parent 5e0767e commit 8f800f5

File tree

3 files changed

+110
-119
lines changed

3 files changed

+110
-119
lines changed

lib/core/defaults.json

Lines changed: 0 additions & 22 deletions
This file was deleted.

lib/core/index.js

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -103,19 +103,9 @@ module.exports = function createMiddleware(_dir, _options) {
103103

104104
const root = path.join(path.resolve(dir), '/');
105105
const opts = optsParser(options);
106-
const cache = opts.cache;
107-
const autoIndex = opts.autoIndex;
108-
const baseDir = opts.baseDir;
109-
let defaultExt = opts.defaultExt;
110-
const handleError = opts.handleError;
111-
const headers = opts.headers;
112-
const weakEtags = opts.weakEtags;
113-
const handleOptionsMethod = opts.handleOptionsMethod;
114106

115107
opts.root = dir;
116-
if (defaultExt && /^\./.test(defaultExt)) {
117-
defaultExt = defaultExt.replace(/^\./, '');
118-
}
108+
119109

120110
// Support hashes and .types files in mimeTypes @since 0.8
121111
if (opts.mimeTypes) {
@@ -197,7 +187,7 @@ module.exports = function createMiddleware(_dir, _options) {
197187
file = path.normalize(
198188
path.join(
199189
root,
200-
path.relative(path.join('/', baseDir), pathname)
190+
path.relative(path.join('/', opts.baseDir), pathname)
201191
)
202192
);
203193
// determine compressed forms if they were to exist, make sure to handle pre-compressed files, i.e. files with .br/.gz extension. we will serve them "as-is"
@@ -209,11 +199,11 @@ module.exports = function createMiddleware(_dir, _options) {
209199
if ( file.endsWith('.br') ) brotliFile = file;
210200
}
211201

212-
Object.keys(headers).forEach((key) => {
213-
res.setHeader(key, headers[key]);
202+
Object.keys(opts.headers).forEach((key) => {
203+
res.setHeader(key, opts.headers[key]);
214204
});
215205

216-
if (req.method === 'OPTIONS' && handleOptionsMethod) {
206+
if (req.method === 'OPTIONS' && opts.handleOptionsMethod) {
217207
res.end();
218208
return;
219209
}
@@ -238,8 +228,8 @@ module.exports = function createMiddleware(_dir, _options) {
238228
let contentType = mime.lookup(file, defaultType);
239229
const range = (req.headers && req.headers.range);
240230
const lastModified = (new Date(stat.mtime)).toUTCString();
241-
const etag = generateEtag(stat, weakEtags);
242-
let cacheControl = cache;
231+
const etag = generateEtag(stat, opts.weakEtags);
232+
let cacheControl = opts.cache;
243233
let stream = null;
244234
if (contentType && isTextFile(contentType)) {
245235
if (stat.size < buffer.constants.MAX_LENGTH) {
@@ -266,7 +256,7 @@ module.exports = function createMiddleware(_dir, _options) {
266256
}
267257

268258
if (typeof cacheControl === 'function') {
269-
cacheControl = cache(pathname);
259+
cacheControl = opts.cache(pathname);
270260
}
271261
if (typeof cacheControl === 'number') {
272262
cacheControl = `max-age=${cacheControl}`;
@@ -374,11 +364,11 @@ module.exports = function createMiddleware(_dir, _options) {
374364
// This means we're already trying ./404.html and can not find it.
375365
// So send plain text response with 404 status code
376366
status[404](res, next);
377-
} else if (!path.extname(parsed.pathname).length && defaultExt) {
367+
} else if (!path.extname(parsed.pathname).length && opts.defaultExt) {
378368
// If there is no file extension in the path and we have a default
379369
// extension try filename and default extension combination before rendering 404.html.
380370
middleware({
381-
url: `${parsed.pathname}.${defaultExt}${(parsed.search) ? parsed.search : ''}`,
371+
url: `${parsed.pathname}.${opts.defaultExt}${(parsed.search) ? parsed.search : ''}`,
382372
headers: req.headers,
383373
}, res, next);
384374
} else if (opts.showDir && opts.dirOverrides404) {
@@ -388,7 +378,7 @@ module.exports = function createMiddleware(_dir, _options) {
388378
return;
389379
} else {
390380
// Try to serve default ./404.html
391-
const rawUrl = (handleError ? `/${path.join(baseDir, `404.${defaultExt}`)}` : req.url);
381+
const rawUrl = (opts.handleError ? `/${path.join(opts.baseDir, `404.${opts.defaultExt}`)}` : req.url);
392382
const encodedUrl = ensureUriEncoded(rawUrl);
393383
middleware({
394384
url: encodedUrl,
@@ -399,7 +389,7 @@ module.exports = function createMiddleware(_dir, _options) {
399389
} else if (err) {
400390
status[500](res, next, { error: err });
401391
} else if (stat.isDirectory()) {
402-
if (!autoIndex && !opts.showDir) {
392+
if (!opts.autoIndex && !opts.showDir) {
403393
status[404](res, next);
404394
return;
405395
}
@@ -417,11 +407,11 @@ module.exports = function createMiddleware(_dir, _options) {
417407
return;
418408
}
419409

420-
if (autoIndex) {
410+
if (opts.autoIndex) {
421411
middleware({
422412
url: urlJoin(
423413
encodeURIComponent(pathname),
424-
`/index.${defaultExt}`
414+
`/index.${opts.defaultExt}`
425415
),
426416
headers: req.headers,
427417
}, res, (autoIndexError) => {

0 commit comments

Comments
 (0)