fastify 3.24.0 → 3.25.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/README.md +30 -29
  2. package/docs/{Benchmarking.md → Guides/Benchmarking.md} +14 -5
  3. package/docs/Guides/Ecosystem.md +513 -0
  4. package/docs/{Fluent-Schema.md → Guides/Fluent-Schema.md} +16 -7
  5. package/docs/{Getting-Started.md → Guides/Getting-Started.md} +180 -60
  6. package/docs/Guides/Index.md +30 -4
  7. package/docs/{Migration-Guide-V3.md → Guides/Migration-Guide-V3.md} +43 -37
  8. package/docs/{Plugins-Guide.md → Guides/Plugins-Guide.md} +196 -82
  9. package/docs/{Recommendations.md → Guides/Recommendations.md} +17 -10
  10. package/docs/{Serverless.md → Guides/Serverless.md} +200 -42
  11. package/docs/Guides/Style-Guide.md +246 -0
  12. package/docs/{Testing.md → Guides/Testing.md} +26 -12
  13. package/docs/Guides/Write-Plugin.md +102 -0
  14. package/docs/{ContentTypeParser.md → Reference/ContentTypeParser.md} +68 -30
  15. package/docs/{Decorators.md → Reference/Decorators.md} +52 -47
  16. package/docs/{Encapsulation.md → Reference/Encapsulation.md} +3 -3
  17. package/docs/{Errors.md → Reference/Errors.md} +77 -47
  18. package/docs/{HTTP2.md → Reference/HTTP2.md} +13 -13
  19. package/docs/{Hooks.md → Reference/Hooks.md} +157 -70
  20. package/docs/Reference/Index.md +71 -0
  21. package/docs/{LTS.md → Reference/LTS.md} +31 -32
  22. package/docs/{Lifecycle.md → Reference/Lifecycle.md} +15 -7
  23. package/docs/{Logging.md → Reference/Logging.md} +68 -28
  24. package/docs/Reference/Middleware.md +78 -0
  25. package/docs/{Plugins.md → Reference/Plugins.md} +91 -34
  26. package/docs/{Reply.md → Reference/Reply.md} +205 -94
  27. package/docs/{Request.md → Reference/Request.md} +32 -16
  28. package/docs/{Routes.md → Reference/Routes.md} +243 -113
  29. package/docs/{Server.md → Reference/Server.md} +516 -267
  30. package/docs/{TypeScript.md → Reference/TypeScript.md} +451 -191
  31. package/docs/{Validation-and-Serialization.md → Reference/Validation-and-Serialization.md} +178 -86
  32. package/docs/index.md +24 -0
  33. package/examples/typescript-server.ts +1 -1
  34. package/fastify.js +2 -3
  35. package/lib/contentTypeParser.js +11 -6
  36. package/lib/decorate.js +6 -3
  37. package/lib/logger.js +1 -1
  38. package/lib/route.js +1 -1
  39. package/lib/server.js +9 -8
  40. package/package.json +9 -4
  41. package/test/als.test.js +74 -0
  42. package/test/constrained-routes.test.js +220 -0
  43. package/test/custom-parser.test.js +11 -2
  44. package/test/decorator.test.js +38 -0
  45. package/test/handler-context.test.js +11 -4
  46. package/test/http2/closing.test.js +14 -5
  47. package/test/http2/constraint.test.js +91 -0
  48. package/test/listen.test.js +36 -22
  49. package/test/logger.test.js +16 -0
  50. package/test/maxRequestsPerSocket.test.js +10 -0
  51. package/test/request-error.test.js +2 -8
  52. package/test/requestTimeout.test.js +4 -1
  53. package/test/router-options.test.js +10 -1
  54. package/test/schema-feature.test.js +146 -0
  55. package/test/stream.test.js +14 -3
  56. package/test/trust-proxy.test.js +15 -7
  57. package/test/types/instance.test-d.ts +52 -1
  58. package/test/types/request.test-d.ts +7 -1
  59. package/test/types/route.test-d.ts +21 -0
  60. package/types/hooks.d.ts +12 -1
  61. package/types/instance.d.ts +16 -6
  62. package/types/request.d.ts +4 -1
  63. package/types/route.d.ts +1 -1
  64. package/docs/Ecosystem.md +0 -211
  65. package/docs/Middleware.md +0 -53
  66. package/docs/Style-Guide.md +0 -185
  67. package/docs/Write-Plugin.md +0 -58
