Skip to content

Instantly share code, notes, and snippets.

@JaapRood
Last active July 9, 2019 15:00
Show Gist options
  • Save JaapRood/9089eb2923ceb625815ff5251838d574 to your computer and use it in GitHub Desktop.
Save JaapRood/9089eb2923ceb625815ff5251838d574 to your computer and use it in GitHub Desktop.
Encoding an avsc message with a Confluent spec header
const magicByte = 0
// Optimized to try and not have to re-allocate memory through Buffer.concat.
// We allocate 1k to begin with, hoping that fits the message
// and we don't have to relocate again. Otherwise, tough luck.
function toBuffer(value, type, schemaId, length = 1024) {
const buf = Buffer.allocUnsafe(length) // unsafe, might contain old / sensitive data
buf.writeInt8(magicByte)
buf.writeInt32BE(schemaId, 1)
const pos = type.encode(value, buf, 5)
if (pos < 0) {
// the buffer wasn't big enough, we'll have to try again
return toBuffer(value, type, schemaId, length - pos)
} else {
return buf.slice(0, pos)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment