node.jsprotocol-buffersprotobufjs

Protobuf Can't decode buffer


protobuf.js version: ^6.2.1

buffer

083515604859581840220a0d6048595815bdf426422a0a0d7047595815bdf426

code:

protobuf.load("PulseFlagData.proto", function(err, root) {
                            if (err) {
                                callback(err);
                            }
                            var pulse = root.lookup("PulseFlagData");

                            callback(null, pulse.decode(parsed_data.payload)); 
 });   

.proto file:

syntax = "proto3";

message PulseFlagSample {
    required fixed32 timestamp = 1;
    required uint32 input = 2;
    required bool flag = 3;
}

message PulseFlagData {
    required uint32 config_version = 1;
    required fixed32 device_time = 2;
    required PulseFlagSample current = 3;
    optional PulseFlagSample last_hour = 4;
}

error:

/home/deck/NetBeansProjects/lora/node_modules/protobufjs/src/reader.js:390 throw indexOutOfRange(this, length); ^

RangeError: index out of range: 23 + 10 > 32 at RangeError (native) at indexOutOfRange (/home/deck/NetBeansProjects/lora/node_modules/protobufjs/src/reader.js:13:12) at BufferReader.skip (/home/deck/NetBeansProjects/lora/node_modules/protobufjs/src/reader.js:390:19) at BufferReader.ReaderPrototype.skipType (/home/deck/NetBeansProjects/lora/node_modules/protobufjs/src/reader.js:410:18) at Type._PulseFlagSample$decode [as decode] (eval at eof (/home/deck/NetBeansProjects/lora/node_modules/protobufjs/node_modules/@protobufjs/codegen/index.js:102:25), :19:7) at Type.decode_setup [as decode] (/home/deck/NetBeansProjects/lora/node_modules/protobufjs/src/type.js:370:7) at Type._PulseFlagData$decode [as decode] (eval at eof (/home/deck/NetBeansProjects/lora/node_modules/protobufjs/node_modules/@protobufjs/codegen/index.js:102:25), :16:27) at Type.decode_setup [as decode] (/home/deck/NetBeansProjects/lora/node_modules/protobufjs/src/type.js:370:7) at /home/deck/NetBeansProjects/lora/server.js:42:50 at finish (/home/deck/NetBeansProjects/lora/node_modules/protobufjs/src/root.js:84:9)


Solution

  • Your input is not a valid protocol buffer.

    1. It appears to be cut off mid-message. The last top-level field is a length-delimited field of 10 bytes, but the message ends after only 9 bytes. Hence, there is at least one byte missing. There could be more bytes missing, if additional fields were supposed to appear.
    2. Even if the message did not cut off prematurely, it does not appear to match the type you are parsing as. The message appears to have fields:

      uint32 a = 1;
      fixed32 b = 2;
      int32 c = 3;
      SomeMessageType d = 4;
      SomeMessageType e = 5;
      

      These field declarations do not line up with the PulseFlagData type you gave.

    Here's the raw data broken down by field:

    08 35                       // 1: [u]int32, = 53
    15 60485958                 // 2: fixed32,  = 1615354200
    18 40                       // 3: [u]int32, = 64
    22 0a 0d6048595815bdf42642  // 4: sub-message, length 10
      0d 60485958               //   1: fixed32, = 1615354200
      15 bdf42642               //   2: fixed32, = 3186894402
    2a 0a 0d7047595815bdf426    // 5: sub-message, length 10, incomplete
      0d 70475958               //   1: fixed32, = 1883724120
      15 bdf426                 //   2: fixed32, incomplete