Skip to content
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

Add config options for brotli.compress #10

Merged
merged 5 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Change Log for `flask-compress`
All notable changes to `flask-compress` will be documented in this file.

## [Unreleased]
### Added
- The following parameters to control Brotli compression are now available: `COMPRESSION_BR_MODE`, `COMPRESSION_BR_QUALITY`, `COMPRESS_BR_WINDOW`, `COMPRESSION_BR_BLOCK`. [#10](https://github.com/colour-science/flask-compress/pull/10)

### Changed
- The default quality level for Brotli is now 6, which provides compression comparable to `gzip` at the default setting, while reducing the time required versus the Brotli default of 11.

## [1.6.0] - 2020-10-05
### Added
- Support for multiple compression algorithms and quality factors [#7](https://github.com/colour-science/flask-compress/pull/7)

### Changed
- Modified default compression settings to use Brotli when available before `gzip`

## [1.5.0] - 2020-05-09

## [1.4.0] - 2017-01-04

## [1.3.2] - 2016-09-28

## [1.3.1] - 2016-09-21

## [1.3.0] - 2015-10-08

## [1.2.1] - 2015-06-02

## [1.2.0] - 2015-03-27

## [1.1.1] - 2015-03-24

## [1.1.0] - 2015-02-16

## [1.0.2] - 2014-04-19

## [1.0.2] - 2014-04-19

## [1.0.1] - 2014-03-04
### Changed
- Temporarily remove test for vary header until it is fixed.

## [1.0.0] - 2013-10-28

## [0.10.0] - 2013-08-15

## [0.9.0] - 2013-08-14
### Fixed
- Fixed a runtime error when `direct_passthrough` is used.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,7 @@ Within your Flask application's settings you can provide the following settings
| `COMPRESS_CACHE_BACKEND` | Specified the backend for storing the cached response data. | `None` |
| `COMPRESS_REGISTER` | Specifies if compression should be automatically registered. | `True` |
| `COMPRESS_ALGORITHM` | Supported compression algorithms. | `['br', 'gzip']` |
| `COMPRESS_BR_MODE` | For Brotli, the compression mode. The options are 0, 1, or 2. These correspond to "generic", "text" (for UTF-8 input), and "font" (for WOFF 2.0). | `0` |
| `COMPRESS_BR_QUALITY` | For Brotli, the desired compression level. Proivdes control over the speed/compression density tradeoff. Higher values provide better compression at the cost of compression time. Ranges from 0 to 11. | `4` |
| `COMPRESS_BR_WINDOW` | For Brotli, this specifies the base-2 logarithm of the sliding window size. Ranges from 10 to 24. | `22` |
| `COMPRESS_BR_BLOCK` | For Brotli, this provides the base-2 logarithm of the maximum input block size. If zero is provided, value will be determined based on the quality. Ranges from 16 to 24. | `0` |
10 changes: 9 additions & 1 deletion flask_compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ def init_app(self, app):
'application/json',
'application/javascript']),
('COMPRESS_LEVEL', 6),
('COMPRESS_BR_MODE', 0),
('COMPRESS_BR_QUALITY', 4),
('COMPRESS_BR_WINDOW', 22),
('COMPRESS_BR_BLOCK', 0),
('COMPRESS_MIN_SIZE', 500),
('COMPRESS_CACHE_KEY', None),
('COMPRESS_CACHE_BACKEND', None),
Expand Down Expand Up @@ -199,4 +203,8 @@ def compress(self, app, response, algorithm):
gzip_file.write(response.get_data())
return gzip_buffer.getvalue()
elif algorithm == 'br':
return brotli.compress(response.get_data())
return brotli.compress(response.get_data(),
mode=app.config['COMPRESS_BR_MODE'],
quality=app.config['COMPRESS_BR_QUALITY'],
lgwin=app.config['COMPRESS_BR_WINDOW'],
lgblock=app.config['COMPRESS_BR_BLOCK'])
28 changes: 28 additions & 0 deletions tests/test_flask_compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ def test_algorithm_default(self):
""" Tests COMPRESS_ALGORITHM default value is correctly set. """
self.assertEqual(self.app.config['COMPRESS_ALGORITHM'], ['br', 'gzip'])

def test_mode_default(self):
""" Tests COMPRESS_BR_MODE default value is correctly set. """
self.assertEqual(self.app.config['COMPRESS_BR_MODE'], 0)

def test_quality_level_default(self):
""" Tests COMPRESS_BR_QUALITY default value is correctly set. """
self.assertEqual(self.app.config['COMPRESS_BR_QUALITY'], 4)

def test_window_size_default(self):
""" Tests COMPRESS_BR_WINDOW default value is correctly set. """
self.assertEqual(self.app.config['COMPRESS_BR_WINDOW'], 22)

def test_block_size_default(self):
""" Tests COMPRESS_BR_BLOCK default value is correctly set. """
self.assertEqual(self.app.config['COMPRESS_BR_BLOCK'], 0)

class InitTests(unittest.TestCase):
def setUp(self):
Expand Down Expand Up @@ -116,6 +131,19 @@ def test_content_length_options(self):
response = client.options('/small/', headers=headers)
self.assertEqual(response.status_code, 200)

def test_quality_level(self):
""" Tests that COMPRESS_BR_QUALITY correctly affects response data. """
self.app.config['COMPRESS_BR_QUALITY'] = 4
client = self.app.test_client()
response = client.get('/large/', headers=[('Accept-Encoding', 'br')])
response4_size = len(response.data)

self.app.config['COMPRESS_BR_QUALITY'] = 11
client = self.app.test_client()
response = client.get('/large/', headers=[('Accept-Encoding', 'br')])
response11_size = len(response.data)

self.assertNotEqual(response4_size, response11_size)

class CompressionAlgoTests(unittest.TestCase):
"""
Expand Down