/** * ================================================================ * aegis-mp-sdk@1.38.4 (c) 2023 TencentCloud Real User Monitoring. * Author pumpkincai. * Last Release Time Mon Oct 30 2023 20:32:22 GMT+0800 (中国标准时间). * Released under the MIT License. * Thanks for supporting RUM & Aegis! * ================================================================ **/ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).Aegis=t()}(this,function(){"use strict";var j=function(e,t){return(j=Object.setPrototypeOf||({__proto__:[]}instanceof Array?function(e,t){e.__proto__=t}:function(e,t){for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}))(e,t)};function e(e,t){function n(){this.constructor=e}j(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}var p=function(){return(p=Object.assign||function(e){for(var t,n=1,r=arguments.length;ns[0]&&t[1]>e/4).toString(16)})}function G(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(e){var t=16*Math.random()|0;return("x"===e?t:3&t|8).toString(16)})}function J(e){for(var t,n,r,o="",i=0,a=(e=String(e)).length%3;i>18&63)+s.charAt(t>>12&63)+s.charAt(t>>6&63)+s.charAt(63&t)}return a?o.slice(0,a-3)+"===".substring(a):o}function z(e){if(e=String(e).replace(/[\t\n\f\r ]+/g,""),!$.test(e))throw new TypeError("Failed to execute 'atob': The string to be decoded is not correctly encoded.");var t;e+="==".slice(2-(3&e.length));for(var n,r,o="",i=0;i>16&255):64===r?String.fromCharCode(t>>16&255,t>>8&255):String.fromCharCode(t>>16&255,t>>8&255,255&t);return o}function K(e){var t,n="";return n="object"==typeof e&&(t=(e=function(e,t){for(var n=0;n=r||n.sendNow&&0n.maxCount||0===e||(n.count=n.count+1,(e=n.taskQueue.shift())&&n.sendRequest(e.options,null==e?void 0:e.success,null==e?void 0:e.fail))},this.complete=function(){1n)return O[r]||Se(r),!1;o[e]=1+~~o[e],R[r]=o}return!0}))}),(p=this.lifeCycle.emit,d=this.config,function(e,t){var n,r=d.logCreated;return"function"==typeof r?(n=e.filter(function(e){return!1!==r(e)}),p("beforeWrite",n),t(n)):(p("beforeWrite",e),t(e))}),(f=this,setTimeout(function(){var e=f.config,t=e.pvUrl,n=void 0===t?"":t,t=e.spa,e=-1<["web-sdk","mp-sdk"].indexOf("mp-sdk");n&&(e&&!t||!e)&&f.sendPipeline([function(e,t){t({url:n,type:b.PV})}],b.PV)(null)},100),function(e,t){t(e)}),(l=c=s=!1,u=[],(a=this).lifeCycle.on("onConfigChange",function(){i&&clearTimeout(i),i=setTimeout(function(){var e,n;!l&&a.config&&(l=!0,e=a.config.whiteListUrl,(n=void 0===e?"":e)&&a.sendPipeline([function(e,t){t({url:n,type:b.WHITE_LIST,success:function(e){c=!0;try{var t=e.data||JSON.parse(e),n=t.retcode,r=t.result,o=void 0===r?{}:r,i=(0===n&&(s=o.is_in_white_list,a.isWhiteList=s,0<=o.rate)&&o.rate<=1&&(a.config.random=o.rate,a.isGetSample=!1),a.isWhiteList&&u.length?Pe(a)(u.splice(0),function(){}):!a.isWhiteList&&u.length&&(u.length=0),a.config.onWhitelist);"function"==typeof i&&i(s)}catch(e){}},fail:function(){c=!0}})}],b.WHITE_LIST)(null),l=!1)},a.config.uin?50:500)}),a.lifeCycle.on("destroy",function(){u.length=0}),function(e,t){var n;s||null!=(n=null==(n=a.config)?void 0:n.api)&&n.reportRequest?t(e.concat(u.splice(0)).map(function(e){return xe(e),e})):(n=e.filter(function(e){return e.level!==v.INFO&&e.level!==v.API_RESPONSE?(xe(e),!0):(c||(u.push(e),200<=u.length&&(u.length=200)),!1)})).length&&t(n)}),function(e,t){try{var n=JSON.parse(JSON.stringify(e)),r=(g.lifeCycle.emit("beforeReport",n),g.config.beforeReport);(e="function"==typeof r?e.filter(function(e){return!1!==r(e)}):e).length&&t(e)}catch(e){}},Pe(this)]),this.eventPipeline=y([m(this,10),(o=this,function(e){o.sendPipeline([function(e,t){var n=e.map(function(e){return{name:e.name,ext1:e.ext1||o.config.ext1||"",ext2:e.ext2||o.config.ext2||"",ext3:e.ext3||o.config.ext3||""}});t({url:o.config.eventUrl+"?payload="+encodeURIComponent(JSON.stringify(n)),type:b.EVENT,log:e})}],b.EVENT)(e)})]),this.timeMap={},this.failRequestCount=0,this.customTimePipeline=y([m(this,10),(r=this,function(e){return r.sendPipeline([function(e,t){t({url:r.config.customTimeUrl+"?payload="+encodeURIComponent(JSON.stringify({custom:e})),type:b.CUSTOM,log:e})}],b.CUSTOM)(e)})]),this.config=(t=this.config,void 0===(e=e.hostUrl)&&(e="https://aegis.qq.com"),t.url=t.url||e+"/collect",t.offlineUrl=t.offlineUrl||e+"/offline",t.whiteListUrl=t.whiteListUrl||e+"/collect/whitelist",t.pvUrl=t.pvUrl||e+"/collect/pv",t.eventUrl=t.eventUrl||e+"/collect/events",t.speedUrl=t.speedUrl||e+"/speed",t.customTimeUrl=t.customTimeUrl||e+"/speed/custom",t.performanceUrl=t.performanceUrl||e+"/speed/performance",t.performanceUrlForHippy=t.performanceUrlForHippy||e+"/speed/hippyPerformance",t.webVitalsUrl=t.webVitalsUrl||e+"/speed/webvitals",t.setDataReportUrl=t.SetDataReportUrl||e+"/speed/miniProgramData",t),A.instances.push(this)}function He(){return null!==me&&me.apply(this,arguments)||this}e(He,me=w),He.prototype.defineApiProperty=function(){var e=this;Object.defineProperty(je,"request",{get:function(){return e.hackHandler.bind(e)}})};function q(r,e,o){return null!=e&&e.length&&"object"==typeof r?e.reduce(function(e,t){var n=r[t];return n?e+(""===e?"\n":"\n\n")+o+" header "+t+": "+n:e},""):""}var U,Me,De=new f({name:"reportApiSpeed",override:!(He.prototype.hackHandler=function(e){var t=this,n=this.prefixHandler(e);return this.onStartHandler(n),_e(p(p({},n),{success:function(e){t.successHandler(e,n)},fail:function(e){t.failHandler(e,n)},complete:function(e){t.completeHandler(e,n)}}))}),onNewAegis:function(e){var t,n;this.override||(null!=(n=(e.config||{}).api)&&n.injectTraceHeader&&(this.traceRequestHeader=new Z(n.injectTraceHeader,null!=(t=null==n?void 0:n.injectTraceIgnoreUrls)?t:[],null==n?void 0:n.injectTraceUrls)),this.override=!0,this.hackRequest(e.config),this.overrideCallFunction(e.config),this.overrideCallContainer(e.config))},hackRequest:function(u){var f=this;pe({apiName:"request",onStart:function(e){var t=((null==u?void 0:u.api)||{}).injectTraceHeader,n=(e||{}).url;t&&(t={host:((t=/^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/.exec(t=n)||[])[1],t[2],[t[3],t[4],t[5]][0]),pathname:(t=getCurrentPages()).length?t[t.length-1].route:""},t=(n=f.traceRequestHeader.generate(n,{},{host:t.host,pathname:t.pathname})||{}).name,n=n.value,t)&&n&&e&&(e.header=p(p({},e.header),((e={})[t]=n,e)))},success:function(e,t){var n,r,o,i,a,s,c,l;h(t.url,u.hostUrl)||(n={method:t.method||"get",url:X(t.url),duration:Date.now()-t.aegisRequestStartTime,status:e.statusCode||0,nextHopProtocol:"",isHttps:ee(t.url),type:"fetch"},i=null==(i=u.api)?void 0:i.apiDetail,r=(o=te(e.data,u.api,{url:t.url,ctx:e,payload:t.data})||{}).code,o=o.isErr,l=i?d(t.data,null==(l=u.api)?void 0:l.reqParamHandler,{url:t.url}):"",i=i?d(e.data,null==(i=u.api)?void 0:i.resBodyHandler,{url:t.url}):"",a=(null==(a=u.api)?void 0:a.reqHeaders)||[],a=q(null==t?void 0:t.header,a,"req"),s=(null==(s=u.api)?void 0:s.resHeaders)||[],s=q(null==e?void 0:e.header,s,"res"),c=K(null==t?void 0:t.header),l="req url: "+n.url+" \n \nres status: "+(e.statusCode||0)+" \n \nres duration: "+n.duration+"ms \n \nreq method: "+n.method+" \n \nreq param: "+l+" \n \nres retcode: "+r+" \n \nres data: "+i+"\n "+a+"\n "+s,n.ret=r,n.isErr=+o,n.payload=t.data,f.publishNormalLog({msg:l,level:o?v.RET_ERROR:v.API_RESPONSE,ctx:e,trace:c}),f.publishSpeedLog(n))},fail:function(e,t){h(t.url,u.hostUrl)||(t={method:t.method||"get",url:X(t.url),duration:Date.now()-t.aegisRequestStartTime,status:0,nextHopProtocol:"",isHttps:ee(t.url),type:"fetch"},f.publishSpeedLog(t))}})},overrideCallFunction:function(n){var r=this;fe({apiName:"callFunction",success:function(e,t){r.cloudSuccessCallback(n,t,e,"callFunction")},fail:function(e,t){t={method:"call",url:"wx.cloud.callFunction."+t.name,duration:Date.now()-t.aegisRequestStartTime,status:0,nextHopProtocol:"",type:"fetch",errMsg:e.errMsg,isHttps:!0};r.publishSpeedLog(t)}})},overrideCallContainer:function(n){var r=this;fe({apiName:"callContainer",success:function(e,t){r.cloudSuccessCallback(n,t,e,"callContainer")},fail:function(e,t){t={method:"call",url:"wx.cloud.callContainer."+t.path,duration:Date.now()-t.aegisRequestStartTime,status:0,nextHopProtocol:"",type:"fetch",errMsg:e.errMsg,isHttps:!0};r.publishSpeedLog(t)}})},cloudSuccessCallback:function(e,n,t,r){var o="callFunction"===r?n.name:n.path,i="callFunction"===r?t.result:t.data,o={method:"call",url:"wx.cloud."+r+"."+o,duration:Date.now()-n.aegisRequestStartTime,status:200,nextHopProtocol:"",type:"fetch",isHttps:!0},a=["apiName","aegisRequestStartTime","config","success","fail","complete"],s=Object.keys(n).reduce(function(e,t){return a.includes(t)||(e[t]=n[t]),e},{}),r="callFunction"===r?(null==(r=t.data)?void 0:r.code)||0:t.statusCode,c=te(i,e.api,{url:o.url,ctx:t,payload:s})||{},l=c.code,c=c.isErr,c=void 0!==c&&c,l=(o.ret=l,o.isErr=+c,o.payload=s,null==(l=e.api)?void 0:l.apiDetail),s=l?d(s,null==(s=e.api)?void 0:s.reqParamHandler,{url:o.url}):"",i=l?d(i,null==(l=e.api)?void 0:l.resBodyHandler,{url:o.url}):"",l=(null==(l=e.api)?void 0:l.reqHeaders)||[],l=q(null==n?void 0:n.header,l,"req"),e=(null==(e=e.api)?void 0:e.resHeaders)||[],e=q(null==t?void 0:t.header,e,"res"),u=K(null==n?void 0:n.header),r="req url: "+o.url+"\n \nres status: "+r+"\n \nres duration: "+o.duration+"ms \n \nreq type: "+o.type+"\n \nreq params: "+s+"\n \nres retcode: "+o.ret+"\n \nres data: "+i+"\n "+l+"\n "+e;this.publishNormalLog({msg:r,level:c?v.RET_ERROR:v.API_RESPONSE,ctx:t,trace:u}),this.publishSpeedLog(o)},publishSpeedLog:function(t){this.$walk(function(e){e.speedLogPipeline(t)})},publishNormalLog:function(t){this.$walk(function(e){e.normalLogPipeline(t)})}}),Fe=wx||qq,Be=Fe.connectSocket,We=(Ke.prototype.addConfig=function(e){var t=e.send,e=e.onError;t&&this.sends.push(t),e&&this.onErrors.push(e)},Ke.prototype.toHack=function(t){var o=this,n=(this.onErrors.forEach(function(e){t.onError(e)}),t.send);return Object.defineProperty(t,"send",{get:function(){return function(r){return new Promise(function(e){n.call(t,p(p({},r),{fail:function(n){o.sends.forEach(function(e){var t;null!=(t=e.fail)&&t.call(e,n,r)}),e(n)}}))})}}}),t},Ke),Ve=(e(I,Me=w),I.prototype.addTaskConf=function(e){this.taskHack.addConfig(e)},I.prototype.defineApiProperty=function(){var e=this;Object.defineProperty(Fe,"connectSocket",{get:function(){return e.hackHandler.bind(e)}})},I.prototype.hackHandler=function(e){var t=this,n=this.prefixHandler(e),e=Be(p(p({},n),{success:function(e){t.successHandler(e,n)},fail:function(e){t.failHandler(e,n)},complete:function(e){t.completeHandler(e,n)}}));return this.taskHack.toHack(e),e},I),w=new f({name:"onError",onNewAegis:function(e){var t=g.getSystemInfoSync().SDKVersion;this.listenError(),this.hackNetWork(e),this.hackCloud(e),e.config.websocketHack&&ce(t,"1.7.0")&&this.hackWsConnect()},listenError:function(){var t=this;"function"==typeof g.onError&&g.onError(function(e){e&&t.publishErrorLog({msg:e,level:v.ERROR})}),i("onUnhandledRejection")&&g.onUnhandledRejection(function(e){e=e.reason;!e||-1