Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion lib/middleware_stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ MiddlewareStack.prototype.findByName = function (name) {
return this._stack[name];
};

/*
* Find the matching handler for a given URL through iteration (instead of recursion)
*/
MiddlewareStack.prototype.findByUrl = function dispatch (url, context) {
url = Url.normalize(url || '/');

for(var index=0; index < this._stack.length; index++) {
var handler = this._stack[index];
if (handler && handler.test(url, {method: context._method})) {
return index;
}
}

return 0;
};

/**
* Push a new handler onto the stack.
*/
Expand Down Expand Up @@ -183,7 +199,9 @@ MiddlewareStack.prototype.dispatch = function dispatch (url, context, done) {
}
};

var index = 0;
// For performance reason. it's faster to iterate and find a matching handler on the URL
// It is also a workaround for a bug on Firefox 56 - Ubuntu 16 where too many routes cause "Too many recursion"
var index = this.findByUrl(url, context);

var next = Meteor.bindEnvironment(function boundNext (err) {
var handler = self._stack[index++];
Expand Down
2 changes: 1 addition & 1 deletion package.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package.describe({
name: 'iron:middleware-stack',
summary: 'Client and server middleware support inspired by Connect.',
version: '1.1.0',
version: '1.1.1',
git: 'https://github.com/iron-meteor/iron-middleware-stack'
});

Expand Down