From 6db4d39a113fe919a03e8c38d6aa8275cc6cd4bc Mon Sep 17 00:00:00 2001 From: Michael Rebello Date: Mon, 27 Mar 2023 18:37:09 -0700 Subject: [PATCH 1/2] Update gRPC and gRPC-Web links --- Examples/ElizaCocoaPodsApp/README.md | 4 +-- Examples/ElizaSwiftPackageApp/README.md | 8 ++--- .../Interceptors/InterceptorChain.swift | 16 ++++------ .../Connect/Interfaces/Interceptor.swift | 32 +++++++++++-------- README.md | 4 +-- 5 files changed, 33 insertions(+), 31 deletions(-) diff --git a/Examples/ElizaCocoaPodsApp/README.md b/Examples/ElizaCocoaPodsApp/README.md index 4ef59c41..3c2ceaf4 100644 --- a/Examples/ElizaCocoaPodsApp/README.md +++ b/Examples/ElizaCocoaPodsApp/README.md @@ -9,8 +9,8 @@ the Connect library: - [Connect](https://connect.build) + unary - [Connect](https://connect.build) + streaming -- [gRPC-Web](https://github.com/grpc/grpc-web) + unary -- [gRPC-Web](https://github.com/grpc/grpc-web) + streaming +- [gRPC-Web](https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-WEB.md) + unary +- [gRPC-Web](https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-WEB.md) + streaming **Note that vanilla gRPC support is not available in this example because [SwiftNIO does not support CocoaPods](https://github.com/apple/swift-nio/issues/2393).** diff --git a/Examples/ElizaSwiftPackageApp/README.md b/Examples/ElizaSwiftPackageApp/README.md index 0161144e..bd406722 100644 --- a/Examples/ElizaSwiftPackageApp/README.md +++ b/Examples/ElizaSwiftPackageApp/README.md @@ -9,10 +9,10 @@ the Connect library: - [Connect](https://connect.build) + unary - [Connect](https://connect.build) + streaming -- [gRPC](https://grpc.io) + unary (using `ConnectGRPC` + `SwiftNIO`) -- [gRPC](https://grpc.io) + streaming (using `ConnectGRPC` + `SwiftNIO`) -- [gRPC-Web](https://github.com/grpc/grpc-web) + unary -- [gRPC-Web](https://github.com/grpc/grpc-web) + streaming +- [gRPC](https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md) + unary (using `ConnectGRPC` + `SwiftNIO`) +- [gRPC](https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md) + streaming (using `ConnectGRPC` + `SwiftNIO`) +- [gRPC-Web](https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-WEB.md) + unary +- [gRPC-Web](https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-WEB.md) + streaming ## Try it out diff --git a/Libraries/Connect/Implementation/Interceptors/InterceptorChain.swift b/Libraries/Connect/Implementation/Interceptors/InterceptorChain.swift index e92866ad..234c90bb 100644 --- a/Libraries/Connect/Implementation/Interceptors/InterceptorChain.swift +++ b/Libraries/Connect/Implementation/Interceptors/InterceptorChain.swift @@ -36,8 +36,14 @@ struct InterceptorChain { /// `caller -> a -> b -> c -> server` /// `caller <- c <- b <- a <- server` /// + /// - parameter send: <#UnaryFunction#> + /// /// - returns: A set of closures that each invoke the chain of interceptors in the above order. - func unaryFunction() -> UnaryFunction { + func unaryFunction(send: UnaryFunction) -> UnaryFunction { + var next = send + for interceptor in self.interceptors { + next = UnaryFunction(requestFunction: <#T##(HTTPRequest) -> Void#>, responseFunction: <#T##(HTTPResponse) -> Void#>, responseMetricsFunction: <#T##(HTTPMetrics) -> Void#>) + } let interceptors = self.interceptors.map { $0.unaryFunction() } return UnaryFunction( requestFunction: { request in @@ -84,11 +90,3 @@ struct InterceptorChain { ) } } - -private func executeInterceptors(_ interceptors: [(T) -> T], initial: T) -> T { - var next = initial - for interceptor in interceptors { - next = interceptor(next) - } - return next -} diff --git a/Libraries/Connect/Interfaces/Interceptor.swift b/Libraries/Connect/Interfaces/Interceptor.swift index 5db787d9..0539d720 100644 --- a/Libraries/Connect/Interfaces/Interceptor.swift +++ b/Libraries/Connect/Interfaces/Interceptor.swift @@ -22,8 +22,10 @@ public protocol Interceptor { /// Invoked when a unary call is started. Provides a set of closures that will be called /// as the request progresses, allowing the interceptor to alter request/response data. /// + /// - parameter next: <#UnaryFunction#> + /// /// - returns: A new set of closures which can be used to read/alter request/response data. - func unaryFunction() -> UnaryFunction + func unaryFunction(next: UnaryFunction) -> UnaryFunction /// Invoked when a streaming call is started. Provides a set of closures that will be called /// as the stream progresses, allowing the interceptor to alter request/response data. @@ -33,19 +35,21 @@ public protocol Interceptor { /// will contain 1 full message (for Connect and gRPC, this includes the prefix and message /// length bytes, followed by the actual message data). /// + /// - parameter next: <#StreamResult#> + /// /// - returns: A new set of closures which can be used to read/alter request/response data. - func streamFunction() -> StreamFunction + func streamFunction(next: StreamFunction) -> StreamFunction } public struct UnaryFunction { - public let requestFunction: (HTTPRequest) -> HTTPRequest - public let responseFunction: (HTTPResponse) -> HTTPResponse - public let responseMetricsFunction: (HTTPMetrics) -> HTTPMetrics + public let requestFunction: (HTTPRequest) -> Void + public let responseFunction: (HTTPResponse) -> Void + public let responseMetricsFunction: (HTTPMetrics) -> Void public init( - requestFunction: @escaping (HTTPRequest) -> HTTPRequest, - responseFunction: @escaping (HTTPResponse) -> HTTPResponse, - responseMetricsFunction: @escaping (HTTPMetrics) -> HTTPMetrics = { $0 } + requestFunction: @escaping (HTTPRequest) -> Void, + responseFunction: @escaping (HTTPResponse) -> Void, + responseMetricsFunction: @escaping (HTTPMetrics) -> Void ) { self.requestFunction = requestFunction self.responseFunction = responseFunction @@ -54,14 +58,14 @@ public struct UnaryFunction { } public struct StreamFunction { - public let requestFunction: (HTTPRequest) -> HTTPRequest - public let requestDataFunction: (Data) -> Data - public let streamResultFunction: (StreamResult) -> StreamResult + public let requestFunction: (HTTPRequest) -> Void + public let requestDataFunction: (Data) -> Void + public let streamResultFunction: (StreamResult) -> Void public init( - requestFunction: @escaping (HTTPRequest) -> HTTPRequest, - requestDataFunction: @escaping (Data) -> Data, - streamResultFunction: @escaping (StreamResult) -> StreamResult + requestFunction: @escaping (HTTPRequest) -> Void, + requestDataFunction: @escaping (Data) -> Void, + streamResultFunction: @escaping (StreamResult) -> Void ) { self.requestFunction = requestFunction self.requestDataFunction = requestDataFunction diff --git a/README.md b/README.md index dc563d5b..464c796b 100644 --- a/README.md +++ b/README.md @@ -175,8 +175,8 @@ Offered under the [Apache 2 license](./LICENSE). [connect-web]: https://www.npmjs.com/package/@bufbuild/connect-web [error-handling]: https://connect.build/docs/swift/errors [getting-started]: https://connect.build/docs/swift/getting-started -[grpc-protocol]: https://grpc.io -[grpc-web-protocol]: https://github.com/grpc/grpc-web +[grpc-protocol]: https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md +[grpc-web-protocol]: https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-WEB.md [interceptors]: https://connect.build/docs/swift/interceptors [protobuf]: https://developers.google.com/protocol-buffers [slack]: https://buf.build/links/slack From 6cabaaebd67d5eb1c05facbe2a108e6c22dddd2c Mon Sep 17 00:00:00 2001 From: Michael Rebello Date: Mon, 27 Mar 2023 18:38:14 -0700 Subject: [PATCH 2/2] revert --- .../Interceptors/InterceptorChain.swift | 16 ++++++---- .../Connect/Interfaces/Interceptor.swift | 32 ++++++++----------- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/Libraries/Connect/Implementation/Interceptors/InterceptorChain.swift b/Libraries/Connect/Implementation/Interceptors/InterceptorChain.swift index 234c90bb..e92866ad 100644 --- a/Libraries/Connect/Implementation/Interceptors/InterceptorChain.swift +++ b/Libraries/Connect/Implementation/Interceptors/InterceptorChain.swift @@ -36,14 +36,8 @@ struct InterceptorChain { /// `caller -> a -> b -> c -> server` /// `caller <- c <- b <- a <- server` /// - /// - parameter send: <#UnaryFunction#> - /// /// - returns: A set of closures that each invoke the chain of interceptors in the above order. - func unaryFunction(send: UnaryFunction) -> UnaryFunction { - var next = send - for interceptor in self.interceptors { - next = UnaryFunction(requestFunction: <#T##(HTTPRequest) -> Void#>, responseFunction: <#T##(HTTPResponse) -> Void#>, responseMetricsFunction: <#T##(HTTPMetrics) -> Void#>) - } + func unaryFunction() -> UnaryFunction { let interceptors = self.interceptors.map { $0.unaryFunction() } return UnaryFunction( requestFunction: { request in @@ -90,3 +84,11 @@ struct InterceptorChain { ) } } + +private func executeInterceptors(_ interceptors: [(T) -> T], initial: T) -> T { + var next = initial + for interceptor in interceptors { + next = interceptor(next) + } + return next +} diff --git a/Libraries/Connect/Interfaces/Interceptor.swift b/Libraries/Connect/Interfaces/Interceptor.swift index 0539d720..5db787d9 100644 --- a/Libraries/Connect/Interfaces/Interceptor.swift +++ b/Libraries/Connect/Interfaces/Interceptor.swift @@ -22,10 +22,8 @@ public protocol Interceptor { /// Invoked when a unary call is started. Provides a set of closures that will be called /// as the request progresses, allowing the interceptor to alter request/response data. /// - /// - parameter next: <#UnaryFunction#> - /// /// - returns: A new set of closures which can be used to read/alter request/response data. - func unaryFunction(next: UnaryFunction) -> UnaryFunction + func unaryFunction() -> UnaryFunction /// Invoked when a streaming call is started. Provides a set of closures that will be called /// as the stream progresses, allowing the interceptor to alter request/response data. @@ -35,21 +33,19 @@ public protocol Interceptor { /// will contain 1 full message (for Connect and gRPC, this includes the prefix and message /// length bytes, followed by the actual message data). /// - /// - parameter next: <#StreamResult#> - /// /// - returns: A new set of closures which can be used to read/alter request/response data. - func streamFunction(next: StreamFunction) -> StreamFunction + func streamFunction() -> StreamFunction } public struct UnaryFunction { - public let requestFunction: (HTTPRequest) -> Void - public let responseFunction: (HTTPResponse) -> Void - public let responseMetricsFunction: (HTTPMetrics) -> Void + public let requestFunction: (HTTPRequest) -> HTTPRequest + public let responseFunction: (HTTPResponse) -> HTTPResponse + public let responseMetricsFunction: (HTTPMetrics) -> HTTPMetrics public init( - requestFunction: @escaping (HTTPRequest) -> Void, - responseFunction: @escaping (HTTPResponse) -> Void, - responseMetricsFunction: @escaping (HTTPMetrics) -> Void + requestFunction: @escaping (HTTPRequest) -> HTTPRequest, + responseFunction: @escaping (HTTPResponse) -> HTTPResponse, + responseMetricsFunction: @escaping (HTTPMetrics) -> HTTPMetrics = { $0 } ) { self.requestFunction = requestFunction self.responseFunction = responseFunction @@ -58,14 +54,14 @@ public struct UnaryFunction { } public struct StreamFunction { - public let requestFunction: (HTTPRequest) -> Void - public let requestDataFunction: (Data) -> Void - public let streamResultFunction: (StreamResult) -> Void + public let requestFunction: (HTTPRequest) -> HTTPRequest + public let requestDataFunction: (Data) -> Data + public let streamResultFunction: (StreamResult) -> StreamResult public init( - requestFunction: @escaping (HTTPRequest) -> Void, - requestDataFunction: @escaping (Data) -> Void, - streamResultFunction: @escaping (StreamResult) -> Void + requestFunction: @escaping (HTTPRequest) -> HTTPRequest, + requestDataFunction: @escaping (Data) -> Data, + streamResultFunction: @escaping (StreamResult) -> StreamResult ) { self.requestFunction = requestFunction self.requestDataFunction = requestDataFunction