Skip to content

Commit 741dff9

Browse files
committed
ref(replay): Use WINDOW instead of window
1 parent 1ae2726 commit 741dff9

File tree

8 files changed

+32
-23
lines changed

8 files changed

+32
-23
lines changed

packages/replay/.eslintrc.js

-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ module.exports = {
3939
rules: {
4040
// TODO (high-prio): Go through console logs and figure out which ones should be replaced with the SDK logger
4141
'no-console': 'off',
42-
// TODO (high-pio): Re-enable this after migration
43-
'no-restricted-globals': 'off',
4442
// TODO (high-prio): Re-enable this after migration
4543
'@typescript-eslint/explicit-member-accessibility': 'off',
4644
// TODO (high-prio): Remove this exception from naming convention after migration

packages/replay/src/createPerformanceEntry.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { WINDOW } from '@sentry/browser';
12
import { browserPerformanceTimeOrigin } from '@sentry/utils';
23
import { record } from 'rrweb';
34

@@ -60,7 +61,7 @@ function createPerformanceEntry(entry: AllPerformanceEntry): ReplayPerformanceEn
6061
function getAbsoluteTime(time: number): number {
6162
// browserPerformanceTimeOrigin can be undefined if `performance` or
6263
// `performance.now` doesn't exist, but this is already checked by this integration
63-
return ((browserPerformanceTimeOrigin || window.performance.timeOrigin) + time) / 1000;
64+
return ((browserPerformanceTimeOrigin || WINDOW.performance.timeOrigin) + time) / 1000;
6465
}
6566

6667
// TODO: type definition!

packages/replay/src/eventBuffer.ts

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ interface CreateEventBufferParams {
1212
}
1313

1414
export function createEventBuffer({ useCompression }: CreateEventBufferParams): IEventBuffer {
15+
// eslint-disable-next-line no-restricted-globals
1516
if (useCompression && window.Worker) {
1617
const workerBlob = new Blob([workerString]);
1718
const workerUrl = URL.createObjectURL(workerBlob);

packages/replay/src/index.ts

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable max-lines */ // TODO: We might want to split this file up
2+
import { WINDOW } from '@sentry/browser';
23
import { addGlobalEventProcessor, getCurrentHub, Scope, setContext } from '@sentry/core';
34
import { Breadcrumb, Client, Event, Integration } from '@sentry/types';
45
import { addInstrumentationHandler, createEnvelope, logger } from '@sentry/utils';
@@ -232,7 +233,7 @@ export class Replay implements Integration {
232233
return;
233234
}
234235
// XXX: See method comments above
235-
window.setTimeout(() => this.start());
236+
setTimeout(() => this.start());
236237
}
237238

238239
/**
@@ -396,8 +397,8 @@ export class Replay implements Integration {
396397
* first flush.
397398
*/
398399
setInitialState(): void {
399-
const urlPath = `${window.location.pathname}${window.location.hash}${window.location.search}`;
400-
const url = `${window.location.origin}${urlPath}`;
400+
const urlPath = `${WINDOW.location.pathname}${WINDOW.location.hash}${WINDOW.location.search}`;
401+
const url = `${WINDOW.location.origin}${urlPath}`;
401402

402403
this.performanceEvents = [];
403404

@@ -414,9 +415,9 @@ export class Replay implements Integration {
414415
*/
415416
addListeners(): void {
416417
try {
417-
document.addEventListener('visibilitychange', this.handleVisibilityChange);
418-
window.addEventListener('blur', this.handleWindowBlur);
419-
window.addEventListener('focus', this.handleWindowFocus);
418+
WINDOW.document.addEventListener('visibilitychange', this.handleVisibilityChange);
419+
WINDOW.addEventListener('blur', this.handleWindowBlur);
420+
WINDOW.addEventListener('focus', this.handleWindowFocus);
420421

421422
// There is no way to remove these listeners, so ensure they are only added once
422423
if (!this.hasInitializedCoreListeners) {
@@ -440,7 +441,7 @@ export class Replay implements Integration {
440441
}
441442

442443
// PerformanceObserver //
443-
if (!('PerformanceObserver' in window)) {
444+
if (!('PerformanceObserver' in WINDOW)) {
444445
return;
445446
}
446447

@@ -475,10 +476,10 @@ export class Replay implements Integration {
475476
*/
476477
removeListeners(): void {
477478
try {
478-
document.removeEventListener('visibilitychange', this.handleVisibilityChange);
479+
WINDOW.document.removeEventListener('visibilitychange', this.handleVisibilityChange);
479480

480-
window.removeEventListener('blur', this.handleWindowBlur);
481-
window.removeEventListener('focus', this.handleWindowFocus);
481+
WINDOW.removeEventListener('blur', this.handleWindowBlur);
482+
WINDOW.removeEventListener('focus', this.handleWindowFocus);
482483

483484
if (this.performanceObserver) {
484485
this.performanceObserver.disconnect();
@@ -665,7 +666,7 @@ export class Replay implements Integration {
665666
* page will also trigger a change to a hidden state.
666667
*/
667668
handleVisibilityChange: () => void = () => {
668-
if (document.visibilityState === 'visible') {
669+
if (WINDOW.document.visibilityState === 'visible') {
669670
this.doChangeToForegroundTasks();
670671
} else {
671672
this.doChangeToBackgroundTasks();
@@ -980,13 +981,13 @@ export class Replay implements Integration {
980981
addMemoryEntry(): Promise<void[]> | undefined {
981982
// window.performance.memory is a non-standard API and doesn't work on all browsers
982983
// so we check before creating the event.
983-
if (!('memory' in window.performance)) {
984+
if (!('memory' in WINDOW.performance)) {
984985
return;
985986
}
986987

987988
return this.createPerformanceSpans([
988989
// @ts-ignore memory doesn't exist on type Performance as the API is non-standard (we check that it exists above)
989-
createMemoryEntry(window.performance.memory),
990+
createMemoryEntry(WINDOW.performance.memory),
990991
]);
991992
}
992993

packages/replay/src/session/deleteSession.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1+
import { WINDOW } from '@sentry/browser';
2+
13
import { REPLAY_SESSION_KEY } from './constants';
24

35
/**
46
* Deletes a session from storage
57
*/
68
export function deleteSession(): void {
7-
const hasSessionStorage = 'sessionStorage' in window;
9+
const hasSessionStorage = 'sessionStorage' in WINDOW;
810

911
if (!hasSessionStorage) {
1012
return;
1113
}
1214

1315
try {
14-
window.sessionStorage.removeItem(REPLAY_SESSION_KEY);
16+
WINDOW.sessionStorage.removeItem(REPLAY_SESSION_KEY);
1517
} catch {
1618
// Ignore potential SecurityError exceptions
1719
}

packages/replay/src/session/fetchSession.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { WINDOW } from '@sentry/browser';
2+
13
import { SampleRates } from '../types';
24
import { REPLAY_SESSION_KEY } from './constants';
35
import { Session } from './Session';
@@ -6,15 +8,15 @@ import { Session } from './Session';
68
* Fetches a session from storage
79
*/
810
export function fetchSession({ sessionSampleRate, errorSampleRate }: SampleRates): Session | null {
9-
const hasSessionStorage = 'sessionStorage' in window;
11+
const hasSessionStorage = 'sessionStorage' in WINDOW;
1012

1113
if (!hasSessionStorage) {
1214
return null;
1315
}
1416

1517
try {
1618
// This can throw if cookies are disabled
17-
const sessionStringFromStorage = window.sessionStorage.getItem(REPLAY_SESSION_KEY);
19+
const sessionStringFromStorage = WINDOW.sessionStorage.getItem(REPLAY_SESSION_KEY);
1820

1921
if (!sessionStringFromStorage) {
2022
return null;

packages/replay/src/session/saveSession.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
import { WINDOW } from '@sentry/browser';
2+
13
import { REPLAY_SESSION_KEY } from './constants';
24
import { Session } from './Session';
35

46
export function saveSession(session: Session): void {
5-
const hasSessionStorage = 'sessionStorage' in window;
7+
const hasSessionStorage = 'sessionStorage' in WINDOW;
68
if (!hasSessionStorage) {
79
return;
810
}
911

1012
try {
11-
window.sessionStorage.setItem(REPLAY_SESSION_KEY, JSON.stringify(session));
13+
WINDOW.sessionStorage.setItem(REPLAY_SESSION_KEY, JSON.stringify(session));
1214
} catch {
1315
// Ignore potential SecurityError exceptions
1416
}
+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import { WINDOW } from '@sentry/browser';
2+
13
import { isBrowser } from './isBrowser';
24

35
/**
46
* Returns true if we are currently recording an internal to Sentry replay
57
* (e.g. on https://sentry.io )
68
*/
79
export function isInternal(): boolean {
8-
return isBrowser() && ['sentry.io', 'dev.getsentry.net'].includes(window.location.host);
10+
return isBrowser() && ['sentry.io', 'dev.getsentry.net'].includes(WINDOW.location.host);
911
}

0 commit comments

Comments
 (0)