MCPcopy Index your code
hub / github.com/freeCodeCamp/freeCodeCamp / shadowCapture

Function shadowCapture

api/src/plugins/shadow-capture.ts:26–60  ·  view source on GitHub ↗
(fastify, _options, done)

Source from the content-addressed store, hash-verified

24 * @param done Callback to signal that the logic has completed.
25 */
26const 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 */
63function captureRequest(req: FastifyRequest) {

Callers

nothing calls this directly

Calls 3

doneFunction · 0.85
captureRequestFunction · 0.85
captureReplyFunction · 0.85

Tested by

no test coverage detected