anys-web 6.3.2 → 6.4.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "anys-web",
3
- "version": "6.3.2",
3
+ "version": "6.4.0",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -12,25 +12,27 @@
12
12
  "author": "tangshuang",
13
13
  "license": "Apache-2.0",
14
14
  "dependencies": {
15
- "anys": "^6.3.0",
16
- "anys-shared": "^6.3.0",
17
- "anys-web-plugin-identify": "^6.2.0",
18
- "anys-web-plugin-monitor-ajax": "^6.3.0",
19
- "anys-web-plugin-monitor-dom-mutation": "^6.3.2",
20
- "anys-web-plugin-monitor-error": "^6.3.1",
21
- "anys-web-plugin-monitor-input-event": "^6.3.0",
22
- "anys-web-plugin-monitor-mouse-event": "^6.3.0",
23
- "anys-web-plugin-monitor-performance": "^6.3.0",
24
- "anys-web-plugin-monitor-scroll-event": "^6.3.0",
25
- "anys-web-plugin-monitor-touch-event": "^6.3.0",
26
- "anys-web-plugin-monitor-url": "^6.3.0",
27
- "anys-web-plugin-monitor-window-activity": "^6.3.0",
28
- "anys-web-plugin-monitor-window-size": "^6.3.0",
29
- "anys-web-plugin-send-by-ajax": "^6.3.0",
30
- "anys-web-plugin-store-offline": "^6.3.0"
15
+ "anys": "^6.4.0",
16
+ "anys-plugin-store-memory": "^6.4.0",
17
+ "anys-shared": "^6.4.0",
18
+ "anys-web-plugin-identify": "^6.4.0",
19
+ "anys-web-plugin-monitor-ajax": "^6.4.0",
20
+ "anys-web-plugin-monitor-dom-mutation": "^6.4.0",
21
+ "anys-web-plugin-monitor-error": "^6.4.0",
22
+ "anys-web-plugin-monitor-input-event": "^6.4.0",
23
+ "anys-web-plugin-monitor-mouse-event": "^6.4.0",
24
+ "anys-web-plugin-monitor-performance": "^6.4.0",
25
+ "anys-web-plugin-monitor-scroll-event": "^6.4.0",
26
+ "anys-web-plugin-monitor-touch-event": "^6.4.0",
27
+ "anys-web-plugin-monitor-url": "^6.4.0",
28
+ "anys-web-plugin-monitor-window-activity": "^6.4.0",
29
+ "anys-web-plugin-monitor-window-size": "^6.4.0",
30
+ "anys-web-plugin-send-by-ajax": "^6.4.0",
31
+ "anys-web-plugin-send-by-beacon": "^6.4.0",
32
+ "anys-web-plugin-store-offline": "^6.4.0"
31
33
  },
32
34
  "publishConfig": {
33
35
  "registry": "https://registry.npmjs.org/"
34
36
  },
35
- "gitHead": "c0f2dddfe6873acf138eb88111a02435147eecb2"
37
+ "gitHead": "b917a175c4434546544756f32d7b71102d058162"
36
38
  }
package/src/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Anys } from 'anys';
2
- import { AnysTracePlugin } from './tracer-plugin.js';
2
+ import { AnysOfflineTracerPlugin } from './offline-tracer-plugin.js';
3
3
 
