123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- Object.defineProperty(exports, '__esModule', { value: true });
- const debugBuild = require('./debug-build.js');
- const logger = require('./logger.js');
- const worldwide = require('./worldwide.js');
- // eslint-disable-next-line deprecation/deprecation
- const WINDOW = worldwide.getGlobalObject();
- /**
- * Tells whether current environment supports ErrorEvent objects
- * {@link supportsErrorEvent}.
- *
- * @returns Answer to the given question.
- */
- function supportsErrorEvent() {
- try {
- new ErrorEvent('');
- return true;
- } catch (e) {
- return false;
- }
- }
- /**
- * Tells whether current environment supports DOMError objects
- * {@link supportsDOMError}.
- *
- * @returns Answer to the given question.
- */
- function supportsDOMError() {
- try {
- // Chrome: VM89:1 Uncaught TypeError: Failed to construct 'DOMError':
- // 1 argument required, but only 0 present.
- // @ts-expect-error It really needs 1 argument, not 0.
- new DOMError('');
- return true;
- } catch (e) {
- 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 (e) {
- return false;
- }
- }
- /**
- * Tells whether current environment supports Fetch API
- * {@link supportsFetch}.
- *
- * @returns Answer to the given question.
- */
- function supportsFetch() {
- if (!('fetch' in WINDOW)) {
- return false;
- }
- try {
- new Headers();
- new Request('http://www.example.com');
- new Response();
- return true;
- } catch (e) {
- return false;
- }
- }
- /**
- * isNativeFetch checks if the given function is a native implementation of fetch()
- */
- // eslint-disable-next-line @typescript-eslint/ban-types
- function isNativeFetch(func) {
- return func && /^function fetch\(\)\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 (!supportsFetch()) {
- return false;
- }
- // Fast path to avoid DOM I/O
- // eslint-disable-next-line @typescript-eslint/unbound-method
- if (isNativeFetch(WINDOW.fetch)) {
- return true;
- }
- // window.fetch is implemented, but is polyfilled or already wrapped (e.g: by a chrome extension)
- // so create a "pure" iframe to see if that has native fetch
- let result = false;
- const doc = WINDOW.document;
- // eslint-disable-next-line deprecation/deprecation
- if (doc && typeof (doc.createElement ) === 'function') {
- try {
- const sandbox = doc.createElement('iframe');
- sandbox.hidden = true;
- doc.head.appendChild(sandbox);
- if (sandbox.contentWindow && sandbox.contentWindow.fetch) {
- // eslint-disable-next-line @typescript-eslint/unbound-method
- result = isNativeFetch(sandbox.contentWindow.fetch);
- }
- doc.head.removeChild(sandbox);
- } catch (err) {
- debugBuild.DEBUG_BUILD &&
- logger.logger.warn('Could not create sandbox iframe for pure fetch check, bailing to window.fetch: ', err);
- }
- }
- return result;
- }
- /**
- * Tells whether current environment supports ReportingObserver API
- * {@link supportsReportingObserver}.
- *
- * @returns Answer to the given question.
- */
- function supportsReportingObserver() {
- return 'ReportingObserver' in WINDOW;
- }
- /**
- * Tells whether current environment supports Referrer Policy API
- * {@link supportsReferrerPolicy}.
- *
- * @returns Answer to the given question.
- */
- function supportsReferrerPolicy() {
- // Despite all stars in the sky saying that Edge supports old draft syntax, aka 'never', 'always', 'origin' and 'default'
- // (see https://caniuse.com/#feat=referrer-policy),
- // it doesn't. And it throws an exception instead of ignoring this parameter...
- // REF: https://github.com/getsentry/raven-js/issues/1233
- if (!supportsFetch()) {
- return false;
- }
- try {
- new Request('_', {
- referrerPolicy: 'origin' ,
- });
- return true;
- } catch (e) {
- return false;
- }
- }
- exports.isNativeFetch = isNativeFetch;
- exports.supportsDOMError = supportsDOMError;
- exports.supportsDOMException = supportsDOMException;
- exports.supportsErrorEvent = supportsErrorEvent;
- exports.supportsFetch = supportsFetch;
- exports.supportsNativeFetch = supportsNativeFetch;
- exports.supportsReferrerPolicy = supportsReferrerPolicy;
- exports.supportsReportingObserver = supportsReportingObserver;
- //# sourceMappingURL=supports.js.map
|