fastify 5.8.5 → 5.10.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 (126) hide show
  1. package/PROJECT_CHARTER.md +1 -1
  2. package/README.md +7 -10
  3. package/SECURITY.md +1 -1
  4. package/SPONSORS.md +6 -4
  5. package/build/build-validation.js +2 -2
  6. package/docs/Guides/Database.md +0 -28
  7. package/docs/Guides/Delay-Accepting-Requests.md +1 -1
  8. package/docs/Guides/Ecosystem.md +23 -9
  9. package/docs/Guides/Getting-Started.md +2 -2
  10. package/docs/Guides/Migration-Guide-V4.md +2 -2
  11. package/docs/Guides/Migration-Guide-V5.md +1 -1
  12. package/docs/Guides/Plugins-Guide.md +1 -1
  13. package/docs/Guides/Prototype-Poisoning.md +3 -3
  14. package/docs/Guides/Serverless.md +8 -15
  15. package/docs/Guides/Style-Guide.md +1 -1
  16. package/docs/Guides/Write-Plugin.md +1 -1
  17. package/docs/Reference/Encapsulation.md +27 -26
  18. package/docs/Reference/Errors.md +12 -4
  19. package/docs/Reference/HTTP2.md +10 -10
  20. package/docs/Reference/Hooks.md +5 -5
  21. package/docs/Reference/Index.md +14 -16
  22. package/docs/Reference/LTS.md +12 -13
  23. package/docs/Reference/Lifecycle.md +9 -8
  24. package/docs/Reference/Logging.md +47 -39
  25. package/docs/Reference/Middleware.md +21 -25
  26. package/docs/Reference/Principles.md +2 -2
  27. package/docs/Reference/Reply.md +6 -1
  28. package/docs/Reference/Request.md +29 -18
  29. package/docs/Reference/Routes.md +5 -2
  30. package/docs/Reference/Server.md +138 -11
  31. package/docs/Reference/Type-Providers.md +53 -9
  32. package/docs/Reference/TypeScript.md +3 -3
  33. package/docs/Reference/Validation-and-Serialization.md +15 -2
  34. package/docs/Reference/Warnings.md +11 -6
  35. package/eslint.config.js +7 -2
  36. package/fastify.d.ts +13 -3
  37. package/fastify.js +60 -31
  38. package/lib/content-type-parser.js +2 -0
  39. package/lib/content-type.js +34 -1
  40. package/lib/context.js +0 -3
  41. package/lib/decorate.js +11 -3
  42. package/lib/error-handler.js +10 -28
  43. package/lib/error-serializer.js +59 -59
  44. package/lib/errors.js +23 -1
  45. package/lib/four-oh-four.js +22 -19
  46. package/lib/handle-request.js +12 -5
  47. package/lib/log-controller.js +169 -0
  48. package/lib/logger-factory.js +25 -4
  49. package/lib/plugin-override.js +2 -1
  50. package/lib/plugin-utils.js +5 -5
  51. package/lib/reply.js +96 -50
  52. package/lib/req-id-gen-factory.js +4 -1
  53. package/lib/request.js +16 -6
  54. package/lib/route.js +47 -41
  55. package/lib/schema-controller.js +1 -1
  56. package/lib/schemas.js +37 -30
  57. package/lib/symbols.js +4 -2
  58. package/lib/validation.js +10 -13
  59. package/lib/warnings.js +22 -4
  60. package/package.json +15 -17
  61. package/scripts/validate-ecosystem-links.js +1 -0
  62. package/test/bundler/esbuild/package.json +1 -1
  63. package/test/close-pipelining.test.js +1 -2
  64. package/test/content-type.test.js +20 -0
  65. package/test/custom-http-server.test.js +38 -0
  66. package/test/decorator-instance-properties.test.js +63 -0
  67. package/test/diagnostics-channel/async-error-handler.test.js +74 -0
  68. package/test/genReqId.test.js +24 -0
  69. package/test/hooks.test.js +94 -0
  70. package/test/http-methods/get.test.js +1 -1
  71. package/test/http2/plain.test.js +135 -0
  72. package/test/http2/secure-with-fallback.test.js +1 -1
  73. package/test/https/https.test.js +1 -2
  74. package/test/internals/errors.test.js +31 -1
  75. package/test/internals/logger.test.js +322 -0
  76. package/test/internals/plugin.test.js +3 -1
  77. package/test/internals/reply.test.js +35 -4
  78. package/test/internals/request.test.js +37 -10
  79. package/test/internals/schema-controller-perf.test.js +33 -0
  80. package/test/logger/logging.test.js +57 -1
  81. package/test/logger/options.test.js +38 -1
  82. package/test/reply-error.test.js +1 -1
  83. package/test/reply-trailers.test.js +70 -0
  84. package/test/request-media-type.test.js +105 -0
  85. package/test/route-prefix.test.js +34 -0
  86. package/test/router-options.test.js +222 -11
  87. package/test/schema-serialization.test.js +108 -0
  88. package/test/schema-validation.test.js +24 -0
  89. package/test/scripts/validate-ecosystem-links.test.js +40 -57
  90. package/test/stream.4.test.js +2 -2
  91. package/test/throw.test.js +14 -0
  92. package/test/trust-proxy.test.js +70 -30
  93. package/test/types/content-type-parser.tst.ts +70 -0
  94. package/test/types/{decorate-request-reply.test-d.ts → decorate-request-reply.tst.ts} +4 -4
  95. package/test/types/{dummy-plugin.ts → dummy-plugin.mts} +1 -1
  96. package/test/types/errors.tst.ts +91 -0
  97. package/test/types/fastify.tst.ts +351 -0
  98. package/test/types/hooks.tst.ts +578 -0
  99. package/test/types/instance.tst.ts +597 -0
  100. package/test/types/{logger.test-d.ts → logger.tst.ts} +61 -62
  101. package/test/types/plugin.tst.ts +96 -0
  102. package/test/types/register.tst.ts +245 -0
  103. package/test/types/reply.tst.ts +297 -0
  104. package/test/types/request.tst.ts +199 -0
  105. package/test/types/route.tst.ts +576 -0
  106. package/test/types/{schema.test-d.ts → schema.tst.ts} +22 -22
  107. package/test/types/{serverFactory.test-d.ts → serverFactory.tst.ts} +4 -4
  108. package/test/types/tsconfig.json +9 -0
  109. package/test/types/{type-provider.test-d.ts → type-provider.tst.ts} +256 -250
  110. package/test/types/using.tst.ts +14 -0
  111. package/types/errors.d.ts +4 -0
  112. package/types/instance.d.ts +2 -0
  113. package/types/logger.d.ts +22 -0
  114. package/types/request.d.ts +23 -2
  115. package/test/types/content-type-parser.test-d.ts +0 -72
  116. package/test/types/errors.test-d.ts +0 -90
  117. package/test/types/fastify.test-d.ts +0 -352
  118. package/test/types/hooks.test-d.ts +0 -550
  119. package/test/types/import.ts +0 -2
  120. package/test/types/instance.test-d.ts +0 -588
  121. package/test/types/plugin.test-d.ts +0 -97
  122. package/test/types/register.test-d.ts +0 -237
  123. package/test/types/reply.test-d.ts +0 -254
  124. package/test/types/request.test-d.ts +0 -188
  125. package/test/types/route.test-d.ts +0 -553
  126. package/test/types/using.test-d.ts +0 -17
