mirror of
https://github.com/swift-server/swift-aws-lambda-runtime.git
synced 2026-06-02 07:27:33 +00:00
- 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.3 KiB
Swift
133 lines
5.3 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftAWSLambdaRuntime open source project
|
|
//
|
|
// Copyright (c) 2017-2018 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 NIOConcurrencyHelpers
|
|
import NIOPosix
|
|
|
|
// import Synchronization
|
|
|
|
enum Consts {
|
|
static let apiPrefix = "/2018-06-01"
|
|
static let invocationURLPrefix = "\(apiPrefix)/runtime/invocation"
|
|
static let getNextInvocationURLSuffix = "/next"
|
|
static let postResponseURLSuffix = "/response"
|
|
static let postErrorURLSuffix = "/error"
|
|
static let postInitErrorURL = "\(apiPrefix)/runtime/init/error"
|
|
static let functionError = "FunctionError"
|
|
static let initializationError = "InitializationError"
|
|
}
|
|
|
|
/// AWS Lambda HTTP Headers, used to populate the `LambdaContext` object.
|
|
enum AmazonHeaders {
|
|
static let requestID = "Lambda-Runtime-Aws-Request-Id"
|
|
static let traceID = "Lambda-Runtime-Trace-Id"
|
|
static let clientContext = "X-Amz-Client-Context"
|
|
static let cognitoIdentity = "X-Amz-Cognito-Identity"
|
|
static let deadline = "Lambda-Runtime-Deadline-Ms"
|
|
static let invokedFunctionARN = "Lambda-Runtime-Invoked-Function-Arn"
|
|
}
|
|
|
|
extension String {
|
|
func encodeAsJSONString(into bytes: inout [UInt8]) {
|
|
bytes.append(UInt8(ascii: "\""))
|
|
let stringBytes = self.utf8
|
|
var startCopyIndex = stringBytes.startIndex
|
|
var nextIndex = startCopyIndex
|
|
|
|
while nextIndex != stringBytes.endIndex {
|
|
switch stringBytes[nextIndex] {
|
|
case 0..<32, UInt8(ascii: "\""), UInt8(ascii: "\\"):
|
|
// All Unicode characters may be placed within the
|
|
// quotation marks, except for the characters that MUST be escaped:
|
|
// quotation mark, reverse solidus, and the control characters (U+0000
|
|
// through U+001F).
|
|
// https://tools.ietf.org/html/rfc7159#section-7
|
|
|
|
// copy the current range over
|
|
bytes.append(contentsOf: stringBytes[startCopyIndex..<nextIndex])
|
|
bytes.append(UInt8(ascii: "\\"))
|
|
bytes.append(stringBytes[nextIndex])
|
|
|
|
nextIndex = stringBytes.index(after: nextIndex)
|
|
startCopyIndex = nextIndex
|
|
default:
|
|
nextIndex = stringBytes.index(after: nextIndex)
|
|
}
|
|
}
|
|
|
|
// copy everything, that hasn't been copied yet
|
|
bytes.append(contentsOf: stringBytes[startCopyIndex..<nextIndex])
|
|
bytes.append(UInt8(ascii: "\""))
|
|
}
|
|
}
|
|
|
|
@available(LambdaSwift 2.0, *)
|
|
extension AmazonHeaders {
|
|
/// Generates (X-Ray) trace ID.
|
|
/// # Trace ID Format
|
|
/// A `trace_id` consists of three numbers separated by hyphens.
|
|
/// For example, `1-58406520-a006649127e371903a2de979`. This includes:
|
|
/// - The version number, that is, 1.
|
|
/// - The time of the original request, in Unix epoch time, in **8 hexadecimal digits**.
|
|
/// For example, 10:00AM December 1st, 2016 PST in epoch time is `1480615200` seconds, or `58406520` in hexadecimal digits.
|
|
/// - A 96-bit identifier for the trace, globally unique, in **24 hexadecimal digits**.
|
|
/// # References
|
|
/// - [Generating trace IDs](https://docs.aws.amazon.com/xray/latest/devguide/xray-api-sendingdata.html#xray-api-traceids)
|
|
/// - [Tracing header](https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-tracingheader)
|
|
static func generateXRayTraceID() -> String {
|
|
// The version number, that is, 1.
|
|
let version: UInt = 1
|
|
// The time of the original request, in Unix epoch time, in 8 hexadecimal digits.
|
|
let now = UInt32(LambdaClock().now.millisecondsSinceEpoch() / 1000)
|
|
let dateValue = String(now, radix: 16, uppercase: false)
|
|
let datePadding = String(repeating: "0", count: max(0, 8 - dateValue.count))
|
|
// A 96-bit identifier for the trace, globally unique, in 24 hexadecimal digits.
|
|
let identifier =
|
|
String(UInt64.random(in: UInt64.min...UInt64.max) | 1 << 63, radix: 16, uppercase: false)
|
|
+ String(UInt32.random(in: UInt32.min...UInt32.max) | 1 << 31, radix: 16, uppercase: false)
|
|
return "\(version)-\(datePadding)\(dateValue)-\(identifier)"
|
|
}
|
|
}
|
|
|
|
/// Temporary storage for value being sent from one isolation domain to another
|
|
// use NIOLockedValueBox instead of Mutex to avoid compiler crashes on 6.0
|
|
// see https://github.com/swiftlang/swift/issues/78048
|
|
@usableFromInline
|
|
struct SendingStorage<Value>: ~Copyable, @unchecked Sendable {
|
|
@usableFromInline
|
|
struct ValueAlreadySentError: Error {
|
|
@usableFromInline
|
|
init() {}
|
|
}
|
|
|
|
@usableFromInline
|
|
// let storage: Mutex<Value?>
|
|
let storage: NIOLockedValueBox<Value?>
|
|
|
|
@inlinable
|
|
init(_ value: sending Value) {
|
|
self.storage = .init(value)
|
|
}
|
|
|
|
@inlinable
|
|
func get() throws -> Value {
|
|
// try self.storage.withLock {
|
|
try self.storage.withLockedValue {
|
|
guard let value = $0 else { throw ValueAlreadySentError() }
|
|
$0 = nil
|
|
return value
|
|
}
|
|
}
|
|
}
|