浏览代码

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
George Barnett 1 年之前
父节点
当前提交
237bcc41b9

+ 5 - 14
Sources/GRPCNIOTransportHTTP2Posix/HTTP2ClientTransport+Posix.swift

@@ -18,10 +18,7 @@ public import GRPCCore
 public import GRPCNIOTransportCore  // should be @usableFromInline
 public import GRPCNIOTransportCore  // should be @usableFromInline
 public import NIOCore  // has to be public because of EventLoopGroup param in init
 public import NIOCore  // has to be public because of EventLoopGroup param in init
 public import NIOPosix  // has to be public because of default argument value in init
 public import NIOPosix  // has to be public because of default argument value in init
-
-#if canImport(NIOSSL)
 private import NIOSSL
 private import NIOSSL
-#endif
 
 
 extension HTTP2ClientTransport {
 extension HTTP2ClientTransport {
   /// A `ClientTransport` using HTTP/2 built on top of `NIOPosix`.
   /// A `ClientTransport` using HTTP/2 built on top of `NIOPosix`.
@@ -129,23 +126,20 @@ extension HTTP2ClientTransport.Posix {
     private let config: HTTP2ClientTransport.Posix.Config
     private let config: HTTP2ClientTransport.Posix.Config
     private let eventLoopGroup: any EventLoopGroup
     private let eventLoopGroup: any EventLoopGroup
 
 
-    #if canImport(NIOSSL)
-    private let nioSSLContext: NIOSSLContext?
+    private let sslContext: NIOSSLContext?
     private let serverHostname: String?
     private let serverHostname: String?
-    #endif
 
 
     init(eventLoopGroup: any EventLoopGroup, config: HTTP2ClientTransport.Posix.Config) throws {
     init(eventLoopGroup: any EventLoopGroup, config: HTTP2ClientTransport.Posix.Config) throws {
       self.eventLoopGroup = eventLoopGroup
       self.eventLoopGroup = eventLoopGroup
       self.config = config
       self.config = config
 
 
-      #if canImport(NIOSSL)
       switch self.config.transportSecurity.wrapped {
       switch self.config.transportSecurity.wrapped {
       case .plaintext:
       case .plaintext:
-        self.nioSSLContext = nil
+        self.sslContext = nil
         self.serverHostname = nil
         self.serverHostname = nil
       case .tls(let tlsConfig):
       case .tls(let tlsConfig):
         do {
         do {
-          self.nioSSLContext = try NIOSSLContext(configuration: TLSConfiguration(tlsConfig))
+          self.sslContext = try NIOSSLContext(configuration: TLSConfiguration(tlsConfig))
           self.serverHostname = tlsConfig.serverHostname
           self.serverHostname = tlsConfig.serverHostname
         } catch {
         } catch {
           throw RuntimeError(
           throw RuntimeError(
@@ -155,7 +149,6 @@ extension HTTP2ClientTransport.Posix {
           )
           )
         }
         }
       }
       }
-      #endif
     }
     }
 
 
     func establishConnection(
     func establishConnection(
@@ -165,16 +158,14 @@ extension HTTP2ClientTransport.Posix {
         group: self.eventLoopGroup
         group: self.eventLoopGroup
       ).connect(to: address) { channel in
       ).connect(to: address) { channel in
         channel.eventLoop.makeCompletedFuture {
         channel.eventLoop.makeCompletedFuture {
-          #if canImport(NIOSSL)
-          if let nioSSLContext = self.nioSSLContext {
+          if let sslContext = self.sslContext {
             try channel.pipeline.syncOperations.addHandler(
             try channel.pipeline.syncOperations.addHandler(
               NIOSSLClientHandler(
               NIOSSLClientHandler(
-                context: nioSSLContext,
+                context: sslContext,
                 serverHostname: self.serverHostname
                 serverHostname: self.serverHostname
               )
               )
             )
             )
           }
           }
-          #endif
 
 
           return try channel.pipeline.syncOperations.configureGRPCClientPipeline(
           return try channel.pipeline.syncOperations.configureGRPCClientPipeline(
             channel: channel,
             channel: channel,

+ 1 - 8
Sources/GRPCNIOTransportHTTP2Posix/HTTP2ServerTransport+Posix.swift

@@ -20,12 +20,9 @@ internal import NIOCore
 internal import NIOExtras
 internal import NIOExtras
 internal import NIOHTTP2
 internal import NIOHTTP2
 public import NIOPosix  // has to be public because of default argument value in init
 public import NIOPosix  // has to be public because of default argument value in init
+private import NIOSSL
 private import Synchronization
 private import Synchronization
 
 
-#if canImport(NIOSSL)
-import NIOSSL
-#endif
-
 extension HTTP2ServerTransport {
 extension HTTP2ServerTransport {
   /// A `ServerTransport` using HTTP/2 built on top of `NIOPosix`.
   /// A `ServerTransport` using HTTP/2 built on top of `NIOPosix`.
   ///
   ///
@@ -63,7 +60,6 @@ extension HTTP2ServerTransport {
         address: GRPCNIOTransportCore.SocketAddress,
         address: GRPCNIOTransportCore.SocketAddress,
         serverQuiescingHelper: ServerQuiescingHelper
         serverQuiescingHelper: ServerQuiescingHelper
       ) async throws -> NIOAsyncChannel<AcceptedChannel, Never> {
       ) async throws -> NIOAsyncChannel<AcceptedChannel, Never> {
-        #if canImport(NIOSSL)
         let sslContext: NIOSSLContext?
         let sslContext: NIOSSLContext?
 
 
         switch self.config.transportSecurity.wrapped {
         switch self.config.transportSecurity.wrapped {
@@ -80,7 +76,6 @@ extension HTTP2ServerTransport {
             )
             )
           }
           }
         }
         }
-        #endif
 
 
         let serverChannel = try await ServerBootstrap(group: eventLoopGroup)
         let serverChannel = try await ServerBootstrap(group: eventLoopGroup)
           .serverChannelOption(.socketOption(.so_reuseaddr), value: 1)
           .serverChannelOption(.socketOption(.so_reuseaddr), value: 1)
@@ -90,13 +85,11 @@ extension HTTP2ServerTransport {
           }
           }
           .bind(to: address) { channel in
           .bind(to: address) { channel in
             channel.eventLoop.makeCompletedFuture {
             channel.eventLoop.makeCompletedFuture {
-              #if canImport(NIOSSL)
               if let sslContext {
               if let sslContext {
                 try channel.pipeline.syncOperations.addHandler(
                 try channel.pipeline.syncOperations.addHandler(
                   NIOSSLServerHandler(context: sslContext)
                   NIOSSLServerHandler(context: sslContext)
                 )
                 )
               }
               }
-              #endif
 
 
               let requireALPN: Bool
               let requireALPN: Bool
               let scheme: Scheme
               let scheme: Scheme

+ 1 - 2
Sources/GRPCNIOTransportHTTP2Posix/NIOSSL+GRPC.swift

@@ -13,7 +13,7 @@
  * See the License for the specific language governing permissions and
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * limitations under the License.
  */
  */
-#if canImport(NIOSSL)
+
 import NIOSSL
 import NIOSSL
 
 
 extension NIOSSLSerializationFormats {
 extension NIOSSLSerializationFormats {
@@ -159,4 +159,3 @@ extension TLSConfiguration {
     self.applicationProtocols = ["grpc-exp", "h2"]
     self.applicationProtocols = ["grpc-exp", "h2"]
   }
   }
 }
 }
-#endif

+ 0 - 4
Sources/GRPCNIOTransportHTTP2Posix/TLSConfig.swift

@@ -146,12 +146,10 @@ extension HTTP2ServerTransport.Posix.Config {
     /// This connection is plaintext: no encryption will take place.
     /// This connection is plaintext: no encryption will take place.
     public static let plaintext = Self(wrapped: .plaintext)
     public static let plaintext = Self(wrapped: .plaintext)
 
 
-    #if canImport(NIOSSL)
     /// This connection will use TLS.
     /// This connection will use TLS.
     public static func tls(_ tls: TLS) -> Self {
     public static func tls(_ tls: TLS) -> Self {
       Self(wrapped: .tls(tls))
       Self(wrapped: .tls(tls))
     }
     }
-    #endif
   }
   }
 
 
   public struct TLS: Sendable {
   public struct TLS: Sendable {
@@ -261,12 +259,10 @@ extension HTTP2ClientTransport.Posix.Config {
     /// This connection is plaintext: no encryption will take place.
     /// This connection is plaintext: no encryption will take place.
     public static let plaintext = Self(wrapped: .plaintext)
     public static let plaintext = Self(wrapped: .plaintext)
 
 
-    #if canImport(NIOSSL)
     /// This connection will use TLS.
     /// This connection will use TLS.
     public static func tls(_ tls: TLS) -> Self {
     public static func tls(_ tls: TLS) -> Self {
       Self(wrapped: .tls(tls))
       Self(wrapped: .tls(tls))
     }
     }
-    #endif
   }
   }
 
 
   public struct TLS: Sendable {
   public struct TLS: Sendable {

+ 1 - 6
Tests/GRPCNIOTransportHTTP2Tests/HTTP2TransportNIOPosixTests.swift

@@ -17,11 +17,8 @@
 import GRPCCore
 import GRPCCore
 import GRPCNIOTransportCore
 import GRPCNIOTransportCore
 import GRPCNIOTransportHTTP2Posix
 import GRPCNIOTransportHTTP2Posix
-import XCTest
-
-#if canImport(NIOSSL)
 import NIOSSL
 import NIOSSL
-#endif
+import XCTest
 
 
 final class HTTP2TransportNIOPosixTests: XCTestCase {
 final class HTTP2TransportNIOPosixTests: XCTestCase {
   func testGetListeningAddress_IPv4() async throws {
   func testGetListeningAddress_IPv4() async throws {
@@ -191,7 +188,6 @@ final class HTTP2TransportNIOPosixTests: XCTestCase {
     XCTAssertEqual(grpcConfig.backoff, HTTP2ClientTransport.Config.Backoff.defaults)
     XCTAssertEqual(grpcConfig.backoff, HTTP2ClientTransport.Config.Backoff.defaults)
   }
   }
 
 
-  #if canImport(NIOSSL)
   static let samplePemCert = """
   static let samplePemCert = """
     -----BEGIN CERTIFICATE-----
     -----BEGIN CERTIFICATE-----
     MIIGGzCCBAOgAwIBAgIJAJ/X0Fo0ynmEMA0GCSqGSIb3DQEBCwUAMIGjMQswCQYD
     MIIGGzCCBAOgAwIBAgIJAJ/X0Fo0ynmEMA0GCSqGSIb3DQEBCwUAMIGjMQswCQYD
@@ -478,5 +474,4 @@ final class HTTP2TransportNIOPosixTests: XCTestCase {
     XCTAssertEqual(nioSSLTLSConfig.trustRoots, .default)
     XCTAssertEqual(nioSSLTLSConfig.trustRoots, .default)
     XCTAssertEqual(nioSSLTLSConfig.applicationProtocols, ["grpc-exp", "h2"])
     XCTAssertEqual(nioSSLTLSConfig.applicationProtocols, ["grpc-exp", "h2"])
   }
   }
-  #endif
 }
 }