Skip to content

Commit

Permalink
Re-encode Context, events for better compat
Browse files Browse the repository at this point in the history
  • Loading branch information
armanbilge committed Nov 20, 2023
1 parent 61331e1 commit 2126768
Show file tree
Hide file tree
Showing 23 changed files with 1,036 additions and 597 deletions.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ lazy val lambda = crossProject(JSPlatform, JVMPlatform)
"org.tpolecat" %%% "natchez-core" % natchezVersion,
"io.circe" %%% "circe-scodec" % circeVersion,
"io.circe" %%% "circe-jawn" % circeVersion,
"com.comcast" %%% "ip4s-core" % "3.4.0",
"org.scodec" %%% "scodec-bits" % "1.1.38",
"org.scalameta" %%% "munit-scalacheck" % munitVersion % Test,
"org.typelevel" %%% "munit-cats-effect-3" % munitCEVersion % Test,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ trait CloudFormationCustomResourceArbitraries {
appVersionName <- arbitrary[String]
appVersionCode <- arbitrary[String]
appPackageName <- arbitrary[String]
} yield new ClientContextClient(
} yield ClientContextClient(
installationId,
appTitle,
appVersionName,
Expand All @@ -56,22 +56,22 @@ trait CloudFormationCustomResourceArbitraries {
make <- arbitrary[String]
model <- arbitrary[String]
locale <- arbitrary[String]
} yield new ClientContextEnv(platformVersion, platform, make, model, locale)
} yield ClientContextEnv(platformVersion, platform, make, model, locale)
implicit val arbClientContextEnv: Arbitrary[ClientContextEnv] = Arbitrary(genClientContextEnv)

val genCognitoIdentity: Gen[CognitoIdentity] =
for {
identityId <- arbitrary[String]
identityPoolId <- arbitrary[String]
} yield new CognitoIdentity(identityId, identityPoolId)
} yield CognitoIdentity(identityId, identityPoolId)
implicit val arbCognitoIdentity: Arbitrary[CognitoIdentity] = Arbitrary(genCognitoIdentity)

val genClientContext: Gen[ClientContext] =
for {
client <- arbitrary[ClientContextClient]
env <- arbitrary[ClientContextEnv]
custom <- arbitrary[JsonObject]
} yield new ClientContext(client, env, custom)
} yield ClientContext(client, env, custom)
implicit val arbClientContext: Arbitrary[ClientContext] = Arbitrary(genClientContext)

