diff --git a/README.md b/README.md index f0f215ce3..de4cc752b 100644 --- a/README.md +++ b/README.md @@ -46,3 +46,11 @@ Directory: [`packages/grpc-health-check`](https://github.com/grpc/grpc-node/tree npm package: [grpc-health-check](https://www.npmjs.com/package/grpc-health-check) Health check service for gRPC servers. + +### gRPC Reflection API Service + +Directory: [`packages/grpc-reflection`](https://github.com/grpc/grpc-node/tree/master/packages/grpc-reflection) + +npm package: [@grpc/reflection](https://www.npmjs.com/package/@grpc/reflection) + +Reflection API service for gRPC servers. diff --git a/examples/package.json b/examples/package.json index aee948c3f..6857aa5d9 100644 --- a/examples/package.json +++ b/examples/package.json @@ -7,6 +7,7 @@ "google-protobuf": "^3.0.0", "@grpc/grpc-js": "^1.8.0", "@grpc/grpc-js-xds": "^1.8.0", + "@grpc/reflection": "^1.0.0", "lodash": "^4.6.1", "minimist": "^1.2.0" } diff --git a/examples/reflection/server.js b/examples/reflection/server.js new file mode 100644 index 000000000..83232e8e5 --- /dev/null +++ b/examples/reflection/server.js @@ -0,0 +1,20 @@ +var path = require('path'); +var grpc = require('@grpc/grpc-js'); +var protoLoader = require('@grpc/proto-loader'); +var reflection = require('@grpc/reflection'); + +var PROTO_PATH = path.join(__dirname, '../protos/helloworld.proto'); + +var server = new grpc.Server(); +var packageDefinition = protoLoader.loadSync(PROTO_PATH); +var proto = grpc.loadPackageDefinition(packageDefinition); +var reflection = new reflection.ReflectionService(packageDefinition); + +reflection.addToServer(server); +server.addService(proto.helloworld.Greeter.service, { + sayHello: (call, callback) => { callback(null, { message: 'Hello' }) } +}); + +server.bindAsync('localhost:5000', grpc.ServerCredentials.createInsecure(), () => { + server.start(); +}); diff --git a/gulpfile.ts b/gulpfile.ts index 7ac4e9a0b..2c2c2e374 100644 --- a/gulpfile.ts +++ b/gulpfile.ts @@ -19,10 +19,11 @@ import * as gulp from 'gulp'; import * as healthCheck from './packages/grpc-health-check/gulpfile'; import * as jsCore from './packages/grpc-js/gulpfile'; import * as jsXds from './packages/grpc-js-xds/gulpfile'; +import * as reflection from './packages/grpc-reflection/gulpfile'; import * as protobuf from './packages/proto-loader/gulpfile'; import * as internalTest from './test/gulpfile'; -const installAll = gulp.series(jsCore.install, healthCheck.install, protobuf.install, internalTest.install, jsXds.install); +const installAll = gulp.series(jsCore.install, healthCheck.install, protobuf.install, internalTest.install, jsXds.install, reflection.install); const lint = gulp.parallel(jsCore.lint); @@ -40,7 +41,7 @@ const nativeTestOnly = gulp.parallel(healthCheck.test); const nativeTest = gulp.series(build, nativeTestOnly); -const testOnly = gulp.parallel(jsCore.test, nativeTestOnly, protobuf.test, jsXds.test); +const testOnly = gulp.parallel(jsCore.test, nativeTestOnly, protobuf.test, jsXds.test, reflection.test); const test = gulp.series(build, testOnly, internalTest.test); diff --git a/package.json b/package.json index 70a15fbbf..a1fd3d59e 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,8 @@ "include": [ "packages/grpc-health-check/health.js", "packages/grpc-js/build/src/*", - "packages/proto-loader/build/src/*" + "packages/proto-loader/build/src/*", + "packages/grpc-reflection/build/src/*" ], "cache": true, "all": true diff --git a/packages/grpc-reflection/LICENSE b/packages/grpc-reflection/LICENSE new file mode 100644 index 000000000..8dada3eda --- /dev/null +++ b/packages/grpc-reflection/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright {yyyy} {name of copyright owner} + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/packages/grpc-reflection/README.md b/packages/grpc-reflection/README.md new file mode 100644 index 000000000..47d790f5a --- /dev/null +++ b/packages/grpc-reflection/README.md @@ -0,0 +1,42 @@ +# gRPC Reflection + +gRPC reflection API service for use with gRPC-node. + +## Background + +This package provides an implementation of the [gRPC Server Reflection Protocol](https://github.com/grpc/grpc/blob/master/doc/server-reflection.md) service which can be added to an existing gRPC server. Adding this service to your server will allow clients [such as postman](https://blog.postman.com/postman-now-supports-grpc/) to dynamically load the API specification from your running application rather than needing to pass around and load proto files manually. + +![example of reflection working with postman](./images/example.gif) + +## Installation + +Use the package manager [npm](https://www.npmjs.com/get-npm) to install `@grpc/reflection`. + +```bash +npm install @grpc/reflection +``` + +## Usage + +Any gRPC-node server can use `@grpc/reflection` to expose reflection information about their gRPC API. + +```typescript +import { ReflectionServer } from '@grpc/reflection'; + +const pkg = protoLoader.load(...); // Load your gRPC package definition as normal + +// Create the reflection implementation based on your gRPC package and add it to your existing server +const reflection = new ReflectionServer(pkg); +reflection.addToServer(server); +``` + +Congrats! Your server now allows any client to request reflection information about its API. + +## Contributing + +Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. The original proposal for this library can be found in [gRFC L108](https://github.com/grpc/proposal/blob/master/L108-node-grpc-reflection-library.md) + +Please make sure to update tests as appropriate. + +## License +[Apache License 2.0](https://choosealicense.com/licenses/apache-2.0/) diff --git a/packages/grpc-reflection/gulpfile.ts b/packages/grpc-reflection/gulpfile.ts new file mode 100644 index 000000000..f95b91bc0 --- /dev/null +++ b/packages/grpc-reflection/gulpfile.ts @@ -0,0 +1,50 @@ +/* + * Copyright 2019 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +import * as gulp from 'gulp'; +import * as mocha from 'gulp-mocha'; +import * as execa from 'execa'; +import * as path from 'path'; + +const reflectionDir = __dirname; +const outDir = path.resolve(reflectionDir, 'build'); + +const execNpmVerb = (verb: string, ...args: string[]) => + execa('npm', [verb, ...args], {cwd: reflectionDir, stdio: 'inherit'}); +const execNpmCommand = execNpmVerb.bind(null, 'run'); + +const install = () => execNpmVerb('install', '--unsafe-perm'); + +/** + * Transpiles TypeScript files in src/ to JavaScript according to the settings + * found in tsconfig.json. + */ +const compile = () => execNpmCommand('compile'); + +const runTests = () => { + return gulp.src(`${outDir}/test/**/*.js`) + .pipe(mocha({reporter: 'mocha-jenkins-reporter', + require: ['ts-node/register']})); +}; + +const test = gulp.series(install, runTests); + +export { + install, + compile, + test +} diff --git a/packages/grpc-reflection/images/example.gif b/packages/grpc-reflection/images/example.gif new file mode 100644 index 000000000..65c92d78f Binary files /dev/null and b/packages/grpc-reflection/images/example.gif differ diff --git a/packages/grpc-reflection/package.json b/packages/grpc-reflection/package.json new file mode 100644 index 000000000..755ae5083 --- /dev/null +++ b/packages/grpc-reflection/package.json @@ -0,0 +1,45 @@ +{ + "name": "@grpc/reflection", + "version": "1.0.0", + "author": { + "name": "Google Inc." + }, + "description": "Reflection API service for use with gRPC-node", + "repository": { + "type": "git", + "url": "https://github.com/grpc/grpc-node.git", + "directory": "packages/grpc-reflection" + }, + "bugs": "https://github.com/grpc/grpc-node/issues", + "contributors": [ + { + "name": "Justin Timmons", + "email": "justinmtimmons@gmail.com" + } + ], + "main": "build/src/index.js", + "types": "build/src/index.d.ts", + "files": [ + "build" + ], + "license": "Apache-2.0", + "scripts": { + "compile": "tsc -p .", + "postcompile": "copyfiles './proto/**/*.proto' build/", + "prepare": "npm run generate-types && npm run compile", + "test": "mocha --require ts-node/register test/**.ts", + "generate-types": "proto-loader-gen-types --longs String --enums String --bytes Array --defaults --oneofs --includeComments --includeDirs proto/ -O src/generated grpc/reflection/v1/reflection.proto grpc/reflection/v1alpha/reflection.proto" + }, + "dependencies": { + "@grpc/proto-loader": "^0.7.10", + "protobufjs": "^7.2.5" + }, + "peerDependencies": { + "@grpc/grpc-js": "^1.8.21" + }, + "devDependencies": { + "@grpc/grpc-js": "file:../grpc-js", + "copyfiles": "^2.4.1", + "typescript": "^5.2.2" + } +} diff --git a/packages/grpc-reflection/proto/grpc/reflection/v1/reflection.proto b/packages/grpc-reflection/proto/grpc/reflection/v1/reflection.proto new file mode 100644 index 000000000..1c106af7f --- /dev/null +++ b/packages/grpc-reflection/proto/grpc/reflection/v1/reflection.proto @@ -0,0 +1,149 @@ +// Taken from spec https://raw.githubusercontent.com/grpc/grpc/master/src/proto/grpc/reflection/v1/reflection.proto +// Additional versions can be found here: https://github.com/grpc/grpc/tree/master/src/proto/grpc/reflection + +// Copyright 2016 The gRPC Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Service exported by server reflection. A more complete description of how +// server reflection works can be found at +// https://github.com/grpc/grpc/blob/master/doc/server-reflection.md +// +// The canonical version of this proto can be found at +// https://github.com/grpc/grpc-proto/blob/master/grpc/reflection/v1/reflection.proto + +syntax = "proto3"; + +package grpc.reflection.v1; + +option go_package = "google.golang.org/grpc/reflection/grpc_reflection_v1"; +option java_multiple_files = true; +option java_package = "io.grpc.reflection.v1"; +option java_outer_classname = "ServerReflectionProto"; + +service ServerReflection { + // The reflection service is structured as a bidirectional stream, ensuring + // all related requests go to a single server. + rpc ServerReflectionInfo(stream ServerReflectionRequest) + returns (stream ServerReflectionResponse); +} + +// The message sent by the client when calling ServerReflectionInfo method. +message ServerReflectionRequest { + string host = 1; + // To use reflection service, the client should set one of the following + // fields in message_request. The server distinguishes requests by their + // defined field and then handles them using corresponding methods. + oneof message_request { + // Find a proto file by the file name. + string file_by_filename = 3; + + // Find the proto file that declares the given fully-qualified symbol name. + // This field should be a fully-qualified symbol name + // (e.g. .[.] or .). + string file_containing_symbol = 4; + + // Find the proto file which defines an extension extending the given + // message type with the given field number. + ExtensionRequest file_containing_extension = 5; + + // Finds the tag numbers used by all known extensions of the given message + // type, and appends them to ExtensionNumberResponse in an undefined order. + // Its corresponding method is best-effort: it's not guaranteed that the + // reflection service will implement this method, and it's not guaranteed + // that this method will provide all extensions. Returns + // StatusCode::UNIMPLEMENTED if it's not implemented. + // This field should be a fully-qualified type name. The format is + // . + string all_extension_numbers_of_type = 6; + + // List the full names of registered services. The content will not be + // checked. + string list_services = 7; + } +} + +// The type name and extension number sent by the client when requesting +// file_containing_extension. +message ExtensionRequest { + // Fully-qualified type name. The format should be . + string containing_type = 1; + int32 extension_number = 2; +} + +// The message sent by the server to answer ServerReflectionInfo method. +message ServerReflectionResponse { + string valid_host = 1; + ServerReflectionRequest original_request = 2; + // The server sets one of the following fields according to the message_request + // in the request. + oneof message_response { + // This message is used to answer file_by_filename, file_containing_symbol, + // file_containing_extension requests with transitive dependencies. + // As the repeated label is not allowed in oneof fields, we use a + // FileDescriptorResponse message to encapsulate the repeated fields. + // The reflection service is allowed to avoid sending FileDescriptorProtos + // that were previously sent in response to earlier requests in the stream. + FileDescriptorResponse file_descriptor_response = 4; + + // This message is used to answer all_extension_numbers_of_type requests. + ExtensionNumberResponse all_extension_numbers_response = 5; + + // This message is used to answer list_services requests. + ListServiceResponse list_services_response = 6; + + // This message is used when an error occurs. + ErrorResponse error_response = 7; + } +} + +// Serialized FileDescriptorProto messages sent by the server answering +// a file_by_filename, file_containing_symbol, or file_containing_extension +// request. +message FileDescriptorResponse { + // Serialized FileDescriptorProto messages. We avoid taking a dependency on + // descriptor.proto, which uses proto2 only features, by making them opaque + // bytes instead. + repeated bytes file_descriptor_proto = 1; +} + +// A list of extension numbers sent by the server answering +// all_extension_numbers_of_type request. +message ExtensionNumberResponse { + // Full name of the base type, including the package name. The format + // is . + string base_type_name = 1; + repeated int32 extension_number = 2; +} + +// A list of ServiceResponse sent by the server answering list_services request. +message ListServiceResponse { + // The information of each service may be expanded in the future, so we use + // ServiceResponse message to encapsulate it. + repeated ServiceResponse service = 1; +} + +// The information of a single service used by ListServiceResponse to answer +// list_services request. +message ServiceResponse { + // Full name of a registered service, including its package name. The format + // is . + string name = 1; +} + +// The error code and error message sent by the server when an error occurs. +message ErrorResponse { + // This field uses the error codes defined in grpc::StatusCode. + int32 error_code = 1; + string error_message = 2; +} diff --git a/packages/grpc-reflection/proto/grpc/reflection/v1alpha/reflection.proto b/packages/grpc-reflection/proto/grpc/reflection/v1alpha/reflection.proto new file mode 100644 index 000000000..781659af2 --- /dev/null +++ b/packages/grpc-reflection/proto/grpc/reflection/v1alpha/reflection.proto @@ -0,0 +1,139 @@ +// Taken from spec https://raw.githubusercontent.com/grpc/grpc/master/src/proto/grpc/reflection/v1alpha/reflection.proto +// Additional versions can be found here: https://github.com/grpc/grpc/tree/master/src/proto/grpc/reflection + +// Copyright 2016 gRPC authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Service exported by server reflection + +syntax = "proto3"; + +package grpc.reflection.v1alpha; + +service ServerReflection { + // The reflection service is structured as a bidirectional stream, ensuring + // all related requests go to a single server. + rpc ServerReflectionInfo(stream ServerReflectionRequest) + returns (stream ServerReflectionResponse); +} + +// The message sent by the client when calling ServerReflectionInfo method. +message ServerReflectionRequest { + string host = 1; + // To use reflection service, the client should set one of the following + // fields in message_request. The server distinguishes requests by their + // defined field and then handles them using corresponding methods. + oneof message_request { + // Find a proto file by the file name. + string file_by_filename = 3; + + // Find the proto file that declares the given fully-qualified symbol name. + // This field should be a fully-qualified symbol name + // (e.g. .[.] or .). + string file_containing_symbol = 4; + + // Find the proto file which defines an extension extending the given + // message type with the given field number. + ExtensionRequest file_containing_extension = 5; + + // Finds the tag numbers used by all known extensions of the given message + // type, and appends them to ExtensionNumberResponse in an undefined order. + // Its corresponding method is best-effort: it's not guaranteed that the + // reflection service will implement this method, and it's not guaranteed + // that this method will provide all extensions. Returns + // StatusCode::UNIMPLEMENTED if it's not implemented. + // This field should be a fully-qualified type name. The format is + // . + string all_extension_numbers_of_type = 6; + + // List the full names of registered services. The content will not be + // checked. + string list_services = 7; + } +} + +// The type name and extension number sent by the client when requesting +// file_containing_extension. +message ExtensionRequest { + // Fully-qualified type name. The format should be . + string containing_type = 1; + int32 extension_number = 2; +} + +// The message sent by the server to answer ServerReflectionInfo method. +message ServerReflectionResponse { + string valid_host = 1; + ServerReflectionRequest original_request = 2; + // The server set one of the following fields accroding to the message_request + // in the request. + oneof message_response { + // This message is used to answer file_by_filename, file_containing_symbol, + // file_containing_extension requests with transitive dependencies. As + // the repeated label is not allowed in oneof fields, we use a + // FileDescriptorResponse message to encapsulate the repeated fields. + // The reflection service is allowed to avoid sending FileDescriptorProtos + // that were previously sent in response to earlier requests in the stream. + FileDescriptorResponse file_descriptor_response = 4; + + // This message is used to answer all_extension_numbers_of_type requst. + ExtensionNumberResponse all_extension_numbers_response = 5; + + // This message is used to answer list_services request. + ListServiceResponse list_services_response = 6; + + // This message is used when an error occurs. + ErrorResponse error_response = 7; + } +} + +// Serialized FileDescriptorProto messages sent by the server answering +// a file_by_filename, file_containing_symbol, or file_containing_extension +// request. +message FileDescriptorResponse { + // Serialized FileDescriptorProto messages. We avoid taking a dependency on + // descriptor.proto, which uses proto2 only features, by making them opaque + // bytes instead. + repeated bytes file_descriptor_proto = 1; +} + +// A list of extension numbers sent by the server answering +// all_extension_numbers_of_type request. +message ExtensionNumberResponse { + // Full name of the base type, including the package name. The format + // is . + string base_type_name = 1; + repeated int32 extension_number = 2; +} + +// A list of ServiceResponse sent by the server answering list_services request. +message ListServiceResponse { + // The information of each service may be expanded in the future, so we use + // ServiceResponse message to encapsulate it. + repeated ServiceResponse service = 1; +} + +// The information of a single service used by ListServiceResponse to answer +// list_services request. +message ServiceResponse { + // Full name of a registered service, including its package name. The format + // is . + string name = 1; +} + +// The error code and error message sent by the server when an error occurs. +message ErrorResponse { + // This field uses the error codes defined in grpc::StatusCode. + int32 error_code = 1; + string error_message = 2; +} diff --git a/packages/grpc-reflection/proto/sample/sample.proto b/packages/grpc-reflection/proto/sample/sample.proto new file mode 100644 index 000000000..acf969c1a --- /dev/null +++ b/packages/grpc-reflection/proto/sample/sample.proto @@ -0,0 +1,43 @@ +syntax = "proto3"; + +package sample; + +import 'vendor.proto'; + +service SampleService { + rpc Hello (HelloRequest) returns (HelloResponse) {} + rpc Hello2 (HelloRequest) returns (CommonMessage) {} +} + +service IgnoreService { + rpc Hello (HelloRequest) returns (HelloResponse) {} +} + +message HelloRequest { + string hello = 1; + HelloNested nested = 2; + ShadowedMessage nestedShadowedMessage = 3; + + message HelloNested { + string hello = 1; + CommonMessage field = 2; + } + + message ShadowedMessage { + int32 item = 1; + } +} + +enum HelloStatus { + HELLO = 1; + WORLD = 2; +} + +message HelloResponse { + string world = 1; + HelloStatus status = 2; +} + +message ShadowedMessage { + string hello = 1; +} diff --git a/packages/grpc-reflection/proto/sample/vendor/common.proto b/packages/grpc-reflection/proto/sample/vendor/common.proto new file mode 100644 index 000000000..c89246b5d --- /dev/null +++ b/packages/grpc-reflection/proto/sample/vendor/common.proto @@ -0,0 +1,15 @@ +syntax = "proto2"; + +// NOTE: intentionally using the same 'vendor' package here to document the +// file/package merging behavior of the reflection service. +// +// this file should be combined with vendor.proto to a single definition because +// it's under the same 'vendor' package +package vendor; + +message CommonMessage { + optional string common = 1; + optional DependentMessage dependency = 2; + + extensions 100 to 199; +} diff --git a/packages/grpc-reflection/proto/sample/vendor/dependency/dependency.proto b/packages/grpc-reflection/proto/sample/vendor/dependency/dependency.proto new file mode 100644 index 000000000..44cadfba1 --- /dev/null +++ b/packages/grpc-reflection/proto/sample/vendor/dependency/dependency.proto @@ -0,0 +1,7 @@ +syntax = "proto2"; + +package vendor.dependency; + +message DependentMessage { + optional string something = 1; +} diff --git a/packages/grpc-reflection/proto/sample/vendor/vendor.proto b/packages/grpc-reflection/proto/sample/vendor/vendor.proto new file mode 100644 index 000000000..f8e0c1e5a --- /dev/null +++ b/packages/grpc-reflection/proto/sample/vendor/vendor.proto @@ -0,0 +1,10 @@ +syntax = "proto2"; + +package vendor; + +import "./common.proto"; +import "./dependency/dependency.proto"; + +extend CommonMessage { + optional bool ext = 101; +} diff --git a/packages/grpc-reflection/src/generated/grpc/reflection/v1/ErrorResponse.ts b/packages/grpc-reflection/src/generated/grpc/reflection/v1/ErrorResponse.ts new file mode 100644 index 000000000..e8168c36d --- /dev/null +++ b/packages/grpc-reflection/src/generated/grpc/reflection/v1/ErrorResponse.ts @@ -0,0 +1,24 @@ +// Original file: proto/grpc/reflection/v1/reflection.proto + + +/** + * The error code and error message sent by the server when an error occurs. + */ +export interface ErrorResponse { + /** + * This field uses the error codes defined in grpc::StatusCode. + */ + 'errorCode'?: (number); + 'errorMessage'?: (string); +} + +/** + * The error code and error message sent by the server when an error occurs. + */ +export interface ErrorResponse__Output { + /** + * This field uses the error codes defined in grpc::StatusCode. + */ + 'errorCode': (number); + 'errorMessage': (string); +} diff --git a/packages/grpc-reflection/src/generated/grpc/reflection/v1/ExtensionNumberResponse.ts b/packages/grpc-reflection/src/generated/grpc/reflection/v1/ExtensionNumberResponse.ts new file mode 100644 index 000000000..fdb88119c --- /dev/null +++ b/packages/grpc-reflection/src/generated/grpc/reflection/v1/ExtensionNumberResponse.ts @@ -0,0 +1,28 @@ +// Original file: proto/grpc/reflection/v1/reflection.proto + + +/** + * A list of extension numbers sent by the server answering + * all_extension_numbers_of_type request. + */ +export interface ExtensionNumberResponse { + /** + * Full name of the base type, including the package name. The format + * is . + */ + 'baseTypeName'?: (string); + 'extensionNumber'?: (number)[]; +} + +/** + * A list of extension numbers sent by the server answering + * all_extension_numbers_of_type request. + */ +export interface ExtensionNumberResponse__Output { + /** + * Full name of the base type, including the package name. The format + * is . + */ + 'baseTypeName': (string); + 'extensionNumber': (number)[]; +} diff --git a/packages/grpc-reflection/src/generated/grpc/reflection/v1/ExtensionRequest.ts b/packages/grpc-reflection/src/generated/grpc/reflection/v1/ExtensionRequest.ts new file mode 100644 index 000000000..34c6fefeb --- /dev/null +++ b/packages/grpc-reflection/src/generated/grpc/reflection/v1/ExtensionRequest.ts @@ -0,0 +1,26 @@ +// Original file: proto/grpc/reflection/v1/reflection.proto + + +/** + * The type name and extension number sent by the client when requesting + * file_containing_extension. + */ +export interface ExtensionRequest { + /** + * Fully-qualified type name. The format should be . + */ + 'containingType'?: (string); + 'extensionNumber'?: (number); +} + +/** + * The type name and extension number sent by the client when requesting + * file_containing_extension. + */ +export interface ExtensionRequest__Output { + /** + * Fully-qualified type name. The format should be . + */ + 'containingType': (string); + 'extensionNumber': (number); +} diff --git a/packages/grpc-reflection/src/generated/grpc/reflection/v1/FileDescriptorResponse.ts b/packages/grpc-reflection/src/generated/grpc/reflection/v1/FileDescriptorResponse.ts new file mode 100644 index 000000000..253e650f9 --- /dev/null +++ b/packages/grpc-reflection/src/generated/grpc/reflection/v1/FileDescriptorResponse.ts @@ -0,0 +1,30 @@ +// Original file: proto/grpc/reflection/v1/reflection.proto + + +/** + * Serialized FileDescriptorProto messages sent by the server answering + * a file_by_filename, file_containing_symbol, or file_containing_extension + * request. + */ +export interface FileDescriptorResponse { + /** + * Serialized FileDescriptorProto messages. We avoid taking a dependency on + * descriptor.proto, which uses proto2 only features, by making them opaque + * bytes instead. + */ + 'fileDescriptorProto'?: (Buffer | Uint8Array | string)[]; +} + +/** + * Serialized FileDescriptorProto messages sent by the server answering + * a file_by_filename, file_containing_symbol, or file_containing_extension + * request. + */ +export interface FileDescriptorResponse__Output { + /** + * Serialized FileDescriptorProto messages. We avoid taking a dependency on + * descriptor.proto, which uses proto2 only features, by making them opaque + * bytes instead. + */ + 'fileDescriptorProto': (Uint8Array)[]; +} diff --git a/packages/grpc-reflection/src/generated/grpc/reflection/v1/ListServiceResponse.ts b/packages/grpc-reflection/src/generated/grpc/reflection/v1/ListServiceResponse.ts new file mode 100644 index 000000000..f1824d4cf --- /dev/null +++ b/packages/grpc-reflection/src/generated/grpc/reflection/v1/ListServiceResponse.ts @@ -0,0 +1,25 @@ +// Original file: proto/grpc/reflection/v1/reflection.proto + +import type { ServiceResponse as _grpc_reflection_v1_ServiceResponse, ServiceResponse__Output as _grpc_reflection_v1_ServiceResponse__Output } from '../../../grpc/reflection/v1/ServiceResponse'; + +/** + * A list of ServiceResponse sent by the server answering list_services request. + */ +export interface ListServiceResponse { + /** + * The information of each service may be expanded in the future, so we use + * ServiceResponse message to encapsulate it. + */ + 'service'?: (_grpc_reflection_v1_ServiceResponse)[]; +} + +/** + * A list of ServiceResponse sent by the server answering list_services request. + */ +export interface ListServiceResponse__Output { + /** + * The information of each service may be expanded in the future, so we use + * ServiceResponse message to encapsulate it. + */ + 'service': (_grpc_reflection_v1_ServiceResponse__Output)[]; +} diff --git a/packages/grpc-reflection/src/generated/grpc/reflection/v1/ServerReflection.ts b/packages/grpc-reflection/src/generated/grpc/reflection/v1/ServerReflection.ts new file mode 100644 index 000000000..65d3b571b --- /dev/null +++ b/packages/grpc-reflection/src/generated/grpc/reflection/v1/ServerReflection.ts @@ -0,0 +1,9 @@ +// Original file: proto/grpc/reflection/v1/reflection.proto + +import type { MethodDefinition } from '@grpc/proto-loader' +import type { ServerReflectionRequest as _grpc_reflection_v1_ServerReflectionRequest, ServerReflectionRequest__Output as _grpc_reflection_v1_ServerReflectionRequest__Output } from '../../../grpc/reflection/v1/ServerReflectionRequest'; +import type { ServerReflectionResponse as _grpc_reflection_v1_ServerReflectionResponse, ServerReflectionResponse__Output as _grpc_reflection_v1_ServerReflectionResponse__Output } from '../../../grpc/reflection/v1/ServerReflectionResponse'; + +export interface ServerReflectionDefinition { + ServerReflectionInfo: MethodDefinition<_grpc_reflection_v1_ServerReflectionRequest, _grpc_reflection_v1_ServerReflectionResponse, _grpc_reflection_v1_ServerReflectionRequest__Output, _grpc_reflection_v1_ServerReflectionResponse__Output> +} diff --git a/packages/grpc-reflection/src/generated/grpc/reflection/v1/ServerReflectionRequest.ts b/packages/grpc-reflection/src/generated/grpc/reflection/v1/ServerReflectionRequest.ts new file mode 100644 index 000000000..301bd3953 --- /dev/null +++ b/packages/grpc-reflection/src/generated/grpc/reflection/v1/ServerReflectionRequest.ts @@ -0,0 +1,91 @@ +// Original file: proto/grpc/reflection/v1/reflection.proto + +import type { ExtensionRequest as _grpc_reflection_v1_ExtensionRequest, ExtensionRequest__Output as _grpc_reflection_v1_ExtensionRequest__Output } from '../../../grpc/reflection/v1/ExtensionRequest'; + +/** + * The message sent by the client when calling ServerReflectionInfo method. + */ +export interface ServerReflectionRequest { + 'host'?: (string); + /** + * Find a proto file by the file name. + */ + 'fileByFilename'?: (string); + /** + * Find the proto file that declares the given fully-qualified symbol name. + * This field should be a fully-qualified symbol name + * (e.g. .[.] or .). + */ + 'fileContainingSymbol'?: (string); + /** + * Find the proto file which defines an extension extending the given + * message type with the given field number. + */ + 'fileContainingExtension'?: (_grpc_reflection_v1_ExtensionRequest | null); + /** + * Finds the tag numbers used by all known extensions of the given message + * type, and appends them to ExtensionNumberResponse in an undefined order. + * Its corresponding method is best-effort: it's not guaranteed that the + * reflection service will implement this method, and it's not guaranteed + * that this method will provide all extensions. Returns + * StatusCode::UNIMPLEMENTED if it's not implemented. + * This field should be a fully-qualified type name. The format is + * . + */ + 'allExtensionNumbersOfType'?: (string); + /** + * List the full names of registered services. The content will not be + * checked. + */ + 'listServices'?: (string); + /** + * To use reflection service, the client should set one of the following + * fields in message_request. The server distinguishes requests by their + * defined field and then handles them using corresponding methods. + */ + 'messageRequest'?: "fileByFilename"|"fileContainingSymbol"|"fileContainingExtension"|"allExtensionNumbersOfType"|"listServices"; +} + +/** + * The message sent by the client when calling ServerReflectionInfo method. + */ +export interface ServerReflectionRequest__Output { + 'host': (string); + /** + * Find a proto file by the file name. + */ + 'fileByFilename'?: (string); + /** + * Find the proto file that declares the given fully-qualified symbol name. + * This field should be a fully-qualified symbol name + * (e.g. .[.] or .). + */ + 'fileContainingSymbol'?: (string); + /** + * Find the proto file which defines an extension extending the given + * message type with the given field number. + */ + 'fileContainingExtension'?: (_grpc_reflection_v1_ExtensionRequest__Output | null); + /** + * Finds the tag numbers used by all known extensions of the given message + * type, and appends them to ExtensionNumberResponse in an undefined order. + * Its corresponding method is best-effort: it's not guaranteed that the + * reflection service will implement this method, and it's not guaranteed + * that this method will provide all extensions. Returns + * StatusCode::UNIMPLEMENTED if it's not implemented. + * This field should be a fully-qualified type name. The format is + * . + */ + 'allExtensionNumbersOfType'?: (string); + /** + * List the full names of registered services. The content will not be + * checked. + */ + 'listServices'?: (string); + /** + * To use reflection service, the client should set one of the following + * fields in message_request. The server distinguishes requests by their + * defined field and then handles them using corresponding methods. + */ + 'messageRequest': "fileByFilename"|"fileContainingSymbol"|"fileContainingExtension"|"allExtensionNumbersOfType"|"listServices"; +} diff --git a/packages/grpc-reflection/src/generated/grpc/reflection/v1/ServerReflectionResponse.ts b/packages/grpc-reflection/src/generated/grpc/reflection/v1/ServerReflectionResponse.ts new file mode 100644 index 000000000..bc2790c15 --- /dev/null +++ b/packages/grpc-reflection/src/generated/grpc/reflection/v1/ServerReflectionResponse.ts @@ -0,0 +1,75 @@ +// Original file: proto/grpc/reflection/v1/reflection.proto + +import type { ServerReflectionRequest as _grpc_reflection_v1_ServerReflectionRequest, ServerReflectionRequest__Output as _grpc_reflection_v1_ServerReflectionRequest__Output } from '../../../grpc/reflection/v1/ServerReflectionRequest'; +import type { FileDescriptorResponse as _grpc_reflection_v1_FileDescriptorResponse, FileDescriptorResponse__Output as _grpc_reflection_v1_FileDescriptorResponse__Output } from '../../../grpc/reflection/v1/FileDescriptorResponse'; +import type { ExtensionNumberResponse as _grpc_reflection_v1_ExtensionNumberResponse, ExtensionNumberResponse__Output as _grpc_reflection_v1_ExtensionNumberResponse__Output } from '../../../grpc/reflection/v1/ExtensionNumberResponse'; +import type { ListServiceResponse as _grpc_reflection_v1_ListServiceResponse, ListServiceResponse__Output as _grpc_reflection_v1_ListServiceResponse__Output } from '../../../grpc/reflection/v1/ListServiceResponse'; +import type { ErrorResponse as _grpc_reflection_v1_ErrorResponse, ErrorResponse__Output as _grpc_reflection_v1_ErrorResponse__Output } from '../../../grpc/reflection/v1/ErrorResponse'; + +/** + * The message sent by the server to answer ServerReflectionInfo method. + */ +export interface ServerReflectionResponse { + 'validHost'?: (string); + 'originalRequest'?: (_grpc_reflection_v1_ServerReflectionRequest | null); + /** + * This message is used to answer file_by_filename, file_containing_symbol, + * file_containing_extension requests with transitive dependencies. + * As the repeated label is not allowed in oneof fields, we use a + * FileDescriptorResponse message to encapsulate the repeated fields. + * The reflection service is allowed to avoid sending FileDescriptorProtos + * that were previously sent in response to earlier requests in the stream. + */ + 'fileDescriptorResponse'?: (_grpc_reflection_v1_FileDescriptorResponse | null); + /** + * This message is used to answer all_extension_numbers_of_type requests. + */ + 'allExtensionNumbersResponse'?: (_grpc_reflection_v1_ExtensionNumberResponse | null); + /** + * This message is used to answer list_services requests. + */ + 'listServicesResponse'?: (_grpc_reflection_v1_ListServiceResponse | null); + /** + * This message is used when an error occurs. + */ + 'errorResponse'?: (_grpc_reflection_v1_ErrorResponse | null); + /** + * The server sets one of the following fields according to the message_request + * in the request. + */ + 'messageResponse'?: "fileDescriptorResponse"|"allExtensionNumbersResponse"|"listServicesResponse"|"errorResponse"; +} + +/** + * The message sent by the server to answer ServerReflectionInfo method. + */ +export interface ServerReflectionResponse__Output { + 'validHost': (string); + 'originalRequest': (_grpc_reflection_v1_ServerReflectionRequest__Output | null); + /** + * This message is used to answer file_by_filename, file_containing_symbol, + * file_containing_extension requests with transitive dependencies. + * As the repeated label is not allowed in oneof fields, we use a + * FileDescriptorResponse message to encapsulate the repeated fields. + * The reflection service is allowed to avoid sending FileDescriptorProtos + * that were previously sent in response to earlier requests in the stream. + */ + 'fileDescriptorResponse'?: (_grpc_reflection_v1_FileDescriptorResponse__Output | null); + /** + * This message is used to answer all_extension_numbers_of_type requests. + */ + 'allExtensionNumbersResponse'?: (_grpc_reflection_v1_ExtensionNumberResponse__Output | null); + /** + * This message is used to answer list_services requests. + */ + 'listServicesResponse'?: (_grpc_reflection_v1_ListServiceResponse__Output | null); + /** + * This message is used when an error occurs. + */ + 'errorResponse'?: (_grpc_reflection_v1_ErrorResponse__Output | null); + /** + * The server sets one of the following fields according to the message_request + * in the request. + */ + 'messageResponse': "fileDescriptorResponse"|"allExtensionNumbersResponse"|"listServicesResponse"|"errorResponse"; +} diff --git a/packages/grpc-reflection/src/generated/grpc/reflection/v1/ServiceResponse.ts b/packages/grpc-reflection/src/generated/grpc/reflection/v1/ServiceResponse.ts new file mode 100644 index 000000000..d529538e2 --- /dev/null +++ b/packages/grpc-reflection/src/generated/grpc/reflection/v1/ServiceResponse.ts @@ -0,0 +1,26 @@ +// Original file: proto/grpc/reflection/v1/reflection.proto + + +/** + * The information of a single service used by ListServiceResponse to answer + * list_services request. + */ +export interface ServiceResponse { + /** + * Full name of a registered service, including its package name. The format + * is . + */ + 'name'?: (string); +} + +/** + * The information of a single service used by ListServiceResponse to answer + * list_services request. + */ +export interface ServiceResponse__Output { + /** + * Full name of a registered service, including its package name. The format + * is . + */ + 'name': (string); +} diff --git a/packages/grpc-reflection/src/generated/grpc/reflection/v1alpha/ErrorResponse.ts b/packages/grpc-reflection/src/generated/grpc/reflection/v1alpha/ErrorResponse.ts new file mode 100644 index 000000000..dc6c3a2e2 --- /dev/null +++ b/packages/grpc-reflection/src/generated/grpc/reflection/v1alpha/ErrorResponse.ts @@ -0,0 +1,24 @@ +// Original file: proto/grpc/reflection/v1alpha/reflection.proto + + +/** + * The error code and error message sent by the server when an error occurs. + */ +export interface ErrorResponse { + /** + * This field uses the error codes defined in grpc::StatusCode. + */ + 'errorCode'?: (number); + 'errorMessage'?: (string); +} + +/** + * The error code and error message sent by the server when an error occurs. + */ +export interface ErrorResponse__Output { + /** + * This field uses the error codes defined in grpc::StatusCode. + */ + 'errorCode': (number); + 'errorMessage': (string); +} diff --git a/packages/grpc-reflection/src/generated/grpc/reflection/v1alpha/ExtensionNumberResponse.ts b/packages/grpc-reflection/src/generated/grpc/reflection/v1alpha/ExtensionNumberResponse.ts new file mode 100644 index 000000000..b6c322c4e --- /dev/null +++ b/packages/grpc-reflection/src/generated/grpc/reflection/v1alpha/ExtensionNumberResponse.ts @@ -0,0 +1,28 @@ +// Original file: proto/grpc/reflection/v1alpha/reflection.proto + + +/** + * A list of extension numbers sent by the server answering + * all_extension_numbers_of_type request. + */ +export interface ExtensionNumberResponse { + /** + * Full name of the base type, including the package name. The format + * is . + */ + 'baseTypeName'?: (string); + 'extensionNumber'?: (number)[]; +} + +/** + * A list of extension numbers sent by the server answering + * all_extension_numbers_of_type request. + */ +export interface ExtensionNumberResponse__Output { + /** + * Full name of the base type, including the package name. The format + * is . + */ + 'baseTypeName': (string); + 'extensionNumber': (number)[]; +} diff --git a/packages/grpc-reflection/src/generated/grpc/reflection/v1alpha/ExtensionRequest.ts b/packages/grpc-reflection/src/generated/grpc/reflection/v1alpha/ExtensionRequest.ts new file mode 100644 index 000000000..4a378b35a --- /dev/null +++ b/packages/grpc-reflection/src/generated/grpc/reflection/v1alpha/ExtensionRequest.ts @@ -0,0 +1,26 @@ +// Original file: proto/grpc/reflection/v1alpha/reflection.proto + + +/** + * The type name and extension number sent by the client when requesting + * file_containing_extension. + */ +export interface ExtensionRequest { + /** + * Fully-qualified type name. The format should be . + */ + 'containingType'?: (string); + 'extensionNumber'?: (number); +} + +/** + * The type name and extension number sent by the client when requesting + * file_containing_extension. + */ +export interface ExtensionRequest__Output { + /** + * Fully-qualified type name. The format should be . + */ + 'containingType': (string); + 'extensionNumber': (number); +} diff --git a/packages/grpc-reflection/src/generated/grpc/reflection/v1alpha/FileDescriptorResponse.ts b/packages/grpc-reflection/src/generated/grpc/reflection/v1alpha/FileDescriptorResponse.ts new file mode 100644 index 000000000..cb1cc38c4 --- /dev/null +++ b/packages/grpc-reflection/src/generated/grpc/reflection/v1alpha/FileDescriptorResponse.ts @@ -0,0 +1,30 @@ +// Original file: proto/grpc/reflection/v1alpha/reflection.proto + + +/** + * Serialized FileDescriptorProto messages sent by the server answering + * a file_by_filename, file_containing_symbol, or file_containing_extension + * request. + */ +export interface FileDescriptorResponse { + /** + * Serialized FileDescriptorProto messages. We avoid taking a dependency on + * descriptor.proto, which uses proto2 only features, by making them opaque + * bytes instead. + */ + 'fileDescriptorProto'?: (Buffer | Uint8Array | string)[]; +} + +/** + * Serialized FileDescriptorProto messages sent by the server answering + * a file_by_filename, file_containing_symbol, or file_containing_extension + * request. + */ +export interface FileDescriptorResponse__Output { + /** + * Serialized FileDescriptorProto messages. We avoid taking a dependency on + * descriptor.proto, which uses proto2 only features, by making them opaque + * bytes instead. + */ + 'fileDescriptorProto': (Uint8Array)[]; +} diff --git a/packages/grpc-reflection/src/generated/grpc/reflection/v1alpha/ListServiceResponse.ts b/packages/grpc-reflection/src/generated/grpc/reflection/v1alpha/ListServiceResponse.ts new file mode 100644 index 000000000..7793a16eb --- /dev/null +++ b/packages/grpc-reflection/src/generated/grpc/reflection/v1alpha/ListServiceResponse.ts @@ -0,0 +1,25 @@ +// Original file: proto/grpc/reflection/v1alpha/reflection.proto + +import type { ServiceResponse as _grpc_reflection_v1alpha_ServiceResponse, ServiceResponse__Output as _grpc_reflection_v1alpha_ServiceResponse__Output } from '../../../grpc/reflection/v1alpha/ServiceResponse'; + +/** + * A list of ServiceResponse sent by the server answering list_services request. + */ +export interface ListServiceResponse { + /** + * The information of each service may be expanded in the future, so we use + * ServiceResponse message to encapsulate it. + */ + 'service'?: (_grpc_reflection_v1alpha_ServiceResponse)[]; +} + +/** + * A list of ServiceResponse sent by the server answering list_services request. + */ +export interface ListServiceResponse__Output { + /** + * The information of each service may be expanded in the future, so we use + * ServiceResponse message to encapsulate it. + */ + 'service': (_grpc_reflection_v1alpha_ServiceResponse__Output)[]; +} diff --git a/packages/grpc-reflection/src/generated/grpc/reflection/v1alpha/ServerReflection.ts b/packages/grpc-reflection/src/generated/grpc/reflection/v1alpha/ServerReflection.ts new file mode 100644 index 000000000..2ab03e93c --- /dev/null +++ b/packages/grpc-reflection/src/generated/grpc/reflection/v1alpha/ServerReflection.ts @@ -0,0 +1,9 @@ +// Original file: proto/grpc/reflection/v1alpha/reflection.proto + +import type { MethodDefinition } from '@grpc/proto-loader' +import type { ServerReflectionRequest as _grpc_reflection_v1alpha_ServerReflectionRequest, ServerReflectionRequest__Output as _grpc_reflection_v1alpha_ServerReflectionRequest__Output } from '../../../grpc/reflection/v1alpha/ServerReflectionRequest'; +import type { ServerReflectionResponse as _grpc_reflection_v1alpha_ServerReflectionResponse, ServerReflectionResponse__Output as _grpc_reflection_v1alpha_ServerReflectionResponse__Output } from '../../../grpc/reflection/v1alpha/ServerReflectionResponse'; + +export interface ServerReflectionDefinition { + ServerReflectionInfo: MethodDefinition<_grpc_reflection_v1alpha_ServerReflectionRequest, _grpc_reflection_v1alpha_ServerReflectionResponse, _grpc_reflection_v1alpha_ServerReflectionRequest__Output, _grpc_reflection_v1alpha_ServerReflectionResponse__Output> +} diff --git a/packages/grpc-reflection/src/generated/grpc/reflection/v1alpha/ServerReflectionRequest.ts b/packages/grpc-reflection/src/generated/grpc/reflection/v1alpha/ServerReflectionRequest.ts new file mode 100644 index 000000000..097d848d0 --- /dev/null +++ b/packages/grpc-reflection/src/generated/grpc/reflection/v1alpha/ServerReflectionRequest.ts @@ -0,0 +1,91 @@ +// Original file: proto/grpc/reflection/v1alpha/reflection.proto + +import type { ExtensionRequest as _grpc_reflection_v1alpha_ExtensionRequest, ExtensionRequest__Output as _grpc_reflection_v1alpha_ExtensionRequest__Output } from '../../../grpc/reflection/v1alpha/ExtensionRequest'; + +/** + * The message sent by the client when calling ServerReflectionInfo method. + */ +export interface ServerReflectionRequest { + 'host'?: (string); + /** + * Find a proto file by the file name. + */ + 'fileByFilename'?: (string); + /** + * Find the proto file that declares the given fully-qualified symbol name. + * This field should be a fully-qualified symbol name + * (e.g. .[.] or .). + */ + 'fileContainingSymbol'?: (string); + /** + * Find the proto file which defines an extension extending the given + * message type with the given field number. + */ + 'fileContainingExtension'?: (_grpc_reflection_v1alpha_ExtensionRequest | null); + /** + * Finds the tag numbers used by all known extensions of the given message + * type, and appends them to ExtensionNumberResponse in an undefined order. + * Its corresponding method is best-effort: it's not guaranteed that the + * reflection service will implement this method, and it's not guaranteed + * that this method will provide all extensions. Returns + * StatusCode::UNIMPLEMENTED if it's not implemented. + * This field should be a fully-qualified type name. The format is + * . + */ + 'allExtensionNumbersOfType'?: (string); + /** + * List the full names of registered services. The content will not be + * checked. + */ + 'listServices'?: (string); + /** + * To use reflection service, the client should set one of the following + * fields in message_request. The server distinguishes requests by their + * defined field and then handles them using corresponding methods. + */ + 'messageRequest'?: "fileByFilename"|"fileContainingSymbol"|"fileContainingExtension"|"allExtensionNumbersOfType"|"listServices"; +} + +/** + * The message sent by the client when calling ServerReflectionInfo method. + */ +export interface ServerReflectionRequest__Output { + 'host': (string); + /** + * Find a proto file by the file name. + */ + 'fileByFilename'?: (string); + /** + * Find the proto file that declares the given fully-qualified symbol name. + * This field should be a fully-qualified symbol name + * (e.g. .[.] or .). + */ + 'fileContainingSymbol'?: (string); + /** + * Find the proto file which defines an extension extending the given + * message type with the given field number. + */ + 'fileContainingExtension'?: (_grpc_reflection_v1alpha_ExtensionRequest__Output | null); + /** + * Finds the tag numbers used by all known extensions of the given message + * type, and appends them to ExtensionNumberResponse in an undefined order. + * Its corresponding method is best-effort: it's not guaranteed that the + * reflection service will implement this method, and it's not guaranteed + * that this method will provide all extensions. Returns + * StatusCode::UNIMPLEMENTED if it's not implemented. + * This field should be a fully-qualified type name. The format is + * . + */ + 'allExtensionNumbersOfType'?: (string); + /** + * List the full names of registered services. The content will not be + * checked. + */ + 'listServices'?: (string); + /** + * To use reflection service, the client should set one of the following + * fields in message_request. The server distinguishes requests by their + * defined field and then handles them using corresponding methods. + */ + 'messageRequest': "fileByFilename"|"fileContainingSymbol"|"fileContainingExtension"|"allExtensionNumbersOfType"|"listServices"; +} diff --git a/packages/grpc-reflection/src/generated/grpc/reflection/v1alpha/ServerReflectionResponse.ts b/packages/grpc-reflection/src/generated/grpc/reflection/v1alpha/ServerReflectionResponse.ts new file mode 100644 index 000000000..eb81a0c2a --- /dev/null +++ b/packages/grpc-reflection/src/generated/grpc/reflection/v1alpha/ServerReflectionResponse.ts @@ -0,0 +1,75 @@ +// Original file: proto/grpc/reflection/v1alpha/reflection.proto + +import type { ServerReflectionRequest as _grpc_reflection_v1alpha_ServerReflectionRequest, ServerReflectionRequest__Output as _grpc_reflection_v1alpha_ServerReflectionRequest__Output } from '../../../grpc/reflection/v1alpha/ServerReflectionRequest'; +import type { FileDescriptorResponse as _grpc_reflection_v1alpha_FileDescriptorResponse, FileDescriptorResponse__Output as _grpc_reflection_v1alpha_FileDescriptorResponse__Output } from '../../../grpc/reflection/v1alpha/FileDescriptorResponse'; +import type { ExtensionNumberResponse as _grpc_reflection_v1alpha_ExtensionNumberResponse, ExtensionNumberResponse__Output as _grpc_reflection_v1alpha_ExtensionNumberResponse__Output } from '../../../grpc/reflection/v1alpha/ExtensionNumberResponse'; +import type { ListServiceResponse as _grpc_reflection_v1alpha_ListServiceResponse, ListServiceResponse__Output as _grpc_reflection_v1alpha_ListServiceResponse__Output } from '../../../grpc/reflection/v1alpha/ListServiceResponse'; +import type { ErrorResponse as _grpc_reflection_v1alpha_ErrorResponse, ErrorResponse__Output as _grpc_reflection_v1alpha_ErrorResponse__Output } from '../../../grpc/reflection/v1alpha/ErrorResponse'; + +/** + * The message sent by the server to answer ServerReflectionInfo method. + */ +export interface ServerReflectionResponse { + 'validHost'?: (string); + 'originalRequest'?: (_grpc_reflection_v1alpha_ServerReflectionRequest | null); + /** + * This message is used to answer file_by_filename, file_containing_symbol, + * file_containing_extension requests with transitive dependencies. As + * the repeated label is not allowed in oneof fields, we use a + * FileDescriptorResponse message to encapsulate the repeated fields. + * The reflection service is allowed to avoid sending FileDescriptorProtos + * that were previously sent in response to earlier requests in the stream. + */ + 'fileDescriptorResponse'?: (_grpc_reflection_v1alpha_FileDescriptorResponse | null); + /** + * This message is used to answer all_extension_numbers_of_type requst. + */ + 'allExtensionNumbersResponse'?: (_grpc_reflection_v1alpha_ExtensionNumberResponse | null); + /** + * This message is used to answer list_services request. + */ + 'listServicesResponse'?: (_grpc_reflection_v1alpha_ListServiceResponse | null); + /** + * This message is used when an error occurs. + */ + 'errorResponse'?: (_grpc_reflection_v1alpha_ErrorResponse | null); + /** + * The server set one of the following fields accroding to the message_request + * in the request. + */ + 'messageResponse'?: "fileDescriptorResponse"|"allExtensionNumbersResponse"|"listServicesResponse"|"errorResponse"; +} + +/** + * The message sent by the server to answer ServerReflectionInfo method. + */ +export interface ServerReflectionResponse__Output { + 'validHost': (string); + 'originalRequest': (_grpc_reflection_v1alpha_ServerReflectionRequest__Output | null); + /** + * This message is used to answer file_by_filename, file_containing_symbol, + * file_containing_extension requests with transitive dependencies. As + * the repeated label is not allowed in oneof fields, we use a + * FileDescriptorResponse message to encapsulate the repeated fields. + * The reflection service is allowed to avoid sending FileDescriptorProtos + * that were previously sent in response to earlier requests in the stream. + */ + 'fileDescriptorResponse'?: (_grpc_reflection_v1alpha_FileDescriptorResponse__Output | null); + /** + * This message is used to answer all_extension_numbers_of_type requst. + */ + 'allExtensionNumbersResponse'?: (_grpc_reflection_v1alpha_ExtensionNumberResponse__Output | null); + /** + * This message is used to answer list_services request. + */ + 'listServicesResponse'?: (_grpc_reflection_v1alpha_ListServiceResponse__Output | null); + /** + * This message is used when an error occurs. + */ + 'errorResponse'?: (_grpc_reflection_v1alpha_ErrorResponse__Output | null); + /** + * The server set one of the following fields accroding to the message_request + * in the request. + */ + 'messageResponse': "fileDescriptorResponse"|"allExtensionNumbersResponse"|"listServicesResponse"|"errorResponse"; +} diff --git a/packages/grpc-reflection/src/generated/grpc/reflection/v1alpha/ServiceResponse.ts b/packages/grpc-reflection/src/generated/grpc/reflection/v1alpha/ServiceResponse.ts new file mode 100644 index 000000000..ff35cf522 --- /dev/null +++ b/packages/grpc-reflection/src/generated/grpc/reflection/v1alpha/ServiceResponse.ts @@ -0,0 +1,26 @@ +// Original file: proto/grpc/reflection/v1alpha/reflection.proto + + +/** + * The information of a single service used by ListServiceResponse to answer + * list_services request. + */ +export interface ServiceResponse { + /** + * Full name of a registered service, including its package name. The format + * is . + */ + 'name'?: (string); +} + +/** + * The information of a single service used by ListServiceResponse to answer + * list_services request. + */ +export interface ServiceResponse__Output { + /** + * Full name of a registered service, including its package name. The format + * is . + */ + 'name': (string); +} diff --git a/packages/grpc-reflection/src/implementations/common/constants.ts b/packages/grpc-reflection/src/implementations/common/constants.ts new file mode 100644 index 000000000..93ad27825 --- /dev/null +++ b/packages/grpc-reflection/src/implementations/common/constants.ts @@ -0,0 +1,14 @@ +import * as protoLoader from '@grpc/proto-loader'; + +/** Options to use when loading protobuf files in this repo +* +* @remarks *must* match the proto-loader-gen-types usage in the package.json +* otherwise the generated types may not match the data coming into this service +*/ +export const PROTO_LOADER_OPTS: protoLoader.Options = { + longs: String, + enums: String, + bytes: Array, + defaults: true, + oneofs: true +}; diff --git a/packages/grpc-reflection/src/implementations/common/interfaces.ts b/packages/grpc-reflection/src/implementations/common/interfaces.ts new file mode 100644 index 000000000..c6b07bccb --- /dev/null +++ b/packages/grpc-reflection/src/implementations/common/interfaces.ts @@ -0,0 +1,5 @@ +/** Options to create a reflection server */ +export interface ReflectionServerOptions { + /** whitelist of fully-qualified service names to expose. (Default: expose all) */ + services?: string[]; +} diff --git a/packages/grpc-reflection/src/implementations/common/protobuf-visitor.ts b/packages/grpc-reflection/src/implementations/common/protobuf-visitor.ts new file mode 100644 index 000000000..451a2c0ea --- /dev/null +++ b/packages/grpc-reflection/src/implementations/common/protobuf-visitor.ts @@ -0,0 +1,109 @@ +import { + IDescriptorProto, + IEnumDescriptorProto, + IEnumValueDescriptorProto, + IFieldDescriptorProto, + IFileDescriptorProto, + IMethodDescriptorProto, + IOneofDescriptorProto, + IServiceDescriptorProto, +} from 'protobufjs/ext/descriptor'; + +/** A set of functions for operating on protobuf objects as we visit them in a traversal */ +interface Visitor { + field?: (fqn: string, file: IFileDescriptorProto, field: IFieldDescriptorProto) => void; + extension?: (fqn: string, file: IFileDescriptorProto, extension: IFieldDescriptorProto) => void; + oneOf?: (fqn: string, file: IFileDescriptorProto, decl: IOneofDescriptorProto) => void; + message?: (fqn: string, file: IFileDescriptorProto, msg: IDescriptorProto) => void; + enum?: (fqn: string, file: IFileDescriptorProto, msg: IEnumDescriptorProto) => void; + enumValue?: (fqn: string, file: IFileDescriptorProto, msg: IEnumValueDescriptorProto) => void; + service?: (fqn: string, file: IFileDescriptorProto, msg: IServiceDescriptorProto) => void; + method?: (fqn: string, file: IFileDescriptorProto, method: IMethodDescriptorProto) => void; +} + +/** Visit each node in a protobuf file and perform an operation on it + * + * This is useful because protocol buffers has nested objects so if we need to + * traverse them multiple times then we don't want to duplicate that traversal + * logic + * + * @see Visitor for the interface to interact with the nodes + */ +export const visit = (file: IFileDescriptorProto, visitor: Visitor): void => { + const processField = (prefix: string, file: IFileDescriptorProto, field: IFieldDescriptorProto) => { + const fqn = `${prefix}.${field.name}`; + if (visitor.field) { + visitor.field(fqn, file, field); + } + }; + + const processExtension = ( + prefix: string, + file: IFileDescriptorProto, + ext: IFieldDescriptorProto, + ) => { + const fqn = `${prefix}.${ext.name}`; + if (visitor.extension) { + visitor.extension(fqn, file, ext); + } + }; + + const processOneOf = (prefix: string, file: IFileDescriptorProto, decl: IOneofDescriptorProto) => { + const fqn = `${prefix}.${decl.name}`; + if (visitor.oneOf) { + visitor.oneOf(fqn, file, decl); + } + }; + + const processEnum = (prefix: string, file: IFileDescriptorProto, decl: IEnumDescriptorProto) => { + const fqn = `${prefix}.${decl.name}`; + + if (visitor.enum) { + visitor.enum(fqn, file, decl); + } + + decl.value?.forEach((value) => { + const valueFqn = `${fqn}.${value.name}`; + if (visitor.enumValue) { + visitor.enumValue(valueFqn, file, value); + } + }); + }; + + const processMessage = (prefix: string, file: IFileDescriptorProto, msg: IDescriptorProto) => { + const fqn = `${prefix}.${msg.name}`; + if (visitor.message) { + visitor.message(fqn, file, msg); + } + + msg.nestedType?.forEach((type) => processMessage(fqn, file, type)); + msg.enumType?.forEach((type) => processEnum(fqn, file, type)); + msg.field?.forEach((field) => processField(fqn, file, field)); + msg.oneofDecl?.forEach((decl) => processOneOf(fqn, file, decl)); + msg.extension?.forEach((ext) => processExtension(fqn, file, ext)); + }; + + const processService = ( + prefix: string, + file: IFileDescriptorProto, + service: IServiceDescriptorProto, + ) => { + const fqn = `${prefix}.${service.name}`; + if (visitor.service) { + visitor.service(fqn, file, service); + } + + service.method?.forEach((method) => { + const methodFqn = `${fqn}.${method.name}`; + if (visitor.method) { + visitor.method(methodFqn, file, method); + } + }); + }; + + const packageName = file.package || ''; + file.enumType?.forEach((type) => processEnum(packageName, file, type)); + file.messageType?.forEach((type) => processMessage(packageName, file, type)); + file.service?.forEach((service) => processService(packageName, file, service)); + file.extension?.forEach((ext) => processExtension(packageName, file, ext)); +}; diff --git a/packages/grpc-reflection/src/implementations/common/utils.ts b/packages/grpc-reflection/src/implementations/common/utils.ts new file mode 100644 index 000000000..4490abd6e --- /dev/null +++ b/packages/grpc-reflection/src/implementations/common/utils.ts @@ -0,0 +1,11 @@ +/** Gets the package scope for a type name + * + * @example scope('grpc.reflection.v1.Type') == 'grpc.reflection.v1' + */ +export const scope = (path: string, separator: string = '.') => { + if (!path.includes(separator)) { + return ''; + } + + return path.split(separator).slice(0, -1).join(separator); +}; diff --git a/packages/grpc-reflection/src/implementations/reflection-v1.ts b/packages/grpc-reflection/src/implementations/reflection-v1.ts new file mode 100644 index 000000000..913f93719 --- /dev/null +++ b/packages/grpc-reflection/src/implementations/reflection-v1.ts @@ -0,0 +1,333 @@ +import * as path from 'path'; +import { + FileDescriptorProto, + IFileDescriptorProto, + IServiceDescriptorProto +} from 'protobufjs/ext/descriptor'; + +import * as grpc from '@grpc/grpc-js'; +import * as protoLoader from '@grpc/proto-loader'; + +import { ExtensionNumberResponse__Output } from '../generated/grpc/reflection/v1/ExtensionNumberResponse'; +import { FileDescriptorResponse__Output } from '../generated/grpc/reflection/v1/FileDescriptorResponse'; +import { ListServiceResponse__Output } from '../generated/grpc/reflection/v1/ListServiceResponse'; +import { ServerReflectionRequest } from '../generated/grpc/reflection/v1/ServerReflectionRequest'; +import { ServerReflectionResponse } from '../generated/grpc/reflection/v1/ServerReflectionResponse'; +import { visit } from './common/protobuf-visitor'; +import { scope } from './common/utils'; +import { PROTO_LOADER_OPTS } from './common/constants'; +import { ReflectionServerOptions } from './common/interfaces'; + +export class ReflectionError extends Error { + constructor( + readonly statusCode: grpc.status, + readonly message: string, + ) { + super(message); + } +} + +/** Analyzes a gRPC package definition and exposes methods to reflect on it + * + * NOTE: the files returned by this service may not match the handwritten ones 1:1. + * This is because proto-loader reorients files based on their package definition, + * combining any that have the same package. + * + * For example: if files 'a.proto' and 'b.proto' are both for the same package 'c' then + * we will always return a reference to a combined 'c.proto' instead of the 2 files. + */ +export class ReflectionV1Implementation { + + /** An index of proto files by file name (eg. 'sample.proto') */ + private readonly files: Record = {}; + + /** A graph of file dependencies */ + private readonly fileDependencies = new Map(); + + /** Pre-computed encoded-versions of each file */ + private readonly fileEncodings = new Map(); + + /** An index of proto files by type extension relationship + * + * extensionIndex[.][] contains a reference to the file containing an + * extension for the type "." and field number "" + */ + private readonly extensions: Record> = {}; + + /** An index of fully qualified symbol names (eg. 'sample.Message') to the files that contain them */ + private readonly symbols: Record = {}; + + /** An index of the services in the analyzed package(s) */ + private readonly services: Record = {}; + + + constructor(root: protoLoader.PackageDefinition, options?: ReflectionServerOptions) { + Object.values(root).forEach(({ fileDescriptorProtos }) => { + if (Array.isArray(fileDescriptorProtos)) { // we use an array check to narrow the type + fileDescriptorProtos.forEach((bin) => { + const proto = FileDescriptorProto.decode(bin) as IFileDescriptorProto; + + if (proto.name && !this.files[proto.name]) { + this.files[proto.name] = proto; + } + }); + } + }); + + // Pass 1: Index Values + const serviceWhitelist = new Set(options?.services); + const index = (fqn: string, file: IFileDescriptorProto) => (this.symbols[fqn] = file); + Object.values(this.files).forEach((file) => + visit(file, { + field: index, + oneOf: index, + message: index, + method: index, + enum: index, + enumValue: index, + service: (fqn, file, service) => { + index(fqn, file); + + if (options?.services === undefined || serviceWhitelist.has(fqn)) { + this.services[fqn] = service; + } + }, + extension: (fqn, file, ext) => { + index(fqn, file); + + const extendeeName = ext.extendee || ''; + this.extensions[extendeeName] = { + ...(this.extensions[extendeeName] || {}), + [ext.number || -1]: file, + }; + }, + }), + ); + + // Pass 2: Link References To Values + // NOTE: this should be unnecessary after https://github.com/grpc/grpc-node/issues/2595 is resolved + const addReference = (ref: string, sourceFile: IFileDescriptorProto, pkgScope: string) => { + if (!ref) { + return; // nothing to do + } + + let referencedFile: IFileDescriptorProto | null = null; + if (ref.startsWith('.')) { + // absolute reference -- just remove the leading '.' and use the ref directly + referencedFile = this.symbols[ref.replace(/^\./, '')]; + } else { + // relative reference -- need to seek upwards up the current package scope until we find it + let pkg = pkgScope; + while (pkg && !referencedFile) { + referencedFile = this.symbols[`${pkg}.${ref}`]; + pkg = scope(pkg); + } + + // if we didn't find anything then try just a FQN lookup + if (!referencedFile) { + referencedFile = this.symbols[ref]; + } + } + + if (!referencedFile) { + console.warn(`Could not find file associated with reference ${ref}`); + return; + } + + if (referencedFile !== sourceFile) { + const existingDeps = this.fileDependencies.get(sourceFile) || []; + this.fileDependencies.set(sourceFile, [referencedFile, ...existingDeps]); + } + }; + + Object.values(this.files).forEach((file) => + visit(file, { + field: (fqn, file, field) => addReference(field.typeName || '', file, scope(fqn)), + extension: (fqn, file, ext) => addReference(ext.typeName || '', file, scope(fqn)), + method: (fqn, file, method) => { + addReference(method.inputType || '', file, scope(fqn)); + addReference(method.outputType || '', file, scope(fqn)); + }, + }), + ); + + // Pass 3: pre-compute file encoding since that can be slow and is done frequently + Object.values(this.files).forEach(file => { + this.fileEncodings.set(file, FileDescriptorProto.encode(file).finish()) + }); + } + + addToServer(server: Pick) { + const protoPath = path.join(__dirname, '../../proto/grpc/reflection/v1/reflection.proto'); + const pkgDefinition = protoLoader.loadSync(protoPath, PROTO_LOADER_OPTS); + const pkg = grpc.loadPackageDefinition(pkgDefinition) as any; + + server.addService(pkg.grpc.reflection.v1.ServerReflection.service, { + ServerReflectionInfo: ( + stream: grpc.ServerDuplexStream + ) => { + stream.on('end', () => stream.end()); + + stream.on('data', (message: ServerReflectionRequest) => { + stream.write(this.handleServerReflectionRequest(message)); + }); + } + }); + } + + /** Assemble a response for a single server reflection request in the stream */ + handleServerReflectionRequest(message: ServerReflectionRequest): ServerReflectionResponse { + const response: ServerReflectionResponse = { + validHost: message.host, + originalRequest: message + }; + + try { + switch(message.messageRequest) { + case 'listServices': + response.listServicesResponse = this.listServices(message.listServices || ''); + break; + case 'fileContainingSymbol': + response.fileDescriptorResponse = this.fileContainingSymbol(message.fileContainingSymbol || ''); + break; + case 'fileByFilename': + response.fileDescriptorResponse = this.fileByFilename(message.fileByFilename || ''); + break; + case 'fileContainingExtension': + response.fileDescriptorResponse = this.fileContainingExtension( + message.fileContainingExtension?.containingType || '', + message.fileContainingExtension?.extensionNumber || -1 + ); + break; + case 'allExtensionNumbersOfType': + response.allExtensionNumbersResponse = this.allExtensionNumbersOfType(message.allExtensionNumbersOfType || ''); + break; + default: + throw new ReflectionError( + grpc.status.UNIMPLEMENTED, + `Unimplemented method for request: ${message.messageRequest}`, + ); + } + } catch (e) { + if (e instanceof ReflectionError) { + response.errorResponse = { + errorCode: e.statusCode, + errorMessage: e.message, + }; + } else { + response.errorResponse = { + errorCode: grpc.status.UNKNOWN, + errorMessage: 'Failed to process gRPC reflection request: unknown error', + }; + } + } + + return response; + } + + /** List the full names of registered gRPC services + * + * note: the spec is unclear as to what the 'listServices' param can be; most + * clients seem to only pass '*' but unsure if this should behave like a + * filter. Until we know how this should behave with different inputs this + * just always returns *all* services. + * + * @returns full-qualified service names (eg. 'sample.SampleService') + */ + listServices(listServices: string): ListServiceResponse__Output { + return { service: Object.keys(this.services).map((service) => ({ name: service })) }; + } + + /** Find the proto file(s) that declares the given fully-qualified symbol name + * + * @param symbol fully-qualified name of the symbol to lookup + * (e.g. package.service[.method] or package.type) + * + * @returns descriptors of the file which contains this symbol and its imports + */ + fileContainingSymbol(symbol: string): FileDescriptorResponse__Output { + const file = this.symbols[symbol]; + + if (!file) { + throw new ReflectionError(grpc.status.NOT_FOUND, `Symbol not found: ${symbol}`); + } + + const deps = this.getFileDependencies(file); + + return { + fileDescriptorProto: [file, ...deps].map((file) => this.fileEncodings.get(file) || new Uint8Array()) + }; + } + + /** Find a proto file by the file name + * + * @returns descriptors of the file which contains this symbol and its imports + */ + fileByFilename(filename: string): FileDescriptorResponse__Output { + const file = this.files[filename]; + + if (!file) { + throw new ReflectionError(grpc.status.NOT_FOUND, `Proto file not found: ${filename}`); + } + + const deps = this.getFileDependencies(file); + + return { + fileDescriptorProto: [file, ...deps].map((file) => this.fileEncodings.get(file) || new Uint8Array), + }; + } + + /** Find a proto file containing an extension to a message type + * + * @returns descriptors of the file which contains this symbol and its imports + */ + fileContainingExtension(symbol: string, field: number): FileDescriptorResponse__Output { + const extensionsByFieldNumber = this.extensions[symbol] || {}; + const file = extensionsByFieldNumber[field]; + + if (!file) { + throw new ReflectionError( + grpc.status.NOT_FOUND, + `Extension not found for symbol ${symbol} at field ${field}`, + ); + } + + const deps = this.getFileDependencies(file); + + return { + fileDescriptorProto: [file, ...deps].map((file) => this.fileEncodings.get(file) || new Uint8Array()), + }; + } + + allExtensionNumbersOfType(symbol: string): ExtensionNumberResponse__Output { + if (!(symbol in this.extensions)) { + throw new ReflectionError(grpc.status.NOT_FOUND, `Extensions not found for symbol ${symbol}`); + } + + const fieldNumbers = Object.keys(this.extensions[symbol]).map((key) => Number(key)); + + return { + baseTypeName: symbol, + extensionNumber: fieldNumbers, + }; + } + + private getFileDependencies(file: IFileDescriptorProto): IFileDescriptorProto[] { + const visited: Set = new Set(); + const toVisit: IFileDescriptorProto[] = this.fileDependencies.get(file) || []; + + while (toVisit.length > 0) { + const current = toVisit.pop(); + + if (!current || visited.has(current)) { + continue; + } + + visited.add(current); + toVisit.push(...this.fileDependencies.get(current)?.filter((dep) => !visited.has(dep)) || []); + } + + return Array.from(visited); + } + +} diff --git a/packages/grpc-reflection/src/implementations/reflection-v1alpha.ts b/packages/grpc-reflection/src/implementations/reflection-v1alpha.ts new file mode 100644 index 000000000..f3fdcafe0 --- /dev/null +++ b/packages/grpc-reflection/src/implementations/reflection-v1alpha.ts @@ -0,0 +1,42 @@ +import * as path from 'path'; + +import * as grpc from '@grpc/grpc-js'; +import * as protoLoader from '@grpc/proto-loader'; + +import { ServerReflectionRequest } from '../generated/grpc/reflection/v1/ServerReflectionRequest'; +import { ServerReflectionResponse } from '../generated/grpc/reflection/v1/ServerReflectionResponse'; +import { PROTO_LOADER_OPTS } from './common/constants'; +import { ReflectionV1Implementation } from './reflection-v1'; + + +/** Analyzes a gRPC server and exposes methods to reflect on it + * + * NOTE: the files returned by this service may not match the handwritten ones 1:1. + * This is because proto-loader reorients files based on their package definition, + * combining any that have the same package. + * + * For example: if files 'a.proto' and 'b.proto' are both for the same package 'c' then + * we will always return a reference to a combined 'c.proto' instead of the 2 files. + * + * @privateRemarks as the v1 and v1alpha specs are identical, this implementation extends + * reflection-v1 and exposes it at the v1alpha package instead + */ +export class ReflectionV1AlphaImplementation extends ReflectionV1Implementation { + addToServer(server: Pick) { + const protoPath = path.join(__dirname, '../../proto/grpc/reflection/v1alpha/reflection.proto'); + const pkgDefinition = protoLoader.loadSync(protoPath, PROTO_LOADER_OPTS); + const pkg = grpc.loadPackageDefinition(pkgDefinition) as any; + + server.addService(pkg.grpc.reflection.v1alpha.ServerReflection.service, { + ServerReflectionInfo: ( + stream: grpc.ServerDuplexStream + ) => { + stream.on('end', () => stream.end()); + + stream.on('data', (message: ServerReflectionRequest) => { + stream.write(this.handleServerReflectionRequest(message)); + }); + } + }); + } +} diff --git a/packages/grpc-reflection/src/index.ts b/packages/grpc-reflection/src/index.ts new file mode 100644 index 000000000..deba7fe33 --- /dev/null +++ b/packages/grpc-reflection/src/index.ts @@ -0,0 +1 @@ +export { ReflectionService } from './service'; diff --git a/packages/grpc-reflection/src/service.ts b/packages/grpc-reflection/src/service.ts new file mode 100644 index 000000000..616d210de --- /dev/null +++ b/packages/grpc-reflection/src/service.ts @@ -0,0 +1,38 @@ +import * as grpc from '@grpc/grpc-js'; +import * as protoLoader from '@grpc/proto-loader'; + +import { ReflectionV1Implementation } from './implementations/reflection-v1'; +import { ReflectionV1AlphaImplementation } from './implementations/reflection-v1alpha'; +import { ReflectionServerOptions } from './implementations/common/interfaces'; + +/** Analyzes a gRPC package and exposes endpoints providing information about + * it according to the gRPC Server Reflection API Specification + * + * @see https://github.com/grpc/grpc/blob/master/doc/server-reflection.md + * + * @remarks + * + * in order to keep backwards compatibility as the reflection schema evolves + * this service contains implementations for each of the published versions + * + * @privateRemarks + * + * this class acts mostly as a facade to several underlying implementations. This + * allows us to add or remove support for different versions of the reflection + * schema without affecting the consumer + * + */ +export class ReflectionService { + private readonly v1: ReflectionV1Implementation; + private readonly v1Alpha: ReflectionV1AlphaImplementation; + + constructor(pkg: protoLoader.PackageDefinition, options?: ReflectionServerOptions) { + this.v1 = new ReflectionV1Implementation(pkg, options); + this.v1Alpha = new ReflectionV1AlphaImplementation(pkg, options); + } + + addToServer(server: Pick) { + this.v1.addToServer(server); + this.v1Alpha.addToServer(server); + } +} diff --git a/packages/grpc-reflection/test/test-reflection-v1-implementation.ts b/packages/grpc-reflection/test/test-reflection-v1-implementation.ts new file mode 100644 index 000000000..552160f74 --- /dev/null +++ b/packages/grpc-reflection/test/test-reflection-v1-implementation.ts @@ -0,0 +1,192 @@ +import * as assert from 'assert'; +import * as path from 'path'; +import { FileDescriptorProto, IFileDescriptorProto } from 'protobufjs/ext/descriptor'; +import * as protoLoader from '@grpc/proto-loader'; + +import { ReflectionV1Implementation } from '../src/implementations/reflection-v1'; + +describe('GrpcReflectionService', () => { + let reflectionService: ReflectionV1Implementation; + + beforeEach(async () => { + const root = protoLoader.loadSync(path.join(__dirname, '../proto/sample/sample.proto'), { + includeDirs: [path.join(__dirname, '../proto/sample/vendor')] + }); + + reflectionService = new ReflectionV1Implementation(root); + }); + + describe('listServices()', () => { + it('lists all services', () => { + const { service: services } = reflectionService.listServices('*'); + assert.equal(services.length, 2); + assert(services.find((s) => s.name === 'sample.SampleService')); + }); + + it('whitelists services properly', () => { + const root = protoLoader.loadSync(path.join(__dirname, '../proto/sample/sample.proto'), { + includeDirs: [path.join(__dirname, '../proto/sample/vendor')] + }); + + reflectionService = new ReflectionV1Implementation(root, { services: ['sample.SampleService'] }); + + const { service: services } = reflectionService.listServices('*'); + assert.equal(services.length, 1); + assert(services.find((s) => s.name === 'sample.SampleService')); + }); + }); + + describe('fileByFilename()', () => { + it('finds files with transitive dependencies', () => { + const descriptors = reflectionService + .fileByFilename('sample.proto') + .fileDescriptorProto.map(f => FileDescriptorProto.decode(f) as IFileDescriptorProto); + + const names = descriptors.map((desc) => desc.name); + assert.deepEqual( + new Set(names), + new Set(['sample.proto', 'vendor.proto', 'vendor_dependency.proto']) + ); + }); + + it('finds files with fewer transitive dependencies', () => { + const descriptors = reflectionService + .fileByFilename('vendor.proto') + .fileDescriptorProto.map(f => FileDescriptorProto.decode(f) as IFileDescriptorProto); + + const names = descriptors.map((desc) => desc.name); + assert.deepEqual(new Set(names), new Set(['vendor.proto', 'vendor_dependency.proto'])); + }); + + it('finds files with no transitive dependencies', () => { + const descriptors = reflectionService + .fileByFilename('vendor_dependency.proto') + .fileDescriptorProto.map(f => FileDescriptorProto.decode(f) as IFileDescriptorProto); + + assert.equal(descriptors.length, 1); + assert.equal(descriptors[0].name, 'vendor_dependency.proto'); + }); + + it('merges files based on package name', () => { + const descriptors = reflectionService + .fileByFilename('vendor.proto') + .fileDescriptorProto.map(f => FileDescriptorProto.decode(f) as IFileDescriptorProto); + + const names = descriptors.map((desc) => desc.name); + assert(!names.includes('common.proto')); // file merged into vendor.proto + }); + + it('errors with no file found', () => { + assert.throws( + () => reflectionService.fileByFilename('nonexistent.proto'), + 'Proto file not found', + ); + }); + }); + + describe('fileContainingSymbol()', () => { + it('finds symbols and returns transitive file dependencies', () => { + const descriptors = reflectionService + .fileContainingSymbol('sample.HelloRequest') + .fileDescriptorProto.map(f => FileDescriptorProto.decode(f) as IFileDescriptorProto); + + const names = descriptors.map((desc) => desc.name); + assert.deepEqual( + new Set(names), + new Set(['sample.proto', 'vendor.proto', 'vendor_dependency.proto']), + ); + }); + + it('finds imported message types', () => { + const descriptors = reflectionService + .fileContainingSymbol('vendor.CommonMessage') + .fileDescriptorProto.map(f => FileDescriptorProto.decode(f) as IFileDescriptorProto); + + const names = descriptors.map((desc) => desc.name); + assert.deepEqual(new Set(names), new Set(['vendor.proto', 'vendor_dependency.proto'])); + }); + + it('finds transitively imported message types', () => { + const descriptors = reflectionService + .fileContainingSymbol('vendor.dependency.DependentMessage') + .fileDescriptorProto.map(f => FileDescriptorProto.decode(f) as IFileDescriptorProto); + + assert.equal(descriptors.length, 1); + assert.equal(descriptors[0].name, 'vendor_dependency.proto'); + }); + + it('finds nested message types', () => { + const descriptors = reflectionService + .fileContainingSymbol('sample.HelloRequest.HelloNested') + .fileDescriptorProto.map(f => FileDescriptorProto.decode(f) as IFileDescriptorProto); + + const names = descriptors.map((desc) => desc.name); + assert.deepEqual( + new Set(names), + new Set(['sample.proto', 'vendor.proto', 'vendor_dependency.proto']), + ); + }); + + it('merges files based on package name', () => { + const descriptors = reflectionService + .fileContainingSymbol('vendor.CommonMessage') + .fileDescriptorProto.map(f => FileDescriptorProto.decode(f) as IFileDescriptorProto); + + const names = descriptors.map((desc) => desc.name); + assert(!names.includes('common.proto')); // file merged into vendor.proto + }); + + it('errors with no symbol found', () => { + assert.throws( + () => reflectionService.fileContainingSymbol('non.existant.symbol'), + 'Symbol not found:', + ); + }); + + it('resolves references to method types', () => { + const descriptors = reflectionService + .fileContainingSymbol('sample.SampleService.Hello2') + .fileDescriptorProto.map(f => FileDescriptorProto.decode(f) as IFileDescriptorProto); + + const names = descriptors.map((desc) => desc.name); + assert.deepEqual( + new Set(names), + new Set(['sample.proto', 'vendor.proto', 'vendor_dependency.proto']), + ); + }); + }); + + describe('fileContainingExtension()', () => { + it('finds extensions and returns transitive file dependencies', () => { + const descriptors = reflectionService + .fileContainingExtension('.vendor.CommonMessage', 101) + .fileDescriptorProto.map(f => FileDescriptorProto.decode(f) as IFileDescriptorProto); + + const names = descriptors.map((desc) => desc.name); + assert.deepEqual(new Set(names), new Set(['vendor.proto', 'vendor_dependency.proto'])); + }); + + it('errors with no symbol found', () => { + assert.throws( + () => reflectionService.fileContainingExtension('non.existant.symbol', 0), + 'Extension not found', + ); + }); + }); + + describe('allExtensionNumbersOfType()', () => { + it('finds extensions and returns transitive file dependencies', () => { + const response = reflectionService.allExtensionNumbersOfType('.vendor.CommonMessage'); + + assert.equal(response.extensionNumber.length, 1); + assert.equal(response.extensionNumber[0], 101); + }); + + it('errors with no symbol found', () => { + assert.throws( + () => reflectionService.allExtensionNumbersOfType('non.existant.symbol'), + 'Extensions not found', + ); + }); + }); +}); diff --git a/packages/grpc-reflection/test/test-utils.ts b/packages/grpc-reflection/test/test-utils.ts new file mode 100644 index 000000000..2f197652a --- /dev/null +++ b/packages/grpc-reflection/test/test-utils.ts @@ -0,0 +1,14 @@ +import * as assert from 'assert'; + +import { scope } from '../src/implementations/common/utils'; + +describe('scope', () => { + it('traverses upwards in the package scope', () => { + assert.strictEqual(scope('grpc.health.v1.HealthCheckResponse.ServiceStatus'), 'grpc.health.v1.HealthCheckResponse'); + assert.strictEqual(scope(scope(scope(scope('grpc.health.v1.HealthCheckResponse.ServiceStatus')))), 'grpc'); + }); + it('returns an empty package when at the top', () => { + assert.strictEqual(scope('Message'), ''); + assert.strictEqual(scope(''), ''); + }); +}); diff --git a/packages/grpc-reflection/tsconfig.json b/packages/grpc-reflection/tsconfig.json new file mode 100644 index 000000000..763ceda98 --- /dev/null +++ b/packages/grpc-reflection/tsconfig.json @@ -0,0 +1,29 @@ +{ + "compilerOptions": { + "allowUnreachableCode": false, + "allowUnusedLabels": false, + "declaration": true, + "forceConsistentCasingInFileNames": true, + "noEmitOnError": true, + "noFallthroughCasesInSwitch": true, + "noImplicitReturns": true, + "pretty": true, + "sourceMap": true, + "strict": true, + "lib": ["es2017"], + "outDir": "build", + "target": "es2017", + "module": "commonjs", + "resolveJsonModule": true, + "incremental": true, + "types": ["mocha"], + "noUnusedLocals": true + }, + "include": [ + "src/**/*.ts", + "test/**/*.ts" + ], + "exclude": [ + "node_modules" + ] +}