UNPKG

monitor-usage

Version:

a code monitoring tool based on Javascript, used to monitor and track target code usage in real time, collect code running log reports, etc.

2 lines 11 kB
!function(){"use strict";function e(t){return e="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(t)}function t(e){return Object.prototype.toString.call(e).match(/\[(.*?)\]/)[1].slice(7)}function r(){return("undefined"==typeof window||"undefined"==typeof document)&&"undefined"!=typeof process}function n(){return(new Date).toLocaleString()}function o(e){return JSON.stringify(e,(function(e,t){return"function"==typeof t?"".concat(t):void 0===t?"undefined":t!=t?"NaN":t instanceof Map?{dataType:"Map",val:Array.from(t.entries())}:t instanceof Set?{dataType:"Set",val:Array.from(t.entries())}:t}))}function a(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}var i=null;function s(e){if(!r()){var t,n,o,s,c,l,u,p;i||(n=document.createElement("div"),o=document.createElement("div"),s=document.createElement("span"),c=document.createElement("h2"),l=document.createElement("h2"),u=document.createElement("h3"),p=document.createElement("div"),o.className="__err_view__",s.className="__err_view_close__",c.className="__err_view_h2__",l.className="__err_view_h2__",u.className="__err_view_h3__",n.className="__err_view_mask__",p.id="errViewInfo",c.textContent="[MonitorUsage]",u.textContent="Error stack",s.onclick=function(){return n.style.display="none"},o.append(s,c,l,u),n.appendChild(o),i=n,document.body.append(((t=document.createElement("style")).textContent="\n .__err_view_mask__ {\n display: none;\n position: fixed;\n width: 100%;\n height: 100%;\n background-color: rgba(0, 0, 0, 0.7);\n \n & .__err_view__ {\n width: 680px;\n height: 420px;\n max-height: 420px;\n position: absolute;\n top: 40%;\n left: 50%;\n transform: translate(-50%, -50%);\n padding: 6px 24px;\n border-radius: 6px;\n overflow: auto;\n color: #ff4d4f;\n background-color: #e6e7eb;\n \n & .__err_view_close__ {\n position: absolute;\n right: 20px;\n top: 10px;\n cursor: pointer;\n }\n \n & .__err_view_h2__ {\n font-size: 1.5em;\n }\n \n & .__err_view_h3__ {\n font-size: 1.171em;\n }\n }\n }\n ",t),i));var f=e.stack.split("\n");f.shift();var d=document.querySelector("#errViewInfo"),y=document.querySelector(".__err_view_h2__"),m=f.map((function(e){var t=document.createElement("div");return t.textContent=e,t}));y.textContent=e.message,d.innerHTML=null,d.append.apply(d,function(e){if(Array.isArray(e))return a(e)}(g=m)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(g)||function(e,t){if(e){if("string"==typeof e)return a(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?a(e,t):void 0}}(g)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()),i.style.display="block"}var g}function c(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function l(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?c(Object(r),!0).forEach((function(t){f(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):c(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function u(e){return u="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},u(e)}function p(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,d(n.key),n)}}function f(e,t,r){return(t=d(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function d(e){var t=function(e,t){if("object"!==u(e)||null===e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,"string");if("object"!==u(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"===u(t)?t:String(t)}var y=["Map","Set","WeakMap"],m={key:void 0,isModifyValue:!0,showErrorView:!1,allowAdditionalProps:!0},g=function(){function a(){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,a)}var i,c,f;return i=a,c=[{key:"getReportObj",value:function(e){return{status:"OK",source:e,useageName:"",usageCount:0,usageType:"Unkown",usageTime:n(),dailyUsageRate:"0%",info:"no problem",oldValue:void 0,newValue:void 0,isNewlyAdded:!1,whenToAdd:void 0,lastModify:void 0,position:"",filepath:""}}},{key:"createStares",value:function(r,i,c,l){var p,f=this;if("string"==typeof(p=r)||"number"==typeof p||"boolean"==typeof p||"symbol"===e(p)||"bigint"==typeof p||null==p)throw TypeError("[MonitorUsage error]: you must pass in non-basic data types as monitoring source");a.saveTraps.set(r,i),c.key&&!(c.key in a.recordsToExport)&&(a.recordsToExport[c.key]=[]);var d=this,m=function(){return f.getReportObj(null!=r&&"object"===u(r)?o(r):r.toString())},g=function(e,t){var r,o=(null!==(r=a.keyUsesMap[e])&&void 0!==r?r:0)+1;t.usageCount=o,t.usageTime=n();var i=o/24;t.dailyUsageRate=i>100?"100%":i>0&&i<1?"1%":i.toPrecision(3)+"%",a.keyUsesMap[e]=o},v=function(e,t,r){a.trap({target:e,repo:t,err:r,recordKey:c.key})},w=function(e){return l?"".concat(String(l),".").concat(String(e)):e},h={has:function(e,t){var r=m(),n=Reflect.has(e,t);return r.useageName=w(t),r.usageType="Has",r.info="this property exists in source",n||(r.status="Fail",r.info="this property does not exist in the target"),g(t,r),v(e,r,new Error),n},get:function(e,r,n){var o=m(),a=Reflect.get(e,r,n);if(null!=a&&"object"===u(a))return d.createStares(a,i,c,r);o.usageType="Get",o.useageName=w(r),g(r,o);var l=t(e);if(y.includes(t(e)))return o.usageType="".concat(l," ").concat(r.toString()),v(e,o,new Error),a.bind(e);if("Function"===t(a)){o.usageType="Method Call";try{o.info="object method is called",a.apply(e)}catch(t){c.showErrorView&&s(t),o.status="Fail",o.info="an error occurred while calling the method",console.error("[MonitorUsage error]: an error occurred while calling the method","\n key: ",r,"\n target: ",e,"\n",t)}finally{v(e,o,new Error)}}else Reflect.has(e,r)?(o.info="property accessed",v(e,o,new Error)):c.allowAdditionalProps?(o.isNewlyAdded=!0,o.info="this is a newly added property",Reflect.set(e,r,void 0),v(e,o,new Error),console.log("[MonitorUsage info]: added a new property","\n key: ",r,"\n target: ",e,"\n")):(o.status="Fail",o.info="this property does not exist in the source",v(e,o,new Error),console.warn("[MonitorUsage warn]: cannot add new properties because 'options.allowAdditionalProps' is set to 'false'"));return a},set:function(e,t,r,o){var a=m(),i=Reflect.get(e,t,o);return a.usageType="Modify",a.oldValue=i,a.useageName=w(t),g(t,a),c.isModifyValue?Reflect.has(e,t)?Object.is(i,r)?(a.info="ignore updating old and new equal values",v(e,a,new Error),!1):(a.info="update value",a.newValue=r.toString(),a.lastModify=n(),v(e,a,new Error),Reflect.set(e,t,r,o)):(a.isNewlyAdded=!0,a.info="a newly set property",a.oldValue=r.toString(),v(e,a,new Error),console.log("[MonitorUsage info]: added a new property","\n key: ",t,"\n target: ",e,"\n"),Reflect.set(e,t,r,o)):(a.status="Fail",a.info="this value is not allowed to be modified",v(e,a,new Error),console.warn("[MonitorUsage warn]: cannot modify value because 'options.isModifyValue' is set to 'false'"),!1)},deleteProperty:function(e,t){var r=m(),n=Reflect.deleteProperty(e,t);return r.useageName=w(t),r.usageType="Delete",r.info="the property has been deleted",n||(r.status="Fail",r.info="the property cannot be deleted"),v(e,r,new Error),n},apply:function(e,t,r){var n=m(),i=(null==e?void 0:e.name)||"anonymous_fid_".concat(++a.anonymousFid);n.useageName=i,n.usageType="Function Call",g(i,n);try{var l=Reflect.apply(e,t,r);return n.info="is called with arguments: ".concat(o(r),", and the result of the call is ").concat(o(l)),l}catch(t){c.showErrorView&&s(t),n.status="Fail",n.info="function call error",console.error("[MonitorUsage error]: function call error","\n useageName: ",i,"\n function: ",o(e),"\n",t)}finally{v(e,n,new Error)}},construct:function(e,t,r){var n=m(),a=r.name;n.usageType="Construct Function",g(a,n);try{return n.info="constructor called with arguments: ".concat(o(t)),Reflect.construct(e,t,r)}catch(e){c.showErrorView&&s(e),n.status="Fail",n.info="an error occurred construct function",console.error("[MonitorUsage error]: an error occurred construct function","\n useageName: ",a,"\n function: ",o(r),"\n",e)}finally{v(e,n,new Error)}},ownKeys:function(e){var t=m();return t.useageName="@@the key of the entire source",t.info="target is traversed",t.usageTime=n(),v(e,t,new Error),Reflect.ownKeys(e)}};return new Proxy(r,h)}},{key:"stares",value:function(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:m;if(a.proxyMemo.has(e))return a.proxyMemo.get(e);var n=this.createStares(e,t,r);return a.proxyMemo.set(e,n),n}},{key:"export",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"reports",t=JSON.stringify(a.recordsToExport);return r(),new Promise((function(r,n){try{var o=new Blob([t],{type:"application/json"}),a=URL.createObjectURL(o),i=document.createElement("a");i.href=a,i.download="".concat(e,".json"),i.click(),r(a),URL.revokeObjectURL(a)}catch(e){n(e)}}))}}],f=[{key:"gcl",value:function(e){var t=e.stack.split("\n")[2].trim(),r=t.split(":"),n=r.pop().replace(")",""),o=r.pop();return{filepath:t.match(/\((.*?)\)/)[1],position:"trace the code to line ".concat(o,", row ").concat(n)}}},{key:"trap",value:function(e){var t,r=e.target,n=e.repo,o=e.err,a=e.recordKey,i=e.callback;this.saveTraps.has(r)||this.saveTraps.set(r,i),this.tempRecords.has(r)||this.tempRecords.set(r,[]);var s=this.saveTraps.get(r),c=this.tempRecords.get(r).slice(),u=l(l({},n),this.gcl(o));c.push(u),null==s||s(c),this.tempRecords.set(r,c),a&&(null===(t=this.recordsToExport[a])||void 0===t||t.push(u))}}],c&&p(i.prototype,c),f&&p(i,f),Object.defineProperty(i,"prototype",{writable:!1}),a}();f(g,"proxyMemo",new WeakMap),f(g,"recordsToExport",{}),f(g,"keyUsesMap",{}),f(g,"tempRecords",new WeakMap),f(g,"saveTraps",new WeakMap),f(g,"anonymousFid",0);var v;r()||(window.MonitorUsage=function(){return v||(v=new g),v})}(); //# sourceMappingURL=monitor-usage.js.map