protocolbuffers / protobuf-javascript

BSD 3-Clause "New" or "Revised" License
351 stars 67 forks source link

Confusion about protoc for Javascript #168

Closed EricBuist closed 1 year ago

EricBuist commented 1 year ago

Hi, I cannot figure out how to make use of the _pb.js files. Documentation at https://protobuf.dev/programming-guides/proto3/ doesn't include anything about Javascript or Node. Documentation on grpc.io refers to a protobuf loader that directly loads the .proto files. Trying to require a generated _pb.js file gives an object providing no methods to create a message from scratch, just to deserialize from binary, not even from JSON. This makes protoc pretty much useless for Javascript.

dibenede commented 1 year ago

https://github.com/protocolbuffers/protobuf-javascript/blob/main/docs/index.md#commonjs-imports might be a better starting point.

Assuming a message like:

message MyMessage {
  optional int32 foo = 1;
}

You should be able to construct from scratch:

const messages = require('./messages_pb');
const message = new messages.MyMessage();

message.setFoo(100);
console.log(message.getFoo());

const serialized = message.serializeBinary();
...

I'm not familiar with grpc specific aspects, however.