Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions lib/core/defaults.json

This file was deleted.

38 changes: 14 additions & 24 deletions lib/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,9 @@ module.exports = function createMiddleware(_dir, _options) {

const root = path.join(path.resolve(dir), '/');
const opts = optsParser(options);
const cache = opts.cache;
const autoIndex = opts.autoIndex;
const baseDir = opts.baseDir;
let defaultExt = opts.defaultExt;
const handleError = opts.handleError;
const headers = opts.headers;
const weakEtags = opts.weakEtags;
const handleOptionsMethod = opts.handleOptionsMethod;

opts.root = dir;
if (defaultExt && /^\./.test(defaultExt)) {
defaultExt = defaultExt.replace(/^\./, '');
}


// Support hashes and .types files in mimeTypes @since 0.8
if (opts.mimeTypes) {
Expand Down Expand Up @@ -197,7 +187,7 @@ module.exports = function createMiddleware(_dir, _options) {
file = path.normalize(
path.join(
root,
path.relative(path.join('/', baseDir), pathname)
path.relative(path.join('/', opts.baseDir), pathname)
)
);
// 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"
Expand All @@ -209,11 +199,11 @@ module.exports = function createMiddleware(_dir, _options) {
if ( file.endsWith('.br') ) brotliFile = file;
}

Object.keys(headers).forEach((key) => {
res.setHeader(key, headers[key]);
Object.keys(opts.headers).forEach((key) => {
res.setHeader(key, opts.headers[key]);
});

if (req.method === 'OPTIONS' && handleOptionsMethod) {
if (req.method === 'OPTIONS' && opts.handleOptionsMethod) {
res.end();
return;
}
Expand All @@ -238,8 +228,8 @@ module.exports = function createMiddleware(_dir, _options) {
let contentType = mime.lookup(file, defaultType);
const range = (req.headers && req.headers.range);
const lastModified = (new Date(stat.mtime)).toUTCString();
const etag = generateEtag(stat, weakEtags);
let cacheControl = cache;
const etag = generateEtag(stat, opts.weakEtags);
let cacheControl = opts.cache;
let stream = null;
if (contentType && isTextFile(contentType)) {
if (stat.size < buffer.constants.MAX_LENGTH) {
Expand All @@ -266,7 +256,7 @@ module.exports = function createMiddleware(_dir, _options) {
}

if (typeof cacheControl === 'function') {
cacheControl = cache(pathname);
cacheControl = opts.cache(pathname);
}
if (typeof cacheControl === 'number') {
cacheControl = `max-age=${cacheControl}`;
Expand Down Expand Up @@ -374,11 +364,11 @@ module.exports = function createMiddleware(_dir, _options) {
// This means we're already trying ./404.html and can not find it.
// So send plain text response with 404 status code
status[404](res, next);
} else if (!path.extname(parsed.pathname).length && defaultExt) {
} else if (!path.extname(parsed.pathname).length && opts.defaultExt) {
// If there is no file extension in the path and we have a default
// extension try filename and default extension combination before rendering 404.html.
middleware({
url: `${parsed.pathname}.${defaultExt}${(parsed.search) ? parsed.search : ''}`,
url: `${parsed.pathname}.${opts.defaultExt}${(parsed.search) ? parsed.search : ''}`,
headers: req.headers,
}, res, next);
} else if (opts.showDir && opts.dirOverrides404) {
Expand All @@ -388,7 +378,7 @@ module.exports = function createMiddleware(_dir, _options) {
return;
} else {
// Try to serve default ./404.html
const rawUrl = (handleError ? `/${path.join(baseDir, `404.${defaultExt}`)}` : req.url);
const rawUrl = (opts.handleError ? `/${path.join(opts.baseDir, `404.${opts.defaultExt}`)}` : req.url);
const encodedUrl = ensureUriEncoded(rawUrl);
middleware({
url: encodedUrl,
Expand All @@ -399,7 +389,7 @@ module.exports = function createMiddleware(_dir, _options) {
} else if (err) {
status[500](res, next, { error: err });
} else if (stat.isDirectory()) {
if (!autoIndex && !opts.showDir) {
if (!opts.autoIndex && !opts.showDir) {
status[404](res, next);
return;
}
Expand All @@ -417,11 +407,11 @@ module.exports = function createMiddleware(_dir, _options) {
return;
}

if (autoIndex) {
if (opts.autoIndex) {
middleware({
url: urlJoin(
encodeURIComponent(pathname),
`/index.${defaultExt}`
`/index.${opts.defaultExt}`
),
headers: req.headers,
}, res, (autoIndexError) => {
Expand Down
Loading
Loading