def genContext[F[_]: Applicative]: Gen[Context[F]] =
Expand All @@ -86,7 +86,7 @@ trait CloudFormationCustomResourceArbitraries {
identity <- arbitrary[Option[CognitoIdentity]]
clientContext <- arbitrary[Option[ClientContext]]
remainingTime <- arbitrary[FiniteDuration]
} yield new Context(
} yield Context(
functionName,
functionVersion,
invokedFunctionArn,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import feral.lambda.events.ApiGatewayProxyEventV2
import feral.lambda.events.ApiGatewayProxyStructuredResultV2
import fs2.Stream
import org.http4s.Charset
import org.http4s.Header
import org.http4s.Headers
import org.http4s.{HttpApp, HttpRoutes}
import org.http4s.Method
Expand Down Expand Up @@ -60,7 +61,7 @@ object ApiGatewayProxyHandler {
response.status.code,
(headers - `Set-Cookie`.name).map {
case (name, values) =>
name.toString -> values.mkString(",")
name -> values.mkString(",")
},
responseBody,
isBase64Encoded,
Expand All @@ -73,8 +74,16 @@ object ApiGatewayProxyHandler {
event: ApiGatewayProxyEventV2): F[Request[F]] = for {
method <- Method.fromString(event.requestContext.http.method).liftTo[F]
uri <- Uri.fromString(event.rawPath + "?" + event.rawQueryString).liftTo[F]
cookies = event.cookies.filter(_.nonEmpty).map(Cookie.name.toString -> _.mkString("; "))
headers = Headers(cookies.toList ::: event.headers.toList)
headers = {
val builder = List.newBuilder[Header.Raw]

event.headers.foreachEntry(builder += Header.Raw(_, _))
event.cookies.filter(_.nonEmpty).foreach { cs =>
builder += Header.Raw(Cookie.name, cs.mkString("; "))
}

Headers(builder.result())
}
readBody =
if (event.isBase64Encoded)
fs2.text.base64.decode[F]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,24 @@ class ApiGatewayProxyHandlerSuite extends CatsEffectSuite {
}

def expectedHeaders = Headers(
("cookie", "s_fid=7AABXMPL1AFD9BBF-0643XMPL09956DE2; regStatus=pre-register"),
("x-forwarded-port", "443"),
(
"accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"),
("sec-fetch-site", "cross-site"),
("x-amzn-trace-id", "Root=1-5e6722a7-cc56xmpl46db7ae02d4da47e"),
("sec-fetch-mode", "navigate"),
("sec-fetch-user", "?1"),
("accept-encoding", "gzip, deflate, br"),
("x-forwarded-for", "205.255.255.176"),
("upgrade-insecure-requests", "1"),
(
"user-agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36"),
("accept-language", "en-US,en;q=0.9"),
("x-forwarded-proto", "https"),
("host", "r3pmxmplak.execute-api.us-east-2.amazonaws.com"),
("content-length", "0"),
("sec-fetch-dest", "document")
"content-length" -> "0",
"accept-language" -> "en-US,en;q=0.9",
"sec-fetch-dest" -> "document",
"sec-fetch-user" -> "?1",
"x-amzn-trace-id" -> "Root=1-5e6722a7-cc56xmpl46db7ae02d4da47e",
"host" -> "r3pmxmplak.execute-api.us-east-2.amazonaws.com",
"sec-fetch-mode" -> "navigate",
"accept-encoding" -> "gzip, deflate, br",
"accept" ->
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"sec-fetch-site" -> "cross-site",
"x-forwarded-port" -> "443",
"x-forwarded-proto" -> "https",
"upgrade-insecure-requests" -> "1",
"user-agent" ->
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36",
"x-forwarded-for" -> "205.255.255.176",
"cookie" -> "s_fid=7AABXMPL1AFD9BBF-0643XMPL09956DE2; regStatus=pre-register"
)

}
10 changes: 5 additions & 5 deletions lambda/js/src/main/scala/feral/lambda/ContextPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import scala.concurrent.duration._
private[lambda] trait ContextCompanionPlatform {

private[lambda] def fromJS[F[_]: Sync](context: facade.Context): Context[F] =
new Context(
Context(
context.functionName,
context.functionVersion,
context.invokedFunctionArn,
Expand All @@ -34,21 +34,21 @@ private[lambda] trait ContextCompanionPlatform {
context.logGroupName,
context.logStreamName,
context.identity.toOption.map { identity =>
new CognitoIdentity(identity.cognitoIdentityId, identity.cognitoIdentityPoolId)
CognitoIdentity(identity.cognitoIdentityId, identity.cognitoIdentityPoolId)
},
context
.clientContext
.toOption
.map { clientContext =>
new ClientContext(
new ClientContextClient(
ClientContext(
ClientContextClient(
clientContext.client.installationId,
clientContext.client.appTitle,
clientContext.client.appVersionName,
clientContext.client.appVersionCode,
clientContext.client.appPackageName
),
new ClientContextEnv(
ClientContextEnv(
clientContext.env.platformVersion,
clientContext.env.platform,
clientContext.env.make,
Expand Down
10 changes: 5 additions & 5 deletions lambda/jvm/src/main/scala/feral/lambda/ContextPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import scala.jdk.CollectionConverters._
private[lambda] trait ContextCompanionPlatform {

private[lambda] def fromJava[F[_]: Sync](context: runtime.Context): Context[F] =
new Context(
Context(
context.getFunctionName(),
context.getFunctionVersion(),
context.getInvokedFunctionArn(),
Expand All @@ -36,18 +36,18 @@ private[lambda] trait ContextCompanionPlatform {
context.getLogGroupName(),
context.getLogStreamName(),
Option(context.getIdentity()).map { identity =>
new CognitoIdentity(identity.getIdentityId(), identity.getIdentityPoolId())
CognitoIdentity(identity.getIdentityId(), identity.getIdentityPoolId())
},
Option(context.getClientContext()).map { clientContext =>
new ClientContext(
new ClientContextClient(
ClientContext(
ClientContextClient(
clientContext.getClient().getInstallationId(),
clientContext.getClient().getAppTitle(),
clientContext.getClient().getAppVersionName(),
clientContext.getClient().getAppVersionCode(),
clientContext.getClient().getAppPackageName()
),
new ClientContextEnv(
ClientContextEnv(
clientContext.getEnvironment().get("platformVersion"),
clientContext.getEnvironment().get("platform"),
clientContext.getEnvironment().get("make"),
Expand Down
Loading

0 comments on commit 2126768

Please sign in to comment.