fastify 5.1.0 → 5.2.0
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 +12 -7
- package/docs/Guides/Database.md +15 -15
- package/docs/Guides/Detecting-When-Clients-Abort.md +28 -28
- package/docs/Guides/Ecosystem.md +13 -7
- package/docs/Guides/Index.md +1 -1
- package/docs/Guides/Migration-Guide-V4.md +11 -11
- package/docs/Guides/Migration-Guide-V5.md +5 -5
- package/docs/Guides/Plugins-Guide.md +1 -1
- package/docs/Guides/Prototype-Poisoning.md +3 -3
- package/docs/Guides/Recommendations.md +9 -9
- package/docs/Guides/Serverless.md +5 -5
- package/docs/Guides/Testing.md +8 -8
- package/docs/Guides/Write-Plugin.md +1 -1
- package/docs/Guides/Write-Type-Provider.md +3 -3
- package/docs/Reference/Decorators.md +2 -2
- package/docs/Reference/Errors.md +2 -2
- package/docs/Reference/Hooks.md +7 -7
- package/docs/Reference/LTS.md +8 -0
- package/docs/Reference/Logging.md +5 -4
- package/docs/Reference/Reply.md +54 -53
- package/docs/Reference/Request.md +49 -43
- package/docs/Reference/Routes.md +7 -7
- package/docs/Reference/Server.md +31 -27
- package/docs/Reference/TypeScript.md +9 -9
- package/docs/Reference/Validation-and-Serialization.md +5 -5
- package/examples/typescript-server.ts +1 -1
- package/fastify.d.ts +4 -4
- package/fastify.js +1 -1
- package/lib/error-handler.js +9 -9
- package/lib/errors.js +1 -1
- package/lib/hooks.js +4 -1
- package/lib/request.js +11 -10
- package/package.json +3 -4
- package/test/{allowUnsafeRegex.test.js → allow-unsafe-regex.test.js} +4 -4
- package/test/async-dispose.test.js +0 -1
- package/test/async_hooks.test.js +4 -4
- package/test/buffer.test.js +4 -4
- package/test/build-certificate.js +1 -1
- package/test/bundler/README.md +5 -5
- package/test/case-insensitive.test.js +10 -10
- package/test/check.test.js +2 -3
- package/test/{childLoggerFactory.test.js → child-logger-factory.test.js} +1 -1
- package/test/client-timeout.test.js +1 -1
- package/test/close-pipelining.test.js +0 -1
- package/test/conditional-pino.test.js +3 -3
- package/test/content-length.test.js +53 -68
- package/test/content-parser.test.js +178 -167
- package/test/content-type.test.js +8 -9
- package/test/context-config.test.js +44 -54
- package/test/custom-parser.5.test.js +32 -32
- package/test/encapsulated-child-logger-factory.test.js +8 -8
- package/test/encapsulated-error-handler.test.js +20 -20
- package/test/fastify-instance.test.js +33 -34
- package/test/{findRoute.test.js → find-route.test.js} +11 -10
- package/test/fluent-schema.test.js +33 -36
- package/test/handler-context.test.js +11 -11
- package/test/has-route.test.js +12 -15
- package/test/header-overflow.test.js +13 -12
- package/test/hooks.on-ready.test.js +2 -2
- package/test/hooks.test.js +19 -19
- package/test/http-methods/head.test.js +0 -3
- package/test/imports.test.js +2 -2
- package/test/internals/errors.test.js +1 -1
- package/test/listen.5.test.js +9 -9
- package/test/{maxRequestsPerSocket.test.js → max-requests-per-socket.test.js} +30 -30
- package/test/middleware.test.js +4 -5
- package/test/noop-set.test.js +1 -1
- package/test/post-empty-body.test.js +18 -11
- package/test/pretty-print.test.js +59 -49
- package/test/proto-poisoning.test.js +42 -37
- package/test/reply-code.test.js +34 -32
- package/test/{reply-earlyHints.test.js → reply-early-hints.test.js} +21 -19
- package/test/request-header-host.test.js +154 -12
- package/test/request-id.test.js +31 -25
- package/test/{requestTimeout.test.js → request-timeout.test.js} +11 -11
- package/test/route.3.test.js +3 -2
- package/test/route.8.test.js +20 -20
- package/test/router-options.test.js +80 -77
- package/test/schema-examples.test.js +72 -38
- package/test/server.test.js +12 -12
- package/test/set-error-handler.test.js +2 -3
- package/test/stream-serializers.test.js +10 -7
- package/test/sync-routes.test.js +18 -18
- package/test/trust-proxy.test.js +51 -45
- package/test/type-provider.test.js +8 -6
- package/test/types/content-type-parser.test-d.ts +1 -1
- package/test/types/fastify.test-d.ts +4 -4
- package/test/types/instance.test-d.ts +3 -1
- package/test/types/logger.test-d.ts +2 -2
- package/test/types/plugin.test-d.ts +2 -2
- package/test/types/register.test-d.ts +2 -2
- package/test/types/reply.test-d.ts +1 -1
- package/test/types/route.test-d.ts +1 -1
- package/test/types/serverFactory.test-d.ts +1 -1
- package/test/types/type-provider.test-d.ts +1 -1
- package/test/url-rewriting.test.js +35 -38
- package/test/{useSemicolonDelimiter.test.js → use-semicolon-delimiter.test.js} +30 -30
- package/test/validation-error-handling.test.js +259 -285
- package/types/hooks.d.ts +1 -1
- package/types/instance.d.ts +9 -2
- package/types/reply.d.ts +1 -1
- package/types/request.d.ts +0 -4
- package/types/serverFactory.d.ts +3 -3
- package/types/utils.d.ts +3 -3
- /package/test/{connectionTimeout.test.js → connection-timeout.test.js} +0 -0
- /package/test/internals/{contentTypeParser.test.js → content-type-parser.test.js} +0 -0
- /package/test/internals/{handleRequest.test.js → handle-request.test.js} +0 -0
- /package/test/internals/{hookRunner.test.js → hook-runner.test.js} +0 -0
- /package/test/internals/{initialConfig.test.js → initial-config.test.js} +0 -0
- /package/test/internals/{reqIdGenFactory.test.js → req-id-gen-factory.test.js} +0 -0
- /package/test/{wrapThenable.test.js → wrap-thenable.test.js} +0 -0
package/docs/Reference/Errors.md
CHANGED
|
@@ -178,13 +178,13 @@ When utilizing Fastify's custom error handling through [`setErrorHandler`](./Ser
|
|
|
178
178
|
you should be aware of how errors are propagated between custom and default
|
|
179
179
|
error handlers.
|
|
180
180
|
|
|
181
|
-
If a plugin's error handler re-throws an error, and the error is not an
|
|
181
|
+
If a plugin's error handler re-throws an error, and the error is not an
|
|
182
182
|
instance of [Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)
|
|
183
183
|
(as seen in the `/bad` route in the following example), it will not propagate
|
|
184
184
|
to the parent context error handler. Instead, it will be caught by the default
|
|
185
185
|
error handler.
|
|
186
186
|
|
|
187
|
-
To ensure consistent error handling, it is recommended to throw instances of
|
|
187
|
+
To ensure consistent error handling, it is recommended to throw instances of
|
|
188
188
|
`Error`. For instance, in the following example, replacing `throw 'foo'` with
|
|
189
189
|
`throw new Error('foo')` in the `/bad` route ensures that errors propagate through
|
|
190
190
|
the custom error handling chain as intended. This practice helps avoid potential
|
package/docs/Reference/Hooks.md
CHANGED
|
@@ -107,7 +107,7 @@ returned stream. This property is used to correctly match the request payload
|
|
|
107
107
|
with the `Content-Length` header value. Ideally, this property should be updated
|
|
108
108
|
on each received chunk.
|
|
109
109
|
|
|
110
|
-
**Notice:** The size of the returned stream is checked to not exceed the limit
|
|
110
|
+
**Notice:** The size of the returned stream is checked to not exceed the limit
|
|
111
111
|
set in [`bodyLimit`](./Server.md#bodylimit) option.
|
|
112
112
|
|
|
113
113
|
### preValidation
|
|
@@ -256,8 +256,8 @@ The `onResponse` hook is executed when a response has been sent, so you will not
|
|
|
256
256
|
be able to send more data to the client. It can however be useful for sending
|
|
257
257
|
data to external services, for example, to gather statistics.
|
|
258
258
|
|
|
259
|
-
**Note:** setting `disableRequestLogging` to `true` will disable any error log
|
|
260
|
-
inside the `onResponse` hook. In this case use `try - catch` to log errors.
|
|
259
|
+
**Note:** setting `disableRequestLogging` to `true` will disable any error log
|
|
260
|
+
inside the `onResponse` hook. In this case use `try - catch` to log errors.
|
|
261
261
|
|
|
262
262
|
### onTimeout
|
|
263
263
|
|
|
@@ -428,8 +428,8 @@ fastify.addHook('onReady', async function () {
|
|
|
428
428
|
|
|
429
429
|
### onListen
|
|
430
430
|
|
|
431
|
-
Triggered when the server starts listening for requests. The hooks run one
|
|
432
|
-
after another. If a hook function causes an error, it is logged and
|
|
431
|
+
Triggered when the server starts listening for requests. The hooks run one
|
|
432
|
+
after another. If a hook function causes an error, it is logged and
|
|
433
433
|
ignored, allowing the queue of hooks to continue. Hook functions accept one
|
|
434
434
|
argument: a callback, `done`, to be invoked after the hook function is
|
|
435
435
|
complete. Hook functions are invoked with `this` bound to the associated
|
|
@@ -451,7 +451,7 @@ fastify.addHook('onListen', async function () {
|
|
|
451
451
|
})
|
|
452
452
|
```
|
|
453
453
|
|
|
454
|
-
> **Note**
|
|
454
|
+
> **Note**
|
|
455
455
|
> This hook will not run when the server is started using `fastify.inject()` or `fastify.ready()`
|
|
456
456
|
|
|
457
457
|
### onClose
|
|
@@ -462,7 +462,7 @@ HTTP requests have been completed.
|
|
|
462
462
|
It is useful when [plugins](./Plugins.md) need a "shutdown" event, for example,
|
|
463
463
|
to close an open connection to a database.
|
|
464
464
|
|
|
465
|
-
The hook function takes the Fastify instance as a first argument,
|
|
465
|
+
The hook function takes the Fastify instance as a first argument,
|
|
466
466
|
and a `done` callback for synchronous hook functions.
|
|
467
467
|
```js
|
|
468
468
|
// callback style
|
package/docs/Reference/LTS.md
CHANGED
|
@@ -48,6 +48,12 @@ A "month" is defined as 30 consecutive days.
|
|
|
48
48
|
> dependency as `"fastify": "~3.15.x"`. This will leave your application
|
|
49
49
|
> vulnerable, so please use with caution.
|
|
50
50
|
|
|
51
|
+
### Security Support Beyond LTS
|
|
52
|
+
|
|
53
|
+
Fastify's partner, HeroDevs, provides commercial security support through the
|
|
54
|
+
OpenJS Ecosystem Sustainability Program for versions of Fastify that are EOL.
|
|
55
|
+
For more information, see their [Never Ending Support][hd-link] service.
|
|
56
|
+
|
|
51
57
|
### Schedule
|
|
52
58
|
|
|
53
59
|
`<a id="lts-schedule"></a>`
|
|
@@ -81,3 +87,5 @@ Using [yarn](https://yarnpkg.com/) might require passing the `--ignore-engines`
|
|
|
81
87
|
flag.
|
|
82
88
|
|
|
83
89
|
[semver]: https://semver.org/
|
|
90
|
+
|
|
91
|
+
[hd-link]: https://www.herodevs.com/support/fastify-nes?utm_source=fastify&utm_medium=link&utm_campaign=eol_support_fastify
|
|
@@ -204,15 +204,16 @@ on serializers for more information.
|
|
|
204
204
|
*Any logger other than Pino will ignore this option.*
|
|
205
205
|
|
|
206
206
|
You can also supply your own logger instance. Instead of passing configuration
|
|
207
|
-
options, pass the instance
|
|
208
|
-
interface; that is, it must have the following methods:
|
|
209
|
-
`debug`, `fatal`, `warn`, `trace`, `silent`, `child` and a
|
|
207
|
+
options, pass the instance as `loggerInstance`. The logger you supply must
|
|
208
|
+
conform to the Pino interface; that is, it must have the following methods:
|
|
209
|
+
`info`, `error`, `debug`, `fatal`, `warn`, `trace`, `silent`, `child` and a
|
|
210
|
+
string property `level`.
|
|
210
211
|
|
|
211
212
|
Example:
|
|
212
213
|
|
|
213
214
|
```js
|
|
214
215
|
const log = require('pino')({ level: 'info' })
|
|
215
|
-
const fastify = require('fastify')({
|
|
216
|
+
const fastify = require('fastify')({ loggerInstance: log })
|
|
216
217
|
|
|
217
218
|
log.info('does not have request information')
|
|
218
219
|
|
package/docs/Reference/Reply.md
CHANGED
|
@@ -11,9 +11,9 @@
|
|
|
11
11
|
- [.headers(object)](#headersobject)
|
|
12
12
|
- [.getHeader(key)](#getheaderkey)
|
|
13
13
|
- [.getHeaders()](#getheaders)
|
|
14
|
-
- [set-cookie](#set-cookie)
|
|
15
14
|
- [.removeHeader(key)](#removeheaderkey)
|
|
16
15
|
- [.hasHeader(key)](#hasheaderkey)
|
|
16
|
+
- [.writeEarlyHints(hints, callback)](#writeearlyhintshints-callback)
|
|
17
17
|
- [.trailer(key, function)](#trailerkey-function)
|
|
18
18
|
- [.hasTrailer(key)](#hastrailerkey)
|
|
19
19
|
- [.removeTrailer(key)](#removetrailerkey)
|
|
@@ -32,8 +32,9 @@
|
|
|
32
32
|
- [Strings](#strings)
|
|
33
33
|
- [Streams](#streams)
|
|
34
34
|
- [Buffers](#buffers)
|
|
35
|
-
- [
|
|
36
|
-
- [
|
|
35
|
+
- [TypedArrays](#typedarrays)
|
|
36
|
+
- [ReadableStream](#readablestream)
|
|
37
|
+
- [Response](#response)
|
|
37
38
|
- [Errors](#errors)
|
|
38
39
|
- [Type of the final payload](#type-of-the-final-payload)
|
|
39
40
|
- [Async-Await and Promises](#async-await-and-promises)
|
|
@@ -71,14 +72,14 @@ since the request was received by Fastify.
|
|
|
71
72
|
serialized payload.
|
|
72
73
|
- `.getSerializationFunction(schema | httpStatus, [contentType])` - Returns the serialization
|
|
73
74
|
function for the specified schema or http status, if any of either are set.
|
|
74
|
-
- `.compileSerializationSchema(schema, [httpStatus], [contentType])` - Compiles
|
|
75
|
-
the specified schema and returns a serialization function using the default
|
|
76
|
-
(or customized) `SerializerCompiler`. The optional `httpStatus` is forwarded
|
|
75
|
+
- `.compileSerializationSchema(schema, [httpStatus], [contentType])` - Compiles
|
|
76
|
+
the specified schema and returns a serialization function using the default
|
|
77
|
+
(or customized) `SerializerCompiler`. The optional `httpStatus` is forwarded
|
|
77
78
|
to the `SerializerCompiler` if provided, default to `undefined`.
|
|
78
|
-
- `.serializeInput(data, schema, [,httpStatus], [contentType])` - Serializes
|
|
79
|
+
- `.serializeInput(data, schema, [,httpStatus], [contentType])` - Serializes
|
|
79
80
|
the specified data using the specified schema and returns the serialized payload.
|
|
80
|
-
If the optional `httpStatus`, and `contentType` are provided, the function
|
|
81
|
-
will use the serializer function given for that specific content type and
|
|
81
|
+
If the optional `httpStatus`, and `contentType` are provided, the function
|
|
82
|
+
will use the serializer function given for that specific content type and
|
|
82
83
|
HTTP Status Code. Default to `undefined`.
|
|
83
84
|
- `.serializer(function)` - Sets a custom serializer for the payload.
|
|
84
85
|
- `.send(payload)` - Sends the payload to the user, could be a plain text, a
|
|
@@ -87,7 +88,7 @@ since the request was received by Fastify.
|
|
|
87
88
|
already been called.
|
|
88
89
|
- `.hijack()` - interrupt the normal request lifecycle.
|
|
89
90
|
- `.raw` - The
|
|
90
|
-
[`http.ServerResponse`](https://nodejs.org/dist/latest-
|
|
91
|
+
[`http.ServerResponse`](https://nodejs.org/dist/latest-v20.x/docs/api/http.html#http_class_http_serverresponse)
|
|
91
92
|
from Node core.
|
|
92
93
|
- `.log` - The logger instance of the incoming request.
|
|
93
94
|
- `.request` - The incoming request.
|
|
@@ -157,7 +158,7 @@ Sets a response header. If the value is omitted or undefined, it is coerced to
|
|
|
157
158
|
> will result in a 500 `TypeError` response.
|
|
158
159
|
|
|
159
160
|
For more information, see
|
|
160
|
-
[`http.ServerResponse#setHeader`](https://nodejs.org/dist/latest-
|
|
161
|
+
[`http.ServerResponse#setHeader`](https://nodejs.org/dist/latest-v20.x/docs/api/http.html#http_response_setheader_name_value).
|
|
161
162
|
|
|
162
163
|
- ### set-cookie
|
|
163
164
|
<a id="set-cookie"></a>
|
|
@@ -243,7 +244,7 @@ The hints parameter is an object containing the early hint key-value pairs.
|
|
|
243
244
|
Example:
|
|
244
245
|
|
|
245
246
|
```js
|
|
246
|
-
reply.writeEarlyHints({
|
|
247
|
+
reply.writeEarlyHints({
|
|
247
248
|
Link: '</styles.css>; rel=preload; as=style'
|
|
248
249
|
});
|
|
249
250
|
```
|
|
@@ -366,8 +367,8 @@ If the `Content-Type` has a JSON subtype, and the charset parameter is not set,
|
|
|
366
367
|
### .getSerializationFunction(schema | httpStatus, [contentType])
|
|
367
368
|
<a id="getserializationfunction"></a>
|
|
368
369
|
|
|
369
|
-
By calling this function using a provided `schema` or `httpStatus`,
|
|
370
|
-
and the optional `contentType`, it will return a `serialzation` function
|
|
370
|
+
By calling this function using a provided `schema` or `httpStatus`,
|
|
371
|
+
and the optional `contentType`, it will return a `serialzation` function
|
|
371
372
|
that can be used to serialize diverse inputs. It returns `undefined` if no
|
|
372
373
|
serialization function was found using either of the provided inputs.
|
|
373
374
|
|
|
@@ -377,12 +378,12 @@ the serialization functions compiled by using `compileSerializationSchema`.
|
|
|
377
378
|
```js
|
|
378
379
|
const serialize = reply
|
|
379
380
|
.getSerializationFunction({
|
|
380
|
-
type: 'object',
|
|
381
|
-
properties: {
|
|
382
|
-
foo: {
|
|
383
|
-
type: 'string'
|
|
384
|
-
}
|
|
385
|
-
}
|
|
381
|
+
type: 'object',
|
|
382
|
+
properties: {
|
|
383
|
+
foo: {
|
|
384
|
+
type: 'string'
|
|
385
|
+
}
|
|
386
|
+
}
|
|
386
387
|
})
|
|
387
388
|
serialize({ foo: 'bar' }) // '{"foo":"bar"}'
|
|
388
389
|
|
|
@@ -411,8 +412,8 @@ The function returned (a.k.a. _serialization function_) returned is compiled
|
|
|
411
412
|
by using the provided `SerializerCompiler`. Also this is cached by using
|
|
412
413
|
a `WeakMap` for reducing compilation calls.
|
|
413
414
|
|
|
414
|
-
The optional parameters `httpStatus` and `contentType`, if provided,
|
|
415
|
-
are forwarded directly to the `SerializerCompiler`, so it can be used
|
|
415
|
+
The optional parameters `httpStatus` and `contentType`, if provided,
|
|
416
|
+
are forwarded directly to the `SerializerCompiler`, so it can be used
|
|
416
417
|
to compile the serialization function if a custom `SerializerCompiler` is used.
|
|
417
418
|
|
|
418
419
|
This heavily depends of the `schema#responses` attached to the route, or
|
|
@@ -421,12 +422,12 @@ the serialization functions compiled by using `compileSerializationSchema`.
|
|
|
421
422
|
```js
|
|
422
423
|
const serialize = reply
|
|
423
424
|
.compileSerializationSchema({
|
|
424
|
-
type: 'object',
|
|
425
|
-
properties: {
|
|
426
|
-
foo: {
|
|
427
|
-
type: 'string'
|
|
428
|
-
}
|
|
429
|
-
}
|
|
425
|
+
type: 'object',
|
|
426
|
+
properties: {
|
|
427
|
+
foo: {
|
|
428
|
+
type: 'string'
|
|
429
|
+
}
|
|
430
|
+
}
|
|
430
431
|
})
|
|
431
432
|
serialize({ foo: 'bar' }) // '{"foo":"bar"}'
|
|
432
433
|
|
|
@@ -434,12 +435,12 @@ serialize({ foo: 'bar' }) // '{"foo":"bar"}'
|
|
|
434
435
|
|
|
435
436
|
const serialize = reply
|
|
436
437
|
.compileSerializationSchema({
|
|
437
|
-
type: 'object',
|
|
438
|
-
properties: {
|
|
439
|
-
foo: {
|
|
440
|
-
type: 'string'
|
|
441
|
-
}
|
|
442
|
-
}
|
|
438
|
+
type: 'object',
|
|
439
|
+
properties: {
|
|
440
|
+
foo: {
|
|
441
|
+
type: 'string'
|
|
442
|
+
}
|
|
443
|
+
}
|
|
443
444
|
}, 200)
|
|
444
445
|
serialize({ foo: 'bar' }) // '{"foo":"bar"}'
|
|
445
446
|
|
|
@@ -485,7 +486,7 @@ const schema1 = {
|
|
|
485
486
|
```
|
|
486
487
|
|
|
487
488
|
*Not*
|
|
488
|
-
```js
|
|
489
|
+
```js
|
|
489
490
|
const serialize = reply.compileSerializationSchema(schema1)
|
|
490
491
|
|
|
491
492
|
// Later on...
|
|
@@ -519,25 +520,25 @@ function will be compiled, forwarding the `httpStatus` and `contentType` if prov
|
|
|
519
520
|
|
|
520
521
|
```js
|
|
521
522
|
reply
|
|
522
|
-
.serializeInput({ foo: 'bar'}, {
|
|
523
|
-
type: 'object',
|
|
524
|
-
properties: {
|
|
525
|
-
foo: {
|
|
526
|
-
type: 'string'
|
|
527
|
-
}
|
|
528
|
-
}
|
|
523
|
+
.serializeInput({ foo: 'bar'}, {
|
|
524
|
+
type: 'object',
|
|
525
|
+
properties: {
|
|
526
|
+
foo: {
|
|
527
|
+
type: 'string'
|
|
528
|
+
}
|
|
529
|
+
}
|
|
529
530
|
}) // '{"foo":"bar"}'
|
|
530
531
|
|
|
531
532
|
// or
|
|
532
533
|
|
|
533
534
|
reply
|
|
534
535
|
.serializeInput({ foo: 'bar'}, {
|
|
535
|
-
type: 'object',
|
|
536
|
-
properties: {
|
|
537
|
-
foo: {
|
|
538
|
-
type: 'string'
|
|
539
|
-
}
|
|
540
|
-
}
|
|
536
|
+
type: 'object',
|
|
537
|
+
properties: {
|
|
538
|
+
foo: {
|
|
539
|
+
type: 'string'
|
|
540
|
+
}
|
|
541
|
+
}
|
|
541
542
|
}, 200) // '{"foo":"bar"}'
|
|
542
543
|
|
|
543
544
|
// or
|
|
@@ -586,7 +587,7 @@ values.
|
|
|
586
587
|
<a id="raw"></a>
|
|
587
588
|
|
|
588
589
|
This is the
|
|
589
|
-
[`http.ServerResponse`](https://nodejs.org/dist/latest-
|
|
590
|
+
[`http.ServerResponse`](https://nodejs.org/dist/latest-v20.x/docs/api/http.html#http_class_http_serverresponse)
|
|
590
591
|
from Node core. Whilst you are using the Fastify `Reply` object, the use of
|
|
591
592
|
`Reply.raw` functions is at your own risk as you are skipping all the Fastify
|
|
592
593
|
logic of handling the HTTP response. e.g.:
|
|
@@ -712,7 +713,7 @@ fastify.get('/streams', async function (request, reply) {
|
|
|
712
713
|
If you are sending a buffer and you have not set a `'Content-Type'` header,
|
|
713
714
|
*send* will set it to `'application/octet-stream'`.
|
|
714
715
|
|
|
715
|
-
As noted above, Buffers are considered to be pre-serialized, so they will be
|
|
716
|
+
As noted above, Buffers are considered to be pre-serialized, so they will be
|
|
716
717
|
sent unmodified without response validation.
|
|
717
718
|
|
|
718
719
|
```js
|
|
@@ -743,7 +744,7 @@ fastify.get('/streams', async function (request, reply) {
|
|
|
743
744
|
`send` manages TypedArray like a Buffer, and sets the `'Content-Type'`
|
|
744
745
|
header to `'application/octet-stream'` if not already set.
|
|
745
746
|
|
|
746
|
-
As noted above, TypedArray/Buffers are considered to be pre-serialized, so they
|
|
747
|
+
As noted above, TypedArray/Buffers are considered to be pre-serialized, so they
|
|
747
748
|
will be sent unmodified without response validation.
|
|
748
749
|
|
|
749
750
|
```js
|
|
@@ -759,7 +760,7 @@ fastify.get('/streams', function (request, reply) {
|
|
|
759
760
|
<a id="send-readablestream"></a>
|
|
760
761
|
|
|
761
762
|
`ReadableStream` will be treated as a node stream mentioned above,
|
|
762
|
-
the content is considered to be pre-serialized, so they will be
|
|
763
|
+
the content is considered to be pre-serialized, so they will be
|
|
763
764
|
sent unmodified without response validation.
|
|
764
765
|
|
|
765
766
|
```js
|
|
@@ -778,7 +779,7 @@ fastify.get('/streams', function (request, reply) {
|
|
|
778
779
|
|
|
779
780
|
`Response` allows to manage the reply payload, status code and
|
|
780
781
|
headers in one place. The payload provided inside `Response` is
|
|
781
|
-
considered to be pre-serialized, so they will be sent unmodified
|
|
782
|
+
considered to be pre-serialized, so they will be sent unmodified
|
|
782
783
|
without response validation.
|
|
783
784
|
|
|
784
785
|
Please be aware when using `Response`, the status code and headers
|
|
@@ -23,20 +23,23 @@ Request is a core Fastify object containing the following fields:
|
|
|
23
23
|
- `host` - the host of the incoming request (derived from `X-Forwarded-Host`
|
|
24
24
|
header when the [`trustProxy`](./Server.md#factory-trust-proxy) option is
|
|
25
25
|
enabled). For HTTP/2 compatibility it returns `:authority` if no host header
|
|
26
|
-
exists.
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
- `
|
|
26
|
+
exists. The host header may return empty string when using
|
|
27
|
+
`requireHostHeader = false`, not suppied when connected with `HTTP/1.0` or
|
|
28
|
+
using schema validation that remove the extra headers.
|
|
29
|
+
- `hostname` - the host of the `host` property, it may refers the incoming
|
|
30
|
+
request hostname
|
|
31
|
+
- `port` - the port of the `host` property, it may refers the port thats
|
|
32
|
+
the server is listening on
|
|
30
33
|
- `protocol` - the protocol of the incoming request (`https` or `http`)
|
|
31
34
|
- `method` - the method of the incoming request
|
|
32
35
|
- `url` - the URL of the incoming request
|
|
33
|
-
- `originalUrl` - similar to `url`, this allows you to access the
|
|
34
|
-
original `url` in case of internal re-routing
|
|
36
|
+
- `originalUrl` - similar to `url`, this allows you to access the
|
|
37
|
+
original `url` in case of internal re-routing
|
|
35
38
|
- `is404` - true if request is being handled by 404 handler, false if it is not
|
|
36
39
|
- `socket` - the underlying connection of the incoming request
|
|
37
40
|
- `context` - Deprecated, use `request.routeOptions.config` instead.
|
|
38
41
|
A Fastify internal object. You should not use
|
|
39
|
-
it directly or modify it. It is useful to access one special key:
|
|
42
|
+
it directly or modify it. It is useful to access one special key:
|
|
40
43
|
- `context.config` - The route [`config`](./Routes.md#routes-config) object.
|
|
41
44
|
- `routeOptions` - The route [`option`](./Routes.md#routes-options) object
|
|
42
45
|
- `bodyLimit` - either server limit or route limit
|
|
@@ -44,15 +47,15 @@ it directly or modify it. It is useful to access one special key:
|
|
|
44
47
|
- `method` - the http method for the route
|
|
45
48
|
- `url` - the path of the URL to match this route
|
|
46
49
|
- `handler` - the handler for this route
|
|
47
|
-
- `attachValidation` - attach `validationError` to request
|
|
50
|
+
- `attachValidation` - attach `validationError` to request
|
|
48
51
|
(if there is a schema defined)
|
|
49
52
|
- `logLevel` - log level defined for this route
|
|
50
53
|
- `schema` - the JSON schemas definition for this route
|
|
51
54
|
- `version` - a semver compatible string that defines the version of the endpoint
|
|
52
55
|
- `exposeHeadRoute` - creates a sibling HEAD route for any GET routes
|
|
53
|
-
- `prefixTrailingSlash` - string used to determine how to handle passing /
|
|
56
|
+
- `prefixTrailingSlash` - string used to determine how to handle passing /
|
|
54
57
|
as a route with a prefix.
|
|
55
|
-
- [.getValidationFunction(schema | httpPart)](#getvalidationfunction) -
|
|
58
|
+
- [.getValidationFunction(schema | httpPart)](#getvalidationfunction) -
|
|
56
59
|
Returns a validation function for the specified schema or http part,
|
|
57
60
|
if any of either are set or cached.
|
|
58
61
|
- [.compileValidationSchema(schema, [httpPart])](#compilevalidationschema) -
|
|
@@ -85,6 +88,9 @@ request's headers with the `request.raw.headers` property.
|
|
|
85
88
|
> Note: For performance reason on `not found` route, you may see that we will
|
|
86
89
|
add an extra property `Symbol('fastify.RequestAcceptVersion')` on the headers.
|
|
87
90
|
|
|
91
|
+
> Note: When using schema, it may mutate the `request.headers` and
|
|
92
|
+
`request.raw.headers` object. So, you may found the headers becomes empty.
|
|
93
|
+
|
|
88
94
|
```js
|
|
89
95
|
fastify.post('/:params', options, function (request, reply) {
|
|
90
96
|
console.log(request.body)
|
|
@@ -117,7 +123,7 @@ fastify.post('/:params', options, function (request, reply) {
|
|
|
117
123
|
### .getValidationFunction(schema | httpPart)
|
|
118
124
|
<a id="getvalidationfunction"></a>
|
|
119
125
|
|
|
120
|
-
By calling this function using a provided `schema` or `httpPart`,
|
|
126
|
+
By calling this function using a provided `schema` or `httpPart`,
|
|
121
127
|
it will return a `validation` function that can be used to
|
|
122
128
|
validate diverse inputs. It returns `undefined` if no
|
|
123
129
|
serialization function was found using either of the provided inputs.
|
|
@@ -128,12 +134,12 @@ are assigned to errors
|
|
|
128
134
|
```js
|
|
129
135
|
const validate = request
|
|
130
136
|
.getValidationFunction({
|
|
131
|
-
type: 'object',
|
|
132
|
-
properties: {
|
|
133
|
-
foo: {
|
|
134
|
-
type: 'string'
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
+
type: 'object',
|
|
138
|
+
properties: {
|
|
139
|
+
foo: {
|
|
140
|
+
type: 'string'
|
|
141
|
+
}
|
|
142
|
+
}
|
|
137
143
|
})
|
|
138
144
|
console.log(validate({ foo: 'bar' })) // true
|
|
139
145
|
console.log(validate.errors) // null
|
|
@@ -168,12 +174,12 @@ are assigned to errors
|
|
|
168
174
|
```js
|
|
169
175
|
const validate = request
|
|
170
176
|
.compileValidationSchema({
|
|
171
|
-
type: 'object',
|
|
172
|
-
properties: {
|
|
173
|
-
foo: {
|
|
174
|
-
type: 'string'
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
+
type: 'object',
|
|
178
|
+
properties: {
|
|
179
|
+
foo: {
|
|
180
|
+
type: 'string'
|
|
181
|
+
}
|
|
182
|
+
}
|
|
177
183
|
})
|
|
178
184
|
console.log(validate({ foo: 'bar' })) // true
|
|
179
185
|
console.log(validate.errors) // null
|
|
@@ -182,12 +188,12 @@ console.log(validate.errors) // null
|
|
|
182
188
|
|
|
183
189
|
const validate = request
|
|
184
190
|
.compileValidationSchema({
|
|
185
|
-
type: 'object',
|
|
186
|
-
properties: {
|
|
187
|
-
foo: {
|
|
188
|
-
type: 'string'
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
+
type: 'object',
|
|
192
|
+
properties: {
|
|
193
|
+
foo: {
|
|
194
|
+
type: 'string'
|
|
195
|
+
}
|
|
196
|
+
}
|
|
191
197
|
}, 200)
|
|
192
198
|
console.log(validate({ hello: 'world' })) // false
|
|
193
199
|
console.log(validate.errors) // validation errors
|
|
@@ -217,7 +223,7 @@ const schema1 = {
|
|
|
217
223
|
```
|
|
218
224
|
|
|
219
225
|
*Not*
|
|
220
|
-
```js
|
|
226
|
+
```js
|
|
221
227
|
const validate = request.compileValidationSchema(schema1)
|
|
222
228
|
|
|
223
229
|
// Later on...
|
|
@@ -252,25 +258,25 @@ function will be compiled, forwarding the `httpPart` if provided.
|
|
|
252
258
|
|
|
253
259
|
```js
|
|
254
260
|
request
|
|
255
|
-
.validateInput({ foo: 'bar'}, {
|
|
256
|
-
type: 'object',
|
|
257
|
-
properties: {
|
|
258
|
-
foo: {
|
|
259
|
-
type: 'string'
|
|
260
|
-
}
|
|
261
|
-
}
|
|
261
|
+
.validateInput({ foo: 'bar'}, {
|
|
262
|
+
type: 'object',
|
|
263
|
+
properties: {
|
|
264
|
+
foo: {
|
|
265
|
+
type: 'string'
|
|
266
|
+
}
|
|
267
|
+
}
|
|
262
268
|
}) // true
|
|
263
269
|
|
|
264
270
|
// or
|
|
265
271
|
|
|
266
272
|
request
|
|
267
273
|
.validateInput({ foo: 'bar'}, {
|
|
268
|
-
type: 'object',
|
|
269
|
-
properties: {
|
|
270
|
-
foo: {
|
|
271
|
-
type: 'string'
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
+
type: 'object',
|
|
275
|
+
properties: {
|
|
276
|
+
foo: {
|
|
277
|
+
type: 'string'
|
|
278
|
+
}
|
|
279
|
+
}
|
|
274
280
|
}, 'body') // true
|
|
275
281
|
|
|
276
282
|
// or
|
package/docs/Reference/Routes.md
CHANGED
|
@@ -93,16 +93,16 @@ fastify.route(options)
|
|
|
93
93
|
* `childLoggerFactory(logger, binding, opts, rawReq)`: a custom factory function
|
|
94
94
|
that will be called to produce a child logger instance for every request.
|
|
95
95
|
See [`childLoggerFactory`](./Server.md#childloggerfactory) for more info.
|
|
96
|
-
Overrides the default logger factory, and anything set by
|
|
96
|
+
Overrides the default logger factory, and anything set by
|
|
97
97
|
[`setChildLoggerFactory`](./Server.md#setchildloggerfactory), for requests to
|
|
98
|
-
the route. To access the default factory, you can access
|
|
98
|
+
the route. To access the default factory, you can access
|
|
99
99
|
`instance.childLoggerFactory`. Note that this will point to Fastify's default
|
|
100
100
|
`childLoggerFactory` only if a plugin hasn't overridden it already.
|
|
101
101
|
* `validatorCompiler({ schema, method, url, httpPart })`: function that builds
|
|
102
102
|
schemas for request validations. See the [Validation and
|
|
103
103
|
Serialization](./Validation-and-Serialization.md#schema-validator)
|
|
104
104
|
documentation.
|
|
105
|
-
* `serializerCompiler({ { schema, method, url, httpStatus, contentType } })`:
|
|
105
|
+
* `serializerCompiler({ { schema, method, url, httpStatus, contentType } })`:
|
|
106
106
|
function that builds schemas for response serialization. See the [Validation and
|
|
107
107
|
Serialization](./Validation-and-Serialization.md#schema-serializer)
|
|
108
108
|
documentation.
|
|
@@ -121,8 +121,8 @@ fastify.route(options)
|
|
|
121
121
|
* `version`: a [semver](https://semver.org/) compatible string that defined the
|
|
122
122
|
version of the endpoint. [Example](#version-constraints).
|
|
123
123
|
* `constraints`: defines route restrictions based on request properties or
|
|
124
|
-
values, enabling customized matching using
|
|
125
|
-
[find-my-way](https://github.com/delvedor/find-my-way) constraints. Includes
|
|
124
|
+
values, enabling customized matching using
|
|
125
|
+
[find-my-way](https://github.com/delvedor/find-my-way) constraints. Includes
|
|
126
126
|
built-in `version` and `host` constraints, with support for custom constraint
|
|
127
127
|
strategies.
|
|
128
128
|
* `prefixTrailingSlash`: string used to determine how to handle passing `/` as a
|
|
@@ -796,10 +796,10 @@ const secret = {
|
|
|
796
796
|
> inside the callback. If the error is not preventable, it is recommended to provide
|
|
797
797
|
> a custom `frameworkErrors` handler to deal with it. Otherwise, you route selection
|
|
798
798
|
> may break or expose sensitive information to attackers.
|
|
799
|
-
>
|
|
799
|
+
>
|
|
800
800
|
> ```js
|
|
801
801
|
> const Fastify = require('fastify')
|
|
802
|
-
>
|
|
802
|
+
>
|
|
803
803
|
> const fastify = Fastify({
|
|
804
804
|
> frameworkErrors: function (err, res, res) {
|
|
805
805
|
> if (err instanceof Fastify.errorCodes.FST_ERR_ASYNC_CONSTRAINT) {
|