@qiu_jun/monitors 0.0.7 → 0.0.8

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/dist/monitor.js CHANGED
@@ -1,218 +1,171 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const exception_1 = require("./exception");
4
- const modules_1 = require("./modules");
5
- const index_1 = require("./utils/index");
6
- const toExcEvent_1 = require("./utils/toExcEvent");
7
- const replay_1 = require("./replay");
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: !0
5
+ });
6
+
7
+ const exception_1 = require("./exception"), modules_1 = require("./modules"), index_1 = require("./utils/index"), toExcEvent_1 = require("./utils/toExcEvent"), replay_1 = require("./replay");
8
+
8
9
  class Monitor {
9
- constructor(options) {
10
- var _a, _b;
11
- /** 异常平台上报地址 */
12
- Object.defineProperty(this, "endpoint", {
13
- enumerable: true,
14
- configurable: true,
15
- writable: true,
16
- value: void 0
17
- });
18
- /** 接入项目 projectKey */
19
- Object.defineProperty(this, "projectKey", {
20
- enumerable: true,
21
- configurable: true,
22
- writable: true,
23
- value: void 0
24
- });
25
- Object.defineProperty(this, "timeoutDuration", {
26
- enumerable: true,
27
- configurable: true,
28
- writable: true,
29
- value: void 0
30
- });
31
- Object.defineProperty(this, "errorQueue", {
32
- enumerable: true,
33
- configurable: true,
34
- writable: true,
35
- value: void 0
36
- });
37
- Object.defineProperty(this, "requestQueue", {
38
- enumerable: true,
39
- configurable: true,
40
- writable: true,
41
- value: void 0
42
- });
43
- Object.defineProperty(this, "maxQueueSize", {
44
- enumerable: true,
45
- configurable: true,
46
- writable: true,
47
- value: void 0
48
- });
49
- Object.defineProperty(this, "sendTimer", {
50
- enumerable: true,
51
- configurable: true,
52
- writable: true,
53
- value: void 0
54
- });
55
- Object.defineProperty(this, "reporter", {
56
- enumerable: true,
57
- configurable: true,
58
- writable: true,
59
- value: void 0
60
- });
61
- Object.defineProperty(this, "replay", {
62
- enumerable: true,
63
- configurable: true,
64
- writable: true,
65
- value: null
66
- });
67
- Object.defineProperty(this, "options", {
68
- enumerable: true,
69
- configurable: true,
70
- writable: true,
71
- value: void 0
72
- });
73
- Object.defineProperty(this, "originalXHR", {
74
- enumerable: true,
75
- configurable: true,
76
- writable: true,
77
- value: void 0
78
- });
79
- Object.defineProperty(this, "originalFetch", {
80
- enumerable: true,
81
- configurable: true,
82
- writable: true,
83
- value: void 0
84
- });
85
- Object.defineProperty(this, "originalOnerror", {
86
- enumerable: true,
87
- configurable: true,
88
- writable: true,
89
- value: void 0
90
- });
91
- this.options = options;
92
- this.endpoint = options.endpoint;
93
- this.projectKey = options.projectKey;
94
- this.timeoutDuration = (_a = options.timeoutDuration) !== null && _a !== void 0 ? _a : 60000;
95
- this.errorQueue = [];
96
- this.requestQueue = [];
97
- this.maxQueueSize = (_b = options.maxQueueSize) !== null && _b !== void 0 ? _b : 10;
98
- this.sendTimer = null;
99
- this.reporter = (0, exception_1.createReporter)({
100
- endpoint: options.endpoint,
101
- projectKey: options.projectKey,
102
- release: options.release,
103
- environment: options.environment,
104
- maxBatch: options.maxBatch,
105
- flushInterval: options.flushInterval,
106
- }, (0, exception_1.makeH5Transport)(options.endpoint));
107
- this.init();
108
- this.initReplay();
109
- this.originalXHR = window.XMLHttpRequest;
110
- this.originalFetch = window.fetch;
111
- this.originalOnerror = window.onerror;
112
- }
113
- init() {
114
- modules_1.onWindowErr.call(this);
115
- modules_1.onLongTask.call(this);
116
- modules_1.onPaint.call(this);
117
- modules_1.onTiming.call(this);
118
- modules_1.onXHR.call(this);
119
- }
120
- /** 回放初始化:显式开启 + 采样命中 + 回放地址可解析 */
121
- initReplay() {
122
- var _a, _b;
123
- const replayOptions = this.options.replay;
124
- if (!(replayOptions === null || replayOptions === void 0 ? void 0 : replayOptions.enabled))
125
- return;
126
- const sampleRate = (_a = replayOptions.sampleRate) !== null && _a !== void 0 ? _a : 1;
127
- if (Math.random() >= sampleRate)
128
- return;
129
- const replayEndpoint = (_b = replayOptions.replayEndpoint) !== null && _b !== void 0 ? _b : (0, replay_1.deriveReplayEndpoint)(this.endpoint);
130
- if (!replayEndpoint) {
131
- console.warn('monitor: replay.replayEndpoint 未配置且无法从 endpoint(需以 /report 结尾)推导,回放已禁用');
132
- return;
133
- }
134
- this.replay = new replay_1.ReplayController({
135
- replayEndpoint,
136
- projectKey: this.projectKey,
137
- bufferSeconds: replayOptions.bufferSeconds,
138
- maxUploadsPerSession: replayOptions.maxUploadsPerSession,
139
- minUploadIntervalMs: replayOptions.minUploadIntervalMs,
140
- maxReplayBytes: replayOptions.maxReplayBytes,
141
- captureRequest: replayOptions.captureRequest,
142
- maskTextClass: replayOptions.maskTextClass,
143
- });
144
- void this.replay.start();
145
- }
146
- /** 统一上报:转异常平台格式 → reporter 批量 POST;错误触发回放切片上传并回填 device.replayId */
147
- send(data) {
148
- var _a;
149
- const baseData = (0, index_1.getLogBaseData)();
150
- const events = (0, toExcEvent_1.toExcEvents)(data, baseData, this.options);
151
- const replayId = (_a = this.replay) === null || _a === void 0 ? void 0 : _a.captureIfTriggered(data);
152
- if (replayId) {
153
- for (const event of events)
154
- event.device = { ...event.device, replayId };
155
- }
156
- for (const event of events)
157
- this.reporter.capture(event);
158
- }
159
- /** 手动上报自定义事件 */
160
- capture(event) {
161
- this.reporter.capture(event);
162
- }
163
- reportRequest() {
164
- if (this.requestQueue.length === 0)
165
- return;
166
- this.send({ type: 'batchXHR', requests: [...this.requestQueue] });
167
- this.requestQueue = [];
168
- }
169
- reportError() {
170
- if (this.errorQueue.length === 0)
171
- return;
172
- this.send({ type: 'batchXHR', requests: [...this.errorQueue] });
173
- this.errorQueue = [];
174
- }
175
- captureRequest(requestInfo) {
176
- var _a;
177
- const validRequestTypes = ['xhr', 'fetch'];
178
- if (!requestInfo.type || !validRequestTypes.includes(requestInfo.type)) {
179
- console.warn(`监控系统: 无效的请求类型 ${requestInfo.type},无法上报`);
180
- return;
181
- }
182
- if ((0, toExcEvent_1.isExceptionReportUrl)(String((_a = requestInfo.url) !== null && _a !== void 0 ? _a : ''), this.endpoint))
183
- return;
184
- const requiredFields = ['url', 'method', 'duration', 'status', 'success', 'startTime'];
185
- for (const field of requiredFields) {
186
- if (requestInfo[field] === undefined) {
187
- console.warn(`监控系统: 请求信息中缺少必填字段 ${field},无法上报`);
188
- return;
189
- }
190
- }
191
- requestInfo.page = window.location.href;
192
- this.requestQueue.push(requestInfo);
193
- if (this.requestQueue.length >= this.maxQueueSize)
194
- this.reportRequest();
195
- }
196
- sendQueuedData() {
197
- if (this.errorQueue.length > 0)
198
- this.reportError();
199
- if (this.requestQueue.length > 0)
200
- this.reportRequest();
201
- }
202
- destroy() {
203
- var _a;
204
- (_a = this.replay) === null || _a === void 0 ? void 0 : _a.stop();
205
- this.sendQueuedData();
206
- this.reporter.destroy();
207
- window.XMLHttpRequest = this.originalXHR;
208
- window.fetch = this.originalFetch;
209
- window.onerror = this.originalOnerror;
210
- if (this.sendTimer) {
211
- clearInterval(this.sendTimer);
212
- this.sendTimer = null;
213
- }
214
- this.errorQueue = [];
215
- this.requestQueue = [];
216
- }
10
+ constructor(e) {
11
+ var t, r;
12
+ Object.defineProperty(this, "endpoint", {
13
+ enumerable: !0,
14
+ configurable: !0,
15
+ writable: !0,
16
+ value: void 0
17
+ }), Object.defineProperty(this, "projectKey", {
18
+ enumerable: !0,
19
+ configurable: !0,
20
+ writable: !0,
21
+ value: void 0
22
+ }), Object.defineProperty(this, "timeoutDuration", {
23
+ enumerable: !0,
24
+ configurable: !0,
25
+ writable: !0,
26
+ value: void 0
27
+ }), Object.defineProperty(this, "errorQueue", {
28
+ enumerable: !0,
29
+ configurable: !0,
30
+ writable: !0,
31
+ value: void 0
32
+ }), Object.defineProperty(this, "requestQueue", {
33
+ enumerable: !0,
34
+ configurable: !0,
35
+ writable: !0,
36
+ value: void 0
37
+ }), Object.defineProperty(this, "maxQueueSize", {
38
+ enumerable: !0,
39
+ configurable: !0,
40
+ writable: !0,
41
+ value: void 0
42
+ }), Object.defineProperty(this, "sendTimer", {
43
+ enumerable: !0,
44
+ configurable: !0,
45
+ writable: !0,
46
+ value: void 0
47
+ }), Object.defineProperty(this, "reporter", {
48
+ enumerable: !0,
49
+ configurable: !0,
50
+ writable: !0,
51
+ value: void 0
52
+ }), Object.defineProperty(this, "replay", {
53
+ enumerable: !0,
54
+ configurable: !0,
55
+ writable: !0,
56
+ value: null
57
+ }), Object.defineProperty(this, "options", {
58
+ enumerable: !0,
59
+ configurable: !0,
60
+ writable: !0,
61
+ value: void 0
62
+ }), Object.defineProperty(this, "errorSentAt", {
63
+ enumerable: !0,
64
+ configurable: !0,
65
+ writable: !0,
66
+ value: new Map
67
+ }), Object.defineProperty(this, "originalXHR", {
68
+ enumerable: !0,
69
+ configurable: !0,
70
+ writable: !0,
71
+ value: void 0
72
+ }), Object.defineProperty(this, "originalFetch", {
73
+ enumerable: !0,
74
+ configurable: !0,
75
+ writable: !0,
76
+ value: void 0
77
+ }), Object.defineProperty(this, "originalOnerror", {
78
+ enumerable: !0,
79
+ configurable: !0,
80
+ writable: !0,
81
+ value: void 0
82
+ }), this.options = e, this.endpoint = e.endpoint, this.projectKey = e.projectKey,
83
+ this.timeoutDuration = null !== (t = e.timeoutDuration) && void 0 !== t ? t : 6e4,
84
+ this.errorQueue = [], this.requestQueue = [], this.maxQueueSize = null !== (r = e.maxQueueSize) && void 0 !== r ? r : 10,
85
+ this.sendTimer = null, this.reporter = (0, exception_1.createReporter)({
86
+ endpoint: e.endpoint,
87
+ projectKey: e.projectKey,
88
+ release: e.release,
89
+ environment: e.environment,
90
+ maxBatch: e.maxBatch,
91
+ flushInterval: e.flushInterval
92
+ }, (0, exception_1.makeH5Transport)(e.endpoint)), this.init(), this.initReplay(),
93
+ this.originalXHR = window.XMLHttpRequest, this.originalFetch = window.fetch, this.originalOnerror = window.onerror;
94
+ }
95
+ init() {
96
+ modules_1.onWindowErr.call(this), modules_1.onLongTask.call(this), modules_1.onPaint.call(this),
97
+ modules_1.onTiming.call(this), modules_1.onXHR.call(this);
98
+ }
99
+ initReplay() {
100
+ var e, t;
101
+ const r = this.options.replay;
102
+ if (!(null == r ? void 0 : r.enabled)) return;
103
+ const i = null !== (e = r.sampleRate) && void 0 !== e ? e : 1;
104
+ if (Math.random() >= i) return;
105
+ const o = null !== (t = r.replayEndpoint) && void 0 !== t ? t : (0, replay_1.deriveReplayEndpoint)(this.endpoint);
106
+ o && (this.replay = new replay_1.ReplayController({
107
+ replayEndpoint: o,
108
+ projectKey: this.projectKey,
109
+ bufferSeconds: r.bufferSeconds,
110
+ maxUploadsPerSession: r.maxUploadsPerSession,
111
+ minUploadIntervalMs: r.minUploadIntervalMs,
112
+ maxReplayBytes: r.maxReplayBytes,
113
+ captureRequest: r.captureRequest,
114
+ maskTextClass: r.maskTextClass
115
+ }), this.replay.start());
116
+ }
117
+ isDuplicateError(e) {
118
+ var t, r;
119
+ const i = null !== (t = this.options.errorDedupeMs) && void 0 !== t ? t : 0;
120
+ if (i <= 0 || "error" !== e.type) return !1;
121
+ const o = `${e.errorType}|${e.message}|${null !== (r = e.filename) && void 0 !== r ? r : ""}`, n = Date.now(), s = this.errorSentAt.get(o);
122
+ if (void 0 !== s && n - s < i) return !0;
123
+ if (this.errorSentAt.size >= 100) for (const [e, t] of this.errorSentAt) n - t >= i && this.errorSentAt.delete(e);
124
+ return this.errorSentAt.set(o, n), !1;
125
+ }
126
+ send(e) {
127
+ var t;
128
+ if (this.isDuplicateError(e)) return;
129
+ const r = (0, index_1.getLogBaseData)(), i = (0, toExcEvent_1.toExcEvents)(e, r, this.options), o = null === (t = this.replay) || void 0 === t ? void 0 : t.captureIfTriggered(e);
130
+ if (o) for (const e of i) e.device = {
131
+ ...e.device,
132
+ replayId: o
133
+ };
134
+ for (const e of i) this.reporter.capture(e);
135
+ }
136
+ capture(e) {
137
+ this.reporter.capture(e);
138
+ }
139
+ reportRequest() {
140
+ 0 !== this.requestQueue.length && (this.send({
141
+ type: "batchXHR",
142
+ requests: [ ...this.requestQueue ]
143
+ }), this.requestQueue = []);
144
+ }
145
+ reportError() {
146
+ 0 !== this.errorQueue.length && (this.send({
147
+ type: "batchXHR",
148
+ requests: [ ...this.errorQueue ]
149
+ }), this.errorQueue = []);
150
+ }
151
+ captureRequest(e) {
152
+ var t;
153
+ if (!e.type || ![ "xhr", "fetch" ].includes(e.type)) return;
154
+ if ((0, toExcEvent_1.isExceptionReportUrl)(String(null !== (t = e.url) && void 0 !== t ? t : ""), this.endpoint)) return;
155
+ const r = [ "url", "method", "duration", "status", "success", "startTime" ];
156
+ for (const t of r) if (void 0 === e[t]) return;
157
+ e.page = window.location.href, this.requestQueue.push(e), this.requestQueue.length >= this.maxQueueSize && this.reportRequest();
158
+ }
159
+ sendQueuedData() {
160
+ this.errorQueue.length > 0 && this.reportError(), this.requestQueue.length > 0 && this.reportRequest();
161
+ }
162
+ destroy() {
163
+ var e;
164
+ null === (e = this.replay) || void 0 === e || e.stop(), this.sendQueuedData(), this.reporter.destroy(),
165
+ window.XMLHttpRequest = this.originalXHR, window.fetch = this.originalFetch, window.onerror = this.originalOnerror,
166
+ this.sendTimer && (clearInterval(this.sendTimer), this.sendTimer = null), this.errorQueue = [],
167
+ this.requestQueue = [], this.errorSentAt.clear();
168
+ }
217
169
  }
218
- exports.default = Monitor;
170
+
171
+ exports.default = Monitor;