node_protoc2

Compile int64 to string when generating static code for protobuf in JavaScript

Usage no npm install needed!

<script type="module">
  import nodeProtoc2 from 'https://cdn.skypack.dev/node_protoc2';
</script>

README

node_protoc2

Wrap grpc-tools with additional ability that compile protobuf 64-int types to string automatically

Why

The official table that shows the type specified in the .proto file and the corresponding type in target program language doesn't include the JavaScript example. So here is:

.proto Type JavaScript
double, float, int32,uint32,sint32,fixed32,sfixed32 number
int64,uint64,sint64,fixed64,sfixed64 number (maybe overflow)
bool boolean
string string
byte Uint8Array

By default the official compiler protoc will compile all protobuf 64-int types into the JavaScript number type that maybe would cause overflow.

To work around this, you need to adding jstype annotation for every 64-int field because protoc doesn't provide any global compile option. Somehow this is annoying when the .proto files are not maintained by other teams.

So, I wrote this tool to make it easy.

Usage

You can generate static codes in JavaScript from .proto files through the following command:

npx node_protoc2 --out_dir output_folder foo.proto

All proto 64-int types would be compiled into JavaScript string type