Skip to content

Commit

Permalink
Merge pull request #153 from opentable/compress
Browse files Browse the repository at this point in the history
js.gz + css.gz handling
  • Loading branch information
andyroyle committed Nov 24, 2015
2 parents 7e7ab2e + 92e2a6c commit 5c434a2
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 18 deletions.
17 changes: 9 additions & 8 deletions registry/domain/s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ var Cache = require('nice-cache');
var format = require('stringformat');
var fs = require('fs-extra');
var nodeDir = require('node-dir');
var path = require('path');
var _ = require('underscore');

var getMimeType = require('../../utils/get-mime-type');
var getFileInfo = require('../../utils/get-file-info');
var getNextYear = require('../../utils/get-next-year');
var strings = require('../../resources');

Expand Down Expand Up @@ -132,7 +131,7 @@ module.exports = function(conf){
},
putFileContent: function(fileContent, fileName, isPrivate, callback){

var mimeType = getMimeType(path.extname(fileName)),
var fileInfo = getFileInfo(fileName),
obj = {
Bucket: bucket,
Key: fileName,
Expand All @@ -142,13 +141,15 @@ module.exports = function(conf){
Expires: getNextYear()
};

if(mimeType){
obj.ContentType = mimeType;
if(!!fileInfo.mimeType){
obj.ContentType = fileInfo.mimeType;
}

client.putObject(obj, function (err, res) {
callback(err, res);
});
if(!!fileInfo.gzip){
obj.ContentEncoding = 'gzip';
}

client.putObject(obj, callback);
}
};
};
13 changes: 8 additions & 5 deletions registry/routes/static-redirector.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var format = require('stringformat');
var fs = require('fs-extra');
var path = require('path');

var getMimeType = require('../../utils/get-mime-type');
var getFileInfo = require('../../utils/get-file-info');

module.exports = function(repository){
return function(req, res){
Expand All @@ -30,11 +30,14 @@ module.exports = function(repository){
}

var fileStream = fs.createReadStream(filePath),
fileExt = path.extname(filePath),
mimeType = getMimeType(fileExt);
fileInfo = getFileInfo(filePath);

if(!!mimeType){
res.set('Content-type', mimeType);
if(!!fileInfo.mimeType){
res.set('Content-Type', fileInfo.mimeType);
}

if(fileInfo.gzip){
res.set('Content-Encoding', 'gzip');
}

fileStream.on('open', function(){
Expand Down
1 change: 0 additions & 1 deletion test/unit/cli-domain-package-static-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ describe('cli : domain : packageStaticFiles', function(){
});
});


describe('when copying folder with css file', function(){

beforeEach(function(){
Expand Down
42 changes: 38 additions & 4 deletions test/unit/registry-domain-s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,31 +240,65 @@ describe('registry : domain : s3', function(){
initialiseAndExecutePut('hello.js', false, done);
});

it('should be saved using application/javascript fileType', function(){
it('should be saved using application/javascript Content-Type', function(){
var params = mockedS3Client.putObject.args;
expect(params[0][0].ContentType).to.equal('application/javascript');
});
});

describe('when putting gzipped js file', function(){

before(function(done){
initialiseAndExecutePut('hello.js.gz', false, done);
});

it('should be saved using application/javascript Content-Type', function(){
var params = mockedS3Client.putObject.args;
expect(params[0][0].ContentType).to.equal('application/javascript');
});

it('should be saved using gzip Content Content-Encoding', function(){
var params = mockedS3Client.putObject.args;
expect(params[0][0].ContentEncoding).to.equal('gzip');
});
});

describe('when putting css file', function(){

before(function(done){
initialiseAndExecutePut('hello.css', false, done);
});

it('should be saved using text/css fileType', function(){
it('should be saved using text/css Content-Type', function(){
var params = mockedS3Client.putObject.args;
expect(params[0][0].ContentType).to.equal('text/css');
});
});

describe('when putting gzipped css file', function(){

before(function(done){
initialiseAndExecutePut('hello.css.gz', false, done);
});

it('should be saved using text/css Content-Type', function(){
var params = mockedS3Client.putObject.args;
expect(params[0][0].ContentType).to.equal('text/css');
});

it('should be saved using text/css Content-Encoding', function(){
var params = mockedS3Client.putObject.args;
expect(params[0][0].ContentEncoding).to.equal('gzip');
});
});

describe('when putting jpg file', function(){

before(function(done){
initialiseAndExecutePut('hello.jpg', false, done);
});

it('should be saved using image/jpeg fileType', function(){
it('should be saved using image/jpeg Content-Type', function(){
var params = mockedS3Client.putObject.args;
expect(params[0][0].ContentType).to.equal('image/jpeg');
});
Expand All @@ -276,7 +310,7 @@ describe('registry : domain : s3', function(){
initialiseAndExecutePut('hello.gif', false, done);
});

it('should be saved using image/gif fileType', function(){
it('should be saved using image/gif Content-Type', function(){
var params = mockedS3Client.putObject.args;
expect(params[0][0].ContentType).to.equal('image/gif');
});
Expand Down
68 changes: 68 additions & 0 deletions test/unit/utils-get-file-info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'use strict';

var expect = require('chai').expect;

describe('utils : getFileInfo', function(){

var getFileInfo = require('../../utils/get-file-info');

describe('when extension is .js', function(){
var fileInfo = getFileInfo('/path/file.js');

it('should return the correct myme type', function(){
expect(fileInfo.mimeType).to.equal('application/javascript');
});

it('should return the correct extension', function(){
expect(fileInfo.extname).to.equal('.js');
});

it('should recognise it is not a gzip', function(){
expect(fileInfo.gzip).to.be.false;
});
});

describe('when extension is .js.gz', function(){
var fileInfo = getFileInfo('path/file.js.gz');

it('should return the correct myme type', function(){
expect(fileInfo.mimeType).to.equal('application/javascript');
});

it('should return the correct extension', function(){
expect(fileInfo.extname).to.equal('.js');
});

it('should recognise it is a gzip', function(){
expect(fileInfo.gzip).to.be.true;
});
});

describe('when extension is .css.gz', function(){
var fileInfo = getFileInfo('path/file.css.gz');

it('should return the correct myme type', function(){
expect(fileInfo.mimeType).to.equal('text/css');
});

it('should return the correct extension', function(){
expect(fileInfo.extname).to.equal('.css');
});

it('should recognise it is a gzip', function(){
expect(fileInfo.gzip).to.be.true;
});
});

describe('when extension is unknown', function(){
var fileInfo = getFileInfo('.heisenberg');

it('should return mime undefined', function(){
expect(fileInfo.mimeType).to.be.undefined;
});

it('should handle it as not gzip', function(){
expect(fileInfo.gzip).to.be.false;
});
});
});
21 changes: 21 additions & 0 deletions utils/get-file-info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

var path = require('path');

var getMimeType = require('./get-mime-type');

module.exports = function(filePath){
var ext = path.extname(filePath).toLowerCase(),
isGzipped = false;

if(ext === '.gz'){
isGzipped = true;
ext = path.extname(filePath.slice(0, -3)).toLowerCase();
}

return {
gzip: isGzipped,
extname: ext,
mimeType: getMimeType(ext)
};
};

0 comments on commit 5c434a2

Please sign in to comment.