Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve paths for views asynchronously #2653

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
34 changes: 19 additions & 15 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,26 +547,30 @@ app.render = function(name, options, fn){
root: this.get('views'),
engines: engines
});
}

if (!view.path) {
var dirs = Array.isArray(view.root) && view.root.length > 1
? 'directories "' + view.root.slice(0, -1).join('", "') + '" or "' + view.root[view.root.length - 1] + '"'
: 'directory "' + view.root + '"'
var err = new Error('Failed to lookup view "' + name + '" in views ' + dirs);
err.view = view;
return fn(err);
}
if (!view.path) {
view.lookupMain(opts, function (err) {
if (err) return fn(err);

// prime the cache
if (opts.cache) cache[name] = view;
afterLookup();
});
} else {
afterLookup();
}

// render
try {
view.render(opts, fn);
} catch (err) {
fn(err);
function afterLookup() {
// prime the cache
if (opts.cache) cache[name] = view;
// render
try {
view.render(opts, fn);
} catch (err) {
fn(err);
}
}


};

/**
Expand Down
133 changes: 96 additions & 37 deletions lib/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,36 +48,50 @@ function View(name, options) {
if (!ext && !this.defaultEngine) throw new Error('No default engine was specified and no extension was provided.');
if (!ext) name += (ext = this.ext = ('.' != this.defaultEngine[0] ? '.' : '') + this.defaultEngine);
this.engine = engines[ext] || (engines[ext] = require(ext.slice(1)).__express);
this.path = this.lookup(name);
}

/**
* Lookup view by the given `name`
* Lookup view by the given `name` and `ext`
*
* @param {String} name
* @return {String}
* @param {String} ext
* @param {Function} cb
* @api private
*/

View.prototype.lookup = function lookup(name) {
var path;
View.prototype.lookup = function lookup(name, options, cb) {
var roots = [].concat(this.root);

debug('lookup "%s"', name);

for (var i = 0; i < roots.length && !path; i++) {
var root = roots[i];
var ext = path.extname(name);

function lookup(roots, callback) {
var root = roots.shift();
if (!root) {
return callback(null, null);
}
debug("looking up '%s' in '%s' with ext '%s'", name, root, ext);

// resolve the path
var loc = resolve(root, name);
var dir = dirname(loc);
var file = basename(loc);

// resolve the file
path = this.resolve(dir, file);
resolveView(dir, file, ext, function (err, resolved) {
if (err) {
return callback(err);
} else if (resolved) {
return callback(null, resolved);
} else {
return lookup(roots, callback);
}
});

}

return path;
return lookup(roots, cb);
};

/**
Expand All @@ -90,53 +104,98 @@ View.prototype.lookup = function lookup(name) {

View.prototype.render = function render(options, fn) {
debug('render "%s"', this.path);
if (!this.path) return fn(new Error("View has not been fully initialized yet"));
this.engine(this.path, options, fn);
};

/** Resolve the main template for this view
*
* @param {function} cb
* @private
*/
View.prototype.lookupMain = function lookupMain(opts, cb) {
if (this.path) return cb();
var view = this;
var name = path.extname(this.name) == this.ext
? this.name
: this.name + this.ext;
this.lookup(name, opts, function (err, path) {
if (err) {
return cb(err);
} else if (!path) {
var dirs = Array.isArray(view.root) && view.root.length > 1
? 'directories "' + view.root.slice(0, -1).join('", "') + '" or "' + view.root[view.root.length - 1] + '"'
: 'directory "' + view.root + '"'
var viewError = new Error('Failed to lookup view "' + view.name + '" in views ' + dirs);
viewError.view = view;
return cb(viewError);
} else {
view.path = path;
cb();
}
});
};

/**
* Resolve the file within the given directory.
*
* @param {string} dir
* @param {string} file
* @param {string} ext
* @param {function} cb
* @private
*/

View.prototype.resolve = function resolve(dir, file) {
var ext = this.ext;
function resolveView(dir, file, ext, cb) {
var path;
var stat;

// <path>.<ext>
path = join(dir, file);
stat = tryStat(path);

if (stat && stat.isFile()) {
return path;
}

// <path>/index.<ext>
path = join(dir, basename(file, ext), 'index' + ext);
stat = tryStat(path);

if (stat && stat.isFile()) {
return path;
}
};
limitStat(path, function (err, stat) {
if (err && err.code != 'ENOENT') {
return cb(err);
} else if (!err && stat && stat.isFile()) {
return cb(null, path);
}

// <path>/index.<ext>
path = join(dir, basename(file, ext), 'index' + ext);
limitStat(path, function (err, stat) {
if (err && err.code == 'ENOENT') {
return cb(null, null);
} else if (!err && stat && stat.isFile()) {
return cb(null, path);
} else {
return cb(err || new Error("error looking up '" + path + "'"));
}
});
});
}

var pendingStats = [];
var numPendingStats = 0;
/**
* Return a stat, maybe.
* an fs.stat call that limits the number of outstanding requests to 10.
*
* @param {string} path
* @return {fs.Stats}
* @private
* @param {String} path
* @param {Function} cb
*/
function limitStat(path, cb) {
if (++numPendingStats > 10) {
pendingStats.push([path, cb]);
} else {
fs.stat(path, cbAndDequeue(cb));
}

function tryStat(path) {
debug('stat "%s"', path);

try {
return fs.statSync(path);
} catch (e) {
return undefined;
function cbAndDequeue(cb) {
return function (err, stat) {
cb(err, stat);
var next = pendingStats.shift();
if (next) {
fs.stat(next[0], cbAndDequeue(next[1]));
} else {
numPendingStats--;
}
}
}
}