blob: 6f19c324d49c43fc79ea22804ad9ba621814a38d [file] [log] [blame]
import { hasLocalStorage } from 'pouchdb-utils';
function canOpenTestDB() {
try {
openDatabase('_pouch_validate_websql', 1, '', 1);
return true;
} catch (err) {
return false;
}
}
// WKWebView had a bug where WebSQL would throw a DOM Exception 18
// (see https://bugs.webkit.org/show_bug.cgi?id=137760 and
// https://github.com/pouchdb/pouchdb/issues/5079)
// This has been fixed in latest WebKit, so we try to detect it here.
function isValidWebSQL() {
// WKWebView UA:
// Mozilla/5.0 (iPhone; CPU iPhone OS 9_2 like Mac OS X)
// AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13C75
// Chrome for iOS UA:
// Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_1_1 like Mac OS X; en)
// AppleWebKit/534.46.0 (KHTML, like Gecko) CriOS/19.0.1084.60
// Mobile/9B206 Safari/7534.48.3
// Firefox for iOS UA:
// Mozilla/5.0 (iPhone; CPU iPhone OS 8_3 like Mac OS X) AppleWebKit/600.1.4
// (KHTML, like Gecko) FxiOS/1.0 Mobile/12F69 Safari/600.1.4
// indexedDB is null on some UIWebViews and undefined in others
// see: https://bugs.webkit.org/show_bug.cgi?id=137034
if (typeof indexedDB === 'undefined' || indexedDB === null ||
!/iP(hone|od|ad)/.test(navigator.userAgent)) {
// definitely not WKWebView, avoid creating an unnecessary database
return true;
}
// Cache the result in LocalStorage. Reason we do this is because if we
// call openDatabase() too many times, Safari craps out in SauceLabs and
// starts throwing DOM Exception 14s.
var hasLS = hasLocalStorage();
// Include user agent in the hash, so that if Safari is upgraded, we don't
// continually think it's broken.
var localStorageKey = '_pouch__websqldb_valid_' + navigator.userAgent;
if (hasLS && localStorage[localStorageKey]) {
return localStorage[localStorageKey] === '1';
}
var openedTestDB = canOpenTestDB();
if (hasLS) {
localStorage[localStorageKey] = openedTestDB ? '1' : '0';
}
return openedTestDB;
}
function valid() {
if (typeof openDatabase !== 'function') {
return false;
}
return isValidWebSQL();
}
export default valid;