Skip to content

Commit

Permalink
Use the shrink-ray middleware to support modern compression schemes u…
Browse files Browse the repository at this point in the history
…pon request.
  • Loading branch information
Berlioz committed Jul 27, 2017
1 parent d37533f commit aaab721
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 14 deletions.
1 change: 0 additions & 1 deletion bin/server
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,5 @@ if (notifier.update) {
var cli = Cli();

cli.run(process.argv, function() {

console.log('\nSuperstatic started.\nVisit ' + format.underline('http://' + cli.get('hostname') + ':' + cli.get('port')) + ' to view your app.');
});
3 changes: 2 additions & 1 deletion examples/middleware/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ var spec = {
destination: '/index.html'
}]
},
cwd: process.cwd()
cwd: process.cwd(),
compression: true
};

var app = connect()
Expand Down
2 changes: 1 addition & 1 deletion examples/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var spec = {
},
cwd: __dirname,
errorPage: __dirname + '/error.html',
gzip: true,
compression: true,
debug: true
};

Expand Down
8 changes: 7 additions & 1 deletion lib/cli/flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ module.exports = function(cli, options, ready) {

cli.flag('--gzip')
.handler(function(shouldCompress, done) {
cli.set('gzip', shouldCompress);
cli.set('compression', shouldCompress);
done();
});

cli.flag('--compression')
.handler(function(shouldCompress, done) {
cli.set('compression', shouldCompress);
done();
});

Expand Down
4 changes: 2 additions & 2 deletions lib/cli/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = function(cli, imports, ready) {
var port = cli.get('port');
var hostname = cli.get('hostname');
var debug = cli.get('debug');
var gzip = cli.get('gzip');
var compression = cli.get('compression');
var env = cli.get('env');
var live = cli.get('live');

Expand All @@ -27,7 +27,7 @@ module.exports = function(cli, imports, ready) {
config: config,
port: port,
hostname: hostname,
gzip: gzip,
compression: compression,
debug: debug,
env: env,
live: live
Expand Down
10 changes: 5 additions & 5 deletions lib/responder.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ var mime = require('mime-types');
var path = require('path');
var awaitFinished = RSVP.denodeify(require('on-finished'));
var destroy = require('destroy');
var compress = require('compression')();
var shrinkRay = require('shrink-ray')();

var Responder = function(req, res, options) {
this.req = req;
this.res = res;
this.provider = options.provider;
this.config = options.config || {};
this.rewriters = options.rewriters || {};
this.gzip = options.gzip;
this.compression = options.compression;
};

Responder.prototype.isNotModified = function(stats) {
Expand Down Expand Up @@ -114,13 +114,13 @@ Responder.prototype.handleFileStream = function(file, result) {
this.res.statusCode = 404;
}
this.res.setHeader('Content-Type', result.contentType || mime.contentType(path.extname(file.file)));
if (result.size) { this.res.setHeader('Content-Length', result.size); }
if (result.etag) { this.res.setHeader('ETag', result.etag); }
if (result.modified) {
this.res.setHeader('Last-Modified', new Date(result.modified).toUTCString());
}
if (this.gzip) {
compress(this.req, this.res, function() {

if (this.compression) {
shrinkRay(this.req, this.res, function() {
result.stream.pipe(self.res);
});
} else {
Expand Down
2 changes: 1 addition & 1 deletion lib/superstatic.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var superstatic = function(spec) {
res.superstatic = new Responder(req, res, {
provider: provider,
config: config,
gzip: spec.gzip,
compression: spec.compression,
rewriters: spec.rewriters
});

Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"hashbang"
],
"dependencies": {
"accepts": "^1.3.3",
"as-array": "^2.0.0",
"async": "^1.5.2",
"basic-auth-connect": "^1.0.0",
Expand All @@ -54,6 +55,7 @@
"glob": "^7.1.2",
"glob-slasher": "^1.0.1",
"home-dir": "^1.0.0",
"iltorb": "^1.0.13",
"is-url": "^1.2.2",
"join-path": "^1.1.1",
"lodash": "^4.17.4",
Expand All @@ -66,6 +68,7 @@
"path-to-regexp": "^1.7.0",
"router": "^1.3.1",
"rsvp": "^3.6.2",
"shrink-ray": "^0.1.3",
"string-length": "^1.0.0",
"try-require": "^1.0.0",
"update-notifier": "^1.0.3"
Expand Down
11 changes: 9 additions & 2 deletions test/unit/cli/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,16 @@ describe('cli', function() {
});
});

it('enables gzipping', function(done) {
it('supports the old --gzip flag', function(done) {
cli.run(['', '', '--gzip'], function() {
expect(cli.get('gzip')).to.equal(true);
expect(cli.get('compression')).to.equal(true);
cli.get('server').close(done);
});
});

it('enables smart compression', function(done) {
cli.run(['', '', '--compression'], function() {
expect(cli.get('compression')).to.equal(true);
cli.get('server').close(done);
});
});
Expand Down

0 comments on commit aaab721

Please sign in to comment.