flask-squeeze 3.3.2__tar.gz → 3.3.3__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.3.2
3
+ Version: 3.3.3
4
4
  Summary: Compress and minify Flask responses!
5
5
  Author-email: Marcel Kröker <kroeker.marcel@gmail.com>
6
6
  License: MIT License
@@ -87,7 +87,6 @@ You can configure Flask-Squeeze with the following options in your [Flask config
87
87
  | `SQUEEZE_COMPRESS` | `True` | Enables or disables compression |
88
88
  | `SQUEEZE_MIN_SIZE` | `500` | Defines the minimum file size in bytes to activate the compression |
89
89
  | `SQUEEZE_VERBOSE_LOGGING` | `False` | Enable or disable verbose logging. If enabled, Flask-Squeeze will print what it does into the terminal in a highlighted color |
90
- | `SQUEEZE_ADD_DEBUG_HEADERS` | `False` | Add debug infos into the response headers, like call durations and cache hit infos. ONLY USE THIS IN DEVELOPMENT.
91
90
 
92
91
  ### Minification options
93
92
  | Option | Default | Description |
@@ -47,7 +47,6 @@ You can configure Flask-Squeeze with the following options in your [Flask config
47
47
  | `SQUEEZE_COMPRESS` | `True` | Enables or disables compression |
48
48
  | `SQUEEZE_MIN_SIZE` | `500` | Defines the minimum file size in bytes to activate the compression |
49
49
  | `SQUEEZE_VERBOSE_LOGGING` | `False` | Enable or disable verbose logging. If enabled, Flask-Squeeze will print what it does into the terminal in a highlighted color |
50
- | `SQUEEZE_ADD_DEBUG_HEADERS` | `False` | Add debug infos into the response headers, like call durations and cache hit infos. ONLY USE THIS IN DEVELOPMENT.
51
50
 
52
51
  ### Minification options
53
52
  | Option | Default | Description |
@@ -17,6 +17,16 @@ class CompressionResult:
17
17
  duration: float
18
18
  ratio: float
19
19
 
20
+ @property
21
+ def info(self) -> str:
22
+ return "; ".join(
23
+ [
24
+ f"ratio={self.ratio:.1f}x",
25
+ f"qualtiy={self.quality}",
26
+ f"duration={self.duration * 1000:.1f}ms",
27
+ ],
28
+ )
29
+
20
30
 
21
31
  def compress(
22
32
  data: bytes,
@@ -44,6 +54,4 @@ def compress(
44
54
 
45
55
  def update_response_with_compressed_data(response: Response, result: CompressionResult) -> None:
46
56
  response.set_data(result.data)
47
- response.headers["X-Flask-Squeeze-Compression-Quality"] = str(result.quality)
48
- response.headers["X-Flask-Squeeze-Compression-Duration"] = f"{result.duration * 1000:.1f}ms"
49
- response.headers["X-Flask-Squeeze-Compression-Ratio"] = f"{result.ratio:.1f}x"
57
+ response.headers["X-Flask-Squeeze-Compress"] = result.info
@@ -7,7 +7,6 @@ from flask import Flask, Response, request
7
7
  from flask_squeeze.utils import add_breach_exploit_protection_header, update_response_headers
8
8
 
9
9
  from .compress import compress, update_response_with_compressed_data
10
- from .debug import add_debug_header
11
10
  from .log import d_log, log
12
11
  from .minify import minify, update_response_with_minified_data
13
12
  from .models import (
@@ -53,7 +52,6 @@ class Squeeze:
53
52
  app.config.setdefault("SQUEEZE_MINIFY_HTML", True)
54
53
  # Logging options
55
54
  app.config.setdefault("SQUEEZE_VERBOSE_LOGGING", False)
56
- app.config.setdefault("SQUEEZE_ADD_DEBUG_HEADERS", False)
57
55
 
58
56
  if (
59
57
  app.config["SQUEEZE_COMPRESS"]
@@ -187,7 +185,6 @@ class Squeeze:
187
185
  #### MARK: After Request
188
186
 
189
187
  @d_log(level=0, with_args=[1])
190
- @add_debug_header("X-Flask-Squeeze-Total-Duration")
191
188
  def after_request(self, response: Response) -> Response:
192
189
  log(1, f"Enter after_request({response})")
193
190
 
@@ -15,6 +15,15 @@ class MinificationResult:
15
15
  duration: float
16
16
  ratio: float
17
17
 
18
+ @property
19
+ def info(self) -> str:
20
+ return "; ".join(
21
+ [
22
+ f"ratio={self.ratio:.1f}x",
23
+ f"duration={self.duration * 1000:.1f}ms",
24
+ ],
25
+ )
26
+
18
27
 
19
28
  def minify_html(html_text: str) -> str:
20
29
  """
@@ -97,5 +106,4 @@ def minify(data: bytes, minify_choice: Minification) -> MinificationResult:
97
106
 
98
107
  def update_response_with_minified_data(response: Response, result: MinificationResult) -> None:
99
108
  response.set_data(result.data)
100
- response.headers["X-Flask-Squeeze-Minify-Duration"] = f"{result.duration * 1000:.1f}ms"
101
- response.headers["X-Flask-Squeeze-Minify-Ratio"] = f"{result.ratio:.1f}x"
109
+ response.headers["X-Flask-Squeeze-Minify"] = result.info
@@ -15,7 +15,7 @@ def add_breach_exploit_protection_header(response: Response) -> None:
15
15
  """
16
16
  padding_length = secrets.randbelow(128) + 1
17
17
  rand_str = secrets.token_urlsafe(padding_length)
18
- response.headers["X-Breach-Exploit-Protection-Padding"] = rand_str
18
+ response.headers["X-Flask-Squeeze-Breach-Protection"] = rand_str
19
19
 
20
20
 
21
21
  def update_response_headers(
@@ -38,4 +38,3 @@ def update_response_headers(
38
38
 
39
39
  if original_content_length != response.content_length:
40
40
  response.headers["Content-Length"] = response.content_length
41
- response.headers["X-Uncompressed-Content-Length"] = original_content_length
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: flask-squeeze
3
- Version: 3.3.2
3
+ Version: 3.3.3
4
4
  Summary: Compress and minify Flask responses!
5
5
  Author-email: Marcel Kröker <kroeker.marcel@gmail.com>
6
6
  License: MIT License
@@ -87,7 +87,6 @@ You can configure Flask-Squeeze with the following options in your [Flask config
87
87
  | `SQUEEZE_COMPRESS` | `True` | Enables or disables compression |
88
88
  | `SQUEEZE_MIN_SIZE` | `500` | Defines the minimum file size in bytes to activate the compression |
89
89
  | `SQUEEZE_VERBOSE_LOGGING` | `False` | Enable or disable verbose logging. If enabled, Flask-Squeeze will print what it does into the terminal in a highlighted color |
90
- | `SQUEEZE_ADD_DEBUG_HEADERS` | `False` | Add debug infos into the response headers, like call durations and cache hit infos. ONLY USE THIS IN DEVELOPMENT.
91
90
 
92
91
  ### Minification options
93
92
  | Option | Default | Description |
@@ -3,7 +3,6 @@ README.md
3
3
  pyproject.toml
4
4
  flask_squeeze/__init__.py
5
5
  flask_squeeze/compress.py
6
- flask_squeeze/debug.py
7
6
  flask_squeeze/flask_squeeze.py
8
7
  flask_squeeze/log.py
9
8
  flask_squeeze/minify.py
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "flask-squeeze"
3
- version = "3.3.2"
3
+ version = "3.3.3"
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" }]
@@ -110,7 +110,6 @@ def test_get_jquery_no_minify(client: FlaskClient) -> None:
110
110
  assert content_length_correct(r_orig)
111
111
  assert "Content-Encoding" not in r_orig.headers
112
112
  assert r_orig.headers["Content-Length"] == "292458"
113
- assert "X-Uncompressed-Content-Length" not in r_orig.headers
114
113
 
115
114
 
116
115
  def test_get_from_cache(client: FlaskClient, use_encoding: str) -> None:
@@ -144,7 +143,7 @@ def test_get_same_repeatedly(client: FlaskClient) -> None:
144
143
 
145
144
 
146
145
  def response_has_breach_header(r: Response) -> bool:
147
- return "X-Breach-Exploit-Protection-Padding" in r.headers
146
+ return "X-Flask-Squeeze-Breach-Protection" in r.headers
148
147
 
149
148
 
150
149
  def response_has_vary_header(r: Response) -> bool:
@@ -173,14 +172,14 @@ def test_disable_minification(client: FlaskClient) -> None:
173
172
  },
174
173
  )
175
174
  r = client.get("/static/jquery.js", headers={"Accept-Encoding": "gzip"})
176
- assert "X-Flask-Squeeze-Minify-Duration" not in r.headers
175
+ assert "X-Flask-Squeeze-Minify" not in r.headers
177
176
 
178
177
 
179
178
  def test_html_response_minification(client: FlaskClient) -> None:
180
179
  """Test minification of HTML responses."""
181
180
  client.application.config.update({"SQUEEZE_MINIFY_HTML": True})
182
181
  r = client.get("/", headers={"Accept-Encoding": "gzip"})
183
- assert "X-Flask-Squeeze-Minify-Duration" in r.headers
182
+ assert "X-Flask-Squeeze-Minify" in r.headers, r.headers
184
183
 
185
184
 
186
185
  def test_breach_header_presence(client: FlaskClient) -> None:
@@ -200,18 +199,10 @@ def test_minification_and_compression_together(client: FlaskClient) -> None:
200
199
  client.application.config.update({"SQUEEZE_MINIFY_CSS": True})
201
200
  r = client.get("/static/fomantic.css", headers={"Accept-Encoding": "gzip"})
202
201
  assert "Content-Encoding" in r.headers
203
- assert "X-Flask-Squeeze-Minify-Duration" in r.headers
202
+ assert "X-Flask-Squeeze-Minify" in r.headers
204
203
  assert content_length_correct(r)
205
204
 
206
205
 
207
- def test_debug_headers_presence(client: FlaskClient) -> None:
208
- """Test if debug headers are added when enabled."""
209
- client.application.config.update({"SQUEEZE_ADD_DEBUG_HEADERS": True})
210
- r = client.get("/static/fomantic.css", headers={"Accept-Encoding": "gzip"})
211
- assert "X-Flask-Squeeze-Total-Duration" in r.headers
212
- assert "X-Flask-Squeeze-Compress-Duration" in r.headers or "X-Flask-Squeeze-Minify-Duration" in r.headers
213
-
214
-
215
206
  def test_no_minify_no_compress(client: FlaskClient) -> None:
216
207
  client.application.config.update(
217
208
  {
@@ -226,15 +217,15 @@ def test_no_minify_no_compress(client: FlaskClient) -> None:
226
217
 
227
218
  r = client.get("/static/fomantic.css", headers={"Accept-Encoding": "gzip"})
228
219
  assert "Content-Encoding" not in r.headers
229
- assert "X-Flask-Squeeze-Minify-Duration" not in r.headers
230
- assert "X-Flask-Squeeze-Compression-Duration" not in r.headers
220
+ assert "X-Flask-Squeeze-Minify" not in r.headers
221
+ assert "X-Flask-Squeeze-Compress" not in r.headers
231
222
 
232
223
  # Dynamic file with no minification or compression
233
224
 
234
225
  r = client.get("/", headers={"Accept-Encoding": "gzip"})
235
226
  assert "Content-Encoding" not in r.headers
236
- assert "X-Flask-Squeeze-Minify-Duration" not in r.headers
237
- assert "X-Flask-Squeeze-Compression-Duration" not in r.headers
227
+ assert "X-Flask-Squeeze-Minify" not in r.headers
228
+ assert "X-Flask-Squeeze-Compress" not in r.headers
238
229
 
239
230
 
240
231
  def test_static_file_cache_behavior(
@@ -1,47 +0,0 @@
1
- from __future__ import annotations
2
-
3
- import functools
4
- import time
5
- from typing import TYPE_CHECKING, Any
6
-
7
- from flask import Response, current_app
8
-
9
- if TYPE_CHECKING:
10
- from collections.abc import Callable
11
-
12
-
13
- def _write_benchmark_debug_header(response: Response, header_name: str, t1: float, t2: float) -> None:
14
- if current_app.config["SQUEEZE_ADD_DEBUG_HEADERS"]:
15
- dur_ms_str = f"{(t2 - t1) * 1000:.1f}ms"
16
- response.headers[header_name] = dur_ms_str
17
-
18
-
19
- class add_debug_header: # noqa: N801
20
- def __init__(self, header_name: str) -> None:
21
- self.header_name = header_name
22
-
23
- def get_response(self, args: tuple, kwargs: dict) -> Response:
24
- for arg in args:
25
- if isinstance(arg, Response):
26
- return arg
27
- for kwarg in kwargs.values():
28
- if isinstance(kwarg, Response):
29
- return kwarg
30
- msg = "You can only deocrate a function with 'add_debug_header' that has a Response as an argument or kwarg."
31
- raise ValueError(msg)
32
-
33
- def __call__(self, method: Callable) -> Callable:
34
- @functools.wraps(method)
35
- def wrapper(*args: tuple, **kwargs: dict[str, Any]):
36
- if not current_app.config["SQUEEZE_ADD_DEBUG_HEADERS"]:
37
- return method(*args, **kwargs)
38
-
39
- response = self.get_response(args, kwargs)
40
- t1 = time.perf_counter()
41
- res = method(*args, **kwargs)
42
- t2 = time.perf_counter()
43
-
44
- _write_benchmark_debug_header(response, self.header_name, t1, t2)
45
- return res
46
-
47
- return wrapper
File without changes
File without changes