@@ -82,33 +82,52 @@ public struct StreamingLambdaCodableAdapter<
82
82
context: LambdaContext
83
83
) async throws {
84
84
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
+
110
115
} 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
112
131
}
113
132
}
114
133
}
0 commit comments