@@ -96,8 +96,7 @@ const _browserApiErrorsIntegration = ((options: Partial<BrowserApiErrorsOptions>
96
96
export const browserApiErrorsIntegration = defineIntegration ( _browserApiErrorsIntegration ) ;
97
97
98
98
function _wrapTimeFunction ( original : ( ) => void ) : ( ) => number {
99
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
100
- return function ( this : any , ...args : any [ ] ) : number {
99
+ return function ( this : unknown , ...args : unknown [ ] ) : number {
101
100
const originalCallback = args [ 0 ] ;
102
101
args [ 0 ] = wrap ( originalCallback , {
103
102
mechanism : {
@@ -110,11 +109,8 @@ function _wrapTimeFunction(original: () => void): () => number {
110
109
} ;
111
110
}
112
111
113
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
114
- function _wrapRAF ( original : any ) : ( callback : ( ) => void ) => any {
115
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
116
- return function ( this : any , callback : ( ) => void ) : ( ) => void {
117
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
112
+ function _wrapRAF ( original : ( ) => void ) : ( callback : ( ) => void ) => unknown {
113
+ return function ( this : unknown , callback : ( ) => void ) : ( ) => void {
118
114
return original . apply ( this , [
119
115
wrap ( callback , {
120
116
mechanism : {
@@ -131,16 +127,14 @@ function _wrapRAF(original: any): (callback: () => void) => any {
131
127
}
132
128
133
129
function _wrapXHR ( originalSend : ( ) => void ) : ( ) => void {
134
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
135
- return function ( this : XMLHttpRequest , ...args : any [ ] ) : void {
130
+ return function ( this : XMLHttpRequest , ...args : unknown [ ] ) : void {
136
131
// eslint-disable-next-line @typescript-eslint/no-this-alias
137
132
const xhr = this ;
138
133
const xmlHttpRequestProps : XMLHttpRequestProp [ ] = [ 'onload' , 'onerror' , 'onprogress' , 'onreadystatechange' ] ;
139
134
140
135
xmlHttpRequestProps . forEach ( prop => {
141
136
if ( prop in xhr && typeof xhr [ prop ] === 'function' ) {
142
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
143
- fill ( xhr , prop , function ( original : WrappedFunction ) : ( ) => any {
137
+ fill ( xhr , prop , function ( original ) {
144
138
const wrapOptions = {
145
139
mechanism : {
146
140
data : {
@@ -169,30 +163,21 @@ function _wrapXHR(originalSend: () => void): () => void {
169
163
}
170
164
171
165
function _wrapEventTarget ( target : string ) : void {
172
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
173
- const globalObject = WINDOW as { [ key : string ] : any } ;
174
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
175
- const proto = globalObject [ target ] && globalObject [ target ] . prototype ;
166
+ const globalObject = WINDOW as unknown as Record < string , { prototype ?: object } > ;
167
+ const targetObj = globalObject [ target ] ;
168
+ const proto = targetObj && targetObj . prototype ;
176
169
177
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, no-prototype-builtins
170
+ // eslint-disable-next-line no-prototype-builtins
178
171
if ( ! proto || ! proto . hasOwnProperty || ! proto . hasOwnProperty ( 'addEventListener' ) ) {
179
172
return ;
180
173
}
181
174
182
175
fill ( proto , 'addEventListener' , function ( original : VoidFunction , ) : (
183
- eventName : string ,
184
- fn : EventListenerObject ,
185
- options ?: boolean | AddEventListenerOptions ,
186
- ) => void {
187
- return function (
188
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
189
- this : any ,
190
- eventName : string ,
191
- fn : EventListenerObject ,
192
- options ?: boolean | AddEventListenerOptions ,
193
- ) : ( eventName : string , fn : EventListenerObject , capture ?: boolean , secure ?: boolean ) => void {
176
+ ...args : Parameters < typeof WINDOW . addEventListener >
177
+ ) => ReturnType < typeof WINDOW . addEventListener > {
178
+ return function ( this : unknown , eventName , fn , options ) : VoidFunction {
194
179
try {
195
- if ( typeof fn . handleEvent === 'function' ) {
180
+ if ( isEventListenerObject ( fn ) ) {
196
181
// ESlint disable explanation:
197
182
// First, it is generally safe to call `wrap` with an unbound function. Furthermore, using `.bind()` would
198
183
// introduce a bug here, because bind returns a new function that doesn't have our
@@ -211,14 +196,13 @@ function _wrapEventTarget(target: string): void {
211
196
} ,
212
197
} ) ;
213
198
}
214
- } catch ( err ) {
199
+ } catch {
215
200
// can sometimes get 'Permission denied to access property "handle Event'
216
201
}
217
202
218
203
return original . apply ( this , [
219
204
eventName ,
220
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
221
- wrap ( fn as any as WrappedFunction , {
205
+ wrap ( fn , {
222
206
mechanism : {
223
207
data : {
224
208
function : 'addEventListener' ,
@@ -234,48 +218,41 @@ function _wrapEventTarget(target: string): void {
234
218
} ;
235
219
} ) ;
236
220
237
- fill (
238
- proto ,
239
- 'removeEventListener' ,
240
- function (
241
- originalRemoveEventListener : ( ) => void ,
242
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
243
- ) : ( this : any , eventName : string , fn : EventListenerObject , options ?: boolean | EventListenerOptions ) => ( ) => void {
244
- return function (
245
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
246
- this : any ,
247
- eventName : string ,
248
- fn : EventListenerObject ,
249
- options ?: boolean | EventListenerOptions ,
250
- ) : ( ) => void {
251
- /**
252
- * There are 2 possible scenarios here:
253
- *
254
- * 1. Someone passes a callback, which was attached prior to Sentry initialization, or by using unmodified
255
- * method, eg. `document.addEventListener.call(el, name, handler). In this case, we treat this function
256
- * as a pass-through, and call original `removeEventListener` with it.
257
- *
258
- * 2. Someone passes a callback, which was attached after Sentry was initialized, which means that it was using
259
- * our wrapped version of `addEventListener`, which internally calls `wrap` helper.
260
- * This helper "wraps" whole callback inside a try/catch statement, and attached appropriate metadata to it,
261
- * in order for us to make a distinction between wrapped/non-wrapped functions possible.
262
- * If a function was wrapped, it has additional property of `__sentry_wrapped__`, holding the handler.
263
- *
264
- * When someone adds a handler prior to initialization, and then do it again, but after,
265
- * then we have to detach both of them. Otherwise, if we'd detach only wrapped one, it'd be impossible
266
- * to get rid of the initial handler and it'd stick there forever.
267
- */
268
- const wrappedEventHandler = fn as unknown as WrappedFunction ;
269
- try {
270
- const originalEventHandler = wrappedEventHandler && wrappedEventHandler . __sentry_wrapped__ ;
271
- if ( originalEventHandler ) {
272
- originalRemoveEventListener . call ( this , eventName , originalEventHandler , options ) ;
273
- }
274
- } catch ( e ) {
275
- // ignore, accessing __sentry_wrapped__ will throw in some Selenium environments
221
+ fill ( proto , 'removeEventListener' , function ( originalRemoveEventListener : VoidFunction , ) : (
222
+ this : unknown ,
223
+ ...args : Parameters < typeof WINDOW . removeEventListener >
224
+ ) => ReturnType < typeof WINDOW . removeEventListener > {
225
+ return function ( this : unknown , eventName , fn , options ) : VoidFunction {
226
+ /**
227
+ * There are 2 possible scenarios here:
228
+ *
229
+ * 1. Someone passes a callback, which was attached prior to Sentry initialization, or by using unmodified
230
+ * method, eg. `document.addEventListener.call(el, name, handler). In this case, we treat this function
231
+ * as a pass-through, and call original `removeEventListener` with it.
232
+ *
233
+ * 2. Someone passes a callback, which was attached after Sentry was initialized, which means that it was using
234
+ * our wrapped version of `addEventListener`, which internally calls `wrap` helper.
235
+ * This helper "wraps" whole callback inside a try/catch statement, and attached appropriate metadata to it,
236
+ * in order for us to make a distinction between wrapped/non-wrapped functions possible.
237
+ * If a function was wrapped, it has additional property of `__sentry_wrapped__`, holding the handler.
238
+ *
239
+ * When someone adds a handler prior to initialization, and then do it again, but after,
240
+ * then we have to detach both of them. Otherwise, if we'd detach only wrapped one, it'd be impossible
241
+ * to get rid of the initial handler and it'd stick there forever.
242
+ */
243
+ try {
244
+ const originalEventHandler = ( fn as WrappedFunction ) . __sentry_wrapped__ ;
245
+ if ( originalEventHandler ) {
246
+ originalRemoveEventListener . call ( this , eventName , originalEventHandler , options ) ;
276
247
}
277
- return originalRemoveEventListener . call ( this , eventName , wrappedEventHandler , options ) ;
278
- } ;
279
- } ,
280
- ) ;
248
+ } catch ( e ) {
249
+ // ignore, accessing __sentry_wrapped__ will throw in some Selenium environments
250
+ }
251
+ return originalRemoveEventListener . call ( this , eventName , fn , options ) ;
252
+ } ;
253
+ } ) ;
254
+ }
255
+
256
+ function isEventListenerObject ( obj : unknown ) : obj is EventListenerObject {
257
+ return typeof ( obj as EventListenerObject ) . handleEvent === 'function' ;
281
258
}
0 commit comments