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.
Files changed (92) hide show
  1. package/GOVERNANCE.md +1 -1
  2. package/README.md +12 -8
  3. package/SECURITY.md +3 -3
  4. package/docs/ContentTypeParser.md +1 -1
  5. package/docs/Ecosystem.md +16 -6
  6. package/docs/Encapsulation.md +5 -2
  7. package/docs/Fluent-Schema.md +4 -4
  8. package/docs/Getting-Started.md +1 -1
  9. package/docs/Hooks.md +28 -1
  10. package/docs/Lifecycle.md +8 -1
  11. package/docs/Middleware.md +5 -4
  12. package/docs/Reply.md +13 -4
  13. package/docs/Routes.md +4 -3
  14. package/docs/Server.md +78 -4
  15. package/docs/Serverless.md +23 -51
  16. package/docs/TypeScript.md +35 -18
  17. package/docs/Validation-and-Serialization.md +4 -4
  18. package/docs/Write-Plugin.md +4 -4
  19. package/examples/hooks-benchmark.js +12 -12
  20. package/examples/hooks.js +16 -16
  21. package/examples/plugin.js +2 -2
  22. package/examples/route-prefix.js +4 -4
  23. package/fastify.d.ts +16 -1
  24. package/fastify.js +33 -16
  25. package/isolate-0x426d1e0-1227-v8.log +4019 -0
  26. package/isolate-0x4d4c7e0-1988-v8.log +4081 -0
  27. package/lib/errors.js +6 -0
  28. package/lib/headRoute.js +31 -0
  29. package/lib/pluginOverride.js +5 -5
  30. package/lib/pluginUtils.js +7 -6
  31. package/lib/reply.js +14 -2
  32. package/lib/reqIdGenFactory.js +5 -0
  33. package/lib/request.js +1 -1
  34. package/lib/route.js +66 -41
  35. package/lib/schema-compilers.js +5 -3
  36. package/lib/schema-controller.js +106 -0
  37. package/lib/schemas.js +14 -24
  38. package/lib/server.js +1 -0
  39. package/lib/symbols.js +1 -3
  40. package/lib/warnings.js +2 -0
  41. package/lib/wrapThenable.js +2 -1
  42. package/package.json +25 -21
  43. package/test/404s.test.js +120 -120
  44. package/test/500s.test.js +8 -8
  45. package/test/async-await.test.js +29 -1
  46. package/test/close.test.js +8 -8
  47. package/test/context-config.test.js +52 -0
  48. package/test/custom-parser.test.js +8 -8
  49. package/test/decorator.test.js +49 -49
  50. package/test/default-route.test.js +43 -0
  51. package/test/fastify-instance.test.js +2 -2
  52. package/test/fluent-schema.test.js +3 -3
  53. package/test/handler-context.test.js +2 -2
  54. package/test/hooks-async.test.js +3 -3
  55. package/test/hooks.on-ready.test.js +12 -12
  56. package/test/hooks.test.js +75 -32
  57. package/test/http2/closing.test.js +23 -1
  58. package/test/inject.test.js +6 -6
  59. package/test/input-validation.js +2 -2
  60. package/test/internals/hookRunner.test.js +50 -50
  61. package/test/internals/reply.test.js +47 -22
  62. package/test/internals/request.test.js +3 -9
  63. package/test/internals/version.test.js +2 -2
  64. package/test/logger.test.js +30 -30
  65. package/test/middleware.test.js +4 -4
  66. package/test/plugin.helper.js +2 -2
  67. package/test/plugin.test.js +154 -99
  68. package/test/register.test.js +11 -11
  69. package/test/request-error.test.js +2 -2
  70. package/test/route-hooks.test.js +24 -24
  71. package/test/route-prefix.test.js +81 -52
  72. package/test/route.test.js +568 -0
  73. package/test/schema-feature.test.js +168 -38
  74. package/test/schema-serialization.test.js +4 -4
  75. package/test/schema-special-usage.test.js +136 -0
  76. package/test/schema-validation.test.js +7 -7
  77. package/test/skip-reply-send.test.js +315 -0
  78. package/test/stream.test.js +6 -6
  79. package/test/throw.test.js +4 -4
  80. package/test/types/instance.test-d.ts +5 -3
  81. package/test/types/plugin.test-d.ts +7 -7
  82. package/test/types/reply.test-d.ts +1 -0
  83. package/test/types/schema.test-d.ts +15 -0
  84. package/test/validation-error-handling.test.js +5 -5
  85. package/test/versioned-routes.test.js +1 -1
  86. package/types/content-type-parser.d.ts +1 -1
  87. package/types/instance.d.ts +6 -3
  88. package/types/plugin.d.ts +1 -1
  89. package/types/reply.d.ts +1 -0
  90. package/types/route.d.ts +8 -2
  91. package/types/schema.d.ts +3 -0
  92. 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
- kSchemas,
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 schemas = new Schemas()
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
- [kSchemas]: schemas,
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: schemas.getSchema.bind(schemas),
228
- getSchemas: schemas.getSchemas.bind(schemas),
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[kValidatorCompiler] }
285
+ get () { return this[kSchemaController].getValidatorCompiler() }
279
286
  },
280
287
  serializerCompiler: {
281
- get () { return this[kSerializerCompiler] }
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 incapsulation
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[kSchemas].add(schema)
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[kValidatorCompiler] = validatorCompiler
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[kSerializerCompiler] = serializerCompiler
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