spy-client 2.1.1 → 2.1.7

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 (70) hide show
  1. package/README.md +9 -3
  2. package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/base.css +224 -0
  3. package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/basicSpec.ts.html +740 -0
  4. package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/block-navigation.js +79 -0
  5. package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/checkSpec.ts.html +479 -0
  6. package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/favicon.png +0 -0
  7. package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/headSpec.ts.html +695 -0
  8. package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/index.html +171 -0
  9. package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/markSpec.ts.html +284 -0
  10. package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/metricSpec.ts.html +977 -0
  11. package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/prettify.css +1 -0
  12. package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/prettify.js +2 -0
  13. package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/sort-arrow-sprite.png +0 -0
  14. package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/sorter.js +170 -0
  15. package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/base.css +224 -0
  16. package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/basicSpec.ts.html +740 -0
  17. package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/block-navigation.js +79 -0
  18. package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/checkSpec.ts.html +479 -0
  19. package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/favicon.png +0 -0
  20. package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/headSpec.ts.html +695 -0
  21. package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/index.html +171 -0
  22. package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/markSpec.ts.html +284 -0
  23. package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/metricSpec.ts.html +977 -0
  24. package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/prettify.css +1 -0
  25. package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/prettify.js +2 -0
  26. package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/sort-arrow-sprite.png +0 -0
  27. package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/sorter.js +170 -0
  28. package/coverage/Chrome 96.0.4664.55 (Mac OS 10.15.7)/html/base.css +224 -0
  29. package/coverage/Chrome 96.0.4664.55 (Mac OS 10.15.7)/html/basicSpec.ts.html +740 -0
  30. package/coverage/Chrome 96.0.4664.55 (Mac OS 10.15.7)/html/block-navigation.js +79 -0
  31. package/coverage/Chrome 96.0.4664.55 (Mac OS 10.15.7)/html/checkSpec.ts.html +479 -0
  32. package/coverage/Chrome 96.0.4664.55 (Mac OS 10.15.7)/html/favicon.png +0 -0
  33. package/coverage/Chrome 96.0.4664.55 (Mac OS 10.15.7)/html/headSpec.ts.html +695 -0
  34. package/coverage/Chrome 96.0.4664.55 (Mac OS 10.15.7)/html/index.html +171 -0
  35. package/coverage/Chrome 96.0.4664.55 (Mac OS 10.15.7)/html/markSpec.ts.html +284 -0
  36. package/coverage/Chrome 96.0.4664.55 (Mac OS 10.15.7)/html/metricSpec.ts.html +977 -0
  37. package/coverage/Chrome 96.0.4664.55 (Mac OS 10.15.7)/html/prettify.css +1 -0
  38. package/coverage/Chrome 96.0.4664.55 (Mac OS 10.15.7)/html/prettify.js +2 -0
  39. package/coverage/Chrome 96.0.4664.55 (Mac OS 10.15.7)/html/sort-arrow-sprite.png +0 -0
  40. package/coverage/Chrome 96.0.4664.55 (Mac OS 10.15.7)/html/sorter.js +170 -0
  41. package/dist/head/base.d.ts +12 -7
  42. package/dist/lib/interface.d.ts +4 -0
  43. package/dist/spy-client-basic.d.ts +5 -1
  44. package/dist/spy-client-basic.esm.js +82 -21
  45. package/dist/spy-client-basic.iife.js +82 -21
  46. package/dist/spy-client-basic.iife.min.js +1 -1
  47. package/dist/spy-client-basic.js +82 -21
  48. package/dist/spy-client-basic.min.js +1 -1
  49. package/dist/spy-client-basic.mjs +82 -21
  50. package/dist/spy-client.d.ts +1 -0
  51. package/dist/spy-client.esm.js +109 -31
  52. package/dist/spy-client.iife.js +109 -31
  53. package/dist/spy-client.iife.min.js +1 -1
  54. package/dist/spy-client.js +109 -31
  55. package/dist/spy-client.min.js +1 -1
  56. package/dist/spy-client.mjs +109 -31
  57. package/dist/spy-head.js +46 -34
  58. package/dist/spy-head.min.js +1 -1
  59. package/dist/spy-local-cache.js +126 -64
  60. package/dist/spy-local-cache.min.js +1 -1
  61. package/example/local-cache.html +77 -15
  62. package/package.json +4 -12
  63. package/src/head/base.ts +37 -4
  64. package/src/head/error.ts +9 -19
  65. package/src/head/whitescreen.ts +14 -14
  66. package/src/lib/huffman.ts +37 -56
  67. package/src/lib/interface.ts +5 -0
  68. package/src/spy-client-basic.ts +95 -26
  69. package/src/spy-client.ts +19 -1
  70. package/src/spy-local-cache.ts +103 -10
@@ -6,7 +6,9 @@ interface Option {
6
6
  key: string;
7
7
  maxRecordLen: number;
8
8
  interval: number;
9
- onFlush: (res: any) => void;
9
+ onFlush?: (res: any) => void;
10
+ onSave?: (res: any) => void;
11
+ onAdd?: (res: any) => boolean;
10
12
  storage: 'indexedDB' | 'localstorage' | 'empty';
11
13
  }
12
14
 
@@ -68,16 +70,78 @@ class LS extends Storage {
68
70
  }
