Skip to content

Commit 237bcc4

Browse files
authored
Remove '#if canImport(NIOSSL)' (#22)
Motivation: Forks of this transport to remove NIOSSL can be done by removing the Posix module rather then removing NIOSSL. This means we no longer need the can-imports for NIOSSL. Modifications: Remove all instances of '#if canImport(NIOSSL)' Result: Simpler code
1 parent a2e1ea8 commit 237bcc4

File tree

5 files changed

+8
-34
lines changed

5 files changed

+8
-34
lines changed

Sources/GRPCNIOTransportHTTP2Posix/HTTP2ClientTransport+Posix.swift

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ public import GRPCCore
1818
public import GRPCNIOTransportCore // should be @usableFromInline
1919
public import NIOCore // has to be public because of EventLoopGroup param in init
2020
public import NIOPosix // has to be public because of default argument value in init
21-
22-
#if canImport(NIOSSL)
2321
private import NIOSSL
24-
#endif
2522

2623
extension HTTP2ClientTransport {
2724
/// A `ClientTransport` using HTTP/2 built on top of `NIOPosix`.
@@ -129,23 +126,20 @@ extension HTTP2ClientTransport.Posix {
129126
private let config: HTTP2ClientTransport.Posix.Config
130127
private let eventLoopGroup: any EventLoopGroup
131128

132-
#if canImport(NIOSSL)
133-
private let nioSSLContext: NIOSSLContext?
129+
private let sslContext: NIOSSLContext?
134130
private let serverHostname: String?
135-
#endif
136131

137132
init(eventLoopGroup: any EventLoopGroup, config: HTTP2ClientTransport.Posix.Config) throws {
138133
self.eventLoopGroup = eventLoopGroup
139134
self.config = config
140135

141-
#if canImport(NIOSSL)
142136
switch self.config.transportSecurity.wrapped {
143137
case .plaintext:
144-
self.nioSSLContext = nil
138+
self.sslContext = nil
145139
self.serverHostname = nil
146140
case .tls(let tlsConfig):
147141
do {
148-
self.nioSSLContext = try NIOSSLContext(configuration: TLSConfiguration(tlsConfig))
142+
self.sslContext = try NIOSSLContext(configuration: TLSConfiguration(tlsConfig))
149143
self.serverHostname = tlsConfig.serverHostname
150144
} catch {
151145
throw RuntimeError(
@@ -155,7 +149,6 @@ extension HTTP2ClientTransport.Posix {
155149
)
156150
}
157151
}
158-
#endif
159152
}
160153

161154
func establishConnection(
@@ -165,16 +158,14 @@ extension HTTP2ClientTransport.Posix {
165158
group: self.eventLoopGroup
166159
).connect(to: address) { channel in
167160
channel.eventLoop.makeCompletedFuture {
168-
#if canImport(NIOSSL)
169-
if let nioSSLContext = self.nioSSLContext {
161+
if let sslContext = self.sslContext {
170162
try channel.pipeline.syncOperations.addHandler(
171163
NIOSSLClientHandler(
172-
context: nioSSLContext,
164+
context: sslContext,
173165
serverHostname: self.serverHostname
174166
)
175167
)
176168
}
177-
#endif
178169

179170
return try channel.pipeline.syncOperations.configureGRPCClientPipeline(
180171
channel: channel,

Sources/GRPCNIOTransportHTTP2Posix/HTTP2ServerTransport+Posix.swift

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,9 @@ internal import NIOCore
2020
internal import NIOExtras
2121
internal import NIOHTTP2
2222
public import NIOPosix // has to be public because of default argument value in init
23+
private import NIOSSL
2324
private import Synchronization
2425

25-
#if canImport(NIOSSL)
26-
import NIOSSL
27-
#endif
28-
2926
extension HTTP2ServerTransport {
3027
/// A `ServerTransport` using HTTP/2 built on top of `NIOPosix`.
3128
///
@@ -63,7 +60,6 @@ extension HTTP2ServerTransport {
6360
address: GRPCNIOTransportCore.SocketAddress,
6461
serverQuiescingHelper: ServerQuiescingHelper
6562
) async throws -> NIOAsyncChannel<AcceptedChannel, Never> {
66-
#if canImport(NIOSSL)
6763
let sslContext: NIOSSLContext?
6864

6965
switch self.config.transportSecurity.wrapped {
@@ -80,7 +76,6 @@ extension HTTP2ServerTransport {
8076
)
8177
}
8278
}
83-
#endif
8479

8580
let serverChannel = try await ServerBootstrap(group: eventLoopGroup)
8681
.serverChannelOption(.socketOption(.so_reuseaddr), value: 1)
@@ -90,13 +85,11 @@ extension HTTP2ServerTransport {
9085
}
9186
.bind(to: address) { channel in
9287
channel.eventLoop.makeCompletedFuture {
93-
#if canImport(NIOSSL)
9488
if let sslContext {
9589
try channel.pipeline.syncOperations.addHandler(
9690
NIOSSLServerHandler(context: sslContext)
9791
)
9892
}
99-
#endif
10093

10194
let requireALPN: Bool
10295
let scheme: Scheme

Sources/GRPCNIOTransportHTTP2Posix/NIOSSL+GRPC.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
#if canImport(NIOSSL)
16+
1717
import NIOSSL
1818

1919
extension NIOSSLSerializationFormats {
@@ -159,4 +159,3 @@ extension TLSConfiguration {
159159
self.applicationProtocols = ["grpc-exp", "h2"]
160160
}
161161
}
162-
#endif

Sources/GRPCNIOTransportHTTP2Posix/TLSConfig.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,10 @@ extension HTTP2ServerTransport.Posix.Config {
146146
/// This connection is plaintext: no encryption will take place.
147147
public static let plaintext = Self(wrapped: .plaintext)
148148

149-
#if canImport(NIOSSL)
150149
/// This connection will use TLS.
151150
public static func tls(_ tls: TLS) -> Self {
152151
Self(wrapped: .tls(tls))
153152
}
154-
#endif
155153
}
156154

157155
public struct TLS: Sendable {
@@ -261,12 +259,10 @@ extension HTTP2ClientTransport.Posix.Config {
261259
/// This connection is plaintext: no encryption will take place.
262260
public static let plaintext = Self(wrapped: .plaintext)
263261

264-
#if canImport(NIOSSL)
265262
/// This connection will use TLS.
266263
public static func tls(_ tls: TLS) -> Self {
267264
Self(wrapped: .tls(tls))
268265
}
269-
#endif
270266
}
271267

272268
public struct TLS: Sendable {

Tests/GRPCNIOTransportHTTP2Tests/HTTP2TransportNIOPosixTests.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@
1717
import GRPCCore
1818
import GRPCNIOTransportCore
1919
import GRPCNIOTransportHTTP2Posix
20-
import XCTest
21-
22-
#if canImport(NIOSSL)
2320
import NIOSSL
24-
#endif
21+
import XCTest
2522

2623
final class HTTP2TransportNIOPosixTests: XCTestCase {
2724
func testGetListeningAddress_IPv4() async throws {
@@ -191,7 +188,6 @@ final class HTTP2TransportNIOPosixTests: XCTestCase {
191188
XCTAssertEqual(grpcConfig.backoff, HTTP2ClientTransport.Config.Backoff.defaults)
192189
}
193190

194-
#if canImport(NIOSSL)
195191
static let samplePemCert = """
196192
-----BEGIN CERTIFICATE-----
197193
MIIGGzCCBAOgAwIBAgIJAJ/X0Fo0ynmEMA0GCSqGSIb3DQEBCwUAMIGjMQswCQYD
@@ -478,5 +474,4 @@ final class HTTP2TransportNIOPosixTests: XCTestCase {
478474
XCTAssertEqual(nioSSLTLSConfig.trustRoots, .default)
479475
XCTAssertEqual(nioSSLTLSConfig.applicationProtocols, ["grpc-exp", "h2"])
480476
}
481-
#endif
482477
}

0 commit comments

Comments
 (0)