(fastify, _options, done)
| 24 | * @param done Callback to signal that the logic has completed. |
| 25 | */ |
| 26 | const shadowCapture: FastifyPluginCallback = (fastify, _options, done) => { |
| 27 | mkdirSync(LOGS_DIRECTORY, { recursive: true }); |
| 28 | fastify.addHook('onRequest', (req, rep, done) => { |
| 29 | // Attach timestamp at beginning of lifecycle |
| 30 | // @ts-expect-error Exists |
| 31 | req.__timestamp = Date.now(); |
| 32 | |
| 33 | // Give request and response same id to match. |
| 34 | const id = randomUUID(); |
| 35 | // @ts-expect-error Exists |
| 36 | req.__id = id; |
| 37 | // @ts-expect-error Exists |
| 38 | rep.__id = id; |
| 39 | done(); |
| 40 | }); |
| 41 | |
| 42 | // Body is only included after `Parsing` lifecycle |
| 43 | fastify.addHook('preValidation', (req, rep, done) => { |
| 44 | captureRequest(req); |
| 45 | done(); |
| 46 | }); |
| 47 | |
| 48 | fastify.addHook('onSend', async (_req, rep, payload) => { |
| 49 | // @ts-expect-error Exists |
| 50 | rep.__payload = payload; |
| 51 | return payload; |
| 52 | }); |
| 53 | |
| 54 | fastify.addHook('onResponse', (_req, rep, done) => { |
| 55 | captureReply(rep); |
| 56 | done(); |
| 57 | }); |
| 58 | |
| 59 | done(); |
| 60 | }; |
| 61 | |
| 62 | /* eslint-disable @typescript-eslint/no-unsafe-assignment */ |
| 63 | function captureRequest(req: FastifyRequest) { |
nothing calls this directly
no test coverage detected