From dd10c2a589002bf127e32c6006899c58ae99bcdc Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Wed, 22 May 2024 12:39:07 +0200 Subject: [PATCH 1/4] fix(core): Re-introduce `isOlderThan` to `AsyncContextStack` --- packages/core/src/asyncContext/stackStrategy.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/core/src/asyncContext/stackStrategy.ts b/packages/core/src/asyncContext/stackStrategy.ts index aa075b644e85..19380d7bf739 100644 --- a/packages/core/src/asyncContext/stackStrategy.ts +++ b/packages/core/src/asyncContext/stackStrategy.ts @@ -104,6 +104,16 @@ export class AsyncContextStack { return this._stack[this._stack.length - 1]; } + /** + * Only here for compatibility with Date: Wed, 22 May 2024 12:42:45 +0200 Subject: [PATCH 2/4] add test --- .../suites/old-sdk-interop/hub/init.js | 7 +++++++ .../suites/old-sdk-interop/hub/subject.js | 8 ++++++++ .../suites/old-sdk-interop/hub/template.html | 9 +++++++++ .../suites/old-sdk-interop/hub/test.ts | 13 +++++++++++++ 4 files changed, 37 insertions(+) create mode 100644 dev-packages/browser-integration-tests/suites/old-sdk-interop/hub/init.js create mode 100644 dev-packages/browser-integration-tests/suites/old-sdk-interop/hub/subject.js create mode 100644 dev-packages/browser-integration-tests/suites/old-sdk-interop/hub/template.html create mode 100644 dev-packages/browser-integration-tests/suites/old-sdk-interop/hub/test.ts diff --git a/dev-packages/browser-integration-tests/suites/old-sdk-interop/hub/init.js b/dev-packages/browser-integration-tests/suites/old-sdk-interop/hub/init.js new file mode 100644 index 000000000000..d8c94f36fdd0 --- /dev/null +++ b/dev-packages/browser-integration-tests/suites/old-sdk-interop/hub/init.js @@ -0,0 +1,7 @@ +import * as Sentry from '@sentry/browser'; + +window.Sentry = Sentry; + +Sentry.init({ + dsn: 'https://public@dsn.ingest.sentry.io/1337', +}); diff --git a/dev-packages/browser-integration-tests/suites/old-sdk-interop/hub/subject.js b/dev-packages/browser-integration-tests/suites/old-sdk-interop/hub/subject.js new file mode 100644 index 000000000000..e9abeffbaf36 --- /dev/null +++ b/dev-packages/browser-integration-tests/suites/old-sdk-interop/hub/subject.js @@ -0,0 +1,8 @@ +/** + * Simulate an old pre v8 SDK obtaining the hub from the global sentry carrier + * and checking for the hub version. + */ +const res = window && window.__SENTRY__ && window.__SENTRY__.hub && window.__SENTRY__.hub.isOlderThan(7); + +// Write back result into the document +document.getElementById('olderThan').innerText = res; diff --git a/dev-packages/browser-integration-tests/suites/old-sdk-interop/hub/template.html b/dev-packages/browser-integration-tests/suites/old-sdk-interop/hub/template.html new file mode 100644 index 000000000000..a363fad46013 --- /dev/null +++ b/dev-packages/browser-integration-tests/suites/old-sdk-interop/hub/template.html @@ -0,0 +1,9 @@ + + + + + + +

