From 287d3a249c48a0666e222988264ae973b0edf723 Mon Sep 17 00:00:00 2001 From: Bo Borgerson Date: Wed, 17 Feb 2016 11:42:49 -0800 Subject: [PATCH 1/3] Add `flushCompression` as an alias for `flush` Supports use such as: ```javascript // Not going to write again for a while. If compression middleware is // installed let's tell it to flush what we've got through to the client. if (res.flushCompression) { res.flushCompression(); } ``` Currently the `res` object has a `flush` method regardless of whether compression middleware is installed. The built-in `flush` method issues a noisy deprecation warning. This patch provides an alias for the `flush` method to allow detection of the compression middleware's `flush`. This addresses https://github.com/expressjs/compression/issues/72. --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index c0e801e7..d43466d7 100644 --- a/index.js +++ b/index.js @@ -66,7 +66,7 @@ function compression (options) { var _write = res.write // flush - res.flush = function flush () { + res.flush = res.flushCompression = function flush() { if (stream) { stream.flush() } From 14cdf4ff8ac9e3f0d19a363a38ea0f0243f1f8aa Mon Sep 17 00:00:00 2001 From: Bo Borgerson Date: Wed, 17 Feb 2016 12:06:29 -0800 Subject: [PATCH 2/3] Add a test for presence of `flushCompression()` alias --- test/compression.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/compression.js b/test/compression.js index 81c00149..7f19c158 100644 --- a/test/compression.js +++ b/test/compression.js @@ -579,6 +579,20 @@ describe('compression()', function () { .expect(200, done) }) + it('should have a res.flushCompression() alias', function (done) { + var server = createServer(null, function (req, res) { + res.statusCode = res.flush === res.flushCompression + ? 200 + : 500 + res.flushCompression() + res.end() + }) + + request(server) + .get('/') + .expect(200, done) + }) + it('should flush the response', function (done) { var chunks = 0 var resp From 1321557ee7d8201415a2158aabf9b7f8a7b5c66a Mon Sep 17 00:00:00 2001 From: Bo Borgerson Date: Wed, 17 Feb 2016 12:07:09 -0800 Subject: [PATCH 3/3] Add `flushCompression()` to README with example use --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index a5d599db..212a768a 100644 --- a/README.md +++ b/README.md @@ -160,6 +160,20 @@ function shouldCompress(req, res) { This module adds a `res.flush()` method to force the partially-compressed response to be flushed to the client. +### res.flushCompression + +This is an alias for the `res.flush()` method that is added by this module. + +This supports use such as: + +```javascript +// Not going to write again for a while. If compression middleware is +// installed let's tell it to flush what we've got through to the client. +if (res.flushCompression) { + res.flushCompression(); +} +``` + ## Examples ### express/connect