@samitouri / QOSami-HFS / commits / a507dde8

debug log in case a plugin changes the response in the middleware upstream phase

Massimo Melina committed Dec 13, 2025 at 10:51 UTC a507dde855a0d5d769b0b7fc2ccdc844b73b8d67
1 file changed +17 -8
src/plugins.ts
+17 -8
@@ -189,11 +189,7 @@ export const pluginsMiddleware: Koa.Middleware = async (ctx, next) => {
189 await Promise.all(mapPlugins(async (pl, id) => {
190 try {
191 const res = await pl.middleware?.(ctx)
192 - if (lastStatus !== ctx.status || lastBody !== ctx.body) {
193 - console.debug("plugin changed response", id)
194 - lastStatus = ctx.status
195 - lastBody = ctx.body
196 - }
192 + printChange(id)
193 if (ctx.isAborted())
194 ctx.stop()
195 // don't just check ctx.isStopped, as the async plugin that called ctx.stop will reach here after sync ones
@@ -206,7 +202,7 @@ export const pluginsMiddleware: Koa.Middleware = async (ctx, next) => {
202 printError(id, e)
203 }
204 }))
209 - // expose public plugins' files`
205 + // expose public plugins' files
206 if (!ctx.isStopped) {
207 const { path } = ctx
208 if (path.startsWith(PLUGINS_PUB_URI)) {
@@ -222,10 +218,23 @@ export const pluginsMiddleware: Koa.Middleware = async (ctx, next) => {
218 if (ctx.body === undefined && ctx.status === HTTP_NOT_FOUND) // no response was provided by plugins, so we'll do
219 await next()
220 }
225 - for (const [id,f] of Object.entries(after))
226 - try { await f() }
221 + lastStatus = ctx.status
222 + lastBody = ctx.body
223 + for (const [id, f] of Object.entries(after))
224 + try {
225 + await f()
226 + printChange(id)
227 + }
228 catch (e) { printError(id, e) }
229
230 +
231 + function printChange(id: string) {
232 + if (id === SERVER_CODE_ID || (lastStatus === ctx.status && lastBody === ctx.body)) return
233 + console.debug("plugin changed response:", id)
234 + lastStatus = ctx.status
235 + lastBody = ctx.body
236 + }
237 +
238 function printError(id: string, e: any) {
239 console.log(`error middleware plugin ${id}: ${e?.message || e}`)
240 console.debug(e)