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
129 lines (118 loc) · 3.86 KB
/
Copy pathstreaming.rs
File metadata and controls
129 lines (118 loc) · 3.86 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
121
122
123
124
125
126
127
128
129
use crate::{http::header::SET_COOKIE, request::LambdaRequest, tower::ServiceBuilder, Request, RequestExt};
use bytes::Bytes;
pub use http::{self, Response};
use http_body::Body;
pub use lambda_runtime::{
self,
tower::{
util::{MapRequest, MapResponse},
ServiceExt,
},
Error, LambdaEvent, MetadataPrelude, Service, StreamResponse,
};
use lambda_runtime::{tower::util::BoxService, Diagnostic};
use std::{
fmt::Debug,
pin::Pin,
task::{Context, Poll},
};
use tokio_stream::Stream;
/// Runs the Lambda runtime with a handler that returns **streaming** HTTP
/// responses.
pub fn into_streaming_response<'a, S, B, E>(
handler: S,
) -> BoxService<LambdaEvent<LambdaRequest>, StreamResponse<BodyStream<B>>, E>
where
S: Service<Request, Response = Response<B>, Error = E> + Send + 'static,
S::Future: Send + 'a,
E: Debug + Into<Diagnostic> + 'static,
B: Body + Unpin + Send + 'static,
B::Data: Into<Bytes> + Send,
B::Error: Into<Error> + Send + Debug,
{
into_streaming_response_inner::<S, B, E>(handler).boxed()
}
#[allow(clippy::type_complexity)]
fn into_streaming_response_inner<'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)
.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 },
}
})
}
/// Runs the Lambda runtime with a handler that returns **streaming** HTTP
/// responses.
///
/// See the [AWS docs for response streaming].
///
/// [AWS docs for response streaming]:
/// https://docs.aws.amazon.com/lambda/latest/dg/configuration-response-streaming.html
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_inner(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),
}
}
}