spy-client
Version:
spy client
2 lines (1 loc) • 4.21 kB
JavaScript
var SpyClient=(()=>{function o(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return(Object.assign||function(t){for(var e,n=1,o=arguments.length;n<o;n++)for(var r in e=arguments[n])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t}).apply(this,t)}var t=navigator&&navigator.userAgent?navigator.userAgent.toLowerCase().match(/cpu iphone os (.*?)_/):"",n=t&&t[2]&&+t[2]<14;function u(t){console.error("[SpyClient_log]".concat(t))}function a(n){return Object.keys(n).map(function(t){var e=n[t];return void 0===e?e="":"string"!=typeof e&&(e=JSON.stringify(e)),encodeURIComponent(t)+"="+encodeURIComponent(e)}).join("&")}function s(t){return"[object Array]"===Object.prototype.toString.call(t)}function e(t){if(this.sample={},this.markCache={},!t.pid)throw new Error("pid is required");this.option={pid:t.pid,lid:t.lid,check:!1!==t.check,sample:t.sample,localCache:t.localCache,logServer:t.logServer||"https://sp1.baidu.com/5b1ZeDe5KgQFm2e88IuM_a/mwb2.gif?"}}return e.prototype.handle=function(t){if(this.check(t)){if(t=o({pid:this.option.pid,lid:this.option.lid,ts:Date.now(),group:"common"},t),this.option.localCache&&this.option.localCache.addLog(t),"number"==typeof t.sample){if(Math.random()>t.sample)return}else if("number"==typeof this.option.sample&&Math.random()>this.option.sample)return;return delete t.sample,t}},e.prototype.send=function(t,e){void 0===e&&(e=!1);for(var n=[],o=0,r=s(t)?t:[t];o<r.length;o++){var i=r[o];(i=this.handle(i))&&(n.push(i),e||(i=this.option.logServer+a(i),this.request(i)))}e&&this.sendPost(n)},e.prototype.check=function(t){if(this.option.check){var e=["perf","except","dist","count"];if(-1===e.indexOf(t.type))return u("type only is one of "+e.join(", ")),!1;if(t.group&&30<t.group.length)return u("group length execeeds 30"),!1;var n=/^[a-zA-Z0-9-_]{0,30}$/;if("except"===t.type){if("string"!=typeof t.info.msg||!t.info.msg.length)return u("info.msg field must be not empty and is String"),!1}else for(var o=0,r=Object.keys(t.info);o<r.length;o++){var i=r[o];if(!n.test(i))return u("info.".concat(i," is unexpected. ")+"Length must be not more than 30. Supported chars: a-zA-Z0-9-_"),!1;var a=t.info[i];if("dist"===t.type){if(30<a.length)return u("info.".concat(i," value length execeeds 30 when type == 'dist'")),!1}else if("number"!=typeof a)return u("info.".concat(i," value must be number")),!1}if(t.dim)for(var s=0,p=Object.keys(t.dim);s<p.length;s++){var c=p[s];if(!n.test(c))return u("dim key [".concat(c,"] is unexpected. ")+"Length must be not more than 30. Supported chars: a-zA-Z0-9-_"),!1;var h=t.dim[c];if(!/^[a-zA-Z0-9\-_\*\.\s\/#\+@\&\u4e00-\u9fa5]{0,30}$/.test(h))return u("dim.".concat(c," value [").concat(h,"] is unexpected. ")+"Length must be not more than 30. Supported chars: a-zA-Z0-9-_*. /#+@& and Chinese"),!1}}return!0},e.prototype.sendPerf=function(t){this.send(o({type:"perf"},t))},e.prototype.sendExcept=function(t){this.send(o({type:"except"},t))},e.prototype.sendDist=function(t){this.send(o({type:"dist"},t))},e.prototype.sendCount=function(t){this.send(o({type:"count"},t))},e.prototype.sendExceptForError=function(t,e){var n=o({},e);n.info=o({},e.info||{},{msg:t.message,stack:t.stack}),this.sendExcept(n)},e.prototype.startMark=function(t){this.markCache[t]={start:Date.now()}},e.prototype.endMark=function(t){return this.markCache[t]?(this.markCache[t].total=Date.now()-this.markCache[t].start,this.markCache[t].total):0},e.prototype.clearMark=function(t){this.markCache[t]&&delete this.markCache[t]},e.prototype.getAllMark=function(){for(var t={},e=0,n=Object.keys(this.markCache);e<n.length;e++){var o=n[e];t[o]=this.markCache[o].total}return t},e.prototype.clearAllMark=function(){this.markCache={}},e.prototype.sendPost=function(t){var e=(s(t)?t:[t])[0],e={pid:e.pid,type:e.type,group:e.group},e=this.option.logServer+a(e);this.request(e,t)},e.prototype.request=function(t,e){!n&&navigator&&navigator.sendBeacon&&navigator.sendBeacon(t,e?JSON.stringify(e):void 0)||(e?this.fetch(t,e):(new Image).src=t)},e.prototype.fetch=function(t,e){fetch?fetch(t,{method:"POST",credentials:"include",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)}):u("Global fetch method doesn't exist")},e})();