@@ -0,0 +1,14 @@
1
+ import { expect, test } from 'tstyche'
2
+ import fastify, { FastifyInstance } from '../../fastify.js'
3
+
4
+ test("has 'Symbol.dispose' when declared with 'using'", async () => {
5
+ await using app = fastify()
6
+ expect(app).type.toBeAssignableTo<FastifyInstance>()
7
+ expect(app[Symbol.asyncDispose]).type.toBe<() => Promise<undefined>>()
8
+ })
9
+
10
+ test("has 'Symbol.dispose'", async () => {
11
+ await using app = fastify()
12
+ expect(app).type.toBeAssignableTo<FastifyInstance>()
13
+ expect(app[Symbol.asyncDispose]).type.toBe<() => Promise<undefined>>()
14
+ })
package/types/errors.d.ts CHANGED
@@ -40,6 +40,7 @@ export type FastifyErrorCodes = Record<
40
40
  'FST_ERR_LOG_INVALID_LOGGER_INSTANCE' |
41
41
  'FST_ERR_LOG_INVALID_LOGGER_CONFIG' |
42
42
  'FST_ERR_LOG_LOGGER_AND_LOGGER_INSTANCE_PROVIDED' |
43
+ 'FST_ERR_LOG_INVALID_LOG_CONTROLLER' |
43
44
  'FST_ERR_REP_INVALID_PAYLOAD_TYPE' |
44
45
  'FST_ERR_REP_RESPONSE_BODY_CONSUMED' |
45
46
  'FST_ERR_REP_READABLE_STREAM_LOCKED' |
@@ -65,6 +66,7 @@ export type FastifyErrorCodes = Record<
65
66
  'FST_ERR_FORCE_CLOSE_CONNECTIONS_IDLE_NOT_AVAILABLE' |
66
67
  'FST_ERR_DUPLICATED_ROUTE' |
67
68
  'FST_ERR_BAD_URL' |
69
+ 'FST_ERR_MAX_PARAM_LENGTH' |
68
70
  'FST_ERR_ASYNC_CONSTRAINT' |
69
71
  'FST_ERR_INVALID_URL' |
70
72
  'FST_ERR_ROUTE_OPTIONS_NOT_OBJ' |
@@ -73,6 +75,7 @@ export type FastifyErrorCodes = Record<
73
75
  'FST_ERR_ROUTE_MISSING_HANDLER' |
74
76
  'FST_ERR_ROUTE_METHOD_INVALID' |