4
4
  class Tracer extends Anys {
5
5
  /**
@@ -16,6 +16,6 @@ export const createTracer = (options = {}) => new Tracer({
16
16
  ...options,
17
17
  plugins: {
18
18
  ...(options.plugins || {}),
19
- tracer: AnysTracePlugin,
19
+ tracer: AnysOfflineTracerPlugin,
20
20
  },
21
21
  });
@@ -0,0 +1,159 @@
1
+ import { AnysPlugin, ajaxPost, replaceUrlSearch } from 'anys-shared';
2
+ import { AnysIdentifyPlugin } from 'anys-web-plugin-identify';
3
+ import { AnysMonitorInputEventPlugin } from 'anys-web-plugin-monitor-input-event';
4
+ import { AnysMonitorMouseEventPlugin } from 'anys-web-plugin-monitor-mouse-event';
5
+ import { AnysMonitorWindowActivityPlugin } from 'anys-web-plugin-monitor-window-activity';
6
+ import { AnysMonitorUrlPlugin } from 'anys-web-plugin-monitor-url';
7
+ import { AnysMonitorTouchEventPlugin } from 'anys-web-plugin-monitor-touch-event';
8
+ import { AnysStoreOfflinePlugin } from 'anys-web-plugin-store-offline';
9
+ import { AnysMonitorDOMMutationPlugin } from 'anys-web-plugin-monitor-dom-mutation';
10
+ import { AnysMonitorWindowSizePlugin } from 'anys-web-plugin-monitor-window-size';
11
+ import { AnysMonitorScrollEventPlugin } from 'anys-web-plugin-monitor-scroll-event';
12
+
13
+ export class AnysOfflineTracerPlugin extends AnysPlugin {
14
+ static dependencies = [
15
+ AnysStoreOfflinePlugin,
16
+ AnysMonitorUrlPlugin,
17
+ AnysMonitorWindowSizePlugin,
18
+ AnysMonitorDOMMutationPlugin,
19
+ AnysIdentifyPlugin,
20
+ AnysMonitorInputEventPlugin,
21
+ AnysMonitorMouseEventPlugin,
22
+ AnysMonitorWindowActivityPlugin,
23
+ AnysMonitorTouchEventPlugin,
24
+ AnysMonitorScrollEventPlugin,
25
+ ];
26
+
27
+ constructor(anys, [offlineStore, urlMonitor, windowSizeMonitor, DOMMutationMonitor]) {
28
+ super(anys);
29
+ this.offlineStore = offlineStore;
30
+ this.urlMonitor = urlMonitor;
31
+ this.windowSizeMonitor = windowSizeMonitor;
32
+ this.DOMMutationMonitor = DOMMutationMonitor;
33
+ // cache for trace logs to be send by beacon
34
+ this.cache = {};
35
+ }
36
+
37
+ options() {
38
+ const isSupportTouch = 'ontouchend' in document;
39
+ return {
40
+ touch: isSupportTouch,
41
+ mouse: !isSupportTouch,
42
+ click: false,
43
+ mousedown: true,
44
+ mousemove: true,
45
+ mouseup: true,
46
+ autoReport: false,
47
+ reportUrl: new Error('options.reportUrl is required!'),
48
+ reportInterval: 10000,
49
+ reportParams: null,
50
+ };
51
+ }
52
+
53
+ registerAutoReport() {
54
+ const { clientId } = this.anys;
55
+ const autoReportWhenRefreshTraceId = ({ prev }) => {
56
+ // report previous trace logs
57
+ this.anys.report([
58
+ { key: 'client', value: clientId },
59
+ { key: 'trace', value: prev },
60
+ ]);
61
+
62
+ // clear cache
63
+ this.cache = {};
64
+
65
+ // record new information
66
+ this.urlMonitor.recordUrl();
67
+ this.windowSizeMonitor.recordSize();
68
+ this.DOMMutationMonitor.recordSnapshot();
69
+ };
70
+ this.anys.on('refreshTraceId', autoReportWhenRefreshTraceId);
71
+
72
+ const timer = setInterval(() => {
73
+ this.anys.report();
74
+ }, this.anys.options.reportInterval);
75
+
76
+ let isUnloaded = 0;
77
+ const sendBeaconWhenBeforeUnload = () => {
78
+ if (isUnloaded) {
79
+ return;
80
+ }
81
+ isUnloaded = 1;
82
+
83
+ const ids = Object.keys(this.cache);
84
+ const logs = { data: Object.values(this.cache), by: 'beacon' };
85
+ if (ids.length) {
86
+ this.offlineStore.remove(ids); // async may not executed
87
+ navigator.sendBeacon(this.anys.options.reportUrl, JSON.stringify(logs));
88
+ this.cache = {};
89
+ }
90
+ };
91
+ window.addEventListener('beforeunload', sendBeaconWhenBeforeUnload);
92
+ window.addEventListener('pagehide', sendBeaconWhenBeforeUnload); // 兼容微信浏览器
93
+ window.addEventListener('unload', sendBeaconWhenBeforeUnload);
94
+
95
+ const addCacheWhenWriteLog = ({ id, data }) => {
96
+ this.cache[id] = data;
97
+ };
98
+ this.anys.on('writeOfflineLog', addCacheWhenWriteLog);
99
+
100
+ return () => {
101
+ this.anys.off('refreshTraceId', autoReportWhenRefreshTraceId);
102
+ this.anys.off('writeOfflineLog', addCacheWhenWriteLog);
103
+ // @ts-ignore
104
+ clearInterval(timer);
105
+ window.removeEventListener('beforeunload', sendBeaconWhenBeforeUnload);
106
+ };
107
+ }
108
+
109
+ /**
110
+ * define read here, so that recorder will not send other plugins 'read' output
111
+ * @param {*} message
112
+ * @returns
113
+ */
114
+ read(message) {
115
+ return this.offlineStore.read(message);
116
+ }
117
+
118
+ arrange(logs) {
119
+ const groups = [];
120
+ let i = 0;
121
+
122
+ logs.forEach((item) => {
123
+ groups[i] = groups[i] || [];
124
+ groups[i].push(item);
125
+ if (groups[i].length > 200) {
126
+ i ++;
127
+ }
128
+ });
129
+
130
+ return groups;
131
+ }
132
+
133
+ send(data) {
134
+ const items = [];
135
+ const ids = [];
136
+
137
+ data.forEach((item) => {
138
+ const { _id, ...info } = item;
139
+ ids.push(_id);
140
+ items.push(info);
141
+ });
142
+
143
+ if (!items.length) {
144
+ return;
145
+ }
146
+
147
+ const { reportUrl, reportParams } = this.anys.options;
148
+ const url = reportParams ? replaceUrlSearch(reportUrl, reportParams) : reportUrl;
149
+ return ajaxPost(url, { data: items }).then(() => {
150
+ ids.forEach((id) => {
151
+ delete this.cache[id];
152
+ });
153
+ });
154
+ }
155
+
156
+ clear() {
157
+ this.cache = {};
158
+ }
159
+ }
@@ -1,18 +1,18 @@
1
- import { AnysPlugin, ajaxPost, replaceUrlSearch } from 'anys-shared';
1
+ import { AnysPlugin } from 'anys-shared';
2
2
  import { AnysIdentifyPlugin } from 'anys-web-plugin-identify';
