123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- import { _optionalChain } from '@sentry/utils';
- import { setAsyncContextStrategy, ensureHubOnCarrier, getHubFromCarrier } from '@sentry/core';
- import { GLOBAL_OBJ, logger } from '@sentry/utils';
- import { DEBUG_BUILD } from './debug-build.js';
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
- const MaybeGlobalAsyncLocalStorage = (GLOBAL_OBJ ).AsyncLocalStorage;
- let asyncStorage;
- /**
- * Sets the async context strategy to use AsyncLocalStorage which should be available in the edge runtime.
- */
- function setAsyncLocalStorageAsyncContextStrategy() {
- if (!MaybeGlobalAsyncLocalStorage) {
- DEBUG_BUILD &&
- logger.warn(
- "Tried to register AsyncLocalStorage async context strategy in a runtime that doesn't support AsyncLocalStorage.",
- );
- return;
- }
- if (!asyncStorage) {
- asyncStorage = new MaybeGlobalAsyncLocalStorage();
- }
- function getCurrentHub() {
- return asyncStorage.getStore();
- }
- function createNewHub(parent) {
- const carrier = {};
- ensureHubOnCarrier(carrier, parent);
- return getHubFromCarrier(carrier);
- }
- function runWithAsyncContext(callback, options) {
- const existingHub = getCurrentHub();
- if (existingHub && _optionalChain([options, 'optionalAccess', _ => _.reuseExisting])) {
- // We're already in an async context, so we don't need to create a new one
- // just call the callback with the current hub
- return callback();
- }
- const newHub = createNewHub(existingHub);
- return asyncStorage.run(newHub, () => {
- return callback();
- });
- }
- setAsyncContextStrategy({ getCurrentHub, runWithAsyncContext });
- }
- export { setAsyncLocalStorageAsyncContextStrategy };
- //# sourceMappingURL=async.js.map
|