Skip to content

Commit 3d64772

Browse files
bijoythomasbjohansebas
authored andcommitted
Change res.flush to accept a callback function argument
1 parent fbb806c commit 3d64772

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ function shouldCompress (req, res) {
196196
### res.flush
197197

198198
This module adds a `res.flush()` method to force the partially-compressed
199-
response to be flushed to the client.
199+
response to be flushed to the client. This function accepts a callback which gets
200+
invoked when the response has been flushed to the client
200201

201202
## Examples
202203

index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ function compression (options) {
7979
var _write = res.write
8080

8181
// flush
82-
res.flush = function flush () {
82+
res.flush = function flush(cb) {
8383
if (stream) {
84-
stream.flush()
84+
stream.flush(cb)
8585
}
8686
}
8787

test/compression.js

+74
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,80 @@ describe('compression()', function () {
835835
.end()
836836
})
837837

838+
it('should invoke flush callback when supplied for gzip', function (done) {
839+
var chunks = 0, callbackInvoked = false;
840+
var resp
841+
var server = createServer({ threshold: 0 }, function (req, res) {
842+
resp = res
843+
res.setHeader('Content-Type', 'text/plain')
844+
write()
845+
})
846+
847+
function flushCallback() {
848+
callbackInvoked = true;
849+
}
850+
851+
function write() {
852+
chunks++
853+
if (chunks === 20) return resp.end()
854+
if (chunks > 20) return chunks--
855+
resp.write('..')
856+
resp.flush(flushCallback)
857+
}
858+
859+
request(server)
860+
.get('/')
861+
.set('Accept-Encoding', 'gzip')
862+
.request()
863+
.on('response', function (res) {
864+
assert.equal(res.headers['content-encoding'], 'gzip')
865+
res.on('data', write)
866+
res.on('end', function(){
867+
assert.equal(chunks, 20)
868+
assert.equal(callbackInvoked, true)
869+
done()
870+
})
871+
})
872+
.end()
873+
})
874+
875+
it('should invoke flush callback when supplied for deflate', function (done) {
876+
var chunks = 0, callbackInvoked = false;
877+
var resp
878+
var server = createServer({ threshold: 0 }, function (req, res) {
879+
resp = res
880+
res.setHeader('Content-Type', 'text/plain')
881+
write()
882+
})
883+
884+
function flushCallback() {
885+
callbackInvoked = true;
886+
}
887+
888+
function write() {
889+
chunks++
890+
if (chunks === 20) return resp.end()
891+
if (chunks > 20) return chunks--
892+
resp.write('..')
893+
resp.flush(flushCallback)
894+
}
895+
896+
request(server)
897+
.get('/')
898+
.set('Accept-Encoding', 'deflate')
899+
.request()
900+
.on('response', function (res) {
901+
assert.equal(res.headers['content-encoding'], 'deflate')
902+
res.on('data', write)
903+
res.on('end', function(){
904+
assert.equal(chunks, 20)
905+
assert.equal(callbackInvoked, true)
906+
done()
907+
})
908+
})
909+
.end()
910+
})
911+
838912
it('should flush small chunks for deflate', function (done) {
839913
var chunks = 0
840914
var next

0 commit comments

Comments
 (0)