UNPKG

@dalet-oss/express-http-context

Version:

Modern request-scoped storage support for Express.js based on Asynchronous Local Storage.

43 lines 1.37 kB
import { AsyncLocalStorage } from 'async_hooks'; if (!global.expressHttpContextStorage) { global.expressHttpContextStorage = new AsyncLocalStorage(); } /** * Express.js middleware that is responsible for initializing the context for * each request. * * Declared as type 'any' because otherwise Typescript consumers can get tie * themselves in knots. */ export const middleware = (_req, _res, next) => { global.expressHttpContextStorage.run(new Map(), () => next()); }; /** * Gets a value from the context by key. * * Will return undefined if the context has not yet been initialized for this * request, or if a value is not found for the specified key. */ export function get(key) { const store = global.expressHttpContextStorage.getStore(); return store?.get(key); } /** * Adds a value to the context by key. * * If the key already exists, its value will be overwritten. * No value will be persisted if the context has not yet been initialized. * * Returns the value that was set, or undefined if the context has not yet been * initialized for this request. */ export function set(key, value) { const store = global.expressHttpContextStorage.getStore(); if (store) { store.set(key, value); return value; } return undefined; } export default { get, middleware, set }; //# sourceMappingURL=index.js.map