Skip to content

Conversation

@JacobCoffee
Copy link
Owner

Summary

  • Add brotli (br) decompression support for toolbar injection
  • Add zstd decompression support for toolbar injection
  • Both are optional dependencies - gracefully skips injection when unavailable
  • Extends existing gzip support to cover all Litestar compression middleware types

Related: litestar-org/litestar#4286

Test plan

  • Local CI passes (make ci - tests pass, pre-existing type issues unrelated)
  • GHA CI passes
  • Existing gzip compression tests cover middleware behavior

🤖 Generated with Claude Code

Extends compression handling to support all compression types used by
Litestar's compression middleware:
- gzip (existing)
- brotli (br) - optional, requires brotli package
- zstd - optional, requires zstandard package

Gracefully skips toolbar injection when compression libraries are unavailable.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings January 3, 2026 17:26
@github-actions
Copy link
Contributor

github-actions bot commented Jan 3, 2026

PR Preview Action v1.6.3

🚀 View preview at
https://JacobCoffee.github.io/debug-toolbar/pr-preview/pr-37/

Built to branch gh-pages at 2026-01-03 17:26 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends the debug toolbar's compression support to handle brotli and zstd encoding in addition to the existing gzip support, aligning with Litestar's compression middleware capabilities.

Key Changes:

  • Added brotli (br) decompression support with graceful ImportError handling
  • Added zstd decompression support with graceful ImportError handling
  • Updated documentation and logging messages to reflect support for multiple compression formats

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

except ImportError:
logger.debug("zstandard not installed, skipping toolbar injection for zstd-encoded response")
return body, content_encoding
except Exception:
Copy link

Copilot AI Jan 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The broad exception handler catches all exceptions from zstandard decompression. This can mask legitimate programming errors or unexpected issues. Consider catching specific exceptions like zstandard.ZstdError to handle only expected decompression failures, while allowing unexpected errors to propagate appropriately.

Suggested change
except Exception:
except zstandard.ZstdError:

Copilot uses AI. Check for mistakes.
Comment on lines +505 to +522
try:
import brotli # type: ignore[import-untyped]

body = brotli.decompress(body)
decompressed = True
except ImportError:
logger.debug("Brotli not installed, skipping toolbar injection for br-encoded response")
return body, content_encoding
except Exception:
logger.debug("Invalid brotli data, attempting to decode as-is")

elif "zstd" in encodings:
try:
import zstandard # type: ignore[import-untyped]

dctx = zstandard.ZstdDecompressor()
body = dctx.decompress(body)
decompressed = True
Copy link

Copilot AI Jan 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The brotli and zstandard libraries are imported inline within a try-except block on every request that uses these compression methods. Consider importing these at the module level with a try-except to set flags (e.g., HAS_BROTLI, HAS_ZSTANDARD), then check those flags before attempting decompression. This would improve performance by avoiding repeated import attempts on every request and make the availability check more efficient.

Copilot uses AI. Check for mistakes.
Comment on lines +504 to +514
elif "br" in encodings:
try:
import brotli # type: ignore[import-untyped]

body = brotli.decompress(body)
decompressed = True
except ImportError:
logger.debug("Brotli not installed, skipping toolbar injection for br-encoded response")
return body, content_encoding
except Exception:
logger.debug("Invalid brotli data, attempting to decode as-is")
Copy link

Copilot AI Jan 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new brotli decompression support lacks test coverage. The existing TestGzipCompression class demonstrates comprehensive testing patterns (valid compression, invalid data, UTF-8 decoding failures, case-insensitive headers) that should be replicated for brotli. Consider adding similar test cases for br-encoded responses to ensure the decompression logic works correctly and handles edge cases appropriately.

Copilot uses AI. Check for mistakes.
Comment on lines +516 to +527
elif "zstd" in encodings:
try:
import zstandard # type: ignore[import-untyped]

dctx = zstandard.ZstdDecompressor()
body = dctx.decompress(body)
decompressed = True
except ImportError:
logger.debug("zstandard not installed, skipping toolbar injection for zstd-encoded response")
return body, content_encoding
except Exception:
logger.debug("Invalid zstd data, attempting to decode as-is")
Copy link

Copilot AI Jan 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new zstd decompression support lacks test coverage. The existing TestGzipCompression class demonstrates comprehensive testing patterns (valid compression, invalid data, UTF-8 decoding failures, case-insensitive headers) that should be replicated for zstd. Consider adding similar test cases for zstd-encoded responses to ensure the decompression logic works correctly and handles edge cases appropriately.

Copilot uses AI. Check for mistakes.
except ImportError:
logger.debug("Brotli not installed, skipping toolbar injection for br-encoded response")
return body, content_encoding
except Exception:
Copy link

Copilot AI Jan 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The broad exception handler catches all exceptions from brotli decompression. This can mask legitimate programming errors or unexpected issues. Consider catching specific exceptions like brotli.error (if available) to handle only expected decompression failures, while allowing unexpected errors to propagate appropriately.

Suggested change
except Exception:
except brotli.error:

Copilot uses AI. Check for mistakes.
@JacobCoffee JacobCoffee merged commit 4d8f3f3 into main Jan 3, 2026
24 checks passed
@JacobCoffee JacobCoffee deleted the fix/compression-support branch January 3, 2026 17:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants