@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
100 lines (99 loc) • 2.77 kB
JavaScript
import { DEBUG_BUILD } from "../debug-build.js";
import { debug } from "./debug-logger.js";
import { GLOBAL_OBJ } from "./worldwide.js";
// src/utils/supports.ts
const WINDOW = GLOBAL_OBJ;
/**
* Tells whether current environment supports ErrorEvent objects
* {@link supportsErrorEvent}.
*
* @returns Answer to the given question.
*/
function supportsErrorEvent() {
try {
new ErrorEvent("");
return true;
} catch {
return false;
}
}
/**
* Tells whether current environment supports DOMException objects
* {@link supportsDOMException}.
*
* @returns Answer to the given question.
*/
function supportsDOMException() {
try {
new DOMException("");
return true;
} catch {
return false;
}
}
/**
* Tells whether current environment supports Fetch API
* {@link supportsFetch}.
*
* @returns Answer to the given question.
* @deprecated This is no longer used and will be removed in a future major version.
*/
const supportsFetch = _isFetchSupported;
function _isFetchSupported() {
if (!("fetch" in WINDOW)) return false;
try {
new Headers();
new Request("data:,");
new Response();
return true;
} catch {
return false;
}
}
/**
* isNative checks if the given function is a native implementation
*/
function isNativeFunction(func) {
return func && /^function\s+\w+\(\)\s+\{\s+\[native code\]\s+\}$/.test(func.toString());
}
/**
* Tells whether current environment supports Fetch API natively
* {@link supportsNativeFetch}.
*
* @returns true if `window.fetch` is natively implemented, false otherwise
*/
function supportsNativeFetch() {
if (typeof EdgeRuntime === "string") return true;
if (!_isFetchSupported()) return false;
if (isNativeFunction(WINDOW.fetch)) return true;
let result = false;
const doc = WINDOW.document;
if (doc && typeof doc.createElement === "function") try {
const sandbox = doc.createElement("iframe");
sandbox.hidden = true;
doc.head.appendChild(sandbox);
if (sandbox.contentWindow?.fetch) result = isNativeFunction(sandbox.contentWindow.fetch);
doc.head.removeChild(sandbox);
} catch (err) {
DEBUG_BUILD && debug.warn("Could not create sandbox iframe for pure fetch check, bailing to window.fetch: ", err);
}
return result;
}
/**
* Tells whether current environment supports Referrer Policy API
* {@link supportsReferrerPolicy}.
*
* @returns Answer to the given question.
* @deprecated This is no longer used and will be removed in a future major version.
*/
function supportsReferrerPolicy() {
if (!_isFetchSupported()) return false;
try {
new Request("_", { referrerPolicy: "origin" });
return true;
} catch {
return false;
}
}
export { isNativeFunction, supportsDOMException, supportsErrorEvent, supportsFetch, supportsNativeFetch, supportsReferrerPolicy };
//# sourceMappingURL=supports.js.map