11import * as domain from 'domain' ;
2- import type { Carrier } from '@sentry/core' ;
32import { getGlobalHub } from '@sentry/core' ;
43import { Hub as HubClass } from '@sentry/core' ;
5- import { ensureHubOnCarrier , getHubFromCarrier , setAsyncContextStrategy , setHubOnCarrier } from '@sentry/core' ;
4+ import { setAsyncContextStrategy } from '@sentry/core' ;
65import type { Client , Hub , Scope } from '@sentry/types' ;
76
7+ type DomainWithHub = domain . Domain & {
8+ hub ?: Hub ;
9+ } ;
10+
811function getActiveDomain < T > ( ) : T | undefined {
912 // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
1013 return ( domain as any ) . active as T | undefined ;
1114}
1215
1316function getCurrentDomainHub ( ) : Hub | undefined {
14- const activeDomain = getActiveDomain < Carrier > ( ) ;
17+ const activeDomain = getActiveDomain < DomainWithHub > ( ) ;
1518
1619 // If there's no active domain, just return undefined and the global hub will be used
1720 if ( ! activeDomain ) {
1821 return undefined ;
1922 }
2023
21- ensureHubOnCarrier ( activeDomain ) ;
24+ if ( activeDomain . hub ) {
25+ return activeDomain . hub ;
26+ }
2227
23- return getHubFromCarrier ( activeDomain ) ;
28+ activeDomain . hub = getCurrentHub ( ) ;
29+ return activeDomain . hub ;
2430}
2531
2632function getCurrentHub ( ) : Hub {
@@ -33,11 +39,11 @@ function withExecutionContext<T>(
3339 isolationScope : Scope ,
3440 callback : ( ) => T ,
3541) : T {
36- const local = domain . create ( ) as domain . Domain & Carrier ;
42+ const local = domain . create ( ) as DomainWithHub ;
3743
3844 // eslint-disable-next-line deprecation/deprecation
3945 const newHub = new HubClass ( client , scope , isolationScope ) ;
40- setHubOnCarrier ( local , newHub ) ;
46+ local . hub = newHub ;
4147
4248 return local . bind ( ( ) => {
4349 return callback ( ) ;
0 commit comments