3
3
/* eslint-disable @typescript-eslint/ban-types */
4
4
import { WrappedFunction } from '@sentry/types' ;
5
5
6
- import { getGlobalObject } from './global ' ;
6
+ import { WINDOW } from './browser ' ;
7
7
import { isInstanceOf , isString } from './is' ;
8
8
import { CONSOLE_LEVELS , logger } from './logger' ;
9
9
import { fill } from './object' ;
10
10
import { getFunctionName } from './stacktrace' ;
11
11
import { supportsHistory , supportsNativeFetch } from './supports' ;
12
12
13
- const global = getGlobalObject < Window > ( ) ;
14
-
15
13
export type InstrumentHandlerType =
16
14
| 'console'
17
15
| 'dom'
@@ -105,22 +103,22 @@ function triggerHandlers(type: InstrumentHandlerType, data: any): void {
105
103
106
104
/** JSDoc */
107
105
function instrumentConsole ( ) : void {
108
- if ( ! ( 'console' in global ) ) {
106
+ if ( ! ( 'console' in WINDOW ) ) {
109
107
return ;
110
108
}
111
109
112
110
CONSOLE_LEVELS . forEach ( function ( level : string ) : void {
113
- if ( ! ( level in global . console ) ) {
111
+ if ( ! ( level in WINDOW . console ) ) {
114
112
return ;
115
113
}
116
114
117
- fill ( global . console , level , function ( originalConsoleMethod : ( ) => any ) : Function {
115
+ fill ( WINDOW . console , level , function ( originalConsoleMethod : ( ) => any ) : Function {
118
116
return function ( ...args : any [ ] ) : void {
119
117
triggerHandlers ( 'console' , { args, level } ) ;
120
118
121
119
// this fails for some browsers. :(
122
120
if ( originalConsoleMethod ) {
123
- originalConsoleMethod . apply ( global . console , args ) ;
121
+ originalConsoleMethod . apply ( WINDOW . console , args ) ;
124
122
}
125
123
} ;
126
124
} ) ;
@@ -133,7 +131,7 @@ function instrumentFetch(): void {
133
131
return ;
134
132
}
135
133
136
- fill ( global , 'fetch' , function ( originalFetch : ( ) => void ) : ( ) => void {
134
+ fill ( WINDOW , 'fetch' , function ( originalFetch : ( ) => void ) : ( ) => void {
137
135
return function ( ...args : any [ ] ) : void {
138
136
const handlerData = {
139
137
args,
@@ -149,7 +147,7 @@ function instrumentFetch(): void {
149
147
} ) ;
150
148
151
149
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
152
- return originalFetch . apply ( global , args ) . then (
150
+ return originalFetch . apply ( WINDOW , args ) . then (
153
151
( response : Response ) => {
154
152
triggerHandlers ( 'fetch' , {
155
153
...handlerData ,
@@ -190,7 +188,7 @@ interface SentryWrappedXMLHttpRequest extends XMLHttpRequest {
190
188
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
191
189
/** Extract `method` from fetch call arguments */
192
190
function getFetchMethod ( fetchArgs : any [ ] = [ ] ) : string {
193
- if ( 'Request' in global && isInstanceOf ( fetchArgs [ 0 ] , Request ) && fetchArgs [ 0 ] . method ) {
191
+ if ( 'Request' in WINDOW && isInstanceOf ( fetchArgs [ 0 ] , Request ) && fetchArgs [ 0 ] . method ) {
194
192
return String ( fetchArgs [ 0 ] . method ) . toUpperCase ( ) ;
195
193
}
196
194
if ( fetchArgs [ 1 ] && fetchArgs [ 1 ] . method ) {
@@ -204,7 +202,7 @@ function getFetchUrl(fetchArgs: any[] = []): string {
204
202
if ( typeof fetchArgs [ 0 ] === 'string' ) {
205
203
return fetchArgs [ 0 ] ;
206
204
}
207
- if ( 'Request' in global && isInstanceOf ( fetchArgs [ 0 ] , Request ) ) {
205
+ if ( 'Request' in WINDOW && isInstanceOf ( fetchArgs [ 0 ] , Request ) ) {
208
206
return fetchArgs [ 0 ] . url ;
209
207
}
210
208
return String ( fetchArgs [ 0 ] ) ;
@@ -213,7 +211,7 @@ function getFetchUrl(fetchArgs: any[] = []): string {
213
211
214
212
/** JSDoc */
215
213
function instrumentXHR ( ) : void {
216
- if ( ! ( 'XMLHttpRequest' in global ) ) {
214
+ if ( ! ( 'XMLHttpRequest' in WINDOW ) ) {
217
215
return ;
218
216
}
219
217
@@ -295,9 +293,9 @@ function instrumentHistory(): void {
295
293
return ;
296
294
}
297
295
298
- const oldOnPopState = global . onpopstate ;
299
- global . onpopstate = function ( this : WindowEventHandlers , ...args : any [ ] ) : any {
300
- const to = global . location . href ;
296
+ const oldOnPopState = WINDOW . onpopstate ;
297
+ WINDOW . onpopstate = function ( this : WindowEventHandlers , ...args : any [ ] ) : any {
298
+ const to = WINDOW . location . href ;
301
299
// keep track of the current URL state, as we always receive only the updated state
302
300
const from = lastHref ;
303
301
lastHref = to ;
@@ -336,8 +334,8 @@ function instrumentHistory(): void {
336
334
} ;
337
335
}
338
336
339
- fill ( global . history , 'pushState' , historyReplacementFunction ) ;
340
- fill ( global . history , 'replaceState' , historyReplacementFunction ) ;
337
+ fill ( WINDOW . history , 'pushState' , historyReplacementFunction ) ;
338
+ fill ( WINDOW . history , 'replaceState' , historyReplacementFunction ) ;
341
339
}
342
340
343
341
const debounceDuration = 1000 ;
@@ -452,7 +450,7 @@ function makeDOMEventHandler(handler: Function, globalListener: boolean = false)
452
450
453
451
// Start a new debounce timer that will prevent us from capturing multiple events that should be grouped together.
454
452
clearTimeout ( debounceTimerID ) ;
455
- debounceTimerID = global . setTimeout ( ( ) => {
453
+ debounceTimerID = WINDOW . setTimeout ( ( ) => {
456
454
debounceTimerID = undefined ;
457
455
} , debounceDuration ) ;
458
456
} ;
@@ -481,7 +479,7 @@ type InstrumentedElement = Element & {
481
479
482
480
/** JSDoc */
483
481
function instrumentDOM ( ) : void {
484
- if ( ! ( 'document' in global ) ) {
482
+ if ( ! ( 'document' in WINDOW ) ) {
485
483
return ;
486
484
}
487
485
@@ -490,8 +488,8 @@ function instrumentDOM(): void {
490
488
// we instrument `addEventListener` so that we don't end up attaching this handler twice.
491
489
const triggerDOMHandler = triggerHandlers . bind ( null , 'dom' ) ;
492
490
const globalDOMEventHandler = makeDOMEventHandler ( triggerDOMHandler , true ) ;
493
- global . document . addEventListener ( 'click' , globalDOMEventHandler , false ) ;
494
- global . document . addEventListener ( 'keypress' , globalDOMEventHandler , false ) ;
491
+ WINDOW . document . addEventListener ( 'click' , globalDOMEventHandler , false ) ;
492
+ WINDOW . document . addEventListener ( 'keypress' , globalDOMEventHandler , false ) ;
495
493
496
494
// After hooking into click and keypress events bubbled up to `document`, we also hook into user-handled
497
495
// clicks & keypresses, by adding an event listener of our own to any element to which they add a listener. That
@@ -500,7 +498,7 @@ function instrumentDOM(): void {
500
498
// guaranteed to fire at least once.)
501
499
[ 'EventTarget' , 'Node' ] . forEach ( ( target : string ) => {
502
500
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
503
- const proto = ( global as any ) [ target ] && ( global as any ) [ target ] . prototype ;
501
+ const proto = ( WINDOW as any ) [ target ] && ( WINDOW as any ) [ target ] . prototype ;
504
502
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, no-prototype-builtins
505
503
if ( ! proto || ! proto . hasOwnProperty || ! proto . hasOwnProperty ( 'addEventListener' ) ) {
506
504
return ;
@@ -582,9 +580,9 @@ function instrumentDOM(): void {
582
580
let _oldOnErrorHandler : OnErrorEventHandler = null ;
583
581
/** JSDoc */
584
582
function instrumentError ( ) : void {
585
- _oldOnErrorHandler = global . onerror ;
583
+ _oldOnErrorHandler = WINDOW . onerror ;
586
584
587
- global . onerror = function ( msg : any , url : any , line : any , column : any , error : any ) : boolean {
585
+ WINDOW . onerror = function ( msg : any , url : any , line : any , column : any , error : any ) : boolean {
588
586
triggerHandlers ( 'error' , {
589
587
column,
590
588
error,
@@ -605,9 +603,9 @@ function instrumentError(): void {
605
603
let _oldOnUnhandledRejectionHandler : ( ( e : any ) => void ) | null = null ;
606
604
/** JSDoc */
607
605
function instrumentUnhandledRejection ( ) : void {
608
- _oldOnUnhandledRejectionHandler = global . onunhandledrejection ;
606
+ _oldOnUnhandledRejectionHandler = WINDOW . onunhandledrejection ;
609
607
610
- global . onunhandledrejection = function ( e : any ) : boolean {
608
+ WINDOW . onunhandledrejection = function ( e : any ) : boolean {
611
609
triggerHandlers ( 'unhandledrejection' , e ) ;
612
610
613
611
if ( _oldOnUnhandledRejectionHandler ) {
0 commit comments