fastify 3.21.4 → 3.22.1
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/build/build-validation.js +2 -0
- package/docs/Ecosystem.md +1 -0
- package/docs/Hooks.md +2 -2
- package/docs/Recommendations.md +55 -19
- package/docs/Request.md +1 -1
- package/docs/Server.md +14 -2
- package/docs/TypeScript.md +3 -0
- package/docs/Validation-and-Serialization.md +1 -1
- package/fastify.d.ts +13 -0
- package/fastify.js +2 -1
- package/lib/configValidator.js +456 -423
- package/lib/decorate.js +9 -5
- package/lib/reply.js +8 -3
- package/lib/request.js +4 -1
- package/lib/server.js +6 -0
- package/package.json +2 -3
- package/test/decorator.test.js +89 -4
- package/test/internals/initialConfig.test.js +2 -0
- package/test/internals/reply.test.js +1 -1
- package/test/maxRequestsPerSocket.test.js +104 -0
- package/test/reply-error.test.js +28 -0
- package/test/schema-special-usage.test.js +1 -1
- package/test/stream.test.js +2 -2
- package/test/types/fastify.test-d.ts +15 -0
- package/test/types/register.test-d.ts +13 -1
- package/types/register.d.ts +2 -1
|
@@ -15,6 +15,7 @@ const ajv = new Ajv({
|
|
|
15
15
|
const defaultInitOptions = {
|
|
16
16
|
connectionTimeout: 0, // 0 sec
|
|
17
17
|
keepAliveTimeout: 5000, // 5 sec
|
|
18
|
+
maxRequestsPerSocket: 0, // no limit
|
|
18
19
|
bodyLimit: 1024 * 1024, // 1 MiB
|
|
19
20
|
caseSensitive: true,
|
|
20
21
|
disableRequestLogging: false,
|
|
@@ -47,6 +48,7 @@ const schema = {
|
|
|
47
48
|
properties: {
|
|
48
49
|
connectionTimeout: { type: 'integer', default: defaultInitOptions.connectionTimeout },
|
|
49
50
|
keepAliveTimeout: { type: 'integer', default: defaultInitOptions.keepAliveTimeout },
|
|
51
|
+
maxRequestsPerSocket: { type: 'integer', default: defaultInitOptions.maxRequestsPerSocket, nullable: true },
|
|
50
52
|
bodyLimit: { type: 'integer', default: defaultInitOptions.bodyLimit },
|
|
51
53
|
caseSensitive: { type: 'boolean', default: defaultInitOptions.caseSensitive },
|
|
52
54
|
http2: { type: 'boolean' },
|
package/docs/Ecosystem.md
CHANGED
|
@@ -187,6 +187,7 @@ Plugins maintained by the Fastify team are listed under [Core](#core) while plug
|
|
|
187
187
|
- [`fastify-sse`](https://github.com/lolo32/fastify-sse) to provide Server-Sent Events with `reply.sse( … )` to Fastify.
|
|
188
188
|
- [`fastify-sse-v2`](https://github.com/nodefactoryio/fastify-sse-v2) to provide Server-Sent Events using Async Iterators (supports newer versions of Fastify).
|
|
189
189
|
- [`fastify-stripe`](https://github.com/coopflow/fastify-stripe) Plugin to initialize and encapsulate [Stripe Node.js](https://github.com/stripe/stripe-node) instances in Fastify.
|
|
190
|
+
- [`fastify-supabase`](https://github.com/coopflow/fastify-supabase) Plugin to initialize and encapsulate [Supabase](https://github.com/supabase/supabase-js) instances in Fastify.
|
|
190
191
|
- [`fastify-tls-keygen`](https://gitlab.com/sebdeckers/fastify-tls-keygen) Automatically generate a browser-compatible, trusted, self-signed, localhost-only, TLS certificate.
|
|
191
192
|
- [`fastify-tokenize`](https://github.com/Bowser65/fastify-tokenize) [Tokenize](https://github.com/Bowser65/Tokenize) plugin for Fastify that removes the pain of managing authentication tokens, with built-in integration for `fastify-auth`.
|
|
192
193
|
- [`fastify-totp`](https://github.com/heply/fastify-totp) A plugin to handle TOTP (e.g. for 2FA).
|
package/docs/Hooks.md
CHANGED
|
@@ -90,7 +90,7 @@ If you are using the `preValidation` hook, you can change the payload before it
|
|
|
90
90
|
|
|
91
91
|
```js
|
|
92
92
|
fastify.addHook('preValidation', (request, reply, done) => {
|
|
93
|
-
|
|
93
|
+
request.body = { ...request.body, importantKey: 'randomString' }
|
|
94
94
|
done()
|
|
95
95
|
})
|
|
96
96
|
```
|
|
@@ -98,7 +98,7 @@ Or `async/await`:
|
|
|
98
98
|
```js
|
|
99
99
|
fastify.addHook('preValidation', async (request, reply) => {
|
|
100
100
|
const importantKey = await generateRandomString()
|
|
101
|
-
|
|
101
|
+
request.body = { ...request.body, importantKey }
|
|
102
102
|
})
|
|
103
103
|
```
|
|
104
104
|
|
package/docs/Recommendations.md
CHANGED
|
@@ -166,62 +166,94 @@ backend static-backend
|
|
|
166
166
|
### Nginx
|
|
167
167
|
|
|
168
168
|
```nginx
|
|
169
|
+
# This upstream block groups 3 servers into one named backend fastify_app
|
|
170
|
+
# with 2 primary servers distributed via round-robin
|
|
171
|
+
# and one backup which is used when the first 2 are not reachable
|
|
172
|
+
# This also assumes your fastify servers are listening on port 80.
|
|
173
|
+
# more info: http://nginx.org/en/docs/http/ngx_http_upstream_module.html
|
|
169
174
|
upstream fastify_app {
|
|
170
|
-
# more info: http://nginx.org/en/docs/http/ngx_http_upstream_module.html
|
|
171
175
|
server 10.10.11.1:80;
|
|
172
176
|
server 10.10.11.2:80;
|
|
173
177
|
server 10.10.11.3:80 backup;
|
|
174
178
|
}
|
|
175
179
|
|
|
180
|
+
# This server block asks NGINX to respond with a redirect when
|
|
181
|
+
# an incoming request from port 80 (typically plain HTTP), to
|
|
182
|
+
# the same request URL but with HTTPS as protocol.
|
|
183
|
+
# This block is optional, and usually used if you are handling
|
|
184
|
+
# SSL termination in NGINX, like in the example here.
|
|
176
185
|
server {
|
|
177
|
-
# default server
|
|
186
|
+
# default server is a special parameter to ask NGINX
|
|
187
|
+
# to set this server block to the default for this address/port
|
|
188
|
+
# which in this case is any address and port 80
|
|
178
189
|
listen 80 default_server;
|
|
179
190
|
listen [::]:80 default_server;
|
|
180
191
|
|
|
181
|
-
#
|
|
192
|
+
# With a server_name directive you can also ask NGINX to
|
|
193
|
+
# use this server block only with matching server name(s)
|
|
182
194
|
# listen 80;
|
|
183
195
|
# listen [::]:80;
|
|
184
196
|
# server_name example.tld;
|
|
185
197
|
|
|
198
|
+
# This matches all paths from the request and responds with
|
|
199
|
+
# the redirect mentioned above.
|
|
186
200
|
location / {
|
|
187
201
|
return 301 https://$host$request_uri;
|
|
188
202
|
}
|
|
189
203
|
}
|
|
190
204
|
|
|
205
|
+
# This server block asks NGINX to respond to requests from
|
|
206
|
+
# port 443 with SSL enabled and accept HTTP/2 connections.
|
|
207
|
+
# This is where the request is then proxied to the fastify_app
|
|
208
|
+
# server group via port 3000.
|
|
191
209
|
server {
|
|
192
|
-
#
|
|
210
|
+
# This listen directive asks NGINX to accept requests
|
|
211
|
+
# coming to any address, port 443, with SSL, and HTTP/2
|
|
212
|
+
# if possible.
|
|
193
213
|
listen 443 ssl http2 default_server;
|
|
194
214
|
listen [::]:443 ssl http2 default_server;
|
|
195
|
-
|
|
196
|
-
#
|
|
215
|
+
|
|
216
|
+
# With a server_name directive you can also ask NGINX to
|
|
217
|
+
# use this server block only with matching server name(s)
|
|
197
218
|
# listen 443 ssl http2;
|
|
198
219
|
# listen [::]:443 ssl http2;
|
|
199
220
|
# server_name example.tld;
|
|
200
221
|
|
|
201
|
-
#
|
|
222
|
+
# Your SSL/TLS certificate (chain) and secret key in the PEM format
|
|
202
223
|
ssl_certificate /path/to/fullchain.pem;
|
|
203
224
|
ssl_certificate_key /path/to/private.pem;
|
|
204
|
-
ssl_trusted_certificate /path/to/chain.pem;
|
|
205
225
|
|
|
206
|
-
#
|
|
226
|
+
# A generic best practice baseline for based
|
|
227
|
+
# on https://ssl-config.mozilla.org/
|
|
207
228
|
ssl_session_timeout 1d;
|
|
208
229
|
ssl_session_cache shared:FastifyApp:10m;
|
|
209
230
|
ssl_session_tickets off;
|
|
210
|
-
|
|
211
|
-
#
|
|
231
|
+
|
|
232
|
+
# This tells NGINX to only accept TLS 1.3, which should be fine
|
|
233
|
+
# with most modern browsers including IE 11 with certain updates.
|
|
234
|
+
# If you want to support older browsers you might need to add
|
|
235
|
+
# additional fallback protocols.
|
|
212
236
|
ssl_protocols TLSv1.3;
|
|
213
237
|
ssl_prefer_server_ciphers off;
|
|
214
|
-
|
|
215
|
-
#
|
|
238
|
+
|
|
239
|
+
# This adds a header that tells browsers to only ever use HTTPS
|
|
240
|
+
# with this server.
|
|
216
241
|
add_header Strict-Transport-Security "max-age=63072000" always;
|
|
217
|
-
|
|
218
|
-
#
|
|
242
|
+
|
|
243
|
+
# The following directives are only necessary if you want to
|
|
244
|
+
# enable OCSP Stapling.
|
|
219
245
|
ssl_stapling on;
|
|
220
246
|
ssl_stapling_verify on;
|
|
247
|
+
ssl_trusted_certificate /path/to/chain.pem;
|
|
221
248
|
|
|
222
|
-
#
|
|
249
|
+
# Custom nameserver to resolve upstream server names
|
|
223
250
|
# resolver 127.0.0.1;
|
|
224
|
-
|
|
251
|
+
|
|
252
|
+
# This section matches all paths and proxies it to the backend server
|
|
253
|
+
# group specified above. Note the additional headers that forward
|
|
254
|
+
# information about the original request. You might want to set
|
|
255
|
+
# trustProxy to the address of your NGINX server so the X-Forwarded
|
|
256
|
+
* fields are used by fastify.
|
|
225
257
|
location / {
|
|
226
258
|
# more info: http://nginx.org/en/docs/http/ngx_http_proxy_module.html
|
|
227
259
|
proxy_http_version 1.1;
|
|
@@ -232,8 +264,12 @@ server {
|
|
|
232
264
|
proxy_set_header X-Real-IP $remote_addr;
|
|
233
265
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
234
266
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
235
|
-
|
|
236
|
-
|
|
267
|
+
|
|
268
|
+
# This is the directive that proxies requests to the specified server.
|
|
269
|
+
# If you are using an upstream group, then you do not need to specify a port.
|
|
270
|
+
# If you are directly proxying to a server e.g.
|
|
271
|
+
# proxy_pass http://127.0.0.1:3000 then specify a port.
|
|
272
|
+
proxy_pass http://fastify_app;
|
|
237
273
|
}
|
|
238
274
|
}
|
|
239
275
|
```
|
package/docs/Request.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
## Request
|
|
4
4
|
The first parameter of the handler function is `Request`.<br>
|
|
5
5
|
Request is a core Fastify object containing the following fields:
|
|
6
|
-
- `query` - the parsed querystring, its format is specified by [`querystringParser`](Server.md#
|
|
6
|
+
- `query` - the parsed querystring, its format is specified by [`querystringParser`](Server.md#querystringparser)
|
|
7
7
|
- `body` - the body
|
|
8
8
|
- `params` - the params matching the URL
|
|
9
9
|
- [`headers`](#headers) - the headers getter and setter
|
package/docs/Server.md
CHANGED
|
@@ -12,6 +12,7 @@ document describes the properties available in that options object.
|
|
|
12
12
|
- [https](./Server.md#https)
|
|
13
13
|
- [connectionTimeout](./Server.md#connectiontimeout)
|
|
14
14
|
- [keepAliveTimeout](./Server.md#keepalivetimeout)
|
|
15
|
+
- [maxRequestsPerSocket](./Server.md#maxRequestsPerSocket)
|
|
15
16
|
- [ignoreTrailingSlash](./Server.md#ignoretrailingslash)
|
|
16
17
|
- [maxParamLength](./Server.md#maxparamlength)
|
|
17
18
|
- [onProtoPoisoning](./Server.md#onprotopoisoning)
|
|
@@ -25,7 +26,7 @@ document describes the properties available in that options object.
|
|
|
25
26
|
- [genReqId](./Server.md#genreqid)
|
|
26
27
|
- [trustProxy](./Server.md#trustProxy)
|
|
27
28
|
- [pluginTimeout](./Server.md#plugintimeout)
|
|
28
|
-
- [querystringParser](./Server.md#
|
|
29
|
+
- [querystringParser](./Server.md#querystringparser)
|
|
29
30
|
- [exposeHeadRoutes](./Server.md#exposeheadroutes)
|
|
30
31
|
- [constraints](./Server.md#constraints)
|
|
31
32
|
- [return503OnClosing](./Server.md#return503onclosing)
|
|
@@ -82,6 +83,17 @@ is in use. Also, when `serverFactory` option is specified, this option is ignore
|
|
|
82
83
|
|
|
83
84
|
+ Default: `5000` (5 seconds)
|
|
84
85
|
|
|
86
|
+
<a name="factory-max-requests-per-socket"></a>
|
|
87
|
+
### `maxRequestsPerSocket`
|
|
88
|
+
|
|
89
|
+
Defines the maximum number of requests socket can handle before closing keep alive connection. See documentation for
|
|
90
|
+
[`server.maxRequestsPerSocket` property](https://nodejs.org/dist/latest/docs/api/http.html#http_server_maxrequestspersocket)
|
|
91
|
+
to understand the effect of this option. This option only applies when HTTP/1.1
|
|
92
|
+
is in use. Also, when `serverFactory` option is specified, this option is ignored.
|
|
93
|
+
> At the time of this writing, only node version greater or equal to 16.10.0 support this option. Check the Node.js documentation for availability in the version you are running.
|
|
94
|
+
|
|
95
|
+
+ Default: `0` (no limit)
|
|
96
|
+
|
|
85
97
|
<a name="factory-ignore-slash"></a>
|
|
86
98
|
### `ignoreTrailingSlash`
|
|
87
99
|
|
|
@@ -298,7 +310,7 @@ fastify.get('/user/:username', (request, reply) => {
|
|
|
298
310
|
Please note that setting this option to `false` goes against
|
|
299
311
|
[RFC3986](https://tools.ietf.org/html/rfc3986#section-6.2.2.1).
|
|
300
312
|
|
|
301
|
-
Also note, this setting will not affect query strings. If you want to change the way query strings are handled take a look at [`querystringParser`](./Server.md#
|
|
313
|
+
Also note, this setting will not affect query strings. If you want to change the way query strings are handled take a look at [`querystringParser`](./Server.md#querystringparser).
|
|
302
314
|
|
|
303
315
|
<a name="factory-request-id-header"></a>
|
|
304
316
|
### `requestIdHeader`
|
package/docs/TypeScript.md
CHANGED
|
@@ -35,6 +35,9 @@ This example will get you up and running with Fastify and TypeScript. It results
|
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
37
|
```
|
|
38
|
+
|
|
39
|
+
*Note: Set `target` property in `tsconfig.json` to `es2017` or greater to avoid [FastifyDeprecation](https://github.com/fastify/fastify/issues/3284) warning.*
|
|
40
|
+
|
|
38
41
|
3. Initialize a TypeScript configuration file:
|
|
39
42
|
```bash
|
|
40
43
|
npx tsc --init
|
|
@@ -257,7 +257,7 @@ curl -X GET "http://localhost:3000/?ids=1
|
|
|
257
257
|
|
|
258
258
|
You can also specify a custom schema validator for each parameter type (body, querystring, params, headers).
|
|
259
259
|
|
|
260
|
-
For example, the following code disable type
|
|
260
|
+
For example, the following code disable type coercion only for the `body` parameters, changing the ajv default options:
|
|
261
261
|
|
|
262
262
|
```js
|
|
263
263
|
const schemaCompilers = {
|
package/fastify.d.ts
CHANGED
|
@@ -15,6 +15,8 @@ import { FastifySchemaValidationError } from './types/schema'
|
|
|
15
15
|
import { ConstructorAction, ProtoAction } from "./types/content-type-parser";
|
|
16
16
|
import { Socket } from 'net'
|
|
17
17
|
import { Options as FJSOptions } from 'fast-json-stringify'
|
|
18
|
+
import { ValidatorCompiler } from '@fastify/ajv-compiler'
|
|
19
|
+
import { FastifySerializerCompiler } from './types/schema';
|
|
18
20
|
|
|
19
21
|
/**
|
|
20
22
|
* Fastify factory function for the standard fastify http, https, or http2 server instance.
|
|
@@ -126,6 +128,17 @@ export type FastifyServerOptions<
|
|
|
126
128
|
constraints?: {
|
|
127
129
|
[name: string]: ConstraintStrategy<FindMyWayVersion<RawServer>, unknown>,
|
|
128
130
|
},
|
|
131
|
+
schemaController?: {
|
|
132
|
+
bucket?: (parentSchemas?: unknown) => {
|
|
133
|
+
addSchema(schema: unknown): FastifyInstance;
|
|
134
|
+
getSchema(schemaId: string): unknown;
|
|
135
|
+
getSchemas(): Record<string, unknown>;
|
|
136
|
+
};
|
|
137
|
+
compilersFactory?: {
|
|
138
|
+
buildValidator?: ValidatorCompiler;
|
|
139
|
+
buildSerializer?: (externalSchemas: unknown, serializerOptsServerOption: FastifyServerOptions["serializerOpts"]) => FastifySerializerCompiler<unknown>;
|
|
140
|
+
};
|
|
141
|
+
};
|
|
129
142
|
return503OnClosing?: boolean,
|
|
130
143
|
ajv?: {
|
|
131
144
|
customOptions?: AjvOptions,
|
package/fastify.js
CHANGED
|
@@ -132,6 +132,7 @@ function fastify (options) {
|
|
|
132
132
|
// Update the options with the fixed values
|
|
133
133
|
options.connectionTimeout = options.connectionTimeout || defaultInitOptions.connectionTimeout
|
|
134
134
|
options.keepAliveTimeout = options.keepAliveTimeout || defaultInitOptions.keepAliveTimeout
|
|
135
|
+
options.maxRequestsPerSocket = options.maxRequestsPerSocket || defaultInitOptions.maxRequestsPerSocket
|
|
135
136
|
options.logger = logger
|
|
136
137
|
options.genReqId = genReqId
|
|
137
138
|
options.requestIdHeader = requestIdHeader
|
|
@@ -420,7 +421,7 @@ function fastify (options) {
|
|
|
420
421
|
// If the server is not ready yet, this
|
|
421
422
|
// utility will automatically force it.
|
|
422
423
|
function inject (opts, cb) {
|
|
423
|
-
// lightMyRequest is dynamically
|
|
424
|
+
// lightMyRequest is dynamically loaded as it seems very expensive
|
|
424
425
|
// because of Ajv
|
|
425
426
|
if (lightMyRequest === undefined) {
|
|
426
427
|
lightMyRequest = require('light-my-request')
|