fastify 4.27.0 → 5.0.0-alpha.2
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/.markdownlint-cli2.yaml +1 -1
- package/.tap/processinfo/09002e93-10ad-430c-bc86-c0576928b0ed.json +241 -0
- package/.tap/processinfo/ee66c5ab-635d-48b5-8be6-3dc3ceea5bfc.json +268 -0
- package/.tap/test-results/test/404s.test.js.tap +623 -0
- package/.tap/test-results/test/500s.test.js.tap +64 -0
- package/.tap/test-results/test/allowUnsafeRegex.test.js.tap +36 -0
- package/.tap/test-results/test/als.test.js.tap +15 -0
- package/.tap/test-results/test/async-await.test.js.tap +184 -0
- package/.tap/test-results/test/async-dispose.test.js.tap +8 -0
- package/.tap/test-results/test/async_hooks.test.js.tap +10 -0
- package/.tap/test-results/test/bodyLimit.test.js.tap +48 -0
- package/.tap/test-results/test/buffer.test.js.tap +20 -0
- package/.tap/test-results/test/build/error-serializer.test.js.tap +12 -0
- package/.tap/test-results/test/build/version.test.js.tap +7 -0
- package/.tap/test-results/test/case-insensitive.test.js.tap +36 -0
- package/.tap/test-results/test/chainable.test.js.tap +17 -0
- package/.tap/test-results/test/check.test.js.tap +10 -0
- package/.tap/test-results/test/childLoggerFactory.test.js.tap +23 -0
- package/.tap/test-results/test/client-timeout.test.js.tap +7 -0
- package/.tap/test-results/test/close-pipelining.test.js.tap +15 -0
- package/.tap/test-results/test/close.test.js.tap +172 -0
- package/.tap/test-results/test/connectionTimeout.test.js.tap +12 -0
- package/.tap/test-results/test/constrained-routes.test.js.tap +173 -0
- package/.tap/test-results/test/content-length.test.js.tap +46 -0
- package/.tap/test-results/test/content-parser.test.js.tap +266 -0
- package/.tap/test-results/test/content-type.test.js.tap +14 -0
- package/.tap/test-results/test/context-config.test.js.tap +41 -0
- package/.tap/test-results/test/copy.test.js.tap +14 -0
- package/.tap/test-results/test/custom-http-server.test.js.tap +30 -0
- package/.tap/test-results/test/custom-parser-async.test.js.tap +21 -0
- package/.tap/test-results/test/custom-parser.0.test.js.tap +199 -0
- package/.tap/test-results/test/custom-parser.1.test.js.tap +90 -0
- package/.tap/test-results/test/custom-parser.2.test.js.tap +22 -0
- package/.tap/test-results/test/custom-parser.3.test.js.tap +53 -0
- package/.tap/test-results/test/custom-parser.4.test.js.tap +45 -0
- package/.tap/test-results/test/custom-parser.5.test.js.tap +41 -0
- package/.tap/test-results/test/custom-querystring-parser.test.js.tap +46 -0
- package/.tap/test-results/test/decorator.test.js.tap +465 -0
- package/.tap/test-results/test/delete.test.js.tap +110 -0
- package/.tap/test-results/test/diagnostics-channel/404.test.js.tap +15 -0
- package/.tap/test-results/test/diagnostics-channel/async-delay-request.test.js.tap +25 -0
- package/.tap/test-results/test/diagnostics-channel/async-request.test.js.tap +24 -0
- package/.tap/test-results/test/diagnostics-channel/error-before-handler.test.js.tap +9 -0
- package/.tap/test-results/test/diagnostics-channel/error-request.test.js.tap +20 -0
- package/.tap/test-results/test/diagnostics-channel/error-status.test.js.tap +10 -0
- package/.tap/test-results/test/diagnostics-channel/init.test.js.tap +14 -0
- package/.tap/test-results/test/diagnostics-channel/sync-delay-request.test.js.tap +16 -0
- package/.tap/test-results/test/diagnostics-channel/sync-request-reply.test.js.tap +16 -0
- package/.tap/test-results/test/diagnostics-channel/sync-request.test.js.tap +19 -0
- package/.tap/test-results/test/encapsulated-child-logger-factory.test.js.tap +18 -0
- package/.tap/test-results/test/encapsulated-error-handler.test.js.tap +243 -0
- package/.tap/test-results/test/esm/errorCodes.test.mjs.tap +9 -0
- package/.tap/test-results/test/esm/esm.test.mjs.tap +8 -0
- package/.tap/test-results/test/esm/index.test.js.tap +8 -0
- package/.tap/test-results/test/fastify-instance.test.js.tap +114 -0
- package/.tap/test-results/test/findRoute.test.js.tap +37 -0
- package/.tap/test-results/test/fluent-schema.test.js.tap +36 -0
- package/.tap/test-results/test/genReqId.test.js.tap +106 -0
- package/.tap/test-results/test/get.test.js.tap +151 -0
- package/.tap/test-results/test/handler-context.test.js.tap +19 -0
- package/.tap/test-results/test/has-route.test.js.tap +30 -0
- package/.tap/test-results/test/head.test.js.tap +130 -0
- package/.tap/test-results/test/header-overflow.test.js.tap +16 -0
- package/.tap/test-results/test/hooks-async.test.js.tap +286 -0
- package/.tap/test-results/test/hooks.on-listen.test.js.tap +311 -0
- package/.tap/test-results/test/hooks.on-ready.test.js.tap +151 -0
- package/.tap/test-results/test/hooks.test.js.tap +966 -0
- package/.tap/test-results/test/http2/closing.test.js.tap +35 -0
- package/.tap/test-results/test/http2/constraint.test.js.tap +32 -0
- package/.tap/test-results/test/http2/head.test.js.tap +9 -0
- package/.tap/test-results/test/http2/missing-http2-module.test.js.tap +8 -0
- package/.tap/test-results/test/http2/plain.test.js.tap +22 -0
- package/.tap/test-results/test/http2/secure-with-fallback.test.js.tap +40 -0
- package/.tap/test-results/test/http2/secure.test.js.tap +27 -0
- package/.tap/test-results/test/http2/unknown-http-method.test.js.tap +9 -0
- package/.tap/test-results/test/https/custom-https-server.test.js.tap +10 -0
- package/.tap/test-results/test/https/https.test.js.tap +45 -0
- package/.tap/test-results/test/imports.test.js.tap +14 -0
- package/.tap/test-results/test/inject.test.js.tap +165 -0
- package/.tap/test-results/test/internals/all.test.js.tap +42 -0
- package/.tap/test-results/test/internals/contentTypeParser.test.js.tap +14 -0
- package/.tap/test-results/test/internals/context.test.js.tap +14 -0
- package/.tap/test-results/test/internals/decorator.test.js.tap +51 -0
- package/.tap/test-results/test/internals/errors.test.js.tap +1212 -0
- package/.tap/test-results/test/internals/handleRequest.test.js.tap +69 -0
- package/.tap/test-results/test/internals/hookRunner.test.js.tap +143 -0
- package/.tap/test-results/test/internals/hooks.test.js.tap +45 -0
- package/.tap/test-results/test/internals/initialConfig.test.js.tap +125 -0
- package/.tap/test-results/test/internals/logger.test.js.tap +71 -0
- package/.tap/test-results/test/internals/plugin.test.js.tap +48 -0
- package/.tap/test-results/test/internals/reply-serialize.test.js.tap +166 -0
- package/.tap/test-results/test/internals/reply.test.js.tap +688 -0
- package/.tap/test-results/test/internals/reqIdGenFactory.test.js.tap +74 -0
- package/.tap/test-results/test/internals/request-validate.test.js.tap +384 -0
- package/.tap/test-results/test/internals/request.test.js.tap +163 -0
- package/.tap/test-results/test/internals/server.test.js.tap +30 -0
- package/.tap/test-results/test/internals/validation.test.js.tap +121 -0
- package/.tap/test-results/test/keepAliveTimeout.test.js.tap +12 -0
- package/.tap/test-results/test/listen.1.test.js.tap +31 -0
- package/.tap/test-results/test/listen.2.test.js.tap +46 -0
- package/.tap/test-results/test/listen.3.test.js.tap +25 -0
- package/.tap/test-results/test/listen.4.test.js.tap +51 -0
- package/.tap/test-results/test/lock.test.js.tap +29 -0
- package/.tap/test-results/test/logger/instantiation.test.js.tap +92 -0
- package/.tap/test-results/test/logger/logging.test.js.tap +117 -0
- package/.tap/test-results/test/logger/options.test.js.tap +165 -0
- package/.tap/test-results/test/logger/request.test.js.tap +82 -0
- package/.tap/test-results/test/logger/response.test.js.tap +38 -0
- package/.tap/test-results/test/maxRequestsPerSocket.test.js.tap +44 -0
- package/.tap/test-results/test/method-missing.test.js.tap +8 -0
- package/.tap/test-results/test/middleware.test.js.tap +17 -0
- package/.tap/test-results/test/mkcalendar.test.js.tap +43 -0
- package/.tap/test-results/test/mkcol.test.js.tap +14 -0
- package/.tap/test-results/test/move.test.js.tap +15 -0
- package/.tap/test-results/test/noop-set.test.js.tap +8 -0
- package/.tap/test-results/test/nullable-validation.test.js.tap +36 -0
- package/.tap/test-results/test/options.error-handler.test.js.tap +186 -0
- package/.tap/test-results/test/options.test.js.tap +174 -0
- package/.tap/test-results/test/output-validation.test.js.tap +66 -0
- package/.tap/test-results/test/patch.error-handler.test.js.tap +206 -0
- package/.tap/test-results/test/patch.test.js.tap +182 -0
- package/.tap/test-results/test/plugin.1.test.js.tap +78 -0
- package/.tap/test-results/test/plugin.2.test.js.tap +102 -0
- package/.tap/test-results/test/plugin.3.test.js.tap +58 -0
- package/.tap/test-results/test/plugin.4.test.js.tap +164 -0
- package/.tap/test-results/test/post-empty-body.test.js.tap +8 -0
- package/.tap/test-results/test/pretty-print.test.js.tap +82 -0
- package/.tap/test-results/test/promises.test.js.tap +46 -0
- package/.tap/test-results/test/propfind.test.js.tap +43 -0
- package/.tap/test-results/test/proppatch.test.js.tap +29 -0
- package/.tap/test-results/test/proto-poisoning.test.js.tap +47 -0
- package/.tap/test-results/test/put.error-handler.test.js.tap +206 -0
- package/.tap/test-results/test/put.test.js.tap +182 -0
- package/.tap/test-results/test/register.test.js.tap +61 -0
- package/.tap/test-results/test/reply-code.test.js.tap +40 -0
- package/.tap/test-results/test/reply-earlyHints.test.js.tap +22 -0
- package/.tap/test-results/test/reply-error.test.js.tap +643 -0
- package/.tap/test-results/test/reply-trailers.test.js.tap +176 -0
- package/.tap/test-results/test/report.test.js.tap +43 -0
- package/.tap/test-results/test/request-error.test.js.tap +98 -0
- package/.tap/test-results/test/request-id.test.js.tap +38 -0
- package/.tap/test-results/test/request.deprecated.test.js.tap +13 -0
- package/.tap/test-results/test/requestTimeout.test.js.tap +21 -0
- package/.tap/test-results/test/route-hooks.test.js.tap +498 -0
- package/.tap/test-results/test/route-prefix.test.js.tap +195 -0
- package/.tap/test-results/test/route-shorthand.test.js.tap +190 -0
- package/.tap/test-results/test/route.1.test.js.tap +93 -0
- package/.tap/test-results/test/route.2.test.js.tap +28 -0
- package/.tap/test-results/test/route.3.test.js.tap +39 -0
- package/.tap/test-results/test/route.4.test.js.tap +32 -0
- package/.tap/test-results/test/route.5.test.js.tap +54 -0
- package/.tap/test-results/test/route.6.test.js.tap +81 -0
- package/.tap/test-results/test/route.7.test.js.tap +93 -0
- package/.tap/test-results/test/route.8.test.js.tap +38 -0
- package/.tap/test-results/test/router-options.test.js.tap +104 -0
- package/.tap/test-results/test/same-shape.test.js.tap +22 -0
- package/.tap/test-results/test/schema-examples.test.js.tap +85 -0
- package/.tap/test-results/test/schema-feature.test.js.tap +445 -0
- package/.tap/test-results/test/schema-serialization.test.js.tap +194 -0
- package/.tap/test-results/test/schema-special-usage.test.js.tap +186 -0
- package/.tap/test-results/test/schema-validation.test.js.tap +199 -0
- package/.tap/test-results/test/search.test.js.tap +77 -0
- package/.tap/test-results/test/serialize-response.test.js.tap +26 -0
- package/.tap/test-results/test/server.test.js.tap +65 -0
- package/.tap/test-results/test/set-error-handler.test.js.tap +7 -0
- package/.tap/test-results/test/skip-reply-send.test.js.tap +272 -0
- package/.tap/test-results/test/stream.1.test.js.tap +36 -0
- package/.tap/test-results/test/stream.2.test.js.tap +20 -0
- package/.tap/test-results/test/stream.3.test.js.tap +34 -0
- package/.tap/test-results/test/stream.4.test.js.tap +40 -0
- package/.tap/test-results/test/stream.5.test.js.tap +37 -0
- package/.tap/test-results/test/sync-routes.test.js.tap +19 -0
- package/.tap/test-results/test/throw.test.js.tap +116 -0
- package/.tap/test-results/test/trace.test.js.tap +7 -0
- package/.tap/test-results/test/trust-proxy.test.js.tap +109 -0
- package/.tap/test-results/test/type-provider.test.js.tap +12 -0
- package/.tap/test-results/test/unlock.test.js.tap +14 -0
- package/.tap/test-results/test/upgrade.test.js.tap +8 -0
- package/.tap/test-results/test/url-rewriting.test.js.tap +39 -0
- package/.tap/test-results/test/useSemicolonDelimiter.test.js.tap +33 -0
- package/.tap/test-results/test/validation-error-handling.test.js.tap +180 -0
- package/.tap/test-results/test/versioned-routes.test.js.tap +151 -0
- package/.tap/test-results/test/web-api.test.js.tap +51 -0
- package/.tap/test-results/test/wrapThenable.test.js.tap +11 -0
- package/.taprc +4 -8
- package/README.md +3 -6
- package/build/build-error-serializer.js +4 -1
- package/build/build-validation.js +5 -4
- package/docs/Guides/Database.md +1 -1
- package/docs/Guides/Delay-Accepting-Requests.md +3 -3
- package/docs/Guides/Ecosystem.md +2 -0
- package/docs/Guides/Migration-Guide-V5.md +20 -0
- package/docs/Guides/Write-Type-Provider.md +4 -2
- package/docs/Reference/ContentTypeParser.md +30 -1
- package/docs/Reference/Decorators.md +42 -16
- package/docs/Reference/Errors.md +10 -2
- package/docs/Reference/Hooks.md +48 -14
- package/docs/Reference/Logging.md +5 -5
- package/docs/Reference/Reply.md +29 -24
- package/docs/Reference/Request.md +5 -1
- package/docs/Reference/Routes.md +24 -28
- package/docs/Reference/Server.md +14 -53
- package/docs/Reference/Type-Providers.md +21 -26
- package/docs/Reference/TypeScript.md +46 -29
- package/docs/Reference/Warnings.md +2 -8
- package/eslint.config.js +27 -0
- package/examples/typescript-server.ts +14 -14
- package/fastify.d.ts +15 -14
- package/fastify.js +41 -15
- package/lib/configValidator.js +94 -76
- package/lib/contentTypeParser.js +54 -88
- package/lib/decorate.js +3 -7
- package/lib/error-serializer.js +77 -19
- package/lib/errors.js +31 -6
- package/lib/handleRequest.js +70 -39
- package/lib/httpMethods.js +34 -18
- package/lib/logger.js +24 -6
- package/lib/pluginUtils.js +5 -5
- package/lib/reply.js +16 -13
- package/lib/request.js +37 -19
- package/lib/route.js +7 -31
- package/lib/server.js +62 -123
- package/lib/warnings.js +28 -27
- package/lib/wrapThenable.js +46 -22
- package/package.json +38 -58
- package/test/404s.test.js +8 -12
- package/test/async-await.test.js +46 -2
- package/test/build/error-serializer.test.js +4 -2
- package/test/check.test.js +225 -0
- package/test/close-pipelining.test.js +2 -34
- package/test/close.test.js +1 -41
- package/test/content-parser.test.js +69 -117
- package/test/custom-parser.1.test.js +40 -1
- package/test/decorator-namespace.test._js_ +31 -0
- package/test/decorator.test.js +92 -43
- package/test/delete.test.js +21 -1
- package/test/diagnostics-channel/404.test.js +57 -0
- package/test/diagnostics-channel/async-delay-request.test.js +74 -0
- package/test/diagnostics-channel/async-request.test.js +72 -0
- package/test/diagnostics-channel/error-before-handler.test.js +36 -0
- package/test/diagnostics-channel/error-request.test.js +61 -0
- package/test/diagnostics-channel/error-status.test.js +39 -0
- package/test/{diagnostics-channel.test.js → diagnostics-channel/init.test.js} +6 -16
- package/test/diagnostics-channel/sync-delay-request.test.js +58 -0
- package/test/diagnostics-channel/sync-request-reply.test.js +58 -0
- package/test/diagnostics-channel/sync-request.test.js +61 -0
- package/test/encapsulated-error-handler.test.js +201 -14
- package/test/esm/index.test.js +2 -12
- package/test/findRoute.test.js +16 -0
- package/test/genReqId.test.js +9 -0
- package/test/get.test.js +28 -0
- package/test/has-route.test.js +18 -2
- package/test/helper.js +1 -5
- package/test/hooks.test.js +0 -4
- package/test/http2/constraint.test.js +22 -1
- package/test/http2/plain.test.js +21 -6
- package/test/http2/secure.test.js +12 -1
- package/test/https/https.test.js +57 -0
- package/test/inject.test.js +1 -2
- package/test/internals/decorator.test.js +0 -2
- package/test/internals/errors.test.js +57 -17
- package/test/internals/handleRequest.test.js +5 -1
- package/test/internals/initialConfig.test.js +5 -5
- package/test/internals/logger.test.js +31 -2
- package/test/internals/reply.test.js +38 -80
- package/test/internals/request.test.js +13 -11
- package/test/listen.1.test.js +5 -15
- package/test/listen.5.test.js +88 -0
- package/test/logger/instantiation.test.js +8 -8
- package/test/logger/logging.test.js +4 -4
- package/test/logger/options.test.js +102 -21
- package/test/logger/response.test.js +6 -6
- package/test/maxRequestsPerSocket.test.js +2 -5
- package/test/method-missing.test.js +24 -0
- package/test/plugin.1.test.js +2 -4
- package/test/plugin.2.test.js +0 -2
- package/test/plugin.3.test.js +0 -2
- package/test/plugin.4.test.js +92 -56
- package/test/register.test.js +2 -4
- package/test/reply-earlyHints.test.js +98 -0
- package/test/reply-error.test.js +0 -2
- package/test/route-hooks.test.js +0 -1
- package/test/route-shorthand.test.js +60 -0
- package/test/schema-special-usage.test.js +1 -1
- package/test/server.test.js +17 -2
- package/test/stream.2.test.js +1 -1
- package/test/stream.4.test.js +0 -42
- package/test/stream.5.test.js +2 -2
- package/test/trust-proxy.test.js +33 -27
- package/test/types/errors.test-d.ts +0 -2
- package/test/types/fastify.test-d.ts +14 -12
- package/test/types/hooks.test-d.ts +1 -0
- package/test/types/import.ts +1 -0
- package/test/types/instance.test-d.ts +10 -51
- package/test/types/logger.test-d.ts +43 -6
- package/test/types/plugin.test-d.ts +5 -2
- package/test/types/register.test-d.ts +2 -2
- package/test/types/reply.test-d.ts +13 -12
- package/test/types/request.test-d.ts +19 -8
- package/test/types/route.test-d.ts +30 -2
- package/test/types/schema.test-d.ts +2 -2
- package/test/types/serverFactory.test-d.ts +1 -1
- package/test/types/type-provider.test-d.ts +60 -13
- package/test/types/using.test-d.ts +4 -1
- package/test/url-rewriting.test.js +3 -2
- package/test/useSemicolonDelimiter.test.js +3 -6
- package/test/versioned-routes.test.js +1 -1
- package/test/web-api.test.js +0 -6
- package/types/content-type-parser.d.ts +3 -3
- package/types/context.d.ts +0 -1
- package/types/errors.d.ts +1 -0
- package/types/hooks.d.ts +6 -6
- package/types/instance.d.ts +28 -41
- package/types/logger.d.ts +3 -3
- package/types/plugin.d.ts +3 -3
- package/types/reply.d.ts +13 -14
- package/types/request.d.ts +5 -3
- package/types/route.d.ts +29 -29
- package/types/schema.d.ts +3 -3
- package/types/serverFactory.d.ts +2 -2
- package/types/type-provider.d.ts +22 -12
- package/types/utils.d.ts +18 -18
- package/.c8rc.json +0 -8
- package/.eslintrc +0 -4
- package/test/default-route.test.js +0 -88
- package/test/listen.deprecated.test.js +0 -229
- package/test/unsupported-httpversion.test.js +0 -31
- package/types/.eslintrc.json +0 -48
|
@@ -9,10 +9,11 @@ const factory = AjvStandaloneCompiler({
|
|
|
9
9
|
readMode: false,
|
|
10
10
|
storeFunction (routeOpts, schemaValidationCode) {
|
|
11
11
|
const moduleCode = `// This file is autogenerated by ${__filename.replace(__dirname, 'build')}, do not edit
|
|
12
|
-
/*
|
|
12
|
+
/* c8 ignore start */
|
|
13
13
|
${schemaValidationCode}
|
|
14
14
|
|
|
15
15
|
module.exports.defaultInitOptions = ${JSON.stringify(defaultInitOptions)}
|
|
16
|
+
/* c8 ignore stop */
|
|
16
17
|
`
|
|
17
18
|
|
|
18
19
|
const file = path.join(__dirname, '..', 'lib', 'configValidator.js')
|
|
@@ -38,11 +39,11 @@ const defaultInitOptions = {
|
|
|
38
39
|
onProtoPoisoning: 'error',
|
|
39
40
|
onConstructorPoisoning: 'error',
|
|
40
41
|
pluginTimeout: 10000,
|
|
41
|
-
requestIdHeader:
|
|
42
|
+
requestIdHeader: false,
|
|
42
43
|
requestIdLogLabel: 'reqId',
|
|
43
44
|
http2SessionTimeout: 72000, // 72 seconds
|
|
44
45
|
exposeHeadRoutes: true,
|
|
45
|
-
useSemicolonDelimiter:
|
|
46
|
+
useSemicolonDelimiter: false
|
|
46
47
|
}
|
|
47
48
|
|
|
48
49
|
const schema = {
|
|
@@ -98,7 +99,7 @@ const schema = {
|
|
|
98
99
|
onProtoPoisoning: { type: 'string', default: defaultInitOptions.onProtoPoisoning },
|
|
99
100
|
onConstructorPoisoning: { type: 'string', default: defaultInitOptions.onConstructorPoisoning },
|
|
100
101
|
pluginTimeout: { type: 'integer', default: defaultInitOptions.pluginTimeout },
|
|
101
|
-
requestIdHeader: { anyOf: [{
|
|
102
|
+
requestIdHeader: { anyOf: [{ type: 'boolean' }, { type: 'string' }], default: defaultInitOptions.requestIdHeader },
|
|
102
103
|
requestIdLogLabel: { type: 'string', default: defaultInitOptions.requestIdLogLabel },
|
|
103
104
|
http2SessionTimeout: { type: 'integer', default: defaultInitOptions.http2SessionTimeout },
|
|
104
105
|
exposeHeadRoutes: { type: 'boolean', default: defaultInitOptions.exposeHeadRoutes },
|
package/docs/Guides/Database.md
CHANGED
|
@@ -245,7 +245,7 @@ for Postgres, MySQL, SQL Server and SQLite. For MongoDB migrations, please check
|
|
|
245
245
|
#### [Postgrator](https://www.npmjs.com/package/postgrator)
|
|
246
246
|
|
|
247
247
|
Postgrator is Node.js SQL migration tool that uses a directory of SQL scripts to
|
|
248
|
-
alter the database schema. Each file
|
|
248
|
+
alter the database schema. Each file in a migrations folder need to follow the
|
|
249
249
|
pattern: ` [version].[action].[optional-description].sql`.
|
|
250
250
|
|
|
251
251
|
**version:** must be an incrementing number (e.g. `001` or a timestamp).
|
|
@@ -103,7 +103,7 @@ server.get('/v1*', async function (request, reply) {
|
|
|
103
103
|
}
|
|
104
104
|
})
|
|
105
105
|
|
|
106
|
-
server.decorate('magicKey'
|
|
106
|
+
server.decorate('magicKey')
|
|
107
107
|
|
|
108
108
|
server.listen({ port: '1234' }, () => {
|
|
109
109
|
provider.thirdPartyMagicKeyGenerator(USUAL_WAIT_TIME_MS)
|
|
@@ -303,7 +303,7 @@ async function setup(fastify) {
|
|
|
303
303
|
fastify.server.on('listening', doMagic)
|
|
304
304
|
|
|
305
305
|
// Set up the placeholder for the magicKey
|
|
306
|
-
fastify.decorate('magicKey'
|
|
306
|
+
fastify.decorate('magicKey')
|
|
307
307
|
|
|
308
308
|
// Our magic -- important to make sure errors are handled. Beware of async
|
|
309
309
|
// functions outside `try/catch` blocks
|
|
@@ -406,7 +406,7 @@ https://nodejs.org/api/net.html#event-listening). We use that to reach out to
|
|
|
406
406
|
our provider as soon as possible, with the `doMagic` function.
|
|
407
407
|
|
|
408
408
|
```js
|
|
409
|
-
fastify.decorate('magicKey'
|
|
409
|
+
fastify.decorate('magicKey')
|
|
410
410
|
```
|
|
411
411
|
|
|
412
412
|
The `magicKey` decoration is also part of the plugin now. We initialize it with
|
package/docs/Guides/Ecosystem.md
CHANGED
|
@@ -447,6 +447,8 @@ section.
|
|
|
447
447
|
middlewares into Fastify plugins
|
|
448
448
|
- [`fastify-kubernetes`](https://github.com/greguz/fastify-kubernetes) Fastify
|
|
449
449
|
Kubernetes client plugin.
|
|
450
|
+
- [`fastify-kysely`](https://github.com/alenap93/fastify-kysely) Fastify
|
|
451
|
+
plugin for supporting Kysely type-safe query builder.
|
|
450
452
|
- [`fastify-language-parser`](https://github.com/lependu/fastify-language-parser)
|
|
451
453
|
Fastify plugin to parse request language.
|
|
452
454
|
- [`fastify-lcache`](https://github.com/denbon05/fastify-lcache)
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# V5 Migration Guide
|
|
2
|
+
|
|
3
|
+
This guide is intended to help with migration from Fastify v4 to v5.
|
|
4
|
+
|
|
5
|
+
Before migrating to v5, please ensure that you have fixed all deprecation
|
|
6
|
+
warnings from v4. All v4 deprecations have been removed and they will no longer
|
|
7
|
+
work after upgrading.
|
|
8
|
+
|
|
9
|
+
## Breaking Changes
|
|
10
|
+
|
|
11
|
+
### `useSemicolonDelimiter` false by default
|
|
12
|
+
|
|
13
|
+
Starting with v5, Fastify instances will no longer default to supporting the use
|
|
14
|
+
of semicolon delimiters in the query string as they did in v4.
|
|
15
|
+
This is due to it being non-standard
|
|
16
|
+
behavior and not adhering to [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986#section-3.4).
|
|
17
|
+
|
|
18
|
+
If you still wish to use semicolons as delimiters, you can do so by
|
|
19
|
+
setting `useSemicolonDelimiter: true` in the server configuration.
|
|
20
|
+
|
|
@@ -19,7 +19,8 @@ For example, `FastifyTypeProviderDefault` will not be assignable to the followin
|
|
|
19
19
|
```ts
|
|
20
20
|
export interface NotSubstitutableTypeProvider extends FastifyTypeProvider {
|
|
21
21
|
// bad, nothing is assignable to `never` (except for itself)
|
|
22
|
-
|
|
22
|
+
validator: this['schema'] extends /** custom check here**/ ? /** narrowed type here **/ : never;
|
|
23
|
+
serializer: this['schema'] extends /** custom check here**/ ? /** narrowed type here **/ : never;
|
|
23
24
|
}
|
|
24
25
|
```
|
|
25
26
|
|
|
@@ -27,6 +28,7 @@ Unless changed to:
|
|
|
27
28
|
```ts
|
|
28
29
|
export interface SubstitutableTypeProvider extends FastifyTypeProvider {
|
|
29
30
|
// good, anything can be assigned to `unknown`
|
|
30
|
-
|
|
31
|
+
validator: this['schema'] extends /** custom check here**/ ? /** narrowed type here **/ : unknown;
|
|
32
|
+
serializer: this['schema'] extends /** custom check here**/ ? /** narrowed type here **/ : unknown;
|
|
31
33
|
}
|
|
32
34
|
```
|
|
@@ -29,6 +29,13 @@ is given in the content-type header. If it is not given, the
|
|
|
29
29
|
[catch-all](#catch-all) parser is not executed as with `POST`, `PUT` and
|
|
30
30
|
`PATCH`, but the payload is simply not parsed.
|
|
31
31
|
|
|
32
|
+
> ## ⚠ Security Notice
|
|
33
|
+
> When using with RegExp to detect `Content-Type`, you should beware of
|
|
34
|
+
> how to properly detect the `Content-Type`. For example, if you need
|
|
35
|
+
> `application/*`, you should use `/^application\/([\w-]+);?/` to match the
|
|
36
|
+
> [essence MIME type](https://mimesniff.spec.whatwg.org/#mime-type-miscellaneous)
|
|
37
|
+
> only.
|
|
38
|
+
|
|
32
39
|
### Usage
|
|
33
40
|
```js
|
|
34
41
|
fastify.addContentTypeParser('application/jsoff', function (request, payload, done) {
|
|
@@ -52,7 +59,7 @@ fastify.addContentTypeParser('application/jsoff', async function (request, paylo
|
|
|
52
59
|
})
|
|
53
60
|
|
|
54
61
|
// Handle all content types that matches RegExp
|
|
55
|
-
fastify.addContentTypeParser(/^image
|
|
62
|
+
fastify.addContentTypeParser(/^image\/([\w-]+);?/, function (request, payload, done) {
|
|
56
63
|
imageParser(payload, function (err, body) {
|
|
57
64
|
done(err, body)
|
|
58
65
|
})
|
|
@@ -80,6 +87,28 @@ fastify.addContentTypeParser('application/vnd.custom', (request, body, done) =>
|
|
|
80
87
|
fastify.addContentTypeParser('application/vnd.custom+xml', (request, body, done) => {} )
|
|
81
88
|
```
|
|
82
89
|
|
|
90
|
+
### Using addContentTypeParser with fastify.register
|
|
91
|
+
When using `addContentTypeParser` in combination with `fastify.register`,
|
|
92
|
+
`await` should not be used when registering routes. Using `await` causes
|
|
93
|
+
the route registration to be asynchronous and can lead to routes being registered
|
|
94
|
+
before the addContentTypeParser has been set.
|
|
95
|
+
|
|
96
|
+
#### Correct Usage
|
|
97
|
+
```js
|
|
98
|
+
const fastify = require('fastify')();
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
fastify.register((fastify, opts) => {
|
|
102
|
+
fastify.addContentTypeParser('application/json', function (request, payload, done) {
|
|
103
|
+
jsonParser(payload, function (err, body) {
|
|
104
|
+
done(err, body)
|
|
105
|
+
})
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
fastify.get('/hello', async (req, res) => {});
|
|
109
|
+
});
|
|
110
|
+
```
|
|
111
|
+
|
|
83
112
|
Besides the `addContentTypeParser` API there are further APIs that can be used.
|
|
84
113
|
These are `hasContentTypeParser`, `removeContentTypeParser` and
|
|
85
114
|
`removeAllContentTypeParsers`.
|
|
@@ -59,8 +59,8 @@ close as possible to the value intended to be set dynamically in the future.
|
|
|
59
59
|
Initialize a decorator as a `''` if the intended value is a string, and as
|
|
60
60
|
`null` if it will be an object or a function.
|
|
61
61
|
|
|
62
|
-
Remember this example works only with value types as reference types will
|
|
63
|
-
|
|
62
|
+
Remember this example works only with value types as reference types will
|
|
63
|
+
thrown and error during the fastify startup. See [decorateRequest](#decorate-request).
|
|
64
64
|
|
|
65
65
|
See [JavaScript engine fundamentals: Shapes and Inline
|
|
66
66
|
Caches](https://mathiasbynens.be/notes/shapes-ics) for more information on this
|
|
@@ -83,7 +83,7 @@ fastify.decorate('utility', function () {
|
|
|
83
83
|
})
|
|
84
84
|
```
|
|
85
85
|
|
|
86
|
-
As mentioned above, non-function values can be attached:
|
|
86
|
+
As mentioned above, non-function values can be attached to the server instance as:
|
|
87
87
|
|
|
88
88
|
```js
|
|
89
89
|
fastify.decorate('conf', {
|
|
@@ -177,23 +177,24 @@ fastify.decorateReply('utility', function () {
|
|
|
177
177
|
Note: using an arrow function will break the binding of `this` to the Fastify
|
|
178
178
|
`Reply` instance.
|
|
179
179
|
|
|
180
|
-
Note: using `decorateReply` will
|
|
180
|
+
Note: using `decorateReply` will throw and error if used with a reference type:
|
|
181
181
|
|
|
182
182
|
```js
|
|
183
183
|
// Don't do this
|
|
184
184
|
fastify.decorateReply('foo', { bar: 'fizz'})
|
|
185
185
|
```
|
|
186
|
-
In this example, the reference of the object
|
|
187
|
-
**any mutation will impact all requests, potentially creating security
|
|
188
|
-
vulnerabilities or memory leaks
|
|
189
|
-
|
|
190
|
-
|
|
186
|
+
In this example, the reference of the object would be shared with all the requests
|
|
187
|
+
and **any mutation will impact all requests, potentially creating security
|
|
188
|
+
vulnerabilities or memory leaks**, so Fastify blocks it.
|
|
189
|
+
|
|
190
|
+
To achieve proper encapsulation across requests configure a new value for each
|
|
191
|
+
incoming request in the [`'onRequest'` hook](./Hooks.md#onrequest). Example:
|
|
191
192
|
|
|
192
193
|
```js
|
|
193
194
|
const fp = require('fastify-plugin')
|
|
194
195
|
|
|
195
196
|
async function myPlugin (app) {
|
|
196
|
-
app.decorateRequest('foo'
|
|
197
|
+
app.decorateRequest('foo')
|
|
197
198
|
app.addHook('onRequest', async (req, reply) => {
|
|
198
199
|
req.foo = { bar: 42 }
|
|
199
200
|
})
|
|
@@ -219,24 +220,26 @@ fastify.decorateRequest('utility', function () {
|
|
|
219
220
|
Note: using an arrow function will break the binding of `this` to the Fastify
|
|
220
221
|
`Request` instance.
|
|
221
222
|
|
|
222
|
-
Note: using `decorateRequest` will emit
|
|
223
|
+
Note: using `decorateRequest` will emit an error if used with a reference type:
|
|
223
224
|
|
|
224
225
|
```js
|
|
225
226
|
// Don't do this
|
|
226
227
|
fastify.decorateRequest('foo', { bar: 'fizz'})
|
|
227
228
|
```
|
|
228
|
-
In this example, the reference of the object
|
|
229
|
-
**any mutation will impact all requests, potentially creating security
|
|
230
|
-
vulnerabilities or memory leaks
|
|
229
|
+
In this example, the reference of the object would be shared with all the requests
|
|
230
|
+
and **any mutation will impact all requests, potentially creating security
|
|
231
|
+
vulnerabilities or memory leaks**, so Fastify blocks it.
|
|
231
232
|
|
|
232
233
|
To achieve proper encapsulation across requests configure a new value for each
|
|
233
|
-
incoming request in the [`'onRequest'` hook](./Hooks.md#onrequest).
|
|
234
|
+
incoming request in the [`'onRequest'` hook](./Hooks.md#onrequest).
|
|
235
|
+
|
|
236
|
+
Example:
|
|
234
237
|
|
|
235
238
|
```js
|
|
236
239
|
const fp = require('fastify-plugin')
|
|
237
240
|
|
|
238
241
|
async function myPlugin (app) {
|
|
239
|
-
app.decorateRequest('foo'
|
|
242
|
+
app.decorateRequest('foo')
|
|
240
243
|
app.addHook('onRequest', async (req, reply) => {
|
|
241
244
|
req.foo = { bar: 42 }
|
|
242
245
|
})
|
|
@@ -245,6 +248,29 @@ async function myPlugin (app) {
|
|
|
245
248
|
module.exports = fp(myPlugin)
|
|
246
249
|
```
|
|
247
250
|
|
|
251
|
+
The hook solution is more flexible and allows for more complex initialization
|
|
252
|
+
because you can add more logic to the `onRequest` hook.
|
|
253
|
+
|
|
254
|
+
Another approach is to use the getter/setter pattern, but it requires 2 decorators:
|
|
255
|
+
|
|
256
|
+
```js
|
|
257
|
+
fastify.decorateRequest('my_decorator_holder') // define the holder
|
|
258
|
+
fastify.decorateRequest('user', {
|
|
259
|
+
getter () {
|
|
260
|
+
this.my_decorator_holder ??= {} // initialize the holder
|
|
261
|
+
return this.my_decorator_holder
|
|
262
|
+
}
|
|
263
|
+
})
|
|
264
|
+
|
|
265
|
+
fastify.get('/', async function (req, reply) {
|
|
266
|
+
req.user.access = 'granted'
|
|
267
|
+
// other code
|
|
268
|
+
})
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
This ensures that the `user` property is always unique for each
|
|
272
|
+
request.
|
|
273
|
+
|
|
248
274
|
See [`decorate`](#decorate) for information about the `dependencies` parameter.
|
|
249
275
|
|
|
250
276
|
#### `hasDecorator(name)`
|
package/docs/Reference/Errors.md
CHANGED
|
@@ -36,6 +36,7 @@
|
|
|
36
36
|
- [FST_ERR_DEC_DEPENDENCY_INVALID_TYPE](#fst_err_dec_dependency_invalid_type)
|
|
37
37
|
- [FST_ERR_DEC_MISSING_DEPENDENCY](#fst_err_dec_missing_dependency)
|
|
38
38
|
- [FST_ERR_DEC_AFTER_START](#fst_err_dec_after_start)
|
|
39
|
+
- [FST_ERR_DEC_REFERENCE_TYPE](#fst_err_dec_reference_type)
|
|
39
40
|
- [FST_ERR_HOOK_INVALID_TYPE](#fst_err_hook_invalid_type)
|
|
40
41
|
- [FST_ERR_HOOK_INVALID_HANDLER](#fst_err_hook_invalid_handler)
|
|
41
42
|
- [FST_ERR_HOOK_INVALID_ASYNC_HANDLER](#fst_err_hook_invalid_async_handler)
|
|
@@ -44,6 +45,9 @@
|
|
|
44
45
|
- [FST_ERR_HOOK_TIMEOUT](#fst_err_hook_timeout)
|
|
45
46
|
- [FST_ERR_LOG_INVALID_DESTINATION](#fst_err_log_invalid_destination)
|
|
46
47
|
- [FST_ERR_LOG_INVALID_LOGGER](#fst_err_log_invalid_logger)
|
|
48
|
+
- [FST_ERR_LOG_INVALID_LOGGER_INSTANCE](#fst_err_log_invalid_logger_instance)
|
|
49
|
+
- [FST_ERR_LOG_INVALID_LOGGER_CONFIG](#fst_err_log_invalid_logger_config)
|
|
50
|
+
- [FST_ERR_LOG_LOGGER_AND_LOGGER_INSTANCE_PROVIDED](#fst_err_log_logger_and_logger_instance_provided)
|
|
47
51
|
- [FST_ERR_REP_INVALID_PAYLOAD_TYPE](#fst_err_rep_invalid_payload_type)
|
|
48
52
|
- [FST_ERR_REP_RESPONSE_BODY_CONSUMED](#fst_err_rep_response_body_consumed)
|
|
49
53
|
- [FST_ERR_REP_ALREADY_SENT](#fst_err_rep_already_sent)
|
|
@@ -70,7 +74,6 @@
|
|
|
70
74
|
- [FST_ERR_DUPLICATED_ROUTE](#fst_err_duplicated_route)
|
|
71
75
|
- [FST_ERR_BAD_URL](#fst_err_bad_url)
|
|
72
76
|
- [FST_ERR_ASYNC_CONSTRAINT](#fst_err_async_constraint)
|
|
73
|
-
- [FST_ERR_DEFAULT_ROUTE_INVALID_TYPE](#fst_err_default_route_invalid_type)
|
|
74
77
|
- [FST_ERR_INVALID_URL](#fst_err_invalid_url)
|
|
75
78
|
- [FST_ERR_ROUTE_OPTIONS_NOT_OBJ](#fst_err_route_options_not_obj)
|
|
76
79
|
- [FST_ERR_ROUTE_DUPLICATED_HANDLER](#fst_err_route_duplicated_handler)
|
|
@@ -90,6 +93,7 @@
|
|
|
90
93
|
- [FST_ERR_PARENT_PLUGIN_BOOTED](#fst_err_parent_plugin_booted)
|
|
91
94
|
- [FST_ERR_PLUGIN_TIMEOUT](#fst_err_plugin_timeout)
|
|
92
95
|
- [FST_ERR_PLUGIN_NOT_PRESENT_IN_INSTANCE](#fst_err_plugin_not_present_in_instance)
|
|
96
|
+
- [FST_ERR_PLUGIN_INVALID_ASYNC_HANDLER](#fst_err_plugin_invalid_async_handler)
|
|
93
97
|
- [FST_ERR_VALIDATION](#fst_err_validation)
|
|
94
98
|
- [FST_ERR_LISTEN_OPTIONS_INVALID](#fst_err_listen_options_invalid)
|
|
95
99
|
- [FST_ERR_ERROR_HANDLER_NOT_FN](#fst_err_error_handler_not_fn)
|
|
@@ -305,6 +309,7 @@ Below is a table with all the error codes that Fastify uses.
|
|
|
305
309
|
| <a id="fst_err_dec_dependency_invalid_type">FST_ERR_DEC_DEPENDENCY_INVALID_TYPE</a> | The dependencies of decorator must be of type `Array`. | Use an array for the dependencies. | [#3090](https://github.com/fastify/fastify/pull/3090) |
|
|
306
310
|
| <a id="fst_err_dec_missing_dependency">FST_ERR_DEC_MISSING_DEPENDENCY</a> | The decorator cannot be registered due to a missing dependency. | Register the missing dependency. | [#1168](https://github.com/fastify/fastify/pull/1168) |
|
|
307
311
|
| <a id="fst_err_dec_after_start">FST_ERR_DEC_AFTER_START</a> | The decorator cannot be added after start. | Add the decorator before starting the server. | [#2128](https://github.com/fastify/fastify/pull/2128) |
|
|
312
|
+
| <a id="fst_err_dec_reference_type">FST_ERR_DEC_REFERENCE_TYPE</a> | The decorator cannot be a reference type. | Define the decorator with a getter/setter interface or an empty decorator with a hook. | [#5462](https://github.com/fastify/fastify/pull/5462) |
|
|
308
313
|
| <a id="fst_err_hook_invalid_type">FST_ERR_HOOK_INVALID_TYPE</a> | The hook name must be a string. | Use a string for the hook name. | [#1168](https://github.com/fastify/fastify/pull/1168) |
|
|
309
314
|
| <a id="fst_err_hook_invalid_handler">FST_ERR_HOOK_INVALID_HANDLER</a> | The hook callback must be a function. | Use a function for the hook callback. | [#1168](https://github.com/fastify/fastify/pull/1168) |
|
|
310
315
|
| <a id="fst_err_hook_invalid_async_handler">FST_ERR_HOOK_INVALID_ASYNC_HANDLER</a> | Async function has too many arguments. Async hooks should not use the `done` argument. | Remove the `done` argument from the async hook. | [#4367](https://github.com/fastify/fastify/pull/4367) |
|
|
@@ -313,6 +318,9 @@ Below is a table with all the error codes that Fastify uses.
|
|
|
313
318
|
| <a id="fst_err_hook_timeout">FST_ERR_HOOK_TIMEOUT</a> | A callback for a hook timed out. | Increase the timeout for the hook. | [#3106](https://github.com/fastify/fastify/pull/3106) |
|
|
314
319
|
| <a id="fst_err_log_invalid_destination">FST_ERR_LOG_INVALID_DESTINATION</a> | The logger does not accept the specified destination. | Use a `'stream'` or a `'file'` as the destination. | [#1168](https://github.com/fastify/fastify/pull/1168) |
|
|
315
320
|
| <a id="fst_err_log_invalid_logger">FST_ERR_LOG_INVALID_LOGGER</a> | The logger should have all these methods: `'info'`, `'error'`, `'debug'`, `'fatal'`, `'warn'`, `'trace'`, `'child'`. | Use a logger with all the required methods. | [#4520](https://github.com/fastify/fastify/pull/4520) |
|
|
321
|
+
| <a id="fst_err_log_invalid_logger_instance">FST_ERR_LOG_INVALID_LOGGER_INSTANCE</a> | The `loggerInstance` only accepts a logger instance, not a configuration object. | To pass a configuration object, use `'logger'` instead. | [#5020](https://github.com/fastify/fastify/pull/5020) |
|
|
322
|
+
| <a id="fst_err_log_invalid_logger_config">FST_ERR_LOG_INVALID_LOGGER_CONFIG</a> | The logger option only accepts a configuration object, not a logger instance. | To pass an instance, use `'loggerInstance'` instead. | [#5020](https://github.com/fastify/fastify/pull/5020) |
|
|
323
|
+
| <a id="fst_err_log_logger_and_logger_instance_provided">FST_ERR_LOG_LOGGER_AND_LOGGER_INSTANCE_PROVIDED</a> | You cannot provide both `'logger'` and `'loggerInstance'`. | Please provide only one option. | [#5020](https://github.com/fastify/fastify/pull/5020) |
|
|
316
324
|
| <a id="fst_err_rep_invalid_payload_type">FST_ERR_REP_INVALID_PAYLOAD_TYPE</a> | Reply payload can be either a `string` or a `Buffer`. | Use a `string` or a `Buffer` for the payload. | [#1168](https://github.com/fastify/fastify/pull/1168) |
|
|
317
325
|
| <a id="fst_err_rep_response_body_consumed">FST_ERR_REP_RESPONSE_BODY_CONSUMED</a> | Using `Response` as reply payload, but the body is being consumed. | Make sure you don't consume the `Response.body` | [#5286](https://github.com/fastify/fastify/pull/5286) |
|
|
318
326
|
| <a id="fst_err_rep_already_sent">FST_ERR_REP_ALREADY_SENT</a> | A response was already sent. | - | [#1336](https://github.com/fastify/fastify/pull/1336) |
|
|
@@ -339,7 +347,6 @@ Below is a table with all the error codes that Fastify uses.
|
|
|
339
347
|
| <a id="fst_err_duplicated_route">FST_ERR_DUPLICATED_ROUTE</a> | The HTTP method already has a registered controller for that URL. | Use a different URL or register the controller for another HTTP method. | [#2954](https://github.com/fastify/fastify/pull/2954) |
|
|
340
348
|
| <a id="fst_err_bad_url">FST_ERR_BAD_URL</a> | The router received an invalid URL. | Use a valid URL. | [#2106](https://github.com/fastify/fastify/pull/2106) |
|
|
341
349
|
| <a id="fst_err_async_constraint">FST_ERR_ASYNC_CONSTRAINT</a> | The router received an error when using asynchronous constraints. | - | [#4323](https://github.com/fastify/fastify/pull/4323) |
|
|
342
|
-
| <a id="fst_err_default_route_invalid_type">FST_ERR_DEFAULT_ROUTE_INVALID_TYPE</a> | The `defaultRoute` type should be a function. | Use a function for the `defaultRoute`. | [#2733](https://github.com/fastify/fastify/pull/2733) |
|
|
343
350
|
| <a id="fst_err_invalid_url">FST_ERR_INVALID_URL</a> | URL must be a string. | Use a string for the URL. | [#3653](https://github.com/fastify/fastify/pull/3653) |
|
|
344
351
|
| <a id="fst_err_route_options_not_obj">FST_ERR_ROUTE_OPTIONS_NOT_OBJ</a> | Options for the route must be an object. | Use an object for the route options. | [#4554](https://github.com/fastify/fastify/pull/4554) |
|
|
345
352
|
| <a id="fst_err_route_duplicated_handler">FST_ERR_ROUTE_DUPLICATED_HANDLER</a> | Duplicate handler for the route is not allowed. | Use a different handler. | [#4554](https://github.com/fastify/fastify/pull/4554) |
|
|
@@ -359,6 +366,7 @@ Below is a table with all the error codes that Fastify uses.
|
|
|
359
366
|
| <a id="fst_err_parent_plugin_booted">FST_ERR_PARENT_PLUGIN_BOOTED</a> | Impossible to load plugin because the parent (mapped directly from `avvio`) | - | [#3106](https://github.com/fastify/fastify/pull/3106) |
|
|
360
367
|
| <a id="fst_err_plugin_timeout">FST_ERR_PLUGIN_TIMEOUT</a> | Plugin did not start in time. | Increase the timeout for the plugin. | [#3106](https://github.com/fastify/fastify/pull/3106) |
|
|
361
368
|
| <a id="fst_err_plugin_not_present_in_instance">FST_ERR_PLUGIN_NOT_PRESENT_IN_INSTANCE</a> | The decorator is not present in the instance. | - | [#4554](https://github.com/fastify/fastify/pull/4554) |
|
|
369
|
+
| <a id="fst_err_plugin_invalid_async_handler">FST_ERR_PLUGIN_INVALID_ASYNC_HANDLER</a> | The plugin being registered mixes async and callback styles. | - | [#5141](https://github.com/fastify/fastify/pull/5141) |
|
|
362
370
|
| <a id="fst_err_validation">FST_ERR_VALIDATION</a> | The Request failed the payload validation. | Check the request payload. | [#4824](https://github.com/fastify/fastify/pull/4824) |
|
|
363
371
|
| <a id="fst_err_listen_options_invalid">FST_ERR_LISTEN_OPTIONS_INVALID</a> | Invalid listen options. | Check the listen options. | [#4886](https://github.com/fastify/fastify/pull/4886) |
|
|
364
372
|
| <a id="fst_err_error_handler_not_fn">FST_ERR_ERROR_HANDLER_NOT_FN</a> | Error Handler must be a function | Provide a function to `setErrorHandler`. | [#5317](https://github.com/fastify/fastify/pull/5317) |
|
package/docs/Reference/Hooks.md
CHANGED
|
@@ -829,18 +829,17 @@ consider creating a custom [Plugin](./Plugins.md) instead.
|
|
|
829
829
|
## Diagnostics Channel Hooks
|
|
830
830
|
|
|
831
831
|
> **Note:** The `diagnostics_channel` is currently experimental on Node.js, so
|
|
832
|
-
> its API is subject to change even in semver-patch releases of Node.js.
|
|
833
|
-
> versions of Node.js supported by Fastify where `diagnostics_channel` is
|
|
834
|
-
> unavailable, the hook
|
|
835
|
-
> [polyfill](https://www.npmjs.com/package/
|
|
836
|
-
>
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
other sort of modification.
|
|
832
|
+
> its API is subject to change even in semver-patch releases of Node.js. As some
|
|
833
|
+
> versions of Node.js are supported by Fastify where `diagnostics_channel` is
|
|
834
|
+
> unavailable, or with an incomplete feature set, the hook uses the
|
|
835
|
+
> [dc-polyfill](https://www.npmjs.com/package/dc-polyfill) package to provide a
|
|
836
|
+
> polyfill.
|
|
837
|
+
|
|
838
|
+
One [`diagnostics_channel`](https://nodejs.org/api/diagnostics_channel.html)
|
|
839
|
+
publish event, `'fastify.initialization'`, happens at initialization time. The
|
|
840
|
+
Fastify instance is passed into the hook as a property of the object passed in.
|
|
841
|
+
At this point, the instance can be interacted with to add hooks, plugins,
|
|
842
|
+
routes, or any other sort of modification.
|
|
844
843
|
|
|
845
844
|
For example, a tracing package might do something like the following (which is,
|
|
846
845
|
of course, a simplification). This would be in a file loaded in the
|
|
@@ -849,13 +848,13 @@ tools first" fashion.
|
|
|
849
848
|
|
|
850
849
|
```js
|
|
851
850
|
const tracer = /* retrieved from elsewhere in the package */
|
|
852
|
-
const dc = require('node:diagnostics_channel')
|
|
851
|
+
const dc = require('node:diagnostics_channel') // or require('dc-polyfill')
|
|
853
852
|
const channel = dc.channel('fastify.initialization')
|
|
854
853
|
const spans = new WeakMap()
|
|
855
854
|
|
|
856
855
|
channel.subscribe(function ({ fastify }) {
|
|
857
856
|
fastify.addHook('onRequest', (request, reply, done) => {
|
|
858
|
-
const span = tracer.startSpan('fastify.request')
|
|
857
|
+
const span = tracer.startSpan('fastify.request.handler')
|
|
859
858
|
spans.set(request, span)
|
|
860
859
|
done()
|
|
861
860
|
})
|
|
@@ -867,3 +866,38 @@ channel.subscribe(function ({ fastify }) {
|
|
|
867
866
|
})
|
|
868
867
|
})
|
|
869
868
|
```
|
|
869
|
+
|
|
870
|
+
Five other events are published on a per-request basis following the
|
|
871
|
+
[Tracing Channel](https://nodejs.org/api/diagnostics_channel.html#class-tracingchannel)
|
|
872
|
+
nomenclature. The list of the channel names and the event they receive is:
|
|
873
|
+
|
|
874
|
+
- `tracing:fastify.request.handler:start`: Always fires
|
|
875
|
+
- `{ request: Request, reply: Reply, route: { url, method } }`
|
|
876
|
+
- `tracing:fastify.request.handler:end`: Always fires
|
|
877
|
+
- `{ request: Request, reply: Reply, route: { url, method }, async: Bool }`
|
|
878
|
+
- `tracing:fastify.request.handler:asyncStart`: Fires for promise/async handlers
|
|
879
|
+
- `{ request: Request, reply: Reply, route: { url, method } }`
|
|
880
|
+
- `tracing:fastify.request.handler:asyncEnd`: Fires for promise/async handlers
|
|
881
|
+
- `{ request: Request, reply: Reply, route: { url, method } }`
|
|
882
|
+
- `tracing:fastify.request.handler:error`: Fires when an error occurs
|
|
883
|
+
- `{ request: Request, reply: Reply, route: { url, method }, error: Error }`
|
|
884
|
+
|
|
885
|
+
The object instance remains the same for all events associated with a given
|
|
886
|
+
request. All payloads include a `request` and `reply` property which are an
|
|
887
|
+
instance of Fastify's `Request` and `Reply` instances. They also include a
|
|
888
|
+
`route` property which is an object with the matched `url` pattern (e.g.
|
|
889
|
+
`/collection/:id`) and the `method` HTTP method (e.g. `GET`). The `:start` and
|
|
890
|
+
`:end` events always fire for requests. If a request handler is an `async`
|
|
891
|
+
function or one that returns a `Promise` then the `:asyncStart` and `:asyncEnd`
|
|
892
|
+
events also fire. Finally, the `:error` event contains an `error` property
|
|
893
|
+
associated with the request's failure.
|
|
894
|
+
|
|
895
|
+
These events can be received like so:
|
|
896
|
+
|
|
897
|
+
```js
|
|
898
|
+
const dc = require('node:diagnostics_channel') // or require('dc-polyfill')
|
|
899
|
+
const channel = dc.channel('tracing:fastify.request.handler:start')
|
|
900
|
+
channel.subscribe((msg) => {
|
|
901
|
+
console.log(msg.request, msg.reply)
|
|
902
|
+
})
|
|
903
|
+
```
|
|
@@ -98,10 +98,10 @@ const fastify = require('fastify')({
|
|
|
98
98
|
<a id="logging-request-id"></a>
|
|
99
99
|
|
|
100
100
|
By default, Fastify adds an ID to every request for easier tracking. If the
|
|
101
|
-
|
|
102
|
-
is generated. See Fastify
|
|
103
|
-
[`requestIdHeader`](./Server.md#factory-request-id-header) and Fastify
|
|
104
|
-
[`genReqId`](./Server.md#genreqid) for customization options.
|
|
101
|
+
requestIdHeader-option is set and the corresponding header is present than
|
|
102
|
+
its value is used, otherwise a new incremental ID is generated. See Fastify
|
|
103
|
+
Factory [`requestIdHeader`](./Server.md#factory-request-id-header) and Fastify
|
|
104
|
+
Factory [`genReqId`](./Server.md#genreqid) for customization options.
|
|
105
105
|
|
|
106
106
|
The default logger is configured with a set of standard serializers that
|
|
107
107
|
serialize objects with `req`, `res`, and `err` properties. The object received
|
|
@@ -243,7 +243,7 @@ const fastify = Fastify({
|
|
|
243
243
|
method: request.method,
|
|
244
244
|
url: request.url,
|
|
245
245
|
headers: request.headers,
|
|
246
|
-
|
|
246
|
+
host: request.host,
|
|
247
247
|
remoteAddress: request.ip,
|
|
248
248
|
remotePort: request.socket.remotePort
|
|
249
249
|
}
|
package/docs/Reference/Reply.md
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
- [.trailer(key, function)](#trailerkey-function)
|
|
18
18
|
- [.hasTrailer(key)](#hastrailerkey)
|
|
19
19
|
- [.removeTrailer(key)](#removetrailerkey)
|
|
20
|
-
- [.redirect([code ,]
|
|
20
|
+
- [.redirect(dest, [code ,])](#redirectdest--code)
|
|
21
21
|
- [.callNotFound()](#callnotfound)
|
|
22
22
|
- [.getResponseTime()](#getresponsetime)
|
|
23
23
|
- [.type(contentType)](#typecontenttype)
|
|
@@ -58,12 +58,14 @@ since the request was received by Fastify.
|
|
|
58
58
|
- `.getHeaders()` - Gets a shallow copy of all current response headers.
|
|
59
59
|
- `.removeHeader(key)` - Remove the value of a previously set header.
|
|
60
60
|
- `.hasHeader(name)` - Determine if a header has been set.
|
|
61
|
+
- `.writeEarlyHints(hints, callback)` - Sends early hints to the user
|
|
62
|
+
while the response is being prepared.
|
|
61
63
|
- `.trailer(key, function)` - Sets a response trailer.
|
|
62
64
|
- `.hasTrailer(key)` - Determine if a trailer has been set.
|
|
63
65
|
- `.removeTrailer(key)` - Remove the value of a previously set trailer.
|
|
64
66
|
- `.type(value)` - Sets the header `Content-Type`.
|
|
65
|
-
- `.redirect([code,]
|
|
66
|
-
optional (
|
|
67
|
+
- `.redirect(dest, [code,])` - Redirect to the specified URL, the status code is
|
|
68
|
+
optional (defaults to `302`).
|
|
67
69
|
- `.callNotFound()` - Invokes the custom not found handler.
|
|
68
70
|
- `.serialize(payload)` - Serializes the specified payload using the default
|
|
69
71
|
JSON serializer or using the custom serializer (if one is set) and returns the
|
|
@@ -90,8 +92,6 @@ since the request was received by Fastify.
|
|
|
90
92
|
from Node core.
|
|
91
93
|
- `.log` - The logger instance of the incoming request.
|
|
92
94
|
- `.request` - The incoming request.
|
|
93
|
-
- `.getResponseTime()` - Deprecated, returns the amount of time passed
|
|
94
|
-
since the request was received by Fastify.
|
|
95
95
|
- `.context` - Deprecated, access the [Request's context](./Request.md) property.
|
|
96
96
|
|
|
97
97
|
```js
|
|
@@ -243,6 +243,27 @@ reply.getHeader('x-foo') // undefined
|
|
|
243
243
|
|
|
244
244
|
Returns a boolean indicating if the specified header has been set.
|
|
245
245
|
|
|
246
|
+
### .writeEarlyHints(hints, callback)
|
|
247
|
+
<a id="writeEarlyHints"></a>
|
|
248
|
+
|
|
249
|
+
Sends early hints to the client. Early hints allow the client to
|
|
250
|
+
start processing resources before the final response is sent.
|
|
251
|
+
This can improve performance by allowing the client to preload
|
|
252
|
+
or preconnect to resources while the server is still generating the response.
|
|
253
|
+
|
|
254
|
+
The hints parameter is an object containing the early hint key-value pairs.
|
|
255
|
+
|
|
256
|
+
Example:
|
|
257
|
+
|
|
258
|
+
```js
|
|
259
|
+
reply.writeEarlyHints({
|
|
260
|
+
Link: '</styles.css>; rel=preload; as=style'
|
|
261
|
+
});
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
The optional callback parameter is a function that will be called
|
|
265
|
+
once the hint is sent or if an error occurs.
|
|
266
|
+
|
|
246
267
|
### .trailer(key, function)
|
|
247
268
|
<a id="trailer"></a>
|
|
248
269
|
|
|
@@ -299,7 +320,7 @@ reply.getTrailer('server-timing') // undefined
|
|
|
299
320
|
```
|
|
300
321
|
|
|
301
322
|
|
|
302
|
-
### .redirect([code ,]
|
|
323
|
+
### .redirect(dest, [code ,])
|
|
303
324
|
<a id="redirect"></a>
|
|
304
325
|
|
|
305
326
|
Redirects a request to the specified URL, the status code is optional, default
|
|
@@ -320,7 +341,7 @@ reply.redirect('/home')
|
|
|
320
341
|
Example (no `reply.code()` call) sets status code to `303` and redirects to
|
|
321
342
|
`/home`
|
|
322
343
|
```js
|
|
323
|
-
reply.redirect(
|
|
344
|
+
reply.redirect('/home', 303)
|
|
324
345
|
```
|
|
325
346
|
|
|
326
347
|
Example (`reply.code()` call) sets status code to `303` and redirects to `/home`
|
|
@@ -330,7 +351,7 @@ reply.code(303).redirect('/home')
|
|
|
330
351
|
|
|
331
352
|
Example (`reply.code()` call) sets status code to `302` and redirects to `/home`
|
|
332
353
|
```js
|
|
333
|
-
reply.code(303).redirect(
|
|
354
|
+
reply.code(303).redirect('/home', 302)
|
|
334
355
|
```
|
|
335
356
|
|
|
336
357
|
### .callNotFound()
|
|
@@ -343,22 +364,6 @@ hook specified in [`setNotFoundHandler`](./Server.md#set-not-found-handler).
|
|
|
343
364
|
reply.callNotFound()
|
|
344
365
|
```
|
|
345
366
|
|
|
346
|
-
### .getResponseTime()
|
|
347
|
-
<a id="getResponseTime"></a>
|
|
348
|
-
|
|
349
|
-
Invokes the custom response time getter to calculate the amount of time passed
|
|
350
|
-
since the request was received by Fastify.
|
|
351
|
-
|
|
352
|
-
Note that unless this function is called in the [`onResponse`
|
|
353
|
-
hook](./Hooks.md#onresponse) it will always return `0`.
|
|
354
|
-
|
|
355
|
-
```js
|
|
356
|
-
const milliseconds = reply.getResponseTime()
|
|
357
|
-
```
|
|
358
|
-
|
|
359
|
-
*Note: This method is deprecated and will be removed in `fastify@5`.
|
|
360
|
-
Use the [.elapsedTime](#elapsedtime) property instead.*
|
|
361
|
-
|
|
362
367
|
### .type(contentType)
|
|
363
368
|
<a id="type"></a>
|
|
364
369
|
|
|
@@ -20,10 +20,12 @@ Request is a core Fastify object containing the following fields:
|
|
|
20
20
|
- `ips` - an array of the IP addresses, ordered from closest to furthest, in the
|
|
21
21
|
`X-Forwarded-For` header of the incoming request (only when the
|
|
22
22
|
[`trustProxy`](./Server.md#factory-trust-proxy) option is enabled)
|
|
23
|
-
- `
|
|
23
|
+
- `host` - the host of the incoming request (derived from `X-Forwarded-Host`
|
|
24
24
|
header when the [`trustProxy`](./Server.md#factory-trust-proxy) option is
|
|
25
25
|
enabled). For HTTP/2 compatibility it returns `:authority` if no host header
|
|
26
26
|
exists.
|
|
27
|
+
- `hostname` - the host of the incoming request without the port
|
|
28
|
+
- `port` - the port that the server is listening on
|
|
27
29
|
- `protocol` - the protocol of the incoming request (`https` or `http`)
|
|
28
30
|
- `method` - the method of the incoming request
|
|
29
31
|
- `url` - the URL of the incoming request
|
|
@@ -104,7 +106,9 @@ fastify.post('/:params', options, function (request, reply) {
|
|
|
104
106
|
console.log(request.id)
|
|
105
107
|
console.log(request.ip)
|
|
106
108
|
console.log(request.ips)
|
|
109
|
+
console.log(request.host)
|
|
107
110
|
console.log(request.hostname)
|
|
111
|
+
console.log(request.port)
|
|
108
112
|
console.log(request.protocol)
|
|
109
113
|
console.log(request.url)
|
|
110
114
|
console.log(request.routeOptions.method)
|