|
1 |
| -//===----------------------------------------------------------------------===// |
2 |
| -// |
3 |
| -// This source file is part of the SwiftAWSLambdaRuntime open source project |
4 |
| -// |
5 |
| -// Copyright (c) 2021 Apple Inc. and the SwiftAWSLambdaRuntime project authors |
6 |
| -// Licensed under Apache License v2.0 |
7 |
| -// |
8 |
| -// See LICENSE.txt for license information |
9 |
| -// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors |
10 |
| -// |
11 |
| -// SPDX-License-Identifier: Apache-2.0 |
12 |
| -// |
13 |
| -//===----------------------------------------------------------------------===// |
14 |
| - |
| 1 | +@_spi(Lambda) import LambdaRuntimeCore |
15 | 2 | import NIOCore
|
16 | 3 |
|
17 |
| -struct ControlPlaneRequestEncoder: _EmittingChannelHandler { |
18 |
| - typealias OutboundOut = ByteBuffer |
| 4 | +@_spi(Lambda) |
| 5 | +extension AWSLambda { |
| 6 | + public struct RequestEncoder: ControlPlaneRequestEncoder { |
| 7 | + private var host: String |
| 8 | + private var byteBuffer: ByteBuffer! |
19 | 9 |
|
20 |
| - private var host: String |
21 |
| - private var byteBuffer: ByteBuffer! |
| 10 | + public init(host: String) { |
| 11 | + self.host = host |
| 12 | + } |
22 | 13 |
|
23 |
| - init(host: String) { |
24 |
| - self.host = host |
25 |
| - } |
| 14 | + @_spi(Lambda) public mutating func writeRequest(_ request: ControlPlaneRequest, context: ChannelHandlerContext, promise: EventLoopPromise<Void>?) { |
| 15 | + self.byteBuffer.clear(minimumCapacity: self.byteBuffer.storageCapacity) |
26 | 16 |
|
27 |
| - mutating func writeRequest(_ request: ControlPlaneRequest, context: ChannelHandlerContext, promise: EventLoopPromise<Void>?) { |
28 |
| - self.byteBuffer.clear(minimumCapacity: self.byteBuffer.storageCapacity) |
29 |
| - |
30 |
| - switch request { |
31 |
| - case .next: |
32 |
| - self.byteBuffer.writeString(.nextInvocationRequestLine) |
33 |
| - self.byteBuffer.writeHostHeader(host: self.host) |
34 |
| - self.byteBuffer.writeString(.userAgentHeader) |
35 |
| - self.byteBuffer.writeString(.CRLF) // end of head |
36 |
| - context.write(self.wrapOutboundOut(self.byteBuffer), promise: promise) |
37 |
| - context.flush() |
38 |
| - |
39 |
| - case .invocationResponse(let requestID, let payload): |
40 |
| - let contentLength = payload?.readableBytes ?? 0 |
41 |
| - self.byteBuffer.writeInvocationResultRequestLine(requestID) |
42 |
| - self.byteBuffer.writeHostHeader(host: self.host) |
43 |
| - self.byteBuffer.writeString(.userAgentHeader) |
44 |
| - self.byteBuffer.writeContentLengthHeader(length: contentLength) |
45 |
| - self.byteBuffer.writeString(.CRLF) // end of head |
46 |
| - if let payload = payload, contentLength > 0 { |
47 |
| - context.write(self.wrapOutboundOut(self.byteBuffer), promise: nil) |
48 |
| - context.write(self.wrapOutboundOut(payload), promise: promise) |
49 |
| - } else { |
| 17 | + switch request { |
| 18 | + case .next: |
| 19 | + self.byteBuffer.writeString(.nextInvocationRequestLine) |
| 20 | + self.byteBuffer.writeHostHeader(host: self.host) |
| 21 | + self.byteBuffer.writeString(.userAgentHeader) |
| 22 | + self.byteBuffer.writeString(.CRLF) // end of head |
| 23 | + context.write(self.wrapOutboundOut(self.byteBuffer), promise: promise) |
| 24 | + context.flush() |
| 25 | + |
| 26 | + case .invocationResponse(let requestID, let payload): |
| 27 | + let contentLength = payload?.readableBytes ?? 0 |
| 28 | + self.byteBuffer.writeInvocationResultRequestLine(requestID) |
| 29 | + self.byteBuffer.writeHostHeader(host: self.host) |
| 30 | + self.byteBuffer.writeString(.userAgentHeader) |
| 31 | + self.byteBuffer.writeContentLengthHeader(length: contentLength) |
| 32 | + self.byteBuffer.writeString(.CRLF) // end of head |
| 33 | + if let payload = payload, contentLength > 0 { |
| 34 | + context.write(self.wrapOutboundOut(self.byteBuffer), promise: nil) |
| 35 | + context.write(self.wrapOutboundOut(payload), promise: promise) |
| 36 | + } else { |
| 37 | + context.write(self.wrapOutboundOut(self.byteBuffer), promise: promise) |
| 38 | + } |
| 39 | + context.flush() |
| 40 | + |
| 41 | + case .invocationError(let requestID, let errorMessage): |
| 42 | + let payload = errorMessage.toJSONBytes() |
| 43 | + self.byteBuffer.writeInvocationErrorRequestLine(requestID) |
| 44 | + self.byteBuffer.writeContentLengthHeader(length: payload.count) |
| 45 | + self.byteBuffer.writeHostHeader(host: self.host) |
| 46 | + self.byteBuffer.writeString(.userAgentHeader) |
| 47 | + self.byteBuffer.writeString(.unhandledErrorHeader) |
| 48 | + self.byteBuffer.writeString(.CRLF) // end of head |
| 49 | + self.byteBuffer.writeBytes(payload) |
| 50 | + context.write(self.wrapOutboundOut(self.byteBuffer), promise: promise) |
| 51 | + context.flush() |
| 52 | + |
| 53 | + case .initializationError(let errorMessage): |
| 54 | + let payload = errorMessage.toJSONBytes() |
| 55 | + self.byteBuffer.writeString(.runtimeInitErrorRequestLine) |
| 56 | + self.byteBuffer.writeContentLengthHeader(length: payload.count) |
| 57 | + self.byteBuffer.writeHostHeader(host: self.host) |
| 58 | + self.byteBuffer.writeString(.userAgentHeader) |
| 59 | + self.byteBuffer.writeString(.unhandledErrorHeader) |
| 60 | + self.byteBuffer.writeString(.CRLF) // end of head |
| 61 | + self.byteBuffer.writeBytes(payload) |
50 | 62 | context.write(self.wrapOutboundOut(self.byteBuffer), promise: promise)
|
| 63 | + context.flush() |
51 | 64 | }
|
52 |
| - context.flush() |
53 |
| - |
54 |
| - case .invocationError(let requestID, let errorMessage): |
55 |
| - let payload = errorMessage.toJSONBytes() |
56 |
| - self.byteBuffer.writeInvocationErrorRequestLine(requestID) |
57 |
| - self.byteBuffer.writeContentLengthHeader(length: payload.count) |
58 |
| - self.byteBuffer.writeHostHeader(host: self.host) |
59 |
| - self.byteBuffer.writeString(.userAgentHeader) |
60 |
| - self.byteBuffer.writeString(.unhandledErrorHeader) |
61 |
| - self.byteBuffer.writeString(.CRLF) // end of head |
62 |
| - self.byteBuffer.writeBytes(payload) |
63 |
| - context.write(self.wrapOutboundOut(self.byteBuffer), promise: promise) |
64 |
| - context.flush() |
65 |
| - |
66 |
| - case .initializationError(let errorMessage): |
67 |
| - let payload = errorMessage.toJSONBytes() |
68 |
| - self.byteBuffer.writeString(.runtimeInitErrorRequestLine) |
69 |
| - self.byteBuffer.writeContentLengthHeader(length: payload.count) |
70 |
| - self.byteBuffer.writeHostHeader(host: self.host) |
71 |
| - self.byteBuffer.writeString(.userAgentHeader) |
72 |
| - self.byteBuffer.writeString(.unhandledErrorHeader) |
73 |
| - self.byteBuffer.writeString(.CRLF) // end of head |
74 |
| - self.byteBuffer.writeBytes(payload) |
75 |
| - context.write(self.wrapOutboundOut(self.byteBuffer), promise: promise) |
76 |
| - context.flush() |
77 | 65 | }
|
78 |
| - } |
79 | 66 |
|
80 |
| - mutating func writerAdded(context: ChannelHandlerContext) { |
81 |
| - self.byteBuffer = context.channel.allocator.buffer(capacity: 256) |
82 |
| - } |
| 67 | + public mutating func writerAdded(context: ChannelHandlerContext) { |
| 68 | + self.byteBuffer = context.channel.allocator.buffer(capacity: 256) |
| 69 | + } |
83 | 70 |
|
84 |
| - mutating func writerRemoved(context: ChannelHandlerContext) { |
85 |
| - self.byteBuffer = nil |
| 71 | + public mutating func writerRemoved(context: ChannelHandlerContext) { |
| 72 | + self.byteBuffer = nil |
| 73 | + } |
86 | 74 | }
|
87 | 75 | }
|
88 | 76 |
|
@@ -124,3 +112,4 @@ extension ByteBuffer {
|
124 | 112 | self.writeString(.CRLF)
|
125 | 113 | }
|
126 | 114 | }
|
| 115 | + |
0 commit comments