A protoc
plugin that generates Protobuf.js service implementations for Twirp.
This plugin supports the following Protobuf.js output formats:
- JSON descriptors, which rely on reflection
- Static JavaScript modules
See Protobuf.js documentation for more details about those formats.
This plugin has two parameters, explained below. Bold values are the defaults.
Name | Description | Values |
---|---|---|
pbjs_format |
Output format of the file generated by pbjs |
|
twirp_version |
Version of the Twirp protocol specification to use. |
|
The plugin generates code with import paths that expect files generated by pbjs
to be in the same directory as the plugin output file(s), with the same name as the source .proto
file.
Consider the following directory structure:
.
├── foo
│ ├── bar.proto # Source Protobuf file
│ └── bar.json # JSON descriptors that correspond to the Protobuf file, generated by Protobuf.js
└── ...
To generate the RPC client:
$ protoc --proto_path=. --twirp_javascript_output=pbjs_format=json:. ./foo/bar.proto
Result:
.
├── foo
│ ├── bar.json
│ ├── bar.proto
│ └── bar.twirp.js # Plugin output file
└── ...
A few things to note:
- Contrary to the JSON file generated by
pbjs
, the source.proto
is not directly used by the generated RPC client. This is a plugin limitation, as Protobuf.js itself supports loading of.proto
files. Support may be added at a later point. - The JSON file is only used at runtime and the plugin does not need it to generate the RPC client. That property can be taken advantage of when
protoc
output directory layout is not known in advance: first invokeprotoc-gen-twirp_javascript
, and thenpbjs
with the now known target directory.
Almost all of the information in the JSON descriptors section also applies here. .json
extension is replaced by .pb.js
.
To generate the RPC client:
$ protoc --proto_path=. --twirp_javascript_output=pbjs_format=static-module:. ./foo/bar.proto