Skip to content

Commit 7ecac7c

Browse files
committed
Fix return value from .end and .write after end
1 parent f351090 commit 7ecac7c

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

HISTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
unreleased
22
==========
33

4+
* Fix return value from `.end` and `.write` after end
45
* Improve detection of zero-length body without `Content-Length`
56
* deps: accepts@~1.2.9
67
- deps: mime-types@~2.1.1

index.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,24 @@ function compression(options) {
4949
}
5050

5151
return function compression(req, res, next){
52+
var ended = false
5253
var length
5354
var listeners = []
5455
var write = res.write
5556
var on = res.on
5657
var end = res.end
5758
var stream
5859

59-
// see #8
60-
req.on('close', function(){
61-
res.write = res.end = noop
62-
});
63-
6460
// flush is noop by default
6561
res.flush = noop;
6662

6763
// proxy
6864

6965
res.write = function(chunk, encoding){
66+
if (ended) {
67+
return false
68+
}
69+
7070
if (!this._header) {
7171
this._implicitHeader()
7272
}
@@ -77,6 +77,10 @@ function compression(options) {
7777
};
7878

7979
res.end = function(chunk, encoding){
80+
if (ended) {
81+
return false
82+
}
83+
8084
if (!this._header) {
8185
// estimate the length
8286
if (!this.getHeader('Content-Length')) {
@@ -90,6 +94,9 @@ function compression(options) {
9094
return end.call(this, chunk, encoding)
9195
}
9296

97+
// mark ended
98+
ended = true
99+
93100
// write Buffer for Node.js 0.8
94101
return chunk
95102
? stream.end(new Buffer(chunk, encoding))

test/compression.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,20 @@ describe('compression()', function(){
434434
.set('Accept-Encoding', 'gzip')
435435
.expect('Content-Encoding', 'gzip', done)
436436
})
437+
438+
it('should return false writing after end', function (done) {
439+
var server = createServer({ threshold: 0 }, function (req, res) {
440+
res.setHeader('Content-Type', 'text/plain')
441+
res.end('hello, world')
442+
assert.ok(res.write() === false)
443+
assert.ok(res.end() === false)
444+
})
445+
446+
request(server)
447+
.get('/')
448+
.set('Accept-Encoding', 'gzip')
449+
.expect('Content-Encoding', 'gzip', done)
450+
})
437451
})
438452

439453
describe('when "Accept-Encoding: deflate"', function () {

0 commit comments

Comments
 (0)