Skip to content

Refactor AWS Lambda streaming support and enhance converters#37

Open
conico974 wants to merge 2 commits into
mainfrom
conico/mandatory-stream
Open

Refactor AWS Lambda streaming support and enhance converters#37
conico974 wants to merge 2 commits into
mainfrom
conico/mandatory-stream

Conversation

@conico974

Copy link
Copy Markdown
Contributor

Refactor AWS Lambda streaming support by introducing a new converter structure and enhancing existing converters for better handling of direct and streaming output types. Improve memory management by removing the retainChunks option and updating response handling. Add tests for the new aws-streaming converter and ensure compatibility with existing functionality.

- Updated aws-lambda-streaming wrapper to utilize new converter structure.
- Introduced aws-streaming converter for handling streaming responses in AWS Lambda.
- Enhanced existing converters to support direct and streaming output types.
- Modified core types to accommodate new converter output structure.
- Updated validation logic to ensure compatibility with new streaming converter.
- Added tests for aws-streaming converter and SQS revalidate functionality.
- Refactored express-dev and cloudflare wrappers to align with new converter outputs.
- Improved response handling in node and cloudflare-node wrappers.
Copilot AI review requested due to automatic review settings July 15, 2026 15:27
@pkg-pr-new

pkg-pr-new Bot commented Jul 15, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@opennextjs/cloudflare@37

commit: 113b5a0

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the converter/wrapper contract to better support AWS Lambda streaming by introducing a ConverterOutput structure (direct vs stream), removing chunk-retention behavior, and updating wrappers/converters to either stream responses or buffer streams into platform-specific return values. It also adds/updates unit tests to cover the new streaming converter and the updated converter behaviors.

Changes:

  • Introduces ConverterOutput and updates the Converter.convertTo(event, context) API to return either a stream creator (optionally with a deferred platform output) or a direct finalizer.
  • Refactors wrappers (AWS Lambda, AWS Lambda streaming/compressed, Node, Cloudflare) to initialize converter output before invoking the handler and to handle direct vs streamed results consistently.
  • Adds a buffered stream creator utility for AWS API Gateway/CloudFront converters and adds/updates unit tests for the new converter behaviors (including the new aws-streaming converter).

Reviewed changes

Copilot reviewed 31 out of 31 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
packages/tests-unit/tests/core/routing/util.test.ts Updates routing util tests to reflect removal of buffered body exposure.
packages/tests-unit/tests/converters/sqs-revalidate.test.ts Adds unit coverage for direct-output behavior of the SQS revalidate converter.
packages/tests-unit/tests/converters/aws-streaming.test.ts Adds coverage for Lambda streaming prelude + streamed body behavior.
packages/tests-unit/tests/converters/aws-cloudfront.test.ts Refactors tests to validate buffered streaming output path for CloudFront converter.
packages/tests-unit/tests/converters/aws-apigw-v2.test.ts Refactors tests to validate buffered streaming output path for API Gateway v2 converter.
packages/tests-unit/tests/converters/aws-apigw-v1.test.ts Refactors tests to validate buffered streaming output path for API Gateway v1 converter.
packages/core/src/types/overrides.ts Adds ConverterOutput and updates Converter.convertTo signature/semantics.
packages/core/src/types/open-next.ts Makes InternalResult.body optional, removes retainChunks, adds aws-streaming converter option.
packages/core/src/plugins/resolve.ts Adds defaulting behavior to select aws-streaming converter for aws-lambda-streaming wrapper.
packages/core/src/overrides/wrappers/node.ts Updates node wrapper to use new converter output model.
packages/core/src/overrides/wrappers/express-dev.ts Updates dev wrapper to use new converter output model for image/all routes.
packages/core/src/overrides/wrappers/dummy.ts Updates dummy wrapper to use new converter output model.
packages/core/src/overrides/wrappers/cloudflare-node.ts Refactors Cloudflare-node wrapper to rely on converter stream output and return output.
packages/core/src/overrides/wrappers/cloudflare-edge.ts Refactors Cloudflare-edge wrapper to initialize converter output early and support direct exceptional results.
packages/core/src/overrides/converters/node.ts Moves Node converter to provide a StreamCreator via convertTo instead of returning an InternalResult mapping.
packages/core/src/overrides/converters/edge.ts Refactors Edge converter to return streaming output + direct exceptional handler for middleware results.
packages/core/src/overrides/converters/dummy.ts Updates dummy converter to return direct ConverterOutput.
packages/core/src/http/openNextResponse.ts Removes chunk retention and related helpers from OpenNextNodeResponse.
packages/core/src/core/routing/util.ts Updates convertRes to stop synthesizing a body from retained chunks.
packages/core/src/core/requestHandler.ts Adjusts request handler to handle optional bodies and updated convertRes output.
packages/core/src/build/validateConfig.ts Updates wrapper/converter compatibility matrix and defaults for Lambda streaming.
packages/core/src/build/generateOutput.ts Updates default converter selection when wrapper is aws-lambda-streaming.
packages/aws/src/overrides/wrappers/aws-lambda.ts Refactors AWS Lambda wrapper to use new converter output model (direct vs stream).
packages/aws/src/overrides/wrappers/aws-lambda-streaming.ts Refactors Lambda streaming wrapper to delegate streaming response construction to converter.
packages/aws/src/overrides/wrappers/aws-lambda-compressed.ts Refactors compressed wrapper to apply compression via StreamCreator composition.
packages/aws/src/overrides/converters/sqs-revalidate.ts Updates SQS revalidate converter to return direct output with data finalizer.
packages/aws/src/overrides/converters/response-stream.ts Introduces a buffered StreamCreator helper to build platform outputs from streamed writes.
packages/aws/src/overrides/converters/aws-streaming.ts Adds new aws-streaming converter for Lambda HTTP integration streaming prelude + body streaming.
packages/aws/src/overrides/converters/aws-cloudfront.ts Refactors CloudFront converter to buffered streaming output with a middleware direct handler.
packages/aws/src/overrides/converters/aws-apigw-v2.ts Refactors API Gateway v2 converter to buffered streaming output.
packages/aws/src/overrides/converters/aws-apigw-v1.ts Refactors API Gateway v1 converter to buffered streaming output.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +18 to +40
return new Writable({
write(chunk, _encoding, callback) {
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
callback();
},
final(callback) {
if (!prelude) {
const error = new Error("Response stream finished before headers were written");
reject(error);
callback(error);
return;
}
try {
const isBase64Encoded =
isBinaryContentType(prelude.headers["content-type"]) || !!prelude.headers["content-encoding"];
resolve(createOutput(prelude, Buffer.concat(chunks), isBase64Encoded));
callback();
} catch (error: unknown) {
reject(error);
callback(error instanceof Error ? error : new Error(String(error)));
}
},
});
Comment on lines +8 to 13
const output = await converter.convertTo(event, options);
if (output.type === "direct") {
return output.data(await handler(event, options));
}
return handler(event, { ...options, streamCreator: output.streamCreator });
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants