RangeError: Illegal offset: 0 <= 49 (+8) <= 52 in NodeJS while decoding
Hi, I am getting following errors when I try decode protobuf message in NodeJS. I am not sure what is causing this because the same message and code works in python.
,/u/choudhab/node_modules/protobufjs/dist/protobuf.js:2938 throw(e); ^
RangeError: Illegal offset: 0 <= 49 (+8) <= 52 at RangeError (native) at module.exports.ByteBufferPrototype.readFloat64 (/u/choudhab/node_modules/protobufjs/node_modules/bytebuffer/dist/bytebuffer-node.js:1403:23) at ProtoBuf.Reflect.ElementPrototype.decode (/u/choudhab/node_modules/protobufjs/dist/protobuf.js:2165:35) at ProtoBuf.Reflect.FieldPrototype.decode (/u/choudhab/node_modules/protobufjs/dist/protobuf.js:3727:33) at ProtoBuf.Reflect.MessagePrototype.decode (/u/choudhab/node_modules/protobufjs/dist/protobuf.js:3238:45) at ProtoBuf.Reflect.ElementPrototype.decode (/u/choudhab/node_modules/protobufjs/dist/protobuf.js:2185:46) at ProtoBuf.Reflect.FieldPrototype.decode (/u/choudhab/node_modules/protobufjs/dist/protobuf.js:3727:33) at ProtoBuf.Reflect.MessagePrototype.decode (/u/choudhab/node_modules/protobufjs/dist/protobuf.js:3233:48) at Function.Message.decode (/u/choudhab/node_modules/protobufjs/dist/protobuf.js:2933:37) at IncomingFrame.read (/u/choudhab/activemq/ActiveMQJavascriptAPIExamples/stompit-protobuf-consumer.js:145:39)
Usually happens if the buffer is truncated or otherwise corrupted. Here, the library tried to read 8 bytes at offset 49, but the buffer had only 52 bytes.
Thanks for the tip. I was able to find the issue.
Which was the solution? i'm having exactly the same problem and i'm not being able to fix it, happens with big amounts of data in a repeat string.
How are you reading the binary data? If the data is big, you have to collect all the chunks in a big buffer and then read it. Something like below: These links helped me:
reading binary data in node consuming from node readable streams
// List of Buffer objects
var buffers = [];
// get binary data in buffer from message
var read = function() {
var buf;
while(null !== (buf = message.read())) {
// Append Buffer object
buffers.push(buf);
}
};
message.on('readable', read);
message.on('end', function(){
// Make one large buffer out of buffer list objects
var buffer = Buffer.concat(buffers)
var msg = proto_message_data.decode(buffer);
// console.log("Received message: " + JSON.stringify(msg));
// Dummy computation
var map = msg['dataEntry'][0];
map['value'] = 12;
});
hey choudhary001, thank you bro!, finally realize what the problem is, this will probably help a lot more people around because is not that much info out there.