Skip to content

feat: add gzip/brotli response compression middleware#242

Open
aojomo wants to merge 1 commit into
Neurowealth:mainfrom
aojomo:feat/response-compression
Open

feat: add gzip/brotli response compression middleware#242
aojomo wants to merge 1 commit into
Neurowealth:mainfrom
aojomo:feat/response-compression

Conversation

@aojomo

@aojomo aojomo commented Jun 26, 2026

Copy link
Copy Markdown

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.ts

  • Configures the compression package with a 1 KB threshold — responses smaller than this are sent uncompressed since the overhead outweighs the savings
  • Brotli enabled at quality level 4 (good ratio/CPU balance) when client sends Accept-Encoding: br
  • /metrics excluded via a filter callback — Prometheus scraper expects raw text/plain and manages its own transport encoding

Modified: src/index.ts

  • Compression middleware wired globally after CORS and before body parsers/route handlers, ensuring all API responses are eligible

Modified: src/middleware/index.ts

  • Barrel export added for compressionMiddleware

Modified: scripts/smoke-health.sh

  • Compression smoke check added — verifies server returns HTTP 200 when Accept-Encoding: gzip, deflate, br is sent; fails CI with a ::error:: annotation otherwise

Modified: .env.example

  • Documentation note added — no env vars required, compression is always active

New: tests/compression.test.ts

  • 8 self-contained unit tests (no env vars / DB required)

Test 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

  • Compression middleware applied globally before route handlers
  • Brotli used when client advertises support
  • /metrics excluded from compression
  • Smoke test verifies compression with Accept-Encoding header

Dependencies

Package Type Version
compression runtime ^1.8.1
@types/compression dev ^1.8.1

@drips-wave

drips-wave Bot commented Jun 26, 2026

Copy link
Copy Markdown

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., Closes #123), or by clicking a button below:

Issue Title
#218 Add HTTP response compression (gzip/brotli) Link to this issue

ℹ️ Learn more about linking PRs to issues

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.

1 participant