UNPKG

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