spy-client 2.1.14 → 2.2.1

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 (44) hide show
  1. package/.travis.yml +7 -4
  2. package/AGENTS.md +132 -0
  3. package/README.md +29 -18
  4. package/dist/head/base.d.ts +27 -25
  5. package/dist/head/error.d.ts +6 -6
  6. package/dist/head/observer.d.ts +5 -5
  7. package/dist/head/whitescreen.d.ts +6 -6
  8. package/dist/lib/data.d.ts +6 -6
  9. package/dist/lib/huffman.d.ts +9 -9
  10. package/dist/lib/interface.d.ts +209 -209
  11. package/dist/lib/spyHeadInterface.d.ts +36 -28
  12. package/dist/lib/util.d.ts +22 -22
  13. package/dist/module/fid.d.ts +15 -15
  14. package/dist/module/layoutShift.d.ts +18 -18
  15. package/dist/module/lcp.d.ts +17 -17
  16. package/dist/module/longtask.d.ts +46 -46
  17. package/dist/module/memory.d.ts +11 -11
  18. package/dist/module/navigatorInfo.d.ts +9 -9
  19. package/dist/module/resource.d.ts +39 -39
  20. package/dist/module/timing.d.ts +11 -11
  21. package/dist/module/tti.d.ts +19 -19
  22. package/dist/spy-client-basic.d.ts +118 -104
  23. package/dist/spy-client-basic.esm.js +298 -294
  24. package/dist/spy-client-basic.iife.js +298 -294
  25. package/dist/spy-client-basic.iife.min.js +1 -1
  26. package/dist/spy-client-basic.js +298 -294
  27. package/dist/spy-client-basic.min.js +1 -1
  28. package/dist/spy-client-basic.mjs +298 -294
  29. package/dist/spy-client.d.ts +32 -32
  30. package/dist/spy-client.esm.js +1430 -1419
  31. package/dist/spy-client.iife.js +1430 -1419
  32. package/dist/spy-client.iife.min.js +1 -1
  33. package/dist/spy-client.js +1430 -1419
  34. package/dist/spy-client.min.js +1 -1
  35. package/dist/spy-client.mjs +1430 -1419
  36. package/dist/spy-head.d.ts +6 -6
  37. package/dist/spy-head.js +359 -355
  38. package/dist/spy-head.min.js +1 -1
  39. package/dist/spy-local-cache.d.ts +18 -18
  40. package/dist/spy-local-cache.js +718 -711
  41. package/dist/spy-local-cache.min.js +1 -1
  42. package/package.json +26 -73
  43. package/test-results/.last-run.json +4 -0
  44. package/karma.conf.js +0 -111
