forked from aws/aws-lambda-rust-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreaming.rs
More file actions
120 lines (110 loc) · 3.77 KB
/
Copy pathstreaming.rs
File metadata and controls
120 lines (110 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use crate::{http::header::SET_COOKIE, request::LambdaRequest, tower::ServiceBuilder, Request, RequestExt};
use bytes::Bytes;
pub use http::{self, Response};
use http_body::Body;
use lambda_runtime::Diagnostic;
pub use lambda_runtime::{
self,
tower::{
util::{MapRequest, MapResponse},
ServiceExt,
},
Error, LambdaEvent, MetadataPrelude, Service, StreamResponse,
};
use std::{
fmt::Debug,
pin::Pin,
task::{Context, Poll},
};
use tokio_stream::Stream;
/// Converts a handler into a streaming-compatible service for use with AWS
/// Lambda.
///
/// This function wraps a `Service` implementation, transforming its input and
/// output to be compatible with AWS Lambda's streaming response feature. It
/// provides the necessary middleware to handle `LambdaEvent` requests and
/// converts the `http::Response` into a `StreamResponse` containing a metadata
/// prelude and body stream.
#[allow(clippy::type_complexity)]
pub fn into_streaming_response<'a, S, B, E>(
handler: S,
) -> MapResponse<
MapRequest<S, impl FnMut(LambdaEvent<LambdaRequest>) -> Request>,
impl FnOnce(Response<B>) -> StreamResponse<BodyStream<B>> + Clone,
>
where
S: Service<Request, Response = Response<B>, Error = E>,
S::Future: Send + 'a,
E: Debug + Into<Diagnostic>,
B: Body + Unpin + Send + 'static,
B::Data: Into<Bytes> + Send,
B::Error: Into<Error> + Send + Debug,
{
ServiceBuilder::new()
.map_request(|req: LambdaEvent<LambdaRequest>| {
let event: Request = req.payload.into();
event.with_lambda_context(req.context)
})
.service(handler)
.map_response(|res: Response<B>| {
let (parts, body) = res.into_parts();
let mut prelude_headers = parts.headers;
let cookies = prelude_headers.get_all(SET_COOKIE);
let cookies = cookies
.iter()
.map(|c| String::from_utf8_lossy(c.as_bytes()).to_string())
.collect::<Vec<String>>();
prelude_headers.remove(SET_COOKIE);
let metadata_prelude = MetadataPrelude {
headers: prelude_headers,
status_code: parts.status,
cookies,
};
StreamResponse {
metadata_prelude,
stream: BodyStream { body },
}
})
}
/// Starts the Lambda Rust runtime and stream response back [Configure Lambda
/// Streaming
/// Response](https://docs.aws.amazon.com/lambda/latest/dg/configuration-response-streaming.html).
///
/// This takes care of transforming the LambdaEvent into a [`Request`] and
/// accepts [`http::Response<http_body::Body>`] as response.
pub async fn run_with_streaming_response<'a, S, B, E>(handler: S) -> Result<(), Error>
where
S: Service<Request, Response = Response<B>, Error = E>,
S::Future: Send + 'a,
E: Debug + Into<Diagnostic>,
B: Body + Unpin + Send + 'static,
B::Data: Into<Bytes> + Send,
B::Error: Into<Error> + Send + Debug,
{
let svc = into_streaming_response(handler);
lambda_runtime::run(svc).await
}
pin_project_lite::pin_project! {
pub struct BodyStream<B> {
#[pin]
pub(crate) body: B,
}
}
impl<B> Stream for BodyStream<B>
where
B: Body + Unpin + Send + 'static,
B::Data: Into<Bytes> + Send,
B::Error: Into<Error> + Send + Debug,
{
type Item = Result<B::Data, B::Error>;
#[inline]
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
match futures_util::ready!(self.as_mut().project().body.poll_frame(cx)?) {
Some(frame) => match frame.into_data() {
Ok(data) => Poll::Ready(Some(Ok(data))),
Err(_frame) => Poll::Ready(None),
},
None => Poll::Ready(None),
}
}
}