@@ -73,6 +73,9 @@ public struct StreamingLambdaCodableAdapter<
73
73
}
74
74
75
75
/// 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.
76
79
/// - Parameters:
77
80
/// - event: The raw ByteBuffer event to decode.
78
81
/// - responseWriter: The response writer to pass to the underlying handler.
@@ -84,43 +87,20 @@ public struct StreamingLambdaCodableAdapter<
84
87
context: LambdaContext
85
88
) async throws {
86
89
87
- // try to decode the event as a FunctionURLRequest and extract its body
88
- let urlRequestBody = bodyFromFunctionURLRequest ( event)
90
+ var decodedBody : Handler . Event !
89
91
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
+ }
92
101
93
102
// 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)
124
104
}
125
105
}
126
106
0 commit comments