router: speed up standard app.use(fn)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent 996d319 · Browse files · 4 files·+115 added−30 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+115 added−30 removed
- other:+115 added−30 removed
Provenance
- Commit
8dc67af- Tree
4d7521b- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
996d319
4 files changed+115 added−30 removed
modified:lib/router/index.js+24 added-22 removed
147 unmodified lines148 return res.set('Allow', body).send(body);149 });150 }151 152+ // setup basic req values153+ req.baseUrl = parentUrl;154+ req.originalUrl = req.originalUrl || req.url;155+156 next();157 158 function next(err) {159 if (err === 'route') {2 unmodified lines162 163 var layer = stack[idx++];164 var layerPath;165 162- if (!layer) {163- return done(err);164- }165-166 if (slashAdded) {167 req.url = req.url.substr(1);168 slashAdded = false;169 }170 171- req.baseUrl = parentUrl;172- req.url = protohost + removed + req.url.substr(protohost.length);173- req.originalUrl = req.originalUrl || req.url;174- removed = '';171+ if (removed.length !== 0) {172+ req.baseUrl = parentUrl;173+ req.url = protohost + removed + req.url.substr(protohost.length);174+ removed = '';175+ }176 177+ if (!layer) {178+ return done(err);179+ }180+181 try {182 var path = parseUrl(req).pathname;178- if (undefined == path) path = '/';183 184 if (!layer.match(path)) return next(err);185 186 // route object and not middleware51 unmodified lines238 if (c && '/' != c && '.' != c) return next(err);239 240 // Trim off the part of the url that matches the route241 // middleware (.use stuff) needs to have the path stripped238- removed = layerPath;239- if (removed.length) {242+ if (layerPath.length !== 0) {243 debug('trim prefix (%s) from url %s', layerPath, req.url);244+ removed = layerPath;245 req.url = protohost + req.url.substr(protohost.length + removed.length);242- }246 244- // Ensure leading slash245- if (!fqdn && req.url[0] !== '/') {246- req.url = '/' + req.url;247- slashAdded = true;248- }247+ // Ensure leading slash248+ if (!fqdn && req.url[0] !== '/') {249+ req.url = '/' + req.url;250+ slashAdded = true;251+ }252 250- // Setup base URL (no trailing slash)251- if (removed.length && removed.substr(-1) === '/') {252- req.baseUrl = parentUrl + removed.substring(0, removed.length - 1);253- } else {254- req.baseUrl = parentUrl + removed;253+ // Setup base URL (no trailing slash)254+ req.baseUrl = parentUrl + (removed[removed.length - 1] === '/'255+ ? removed.substring(0, removed.length - 1)256+ : removed);257 }258 259 debug('%s %s : %s', layer.handle.name || 'anonymous', layerPath, req.originalUrl);260 var arity = layer.handle.length;261 unmodified linesmodified:lib/router/layer.js+29 added-8 removed
16 unmodified lines1718192021212223242526272829303132333 unmodified lines373839403435364142434445464748493839405042515253545556575859606162636465666768697031 unmodified lines16 unmodified lines }
debug('new %s', path); options = options || {}; this.regexp = pathRegexp(path, this.keys = [], options);
this.handle = fn; this.params = undefined; this.path = undefined; this.regexp = pathRegexp(path, this.keys = [], options);
if (path === '/' && options.end === false) { this.regexp.fast_slash = true; }}
/** * Check if this route matches `path`, if so3 unmodified lines * @return {Boolean} * @api private */
Layer.prototype.match = function(path){ var keys = this.keys; var params = this.params = {};Layer.prototype.match = function match(path) { if (this.regexp.fast_slash) { // fast path non-ending match for / (everything matches) this.params = {}; this.path = ''; return true; }
var m = this.regexp.exec(path); var n = 0; var key; var val;
if (!m) return false; if (!m) { this.params = undefined; this.path = undefined; return false; }
// store values this.params = {}; this.path = m[0];
var keys = this.keys; var params = this.params; var n = 0; var key; var val;
for (var i = 1, len = m.length; i < len; ++i) { key = keys[i - 1]; val = decode_param(m[i]);
31 unmodified linesadded:lib/router/match.js+61 added
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061/** * Module dependencies. */
var pathRegexp = require('path-to-regexp');
/** * Expose `Layer`. */
module.exports = Match;
function Match(layer, path, params) { this.layer = layer; this.params = {}; this.path = path || '';
if (!params) { return this; }
var keys = layer.keys; var n = 0; var prop; var key; var val;
for (var i = 0; i < params.length; i++) { key = keys[i]; val = decode_param(params[i]); prop = key ? key.name : n++;
this.params[prop] = val; }
return this;};
/** * Decode param value. * * @param {string} val * @return {string} * @api private */
function decode_param(val){ if (typeof val !== 'string') { return val; }
try { return decodeURIComponent(val); } catch (e) { var err = new TypeError("Failed to decode param '" + val + "'"); err.status = 400; throw err; }}modified:History.md+1 added
8 unmodified lines9 * support non-string `path` in `app.use(path, fn)`10 - supports array of paths11 - supports `RegExp`12 * router: fix optimization on router exit13+ * router: speed up standard `app.use(fn)`14 * deps: debug@1.0.315 - Add support for multiple wildcards in namespaces16 * deps: methods@1.1.017 - add `CONNECT`1775 unmodified lines