Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
murrayirrigation opened this issue Feb 20, 2024 · 1 comment
Closed

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

murrayirrigation opened this issue Feb 20, 2024 · 1 comment

Comments

@murrayirrigation
Copy link

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
Copy link
Owner

mtth commented Feb 22, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants