Skip to content

Commit

Permalink
Merge pull request #104 from cibernox/support-brotli
Browse files Browse the repository at this point in the history
Support brotli
  • Loading branch information
lukemelia authored Feb 5, 2018
2 parents 7fbf82b + 3f6cba3 commit 488cee0
Show file tree
Hide file tree
Showing 7 changed files with 1,223 additions and 576 deletions.
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ module.exports = {
gzippedFiles: function(context) {
return context.gzippedFiles || []; // e.g. from ember-cli-deploy-gzip
},
brotliCompressedFiles: function(context) {
return context.brotliCompressedFiles || []; // e.g. from ember-cli-deploy-gzip
},
manifestPath: function(context) {
return context.manifestPath; // e.g. from ember-cli-deploy-manifest
},
Expand All @@ -53,6 +56,7 @@ module.exports = {
var distDir = this.readConfig('distDir');
var distFiles = this.readConfig('distFiles');
var gzippedFiles = this.readConfig('gzippedFiles');
var brotliCompressedFiles = this.readConfig('brotliCompressedFiles');
var bucket = this.readConfig('bucket');
var acl = this.readConfig('acl');
var prefix = this.readConfig('prefix');
Expand All @@ -74,6 +78,7 @@ module.exports = {
cwd: distDir,
filePaths: filesToUpload,
gzippedFilePaths: gzippedFiles,
brotliCompressedFilePaths: brotliCompressedFiles,
prefix: prefix,
bucket: bucket,
acl: acl,
Expand Down
32 changes: 22 additions & 10 deletions lib/s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,16 @@ module.exports = CoreObject.extend({
},

_putObject: function(filePath, options, filePaths) {
var plugin = this.plugin;
var cwd = options.cwd;
var bucket = options.bucket;
var prefix = options.prefix;
var acl = options.acl;
var gzippedFilePaths = options.gzippedFilePaths || [];
var cacheControl = options.cacheControl;
var expires = options.expires;
var serverSideEncryption = options.serverSideEncryption;
var plugin = this.plugin;
var cwd = options.cwd;
var bucket = options.bucket;
var prefix = options.prefix;
var acl = options.acl;
var gzippedFilePaths = options.gzippedFilePaths || [];
var brotliCompressedFilePaths = options.brotliCompressedFilePaths || [];
var cacheControl = options.cacheControl;
var expires = options.expires;
var serverSideEncryption = options.serverSideEncryption;

mime.default_type = options.defaultMimeType || mime.lookup('bin');

Expand All @@ -121,9 +122,17 @@ module.exports = CoreObject.extend({
var encoding = mime.charsets.lookup(contentType);
var key = prefix === '' ? filePath : [prefix, filePath].join('/');
var isGzipped = gzippedFilePaths.indexOf(filePath) !== -1;
var isBrotliCompressed = brotliCompressedFilePaths.indexOf(filePath) !== -1;

if (isGzipped && path.extname(basePath) === '.gz') {
var basePathUncompressed = path.basename(basePath, '.gz');
var basePathUngzipped = path.basename(basePath, '.gz');
if (filePaths && filePaths.indexOf(basePathUngzipped) !== -1) {
contentType = mime.lookup(basePathUngzipped);
encoding = mime.charsets.lookup(contentType);
}
}
if (isBrotliCompressed) {
var basePathUncompressed = path.basename(basePath, '.br');
if (filePaths && filePaths.indexOf(basePathUncompressed) !== -1) {
contentType = mime.lookup(basePathUncompressed);
encoding = mime.charsets.lookup(contentType);
Expand Down Expand Up @@ -152,6 +161,9 @@ module.exports = CoreObject.extend({
if (isGzipped) {
params.ContentEncoding = 'gzip';
}
if (isBrotliCompressed) {
params.ContentEncoding = 'br';
}

return new RSVP.Promise(function(resolve, reject) {
this._client.putObject(params, function(error) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@
"chai-as-promised": "^6.0.0",
"chai": "^3.5.0",
"ember-cli-release": "^1.0.0-beta.2",
"ember-cli": "^2.12.0",
"ember-cli": "^2.18.0",
"eslint": "^3.18.0",
"github": "^0.2.4",
"glob": "^7.1.1",
"mocha": "^3.2.0",
"multiline": "^1.0.2"
},
"engines": {
"node": ">= 4"
"node": "^4.5 || 6.* || >= 7.*"
},
"ember-addon": {
"before": "ember-cli-deploy-redis"
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/dist/app.css.br
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
�body: {}

2 changes: 1 addition & 1 deletion tests/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ describe('s3 plugin', function() {
return previous;
}, []);

assert.equal(messages.length, 7);
assert.equal(messages.length, 8);
});

describe('required config', function() {
Expand Down
30 changes: 30 additions & 0 deletions tests/lib/s3-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,36 @@ describe('s3', function() {
});
});

it('sends the correct content type params for brotli-compressed files with .br extension', function () {
var s3Params;
s3Client.putObject = function (params, cb) {
s3Params = params;
cb();
};

var options = {
filePaths: ['app.css', 'app.css.br'],
brotliCompressedFilePaths: ['app.css.br'],
cwd: process.cwd() + '/tests/fixtures/dist',
prefix: 'js-app',
acl: 'public-read',
bucket: 'some-bucket',
cacheControl: 'max-age=1234, public',
expires: '2010'
};

var promises = subject.upload(options);

return assert.isFulfilled(promises)
.then(function () {
assert.equal(s3Params.ContentType, 'text/css; charset=utf-8');
assert.equal(s3Params.Key, 'js-app/app.css.br');
assert.equal(s3Params.ContentEncoding, 'br');
assert.equal(s3Params.CacheControl, 'max-age=1234, public');
assert.equal(s3Params.Expires, '2010');
});
});

it('sets the content type using defaultMimeType', function() {
var s3Params;
s3Client.putObject = function(params, cb) {
Expand Down
Loading

0 comments on commit 488cee0

Please sign in to comment.