Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref(browser): Refactor isNativeFetch -> isNativeFunction #12166

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions packages/browser-utils/src/getNativeImplementation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logger } from '@sentry/utils';
import { isNativeFunction, logger } from '@sentry/utils';
import { DEBUG_BUILD } from './debug-build';
import { WINDOW } from './types';

Expand All @@ -15,14 +15,6 @@ interface CacheableImplementations {

const cachedImplementations: Partial<CacheableImplementations> = {};

/**
* isNative checks if the given function is a native implementation
*/
// eslint-disable-next-line @typescript-eslint/ban-types
function isNative(func: Function): boolean {
return func && /^function\s+\w+\(\)\s+\{\s+\[native code\]\s+\}$/.test(func.toString());
}

/**
* Get the native implementation of a browser function.
*
Expand All @@ -43,7 +35,7 @@ export function getNativeImplementation<T extends keyof CacheableImplementations
let impl = WINDOW[name] as CacheableImplementations[T];

// Fast path to avoid DOM I/O
if (isNative(impl)) {
if (isNativeFunction(impl)) {
return (cachedImplementations[name] = impl.bind(WINDOW) as CacheableImplementations[T]);
}

Expand Down
11 changes: 6 additions & 5 deletions packages/utils/src/supports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,13 @@ export function supportsFetch(): boolean {
return false;
}
}

/**
* isNativeFetch checks if the given function is a native implementation of fetch()
* isNative checks if the given function is a native implementation
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export function isNativeFetch(func: Function): boolean {
return func && /^function fetch\(\)\s+\{\s+\[native code\]\s+\}$/.test(func.toString());
export function isNativeFunction(func: Function): boolean {
return func && /^function\s+\w+\(\)\s+\{\s+\[native code\]\s+\}$/.test(func.toString());
}

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

// Fast path to avoid DOM I/O
// eslint-disable-next-line @typescript-eslint/unbound-method
if (isNativeFetch(WINDOW.fetch)) {
if (isNativeFunction(WINDOW.fetch)) {
return true;
}

Expand All @@ -117,7 +118,7 @@ export function supportsNativeFetch(): boolean {
doc.head.appendChild(sandbox);
if (sandbox.contentWindow && sandbox.contentWindow.fetch) {
// eslint-disable-next-line @typescript-eslint/unbound-method
result = isNativeFetch(sandbox.contentWindow.fetch);
result = isNativeFunction(sandbox.contentWindow.fetch);
}
doc.head.removeChild(sandbox);
} catch (err) {
Expand Down
Loading