Spaces:
Runtime error
Runtime error
File size: 1,034 Bytes
46252cd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import { Request, Response, NextFunction } from 'express';
import { randomUUID } from 'node:crypto';
import { runWithRequestId } from '../services/request-context';
/**
* Functional Express middleware: assign a request id to every inbound request, echo it on the
* `X-Request-ID` response header, and run the entire downstream chain inside the request scope so
* every log line and audit record can carry it (see LoggerService / AuditService).
*
* Accepts a sane client-supplied id (alphanumeric + dash, ≤128 chars); anything else — including a
* CRLF header-injection attempt — is ignored and a fresh UUID is generated instead.
*/
const CLIENT_REQUEST_ID_PATTERN = /^[A-Za-z0-9-]{1,128}$/;
export function requestContextMiddleware(req: Request, res: Response, next: NextFunction): void {
const incoming = req.header('x-request-id');
const requestId = incoming && CLIENT_REQUEST_ID_PATTERN.test(incoming) ? incoming : randomUUID();
res.setHeader('X-Request-ID', requestId);
runWithRequestId(requestId, next);
}
|