fastify 5.8.4 → 5.9.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 (105) hide show
  1. package/SECURITY.md +1 -1
  2. package/SPONSORS.md +6 -4
  3. package/docs/Guides/Database.md +0 -28
  4. package/docs/Guides/Ecosystem.md +17 -2
  5. package/docs/Guides/Serverless.md +2 -2
  6. package/docs/Guides/Write-Plugin.md +1 -1
  7. package/docs/Reference/Encapsulation.md +27 -26
  8. package/docs/Reference/Errors.md +10 -4
  9. package/docs/Reference/HTTP2.md +10 -10
  10. package/docs/Reference/Hooks.md +4 -4
  11. package/docs/Reference/Index.md +14 -16
  12. package/docs/Reference/LTS.md +12 -13
  13. package/docs/Reference/Lifecycle.md +9 -8
  14. package/docs/Reference/Logging.md +44 -39
  15. package/docs/Reference/Middleware.md +21 -25
  16. package/docs/Reference/Principles.md +2 -2
  17. package/docs/Reference/Reply.md +6 -1
  18. package/docs/Reference/Request.md +27 -16
  19. package/docs/Reference/Routes.md +5 -2
  20. package/docs/Reference/Server.md +31 -3
  21. package/docs/Reference/Type-Providers.md +29 -5
  22. package/docs/Reference/Validation-and-Serialization.md +15 -2
  23. package/docs/Reference/Warnings.md +7 -6
  24. package/eslint.config.js +7 -2
  25. package/fastify.d.ts +8 -3
  26. package/fastify.js +43 -14
  27. package/lib/content-type-parser.js +13 -1
  28. package/lib/decorate.js +11 -3
  29. package/lib/error-handler.js +4 -3
  30. package/lib/error-serializer.js +59 -59
  31. package/lib/errors.js +16 -1
  32. package/lib/four-oh-four.js +14 -9
  33. package/lib/handle-request.js +11 -5
  34. package/lib/plugin-override.js +2 -1
  35. package/lib/plugin-utils.js +5 -5
  36. package/lib/reply.js +63 -8
  37. package/lib/request.js +23 -19
  38. package/lib/route.js +20 -6
  39. package/lib/schema-controller.js +1 -1
  40. package/lib/schemas.js +37 -30
  41. package/lib/symbols.js +3 -1
  42. package/lib/validation.js +1 -13
  43. package/lib/warnings.js +3 -3
  44. package/package.json +16 -18
  45. package/scripts/validate-ecosystem-links.js +1 -0
  46. package/test/bundler/esbuild/package.json +1 -1
  47. package/test/close-pipelining.test.js +1 -2
  48. package/test/custom-http-server.test.js +38 -0
  49. package/test/decorator-instance-properties.test.js +63 -0
  50. package/test/diagnostics-channel/async-error-handler.test.js +74 -0
  51. package/test/hooks.test.js +23 -0
  52. package/test/http-methods/get.test.js +1 -1
  53. package/test/http2/plain.test.js +135 -0
  54. package/test/http2/secure-with-fallback.test.js +1 -1
  55. package/test/https/https.test.js +1 -2
  56. package/test/internals/errors.test.js +31 -1
  57. package/test/internals/plugin.test.js +3 -1
  58. package/test/internals/request.test.js +27 -3
  59. package/test/internals/schema-controller-perf.test.js +33 -0
  60. package/test/logger/logging.test.js +18 -1
  61. package/test/logger/options.test.js +38 -1
  62. package/test/reply-error.test.js +1 -1
  63. package/test/reply-trailers.test.js +70 -0
  64. package/test/request-media-type.test.js +105 -0
  65. package/test/request-port.test.js +72 -0
  66. package/test/route-prefix.test.js +34 -0
  67. package/test/router-options.test.js +222 -11
  68. package/test/schema-serialization.test.js +108 -0
  69. package/test/schema-validation.test.js +45 -0
  70. package/test/scripts/validate-ecosystem-links.test.js +40 -57
  71. package/test/throw.test.js +14 -0
  72. package/test/trust-proxy.test.js +74 -0
  73. package/test/types/content-type-parser.tst.ts +70 -0
  74. package/test/types/{decorate-request-reply.test-d.ts → decorate-request-reply.tst.ts} +4 -4
  75. package/test/types/{dummy-plugin.ts → dummy-plugin.mts} +1 -1
  76. package/test/types/errors.tst.ts +91 -0
  77. package/test/types/fastify.tst.ts +351 -0
  78. package/test/types/hooks.tst.ts +578 -0
  79. package/test/types/instance.tst.ts +597 -0
  80. package/test/types/{logger.test-d.ts → logger.tst.ts} +61 -62
  81. package/test/types/plugin.tst.ts +96 -0
  82. package/test/types/register.tst.ts +245 -0
  83. package/test/types/reply.tst.ts +297 -0
  84. package/test/types/request.tst.ts +199 -0
  85. package/test/types/route.tst.ts +576 -0
  86. package/test/types/{schema.test-d.ts → schema.tst.ts} +22 -22
  87. package/test/types/{serverFactory.test-d.ts → serverFactory.tst.ts} +4 -4
  88. package/test/types/tsconfig.json +9 -0
  89. package/test/types/{type-provider.test-d.ts → type-provider.tst.ts} +256 -250
  90. package/test/types/using.tst.ts +14 -0
  91. package/types/errors.d.ts +3 -0
  92. package/types/request.d.ts +23 -2
  93. package/AGENTS.md +0 -290
  94. package/test/types/content-type-parser.test-d.ts +0 -72
  95. package/test/types/errors.test-d.ts +0 -90
  96. package/test/types/fastify.test-d.ts +0 -352
  97. package/test/types/hooks.test-d.ts +0 -550
  98. package/test/types/import.ts +0 -2
  99. package/test/types/instance.test-d.ts +0 -588
  100. package/test/types/plugin.test-d.ts +0 -97
  101. package/test/types/register.test-d.ts +0 -237
  102. package/test/types/reply.test-d.ts +0 -254
  103. package/test/types/request.test-d.ts +0 -188
  104. package/test/types/route.test-d.ts +0 -553
  105. package/test/types/using.test-d.ts +0 -17
