-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #178 from AndrewBarba/master
Automatic Compression
- Loading branch information
Showing
6 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use strict' | ||
|
||
/** | ||
* Lightweight web framework for your serverless applications | ||
* @author Jeremy Daly <jeremy@jeremydaly.com> | ||
* @license MIT | ||
*/ | ||
|
||
const zlib = require('zlib') | ||
|
||
exports.compress = (input,headers) => { | ||
const acceptEncodingHeader = headers['accept-encoding'] || '' | ||
const acceptableEncodings = new Set(acceptEncodingHeader.toLowerCase().split(',').map(str => str.trim())) | ||
|
||
// Handle Brotli compression (Only supported in Node v10 and later) | ||
if (acceptableEncodings.has('br') && typeof zlib.brotliCompressSync === 'function') { | ||
return { | ||
data: zlib.brotliCompressSync(input), | ||
contentEncoding: 'br' | ||
} | ||
} | ||
|
||
// Handle Gzip compression | ||
if (acceptableEncodings.has('gzip')) { | ||
return { | ||
data: zlib.gzipSync(input), | ||
contentEncoding: 'gzip' | ||
} | ||
} | ||
|
||
// Handle deflate compression | ||
if (acceptableEncodings.has('deflate')) { | ||
return { | ||
data: zlib.deflateSync(input), | ||
contentEncoding: 'deflate' | ||
} | ||
} | ||
|
||
return { | ||
data: input, | ||
contentEncoding: null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters