Skip to content

Commit 8a45505

Browse files
committed
ref(core): Use versioned carrier on global object
1 parent c600ac8 commit 8a45505

File tree

13 files changed

+104
-52
lines changed

13 files changed

+104
-52
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
/**
4+
* This simulates an old, pre-v8 SDK setting itself up on the global __SENTRY__ carrier.
5+
*/
6+
window.__SENTRY__ = {
7+
hub: {
8+
isOlderThan: version => {
9+
return version < 7;
10+
},
11+
},
12+
};
13+
14+
window.Sentry = Sentry;
15+
16+
Sentry.init({
17+
dsn: 'https://[email protected]/1337',
18+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const sentryCarrier = window && window.__SENTRY__;
2+
3+
/**
4+
* Simulate an old pre v8 SDK obtaining the hub from the global sentry carrier
5+
* and checking for the hub version.
6+
*/
7+
const res = sentryCarrier.hub && sentryCarrier.hub.isOlderThan(7);
8+
9+
// Write back result into the document
10+
document.getElementById('olderThan').innerText = res;
11+
12+
console.log(sentryCarrier);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
</head>
6+
<body>
7+
<p id="olderThan"></p>
8+
</body>
9+
</html>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { expect } from '@playwright/test';
2+
3+
import { sentryTest } from '../../../utils/fixtures';
4+
5+
sentryTest(
6+
"doesn't crash if older SDKs access `hub.isOlderThan` on the global object",
7+
async ({ getLocalTestUrl, page }) => {
8+
const url = await getLocalTestUrl({ testDir: __dirname });
9+
await page.goto(url);
10+
11+
await expect(page.locator('#olderThan')).toHaveText('false');
12+
},
13+
);

packages/core/src/asyncContext/stackStrategy.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,29 +132,19 @@ export class AsyncContextStack {
132132
*/
133133
function getAsyncContextStack(): AsyncContextStack {
134134
const registry = getMainCarrier();
135+
const sentry = getSentryCarrier(registry);
135136

136-
// For now we continue to keep this as `hub` on the ACS,
137-
// as e.g. the Loader Script relies on this.
138-
// Eventually we may change this if/when we update the loader to not require this field anymore
139-
// Related, we also write to `hub` in {@link ./../sdk.ts registerClientOnGlobalHub}
140-
const sentry = getSentryCarrier(registry) as { hub?: AsyncContextStack };
141-
142-
if (sentry.hub) {
143-
return sentry.hub;
144-
}
145-
146-
sentry.hub = new AsyncContextStack(getDefaultCurrentScope(), getDefaultIsolationScope());
147-
return sentry.hub;
137+
return (sentry.stack = sentry.stack || new AsyncContextStack(getDefaultCurrentScope(), getDefaultIsolationScope()));
148138
}
149139

150140
function withScope<T>(callback: (scope: ScopeInterface) => T): T {
151141
return getAsyncContextStack().withScope(callback);
152142
}
153143

154144
function withSetScope<T>(scope: ScopeInterface, callback: (scope: ScopeInterface) => T): T {
155-
const hub = getAsyncContextStack() as AsyncContextStack;
156-
return hub.withScope(() => {
157-
hub.getStackTop().scope = scope;
145+
const stack = getAsyncContextStack() as AsyncContextStack;
146+
return stack.withScope(() => {
147+
stack.getStackTop().scope = scope;
158148
return callback(scope);
159149
});
160150
}

packages/core/src/carrier.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import type { Integration, VersionString } from '@sentry/types';
1+
import type { Integration, Scope, VersionString } from '@sentry/types';
22
import { GLOBAL_OBJ } from '@sentry/utils';
33
import type { AsyncContextStack } from './asyncContext/stackStrategy';
44
import type { AsyncContextStrategy } from './asyncContext/types';
55
import { SDK_VERSION } from './version';
66

77
/**
8-
* An object that contains a hub and maintains a scope stack.
8+
* An object that contains globally accessible properties and maintains a scope stack.
99
* @hidden
1010
*/
1111
export interface Carrier {
@@ -29,6 +29,10 @@ interface SentryCarrier {
2929
[key: string]: Function;
3030
};
3131
stack?: AsyncContextStack;
32+
33+
globalScope?: Scope;
34+
defaultIsolationScope?: Scope;
35+
defaultCurrentScope?: Scope;
3236
}
3337

3438
/**

packages/core/src/integration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export function setupIntegration(client: Client, integration: Integration, integ
146146
DEBUG_BUILD && logger.log(`Integration installed: ${integration.name}`);
147147
}
148148

149-
/** Add an integration to the current hub's client. */
149+
/** Add an integration to the current scope's client. */
150150
export function addIntegration(integration: Integration): void {
151151
const client = getClient();
152152

packages/core/src/sdk.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function setCurrentClient(client: Client): void {
4747
}
4848

4949
/**
50-
* Unfortunately, we still have to manually bind the client to the "hub" property set on the global
50+
* Unfortunately, we still have to manually bind the client to the "stack" property set on the global
5151
* Sentry carrier object. This is because certain scripts (e.g. our loader script) obtain
5252
* the client via `window.__SENTRY__[version].stack.getClient()`.
5353
*

packages/core/src/utils/prepareEvent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export function prepareEvent(
7676
const clientEventProcessors = client ? client.getEventProcessors() : [];
7777

7878
// This should be the last thing called, since we want that
79-
// {@link Hub.addEventProcessor} gets the finished prepared event.
79+
// {@link Scope.addEventProcessor} gets the finished prepared event.
8080
// Merge scope data together
8181
const data = getGlobalScope().getScopeData();
8282

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { GLOBAL_OBJ } from '@sentry/utils';
2+
import { getSentryCarrier } from '../../src/carrier';
23

34
export function clearGlobalScope() {
4-
const __SENTRY__ = (GLOBAL_OBJ.__SENTRY__ = GLOBAL_OBJ.__SENTRY__ || {});
5-
__SENTRY__.globalScope = undefined;
5+
const carrier = getSentryCarrier(GLOBAL_OBJ);
6+
carrier.globalScope = undefined;
67
}

0 commit comments

Comments
 (0)