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
@@ -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 }))