File size: 559 Bytes
5b12974
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { AsyncLocalStorage } from "node:async_hooks";

export type HttpLocalStorage = {
  requestId: string;
  path: string;
  host: string;
  method: string;
};

const httpLocalStorage = new AsyncLocalStorage<HttpLocalStorage>();

export type RunWithHttpContextFunction = <T>(context: HttpLocalStorage, fn: () => T) => T;

export function runWithHttpContext<T>(context: HttpLocalStorage, fn: () => T): T {
  return httpLocalStorage.run(context, fn);
}

export function getHttpContext(): HttpLocalStorage | undefined {
  return httpLocalStorage.getStore();
}