Skip to content

[core] Implement Lambda streaming with custom HTTP headers #521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Jul 24, 2025

Conversation

sebsto
Copy link
Contributor

@sebsto sebsto commented Jun 29, 2025

Fix #520

@sebsto sebsto self-assigned this Jun 29, 2025
@sebsto sebsto added the ⚠️ semver/major Breaks existing public API. label Jun 29, 2025
@sebsto sebsto changed the title [WIP] do not merge : work in progress to implement Lambda streaming with custom HTTP headers [WIP] DO NOT MERGE : work in progress to implement Lambda streaming with custom HTTP headers Jun 30, 2025
@sebsto sebsto changed the title [WIP] DO NOT MERGE : work in progress to implement Lambda streaming with custom HTTP headers [WIP] DO NOT MERGE : Implement Lambda streaming with custom HTTP headers Jun 30, 2025
@sebsto sebsto changed the title [WIP] DO NOT MERGE : Implement Lambda streaming with custom HTTP headers [WIP] DO NOT MERGE : [core] Implement Lambda streaming with custom HTTP headers Jul 2, 2025
@sebsto sebsto marked this pull request as draft July 15, 2025 10:51
@sebsto sebsto force-pushed the sebsto/streaming+http-headers branch from 970ecef to b6fb60c Compare July 16, 2025 07:06
@sebsto sebsto changed the title [WIP] DO NOT MERGE : [core] Implement Lambda streaming with custom HTTP headers [core] Implement Lambda streaming with custom HTTP headers Jul 16, 2025
@sebsto sebsto requested a review from Copilot July 16, 2025 07:11
@sebsto sebsto added 🆕 semver/minor Adds new public API. and removed ⚠️ semver/major Breaks existing public API. labels Jul 16, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

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 implements Lambda streaming functionality with custom HTTP headers, allowing developers to set HTTP status codes and headers before streaming response bodies. This addresses the need for more control over HTTP response metadata in streaming Lambda functions.

  • Adds StreamingLambdaStatusAndHeadersResponse struct for configuring HTTP status and headers
  • Implements writeStatusAndHeaders extension method on LambdaResponseStreamWriter
  • Updates documentation and examples to demonstrate the new streaming capabilities

Reviewed Changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
readme.md Adds documentation and examples for streaming with HTTP headers
Sources/AWSLambdaRuntime/LambdaResponseStreamWriter+Headers.swift Core implementation of headers functionality with response struct and extension methods
Tests/AWSLambdaRuntimeTests/LambdaResponseStreamWriter+HeadersTests.swift Comprehensive test suite covering functionality, error handling, and integration scenarios
Examples/Streaming/Sources/main.swift Updated streaming examples demonstrating headers usage and conditional responses
Examples/Streaming/README.md Enhanced documentation explaining header usage and streaming patterns

@sebsto sebsto marked this pull request as ready for review July 19, 2025 22:11
Copy link
Member

@adam-fowler adam-fowler left a comment

Choose a reason for hiding this comment

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

Some comments

public let statusCode: Int

/// Dictionary of single-value HTTP headers
public let headers: [String: String]?
Copy link
Member

Choose a reason for hiding this comment

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

Any reason we aren't use swift-http-types here?

Copy link
Contributor Author

@sebsto sebsto Jul 21, 2025

Choose a reason for hiding this comment

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

Because there is no dependency on HTTPTypes in this library and I did not want to pull a new dependency just for this. The data format is well controlled. It's between the Swift runtime and the Lambda data plane.

/// Writes the HTTP status code and headers to the response stream.
///
/// This method serializes the status and headers as JSON and writes them to the stream,
/// followed by eight null bytes as a separator before the response body.
Copy link
Member

Choose a reason for hiding this comment

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

Is there any documentation anywhere we can link to that provides a reason for writing 8 null bytes

Copy link
Contributor Author

@sebsto sebsto Jul 21, 2025

Choose a reason for hiding this comment

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

This is undocumented at the moment I received the information from an SDM in the Lambda service team and I have confirmed by looking at the NodeJS runtime implementation.
https://github.com/aws/aws-lambda-nodejs-runtime-interface-client/blob/main/src/HttpResponseStream.js

The value of the "magic" header is here
https://github.com/aws/aws-lambda-nodejs-runtime-interface-client/blob/a5ae1c2a92708e81c9df4949c60fd9e1e6e46bed/src/HttpResponseStream.js#L17

The 8 x 0 bytes are defined here
https://github.com/aws/aws-lambda-nodejs-runtime-interface-client/blob/a5ae1c2a92708e81c9df4949c60fd9e1e6e46bed/src/HttpResponseStream.js#L26

BTW, at the moment, the NodeJS runtime is the only runtime supporting this capability. When we will release the Swift runtime, it will be second to offer this possibility, before all the AWS managed runtime

@sebsto
Copy link
Contributor Author

sebsto commented Jul 23, 2025

@adam-fowler ready to give it another review

Copy link
Member

@adam-fowler adam-fowler left a comment

Choose a reason for hiding this comment

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

One minor thing

@sebsto sebsto merged commit 9287d56 into swift-server:main Jul 24, 2025
31 of 33 checks passed
@sebsto sebsto deleted the sebsto/streaming+http-headers branch July 24, 2025 11:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🆕 semver/minor Adds new public API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[core] Runtime does not support AWS Lambda Streaming specification
2 participants