UNPKG

15.9 kBJavaScriptView Raw
1!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("aws_amplify_core")):"function"==typeof define&&define.amd?define("aws_amplify_cache",["aws_amplify_core"],t):"object"==typeof exports?exports.aws_amplify_cache=t(require("aws_amplify_core")):e.aws_amplify_cache=t(e.aws_amplify_core)}(this,(function(e){return function(e){var t={};function i(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,i),o.l=!0,o.exports}return i.m=e,i.c=t,i.d=function(e,t,r){i.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},i.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},i.t=function(e,t){if(1&t&&(e=i(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(i.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)i.d(r,o,function(t){return e[t]}.bind(null,o));return r},i.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return i.d(t,"a",t),t},i.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},i.p="",i(i.s=2)}([function(t,i){t.exports=e},,function(e,t,i){"use strict";i.r(t),i.d(t,"BrowserStorageCache",(function(){return d})),i.d(t,"InMemoryCache",(function(){return _}));var r=i(0),o={keyPrefix:"aws-amplify-cache",capacityInBytes:1048576,itemMaxSize:21e4,defaultTTL:2592e5,defaultPriority:5,warningThreshold:.8,storage:(new r.StorageHelper).getStorage()};function n(e){var t=0;t=e.length;for(var i=e.length;i>=0;i-=1){var r=e.charCodeAt(i);r>127&&r<=2047?t+=1:r>2047&&r<=65535&&(t+=2),r>=56320&&r<=57343&&(i-=1)}return t}function a(){return(new Date).getTime()}function s(e){return Number.isInteger?Number.isInteger(e):function(e){return"number"==typeof e&&isFinite(e)&&Math.floor(e)===e}(e)}var c={},h=function(){function e(){}return e.clear=function(){c={}},e.getItem=function(e){return c[e]||null},e.setItem=function(e,t){c[e]=t},e.removeItem=function(e){delete c[e]},e}();function f(e){return(f="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}var u,y=new r.ConsoleLogger("StorageCache"),l=function(){function e(e){this.config=Object.assign({},e),this.cacheCurSizeKey=this.config.keyPrefix+"CurSize",this.checkConfig()}return e.prototype.getModuleName=function(){return"Cache"},e.prototype.checkConfig=function(){s(this.config.capacityInBytes)||(y.error("Invalid parameter: capacityInBytes. It should be an Integer. Setting back to default."),this.config.capacityInBytes=o.capacityInBytes),s(this.config.itemMaxSize)||(y.error("Invalid parameter: itemMaxSize. It should be an Integer. Setting back to default."),this.config.itemMaxSize=o.itemMaxSize),s(this.config.defaultTTL)||(y.error("Invalid parameter: defaultTTL. It should be an Integer. Setting back to default."),this.config.defaultTTL=o.defaultTTL),s(this.config.defaultPriority)||(y.error("Invalid parameter: defaultPriority. It should be an Integer. Setting back to default."),this.config.defaultPriority=o.defaultPriority),this.config.itemMaxSize>this.config.capacityInBytes&&(y.error("Invalid parameter: itemMaxSize. It should be smaller than capacityInBytes. Setting back to default."),this.config.itemMaxSize=o.itemMaxSize),(this.config.defaultPriority>5||this.config.defaultPriority<1)&&(y.error("Invalid parameter: defaultPriority. It should be between 1 and 5. Setting back to default."),this.config.defaultPriority=o.defaultPriority),(Number(this.config.warningThreshold)>1||Number(this.config.warningThreshold)<0)&&(y.error("Invalid parameter: warningThreshold. It should be between 0 and 1. Setting back to default."),this.config.warningThreshold=o.warningThreshold);this.config.capacityInBytes>5242880&&(y.error("Cache Capacity should be less than 5MB. Setting back to default. Setting back to default."),this.config.capacityInBytes=o.capacityInBytes)},e.prototype.fillCacheItem=function(e,t,i){var r={key:e,data:t,timestamp:a(),visitedTime:a(),priority:i.priority,expires:i.expires,type:f(t),byteSize:0};return r.byteSize=n(JSON.stringify(r)),r.byteSize=n(JSON.stringify(r)),r},e.prototype.configure=function(e){return e?(e.keyPrefix&&y.warn("Don't try to configure keyPrefix!"),this.config=Object.assign({},this.config,e,e.Cache),this.checkConfig(),this.config):this.config},e}(),p=(u=function(e,t){return(u=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var i in t)t.hasOwnProperty(i)&&(e[i]=t[i])})(e,t)},function(e,t){function i(){this.constructor=e}u(e,t),e.prototype=null===t?Object.create(t):(i.prototype=t.prototype,new i)}),g=new r.ConsoleLogger("Cache"),d=new(function(e){function t(t){var i=this,r=t?Object.assign({},o,t):o;return(i=e.call(this,r)||this).config.storage=r.storage,i.getItem=i.getItem.bind(i),i.setItem=i.setItem.bind(i),i.removeItem=i.removeItem.bind(i),i}return p(t,e),t.prototype._decreaseCurSizeInBytes=function(e){var t=this.getCacheCurSize();this.config.storage.setItem(this.cacheCurSizeKey,(t-e).toString())},t.prototype._increaseCurSizeInBytes=function(e){var t=this.getCacheCurSize();this.config.storage.setItem(this.cacheCurSizeKey,(t+e).toString())},t.prototype._refreshItem=function(e,t){return e.visitedTime=a(),this.config.storage.setItem(t,JSON.stringify(e)),e},t.prototype._isExpired=function(e){var t=this.config.storage.getItem(e),i=JSON.parse(t);return a()>=i.expires},t.prototype._removeItem=function(e,t){var i=t||JSON.parse(this.config.storage.getItem(e)).byteSize;this._decreaseCurSizeInBytes(i),this.config.storage.removeItem(e)},t.prototype._setItem=function(e,t){this._increaseCurSizeInBytes(t.byteSize);try{this.config.storage.setItem(e,JSON.stringify(t))}catch(e){this._decreaseCurSizeInBytes(t.byteSize),g.error("Failed to set item "+e)}},t.prototype._sizeToPop=function(e){var t=this.getCacheCurSize()+e-this.config.capacityInBytes,i=(1-this.config.warningThreshold)*this.config.capacityInBytes;return t>i?t:i},t.prototype._isCacheFull=function(e){return e+this.getCacheCurSize()>this.config.capacityInBytes},t.prototype._findValidKeys=function(){for(var e=[],t=[],i=0;i<this.config.storage.length;i+=1)t.push(this.config.storage.key(i));for(i=0;i<t.length;i+=1){var r=t[i];0===r.indexOf(this.config.keyPrefix)&&r!==this.cacheCurSizeKey&&(this._isExpired(r)?this._removeItem(r):e.push(r))}return e},t.prototype._popOutItems=function(e,t){for(var i=[],r=t,o=0;o<e.length;o+=1){var n=this.config.storage.getItem(e[o]);if(null!=n){var a=JSON.parse(n);i.push(a)}}i.sort((function(e,t){return e.priority>t.priority?-1:e.priority<t.priority?1:e.visitedTime<t.visitedTime?-1:1}));for(o=0;o<i.length;o+=1)if(this._removeItem(i[o].key,i[o].byteSize),(r-=i[o].byteSize)<=0)return},t.prototype.setItem=function(e,t,i){g.log("Set item: key is "+e+", value is "+t+" with options: "+i);var r=this.config.keyPrefix+e;if(r!==this.config.keyPrefix&&r!==this.cacheCurSizeKey)if(void 0!==t){var o={priority:i&&void 0!==i.priority?i.priority:this.config.defaultPriority,expires:i&&void 0!==i.expires?i.expires:this.config.defaultTTL+a()};if(o.priority<1||o.priority>5)g.warn("Invalid parameter: priority due to out or range. It should be within 1 and 5.");else{var n=this.fillCacheItem(r,t,o);if(n.byteSize>this.config.itemMaxSize)g.warn("Item with key: "+e+" you are trying to put into is too big!");else try{var s=this.config.storage.getItem(r);if(s&&this._removeItem(r,JSON.parse(s).byteSize),this._isCacheFull(n.byteSize)){var c=this._findValidKeys();if(this._isCacheFull(n.byteSize)){var h=this._sizeToPop(n.byteSize);this._popOutItems(c,h)}}this._setItem(r,n)}catch(e){g.warn("setItem failed! "+e)}}}else g.warn("The value of item should not be undefined!");else g.warn("Invalid key: should not be empty or 'CurSize'")},t.prototype.getItem=function(e,t){g.log("Get item: key is "+e+" with options "+t);var i=null,r=this.config.keyPrefix+e;if(r===this.config.keyPrefix||r===this.cacheCurSizeKey)return g.warn("Invalid key: should not be empty or 'CurSize'"),null;try{if(null!=(i=this.config.storage.getItem(r))){if(!this._isExpired(r)){var o=JSON.parse(i);return(o=this._refreshItem(o,r)).data}this._removeItem(r,JSON.parse(i).byteSize),i=null}if(t&&void 0!==t.callback){var n=t.callback();return null!==n&&this.setItem(e,n,t),n}return null}catch(e){return g.warn("getItem failed! "+e),null}},t.prototype.removeItem=function(e){g.log("Remove item: key is "+e);var t=this.config.keyPrefix+e;if(t!==this.config.keyPrefix&&t!==this.cacheCurSizeKey)try{var i=this.config.storage.getItem(t);i&&this._removeItem(t,JSON.parse(i).byteSize)}catch(e){g.warn("removeItem failed! "+e)}},t.prototype.clear=function(){g.log("Clear Cache");for(var e=[],t=0;t<this.config.storage.length;t+=1){var i=this.config.storage.key(t);0===i.indexOf(this.config.keyPrefix)&&e.push(i)}try{for(t=0;t<e.length;t+=1)this.config.storage.removeItem(e[t])}catch(e){g.warn("clear failed! "+e)}},t.prototype.getAllKeys=function(){for(var e=[],t=0;t<this.config.storage.length;t+=1){var i=this.config.storage.key(t);0===i.indexOf(this.config.keyPrefix)&&i!==this.cacheCurSizeKey&&e.push(i.substring(this.config.keyPrefix.length))}return e},t.prototype.getCacheCurSize=function(){var e=this.config.storage.getItem(this.cacheCurSizeKey);return e||(this.config.storage.setItem(this.cacheCurSizeKey,"0"),e="0"),Number(e)},t.prototype.createInstance=function(e){return e.keyPrefix&&e.keyPrefix!==o.keyPrefix||(g.error("invalid keyPrefix, setting keyPrefix with timeStamp"),e.keyPrefix=a.toString()),new t(e)},t}(l)),m=function(e){var t="function"==typeof Symbol&&Symbol.iterator,i=t&&e[t],r=0;if(i)return i.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&r>=e.length&&(e=void 0),{value:e&&e[r++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")},v=function(e){this.key=e||"",this.prevNode=null,this.nextNode=null},I=function(){function e(){this.head=new v,this.tail=new v,this.hashtable={},this.length=0,this.head.nextNode=this.tail,this.tail.prevNode=this.head}return e.prototype.insertNodeToHead=function(e){var t=this.head.nextNode;this.head.nextNode=e,e.nextNode=t,e.prevNode=this.head,t.prevNode=e,this.length=this.length+1},e.prototype.removeNode=function(e){e.prevNode.nextNode=e.nextNode,e.nextNode.prevNode=e.prevNode,e.prevNode=null,e.nextNode=null,this.length=this.length-1},e.prototype.isEmpty=function(){return 0===this.length},e.prototype.refresh=function(e){var t=this.hashtable[e];this.removeNode(t),this.insertNodeToHead(t)},e.prototype.insertItem=function(e){var t=new v(e);this.hashtable[e]=t,this.insertNodeToHead(t)},e.prototype.getLastItem=function(){return this.tail.prevNode.key},e.prototype.removeItem=function(e){var t=this.hashtable[e];this.removeNode(t),delete this.hashtable[e]},e.prototype.getSize=function(){return this.length},e.prototype.containsKey=function(e){return e in this.hashtable},e.prototype.clearList=function(){var e,t;try{for(var i=m(Object.keys(this.hashtable)),r=i.next();!r.done;r=i.next()){var o=r.value;this.hashtable.hasOwnProperty(o)&&delete this.hashtable[o]}}catch(t){e={error:t}}finally{try{r&&!r.done&&(t=i.return)&&t.call(i)}finally{if(e)throw e.error}}this.head.nextNode=this.tail,this.tail.prevNode=this.head,this.length=0},e.prototype.getKeys=function(){return Object.keys(this.hashtable)},e.prototype.isHeadNode=function(e){return this.hashtable[e].prevNode===this.head},e.prototype.isTailNode=function(e){return this.hashtable[e].nextNode===this.tail},e}(),b=function(){var e=function(t,i){return(e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var i in t)t.hasOwnProperty(i)&&(e[i]=t[i])})(t,i)};return function(t,i){function r(){this.constructor=t}e(t,i),t.prototype=null===i?Object.create(i):(r.prototype=i.prototype,new r)}}(),S=function(e){var t="function"==typeof Symbol&&Symbol.iterator,i=t&&e[t],r=0;if(i)return i.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&r>=e.length&&(e=void 0),{value:e&&e[r++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")},x=new r.ConsoleLogger("InMemoryCache"),_=new(function(e){function t(t){var i=this,r=t?Object.assign({},o,t):o;i=e.call(this,r)||this,x.debug("now we start!"),i.cacheList=[],i.curSizeInBytes=0,i.maxPriority=5,i.getItem=i.getItem.bind(i),i.setItem=i.setItem.bind(i),i.removeItem=i.removeItem.bind(i);for(var n=0;n<i.maxPriority;n+=1)i.cacheList[n]=new I;return i}return b(t,e),t.prototype._decreaseCurSizeInBytes=function(e){this.curSizeInBytes-=e},t.prototype._increaseCurSizeInBytes=function(e){this.curSizeInBytes+=e},t.prototype._isExpired=function(e){var t=h.getItem(e),i=JSON.parse(t);return a()>=i.expires},t.prototype._removeItem=function(e,t){this.cacheList[t].removeItem(e),this._decreaseCurSizeInBytes(JSON.parse(h.getItem(e)).byteSize),h.removeItem(e)},t.prototype._setItem=function(e,t,i){this.cacheList[i].insertItem(e),this._increaseCurSizeInBytes(t.byteSize),h.setItem(e,JSON.stringify(t))},t.prototype._isCacheFull=function(e){return this.curSizeInBytes+e>this.config.capacityInBytes},t.prototype.containsKey=function(e){for(var t=this.config.keyPrefix+e,i=0;i<this.maxPriority;i+=1)if(this.cacheList[i].containsKey(t))return i+1;return-1},t.prototype.setItem=function(e,t,i){var r=this.config.keyPrefix+e;if(r!==this.config.keyPrefix&&r!==this.cacheCurSizeKey)if(void 0!==t){var o={priority:i&&void 0!==i.priority?i.priority:this.config.defaultPriority,expires:i&&void 0!==i.expires?i.expires:this.config.defaultTTL+a()};if(o.priority<1||o.priority>5)x.warn("Invalid parameter: priority due to out or range. It should be within 1 and 5.");else{var n=this.fillCacheItem(r,t,o);if(n.byteSize>this.config.itemMaxSize)x.warn("Item with key: "+e+" you are trying to put into is too big!");else{var s=this.containsKey(e);-1!==s&&this._removeItem(r,s-1);for(var c=this.maxPriority-1;this._isCacheFull(n.byteSize)&&c>=0;)if(this.cacheList[c].isEmpty())c-=1;else{var h=this.cacheList[c].getLastItem();this._removeItem(h,c)}this._setItem(r,n,Number(n.priority)-1)}}}else x.warn("The value of item should not be undefined!");else x.warn("Invalid key: should not be empty or 'CurSize'")},t.prototype.getItem=function(e,t){var i=null,r=this.config.keyPrefix+e;if(r===this.config.keyPrefix||r===this.cacheCurSizeKey)return x.warn("Invalid key: should not be empty or 'CurSize'"),null;var o=this.containsKey(e);if(-1!==o){if(!this._isExpired(r)){i=h.getItem(r);var n=JSON.parse(i);return this.cacheList[n.priority-1].refresh(r),n.data}this._removeItem(r,o-1)}if(t&&void 0!==t.callback){var a=t.callback();return null!==a&&this.setItem(e,a,t),a}return null},t.prototype.removeItem=function(e){var t=this.config.keyPrefix+e,i=this.containsKey(e);-1!==i&&this._removeItem(t,i-1)},t.prototype.clear=function(){for(var e,t,i=0;i<this.maxPriority;i+=1)try{for(var r=(e=void 0,S(this.cacheList[i].getKeys())),o=r.next();!o.done;o=r.next()){var n=o.value;this._removeItem(n,i)}}catch(t){e={error:t}}finally{try{o&&!o.done&&(t=r.return)&&t.call(r)}finally{if(e)throw e.error}}},t.prototype.getAllKeys=function(){for(var e,t,i=[],r=0;r<this.maxPriority;r+=1)try{for(var o=(e=void 0,S(this.cacheList[r].getKeys())),n=o.next();!n.done;n=o.next()){var a=n.value;i.push(a.substring(this.config.keyPrefix.length))}}catch(t){e={error:t}}finally{try{n&&!n.done&&(t=o.return)&&t.call(o)}finally{if(e)throw e.error}}return i},t.prototype.getCacheCurSize=function(){return this.curSizeInBytes},t.prototype.createInstance=function(e){return new t(e)},t}(l));t.default=d;r.Amplify.register(d)}])}));
2//# sourceMappingURL=aws-amplify-cache.min.js.map
\No newline at end of file