File size: 994 Bytes
bf48b89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import type { MiddlewareHandler } from 'hono';
import { routePath } from 'hono/route';

import { getDebugInfo, setDebugInfo } from '@/utils/debug-info';

const middleware: MiddlewareHandler = async (ctx, next) => {
    {
        const debug = getDebugInfo();
        if (!debug.paths[ctx.req.path]) {
            debug.paths[ctx.req.path] = 0;
        }
        debug.paths[ctx.req.path]++;

        debug.request++;
        setDebugInfo(debug);
    }

    await next();

    {
        const debug = getDebugInfo();
        const rPath = routePath(ctx);
        const hasMatchedRoute = rPath !== '/*';
        if (!debug.routes[rPath] && hasMatchedRoute) {
            debug.routes[rPath] = 0;
        }
        hasMatchedRoute && debug.routes[rPath]++;

        if (ctx.res.headers.get('RSSHub-Cache-Status')) {
            debug.hitCache++;
        }

        if (ctx.res.status === 304) {
            debug.etag++;
        }
        setDebugInfo(debug);
    }
};

export default middleware;