strong-tie/inbound-calls
0
1<h1 align="center">Fastify</h1>2 3## Lifecycle4<a id="lifecycle"></a>5 6Following the schema of the internal lifecycle of Fastify.7 8On the right branch of every section there is the next phase of the lifecycle,9on the left branch there is the corresponding error code that will be generated10if the parent throws an error *(note that all the errors are automatically11handled by Fastify)*.12 13```14Incoming Request15 │16 └─▶ Routing17 │18 └─▶ Instance Logger19 │20 4**/5** ◀─┴─▶ onRequest Hook21 │22 4**/5** ◀─┴─▶ preParsing Hook23 │24 4**/5** ◀─┴─▶ Parsing25 │26 4**/5** ◀─┴─▶ preValidation Hook27 │28 400 ◀─┴─▶ Validation29 │30 4**/5** ◀─┴─▶ preHandler Hook31 │32 4**/5** ◀─┴─▶ User Handler33 │34 └─▶ Reply35 │36 4**/5** ◀─┴─▶ preSerialization Hook37 │38 └─▶ onSend Hook39 │40 4**/5** ◀─┴─▶ Outgoing Response41 │42 └─▶ onResponse Hook43```44 45At any point before or during the `User Handler`, `reply.hijack()` can be called46to prevent Fastify from:47- Running all the following hooks and user handler48- Sending the response automatically49 50NB (*): If `reply.raw` is used to send a response back to the user, `onResponse`51hooks will still be executed52 53## Reply Lifecycle54<a id="reply-lifecycle"></a>55 56Whenever the user handles the request, the result may be:57 58- in async handler: it returns a payload59- in async handler: it throws an `Error`60- in sync handler: it sends a payload61- in sync handler: it sends an `Error` instance62 63If the reply was hijacked, we skip all the below steps. Otherwise, when it is64being submitted, the data flow performed is the following:65 66```67 ★ schema validation Error68 │69 └─▶ schemaErrorFormatter70 │71 reply sent ◀── JSON ─┴─ Error instance72 │73 │ ★ throw an Error74 ★ send or return │ │75 │ │ │76 │ ▼ │77 reply sent ◀── JSON ─┴─ Error instance ──▶ setErrorHandler ◀─────┘78 │79 reply sent ◀── JSON ─┴─ Error instance ──▶ onError Hook80 │81 └─▶ reply sent82```83 84Note: `reply sent` means that the JSON payload will be serialized by:85 86- the [reply serialized](./Server.md#setreplyserializer) if set87- or by the [serializer compiler](./Server.md#setserializercompiler) when a JSON88 schema has been set for the returning HTTP status code89- or by the default `JSON.stringify` function90 