Skip to content

Commit 924fa82

Browse files
committed
feat: Support versioned Sentry carrier object introduced in 8.6.0+ (#402)
1 parent 2319c4f commit 924fa82

File tree

4 files changed

+693
-119
lines changed

4 files changed

+693
-119
lines changed

.changeset/happy-adults-jam.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@spotlightjs/spotlight': minor
3+
'@spotlightjs/electron': minor
4+
'@spotlightjs/overlay': minor
5+
'@spotlightjs/astro': minor
6+
---
7+
8+
feat: Support versioned Sentry carrier object introduced in 8.6.0+

demos/astro-playground/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"@astrojs/node": "^6.0.4",
1818
"@astrojs/react": "^3.0.5",
1919
"@astrojs/svelte": "^4.0.4",
20-
"@sentry/astro": "^8.0.0-alpha.7",
20+
"@sentry/astro": "8.7.0",
2121
"@spotlightjs/astro": "workspace:*",
2222
"@types/react": "^18.2.37",
2323
"@types/react-dom": "^18.2.15",

packages/overlay/src/integrations/sentry/index.ts

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,6 @@ export default function sentryIntegration(options?: SentryIntegrationOptions) {
9696
} satisfies Integration<Envelope>;
9797
}
9898

99-
type WindowWithSentry = Window & {
100-
__SENTRY__?: {
101-
/** Future-proof v8 way of accessing Sentry APIs via the global */
102-
acs?: {
103-
getCurrentScope: () => {
104-
getClient: () => Client | undefined;
105-
};
106-
};
107-
hub?: {
108-
getClient: () => Client | undefined;
109-
};
110-
};
111-
};
112-
11399
export function processEnvelope(rawEvent: RawEventContext) {
114100
const { data } = rawEvent;
115101
const [rawHeader, ...rawEntries] = data.split(/\n/gm);
@@ -146,6 +132,27 @@ export function processEnvelope(rawEvent: RawEventContext) {
146132
};
147133
}
148134

135+
type V8Carrier = {
136+
stack: {
137+
getScope?: () => {
138+
getClient?: () => Client | undefined;
139+
};
140+
};
141+
};
142+
143+
type LegacyCarrier = {
144+
/** pre-v8 way of accessing client (v7 and earlier) */
145+
hub?: {
146+
getClient?: () => Client | undefined;
147+
};
148+
};
149+
150+
type VersionedCarrier = { version: string } & Record<Exclude<string, 'version'>, V8Carrier>;
151+
152+
type WindowWithSentry = Window & {
153+
__SENTRY__?: LegacyCarrier & VersionedCarrier;
154+
};
155+
149156
/**
150157
* Takes care of injecting spotlight-specific behavior into the Sentry SDK by
151158
* accessing the global __SENTRY__ carrier object.
@@ -167,22 +174,11 @@ function addSpotlightIntegrationToSentry(options?: SentryIntegrationOptions) {
167174
return;
168175
}
169176

170-
const sentryGlobal =
171-
// This is what we expect the v8-stable accessor to be
172-
(window as WindowWithSentry).__SENTRY__?.acs?.getCurrentScope() ||
173-
// This is the current accessor (v7 and v8-alpha)
174-
(window as WindowWithSentry).__SENTRY__?.hub;
175-
176-
if (!sentryGlobal) {
177-
log(
178-
"Couldn't find the Sentry SDK on this page. If you're using a Sentry SDK, make sure you're using version >=7.99.0 or 8.x",
179-
);
180-
return;
181-
}
177+
const sentryCarrier = (window as WindowWithSentry).__SENTRY__;
178+
const sentryClient = sentryCarrier && getSentryClient(sentryCarrier);
182179

183-
const sentryClient = sentryGlobal.getClient();
184180
if (!sentryClient) {
185-
warn("Couldn't find a Sentry SDK client. Make sure you're using a Sentry SDK with version >=7.99.0 or 8.x");
181+
log("Couldn't find a Sentry SDK client. Make sure you're using a Sentry SDK with version >=7.99.0 or 8.x");
186182
return;
187183
}
188184

@@ -211,3 +207,30 @@ function addSpotlightIntegrationToSentry(options?: SentryIntegrationOptions) {
211207

212208
log('Added Spotlight integration to Sentry SDK');
213209
}
210+
211+
/**
212+
* Accesses the `window.__SENTRY__` carrier object and tries to get the Sentry client
213+
* from it. This function supports all carrier object structures from v7 to all versions
214+
* of v8.
215+
*/
216+
function getSentryClient(sentryCarrier: LegacyCarrier & VersionedCarrier): Client | undefined {
217+
// 8.6.0+ way to get the client
218+
if (sentryCarrier.version) {
219+
const versionedCarrier = sentryCarrier[sentryCarrier.version];
220+
const scope =
221+
typeof versionedCarrier?.stack?.getScope === 'function' ? versionedCarrier?.stack?.getScope?.() : undefined;
222+
if (typeof scope?.getClient === 'function') {
223+
return scope.getClient();
224+
}
225+
}
226+
227+
// pre-8.6.0 (+v7) way to get the client
228+
if (sentryCarrier.hub) {
229+
const hub = sentryCarrier.hub;
230+
if (typeof hub.getClient === 'function') {
231+
return hub.getClient();
232+
}
233+
}
234+
235+
return undefined;
236+
}

0 commit comments

Comments
 (0)