{"_id":"@fastify/fast-json-stringify-compiler@5.1.1","bugs":{"url":"https://github.com/fastify/fast-json-stringify-compiler/issues"},"dist":{"shasum":"e15a1c0b2f0e237bec364012d4a5e33baf40d44a","tarball":"http://registry-npm.ywwl.com/@fastify/fast-json-stringify-compiler/download/@fastify/fast-json-stringify-compiler-5.1.1.tgz","fileCount":16,"integrity":"sha512-pyxRKk7UpUnnrztP9pF4+ZYxusEQ4LifTyzRdiXmyql1QezNHxO02D25MVU2o4kMeAMYWAjG6RKRlojgkbiDAw==","signatures":[{"sig":"MEYCIQCxJ5PnAYr2eGxBYKygo63x05n8BmQTUotUD/16s3X+LAIhALO6l1Mzo9BDcoz9xL8ppekUCV4NYmpW32S6RTK3e6ad","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"},{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQCI9x2XbqwWzf2m6O+SxvokjeMGC7TU+tfRoTdllam8qwIhAIeHfBF03ZW8iMQUzeCCm8jEj6gDic4GMp1C/fIQb1cM"}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/@fastify%2ffast-json-stringify-compiler@5.1.1","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":26811,"size":7691,"noattachment":false,"key":"/@fastify/fast-json-stringify-compiler/-/@fastify/fast-json-stringify-compiler-5.1.1.tgz"},"main":"index.js","name":"@fastify/fast-json-stringify-compiler","type":"commonjs","types":"types/index.d.ts","author":{"url":"https://github.com/Eomm","name":"Manuel Spigolon","email":"manuel.spigolon@nearform.com"},"funding":[{"url":"https://github.com/sponsors/fastify","type":"github"},{"url":"https://opencollective.com/fastify","type":"opencollective"}],"gitHead":"9daaaf142c3a536c8e3918e6f8508c3b58ff5eb9","license":"MIT","scripts":{"lint":"eslint","test":"npm run unit && npm run test:typescript","unit":"c8 --100 node --test","lint:fix":"eslint --fix","test:typescript":"tstyche"},"version":"5.1.1","_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"1c62f346-b055-44e5-a635-a0979fc49d71"}},"homepage":"https://github.com/fastify/fast-json-stringify-compiler#readme","keywords":[],"repository":{"url":"git+https://github.com/fastify/fast-json-stringify-compiler.git","type":"git"},"_npmVersion":"11.19.0","description":"Build and manage the fast-json-stringify instances for the fastify framework","directories":{},"maintainers":[{"name":"delvedor","email":"tommydelved@gmail.com"},{"name":"matteo.collina","email":"hello@matteocollina.com"},{"name":"jsumners","email":"james.sumners@gmail.com"},{"name":"eomm","email":"behemoth89@gmail.com"},{"name":"climba03003","email":"kaka@kakawebsitedemo.com"}],"_nodeVersion":"24.21.0","contributors":[{"name":"Matteo Collina","email":"hello@matteocollina.com"},{"name":"Aras Abbasi","email":"aras.abbasi@gmail.com"},{"url":"https://james.sumners.info","name":"James Sumners"},{"url":"https://github.com/fdawgs","name":"Frazer Smith","email":"frazer.dev@icloud.com"}],"dependencies":{"fast-json-stringify":"^7.0.0"},"_hasShrinkwrap":false,"devDependencies":{"c8":"^12.0.0","eslint":"^9.17.0","fastify":"^5.0.0","tstyche":"^7.1.0","neostandard":"^0.13.0","sanitize-filename":"^1.6.3"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/fast-json-stringify-compiler_5.1.1_1791626895665_0.014504301200656933"},"readme":"# @fastify/fast-json-stringify-compiler\n\n[![CI](https://github.com/fastify/fast-json-stringify-compiler/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/fastify/fast-json-stringify-compiler/actions/workflows/ci.yml)\n[![NPM version](https://img.shields.io/npm/v/@fastify/fast-json-stringify-compiler.svg?style=flat)](https://www.npmjs.com/package/@fastify/fast-json-stringify-compiler)\n[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)\n\nBuild and manage the [`fast-json-stringify`](https://www.npmjs.com/package/fast-json-stringify) instances for the Fastify framework.\nThis package is responsible for compiling the application's `response` JSON schemas into optimized functions to speed up the response time.\n\n## Versions\n\n| `@fastify/fast-json-stringify-compiler` | `fast-json-stringify` | Supported `fastify` |\n|----------------------------------------:|----------------------:|--------------------:|\n|                                    v5.x |                v6-7.x |                ^5.x |\n|                                    v4.x |                  v5.x |                ^5.x |\n|                                    v3.x |                  v4.x |                ^4.x |\n|                                    v2.x |                  v3.x |                ^4.x |\n|                                    v1.x |                  v3.x |                ^3.x |\n\n### fast-json-stringify Configuration\n\nThe `fast-json-stringify` configuration is the default one. You can check the default settings in the [`fast-json-stringify` option](https://github.com/fastify/fast-json-stringify/#options) documentation.\n\nYou can also override the default configuration by passing the [`serializerOpts`](https://fastify.dev/docs/latest/Reference/Server/#serializeropts) configuration to the Fastify instance.\n\n## Usage\n\nThis module is already used as default by Fastify.\nIf you need to provide to your server instance a different version, refer to [the official doc](https://fastify.dev/docs/latest/Reference/Server/#schemacontroller).\n\n### fast-json-stringify Standalone\n\n`fast-json-stringify@v4.1.0` introduces the [standalone feature](https://github.com/fastify/fast-json-stringify#standalone-mode) that lets you pre-compile your schemas and use them in your application for a faster startup.\n\nTo use this feature, you must be aware of the following:\n\n1. You must generate and save the application's compiled schemas.\n2. Read the compiled schemas from the file and provide them back to your Fastify application.\n\n\n#### Generate and save the compiled schemas\n\nFastify helps you to generate the serialization schemas functions and it is your choice to save them where you want.\nTo accomplish this, you must use a new compiler: `@fastify/fast-json-stringify-compiler/standalone`.\n\nYou must provide 2 parameters to this compiler:\n\n- `readMode: false`: a boolean to indicate that you want to generate the schemas functions string.\n- `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.\n\nWhen `readMode: false`, **the compiler is meant to be used in development ONLY**.\n\n\n```js\nconst { StandaloneSerializer } = require('@fastify/fast-json-stringify-compiler')\n\nconst factory = StandaloneSerializer({\n  readMode: false,\n  storeFunction (routeOpts, schemaSerializationCode) {\n    // routeOpts is like: { schema, method, url, httpStatus }\n    // schemaSerializationCode is a string source code that is the compiled schema function\n    const fileName = generateFileName(routeOpts)\n    fs.writeFileSync(path.join(__dirname, fileName), schemaSerializationCode)\n  }\n})\n\nconst app = fastify({\n  jsonShorthand: false,\n  schemaController: {\n    compilersFactory: {\n      buildSerializer: factory\n    }\n  }\n})\n\n// ... add all your routes with schemas ...\n\napp.ready().then(() => {\n  // at this stage all your schemas are compiled and stored in the file system\n  // now it is important to turn off the readMode\n})\n```\n\n#### Read the compiled schemas functions\n\nAt this stage, you should have a file for every route's schema.\nTo use them, you must use the `@fastify/fast-json-stringify-compiler/standalone` with the parameters:\n\n- `readMode: true`: a boolean to indicate that you want to read and use the schemas functions string.\n- `restoreFunction`\" a sync function that must return a function to serialize the route's payload.\n\nImportant keep away before you continue reading the documentation:\n\n- 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!\n- 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.\n\n```js\nconst { StandaloneSerializer } = require('@fastify/fast-json-stringify-compiler')\n\nconst factory = StandaloneSerializer({\n  readMode: true,\n  restoreFunction (routeOpts) {\n    // routeOpts is like: { schema, method, url, httpStatus }\n    const fileName = generateFileName(routeOpts)\n    return require(path.join(__dirname, fileName))\n  }\n})\n\nconst app = fastify({\n  jsonShorthand: false,\n  schemaController: {\n    compilersFactory: {\n      buildSerializer: factory\n    }\n  }\n})\n\n// ... add all your routes with schemas as before...\n\napp.listen({ port: 3000 })\n```\n\n### How it works\n\nThis module provides a factory function to produce [Serializer Compilers](https://fastify.dev/docs/latest/Reference/Server/#serializercompiler) functions.\n\n## License\n\nLicensed under [MIT](./LICENSE).\n","publish_time":1791626895767,"_cnpm_publish_time":1791626895767,"dist-tags":{"latest":"5.1.1","next":"5.0.0"}}