UNPKG

@platformos/pos-cli

Version:

Manage your platformOS application

182 lines 5.71 kB
Object.defineProperty(exports, "__esModule", { value: true }); var logger_1 = require("./logger"); var misc_1 = require("./misc"); /** * Tells whether current environment supports ErrorEvent objects * {@link supportsErrorEvent}. * * @returns Answer to the given question. */ function supportsErrorEvent() { try { // tslint:disable:no-unused-expression new ErrorEvent(''); return true; } catch (e) { return false; } } exports.supportsErrorEvent = supportsErrorEvent; /** * Tells whether current environment supports DOMError objects * {@link supportsDOMError}. * * @returns Answer to the given question. */ function supportsDOMError() { try { // It really needs 1 argument, not 0. // Chrome: VM89:1 Uncaught TypeError: Failed to construct 'DOMError': // 1 argument required, but only 0 present. // @ts-ignore // tslint:disable:no-unused-expression new DOMError(''); return true; } catch (e) { return false; } } exports.supportsDOMError = supportsDOMError; /** * Tells whether current environment supports DOMException objects * {@link supportsDOMException}. * * @returns Answer to the given question. */ function supportsDOMException() { try { // tslint:disable:no-unused-expression new DOMException(''); return true; } catch (e) { return false; } } exports.supportsDOMException = supportsDOMException; /** * Tells whether current environment supports Fetch API * {@link supportsFetch}. * * @returns Answer to the given question. */ function supportsFetch() { if (!('fetch' in misc_1.getGlobalObject())) { return false; } try { // tslint:disable-next-line:no-unused-expression new Headers(); // tslint:disable-next-line:no-unused-expression new Request(''); // tslint:disable-next-line:no-unused-expression new Response(); return true; } catch (e) { return false; } } exports.supportsFetch = supportsFetch; /** * isNativeFetch checks if the given function is a native implementation of fetch() */ 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 (!supportsFetch()) { return false; } var global = misc_1.getGlobalObject(); // Fast path to avoid DOM I/O // tslint:disable-next-line:no-unbound-method if (isNativeFetch(global.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 var result = false; var doc = global.document; if (doc) { try { var sandbox = doc.createElement('iframe'); sandbox.hidden = true; doc.head.appendChild(sandbox); if (sandbox.contentWindow && sandbox.contentWindow.fetch) { // tslint:disable-next-line:no-unbound-method result = isNativeFetch(sandbox.contentWindow.fetch); } doc.head.removeChild(sandbox); } catch (err) { logger_1.logger.warn('Could not create sandbox iframe for pure fetch check, bailing to window.fetch: ', err); } } return result; } exports.supportsNativeFetch = supportsNativeFetch; /** * Tells whether current environment supports ReportingObserver API * {@link supportsReportingObserver}. * * @returns Answer to the given question. */ function supportsReportingObserver() { // tslint:disable-next-line: no-unsafe-any return 'ReportingObserver' in misc_1.getGlobalObject(); } exports.supportsReportingObserver = supportsReportingObserver; /** * 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 // https://caniuse.com/#feat=referrer-policy // It doesn't. And it throw exception instead of ignoring this parameter... // REF: https://github.com/getsentry/raven-js/issues/1233 if (!supportsFetch()) { return false; } try { // tslint:disable:no-unused-expression new Request('_', { referrerPolicy: 'origin', }); return true; } catch (e) { return false; } } exports.supportsReferrerPolicy = supportsReferrerPolicy; /** * Tells whether current environment supports History API * {@link supportsHistory}. * * @returns Answer to the given question. */ function supportsHistory() { // NOTE: in Chrome App environment, touching history.pushState, *even inside // a try/catch block*, will cause Chrome to output an error to console.error // borrowed from: https://github.com/angular/angular.js/pull/13945/files var global = misc_1.getGlobalObject(); var chrome = global.chrome; // tslint:disable-next-line:no-unsafe-any var isChromePackagedApp = chrome && chrome.app && chrome.app.runtime; var hasHistoryApi = 'history' in global && !!global.history.pushState && !!global.history.replaceState; return !isChromePackagedApp && hasHistoryApi; } exports.supportsHistory = supportsHistory; //# sourceMappingURL=supports.js.map