{"version":3,"file":"index.cjs","names":["AsyncLocalStorage"],"sources":["../src/index.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks'\nimport type { NextFunction, Request, Response } from 'express'\n\ntype Store = Map<string, any>\n\n// Several copies of this package can be loaded at once: the CommonJS and ESM\n// builds, or different versions required by an app and by its dependencies.\n// They all share one AsyncLocalStorage, registered globally, so a value set\n// through one copy can be read through any other.\n// See https://github.com/artberri/express-http-context2/issues/1\nconst storageKey: unique symbol = Symbol.for('express-http-context2.storage')\nconst requestStoreKey: unique symbol = Symbol.for('express-http-context2.store')\n\nconst registry = globalThis as typeof globalThis & {\n\t[storageKey]?: AsyncLocalStorage<Store>\n}\nconst asyncLocalStorage: AsyncLocalStorage<Store> = (registry[storageKey] ??=\n\tnew AsyncLocalStorage<Store>())\n\n/**\n * Returns the store of this request, creating it the first time.\n * Keeping it on the request means that mounting the middleware more than\n * once (for example by the app and by a library) keeps the values already set.\n */\nfunction getRequestStore(req: Request): Store {\n\tconst request = req as Request & { [requestStoreKey]?: Store }\n\tlet store = request[requestStoreKey]\n\tif (!store) {\n\t\tstore = new Map()\n\t\tObject.defineProperty(request, requestStoreKey, { value: store })\n\t}\n\n\treturn store\n}\n\n/**\n * Express.js middleware that is responsible for initializing the context for each request.\n */\nexport function middleware(\n\treq: Request,\n\tres: Response,\n\tnext: NextFunction,\n): void {\n\tasyncLocalStorage.run(getRequestStore(req), () => next())\n}\n\n/**\n * Gets a value from the context by key.\n * Will return undefined if the context has not yet been initialized for this request\n * or if a value is not found for the specified key.\n */\nexport function get<T = any>(key: string): T | undefined {\n\treturn asyncLocalStorage.getStore()?.get(key)\n}\n\n/**\n * Adds a value to the context by key.\n * If the key already exists, its value will be overwritten.\n * No value will persist if the context has not yet been initialized.\n */\nexport function set<T = any>(key: string, value: T): void {\n\tasyncLocalStorage.getStore()?.set(key, value)\n}\n\nconst httpContext: {\n\tget: typeof get\n\tmiddleware: typeof middleware\n\tset: typeof set\n} = { get, middleware, set }\n\nexport default httpContext\n"],"mappings":";;;;;;AAUA,MAAM,aAA4B,OAAO,IAAI,+BAA+B;AAC5E,MAAM,kBAAiC,OAAO,IAAI,6BAA6B;AAE/E,MAAM,WAAW;AAGjB,MAAM,oBAA+C,SAAS,gBAC7D,IAAIA,iBAAAA,kBAAyB;;;;;;AAO9B,SAAS,gBAAgB,KAAqB;CAC7C,MAAM,UAAU;CAChB,IAAI,QAAQ,QAAQ;CACpB,IAAI,CAAC,OAAO;EACX,wBAAQ,IAAI,IAAI;EAChB,OAAO,eAAe,SAAS,iBAAiB,EAAE,OAAO,MAAM,CAAC;CACjE;CAEA,OAAO;AACR;;;;AAKA,SAAgB,WACf,KACA,KACA,MACO;CACP,kBAAkB,IAAI,gBAAgB,GAAG,SAAS,KAAK,CAAC;AACzD;;;;;;AAOA,SAAgB,IAAa,KAA4B;CACxD,OAAO,kBAAkB,SAAS,CAAC,EAAE,IAAI,GAAG;AAC7C;;;;;;AAOA,SAAgB,IAAa,KAAa,OAAgB;CACzD,kBAAkB,SAAS,CAAC,EAAE,IAAI,KAAK,KAAK;AAC7C;AAEA,MAAM,cAIF;CAAE;CAAK;CAAY;AAAI"}