-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathRuntime.swift
134 lines (106 loc) · 5.28 KB
/
Runtime.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import Foundation
public func log(_ object: Any, flush: Bool = false) {
fputs("\(object)\n", stderr)
if flush {
fflush(stderr)
}
}
public typealias JSONDictionary = [String: Any]
struct InvocationError: Codable {
let errorMessage: String
let errorType: String
}
public class Runtime {
let urlSession: URLSession
let awsLambdaRuntimeAPI: String
let handlerName: String
var handlers: [String: Handler]
public init() throws {
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 3600
self.urlSession = URLSession(configuration: configuration)
self.handlers = [:]
let environment = ProcessInfo.processInfo.environment
guard let awsLambdaRuntimeAPI = environment["AWS_LAMBDA_RUNTIME_API"],
let handler = environment["_HANDLER"] else {
throw RuntimeError.missingEnvironmentVariables
}
guard let periodIndex = handler.firstIndex(of: ".") else {
throw RuntimeError.invalidHandlerName
}
self.awsLambdaRuntimeAPI = awsLambdaRuntimeAPI
self.handlerName = String(handler[handler.index(after: periodIndex)...])
}
func getNextInvocation() throws -> (inputData: Data, responseHeaderFields: [AnyHashable: Any]) {
let getNextInvocationEndpoint = URL(string: "http://\(awsLambdaRuntimeAPI)/2018-06-01/runtime/invocation/next")!
let (optData, optResponse, optError) = urlSession.synchronousDataTask(with: getNextInvocationEndpoint)
guard optError == nil else {
throw RuntimeError.endpointError(optError!.localizedDescription)
}
guard let inputData = optData else {
throw RuntimeError.missingData
}
let httpResponse = optResponse as! HTTPURLResponse
return (inputData: inputData, responseHeaderFields: httpResponse.allHeaderFields)
}
func postInvocationResponse(for requestId: String, httpBody: Data) {
let postInvocationResponseEndpoint = URL(string: "http://\(awsLambdaRuntimeAPI)/2018-06-01/runtime/invocation/\(requestId)/response")!
var urlRequest = URLRequest(url: postInvocationResponseEndpoint)
urlRequest.httpMethod = "POST"
urlRequest.httpBody = httpBody
_ = urlSession.synchronousDataTask(with: urlRequest)
}
func postInvocationError(for requestId: String, error: Error) {
let errorMessage = error.localizedDescription
let invocationError = InvocationError(errorMessage: errorMessage,
errorType: "PostInvocationError")
let jsonEncoder = JSONEncoder()
let httpBody = try? jsonEncoder.encode(invocationError)
let postInvocationErrorEndpoint = URL(string: "http://\(awsLambdaRuntimeAPI)/2018-06-01/runtime/invocation/\(requestId)/error")!
var urlRequest = URLRequest(url: postInvocationErrorEndpoint)
urlRequest.httpMethod = "POST"
urlRequest.httpBody = httpBody
_ = urlSession.synchronousDataTask(with: urlRequest)
}
public func registerLambda(_ name: String, handlerFunction: @escaping (JSONDictionary, Context) throws -> JSONDictionary) {
let handler = JSONSyncHandler(handlerFunction: handlerFunction)
handlers[name] = .sync(handler)
}
public func registerLambda(_ name: String,
handlerFunction: @escaping (JSONDictionary, Context, @escaping (JSONDictionary) -> Void) -> Void) {
let handler = JSONAsyncHandler(handlerFunction: handlerFunction)
handlers[name] = .async(handler)
}
public func registerLambda<Input: Decodable, Output: Encodable>(_ name: String, handlerFunction: @escaping (Input, Context) throws -> Output) {
let handler = CodableSyncHandler(handlerFunction: handlerFunction)
handlers[name] = .sync(handler)
}
public func registerLambda<Input: Decodable, Output: Encodable>(_ name: String,
handlerFunction: @escaping (Input, Context, @escaping (Output) -> Void) -> Void) {
let handler = CodableAsyncHandler(handlerFunction: handlerFunction)
handlers[name] = .async(handler)
}
public func start() throws {
var counter = 0
while true {
let (inputData, responseHeaderFields) = try getNextInvocation()
counter += 1
log("Invocation-Counter: \(counter)")
guard let handler = handlers[handlerName] else {
throw RuntimeError.unknownLambdaHandler
}
if let lambdaRuntimeTraceId = responseHeaderFields["Lambda-Runtime-Trace-Id"] as? String {
setenv("_X_AMZN_TRACE_ID", lambdaRuntimeTraceId, 0)
}
let environment = ProcessInfo.processInfo.environment
let context = Context(environment: environment, responseHeaderFields: responseHeaderFields)
let result = handler.apply(inputData: inputData, context: context)
switch result {
case .success(let outputData):
postInvocationResponse(for: context.awsRequestId, httpBody: outputData)
case .failure(let error):
postInvocationError(for: context.awsRequestId, error: error)
}
}
}
}