Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Simplify file finding with promises

  • Loading branch information...
commit f5c7edfece8fb999017dd792542d0fc226ab8768 1 parent 942fa89
@mjackson authored
Showing with 44 additions and 60 deletions.
  1. +43 −60 lib/file.js
  2. +1 −0  package.json
View
103 lib/file.js
@@ -1,6 +1,7 @@
var path = require('path');
var fs = require('fs');
var mime = require('mime');
+var when = require('when');
var strata = require('./index');
var utils = require('./utils');
module.exports = fileMiddleware;
@@ -34,84 +35,66 @@ function fileMiddleware(app, root, index) {
return function (env, callback) {
if (env.requestMethod !== 'GET') {
- app(env, callback);
- return;
+ return app(env, callback);
}
- var pathInfo = unescape(env.pathInfo);
-
+ var pathInfo = env.pathInfo;
if (pathInfo.indexOf('..') !== -1) {
- utils.forbidden(env, callback);
- return;
+ return utils.forbidden(env, callback);
}
var fullPath = path.join(root, pathInfo);
- fs.exists(fullPath, function (exists) {
- if (!exists) {
- app(env, callback);
- return;
+ getStat(fullPath).then(function (stat) {
+ if (!stat) {
+ return app(env, callback);
}
- fs.stat(fullPath, function (err, stats) {
- if (err && strata.handleError(err, env, callback)) {
- return;
- }
-
- if (stats.isFile()) {
- sendFile(callback, fullPath, stats);
- } else if (stats.isDirectory() && index) {
- // If the request targets a directory check all index
- // files to see if we can serve any of them.
- var indexExists = {};
-
- function sendFirstExisting() {
- var found = false,
- indexPath;
-
- for (var i = 0, len = index.length; !found && i < len; ++i) {
- indexPath = path.join(fullPath, index[i]);
-
- if (indexExists[indexPath]) {
- fs.stat(indexPath, function (err, stats) {
- if (err && strata.handleError(err, env, callback)) {
- return;
- }
-
- sendFile(callback, indexPath, stats);
- });
-
- found = true;
- }
- }
+ if (stat.isFile()) {
+ return sendFile(callback, fullPath, stat);
+ }
- if (!found) {
- app(env, callback);
+ // If the request targets a directory check all index
+ // files to see if we can serve any of them.
+ if (stat.isDirectory() && index) {
+ var indexPaths = index.map(function (file) {
+ return path.join(fullPath, file);
+ });
+
+ return when.all(indexPaths.map(getStat)).then(function (stats) {
+ for (var i = 0, len = stats.length; i < len; ++i) {
+ if (stats[i]) {
+ return sendFile(callback, indexPaths[i], stats[i]);
}
}
- var checked = 0;
-
- for (var i = 0, len = index.length; i < len; ++i) {
- (function (indexPath) {
- fs.exists(indexPath, function (exists) {
- indexExists[indexPath] = exists;
- checked += 1;
-
- if (checked == len) {
- sendFirstExisting();
- }
- });
- })(path.join(fullPath, index[i]));
- }
- } else {
app(env, callback);
- }
- });
+ });
+ }
+
+ app(env, callback);
});
};
}
+var statFile = require('when/node/function').lift(fs.stat);
+
+// Attempt to get a stat for the given file. Return null if it
+// does not exist.
+function getStat(file) {
+ return statFile(file).then(null, function (error) {
+ if (isMissingFileError(error)) {
+ return null;
+ }
+
+ throw error;
+ });
+}
+
+function isMissingFileError(error) {
+ return error.code === 'ENOENT';
+}
+
function sendFile(callback, path, stats) {
callback(200, {
'Content-Type': mime.lookup(path),
View
1  package.json
@@ -13,6 +13,7 @@
"url": "git://github.com/mjijackson/strata.git"
},
"dependencies": {
+ "when": "2.0.x",
"mime": "1.2.3",
"strftime": "0.4.6",
"markdown": "~0.3.1"
Please sign in to comment.
Something went wrong with that request. Please try again.