Skip to content

Commit

Permalink
Serve pre-gzipped files instead of gzipping them on the fly
Browse files Browse the repository at this point in the history
  • Loading branch information
Rush committed Jan 6, 2016
1 parent 8d702b2 commit 3ea5650
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 4 deletions.
3 changes: 1 addition & 2 deletions modules/middleware/static.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var serveStatic = require('serve-static');
var serveStatic = require('connect-gzip-static');
var send = require('send');
var path = require('path');

Expand All @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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": {
Expand Down
48 changes: 48 additions & 0 deletions tests/static.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
1 change: 1 addition & 0 deletions tests/static_data/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
not found
1 change: 1 addition & 0 deletions tests/static_data/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo bar
Binary file added tests/static_data/test2.txt.gz
Binary file not shown.

0 comments on commit 3ea5650

Please sign in to comment.