-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support compression based on accept-encoding header
- Loading branch information
1 parent
f0dd13b
commit 209deee
Showing
4 changed files
with
59 additions
and
0 deletions.
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
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.split(',').map(str => str.trim())) | ||
|
||
// Handle Brotli compression | ||
if (acceptableEncodings.has('br')) { | ||
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