CodeGenieApp / CodeGenieApp/serverless-express
Feat: Add AWS Lambda Response Streaming support
- Dominant language
- JavaScript
- Stars
- 5.3k
- Forks
- 676
- PR merge metrics
- No merged PRs in 30d
Description
## Problem / Motivation
Currently, `@codegenie/serverless-express` buffers the entire Express response in memory before transforming it into the standard Lambda proxy response object. This architecture introduces two main limitations:
1. **6 MB Payload Limit:** We are bound by the strict 6 MB limit for synchronous Lambda responses.
2. **TTFB (Time to First Byte):** For large payloads (like large JSON datasets, file downloads, or SSR pages), the client has to wait until the entire Express response is fully buffered before receiving any data.
AWS supports **Lambda Response Streaming**, which allows functions to stream payloads back to the client, bypassing the 6 MB limit (up to 20 MB) and drastically improving TTFB.
Ref: https://aws.amazon.com/blogs/compute/introducing-aws-lambda-response-streaming/
## Proposed Solution
We can introduce support for response streaming by extending the `serverlessExpress` instance signature.
Since the existing implementation can accept a `callback` function, we should introduce a backward-compatible overloading (type checking) to accept the `responseStream` object either as the **third argument** (when `callback` is omitted in async handlers) or as the **fourth argument** (if a callback is present).
Under the hood, the package will detect the presence of the `responseStream` object and stream/pipe the Express `res` object directly into it instead of buffering the response.
## Proposed Usage Idea
Here is how the implementation will look from the user's perspective using the standard `async/await` pattern (which is the default for Lambda response streaming):
```typescript
import serverlessExpress from '@codegenie/serverless-express';
import app from './app';
const serverlessExpressInstance = serverlessExpress({ app });
export const handler = awslambda.streamifyHandler(
async (event, responseStream, context) => {
// Overloading: passing responseStream as the 3rd argument
return serverlessExpressInstance(event, context, responseStream);
}
);
Contributor guide
Assessment
This issue has not been assessed yet.