From 5e271af9c98ccc42ccf55a0d491d23be6de0cd54 Mon Sep 17 00:00:00 2001 From: Holly Date: Mon, 27 Jan 2025 09:57:45 +0000 Subject: [PATCH] print request info to console/log when it's an image request As we take control of the request at this point and don't hand the control back, we never hit the logger line on the next hook. --- index.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/index.ts b/index.ts index 9b078aa..1dad053 100644 --- a/index.ts +++ b/index.ts @@ -1,4 +1,4 @@ -import Fastify from "fastify"; +import Fastify, { type FastifyReply, type FastifyRequest } from "fastify"; import FastifyFormBody from "@fastify/formbody"; import FastifyMultipart from "@fastify/multipart"; import FastifyCookie from "@fastify/cookie"; @@ -57,6 +57,11 @@ fastify.register(FastifyStatic, { redirect: false }); +function printReqInfo(req: FastifyRequest, res: FastifyReply) { + // @ts-ignore + Console.printInfo(`[ ${req.logType} ] [ ${req.method.toUpperCase()} ] [ ${ConsoleUtility.StatusColor(res.statusCode)} ] [ ${blue(`${Date.now() - req.startTime}ms`)} ] > ${req.url}`); +} + const hashLookupCache = new FunkyArray(); fastify.addHook("preHandler", (req, res, done) => { (async () => { @@ -90,6 +95,8 @@ fastify.addHook("preHandler", (req, res, done) => { "content-length": media.FileSize, })); readStream.pipe(res.raw); + + printReqInfo(req, res); return; } } else { @@ -105,10 +112,7 @@ fastify.addHook("preHandler", (req, res, done) => { }); fastify.addHook("onSend", (req, res, _payload, done) => { - // @ts-ignore - Console.printInfo(`[ ${req.logType} ] [ ${req.method.toUpperCase()} ] [ ${ConsoleUtility.StatusColor(res.statusCode)} ] [ ${blue(`${Date.now() - req.startTime}ms`)} ] > ${req.url}`); - - //console.log(res.getHeaders()); + printReqInfo(req, res); done(); });