diff --git a/index.js b/index.js index 6e20f906..104064b6 100644 --- a/index.js +++ b/index.js @@ -81,8 +81,6 @@ function compression (options) { var shouldCache = opts.cache || function () { return true } - var dummyBrotliFlush = function () { } - return function compression (req, res, next) { var ended = false var length @@ -239,8 +237,6 @@ function compression (options) { switch (method) { case 'br': stream = iltorb.compressStream(brotliOpts) - // brotli has no flush method. add a dummy flush method here. - stream.flush = dummyBrotliFlush break case 'gzip': stream = zlib.createGzip(zlibOpts) diff --git a/package.json b/package.json index 4bc9db05..2d614a53 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "bytes": "2.5.0", "compressible": "2.0.10", "debug": "2.6.3", - "iltorb": "1.0.13", + "iltorb": "2.0.2", "lru-cache": "4.0.2", "multipipe": "1.0.2", "node-zopfli": "2.0.2", diff --git a/test/compression.js b/test/compression.js index 85e47343..e6979e8f 100644 --- a/test/compression.js +++ b/test/compression.js @@ -527,17 +527,6 @@ describe('compression()', function () { done() }) }) - - it('should not throw if flush() is called', function (done) { - var server = createServer({ threshold: 0 }, function (req, res) { - res.setHeader('Content-Type', 'text/plain') - res.write('hello, ') - res.flush() - res.end('world') - }) - - brotliRequest(server).expect('Content-Encoding', 'br', done) - }) }) describe('when caching is turned on', function () { @@ -813,6 +802,37 @@ describe('compression()', function () { .end() }) + it('should flush the response for brotli', function (done) { + var chunks = 0 + var resp + var server = createServer({ threshold: 0 }, function (req, res) { + resp = res + res.setHeader('Content-Type', 'text/plain') + res.setHeader('Content-Length', '2048') + write() + }) + + function write () { + chunks++ + if (chunks === 2) return resp.end() + if (chunks > 2) return chunks-- + resp.write(new Buffer(1024)) + resp.flush() + } + + brotliRequest(server) + .request() + .on('response', function (res) { + assert.equal(res.headers['content-encoding'], 'br') + res.on('data', write) + res.on('end', function () { + assert.equal(chunks, 2) + done() + }) + }) + .end() + }) + it('should flush small chunks for gzip', function (done) { var chunks = 0 var resp @@ -872,6 +892,36 @@ describe('compression()', function () { }) .end() }) + + it('should flush small chunks for brotli', function (done) { + var chunks = 0 + var resp + var server = createServer({ threshold: 0 }, function (req, res) { + resp = res + res.setHeader('Content-Type', 'text/plain') + write() + }) + + function write () { + chunks++ + if (chunks === 20) return resp.end() + if (chunks > 20) return chunks-- + resp.write('..') + resp.flush() + } + + brotliRequest(server) + .request() + .on('response', function (res) { + assert.equal(res.headers['content-encoding'], 'br') + res.on('data', write) + res.on('end', function () { + assert.equal(chunks, 20) + done() + }) + }) + .end() + }) }) })