-
Notifications
You must be signed in to change notification settings - Fork 51
Issue: Add Response Compression Middleware #244
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
Merged
OlufunbiIK
merged 1 commit into
OlufunbiIK:main
from
robertocarlous:feat/Add-Response-Compression-Middleware
Mar 23, 2026
Merged
Changes from all commits
Commits
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,24 @@ | ||
| # Response Compression Measurements | ||
|
|
||
| This document captures before/after response sizes for the high-volume JSON response categories covered by the compression integration tests in `test/compression-integration.e2e-spec.ts`. | ||
|
|
||
| ## Measurement setup | ||
|
|
||
| - Payloads: representative list responses generated for track listings, tip history, analytics, search, and activity feed. | ||
| - Compression config: brotli (quality 4), gzip fallback, threshold `1024` bytes. | ||
| - Method: compare uncompressed JSON size against compressed payload size for the same response body. | ||
|
|
||
| ## Results | ||
|
|
||
| | Endpoint Category | Uncompressed (bytes) | Gzip (bytes) | Gzip Reduction | Brotli (bytes) | Brotli Reduction | | ||
| | --- | ---: | ---: | ---: | ---: | ---: | | ||
| | Track listings | 39,791 | 2,048 | 94.85% | 1,094 | 97.25% | | ||
| | Tip history | 41,231 | 2,062 | 95.00% | 1,055 | 97.44% | | ||
| | Analytics | 40,751 | 2,071 | 94.92% | 1,066 | 97.38% | | ||
| | Search results | 40,031 | 2,055 | 94.87% | 1,105 | 97.24% | | ||
| | Activity feed | 40,511 | 2,066 | 94.90% | 1,065 | 97.37% | | ||
|
|
||
| ## Acceptance check | ||
|
|
||
| - All target large-list responses exceed the required 40% size reduction. | ||
| - Brotli consistently outperforms gzip for these payloads. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
43 changes: 43 additions & 0 deletions
43
backend/src/common/middleware/response-compression.middleware.ts
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,43 @@ | ||
| import * as compression from 'compression'; | ||
| import * as zlib from 'zlib'; | ||
| import { Request, Response } from 'express'; | ||
|
|
||
| type BinaryEndpointRule = { | ||
| method: string; | ||
| pattern: RegExp; | ||
| }; | ||
|
|
||
| const BINARY_ENDPOINT_RULES: BinaryEndpointRule[] = [ | ||
| { method: 'POST', pattern: /\/files\/upload\/?$/ }, | ||
| { method: 'GET', pattern: /\/files\/[^/]+\/stream\/?$/ }, | ||
| { method: 'GET', pattern: /\/files\/[^/]+\/?$/ }, | ||
| ]; | ||
|
|
||
| function isBinaryEndpoint(method: string, requestPath: string): boolean { | ||
| return BINARY_ENDPOINT_RULES.some((rule) => { | ||
| return rule.method === method.toUpperCase() && rule.pattern.test(requestPath); | ||
| }); | ||
| } | ||
|
|
||
| export function shouldCompress(req: Request, res: Response): boolean { | ||
| const method = req.method ?? ''; | ||
| const path = req.path ?? req.url ?? ''; | ||
|
|
||
| if (isBinaryEndpoint(method, path)) { | ||
| return false; | ||
| } | ||
|
|
||
| return compression.filter(req as any, res as any); | ||
| } | ||
|
|
||
| export function createCompressionMiddleware() { | ||
| return compression({ | ||
| threshold: 1024, | ||
| filter: shouldCompress as compression.CompressionFilter, | ||
| brotli: { | ||
| params: { | ||
| [zlib.constants.BROTLI_PARAM_QUALITY]: 4, | ||
| }, | ||
| }, | ||
| }); | ||
| } | ||
10 changes: 10 additions & 0 deletions
10
backend/src/common/middleware/vary-accept-encoding.middleware.ts
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,10 @@ | ||
| import { Injectable, NestMiddleware } from '@nestjs/common'; | ||
| import { NextFunction, Request, Response } from 'express'; | ||
|
|
||
| @Injectable() | ||
| export class VaryAcceptEncodingMiddleware implements NestMiddleware { | ||
| use(_req: Request, res: Response, next: NextFunction): void { | ||
| res.vary('Accept-Encoding'); | ||
| next(); | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
🧩 Analysis chain
🏁 Script executed:
Repository: OlufunbiIK/tip-tune
Length of output: 696
🏁 Script executed:
Repository: OlufunbiIK/tip-tune
Length of output: 1515
🏁 Script executed:
rg -n "createCompressionMiddleware\|compression" backend/src/main.ts -B 3 -A 3Repository: OlufunbiIK/tip-tune
Length of output: 45
🏁 Script executed:
rg -n "compression" backend/src -A 2 -B 2Repository: OlufunbiIK/tip-tune
Length of output: 1733
🏁 Script executed:
rg -n "app.use" backend/src/main.ts -B 2 -A 2Repository: OlufunbiIK/tip-tune
Length of output: 455
🏁 Script executed:
Repository: OlufunbiIK/tip-tune
Length of output: 2205
🏁 Script executed:
Repository: OlufunbiIK/tip-tune
Length of output: 45
🏁 Script executed:
cat -n backend/src/storage/storage.controller.ts | head -60Repository: OlufunbiIK/tip-tune
Length of output: 2215
🏁 Script executed:
Repository: OlufunbiIK/tip-tune
Length of output: 45
🏁 Script executed:
Repository: OlufunbiIK/tip-tune
Length of output: 45
🏁 Script executed:
Repository: OlufunbiIK/tip-tune
Length of output: 585
🏁 Script executed:
Repository: OlufunbiIK/tip-tune
Length of output: 692
🏁 Script executed:
Repository: OlufunbiIK/tip-tune
Length of output: 173
Binary endpoint patterns must include the
/apiglobal prefix to match requests.The compression middleware is registered via
app.use()at line 22 ofmain.ts, which operates on the full request path including the global prefix. SincesetGlobalPrefix('api')is applied at line 45, all StorageController routes become/api/files/.... However, the current patterns inBINARY_ENDPOINT_RULESonly match/files/...without the prefix.When a client requests
/api/files/upload, the pattern/\/files\/upload\/?$/will not match, causing binary endpoints to be compressed when they should not be.🐛 Proposed fix
The optional segments handle the
api/prefix and URI versioning segment (v1/, etc.) configured inmain.ts.📝 Committable suggestion
🤖 Prompt for AI Agents