Skip to content

Commit

Permalink
Don't store request specific data into the Layer object, should be im…
Browse files Browse the repository at this point in the history
…mutable
  • Loading branch information
Colin GILLE committed Feb 1, 2024
1 parent 2a00da2 commit beaddd1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 44 deletions.
41 changes: 12 additions & 29 deletions lib/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,20 +218,19 @@ proto.handle = function handle(req, res, out) {

// find next matching layer
var layer;
var match;
var match = false;
var route;

while (match !== true && idx < stack.length) {
while (match === false && idx < stack.length) {
layer = stack[idx++];
match = matchLayer(layer, path);
route = layer.route;

if (typeof match !== 'boolean') {
// hold on to layerError
layerError = layerError || match;
try {
match = layer.match(path);
} catch (err) {
layerError = layerError || err;
}
route = layer.route;

if (match !== true) {
if (match === false) {
continue;
}

Expand Down Expand Up @@ -261,7 +260,7 @@ proto.handle = function handle(req, res, out) {
}

// no match
if (match !== true) {
if (match === false) {
return done(layerError);
}

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

// Capture one-time layer values
req.params = self.mergeParams
? mergeParams(layer.params, parentParams)
: layer.params;
var layerPath = layer.path;
? mergeParams(match.params, parentParams)
: match.params;
var layerPath = match.path;

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

/**
* Match path to a layer.
*
* @param {Layer} layer
* @param {string} path
* @private
*/

function matchLayer(layer, path) {
try {
return layer.match(path);
} catch (err) {
return err;
}
}

// merge params with parent params
function mergeParams(params, parent) {
if (typeof parent !== 'object' || !parent) {
Expand Down
28 changes: 13 additions & 15 deletions lib/router/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ function Layer(path, options, fn) {

this.handle = fn;
this.name = fn.name || '<anonymous>';
this.params = undefined;
this.path = undefined;
this.regexp = pathRegexp(path, this.keys = [], opts);

// set fast path flags
Expand Down Expand Up @@ -113,34 +111,31 @@ Layer.prototype.match = function match(path) {
if (path != null) {
// fast path non-ending match for / (any path matches)
if (this.regexp.fast_slash) {
this.params = {}
this.path = ''
return true
return {
path: '',
params: {}
}
}

// fast path for * (everything matched in a param)
if (this.regexp.fast_star) {
this.params = {'0': decode_param(path)}
this.path = path
return true
return {
path: path,
params: {'0': decode_param(path)}
}
}

// match the path
match = this.regexp.exec(path)
}

if (!match) {
this.params = undefined;
this.path = undefined;
return false;
}

// store values
this.params = {};
this.path = match[0]

var keys = this.keys;
var params = this.params;
var params = {};

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

return true;
return {
path: match[0],
params: params
};
};

/**
Expand Down

0 comments on commit beaddd1

Please sign in to comment.