Skip to content

Commit ed78244

Browse files
Merge pull request #97 from simonihmig/brotli
Add brotli support
2 parents 135311c + 3c099b5 commit ed78244

File tree

4 files changed

+60
-12
lines changed

4 files changed

+60
-12
lines changed

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ module.exports = {
3030
gzippedFiles: function(context) {
3131
return context.gzippedFiles || [];
3232
},
33+
brotliCompressedFiles: function(context) {
34+
return context.brotliCompressedFiles || [];
35+
},
3336
allowOverwrite: false
3437
},
3538

@@ -44,6 +47,7 @@ module.exports = {
4447
var distDir = this.readConfig('distDir');
4548
var filePattern = this.readConfig('filePattern');
4649
var gzippedFiles = this.readConfig('gzippedFiles');
50+
var brotliCompressedFiles = this.readConfig('brotliCompressedFiles');
4751
var allowOverwrite = this.readConfig('allowOverwrite');
4852
var serverSideEncryption = this.readConfig('serverSideEncryption');
4953
var filePath = joinUriSegments(distDir, filePattern);
@@ -57,6 +61,7 @@ module.exports = {
5761
filePath: filePath,
5862
revisionKey: revisionKey,
5963
gzippedFilePaths: gzippedFiles,
64+
brotliCompressedFilePaths: brotliCompressedFiles,
6065
allowOverwrite: allowOverwrite
6166
};
6267

lib/s3.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,20 @@ module.exports = CoreObject.extend({
4848
},
4949

5050
upload: function(options) {
51-
var client = this._client;
52-
var plugin = this._plugin;
53-
var bucket = options.bucket;
54-
var acl = options.acl;
55-
var cacheControl = options.cacheControl;
56-
var allowOverwrite = options.allowOverwrite;
57-
var key = options.filePattern + ":" + options.revisionKey;
58-
var revisionKey = joinUriSegments(options.prefix, key);
59-
var putObject = RSVP.denodeify(client.putObject.bind(client));
60-
var gzippedFilePaths = options.gzippedFilePaths || [];
61-
var isGzipped = gzippedFilePaths.indexOf(options.filePattern) !== -1;
62-
var serverSideEncryption = options.serverSideEncryption;
51+
var client = this._client;
52+
var plugin = this._plugin;
53+
var bucket = options.bucket;
54+
var acl = options.acl;
55+
var cacheControl = options.cacheControl;
56+
var allowOverwrite = options.allowOverwrite;
57+
var key = options.filePattern + ":" + options.revisionKey;
58+
var revisionKey = joinUriSegments(options.prefix, key);
59+
var putObject = RSVP.denodeify(client.putObject.bind(client));
60+
var gzippedFilePaths = options.gzippedFilePaths || [];
61+
var brotliCompressedFilePaths = options.brotliCompressedFilePaths || [];
62+
var isGzipped = gzippedFilePaths.indexOf(options.filePattern) !== -1;
63+
var isBrotliCompressed = brotliCompressedFilePaths.indexOf(options.filePattern) !== -1;
64+
var serverSideEncryption = options.serverSideEncryption;
6365

6466
var params = {
6567
Bucket: bucket,
@@ -77,6 +79,10 @@ module.exports = CoreObject.extend({
7779
params.ContentEncoding = 'gzip';
7880
}
7981

82+
if (isBrotliCompressed) {
83+
params.ContentEncoding = 'br';
84+
}
85+
8086
return this.fetchRevisions(options)
8187
.then(function(revisions) {
8288
var found = revisions.map(function(element) { return element.revision; }).indexOf(options.revisionKey);

tests/unit/index-test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ describe('s3-index plugin', function() {
117117
filePattern: DEFAULT_FILE_PATTERN,
118118
filePath: DIST_DIR+'/'+DEFAULT_FILE_PATTERN,
119119
gzippedFilePaths: [],
120+
brotliCompressedFilePaths: [],
120121
revisionKey: REVISION_KEY,
121122
allowOverwrite: false
122123
};
@@ -160,6 +161,7 @@ describe('s3-index plugin', function() {
160161
filePattern: DEFAULT_FILE_PATTERN,
161162
filePath: DIST_DIR+'/'+DEFAULT_FILE_PATTERN,
162163
gzippedFilePaths: [],
164+
brotliCompressedFilePaths: [],
163165
revisionKey: REVISION_KEY,
164166
allowOverwrite: false
165167
};
@@ -183,6 +185,31 @@ describe('s3-index plugin', function() {
183185
filePattern: DEFAULT_FILE_PATTERN,
184186
filePath: DIST_DIR+'/'+DEFAULT_FILE_PATTERN,
185187
gzippedFilePaths: ['index.html'],
188+
brotliCompressedFilePaths: [],
189+
revisionKey: REVISION_KEY,
190+
allowOverwrite: false
191+
};
192+
193+
assert.deepEqual(s3Options, expected);
194+
});
195+
});
196+
197+
it('passes brotliCompressedFilePaths to S3 based on the `context.brotliCompressedFiles` that ember-cli-deploy-compress provides', function() {
198+
context.brotliCompressedFiles = ['index.html'];
199+
200+
var promise = plugin.upload(context);
201+
202+
return assert.isFulfilled(promise)
203+
.then(function() {
204+
var expected = {
205+
acl: DEFAULT_ACL,
206+
cacheControl: DEFAULT_CACHE_CONTROL,
207+
bucket: BUCKET,
208+
prefix: DEFAULT_PREFIX,
209+
filePattern: DEFAULT_FILE_PATTERN,
210+
filePath: DIST_DIR+'/'+DEFAULT_FILE_PATTERN,
211+
gzippedFilePaths: [],
212+
brotliCompressedFilePaths: ['index.html'],
186213
revisionKey: REVISION_KEY,
187214
allowOverwrite: false
188215
};

tests/unit/lib/s3-test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,16 @@ describe('s3', function() {
167167
});
168168
});
169169

170+
it('sets the Content-Encoding header to br when the index file is brotli compressed', function() {
171+
options.brotliCompressedFilePaths = [filePattern];
172+
var promise = subject.upload(options);
173+
174+
return assert.isFulfilled(promise)
175+
.then(function() {
176+
assert.equal(s3Params.ContentEncoding, 'br', 'contentEncoding is set to br');
177+
});
178+
});
179+
170180
it('allows `prefix` option to be passed to customize upload-path', function() {
171181
var prefix = 'my-app';
172182

0 commit comments

Comments
 (0)