Files
AFFiNE-Mirror/packages/backend/server/src/middleware/timing.ts
T
Brooooooklyn 482b534a90 chore(server): setup winston logger (#9561)
There is no impact on the existing logger, as the current logger is used with `new Logger(Context)` and does not utilize dependency injection. In the next phase, gradually replace and supplement the existing `Logger`.
2025-01-07 09:15:08 +00:00

26 lines
659 B
TypeScript

import { NextFunction, Request, Response } from 'express';
import onHeaders from 'on-headers';
export const serverTimingAndCache = (
req: Request,
res: Response,
next: NextFunction
) => {
req.res = res;
const now = process.hrtime();
onHeaders(res, () => {
const delta = process.hrtime(now);
const costInMilliseconds = (delta[0] + delta[1] / 1e9) * 1000;
const serverTiming = res.getHeader('Server-Timing') as string | undefined;
const serverTimingValue = `${
serverTiming ? `${serverTiming}, ` : ''
}affine-server;dur=${costInMilliseconds}`;
res.setHeader('Server-Timing', serverTimingValue);
});
next();
};