Skip to main content
Fastify is built for speed. ZON is built for speed. They are a perfect match.

Content Type Parser

Fastify allows you to register custom content type parsers. We can register one for application/x-zon that performs the zero-copy wrap.
server.js
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);
}