Skip to content

Commit 5610746

Browse files
committed
ref(replay): Use WINDOW instead of window
1 parent 0522caa commit 5610746

File tree

10 files changed

+48
-30
lines changed

10 files changed

+48
-30
lines changed

packages/replay/.eslintrc.js

Lines changed: 0 additions & 2 deletions
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/jest.setup.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ global.__SENTRY_REPLAY_VERSION__ = 'version:Test';
1010

1111
type MockTransport = jest.MockedFunction<Transport['send']>;
1212

13+
jest.mock('./src/util/isBrowser', () => {
14+
return {
15+
isBrowser: () => true,
16+
};
17+
});
18+
1319
afterEach(() => {
1420
const hub = getCurrentHub();
1521
if (typeof hub?.getClient !== 'function') {

packages/replay/src/createPerformanceEntry.ts

Lines changed: 2 additions & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 19 additions & 19 deletions
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';
@@ -38,6 +39,7 @@ import { captureInternalException } from './util/captureInternalException';
3839
import { createBreadcrumb } from './util/createBreadcrumb';
3940
import { createPayload } from './util/createPayload';
4041
import { dedupePerformanceEntries } from './util/dedupePerformanceEntries';
42+
import { isBrowser } from './util/isBrowser';
4143
import { isExpired } from './util/isExpired';
4244
import { isSessionExpired } from './util/isSessionExpired';
4345

@@ -53,8 +55,6 @@ const MEDIA_SELECTORS = 'img,image,svg,path,rect,area,video,object,picture,embed
5355

5456
let _initialized = false;
5557

56-
const isBrowser = typeof window !== 'undefined';
57-
5858
export class Replay implements Integration {
5959
/**
6060
* @inheritDoc
@@ -210,7 +210,7 @@ export class Replay implements Integration {
210210
maxWait: this.options.flushMaxDelay,
211211
});
212212

213-
if (isBrowser && _initialized) {
213+
if (isBrowser() && _initialized) {
214214
const error = new Error('Multiple Sentry Session Replay instances are not supported');
215215
captureInternalException(error);
216216
throw error;
@@ -229,11 +229,11 @@ export class Replay implements Integration {
229229
* here to avoid any future issues.
230230
*/
231231
setupOnce(): void {
232-
if (!isBrowser) {
232+
if (!isBrowser()) {
233233
return;
234234
}
235235
// XXX: See method comments above
236-
window.setTimeout(() => this.start());
236+
setTimeout(() => this.start());
237237
}
238238

239239
/**
@@ -243,7 +243,7 @@ export class Replay implements Integration {
243243
* PerformanceObserver, Recording, Sentry SDK, etc)
244244
*/
245245
start(): void {
246-
if (!isBrowser) {
246+
if (!isBrowser()) {
247247
return;
248248
}
249249

@@ -309,7 +309,7 @@ export class Replay implements Integration {
309309
* does not support a teardown
310310
*/
311311
stop(): void {
312-
if (!isBrowser) {
312+
if (!isBrowser()) {
313313
return;
314314
}
315315

@@ -397,8 +397,8 @@ export class Replay implements Integration {
397397
* first flush.
398398
*/
399399
setInitialState(): void {
400-
const urlPath = `${window.location.pathname}${window.location.hash}${window.location.search}`;
401-
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}`;
402402

403403
this.performanceEvents = [];
404404

@@ -415,9 +415,9 @@ export class Replay implements Integration {
415415
*/
416416
addListeners(): void {
417417
try {
418-
document.addEventListener('visibilitychange', this.handleVisibilityChange);
419-
window.addEventListener('blur', this.handleWindowBlur);
420-
window.addEventListener('focus', this.handleWindowFocus);
418+
WINDOW.document.addEventListener('visibilitychange', this.handleVisibilityChange);
419+
WINDOW.addEventListener('blur', this.handleWindowBlur);
420+
WINDOW.addEventListener('focus', this.handleWindowFocus);
421421

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

443443
// PerformanceObserver //
444-
if (!('PerformanceObserver' in window)) {
444+
if (!('PerformanceObserver' in WINDOW)) {
445445
return;
446446
}
447447

@@ -476,10 +476,10 @@ export class Replay implements Integration {
476476
*/
477477
removeListeners(): void {
478478
try {
479-
document.removeEventListener('visibilitychange', this.handleVisibilityChange);
479+
WINDOW.document.removeEventListener('visibilitychange', this.handleVisibilityChange);
480480

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

484484
if (this.performanceObserver) {
485485
this.performanceObserver.disconnect();
@@ -666,7 +666,7 @@ export class Replay implements Integration {
666666
* page will also trigger a change to a hidden state.
667667
*/
668668
handleVisibilityChange: () => void = () => {
669-
if (document.visibilityState === 'visible') {
669+
if (WINDOW.document.visibilityState === 'visible') {
670670
this.doChangeToForegroundTasks();
671671
} else {
672672
this.doChangeToBackgroundTasks();
@@ -981,13 +981,13 @@ export class Replay implements Integration {
981981
addMemoryEntry(): Promise<void[]> | undefined {
982982
// window.performance.memory is a non-standard API and doesn't work on all browsers
983983
// so we check before creating the event.
984-
if (!('memory' in window.performance)) {
984+
if (!('memory' in WINDOW.performance)) {
985985
return;
986986
}
987987

988988
return this.createPerformanceSpans([
989989
// @ts-ignore memory doesn't exist on type Performance as the API is non-standard (we check that it exists above)
990-
createMemoryEntry(window.performance.memory),
990+
createMemoryEntry(WINDOW.performance.memory),
991991
]);
992992
}
993993

packages/replay/src/session/deleteSession.ts

Lines changed: 4 additions & 2 deletions
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

Lines changed: 4 additions & 2 deletions
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

Lines changed: 4 additions & 2 deletions
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
}

packages/replay/src/util/isBrowser.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { isNodeEnv } from '@sentry/utils';
2+
3+
export function isBrowser(): boolean {
4+
return !isNodeEnv();
5+
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
const isBrowser = typeof window !== 'undefined';
1+
import { WINDOW } from '@sentry/browser';
2+
import { isBrowser } from './isBrowser';
23

34
/**
45
* Returns true if we are currently recording an internal to Sentry replay
56
* (e.g. on https://sentry.io )
67
*/
78
export function isInternal(): boolean {
8-
return isBrowser && ['sentry.io', 'dev.getsentry.net'].includes(window.location.host);
9+
return isBrowser() && ['sentry.io', 'dev.getsentry.net'].includes(WINDOW.location.host);
910
}

0 commit comments

Comments
 (0)