+ + diff --git a/dev-packages/browser-integration-tests/suites/old-sdk-interop/hub/test.ts b/dev-packages/browser-integration-tests/suites/old-sdk-interop/hub/test.ts new file mode 100644 index 000000000000..00b94eef4bd5 --- /dev/null +++ b/dev-packages/browser-integration-tests/suites/old-sdk-interop/hub/test.ts @@ -0,0 +1,13 @@ +import { expect } from '@playwright/test'; + +import { sentryTest } from '../../../utils/fixtures'; + +sentryTest( + "doesn't crash if older SDKs access `hub.isOlderThan` on the global object", + async ({ getLocalTestUrl, page }) => { + const url = await getLocalTestUrl({ testDir: __dirname }); + await page.goto(url); + + await expect(page.locator('#olderThan')).toHaveText('false'); + }, +); From 4bf09495ff4c66a0e9ab02311d4292a7dc31e3dc Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Wed, 22 May 2024 15:08:49 +0200 Subject: [PATCH 3/4] use `stack` to set AsyncStackStrategy; use `hub` via `getCurrentHub` --- .../suites/old-sdk-interop/hub/subject.js | 1 + .../core/src/asyncContext/stackStrategy.ts | 36 +++++++------------ packages/core/src/getCurrentHubShim.ts | 14 ++++++-- packages/utils/src/worldwide.ts | 1 + 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/dev-packages/browser-integration-tests/suites/old-sdk-interop/hub/subject.js b/dev-packages/browser-integration-tests/suites/old-sdk-interop/hub/subject.js index e9abeffbaf36..5d97d6c4c1fa 100644 --- a/dev-packages/browser-integration-tests/suites/old-sdk-interop/hub/subject.js +++ b/dev-packages/browser-integration-tests/suites/old-sdk-interop/hub/subject.js @@ -1,3 +1,4 @@ +console.log(window.__SENTRY__); /** * Simulate an old pre v8 SDK obtaining the hub from the global sentry carrier * and checking for the hub version. diff --git a/packages/core/src/asyncContext/stackStrategy.ts b/packages/core/src/asyncContext/stackStrategy.ts index 19380d7bf739..940e2c58e98a 100644 --- a/packages/core/src/asyncContext/stackStrategy.ts +++ b/packages/core/src/asyncContext/stackStrategy.ts @@ -1,9 +1,10 @@ import type { Client, Scope as ScopeInterface } from '@sentry/types'; -import { isThenable } from '@sentry/utils'; +import { getGlobalSingleton, isThenable } from '@sentry/utils'; import { getDefaultCurrentScope, getDefaultIsolationScope } from '../defaultScopes'; import { Scope } from '../scope'; -import { getMainCarrier, getSentryCarrier } from './../carrier'; +import { getMainCarrier, getSentryCarrier } from '../carrier'; +import { getCurrentHubShim } from '../getCurrentHubShim'; import type { AsyncContextStrategy } from './types'; interface Layer { @@ -104,16 +105,6 @@ export class AsyncContextStack { return this._stack[this._stack.length - 1]; } - /** - * Only here for compatibility with getCurrentHubShim(), enumerable: true }); } - sentry.hub = new AsyncContextStack(getDefaultCurrentScope(), getDefaultIsolationScope()); - return sentry.hub; + return getGlobalSingleton('stack', () => new AsyncContextStack(getDefaultCurrentScope(), getDefaultIsolationScope())); } function withScope(callback: (scope: ScopeInterface) => T): T { diff --git a/packages/core/src/getCurrentHubShim.ts b/packages/core/src/getCurrentHubShim.ts index 46f8f94cb490..ee5d45528aff 100644 --- a/packages/core/src/getCurrentHubShim.ts +++ b/packages/core/src/getCurrentHubShim.ts @@ -1,4 +1,4 @@ -import type { Client, EventHint, Hub, Integration, IntegrationClass, SeverityLevel } from '@sentry/types'; +import type { Client, EventHint, Hub, Integration, IntegrationClass, Scope, SeverityLevel } from '@sentry/types'; import { addBreadcrumb } from './breadcrumbs'; import { getClient, getCurrentScope, getIsolationScope, withScope } from './currentScopes'; import { @@ -22,7 +22,10 @@ import { * usage */ // eslint-disable-next-line deprecation/deprecation -export function getCurrentHubShim(): Hub { +export function getCurrentHubShim(): Hub & { + getStackTop: () => { client: Client | undefined; scope: Scope }; + isOlderThan: () => boolean; +} { return { bindClient(client: Client): void { const scope = getCurrentScope(); @@ -64,6 +67,13 @@ export function getCurrentHubShim(): Hub { // only send the update _sendSessionUpdate(); }, + getStackTop: () => ({ + client: getClient(), + scope: getCurrentScope(), + }), + isOlderThan() { + return false; + }, }; } diff --git a/packages/utils/src/worldwide.ts b/packages/utils/src/worldwide.ts index cc5241f00e0f..ac5fd4f2b8c4 100644 --- a/packages/utils/src/worldwide.ts +++ b/packages/utils/src/worldwide.ts @@ -45,6 +45,7 @@ export interface InternalGlobal { _sentryDebugIds?: Record; __SENTRY__: { hub: any; + stack: any; logger: any; extensions?: { /** Extension methods for the hub, which are bound to the current Hub instance */ From f3ddaac510394bc76d1f03b34a676bdafd0d1756 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Wed, 22 May 2024 16:14:21 +0200 Subject: [PATCH 4/4] size limit, biome --- .size-limit.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.size-limit.js b/.size-limit.js index 1747b93aea21..199341dcc00a 100644 --- a/.size-limit.js +++ b/.size-limit.js @@ -22,7 +22,7 @@ module.exports = [ path: 'packages/browser/build/npm/esm/index.js', import: createImport('init', 'browserTracingIntegration', 'replayIntegration'), gzip: true, - limit: '70 KB', + limit: '71 KB', }, { name: '@sentry/browser (incl. Tracing, Replay) - with treeshaking flags',