Skip to content

Commit d59ea01

Browse files
authored
ref(utils): Remove getGlobalObject() usage from @sentry/node and integrations
1 parent 74e787f commit d59ea01

File tree

5 files changed

+39
-43
lines changed

5 files changed

+39
-43
lines changed

Diff for: packages/integrations/src/captureconsole.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { EventProcessor, Hub, Integration } from '@sentry/types';
2-
import { CONSOLE_LEVELS, fill, getGlobalObject, safeJoin, severityLevelFromString } from '@sentry/utils';
3-
4-
const global = getGlobalObject<Window | NodeJS.Global>();
2+
import { CONSOLE_LEVELS, fill, GLOBAL_OBJ, safeJoin, severityLevelFromString } from '@sentry/utils';
53

64
/** Send Console API calls as Sentry Events */
75
export class CaptureConsole implements Integration {
@@ -33,17 +31,18 @@ export class CaptureConsole implements Integration {
3331
* @inheritDoc
3432
*/
3533
public setupOnce(_: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
36-
if (!('console' in global)) {
34+
if (!('console' in GLOBAL_OBJ)) {
3735
return;
3836
}
3937

4038
this._levels.forEach((level: string) => {
41-
if (!(level in global.console)) {
39+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
40+
if (!(level in (GLOBAL_OBJ as any).console)) {
4241
return;
4342
}
4443

45-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
46-
fill(global.console, level, (originalConsoleMethod: () => any) => (...args: any[]): void => {
44+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
45+
fill((GLOBAL_OBJ as any).console, level, (originalConsoleMethod: () => any) => (...args: any[]): void => {
4746
const hub = getCurrentHub();
4847

4948
if (hub.getIntegration(CaptureConsole)) {
@@ -72,7 +71,7 @@ export class CaptureConsole implements Integration {
7271

7372
// this fails for some browsers. :(
7473
if (originalConsoleMethod) {
75-
originalConsoleMethod.apply(global.console, args);
74+
originalConsoleMethod.apply(GLOBAL_OBJ.console, args);
7675
}
7776
});
7877
});

Diff for: packages/integrations/src/offline.ts

+5-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
33
import { Event, EventProcessor, Hub, Integration } from '@sentry/types';
4-
import { getGlobalObject, logger, normalize, uuid4 } from '@sentry/utils';
4+
import { logger, normalize, uuid4, WINDOW } from '@sentry/utils';
55
import localForage from 'localforage';
66

77
type LocalForage = {
@@ -30,12 +30,6 @@ export class Offline implements Integration {
3030
*/
3131
public readonly name: string = Offline.id;
3232

33-
/**
34-
* the global instance
35-
*/
36-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
37-
public global: any;
38-
3933
/**
4034
* the current hub instance
4135
*/
@@ -55,8 +49,6 @@ export class Offline implements Integration {
5549
* @inheritDoc
5650
*/
5751
public constructor(options: { maxStoredEvents?: number } = {}) {
58-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
59-
this.global = getGlobalObject<any>();
6052
this.maxStoredEvents = options.maxStoredEvents || 30; // set a reasonable default
6153
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
6254
this.offlineEventStore = localForage.createInstance({
@@ -70,8 +62,8 @@ export class Offline implements Integration {
7062
public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
7163
this.hub = getCurrentHub();
7264

73-
if ('addEventListener' in this.global) {
74-
this.global.addEventListener('online', () => {
65+
if ('addEventListener' in WINDOW) {
66+
WINDOW.addEventListener('online', () => {
7567
void this._sendEvents().catch(() => {
7668
__DEBUG_BUILD__ && logger.warn('could not send cached events');
7769
});
@@ -81,7 +73,7 @@ export class Offline implements Integration {
8173
const eventProcessor: EventProcessor = event => {
8274
if (this.hub && this.hub.getIntegration(Offline)) {
8375
// cache if we are positively offline
84-
if ('navigator' in this.global && 'onLine' in this.global.navigator && !this.global.navigator.onLine) {
76+
if ('navigator' in WINDOW && 'onLine' in WINDOW.navigator && !WINDOW.navigator.onLine) {
8577
__DEBUG_BUILD__ && logger.log('Event dropped due to being a offline - caching instead');
8678

8779
void this._cacheEvent(event)
@@ -102,7 +94,7 @@ export class Offline implements Integration {
10294
addGlobalEventProcessor(eventProcessor);
10395

10496
// if online now, send any events stored in a previous offline session
105-
if ('navigator' in this.global && 'onLine' in this.global.navigator && this.global.navigator.onLine) {
97+
if ('navigator' in WINDOW && 'onLine' in WINDOW.navigator && WINDOW.navigator.onLine) {
10698
void this._sendEvents().catch(() => {
10799
__DEBUG_BUILD__ && logger.warn('could not send cached events');
108100
});

Diff for: packages/integrations/src/reportingobserver.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { EventProcessor, Hub, Integration } from '@sentry/types';
2-
import { getGlobalObject, supportsReportingObserver } from '@sentry/utils';
2+
import { supportsReportingObserver, WINDOW } from '@sentry/utils';
33

44
interface Report {
55
[key: string]: unknown;
@@ -76,7 +76,7 @@ export class ReportingObserver implements Integration {
7676
this._getCurrentHub = getCurrentHub;
7777

7878
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
79-
const observer = new (getGlobalObject<any>().ReportingObserver)(this.handler.bind(this), {
79+
const observer = new (WINDOW as any).ReportingObserver(this.handler.bind(this), {
8080
buffered: true,
8181
types: this._options.types,
8282
});

Diff for: packages/integrations/test/offline.test.ts

+22-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Event, EventProcessor, Hub, Integration, IntegrationClass } from '@sentry/types';
2-
import * as utils from '@sentry/utils';
2+
import { WINDOW } from '@sentry/utils';
33

44
import { Item, Offline } from '../src/offline';
55

@@ -34,12 +34,30 @@ jest.mock('localforage', () => ({
3434
}));
3535

3636
let integration: Offline;
37-
let online: boolean;
37+
38+
// We need to mock the WINDOW object so we can modify 'navigator.online' which is readonly
39+
jest.mock('@sentry/utils', () => {
40+
const originalModule = jest.requireActual('@sentry/utils');
41+
42+
return {
43+
...originalModule,
44+
get WINDOW() {
45+
return {
46+
addEventListener: (_windowEvent: any, callback: any) => {
47+
eventListeners.push(callback);
48+
},
49+
navigator: {
50+
onLine: false,
51+
},
52+
};
53+
},
54+
};
55+
});
3856

3957
describe('Offline', () => {
4058
describe('when app is online', () => {
4159
beforeEach(() => {
42-
online = true;
60+
(WINDOW.navigator as any).onLine = true;
4361

4462
initIntegration();
4563
});
@@ -71,7 +89,7 @@ describe('Offline', () => {
7189

7290
describe('when app is offline', () => {
7391
beforeEach(() => {
74-
online = false;
92+
(WINDOW.navigator as any).onLine = false;
7593
});
7694

7795
it('stores events in offline store', async () => {
@@ -166,18 +184,6 @@ function initIntegration(options: { maxStoredEvents?: number } = {}): void {
166184
eventProcessors = [];
167185
events = [];
168186

169-
jest.spyOn(utils, 'getGlobalObject').mockImplementation(
170-
() =>
171-
({
172-
addEventListener: (_windowEvent: any, callback: any) => {
173-
eventListeners.push(callback);
174-
},
175-
navigator: {
176-
onLine: online,
177-
},
178-
} as any),
179-
);
180-
181187
integration = new Offline(options);
182188
}
183189

Diff for: packages/node/src/sdk.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
import { SessionStatus, StackParser } from '@sentry/types';
1111
import {
1212
createStackParser,
13-
getGlobalObject,
13+
GLOBAL_OBJ,
1414
logger,
1515
nodeStackLineParser,
1616
stackParserFromStackParserOptions,
@@ -233,9 +233,8 @@ export function getSentryRelease(fallback?: string): string | undefined {
233233
}
234234

235235
// This supports the variable that sentry-webpack-plugin injects
236-
const global = getGlobalObject();
237-
if (global.SENTRY_RELEASE && global.SENTRY_RELEASE.id) {
238-
return global.SENTRY_RELEASE.id;
236+
if (GLOBAL_OBJ.SENTRY_RELEASE && GLOBAL_OBJ.SENTRY_RELEASE.id) {
237+
return GLOBAL_OBJ.SENTRY_RELEASE.id;
239238
}
240239

241240
return (

0 commit comments

Comments
 (0)