spy-client 2.1.2 → 2.1.3

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.
Files changed (36) hide show
  1. package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/basicSpec.ts.html +1 -1
  2. package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/checkSpec.ts.html +1 -1
  3. package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/headSpec.ts.html +1 -1
  4. package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/index.html +1 -1
  5. package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/markSpec.ts.html +1 -1
  6. package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/metricSpec.ts.html +1 -1
  7. package/dist/head/base.d.ts +12 -7
  8. package/dist/lib/interface.d.ts +4 -0
  9. package/dist/spy-client-basic.d.ts +3 -3
  10. package/dist/spy-client-basic.esm.js +12 -8
  11. package/dist/spy-client-basic.iife.js +12 -8
  12. package/dist/spy-client-basic.iife.min.js +1 -1
  13. package/dist/spy-client-basic.js +12 -8
  14. package/dist/spy-client-basic.min.js +1 -1
  15. package/dist/spy-client-basic.mjs +12 -8
  16. package/dist/spy-client.d.ts +1 -0
  17. package/dist/spy-client.esm.js +29 -8
  18. package/dist/spy-client.iife.js +29 -8
  19. package/dist/spy-client.iife.min.js +1 -1
  20. package/dist/spy-client.js +29 -8
  21. package/dist/spy-client.min.js +1 -1
  22. package/dist/spy-client.mjs +29 -8
  23. package/dist/spy-head.js +46 -34
  24. package/dist/spy-head.min.js +1 -1
  25. package/dist/spy-local-cache.js +28 -41
  26. package/dist/spy-local-cache.min.js +1 -1
  27. package/example/local-cache.html +77 -15
  28. package/package.json +3 -3
  29. package/src/head/base.ts +37 -4
  30. package/src/head/error.ts +9 -19
  31. package/src/head/whitescreen.ts +14 -14
  32. package/src/lib/huffman.ts +37 -56
  33. package/src/lib/interface.ts +5 -0
  34. package/src/spy-client-basic.ts +20 -15
  35. package/src/spy-client.ts +19 -1
  36. package/src/spy-local-cache.ts +12 -2
