1
- import type { Integration , IntegrationClass , IntegrationFn , WrappedFunction } from '@sentry/types' ;
1
+ import type { Client , Integration , IntegrationClass , IntegrationFn , WrappedFunction } from '@sentry/types' ;
2
2
import { getOriginalFunction } from '@sentry/utils' ;
3
+ import { getClient } from '../exports' ;
3
4
import { convertIntegrationFnToClass , defineIntegration } from '../integration' ;
4
5
5
6
let originalFunctionToString : ( ) => void ;
6
7
7
8
const INTEGRATION_NAME = 'FunctionToString' ;
8
9
10
+ const SETUP_CLIENTS = new WeakMap < Client , boolean > ( ) ;
11
+
9
12
const _functionToStringIntegration = ( ( ) => {
10
13
return {
11
14
name : INTEGRATION_NAME ,
@@ -18,24 +21,44 @@ const _functionToStringIntegration = (() => {
18
21
try {
19
22
// eslint-disable-next-line @typescript-eslint/no-explicit-any
20
23
Function . prototype . toString = function ( this : WrappedFunction , ...args : any [ ] ) : string {
21
- const context = getOriginalFunction ( this ) || this ;
24
+ const originalFunction = getOriginalFunction ( this ) ;
25
+ const context =
26
+ SETUP_CLIENTS . has ( getClient ( ) as Client ) && originalFunction !== undefined ? originalFunction : this ;
22
27
return originalFunctionToString . apply ( context , args ) ;
23
28
} ;
24
29
} catch {
25
30
// ignore errors here, just don't patch this
26
31
}
27
32
} ,
33
+ setup ( client ) {
34
+ SETUP_CLIENTS . set ( client , true ) ;
35
+ } ,
28
36
} ;
29
37
} ) satisfies IntegrationFn ;
30
38
39
+ /**
40
+ * Patch toString calls to return proper name for wrapped functions.
41
+ *
42
+ * ```js
43
+ * Sentry.init({
44
+ * integrations: [
45
+ * functionToStringIntegration(),
46
+ * ],
47
+ * });
48
+ * ```
49
+ */
31
50
export const functionToStringIntegration = defineIntegration ( _functionToStringIntegration ) ;
32
51
33
52
/**
34
53
* Patch toString calls to return proper name for wrapped functions.
54
+ *
35
55
* @deprecated Use `functionToStringIntegration()` instead.
36
56
*/
37
57
// eslint-disable-next-line deprecation/deprecation
38
58
export const FunctionToString = convertIntegrationFnToClass (
39
59
INTEGRATION_NAME ,
40
60
functionToStringIntegration ,
41
61
) as IntegrationClass < Integration & { setupOnce : ( ) => void } > ;
62
+
63
+ // eslint-disable-next-line deprecation/deprecation
64
+ export type FunctionToString = typeof FunctionToString ;
0 commit comments