spy-client 2.1.1-alpha.3 → 2.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/README.md +1 -0
  2. package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/base.css +224 -0
  3. package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/basicSpec.ts.html +740 -0
  4. package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/block-navigation.js +79 -0
  5. package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/checkSpec.ts.html +479 -0
  6. package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/favicon.png +0 -0
  7. package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/headSpec.ts.html +695 -0
  8. package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/index.html +171 -0
  9. package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/markSpec.ts.html +284 -0
  10. package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/metricSpec.ts.html +977 -0
  11. package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/prettify.css +1 -0
  12. package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/prettify.js +2 -0
  13. package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/sort-arrow-sprite.png +0 -0
  14. package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/sorter.js +170 -0
  15. package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/base.css +224 -0
  16. package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/basicSpec.ts.html +740 -0
  17. package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/block-navigation.js +79 -0
  18. package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/checkSpec.ts.html +479 -0
  19. package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/favicon.png +0 -0
  20. package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/headSpec.ts.html +695 -0
  21. package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/index.html +171 -0
  22. package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/markSpec.ts.html +284 -0
  23. package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/metricSpec.ts.html +977 -0
  24. package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/prettify.css +1 -0
  25. package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/prettify.js +2 -0
  26. package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/sort-arrow-sprite.png +0 -0
  27. package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/sorter.js +170 -0
  28. package/dist/head/base.d.ts +12 -7
  29. package/dist/lib/interface.d.ts +4 -0
  30. package/dist/spy-client-basic.d.ts +7 -4
  31. package/dist/spy-client-basic.esm.js +80 -21
  32. package/dist/spy-client-basic.iife.js +80 -21
  33. package/dist/spy-client-basic.iife.min.js +1 -1
  34. package/dist/spy-client-basic.js +80 -21
  35. package/dist/spy-client-basic.min.js +1 -1
  36. package/dist/spy-client-basic.mjs +80 -21
  37. package/dist/spy-client.d.ts +1 -0
  38. package/dist/spy-client.esm.js +107 -31
  39. package/dist/spy-client.iife.js +107 -31
  40. package/dist/spy-client.iife.min.js +1 -1
  41. package/dist/spy-client.js +107 -31
  42. package/dist/spy-client.min.js +1 -1
  43. package/dist/spy-client.mjs +107 -31
  44. package/dist/spy-head.js +46 -34
  45. package/dist/spy-head.min.js +1 -1
  46. package/dist/spy-local-cache.js +65 -65
  47. package/dist/spy-local-cache.min.js +1 -1
  48. package/example/local-cache.html +77 -15
  49. package/package.json +4 -12
  50. package/src/head/base.ts +37 -4
  51. package/src/head/error.ts +9 -19
  52. package/src/head/whitescreen.ts +14 -14
  53. package/src/lib/huffman.ts +37 -56
  54. package/src/lib/interface.ts +5 -0
  55. package/src/spy-client-basic.ts +95 -26
  56. package/src/spy-client.ts +19 -1
  57. package/src/spy-local-cache.ts +41 -16
