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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: flask-squeeze
3
- Version: 3.2.0
3
+ Version: 3.3.1
4
4
  Summary: Compress and minify Flask responses!
5
5
  Author-email: Marcel Kröker <kroeker.marcel@gmail.com>
6
6
  License: MIT License
@@ -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], tuple[str, bytes]]
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
- raise ValueError(f"Invalid encoding choice {encode_choice} for {resource_type} resource at {request.path}")
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(response.get_data(as_text=False), minify_choice)
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
- response.get_data(as_text=False),
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
- data = response.get_data(as_text=False)
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
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: flask-squeeze
3
- Version: 3.2.0
3
+ Version: 3.3.1
4
4
  Summary: Compress and minify Flask responses!
5
5
  Author-email: Marcel Kröker <kroeker.marcel@gmail.com>
6
6
  License: MIT License
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "flask-squeeze"
3
- version = "3.2.0"
3
+ version = "3.3.1"
4
4
  description = "Compress and minify Flask responses!"
5
5
  readme = "README.md"
6
6
  authors = [{ name = "Marcel Kröker", email = "kroeker.marcel@gmail.com" }]
File without changes
File without changes
File without changes