const fastify = require('fastify')({ logger: true });
const { ZonReader } = require('@zon-lib/zon');
// 1. Register the Parser
fastify.addContentTypeParser('application/x-zon', { parseAs: 'buffer' }, (req, body, done) => {
try {
// 'body' is already a Buffer here.
// We wrapped it instantly.
const reader = new ZonReader(body);
done(null, reader);
} catch (err) {
done(err);
}
});
// 2. Use it in a route
fastify.post('/api/ingest', (req, reply) => {
// req.body IS the ZonReader instance
const reader = req.body;
const root = reader.rootOffset;
return {
accepted: true,
event: reader.readString(root)
};
});
try {
fastify.listen({ port: 3000 });
} catch (err) {
fastify.log.error(err);
process.exit(1);
}