fastify/fastify-accepts-serializer

Serializer according to the accept header

29

stars

200

commits

JavaScript

primary language

Aug 12, 2026

updated

npmjs.com/package/@fastify/accepts-serializer
fastify
fastify-plugin

README

@fastify/accepts-serializer

CI npm version neostandard javascript style

Serialize according to the Accept header.

Install

npm i @fastify/accepts-serializer

Compatibility

Plugin versionFastify version
>=6.x^5.x
^5.x^4.x
>=3.x <5.x^3.x
^2.x^2.x
^1.x^1.x

Please note that if a Fastify version is out of support, then so are the corresponding versions of this plugin in the table above. See Fastify's LTS policy for more details.

Usage


const protobuf = require('protobufjs')
const YAML = require('yamljs')
const msgpack = require('msgpack5')()

const root = protobuf.loadSync('test/awesome.proto')
const AwesomeMessage = root.lookupType('awesomepackage.AwesomeMessage')

const fastify = require('fastify')()

// Global serializers
fastify.register(require('@fastify/accepts-serializer'), {
  serializers: [
    {
      regex: /^application\/yaml$/,
      serializer: body => YAML.stringify(body)
    },
    {
      regex: /^application\/x-msgpack$/,
      serializer: body => msgpack.encode(body)
    }
  ],
  default: 'application/yaml', // MIME type used if Accept header does not match anything
  cacheSize: 100               // max number of Accept header combinations to cache (default: 100)
})

// Per-router serializers
const config = {
  serializers: [
    {
      regex: /^application\/x-protobuf$/,
      serializer: body => AwesomeMessage.encode(AwesomeMessage.create(body)).finish()
    }
  ]
}

fastify.get('/request', { config }, function (req, reply) {
  reply.send({pippo: 'pluto'})
})

Options

OptionTypeDefaultDescription
serializersArray[]List of serializer definitions, each with a regex and a serializer function
defaultstringMIME type to use when no serializer matches the Accept header. If omitted, unmatched requests receive a 406 response
cacheSizenumber100Maximum number of distinct Accept header combinations to cache. Entries are evicted in LRU order once the limit is reached

Behavior

For each route, a SerializerManager is defined, which has both per-route and global serializer definitions.

The MIME type application/json is always handled by fastify if no serializer is registered for that MIME type.

If no default key is specified in configuration, all requests with an unknown Accept header will be replied to with a 406 response (a boom error is used).

Serializer selection results are cached by Accept header value using an LRU cache bounded by cacheSize. This prevents unbounded memory growth from attacker-controlled Accept header variants.

License

Licensed under MIT.

Contributors

dependabot[bot]

49 commits

Fdawgs

49 commits

cemremengu

31 commits

allevo

16 commits

fastify/fastify-accepts-serializer

Serializer according to the accept header

29

stars

200

commits

JavaScript

primary language

Aug 12, 2026

updated

npmjs.com/package/@fastify/accepts-serializer
fastify
fastify-plugin

README

@fastify/accepts-serializer

CI npm version neostandard javascript style

Serialize according to the Accept header.

Install

npm i @fastify/accepts-serializer

Compatibility

Plugin versionFastify version
>=6.x^5.x
^5.x^4.x
>=3.x <5.x^3.x
^2.x^2.x
^1.x^1.x

Please note that if a Fastify version is out of support, then so are the corresponding versions of this plugin in the table above. See Fastify's LTS policy for more details.

Usage


const protobuf = require('protobufjs')
const YAML = require('yamljs')
const msgpack = require('msgpack5')()

const root = protobuf.loadSync('test/awesome.proto')
const AwesomeMessage = root.lookupType('awesomepackage.AwesomeMessage')

const fastify = require('fastify')()

// Global serializers
fastify.register(require('@fastify/accepts-serializer'), {
  serializers: [
    {
      regex: /^application\/yaml$/,
      serializer: body => YAML.stringify(body)
    },
    {
      regex: /^application\/x-msgpack$/,
      serializer: body => msgpack.encode(body)
    }
  ],
  default: 'application/yaml', // MIME type used if Accept header does not match anything
  cacheSize: 100               // max number of Accept header combinations to cache (default: 100)
})

// Per-router serializers
const config = {
  serializers: [
    {
      regex: /^application\/x-protobuf$/,
      serializer: body => AwesomeMessage.encode(AwesomeMessage.create(body)).finish()
    }
  ]
}

fastify.get('/request', { config }, function (req, reply) {
  reply.send({pippo: 'pluto'})
})

Options

OptionTypeDefaultDescription
serializersArray[]List of serializer definitions, each with a regex and a serializer function
defaultstringMIME type to use when no serializer matches the Accept header. If omitted, unmatched requests receive a 406 response
cacheSizenumber100Maximum number of distinct Accept header combinations to cache. Entries are evicted in LRU order once the limit is reached

Behavior

For each route, a SerializerManager is defined, which has both per-route and global serializer definitions.

The MIME type application/json is always handled by fastify if no serializer is registered for that MIME type.

If no default key is specified in configuration, all requests with an unknown Accept header will be replied to with a 406 response (a boom error is used).

Serializer selection results are cached by Accept header value using an LRU cache bounded by cacheSize. This prevents unbounded memory growth from attacker-controlled Accept header variants.

License

Licensed under MIT.

Contributors

dependabot[bot]

49 commits

Fdawgs

49 commits

cemremengu

31 commits

allevo

16 commits

Languages

JavaScript

93.3%

TypeScript

6.7%