fastify 5.1.0 → 5.2.1

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 (120) hide show
  1. package/README.md +54 -50
  2. package/build/build-validation.js +1 -1
  3. package/docs/Guides/Database.md +15 -15
  4. package/docs/Guides/Detecting-When-Clients-Abort.md +28 -28
  5. package/docs/Guides/Ecosystem.md +13 -7
  6. package/docs/Guides/Index.md +1 -1
  7. package/docs/Guides/Migration-Guide-V4.md +11 -11
  8. package/docs/Guides/Migration-Guide-V5.md +5 -5
  9. package/docs/Guides/Plugins-Guide.md +1 -1
  10. package/docs/Guides/Prototype-Poisoning.md +3 -3
  11. package/docs/Guides/Recommendations.md +9 -9
  12. package/docs/Guides/Serverless.md +5 -5
  13. package/docs/Guides/Testing.md +8 -8
  14. package/docs/Guides/Write-Plugin.md +1 -1
  15. package/docs/Guides/Write-Type-Provider.md +3 -3
  16. package/docs/Reference/Decorators.md +2 -2
  17. package/docs/Reference/Errors.md +4 -2
  18. package/docs/Reference/Hooks.md +7 -7
  19. package/docs/Reference/LTS.md +8 -0
  20. package/docs/Reference/Logging.md +5 -4
  21. package/docs/Reference/Reply.md +54 -53
  22. package/docs/Reference/Request.md +49 -44
  23. package/docs/Reference/Routes.md +7 -7
  24. package/docs/Reference/Server.md +32 -28
  25. package/docs/Reference/TypeScript.md +9 -9
  26. package/docs/Reference/Validation-and-Serialization.md +5 -5
  27. package/examples/typescript-server.ts +1 -1
  28. package/fastify.d.ts +4 -4
  29. package/fastify.js +1 -1
  30. package/lib/error-handler.js +9 -9
  31. package/lib/errors.js +5 -1
  32. package/lib/hooks.js +4 -1
  33. package/lib/reply.js +4 -0
  34. package/lib/request.js +16 -11
  35. package/package.json +21 -11
  36. package/test/{allowUnsafeRegex.test.js → allow-unsafe-regex.test.js} +4 -4
  37. package/test/async-dispose.test.js +0 -1
  38. package/test/async_hooks.test.js +4 -4
  39. package/test/buffer.test.js +4 -4
  40. package/test/build-certificate.js +1 -1
  41. package/test/bundler/README.md +5 -5
  42. package/test/case-insensitive.test.js +10 -10
  43. package/test/check.test.js +2 -3
  44. package/test/{childLoggerFactory.test.js → child-logger-factory.test.js} +1 -1
  45. package/test/client-timeout.test.js +1 -1
  46. package/test/close-pipelining.test.js +0 -1
  47. package/test/conditional-pino.test.js +3 -3
  48. package/test/content-length.test.js +53 -68
  49. package/test/content-parser.test.js +178 -167
  50. package/test/content-type.test.js +8 -9
  51. package/test/context-config.test.js +44 -54
  52. package/test/custom-parser.2.test.js +19 -20
  53. package/test/custom-parser.3.test.js +56 -45
  54. package/test/custom-parser.5.test.js +32 -32
  55. package/test/delete.test.js +79 -67
  56. package/test/encapsulated-child-logger-factory.test.js +8 -8
  57. package/test/encapsulated-error-handler.test.js +20 -20
  58. package/test/fastify-instance.test.js +33 -34
  59. package/test/{findRoute.test.js → find-route.test.js} +11 -10
  60. package/test/fluent-schema.test.js +33 -36
  61. package/test/handler-context.test.js +11 -11
  62. package/test/has-route.test.js +12 -15
  63. package/test/header-overflow.test.js +13 -12
  64. package/test/hooks.on-ready.test.js +2 -2
  65. package/test/hooks.test.js +19 -19
  66. package/test/http-methods/head.test.js +0 -3
  67. package/test/imports.test.js +2 -2
  68. package/test/internals/errors.test.js +20 -8
  69. package/test/listen.5.test.js +9 -9
  70. package/test/{maxRequestsPerSocket.test.js → max-requests-per-socket.test.js} +30 -30
  71. package/test/middleware.test.js +4 -5
  72. package/test/noop-set.test.js +1 -1
  73. package/test/nullable-validation.test.js +30 -27
  74. package/test/post-empty-body.test.js +18 -11
  75. package/test/pretty-print.test.js +59 -49
  76. package/test/proto-poisoning.test.js +42 -37
  77. package/test/reply-code.test.js +34 -32
  78. package/test/{reply-earlyHints.test.js → reply-early-hints.test.js} +21 -19
  79. package/test/request-header-host.test.js +154 -12
  80. package/test/request-id.test.js +31 -25
  81. package/test/{requestTimeout.test.js → request-timeout.test.js} +11 -11
  82. package/test/route.3.test.js +3 -2
  83. package/test/route.8.test.js +20 -20
  84. package/test/router-options.test.js +80 -77
  85. package/test/schema-examples.test.js +72 -38
  86. package/test/server.test.js +12 -12
  87. package/test/set-error-handler.test.js +2 -3
  88. package/test/stream-serializers.test.js +10 -7
  89. package/test/sync-routes.test.js +18 -18
  90. package/test/trust-proxy.test.js +51 -45
  91. package/test/type-provider.test.js +8 -6
  92. package/test/types/content-type-parser.test-d.ts +1 -1
  93. package/test/types/fastify.test-d.ts +4 -4
  94. package/test/types/instance.test-d.ts +3 -1
  95. package/test/types/logger.test-d.ts +2 -2
  96. package/test/types/plugin.test-d.ts +2 -2
  97. package/test/types/register.test-d.ts +2 -2
  98. package/test/types/reply.test-d.ts +1 -1
  99. package/test/types/route.test-d.ts +1 -1
  100. package/test/types/serverFactory.test-d.ts +1 -1
  101. package/test/types/type-provider.test-d.ts +1 -1
  102. package/test/url-rewriting.test.js +35 -38
  103. package/test/{useSemicolonDelimiter.test.js → use-semicolon-delimiter.test.js} +30 -30
  104. package/test/validation-error-handling.test.js +259 -285
  105. package/test/web-api.test.js +44 -0
  106. package/types/hooks.d.ts +1 -1
  107. package/types/instance.d.ts +9 -2
  108. package/types/reply.d.ts +1 -1
  109. package/types/request.d.ts +0 -4
  110. package/types/serverFactory.d.ts +3 -3
  111. package/types/utils.d.ts +3 -3
  112. package/.vscode/settings.json +0 -22
  113. package/test/test-reporter.mjs +0 -68
  114. /package/test/{connectionTimeout.test.js → connection-timeout.test.js} +0 -0
  115. /package/test/internals/{contentTypeParser.test.js → content-type-parser.test.js} +0 -0
  116. /package/test/internals/{handleRequest.test.js → handle-request.test.js} +0 -0
  117. /package/test/internals/{hookRunner.test.js → hook-runner.test.js} +0 -0
  118. /package/test/internals/{initialConfig.test.js → initial-config.test.js} +0 -0
  119. /package/test/internals/{reqIdGenFactory.test.js → req-id-gen-factory.test.js} +0 -0
  120. /package/test/{wrapThenable.test.js → wrap-thenable.test.js} +0 -0
