Skip to content

Commit

Permalink
Fix regression routing a large stack in a single route
Browse files Browse the repository at this point in the history
fixes #4913
  • Loading branch information
dougwilson committed May 20, 2022
1 parent ab2c70b commit 7ec5dd2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
5 changes: 5 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
unreleased
==========

* Fix regression routing a large stack in a single route

4.18.1 / 2022-04-29
===================

Expand Down
16 changes: 8 additions & 8 deletions lib/router/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,21 +124,21 @@ Route.prototype.dispatch = function dispatch(req, res, done) {
return done(err)
}

var layer = stack[idx++];
if (!layer) {
return done(err);
}

// max sync stack
if (++sync > 100) {
return setImmediate(next, err)
}

if (layer.method && layer.method !== method) {
return next(err);
var layer = stack[idx++]

// end of layers
if (!layer) {
return done(err)
}

if (err) {
if (layer.method && layer.method !== method) {
next(err)
} else if (err) {
layer.handle_error(err, req, res, next);
} else {
layer.handle_request(req, res, next);
Expand Down
11 changes: 10 additions & 1 deletion test/Route.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,16 @@ describe('Route', function(){
var req = { method: 'GET', url: '/' }
var route = new Route('/foo')

route.get(function (req, res, next) {
req.counter = 0
next()
})

for (var i = 0; i < 6000; i++) {
route.all(function (req, res, next) { next() })
route.all(function (req, res, next) {
req.counter++
next()
})
}

route.get(function (req, res, next) {
Expand All @@ -31,6 +39,7 @@ describe('Route', function(){
route.dispatch(req, {}, function (err) {
if (err) return done(err)
assert.ok(req.called)
assert.strictEqual(req.counter, 6000)
done()
})
})
Expand Down

0 comments on commit 7ec5dd2

Please sign in to comment.