fastify 5.0.0 → 5.2.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 (201) hide show
  1. package/.borp.yaml +3 -0
  2. package/.vscode/settings.json +22 -0
  3. package/README.md +12 -7
  4. package/docs/Guides/Database.md +15 -15
  5. package/docs/Guides/Detecting-When-Clients-Abort.md +28 -28
  6. package/docs/Guides/Ecosystem.md +14 -15
  7. package/docs/Guides/Index.md +1 -1
  8. package/docs/Guides/Migration-Guide-V4.md +11 -11
  9. package/docs/Guides/Migration-Guide-V5.md +133 -9
  10. package/docs/Guides/Plugins-Guide.md +1 -1
  11. package/docs/Guides/Prototype-Poisoning.md +3 -3
  12. package/docs/Guides/Recommendations.md +9 -9
  13. package/docs/Guides/Serverless.md +5 -5
  14. package/docs/Guides/Testing.md +58 -57
  15. package/docs/Guides/Write-Plugin.md +2 -2
  16. package/docs/Guides/Write-Type-Provider.md +3 -3
  17. package/docs/Reference/ContentTypeParser.md +4 -4
  18. package/docs/Reference/Decorators.md +2 -2
  19. package/docs/Reference/Errors.md +3 -3
  20. package/docs/Reference/Hooks.md +7 -7
  21. package/docs/Reference/LTS.md +8 -0
  22. package/docs/Reference/Logging.md +5 -4
  23. package/docs/Reference/Reply.md +55 -58
  24. package/docs/Reference/Request.md +49 -42
  25. package/docs/Reference/Routes.md +16 -13
  26. package/docs/Reference/Server.md +32 -28
  27. package/docs/Reference/TypeScript.md +9 -9
  28. package/docs/Reference/Validation-and-Serialization.md +5 -5
  29. package/examples/typescript-server.ts +1 -1
  30. package/fastify.d.ts +14 -5
  31. package/fastify.js +8 -6
  32. package/lib/contentTypeParser.js +9 -7
  33. package/lib/context.js +1 -2
  34. package/lib/error-handler.js +9 -9
  35. package/lib/errors.js +1 -1
  36. package/lib/fourOhFour.js +1 -1
  37. package/lib/hooks.js +4 -1
  38. package/lib/{logger.js → logger-factory.js} +70 -122
  39. package/lib/logger-pino.js +68 -0
  40. package/lib/pluginOverride.js +1 -1
  41. package/lib/pluginUtils.js +2 -2
  42. package/lib/reply.js +4 -5
  43. package/lib/request.js +16 -9
  44. package/lib/route.js +23 -22
  45. package/lib/validation.js +2 -2
  46. package/package.json +13 -15
  47. package/test/404s.test.js +675 -629
  48. package/test/500s.test.js +72 -63
  49. package/test/{allowUnsafeRegex.test.js → allow-unsafe-regex.test.js} +30 -26
  50. package/test/als.test.js +48 -45
  51. package/test/async-await.test.js +148 -134
  52. package/test/async-dispose.test.js +4 -5
  53. package/test/async_hooks.test.js +30 -28
  54. package/test/{bodyLimit.test.js → body-limit.test.js} +61 -58
  55. package/test/buffer.test.js +9 -10
  56. package/test/build/error-serializer.test.js +3 -4
  57. package/test/build/version.test.js +2 -3
  58. package/test/build-certificate.js +1 -1
  59. package/test/bundler/README.md +5 -5
  60. package/test/bundler/esbuild/bundler-test.js +10 -9
  61. package/test/bundler/webpack/bundler-test.js +10 -9
  62. package/test/case-insensitive.test.js +31 -28
  63. package/test/chainable.test.js +4 -5
  64. package/test/check.test.js +8 -10
  65. package/test/{childLoggerFactory.test.js → child-logger-factory.test.js} +56 -19
  66. package/test/client-timeout.test.js +5 -5
  67. package/test/close-pipelining.test.js +6 -8
  68. package/test/conditional-pino.test.js +47 -0
  69. package/test/{connectionTimeout.test.js → connection-timeout.test.js} +10 -11
  70. package/test/constrained-routes.test.js +243 -236
  71. package/test/content-length.test.js +53 -68
  72. package/test/content-parser.test.js +186 -158
  73. package/test/content-type.test.js +8 -9
  74. package/test/context-config.test.js +44 -54
  75. package/test/custom-http-server.test.js +16 -20
  76. package/test/custom-parser.5.test.js +32 -32
  77. package/test/diagnostics-channel/404.test.js +15 -15
  78. package/test/diagnostics-channel/async-delay-request.test.js +25 -25
  79. package/test/diagnostics-channel/async-request.test.js +24 -24
  80. package/test/diagnostics-channel/error-before-handler.test.js +4 -5
  81. package/test/diagnostics-channel/error-request.test.js +19 -19
  82. package/test/diagnostics-channel/error-status.test.js +8 -8
  83. package/test/diagnostics-channel/init.test.js +6 -7
  84. package/test/diagnostics-channel/sync-delay-request.test.js +16 -16
  85. package/test/diagnostics-channel/sync-request-reply.test.js +16 -16
  86. package/test/diagnostics-channel/sync-request.test.js +19 -19
  87. package/test/encapsulated-child-logger-factory.test.js +8 -8
  88. package/test/encapsulated-error-handler.test.js +20 -20
  89. package/test/esm/errorCodes.test.mjs +5 -5
  90. package/test/esm/esm.test.mjs +3 -3
  91. package/test/esm/named-exports.mjs +3 -3
  92. package/test/esm/other.mjs +2 -2
  93. package/test/fastify-instance.test.js +33 -34
  94. package/test/{findRoute.test.js → find-route.test.js} +11 -10
  95. package/test/fluent-schema.test.js +33 -36
  96. package/test/handler-context.test.js +11 -11
  97. package/test/has-route.test.js +12 -15
  98. package/test/header-overflow.test.js +13 -12
  99. package/test/hooks.on-ready.test.js +2 -2
  100. package/test/hooks.test.js +25 -25
  101. package/test/http-methods/copy.test.js +22 -24
  102. package/test/http-methods/custom-http-methods.test.js +24 -21
  103. package/test/http-methods/get.test.js +97 -84
  104. package/test/http-methods/head.test.js +63 -57
  105. package/test/http-methods/lock.test.js +21 -20
  106. package/test/http-methods/mkcalendar.test.js +31 -27
  107. package/test/http-methods/mkcol.test.js +10 -10
  108. package/test/http-methods/move.test.js +11 -11
  109. package/test/http-methods/propfind.test.js +32 -27
  110. package/test/http-methods/proppatch.test.js +21 -19
  111. package/test/http-methods/report.test.js +32 -27
  112. package/test/http-methods/search.test.js +52 -47
  113. package/test/http-methods/trace.test.js +3 -4
  114. package/test/http-methods/unlock.test.js +10 -10
  115. package/test/http2/closing.test.js +50 -58
  116. package/test/http2/constraint.test.js +47 -50
  117. package/test/http2/head.test.js +18 -19
  118. package/test/http2/missing-http2-module.test.js +4 -5
  119. package/test/http2/plain.test.js +31 -31
  120. package/test/http2/secure-with-fallback.test.js +61 -61
  121. package/test/http2/secure.test.js +28 -31
  122. package/test/http2/unknown-http-method.test.js +13 -14
  123. package/test/https/custom-https-server.test.js +6 -7
  124. package/test/https/https.test.js +78 -78
  125. package/test/imports.test.js +5 -6
  126. package/test/internals/all.test.js +8 -11
  127. package/test/internals/{contentTypeParser.test.js → content-type-parser.test.js} +5 -6
  128. package/test/internals/context.test.js +9 -11
  129. package/test/internals/decorator.test.js +20 -21
  130. package/test/internals/errors.test.js +427 -427
  131. package/test/internals/{handleRequest.test.js → handle-request.test.js} +53 -42
  132. package/test/internals/{hookRunner.test.js → hook-runner.test.js} +99 -100
  133. package/test/internals/hooks.test.js +31 -35
  134. package/test/internals/{initialConfig.test.js → initial-config.test.js} +92 -80
  135. package/test/internals/logger.test.js +28 -28
  136. package/test/internals/plugin.test.js +17 -18
  137. package/test/internals/reply-serialize.test.js +106 -106
  138. package/test/internals/reply.test.js +620 -585
  139. package/test/internals/{reqIdGenFactory.test.js → req-id-gen-factory.test.js} +31 -31
  140. package/test/internals/request-validate.test.js +218 -221
  141. package/test/internals/request.test.js +225 -107
  142. package/test/internals/server.test.js +15 -12
  143. package/test/internals/validation.test.js +35 -36
  144. package/test/{keepAliveTimeout.test.js → keep-alive-timeout.test.js} +9 -10
  145. package/test/listen.5.test.js +9 -9
  146. package/test/{maxRequestsPerSocket.test.js → max-requests-per-socket.test.js} +30 -30
  147. package/test/middleware.test.js +4 -5
  148. package/test/noop-set.test.js +5 -5
  149. package/test/post-empty-body.test.js +18 -11
  150. package/test/pretty-print.test.js +59 -49
  151. package/test/proto-poisoning.test.js +42 -37
  152. package/test/reply-code.test.js +34 -32
  153. package/test/{reply-earlyHints.test.js → reply-early-hints.test.js} +21 -19
  154. package/test/request-error.test.js +122 -0
  155. package/test/request-header-host.test.js +339 -0
  156. package/test/request-id.test.js +31 -25
  157. package/test/{requestTimeout.test.js → request-timeout.test.js} +11 -11
  158. package/test/route.1.test.js +79 -72
  159. package/test/route.2.test.js +17 -16
  160. package/test/route.3.test.js +32 -27
  161. package/test/route.4.test.js +21 -25
  162. package/test/route.5.test.js +45 -64
  163. package/test/route.6.test.js +70 -89
  164. package/test/route.7.test.js +61 -65
  165. package/test/route.8.test.js +80 -18
  166. package/test/router-options.test.js +80 -77
  167. package/test/same-shape.test.js +5 -5
  168. package/test/schema-examples.test.js +72 -38
  169. package/test/serialize-response.test.js +9 -10
  170. package/test/server.test.js +75 -78
  171. package/test/set-error-handler.test.js +2 -3
  172. package/test/stream-serializers.test.js +10 -7
  173. package/test/sync-routes.test.js +18 -18
  174. package/test/test-reporter.mjs +68 -0
  175. package/test/trust-proxy.test.js +51 -45
  176. package/test/type-provider.test.js +8 -6
  177. package/test/types/content-type-parser.test-d.ts +1 -1
  178. package/test/types/fastify.test-d.ts +16 -4
  179. package/test/types/hooks.test-d.ts +2 -1
  180. package/test/types/instance.test-d.ts +13 -13
  181. package/test/types/logger.test-d.ts +2 -2
  182. package/test/types/plugin.test-d.ts +17 -9
  183. package/test/types/register.test-d.ts +22 -6
  184. package/test/types/reply.test-d.ts +1 -1
  185. package/test/types/route.test-d.ts +34 -4
  186. package/test/types/serverFactory.test-d.ts +1 -1
  187. package/test/types/type-provider.test-d.ts +1 -1
  188. package/test/url-rewriting.test.js +35 -38
  189. package/test/{useSemicolonDelimiter.test.js → use-semicolon-delimiter.test.js} +30 -30
  190. package/test/validation-error-handling.test.js +259 -285
  191. package/test/versioned-routes.test.js +126 -113
  192. package/test/web-api.test.js +48 -37
  193. package/test/{wrapThenable.test.js → wrap-thenable.test.js} +10 -9
  194. package/types/hooks.d.ts +2 -1
  195. package/types/instance.d.ts +9 -2
  196. package/types/register.d.ts +12 -3
  197. package/types/reply.d.ts +1 -1
  198. package/types/request.d.ts +2 -6
  199. package/types/serverFactory.d.ts +3 -3
  200. package/types/utils.d.ts +13 -5
  201. package/test/types/import.js +0 -2
