@fastify/fast-json-stringify-compiler

中文文档
5.2.0发布于 2026/11/12MITTypeScriptCommonJS构建来源已验证近 30 天下载 7931.2万

Build and manage the fast-json-stringify instances for the fastify framework

原始使用文档
文档目录6 节
  1. Versions
  2. fast-json-stringify Configuration
  3. Usage
  4. fast-json-stringify Standalone
  5. How it works
  6. License

@fastify/fast-json-stringify-compiler

CI NPM version neostandard javascript style

Build and manage the fast-json-stringify instances for the Fastify framework. This package is responsible for compiling the application's response JSON schemas into optimized functions to speed up the response time.

Versions

@fastify/fast-json-stringify-compilerfast-json-stringifySupported fastify
v5.xv6-7.x^5.x
v4.xv5.x^5.x
v3.xv4.x^4.x
v2.xv3.x^4.x
v1.xv3.x^3.x

fast-json-stringify Configuration

The fast-json-stringify configuration is the default one. You can check the default settings in the fast-json-stringify option documentation.

You can also override the default configuration by passing the serializerOpts configuration to the Fastify instance.

objectMechanism: 'native'

[email protected] added the objectMechanism option. With 'native', the serializer projects the payload into schema-shaped objects and delegates to JSON.stringify, so on Node.js 25+ V8 stays on its fast path while the response schema is still applied (undeclared properties stripped, coercions and formats as usual). The output is identical to the default mechanism, and the option is a no-op on Node.js < 25.

const app = fastify({
  serializerOpts: {
    objectMechanism: 'native'
  }
})

Usage

This module is already used as default by Fastify. If you need to provide to your server instance a different version, refer to the official doc.

fast-json-stringify Standalone

[email protected] introduces the standalone feature that lets you pre-compile your schemas and use them in your application for a faster startup.

To use this feature, you must be aware of the following:

  1. You must generate and save the application's compiled schemas.
  2. Read the compiled schemas from the file and provide them back to your Fastify application.

Generate and save the compiled schemas

Fastify helps you to generate the serialization schemas functions and it is your choice to save them where you want. To accomplish this, you must use a new compiler: @fastify/fast-json-stringify-compiler/standalone.

You must provide 2 parameters to this compiler:

  • readMode: false: a boolean to indicate that you want to generate the schemas functions string.
  • storeFunction" a sync function that must store the source code of the schemas functions. You may provide an async function too, but you must manage errors.

When readMode: false, the compiler is meant to be used in development ONLY.

const { StandaloneSerializer } = require('@fastify/fast-json-stringify-compiler')

const factory = StandaloneSerializer({
  readMode: false,
  storeFunction (routeOpts, schemaSerializationCode) {
    // routeOpts is like: { schema, method, url, httpStatus }
    // schemaSerializationCode is a string source code that is the compiled schema function
    const fileName = generateFileName(routeOpts)
    fs.writeFileSync(path.join(__dirname, fileName), schemaSerializationCode)
  }
})

const app = fastify({
  jsonShorthand: false,
  schemaController: {
    compilersFactory: {
      buildSerializer: factory
    }
  }
})

// ... add all your routes with schemas ...

app.ready().then(() => {
  // at this stage all your schemas are compiled and stored in the file system
  // now it is important to turn off the readMode
})

Read the compiled schemas functions

At this stage, you should have a file for every route's schema. To use them, you must use the @fastify/fast-json-stringify-compiler/standalone with the parameters:

  • readMode: true: a boolean to indicate that you want to read and use the schemas functions string.
  • restoreFunction" a sync function that must return a function to serialize the route's payload.

Important keep away before you continue reading the documentation:

  • when you use the readMode: true, the application schemas are not compiled (they are ignored). So, if you change your schemas, you must recompile them!
  • as you can see, you must relate the route's schema to the file name using the routeOpts object. You may use the routeOpts.schema.$id field to do so, it is up to you to define a unique schema identifier.
const { StandaloneSerializer } = require('@fastify/fast-json-stringify-compiler')

const factory = StandaloneSerializer({
  readMode: true,
  restoreFunction (routeOpts) {
    // routeOpts is like: { schema, method, url, httpStatus }
    const fileName = generateFileName(routeOpts)
    return require(path.join(__dirname, fileName))
  }
})

const app = fastify({
  jsonShorthand: false,
  schemaController: {
    compilersFactory: {
      buildSerializer: factory
    }
  }
})

// ... add all your routes with schemas as before...

app.listen({ port: 3000 })

How it works

This module provides a factory function to produce Serializer Compilers functions.

License

Licensed under MIT.

版本与发布记录

@fastify/fast-json-stringify-compiler 共发布 16 个版本,首次发布于 2022/01/05;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
5.2.0latest2026/11/121 天前
5.1.12026/10/101 个月前
5.1.02026/07/054 个月前
5.0.32025/04/081.6 年前
5.0.22025/01/051.9 年前
5.0.12024/09/212 年前

其他发布通道:next → 5.0.0。

关于 @fastify/fast-json-stringify-compiler 的常见问题

@fastify/fast-json-stringify-compiler 是什么?

Build and manage the fast-json-stringify instances for the fastify framework

如何安装 @fastify/fast-json-stringify-compiler?

在项目目录运行 npm install @fastify/fast-json-stringify-compiler,或使用 pnpm add / yarn add / bun add。

@fastify/fast-json-stringify-compiler 支持 TypeScript 吗?

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

@fastify/fast-json-stringify-compiler 支持 ESM 还是 CommonJS?

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

@fastify/fast-json-stringify-compiler 可以商用吗?

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

@fastify/fast-json-stringify-compiler 的下载量有多少?

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

@fastify/fast-json-stringify-compiler 还在维护吗?

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