33/* eslint-disable @typescript-eslint/ban-types */
44import { WrappedFunction } from '@sentry/types' ;
55
6- import { getGlobalObject } from './global ' ;
6+ import { WINDOW } from './browser ' ;
77import { isInstanceOf , isString } from './is' ;
88import { CONSOLE_LEVELS , logger } from './logger' ;
99import { fill } from './object' ;
1010import { getFunctionName } from './stacktrace' ;
1111import { supportsHistory , supportsNativeFetch } from './supports' ;
1212
13- const global = getGlobalObject < Window > ( ) ;
14-
1513export type InstrumentHandlerType =
1614 | 'console'
1715 | 'dom'
@@ -105,22 +103,22 @@ function triggerHandlers(type: InstrumentHandlerType, data: any): void {
105103
106104/** JSDoc */
107105function instrumentConsole ( ) : void {
108- if ( ! ( 'console' in global ) ) {
106+ if ( ! ( 'console' in WINDOW ) ) {
109107 return ;
110108 }
111109
112110 CONSOLE_LEVELS . forEach ( function ( level : string ) : void {
113- if ( ! ( level in global . console ) ) {
111+ if ( ! ( level in WINDOW . console ) ) {
114112 return ;
115113 }
116114
117- fill ( global . console , level , function ( originalConsoleMethod : ( ) => any ) : Function {
115+ fill ( WINDOW . console , level , function ( originalConsoleMethod : ( ) => any ) : Function {
118116 return function ( ...args : any [ ] ) : void {
119117 triggerHandlers ( 'console' , { args, level } ) ;
120118
121119 // this fails for some browsers. :(
122120 if ( originalConsoleMethod ) {
123- originalConsoleMethod . apply ( global . console , args ) ;
121+ originalConsoleMethod . apply ( WINDOW . console , args ) ;
124122 }
125123 } ;
126124 } ) ;
@@ -133,7 +131,7 @@ function instrumentFetch(): void {
133131 return ;
134132 }
135133
136- fill ( global , 'fetch' , function ( originalFetch : ( ) => void ) : ( ) => void {
134+ fill ( WINDOW , 'fetch' , function ( originalFetch : ( ) => void ) : ( ) => void {
137135 return function ( ...args : any [ ] ) : void {
138136 const handlerData = {
139137 args,
@@ -149,7 +147,7 @@ function instrumentFetch(): void {
149147 } ) ;
150148
151149 // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
152- return originalFetch . apply ( global , args ) . then (
150+ return originalFetch . apply ( WINDOW , args ) . then (
153151 ( response : Response ) => {
154152 triggerHandlers ( 'fetch' , {
155153 ...handlerData ,
@@ -190,7 +188,7 @@ interface SentryWrappedXMLHttpRequest extends XMLHttpRequest {
190188/* eslint-disable @typescript-eslint/no-unsafe-member-access */
191189/** Extract `method` from fetch call arguments */
192190function 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 ) {
194192 return String ( fetchArgs [ 0 ] . method ) . toUpperCase ( ) ;
195193 }
196194 if ( fetchArgs [ 1 ] && fetchArgs [ 1 ] . method ) {
@@ -204,7 +202,7 @@ function getFetchUrl(fetchArgs: any[] = []): string {
204202 if ( typeof fetchArgs [ 0 ] === 'string' ) {
205203 return fetchArgs [ 0 ] ;
206204 }
207- if ( 'Request' in global && isInstanceOf ( fetchArgs [ 0 ] , Request ) ) {
205+ if ( 'Request' in WINDOW && isInstanceOf ( fetchArgs [ 0 ] , Request ) ) {
208206 return fetchArgs [ 0 ] . url ;
209207 }
210208 return String ( fetchArgs [ 0 ] ) ;
@@ -213,7 +211,7 @@ function getFetchUrl(fetchArgs: any[] = []): string {
213211
214212/** JSDoc */
215213function instrumentXHR ( ) : void {
216- if ( ! ( 'XMLHttpRequest' in global ) ) {
214+ if ( ! ( 'XMLHttpRequest' in WINDOW ) ) {
217215 return ;
218216 }
219217
@@ -295,9 +293,9 @@ function instrumentHistory(): void {
295293 return ;
296294 }
297295
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 ;
301299 // keep track of the current URL state, as we always receive only the updated state
302300 const from = lastHref ;
303301 lastHref = to ;
@@ -336,8 +334,8 @@ function instrumentHistory(): void {
336334 } ;
337335 }
338336
339- fill ( global . history , 'pushState' , historyReplacementFunction ) ;
340- fill ( global . history , 'replaceState' , historyReplacementFunction ) ;
337+ fill ( WINDOW . history , 'pushState' , historyReplacementFunction ) ;
338+ fill ( WINDOW . history , 'replaceState' , historyReplacementFunction ) ;
341339}
342340
343341const debounceDuration = 1000 ;
@@ -452,7 +450,7 @@ function makeDOMEventHandler(handler: Function, globalListener: boolean = false)
452450
453451 // Start a new debounce timer that will prevent us from capturing multiple events that should be grouped together.
454452 clearTimeout ( debounceTimerID ) ;
455- debounceTimerID = global . setTimeout ( ( ) => {
453+ debounceTimerID = WINDOW . setTimeout ( ( ) => {
456454 debounceTimerID = undefined ;
457455 } , debounceDuration ) ;
458456 } ;
@@ -481,7 +479,7 @@ type InstrumentedElement = Element & {
481479
482480/** JSDoc */
483481function instrumentDOM ( ) : void {
484- if ( ! ( 'document' in global ) ) {
482+ if ( ! ( 'document' in WINDOW ) ) {
485483 return ;
486484 }
487485
@@ -490,8 +488,8 @@ function instrumentDOM(): void {
490488 // we instrument `addEventListener` so that we don't end up attaching this handler twice.
491489 const triggerDOMHandler = triggerHandlers . bind ( null , 'dom' ) ;
492490 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 ) ;
495493
496494 // After hooking into click and keypress events bubbled up to `document`, we also hook into user-handled
497495 // 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 {
500498 // guaranteed to fire at least once.)
501499 [ 'EventTarget' , 'Node' ] . forEach ( ( target : string ) => {
502500 // 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 ;
504502 // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, no-prototype-builtins
505503 if ( ! proto || ! proto . hasOwnProperty || ! proto . hasOwnProperty ( 'addEventListener' ) ) {
506504 return ;
@@ -582,9 +580,9 @@ function instrumentDOM(): void {
582580let _oldOnErrorHandler : OnErrorEventHandler = null ;
583581/** JSDoc */
584582function instrumentError ( ) : void {
585- _oldOnErrorHandler = global . onerror ;
583+ _oldOnErrorHandler = WINDOW . onerror ;
586584
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 {
588586 triggerHandlers ( 'error' , {
589587 column,
590588 error,
@@ -605,9 +603,9 @@ function instrumentError(): void {
605603let _oldOnUnhandledRejectionHandler : ( ( e : any ) => void ) | null = null ;
606604/** JSDoc */
607605function instrumentUnhandledRejection ( ) : void {
608- _oldOnUnhandledRejectionHandler = global . onunhandledrejection ;
606+ _oldOnUnhandledRejectionHandler = WINDOW . onunhandledrejection ;
609607
610- global . onunhandledrejection = function ( e : any ) : boolean {
608+ WINDOW . onunhandledrejection = function ( e : any ) : boolean {
611609 triggerHandlers ( 'unhandledrejection' , e ) ;
612610
613611 if ( _oldOnUnhandledRejectionHandler ) {
0 commit comments