Last active
July 9, 2019 15:00
-
-
Save JaapRood/9089eb2923ceb625815ff5251838d574 to your computer and use it in GitHub Desktop.
Encoding an avsc message with a Confluent spec header
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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