@fastify/accepts-serializer

中文文档
6.0.6发布于 2026/10/09MITTypeScriptCommonJS构建来源已验证近 30 天下载 1.7万

Serializer according to the accept header

原始使用文档
文档目录6 节
  1. Install
  2. Compatibility
  3. Usage
  4. Options
  5. Behavior
  6. License

@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
defaultstring—MIME 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.

版本与发布记录

@fastify/accepts-serializer 共发布 15 个版本,首次发布于 2022/04/27;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
6.0.6latest2026/10/091 个月前
6.0.52026/08/123 个月前
6.0.42026/05/046 个月前
6.0.32025/09/281.1 年前
6.0.22024/12/141.9 年前
6.0.12024/09/222 年前

其他发布通道:next → 6.0.0。

关于 @fastify/accepts-serializer 的常见问题

@fastify/accepts-serializer 是什么?

Serializer according to the accept header

如何安装 @fastify/accepts-serializer?

在项目目录运行 npm install @fastify/accepts-serializer,或使用 pnpm add / yarn add / bun add。

@fastify/accepts-serializer 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@fastify/accepts-serializer 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@fastify/accepts-serializer 可以商用吗?

@fastify/accepts-serializer 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@fastify/accepts-serializer 的下载量有多少?

根据 npm 官方下载统计,@fastify/accepts-serializer 在 2026/10/15 – 2026/11/13 这 30 天内被下载 17,019 次,日均约 567 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fastify/accepts-serializer 还在维护吗?

最新版本 6.0.6 发布于 2026/10/09(1 个月前);至今共发布 15 个版本,其中过去 12 个月发布了 3 个;首次发布于 2022/04/27。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。