spy-client
Version:
spy client
2 lines (1 loc) • 7.34 kB
JavaScript
((t,e)=>{"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).SpyLocalCache=e()})(this,function(){var r=function(t,e){return(r=Object.setPrototypeOf||({__proto__:[]}instanceof Array?function(t,e){t.__proto__=e}:function(t,e){for(var o in e)e.hasOwnProperty(o)&&(t[o]=e[o])}))(t,e)};function t(t,e){function o(){this.constructor=t}r(t,e),t.prototype=null===e?Object.create(e):(o.prototype=e.prototype,new o)}e.prototype.preOrder=function(t){t.push(this),this.left&&this.left.preOrder(t),this.right&&this.right.preOrder(t)};var c=e;function e(t,e){this.data=t,this.weight=e}function o(t){var n;return null==t?null:(n={},function t(e,o,r){r=[].concat(r);r.push(o),null!=e&&(null==e.data?(t(e.left,"0",r),t(e.right,"1",r)):n[e.data]=r.join(""))}(t,"",[]),n)}function n(t){var e=o((t=>{for(var e=function(t,e){return t.weight-e.weight};1<t.length;){t.sort(e);var o,r=t.shift(),n=t.shift();r&&n&&((o=new c(null,r.weight+n.weight)).left=r,o.right=n,t.unshift(o))}return t.shift()})((t=>{for(var e=[],o={},r=0,n=t;r<n.length;r++){var i=n[r];null==o[i]?o[i]=1:o[i]++}for(var a=0,s=Object.entries(o);a<s.length;a++){var u=s[a],h=u[0];e.push(new c(h,u[1]))}return e})(t)));return{result:(t=>{for(var e=t.join(""),t=Math.ceil(e.length/8),o=new Array(t+1),r=0,n="",i=0;i<e.length;i+=8)n=e.substring(i,i+8),o[r]=parseInt(n,2),r++;return o[r]=n.length,o})(((t,e)=>{for(var o=[],r=0,n=t;r<n.length;r++){var i=n[r];o.push(e[i])}return o})(t,e)),codes:e}}function i(t,e){for(var o,r,n=[],i=0;i<e.length-1;i++){var a=i!==e.length-2;n.push((o=a,r=e[i],o&&(r|=256),r=Number(r).toString(2),o?r.substring(r.length-8):r))}var s=e[e.length-1],s="00000000".substring(8-(s-(s=n[n.length-1]).length))+s;n[n.length-1]=s;for(var u={},h=0,c=Object.entries(t);h<c.length;h++){var l=c[h],p=l[0];u[l[1]]=p}for(var f=n.join(""),d=[],g=f.length,i=0;i<g;){for(var v=1,a=!0,y=null;a&&i+v<=g;){if(""===(p=f.substring(i,i+v)))break;(y=u[p])?a=!1:v++}d.push(parseInt(y,10)),i+=v}return d}function a(t){for(var e=[],o=0;o<t.length;o++)e.push(t.charCodeAt(o));return e}function s(t){for(var e="",o=0,r=t;o<r.length;o++){var n=r[o];e+=String.fromCharCode(n)}return e}h.prototype.set=function(t,e){},h.prototype.get=function(t,e){},h.prototype.rm=function(t){};var u=h;function h(){}t(f,l=u),f.isSupport=function(){return!!window.localStorage},f.prototype.set=function(t,e){try{localStorage.setItem(t,e)}catch(t){console.error(t)}},f.prototype.get=function(t,e){var o=null;try{o=localStorage.getItem(t)}catch(t){console.error(t)}e&&e(o)},f.prototype.rm=function(t){try{localStorage.removeItem(t)}catch(t){console.error(t)}};var l,p=f;function f(){return null!==l&&l.apply(this,arguments)||this}t(v,d=u),v.isSupport=function(){return!!window.indexedDB},v.prototype.set=function(t,e){this.db?this.db.transaction([this.databaseName],"readwrite").objectStore(this.databaseName).add({key:t,value:e}):this.setQueue.push({key:t,value:e})},v.prototype.get=function(t,e){var o;this.db?(o=this.db.transaction([this.databaseName]).objectStore(this.databaseName).get(t)).onsuccess=function(){var t=o.result||{value:""};e(t.value)}:this.getQueue.push({key:t,cb:e})},v.prototype.runQueueTask=function(){var e=this;this.setQueue.forEach(function(t){e.set(t.key,t.value)}),this.getQueue.forEach(function(t){e.get(t.key,t.cb)})};var d,g=v;function v(){var e=d.call(this)||this,t=(e.databaseName="spyLC",e.db=null,e.setQueue=[],e.getQueue=[],window.indexedDB.open(e.databaseName));return t.onupgradeneeded=function(t){e.db=t.target&&t.target.result,e.db&&!e.db.objectStoreNames.contains(e.databaseName)&&e.db.createObjectStore(e.databaseName,{keyPath:"key"}),e.runQueueTask()},t.onsuccess=function(){e.db=t.result,e.runQueueTask()},e}function y(t,e){if(!t)return"";(e=void 0===e?!1:e)||(t=(t=>{for(var e="",o=0;o<t.length;o++){var r=t.charCodeAt(o);r<128?e+=String.fromCharCode(r):e=127<r&&r<2048?(e+=String.fromCharCode(r>>6|192))+String.fromCharCode(63&r|128):(e=(e+=String.fromCharCode(r>>12|224))+String.fromCharCode(r>>6&63|128))+String.fromCharCode(63&r|128)}return e})(t));function o(){s.push(1<u.length?String.fromCharCode(a[u]):u)}var r,n,i,a={},s=[],u=t.charAt(0),h=u,c=h,l=256;for(e&&s.push(u),r=1;r<t.length;r++)n=t.charAt(r),e?(i=t.charCodeAt(r),u=i<256?n:a[i]||u+h,s.push(u),h=u.charAt(0),a[l++]=c+h,c=u):a.hasOwnProperty(u+n)?u+=n:(o(),a[u+n]=l++,u=n);e||o();var p=s.join("");return p=e?(t=>{for(var e,o,r="",n=0,i=0;n<t.length;)(e=t.charCodeAt(n))<128?(r+=String.fromCharCode(e),n++):191<e&&e<224?(i=t.charCodeAt(n+1),r+=String.fromCharCode((31&e)<<6|63&i),n+=2):(i=t.charCodeAt(n+1),o=t.charCodeAt(n+2),r+=String.fromCharCode((15&e)<<12|(63&i)<<6|63&o),n+=3);return r})(p):p}function m(t){void 0===t&&(t={}),this.tmpList=[],this.option=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return(Object.assign||function(t){for(var e,o=1,r=arguments.length;o<r;o++)for(var n in e=arguments[o])Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n]);return t}).apply(this,t)}({defaultTrigger:!0,compress:"lzw",key:"SpyLocalCache",interval:500,maxRecordLen:30,onFlush:function(){},onSave:function(){},onAdd:function(){return!0},storage:g.isSupport()?"indexedDB":p.isSupport()?"localstorage":"empty"},t),this.load=this.load.bind(this),this.init()}return m.prototype.init=function(){"indexedDB"===this.option.storage?this.storage=new g:"localstorage"===this.option.storage?this.storage=new p:this.storage=new u,"complete"===document.readyState?this.load():window.addEventListener("load",this.load)},m.prototype.load=function(){-1<location.search.indexOf("_FlushLogLocalCache=1")&&this.option.defaultTrigger&&this.flushLog()},m.prototype.addLog=function(t){var e=this;this.option.onAdd&&!this.option.onAdd(t)||(t=JSON.stringify(t),this.tmpList.push(t),this.timer&&clearTimeout(this.timer),this.timer=setTimeout(function(){e.save()},this.option.interval))},m.prototype.getData=function(r){var n=this;try{this.storage.get(this.option.key,function(o){o?n.storage.get(n.option.key+"Codes",function(t){var e=[];try{t=t&&JSON.parse(t);e=n.unzip(o,t).split("\n")}catch(t){console.error(t)}r(e)}):r([])})}catch(t){console.error(t),r([])}},m.prototype.save=function(){var a=this,s=Date.now();this.getData(function(t){var e=a.option.maxRecordLen-1,e=(t.length>e?t.slice(t.length-e,t.length):t).concat(a.tmpList),t=e.join("\n"),o=null,r=0;try{var n,i=a.zip(t);i.codes?(n=JSON.stringify(i.codes),a.storage.set(a.option.key+"Codes",n)):a.storage.rm(a.option.key+"Codes"),a.storage.set(a.option.key,i.result),r=i.result.length}catch(t){o=t,console.error(t)}a.tmpList=[],a.option.onSave&&a.option.onSave({cost:Date.now()-s,length:r/1024,list:e,error:o})})},m.prototype.flushLog=function(){var o=this;this.getData(function(e){try{for(var t=0;t<e.length;t++)e[t]=JSON.parse(e[t])}catch(t){e=[],o.storage.rm(o.option.key),console.error(t)}for(t=0;t<o.tmpList.length;t++)e.push(JSON.parse(o.tmpList[t]));o.option.onFlush&&o.option.onFlush(e)})},m.prototype.zip=function(t){var e,o;return"lzw"===this.option.compress?{codes:null,result:y(t)}:"huffman"===this.option.compress?(o=(e=n(a(e=t))).result,{codes:e.codes,result:s(o)}):{codes:null,result:t}},m.prototype.unzip=function(t,e){return"lzw"===this.option.compress?y(t,!0):"huffman"===this.option.compress?s(i(e,a(t))):t},m});