protocolbuffers / protobuf-javascript

BSD 3-Clause "New" or "Revised" License
330 stars 66 forks source link

JS serialize binary error "Unknown base64 encoding at char: @" #172

Closed dnovak270 closed 1 year ago

dnovak270 commented 1 year ago

Create simple proto file:

syntax = "proto3";

message Test {
  bytes test_bytes_field = 1;
}

Generate code using buf:

# buf.gen.yaml
version: v1
plugins:
  - name: grpc-web
    out: ./src/grpc
    opt: import_style=commonjs+dts,mode=grpcwebtext
  - name: js
    out: ./src/grpc
    opt: import_style=commonjs,binary

Try setting a non base64 char (e.g. @) to test_bytes_field and use serializeBinary method on generated jspb.Message:

try {
  const test = new Test();
  test.setTestBytesField('@');
  test.serializeBinary();
} catch (e) {
  console.error({ e });
}

Results in an error "Unknown base64 encoding at char: @"

dibenede commented 1 year ago

This is working as intended: a string argument is intended to be used for base64 strings. Unfortunately, there's nothing in the setter API to signal as much, so the error only pops up during serialization:

https://github.com/protocolbuffers/protobuf-javascript/blob/566f35982a0a04718855f650e4496c2add001988/binary/utils.js#L1093-L1120

Depending on your use case, you may just want to use a Uint8Array instead.