-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat: add br & zstd to supported compression 🗜️ algos
#4549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
77e8ffd
3b168b5
0d8cdd4
a302743
f3cb451
16d3e98
36db0b6
a5ba404
b26b41a
ec58da7
b5f50e6
3a64184
0f05108
6792069
7888aad
9a4336d
31c1b4b
08bf768
22fac0d
2163c18
df96b0f
d5e7fa8
af8b50b
498ba9f
f5607aa
89429f0
7b29979
dd88ffc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,33 +5,87 @@ const Zlib = require('zlib'); | |
| const Accept = require('@hapi/accept'); | ||
| const Bounce = require('@hapi/bounce'); | ||
| const Hoek = require('@hapi/hoek'); | ||
| const Boom = require('@hapi/boom'); | ||
|
|
||
| const defaultBrotliOptions = { | ||
| params: { | ||
| [Zlib.constants.BROTLI_PARAM_QUALITY]: 4 | ||
| } | ||
| }; | ||
|
|
||
| const defaultZstdOptions = { | ||
| params: { | ||
| [Zlib.constants.ZSTD_c_compressionLevel]: 6 | ||
| } | ||
| }; | ||
|
|
||
| const internals = { | ||
| common: ['gzip, deflate', 'deflate, gzip', 'gzip', 'deflate', 'gzip, deflate, br'] | ||
| common: [ | ||
| 'gzip, deflate, br, zstd', | ||
| 'gzip, deflate, br', | ||
| 'zstd', | ||
| 'br', | ||
| 'gzip, deflate', | ||
| 'deflate, gzip', | ||
| 'gzip', | ||
| 'deflate' | ||
| ], | ||
| provision: new Map([ | ||
| ['zstd', [ | ||
| (options = {}) => Zlib.createZstdCompress(Hoek.applyToDefaults(defaultZstdOptions, options)), | ||
| (options) => Zlib.createZstdDecompress(options) | ||
| ]], | ||
| ['br', [ | ||
| (options = {}) => Zlib.createBrotliCompress(Hoek.applyToDefaults(defaultBrotliOptions, options)), | ||
| (options) => Zlib.createBrotliDecompress(options) | ||
| ]], | ||
| ['deflate', [ | ||
| (options) => Zlib.createDeflate(options), | ||
| (options) => Zlib.createInflate(options) | ||
| ]], | ||
| ['gzip', [ | ||
| (options) => Zlib.createGzip(options), | ||
| (options) => Zlib.createGunzip(options) | ||
| ]] | ||
| ]) | ||
| }; | ||
|
|
||
|
|
||
| exports = module.exports = internals.Compression = class { | ||
|
|
||
| decoders = { | ||
| gzip: (options) => Zlib.createGunzip(options), | ||
| deflate: (options) => Zlib.createInflate(options) | ||
| }; | ||
| decoders = {}; | ||
|
|
||
| encodings = ['identity', 'gzip', 'deflate']; | ||
| encodings = ['identity']; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this change accounted for somewhere else? Is this intentional? This could change existing behavior.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All |
||
|
|
||
| encoders = { | ||
| identity: null, | ||
| gzip: (options) => Zlib.createGzip(options), | ||
| deflate: (options) => Zlib.createDeflate(options) | ||
| identity: null | ||
| }; | ||
|
|
||
| #common = null; | ||
| #options = null; | ||
|
|
||
| constructor() { | ||
| constructor(options) { | ||
|
|
||
| this._updateCommons(); | ||
| this.#options = options; | ||
| if (!this.#options) { | ||
| return this._updateCommons(); | ||
| } | ||
|
|
||
| for (const [encoding, [encoder, decoder]] of internals.provision.entries()) { | ||
| const conditions = this.#options?.encodings?.[encoding]; | ||
| if (conditions) { | ||
| this.addEncoder(encoding, encoder); | ||
| if (this.#options.decompress !== false) { | ||
| this.addDecoder(encoding, decoder); | ||
| } | ||
| else { | ||
| this.addDecoder(encoding, () => { | ||
|
|
||
| throw Boom.unsupportedMediaType(); | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| _updateCommons() { | ||
|
|
@@ -89,13 +143,13 @@ exports = module.exports = internals.Compression = class { | |
| return null; | ||
| } | ||
|
|
||
| const request = response.request; | ||
| if (!request._core.settings.compression || | ||
| length !== null && length < request._core.settings.compression.minBytes) { | ||
| if (!this.#options || | ||
| length !== null && length < this.#options.minBytes) { | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| const request = response.request; | ||
| const mime = request._core.mime.type(response.headers['content-type'] || 'application/octet-stream'); | ||
| if (!mime.compressible) { | ||
| return null; | ||
|
|
@@ -116,4 +170,10 @@ exports = module.exports = internals.Compression = class { | |
| Hoek.assert(encoder !== undefined, `Unknown encoding ${encoding}`); | ||
| return encoder(request.route.settings.compression[encoding]); | ||
| } | ||
|
|
||
| setPriority(priority) { | ||
|
|
||
| this.encodings = [...new Set([...priority, ...this.encodings])]; | ||
| this._updateCommons(); | ||
| } | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It makes sense to update the common encoding with
'gzip, deflate, br, zstd', as this is used by both Chrome and Firefox. This can go in a separate PR.