|
1 |
| -import type { IntegrationFn, WrappedFunction } from '@sentry/types'; |
| 1 | +import type { Integration, WrappedFunction } from '@sentry/types'; |
2 | 2 | import { getOriginalFunction } from '@sentry/utils';
|
3 |
| -import { convertIntegrationFnToClass } from '../integration'; |
| 3 | +import { defineSentryIntegration } from '../integration'; |
4 | 4 |
|
5 | 5 | let originalFunctionToString: () => void;
|
6 | 6 |
|
7 | 7 | const INTEGRATION_NAME = 'FunctionToString';
|
8 | 8 |
|
9 |
| -const functionToStringIntegration: IntegrationFn = () => { |
10 |
| - return { |
11 |
| - name: INTEGRATION_NAME, |
12 |
| - setupOnce() { |
13 |
| - // eslint-disable-next-line @typescript-eslint/unbound-method |
14 |
| - originalFunctionToString = Function.prototype.toString; |
15 |
| - |
16 |
| - // intrinsics (like Function.prototype) might be immutable in some environments |
17 |
| - // e.g. Node with --frozen-intrinsics, XS (an embedded JavaScript engine) or SES (a JavaScript proposal) |
18 |
| - try { |
19 |
| - // eslint-disable-next-line @typescript-eslint/no-explicit-any |
20 |
| - Function.prototype.toString = function (this: WrappedFunction, ...args: any[]): string { |
21 |
| - const context = getOriginalFunction(this) || this; |
22 |
| - return originalFunctionToString.apply(context, args); |
23 |
| - }; |
24 |
| - } catch { |
25 |
| - // ignore errors here, just don't patch this |
26 |
| - } |
27 |
| - }, |
28 |
| - }; |
29 |
| -}; |
30 |
| - |
31 |
| -/** Patch toString calls to return proper name for wrapped functions */ |
32 |
| -// eslint-disable-next-line deprecation/deprecation |
33 |
| -export const FunctionToString = convertIntegrationFnToClass(INTEGRATION_NAME, functionToStringIntegration); |
| 9 | +/** |
| 10 | + * Patch toString calls to return proper name for wrapped functions |
| 11 | + */ |
| 12 | +export const functionToStringIntegration = defineSentryIntegration(() => { |
| 13 | + // eslint-disable-next-line deprecation/deprecation |
| 14 | + return new FunctionToString(); |
| 15 | +}); |
| 16 | + |
| 17 | +/** |
| 18 | + * Patch toString calls to return proper name for wrapped functions |
| 19 | + * |
| 20 | + * @deprecated Use `functionToStringIntegration()` instead. |
| 21 | + */ |
| 22 | +export class FunctionToString implements Integration { |
| 23 | + public static id = INTEGRATION_NAME; |
| 24 | + |
| 25 | + public name: typeof INTEGRATION_NAME; |
| 26 | + |
| 27 | + public constructor() { |
| 28 | + this.name = INTEGRATION_NAME; |
| 29 | + } |
| 30 | + |
| 31 | + // eslint-disable-next-line jsdoc/require-jsdoc |
| 32 | + public setupOnce(): void { |
| 33 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 34 | + originalFunctionToString = Function.prototype.toString; |
| 35 | + |
| 36 | + // intrinsics (like Function.prototype) might be immutable in some environments |
| 37 | + // e.g. Node with --frozen-intrinsics, XS (an embedded JavaScript engine) or SES (a JavaScript proposal) |
| 38 | + try { |
| 39 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 40 | + Function.prototype.toString = function (this: WrappedFunction, ...args: any[]): string { |
| 41 | + const context = getOriginalFunction(this) || this; |
| 42 | + return originalFunctionToString.apply(context, args); |
| 43 | + }; |
| 44 | + } catch { |
| 45 | + // ignore errors here, just don't patch this |
| 46 | + } |
| 47 | + } |
| 48 | +} |
0 commit comments