diff options
Diffstat (limited to 'www/lib/localforage/src')
| -rw-r--r-- | www/lib/localforage/src/drivers/indexeddb.js | 17 | ||||
| -rw-r--r-- | www/lib/localforage/src/drivers/websql.js | 12 | ||||
| -rw-r--r-- | www/lib/localforage/src/localforage.js | 24 | ||||
| -rw-r--r-- | www/lib/localforage/src/utils/idb.js | 32 | ||||
| -rw-r--r-- | www/lib/localforage/src/utils/isIndexedDBValid.js | 33 | ||||
| -rw-r--r-- | www/lib/localforage/src/utils/promise.js | 4 | ||||
| -rw-r--r-- | www/lib/localforage/src/utils/serializer.js | 30 |
7 files changed, 86 insertions, 66 deletions
diff --git a/www/lib/localforage/src/drivers/indexeddb.js b/www/lib/localforage/src/drivers/indexeddb.js index 2dd52554..51f936dc 100644 --- a/www/lib/localforage/src/drivers/indexeddb.js +++ b/www/lib/localforage/src/drivers/indexeddb.js @@ -2,6 +2,7 @@ import createBlob from '../utils/createBlob'; import idb from '../utils/idb'; import Promise from '../utils/promise'; import executeCallback from '../utils/executeCallback'; +import executeTwoCallbacks from '../utils/executeTwoCallbacks'; // Some code originally from async_storage.js in // [Gaia](https://github.com/mozilla-b2g/gaia). @@ -9,6 +10,7 @@ import executeCallback from '../utils/executeCallback'; var DETECT_BLOB_SUPPORT_STORE = 'local-forage-detect-blob-support'; var supportsBlobs; var dbContexts; +var toString = Object.prototype.toString; // Transform a binary string to an array buffer, because otherwise // weird stuff happens when you try to work with the binary string directly. @@ -38,10 +40,11 @@ function _binStringToArrayBuffer(bin) { // FileReader bug: https://code.google.com/p/chromium/issues/detail?id=447836 // // Code borrowed from PouchDB. See: -// https://github.com/pouchdb/pouchdb/blob/9c25a23/src/adapters/idb/blobSupport.js +// https://github.com/pouchdb/pouchdb/blob/master/packages/node_modules/pouchdb-adapter-idb/src/blobSupport.js // -function _checkBlobSupportWithoutCaching(txn) { +function _checkBlobSupportWithoutCaching(idb) { return new Promise(function(resolve) { + var txn = idb.transaction(DETECT_BLOB_SUPPORT_STORE, 'readwrite'); var blob = createBlob(['']); txn.objectStore(DETECT_BLOB_SUPPORT_STORE).put(blob, 'key'); @@ -154,7 +157,8 @@ function _getConnection(dbInfo, upgradeNeeded) { }; } - openreq.onerror = function() { + openreq.onerror = function(e) { + e.preventDefault(); reject(openreq.error); }; @@ -254,7 +258,7 @@ function _fullyReady(callback) { } }); - promise.then(callback, callback); + executeTwoCallbacks(promise, callback, callback); return promise; } @@ -448,7 +452,7 @@ function setItem(key, value, callback) { var dbInfo; self.ready().then(function() { dbInfo = self._dbInfo; - if (value instanceof Blob) { + if (toString.call(value) === '[object Blob]') { return _checkBlobSupport(dbInfo.db).then(function(blobSupport) { if (blobSupport) { return value; @@ -460,6 +464,7 @@ function setItem(key, value, callback) { }).then(function(value) { var transaction = dbInfo.db.transaction(dbInfo.storeName, 'readwrite'); var store = transaction.objectStore(dbInfo.storeName); + var req = store.put(value, key); // The reason we don't _save_ null is because IE 10 does // not support saving the `null` type in IndexedDB. How @@ -486,8 +491,6 @@ function setItem(key, value, callback) { var err = req.error ? req.error : req.transaction.error; reject(err); }; - - var req = store.put(value, key); }).catch(reject); }); diff --git a/www/lib/localforage/src/drivers/websql.js b/www/lib/localforage/src/drivers/websql.js index 04e6df2d..e99398b8 100644 --- a/www/lib/localforage/src/drivers/websql.js +++ b/www/lib/localforage/src/drivers/websql.js @@ -137,7 +137,7 @@ function iterate(iterator, callback) { return promise; } -function setItem(key, value, callback) { +function _setItem(key, value, callback, retriesLeft) { var self = this; // Cast the key to a string, as that's all we can set as a key. @@ -183,7 +183,11 @@ function setItem(key, value, callback) { // more storage on Safari, this error will // be called. // - // TODO: Try to re-run the transaction. + // Try to re-run the transaction. + if (retriesLeft > 0) { + resolve(_setItem.apply(self, [key, originalValue, callback, retriesLeft - 1])); + return; + } reject(sqlError); } }); @@ -196,6 +200,10 @@ function setItem(key, value, callback) { return promise; } +function setItem(key, value, callback) { + return _setItem.apply(this, [key, value, callback, 1]); +} + function removeItem(key, callback) { var self = this; diff --git a/www/lib/localforage/src/localforage.js b/www/lib/localforage/src/localforage.js index 837f3c7a..36546ee6 100644 --- a/www/lib/localforage/src/localforage.js +++ b/www/lib/localforage/src/localforage.js @@ -116,7 +116,7 @@ class LocalForage { this._dbInfo = null; this._wrapLibraryMethodsWithReady(); - this.setDriver(this._config.driver); + this.setDriver(this._config.driver).catch(() => {}); } // Set any config values for localForage; can be called anytime before @@ -140,13 +140,17 @@ class LocalForage { options[i] = options[i].replace(/\W/g, '_'); } + if (i === 'version' && typeof options[i] !== 'number') { + return new Error('Database version must be a number.'); + } + this._config[i] = options[i]; } // after all config options are set and // the driver option is used, try setting it if ('driver' in options && options.driver) { - this.setDriver(this._config.driver); + return this.setDriver(this._config.driver); } return true; @@ -275,6 +279,14 @@ class LocalForage { self._config.driver = self.driver(); } + function extendSelfWithDriver(driver) { + self._extend(driver); + setDriverToConfig(); + + self._ready = self._initStorage(self._config); + return self._ready; + } + function initDriver(supportedDrivers) { return function() { var currentDriverIndex = 0; @@ -288,13 +300,7 @@ class LocalForage { self._ready = null; return self.getDriver(driverName) - .then(driver => { - self._extend(driver); - setDriverToConfig(); - - self._ready = self._initStorage(self._config); - return self._ready; - }) + .then(extendSelfWithDriver) .catch(driverPromiseLoop); } diff --git a/www/lib/localforage/src/utils/idb.js b/www/lib/localforage/src/utils/idb.js index 47541f6a..f25f2dcd 100644 --- a/www/lib/localforage/src/utils/idb.js +++ b/www/lib/localforage/src/utils/idb.js @@ -1,20 +1,22 @@ function getIDB() { /* global indexedDB,webkitIndexedDB,mozIndexedDB,OIndexedDB,msIndexedDB */ - if (typeof indexedDB !== 'undefined') { - return indexedDB; - } - if (typeof webkitIndexedDB !== 'undefined') { - return webkitIndexedDB; - } - if (typeof mozIndexedDB !== 'undefined') { - return mozIndexedDB; - } - if (typeof OIndexedDB !== 'undefined') { - return OIndexedDB; - } - if (typeof msIndexedDB !== 'undefined') { - return msIndexedDB; - } + try { + if (typeof indexedDB !== 'undefined') { + return indexedDB; + } + if (typeof webkitIndexedDB !== 'undefined') { + return webkitIndexedDB; + } + if (typeof mozIndexedDB !== 'undefined') { + return mozIndexedDB; + } + if (typeof OIndexedDB !== 'undefined') { + return OIndexedDB; + } + if (typeof msIndexedDB !== 'undefined') { + return msIndexedDB; + } + } catch (e) { } } var idb = getIDB(); diff --git a/www/lib/localforage/src/utils/isIndexedDBValid.js b/www/lib/localforage/src/utils/isIndexedDBValid.js index aa14b6d8..f8150875 100644 --- a/www/lib/localforage/src/utils/isIndexedDBValid.js +++ b/www/lib/localforage/src/utils/isIndexedDBValid.js @@ -7,28 +7,25 @@ function isIndexedDBValid() { if (!idb) { return false; } - // We mimic PouchDB here; just UA test for Safari (which, as of - // iOS 8/Yosemite, doesn't properly support IndexedDB). - // IndexedDB support is broken and different from Blink's. - // This is faster than the test case (and it's sync), so we just - // do this. *SIGH* - // http://bl.ocks.org/nolanlawson/raw/c83e9039edf2278047e9/ + // We mimic PouchDB here; // // We test for openDatabase because IE Mobile identifies itself // as Safari. Oh the lulz... - if (typeof openDatabase !== 'undefined' && typeof navigator !== 'undefined' && - navigator.userAgent && - /Safari/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgent)) { - return false; - } + var isSafari = typeof openDatabase !== 'undefined' && + /(Safari|iPhone|iPad|iPod)/.test(navigator.userAgent) && + !/Chrome/.test(navigator.userAgent) && + !/BlackBerry/.test(navigator.platform); + + var hasFetch = typeof fetch === 'function' && + fetch.toString().indexOf('[native code') !== -1; - return idb && - typeof idb.open === 'function' && - // Some Samsung/HTC Android 4.0-4.3 devices - // have older IndexedDB specs; if this isn't available - // their IndexedDB is too old for us to use. - // (Replaces the onupgradeneeded test.) - typeof IDBKeyRange !== 'undefined'; + // Safari <10.1 does not meet our requirements for IDB support (#5572) + // since Safari 10.1 shipped with fetch, we can use that to detect it + return (!isSafari || hasFetch) && + typeof indexedDB !== 'undefined' && + // some outdated implementations of IDB that appear on Samsung + // and HTC Android devices <4.4 are missing IDBKeyRange + typeof IDBKeyRange !== 'undefined'; } catch (e) { return false; } diff --git a/www/lib/localforage/src/utils/promise.js b/www/lib/localforage/src/utils/promise.js index 177a10d0..fe908a82 100644 --- a/www/lib/localforage/src/utils/promise.js +++ b/www/lib/localforage/src/utils/promise.js @@ -1,6 +1,8 @@ // This is CommonJS because lie is an external dependency, so Rollup // can just ignore it. -if (typeof Promise === 'undefined' && typeof require !== 'undefined') { +if (typeof Promise === 'undefined') { + // In the "nopromises" build this will just throw if you don't have + // a global promise object, but it would throw anyway later. require('lie/polyfill'); } export default Promise; diff --git a/www/lib/localforage/src/utils/serializer.js b/www/lib/localforage/src/utils/serializer.js index 08e5fc8f..ddeed301 100644 --- a/www/lib/localforage/src/utils/serializer.js +++ b/www/lib/localforage/src/utils/serializer.js @@ -26,6 +26,8 @@ var TYPE_FLOAT64ARRAY = 'fl64'; var TYPE_SERIALIZED_MARKER_LENGTH = SERIALIZED_MARKER_LENGTH + TYPE_ARRAYBUFFER.length; +var toString = Object.prototype.toString; + function stringToBuffer(serializedString) { // Fill the string into a ArrayBuffer. var bufferLength = serializedString.length * 0.75; @@ -87,18 +89,18 @@ function bufferToString(buffer) { // instructs the `setItem()` callback/promise to be executed). This is how // we store binary data with localStorage. function serialize(value, callback) { - var valueString = ''; + var valueType = ''; if (value) { - valueString = value.toString(); + valueType = toString.call(value); } // Cannot use `value instanceof ArrayBuffer` or such here, as these // checks fail when running the tests using casper.js... // // TODO: See why those tests fail and use a better solution. - if (value && (value.toString() === '[object ArrayBuffer]' || + if (value && (valueType === '[object ArrayBuffer]' || value.buffer && - value.buffer.toString() === '[object ArrayBuffer]')) { + toString.call(value.buffer) === '[object ArrayBuffer]')) { // Convert binary arrays to a string and prefix the string with // a special marker. var buffer; @@ -110,23 +112,23 @@ function serialize(value, callback) { } else { buffer = value.buffer; - if (valueString === '[object Int8Array]') { + if (valueType === '[object Int8Array]') { marker += TYPE_INT8ARRAY; - } else if (valueString === '[object Uint8Array]') { + } else if (valueType === '[object Uint8Array]') { marker += TYPE_UINT8ARRAY; - } else if (valueString === '[object Uint8ClampedArray]') { + } else if (valueType === '[object Uint8ClampedArray]') { marker += TYPE_UINT8CLAMPEDARRAY; - } else if (valueString === '[object Int16Array]') { + } else if (valueType === '[object Int16Array]') { marker += TYPE_INT16ARRAY; - } else if (valueString === '[object Uint16Array]') { + } else if (valueType === '[object Uint16Array]') { marker += TYPE_UINT16ARRAY; - } else if (valueString === '[object Int32Array]') { + } else if (valueType === '[object Int32Array]') { marker += TYPE_INT32ARRAY; - } else if (valueString === '[object Uint32Array]') { + } else if (valueType === '[object Uint32Array]') { marker += TYPE_UINT32ARRAY; - } else if (valueString === '[object Float32Array]') { + } else if (valueType === '[object Float32Array]') { marker += TYPE_FLOAT32ARRAY; - } else if (valueString === '[object Float64Array]') { + } else if (valueType === '[object Float64Array]') { marker += TYPE_FLOAT64ARRAY; } else { callback(new Error('Failed to get type for BinaryArray')); @@ -134,7 +136,7 @@ function serialize(value, callback) { } callback(marker + bufferToString(buffer)); - } else if (valueString === '[object Blob]') { + } else if (valueType === '[object Blob]') { // Conver the blob to a binaryArray and then to a string. var fileReader = new FileReader(); |
