fastify 4.0.0-rc.3 → 4.0.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 (48) hide show
  1. package/README.md +2 -1
  2. package/build/build-validation.js +14 -2
  3. package/docs/Guides/Ecosystem.md +23 -11
  4. package/docs/Guides/Index.md +1 -1
  5. package/docs/Guides/Migration-Guide-V3.md +1 -1
  6. package/docs/Guides/Plugins-Guide.md +3 -3
  7. package/docs/Guides/Prototype-Poisoning.md +1 -1
  8. package/docs/Guides/Recommendations.md +2 -2
  9. package/docs/Guides/Serverless.md +5 -5
  10. package/docs/Guides/Testing.md +3 -1
  11. package/docs/Guides/Write-Plugin.md +3 -3
  12. package/docs/Migration-Guide-V4.md +1 -1
  13. package/docs/Reference/Logging.md +10 -5
  14. package/docs/Reference/Middleware.md +3 -3
  15. package/docs/Reference/Routes.md +2 -2
  16. package/docs/Reference/Server.md +59 -10
  17. package/docs/Reference/TypeScript.md +2 -2
  18. package/docs/Reference/Validation-and-Serialization.md +11 -1
  19. package/fastify.d.ts +2 -1
  20. package/fastify.js +38 -14
  21. package/lib/configValidator.js +456 -332
  22. package/lib/errors.js +4 -0
  23. package/lib/request.js +2 -2
  24. package/lib/route.js +5 -2
  25. package/lib/schemas.js +3 -0
  26. package/lib/validation.js +8 -2
  27. package/package.json +34 -34
  28. package/test/close-pipelining.test.js +4 -2
  29. package/test/close.test.js +93 -3
  30. package/test/custom-http-server.test.js +22 -0
  31. package/test/decorator.test.js +146 -0
  32. package/test/internals/initialConfig.test.js +5 -3
  33. package/test/output-validation.test.js +6 -2
  34. package/test/plugin.test.js +177 -14
  35. package/test/route-prefix.test.js +250 -0
  36. package/test/router-options.test.js +61 -0
  37. package/test/schema-feature.test.js +24 -0
  38. package/test/schema-serialization.test.js +57 -0
  39. package/test/types/fastify.test-d.ts +1 -0
  40. package/test/types/instance.test-d.ts +1 -0
  41. package/test/types/logger.test-d.ts +13 -1
  42. package/test/types/route.test-d.ts +14 -0
  43. package/test/types/type-provider.test-d.ts +6 -0
  44. package/types/instance.d.ts +1 -0
  45. package/types/logger.d.ts +3 -1
  46. package/types/route.d.ts +1 -1
  47. package/types/schema.d.ts +1 -1
  48. package/types/type-provider.d.ts +3 -4
package/fastify.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict'
2
2
 
3
- const VERSION = '4.0.0-rc.3'
3
+ const VERSION = '4.0.0'
4
4
 
5
5
  const Avvio = require('avvio')
6
6
  const http = require('http')
@@ -52,6 +52,7 @@ const { defaultInitOptions } = getSecuredInitialConfig
52
52
 
53
53
  const {
54
54
  FST_ERR_BAD_URL,
55
+ FST_ERR_FORCE_CLOSE_CONNECTIONS_IDLE_NOT_AVAILABLE,
55
56
  AVVIO_ERRORS_MAP,
56
57
  appendStackTrace
57
58
  } = require('./lib/errors')
@@ -123,7 +124,6 @@ function fastify (options) {
123
124
  // Update the options with the fixed values
124
125
  options.connectionTimeout = options.connectionTimeout || defaultInitOptions.connectionTimeout
125
126
  options.keepAliveTimeout = options.keepAliveTimeout || defaultInitOptions.keepAliveTimeout
126
- options.forceCloseConnections = typeof options.forceCloseConnections === 'boolean' ? options.forceCloseConnections : defaultInitOptions.forceCloseConnections
127
127
  options.maxRequestsPerSocket = options.maxRequestsPerSocket || defaultInitOptions.maxRequestsPerSocket
128
128
  options.requestTimeout = options.requestTimeout || defaultInitOptions.requestTimeout
129
129
  options.logger = logger
@@ -135,7 +135,6 @@ function fastify (options) {
135
135
  options.clientErrorHandler = options.clientErrorHandler || defaultClientErrorHandler
136
136
 
137
137
  const initialConfig = getSecuredInitialConfig(options)
138
- const keepAliveConnections = options.forceCloseConnections === true ? new Set() : noopSet()
139
138
 
140
139
  // exposeHeadRoutes have its default set from the validator
141
140
  options.exposeHeadRoutes = initialConfig.exposeHeadRoutes
@@ -166,13 +165,13 @@ function fastify (options) {
166
165
  onBadUrl,
167
166
  constraints,
168
167
  ignoreTrailingSlash: options.ignoreTrailingSlash || defaultInitOptions.ignoreTrailingSlash,
168
+ ignoreDuplicateSlashes: options.ignoreDuplicateSlashes || defaultInitOptions.ignoreDuplicateSlashes,
169
169
  maxParamLength: options.maxParamLength || defaultInitOptions.maxParamLength,
170
170
  caseSensitive: options.caseSensitive,
171
171
  allowUnsafeRegex: options.allowUnsafeRegex || defaultInitOptions.allowUnsafeRegex,
172
172
  buildPrettyMeta: defaultBuildPrettyMeta,
173
173
  querystringParser: options.querystringParser
174
- },
175
- keepAliveConnections
174
+ }
176
175
  })
