diff --git a/Sources/AWSLambdaRuntime/Lambda+Codable.swift b/Sources/AWSLambdaRuntime/Lambda+Codable.swift index 6751afe9..531eb96b 100644 --- a/Sources/AWSLambdaRuntime/Lambda+Codable.swift +++ b/Sources/AWSLambdaRuntime/Lambda+Codable.swift @@ -26,17 +26,16 @@ import class Foundation.JSONEncoder extension JSONDecoder: AWSLambdaRuntimeCore.LambdaEventDecoder {} -@usableFromInline -package struct LambdaJSONOutputEncoder: LambdaOutputEncoder { +public struct LambdaJSONOutputEncoder: LambdaOutputEncoder { @usableFromInline let jsonEncoder: JSONEncoder @inlinable - package init(_ jsonEncoder: JSONEncoder) { + public init(_ jsonEncoder: JSONEncoder) { self.jsonEncoder = jsonEncoder } @inlinable - package func encode(_ value: Output, into buffer: inout ByteBuffer) throws { + public func encode(_ value: Output, into buffer: inout ByteBuffer) throws { try self.jsonEncoder.encode(value, into: &buffer) } } @@ -47,7 +46,7 @@ extension LambdaCodableAdapter { /// - encoder: The encoder object that will be used to encode the generic ``Output`` obtained from the `handler`'s `outputWriter` into a ``ByteBuffer``. /// - decoder: The decoder object that will be used to decode the received ``ByteBuffer`` event into the generic ``Event`` type served to the `handler`. /// - handler: The handler object. - package init( + public init( encoder: JSONEncoder, decoder: JSONDecoder, handler: Handler @@ -71,7 +70,7 @@ extension LambdaRuntime { /// - Parameter body: The handler in the form of a closure. /// - Parameter encoder: The encoder object that will be used to encode the generic ``Output`` into a ``ByteBuffer``. ``JSONEncoder()`` used as default. /// - Parameter decoder: The decoder object that will be used to decode the incoming ``ByteBuffer`` event into the generic ``Event`` type. ``JSONDecoder()`` used as default. - package convenience init( + public convenience init( body: @escaping (Event, LambdaContext) async throws -> Output, encoder: JSONEncoder = JSONEncoder(), decoder: JSONDecoder = JSONDecoder() @@ -97,7 +96,7 @@ extension LambdaRuntime { /// Initialize an instance with a ``LambdaHandler`` defined in the form of a closure **with a `Void` return type**. /// - Parameter body: The handler in the form of a closure. /// - Parameter decoder: The decoder object that will be used to decode the incoming ``ByteBuffer`` event into the generic ``Event`` type. ``JSONDecoder()`` used as default. - package convenience init( + public convenience init( body: @escaping (Event, LambdaContext) async throws -> Void, decoder: JSONDecoder = JSONDecoder() ) diff --git a/Sources/AWSLambdaRuntimeCore/NewLambda+JSON.swift b/Sources/AWSLambdaRuntimeCore/Lambda+Codable.swift similarity index 87% rename from Sources/AWSLambdaRuntimeCore/NewLambda+JSON.swift rename to Sources/AWSLambdaRuntimeCore/Lambda+Codable.swift index b0d6b313..fdc4c0b4 100644 --- a/Sources/AWSLambdaRuntimeCore/NewLambda+JSON.swift +++ b/Sources/AWSLambdaRuntimeCore/Lambda+Codable.swift @@ -16,7 +16,7 @@ import NIOCore /// The protocol a decoder must conform to so that it can be used with ``LambdaCodableAdapter`` to decode incoming /// ``ByteBuffer`` events. -package protocol LambdaEventDecoder { +public protocol LambdaEventDecoder { /// Decode the ``ByteBuffer`` representing the received event into the generic ``Event`` type /// the handler will receive. /// - Parameters: @@ -28,7 +28,7 @@ package protocol LambdaEventDecoder { /// The protocol an encoder must conform to so that it can be used with ``LambdaCodableAdapter`` to encode the generic /// ``Output`` object into a ``ByteBuffer``. -package protocol LambdaOutputEncoder { +public protocol LambdaOutputEncoder { associatedtype Output /// Encode the generic type `Output` the handler has returned into a ``ByteBuffer``. @@ -38,15 +38,17 @@ package protocol LambdaOutputEncoder { func encode(_ value: Output, into buffer: inout ByteBuffer) throws } -package struct VoidEncoder: LambdaOutputEncoder { - package typealias Output = Void +public struct VoidEncoder: LambdaOutputEncoder { + public typealias Output = Void + + public init() {} @inlinable - package func encode(_ value: Void, into buffer: inout NIOCore.ByteBuffer) throws {} + public func encode(_ value: Void, into buffer: inout NIOCore.ByteBuffer) throws {} } /// Adapts a ``LambdaHandler`` conforming handler to conform to ``LambdaWithBackgroundProcessingHandler``. -package struct LambdaHandlerAdapter< +public struct LambdaHandlerAdapter< Event: Decodable, Output, Handler: LambdaHandler @@ -56,7 +58,7 @@ package struct LambdaHandlerAdapter< /// Initializes an instance given a concrete handler. /// - Parameter handler: The ``LambdaHandler`` conforming handler that is to be adapted to ``LambdaWithBackgroundProcessingHandler``. @inlinable - package init(handler: Handler) { + public init(handler: Handler) { self.handler = handler } @@ -67,7 +69,7 @@ package struct LambdaHandlerAdapter< /// - outputWriter: The writer to write the computed response to. /// - context: The ``LambdaContext`` containing the invocation's metadata. @inlinable - package func handle( + public func handle( _ event: Event, outputWriter: some LambdaResponseWriter, context: LambdaContext @@ -78,7 +80,7 @@ package struct LambdaHandlerAdapter< } /// Adapts a ``LambdaWithBackgroundProcessingHandler`` conforming handler to conform to ``StreamingLambdaHandler``. -package struct LambdaCodableAdapter< +public struct LambdaCodableAdapter< Handler: LambdaWithBackgroundProcessingHandler, Event: Decodable, Output, @@ -88,7 +90,6 @@ package struct LambdaCodableAdapter< @usableFromInline let handler: Handler @usableFromInline let encoder: Encoder @usableFromInline let decoder: Decoder - // @usableFromInline var byteBuffer: ByteBuffer = .init() /// Initializes an instance given an encoder, decoder, and a handler with a non-`Void` output. @@ -97,7 +98,7 @@ package struct LambdaCodableAdapter< /// - decoder: The decoder object that will be used to decode the received ``ByteBuffer`` event into the generic ``Event`` type served to the `handler`. /// - handler: The handler object. @inlinable - package init(encoder: Encoder, decoder: Decoder, handler: Handler) where Output: Encodable { + public init(encoder: Encoder, decoder: Decoder, handler: Handler) where Output: Encodable { self.encoder = encoder self.decoder = decoder self.handler = handler @@ -108,7 +109,7 @@ package struct LambdaCodableAdapter< /// - decoder: The decoder object that will be used to decode the received ``ByteBuffer`` event into the generic ``Event`` type served to the `handler`. /// - handler: The handler object. @inlinable - package init(decoder: Decoder, handler: Handler) where Output == Void, Encoder == VoidEncoder { + public init(decoder: Decoder, handler: Handler) where Output == Void, Encoder == VoidEncoder { self.encoder = VoidEncoder() self.decoder = decoder self.handler = handler @@ -120,7 +121,7 @@ package struct LambdaCodableAdapter< /// - outputWriter: The writer to write the computed response to. /// - context: The ``LambdaContext`` containing the invocation's metadata. @inlinable - package mutating func handle( + public mutating func handle( _ request: ByteBuffer, responseWriter: Writer, context: LambdaContext @@ -136,7 +137,7 @@ package struct LambdaCodableAdapter< } /// A ``LambdaResponseStreamWriter`` wrapper that conforms to ``LambdaResponseWriter``. -package struct LambdaCodableResponseWriter: +public struct LambdaCodableResponseWriter: LambdaResponseWriter where Output == Encoder.Output { @usableFromInline let underlyingStreamWriter: Base @@ -147,13 +148,13 @@ where Output == Encoder.Output { /// - encoder: The encoder object that will be used to encode the generic ``Output`` into a ``ByteBuffer``, which will then be passed to `streamWriter`. /// - streamWriter: The underlying ``LambdaResponseStreamWriter`` that will be wrapped. @inlinable - package init(encoder: Encoder, streamWriter: Base) { + public init(encoder: Encoder, streamWriter: Base) { self.encoder = encoder self.underlyingStreamWriter = streamWriter } @inlinable - package func write(_ output: Output) async throws { + public func write(_ output: Output) async throws { var outputBuffer = ByteBuffer() try self.encoder.encode(output, into: &outputBuffer) try await self.underlyingStreamWriter.writeAndFinish(outputBuffer) diff --git a/Sources/AWSLambdaRuntimeCore/Lambda.swift b/Sources/AWSLambdaRuntimeCore/Lambda.swift index 86ee912b..87026693 100644 --- a/Sources/AWSLambdaRuntimeCore/Lambda.swift +++ b/Sources/AWSLambdaRuntimeCore/Lambda.swift @@ -29,7 +29,7 @@ import ucrt #error("Unsupported platform") #endif -enum Lambda { +public enum Lambda { package static func runLoop( runtimeClient: RuntimeClient, handler: Handler, @@ -60,7 +60,7 @@ enum Lambda { } /// The default EventLoop the Lambda is scheduled on. - package static var defaultEventLoop: any EventLoop = NIOSingletons.posixEventLoopGroup.next() + public static var defaultEventLoop: any EventLoop = NIOSingletons.posixEventLoopGroup.next() } // MARK: - Public API diff --git a/Sources/AWSLambdaRuntimeCore/LambdaContext.swift b/Sources/AWSLambdaRuntimeCore/LambdaContext.swift index a2236618..291a732d 100644 --- a/Sources/AWSLambdaRuntimeCore/LambdaContext.swift +++ b/Sources/AWSLambdaRuntimeCore/LambdaContext.swift @@ -20,7 +20,7 @@ import NIOCore /// Lambda runtime context. /// The Lambda runtime generates and passes the `LambdaContext` to the Lambda handler as an argument. -package struct LambdaContext: CustomDebugStringConvertible, Sendable { +public struct LambdaContext: CustomDebugStringConvertible, Sendable { final class _Storage: Sendable { let requestID: String let traceID: String @@ -52,39 +52,39 @@ package struct LambdaContext: CustomDebugStringConvertible, Sendable { private var storage: _Storage /// The request ID, which identifies the request that triggered the function invocation. - package var requestID: String { + public var requestID: String { self.storage.requestID } /// The AWS X-Ray tracing header. - package var traceID: String { + public var traceID: String { self.storage.traceID } /// The ARN of the Lambda function, version, or alias that's specified in the invocation. - package var invokedFunctionARN: String { + public var invokedFunctionARN: String { self.storage.invokedFunctionARN } /// The timestamp that the function times out. - package var deadline: DispatchWallTime { + public var deadline: DispatchWallTime { self.storage.deadline } /// For invocations from the AWS Mobile SDK, data about the Amazon Cognito identity provider. - package var cognitoIdentity: String? { + public var cognitoIdentity: String? { self.storage.cognitoIdentity } /// For invocations from the AWS Mobile SDK, data about the client application and device. - package var clientContext: String? { + public var clientContext: String? { self.storage.clientContext } /// `Logger` to log with. /// /// - note: The `LogLevel` can be configured using the `LOG_LEVEL` environment variable. - package var logger: Logger { + public var logger: Logger { self.storage.logger } @@ -108,7 +108,7 @@ package struct LambdaContext: CustomDebugStringConvertible, Sendable { ) } - package func getRemainingTime() -> TimeAmount { + public func getRemainingTime() -> Duration { let deadline = self.deadline.millisSinceEpoch let now = DispatchWallTime.now().millisSinceEpoch @@ -116,7 +116,7 @@ package struct LambdaContext: CustomDebugStringConvertible, Sendable { return .milliseconds(remaining) } - package var debugDescription: String { + public var debugDescription: String { "\(Self.self)(requestID: \(self.requestID), traceID: \(self.traceID), invokedFunctionARN: \(self.invokedFunctionARN), cognitoIdentity: \(self.cognitoIdentity ?? "nil"), clientContext: \(self.clientContext ?? "nil"), deadline: \(self.deadline))" } diff --git a/Sources/AWSLambdaRuntimeCore/LambdaHandlers.swift b/Sources/AWSLambdaRuntimeCore/LambdaHandlers.swift index 9637f0ea..e112e29a 100644 --- a/Sources/AWSLambdaRuntimeCore/LambdaHandlers.swift +++ b/Sources/AWSLambdaRuntimeCore/LambdaHandlers.swift @@ -20,7 +20,7 @@ import NIOCore /// Background work can also be executed after returning the response. After closing the response stream by calling /// ``LambdaResponseStreamWriter/finish()`` or ``LambdaResponseStreamWriter/writeAndFinish(_:)``, /// the ``handle(_:responseWriter:context:)`` function is free to execute any background work. -package protocol StreamingLambdaHandler { +public protocol StreamingLambdaHandler { /// The handler function -- implement the business logic of the Lambda function here. /// - Parameters: /// - event: The invocation's input data. @@ -45,7 +45,7 @@ package protocol StreamingLambdaHandler { /// A writer object to write the Lambda response stream into. The HTTP response is started lazily. /// before the first call to ``write(_:)`` or ``writeAndFinish(_:)``. -package protocol LambdaResponseStreamWriter { +public protocol LambdaResponseStreamWriter { /// Write a response part into the stream. Bytes written are streamed continually. /// - Parameter buffer: The buffer to write. func write(_ buffer: ByteBuffer) async throws @@ -64,7 +64,7 @@ package protocol LambdaResponseStreamWriter { /// /// - note: This handler protocol does not support response streaming because the output has to be encoded prior to it being sent, e.g. it is not possible to encode a partial/incomplete JSON string. /// This protocol also does not support the execution of background work after the response has been returned -- the ``LambdaWithBackgroundProcessingHandler`` protocol caters for such use-cases. -package protocol LambdaHandler { +public protocol LambdaHandler { /// Generic input type. /// The body of the request sent to Lambda will be decoded into this type for the handler to consume. associatedtype Event: Decodable @@ -85,7 +85,7 @@ package protocol LambdaHandler { /// This is achieved by not having a return type in the `handle` function. The output is instead written into a /// ``LambdaResponseWriter``that is passed in as an argument, meaning that the ``handle(_:)`` function is then free to implement /// any background work after the result has been sent to the AWS Lambda control plane. -package protocol LambdaWithBackgroundProcessingHandler { +public protocol LambdaWithBackgroundProcessingHandler { /// Generic input type. /// The body of the request sent to Lambda will be decoded into this type for the handler to consume. associatedtype Event: Decodable @@ -109,7 +109,7 @@ package protocol LambdaWithBackgroundProcessingHandler { /// Used with ``LambdaWithBackgroundProcessingHandler``. /// A mechanism to "return" an output from ``LambdaWithBackgroundProcessingHandler/handle(_:outputWriter:context:)`` without the function needing to /// have a return type and exit at that point. This allows for background work to be executed _after_ a response has been sent to the AWS Lambda response endpoint. -package protocol LambdaResponseWriter { +public protocol LambdaResponseWriter { associatedtype Output /// Sends the generic ``Output`` object (representing the computed result of the handler) /// to the AWS Lambda response endpoint. @@ -120,12 +120,12 @@ package protocol LambdaResponseWriter { /// A ``StreamingLambdaHandler`` conforming handler object that can be constructed with a closure. /// Allows for a handler to be defined in a clean manner, leveraging Swift's trailing closure syntax. -package struct StreamingClosureHandler: StreamingLambdaHandler { +public struct StreamingClosureHandler: StreamingLambdaHandler { let body: @Sendable (ByteBuffer, LambdaResponseStreamWriter, LambdaContext) async throws -> Void /// Initialize an instance from a handler function in the form of a closure. /// - Parameter body: The handler function written as a closure. - package init( + public init( body: @Sendable @escaping (ByteBuffer, LambdaResponseStreamWriter, LambdaContext) async throws -> Void ) { self.body = body @@ -137,7 +137,7 @@ package struct StreamingClosureHandler: StreamingLambdaHandler { /// - responseWriter: A ``LambdaResponseStreamWriter`` to write the invocation's response to. /// If no response or error is written to `responseWriter` an error will be reported to the invoker. /// - context: The ``LambdaContext`` containing the invocation's metadata. - package func handle( + public func handle( _ request: ByteBuffer, responseWriter: some LambdaResponseStreamWriter, context: LambdaContext @@ -148,18 +148,18 @@ package struct StreamingClosureHandler: StreamingLambdaHandler { /// A ``LambdaHandler`` conforming handler object that can be constructed with a closure. /// Allows for a handler to be defined in a clean manner, leveraging Swift's trailing closure syntax. -package struct ClosureHandler: LambdaHandler { +public struct ClosureHandler: LambdaHandler { let body: (Event, LambdaContext) async throws -> Output /// Initialize with a closure handler over generic `Input` and `Output` types. /// - Parameter body: The handler function written as a closure. - package init(body: @escaping (Event, LambdaContext) async throws -> Output) where Output: Encodable { + public init(body: @escaping (Event, LambdaContext) async throws -> Output) where Output: Encodable { self.body = body } /// Initialize with a closure handler over a generic `Input` type, and a `Void` `Output`. /// - Parameter body: The handler function written as a closure. - package init(body: @escaping (Event, LambdaContext) async throws -> Void) where Output == Void { + public init(body: @escaping (Event, LambdaContext) async throws -> Void) where Output == Void { self.body = body } @@ -167,7 +167,7 @@ package struct ClosureHandler: LambdaHandler { /// - Parameters: /// - event: The generic ``Event`` object representing the invocation's input data. /// - context: The ``LambdaContext`` containing the invocation's metadata. - package func handle(_ event: Event, context: LambdaContext) async throws -> Output { + public func handle(_ event: Event, context: LambdaContext) async throws -> Output { try await self.body(event, context) } } @@ -175,7 +175,7 @@ package struct ClosureHandler: LambdaHandler { extension LambdaRuntime { /// Initialize an instance with a ``StreamingLambdaHandler`` in the form of a closure. /// - Parameter body: The handler in the form of a closure. - package convenience init( + public convenience init( body: @Sendable @escaping (ByteBuffer, LambdaResponseStreamWriter, LambdaContext) async throws -> Void ) where Handler == StreamingClosureHandler { self.init(handler: StreamingClosureHandler(body: body)) @@ -185,7 +185,7 @@ extension LambdaRuntime { /// - Parameter body: The handler in the form of a closure. /// - Parameter encoder: The encoder object that will be used to encode the generic ``Output`` into a ``ByteBuffer``. /// - Parameter decoder: The decoder object that will be used to decode the incoming ``ByteBuffer`` event into the generic ``Event`` type. - package convenience init< + public convenience init< Event: Decodable, Output: Encodable, Encoder: LambdaOutputEncoder, @@ -217,7 +217,7 @@ extension LambdaRuntime { /// - Parameter body: The handler in the form of a closure. /// - Parameter encoder: The encoder object that will be used to encode the generic ``Output`` into a ``ByteBuffer``. /// - Parameter decoder: The decoder object that will be used to decode the incoming ``ByteBuffer`` event into the generic ``Event`` type. - package convenience init( + public convenience init( decoder: Decoder, body: @escaping (Event, LambdaContext) async throws -> Void ) diff --git a/Sources/AWSLambdaRuntimeCore/LambdaRuntime.swift b/Sources/AWSLambdaRuntimeCore/LambdaRuntime.swift index c01215ac..f36c7ee3 100644 --- a/Sources/AWSLambdaRuntimeCore/LambdaRuntime.swift +++ b/Sources/AWSLambdaRuntimeCore/LambdaRuntime.swift @@ -20,13 +20,13 @@ import NIOConcurrencyHelpers // We need `@unchecked` Sendable here, as `NIOLockedValueBox` does not understand `sending` today. // We don't want to use `NIOLockedValueBox` here anyway. We would love to use Mutex here, but this // sadly crashes the compiler today. -package final class LambdaRuntime: @unchecked Sendable where Handler: StreamingLambdaHandler { +public final class LambdaRuntime: @unchecked Sendable where Handler: StreamingLambdaHandler { // TODO: We want to change this to Mutex as soon as this doesn't crash the Swift compiler on Linux anymore let handlerMutex: NIOLockedValueBox> let logger: Logger let eventLoop: EventLoop - package init( + public init( handler: sending Handler, eventLoop: EventLoop = Lambda.defaultEventLoop, logger: Logger = Logger(label: "LambdaRuntime") @@ -36,7 +36,7 @@ package final class LambdaRuntime: @unchecked Sendable where Handler: S self.logger = logger } - package func run() async throws { + public func run() async throws { guard let runtimeEndpoint = Lambda.env("AWS_LAMBDA_RUNTIME_API") else { throw LambdaRuntimeError(code: .missingLambdaRuntimeAPIEnvironmentVariable) }