75
77
  'FST_ERR_ROUTE_METHOD_NOT_SUPPORTED' |
78
+ 'FST_ERR_ROUTE_LOG_LEVEL_INVALID' |
76
79
  'FST_ERR_ROUTE_BODY_VALIDATION_SCHEMA_NOT_SUPPORTED' |
77
80
  'FST_ERR_ROUTE_BODY_LIMIT_OPTION_NOT_INT' |
78
81
  'FST_ERR_HANDLER_TIMEOUT' |
@@ -84,6 +87,7 @@ export type FastifyErrorCodes = Record<
84
87
  'FST_ERR_PLUGIN_VERSION_MISMATCH' |
85
88
  'FST_ERR_PLUGIN_NOT_PRESENT_IN_INSTANCE' |
86
89
  'FST_ERR_PLUGIN_INVALID_ASYNC_HANDLER' |
90
+ 'FST_ERR_PLUGIN_DEPENDENCY_NOT_REGISTERED' |
87
91
  'FST_ERR_PLUGIN_CALLBACK_NOT_FN' |
88
92
  'FST_ERR_PLUGIN_NOT_VALID' |
89
93
  'FST_ERR_ROOT_PLG_BOOTED' |
@@ -595,12 +595,14 @@ export interface FastifyInstance<
595
595
  https?: boolean | Readonly<{ allowHTTP1: boolean }>,
596
596
  ignoreTrailingSlash?: boolean,
597
597
  ignoreDuplicateSlashes?: boolean,
598
+ /** @deprecated Use the `logController` option with `disableRequestLogging` or `isLogDisabled` override instead. Will be removed in `fastify@6`. */
598
599
  disableRequestLogging?: boolean | ((req: FastifyRequest) => boolean),
599
600
  maxParamLength?: number,
600
601
  onProtoPoisoning?: ProtoAction,
601
602
  onConstructorPoisoning?: ConstructorAction,
602
603
  pluginTimeout?: number,
603
604
  requestIdHeader?: string | false,
605
+ /** @deprecated Use the `logController` option with `requestIdLogLabel` instead. Will be removed in `fastify@6`. */
604
606
  requestIdLogLabel?: string,
605
607
  http2SessionTimeout?: number,
606
608
  useSemicolonDelimiter?: boolean,
package/types/logger.d.ts CHANGED
@@ -82,6 +82,28 @@ export interface FastifyLoggerOptions<
82
82
  stream?: FastifyLoggerStreamDestination;
83
83
  }
84
84
 
85
+ export interface LogControllerOptions {
86
+ disableRequestLogging?: boolean | ((req: FastifyRequest) => boolean)
87
+ requestIdLogLabel?: string
88
+ }
89
+
90
+ export declare class LogController {
91
+ disableRequestLogging: boolean | ((req: FastifyRequest) => boolean)
92
+ requestIdLogLabel: string
93
+
94
+ constructor (options?: LogControllerOptions)
95
+
96
+ isLogDisabled (request: FastifyRequest): boolean
97
+ incomingRequest (request: FastifyRequest, reply: FastifyReply, metadata?: Record<string, unknown>): void
98
+ requestCompleted (error: Error | null, request: FastifyRequest, reply: FastifyReply, metadata?: Record<string, unknown>): void
99
+ defaultErrorLog (error: Error, request: FastifyRequest, reply: FastifyReply, metadata?: Record<string, unknown>): void
100
+ streamError (error: Error, request: FastifyRequest, reply: FastifyReply, metadata?: Record<string, unknown>): void
101
+ routeNotFound (request: FastifyRequest, reply: FastifyReply, metadata?: Record<string, unknown>): void
102
+ writeHeadError (error: Error, request: FastifyRequest, reply: FastifyReply, metadata?: Record<string, unknown>): void
103
+ serializerError (error: Error, request: FastifyRequest, reply: FastifyReply, metadata: { statusCode: number }): void
104
+ serviceUnavailable (logger: FastifyBaseLogger, server: FastifyInstance): void
105
+ }
106
+
85
107
  export interface FastifyChildLoggerFactory<
86
108
  RawServer extends RawServerBase = RawServerDefault,
87
109
  RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
@@ -71,22 +71,43 @@ export interface FastifyRequest<RouteGeneric extends RouteGenericInterface = Rou
71
71
  * @deprecated Use `raw` property
72
72
  */
73
73
  readonly req: RawRequest & RouteGeneric['Headers']; // this enables the developer to extend the existing http(s|2) headers list
74
+ /**
75
+ * Derived from request socket metadata or forwarding headers.
76
+ * Treat as untrusted input and validate before security-sensitive use.
77
+ */
74
78
  readonly ip: string;
79
+ /**
80
+ * Derived from forwarding headers when trustProxy is enabled.
81
+ * Treat as untrusted input and validate before security-sensitive use.
82
+ */
75
83
  readonly ips?: string[];
84
+ /**
85
+ * Derived from Host/:authority/X-Forwarded-Host request metadata.
86
+ * Treat as untrusted input and validate before security-sensitive use.
87
+ */
76
88
  readonly host: string;
