|
| 1 | +/* |
| 2 | + * Copyright 2024, gRPC Authors All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +private import DequeModule |
| 18 | +public import GRPCCore |
| 19 | +public import SwiftProtobuf |
| 20 | + |
| 21 | +#if canImport(FoundationEssentials) |
| 22 | +public import struct FoundationEssentials.URL |
| 23 | +public import struct FoundationEssentials.Data |
| 24 | +#else |
| 25 | +public import struct Foundation.URL |
| 26 | +public import struct Foundation.Data |
| 27 | +#endif |
| 28 | + |
| 29 | +/// Implements the gRPC Reflection service (v1). |
| 30 | +/// |
| 31 | +/// The reflection service is a regular gRPC service providing information about other |
| 32 | +/// services. |
| 33 | +/// |
| 34 | +/// The service will offer information to clients about any registered services. You can register |
| 35 | +/// a service by providing its descriptor set to the service. |
| 36 | +public final class ReflectionService: Sendable { |
| 37 | + private let service: ReflectionService.V1 |
| 38 | + |
| 39 | + /// Create a new instance of the reflection service from a list of descriptor set file URLs. |
| 40 | + /// |
| 41 | + /// - Parameter fileURLs: A list of file URLs containing serialized descriptor sets. |
| 42 | + public convenience init( |
| 43 | + descriptorSetFileURLs fileURLs: [URL] |
| 44 | + ) throws { |
| 45 | + let fileDescriptorProtos = try Self.readDescriptorSets(atURLs: fileURLs) |
| 46 | + try self.init(fileDescriptors: fileDescriptorProtos) |
| 47 | + } |
| 48 | + |
| 49 | + /// Create a new instance of the reflection service from a list of descriptor set file paths. |
| 50 | + /// |
| 51 | + /// - Parameter filePaths: A list of file paths containing serialized descriptor sets. |
| 52 | + public convenience init( |
| 53 | + descriptorSetFilePaths filePaths: [String] |
| 54 | + ) throws { |
| 55 | + let fileDescriptorProtos = try Self.readDescriptorSets(atPaths: filePaths) |
| 56 | + try self.init(fileDescriptors: fileDescriptorProtos) |
| 57 | + } |
| 58 | + |
| 59 | + /// Create a new instance of the reflection service from a list of file descriptor messages. |
| 60 | + /// |
| 61 | + /// - Parameter fileDescriptors: A list of file descriptors of the services to register. |
| 62 | + public init( |
| 63 | + fileDescriptors: [Google_Protobuf_FileDescriptorProto] |
| 64 | + ) throws { |
| 65 | + let registry = try ReflectionServiceRegistry(fileDescriptors: fileDescriptors) |
| 66 | + self.service = ReflectionService.V1(registry: registry) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +extension ReflectionService: RegistrableRPCService { |
| 71 | + public func registerMethods(with router: inout RPCRouter) { |
| 72 | + self.service.registerMethods(with: &router) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +extension ReflectionService { |
| 77 | + static func readSerializedFileDescriptorProto( |
| 78 | + atPath path: String |
| 79 | + ) throws -> Google_Protobuf_FileDescriptorProto { |
| 80 | + let fileURL: URL |
| 81 | + #if canImport(Darwin) |
| 82 | + fileURL = URL(filePath: path, directoryHint: .notDirectory) |
| 83 | + #else |
| 84 | + fileURL = URL(fileURLWithPath: path) |
| 85 | + #endif |
| 86 | + |
| 87 | + let binaryData = try Data(contentsOf: fileURL) |
| 88 | + guard let serializedData = Data(base64Encoded: binaryData) else { |
| 89 | + throw RPCError( |
| 90 | + code: .invalidArgument, |
| 91 | + message: |
| 92 | + """ |
| 93 | + The \(path) file contents could not be transformed \ |
| 94 | + into serialized data representing a file descriptor proto. |
| 95 | + """ |
| 96 | + ) |
| 97 | + } |
| 98 | + |
| 99 | + return try Google_Protobuf_FileDescriptorProto(serializedBytes: serializedData) |
| 100 | + } |
| 101 | + |
| 102 | + static func readSerializedFileDescriptorProtos( |
| 103 | + atPaths paths: [String] |
| 104 | + ) throws -> [Google_Protobuf_FileDescriptorProto] { |
| 105 | + var fileDescriptorProtos = [Google_Protobuf_FileDescriptorProto]() |
| 106 | + fileDescriptorProtos.reserveCapacity(paths.count) |
| 107 | + for path in paths { |
| 108 | + try fileDescriptorProtos.append(Self.readSerializedFileDescriptorProto(atPath: path)) |
| 109 | + } |
| 110 | + return fileDescriptorProtos |
| 111 | + } |
| 112 | + |
| 113 | + static func readDescriptorSet( |
| 114 | + atURL fileURL: URL |
| 115 | + ) throws -> [Google_Protobuf_FileDescriptorProto] { |
| 116 | + let binaryData = try Data(contentsOf: fileURL) |
| 117 | + let descriptorSet = try Google_Protobuf_FileDescriptorSet(serializedBytes: binaryData) |
| 118 | + return descriptorSet.file |
| 119 | + } |
| 120 | + |
| 121 | + static func readDescriptorSet( |
| 122 | + atPath path: String |
| 123 | + ) throws -> [Google_Protobuf_FileDescriptorProto] { |
| 124 | + let fileURL: URL |
| 125 | + #if canImport(Darwin) |
| 126 | + fileURL = URL(filePath: path, directoryHint: .notDirectory) |
| 127 | + #else |
| 128 | + fileURL = URL(fileURLWithPath: path) |
| 129 | + #endif |
| 130 | + return try Self.readDescriptorSet(atURL: fileURL) |
| 131 | + } |
| 132 | + |
| 133 | + static func readDescriptorSets( |
| 134 | + atURLs fileURLs: [URL] |
| 135 | + ) throws -> [Google_Protobuf_FileDescriptorProto] { |
| 136 | + var fileDescriptorProtos = [Google_Protobuf_FileDescriptorProto]() |
| 137 | + fileDescriptorProtos.reserveCapacity(fileURLs.count) |
| 138 | + for url in fileURLs { |
| 139 | + try fileDescriptorProtos.append(contentsOf: Self.readDescriptorSet(atURL: url)) |
| 140 | + } |
| 141 | + return fileDescriptorProtos |
| 142 | + } |
| 143 | + |
| 144 | + static func readDescriptorSets( |
| 145 | + atPaths paths: [String] |
| 146 | + ) throws -> [Google_Protobuf_FileDescriptorProto] { |
| 147 | + var fileDescriptorProtos = [Google_Protobuf_FileDescriptorProto]() |
| 148 | + fileDescriptorProtos.reserveCapacity(paths.count) |
| 149 | + for path in paths { |
| 150 | + try fileDescriptorProtos.append(contentsOf: Self.readDescriptorSet(atPath: path)) |
| 151 | + } |
| 152 | + return fileDescriptorProtos |
| 153 | + } |
| 154 | +} |
0 commit comments