@@ -1,171 +1,201 @@
1
1
  <h1 align="center">Fastify</h1>
2
2
 
3
- <a id="errors"></a>
4
3
  ## Errors
4
+ <a id="errors"></a>
5
5
 
6
- <a name="error-handling"></a>
7
6
  ### Error Handling In Node.js
7
+ <a id="error-handling"></a>
8
8
 
9
9
  #### Uncaught Errors
10
- In Node.js, uncaught errors are likely to cause memory leaks, file descriptor leaks, and other major production issues. [Domains](https://nodejs.org/en/docs/guides/domain-postmortem/) were a failed attempt to fix this.
10
+ In Node.js, uncaught errors are likely to cause memory leaks, file descriptor
11
+ leaks, and other major production issues.
12
+ [Domains](https://nodejs.org/en/docs/guides/domain-postmortem/) were a failed
13
+ attempt to fix this.
11
14
 
12
- Given that it is not possible to process all uncaught errors sensibly, the best way to deal with them is to [crash](https://nodejs.org/api/process.html#process_warning_using_uncaughtexception_correctly).
15
+ Given that it is not possible to process all uncaught errors sensibly, the best
16
+ way to deal with them is to
17
+ [crash](https://nodejs.org/api/process.html#process_warning_using_uncaughtexception_correctly).
13
18
 
14
19
  #### Catching Errors In Promises
15
- In Node.js, unhandled promise rejections (that is, without a `.catch()` handler) can also cause memory and file descriptor leaks. While `unhandledRejection` is deprecated in Node.js, unhandled rejections will not throw, and still potentially leak. You should use a module like [`make-promises-safe`](https://github.com/mcollina/make-promises-safe) to ensure unhandled rejections _always_ throw.
20
+ In Node.js, unhandled promise rejections (that is, without a `.catch()` handler)
21
+ can also cause memory and file descriptor leaks. While `unhandledRejection` is
22
+ deprecated in Node.js, unhandled rejections will not throw, and still
23
+ potentially leak. You should use a module like
24
+ [`make-promises-safe`](https://github.com/mcollina/make-promises-safe) to ensure
25
+ unhandled rejections _always_ throw.
16
26
 
17
27
  If you are using promises, you should attach a `.catch()` handler synchronously.
18
28
 
19
29
  ### Errors In Fastify
20
- Fastify follows an all-or-nothing approach and aims to be lean and optimal as much as possible. The developer is responsible for making sure that the errors are handled properly.
30
+ Fastify follows an all-or-nothing approach and aims to be lean and optimal as
31
+ much as possible. The developer is responsible for making sure that the errors
32
+ are handled properly.
21
33
 
22
34
  #### Errors In Input Data
23
- Most errors are a result of unexpected input data, so we recommend [validating your input data against a JSON schema](Validation-and-Serialization.md).
35
+ Most errors are a result of unexpected input data, so we recommend [validating
36
+ your input data against a JSON schema](./Validation-and-Serialization.md).
24
37
 
25
38
  #### Catching Uncaught Errors In Fastify
26
- Fastify tries to catch as many uncaught errors as it can without hindering performance. This includes:
39
+ Fastify tries to catch as many uncaught errors as it can without hindering
40
+ performance. This includes:
27
41
 
28
42
  1. synchronous routes, e.g. `app.get('/', () => { throw new Error('kaboom') })`
29
- 2. `async` routes, e.g. `app.get('/', async () => { throw new Error('kaboom') })`
43
+ 2. `async` routes, e.g. `app.get('/', async () => { throw new Error('kaboom')
44
+ })`
30
45
 
31
- The error in both cases will be caught safely and routed to Fastify's default error handler for a generic `500 Internal Server Error` response.
46
+ The error in both cases will be caught safely and routed to Fastify's default
47
+ error handler for a generic `500 Internal Server Error` response.
32
48
 
33
- To customize this behavior you should use [`setErrorHandler`](Server.md#seterrorhandler).
49
+ To customize this behavior you should use
50
+ [`setErrorHandler`](./Server.md#seterrorhandler).
34
51
 
35
52
  ### Errors In Fastify Lifecycle Hooks And A Custom Error Handler
36
53
 
37
- From the [Hooks documentation](Hooks.md#manage-errors-from-a-hook):
38
- > If you get an error during the execution of your hook, just pass it to `done()` and Fastify will automatically close the request and send the appropriate error code to the user.
54
+ From the [Hooks documentation](./Hooks.md#manage-errors-from-a-hook):
55
+ > If you get an error during the execution of your hook, just pass it to
56
+ > `done()` and Fastify will automatically close the request and send the
57
+ > appropriate error code to the user.
39
58
 
40
- If you have defined a custom error handler for using `setErrorHandler` the error will be routed there. otherwise, it will be routed to Fastify’s generic error handler.
59
+ If you have defined a custom error handler for using `setErrorHandler` the error
60
+ will be routed there. otherwise, it will be routed to Fastify’s generic error
61
+ handler.
41
62
 
42
- Some things to consider in your custom error handler:
63
+ Some things to consider in your custom error handler:
43
64
 
44
- - you can `reply.send(data)`, which will behave as it would in [regular route handlers](Reply.md#senddata)
45
- - objects are serialized, triggering the `preSerialization` lifecycle hook if you have one defined
46
- - strings, buffers, and streams are sent to the client, with appropriate headers (no serialization)
65
+ - you can `reply.send(data)`, which will behave as it would in [regular route
66
+ handlers](./Reply.md#senddata)
67
+ - objects are serialized, triggering the `preSerialization` lifecycle hook if
68
+ you have one defined
69
+ - strings, buffers, and streams are sent to the client, with appropriate
70
+ headers (no serialization)
47
71
 
48
72
  - You can throw a new error in your custom error handler
49
- - errors (new error or the received error parameter re-thrown) - will trigger the `onError` lifecycle hook and send the error to the user
50
- - an error will not be triggered twice from a lifecycle hook - Fastify internally monitors the error invocation to avoid infinite loops for errors thrown in the reply phases of the lifecycle. (those after the route handler)
73
+ - errors (new error or the received error parameter re-thrown) - will trigger
74
+ the `onError` lifecycle hook and send the error to the user
75
+ - an error will not be triggered twice from a lifecycle hook - Fastify
76
+ internally monitors the error invocation to avoid infinite loops for errors
77
+ thrown in the reply phases of the lifecycle. (those after the route handler)
51
78
 
52
79
 
53
- <a name="fastify-error-codes"></a>
54
80
  ### Fastify Error Codes
81
+ <a id="fastify-error-codes"></a>
55
82
 
56
- <a name="FST_ERR_BAD_URL"></a>
57
83
  #### FST_ERR_BAD_URL
84
+ <a id="FST_ERR_BAD_URL"></a>
58
85
 
59
86
  The router received an invalid url.
60
87
 
61
- <a name="FST_ERR_CTP_ALREADY_PRESENT"></a>
62
88
  #### FST_ERR_CTP_ALREADY_PRESENT
89
+ <a id="FST_ERR_CTP_ALREADY_PRESENT"></a>
63
90
 
64
91
  The parser for this content type was already registered.
65
92
 
66
- <a name="FST_ERR_CTP_BODY_TOO_LARGE"></a>
67
93
  #### FST_ERR_CTP_BODY_TOO_LARGE
94
+ <a id="FST_ERR_CTP_BODY_TOO_LARGE"></a>
68
95
 
69
96
  The request body is larger than the provided limit.
70
97
 
71
- This setting can be defined in the Fastify server instance: [`bodyLimit`](Server.md#bodyLimit)
98
+ This setting can be defined in the Fastify server instance:
99
+ [`bodyLimit`](./Server.md#bodylimit)
72
100
 
73
- <a name="FST_ERR_CTP_EMPTY_TYPE"></a>
74
101
  #### FST_ERR_CTP_EMPTY_TYPE
102
+ <a id="FST_ERR_CTP_EMPTY_TYPE"></a>
75
103
 
76
104
  The content type cannot be an empty string.
77
105
 
78
- <a name="FST_ERR_CTP_INVALID_CONTENT_LENGTH"></a>
79
106
  #### FST_ERR_CTP_INVALID_CONTENT_LENGTH
107
+ <a id="FST_ERR_CTP_INVALID_CONTENT_LENGTH"></a>
80
108
 
81
109
  Request body size did not match Content-Length.
82
110
 
83
- <a name="FST_ERR_CTP_INVALID_HANDLER"></a>
84
111
  #### FST_ERR_CTP_INVALID_HANDLER
112
+ <a id="FST_ERR_CTP_INVALID_HANDLER"></a>
85
113
 
86
114
  An invalid handler was passed for the content type.
87
115
 
88
- <a name="FST_ERR_CTP_INVALID_MEDIA_TYPE"></a>
89
116
  #### FST_ERR_CTP_INVALID_MEDIA_TYPE
117
+ <a id="FST_ERR_CTP_INVALID_MEDIA_TYPE"></a>
90
118
 
91
- The received media type is not supported (i.e. there is no suitable `Content-Type` parser for it).
119
+ The received media type is not supported (i.e. there is no suitable
120
+ `Content-Type` parser for it).
92
121
 
93
- <a name="FST_ERR_CTP_INVALID_PARSE_TYPE"></a>
94
122
  #### FST_ERR_CTP_INVALID_PARSE_TYPE
123
+ <a id="FST_ERR_CTP_INVALID_PARSE_TYPE"></a>
95
124
 
96
- The provided parse type is not supported. Accepted values are `string` or `buffer`.
125
+ The provided parse type is not supported. Accepted values are `string` or
126
+ `buffer`.
97
127
 
98
- <a name="FST_ERR_CTP_INVALID_TYPE"></a>
99
128
  #### FST_ERR_CTP_INVALID_TYPE
129
+ <a id="FST_ERR_CTP_INVALID_TYPE"></a>
100
130
 
101
131
  The `Content-Type` should be a string.
102
132
 
103
- <a name="FST_ERR_DEC_ALREADY_PRESENT"></a>
104
133
  #### FST_ERR_DEC_ALREADY_PRESENT
134
+ <a id="FST_ERR_DEC_ALREADY_PRESENT"></a>
105
135
 
106
136
  A decorator with the same name is already registered.
107
137
 
108
- <a name="FST_ERR_DEC_MISSING_DEPENDENCY"></a>
109
138
  #### FST_ERR_DEC_MISSING_DEPENDENCY
139
+ <a id="FST_ERR_DEC_MISSING_DEPENDENCY"></a>
110
140
 
111
141
  The decorator cannot be registered due to a missing dependency.
112
142
 
113
- <a name="FST_ERR_HOOK_INVALID_HANDLER"></a>
114
143
  #### FST_ERR_HOOK_INVALID_HANDLER
144
+ <a id="FST_ERR_HOOK_INVALID_HANDLER"></a>
115
145
 
116
146
  The hook callback must be a function.
117
147
 
118
- <a name="FST_ERR_HOOK_INVALID_TYPE"></a>
119
148
  #### FST_ERR_HOOK_INVALID_TYPE
149
+ <a id="FST_ERR_HOOK_INVALID_TYPE"></a>
120
150
 
121
151
  The hook name must be a string.
122
152
 
123
- <a name="FST_ERR_LOG_INVALID_DESTINATION"></a>
124
153
  #### FST_ERR_LOG_INVALID_DESTINATION
154
+ <a id="FST_ERR_LOG_INVALID_DESTINATION"></a>
125
155
 
126
156
  The logger accepts either a `'stream'` or a `'file'` as the destination.
127
157
 
128
- <a name="FST_ERR_PROMISE_NOT_FULFILLED"></a>
129
158
  #### FST_ERR_PROMISE_NOT_FULFILLED
159
+ <a id="FST_ERR_PROMISE_NOT_FULFILLED"></a>
130
160
 
131
161
  A promise may not be fulfilled with 'undefined' when statusCode is not 204.
132
162
 
133
- <a id="FST_ERR_REP_ALREADY_SENT"></a>
134
163
  #### FST_ERR_REP_ALREADY_SENT
164
+ <a id="FST_ERR_REP_ALREADY_SENT"></a>
135
165
 
136
166
  A response was already sent.
137
167
 
138
- <a name="FST_ERR_REP_INVALID_PAYLOAD_TYPE"></a>
139
168
  #### FST_ERR_REP_INVALID_PAYLOAD_TYPE
169
+ <a id="FST_ERR_REP_INVALID_PAYLOAD_TYPE"></a>
140
170
 
141
171
  Reply payload can be either a `string` or a `Buffer`.
142
172
 
143
- <a name="FST_ERR_SCH_ALREADY_PRESENT"></a>
144
173
  #### FST_ERR_SCH_ALREADY_PRESENT
174
+ <a id="FST_ERR_SCH_ALREADY_PRESENT"></a>
145
175
 
146
176
  A schema with the same `$id` already exists.
147
177
 
148
- <a name="FST_ERR_SCH_MISSING_ID"></a>
149
178
  #### FST_ERR_SCH_MISSING_ID
179
+ <a id="FST_ERR_SCH_MISSING_ID"></a>
150
180
 
151
181
  The schema provided does not have `$id` property.
152
182
 
153
- <a name="FST_ERR_SCH_SERIALIZATION_BUILD"></a>
154
183
  #### FST_ERR_SCH_SERIALIZATION_BUILD
184
+ <a id="FST_ERR_SCH_SERIALIZATION_BUILD"></a>
155
185
 
156
186
  The JSON schema provided for serialization of a route response is not valid.
157
187
 
158
- <a name="FST_ERR_SCH_VALIDATION_BUILD"></a>
159
188
  #### FST_ERR_SCH_VALIDATION_BUILD
189
+ <a id="FST_ERR_SCH_VALIDATION_BUILD"></a>
160
190
 
161
191
  The JSON schema provided for validation to a route is not valid.
162
192
 
163
- <a id="FST_ERR_SEND_INSIDE_ONERR"></a>
164
193
  #### FST_ERR_SEND_INSIDE_ONERR
194
+ <a id="FST_ERR_SEND_INSIDE_ONERR"></a>
165
195
 
166
196
  You cannot use `send` inside the `onError` hook.
167
197
 
168
- <a name="FST_ERR_SEND_UNDEFINED_ERR"></a>
169
198
  #### FST_ERR_SEND_UNDEFINED_ERR
199
+ <a id="FST_ERR_SEND_UNDEFINED_ERR"></a>
170
200
 
171
201
  Undefined error has occurred.
@@ -2,18 +2,17 @@
2
2
 
3
3
  ## HTTP2
4
4
 
5
- _Fastify_ offers **experimental support** for HTTP2 starting from
6
- Node 8 LTS, which includes HTTP2 without a flag; HTTP2 is supported
7
- over either HTTPS or plaintext.
5
+ _Fastify_ offers **experimental support** for HTTP2 starting from Node 8 LTS,
6
+ which includes HTTP2 without a flag; HTTP2 is supported over either HTTPS or
7
+ plaintext.
8
8
 
9
- Currently, none of the HTTP2-specific APIs are available through
10
- _Fastify_, but Node's `req` and `res` can be accessed through our
11
- `Request` and `Reply` interface. PRs are welcome.
9
+ Currently, none of the HTTP2-specific APIs are available through _Fastify_, but
10
+ Node's `req` and `res` can be accessed through our `Request` and `Reply`
11
+ interface. PRs are welcome.
12
12
 
13
13
  ### Secure (HTTPS)
14
14
 
15
- HTTP2 is supported in all modern browsers __only over a secure
16
- connection__:
15
+ HTTP2 is supported in all modern browsers __only over a secure connection__:
17
16
 
18
17
  ```js
19
18
  'use strict'
@@ -36,9 +35,10 @@ fastify.listen(3000)
36
35
  ```
37
36
 
38
37
  ALPN negotiation allows support for both HTTPS and HTTP/2 over the same socket.
39
- Node core `req` and `res` objects can be either [HTTP/1](https://nodejs.org/api/http.html)
40
- or [HTTP/2](https://nodejs.org/api/http2.html).
41
- _Fastify_ supports this out of the box:
38
+ Node core `req` and `res` objects can be either
39
+ [HTTP/1](https://nodejs.org/api/http.html) or
40
+ [HTTP/2](https://nodejs.org/api/http2.html). _Fastify_ supports this out of the
41
+ box:
42
42
 
43
43
  ```js
44
44
  'use strict'
@@ -70,8 +70,8 @@ $ npx h2url https://localhost:3000
70
70
 
71
71
  ### Plain or insecure
72
72
 
73
- If you are building microservices, you can connect to HTTP2 in plain
74
- text, however, this is not supported by browsers.
73
+ If you are building microservices, you can connect to HTTP2 in plain text,
74
+ however, this is not supported by browsers.
75
75
 
76
76
  ```js
77
77
  'use strict'