keeson-web-report-sdk 1.1.2 → 1.1.6

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.
package/bundle.esm.js ADDED
@@ -0,0 +1 @@
1
+ const e="error",t="performance",r="user",s="main",o="resourceError",a="PromiseRejection",n="VueError",i="error",p="loaded",u="paint",c="first-contentful-paint",l="largest-contentful-paint",d="layout-shift",m="first-input",h="longTask",g="resource",f="pageResource",y="LocationChange",T="RouteChange";function v(e,t){if(window.PerformanceObserver){const r=new PerformanceObserver(function(r){const s=function(e,t){const r=t.getEntries(),s=[];return r.forEach(t=>{if("paint"===t.entryType&&("first-paint"===t.name&&s.push({type:u,message:`白屏时间 (FP): ${t.startTime+t.duration}ms`,extra:{value:t.startTime+t.duration},value:t.startTime+t.duration}),"first-contentful-paint"===t.name&&s.push({type:c,message:`首屏时间 (FCP): ${t.startTime+t.duration}ms`,extra:{value:t.startTime+t.duration},value:t.startTime+t.duration})),"largest-contentful-paint"===t.entryType){const{renderTime:e,startTime:r,duration:o}=t.renderTime,a=e||r+o;a>2500&&s.push({type:l,message:`元素 ${t.element.outerHTML} LCP 时间${a}毫秒,超过了 2.5 秒`,extra:{element:t.element.outerHTML,value:a},value:a})}if("first-input"===t.entryType&&t.processingStart-t.startTime>100&&s.push({type:m,message:"主线程阻塞时长超过 100 毫秒,用户体验不佳",extra:{value:t.processingStart-t.startTime},value:t.processingStart-t.startTime}),"layout-shift"!==t.entryType||t.hadRecentInput||t.value>.1&&s.push({type:d,message:"CLS 值超过 0.1,用户体验不佳",extra:{value:t.value},value:t.value}),"navigation"===t.entryType){s.push({type:p,message:`加载完成时间 (Load): ${t.loadEventEnd}ms`,value:t.loadEventEnd,extra:{value:t.loadEventEnd}});const r=t.domainLookupEnd-t.domainLookupStart,o=t.responseEnd-t.startTime;o>e.longTimeRequestThreshold&&s.push({type:f,message:`页面${t.name}请求耗时 ${o} 毫秒,超过了 ${e.longTimeRequestThreshold} 毫秒`,extra:{dnsTime:r,value:o,resourceName:t.name},value:o})}if("resource"===t.entryType){const r=t.domainLookupEnd-t.domainLookupStart,o=t.duration;o>e.longTimeRequestThreshold&&s.push({type:g,message:`资源${t.name}请求耗时 ${o} 毫秒,超过了 ${e.longTimeRequestThreshold} 毫秒`,extra:{dnsTime:r,value:o,resourceName:t.name},value:o})}if("longtask"===t.entryType){const e=t.duration-50>0?t.duration-50:0;e>50&&s.push({type:h,message:`检测到长任务,总耗时: ${t.duration} ms, 阻塞时间约: ${e} ms,大于建议的50ms内,请使用控制台performance(性能)进行性能分析`,extra:{duration:t.duration,blockingTime:e,resourceName:t.name},value:e})}}),s}(e,r);return"function"==typeof t&&t(s),s});r.observe({entryTypes:["paint","first-contentful-paint","largest-contentful-paint","first-input","layout-shift","interaction-to-next-paint","navigation","resource","longtask"],buffered:!0})}}function $(e){const t=[];return t.push(function(e){const t=history.pushState,r=history.replaceState;history.pushState=function(...r){const s=window.location.href,o=t.apply(this,r),a=window.location.href;return s!==a&&e({to:a,from:s}),o},history.replaceState=function(...t){const s=window.location.href,o=r.apply(this,t),a=window.location.href;return s!==a&&e({to:a,from:s}),o};const s=()=>{e({to:window.location.href,from:window.location.href})};return window.addEventListener("popstate",s),()=>{window.removeEventListener("popstate",s),history.pushState=t,history.replaceState=r}}(e)),()=>{t.forEach(e=>e())}}const w={reportUrl:"",appId:"",appVersion:"",appUrl:window.location.origin,appEnv:"prod",userAgent:window.navigator.userAgent,reportPath:"",reportError:!0,reportPromiseReject:!0,reportVueError:!0,vueInstance:null,reportPerformance:!0,listenPerformance:!0,longTimeRequestThreshold:2e3,reportErrorReqest:!0,errorRequestFunc:null,reportUserNavigate:!0,appSecretKey:""};class R{records=[];timer=null;options={};constructor(e){this.options={...w,...e}}init(){const p=localStorage.getItem("report-store-date"),u=`${(c=new Date).getFullYear()}-${c.getMonth()+1}-${c.getDate()}`;var c;p!==u&&(localStorage.setItem("report-store-date",u),localStorage.setItem("report-store-list","[]"));const l=JSON.parse(localStorage.getItem("report-store-list"));var d,m,h,g;if(this.options.reportError&&(d=t=>{l.includes(t.message)||(this.addReportRecord({reportType:e,...t}),l.push(t.message),localStorage.setItem("report-store-list",JSON.stringify(l)))},window.addEventListener("error",function(e){if(e.target){if(e.target&&("IMG"===e.target.tagName||"SCRIPT"===e.target.tagName||"LINK"===e.target.tagName||"VIDEO"===e.target.tagName||"AUDIO"===e.target.tagName||"SOURCE"===e.target.tagName)){const t=e.target,r={tagName:t.tagName,src:t.src||t.href,outerHTML:t.outerHTML,time:(new Date).toISOString()};e.preventDefault(),d&&d({type:o,message:`资源加载失败,${r.tagName}: ${r.src}`,extra:r})}}else{const t={message:e.error.stack,filename:e.filename,line:e.lineno,col:e.colno};e.preventDefault(),d&&d({type:s,message:t.message,extra:t})}},!0)),this.options.reportPromiseReject&&function(e){window.addEventListener("unhandledrejection",function(t){e&&e(t)})}(t=>{l.includes(t.reason?.stack)||(this.addReportRecord({reportType:e,type:a,message:t.reason?.stack}),l.push(t.reason?.stack),localStorage.setItem("report-store-list",JSON.stringify(l)))}),this.options.reportVueError&&this.options.vueInstance&&function(e,t){e.config.errorHandler=function(e,r,s){t&&t({err:e,vm:r,info:s})}}(this.options.vueInstance,({err:t})=>{l.includes(t.message)||(this.addReportRecord({reportType:e,type:n,message:t.message}),l.push(t.message),localStorage.setItem("report-store-list",JSON.stringify(l)))}),this.options.listenPerformance&&v(this.options,e=>{e.forEach(e=>{e.extra.resourceName!==this.options.reportUrl&&this.addReportRecord({reportType:t,...e})})}),this.options.reportErrorReqest&&(m=t=>{const{status:r,url:s,method:o,statusText:a}=t,n=`请求报错接口:${s},方法:${o},状态码:${r},状态信息:${a}`;l.includes(n)||(this.addReportRecord({reportType:e,type:i,message:n}),l.push(n),localStorage.setItem("report-store-list",JSON.stringify(l)))},h=this.options,g=XMLHttpRequest.prototype.open,XMLHttpRequest.prototype.open=function(e,t,r,s,o){let a=Date.now();this.addEventListener("load",function(){const r=Date.now()-a;if(200===this.status){if("string"==typeof this.response)try{const s=JSON.parse(this.response);if("function"==typeof h.errorRequestFunc&&h.errorRequestFunc(s))return void(m&&m({message:`请求成功接口:${t},方法:${e},状态码:${this.status},状态信息:${this.statusText}, 请求耗时:${r}ms;服务器处理错误`,extra:{response:s,headers:this.getAllResponseHeaders()}}))}catch(e){}}else m&&m({message:`请求报错接口:${t},方法:${e},状态码:${this.status},状态信息:${this.statusText}`,extra:{headers:this.getAllResponseHeaders()}})}),this.addEventListener("error",function(){m&&m({message:`请求报错接口:${t},方法:${e},状态码:${this.status},状态信息:${this.statusText}`})}),g.call(this,e,t,r,s,o)},function(e,t,r){var s=window.fetch;window.fetch=function(e,o){let a=Date.now();return s(e,o).then(async s=>{const n=Date.now()-a,i=[...s.headers.entries()].map(e=>e.join(": ")).join("\n");if(200===s.status)try{const o=await s.json();if("function"==typeof r.errorRequestFunc&&r.errorRequestFunc(o))return void(t&&t({message:`请求成功接口:${e},方法:${method},状态码:${s.status},状态信息:${s.statusText}, 请求耗时:${n}ms\n服务器处理错误`,extra:{response:o,headers:i}}))}catch(e){}else t&&t({message:`请求报错接口:${e},方法:${o.method},状态码:${s.status},状态信息:${s.statusText}, 耗时:${n}ms`,extra:{headers:i}});return s}).catch(r=>{t&&t({message:`请求报错接口:${e},方法:${o.method},错误信息\n${r.stack}`})})}}(0,t=>{const{url:r,method:s,statusText:o}=t,a=`请求报错接口:${r},方法:${s},状态信息:${o}`;l.includes(a)||(this.addReportRecord({reportType:e,type:i,message:a}),l.push(a),localStorage.setItem("report-store-list",JSON.stringify(l)))},this.options)),this.options.reportUserNavigate){const e=window.location.href;this.addReportRecord({reportType:r,type:y,message:`请求地址:${e}`,extra:JSON.stringify({url:e})}),$(e=>{this.addReportRecord({reportType:r,type:T,message:`路由变化:${e.to}`,extra:JSON.stringify({route:e.to})})})}}addReportRecord(e){this.timer&&clearTimeout(this.timer),this.timer=setTimeout(()=>{try{this.sendReport(JSON.parse(JSON.stringify(this.records))),this.records=[]}catch(e){}},1e3),this.records.push({appId:this.options.appId,appUrl:this.options.appUrl,reportPath:window.location.pathname,userAgent:window.navigator.userAgent,...e,extra:e.extra&&"object"==typeof e.extra?JSON.stringify(e.extra):void 0})}sendReport(e){const t={appSecretKey:this.options.appSecretKey,appId:this.options.appId,data:e},r=new Blob([JSON.stringify(t)],{type:"application/json; charset=UTF-8"});navigator.sendBeacon(this.options.reportUrl,r)}}let S;function x(e={}){S||(S=new R(e)),S.init()}var E={init:x};export{E as default,x as init};
@@ -0,0 +1 @@
1
+ var reportSdk=function(e){"use strict";const t="error",r="performance",s="user",o="main",n="resourceError",a="PromiseRejection",i="VueError",p="error",u="loaded",c="paint",l="first-contentful-paint",d="largest-contentful-paint",m="layout-shift",h="first-input",g="longTask",f="resource",y="pageResource",T="LocationChange",v="RouteChange";function $(e,t){if(window.PerformanceObserver){const r=new PerformanceObserver(function(r){const s=function(e,t){const r=t.getEntries(),s=[];return r.forEach(t=>{if("paint"===t.entryType&&("first-paint"===t.name&&s.push({type:c,message:`白屏时间 (FP): ${t.startTime+t.duration}ms`,extra:{value:t.startTime+t.duration},value:t.startTime+t.duration}),"first-contentful-paint"===t.name&&s.push({type:l,message:`首屏时间 (FCP): ${t.startTime+t.duration}ms`,extra:{value:t.startTime+t.duration},value:t.startTime+t.duration})),"largest-contentful-paint"===t.entryType){const{renderTime:e,startTime:r,duration:o}=t.renderTime,n=e||r+o;n>2500&&s.push({type:d,message:`元素 ${t.element.outerHTML} LCP 时间${n}毫秒,超过了 2.5 秒`,extra:{element:t.element.outerHTML,value:n},value:n})}if("first-input"===t.entryType&&t.processingStart-t.startTime>100&&s.push({type:h,message:"主线程阻塞时长超过 100 毫秒,用户体验不佳",extra:{value:t.processingStart-t.startTime},value:t.processingStart-t.startTime}),"layout-shift"!==t.entryType||t.hadRecentInput||t.value>.1&&s.push({type:m,message:"CLS 值超过 0.1,用户体验不佳",extra:{value:t.value},value:t.value}),"navigation"===t.entryType){s.push({type:u,message:`加载完成时间 (Load): ${t.loadEventEnd}ms`,value:t.loadEventEnd,extra:{value:t.loadEventEnd}});const r=t.domainLookupEnd-t.domainLookupStart,o=t.responseEnd-t.startTime;o>e.longTimeRequestThreshold&&s.push({type:y,message:`页面${t.name}请求耗时 ${o} 毫秒,超过了 ${e.longTimeRequestThreshold} 毫秒`,extra:{dnsTime:r,value:o,resourceName:t.name},value:o})}if("resource"===t.entryType){const r=t.domainLookupEnd-t.domainLookupStart,o=t.duration;o>e.longTimeRequestThreshold&&s.push({type:f,message:`资源${t.name}请求耗时 ${o} 毫秒,超过了 ${e.longTimeRequestThreshold} 毫秒`,extra:{dnsTime:r,value:o,resourceName:t.name},value:o})}if("longtask"===t.entryType){const e=t.duration-50>0?t.duration-50:0;e>50&&s.push({type:g,message:`检测到长任务,总耗时: ${t.duration} ms, 阻塞时间约: ${e} ms,大于建议的50ms内,请使用控制台performance(性能)进行性能分析`,extra:{duration:t.duration,blockingTime:e,resourceName:t.name},value:e})}}),s}(e,r);return"function"==typeof t&&t(s),s});r.observe({entryTypes:["paint","first-contentful-paint","largest-contentful-paint","first-input","layout-shift","interaction-to-next-paint","navigation","resource","longtask"],buffered:!0})}}function w(e){const t=[];return t.push(function(e){const t=history.pushState,r=history.replaceState;history.pushState=function(...r){const s=window.location.href,o=t.apply(this,r),n=window.location.href;return s!==n&&e({to:n,from:s}),o},history.replaceState=function(...t){const s=window.location.href,o=r.apply(this,t),n=window.location.href;return s!==n&&e({to:n,from:s}),o};const s=()=>{e({to:window.location.href,from:window.location.href})};return window.addEventListener("popstate",s),()=>{window.removeEventListener("popstate",s),history.pushState=t,history.replaceState=r}}(e)),()=>{t.forEach(e=>e())}}const R={reportUrl:"",appId:"",appVersion:"",appUrl:window.location.origin,appEnv:"prod",userAgent:window.navigator.userAgent,reportPath:"",reportError:!0,reportPromiseReject:!0,reportVueError:!0,vueInstance:null,reportPerformance:!0,listenPerformance:!0,longTimeRequestThreshold:2e3,reportErrorReqest:!0,errorRequestFunc:null,reportUserNavigate:!0,appSecretKey:""};class S{records=[];timer=null;options={};constructor(e){this.options={...R,...e}}init(){const e=localStorage.getItem("report-store-date"),u=`${(c=new Date).getFullYear()}-${c.getMonth()+1}-${c.getDate()}`;var c;e!==u&&(localStorage.setItem("report-store-date",u),localStorage.setItem("report-store-list","[]"));const l=JSON.parse(localStorage.getItem("report-store-list"));var d,m,h,g;if(this.options.reportError&&(d=e=>{l.includes(e.message)||(this.addReportRecord({reportType:t,...e}),l.push(e.message),localStorage.setItem("report-store-list",JSON.stringify(l)))},window.addEventListener("error",function(e){if(e.target){if(e.target&&("IMG"===e.target.tagName||"SCRIPT"===e.target.tagName||"LINK"===e.target.tagName||"VIDEO"===e.target.tagName||"AUDIO"===e.target.tagName||"SOURCE"===e.target.tagName)){const t=e.target,r={tagName:t.tagName,src:t.src||t.href,outerHTML:t.outerHTML,time:(new Date).toISOString()};e.preventDefault(),d&&d({type:n,message:`资源加载失败,${r.tagName}: ${r.src}`,extra:r})}}else{const t={message:e.error.stack,filename:e.filename,line:e.lineno,col:e.colno};e.preventDefault(),d&&d({type:o,message:t.message,extra:t})}},!0)),this.options.reportPromiseReject&&function(e){window.addEventListener("unhandledrejection",function(t){e&&e(t)})}(e=>{l.includes(e.reason?.stack)||(this.addReportRecord({reportType:t,type:a,message:e.reason?.stack}),l.push(e.reason?.stack),localStorage.setItem("report-store-list",JSON.stringify(l)))}),this.options.reportVueError&&this.options.vueInstance&&function(e,t){e.config.errorHandler=function(e,r,s){t&&t({err:e,vm:r,info:s})}}(this.options.vueInstance,({err:e})=>{l.includes(e.message)||(this.addReportRecord({reportType:t,type:i,message:e.message}),l.push(e.message),localStorage.setItem("report-store-list",JSON.stringify(l)))}),this.options.listenPerformance&&$(this.options,e=>{e.forEach(e=>{e.extra.resourceName!==this.options.reportUrl&&this.addReportRecord({reportType:r,...e})})}),this.options.reportErrorReqest&&(m=e=>{const{status:r,url:s,method:o,statusText:n}=e,a=`请求报错接口:${s},方法:${o},状态码:${r},状态信息:${n}`;l.includes(a)||(this.addReportRecord({reportType:t,type:p,message:a}),l.push(a),localStorage.setItem("report-store-list",JSON.stringify(l)))},h=this.options,g=XMLHttpRequest.prototype.open,XMLHttpRequest.prototype.open=function(e,t,r,s,o){let n=Date.now();this.addEventListener("load",function(){const r=Date.now()-n;if(200===this.status){if("string"==typeof this.response)try{const s=JSON.parse(this.response);if("function"==typeof h.errorRequestFunc&&h.errorRequestFunc(s))return void(m&&m({message:`请求成功接口:${t},方法:${e},状态码:${this.status},状态信息:${this.statusText}, 请求耗时:${r}ms;服务器处理错误`,extra:{response:s,headers:this.getAllResponseHeaders()}}))}catch(e){}}else m&&m({message:`请求报错接口:${t},方法:${e},状态码:${this.status},状态信息:${this.statusText}`,extra:{headers:this.getAllResponseHeaders()}})}),this.addEventListener("error",function(){m&&m({message:`请求报错接口:${t},方法:${e},状态码:${this.status},状态信息:${this.statusText}`})}),g.call(this,e,t,r,s,o)},function(e,t,r){var s=window.fetch;window.fetch=function(e,o){let n=Date.now();return s(e,o).then(async s=>{const a=Date.now()-n,i=[...s.headers.entries()].map(e=>e.join(": ")).join("\n");if(200===s.status)try{const o=await s.json();if("function"==typeof r.errorRequestFunc&&r.errorRequestFunc(o))return void(t&&t({message:`请求成功接口:${e},方法:${method},状态码:${s.status},状态信息:${s.statusText}, 请求耗时:${a}ms\n服务器处理错误`,extra:{response:o,headers:i}}))}catch(e){}else t&&t({message:`请求报错接口:${e},方法:${o.method},状态码:${s.status},状态信息:${s.statusText}, 耗时:${a}ms`,extra:{headers:i}});return s}).catch(r=>{t&&t({message:`请求报错接口:${e},方法:${o.method},错误信息\n${r.stack}`})})}}(0,e=>{const{url:r,method:s,statusText:o}=e,n=`请求报错接口:${r},方法:${s},状态信息:${o}`;l.includes(n)||(this.addReportRecord({reportType:t,type:p,message:n}),l.push(n),localStorage.setItem("report-store-list",JSON.stringify(l)))},this.options)),this.options.reportUserNavigate){const e=window.location.href;this.addReportRecord({reportType:s,type:T,message:`请求地址:${e}`,extra:JSON.stringify({url:e})}),w(e=>{this.addReportRecord({reportType:s,type:v,message:`路由变化:${e.to}`,extra:JSON.stringify({route:e.to})})})}}addReportRecord(e){this.timer&&clearTimeout(this.timer),this.timer=setTimeout(()=>{try{this.sendReport(JSON.parse(JSON.stringify(this.records))),this.records=[]}catch(e){}},1e3),this.records.push({appId:this.options.appId,appUrl:this.options.appUrl,reportPath:window.location.pathname,userAgent:window.navigator.userAgent,...e,extra:e.extra&&"object"==typeof e.extra?JSON.stringify(e.extra):void 0})}sendReport(e){const t={appSecretKey:this.options.appSecretKey,appId:this.options.appId,data:e},r=new Blob([JSON.stringify(t)],{type:"application/json; charset=UTF-8"});navigator.sendBeacon(this.options.reportUrl,r)}}let x;function E(e={}){x||(x=new S(e)),x.init()}var N={init:E};return e.default=N,e.init=E,Object.defineProperty(e,"__esModule",{value:!0}),e}({});
package/bundle.min.js CHANGED
@@ -1 +1 @@
1
- !function(o,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((o="undefined"!=typeof globalThis?globalThis:o||self).reportSdk={})}(this,function(o){"use strict";function e(o,e,t){return e&&function(o,e){for(var t=0;t<e.length;t++){var n=e[t];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(o,i(n.key),n)}}(o.prototype,e),Object.defineProperty(o,"prototype",{writable:!1}),o}function t(o,e,t){return(e=i(e))in o?Object.defineProperty(o,e,{value:t,enumerable:!0,configurable:!0,writable:!0}):o[e]=t,o}function n(o,e){var t=Object.keys(o);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(o);e&&(n=n.filter(function(e){return Object.getOwnPropertyDescriptor(o,e).enumerable})),t.push.apply(t,n)}return t}function r(o){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?n(Object(r),!0).forEach(function(e){t(o,e,r[e])}):Object.getOwnPropertyDescriptors?Object.defineProperties(o,Object.getOwnPropertyDescriptors(r)):n(Object(r)).forEach(function(e){Object.defineProperty(o,e,Object.getOwnPropertyDescriptor(r,e))})}return o}function i(o){var e=function(o,e){if("object"!=typeof o||!o)return o;var t=o[Symbol.toPrimitive];if(void 0!==t){var n=t.call(o,e);if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(o)}(o,"string");return"symbol"==typeof e?e:e+""}function s(o){var e=o.getEntries();console.log(e),e.forEach(function(o){if("paint"===o.entryType&&(console.log("-------------------------FP/FCP-------------------------"),console.log(o),"first-paint"===o.name&&console.log("白屏时间 (FP):",o.startTime,"毫秒"),"first-contentful-paint"===o.name&&console.log("首屏时间 (FCP):",o.startTime,"毫秒"),console.log("-------------------------FP/FCP-------------------------")),"largest-contentful-paint"===o.entryType&&(console.log("-------------------------lcp-------------------------"),console.log("LCP:",o.startTime,"ms"),console.log("-------------------------lcp-------------------------")),"first-input"===o.entryType&&(console.log("-------------------------fid-------------------------"),console.log("FID:",o.processingStart-o.startTime,"ms"),console.log("-------------------------fid-------------------------")),"layout-shift"!==o.entryType||o.hadRecentInput||(console.log("-------------------------cls-------------------------"),console.log("CLS:",o.value),console.log("is smaller than 0.1:",o.value<=.1),console.log("-------------------------cls-------------------------")),"navigation"===o.entryType){console.log("-------------------------navigation-------------------------");var e=o.domainLookupEnd-o.domainLookupStart;console.log("页面dnsTime",e);var t=o.responseEnd-o.startTime;console.log("页面请求耗时",t),console.log("-------------------------navigation-------------------------")}if("resource"===o.entryType){console.log("-------------------------资源请求-------------------------"),console.log("resource",o,"resource");var n=o.domainLookupEnd-o.domainLookupStart,r=o.responseEnd-o.requestStart,i=o.duration;console.log("dnsTime",n,"serverTime",r,"totalTime",i),console.log("-------------------------资源请求-------------------------")}if("longtask"===o.entryType){console.log("-------------------------长任务-------------------------"),console.log("longtask",o,"longtask");var s=o.duration-50;console.log("检测到长任务,总耗时: ".concat(o.duration," ms, 阻塞时间约: ").concat(s>0?s:0," ms"),o),console.log("-------------------------长任务-------------------------")}})}function l(o){var e=[];return e.push(function(o){var e=history.pushState,t=history.replaceState;history.pushState=function(){for(var t=window.location.href,n=arguments.length,r=new Array(n),i=0;i<n;i++)r[i]=arguments[i];var s=e.apply(this,r),l=window.location.href;return t!==l&&o({to:l,from:t}),s},history.replaceState=function(){for(var e=window.location.href,n=arguments.length,r=new Array(n),i=0;i<n;i++)r[i]=arguments[i];var s=t.apply(this,r),l=window.location.href;return e!==l&&o({to:l,from:e}),s};var n=function(){o({to:window.location.href,from:window.location.href})};return window.addEventListener("popstate",n),function(){window.removeEventListener("popstate",n),history.pushState=e,history.replaceState=t}}(o)),function(){e.forEach(function(o){return o()})}}var c,a={appId:"",reportUrl:"",env:"production",appUrl:window.location.origin,reportError:!0,reportPromiseReject:!0,reportVueError:!0,vueInstance:null,reportPerformance:!0,reportLongTimeRequest:!0,longTimeRequestThreshold:2e3,reportErrorReqest:!0,reportUserNavigate:!0},u=function(){return e(function o(e){!function(o,e){if(!(o instanceof e))throw new TypeError("Cannot call a class as a function")}(this,o),t(this,"records",[]),t(this,"timer",null),t(this,"options",{}),this.options=r(r({},a),e)},[{key:"init",value:function(){var o,e,t,n;if(this.options.reportError&&(o=function(o){var e=o.msg,t=o.url,n=o.line,r=o.col,i=o.error;console.log({msg:e,url:t,line:n,col:r,error:i})},window.onerror=function(e,t,n,r,i){o&&o({msg:e,url:t,line:n,col:r,error:i})}),this.options.reportPromiseReject&&function(o){window.addEventListener("unhandledrejection",function(e){o&&o(e)})}(function(o){console.log("Unhandled Promise Rejection:",o,o.reason)}),this.options.reportVueError&&this.options.vueInstance&&function(o,e){o.config.errorHandler=function(o,t,n){console.error("Error in ".concat(n,":"),o),e&&e({err:o,vm:t,info:n})}}(this.options.vueInstance,function(o){var e=o.err,t=o.vm,n=o.info;console.log("Vue error:",e,t,n)}),this.options.reportPerformance&&(window.onload=function(){if(console.log("window.onload"),window.PerformanceObserver){var o=new PerformanceObserver(s);console.log("supportedEntryTypes",PerformanceObserver.supportedEntryTypes),o.observe({entryTypes:["paint","largest-contentful-paint","first-input","layout-shift","navigation","resource","longtask"],buffered:!0})}window.performance&&(console.log("-----------------------------------"),s(window.performance),console.log("-----------------------------------"))}),this.options.reportLongTimeRequest&&(e=function(o){console.log(o)},t=function(o){console.log(o)},n=XMLHttpRequest.prototype.open,XMLHttpRequest.prototype.open=function(o,r,i,s,l){var c=Date.now();this.addEventListener("load",function(){var t=Date.now();e&&e({url:r,method:o,duration:t-c,status:this.status,statusText:this.statusText,response:this.responseText}),console.log("Response:",this.responseText)}),this.addEventListener("error",function(){console.log("Error:",this.statusText),t&&t({url:r,method:o,duration:duration,status:this.status,statusText:this.statusText,response:this.responseText})}),console.log("Request URL:",r),console.log("Request Method:",o),n.call(this,o,r,i,s,l)},function(o,e){var t=window.fetch;window.fetch=function(n,r){console.log("Fetch URL:",n),console.log("Fetch Options:",r),r&&r.headers&&(r.headers["X-Custom-Header"]="Value");var i=Date.now();return t(n,r).then(function(e){var t=Date.now();return o&&o({url:n,method:method,duration:t-i,status:e.status,statusText:e.statusText,response:e.responseText}),console.log("Response:",e),e}).catch(function(o){console.log("Error:",o),e&&e({url:n,method:method,duration:duration})})}}(function(o){console.log(o)},function(o){console.log(o)})),this.options.reportUserNavigate){var r=window.location.href;console.log("current href:",r),l(function(o){console.log("route change:",o)})}}},{key:"addReportRecord",value:function(o){var e=this;this.timer&&clearTimeout(this.timer),this.timer=setTimeout(function(){try{e.sendReport(JSON.parse(JSON.stringify(e.records))),e.records=[]}catch(o){console.log(o)}},1e3),this.records.push(o)}},{key:"sendReport",value:function(o){navigator.sendBeacon(this.options.reportUrl,JSON.stringify(o))}}])}();o.init=function(){c||(c=new u(arguments.length>0&&void 0!==arguments[0]?arguments[0]:{})),c.init()}});
1
+ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).reportSdk={})}(this,function(e){"use strict";const t="error",r="performance",s="user",o="main",n="resourceError",a="PromiseRejection",i="VueError",p="error",u="loaded",c="paint",l="first-contentful-paint",d="largest-contentful-paint",m="layout-shift",h="first-input",f="longTask",g="resource",y="pageResource",T="LocationChange",v="RouteChange";function $(e,t){if(window.PerformanceObserver){const r=new PerformanceObserver(function(r){const s=function(e,t){const r=t.getEntries(),s=[];return r.forEach(t=>{if("paint"===t.entryType&&("first-paint"===t.name&&s.push({type:c,message:`白屏时间 (FP): ${t.startTime+t.duration}ms`,extra:{value:t.startTime+t.duration},value:t.startTime+t.duration}),"first-contentful-paint"===t.name&&s.push({type:l,message:`首屏时间 (FCP): ${t.startTime+t.duration}ms`,extra:{value:t.startTime+t.duration},value:t.startTime+t.duration})),"largest-contentful-paint"===t.entryType){const{renderTime:e,startTime:r,duration:o}=t.renderTime,n=e||r+o;n>2500&&s.push({type:d,message:`元素 ${t.element.outerHTML} LCP 时间${n}毫秒,超过了 2.5 秒`,extra:{element:t.element.outerHTML,value:n},value:n})}if("first-input"===t.entryType&&t.processingStart-t.startTime>100&&s.push({type:h,message:"主线程阻塞时长超过 100 毫秒,用户体验不佳",extra:{value:t.processingStart-t.startTime},value:t.processingStart-t.startTime}),"layout-shift"!==t.entryType||t.hadRecentInput||t.value>.1&&s.push({type:m,message:"CLS 值超过 0.1,用户体验不佳",extra:{value:t.value},value:t.value}),"navigation"===t.entryType){s.push({type:u,message:`加载完成时间 (Load): ${t.loadEventEnd}ms`,value:t.loadEventEnd,extra:{value:t.loadEventEnd}});const r=t.domainLookupEnd-t.domainLookupStart,o=t.responseEnd-t.startTime;o>e.longTimeRequestThreshold&&s.push({type:y,message:`页面${t.name}请求耗时 ${o} 毫秒,超过了 ${e.longTimeRequestThreshold} 毫秒`,extra:{dnsTime:r,value:o,resourceName:t.name},value:o})}if("resource"===t.entryType){const r=t.domainLookupEnd-t.domainLookupStart,o=t.duration;o>e.longTimeRequestThreshold&&s.push({type:g,message:`资源${t.name}请求耗时 ${o} 毫秒,超过了 ${e.longTimeRequestThreshold} 毫秒`,extra:{dnsTime:r,value:o,resourceName:t.name},value:o})}if("longtask"===t.entryType){const e=t.duration-50>0?t.duration-50:0;e>50&&s.push({type:f,message:`检测到长任务,总耗时: ${t.duration} ms, 阻塞时间约: ${e} ms,大于建议的50ms内,请使用控制台performance(性能)进行性能分析`,extra:{duration:t.duration,blockingTime:e,resourceName:t.name},value:e})}}),s}(e,r);return"function"==typeof t&&t(s),s});r.observe({entryTypes:["paint","first-contentful-paint","largest-contentful-paint","first-input","layout-shift","interaction-to-next-paint","navigation","resource","longtask"],buffered:!0})}}function w(e){const t=[];return t.push(function(e){const t=history.pushState,r=history.replaceState;history.pushState=function(...r){const s=window.location.href,o=t.apply(this,r),n=window.location.href;return s!==n&&e({to:n,from:s}),o},history.replaceState=function(...t){const s=window.location.href,o=r.apply(this,t),n=window.location.href;return s!==n&&e({to:n,from:s}),o};const s=()=>{e({to:window.location.href,from:window.location.href})};return window.addEventListener("popstate",s),()=>{window.removeEventListener("popstate",s),history.pushState=t,history.replaceState=r}}(e)),()=>{t.forEach(e=>e())}}const R={reportUrl:"",appId:"",appVersion:"",appUrl:window.location.origin,appEnv:"prod",userAgent:window.navigator.userAgent,reportPath:"",reportError:!0,reportPromiseReject:!0,reportVueError:!0,vueInstance:null,reportPerformance:!0,listenPerformance:!0,longTimeRequestThreshold:2e3,reportErrorReqest:!0,errorRequestFunc:null,reportUserNavigate:!0,appSecretKey:""};class S{records=[];timer=null;options={};constructor(e){this.options={...R,...e}}init(){const e=localStorage.getItem("report-store-date"),u=`${(c=new Date).getFullYear()}-${c.getMonth()+1}-${c.getDate()}`;var c;e!==u&&(localStorage.setItem("report-store-date",u),localStorage.setItem("report-store-list","[]"));const l=JSON.parse(localStorage.getItem("report-store-list"));var d,m,h,f;if(this.options.reportError&&(d=e=>{l.includes(e.message)||(this.addReportRecord({reportType:t,...e}),l.push(e.message),localStorage.setItem("report-store-list",JSON.stringify(l)))},window.addEventListener("error",function(e){if(e.target){if(e.target&&("IMG"===e.target.tagName||"SCRIPT"===e.target.tagName||"LINK"===e.target.tagName||"VIDEO"===e.target.tagName||"AUDIO"===e.target.tagName||"SOURCE"===e.target.tagName)){const t=e.target,r={tagName:t.tagName,src:t.src||t.href,outerHTML:t.outerHTML,time:(new Date).toISOString()};e.preventDefault(),d&&d({type:n,message:`资源加载失败,${r.tagName}: ${r.src}`,extra:r})}}else{const t={message:e.error.stack,filename:e.filename,line:e.lineno,col:e.colno};e.preventDefault(),d&&d({type:o,message:t.message,extra:t})}},!0)),this.options.reportPromiseReject&&function(e){window.addEventListener("unhandledrejection",function(t){e&&e(t)})}(e=>{l.includes(e.reason?.stack)||(this.addReportRecord({reportType:t,type:a,message:e.reason?.stack}),l.push(e.reason?.stack),localStorage.setItem("report-store-list",JSON.stringify(l)))}),this.options.reportVueError&&this.options.vueInstance&&function(e,t){e.config.errorHandler=function(e,r,s){t&&t({err:e,vm:r,info:s})}}(this.options.vueInstance,({err:e})=>{l.includes(e.message)||(this.addReportRecord({reportType:t,type:i,message:e.message}),l.push(e.message),localStorage.setItem("report-store-list",JSON.stringify(l)))}),this.options.listenPerformance&&$(this.options,e=>{e.forEach(e=>{e.extra.resourceName!==this.options.reportUrl&&this.addReportRecord({reportType:r,...e})})}),this.options.reportErrorReqest&&(m=e=>{const{status:r,url:s,method:o,statusText:n}=e,a=`请求报错接口:${s},方法:${o},状态码:${r},状态信息:${n}`;l.includes(a)||(this.addReportRecord({reportType:t,type:p,message:a}),l.push(a),localStorage.setItem("report-store-list",JSON.stringify(l)))},h=this.options,f=XMLHttpRequest.prototype.open,XMLHttpRequest.prototype.open=function(e,t,r,s,o){let n=Date.now();this.addEventListener("load",function(){const r=Date.now()-n;if(200===this.status){if("string"==typeof this.response)try{const s=JSON.parse(this.response);if("function"==typeof h.errorRequestFunc&&h.errorRequestFunc(s))return void(m&&m({message:`请求成功接口:${t},方法:${e},状态码:${this.status},状态信息:${this.statusText}, 请求耗时:${r}ms;服务器处理错误`,extra:{response:s,headers:this.getAllResponseHeaders()}}))}catch(e){}}else m&&m({message:`请求报错接口:${t},方法:${e},状态码:${this.status},状态信息:${this.statusText}`,extra:{headers:this.getAllResponseHeaders()}})}),this.addEventListener("error",function(){m&&m({message:`请求报错接口:${t},方法:${e},状态码:${this.status},状态信息:${this.statusText}`})}),f.call(this,e,t,r,s,o)},function(e,t,r){var s=window.fetch;window.fetch=function(e,o){let n=Date.now();return s(e,o).then(async s=>{const a=Date.now()-n,i=[...s.headers.entries()].map(e=>e.join(": ")).join("\n");if(200===s.status)try{const o=await s.json();if("function"==typeof r.errorRequestFunc&&r.errorRequestFunc(o))return void(t&&t({message:`请求成功接口:${e},方法:${method},状态码:${s.status},状态信息:${s.statusText}, 请求耗时:${a}ms\n服务器处理错误`,extra:{response:o,headers:i}}))}catch(e){}else t&&t({message:`请求报错接口:${e},方法:${o.method},状态码:${s.status},状态信息:${s.statusText}, 耗时:${a}ms`,extra:{headers:i}});return s}).catch(r=>{t&&t({message:`请求报错接口:${e},方法:${o.method},错误信息\n${r.stack}`})})}}(0,e=>{const{url:r,method:s,statusText:o}=e,n=`请求报错接口:${r},方法:${s},状态信息:${o}`;l.includes(n)||(this.addReportRecord({reportType:t,type:p,message:n}),l.push(n),localStorage.setItem("report-store-list",JSON.stringify(l)))},this.options)),this.options.reportUserNavigate){const e=window.location.href;this.addReportRecord({reportType:s,type:T,message:`请求地址:${e}`,extra:JSON.stringify({url:e})}),w(e=>{this.addReportRecord({reportType:s,type:v,message:`路由变化:${e.to}`,extra:JSON.stringify({route:e.to})})})}}addReportRecord(e){this.timer&&clearTimeout(this.timer),this.timer=setTimeout(()=>{try{this.sendReport(JSON.parse(JSON.stringify(this.records))),this.records=[]}catch(e){}},1e3),this.records.push({appId:this.options.appId,appUrl:this.options.appUrl,reportPath:window.location.pathname,userAgent:window.navigator.userAgent,...e,extra:e.extra&&"object"==typeof e.extra?JSON.stringify(e.extra):void 0})}sendReport(e){const t={appSecretKey:this.options.appSecretKey,appId:this.options.appId,data:e},r=new Blob([JSON.stringify(t)],{type:"application/json; charset=UTF-8"});navigator.sendBeacon(this.options.reportUrl,r)}}let x;function E(e={}){x||(x=new S(e)),x.init()}var N={init:E};e.default=N,e.init=E,Object.defineProperty(e,"__esModule",{value:!0})});
package/package.json CHANGED
@@ -1,8 +1,16 @@
1
1
  {
2
2
  "name": "keeson-web-report-sdk",
3
- "version": "1.1.2",
3
+ "version": "1.1.6",
4
4
  "description": "for web/h5",
5
5
  "main": "bundle.min.js",
6
+ "module": "bundle.esm.js",
7
+ "exports": {
8
+ ".": {
9
+ "require": "./bundle.cjs.js",
10
+ "import": "./bundle.esm.js",
11
+ "browser": "./bundle.min.js"
12
+ }
13
+ },
6
14
  "scripts": {
7
15
  "test": "echo \"Error: no test specified\" && exit 1",
8
16
  "build": "rollup --config rollup.config.js"
@@ -20,7 +28,10 @@
20
28
  "rollup": "^4.55.1"
21
29
  },
22
30
  "files": [
23
- "bundle.min.js"
31
+ "bundle.cjs.js",
32
+ "bundle.esm.js",
33
+ "bundle.min.js",
34
+ "bundle.iife.min.js"
24
35
  ],
25
36
  "dependencies": {
26
37
  }