1 1 | // Code generated by software.amazon.smithy.rust.codegen.smithy-rs. DO NOT EDIT.
|
2 + | /* TsApplicationGenerator.kt:62 */
|
2 3 | use napi_derive::napi;
|
4 + | /* RustType.kt:516 */
|
3 5 | #[derive(::std::clone::Clone)]
|
6 + | /* TsApplicationGenerator.kt:73 */
|
4 7 | pub struct Handlers {
|
8 + | /* TsApplicationGenerator.kt:78 */
|
5 9 | pub(crate) operation: ::napi::threadsafe_function::ThreadsafeFunction<
|
6 10 | crate::input::OperationInput,
|
7 11 | ::napi::threadsafe_function::ErrorStrategy::Fatal,
|
8 12 | >,
|
13 + | /* TsApplicationGenerator.kt:73 */
|
9 14 | }
|
15 + | /* RustType.kt:516 */
|
10 16 | #[napi(object)]
|
17 + | /* TsApplicationGenerator.kt:90 */
|
11 18 | pub struct TsHandlers {
|
19 + | /* TsApplicationGenerator.kt:96 */
|
12 20 | #[napi(ts_type = "(input: OperationInput) => Promise<OperationOutput>")]
|
13 21 | pub operation: ::napi::JsFunction,
|
22 + | /* TsApplicationGenerator.kt:90 */
|
14 23 | }
|
24 + | /* RustType.kt:516 */
|
15 25 | #[napi]
|
26 + | /* TsApplicationGenerator.kt:109 */
|
16 27 | pub struct App {
|
17 28 | handlers: Handlers,
|
18 29 | }
|
30 + | /* RustType.kt:516 */
|
19 31 | #[napi]
|
32 + | /* TsApplicationGenerator.kt:117 */
|
20 33 | impl App {
|
34 + | /* RustType.kt:516 */
|
21 35 | #[napi(constructor)]
|
36 + | /* TsApplicationGenerator.kt:125 */
|
22 37 | pub fn create(ts_handlers: TsHandlers) -> ::napi::Result<Self> {
|
38 + | /* TsApplicationGenerator.kt:133 */
|
23 39 | let operation: ::napi::threadsafe_function::ThreadsafeFunction<
|
24 40 | crate::input::OperationInput,
|
25 41 | ::napi::threadsafe_function::ErrorStrategy::Fatal,
|
26 42 | > = ts_handlers
|
27 43 | .operation
|
28 44 | .create_threadsafe_function(0, |ctx| Ok(vec![ctx.value]))?;
|
45 + | /* TsApplicationGenerator.kt:142 */
|
29 46 | let handlers = Handlers {
|
47 + | /* TsApplicationGenerator.kt:145 */
|
30 48 | operation: operation.clone(),
|
49 + | /* TsApplicationGenerator.kt:147 */
|
31 50 | };
|
51 + | /* TsApplicationGenerator.kt:148 */
|
32 52 | Ok(Self { handlers })
|
53 + | /* TsApplicationGenerator.kt:125 */
|
33 54 | }
|
55 + | /* RustType.kt:516 */
|
34 56 | #[napi]
|
57 + | /* TsApplicationGenerator.kt:154 */
|
35 58 | pub fn start(&self, socket: &TsSocket) -> ::napi::Result<()> {
|
59 + | /* TsApplicationGenerator.kt:158 */
|
36 60 | let plugins = ::aws_smithy_http_server::plugin::PluginPipeline::new();
|
37 61 | let builder = crate::service::SimpleService::builder_with_plugins(plugins);
|
62 + | /* TsApplicationGenerator.kt:167 */
|
38 63 | let builder = builder.operation(crate::ts_operation_adaptor::operation);
|
64 + | /* TsApplicationGenerator.kt:169 */
|
39 65 | let app = builder
|
40 66 | .build()
|
41 67 | .expect("failed to build instance of SimpleService")
|
42 68 | .layer(&::aws_smithy_http_server::AddExtensionLayer::new(
|
43 69 | self.handlers.clone(),
|
44 70 | ));
|
45 71 | let service = ::tower::util::BoxCloneService::new(app);
|
46 72 | start_hyper_worker(socket, service).expect("failed to start the hyper server");
|
47 73 | Ok(())
|
74 + | /* TsApplicationGenerator.kt:154 */
|
48 75 | }
|
76 + | /* TsApplicationGenerator.kt:117 */
|
49 77 | }
|
78 + | /* RustType.kt:516 */
|
50 79 | #[napi]
|
80 + | /* RustType.kt:516 */
|
51 81 | #[derive(::std::fmt::Debug)]
|
82 + | /* TsApplicationGenerator.kt:185 */
|
52 83 | pub struct TsSocket(::socket2::Socket);
|
84 + | /* RustType.kt:516 */
|
53 85 | #[napi]
|
86 + | /* TsApplicationGenerator.kt:193 */
|
54 87 | impl TsSocket {
|
88 + | /* TsApplicationGenerator.kt:194 */
|
55 89 | /// Create a new UNIX `Socket` from an address, port and backlog.
|
56 90 | /// If not specified, the backlog defaults to 1024 connections.
|
91 + | /* RustType.kt:516 */
|
57 92 | #[napi(constructor)]
|
93 + | /* TsApplicationGenerator.kt:201 */
|
58 94 | pub fn new(address: String, port: i32, backlog: Option<i32>) -> ::napi::Result<Self> {
|
95 + | /* TsApplicationGenerator.kt:205 */
|
59 96 | let socket = Self::new_socket(address, port, backlog)
|
60 97 | .map_err(|e| ::napi::Error::from_reason(e.to_string()))?;
|
61 98 | Ok(Self(socket))
|
99 + | /* TsApplicationGenerator.kt:201 */
|
62 100 | }
|
101 + | /* TsApplicationGenerator.kt:214 */
|
63 102 | pub fn new_socket(
|
64 103 | address: String,
|
65 104 | port: i32,
|
66 105 | backlog: Option<i32>,
|
67 106 | ) -> Result<::socket2::Socket, Box<dyn std::error::Error>> {
|
107 + | /* TsApplicationGenerator.kt:218 */
|
68 108 | let address: std::net::SocketAddr = format!("{}:{}", address, port).parse()?;
|
69 109 | let domain = if address.is_ipv6() {
|
70 110 | ::socket2::Domain::IPV6
|
71 111 | } else {
|
72 112 | ::socket2::Domain::IPV4
|
73 113 | };
|
74 114 | let socket = ::socket2::Socket::new(
|
75 115 | domain,
|
76 116 | ::socket2::Type::STREAM,
|
77 117 | Some(::socket2::Protocol::TCP),
|
78 118 | )?;
|
79 119 | // Set value for the `SO_REUSEPORT` and `SO_REUSEADDR` options on this socket.
|
80 120 | // This indicates that further calls to `bind` may allow reuse of local
|
81 121 | // addresses. For IPv4 sockets this means that a socket may bind even when
|
82 122 | // there's a socket already listening on this port.
|
83 123 | socket.set_reuse_port(true)?;
|
84 124 | socket.set_reuse_address(true)?;
|
85 125 | socket.bind(&address.into())?;
|
86 126 | socket.listen(backlog.unwrap_or(1024))?;
|
87 127 | Ok(socket)
|
128 + | /* TsApplicationGenerator.kt:214 */
|
88 129 | }
|
130 + | /* TsApplicationGenerator.kt:241 */
|
89 131 | /// Clone the inner socket allowing it to be shared between multiple
|
90 132 | /// Nodejs processes.
|
133 + | /* RustType.kt:516 */
|
91 134 | #[napi]
|
135 + | /* TsApplicationGenerator.kt:248 */
|
92 136 | pub fn try_clone(&self) -> ::napi::Result<Self> {
|
137 + | /* TsApplicationGenerator.kt:252 */
|
93 138 | Ok(TsSocket(
|
94 139 | self.0
|
95 140 | .try_clone()
|
96 141 | .map_err(|e| ::napi::Error::from_reason(e.to_string()))?,
|
97 142 | ))
|
143 + | /* TsApplicationGenerator.kt:248 */
|
98 144 | }
|
145 + | /* TsApplicationGenerator.kt:193 */
|
99 146 | }
|
147 + | /* TsApplicationGenerator.kt:265 */
|
100 148 | impl TsSocket {
|
149 + | /* TsApplicationGenerator.kt:266 */
|
101 150 | pub fn to_raw_socket(&self) -> ::napi::Result<::socket2::Socket> {
|
102 151 | self.0
|
103 152 | .try_clone()
|
104 153 | .map_err(|e| ::napi::Error::from_reason(e.to_string()))
|
105 154 | }
|
155 + | /* TsApplicationGenerator.kt:265 */
|
106 156 | }
|
157 + | /* TsApplicationGenerator.kt:281 */
|
107 158 | pub fn start_hyper_worker(
|
108 159 | socket: &TsSocket,
|
109 160 | app: ::tower::util::BoxCloneService<
|
110 161 | ::http::Request<::aws_smithy_http_server::body::Body>,
|
111 162 | ::http::Response<::aws_smithy_http_server::body::BoxBody>,
|
112 163 | std::convert::Infallible,
|
113 164 | >,
|
114 165 | ) -> ::napi::Result<()> {
|
166 + | /* TsApplicationGenerator.kt:294 */
|
115 167 | let server = ::hyper::Server::from_tcp(
|
116 168 | socket
|
117 169 | .to_raw_socket()?
|
118 170 | .try_into()
|
119 171 | .expect("Unable to convert socket2::Socket into std::net::TcpListener"),
|
120 172 | )
|
121 173 | .expect("Unable to create hyper server from shared socket")
|
122 174 | .serve(::aws_smithy_http_server::routing::IntoMakeService::new(app));
|
123 175 |
|
124 176 | let handle = ::tokio::runtime::Handle::current();
|
125 177 | handle.spawn(async move {
|
126 178 | // Process each socket concurrently.
|
127 179 | server.await
|
128 180 | });
|
129 181 |
|
130 182 | Ok(())
|
183 + | /* TsApplicationGenerator.kt:281 */
|
131 184 | }
|