@@ -3,20 +3,33 @@ use bytes::Bytes;
3
3
pub use http:: { self , Response } ;
4
4
use http_body:: Body ;
5
5
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
+ } ;
7
12
use std:: {
8
13
fmt:: Debug ,
9
14
pin:: Pin ,
10
15
task:: { Context , Poll } ,
11
16
} ;
12
17
use tokio_stream:: Stream ;
13
18
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 .
16
21
///
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
+ >
20
33
where
21
34
S : Service < Request , Response = Response < B > , Error = E > ,
22
35
S :: Future : Send + ' a ,
@@ -25,13 +38,13 @@ where
25
38
B :: Data : Into < Bytes > + Send ,
26
39
B :: Error : Into < Error > + Send + Debug ,
27
40
{
28
- let svc = ServiceBuilder :: new ( )
41
+ ServiceBuilder :: new ( )
29
42
. map_request ( |req : LambdaEvent < LambdaRequest > | {
30
43
let event: Request = req. payload . into ( ) ;
31
44
event. with_lambda_context ( req. context )
32
45
} )
33
46
. service ( handler)
34
- . map_response ( |res| {
47
+ . map_response ( |res : Response < B > | {
35
48
let ( parts, body) = res. into_parts ( ) ;
36
49
37
50
let mut prelude_headers = parts. headers ;
54
67
metadata_prelude,
55
68
stream : BodyStream { body } ,
56
69
}
57
- } ) ;
70
+ } )
71
+ }
58
72
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) ;
59
89
lambda_runtime:: run ( svc) . await
60
90
}
61
91
0 commit comments