aws-powertools/powertools-lambda-typescript

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 enocding option is not present, infer from Accept-Encoding header with preference given to gzip
  • Avoid loading the whole request into memory, use streams instead
  • 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

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!