Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
db95e7b
Resolve paths for views asynchronously
aredridel May 18, 2015
3dd0d30
Add limitStat to keep concurrent requests under control
aredridel May 18, 2015
0594ee6
fix(view): prevent limitStat queue stall when a callback throws
bjohansebas Jul 13, 2026
f5ad5a3
fix(view): treat stat errors as lookup misses like the sync implement…
bjohansebas Jul 13, 2026
48b6588
refactor(view): resolve the view path lazily in View#render
bjohansebas Jul 13, 2026
4786b4e
fix(view): deliver sync engine throws via callback on first render
bjohansebas Jul 13, 2026
d08d353
docs!
bjohansebas Jul 13, 2026
82742aa
refactor(view): drop dead error plumbing and derive the lookup file name
bjohansebas Jul 13, 2026
449845d
perf(view): coalesce concurrent lookups of the same view
bjohansebas Jul 13, 2026
9de5eb5
add test
bjohansebas Jul 13, 2026
8462786
fix(view): reset pendingRenders when lookup throws synchronously
bjohansebas Jul 13, 2026
de6eb66
fix(view): survive synchronous throws from fs.stat and skip falsy vie…
bjohansebas Jul 13, 2026
b250f32
fix(view): guarantee the render callback is invoked at most once
bjohansebas Jul 13, 2026
b367cdf
fix(view): handle synchronous stat failures asynchronously to prevent…
bjohansebas Jul 13, 2026
29a2d83
test: restore fs.stat via afterEach and simplify falsy-root check
bjohansebas Jul 13, 2026
df777dc
test: assert sync-parity error messages for misconfigured views
bjohansebas Jul 13, 2026
97ba3e4
fix(app): keep requiring path from custom view classes
bjohansebas Jul 13, 2026
38278a6
fix(app): drop views with failed lookups from the cache like the sync…
bjohansebas Jul 13, 2026
4859afa
fix(view): clarify path resolution behavior for custom view subclasses
bjohansebas Jul 13, 2026
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
29 changes: 17 additions & 12 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,25 +554,30 @@ app.render = function render(name, options, callback) {
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 done(err);
}
if (!view.path) {
view.lookupMain(function (err) {
if (err) return done(err);

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

// render
tryRender(view, renderOptions, done);
});
} else {
// prime the cache
if (renderOptions.cache) {
cache[name] = view;
}
}

// render
tryRender(view, renderOptions, done);
};
// render
tryRender(view, renderOptions, done);
}
}

/**
* Listen for connections.
Expand Down
138 changes: 97 additions & 41 deletions lib/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,11 @@ function View(name, options) {
throw new Error('No default engine was specified and no extension was provided.');
}

var fileName = name;

if (!this.ext) {
// get extension from default engine name
this.ext = this.defaultEngine[0] !== '.'
? '.' + this.defaultEngine
: this.defaultEngine;

fileName += this.ext;
}

if (!opts.engines[this.ext]) {
Expand All @@ -89,37 +85,50 @@ function View(name, options) {

// store loaded engine
this.engine = opts.engines[this.ext];

// lookup path
this.path = this.lookup(fileName);
}

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

View.prototype.lookup = function lookup(name) {
var path;
View.prototype.lookup = function lookup(name, 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 = this.ext;

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

// 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 @@ -135,6 +144,8 @@ View.prototype.render = function render(options, callback) {

debug('render "%s"', this.path);

if (!this.path) return fn(new Error("View has not been fully initialized yet"));

// render, normalizing sync callbacks
this.engine(this.path, options, function onRender() {
if (!sync) {
Expand All @@ -158,48 +169,93 @@ View.prototype.render = function render(options, callback) {
sync = false;
};

/** Resolve the main template for this view
*
* @param {function} cb
* @private
*/
View.prototype.lookupMain = function lookupMain(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, 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;

// <path>.<ext>
function resolveView(dir, file, ext, cb) {
var path = join(dir, file);
var stat = tryStat(path);

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

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

if (stat && stat.isFile()) {
return 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--;
}
}
}
}