@fastify/fast-json-stringify-compiler

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

构建和管理用于加固框架的快速加固实例

中文使用文档
文档目录6 节
  1. 版本数
  2. 快速字符串配置
  3. 使用量
  4. 快速 Json 字符串独立
  5. 如何运作
  6. 许可证

@ fastify/ fast-json-stringify-compilier (中文(简体) )

CI NPM version neostandard javascript style

建立和管理 fast-json-stringify 快速化框架实例. 这个软件包负责编译应用程序的 response JSON schemas进入优化功能,以加快响应时间.

版本数

@fastify/fast-json-stringify-compilerfast-json-stringify支助 fastify
页:1页:1^ 5.x (韩语)
v4.x (英语)页:1^ 5.x (韩语)
ưμ㼯av4.x (英语)^ 4.x 键
v2.x (英语)ưμ㼯a^ 4.x 键
v1.x (韩语)ưμ㼯a^3.x

快速字符串配置

这个 fast-json-stringify 配置是默认配置 。 您可以检查默认设置 fast-json-stringify 选项 文档.

您也可以通过传出 serializerOpts 配置到快捷实例.

objectMechanism: 'native'

[email protected] 添加 objectMechanism 选项. 与 'native',序列器将有效载荷投射到有计划形状的物体和代表 JSON.stringify,因此在Node.js 25+ V8上继续走快路,而反应方案仍在应用(未宣布属性剥离,胁迫和格式如常). 输出与默认机制相同,选项是Node.js < 25.

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

使用量

此模块已被 Fastify 用作默认值 . 如果您需要向您的服务器实例提供不同的版本,请参考 官方医生。 。 。 .

快速 Json 字符串独立

[email protected] 介绍 独立特性 让你预先编译你的计划 并用在你的应用程序中 以更快的启动.

要使用此特性,您必须了解以下内容:

  1. 您必须生成并保存应用程序的编译计划 .
  2. 从文件中读取所编译的策略, 并将其反馈到您的 Fastify 应用程序 .

生成并保存编译的策略

Fastify 帮助您生成序列化的 schemas 函数, 您选择将其保存到您想要的地方 . 要完成此任务, 您必须使用新编译器 : @fastify/fast-json-stringify-compiler/standalone。 。 。 .

您必须为此编译器提供 2 个参数 :

  • readMode: false: 一个布尔来表示您想要生成策略函数字符串.
  • storeFunction" 一个同步函数,必须存储 schemas 函数的源代码. 您也可以提供同步函数, 但是您必须管理错误 .

何时 readMode: false, (中文(简体) ), 仅用于开发的编译器。 。 。 .

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
})

读取汇编的策略函数

在这个阶段,你应该有一个文件 每个路线的策略. 要使用它们,必须使用 @fastify/fast-json-stringify-compiler/standalone 参数:

  • readMode: true: 一个布尔来表示您想要读取和使用策略函数字符串.
  • restoreFunction" 一个同步函数,必须返回一个函数来序列化路由的有效载荷.

在继续阅读文件之前

  • 当你使用 readMode: true,应用程序计划没有编译(它们被忽略). 所以,如果你改变你的计划, 你必须重新编译它们!
  • 如您所见,您必须使用 routeOpts 对象。 你可以用这个 routeOpts.schema.$id 字段,由您来定义一个独特的计划标识符.
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 })

如何运作

这个模块提供工厂的功能来生产 序列器编译器 函数.

许可证

许可证根据 麻省理工学院。 。 。 .

版本与发布记录

@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 是什么?

构建和管理用于加固框架的快速加固实例

如何安装 @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 和提交记录判断。