@@ -0,0 +1,351 @@
1
+ import * as http from 'node:http'
2
+ import * as http2 from 'node:http2'
3
+ import * as https from 'node:https'
4
+ import { Socket } from 'node:net'
5
+ import Ajv, { ErrorObject as AjvErrorObject } from 'ajv'
6
+ import { expect } from 'tstyche'
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.js'
24
+ import { Bindings, ChildLoggerOptions } from '../../types/logger.js'
25
+
26
+ // FastifyInstance
27
+ // http server
28
+ expect(fastify()).type.not.toBeAssignableTo<
29
+ FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse> &
30
+ Promise<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>>
31
+ >()
32
+ expect(fastify()).type.toBeAssignableTo<
33
+ FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse> &
34
+ PromiseLike<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>>
35
+ >()
36
+ expect(fastify()).type.toBe<
37
+ FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse> &
38
+ SafePromiseLike<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>>
39
+ >()
40
+ expect(fastify({})).type.toBe<
41
+ FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse> &
42
+ SafePromiseLike<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>>
43
+ >()
44
+ expect(fastify({ http: {} })).type.toBe<
45
+ FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse> &
46
+ SafePromiseLike<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>>
47
+ >()
48
+ // https server
49
+ expect(fastify({ https: {} })).type.toBe<
50
+ FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse> &
51
+ SafePromiseLike<FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse>>
52
+ >()
53
+ expect(fastify({ https: null })).type.toBe<
54
+ FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse> &
55
+ SafePromiseLike<FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse>>
56
+ >()
57
+ // http2 server
58
+ expect(fastify({ http2: true, http2SessionTimeout: 1000 })).type.toBe<
59
+ FastifyInstance<http2.Http2Server, http2.Http2ServerRequest, http2.Http2ServerResponse> &
60
+ SafePromiseLike<FastifyInstance<http2.Http2Server, http2.Http2ServerRequest, http2.Http2ServerResponse>>
61
+ >()
62
+ expect(fastify({ http2: true, https: {}, http2SessionTimeout: 1000 })).type.toBe<
63
+ FastifyInstance<http2.Http2SecureServer, http2.Http2ServerRequest, http2.Http2ServerResponse> &
64
+ SafePromiseLike<FastifyInstance<http2.Http2SecureServer, http2.Http2ServerRequest, http2.Http2ServerResponse>>
65
+ >()
66
+ expect(fastify({ http2: true, https: {} }).inject()).type.toBe<LightMyRequestChain>()
67
+ expect(fastify({ schemaController: {} })).type.toBe<
68
+ FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse> &
69
+ SafePromiseLike<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>>
70
+ >()
71
+ expect(
72
+ fastify({
73
+ schemaController: {
74
+ compilersFactory: {}
75
+ }
76
+ })
77
+ ).type.toBe<
78
+ FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse> &
79
+ SafePromiseLike<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>>
80
+ >()
81
+
82
+ // @ts-expect-error Type 'false' is not assignable to type 'true'.
83
+ fastify<http2.Http2Server>({ http2: false })
84
+ // @ts-expect-error Type 'false' is not assignable to type 'true'.
85
+ fastify<http2.Http2SecureServer>({ http2: false })
86
+ fastify({
87
+ schemaController: {
88
+ // @ts-expect-error No overload matches this call.
89
+ bucket: () => ({})
90
+ }
91
+ })
92
+
93
+ // light-my-request
94
+ expect<InjectOptions>().type.toBeAssignableFrom({ query: '' })
95
+ fastify({ http2: true, https: {} }).inject().then((resp) => {
96
+ expect(resp).type.toBe<LightMyRequestResponse>()
97
+ })
98
+ const lightMyRequestCallback: LightMyRequestCallback = (
99
+ err: Error | undefined,
100
+ response: LightMyRequestResponse | undefined
101
+ ) => {
102
+ if (err) throw err
103
+ }
104
+ fastify({ http2: true, https: {} }).inject({}, lightMyRequestCallback)
105
+
106
+ // server options
107
+ expect(fastify({ http2: true })).type.toBeAssignableTo<
108
+ FastifyInstance<http2.Http2Server, http2.Http2ServerRequest, http2.Http2ServerResponse>
109
+ >()
110
+ expect(fastify({ ignoreTrailingSlash: true })).type.toBeAssignableTo<FastifyInstance>()
111
+ expect(fastify({ ignoreDuplicateSlashes: true })).type.toBeAssignableTo<FastifyInstance>()
112
+ expect(fastify({ connectionTimeout: 1000 })).type.toBeAssignableTo<FastifyInstance>()
113
+ expect(fastify({ forceCloseConnections: true })).type.toBeAssignableTo<FastifyInstance>()
114
+ expect(fastify({ keepAliveTimeout: 1000 })).type.toBeAssignableTo<FastifyInstance>()
115
+ expect(fastify({ pluginTimeout: 1000 })).type.toBeAssignableTo<FastifyInstance>()
116
+ expect(fastify({ bodyLimit: 100 })).type.toBeAssignableTo<FastifyInstance>()
117
+ expect(fastify({ handlerTimeout: 5000 })).type.toBeAssignableTo<FastifyInstance>()
118
+ expect(fastify({ maxParamLength: 100 })).type.toBeAssignableTo<FastifyInstance>()
119
+ expect(fastify({ disableRequestLogging: true })).type.toBeAssignableTo<FastifyInstance>()
120
+ expect(fastify({ disableRequestLogging: (req) => req.url?.includes('/health') ?? false })).type.toBeAssignableTo<FastifyInstance>()
121
+ expect(fastify({ requestIdLogLabel: 'request-id' })).type.toBeAssignableTo<FastifyInstance>()
122
+ expect(fastify({ onProtoPoisoning: 'error' })).type.toBeAssignableTo<FastifyInstance>()
123
+ expect(fastify({ onConstructorPoisoning: 'error' })).type.toBeAssignableTo<FastifyInstance>()
124
+ expect(fastify({ serializerOpts: { rounding: 'ceil' } })).type.toBeAssignableTo<FastifyInstance>()
125
+ expect(
126
+ fastify({ serializerOpts: { ajv: { missingRefs: 'ignore' } } })
127
+ ).type.toBeAssignableTo<FastifyInstance>()
128
+ expect(fastify({ serializerOpts: { schema: {} } })).type.toBeAssignableTo<FastifyInstance>()
129
+ expect(fastify({ serializerOpts: { otherProp: {} } })).type.toBeAssignableTo<FastifyInstance>()
130
+ expect(fastify({ logger: true })).type.toBeAssignableTo<
131
+ FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyBaseLogger>
132
+ >()
133
+ expect(fastify({ logger: true })).type.toBeAssignableTo<
134
+ FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyBaseLogger>
135
+ >()
136
+ expect(fastify({
137
+ logger: {
138
+ level: 'info',
139
+ genReqId: () => 'request-id',
140
+ serializers: {
141
+ req: () => {
142
+ return {
143
+ method: 'GET',
144
+ url: '/',
145
+ version: '1.0.0',
146
+ host: 'localhost',
147
+ remoteAddress: '127.0.0.1',
148
+ remotePort: 3000
149
+ }
150
+ },
151
+ res: () => {
152
+ return {
153
+ statusCode: 200
154
+ }
155
+ },
156
+ err: () => {
157
+ return {
158
+ type: 'Error',
159
+ message: 'foo',
160
+ stack: ''
161
+ }
162
+ }
163
+ }
164
+ }
165
+ })).type.toBeAssignableTo<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyBaseLogger>>()
166
+ const customLogger = {
167
+ level: 'info',
168
+ info: () => { },
169
+ warn: () => { },
170
+ error: () => { },
171
+ fatal: () => { },
172
+ trace: () => { },
173
+ debug: () => { },
174
+ child: () => customLogger
175
+ }
176
+ expect(fastify({ logger: customLogger })).type.toBeAssignableTo<
177
+ FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyBaseLogger>
178
+ >()
179
+ expect(fastify({ serverFactory: () => http.createServer() })).type.toBeAssignableTo<FastifyInstance>()
180
+ expect(fastify({ caseSensitive: true })).type.toBeAssignableTo<FastifyInstance>()
181
+ expect(fastify({ requestIdHeader: 'request-id' })).type.toBeAssignableTo<FastifyInstance>()
182
+ expect(fastify({ requestIdHeader: false })).type.toBeAssignableTo<FastifyInstance>()
183
+ expect(fastify({
184
+ genReqId: (req) => {
185
+ expect(req).type.toBe<RawRequestDefaultExpression>()
186
+ return 'foo'
187
+ }
188
+ })).type.toBeAssignableTo<FastifyInstance>()
189
+ expect(fastify({ trustProxy: true })).type.toBeAssignableTo<FastifyInstance>()
190
+ expect(fastify({ querystringParser: () => ({ foo: 'bar' }) })).type.toBeAssignableTo<FastifyInstance>()
191
+ expect(fastify({ querystringParser: () => ({ foo: { bar: 'fuzz' } }) })).type.toBeAssignableTo<FastifyInstance>()
192
+ expect(fastify({ querystringParser: () => ({ foo: ['bar', 'fuzz'] }) })).type.toBeAssignableTo<FastifyInstance>()
193
+ expect(fastify({ constraints: {} })).type.toBeAssignableTo<FastifyInstance>()
194
+ expect(fastify({
195
+ constraints: {
196
+ version: {
197
+ name: 'version',
198
+ storage: () => ({
199
+ get: () => () => { },
200
+ set: () => { },
201
+ del: () => { },
202
+ empty: () => { }
203
+ }),
204
+ validate () { },
205
+ deriveConstraint: () => 'foo'
206
+ },
207
+ host: {
208
+ name: 'host',
209
+ storage: () => ({
210
+ get: () => () => { },
211
+ set: () => { },
212
+ del: () => { },
213
+ empty: () => { }
214
+ }),
215
+ validate () { },
216
+ deriveConstraint: () => 'foo'
217
+ },
218
+ withObjectValue: {
219
+ name: 'withObjectValue',
220
+ storage: () => ({
221
+ get: () => () => { },
222
+ set: () => { },
223
+ del: () => { },
224
+ empty: () => { }
225
+ }),
226
+ validate () { },
227
+ deriveConstraint: () => { }
228
+
229
+ }
230
+ }
231
+ })).type.toBeAssignableTo<FastifyInstance>()
232
+ expect(fastify({ return503OnClosing: true })).type.toBeAssignableTo<FastifyInstance>()
233
+ expect(fastify({
234
+ ajv: {
235
+ customOptions: {
236
+ removeAdditional: 'all'
237
+ },
238
+ plugins: [(ajv: Ajv): Ajv => ajv]
239
+ }
240
+ })).type.toBeAssignableTo<FastifyInstance>()
241
+ expect(fastify({
242
+ ajv: {
243
+ plugins: [[(ajv: Ajv): Ajv => ajv, ['keyword1', 'keyword2']]]
244
+ }
245
+ })).type.toBeAssignableTo<FastifyInstance>()
246
+ fastify({
247
+ ajv: {
248
+ customOptions: {
249
+ removeAdditional: 'all'
250
+ },
251
+ plugins: [
252
+ // @ts-expect-error Type 'void' is not assignable to type 'Ajv'.
253
+ () => { }
254
+ ]
255
+ }
256
+ })
257
+ expect(fastify({
258
+ ajv: {
259
+ onCreate: (ajvInstance) => {
260
+ expect(ajvInstance).type.toBe<Ajv>()
261
+ return ajvInstance
262
+ }
263
+ }
264
+ })).type.toBeAssignableTo<FastifyInstance>()
265
+ expect(fastify({ frameworkErrors: () => { } })).type.toBeAssignableTo<FastifyInstance>()
266
+ expect(fastify({
267
+ rewriteUrl: function (req) {
268
+ this.log.debug('rewrite url')
269
+ return req.url === '/hi' ? '/hello' : req.url!
270
+ }
271
+ })).type.toBeAssignableTo<FastifyInstance>()
272
+ expect(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
+ })).type.toBeAssignableTo<FastifyInstance>()
284
+ expect(fastify({
285
+ clientErrorHandler: (err, socket) => {
286
+ expect(err).type.toBe<ConnectionError>()
287
+ expect(socket).type.toBe<Socket>()
288
+ }
289
+ })).type.toBeAssignableTo<FastifyInstance>()
290
+
291
+ expect(fastify({
292
+ childLoggerFactory: function (
293
+ this,
294
+ logger,
295
+ bindings,
296
+ opts,
297
+ req
298
+ ) {
299
+ expect(logger).type.toBe<FastifyBaseLogger>()
300
+ expect(bindings).type.toBe<Bindings>()
301
+ expect(opts).type.toBe<ChildLoggerOptions>()
302
+ expect(req).type.toBe<RawRequestDefaultExpression>()
303
+ expect(this).type.toBeAssignableTo<FastifyInstance>()
304
+ return logger.child(bindings, opts)
305
+ }
306
+ })).type.toBeAssignableTo<FastifyInstance>()
307
+
308
+ // Thenable
309
+ expect(fastify({ return503OnClosing: true })).type.toBeAssignableTo<PromiseLike<FastifyInstance>>()
310
+ fastify().then(fastifyInstance => expect(fastifyInstance).type.toBeAssignableTo<FastifyInstance>())
311
+
312
+ expect<FastifyPluginAsync>().type.toBeAssignableFrom(async () => { })
313
+ expect<FastifyPluginCallback>().type.toBeAssignableFrom(() => { })
314
+ expect<FastifyPlugin>().type.toBeAssignableFrom(() => { })
315
+
316
+ const ajvErrorObject: AjvErrorObject = {
317
+ keyword: '',
318
+ instancePath: '',
319
+ schemaPath: '',
320
+ params: {},
321
+ message: ''
322
+ }
323
+ expect<AjvErrorObject>().type.not.toBeAssignableFrom({
324
+ keyword: '',
325
+ instancePath: '',
326
+ schemaPath: '',
327
+ params: '',
328
+ message: ''
329
+ })
330
+
331
+ expect<FastifyError['validation']>().type.toBeAssignableFrom([ajvErrorObject])
332
+ expect<FastifyError['validationContext']>().type.toBeAssignableFrom('body')
333
+ expect<FastifyError['validationContext']>().type.toBeAssignableFrom('headers')
334
+ expect<FastifyError['validationContext']>().type.toBeAssignableFrom('params')
335
+ expect<FastifyError['validationContext']>().type.toBeAssignableFrom('querystring')
336
+
337
+ expect<RouteGenericInterface['Body']>().type.toBe<unknown>()
338
+ expect<RouteGenericInterface['Headers']>().type.toBe<unknown>()
339
+ expect<RouteGenericInterface['Params']>().type.toBe<unknown>()
340
+ expect<RouteGenericInterface['Querystring']>().type.toBe<unknown>()
341
+ expect<RouteGenericInterface['Reply']>().type.toBe<unknown>()
342
+
343
+ // ErrorCodes
344
+ expect(fastify.errorCodes).type.toBe<FastifyErrorCodes>()
345
+
346
+ fastify({ allowUnsafeRegex: true })
347
+ fastify({ allowUnsafeRegex: false })
348
+ expect(fastify).type.not.toBeCallableWith({ allowUnsafeRegex: 'invalid' })
349
+
350
+ expect(fastify({ allowErrorHandlerOverride: true })).type.toBeAssignableTo<FastifyInstance>()
351
+ expect(fastify({ allowErrorHandlerOverride: false })).type.toBeAssignableTo<FastifyInstance>()