Skip to content

Instantly share code, notes, and snippets.

@alejandromav
Last active April 6, 2020 15:45
Show Gist options
  • Select an option

  • Save alejandromav/78a382a7965837db1128dd42d8cb08ca to your computer and use it in GitHub Desktop.

Select an option

Save alejandromav/78a382a7965837db1128dd42d8cb08ca to your computer and use it in GitHub Desktop.
Avro Wire format
// https://docs.confluent.io/current/schema-registry/serializer-formatter.html#wire-format
const avro = require('avsc');
const schemas = {
1337: {
type: 'record',
fields: [
{name: 'name', type: 'string'}
]
}
}
function encode(schemaId, obj) {
const type = avro.Type.forSchema(schemas[schemaId]);
const magicByte = Buffer.alloc(1);
const id = toBytesInt32(1337);
const payload = type.toBuffer(obj);
return Buffer.concat([magicByte, id, payload]);
}
function decode(avroBuffer) {
const magicByte = avroBuffer.slice(0);
const id = avroBuffer.readInt32BE(1);
const payload = avroBuffer.slice(5);
const type = avro.Type.forSchema(schemas[id]);
return type.fromBuffer(payload);
}
function toBytesInt32 (num) {
const buff = Buffer.alloc(4);
buff.writeInt32BE(num)
return buff;
}
const encodedAvro = encode(1337, { name: 'fooo' });
console.log(encodedAvro);
// <Buffer 00 00 00 05 39 08 66 6f 6f 6f>
// "magic byte": 00
// Schema id (1337): 00 00 05 39
// Payload: 08 66 6f 6f 6f
console.log(decode(encodedAvro));
// Record$ { name: 'fooo' }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment