-
Notifications
You must be signed in to change notification settings - Fork 16
feat: Add transport-level compression, remove application-level is_compressed #210
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
Open
tomaz-lc
wants to merge
8
commits into
cli-v2
Choose a base branch
from
feat/transport-compression
base: cli-v2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bbc0c56
feat: Add transport-level compression, remove application-level is_co…
tomaz-lc 3fea868
feat: Make zstandard a hard dependency, add cross-platform GHA CI
tomaz-lc 4751ac6
fix: Skip os.chown on Windows where it is not available
tomaz-lc 4747e6e
fix: Fix GHA CI - use --version flag and cross-platform wheel install
tomaz-lc 36627f0
fix: Skip Unix permission assertion on Windows in test_config.py
tomaz-lc 4d9ad55
feat: Graceful fallback when zstandard is unavailable
tomaz-lc b7b16f4
refactor: Move inline CI scripts to scripts/ci/, add edge case tests
tomaz-lc a579d68
fix: Make tests collect without zstandard installed
tomaz-lc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| name: CI | ||
|
|
||
| on: | ||
| pull_request: | ||
| branches: [cli-v2, master] | ||
| push: | ||
| branches: [cli-v2, master] | ||
|
|
||
| jobs: | ||
| unit-tests: | ||
| name: "Tests - Python ${{ matrix.python-version }} / ${{ matrix.os }}" | ||
| runs-on: ${{ matrix.os }} | ||
| strategy: | ||
| fail-fast: false | ||
| matrix: | ||
| os: [ubuntu-latest, macos-latest, windows-latest] | ||
| python-version: ["3.10", "3.11", "3.12", "3.13"] | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - name: Set up Python ${{ matrix.python-version }} | ||
| uses: actions/setup-python@v5 | ||
| with: | ||
| python-version: ${{ matrix.python-version }} | ||
|
|
||
| - name: Install package | ||
| run: | | ||
| python -m pip install --upgrade pip | ||
| pip install ".[dev]" | ||
|
|
||
| - name: Verify zstandard is installed | ||
| run: python scripts/ci/verify_zstd_installed.py | ||
|
|
||
| - name: Verify transport compression imports | ||
| run: python scripts/ci/verify_transport_compression.py | ||
|
|
||
| - name: Run unit tests | ||
| run: pytest -v --durations=10 tests/unit/ | ||
|
|
||
| wheel-sanity: | ||
| name: "Wheel install - Python ${{ matrix.python-version }} / ${{ matrix.os }}" | ||
| runs-on: ${{ matrix.os }} | ||
| strategy: | ||
| fail-fast: false | ||
| matrix: | ||
| os: [ubuntu-latest, macos-latest, windows-latest] | ||
| python-version: ["3.10", "3.13"] | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - name: Set up Python ${{ matrix.python-version }} | ||
| uses: actions/setup-python@v5 | ||
| with: | ||
| python-version: ${{ matrix.python-version }} | ||
|
|
||
| - name: Build and install wheel | ||
| run: | | ||
| python -m pip install --upgrade pip setuptools wheel build | ||
| python -m build | ||
| python -m pip install --find-links dist limacharlie | ||
|
|
||
| - name: Verify CLI works | ||
| run: limacharlie --version | ||
|
|
||
| - name: Verify zstandard available after wheel install | ||
| run: python scripts/ci/verify_zstd_installed.py | ||
|
|
||
| - name: Verify zstd decompression works end-to-end | ||
| run: python scripts/ci/verify_zstd_decompression.py | ||
|
|
||
| no-zstd-fallback: | ||
| name: "Fallback without zstandard - Python ${{ matrix.python-version }} / ${{ matrix.os }}" | ||
| runs-on: ${{ matrix.os }} | ||
| strategy: | ||
| fail-fast: false | ||
| matrix: | ||
| os: [ubuntu-latest, macos-latest, windows-latest] | ||
| python-version: ["3.10", "3.13"] | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - name: Set up Python ${{ matrix.python-version }} | ||
| uses: actions/setup-python@v5 | ||
| with: | ||
| python-version: ${{ matrix.python-version }} | ||
|
|
||
| - name: Install package then remove zstandard | ||
| run: | | ||
| python -m pip install --upgrade pip | ||
| pip install ".[dev]" | ||
| pip uninstall -y zstandard | ||
|
|
||
| - name: Verify zstandard is NOT importable | ||
| run: python scripts/ci/verify_zstd_not_importable.py | ||
|
|
||
| - name: Verify fallback Accept-Encoding (no zstd) | ||
| run: python scripts/ci/verify_fallback_encoding.py | ||
|
|
||
| - name: Run unit tests without zstandard | ||
| run: pytest -v --durations=10 tests/unit/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| """Transport-level HTTP compression support. | ||
|
|
||
| Handles Accept-Encoding negotiation and Content-Encoding decompression | ||
| for HTTP responses. Supports zstd, gzip, and deflate. | ||
|
|
||
| zstd is preferred because it offers better compression ratios and faster | ||
| decompression than gzip. The zstandard package is a hard dependency but | ||
| the runtime gracefully falls back to gzip/deflate if it's unavailable | ||
| (e.g., exotic platform where the wheel couldn't be installed). | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import zlib | ||
|
|
||
| # zstandard is a hard dependency (listed in pyproject.toml) with pre-built | ||
| # wheels for all major platforms. However, we guard the import so the SDK | ||
| # still works if someone is on an exotic platform where the wheel isn't | ||
| # available and there's no C compiler to build from source. | ||
| try: | ||
| import zstandard as _zstd | ||
|
|
||
| _HAS_ZSTD = True | ||
| except ImportError: | ||
| _zstd = None # type: ignore[assignment] | ||
| _HAS_ZSTD = False | ||
|
|
||
| # Header value sent on every request. Prefer zstd when available. | ||
| ACCEPT_ENCODING: str = "zstd, gzip, deflate" if _HAS_ZSTD else "gzip, deflate" | ||
|
|
||
|
|
||
| def decompress_response(data: bytes, content_encoding: str | None) -> bytes: | ||
| """Decompress an HTTP response body based on Content-Encoding. | ||
|
|
||
| If the encoding is unrecognized or absent, the data is returned as-is | ||
| (passthrough). This matches standard HTTP client behavior - servers may | ||
| return uncompressed responses even when Accept-Encoding was sent. | ||
|
|
||
| Parameters: | ||
| data: Raw response body bytes. | ||
| content_encoding: Value of the Content-Encoding response header, | ||
| or None if the header was absent. | ||
|
|
||
| Returns: | ||
| Decompressed bytes, or the original bytes if no decompression needed. | ||
| """ | ||
| if not content_encoding: | ||
| return data | ||
|
|
||
| encoding = content_encoding.strip().lower() | ||
|
|
||
| if encoding == "zstd": | ||
| if not _HAS_ZSTD: | ||
| # Server sent zstd but we can't decompress - return as-is. | ||
| # JSON parsing will fail with a clear error downstream. | ||
| return data | ||
| return _zstd.ZstdDecompressor().decompress(data) | ||
|
|
||
| if encoding in ("gzip", "x-gzip"): | ||
| # 16 + MAX_WBITS tells zlib to auto-detect gzip vs raw deflate | ||
| return zlib.decompress(data, 16 + zlib.MAX_WBITS) | ||
|
|
||
| if encoding == "deflate": | ||
| # Try raw deflate first, fall back to zlib-wrapped deflate | ||
| try: | ||
| return zlib.decompress(data, -zlib.MAX_WBITS) | ||
| except zlib.error: | ||
| return zlib.decompress(data) | ||
|
|
||
| # Unknown encoding - return data as-is rather than crashing. | ||
| # The caller will attempt JSON parsing which will fail with a clear | ||
| # error if the data is actually compressed. | ||
| return data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| """Verify Accept-Encoding falls back to gzip/deflate when zstd is unavailable.""" | ||
|
|
||
| from limacharlie.transport_compression import ACCEPT_ENCODING, _HAS_ZSTD | ||
|
|
||
| assert not _HAS_ZSTD, "_HAS_ZSTD should be False" | ||
| assert "zstd" not in ACCEPT_ENCODING, f"zstd should not be in: {ACCEPT_ENCODING}" | ||
| assert "gzip" in ACCEPT_ENCODING, f"gzip missing from: {ACCEPT_ENCODING}" | ||
| print(f"Accept-Encoding (fallback): {ACCEPT_ENCODING}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| """Verify transport compression module loads with zstd support.""" | ||
|
|
||
| from limacharlie.transport_compression import ACCEPT_ENCODING | ||
|
|
||
| assert "zstd" in ACCEPT_ENCODING, f"zstd missing from: {ACCEPT_ENCODING}" | ||
| print(f"Accept-Encoding: {ACCEPT_ENCODING}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| """Verify zstd decompression works end-to-end (compress then decompress).""" | ||
|
|
||
| import json | ||
|
|
||
| import zstandard | ||
|
|
||
| from limacharlie.transport_compression import decompress_response | ||
|
|
||
| original = json.dumps({"events": [{"type": "test"}]}).encode() | ||
| compressed = zstandard.ZstdCompressor().compress(original) | ||
| result = decompress_response(compressed, "zstd") | ||
| assert result == original, "zstd round-trip failed" | ||
| print("zstd round-trip OK") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| """Verify zstandard is installed and importable.""" | ||
|
|
||
| import zstandard | ||
|
|
||
| print(f"zstandard {zstandard.__version__} OK") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| """Verify zstandard is NOT importable (used after pip uninstall).""" | ||
|
|
||
| try: | ||
| import zstandard | ||
|
|
||
| raise AssertionError("zstandard should not be importable") | ||
| except ImportError: | ||
| print("zstandard correctly unavailable") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I am -1 on any backward support to old python, we leave it on v1 (v3 to be precise), propose we don't carry any backward compatibiltiy stuff to cli-v2
Suggest you modify it to 1) only act on
cli-v2branch and remove all backward compatibility part - matrix-python-version.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.
I wasn't aware until very recently that
cli-v2branch will be long running for a while.Having said that, since this branch is already targeted towards
cli-v2it should work fine. Once it's merged / takes over the old master, it should work just fine.In short - it won't run against old version, just against the new one.