From 3ea56506d35d5bced86db3e552ec11065cf3cf7f Mon Sep 17 00:00:00 2001 From: Damian Kaczmarek Date: Wed, 6 Jan 2016 16:52:35 +0100 Subject: [PATCH] Serve pre-gzipped files instead of gzipping them on the fly --- modules/middleware/static.js | 3 +-- package.json | 5 ++-- tests/static.js | 48 +++++++++++++++++++++++++++++++++ tests/static_data/404.html | 1 + tests/static_data/test.txt | 1 + tests/static_data/test2.txt.gz | Bin 0 -> 27 bytes 6 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 tests/static.js create mode 100644 tests/static_data/404.html create mode 100644 tests/static_data/test.txt create mode 100644 tests/static_data/test2.txt.gz diff --git a/modules/middleware/static.js b/modules/middleware/static.js index 2091f95..39da09a 100644 --- a/modules/middleware/static.js +++ b/modules/middleware/static.js @@ -1,4 +1,4 @@ -var serveStatic = require('serve-static'); +var serveStatic = require('connect-gzip-static'); var send = require('send'); var path = require('path'); @@ -7,7 +7,6 @@ module.exports = function StaticMiddleware() { requestHandler: function(req, res, next, target) { target.middleware(req, res, function(err) { if(err) return next(err); - var stream = send(req, path.join(target.entry, '404.html'), {}); stream.on('error', next); stream.pipe(res); diff --git a/package.json b/package.json index 1476242..4a49df7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "http-master", - "version": "1.0.25", + "version": "1.0.26", "description": "Easy to setup, convenient, universal, parallel, http/https proxy daemon. Setup in 1 minute, run, configure, adapt. Proxying based on excellent node-http-proxy.", "main": "src/HttpMaster.js", "scripts": { @@ -29,6 +29,7 @@ "async": "1.4.2", "bugsnag": "1.6.4", "compression": "1.6.0", + "connect-gzip-static": "^1.0.0", "eventemitter3": "1.1.1", "extend": "2.0.1", "http-auth": "2.2.5", @@ -42,7 +43,6 @@ "ocsp": "1.0.2", "parseurl": "1.3.0", "send": "0.13.0", - "serve-static": "1.10.0", "spdy": "3.0.0", "uid-number": "0.0.6", "ws": "0.8.0", @@ -55,6 +55,7 @@ "fs.extra": "1.2.1", "istanbul": "0.3.2", "mocha": "2.3.4", + "request-promise": "^1.0.2", "should": "4.3.0" }, "optionalDependencies": { diff --git a/tests/static.js b/tests/static.js new file mode 100644 index 0000000..6174530 --- /dev/null +++ b/tests/static.js @@ -0,0 +1,48 @@ +'use strict'; +var HttpMaster = require('../src/HttpMaster'); +var expect = require('chai').expect; +require('should'); + +var testUtils = require('../src/testUtils'); +var rp = require('request-promise'); + +describe('static middleware', function() { + + var master; + before(function(done) { + master = new HttpMaster(); + master.on('error', done); + master.init({ + workerCount: 0, + ports: { + 23441: 'static -> ./tests/static_data' + } + }, function(err) { + done(err); + }); + }); + + it('should serve uncompressed test.txt', function(cb) { + rp('http://localhost:23441/test.txt').then(function(data) { + expect(data).to.equal('foo bar'); + }).nodeify(cb); + }); + + it('should work with gzip capable client even if no .gz variant of a file exists', function(cb) { + rp('http://localhost:23441/test.txt', {gzip: true}).then(function(data) { + expect(data).to.equal('foo bar'); + }).nodeify(cb); + }); + + it('should serve pre-compressed test2.txt to gzip capable client', function(cb) { + rp('http://localhost:23441/test2.txt', {gzip: true}).then(function(data) { + expect(data).to.equal('foo bar'); + }).nodeify(cb); + }); + + it('should 404 while sending file that is only gzipped and if client is not gzip capable', function(cb) { + rp('http://localhost:23441/test2.txt').then(function(data) { + expect(data).to.equal('not found'); + }).nodeify(cb); + }); +}); diff --git a/tests/static_data/404.html b/tests/static_data/404.html new file mode 100644 index 0000000..8347208 --- /dev/null +++ b/tests/static_data/404.html @@ -0,0 +1 @@ +not found \ No newline at end of file diff --git a/tests/static_data/test.txt b/tests/static_data/test.txt new file mode 100644 index 0000000..96c9067 --- /dev/null +++ b/tests/static_data/test.txt @@ -0,0 +1 @@ +foo bar \ No newline at end of file diff --git a/tests/static_data/test2.txt.gz b/tests/static_data/test2.txt.gz new file mode 100644 index 0000000000000000000000000000000000000000..d672863ba157d05aa2a8da4f25288bdcefb3a7b0 GIT binary patch literal 27 icmb2|=3rQ?+Z)Ef?0x!txQCYx6N3q(+dg&%1_l6X3kIbC literal 0 HcmV?d00001