Skip to content

Commit beaddd1

Browse files
author
Colin GILLE
committed
Don't store request specific data into the Layer object, should be immutable
1 parent 2a00da2 commit beaddd1

File tree

2 files changed

+25
-44
lines changed

2 files changed

+25
-44
lines changed

lib/router/index.js

+12-29
Original file line numberDiff line numberDiff line change
@@ -218,20 +218,19 @@ proto.handle = function handle(req, res, out) {
218218

219219
// find next matching layer
220220
var layer;
221-
var match;
221+
var match = false;
222222
var route;
223223

224-
while (match !== true && idx < stack.length) {
224+
while (match === false && idx < stack.length) {
225225
layer = stack[idx++];
226-
match = matchLayer(layer, path);
227-
route = layer.route;
228-
229-
if (typeof match !== 'boolean') {
230-
// hold on to layerError
231-
layerError = layerError || match;
226+
try {
227+
match = layer.match(path);
228+
} catch (err) {
229+
layerError = layerError || err;
232230
}
231+
route = layer.route;
233232

234-
if (match !== true) {
233+
if (match === false) {
235234
continue;
236235
}
237236

@@ -261,7 +260,7 @@ proto.handle = function handle(req, res, out) {
261260
}
262261

263262
// no match
264-
if (match !== true) {
263+
if (match === false) {
265264
return done(layerError);
266265
}
267266

@@ -272,9 +271,9 @@ proto.handle = function handle(req, res, out) {
272271

273272
// Capture one-time layer values
274273
req.params = self.mergeParams
275-
? mergeParams(layer.params, parentParams)
276-
: layer.params;
277-
var layerPath = layer.path;
274+
? mergeParams(match.params, parentParams)
275+
: match.params;
276+
var layerPath = match.path;
278277

279278
// this should be done for the layer
280279
self.process_params(layer, paramcalled, req, res, function (err) {
@@ -572,22 +571,6 @@ function gettype(obj) {
572571
.replace(objectRegExp, '$1');
573572
}
574573

575-
/**
576-
* Match path to a layer.
577-
*
578-
* @param {Layer} layer
579-
* @param {string} path
580-
* @private
581-
*/
582-
583-
function matchLayer(layer, path) {
584-
try {
585-
return layer.match(path);
586-
} catch (err) {
587-
return err;
588-
}
589-
}
590-
591574
// merge params with parent params
592575
function mergeParams(params, parent) {
593576
if (typeof parent !== 'object' || !parent) {

lib/router/layer.js

+13-15
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ function Layer(path, options, fn) {
4040

4141
this.handle = fn;
4242
this.name = fn.name || '<anonymous>';
43-
this.params = undefined;
44-
this.path = undefined;
4543
this.regexp = pathRegexp(path, this.keys = [], opts);
4644

4745
// set fast path flags
@@ -113,34 +111,31 @@ Layer.prototype.match = function match(path) {
113111
if (path != null) {
114112
// fast path non-ending match for / (any path matches)
115113
if (this.regexp.fast_slash) {
116-
this.params = {}
117-
this.path = ''
118-
return true
114+
return {
115+
path: '',
116+
params: {}
117+
}
119118
}
120119

121120
// fast path for * (everything matched in a param)
122121
if (this.regexp.fast_star) {
123-
this.params = {'0': decode_param(path)}
124-
this.path = path
125-
return true
122+
return {
123+
path: path,
124+
params: {'0': decode_param(path)}
125+
}
126126
}
127127

128128
// match the path
129129
match = this.regexp.exec(path)
130130
}
131131

132132
if (!match) {
133-
this.params = undefined;
134-
this.path = undefined;
135133
return false;
136134
}
137135

138136
// store values
139-
this.params = {};
140-
this.path = match[0]
141-
142137
var keys = this.keys;
143-
var params = this.params;
138+
var params = {};
144139

145140
for (var i = 1; i < match.length; i++) {
146141
var key = keys[i - 1];
@@ -152,7 +147,10 @@ Layer.prototype.match = function match(path) {
152147
}
153148
}
154149

155-
return true;
150+
return {
151+
path: match[0],
152+
params: params
153+
};
156154
};
157155

158156
/**

0 commit comments

Comments
 (0)