@@ -1,299 +1,303 @@
1
- /**
2
- * @file utils
3
- * @author kaivean
4
- */
5
- function assign() {
6
- var args = [];
7
- for (var _i = 0; _i < arguments.length; _i++) {
8
- args[_i] = arguments[_i];
9
- }
10
- var __assign = Object.assign || function __assign(t) {
11
- for (var s = void 0, i = 1, n = arguments.length; i < n; i++) {
12
- // eslint-disable-next-line prefer-rest-params
13
- s = arguments[i];
14
- for (var p in s) {
15
- if (Object.prototype.hasOwnProperty.call(s, p)) {
16
- t[p] = s[p];
17
- }
18
- }
19
- }
20
- return t;
21
- };
22
- return __assign.apply(this, args);
1
+ /**
2
+ * @file utils
3
+ * @author kaivean
4
+ */
5
+ function assign() {
6
+ var args = [];
7
+ for (var _i = 0; _i < arguments.length; _i++) {
8
+ args[_i] = arguments[_i];
9
+ }
10
+ var __assign = Object.assign || function __assign(t) {
11
+ for (var s = void 0, i = 1, n = arguments.length; i < n; i++) {
12
+ // eslint-disable-next-line prefer-rest-params
13
+ s = arguments[i];
14
+ for (var p in s) {
15
+ if (Object.prototype.hasOwnProperty.call(s, p)) {
16
+ t[p] = s[p];
17
+ }
18
+ }
19
+ }
20
+ return t;
21
+ };
22
+ return __assign.apply(null, args);
23
23
  }
24
24
 
25
- /**
26
- * @file SpyClient
27
- * @author kaivean
28
- */
29
- var defaultLogServer = 'https://sp1.baidu.com/5b1ZeDe5KgQFm2e88IuM_a/mwb2.gif?';
30
- // 基础版本兼容非浏览器环境
31
- var ver = navigator && navigator.userAgent ? navigator.userAgent.toLowerCase().match(/cpu iphone os (.*?)_/) : '';
32
- var isLtIos14 = ver && ver[2] && (+ver[2] < 14);
33
- function err(msg) {
34
- console.error("[SpyClient_log]".concat(msg));
35
- // throw new Error(msg);
36
- }
37
- function stringify(obj) {
38
- return Object.keys(obj).map(function (key) {
39
- var value = obj[key];
40
- if (typeof value === 'undefined') {
41
- value = '';
42
- }
43
- else if (typeof value !== 'string') {
44
- value = JSON.stringify(value);
45
- }
46
- return encodeURIComponent(key) + '=' + encodeURIComponent(value);
47
- }).join('&');
48
- }
49
- function isArray(arr) {
50
- return Object.prototype.toString.call(arr) === '[object Array]';
51
- }
52
- var SpyClient = /** @class */ (function () {
53
- function SpyClient(option) {
54
- this.sample = {};
55
- this.markCache = {};
56
- if (!option.pid) {
57
- throw new Error('pid is required');
58
- }
59
- this.option = {
60
- pid: option.pid,
61
- lid: option.lid,
62
- check: option.check !== false,
63
- sample: option.sample,
64
- localCache: option.localCache,
65
- logServer: option.logServer || defaultLogServer,
66
- };
67
- }
68
- SpyClient.prototype.handle = function (logItem) {
69
- if (!this.check(logItem)) {
70
- return;
71
- }
72
- logItem = assign({
73
- pid: this.option.pid,
74
- lid: this.option.lid,
75
- ts: Date.now(),
76
- group: 'common',
77
- }, logItem);
78
- if (this.option.localCache) {
79
- this.option.localCache.addLog(logItem);
80
- }
81
- // 当前api设置了抽样,
82
- if (typeof logItem.sample === 'number') {
83
- if (Math.random() > logItem.sample) {
84
- return;
85
- }
86
- }
87
- else if (typeof this.option.sample === 'number' && Math.random() > this.option.sample) { // 否则,用全局抽样
88
- return;
89
- }
90
- delete logItem.sample;
91
- return logItem;
92
- };
93
- SpyClient.prototype.send = function (data, post) {
94
- if (post === void 0) { post = false; }
95
- var logItems = isArray(data) ? data : [data];
96
- var postData = [];
97
- for (var _i = 0, logItems_1 = logItems; _i < logItems_1.length; _i++) {
98
- var logItem = logItems_1[_i];
99
- logItem = this.handle(logItem);
100
- if (!logItem) {
101
- continue;
102
- }
103
- postData.push(logItem);
104
- // 期望通过post方式上传日志
105
- if (post) {
106
- continue;
107
- }
108
- var url = this.option.logServer + stringify(logItem);
109
- this.request(url);
110
- }
111
- if (post) {
112
- this.sendPost(postData);
113
- }
114
- };
115
- SpyClient.prototype.check = function (query) {
116
- if (!this.option.check) {
117
- return true;
118
- }
119
- var types = ['perf', 'except', 'dist', 'count'];
120
- if (types.indexOf(query.type) === -1) {
121
- err('type only is one of ' + types.join(', '));
122
- return false;
123
- }
124
- if (query.group && query.group.length > 30) {
125
- err('group length execeeds 30');
126
- return false;
127
- }
128
- var simpleReg = /^[a-zA-Z0-9-_]{0,30}$/;
129
- if (query.type === 'except') {
130
- if (!(typeof query.info.msg === 'string' && query.info.msg.length)) {
131
- err('info.msg field must be not empty and is String');
132
- return false;
133
- }
134
- }
135
- else {
136
- for (var _i = 0, _a = Object.keys(query.info); _i < _a.length; _i++) {
137
- var infoKey = _a[_i];
138
- if (!simpleReg.test(infoKey)) {
139
- err("info.".concat(infoKey, " is unexpected. ")
140
- + 'Length must be not more than 30. '
141
- + 'Supported chars: a-zA-Z0-9-_');
142
- return false;
143
- }
144
- var infoVal = query.info[infoKey];
145
- if (query.type === 'dist') {
146
- if (infoVal.length > 30) {
147
- err("info.".concat(infoKey, " value length execeeds 30 when type == 'dist'"));
148
- return false;
149
- }
150
- }
151
- else if (typeof infoVal !== 'number') {
152
- err("info.".concat(infoKey, " value must be number"));
153
- return false;
154
- }
155
- }
156
- }
157
- if (query.dim) {
158
- for (var _b = 0, _c = Object.keys(query.dim); _b < _c.length; _b++) {
159
- var dimKey = _c[_b];
160
- if (!simpleReg.test(dimKey)) {
161
- err("dim key [".concat(dimKey, "] is unexpected. ")
162
- + 'Length must be not more than 30. '
163
- + 'Supported chars: a-zA-Z0-9-_');
164
- return false;
165
- }
166
- var dimVal = query.dim[dimKey];
167
- if (!/^[a-zA-Z0-9\-_\*\.\s\/#\+@\&\u4e00-\u9fa5]{0,30}$/.test(dimVal)) {
168
- err("dim.".concat(dimKey, " value [").concat(dimVal, "] is unexpected. ")
169
- + 'Length must be not more than 30. '
170
- + 'Supported chars: a-zA-Z0-9-_*. /#+@& and Chinese');
171
- return false;
172
- }
173
- }
174
- }
175
- return true;
176
- };
177
- /**
178
- *
179
- * @param option 配置
180
- */
181
- SpyClient.prototype.sendPerf = function (option) {
182
- this.send(assign({
183
- type: 'perf',
184
- }, option));
185
- };
186
- /**
187
- *
188
- * @param option 错误配置项
189
- */
190
- SpyClient.prototype.sendExcept = function (option) {
191
- this.send(assign({
192
- type: 'except',
193
- }, option));
194
- };
195
- /**
196
- *
197
- * @param option 配置
198
- */
199
- SpyClient.prototype.sendDist = function (option) {
200
- this.send(assign({
201
- type: 'dist',
202
- }, option));
203
- };
204
- /**
205
- *
206
- * @param option 配置
207
- */
208
- SpyClient.prototype.sendCount = function (option) {
209
- this.send(assign({
210
- type: 'count',
211
- }, option));
212
- };
213
- /**
214
- *
215
- * @param e 错误实例
216
- * @param option 错误配置项
217
- */
218
- SpyClient.prototype.sendExceptForError = function (e, option) {
219
- var newOpt = assign({}, option);
220
- newOpt.info = assign({}, option.info || {}, {
221
- msg: e.message,
222
- stack: e.stack,
223
- });
224
- this.sendExcept(newOpt);
225
- };
226
- SpyClient.prototype.startMark = function (sign) {
227
- this.markCache[sign] = {
228
- start: Date.now(),
229
- };
230
- };
231
- SpyClient.prototype.endMark = function (sign) {
232
- if (this.markCache[sign]) {
233
- this.markCache[sign].total = Date.now() - this.markCache[sign].start;
234
- return this.markCache[sign].total;
235
- }
236
- return 0;
237
- };
238
- SpyClient.prototype.clearMark = function (sign) {
239
- if (this.markCache[sign]) {
240
- delete this.markCache[sign];
241
- }
242
- };
243
- SpyClient.prototype.getAllMark = function () {
244
- var ret = {};
245
- for (var _i = 0, _a = Object.keys(this.markCache); _i < _a.length; _i++) {
246
- var sign = _a[_i];
247
- ret[sign] = this.markCache[sign].total;
248
- }
249
- return ret;
250
- };
251
- SpyClient.prototype.clearAllMark = function () {
252
- this.markCache = {};
253
- };
254
- // send(data, true) 也能以post发送,但是会有严格校验
255
- // sendPost能以post发送,但没有校验
256
- SpyClient.prototype.sendPost = function (data) {
257
- var logItems = isArray(data) ? data : [data];
258
- var first = logItems[0];
259
- // 需要在页面暴漏出来pid等基本信息,方式调试查看基本信息与兼容目前的监控
260
- var query = {
261
- pid: first.pid,
262
- type: first.type,
263
- group: first.group,
264
- };
265
- var url = this.option.logServer + stringify(query);
266
- this.request(url, data);
267
- };
268
- // 有data时,意味着要用post发送请求
269
- SpyClient.prototype.request = function (url, data) {
270
- if (!(!isLtIos14
271
- && navigator
272
- && navigator.sendBeacon
273
- && navigator.sendBeacon(url, data ? JSON.stringify(data) : undefined))) {
274
- if (data) {
275
- this.fetch(url, data);
276
- }
277
- else {
278
- (new Image()).src = url;
279
- }
280
- }
281
- };
282
- SpyClient.prototype.fetch = function (url, data) {
283
- if (!fetch) {
284
- err('Global fetch method doesn\'t exist');
285
- return;
286
- }
287
- fetch(url, {
288
- method: 'POST',
289
- credentials: 'include',
290
- headers: {
291
- 'Content-Type': 'application/json',
292
- },
293
- body: JSON.stringify(data),
294
- });
295
- };
296
- return SpyClient;
25
+ /**
26
+ * @file SpyClient
27
+ * @author kaivean
28
+ */
29
+ var defaultLogServer = 'https://sp1.baidu.com/5b1ZeDe5KgQFm2e88IuM_a/mwb2.gif?';
30
+ // 基础版本兼容非浏览器环境
31
+ var ver = navigator && navigator.userAgent ? navigator.userAgent.toLowerCase().match(/cpu iphone os (.*?)_/) : '';
32
+ var isLtIos14 = ver && ver[2] && (+ver[2] < 14);
33
+ function err(msg) {
34
+ console.error("[SpyClient_log]".concat(msg));
35
+ // throw new Error(msg);
36
+ }
37
+ function stringify(obj) {
38
+ return Object.keys(obj).map(function (key) {
39
+ var value = obj[key];
40
+ if (typeof value === 'undefined') {
41
+ value = '';
42
+ }
43
+ else if (typeof value !== 'string') {
44
+ value = JSON.stringify(value);
45
+ }
46
+ return encodeURIComponent(key) + '=' + encodeURIComponent(value);
47
+ }).join('&');
48
+ }
49
+ function isArray(arr) {
50
+ return Object.prototype.toString.call(arr) === '[object Array]';
51
+ }
52
+ var SpyClient = /** @class */ (function () {
53
+ function SpyClient(option) {
54
+ this.sample = {};
55
+ this.markCache = {};
56
+ if (!option.pid) {
57
+ throw new Error('pid is required');
58
+ }
59
+ this.option = {
60
+ pid: option.pid,
61
+ lid: option.lid,
62
+ check: option.check !== false,
63
+ sample: option.sample,
64
+ localCache: option.localCache,
65
+ logServer: option.logServer || defaultLogServer,
66
+ };
67
+ }
68
+ SpyClient.prototype.handle = function (logItem) {
69
+ if (!this.check(logItem)) {
70
+ return;
71
+ }
72
+ logItem = assign({
73
+ pid: this.option.pid,
74
+ lid: this.option.lid,
75
+ ts: Date.now(),
76
+ group: 'common',
77
+ }, logItem);
78
+ if (this.option.localCache) {
79
+ this.option.localCache.addLog(logItem);
80
+ }
81
+ // 当前api设置了抽样,
82
+ if (typeof logItem.sample === 'number') {
83
+ if (Math.random() > logItem.sample) {
84
+ return;
85
+ }
86
+ }
87
+ else if (typeof this.option.sample === 'number' && Math.random() > this.option.sample) { // 否则,用全局抽样
88
+ return;
89
+ }
90
+ delete logItem.sample;
91
+ return logItem;
92
+ };
93
+ SpyClient.prototype.send = function (data, post) {
94
+ if (post === void 0) { post = false; }
95
+ var logItems = isArray(data) ? data : [data];
96
+ var postData = [];
97
+ for (var _i = 0, logItems_1 = logItems; _i < logItems_1.length; _i++) {
98
+ var logItem = logItems_1[_i];
99
+ logItem = this.handle(logItem);
100
+ if (!logItem) {
101
+ continue;
102
+ }
103
+ postData.push(logItem);
104
+ // 期望通过post方式上传日志
105
+ if (post) {
106
+ continue;
107
+ }
108
+ var url = this.option.logServer + stringify(logItem);
109
+ this.request(url);
110
+ }
111
+ if (post) {
112
+ this.sendPost(postData);
113
+ }
114
+ };
115
+ SpyClient.prototype.check = function (query) {
116
+ if (!this.option.check) {
117
+ return true;
118
+ }
119
+ var types = ['perf', 'except', 'dist', 'count'];
120
+ if (types.indexOf(query.type) === -1) {
121
+ err('type only is one of ' + types.join(', '));
122
+ return false;
123
+ }
124
+ if (query.group && query.group.length > 30) {
125
+ err('group length execeeds 30');
126
+ return false;
127
+ }
128
+ var simpleReg = /^[a-zA-Z0-9-_]{0,30}$/;
129
+ if (query.type === 'except') {
130
+ if (!(typeof query.info.msg === 'string' && query.info.msg.length)) {
131
+ err('info.msg field must be not empty and is String');
132
+ return false;
133
+ }
134
+ }
135
+ else {
136
+ for (var _i = 0, _a = Object.keys(query.info); _i < _a.length; _i++) {
137
+ var infoKey = _a[_i];
138
+ if (!simpleReg.test(infoKey)) {
139
+ err("info.".concat(infoKey, " is unexpected. ")
140
+ + 'Length must be not more than 30. '
141
+ + 'Supported chars: a-zA-Z0-9-_');
142
+ return false;
143
+ }
144
+ var infoVal = query.info[infoKey];
145
+ if (query.type === 'dist') {
146
+ if (infoVal.length > 30) {
147
+ err("info.".concat(infoKey, " value length execeeds 30 when type == 'dist'"));
148
+ return false;
149
+ }
150
+ }
151
+ else if (typeof infoVal !== 'number') {
152
+ err("info.".concat(infoKey, " value must be number"));
153
+ return false;
154
+ }
155
+ }
156
+ }
157
+ if (query.dim) {
158
+ for (var _b = 0, _c = Object.keys(query.dim); _b < _c.length; _b++) {
159
+ var dimKey = _c[_b];
160
+ if (!simpleReg.test(dimKey)) {
161
+ err("dim key [".concat(dimKey, "] is unexpected. ")
162
+ + 'Length must be not more than 30. '
163
+ + 'Supported chars: a-zA-Z0-9-_');
164
+ return false;
165
+ }
166
+ var dimVal = query.dim[dimKey];
167
+ if (!/^[a-zA-Z0-9\-_\*\.\s\/#\+@\&\u4e00-\u9fa5]{0,30}$/.test(dimVal)) {
168
+ err("dim.".concat(dimKey, " value [").concat(dimVal, "] is unexpected. ")
169
+ + 'Length must be not more than 30. '
170
+ + 'Supported chars: a-zA-Z0-9-_*. /#+@& and Chinese');
171
+ return false;
172
+ }
173
+ }
174
+ }
175
+ return true;
176
+ };
177
+ /**
178
+ *
179
+ * @param option 配置
180
+ */
181
+ SpyClient.prototype.sendPerf = function (option) {
182
+ this.send(assign({
183
+ type: 'perf',
184
+ }, option));
185
+ };
186
+ /**
187
+ *
188
+ * @param option 错误配置项
189
+ */
190
+ SpyClient.prototype.sendExcept = function (option) {
191
+ this.send(assign({
192
+ type: 'except',
193
+ }, option));
194
+ };
195
+ /**
196
+ *
197
+ * @param option 配置
198
+ */
199
+ SpyClient.prototype.sendDist = function (option) {
200
+ this.send(assign({
201
+ type: 'dist',
202
+ }, option));
203
+ };
204
+ /**
205
+ *
206
+ * @param option 配置
207
+ */
208
+ SpyClient.prototype.sendCount = function (option) {
209
+ this.send(assign({
210
+ type: 'count',
211
+ }, option));
212
+ };
213
+ /**
214
+ *
215
+ * @param e 错误实例
216
+ * @param option 错误配置项
217
+ */
218
+ SpyClient.prototype.sendExceptForError = function (e, option) {
219
+ var newOpt = assign({}, option);
220
+ newOpt.info = assign({}, option.info || {}, {
221
+ msg: e.message,
222
+ stack: e.stack,
223
+ });
224
+ this.sendExcept(newOpt);
225
+ };
226
+ SpyClient.prototype.startMark = function (sign) {
227
+ this.markCache[sign] = {
228
+ start: Date.now(),
229
+ };
230
+ };
231
+ SpyClient.prototype.endMark = function (sign) {
232
+ if (this.markCache[sign]) {
233
+ this.markCache[sign].total = Date.now() - this.markCache[sign].start;
234
+ return this.markCache[sign].total;
235
+ }
236
+ return 0;
237
+ };
238
+ SpyClient.prototype.clearMark = function (sign) {
239
+ if (this.markCache[sign]) {
240
+ delete this.markCache[sign];
241
+ }
242
+ };
243
+ SpyClient.prototype.getAllMark = function () {
244
+ var ret = {};
245
+ for (var _i = 0, _a = Object.keys(this.markCache); _i < _a.length; _i++) {
246
+ var sign = _a[_i];
247
+ ret[sign] = this.markCache[sign].total;
248
+ }
249
+ return ret;
250
+ };
251
+ SpyClient.prototype.clearAllMark = function () {
252
+ this.markCache = {};
253
+ };
254
+ /**
255
+ * sendPost能以post请求发送,可以承接多个日志项,上报内容size可以提升,但没有前端校验
256
+ * send(data, true) 也能以post请求发送,但是会有严格校验
257
+ * 要求:发送的多条日志必须是同一个pid,同一类型type,同一分组group
258
+ * @param data 日志项或者日志项数组
259
+ */
260
+ SpyClient.prototype.sendPost = function (data) {
261
+ var logItems = isArray(data) ? data : [data];
262
+ var first = logItems[0];
263
+ // 需要在页面暴漏出来pid等基本信息,方式调试查看基本信息与兼容目前的监控
264
+ var query = {
265
+ pid: first.pid,
266
+ type: first.type,
267
+ group: first.group,
268
+ };
269
+ var url = this.option.logServer + stringify(query);
270
+ this.request(url, data);
271
+ };
272
+ // 有data时,意味着要用post发送请求
273
+ SpyClient.prototype.request = function (url, data) {
274
+ if (!(!isLtIos14
275
+ && navigator
276
+ && navigator.sendBeacon
277
+ && navigator.sendBeacon(url, data ? JSON.stringify(data) : undefined))) {
278
+ if (data) {
279
+ this.fetch(url, data);
280
+ }
281
+ else {
282
+ (new Image()).src = url;
283
+ }
284
+ }
285
+ };
286
+ SpyClient.prototype.fetch = function (url, data) {
287
+ if (!fetch) {
288
+ err('Global fetch method doesn\'t exist');
289
+ return;
290
+ }
291
+ fetch(url, {
292
+ method: 'POST',
293
+ credentials: 'include',
294
+ headers: {
295
+ 'Content-Type': 'application/json',
296
+ },
297
+ body: JSON.stringify(data),
298
+ });
299
+ };
300
+ return SpyClient;
297
301
  }());
298
302
 
299
303
  export { SpyClient as default };