|
| 1 | +import Foundation |
| 2 | + |
| 3 | +func log(_ object: Any, flush: Bool = false) { |
| 4 | + fputs("\(object)\n", stderr) |
| 5 | + if flush { |
| 6 | + fflush(stderr) |
| 7 | + } |
| 8 | +} |
| 9 | + |
| 10 | +public typealias JSONDictionary = [String: Any] |
| 11 | + |
| 12 | +public class Runtime { |
| 13 | + var counter = 0 |
| 14 | + let urlSession: URLSession |
| 15 | + let awsLambdaRuntimeAPI: String |
| 16 | + let handler: String |
| 17 | + var lambdas: [String: (JSONDictionary) -> JSONDictionary] |
| 18 | + |
| 19 | + public init() throws { |
| 20 | + self.urlSession = URLSession.shared |
| 21 | + self.lambdas = [:] |
| 22 | + |
| 23 | + let environment = ProcessInfo.processInfo.environment |
| 24 | + guard let awsLambdaRuntimeAPI = environment["AWS_LAMBDA_RUNTIME_API"], |
| 25 | + let handler = environment["_HANDLER"] else { |
| 26 | + throw RuntimeError.missingEnvironmentVariables |
| 27 | + } |
| 28 | + |
| 29 | + self.awsLambdaRuntimeAPI = awsLambdaRuntimeAPI |
| 30 | + self.handler = handler |
| 31 | + } |
| 32 | + |
| 33 | + func getNextInvocation() throws -> (input: JSONDictionary, requestId: String) { |
| 34 | + let getNextInvocationEndpoint = URL(string: "http://\(awsLambdaRuntimeAPI)/2018-06-01/runtime/invocation/next")! |
| 35 | + let (optData, optResponse, optError) = urlSession.synchronousDataTask(with: getNextInvocationEndpoint) |
| 36 | + |
| 37 | + guard optError == nil else { |
| 38 | + throw RuntimeError.endpointError(optError!.localizedDescription) |
| 39 | + } |
| 40 | + |
| 41 | + guard let data = optData else { |
| 42 | + throw RuntimeError.missingData |
| 43 | + } |
| 44 | + |
| 45 | + guard let jsonObject = try? JSONSerialization.jsonObject(with: data), |
| 46 | + let input = jsonObject as? JSONDictionary else { |
| 47 | + throw RuntimeError.invalidData |
| 48 | + } |
| 49 | + |
| 50 | + let httpResponse = optResponse as! HTTPURLResponse |
| 51 | + let requestId = httpResponse.allHeaderFields["Lambda-Runtime-Aws-Request-Id"] as! String |
| 52 | + return (input: input, requestId: requestId) |
| 53 | + } |
| 54 | + |
| 55 | + func postInvocationResponse(for requestId: String, response: JSONDictionary) throws { |
| 56 | + let postInvocationResponseEndpoint = URL(string: "http://\(awsLambdaRuntimeAPI)/2018-06-01/runtime/invocation/\(requestId)/response")! |
| 57 | + guard let httpBody = try? JSONSerialization.data(withJSONObject: response) else { |
| 58 | + throw RuntimeError.invalidData |
| 59 | + } |
| 60 | + |
| 61 | + var urlRequest = URLRequest(url: postInvocationResponseEndpoint) |
| 62 | + urlRequest.httpMethod = "POST" |
| 63 | + urlRequest.httpBody = httpBody |
| 64 | + |
| 65 | + _ = urlSession.synchronousDataTask(with: urlRequest) |
| 66 | + } |
| 67 | + |
| 68 | + public func registerLambda(_ name: String, handler: @escaping (JSONDictionary) -> JSONDictionary) { |
| 69 | + lambdas[name] = handler |
| 70 | + } |
| 71 | + |
| 72 | + public func start() throws { |
| 73 | + while true { |
| 74 | + let (input, requestId) = try getNextInvocation() |
| 75 | + counter += 1 |
| 76 | + log("Invocation-Counter: \(counter)") |
| 77 | + |
| 78 | + let lambda = lambdas["lambda"]! |
| 79 | + let output = lambda(input) |
| 80 | + try postInvocationResponse(for: requestId, response: output) |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments