2026-09-17 19:14:41 +08:00
|
|
|
|
// services/api/src/request-id.middleware.ts
|
|
|
|
|
|
// 请求 ID 约定:优先透传上游 x-request-id(网关/前端可携带),否则生成 UUID v4;
|
|
|
|
|
|
// 挂到 req.id(pino-http 日志字段)并写回响应头 x-request-id,便于全链路排障。
|
2026-09-17 20:54:30 +08:00
|
|
|
|
// 长度上限(Task 7 遗留项④):上游值 >128 字符时视为滥用/异常,静默换成新 UUID
|
|
|
|
|
|
//(不透传也不告警——日志与响应头由本函数统一写回,不会出现半截 ID)。
|
2026-09-17 19:14:41 +08:00
|
|
|
|
// genReqId 与本中间件共用 resolveRequestId,先后顺序不影响结果。
|
|
|
|
|
|
import { randomUUID } from 'node:crypto';
|
2026-09-17 20:54:30 +08:00
|
|
|
|
import type { ServerResponse } from 'node:http';
|
2026-09-17 19:14:41 +08:00
|
|
|
|
import type { NestMiddleware } from '@nestjs/common';
|
2026-09-17 20:54:30 +08:00
|
|
|
|
import type { ServerRequestWithId } from './shared/request-with-id';
|
2026-09-17 19:14:41 +08:00
|
|
|
|
|
2026-09-17 20:54:30 +08:00
|
|
|
|
/** 透传上游 x-request-id 的长度上限(UUID v4 为 36 字符,128 已十分宽裕)。 */
|
|
|
|
|
|
export const MAX_REQUEST_ID_LENGTH = 128;
|
2026-09-17 19:14:41 +08:00
|
|
|
|
|
|
|
|
|
|
export function resolveRequestId(
|
2026-09-17 20:54:30 +08:00
|
|
|
|
req: ServerRequestWithId,
|
2026-09-17 19:14:41 +08:00
|
|
|
|
res: ServerResponse,
|
|
|
|
|
|
): string {
|
2026-09-17 20:54:30 +08:00
|
|
|
|
if (req.id) {
|
|
|
|
|
|
return req.id;
|
2026-09-17 19:14:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
const header = req.headers['x-request-id'];
|
|
|
|
|
|
const fromHeader = Array.isArray(header) ? header[0] : header;
|
|
|
|
|
|
const id =
|
2026-09-17 20:54:30 +08:00
|
|
|
|
typeof fromHeader === 'string' &&
|
|
|
|
|
|
fromHeader.trim() !== '' &&
|
|
|
|
|
|
fromHeader.length <= MAX_REQUEST_ID_LENGTH
|
2026-09-17 19:14:41 +08:00
|
|
|
|
? fromHeader
|
|
|
|
|
|
: randomUUID();
|
2026-09-17 20:54:30 +08:00
|
|
|
|
req.id = id;
|
2026-09-17 19:14:41 +08:00
|
|
|
|
res.setHeader('x-request-id', id);
|
|
|
|
|
|
return id;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export class RequestIdMiddleware implements NestMiddleware {
|
2026-09-17 20:54:30 +08:00
|
|
|
|
use(req: ServerRequestWithId, res: ServerResponse, next: () => void): void {
|
2026-09-17 19:14:41 +08:00
|
|
|
|
resolveRequestId(req, res);
|
|
|
|
|
|
next();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|