Skip to content

Commit b2e1baf

Browse files
committedMay 8, 2019
fix(sirv): handle extension-less files
- Closes #26
1 parent 2fb5bac commit b2e1baf

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed
 

‎packages/sirv/index.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ function toAssume(uri, extns) {
2323
}
2424

2525
function find(uri, extns) {
26-
if (!!~uri.lastIndexOf('.')) return FILES[uri];
2726
let i=0, data, arr=toAssume(uri, extns);
2827
for (; i < arr.length; i++) {
2928
if (data=FILES[arr[i]]) break;
@@ -80,12 +79,11 @@ module.exports = function (dir, opts={}) {
8079

8180
if (opts.dev) {
8281
return function (req, res, next) {
83-
let uri = decodeURIComponent(req.path || req.pathname || parser(req).pathname);
84-
let arr = uri.includes('.') ? [uri] : toAssume(uri, extensions);
85-
let file = arr.map(x => join(dir, x)).find(fs.existsSync);
86-
if (!file) return next ? next() : isNotFound(req, res);
87-
88-
let stats = fs.statSync(file);
82+
let stats, file, uri=decodeURIComponent(req.path || req.pathname || parser(req).pathname);
83+
let arr = [uri].concat(toAssume(uri, extensions)).map(x => join(dir, x)).filter(fs.existsSync);
84+
while (file = arr.shift()) {
85+
stats = fs.statSync(file);
86+
if (stats.isDirectory()) continue;
8987
setHeaders(res, uri, stats);
9088
send(req, res, file, stats, {
9189
'Content-Type': mime.getType(file),
@@ -94,6 +92,7 @@ module.exports = function (dir, opts={}) {
9492
});
9593
}
9694
}
95+
}
9796

9897
let cc = opts.maxAge != null && `public,max-age=${opts.maxAge}`;
9998
if (cc && opts.immutable) cc += ',immutable';
@@ -117,7 +116,7 @@ module.exports = function (dir, opts={}) {
117116

118117
return function (req, res, next) {
119118
let pathname = decodeURIComponent(req.path || req.pathname || parser(req).pathname);
120-
let data = find(pathname, extensions);
119+
let data = FILES[pathname] || find(pathname, extensions);
121120
if (!data) return next ? next() : isNotFound(req, res);
122121

123122
setHeaders(res, pathname, data.stats);

0 commit comments

Comments
 (0)
Please sign in to comment.