Skip to content

Commit

Permalink
add underscores
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Rebello <[email protected]>
  • Loading branch information
rebello95 committed Oct 17, 2024
1 parent d13d949 commit 1b02c8d
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ extension ConnectInterceptor: StreamInterceptor {

@Sendable
func handleStreamRawInput(_ input: Data, proceed: @escaping (Data) -> Void) {
proceed(Envelope.packMessage(input, using: self.config.requestCompression))
proceed(Envelope._packMessage(input, using: self.config.requestCompression))
}

@Sendable
Expand Down Expand Up @@ -190,7 +190,7 @@ extension ConnectInterceptor: StreamInterceptor {
let responseCompressionPool = self.streamResponseHeaders.value?[
HeaderConstants.connectStreamingContentEncoding
]?.first.flatMap { self.config.responseCompressionPool(forName: $0) }
if responseCompressionPool == nil && Envelope.isCompressed(data) {
if responseCompressionPool == nil && Envelope._isCompressed(data) {
proceed(.complete(
code: .internalError, error: ConnectError(
code: .internalError, message: "received unexpected compressed message"
Expand All @@ -199,7 +199,7 @@ extension ConnectInterceptor: StreamInterceptor {
return
}

let (headerByte, message) = try Envelope.unpackMessage(
let (headerByte, message) = try Envelope._unpackMessage(
data, compressionPool: responseCompressionPool
)
let isEndStream = 0b00000010 & headerByte != 0
Expand Down
30 changes: 15 additions & 15 deletions Libraries/Connect/Internal/Interceptors/GRPCWebInterceptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ extension GRPCWebInterceptor: UnaryInterceptor {
proceed: @escaping (Result<HTTPRequest<Data?>, ConnectError>) -> Void
) {
// gRPC-Web unary payloads are enveloped.
let envelopedRequestBody = Envelope.packMessage(
let envelopedRequestBody = Envelope._packMessage(
request.message ?? Data(), using: self.config.requestCompression
)
proceed(.success(HTTPRequest(
url: request.url,
headers: request.headers.addingGRPCHeaders(using: self.config, grpcWeb: true),
headers: request.headers._addingGRPCHeaders(using: self.config, grpcWeb: true),
message: envelopedRequestBody,
method: request.method,
trailers: nil,
Expand All @@ -57,7 +57,7 @@ extension GRPCWebInterceptor: UnaryInterceptor {
}

guard let responseData = response.message, !responseData.isEmpty else {
let (grpcCode, connectError) = ConnectError.parseGRPCHeaders(
let (grpcCode, connectError) = ConnectError._parseGRPCHeaders(
response.headers,
trailers: response.trailers
)
Expand Down Expand Up @@ -102,7 +102,7 @@ extension GRPCWebInterceptor: UnaryInterceptor {
let compressionPool = response.headers[HeaderConstants.grpcContentEncoding]?
.first
.flatMap { self.config.responseCompressionPool(forName: $0) }
if compressionPool == nil && Envelope.isCompressed(responseData) {
if compressionPool == nil && Envelope._isCompressed(responseData) {
proceed(HTTPResponse(
code: .internalError, headers: response.headers, message: nil,
trailers: response.trailers,
Expand All @@ -120,9 +120,9 @@ extension GRPCWebInterceptor: UnaryInterceptor {
// message data.
// 2. The (headers and length prefixed) trailers data.
// https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-WEB.md
let firstChunkLength = Envelope.messageLength(forPackedData: responseData)
let prefixedFirstChunkLength = Envelope.prefixLength + firstChunkLength
let firstChunk = try Envelope.unpackMessage(
let firstChunkLength = Envelope._messageLength(forPackedData: responseData)
let prefixedFirstChunkLength = Envelope._prefixLength + firstChunkLength
let firstChunk = try Envelope._unpackMessage(
Data(responseData.prefix(upTo: prefixedFirstChunkLength)),
compressionPool: compressionPool
)
Expand All @@ -135,7 +135,7 @@ extension GRPCWebInterceptor: UnaryInterceptor {
} else {
let trailersData = Data(responseData.suffix(from: prefixedFirstChunkLength))
let unpackedTrailers = try Trailers.fromGRPCHeadersBlock(
try Envelope.unpackMessage(
try Envelope._unpackMessage(
trailersData, compressionPool: compressionPool
).unpacked
)
Expand Down Expand Up @@ -165,7 +165,7 @@ extension GRPCWebInterceptor: StreamInterceptor {
) {
proceed(.success(HTTPRequest(
url: request.url,
headers: request.headers.addingGRPCHeaders(using: self.config, grpcWeb: true),
headers: request.headers._addingGRPCHeaders(using: self.config, grpcWeb: true),
message: request.message,
method: request.method,
trailers: nil,
Expand All @@ -175,7 +175,7 @@ extension GRPCWebInterceptor: StreamInterceptor {

@Sendable
func handleStreamRawInput(_ input: Data, proceed: @escaping (Data) -> Void) {
proceed(Envelope.packMessage(input, using: self.config.requestCompression))
proceed(Envelope._packMessage(input, using: self.config.requestCompression))
}

@Sendable
Expand All @@ -198,11 +198,11 @@ extension GRPCWebInterceptor: StreamInterceptor {
return
}

if let grpcCode = headers.grpcStatus() {
if let grpcCode = headers._grpcStatus() {
// Headers-only response.
proceed(.complete(
code: grpcCode,
error: ConnectError.parseGRPCHeaders(nil, trailers: headers).error,
error: ConnectError._parseGRPCHeaders(nil, trailers: headers).error,
trailers: headers
))
} else {
Expand All @@ -215,13 +215,13 @@ extension GRPCWebInterceptor: StreamInterceptor {
let responseCompressionPool = self.streamResponseHeaders.value?[
HeaderConstants.grpcContentEncoding
]?.first.flatMap { self.config.responseCompressionPool(forName: $0) }
let (headerByte, unpackedData) = try Envelope.unpackMessage(
let (headerByte, unpackedData) = try Envelope._unpackMessage(
data, compressionPool: responseCompressionPool
)
let isTrailers = 0b10000000 & headerByte != 0
if isTrailers {
let trailers = try Trailers.fromGRPCHeadersBlock(unpackedData)
let (grpcCode, error) = ConnectError.parseGRPCHeaders(
let (grpcCode, error) = ConnectError._parseGRPCHeaders(
self.streamResponseHeaders.value, trailers: trailers
)
if grpcCode == .ok {
Expand Down Expand Up @@ -290,7 +290,7 @@ private extension Trailers {

private extension HTTPResponse {
func withHandledGRPCWebTrailers(_ trailers: Trailers, message: Data?) -> Self {
let (grpcCode, error) = ConnectError.parseGRPCHeaders(self.headers, trailers: trailers)
let (grpcCode, error) = ConnectError._parseGRPCHeaders(self.headers, trailers: trailers)
if grpcCode != .ok || error != nil {
return HTTPResponse(
// Rewrite the gRPC code if it is "ok" but `connectError` is non-nil.
Expand Down
4 changes: 2 additions & 2 deletions Libraries/Connect/PackageInternal/ConnectError+GRPC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ extension ConnectError {
deprecated: 100.0,
message: "This is an internal-only API which will be made package-private in Swift 6."
)
public static func parseGRPCHeaders(
public static func _parseGRPCHeaders(
_ headers: Headers?, trailers: Trailers?
) -> (grpcCode: Code, error: ConnectError?) {
// "Trailers-only" responses can be sent in the headers or trailers block.
guard let grpcCode = trailers?.grpcStatus() ?? headers?.grpcStatus() else {
guard let grpcCode = trailers?._grpcStatus() ?? headers?._grpcStatus() else {
return (.unknown, ConnectError(code: .unknown, message: "RPC response missing status"))
}

Expand Down
14 changes: 7 additions & 7 deletions Libraries/Connect/PackageInternal/Envelope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public enum Envelope {
deprecated: 100.0,
message: "This is an internal-only API which will be made package-private in Swift 6."
)
public static var prefixLength: Int {
public static var _prefixLength: Int {
return 5 // Header flags (1 byte) + message length (4 bytes)
}

Expand All @@ -50,7 +50,7 @@ public enum Envelope {
deprecated: 100.0,
message: "This is an internal-only API which will be made package-private in Swift 6."
)
public static func packMessage(
public static func _packMessage(
_ source: Data, using compression: ProtocolClientConfig.RequestCompression?
) -> Data {
var buffer = Data()
Expand Down Expand Up @@ -90,7 +90,7 @@ public enum Envelope {
deprecated: 100.0,
message: "This is an internal-only API which will be made package-private in Swift 6."
)
public static func unpackMessage(
public static func _unpackMessage(
_ source: Data, compressionPool: CompressionPool?
) throws -> (headerByte: UInt8, unpacked: Data) {
if source.isEmpty {
Expand All @@ -99,7 +99,7 @@ public enum Envelope {

let headerByte = source[0]
let isCompressed = 0b00000001 & headerByte != 0
let messageData = Data(source.dropFirst(self.prefixLength))
let messageData = Data(source.dropFirst(self._prefixLength))
if isCompressed {
guard let compressionPool = compressionPool else {
throw Error.missingExpectedCompressionPool
Expand All @@ -121,7 +121,7 @@ public enum Envelope {
deprecated: 100.0,
message: "This is an internal-only API which will be made package-private in Swift 6."
)
public static func isCompressed(_ packedData: Data) -> Bool {
public static func _isCompressed(_ packedData: Data) -> Bool {
return !packedData.isEmpty && (0b00000001 & packedData[0] != 0)
}

Expand All @@ -141,8 +141,8 @@ public enum Envelope {
deprecated: 100.0,
message: "This is an internal-only API which will be made package-private in Swift 6."
)
public static func messageLength(forPackedData data: Data) -> Int {
guard data.count >= self.prefixLength else {
public static func _messageLength(forPackedData data: Data) -> Int {
guard data.count >= self._prefixLength else {
return -1
}

Expand Down
2 changes: 1 addition & 1 deletion Libraries/Connect/PackageInternal/Headers+GRPC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extension Headers {
deprecated: 100.0,
message: "This is an internal-only API which will be made package-private in Swift 6."
)
public func addingGRPCHeaders(using config: ProtocolClientConfig, grpcWeb: Bool) -> Self {
public func _addingGRPCHeaders(using config: ProtocolClientConfig, grpcWeb: Bool) -> Self {
var headers = self
headers[HeaderConstants.grpcAcceptEncoding] = config
.acceptCompressionPoolNames()
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Connect/PackageInternal/Trailers+gRPC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extension Trailers {
deprecated: 100.0,
message: "This is an internal-only API which will be made package-private in Swift 6."
)
public func grpcStatus() -> Code? {
public func _grpcStatus() -> Code? {
return self[HeaderConstants.grpcStatus]?
.first
.flatMap(Int.init)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,12 @@ extension ProtocolClient: ProtocolClientInterface {
// Handle cases where multiple messages are received in a single chunk.
responseBuffer += data
while true {
let messageLength = Envelope.messageLength(forPackedData: responseBuffer)
let messageLength = Envelope._messageLength(forPackedData: responseBuffer)
if messageLength < 0 {
return
}

let prefixedMessageLength = Envelope.prefixLength + messageLength
let prefixedMessageLength = Envelope._prefixLength + messageLength
guard responseBuffer.count >= prefixedMessageLength else {
return
}
Expand Down
26 changes: 13 additions & 13 deletions Libraries/ConnectNIO/Internal/GRPCInterceptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ extension GRPCInterceptor: UnaryInterceptor {
proceed: @escaping (Result<HTTPRequest<Data?>, ConnectError>) -> Void
) {
// gRPC unary payloads are enveloped.
let envelopedRequestBody = Envelope.packMessage(
let envelopedRequestBody = Envelope._packMessage(
request.message ?? Data(), using: self.config.requestCompression
)

proceed(.success(HTTPRequest(
url: request.url,
headers: request.headers.addingGRPCHeaders(using: self.config, grpcWeb: false),
headers: request.headers._addingGRPCHeaders(using: self.config, grpcWeb: false),
message: envelopedRequestBody,
method: request.method,
trailers: nil,
Expand Down Expand Up @@ -72,12 +72,12 @@ extension GRPCInterceptor: UnaryInterceptor {
return
}

let (grpcCode, connectError) = ConnectError.parseGRPCHeaders(
let (grpcCode, connectError) = ConnectError._parseGRPCHeaders(
response.headers,
trailers: response.trailers
)
guard grpcCode == .ok, let rawData = response.message, !rawData.isEmpty else {
if response.trailers.grpcStatus() == nil && response.message?.isEmpty == false {
if response.trailers._grpcStatus() == nil && response.message?.isEmpty == false {
proceed(HTTPResponse(
code: .internalError,
headers: response.headers,
Expand Down Expand Up @@ -117,7 +117,7 @@ extension GRPCInterceptor: UnaryInterceptor {
.headers[HeaderConstants.grpcContentEncoding]?
.first
.flatMap { self.config.responseCompressionPool(forName: $0) }
if compressionPool == nil && Envelope.isCompressed(rawData) {
if compressionPool == nil && Envelope._isCompressed(rawData) {
proceed(HTTPResponse(
code: .internalError, headers: response.headers, message: nil,
trailers: response.trailers, error: ConnectError(
Expand All @@ -140,7 +140,7 @@ extension GRPCInterceptor: UnaryInterceptor {
}

do {
let messageData = try Envelope.unpackMessage(
let messageData = try Envelope._unpackMessage(
rawData, compressionPool: compressionPool
).unpacked
proceed(HTTPResponse(
Expand Down Expand Up @@ -172,7 +172,7 @@ extension GRPCInterceptor: StreamInterceptor {
) {
proceed(.success(HTTPRequest(
url: request.url,
headers: request.headers.addingGRPCHeaders(using: self.config, grpcWeb: false),
headers: request.headers._addingGRPCHeaders(using: self.config, grpcWeb: false),
message: request.message,
method: request.method,
trailers: nil,
Expand All @@ -182,7 +182,7 @@ extension GRPCInterceptor: StreamInterceptor {

@Sendable
func handleStreamRawInput(_ input: Data, proceed: @escaping (Data) -> Void) {
proceed(Envelope.packMessage(input, using: self.config.requestCompression))
proceed(Envelope._packMessage(input, using: self.config.requestCompression))
}

@Sendable
Expand Down Expand Up @@ -214,7 +214,7 @@ extension GRPCInterceptor: StreamInterceptor {
let responseCompressionPool = self.streamResponseHeaders.value?[
HeaderConstants.grpcContentEncoding
]?.first.flatMap { self.config.responseCompressionPool(forName: $0) }
if responseCompressionPool == nil && Envelope.isCompressed(rawData) {
if responseCompressionPool == nil && Envelope._isCompressed(rawData) {
proceed(.complete(
code: .internalError, error: ConnectError(
code: .internalError, message: "received unexpected compressed message"
Expand All @@ -223,7 +223,7 @@ extension GRPCInterceptor: StreamInterceptor {
return
}

let unpackedMessage = try Envelope.unpackMessage(
let unpackedMessage = try Envelope._unpackMessage(
rawData, compressionPool: responseCompressionPool
).unpacked
proceed(.message(unpackedMessage))
Expand All @@ -239,7 +239,7 @@ extension GRPCInterceptor: StreamInterceptor {
return
}

let (grpcCode, connectError) = ConnectError.parseGRPCHeaders(
let (grpcCode, connectError) = ConnectError._parseGRPCHeaders(
self.streamResponseHeaders.value,
trailers: trailers
)
Expand Down Expand Up @@ -275,8 +275,8 @@ extension GRPCInterceptor: StreamInterceptor {

private extension Envelope {
static func containsMultipleGRPCMessages(_ packedData: Data) -> Bool {
let messageLength = self.messageLength(forPackedData: packedData)
return packedData.count > messageLength + self.prefixLength
let messageLength = self._messageLength(forPackedData: packedData)
return packedData.count > messageLength + self._prefixLength
}
}

Expand Down
Loading

0 comments on commit 1b02c8d

Please sign in to comment.