fastify 4.26.2 → 5.0.0-aplha.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 (115) hide show
  1. package/.markdownlint-cli2.yaml +1 -1
  2. package/.taprc +1 -0
  3. package/README.md +6 -4
  4. package/SPONSORS.md +1 -1
  5. package/build/build-validation.js +3 -3
  6. package/docs/Guides/Database.md +2 -2
  7. package/docs/Guides/Delay-Accepting-Requests.md +4 -4
  8. package/docs/Guides/Detecting-When-Clients-Abort.md +6 -4
  9. package/docs/Guides/Ecosystem.md +21 -11
  10. package/docs/Guides/Getting-Started.md +2 -0
  11. package/docs/Guides/Migration-Guide-V5.md +20 -0
  12. package/docs/Guides/Plugins-Guide.md +1 -1
  13. package/docs/Guides/Prototype-Poisoning.md +1 -1
  14. package/docs/Reference/ContentTypeParser.md +8 -1
  15. package/docs/Reference/Decorators.md +2 -2
  16. package/docs/Reference/Errors.md +10 -5
  17. package/docs/Reference/Hooks.md +5 -3
  18. package/docs/Reference/LTS.md +32 -20
  19. package/docs/Reference/Logging.md +6 -6
  20. package/docs/Reference/Reply.md +3 -3
  21. package/docs/Reference/Request.md +7 -3
  22. package/docs/Reference/Routes.md +22 -25
  23. package/docs/Reference/Server.md +24 -57
  24. package/docs/Reference/Type-Providers.md +10 -4
  25. package/docs/Reference/TypeScript.md +25 -24
  26. package/docs/Reference/Validation-and-Serialization.md +1 -1
  27. package/docs/Reference/Warnings.md +0 -4
  28. package/fastify.d.ts +2 -1
  29. package/fastify.js +38 -9
  30. package/lib/configValidator.js +92 -75
  31. package/lib/contentTypeParser.js +54 -88
  32. package/lib/error-handler.js +34 -21
  33. package/lib/errors.js +27 -6
  34. package/lib/fourOhFour.js +5 -3
  35. package/lib/handleRequest.js +15 -21
  36. package/lib/httpMethods.js +34 -16
  37. package/lib/logger.js +24 -6
  38. package/lib/pluginUtils.js +3 -3
  39. package/lib/request.js +32 -11
  40. package/lib/route.js +6 -31
  41. package/lib/server.js +5 -106
  42. package/lib/warnings.js +24 -17
  43. package/lib/wrapThenable.js +2 -0
  44. package/package.json +31 -30
  45. package/test/404s.test.js +2 -2
  46. package/test/500s.test.js +32 -0
  47. package/test/async-await.test.js +46 -2
  48. package/test/check.test.js +225 -0
  49. package/test/close.test.js +1 -1
  50. package/test/content-parser.test.js +69 -117
  51. package/test/custom-parser.1.test.js +40 -1
  52. package/test/delete.test.js +21 -1
  53. package/test/encapsulated-error-handler.test.js +1 -1
  54. package/test/esm/errorCodes.test.mjs +10 -0
  55. package/test/esm/named-exports.mjs +1 -0
  56. package/test/esm/other.mjs +1 -0
  57. package/test/esm/plugin.mjs +1 -0
  58. package/test/findRoute.test.js +16 -0
  59. package/test/genReqId.test.js +9 -0
  60. package/test/get.test.js +28 -0
  61. package/test/has-route.test.js +1 -1
  62. package/test/helper.js +1 -5
  63. package/test/hooks.test.js +2 -2
  64. package/test/http2/constraint.test.js +22 -1
  65. package/test/http2/plain.test.js +21 -6
  66. package/test/http2/secure.test.js +12 -1
  67. package/test/https/https.test.js +57 -0
  68. package/test/internals/errors.test.js +47 -17
  69. package/test/internals/handleRequest.test.js +2 -2
  70. package/test/internals/initialConfig.test.js +5 -5
  71. package/test/internals/logger.test.js +31 -2
  72. package/test/internals/request.test.js +13 -11
  73. package/test/lock.test.js +43 -10
  74. package/test/logger/instantiation.test.js +9 -9
  75. package/test/logger/logging.test.js +41 -20
  76. package/test/logger/options.test.js +103 -22
  77. package/test/logger/response.test.js +6 -6
  78. package/test/method-missing.test.js +24 -0
  79. package/test/mkcalendar.test.js +165 -0
  80. package/test/plugin.4.test.js +27 -31
  81. package/test/propfind.test.js +40 -8
  82. package/test/proppatch.test.js +48 -18
  83. package/test/report.test.js +161 -0
  84. package/test/route-shorthand.test.js +60 -0
  85. package/test/schema-examples.test.js +1 -1
  86. package/test/schema-feature.test.js +6 -6
  87. package/test/schema-serialization.test.js +2 -2
  88. package/test/schema-special-usage.test.js +1 -1
  89. package/test/server.test.js +16 -0
  90. package/test/skip-reply-send.test.js +1 -1
  91. package/test/stream.2.test.js +1 -1
  92. package/test/stream.5.test.js +4 -4
  93. package/test/trust-proxy.test.js +33 -27
  94. package/test/types/errors.test-d.ts +0 -2
  95. package/test/types/fastify.test-d.ts +1 -1
  96. package/test/types/instance.test-d.ts +4 -50
  97. package/test/types/logger.test-d.ts +42 -4
  98. package/test/types/register.test-d.ts +1 -1
  99. package/test/types/reply.test-d.ts +2 -2
  100. package/test/types/request.test-d.ts +5 -3
  101. package/test/types/route.test-d.ts +6 -2
  102. package/test/types/type-provider.test-d.ts +7 -3
  103. package/test/url-rewriting.test.js +3 -2
  104. package/test/useSemicolonDelimiter.test.js +3 -6
  105. package/test/versioned-routes.test.js +1 -1
  106. package/types/instance.d.ts +12 -28
  107. package/types/logger.d.ts +1 -1
  108. package/types/reply.d.ts +6 -4
  109. package/types/request.d.ts +4 -2
  110. package/types/type-provider.d.ts +1 -1
  111. package/types/utils.d.ts +1 -1
  112. package/test/default-route.test.js +0 -88
  113. package/test/listen.deprecated.test.js +0 -229
  114. package/test/unsupported-httpversion.test.js +0 -31
  115. /package/test/esm/{esm.mjs → esm.test.mjs} +0 -0
