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.
- package/README.md +2 -1
- package/build/build-validation.js +14 -2
- package/docs/Guides/Ecosystem.md +23 -11
- package/docs/Guides/Index.md +1 -1
- package/docs/Guides/Migration-Guide-V3.md +1 -1
- package/docs/Guides/Plugins-Guide.md +3 -3
- package/docs/Guides/Prototype-Poisoning.md +1 -1
- package/docs/Guides/Recommendations.md +2 -2
- package/docs/Guides/Serverless.md +5 -5
- package/docs/Guides/Testing.md +3 -1
- package/docs/Guides/Write-Plugin.md +3 -3
- package/docs/Migration-Guide-V4.md +1 -1
- package/docs/Reference/Logging.md +10 -5
- package/docs/Reference/Middleware.md +3 -3
- package/docs/Reference/Routes.md +2 -2
- package/docs/Reference/Server.md +59 -10
- package/docs/Reference/TypeScript.md +2 -2
- package/docs/Reference/Validation-and-Serialization.md +11 -1
- package/fastify.d.ts +2 -1
- package/fastify.js +38 -14
- package/lib/configValidator.js +456 -332
- package/lib/errors.js +4 -0
- package/lib/request.js +2 -2
- package/lib/route.js +5 -2
- package/lib/schemas.js +3 -0
- package/lib/validation.js +8 -2
- package/package.json +34 -34
- package/test/close-pipelining.test.js +4 -2
- package/test/close.test.js +93 -3
- package/test/custom-http-server.test.js +22 -0
- package/test/decorator.test.js +146 -0
- package/test/internals/initialConfig.test.js +5 -3
- package/test/output-validation.test.js +6 -2
- package/test/plugin.test.js +177 -14
- package/test/route-prefix.test.js +250 -0
- package/test/router-options.test.js +61 -0
- package/test/schema-feature.test.js +24 -0
- package/test/schema-serialization.test.js +57 -0
- package/test/types/fastify.test-d.ts +1 -0
- package/test/types/instance.test-d.ts +1 -0
- package/test/types/logger.test-d.ts +13 -1
- package/test/types/route.test-d.ts +14 -0
- package/test/types/type-provider.test-d.ts +6 -0
- package/types/instance.d.ts +1 -0
- package/types/logger.d.ts +3 -1
- package/types/route.d.ts +1 -1
- package/types/schema.d.ts +1 -1
- 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
|
|
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
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
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.
|