Skip to content

Commit 9904d28

Browse files
committed
use stack to set AsyncStackStrategy; use hub via getCurrentHub
1 parent dc10726 commit 9904d28

File tree

4 files changed

+27
-27
lines changed

4 files changed

+27
-27
lines changed

dev-packages/browser-integration-tests/suites/old-sdk-interop/hub/subject.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
console.log(window.__SENTRY__);
12
/**
23
* Simulate an old pre v8 SDK obtaining the hub from the global sentry carrier
34
* and checking for the hub version.

packages/core/src/asyncContext/stackStrategy.ts

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import type { Client, Scope as ScopeInterface } from '@sentry/types';
2-
import { isThenable } from '@sentry/utils';
1+
import type { Client, Hub, Scope as ScopeInterface } from '@sentry/types';
2+
import { GLOBAL_OBJ, getGlobalSingleton, isThenable, logger } from '@sentry/utils';
33
import { getDefaultCurrentScope, getDefaultIsolationScope } from '../currentScopes';
44
import { Scope } from '../scope';
55

6-
import { getMainCarrier, getSentryCarrier } from './../carrier';
6+
import { Carrier, getMainCarrier, getSentryCarrier } from '../carrier';
7+
import { getCurrentHubShim } from '../getCurrentHubShim';
78
import type { AsyncContextStrategy } from './types';
89

910
interface Layer {
@@ -104,16 +105,6 @@ export class AsyncContextStack {
104105
return this._stack[this._stack.length - 1];
105106
}
106107

107-
/**
108-
* Only here for compatibility with <v8 versions
109-
* @deprecated DO NOT use this function
110-
* @hidden
111-
* @internal
112-
*/
113-
public isOlderThan(_version: number): boolean {
114-
return false;
115-
}
116-
117108
/**
118109
* Push a scope to the stack.
119110
*/
@@ -141,20 +132,17 @@ export class AsyncContextStack {
141132
* This will be removed during the v8 cycle and is only here to make migration easier.
142133
*/
143134
function getAsyncContextStack(): AsyncContextStack {
144-
const registry = getMainCarrier();
145-
146-
// For now we continue to keep this as `hub` on the ACS,
147-
// as e.g. the Loader Script relies on this.
148-
// Eventually we may change this if/when we update the loader to not require this field anymore
149-
// Related, we also write to `hub` in {@link ./../sdk.ts registerClientOnGlobalHub}
150-
const sentry = getSentryCarrier(registry) as { hub?: AsyncContextStack };
151-
152-
if (sentry.hub) {
153-
return sentry.hub;
135+
// eslint-disable-next-line deprecation/deprecation
136+
const sentry = getSentryCarrier(getMainCarrier()) as { hub?: AsyncContextStack };
137+
if (!sentry.hub) {
138+
// For now, we still set the `hub` object to gracefully handle pre v8 SDKs and certain scripts
139+
// (e.g. our loader script) accessing properties on `window.__SENTRY__.hub`.
140+
// This will just call the `getCurrentHubShim` function, as if users would interact with `getCurrentHub`.
141+
// eslint-disable-next-line deprecation/deprecation
142+
Object.defineProperty(sentry, 'hub', { get: () => getCurrentHubShim(), enumerable: true });
154143
}
155144

156-
sentry.hub = new AsyncContextStack(getDefaultCurrentScope(), getDefaultIsolationScope());
157-
return sentry.hub;
145+
return getGlobalSingleton('stack', () => new AsyncContextStack(getDefaultCurrentScope(), getDefaultIsolationScope()));
158146
}
159147

160148
function withScope<T>(callback: (scope: ScopeInterface) => T): T {

packages/core/src/getCurrentHubShim.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Client, EventHint, Hub, Integration, IntegrationClass, SeverityLevel } from '@sentry/types';
1+
import type { Client, EventHint, Hub, Integration, IntegrationClass, Scope, SeverityLevel } from '@sentry/types';
22
import { addBreadcrumb } from './breadcrumbs';
33
import { getClient, getCurrentScope, getIsolationScope, withScope } from './currentScopes';
44
import {
@@ -22,7 +22,10 @@ import {
2222
* usage
2323
*/
2424
// eslint-disable-next-line deprecation/deprecation
25-
export function getCurrentHubShim(): Hub {
25+
export function getCurrentHubShim(): Hub & {
26+
getStackTop: () => { client: Client | undefined; scope: Scope };
27+
isOlderThan: () => boolean;
28+
} {
2629
return {
2730
bindClient(client: Client): void {
2831
const scope = getCurrentScope();
@@ -64,6 +67,13 @@ export function getCurrentHubShim(): Hub {
6467
// only send the update
6568
_sendSessionUpdate();
6669
},
70+
getStackTop: () => ({
71+
client: getClient(),
72+
scope: getCurrentScope(),
73+
}),
74+
isOlderThan() {
75+
return false;
76+
},
6777
};
6878
}
6979

packages/utils/src/worldwide.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export interface InternalGlobal {
4545
_sentryDebugIds?: Record<string, string>;
4646
__SENTRY__: {
4747
hub: any;
48+
stack: any;
4849
logger: any;
4950
extensions?: {
5051
/** Extension methods for the hub, which are bound to the current Hub instance */

0 commit comments

Comments
 (0)