69
71
 
70
72
  class IndexedDB extends Storage {
73
+ private readonly databaseName = 'spyLC';
74
+ private db: IDBDatabase | null = null;
75
+ private readonly setQueue: any[] = [];
76
+ private readonly getQueue: any[] = [];
77
+ constructor() {
78
+ super();
79
+
80
+ let request = window.indexedDB.open(this.databaseName);
81
+
82
+ request.onupgradeneeded = event => {
83
+ // @ts-ignore ts默认的EventTarget类型有问题,先ignore掉
84
+ this.db = event.target && event.target.result;
85
+ if (this.db && !this.db.objectStoreNames.contains(this.databaseName)) {
86
+ this.db.createObjectStore(this.databaseName, {keyPath: 'key'});
87
+ }
88
+ this.runQueueTask();
89
+ };
90
+
91
+ request.onsuccess = () => {
92
+ this.db = request.result;
93
+ this.runQueueTask();
94
+ };
95
+
96
+ }
71
97
  static isSupport() {
72
98
  return !!window.indexedDB;
73
99
  }
74
100
 
75
101
  set(key: string, value: any) {
102
+ // 因为indexDB是异步初始化的,如果set的时候未初始化,先缓存之后再执行set
103
+ if (!this.db) {
104
+ this.setQueue.push({
105
+ key,
106
+ value,
107
+ });
108
+ return;
109
+ }
110
+ this.db.transaction([this.databaseName], 'readwrite')
111
+ .objectStore(this.databaseName)
112
+ .add({
113
+ key,
114
+ value,
115
+ });
76
116
 
77
117
  }
78
118
 
79
119
  get(key: string, cb: any) {
120
+ // 因为indexDB是异步初始化的,如果get的时候未初始化,先缓存之后再执行get
121
+ if (!this.db) {
122
+ this.getQueue.push({
123
+ key,
124
+ cb,
125
+ });
126
+ return;
127
+ }
128
+ const transaction = this.db.transaction([this.databaseName]);
129
+ const objectStore = transaction.objectStore(this.databaseName);
130
+ const request = objectStore.get(key);
80
131
 
132
+ request.onsuccess = () => {
133
+ const result = request.result ? request.result : {value: ''};
134
+ cb(result.value);
135
+ };
136
+ }
137
+
138
+ private runQueueTask() {
139
+ this.setQueue.forEach(ele => {
140
+ this.set(ele.key, ele.value);
141
+ });
142
+ this.getQueue.forEach(ele => {
143
+ this.get(ele.key, ele.cb);
144
+ });
81
145
  }
82
146
  }
83
147
 
@@ -217,6 +281,10 @@ export default class SpyLocalCache {
217
281
  interval: 500,
218
282
  maxRecordLen: 30,
219
283
  onFlush: () => {},
284
+ onSave: () => {},
285
+ onAdd: () => {
286
+ return true;
287
+ },
220
288
  storage: IndexedDB.isSupport()
221
289
  ? 'indexedDB'
222
290
  : LS.isSupport()
@@ -255,6 +323,12 @@ export default class SpyLocalCache {
255
323
  }
256
324
 
257
325
  addLog(info: any) {
326
+ if (this.option.onAdd) {
327
+ if (!this.option.onAdd(info)) {
328
+ return;
329
+ }
330
+ }
331
+
258
332
  info = JSON.stringify(info);
259
333
 
260
334
  this.tmpList.push(info as string);
@@ -300,23 +374,42 @@ export default class SpyLocalCache {
300
374
  }
301
375
 
302
376
  save() {
377
+ const st = Date.now();
303
378
  this.getData((list: any[]) => {
304
379
  // 只保留最近的maxRecordLen条日志
305
380
  const reserveLen = this.option.maxRecordLen - 1;
306
381
  const originList = list.length > reserveLen ? list.slice(list.length - reserveLen, list.length) : list;
307
- let content = originList.concat(this.tmpList).join('\n');
308
- let data = this.zip(content);
309
- let codesStr = '';
310
- if (data.codes) {
311
- codesStr = JSON.stringify(data.codes);
312
- this.storage.set(this.option.key + 'Codes', codesStr);
382
+ let newList = originList.concat(this.tmpList);
383
+ let content = newList.join('\n');
384
+ let error: Error | null = null;
385
+ let len = 0;
386
+ try {
387
+ let data = this.zip(content);
388
+ let codesStr = '';
389
+ if (data.codes) {
390
+ codesStr = JSON.stringify(data.codes);
391
+ this.storage.set(this.option.key + 'Codes', codesStr);
392
+ }
393
+ else {
394
+ this.storage.rm(this.option.key + 'Codes');
395
+ }
396
+
397
+ this.storage.set(this.option.key, data.result);
398
+ len = data.result.length;
313
399
  }
314
- else {
315
- this.storage.rm(this.option.key + 'Codes');
400
+ catch (e) {
401
+ error = e;
402
+ console.error(e);
316
403
  }
317
- this.storage.set(this.option.key, data.result);
318
404
 
319
405
  this.tmpList = [];
406
+
407
+ this.option.onSave && this.option.onSave({
408
+ cost: Date.now() - st,
409
+ length: len / 1024,
410
+ list: newList,
411
+ error: error,
412
+ });
320
413
  });
321
414
  }
322
415