mirror of
https://github.com/swift-server/swift-aws-lambda-runtime.git
synced 2026-05-03 07:22:27 +00:00
d42ae6975e
- Use the new Swift 6 `@available` macro to remove requirement on `.platform` in Package.swift. - DRY: define the swift settings once for all in `Package.swift` ### Motivation: - Remove the requirement to build on macOS 15 in `Package.swift`. This allows library builders and end users to be more flexible on their dependency requirements. - The code is optionally compiled on macOS 15 and Linux, but SPM don't enforce it anymore. - Avoid repeating ourself. Be sure the same settings are applied on all targets. ### Modifications: - Create a `var swiftSetting: [SwiftSettings]` and reuse it for all targets. - Use `AvailabilityMacro=LambdaSwift 2.0:macOS 15.0` - Add this on top of the majority struct / classes ```swift #if swift(>=6.1) @available(LambdaSwift 2.0, *) #endif ``` ### Result: When using Swift 6.1, there is no more SPM dependency on macOS 15
133 lines
5.1 KiB
Swift
133 lines
5.1 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftAWSLambdaRuntime open source project
|
|
//
|
|
// Copyright (c) 2021 Apple Inc. and the SwiftAWSLambdaRuntime project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import NIOCore
|
|
|
|
@available(LambdaSwift 2.0, *)
|
|
struct ControlPlaneRequestEncoder: _EmittingChannelHandler {
|
|
typealias OutboundOut = ByteBuffer
|
|
|
|
private var host: String
|
|
private var byteBuffer: ByteBuffer!
|
|
|
|
init(host: String) {
|
|
self.host = host
|
|
}
|
|
|
|
mutating func writeRequest(
|
|
_ request: ControlPlaneRequest,
|
|
context: ChannelHandlerContext,
|
|
promise: EventLoopPromise<Void>?
|
|
) {
|
|
self.byteBuffer.clear(minimumCapacity: self.byteBuffer.storageCapacity)
|
|
|
|
switch request {
|
|
case .next:
|
|
self.byteBuffer.writeString(.nextInvocationRequestLine)
|
|
self.byteBuffer.writeHostHeader(host: self.host)
|
|
self.byteBuffer.writeString(.userAgentHeader)
|
|
self.byteBuffer.writeString(.CRLF) // end of head
|
|
context.write(self.wrapOutboundOut(self.byteBuffer), promise: promise)
|
|
context.flush()
|
|
|
|
case .invocationResponse(let requestID, let payload):
|
|
let contentLength = payload?.readableBytes ?? 0
|
|
self.byteBuffer.writeInvocationResultRequestLine(requestID)
|
|
self.byteBuffer.writeHostHeader(host: self.host)
|
|
self.byteBuffer.writeString(.userAgentHeader)
|
|
self.byteBuffer.writeContentLengthHeader(length: contentLength)
|
|
self.byteBuffer.writeString(.CRLF) // end of head
|
|
if let payload = payload, contentLength > 0 {
|
|
context.write(self.wrapOutboundOut(self.byteBuffer), promise: nil)
|
|
context.write(self.wrapOutboundOut(payload), promise: promise)
|
|
} else {
|
|
context.write(self.wrapOutboundOut(self.byteBuffer), promise: promise)
|
|
}
|
|
context.flush()
|
|
|
|
case .invocationError(let requestID, let errorMessage):
|
|
let payload = errorMessage.toJSONBytes()
|
|
self.byteBuffer.writeInvocationErrorRequestLine(requestID)
|
|
self.byteBuffer.writeContentLengthHeader(length: payload.count)
|
|
self.byteBuffer.writeHostHeader(host: self.host)
|
|
self.byteBuffer.writeString(.userAgentHeader)
|
|
self.byteBuffer.writeString(.unhandledErrorHeader)
|
|
self.byteBuffer.writeString(.CRLF) // end of head
|
|
self.byteBuffer.writeBytes(payload)
|
|
context.write(self.wrapOutboundOut(self.byteBuffer), promise: promise)
|
|
context.flush()
|
|
|
|
case .initializationError(let errorMessage):
|
|
let payload = errorMessage.toJSONBytes()
|
|
self.byteBuffer.writeString(.runtimeInitErrorRequestLine)
|
|
self.byteBuffer.writeContentLengthHeader(length: payload.count)
|
|
self.byteBuffer.writeHostHeader(host: self.host)
|
|
self.byteBuffer.writeString(.userAgentHeader)
|
|
self.byteBuffer.writeString(.unhandledErrorHeader)
|
|
self.byteBuffer.writeString(.CRLF) // end of head
|
|
self.byteBuffer.writeBytes(payload)
|
|
context.write(self.wrapOutboundOut(self.byteBuffer), promise: promise)
|
|
context.flush()
|
|
}
|
|
}
|
|
|
|
mutating func writerAdded(context: ChannelHandlerContext) {
|
|
self.byteBuffer = context.channel.allocator.buffer(capacity: 256)
|
|
}
|
|
|
|
mutating func writerRemoved(context: ChannelHandlerContext) {
|
|
self.byteBuffer = nil
|
|
}
|
|
}
|
|
|
|
extension String {
|
|
static let CRLF: String = "\r\n"
|
|
|
|
static let userAgent = "Swift-Lambda/\(Version.current)"
|
|
static let userAgentHeader: String = "user-agent: \(userAgent)\r\n"
|
|
static let unhandledErrorHeader: String = "lambda-runtime-function-error-type: Unhandled\r\n"
|
|
|
|
static let nextInvocationRequestLine: String =
|
|
"GET /2018-06-01/runtime/invocation/next HTTP/1.1\r\n"
|
|
|
|
static let runtimeInitErrorRequestLine: String =
|
|
"POST /2018-06-01/runtime/init/error HTTP/1.1\r\n"
|
|
}
|
|
|
|
extension ByteBuffer {
|
|
fileprivate mutating func writeInvocationResultRequestLine(_ requestID: String) {
|
|
self.writeString("POST /2018-06-01/runtime/invocation/")
|
|
self.writeString(requestID)
|
|
self.writeString("/response HTTP/1.1\r\n")
|
|
}
|
|
|
|
fileprivate mutating func writeInvocationErrorRequestLine(_ requestID: String) {
|
|
self.writeString("POST /2018-06-01/runtime/invocation/")
|
|
self.writeString(requestID)
|
|
self.writeString("/error HTTP/1.1\r\n")
|
|
}
|
|
|
|
fileprivate mutating func writeHostHeader(host: String) {
|
|
self.writeString("host: ")
|
|
self.writeString(host)
|
|
self.writeString(.CRLF)
|
|
}
|
|
|
|
fileprivate mutating func writeContentLengthHeader(length: Int) {
|
|
self.writeString("content-length: ")
|
|
self.writeString("\(length)")
|
|
self.writeString(.CRLF)
|
|
}
|
|
}
|