@@ -1 +1 @@
1
- !function(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(){"use strict";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)}var c=(e.prototype.preOrder=function(t){t.push(this),this.left&&this.left.preOrder(t),this.right&&this.right.preOrder(t)},e);function e(t,e){this.data=t,this.weight=e}function o(t){if(null==t)return null;var n={};return 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(function(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()}(function(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],u=u[1];e.push(new c(h,u))}return e}(t)));return{result:function(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}(function(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=n[n.length-1],u=e[e.length-1],s="00000000".substring(8-(u-s.length))+s;n[n.length-1]=s;for(var h={},c=0,p=Object.entries(t);c<p.length;c++){var l=p[c],f=l[0];h[l[1]]=f}for(var g=n.join(""),d=[],v=g.length,i=0;i<v;){for(var y=1,a=!0,m=null;a&&i+y<=v;){if(""===(f=g.substring(i,i+y)))break;(m=h[f])?a=!1:y++}d.push(parseInt(m)),i+=y}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}var u=(h.prototype.set=function(t,e){},h.prototype.get=function(t,e){},h.prototype.rm=function(t){},h);function h(){}var p,l=(t(f,p=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)}},f);function f(){return null!==p&&p.apply(this,arguments)||this}var g,d=(t(v,g=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)})},v);function v(){var e=g.call(this)||this;e.databaseName="spyLC",e.db=null,e.setQueue=[],e.getQueue=[];var t=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=function(t){for(var e="",o=0;o<t.length;o++){var r=t.charCodeAt(o);r<128?e+=String.fromCharCode(r):(127<r&&r<2048?e+=String.fromCharCode(r>>6|192):(e+=String.fromCharCode(r>>12|224),e+=String.fromCharCode(r>>6&63|128)),e+=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,p=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[p++]=c+h,c=u):a.hasOwnProperty(u+n)?u+=n:(o(),a[u+n]=p++,u=n);e||o();var l=s.join("");return l=e?function(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}(l):l}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(){},storage:d.isSupport()?"indexedDB":l.isSupport()?"localstorage":"empty"},t),this.load=this.load.bind(this),this.init()}return m.prototype.init=function(){"indexedDB"===this.option.storage?this.storage=new d:"localstorage"===this.option.storage?this.storage=new l: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;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 o=this;this.getData(function(t){var e=o.option.maxRecordLen-1,e=(t.length>e?t.slice(t.length-e,t.length):t).concat(o.tmpList).join("\n"),t=o.zip(e);t.codes?(e=JSON.stringify(t.codes),o.storage.set(o.option.key+"Codes",e)):o.storage.rm(o.option.key+"Codes");e=performance.now();o.storage.set(o.option.key,t.result);e=(100*(performance.now()-e)).toFixed(2);o.option.costTime&&o.option.costTime(e),o.tmpList=[]})},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){return"lzw"===this.option.compress?{codes:null,result:y(t)}:"huffman"===this.option.compress?(e=(o=n(a(e=t))).result,{codes:o.codes,result:s(e)}):{codes:null,result:t};var e,o},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});
1
+ !function(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(){"use strict";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)}var l=(e.prototype.preOrder=function(t){t.push(this),this.left&&this.left.preOrder(t),this.right&&this.right.preOrder(t)},e);function e(t,e){this.data=t,this.weight=e}function n(t){var e=function(t){if(null==t)return null;var i={};return function t(e,o,r){var n=[].concat(r);n.push(o),null!=e&&(null==e.data?(t(e.left,"0",n),t(e.right,"1",n)):i[e.data]=n.join(""))}(t,"",[]),i}(function(t){for(var e=function(t,e){return t.weight-e.weight};1<t.length;){t.sort(e);var o=t.shift(),r=t.shift();if(o&&r){var n=new l(null,o.weight+r.weight);n.left=o,n.right=r,t.unshift(n)}}return t.shift()}(function(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],c=u[1];e.push(new l(h,c))}return e}(t)));return{result:function(t){for(var e=t.join(""),o=Math.ceil(e.length/8),r=new Array(o+1),n=0,i="",a=0;a<e.length;a+=8)i=e.substring(a,a+8),r[n]=parseInt(i,2),n++;return r[n]=i.length,r}(function(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 y(t,e){t&&(e|=256);var o=Number(e).toString(2);return t?o.substring(o.length-8):o}function i(t){for(var e=[],o=0;o<t.length;o++)e.push(t.charCodeAt(o));return e}function a(t){for(var e="",o=0,r=t;o<r.length;o++){var n=r[o];e+=String.fromCharCode(n)}return e}function o(t,e){return a(function(t,e){for(var o=[],r=0;r<e.length-1;r++){var n=r!==e.length-2;o.push(y(n,e[r]))}var i=o[o.length-1],a=e[e.length-1];i="00000000".substring(8-(a-i.length))+i,o[o.length-1]=i;for(var s={},u=0,h=Object.entries(t);u<h.length;u++){var c=h[u],l=c[0];s[c[1]]=l}var p=o.join(""),f=[],d=p.length;for(r=0;r<d;){for(var g=1,v=(n=!0,null);n&&r+g<=d;){if(""===(l=p.substring(r,r+g)))break;(v=s[l])?n=!1:g++}f.push(parseInt(v,10)),r+=g}return f}(t,i(e)))}var s=(u.prototype.set=function(t,e){},u.prototype.get=function(t,e){},u.prototype.rm=function(t){},u);function u(){}var h,c=(t(p,h=s),p.isSupport=function(){return!!window.localStorage},p.prototype.set=function(t,e){try{localStorage.setItem(t,e)}catch(t){console.error(t)}},p.prototype.get=function(t,e){var o=null;try{o=localStorage.getItem(t)}catch(t){console.error(t)}e&&e(o)},p.prototype.rm=function(t){try{localStorage.removeItem(t)}catch(t){console.error(t)}},p);function p(){return null!==h&&h.apply(this,arguments)||this}var f,d=(t(g,f=s),g.isSupport=function(){return!!window.indexedDB},g.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})},g.prototype.get=function(t,e){if(this.db){var o=this.db.transaction([this.databaseName]).objectStore(this.databaseName).get(t);o.onsuccess=function(){var t=o.result?o.result:{value:""};e(t.value)}}else this.getQueue.push({key:t,cb:e})},g.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)})},g);function g(){var e=f.call(this)||this;e.databaseName="spyLC",e.db=null,e.setQueue=[],e.getQueue=[];var t=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 v(t,e){if(void 0===e&&(e=!1),!t)return"";e||(t=function(t){for(var e="",o=0;o<t.length;o++){var r=t.charCodeAt(o);r<128?e+=String.fromCharCode(r):(127<r&&r<2048?e+=String.fromCharCode(r>>6|192):(e+=String.fromCharCode(r>>12|224),e+=String.fromCharCode(r>>6&63|128)),e+=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 e&&(p=function(t){for(var e="",o=0,r=0,n=0,i=0;o<t.length;)(r=t.charCodeAt(o))<128?(e+=String.fromCharCode(r),o++):191<r&&r<224?(n=t.charCodeAt(o+1),e+=String.fromCharCode((31&r)<<6|63&n),o+=2):(n=t.charCodeAt(o+1),i=t.charCodeAt(o+2),e+=String.fromCharCode((15&r)<<12|(63&n)<<6|63&i),o+=3);return e}(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=void 0,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:d.isSupport()?"indexedDB":c.isSupport()?"localstorage":"empty"},t),this.load=this.load.bind(this),this.init()}return m.prototype.init=function(){"indexedDB"===this.option.storage?this.storage=new d:"localstorage"===this.option.storage?this.storage=new c:this.storage=new s,"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 u=this,h=Date.now();this.getData(function(t){var e=u.option.maxRecordLen-1,o=(t.length>e?t.slice(t.length-e,t.length):t).concat(u.tmpList),r=o.join("\n"),n=null,i=0;try{var a=u.zip(r),s="";a.codes?(s=JSON.stringify(a.codes),u.storage.set(u.option.key+"Codes",s)):u.storage.rm(u.option.key+"Codes"),u.storage.set(u.option.key,a.result),i=a.result.length}catch(t){n=t,console.error(t)}u.tmpList=[],u.option.onSave&&u.option.onSave({cost:Date.now()-h,length:i/1024,list:o,error:n})})},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){return"lzw"===this.option.compress?{codes:null,result:v(t)}:"huffman"===this.option.compress?function(t){var e=n(i(t)),o=e.result;return{codes:e.codes,result:a(o)}}(t):{codes:null,result:t}},m.prototype.unzip=function(t,e){return"lzw"===this.option.compress?v(t,!0):"huffman"===this.option.compress?o(e,t):t},m});
@@ -9,11 +9,11 @@
9
9
  // 配置
10
10
  window.__spyHead && window.__spyHead.init({
11
11
  pid: '1_1000',
12
- lid: '',
12
+ lid: 'xx',
13
13
  // 数据类型:异常,触发时间:OnLoadResourceError
14
14
  resourceError: {
15
15
  group: 'resource',
16
- sample: 1,
16
+ sample: 0.1,
17
17
  handler: function (data) {
18
18
 
19
19
  }
@@ -28,7 +28,7 @@
28
28
  },
29
29
  // 数据类型:异常,触发时间:OnJudgeReturnFalseWhenTimeout
30
30
  whiteScreenError: {
31
- sample: 1,
31
+ sample: 0.1,
32
32
  group: 'whiteScreen',
33
33
  selector: 'body',
34
34
  subSelector: 'button1',
@@ -38,15 +38,6 @@
38
38
  }
39
39
  }
40
40
  });
41
-
42
-
43
- function en(c){
44
- var x='charCodeAt',
45
- b, e={},
46
- f=c.split(""),d=[],a=f[0],g=256;for(b=1;b<f.length;b++)c=f[b],null!=e[a+c]?a+=c:(d.push(1<a.length?e[a]:a[x](0)),e[a+c]=g,g++,a=c);d.push(1<a.length?e[a]:a[x](0));for(b=0;b<d.length;b++)d[b]=String.fromCharCode(d[b]);return d.join("")}
47
-
48
- function de(b){var a,e={},d=b.split(""),c=f=d[0],g=[c],h=o=256;for(b=1;b<d.length;b++)a=d[b].charCodeAt(0),a=h>a?d[b]:e[a]?e[a]:f+c,g.push(a),c=a.charAt(0),e[o]=f+c,o++,f=a;return g.join("")}
49
-
50
41
  </script>
51
42
  </head>
52
43
  <body>
@@ -60,19 +51,90 @@
60
51
 
61
52
  </div>
62
53
  <script src="https://mss0.bdstatic.com/se/static/js/iphone/zbios/zbiosT_f69.js"></script>
54
+ <script src="/dist/spy-client.js"></script>
63
55
  <script src="/dist/spy-local-cache.js"></script>
64
56
 
65
57
  <script type="text/javascript">
66
58
  const localCache = new SpyLocalCache({
67
59
  storage: 'localstorage',
68
- // compress: 'huffman',
69
- // compress: 'no',
70
- compress: 'lzw',
60
+ maxRecordLen: 300,
61
+ compress: 'no',
62
+ onAdd(log) {
63
+ // 仅保存性能异常
64
+ console.log('onAdd', log && log.type && (log.type === 'perf' || log.type === 'except'));
65
+ return log && log.type && (log.type === 'perf' || log.type === 'except');
66
+ },
71
67
  onFlush(list) {
72
68
  console.log('flushed list', list);
69
+
70
+ if (list.length) {
71
+ // 将回捞的日志上报的spy,记住在spy平台配置 对应group
72
+ spy.sendPost({
73
+ pid: '1_1000',
74
+ type: 'except',
75
+ group: 'localCache',
76
+ ts: Date.now(),
77
+ lid: 'xxxx',
78
+ info: {
79
+ msg: 'localcache',
80
+ pageUrl: location.href,
81
+ log: JSON.stringify(list)
82
+ }
83
+ });
84
+ }
85
+ }
86
+ });
87
+
88
+
89
+ const spy = new SpyClient({
90
+ pid: '1_1000',
91
+ lid: 'xxxx',
92
+ localCache: localCache
93
+ });
94
+ // 采用spy-client sdk内置的抽样配置,则此异常日志不管是否命中抽样,都会存在本地,同时若命中抽样,则日志发送给服务端
95
+ // 那么所有spy sdk发送的日志会存到localstorage
96
+ spy.sendExcept({
97
+ info: {
98
+ msg: 'xxxx is not undefined'
99
+ }
100
+ });
101
+
102
+
103
+ // 若自行做了抽样,那么此时则需要主动将此异常或性能日志存在本地,才能百分百保存异常信息到本地
104
+ localCache.addLog({
105
+ type: 'except',
106
+ ts: Date.now(),
107
+ lid: 'xxx',
108
+ info: {
109
+ msg: 'module "jquery" miss',
110
+ pageUrl: location.href,
111
+ }
112
+ });
113
+ if (Math.random() < 0.1) {
114
+ spy.sendExcept({
115
+ info: {
116
+ msg: 'module "jquery" miss',
117
+ pageUrl: location.href,
118
+ }
119
+ });
120
+ }
121
+
122
+
123
+ spy.sendPerf({
124
+ // 可选, 分组,默认common,用户自定义
125
+ group: 'test',
126
+ // 必须, 指标信息,每个字段为一个指标,由用户自定义
127
+ info: {
128
+ fisrtScreen: 200, // 需要你自行计算好时间再发送,不能带单位
73
129
  },
130
+ // 可选,维度信息,每个字段为一个维度,由用户自定义
131
+ dim: {
132
+ os: 'ios',
133
+ netType: 'wifi'
134
+ }
74
135
  });
75
136
 
137
+
76
138
  // 当有22条如下日志时
77
139
  // lzw 压缩率 68%,耗时 2ms
78
140
  // huffman压缩率 21% ,耗时2ms
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spy-client",
3
- "version": "2.1.1-alpha.3",
3
+ "version": "2.1.3",
4
4
  "description": "spy client",
5
5
  "main": "dist/spy-client.js",
6
6
  "module": "dist/spy-client.esm.js",
@@ -15,8 +15,8 @@
15
15
  "lint": "eslint src/**/*.ts",
16
16
  "example": "npm run dev && echo 'open url http://localhost:8000/example' && python -m SimpleHTTPServer 8000 ",
17
17
  "test": "karma start karma.conf.js",
18
- "release_pre": "npm version patch",
19
- "release": "rm -fr dist && npm run build && npm run lint && npm run test && npm publish",
18
+ "release_pre": "rm -fr dist && npm run build && npm run lint && npm run test",
19
+ "release": "npm version patch && npm publish",
20
20
  "release_post": "git push origin master && git push origin --tags"
21
21
  },
22
22
  "directories": {
@@ -101,15 +101,7 @@
101
101
  "license": "ISC",
102
102
  "babel": {
103
103
  "comments": false,
104
- "presets": [
105
- [
106
- "@babel/preset-env",
107
- {
108
- "useBuiltIns": "usage",
109
- "corejs": 3
110
- }
111
- ]
112
- ],
104
+ "presets": [],
113
105
  "env": {
114
106
  "test": {
115
107
  "plugins": [
package/src/head/base.ts CHANGED
@@ -7,20 +7,53 @@ import {
7
7
  SpyHeadConf,
8
8
  } from '../lib/spyHeadInterface';
9
9
 
10
+ interface SendObj {
11
+ type?: 'perf'| 'except' | 'dist' | 'count';
12
+ group: string;
13
+ info: any;
14
+ dim?: any;
15
+ lid?: string;
16
+ pid?: string;
17
+ ts?: number;
18
+ }
19
+
10
20
  export default {
11
21
  conf: {} as SpyHeadConf,
12
- winerrors: [] as any,
22
+ winerrors: [] as SendObj[],
13
23
  errorDestroy() {},
14
24
  observerDestroy() {},
15
25
  entryMap: {} as any,
16
26
  init(conf: SpyHeadConf) {
17
27
  this.conf = conf;
18
28
  },
19
- send(obj: {type?: 'perf'| 'except' | 'dist' | 'count', group: string, info: any, dim?: any}, logServer?: string) {
20
- obj.type = obj.type || 'except';
29
+ addError(obj: SendObj) {
30
+ // 有些错误一下出现很多次,都聚合都一个错误,加上次数
31
+ if (this.winerrors.length > 0) {
32
+ const lastObj = this.winerrors[this.winerrors.length - 1];
33
+ if (obj.info.msg === lastObj.info.msg) {
34
+ lastObj.info.count += (lastObj.info.count || 0);
35
+ return;
36
+ }
37
+ }
38
+ if (this.winerrors.length < 1000) {
39
+ this.winerrors.push(obj);
40
+ }
41
+ },
42
+ send(obj: SendObj, isSend?: boolean, logServer?: string) {
21
43
  const conf = this.conf;
44
+ obj.type = obj.type || 'except';
45
+ obj.pid = conf.pid;
46
+ obj.lid = conf.lid;
47
+ obj.ts = Date.now();
48
+
49
+ this.addError(obj);
50
+ this.interceptor && this.interceptor(obj);
51
+ if (isSend === false) {
52
+ return;
53
+ }
54
+
22
55
  logServer = logServer || conf.logServer;
23
- let logUrl = `${logServer}?pid=${conf.pid}&lid=${conf.lid}&ts=${Date.now()}`
56
+ let logUrl = `${logServer}?pid=${obj.pid}&lid=${obj.lid}&ts=${obj.ts}`
24
57
  + `&type=${obj.type}&group=${obj.group}&info=${encodeURIComponent(JSON.stringify(obj.info))}`;
25
58
 
26
59
  if (obj.dim) {
package/src/head/error.ts CHANGED
@@ -58,10 +58,10 @@ export function init(conf: SpyHeadConf) {
58
58
 
59
59
  // 历史错误
60
60
  const historys = [];
61
- for (let index = 0; index < spyHead.winerrors.length; index++) {
62
- const item = spyHead.winerrors[index];
63
- const prefix = item.count > 1 ? `(${item.count})` : '';
64
- historys.push(prefix + (item.msg as string));
61
+ for (let index = 0; index < winerrors.length; index++) {
62
+ const item = winerrors[index];
63
+ const prefix = item.info.count > 1 ? `(${item.info.count})` : '';
64
+ historys.push(prefix + (item.info.msg as string));
65
65
  }
66
66
 
67
67
  info.hisErrors = historys.join('----');
@@ -71,8 +71,8 @@ export function init(conf: SpyHeadConf) {
71
71
  allow = jsError.handler(obj);
72
72
  }
73
73
 
74
- if (allow !== false && isSendJserror) {
75
- spyHead.send(obj);
74
+ if (allow !== false) {
75
+ spyHead.send(obj, isSendJserror);
76
76
  }
77
77
  }
78
78
  // 资源错误
@@ -98,22 +98,12 @@ export function init(conf: SpyHeadConf) {
98
98
  allow = resourceError.handler(obj);
99
99
  }
100
100
 
101
- if (allow !== false && isSendResource) {
102
- spyHead.send(obj);
101
+ if (allow !== false) {
102
+ spyHead.send(obj, isSendResource);
103
103
  }
104
104
 
105
105
  resourceErrorCount++;
106
106
  }
107
-
108
- // 有些错误一下出现很多次,都聚合都一个错误,加上次数
109
- if (winerrors.length > 0) {
110
- const lastInfo = winerrors[winerrors.length - 1];
111
- if (info.msg === lastInfo.msg) {
112
- lastInfo.count += (lastInfo.count || 0);
113
- return;
114
- }
115
- }
116
- winerrors.push(info);
117
107
  }
118
108
  catch (e) {
119
109
  console.error(e);
@@ -122,7 +112,7 @@ export function init(conf: SpyHeadConf) {
122
112
  window.addEventListener('error', spyListenError, true);
123
113
  spyHead.errorDestroy = function () {
124
114
  window.removeEventListener('error', spyListenError, true);
125
- spyHead.winerrors = null;
115
+ spyHead.winerrors = [];
126
116
  };
127
117
  }
128
118
 
@@ -42,8 +42,8 @@ export function init(conf: SpyHeadConf) {
42
42
  const errors = spyHead.winerrors;
43
43
  const historys = [];
44
44
  for (let i = 0; i < errors.length; i++) {
45
- const stack = (errors[i].stack || '').split('\n')[0];
46
- historys.push(`(${i })${stack || errors[i].msg}`);
45
+ const stack = (errors[i].info.stack || '').split('\n')[0];
46
+ historys.push(`(${i })${stack || errors[i].info.msg}`);
47
47
  }
48
48
  return historys.join(';;');
49
49
  }
@@ -76,19 +76,19 @@ export function init(conf: SpyHeadConf) {
76
76
  return false;
77
77
  }
78
78
 
79
- if (isSend) {
79
+ if (selector) {
80
80
  setTimeout(function () {
81
- const obj = {
82
- group: whiteScreenError.group,
83
- info: {
84
- msg: '',
85
- netTime: getNetTime(),
86
- hisErrors: getHisError(),
87
- deviceInfo: getDeviceInfo(),
88
- },
89
- } as ErrorHandlerData;
90
-
91
81
  if (isWhiteScreen()) {
82
+ const obj = {
83
+ group: whiteScreenError.group,
84
+ info: {
85
+ msg: '',
86
+ netTime: getNetTime(),
87
+ hisErrors: getHisError(),
88
+ deviceInfo: getDeviceInfo(),
89
+ },
90
+ } as ErrorHandlerData;
91
+
92
92
  obj.info.msg = 'WhiteScren Error';
93
93
 
94
94
  let allow: boolean | undefined | void = true;
@@ -97,7 +97,7 @@ export function init(conf: SpyHeadConf) {
97
97
  }
98
98
 
99
99
  if (allow !== false && obj.info.msg) {
100
- spyHead && spyHead.send(obj);
100
+ spyHead && spyHead.send(obj, isSend);
101
101
  }
102
102
  }
103
103
  }, timeout);
@@ -46,16 +46,17 @@ class HuffmanNode {
46
46
  * @param {接受字符数组} bytes
47
47
  * @return 返回的就是list形式
48
48
  */
49
- function getNodes(bytes: Array<any>): HuffmanNode[] {
50
- //创建一个list
49
+ function getNodes(bytes: any[]): HuffmanNode[] {
50
+ // 创建一个list
51
51
  let list: HuffmanNode[] = [];
52
- //counts 统计每一个byte出现的次数
52
+ // counts 统计每一个byte出现的次数
53
53
  let counts: any = {};
54
54
  for (let b of bytes) {
55
55
  let count = counts[b]; // map还没有这个字符数据
56
56
  if (count == null) {
57
57
  counts[b] = 1;
58
- } else {
58
+ }
59
+ else {
59
60
  counts[b]++;
60
61
  }
61
62
  }
@@ -84,7 +85,7 @@ function createHuffmanTree(nodes: HuffmanNode[]): HuffmanNode | undefined {
84
85
  parent.left = leftNode;
85
86
  parent.right = rightNode;
86
87
 
87
- //将新的二叉树,加入到nodes
88
+ // 将新的二叉树,加入到nodes
88
89
  nodes.unshift(parent);
89
90
  }
90
91
 
@@ -118,44 +119,45 @@ function getHuffmanCodes(root: any) {
118
119
  string2.push(code);
119
120
  if (node != null) { // 如果node == null不处理
120
121
  // 判断当前node是叶子节点还是非叶子节点
121
- if (node.data == null) {//非叶子节点
122
+ if (node.data == null) { // 非叶子节点
122
123
  // 递归处理
123
124
  // 向左递归
124
125
  getCodes(node.left, '0', string2);
125
126
  // 向右递归
126
- getCodes(node.right, '1', string2)
127
- } else {//说明是一个叶子节点
127
+ getCodes(node.right, '1', string2);
128
+ }
129
+ else { // 说明是一个叶子节点
128
130
  // 就表示找到了某个叶子节点的最后
129
131
  huffmanCodes[node.data] = string2.join('');
130
132
  }
131
133
  }
132
134
  }
133
- getCodes(root, "", strings);
135
+ getCodes(root, '', strings);
134
136
  return huffmanCodes;
135
137
  }
136
138
 
137
- //编写一个方法,将字符串对应的bytes数组,通过生成的赫夫曼编码表,返回一个赫夫曼编码压缩后的byte数组
139
+
138
140
  /**
139
- *
141
+ * 编写一个方法,将字符串对应的bytes数组,通过生成的赫夫曼编码表,返回一个赫夫曼编码压缩后的byte数组
140
142
  * @param {原始的字符串对应的bytes数组} bytes
141
143
  * @param {生成的赫夫曼编码表} huffmanCodes
142
144
  * @return 返回的是字符串对应的一个byte数组
143
145
  */
144
- function zip(bytes: Array<number>, huffmanCodes: any) {
145
- //1.利用huffmanCodes将bytes转成赫夫曼编码对应的字符串
146
+ function zip(bytes: number[], huffmanCodes: any) {
147
+ // 1.利用huffmanCodes将bytes转成赫夫曼编码对应的字符串
146
148
  let string = [];
147
- //遍历数组
149
+ // 遍历数组
148
150
  for (let b of bytes) {
149
151
  string.push(huffmanCodes[b]);
150
152
  }
151
153
  return string;
152
154
  }
153
155
 
154
- function huffStringToByte(strs: Array<string>) {
155
- //计算赫夫曼编码字符串的长度
156
+ function huffStringToByte(strs: string[]) {
157
+ // 计算赫夫曼编码字符串的长度
156
158
  let str = strs.join('');
157
159
  let len = Math.ceil(str.length / 8);
158
- //创建存储压缩后的byte数组
160
+ // 创建存储压缩后的byte数组
159
161
  let huffmanCodeByte = new Array(len + 1);
160
162
  let index = 0;
161
163
  let strByte: string = ''; // 记录是第几个byte
@@ -178,7 +180,7 @@ function huffStringToByte(strs: Array<string>) {
178
180
  * @returns 是经过赫夫曼编码处理后,压缩后的字节数组
179
181
  *
180
182
  */
181
- function huffmanZip(bytes: Array<any>) {
183
+ function huffmanZip(bytes: any[]) {
182
184
  // 1.生成节点数组
183
185
  let nodes = getNodes(bytes);
184
186
  // 2.根据节点数组创建赫夫曼树
@@ -204,31 +206,30 @@ function huffmanZip(bytes: Array<any>) {
204
206
  * @returns 是byte对应的二进制字符串
205
207
  */
206
208
  function huffByteToString(flag: boolean, byte: number) {
207
- //如果是
209
+ // 如果是
208
210
  if (flag) {
209
211
  byte |= 256;
210
212
  }
211
- let str = Number(byte).toString(2)
213
+ let str = Number(byte).toString(2);
212
214
  if (flag) {
213
215
  return str.substring(str.length - 8);
214
- } else {
215
- return str;
216
216
  }
217
+ return str;
217
218
  }
218
219
 
219
- //编写一份方法,完成对压缩数据的解码
220
+
220
221
  /**
221
- *
222
+ * 编写一份方法,完成对压缩数据的解码
222
223
  * @param {赫夫曼编码表} huffmanCodes
223
224
  * @param {赫夫曼编码得到的二进制数组} huffmanBytes
224
225
  */
225
- function decode(huffmanCodes: {[key:string]: string}, huffmanBytes: Array<number>) {
226
+ function decode(huffmanCodes: {[key: string]: string}, huffmanBytes: number[]) {
226
227
  // 1.先得到二进制字符串 形式11001111111011......
227
228
  let heffmanStrArr = [];
228
229
  for (let i = 0; i < huffmanBytes.length - 1; i++) {
229
- //判断是不是最后一个字节
230
+ // 判断是不是最后一个字节
230
231
  let flag = (i !== huffmanBytes.length - 2);
231
- heffmanStrArr.push(huffByteToString(flag, huffmanBytes[i]))
232
+ heffmanStrArr.push(huffByteToString(flag, huffmanBytes[i]));
232
233
  }
233
234
  // 最后一位记录的是最后一位二进制字符串的长度,该长度主要用于补足最后一位丢失的0,所以要单独处理,
234
235
  let lastByteStr = heffmanStrArr[heffmanStrArr.length - 1];
@@ -237,7 +238,7 @@ function decode(huffmanCodes: {[key:string]: string}, huffmanBytes: Array<number
237
238
  heffmanStrArr[heffmanStrArr.length - 1] = lastByteStr;
238
239
 
239
240
  // 把赫夫曼编码表进行调换
240
- let map: {[key:string]: string} = {};
241
+ let map: {[key: string]: string} = {};
241
242
  for (const [key, value] of Object.entries(huffmanCodes)) {
242
243
  map[value] = key;
243
244
  }
@@ -256,14 +257,15 @@ function decode(huffmanCodes: {[key:string]: string}, huffmanBytes: Array<number
256
257
  break;
257
258
  }
258
259
  b = map[key];
259
- if (!b) {//没有匹配到
260
+ if (!b) { // 没有匹配到
260
261
  count++;
261
- } else {
262
- //匹配到
262
+ }
263
+ else {
264
+ // 匹配到
263
265
  flag = false;
264
266
  }
265
267
  }
266
- list.push(parseInt(b as string));
268
+ list.push(parseInt(b as string, 10));
267
269
  i += count;
268
270
  }
269
271
  // 当for循环结束后,list中就存放了所有的字符
@@ -273,7 +275,7 @@ function decode(huffmanCodes: {[key:string]: string}, huffmanBytes: Array<number
273
275
 
274
276
 
275
277
  // js byte[] 和string 相互转换 UTF-8
276
- function stringToByte(str: string): Array<number> {
278
+ function stringToByte(str: string): number[] {
277
279
  let bytes = [];
278
280
  for (let index = 0; index < str.length; index++) {
279
281
  bytes.push(str.charCodeAt(index));
@@ -281,7 +283,7 @@ function stringToByte(str: string): Array<number> {
281
283
  return bytes;
282
284
  }
283
285
 
284
- function byteToString(arr: Array<number>): string {
286
+ function byteToString(arr: number[]): string {
285
287
  let data = '';
286
288
  for (const code of arr) {
287
289
  data += String.fromCharCode(code);
@@ -294,7 +296,7 @@ export function huffmanEncode(str: string) {
294
296
  let {result, codes} = huffmanZip(bytes);
295
297
  return {
296
298
  codes,
297
- result: byteToString(result)
299
+ result: byteToString(result),
298
300
  };
299
301
  }
300
302
 
@@ -303,24 +305,3 @@ export function huffmanDecode(codes: any, str: string) {
303
305
  const data = decode(codes, bytes);
304
306
  return byteToString(data);
305
307
  }
306
-
307
- // For test
308
- // let content = JSON.stringify({
309
- // type: 3,
310
- // fm: 'disp',
311
- // data: [{"base":{"size":{"doc":{"w":360,"h":4875},"wind":{"w":360,"h":640},"scr":{"w":360,"h":640}},"vsb":"visible","num":16},"t":1629773746698,"path":"/s"}],
312
- // qid: 10991431029479106376,
313
- // did: '8dd09c47c7bc90c9fd7274f0ad2c581e',
314
- // q: '刘德华',
315
- // t: 1629773746698
316
- // });
317
-
318
- // console.log('压缩前的字符串', content, '其长度:', content.length);
319
-
320
- // const res = huffmanEncode(content);
321
- // console.log('压缩后的字符串长度', res.result.length);
322
- // console.log('压缩后的字符串', res.result);
323
-
324
- // const out = huffmanDecode(res.codes, res.result);
325
- // console.log('解压后的字符串', out, '其长度:', out.length);
326
- // console.log('解压后的数据', JSON.stringify(JSON.parse(out)));
@@ -24,6 +24,11 @@ export interface SpyClientOption {
24
24
  * 日志服务器,默认是webb服务器,尾部需要加?
25
25
  */
26
26
  logServer?: string;
27
+
28
+ /**
29
+ * 本地日志cache对象,是指独立的spy-local-cache模块实例
30
+ */
31
+ localCache?: any;
27
32
  }
28
33
 
29
34
  export class Module {