UNPKG

nats-micro

Version:

NATS micro compatible extra-lightweight microservice library

26 lines 1.42 kB
import { AsyncLocalStorage } from 'async_hooks'; import { contextHeadersToObject, objectToContextHeaders } from './misc.js'; // eslint-disable-next-line @typescript-eslint/no-explicit-any export const threadContext = new AsyncLocalStorage(); export var ThreadContextKey; (function (ThreadContextKey) { ThreadContextKey["additionalHeaders"] = "additionalHeaders"; ThreadContextKey["context"] = "context"; })(ThreadContextKey || (ThreadContextKey = {})); export function addThreadContextHeaders(headers) { var _a, _b; const store = threadContext.getStore(); if (!store) return headers; const allHeaders = Array.from(headers !== null && headers !== void 0 ? headers : []) .filter((header) => header[0] !== ThreadContextKey.additionalHeaders && header[0] !== ThreadContextKey.context); allHeaders.push(...((_a = store.get(ThreadContextKey.additionalHeaders)) !== null && _a !== void 0 ? _a : [])); allHeaders.push(...objectToContextHeaders((_b = store.get(ThreadContextKey.context)) !== null && _b !== void 0 ? _b : {})); return allHeaders; } export const addContextHeadersToThreadContext = (headers) => { const store = threadContext.getStore(); store === null || store === void 0 ? void 0 : store.set(ThreadContextKey.context, contextHeadersToObject(headers !== null && headers !== void 0 ? headers : [])); }; //# sourceMappingURL=threadContext.js.map