1
- import { Hub as HubClass , getGlobalHub } from '@sentry/core' ;
2
- import { setAsyncContextStrategy } from '@sentry/core' ;
1
+ import {
2
+ getCurrentHubShim ,
3
+ getDefaultCurrentScope ,
4
+ getDefaultIsolationScope ,
5
+ setAsyncContextStrategy ,
6
+ } from '@sentry/core' ;
3
7
import type { Hub , Scope } from '@sentry/types' ;
4
8
import { GLOBAL_OBJ , logger } from '@sentry/utils' ;
5
9
@@ -11,7 +15,7 @@ interface AsyncLocalStorage<T> {
11
15
run < R , TArgs extends any [ ] > ( store : T , callback : ( ...args : TArgs ) => R , ...args : TArgs ) : R ;
12
16
}
13
17
14
- let asyncStorage : AsyncLocalStorage < Hub > ;
18
+ let asyncStorage : AsyncLocalStorage < { scope : Scope ; isolationScope : Scope } > ;
15
19
16
20
/**
17
21
* Sets the async context strategy to use AsyncLocalStorage which should be available in the edge runtime.
@@ -32,68 +36,63 @@ export function setAsyncLocalStorageAsyncContextStrategy(): void {
32
36
asyncStorage = new MaybeGlobalAsyncLocalStorage ( ) ;
33
37
}
34
38
35
- function getCurrentAsyncStorageHub ( ) : Hub | undefined {
36
- return asyncStorage . getStore ( ) ;
39
+ function getScopes ( ) : { scope : Scope ; isolationScope : Scope } {
40
+ const scopes = asyncStorage . getStore ( ) ;
41
+
42
+ if ( scopes ) {
43
+ return scopes ;
44
+ }
45
+
46
+ // fallback behavior:
47
+ // if, for whatever reason, we can't find scopes on the context here, we have to fix this somehow
48
+ return {
49
+ scope : getDefaultCurrentScope ( ) ,
50
+ isolationScope : getDefaultIsolationScope ( ) ,
51
+ } ;
37
52
}
38
53
39
54
function getCurrentHub ( ) : Hub {
40
- return getCurrentAsyncStorageHub ( ) || getGlobalHub ( ) ;
55
+ // eslint-disable-next-line deprecation/deprecation
56
+ const hub = getCurrentHubShim ( ) ;
57
+ return {
58
+ ...hub ,
59
+ getScope : ( ) => {
60
+ const scopes = getScopes ( ) ;
61
+ return scopes . scope ;
62
+ } ,
63
+ getIsolationScope : ( ) => {
64
+ const scopes = getScopes ( ) ;
65
+ return scopes . isolationScope ;
66
+ } ,
67
+ } ;
41
68
}
42
69
43
70
function withScope < T > ( callback : ( scope : Scope ) => T ) : T {
44
- const parentHub = getCurrentHub ( ) ;
45
-
46
- /* eslint-disable deprecation/deprecation */
47
- const client = parentHub . getClient ( ) ;
48
- const scope = parentHub . getScope ( ) . clone ( ) ;
49
- const isolationScope = parentHub . getIsolationScope ( ) ;
50
- const newHub = new HubClass ( client , scope , isolationScope ) ;
51
- /* eslint-enable deprecation/deprecation */
52
-
53
- return asyncStorage . run ( newHub , ( ) => {
71
+ const scope = getScopes ( ) . scope . clone ( ) ;
72
+ const isolationScope = getScopes ( ) . isolationScope ;
73
+ return asyncStorage . run ( { scope, isolationScope } , ( ) => {
54
74
return callback ( scope ) ;
55
75
} ) ;
56
76
}
57
77
58
78
function withSetScope < T > ( scope : Scope , callback : ( scope : Scope ) => T ) : T {
59
- const parentHub = getCurrentHub ( ) ;
60
-
61
- /* eslint-disable deprecation/deprecation */
62
- const client = parentHub . getClient ( ) ;
63
- const isolationScope = parentHub . getIsolationScope ( ) ;
64
- const newHub = new HubClass ( client , scope , isolationScope ) ;
65
- /* eslint-enable deprecation/deprecation */
66
-
67
- return asyncStorage . run ( newHub , ( ) => {
79
+ const isolationScope = getScopes ( ) . isolationScope . clone ( ) ;
80
+ return asyncStorage . run ( { scope, isolationScope } , ( ) => {
68
81
return callback ( scope ) ;
69
82
} ) ;
70
83
}
71
84
72
85
function withIsolationScope < T > ( callback : ( isolationScope : Scope ) => T ) : T {
73
- const parentHub = getCurrentHub ( ) ;
74
-
75
- /* eslint-disable deprecation/deprecation */
76
- const client = parentHub . getClient ( ) ;
77
- const scope = parentHub . getScope ( ) . clone ( ) ;
78
- const isolationScope = parentHub . getIsolationScope ( ) . clone ( ) ;
79
- const newHub = new HubClass ( client , scope , isolationScope ) ;
80
- /* eslint-enable deprecation/deprecation */
81
-
82
- return asyncStorage . run ( newHub , ( ) => {
86
+ const scope = getScopes ( ) . scope ;
87
+ const isolationScope = getScopes ( ) . isolationScope . clone ( ) ;
88
+ return asyncStorage . run ( { scope, isolationScope } , ( ) => {
83
89
return callback ( isolationScope ) ;
84
90
} ) ;
85
91
}
86
92
87
93
function withSetIsolationScope < T > ( isolationScope : Scope , callback : ( isolationScope : Scope ) => T ) : T {
88
- const parentHub = getCurrentHub ( ) ;
89
-
90
- /* eslint-disable deprecation/deprecation */
91
- const client = parentHub . getClient ( ) ;
92
- const scope = parentHub . getScope ( ) . clone ( ) ;
93
- const newHub = new HubClass ( client , scope , isolationScope ) ;
94
- /* eslint-enable deprecation/deprecation */
95
-
96
- return asyncStorage . run ( newHub , ( ) => {
94
+ const scope = getScopes ( ) . scope ;
95
+ return asyncStorage . run ( { scope, isolationScope } , ( ) => {
97
96
return callback ( isolationScope ) ;
98
97
} ) ;
99
98
}
@@ -104,9 +103,7 @@ export function setAsyncLocalStorageAsyncContextStrategy(): void {
104
103
withSetScope,
105
104
withIsolationScope,
106
105
withSetIsolationScope,
107
- // eslint-disable-next-line deprecation/deprecation
108
- getCurrentScope : ( ) => getCurrentHub ( ) . getScope ( ) ,
109
- // eslint-disable-next-line deprecation/deprecation
110
- getIsolationScope : ( ) => getCurrentHub ( ) . getIsolationScope ( ) ,
106
+ getCurrentScope : ( ) => getScopes ( ) . scope ,
107
+ getIsolationScope : ( ) => getScopes ( ) . isolationScope ,
111
108
} ) ;
112
109
}
0 commit comments