Douglas dos Santos
Douglas dos Santos

Reputation: 121

Avsc (avro implementation in Javascript) not being able to use records that use the "bytes" type field

I am using a lib https://github.com/mtth/avsc to work with avro em nodeJs but i have some complications

Has anyone here suffered from the problem of not being able to use records that use the "bytes" type field? I've already tried passing the information in byte format to the registry in many different ways (Uint8Array, Buffer, TextEncoder) but all without success

npm install avsc

const avro = require('avsc');

const type = avro.Type.forSchema({
    type: 'record',
    name: 'Pet',
    fields: [
        { name: 'name', type: 'string' },
        { name: 'payload', type: 'bytes' },
    ]
});

const AVRO_RECORD = {
    name: 'Douglas',
    payload: new TextEncoder().encode('This is a string')
}

try {
    const buf = type.toBuffer(AVRO_RECORD); // Encoded buffer.
    const val = type.fromBuffer(buf); // AVRO_RECORD { name: 'Douglas', payload: .....}
    console.log('VALUE', val)
} catch (e) {
    console.error(e) //"Error: invalid 'bytes': {'0':84,'1':104,'2':105,'3':115,'4':32,'5':105,'6':115,'7':32,'8':97,'9':32,'10':115,'11':116,'12':114,'13':105,'14':110,'15':103}

}

CodePen

I've already tried passing the information in byte format to the registry in many different ways (Uint8Array, Buffer, TextEncoder) but all without success

Upvotes: 0

Views: 250

Answers (1)

J. Caputo
J. Caputo

Reputation: 21

Works for me when I use Buffer.from(<string>, <encoding>), like so:

const AVRO_RECORD = {
    name: 'Douglas',
    payload: Buffer.from('This is a string', 'utf-8')
}

try {
    const buf = type.toBuffer(AVRO_RECORD); // Encoded buffer.
    const val = type.fromBuffer(buf); // AVRO_RECORD { name: 'Douglas', payload: .....}
    console.log('VALUE', val)
    console.log( 'Decoded payload', val.payload.toString('utf-8') )
} catch (e) {
    console.error(e) //"Error: invalid 'bytes': {'0':84,'1':104,'2':105,'3':115,'4':32,'5':105,'6':115,'7':32,'8':97,'9':32,'10':115,'11':116,'12':114,'13':105,'14':110,'15':103}

}

Upvotes: 0

Related Questions