From 03000eb5005c5dbf985b2b167466449add00325d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dirk=20L=C3=BCth?= Date: Thu, 18 Jan 2018 00:00:13 +0100 Subject: [PATCH 1/2] Fix usage of undocumented _implicitHeader --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 6e20f906..d0756f5c 100644 --- a/index.js +++ b/index.js @@ -108,7 +108,7 @@ function compression (options) { } if (!this._header) { - this._implicitHeader() + this.writeHead(this.statusCode) } return stream @@ -127,7 +127,7 @@ function compression (options) { length = chunkLength(chunk, encoding) } - this._implicitHeader() + this.writeHead(this.statusCode) } if (!stream) { From 841024e2539e0dbbcd7c832083a7fa5a4c7df4bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dirk=20L=C3=BCth?= Date: Fri, 19 Jan 2018 13:48:54 +0100 Subject: [PATCH 2/2] add tests for Node http2 --- test/compression.js | 74 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/test/compression.js b/test/compression.js index 85e47343..b7c862dc 100644 --- a/test/compression.js +++ b/test/compression.js @@ -8,6 +8,16 @@ var request = require('supertest') var compression = require('..') +var describeHttp2 = describe.skip +try { + var http2 = require('http2') + describeHttp2 = describe +} catch (err) { + if (err) { + console.log('http2 tests disabled.') + } +} + describe('compression()', function () { it('should skip HEAD', function (done) { var server = createServer({ threshold: 0 }, function (req, res) { @@ -296,6 +306,26 @@ describe('compression()', function () { .expect(200, done) }) + describeHttp2('http2', function () { + it('should work with http2 server', function (done) { + var server = createHttp2Server({ threshold: 0 }, function (req, res) { + res.setHeader('Content-Type', 'text/plain') + res.end('hello, world') + }) + server.on('listening', function () { + var client = createHttp2Client(server.address().port) + var request = client.request({ + 'Accept-Encoding': 'gzip' + }) + request.on('response', function (headers) { + assert.equal(headers['content-encoding'], 'gzip') + closeHttp2(request, client, server, done) + }) + request.end() + }) + }) + }) + describe('threshold', function () { it('should not compress responses below the threshold size', function (done) { var server = createServer({ threshold: '1kb' }, function (req, res) { @@ -890,6 +920,50 @@ function createServer (opts, fn) { }) } +function createHttp2Server (opts, fn) { + var _compression = compression(opts) + var server = http2.createServer(function (req, res) { + _compression(req, res, function (err) { + if (err) { + res.statusCode = err.status || 500 + res.end(err.message) + return + } + + fn(req, res) + }) + }) + server.listen(0, '127.0.0.1') + return server +} + +function createHttp2Client (port) { + var client = http2.connect('http://127.0.0.1:' + port) + return client +} + +function closeHttp2 (request, client, server, callback) { + if (typeof client.shutdown === 'function') { + // this is the node v8.x way of closing the connections + request.destroy(http2.constants.NGHTTP2_NO_ERROR, function () { + client.shutdown({}, function () { + server.close(function () { + callback() + }) + }) + }) + } else { + // this is the node v9.x onwards (hopefully) way of closing the connections + request.close(http2.constants.NGHTTP2_NO_ERROR, function () { + client.close(function () { + server.close(function () { + callback() + }) + }) + }) + } +} + function shouldHaveBodyLength (length) { return function (res) { assert.equal(res.text.length, length, 'should have body length of ' + length)