Skip to content

Commit 7ec5dd2

Browse files
committedMay 20, 2022
Fix regression routing a large stack in a single route
fixes #4913
1 parent ab2c70b commit 7ec5dd2

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed
 

‎History.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
unreleased
2+
==========
3+
4+
* Fix regression routing a large stack in a single route
5+
16
4.18.1 / 2022-04-29
27
===================
38

‎lib/router/route.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -124,21 +124,21 @@ Route.prototype.dispatch = function dispatch(req, res, done) {
124124
return done(err)
125125
}
126126

127-
var layer = stack[idx++];
128-
if (!layer) {
129-
return done(err);
130-
}
131-
132127
// max sync stack
133128
if (++sync > 100) {
134129
return setImmediate(next, err)
135130
}
136131

137-
if (layer.method && layer.method !== method) {
138-
return next(err);
132+
var layer = stack[idx++]
133+
134+
// end of layers
135+
if (!layer) {
136+
return done(err)
139137
}
140138

141-
if (err) {
139+
if (layer.method && layer.method !== method) {
140+
next(err)
141+
} else if (err) {
142142
layer.handle_error(err, req, res, next);
143143
} else {
144144
layer.handle_request(req, res, next);

‎test/Route.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,16 @@ describe('Route', function(){
1919
var req = { method: 'GET', url: '/' }
2020
var route = new Route('/foo')
2121

22+
route.get(function (req, res, next) {
23+
req.counter = 0
24+
next()
25+
})
26+
2227
for (var i = 0; i < 6000; i++) {
23-
route.all(function (req, res, next) { next() })
28+
route.all(function (req, res, next) {
29+
req.counter++
30+
next()
31+
})
2432
}
2533

2634
route.get(function (req, res, next) {
@@ -31,6 +39,7 @@ describe('Route', function(){
3139
route.dispatch(req, {}, function (err) {
3240
if (err) return done(err)
3341
assert.ok(req.called)
42+
assert.strictEqual(req.counter, 6000)
3443
done()
3544
})
3645
})

0 commit comments

Comments
 (0)
Please sign in to comment.