Skip to content

Commit 9d0472b

Browse files
author
Luca Forstner
authored
feat(vercel-edge): Stop using hub (#11539)
1 parent c8bc80b commit 9d0472b

File tree

1 file changed

+45
-48
lines changed

1 file changed

+45
-48
lines changed

packages/vercel-edge/src/async.ts

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
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';
37
import type { Hub, Scope } from '@sentry/types';
48
import { GLOBAL_OBJ, logger } from '@sentry/utils';
59

@@ -11,7 +15,7 @@ interface AsyncLocalStorage<T> {
1115
run<R, TArgs extends any[]>(store: T, callback: (...args: TArgs) => R, ...args: TArgs): R;
1216
}
1317

14-
let asyncStorage: AsyncLocalStorage<Hub>;
18+
let asyncStorage: AsyncLocalStorage<{ scope: Scope; isolationScope: Scope }>;
1519

1620
/**
1721
* Sets the async context strategy to use AsyncLocalStorage which should be available in the edge runtime.
@@ -32,68 +36,63 @@ export function setAsyncLocalStorageAsyncContextStrategy(): void {
3236
asyncStorage = new MaybeGlobalAsyncLocalStorage();
3337
}
3438

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+
};
3752
}
3853

3954
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+
};
4168
}
4269

4370
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 }, () => {
5474
return callback(scope);
5575
});
5676
}
5777

5878
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 }, () => {
6881
return callback(scope);
6982
});
7083
}
7184

7285
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 }, () => {
8389
return callback(isolationScope);
8490
});
8591
}
8692

8793
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 }, () => {
9796
return callback(isolationScope);
9897
});
9998
}
@@ -104,9 +103,7 @@ export function setAsyncLocalStorageAsyncContextStrategy(): void {
104103
withSetScope,
105104
withIsolationScope,
106105
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,
111108
});
112109
}

0 commit comments

Comments
 (0)