-
-
Notifications
You must be signed in to change notification settings - Fork 242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Do not swallow calls to res.end()
#188
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ var Buffer = require('safe-buffer').Buffer | |
var bytes = require('bytes') | ||
var compressible = require('compressible') | ||
var debug = require('debug')('compression') | ||
var onFinished = require('on-finished') | ||
var onHeaders = require('on-headers') | ||
var vary = require('vary') | ||
var zlib = require('zlib') | ||
|
@@ -66,6 +67,14 @@ function compression (options) { | |
var _on = res.on | ||
var _write = res.write | ||
|
||
var endCalled = false | ||
function endOnce () { | ||
if (!endCalled) { | ||
endCalled = true | ||
_end.apply(this, arguments) | ||
} | ||
} | ||
|
||
// flush | ||
res.flush = function flush () { | ||
if (stream) { | ||
|
@@ -104,12 +113,16 @@ function compression (options) { | |
} | ||
|
||
if (!stream) { | ||
return _end.call(this, chunk, encoding) | ||
return endOnce.call(this, chunk, encoding) | ||
} | ||
|
||
// mark ended | ||
ended = true | ||
|
||
if (onFinished.isFinished(this)) { | ||
return endOnce.call(this) | ||
} | ||
|
||
// write Buffer for Node.js 0.8 | ||
return chunk | ||
? stream.end(toBuffer(chunk, encoding)) | ||
|
@@ -209,12 +222,18 @@ function compression (options) { | |
}) | ||
|
||
stream.on('end', function onStreamEnd () { | ||
_end.call(res) | ||
endOnce.call(res) | ||
}) | ||
|
||
_on.call(res, 'drain', function onResponseDrain () { | ||
stream.resume() | ||
}) | ||
|
||
onFinished(res, function onFinished () { | ||
if (ended) { | ||
endOnce.call(res) | ||
} | ||
}) | ||
Comment on lines
+232
to
+236
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This addition takes care of situations where a response becomes "finished" soon after |
||
}) | ||
|
||
next() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ var Buffer = require('safe-buffer').Buffer | |
var bytes = require('bytes') | ||
var crypto = require('crypto') | ||
var http = require('http') | ||
var net = require('net') | ||
var request = require('supertest') | ||
var zlib = require('zlib') | ||
|
||
|
@@ -657,6 +658,113 @@ describe('compression()', function () { | |
.end() | ||
}) | ||
}) | ||
|
||
describe('when the client closes the connection before consuming the response', function () { | ||
it('should call the original res.end() if connection is cut early on', function (done) { | ||
var server = http.createServer(function (req, res) { | ||
var originalResEnd = res.end | ||
var originalResEndCalledTimes = 0 | ||
res.end = function () { | ||
originalResEndCalledTimes++ | ||
return originalResEnd.apply(this, arguments) | ||
} | ||
|
||
compression({ threshold: 0 })(req, res, function () { | ||
socket.end() | ||
|
||
res.setHeader('Content-Type', 'text/plain') | ||
res.write('hello, ') | ||
setTimeout(function () { | ||
res.end('world!') | ||
|
||
setTimeout(function () { | ||
server.close(function () { | ||
if (originalResEndCalledTimes === 1) { | ||
done() | ||
} else { | ||
done(new Error('The original res.end() was called ' + originalResEndCalledTimes + ' times')) | ||
} | ||
}) | ||
}, 5) | ||
Comment on lines
+680
to
+688
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Waiting for the condition to assert could be extracted into a generic helper, and it could also poll a couple of times if that is desired. I left this code in its naked form for now, and it is also duplicated in the other added test cases. I'm happy to improve this further as needed. |
||
}, 5) | ||
}) | ||
}) | ||
|
||
server.listen() | ||
|
||
var port = server.address().port | ||
var socket = openSocketWithRequest(port) | ||
}) | ||
|
||
it('should call the original res.end() if connection is cut after an initial write', function (done) { | ||
var server = http.createServer(function (req, res) { | ||
var originalResEnd = res.end | ||
var originalResEndCalledTimes = 0 | ||
res.end = function () { | ||
originalResEndCalledTimes++ | ||
return originalResEnd.apply(this, arguments) | ||
} | ||
|
||
compression({ threshold: 0 })(req, res, function () { | ||
res.setHeader('Content-Type', 'text/plain') | ||
res.write('hello, ') | ||
socket.end() | ||
|
||
setTimeout(function () { | ||
res.end('world!') | ||
|
||
setTimeout(function () { | ||
server.close(function () { | ||
if (originalResEndCalledTimes === 1) { | ||
done() | ||
} else { | ||
done(new Error('The original res.end() was called ' + originalResEndCalledTimes + ' times')) | ||
} | ||
}) | ||
}, 5) | ||
}, 5) | ||
}) | ||
}) | ||
|
||
server.listen() | ||
|
||
var port = server.address().port | ||
var socket = openSocketWithRequest(port) | ||
}) | ||
|
||
it('should call the original res.end() if connection is cut just after response body was generated', function (done) { | ||
var server = http.createServer(function (req, res) { | ||
var originalResEnd = res.end | ||
var originalResEndCalledTimes = 0 | ||
res.end = function () { | ||
originalResEndCalledTimes++ | ||
return originalResEnd.apply(this, arguments) | ||
} | ||
|
||
compression({ threshold: 0 })(req, res, function () { | ||
res.setHeader('Content-Type', 'text/plain') | ||
res.write('hello, ') | ||
res.end('world!') | ||
socket.end() | ||
|
||
setTimeout(function () { | ||
server.close(function () { | ||
if (originalResEndCalledTimes === 1) { | ||
done() | ||
} else { | ||
done(new Error('The original res.end() was called ' + originalResEndCalledTimes + ' times')) | ||
} | ||
}) | ||
}, 5) | ||
}) | ||
}) | ||
|
||
server.listen() | ||
|
||
var port = server.address().port | ||
var socket = openSocketWithRequest(port) | ||
}) | ||
}) | ||
}) | ||
|
||
function createServer (opts, fn) { | ||
|
@@ -716,3 +824,16 @@ function unchunk (encoding, onchunk, onend) { | |
stream.on('end', onend) | ||
} | ||
} | ||
|
||
function openSocketWithRequest (port) { | ||
var socket = net.connect(port, function onConnect () { | ||
socket.write('GET / HTTP/1.1\r\n') | ||
socket.write('Accept-Encoding: gzip\r\n') | ||
socket.write('Host: localhost:' + port + '\r\n') | ||
socket.write('Content-Type: text/plain\r\n') | ||
socket.write('Content-Length: 0\r\n') | ||
socket.write('Connection: keep-alive\r\n') | ||
socket.write('\r\n') | ||
}) | ||
return socket | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This addition takes care of situations where a route handler calls
res.end()
after the response is considered "finished". This is often due to the client having moved on before the response is ready for them.