Skip to content

Commit 2a66c95

Browse files
committed
fix callback stack
1 parent fde16d1 commit 2a66c95

File tree

4 files changed

+56
-37
lines changed

4 files changed

+56
-37
lines changed

build/router.js

Lines changed: 28 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/server.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ var interceptorOne = function (done, next) {
66

77
next(function (name, args) {
88
console.log(2, name, args);
9+
10+
next();
911
});
1012

1113
console.log(this.request.agent);
@@ -19,6 +21,8 @@ http.on('/', function (done) {
1921
next(function (name, args) {
2022
console.log(1, name, args);
2123
args[0] = 'changed ' + args[0];
24+
25+
next();
2226
});
2327
});
2428

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-tiny-http",
3-
"version": "1.2.10",
3+
"version": "1.2.11",
44
"description": "A tiny node.js http framework",
55
"main": "build/http.js",
66
"scripts": {

src/router.coffee

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,35 +80,43 @@ handler = (result, options) ->
8080
context = { request, response }
8181
callbacks = []
8282
returned = no
83-
83+
index = -1
84+
resultArgs = null
85+
next = null
86+
8487
done = (name, args...) ->
8588
return if returned
8689
returned = yes
90+
index = callbacks.length
8791
name = 'blank' if not result[name]?
88-
89-
for callback in callbacks by -1
90-
callback.call context, name, args
91-
92-
result[name].apply null, args
93-
.call null, request, response
94-
response.respond() if not response.responded
92+
resultArgs = [name, args]
93+
94+
next()
9595

9696
for pattern, def of routes
9797
[tester, functions] = def.get()
9898
params = {}
99-
index = -1
10099

101100
# deny not matched
102101
continue if not tester request.method, request.path, params
103102
request.set params
104103

105104
do next = (callback = null) ->
106-
return if returned
107-
108-
callbacks.push callback if callback?
109-
index += 1
110-
fn = if index >= defaults.length then functions[index - defaults.length] else defaults[index]
111-
fn.call context, done, next if fn?
105+
if returned
106+
index -= 1
107+
108+
if index >= 0
109+
callbacks[index].apply context, resultArgs
110+
else
111+
[name, args] = resultArgs
112+
result[name].apply null, args
113+
.call null, request, response
114+
response.respond() if not response.responded
115+
else
116+
callbacks.push callback if callback?
117+
index += 1
118+
fn = if index >= defaults.length then functions[index - defaults.length] else defaults[index]
119+
fn.call context, done, next if fn?
112120

113121
return
114122

0 commit comments

Comments
 (0)