message: add asyncLocalStorage instance in getCorrelationId and bindCorrelationId functions
diff: @@ -7,16 +7,24 @@ import { AsyncLocalStorage } from "async_hooks";
  import * as uuid from "uuid";
  import { Request, Response, NextFunction } from "express";
  -const asyncLocalStorage = new AsyncLocalStorage<string>();
  +const defaultAsyncLocalStorage = new AsyncLocalStorage<string>();
  -export function getCorrelationId(): string | undefined {
  - const id = asyncLocalStorage.getStore();
  - return id;
  +export function getCorrelationId(altAsyncLocalStorage?: AsyncLocalStorage<string>): string | undefined {
  + if (altAsyncLocalStorage) {
  + return altAsyncLocalStorage.getStore();
  + } else {
  + return defaultAsyncLocalStorage.getStore();
  + }
  }
  -export const bindCorrelationId = (headerName: string = "x-correlation-id") =>
  +export const bindCorrelationId =
  + (altAsyncLocalStorage?: AsyncLocalStorage<string>, headerName: string = "x-correlation-id") =>
  ((req: Request, res: Response, next: NextFunction): void => {
  const id: string = req.header(headerName) || uuid.v4();
  res.setHeader(headerName, id);
  - asyncLocalStorage.run(id, () => next());
  + if (altAsyncLocalStorage) {
  + altAsyncLocalStorage.run(id, () => next());
  + } else {
  + defaultAsyncLocalStorage.run(id, () => next());
  + }
  });
  