GSI - Employe Self Service Mobile
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
5.7 KiB

2 months ago
  1. # gRPC Protobuf Loader
  2. A utility package for loading `.proto` files for use with gRPC, using the latest Protobuf.js package.
  3. Please refer to [protobuf.js' documentation](https://github.com/dcodeIO/protobuf.js/blob/master/README.md)
  4. to understands its features and limitations.
  5. ## Installation
  6. ```sh
  7. npm install @grpc/proto-loader
  8. ```
  9. ## Usage
  10. ```js
  11. const protoLoader = require('@grpc/proto-loader');
  12. const grpcLibrary = require('grpc');
  13. // OR
  14. const grpcLibrary = require('@grpc/grpc-js');
  15. protoLoader.load(protoFileName, options).then(packageDefinition => {
  16. const packageObject = grpcLibrary.loadPackageDefinition(packageDefinition);
  17. });
  18. // OR
  19. const packageDefinition = protoLoader.loadSync(protoFileName, options);
  20. const packageObject = grpcLibrary.loadPackageDefinition(packageDefinition);
  21. ```
  22. The options parameter is an object that can have the following optional properties:
  23. | Field name | Valid values | Description
  24. |------------|--------------|------------
  25. | `keepCase` | `true` or `false` | Preserve field names. The default is to change them to camel case.
  26. | `longs` | `String` or `Number` | The type to use to represent `long` values. Defaults to a `Long` object type.
  27. | `enums` | `String` | The type to use to represent `enum` values. Defaults to the numeric value.
  28. | `bytes` | `Array` or `String` | The type to use to represent `bytes` values. Defaults to `Buffer`.
  29. | `defaults` | `true` or `false` | Set default values on output objects. Defaults to `false`.
  30. | `arrays` | `true` or `false` | Set empty arrays for missing array values even if `defaults` is `false` Defaults to `false`.
  31. | `objects` | `true` or `false` | Set empty objects for missing object values even if `defaults` is `false` Defaults to `false`.
  32. | `oneofs` | `true` or `false` | Set virtual oneof properties to the present field's name. Defaults to `false`.
  33. | `json` | `true` or `false` | Represent `Infinity` and `NaN` as strings in `float` fields, and automatically decode `google.protobuf.Any` values. Defaults to `false`
  34. | `includeDirs` | An array of strings | A list of search paths for imported `.proto` files.
  35. The following options object closely approximates the existing behavior of `grpc.load`:
  36. ```js
  37. const options = {
  38. keepCase: true,
  39. longs: String,
  40. enums: String,
  41. defaults: true,
  42. oneofs: true
  43. }
  44. ```
  45. ## Generating TypeScript types
  46. The `proto-loader-gen-types` script distributed with this package can be used to generate TypeScript type information for the objects loaded at runtime. More information about how to use it can be found in [the *@grpc/proto-loader TypeScript Type Generator CLI Tool* proposal document](https://github.com/grpc/proposal/blob/master/L70-node-proto-loader-type-generator.md). The arguments mostly match the `load` function's options; the full usage information is as follows:
  47. ```console
  48. proto-loader-gen-types.js [options] filenames...
  49. Options:
  50. --help Show help [boolean]
  51. --version Show version number [boolean]
  52. --keepCase Preserve the case of field names
  53. [boolean] [default: false]
  54. --longs The type that should be used to output 64 bit integer
  55. values. Can be String, Number[string] [default: "Long"]
  56. --enums The type that should be used to output enum fields. Can
  57. be String [string] [default: "number"]
  58. --bytes The type that should be used to output bytes fields.
  59. Can be String, Array [string] [default: "Buffer"]
  60. --defaults Output default values for omitted fields
  61. [boolean] [default: false]
  62. --arrays Output default values for omitted repeated fields even
  63. if --defaults is not set [boolean] [default: false]
  64. --objects Output default values for omitted message fields even
  65. if --defaults is not set [boolean] [default: false]
  66. --oneofs Output virtual oneof fields set to the present field's
  67. name [boolean] [default: false]
  68. --json Represent Infinity and NaN as strings in float fields.
  69. Also decode google.protobuf.Any automatically
  70. [boolean] [default: false]
  71. --includeComments Generate doc comments from comments in the original
  72. files [boolean] [default: false]
  73. -I, --includeDirs Directories to search for included files [array]
  74. -O, --outDir Directory in which to output files [string] [required]
  75. --grpcLib The gRPC implementation library that these types will
  76. be used with [string] [required]
  77. ```
  78. ### Example Usage
  79. Generate the types:
  80. ```sh
  81. $(npm bin)/proto-loader-gen-types --longs=String --enums=String --defaults --oneofs --grpcLib=@grpc/grpc-js --outDir=proto/ proto/*.proto
  82. ```
  83. Consume the types:
  84. ```ts
  85. import * as grpc from '@grpc/grpc-js';
  86. import * as protoLoader from '@grpc/proto-loader';
  87. import { ProtoGrpcType } from './proto/example';
  88. import { ExampleHandlers } from './proto/example_package/Example';
  89. const exampleServer: ExampleHandlers = {
  90. // server handlers implementation...
  91. };
  92. const packageDefinition = protoLoader.loadSync('./proto/example.proto');
  93. const proto = (grpc.loadPackageDefinition(
  94. packageDefinition
  95. ) as unknown) as ProtoGrpcType;
  96. const server = new grpc.Server();
  97. server.addService(proto.example_package.Example.service, exampleServer);
  98. ```