Skip to content

Commit 5446c36

Browse files
authored
ref(browser): Refactor isNativeFetch -> isNativeFunction (#12166)
This makes the `isNativeFetch` check a bit more generic/naive. It no longer checks that the function name matches `fetch` explicitly, but I think thats ok. Follow-up to #11924
1 parent eea57bf commit 5446c36

File tree

2 files changed

+8
-15
lines changed

2 files changed

+8
-15
lines changed

packages/browser-utils/src/getNativeImplementation.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { logger } from '@sentry/utils';
1+
import { isNativeFunction, logger } from '@sentry/utils';
22
import { DEBUG_BUILD } from './debug-build';
33
import { WINDOW } from './types';
44

@@ -15,14 +15,6 @@ interface CacheableImplementations {
1515

1616
const cachedImplementations: Partial<CacheableImplementations> = {};
1717

18-
/**
19-
* isNative checks if the given function is a native implementation
20-
*/
21-
// eslint-disable-next-line @typescript-eslint/ban-types
22-
function isNative(func: Function): boolean {
23-
return func && /^function\s+\w+\(\)\s+\{\s+\[native code\]\s+\}$/.test(func.toString());
24-
}
25-
2618
/**
2719
* Get the native implementation of a browser function.
2820
*
@@ -43,7 +35,7 @@ export function getNativeImplementation<T extends keyof CacheableImplementations
4335
let impl = WINDOW[name] as CacheableImplementations[T];
4436

4537
// Fast path to avoid DOM I/O
46-
if (isNative(impl)) {
38+
if (isNativeFunction(impl)) {
4739
return (cachedImplementations[name] = impl.bind(WINDOW) as CacheableImplementations[T]);
4840
}
4941

packages/utils/src/supports.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,13 @@ export function supportsFetch(): boolean {
7676
return false;
7777
}
7878
}
79+
7980
/**
80-
* isNativeFetch checks if the given function is a native implementation of fetch()
81+
* isNative checks if the given function is a native implementation
8182
*/
8283
// eslint-disable-next-line @typescript-eslint/ban-types
83-
export function isNativeFetch(func: Function): boolean {
84-
return func && /^function fetch\(\)\s+\{\s+\[native code\]\s+\}$/.test(func.toString());
84+
export function isNativeFunction(func: Function): boolean {
85+
return func && /^function\s+\w+\(\)\s+\{\s+\[native code\]\s+\}$/.test(func.toString());
8586
}
8687

8788
/**
@@ -101,7 +102,7 @@ export function supportsNativeFetch(): boolean {
101102

102103
// Fast path to avoid DOM I/O
103104
// eslint-disable-next-line @typescript-eslint/unbound-method
104-
if (isNativeFetch(WINDOW.fetch)) {
105+
if (isNativeFunction(WINDOW.fetch)) {
105106
return true;
106107
}
107108

@@ -117,7 +118,7 @@ export function supportsNativeFetch(): boolean {
117118
doc.head.appendChild(sandbox);
118119
if (sandbox.contentWindow && sandbox.contentWindow.fetch) {
119120
// eslint-disable-next-line @typescript-eslint/unbound-method
120-
result = isNativeFetch(sandbox.contentWindow.fetch);
121+
result = isNativeFunction(sandbox.contentWindow.fetch);
121122
}
122123
doc.head.removeChild(sandbox);
123124
} catch (err) {

0 commit comments

Comments
 (0)