Skip to content

Commit 74e787f

Browse files
authored
ref(utils): Remove getGlobalObject() usage from all framework specific SDKs (#5948)
1 parent 47aabd8 commit 74e787f

File tree

15 files changed

+63
-85
lines changed

15 files changed

+63
-85
lines changed

packages/angular/src/tracing.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { AfterViewInit, Directive, Injectable, Input, NgModule, OnDestroy, OnIni
33
import { ActivatedRouteSnapshot, Event, NavigationEnd, NavigationStart, ResolveEnd, Router } from '@angular/router';
44
import { getCurrentHub } from '@sentry/browser';
55
import { Span, Transaction, TransactionContext } from '@sentry/types';
6-
import { getGlobalObject, logger, stripUrlQueryAndFragment, timestampWithMs } from '@sentry/utils';
6+
import { logger, stripUrlQueryAndFragment, timestampWithMs, WINDOW } from '@sentry/utils';
77
import { Observable, Subscription } from 'rxjs';
88
import { filter, tap } from 'rxjs/operators';
99

@@ -15,8 +15,6 @@ let instrumentationInitialized: boolean;
1515
let stashedStartTransaction: (context: TransactionContext) => Transaction | undefined;
1616
let stashedStartTransactionOnLocationChange: boolean;
1717

18-
const global = getGlobalObject<Window>();
19-
2018
/**
2119
* Creates routing instrumentation for Angular Router.
2220
*/
@@ -29,9 +27,9 @@ export function routingInstrumentation(
2927
stashedStartTransaction = customStartTransaction;
3028
stashedStartTransactionOnLocationChange = startTransactionOnLocationChange;
3129

32-
if (startTransactionOnPageLoad && global && global.location) {
30+
if (startTransactionOnPageLoad && WINDOW && WINDOW.location) {
3331
customStartTransaction({
34-
name: global.location.pathname,
32+
name: WINDOW.location.pathname,
3533
op: 'pageload',
3634
metadata: { source: 'url' },
3735
});

packages/ember/addon/index.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ import { macroCondition, isDevelopingApp, getOwnConfig } from '@embroider/macros
44
import { next } from '@ember/runloop';
55
import { assert, warn } from '@ember/debug';
66
import Ember from 'ember';
7-
import { timestampWithMs } from '@sentry/utils';
7+
import { timestampWithMs, GLOBAL_OBJ } from '@sentry/utils';
88
import { GlobalConfig, OwnConfig } from './types';
9-
import { getGlobalObject } from '@sentry/utils';
109

1110
function _getSentryInitConfig() {
12-
const _global = getGlobalObject<GlobalConfig>();
11+
const _global = GLOBAL_OBJ as typeof GLOBAL_OBJ & GlobalConfig;
1312
_global.__sentryEmberConfig = _global.__sentryEmberConfig ?? {};
1413
return _global.__sentryEmberConfig;
1514
}

packages/ember/addon/instance-initializers/sentry-performance.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import { ExtendedBackburner } from '@sentry/ember/runloop';
66
import { Span, Transaction, Integration } from '@sentry/types';
77
import { EmberRunQueues } from '@ember/runloop/-private/types';
88
import { getActiveTransaction } from '..';
9-
import { browserPerformanceTimeOrigin, getGlobalObject, timestampWithMs } from '@sentry/utils';
9+
import { browserPerformanceTimeOrigin, GLOBAL_OBJ, timestampWithMs } from '@sentry/utils';
1010
import { macroCondition, isTesting, getOwnConfig } from '@embroider/macros';
1111
import { EmberSentryConfig, GlobalConfig, OwnConfig } from '../types';
1212

1313
function getSentryConfig() {
14-
const _global = getGlobalObject<GlobalConfig>();
14+
const _global = GLOBAL_OBJ as typeof GLOBAL_OBJ & GlobalConfig;
1515
_global.__sentryEmberConfig = _global.__sentryEmberConfig ?? {};
1616
const environmentConfig = getOwnConfig<OwnConfig>().sentryConfig;
1717
if (!environmentConfig.sentry) {

packages/nextjs/src/performance/client.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,19 @@ import { Primitive, TraceparentData, Transaction, TransactionContext, Transactio
33
import {
44
baggageHeaderToDynamicSamplingContext,
55
extractTraceparentData,
6-
getGlobalObject,
76
logger,
87
stripUrlQueryAndFragment,
8+
WINDOW,
99
} from '@sentry/utils';
1010
import type { NEXT_DATA as NextData } from 'next/dist/next-server/lib/utils';
1111
import { default as Router } from 'next/router';
1212
import type { ParsedUrlQuery } from 'querystring';
1313

14-
const global = getGlobalObject<
15-
Window & {
16-
__BUILD_MANIFEST?: {
17-
sortedPages?: string[];
18-
};
19-
}
20-
>();
14+
const global = WINDOW as typeof WINDOW & {
15+
__BUILD_MANIFEST?: {
16+
sortedPages?: string[];
17+
};
18+
};
2119

2220
type StartTransactionCb = (context: TransactionContext) => Transaction | undefined;
2321

packages/nextjs/test/index.client.test.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@ import { BaseClient, getCurrentHub } from '@sentry/core';
22
import * as SentryReact from '@sentry/react';
33
import { Integrations as TracingIntegrations } from '@sentry/tracing';
44
import { Integration } from '@sentry/types';
5-
import { getGlobalObject, logger } from '@sentry/utils';
5+
import { logger, WINDOW } from '@sentry/utils';
66
import { JSDOM } from 'jsdom';
77

88
import { init, Integrations, nextRouterInstrumentation } from '../src/index.client';
99
import { UserIntegrationsFunction } from '../src/utils/userIntegrations';
1010

1111
const { BrowserTracing } = TracingIntegrations;
1212

13-
const global = getGlobalObject();
14-
1513
const reactInit = jest.spyOn(SentryReact, 'init');
1614
const captureEvent = jest.spyOn(BaseClient.prototype, 'captureEvent');
1715
const loggerLogSpy = jest.spyOn(logger, 'log');
@@ -23,12 +21,12 @@ const dom = new JSDOM(undefined, { url: 'https://example.com/' });
2321
Object.defineProperty(global, 'document', { value: dom.window.document, writable: true });
2422
Object.defineProperty(global, 'location', { value: dom.window.document.location, writable: true });
2523

26-
const originalGlobalDocument = getGlobalObject<Window>().document;
27-
const originalGlobalLocation = getGlobalObject<Window>().location;
24+
const originalGlobalDocument = WINDOW.document;
25+
const originalGlobalLocation = WINDOW.location;
2826
afterAll(() => {
2927
// Clean up JSDom
30-
Object.defineProperty(global, 'document', { value: originalGlobalDocument });
31-
Object.defineProperty(global, 'location', { value: originalGlobalLocation });
28+
Object.defineProperty(WINDOW, 'document', { value: originalGlobalDocument });
29+
Object.defineProperty(WINDOW, 'location', { value: originalGlobalLocation });
3230
});
3331

3432
function findIntegrationByName(integrations: Integration[] = [], name: string): Integration | undefined {
@@ -38,7 +36,7 @@ function findIntegrationByName(integrations: Integration[] = [], name: string):
3836
describe('Client init()', () => {
3937
afterEach(() => {
4038
jest.clearAllMocks();
41-
global.__SENTRY__.hub = undefined;
39+
WINDOW.__SENTRY__.hub = undefined;
4240
});
4341

4442
it('inits the React SDK', () => {

packages/nextjs/test/index.server.test.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import * as SentryNode from '@sentry/node';
22
import { getCurrentHub, NodeClient } from '@sentry/node';
33
import { Integration } from '@sentry/types';
4-
import { getGlobalObject, logger } from '@sentry/utils';
4+
import { GLOBAL_OBJ, logger } from '@sentry/utils';
55
import * as domain from 'domain';
66

77
import { init } from '../src/index.server';
88

99
const { Integrations } = SentryNode;
1010

11-
const global = getGlobalObject();
12-
1311
// normally this is set as part of the build process, so mock it here
14-
(global as typeof global & { __rewriteFramesDistDir__: string }).__rewriteFramesDistDir__ = '.next';
12+
(GLOBAL_OBJ as typeof GLOBAL_OBJ & { __rewriteFramesDistDir__: string }).__rewriteFramesDistDir__ = '.next';
1513

1614
const nodeInit = jest.spyOn(SentryNode, 'init');
1715
const loggerLogSpy = jest.spyOn(logger, 'log');
@@ -23,7 +21,7 @@ function findIntegrationByName(integrations: Integration[] = [], name: string):
2321
describe('Server init()', () => {
2422
afterEach(() => {
2523
jest.clearAllMocks();
26-
global.__SENTRY__.hub = undefined;
24+
GLOBAL_OBJ.__SENTRY__.hub = undefined;
2725
delete process.env.VERCEL;
2826
});
2927

packages/nextjs/test/performance/client.test.ts

+18-20
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import { Transaction } from '@sentry/types';
2-
import { getGlobalObject } from '@sentry/utils';
2+
import { WINDOW } from '@sentry/utils';
33
import { JSDOM } from 'jsdom';
44
import { NEXT_DATA as NextData } from 'next/dist/next-server/lib/utils';
55
import { default as Router } from 'next/router';
66

77
import { nextRouterInstrumentation } from '../../src/performance/client';
88

9-
const globalObject = getGlobalObject<
10-
Window & {
11-
__BUILD_MANIFEST?: {
12-
sortedPages?: string[];
13-
};
14-
}
15-
>();
9+
const globalObject = WINDOW as typeof WINDOW & {
10+
__BUILD_MANIFEST?: {
11+
sortedPages?: string[];
12+
};
13+
};
1614

1715
const originalBuildManifest = globalObject.__BUILD_MANIFEST;
1816
const originalBuildManifestRoutes = globalObject.__BUILD_MANIFEST?.sortedPages;
@@ -60,8 +58,8 @@ function createMockStartTransaction() {
6058
}
6159

6260
describe('nextRouterInstrumentation', () => {
63-
const originalGlobalDocument = getGlobalObject<Window>().document;
64-
const originalGlobalLocation = getGlobalObject<Window>().location;
61+
const originalGlobalDocument = WINDOW.document;
62+
const originalGlobalLocation = WINDOW.location;
6563

6664
function setUpNextPage(pageProperties: {
6765
url: string;
@@ -93,25 +91,25 @@ describe('nextRouterInstrumentation', () => {
9391
// The Next.js routing instrumentations requires a few things to be present on pageload:
9492
// 1. Access to window.document API for `window.document.getElementById`
9593
// 2. Access to window.location API for `window.location.pathname`
96-
Object.defineProperty(global, 'document', { value: dom.window.document, writable: true });
97-
Object.defineProperty(global, 'location', { value: dom.window.document.location, writable: true });
94+
Object.defineProperty(WINDOW, 'document', { value: dom.window.document, writable: true });
95+
Object.defineProperty(WINDOW, 'location', { value: dom.window.document.location, writable: true });
9896

9997
// Define Next.js clientside build manifest with navigatable routes
100-
(global as any).__BUILD_MANIFEST = {
101-
...(global as any).__BUILD_MANIFEST,
102-
sortedPages: pageProperties.navigatableRoutes,
98+
globalObject.__BUILD_MANIFEST = {
99+
...globalObject.__BUILD_MANIFEST,
100+
sortedPages: pageProperties.navigatableRoutes as string[],
103101
};
104102
}
105103

106104
afterEach(() => {
107105
// Clean up JSDom
108-
Object.defineProperty(global, 'document', { value: originalGlobalDocument });
109-
Object.defineProperty(global, 'location', { value: originalGlobalLocation });
106+
Object.defineProperty(WINDOW, 'document', { value: originalGlobalDocument });
107+
Object.defineProperty(WINDOW, 'location', { value: originalGlobalLocation });
110108

111109
// Reset Next.js' __BUILD_MANIFEST
112-
(global as any).__BUILD_MANIFEST = originalBuildManifest;
113-
if ((global as any).__BUILD_MANIFEST) {
114-
(global as any).__BUILD_MANIFEST.sortedPages = originalBuildManifestRoutes;
110+
globalObject.__BUILD_MANIFEST = originalBuildManifest;
111+
if (globalObject.__BUILD_MANIFEST) {
112+
globalObject.__BUILD_MANIFEST.sortedPages = originalBuildManifestRoutes as string[];
115113
}
116114

117115
// Clear all event handlers

packages/react/src/reactrouter.tsx

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Transaction, TransactionSource } from '@sentry/types';
2-
import { getGlobalObject } from '@sentry/utils';
2+
import { WINDOW } from '@sentry/utils';
33
import hoistNonReactStatics from 'hoist-non-react-statics';
44
import * as React from 'react';
55

@@ -26,8 +26,6 @@ export type RouteConfig = {
2626
type MatchPath = (pathname: string, props: string | string[] | any, parent?: Match | null) => Match | null;
2727
/* eslint-enable @typescript-eslint/no-explicit-any */
2828

29-
const global = getGlobalObject<Window>();
30-
3129
let activeTransaction: Transaction | undefined;
3230

3331
export function reactRouterV4Instrumentation(
@@ -57,8 +55,8 @@ function createReactRouterInstrumentation(
5755
return history.location.pathname;
5856
}
5957

60-
if (global && global.location) {
61-
return global.location.pathname;
58+
if (WINDOW && WINDOW.location) {
59+
return WINDOW.location.pathname;
6260
}
6361

6462
return undefined;

packages/react/src/reactrouterv3.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Primitive, Transaction, TransactionContext, TransactionSource } from '@sentry/types';
2-
import { getGlobalObject } from '@sentry/utils';
2+
import { WINDOW } from '@sentry/utils';
33

44
import { Location, ReactRouterInstrumentation } from './types';
55

@@ -21,8 +21,6 @@ export type Match = (
2121

2222
type ReactRouterV3TransactionSource = Extract<TransactionSource, 'url' | 'route'>;
2323

24-
const global = getGlobalObject<Window>();
25-
2624
/**
2725
* Creates routing instrumentation for React Router v3
2826
* Works for React Router >= 3.2.0 and < 4.0.0
@@ -44,11 +42,11 @@ export function reactRouterV3Instrumentation(
4442
let activeTransaction: Transaction | undefined;
4543
let prevName: string | undefined;
4644

47-
// Have to use global.location because history.location might not be defined.
48-
if (startTransactionOnPageLoad && global && global.location) {
45+
// Have to use window.location because history.location might not be defined.
46+
if (startTransactionOnPageLoad && WINDOW && WINDOW.location) {
4947
normalizeTransactionName(
5048
routes,
51-
global.location as unknown as Location,
49+
WINDOW.location as unknown as Location,
5250
match,
5351
(localName: string, source: ReactRouterV3TransactionSource = 'url') => {
5452
prevName = localName;

packages/react/src/reactrouterv6.tsx

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// https://gist.github.com/wontondon/e8c4bdf2888875e4c755712e99279536
33

44
import { Transaction, TransactionContext, TransactionSource } from '@sentry/types';
5-
import { getGlobalObject, getNumberOfUrlSegments, logger } from '@sentry/utils';
5+
import { getNumberOfUrlSegments, logger, WINDOW } from '@sentry/utils';
66
import hoistNonReactStatics from 'hoist-non-react-statics';
77
import React from 'react';
88

@@ -58,8 +58,6 @@ let _matchRoutes: MatchRoutes;
5858
let _customStartTransaction: (context: TransactionContext) => Transaction | undefined;
5959
let _startTransactionOnLocationChange: boolean;
6060

61-
const global = getGlobalObject<Window>();
62-
6361
const SENTRY_TAGS = {
6462
'routing.instrumentation': 'react-router-v6',
6563
};
@@ -76,7 +74,7 @@ export function reactRouterV6Instrumentation(
7674
startTransactionOnPageLoad = true,
7775
startTransactionOnLocationChange = true,
7876
): void => {
79-
const initPathName = global && global.location && global.location.pathname;
77+
const initPathName = WINDOW && WINDOW.location && WINDOW.location.pathname;
8078
if (startTransactionOnPageLoad && initPathName) {
8179
activeTransaction = customStartTransaction({
8280
name: initPathName,

packages/remix/src/performance/client.tsx

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ErrorBoundaryProps } from '@sentry/react';
22
import { withErrorBoundary } from '@sentry/react';
33
import { Transaction, TransactionContext } from '@sentry/types';
4-
import { getGlobalObject, logger } from '@sentry/utils';
4+
import { logger, WINDOW } from '@sentry/utils';
55
import * as React from 'react';
66

77
const DEFAULT_TAGS = {
@@ -38,11 +38,9 @@ let _useMatches: UseMatches;
3838
let _customStartTransaction: (context: TransactionContext) => Transaction | undefined;
3939
let _startTransactionOnLocationChange: boolean;
4040

41-
const global = getGlobalObject<Window>();
42-
4341
function getInitPathName(): string | undefined {
44-
if (global && global.location) {
45-
return global.location.pathname;
42+
if (WINDOW && WINDOW.location) {
43+
return WINDOW.location.pathname;
4644
}
4745

4846
return undefined;

packages/remix/test/index.client.test.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import { getCurrentHub } from '@sentry/core';
22
import * as SentryReact from '@sentry/react';
3-
import { getGlobalObject } from '@sentry/utils';
3+
import { GLOBAL_OBJ } from '@sentry/utils';
44

55
import { init } from '../src/index.client';
66

7-
const global = getGlobalObject();
8-
97
const reactInit = jest.spyOn(SentryReact, 'init');
108

119
describe('Client init()', () => {
1210
afterEach(() => {
1311
jest.clearAllMocks();
14-
global.__SENTRY__.hub = undefined;
12+
GLOBAL_OBJ.__SENTRY__.hub = undefined;
1513
});
1614

1715
it('inits the React SDK', () => {

packages/remix/test/index.server.test.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import * as SentryNode from '@sentry/node';
22
import { getCurrentHub } from '@sentry/node';
3-
import { getGlobalObject } from '@sentry/utils';
3+
import { GLOBAL_OBJ } from '@sentry/utils';
44

55
import { init } from '../src/index.server';
66

7-
const global = getGlobalObject();
8-
97
const nodeInit = jest.spyOn(SentryNode, 'init');
108

119
describe('Server init()', () => {
1210
afterEach(() => {
1311
jest.clearAllMocks();
14-
global.__SENTRY__.hub = undefined;
12+
GLOBAL_OBJ.__SENTRY__.hub = undefined;
1513
});
1614

1715
it('inits the Node SDK', () => {

0 commit comments

Comments
 (0)