Feature request: Add compress middleware for Event Handler REST API
Closed this issue · 2 comments
Use case
We should have a middleware that compresses response bodies to reduce payload size and improve performance. The middleware should support common compression algorithms with sensible defaults that can be overridden when initializing the middleware.
Solution/User Experience
The middleware will be applied like any other:
import { compress } from '@aws-lambda-powertools/event-handler/rest/middleware';
// use defaults
app.use(compress());
type options = {
encoding?: 'gzip' | 'deflate'
threshold?: number
}
// custom configuration
app.use(compress({
encoding: 'gzip',
threshold: 1024, // this will also be the default
}));The middleware should:
- Only compress responses above the threshold size
- If
enocdingoption is not present, infer fromAccept-Encodingheader with preference given togzip - Avoid loading the whole request into memory, use streams instead
- Should we use the inbuilt Nodejs compression or CompressionStream. Explain your reasoning.
- Set appropriate Content-Encoding header
- Remove Content-Length header (since compressed size differs)
- Support gzip and deflate algorithms
- Check Accept-Encoding header to ensure client supports compression
- Skip compression for already compressed content types (images, videos, etc.)
Alternative solutions
Acknowledgment
- This feature request meets Powertools for AWS Lambda (TypeScript) Tenets
- Should this be considered in other Powertools for AWS Lambda languages? i.e. Python, Java, and .NET
Future readers
Please react with 👍 and your use case to help us understand customer demand.
Warning
This issue is now closed. Please be mindful that future comments are hard for our team to see.
If you need more assistance, please either reopen the issue, or open a new issue referencing this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.
This is now released under v2.27.0 version!