UNPKG

@sentry/nextjs

Version:
48 lines (39 loc) • 1.86 kB
import { debug, GLOBAL_OBJ } from '@sentry/core'; import { DEBUG_BUILD } from '../common/debug-build.js'; // Inline AsyncLocalStorage interface from current types // Avoids conflict with resolving it from getBuiltinModule /** * Prepares the global object to generate safe random IDs in cache components contexts * See: https://github.com/getsentry/sentry-javascript/blob/ceb003c15973c2d8f437dfb7025eedffbc8bc8b0/packages/core/src/utils/propagationContext.ts#L1 */ function prepareSafeIdGeneratorContext() { const sym = Symbol.for('__SENTRY_SAFE_RANDOM_ID_WRAPPER__'); const globalWithSymbol = GLOBAL_OBJ; const als = getAsyncLocalStorage(); if (!als || typeof als.snapshot !== 'function') { DEBUG_BUILD && debug.warn( '[@sentry/nextjs] No AsyncLocalStorage found in the runtime or AsyncLocalStorage.snapshot() is not available, skipping safe random ID generator context preparation, you may see some errors with cache components.', ); return; } globalWithSymbol[sym] = als.snapshot(); DEBUG_BUILD && debug.log('[@sentry/nextjs] Prepared safe random ID generator context'); } function getAsyncLocalStorage() { // May exist in the Next.js runtime globals // Doesn't exist in some of our tests if (typeof AsyncLocalStorage !== 'undefined') { return AsyncLocalStorage; } // Try to resolve it dynamically without synchronously importing the module // This is done to avoid importing the module synchronously at the top // which means this is safe across runtimes if ('getBuiltinModule' in process && typeof process.getBuiltinModule === 'function') { const { AsyncLocalStorage } = process.getBuiltinModule('async_hooks') ?? {}; return AsyncLocalStorage ; } return undefined; } export { prepareSafeIdGeneratorContext }; //# sourceMappingURL=prepareSafeIdGeneratorContext.js.map