Aktueller Stand
This commit is contained in:
131
backend/node_modules/fastify/docs/Reference/Hooks.md
generated
vendored
131
backend/node_modules/fastify/docs/Reference/Hooks.md
generated
vendored
@@ -34,9 +34,9 @@ are Request/Reply hooks and application hooks:
|
||||
- [Using Hooks to Inject Custom Properties](#using-hooks-to-inject-custom-properties)
|
||||
- [Diagnostics Channel Hooks](#diagnostics-channel-hooks)
|
||||
|
||||
**Notice:** the `done` callback is not available when using `async`/`await` or
|
||||
returning a `Promise`. If you do invoke a `done` callback in this situation
|
||||
unexpected behavior may occur, e.g. duplicate invocation of handlers.
|
||||
> ℹ️ Note: The `done` callback is not available when using `async`/`await` or
|
||||
> returning a `Promise`. If you do invoke a `done` callback in this situation
|
||||
> unexpected behavior may occur, e.g. duplicate invocation of handlers.
|
||||
|
||||
## Request/Reply Hooks
|
||||
|
||||
@@ -68,9 +68,9 @@ fastify.addHook('onRequest', async (request, reply) => {
|
||||
})
|
||||
```
|
||||
|
||||
**Notice:** in the [onRequest](#onrequest) hook, `request.body` will always be
|
||||
`undefined`, because the body parsing happens before the
|
||||
[preValidation](#prevalidation) hook.
|
||||
> ℹ️ Note: In the [onRequest](#onrequest) hook, `request.body` will always be
|
||||
> `undefined`, because the body parsing happens before the
|
||||
> [preValidation](#prevalidation) hook.
|
||||
|
||||
### preParsing
|
||||
|
||||
@@ -98,17 +98,17 @@ fastify.addHook('preParsing', async (request, reply, payload) => {
|
||||
})
|
||||
```
|
||||
|
||||
**Notice:** in the [preParsing](#preparsing) hook, `request.body` will always be
|
||||
`undefined`, because the body parsing happens before the
|
||||
[preValidation](#prevalidation) hook.
|
||||
> ℹ️ Note: In the [preParsing](#preparsing) hook, `request.body` will always be
|
||||
> `undefined`, because the body parsing happens before the
|
||||
> [preValidation](#prevalidation) hook.
|
||||
|
||||
**Notice:** you should also add a `receivedEncodedLength` property to the
|
||||
returned stream. This property is used to correctly match the request payload
|
||||
with the `Content-Length` header value. Ideally, this property should be updated
|
||||
on each received chunk.
|
||||
> ℹ️ Note: You should also add a `receivedEncodedLength` property to the
|
||||
> returned stream. This property is used to correctly match the request payload
|
||||
> with the `Content-Length` header value. Ideally, this property should be updated
|
||||
> on each received chunk.
|
||||
|
||||
**Notice:** The size of the returned stream is checked to not exceed the limit
|
||||
set in [`bodyLimit`](./Server.md#bodylimit) option.
|
||||
> ℹ️ Note: The size of the returned stream is checked to not exceed the limit
|
||||
> set in [`bodyLimit`](./Server.md#bodylimit) option.
|
||||
|
||||
### preValidation
|
||||
|
||||
@@ -166,8 +166,8 @@ fastify.addHook('preSerialization', async (request, reply, payload) => {
|
||||
})
|
||||
```
|
||||
|
||||
Note: the hook is NOT called if the payload is a `string`, a `Buffer`, a
|
||||
`stream`, or `null`.
|
||||
> ℹ️ Note: The hook is NOT called if the payload is a `string`, a `Buffer`, a
|
||||
> `stream`, or `null`.
|
||||
|
||||
### onError
|
||||
```js
|
||||
@@ -189,15 +189,11 @@ specific header in case of error.
|
||||
It is not intended for changing the error, and calling `reply.send` will throw
|
||||
an exception.
|
||||
|
||||
This hook will be executed only after
|
||||
the [Custom Error Handler set by `setErrorHandler`](./Server.md#seterrorhandler)
|
||||
has been executed, and only if the custom error handler sends an error back to the
|
||||
user
|
||||
*(Note that the default error handler always sends the error back to the
|
||||
user)*.
|
||||
This hook will be executed before
|
||||
the [Custom Error Handler set by `setErrorHandler`](./Server.md#seterrorhandler).
|
||||
|
||||
**Notice:** unlike the other hooks, passing an error to the `done` function is not
|
||||
supported.
|
||||
> ℹ️ Note: Unlike the other hooks, passing an error to the `done` function is not
|
||||
> supported.
|
||||
|
||||
### onSend
|
||||
If you are using the `onSend` hook, you can change the payload. For example:
|
||||
@@ -233,8 +229,8 @@ fastify.addHook('onSend', (request, reply, payload, done) => {
|
||||
> to `0`, whereas the `Content-Length` header will not be set if the payload is
|
||||
> `null`.
|
||||
|
||||
Note: If you change the payload, you may only change it to a `string`, a
|
||||
`Buffer`, a `stream`, a `ReadableStream`, a `Response`, or `null`.
|
||||
> ℹ️ Note: If you change the payload, you may only change it to a `string`, a
|
||||
> `Buffer`, a `stream`, a `ReadableStream`, a `Response`, or `null`.
|
||||
|
||||
|
||||
### onResponse
|
||||
@@ -256,8 +252,8 @@ The `onResponse` hook is executed when a response has been sent, so you will not
|
||||
be able to send more data to the client. It can however be useful for sending
|
||||
data to external services, for example, to gather statistics.
|
||||
|
||||
**Note:** setting `disableRequestLogging` to `true` will disable any error log
|
||||
inside the `onResponse` hook. In this case use `try - catch` to log errors.
|
||||
> ℹ️ Note: Setting `disableRequestLogging` to `true` will disable any error log
|
||||
> inside the `onResponse` hook. In this case use `try - catch` to log errors.
|
||||
|
||||
### onTimeout
|
||||
|
||||
@@ -298,7 +294,8 @@ The `onRequestAbort` hook is executed when a client closes the connection before
|
||||
the entire request has been processed. Therefore, you will not be able to send
|
||||
data to the client.
|
||||
|
||||
**Notice:** client abort detection is not completely reliable. See: [`Detecting-When-Clients-Abort.md`](../Guides/Detecting-When-Clients-Abort.md)
|
||||
> ℹ️ Note: Client abort detection is not completely reliable.
|
||||
> See: [`Detecting-When-Clients-Abort.md`](../Guides/Detecting-When-Clients-Abort.md)
|
||||
|
||||
### Manage Errors from a hook
|
||||
If you get an error during the execution of your hook, just pass it to `done()`
|
||||
@@ -428,8 +425,8 @@ fastify.addHook('onReady', async function () {
|
||||
|
||||
### onListen
|
||||
|
||||
Triggered when the server starts listening for requests. The hooks run one
|
||||
after another. If a hook function causes an error, it is logged and
|
||||
Triggered when the server starts listening for requests. The hooks run one
|
||||
after another. If a hook function causes an error, it is logged and
|
||||
ignored, allowing the queue of hooks to continue. Hook functions accept one
|
||||
argument: a callback, `done`, to be invoked after the hook function is
|
||||
complete. Hook functions are invoked with `this` bound to the associated
|
||||
@@ -451,8 +448,8 @@ fastify.addHook('onListen', async function () {
|
||||
})
|
||||
```
|
||||
|
||||
> **Note**
|
||||
> This hook will not run when the server is started using `fastify.inject()` or `fastify.ready()`
|
||||
> ℹ️ Note: This hook will not run when the server is started using
|
||||
> fastify.inject()` or `fastify.ready()`.
|
||||
|
||||
### onClose
|
||||
<a id="on-close"></a>
|
||||
@@ -462,7 +459,7 @@ HTTP requests have been completed.
|
||||
It is useful when [plugins](./Plugins.md) need a "shutdown" event, for example,
|
||||
to close an open connection to a database.
|
||||
|
||||
The hook function takes the Fastify instance as a first argument,
|
||||
The hook function takes the Fastify instance as a first argument,
|
||||
and a `done` callback for synchronous hook functions.
|
||||
```js
|
||||
// callback style
|
||||
@@ -575,8 +572,8 @@ This hook can be useful if you are developing a plugin that needs to know when a
|
||||
plugin context is formed, and you want to operate in that specific context, thus
|
||||
this hook is encapsulated.
|
||||
|
||||
**Note:** This hook will not be called if a plugin is wrapped inside
|
||||
[`fastify-plugin`](https://github.com/fastify/fastify-plugin).
|
||||
> ℹ️ Note: This hook will not be called if a plugin is wrapped inside
|
||||
> [`fastify-plugin`](https://github.com/fastify/fastify-plugin).
|
||||
```js
|
||||
fastify.decorate('data', [])
|
||||
|
||||
@@ -773,7 +770,7 @@ fastify.route({
|
||||
})
|
||||
```
|
||||
|
||||
**Note**: both options also accept an array of functions.
|
||||
> ℹ️ Note: Both options also accept an array of functions.
|
||||
|
||||
## Using Hooks to Inject Custom Properties
|
||||
<a id="using-hooks-to-inject-custom-properties"></a>
|
||||
@@ -828,19 +825,11 @@ consider creating a custom [Plugin](./Plugins.md) instead.
|
||||
|
||||
## Diagnostics Channel Hooks
|
||||
|
||||
> **Note:** The `diagnostics_channel` is currently experimental on Node.js, so
|
||||
> its API is subject to change even in semver-patch releases of Node.js. For
|
||||
> versions of Node.js supported by Fastify where `diagnostics_channel` is
|
||||
> unavailable, the hook will use the
|
||||
> [polyfill](https://www.npmjs.com/package/diagnostics_channel) if it is
|
||||
> available. Otherwise, this feature will not be present.
|
||||
|
||||
Currently, one
|
||||
[`diagnostics_channel`](https://nodejs.org/api/diagnostics_channel.html) publish
|
||||
event, `'fastify.initialization'`, happens at initialization time. The Fastify
|
||||
instance is passed into the hook as a property of the object passed in. At this
|
||||
point, the instance can be interacted with to add hooks, plugins, routes, or any
|
||||
other sort of modification.
|
||||
One [`diagnostics_channel`](https://nodejs.org/api/diagnostics_channel.html)
|
||||
publish event, `'fastify.initialization'`, happens at initialization time. The
|
||||
Fastify instance is passed into the hook as a property of the object passed in.
|
||||
At this point, the instance can be interacted with to add hooks, plugins,
|
||||
routes, or any other sort of modification.
|
||||
|
||||
For example, a tracing package might do something like the following (which is,
|
||||
of course, a simplification). This would be in a file loaded in the
|
||||
@@ -855,7 +844,7 @@ const spans = new WeakMap()
|
||||
|
||||
channel.subscribe(function ({ fastify }) {
|
||||
fastify.addHook('onRequest', (request, reply, done) => {
|
||||
const span = tracer.startSpan('fastify.request')
|
||||
const span = tracer.startSpan('fastify.request.handler')
|
||||
spans.set(request, span)
|
||||
done()
|
||||
})
|
||||
@@ -867,3 +856,41 @@ channel.subscribe(function ({ fastify }) {
|
||||
})
|
||||
})
|
||||
```
|
||||
|
||||
> ℹ️ Note: The TracingChannel class API is currently experimental and may undergo
|
||||
> breaking changes even in semver-patch releases of Node.js.
|
||||
|
||||
Five other events are published on a per-request basis following the
|
||||
[Tracing Channel](https://nodejs.org/api/diagnostics_channel.html#class-tracingchannel)
|
||||
nomenclature. The list of the channel names and the event they receive is:
|
||||
|
||||
- `tracing:fastify.request.handler:start`: Always fires
|
||||
- `{ request: Request, reply: Reply, route: { url, method } }`
|
||||
- `tracing:fastify.request.handler:end`: Always fires
|
||||
- `{ request: Request, reply: Reply, route: { url, method }, async: Bool }`
|
||||
- `tracing:fastify.request.handler:asyncStart`: Fires for promise/async handlers
|
||||
- `{ request: Request, reply: Reply, route: { url, method } }`
|
||||
- `tracing:fastify.request.handler:asyncEnd`: Fires for promise/async handlers
|
||||
- `{ request: Request, reply: Reply, route: { url, method } }`
|
||||
- `tracing:fastify.request.handler:error`: Fires when an error occurs
|
||||
- `{ request: Request, reply: Reply, route: { url, method }, error: Error }`
|
||||
|
||||
The object instance remains the same for all events associated with a given
|
||||
request. All payloads include a `request` and `reply` property which are an
|
||||
instance of Fastify's `Request` and `Reply` instances. They also include a
|
||||
`route` property which is an object with the matched `url` pattern (e.g.
|
||||
`/collection/:id`) and the `method` HTTP method (e.g. `GET`). The `:start` and
|
||||
`:end` events always fire for requests. If a request handler is an `async`
|
||||
function or one that returns a `Promise` then the `:asyncStart` and `:asyncEnd`
|
||||
events also fire. Finally, the `:error` event contains an `error` property
|
||||
associated with the request's failure.
|
||||
|
||||
These events can be received like so:
|
||||
|
||||
```js
|
||||
const dc = require('node:diagnostics_channel')
|
||||
const channel = dc.channel('tracing:fastify.request.handler:start')
|
||||
channel.subscribe((msg) => {
|
||||
console.log(msg.request, msg.reply)
|
||||
})
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user