From 8e87e27aa75e085fe6808b4a8bb137fc18833d0f Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Tue, 6 Mar 2018 12:49:47 -0500 Subject: [PATCH 1/2] 3.6.7 --- HISTORY.md | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 0253ee2f5..86166e0b2 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,5 +1,5 @@ -unreleased -========== +3.6.7 / 2018-03-06 +================== * deps: finalhandler@1.1.1 - Fix 404 output for bad / missing pathnames diff --git a/package.json b/package.json index a61991e4b..ee73ae98c 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "connect", "description": "High performance middleware framework", - "version": "3.6.6", + "version": "3.6.7", "author": "TJ Holowaychuk (http://tjholowaychuk.com)", "contributors": [ "Douglas Christopher Wilson ", From faeee930ee2136b0e7c8a32a09ba88539e6c339e Mon Sep 17 00:00:00 2001 From: pixcai Date: Fri, 15 Jun 2018 16:56:57 +0800 Subject: [PATCH 2/2] invoke all listeners instead of just the first --- index.js | 21 +++++++++++++++++- test/server.js | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index dac98baf9..8b06db728 100644 --- a/index.js +++ b/index.js @@ -86,6 +86,7 @@ proto.use = function use(route, fn) { // wrap sub-apps if (typeof handle.handle === 'function') { var server = handle; + server.route = path; handle = function (req, res, next) { server.handle(req, res, next); @@ -94,7 +95,25 @@ proto.use = function use(route, fn) { // wrap vanilla http.Servers if (handle instanceof http.Server) { - handle = handle.listeners('request')[0]; + var server = handle; + + handle = function (req, res, next) { + var listeners = server.listeners('request'); + var error; + + try { + for (var i = 0; i < listeners.length; i++) { + if (!res.finished) { + listeners[i].call(server, req, res); + } + } + } catch (e) { + error = e; + } + if (!res.finished) { + next(error); + } + }; } // strip trailing slash diff --git a/test/server.js b/test/server.js index 5f8c80a2b..33074fc56 100644 --- a/test/server.js +++ b/test/server.js @@ -31,6 +31,25 @@ describe('app', function(){ .expect(200, 'hello, world!', done); }) + it('should invoke all request listeners', function (done) { + var app = connect(); + var router = http.createServer(); + + router.on('request', function (req, res) { + res.write('hello, '); + }); + router.on('request', function (req, res) { + res.end('world!'); + }); + app.use(router); + + var server = http.createServer(app); + + request(server) + .get('/') + .expect(200, 'hello, world!', done); + }) + it('should be a callable function', function(done){ var app = connect(); @@ -92,6 +111,25 @@ describe('app', function(){ .expect(200, 'oh, boom!', done); }) + it('should invoke callback on request listener error', function (done) { + var app = connect(); + var router = http.createServer(); + + router.on('request', function (req, res) { + throw new Error('boom!'); + }); + app.use(router); + app.use(function (err, req, res, next) { + res.end('oh, ' + err.message); + }); + + var server = http.createServer(app); + + request(server) + .get('/') + .expect(200, 'oh, boom!', done); + }) + it('should work as middleware', function(done){ // custom server handler array var handlers = [connect(), function(req, res, next){ @@ -214,6 +252,28 @@ describe('app', function(){ .expect(200, done); }) + it('should not fire after headers sent in request listener', function (done) { + var app = connect(); + var router = http.createServer(); + + router.on('request', function (req, res) { + res.end('hello'); + }); + router.on('request', function (req, res) { + res.write('world'); + }); + app.use(router); + app.use(function (req, res) { + res.end('body'); + }); + + var server = http.createServer(app); + + request(app) + .get('/') + .expect(200, 'hello', done); + }) + it('shoud have no body for HEAD', function(done){ var app = connect();