Skip to content

Commit 98ef895

Browse files
committed
fix tests
1 parent 64282d4 commit 98ef895

File tree

1 file changed

+45
-26
lines changed

1 file changed

+45
-26
lines changed

Sources/AWSLambdaRuntime/LambdaStreaming+Codable.swift

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -82,33 +82,52 @@ public struct StreamingLambdaCodableAdapter<
8282
context: LambdaContext
8383
) async throws {
8484

85-
// for some reasons I don't understand the "body" param contains the complete FunctionURL request
86-
// so, 1/ we decode the event we receive, 2/ we base64 decode the body, 3/ we decode a FunctionURLRequest again,
87-
// then 4/ we can access the actual payload body, decode it pass it to the handler
88-
let functionUrlEvent1 = try self.decoder.decode(FunctionURLRequest.self, from: event)
89-
90-
if let base64EncodedString = functionUrlEvent1.body,
91-
// this is the minimal way to base64 decode without importing new dependencies
92-
let decodedData = Data(base64Encoded: base64EncodedString),
93-
let decodedString = String(data: decodedData, encoding: .utf8)
94-
{
95-
96-
// decode the FunctionURL event inside the body
97-
let functionUrlEvent2 = try self.decoder.decode(
98-
FunctionURLRequest.self,
99-
from: ByteBuffer(string: decodedString)
100-
)
101-
102-
// finally decode the actual payload passed by the caller
103-
let decodedEvent = try self.decoder.decode(
104-
Handler.Event.self,
105-
from: ByteBuffer(string: functionUrlEvent2.body ?? "")
106-
)
107-
108-
// and invoke the user-provided handler
109-
try await self.handler.handle(decodedEvent, responseWriter: responseWriter, context: context)
85+
// try to decode the event as a FunctionURLRequest
86+
if let functionUrlEvent1 = isFunctionURLRequest(event) {
87+
88+
// for some reasons I don't understand the "body" param contains the complete FunctionURL request
89+
// so, 1/ we decode the event we receive, 2/ we base64 decode the body, 3/ we decode a FunctionURLRequest again,
90+
// then 4/ we can access the actual payload body, decode it pass it to the handler
91+
if let base64EncodedString = functionUrlEvent1.body,
92+
// this is the minimal way to base64 decode without importing new dependencies
93+
let decodedData = Data(base64Encoded: base64EncodedString),
94+
let decodedString = String(data: decodedData, encoding: .utf8)
95+
{
96+
97+
// decode the FunctionURL event inside the body
98+
let functionUrlEvent2 = try self.decoder.decode(
99+
FunctionURLRequest.self,
100+
from: ByteBuffer(string: decodedString)
101+
)
102+
103+
// finally decode the actual payload passed by the caller
104+
let decodedEvent = try self.decoder.decode(
105+
Handler.Event.self,
106+
from: ByteBuffer(string: functionUrlEvent2.body ?? "")
107+
)
108+
109+
// and invoke the user-provided handler
110+
try await self.handler.handle(decodedEvent, responseWriter: responseWriter, context: context)
111+
} else {
112+
context.logger.trace("Can't decode FunctionURLRequest's body", metadata: ["Event": "\(event)"])
113+
}
114+
110115
} else {
111-
context.logger.trace("Can't decode FunctionURLRequest's body", metadata: ["Event": "\(event)"])
116+
// otherwise, decode the event as a user-provided JSON event
117+
let decodedEvent = try self.decoder.decode(Handler.Event.self, from: event)
118+
try await self.handler.handle(decodedEvent, responseWriter: responseWriter, context: context)
119+
}
120+
}
121+
122+
/// Check if the payload is an FunctionURLlRequest or a direct invocation
123+
/// - Parameter event: The raw ByteBuffer event to check.
124+
/// - Returns: the FunctionURLRequest if the event is a FunctionURLRequest, nil otherwise
125+
@inlinable
126+
package func isFunctionURLRequest(_ event: ByteBuffer) -> FunctionURLRequest? {
127+
do {
128+
return try self.decoder.decode(FunctionURLRequest.self, from: event)
129+
} catch {
130+
return nil
112131
}
113132
}
114133
}

0 commit comments

Comments
 (0)