@@ -7,7 +7,7 @@ config:
7
7
  MD013:
8
8
  line_length: 80
9
9
  code_block_line_length: 120
10
- headers: false
10
+ headings: false
11
11
  tables: false
12
12
  strict: false
13
13
  stern: false
package/.taprc CHANGED
@@ -8,3 +8,4 @@ node-arg: --allow-natives-syntax
8
8
 
9
9
  files:
10
10
  - 'test/**/*.test.js'
11
+ - 'test/**/*.test.mjs'
package/README.md CHANGED
@@ -46,10 +46,8 @@ developer experience with the least overhead and a powerful plugin architecture.
46
46
  It is inspired by Hapi and Express and as far as we know, it is one of the
47
47
  fastest web frameworks in town.
48
48
 
49
- The `main` branch refers to the Fastify `v4` release. Check out the
50
- [`v3.x` branch](https://github.com/fastify/fastify/tree/3.x) for `v3`.
51
-
52
-
49
+ The `main` branch refers to the Fastify `v5` release, which is not released/LTS yet.
50
+ Check out the [`4.x` branch](https://github.com/fastify/fastify/tree/4.x) for `v4`.
53
51
 
54
52
  ### Table of Contents
55
53
 
@@ -125,6 +123,7 @@ yarn add fastify
125
123
 
126
124
  // ESM
127
125
  import Fastify from 'fastify'
126
+
128
127
  const fastify = Fastify({
129
128
  logger: true
130
129
  })
@@ -150,6 +149,7 @@ with async-await:
150
149
  ```js
151
150
  // ESM
152
151
  import Fastify from 'fastify'
152
+
153
153
  const fastify = Fastify({
154
154
  logger: true
155
155
  })
@@ -323,6 +323,8 @@ listed in alphabetical order.
323
323
  <https://twitter.com/jsumners79>, <https://www.npmjs.com/~jsumners>
324
324
  * [__Aras Abbasi__](https://github.com/uzlopak),
325
325
  <https://www.npmjs.com/~uzlopak>
326
+ * [__Gürgün Dayıoğlu__](https://github.com/gurgunday),
327
+ <https://www.npmjs.com/~gurgunday>
326
328
 
327
329
  ### Fastify Plugins team
328
330
  * [__Matteo Collina__](https://github.com/mcollina),
package/SPONSORS.md CHANGED
@@ -13,7 +13,7 @@ _Be the first!_
13
13
 
14
14
  ## Tier 3
15
15
 
16
- _Be the first!_
16
+ - [Mercedes-Benz Group](https://github.com/mercedes-benz)
17
17
 
18
18
  ## Tier 2
19
19
 
@@ -38,11 +38,11 @@ const defaultInitOptions = {
38
38
  onProtoPoisoning: 'error',
39
39
  onConstructorPoisoning: 'error',
40
40
  pluginTimeout: 10000,
41
- requestIdHeader: 'request-id',
41
+ requestIdHeader: false,
42
42
  requestIdLogLabel: 'reqId',
43
43
  http2SessionTimeout: 72000, // 72 seconds
44
44
  exposeHeadRoutes: true,
45
- useSemicolonDelimiter: true
45
+ useSemicolonDelimiter: false
46
46
  }
47
47
 
48
48
  const schema = {
@@ -98,7 +98,7 @@ const schema = {
98
98
  onProtoPoisoning: { type: 'string', default: defaultInitOptions.onProtoPoisoning },
99
99
  onConstructorPoisoning: { type: 'string', default: defaultInitOptions.onConstructorPoisoning },
100
100
  pluginTimeout: { type: 'integer', default: defaultInitOptions.pluginTimeout },
101
- requestIdHeader: { anyOf: [{ enum: [false] }, { type: 'string' }], default: defaultInitOptions.requestIdHeader },
101
+ requestIdHeader: { anyOf: [{ type: 'boolean' }, { type: 'string' }], default: defaultInitOptions.requestIdHeader },
102
102
  requestIdLogLabel: { type: 'string', default: defaultInitOptions.requestIdLogLabel },
103
103
  http2SessionTimeout: { type: 'integer', default: defaultInitOptions.http2SessionTimeout },
104
104
  exposeHeadRoutes: { type: 'boolean', default: defaultInitOptions.exposeHeadRoutes },
@@ -237,14 +237,14 @@ development. Migrations provide a repeatable and testable way to modify a
237
237
  database's schema and prevent data loss.
238
238
 
239
239
  As stated at the beginning of the guide, Fastify is database agnostic and any
240
- NodeJS database migration tool can be used with it. We will give an example of
240
+ Node.js database migration tool can be used with it. We will give an example of
241
241
  using [Postgrator](https://www.npmjs.com/package/postgrator) which has support
242
242
  for Postgres, MySQL, SQL Server and SQLite. For MongoDB migrations, please check
243
243
  [migrate-mongo](https://www.npmjs.com/package/migrate-mongo).
244
244
 
245
245
  #### [Postgrator](https://www.npmjs.com/package/postgrator)
246
246
 
247
- Postgrator is NodeJS SQL migration tool that uses a directory of SQL scripts to
247
+ Postgrator is Node.js SQL migration tool that uses a directory of SQL scripts to
248
248
  alter the database schema. Each file an migrations folder need to follow the
249
249
  pattern: ` [version].[action].[optional-description].sql`.
250
250
 
@@ -49,7 +49,7 @@ That will be achieved by wrapping into a custom plugin two main features:
49
49
 
50
50
  1. the mechanism for authenticating with the provider
51
51
  [decorating](../Reference/Decorators.md) the `fastify` object with the
52
- authentication key (`magicKey` from here onwards)
52
+ authentication key (`magicKey` from here onward)
53
53
  1. the mechanism for denying requests that would, otherwise, fail
54
54
 
55
55
  ### Hands-on
@@ -103,7 +103,7 @@ server.get('/v1*', async function (request, reply) {
103
103
  }
104
104
  })
105
105
 
106
- server.decorate('magicKey', null)
106
+ server.decorate('magicKey')
107
107
 
108
108
  server.listen({ port: '1234' }, () => {
109
109
  provider.thirdPartyMagicKeyGenerator(USUAL_WAIT_TIME_MS)
@@ -303,7 +303,7 @@ async function setup(fastify) {
303
303
  fastify.server.on('listening', doMagic)
304
304
 
305
305
  // Set up the placeholder for the magicKey
306
- fastify.decorate('magicKey', null)
306
+ fastify.decorate('magicKey')
307
307
 
308
308
  // Our magic -- important to make sure errors are handled. Beware of async
309
309
  // functions outside `try/catch` blocks
@@ -406,7 +406,7 @@ https://nodejs.org/api/net.html#event-listening). We use that to reach out to
406
406
  our provider as soon as possible, with the `doMagic` function.
407
407
 
408
408
  ```js
409
- fastify.decorate('magicKey', null)
409
+ fastify.decorate('magicKey')
410
410
  ```
411
411
 
412
412
  The `magicKey` decoration is also part of the plugin now. We initialize it with
@@ -87,10 +87,12 @@ of `{ ok: true }`.
87
87
  - Logic that triggers in the hook when the request is closed.
88
88
  - Logging that occurs when the closed request property `aborted` is true.
89
89
 
90
- In the request close event, you should examine the diff between a successful
91
- request and one aborted by the client to determine the best property for your
92
- use case. You can find details about request properties in the
93
- [NodeJS documentation](https://nodejs.org/api/http.html).
90
+ Whilst the `aborted` property has been deprecated, `destroyed` is not a
91
+ suitable replacement as the
92
+ [Node.js documentation suggests](https://nodejs.org/api/http.html#requestaborted).
93
+ A request can be `destroyed` for various reasons, such as when the server closes
94
+ the connection. The `aborted` property is still the most reliable way to detect
95
+ when a client intentionally aborts a request.
94
96
 
95
97
  You can also perform this logic outside of a hook, directly in a specific route.
96
98
 
@@ -185,7 +185,7 @@ section.
185
185
  - [`@fastify-userland/typeorm-query-runner`](https://github.com/fastify-userland/typeorm-query-runner)
186
186
  Fastify typeorm QueryRunner plugin
187
187
  - [`@gquittet/graceful-server`](https://github.com/gquittet/graceful-server)
188
- Tiny (~5k), Fast, KISS, and dependency-free Node.JS library to make your
188
+ Tiny (~5k), Fast, KISS, and dependency-free Node.js library to make your
189
189
  Fastify API graceful.
190
190
  - [`@h4ad/serverless-adapter`](https://github.com/H4ad/serverless-adapter)
191
191
  Run REST APIs and other web applications using your existing Node.js
@@ -222,6 +222,8 @@ section.
222
222
  API key management solution.
223
223
  - [`arecibo`](https://github.com/nucleode/arecibo) Fastify ping responder for
224
224
  Kubernetes Liveness and Readiness Probes.
225
+ - [`aws-xray-sdk-fastify`](https://github.com/aws/aws-xray-sdk-node/tree/master/sdk_contrib/fastify)
226
+ A Fastify plugin to log requests and subsegments through AWSXray.
225
227
  - [`cls-rtracer`](https://github.com/puzpuzpuz/cls-rtracer) Fastify middleware
226
228
  for CLS-based request ID generation. An out-of-the-box solution for adding
227
229
  request IDs into your logs.
@@ -247,9 +249,12 @@ section.
247
249
  [`@angular/platform-server`](https://github.com/angular/angular/tree/master/packages/platform-server)
248
250
  for Fastify
249
251
  - [`fastify-api-key`](https://github.com/arkerone/fastify-api-key) Fastify
250
- plugin to authenticate HTTP requests based on api key and signature
252
+ plugin to authenticate HTTP requests based on API key and signature
251
253
  - [`fastify-appwrite`](https://github.com/Dev-Manny/fastify-appwrite) Fastify
252
254
  Plugin for interacting with Appwrite server.
255
+ - [`fastify-asyncforge`](https://github.com/mcollina/fastify-asyncforge) Plugin
256
+ to access Fastify instance, logger, request and reply from Node.js [Async
257
+ Local Storage](https://nodejs.org/api/async_context.html#class-asynclocalstorage).
253
258
  - [`fastify-at-mysql`](https://github.com/mateonunez/fastify-at-mysql) Fastify
254
259
  MySQL plugin with auto SQL injection attack prevention.
255
260
  - [`fastify-at-postgres`](https://github.com/mateonunez/fastify-at-postgres) Fastify
@@ -288,7 +293,7 @@ section.
288
293
  - [`fastify-bugsnag`](https://github.com/ZigaStrgar/fastify-bugsnag) Fastify plugin
289
294
  to add support for [Bugsnag](https://www.bugsnag.com/) error reporting.
290
295
  - [`fastify-cacheman`](https://gitlab.com/aalfiann/fastify-cacheman)
291
- Small and efficient cache provider for Node.JS with In-memory, File, Redis
296
+ Small and efficient cache provider for Node.js with In-memory, File, Redis
292
297
  and MongoDB engines for Fastify
293
298
  - [`fastify-casbin`](https://github.com/nearform/fastify-casbin) Casbin support
294
299
  for Fastify.
@@ -304,9 +309,7 @@ section.
304
309
  - [`fastify-cloudflare-turnstile`](https://github.com/112RG/fastify-cloudflare-turnstile)
305
310
  Fastify plugin for CloudFlare Turnstile.
306
311
  - [`fastify-cloudinary`](https://github.com/Vanilla-IceCream/fastify-cloudinary)
307
- The Cloudinary Fastify SDK allows you to quickly and easily integrate your
308
- application with Cloudinary. Effortlessly optimize and transform your cloud's
309
- assets.
312
+ Plugin to share a common Cloudinary connection across Fastify.
310
313
  - [`fastify-cockroachdb`](https://github.com/alex-ppg/fastify-cockroachdb)
311
314
  Fastify plugin to connect to a CockroachDB PostgreSQL instance via the
312
315
  Sequelize ORM.
@@ -416,6 +419,8 @@ section.
416
419
  Lightweight, proxy-aware redirect plugin from HTTP to HTTPS.
417
420
  - [`fastify-https-redirect`](https://github.com/tomsvogel/fastify-https-redirect)
418
421
  Fastify plugin for auto-redirect from HTTP to HTTPS.
422
+ - [`fastify-i18n`](https://github.com/Vanilla-IceCream/fastify-i18n)
423
+ Internationalization plugin for Fastify. Built upon node-polyglot.
419
424
  - [`fastify-impressions`](https://github.com/manju4ever/fastify-impressions)
420
425
  Fastify plugin to track impressions of all the routes.
421
426
  - [`fastify-influxdb`](https://github.com/alex-ppg/fastify-influxdb) Fastify
@@ -483,6 +488,9 @@ middlewares into Fastify plugins
483
488
  [mqtt](https://www.npmjs.com/package/mqtt) client across Fastify.
484
489
  - [`fastify-msgpack`](https://github.com/kenriortega/fastify-msgpack) Fastify
485
490
  and MessagePack, together at last. Uses @msgpack/msgpack by default.
491
+ - [`fastify-msgraph-webhook`](https://github.com/flower-of-the-bridges/fastify-msgraph-change-notifications-webhook)
492
+ to manage
493
+ [MS Graph Change Notifications webhooks](https://learn.microsoft.com/it-it/graph/change-notifications-delivery-webhooks?tabs=http).
486
494
  - [`fastify-multer`](https://github.com/fox1t/fastify-multer) Multer is a plugin
487
495
  for handling multipart/form-data, which is primarily used for uploading files.
488
496
  - [`fastify-nats`](https://github.com/mahmed8003/fastify-nats) Plugin to share
@@ -569,7 +577,7 @@ middlewares into Fastify plugins
569
577
  - [`fastify-rbac`](https://gitlab.com/m03geek/fastify-rbac) Fastify role-based
570
578
  access control plugin.
571
579
  - [`fastify-recaptcha`](https://github.com/qwertyforce/fastify-recaptcha)
572
- Fastify plugin for recaptcha verification.
580
+ Fastify plugin for reCAPTCHA verification.
573
581
  - [`fastify-redis-channels`](https://github.com/hearit-io/fastify-redis-channels)
574
582
  A plugin for fast, reliable, and scalable channels implementation based on
575
583
  Redis streams.
@@ -602,7 +610,7 @@ middlewares into Fastify plugins
602
610
  - [`fastify-sentry`](https://github.com/alex-ppg/fastify-sentry) Fastify plugin
603
611
  to add the Sentry SDK error handler to requests.
604
612
  - [`fastify-sequelize`](https://github.com/lyquocnam/fastify-sequelize) Fastify
605
- plugin work with Sequelize (adapter for NodeJS -> Sqlite, Mysql, Mssql,
613
+ plugin work with Sequelize (adapter for Node.js -> Sqlite, Mysql, Mssql,
606
614
  Postgres).
607
615
  - [`fastify-server-session`](https://github.com/jsumners/fastify-server-session)
608
616
  A session plugin with support for arbitrary backing caches via
@@ -682,10 +690,8 @@ middlewares into Fastify plugins
682
690
  [uws](https://github.com/uNetworking/uWebSockets).
683
691
  - [`fastify-xml-body-parser`](https://github.com/NaturalIntelligence/fastify-xml-body-parser)
684
692
  Parse XML payload / request body into JS / JSON object.
685
- - [`fastify-xray`](https://github.com/jeromemacias/fastify-xray) Fastify plugin
686
- for AWS XRay recording.
687
693
  - [`http-wizard`](https://github.com/flodlc/http-wizard)
688
- Exports a typescript api client for your Fastify api and ensures fullstack type
694
+ Exports a typescript API client for your Fastify API and ensures fullstack type
689
695
  safety for your project.
690
696
  - [`i18next-http-middleware`](https://github.com/i18next/i18next-http-middleware#fastify-usage)
691
697
  An [i18next](https://www.i18next.com) based i18n (internationalization)
@@ -722,3 +728,7 @@ middlewares into Fastify plugins
722
728
  Fastify APIs using decorators and convert Typescript interface to JSON Schema.
723
729
  - [`simple-tjscli`](https://github.com/imjuni/simple-tjscli) CLI tool to
724
730
  generate JSON Schema from TypeScript interfaces.
731
+ - [`vite-plugin-fastify`](https://github.com/Vanilla-IceCream/vite-plugin-fastify)
732
+ Fastify plugin for Vite with Hot-module Replacement.
733
+ - [`vite-plugin-fastify-routes`](https://github.com/Vanilla-IceCream/vite-plugin-fastify-routes)
734
+ File-based routing for Fastify applications using Vite.
@@ -32,6 +32,7 @@ Let's write our first server:
32
32
 
33
33
  // ESM
34
34
  import Fastify from 'fastify'
35
+
35
36
  const fastify = Fastify({
36
37
  logger: true
37
38
  })
@@ -68,6 +69,7 @@ Do you prefer to use `async/await`? Fastify supports it out-of-the-box.
68
69
  ```js
69
70
  // ESM
70
71
  import Fastify from 'fastify'
72
+
71
73
  const fastify = Fastify({
72
74
  logger: true
73
75
  })
@@ -0,0 +1,20 @@
1
+ # V5 Migration Guide
2
+
3
+ This guide is intended to help with migration from Fastify v4 to v5.
4
+
5
+ Before migrating to v5, please ensure that you have fixed all deprecation
6
+ warnings from v4. All v4 deprecations have been removed and they will no longer
7
+ work after upgrading.
8
+
9
+ ## Breaking Changes
10
+
11
+ ### `useSemicolonDelimiter` false by default
12
+
13
+ Starting with v5, Fastify instances will no longer default to supporting the use
14
+ of semicolon delimiters in the query string as they did in v4.
15
+ This is due to it being non-standard
16
+ behavior and not adhering to [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986#section-3.4).
17
+
18
+ If you still wish to use semicolons as delimiters, you can do so by
19
+ setting `useSemicolonDelimiter: true` in the server configuration.
20
+
@@ -353,7 +353,7 @@ This variant becomes extremely useful if you plan to distribute your plugin, as
353
353
  described in the next section.
354
354
 
355
355
  As you probably noticed by now, `request` and `reply` are not the standard
356
- Nodejs *request* and *response* objects, but Fastify's objects.
356
+ Node.js *request* and *response* objects, but Fastify's objects.
357
357
 
358
358
 
359
359
  ## How to handle encapsulation and distribution
@@ -43,7 +43,7 @@ defect a validation library can have.
43
43
  To understand this, we need to understand how JavaScript works a bit.
44
44
  Every object in JavaScript can have a prototype. It is a set of methods and
45
45
  properties it "inherits" from another object. I have put inherits in quotes
46
- because JavaScript isn't really an object-oriented language.It is prototype-
46
+ because JavaScript isn't really an object-oriented language. It is a prototype-
47
47
  based object-oriented language.
48
48
 
49
49
  A long time ago, for a bunch of irrelevant reasons, someone decided that it
@@ -29,6 +29,13 @@ is given in the content-type header. If it is not given, the
29
29
  [catch-all](#catch-all) parser is not executed as with `POST`, `PUT` and
30
30
  `PATCH`, but the payload is simply not parsed.
31
31
 
32
+ > ## ⚠ Security Notice
33
+ > When using with RegExp to detect `Content-Type`, you should beware of
34
+ > how to properly detect the `Content-Type`. For example, if you need
35
+ > `application/*`, you should use `/^application\/([\w-]+);?/` to match the
36
+ > [essence MIME type](https://mimesniff.spec.whatwg.org/#mime-type-miscellaneous)
37
+ > only.
38
+
32
39
  ### Usage
33
40
  ```js
34
41
  fastify.addContentTypeParser('application/jsoff', function (request, payload, done) {
@@ -52,7 +59,7 @@ fastify.addContentTypeParser('application/jsoff', async function (request, paylo
52
59
  })
53
60
 
54
61
  // Handle all content types that matches RegExp
55
- fastify.addContentTypeParser(/^image\/.*/, function (request, payload, done) {
62
+ fastify.addContentTypeParser(/^image\/([\w-]+);?/, function (request, payload, done) {
56
63
  imageParser(payload, function (err, body) {
57
64
  done(err, body)
58
65
  })
@@ -193,7 +193,7 @@ hook](./Hooks.md#onrequest). Example:
193
193
  const fp = require('fastify-plugin')
194
194
 
195
195
  async function myPlugin (app) {
196
- app.decorateRequest('foo', null)
196
+ app.decorateRequest('foo')
197
197
  app.addHook('onRequest', async (req, reply) => {
198
198
  req.foo = { bar: 42 }
199
199
  })
@@ -236,7 +236,7 @@ incoming request in the [`'onRequest'` hook](./Hooks.md#onrequest). Example:
236
236
  const fp = require('fastify-plugin')
237
237
 
238
238
  async function myPlugin (app) {
239
- app.decorateRequest('foo', null)
239
+ app.decorateRequest('foo')
240
240
  app.addHook('onRequest', async (req, reply) => {
241
241
  req.foo = { bar: 42 }
242
242
  })
@@ -44,6 +44,9 @@
44
44
  - [FST_ERR_HOOK_TIMEOUT](#fst_err_hook_timeout)
45
45
  - [FST_ERR_LOG_INVALID_DESTINATION](#fst_err_log_invalid_destination)
46
46
  - [FST_ERR_LOG_INVALID_LOGGER](#fst_err_log_invalid_logger)
47
+ - [FST_ERR_LOG_INVALID_LOGGER_INSTANCE](#fst_err_log_invalid_logger_instance)
48
+ - [FST_ERR_LOG_INVALID_LOGGER_CONFIG](#fst_err_log_invalid_logger_config)
49
+ - [FST_ERR_LOG_LOGGER_AND_LOGGER_INSTANCE_PROVIDED](#fst_err_log_logger_and_logger_instance_provided)
47
50
  - [FST_ERR_REP_INVALID_PAYLOAD_TYPE](#fst_err_rep_invalid_payload_type)
48
51
  - [FST_ERR_REP_RESPONSE_BODY_CONSUMED](#fst_err_rep_response_body_consumed)
49
52
  - [FST_ERR_REP_ALREADY_SENT](#fst_err_rep_already_sent)
@@ -70,7 +73,6 @@
70
73
  - [FST_ERR_DUPLICATED_ROUTE](#fst_err_duplicated_route)
71
74
  - [FST_ERR_BAD_URL](#fst_err_bad_url)
72
75
  - [FST_ERR_ASYNC_CONSTRAINT](#fst_err_async_constraint)
73
- - [FST_ERR_DEFAULT_ROUTE_INVALID_TYPE](#fst_err_default_route_invalid_type)
74
76
  - [FST_ERR_INVALID_URL](#fst_err_invalid_url)
75
77
  - [FST_ERR_ROUTE_OPTIONS_NOT_OBJ](#fst_err_route_options_not_obj)
76
78
  - [FST_ERR_ROUTE_DUPLICATED_HANDLER](#fst_err_route_duplicated_handler)
@@ -90,6 +92,7 @@
90
92
  - [FST_ERR_PARENT_PLUGIN_BOOTED](#fst_err_parent_plugin_booted)
91
93
  - [FST_ERR_PLUGIN_TIMEOUT](#fst_err_plugin_timeout)
92
94
  - [FST_ERR_PLUGIN_NOT_PRESENT_IN_INSTANCE](#fst_err_plugin_not_present_in_instance)
95
+ - [FST_ERR_PLUGIN_INVALID_ASYNC_HANDLER](#fst_err_plugin_invalid_async_handler)
93
96
  - [FST_ERR_VALIDATION](#fst_err_validation)
94
97
  - [FST_ERR_LISTEN_OPTIONS_INVALID](#fst_err_listen_options_invalid)
95
98
  - [FST_ERR_ERROR_HANDLER_NOT_FN](#fst_err_error_handler_not_fn)
@@ -313,9 +316,11 @@ Below is a table with all the error codes that Fastify uses.
313
316
  | <a id="fst_err_hook_timeout">FST_ERR_HOOK_TIMEOUT</a> | A callback for a hook timed out. | Increase the timeout for the hook. | [#3106](https://github.com/fastify/fastify/pull/3106) |
314
317
  | <a id="fst_err_log_invalid_destination">FST_ERR_LOG_INVALID_DESTINATION</a> | The logger does not accept the specified destination. | Use a `'stream'` or a `'file'` as the destination. | [#1168](https://github.com/fastify/fastify/pull/1168) |
315
318
  | <a id="fst_err_log_invalid_logger">FST_ERR_LOG_INVALID_LOGGER</a> | The logger should have all these methods: `'info'`, `'error'`, `'debug'`, `'fatal'`, `'warn'`, `'trace'`, `'child'`. | Use a logger with all the required methods. | [#4520](https://github.com/fastify/fastify/pull/4520) |
319
+ | <a id="fst_err_log_invalid_logger_instance">FST_ERR_LOG_INVALID_LOGGER_INSTANCE</a> | The `loggerInstance` only accepts a logger instance, not a configuration object. | To pass a configuration object, use `'logger'` instead. | [#5020](https://github.com/fastify/fastify/pull/5020) |
320
+ | <a id="fst_err_log_invalid_logger_config">FST_ERR_LOG_INVALID_LOGGER_CONFIG</a> | The logger option only accepts a configuration object, not a logger instance. | To pass an instance, use `'loggerInstance'` instead. | [#5020](https://github.com/fastify/fastify/pull/5020) |
321
+ | <a id="fst_err_log_logger_and_logger_instance_provided">FST_ERR_LOG_LOGGER_AND_LOGGER_INSTANCE_PROVIDED</a> | You cannot provide both `'logger'` and `'loggerInstance'`. | Please provide only one option. | [#5020](https://github.com/fastify/fastify/pull/5020) |
316
322
  | <a id="fst_err_rep_invalid_payload_type">FST_ERR_REP_INVALID_PAYLOAD_TYPE</a> | Reply payload can be either a `string` or a `Buffer`. | Use a `string` or a `Buffer` for the payload. | [#1168](https://github.com/fastify/fastify/pull/1168) |
317
- | <a id="fst_err_rep_response_body_consumed">FST_ERR_REP_RESPONSE_BODY_CONSUMED</a> | Using `Response` as reply payload
318
- but the body is being consumed. | Make sure you don't consume the `Response.body` | [#5286](https://github.com/fastify/fastify/pull/5286) |
323
+ | <a id="fst_err_rep_response_body_consumed">FST_ERR_REP_RESPONSE_BODY_CONSUMED</a> | Using `Response` as reply payload, but the body is being consumed. | Make sure you don't consume the `Response.body` | [#5286](https://github.com/fastify/fastify/pull/5286) |
319
324
  | <a id="fst_err_rep_already_sent">FST_ERR_REP_ALREADY_SENT</a> | A response was already sent. | - | [#1336](https://github.com/fastify/fastify/pull/1336) |
320
325
  | <a id="fst_err_rep_sent_value">FST_ERR_REP_SENT_VALUE</a> | The only possible value for `reply.sent` is `true`. | - | [#1336](https://github.com/fastify/fastify/pull/1336) |
321
326
  | <a id="fst_err_send_inside_onerr">FST_ERR_SEND_INSIDE_ONERR</a> | You cannot use `send` inside the `onError` hook. | - | [#1348](https://github.com/fastify/fastify/pull/1348) |
@@ -338,9 +343,8 @@ but the body is being consumed. | Make sure you don't consume the `Response.body
338
343
  | <a id="fst_err_init_opts_invalid">FST_ERR_INIT_OPTS_INVALID</a> | Invalid initialization options. | Use valid initialization options. | [#1471](https://github.com/fastify/fastify/pull/1471) |
339
344
  | <a id="fst_err_force_close_connections_idle_not_available">FST_ERR_FORCE_CLOSE_CONNECTIONS_IDLE_NOT_AVAILABLE</a> | Cannot set forceCloseConnections to `idle` as your HTTP server does not support `closeIdleConnections` method. | Use a different value for `forceCloseConnections`. | [#3925](https://github.com/fastify/fastify/pull/3925) |
340
345
  | <a id="fst_err_duplicated_route">FST_ERR_DUPLICATED_ROUTE</a> | The HTTP method already has a registered controller for that URL. | Use a different URL or register the controller for another HTTP method. | [#2954](https://github.com/fastify/fastify/pull/2954) |
341
- | <a id="fst_err_bad_url">FST_ERR_BAD_URL</a> | The router received an invalid url. | Use a valid URL. | [#2106](https://github.com/fastify/fastify/pull/2106) |
346
+ | <a id="fst_err_bad_url">FST_ERR_BAD_URL</a> | The router received an invalid URL. | Use a valid URL. | [#2106](https://github.com/fastify/fastify/pull/2106) |
342
347
  | <a id="fst_err_async_constraint">FST_ERR_ASYNC_CONSTRAINT</a> | The router received an error when using asynchronous constraints. | - | [#4323](https://github.com/fastify/fastify/pull/4323) |
343
- | <a id="fst_err_default_route_invalid_type">FST_ERR_DEFAULT_ROUTE_INVALID_TYPE</a> | The `defaultRoute` type should be a function. | Use a function for the `defaultRoute`. | [#2733](https://github.com/fastify/fastify/pull/2733) |
344
348
  | <a id="fst_err_invalid_url">FST_ERR_INVALID_URL</a> | URL must be a string. | Use a string for the URL. | [#3653](https://github.com/fastify/fastify/pull/3653) |
345
349
  | <a id="fst_err_route_options_not_obj">FST_ERR_ROUTE_OPTIONS_NOT_OBJ</a> | Options for the route must be an object. | Use an object for the route options. | [#4554](https://github.com/fastify/fastify/pull/4554) |
346
350
  | <a id="fst_err_route_duplicated_handler">FST_ERR_ROUTE_DUPLICATED_HANDLER</a> | Duplicate handler for the route is not allowed. | Use a different handler. | [#4554](https://github.com/fastify/fastify/pull/4554) |
@@ -360,6 +364,7 @@ but the body is being consumed. | Make sure you don't consume the `Response.body
360
364
  | <a id="fst_err_parent_plugin_booted">FST_ERR_PARENT_PLUGIN_BOOTED</a> | Impossible to load plugin because the parent (mapped directly from `avvio`) | - | [#3106](https://github.com/fastify/fastify/pull/3106) |
361
365
  | <a id="fst_err_plugin_timeout">FST_ERR_PLUGIN_TIMEOUT</a> | Plugin did not start in time. | Increase the timeout for the plugin. | [#3106](https://github.com/fastify/fastify/pull/3106) |
362
366
  | <a id="fst_err_plugin_not_present_in_instance">FST_ERR_PLUGIN_NOT_PRESENT_IN_INSTANCE</a> | The decorator is not present in the instance. | - | [#4554](https://github.com/fastify/fastify/pull/4554) |
367
+ | <a id="fst_err_plugin_invalid_async_handler">FST_ERR_PLUGIN_INVALID_ASYNC_HANDLER</a> | The plugin being registered mixes async and callback styles. | - | [#5141](https://github.com/fastify/fastify/pull/5141) |
363
368
  | <a id="fst_err_validation">FST_ERR_VALIDATION</a> | The Request failed the payload validation. | Check the request payload. | [#4824](https://github.com/fastify/fastify/pull/4824) |
364
369
  | <a id="fst_err_listen_options_invalid">FST_ERR_LISTEN_OPTIONS_INVALID</a> | Invalid listen options. | Check the listen options. | [#4886](https://github.com/fastify/fastify/pull/4886) |
365
370
  | <a id="fst_err_error_handler_not_fn">FST_ERR_ERROR_HANDLER_NOT_FN</a> | Error Handler must be a function | Provide a function to `setErrorHandler`. | [#5317](https://github.com/fastify/fastify/pull/5317) |
@@ -189,9 +189,11 @@ specific header in case of error.
189
189
  It is not intended for changing the error, and calling `reply.send` will throw
190
190
  an exception.
191
191
 
192
- This hook will be executed only after the `customErrorHandler` has been
193
- executed, and only if the `customErrorHandler` sends an error back to the user
194
- *(Note that the default `customErrorHandler` always sends the error back to the
192
+ This hook will be executed only after
193
+ the [Custom Error Handler set by `setErrorHandler`](./Server.md#seterrorhandler)
194
+ has been executed, and only if the custom error handler sends an error back to the
195
+ user
196
+ *(Note that the default error handler always sends the error back to the
195
197
  user)*.
196
198
 
197
199
  **Notice:** unlike the other hooks, passing an error to the `done` function is not
@@ -1,7 +1,8 @@
1
1
  <h1 align="center">Fastify</h1>
2
2
 
3
3
  ## Long Term Support
4
- <a id="lts"></a>
4
+
5
+ `<a id="lts"></a>`
5
6
 
6
7
  Fastify's Long Term Support (LTS) is provided according to the schedule laid out
7
8
  in this document:
@@ -10,18 +11,27 @@ in this document:
10
11
  versions, are supported for a minimum period of six months from their release
11
12
  date. The release date of any specific version can be found at
12
13
  [https://github.com/fastify/fastify/releases](https://github.com/fastify/fastify/releases).
13
-
14
14
  2. Major releases will receive security updates for an additional six months
15
15
  from the release of the next major release. After this period we will still
16
16
  review and release security fixes as long as they are provided by the
17
17
  community and they do not violate other constraints, e.g. minimum supported
18
18
  Node.js version.
19
-
20
19
  3. Major releases will be tested and verified against all Node.js release lines
21
20
  that are supported by the [Node.js LTS
22
21
  policy](https://github.com/nodejs/Release) within the LTS period of that
23
22
  given Fastify release line. This implies that only the latest Node.js release
24
23
  of a given line is supported.
24
+ 4. In addition to Node.js runtime, major releases of Fastify will also be tested
25
+ and verified against alternative runtimes that are compatible with Node.js.
26
+ The maintenance teams of these alternative runtimes are responsible for ensuring
27
+ and guaranteeing these tests work properly.
28
+ 1. [N|Solid](https://docs.nodesource.com/nsolid), maintained by NodeSource,
29
+ commits to testing and verifying each Fastify major release against the N|Solid
30
+ LTS versions that are current at the time of the Fastify release.
31
+ NodeSource guarantees that Fastify will be compatible and function correctly
32
+ with N|Solid, aligning with the support and compatibility scope of the N|Solid
33
+ LTS versions available at the time of the Fastify release.
34
+ This ensures users of N|Solid can confidently use Fastify.
25
35
 
26
36
  A "month" is defined as 30 consecutive days.
27
37
 
@@ -38,33 +48,35 @@ A "month" is defined as 30 consecutive days.
38
48
  > dependency as `"fastify": "~3.15.x"`. This will leave your application
39
49
  > vulnerable, so please use with caution.
40
50
 
41
- [semver]: https://semver.org/
42
-
43
51
  ### Schedule
44
- <a id="lts-schedule"></a>
45
52
 
46
- | Version | Release Date | End Of LTS Date | Node.js |
47
- | :------ | :----------- | :-------------- | :------------------- |
48
- | 1.0.0 | 2018-03-06 | 2019-09-01 | 6, 8, 9, 10, 11 |
49
- | 2.0.0 | 2019-02-25 | 2021-01-31 | 6, 8, 10, 12, 14 |
50
- | 3.0.0 | 2020-07-07 | 2023-06-30 | 10, 12, 14, 16, 18 |
51
- | 4.0.0 | 2022-06-08 | TBD | 14, 16, 18, 20 |
53
+ `<a id="lts-schedule"></a>`
54
+
55
+ | Version | Release Date | End Of LTS Date | Node.js | Nsolid(Node) |
56
+ | :------ | :----------- | :-------------- | :----------------- | :------------- |
57
+ | 1.0.0 | 2018-03-06 | 2019-09-01 | 6, 8, 9, 10, 11 | |
58
+ | 2.0.0 | 2019-02-25 | 2021-01-31 | 6, 8, 10, 12, 14 | |
59
+ | 3.0.0 | 2020-07-07 | 2023-06-30 | 10, 12, 14, 16, 18 | v5(18) |
60
+ | 4.0.0 | 2022-06-08 | TBD | 14, 16, 18, 20 | v5(18), v5(20) |
52
61
 
53
62
  ### CI tested operating systems
54
- <a id="supported-os"></a>
55
63
 
56
- Fastify uses GitHub Actions for CI testing, please refer to [GitHub's
64
+ `<a id="supported-os"></a>`
65
+
66
+ Fastify uses GitHub Actions for CI testing, please refer to [GitHub&#39;s
57
67
  documentation regarding workflow
58
68
  runners](https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners#supported-runners-and-hardware-resources)
59
69
  for further details on what the latest virtual environment is in relation to the
60
70
  YAML workflow labels below:
61
71
 
62
- | OS | YAML Workflow Label | Package Manager | Node.js |
63
- |---------|------------------------|---------------------------|--------------|
64
- | Linux | `ubuntu-latest` | npm | 14,16,18,20 |
65
- | Linux | `ubuntu-latest` | yarn,pnpm | 14,16,18,20 |
66
- | Windows | `windows-latest` | npm | 14,16,18,20 |
67
- | MacOS | `macos-latest` | npm | 14,16,18,20 |
72
+ | OS | YAML Workflow Label | Package Manager | Node.js | Nsolid(Node) |
73
+ | ------- | ------------------- | --------------- | ----------- | ------------- |
74
+ | Linux | `ubuntu-latest` | npm | 14,16,18,20 | v5(18),v5(20) |
75
+ | Linux | `ubuntu-latest` | yarn,pnpm | 14,16,18,20 | v5(18),v5(20) |
76
+ | Windows | `windows-latest` | npm | 14,16,18,20 | v5(18),v5(20) |
77
+ | MacOS | `macos-latest` | npm | 14,16,18,20 | v5(18),v5(20) |
68
78
 
69
79
  Using [yarn](https://yarnpkg.com/) might require passing the `--ignore-engines`
70
80
  flag.
81
+
82
+ [semver]: https://semver.org/
@@ -98,15 +98,15 @@ const fastify = require('fastify')({
98
98
  <a id="logging-request-id"></a>
99
99
 
100
100
  By default, Fastify adds an ID to every request for easier tracking. If the
101
- "request-id" header is present its value is used, otherwise a new incremental ID
102
- is generated. See Fastify Factory
103
- [`requestIdHeader`](./Server.md#factory-request-id-header) and Fastify Factory
104
- [`genReqId`](./Server.md#genreqid) for customization options.
101
+ requestIdHeader-option is set and the corresponding header is present than
102
+ its value is used, otherwise a new incremental ID is generated. See Fastify
103
+ Factory [`requestIdHeader`](./Server.md#factory-request-id-header) and Fastify
104
+ Factory [`genReqId`](./Server.md#genreqid) for customization options.
105
105
 
106
106
  The default logger is configured with a set of standard serializers that
107
107
  serialize objects with `req`, `res`, and `err` properties. The object received
108
108
  by `req` is the Fastify [`Request`](./Request.md) object, while the object
109
- received by `res` is the Fastify [`Reply`](./Reply.md) object. This behaviour
109
+ received by `res` is the Fastify [`Reply`](./Reply.md) object. This behavior
110
110
  can be customized by specifying custom serializers.
111
111
 
112
112
  ```js
@@ -243,7 +243,7 @@ const fastify = Fastify({
243
243
  method: request.method,
244
244
  url: request.url,
245
245
  headers: request.headers,
246
- hostname: request.hostname,
246
+ host: request.host,
247
247
  remoteAddress: request.ip,
248
248
  remotePort: request.socket.remotePort
249
249
  }
@@ -62,7 +62,7 @@ since the request was received by Fastify.
62
62
  - `.hasTrailer(key)` - Determine if a trailer has been set.
63
63
  - `.removeTrailer(key)` - Remove the value of a previously set trailer.
64
64
  - `.type(value)` - Sets the header `Content-Type`.
65
- - `.redirect([code,] dest)` - Redirect to the specified url, the status code is
65
+ - `.redirect([code,] dest)` - Redirect to the specified URL, the status code is
66
66
  optional (default to `302`).
67
67
  - `.callNotFound()` - Invokes the custom not found handler.
68
68
  - `.serialize(payload)` - Serializes the specified payload using the default
@@ -783,7 +783,7 @@ headers in one place. The payload provided inside `Response` is
783
783
  considered to be pre-serialized, so they will be sent unmodified
784
784
  without response validation.
785
785
 
786
- Plese be aware when using `Response`, the status code and headers
786
+ Please be aware when using `Response`, the status code and headers
787
787
  will not directly reflect to `reply.statusCode` and `reply.getHeaders()`.
788
788
  Such behavior is based on `Response` only allow `readonly` status
789
789
  code and headers. The data is not allow to be bi-direction editing,
@@ -842,7 +842,7 @@ To customize the JSON error output you can do it by:
842
842
  - add the additional properties to the `Error` instance
843
843
 
844
844
  Notice that if the returned status code is not in the response schema list, the
845
- default behaviour will be applied.
845
+ default behavior will be applied.
846
846
 
847
847
  ```js
848
848
  fastify.get('/', {