1- import { debug , GLOBAL_OBJ } from '@sentry/core' ;
1+ import { type _INTERNAL_RandomSafeContextRunner as RandomSafeContextRunner , debug , GLOBAL_OBJ } from '@sentry/core' ;
22import { DEBUG_BUILD } from '../common/debug-build' ;
33
4- type SafeRandomContextRunner = < T > ( callback : ( ) => T ) => T ;
5-
4+ // Inline AsyncLocalStorage interface from current types
5+ // Avoids conflict with resolving it from getBuiltinModule
66type OriginalAsyncLocalStorage = typeof AsyncLocalStorage ;
77
88/**
@@ -11,7 +11,7 @@ type OriginalAsyncLocalStorage = typeof AsyncLocalStorage;
1111 */
1212export function prepareSafeIdGeneratorContext ( ) : void {
1313 const sym = Symbol . for ( '__SENTRY_SAFE_RANDOM_ID_WRAPPER__' ) ;
14- const globalWithSymbol : typeof GLOBAL_OBJ & { [ sym ] ?: SafeRandomContextRunner } = GLOBAL_OBJ ;
14+ const globalWithSymbol : typeof GLOBAL_OBJ & { [ sym ] ?: RandomSafeContextRunner } = GLOBAL_OBJ ;
1515 const als = getAsyncLocalStorage ( ) ;
1616 if ( ! als ) {
1717 DEBUG_BUILD &&
@@ -27,11 +27,14 @@ export function prepareSafeIdGeneratorContext(): void {
2727
2828function getAsyncLocalStorage ( ) : OriginalAsyncLocalStorage | undefined {
2929 // May exist in the Next.js runtime globals
30- if ( 'AsyncLocalStorage' in GLOBAL_OBJ ) {
30+ // Doesn't exist in some of our tests
31+ if ( typeof AsyncLocalStorage !== 'undefined' ) {
3132 return AsyncLocalStorage ;
3233 }
3334
3435 // Try to resolve it dynamically without synchronously importing the module
36+ // This is done to avoid importing the module synchronously at the top
37+ // which means this is safe across runtimes
3538 if ( 'getBuiltinModule' in process && typeof process . getBuiltinModule === 'function' ) {
3639 const { AsyncLocalStorage } = process . getBuiltinModule ( 'async_hooks' ) ?? { } ;
3740
0 commit comments