1234567891011121314151617181920212223242526 |
- /*!
- MIT License
- Copyright (c) 2018 Arturas Molcanovas <a.molcanovas@gmail.com>
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- SOFTWARE.
- */
- !function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define("localforage-driver-memory",["exports"],r):r(e.LocalforageDriverMemory={})}("undefined"!=typeof self?self:this,function(e){"use strict";var r=/^~~local_forage_type~([^~]+)~/,t="__lfsc__:".length,n=t+"arbf".length,o=Object.prototype.toString;function i(e){var r=.75*e.length,t=e.length;"="===e[e.length-1]&&(r--,"="===e[e.length-2]&&r--);for(var n=new ArrayBuffer(r),o=new Uint8Array(n),i=0,a=0;i<t;i+=4){var u="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".indexOf(e[i]),f="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".indexOf(e[i+1]),l="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".indexOf(e[i+2]),s="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".indexOf(e[i+3]);o[a++]=u<<2|f>>4,o[a++]=(15&f)<<4|l>>2,o[a++]=(3&l)<<6|63&s}return n}function a(e){for(var r=new Uint8Array(e),t="",n=0;n<r.length;n+=3)t+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[r[n]>>2],t+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(3&r[n])<<4|r[n+1]>>4],t+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(15&r[n+1])<<2|r[n+2]>>6],t+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[63&r[n+2]];return r.length%3==2?t=t.substring(0,t.length-1)+"=":r.length%3==1&&(t=t.substring(0,t.length-2)+"=="),t}function u(e){var r,t;if(null===e||"object"!=typeof e||"isActiveClone"in e)return e;var n=e instanceof Date?new Date(e):e.constructor();try{for(var o=function(e){var r="function"==typeof Symbol&&e[Symbol.iterator],t=0;return r?r.call(e):{next:function(){return e&&t>=e.length&&(e=void 0),{value:e&&e[t++],done:!e}}}}(Object.keys(e)),i=o.next();!i.done;i=o.next()){var a=i.value;Object.prototype.hasOwnProperty.call(e,a)&&(e.isActiveClone=null,n[a]=u(e[a]),delete e.isActiveClone)}}catch(e){r={error:e}}finally{try{i&&!i.done&&(t=o.return)&&t.call(o)}finally{if(r)throw r.error}}return n}function f(e,r){return(e.name||r.name)+"/"+(e.storeName||r.storeName)+"/"}function l(e,r){r&&e.then(function(e){r(null,e)},function(e){r(e)})}function s(e){return"string"!=typeof e&&(console.warn(e+" used as a key, but it is not a string."),e=String(e)),e}var c={bufferToString:a,deserialize:function(e){if("__lfsc__:"!==e.substring(0,t))return JSON.parse(e);var o,a=e.substring(n),u=e.substring(t,n);if("blob"===u&&r.test(a)){var f=a.match(r);o=f[1],a=a.substring(f[0].length)}var l=i(a);switch(u){case"arbf":return l;case"blob":return function(e,r){e=e||[],r=r||{};try{return new Blob(e,r)}catch(o){if("TypeError"!==o.name)throw o;for(var t=new("undefined"!=typeof BlobBuilder?BlobBuilder:"undefined"!=typeof MSBlobBuilder?MSBlobBuilder:"undefined"!=typeof MozBlobBuilder?MozBlobBuilder:WebKitBlobBuilder),n=0;n<e.length;n+=1)t.append(e[n]);return t.getBlob(r.type)}}([l],{type:o});case"si08":return new Int8Array(l);case"ui08":return new Uint8Array(l);case"uic8":return new Uint8ClampedArray(l);case"si16":return new Int16Array(l);case"ur16":return new Uint16Array(l);case"si32":return new Int32Array(l);case"ui32":return new Uint32Array(l);case"fl32":return new Float32Array(l);case"fl64":return new Float64Array(l);default:throw new Error("Unkown type: "+u)}},serialize:function(e,r){var t="";if(e&&(t=o.call(e)),e&&("[object ArrayBuffer]"===t||e.buffer&&"[object ArrayBuffer]"===o.call(e.buffer))){var n=void 0,i="__lfsc__:";e instanceof ArrayBuffer?(n=e,i+="arbf"):(n=e.buffer,"[object Int8Array]"===t?i+="si08":"[object Uint8Array]"===t?i+="ui08":"[object Uint8ClampedArray]"===t?i+="uic8":"[object Int16Array]"===t?i+="si16":"[object Uint16Array]"===t?i+="ur16":"[object Int32Array]"===t?i+="si32":"[object Uint32Array]"===t?i+="ui32":"[object Float32Array]"===t?i+="fl32":"[object Float64Array]"===t?i+="fl64":r(new Error("Failed to get type for BinaryArray"))),r(i+a(n))}else if("[object Blob]"===t){var u=new FileReader;u.onload=function(){var t="~~local_forage_type~"+e.type+"~"+a(this.result);r("__lfsc__:blob"+t)},u.readAsArrayBuffer(e)}else try{r(JSON.stringify(e))}catch(t){console.error("Couldn't convert value into a JSON string: ",e),r(null,t)}},stringToBuffer:i},y={},d=function(){function e(e){this.kp=e,this.data={}}return e.resolve=function(r){return y[r]||(y[r]=new e(r)),y[r]},e.prototype.clear=function(){this.data={}},e.prototype.drop=function(){this.clear(),delete y[this.kp]},e.prototype.get=function(e){return this.data[e]},e.prototype.key=function(e){return this.keys()[e]},e.prototype.keys=function(){return Object.keys(this.data)},e.prototype.rm=function(e){delete this.data[e]},e.prototype.set=function(e,r){this.data[e]=r},e}();e._support=!0,e._driver="localforage-driver-memory",e._initStorage=function(e){var r=e?u(e):{},t=f(r,this._defaultConfig),n=d.resolve(t);return this._dbInfo=r,this._dbInfo.serializer=c,this._dbInfo.keyPrefix=t,this._dbInfo.mStore=n,Promise.resolve()},e.clear=function(e){var r=this,t=this.ready().then(function(){r._dbInfo.mStore.clear()});return l(t,e),t},e.dropInstance=function(e,r){var t=function(e,r){var t=this;if(r=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];if(arguments.length&&"function"==typeof arguments[arguments.length-1])return arguments[arguments.length-1]}.apply(this,arguments),!(e="function"!=typeof e&&e||{}).name){var n=this.config();e.name=e.name||n.name,e.storeName=e.storeName||n.storeName}return{promise:e.name?new Promise(function(r){e.storeName?r(f(e,t._defaultConfig)):r(e.name+"/")}):Promise.reject("Invalid arguments"),callback:r}}.apply(this,arguments),n=t.promise,o=t.callback;return l(n.then(function(e){d.resolve(e).drop()}),o),n},e.getItem=function(e,r){var t=this;e=s(e);var n=this.ready().then(function(){var r=t._dbInfo.mStore.get(e);return null==r?null:t._dbInfo.serializer.deserialize(r)});return l(n,r),n},e.iterate=function(e,r){var t=this,n=this.ready().then(function(){for(var r=t._dbInfo.mStore,n=r.keys(),o=0;o<n.length;o++){var i=r.get(n[o]);if(i&&(i=t._dbInfo.serializer.deserialize(i)),void 0!==(i=e(i,n[o],o+1)))return i}});return l(n,r),n},e.key=function(e,r){var t=this,n=this.ready().then(function(){var r;try{void 0===(r=t._dbInfo.mStore.key(e))&&(r=null)}catch(e){r=null}return r});return l(n,r),n},e.keys=function(e){var r=this,t=this.ready().then(function(){return r._dbInfo.mStore.keys()});return l(t,e),t},e.length=function(e){var r=this.keys().then(function(e){return e.length});return l(r,e),r},e.removeItem=function(e,r){var t=this;e=s(e);var n=this.ready().then(function(){t._dbInfo.mStore.rm(e)});return l(n,r),n},e.setItem=function(e,r,t){var n=this;e=s(e);var o=this.ready().then(function(){void 0===r&&(r=null);var t=r;return new Promise(function(o,i){n._dbInfo.serializer.serialize(r,function(r,a){if(a)i(a);else try{n._dbInfo.mStore.set(e,r),o(t)}catch(e){i(e)}})})});return l(o,t),o},Object.defineProperty(e,"__esModule",{value:!0})});
|