|
| 1 | +import gleam/bit_array |
| 2 | +import gleam/bool |
1 | 3 | import gleam/dict.{type Dict}
|
2 | 4 | import gleam/dynamic.{type Dynamic}
|
| 5 | +import gleam/http |
| 6 | +import gleam/http/request.{type Request, Request} |
| 7 | +import gleam/http/response.{type Response} |
| 8 | +import gleam/io |
3 | 9 | import gleam/javascript/promise.{type Promise}
|
4 |
| -import gleam/option.{type Option} |
| 10 | +import gleam/list |
| 11 | +import gleam/option.{type Option, None, Some} |
| 12 | +import gleam/regex |
| 13 | +import gleam/result |
| 14 | +import gleam/string |
5 | 15 |
|
6 | 16 | pub type JsEvent
|
7 | 17 |
|
@@ -182,6 +192,108 @@ pub fn to_handler(
|
182 | 192 | }
|
183 | 193 | }
|
184 | 194 |
|
| 195 | +pub fn http_handler( |
| 196 | + handler: fn(Request(BitArray), Context) -> Promise(Response(BitArray)), |
| 197 | +) -> JsHandler { |
| 198 | + api_gateway_proxy_v2_handler(fn(event, ctx) { |
| 199 | + event |
| 200 | + |> create_request |
| 201 | + |> handler(ctx) |
| 202 | + |> promise.map(create_response) |
| 203 | + }) |
| 204 | +} |
| 205 | + |
| 206 | +fn create_request(event: ApiGatewayProxyEventV2) -> Request(BitArray) { |
| 207 | + io.debug(event.headers) |
| 208 | + let method = |
| 209 | + event.request_context.http.method |
| 210 | + |> http.parse_method |
| 211 | + |> result.unwrap(http.Get) |
| 212 | + let headers = case event.cookies { |
| 213 | + Some([_] as cookies) -> { |
| 214 | + event.headers |
| 215 | + |> dict.insert("cookie", string.join(cookies, "; ")) |
| 216 | + |> dict.to_list |
| 217 | + } |
| 218 | + _ -> dict.to_list(event.headers) |
| 219 | + } |
| 220 | + let body = |
| 221 | + event.body |
| 222 | + |> body_bit_array(event.is_base64_encoded) |
| 223 | + |> result.unwrap(<<>>) |
| 224 | + let host = event.request_context.domain_name |
| 225 | + let path = event.raw_path |
| 226 | + let query = string.to_option(event.raw_query_string) |
| 227 | + |
| 228 | + Request( |
| 229 | + method:, |
| 230 | + headers:, |
| 231 | + body:, |
| 232 | + scheme: http.Https, |
| 233 | + host:, |
| 234 | + port: None, |
| 235 | + path:, |
| 236 | + query:, |
| 237 | + ) |
| 238 | +} |
| 239 | + |
| 240 | +fn body_bit_array( |
| 241 | + body: Option(String), |
| 242 | + is_base64_encoded: Bool, |
| 243 | +) -> Result(BitArray, Nil) { |
| 244 | + use body <- result.try(option.to_result(body, Nil)) |
| 245 | + use <- bool.guard(!is_base64_encoded, Ok(bit_array.from_string(body))) |
| 246 | + use body <- result.try(bit_array.base64_decode(body)) |
| 247 | + Ok(body) |
| 248 | +} |
| 249 | + |
| 250 | +fn create_response(response: Response(BitArray)) -> ApiGatewayProxyResultV2 { |
| 251 | + let is_content_type_binary = |
| 252 | + response.get_header(response, "content-type") |
| 253 | + |> result.map(is_content_type_binary) |
| 254 | + |
| 255 | + let is_base64_encoded = case is_content_type_binary { |
| 256 | + Ok(True) -> True |
| 257 | + _ -> { |
| 258 | + response.get_header(response, "content-encoding") |
| 259 | + |> result.map(is_content_encoding_binary) |
| 260 | + |> result.unwrap(False) |
| 261 | + } |
| 262 | + } |
| 263 | + |
| 264 | + let body = case is_base64_encoded { |
| 265 | + True -> bit_array.base64_encode(response.body, False) |
| 266 | + False -> bit_array.to_string(response.body) |> result.unwrap("") |
| 267 | + } |
| 268 | + let cookies = get_cookies(response) |
| 269 | + |
| 270 | + ApiGatewayProxyResultV2( |
| 271 | + status_code: response.status, |
| 272 | + headers: dict.from_list(response.headers), |
| 273 | + body: string.to_option(body), |
| 274 | + is_base64_encoded:, |
| 275 | + cookies:, |
| 276 | + ) |
| 277 | +} |
| 278 | + |
| 279 | +fn get_cookies(response: Response(a)) -> List(String) { |
| 280 | + response.get_cookies(response) |
| 281 | + |> list.map(fn(cookie) { cookie.0 <> "=" <> cookie.1 }) |
| 282 | +} |
| 283 | + |
| 284 | +fn is_content_type_binary(content_type: String) -> Bool { |
| 285 | + let assert Ok(re) = |
| 286 | + regex.from_string( |
| 287 | + "!/^(text\\/(plain|html|css|javascript|csv).*|application\\/(.*json|.*xml).*|image\\/svg\\+xml.*)$/", |
| 288 | + ) |
| 289 | + regex.check(re, content_type) |
| 290 | +} |
| 291 | + |
| 292 | +fn is_content_encoding_binary(content_encoding: String) -> Bool { |
| 293 | + let assert Ok(re) = regex.from_string("/^(gzip|deflate|compress|br)/") |
| 294 | + regex.check(re, content_encoding) |
| 295 | +} |
| 296 | + |
185 | 297 | pub fn api_gateway_proxy_v2_handler(
|
186 | 298 | handler: Handler(ApiGatewayProxyEventV2, ApiGatewayProxyResultV2),
|
187 | 299 | ) -> JsHandler {
|
|
0 commit comments