Skip to content

Commit 0b00f7b

Browse files
authored
docs(reference): even more conciseness (fastify#5951)
* docs(reference): even more conciseness * docs(reference/principles): add link to lts doc * chore: shrinky shrink
1 parent 4e0fa5d commit 0b00f7b

File tree

8 files changed

+127
-151
lines changed

8 files changed

+127
-151
lines changed

docs/Reference/ContentTypeParser.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -199,16 +199,15 @@ fastify.addContentTypeParser('*', function (request, payload, done) {
199199
All requests without a corresponding content type parser will be handled by
200200
this function.
201201

202-
This is also useful for piping the request stream. You can define a content
203-
parser like:
202+
This is also useful for piping the request stream. Define a content parser like:
204203

205204
```js
206205
fastify.addContentTypeParser('*', function (request, payload, done) {
207206
done()
208207
})
209208
```
210209

211-
And then access the core HTTP request directly for piping it where you want:
210+
And then access the core HTTP request directly for piping:
212211

213212
```js
214213
app.post('/hello', (request, reply) => {

docs/Reference/Encapsulation.md

+28-33
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@
33
## Encapsulation
44
<a id="encapsulation"></a>
55

6-
A fundamental feature of Fastify is the "encapsulation context." The
7-
encapsulation context governs which [decorators](./Decorators.md), registered
8-
[hooks](./Hooks.md), and [plugins](./Plugins.md) are available to
9-
[routes](./Routes.md). A visual representation of the encapsulation context
10-
is shown in the following figure:
6+
A fundamental feature of Fastify is the "encapsulation context." It governs
7+
which [decorators](./Decorators.md), registered [hooks](./Hooks.md), and
8+
[plugins](./Plugins.md) are available to [routes](./Routes.md). A visual
9+
representation of the encapsulation context is shown in the following figure:
1110

1211
![Figure 1](../resources/encapsulation_context.svg)
1312

14-
In the above figure, there are several entities:
13+
In the figure above, there are several entities:
1514

1615
1. The _root context_
1716
2. Three _root plugins_
18-
3. Two _child contexts_ where each _child context_ has
17+
3. Two _child contexts_, each with:
1918
* Two _child plugins_
20-
* One _grandchild context_ where each _grandchild context_ has
19+
* One _grandchild context_, each with:
2120
- Three _child plugins_
2221

2322
Every _child context_ and _grandchild context_ has access to the _root plugins_.
@@ -26,15 +25,14 @@ _child plugins_ registered within the containing _child context_, but the
2625
containing _child context_ **does not** have access to the _child plugins_
2726
registered within its _grandchild context_.
2827

29-
Given that everything in Fastify is a [plugin](./Plugins.md), except for the
28+
Given that everything in Fastify is a [plugin](./Plugins.md) except for the
3029
_root context_, every "context" and "plugin" in this example is a plugin
31-
that can consist of decorators, hooks, plugins, and routes. Thus, to put
32-
this example into concrete terms, consider a basic scenario of a REST API
33-
server that has three routes: the first route (`/one`) requires authentication,
34-
the second route (`/two`) does not, and the third route (`/three`) has
35-
access to the same context as the second route. Using
36-
[@fastify/bearer-auth][bearer] to provide the authentication, the code for this
37-
example is as follows:
30+
that can consist of decorators, hooks, plugins, and routes. To put this
31+
example into concrete terms, consider a basic scenario of a REST API server
32+
with three routes: the first route (`/one`) requires authentication, the
33+
second route (`/two`) does not, and the third route (`/three`) has access to
34+
the same context as the second route. Using [@fastify/bearer-auth][bearer] to
35+
provide authentication, the code for this example is as follows:
3836

3937
```js
4038
'use strict'
@@ -52,9 +50,9 @@ fastify.register(async function authenticatedContext (childServer) {
5250
handler (request, response) {
5351
response.send({
5452
answer: request.answer,
55-
// request.foo will be undefined as it's only defined in publicContext
53+
// request.foo will be undefined as it is only defined in publicContext
5654
foo: request.foo,
57-
// request.bar will be undefined as it's only defined in grandchildContext
55+
// request.bar will be undefined as it is only defined in grandchildContext
5856
bar: request.bar
5957
})
6058
}
@@ -71,7 +69,7 @@ fastify.register(async function publicContext (childServer) {
7169
response.send({
7270
answer: request.answer,
7371
foo: request.foo,
74-
// request.bar will be undefined as it's only defined in grandchildContext
72+
// request.bar will be undefined as it is only defined in grandchildContext
7573
bar: request.bar
7674
})
7775
}
@@ -97,16 +95,16 @@ fastify.register(async function publicContext (childServer) {
9795
fastify.listen({ port: 8000 })
9896
```
9997

100-
The above server example shows all of the encapsulation concepts outlined in the
98+
The server example above demonstrates the encapsulation concepts from the
10199
original diagram:
102100

103101
1. Each _child context_ (`authenticatedContext`, `publicContext`, and
104-
`grandchildContext`) has access to the `answer` request decorator defined in
105-
the _root context_.
102+
`grandchildContext`) has access to the `answer` request decorator defined in
103+
the _root context_.
106104
2. Only the `authenticatedContext` has access to the `@fastify/bearer-auth`
107-
plugin.
105+
plugin.
108106
3. Both the `publicContext` and `grandchildContext` have access to the `foo`
109-
request decorator.
107+
request decorator.
110108
4. Only the `grandchildContext` has access to the `bar` request decorator.
111109

112110
To see this, start the server and issue requests:
@@ -125,16 +123,13 @@ To see this, start the server and issue requests:
125123
## Sharing Between Contexts
126124
<a id="shared-context"></a>
127125

128-
Notice that each context in the prior example inherits _only_ from the parent
129-
contexts. Parent contexts cannot access any entities within their descendent
130-
contexts. This default is occasionally not desired. In such cases, the
131-
encapsulation context can be broken through the usage of
132-
[fastify-plugin][fastify-plugin] such that anything registered in a descendent
133-
context is available to the containing parent context.
126+
Each context in the prior example inherits _only_ from its parent contexts. Parent
127+
contexts cannot access entities within their descendant contexts. If needed,
128+
encapsulation can be broken using [fastify-plugin][fastify-plugin], making
129+
anything registered in a descendant context available to the parent context.
134130

135-
Assuming the `publicContext` needs access to the `bar` decorator defined
136-
within the `grandchildContext` in the previous example, the code can be
137-
rewritten as:
131+
To allow `publicContext` access to the `bar` decorator in `grandchildContext`,
132+
rewrite the code as follows:
138133

139134
```js
140135
'use strict'

docs/Reference/HTTP2.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
## HTTP2
44

5-
_Fastify_ supports HTTP2 over either HTTPS (h2) or plaintext (h2c).
5+
_Fastify_ supports HTTP2 over HTTPS (h2) or plaintext (h2c).
66

77
Currently, none of the HTTP2-specific APIs are available through _Fastify_, but
8-
Node's `req` and `res` can be accessed through our `Request` and `Reply`
9-
interface. PRs are welcome.
8+
Node's `req` and `res` can be accessed through the `Request` and `Reply`
9+
interfaces. PRs are welcome.
1010

1111
### Secure (HTTPS)
1212

@@ -61,16 +61,16 @@ fastify.get('/', function (request, reply) {
6161
fastify.listen({ port: 3000 })
6262
```
6363

64-
You can test your new server with:
64+
Test the new server with:
6565

6666
```
6767
$ npx h2url https://localhost:3000
6868
```
6969

7070
### Plain or insecure
7171

72-
If you are building microservices, you can connect to HTTP2 in plain text,
73-
however, this is not supported by browsers.
72+
For microservices, HTTP2 can connect in plain text, but this is not
73+
supported by browsers.
7474

7575
```js
7676
'use strict'
@@ -86,7 +86,7 @@ fastify.get('/', function (request, reply) {
8686
fastify.listen({ port: 3000 })
8787
```
8888

89-
You can test your new server with:
89+
Test the new server with:
9090

9191
```
9292
$ npx h2url http://localhost:3000

docs/Reference/LTS.md

+5-10
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,11 @@ in this document:
2525
and verified against alternative runtimes that are compatible with Node.js.
2626
The maintenance teams of these alternative runtimes are responsible for ensuring
2727
and guaranteeing these tests work properly.
28-
1. [N|Solid](https://docs.nodesource.com/docs/product_suite), 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.
28+
1. [N|Solid](https://docs.nodesource.com/docs/product_suite) tests and
29+
verifies each Fastify major release against current N|Solid LTS versions.
30+
NodeSource ensures Fastify compatibility with N|Solid, aligning with the
31+
support scope of N|Solid LTS versions at the time of the Fastify release.
32+
This guarantees N|Solid users can confidently use Fastify.
3533

3634
A "month" is defined as 30 consecutive days.
3735

@@ -40,13 +38,10 @@ A "month" is defined as 30 consecutive days.
4038
> As a consequence of providing long-term support for major releases, there are
4139
> occasions where we need to release breaking changes as a _minor_ version
4240
> release. Such changes will _always_ be noted in the [release
43-
> notes](https://github.com/fastify/fastify/releases).
4441
>
4542
> To avoid automatically receiving breaking security updates it is possible to
4643
> use the tilde (`~`) range qualifier. For example, to get patches for the 3.15
4744
> release, and avoid automatically updating to the 3.16 release, specify the
48-
> dependency as `"fastify": "~3.15.x"`. This will leave your application
49-
> vulnerable, so please use with caution.
5045
5146
### Security Support Beyond LTS
5247

docs/Reference/Lifecycle.md

+19-24
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
## Lifecycle
44
<a id="lifecycle"></a>
55

6-
Following the schema of the internal lifecycle of Fastify.
6+
This schema shows the internal lifecycle of Fastify.
77

8-
On the right branch of every section there is the next phase of the lifecycle,
9-
on the left branch there is the corresponding error code that will be generated
10-
if the parent throws an error *(note that all the errors are automatically
11-
handled by Fastify)*.
8+
The right branch of each section shows the next phase of the lifecycle. The left
9+
branch shows the corresponding error code generated if the parent throws an
10+
error. All errors are automatically handled by Fastify.
1211

1312
```
1413
Incoming Request
@@ -42,26 +41,23 @@ Incoming Request
4241
└─▶ onResponse Hook
4342
```
4443

45-
At any point before or during the `User Handler`, `reply.hijack()` can be called
46-
to prevent Fastify from:
47-
- Running all the following hooks and user handler
48-
- Sending the response automatically
44+
Before or during the `User Handler`, `reply.hijack()` can be called to:
45+
- Prevent Fastify from running subsequent hooks and the user handler
46+
- Prevent Fastify from sending the response automatically
4947

50-
NB (*): If `reply.raw` is used to send a response back to the user, `onResponse`
51-
hooks will still be executed
48+
If `reply.raw` is used to send a response, `onResponse` hooks will still
49+
be executed.
5250

5351
## Reply Lifecycle
5452
<a id="reply-lifecycle"></a>
5553

56-
Whenever the user handles the request, the result may be:
54+
When the user handles the request, the result may be:
5755

58-
- in async handler: it returns a payload
59-
- in async handler: it throws an `Error`
60-
- in sync handler: it sends a payload
61-
- in sync handler: it sends an `Error` instance
56+
- In an async handler: it returns a payload or throws an `Error`
57+
- In a sync handler: it sends a payload or an `Error` instance
6258

63-
If the reply was hijacked, we skip all the below steps. Otherwise, when it is
64-
being submitted, the data flow performed is the following:
59+
If the reply was hijacked, all subsequent steps are skipped. Otherwise, when
60+
submitted, the data flow is as follows:
6561

6662
```
6763
★ schema validation Error
@@ -81,9 +77,8 @@ being submitted, the data flow performed is the following:
8177
└─▶ reply sent
8278
```
8379

84-
Note: `reply sent` means that the JSON payload will be serialized by:
85-
86-
- the [reply serialized](./Server.md#setreplyserializer) if set
87-
- or by the [serializer compiler](./Server.md#setserializercompiler) when a JSON
88-
schema has been set for the returning HTTP status code
89-
- or by the default `JSON.stringify` function
80+
`reply sent` means the JSON payload will be serialized by one of the following:
81+
- The [reply serializer](./Server.md#setreplyserializer) if set
82+
- The [serializer compiler](./Server.md#setserializercompiler) if a JSON schema
83+
is set for the HTTP status code
84+
- The default `JSON.stringify` function

docs/Reference/Middleware.md

+19-19
Original file line numberDiff line numberDiff line change
@@ -22,36 +22,36 @@ fastify.use(require('ienoopen')())
2222
fastify.use(require('x-xss-protection')())
2323
```
2424

25-
You can also use [`@fastify/middie`](https://github.com/fastify/middie), which provides
26-
support for simple Express-style middleware but with improved performance:
25+
[`@fastify/middie`](https://github.com/fastify/middie) can also be used,
26+
which provides support for simple Express-style middleware with improved
27+
performance:
2728

2829
```js
2930
await fastify.register(require('@fastify/middie'))
3031
fastify.use(require('cors')())
3132
```
3233

33-
Remember that middleware can be encapsulated; this means that you can decide
34-
where your middleware should run by using `register` as explained in the
35-
[plugins guide](../Guides/Plugins-Guide.md).
34+
Middleware can be encapsulated, allowing control over where it runs using
35+
`register` as explained in the [plugins guide](../Guides/Plugins-Guide.md).
3636

37-
Fastify middleware does not expose the `send` method or other methods specific to
38-
the Fastify [Reply](./Reply.md#reply) instance. This is because Fastify wraps
37+
Fastify middleware does not expose the `send` method or other methods specific
38+
to the Fastify [Reply](./Reply.md#reply) instance. This is because Fastify wraps
3939
the incoming `req` and `res` Node instances using the
4040
[Request](./Request.md#request) and [Reply](./Reply.md#reply) objects
41-
internally, but this is done after the middleware phase. If you need to create
42-
middleware, you have to use the Node `req` and `res` instances. Otherwise, you
43-
can use the `preHandler` hook that already has the
44-
[Request](./Request.md#request) and [Reply](./Reply.md#reply) Fastify instances.
45-
For more information, see [Hooks](./Hooks.md#hooks).
41+
internally, but this is done after the middleware phase. To create middleware,
42+
use the Node `req` and `res` instances. Alternatively, use the `preHandler` hook
43+
that already has the Fastify [Request](./Request.md#request) and
44+
[Reply](./Reply.md#reply) instances. For more information, see
45+
[Hooks](./Hooks.md#hooks).
4646

4747
#### Restrict middleware execution to certain paths
4848
<a id="restrict-usage"></a>
4949

50-
If you need to only run middleware under certain paths, just pass the path as
51-
the first parameter to `use` and you are done!
50+
To run middleware under certain paths, pass the path as the first parameter to
51+
`use`.
5252

53-
*Note that this does not support routes with parameters, (e.g.
54-
`/user/:id/comments`) and wildcards are not supported in multiple paths.*
53+
*Note: This does not support routes with parameters (e.g. `/user/:id/comments`)
54+
and wildcards are not supported in multiple paths.*
5555

5656
```js
5757
const path = require('node:path')
@@ -69,10 +69,10 @@ fastify.use(['/css', '/js'], serveStatic(path.join(__dirname, '/assets')))
6969

7070
### Alternatives
7171

72-
Fastify offers some alternatives to the most commonly used middleware, such as
73-
[`@fastify/helmet`](https://github.com/fastify/fastify-helmet) in case of
72+
Fastify offers alternatives to commonly used middleware, such as
73+
[`@fastify/helmet`](https://github.com/fastify/fastify-helmet) for
7474
[`helmet`](https://github.com/helmetjs/helmet),
7575
[`@fastify/cors`](https://github.com/fastify/fastify-cors) for
7676
[`cors`](https://github.com/expressjs/cors), and
7777
[`@fastify/static`](https://github.com/fastify/fastify-static) for
78-
[`serve-static`](https://github.com/expressjs/serve-static).
78+
[`serve-static`](https://github.com/expressjs/serve-static).

0 commit comments

Comments
 (0)