Socket
Socket
Sign inDemoInstall

@fastify/fast-json-stringify-compiler

Package Overview
Dependencies
12
Maintainers
17
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 4.0.0 to 4.1.0

standalone.js

24

index.d.ts
import { Options as FJSOptions } from 'fast-json-stringify'
export type { Options } from 'fast-json-stringify'
export type SerializerCompiler = (

@@ -8,6 +10,22 @@ externalSchemas: unknown,

export declare function SerializerSelector(): SerializerCompiler;
export type RouteDefinition = {
method: string,
url: string,
httpStatus: string,
schema?: unknown,
}
export type { Options } from 'fast-json-stringify'
export interface StandaloneOptions {
readMode: Boolean,
storeFunction?(opts: RouteDefinition, schemaSerializationCode: string): void,
restoreFunction?(opts: RouteDefinition): void,
}
export default SerializerSelector;
declare function SerializerSelector(): SerializerCompiler;
declare function StandaloneSerializer(options: StandaloneOptions): SerializerCompiler;
export default SerializerSelector;
export {
SerializerSelector,
StandaloneSerializer,
};

@@ -21,1 +21,4 @@ 'use strict'

module.exports = SerializerSelector
module.exports.default = SerializerSelector
module.exports.SerializerSelector = SerializerSelector
module.exports.StandaloneSerializer = require('./standalone')

12

package.json
{
"name": "@fastify/fast-json-stringify-compiler",
"description": "Build and manage the fast-json-stringify instances for the fastify framework",
"version": "4.0.0",
"version": "4.1.0",
"main": "index.js",
"types": "index.d.ts",
"scripts": {
"lint": "standard",
"lint:fix": "standard --fix",
"unit": "tap --100 test/**/*.test.js",
"test": "standard && npm run unit && npm run test:typescript",
"unit": "tap test/**/*.test.js",
"test": "npm run unit && npm run test:typescript",
"posttest": "rimraf test/fjs-generated*.js",
"test:typescript": "tsd"

@@ -26,5 +28,7 @@ },

"fastify": "^4.0.0",
"rimraf": "^3.0.2",
"sanitize-filename": "^1.6.3",
"standard": "^17.0.0",
"tap": "^16.0.0",
"tsd": "^0.21.0"
"tsd": "^0.22.0"
},

@@ -31,0 +35,0 @@ "dependencies": {

@@ -16,3 +16,3 @@ # @fastify/fast-json-stringify-compiler

| v3.x | v4.x | ^4.x |
| v3.x | v5.x | ^4.x |
| v4.x | v5.x | ^4.x |

@@ -30,2 +30,94 @@ ### fast-json-stringify Configuration

### fast-json-stringify Standalone
`fast-json-stringify@v4.1.0` introduces the [standalone feature](https://github.com/fastify/fast-json-stringify#standalone) that let you to 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 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**.
```js
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 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.
```js
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

@@ -32,0 +124,0 @@

@@ -28,5 +28,3 @@ 'use strict'

const fastifyFjsOptionsDefault = Object.freeze({
customOptions: {}
})
const fastifyFjsOptionsDefault = Object.freeze({})

@@ -33,0 +31,0 @@ t.test('basic usage', t => {

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc