fastify 3.21.2 → 3.21.3
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/README.md +3 -1
- package/docs/Recommendations.md +1 -1
- package/docs/Reply.md +2 -2
- package/docs/Request.md +4 -4
- package/docs/Server.md +15 -15
- package/lib/decorate.js +2 -0
- package/package.json +1 -1
- package/test/decorator.test.js +27 -0
- package/test/same-shape.test.js +60 -0
package/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
<div align="center">
|
|
2
|
-
<
|
|
2
|
+
<a href="https://fastify.io/">
|
|
3
|
+
<img src="https://github.com/fastify/graphics/raw/HEAD/fastify-landscape-outlined.svg" width="650" height="auto"/>
|
|
4
|
+
</a>
|
|
3
5
|
</div>
|
|
4
6
|
|
|
5
7
|
<div align="center">
|
package/docs/Recommendations.md
CHANGED
package/docs/Reply.md
CHANGED
|
@@ -335,7 +335,7 @@ If you pass to *send* an object that is an instance of *Error*, Fastify will aut
|
|
|
335
335
|
}
|
|
336
336
|
```
|
|
337
337
|
|
|
338
|
-
You can add
|
|
338
|
+
You can add custom properties to the Error object, such as `headers`, that will be used to enhance the HTTP response.<br>
|
|
339
339
|
*Note: If you are passing an error to `send` and the statusCode is less than 400, Fastify will automatically set it at 500.*
|
|
340
340
|
|
|
341
341
|
Tip: you can simplify errors by using the [`http-errors`](https://npm.im/http-errors) module or [`fastify-sensible`](https://github.com/fastify/fastify-sensible) plugin to generate errors:
|
|
@@ -376,7 +376,7 @@ fastify.get('/', {
|
|
|
376
376
|
})
|
|
377
377
|
```
|
|
378
378
|
|
|
379
|
-
If you want to
|
|
379
|
+
If you want to customize error handling, check out [`setErrorHandler`](Server.md#seterrorhandler) API.<br>
|
|
380
380
|
*Note: you are responsible for logging when customizing the error handler*
|
|
381
381
|
|
|
382
382
|
API:
|
package/docs/Request.md
CHANGED
|
@@ -10,25 +10,25 @@ Request is a core Fastify object containing the following fields:
|
|
|
10
10
|
- `raw` - the incoming HTTP request from Node core
|
|
11
11
|
- `req` *(deprecated, use `.raw` instead)* - the incoming HTTP request from Node core
|
|
12
12
|
- `server` - The Fastify server instance, scoped to the current [encapsulation context](Encapsulation.md)
|
|
13
|
-
- `id` - the request
|
|
13
|
+
- `id` - the request ID
|
|
14
14
|
- `log` - the logger instance of the incoming request
|
|
15
15
|
- `ip` - the IP address of the incoming request
|
|
16
16
|
- `ips` - an array of the IP addresses, ordered from closest to furthest, in the `X-Forwarded-For` header of the incoming request (only when the [`trustProxy`](Server.md#factory-trust-proxy) option is enabled)
|
|
17
17
|
- `hostname` - the hostname of the incoming request (derived from `X-Forwarded-Host` header when the [`trustProxy`](Server.md#factory-trust-proxy) option is enabled)
|
|
18
18
|
- `protocol` - the protocol of the incoming request (`https` or `http`)
|
|
19
19
|
- `method` - the method of the incoming request
|
|
20
|
-
- `url` - the
|
|
20
|
+
- `url` - the URL of the incoming request
|
|
21
21
|
- `routerMethod` - the method defined for the router that is handling the request
|
|
22
22
|
- `routerPath` - the path pattern defined for the router that is handling the request
|
|
23
23
|
- `is404` - true if request is being handled by 404 handler, false if it is not
|
|
24
24
|
- `connection` - Deprecated, use `socket` instead. The underlying connection of the incoming request.
|
|
25
25
|
- `socket` - the underlying connection of the incoming request
|
|
26
|
-
- `context` - A Fastify internal object. You should not use it directly or modify it. It is
|
|
26
|
+
- `context` - A Fastify internal object. You should not use it directly or modify it. It is useful to access one special key:
|
|
27
27
|
- `context.config` - The route [`config`](Routes.md#routes-config) object.
|
|
28
28
|
|
|
29
29
|
### Headers
|
|
30
30
|
|
|
31
|
-
The `request.headers` is a getter that
|
|
31
|
+
The `request.headers` is a getter that returns an Object with the headers of the incoming request.
|
|
32
32
|
You can set custom headers like this:
|
|
33
33
|
|
|
34
34
|
```js
|
package/docs/Server.md
CHANGED
|
@@ -110,7 +110,7 @@ fastify.get('/bar', function (req, reply) {
|
|
|
110
110
|
|
|
111
111
|
<a name="factory-max-param-length"></a>
|
|
112
112
|
### `maxParamLength`
|
|
113
|
-
You can set a custom length for parameters in parametric (standard, regex, and multi) routes by using `maxParamLength` option
|
|
113
|
+
You can set a custom length for parameters in parametric (standard, regex, and multi) routes by using `maxParamLength` option; the default value is 100 characters.<br>
|
|
114
114
|
This can be useful especially if you have some regex based route, protecting you against [DoS attacks](https://www.owasp.org/index.php/Regular_expression_Denial_of_Service_-_ReDoS).<br>
|
|
115
115
|
*If the maximum length limit is reached, the not found route will be invoked.*
|
|
116
116
|
|
|
@@ -167,7 +167,7 @@ are not present on the object, they will be added accordingly:
|
|
|
167
167
|
* `level`: the minimum logging level. If not set, it will be set to `'info'`.
|
|
168
168
|
* `serializers`: a hash of serialization functions. By default, serializers
|
|
169
169
|
are added for `req` (incoming request objects), `res` (outgoing response
|
|
170
|
-
|
|
170
|
+
objects), and `err` (standard `Error` objects). When a log method receives
|
|
171
171
|
an object with any of these properties then the respective serializer will
|
|
172
172
|
be used for that property. For example:
|
|
173
173
|
```js
|
|
@@ -228,7 +228,7 @@ Please note that this setting will also disable an error log written by the defa
|
|
|
228
228
|
<a name="custom-http-server"></a>
|
|
229
229
|
### `serverFactory`
|
|
230
230
|
You can pass a custom HTTP server to Fastify by using the `serverFactory` option.<br/>
|
|
231
|
-
`serverFactory` is a function that takes
|
|
231
|
+
`serverFactory` is a function that takes a `handler` parameter, which takes the `request` and `response` objects as parameters, and an options object, which is the same you have passed to Fastify.
|
|
232
232
|
|
|
233
233
|
```js
|
|
234
234
|
const serverFactory = (handler, opts) => {
|
|
@@ -255,7 +255,7 @@ Internally Fastify uses the API of Node core HTTP server, so if you are using a
|
|
|
255
255
|
|
|
256
256
|
+ Default: `true`
|
|
257
257
|
|
|
258
|
-
Internally, and by default, Fastify will automatically infer the root properties of JSON Schemas if it
|
|
258
|
+
Internally, and by default, Fastify will automatically infer the root properties of JSON Schemas if it does not find valid root properties according to the JSON Schema spec. If you wish to implement your own schema validation compiler, for example: to parse schemas as JTD instead of JSON Schema, then you can explicitly set this option to `false` to make sure the schemas you receive are unmodified and are not being treated internally as JSON Schema.
|
|
259
259
|
|
|
260
260
|
```js
|
|
261
261
|
const AjvJTD = require('ajv/dist/jtd'/* only valid for AJV v7+ */)
|
|
@@ -333,7 +333,7 @@ const fastify = require('fastify')({
|
|
|
333
333
|
<a name="factory-trust-proxy"></a>
|
|
334
334
|
### `trustProxy`
|
|
335
335
|
|
|
336
|
-
By enabling the `trustProxy` option, Fastify will
|
|
336
|
+
By enabling the `trustProxy` option, Fastify will know that it is sitting behind a proxy and that the `X-Forwarded-*` header fields may be trusted, which otherwise may be easily spoofed.
|
|
337
337
|
|
|
338
338
|
```js
|
|
339
339
|
const fastify = Fastify({ trustProxy: true })
|
|
@@ -497,7 +497,7 @@ increased to fit the use case. Node core defaults this to `0`. `
|
|
|
497
497
|
+ Default: `null`
|
|
498
498
|
|
|
499
499
|
Fastify provides default error handlers for the most common use cases.
|
|
500
|
-
|
|
500
|
+
It is possible to override one or more of those handlers with custom code using this option.
|
|
501
501
|
|
|
502
502
|
*Note: Only `FST_ERR_BAD_URL` is implemented at the moment.*
|
|
503
503
|
|
|
@@ -519,7 +519,7 @@ const fastify = require('fastify')({
|
|
|
519
519
|
|
|
520
520
|
Set a [clientErrorHandler](https://nodejs.org/api/http.html#http_event_clienterror) that listens to `error` events emitted by client connections and responds with a `400`.
|
|
521
521
|
|
|
522
|
-
|
|
522
|
+
It is possible to override the default `clientErrorHandler` using this option.
|
|
523
523
|
|
|
524
524
|
+ Default:
|
|
525
525
|
```js
|
|
@@ -649,7 +649,7 @@ fastify.ready().then(() => {
|
|
|
649
649
|
|
|
650
650
|
<a name="listen"></a>
|
|
651
651
|
#### listen
|
|
652
|
-
Starts the server on the given port after all the plugins are loaded, internally waits for the `.ready()` event. The callback is the same as the Node core. By default, the server will listen on the address resolved by `localhost` when no specific address is provided (`127.0.0.1` or `::1` depending on the operating system). If listening on any available interface is desired, then specifying `0.0.0.0` for the address will listen on all IPv4
|
|
652
|
+
Starts the server on the given port after all the plugins are loaded, internally waits for the `.ready()` event. The callback is the same as the Node core. By default, the server will listen on the address resolved by `localhost` when no specific address is provided (`127.0.0.1` or `::1` depending on the operating system). If listening on any available interface is desired, then specifying `0.0.0.0` for the address will listen on all IPv4 addresses. Using `::` for the address will listen on all IPv6 addresses and, depending on OS, may also listen on all IPv4 addresses. Be careful when deciding to listen on all interfaces; it comes with inherent [security risks](https://web.archive.org/web/20170831174611/https://snyk.io/blog/mongodb-hack-and-secure-defaults/).
|
|
653
653
|
|
|
654
654
|
```js
|
|
655
655
|
fastify.listen(3000, (err, address) => {
|
|
@@ -962,15 +962,15 @@ const fastify = Fastify({
|
|
|
962
962
|
},
|
|
963
963
|
|
|
964
964
|
/**
|
|
965
|
-
* The compilers factory let you
|
|
965
|
+
* The compilers factory let you fully control the validator and serializer
|
|
966
966
|
* in the Fastify's lifecycle, providing the encapsulation to your compilers.
|
|
967
967
|
*/
|
|
968
968
|
compilersFactory: {
|
|
969
969
|
/**
|
|
970
970
|
* This factory is called whenever a new validator instance is needed.
|
|
971
|
-
* It may be called whenever `fastify.register()` is called only if new schemas
|
|
971
|
+
* It may be called whenever `fastify.register()` is called only if new schemas have been added to the
|
|
972
972
|
* encapsulation context.
|
|
973
|
-
* It may receive as input the schemas of the parent context if some schemas
|
|
973
|
+
* It may receive as input the schemas of the parent context if some schemas have been added.
|
|
974
974
|
* @param {object} externalSchemas these schemas will be returned by the `bucket.getSchemas()`. Needed to resolve the external references $ref.
|
|
975
975
|
* @param {object} ajvServerOption the server `ajv` options to build your compilers accordingly
|
|
976
976
|
*/
|
|
@@ -985,9 +985,9 @@ const fastify = Fastify({
|
|
|
985
985
|
|
|
986
986
|
/**
|
|
987
987
|
* This factory is called whenever a new serializer instance is needed.
|
|
988
|
-
* It may be called whenever `fastify.register()` is called only if new schemas
|
|
988
|
+
* It may be called whenever `fastify.register()` is called only if new schemas have been added to the
|
|
989
989
|
* encapsulation context.
|
|
990
|
-
* It may receive as input the schemas of the parent context if some schemas
|
|
990
|
+
* It may receive as input the schemas of the parent context if some schemas have been added.
|
|
991
991
|
* @param {object} externalSchemas these schemas will be returned by the `bucket.getSchemas()`. Needed to resolve the external references $ref.
|
|
992
992
|
* @param {object} serializerOptsServerOption the server `serializerOpts` options to build your compilers accordingly
|
|
993
993
|
*/
|
|
@@ -1006,7 +1006,7 @@ const fastify = Fastify({
|
|
|
1006
1006
|
##### Ajv 8 as default schema validator
|
|
1007
1007
|
|
|
1008
1008
|
Ajv 8 is the evolution of Ajv 6, and it has a lot of improvements and new features.
|
|
1009
|
-
To use the new Ajv 8 features such as JTD or the Standalone mode,
|
|
1009
|
+
To use the new Ajv 8 features such as JTD or the Standalone mode, refer to the [`@fastify/ajv-compiler` documentation](https://github.com/fastify/ajv-compiler#usage).
|
|
1010
1010
|
|
|
1011
1011
|
To use Ajv 8 as default schema validator, you can use the following code:
|
|
1012
1012
|
|
|
@@ -1031,7 +1031,7 @@ const app = fastify({
|
|
|
1031
1031
|
}
|
|
1032
1032
|
})
|
|
1033
1033
|
|
|
1034
|
-
// Done! You can now use Ajv 8 options and keywords
|
|
1034
|
+
// Done! You can now use Ajv 8 options and keywords in your schemas!
|
|
1035
1035
|
```
|
|
1036
1036
|
|
|
1037
1037
|
<a name="set-not-found-handler"></a>
|
package/lib/decorate.js
CHANGED
|
@@ -75,10 +75,12 @@ function checkExistence (instance, name) {
|
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
function checkRequestExistence (name) {
|
|
78
|
+
if (name && this[kRequest].props.includes(name)) return true
|
|
78
79
|
return checkExistence(this[kRequest].prototype, name)
|
|
79
80
|
}
|
|
80
81
|
|
|
81
82
|
function checkReplyExistence (name) {
|
|
83
|
+
if (name && this[kReply].props.includes(name)) return true
|
|
82
84
|
return checkExistence(this[kReply].prototype, name)
|
|
83
85
|
}
|
|
84
86
|
|
package/package.json
CHANGED
package/test/decorator.test.js
CHANGED
|
@@ -17,6 +17,15 @@ test('server methods should exist', t => {
|
|
|
17
17
|
t.ok(fastify.hasDecorator)
|
|
18
18
|
})
|
|
19
19
|
|
|
20
|
+
test('should check if the given decoration already exist when null', t => {
|
|
21
|
+
t.plan(1)
|
|
22
|
+
const fastify = Fastify()
|
|
23
|
+
fastify.decorate('null', null)
|
|
24
|
+
fastify.ready(() => {
|
|
25
|
+
t.ok(fastify.hasDecorator('null'))
|
|
26
|
+
})
|
|
27
|
+
})
|
|
28
|
+
|
|
20
29
|
test('server methods should be encapsulated via .register', t => {
|
|
21
30
|
t.plan(2)
|
|
22
31
|
const fastify = Fastify()
|
|
@@ -425,6 +434,15 @@ test('hasRequestDecorator', t => {
|
|
|
425
434
|
t.ok(fastify.hasRequestDecorator(requestDecoratorName))
|
|
426
435
|
})
|
|
427
436
|
|
|
437
|
+
t.test('should check if the given request decoration already exist when null', t => {
|
|
438
|
+
t.plan(2)
|
|
439
|
+
const fastify = Fastify()
|
|
440
|
+
|
|
441
|
+
t.notOk(fastify.hasRequestDecorator(requestDecoratorName))
|
|
442
|
+
fastify.decorateRequest(requestDecoratorName, null)
|
|
443
|
+
t.ok(fastify.hasRequestDecorator(requestDecoratorName))
|
|
444
|
+
})
|
|
445
|
+
|
|
428
446
|
t.test('should be plugin encapsulable', t => {
|
|
429
447
|
t.plan(4)
|
|
430
448
|
const fastify = Fastify()
|
|
@@ -481,6 +499,15 @@ test('hasReplyDecorator', t => {
|
|
|
481
499
|
t.ok(fastify.hasReplyDecorator(replyDecoratorName))
|
|
482
500
|
})
|
|
483
501
|
|
|
502
|
+
t.test('should check if the given reply decoration already exist when null', t => {
|
|
503
|
+
t.plan(2)
|
|
504
|
+
const fastify = Fastify()
|
|
505
|
+
|
|
506
|
+
t.notOk(fastify.hasReplyDecorator(replyDecoratorName))
|
|
507
|
+
fastify.decorateReply(replyDecoratorName, null)
|
|
508
|
+
t.ok(fastify.hasReplyDecorator(replyDecoratorName))
|
|
509
|
+
})
|
|
510
|
+
|
|
484
511
|
t.test('should be plugin encapsulable', t => {
|
|
485
512
|
t.plan(4)
|
|
486
513
|
const fastify = Fastify()
|
package/test/same-shape.test.js
CHANGED
|
@@ -33,6 +33,36 @@ test('same shape on Request', async (t) => {
|
|
|
33
33
|
await app.inject('/')
|
|
34
34
|
})
|
|
35
35
|
|
|
36
|
+
test('same shape on Request when object', async (t) => {
|
|
37
|
+
t.plan(1)
|
|
38
|
+
|
|
39
|
+
const app = fastify()
|
|
40
|
+
|
|
41
|
+
let request
|
|
42
|
+
|
|
43
|
+
app.decorateRequest('object', null)
|
|
44
|
+
|
|
45
|
+
app.addHook('preHandler', (req, reply, done) => {
|
|
46
|
+
if (request) {
|
|
47
|
+
req.object = {}
|
|
48
|
+
}
|
|
49
|
+
done()
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
app.get('/', (req, reply) => {
|
|
53
|
+
if (request) {
|
|
54
|
+
t.equal(%HaveSameMap(request, req), true)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
request = req
|
|
58
|
+
|
|
59
|
+
return 'hello world'
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
await app.inject('/')
|
|
63
|
+
await app.inject('/')
|
|
64
|
+
})
|
|
65
|
+
|
|
36
66
|
test('same shape on Reply', async (t) => {
|
|
37
67
|
t.plan(1)
|
|
38
68
|
|
|
@@ -62,3 +92,33 @@ test('same shape on Reply', async (t) => {
|
|
|
62
92
|
await app.inject('/')
|
|
63
93
|
await app.inject('/')
|
|
64
94
|
})
|
|
95
|
+
|
|
96
|
+
test('same shape on Reply when object', async (t) => {
|
|
97
|
+
t.plan(1)
|
|
98
|
+
|
|
99
|
+
const app = fastify()
|
|
100
|
+
|
|
101
|
+
let _reply
|
|
102
|
+
|
|
103
|
+
app.decorateReply('object', null)
|
|
104
|
+
|
|
105
|
+
app.addHook('preHandler', (req, reply, done) => {
|
|
106
|
+
if (_reply) {
|
|
107
|
+
reply.object = {}
|
|
108
|
+
}
|
|
109
|
+
done()
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
app.get('/', (req, reply) => {
|
|
113
|
+
if (_reply) {
|
|
114
|
+
t.equal(%HaveSameMap(_reply, reply), true)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
_reply = reply
|
|
118
|
+
|
|
119
|
+
return 'hello world'
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
await app.inject('/')
|
|
123
|
+
await app.inject('/')
|
|
124
|
+
})
|