89
+ /**
90
+ * Parsed from request host metadata.
91
+ * Treat as untrusted input and validate before security-sensitive use.
92
+ */
77
93
  readonly port: number | null;
78
94
  readonly hostname: string;
79
95
  readonly url: string;
80
96
  readonly originalUrl: string;
97
+ /**
98
+ * Derived from socket state or forwarding headers.
99
+ * Treat as untrusted input and validate before security-sensitive use.
100
+ */
81
101
  readonly protocol: 'http' | 'https';
82
102
  readonly method: string;
83
103
  readonly routeOptions: Readonly<RequestRouteOptions<ContextConfig, SchemaCompiler>>
84
104
  readonly is404: boolean;
85
105
  readonly socket: RawRequest['socket'];
86
106
  readonly signal: AbortSignal;
107
+ readonly mediaType: string | undefined;
87
108
 
88
- getValidationFunction(httpPart: HTTPRequestPart): ValidationFunction
89
- getValidationFunction(schema: { [key: string]: any }): ValidationFunction
109
+ getValidationFunction(httpPart: HTTPRequestPart): ValidationFunction | undefined
110
+ getValidationFunction(schema: { [key: string]: any }): ValidationFunction | undefined
90
111
  compileValidationSchema(schema: { [key: string]: any }, httpPart?: HTTPRequestPart): ValidationFunction
91
112
  validateInput(input: any, schema: { [key: string]: any }, httpPart?: HTTPRequestPart): boolean
92
113
  validateInput(input: any, httpPart?: HTTPRequestPart): boolean
