File tree Expand file tree Collapse file tree 6 files changed +37
-17
lines changed Expand file tree Collapse file tree 6 files changed +37
-17
lines changed Original file line number Diff line number Diff line change 1
1
import { captureException } from '@sentry/minimal' ;
2
2
import { Integration } from '@sentry/types' ;
3
+ import { getGlobalObject } from '@sentry/utils' ;
3
4
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 ( ) ;
11
6
12
7
/** Global Promise Rejection handler */
13
8
export class OnUnhandledRejection implements Integration {
@@ -25,8 +20,10 @@ export class OnUnhandledRejection implements Integration {
25
20
* @inheritDoc
26
21
*/
27
22
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
+ }
31
28
}
32
29
}
Original file line number Diff line number Diff line change 1
1
import { SentryEvent } from '@sentry/types' ;
2
- import { serialize , supportsReferrerPolicy } from '@sentry/utils' ;
2
+ import {
3
+ getGlobalObject ,
4
+ serialize ,
5
+ supportsReferrerPolicy ,
6
+ } from '@sentry/utils' ;
3
7
import { BaseTransport } from './base' ;
4
8
9
+ const global : any = getGlobalObject ( ) ;
10
+
5
11
/** `fetch` based transport */
6
12
export class FetchTransport extends BaseTransport {
7
13
/**
@@ -21,7 +27,6 @@ export class FetchTransport extends BaseTransport {
21
27
: '' ) as ReferrerPolicy ,
22
28
} ;
23
29
24
- // TODO: Safe _window access
25
- return window . fetch ( this . url , defaultOptions ) ;
30
+ return global . fetch ( this . url , defaultOptions ) ;
26
31
}
27
32
}
Original file line number Diff line number Diff line change @@ -11,3 +11,4 @@ export {
11
11
supportsFetch ,
12
12
supportsReferrerPolicy ,
13
13
} from './supports' ;
14
+ export { getGlobalObject } from './misc' ;
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
1
+ import { getGlobalObject } from './misc' ;
2
+
1
3
/**
2
4
* Encodes given object into url-friendly format
3
5
*
@@ -26,8 +28,8 @@ interface MsCryptoWindow extends Window {
26
28
* @returns string Generated UUID4.
27
29
*/
28
30
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 ;
31
33
32
34
if ( ! ( crypto === void 0 ) && crypto . getRandomValues ) {
33
35
// Use window.crypto API if available
Original file line number Diff line number Diff line change
1
+ import { getGlobalObject } from './misc' ;
2
+
1
3
/**
2
4
* Tells whether current environment supports ErrorEvent objects
3
5
* {@link supportsErrorEvent}.
@@ -57,8 +59,7 @@ export function supportsDOMException(): boolean {
57
59
* @returns Answer to the given question.
58
60
*/
59
61
export function supportsFetch ( ) : boolean {
60
- // TODO: Safe window access
61
- if ( ! ( 'fetch' in window ) ) {
62
+ if ( ! ( 'fetch' in getGlobalObject ( ) ) ) {
62
63
return false ;
63
64
}
64
65
You can’t perform that action at this time.
0 commit comments