From 3d64772c223c1fa5e9f97efb4cc52b7e3266c7f9 Mon Sep 17 00:00:00 2001 From: Bijoy Thomas Date: Fri, 23 Oct 2015 09:32:26 -0500 Subject: [PATCH 1/4] Change res.flush to accept a callback function argument --- README.md | 3 +- index.js | 4 +-- test/compression.js | 74 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c3375da8..682517b4 100644 --- a/README.md +++ b/README.md @@ -196,7 +196,8 @@ function shouldCompress (req, res) { ### res.flush This module adds a `res.flush()` method to force the partially-compressed -response to be flushed to the client. +response to be flushed to the client. This function accepts a callback which gets +invoked when the response has been flushed to the client ## Examples diff --git a/index.js b/index.js index 3a0c18e3..68717c7d 100644 --- a/index.js +++ b/index.js @@ -79,9 +79,9 @@ function compression (options) { var _write = res.write // flush - res.flush = function flush () { + res.flush = function flush(cb) { if (stream) { - stream.flush() + stream.flush(cb) } } diff --git a/test/compression.js b/test/compression.js index 9ba4838d..f56f60b4 100644 --- a/test/compression.js +++ b/test/compression.js @@ -835,6 +835,80 @@ describe('compression()', function () { .end() }) + it('should invoke flush callback when supplied for gzip', function (done) { + var chunks = 0, callbackInvoked = false; + var resp + var server = createServer({ threshold: 0 }, function (req, res) { + resp = res + res.setHeader('Content-Type', 'text/plain') + write() + }) + + function flushCallback() { + callbackInvoked = true; + } + + function write() { + chunks++ + if (chunks === 20) return resp.end() + if (chunks > 20) return chunks-- + resp.write('..') + resp.flush(flushCallback) + } + + request(server) + .get('/') + .set('Accept-Encoding', 'gzip') + .request() + .on('response', function (res) { + assert.equal(res.headers['content-encoding'], 'gzip') + res.on('data', write) + res.on('end', function(){ + assert.equal(chunks, 20) + assert.equal(callbackInvoked, true) + done() + }) + }) + .end() + }) + + it('should invoke flush callback when supplied for deflate', function (done) { + var chunks = 0, callbackInvoked = false; + var resp + var server = createServer({ threshold: 0 }, function (req, res) { + resp = res + res.setHeader('Content-Type', 'text/plain') + write() + }) + + function flushCallback() { + callbackInvoked = true; + } + + function write() { + chunks++ + if (chunks === 20) return resp.end() + if (chunks > 20) return chunks-- + resp.write('..') + resp.flush(flushCallback) + } + + request(server) + .get('/') + .set('Accept-Encoding', 'deflate') + .request() + .on('response', function (res) { + assert.equal(res.headers['content-encoding'], 'deflate') + res.on('data', write) + res.on('end', function(){ + assert.equal(chunks, 20) + assert.equal(callbackInvoked, true) + done() + }) + }) + .end() + }) + it('should flush small chunks for deflate', function (done) { var chunks = 0 var next From 48f195cdeba63cb0520c474d82487e3e1601e040 Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Wed, 16 Apr 2025 12:53:35 -0500 Subject: [PATCH 2/4] test: add callback invocation check for flush in brotli compression --- test/compression.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/test/compression.js b/test/compression.js index f56f60b4..ead645f1 100644 --- a/test/compression.js +++ b/test/compression.js @@ -872,6 +872,43 @@ describe('compression()', function () { .end() }) + it('should invoke flush callback when supplied for brotli', function (done) { + var chunks = 0, callbackInvoked = false; + var resp + var server = createServer({ threshold: 0 }, function (req, res) { + resp = res + res.setHeader('Content-Type', 'text/plain') + write() + }) + + function flushCallback() { + callbackInvoked = true; + } + + function write() { + chunks++ + if (chunks === 20) return resp.end() + if (chunks > 20) return chunks-- + resp.write('..') + resp.flush(flushCallback) + } + + request(server) + .get('/') + .set('Accept-Encoding', 'br') + .request() + .on('response', function (res) { + assert.equal(res.headers['content-encoding'], 'br') + res.on('data', write) + res.on('end', function(){ + assert.equal(chunks, 20) + assert.equal(callbackInvoked, true) + done() + }) + }) + .end() + }) + it('should invoke flush callback when supplied for deflate', function (done) { var chunks = 0, callbackInvoked = false; var resp From c641c6d3fba8209ed6adb9864cbcebc40ccc72ff Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Wed, 16 Apr 2025 12:55:34 -0500 Subject: [PATCH 3/4] fix lint --- index.js | 2 +- test/compression.js | 96 ++++++++++++++++++++++----------------------- 2 files changed, 49 insertions(+), 49 deletions(-) diff --git a/index.js b/index.js index 68717c7d..5dd09c7b 100644 --- a/index.js +++ b/index.js @@ -79,7 +79,7 @@ function compression (options) { var _write = res.write // flush - res.flush = function flush(cb) { + res.flush = function flush (cb) { if (stream) { stream.flush(cb) } diff --git a/test/compression.js b/test/compression.js index ead645f1..78173c33 100644 --- a/test/compression.js +++ b/test/compression.js @@ -836,7 +836,7 @@ describe('compression()', function () { }) it('should invoke flush callback when supplied for gzip', function (done) { - var chunks = 0, callbackInvoked = false; + var chunks = 0; var callbackInvoked = false var resp var server = createServer({ threshold: 0 }, function (req, res) { resp = res @@ -844,11 +844,11 @@ describe('compression()', function () { write() }) - function flushCallback() { - callbackInvoked = true; + function flushCallback () { + callbackInvoked = true } - function write() { + function write () { chunks++ if (chunks === 20) return resp.end() if (chunks > 20) return chunks-- @@ -857,23 +857,23 @@ describe('compression()', function () { } request(server) - .get('/') - .set('Accept-Encoding', 'gzip') - .request() - .on('response', function (res) { - assert.equal(res.headers['content-encoding'], 'gzip') - res.on('data', write) - res.on('end', function(){ - assert.equal(chunks, 20) - assert.equal(callbackInvoked, true) - done() + .get('/') + .set('Accept-Encoding', 'gzip') + .request() + .on('response', function (res) { + assert.equal(res.headers['content-encoding'], 'gzip') + res.on('data', write) + res.on('end', function () { + assert.equal(chunks, 20) + assert.equal(callbackInvoked, true) + done() + }) }) - }) - .end() + .end() }) it('should invoke flush callback when supplied for brotli', function (done) { - var chunks = 0, callbackInvoked = false; + var chunks = 0; var callbackInvoked = false var resp var server = createServer({ threshold: 0 }, function (req, res) { resp = res @@ -881,11 +881,11 @@ describe('compression()', function () { write() }) - function flushCallback() { - callbackInvoked = true; + function flushCallback () { + callbackInvoked = true } - function write() { + function write () { chunks++ if (chunks === 20) return resp.end() if (chunks > 20) return chunks-- @@ -894,23 +894,23 @@ describe('compression()', function () { } request(server) - .get('/') - .set('Accept-Encoding', 'br') - .request() - .on('response', function (res) { - assert.equal(res.headers['content-encoding'], 'br') - res.on('data', write) - res.on('end', function(){ - assert.equal(chunks, 20) - assert.equal(callbackInvoked, true) - done() + .get('/') + .set('Accept-Encoding', 'br') + .request() + .on('response', function (res) { + assert.equal(res.headers['content-encoding'], 'br') + res.on('data', write) + res.on('end', function () { + assert.equal(chunks, 20) + assert.equal(callbackInvoked, true) + done() + }) }) - }) - .end() + .end() }) it('should invoke flush callback when supplied for deflate', function (done) { - var chunks = 0, callbackInvoked = false; + var chunks = 0; var callbackInvoked = false var resp var server = createServer({ threshold: 0 }, function (req, res) { resp = res @@ -918,11 +918,11 @@ describe('compression()', function () { write() }) - function flushCallback() { - callbackInvoked = true; + function flushCallback () { + callbackInvoked = true } - function write() { + function write () { chunks++ if (chunks === 20) return resp.end() if (chunks > 20) return chunks-- @@ -931,19 +931,19 @@ describe('compression()', function () { } request(server) - .get('/') - .set('Accept-Encoding', 'deflate') - .request() - .on('response', function (res) { - assert.equal(res.headers['content-encoding'], 'deflate') - res.on('data', write) - res.on('end', function(){ - assert.equal(chunks, 20) - assert.equal(callbackInvoked, true) - done() + .get('/') + .set('Accept-Encoding', 'deflate') + .request() + .on('response', function (res) { + assert.equal(res.headers['content-encoding'], 'deflate') + res.on('data', write) + res.on('end', function () { + assert.equal(chunks, 20) + assert.equal(callbackInvoked, true) + done() + }) }) - }) - .end() + .end() }) it('should flush small chunks for deflate', function (done) { From 8b33846b2ddc79668a618055d01fb519d596182d Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Wed, 16 Apr 2025 13:07:16 -0500 Subject: [PATCH 4/4] deps: update mocha to version 10.8.2 and add --exit option --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 449cbe23..b73fd476 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "eslint-plugin-node": "^11.1.0", "eslint-plugin-promise": "^5.2.0", "eslint-plugin-standard": "^4.1.0", - "mocha": "^9.2.2", + "mocha": "^10.8.2", "nyc": "^15.1.0", "supertest": "^6.2.3" }, @@ -41,7 +41,7 @@ }, "scripts": { "lint": "eslint .", - "test": "mocha --check-leaks --reporter spec", + "test": "mocha --check-leaks --exit --reporter spec", "test-ci": "nyc --reporter=lcov --reporter=text npm test", "test-cov": "nyc --reporter=html --reporter=text npm test" }