@@ -227,6 +227,50 @@ test('Error when Response.bodyUsed', async (t) => {
227
227
  t.assert.strictEqual(body.code, 'FST_ERR_REP_RESPONSE_BODY_CONSUMED')
228
228
  })
229
229
 
230
+ test('Error when Response.body.locked', async (t) => {
231
+ t.plan(3)
232
+
233
+ const fastify = Fastify()
234
+
235
+ fastify.get('/', async function (request, reply) {
236
+ const stream = Readable.toWeb(fs.createReadStream(__filename))
237
+ const response = new Response(stream, {
238
+ status: 200,
239
+ headers: {
240
+ hello: 'world'
241
+ }
242
+ })
243
+ stream.getReader()
244
+ t.assert.strictEqual(stream.locked, true)
245
+ return reply.send(response)
246
+ })
247
+
248
+ const response = await fastify.inject({ method: 'GET', path: '/' })
249
+
250
+ t.assert.strictEqual(response.statusCode, 500)
251
+ const body = response.json()
252
+ t.assert.strictEqual(body.code, 'FST_ERR_REP_READABLE_STREAM_LOCKED')
253
+ })
254
+
255
+ test('Error when ReadableStream.locked', async (t) => {
256
+ t.plan(3)
257
+
258
+ const fastify = Fastify()
259
+
260
+ fastify.get('/', async function (request, reply) {
261
+ const stream = Readable.toWeb(fs.createReadStream(__filename))
262
+ stream.getReader()
263
+ t.assert.strictEqual(stream.locked, true)
264
+ return reply.send(stream)
265
+ })
266
+
267
+ const response = await fastify.inject({ method: 'GET', path: '/' })
268
+
269
+ t.assert.strictEqual(response.statusCode, 500)
270
+ const body = response.json()
271
+ t.assert.strictEqual(body.code, 'FST_ERR_REP_READABLE_STREAM_LOCKED')
272
+ })
273
+
230
274
  test('allow to pipe with fetch', async (t) => {
231
275
  t.plan(2)
232
276
  const abortController = new AbortController()
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'
@@ -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
  */
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'
@@ -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,6 +1,6 @@
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
4
 
5
5
  type AutocompletePrimitiveBaseType<T> =
6
6
  T extends string ? string :
@@ -1,22 +0,0 @@
1
- {
2
- "workbench.colorCustomizations": {
3
- "[GitHub Dark]": {
4
- "tab.activeBackground": "#0d0d0d",
5
- "tab.activeBorder": "#ffff00"
6
- },
7
- "activityBar.background": "#FBE7B2",
8
- "activityBar.foreground": "#52358C",
9
- "activityBar.inactiveForeground": "#616161",
10
- "activityBar.activeBorder": "#04184d",
11
- "activityBar.activeBackground": "#C3B48B",
12
- "activityBar.border": "#C3B48B",
13
- "titleBar.activeBackground": "#D2BE88",
14
- "titleBar.activeForeground": "#52358C",
15
- "titleBar.inactiveBackground": "#bdb59c",
16
- "titleBar.inactiveForeground": "#616161",
17
- "titleBar.border": "#C3B48B",
18
- "statusBar.background": "#E9DBB7",
19
- "statusBar.foreground": "#52358C",
20
- "statusBar.border": "#C3B48B"
21
- }
22
- }
@@ -1,68 +0,0 @@
1
- function colorize (type, text) {
2
- if (type === 'pass') {
3
- const blackText = `\x1b[30m${text}`
4
- const boldblackText = `\x1b[1m${blackText}`
5
- // Green background with black text
6
- return `\x1b[42m${boldblackText}\x1b[0m`
7
- }
8
-
9
- if (type === 'fail') {
10
- const whiteText = `\x1b[37m${text}`
11
- const boldWhiteText = `\x1b[1m${whiteText}`
12
- // Red background with white text
13
- return `\x1b[41m${boldWhiteText}\x1b[0m`
14
- }
15
-
16
- return text
17
- }
18
-
19
- function formatDiagnosticStr (str) {
20
- return str.replace(/^(\w+)(\s*\d*)/i, (_, firstWord, rest) => {
21
- return firstWord.charAt(0).toUpperCase() + firstWord.slice(1).toLowerCase() + ':' + rest
22
- })
23
- }
24
-
25
- async function * reporter (source) {
26
- const failed = new Set()
27
- const diagnostics = new Set()
28
-
29
- for await (const event of source) {
30
- switch (event.type) {
31
- case 'test:pass': {
32
- yield `${colorize('pass', 'PASSED')}: ${event.data.file || event.data.name}\n`
33
- break
34
- }
35
-
36
- case 'test:fail': {
37
- failed.add(event.data.name || event.data.file)
38
- yield `${colorize('fail', 'FAILED')}: ${event.data.file || event.data.name}\n`
39
- break
40
- }
41
-
42
- case 'test:diagnostic': {
43
- diagnostics.add(`${formatDiagnosticStr(event.data.message)}\n`)
44
- break
45
- }
46
-
47
- default: {
48
- yield ''
49
- }
50
- }
51
- }
52
-
53
- if (failed.size > 0) {
54
- yield `\n\n${colorize('fail', 'Failed tests:')}\n`
55
- for (const file of failed) {
56
- yield `${file}\n`
57
- }
58
- }
59
-
60
- yield '\n'
61
-
62
- for (const diagnostic of diagnostics) {
63
- yield `${diagnostic}`
64
- }
65
- yield '\n'
66
- }
67
-
68
- export default reporter