view.js
This file
Path
lib/view.js
205 lines · 3.7 KiB · JavaScript
Last changed by
fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359
History of this fileTake it
The walk stopped after 200 commits: lines older than 2a0c35a point at it.
Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 1–10why, one step back1/*!2 * express3 * Copyright(c) 2009-2013 TJ Holowaychuk4 * Copyright(c) 2013 Roman Shtylman5 * Copyright(c) 2014-2015 Douglas Christopher Wilson6 * MIT Licensed7 */89'use strict';10Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 11–12why, one step back11/**12 * Module dependencies.Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 13why, one step back13 * @privateInvoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 14–15why, one step back14 */15Use path.resolve in view lookupDouglas Christopher Wilsonbefore adoption0e5f2f8
line 16why, one step back16var debug = require('debug')('express:view');fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359
lines 17–18why, one step back17var path = require('node:path');18var fs = require('node:fs');Use path.resolve in view lookupDouglas Christopher Wilsonbefore adoption0e5f2f8
lines 19–24why, one step back1920/**21 * Module variables.22 * @private23 */24Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 25–28why, one step back25var dirname = path.dirname;26var basename = path.basename;27var extname = path.extname;28var join = path.join;Use path.resolve in view lookupDouglas Christopher Wilsonbefore adoption0e5f2f8
line 29why, one step back29var resolve = path.resolve;Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 30–31why, one step back3031/**Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 32–33why, one step back32 * Module exports.33 * @publicInvoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 34–46why, one step back34 */3536module.exports = View;3738/**39 * Initialize a new `View` with the given `name`.40 *41 * Options:42 *43 * - `defaultEngine` the default template engine name44 * - `engines` template engine require() cache45 * - `root` root path for view lookup46 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 47–49why, one step back47 * @param {string} name48 * @param {object} options49 * @publicInvoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 50–52why, one step back50 */5152function View(name, options) {Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 53–56why, one step back53 var opts = options || {};5455 this.defaultEngine = opts.defaultEngine;56 this.ext = extname(name);Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 57why, one step back57 this.name = name;Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 58–76why, one step back58 this.root = opts.root;5960 if (!this.ext && !this.defaultEngine) {61 throw new Error('No default engine was specified and no extension was provided.');62 }6364 var fileName = name;6566 if (!this.ext) {67 // get extension from default engine name68 this.ext = this.defaultEngine[0] !== '.'69 ? '.' + this.defaultEngine70 : this.defaultEngine;7172 fileName += this.ext;73 }7475 if (!opts.engines[this.ext]) {76 // load engineMerge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 77why, one step back77 var mod = this.ext.slice(1)Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
line 78why, one step back78 debug('require "%s"', mod)Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 79–87why, one step back7980 // default engine export81 var fn = require(mod).__express8283 if (typeof fn !== 'function') {84 throw new Error('Module "' + mod + '" does not provide a view engine.')85 }8687 opts.engines[this.ext] = fnMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 88–94why, one step back88 }8990 // store loaded engine91 this.engine = opts.engines[this.ext];9293 // lookup path94 this.path = this.lookup(fileName);Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 95–97why, one step back95}9697/**Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
line 98why, one step back98 * Lookup view by the given `name`Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 99why, one step back99 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 100–101why, one step back100 * @param {string} name101 * @privateInvoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 102–103why, one step back102 */103Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 104–106why, one step back104View.prototype.lookup = function lookup(name) {105 var path;106 var roots = [].concat(this.root);Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 107why, one step back107Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
line 108why, one step back108 debug('lookup "%s"', name);Use path.resolve in view lookupDouglas Christopher Wilsonbefore adoption0e5f2f8
line 109why, one step back109Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 110–111why, one step back110 for (var i = 0; i < roots.length && !path; i++) {111 var root = roots[i];Use path.resolve in view lookupDouglas Christopher Wilsonbefore adoption0e5f2f8
line 112why, one step back112Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 113–116why, one step back113 // resolve the path114 var loc = resolve(root, name);115 var dir = dirname(loc);116 var file = basename(loc);Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 117why, one step back117Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 118–122why, one step back118 // resolve the file119 path = this.resolve(dir, file);120 }121122 return path;Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 123–125why, one step back123};124125/**Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 126why, one step back126 * Render with the given options.Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 127why, one step back127 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 128–130why, one step back128 * @param {object} options129 * @param {function} callback130 * @privateInvoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 131–132why, one step back131 */132Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 133why, one step back133View.prototype.render = function render(options, callback) {Make res.render callback is always async, even for sync view enginesDouglas Christopher Wilsonbefore adoption6343288
lines 134–135why, one step back134 var sync = true;135Use path.resolve in view lookupDouglas Christopher Wilsonbefore adoption0e5f2f8
line 136why, one step back136 debug('render "%s"', this.path);Make res.render callback is always async, even for sync view enginesDouglas Christopher Wilsonbefore adoption6343288
lines 137–158why, one step back137138 // render, normalizing sync callbacks139 this.engine(this.path, options, function onRender() {140 if (!sync) {141 return callback.apply(this, arguments);142 }143144 // copy arguments145 var args = new Array(arguments.length);146 var cntx = this;147148 for (var i = 0; i < arguments.length; i++) {149 args[i] = arguments[i];150 }151152 // force callback to be async153 return process.nextTick(function renderTick() {154 return callback.apply(cntx, args);155 });156 });157158 sync = false;Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 159why, one step back159};Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 160–172why, one step back160161/**162 * Resolve the file within the given directory.163 *164 * @param {string} dir165 * @param {string} file166 * @private167 */168169View.prototype.resolve = function resolve(dir, file) {170 var ext = this.ext;171172 // <path>.<ext>Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 173–174why, one step back173 var path = join(dir, file);174 var stat = tryStat(path);Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 175–205why, one step back175176 if (stat && stat.isFile()) {177 return path;178 }179180 // <path>/index.<ext>181 path = join(dir, basename(file, ext), 'index' + ext);182 stat = tryStat(path);183184 if (stat && stat.isFile()) {185 return path;186 }187};188189/**190 * Return a stat, maybe.191 *192 * @param {string} path193 * @return {fs.Stats}194 * @private195 */196197function tryStat(path) {198 debug('stat "%s"', path);199200 try {201 return fs.statSync(path);202 } catch (e) {203 return undefined;204 }205}