{"version":3,"file":"storage.cjs","sources":["../../../src/context/storage.ts"],"sourcesContent":["import { Formatter } from '@axiomhq/logging';\nimport { EVENT } from '@axiomhq/logging';\n\nexport type ServerContextFields = Map<string | typeof EVENT, any> | Record<string | typeof EVENT, any>;\nexport const storage = new globalThis.AsyncLocalStorage<ServerContextFields | undefined>();\n\n/**\n * Formatter that adds context fields from the server context to log events.\n *\n * Regular fields are added to the `fields` property of the log event.\n * Properties under the EVENT symbol are added directly to the root of the log event.\n *\n * @example\n * // Using a Map\n * const contextMap = new Map();\n * contextMap.set('userId', 123);\n * contextMap.set(EVENT, { traceId: 'abc123' });\n * runWithServerContext(() => {\n *   logger.info('Request processed');\n *   // Results in a log with fields.userId=123 and traceId at the root\n * }, contextMap);\n *\n * @example\n * // Using an object\n * const contextObj = {\n *   userId: 123,\n *   [EVENT]: { traceId: 'abc123' }\n * };\n * runWithServerContext(() => {\n *   logger.info('Request processed');\n *   // Results in a log with fields.userId=123 and traceId at the root\n * }, contextObj);\n *\n * @param logEvent - The log event to format\n * @returns The formatted log event with server context fields added\n */\nexport const serverContextFieldsFormatter: Formatter = (logEvent) => {\n  const store = storage.getStore();\n  if (!store) {\n    return logEvent;\n  }\n\n  if (store instanceof Map) {\n    // Extract EVENT properties and regular fields separately\n    const eventProps = store.get(EVENT);\n    const regularFields = Object.fromEntries(Array.from(store.entries()).filter(([key]) => key !== EVENT));\n\n    // Apply EVENT properties directly to the root of the log event\n    const result = {\n      ...logEvent,\n      fields: {\n        ...logEvent.fields,\n        ...regularFields,\n      },\n    };\n\n    // Apply EVENT properties to the root if present\n    if (eventProps && typeof eventProps === 'object') {\n      Object.assign(result, eventProps);\n    }\n\n    return result;\n  }\n\n  // For regular objects, extract EVENT properties and regular fields separately\n  const { [EVENT]: eventProps, ...regularFields } = store;\n\n  // Apply regular fields to the fields property\n  const result = {\n    ...logEvent,\n    fields: {\n      ...logEvent.fields,\n      ...regularFields,\n    },\n  };\n\n  // Apply EVENT properties to the root if present\n  if (eventProps && typeof eventProps === 'object') {\n    Object.assign(result, eventProps);\n  }\n\n  return result;\n};\n\n/**\n * Runs a callback with the provided server context.\n * The context will be available to all loggers within the callback.\n *\n * @example\n * // Add regular fields and root-level properties to all logs in a request\n * import { EVENT } from '@axiomhq/logging';\n *\n * runWithServerContext(() => {\n *   // All logs within this callback will have userId in fields and traceId at the root\n *   logger.info('Request started');\n *   processRequest();\n *   logger.info('Request completed');\n * }, {\n *   userId: 123,\n *   requestPath: '/api/users',\n *   [EVENT]: {\n *     traceId: 'abc123',\n *     requestId: 'req-456'\n *   }\n * });\n *\n * @param callback - The function to run with the server context\n * @param store - The context to use, can be a Map or an object with optional EVENT symbol\n * @returns The result of the callback\n */\nexport const runWithServerContext = (callback: () => any, store: ReturnType<typeof storage.getStore>) => {\n  return storage.run(store, callback);\n};\n"],"names":["eventProps","EVENT","regularFields","result"],"mappings":";;;AAIO,MAAM,UAAU,IAAI,WAAW,kBAAA;AAgC/B,MAAM,+BAA0C,CAAC,aAAa;AACnE,QAAM,QAAQ,QAAQ,SAAA;AACtB,MAAI,CAAC,OAAO;AACV,WAAO;AAAA,EACT;AAEA,MAAI,iBAAiB,KAAK;AAExB,UAAMA,cAAa,MAAM,IAAIC,aAAK;AAClC,UAAMC,iBAAgB,OAAO,YAAY,MAAM,KAAK,MAAM,QAAA,CAAS,EAAE,OAAO,CAAC,CAAC,GAAG,MAAM,QAAQD,QAAAA,KAAK,CAAC;AAGrG,UAAME,UAAS;AAAA,MACb,GAAG;AAAA,MACH,QAAQ;AAAA,QACN,GAAG,SAAS;AAAA,QACZ,GAAGD;AAAAA,MAAA;AAAA,IACL;AAIF,QAAIF,eAAc,OAAOA,gBAAe,UAAU;AAChD,aAAO,OAAOG,SAAQH,WAAU;AAAA,IAClC;AAEA,WAAOG;AAAAA,EACT;AAGA,QAAM,EAAE,CAACF,QAAAA,KAAK,GAAG,YAAY,GAAG,kBAAkB;AAGlD,QAAM,SAAS;AAAA,IACb,GAAG;AAAA,IACH,QAAQ;AAAA,MACN,GAAG,SAAS;AAAA,MACZ,GAAG;AAAA,IAAA;AAAA,EACL;AAIF,MAAI,cAAc,OAAO,eAAe,UAAU;AAChD,WAAO,OAAO,QAAQ,UAAU;AAAA,EAClC;AAEA,SAAO;AACT;AA4BO,MAAM,uBAAuB,CAAC,UAAqB,UAA+C;AACvG,SAAO,QAAQ,IAAI,OAAO,QAAQ;AACpC;;;;"}