@ fastify/ fast-json-stringify-compilier (中文(简体) )
建立和管理 fast-json-stringify 快速化框架实例.
这个软件包负责编译应用程序的 response JSON schemas进入优化功能,以加快响应时间.
版本数
@fastify/fast-json-stringify-compiler | fast-json-stringify | 支助 fastify |
|---|---|---|
| 页:1 | 页:1 | ^ 5.x (韩语) |
| v4.x (英语) | 页:1 | ^ 5.x (韩语) |
| ưμ㼯a | v4.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] 介绍 独立特性 让你预先编译你的计划 并用在你的应用程序中 以更快的启动.
要使用此特性,您必须了解以下内容:
- 您必须生成并保存应用程序的编译计划 .
- 从文件中读取所编译的策略, 并将其反馈到您的 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 })
如何运作
这个模块提供工厂的功能来生产 序列器编译器 函数.
许可证
许可证根据 麻省理工学院。 。 。 .