UNPKG

nats-micro

Version:

NATS micro compatible extra-lightweight microservice library

31 lines 1.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.addContextHeadersToThreadContext = exports.addThreadContextHeaders = exports.ThreadContextKey = exports.threadContext = void 0; const async_hooks_1 = require("async_hooks"); const misc_js_1 = require("./misc.js"); // eslint-disable-next-line @typescript-eslint/no-explicit-any exports.threadContext = new async_hooks_1.AsyncLocalStorage(); var ThreadContextKey; (function (ThreadContextKey) { ThreadContextKey["additionalHeaders"] = "additionalHeaders"; ThreadContextKey["context"] = "context"; })(ThreadContextKey = exports.ThreadContextKey || (exports.ThreadContextKey = {})); function addThreadContextHeaders(headers) { var _a, _b; const store = exports.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(...(0, misc_js_1.objectToContextHeaders)((_b = store.get(ThreadContextKey.context)) !== null && _b !== void 0 ? _b : {})); return allHeaders; } exports.addThreadContextHeaders = addThreadContextHeaders; const addContextHeadersToThreadContext = (headers) => { const store = exports.threadContext.getStore(); store === null || store === void 0 ? void 0 : store.set(ThreadContextKey.context, (0, misc_js_1.contextHeadersToObject)(headers !== null && headers !== void 0 ? headers : [])); }; exports.addContextHeadersToThreadContext = addContextHeadersToThreadContext; //# sourceMappingURL=threadContext.js.map