fastify 3.9.2 → 3.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/GOVERNANCE.md +1 -1
- package/README.md +12 -8
- package/SECURITY.md +3 -3
- package/docs/ContentTypeParser.md +1 -1
- package/docs/Ecosystem.md +16 -6
- package/docs/Encapsulation.md +5 -2
- package/docs/Fluent-Schema.md +4 -4
- package/docs/Getting-Started.md +1 -1
- package/docs/Hooks.md +28 -1
- package/docs/Lifecycle.md +8 -1
- package/docs/Middleware.md +5 -4
- package/docs/Reply.md +13 -4
- package/docs/Routes.md +4 -3
- package/docs/Server.md +78 -4
- package/docs/Serverless.md +23 -51
- package/docs/TypeScript.md +35 -18
- package/docs/Validation-and-Serialization.md +4 -4
- package/docs/Write-Plugin.md +4 -4
- package/examples/hooks-benchmark.js +12 -12
- package/examples/hooks.js +16 -16
- package/examples/plugin.js +2 -2
- package/examples/route-prefix.js +4 -4
- package/fastify.d.ts +16 -1
- package/fastify.js +33 -16
- package/isolate-0x426d1e0-1227-v8.log +4019 -0
- package/isolate-0x4d4c7e0-1988-v8.log +4081 -0
- package/lib/errors.js +6 -0
- package/lib/headRoute.js +31 -0
- package/lib/pluginOverride.js +5 -5
- package/lib/pluginUtils.js +7 -6
- package/lib/reply.js +14 -2
- package/lib/reqIdGenFactory.js +5 -0
- package/lib/request.js +1 -1
- package/lib/route.js +66 -41
- package/lib/schema-compilers.js +5 -3
- package/lib/schema-controller.js +106 -0
- package/lib/schemas.js +14 -24
- package/lib/server.js +1 -0
- package/lib/symbols.js +1 -3
- package/lib/warnings.js +2 -0
- package/lib/wrapThenable.js +2 -1
- package/package.json +25 -21
- package/test/404s.test.js +120 -120
- package/test/500s.test.js +8 -8
- package/test/async-await.test.js +29 -1
- package/test/close.test.js +8 -8
- package/test/context-config.test.js +52 -0
- package/test/custom-parser.test.js +8 -8
- package/test/decorator.test.js +49 -49
- package/test/default-route.test.js +43 -0
- package/test/fastify-instance.test.js +2 -2
- package/test/fluent-schema.test.js +3 -3
- package/test/handler-context.test.js +2 -2
- package/test/hooks-async.test.js +3 -3
- package/test/hooks.on-ready.test.js +12 -12
- package/test/hooks.test.js +75 -32
- package/test/http2/closing.test.js +23 -1
- package/test/inject.test.js +6 -6
- package/test/input-validation.js +2 -2
- package/test/internals/hookRunner.test.js +50 -50
- package/test/internals/reply.test.js +47 -22
- package/test/internals/request.test.js +3 -9
- package/test/internals/version.test.js +2 -2
- package/test/logger.test.js +30 -30
- package/test/middleware.test.js +4 -4
- package/test/plugin.helper.js +2 -2
- package/test/plugin.test.js +154 -99
- package/test/register.test.js +11 -11
- package/test/request-error.test.js +2 -2
- package/test/route-hooks.test.js +24 -24
- package/test/route-prefix.test.js +81 -52
- package/test/route.test.js +568 -0
- package/test/schema-feature.test.js +168 -38
- package/test/schema-serialization.test.js +4 -4
- package/test/schema-special-usage.test.js +136 -0
- package/test/schema-validation.test.js +7 -7
- package/test/skip-reply-send.test.js +315 -0
- package/test/stream.test.js +6 -6
- package/test/throw.test.js +4 -4
- package/test/types/instance.test-d.ts +5 -3
- package/test/types/plugin.test-d.ts +7 -7
- package/test/types/reply.test-d.ts +1 -0
- package/test/types/schema.test-d.ts +15 -0
- package/test/validation-error-handling.test.js +5 -5
- package/test/versioned-routes.test.js +1 -1
- package/types/content-type-parser.d.ts +1 -1
- package/types/instance.d.ts +6 -3
- package/types/plugin.d.ts +1 -1
- package/types/reply.d.ts +1 -0
- package/types/route.d.ts +8 -2
- package/types/schema.d.ts +3 -0
- package/test/skip-reply-send.js +0 -98
package/fastify.js
CHANGED
|
@@ -15,9 +15,7 @@ const {
|
|
|
15
15
|
kLogLevel,
|
|
16
16
|
kLogSerializers,
|
|
17
17
|
kHooks,
|
|
18
|
-
|
|
19
|
-
kValidatorCompiler,
|
|
20
|
-
kSerializerCompiler,
|
|
18
|
+
kSchemaController,
|
|
21
19
|
kReplySerializerDefault,
|
|
22
20
|
kContentTypeParser,
|
|
23
21
|
kReply,
|
|
@@ -36,8 +34,8 @@ const Request = require('./lib/request')
|
|
|
36
34
|
const supportedMethods = ['DELETE', 'GET', 'HEAD', 'PATCH', 'POST', 'PUT', 'OPTIONS']
|
|
37
35
|
const decorator = require('./lib/decorate')
|
|
38
36
|
const ContentTypeParser = require('./lib/contentTypeParser')
|
|
37
|
+
const SchemaController = require('./lib/schema-controller')
|
|
39
38
|
const { Hooks, hookRunnerApplication } = require('./lib/hooks')
|
|
40
|
-
const { Schemas } = require('./lib/schemas')
|
|
41
39
|
const { createLogger } = require('./lib/logger')
|
|
42
40
|
const pluginUtils = require('./lib/pluginUtils')
|
|
43
41
|
const reqIdGenFactory = require('./lib/reqIdGenFactory')
|
|
@@ -86,6 +84,10 @@ function fastify (options) {
|
|
|
86
84
|
throw new Error(`querystringParser option should be a function, instead got '${typeof options.querystringParser}'`)
|
|
87
85
|
}
|
|
88
86
|
|
|
87
|
+
if (options.schemaController && options.schemaController.bucket && typeof options.schemaController.bucket !== 'function') {
|
|
88
|
+
throw new Error(`schemaController.bucket option should be a function, instead got '${typeof options.schemaController.bucket}'`)
|
|
89
|
+
}
|
|
90
|
+
|
|
89
91
|
validateBodyLimitOption(options.bodyLimit)
|
|
90
92
|
|
|
91
93
|
const requestIdHeader = options.requestIdHeader || defaultInitOptions.requestIdHeader
|
|
@@ -94,6 +96,7 @@ function fastify (options) {
|
|
|
94
96
|
const requestIdLogLabel = options.requestIdLogLabel || 'reqId'
|
|
95
97
|
const bodyLimit = options.bodyLimit || defaultInitOptions.bodyLimit
|
|
96
98
|
const disableRequestLogging = options.disableRequestLogging || false
|
|
99
|
+
const exposeHeadRoutes = options.exposeHeadRoutes != null ? options.exposeHeadRoutes : false
|
|
97
100
|
|
|
98
101
|
const ajvOptions = Object.assign({
|
|
99
102
|
customOptions: {},
|
|
@@ -127,6 +130,7 @@ function fastify (options) {
|
|
|
127
130
|
options.disableRequestLogging = disableRequestLogging
|
|
128
131
|
options.ajv = ajvOptions
|
|
129
132
|
options.clientErrorHandler = options.clientErrorHandler || defaultClientErrorHandler
|
|
133
|
+
options.exposeHeadRoutes = exposeHeadRoutes
|
|
130
134
|
|
|
131
135
|
const initialConfig = getSecuredInitialConfig(options)
|
|
132
136
|
|
|
@@ -153,7 +157,7 @@ function fastify (options) {
|
|
|
153
157
|
const { server, listen } = createServer(options, httpHandler)
|
|
154
158
|
|
|
155
159
|
const setupResponseListeners = Reply.setupResponseListeners
|
|
156
|
-
const
|
|
160
|
+
const schemaController = SchemaController.buildSchemaController(null, options.schemaController)
|
|
157
161
|
|
|
158
162
|
// Public API
|
|
159
163
|
const fastify = {
|
|
@@ -170,11 +174,9 @@ function fastify (options) {
|
|
|
170
174
|
[kLogLevel]: '',
|
|
171
175
|
[kLogSerializers]: null,
|
|
172
176
|
[kHooks]: new Hooks(),
|
|
173
|
-
[
|
|
174
|
-
[kValidatorCompiler]: null,
|
|
177
|
+
[kSchemaController]: schemaController,
|
|
175
178
|
[kSchemaErrorFormatter]: null,
|
|
176
179
|
[kErrorHandler]: defaultErrorHandler,
|
|
177
|
-
[kSerializerCompiler]: null,
|
|
178
180
|
[kReplySerializerDefault]: null,
|
|
179
181
|
[kContentTypeParser]: new ContentTypeParser(
|
|
180
182
|
bodyLimit,
|
|
@@ -187,6 +189,10 @@ function fastify (options) {
|
|
|
187
189
|
[pluginUtils.registeredPlugins]: [],
|
|
188
190
|
[kPluginNameChain]: [],
|
|
189
191
|
[kAvvioBoot]: null,
|
|
192
|
+
// routing method
|
|
193
|
+
routing: httpHandler,
|
|
194
|
+
getDefaultRoute: router.getDefaultRoute.bind(router),
|
|
195
|
+
setDefaultRoute: router.setDefaultRoute.bind(router),
|
|
190
196
|
// routes shorthand methods
|
|
191
197
|
delete: function _delete (url, opts, handler) {
|
|
192
198
|
return router.prepareRoute.call(this, 'DELETE', url, opts, handler)
|
|
@@ -224,10 +230,11 @@ function fastify (options) {
|
|
|
224
230
|
addHook: addHook,
|
|
225
231
|
// schemas
|
|
226
232
|
addSchema: addSchema,
|
|
227
|
-
getSchema:
|
|
228
|
-
getSchemas:
|
|
233
|
+
getSchema: schemaController.getSchema.bind(schemaController),
|
|
234
|
+
getSchemas: schemaController.getSchemas.bind(schemaController),
|
|
229
235
|
setValidatorCompiler: setValidatorCompiler,
|
|
230
236
|
setSerializerCompiler: setSerializerCompiler,
|
|
237
|
+
setSchemaController: setSchemaController,
|
|
231
238
|
setReplySerializer: setReplySerializer,
|
|
232
239
|
setSchemaErrorFormatter: setSchemaErrorFormatter,
|
|
233
240
|
// custom parsers
|
|
@@ -275,10 +282,10 @@ function fastify (options) {
|
|
|
275
282
|
get () { return this[kRoutePrefix] }
|
|
276
283
|
},
|
|
277
284
|
validatorCompiler: {
|
|
278
|
-
get () { return this[
|
|
285
|
+
get () { return this[kSchemaController].getValidatorCompiler() }
|
|
279
286
|
},
|
|
280
287
|
serializerCompiler: {
|
|
281
|
-
get () { return this[
|
|
288
|
+
get () { return this[kSchemaController].getSerializerCompiler() }
|
|
282
289
|
},
|
|
283
290
|
version: {
|
|
284
291
|
get () {
|
|
@@ -319,7 +326,7 @@ function fastify (options) {
|
|
|
319
326
|
use: 'register'
|
|
320
327
|
}
|
|
321
328
|
})
|
|
322
|
-
// Override to allow the plugin
|
|
329
|
+
// Override to allow the plugin encapsulation
|
|
323
330
|
avvio.override = override
|
|
324
331
|
avvio.on('start', () => (fastify[kState].started = true))
|
|
325
332
|
fastify[kAvvioBoot] = fastify.ready // the avvio ready function
|
|
@@ -489,7 +496,7 @@ function fastify (options) {
|
|
|
489
496
|
// wrapper that we expose to the user for schemas handling
|
|
490
497
|
function addSchema (schema) {
|
|
491
498
|
throwIfAlreadyStarted('Cannot call "addSchema" when fastify instance is already started!')
|
|
492
|
-
this[
|
|
499
|
+
this[kSchemaController].add(schema)
|
|
493
500
|
this[kChildren].forEach(child => child.addSchema(schema))
|
|
494
501
|
return this
|
|
495
502
|
}
|
|
@@ -556,7 +563,7 @@ function fastify (options) {
|
|
|
556
563
|
|
|
557
564
|
function setValidatorCompiler (validatorCompiler) {
|
|
558
565
|
throwIfAlreadyStarted('Cannot call "setValidatorCompiler" when fastify instance is already started!')
|
|
559
|
-
this[
|
|
566
|
+
this[kSchemaController].setValidatorCompiler(validatorCompiler)
|
|
560
567
|
return this
|
|
561
568
|
}
|
|
562
569
|
|
|
@@ -569,7 +576,17 @@ function fastify (options) {
|
|
|
569
576
|
|
|
570
577
|
function setSerializerCompiler (serializerCompiler) {
|
|
571
578
|
throwIfAlreadyStarted('Cannot call "setSerializerCompiler" when fastify instance is already started!')
|
|
572
|
-
this[
|
|
579
|
+
this[kSchemaController].setSerializerCompiler(serializerCompiler)
|
|
580
|
+
return this
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
function setSchemaController (schemaControllerOpts) {
|
|
584
|
+
throwIfAlreadyStarted('Cannot call "setSchemaController" when fastify instance is already started!')
|
|
585
|
+
const old = this[kSchemaController]
|
|
586
|
+
const schemaController = SchemaController.buildSchemaController(old.parent, Object.assign({}, old.opts, schemaControllerOpts))
|
|
587
|
+
this[kSchemaController] = schemaController
|
|
588
|
+
this.getSchema = schemaController.getSchema.bind(schemaController)
|
|
589
|
+
this.getSchemas = schemaController.getSchemas.bind(schemaController)
|
|
573
590
|
return this
|
|
574
591
|
}
|
|
575
592
|
|