0

Am trying to send a proto3 message in node js and encoding into byte array. however according to protobuf js the message is encoded to buffer (and not byte array) instead in node js. Is there a way of encoding this message to byte array as well?

var message = SomeMessage.create(payload);
var buffer = SomeMessage.encode(messager).finish();
console.log(buffer) // returns <Buffer>
Stanley
  • 2,798
  • 5
  • 22
  • 44
  • 1
    Buffers _are_ byte arrays (well, [_"Uint8Array instances"_](https://nodejs.org/api/buffer.html#buffer_buffers_and_typedarray)). Do you want a plain JS array with the numerical values of the bytes? – robertklep Jul 15 '17 at 09:08
  • Would it matter if the grpc is sent to another program that is written in other languages? will the other program be able to read it as Uint8Array? – Stanley Jul 17 '17 at 13:32
  • 1
    The buffer contains the raw (encoded) data, so if you send it correctly (as binary, not as string), the "other side" should have no problems reading it. – robertklep Jul 17 '17 at 14:12

1 Answers1

0

If you want an array with the numerical values of the bytes you can try:

const bufferedData = Buffer.from(JSON.stringify(message));
bogdanc
  • 68
  • 2
  • 7