ProtobufJS repeated bytes field not encoding Uint8Array properly

1.6k Views Asked by At

I have a protobuf message defined as:

message examplemessage
{
    string field1 = 1;
    string field2 = 2;
    repeated bytes field3 = 3;
}

I load my protobuf with:

protobuf.load(path).then(root => {
    // global for example
    examplemessage = root.lookupType("test.examplemessage");
    resolve(); 
});

I create a protobuf message object with

let createdMessage = examplemessage.create({
    field1: "test1",
    field2: "test2",
    field3: new Uint8Array([0,0,0,33])
});

I then encode it

let encoded = examplemessage.encode(createdMessage).finish();

I then decode and expect

{
    field1: "test1",
    field2: "test2",
    field3: Uint8Array(4) [0, 0, 0, 33]
}

Instead I see

{
    field1: "test1",
    field2: "test2",
    [Uint8Array(0), Uint8Array(0), Uint8Array(0), Uint8Array(0)]
}

I then changed my protobuf loading to JSON

const root = protobuf.Root.fromJSON(json);

This works as expected with no other changes.

Am I doing something wrong or is this a bug?

Thanks

Protoubuf Version: 6.8.6

Browser: Chrome

JSFiddle example with working JSON loading: https://jsfiddle.net/740snmu6/12/

1

There are 1 best solutions below

0
On

repeated bytes means a Array of bytes (as you might already know, the corresponding type of bytes is Uint8Array or Array in JavaScript), so to make it work, you should create the message in this way:

{
    field1: "test1",
    field2: "test2",
    field3: [new Uint8Array([0, 0, 0, 33]), new Uint8Array([0, 0, 0, 33]), new Uint8Array([0, 0, 0, 33])]
}