@@ -724,7 +724,7 @@ describe('基本发送功能', () => {
724
724
  <div class='footer quiet pad2 space-top1 center small'>
725
725
  Code coverage generated by
726
726
  <a href="https://istanbul.js.org/" target="_blank">istanbul</a>
727
- at Fri Nov 12 2021 17:49:17 GMT+0800 (中国标准时间)
727
+ at Tue Nov 23 2021 15:10:01 GMT+0800 (中国标准时间)
728
728
  </div>
729
729
  </div>
730
730
  <script src="prettify.js"></script>
@@ -463,7 +463,7 @@ describe('问题检测能力', () =&gt; {
463
463
  <div class='footer quiet pad2 space-top1 center small'>
464
464
  Code coverage generated by
465
465
  <a href="https://istanbul.js.org/" target="_blank">istanbul</a>
466
- at Fri Nov 12 2021 17:49:17 GMT+0800 (中国标准时间)
466
+ at Tue Nov 23 2021 15:10:01 GMT+0800 (中国标准时间)
467
467
  </div>
468
468
  </div>
469
469
  <script src="prettify.js"></script>
@@ -679,7 +679,7 @@ describe('spy-head', async () =&gt; {
679
679
  <div class='footer quiet pad2 space-top1 center small'>
680
680
  Code coverage generated by
681
681
  <a href="https://istanbul.js.org/" target="_blank">istanbul</a>
682
- at Fri Nov 12 2021 17:49:17 GMT+0800 (中国标准时间)
682
+ at Tue Nov 23 2021 15:10:01 GMT+0800 (中国标准时间)
683
683
  </div>
684
684
  </div>
685
685
  <script src="prettify.js"></script>
@@ -155,7 +155,7 @@
155
155
  <div class='footer quiet pad2 space-top1 center small'>
156
156
  Code coverage generated by
157
157
  <a href="https://istanbul.js.org/" target="_blank">istanbul</a>
158
- at Fri Nov 12 2021 17:49:17 GMT+0800 (中国标准时间)
158
+ at Tue Nov 23 2021 15:10:01 GMT+0800 (中国标准时间)
159
159
  </div>
160
160
  </div>
161
161
  <script src="prettify.js"></script>
@@ -268,7 +268,7 @@ describe('mark', async () =&gt; {
268
268
  <div class='footer quiet pad2 space-top1 center small'>
269
269
  Code coverage generated by
270
270
  <a href="https://istanbul.js.org/" target="_blank">istanbul</a>
271
- at Fri Nov 12 2021 17:49:17 GMT+0800 (中国标准时间)
271
+ at Tue Nov 23 2021 15:10:01 GMT+0800 (中国标准时间)
272
272
  </div>
273
273
  </div>
274
274
  <script src="prettify.js"></script>
@@ -961,7 +961,7 @@ describe('metric', async () =&gt; {
961
961
  <div class='footer quiet pad2 space-top1 center small'>
962
962
  Code coverage generated by
963
963
  <a href="https://istanbul.js.org/" target="_blank">istanbul</a>
964
- at Fri Nov 12 2021 17:49:17 GMT+0800 (中国标准时间)
964
+ at Tue Nov 23 2021 15:10:01 GMT+0800 (中国标准时间)
965
965
  </div>
966
966
  </div>
967
967
  <script src="prettify.js"></script>
@@ -3,18 +3,23 @@
3
3
  * @author kaivean
4
4
  */
5
5
  import { SpyHeadConf } from '../lib/spyHeadInterface';
6
+ interface SendObj {
7
+ type?: 'perf' | 'except' | 'dist' | 'count';
8
+ group: string;
9
+ info: any;
10
+ dim?: any;
11
+ lid?: string;
12
+ pid?: string;
13
+ ts?: number;
14
+ }
6
15
  declare const _default: {
7
16
  conf: SpyHeadConf;
8
- winerrors: any;
17
+ winerrors: SendObj[];
9
18
  errorDestroy(): void;
10
19
  observerDestroy(): void;
11
20
  entryMap: any;
12
21
  init(conf: SpyHeadConf): void;
13
- send(obj: {
14
- type?: 'perf' | 'except' | 'dist' | 'count';
15
- group: string;
16
- info: any;
17
- dim?: any;
18
- }, logServer?: string | undefined): void;
22
+ addError(obj: SendObj): void;
23
+ send(obj: SendObj, isSend?: boolean | undefined, logServer?: string | undefined): void;
19
24
  };
20
25
  export default _default;
@@ -19,6 +19,10 @@ export interface SpyClientOption {
19
19
  * 日志服务器,默认是webb服务器,尾部需要加?
20
20
  */
21
21
  logServer?: string;
22
+ /**
23
+ * 本地日志cache对象,是指独立的spy-local-cache模块实例
24
+ */
25
+ localCache?: any;
22
26
  }
23
27
  export declare class Module {
24
28
  load?: () => void;
@@ -7,11 +7,11 @@ interface Option {
7
7
  /**
8
8
  * 指标组,它的每个key就是指标名称(英文表示),在平台对应分组添加该指标名称便能实现自动统计
9
9
  */
10
- info: object;
10
+ info: Record<string, unknown>;
11
11
  /**
12
12
  * 维度信息对象,它的每个字段就是一个维度名称(英文表示),在平台对应分组添加该维度名称便能实现自动统计
13
13
  */
14
- dim?: object;
14
+ dim?: Record<string, unknown>;
15
15
  /**
16
16
  * 分组,默认:common
17
17
  */
@@ -39,7 +39,7 @@ interface ErrorOption {
39
39
  /**
40
40
  * 维度信息对象,它的每个字段就是一个维度名称(英文表示),在平台对应分组添加该维度名称便能实现自动统计
41
41
  */
42
- dim?: object;
42
+ dim?: Record<string, unknown>;
43
43
  /**
44
44
  * 分组,默认:common
45
45
  */
@@ -60,6 +60,7 @@ var SpyClient = /** @class */ (function () {
60
60
  lid: option.lid,
61
61
  check: option.check !== false,
62
62
  sample: option.sample,
63
+ localCache: option.localCache,
63
64
  logServer: option.logServer || defaultLogServer,
64
65
  };
65
66
  }
@@ -71,9 +72,9 @@ var SpyClient = /** @class */ (function () {
71
72
  method: 'POST',
72
73
  credentials: 'include',
73
74
  headers: {
74
- 'Content-Type': 'application/json'
75
+ 'Content-Type': 'application/json',
75
76
  },
76
- body: JSON.stringify(data)
77
+ body: JSON.stringify(data),
77
78
  });
78
79
  };
79
80
  SpyClient.prototype.sendPost = function (data) {
@@ -96,6 +97,15 @@ var SpyClient = /** @class */ (function () {
96
97
  if (!this.check(logItem)) {
97
98
  return;
98
99
  }
100
+ logItem = assign({
101
+ pid: this.option.pid,
102
+ lid: this.option.lid,
103
+ ts: Date.now(),
104
+ group: 'common',
105
+ }, logItem);
106
+ if (this.option.localCache) {
107
+ this.option.localCache.addLog(logItem);
108
+ }
99
109
  // 当前api设置了抽样,
100
110
  if (typeof logItem.sample === 'number') {
101
111
  if (Math.random() > logItem.sample) {
@@ -105,12 +115,6 @@ var SpyClient = /** @class */ (function () {
105
115
  else if (typeof this.option.sample === 'number' && Math.random() > this.option.sample) { // 否则,用全局抽样
106
116
  return;
107
117
  }
108
- logItem = assign({
109
- pid: this.option.pid,
110
- lid: this.option.lid,
111
- ts: Date.now(),
112
- group: 'common',
113
- }, logItem);
114
118
  delete logItem.sample;
115
119
  return logItem;
116
120
  };
@@ -63,6 +63,7 @@ var SpyClient = (function () {
63
63
  lid: option.lid,
64
64
  check: option.check !== false,
65
65
  sample: option.sample,
66
+ localCache: option.localCache,
66
67
  logServer: option.logServer || defaultLogServer,
67
68
  };
68
69
  }
@@ -74,9 +75,9 @@ var SpyClient = (function () {
74
75
  method: 'POST',
75
76
  credentials: 'include',
76
77
  headers: {
77
- 'Content-Type': 'application/json'
78
+ 'Content-Type': 'application/json',
78
79
  },
79
- body: JSON.stringify(data)
80
+ body: JSON.stringify(data),
80
81
  });
81
82
  };
82
83
  SpyClient.prototype.sendPost = function (data) {
@@ -99,6 +100,15 @@ var SpyClient = (function () {
99
100
  if (!this.check(logItem)) {
100
101
  return;
101
102
  }
103
+ logItem = assign({
104
+ pid: this.option.pid,
105
+ lid: this.option.lid,
106
+ ts: Date.now(),
107
+ group: 'common',
108
+ }, logItem);
109
+ if (this.option.localCache) {
110
+ this.option.localCache.addLog(logItem);
111
+ }
102
112
  // 当前api设置了抽样,
103
113
  if (typeof logItem.sample === 'number') {
104
114
  if (Math.random() > logItem.sample) {
@@ -108,12 +118,6 @@ var SpyClient = (function () {
108
118
  else if (typeof this.option.sample === 'number' && Math.random() > this.option.sample) { // 否则,用全局抽样
109
119
  return;
110
120
  }
111
- logItem = assign({
112
- pid: this.option.pid,
113
- lid: this.option.lid,
114
- ts: Date.now(),
115
- group: 'common',
116
- }, logItem);
117
121
  delete logItem.sample;
118
122
  return logItem;
119
123
  };
@@ -1 +1 @@
1
- var SpyClient=function(){"use strict";function o(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return(Object.assign||function(t){for(var e=void 0,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.userAgent.toLowerCase().match(/cpu iphone os (.*?)_/),s=t&&t[2]&&+t[2]<14;function u(t){console.error("[SpyClient_log]"+t)}function p(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 c(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,logServer:t.logServer||"https://sp1.baidu.com/5b1ZeDe5KgQFm2e88IuM_a/mwb2.gif?"}}return e.prototype.fetch=function(t,e){window.fetch&&fetch(t,{method:"POST",credentials:"include",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)})},e.prototype.sendPost=function(t){var e=(c(t)?t:[t])[0],n={pid:e.pid,type:e.type,group:e.group},o=this.option.logServer+p(n);!s&&navigator.sendBeacon&&navigator.sendBeacon(o,JSON.stringify(t))||this.fetch(o,t)},e.prototype.handle=function(t){if(this.check(t)){if("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=o({pid:this.option.pid,lid:this.option.lid,ts:Date.now(),group:"common"},t)).sample,t}},e.prototype.send=function(t,e){void 0===e&&(e=!1);for(var n=[],o=0,r=c(t)?t:[t];o<r.length;o++){var i=r[o];if((i=this.handle(i))&&(n.push(i),!e)){var a=this.option.logServer+p(i);!s&&navigator.sendBeacon&&navigator.sendBeacon(a)||((new Image).src=a)}}e&&this.sendPost(n)},e.prototype.check=function(t){if(!this.option.check)return!0;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."+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."+i+" value length execeeds 30 when type == 'dist'"),!1}else if("number"!=typeof a)return u("info."+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 ["+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."+c+" value ["+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}();
1
+ var SpyClient=function(){"use strict";function o(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return(Object.assign||function(t){for(var e=void 0,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.userAgent.toLowerCase().match(/cpu iphone os (.*?)_/),s=t&&t[2]&&+t[2]<14;function u(t){console.error("[SpyClient_log]"+t)}function p(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 c(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.fetch=function(t,e){window.fetch&&fetch(t,{method:"POST",credentials:"include",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)})},e.prototype.sendPost=function(t){var e=(c(t)?t:[t])[0],n={pid:e.pid,type:e.type,group:e.group},o=this.option.logServer+p(n);!s&&navigator.sendBeacon&&navigator.sendBeacon(o,JSON.stringify(t))||this.fetch(o,t)},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=c(t)?t:[t];o<r.length;o++){var i=r[o];if((i=this.handle(i))&&(n.push(i),!e)){var a=this.option.logServer+p(i);!s&&navigator.sendBeacon&&navigator.sendBeacon(a)||((new Image).src=a)}}e&&this.sendPost(n)},e.prototype.check=function(t){if(!this.option.check)return!0;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."+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."+i+" value length execeeds 30 when type == 'dist'"),!1}else if("number"!=typeof a)return u("info."+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 ["+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."+c+" value ["+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}();
@@ -66,6 +66,7 @@
66
66
  lid: option.lid,
67
67
  check: option.check !== false,
68
68
  sample: option.sample,
69
+ localCache: option.localCache,
69
70
  logServer: option.logServer || defaultLogServer,
70
71
  };
71
72
  }
@@ -77,9 +78,9 @@
77
78
  method: 'POST',
78
79
  credentials: 'include',
79
80
  headers: {
80
- 'Content-Type': 'application/json'
81
+ 'Content-Type': 'application/json',
81
82
  },
82
- body: JSON.stringify(data)
83
+ body: JSON.stringify(data),
83
84
  });
84
85
  };
85
86
  SpyClient.prototype.sendPost = function (data) {
@@ -102,6 +103,15 @@
102
103
  if (!this.check(logItem)) {
103
104
  return;
104
105
  }
106
+ logItem = assign({
107
+ pid: this.option.pid,
108
+ lid: this.option.lid,
109
+ ts: Date.now(),
110
+ group: 'common',
111
+ }, logItem);
112
+ if (this.option.localCache) {
113
+ this.option.localCache.addLog(logItem);
114
+ }
105
115
  // 当前api设置了抽样,
106
116
  if (typeof logItem.sample === 'number') {
107
117
  if (Math.random() > logItem.sample) {
@@ -111,12 +121,6 @@
111
121
  else if (typeof this.option.sample === 'number' && Math.random() > this.option.sample) { // 否则,用全局抽样
112
122
  return;
113
123
  }
114
- logItem = assign({
115
- pid: this.option.pid,
116
- lid: this.option.lid,
117
- ts: Date.now(),
118
- group: 'common',
119
- }, logItem);
120
124
  delete logItem.sample;
121
125
  return logItem;
122
126
  };
@@ -1 +1 @@
1
- !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).SpyClient=t()}(this,function(){"use strict";function o(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return(Object.assign||function(e){for(var t=void 0,n=1,o=arguments.length;n<o;n++)for(var r in t=arguments[n])Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r]);return e}).apply(this,e)}var e=navigator.userAgent.toLowerCase().match(/cpu iphone os (.*?)_/),a=e&&e[2]&&+e[2]<14;function h(e){console.error("[SpyClient_log]"+e)}function p(n){return Object.keys(n).map(function(e){var t=n[e];return void 0===t?t="":"string"!=typeof t&&(t=JSON.stringify(t)),encodeURIComponent(e)+"="+encodeURIComponent(t)}).join("&")}function c(e){return"[object Array]"===Object.prototype.toString.call(e)}function t(e){if(this.sample={},this.markCache={},!e.pid)throw new Error("pid is required");this.option={pid:e.pid,lid:e.lid,check:!1!==e.check,sample:e.sample,logServer:e.logServer||"https://sp1.baidu.com/5b1ZeDe5KgQFm2e88IuM_a/mwb2.gif?"}}return t.prototype.fetch=function(e,t){window.fetch&&fetch(e,{method:"POST",credentials:"include",headers:{"Content-Type":"application/json"},body:JSON.stringify(t)})},t.prototype.sendPost=function(e){var t=(c(e)?e:[e])[0],n={pid:t.pid,type:t.type,group:t.group},o=this.option.logServer+p(n);!a&&navigator.sendBeacon&&navigator.sendBeacon(o,JSON.stringify(e))||this.fetch(o,e)},t.prototype.handle=function(e){if(this.check(e)){if("number"==typeof e.sample){if(Math.random()>e.sample)return}else if("number"==typeof this.option.sample&&Math.random()>this.option.sample)return;return delete(e=o({pid:this.option.pid,lid:this.option.lid,ts:Date.now(),group:"common"},e)).sample,e}},t.prototype.send=function(e,t){void 0===t&&(t=!1);for(var n=[],o=0,r=c(e)?e:[e];o<r.length;o++){var i=r[o];if((i=this.handle(i))&&(n.push(i),!t)){var s=this.option.logServer+p(i);!a&&navigator.sendBeacon&&navigator.sendBeacon(s)||((new Image).src=s)}}t&&this.sendPost(n)},t.prototype.check=function(e){if(!this.option.check)return!0;var t=["perf","except","dist","count"];if(-1===t.indexOf(e.type))return h("type only is one of "+t.join(", ")),!1;if(e.group&&30<e.group.length)return h("group length execeeds 30"),!1;var n=/^[a-zA-Z0-9-_]{0,30}$/;if("except"===e.type){if("string"!=typeof e.info.msg||!e.info.msg.length)return h("info.msg field must be not empty and is String"),!1}else for(var o=0,r=Object.keys(e.info);o<r.length;o++){var i=r[o];if(!n.test(i))return h("info."+i+" is unexpected. Length must be not more than 30. Supported chars: a-zA-Z0-9-_"),!1;var s=e.info[i];if("dist"===e.type){if(30<s.length)return h("info."+i+" value length execeeds 30 when type == 'dist'"),!1}else if("number"!=typeof s)return h("info."+i+" value must be number"),!1}if(e.dim)for(var a=0,p=Object.keys(e.dim);a<p.length;a++){var c=p[a];if(!n.test(c))return h("dim key ["+c+"] is unexpected. Length must be not more than 30. Supported chars: a-zA-Z0-9-_"),!1;var f=e.dim[c];if(!/^[a-zA-Z0-9\-_\*\.\s\/#\+@\&\u4e00-\u9fa5]{0,30}$/.test(f))return h("dim."+c+" value ["+f+"] is unexpected. Length must be not more than 30. Supported chars: a-zA-Z0-9-_*. /#+@& and Chinese"),!1}return!0},t.prototype.sendPerf=function(e){this.send(o({type:"perf"},e))},t.prototype.sendExcept=function(e){this.send(o({type:"except"},e))},t.prototype.sendDist=function(e){this.send(o({type:"dist"},e))},t.prototype.sendCount=function(e){this.send(o({type:"count"},e))},t.prototype.sendExceptForError=function(e,t){var n=o({},t);n.info=o({},t.info||{},{msg:e.message,stack:e.stack}),this.sendExcept(n)},t.prototype.startMark=function(e){this.markCache[e]={start:Date.now()}},t.prototype.endMark=function(e){return this.markCache[e]?(this.markCache[e].total=Date.now()-this.markCache[e].start,this.markCache[e].total):0},t.prototype.clearMark=function(e){this.markCache[e]&&delete this.markCache[e]},t.prototype.getAllMark=function(){for(var e={},t=0,n=Object.keys(this.markCache);t<n.length;t++){var o=n[t];e[o]=this.markCache[o].total}return e},t.prototype.clearAllMark=function(){this.markCache={}},t});
1
+ !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).SpyClient=t()}(this,function(){"use strict";function o(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return(Object.assign||function(e){for(var t=void 0,n=1,o=arguments.length;n<o;n++)for(var r in t=arguments[n])Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r]);return e}).apply(this,e)}var e=navigator.userAgent.toLowerCase().match(/cpu iphone os (.*?)_/),s=e&&e[2]&&+e[2]<14;function f(e){console.error("[SpyClient_log]"+e)}function p(n){return Object.keys(n).map(function(e){var t=n[e];return void 0===t?t="":"string"!=typeof t&&(t=JSON.stringify(t)),encodeURIComponent(e)+"="+encodeURIComponent(t)}).join("&")}function c(e){return"[object Array]"===Object.prototype.toString.call(e)}function t(e){if(this.sample={},this.markCache={},!e.pid)throw new Error("pid is required");this.option={pid:e.pid,lid:e.lid,check:!1!==e.check,sample:e.sample,localCache:e.localCache,logServer:e.logServer||"https://sp1.baidu.com/5b1ZeDe5KgQFm2e88IuM_a/mwb2.gif?"}}return t.prototype.fetch=function(e,t){window.fetch&&fetch(e,{method:"POST",credentials:"include",headers:{"Content-Type":"application/json"},body:JSON.stringify(t)})},t.prototype.sendPost=function(e){var t=(c(e)?e:[e])[0],n={pid:t.pid,type:t.type,group:t.group},o=this.option.logServer+p(n);!s&&navigator.sendBeacon&&navigator.sendBeacon(o,JSON.stringify(e))||this.fetch(o,e)},t.prototype.handle=function(e){if(this.check(e)){if(e=o({pid:this.option.pid,lid:this.option.lid,ts:Date.now(),group:"common"},e),this.option.localCache&&this.option.localCache.addLog(e),"number"==typeof e.sample){if(Math.random()>e.sample)return}else if("number"==typeof this.option.sample&&Math.random()>this.option.sample)return;return delete e.sample,e}},t.prototype.send=function(e,t){void 0===t&&(t=!1);for(var n=[],o=0,r=c(e)?e:[e];o<r.length;o++){var i=r[o];if((i=this.handle(i))&&(n.push(i),!t)){var a=this.option.logServer+p(i);!s&&navigator.sendBeacon&&navigator.sendBeacon(a)||((new Image).src=a)}}t&&this.sendPost(n)},t.prototype.check=function(e){if(!this.option.check)return!0;var t=["perf","except","dist","count"];if(-1===t.indexOf(e.type))return f("type only is one of "+t.join(", ")),!1;if(e.group&&30<e.group.length)return f("group length execeeds 30"),!1;var n=/^[a-zA-Z0-9-_]{0,30}$/;if("except"===e.type){if("string"!=typeof e.info.msg||!e.info.msg.length)return f("info.msg field must be not empty and is String"),!1}else for(var o=0,r=Object.keys(e.info);o<r.length;o++){var i=r[o];if(!n.test(i))return f("info."+i+" is unexpected. Length must be not more than 30. Supported chars: a-zA-Z0-9-_"),!1;var a=e.info[i];if("dist"===e.type){if(30<a.length)return f("info."+i+" value length execeeds 30 when type == 'dist'"),!1}else if("number"!=typeof a)return f("info."+i+" value must be number"),!1}if(e.dim)for(var s=0,p=Object.keys(e.dim);s<p.length;s++){var c=p[s];if(!n.test(c))return f("dim key ["+c+"] is unexpected. Length must be not more than 30. Supported chars: a-zA-Z0-9-_"),!1;var h=e.dim[c];if(!/^[a-zA-Z0-9\-_\*\.\s\/#\+@\&\u4e00-\u9fa5]{0,30}$/.test(h))return f("dim."+c+" value ["+h+"] is unexpected. Length must be not more than 30. Supported chars: a-zA-Z0-9-_*. /#+@& and Chinese"),!1}return!0},t.prototype.sendPerf=function(e){this.send(o({type:"perf"},e))},t.prototype.sendExcept=function(e){this.send(o({type:"except"},e))},t.prototype.sendDist=function(e){this.send(o({type:"dist"},e))},t.prototype.sendCount=function(e){this.send(o({type:"count"},e))},t.prototype.sendExceptForError=function(e,t){var n=o({},t);n.info=o({},t.info||{},{msg:e.message,stack:e.stack}),this.sendExcept(n)},t.prototype.startMark=function(e){this.markCache[e]={start:Date.now()}},t.prototype.endMark=function(e){return this.markCache[e]?(this.markCache[e].total=Date.now()-this.markCache[e].start,this.markCache[e].total):0},t.prototype.clearMark=function(e){this.markCache[e]&&delete this.markCache[e]},t.prototype.getAllMark=function(){for(var e={},t=0,n=Object.keys(this.markCache);t<n.length;t++){var o=n[t];e[o]=this.markCache[o].total}return e},t.prototype.clearAllMark=function(){this.markCache={}},t});
@@ -60,6 +60,7 @@ var SpyClient = /** @class */ (function () {
60
60
  lid: option.lid,
61
61
  check: option.check !== false,
62
62
  sample: option.sample,
63
+ localCache: option.localCache,
63
64
  logServer: option.logServer || defaultLogServer,
64
65
  };
65
66
  }
@@ -71,9 +72,9 @@ var SpyClient = /** @class */ (function () {
71
72
  method: 'POST',
72
73
  credentials: 'include',
73
74
  headers: {
74
- 'Content-Type': 'application/json'
75
+ 'Content-Type': 'application/json',
75
76
  },
76
- body: JSON.stringify(data)
77
+ body: JSON.stringify(data),
77
78
  });
78
79
  };
79
80
  SpyClient.prototype.sendPost = function (data) {
@@ -96,6 +97,15 @@ var SpyClient = /** @class */ (function () {
96
97
  if (!this.check(logItem)) {
97
98
  return;
98
99
  }
100
+ logItem = assign({
101
+ pid: this.option.pid,
102
+ lid: this.option.lid,
103
+ ts: Date.now(),
104
+ group: 'common',
105
+ }, logItem);
106
+ if (this.option.localCache) {
107
+ this.option.localCache.addLog(logItem);
108
+ }
99
109
  // 当前api设置了抽样,
100
110
  if (typeof logItem.sample === 'number') {
101
111
  if (Math.random() > logItem.sample) {
@@ -105,12 +115,6 @@ var SpyClient = /** @class */ (function () {
105
115
  else if (typeof this.option.sample === 'number' && Math.random() > this.option.sample) { // 否则,用全局抽样
106
116
  return;
107
117
  }
108
- logItem = assign({
109
- pid: this.option.pid,
110
- lid: this.option.lid,
111
- ts: Date.now(),
112
- group: 'common',
113
- }, logItem);
114
118
  delete logItem.sample;
115
119
  return logItem;
116
120
  };
@@ -7,6 +7,7 @@ import { Module, SpyClientOption, FIDCB, LCPCB, LayoutShiftCB, PageLongtaskCB, L
7
7
  export default class SpyClient extends SpyClientBasic {
8
8
  private readonly modules;
9
9
  constructor(option: SpyClientOption);
10
+ handleHead(): void;
10
11
  listenFID(cb: FIDCB): void;
11
12
  listenLayoutShift(cb: LayoutShiftCB): void;
12
13
  listenLCP(cb: LCPCB): void;
@@ -165,6 +165,7 @@ var SpyClient$1 = /** @class */ (function () {
165
165
  lid: option.lid,
166
166
  check: option.check !== false,
167
167
  sample: option.sample,
168
+ localCache: option.localCache,
168
169
  logServer: option.logServer || defaultLogServer,
169
170
  };
170
171
  }
@@ -176,9 +177,9 @@ var SpyClient$1 = /** @class */ (function () {
176
177
  method: 'POST',
177
178
  credentials: 'include',
178
179
  headers: {
179
- 'Content-Type': 'application/json'
180
+ 'Content-Type': 'application/json',
180
181
  },
181
- body: JSON.stringify(data)
182
+ body: JSON.stringify(data),
182
183
  });
183
184
  };
184
185
  SpyClient.prototype.sendPost = function (data) {
@@ -201,6 +202,15 @@ var SpyClient$1 = /** @class */ (function () {
201
202
  if (!this.check(logItem)) {
202
203
  return;
203
204
  }
205
+ logItem = assign({
206
+ pid: this.option.pid,
207
+ lid: this.option.lid,
208
+ ts: Date.now(),
209
+ group: 'common',
210
+ }, logItem);
211
+ if (this.option.localCache) {
212
+ this.option.localCache.addLog(logItem);
213
+ }
204
214
  // 当前api设置了抽样,
205
215
  if (typeof logItem.sample === 'number') {
206
216
  if (Math.random() > logItem.sample) {
@@ -210,12 +220,6 @@ var SpyClient$1 = /** @class */ (function () {
210
220
  else if (typeof this.option.sample === 'number' && Math.random() > this.option.sample) { // 否则,用全局抽样
211
221
  return;
212
222
  }
213
- logItem = assign({
214
- pid: this.option.pid,
215
- lid: this.option.lid,
216
- ts: Date.now(),
217
- group: 'common',
218
- }, logItem);
219
223
  delete logItem.sample;
220
224
  return logItem;
221
225
  };
@@ -1362,8 +1366,25 @@ var SpyClient = /** @class */ (function (_super) {
1362
1366
  document.addEventListener('visibilitychange', _this.visibilitychangeCB);
1363
1367
  window.addEventListener('beforeunload', _this.leave, false);
1364
1368
  window.addEventListener('unload', _this.leave, false);
1369
+ _this.handleHead();
1365
1370
  return _this;
1366
1371
  }
1372
+ SpyClient.prototype.handleHead = function () {
1373
+ var _this = this;
1374
+ if (this.option.localCache) {
1375
+ var spyHead = window.__spyHead;
1376
+ if (spyHead && spyHead.winerrors) {
1377
+ for (var index = 0; index < spyHead.winerrors.length; index++) {
1378
+ var obj = spyHead.winerrors[index];
1379
+ this.option.localCache.addLog(obj);
1380
+ }
1381
+ // Head发送的异常,也保存一份到本地,主要是JS错误和资源加载异常
1382
+ spyHead.interceptor = function (obj) {
1383
+ _this.option.localCache.addLog(obj);
1384
+ };
1385
+ }
1386
+ }
1387
+ };
1367
1388
  SpyClient.prototype.listenFID = function (cb) {
1368
1389
  this.invoke('listenFID', cb);
1369
1390
  };
@@ -168,6 +168,7 @@ var SpyClient = (function () {
168
168
  lid: option.lid,
169
169
  check: option.check !== false,
170
170
  sample: option.sample,
171
+ localCache: option.localCache,
171
172
  logServer: option.logServer || defaultLogServer,
172
173
  };
173
174
  }
@@ -179,9 +180,9 @@ var SpyClient = (function () {
179
180
  method: 'POST',
180
181
  credentials: 'include',
181
182
  headers: {
182
- 'Content-Type': 'application/json'
183
+ 'Content-Type': 'application/json',
183
184
  },
184
- body: JSON.stringify(data)
185
+ body: JSON.stringify(data),
185
186
  });
186
187
  };
187
188
  SpyClient.prototype.sendPost = function (data) {
@@ -204,6 +205,15 @@ var SpyClient = (function () {
204
205
  if (!this.check(logItem)) {
205
206
  return;
206
207
  }
208
+ logItem = assign({
209
+ pid: this.option.pid,
210
+ lid: this.option.lid,
211
+ ts: Date.now(),
212
+ group: 'common',
213
+ }, logItem);
214
+ if (this.option.localCache) {
215
+ this.option.localCache.addLog(logItem);
216
+ }
207
217
  // 当前api设置了抽样,
208
218
  if (typeof logItem.sample === 'number') {
209
219
  if (Math.random() > logItem.sample) {
@@ -213,12 +223,6 @@ var SpyClient = (function () {
213
223
  else if (typeof this.option.sample === 'number' && Math.random() > this.option.sample) { // 否则,用全局抽样
214
224
  return;
215
225
  }
216
- logItem = assign({
217
- pid: this.option.pid,
218
- lid: this.option.lid,
219
- ts: Date.now(),
220
- group: 'common',
221
- }, logItem);
222
226
  delete logItem.sample;
223
227
  return logItem;
224
228
  };
@@ -1365,8 +1369,25 @@ var SpyClient = (function () {
1365
1369
  document.addEventListener('visibilitychange', _this.visibilitychangeCB);
1366
1370
  window.addEventListener('beforeunload', _this.leave, false);
1367
1371
  window.addEventListener('unload', _this.leave, false);
1372
+ _this.handleHead();
1368
1373
  return _this;
1369
1374
  }
1375
+ SpyClient.prototype.handleHead = function () {
1376
+ var _this = this;
1377
+ if (this.option.localCache) {
1378
+ var spyHead = window.__spyHead;
1379
+ if (spyHead && spyHead.winerrors) {
1380
+ for (var index = 0; index < spyHead.winerrors.length; index++) {
1381
+ var obj = spyHead.winerrors[index];
1382
+ this.option.localCache.addLog(obj);
1383
+ }
1384
+ // Head发送的异常,也保存一份到本地,主要是JS错误和资源加载异常
1385
+ spyHead.interceptor = function (obj) {
1386
+ _this.option.localCache.addLog(obj);
1387
+ };
1388
+ }
1389
+ }
1390
+ };
1370
1391
  SpyClient.prototype.listenFID = function (cb) {
1371
1392
  this.invoke('listenFID', cb);
1372
1393
  };