localforage
Advanced tools
Comparing version 1.4.0 to 1.4.1
@@ -28,3 +28,3 @@ { | ||
}, | ||
"version": "1.4.0" | ||
"version": "1.4.1" | ||
} |
{ | ||
"name": "localforage", | ||
"version": "1.4.0", | ||
"version": "1.4.1", | ||
"dependencies": { | ||
@@ -5,0 +5,0 @@ "then/promise": "5.0.0" |
/*! | ||
localForage -- Offline Storage, Improved | ||
Version 1.4.0 | ||
Version 1.4.1 | ||
https://mozilla.github.io/localForage | ||
(c) 2013-2015 Mozilla, Apache License 2.0 | ||
*/ | ||
!function(){var a,b,c,d;!function(){var e={},f={};a=function(a,b,c){e[a]={deps:b,callback:c}},d=c=b=function(a){function c(b){if("."!==b.charAt(0))return b;for(var c=b.split("/"),d=a.split("/").slice(0,-1),e=0,f=c.length;f>e;e++){var g=c[e];if(".."===g)d.pop();else{if("."===g)continue;d.push(g)}}return d.join("/")}if(d._eak_seen=e,f[a])return f[a];if(f[a]={},!e[a])throw new Error("Could not find module "+a);for(var g,h=e[a],i=h.deps,j=h.callback,k=[],l=0,m=i.length;m>l;l++)"exports"===i[l]?k.push(g={}):k.push(b(c(i[l])));var n=j.apply(this,k);return f[a]=g||n}}(),a("promise/all",["./utils","exports"],function(a,b){"use strict";function c(a){var b=this;if(!d(a))throw new TypeError("You must pass an array to all.");return new b(function(b,c){function d(a){return function(b){f(a,b)}}function f(a,c){h[a]=c,0===--i&&b(h)}var g,h=[],i=a.length;0===i&&b([]);for(var j=0;j<a.length;j++)g=a[j],g&&e(g.then)?g.then(d(j),c):f(j,g)})}var d=a.isArray,e=a.isFunction;b.all=c}),a("promise/asap",["exports"],function(a){"use strict";function b(){return function(){process.nextTick(e)}}function c(){var a=0,b=new i(e),c=document.createTextNode("");return b.observe(c,{characterData:!0}),function(){c.data=a=++a%2}}function d(){return function(){j.setTimeout(e,1)}}function e(){for(var a=0;a<k.length;a++){var b=k[a],c=b[0],d=b[1];c(d)}k=[]}function f(a,b){var c=k.push([a,b]);1===c&&g()}var g,h="undefined"!=typeof window?window:{},i=h.MutationObserver||h.WebKitMutationObserver,j="undefined"!=typeof global?global:void 0===this?window:this,k=[];g="undefined"!=typeof process&&"[object process]"==={}.toString.call(process)?b():i?c():d(),a.asap=f}),a("promise/config",["exports"],function(a){"use strict";function b(a,b){return 2!==arguments.length?c[a]:void(c[a]=b)}var c={instrument:!1};a.config=c,a.configure=b}),a("promise/polyfill",["./promise","./utils","exports"],function(a,b,c){"use strict";function d(){var a;a="undefined"!=typeof global?global:"undefined"!=typeof window&&window.document?window:self;var b="Promise"in a&&"resolve"in a.Promise&&"reject"in a.Promise&&"all"in a.Promise&&"race"in a.Promise&&function(){var b;return new a.Promise(function(a){b=a}),f(b)}();b||(a.Promise=e)}var e=a.Promise,f=b.isFunction;c.polyfill=d}),a("promise/promise",["./config","./utils","./all","./race","./resolve","./reject","./asap","exports"],function(a,b,c,d,e,f,g,h){"use strict";function i(a){if(!v(a))throw new TypeError("You must pass a resolver function as the first argument to the promise constructor");if(!(this instanceof i))throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function.");this._subscribers=[],j(a,this)}function j(a,b){function c(a){o(b,a)}function d(a){q(b,a)}try{a(c,d)}catch(e){d(e)}}function k(a,b,c,d){var e,f,g,h,i=v(c);if(i)try{e=c(d),g=!0}catch(j){h=!0,f=j}else e=d,g=!0;n(b,e)||(i&&g?o(b,e):h?q(b,f):a===D?o(b,e):a===E&&q(b,e))}function l(a,b,c,d){var e=a._subscribers,f=e.length;e[f]=b,e[f+D]=c,e[f+E]=d}function m(a,b){for(var c,d,e=a._subscribers,f=a._detail,g=0;g<e.length;g+=3)c=e[g],d=e[g+b],k(b,c,d,f);a._subscribers=null}function n(a,b){var c,d=null;try{if(a===b)throw new TypeError("A promises callback cannot return that same promise.");if(u(b)&&(d=b.then,v(d)))return d.call(b,function(d){return c?!0:(c=!0,void(b!==d?o(a,d):p(a,d)))},function(b){return c?!0:(c=!0,void q(a,b))}),!0}catch(e){return c?!0:(q(a,e),!0)}return!1}function o(a,b){a===b?p(a,b):n(a,b)||p(a,b)}function p(a,b){a._state===B&&(a._state=C,a._detail=b,t.async(r,a))}function q(a,b){a._state===B&&(a._state=C,a._detail=b,t.async(s,a))}function r(a){m(a,a._state=D)}function s(a){m(a,a._state=E)}var t=a.config,u=(a.configure,b.objectOrFunction),v=b.isFunction,w=(b.now,c.all),x=d.race,y=e.resolve,z=f.reject,A=g.asap;t.async=A;var B=void 0,C=0,D=1,E=2;i.prototype={constructor:i,_state:void 0,_detail:void 0,_subscribers:void 0,then:function(a,b){var c=this,d=new this.constructor(function(){});if(this._state){var e=arguments;t.async(function(){k(c._state,d,e[c._state-1],c._detail)})}else l(this,d,a,b);return d},"catch":function(a){return this.then(null,a)}},i.all=w,i.race=x,i.resolve=y,i.reject=z,h.Promise=i}),a("promise/race",["./utils","exports"],function(a,b){"use strict";function c(a){var b=this;if(!d(a))throw new TypeError("You must pass an array to race.");return new b(function(b,c){for(var d,e=0;e<a.length;e++)d=a[e],d&&"function"==typeof d.then?d.then(b,c):b(d)})}var d=a.isArray;b.race=c}),a("promise/reject",["exports"],function(a){"use strict";function b(a){var b=this;return new b(function(b,c){c(a)})}a.reject=b}),a("promise/resolve",["exports"],function(a){"use strict";function b(a){if(a&&"object"==typeof a&&a.constructor===this)return a;var b=this;return new b(function(b){b(a)})}a.resolve=b}),a("promise/utils",["exports"],function(a){"use strict";function b(a){return c(a)||"object"==typeof a&&null!==a}function c(a){return"function"==typeof a}function d(a){return"[object Array]"===Object.prototype.toString.call(a)}var e=Date.now||function(){return(new Date).getTime()};a.objectOrFunction=b,a.isFunction=c,a.isArray=d,a.now=e}),b("promise/polyfill").polyfill()}(),function(a,b){"object"==typeof exports&&"object"==typeof module?module.exports=b():"function"==typeof define&&define.amd?define([],b):"object"==typeof exports?exports.localforage=b():a.localforage=b()}(this,function(){return function(a){function b(d){if(c[d])return c[d].exports;var e=c[d]={exports:{},id:d,loaded:!1};return a[d].call(e.exports,e,e.exports,b),e.loaded=!0,e.exports}var c={};return b.m=a,b.c=c,b.p="",b(0)}([function(a,b,c){"use strict";function d(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}b.__esModule=!0;var e=function(a){function b(a,b){a[b]=function(){var c=arguments;return a.ready().then(function(){return a[b].apply(a,c)})}}function e(){for(var a=1;a<arguments.length;a++){var b=arguments[a];if(b)for(var c in b)b.hasOwnProperty(c)&&(m(b[c])?arguments[0][c]=b[c].slice():arguments[0][c]=b[c])}return arguments[0]}function f(a){for(var b in h)if(h.hasOwnProperty(b)&&h[b]===a)return!0;return!1}var g={},h={INDEXEDDB:"asyncStorage",LOCALSTORAGE:"localStorageWrapper",WEBSQL:"webSQLStorage"},i=[h.INDEXEDDB,h.WEBSQL,h.LOCALSTORAGE],j=["clear","getItem","iterate","key","keys","length","removeItem","setItem"],k={description:"",driver:i.slice(),name:"localforage",size:4980736,storeName:"keyvaluepairs",version:1},l=function(a){var b={};return b[h.INDEXEDDB]=!!function(){try{var b=b||a.indexedDB||a.webkitIndexedDB||a.mozIndexedDB||a.OIndexedDB||a.msIndexedDB;return"undefined"!=typeof a.openDatabase&&a.navigator&&a.navigator.userAgent&&/Safari/.test(a.navigator.userAgent)&&!/Chrome/.test(a.navigator.userAgent)?!1:b&&"function"==typeof b.open&&"undefined"!=typeof a.IDBKeyRange}catch(c){return!1}}(),b[h.WEBSQL]=!!function(){try{return a.openDatabase}catch(b){return!1}}(),b[h.LOCALSTORAGE]=!!function(){try{return a.localStorage&&"setItem"in a.localStorage&&a.localStorage.setItem}catch(b){return!1}}(),b}(a),m=Array.isArray||function(a){return"[object Array]"===Object.prototype.toString.call(a)},n=function(){function a(b){d(this,a),this.INDEXEDDB=h.INDEXEDDB,this.LOCALSTORAGE=h.LOCALSTORAGE,this.WEBSQL=h.WEBSQL,this._defaultConfig=e({},k),this._config=e({},this._defaultConfig,b),this._driverSet=null,this._initDriver=null,this._ready=!1,this._dbInfo=null,this._wrapLibraryMethodsWithReady(),this.setDriver(this._config.driver)}return a.prototype.config=function(a){if("object"==typeof a){if(this._ready)return new Error("Can't call config() after localforage has been used.");for(var b in a)"storeName"===b&&(a[b]=a[b].replace(/\W/g,"_")),this._config[b]=a[b];return"driver"in a&&a.driver&&this.setDriver(this._config.driver),!0}return"string"==typeof a?this._config[a]:this._config},a.prototype.defineDriver=function(a,b,c){var d=new Promise(function(b,c){try{var d=a._driver,e=new Error("Custom driver not compliant; see https://mozilla.github.io/localForage/#definedriver"),h=new Error("Custom driver name already in use: "+a._driver);if(!a._driver)return void c(e);if(f(a._driver))return void c(h);for(var i=j.concat("_initStorage"),k=0;k<i.length;k++){var m=i[k];if(!m||!a[m]||"function"!=typeof a[m])return void c(e)}var n=Promise.resolve(!0);"_support"in a&&(n=a._support&&"function"==typeof a._support?a._support():Promise.resolve(!!a._support)),n.then(function(c){l[d]=c,g[d]=a,b()},c)}catch(o){c(o)}});return d.then(b,c),d},a.prototype.driver=function(){return this._driver||null},a.prototype.getDriver=function(a,b,d){var e=this,h=function(){if(f(a))switch(a){case e.INDEXEDDB:return new Promise(function(a,b){a(c(1))});case e.LOCALSTORAGE:return new Promise(function(a,b){a(c(2))});case e.WEBSQL:return new Promise(function(a,b){a(c(4))})}else if(g[a])return Promise.resolve(g[a]);return Promise.reject(new Error("Driver not found."))}();return h.then(b,d),h},a.prototype.getSerializer=function(a){var b=new Promise(function(a,b){a(c(3))});return a&&"function"==typeof a&&b.then(function(b){a(b)}),b},a.prototype.ready=function(a){var b=this,c=b._driverSet.then(function(){return null===b._ready&&(b._ready=b._initDriver()),b._ready});return c.then(a,a),c},a.prototype.setDriver=function(a,b,c){function d(){f._config.driver=f.driver()}function e(a){return function(){function b(){for(;c<a.length;){var e=a[c];return c++,f._dbInfo=null,f._ready=null,f.getDriver(e).then(function(a){return f._extend(a),d(),f._ready=f._initStorage(f._config),f._ready})["catch"](b)}d();var g=new Error("No available storage method found.");return f._driverSet=Promise.reject(g),f._driverSet}var c=0;return b()}}var f=this;m(a)||(a=[a]);var g=this._getSupportedDrivers(a),h=null!==this._driverSet?this._driverSet["catch"](function(){return Promise.resolve()}):Promise.resolve();return this._driverSet=h.then(function(){var a=g[0];return f._dbInfo=null,f._ready=null,f.getDriver(a).then(function(a){f._driver=a._driver,d(),f._wrapLibraryMethodsWithReady(),f._initDriver=e(g)})})["catch"](function(){d();var a=new Error("No available storage method found.");return f._driverSet=Promise.reject(a),f._driverSet}),this._driverSet.then(b,c),this._driverSet},a.prototype.supports=function(a){return!!l[a]},a.prototype._extend=function(a){e(this,a)},a.prototype._getSupportedDrivers=function(a){for(var b=[],c=0,d=a.length;d>c;c++){var e=a[c];this.supports(e)&&b.push(e)}return b},a.prototype._wrapLibraryMethodsWithReady=function(){for(var a=0;a<j.length;a++)b(this,j[a])},a.prototype.createInstance=function(b){return new a(b)},a}();return new n}("undefined"!=typeof window?window:self);b["default"]=e,a.exports=b["default"]},function(a,b){"use strict";b.__esModule=!0;var c=function(a){function b(b,c){b=b||[],c=c||{};try{return new Blob(b,c)}catch(d){if("TypeError"!==d.name)throw d;for(var e=a.BlobBuilder||a.MSBlobBuilder||a.MozBlobBuilder||a.WebKitBlobBuilder,f=new e,g=0;g<b.length;g+=1)f.append(b[g]);return f.getBlob(c.type)}}function c(a){for(var b=a.length,c=new ArrayBuffer(b),d=new Uint8Array(c),e=0;b>e;e++)d[e]=a.charCodeAt(e);return c}function d(a){return new Promise(function(b,c){var d=new XMLHttpRequest;d.open("GET",a),d.withCredentials=!0,d.responseType="arraybuffer",d.onreadystatechange=function(){return 4===d.readyState?200===d.status?b({response:d.response,type:d.getResponseHeader("Content-Type")}):void c({status:d.status,response:d.response}):void 0},d.send()})}function e(a){return new Promise(function(c,e){var f=b([""],{type:"image/png"}),g=a.transaction([D],"readwrite");g.objectStore(D).put(f,"key"),g.oncomplete=function(){var b=a.transaction([D],"readwrite"),f=b.objectStore(D).get("key");f.onerror=e,f.onsuccess=function(a){var b=a.target.result,e=URL.createObjectURL(b);d(e).then(function(a){c(!(!a||"image/png"!==a.type))},function(){c(!1)}).then(function(){URL.revokeObjectURL(e)})}},g.onerror=g.onabort=e})["catch"](function(){return!1})}function f(a){return"boolean"==typeof B?Promise.resolve(B):e(a).then(function(a){return B=a})}function g(a){return new Promise(function(b,c){var d=new FileReader;d.onerror=c,d.onloadend=function(c){var d=btoa(c.target.result||"");b({__local_forage_encoded_blob:!0,data:d,type:a.type})},d.readAsBinaryString(a)})}function h(a){var d=c(atob(a.data));return b([d],{type:a.type})}function i(a){return a&&a.__local_forage_encoded_blob}function j(a){var b=this,c=b._initReady().then(function(){var a=C[b._dbInfo.name];return a&&a.dbReady?a.dbReady:void 0});return c.then(a,a),c}function k(a){var b=C[a.name],c={};c.promise=new Promise(function(a){c.resolve=a}),b.deferredOperations.push(c),b.dbReady?b.dbReady=b.dbReady.then(function(){return c.promise}):b.dbReady=c.promise}function l(a){var b=C[a.name],c=b.deferredOperations.pop();c&&c.resolve()}function m(a){function b(){return Promise.resolve()}var c=this,d={db:null};if(a)for(var e in a)d[e]=a[e];C||(C={});var f=C[d.name];f||(f={forages:[],db:null,dbReady:null,deferredOperations:[]},C[d.name]=f),f.forages.push(c),c._initReady||(c._initReady=c.ready,c.ready=j);for(var g=[],h=0;h<f.forages.length;h++){var i=f.forages[h];i!==c&&g.push(i._initReady()["catch"](b))}var k=f.forages.slice(0);return Promise.all(g).then(function(){return d.db=f.db,n(d)}).then(function(a){return d.db=a,q(d,c._defaultConfig.version)?o(d):a}).then(function(a){d.db=f.db=a,c._dbInfo=d;for(var b=0;b<k.length;b++){var e=k[b];e!==c&&(e._dbInfo.db=d.db,e._dbInfo.version=d.version)}})}function n(a){return p(a,!1)}function o(a){return p(a,!0)}function p(b,c){return new Promise(function(d,e){if(b.db){if(!c)return d(b.db);k(b),b.db.close()}var f=[b.name];c&&f.push(b.version);var g=A.open.apply(A,f);c&&(g.onupgradeneeded=function(c){var d=g.result;try{d.createObjectStore(b.storeName),c.oldVersion<=1&&d.createObjectStore(D)}catch(e){if("ConstraintError"!==e.name)throw e;a.console.warn('The database "'+b.name+'" has been upgraded from version '+c.oldVersion+" to version "+c.newVersion+', but the storage "'+b.storeName+'" already exists.')}}),g.onerror=function(){e(g.error)},g.onsuccess=function(){d(g.result),l(b)}})}function q(b,c){if(!b.db)return!0;var d=!b.db.objectStoreNames.contains(b.storeName),e=b.version<b.db.version,f=b.version>b.db.version;if(e&&(b.version!==c&&a.console.warn('The database "'+b.name+"\" can't be downgraded from version "+b.db.version+" to version "+b.version+"."),b.version=b.db.version),f||d){if(d){var g=b.db.version+1;g>b.version&&(b.version=g)}return!0}return!1}function r(b,c){var d=this;"string"!=typeof b&&(a.console.warn(b+" used as a key, but it is not a string."),b=String(b));var e=new Promise(function(a,c){d.ready().then(function(){var e=d._dbInfo,f=e.db.transaction(e.storeName,"readonly").objectStore(e.storeName),g=f.get(b);g.onsuccess=function(){var b=g.result;void 0===b&&(b=null),i(b)&&(b=h(b)),a(b)},g.onerror=function(){c(g.error)}})["catch"](c)});return z(e,c),e}function s(a,b){var c=this,d=new Promise(function(b,d){c.ready().then(function(){var e=c._dbInfo,f=e.db.transaction(e.storeName,"readonly").objectStore(e.storeName),g=f.openCursor(),j=1;g.onsuccess=function(){var c=g.result;if(c){var d=c.value;i(d)&&(d=h(d));var e=a(d,c.key,j++);void 0!==e?b(e):c["continue"]()}else b()},g.onerror=function(){d(g.error)}})["catch"](d)});return z(d,b),d}function t(b,c,d){var e=this;"string"!=typeof b&&(a.console.warn(b+" used as a key, but it is not a string."),b=String(b));var h=new Promise(function(a,d){var h;e.ready().then(function(){return h=e._dbInfo,c instanceof Blob?f(h.db).then(function(a){return a?c:g(c)}):c}).then(function(c){var e=h.db.transaction(h.storeName,"readwrite"),f=e.objectStore(h.storeName);null===c&&(c=void 0),e.oncomplete=function(){void 0===c&&(c=null),a(c)},e.onabort=e.onerror=function(){var a=g.error?g.error:g.transaction.error;d(a)};var g=f.put(c,b)})["catch"](d)});return z(h,d),h}function u(b,c){var d=this;"string"!=typeof b&&(a.console.warn(b+" used as a key, but it is not a string."),b=String(b));var e=new Promise(function(a,c){d.ready().then(function(){var e=d._dbInfo,f=e.db.transaction(e.storeName,"readwrite"),g=f.objectStore(e.storeName),h=g["delete"](b);f.oncomplete=function(){a()},f.onerror=function(){c(h.error)},f.onabort=function(){var a=h.error?h.error:h.transaction.error;c(a)}})["catch"](c)});return z(e,c),e}function v(a){var b=this,c=new Promise(function(a,c){b.ready().then(function(){var d=b._dbInfo,e=d.db.transaction(d.storeName,"readwrite"),f=e.objectStore(d.storeName),g=f.clear();e.oncomplete=function(){a()},e.onabort=e.onerror=function(){var a=g.error?g.error:g.transaction.error;c(a)}})["catch"](c)});return z(c,a),c}function w(a){var b=this,c=new Promise(function(a,c){b.ready().then(function(){var d=b._dbInfo,e=d.db.transaction(d.storeName,"readonly").objectStore(d.storeName),f=e.count();f.onsuccess=function(){a(f.result)},f.onerror=function(){c(f.error)}})["catch"](c)});return z(c,a),c}function x(a,b){var c=this,d=new Promise(function(b,d){return 0>a?void b(null):void c.ready().then(function(){var e=c._dbInfo,f=e.db.transaction(e.storeName,"readonly").objectStore(e.storeName),g=!1,h=f.openCursor();h.onsuccess=function(){var c=h.result;return c?void(0===a?b(c.key):g?b(c.key):(g=!0,c.advance(a))):void b(null)},h.onerror=function(){d(h.error)}})["catch"](d)});return z(d,b),d}function y(a){var b=this,c=new Promise(function(a,c){b.ready().then(function(){var d=b._dbInfo,e=d.db.transaction(d.storeName,"readonly").objectStore(d.storeName),f=e.openCursor(),g=[];f.onsuccess=function(){var b=f.result;return b?(g.push(b.key),void b["continue"]()):void a(g)},f.onerror=function(){c(f.error)}})["catch"](c)});return z(c,a),c}function z(a,b){b&&a.then(function(a){b(null,a)},function(a){b(a)})}var A=A||a.indexedDB||a.webkitIndexedDB||a.mozIndexedDB||a.OIndexedDB||a.msIndexedDB;if(A){var B,C,D="local-forage-detect-blob-support",E={_driver:"asyncStorage",_initStorage:m,iterate:s,getItem:r,setItem:t,removeItem:u,clear:v,length:w,key:x,keys:y};return E}}("undefined"!=typeof window?window:self);b["default"]=c,a.exports=b["default"]},function(a,b,c){"use strict";b.__esModule=!0;var d=function(a){function b(a){var b=this,d={};if(a)for(var e in a)d[e]=a[e];return d.keyPrefix=d.name+"/",d.storeName!==b._defaultConfig.storeName&&(d.keyPrefix+=d.storeName+"/"),b._dbInfo=d,new Promise(function(a,b){a(c(3))}).then(function(a){return d.serializer=a,Promise.resolve()})}function d(a){var b=this,c=b.ready().then(function(){for(var a=b._dbInfo.keyPrefix,c=m.length-1;c>=0;c--){var d=m.key(c);0===d.indexOf(a)&&m.removeItem(d)}});return l(c,a),c}function e(b,c){var d=this;"string"!=typeof b&&(a.console.warn(b+" used as a key, but it is not a string."),b=String(b));var e=d.ready().then(function(){var a=d._dbInfo,c=m.getItem(a.keyPrefix+b);return c&&(c=a.serializer.deserialize(c)),c});return l(e,c),e}function f(a,b){var c=this,d=c.ready().then(function(){for(var b=c._dbInfo,d=b.keyPrefix,e=d.length,f=m.length,g=1,h=0;f>h;h++){var i=m.key(h);if(0===i.indexOf(d)){var j=m.getItem(i);if(j&&(j=b.serializer.deserialize(j)),j=a(j,i.substring(e),g++),void 0!==j)return j}}});return l(d,b),d}function g(a,b){var c=this,d=c.ready().then(function(){var b,d=c._dbInfo;try{b=m.key(a)}catch(e){b=null}return b&&(b=b.substring(d.keyPrefix.length)),b});return l(d,b),d}function h(a){var b=this,c=b.ready().then(function(){for(var a=b._dbInfo,c=m.length,d=[],e=0;c>e;e++)0===m.key(e).indexOf(a.keyPrefix)&&d.push(m.key(e).substring(a.keyPrefix.length));return d});return l(c,a),c}function i(a){var b=this,c=b.keys().then(function(a){return a.length});return l(c,a),c}function j(b,c){var d=this;"string"!=typeof b&&(a.console.warn(b+" used as a key, but it is not a string."),b=String(b));var e=d.ready().then(function(){var a=d._dbInfo;m.removeItem(a.keyPrefix+b)});return l(e,c),e}function k(b,c,d){var e=this;"string"!=typeof b&&(a.console.warn(b+" used as a key, but it is not a string."),b=String(b));var f=e.ready().then(function(){void 0===c&&(c=null);var a=c;return new Promise(function(d,f){var g=e._dbInfo;g.serializer.serialize(c,function(c,e){if(e)f(e);else try{m.setItem(g.keyPrefix+b,c),d(a)}catch(h){("QuotaExceededError"===h.name||"NS_ERROR_DOM_QUOTA_REACHED"===h.name)&&f(h),f(h)}})})});return l(f,d),f}function l(a,b){b&&a.then(function(a){b(null,a)},function(a){b(a)})}var m=null;try{if(!(a.localStorage&&"setItem"in a.localStorage))return;m=a.localStorage}catch(n){return}var o={_driver:"localStorageWrapper",_initStorage:b,iterate:f,getItem:e,setItem:k,removeItem:j,clear:d,length:i,key:g,keys:h};return o}("undefined"!=typeof window?window:self);b["default"]=d,a.exports=b["default"]},function(a,b){"use strict";b.__esModule=!0;var c=function(a){function b(b,c){b=b||[],c=c||{};try{return new Blob(b,c)}catch(d){if("TypeError"!==d.name)throw d;for(var e=a.BlobBuilder||a.MSBlobBuilder||a.MozBlobBuilder||a.WebKitBlobBuilder,f=new e,g=0;g<b.length;g+=1)f.append(b[g]);return f.getBlob(c.type)}}function c(a,b){var c="";if(a&&(c=a.toString()),a&&("[object ArrayBuffer]"===a.toString()||a.buffer&&"[object ArrayBuffer]"===a.buffer.toString())){var d,e=j;a instanceof ArrayBuffer?(d=a,e+=l):(d=a.buffer,"[object Int8Array]"===c?e+=n:"[object Uint8Array]"===c?e+=o:"[object Uint8ClampedArray]"===c?e+=p:"[object Int16Array]"===c?e+=q:"[object Uint16Array]"===c?e+=s:"[object Int32Array]"===c?e+=r:"[object Uint32Array]"===c?e+=t:"[object Float32Array]"===c?e+=u:"[object Float64Array]"===c?e+=v:b(new Error("Failed to get type for BinaryArray"))),b(e+f(d))}else if("[object Blob]"===c){var g=new FileReader;g.onload=function(){var c=h+a.type+"~"+f(this.result);b(j+m+c)},g.readAsArrayBuffer(a)}else try{b(JSON.stringify(a))}catch(i){console.error("Couldn't convert value into a JSON string: ",a),b(null,i)}}function d(a){if(a.substring(0,k)!==j)return JSON.parse(a);var c,d=a.substring(w),f=a.substring(k,w);if(f===m&&i.test(d)){var g=d.match(i);c=g[1],d=d.substring(g[0].length)}var h=e(d);switch(f){case l:return h;case m:return b([h],{type:c});case n:return new Int8Array(h);case o:return new Uint8Array(h);case p:return new Uint8ClampedArray(h);case q:return new Int16Array(h);case s:return new Uint16Array(h);case r:return new Int32Array(h);case t:return new Uint32Array(h);case u:return new Float32Array(h);case v:return new Float64Array(h);default:throw new Error("Unkown type: "+f)}}function e(a){var b,c,d,e,f,h=.75*a.length,i=a.length,j=0;"="===a[a.length-1]&&(h--,"="===a[a.length-2]&&h--);var k=new ArrayBuffer(h),l=new Uint8Array(k);for(b=0;i>b;b+=4)c=g.indexOf(a[b]),d=g.indexOf(a[b+1]),e=g.indexOf(a[b+2]),f=g.indexOf(a[b+3]),l[j++]=c<<2|d>>4,l[j++]=(15&d)<<4|e>>2,l[j++]=(3&e)<<6|63&f;return k}function f(a){var b,c=new Uint8Array(a),d="";for(b=0;b<c.length;b+=3)d+=g[c[b]>>2],d+=g[(3&c[b])<<4|c[b+1]>>4],d+=g[(15&c[b+1])<<2|c[b+2]>>6],d+=g[63&c[b+2]];return c.length%3===2?d=d.substring(0,d.length-1)+"=":c.length%3===1&&(d=d.substring(0,d.length-2)+"=="),d}var g="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",h="~~local_forage_type~",i=/^~~local_forage_type~([^~]+)~/,j="__lfsc__:",k=j.length,l="arbf",m="blob",n="si08",o="ui08",p="uic8",q="si16",r="si32",s="ur16",t="ui32",u="fl32",v="fl64",w=k+l.length,x={serialize:c,deserialize:d,stringToBuffer:e,bufferToString:f};return x}("undefined"!=typeof window?window:self);b["default"]=c,a.exports=b["default"]},function(a,b,c){"use strict";b.__esModule=!0;var d=function(a){function b(a){var b=this,d={db:null};if(a)for(var e in a)d[e]="string"!=typeof a[e]?a[e].toString():a[e];var f=new Promise(function(a,c){try{d.db=m(d.name,String(d.version),d.description,d.size)}catch(e){return c(e)}d.db.transaction(function(e){e.executeSql("CREATE TABLE IF NOT EXISTS "+d.storeName+" (id INTEGER PRIMARY KEY, key unique, value)",[],function(){b._dbInfo=d,a()},function(a,b){c(b)})})});return new Promise(function(a,b){a(c(3))}).then(function(a){return d.serializer=a,f})}function d(b,c){var d=this;"string"!=typeof b&&(a.console.warn(b+" used as a key, but it is not a string."),b=String(b));var e=new Promise(function(a,c){d.ready().then(function(){var e=d._dbInfo;e.db.transaction(function(d){d.executeSql("SELECT * FROM "+e.storeName+" WHERE key = ? LIMIT 1",[b],function(b,c){var d=c.rows.length?c.rows.item(0).value:null;d&&(d=e.serializer.deserialize(d)),a(d)},function(a,b){c(b)})})})["catch"](c)});return l(e,c),e}function e(a,b){var c=this,d=new Promise(function(b,d){c.ready().then(function(){var e=c._dbInfo;e.db.transaction(function(c){c.executeSql("SELECT * FROM "+e.storeName,[],function(c,d){for(var f=d.rows,g=f.length,h=0;g>h;h++){var i=f.item(h),j=i.value;if(j&&(j=e.serializer.deserialize(j)),j=a(j,i.key,h+1),void 0!==j)return void b(j)}b()},function(a,b){d(b)})})})["catch"](d)});return l(d,b),d}function f(b,c,d){var e=this;"string"!=typeof b&&(a.console.warn(b+" used as a key, but it is not a string."),b=String(b));var f=new Promise(function(a,d){e.ready().then(function(){void 0===c&&(c=null);var f=c,g=e._dbInfo;g.serializer.serialize(c,function(c,e){e?d(e):g.db.transaction(function(e){e.executeSql("INSERT OR REPLACE INTO "+g.storeName+" (key, value) VALUES (?, ?)",[b,c],function(){a(f)},function(a,b){d(b)})},function(a){a.code===a.QUOTA_ERR&&d(a)})})})["catch"](d)});return l(f,d),f}function g(b,c){var d=this;"string"!=typeof b&&(a.console.warn(b+" used as a key, but it is not a string."),b=String(b));var e=new Promise(function(a,c){d.ready().then(function(){var e=d._dbInfo;e.db.transaction(function(d){d.executeSql("DELETE FROM "+e.storeName+" WHERE key = ?",[b],function(){a()},function(a,b){c(b)})})})["catch"](c)});return l(e,c),e}function h(a){var b=this,c=new Promise(function(a,c){b.ready().then(function(){var d=b._dbInfo;d.db.transaction(function(b){b.executeSql("DELETE FROM "+d.storeName,[],function(){a()},function(a,b){c(b)})})})["catch"](c)});return l(c,a),c}function i(a){var b=this,c=new Promise(function(a,c){b.ready().then(function(){var d=b._dbInfo;d.db.transaction(function(b){b.executeSql("SELECT COUNT(key) as c FROM "+d.storeName,[],function(b,c){var d=c.rows.item(0).c;a(d)},function(a,b){c(b)})})})["catch"](c)});return l(c,a),c}function j(a,b){var c=this,d=new Promise(function(b,d){c.ready().then(function(){var e=c._dbInfo;e.db.transaction(function(c){c.executeSql("SELECT key FROM "+e.storeName+" WHERE id = ? LIMIT 1",[a+1],function(a,c){var d=c.rows.length?c.rows.item(0).key:null;b(d)},function(a,b){d(b)})})})["catch"](d)});return l(d,b),d}function k(a){var b=this,c=new Promise(function(a,c){b.ready().then(function(){var d=b._dbInfo;d.db.transaction(function(b){b.executeSql("SELECT key FROM "+d.storeName,[],function(b,c){for(var d=[],e=0;e<c.rows.length;e++)d.push(c.rows.item(e).key);a(d)},function(a,b){c(b)})})})["catch"](c)});return l(c,a),c}function l(a,b){b&&a.then(function(a){b(null,a)},function(a){b(a)})}var m=a.openDatabase;if(m){var n={_driver:"webSQLStorage",_initStorage:b,iterate:e,getItem:d,setItem:f,removeItem:g,clear:h,length:i,key:j,keys:k};return n}}("undefined"!=typeof window?window:self);b["default"]=d,a.exports=b["default"]}])}); | ||
!function(a){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;b="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,b.localforage=a()}}(function(){return function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);var j=new Error("Cannot find module '"+g+"'");throw j.code="MODULE_NOT_FOUND",j}var k=c[g]={exports:{}};b[g][0].call(k.exports,function(a){var c=b[g][1][a];return e(c?c:a)},k,k.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({1:[function(a,b,c){"use strict";function d(){}function e(a){if("function"!=typeof a)throw new TypeError("resolver must be a function");this.state=s,this.queue=[],this.outcome=void 0,a!==d&&i(this,a)}function f(a,b,c){this.promise=a,"function"==typeof b&&(this.onFulfilled=b,this.callFulfilled=this.otherCallFulfilled),"function"==typeof c&&(this.onRejected=c,this.callRejected=this.otherCallRejected)}function g(a,b,c){o(function(){var d;try{d=b(c)}catch(e){return p.reject(a,e)}d===a?p.reject(a,new TypeError("Cannot resolve promise with itself")):p.resolve(a,d)})}function h(a){var b=a&&a.then;return a&&"object"==typeof a&&"function"==typeof b?function(){b.apply(a,arguments)}:void 0}function i(a,b){function c(b){f||(f=!0,p.reject(a,b))}function d(b){f||(f=!0,p.resolve(a,b))}function e(){b(d,c)}var f=!1,g=j(e);"error"===g.status&&c(g.value)}function j(a,b){var c={};try{c.value=a(b),c.status="success"}catch(d){c.status="error",c.value=d}return c}function k(a){return a instanceof this?a:p.resolve(new this(d),a)}function l(a){var b=new this(d);return p.reject(b,a)}function m(a){function b(a,b){function d(a){g[b]=a,++h!==e||f||(f=!0,p.resolve(j,g))}c.resolve(a).then(d,function(a){f||(f=!0,p.reject(j,a))})}var c=this;if("[object Array]"!==Object.prototype.toString.call(a))return this.reject(new TypeError("must be an array"));var e=a.length,f=!1;if(!e)return this.resolve([]);for(var g=new Array(e),h=0,i=-1,j=new this(d);++i<e;)b(a[i],i);return j}function n(a){function b(a){c.resolve(a).then(function(a){f||(f=!0,p.resolve(h,a))},function(a){f||(f=!0,p.reject(h,a))})}var c=this;if("[object Array]"!==Object.prototype.toString.call(a))return this.reject(new TypeError("must be an array"));var e=a.length,f=!1;if(!e)return this.resolve([]);for(var g=-1,h=new this(d);++g<e;)b(a[g]);return h}var o=a(2),p={},q=["REJECTED"],r=["FULFILLED"],s=["PENDING"];b.exports=c=e,e.prototype["catch"]=function(a){return this.then(null,a)},e.prototype.then=function(a,b){if("function"!=typeof a&&this.state===r||"function"!=typeof b&&this.state===q)return this;var c=new this.constructor(d);if(this.state!==s){var e=this.state===r?a:b;g(c,e,this.outcome)}else this.queue.push(new f(c,a,b));return c},f.prototype.callFulfilled=function(a){p.resolve(this.promise,a)},f.prototype.otherCallFulfilled=function(a){g(this.promise,this.onFulfilled,a)},f.prototype.callRejected=function(a){p.reject(this.promise,a)},f.prototype.otherCallRejected=function(a){g(this.promise,this.onRejected,a)},p.resolve=function(a,b){var c=j(h,b);if("error"===c.status)return p.reject(a,c.value);var d=c.value;if(d)i(a,d);else{a.state=r,a.outcome=b;for(var e=-1,f=a.queue.length;++e<f;)a.queue[e].callFulfilled(b)}return a},p.reject=function(a,b){a.state=q,a.outcome=b;for(var c=-1,d=a.queue.length;++c<d;)a.queue[c].callRejected(b);return a},c.resolve=k,c.reject=l,c.all=m,c.race=n},{2:2}],2:[function(a,b,c){(function(a){"use strict";function c(){k=!0;for(var a,b,c=l.length;c;){for(b=l,l=[],a=-1;++a<c;)b[a]();c=l.length}k=!1}function d(a){1!==l.push(a)||k||e()}var e,f=a.MutationObserver||a.WebKitMutationObserver;if(f){var g=0,h=new f(c),i=a.document.createTextNode("");h.observe(i,{characterData:!0}),e=function(){i.data=g=++g%2}}else if(a.setImmediate||"undefined"==typeof a.MessageChannel)e="document"in a&&"onreadystatechange"in a.document.createElement("script")?function(){var b=a.document.createElement("script");b.onreadystatechange=function(){c(),b.onreadystatechange=null,b.parentNode.removeChild(b),b=null},a.document.documentElement.appendChild(b)}:function(){setTimeout(c,0)};else{var j=new a.MessageChannel;j.port1.onmessage=c,e=function(){j.port2.postMessage(0)}}var k,l=[];b.exports=d}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{}],3:[function(a,b,c){(function(b){"use strict";"function"!=typeof b.Promise&&(b.Promise=a(1))}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{1:1}],4:[function(a,b,c){"use strict";function d(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function e(){return"undefined"!=typeof indexedDB?indexedDB:"undefined"!=typeof webkitIndexedDB?webkitIndexedDB:"undefined"!=typeof mozIndexedDB?mozIndexedDB:"undefined"!=typeof OIndexedDB?OIndexedDB:"undefined"!=typeof msIndexedDB?msIndexedDB:void 0}function f(){try{return fa?"undefined"!=typeof openDatabase&&"undefined"!=typeof navigator&&navigator.userAgent&&/Safari/.test(navigator.userAgent)&&!/Chrome/.test(navigator.userAgent)?!1:fa&&"function"==typeof fa.open&&"undefined"!=typeof IDBKeyRange:!1}catch(a){return!1}}function g(){return"function"==typeof openDatabase}function h(){try{return"undefined"!=typeof localStorage&&"setItem"in localStorage&&localStorage.setItem}catch(a){return!1}}function i(a,b){a=a||[],b=b||{};try{return new Blob(a,b)}catch(c){if("TypeError"!==c.name)throw c;for(var d="undefined"!=typeof BlobBuilder?BlobBuilder:"undefined"!=typeof MSBlobBuilder?MSBlobBuilder:"undefined"!=typeof MozBlobBuilder?MozBlobBuilder:WebKitBlobBuilder,e=new d,f=0;f<a.length;f+=1)e.append(a[f]);return e.getBlob(b.type)}}function j(a,b){b&&a.then(function(a){b(null,a)},function(a){b(a)})}function k(a){for(var b=a.length,c=new ArrayBuffer(b),d=new Uint8Array(c),e=0;b>e;e++)d[e]=a.charCodeAt(e);return c}function l(a){return new ia(function(b){var c=i([""]);a.objectStore(ja).put(c,"key"),a.onabort=function(a){a.preventDefault(),a.stopPropagation(),b(!1)},a.oncomplete=function(){var a=navigator.userAgent.match(/Chrome\/(\d+)/),c=navigator.userAgent.match(/Edge\//);b(c||!a||parseInt(a[1],10)>=43)}})["catch"](function(){return!1})}function m(a){return"boolean"==typeof ga?ia.resolve(ga):l(a).then(function(a){return ga=a})}function n(a){var b=ha[a.name],c={};c.promise=new ia(function(a){c.resolve=a}),b.deferredOperations.push(c),b.dbReady?b.dbReady=b.dbReady.then(function(){return c.promise}):b.dbReady=c.promise}function o(a){var b=ha[a.name],c=b.deferredOperations.pop();c&&c.resolve()}function p(a,b){return new ia(function(c,d){if(a.db){if(!b)return c(a.db);n(a),a.db.close()}var e=[a.name];b&&e.push(a.version);var f=fa.open.apply(fa,e);b&&(f.onupgradeneeded=function(b){var c=f.result;try{c.createObjectStore(a.storeName),b.oldVersion<=1&&c.createObjectStore(ja)}catch(d){if("ConstraintError"!==d.name)throw d;console.warn('The database "'+a.name+'" has been upgraded from version '+b.oldVersion+" to version "+b.newVersion+', but the storage "'+a.storeName+'" already exists.')}}),f.onerror=function(){d(f.error)},f.onsuccess=function(){c(f.result),o(a)}})}function q(a){return p(a,!1)}function r(a){return p(a,!0)}function s(a,b){if(!a.db)return!0;var c=!a.db.objectStoreNames.contains(a.storeName),d=a.version<a.db.version,e=a.version>a.db.version;if(d&&(a.version!==b&&console.warn('The database "'+a.name+"\" can't be downgraded from version "+a.db.version+" to version "+a.version+"."),a.version=a.db.version),e||c){if(c){var f=a.db.version+1;f>a.version&&(a.version=f)}return!0}return!1}function t(a){return new ia(function(b,c){var d=new FileReader;d.onerror=c,d.onloadend=function(c){var d=btoa(c.target.result||"");b({__local_forage_encoded_blob:!0,data:d,type:a.type})},d.readAsBinaryString(a)})}function u(a){var b=k(atob(a.data));return i([b],{type:a.type})}function v(a){return a&&a.__local_forage_encoded_blob}function w(a){var b=this,c=b._initReady().then(function(){var a=ha[b._dbInfo.name];return a&&a.dbReady?a.dbReady:void 0});return c.then(a,a),c}function x(a){function b(){return ia.resolve()}var c=this,d={db:null};if(a)for(var e in a)d[e]=a[e];ha||(ha={});var f=ha[d.name];f||(f={forages:[],db:null,dbReady:null,deferredOperations:[]},ha[d.name]=f),f.forages.push(c),c._initReady||(c._initReady=c.ready,c.ready=w);for(var g=[],h=0;h<f.forages.length;h++){var i=f.forages[h];i!==c&&g.push(i._initReady()["catch"](b))}var j=f.forages.slice(0);return ia.all(g).then(function(){return d.db=f.db,q(d)}).then(function(a){return d.db=a,s(d,c._defaultConfig.version)?r(d):a}).then(function(a){d.db=f.db=a,c._dbInfo=d;for(var b=0;b<j.length;b++){var e=j[b];e!==c&&(e._dbInfo.db=d.db,e._dbInfo.version=d.version)}})}function y(a,b){var c=this;"string"!=typeof a&&(console.warn(a+" used as a key, but it is not a string."),a=String(a));var d=new ia(function(b,d){c.ready().then(function(){var e=c._dbInfo,f=e.db.transaction(e.storeName,"readonly").objectStore(e.storeName),g=f.get(a);g.onsuccess=function(){var a=g.result;void 0===a&&(a=null),v(a)&&(a=u(a)),b(a)},g.onerror=function(){d(g.error)}})["catch"](d)});return j(d,b),d}function z(a,b){var c=this,d=new ia(function(b,d){c.ready().then(function(){var e=c._dbInfo,f=e.db.transaction(e.storeName,"readonly").objectStore(e.storeName),g=f.openCursor(),h=1;g.onsuccess=function(){var c=g.result;if(c){var d=c.value;v(d)&&(d=u(d));var e=a(d,c.key,h++);void 0!==e?b(e):c["continue"]()}else b()},g.onerror=function(){d(g.error)}})["catch"](d)});return j(d,b),d}function A(a,b,c){var d=this;"string"!=typeof a&&(console.warn(a+" used as a key, but it is not a string."),a=String(a));var e=new ia(function(c,e){var f;d.ready().then(function(){return f=d._dbInfo,b instanceof Blob?m(f.db).then(function(a){return a?b:t(b)}):b}).then(function(b){var d=f.db.transaction(f.storeName,"readwrite"),g=d.objectStore(f.storeName);null===b&&(b=void 0),d.oncomplete=function(){void 0===b&&(b=null),c(b)},d.onabort=d.onerror=function(){var a=h.error?h.error:h.transaction.error;e(a)};var h=g.put(b,a)})["catch"](e)});return j(e,c),e}function B(a,b){var c=this;"string"!=typeof a&&(console.warn(a+" used as a key, but it is not a string."),a=String(a));var d=new ia(function(b,d){c.ready().then(function(){var e=c._dbInfo,f=e.db.transaction(e.storeName,"readwrite"),g=f.objectStore(e.storeName),h=g["delete"](a);f.oncomplete=function(){b()},f.onerror=function(){d(h.error)},f.onabort=function(){var a=h.error?h.error:h.transaction.error;d(a)}})["catch"](d)});return j(d,b),d}function C(a){var b=this,c=new ia(function(a,c){b.ready().then(function(){var d=b._dbInfo,e=d.db.transaction(d.storeName,"readwrite"),f=e.objectStore(d.storeName),g=f.clear();e.oncomplete=function(){a()},e.onabort=e.onerror=function(){var a=g.error?g.error:g.transaction.error;c(a)}})["catch"](c)});return j(c,a),c}function D(a){var b=this,c=new ia(function(a,c){b.ready().then(function(){var d=b._dbInfo,e=d.db.transaction(d.storeName,"readonly").objectStore(d.storeName),f=e.count();f.onsuccess=function(){a(f.result)},f.onerror=function(){c(f.error)}})["catch"](c)});return j(c,a),c}function E(a,b){var c=this,d=new ia(function(b,d){return 0>a?void b(null):void c.ready().then(function(){var e=c._dbInfo,f=e.db.transaction(e.storeName,"readonly").objectStore(e.storeName),g=!1,h=f.openCursor();h.onsuccess=function(){var c=h.result;return c?void(0===a?b(c.key):g?b(c.key):(g=!0,c.advance(a))):void b(null)},h.onerror=function(){d(h.error)}})["catch"](d)});return j(d,b),d}function F(a){var b=this,c=new ia(function(a,c){b.ready().then(function(){var d=b._dbInfo,e=d.db.transaction(d.storeName,"readonly").objectStore(d.storeName),f=e.openCursor(),g=[];f.onsuccess=function(){var b=f.result;return b?(g.push(b.key),void b["continue"]()):void a(g)},f.onerror=function(){c(f.error)}})["catch"](c)});return j(c,a),c}function G(a){var b,c,d,e,f,g=.75*a.length,h=a.length,i=0;"="===a[a.length-1]&&(g--,"="===a[a.length-2]&&g--);var j=new ArrayBuffer(g),k=new Uint8Array(j);for(b=0;h>b;b+=4)c=la.indexOf(a[b]),d=la.indexOf(a[b+1]),e=la.indexOf(a[b+2]),f=la.indexOf(a[b+3]),k[i++]=c<<2|d>>4,k[i++]=(15&d)<<4|e>>2,k[i++]=(3&e)<<6|63&f;return j}function H(a){var b,c=new Uint8Array(a),d="";for(b=0;b<c.length;b+=3)d+=la[c[b]>>2],d+=la[(3&c[b])<<4|c[b+1]>>4],d+=la[(15&c[b+1])<<2|c[b+2]>>6],d+=la[63&c[b+2]];return c.length%3===2?d=d.substring(0,d.length-1)+"=":c.length%3===1&&(d=d.substring(0,d.length-2)+"=="),d}function I(a,b){var c="";if(a&&(c=a.toString()),a&&("[object ArrayBuffer]"===a.toString()||a.buffer&&"[object ArrayBuffer]"===a.buffer.toString())){var d,e=oa;a instanceof ArrayBuffer?(d=a,e+=qa):(d=a.buffer,"[object Int8Array]"===c?e+=sa:"[object Uint8Array]"===c?e+=ta:"[object Uint8ClampedArray]"===c?e+=ua:"[object Int16Array]"===c?e+=va:"[object Uint16Array]"===c?e+=xa:"[object Int32Array]"===c?e+=wa:"[object Uint32Array]"===c?e+=ya:"[object Float32Array]"===c?e+=za:"[object Float64Array]"===c?e+=Aa:b(new Error("Failed to get type for BinaryArray"))),b(e+H(d))}else if("[object Blob]"===c){var f=new FileReader;f.onload=function(){var c=ma+a.type+"~"+H(this.result);b(oa+ra+c)},f.readAsArrayBuffer(a)}else try{b(JSON.stringify(a))}catch(g){console.error("Couldn't convert value into a JSON string: ",a),b(null,g)}}function J(a){if(a.substring(0,pa)!==oa)return JSON.parse(a);var b,c=a.substring(Ba),d=a.substring(pa,Ba);if(d===ra&&na.test(c)){var e=c.match(na);b=e[1],c=c.substring(e[0].length)}var f=G(c);switch(d){case qa:return f;case ra:return i([f],{type:b});case sa:return new Int8Array(f);case ta:return new Uint8Array(f);case ua:return new Uint8ClampedArray(f);case va:return new Int16Array(f);case xa:return new Uint16Array(f);case wa:return new Int32Array(f);case ya:return new Uint32Array(f);case za:return new Float32Array(f);case Aa:return new Float64Array(f);default:throw new Error("Unkown type: "+d)}}function K(a){var b=this,c={db:null};if(a)for(var d in a)c[d]="string"!=typeof a[d]?a[d].toString():a[d];var e=new ia(function(a,d){try{c.db=openDatabase(c.name,String(c.version),c.description,c.size)}catch(e){return d(e)}c.db.transaction(function(e){e.executeSql("CREATE TABLE IF NOT EXISTS "+c.storeName+" (id INTEGER PRIMARY KEY, key unique, value)",[],function(){b._dbInfo=c,a()},function(a,b){d(b)})})});return c.serializer=Ca,e}function L(a,b){var c=this;"string"!=typeof a&&(console.warn(a+" used as a key, but it is not a string."),a=String(a));var d=new ia(function(b,d){c.ready().then(function(){var e=c._dbInfo;e.db.transaction(function(c){c.executeSql("SELECT * FROM "+e.storeName+" WHERE key = ? LIMIT 1",[a],function(a,c){var d=c.rows.length?c.rows.item(0).value:null;d&&(d=e.serializer.deserialize(d)),b(d)},function(a,b){d(b)})})})["catch"](d)});return j(d,b),d}function M(a,b){var c=this,d=new ia(function(b,d){c.ready().then(function(){var e=c._dbInfo;e.db.transaction(function(c){c.executeSql("SELECT * FROM "+e.storeName,[],function(c,d){for(var f=d.rows,g=f.length,h=0;g>h;h++){var i=f.item(h),j=i.value;if(j&&(j=e.serializer.deserialize(j)),j=a(j,i.key,h+1),void 0!==j)return void b(j)}b()},function(a,b){d(b)})})})["catch"](d)});return j(d,b),d}function N(a,b,c){var d=this;"string"!=typeof a&&(console.warn(a+" used as a key, but it is not a string."),a=String(a));var e=new ia(function(c,e){d.ready().then(function(){void 0===b&&(b=null);var f=b,g=d._dbInfo;g.serializer.serialize(b,function(b,d){d?e(d):g.db.transaction(function(d){d.executeSql("INSERT OR REPLACE INTO "+g.storeName+" (key, value) VALUES (?, ?)",[a,b],function(){c(f)},function(a,b){e(b)})},function(a){a.code===a.QUOTA_ERR&&e(a)})})})["catch"](e)});return j(e,c),e}function O(a,b){var c=this;"string"!=typeof a&&(console.warn(a+" used as a key, but it is not a string."),a=String(a));var d=new ia(function(b,d){c.ready().then(function(){var e=c._dbInfo;e.db.transaction(function(c){c.executeSql("DELETE FROM "+e.storeName+" WHERE key = ?",[a],function(){b()},function(a,b){d(b)})})})["catch"](d)});return j(d,b),d}function P(a){var b=this,c=new ia(function(a,c){b.ready().then(function(){var d=b._dbInfo;d.db.transaction(function(b){b.executeSql("DELETE FROM "+d.storeName,[],function(){a()},function(a,b){c(b)})})})["catch"](c)});return j(c,a),c}function Q(a){var b=this,c=new ia(function(a,c){b.ready().then(function(){var d=b._dbInfo;d.db.transaction(function(b){b.executeSql("SELECT COUNT(key) as c FROM "+d.storeName,[],function(b,c){var d=c.rows.item(0).c;a(d)},function(a,b){c(b)})})})["catch"](c)});return j(c,a),c}function R(a,b){var c=this,d=new ia(function(b,d){c.ready().then(function(){var e=c._dbInfo;e.db.transaction(function(c){c.executeSql("SELECT key FROM "+e.storeName+" WHERE id = ? LIMIT 1",[a+1],function(a,c){var d=c.rows.length?c.rows.item(0).key:null;b(d)},function(a,b){d(b)})})})["catch"](d)});return j(d,b),d}function S(a){var b=this,c=new ia(function(a,c){b.ready().then(function(){var d=b._dbInfo;d.db.transaction(function(b){b.executeSql("SELECT key FROM "+d.storeName,[],function(b,c){for(var d=[],e=0;e<c.rows.length;e++)d.push(c.rows.item(e).key);a(d)},function(a,b){c(b)})})})["catch"](c)});return j(c,a),c}function T(a){var b=this,c={};if(a)for(var d in a)c[d]=a[d];return c.keyPrefix=c.name+"/",c.storeName!==b._defaultConfig.storeName&&(c.keyPrefix+=c.storeName+"/"),b._dbInfo=c,c.serializer=Ca,ia.resolve()}function U(a){var b=this,c=b.ready().then(function(){for(var a=b._dbInfo.keyPrefix,c=localStorage.length-1;c>=0;c--){var d=localStorage.key(c);0===d.indexOf(a)&&localStorage.removeItem(d)}});return j(c,a),c}function V(a,b){var c=this;"string"!=typeof a&&(console.warn(a+" used as a key, but it is not a string."),a=String(a));var d=c.ready().then(function(){var b=c._dbInfo,d=localStorage.getItem(b.keyPrefix+a);return d&&(d=b.serializer.deserialize(d)),d});return j(d,b),d}function W(a,b){var c=this,d=c.ready().then(function(){for(var b=c._dbInfo,d=b.keyPrefix,e=d.length,f=localStorage.length,g=1,h=0;f>h;h++){var i=localStorage.key(h);if(0===i.indexOf(d)){var j=localStorage.getItem(i);if(j&&(j=b.serializer.deserialize(j)),j=a(j,i.substring(e),g++),void 0!==j)return j}}});return j(d,b),d}function X(a,b){var c=this,d=c.ready().then(function(){var b,d=c._dbInfo;try{b=localStorage.key(a)}catch(e){b=null}return b&&(b=b.substring(d.keyPrefix.length)),b});return j(d,b),d}function Y(a){var b=this,c=b.ready().then(function(){for(var a=b._dbInfo,c=localStorage.length,d=[],e=0;c>e;e++)0===localStorage.key(e).indexOf(a.keyPrefix)&&d.push(localStorage.key(e).substring(a.keyPrefix.length));return d});return j(c,a),c}function Z(a){var b=this,c=b.keys().then(function(a){return a.length});return j(c,a),c}function $(a,b){var c=this;"string"!=typeof a&&(console.warn(a+" used as a key, but it is not a string."),a=String(a));var d=c.ready().then(function(){var b=c._dbInfo;localStorage.removeItem(b.keyPrefix+a)});return j(d,b),d}function _(a,b,c){var d=this;"string"!=typeof a&&(console.warn(a+" used as a key, but it is not a string."),a=String(a));var e=d.ready().then(function(){void 0===b&&(b=null);var c=b;return new ia(function(e,f){var g=d._dbInfo;g.serializer.serialize(b,function(b,d){if(d)f(d);else try{localStorage.setItem(g.keyPrefix+a,b),e(c)}catch(h){"QuotaExceededError"!==h.name&&"NS_ERROR_DOM_QUOTA_REACHED"!==h.name||f(h),f(h)}})})});return j(e,c),e}function aa(a,b,c){"function"==typeof b&&a.then(b),"function"==typeof c&&a["catch"](c)}function ba(a,b){a[b]=function(){var c=arguments;return a.ready().then(function(){return a[b].apply(a,c)})}}function ca(){for(var a=1;a<arguments.length;a++){var b=arguments[a];if(b)for(var c in b)b.hasOwnProperty(c)&&(La(b[c])?arguments[0][c]=b[c].slice():arguments[0][c]=b[c])}return arguments[0]}function da(a){for(var b in Ga)if(Ga.hasOwnProperty(b)&&Ga[b]===a)return!0;return!1}var ea="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol?"symbol":typeof a},fa=e();a(3);var ga,ha,ia=Promise,ja="local-forage-detect-blob-support",ka={_driver:"asyncStorage",_initStorage:x,iterate:z,getItem:y,setItem:A,removeItem:B,clear:C,length:D,key:E,keys:F},la="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",ma="~~local_forage_type~",na=/^~~local_forage_type~([^~]+)~/,oa="__lfsc__:",pa=oa.length,qa="arbf",ra="blob",sa="si08",ta="ui08",ua="uic8",va="si16",wa="si32",xa="ur16",ya="ui32",za="fl32",Aa="fl64",Ba=pa+qa.length,Ca={serialize:I,deserialize:J,stringToBuffer:G,bufferToString:H},Da={_driver:"webSQLStorage",_initStorage:K,iterate:M,getItem:L,setItem:N,removeItem:O,clear:P,length:Q,key:R,keys:S},Ea={_driver:"localStorageWrapper",_initStorage:T,iterate:W,getItem:V,setItem:_,removeItem:$,clear:U,length:Z,key:X,keys:Y},Fa={},Ga={INDEXEDDB:"asyncStorage",LOCALSTORAGE:"localStorageWrapper",WEBSQL:"webSQLStorage"},Ha=[Ga.INDEXEDDB,Ga.WEBSQL,Ga.LOCALSTORAGE],Ia=["clear","getItem","iterate","key","keys","length","removeItem","setItem"],Ja={description:"",driver:Ha.slice(),name:"localforage",size:4980736,storeName:"keyvaluepairs",version:1},Ka={};Ka[Ga.INDEXEDDB]=f(),Ka[Ga.WEBSQL]=g(),Ka[Ga.LOCALSTORAGE]=h();var La=Array.isArray||function(a){return"[object Array]"===Object.prototype.toString.call(a)},Ma=function(){function a(b){d(this,a),this.INDEXEDDB=Ga.INDEXEDDB,this.LOCALSTORAGE=Ga.LOCALSTORAGE,this.WEBSQL=Ga.WEBSQL,this._defaultConfig=ca({},Ja),this._config=ca({},this._defaultConfig,b),this._driverSet=null,this._initDriver=null,this._ready=!1,this._dbInfo=null,this._wrapLibraryMethodsWithReady(),this.setDriver(this._config.driver)}return a.prototype.config=function(a){if("object"===("undefined"==typeof a?"undefined":ea(a))){if(this._ready)return new Error("Can't call config() after localforage has been used.");for(var b in a)"storeName"===b&&(a[b]=a[b].replace(/\W/g,"_")),this._config[b]=a[b];return"driver"in a&&a.driver&&this.setDriver(this._config.driver),!0}return"string"==typeof a?this._config[a]:this._config},a.prototype.defineDriver=function(a,b,c){var d=new ia(function(b,c){try{var d=a._driver,e=new Error("Custom driver not compliant; see https://mozilla.github.io/localForage/#definedriver"),f=new Error("Custom driver name already in use: "+a._driver);if(!a._driver)return void c(e);if(da(a._driver))return void c(f);for(var g=Ia.concat("_initStorage"),h=0;h<g.length;h++){var i=g[h];if(!i||!a[i]||"function"!=typeof a[i])return void c(e)}var j=ia.resolve(!0);"_support"in a&&(j=a._support&&"function"==typeof a._support?a._support():ia.resolve(!!a._support)),j.then(function(c){Ka[d]=c,Fa[d]=a,b()},c)}catch(k){c(k)}});return aa(d,b,c),d},a.prototype.driver=function(){return this._driver||null},a.prototype.getDriver=function(a,b,c){var d=this,e=ia.resolve().then(function(){if(!da(a)){if(Fa[a])return Fa[a];throw new Error("Driver not found.")}switch(a){case d.INDEXEDDB:return ka;case d.LOCALSTORAGE:return Ea;case d.WEBSQL:return Da}});return aa(e,b,c),e},a.prototype.getSerializer=function(a){var b=ia.resolve(Ca);return aa(b,a),b},a.prototype.ready=function(a){var b=this,c=b._driverSet.then(function(){return null===b._ready&&(b._ready=b._initDriver()),b._ready});return aa(c,a,a),c},a.prototype.setDriver=function(a,b,c){function d(){f._config.driver=f.driver()}function e(a){return function(){function b(){for(;c<a.length;){var e=a[c];return c++,f._dbInfo=null,f._ready=null,f.getDriver(e).then(function(a){return f._extend(a),d(),f._ready=f._initStorage(f._config),f._ready})["catch"](b)}d();var g=new Error("No available storage method found.");return f._driverSet=ia.reject(g),f._driverSet}var c=0;return b()}}var f=this;La(a)||(a=[a]);var g=this._getSupportedDrivers(a),h=null!==this._driverSet?this._driverSet["catch"](function(){return ia.resolve()}):ia.resolve();return this._driverSet=h.then(function(){var a=g[0];return f._dbInfo=null,f._ready=null,f.getDriver(a).then(function(a){f._driver=a._driver,d(),f._wrapLibraryMethodsWithReady(),f._initDriver=e(g)})})["catch"](function(){d();var a=new Error("No available storage method found.");return f._driverSet=ia.reject(a),f._driverSet}),aa(this._driverSet,b,c),this._driverSet},a.prototype.supports=function(a){return!!Ka[a]},a.prototype._extend=function(a){ca(this,a)},a.prototype._getSupportedDrivers=function(a){for(var b=[],c=0,d=a.length;d>c;c++){var e=a[c];this.supports(e)&&b.push(e)}return b},a.prototype._wrapLibraryMethodsWithReady=function(){for(var a=0;a<Ia.length;a++)ba(this,Ia[a])},a.prototype.createInstance=function(b){return new a(b)},a}(),Na=new Ma;b.exports=Na},{3:3}]},{},[4])(4)}); |
/*! | ||
localForage -- Offline Storage, Improved | ||
Version 1.4.0 | ||
Version 1.4.1 | ||
https://mozilla.github.io/localForage | ||
(c) 2013-2015 Mozilla, Apache License 2.0 | ||
*/ | ||
!function(a,b){"object"==typeof exports&&"object"==typeof module?module.exports=b():"function"==typeof define&&define.amd?define([],b):"object"==typeof exports?exports.localforage=b():a.localforage=b()}(this,function(){return function(a){function b(d){if(c[d])return c[d].exports;var e=c[d]={exports:{},id:d,loaded:!1};return a[d].call(e.exports,e,e.exports,b),e.loaded=!0,e.exports}var c={};return b.m=a,b.c=c,b.p="",b(0)}([function(a,b,c){"use strict";function d(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}b.__esModule=!0;var e=function(a){function b(a,b){a[b]=function(){var c=arguments;return a.ready().then(function(){return a[b].apply(a,c)})}}function e(){for(var a=1;a<arguments.length;a++){var b=arguments[a];if(b)for(var c in b)b.hasOwnProperty(c)&&(m(b[c])?arguments[0][c]=b[c].slice():arguments[0][c]=b[c])}return arguments[0]}function f(a){for(var b in h)if(h.hasOwnProperty(b)&&h[b]===a)return!0;return!1}var g={},h={INDEXEDDB:"asyncStorage",LOCALSTORAGE:"localStorageWrapper",WEBSQL:"webSQLStorage"},i=[h.INDEXEDDB,h.WEBSQL,h.LOCALSTORAGE],j=["clear","getItem","iterate","key","keys","length","removeItem","setItem"],k={description:"",driver:i.slice(),name:"localforage",size:4980736,storeName:"keyvaluepairs",version:1},l=function(a){var b={};return b[h.INDEXEDDB]=!!function(){try{var b=b||a.indexedDB||a.webkitIndexedDB||a.mozIndexedDB||a.OIndexedDB||a.msIndexedDB;return"undefined"!=typeof a.openDatabase&&a.navigator&&a.navigator.userAgent&&/Safari/.test(a.navigator.userAgent)&&!/Chrome/.test(a.navigator.userAgent)?!1:b&&"function"==typeof b.open&&"undefined"!=typeof a.IDBKeyRange}catch(c){return!1}}(),b[h.WEBSQL]=!!function(){try{return a.openDatabase}catch(b){return!1}}(),b[h.LOCALSTORAGE]=!!function(){try{return a.localStorage&&"setItem"in a.localStorage&&a.localStorage.setItem}catch(b){return!1}}(),b}(a),m=Array.isArray||function(a){return"[object Array]"===Object.prototype.toString.call(a)},n=function(){function a(b){d(this,a),this.INDEXEDDB=h.INDEXEDDB,this.LOCALSTORAGE=h.LOCALSTORAGE,this.WEBSQL=h.WEBSQL,this._defaultConfig=e({},k),this._config=e({},this._defaultConfig,b),this._driverSet=null,this._initDriver=null,this._ready=!1,this._dbInfo=null,this._wrapLibraryMethodsWithReady(),this.setDriver(this._config.driver)}return a.prototype.config=function(a){if("object"==typeof a){if(this._ready)return new Error("Can't call config() after localforage has been used.");for(var b in a)"storeName"===b&&(a[b]=a[b].replace(/\W/g,"_")),this._config[b]=a[b];return"driver"in a&&a.driver&&this.setDriver(this._config.driver),!0}return"string"==typeof a?this._config[a]:this._config},a.prototype.defineDriver=function(a,b,c){var d=new Promise(function(b,c){try{var d=a._driver,e=new Error("Custom driver not compliant; see https://mozilla.github.io/localForage/#definedriver"),h=new Error("Custom driver name already in use: "+a._driver);if(!a._driver)return void c(e);if(f(a._driver))return void c(h);for(var i=j.concat("_initStorage"),k=0;k<i.length;k++){var m=i[k];if(!m||!a[m]||"function"!=typeof a[m])return void c(e)}var n=Promise.resolve(!0);"_support"in a&&(n=a._support&&"function"==typeof a._support?a._support():Promise.resolve(!!a._support)),n.then(function(c){l[d]=c,g[d]=a,b()},c)}catch(o){c(o)}});return d.then(b,c),d},a.prototype.driver=function(){return this._driver||null},a.prototype.getDriver=function(a,b,d){var e=this,h=function(){if(f(a))switch(a){case e.INDEXEDDB:return new Promise(function(a,b){a(c(1))});case e.LOCALSTORAGE:return new Promise(function(a,b){a(c(2))});case e.WEBSQL:return new Promise(function(a,b){a(c(4))})}else if(g[a])return Promise.resolve(g[a]);return Promise.reject(new Error("Driver not found."))}();return h.then(b,d),h},a.prototype.getSerializer=function(a){var b=new Promise(function(a,b){a(c(3))});return a&&"function"==typeof a&&b.then(function(b){a(b)}),b},a.prototype.ready=function(a){var b=this,c=b._driverSet.then(function(){return null===b._ready&&(b._ready=b._initDriver()),b._ready});return c.then(a,a),c},a.prototype.setDriver=function(a,b,c){function d(){f._config.driver=f.driver()}function e(a){return function(){function b(){for(;c<a.length;){var e=a[c];return c++,f._dbInfo=null,f._ready=null,f.getDriver(e).then(function(a){return f._extend(a),d(),f._ready=f._initStorage(f._config),f._ready})["catch"](b)}d();var g=new Error("No available storage method found.");return f._driverSet=Promise.reject(g),f._driverSet}var c=0;return b()}}var f=this;m(a)||(a=[a]);var g=this._getSupportedDrivers(a),h=null!==this._driverSet?this._driverSet["catch"](function(){return Promise.resolve()}):Promise.resolve();return this._driverSet=h.then(function(){var a=g[0];return f._dbInfo=null,f._ready=null,f.getDriver(a).then(function(a){f._driver=a._driver,d(),f._wrapLibraryMethodsWithReady(),f._initDriver=e(g)})})["catch"](function(){d();var a=new Error("No available storage method found.");return f._driverSet=Promise.reject(a),f._driverSet}),this._driverSet.then(b,c),this._driverSet},a.prototype.supports=function(a){return!!l[a]},a.prototype._extend=function(a){e(this,a)},a.prototype._getSupportedDrivers=function(a){for(var b=[],c=0,d=a.length;d>c;c++){var e=a[c];this.supports(e)&&b.push(e)}return b},a.prototype._wrapLibraryMethodsWithReady=function(){for(var a=0;a<j.length;a++)b(this,j[a])},a.prototype.createInstance=function(b){return new a(b)},a}();return new n}("undefined"!=typeof window?window:self);b["default"]=e,a.exports=b["default"]},function(a,b){"use strict";b.__esModule=!0;var c=function(a){function b(b,c){b=b||[],c=c||{};try{return new Blob(b,c)}catch(d){if("TypeError"!==d.name)throw d;for(var e=a.BlobBuilder||a.MSBlobBuilder||a.MozBlobBuilder||a.WebKitBlobBuilder,f=new e,g=0;g<b.length;g+=1)f.append(b[g]);return f.getBlob(c.type)}}function c(a){for(var b=a.length,c=new ArrayBuffer(b),d=new Uint8Array(c),e=0;b>e;e++)d[e]=a.charCodeAt(e);return c}function d(a){return new Promise(function(b,c){var d=new XMLHttpRequest;d.open("GET",a),d.withCredentials=!0,d.responseType="arraybuffer",d.onreadystatechange=function(){return 4===d.readyState?200===d.status?b({response:d.response,type:d.getResponseHeader("Content-Type")}):void c({status:d.status,response:d.response}):void 0},d.send()})}function e(a){return new Promise(function(c,e){var f=b([""],{type:"image/png"}),g=a.transaction([D],"readwrite");g.objectStore(D).put(f,"key"),g.oncomplete=function(){var b=a.transaction([D],"readwrite"),f=b.objectStore(D).get("key");f.onerror=e,f.onsuccess=function(a){var b=a.target.result,e=URL.createObjectURL(b);d(e).then(function(a){c(!(!a||"image/png"!==a.type))},function(){c(!1)}).then(function(){URL.revokeObjectURL(e)})}},g.onerror=g.onabort=e})["catch"](function(){return!1})}function f(a){return"boolean"==typeof B?Promise.resolve(B):e(a).then(function(a){return B=a})}function g(a){return new Promise(function(b,c){var d=new FileReader;d.onerror=c,d.onloadend=function(c){var d=btoa(c.target.result||"");b({__local_forage_encoded_blob:!0,data:d,type:a.type})},d.readAsBinaryString(a)})}function h(a){var d=c(atob(a.data));return b([d],{type:a.type})}function i(a){return a&&a.__local_forage_encoded_blob}function j(a){var b=this,c=b._initReady().then(function(){var a=C[b._dbInfo.name];return a&&a.dbReady?a.dbReady:void 0});return c.then(a,a),c}function k(a){var b=C[a.name],c={};c.promise=new Promise(function(a){c.resolve=a}),b.deferredOperations.push(c),b.dbReady?b.dbReady=b.dbReady.then(function(){return c.promise}):b.dbReady=c.promise}function l(a){var b=C[a.name],c=b.deferredOperations.pop();c&&c.resolve()}function m(a){function b(){return Promise.resolve()}var c=this,d={db:null};if(a)for(var e in a)d[e]=a[e];C||(C={});var f=C[d.name];f||(f={forages:[],db:null,dbReady:null,deferredOperations:[]},C[d.name]=f),f.forages.push(c),c._initReady||(c._initReady=c.ready,c.ready=j);for(var g=[],h=0;h<f.forages.length;h++){var i=f.forages[h];i!==c&&g.push(i._initReady()["catch"](b))}var k=f.forages.slice(0);return Promise.all(g).then(function(){return d.db=f.db,n(d)}).then(function(a){return d.db=a,q(d,c._defaultConfig.version)?o(d):a}).then(function(a){d.db=f.db=a,c._dbInfo=d;for(var b=0;b<k.length;b++){var e=k[b];e!==c&&(e._dbInfo.db=d.db,e._dbInfo.version=d.version)}})}function n(a){return p(a,!1)}function o(a){return p(a,!0)}function p(b,c){return new Promise(function(d,e){if(b.db){if(!c)return d(b.db);k(b),b.db.close()}var f=[b.name];c&&f.push(b.version);var g=A.open.apply(A,f);c&&(g.onupgradeneeded=function(c){var d=g.result;try{d.createObjectStore(b.storeName),c.oldVersion<=1&&d.createObjectStore(D)}catch(e){if("ConstraintError"!==e.name)throw e;a.console.warn('The database "'+b.name+'" has been upgraded from version '+c.oldVersion+" to version "+c.newVersion+', but the storage "'+b.storeName+'" already exists.')}}),g.onerror=function(){e(g.error)},g.onsuccess=function(){d(g.result),l(b)}})}function q(b,c){if(!b.db)return!0;var d=!b.db.objectStoreNames.contains(b.storeName),e=b.version<b.db.version,f=b.version>b.db.version;if(e&&(b.version!==c&&a.console.warn('The database "'+b.name+"\" can't be downgraded from version "+b.db.version+" to version "+b.version+"."),b.version=b.db.version),f||d){if(d){var g=b.db.version+1;g>b.version&&(b.version=g)}return!0}return!1}function r(b,c){var d=this;"string"!=typeof b&&(a.console.warn(b+" used as a key, but it is not a string."),b=String(b));var e=new Promise(function(a,c){d.ready().then(function(){var e=d._dbInfo,f=e.db.transaction(e.storeName,"readonly").objectStore(e.storeName),g=f.get(b);g.onsuccess=function(){var b=g.result;void 0===b&&(b=null),i(b)&&(b=h(b)),a(b)},g.onerror=function(){c(g.error)}})["catch"](c)});return z(e,c),e}function s(a,b){var c=this,d=new Promise(function(b,d){c.ready().then(function(){var e=c._dbInfo,f=e.db.transaction(e.storeName,"readonly").objectStore(e.storeName),g=f.openCursor(),j=1;g.onsuccess=function(){var c=g.result;if(c){var d=c.value;i(d)&&(d=h(d));var e=a(d,c.key,j++);void 0!==e?b(e):c["continue"]()}else b()},g.onerror=function(){d(g.error)}})["catch"](d)});return z(d,b),d}function t(b,c,d){var e=this;"string"!=typeof b&&(a.console.warn(b+" used as a key, but it is not a string."),b=String(b));var h=new Promise(function(a,d){var h;e.ready().then(function(){return h=e._dbInfo,c instanceof Blob?f(h.db).then(function(a){return a?c:g(c)}):c}).then(function(c){var e=h.db.transaction(h.storeName,"readwrite"),f=e.objectStore(h.storeName);null===c&&(c=void 0),e.oncomplete=function(){void 0===c&&(c=null),a(c)},e.onabort=e.onerror=function(){var a=g.error?g.error:g.transaction.error;d(a)};var g=f.put(c,b)})["catch"](d)});return z(h,d),h}function u(b,c){var d=this;"string"!=typeof b&&(a.console.warn(b+" used as a key, but it is not a string."),b=String(b));var e=new Promise(function(a,c){d.ready().then(function(){var e=d._dbInfo,f=e.db.transaction(e.storeName,"readwrite"),g=f.objectStore(e.storeName),h=g["delete"](b);f.oncomplete=function(){a()},f.onerror=function(){c(h.error)},f.onabort=function(){var a=h.error?h.error:h.transaction.error;c(a)}})["catch"](c)});return z(e,c),e}function v(a){var b=this,c=new Promise(function(a,c){b.ready().then(function(){var d=b._dbInfo,e=d.db.transaction(d.storeName,"readwrite"),f=e.objectStore(d.storeName),g=f.clear();e.oncomplete=function(){a()},e.onabort=e.onerror=function(){var a=g.error?g.error:g.transaction.error;c(a)}})["catch"](c)});return z(c,a),c}function w(a){var b=this,c=new Promise(function(a,c){b.ready().then(function(){var d=b._dbInfo,e=d.db.transaction(d.storeName,"readonly").objectStore(d.storeName),f=e.count();f.onsuccess=function(){a(f.result)},f.onerror=function(){c(f.error)}})["catch"](c)});return z(c,a),c}function x(a,b){var c=this,d=new Promise(function(b,d){return 0>a?void b(null):void c.ready().then(function(){var e=c._dbInfo,f=e.db.transaction(e.storeName,"readonly").objectStore(e.storeName),g=!1,h=f.openCursor();h.onsuccess=function(){var c=h.result;return c?void(0===a?b(c.key):g?b(c.key):(g=!0,c.advance(a))):void b(null)},h.onerror=function(){d(h.error)}})["catch"](d)});return z(d,b),d}function y(a){var b=this,c=new Promise(function(a,c){b.ready().then(function(){var d=b._dbInfo,e=d.db.transaction(d.storeName,"readonly").objectStore(d.storeName),f=e.openCursor(),g=[];f.onsuccess=function(){var b=f.result;return b?(g.push(b.key),void b["continue"]()):void a(g)},f.onerror=function(){c(f.error)}})["catch"](c)});return z(c,a),c}function z(a,b){b&&a.then(function(a){b(null,a)},function(a){b(a)})}var A=A||a.indexedDB||a.webkitIndexedDB||a.mozIndexedDB||a.OIndexedDB||a.msIndexedDB;if(A){var B,C,D="local-forage-detect-blob-support",E={_driver:"asyncStorage",_initStorage:m,iterate:s,getItem:r,setItem:t,removeItem:u,clear:v,length:w,key:x,keys:y};return E}}("undefined"!=typeof window?window:self);b["default"]=c,a.exports=b["default"]},function(a,b,c){"use strict";b.__esModule=!0;var d=function(a){function b(a){var b=this,d={};if(a)for(var e in a)d[e]=a[e];return d.keyPrefix=d.name+"/",d.storeName!==b._defaultConfig.storeName&&(d.keyPrefix+=d.storeName+"/"),b._dbInfo=d,new Promise(function(a,b){a(c(3))}).then(function(a){return d.serializer=a,Promise.resolve()})}function d(a){var b=this,c=b.ready().then(function(){for(var a=b._dbInfo.keyPrefix,c=m.length-1;c>=0;c--){var d=m.key(c);0===d.indexOf(a)&&m.removeItem(d)}});return l(c,a),c}function e(b,c){var d=this;"string"!=typeof b&&(a.console.warn(b+" used as a key, but it is not a string."),b=String(b));var e=d.ready().then(function(){var a=d._dbInfo,c=m.getItem(a.keyPrefix+b);return c&&(c=a.serializer.deserialize(c)),c});return l(e,c),e}function f(a,b){var c=this,d=c.ready().then(function(){for(var b=c._dbInfo,d=b.keyPrefix,e=d.length,f=m.length,g=1,h=0;f>h;h++){var i=m.key(h);if(0===i.indexOf(d)){var j=m.getItem(i);if(j&&(j=b.serializer.deserialize(j)),j=a(j,i.substring(e),g++),void 0!==j)return j}}});return l(d,b),d}function g(a,b){var c=this,d=c.ready().then(function(){var b,d=c._dbInfo;try{b=m.key(a)}catch(e){b=null}return b&&(b=b.substring(d.keyPrefix.length)),b});return l(d,b),d}function h(a){var b=this,c=b.ready().then(function(){for(var a=b._dbInfo,c=m.length,d=[],e=0;c>e;e++)0===m.key(e).indexOf(a.keyPrefix)&&d.push(m.key(e).substring(a.keyPrefix.length));return d});return l(c,a),c}function i(a){var b=this,c=b.keys().then(function(a){return a.length});return l(c,a),c}function j(b,c){var d=this;"string"!=typeof b&&(a.console.warn(b+" used as a key, but it is not a string."),b=String(b));var e=d.ready().then(function(){var a=d._dbInfo;m.removeItem(a.keyPrefix+b)});return l(e,c),e}function k(b,c,d){var e=this;"string"!=typeof b&&(a.console.warn(b+" used as a key, but it is not a string."),b=String(b));var f=e.ready().then(function(){void 0===c&&(c=null);var a=c;return new Promise(function(d,f){var g=e._dbInfo;g.serializer.serialize(c,function(c,e){if(e)f(e);else try{m.setItem(g.keyPrefix+b,c),d(a)}catch(h){("QuotaExceededError"===h.name||"NS_ERROR_DOM_QUOTA_REACHED"===h.name)&&f(h),f(h)}})})});return l(f,d),f}function l(a,b){b&&a.then(function(a){b(null,a)},function(a){b(a)})}var m=null;try{if(!(a.localStorage&&"setItem"in a.localStorage))return;m=a.localStorage}catch(n){return}var o={_driver:"localStorageWrapper",_initStorage:b,iterate:f,getItem:e,setItem:k,removeItem:j,clear:d,length:i,key:g,keys:h};return o}("undefined"!=typeof window?window:self);b["default"]=d,a.exports=b["default"]},function(a,b){"use strict";b.__esModule=!0;var c=function(a){function b(b,c){b=b||[],c=c||{};try{return new Blob(b,c)}catch(d){if("TypeError"!==d.name)throw d;for(var e=a.BlobBuilder||a.MSBlobBuilder||a.MozBlobBuilder||a.WebKitBlobBuilder,f=new e,g=0;g<b.length;g+=1)f.append(b[g]);return f.getBlob(c.type)}}function c(a,b){var c="";if(a&&(c=a.toString()),a&&("[object ArrayBuffer]"===a.toString()||a.buffer&&"[object ArrayBuffer]"===a.buffer.toString())){var d,e=j;a instanceof ArrayBuffer?(d=a,e+=l):(d=a.buffer,"[object Int8Array]"===c?e+=n:"[object Uint8Array]"===c?e+=o:"[object Uint8ClampedArray]"===c?e+=p:"[object Int16Array]"===c?e+=q:"[object Uint16Array]"===c?e+=s:"[object Int32Array]"===c?e+=r:"[object Uint32Array]"===c?e+=t:"[object Float32Array]"===c?e+=u:"[object Float64Array]"===c?e+=v:b(new Error("Failed to get type for BinaryArray"))),b(e+f(d))}else if("[object Blob]"===c){var g=new FileReader;g.onload=function(){var c=h+a.type+"~"+f(this.result);b(j+m+c)},g.readAsArrayBuffer(a)}else try{b(JSON.stringify(a))}catch(i){console.error("Couldn't convert value into a JSON string: ",a),b(null,i)}}function d(a){if(a.substring(0,k)!==j)return JSON.parse(a);var c,d=a.substring(w),f=a.substring(k,w);if(f===m&&i.test(d)){var g=d.match(i);c=g[1],d=d.substring(g[0].length)}var h=e(d);switch(f){case l:return h;case m:return b([h],{type:c});case n:return new Int8Array(h);case o:return new Uint8Array(h);case p:return new Uint8ClampedArray(h);case q:return new Int16Array(h);case s:return new Uint16Array(h);case r:return new Int32Array(h);case t:return new Uint32Array(h);case u:return new Float32Array(h);case v:return new Float64Array(h);default:throw new Error("Unkown type: "+f)}}function e(a){var b,c,d,e,f,h=.75*a.length,i=a.length,j=0;"="===a[a.length-1]&&(h--,"="===a[a.length-2]&&h--);var k=new ArrayBuffer(h),l=new Uint8Array(k);for(b=0;i>b;b+=4)c=g.indexOf(a[b]),d=g.indexOf(a[b+1]),e=g.indexOf(a[b+2]),f=g.indexOf(a[b+3]),l[j++]=c<<2|d>>4,l[j++]=(15&d)<<4|e>>2,l[j++]=(3&e)<<6|63&f;return k}function f(a){var b,c=new Uint8Array(a),d="";for(b=0;b<c.length;b+=3)d+=g[c[b]>>2],d+=g[(3&c[b])<<4|c[b+1]>>4],d+=g[(15&c[b+1])<<2|c[b+2]>>6],d+=g[63&c[b+2]];return c.length%3===2?d=d.substring(0,d.length-1)+"=":c.length%3===1&&(d=d.substring(0,d.length-2)+"=="),d}var g="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",h="~~local_forage_type~",i=/^~~local_forage_type~([^~]+)~/,j="__lfsc__:",k=j.length,l="arbf",m="blob",n="si08",o="ui08",p="uic8",q="si16",r="si32",s="ur16",t="ui32",u="fl32",v="fl64",w=k+l.length,x={serialize:c,deserialize:d,stringToBuffer:e,bufferToString:f};return x}("undefined"!=typeof window?window:self);b["default"]=c,a.exports=b["default"]},function(a,b,c){"use strict";b.__esModule=!0;var d=function(a){function b(a){var b=this,d={db:null};if(a)for(var e in a)d[e]="string"!=typeof a[e]?a[e].toString():a[e];var f=new Promise(function(a,c){try{d.db=m(d.name,String(d.version),d.description,d.size)}catch(e){return c(e)}d.db.transaction(function(e){e.executeSql("CREATE TABLE IF NOT EXISTS "+d.storeName+" (id INTEGER PRIMARY KEY, key unique, value)",[],function(){b._dbInfo=d,a()},function(a,b){c(b)})})});return new Promise(function(a,b){a(c(3))}).then(function(a){return d.serializer=a,f})}function d(b,c){var d=this;"string"!=typeof b&&(a.console.warn(b+" used as a key, but it is not a string."),b=String(b));var e=new Promise(function(a,c){d.ready().then(function(){var e=d._dbInfo;e.db.transaction(function(d){d.executeSql("SELECT * FROM "+e.storeName+" WHERE key = ? LIMIT 1",[b],function(b,c){var d=c.rows.length?c.rows.item(0).value:null;d&&(d=e.serializer.deserialize(d)),a(d)},function(a,b){c(b)})})})["catch"](c)});return l(e,c),e}function e(a,b){var c=this,d=new Promise(function(b,d){c.ready().then(function(){var e=c._dbInfo;e.db.transaction(function(c){c.executeSql("SELECT * FROM "+e.storeName,[],function(c,d){for(var f=d.rows,g=f.length,h=0;g>h;h++){var i=f.item(h),j=i.value;if(j&&(j=e.serializer.deserialize(j)),j=a(j,i.key,h+1),void 0!==j)return void b(j)}b()},function(a,b){d(b)})})})["catch"](d)});return l(d,b),d}function f(b,c,d){var e=this;"string"!=typeof b&&(a.console.warn(b+" used as a key, but it is not a string."),b=String(b));var f=new Promise(function(a,d){e.ready().then(function(){void 0===c&&(c=null);var f=c,g=e._dbInfo;g.serializer.serialize(c,function(c,e){e?d(e):g.db.transaction(function(e){e.executeSql("INSERT OR REPLACE INTO "+g.storeName+" (key, value) VALUES (?, ?)",[b,c],function(){a(f)},function(a,b){d(b)})},function(a){a.code===a.QUOTA_ERR&&d(a)})})})["catch"](d)});return l(f,d),f}function g(b,c){var d=this;"string"!=typeof b&&(a.console.warn(b+" used as a key, but it is not a string."),b=String(b));var e=new Promise(function(a,c){d.ready().then(function(){var e=d._dbInfo;e.db.transaction(function(d){d.executeSql("DELETE FROM "+e.storeName+" WHERE key = ?",[b],function(){a()},function(a,b){c(b)})})})["catch"](c)});return l(e,c),e}function h(a){var b=this,c=new Promise(function(a,c){b.ready().then(function(){var d=b._dbInfo;d.db.transaction(function(b){b.executeSql("DELETE FROM "+d.storeName,[],function(){a()},function(a,b){c(b)})})})["catch"](c)});return l(c,a),c}function i(a){var b=this,c=new Promise(function(a,c){b.ready().then(function(){var d=b._dbInfo;d.db.transaction(function(b){b.executeSql("SELECT COUNT(key) as c FROM "+d.storeName,[],function(b,c){var d=c.rows.item(0).c;a(d)},function(a,b){c(b)})})})["catch"](c)});return l(c,a),c}function j(a,b){var c=this,d=new Promise(function(b,d){c.ready().then(function(){var e=c._dbInfo;e.db.transaction(function(c){c.executeSql("SELECT key FROM "+e.storeName+" WHERE id = ? LIMIT 1",[a+1],function(a,c){var d=c.rows.length?c.rows.item(0).key:null;b(d)},function(a,b){d(b)})})})["catch"](d)});return l(d,b),d}function k(a){var b=this,c=new Promise(function(a,c){b.ready().then(function(){var d=b._dbInfo;d.db.transaction(function(b){b.executeSql("SELECT key FROM "+d.storeName,[],function(b,c){for(var d=[],e=0;e<c.rows.length;e++)d.push(c.rows.item(e).key);a(d)},function(a,b){c(b)})})})["catch"](c)});return l(c,a),c}function l(a,b){b&&a.then(function(a){b(null,a)},function(a){b(a)})}var m=a.openDatabase;if(m){var n={_driver:"webSQLStorage",_initStorage:b,iterate:e,getItem:d,setItem:f,removeItem:g,clear:h,length:i,key:j,keys:k};return n}}("undefined"!=typeof window?window:self);b["default"]=d,a.exports=b["default"]}])}); | ||
!function(a){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;b="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,b.localforage=a()}}(function(){return function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);var j=new Error("Cannot find module '"+g+"'");throw j.code="MODULE_NOT_FOUND",j}var k=c[g]={exports:{}};b[g][0].call(k.exports,function(a){var c=b[g][1][a];return e(c?c:a)},k,k.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({1:[function(a,b,c){"use strict";function d(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function e(){return"undefined"!=typeof indexedDB?indexedDB:"undefined"!=typeof webkitIndexedDB?webkitIndexedDB:"undefined"!=typeof mozIndexedDB?mozIndexedDB:"undefined"!=typeof OIndexedDB?OIndexedDB:"undefined"!=typeof msIndexedDB?msIndexedDB:void 0}function f(){try{return fa?"undefined"!=typeof openDatabase&&"undefined"!=typeof navigator&&navigator.userAgent&&/Safari/.test(navigator.userAgent)&&!/Chrome/.test(navigator.userAgent)?!1:fa&&"function"==typeof fa.open&&"undefined"!=typeof IDBKeyRange:!1}catch(a){return!1}}function g(){return"function"==typeof openDatabase}function h(){try{return"undefined"!=typeof localStorage&&"setItem"in localStorage&&localStorage.setItem}catch(a){return!1}}function i(a,b){a=a||[],b=b||{};try{return new Blob(a,b)}catch(c){if("TypeError"!==c.name)throw c;for(var d="undefined"!=typeof BlobBuilder?BlobBuilder:"undefined"!=typeof MSBlobBuilder?MSBlobBuilder:"undefined"!=typeof MozBlobBuilder?MozBlobBuilder:WebKitBlobBuilder,e=new d,f=0;f<a.length;f+=1)e.append(a[f]);return e.getBlob(b.type)}}function j(a,b){b&&a.then(function(a){b(null,a)},function(a){b(a)})}function k(a){for(var b=a.length,c=new ArrayBuffer(b),d=new Uint8Array(c),e=0;b>e;e++)d[e]=a.charCodeAt(e);return c}function l(a){return new ia(function(b){var c=i([""]);a.objectStore(ja).put(c,"key"),a.onabort=function(a){a.preventDefault(),a.stopPropagation(),b(!1)},a.oncomplete=function(){var a=navigator.userAgent.match(/Chrome\/(\d+)/),c=navigator.userAgent.match(/Edge\//);b(c||!a||parseInt(a[1],10)>=43)}})["catch"](function(){return!1})}function m(a){return"boolean"==typeof ga?ia.resolve(ga):l(a).then(function(a){return ga=a})}function n(a){var b=ha[a.name],c={};c.promise=new ia(function(a){c.resolve=a}),b.deferredOperations.push(c),b.dbReady?b.dbReady=b.dbReady.then(function(){return c.promise}):b.dbReady=c.promise}function o(a){var b=ha[a.name],c=b.deferredOperations.pop();c&&c.resolve()}function p(a,b){return new ia(function(c,d){if(a.db){if(!b)return c(a.db);n(a),a.db.close()}var e=[a.name];b&&e.push(a.version);var f=fa.open.apply(fa,e);b&&(f.onupgradeneeded=function(b){var c=f.result;try{c.createObjectStore(a.storeName),b.oldVersion<=1&&c.createObjectStore(ja)}catch(d){if("ConstraintError"!==d.name)throw d;console.warn('The database "'+a.name+'" has been upgraded from version '+b.oldVersion+" to version "+b.newVersion+', but the storage "'+a.storeName+'" already exists.')}}),f.onerror=function(){d(f.error)},f.onsuccess=function(){c(f.result),o(a)}})}function q(a){return p(a,!1)}function r(a){return p(a,!0)}function s(a,b){if(!a.db)return!0;var c=!a.db.objectStoreNames.contains(a.storeName),d=a.version<a.db.version,e=a.version>a.db.version;if(d&&(a.version!==b&&console.warn('The database "'+a.name+"\" can't be downgraded from version "+a.db.version+" to version "+a.version+"."),a.version=a.db.version),e||c){if(c){var f=a.db.version+1;f>a.version&&(a.version=f)}return!0}return!1}function t(a){return new ia(function(b,c){var d=new FileReader;d.onerror=c,d.onloadend=function(c){var d=btoa(c.target.result||"");b({__local_forage_encoded_blob:!0,data:d,type:a.type})},d.readAsBinaryString(a)})}function u(a){var b=k(atob(a.data));return i([b],{type:a.type})}function v(a){return a&&a.__local_forage_encoded_blob}function w(a){var b=this,c=b._initReady().then(function(){var a=ha[b._dbInfo.name];return a&&a.dbReady?a.dbReady:void 0});return c.then(a,a),c}function x(a){function b(){return ia.resolve()}var c=this,d={db:null};if(a)for(var e in a)d[e]=a[e];ha||(ha={});var f=ha[d.name];f||(f={forages:[],db:null,dbReady:null,deferredOperations:[]},ha[d.name]=f),f.forages.push(c),c._initReady||(c._initReady=c.ready,c.ready=w);for(var g=[],h=0;h<f.forages.length;h++){var i=f.forages[h];i!==c&&g.push(i._initReady()["catch"](b))}var j=f.forages.slice(0);return ia.all(g).then(function(){return d.db=f.db,q(d)}).then(function(a){return d.db=a,s(d,c._defaultConfig.version)?r(d):a}).then(function(a){d.db=f.db=a,c._dbInfo=d;for(var b=0;b<j.length;b++){var e=j[b];e!==c&&(e._dbInfo.db=d.db,e._dbInfo.version=d.version)}})}function y(a,b){var c=this;"string"!=typeof a&&(console.warn(a+" used as a key, but it is not a string."),a=String(a));var d=new ia(function(b,d){c.ready().then(function(){var e=c._dbInfo,f=e.db.transaction(e.storeName,"readonly").objectStore(e.storeName),g=f.get(a);g.onsuccess=function(){var a=g.result;void 0===a&&(a=null),v(a)&&(a=u(a)),b(a)},g.onerror=function(){d(g.error)}})["catch"](d)});return j(d,b),d}function z(a,b){var c=this,d=new ia(function(b,d){c.ready().then(function(){var e=c._dbInfo,f=e.db.transaction(e.storeName,"readonly").objectStore(e.storeName),g=f.openCursor(),h=1;g.onsuccess=function(){var c=g.result;if(c){var d=c.value;v(d)&&(d=u(d));var e=a(d,c.key,h++);void 0!==e?b(e):c["continue"]()}else b()},g.onerror=function(){d(g.error)}})["catch"](d)});return j(d,b),d}function A(a,b,c){var d=this;"string"!=typeof a&&(console.warn(a+" used as a key, but it is not a string."),a=String(a));var e=new ia(function(c,e){var f;d.ready().then(function(){return f=d._dbInfo,b instanceof Blob?m(f.db).then(function(a){return a?b:t(b)}):b}).then(function(b){var d=f.db.transaction(f.storeName,"readwrite"),g=d.objectStore(f.storeName);null===b&&(b=void 0),d.oncomplete=function(){void 0===b&&(b=null),c(b)},d.onabort=d.onerror=function(){var a=h.error?h.error:h.transaction.error;e(a)};var h=g.put(b,a)})["catch"](e)});return j(e,c),e}function B(a,b){var c=this;"string"!=typeof a&&(console.warn(a+" used as a key, but it is not a string."),a=String(a));var d=new ia(function(b,d){c.ready().then(function(){var e=c._dbInfo,f=e.db.transaction(e.storeName,"readwrite"),g=f.objectStore(e.storeName),h=g["delete"](a);f.oncomplete=function(){b()},f.onerror=function(){d(h.error)},f.onabort=function(){var a=h.error?h.error:h.transaction.error;d(a)}})["catch"](d)});return j(d,b),d}function C(a){var b=this,c=new ia(function(a,c){b.ready().then(function(){var d=b._dbInfo,e=d.db.transaction(d.storeName,"readwrite"),f=e.objectStore(d.storeName),g=f.clear();e.oncomplete=function(){a()},e.onabort=e.onerror=function(){var a=g.error?g.error:g.transaction.error;c(a)}})["catch"](c)});return j(c,a),c}function D(a){var b=this,c=new ia(function(a,c){b.ready().then(function(){var d=b._dbInfo,e=d.db.transaction(d.storeName,"readonly").objectStore(d.storeName),f=e.count();f.onsuccess=function(){a(f.result)},f.onerror=function(){c(f.error)}})["catch"](c)});return j(c,a),c}function E(a,b){var c=this,d=new ia(function(b,d){return 0>a?void b(null):void c.ready().then(function(){var e=c._dbInfo,f=e.db.transaction(e.storeName,"readonly").objectStore(e.storeName),g=!1,h=f.openCursor();h.onsuccess=function(){var c=h.result;return c?void(0===a?b(c.key):g?b(c.key):(g=!0,c.advance(a))):void b(null)},h.onerror=function(){d(h.error)}})["catch"](d)});return j(d,b),d}function F(a){var b=this,c=new ia(function(a,c){b.ready().then(function(){var d=b._dbInfo,e=d.db.transaction(d.storeName,"readonly").objectStore(d.storeName),f=e.openCursor(),g=[];f.onsuccess=function(){var b=f.result;return b?(g.push(b.key),void b["continue"]()):void a(g)},f.onerror=function(){c(f.error)}})["catch"](c)});return j(c,a),c}function G(a){var b,c,d,e,f,g=.75*a.length,h=a.length,i=0;"="===a[a.length-1]&&(g--,"="===a[a.length-2]&&g--);var j=new ArrayBuffer(g),k=new Uint8Array(j);for(b=0;h>b;b+=4)c=la.indexOf(a[b]),d=la.indexOf(a[b+1]),e=la.indexOf(a[b+2]),f=la.indexOf(a[b+3]),k[i++]=c<<2|d>>4,k[i++]=(15&d)<<4|e>>2,k[i++]=(3&e)<<6|63&f;return j}function H(a){var b,c=new Uint8Array(a),d="";for(b=0;b<c.length;b+=3)d+=la[c[b]>>2],d+=la[(3&c[b])<<4|c[b+1]>>4],d+=la[(15&c[b+1])<<2|c[b+2]>>6],d+=la[63&c[b+2]];return c.length%3===2?d=d.substring(0,d.length-1)+"=":c.length%3===1&&(d=d.substring(0,d.length-2)+"=="),d}function I(a,b){var c="";if(a&&(c=a.toString()),a&&("[object ArrayBuffer]"===a.toString()||a.buffer&&"[object ArrayBuffer]"===a.buffer.toString())){var d,e=oa;a instanceof ArrayBuffer?(d=a,e+=qa):(d=a.buffer,"[object Int8Array]"===c?e+=sa:"[object Uint8Array]"===c?e+=ta:"[object Uint8ClampedArray]"===c?e+=ua:"[object Int16Array]"===c?e+=va:"[object Uint16Array]"===c?e+=xa:"[object Int32Array]"===c?e+=wa:"[object Uint32Array]"===c?e+=ya:"[object Float32Array]"===c?e+=za:"[object Float64Array]"===c?e+=Aa:b(new Error("Failed to get type for BinaryArray"))),b(e+H(d))}else if("[object Blob]"===c){var f=new FileReader;f.onload=function(){var c=ma+a.type+"~"+H(this.result);b(oa+ra+c)},f.readAsArrayBuffer(a)}else try{b(JSON.stringify(a))}catch(g){console.error("Couldn't convert value into a JSON string: ",a),b(null,g)}}function J(a){if(a.substring(0,pa)!==oa)return JSON.parse(a);var b,c=a.substring(Ba),d=a.substring(pa,Ba);if(d===ra&&na.test(c)){var e=c.match(na);b=e[1],c=c.substring(e[0].length)}var f=G(c);switch(d){case qa:return f;case ra:return i([f],{type:b});case sa:return new Int8Array(f);case ta:return new Uint8Array(f);case ua:return new Uint8ClampedArray(f);case va:return new Int16Array(f);case xa:return new Uint16Array(f);case wa:return new Int32Array(f);case ya:return new Uint32Array(f);case za:return new Float32Array(f);case Aa:return new Float64Array(f);default:throw new Error("Unkown type: "+d)}}function K(a){var b=this,c={db:null};if(a)for(var d in a)c[d]="string"!=typeof a[d]?a[d].toString():a[d];var e=new ia(function(a,d){try{c.db=openDatabase(c.name,String(c.version),c.description,c.size)}catch(e){return d(e)}c.db.transaction(function(e){e.executeSql("CREATE TABLE IF NOT EXISTS "+c.storeName+" (id INTEGER PRIMARY KEY, key unique, value)",[],function(){b._dbInfo=c,a()},function(a,b){d(b)})})});return c.serializer=Ca,e}function L(a,b){var c=this;"string"!=typeof a&&(console.warn(a+" used as a key, but it is not a string."),a=String(a));var d=new ia(function(b,d){c.ready().then(function(){var e=c._dbInfo;e.db.transaction(function(c){c.executeSql("SELECT * FROM "+e.storeName+" WHERE key = ? LIMIT 1",[a],function(a,c){var d=c.rows.length?c.rows.item(0).value:null;d&&(d=e.serializer.deserialize(d)),b(d)},function(a,b){d(b)})})})["catch"](d)});return j(d,b),d}function M(a,b){var c=this,d=new ia(function(b,d){c.ready().then(function(){var e=c._dbInfo;e.db.transaction(function(c){c.executeSql("SELECT * FROM "+e.storeName,[],function(c,d){for(var f=d.rows,g=f.length,h=0;g>h;h++){var i=f.item(h),j=i.value;if(j&&(j=e.serializer.deserialize(j)),j=a(j,i.key,h+1),void 0!==j)return void b(j)}b()},function(a,b){d(b)})})})["catch"](d)});return j(d,b),d}function N(a,b,c){var d=this;"string"!=typeof a&&(console.warn(a+" used as a key, but it is not a string."),a=String(a));var e=new ia(function(c,e){d.ready().then(function(){void 0===b&&(b=null);var f=b,g=d._dbInfo;g.serializer.serialize(b,function(b,d){d?e(d):g.db.transaction(function(d){d.executeSql("INSERT OR REPLACE INTO "+g.storeName+" (key, value) VALUES (?, ?)",[a,b],function(){c(f)},function(a,b){e(b)})},function(a){a.code===a.QUOTA_ERR&&e(a)})})})["catch"](e)});return j(e,c),e}function O(a,b){var c=this;"string"!=typeof a&&(console.warn(a+" used as a key, but it is not a string."),a=String(a));var d=new ia(function(b,d){c.ready().then(function(){var e=c._dbInfo;e.db.transaction(function(c){c.executeSql("DELETE FROM "+e.storeName+" WHERE key = ?",[a],function(){b()},function(a,b){d(b)})})})["catch"](d)});return j(d,b),d}function P(a){var b=this,c=new ia(function(a,c){b.ready().then(function(){var d=b._dbInfo;d.db.transaction(function(b){b.executeSql("DELETE FROM "+d.storeName,[],function(){a()},function(a,b){c(b)})})})["catch"](c)});return j(c,a),c}function Q(a){var b=this,c=new ia(function(a,c){b.ready().then(function(){var d=b._dbInfo;d.db.transaction(function(b){b.executeSql("SELECT COUNT(key) as c FROM "+d.storeName,[],function(b,c){var d=c.rows.item(0).c;a(d)},function(a,b){c(b)})})})["catch"](c)});return j(c,a),c}function R(a,b){var c=this,d=new ia(function(b,d){c.ready().then(function(){var e=c._dbInfo;e.db.transaction(function(c){c.executeSql("SELECT key FROM "+e.storeName+" WHERE id = ? LIMIT 1",[a+1],function(a,c){var d=c.rows.length?c.rows.item(0).key:null;b(d)},function(a,b){d(b)})})})["catch"](d)});return j(d,b),d}function S(a){var b=this,c=new ia(function(a,c){b.ready().then(function(){var d=b._dbInfo;d.db.transaction(function(b){b.executeSql("SELECT key FROM "+d.storeName,[],function(b,c){for(var d=[],e=0;e<c.rows.length;e++)d.push(c.rows.item(e).key);a(d)},function(a,b){c(b)})})})["catch"](c)});return j(c,a),c}function T(a){var b=this,c={};if(a)for(var d in a)c[d]=a[d];return c.keyPrefix=c.name+"/",c.storeName!==b._defaultConfig.storeName&&(c.keyPrefix+=c.storeName+"/"),b._dbInfo=c,c.serializer=Ca,ia.resolve()}function U(a){var b=this,c=b.ready().then(function(){for(var a=b._dbInfo.keyPrefix,c=localStorage.length-1;c>=0;c--){var d=localStorage.key(c);0===d.indexOf(a)&&localStorage.removeItem(d)}});return j(c,a),c}function V(a,b){var c=this;"string"!=typeof a&&(console.warn(a+" used as a key, but it is not a string."),a=String(a));var d=c.ready().then(function(){var b=c._dbInfo,d=localStorage.getItem(b.keyPrefix+a);return d&&(d=b.serializer.deserialize(d)),d});return j(d,b),d}function W(a,b){var c=this,d=c.ready().then(function(){for(var b=c._dbInfo,d=b.keyPrefix,e=d.length,f=localStorage.length,g=1,h=0;f>h;h++){var i=localStorage.key(h);if(0===i.indexOf(d)){var j=localStorage.getItem(i);if(j&&(j=b.serializer.deserialize(j)),j=a(j,i.substring(e),g++),void 0!==j)return j}}});return j(d,b),d}function X(a,b){var c=this,d=c.ready().then(function(){var b,d=c._dbInfo;try{b=localStorage.key(a)}catch(e){b=null}return b&&(b=b.substring(d.keyPrefix.length)),b});return j(d,b),d}function Y(a){var b=this,c=b.ready().then(function(){for(var a=b._dbInfo,c=localStorage.length,d=[],e=0;c>e;e++)0===localStorage.key(e).indexOf(a.keyPrefix)&&d.push(localStorage.key(e).substring(a.keyPrefix.length));return d});return j(c,a),c}function Z(a){var b=this,c=b.keys().then(function(a){return a.length});return j(c,a),c}function $(a,b){var c=this;"string"!=typeof a&&(console.warn(a+" used as a key, but it is not a string."),a=String(a));var d=c.ready().then(function(){var b=c._dbInfo;localStorage.removeItem(b.keyPrefix+a)});return j(d,b),d}function _(a,b,c){var d=this;"string"!=typeof a&&(console.warn(a+" used as a key, but it is not a string."),a=String(a));var e=d.ready().then(function(){void 0===b&&(b=null);var c=b;return new ia(function(e,f){var g=d._dbInfo;g.serializer.serialize(b,function(b,d){if(d)f(d);else try{localStorage.setItem(g.keyPrefix+a,b),e(c)}catch(h){"QuotaExceededError"!==h.name&&"NS_ERROR_DOM_QUOTA_REACHED"!==h.name||f(h),f(h)}})})});return j(e,c),e}function aa(a,b,c){"function"==typeof b&&a.then(b),"function"==typeof c&&a["catch"](c)}function ba(a,b){a[b]=function(){var c=arguments;return a.ready().then(function(){return a[b].apply(a,c)})}}function ca(){for(var a=1;a<arguments.length;a++){var b=arguments[a];if(b)for(var c in b)b.hasOwnProperty(c)&&(La(b[c])?arguments[0][c]=b[c].slice():arguments[0][c]=b[c])}return arguments[0]}function da(a){for(var b in Ga)if(Ga.hasOwnProperty(b)&&Ga[b]===a)return!0;return!1}var ea="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol?"symbol":typeof a},fa=e();a("lie/polyfill");var ga,ha,ia=Promise,ja="local-forage-detect-blob-support",ka={_driver:"asyncStorage",_initStorage:x,iterate:z,getItem:y,setItem:A,removeItem:B,clear:C,length:D,key:E,keys:F},la="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",ma="~~local_forage_type~",na=/^~~local_forage_type~([^~]+)~/,oa="__lfsc__:",pa=oa.length,qa="arbf",ra="blob",sa="si08",ta="ui08",ua="uic8",va="si16",wa="si32",xa="ur16",ya="ui32",za="fl32",Aa="fl64",Ba=pa+qa.length,Ca={serialize:I,deserialize:J,stringToBuffer:G,bufferToString:H},Da={_driver:"webSQLStorage",_initStorage:K,iterate:M,getItem:L,setItem:N,removeItem:O,clear:P,length:Q,key:R,keys:S},Ea={_driver:"localStorageWrapper",_initStorage:T,iterate:W,getItem:V,setItem:_,removeItem:$,clear:U,length:Z,key:X,keys:Y},Fa={},Ga={INDEXEDDB:"asyncStorage",LOCALSTORAGE:"localStorageWrapper",WEBSQL:"webSQLStorage"},Ha=[Ga.INDEXEDDB,Ga.WEBSQL,Ga.LOCALSTORAGE],Ia=["clear","getItem","iterate","key","keys","length","removeItem","setItem"],Ja={description:"",driver:Ha.slice(),name:"localforage",size:4980736,storeName:"keyvaluepairs",version:1},Ka={};Ka[Ga.INDEXEDDB]=f(),Ka[Ga.WEBSQL]=g(),Ka[Ga.LOCALSTORAGE]=h();var La=Array.isArray||function(a){return"[object Array]"===Object.prototype.toString.call(a)},Ma=function(){function a(b){d(this,a),this.INDEXEDDB=Ga.INDEXEDDB,this.LOCALSTORAGE=Ga.LOCALSTORAGE,this.WEBSQL=Ga.WEBSQL,this._defaultConfig=ca({},Ja),this._config=ca({},this._defaultConfig,b),this._driverSet=null,this._initDriver=null,this._ready=!1,this._dbInfo=null,this._wrapLibraryMethodsWithReady(),this.setDriver(this._config.driver)}return a.prototype.config=function(a){if("object"===("undefined"==typeof a?"undefined":ea(a))){if(this._ready)return new Error("Can't call config() after localforage has been used.");for(var b in a)"storeName"===b&&(a[b]=a[b].replace(/\W/g,"_")),this._config[b]=a[b];return"driver"in a&&a.driver&&this.setDriver(this._config.driver),!0}return"string"==typeof a?this._config[a]:this._config},a.prototype.defineDriver=function(a,b,c){var d=new ia(function(b,c){try{var d=a._driver,e=new Error("Custom driver not compliant; see https://mozilla.github.io/localForage/#definedriver"),f=new Error("Custom driver name already in use: "+a._driver);if(!a._driver)return void c(e);if(da(a._driver))return void c(f);for(var g=Ia.concat("_initStorage"),h=0;h<g.length;h++){var i=g[h];if(!i||!a[i]||"function"!=typeof a[i])return void c(e)}var j=ia.resolve(!0);"_support"in a&&(j=a._support&&"function"==typeof a._support?a._support():ia.resolve(!!a._support)),j.then(function(c){Ka[d]=c,Fa[d]=a,b()},c)}catch(k){c(k)}});return aa(d,b,c),d},a.prototype.driver=function(){return this._driver||null},a.prototype.getDriver=function(a,b,c){var d=this,e=ia.resolve().then(function(){if(!da(a)){if(Fa[a])return Fa[a];throw new Error("Driver not found.")}switch(a){case d.INDEXEDDB:return ka;case d.LOCALSTORAGE:return Ea;case d.WEBSQL:return Da}});return aa(e,b,c),e},a.prototype.getSerializer=function(a){var b=ia.resolve(Ca);return aa(b,a),b},a.prototype.ready=function(a){var b=this,c=b._driverSet.then(function(){return null===b._ready&&(b._ready=b._initDriver()),b._ready});return aa(c,a,a),c},a.prototype.setDriver=function(a,b,c){function d(){f._config.driver=f.driver()}function e(a){return function(){function b(){for(;c<a.length;){var e=a[c];return c++,f._dbInfo=null,f._ready=null,f.getDriver(e).then(function(a){return f._extend(a),d(),f._ready=f._initStorage(f._config),f._ready})["catch"](b)}d();var g=new Error("No available storage method found.");return f._driverSet=ia.reject(g),f._driverSet}var c=0;return b()}}var f=this;La(a)||(a=[a]);var g=this._getSupportedDrivers(a),h=null!==this._driverSet?this._driverSet["catch"](function(){return ia.resolve()}):ia.resolve();return this._driverSet=h.then(function(){var a=g[0];return f._dbInfo=null,f._ready=null,f.getDriver(a).then(function(a){f._driver=a._driver,d(),f._wrapLibraryMethodsWithReady(),f._initDriver=e(g)})})["catch"](function(){d();var a=new Error("No available storage method found.");return f._driverSet=ia.reject(a),f._driverSet}),aa(this._driverSet,b,c),this._driverSet},a.prototype.supports=function(a){return!!Ka[a]},a.prototype._extend=function(a){ca(this,a)},a.prototype._getSupportedDrivers=function(a){for(var b=[],c=0,d=a.length;d>c;c++){var e=a[c];this.supports(e)&&b.push(e)}return b},a.prototype._wrapLibraryMethodsWithReady=function(){for(var a=0;a<Ia.length;a++)ba(this,Ia[a])},a.prototype.createInstance=function(b){return new a(b)},a}(),Na=new Ma;b.exports=Na},{undefined:void 0}]},{},[1])(1)}); |
111
Gruntfile.js
@@ -37,3 +37,4 @@ /* jshint node:true */ | ||
options: { | ||
modules: 'umd', | ||
babelrc: false, | ||
extends: path.resolve('.babelrc-umd'), | ||
moduleIds: true, | ||
@@ -56,4 +57,43 @@ getModuleId: babelModuleIdProvider | ||
dest: 'test/localforage.browserify.js' | ||
}, | ||
main: { | ||
files: { | ||
'dist/localforage.js': 'src/localforage.js' | ||
}, | ||
options: { | ||
browserifyOptions: { | ||
standalone: 'localforage' | ||
}, | ||
transform: ['rollupify', 'babelify'], | ||
plugin: ['bundle-collapser/plugin'] | ||
} | ||
}, | ||
no_promises: { | ||
files: { | ||
'dist/localforage.nopromises.js': 'src/localforage.js' | ||
}, | ||
options: { | ||
browserifyOptions: { | ||
standalone: 'localforage' | ||
}, | ||
transform: ['rollupify', 'babelify'], | ||
plugin: ['bundle-collapser/plugin'], | ||
exclude: ['lie/polyfill'] | ||
} | ||
} | ||
}, | ||
run: { | ||
derequire: { | ||
exec: 'derequire ' + | ||
'< dist/localforage.js > dist/localforage.tmp ' + | ||
'&& ncp dist/localforage.tmp dist/localforage.js' + | ||
'&& rimraf dist/localforage.tmp' | ||
}, | ||
derequire_no_promises: { | ||
exec: 'derequire ' + | ||
'< dist/localforage.nopromises.js > dist/localforage.nopromises.tmp ' + | ||
'&& ncp dist/localforage.nopromises.tmp dist/localforage.nopromises.js' + | ||
'&& rimraf dist/localforage.nopromises.tmp' | ||
} | ||
}, | ||
concat: { | ||
@@ -64,10 +104,9 @@ options: { | ||
localforage: { | ||
// just to add the BANNER | ||
// without adding an extra grunt module | ||
files: { | ||
'dist/localforage.js': [ | ||
'bower_components/es6-promise/promise.js', | ||
'dist/localforage.nopromises.js' | ||
'dist/localforage.js' | ||
], | ||
'dist/localforage.nopromises.js': [ | ||
// just to add the BANNER | ||
// without adding an extra grunt module | ||
'dist/localforage.nopromises.js' | ||
@@ -130,13 +169,11 @@ ] | ||
urls: [ | ||
'http://localhost:9999/test/test.component.html', | ||
'http://localhost:9999/test/test.nodriver.html', | ||
'http://localhost:9999/test/test.faultydriver.html', | ||
'http://localhost:9999/test/test.main.html', | ||
'http://localhost:9999/test/test.min.html', | ||
'http://localhost:9999/test/test.callwhenready.html', | ||
'http://localhost:9999/test/test.customdriver.html', | ||
'http://localhost:9999/test/test.faultydriver.html', | ||
'http://localhost:9999/test/test.nodriver.html', | ||
'http://localhost:9999/test/test.browserify.html', | ||
'http://localhost:9999/test/test.require.html', | ||
'http://localhost:9999/test/test.require.unbundled.html', | ||
'http://localhost:9999/test/test.browserify.html', | ||
'http://localhost:9999/test/test.webpack.html', | ||
'http://localhost:9999/test/test.callwhenready.html', | ||
'http://localhost:9999/test/test.customdriver.html' | ||
'http://localhost:9999/test/test.webpack.html' | ||
] | ||
@@ -146,7 +183,2 @@ } | ||
}, | ||
open: { | ||
site: { | ||
path: 'http://localhost:4567/' | ||
} | ||
}, | ||
'saucelabs-mocha': { | ||
@@ -166,18 +198,2 @@ all: { | ||
}, | ||
shell: { | ||
options: { | ||
stdout: true | ||
}, | ||
component: { | ||
command: path.resolve('node_modules', 'component', 'bin', | ||
'component-build') + | ||
' --dev -o test -n localforage.component' | ||
}, | ||
'publish-site': { | ||
command: 'rake publish ALLOW_DIRTY=true' | ||
}, | ||
'serve-site': { | ||
command: 'bundle exec middleman server' | ||
} | ||
}, | ||
uglify: { | ||
@@ -189,4 +205,3 @@ localforage: { | ||
'dist/localforage.nopromises.js' | ||
], | ||
'site/localforage.min.js': ['dist/localforage.js'] | ||
] | ||
}, | ||
@@ -213,3 +228,2 @@ options: { | ||
'jscs', | ||
'shell:component', | ||
'browserify:package_bundling_test', | ||
@@ -228,18 +242,2 @@ 'webpack:package_bundling_test', | ||
} | ||
}, | ||
localforage_nopromises: { | ||
entry: './src/localforage.js', | ||
output: { | ||
path: 'dist/', | ||
filename: 'localforage.nopromises.js', | ||
library: ['localforage'], | ||
libraryTarget: 'umd' | ||
}, | ||
module: { | ||
loaders: [{ | ||
test: /\.js?$/, | ||
exclude: /(node_modules|bower_components)/, | ||
loader: 'babel' | ||
}] | ||
} | ||
} | ||
@@ -252,6 +250,6 @@ } | ||
grunt.registerTask('default', ['build', 'connect', 'watch']); | ||
grunt.registerTask('build', ['webpack:localforage_nopromises', 'concat', 'es3_safe_recast', 'uglify']); | ||
grunt.registerTask('publish', ['build', 'shell:publish-site']); | ||
grunt.registerTask('build', ['browserify:main', 'browserify:no_promises', | ||
'run:derequire', 'run:derequire_no_promises', | ||
'concat', 'es3_safe_recast', 'uglify']); | ||
grunt.registerTask('serve', ['build', 'connect:test', 'watch']); | ||
grunt.registerTask('site', ['shell:serve-site']); | ||
@@ -264,3 +262,2 @@ // These are the test tasks we run regardless of Sauce Labs credentials. | ||
'jscs', | ||
'shell:component', | ||
'browserify:package_bundling_test', | ||
@@ -267,0 +264,0 @@ 'webpack:package_bundling_test', |
@@ -12,3 +12,3 @@ { | ||
], | ||
"version": "1.4.0", | ||
"version": "1.4.1", | ||
"homepage": "https://github.com/mozilla/localForage", | ||
@@ -20,13 +20,18 @@ "repository": { | ||
"scripts": { | ||
"publish-docs": "node -e \"require('grunt').cli()\" null copy build-rules-html publish-rules", | ||
"test": "node -e \"require('grunt').cli()\" null test" | ||
}, | ||
"devDependencies": { | ||
"babel-core": "^5.8.22", | ||
"babel-loader": "^5.3.2", | ||
"babel-plugin-system-import-transformer": "^1.1.4", | ||
"babelify": "^6.1.2", | ||
"component": "^1.0.0-rc7", | ||
"babel-core": "^6.5.1", | ||
"babel-loader": "^6.2.2", | ||
"babel-plugin-add-module-exports": "^0.1.2", | ||
"babel-plugin-transform-es2015-modules-umd": "^6.5.0", | ||
"babel-preset-es2015": "^6.6.0", | ||
"babel-preset-es2015-loose": "^7.0.0", | ||
"babelify": "^7.2.0", | ||
"bundle-collapser": "^1.2.1", | ||
"cors": "^2.3.1", | ||
"derequire": "^2.0.3", | ||
"grunt": "^0.4.2", | ||
"grunt-babel": "^5.0.0", | ||
"grunt-babel": "^6.0.0", | ||
"grunt-browserify": "^3.8.0", | ||
@@ -41,11 +46,15 @@ "grunt-contrib-concat": "^0.3.0", | ||
"grunt-mocha": "^0.4.10", | ||
"grunt-rollup": "^0.6.2", | ||
"grunt-run": "^0.5.2", | ||
"grunt-saucelabs": "^5.1.2", | ||
"grunt-shell": "^0.6.4", | ||
"grunt-webpack": "^1.0.11", | ||
"load-grunt-tasks": "^0.4.0", | ||
"mocha": "^1.18.2", | ||
"ncp": "^2.0.0", | ||
"phantomjs": "^1.9.7-12", | ||
"rimraf": "^2.5.2", | ||
"rollupify": "^0.1.0", | ||
"script-loader": "^0.6.1", | ||
"uglify-js": "^2.3.x", | ||
"webpack": "^1.11.0", | ||
"webpack": "^1.12.13", | ||
"webpack-dev-server": "^1.10.1" | ||
@@ -58,4 +67,4 @@ }, | ||
"dependencies": { | ||
"promise": "^5.0.0" | ||
"lie": "3.0.2" | ||
} | ||
} |
@@ -51,3 +51,3 @@ # localForage [![Build Status](https://travis-ci.org/mozilla/localForage.svg?branch=master)](http://travis-ci.org/mozilla/localForage) | ||
## Callbacks | ||
## Callbacks vs Promises | ||
@@ -58,38 +58,32 @@ Because localForage uses async storage, it has an async API. | ||
```javascript | ||
// In localStorage, we would do: | ||
var obj = { value: "hello world" }; | ||
localStorage.setItem('key', JSON.stringify(obj)); | ||
alert(obj.value); | ||
localForage has a dual API that allows you to either use Node-style callbacks | ||
or [Promises](https://www.promisejs.org/). If you are unsure which one is right for you, it's recommend to use Promises. | ||
// With localForage, we use callbacks: | ||
localforage.setItem('key', obj, function(err, result) { alert(result.value); }); | ||
``` | ||
Here's an example of the Node-style callback form: | ||
Similarly, please don't expect a return value from calls to | ||
`localforage.getItem()`. Instead, use a callback: | ||
```javascript | ||
// Synchronous; slower! | ||
var value = JSON.parse(localStorage.getItem('key')); | ||
alert(value); | ||
// Async, fast, and non-blocking! | ||
localforage.getItem('key', function(err, value) { alert(value) }); | ||
```js | ||
localforage.setItem('key', 'value', function (err) { | ||
// if err is non-null, we got an error | ||
localforage.getItem('key', function (err, value) { | ||
// if err is non-null, we got an error. otherwise, value is the value | ||
}); | ||
}); | ||
``` | ||
Callbacks in localForage are Node-style (error argument first) since version | ||
`0.9.3`. This means if you're using callbacks, your code should look like this: | ||
And the Promise form: | ||
```javascript | ||
// Use err as your first argument. | ||
localforage.getItem('key', function(err, value) { | ||
if (err) { | ||
console.error('Oh noes!'); | ||
} else { | ||
alert(value); | ||
} | ||
```js | ||
localforage.setItem('key', 'value').then(function () { | ||
return localforage.getItem('key'); | ||
}).then(function (value) { | ||
// we got our value | ||
}).catch(function (err) { | ||
// we got an error | ||
}); | ||
``` | ||
For more examples, please visit [the API docs](http://mozilla.github.io/localForage). | ||
## Storing Blobs, TypedArrays, and other JS objects | ||
You can store any type in localForage; you aren't limited to strings like in | ||
@@ -100,34 +94,2 @@ localStorage. Even if localStorage is your storage backend, localForage | ||
## Promises | ||
Promises are pretty cool! If you'd rather use promises than callbacks, | ||
localForage supports that too: | ||
```javascript | ||
function doSomethingElse(value) { | ||
console.log(value); | ||
} | ||
// With localForage, we allow promises: | ||
localforage.setItem('key', 'value').then(doSomethingElse); | ||
``` | ||
When using Promises, `err` is **not** the first argument passed to a function. | ||
Instead, you handle an error with the rejection part of the Promise: | ||
```javascript | ||
// A full setItem() call with Promises. | ||
localforage.setItem('key', 'value').then(function(value) { | ||
alert(value + ' was set!'); | ||
}, function(error) { | ||
console.error(error); | ||
}); | ||
``` | ||
localForage relies on native [ES6 Promises](https://www.promisejs.org/), but | ||
[ships with an awesome polyfill](https://github.com/jakearchibald/es6-promise) | ||
for browsers that don't support ES6 Promises yet. | ||
## Storing Blobs, TypedArrays, and other JS objects | ||
localForage supports storing all native JS objects that can be serialized to | ||
@@ -134,0 +96,0 @@ JSON, as well as ArrayBuffers, Blobs, and TypedArrays. Check the |
@@ -0,766 +1,689 @@ | ||
import createBlob from '../utils/createBlob'; | ||
import idb from '../utils/idb'; | ||
import Promise from '../utils/promise'; | ||
import executeCallback from '../utils/executeCallback'; | ||
// Some code originally from async_storage.js in | ||
// [Gaia](https://github.com/mozilla-b2g/gaia). | ||
var asyncStorage = (function(globalObject) { | ||
'use strict'; | ||
// Initialize IndexedDB; fall back to vendor-prefixed versions if needed. | ||
var indexedDB = indexedDB || globalObject.indexedDB || globalObject.webkitIndexedDB || | ||
globalObject.mozIndexedDB || globalObject.OIndexedDB || | ||
globalObject.msIndexedDB; | ||
var DETECT_BLOB_SUPPORT_STORE = 'local-forage-detect-blob-support'; | ||
var supportsBlobs; | ||
var dbContexts; | ||
// If IndexedDB isn't available, we get outta here! | ||
if (!indexedDB) { | ||
return; | ||
// Transform a binary string to an array buffer, because otherwise | ||
// weird stuff happens when you try to work with the binary string directly. | ||
// It is known. | ||
// From http://stackoverflow.com/questions/14967647/ (continues on next line) | ||
// encode-decode-image-with-base64-breaks-image (2013-04-21) | ||
function _binStringToArrayBuffer(bin) { | ||
var length = bin.length; | ||
var buf = new ArrayBuffer(length); | ||
var arr = new Uint8Array(buf); | ||
for (var i = 0; i < length; i++) { | ||
arr[i] = bin.charCodeAt(i); | ||
} | ||
return buf; | ||
} | ||
var DETECT_BLOB_SUPPORT_STORE = 'local-forage-detect-blob-support'; | ||
var supportsBlobs; | ||
var dbContexts; | ||
// | ||
// Blobs are not supported in all versions of IndexedDB, notably | ||
// Chrome <37 and Android <5. In those versions, storing a blob will throw. | ||
// | ||
// Various other blob bugs exist in Chrome v37-42 (inclusive). | ||
// Detecting them is expensive and confusing to users, and Chrome 37-42 | ||
// is at very low usage worldwide, so we do a hacky userAgent check instead. | ||
// | ||
// content-type bug: https://code.google.com/p/chromium/issues/detail?id=408120 | ||
// 404 bug: https://code.google.com/p/chromium/issues/detail?id=447916 | ||
// 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 | ||
// | ||
function _checkBlobSupportWithoutCaching(txn) { | ||
return new Promise(function(resolve) { | ||
var blob = createBlob(['']); | ||
txn.objectStore(DETECT_BLOB_SUPPORT_STORE).put(blob, 'key'); | ||
// Abstracts constructing a Blob object, so it also works in older | ||
// browsers that don't support the native Blob constructor. (i.e. | ||
// old QtWebKit versions, at least). | ||
function _createBlob(parts, properties) { | ||
parts = parts || []; | ||
properties = properties || {}; | ||
try { | ||
return new Blob(parts, properties); | ||
} catch (e) { | ||
if (e.name !== 'TypeError') { | ||
throw e; | ||
} | ||
var BlobBuilder = globalObject.BlobBuilder || | ||
globalObject.MSBlobBuilder || | ||
globalObject.MozBlobBuilder || | ||
globalObject.WebKitBlobBuilder; | ||
var builder = new BlobBuilder(); | ||
for (var i = 0; i < parts.length; i += 1) { | ||
builder.append(parts[i]); | ||
} | ||
return builder.getBlob(properties.type); | ||
} | ||
} | ||
txn.onabort = function(e) { | ||
// If the transaction aborts now its due to not being able to | ||
// write to the database, likely due to the disk being full | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
resolve(false); | ||
}; | ||
// Transform a binary string to an array buffer, because otherwise | ||
// weird stuff happens when you try to work with the binary string directly. | ||
// It is known. | ||
// From http://stackoverflow.com/questions/14967647/ (continues on next line) | ||
// encode-decode-image-with-base64-breaks-image (2013-04-21) | ||
function _binStringToArrayBuffer(bin) { | ||
var length = bin.length; | ||
var buf = new ArrayBuffer(length); | ||
var arr = new Uint8Array(buf); | ||
for (var i = 0; i < length; i++) { | ||
arr[i] = bin.charCodeAt(i); | ||
} | ||
return buf; | ||
txn.oncomplete = function() { | ||
var matchedChrome = navigator.userAgent.match(/Chrome\/(\d+)/); | ||
var matchedEdge = navigator.userAgent.match(/Edge\//); | ||
// MS Edge pretends to be Chrome 42: | ||
// https://msdn.microsoft.com/en-us/library/hh869301%28v=vs.85%29.aspx | ||
resolve(matchedEdge || !matchedChrome || | ||
parseInt(matchedChrome[1], 10) >= 43); | ||
}; | ||
}).catch(function() { | ||
return false; // error, so assume unsupported | ||
}); | ||
} | ||
function _checkBlobSupport(idb) { | ||
if (typeof supportsBlobs === 'boolean') { | ||
return Promise.resolve(supportsBlobs); | ||
} | ||
return _checkBlobSupportWithoutCaching(idb).then(function(value) { | ||
supportsBlobs = value; | ||
return supportsBlobs; | ||
}); | ||
} | ||
// Fetch a blob using ajax. This reveals bugs in Chrome < 43. | ||
// For details on all this junk: | ||
// https://github.com/nolanlawson/state-of-binary-data-in-the-browser#readme | ||
function _blobAjax(url) { | ||
return new Promise(function(resolve, reject) { | ||
var xhr = new XMLHttpRequest(); | ||
xhr.open('GET', url); | ||
xhr.withCredentials = true; | ||
xhr.responseType = 'arraybuffer'; | ||
function _deferReadiness(dbInfo) { | ||
var dbContext = dbContexts[dbInfo.name]; | ||
xhr.onreadystatechange = function() { | ||
if (xhr.readyState !== 4) { | ||
return; | ||
} | ||
if (xhr.status === 200) { | ||
return resolve({ | ||
response: xhr.response, | ||
type: xhr.getResponseHeader('Content-Type') | ||
}); | ||
} | ||
reject({status: xhr.status, response: xhr.response}); | ||
}; | ||
xhr.send(); | ||
}); | ||
} | ||
// Create a deferred object representing the current database operation. | ||
var deferredOperation = {}; | ||
// | ||
// Detect blob support. Chrome didn't support it until version 38. | ||
// In version 37 they had a broken version where PNGs (and possibly | ||
// other binary types) aren't stored correctly, because when you fetch | ||
// them, the content type is always null. | ||
// | ||
// Furthermore, they have some outstanding bugs where blobs occasionally | ||
// are read by FileReader as null, or by ajax as 404s. | ||
// | ||
// Sadly we use the 404 bug to detect the FileReader bug, so if they | ||
// get fixed independently and released in different versions of Chrome, | ||
// then the bug could come back. So it's worthwhile to watch these issues: | ||
// 404 bug: https://code.google.com/p/chromium/issues/detail?id=447916 | ||
// FileReader bug: https://code.google.com/p/chromium/issues/detail?id=447836 | ||
// | ||
function _checkBlobSupportWithoutCaching(idb) { | ||
return new Promise(function(resolve, reject) { | ||
var blob = _createBlob([''], {type: 'image/png'}); | ||
var txn = idb.transaction([DETECT_BLOB_SUPPORT_STORE], 'readwrite'); | ||
txn.objectStore(DETECT_BLOB_SUPPORT_STORE).put(blob, 'key'); | ||
txn.oncomplete = function() { | ||
// have to do it in a separate transaction, else the correct | ||
// content type is always returned | ||
var blobTxn = idb.transaction([DETECT_BLOB_SUPPORT_STORE], | ||
'readwrite'); | ||
var getBlobReq = blobTxn.objectStore( | ||
DETECT_BLOB_SUPPORT_STORE).get('key'); | ||
getBlobReq.onerror = reject; | ||
getBlobReq.onsuccess = function(e) { | ||
deferredOperation.promise = new Promise(function(resolve) { | ||
deferredOperation.resolve = resolve; | ||
}); | ||
var storedBlob = e.target.result; | ||
var url = URL.createObjectURL(storedBlob); | ||
// Enqueue the deferred operation. | ||
dbContext.deferredOperations.push(deferredOperation); | ||
_blobAjax(url).then(function(res) { | ||
resolve(!!(res && res.type === 'image/png')); | ||
}, function() { | ||
resolve(false); | ||
}).then(function() { | ||
URL.revokeObjectURL(url); | ||
}); | ||
}; | ||
}; | ||
txn.onerror = txn.onabort = reject; | ||
}).catch(function() { | ||
return false; // error, so assume unsupported | ||
// Chain its promise to the database readiness. | ||
if (!dbContext.dbReady) { | ||
dbContext.dbReady = deferredOperation.promise; | ||
} else { | ||
dbContext.dbReady = dbContext.dbReady.then(function() { | ||
return deferredOperation.promise; | ||
}); | ||
} | ||
} | ||
function _checkBlobSupport(idb) { | ||
if (typeof supportsBlobs === 'boolean') { | ||
return Promise.resolve(supportsBlobs); | ||
} | ||
return _checkBlobSupportWithoutCaching(idb).then(function(value) { | ||
supportsBlobs = value; | ||
return supportsBlobs; | ||
}); | ||
} | ||
function _advanceReadiness(dbInfo) { | ||
var dbContext = dbContexts[dbInfo.name]; | ||
// encode a blob for indexeddb engines that don't support blobs | ||
function _encodeBlob(blob) { | ||
return new Promise(function(resolve, reject) { | ||
var reader = new FileReader(); | ||
reader.onerror = reject; | ||
reader.onloadend = function(e) { | ||
var base64 = btoa(e.target.result || ''); | ||
resolve({ | ||
__local_forage_encoded_blob: true, | ||
data: base64, | ||
type: blob.type | ||
}); | ||
}; | ||
reader.readAsBinaryString(blob); | ||
}); | ||
} | ||
// Dequeue a deferred operation. | ||
var deferredOperation = dbContext.deferredOperations.pop(); | ||
// decode an encoded blob | ||
function _decodeBlob(encodedBlob) { | ||
var arrayBuff = _binStringToArrayBuffer(atob(encodedBlob.data)); | ||
return _createBlob([arrayBuff], { type: encodedBlob.type}); | ||
// Resolve its promise (which is part of the database readiness | ||
// chain of promises). | ||
if (deferredOperation) { | ||
deferredOperation.resolve(); | ||
} | ||
} | ||
// is this one of our fancy encoded blobs? | ||
function _isEncodedBlob(value) { | ||
return value && value.__local_forage_encoded_blob; | ||
} | ||
function _getConnection(dbInfo, upgradeNeeded) { | ||
return new Promise(function(resolve, reject) { | ||
// Specialize the default `ready()` function by making it dependent | ||
// on the current database operations. Thus, the driver will be actually | ||
// ready when it's been initialized (default) *and* there are no pending | ||
// operations on the database (initiated by some other instances). | ||
function _fullyReady(callback) { | ||
var self = this; | ||
if (dbInfo.db) { | ||
if (upgradeNeeded) { | ||
_deferReadiness(dbInfo); | ||
dbInfo.db.close(); | ||
} else { | ||
return resolve(dbInfo.db); | ||
} | ||
} | ||
var promise = self._initReady().then(() => { | ||
var dbContext = dbContexts[self._dbInfo.name]; | ||
var dbArgs = [dbInfo.name]; | ||
if (dbContext && dbContext.dbReady) { | ||
return dbContext.dbReady; | ||
} | ||
}); | ||
if (upgradeNeeded) { | ||
dbArgs.push(dbInfo.version); | ||
} | ||
promise.then(callback, callback); | ||
return promise; | ||
} | ||
var openreq = idb.open.apply(idb, dbArgs); | ||
function _deferReadiness(dbInfo) { | ||
var dbContext = dbContexts[dbInfo.name]; | ||
if (upgradeNeeded) { | ||
openreq.onupgradeneeded = function(e) { | ||
var db = openreq.result; | ||
try { | ||
db.createObjectStore(dbInfo.storeName); | ||
if (e.oldVersion <= 1) { | ||
// Added when support for blob shims was added | ||
db.createObjectStore(DETECT_BLOB_SUPPORT_STORE); | ||
} | ||
} catch (ex) { | ||
if (ex.name === 'ConstraintError') { | ||
console.warn('The database "' + dbInfo.name + '"' + | ||
' has been upgraded from version ' + e.oldVersion + | ||
' to version ' + e.newVersion + | ||
', but the storage "' + dbInfo.storeName + '" already exists.'); | ||
} else { | ||
throw ex; | ||
} | ||
} | ||
}; | ||
} | ||
// Create a deferred object representing the current database operation. | ||
var deferredOperation = {}; | ||
openreq.onerror = function() { | ||
reject(openreq.error); | ||
}; | ||
deferredOperation.promise = new Promise(function(resolve) { | ||
deferredOperation.resolve = resolve; | ||
}); | ||
openreq.onsuccess = function() { | ||
resolve(openreq.result); | ||
_advanceReadiness(dbInfo); | ||
}; | ||
}); | ||
} | ||
// Enqueue the deferred operation. | ||
dbContext.deferredOperations.push(deferredOperation); | ||
function _getOriginalConnection(dbInfo) { | ||
return _getConnection(dbInfo, false); | ||
} | ||
// Chain its promise to the database readiness. | ||
if (!dbContext.dbReady) { | ||
dbContext.dbReady = deferredOperation.promise; | ||
} else { | ||
dbContext.dbReady = dbContext.dbReady.then(function() { | ||
return deferredOperation.promise; | ||
}); | ||
} | ||
function _getUpgradedConnection(dbInfo) { | ||
return _getConnection(dbInfo, true); | ||
} | ||
function _isUpgradeNeeded(dbInfo, defaultVersion) { | ||
if (!dbInfo.db) { | ||
return true; | ||
} | ||
function _advanceReadiness(dbInfo) { | ||
var dbContext = dbContexts[dbInfo.name]; | ||
var isNewStore = !dbInfo.db.objectStoreNames.contains(dbInfo.storeName); | ||
var isDowngrade = dbInfo.version < dbInfo.db.version; | ||
var isUpgrade = dbInfo.version > dbInfo.db.version; | ||
// Dequeue a deferred operation. | ||
var deferredOperation = dbContext.deferredOperations.pop(); | ||
// Resolve its promise (which is part of the database readiness | ||
// chain of promises). | ||
if (deferredOperation) { | ||
deferredOperation.resolve(); | ||
if (isDowngrade) { | ||
// If the version is not the default one | ||
// then warn for impossible downgrade. | ||
if (dbInfo.version !== defaultVersion) { | ||
console.warn('The database "' + dbInfo.name + '"' + | ||
' can\'t be downgraded from version ' + dbInfo.db.version + | ||
' to version ' + dbInfo.version + '.'); | ||
} | ||
// Align the versions to prevent errors. | ||
dbInfo.version = dbInfo.db.version; | ||
} | ||
// Open the IndexedDB database (automatically creates one if one didn't | ||
// previously exist), using any options set in the config. | ||
function _initStorage(options) { | ||
var self = this; | ||
var dbInfo = { | ||
db: null | ||
}; | ||
if (options) { | ||
for (var i in options) { | ||
dbInfo[i] = options[i]; | ||
if (isUpgrade || isNewStore) { | ||
// If the store is new then increment the version (if needed). | ||
// This will trigger an "upgradeneeded" event which is required | ||
// for creating a store. | ||
if (isNewStore) { | ||
var incVersion = dbInfo.db.version + 1; | ||
if (incVersion > dbInfo.version) { | ||
dbInfo.version = incVersion; | ||
} | ||
} | ||
// Initialize a singleton container for all running localForages. | ||
if (!dbContexts) { | ||
dbContexts = {}; | ||
} | ||
return true; | ||
} | ||
// Get the current context of the database; | ||
var dbContext = dbContexts[dbInfo.name]; | ||
return false; | ||
} | ||
// ...or create a new context. | ||
if (!dbContext) { | ||
dbContext = { | ||
// Running localForages sharing a database. | ||
forages: [], | ||
// Shared database. | ||
db: null, | ||
// Database readiness (promise). | ||
dbReady: null, | ||
// Deferred operations on the database. | ||
deferredOperations: [] | ||
}; | ||
// Register the new context in the global container. | ||
dbContexts[dbInfo.name] = dbContext; | ||
} | ||
// encode a blob for indexeddb engines that don't support blobs | ||
function _encodeBlob(blob) { | ||
return new Promise(function(resolve, reject) { | ||
var reader = new FileReader(); | ||
reader.onerror = reject; | ||
reader.onloadend = function(e) { | ||
var base64 = btoa(e.target.result || ''); | ||
resolve({ | ||
__local_forage_encoded_blob: true, | ||
data: base64, | ||
type: blob.type | ||
}); | ||
}; | ||
reader.readAsBinaryString(blob); | ||
}); | ||
} | ||
// Register itself as a running localForage in the current context. | ||
dbContext.forages.push(self); | ||
// decode an encoded blob | ||
function _decodeBlob(encodedBlob) { | ||
var arrayBuff = _binStringToArrayBuffer(atob(encodedBlob.data)); | ||
return createBlob([arrayBuff], {type: encodedBlob.type}); | ||
} | ||
// Replace the default `ready()` function with the specialized one. | ||
if (!self._initReady) { | ||
self._initReady = self.ready; | ||
self.ready = _fullyReady; | ||
} | ||
// is this one of our fancy encoded blobs? | ||
function _isEncodedBlob(value) { | ||
return value && value.__local_forage_encoded_blob; | ||
} | ||
// Create an array of initialization states of the related localForages. | ||
var initPromises = []; | ||
// Specialize the default `ready()` function by making it dependent | ||
// on the current database operations. Thus, the driver will be actually | ||
// ready when it's been initialized (default) *and* there are no pending | ||
// operations on the database (initiated by some other instances). | ||
function _fullyReady(callback) { | ||
var self = this; | ||
function ignoreErrors() { | ||
// Don't handle errors here, | ||
// just makes sure related localForages aren't pending. | ||
return Promise.resolve(); | ||
} | ||
var promise = self._initReady().then(function() { | ||
var dbContext = dbContexts[self._dbInfo.name]; | ||
for (var j = 0; j < dbContext.forages.length; j++) { | ||
var forage = dbContext.forages[j]; | ||
if (forage !== self) { // Don't wait for itself... | ||
initPromises.push(forage._initReady().catch(ignoreErrors)); | ||
} | ||
if (dbContext && dbContext.dbReady) { | ||
return dbContext.dbReady; | ||
} | ||
}); | ||
// Take a snapshot of the related localForages. | ||
var forages = dbContext.forages.slice(0); | ||
promise.then(callback, callback); | ||
return promise; | ||
} | ||
// Initialize the connection process only when | ||
// all the related localForages aren't pending. | ||
return Promise.all(initPromises).then(function() { | ||
dbInfo.db = dbContext.db; | ||
// Get the connection or open a new one without upgrade. | ||
return _getOriginalConnection(dbInfo); | ||
}).then(function(db) { | ||
dbInfo.db = db; | ||
if (_isUpgradeNeeded(dbInfo, self._defaultConfig.version)) { | ||
// Reopen the database for upgrading. | ||
return _getUpgradedConnection(dbInfo); | ||
} | ||
return db; | ||
}).then(function(db) { | ||
dbInfo.db = dbContext.db = db; | ||
self._dbInfo = dbInfo; | ||
// Share the final connection amongst related localForages. | ||
for (var k = 0; k < forages.length; k++) { | ||
var forage = forages[k]; | ||
if (forage !== self) { // Self is already up-to-date. | ||
forage._dbInfo.db = dbInfo.db; | ||
forage._dbInfo.version = dbInfo.version; | ||
} | ||
} | ||
}); | ||
} | ||
// Open the IndexedDB database (automatically creates one if one didn't | ||
// previously exist), using any options set in the config. | ||
function _initStorage(options) { | ||
var self = this; | ||
var dbInfo = { | ||
db: null | ||
}; | ||
function _getOriginalConnection(dbInfo) { | ||
return _getConnection(dbInfo, false); | ||
if (options) { | ||
for (var i in options) { | ||
dbInfo[i] = options[i]; | ||
} | ||
} | ||
function _getUpgradedConnection(dbInfo) { | ||
return _getConnection(dbInfo, true); | ||
// Initialize a singleton container for all running localForages. | ||
if (!dbContexts) { | ||
dbContexts = {}; | ||
} | ||
function _getConnection(dbInfo, upgradeNeeded) { | ||
return new Promise(function(resolve, reject) { | ||
// Get the current context of the database; | ||
var dbContext = dbContexts[dbInfo.name]; | ||
if (dbInfo.db) { | ||
if (upgradeNeeded) { | ||
_deferReadiness(dbInfo); | ||
dbInfo.db.close(); | ||
} else { | ||
return resolve(dbInfo.db); | ||
} | ||
} | ||
// ...or create a new context. | ||
if (!dbContext) { | ||
dbContext = { | ||
// Running localForages sharing a database. | ||
forages: [], | ||
// Shared database. | ||
db: null, | ||
// Database readiness (promise). | ||
dbReady: null, | ||
// Deferred operations on the database. | ||
deferredOperations: [] | ||
}; | ||
// Register the new context in the global container. | ||
dbContexts[dbInfo.name] = dbContext; | ||
} | ||
var dbArgs = [dbInfo.name]; | ||
// Register itself as a running localForage in the current context. | ||
dbContext.forages.push(self); | ||
if (upgradeNeeded) { | ||
dbArgs.push(dbInfo.version); | ||
} | ||
// Replace the default `ready()` function with the specialized one. | ||
if (!self._initReady) { | ||
self._initReady = self.ready; | ||
self.ready = _fullyReady; | ||
} | ||
var openreq = indexedDB.open.apply(indexedDB, dbArgs); | ||
// Create an array of initialization states of the related localForages. | ||
var initPromises = []; | ||
if (upgradeNeeded) { | ||
openreq.onupgradeneeded = function(e) { | ||
var db = openreq.result; | ||
try { | ||
db.createObjectStore(dbInfo.storeName); | ||
if (e.oldVersion <= 1) { | ||
// Added when support for blob shims was added | ||
db.createObjectStore(DETECT_BLOB_SUPPORT_STORE); | ||
} | ||
} catch (ex) { | ||
if (ex.name === 'ConstraintError') { | ||
globalObject.console.warn('The database "' + dbInfo.name + '"' + | ||
' has been upgraded from version ' + e.oldVersion + | ||
' to version ' + e.newVersion + | ||
', but the storage "' + dbInfo.storeName + '" already exists.'); | ||
} else { | ||
throw ex; | ||
} | ||
} | ||
}; | ||
} | ||
openreq.onerror = function() { | ||
reject(openreq.error); | ||
}; | ||
openreq.onsuccess = function() { | ||
resolve(openreq.result); | ||
_advanceReadiness(dbInfo); | ||
}; | ||
}); | ||
function ignoreErrors() { | ||
// Don't handle errors here, | ||
// just makes sure related localForages aren't pending. | ||
return Promise.resolve(); | ||
} | ||
function _isUpgradeNeeded(dbInfo, defaultVersion) { | ||
if (!dbInfo.db) { | ||
return true; | ||
for (var j = 0; j < dbContext.forages.length; j++) { | ||
var forage = dbContext.forages[j]; | ||
if (forage !== self) { // Don't wait for itself... | ||
initPromises.push(forage._initReady().catch(ignoreErrors)); | ||
} | ||
} | ||
var isNewStore = !dbInfo.db.objectStoreNames.contains(dbInfo.storeName); | ||
var isDowngrade = dbInfo.version < dbInfo.db.version; | ||
var isUpgrade = dbInfo.version > dbInfo.db.version; | ||
// Take a snapshot of the related localForages. | ||
var forages = dbContext.forages.slice(0); | ||
if (isDowngrade) { | ||
// If the version is not the default one | ||
// then warn for impossible downgrade. | ||
if (dbInfo.version !== defaultVersion) { | ||
globalObject.console.warn('The database "' + dbInfo.name + '"' + | ||
' can\'t be downgraded from version ' + dbInfo.db.version + | ||
' to version ' + dbInfo.version + '.'); | ||
// Initialize the connection process only when | ||
// all the related localForages aren't pending. | ||
return Promise.all(initPromises).then(function() { | ||
dbInfo.db = dbContext.db; | ||
// Get the connection or open a new one without upgrade. | ||
return _getOriginalConnection(dbInfo); | ||
}).then(function(db) { | ||
dbInfo.db = db; | ||
if (_isUpgradeNeeded(dbInfo, self._defaultConfig.version)) { | ||
// Reopen the database for upgrading. | ||
return _getUpgradedConnection(dbInfo); | ||
} | ||
return db; | ||
}).then(function(db) { | ||
dbInfo.db = dbContext.db = db; | ||
self._dbInfo = dbInfo; | ||
// Share the final connection amongst related localForages. | ||
for (var k = 0; k < forages.length; k++) { | ||
var forage = forages[k]; | ||
if (forage !== self) { // Self is already up-to-date. | ||
forage._dbInfo.db = dbInfo.db; | ||
forage._dbInfo.version = dbInfo.version; | ||
} | ||
// Align the versions to prevent errors. | ||
dbInfo.version = dbInfo.db.version; | ||
} | ||
}); | ||
} | ||
if (isUpgrade || isNewStore) { | ||
// If the store is new then increment the version (if needed). | ||
// This will trigger an "upgradeneeded" event which is required | ||
// for creating a store. | ||
if (isNewStore) { | ||
var incVersion = dbInfo.db.version + 1; | ||
if (incVersion > dbInfo.version) { | ||
dbInfo.version = incVersion; | ||
} | ||
} | ||
function getItem(key, callback) { | ||
var self = this; | ||
return true; | ||
} | ||
return false; | ||
// Cast the key to a string, as that's all we can set as a key. | ||
if (typeof key !== 'string') { | ||
console.warn(key + | ||
' used as a key, but it is not a string.'); | ||
key = String(key); | ||
} | ||
function getItem(key, callback) { | ||
var self = this; | ||
var promise = new Promise(function(resolve, reject) { | ||
self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
var store = dbInfo.db.transaction(dbInfo.storeName, 'readonly') | ||
.objectStore(dbInfo.storeName); | ||
var req = store.get(key); | ||
// Cast the key to a string, as that's all we can set as a key. | ||
if (typeof key !== 'string') { | ||
globalObject.console.warn(key + | ||
' used as a key, but it is not a string.'); | ||
key = String(key); | ||
} | ||
req.onsuccess = function() { | ||
var value = req.result; | ||
if (value === undefined) { | ||
value = null; | ||
} | ||
if (_isEncodedBlob(value)) { | ||
value = _decodeBlob(value); | ||
} | ||
resolve(value); | ||
}; | ||
var promise = new Promise(function(resolve, reject) { | ||
self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
var store = dbInfo.db.transaction(dbInfo.storeName, 'readonly') | ||
.objectStore(dbInfo.storeName); | ||
var req = store.get(key); | ||
req.onerror = function() { | ||
reject(req.error); | ||
}; | ||
}).catch(reject); | ||
}); | ||
req.onsuccess = function() { | ||
var value = req.result; | ||
if (value === undefined) { | ||
value = null; | ||
} | ||
if (_isEncodedBlob(value)) { | ||
value = _decodeBlob(value); | ||
} | ||
resolve(value); | ||
}; | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
req.onerror = function() { | ||
reject(req.error); | ||
}; | ||
}).catch(reject); | ||
}); | ||
// Iterate over all items stored in database. | ||
function iterate(iterator, callback) { | ||
var self = this; | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
var promise = new Promise(function(resolve, reject) { | ||
self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
var store = dbInfo.db.transaction(dbInfo.storeName, 'readonly') | ||
.objectStore(dbInfo.storeName); | ||
// Iterate over all items stored in database. | ||
function iterate(iterator, callback) { | ||
var self = this; | ||
var req = store.openCursor(); | ||
var iterationNumber = 1; | ||
var promise = new Promise(function(resolve, reject) { | ||
self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
var store = dbInfo.db.transaction(dbInfo.storeName, 'readonly') | ||
.objectStore(dbInfo.storeName); | ||
req.onsuccess = function() { | ||
var cursor = req.result; | ||
var req = store.openCursor(); | ||
var iterationNumber = 1; | ||
if (cursor) { | ||
var value = cursor.value; | ||
if (_isEncodedBlob(value)) { | ||
value = _decodeBlob(value); | ||
} | ||
var result = iterator(value, cursor.key, | ||
iterationNumber++); | ||
req.onsuccess = function() { | ||
var cursor = req.result; | ||
if (cursor) { | ||
var value = cursor.value; | ||
if (_isEncodedBlob(value)) { | ||
value = _decodeBlob(value); | ||
} | ||
var result = iterator(value, cursor.key, | ||
iterationNumber++); | ||
if (result !== void(0)) { | ||
resolve(result); | ||
} else { | ||
cursor.continue(); | ||
} | ||
if (result !== void(0)) { | ||
resolve(result); | ||
} else { | ||
resolve(); | ||
cursor.continue(); | ||
} | ||
}; | ||
} else { | ||
resolve(); | ||
} | ||
}; | ||
req.onerror = function() { | ||
reject(req.error); | ||
}; | ||
}).catch(reject); | ||
}); | ||
req.onerror = function() { | ||
reject(req.error); | ||
}; | ||
}).catch(reject); | ||
}); | ||
executeCallback(promise, callback); | ||
executeCallback(promise, callback); | ||
return promise; | ||
return promise; | ||
} | ||
function setItem(key, value, callback) { | ||
var self = this; | ||
// Cast the key to a string, as that's all we can set as a key. | ||
if (typeof key !== 'string') { | ||
console.warn(key + | ||
' used as a key, but it is not a string.'); | ||
key = String(key); | ||
} | ||
function setItem(key, value, callback) { | ||
var self = this; | ||
var promise = new Promise(function(resolve, reject) { | ||
var dbInfo; | ||
self.ready().then(function() { | ||
dbInfo = self._dbInfo; | ||
if (value instanceof Blob) { | ||
return _checkBlobSupport(dbInfo.db).then(function(blobSupport) { | ||
if (blobSupport) { | ||
return value; | ||
} | ||
return _encodeBlob(value); | ||
}); | ||
} | ||
return value; | ||
}).then(function(value) { | ||
var transaction = dbInfo.db.transaction(dbInfo.storeName, 'readwrite'); | ||
var store = transaction.objectStore(dbInfo.storeName); | ||
// Cast the key to a string, as that's all we can set as a key. | ||
if (typeof key !== 'string') { | ||
globalObject.console.warn(key + | ||
' used as a key, but it is not a string.'); | ||
key = String(key); | ||
} | ||
// The reason we don't _save_ null is because IE 10 does | ||
// not support saving the `null` type in IndexedDB. How | ||
// ironic, given the bug below! | ||
// See: https://github.com/mozilla/localForage/issues/161 | ||
if (value === null) { | ||
value = undefined; | ||
} | ||
var promise = new Promise(function(resolve, reject) { | ||
var dbInfo; | ||
self.ready().then(function() { | ||
dbInfo = self._dbInfo; | ||
if (value instanceof Blob) { | ||
return _checkBlobSupport(dbInfo.db).then(function(blobSupport) { | ||
if (blobSupport) { | ||
return value; | ||
} | ||
return _encodeBlob(value); | ||
}); | ||
transaction.oncomplete = function() { | ||
// Cast to undefined so the value passed to | ||
// callback/promise is the same as what one would get out | ||
// of `getItem()` later. This leads to some weirdness | ||
// (setItem('foo', undefined) will return `null`), but | ||
// it's not my fault localStorage is our baseline and that | ||
// it's weird. | ||
if (value === undefined) { | ||
value = null; | ||
} | ||
return value; | ||
}).then(function(value) { | ||
var transaction = dbInfo.db.transaction(dbInfo.storeName, 'readwrite'); | ||
var store = transaction.objectStore(dbInfo.storeName); | ||
// The reason we don't _save_ null is because IE 10 does | ||
// not support saving the `null` type in IndexedDB. How | ||
// ironic, given the bug below! | ||
// See: https://github.com/mozilla/localForage/issues/161 | ||
if (value === null) { | ||
value = undefined; | ||
} | ||
resolve(value); | ||
}; | ||
transaction.onabort = transaction.onerror = function() { | ||
var err = req.error ? req.error : req.transaction.error; | ||
reject(err); | ||
}; | ||
transaction.oncomplete = function() { | ||
// Cast to undefined so the value passed to | ||
// callback/promise is the same as what one would get out | ||
// of `getItem()` later. This leads to some weirdness | ||
// (setItem('foo', undefined) will return `null`), but | ||
// it's not my fault localStorage is our baseline and that | ||
// it's weird. | ||
if (value === undefined) { | ||
value = null; | ||
} | ||
var req = store.put(value, key); | ||
}).catch(reject); | ||
}); | ||
resolve(value); | ||
}; | ||
transaction.onabort = transaction.onerror = function() { | ||
var err = req.error ? req.error : req.transaction.error; | ||
reject(err); | ||
}; | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
var req = store.put(value, key); | ||
}).catch(reject); | ||
}); | ||
function removeItem(key, callback) { | ||
var self = this; | ||
executeCallback(promise, callback); | ||
return promise; | ||
// Cast the key to a string, as that's all we can set as a key. | ||
if (typeof key !== 'string') { | ||
console.warn(key + | ||
' used as a key, but it is not a string.'); | ||
key = String(key); | ||
} | ||
function removeItem(key, callback) { | ||
var self = this; | ||
var promise = new Promise(function(resolve, reject) { | ||
self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
var transaction = dbInfo.db.transaction(dbInfo.storeName, 'readwrite'); | ||
var store = transaction.objectStore(dbInfo.storeName); | ||
// Cast the key to a string, as that's all we can set as a key. | ||
if (typeof key !== 'string') { | ||
globalObject.console.warn(key + | ||
' used as a key, but it is not a string.'); | ||
key = String(key); | ||
} | ||
// We use a Grunt task to make this safe for IE and some | ||
// versions of Android (including those used by Cordova). | ||
// Normally IE won't like `.delete()` and will insist on | ||
// using `['delete']()`, but we have a build step that | ||
// fixes this for us now. | ||
var req = store.delete(key); | ||
transaction.oncomplete = function() { | ||
resolve(); | ||
}; | ||
var promise = new Promise(function(resolve, reject) { | ||
self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
var transaction = dbInfo.db.transaction(dbInfo.storeName, 'readwrite'); | ||
var store = transaction.objectStore(dbInfo.storeName); | ||
transaction.onerror = function() { | ||
reject(req.error); | ||
}; | ||
// We use a Grunt task to make this safe for IE and some | ||
// versions of Android (including those used by Cordova). | ||
// Normally IE won't like `.delete()` and will insist on | ||
// using `['delete']()`, but we have a build step that | ||
// fixes this for us now. | ||
var req = store.delete(key); | ||
transaction.oncomplete = function() { | ||
resolve(); | ||
}; | ||
// The request will be also be aborted if we've exceeded our storage | ||
// space. | ||
transaction.onabort = function() { | ||
var err = req.error ? req.error : req.transaction.error; | ||
reject(err); | ||
}; | ||
}).catch(reject); | ||
}); | ||
transaction.onerror = function() { | ||
reject(req.error); | ||
}; | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
// The request will be also be aborted if we've exceeded our storage | ||
// space. | ||
transaction.onabort = function() { | ||
var err = req.error ? req.error : req.transaction.error; | ||
reject(err); | ||
}; | ||
}).catch(reject); | ||
}); | ||
function clear(callback) { | ||
var self = this; | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
var promise = new Promise(function(resolve, reject) { | ||
self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
var transaction = dbInfo.db.transaction(dbInfo.storeName, 'readwrite'); | ||
var store = transaction.objectStore(dbInfo.storeName); | ||
var req = store.clear(); | ||
function clear(callback) { | ||
var self = this; | ||
transaction.oncomplete = function() { | ||
resolve(); | ||
}; | ||
var promise = new Promise(function(resolve, reject) { | ||
self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
var transaction = dbInfo.db.transaction(dbInfo.storeName, 'readwrite'); | ||
var store = transaction.objectStore(dbInfo.storeName); | ||
var req = store.clear(); | ||
transaction.onabort = transaction.onerror = function() { | ||
var err = req.error ? req.error : req.transaction.error; | ||
reject(err); | ||
}; | ||
}).catch(reject); | ||
}); | ||
transaction.oncomplete = function() { | ||
resolve(); | ||
}; | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
transaction.onabort = transaction.onerror = function() { | ||
var err = req.error ? req.error : req.transaction.error; | ||
reject(err); | ||
}; | ||
}).catch(reject); | ||
}); | ||
function length(callback) { | ||
var self = this; | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
var promise = new Promise(function(resolve, reject) { | ||
self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
var store = dbInfo.db.transaction(dbInfo.storeName, 'readonly') | ||
.objectStore(dbInfo.storeName); | ||
var req = store.count(); | ||
function length(callback) { | ||
var self = this; | ||
req.onsuccess = function() { | ||
resolve(req.result); | ||
}; | ||
var promise = new Promise(function(resolve, reject) { | ||
self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
var store = dbInfo.db.transaction(dbInfo.storeName, 'readonly') | ||
.objectStore(dbInfo.storeName); | ||
var req = store.count(); | ||
req.onerror = function() { | ||
reject(req.error); | ||
}; | ||
}).catch(reject); | ||
}); | ||
req.onsuccess = function() { | ||
resolve(req.result); | ||
}; | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
req.onerror = function() { | ||
reject(req.error); | ||
}; | ||
}).catch(reject); | ||
}); | ||
function key(n, callback) { | ||
var self = this; | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
var promise = new Promise(function(resolve, reject) { | ||
if (n < 0) { | ||
resolve(null); | ||
function key(n, callback) { | ||
var self = this; | ||
return; | ||
} | ||
var promise = new Promise(function(resolve, reject) { | ||
if (n < 0) { | ||
resolve(null); | ||
self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
var store = dbInfo.db.transaction(dbInfo.storeName, 'readonly') | ||
.objectStore(dbInfo.storeName); | ||
return; | ||
} | ||
var advanced = false; | ||
var req = store.openCursor(); | ||
req.onsuccess = function() { | ||
var cursor = req.result; | ||
if (!cursor) { | ||
// this means there weren't enough keys | ||
resolve(null); | ||
self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
var store = dbInfo.db.transaction(dbInfo.storeName, 'readonly') | ||
.objectStore(dbInfo.storeName); | ||
return; | ||
} | ||
var advanced = false; | ||
var req = store.openCursor(); | ||
req.onsuccess = function() { | ||
var cursor = req.result; | ||
if (!cursor) { | ||
// this means there weren't enough keys | ||
resolve(null); | ||
return; | ||
} | ||
if (n === 0) { | ||
// We have the first key, return it if that's what they | ||
// wanted. | ||
if (n === 0) { | ||
// We have the first key, return it if that's what they | ||
// wanted. | ||
resolve(cursor.key); | ||
} else { | ||
if (!advanced) { | ||
// Otherwise, ask the cursor to skip ahead n | ||
// records. | ||
advanced = true; | ||
cursor.advance(n); | ||
} else { | ||
// When we get here, we've got the nth key. | ||
resolve(cursor.key); | ||
} else { | ||
if (!advanced) { | ||
// Otherwise, ask the cursor to skip ahead n | ||
// records. | ||
advanced = true; | ||
cursor.advance(n); | ||
} else { | ||
// When we get here, we've got the nth key. | ||
resolve(cursor.key); | ||
} | ||
} | ||
}; | ||
} | ||
}; | ||
req.onerror = function() { | ||
reject(req.error); | ||
}; | ||
}).catch(reject); | ||
}); | ||
req.onerror = function() { | ||
reject(req.error); | ||
}; | ||
}).catch(reject); | ||
}); | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
function keys(callback) { | ||
var self = this; | ||
function keys(callback) { | ||
var self = this; | ||
var promise = new Promise(function(resolve, reject) { | ||
self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
var store = dbInfo.db.transaction(dbInfo.storeName, 'readonly') | ||
.objectStore(dbInfo.storeName); | ||
var promise = new Promise(function(resolve, reject) { | ||
self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
var store = dbInfo.db.transaction(dbInfo.storeName, 'readonly') | ||
.objectStore(dbInfo.storeName); | ||
var req = store.openCursor(); | ||
var keys = []; | ||
var req = store.openCursor(); | ||
var keys = []; | ||
req.onsuccess = function() { | ||
var cursor = req.result; | ||
req.onsuccess = function() { | ||
var cursor = req.result; | ||
if (!cursor) { | ||
resolve(keys); | ||
return; | ||
} | ||
if (!cursor) { | ||
resolve(keys); | ||
return; | ||
} | ||
keys.push(cursor.key); | ||
cursor.continue(); | ||
}; | ||
keys.push(cursor.key); | ||
cursor.continue(); | ||
}; | ||
req.onerror = function() { | ||
reject(req.error); | ||
}; | ||
}).catch(reject); | ||
}); | ||
req.onerror = function() { | ||
reject(req.error); | ||
}; | ||
}).catch(reject); | ||
}); | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
function executeCallback(promise, callback) { | ||
if (callback) { | ||
promise.then(function(result) { | ||
callback(null, result); | ||
}, function(error) { | ||
callback(error); | ||
}); | ||
} | ||
} | ||
var asyncStorage = { | ||
_driver: 'asyncStorage', | ||
_initStorage: _initStorage, | ||
iterate: iterate, | ||
getItem: getItem, | ||
setItem: setItem, | ||
removeItem: removeItem, | ||
clear: clear, | ||
length: length, | ||
key: key, | ||
keys: keys | ||
}; | ||
return asyncStorage; | ||
})(typeof window !== 'undefined' ? window : self); | ||
var asyncStorage = { | ||
_driver: 'asyncStorage', | ||
_initStorage: _initStorage, | ||
iterate: iterate, | ||
getItem: getItem, | ||
setItem: setItem, | ||
removeItem: removeItem, | ||
clear: clear, | ||
length: length, | ||
key: key, | ||
keys: keys | ||
}; | ||
export default asyncStorage; |
@@ -5,299 +5,267 @@ // If IndexedDB isn't available, we'll fall back to localStorage. | ||
// can be converted to a string via `JSON.stringify()` will be saved). | ||
var localStorageWrapper = (function(globalObject) { | ||
'use strict'; | ||
var localStorage = null; | ||
import serializer from '../utils/serializer'; | ||
import Promise from '../utils/promise'; | ||
import executeCallback from '../utils/executeCallback'; | ||
// If the app is running inside a Google Chrome packaged webapp, or some | ||
// other context where localStorage isn't available, we don't use | ||
// localStorage. This feature detection is preferred over the old | ||
// `if (window.chrome && window.chrome.runtime)` code. | ||
// See: https://github.com/mozilla/localForage/issues/68 | ||
try { | ||
// If localStorage isn't available, we get outta here! | ||
// This should be inside a try catch | ||
if (!globalObject.localStorage || !('setItem' in globalObject.localStorage)) { | ||
return; | ||
// Config the localStorage backend, using options set in the config. | ||
function _initStorage(options) { | ||
var self = this; | ||
var dbInfo = {}; | ||
if (options) { | ||
for (var i in options) { | ||
dbInfo[i] = options[i]; | ||
} | ||
// Initialize localStorage and create a variable to use throughout | ||
// the code. | ||
localStorage = globalObject.localStorage; | ||
} catch (e) { | ||
return; | ||
} | ||
// Config the localStorage backend, using options set in the config. | ||
function _initStorage(options) { | ||
var self = this; | ||
var dbInfo = {}; | ||
if (options) { | ||
for (var i in options) { | ||
dbInfo[i] = options[i]; | ||
} | ||
} | ||
dbInfo.keyPrefix = dbInfo.name + '/'; | ||
dbInfo.keyPrefix = dbInfo.name + '/'; | ||
if (dbInfo.storeName !== self._defaultConfig.storeName) { | ||
dbInfo.keyPrefix += dbInfo.storeName + '/'; | ||
} | ||
if (dbInfo.storeName !== self._defaultConfig.storeName) { | ||
dbInfo.keyPrefix += dbInfo.storeName + '/'; | ||
} | ||
self._dbInfo = dbInfo; | ||
dbInfo.serializer = serializer; | ||
self._dbInfo = dbInfo; | ||
return Promise.resolve(); | ||
} | ||
return System.import('./../utils/serializer').then(function(lib) { | ||
dbInfo.serializer = lib; | ||
return Promise.resolve(); | ||
}); | ||
} | ||
// Remove all keys from the datastore, effectively destroying all data in | ||
// the app's key/value store! | ||
function clear(callback) { | ||
var self = this; | ||
var promise = self.ready().then(function() { | ||
var keyPrefix = self._dbInfo.keyPrefix; | ||
// Remove all keys from the datastore, effectively destroying all data in | ||
// the app's key/value store! | ||
function clear(callback) { | ||
var self = this; | ||
var promise = self.ready().then(function() { | ||
var keyPrefix = self._dbInfo.keyPrefix; | ||
for (var i = localStorage.length - 1; i >= 0; i--) { | ||
var key = localStorage.key(i); | ||
for (var i = localStorage.length - 1; i >= 0; i--) { | ||
var key = localStorage.key(i); | ||
if (key.indexOf(keyPrefix) === 0) { | ||
localStorage.removeItem(key); | ||
} | ||
if (key.indexOf(keyPrefix) === 0) { | ||
localStorage.removeItem(key); | ||
} | ||
}); | ||
} | ||
}); | ||
executeCallback(promise, callback); | ||
return promise; | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
// Retrieve an item from the store. Unlike the original async_storage | ||
// library in Gaia, we don't modify return values at all. If a key's value | ||
// is `undefined`, we pass that value to the callback function. | ||
function getItem(key, callback) { | ||
var self = this; | ||
// Cast the key to a string, as that's all we can set as a key. | ||
if (typeof key !== 'string') { | ||
console.warn(key + | ||
' used as a key, but it is not a string.'); | ||
key = String(key); | ||
} | ||
// Retrieve an item from the store. Unlike the original async_storage | ||
// library in Gaia, we don't modify return values at all. If a key's value | ||
// is `undefined`, we pass that value to the callback function. | ||
function getItem(key, callback) { | ||
var self = this; | ||
var promise = self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
var result = localStorage.getItem(dbInfo.keyPrefix + key); | ||
// Cast the key to a string, as that's all we can set as a key. | ||
if (typeof key !== 'string') { | ||
globalObject.console.warn(key + | ||
' used as a key, but it is not a string.'); | ||
key = String(key); | ||
// If a result was found, parse it from the serialized | ||
// string into a JS object. If result isn't truthy, the key | ||
// is likely undefined and we'll pass it straight to the | ||
// callback. | ||
if (result) { | ||
result = dbInfo.serializer.deserialize(result); | ||
} | ||
var promise = self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
var result = localStorage.getItem(dbInfo.keyPrefix + key); | ||
return result; | ||
}); | ||
// If a result was found, parse it from the serialized | ||
// string into a JS object. If result isn't truthy, the key | ||
// is likely undefined and we'll pass it straight to the | ||
// callback. | ||
if (result) { | ||
result = dbInfo.serializer.deserialize(result); | ||
} | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
return result; | ||
}); | ||
// Iterate over all items in the store. | ||
function iterate(iterator, callback) { | ||
var self = this; | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
var promise = self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
var keyPrefix = dbInfo.keyPrefix; | ||
var keyPrefixLength = keyPrefix.length; | ||
var length = localStorage.length; | ||
// Iterate over all items in the store. | ||
function iterate(iterator, callback) { | ||
var self = this; | ||
// We use a dedicated iterator instead of the `i` variable below | ||
// so other keys we fetch in localStorage aren't counted in | ||
// the `iterationNumber` argument passed to the `iterate()` | ||
// callback. | ||
// | ||
// See: github.com/mozilla/localForage/pull/435#discussion_r38061530 | ||
var iterationNumber = 1; | ||
var promise = self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
var keyPrefix = dbInfo.keyPrefix; | ||
var keyPrefixLength = keyPrefix.length; | ||
var length = localStorage.length; | ||
for (var i = 0; i < length; i++) { | ||
var key = localStorage.key(i); | ||
if (key.indexOf(keyPrefix) !== 0) { | ||
continue; | ||
} | ||
var value = localStorage.getItem(key); | ||
// We use a dedicated iterator instead of the `i` variable below | ||
// so other keys we fetch in localStorage aren't counted in | ||
// the `iterationNumber` argument passed to the `iterate()` | ||
// callback. | ||
// | ||
// See: github.com/mozilla/localForage/pull/435#discussion_r38061530 | ||
var iterationNumber = 1; | ||
// If a result was found, parse it from the serialized | ||
// string into a JS object. If result isn't truthy, the | ||
// key is likely undefined and we'll pass it straight | ||
// to the iterator. | ||
if (value) { | ||
value = dbInfo.serializer.deserialize(value); | ||
} | ||
for (var i = 0; i < length; i++) { | ||
var key = localStorage.key(i); | ||
if (key.indexOf(keyPrefix) !== 0) { | ||
continue; | ||
} | ||
var value = localStorage.getItem(key); | ||
value = iterator(value, key.substring(keyPrefixLength), | ||
iterationNumber++); | ||
// If a result was found, parse it from the serialized | ||
// string into a JS object. If result isn't truthy, the | ||
// key is likely undefined and we'll pass it straight | ||
// to the iterator. | ||
if (value) { | ||
value = dbInfo.serializer.deserialize(value); | ||
} | ||
value = iterator(value, key.substring(keyPrefixLength), | ||
iterationNumber++); | ||
if (value !== void(0)) { | ||
return value; | ||
} | ||
if (value !== void(0)) { | ||
return value; | ||
} | ||
}); | ||
} | ||
}); | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
// Same as localStorage's key() method, except takes a callback. | ||
function key(n, callback) { | ||
var self = this; | ||
var promise = self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
var result; | ||
try { | ||
result = localStorage.key(n); | ||
} catch (error) { | ||
result = null; | ||
} | ||
// Same as localStorage's key() method, except takes a callback. | ||
function key(n, callback) { | ||
var self = this; | ||
var promise = self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
var result; | ||
try { | ||
result = localStorage.key(n); | ||
} catch (error) { | ||
result = null; | ||
} | ||
// Remove the prefix from the key, if a key is found. | ||
if (result) { | ||
result = result.substring(dbInfo.keyPrefix.length); | ||
} | ||
// Remove the prefix from the key, if a key is found. | ||
if (result) { | ||
result = result.substring(dbInfo.keyPrefix.length); | ||
} | ||
return result; | ||
}); | ||
return result; | ||
}); | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
function keys(callback) { | ||
var self = this; | ||
var promise = self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
var length = localStorage.length; | ||
var keys = []; | ||
function keys(callback) { | ||
var self = this; | ||
var promise = self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
var length = localStorage.length; | ||
var keys = []; | ||
for (var i = 0; i < length; i++) { | ||
if (localStorage.key(i).indexOf(dbInfo.keyPrefix) === 0) { | ||
keys.push(localStorage.key(i).substring(dbInfo.keyPrefix.length)); | ||
} | ||
for (var i = 0; i < length; i++) { | ||
if (localStorage.key(i).indexOf(dbInfo.keyPrefix) === 0) { | ||
keys.push(localStorage.key(i).substring(dbInfo.keyPrefix.length)); | ||
} | ||
} | ||
return keys; | ||
}); | ||
return keys; | ||
}); | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
// Supply the number of keys in the datastore to the callback function. | ||
function length(callback) { | ||
var self = this; | ||
var promise = self.keys().then(function(keys) { | ||
return keys.length; | ||
}); | ||
// Supply the number of keys in the datastore to the callback function. | ||
function length(callback) { | ||
var self = this; | ||
var promise = self.keys().then(function(keys) { | ||
return keys.length; | ||
}); | ||
executeCallback(promise, callback); | ||
return promise; | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
// Remove an item from the store, nice and simple. | ||
function removeItem(key, callback) { | ||
var self = this; | ||
// Cast the key to a string, as that's all we can set as a key. | ||
if (typeof key !== 'string') { | ||
console.warn(key + | ||
' used as a key, but it is not a string.'); | ||
key = String(key); | ||
} | ||
// Remove an item from the store, nice and simple. | ||
function removeItem(key, callback) { | ||
var self = this; | ||
var promise = self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
localStorage.removeItem(dbInfo.keyPrefix + key); | ||
}); | ||
// Cast the key to a string, as that's all we can set as a key. | ||
if (typeof key !== 'string') { | ||
globalObject.console.warn(key + | ||
' used as a key, but it is not a string.'); | ||
key = String(key); | ||
} | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
var promise = self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
localStorage.removeItem(dbInfo.keyPrefix + key); | ||
}); | ||
// Set a key's value and run an optional callback once the value is set. | ||
// Unlike Gaia's implementation, the callback function is passed the value, | ||
// in case you want to operate on that value only after you're sure it | ||
// saved, or something like that. | ||
function setItem(key, value, callback) { | ||
var self = this; | ||
executeCallback(promise, callback); | ||
return promise; | ||
// Cast the key to a string, as that's all we can set as a key. | ||
if (typeof key !== 'string') { | ||
console.warn(key + | ||
' used as a key, but it is not a string.'); | ||
key = String(key); | ||
} | ||
// Set a key's value and run an optional callback once the value is set. | ||
// Unlike Gaia's implementation, the callback function is passed the value, | ||
// in case you want to operate on that value only after you're sure it | ||
// saved, or something like that. | ||
function setItem(key, value, callback) { | ||
var self = this; | ||
// Cast the key to a string, as that's all we can set as a key. | ||
if (typeof key !== 'string') { | ||
globalObject.console.warn(key + | ||
' used as a key, but it is not a string.'); | ||
key = String(key); | ||
var promise = self.ready().then(function() { | ||
// Convert undefined values to null. | ||
// https://github.com/mozilla/localForage/pull/42 | ||
if (value === undefined) { | ||
value = null; | ||
} | ||
var promise = self.ready().then(function() { | ||
// Convert undefined values to null. | ||
// https://github.com/mozilla/localForage/pull/42 | ||
if (value === undefined) { | ||
value = null; | ||
} | ||
// Save the original value to pass to the callback. | ||
var originalValue = value; | ||
// Save the original value to pass to the callback. | ||
var originalValue = value; | ||
return new Promise(function(resolve, reject) { | ||
var dbInfo = self._dbInfo; | ||
dbInfo.serializer.serialize(value, function(value, error) { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
try { | ||
localStorage.setItem(dbInfo.keyPrefix + key, value); | ||
resolve(originalValue); | ||
} catch (e) { | ||
// localStorage capacity exceeded. | ||
// TODO: Make this a specific error/event. | ||
if (e.name === 'QuotaExceededError' || | ||
e.name === 'NS_ERROR_DOM_QUOTA_REACHED') { | ||
reject(e); | ||
} | ||
return new Promise(function(resolve, reject) { | ||
var dbInfo = self._dbInfo; | ||
dbInfo.serializer.serialize(value, function(value, error) { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
try { | ||
localStorage.setItem(dbInfo.keyPrefix + key, value); | ||
resolve(originalValue); | ||
} catch (e) { | ||
// localStorage capacity exceeded. | ||
// TODO: Make this a specific error/event. | ||
if (e.name === 'QuotaExceededError' || | ||
e.name === 'NS_ERROR_DOM_QUOTA_REACHED') { | ||
reject(e); | ||
} | ||
reject(e); | ||
} | ||
}); | ||
} | ||
}); | ||
}); | ||
}); | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
function executeCallback(promise, callback) { | ||
if (callback) { | ||
promise.then(function(result) { | ||
callback(null, result); | ||
}, function(error) { | ||
callback(error); | ||
}); | ||
} | ||
} | ||
var localStorageWrapper = { | ||
_driver: 'localStorageWrapper', | ||
_initStorage: _initStorage, | ||
// Default API, from Gaia/localStorage. | ||
iterate: iterate, | ||
getItem: getItem, | ||
setItem: setItem, | ||
removeItem: removeItem, | ||
clear: clear, | ||
length: length, | ||
key: key, | ||
keys: keys | ||
}; | ||
var localStorageWrapper = { | ||
_driver: 'localStorageWrapper', | ||
_initStorage: _initStorage, | ||
// Default API, from Gaia/localStorage. | ||
iterate: iterate, | ||
getItem: getItem, | ||
setItem: setItem, | ||
removeItem: removeItem, | ||
clear: clear, | ||
length: length, | ||
key: key, | ||
keys: keys | ||
}; | ||
return localStorageWrapper; | ||
})(typeof window !== 'undefined' ? window : self); | ||
export default localStorageWrapper; |
@@ -0,1 +1,5 @@ | ||
import serializer from '../utils/serializer'; | ||
import Promise from '../utils/promise'; | ||
import executeCallback from '../utils/executeCallback'; | ||
/* | ||
@@ -10,363 +14,339 @@ * Includes code from: | ||
*/ | ||
var webSQLStorage = (function(globalObject) { | ||
'use strict'; | ||
// Open the WebSQL database (automatically creates one if one didn't | ||
// previously exist), using any options set in the config. | ||
function _initStorage(options) { | ||
var self = this; | ||
var dbInfo = { | ||
db: null | ||
}; | ||
var openDatabase = globalObject.openDatabase; | ||
// If WebSQL methods aren't available, we can stop now. | ||
if (!openDatabase) { | ||
return; | ||
if (options) { | ||
for (var i in options) { | ||
dbInfo[i] = typeof(options[i]) !== 'string' ? | ||
options[i].toString() : options[i]; | ||
} | ||
} | ||
// Open the WebSQL database (automatically creates one if one didn't | ||
// previously exist), using any options set in the config. | ||
function _initStorage(options) { | ||
var self = this; | ||
var dbInfo = { | ||
db: null | ||
}; | ||
if (options) { | ||
for (var i in options) { | ||
dbInfo[i] = typeof(options[i]) !== 'string' ? | ||
options[i].toString() : options[i]; | ||
} | ||
var dbInfoPromise = new Promise(function(resolve, reject) { | ||
// Open the database; the openDatabase API will automatically | ||
// create it for us if it doesn't exist. | ||
try { | ||
dbInfo.db = openDatabase(dbInfo.name, String(dbInfo.version), | ||
dbInfo.description, dbInfo.size); | ||
} catch (e) { | ||
return reject(e); | ||
} | ||
var dbInfoPromise = new Promise(function(resolve, reject) { | ||
// Open the database; the openDatabase API will automatically | ||
// create it for us if it doesn't exist. | ||
try { | ||
dbInfo.db = openDatabase(dbInfo.name, String(dbInfo.version), | ||
dbInfo.description, dbInfo.size); | ||
} catch (e) { | ||
return reject(e); | ||
} | ||
// Create our key/value table if it doesn't exist. | ||
dbInfo.db.transaction(function(t) { | ||
t.executeSql('CREATE TABLE IF NOT EXISTS ' + dbInfo.storeName + | ||
' (id INTEGER PRIMARY KEY, key unique, value)', [], | ||
function() { | ||
self._dbInfo = dbInfo; | ||
resolve(); | ||
}, function(t, error) { | ||
reject(error); | ||
}); | ||
}); | ||
}); | ||
// Create our key/value table if it doesn't exist. | ||
dbInfo.serializer = serializer; | ||
return dbInfoPromise; | ||
} | ||
function getItem(key, callback) { | ||
var self = this; | ||
// Cast the key to a string, as that's all we can set as a key. | ||
if (typeof key !== 'string') { | ||
console.warn(key + | ||
' used as a key, but it is not a string.'); | ||
key = String(key); | ||
} | ||
var promise = new Promise(function(resolve, reject) { | ||
self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
dbInfo.db.transaction(function(t) { | ||
t.executeSql('CREATE TABLE IF NOT EXISTS ' + dbInfo.storeName + | ||
' (id INTEGER PRIMARY KEY, key unique, value)', [], | ||
function() { | ||
self._dbInfo = dbInfo; | ||
resolve(); | ||
t.executeSql('SELECT * FROM ' + dbInfo.storeName + | ||
' WHERE key = ? LIMIT 1', [key], | ||
function(t, results) { | ||
var result = results.rows.length ? | ||
results.rows.item(0).value : null; | ||
// Check to see if this is serialized content we need to | ||
// unpack. | ||
if (result) { | ||
result = dbInfo.serializer.deserialize(result); | ||
} | ||
resolve(result); | ||
}, function(t, error) { | ||
reject(error); | ||
}); | ||
}); | ||
}); | ||
}).catch(reject); | ||
}); | ||
return System.import('./../utils/serializer').then(function(lib) { | ||
dbInfo.serializer = lib; | ||
return dbInfoPromise; | ||
}); | ||
} | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
function getItem(key, callback) { | ||
var self = this; | ||
function iterate(iterator, callback) { | ||
var self = this; | ||
// Cast the key to a string, as that's all we can set as a key. | ||
if (typeof key !== 'string') { | ||
globalObject.console.warn(key + | ||
' used as a key, but it is not a string.'); | ||
key = String(key); | ||
} | ||
var promise = new Promise(function(resolve, reject) { | ||
self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
var promise = new Promise(function(resolve, reject) { | ||
self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
dbInfo.db.transaction(function(t) { | ||
t.executeSql('SELECT * FROM ' + dbInfo.storeName + | ||
' WHERE key = ? LIMIT 1', [key], | ||
function(t, results) { | ||
var result = results.rows.length ? | ||
results.rows.item(0).value : null; | ||
dbInfo.db.transaction(function(t) { | ||
t.executeSql('SELECT * FROM ' + dbInfo.storeName, [], | ||
function(t, results) { | ||
var rows = results.rows; | ||
var length = rows.length; | ||
// Check to see if this is serialized content we need to | ||
// unpack. | ||
if (result) { | ||
result = dbInfo.serializer.deserialize(result); | ||
for (var i = 0; i < length; i++) { | ||
var item = rows.item(i); | ||
var result = item.value; | ||
// Check to see if this is serialized content | ||
// we need to unpack. | ||
if (result) { | ||
result = dbInfo.serializer.deserialize(result); | ||
} | ||
result = iterator(result, item.key, i + 1); | ||
// void(0) prevents problems with redefinition | ||
// of `undefined`. | ||
if (result !== void(0)) { | ||
resolve(result); | ||
return; | ||
} | ||
} | ||
resolve(result); | ||
resolve(); | ||
}, function(t, error) { | ||
reject(error); | ||
}); | ||
}); | ||
}).catch(reject); | ||
}); | ||
}); | ||
}).catch(reject); | ||
}); | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
function iterate(iterator, callback) { | ||
var self = this; | ||
function setItem(key, value, callback) { | ||
var self = this; | ||
var promise = new Promise(function(resolve, reject) { | ||
self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
// Cast the key to a string, as that's all we can set as a key. | ||
if (typeof key !== 'string') { | ||
console.warn(key + | ||
' used as a key, but it is not a string.'); | ||
key = String(key); | ||
} | ||
dbInfo.db.transaction(function(t) { | ||
t.executeSql('SELECT * FROM ' + dbInfo.storeName, [], | ||
function(t, results) { | ||
var rows = results.rows; | ||
var length = rows.length; | ||
var promise = new Promise(function(resolve, reject) { | ||
self.ready().then(function() { | ||
// The localStorage API doesn't return undefined values in an | ||
// "expected" way, so undefined is always cast to null in all | ||
// drivers. See: https://github.com/mozilla/localForage/pull/42 | ||
if (value === undefined) { | ||
value = null; | ||
} | ||
for (var i = 0; i < length; i++) { | ||
var item = rows.item(i); | ||
var result = item.value; | ||
// Save the original value to pass to the callback. | ||
var originalValue = value; | ||
// Check to see if this is serialized content | ||
// we need to unpack. | ||
if (result) { | ||
result = dbInfo.serializer.deserialize(result); | ||
} | ||
result = iterator(result, item.key, i + 1); | ||
// void(0) prevents problems with redefinition | ||
// of `undefined`. | ||
if (result !== void(0)) { | ||
resolve(result); | ||
return; | ||
} | ||
} | ||
resolve(); | ||
var dbInfo = self._dbInfo; | ||
dbInfo.serializer.serialize(value, function(value, error) { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
dbInfo.db.transaction(function(t) { | ||
t.executeSql('INSERT OR REPLACE INTO ' + | ||
dbInfo.storeName + | ||
' (key, value) VALUES (?, ?)', | ||
[key, value], function() { | ||
resolve(originalValue); | ||
}, function(t, error) { | ||
reject(error); | ||
}); | ||
}); | ||
}).catch(reject); | ||
}); | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
function setItem(key, value, callback) { | ||
var self = this; | ||
// Cast the key to a string, as that's all we can set as a key. | ||
if (typeof key !== 'string') { | ||
globalObject.console.warn(key + | ||
' used as a key, but it is not a string.'); | ||
key = String(key); | ||
} | ||
var promise = new Promise(function(resolve, reject) { | ||
self.ready().then(function() { | ||
// The localStorage API doesn't return undefined values in an | ||
// "expected" way, so undefined is always cast to null in all | ||
// drivers. See: https://github.com/mozilla/localForage/pull/42 | ||
if (value === undefined) { | ||
value = null; | ||
}, function(sqlError) { | ||
// The transaction failed; check | ||
// to see if it's a quota error. | ||
if (sqlError.code === sqlError.QUOTA_ERR) { | ||
// We reject the callback outright for now, but | ||
// it's worth trying to re-run the transaction. | ||
// Even if the user accepts the prompt to use | ||
// more storage on Safari, this error will | ||
// be called. | ||
// | ||
// TODO: Try to re-run the transaction. | ||
reject(sqlError); | ||
} | ||
}); | ||
} | ||
}); | ||
}).catch(reject); | ||
}); | ||
// Save the original value to pass to the callback. | ||
var originalValue = value; | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
var dbInfo = self._dbInfo; | ||
dbInfo.serializer.serialize(value, function(value, error) { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
dbInfo.db.transaction(function(t) { | ||
t.executeSql('INSERT OR REPLACE INTO ' + | ||
dbInfo.storeName + | ||
' (key, value) VALUES (?, ?)', | ||
[key, value], function() { | ||
resolve(originalValue); | ||
}, function(t, error) { | ||
reject(error); | ||
}); | ||
}, function(sqlError) { | ||
// The transaction failed; check | ||
// to see if it's a quota error. | ||
if (sqlError.code === sqlError.QUOTA_ERR) { | ||
// We reject the callback outright for now, but | ||
// it's worth trying to re-run the transaction. | ||
// Even if the user accepts the prompt to use | ||
// more storage on Safari, this error will | ||
// be called. | ||
// | ||
// TODO: Try to re-run the transaction. | ||
reject(sqlError); | ||
} | ||
}); | ||
} | ||
}); | ||
}).catch(reject); | ||
}); | ||
function removeItem(key, callback) { | ||
var self = this; | ||
executeCallback(promise, callback); | ||
return promise; | ||
// Cast the key to a string, as that's all we can set as a key. | ||
if (typeof key !== 'string') { | ||
console.warn(key + | ||
' used as a key, but it is not a string.'); | ||
key = String(key); | ||
} | ||
function removeItem(key, callback) { | ||
var self = this; | ||
var promise = new Promise(function(resolve, reject) { | ||
self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
dbInfo.db.transaction(function(t) { | ||
t.executeSql('DELETE FROM ' + dbInfo.storeName + | ||
' WHERE key = ?', [key], | ||
function() { | ||
resolve(); | ||
}, function(t, error) { | ||
// Cast the key to a string, as that's all we can set as a key. | ||
if (typeof key !== 'string') { | ||
globalObject.console.warn(key + | ||
' used as a key, but it is not a string.'); | ||
key = String(key); | ||
} | ||
var promise = new Promise(function(resolve, reject) { | ||
self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
dbInfo.db.transaction(function(t) { | ||
t.executeSql('DELETE FROM ' + dbInfo.storeName + | ||
' WHERE key = ?', [key], | ||
function() { | ||
resolve(); | ||
}, function(t, error) { | ||
reject(error); | ||
}); | ||
reject(error); | ||
}); | ||
}).catch(reject); | ||
}); | ||
}); | ||
}).catch(reject); | ||
}); | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
// Deletes every item in the table. | ||
// TODO: Find out if this resets the AUTO_INCREMENT number. | ||
function clear(callback) { | ||
var self = this; | ||
// Deletes every item in the table. | ||
// TODO: Find out if this resets the AUTO_INCREMENT number. | ||
function clear(callback) { | ||
var self = this; | ||
var promise = new Promise(function(resolve, reject) { | ||
self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
dbInfo.db.transaction(function(t) { | ||
t.executeSql('DELETE FROM ' + dbInfo.storeName, [], | ||
function() { | ||
resolve(); | ||
}, function(t, error) { | ||
reject(error); | ||
}); | ||
var promise = new Promise(function(resolve, reject) { | ||
self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
dbInfo.db.transaction(function(t) { | ||
t.executeSql('DELETE FROM ' + dbInfo.storeName, [], | ||
function() { | ||
resolve(); | ||
}, function(t, error) { | ||
reject(error); | ||
}); | ||
}).catch(reject); | ||
}); | ||
}); | ||
}).catch(reject); | ||
}); | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
// Does a simple `COUNT(key)` to get the number of items stored in | ||
// localForage. | ||
function length(callback) { | ||
var self = this; | ||
// Does a simple `COUNT(key)` to get the number of items stored in | ||
// localForage. | ||
function length(callback) { | ||
var self = this; | ||
var promise = new Promise(function(resolve, reject) { | ||
self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
dbInfo.db.transaction(function(t) { | ||
// Ahhh, SQL makes this one soooooo easy. | ||
t.executeSql('SELECT COUNT(key) as c FROM ' + | ||
dbInfo.storeName, [], function(t, results) { | ||
var result = results.rows.item(0).c; | ||
var promise = new Promise(function(resolve, reject) { | ||
self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
dbInfo.db.transaction(function(t) { | ||
// Ahhh, SQL makes this one soooooo easy. | ||
t.executeSql('SELECT COUNT(key) as c FROM ' + | ||
dbInfo.storeName, [], function(t, results) { | ||
var result = results.rows.item(0).c; | ||
resolve(result); | ||
}, function(t, error) { | ||
resolve(result); | ||
}, function(t, error) { | ||
reject(error); | ||
}); | ||
reject(error); | ||
}); | ||
}).catch(reject); | ||
}); | ||
}); | ||
}).catch(reject); | ||
}); | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
// Return the key located at key index X; essentially gets the key from a | ||
// `WHERE id = ?`. This is the most efficient way I can think to implement | ||
// this rarely-used (in my experience) part of the API, but it can seem | ||
// inconsistent, because we do `INSERT OR REPLACE INTO` on `setItem()`, so | ||
// the ID of each key will change every time it's updated. Perhaps a stored | ||
// procedure for the `setItem()` SQL would solve this problem? | ||
// TODO: Don't change ID on `setItem()`. | ||
function key(n, callback) { | ||
var self = this; | ||
// Return the key located at key index X; essentially gets the key from a | ||
// `WHERE id = ?`. This is the most efficient way I can think to implement | ||
// this rarely-used (in my experience) part of the API, but it can seem | ||
// inconsistent, because we do `INSERT OR REPLACE INTO` on `setItem()`, so | ||
// the ID of each key will change every time it's updated. Perhaps a stored | ||
// procedure for the `setItem()` SQL would solve this problem? | ||
// TODO: Don't change ID on `setItem()`. | ||
function key(n, callback) { | ||
var self = this; | ||
var promise = new Promise(function(resolve, reject) { | ||
self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
dbInfo.db.transaction(function(t) { | ||
t.executeSql('SELECT key FROM ' + dbInfo.storeName + | ||
' WHERE id = ? LIMIT 1', [n + 1], | ||
function(t, results) { | ||
var result = results.rows.length ? | ||
results.rows.item(0).key : null; | ||
resolve(result); | ||
}, function(t, error) { | ||
reject(error); | ||
}); | ||
var promise = new Promise(function(resolve, reject) { | ||
self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
dbInfo.db.transaction(function(t) { | ||
t.executeSql('SELECT key FROM ' + dbInfo.storeName + | ||
' WHERE id = ? LIMIT 1', [n + 1], | ||
function(t, results) { | ||
var result = results.rows.length ? | ||
results.rows.item(0).key : null; | ||
resolve(result); | ||
}, function(t, error) { | ||
reject(error); | ||
}); | ||
}).catch(reject); | ||
}); | ||
}); | ||
}).catch(reject); | ||
}); | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
function keys(callback) { | ||
var self = this; | ||
function keys(callback) { | ||
var self = this; | ||
var promise = new Promise(function(resolve, reject) { | ||
self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
dbInfo.db.transaction(function(t) { | ||
t.executeSql('SELECT key FROM ' + dbInfo.storeName, [], | ||
function(t, results) { | ||
var keys = []; | ||
var promise = new Promise(function(resolve, reject) { | ||
self.ready().then(function() { | ||
var dbInfo = self._dbInfo; | ||
dbInfo.db.transaction(function(t) { | ||
t.executeSql('SELECT key FROM ' + dbInfo.storeName, [], | ||
function(t, results) { | ||
var keys = []; | ||
for (var i = 0; i < results.rows.length; i++) { | ||
keys.push(results.rows.item(i).key); | ||
} | ||
for (var i = 0; i < results.rows.length; i++) { | ||
keys.push(results.rows.item(i).key); | ||
} | ||
resolve(keys); | ||
}, function(t, error) { | ||
resolve(keys); | ||
}, function(t, error) { | ||
reject(error); | ||
}); | ||
reject(error); | ||
}); | ||
}).catch(reject); | ||
}); | ||
}); | ||
}).catch(reject); | ||
}); | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
function executeCallback(promise, callback) { | ||
if (callback) { | ||
promise.then(function(result) { | ||
callback(null, result); | ||
}, function(error) { | ||
callback(error); | ||
}); | ||
} | ||
} | ||
var webSQLStorage = { | ||
_driver: 'webSQLStorage', | ||
_initStorage: _initStorage, | ||
iterate: iterate, | ||
getItem: getItem, | ||
setItem: setItem, | ||
removeItem: removeItem, | ||
clear: clear, | ||
length: length, | ||
key: key, | ||
keys: keys | ||
}; | ||
var webSQLStorage = { | ||
_driver: 'webSQLStorage', | ||
_initStorage: _initStorage, | ||
iterate: iterate, | ||
getItem: getItem, | ||
setItem: setItem, | ||
removeItem: removeItem, | ||
clear: clear, | ||
length: length, | ||
key: key, | ||
keys: keys | ||
}; | ||
return webSQLStorage; | ||
})(typeof window !== 'undefined' ? window : self); | ||
export default webSQLStorage; |
@@ -1,131 +0,85 @@ | ||
var localForage = (function(globalObject) { | ||
'use strict'; | ||
import isIndexedDBValid from './utils/isIndexedDBValid'; | ||
import isWebSQLValid from './utils/isWebSQLValid'; | ||
import isLocalStorageValid from './utils/isLocalStorageValid'; | ||
import idbDriver from './drivers/indexeddb'; | ||
import websqlDriver from './drivers/websql'; | ||
import localstorageDriver from './drivers/localstorage'; | ||
import serializer from './utils/serializer'; | ||
import Promise from './utils/promise'; | ||
import executeTwoCallbacks from './utils/executeTwoCallbacks'; | ||
// Custom drivers are stored here when `defineDriver()` is called. | ||
// They are shared across all instances of localForage. | ||
var CustomDrivers = {}; | ||
// Custom drivers are stored here when `defineDriver()` is called. | ||
// They are shared across all instances of localForage. | ||
var CustomDrivers = {}; | ||
var DriverType = { | ||
INDEXEDDB: 'asyncStorage', | ||
LOCALSTORAGE: 'localStorageWrapper', | ||
WEBSQL: 'webSQLStorage' | ||
}; | ||
var DriverType = { | ||
INDEXEDDB: 'asyncStorage', | ||
LOCALSTORAGE: 'localStorageWrapper', | ||
WEBSQL: 'webSQLStorage' | ||
}; | ||
var DefaultDriverOrder = [ | ||
DriverType.INDEXEDDB, | ||
DriverType.WEBSQL, | ||
DriverType.LOCALSTORAGE | ||
]; | ||
var DefaultDriverOrder = [ | ||
DriverType.INDEXEDDB, | ||
DriverType.WEBSQL, | ||
DriverType.LOCALSTORAGE | ||
]; | ||
var LibraryMethods = [ | ||
'clear', | ||
'getItem', | ||
'iterate', | ||
'key', | ||
'keys', | ||
'length', | ||
'removeItem', | ||
'setItem' | ||
]; | ||
var LibraryMethods = [ | ||
'clear', | ||
'getItem', | ||
'iterate', | ||
'key', | ||
'keys', | ||
'length', | ||
'removeItem', | ||
'setItem' | ||
]; | ||
var DefaultConfig = { | ||
description: '', | ||
driver: DefaultDriverOrder.slice(), | ||
name: 'localforage', | ||
// Default DB size is _JUST UNDER_ 5MB, as it's the highest size | ||
// we can use without a prompt. | ||
size: 4980736, | ||
storeName: 'keyvaluepairs', | ||
version: 1.0 | ||
}; | ||
var DefaultConfig = { | ||
description: '', | ||
driver: DefaultDriverOrder.slice(), | ||
name: 'localforage', | ||
// Default DB size is _JUST UNDER_ 5MB, as it's the highest size | ||
// we can use without a prompt. | ||
size: 4980736, | ||
storeName: 'keyvaluepairs', | ||
version: 1.0 | ||
}; | ||
var driverSupport = (function(self) { | ||
var result = {}; | ||
var driverSupport = {}; | ||
// Check to see if IndexedDB is available and if it is the latest | ||
// implementation; it's our preferred backend library. We use "_spec_test" | ||
// as the name of the database because it's not the one we'll operate on, | ||
// but it's useful to make sure its using the right spec. | ||
// See: https://github.com/mozilla/localForage/issues/128 | ||
driverSupport[DriverType.INDEXEDDB] = isIndexedDBValid(); | ||
// Check to see if IndexedDB is available and if it is the latest | ||
// implementation; it's our preferred backend library. We use "_spec_test" | ||
// as the name of the database because it's not the one we'll operate on, | ||
// but it's useful to make sure its using the right spec. | ||
// See: https://github.com/mozilla/localForage/issues/128 | ||
result[DriverType.INDEXEDDB] = !!(function() { | ||
try { | ||
// Initialize IndexedDB; fall back to vendor-prefixed versions | ||
// if needed. | ||
var indexedDB = indexedDB || self.indexedDB || self.webkitIndexedDB || | ||
self.mozIndexedDB || self.OIndexedDB || | ||
self.msIndexedDB; | ||
// 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 test for openDatabase because IE Mobile identifies itself | ||
// as Safari. Oh the lulz... | ||
if (typeof self.openDatabase !== 'undefined' && self.navigator && | ||
self.navigator.userAgent && | ||
/Safari/.test(self.navigator.userAgent) && | ||
!/Chrome/.test(self.navigator.userAgent)) { | ||
return false; | ||
} | ||
driverSupport[DriverType.WEBSQL] = isWebSQLValid(); | ||
return indexedDB && | ||
typeof indexedDB.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 self.IDBKeyRange !== 'undefined'; | ||
} catch (e) { | ||
return false; | ||
} | ||
})(); | ||
driverSupport[DriverType.LOCALSTORAGE] = isLocalStorageValid(); | ||
result[DriverType.WEBSQL] = !!(function() { | ||
try { | ||
return self.openDatabase; | ||
} catch (e) { | ||
return false; | ||
} | ||
})(); | ||
result[DriverType.LOCALSTORAGE] = !!(function() { | ||
try { | ||
return (self.localStorage && | ||
('setItem' in self.localStorage) && | ||
(self.localStorage.setItem)); | ||
} catch (e) { | ||
return false; | ||
} | ||
})(); | ||
return result; | ||
})(globalObject); | ||
var isArray = Array.isArray || function(arg) { | ||
var isArray = Array.isArray || function(arg) { | ||
return Object.prototype.toString.call(arg) === '[object Array]'; | ||
}; | ||
function callWhenReady(localForageInstance, libraryMethod) { | ||
localForageInstance[libraryMethod] = function() { | ||
var _args = arguments; | ||
return localForageInstance.ready().then(function() { | ||
return localForageInstance[libraryMethod].apply(localForageInstance, _args); | ||
}); | ||
}; | ||
} | ||
function callWhenReady(localForageInstance, libraryMethod) { | ||
localForageInstance[libraryMethod] = function() { | ||
var _args = arguments; | ||
return localForageInstance.ready().then(function() { | ||
return localForageInstance[libraryMethod].apply(localForageInstance, _args); | ||
}); | ||
}; | ||
} | ||
function extend() { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var arg = arguments[i]; | ||
function extend() { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var arg = arguments[i]; | ||
if (arg) { | ||
for (var key in arg) { | ||
if (arg.hasOwnProperty(key)) { | ||
if (isArray(arg[key])) { | ||
arguments[0][key] = arg[key].slice(); | ||
} else { | ||
arguments[0][key] = arg[key]; | ||
} | ||
if (arg) { | ||
for (var key in arg) { | ||
if (arg.hasOwnProperty(key)) { | ||
if (isArray(arg[key])) { | ||
arguments[0][key] = arg[key].slice(); | ||
} else { | ||
arguments[0][key] = arg[key]; | ||
} | ||
@@ -135,297 +89,290 @@ } | ||
} | ||
return arguments[0]; | ||
} | ||
function isLibraryDriver(driverName) { | ||
for (var driver in DriverType) { | ||
if (DriverType.hasOwnProperty(driver) && | ||
DriverType[driver] === driverName) { | ||
return true; | ||
} | ||
return arguments[0]; | ||
} | ||
function isLibraryDriver(driverName) { | ||
for (var driver in DriverType) { | ||
if (DriverType.hasOwnProperty(driver) && | ||
DriverType[driver] === driverName) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
class LocalForage { | ||
constructor(options) { | ||
this.INDEXEDDB = DriverType.INDEXEDDB; | ||
this.LOCALSTORAGE = DriverType.LOCALSTORAGE; | ||
this.WEBSQL = DriverType.WEBSQL; | ||
return false; | ||
} | ||
this._defaultConfig = extend({}, DefaultConfig); | ||
this._config = extend({}, this._defaultConfig, options); | ||
this._driverSet = null; | ||
this._initDriver = null; | ||
this._ready = false; | ||
this._dbInfo = null; | ||
class LocalForage { | ||
constructor(options) { | ||
this.INDEXEDDB = DriverType.INDEXEDDB; | ||
this.LOCALSTORAGE = DriverType.LOCALSTORAGE; | ||
this.WEBSQL = DriverType.WEBSQL; | ||
this._wrapLibraryMethodsWithReady(); | ||
this.setDriver(this._config.driver); | ||
} | ||
this._defaultConfig = extend({}, DefaultConfig); | ||
this._config = extend({}, this._defaultConfig, options); | ||
this._driverSet = null; | ||
this._initDriver = null; | ||
this._ready = false; | ||
this._dbInfo = null; | ||
// Set any config values for localForage; can be called anytime before | ||
// the first API call (e.g. `getItem`, `setItem`). | ||
// We loop through options so we don't overwrite existing config | ||
// values. | ||
config(options) { | ||
// If the options argument is an object, we use it to set values. | ||
// Otherwise, we return either a specified config value or all | ||
// config values. | ||
if (typeof(options) === 'object') { | ||
// If localforage is ready and fully initialized, we can't set | ||
// any new configuration values. Instead, we return an error. | ||
if (this._ready) { | ||
return new Error("Can't call config() after localforage " + | ||
'has been used.'); | ||
} | ||
this._wrapLibraryMethodsWithReady(); | ||
this.setDriver(this._config.driver); | ||
} | ||
for (var i in options) { | ||
if (i === 'storeName') { | ||
options[i] = options[i].replace(/\W/g, '_'); | ||
} | ||
// Set any config values for localForage; can be called anytime before | ||
// the first API call (e.g. `getItem`, `setItem`). | ||
// We loop through options so we don't overwrite existing config | ||
// values. | ||
config(options) { | ||
// If the options argument is an object, we use it to set values. | ||
// Otherwise, we return either a specified config value or all | ||
// config values. | ||
if (typeof(options) === 'object') { | ||
// If localforage is ready and fully initialized, we can't set | ||
// any new configuration values. Instead, we return an error. | ||
if (this._ready) { | ||
return new Error("Can't call config() after localforage " + | ||
'has been used.'); | ||
} | ||
this._config[i] = options[i]; | ||
for (var i in options) { | ||
if (i === 'storeName') { | ||
options[i] = options[i].replace(/\W/g, '_'); | ||
} | ||
// 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); | ||
} | ||
this._config[i] = options[i]; | ||
} | ||
return true; | ||
} else if (typeof(options) === 'string') { | ||
return this._config[options]; | ||
} else { | ||
return this._config; | ||
// 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 true; | ||
} else if (typeof(options) === 'string') { | ||
return this._config[options]; | ||
} else { | ||
return this._config; | ||
} | ||
} | ||
// Used to define a custom driver, shared across all instances of | ||
// localForage. | ||
defineDriver(driverObject, callback, errorCallback) { | ||
var promise = new Promise(function(resolve, reject) { | ||
try { | ||
var driverName = driverObject._driver; | ||
var complianceError = new Error( | ||
'Custom driver not compliant; see ' + | ||
'https://mozilla.github.io/localForage/#definedriver' | ||
); | ||
var namingError = new Error( | ||
'Custom driver name already in use: ' + driverObject._driver | ||
); | ||
// Used to define a custom driver, shared across all instances of | ||
// localForage. | ||
defineDriver(driverObject, callback, errorCallback) { | ||
var promise = new Promise(function(resolve, reject) { | ||
try { | ||
var driverName = driverObject._driver; | ||
var complianceError = new Error( | ||
'Custom driver not compliant; see ' + | ||
'https://mozilla.github.io/localForage/#definedriver' | ||
); | ||
var namingError = new Error( | ||
'Custom driver name already in use: ' + driverObject._driver | ||
); | ||
// A driver name should be defined and not overlap with the | ||
// library-defined, default drivers. | ||
if (!driverObject._driver) { | ||
// A driver name should be defined and not overlap with the | ||
// library-defined, default drivers. | ||
if (!driverObject._driver) { | ||
reject(complianceError); | ||
return; | ||
} | ||
if (isLibraryDriver(driverObject._driver)) { | ||
reject(namingError); | ||
return; | ||
} | ||
var customDriverMethods = LibraryMethods.concat('_initStorage'); | ||
for (var i = 0; i < customDriverMethods.length; i++) { | ||
var customDriverMethod = customDriverMethods[i]; | ||
if (!customDriverMethod || !driverObject[customDriverMethod] || | ||
typeof driverObject[customDriverMethod] !== 'function') { | ||
reject(complianceError); | ||
return; | ||
} | ||
if (isLibraryDriver(driverObject._driver)) { | ||
reject(namingError); | ||
return; | ||
} | ||
} | ||
var customDriverMethods = LibraryMethods.concat('_initStorage'); | ||
for (var i = 0; i < customDriverMethods.length; i++) { | ||
var customDriverMethod = customDriverMethods[i]; | ||
if (!customDriverMethod || | ||
!driverObject[customDriverMethod] || | ||
typeof driverObject[customDriverMethod] !== 'function') { | ||
reject(complianceError); | ||
return; | ||
} | ||
var supportPromise = Promise.resolve(true); | ||
if ('_support' in driverObject) { | ||
if (driverObject._support && typeof driverObject._support === 'function') { | ||
supportPromise = driverObject._support(); | ||
} else { | ||
supportPromise = Promise.resolve(!!driverObject._support); | ||
} | ||
} | ||
var supportPromise = Promise.resolve(true); | ||
if ('_support' in driverObject) { | ||
if (driverObject._support && typeof driverObject._support === 'function') { | ||
supportPromise = driverObject._support(); | ||
} else { | ||
supportPromise = Promise.resolve(!!driverObject._support); | ||
} | ||
} | ||
supportPromise.then(function(supportResult) { | ||
driverSupport[driverName] = supportResult; | ||
CustomDrivers[driverName] = driverObject; | ||
resolve(); | ||
}, reject); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
supportPromise.then(function(supportResult) { | ||
driverSupport[driverName] = supportResult; | ||
CustomDrivers[driverName] = driverObject; | ||
resolve(); | ||
}, reject); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
executeTwoCallbacks(promise, callback, errorCallback); | ||
return promise; | ||
} | ||
promise.then(callback, errorCallback); | ||
return promise; | ||
} | ||
driver() { | ||
return this._driver || null; | ||
} | ||
driver() { | ||
return this._driver || null; | ||
} | ||
getDriver(driverName, callback, errorCallback) { | ||
var self = this; | ||
var getDriverPromise = (function() { | ||
if (isLibraryDriver(driverName)) { | ||
switch (driverName) { | ||
case self.INDEXEDDB: | ||
return System.import('./drivers/indexeddb'); | ||
case self.LOCALSTORAGE: | ||
return System.import('./drivers/localstorage'); | ||
case self.WEBSQL: | ||
return System.import('./drivers/websql'); | ||
} | ||
} else if (CustomDrivers[driverName]) { | ||
return Promise.resolve(CustomDrivers[driverName]); | ||
getDriver(driverName, callback, errorCallback) { | ||
var self = this; | ||
var getDriverPromise = Promise.resolve().then(() => { | ||
if (isLibraryDriver(driverName)) { | ||
switch (driverName) { | ||
case self.INDEXEDDB: | ||
return idbDriver; | ||
case self.LOCALSTORAGE: | ||
return localstorageDriver; | ||
case self.WEBSQL: | ||
return websqlDriver; | ||
} | ||
} else if (CustomDrivers[driverName]) { | ||
return CustomDrivers[driverName]; | ||
} else { | ||
throw new Error('Driver not found.'); | ||
} | ||
}); | ||
executeTwoCallbacks(getDriverPromise, callback, errorCallback); | ||
return getDriverPromise; | ||
} | ||
return Promise.reject(new Error('Driver not found.')); | ||
})(); | ||
getSerializer(callback) { | ||
var serializerPromise = Promise.resolve(serializer); | ||
executeTwoCallbacks(serializerPromise, callback); | ||
return serializerPromise; | ||
} | ||
getDriverPromise.then(callback, errorCallback); | ||
return getDriverPromise; | ||
} | ||
ready(callback) { | ||
var self = this; | ||
getSerializer(callback) { | ||
var serializerPromise = System.import('./utils/serializer'); | ||
if (callback && typeof callback === 'function') { | ||
serializerPromise.then(function(result) { | ||
callback(result); | ||
}); | ||
var promise = self._driverSet.then(() => { | ||
if (self._ready === null) { | ||
self._ready = self._initDriver(); | ||
} | ||
return serializerPromise; | ||
} | ||
ready(callback) { | ||
var self = this; | ||
return self._ready; | ||
}); | ||
var promise = self._driverSet.then(() => { | ||
if (self._ready === null) { | ||
self._ready = self._initDriver(); | ||
} | ||
executeTwoCallbacks(promise, callback, callback); | ||
return promise; | ||
} | ||
return self._ready; | ||
}); | ||
setDriver(drivers, callback, errorCallback) { | ||
var self = this; | ||
promise.then(callback, callback); | ||
return promise; | ||
if (!isArray(drivers)) { | ||
drivers = [drivers]; | ||
} | ||
setDriver(drivers, callback, errorCallback) { | ||
var self = this; | ||
var supportedDrivers = this._getSupportedDrivers(drivers); | ||
if (!isArray(drivers)) { | ||
drivers = [drivers]; | ||
} | ||
function setDriverToConfig() { | ||
self._config.driver = self.driver(); | ||
} | ||
var supportedDrivers = this._getSupportedDrivers(drivers); | ||
function initDriver(supportedDrivers) { | ||
return function() { | ||
var currentDriverIndex = 0; | ||
function setDriverToConfig() { | ||
self._config.driver = self.driver(); | ||
} | ||
function driverPromiseLoop() { | ||
while (currentDriverIndex < supportedDrivers.length) { | ||
var driverName = supportedDrivers[currentDriverIndex]; | ||
currentDriverIndex++; | ||
function initDriver(supportedDrivers) { | ||
return function() { | ||
var currentDriverIndex = 0; | ||
self._dbInfo = null; | ||
self._ready = null; | ||
function driverPromiseLoop() { | ||
while (currentDriverIndex < supportedDrivers.length) { | ||
var driverName = supportedDrivers[currentDriverIndex]; | ||
currentDriverIndex++; | ||
return self.getDriver(driverName) | ||
.then(driver => { | ||
self._extend(driver); | ||
setDriverToConfig(); | ||
self._dbInfo = null; | ||
self._ready = null; | ||
return self.getDriver(driverName) | ||
.then(driver => { | ||
self._extend(driver); | ||
setDriverToConfig(); | ||
self._ready = self._initStorage(self._config); | ||
return self._ready; | ||
}) | ||
.catch(driverPromiseLoop); | ||
} | ||
setDriverToConfig(); | ||
var error = new Error('No available storage method found.'); | ||
self._driverSet = Promise.reject(error); | ||
return self._driverSet; | ||
self._ready = self._initStorage(self._config); | ||
return self._ready; | ||
}) | ||
.catch(driverPromiseLoop); | ||
} | ||
return driverPromiseLoop(); | ||
}; | ||
} | ||
setDriverToConfig(); | ||
var error = new Error('No available storage method found.'); | ||
self._driverSet = Promise.reject(error); | ||
return self._driverSet; | ||
} | ||
// There might be a driver initialization in progress | ||
// so wait for it to finish in order to avoid a possible | ||
// race condition to set _dbInfo | ||
var oldDriverSetDone = this._driverSet !== null ? | ||
this._driverSet.catch(() => Promise.resolve()) : | ||
Promise.resolve(); | ||
return driverPromiseLoop(); | ||
}; | ||
} | ||
this._driverSet = oldDriverSetDone.then(() => { | ||
var driverName = supportedDrivers[0]; | ||
self._dbInfo = null; | ||
self._ready = null; | ||
// There might be a driver initialization in progress | ||
// so wait for it to finish in order to avoid a possible | ||
// race condition to set _dbInfo | ||
var oldDriverSetDone = this._driverSet !== null ? | ||
this._driverSet.catch(() => Promise.resolve()) : | ||
Promise.resolve(); | ||
return self.getDriver(driverName) | ||
.then(driver => { | ||
self._driver = driver._driver; | ||
setDriverToConfig(); | ||
self._wrapLibraryMethodsWithReady(); | ||
self._initDriver = initDriver(supportedDrivers); | ||
}); | ||
}).catch(() => { | ||
setDriverToConfig(); | ||
var error = new Error('No available storage method found.'); | ||
self._driverSet = Promise.reject(error); | ||
return self._driverSet; | ||
}); | ||
this._driverSet = oldDriverSetDone.then(() => { | ||
var driverName = supportedDrivers[0]; | ||
self._dbInfo = null; | ||
self._ready = null; | ||
this._driverSet.then(callback, errorCallback); | ||
return this._driverSet; | ||
} | ||
return self.getDriver(driverName) | ||
.then(driver => { | ||
self._driver = driver._driver; | ||
setDriverToConfig(); | ||
self._wrapLibraryMethodsWithReady(); | ||
self._initDriver = initDriver(supportedDrivers); | ||
}); | ||
}).catch(() => { | ||
setDriverToConfig(); | ||
var error = new Error('No available storage method found.'); | ||
self._driverSet = Promise.reject(error); | ||
return self._driverSet; | ||
}); | ||
supports(driverName) { | ||
return !!driverSupport[driverName]; | ||
} | ||
executeTwoCallbacks(this._driverSet, callback, errorCallback); | ||
return this._driverSet; | ||
} | ||
_extend(libraryMethodsAndProperties) { | ||
extend(this, libraryMethodsAndProperties); | ||
} | ||
supports(driverName) { | ||
return !!driverSupport[driverName]; | ||
} | ||
_getSupportedDrivers(drivers) { | ||
var supportedDrivers = []; | ||
for (var i = 0, len = drivers.length; i < len; i++) { | ||
var driverName = drivers[i]; | ||
if (this.supports(driverName)) { | ||
supportedDrivers.push(driverName); | ||
} | ||
} | ||
return supportedDrivers; | ||
} | ||
_extend(libraryMethodsAndProperties) { | ||
extend(this, libraryMethodsAndProperties); | ||
} | ||
_wrapLibraryMethodsWithReady() { | ||
// Add a stub for each driver API method that delays the call to the | ||
// corresponding driver method until localForage is ready. These stubs | ||
// will be replaced by the driver methods as soon as the driver is | ||
// loaded, so there is no performance impact. | ||
for (var i = 0; i < LibraryMethods.length; i++) { | ||
callWhenReady(this, LibraryMethods[i]); | ||
_getSupportedDrivers(drivers) { | ||
var supportedDrivers = []; | ||
for (var i = 0, len = drivers.length; i < len; i++) { | ||
var driverName = drivers[i]; | ||
if (this.supports(driverName)) { | ||
supportedDrivers.push(driverName); | ||
} | ||
} | ||
return supportedDrivers; | ||
} | ||
createInstance(options) { | ||
return new LocalForage(options); | ||
_wrapLibraryMethodsWithReady() { | ||
// Add a stub for each driver API method that delays the call to the | ||
// corresponding driver method until localForage is ready. These stubs | ||
// will be replaced by the driver methods as soon as the driver is | ||
// loaded, so there is no performance impact. | ||
for (var i = 0; i < LibraryMethods.length; i++) { | ||
callWhenReady(this, LibraryMethods[i]); | ||
} | ||
} | ||
// The actual localForage object that we expose as a module or via a | ||
// global. It's extended by pulling in one of our other libraries. | ||
return new LocalForage(); | ||
})(typeof window !== 'undefined' ? window : self); | ||
export default localForage; | ||
createInstance(options) { | ||
return new LocalForage(options); | ||
} | ||
} | ||
// The actual localForage object that we expose as a module or via a | ||
// global. It's extended by pulling in one of our other libraries. | ||
export default new LocalForage(); |
@@ -1,264 +0,233 @@ | ||
var localforageSerializer = (function(globalObject) { | ||
'use strict'; | ||
import createBlob from './createBlob'; | ||
// Sadly, the best way to save binary data in WebSQL/localStorage is serializing | ||
// it to Base64, so this is how we store it to prevent very strange errors with less | ||
// verbose ways of binary <-> string data storage. | ||
var BASE_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; | ||
// Sadly, the best way to save binary data in WebSQL/localStorage is serializing | ||
// it to Base64, so this is how we store it to prevent very strange errors with less | ||
// verbose ways of binary <-> string data storage. | ||
var BASE_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; | ||
var BLOB_TYPE_PREFIX = '~~local_forage_type~'; | ||
var BLOB_TYPE_PREFIX_REGEX = /^~~local_forage_type~([^~]+)~/; | ||
var BLOB_TYPE_PREFIX = '~~local_forage_type~'; | ||
var BLOB_TYPE_PREFIX_REGEX = /^~~local_forage_type~([^~]+)~/; | ||
var SERIALIZED_MARKER = '__lfsc__:'; | ||
var SERIALIZED_MARKER_LENGTH = SERIALIZED_MARKER.length; | ||
var SERIALIZED_MARKER = '__lfsc__:'; | ||
var SERIALIZED_MARKER_LENGTH = SERIALIZED_MARKER.length; | ||
// OMG the serializations! | ||
var TYPE_ARRAYBUFFER = 'arbf'; | ||
var TYPE_BLOB = 'blob'; | ||
var TYPE_INT8ARRAY = 'si08'; | ||
var TYPE_UINT8ARRAY = 'ui08'; | ||
var TYPE_UINT8CLAMPEDARRAY = 'uic8'; | ||
var TYPE_INT16ARRAY = 'si16'; | ||
var TYPE_INT32ARRAY = 'si32'; | ||
var TYPE_UINT16ARRAY = 'ur16'; | ||
var TYPE_UINT32ARRAY = 'ui32'; | ||
var TYPE_FLOAT32ARRAY = 'fl32'; | ||
var TYPE_FLOAT64ARRAY = 'fl64'; | ||
var TYPE_SERIALIZED_MARKER_LENGTH = SERIALIZED_MARKER_LENGTH + | ||
TYPE_ARRAYBUFFER.length; | ||
// OMG the serializations! | ||
var TYPE_ARRAYBUFFER = 'arbf'; | ||
var TYPE_BLOB = 'blob'; | ||
var TYPE_INT8ARRAY = 'si08'; | ||
var TYPE_UINT8ARRAY = 'ui08'; | ||
var TYPE_UINT8CLAMPEDARRAY = 'uic8'; | ||
var TYPE_INT16ARRAY = 'si16'; | ||
var TYPE_INT32ARRAY = 'si32'; | ||
var TYPE_UINT16ARRAY = 'ur16'; | ||
var TYPE_UINT32ARRAY = 'ui32'; | ||
var TYPE_FLOAT32ARRAY = 'fl32'; | ||
var TYPE_FLOAT64ARRAY = 'fl64'; | ||
var TYPE_SERIALIZED_MARKER_LENGTH = SERIALIZED_MARKER_LENGTH + | ||
TYPE_ARRAYBUFFER.length; | ||
// Abstracts constructing a Blob object, so it also works in older | ||
// browsers that don't support the native Blob constructor. (i.e. | ||
// old QtWebKit versions, at least). | ||
function _createBlob(parts, properties) { | ||
parts = parts || []; | ||
properties = properties || {}; | ||
function stringToBuffer(serializedString) { | ||
// Fill the string into a ArrayBuffer. | ||
var bufferLength = serializedString.length * 0.75; | ||
var len = serializedString.length; | ||
var i; | ||
var p = 0; | ||
var encoded1, encoded2, encoded3, encoded4; | ||
try { | ||
return new Blob(parts, properties); | ||
} catch (err) { | ||
if (err.name !== 'TypeError') { | ||
throw err; | ||
} | ||
var BlobBuilder = globalObject.BlobBuilder || | ||
globalObject.MSBlobBuilder || | ||
globalObject.MozBlobBuilder || | ||
globalObject.WebKitBlobBuilder; | ||
var builder = new BlobBuilder(); | ||
for (var i = 0; i < parts.length; i += 1) { | ||
builder.append(parts[i]); | ||
} | ||
return builder.getBlob(properties.type); | ||
if (serializedString[serializedString.length - 1] === '=') { | ||
bufferLength--; | ||
if (serializedString[serializedString.length - 2] === '=') { | ||
bufferLength--; | ||
} | ||
} | ||
// Serialize a value, afterwards executing a callback (which usually | ||
// instructs the `setItem()` callback/promise to be executed). This is how | ||
// we store binary data with localStorage. | ||
function serialize(value, callback) { | ||
var valueString = ''; | ||
if (value) { | ||
valueString = value.toString(); | ||
} | ||
var buffer = new ArrayBuffer(bufferLength); | ||
var bytes = new Uint8Array(buffer); | ||
// 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]' || | ||
value.buffer && | ||
value.buffer.toString() === '[object ArrayBuffer]')) { | ||
// Convert binary arrays to a string and prefix the string with | ||
// a special marker. | ||
var buffer; | ||
var marker = SERIALIZED_MARKER; | ||
for (i = 0; i < len; i += 4) { | ||
encoded1 = BASE_CHARS.indexOf(serializedString[i]); | ||
encoded2 = BASE_CHARS.indexOf(serializedString[i + 1]); | ||
encoded3 = BASE_CHARS.indexOf(serializedString[i + 2]); | ||
encoded4 = BASE_CHARS.indexOf(serializedString[i + 3]); | ||
if (value instanceof ArrayBuffer) { | ||
buffer = value; | ||
marker += TYPE_ARRAYBUFFER; | ||
} else { | ||
buffer = value.buffer; | ||
/*jslint bitwise: true */ | ||
bytes[p++] = (encoded1 << 2) | (encoded2 >> 4); | ||
bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2); | ||
bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63); | ||
} | ||
return buffer; | ||
} | ||
if (valueString === '[object Int8Array]') { | ||
marker += TYPE_INT8ARRAY; | ||
} else if (valueString === '[object Uint8Array]') { | ||
marker += TYPE_UINT8ARRAY; | ||
} else if (valueString === '[object Uint8ClampedArray]') { | ||
marker += TYPE_UINT8CLAMPEDARRAY; | ||
} else if (valueString === '[object Int16Array]') { | ||
marker += TYPE_INT16ARRAY; | ||
} else if (valueString === '[object Uint16Array]') { | ||
marker += TYPE_UINT16ARRAY; | ||
} else if (valueString === '[object Int32Array]') { | ||
marker += TYPE_INT32ARRAY; | ||
} else if (valueString === '[object Uint32Array]') { | ||
marker += TYPE_UINT32ARRAY; | ||
} else if (valueString === '[object Float32Array]') { | ||
marker += TYPE_FLOAT32ARRAY; | ||
} else if (valueString === '[object Float64Array]') { | ||
marker += TYPE_FLOAT64ARRAY; | ||
} else { | ||
callback(new Error('Failed to get type for BinaryArray')); | ||
} | ||
} | ||
// Converts a buffer to a string to store, serialized, in the backend | ||
// storage library. | ||
function bufferToString(buffer) { | ||
// base64-arraybuffer | ||
var bytes = new Uint8Array(buffer); | ||
var base64String = ''; | ||
var i; | ||
callback(marker + bufferToString(buffer)); | ||
} else if (valueString === '[object Blob]') { | ||
// Conver the blob to a binaryArray and then to a string. | ||
var fileReader = new FileReader(); | ||
for (i = 0; i < bytes.length; i += 3) { | ||
/*jslint bitwise: true */ | ||
base64String += BASE_CHARS[bytes[i] >> 2]; | ||
base64String += BASE_CHARS[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)]; | ||
base64String += BASE_CHARS[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)]; | ||
base64String += BASE_CHARS[bytes[i + 2] & 63]; | ||
} | ||
fileReader.onload = function() { | ||
// Backwards-compatible prefix for the blob type. | ||
var str = BLOB_TYPE_PREFIX + value.type + '~' + | ||
bufferToString(this.result); | ||
if ((bytes.length % 3) === 2) { | ||
base64String = base64String.substring(0, base64String.length - 1) + '='; | ||
} else if (bytes.length % 3 === 1) { | ||
base64String = base64String.substring(0, base64String.length - 2) + '=='; | ||
} | ||
callback(SERIALIZED_MARKER + TYPE_BLOB + str); | ||
}; | ||
return base64String; | ||
} | ||
fileReader.readAsArrayBuffer(value); | ||
} else { | ||
try { | ||
callback(JSON.stringify(value)); | ||
} catch (e) { | ||
console.error("Couldn't convert value into a JSON string: ", | ||
value); | ||
callback(null, e); | ||
} | ||
} | ||
// Serialize a value, afterwards executing a callback (which usually | ||
// instructs the `setItem()` callback/promise to be executed). This is how | ||
// we store binary data with localStorage. | ||
function serialize(value, callback) { | ||
var valueString = ''; | ||
if (value) { | ||
valueString = value.toString(); | ||
} | ||
// Deserialize data we've inserted into a value column/field. We place | ||
// special markers into our strings to mark them as encoded; this isn't | ||
// as nice as a meta field, but it's the only sane thing we can do whilst | ||
// keeping localStorage support intact. | ||
// Cannot use `value instanceof ArrayBuffer` or such here, as these | ||
// checks fail when running the tests using casper.js... | ||
// | ||
// Oftentimes this will just deserialize JSON content, but if we have a | ||
// special marker (SERIALIZED_MARKER, defined above), we will extract | ||
// some kind of arraybuffer/binary data/typed array out of the string. | ||
function deserialize(value) { | ||
// If we haven't marked this string as being specially serialized (i.e. | ||
// something other than serialized JSON), we can just return it and be | ||
// done with it. | ||
if (value.substring(0, | ||
SERIALIZED_MARKER_LENGTH) !== SERIALIZED_MARKER) { | ||
return JSON.parse(value); | ||
} | ||
// TODO: See why those tests fail and use a better solution. | ||
if (value && (value.toString() === '[object ArrayBuffer]' || | ||
value.buffer && | ||
value.buffer.toString() === '[object ArrayBuffer]')) { | ||
// Convert binary arrays to a string and prefix the string with | ||
// a special marker. | ||
var buffer; | ||
var marker = SERIALIZED_MARKER; | ||
// The following code deals with deserializing some kind of Blob or | ||
// TypedArray. First we separate out the type of data we're dealing | ||
// with from the data itself. | ||
var serializedString = value.substring(TYPE_SERIALIZED_MARKER_LENGTH); | ||
var type = value.substring(SERIALIZED_MARKER_LENGTH, | ||
TYPE_SERIALIZED_MARKER_LENGTH); | ||
if (value instanceof ArrayBuffer) { | ||
buffer = value; | ||
marker += TYPE_ARRAYBUFFER; | ||
} else { | ||
buffer = value.buffer; | ||
var blobType; | ||
// Backwards-compatible blob type serialization strategy. | ||
// DBs created with older versions of localForage will simply not have the blob type. | ||
if (type === TYPE_BLOB && BLOB_TYPE_PREFIX_REGEX.test(serializedString)) { | ||
var matcher = serializedString.match(BLOB_TYPE_PREFIX_REGEX); | ||
blobType = matcher[1]; | ||
serializedString = serializedString.substring(matcher[0].length); | ||
if (valueString === '[object Int8Array]') { | ||
marker += TYPE_INT8ARRAY; | ||
} else if (valueString === '[object Uint8Array]') { | ||
marker += TYPE_UINT8ARRAY; | ||
} else if (valueString === '[object Uint8ClampedArray]') { | ||
marker += TYPE_UINT8CLAMPEDARRAY; | ||
} else if (valueString === '[object Int16Array]') { | ||
marker += TYPE_INT16ARRAY; | ||
} else if (valueString === '[object Uint16Array]') { | ||
marker += TYPE_UINT16ARRAY; | ||
} else if (valueString === '[object Int32Array]') { | ||
marker += TYPE_INT32ARRAY; | ||
} else if (valueString === '[object Uint32Array]') { | ||
marker += TYPE_UINT32ARRAY; | ||
} else if (valueString === '[object Float32Array]') { | ||
marker += TYPE_FLOAT32ARRAY; | ||
} else if (valueString === '[object Float64Array]') { | ||
marker += TYPE_FLOAT64ARRAY; | ||
} else { | ||
callback(new Error('Failed to get type for BinaryArray')); | ||
} | ||
} | ||
var buffer = stringToBuffer(serializedString); | ||
// Return the right type based on the code/type set during | ||
// serialization. | ||
switch (type) { | ||
case TYPE_ARRAYBUFFER: | ||
return buffer; | ||
case TYPE_BLOB: | ||
return _createBlob([buffer], {type: blobType}); | ||
case TYPE_INT8ARRAY: | ||
return new Int8Array(buffer); | ||
case TYPE_UINT8ARRAY: | ||
return new Uint8Array(buffer); | ||
case TYPE_UINT8CLAMPEDARRAY: | ||
return new Uint8ClampedArray(buffer); | ||
case TYPE_INT16ARRAY: | ||
return new Int16Array(buffer); | ||
case TYPE_UINT16ARRAY: | ||
return new Uint16Array(buffer); | ||
case TYPE_INT32ARRAY: | ||
return new Int32Array(buffer); | ||
case TYPE_UINT32ARRAY: | ||
return new Uint32Array(buffer); | ||
case TYPE_FLOAT32ARRAY: | ||
return new Float32Array(buffer); | ||
case TYPE_FLOAT64ARRAY: | ||
return new Float64Array(buffer); | ||
default: | ||
throw new Error('Unkown type: ' + type); | ||
} | ||
} | ||
callback(marker + bufferToString(buffer)); | ||
} else if (valueString === '[object Blob]') { | ||
// Conver the blob to a binaryArray and then to a string. | ||
var fileReader = new FileReader(); | ||
function stringToBuffer(serializedString) { | ||
// Fill the string into a ArrayBuffer. | ||
var bufferLength = serializedString.length * 0.75; | ||
var len = serializedString.length; | ||
var i; | ||
var p = 0; | ||
var encoded1, encoded2, encoded3, encoded4; | ||
fileReader.onload = function() { | ||
// Backwards-compatible prefix for the blob type. | ||
var str = BLOB_TYPE_PREFIX + value.type + '~' + | ||
bufferToString(this.result); | ||
if (serializedString[serializedString.length - 1] === '=') { | ||
bufferLength--; | ||
if (serializedString[serializedString.length - 2] === '=') { | ||
bufferLength--; | ||
} | ||
} | ||
callback(SERIALIZED_MARKER + TYPE_BLOB + str); | ||
}; | ||
var buffer = new ArrayBuffer(bufferLength); | ||
var bytes = new Uint8Array(buffer); | ||
fileReader.readAsArrayBuffer(value); | ||
} else { | ||
try { | ||
callback(JSON.stringify(value)); | ||
} catch (e) { | ||
console.error("Couldn't convert value into a JSON string: ", | ||
value); | ||
for (i = 0; i < len; i+=4) { | ||
encoded1 = BASE_CHARS.indexOf(serializedString[i]); | ||
encoded2 = BASE_CHARS.indexOf(serializedString[i+1]); | ||
encoded3 = BASE_CHARS.indexOf(serializedString[i+2]); | ||
encoded4 = BASE_CHARS.indexOf(serializedString[i+3]); | ||
/*jslint bitwise: true */ | ||
bytes[p++] = (encoded1 << 2) | (encoded2 >> 4); | ||
bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2); | ||
bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63); | ||
callback(null, e); | ||
} | ||
return buffer; | ||
} | ||
} | ||
// Converts a buffer to a string to store, serialized, in the backend | ||
// storage library. | ||
function bufferToString(buffer) { | ||
// base64-arraybuffer | ||
var bytes = new Uint8Array(buffer); | ||
var base64String = ''; | ||
var i; | ||
// Deserialize data we've inserted into a value column/field. We place | ||
// special markers into our strings to mark them as encoded; this isn't | ||
// as nice as a meta field, but it's the only sane thing we can do whilst | ||
// keeping localStorage support intact. | ||
// | ||
// Oftentimes this will just deserialize JSON content, but if we have a | ||
// special marker (SERIALIZED_MARKER, defined above), we will extract | ||
// some kind of arraybuffer/binary data/typed array out of the string. | ||
function deserialize(value) { | ||
// If we haven't marked this string as being specially serialized (i.e. | ||
// something other than serialized JSON), we can just return it and be | ||
// done with it. | ||
if (value.substring(0, | ||
SERIALIZED_MARKER_LENGTH) !== SERIALIZED_MARKER) { | ||
return JSON.parse(value); | ||
} | ||
for (i = 0; i < bytes.length; i += 3) { | ||
/*jslint bitwise: true */ | ||
base64String += BASE_CHARS[bytes[i] >> 2]; | ||
base64String += BASE_CHARS[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)]; | ||
base64String += BASE_CHARS[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)]; | ||
base64String += BASE_CHARS[bytes[i + 2] & 63]; | ||
} | ||
// The following code deals with deserializing some kind of Blob or | ||
// TypedArray. First we separate out the type of data we're dealing | ||
// with from the data itself. | ||
var serializedString = value.substring(TYPE_SERIALIZED_MARKER_LENGTH); | ||
var type = value.substring(SERIALIZED_MARKER_LENGTH, | ||
TYPE_SERIALIZED_MARKER_LENGTH); | ||
if ((bytes.length % 3) === 2) { | ||
base64String = base64String.substring(0, base64String.length - 1) + '='; | ||
} else if (bytes.length % 3 === 1) { | ||
base64String = base64String.substring(0, base64String.length - 2) + '=='; | ||
} | ||
var blobType; | ||
// Backwards-compatible blob type serialization strategy. | ||
// DBs created with older versions of localForage will simply not have the blob type. | ||
if (type === TYPE_BLOB && BLOB_TYPE_PREFIX_REGEX.test(serializedString)) { | ||
var matcher = serializedString.match(BLOB_TYPE_PREFIX_REGEX); | ||
blobType = matcher[1]; | ||
serializedString = serializedString.substring(matcher[0].length); | ||
} | ||
var buffer = stringToBuffer(serializedString); | ||
return base64String; | ||
// Return the right type based on the code/type set during | ||
// serialization. | ||
switch (type) { | ||
case TYPE_ARRAYBUFFER: | ||
return buffer; | ||
case TYPE_BLOB: | ||
return createBlob([buffer], {type: blobType}); | ||
case TYPE_INT8ARRAY: | ||
return new Int8Array(buffer); | ||
case TYPE_UINT8ARRAY: | ||
return new Uint8Array(buffer); | ||
case TYPE_UINT8CLAMPEDARRAY: | ||
return new Uint8ClampedArray(buffer); | ||
case TYPE_INT16ARRAY: | ||
return new Int16Array(buffer); | ||
case TYPE_UINT16ARRAY: | ||
return new Uint16Array(buffer); | ||
case TYPE_INT32ARRAY: | ||
return new Int32Array(buffer); | ||
case TYPE_UINT32ARRAY: | ||
return new Uint32Array(buffer); | ||
case TYPE_FLOAT32ARRAY: | ||
return new Float32Array(buffer); | ||
case TYPE_FLOAT64ARRAY: | ||
return new Float64Array(buffer); | ||
default: | ||
throw new Error('Unkown type: ' + type); | ||
} | ||
} | ||
var localforageSerializer = { | ||
serialize: serialize, | ||
deserialize: deserialize, | ||
stringToBuffer: stringToBuffer, | ||
bufferToString: bufferToString | ||
}; | ||
var localforageSerializer = { | ||
serialize: serialize, | ||
deserialize: deserialize, | ||
stringToBuffer: stringToBuffer, | ||
bufferToString: bufferToString | ||
}; | ||
return localforageSerializer; | ||
})(typeof window !== 'undefined' ? window : self); | ||
export default localforageSerializer; |
(function() { | ||
'use strict'; | ||
// Promises! | ||
var Promise = (typeof module !== 'undefined' && module.exports && typeof require !== 'undefined') ? | ||
require('promise') : this.Promise; | ||
var dummyStorage = {}; | ||
@@ -9,0 +5,0 @@ |
@@ -53,23 +53,8 @@ // Run before window.onload to make sure the specs have access to describe() | ||
if (require) { | ||
if (this.requireUnbundledLocalForage) { | ||
requirejs.config({ | ||
paths: { | ||
localforage: '/build/es5src/localforage', | ||
localforageSerializer: '/build/es5src/utils/serializer', | ||
asyncStorage: '/build/es5src/drivers/indexeddb', | ||
localStorageWrapper: '/build/es5src/drivers/localstorage', | ||
webSQLStorage: '/build/es5src/drivers/websql', | ||
Promise: '/bower_components/es6-promise/promise' | ||
}, | ||
shim: { | ||
localforage: ['Promise'] | ||
} | ||
}); | ||
} else { | ||
requirejs.config({ | ||
paths: { | ||
localforage: '/dist/localforage' | ||
} | ||
}); | ||
} | ||
requirejs.config({ | ||
paths: { | ||
localforage: '/dist/localforage' | ||
} | ||
}); | ||
require(['localforage'], function(localforage) { | ||
@@ -76,0 +61,0 @@ window.localforage = localforage; |
@@ -1,2 +0,2 @@ | ||
/* global after:true, afterEach:true, before:true, beforeEach:true, describe:true, expect:true, it:true, Promise:true, require:true */ | ||
/* global after:true, afterEach:true, before:true, beforeEach:true, describe:true, expect:true, it:true, Promise:true */ | ||
var DRIVERS = [ | ||
@@ -18,16 +18,6 @@ localforage.INDEXEDDB, | ||
var componentBuild = window.require && window.require.modules && | ||
window.require.modules.localforage && | ||
window.require.modules.localforage.component; | ||
describe('localForage API', function() { | ||
// If this test is failing, you are likely missing the Promises polyfill, | ||
// installed via bower. Read more here: | ||
// https://github.com/mozilla/localForage#working-on-localforage | ||
it('has Promises available', function() { | ||
if (componentBuild) { | ||
expect(require('promise')).to.be.a('function'); | ||
} else { | ||
expect(Promise).to.be.a('function'); | ||
} | ||
expect(Promise).to.be.a('function'); | ||
}); | ||
@@ -870,6 +860,4 @@ }); | ||
var localforage3 = null; | ||
var Promise; | ||
before(function(done) { | ||
Promise = window.Promise || require('promise'); | ||
@@ -1135,8 +1123,3 @@ prepareStorage('storage2').then(function() { | ||
var _oldReady; | ||
var Promise; | ||
before(function() { | ||
Promise = window.Promise || require('promise'); | ||
}); | ||
beforeEach(function(done) { | ||
@@ -1143,0 +1126,0 @@ _oldReady = localforage.ready; |
/* global before:true, beforeEach:true, describe:true, expect:true, it:true */ | ||
var DRIVERS = [ | ||
localforage.INDEXEDDB, | ||
localforage.LOCALSTORAGE, | ||
localforage.WEBSQL | ||
]; | ||
// Abstracts constructing a Blob object, so it also works in older | ||
// browsers that don't support the native Blob constructor. (i.e. | ||
// old QtWebKit versions, at least). | ||
function _createBlob(parts, properties) { | ||
// kinda lame to define this twice, but it seems require() isn't available here | ||
function createBlob(parts, properties) { | ||
/* global BlobBuilder,MSBlobBuilder,MozBlobBuilder,WebKitBlobBuilder */ | ||
parts = parts || []; | ||
@@ -20,7 +14,7 @@ properties = properties || {}; | ||
} | ||
var BlobBuilder = window.BlobBuilder || | ||
window.MSBlobBuilder || | ||
window.MozBlobBuilder || | ||
window.WebKitBlobBuilder; | ||
var builder = new BlobBuilder(); | ||
var Builder = typeof BlobBuilder !== 'undefined' ? BlobBuilder : | ||
typeof MSBlobBuilder !== 'undefined' ? MSBlobBuilder : | ||
typeof MozBlobBuilder !== 'undefined' ? MozBlobBuilder : | ||
WebKitBlobBuilder; | ||
var builder = new Builder(); | ||
for (var i = 0; i < parts.length; i += 1) { | ||
@@ -33,2 +27,8 @@ builder.append(parts[i]); | ||
var DRIVERS = [ | ||
localforage.INDEXEDDB, | ||
localforage.LOCALSTORAGE, | ||
localforage.WEBSQL | ||
]; | ||
DRIVERS.forEach(function(driverName) { | ||
@@ -329,3 +329,3 @@ if ((!localforage.supports(localforage.INDEXEDDB) && | ||
var testBlob = _createBlob(fileParts, { 'type' : mimeString }); | ||
var testBlob = createBlob(fileParts, { 'type' : mimeString }); | ||
@@ -362,3 +362,3 @@ localforage.setItem('blob', testBlob, function(err, blob) { | ||
var testBlob = _createBlob(fileParts, { 'type' : mimeString }); | ||
var testBlob = createBlob(fileParts, { 'type' : mimeString }); | ||
@@ -365,0 +365,0 @@ localforage.setItem('blob', testBlob, function(err, blob) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
923833
12659
35
82
3
269
19
+ Addedlie@3.0.2
+ Addedacorn@1.2.2(transitive)
+ Addedamdefine@1.0.1(transitive)
+ Addedast-types@0.8.15(transitive)
+ Addedbase62@0.1.1(transitive)
+ Addedcore-util-is@1.0.3(transitive)
+ Addedes3ify@0.1.4(transitive)
+ Addedesmangle-evaluator@1.0.1(transitive)
+ Addedesprima-fb@15001.1001.0-dev-harmony-fb3001.1.0-dev-harmony-fb(transitive)
+ Addedfalafel@1.2.0(transitive)
+ Addedforeach@2.0.6(transitive)
+ Addedimmediate@3.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedinline-process-browser@1.0.0(transitive)
+ Addedisarray@0.0.1(transitive)
+ Addedjstransform@3.0.0(transitive)
+ Addedlie@3.0.2(transitive)
+ Addedobject-keys@1.1.1(transitive)
+ Addedprivate@0.1.8(transitive)
+ Addedreadable-stream@1.0.34(transitive)
+ Addedrecast@0.10.43(transitive)
+ Addedsource-map@0.1.310.5.7(transitive)
+ Addedstring_decoder@0.10.31(transitive)
+ Addedthrough@2.3.8(transitive)
+ Addedthrough2@0.6.5(transitive)
+ Addedunreachable-branch-transform@0.3.0(transitive)
+ Addedxtend@4.0.2(transitive)
- Removedpromise@^5.0.0
- Removedasap@1.0.0(transitive)
- Removedpromise@5.0.0(transitive)