@@ -1,7 +1,6 @@
1
1
  'use strict'
2
2
 
3
- const t = require('tap')
4
- const test = t.test
3
+ const { test } = require('node:test')
5
4
  const Fastify = require('../fastify')
6
5
  const fs = require('node:fs')
7
6
  const { Readable } = require('node:stream')
@@ -24,8 +23,8 @@ test('should response with a ReadableStream', async (t) => {
24
23
 
25
24
  const expected = await fs.promises.readFile(__filename)
26
25
 
27
- t.equal(statusCode, 200)
28
- t.equal(expected.toString(), body.toString())
26
+ t.assert.strictEqual(statusCode, 200)
27
+ t.assert.strictEqual(expected.toString(), body.toString())
29
28
  })
30
29
 
31
30
  test('should response with a Response', async (t) => {
@@ -51,9 +50,9 @@ test('should response with a Response', async (t) => {
51
50
 
52
51
  const expected = await fs.promises.readFile(__filename)
53
52
 
54
- t.equal(statusCode, 200)
55
- t.equal(expected.toString(), body.toString())
56
- t.equal(headers.hello, 'world')
53
+ t.assert.strictEqual(statusCode, 200)
54
+ t.assert.strictEqual(expected.toString(), body.toString())
55
+ t.assert.strictEqual(headers.hello, 'world')
57
56
  })
58
57
 
59
58
  test('should response with a Response 204', async (t) => {
@@ -76,9 +75,9 @@ test('should response with a Response 204', async (t) => {
76
75
  body
77
76
  } = await fastify.inject({ method: 'GET', path: '/' })
78
77
 
79
- t.equal(statusCode, 204)
80
- t.equal(body, '')
81
- t.equal(headers.hello, 'world')
78
+ t.assert.strictEqual(statusCode, 204)
79
+ t.assert.strictEqual(body, '')
80
+ t.assert.strictEqual(headers.hello, 'world')
82
81
  })
83
82
 
84
83
  test('should response with a Response 304', async (t) => {
@@ -101,9 +100,9 @@ test('should response with a Response 304', async (t) => {
101
100
  body
102
101
  } = await fastify.inject({ method: 'GET', path: '/' })
103
102
 
104
- t.equal(statusCode, 304)
105
- t.equal(body, '')
106
- t.equal(headers.hello, 'world')
103
+ t.assert.strictEqual(statusCode, 304)
104
+ t.assert.strictEqual(body, '')
105
+ t.assert.strictEqual(headers.hello, 'world')
107
106
  })
108
107
 
109
108
  test('should response with a Response without body', async (t) => {
@@ -126,9 +125,9 @@ test('should response with a Response without body', async (t) => {
126
125
  body
127
126
  } = await fastify.inject({ method: 'GET', path: '/' })
128
127
 
129
- t.equal(statusCode, 200)
130
- t.equal(body, '')
131
- t.equal(headers.hello, 'world')
128
+ t.assert.strictEqual(statusCode, 200)
129
+ t.assert.strictEqual(body, '')
130
+ t.assert.strictEqual(headers.hello, 'world')
132
131
  })
133
132
 
134
133
  test('able to use in onSend hook - ReadableStream', async (t) => {
@@ -142,7 +141,7 @@ test('able to use in onSend hook - ReadableStream', async (t) => {
142
141
  })
143
142
 
144
143
  fastify.addHook('onSend', (request, reply, payload, done) => {
145
- t.equal(Object.prototype.toString.call(payload), '[object ReadableStream]')
144
+ t.assert.strictEqual(Object.prototype.toString.call(payload), '[object ReadableStream]')
146
145
  done(null, new Response(payload, {
147
146
  status: 200,
148
147
  headers: {
@@ -159,9 +158,9 @@ test('able to use in onSend hook - ReadableStream', async (t) => {
159
158
 
160
159
  const expected = await fs.promises.readFile(__filename)
161
160
 
162
- t.equal(statusCode, 200)
163
- t.equal(expected.toString(), body.toString())
164
- t.equal(headers.hello, 'world')
161
+ t.assert.strictEqual(statusCode, 200)
162
+ t.assert.strictEqual(expected.toString(), body.toString())
163
+ t.assert.strictEqual(headers.hello, 'world')
165
164
  })
166
165
 
167
166
  test('able to use in onSend hook - Response', async (t) => {
@@ -180,7 +179,7 @@ test('able to use in onSend hook - Response', async (t) => {
180
179
  })
181
180
 
182
181
  fastify.addHook('onSend', (request, reply, payload, done) => {
183
- t.equal(Object.prototype.toString.call(payload), '[object Response]')
182
+ t.assert.strictEqual(Object.prototype.toString.call(payload), '[object Response]')
184
183
  done(null, new Response(payload.body, {
185
184
  status: 200,
186
185
  headers: payload.headers
@@ -195,9 +194,9 @@ test('able to use in onSend hook - Response', async (t) => {
195
194
 
196
195
  const expected = await fs.promises.readFile(__filename)
197
196
 
198
- t.equal(statusCode, 200)
199
- t.equal(expected.toString(), body.toString())
200
- t.equal(headers.hello, 'world')
197
+ t.assert.strictEqual(statusCode, 200)
198
+ t.assert.strictEqual(expected.toString(), body.toString())
199
+ t.assert.strictEqual(headers.hello, 'world')
201
200
  })
202
201
 
203
202
  test('Error when Response.bodyUsed', async (t) => {
@@ -216,31 +215,37 @@ test('Error when Response.bodyUsed', async (t) => {
216
215
  }
217
216
  })
218
217
  const file = await response.text()
219
- t.equal(expected.toString(), file)
220
- t.equal(response.bodyUsed, true)
218
+ t.assert.strictEqual(expected.toString(), file)
219
+ t.assert.strictEqual(response.bodyUsed, true)
221
220
  return reply.send(response)
222
221
  })
223
222
 
224
223
  const response = await fastify.inject({ method: 'GET', path: '/' })
225
224
 
226
- t.equal(response.statusCode, 500)
225
+ t.assert.strictEqual(response.statusCode, 500)
227
226
  const body = response.json()
228
- t.equal(body.code, 'FST_ERR_REP_RESPONSE_BODY_CONSUMED')
227
+ t.assert.strictEqual(body.code, 'FST_ERR_REP_RESPONSE_BODY_CONSUMED')
229
228
  })
230
229
 
231
230
  test('allow to pipe with fetch', async (t) => {
232
231
  t.plan(2)
232
+ const abortController = new AbortController()
233
+ const { signal } = abortController
233
234
 
234
235
  const fastify = Fastify()
235
- t.teardown(fastify.close.bind(fastify))
236
+ t.after(() => {
237
+ fastify.close()
238
+ abortController.abort()
239
+ })
236
240
 
237
241
  fastify.get('/', function (request, reply) {
238
242
  return fetch(`${fastify.listeningOrigin}/fetch`, {
239
- method: 'GET'
243
+ method: 'GET',
244
+ signal
240
245
  })
241
246
  })
242
247
 
243
- fastify.get('/fetch', function (request, reply) {
248
+ fastify.get('/fetch', function async (request, reply) {
244
249
  reply.code(200).send({ ok: true })
245
250
  })
246
251
 
@@ -248,19 +253,25 @@ test('allow to pipe with fetch', async (t) => {
248
253
 
249
254
  const response = await fastify.inject({ method: 'GET', path: '/' })
250
255
 
251
- t.equal(response.statusCode, 200)
252
- t.same(response.json(), { ok: true })
256
+ t.assert.strictEqual(response.statusCode, 200)
257
+ t.assert.deepStrictEqual(response.json(), { ok: true })
253
258
  })
254
259
 
255
260
  test('allow to pipe with undici.fetch', async (t) => {
256
261
  t.plan(2)
262
+ const abortController = new AbortController()
263
+ const { signal } = abortController
257
264
 
258
265
  const fastify = Fastify()
259
- t.teardown(fastify.close.bind(fastify))
266
+ t.after(() => {
267
+ fastify.close()
268
+ abortController.abort()
269
+ })
260
270
 
261
271
  fastify.get('/', function (request, reply) {
262
272
  return undiciFetch(`${fastify.listeningOrigin}/fetch`, {
263
- method: 'GET'
273
+ method: 'GET',
274
+ signal
264
275
  })
265
276
  })
266
277
 
@@ -272,6 +283,6 @@ test('allow to pipe with undici.fetch', async (t) => {
272
283
 
273
284
  const response = await fastify.inject({ method: 'GET', path: '/' })
274
285
 
275
- t.equal(response.statusCode, 200)
276
- t.same(response.json(), { ok: true })
286
+ t.assert.strictEqual(response.statusCode, 200)
287
+ t.assert.deepStrictEqual(response.json(), { ok: true })
277
288
  })
@@ -1,17 +1,18 @@
1
1
  'use strict'
2
2
 
3
- const t = require('tap')
4
- const test = t.test
3
+ const { test } = require('node:test')
5
4
  const { kReplyHijacked } = require('../lib/symbols')
6
5
  const wrapThenable = require('../lib/wrapThenable')
7
6
  const Reply = require('../lib/reply')
8
7
 
9
- test('should resolve immediately when reply[kReplyHijacked] is true', t => {
10
- const reply = {}
11
- reply[kReplyHijacked] = true
12
- const thenable = Promise.resolve()
13
- wrapThenable(thenable, reply)
14
- t.end()
8
+ test('should resolve immediately when reply[kReplyHijacked] is true', async t => {
9
+ await new Promise(resolve => {
10
+ const reply = {}
11
+ reply[kReplyHijacked] = true
12
+ const thenable = Promise.resolve()
13
+ wrapThenable(thenable, reply)
14
+ resolve()
15
+ })
15
16
  })
16
17
 
17
18
  test('should reject immediately when reply[kReplyHijacked] is true', t => {
@@ -20,7 +21,7 @@ test('should reject immediately when reply[kReplyHijacked] is true', t => {
20
21
  reply[kReplyHijacked] = true
21
22
  reply.log = {
22
23
  error: ({ err }) => {
23
- t.equal(err.message, 'Reply sent already')
24
+ t.assert.strictEqual(err.message, 'Reply sent already')
24
25
  }
25
26
  }
26
27
 
package/types/hooks.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Readable } from 'stream'
1
+ import { Readable } from 'node:stream'
2
2
  import { FastifyInstance } from './instance'
3
3
  import { RouteOptions, RouteGenericInterface } from './route'
4
4
  import { RawServerBase, RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, ContextConfigDefault } from './utils'
@@ -710,6 +710,7 @@ export interface onRegisterHookHandler<
710
710
  Options extends FastifyPluginOptions = FastifyPluginOptions
711
711
  > {
712
712
  (
713
+ this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
713
714
  instance: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
714
715
  opts: RegisterOptions & Options
715
716
  ): Promise<unknown> | void;
@@ -1,8 +1,8 @@
1
1
  import { FastifyError } from '@fastify/error'
2
2
  import { ConstraintStrategy, FindResult, HTTPVersion } from 'find-my-way'
3
- import * as http from 'http'
3
+ import * as http from 'node:http'
4
4
  import { InjectOptions, CallbackFunc as LightMyRequestCallback, Chain as LightMyRequestChain, Response as LightMyRequestResponse } from 'light-my-request'
5
- import { AddressInfo } from 'net'
5
+ import { AddressInfo } from 'node:net'
6
6
  import { AddContentTypeParser, ConstructorAction, FastifyBodyParser, ProtoAction, getDefaultJsonParser, hasContentTypeParser, removeAllContentTypeParsers, removeContentTypeParser } from './content-type-parser'
7
7
  import { ApplicationHook, HookAsyncLookup, HookLookup, LifecycleHook, onCloseAsyncHookHandler, onCloseHookHandler, onErrorAsyncHookHandler, onErrorHookHandler, onListenAsyncHookHandler, onListenHookHandler, onReadyAsyncHookHandler, onReadyHookHandler, onRegisterHookHandler, onRequestAbortAsyncHookHandler, onRequestAbortHookHandler, onRequestAsyncHookHandler, onRequestHookHandler, onResponseAsyncHookHandler, onResponseHookHandler, onRouteHookHandler, onSendAsyncHookHandler, onSendHookHandler, onTimeoutAsyncHookHandler, onTimeoutHookHandler, preCloseAsyncHookHandler, preCloseHookHandler, preHandlerAsyncHookHandler, preHandlerHookHandler, preParsingAsyncHookHandler, preParsingHookHandler, preSerializationAsyncHookHandler, preSerializationHookHandler, preValidationAsyncHookHandler, preValidationHookHandler } from './hooks'
8
8
  import { FastifyBaseLogger, FastifyChildLoggerFactory } from './logger'
@@ -548,6 +548,13 @@ export interface FastifyInstance<
548
548
  * Remove all content type parsers, including the default ones
549
549
  */
550
550
  removeAllContentTypeParsers: removeAllContentTypeParsers
551
+ /**
552
+ * Add a non-standard HTTP method
553
+ *
554
+ * Methods defined by default include `GET`, `HEAD`, `TRACE`, `DELETE`,
555
+ * `OPTIONS`, `PATCH`, `PUT` and `POST`
556
+ */
557
+ addHttpMethod(method: string, methodOptions?: { hasBody: boolean }): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
551
558
  /**
552
559
  * Fastify default JSON parser
553
560
  */
@@ -18,16 +18,25 @@ export type FastifyRegisterOptions<Options> = (RegisterOptions & Options) | ((in
18
18
  * Function for adding a plugin to fastify. The options are inferred from the passed in FastifyPlugin parameter.
19
19
  */
20
20
  export interface FastifyRegister<T = void, RawServer extends RawServerBase = RawServerDefault, TypeProviderDefault extends FastifyTypeProvider = FastifyTypeProvider, LoggerDefault extends FastifyBaseLogger = FastifyBaseLogger> {
21
+ <Server extends RawServerBase = RawServer, TypeProvider extends FastifyTypeProvider = TypeProviderDefault, Logger extends FastifyBaseLogger = LoggerDefault>(
22
+ plugin: FastifyPluginCallback<FastifyPluginOptions, Server, TypeProvider, Logger>
23
+ ): T;
21
24
  <Options extends FastifyPluginOptions, Server extends RawServerBase = RawServer, TypeProvider extends FastifyTypeProvider = TypeProviderDefault, Logger extends FastifyBaseLogger = LoggerDefault>(
22
25
  plugin: FastifyPluginCallback<Options, Server, TypeProvider, Logger>,
23
- opts?: FastifyRegisterOptions<Options>
26
+ opts: FastifyRegisterOptions<Options>
27
+ ): T;
28
+ <Server extends RawServerBase = RawServer, TypeProvider extends FastifyTypeProvider = TypeProviderDefault, Logger extends FastifyBaseLogger = LoggerDefault>(
29
+ plugin: FastifyPluginAsync<FastifyPluginOptions, Server, TypeProvider, Logger>
24
30
  ): T;
25
31
  <Options extends FastifyPluginOptions, Server extends RawServerBase = RawServer, TypeProvider extends FastifyTypeProvider = TypeProviderDefault, Logger extends FastifyBaseLogger = LoggerDefault>(
26
32
  plugin: FastifyPluginAsync<Options, Server, TypeProvider, Logger>,
27
- opts?: FastifyRegisterOptions<Options>
33
+ opts: FastifyRegisterOptions<Options>
34
+ ): T;
35
+ <Server extends RawServerBase = RawServer, TypeProvider extends FastifyTypeProvider = TypeProviderDefault, Logger extends FastifyBaseLogger = LoggerDefault>(
36
+ plugin: FastifyPluginCallback<FastifyPluginOptions, Server, TypeProvider, Logger> | FastifyPluginAsync<FastifyPluginOptions, Server, TypeProvider, Logger> | Promise<{ default: FastifyPluginCallback<FastifyPluginOptions, Server, TypeProvider, Logger> }> | Promise<{ default: FastifyPluginAsync<FastifyPluginOptions, Server, TypeProvider, Logger> }>,
28
37
  ): T;
29
38
  <Options extends FastifyPluginOptions, Server extends RawServerBase = RawServer, TypeProvider extends FastifyTypeProvider = TypeProviderDefault, Logger extends FastifyBaseLogger = LoggerDefault>(
30
39
  plugin: FastifyPluginCallback<Options, Server, TypeProvider, Logger> | FastifyPluginAsync<Options, Server, TypeProvider, Logger> | Promise<{ default: FastifyPluginCallback<Options, Server, TypeProvider, Logger> }> | Promise<{ default: FastifyPluginAsync<Options, Server, TypeProvider, Logger> }>,
31
- opts?: FastifyRegisterOptions<Options>
40
+ opts: FastifyRegisterOptions<Options>
32
41
  ): T;
33
42
  }
package/types/reply.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Buffer } from 'buffer'
1
+ import { Buffer } from 'node:buffer'
2
2
  import { FastifyInstance } from './instance'
3
3
  import { FastifyBaseLogger } from './logger'
4
4
  import { FastifyRequest, RequestRouteOptions } from './request'
@@ -5,7 +5,7 @@ import { FastifyBaseLogger } from './logger'
5
5
  import { FastifyRouteConfig, RouteGenericInterface, RouteHandlerMethod } from './route'
6
6
  import { FastifySchema } from './schema'
7
7
  import { FastifyRequestType, FastifyTypeProvider, FastifyTypeProviderDefault, ResolveFastifyRequestType } from './type-provider'
8
- import { ContextConfigDefault, RawRequestDefaultExpression, RawServerBase, RawServerDefault, RequestBodyDefault, RequestHeadersDefault, RequestParamsDefault, RequestQuerystringDefault } from './utils'
8
+ import { ContextConfigDefault, HTTPMethods, RawRequestDefaultExpression, RawServerBase, RawServerDefault, RequestBodyDefault, RequestHeadersDefault, RequestParamsDefault, RequestQuerystringDefault } from './utils'
9
9
 
10
10
  type HTTPRequestPart = 'body' | 'query' | 'querystring' | 'params' | 'headers'
11
11
  export interface RequestGenericInterface {
@@ -21,7 +21,7 @@ export interface ValidationFunction {
21
21
  }
22
22
 
23
23
  export interface RequestRouteOptions<ContextConfig = ContextConfigDefault, SchemaCompiler = FastifySchema> {
24
- method: string;
24
+ method: HTTPMethods | HTTPMethods[];
25
25
  // `url` can be `undefined` for instance when `request.is404` is true
26
26
  url: string | undefined;
27
27
  bodyLimit: number;
@@ -87,8 +87,4 @@ export interface FastifyRequest<RouteGeneric extends RouteGenericInterface = Rou
87
87
  compileValidationSchema(schema: { [key: string]: any }, httpPart?: HTTPRequestPart): ValidationFunction
88
88
  validateInput(input: any, schema: { [key: string]: any }, httpPart?: HTTPRequestPart): boolean
89
89
  validateInput(input: any, httpPart?: HTTPRequestPart): boolean
90
-
91
- // Prefer `socket` over deprecated `connection` property in node 13.0.0 or higher
92
- // @deprecated
93
- readonly connection: RawRequest['socket'];
94
90
  }
@@ -1,7 +1,7 @@
1
1
  import { RawServerBase, RawServerDefault, RawReplyDefaultExpression, RawRequestDefaultExpression } from './utils'
2
- import * as http from 'http'
3
- import * as https from 'https'
4
- import * as http2 from 'http2'
2
+ import * as http from 'node:http'
3
+ import * as https from 'node:https'
4
+ import * as http2 from 'node:http2'
5
5
 
6
6
  export type FastifyServerFactoryHandler<
7
7
  RawServer extends RawServerBase = RawServerDefault,
package/types/utils.d.ts CHANGED
@@ -1,14 +1,23 @@
1
- import * as http from 'http'
2
- import * as http2 from 'http2'
3
- import * as https from 'https'
1
+ import * as http from 'node:http'
2
+ import * as http2 from 'node:http2'
3
+ import * as https from 'node:https'
4
+
5
+ type AutocompletePrimitiveBaseType<T> =
6
+ T extends string ? string :
7
+ T extends number ? number :
8
+ T extends boolean ? boolean :
9
+ never
10
+
11
+ export type Autocomplete<T> = T | (AutocompletePrimitiveBaseType<T> & Record<never, never>)
4
12
 
5
13
  /**
6
14
  * Standard HTTP method strings
15
+ * for internal use
7
16
  */
8
17
  type _HTTPMethods = 'DELETE' | 'GET' | 'HEAD' | 'PATCH' | 'POST' | 'PUT' | 'OPTIONS' |
9
18
  'PROPFIND' | 'PROPPATCH' | 'MKCOL' | 'COPY' | 'MOVE' | 'LOCK' | 'UNLOCK' | 'TRACE' | 'SEARCH' | 'REPORT' | 'MKCALENDAR'
10
19
 
11
- export type HTTPMethods = Uppercase<_HTTPMethods> | Lowercase<_HTTPMethods>
20
+ export type HTTPMethods = Autocomplete<_HTTPMethods | Lowercase<_HTTPMethods>>
12
21
 
13
22
  /**
14
23
  * A union type of the Node.js server types from the http, https, and http2 modules.
@@ -56,7 +65,6 @@ type Digit = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
56
65
  type HttpCodes = StringAsNumber<`${CodeClasses}${Digit}${Digit}`>
57
66
  type HttpKeys = HttpCodes | `${Digit}xx`
58
67
  export type StatusCodeReply = {
59
-
60
68
  [Key in HttpKeys]?: unknown;
61
69
  }
62
70
 
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });