Skip to content
Myr

layer.js

This file

Path

lib/router/layer.js

166 lines · 2.8 KiB · JavaScript

Last changed by

Fix handling of invalid empty URLsDouglas Christopher Wilsonbefore adoption68290ee

History of this file

Take it

The walk stopped after 200 commits: lines older than 269dc53 point at it.

Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53

lines 1–8why, one step back
1/**
2 * Module dependencies.3 */45var pathRegexp = require('path-to-regexp');6var debug = require('debug')('express:router:layer');78/**

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 9–14why, one step back
9 * Module variables.
10 */1112var hasOwnProperty = Object.prototype.hasOwnProperty;1314/**

Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53

lines 15–26why, one step back
15 * Expose `Layer`.
16 */1718module.exports = Layer;1920function Layer(path, options, fn) {21 if (!(this instanceof Layer)) {22 return new Layer(path, options, fn);23 }2425 debug('new %s', path);26 options = options || {};

router: speed up standard app.use(fn)Douglas Christopher Wilsonbefore adoption8dc67af

line 27why, one step back
27

Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53

line 28why, one step back
28 this.handle = fn;

router: refactor location of try blocksDouglas Christopher Wilsonbefore adoption76e8bfa

line 29why, one step back
29 this.name = fn.name || '<anonymous>';

router: speed up standard app.use(fn)Douglas Christopher Wilsonbefore adoption8dc67af

lines 30–36why, one step back
30 this.params = undefined;
31 this.path = undefined;32 this.regexp = pathRegexp(path, this.keys = [], options);3334 if (path === '/' && options.end === false) {35 this.regexp.fast_slash = true;36 }

Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53

line 37why, one step back
37}

router: refactor location of try blocksDouglas Christopher Wilsonbefore adoption76e8bfa

lines 38–86why, one step back
38
39/**40 * Handle the error for the layer.41 *42 * @param {Error} error43 * @param {Request} req44 * @param {Response} res45 * @param {function} next46 * @api private47 */4849Layer.prototype.handle_error = function handle_error(error, req, res, next) {50 var fn = this.handle;5152 if (fn.length !== 4) {53 // not a standard error handler54 return next(error);55 }5657 try {58 fn(error, req, res, next);59 } catch (err) {60 next(err);61 }62};6364/**65 * Handle the request for the layer.66 *67 * @param {Request} req68 * @param {Response} res69 * @param {function} next70 * @api private71 */7273Layer.prototype.handle_request = function handle(req, res, next) {74 var fn = this.handle;7576 if (fn.length > 3) {77 // not a standard request handler78 return next();79 }8081 try {82 fn(req, res, next);83 } catch (err) {84 next(err);85 }86};

Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53

lines 87–96why, one step back
87
88/**89 * Check if this route matches `path`, if so90 * populate `.params`.91 *92 * @param {String} path93 * @return {Boolean}94 * @api private95 */96

router: speed up standard app.use(fn)Douglas Christopher Wilsonbefore adoption8dc67af

line 97why, one step back
97Layer.prototype.match = function match(path) {

Fix handling of invalid empty URLsDouglas Christopher Wilsonbefore adoption68290ee

lines 98–104why, one step back
98 if (path == null) {
99 // no path, nothing matches100 this.params = undefined;101 this.path = undefined;102 return false;103 }104

router: speed up standard app.use(fn)Douglas Christopher Wilsonbefore adoption8dc67af

lines 105–111why, one step back
105 if (this.regexp.fast_slash) {
106 // fast path non-ending match for / (everything matches)107 this.params = {};108 this.path = '';109 return true;110 }111

Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53

lines 112–113why, one step back
112 var m = this.regexp.exec(path);
113

router: speed up standard app.use(fn)Douglas Christopher Wilsonbefore adoption8dc67af

lines 114–118why, one step back
114 if (!m) {
115 this.params = undefined;116 this.path = undefined;117 return false;118 }

Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53

line 119why, one step back
119

router: speed up standard app.use(fn)Douglas Christopher Wilsonbefore adoption8dc67af

lines 120–121why, one step back
120 // store values
121 this.params = {};

Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53

line 122why, one step back
122 this.path = m[0];

router: speed up standard app.use(fn)Douglas Christopher Wilsonbefore adoption8dc67af

lines 123–125why, one step back
123
124 var keys = this.keys;125 var params = this.params;

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

line 126why, one step back
126 var prop;

router: speed up standard app.use(fn)Douglas Christopher Wilsonbefore adoption8dc67af

lines 127–129why, one step back
127 var n = 0;
128 var key;129 var val;

Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53

lines 130–132why, one step back
130
131 for (var i = 1, len = m.length; i < len; ++i) {132 key = keys[i - 1];

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 133–135why, one step back
133 prop = key
134 ? key.name135 : n++;

Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53

lines 136–137why, one step back
136 val = decode_param(m[i]);
137

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 138–139why, one step back
138 if (val !== undefined || !(hasOwnProperty.call(params, prop))) {
139 params[prop] = val;

Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53

lines 140–166why, one step back
140 }
141 }142143 return true;144};145146/**147 * Decode param value.148 *149 * @param {string} val150 * @return {string}151 * @api private152 */153154function decode_param(val){155 if (typeof val !== 'string') {156 return val;157 }158159 try {160 return decodeURIComponent(val);161 } catch (e) {162 var err = new TypeError("Failed to decode param '" + val + "'");163 err.status = 400;164 throw err;165 }166}