3
3
  import { AnysMonitorInputEventPlugin } from 'anys-web-plugin-monitor-input-event';
4
4
  import { AnysMonitorMouseEventPlugin } from 'anys-web-plugin-monitor-mouse-event';
5
5
  import { AnysMonitorWindowActivityPlugin } from 'anys-web-plugin-monitor-window-activity';
6
6
  import { AnysMonitorUrlPlugin } from 'anys-web-plugin-monitor-url';
7
7
  import { AnysMonitorTouchEventPlugin } from 'anys-web-plugin-monitor-touch-event';
8
- import { AnysStoreOfflinePlugin } from 'anys-web-plugin-store-offline';
9
8
  import { AnysMonitorDOMMutationPlugin } from 'anys-web-plugin-monitor-dom-mutation';
10
9
  import { AnysMonitorWindowSizePlugin } from 'anys-web-plugin-monitor-window-size';
11
10
  import { AnysMonitorScrollEventPlugin } from 'anys-web-plugin-monitor-scroll-event';
11
+ import { AnysStoreMemoryPlugin } from 'anys-plugin-store-memory';
12
+ import { AnysSendByBeaconPlugin } from 'anys-web-plugin-send-by-beacon';
12
13
 
13
- export class AnysTracePlugin extends AnysPlugin {
14
+ export class AnysTracerPlugin extends AnysPlugin {
14
15
  static dependencies = [
15
- AnysStoreOfflinePlugin,
16
16
  AnysMonitorUrlPlugin,
17
17
  AnysMonitorWindowSizePlugin,
18
18
  AnysMonitorDOMMutationPlugin,
@@ -22,18 +22,10 @@ export class AnysTracePlugin extends AnysPlugin {
22
22
  AnysMonitorWindowActivityPlugin,
23
23
  AnysMonitorTouchEventPlugin,
24
24
  AnysMonitorScrollEventPlugin,
25
+ AnysStoreMemoryPlugin,
26
+ AnysSendByBeaconPlugin,
25
27
  ];
26
28
 
27
- constructor(anys, [offlineStore, urlMonitor, windowSizeMonitor, DOMMutationMonitor]) {
28
- super(anys);
29
- this.offlineStore = offlineStore;
30
- this.urlMonitor = urlMonitor;
31
- this.windowSizeMonitor = windowSizeMonitor;
32
- this.DOMMutationMonitor = DOMMutationMonitor;
33
- // cache for trace logs to be send by beacon
34
- this.cache = {};
35
- }
36
-
37
29
  options() {
38
30
  const isSupportTouch = 'ontouchend' in document;
39
31
  return {
@@ -43,36 +35,14 @@ export class AnysTracePlugin extends AnysPlugin {
43
35
  mousedown: true,
44
36
  mousemove: true,
45
37
  mouseup: true,
46
- autoReport: false,
38
+ autoReport: true,
47
39
  reportUrl: new Error('options.reportUrl is required!'),
48
- reportInterval: 10000,
40
+ reportInterval: 5000,
49
41
  reportParams: null,
50
42
  };
51
43
  }
52
44
 
53
45
  registerAutoReport() {
54
- const { clientId } = this.anys;
55
- const autoReportWhenRefreshTraceId = ({ prev }) => {
56
- // report previous trace logs
57
- this.anys.report([
58
- { key: 'client', value: clientId },
59
- { key: 'trace', value: prev },
60
- ]);
61
-
62
- // clear cache
63
- this.cache = {};
64
-
65
- // record new information
66
- this.urlMonitor.recordUrl();
67
- this.windowSizeMonitor.recordSize();
68
- this.DOMMutationMonitor.recordSnapshot();
69
- };
70
- this.anys.on('refreshTraceId', autoReportWhenRefreshTraceId);
71
-
72
- const timer = setInterval(() => {
73
- this.anys.report();
74
- }, this.anys.options.reportInterval);
75
-
76
46
  let isUnloaded = 0;
77
47
  const sendBeaconWhenBeforeUnload = () => {
78
48
  if (isUnloaded) {
@@ -80,80 +50,14 @@ export class AnysTracePlugin extends AnysPlugin {
80
50
  }
81
51
  isUnloaded = 1;
82
52
 
83
- const ids = Object.keys(this.cache);
84
- const logs = { data: Object.values(this.cache), by: 'beacon' };
85
- if (ids.length) {
86
- this.offlineStore.remove(ids); // async may not executed
87
- navigator.sendBeacon(this.anys.options.reportUrl, JSON.stringify(logs));
88
- this.cache = {};
89
- }
53
+ this.anys.report();
90
54
  };
91
55
  window.addEventListener('beforeunload', sendBeaconWhenBeforeUnload);
92
56
  window.addEventListener('pagehide', sendBeaconWhenBeforeUnload); // 兼容微信浏览器
93
57
  window.addEventListener('unload', sendBeaconWhenBeforeUnload);
94
58
 
95
- const addCacheWhenWriteLog = ({ id, data }) => {
96
- this.cache[id] = data;
97
- };
98
- this.anys.on('writeOfflineLog', addCacheWhenWriteLog);
99
-
100
59
  return () => {
101
- this.anys.off('refreshTraceId', autoReportWhenRefreshTraceId);
102
- this.anys.off('writeOfflineLog', addCacheWhenWriteLog);
103
- // @ts-ignore
104
- clearInterval(timer);
105
60
  window.removeEventListener('beforeunload', sendBeaconWhenBeforeUnload);
106
61
  };
107
62
  }
108
-
109
- /**
110
- * define read here, so that recorder will not send other plugins 'read' output
111
- * @param {*} message
112
- * @returns
113
- */
114
- read(message) {
115
- return this.offlineStore.read(message);
116
- }
117
-
118
- arrange(logs) {
119
- const groups = [];
120
- let i = 0;
121
-
122
- logs.forEach((item) => {
123
- groups[i] = groups[i] || [];
124
- groups[i].push(item);
125
- if (groups[i].length > 200) {
126
- i ++;
127
- }
128
- });
129
-
130
- return groups;
131
- }
132
-
133
- send(data) {
134
- const items = [];
135
- const ids = [];
136
-
137
- data.forEach((item) => {
138
- const { _id, ...info } = item;
139
- ids.push(_id);
140
- items.push(info);
141
- });
142
-
143
- if (!items.length) {
144
- return;
145
- }
146
-
147
- const { reportUrl, reportParams } = this.anys.options;
148
- const url = reportParams ? replaceUrlSearch(reportUrl, reportParams) : reportUrl;
149
- return ajaxPost(url, { data: items }).then(() => {
150
- ids.forEach((id) => {
151
- delete this.cache[id];
152
- });
153
- });
154
- }
155
-
156
- clear() {
157
- this.cache = {};
158
- }
159
63
  }