mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-05 03:25:10 +08:00
482b534a90
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`.
26 lines
659 B
TypeScript
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();
|
|
};
|