Skip to content

Commit e2abcbf

Browse files
Change res.flush to accept a callback function argument (#227)
* Change res.flush to accept a callback function argument * test: add callback invocation check for flush in brotli compression * fix lint * deps: update mocha to version 10.8.2 and add --exit option --------- Co-authored-by: Bijoy Thomas <[email protected]>
1 parent fbb806c commit e2abcbf

File tree

4 files changed

+117
-5
lines changed

4 files changed

+117
-5
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

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"eslint-plugin-node": "^11.1.0",
2828
"eslint-plugin-promise": "^5.2.0",
2929
"eslint-plugin-standard": "^4.1.0",
30-
"mocha": "^9.2.2",
30+
"mocha": "^10.8.2",
3131
"nyc": "^15.1.0",
3232
"supertest": "^6.2.3"
3333
},
@@ -41,7 +41,7 @@
4141
},
4242
"scripts": {
4343
"lint": "eslint .",
44-
"test": "mocha --check-leaks --reporter spec",
44+
"test": "mocha --check-leaks --exit --reporter spec",
4545
"test-ci": "nyc --reporter=lcov --reporter=text npm test",
4646
"test-cov": "nyc --reporter=html --reporter=text npm test"
4747
}

test/compression.js

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

838+
it('should invoke flush callback when supplied for gzip', function (done) {
839+
var chunks = 0; var 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 brotli', function (done) {
876+
var chunks = 0; var 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', 'br')
899+
.request()
900+
.on('response', function (res) {
901+
assert.equal(res.headers['content-encoding'], 'br')
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+
912+
it('should invoke flush callback when supplied for deflate', function (done) {
913+
var chunks = 0; var callbackInvoked = false
914+
var resp
915+
var server = createServer({ threshold: 0 }, function (req, res) {
916+
resp = res
917+
res.setHeader('Content-Type', 'text/plain')
918+
write()
919+
})
920+
921+
function flushCallback () {
922+
callbackInvoked = true
923+
}
924+
925+
function write () {
926+
chunks++
927+
if (chunks === 20) return resp.end()
928+
if (chunks > 20) return chunks--
929+
resp.write('..')
930+
resp.flush(flushCallback)
931+
}
932+
933+
request(server)
934+
.get('/')
935+
.set('Accept-Encoding', 'deflate')
936+
.request()
937+
.on('response', function (res) {
938+
assert.equal(res.headers['content-encoding'], 'deflate')
939+
res.on('data', write)
940+
res.on('end', function () {
941+
assert.equal(chunks, 20)
942+
assert.equal(callbackInvoked, true)
943+
done()
944+
})
945+
})
946+
.end()
947+
})
948+
838949
it('should flush small chunks for deflate', function (done) {
839950
var chunks = 0
840951
var next

0 commit comments

Comments
 (0)