{"version":3,"file":"currentScopes.js","sources":["../../src/currentScopes.ts"],"sourcesContent":["import { getAsyncContextStrategy } from './asyncContext';\nimport { getGlobalSingleton, getMainCarrier } from './carrier';\nimport type { Client } from './client';\nimport { Scope } from './scope';\nimport type { TraceContext } from './types/context';\nimport { generateSpanId } from './utils/propagationContext';\n\nlet _externalPropagationContextProvider: (() => { traceId: string; spanId: string } | undefined) | undefined;\n\n/**\n * Register an external propagation context provider function.\n * When registered, trace context will be read from the external source (e.g. OpenTelemetry)\n * instead of from the Sentry scope's propagation context.\n */\nexport function registerExternalPropagationContext(fn: () => { traceId: string; spanId: string } | undefined): void {\n _externalPropagationContextProvider = fn;\n}\n\n/**\n * Get the external propagation context, if a provider has been registered.\n */\nexport function getExternalPropagationContext(): { traceId: string; spanId: string } | undefined {\n return _externalPropagationContextProvider?.();\n}\n\n/**\n * Check if an external propagation context provider has been registered.\n */\nexport function hasExternalPropagationContext(): boolean {\n return _externalPropagationContextProvider !== undefined;\n}\n\n/**\n * Get the currently active scope.\n */\nexport function getCurrentScope(): Scope {\n const carrier = getMainCarrier();\n const acs = getAsyncContextStrategy(carrier);\n return acs.getCurrentScope();\n}\n\n/**\n * Get the currently active isolation scope.\n * The isolation scope is active for the current execution context.\n */\nexport function getIsolationScope(): Scope {\n const carrier = getMainCarrier();\n const acs = getAsyncContextStrategy(carrier);\n return acs.getIsolationScope();\n}\n\n/**\n * Get the global scope.\n * This scope is applied to _all_ events.\n */\nexport function getGlobalScope(): Scope {\n return getGlobalSingleton('globalScope', () => new Scope());\n}\n\n/**\n * Creates a new scope with and executes the given operation within.\n * The scope is automatically removed once the operation\n * finishes or throws.\n */\nexport function withScope(callback: (scope: Scope) => T): T;\n/**\n * Set the given scope as the active scope in the callback.\n */\nexport function withScope(scope: Scope | undefined, callback: (scope: Scope) => T): T;\n/**\n * Either creates a new active scope, or sets the given scope as active scope in the given callback.\n */\nexport function withScope(\n ...rest: [callback: (scope: Scope) => T] | [scope: Scope | undefined, callback: (scope: Scope) => T]\n): T {\n const carrier = getMainCarrier();\n const acs = getAsyncContextStrategy(carrier);\n\n // If a scope is defined, we want to make this the active scope instead of the default one\n if (rest.length === 2) {\n const [scope, callback] = rest;\n\n if (!scope) {\n return acs.withScope(callback);\n }\n\n return acs.withSetScope(scope, callback);\n }\n\n return acs.withScope(rest[0]);\n}\n\n/**\n * Attempts to fork the current isolation scope and the current scope based on the current async context strategy. If no\n * async context strategy is set, the isolation scope and the current scope will not be forked (this is currently the\n * case, for example, in the browser).\n *\n * Usage of this function in environments without async context strategy is discouraged and may lead to unexpected behaviour.\n *\n * This function is intended for Sentry SDK and SDK integration development. It is not recommended to be used in \"normal\"\n * applications directly because it comes with pitfalls. Use at your own risk!\n */\nexport function withIsolationScope(callback: (isolationScope: Scope) => T): T;\n/**\n * Set the provided isolation scope as active in the given callback. If no\n * async context strategy is set, the isolation scope and the current scope will not be forked (this is currently the\n * case, for example, in the browser).\n *\n * Usage of this function in environments without async context strategy is discouraged and may lead to unexpected behaviour.\n *\n * This function is intended for Sentry SDK and SDK integration development. It is not recommended to be used in \"normal\"\n * applications directly because it comes with pitfalls. Use at your own risk!\n *\n * If you pass in `undefined` as a scope, it will fork a new isolation scope, the same as if no scope is passed.\n */\nexport function withIsolationScope(isolationScope: Scope | undefined, callback: (isolationScope: Scope) => T): T;\n/**\n * Either creates a new active isolation scope, or sets the given isolation scope as active scope in the given callback.\n */\nexport function withIsolationScope(\n ...rest:\n | [callback: (isolationScope: Scope) => T]\n | [isolationScope: Scope | undefined, callback: (isolationScope: Scope) => T]\n): T {\n const carrier = getMainCarrier();\n const acs = getAsyncContextStrategy(carrier);\n\n // If a scope is defined, we want to make this the active scope instead of the default one\n if (rest.length === 2) {\n const [isolationScope, callback] = rest;\n\n if (!isolationScope) {\n return acs.withIsolationScope(callback);\n }\n\n return acs.withSetIsolationScope(isolationScope, callback);\n }\n\n return acs.withIsolationScope(rest[0]);\n}\n\n/**\n * Get the currently active client.\n */\nexport function getClient(): C | undefined {\n return getCurrentScope().getClient();\n}\n\n/**\n * Get a trace context for the given scope.\n */\nexport function getTraceContextFromScope(scope: Scope): TraceContext {\n const externalContext = getExternalPropagationContext();\n if (externalContext) {\n return { trace_id: externalContext.traceId, span_id: externalContext.spanId };\n }\n\n const propagationContext = scope.getPropagationContext();\n\n const { traceId, parentSpanId, propagationSpanId } = propagationContext;\n\n const traceContext: TraceContext = {\n trace_id: traceId,\n span_id: propagationSpanId || generateSpanId(),\n };\n\n if (parentSpanId) {\n traceContext.parent_span_id = parentSpanId;\n }\n\n return traceContext;\n}\n"],"names":["carrier","getMainCarrier","getAsyncContextStrategy","getGlobalSingleton","Scope","propagationContext","generateSpanId"],"mappings":";;;;;;;AAOA,IAAI,mCAAA;AAOG,SAAS,mCAAmC,EAAA,EAAiE;AAClH,EAAA,mCAAA,GAAsC,EAAA;AACxC;AAKO,SAAS,6BAAA,GAAiF;AAC/F,EAAA,OAAO,mCAAA,IAAsC;AAC/C;AAKO,SAAS,6BAAA,GAAyC;AACvD,EAAA,OAAO,mCAAA,KAAwC,MAAA;AACjD;AAKO,SAAS,eAAA,GAAyB;AACvC,EAAA,MAAMA,YAAUC,sBAAA,EAAe;AAC/B,EAAA,MAAM,GAAA,GAAMC,8BAAwBF,SAAO,CAAA;AAC3C,EAAA,OAAO,IAAI,eAAA,EAAgB;AAC7B;AAMO,SAAS,iBAAA,GAA2B;AACzC,EAAA,MAAMA,YAAUC,sBAAA,EAAe;AAC/B,EAAA,MAAM,GAAA,GAAMC,8BAAwBF,SAAO,CAAA;AAC3C,EAAA,OAAO,IAAI,iBAAA,EAAkB;AAC/B;AAMO,SAAS,cAAA,GAAwB;AACtC,EAAA,OAAOG,0BAAA,CAAmB,aAAA,EAAe,MAAM,IAAIC,aAAO,CAAA;AAC5D;AAeO,SAAS,aACX,IAAA,EACA;AACH,EAAA,MAAMJ,YAAUC,sBAAA,EAAe;AAC/B,EAAA,MAAM,GAAA,GAAMC,8BAAwBF,SAAO,CAAA;AAG3C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