fastify 6.0.0-alpha.5 → 6.0.0-alpha.6

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 (46) hide show
  1. package/README.md +1 -0
  2. package/build/build-validation.js +2 -0
  3. package/docs/Guides/Ecosystem.md +4 -0
  4. package/docs/Guides/Migration-Guide-V5.md +2 -2
  5. package/docs/Guides/Serverless.md +33 -10
  6. package/docs/Reference/Errors.md +13 -0
  7. package/docs/Reference/Hooks.md +13 -0
  8. package/docs/Reference/Reply.md +786 -697
  9. package/docs/Reference/Request.md +303 -107
  10. package/docs/Reference/Server.md +39 -2
  11. package/docs/Reference/Validation-and-Serialization.md +15 -1
  12. package/fastify.d.ts +1 -0
  13. package/fastify.js +35 -1
  14. package/lib/config-validator.js +102 -73
  15. package/lib/content-type-parser.js +14 -2
  16. package/lib/context.js +2 -1
  17. package/lib/decorate.js +4 -3
  18. package/lib/error-handler.js +21 -4
  19. package/lib/error-status.js +4 -2
  20. package/lib/errors.js +15 -9
  21. package/lib/reply.js +12 -11
  22. package/lib/request.js +3 -2
  23. package/lib/route.js +34 -5
  24. package/lib/schemas.js +44 -32
  25. package/package.json +8 -3
  26. package/test/close-disposable.test.js +154 -0
  27. package/test/constrained-routes.test.js +4 -2
  28. package/test/content-parser.test.js +39 -0
  29. package/test/decorator.test.js +29 -0
  30. package/test/diagnostics-channel/error-status.test.js +35 -2
  31. package/test/handler-timeout.test.js +224 -0
  32. package/test/internals/all.test.js +5 -0
  33. package/test/internals/errors.test.js +18 -9
  34. package/test/internals/initial-config.test.js +2 -0
  35. package/test/internals/server.test.js +6 -4
  36. package/test/reply-error.test.js +82 -0
  37. package/test/reply-trailers.test.js +159 -1
  38. package/test/router-compile.test.js +241 -0
  39. package/test/schema-serialization.test.js +120 -7
  40. package/test/types/fastify.tst.ts +4 -0
  41. package/test/types/instance.tst.ts +1 -1
  42. package/test/types/route.tst.ts +16 -0
  43. package/test/types/using.tst.ts +20 -0
  44. package/test/validation-error-handling.test.js +21 -1
  45. package/types/instance.d.ts +5 -1
  46. package/types/route.d.ts +1 -0
package/README.md CHANGED
@@ -319,6 +319,7 @@ listed in alphabetical order.
319
319
  <https://x.com/manueomm>, <https://www.npmjs.com/~eomm>
320
320
  * [__James Sumners__](https://github.com/jsumners),
321
321
  <https://x.com/jsumners79>, <https://www.npmjs.com/~jsumners>
322
+ * [__Antonio Tripodi__](https://github.com/Tony133), <https://www.npmjs.com/~tony133>
322
323
 
323
324
  ### Fastify Plugins team
324
325
  * [__Harry Brundage__](https://github.com/airhorns/),
@@ -40,6 +40,7 @@ const defaultInitOptions = {
40
40
  routerOptions: {
41
41
  allowUnsafeRegex: false,
42
42
  caseSensitive: true,
43
+ compile: false,
43
44
  ignoreTrailingSlash: false,
44
45
  ignoreDuplicateSlashes: false,
45
46
  maxParamLength: 100,
@@ -102,6 +103,7 @@ const schema = {
102
103
  properties: {
103
104
  allowUnsafeRegex: { type: 'boolean', default: defaultInitOptions.routerOptions.allowUnsafeRegex },
104
105
  caseSensitive: { type: 'boolean', default: defaultInitOptions.routerOptions.caseSensitive },
106
+ compile: { type: 'boolean', default: defaultInitOptions.routerOptions.compile },
105
107
  constraints: {
106
108
  type: 'object',
107
109
  additionalProperties: {
@@ -190,6 +190,10 @@ section.
190
190
  - [`@gquittet/graceful-server`](https://github.com/gquittet/graceful-server)
191
191
  Tiny (~5k), Fast, KISS, and dependency-free Node.js library to make your
192
192
  Fastify API graceful.
193
+ - [`@guardcore/fastify`](https://github.com/Guard-Core/guard-core-ts/tree/master/packages/fastify)
194
+ Application-layer security plugin: request attack detection, IP and country
195
+ controls, bans, behavior rules, honeypots, security headers and per-route
196
+ decorators.
193
197
  - [`@h4ad/serverless-adapter`](https://github.com/H4ad/serverless-adapter)
194
198
  Run REST APIs and other web applications using your existing Node.js
195
199
  application framework (Express, Koa, Hapi and Fastify), on top of AWS Lambda,
@@ -405,7 +405,7 @@ fastify.get('/example/:file(^\\d+).png', function (request, reply) { })
405
405
  console.log(fastify.hasRoute({
406
406
  method: 'GET',
407
407
  url: '/example/12345.png'
408
- )); // true
408
+ })); // true
409
409
  ```
410
410
 
411
411
  ```js
@@ -416,7 +416,7 @@ fastify.get('/example/:file(^\\d+).png', function (request, reply) { })
416
416
  console.log(fastify.hasRoute({
417
417
  method: 'GET',
418
418
  url: '/example/:file(^\\d+).png'
419
- )); // true
419
+ })); // true
420
420
  ```
421
421
 
422
422
  ### Removal of some non-standard HTTP methods
@@ -127,8 +127,6 @@ on Fastify to find out how to integrate.
127
127
  [Genezio](https://genezio.com/) is a platform designed to simplify the deployment
128
128
  of serverless applications to the cloud.
129
129
 
130
- [Genezio has a dedicated guide for deploying a Fastify application.](https://deployapps.dev/docs/frameworks/fastify/)
131
-
132
130
  ## Google Cloud Functions
133
131
 
134
132
  ### Creation of Fastify instance
@@ -277,20 +275,21 @@ fastify.addHook('onResponse', async (request, reply) => {
277
275
  `request.routeOptions.url` is the route pattern, so `/users/123` and
278
276
  `/users/456` are grouped under `/users/:id`. With Fastify's logger enabled, the
279
277
  fields are emitted as structured log fields, as described in the [Cloud
280
- Logging structured logging documentation](https://cloud.google.com/logging/docs/structured-logging)
281
- and the [Cloud Functions logging guide](https://cloud.google.com/functions/docs/monitoring/logging).
278
+ Logging structured logging documentation](https://docs.cloud.google.com/logging/docs/structured-logging)
279
+ and the [Cloud Run logging guide](https://docs.cloud.google.com/run/docs/logging).
282
280
  In Cloud Logging, create a [log-based
283
- metric](https://cloud.google.com/logging/docs/logs-based-metrics) or filter on
284
- `jsonPayload.route` to get per-route request counts, latency, and error rates.
281
+ metric](https://docs.cloud.google.com/logging/docs/logs-based-metrics)
282
+ or filter on `jsonPayload.route` to get per-route request counts, latency, and
283
+ error rates.
285
284
 
286
285
  The same hook works when deploying through Firebase Functions with `onRequest`.
287
286
 
288
287
  ### References
289
288
  - [Google Cloud Functions - Node.js Quickstart
290
- ](https://cloud.google.com/run/docs/quickstarts/functions/deploy-functions-gcloud)
291
- - [Cloud Logging - Structured Logging](https://cloud.google.com/logging/docs/structured-logging)
292
- - [Cloud Logging - Log-based Metrics](https://cloud.google.com/logging/docs/logs-based-metrics)
293
- - [Cloud Functions - Logging](https://cloud.google.com/functions/docs/monitoring/logging)
289
+ ](https://docs.cloud.google.com/run/docs/quickstarts/functions/deploy-functions-gcloud)
290
+ - [Cloud Logging - Structured Logging](https://docs.cloud.google.com/logging/docs/structured-logging)
291
+ - [Cloud Logging - Log-based Metrics](https://docs.cloud.google.com/logging/docs/logs-based-metrics)
292
+ - [Cloud Run - Logging](https://docs.cloud.google.com/run/docs/logging)
294
293
 
295
294
  ## Google Firebase Functions
296
295
 
@@ -384,6 +383,30 @@ Final step is to export the Fastify app instance to Firebase's own
384
383
  exports.app = onRequest(fastifyApp)
385
384
  ```
386
385
 
386
+ ### A note on per-route functions and observability
387
+
388
+ When a Fastify instance is wrapped with a single `onRequest()` handler, all
389
+ routes are handled through a single Cloud Function entry point (e.g. `app`).
390
+ Fastify still performs internal routing as usual, but from the provider's
391
+ perspective there is only one deployed function. As a result, logs, metrics,
392
+ and analytics in the Cloud Console are grouped under that single function
393
+ rather than being split per route (e.g. `app-api-v1-user`).
394
+
395
+ This behavior is expected because Fastify acts as a single HTTP server with
396
+ internal routing, while Cloud Functions exposes only function-level
397
+ observability. This is different from architectures where each route is
398
+ deployed as an individual Cloud Function.
399
+
400
+ If per-function grouping is required, you must export multiple `onRequest()`
401
+ handlers instead of routing everything through a single Fastify instance.
402
+
403
+ Alternatively, you can keep a single Fastify application and deploy it to
404
+ Cloud Run, where it runs as a long-lived server. In that setup, Fastify's
405
+ built-in logging (request method, URL, and response timing) provides per-route
406
+ observability. On Cloud Functions, similar request-level logs are still
407
+ available, but they are grouped under the single function entry in the
408
+ provider's console.
409
+
387
410
  ### Local test
388
411
 
389
412
  Install the Firebase tools functions so you can use the CLI:
@@ -134,6 +134,19 @@ developer is responsible for ensuring errors are handled properly.
134
134
  Most errors result from unexpected input data, so it is recommended to
135
135
  [validate input data against a JSON schema](./Validation-and-Serialization.md).
136
136
 
137
+ Errors that Fastify creates when it rejects an invalid request, such as a
138
+ failed schema validation or a malformed body, do not have a stack trace, as it
139
+ would only point at Fastify internals. These are
140
+ [`FST_ERR_VALIDATION`](#fst_err_validation),
141
+ [`FST_ERR_CTP_BODY_TOO_LARGE`](#fst_err_ctp_body_too_large),
142
+ [`FST_ERR_CTP_INVALID_MEDIA_TYPE`](#fst_err_ctp_invalid_media_type),
143
+ [`FST_ERR_CTP_INVALID_CONTENT_LENGTH`](#fst_err_ctp_invalid_content_length),
144
+ [`FST_ERR_CTP_EMPTY_JSON_BODY`](#fst_err_ctp_empty_json_body),
145
+ [`FST_ERR_CTP_INVALID_JSON_BODY`](#fst_err_ctp_invalid_json_body),
146
+ [`FST_ERR_BAD_URL`](#fst_err_bad_url),
147
+ [`FST_ERR_MAX_PARAM_LENGTH`](#fst_err_max_param_length)
148
+ and [`FST_ERR_ROUTE_MISSING_CONTENT_TYPE`](#fst_err_route_missing_content_type).
149
+
137
150
  #### Catching Uncaught Errors In Fastify
138
151
  Fastify tries to catch as many uncaught errors as possible without hindering
139
152
  performance. This includes:
@@ -476,6 +476,12 @@ arrive.
476
476
 
477
477
  The hook function takes the Fastify instance as a first argument,
478
478
  and a `done` callback for synchronous hook functions.
479
+
480
+ Alternatively, an object implementing `Symbol.asyncDispose` or
481
+ `Symbol.dispose` can be registered as an `onClose` hook. Fastify invokes
482
+ the appropriate disposal method when closing the server, allowing resources
483
+ to be cleaned up using JavaScript's Explicit Resource Management protocol.
484
+
479
485
  ```js
480
486
  // callback style
481
487
  fastify.addHook('onClose', (instance, done) => {
@@ -488,6 +494,13 @@ fastify.addHook('onClose', async (instance) => {
488
494
  // Some async code
489
495
  await closeDatabaseConnections()
490
496
  })
497
+
498
+ // or disposable object style (Explicit Resource Management)
499
+ fastify.addHook('onClose', {
500
+ async [Symbol.asyncDispose] () {
501
+ await closeDatabaseConnections()
502
+ }
503
+ })
491
504
  ```
492
505
 
493
506
  #### Execution order