@mitojs/core
Version:
2 lines (1 loc) • 10.5 kB
JavaScript
var MITO=function(t){"use strict";var e=function(){return(e=Object.assign||function(t){for(var e,r=1,n=arguments.length;r<n;r++)for(var i in e=arguments[r])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t}).apply(this,arguments)};function r(t,e,r,n){return new(r||(r=Promise))((function(i,o){function a(t){try{c(n.next(t))}catch(t){o(t)}}function s(t){try{c(n.throw(t))}catch(t){o(t)}}function c(t){var e;t.done?i(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(a,s)}c((n=n.apply(t,e||[])).next())}))}function n(t,e){var r,n,i,o,a={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function s(o){return function(s){return function(o){if(r)throw new TypeError("Generator is already executing.");for(;a;)try{if(r=1,n&&(i=2&o[0]?n.return:o[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;switch(n=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return a.label++,{value:o[1],done:!1};case 5:a.label++,n=o[1],o=[0];continue;case 7:o=a.ops.pop(),a.trys.pop();continue;default:if(!(i=a.trys,(i=i.length>0&&i[i.length-1])||6!==o[0]&&2!==o[0])){a=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]<i[3])){a.label=o[1];break}if(6===o[0]&&a.label<i[1]){a.label=i[1],i=o;break}if(i&&a.label<i[2]){a.label=i[2],a.ops.push(o);break}i[2]&&a.ops.pop(),a.trys.pop();continue}o=e.call(t,a)}catch(t){o=[6,t],n=0}finally{r=i=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,s])}}}function i(t,e,r){if(r||2===arguments.length)for(var n,i=0,o=e.length;i<o;i++)!n&&i in e||(n||(n=Array.prototype.slice.call(e,0,i)),n[i]=e[i]);return t.concat(n||Array.prototype.slice.call(e))}var o=Object.prototype.toString;function a(t){return function(e){return o.call(e)==="[object "+t+"]"}}var s={isNumber:a("Number"),isString:a("String"),isBoolean:a("Boolean"),isNull:a("Null"),isUndefined:a("Undefined"),isSymbol:a("Symbol"),isFunction:a("Function"),isObject:a("Object"),isArray:a("Array"),isProcess:a("process"),isWindow:a("Window")};var c=s.isProcess("undefined"!=typeof process?process:0),u=s.isObject("undefined"!=typeof wx?wx:0)&&s.isFunction("undefined"!=typeof App?App:0);var l=s.isWindow("undefined"!=typeof window?window:0)?window:u?wx:c?process:void 0,f=(l.__MITO__=l.__MITO__||{},l.__MITO__);var p="MITO Logger",h=function(){function t(){var t=this;if(this.enabled=!1,this._console={},l.console=console||l.console,console||l.console){["log","debug","info","warn","error","assert"].forEach((function(e){e in l.console&&(t._console[e]=l.console[e])}))}}return t.prototype.disable=function(){this.enabled=!1},t.prototype.bindOptions=function(t){this.enabled=!!t},t.prototype.enable=function(){this.enabled=!0},t.prototype.getEnableStatus=function(){return this.enabled},t.prototype.log=function(){for(var t,e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];this.enabled&&(t=this._console).log.apply(t,i([p+"[Log]:"],e,!1))},t.prototype.warn=function(){for(var t,e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];this.enabled&&(t=this._console).warn.apply(t,i([p+"[Warn]:"],e,!1))},t.prototype.error=function(){for(var t,e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];(t=this._console).error.apply(t,i([p+"[Error]:"],e,!1))},t}(),d=f.logger||(f.logger=new h);function b(t,e,r){return!!function(t,e){return o.call(t)==="[object "+e+"]"}(t,r)||(void 0!==t&&d.error(e+"期望传入:"+r+"类型,当前是:"+o.call(t)+"类型"),!1)}function y(t){var e=this;t.forEach((function(t){var r=t[0],n=t[1];return b(r,n,t[2])&&(e[n]=r)}))}var g,m=function(){function t(){this.stack=[],this.isFlushing=!1,"Promise"in l&&(this.micro=Promise.resolve())}return t.prototype.addTask=function(t){var e=this;"function"==typeof t&&("Promise"in l?(this.stack.push(t),this.isFlushing||(this.isFlushing=!0,this.micro.then((function(){return e.flushStack()})))):t())},t.prototype.clear=function(){this.stack=[]},t.prototype.getStack=function(){return this.stack},t.prototype.flushStack=function(){var t=this.stack.slice(0);this.stack.length=0,this.isFlushing=!1;for(var e=0,r=t;e<r.length;e++){(0,r[e])()}},t}();!function(t){t.Else="else",t.Error="error",t.Warning="warning",t.Info="info",t.Debug="debug",t.Low="low",t.Normal="normal",t.High="high",t.Critical="critical"}(g||(g={})),function(t){t.fromString=function(e){switch(e){case"debug":return t.Debug;case"info":case"log":case"assert":return t.Info;case"warn":case"warning":return t.Warning;case t.Low:case t.Normal:case t.High:case t.Critical:case"error":return t.Error;default:return t.Else}}}(g||(g={}));var v={};function x(t,e,r){var n;switch(t.type){case"HTTP":n=t.type+t.request.method+t.response.status+w(t.request.url)+e;break;case"JAVASCRIPT":case"VUE":case"REACT":n=t.type+t.name+t.message+e;break;case"LOG":n=t.customTag+t.type+t.name+e;break;case"PROMISE":n=function(t,e){var r=w(t.url);if("unhandledrejection"===t.name)return t.type+k(t.message)+e;return t.type+t.name+k(t.message)+r}(t,e);break;default:n=t.type+t.message+e}return n=function(t){var e=0;if(0==t.length)return e;for(var r=0;r<t.length;r++){var n=t.charCodeAt(r);e=(e<<5)-e+n,e&=e}return e}(n),v[n]>=r?null:("number"==typeof v[n]?v[n]++:v[n]=1,n)}function k(t){try{if(/\{.*\}/.test(t)){var e=JSON.parse(t);return e=function t(e){return Object.keys(e).sort().reduce((function(r,n){return s.isObject(e[n])?r[n]=t(e[n]):r[n]=e[n],r}),{})}(e),JSON.stringify(e)}return t}catch(e){return t}}function w(t){return t.replace(/[?#].*$/,"").replace(/\/(\d+)\//g,"/{param}/$1").replace(/\/\d+([/]*$)/g,"/{param}$1")}var T=function(){function t(t){void 0===t&&(t={}),this.maxBreadcrumbs=10,this.beforePushBreadcrumb=null,this.stack=[],this.bindOptions(t)}return t.prototype.push=function(t){var e=this;if("function"==typeof this.beforePushBreadcrumb){var r=null,n=this.beforePushBreadcrumb;return function(){r=n.call(e,e,t)}(),r?this.immediatePush(r):this.stack}return this.immediatePush(t)},t.prototype.immediatePush=function(t){return t.time||(t.time=Date.now()),this.stack.length>=this.maxBreadcrumbs&&this.shift(),this.stack.push(t),this.stack.sort((function(t,e){return t.time-e.time})),d.log(this.stack),this.stack},t.prototype.shift=function(){return void 0!==this.stack.shift()},t.prototype.clear=function(){this.stack=[]},t.prototype.getStack=function(){return this.stack},t.prototype.bindOptions=function(t){void 0===t&&(t={});var e=t.maxBreadcrumbs,r=t.beforePushBreadcrumb;b(e,"maxBreadcrumbs","Number")&&(this.maxBreadcrumbs=e),b(r,"beforePushBreadcrumb","Function")&&(this.beforePushBreadcrumb=r)},t}(),I=function(){function t(){this.dep=new Map}return t.prototype.watch=function(t,e){var r=this.dep.get(t);r?this.dep.set(t,r.concat(e)):this.dep.set(t,[e])},t.prototype.notify=function(t,e){var r=this.dep.get(t);t&&r&&r.forEach((function(r){!function(t,e){try{t()}catch(t){console.error("err",t),e&&e(t)}}((function(){r(e)}),(function(e){d.error("Subscrib.notify:监听事件的回调函数发生错误\neventName:"+t+"\nName: "+function(t){return t&&"function"==typeof t&&t.name||"<anonymous>"}(r)+"\nError: "+e)}))}))},t}(),S=function(){function t(t){this.SDK_VERSION="3.0.1",this.options=t,d.bindOptions(t.debug)}return t.prototype.use=function(t){var e=this;if(!this.options.disabled){var r=new I;t.forEach((function(t){if(e.isPluginEnable(t.name)){t.monitor.call(e,r.notify.bind(r));r.watch(t.name,(function(){for(var r,n,i=[],o=0;o<arguments.length;o++)i[o]=arguments[o];var a=null===(r=t.transform)||void 0===r?void 0:r.apply(e,i);null===(n=t.consumer)||void 0===n||n.call(e,a)}))}}))}},t.prototype.getOptions=function(){return this.options},t}(),O=function(){function t(){this.enableTraceId=!1,this.includeHttpUrlTraceIdRegExp=/.*/,this.traceIdFieldName="Trace-Id",this.throttleDelayTime=0,this.beforeAppAjaxSend=null,this.vue=null}return t.prototype.bindOptions=function(t){var e=t.enableTraceId,r=t.vue,n=t.filterXhrUrlRegExp,i=[[e,"enableTraceId","Boolean"],[t.traceIdFieldName,"traceIdFieldName","String"],[t.throttleDelayTime,"throttleDelayTime","Number"],[n,"filterXhrUrlRegExp","RegExp"],[t.includeHttpUrlTraceIdRegExp,"includeHttpUrlTraceIdRegExp","RegExp"],[t.beforeAppAjaxSend,"beforeAppAjaxSend","Function"]];y.call(this,i),this.vue=r},t.prototype.isFilterHttpUrl=function(t){return this.filterXhrUrlRegExp&&this.filterXhrUrlRegExp.test(t)},t.prototype.setTraceId=function(t,e){var r,n=this.includeHttpUrlTraceIdRegExp;if(this.enableTraceId&&n&&n.test(t)){var i=(r=(new Date).getTime(),"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(function(t){var e=(r+16*Math.random())%16|0;return r=Math.floor(r/16),("x"==t?e:3&e|8).toString(16)})));e(this.traceIdFieldName,i)}},t}(),E=function(){function t(){this.apikey="",this.dsn="",this.beforeDataReport=null,this.backTrackerId=null,this.configReportUrl=null,this.maxDuplicateCount=3,this.queue=new m}return t.prototype.getAuthInfo=function(){var t=this.getTrackerId();return{trackerId:String(t),sdkVersion:"3.0.1",sdkName:"mitojs",apikey:this.apikey}},t.prototype.getTrackerId=function(){if("function"==typeof this.backTrackerId){var t=this.backTrackerId();if("string"==typeof t||"number"==typeof t)return t;d.error("trackerId:"+t+" 期望 string 或 number 类型,但是传入 "+typeof t)}return""},t.prototype.isSelfDsn=function(t){return this.dsn&&(e=t,r=this.dsn,!!~e.indexOf(r));var e,r},t.prototype.bindOptions=function(t){void 0===t&&(t={});var e=t.dsn,r=t.beforeDataReport,n=[[t.apikey,"apikey","String"],[e,"dsn","String"],[t.maxDuplicateCount,"maxDuplicateCount","Number"],[r,"beforeDataReport","Function"],[t.backTrackerId,"backTrackerId","Function"],[t.configReportUrl,"configReportUrl","Function"]];y.call(this,n)},t.prototype.send=function(t,i){return void 0===i&&(i=[]),r(this,void 0,void 0,(function(){var r,o,a;return n(this,(function(n){switch(n.label){case 0:if(!t.isTrack){if(!(r=x(t,this.apikey,this.maxDuplicateCount)))return[2];t.errorId=r}return o=e(e({},this.getTransportData(t)),{breadcrumb:i}),"function"!=typeof this.beforeDataReport?[3,2]:[4,this.beforeDataReport(o)];case 1:if(!(o=n.sent()))return[2];n.label=2;case 2:return a=this.dsn,c=a,s.isString(c)&&""===c.trim()||null==c?(d.error("dsn is empty,pass in when initializing please"),[2]):"function"!=typeof this.configReportUrl||(a=this.configReportUrl(o,a))?[2,this.sendToServer(o,a)]:[2]}var c}))}))},t}();return t.BaseClient=S,t.BaseOptions=O,t.BaseTransport=E,t.Breadcrumb=T,t.Subscrib=I,Object.defineProperty(t,"__esModule",{value:!0}),t}({});