mtth / avsc

Avro for JavaScript :zap:
MIT License
1.27k stars 147 forks source link

Doesn't handle trailing 0s from buffer. #450

Closed murrayirrigation closed 6 months ago

murrayirrigation commented 6 months ago

I have a simple key value schema

const keySchemaJson = JSON.parse(fs.readFileSync('key.json', 'utf8'));
const valueSchemaJson = JSON.parse(fs.readFileSync('values.json', 'utf8'));

const keySchema = avro.Type.forSchema(keySchemaJson);
const valueSchema = avro.Type.forSchema(valueSchemaJson);

When I attempt to decode:


await consumer.run({
    eachMessage: async ({ topic, partition, message }) => {
      const key = keySchema.fromBuffer(message.key);
      const value = valueSchema.fromBuffer(message.value);

I get get a buffer in the messages 4096 long full of 0s after the data. If I don't trim it the fromBuffer throws an error about trailing 0s. ,"stack":"Error: trailing data\n I can remove trailing 0s and it works, I've verified encoding and decoding in python. I'm not super happy removing trailing 0s because what if the last encoded utf8 is validly 0? Should the keySchema/valueSchema not just provide a length or something?

I feel like this isn't expected behaviour?

mtth commented 6 months ago

Hi @murrayirrigation. This error is avsc protecting you from incorrect data: toBuffer expects its input buffer to contain exactly one value's encoding. If additional data is expected in the buffer, you can replace it with decode:

const {value, offset} = type.decode(buf);

value will contain the decoded value, and offset the start of the trailing data. You may for example want to check that buf only contains zero after it.