fastify 4.26.1 → 4.27.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.taprc +1 -0
- package/README.md +4 -0
- package/SPONSORS.md +1 -1
- package/docs/Guides/Database.md +4 -2
- package/docs/Guides/Delay-Accepting-Requests.md +1 -1
- package/docs/Guides/Detecting-When-Clients-Abort.md +6 -4
- package/docs/Guides/Ecosystem.md +23 -11
- package/docs/Guides/Getting-Started.md +2 -0
- package/docs/Guides/Plugins-Guide.md +1 -1
- package/docs/Guides/Prototype-Poisoning.md +1 -1
- package/docs/Guides/Serverless.md +111 -1
- package/docs/Reference/Errors.md +4 -3
- package/docs/Reference/Hooks.md +5 -3
- package/docs/Reference/LTS.md +32 -20
- package/docs/Reference/Logging.md +1 -1
- package/docs/Reference/Reply.md +3 -3
- package/docs/Reference/Request.md +2 -2
- package/docs/Reference/Routes.md +3 -2
- package/docs/Reference/Server.md +11 -5
- package/docs/Reference/Type-Providers.md +10 -4
- package/docs/Reference/TypeScript.md +26 -25
- package/docs/Reference/Validation-and-Serialization.md +1 -1
- package/fastify.d.ts +3 -3
- package/fastify.js +9 -5
- package/lib/contentTypeParser.js +1 -1
- package/lib/error-handler.js +34 -21
- package/lib/errors.js +6 -0
- package/lib/fourOhFour.js +5 -3
- package/lib/handleRequest.js +2 -1
- package/lib/httpMethods.js +3 -1
- package/lib/wrapThenable.js +2 -0
- package/package.json +3 -3
- package/test/404s.test.js +2 -2
- package/test/500s.test.js +32 -0
- package/test/close.test.js +1 -1
- package/test/encapsulated-error-handler.test.js +1 -1
- package/test/esm/errorCodes.test.mjs +10 -0
- package/test/esm/named-exports.mjs +1 -0
- package/test/esm/other.mjs +1 -0
- package/test/esm/plugin.mjs +1 -0
- package/test/hooks.test.js +2 -2
- package/test/internals/errors.test.js +17 -7
- package/test/internals/handleRequest.test.js +2 -2
- package/test/lock.test.js +43 -10
- package/test/logger/instantiation.test.js +1 -1
- package/test/logger/logging.test.js +37 -16
- package/test/logger/options.test.js +1 -1
- package/test/mkcalendar.test.js +165 -0
- package/test/propfind.test.js +40 -8
- package/test/proppatch.test.js +48 -18
- package/test/report.test.js +161 -0
- package/test/schema-examples.test.js +1 -1
- package/test/schema-feature.test.js +6 -6
- package/test/schema-serialization.test.js +2 -2
- package/test/schema-special-usage.test.js +1 -1
- package/test/set-error-handler.test.js +13 -0
- package/test/skip-reply-send.test.js +1 -1
- package/test/stream.5.test.js +2 -2
- package/test/types/fastify.test-d.ts +1 -1
- package/test/types/hooks.test-d.ts +4 -1
- package/test/types/instance.test-d.ts +1 -1
- package/test/types/reply.test-d.ts +2 -2
- package/test/types/request.test-d.ts +2 -2
- package/test/types/type-provider.test-d.ts +7 -3
- package/types/reply.d.ts +6 -4
- package/types/request.d.ts +2 -2
- package/types/type-provider.d.ts +1 -1
- package/types/utils.d.ts +1 -1
- /package/test/esm/{esm.mjs → esm.test.mjs} +0 -0
package/.taprc
CHANGED
package/README.md
CHANGED
|
@@ -125,6 +125,7 @@ yarn add fastify
|
|
|
125
125
|
|
|
126
126
|
// ESM
|
|
127
127
|
import Fastify from 'fastify'
|
|
128
|
+
|
|
128
129
|
const fastify = Fastify({
|
|
129
130
|
logger: true
|
|
130
131
|
})
|
|
@@ -150,6 +151,7 @@ with async-await:
|
|
|
150
151
|
```js
|
|
151
152
|
// ESM
|
|
152
153
|
import Fastify from 'fastify'
|
|
154
|
+
|
|
153
155
|
const fastify = Fastify({
|
|
154
156
|
logger: true
|
|
155
157
|
})
|
|
@@ -323,6 +325,8 @@ listed in alphabetical order.
|
|
|
323
325
|
<https://twitter.com/jsumners79>, <https://www.npmjs.com/~jsumners>
|
|
324
326
|
* [__Aras Abbasi__](https://github.com/uzlopak),
|
|
325
327
|
<https://www.npmjs.com/~uzlopak>
|
|
328
|
+
* [__Gürgün Dayıoğlu__](https://github.com/gurgunday),
|
|
329
|
+
<https://www.npmjs.com/~gurgunday>
|
|
326
330
|
|
|
327
331
|
### Fastify Plugins team
|
|
328
332
|
* [__Matteo Collina__](https://github.com/mcollina),
|
package/SPONSORS.md
CHANGED
package/docs/Guides/Database.md
CHANGED
|
@@ -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
|
-
|
|
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
|
|
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
|
|
|
@@ -287,6 +287,8 @@ async function migrate() {
|
|
|
287
287
|
});
|
|
288
288
|
|
|
289
289
|
try {
|
|
290
|
+
await client.connect();
|
|
291
|
+
|
|
290
292
|
const postgrator = new Postgrator({
|
|
291
293
|
migrationPattern: path.join(__dirname, '/migrations/*'),
|
|
292
294
|
driver: 'pg',
|
|
@@ -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
|
|
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
|
|
@@ -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
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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
|
|
package/docs/Guides/Ecosystem.md
CHANGED
|
@@ -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.
|
|
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
|
|
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.
|
|
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
|
-
|
|
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
|
|
@@ -529,6 +537,8 @@ middlewares into Fastify plugins
|
|
|
529
537
|
across every part of your server.
|
|
530
538
|
- [`fastify-osm`](https://github.com/gzileni/fastify-osm) Fastify
|
|
531
539
|
OSM plugin to run overpass queries by OpenStreetMap.
|
|
540
|
+
- [`fastify-override`](https://github.com/matthyk/fastify-override)
|
|
541
|
+
Fastify plugin to override decorators, plugins and hooks for testing purposes
|
|
532
542
|
- [`fastify-peekaboo`](https://github.com/simone-sanfratello/fastify-peekaboo)
|
|
533
543
|
Fastify plugin for memoize responses by expressive settings.
|
|
534
544
|
- [`fastify-piscina`](https://github.com/piscinajs/fastify-piscina) A worker
|
|
@@ -567,7 +577,7 @@ middlewares into Fastify plugins
|
|
|
567
577
|
- [`fastify-rbac`](https://gitlab.com/m03geek/fastify-rbac) Fastify role-based
|
|
568
578
|
access control plugin.
|
|
569
579
|
- [`fastify-recaptcha`](https://github.com/qwertyforce/fastify-recaptcha)
|
|
570
|
-
Fastify plugin for
|
|
580
|
+
Fastify plugin for reCAPTCHA verification.
|
|
571
581
|
- [`fastify-redis-channels`](https://github.com/hearit-io/fastify-redis-channels)
|
|
572
582
|
A plugin for fast, reliable, and scalable channels implementation based on
|
|
573
583
|
Redis streams.
|
|
@@ -600,7 +610,7 @@ middlewares into Fastify plugins
|
|
|
600
610
|
- [`fastify-sentry`](https://github.com/alex-ppg/fastify-sentry) Fastify plugin
|
|
601
611
|
to add the Sentry SDK error handler to requests.
|
|
602
612
|
- [`fastify-sequelize`](https://github.com/lyquocnam/fastify-sequelize) Fastify
|
|
603
|
-
plugin work with Sequelize (adapter for
|
|
613
|
+
plugin work with Sequelize (adapter for Node.js -> Sqlite, Mysql, Mssql,
|
|
604
614
|
Postgres).
|
|
605
615
|
- [`fastify-server-session`](https://github.com/jsumners/fastify-server-session)
|
|
606
616
|
A session plugin with support for arbitrary backing caches via
|
|
@@ -680,10 +690,8 @@ middlewares into Fastify plugins
|
|
|
680
690
|
[uws](https://github.com/uNetworking/uWebSockets).
|
|
681
691
|
- [`fastify-xml-body-parser`](https://github.com/NaturalIntelligence/fastify-xml-body-parser)
|
|
682
692
|
Parse XML payload / request body into JS / JSON object.
|
|
683
|
-
- [`fastify-xray`](https://github.com/jeromemacias/fastify-xray) Fastify plugin
|
|
684
|
-
for AWS XRay recording.
|
|
685
693
|
- [`http-wizard`](https://github.com/flodlc/http-wizard)
|
|
686
|
-
Exports a typescript
|
|
694
|
+
Exports a typescript API client for your Fastify API and ensures fullstack type
|
|
687
695
|
safety for your project.
|
|
688
696
|
- [`i18next-http-middleware`](https://github.com/i18next/i18next-http-middleware#fastify-usage)
|
|
689
697
|
An [i18next](https://www.i18next.com) based i18n (internationalization)
|
|
@@ -720,3 +728,7 @@ middlewares into Fastify plugins
|
|
|
720
728
|
Fastify APIs using decorators and convert Typescript interface to JSON Schema.
|
|
721
729
|
- [`simple-tjscli`](https://github.com/imjuni/simple-tjscli) CLI tool to
|
|
722
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
|
})
|
|
@@ -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
|
-
|
|
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
|
|
@@ -26,6 +26,7 @@ snippet of code.
|
|
|
26
26
|
|
|
27
27
|
- [AWS](#aws)
|
|
28
28
|
- [Google Cloud Functions](#google-cloud-functions)
|
|
29
|
+
- [Google Firebase Functions](#google-firebase-functions)
|
|
29
30
|
- [Google Cloud Run](#google-cloud-run)
|
|
30
31
|
- [Netlify Lambda](#netlify-lambda)
|
|
31
32
|
- [Platformatic Cloud](#platformatic-cloud)
|
|
@@ -203,7 +204,7 @@ const fastifyFunction = async (request, reply) => {
|
|
|
203
204
|
fastify.server.emit('request', request, reply)
|
|
204
205
|
}
|
|
205
206
|
|
|
206
|
-
|
|
207
|
+
exports.fastifyFunction = fastifyFunction;
|
|
207
208
|
```
|
|
208
209
|
|
|
209
210
|
### Local test
|
|
@@ -260,6 +261,115 @@ curl -X POST https://$GOOGLE_REGION-$GOOGLE_PROJECT.cloudfunctions.net/me \
|
|
|
260
261
|
- [Google Cloud Functions - Node.js Quickstart
|
|
261
262
|
](https://cloud.google.com/functions/docs/quickstart-nodejs)
|
|
262
263
|
|
|
264
|
+
## Google Firebase Functions
|
|
265
|
+
|
|
266
|
+
Follow this guide if you want to use Fastify as the HTTP framework for
|
|
267
|
+
Firebase Functions instead of the vanilla JavaScript router provided with
|
|
268
|
+
`onRequest(async (req, res) => {}`.
|
|
269
|
+
|
|
270
|
+
### The onRequest() handler
|
|
271
|
+
|
|
272
|
+
We use the `onRequest` function to wrap our Fastify application instance.
|
|
273
|
+
|
|
274
|
+
As such, we'll begin with importing it to the code:
|
|
275
|
+
|
|
276
|
+
```js
|
|
277
|
+
const { onRequest } = require("firebase-functions/v2/https")
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
### Creation of Fastify instance
|
|
281
|
+
|
|
282
|
+
Create the Fastify instance and encapsulate the returned application instance
|
|
283
|
+
in a function which will register routes, await the server's processing of
|
|
284
|
+
plugins, hooks and other settings. As follows:
|
|
285
|
+
|
|
286
|
+
```js
|
|
287
|
+
const fastify = require("fastify")({
|
|
288
|
+
logger: true,
|
|
289
|
+
})
|
|
290
|
+
|
|
291
|
+
const fastifyApp = async (request, reply) => {
|
|
292
|
+
await registerRoutes(fastify)
|
|
293
|
+
await fastify.ready()
|
|
294
|
+
fastify.server.emit("request", request, reply)
|
|
295
|
+
}
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
### Add Custom `contentTypeParser` to Fastify instance and define endpoints
|
|
299
|
+
|
|
300
|
+
Firebase Function's HTTP layer already parses the request
|
|
301
|
+
and makes a JSON payload available. It also provides access
|
|
302
|
+
to the raw body, unparsed, which is useful in order to calculate
|
|
303
|
+
request signatures to validate HTTP webhooks.
|
|
304
|
+
|
|
305
|
+
Add as follows to the `registerRoutes()` function:
|
|
306
|
+
|
|
307
|
+
```js
|
|
308
|
+
async function registerRoutes (fastify) {
|
|
309
|
+
fastify.addContentTypeParser("application/json", {}, (req, payload, done) => {
|
|
310
|
+
// useful to include the request's raw body on the `req` object that will
|
|
311
|
+
// later be available in your other routes so you can calculate the HMAC
|
|
312
|
+
// if needed
|
|
313
|
+
req.rawBody = payload.rawBody
|
|
314
|
+
|
|
315
|
+
// payload.body is already the parsed JSON so we just fire the done callback
|
|
316
|
+
// with it
|
|
317
|
+
done(null, payload.body)
|
|
318
|
+
})
|
|
319
|
+
|
|
320
|
+
// define your endpoints here...
|
|
321
|
+
fastify.post("/some-route-here", async (request, reply) => {}
|
|
322
|
+
|
|
323
|
+
fastify.get('/', async (request, reply) => {
|
|
324
|
+
reply.send({message: 'Hello World!'})
|
|
325
|
+
})
|
|
326
|
+
}
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
### Export the function using Firebase onRequest
|
|
330
|
+
|
|
331
|
+
Final step is to export the Fastify app instance to Firebase's own
|
|
332
|
+
`onRequest()` function so it can pass the request and reply objects to it:
|
|
333
|
+
|
|
334
|
+
```js
|
|
335
|
+
exports.app = onRequest(fastifyApp)
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
### Local test
|
|
339
|
+
|
|
340
|
+
Install the Firebase tools functions so you can use the CLI:
|
|
341
|
+
|
|
342
|
+
```bash
|
|
343
|
+
npm i -g firebase-tools
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
Then you can run your function locally with:
|
|
347
|
+
|
|
348
|
+
```bash
|
|
349
|
+
firebase emulators:start --only functions
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
### Deploy
|
|
353
|
+
|
|
354
|
+
Deploy your Firebase Functions with:
|
|
355
|
+
|
|
356
|
+
```bash
|
|
357
|
+
firebase deploy --only functions
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
#### Read logs
|
|
361
|
+
|
|
362
|
+
Use the Firebase tools CLI:
|
|
363
|
+
|
|
364
|
+
```bash
|
|
365
|
+
firebase functions:log
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
### References
|
|
369
|
+
- [Fastify on Firebase Functions](https://github.com/lirantal/lemon-squeezy-firebase-webhook-fastify/blob/main/package.json)
|
|
370
|
+
- [An article about HTTP webhooks on Firebase Functions and Fastify: A Practical Case Study with Lemon Squeezy](https://lirantal.com/blog/http-webhooks-firebase-functions-fastify-practical-case-study-lemon-squeezy)
|
|
371
|
+
|
|
372
|
+
|
|
263
373
|
## Google Cloud Run
|
|
264
374
|
|
|
265
375
|
Unlike AWS Lambda or Google Cloud Functions, Google Cloud Run is a serverless
|
package/docs/Reference/Errors.md
CHANGED
|
@@ -92,6 +92,7 @@
|
|
|
92
92
|
- [FST_ERR_PLUGIN_NOT_PRESENT_IN_INSTANCE](#fst_err_plugin_not_present_in_instance)
|
|
93
93
|
- [FST_ERR_VALIDATION](#fst_err_validation)
|
|
94
94
|
- [FST_ERR_LISTEN_OPTIONS_INVALID](#fst_err_listen_options_invalid)
|
|
95
|
+
- [FST_ERR_ERROR_HANDLER_NOT_FN](#fst_err_error_handler_not_fn)
|
|
95
96
|
|
|
96
97
|
### Error Handling In Node.js
|
|
97
98
|
<a id="error-handling"></a>
|
|
@@ -313,8 +314,7 @@ Below is a table with all the error codes that Fastify uses.
|
|
|
313
314
|
| <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) |
|
|
314
315
|
| <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) |
|
|
315
316
|
| <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) |
|
|
316
|
-
| <a id="fst_err_rep_response_body_consumed">FST_ERR_REP_RESPONSE_BODY_CONSUMED</a> | Using `Response` as reply payload
|
|
317
|
-
but the body is being consumed. | Make sure you don't consume the `Response.body` | [#5286](https://github.com/fastify/fastify/pull/5286) |
|
|
317
|
+
| <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) |
|
|
318
318
|
| <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) |
|
|
319
319
|
| <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) |
|
|
320
320
|
| <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) |
|
|
@@ -337,7 +337,7 @@ but the body is being consumed. | Make sure you don't consume the `Response.body
|
|
|
337
337
|
| <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) |
|
|
338
338
|
| <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) |
|
|
339
339
|
| <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) |
|
|
340
|
-
| <a id="fst_err_bad_url">FST_ERR_BAD_URL</a> | The router received an invalid
|
|
340
|
+
| <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) |
|
|
341
341
|
| <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) |
|
|
342
342
|
| <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) |
|
|
343
343
|
| <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) |
|
|
@@ -361,4 +361,5 @@ but the body is being consumed. | Make sure you don't consume the `Response.body
|
|
|
361
361
|
| <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) |
|
|
362
362
|
| <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) |
|
|
363
363
|
| <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) |
|
|
364
|
+
| <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) |
|
|
364
365
|
|
package/docs/Reference/Hooks.md
CHANGED
|
@@ -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
|
|
193
|
-
|
|
194
|
-
|
|
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
|
package/docs/Reference/LTS.md
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
<h1 align="center">Fastify</h1>
|
|
2
2
|
|
|
3
3
|
## Long Term Support
|
|
4
|
-
|
|
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
|
-
|
|
47
|
-
|
|
48
|
-
|
|
|
49
|
-
|
|
|
50
|
-
|
|
|
51
|
-
|
|
|
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
|
-
|
|
64
|
+
`<a id="supported-os"></a>`
|
|
65
|
+
|
|
66
|
+
Fastify uses GitHub Actions for CI testing, please refer to [GitHub'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
|
|
63
|
-
|
|
64
|
-
| Linux | `ubuntu-latest`
|
|
65
|
-
| Linux | `ubuntu-latest`
|
|
66
|
-
| Windows | `windows-latest`
|
|
67
|
-
| MacOS | `macos-latest`
|
|
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/
|
|
@@ -106,7 +106,7 @@ is generated. See Fastify Factory
|
|
|
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
|
|
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
|
package/docs/Reference/Reply.md
CHANGED
|
@@ -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
|
|
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
|
-
|
|
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
|
|
845
|
+
default behavior will be applied.
|
|
846
846
|
|
|
847
847
|
```js
|
|
848
848
|
fastify.get('/', {
|
|
@@ -107,7 +107,7 @@ fastify.post('/:params', options, function (request, reply) {
|
|
|
107
107
|
console.log(request.hostname)
|
|
108
108
|
console.log(request.protocol)
|
|
109
109
|
console.log(request.url)
|
|
110
|
-
console.log(request.
|
|
110
|
+
console.log(request.routeOptions.method)
|
|
111
111
|
console.log(request.routeOptions.bodyLimit)
|
|
112
112
|
console.log(request.routeOptions.method)
|
|
113
113
|
console.log(request.routeOptions.url)
|
|
@@ -116,7 +116,7 @@ fastify.post('/:params', options, function (request, reply) {
|
|
|
116
116
|
console.log(request.routeOptions.version)
|
|
117
117
|
console.log(request.routeOptions.exposeHeadRoute)
|
|
118
118
|
console.log(request.routeOptions.prefixTrailingSlash)
|
|
119
|
-
console.log(request.
|
|
119
|
+
console.log(request.routeOptions.logLevel)
|
|
120
120
|
request.log.info('some info')
|
|
121
121
|
})
|
|
122
122
|
```
|
package/docs/Reference/Routes.md
CHANGED
|
@@ -34,7 +34,8 @@ fastify.route(options)
|
|
|
34
34
|
|
|
35
35
|
* `method`: currently it supports `'DELETE'`, `'GET'`, `'HEAD'`, `'PATCH'`,
|
|
36
36
|
`'POST'`, `'PUT'`, `'OPTIONS'`, `'SEARCH'`, `'TRACE'`, `'PROPFIND'`,
|
|
37
|
-
`'PROPPATCH'`, `'MKCOL'`, `'COPY'`, `'MOVE'`, `'LOCK'
|
|
37
|
+
`'PROPPATCH'`, `'MKCOL'`, `'COPY'`, `'MOVE'`, `'LOCK'`, `'UNLOCK'`,
|
|
38
|
+
`'REPORT'` and `'MKCALENDAR'`.
|
|
38
39
|
It could also be an array of methods.
|
|
39
40
|
* `url`: the path of the URL to match this route (alias: `path`).
|
|
40
41
|
* `schema`: an object containing the schemas for the request and response. They
|
|
@@ -42,7 +43,7 @@ fastify.route(options)
|
|
|
42
43
|
[here](./Validation-and-Serialization.md) for more info.
|
|
43
44
|
|
|
44
45
|
* `body`: validates the body of the request if it is a POST, PUT, PATCH,
|
|
45
|
-
TRACE, or
|
|
46
|
+
TRACE, SEARCH, PROPFIND, PROPPATCH or LOCK method.
|
|
46
47
|
* `querystring` or `query`: validates the querystring. This can be a complete
|
|
47
48
|
JSON Schema object, with the property `type` of `object` and `properties`
|
|
48
49
|
object of parameters, or simply the values of what would be contained in the
|
package/docs/Reference/Server.md
CHANGED
|
@@ -385,9 +385,15 @@ been sent. By setting this option to `true`, these log messages will be
|
|
|
385
385
|
disabled. This allows for more flexible request start and end logging by
|
|
386
386
|
attaching custom `onRequest` and `onResponse` hooks.
|
|
387
387
|
|
|
388
|
-
|
|
389
|
-
default `onResponse` hook on reply callback errors
|
|
390
|
-
|
|
388
|
+
The other log entries that will be disabled are:
|
|
389
|
+
- an error log written by the default `onResponse` hook on reply callback errors
|
|
390
|
+
- the error and info logs written by the `defaultErrorHandler`
|
|
391
|
+
on error management
|
|
392
|
+
- the info log written by the `fourOhFour` handler when a
|
|
393
|
+
non existent route is requested
|
|
394
|
+
|
|
395
|
+
Other log messages emitted by Fastify will stay enabled,
|
|
396
|
+
like deprecation warnings and messages
|
|
391
397
|
emitted when requests are received while the server is closing.
|
|
392
398
|
|
|
393
399
|
```js
|
|
@@ -572,7 +578,7 @@ const fastify = require('fastify')({
|
|
|
572
578
|
comma separated values (e.g. `'127.0.0.1,192.168.1.1/24'`).
|
|
573
579
|
+ `Array<string>`: Trust only given IP/CIDR list (e.g. `['127.0.0.1']`).
|
|
574
580
|
+ `number`: Trust the nth hop from the front-facing proxy server as the client.
|
|
575
|
-
+ `Function`: Custom trust function that takes `address` as first
|
|
581
|
+
+ `Function`: Custom trust function that takes `address` as first argument
|
|
576
582
|
```js
|
|
577
583
|
function myTrustFn(address, hop) {
|
|
578
584
|
return address === '1.2.3.4' || hop === 1
|
|
@@ -871,7 +877,7 @@ Fastify uses [find-my-way](https://github.com/delvedor/find-my-way) which suppor
|
|
|
871
877
|
separating the path and query string with a `;` character (code 59), e.g. `/dev;foo=bar`.
|
|
872
878
|
This decision originated from [delvedor/find-my-way#76]
|
|
873
879
|
(https://github.com/delvedor/find-my-way/issues/76). Thus, this option will support
|
|
874
|
-
backwards
|
|
880
|
+
backwards compatibility for the need to split on `;`. To disable support for splitting
|
|
875
881
|
on `;` set `useSemicolonDelimiter` to `false`.
|
|
876
882
|
|
|
877
883
|
```js
|