From f93bbe888fa0f72ab9509fb9df447d432326ab22 Mon Sep 17 00:00:00 2001 From: Thiago de Arruda Date: Tue, 28 Aug 2012 21:59:49 -0300 Subject: [PATCH] Now invoking 'response.end' will ensure any remaining handlers are canceled --- src/router.coffee | 8 +++++++- test/router.coffee | 15 ++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/router.coffee b/src/router.coffee index 58a9a58..7e9e65e 100644 --- a/src/router.coffee +++ b/src/router.coffee @@ -233,12 +233,18 @@ class Router for rule in ruleArray if extracted = rule.extractor.extract(p) req.params = extracted + end = res.end + status = + done = false + res.end = -> + status.done = true + end.call(res) handlerChain = rule.handlers handle = (i) -> if i == handlerChain.length - 1 n = next else - n = -> process.nextTick(-> handle(i + 1)) + n = -> process.nextTick(-> handle(i + 1)) if ! status.done current = handlerChain[i] current(req, res, n) handle(0) diff --git a/test/router.coffee b/test/router.coffee index dde205b..afc50e1 100644 --- a/test/router.coffee +++ b/test/router.coffee @@ -1,7 +1,7 @@ connect = require('connect') createRouter = require('../src/router') -describe 'Static rule matching', -> +describe 'Rules', -> router = createRouter() app = connect() app.use(router.route) @@ -21,6 +21,12 @@ describe 'Static rule matching', -> router.get '/^pattern/that/uses/many/handlers', (req, res) -> res.write('part3'); res.end() + router.get '/cancel', + (req, res, next) -> res.write('p1'); next(), + (req, res, next) -> res.write('p2'); res.end(), + (req, res, next) -> res.write('p3'); next(), + (req, res, next) -> res.write('p4'); res.end() + it 'should match simple patterns', (done) -> app.request() .get('/$imple/.get/pattern$') @@ -43,6 +49,13 @@ describe 'Static rule matching', -> res.body.should.eql('part1part2part3') done() + it 'should cancel pipeline when handler ends the request', (done) -> + app.request() + .get('/cancel') + .end (res) -> + res.body.should.eql('p1p2') + done() + describe 'Builtin string parser', -> router = createRouter()