Skip to content

Commit

Permalink
Merge branch 'main' into addHttpMethod-type-signature
Browse files Browse the repository at this point in the history
  • Loading branch information
kalvenschraut authored Nov 5, 2024
2 parents e550f50 + e44c7ba commit 9eab296
Show file tree
Hide file tree
Showing 40 changed files with 284 additions and 291 deletions.
7 changes: 2 additions & 5 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
# Set the default behavior, in case people don't have core.autocrlf set
* text=auto

# Require Unix line endings
* text eol=lf
# Set default behavior to automatically convert line endings
* text=auto eol=lf
4 changes: 2 additions & 2 deletions .github/workflows/benchmark-parser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ jobs:
**Node**: 20
**PR**: ${{ needs.benchmark.outputs.PR-BENCH-20 }}
**MAIN**: ${{ needs.benchmark.outputs.MAIN-BENCH-20 }}
---
**Node**: 22
**PR**: ${{ needs.benchmark.outputs.PR-BENCH-22 }}
**MAIN**: ${{ needs.benchmark.outputs.MAIN-BENCH-22 }}
4 changes: 2 additions & 2 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ jobs:
**Node**: 20
**PR**: ${{ needs.benchmark.outputs.PR-BENCH-20 }}
**MAIN**: ${{ needs.benchmark.outputs.MAIN-BENCH-20 }}
---
**Node**: 22
**PR**: ${{ needs.benchmark.outputs.PR-BENCH-22 }}
**MAIN**: ${{ needs.benchmark.outputs.MAIN-BENCH-22 }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/md-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
uses: actions/checkout@v4
with:
persist-credentials: false

- name: Setup Node
uses: actions/setup-node@v4
with:
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ the following tasks:
5. The person that does the onboarding must add you to the [npm
org](https://www.npmjs.com/org/fastify), so that you can help maintaining the
official plugins.
6. Optionally, the person can be added as an Open Collective member
6. Optionally, the person can be added as an Open Collective member
by the lead team.

### Offboarding Collaborators
Expand Down
2 changes: 1 addition & 1 deletion EXPENSE_POLICY.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ To claim a bounty:
- The expense will be validated by a lead maintainer and then the payment will be
processed by Open Collective

If the Open Collective budget is insufficient, the expense will be rejected.
If the Open Collective budget is insufficient, the expense will be rejected.
Unclaimed bounties are available for other issues.

[submit]: https://opencollective.com/fastify/expenses/new
Expand Down
30 changes: 15 additions & 15 deletions docs/Guides/Database.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

## Database

Fastify's ecosystem provides a handful of
plugins for connecting to various database engines.
This guide covers engines that have Fastify
Fastify's ecosystem provides a handful of
plugins for connecting to various database engines.
This guide covers engines that have Fastify
plugins maintained within the Fastify organization.

> If a plugin for your database of choice does not exist
> you can still use the database as Fastify is database agnostic.
> By following the examples of the database plugins listed in this guide,
> a plugin can be written for the missing database engine.
> If a plugin for your database of choice does not exist
> you can still use the database as Fastify is database agnostic.
> By following the examples of the database plugins listed in this guide,
> a plugin can be written for the missing database engine.
> If you would like to write your own Fastify plugin
> If you would like to write your own Fastify plugin
> please take a look at the [plugins guide](./Plugins-Guide.md)
### [MySQL](https://github.com/fastify/fastify-mysql)
Expand Down Expand Up @@ -104,8 +104,8 @@ fastify.listen({ port: 3000 }, err => {
})
```

By default `@fastify/redis` doesn't close
the client connection when Fastify server shuts down.
By default `@fastify/redis` doesn't close
the client connection when Fastify server shuts down.
To opt-in to this behavior, register the client like so:

```javascript
Expand All @@ -126,7 +126,7 @@ fastify.register(require('@fastify/mongodb'), {
// force to close the mongodb connection when app stopped
// the default value is false
forceClose: true,

url: 'mongodb://mongo/mydb'
})

Expand Down Expand Up @@ -178,8 +178,8 @@ fastify.listen({ port: 3000 }, err => {
```

### Writing plugin for a database library
We could write a plugin for a database
library too (e.g. Knex, Prisma, or TypeORM).
We could write a plugin for a database
library too (e.g. Knex, Prisma, or TypeORM).
We will use [Knex](https://knexjs.org/) in our example.

```javascript
Expand Down Expand Up @@ -281,7 +281,7 @@ async function migrate() {
const client = new pg.Client({
host: 'localhost',
port: 5432,
database: 'example',
database: 'example',
user: 'example',
password: 'example',
});
Expand Down Expand Up @@ -313,7 +313,7 @@ async function migrate() {
console.error(err)
process.exitCode = 1
}

await client.end()
}

Expand Down
56 changes: 28 additions & 28 deletions docs/Guides/Detecting-When-Clients-Abort.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@

## Introduction

Fastify provides request events to trigger at certain points in a request's
lifecycle. However, there isn't a built-in mechanism to
detect unintentional client disconnection scenarios such as when the client's
Fastify provides request events to trigger at certain points in a request's
lifecycle. However, there isn't a built-in mechanism to
detect unintentional client disconnection scenarios such as when the client's
internet connection is interrupted. This guide covers methods to detect if
and when a client intentionally aborts a request.

Keep in mind, Fastify's `clientErrorHandler` is not designed to detect when a
client aborts a request. This works in the same way as the standard Node HTTP
module, which triggers the `clientError` event when there is a bad request or
exceedingly large header data. When a client aborts a request, there is no
Keep in mind, Fastify's `clientErrorHandler` is not designed to detect when a
client aborts a request. This works in the same way as the standard Node HTTP
module, which triggers the `clientError` event when there is a bad request or
exceedingly large header data. When a client aborts a request, there is no
error on the socket and the `clientErrorHandler` will not be triggered.

## Solution

### Overview

The proposed solution is a possible way of detecting when a client
intentionally aborts a request, such as when a browser is closed or the HTTP
request is aborted from your client application. If there is an error in your
application code that results in the server crashing, you may require
The proposed solution is a possible way of detecting when a client
intentionally aborts a request, such as when a browser is closed or the HTTP
request is aborted from your client application. If there is an error in your
application code that results in the server crashing, you may require
additional logic to avoid a false abort detection.

The goal here is to detect when a client intentionally aborts a connection
so your application logic can proceed accordingly. This can be useful for
The goal here is to detect when a client intentionally aborts a connection
so your application logic can proceed accordingly. This can be useful for
logging purposes or halting business logic.

### Hands-on
Expand Down Expand Up @@ -78,10 +78,10 @@ const start = async () => {
start()
```

Our code is setting up a Fastify server which includes the following
Our code is setting up a Fastify server which includes the following
functionality:

- Accepting requests at http://localhost:3000, with a 3 second delayed response
- Accepting requests at http://localhost:3000, with a 3 second delayed response
of `{ ok: true }`.
- An onRequest hook that triggers when every request is received.
- Logic that triggers in the hook when the request is closed.
Expand All @@ -108,7 +108,7 @@ app.get('/', async (request, reply) => {
})
```

At any point in your business logic, you can check if the request has been
At any point in your business logic, you can check if the request has been
aborted and perform alternative actions.

```js
Expand All @@ -122,14 +122,14 @@ app.get('/', async (request, reply) => {
})
```

A benefit to adding this in your application code is that you can log Fastify
details such as the reqId, which may be unavailable in lower-level code that
A benefit to adding this in your application code is that you can log Fastify
details such as the reqId, which may be unavailable in lower-level code that
only has access to the raw request information.

### Testing

To test this functionality you can use an app like Postman and cancel your
request within 3 seconds. Alternatively, you can use Node to send an HTTP
To test this functionality you can use an app like Postman and cancel your
request within 3 seconds. Alternatively, you can use Node to send an HTTP
request with logic to abort the request before 3 seconds. Example:

```js
Expand All @@ -151,7 +151,7 @@ setTimeout(() => {
}, 1000);
```

With either approach, you should see the Fastify log appear at the moment the
With either approach, you should see the Fastify log appear at the moment the
request is aborted.

## Conclusion
Expand All @@ -160,13 +160,13 @@ Specifics of the implementation will vary from one problem to another, but the
main goal of this guide was to show a very specific use case of an issue that
could be solved within Fastify's ecosystem.

You can listen to the request close event and determine if the request was
aborted or if it was successfully delivered. You can implement this solution
You can listen to the request close event and determine if the request was
aborted or if it was successfully delivered. You can implement this solution
in an onRequest hook or directly in an individual route.

This approach will not trigger in the event of internet disruption, and such
detection would require additional business logic. If you have flawed backend
application logic that results in a server crash, then you could trigger a
false detection. The `clientErrorHandler`, either by default or with custom
logic, is not intended to handle this scenario and will not trigger when the
This approach will not trigger in the event of internet disruption, and such
detection would require additional business logic. If you have flawed backend
application logic that results in a server crash, then you could trigger a
false detection. The `clientErrorHandler`, either by default or with custom
logic, is not intended to handle this scenario and will not trigger when the
client aborts a request.
8 changes: 4 additions & 4 deletions docs/Guides/Ecosystem.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ section.
plugin to authenticate HTTP requests based on API key and signature
- [`fastify-appwrite`](https://github.com/Dev-Manny/fastify-appwrite) Fastify
Plugin for interacting with Appwrite server.
- [`fastify-asyncforge`](https://github.com/mcollina/fastify-asyncforge) Plugin
- [`fastify-asyncforge`](https://github.com/mcollina/fastify-asyncforge) Plugin
to access Fastify instance, logger, request and reply from Node.js [Async
Local Storage](https://nodejs.org/api/async_context.html#class-asynclocalstorage).
- [`fastify-at-mysql`](https://github.com/mateonunez/fastify-at-mysql) Fastify
Expand Down Expand Up @@ -277,7 +277,7 @@ section.
development servers that require Babel transformations of JavaScript sources.
- [`fastify-bcrypt`](https://github.com/beliven-it/fastify-bcrypt) A Bcrypt hash
generator & checker.
- [`fastify-better-sqlite3`](https://github.com/punkish/fastify-better-sqlite3)
- [`fastify-better-sqlite3`](https://github.com/punkish/fastify-better-sqlite3)
Plugin for better-sqlite3.
- [`fastify-blipp`](https://github.com/PavelPolyakov/fastify-blipp) Prints your
routes to the console, so you definitely know which endpoints are available.
Expand All @@ -289,7 +289,7 @@ section.
to add [bree](https://github.com/breejs/bree) support.
- [`fastify-bugsnag`](https://github.com/ZigaStrgar/fastify-bugsnag) Fastify plugin
to add support for [Bugsnag](https://www.bugsnag.com/) error reporting.
- [`fastify-cacheman`](https://gitlab.com/aalfiann/fastify-cacheman)
- [`fastify-cacheman`](https://gitlab.com/aalfiann/fastify-cacheman)
Small and efficient cache provider for Node.js with In-memory, File, Redis
and MongoDB engines for Fastify
- [`fastify-casbin`](https://github.com/nearform/fastify-casbin) Casbin support
Expand Down Expand Up @@ -344,7 +344,7 @@ section.
- [`fastify-event-bus`](https://github.com/Shiva127/fastify-event-bus) Event bus
support for Fastify. Built upon [js-event-bus](https://github.com/bcerati/js-event-bus).
- [`fastify-evervault`](https://github.com/Briscoooe/fastify-evervault/) Fastify
plugin for instantiating and encapsulating the
plugin for instantiating and encapsulating the
[Evervault](https://evervault.com/) client.
- [`fastify-explorer`](https://github.com/Eomm/fastify-explorer) Get control of
your decorators across all the encapsulated contexts.
Expand Down
2 changes: 1 addition & 1 deletion docs/Guides/Index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ This table of contents is in alphabetical order.
met in your application. This guide focuses on solving the problem using
[`Hooks`](../Reference/Hooks.md), [`Decorators`](../Reference/Decorators.md),
and [`Plugins`](../Reference/Plugins.md).
+ [Detecting When Clients Abort](./Detecting-When-Clients-Abort.md): A
+ [Detecting When Clients Abort](./Detecting-When-Clients-Abort.md): A
practical guide on detecting if and when a client aborts a request.
+ [Ecosystem](./Ecosystem.md): Lists all core plugins and many known community
plugins.
Expand Down
22 changes: 11 additions & 11 deletions docs/Guides/Migration-Guide-V4.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ work after upgrading.
## Codemods
### Fastify v4 Codemods

To help with the upgrade, we’ve worked with the team at
To help with the upgrade, we’ve worked with the team at
[Codemod](https://github.com/codemod-com/codemod) to
publish codemods that will automatically update your code to many of
publish codemods that will automatically update your code to many of
the new APIs and patterns in Fastify v4.

Run the following
[migration recipe](https://go.codemod.com/fastify-4-migration-recipe) to
Run the following
[migration recipe](https://go.codemod.com/fastify-4-migration-recipe) to
automatically update your code to Fastify v4:

```
Expand All @@ -30,7 +30,7 @@ This will run the following codemods:
- [`fastify/4/await-register-calls`](https://go.codemod.com/fastify-4-await-register-calls)

Each of these codemods automates the changes listed in the v4 migration guide.
For a complete list of available Fastify codemods and further details,
For a complete list of available Fastify codemods and further details,
see [Codemod Registry](https://go.codemod.com/fastify).


Expand All @@ -52,25 +52,25 @@ fastify.register(async fastify => {
console.log(err.message) // 'kaboom'
throw new Error('caught')
})

fastify.get('/encapsulated', async () => {
throw new Error('kaboom')
})
})

fastify.setErrorHandler(async err => {
console.log(err.message) // 'caught'
console.log(err.message) // 'caught'
throw new Error('wrapped')
})

const res = await fastify.inject('/encapsulated')
console.log(res.json().message) // 'wrapped'
```

>The root error handler is Fastify’s generic error handler.
>This error handler will use the headers and status code in the Error object,
>The root error handler is Fastify’s generic error handler.
>This error handler will use the headers and status code in the Error object,
>if they exist. **The headers and status code will not be automatically set if
>a custom error handler is provided**.
>a custom error handler is provided**.
### Removed `app.use()` ([#3506](https://github.com/fastify/fastify/pull/3506))

Expand Down Expand Up @@ -242,7 +242,7 @@ As such, schemas like below will need to be changed from:
properties: {
api_key: { type: 'string' },
image: { type: ['object', 'array'] }
}
}
}
```
Expand Down
Loading

0 comments on commit 9eab296

Please sign in to comment.