supports.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const debugBuild = require('./debug-build.js');
  3. const logger = require('./logger.js');
  4. const worldwide = require('./worldwide.js');
  5. // eslint-disable-next-line deprecation/deprecation
  6. const WINDOW = worldwide.getGlobalObject();
  7. /**
  8. * Tells whether current environment supports ErrorEvent objects
  9. * {@link supportsErrorEvent}.
  10. *
  11. * @returns Answer to the given question.
  12. */
  13. function supportsErrorEvent() {
  14. try {
  15. new ErrorEvent('');
  16. return true;
  17. } catch (e) {
  18. return false;
  19. }
  20. }
  21. /**
  22. * Tells whether current environment supports DOMError objects
  23. * {@link supportsDOMError}.
  24. *
  25. * @returns Answer to the given question.
  26. */
  27. function supportsDOMError() {
  28. try {
  29. // Chrome: VM89:1 Uncaught TypeError: Failed to construct 'DOMError':
  30. // 1 argument required, but only 0 present.
  31. // @ts-expect-error It really needs 1 argument, not 0.
  32. new DOMError('');
  33. return true;
  34. } catch (e) {
  35. return false;
  36. }
  37. }
  38. /**
  39. * Tells whether current environment supports DOMException objects
  40. * {@link supportsDOMException}.
  41. *
  42. * @returns Answer to the given question.
  43. */
  44. function supportsDOMException() {
  45. try {
  46. new DOMException('');
  47. return true;
  48. } catch (e) {
  49. return false;
  50. }
  51. }
  52. /**
  53. * Tells whether current environment supports Fetch API
  54. * {@link supportsFetch}.
  55. *
  56. * @returns Answer to the given question.
  57. */
  58. function supportsFetch() {
  59. if (!('fetch' in WINDOW)) {
  60. return false;
  61. }
  62. try {
  63. new Headers();
  64. new Request('http://www.example.com');
  65. new Response();
  66. return true;
  67. } catch (e) {
  68. return false;
  69. }
  70. }
  71. /**
  72. * isNativeFetch checks if the given function is a native implementation of fetch()
  73. */
  74. // eslint-disable-next-line @typescript-eslint/ban-types
  75. function isNativeFetch(func) {
  76. return func && /^function fetch\(\)\s+\{\s+\[native code\]\s+\}$/.test(func.toString());
  77. }
  78. /**
  79. * Tells whether current environment supports Fetch API natively
  80. * {@link supportsNativeFetch}.
  81. *
  82. * @returns true if `window.fetch` is natively implemented, false otherwise
  83. */
  84. function supportsNativeFetch() {
  85. if (typeof EdgeRuntime === 'string') {
  86. return true;
  87. }
  88. if (!supportsFetch()) {
  89. return false;
  90. }
  91. // Fast path to avoid DOM I/O
  92. // eslint-disable-next-line @typescript-eslint/unbound-method
  93. if (isNativeFetch(WINDOW.fetch)) {
  94. return true;
  95. }
  96. // window.fetch is implemented, but is polyfilled or already wrapped (e.g: by a chrome extension)
  97. // so create a "pure" iframe to see if that has native fetch
  98. let result = false;
  99. const doc = WINDOW.document;
  100. // eslint-disable-next-line deprecation/deprecation
  101. if (doc && typeof (doc.createElement ) === 'function') {
  102. try {
  103. const sandbox = doc.createElement('iframe');
  104. sandbox.hidden = true;
  105. doc.head.appendChild(sandbox);
  106. if (sandbox.contentWindow && sandbox.contentWindow.fetch) {
  107. // eslint-disable-next-line @typescript-eslint/unbound-method
  108. result = isNativeFetch(sandbox.contentWindow.fetch);
  109. }
  110. doc.head.removeChild(sandbox);
  111. } catch (err) {
  112. debugBuild.DEBUG_BUILD &&
  113. logger.logger.warn('Could not create sandbox iframe for pure fetch check, bailing to window.fetch: ', err);
  114. }
  115. }
  116. return result;
  117. }
  118. /**
  119. * Tells whether current environment supports ReportingObserver API
  120. * {@link supportsReportingObserver}.
  121. *
  122. * @returns Answer to the given question.
  123. */
  124. function supportsReportingObserver() {
  125. return 'ReportingObserver' in WINDOW;
  126. }
  127. /**
  128. * Tells whether current environment supports Referrer Policy API
  129. * {@link supportsReferrerPolicy}.
  130. *
  131. * @returns Answer to the given question.
  132. */
  133. function supportsReferrerPolicy() {
  134. // Despite all stars in the sky saying that Edge supports old draft syntax, aka 'never', 'always', 'origin' and 'default'
  135. // (see https://caniuse.com/#feat=referrer-policy),
  136. // it doesn't. And it throws an exception instead of ignoring this parameter...
  137. // REF: https://github.com/getsentry/raven-js/issues/1233
  138. if (!supportsFetch()) {
  139. return false;
  140. }
  141. try {
  142. new Request('_', {
  143. referrerPolicy: 'origin' ,
  144. });
  145. return true;
  146. } catch (e) {
  147. return false;
  148. }
  149. }
  150. exports.isNativeFetch = isNativeFetch;
  151. exports.supportsDOMError = supportsDOMError;
  152. exports.supportsDOMException = supportsDOMException;
  153. exports.supportsErrorEvent = supportsErrorEvent;
  154. exports.supportsFetch = supportsFetch;
  155. exports.supportsNativeFetch = supportsNativeFetch;
  156. exports.supportsReferrerPolicy = supportsReferrerPolicy;
  157. exports.supportsReportingObserver = supportsReportingObserver;
  158. //# sourceMappingURL=supports.js.map