Spaces:
Runtime error
Runtime error
File size: 5,860 Bytes
23ac194 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
'use strict'
const { buildSchemas } = require('./schemas')
const SerializerSelector = require('@fastify/fast-json-stringify-compiler')
const ValidatorSelector = require('@fastify/ajv-compiler')
/**
* Called at every fastify context that is being created.
* @param {object} parentSchemaCtrl: the SchemaController instance of the Fastify parent context
* @param {object} opts: the `schemaController` server option. It can be undefined when a parentSchemaCtrl is set
* @return {object}:a new SchemaController
*/
function buildSchemaController (parentSchemaCtrl, opts) {
if (parentSchemaCtrl) {
return new SchemaController(parentSchemaCtrl, opts)
}
const compilersFactory = Object.assign({
buildValidator: null,
buildSerializer: null
}, opts?.compilersFactory)
if (!compilersFactory.buildValidator) {
compilersFactory.buildValidator = ValidatorSelector()
}
if (!compilersFactory.buildSerializer) {
compilersFactory.buildSerializer = SerializerSelector()
}
const option = {
bucket: (opts && opts.bucket) || buildSchemas,
compilersFactory,
isCustomValidatorCompiler: typeof opts?.compilersFactory?.buildValidator === 'function',
isCustomSerializerCompiler: typeof opts?.compilersFactory?.buildValidator === 'function'
}
return new SchemaController(undefined, option)
}
class SchemaController {
constructor (parent, options) {
this.opts = options || parent?.opts
this.addedSchemas = false
this.compilersFactory = this.opts.compilersFactory
if (parent) {
this.schemaBucket = this.opts.bucket(parent.getSchemas())
this.validatorCompiler = parent.getValidatorCompiler()
this.serializerCompiler = parent.getSerializerCompiler()
this.isCustomValidatorCompiler = parent.isCustomValidatorCompiler
this.isCustomSerializerCompiler = parent.isCustomSerializerCompiler
this.parent = parent
} else {
this.schemaBucket = this.opts.bucket()
this.isCustomValidatorCompiler = this.opts.isCustomValidatorCompiler || false
this.isCustomSerializerCompiler = this.opts.isCustomSerializerCompiler || false
}
}
// Bucket interface
add (schema) {
this.addedSchemas = true
return this.schemaBucket.add(schema)
}
getSchema (schemaId) {
return this.schemaBucket.getSchema(schemaId)
}
getSchemas () {
return this.schemaBucket.getSchemas()
}
setValidatorCompiler (validatorCompiler) {
// Set up as if the fixed validator compiler had been provided
// by a custom 'options.compilersFactory.buildValidator' that
// always returns the same compiler object. This is required because:
//
// - setValidatorCompiler must immediately install a compiler to preserve
// legacy behavior
// - setupValidator will recreate compilers from builders in some
// circumstances, so we have to install this adapter to make it
// behave the same if the legacy API is used
//
// The cloning of the compilersFactory object is necessary because
// we are aliasing the parent compilersFactory if none was provided
// to us (see constructor.)
this.compilersFactory = Object.assign(
{},
this.compilersFactory,
{ buildValidator: () => validatorCompiler })
this.validatorCompiler = validatorCompiler
this.isCustomValidatorCompiler = true
}
setSerializerCompiler (serializerCompiler) {
// Set up as if the fixed serializer compiler had been provided
// by a custom 'options.compilersFactory.buildSerializer' that
// always returns the same compiler object. This is required because:
//
// - setSerializerCompiler must immediately install a compiler to preserve
// legacy behavior
// - setupSerializer will recreate compilers from builders in some
// circumstances, so we have to install this adapter to make it
// behave the same if the legacy API is used
//
// The cloning of the compilersFactory object is necessary because
// we are aliasing the parent compilersFactory if none was provided
// to us (see constructor.)
this.compilersFactory = Object.assign(
{},
this.compilersFactory,
{ buildSerializer: () => serializerCompiler })
this.serializerCompiler = serializerCompiler
this.isCustomSerializerCompiler = true
}
getValidatorCompiler () {
return this.validatorCompiler || (this.parent && this.parent.getValidatorCompiler())
}
getSerializerCompiler () {
return this.serializerCompiler || (this.parent && this.parent.getSerializerCompiler())
}
getSerializerBuilder () {
return this.compilersFactory.buildSerializer || (this.parent && this.parent.getSerializerBuilder())
}
getValidatorBuilder () {
return this.compilersFactory.buildValidator || (this.parent && this.parent.getValidatorBuilder())
}
/**
* This method will be called when a validator must be setup.
* Do not setup the compiler more than once
* @param {object} serverOptions the fastify server options
*/
setupValidator (serverOptions) {
const isReady = this.validatorCompiler !== undefined && !this.addedSchemas
if (isReady) {
return
}
this.validatorCompiler = this.getValidatorBuilder()(this.schemaBucket.getSchemas(), serverOptions.ajv)
}
/**
* This method will be called when a serializer must be setup.
* Do not setup the compiler more than once
* @param {object} serverOptions the fastify server options
*/
setupSerializer (serverOptions) {
const isReady = this.serializerCompiler !== undefined && !this.addedSchemas
if (isReady) {
return
}
this.serializerCompiler = this.getSerializerBuilder()(this.schemaBucket.getSchemas(), serverOptions.serializerOpts)
}
}
SchemaController.buildSchemaController = buildSchemaController
module.exports = SchemaController
|