Skip to content

Commit 197206c

Browse files
committed
Expose streaming API
1 parent fd0354e commit 197206c

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

lambda-http/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ use std::{
101101
};
102102

103103
mod streaming;
104-
pub use streaming::run_with_streaming_response;
104+
pub use streaming::{into_streaming_response, run_with_streaming_response};
105105

106106
/// Type alias for `http::Request`s with a fixed [`Body`](enum.Body.html) type
107107
pub type Request = http::Request<Body>;

lambda-http/src/streaming.rs

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,33 @@ use bytes::Bytes;
33
pub use http::{self, Response};
44
use http_body::Body;
55
use lambda_runtime::Diagnostic;
6-
pub use lambda_runtime::{self, tower::ServiceExt, Error, LambdaEvent, MetadataPrelude, Service, StreamResponse};
6+
pub use lambda_runtime::{
7+
self,
8+
tower::util::{MapRequest, MapResponse},
9+
tower::ServiceExt,
10+
Error, LambdaEvent, MetadataPrelude, Service, StreamResponse,
11+
};
712
use std::{
813
fmt::Debug,
914
pin::Pin,
1015
task::{Context, Poll},
1116
};
1217
use tokio_stream::Stream;
1318

14-
/// Starts the Lambda Rust runtime and stream response back [Configure Lambda
15-
/// Streaming Response](https://docs.aws.amazon.com/lambda/latest/dg/configuration-response-streaming.html).
19+
/// Converts a handler into a streaming-compatible service for use with AWS
20+
/// Lambda.
1621
///
17-
/// This takes care of transforming the LambdaEvent into a [`Request`] and
18-
/// accepts [`http::Response<http_body::Body>`] as response.
19-
pub async fn run_with_streaming_response<'a, S, B, E>(handler: S) -> Result<(), Error>
22+
/// This function wraps a `Service` implementation, transforming its input and
23+
/// output to be compatible with AWS Lambda's streaming response feature. It
24+
/// provides the necessary middleware to handle `LambdaEvent` requests and
25+
/// converts the `http::Response` into a `StreamResponse` containing a metadata
26+
/// prelude and body stream.
27+
pub fn into_streaming_response<'a, S, B, E>(
28+
handler: S,
29+
) -> MapResponse<
30+
MapRequest<S, impl FnMut(LambdaEvent<LambdaRequest>) -> Request>,
31+
impl FnOnce(Response<B>) -> StreamResponse<BodyStream<B>> + Clone,
32+
>
2033
where
2134
S: Service<Request, Response = Response<B>, Error = E>,
2235
S::Future: Send + 'a,
@@ -25,13 +38,13 @@ where
2538
B::Data: Into<Bytes> + Send,
2639
B::Error: Into<Error> + Send + Debug,
2740
{
28-
let svc = ServiceBuilder::new()
41+
ServiceBuilder::new()
2942
.map_request(|req: LambdaEvent<LambdaRequest>| {
3043
let event: Request = req.payload.into();
3144
event.with_lambda_context(req.context)
3245
})
3346
.service(handler)
34-
.map_response(|res| {
47+
.map_response(|res: Response<B>| {
3548
let (parts, body) = res.into_parts();
3649

3750
let mut prelude_headers = parts.headers;
@@ -54,8 +67,25 @@ where
5467
metadata_prelude,
5568
stream: BodyStream { body },
5669
}
57-
});
70+
})
71+
}
5872

73+
/// Starts the Lambda Rust runtime and stream response back [Configure Lambda
74+
/// Streaming
75+
/// Response](https://docs.aws.amazon.com/lambda/latest/dg/configuration-response-streaming.html).
76+
///
77+
/// This takes care of transforming the LambdaEvent into a [`Request`] and
78+
/// accepts [`http::Response<http_body::Body>`] as response.
79+
pub async fn run_with_streaming_response<'a, S, B, E>(handler: S) -> Result<(), Error>
80+
where
81+
S: Service<Request, Response = Response<B>, Error = E>,
82+
S::Future: Send + 'a,
83+
E: Debug + Into<Diagnostic>,
84+
B: Body + Unpin + Send + 'static,
85+
B::Data: Into<Bytes> + Send,
86+
B::Error: Into<Error> + Send + Debug,
87+
{
88+
let svc = into_streaming_response(handler);
5989
lambda_runtime::run(svc).await
6090
}
6191

0 commit comments

Comments
 (0)