Skip to content

Commit 1ae28f2

Browse files
committed
feat: Add getGlobalObject method to utils
1 parent e3d862d commit 1ae28f2

File tree

6 files changed

+37
-17
lines changed

6 files changed

+37
-17
lines changed
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
import { captureException } from '@sentry/minimal';
22
import { Integration } from '@sentry/types';
3+
import { getGlobalObject } from '@sentry/utils';
34

4-
/** onunhandledrejection is not standardized, thus not available on Window type */
5-
interface PromisifiedWindow extends Window {
6-
onunhandledrejection?(event: PromiseRejectionEvent): void;
7-
}
8-
9-
/** TODO: Change to safe window access, window||global||self||{} */
10-
const _window: PromisifiedWindow = window;
5+
const global: any = getGlobalObject();
116

127
/** Global Promise Rejection handler */
138
export class OnUnhandledRejection implements Integration {
@@ -25,8 +20,10 @@ export class OnUnhandledRejection implements Integration {
2520
* @inheritDoc
2621
*/
2722
public install(): void {
28-
_window.addEventListener('unhandledrejection', this.handler.bind(
29-
this,
30-
) as EventListener);
23+
if (global.addEventListener) {
24+
global.addEventListener('unhandledrejection', this.handler.bind(
25+
this,
26+
) as EventListener);
27+
}
3128
}
3229
}

packages/browser/src/transports/fetch.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { SentryEvent } from '@sentry/types';
2-
import { serialize, supportsReferrerPolicy } from '@sentry/utils';
2+
import {
3+
getGlobalObject,
4+
serialize,
5+
supportsReferrerPolicy,
6+
} from '@sentry/utils';
37
import { BaseTransport } from './base';
48

9+
const global: any = getGlobalObject();
10+
511
/** `fetch` based transport */
612
export class FetchTransport extends BaseTransport {
713
/**
@@ -21,7 +27,6 @@ export class FetchTransport extends BaseTransport {
2127
: '') as ReferrerPolicy,
2228
};
2329

24-
// TODO: Safe _window access
25-
return window.fetch(this.url, defaultOptions);
30+
return global.fetch(this.url, defaultOptions);
2631
}
2732
}

packages/utils/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ export {
1111
supportsFetch,
1212
supportsReferrerPolicy,
1313
} from './supports';
14+
export { getGlobalObject } from './misc';

packages/utils/src/misc.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Safely get global scope object
3+
*
4+
* @returns Global scope object
5+
*/
6+
// tslint:disable:strict-type-predicates
7+
export function getGlobalObject(): Window | NodeJS.Global | {} {
8+
return typeof window !== 'undefined'
9+
? window
10+
: typeof global !== 'undefined'
11+
? global
12+
: typeof self !== 'undefined' ? self : {};
13+
}
14+
// tslint:enable:strict-type-predicates

packages/utils/src/string.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { getGlobalObject } from './misc';
2+
13
/**
24
* Encodes given object into url-friendly format
35
*
@@ -26,8 +28,8 @@ interface MsCryptoWindow extends Window {
2628
* @returns string Generated UUID4.
2729
*/
2830
export function uuid4(): string {
29-
// TODO: Safe window access
30-
const crypto = window.crypto || (window as MsCryptoWindow).msCrypto;
31+
const global = getGlobalObject() as MsCryptoWindow;
32+
const crypto = global.crypto || global.msCrypto;
3133

3234
if (!(crypto === void 0) && crypto.getRandomValues) {
3335
// Use window.crypto API if available

packages/utils/src/supports.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { getGlobalObject } from './misc';
2+
13
/**
24
* Tells whether current environment supports ErrorEvent objects
35
* {@link supportsErrorEvent}.
@@ -57,8 +59,7 @@ export function supportsDOMException(): boolean {
5759
* @returns Answer to the given question.
5860
*/
5961
export function supportsFetch(): boolean {
60-
// TODO: Safe window access
61-
if (!('fetch' in window)) {
62+
if (!('fetch' in getGlobalObject())) {
6263
return false;
6364
}
6465

0 commit comments

Comments
 (0)