flask-squeeze 3.2.0__tar.gz → 3.3.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {flask_squeeze-3.2.0 → flask_squeeze-3.3.1}/PKG-INFO +1 -1
- {flask_squeeze-3.2.0 → flask_squeeze-3.3.1}/flask_squeeze/compress.py +0 -8
- {flask_squeeze-3.2.0 → flask_squeeze-3.3.1}/flask_squeeze/flask_squeeze.py +19 -10
- {flask_squeeze-3.2.0 → flask_squeeze-3.3.1}/flask_squeeze.egg-info/PKG-INFO +1 -1
- {flask_squeeze-3.2.0 → flask_squeeze-3.3.1}/pyproject.toml +1 -1
- {flask_squeeze-3.2.0 → flask_squeeze-3.3.1}/LICENSE +0 -0
- {flask_squeeze-3.2.0 → flask_squeeze-3.3.1}/README.md +0 -0
- {flask_squeeze-3.2.0 → flask_squeeze-3.3.1}/flask_squeeze/__init__.py +0 -0
- {flask_squeeze-3.2.0 → flask_squeeze-3.3.1}/flask_squeeze/debug.py +0 -0
- {flask_squeeze-3.2.0 → flask_squeeze-3.3.1}/flask_squeeze/log.py +0 -0
- {flask_squeeze-3.2.0 → flask_squeeze-3.3.1}/flask_squeeze/minify.py +0 -0
- {flask_squeeze-3.2.0 → flask_squeeze-3.3.1}/flask_squeeze/models.py +0 -0
- {flask_squeeze-3.2.0 → flask_squeeze-3.3.1}/flask_squeeze/utils.py +0 -0
- {flask_squeeze-3.2.0 → flask_squeeze-3.3.1}/flask_squeeze.egg-info/SOURCES.txt +0 -0
- {flask_squeeze-3.2.0 → flask_squeeze-3.3.1}/flask_squeeze.egg-info/dependency_links.txt +0 -0
- {flask_squeeze-3.2.0 → flask_squeeze-3.3.1}/flask_squeeze.egg-info/requires.txt +0 -0
- {flask_squeeze-3.2.0 → flask_squeeze-3.3.1}/flask_squeeze.egg-info/top_level.txt +0 -0
- {flask_squeeze-3.2.0 → flask_squeeze-3.3.1}/setup.cfg +0 -0
- {flask_squeeze-3.2.0 → flask_squeeze-3.3.1}/tests/test_flask_squeeze.py +0 -0
|
@@ -48,11 +48,3 @@ def update_response_with_compressed_data(response: Response, result: Compression
|
|
|
48
48
|
response.headers["X-Flask-Squeeze-Compression-Quality"] = str(result.quality)
|
|
49
49
|
response.headers["X-Flask-Squeeze-Compression-Duration"] = f"{result.duration * 1000:.1f}ms"
|
|
50
50
|
response.headers["X-Flask-Squeeze-Compression-Ratio"] = f"{result.ratio:.1f}x"
|
|
51
|
-
|
|
52
|
-
# Set the Content-Encoding and Vary headers
|
|
53
|
-
|
|
54
|
-
response.headers["Content-Encoding"] = result.encoding.value
|
|
55
|
-
vary = {x.strip() for x in response.headers.get("Vary", "").split(",")}
|
|
56
|
-
vary.add("Accept-Encoding")
|
|
57
|
-
vary.discard("")
|
|
58
|
-
response.headers["Vary"] = ",".join(vary)
|
|
@@ -22,7 +22,7 @@ class Squeeze:
|
|
|
22
22
|
__slots__ = "app", "cache_static"
|
|
23
23
|
app: Flask
|
|
24
24
|
|
|
25
|
-
cache_static: Dict[Tuple[str, str],
|
|
25
|
+
cache_static: Dict[Tuple[str, str], Tuple[str, bytes]]
|
|
26
26
|
""" (request.path, encoding) -> (original file sha256 hash, compressed bytes) """
|
|
27
27
|
|
|
28
28
|
def __init__(self, app: Union[Flask, None] = None) -> None:
|
|
@@ -62,8 +62,8 @@ class Squeeze:
|
|
|
62
62
|
):
|
|
63
63
|
app.after_request(self.after_request)
|
|
64
64
|
|
|
65
|
-
# Helpers
|
|
66
65
|
####################################################################################
|
|
66
|
+
#### MARK: Utils
|
|
67
67
|
|
|
68
68
|
def get_configured_quality(self, encode_choice: Encoding, resource_type: ResourceType) -> int:
|
|
69
69
|
options = {
|
|
@@ -76,7 +76,8 @@ class Squeeze:
|
|
|
76
76
|
}
|
|
77
77
|
|
|
78
78
|
if not (option := options.get((encode_choice or Encoding.gzip, resource_type))):
|
|
79
|
-
|
|
79
|
+
msg = f"Invalid encoding choice {encode_choice} for {resource_type} resource at {request.path}"
|
|
80
|
+
raise ValueError(msg)
|
|
80
81
|
|
|
81
82
|
return self.app.config[option]
|
|
82
83
|
|
|
@@ -84,6 +85,7 @@ class Squeeze:
|
|
|
84
85
|
self,
|
|
85
86
|
response: Response,
|
|
86
87
|
original_content_length: int,
|
|
88
|
+
encode_choice: Union[Encoding, None],
|
|
87
89
|
) -> None:
|
|
88
90
|
"""
|
|
89
91
|
Set the Content-Length header if it has changed.
|
|
@@ -92,6 +94,13 @@ class Squeeze:
|
|
|
92
94
|
if response.direct_passthrough:
|
|
93
95
|
return
|
|
94
96
|
|
|
97
|
+
if encode_choice is not None:
|
|
98
|
+
response.headers["Content-Encoding"] = encode_choice.value
|
|
99
|
+
vary = {x.strip() for x in response.headers.get("Vary", "").split(",")}
|
|
100
|
+
vary.add("Accept-Encoding")
|
|
101
|
+
vary.discard("")
|
|
102
|
+
response.headers["Vary"] = ",".join(vary)
|
|
103
|
+
|
|
95
104
|
if original_content_length != response.content_length:
|
|
96
105
|
response.headers["Content-Length"] = response.content_length
|
|
97
106
|
response.headers["X-Uncompressed-Content-Length"] = original_content_length
|
|
@@ -152,7 +161,7 @@ class Squeeze:
|
|
|
152
161
|
current_sha256 = hashlib.sha256(current_data).hexdigest()
|
|
153
162
|
|
|
154
163
|
if current_sha256 == original_sha256:
|
|
155
|
-
log(2, "Found in cache. RETURN")
|
|
164
|
+
log(2, "Found in cache, hashes match. RETURN")
|
|
156
165
|
response.set_data(compressed_data)
|
|
157
166
|
response.headers["X-Flask-Squeeze-Cache"] = "HIT"
|
|
158
167
|
return
|
|
@@ -160,22 +169,22 @@ class Squeeze:
|
|
|
160
169
|
log(2, "File has changed.")
|
|
161
170
|
|
|
162
171
|
if minify_choice is not None:
|
|
163
|
-
minfication_result = minify(
|
|
172
|
+
minfication_result = minify(current_data, minify_choice)
|
|
164
173
|
update_response_with_minified_data(response, minfication_result)
|
|
174
|
+
current_data = minfication_result.data
|
|
165
175
|
if encode_choice is not None:
|
|
166
176
|
compression_result = compress(
|
|
167
|
-
|
|
177
|
+
current_data,
|
|
168
178
|
encode_choice,
|
|
169
179
|
self.get_configured_quality(encode_choice, ResourceType.static),
|
|
170
180
|
)
|
|
171
181
|
update_response_with_compressed_data(response, compression_result)
|
|
182
|
+
current_data = compression_result.data
|
|
172
183
|
|
|
173
184
|
# Assert: At least one of minify or compress was run
|
|
174
185
|
|
|
175
186
|
response.headers["X-Flask-Squeeze-Cache"] = "MISS"
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
self.cache_static[(request.path, encode_choice_str)] = (current_sha256, data)
|
|
187
|
+
self.cache_static[(request.path, encode_choice_str)] = (current_sha256, current_data)
|
|
179
188
|
|
|
180
189
|
# Not in cache, compress and minify
|
|
181
190
|
|
|
@@ -248,6 +257,6 @@ class Squeeze:
|
|
|
248
257
|
else:
|
|
249
258
|
self.run_dynamic(response, encode_choice, minify_choice)
|
|
250
259
|
|
|
251
|
-
self.recompute_headers(response, original_content_length)
|
|
260
|
+
self.recompute_headers(response, original_content_length, encode_choice)
|
|
252
261
|
log(1, f"Static cache: {self.cache_static.keys()}")
|
|
253
262
|
return response
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|