Skip to content

Commit

Permalink
Change brotli compression to an opt-in feature due to its excessive C…
Browse files Browse the repository at this point in the history
…PU usage.
  • Loading branch information
AndrewBarba committed Jul 7, 2021
1 parent d762d86 commit 26ab7d6
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1411,7 +1411,15 @@ const api = require('lambda-api')({
});
```

The response will automatically be compressed based on the `Accept-Encoding` header in the request. Supported compressions are Brotli, Gzip and Deflate - in that priority order.
The response will automatically be compressed based on the `Accept-Encoding` header in the request. Supported compressions are Gzip and Deflate, with opt-in support for Brotli:

```javascript
const api = require('lambda-api')({
compression: ['br', 'gzip'],
});
```

> Note: Brotli compression is significantly slower than Gzip due to its CPU intensive algorithm. Please test extensively before enabling on a production environment.
For full control over the response compression, instantiate the API with `isBase64` set to true, and a custom serializer that returns a compressed response as a base64 encoded string. Also, don't forget to set the correct `content-encoding` header:

Expand Down
2 changes: 1 addition & 1 deletion __tests__/responses.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const api4 = require('../index')({
// Init API with compression
const api5 = require('../index')({
version: 'v1.0',
compression: true
compression: ['br', 'gzip', 'deflate']
})

let event = {
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class API {
? props.headers
: {};
this._compression =
props && typeof props.compression === 'boolean'
props && (typeof props.compression === 'boolean' || Array.isArray(props.compression))
? props.compression
: false;

Expand Down
10 changes: 7 additions & 3 deletions lib/compression.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

const zlib = require('zlib');

exports.compress = (input, headers) => {
const defaultEnabledEcodings = ['gzip', 'deflate'];

exports.compress = (input, headers, _enabledEncodings) => {
const enabledEncodings = new Set(_enabledEncodings || defaultEnabledEcodings);
const acceptEncodingHeader = headers['accept-encoding'] || '';
const acceptableEncodings = new Set(
acceptEncodingHeader
Expand All @@ -20,6 +23,7 @@ exports.compress = (input, headers) => {
// Handle Brotli compression (Only supported in Node v10 and later)
if (
acceptableEncodings.has('br') &&
enabledEncodings.has('br') &&
typeof zlib.brotliCompressSync === 'function'
) {
return {
Expand All @@ -29,15 +33,15 @@ exports.compress = (input, headers) => {
}

// Handle Gzip compression
if (acceptableEncodings.has('gzip')) {
if (acceptableEncodings.has('gzip') && enabledEncodings.has('gzip')) {
return {
data: zlib.gzipSync(input),
contentEncoding: 'gzip',
};
}

// Handle deflate compression
if (acceptableEncodings.has('deflate')) {
if (acceptableEncodings.has('deflate') && enabledEncodings.has('deflate')) {
return {
data: zlib.deflateSync(input),
contentEncoding: 'deflate',
Expand Down
3 changes: 2 additions & 1 deletion lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,8 @@ class RESPONSE {
if (this._compression && this._response.body) {
const { data, contentEncoding } = compression.compress(
this._response.body,
this._request.headers
this._request.headers,
Array.isArray(this._compression) ? this._compression : null
);
if (contentEncoding) {
Object.assign(this._response, {
Expand Down

0 comments on commit 26ab7d6

Please sign in to comment.