Skip to content

Commit

Permalink
Merge pull request #97 from simonihmig/brotli
Browse files Browse the repository at this point in the history
Add brotli support
  • Loading branch information
LevelbossMike authored Sep 10, 2020
2 parents 135311c + 3c099b5 commit ed78244
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ module.exports = {
gzippedFiles: function(context) {
return context.gzippedFiles || [];
},
brotliCompressedFiles: function(context) {
return context.brotliCompressedFiles || [];
},
allowOverwrite: false
},

Expand All @@ -44,6 +47,7 @@ module.exports = {
var distDir = this.readConfig('distDir');
var filePattern = this.readConfig('filePattern');
var gzippedFiles = this.readConfig('gzippedFiles');
var brotliCompressedFiles = this.readConfig('brotliCompressedFiles');
var allowOverwrite = this.readConfig('allowOverwrite');
var serverSideEncryption = this.readConfig('serverSideEncryption');
var filePath = joinUriSegments(distDir, filePattern);
Expand All @@ -57,6 +61,7 @@ module.exports = {
filePath: filePath,
revisionKey: revisionKey,
gzippedFilePaths: gzippedFiles,
brotliCompressedFilePaths: brotliCompressedFiles,
allowOverwrite: allowOverwrite
};

Expand Down
30 changes: 18 additions & 12 deletions lib/s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,20 @@ module.exports = CoreObject.extend({
},

upload: function(options) {
var client = this._client;
var plugin = this._plugin;
var bucket = options.bucket;
var acl = options.acl;
var cacheControl = options.cacheControl;
var allowOverwrite = options.allowOverwrite;
var key = options.filePattern + ":" + options.revisionKey;
var revisionKey = joinUriSegments(options.prefix, key);
var putObject = RSVP.denodeify(client.putObject.bind(client));
var gzippedFilePaths = options.gzippedFilePaths || [];
var isGzipped = gzippedFilePaths.indexOf(options.filePattern) !== -1;
var serverSideEncryption = options.serverSideEncryption;
var client = this._client;
var plugin = this._plugin;
var bucket = options.bucket;
var acl = options.acl;
var cacheControl = options.cacheControl;
var allowOverwrite = options.allowOverwrite;
var key = options.filePattern + ":" + options.revisionKey;
var revisionKey = joinUriSegments(options.prefix, key);
var putObject = RSVP.denodeify(client.putObject.bind(client));
var gzippedFilePaths = options.gzippedFilePaths || [];
var brotliCompressedFilePaths = options.brotliCompressedFilePaths || [];
var isGzipped = gzippedFilePaths.indexOf(options.filePattern) !== -1;
var isBrotliCompressed = brotliCompressedFilePaths.indexOf(options.filePattern) !== -1;
var serverSideEncryption = options.serverSideEncryption;

var params = {
Bucket: bucket,
Expand All @@ -77,6 +79,10 @@ module.exports = CoreObject.extend({
params.ContentEncoding = 'gzip';
}

if (isBrotliCompressed) {
params.ContentEncoding = 'br';
}

return this.fetchRevisions(options)
.then(function(revisions) {
var found = revisions.map(function(element) { return element.revision; }).indexOf(options.revisionKey);
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ describe('s3-index plugin', function() {
filePattern: DEFAULT_FILE_PATTERN,
filePath: DIST_DIR+'/'+DEFAULT_FILE_PATTERN,
gzippedFilePaths: [],
brotliCompressedFilePaths: [],
revisionKey: REVISION_KEY,
allowOverwrite: false
};
Expand Down Expand Up @@ -160,6 +161,7 @@ describe('s3-index plugin', function() {
filePattern: DEFAULT_FILE_PATTERN,
filePath: DIST_DIR+'/'+DEFAULT_FILE_PATTERN,
gzippedFilePaths: [],
brotliCompressedFilePaths: [],
revisionKey: REVISION_KEY,
allowOverwrite: false
};
Expand All @@ -183,6 +185,31 @@ describe('s3-index plugin', function() {
filePattern: DEFAULT_FILE_PATTERN,
filePath: DIST_DIR+'/'+DEFAULT_FILE_PATTERN,
gzippedFilePaths: ['index.html'],
brotliCompressedFilePaths: [],
revisionKey: REVISION_KEY,
allowOverwrite: false
};

assert.deepEqual(s3Options, expected);
});
});

it('passes brotliCompressedFilePaths to S3 based on the `context.brotliCompressedFiles` that ember-cli-deploy-compress provides', function() {
context.brotliCompressedFiles = ['index.html'];

var promise = plugin.upload(context);

return assert.isFulfilled(promise)
.then(function() {
var expected = {
acl: DEFAULT_ACL,
cacheControl: DEFAULT_CACHE_CONTROL,
bucket: BUCKET,
prefix: DEFAULT_PREFIX,
filePattern: DEFAULT_FILE_PATTERN,
filePath: DIST_DIR+'/'+DEFAULT_FILE_PATTERN,
gzippedFilePaths: [],
brotliCompressedFilePaths: ['index.html'],
revisionKey: REVISION_KEY,
allowOverwrite: false
};
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/lib/s3-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,16 @@ describe('s3', function() {
});
});

it('sets the Content-Encoding header to br when the index file is brotli compressed', function() {
options.brotliCompressedFilePaths = [filePattern];
var promise = subject.upload(options);

return assert.isFulfilled(promise)
.then(function() {
assert.equal(s3Params.ContentEncoding, 'br', 'contentEncoding is set to br');
});
});

it('allows `prefix` option to be passed to customize upload-path', function() {
var prefix = 'my-app';

Expand Down

0 comments on commit ed78244

Please sign in to comment.