Skip to content

Feature request: Add compress middleware for Event Handler REST API #4474

@svozza

Description

@svozza

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.

Metadata

Metadata

Assignees

Labels

completedThis item is complete and has been merged/shippedevent-handlerThis item relates to the Event Handler Utilityfeature-requestThis item refers to a feature request for an existing or new utility

Type

No type

Projects

Status

Shipped

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions