fastify 6.0.0-alpha.4 → 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.
- package/README.md +1 -0
- package/SECURITY.md +6 -0
- package/build/build-validation.js +2 -0
- package/docs/Guides/Ecosystem.md +12 -53
- package/docs/Guides/Migration-Guide-V5.md +2 -2
- package/docs/Guides/Serverless.md +33 -10
- package/docs/Reference/Errors.md +13 -2
- package/docs/Reference/Hooks.md +13 -0
- package/docs/Reference/Reply.md +787 -681
- package/docs/Reference/Request.md +308 -104
- package/docs/Reference/Routes.md +6 -2
- package/docs/Reference/Server.md +41 -4
- package/docs/Reference/Validation-and-Serialization.md +48 -1
- package/docs/Reference/Warnings.md +2 -0
- package/fastify.d.ts +1 -0
- package/fastify.js +35 -1
- package/lib/config-validator.js +102 -73
- package/lib/content-type-parser.js +35 -14
- package/lib/content-type.js +4 -4
- package/lib/context.js +2 -1
- package/lib/decorate.js +4 -3
- package/lib/error-handler.js +21 -4
- package/lib/error-status.js +4 -2
- package/lib/errors.js +15 -14
- package/lib/handle-request.js +7 -20
- package/lib/reply.js +18 -13
- package/lib/request.js +12 -7
- package/lib/route.js +40 -7
- package/lib/schemas.js +44 -32
- package/lib/validation.js +67 -10
- package/lib/warnings.js +10 -1
- package/package.json +8 -3
- package/test/child-logger-factory.test.js +25 -0
- package/test/close-disposable.test.js +154 -0
- package/test/constrained-routes.test.js +4 -2
- package/test/content-parser.test.js +99 -0
- package/test/decorator.test.js +29 -0
- package/test/diagnostics-channel/error-status.test.js +35 -2
- package/test/handler-timeout.test.js +224 -0
- package/test/internals/all.test.js +5 -0
- package/test/internals/errors.test.js +19 -20
- package/test/internals/initial-config.test.js +2 -0
- package/test/internals/reply-serialize.test.js +20 -0
- package/test/internals/reply.test.js +30 -1
- package/test/internals/request.test.js +50 -0
- package/test/internals/server.test.js +6 -4
- package/test/reply-error.test.js +82 -0
- package/test/reply-media-type.test.js +57 -0
- package/test/reply-trailers.test.js +159 -1
- package/test/rfc-10008.test.js +6 -9
- package/test/router-compile.test.js +241 -0
- package/test/schema-feature.test.js +1 -1
- package/test/schema-serialization.test.js +120 -7
- package/test/schema-validation.test.js +251 -1
- package/test/types/errors.tst.ts +0 -1
- package/test/types/fastify.tst.ts +9 -0
- package/test/types/instance.tst.ts +1 -1
- package/test/types/route.tst.ts +37 -0
- package/test/types/using.tst.ts +20 -0
- package/test/validation-error-handling.test.js +21 -1
- package/types/errors.d.ts +0 -1
- package/types/instance.d.ts +9 -5
- package/types/route.d.ts +4 -2
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/),
|
package/SECURITY.md
CHANGED
|
@@ -30,6 +30,12 @@ The following are **not** considered vulnerabilities in Fastify:
|
|
|
30
30
|
|
|
31
31
|
- **Application code vulnerabilities**: XSS, SQL injection, or other flaws in
|
|
32
32
|
user-written route handlers, hooks, or plugins
|
|
33
|
+
- **Unvalidated template selection**: Passing untrusted input directly as a
|
|
34
|
+
template name or path (for example, `reply.view(request.query.page)`) without
|
|
35
|
+
validating or allowlisting it is an application-level vulnerability. A template
|
|
36
|
+
root used to resolve filenames is not a security sandbox unless the API
|
|
37
|
+
explicitly guarantees path containment. Bypasses of documented containment
|
|
38
|
+
or built-in path validation remain in scope.
|
|
33
39
|
- **Malicious application code**: Issues caused by intentionally malicious
|
|
34
40
|
plugins or handlers (application code is trusted)
|
|
35
41
|
- **Validation schema issues**: Weak or incorrect schemas provided by developers
|
|
@@ -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: {
|
package/docs/Guides/Ecosystem.md
CHANGED
|
@@ -172,8 +172,6 @@ section.
|
|
|
172
172
|
A plugin to help with caching API responses using AWS DynamoDB.
|
|
173
173
|
- [`@clerk/fastify`](https://github.com/clerk/javascript/tree/main/packages/fastify)
|
|
174
174
|
Add authentication and user management to your Fastify application with Clerk.
|
|
175
|
-
- [`@coobaha/typed-fastify`](https://github.com/Coobaha/typed-fastify) Strongly
|
|
176
|
-
typed routes with a runtime validation using JSON schema generated from types.
|
|
177
175
|
- [`@dnlup/fastify-doc`](https://github.com/dnlup/fastify-doc) A plugin for
|
|
178
176
|
sampling process metrics.
|
|
179
177
|
- [`@dnlup/fastify-traps`](https://github.com/dnlup/fastify-traps) A plugin to
|
|
@@ -192,6 +190,10 @@ section.
|
|
|
192
190
|
- [`@gquittet/graceful-server`](https://github.com/gquittet/graceful-server)
|
|
193
191
|
Tiny (~5k), Fast, KISS, and dependency-free Node.js library to make your
|
|
194
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.
|
|
195
197
|
- [`@h4ad/serverless-adapter`](https://github.com/H4ad/serverless-adapter)
|
|
196
198
|
Run REST APIs and other web applications using your existing Node.js
|
|
197
199
|
application framework (Express, Koa, Hapi and Fastify), on top of AWS Lambda,
|
|
@@ -211,6 +213,9 @@ section.
|
|
|
211
213
|
- [`@matths/fastify-svelte-view`](https://github.com/matths/fastify-svelte-view)
|
|
212
214
|
A Fastify plugin for rendering Svelte components with support for SSR
|
|
213
215
|
(Server-Side Rendering), CSR (Client-Side Rendering), and SSR with hydration.
|
|
216
|
+
- [`@melroy89/fastify-mariadb`](https://github.com/melroy89/fastify-mariadb)
|
|
217
|
+
Fastify MariaDB connection plugin that shares a connection pool across the
|
|
218
|
+
server.
|
|
214
219
|
- [`@mgcrea/fastify-graceful-exit`](https://github.com/mgcrea/fastify-graceful-exit)
|
|
215
220
|
A plugin to close the server gracefully
|
|
216
221
|
- [`@mgcrea/fastify-request-logger`](https://github.com/mgcrea/fastify-request-logger)
|
|
@@ -240,6 +245,9 @@ section.
|
|
|
240
245
|
method with a body).
|
|
241
246
|
- [`@trubavuong/fastify-seaweedfs`](https://github.com/trubavuong/fastify-seaweedfs)
|
|
242
247
|
SeaweedFS for Fastify
|
|
248
|
+
- [`@validex/fastify`](https://github.com/chiptoma/validex/tree/main/packages/fastify)
|
|
249
|
+
Zod 4 validation plugin with 25 typed rules, structured error codes, and
|
|
250
|
+
`request.validate()` decorator.
|
|
243
251
|
- [`@yeliex/fastify-problem-details`](https://github.com/yeliex/fastify-problem-details)
|
|
244
252
|
RFC 9457 Problem Details implementation for Fastify, with typed HTTP errors.
|
|
245
253
|
- [`@zrosenbauer/fastify-prisma`](https://github.com/zrosenbauer/fastify-prisma)
|
|
@@ -249,8 +257,6 @@ section.
|
|
|
249
257
|
logging and monitoring tool.
|
|
250
258
|
- [`arecibo`](https://github.com/ducktors/arecibo) Fastify ping responder for
|
|
251
259
|
Kubernetes Liveness and Readiness Probes.
|
|
252
|
-
- [`aws-xray-sdk-fastify`](https://github.com/aws/aws-xray-sdk-node/tree/master/sdk_contrib/fastify)
|
|
253
|
-
A Fastify plugin to log requests and subsegments through AWSXray.
|
|
254
260
|
- [`cls-rtracer`](https://github.com/puzpuzpuz/cls-rtracer) Fastify middleware
|
|
255
261
|
for CLS-based request ID generation. An out-of-the-box solution for adding
|
|
256
262
|
request IDs into your logs.
|
|
@@ -274,14 +280,8 @@ section.
|
|
|
274
280
|
- [`fastify-amqp-async`](https://github.com/kffl/fastify-amqp-async) Fastify
|
|
275
281
|
AMQP plugin with a Promise-based API provided by
|
|
276
282
|
[`amqplib-as-promised`](https://github.com/twawszczak/amqplib-as-promised).
|
|
277
|
-
- [`fastify-angular-universal`](https://github.com/exequiel09/fastify-angular-universal)
|
|
278
|
-
Angular server-side rendering support using
|
|
279
|
-
[`@angular/platform-server`](https://github.com/angular/angular/tree/master/packages/platform-server)
|
|
280
|
-
for Fastify
|
|
281
283
|
- [`fastify-api-key`](https://github.com/arkerone/fastify-api-key) Fastify
|
|
282
284
|
plugin to authenticate HTTP requests based on API key and signature
|
|
283
|
-
- [`fastify-appwrite`](https://github.com/maniecodes/fastify-appwrite) Fastify
|
|
284
|
-
Plugin for interacting with Appwrite server.
|
|
285
285
|
- [`fastify-arktype`](https://github.com/BlairCurrey/fastify-arktype) ArkType
|
|
286
286
|
type provider with validation and @fastify/swagger OpenAPI generation.
|
|
287
287
|
- [`fastify-asyncforge`](https://github.com/mcollina/fastify-asyncforge) Plugin
|
|
@@ -298,15 +298,9 @@ section.
|
|
|
298
298
|
Auth0 verification plugin for Fastify, internally uses
|
|
299
299
|
[fastify-jwt](https://www.npmjs.com/package/fastify-jwt) and
|
|
300
300
|
[jsonwebtoken](https://www.npmjs.com/package/jsonwebtoken).
|
|
301
|
-
- [`fastify-autoroutes`](https://github.com/GiovanniCardamone/fastify-autoroutes)
|
|
302
|
-
Plugin to scan and load routes based on filesystem path from a custom
|
|
303
|
-
directory.
|
|
304
301
|
- [`fastify-aws-sns`](https://github.com/gzileni/fastify-aws-sns) Fastify plugin
|
|
305
302
|
for AWS Simple Notification Service (AWS SNS) that coordinates and manages
|
|
306
303
|
the delivery or sending of messages to subscribing endpoints or clients.
|
|
307
|
-
- [`fastify-aws-timestream`](https://github.com/gzileni/fastify-aws-timestream)
|
|
308
|
-
Fastify plugin for managing databases, tables, and querying and creating
|
|
309
|
-
scheduled queries with AWS Timestream.
|
|
310
304
|
- [`fastify-axios`](https://github.com/davidedantonio/fastify-axios) Plugin to
|
|
311
305
|
send HTTP requests via [axios](https://github.com/axios/axios).
|
|
312
306
|
- [`fastify-babel`](https://github.com/cfware/fastify-babel) Fastify plugin for
|
|
@@ -341,9 +335,6 @@ section.
|
|
|
341
335
|
Fastify plugin for CloudFlare Turnstile.
|
|
342
336
|
- [`fastify-cloudinary`](https://github.com/Vanilla-IceCream/fastify-cloudinary)
|
|
343
337
|
Plugin to share a common Cloudinary connection across Fastify.
|
|
344
|
-
- [`fastify-cockroachdb`](https://github.com/alex-ppg/fastify-cockroachdb)
|
|
345
|
-
Fastify plugin to connect to a CockroachDB PostgreSQL instance via the
|
|
346
|
-
Sequelize ORM.
|
|
347
338
|
- [`fastify-constraints`](https://github.com/nearform/fastify-constraints)
|
|
348
339
|
Fastify plugin to add constraints to multiple routes
|
|
349
340
|
- [`fastify-couchdb`](https://github.com/nigelhanlon/fastify-couchdb) Fastify
|
|
@@ -361,10 +352,6 @@ section.
|
|
|
361
352
|
- [`fastify-disablecache`](https://github.com/Fdawgs/fastify-disablecache)
|
|
362
353
|
Fastify plugin to disable client-side caching, inspired by
|
|
363
354
|
[nocache](https://github.com/helmetjs/nocache).
|
|
364
|
-
- [`fastify-dynamodb`](https://github.com/matrus2/fastify-dynamodb) AWS DynamoDB
|
|
365
|
-
plugin for Fastify. It exposes
|
|
366
|
-
[AWS.DynamoDB.DocumentClient()](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html)
|
|
367
|
-
object.
|
|
368
355
|
- [`fastify-dynareg`](https://github.com/greguz/fastify-dynareg) Dynamic plugin
|
|
369
356
|
register for Fastify.
|
|
370
357
|
- [`fastify-envalid`](https://github.com/alemagio/fastify-envalid) Fastify
|
|
@@ -372,9 +359,6 @@ section.
|
|
|
372
359
|
project.
|
|
373
360
|
- [`fastify-error-page`](https://github.com/hemerajs/fastify-error-page) Fastify
|
|
374
361
|
plugin to print errors in structured HTML to the browser.
|
|
375
|
-
- [`fastify-esso`](https://github.com/patrickpissurno/fastify-esso) The easiest
|
|
376
|
-
authentication plugin for Fastify, with built-in support for Single sign-on
|
|
377
|
-
(and great documentation).
|
|
378
362
|
- [`fastify-event-bus`](https://github.com/Shiva127/fastify-event-bus) Event bus
|
|
379
363
|
support for Fastify. Built upon [js-event-bus](https://github.com/bcerati/js-event-bus).
|
|
380
364
|
- [`fastify-evervault`](https://github.com/Briscoooe/fastify-evervault/) Fastify
|
|
@@ -457,14 +441,10 @@ section.
|
|
|
457
441
|
plugin to serialize JSON responses into XML.
|
|
458
442
|
- [`fastify-jwt-authz`](https://github.com/Ethan-Arrowood/fastify-jwt-authz) JWT
|
|
459
443
|
user scope verifier.
|
|
460
|
-
- [`fastify-jwt-webapp`](https://github.com/charlesread/fastify-jwt-webapp) JWT
|
|
461
|
-
authentication for Fastify-based web apps.
|
|
462
444
|
- [`fastify-kafkajs`](https://github.com/kffl/fastify-kafkajs) Fastify plugin
|
|
463
445
|
that adds support for KafkaJS - a modern Apache Kafka client library.
|
|
464
446
|
- [`fastify-keycloak-adapter`](https://github.com/yubinTW/fastify-keycloak-adapter)
|
|
465
447
|
A keycloak adapter for a Fastify app.
|
|
466
|
-
- [`fastify-koa`](https://github.com/rozzilla/fastify-koa) Convert Koa
|
|
467
|
-
middlewares into Fastify plugins
|
|
468
448
|
- [`fastify-kubernetes`](https://github.com/greguz/fastify-kubernetes) Fastify
|
|
469
449
|
Kubernetes client plugin.
|
|
470
450
|
- [`fastify-kysely`](https://github.com/alenap93/fastify-kysely) Fastify
|
|
@@ -504,6 +484,8 @@ middlewares into Fastify plugins
|
|
|
504
484
|
- [`fastify-mongoose-driver`](https://github.com/alex-ppg/fastify-mongoose)
|
|
505
485
|
Fastify Mongoose plugin that connects to a MongoDB via the Mongoose plugin
|
|
506
486
|
with support for Models.
|
|
487
|
+
- [`fastify-mosquitto-dynsec`](https://github.com/LuizTakeda/fastify-mosquitto-dynsec)
|
|
488
|
+
Fastify plugin to manage Mosquitto Dynamic Security over MQTT.
|
|
507
489
|
- [`fastify-mqtt`](https://github.com/love-lena/fastify-mqtt) Plugin to share
|
|
508
490
|
[mqtt](https://www.npmjs.com/package/mqtt) client across Fastify.
|
|
509
491
|
- [`fastify-msgpack`](https://github.com/kenriortega/fastify-msgpack) Fastify
|
|
@@ -546,8 +528,6 @@ middlewares into Fastify plugins
|
|
|
546
528
|
[`oracledb`](https://github.com/oracle/node-oracledb) connection pool to a
|
|
547
529
|
Fastify server instance.
|
|
548
530
|
- [`fastify-orama`](https://github.com/mateonunez/fastify-orama)
|
|
549
|
-
- [`fastify-osm`](https://github.com/gzileni/fastify-osm) Fastify
|
|
550
|
-
OSM plugin to run overpass queries by OpenStreetMap.
|
|
551
531
|
- [`fastify-override`](https://github.com/matthyk/fastify-override)
|
|
552
532
|
Fastify plugin to override decorators, plugins and hooks for testing purposes
|
|
553
533
|
- [`fastify-param-schema-validation`](https://github.com/Player1205/fastify-param-schema-validation)
|
|
@@ -568,8 +548,6 @@ middlewares into Fastify plugins
|
|
|
568
548
|
- [`fastify-postgraphile`](https://github.com/alemagio/fastify-postgraphile)
|
|
569
549
|
Plugin to integrate [PostGraphile](https://postgraphile.org/postgraphile/4/) in
|
|
570
550
|
a Fastify project.
|
|
571
|
-
- [`fastify-postgres-dot-js`](https://github.com/kylerush/fastify-postgresjs) Fastify
|
|
572
|
-
PostgreSQL connection plugin that uses [Postgres.js](https://github.com/porsager/postgres).
|
|
573
551
|
- [`fastify-prettier`](https://github.com/hsynlms/fastify-prettier) A Fastify
|
|
574
552
|
plugin that uses [prettier](https://github.com/prettier/prettier) under the
|
|
575
553
|
hood to beautify outgoing responses and/or other things in the Fastify server.
|
|
@@ -617,8 +595,6 @@ middlewares into Fastify plugins
|
|
|
617
595
|
using name and parameters.
|
|
618
596
|
- [`fastify-rob-config`](https://github.com/jeromemacias/fastify-rob-config)
|
|
619
597
|
Fastify Rob-Config integration.
|
|
620
|
-
- [`fastify-route-group`](https://github.com/TakNePoidet/fastify-route-group)
|
|
621
|
-
Convenient grouping and inheritance of routes.
|
|
622
598
|
- [`fastify-route-preset`](https://github.com/inyourtime/fastify-route-preset)
|
|
623
599
|
A Fastify plugin that enables you to create route configurations that can be
|
|
624
600
|
applied to multiple routes.
|
|
@@ -626,9 +602,6 @@ middlewares into Fastify plugins
|
|
|
626
602
|
Ensure the existence of defined S3 buckets on the application startup.
|
|
627
603
|
- [`fastify-schema-constraint`](https://github.com/Eomm/fastify-schema-constraint)
|
|
628
604
|
Choose the JSON schema to use based on request parameters.
|
|
629
|
-
- [`fastify-schema-to-typescript`](https://github.com/thomasthiebaud/fastify-schema-to-typescript)
|
|
630
|
-
Generate typescript types based on your JSON/YAML validation schemas so they
|
|
631
|
-
are always in sync.
|
|
632
605
|
- [`fastify-sentry`](https://github.com/alex-ppg/fastify-sentry) Fastify plugin
|
|
633
606
|
to add the Sentry SDK error handler to requests.
|
|
634
607
|
- [`fastify-sequelize`](https://github.com/lyquocnam/fastify-sequelize) Fastify
|
|
@@ -639,8 +612,6 @@ middlewares into Fastify plugins
|
|
|
639
612
|
`fastify-caching`.
|
|
640
613
|
- [`fastify-ses-mailer`](https://github.com/KaranHotwani/fastify-ses-mailer) A
|
|
641
614
|
Fastify plugin for sending emails via AWS SES using AWS SDK v3.
|
|
642
|
-
- [`fastify-shared-schema`](https://github.com/Adibla/fastify-shared-schema) Plugin
|
|
643
|
-
for sharing schemas between different routes.
|
|
644
615
|
- [`fastify-slow-down`](https://github.com/nearform/fastify-slow-down) A plugin
|
|
645
616
|
to delay the response from the server.
|
|
646
617
|
- [`fastify-split-validator`](https://github.com/MetCoder95/fastify-split-validator)
|
|
@@ -665,10 +636,6 @@ middlewares into Fastify plugins
|
|
|
665
636
|
localhost-only, TLS certificate.
|
|
666
637
|
- [`fastify-totp`](https://github.com/beliven-it/fastify-totp) A plugin to handle
|
|
667
638
|
TOTP (e.g. for 2FA).
|
|
668
|
-
- [`fastify-type-provider-effect-schema`](https://github.com/daotl/fastify-type-provider-effect-schema)
|
|
669
|
-
Fastify
|
|
670
|
-
[type provider](https://fastify.dev/docs/latest/Reference/Type-Providers/)
|
|
671
|
-
for [@effect/schema](https://github.com/Effect-TS/effect).
|
|
672
639
|
- [`fastify-type-provider-zod`](https://github.com/turkerdev/fastify-type-provider-zod)
|
|
673
640
|
Fastify
|
|
674
641
|
[type provider](https://fastify.dev/docs/latest/Reference/Type-Providers/)
|
|
@@ -679,10 +646,6 @@ middlewares into Fastify plugins
|
|
|
679
646
|
request's `user-agent` header.
|
|
680
647
|
- [`fastify-uws`](https://github.com/geut/fastify-uws) A Fastify plugin to
|
|
681
648
|
use the web server [uWebSockets.js](https://github.com/uNetworking/uWebSockets.js).
|
|
682
|
-
- [`fastify-vhost`](https://github.com/patrickpissurno/fastify-vhost) Proxy
|
|
683
|
-
subdomain HTTP requests to another server (useful if you want to point
|
|
684
|
-
multiple subdomains to the same IP address, while running different servers on
|
|
685
|
-
the same machine).
|
|
686
649
|
- [`fastify-vue-plugin`](https://github.com/TheNoim/fastify-vue)
|
|
687
650
|
[Nuxt.js](https://nuxt.com) plugin for Fastify. Control the routes nuxt
|
|
688
651
|
should use.
|
|
@@ -725,10 +688,6 @@ middlewares into Fastify plugins
|
|
|
725
688
|
|
|
726
689
|
#### [Community Tools](#community-tools)
|
|
727
690
|
|
|
728
|
-
- [`fast-maker`](https://github.com/imjuni/fast-maker) route configuration
|
|
729
|
-
generator by directory structure.
|
|
730
|
-
- [`fastify-flux`](https://github.com/Jnig/fastify-flux) Tool for building
|
|
731
|
-
Fastify APIs using decorators and convert Typescript interface to JSON Schema.
|
|
732
691
|
- [`fastify-intlayer`](https://intlayer.org/doc/environment/fastify)
|
|
733
692
|
i18n solution for error handling, email template
|
|
734
693
|
- [`jeasx`](https://www.jeasx.dev)
|
|
@@ -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
|
|
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)
|
|
284
|
-
`jsonPayload.route` to get per-route request counts, latency, and
|
|
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
|
|
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:
|
package/docs/Reference/Errors.md
CHANGED
|
@@ -94,7 +94,6 @@
|
|
|
94
94
|
- [FST_ERR_ROUTE_HANDLER_TIMEOUT_OPTION_NOT_INT](#fst_err_route_handler_timeout_option_not_int)
|
|
95
95
|
- [FST_ERR_ROUTE_REWRITE_NOT_STR](#fst_err_route_rewrite_not_str)
|
|
96
96
|
- [FST_ERR_ROUTE_MISSING_CONTENT_TYPE](#fst_err_route_missing_content_type)
|
|
97
|
-
- [FST_ERR_ROUTE_MISSING_CONTENT](#fst_err_route_missing_content)
|
|
98
97
|
- [FST_ERR_REOPENED_CLOSE_SERVER](#fst_err_reopened_close_server)
|
|
99
98
|
- [FST_ERR_REOPENED_SERVER](#fst_err_reopened_server)
|
|
100
99
|
- [FST_ERR_PLUGIN_VERSION_MISMATCH](#fst_err_plugin_version_mismatch)
|
|
@@ -135,6 +134,19 @@ developer is responsible for ensuring errors are handled properly.
|
|
|
135
134
|
Most errors result from unexpected input data, so it is recommended to
|
|
136
135
|
[validate input data against a JSON schema](./Validation-and-Serialization.md).
|
|
137
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
|
+
|
|
138
150
|
#### Catching Uncaught Errors In Fastify
|
|
139
151
|
Fastify tries to catch as many uncaught errors as possible without hindering
|
|
140
152
|
performance. This includes:
|
|
@@ -447,7 +459,6 @@ Below is a table with all the error codes used by Fastify.
|
|
|
447
459
|
| <a id="fst_err_route_handler_timeout_option_not_int">FST_ERR_ROUTE_HANDLER_TIMEOUT_OPTION_NOT_INT</a> | `handlerTimeout` option must be a positive integer. | Use a positive integer for the `handlerTimeout` option. | - |
|
|
448
460
|
| <a id="fst_err_route_rewrite_not_str">FST_ERR_ROUTE_REWRITE_NOT_STR</a> | `rewriteUrl` needs to be of type `string`. | Use a string for the `rewriteUrl`. | [#4554](https://github.com/fastify/fastify/pull/4554) |
|
|
449
461
|
| <a id="fst_err_route_missing_content_type">FST_ERR_ROUTE_MISSING_CONTENT_TYPE</a> | `Content-Type` header is required for the request. | Send request with `Content-Type` header. | [#6832](https://github.com/fastify/fastify/pull/6832) |
|
|
450
|
-
| <a id="fst_err_route_missing_content">FST_ERR_ROUTE_MISSING_CONTENT</a> | Body is required for the request. | Send request with payload. | [#6832](https://github.com/fastify/fastify/pull/6832) |
|
|
451
462
|
| <a id="fst_err_reopened_close_server">FST_ERR_REOPENED_CLOSE_SERVER</a> | Fastify has already been closed and cannot be reopened. | - | [#2415](https://github.com/fastify/fastify/pull/2415) |
|
|
452
463
|
| <a id="fst_err_reopened_server">FST_ERR_REOPENED_SERVER</a> | Fastify is already started. | - | [#2415](https://github.com/fastify/fastify/pull/2415) |
|
|
453
464
|
| <a id="fst_err_plugin_version_mismatch">FST_ERR_PLUGIN_VERSION_MISMATCH</a> | Installed Fastify plugin mismatched expected version. | Use a compatible version of the plugin. | [#2549](https://github.com/fastify/fastify/pull/2549) |
|
package/docs/Reference/Hooks.md
CHANGED
|
@@ -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
|