Skip to content
This repository has been archived by the owner on Mar 16, 2022. It is now read-only.

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
h2non committed Nov 29, 2016
1 parent 6bc4110 commit c798c7a
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
4 changes: 4 additions & 0 deletions examples/response-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,15 @@ http.createServer(function (req, res) {
res.end()
}).listen(3002)

// setTimeout(function() {}, 10000)
// return false

// Client test request
supertest('http://localhost:3000')
.post('/foo')
.send({'hello': 'world'})
.expect(200)
.end(function (err, res) {
console.error(err)
console.log('Transformed body:', res.body)
})
15 changes: 8 additions & 7 deletions lib/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,13 +277,14 @@ Base.prototype.useResponse = function () {
const stack = this.mw.stack('response')

if (!stack || stack.length < 2) {
this.use(middleware.responseBody(dispatch.bind(this)))
}

function dispatch (req, res, next) {
if (res._alreadyIntercepted) return next()
res._alreadyIntercepted = true
this.mw.run('response', req, res, next)
this.use(middleware.responseBody(function dispatch (req, res, next) {
// If body was already intercepted, just continue with it
if (res._alreadyIntercepted) return next()
// Flag the response as intercepted
res._alreadyIntercepted = true
// Run the response middleware
this.mw.run('response', req, res, next)
}.bind(this)))
}

this.useFor('response', arguments)
Expand Down
7 changes: 2 additions & 5 deletions lib/middleware/response-body.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@ module.exports = function responseBody (middleware, filter) {
var closed = false
var headArgs = null

// Forces buffering all the stream write() calls
res.connection.cork()

// Listen for client connection close
req.once('close', onClose)
req.socket.once('close', onClose)

// Wrap native http.ServerResponse methods
// to intercept data and handle the state
Expand Down Expand Up @@ -145,7 +142,7 @@ module.exports = function responseBody (middleware, filter) {

function cleanup () {
buf = length = headArgs = null
req.removeListener('close', onClose)
req.socket.removeListener('close', onClose)
}

next()
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"rocky-consul": "^0.1.0",
"sinon": "^1.15.3",
"standard": "^5.4.1",
"supertest": "^1.2",
"supertest": "^2",
"vhost": "^3.0.0",
"ws": "^0.8.0"
},
Expand Down
2 changes: 1 addition & 1 deletion test/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ suite('http', function () {
}
})

test('response middleware', function (done) {
test.only('response middleware', function (done) {
proxy = rocky().forward(targetUrl)
server = createTestServer()

Expand Down

0 comments on commit c798c7a

Please sign in to comment.