Skip to content

Commit e409852

Browse files
committed
us ethe new decoding capability provided by event library 1.2
1 parent f178bba commit e409852

File tree

2 files changed

+15
-35
lines changed

2 files changed

+15
-35
lines changed

Examples/Streaming+Codable/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ let package = Package(
1111
dependencies: [
1212
// during CI, the dependency on local version of swift-aws-lambda-runtime is added dynamically below
1313
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "2.0.0-beta.1"),
14-
.package(url: "https://github.com/swift-server/swift-aws-lambda-events.git", from: "1.0.0"),
14+
.package(url: "https://github.com/swift-server/swift-aws-lambda-events.git", from: "1.2.0"),
1515
],
1616
targets: [
1717
.executableTarget(

Examples/Streaming+Codable/Sources/LambdaStreaming+Codable.swift

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ public struct StreamingLambdaCodableAdapter<
7373
}
7474

7575
/// Handles the raw ByteBuffer by decoding it and passing to the underlying handler.
76+
/// This function attempts to decode the event as a `FunctionURLRequest` first, which allows for
77+
/// handling Function URL requests that may have a base64-encoded body.
78+
/// If the decoding fails, it falls back to decoding the event "as-is" with the provided JSON type.
7679
/// - Parameters:
7780
/// - event: The raw ByteBuffer event to decode.
7881
/// - responseWriter: The response writer to pass to the underlying handler.
@@ -84,43 +87,20 @@ public struct StreamingLambdaCodableAdapter<
8487
context: LambdaContext
8588
) async throws {
8689

87-
// try to decode the event as a FunctionURLRequest and extract its body
88-
let urlRequestBody = bodyFromFunctionURLRequest(event)
90+
var decodedBody: Handler.Event!
8991

90-
// decode the body or the event as user-provided JSON
91-
let decodedEvent = try self.decoder.decode(Handler.Event.self, from: urlRequestBody ?? event)
92+
// try to decode the event as a FunctionURLRequest, then fetch its body attribute
93+
if let request = try? self.decoder.decode(FunctionURLRequest.self, from: event) {
94+
// decode the body as user-provided JSON type
95+
// this function handles the base64 decoding when needed
96+
decodedBody = try request.decodeBody(Handler.Event.self)
97+
} else {
98+
// try to decode the event "as-is" with the provided JSON type
99+
decodedBody = try self.decoder.decode(Handler.Event.self, from: event)
100+
}
92101

93102
// and pass it to the handler
94-
try await self.handler.handle(decodedEvent, responseWriter: responseWriter, context: context)
95-
}
96-
97-
/// Extract the body payload from a FunctionURLRequest event.
98-
/// This function checks if the event is a valid `FunctionURLRequest` and decodes the body if it is base64 encoded.
99-
/// If the event is not a valid `FunctionURLRequest`, it returns nil.
100-
/// - Parameter event: The raw ByteBuffer event to check.
101-
/// - Returns: The base64 decoded body of the FunctionURLRequest if it is a valid FunctionURLRequest, otherwise nil.
102-
@inlinable
103-
package func bodyFromFunctionURLRequest(_ event: ByteBuffer) -> ByteBuffer? {
104-
do {
105-
// try to decode as a FunctionURLRequest
106-
let request = try self.decoder.decode(FunctionURLRequest.self, from: event)
107-
108-
// if the body is encoded in base64, decode it
109-
if request.isBase64Encoded,
110-
let base64EncodedString = request.body,
111-
// this is the minimal way to base64 decode without importing new dependencies
112-
let decodedData = Data(base64Encoded: base64EncodedString),
113-
let decodedString = String(data: decodedData, encoding: .utf8)
114-
{
115-
116-
return ByteBuffer(string: decodedString)
117-
} else {
118-
return ByteBuffer(string: request.body ?? "")
119-
}
120-
} catch {
121-
// not a FunctionURLRequest, return nil
122-
return nil
123-
}
103+
try await self.handler.handle(decodedBody, responseWriter: responseWriter, context: context)
124104
}
125105
}
126106

0 commit comments

Comments
 (0)