feat: add gzip/brotli response compression middleware#242
Open
aojomo wants to merge 1 commit into
Open
Conversation
|
Hey @aojomo! 👋 It looks like this PR isn't linked to any issue. If this PR is for one of the issues assigned to you as part of a Wave, please link it to ensure your contribution is tracked properly. You can do this by adding a keyword to the PR description (e.g.,
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
API responses (analytics, transaction history, agent logs) were being returned uncompressed, wasting bandwidth and increasing latency — particularly on mobile connections.
This PR adds a global gzip + brotli response compression middleware to address that.
Changes
New:
src/middleware/compression.tscompressionpackage with a 1 KB threshold — responses smaller than this are sent uncompressed since the overhead outweighs the savingsAccept-Encoding: br/metricsexcluded via afiltercallback — Prometheus scraper expects rawtext/plainand manages its own transport encodingModified:
src/index.tsModified:
src/middleware/index.tscompressionMiddlewareModified:
scripts/smoke-health.shAccept-Encoding: gzip, deflate, bris sent; fails CI with a::error::annotation otherwiseModified:
.env.exampleNew:
tests/compression.test.tsTest Results
PASS tests/compression.test.ts
compressionMiddleware
gzip compression
✓ returns Content-Encoding: gzip on responses > 1 KB
✓ decompresses correctly — response body is valid JSON
brotli compression
✓ returns Content-Encoding: br on responses > 1 KB when br is advertised
✓ prefers br over gzip when both are advertised
threshold (< 1 KB)
✓ does NOT add Content-Encoding for responses under 1 KB
/metrics path exclusion
✓ does NOT add Content-Encoding for /metrics even when gzip is advertised
✓ does NOT add Content-Encoding for /metrics when br is advertised
✓ still serves /metrics content correctly when compression is excluded
Tests: 8 passed, 8 total
Acceptance Criteria
/metricsexcluded from compressionAccept-EncodingheaderDependencies
compression^1.8.1@types/compression^1.8.1