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 +1 -0
- package/bundle.iife.min.js +1 -0
- package/bundle.min.js +1 -1
- package/package.json +13 -2
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(
|
|
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.
|
|
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.
|
|
31
|
+
"bundle.cjs.js",
|
|
32
|
+
"bundle.esm.js",
|
|
33
|
+
"bundle.min.js",
|
|
34
|
+
"bundle.iife.min.js"
|
|
24
35
|
],
|
|
25
36
|
"dependencies": {
|
|
26
37
|
}
|