@@ -1,72 +0,0 @@
1
- import fastify, { FastifyBodyParser } from '../../fastify'
2
- import { expectError, expectType } from 'tsd'
3
- import { IncomingMessage } from 'node:http'
4
- import { FastifyRequest } from '../../types/request'
5
-
6
- expectType<void>(fastify().addContentTypeParser('contentType', function (request, payload, done) {
7
- expectType<FastifyRequest>(request)
8
- expectType<IncomingMessage>(payload)
9
- done(null)
10
- }))
11
-
12
- // Body limit options
13
-
14
- expectType<void>(fastify().addContentTypeParser('contentType', { bodyLimit: 99 }, function (request, payload, done) {
15
- expectType<FastifyRequest>(request)
16
- expectType<IncomingMessage>(payload)
17
- done(null)
18
- }))
19
-
20
- // Array for contentType
21
-
22
- expectType<void>(fastify().addContentTypeParser(['contentType'], function (request, payload, done) {
23
- expectType<FastifyRequest>(request)
24
- expectType<IncomingMessage>(payload)
25
- done(null)
26
- }))
27
-
28
- // Body Parser - the generic after addContentTypeParser enforces the type of the `body` parameter as well as the value of the `parseAs` property
29
-
30
- expectType<void>(fastify().addContentTypeParser<string>('bodyContentType', { parseAs: 'string' }, function (request, body, done) {
31
- expectType<FastifyRequest>(request)
32
- expectType<string>(body)
33
- done(null)
34
- }))
35
-
36
- expectType<void>(fastify().addContentTypeParser<Buffer>('bodyContentType', { parseAs: 'buffer' }, function (request, body, done) {
37
- expectType<FastifyRequest>(request)
38
- expectType<Buffer>(body)
39
- done(null)
40
- }))
41
-
42
- expectType<void>(fastify().addContentTypeParser('contentType', async function (request: FastifyRequest, payload: IncomingMessage) {
43
- expectType<FastifyRequest>(request)
44
- expectType<IncomingMessage>(payload)
45
- return null
46
- }))
47
-
48
- expectType<void>(fastify().addContentTypeParser<string>('bodyContentType', { parseAs: 'string' }, async function (request: FastifyRequest, body: string) {
49
- expectType<FastifyRequest>(request)
50
- expectType<string>(body)
51
- return null
52
- }))
53
-
54
- expectType<void>(fastify().addContentTypeParser<Buffer>('bodyContentType', { parseAs: 'buffer' }, async function (request: FastifyRequest, body: Buffer) {
55
- expectType<FastifyRequest>(request)
56
- expectType<Buffer>(body)
57
- return null
58
- }))
59
-
60
- expectType<FastifyBodyParser<string>>(fastify().getDefaultJsonParser('error', 'ignore'))
61
-
62
- expectError(fastify().getDefaultJsonParser('error', 'skip'))
63
-
64
- expectError(fastify().getDefaultJsonParser('nothing', 'ignore'))
65
-
66
- expectType<void>(fastify().removeAllContentTypeParsers())
67
- expectError(fastify().removeAllContentTypeParsers('contentType'))
68
-
69
- expectType<void>(fastify().removeContentTypeParser('contentType'))
70
- expectType<void>(fastify().removeContentTypeParser(/contentType+.*/))
71
- expectType<void>(fastify().removeContentTypeParser(['contentType', /contentType+.*/]))
72
- expectError(fastify().removeContentTypeParser({}))
@@ -1,90 +0,0 @@
1
- import { FastifyErrorConstructor } from '@fastify/error'
2
- import { expectAssignable } from 'tsd'
3
- import { errorCodes } from '../../fastify'
4
-
5
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_NOT_FOUND)
6
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_OPTIONS_NOT_OBJ)
7
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_QSP_NOT_FN)
8
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_SCHEMA_CONTROLLER_BUCKET_OPT_NOT_FN)
9
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_SCHEMA_ERROR_FORMATTER_NOT_FN)
10
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_AJV_CUSTOM_OPTIONS_OPT_NOT_OBJ)
11
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_AJV_CUSTOM_OPTIONS_OPT_NOT_ARR)
12
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_VALIDATION)
13
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_LISTEN_OPTIONS_INVALID)
14
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_ERROR_HANDLER_NOT_FN)
15
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_ERROR_HANDLER_ALREADY_SET)
16
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_CTP_ALREADY_PRESENT)
17
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_CTP_INVALID_TYPE)
18
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_CTP_EMPTY_TYPE)
19
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_CTP_INVALID_HANDLER)
20
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_CTP_INVALID_PARSE_TYPE)
21
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_CTP_BODY_TOO_LARGE)
22
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_CTP_INVALID_MEDIA_TYPE)
23
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_CTP_INVALID_CONTENT_LENGTH)
24
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_CTP_EMPTY_JSON_BODY)
25
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_CTP_INVALID_JSON_BODY)
26
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_CTP_INSTANCE_ALREADY_STARTED)
27
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_DEC_ALREADY_PRESENT)
28
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_DEC_DEPENDENCY_INVALID_TYPE)
29
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_DEC_MISSING_DEPENDENCY)
30
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_DEC_AFTER_START)
31
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_DEC_REFERENCE_TYPE)
32
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_DEC_UNDECLARED)
33
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_HOOK_INVALID_TYPE)
34
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_HOOK_INVALID_HANDLER)
35
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_HOOK_INVALID_ASYNC_HANDLER)
36
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_HOOK_NOT_SUPPORTED)
37
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_MISSING_MIDDLEWARE)
38
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_HOOK_TIMEOUT)
39
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_LOG_INVALID_DESTINATION)
40
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_LOG_INVALID_LOGGER)
41
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_LOG_INVALID_LOGGER_INSTANCE)
42
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_LOG_INVALID_LOGGER_CONFIG)
43
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_LOG_LOGGER_AND_LOGGER_INSTANCE_PROVIDED)
44
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_REP_INVALID_PAYLOAD_TYPE)
45
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_REP_RESPONSE_BODY_CONSUMED)
46
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_REP_READABLE_STREAM_LOCKED)
47
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_REP_ALREADY_SENT)
48
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_REP_SENT_VALUE)
49
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_SEND_INSIDE_ONERR)
50
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_SEND_UNDEFINED_ERR)
51
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_BAD_STATUS_CODE)
52
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_BAD_TRAILER_NAME)
53
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_BAD_TRAILER_VALUE)
54
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_FAILED_ERROR_SERIALIZATION)
55
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_MISSING_SERIALIZATION_FN)
56
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_MISSING_CONTENTTYPE_SERIALIZATION_FN)
57
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_REQ_INVALID_VALIDATION_INVOCATION)
58
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_SCH_MISSING_ID)
59
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_SCH_ALREADY_PRESENT)
60
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_SCH_CONTENT_MISSING_SCHEMA)
61
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_SCH_DUPLICATE)
62
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_SCH_VALIDATION_BUILD)
63
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_SCH_SERIALIZATION_BUILD)
64
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_SCH_RESPONSE_SCHEMA_NOT_NESTED_2XX)
65
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_INIT_OPTS_INVALID)
66
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_FORCE_CLOSE_CONNECTIONS_IDLE_NOT_AVAILABLE)
67
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_DUPLICATED_ROUTE)
68
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_BAD_URL)
69
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_ASYNC_CONSTRAINT)
70
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_INVALID_URL)
71
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_ROUTE_OPTIONS_NOT_OBJ)
72
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_ROUTE_DUPLICATED_HANDLER)
73
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_ROUTE_HANDLER_NOT_FN)
74
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_ROUTE_MISSING_HANDLER)
75
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_ROUTE_METHOD_INVALID)
76
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_ROUTE_METHOD_NOT_SUPPORTED)
77
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_ROUTE_BODY_VALIDATION_SCHEMA_NOT_SUPPORTED)
78
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_ROUTE_BODY_LIMIT_OPTION_NOT_INT)
79
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_ROUTE_REWRITE_NOT_STR)
80
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_REOPENED_CLOSE_SERVER)
81
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_REOPENED_SERVER)
82
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_INSTANCE_ALREADY_LISTENING)
83
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_PLUGIN_VERSION_MISMATCH)
84
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_PLUGIN_NOT_PRESENT_IN_INSTANCE)
85
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_PLUGIN_INVALID_ASYNC_HANDLER)
86
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_PLUGIN_CALLBACK_NOT_FN)
87
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_PLUGIN_NOT_VALID)
88
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_ROOT_PLG_BOOTED)
89
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_PARENT_PLUGIN_BOOTED)
90
- expectAssignable<FastifyErrorConstructor>(errorCodes.FST_ERR_PLUGIN_TIMEOUT)
@@ -1,352 +0,0 @@
1
- import Ajv, { ErrorObject as AjvErrorObject } from 'ajv'
2
- import * as http from 'node:http'
3
- import * as http2 from 'node:http2'
4
- import * as https from 'node:https'
5
- import { Socket } from 'node:net'
6
- import { expectAssignable, expectError, expectNotAssignable, expectType } from 'tsd'
7
- import fastify, {
8
- ConnectionError,
9
- FastifyBaseLogger,
10
- FastifyError,
11
- FastifyErrorCodes,
12
- FastifyInstance,
13
- FastifyPlugin,
14
- FastifyPluginAsync,
15
- FastifyPluginCallback,
16
- InjectOptions,
17
- LightMyRequestCallback,
18
- LightMyRequestChain,
19
- LightMyRequestResponse,
20
- RawRequestDefaultExpression,
21
- RouteGenericInterface,
22
- SafePromiseLike
23
- } from '../../fastify'
24
- import { Bindings, ChildLoggerOptions } from '../../types/logger'
25
-
26
- // FastifyInstance
27
- // http server
28
- expectError<
29
- FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse> &
30
- Promise<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>>
31
- >(fastify())
32
- expectAssignable<
33
- FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse> &
34
- PromiseLike<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>>
35
- >(fastify())
36
- expectType<
37
- FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse> &
38
- SafePromiseLike<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>>
39
- >(fastify())
40
- expectType<
41
- FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse> &
42
- SafePromiseLike<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>>
43
- >(fastify({}))
44
- expectType<
45
- FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse> &
46
- SafePromiseLike<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>>
47
- >(fastify({ http: {} }))
48
- // https server
49
- expectType<
50
- FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse> &
51
- SafePromiseLike<FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse>>
52
- >(fastify({ https: {} }))
53
- expectType<
54
- FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse> &
55
- SafePromiseLike<FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse>>
56
- >(fastify({ https: null }))
57
- // http2 server
58
- expectType<
59
- FastifyInstance<http2.Http2Server, http2.Http2ServerRequest, http2.Http2ServerResponse> &
60
- SafePromiseLike<FastifyInstance<http2.Http2Server, http2.Http2ServerRequest, http2.Http2ServerResponse>>
61
- >(fastify({ http2: true, http2SessionTimeout: 1000 }))
62
- expectType<
63
- FastifyInstance<http2.Http2SecureServer, http2.Http2ServerRequest, http2.Http2ServerResponse> &
64
- SafePromiseLike<FastifyInstance<http2.Http2SecureServer, http2.Http2ServerRequest, http2.Http2ServerResponse>>
65
- >(fastify({ http2: true, https: {}, http2SessionTimeout: 1000 }))
66
- expectType<LightMyRequestChain>(fastify({ http2: true, https: {} }).inject())
67
- expectType<
68
- FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse> &
69
- SafePromiseLike<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>>
70
- >(fastify({ schemaController: {} }))
71
- expectType<
72
- FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse> &
73
- SafePromiseLike<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>>
74
- >(
75
- fastify({
76
- schemaController: {
77
- compilersFactory: {}
78
- }
79
- })
80
- )
81
-
82
- expectError(fastify<http2.Http2Server>({ http2: false })) // http2 option must be true
83
- expectError(fastify<http2.Http2SecureServer>({ http2: false })) // http2 option must be true
84
- expectError(
85
- fastify({
86
- schemaController: {
87
- bucket: () => ({}) // cannot be empty
88
- }
89
- })
90
- )
91
-
92
- // light-my-request
93
- expectAssignable<InjectOptions>({ query: '' })
94
- fastify({ http2: true, https: {} }).inject().then((resp) => {
95
- expectAssignable<LightMyRequestResponse>(resp)
96
- })
97
- const lightMyRequestCallback: LightMyRequestCallback = (
98
- err: Error | undefined,
99
- response: LightMyRequestResponse | undefined
100
- ) => {
101
- if (err) throw err
102
- }
103
- fastify({ http2: true, https: {} }).inject({}, lightMyRequestCallback)
104
-
105
- // server options
106
- expectAssignable<
107
- FastifyInstance<http2.Http2Server, http2.Http2ServerRequest, http2.Http2ServerResponse>
108
- >(fastify({ http2: true }))
109
- expectAssignable<FastifyInstance>(fastify({ ignoreTrailingSlash: true }))
110
- expectAssignable<FastifyInstance>(fastify({ ignoreDuplicateSlashes: true }))
111
- expectAssignable<FastifyInstance>(fastify({ connectionTimeout: 1000 }))
112
- expectAssignable<FastifyInstance>(fastify({ forceCloseConnections: true }))
113
- expectAssignable<FastifyInstance>(fastify({ keepAliveTimeout: 1000 }))
114
- expectAssignable<FastifyInstance>(fastify({ pluginTimeout: 1000 }))
115
- expectAssignable<FastifyInstance>(fastify({ bodyLimit: 100 }))
116
- expectAssignable<FastifyInstance>(fastify({ handlerTimeout: 5000 }))
117
- expectAssignable<FastifyInstance>(fastify({ maxParamLength: 100 }))
118
- expectAssignable<FastifyInstance>(fastify({ disableRequestLogging: true }))
119
- expectAssignable<FastifyInstance>(fastify({ disableRequestLogging: (req) => req.url?.includes('/health') ?? false }))
120
- expectAssignable<FastifyInstance>(fastify({ requestIdLogLabel: 'request-id' }))
121
- expectAssignable<FastifyInstance>(fastify({ onProtoPoisoning: 'error' }))
122
- expectAssignable<FastifyInstance>(fastify({ onConstructorPoisoning: 'error' }))
123
- expectAssignable<FastifyInstance>(fastify({ serializerOpts: { rounding: 'ceil' } }))
124
- expectAssignable<FastifyInstance>(
125
- fastify({ serializerOpts: { ajv: { missingRefs: 'ignore' } } })
126
- )
127
- expectAssignable<FastifyInstance>(fastify({ serializerOpts: { schema: {} } }))
128
- expectAssignable<FastifyInstance>(fastify({ serializerOpts: { otherProp: {} } }))
129
- expectAssignable<
130
- FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyBaseLogger>
131
- >(fastify({ logger: true }))
132
- expectAssignable<
133
- FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyBaseLogger>
134
- >(fastify({ logger: true }))
135
- expectAssignable<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyBaseLogger>>(fastify({
136
- logger: {
137
- level: 'info',
138
- genReqId: () => 'request-id',
139
- serializers: {
140
- req: () => {
141
- return {
142
- method: 'GET',
143
- url: '/',
144
- version: '1.0.0',
145
- host: 'localhost',
146
- remoteAddress: '127.0.0.1',
147
- remotePort: 3000
148
- }
149
- },
150
- res: () => {
151
- return {
152
- statusCode: 200
153
- }
154
- },
155
- err: () => {
156
- return {
157
- type: 'Error',
158
- message: 'foo',
159
- stack: ''
160
- }
161
- }
162
- }
163
- }
164
- }))
165
- const customLogger = {
166
- level: 'info',
167
- info: () => { },
168
- warn: () => { },
169
- error: () => { },
170
- fatal: () => { },
171
- trace: () => { },
172
- debug: () => { },
173
- child: () => customLogger
174
- }
175
- expectAssignable<
176
- FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyBaseLogger>
177
- >(fastify({ logger: customLogger }))
178
- expectAssignable<FastifyInstance>(fastify({ serverFactory: () => http.createServer() }))
179
- expectAssignable<FastifyInstance>(fastify({ caseSensitive: true }))
180
- expectAssignable<FastifyInstance>(fastify({ requestIdHeader: 'request-id' }))
181
- expectAssignable<FastifyInstance>(fastify({ requestIdHeader: false }))
182
- expectAssignable<FastifyInstance>(fastify({
183
- genReqId: (req) => {
184
- expectType<RawRequestDefaultExpression>(req)
185
- return 'foo'
186
- }
187
- }))
188
- expectAssignable<FastifyInstance>(fastify({ trustProxy: true }))
189
- expectAssignable<FastifyInstance>(fastify({ querystringParser: () => ({ foo: 'bar' }) }))
190
- expectAssignable<FastifyInstance>(fastify({ querystringParser: () => ({ foo: { bar: 'fuzz' } }) }))
191
- expectAssignable<FastifyInstance>(fastify({ querystringParser: () => ({ foo: ['bar', 'fuzz'] }) }))
192
- expectAssignable<FastifyInstance>(fastify({ constraints: {} }))
193
- expectAssignable<FastifyInstance>(fastify({
194
- constraints: {
195
- version: {
196
- name: 'version',
197
- storage: () => ({
198
- get: () => () => { },
199
- set: () => { },
200
- del: () => { },
201
- empty: () => { }
202
- }),
203
- validate () { },
204
- deriveConstraint: () => 'foo'
205
- },
206
- host: {
207
- name: 'host',
208
- storage: () => ({
209
- get: () => () => { },
210
- set: () => { },
211
- del: () => { },
212
- empty: () => { }
213
- }),
214
- validate () { },
215
- deriveConstraint: () => 'foo'
216
- },
217
- withObjectValue: {
218
- name: 'withObjectValue',
219
- storage: () => ({
220
- get: () => () => { },
221
- set: () => { },
222
- del: () => { },
223
- empty: () => { }
224
- }),
225
- validate () { },
226
- deriveConstraint: () => { }
227
-
228
- }
229
- }
230
- }))
231
- expectAssignable<FastifyInstance>(fastify({ return503OnClosing: true }))
232
- expectAssignable<FastifyInstance>(fastify({
233
- ajv: {
234
- customOptions: {
235
- removeAdditional: 'all'
236
- },
237
- plugins: [(ajv: Ajv): Ajv => ajv]
238
- }
239
- }))
240
- expectAssignable<FastifyInstance>(fastify({
241
- ajv: {
242
- plugins: [[(ajv: Ajv): Ajv => ajv, ['keyword1', 'keyword2']]]
243
- }
244
- }))
245
- expectError(fastify({
246
- ajv: {
247
- customOptions: {
248
- removeAdditional: 'all'
249
- },
250
- plugins: [
251
- () => {
252
- // error, plugins always return the Ajv instance fluently
253
- }
254
- ]
255
- }
256
- }))
257
- expectAssignable<FastifyInstance>(fastify({
258
- ajv: {
259
- onCreate: (ajvInstance) => {
260
- expectType<Ajv>(ajvInstance)
261
- return ajvInstance
262
- }
263
- }
264
- }))
265
- expectAssignable<FastifyInstance>(fastify({ frameworkErrors: () => { } }))
266
- expectAssignable<FastifyInstance>(fastify({
267
- rewriteUrl: function (req) {
268
- this.log.debug('rewrite url')
269
- return req.url === '/hi' ? '/hello' : req.url!
270
- }
271
- }))
272
- expectAssignable<FastifyInstance>(fastify({
273
- schemaErrorFormatter: (errors, dataVar) => {
274
- console.log(
275
- errors[0].keyword.toLowerCase(),
276
- errors[0].message?.toLowerCase(),
277
- errors[0].params,
278
- errors[0].instancePath.toLowerCase(),
279
- errors[0].schemaPath.toLowerCase()
280
- )
281
- return new Error()
282
- }
283
- }))
284
- expectAssignable<FastifyInstance>(fastify({
285
- clientErrorHandler: (err, socket) => {
286
- expectType<ConnectionError>(err)
287
- expectType<Socket>(socket)
288
- }
289
- }))
290
-
291
- expectAssignable<FastifyInstance>(fastify({
292
- childLoggerFactory: function (
293
- this: FastifyInstance,
294
- logger: FastifyBaseLogger,
295
- bindings: Bindings,
296
- opts: ChildLoggerOptions,
297
- req: RawRequestDefaultExpression
298
- ) {
299
- expectType<FastifyBaseLogger>(logger)
300
- expectType<Bindings>(bindings)
301
- expectType<ChildLoggerOptions>(opts)
302
- expectType<RawRequestDefaultExpression>(req)
303
- expectAssignable<FastifyInstance>(this)
304
- return logger.child(bindings, opts)
305
- }
306
- }))
307
-
308
- // Thenable
309
- expectAssignable<PromiseLike<FastifyInstance>>(fastify({ return503OnClosing: true }))
310
- fastify().then(fastifyInstance => expectAssignable<FastifyInstance>(fastifyInstance))
311
-
312
- expectAssignable<FastifyPluginAsync>(async () => { })
313
- expectAssignable<FastifyPluginCallback>(() => { })
314
- expectAssignable<FastifyPlugin>(() => { })
315
-
316
- const ajvErrorObject: AjvErrorObject = {
317
- keyword: '',
318
- instancePath: '',
319
- schemaPath: '',
320
- params: {},
321
- message: ''
322
- }
323
- expectNotAssignable<AjvErrorObject>({
324
- keyword: '',
325
- instancePath: '',
326
- schemaPath: '',
327
- params: '',
328
- message: ''
329
- })
330
-
331
- expectAssignable<FastifyError['validation']>([ajvErrorObject])
332
- expectAssignable<FastifyError['validationContext']>('body')
333
- expectAssignable<FastifyError['validationContext']>('headers')
334
- expectAssignable<FastifyError['validationContext']>('params')
335
- expectAssignable<FastifyError['validationContext']>('querystring')
336
-
337
- const routeGeneric: RouteGenericInterface = {}
338
- expectType<unknown>(routeGeneric.Body)
339
- expectType<unknown>(routeGeneric.Headers)
340
- expectType<unknown>(routeGeneric.Params)
341
- expectType<unknown>(routeGeneric.Querystring)
342
- expectType<unknown>(routeGeneric.Reply)
343
-
344
- // ErrorCodes
345
- expectType<FastifyErrorCodes>(fastify.errorCodes)
346
-
347
- fastify({ allowUnsafeRegex: true })
348
- fastify({ allowUnsafeRegex: false })
349
- expectError(fastify({ allowUnsafeRegex: 'invalid' }))
350
-
351
- expectAssignable<FastifyInstance>(fastify({ allowErrorHandlerOverride: true }))
352
- expectAssignable<FastifyInstance>(fastify({ allowErrorHandlerOverride: false }))