177
176
 
178
177
  // 404 router, used for handling encapsulated 404 handlers
@@ -185,6 +184,19 @@ function fastify (options) {
185
184
  options.http2SessionTimeout = initialConfig.http2SessionTimeout
186
185
  const { server, listen } = createServer(options, httpHandler)
187
186
 
187
+ const serverHasCloseAllConnections = typeof server.closeAllConnections === 'function'
188
+ const serverHasCloseIdleConnections = typeof server.closeIdleConnections === 'function'
189
+
190
+ let forceCloseConnections = options.forceCloseConnections
191
+ if (forceCloseConnections === 'idle' && !serverHasCloseIdleConnections) {
192
+ throw new FST_ERR_FORCE_CLOSE_CONNECTIONS_IDLE_NOT_AVAILABLE()
193
+ } else if (typeof forceCloseConnections !== 'boolean') {
194
+ /* istanbul ignore next: only one branch can be valid in a given Node.js version */
195
+ forceCloseConnections = serverHasCloseIdleConnections ? 'idle' : false
196
+ }
197
+
198
+ const keepAliveConnections = !serverHasCloseAllConnections && forceCloseConnections === true ? new Set() : noopSet()
199
+
188
200
  const setupResponseListeners = Reply.setupResponseListeners
189
201
  const schemaController = SchemaController.buildSchemaController(null, options.schemaController)
190
202
 
@@ -218,7 +230,7 @@ function fastify (options) {
218
230
  [kRequest]: Request.buildRequest(Request, options.trustProxy),
219
231
  [kFourOhFour]: fourOhFour,
220
232
  [pluginUtils.registeredPlugins]: [],
221
- [kPluginNameChain]: [],
233
+ [kPluginNameChain]: ['fastify'],
222
234
  [kAvvioBoot]: null,
223
235
  // routing method
224
236
  routing: httpHandler,
@@ -284,6 +296,9 @@ function fastify (options) {
284
296
  onClose: null,
285
297
  close: null,
286
298
  printPlugins: null,
299
+ hasPlugin: function (name) {
300
+ return this[kPluginNameChain].includes(name)
301
+ },
287
302
  // http server
288
303
  listen,
289
304
  server,
@@ -385,13 +400,21 @@ function fastify (options) {
385
400
  // No new TCP connections are accepted
386
401
  instance.server.close(done)
387
402
 
388
- for (const conn of fastify[kKeepAliveConnections]) {
389
- // We must invoke the destroy method instead of merely unreffing
390
- // the sockets. If we only unref, then the callback passed to
391
- // `fastify.close` will never be invoked; nor will any of the
392
- // registered `onClose` hooks.
393
- conn.destroy()
394
- fastify[kKeepAliveConnections].delete(conn)
403
+ /* istanbul ignore next: Cannot test this without Node.js core support */
404
+ if (forceCloseConnections === 'idle') {
405
+ instance.server.closeIdleConnections()
406
+ /* istanbul ignore next: Cannot test this without Node.js core support */
407
+ } else if (serverHasCloseAllConnections && forceCloseConnections) {
408
+ instance.server.closeAllConnections()
409
+ } else {
410
+ for (const conn of fastify[kKeepAliveConnections]) {
411
+ // We must invoke the destroy method instead of merely unreffing
412
+ // the sockets. If we only unref, then the callback passed to
413
+ // `fastify.close` will never be invoked; nor will any of the
414
+ // registered `onClose` hooks.
415
+ conn.destroy()
416
+ fastify[kKeepAliveConnections].delete(conn)
417
+ }
395
418
  }
396
419
  } else {
397
420
  done(null)
@@ -410,7 +433,8 @@ function fastify (options) {
410
433
  hasLogger,
411
434
  setupResponseListeners,
412
435
  throwIfAlreadyStarted,
413
- validateHTTPVersion: compileValidateHTTPVersion(options)
436
+ validateHTTPVersion: compileValidateHTTPVersion(options),
437
+ keepAliveConnections
414
438
  })
415
439
 
416
440
  // Delay configuring clientError handler so that it can access fastify state.