@be-link/cls-logger 1.0.11 → 1.0.13
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/README.md +430 -68
- package/dist/ClsLoggerCore.d.ts +38 -5
- package/dist/ClsLoggerCore.d.ts.map +1 -1
- package/dist/index.esm.js +380 -62
- package/dist/index.js +380 -62
- package/dist/index.umd.js +380 -62
- package/dist/mini/ClsLogger.d.ts +6 -0
- package/dist/mini/ClsLogger.d.ts.map +1 -1
- package/dist/mini/behaviorMonitor.d.ts.map +1 -1
- package/dist/mini/deviceInfo.d.ts.map +1 -1
- package/dist/mini/errorMonitor.d.ts.map +1 -1
- package/dist/mini.esm.js +298 -49
- package/dist/mini.js +298 -49
- package/dist/types.d.ts +41 -3
- package/dist/types.d.ts.map +1 -1
- package/dist/web/deviceInfo.d.ts.map +1 -1
- package/dist/web/errorMonitor.d.ts.map +1 -1
- package/dist/web/performanceMonitor.d.ts.map +1 -1
- package/dist/web.esm.js +304 -38
- package/dist/web.js +304 -38
- package/package.json +1 -1
package/dist/web.esm.js
CHANGED
|
@@ -178,6 +178,11 @@ class ClsLoggerCore {
|
|
|
178
178
|
this.batchTimerDueAt = null;
|
|
179
179
|
this.initTs = 0;
|
|
180
180
|
this.startupDelayMs = 0;
|
|
181
|
+
this.startupMaxSize = 0; // 启动窗口内的 maxSize,0 表示使用默认计算值
|
|
182
|
+
this.useIdleCallback = false;
|
|
183
|
+
this.idleTimeout = 3000;
|
|
184
|
+
this.pendingIdleCallback = null; // requestIdleCallback 的 id
|
|
185
|
+
this.visibilityCleanup = null;
|
|
181
186
|
// 参考文档:失败缓存 + 重试
|
|
182
187
|
this.failedCacheKey = 'cls_failed_logs';
|
|
183
188
|
this.failedCacheMax = 200;
|
|
@@ -201,7 +206,7 @@ class ClsLoggerCore {
|
|
|
201
206
|
}
|
|
202
207
|
return 'browser';
|
|
203
208
|
}
|
|
204
|
-
init(options) {
|
|
209
|
+
async init(options) {
|
|
205
210
|
this.initTs = Date.now();
|
|
206
211
|
const topicId = options?.tencentCloud?.topicID ?? options?.topic_id ?? options?.topicID ?? this.topicId;
|
|
207
212
|
const endpoint = options?.tencentCloud?.endpoint ?? options?.endpoint ?? this.endpoint;
|
|
@@ -210,7 +215,7 @@ class ClsLoggerCore {
|
|
|
210
215
|
if (!topicId) {
|
|
211
216
|
// eslint-disable-next-line no-console
|
|
212
217
|
console.warn('ClsLogger.init 没有传 topicID/topic_id');
|
|
213
|
-
return;
|
|
218
|
+
return false;
|
|
214
219
|
}
|
|
215
220
|
const nextEnvType = options.envType ?? this.detectEnvType();
|
|
216
221
|
// envType/endpoint/retryTimes 变化时:重置 client(以及可能的 sdk)
|
|
@@ -247,15 +252,21 @@ class ClsLoggerCore {
|
|
|
247
252
|
this.batchMaxSize = options.batch?.maxSize ?? this.batchMaxSize;
|
|
248
253
|
this.batchIntervalMs = options.batch?.intervalMs ?? this.batchIntervalMs;
|
|
249
254
|
this.startupDelayMs = options.batch?.startupDelayMs ?? this.startupDelayMs;
|
|
255
|
+
this.useIdleCallback = options.batch?.useIdleCallback ?? this.useIdleCallback;
|
|
256
|
+
this.idleTimeout = options.batch?.idleTimeout ?? this.idleTimeout;
|
|
257
|
+
// startupMaxSize:启动窗口内的队列阈值,默认为 maxSize * 10(至少 200)
|
|
258
|
+
this.startupMaxSize = options.batch?.startupMaxSize ?? Math.max(this.batchMaxSize * 10, 200);
|
|
250
259
|
this.failedCacheKey = options.failedCacheKey ?? this.failedCacheKey;
|
|
251
260
|
this.failedCacheMax = options.failedCacheMax ?? this.failedCacheMax;
|
|
252
261
|
// 预热(避免首条日志触发 import/初始化开销)
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
262
|
+
const sdkReadyPromise = this.getInstance()
|
|
263
|
+
.then(() => true)
|
|
264
|
+
.catch(() => false);
|
|
256
265
|
if (this.enabled) {
|
|
257
266
|
// 启动时尝试发送失败缓存
|
|
258
267
|
this.flushFailed();
|
|
268
|
+
// 添加页面可见性监听(确保页面关闭时数据不丢失)
|
|
269
|
+
this.setupVisibilityListener();
|
|
259
270
|
// 初始化后立即启动请求监听
|
|
260
271
|
this.startRequestMonitor(options.requestMonitor);
|
|
261
272
|
// 初始化后立即启动错误监控/性能监控
|
|
@@ -264,6 +275,7 @@ class ClsLoggerCore {
|
|
|
264
275
|
// 初始化后立即启动行为埋点(PV/UV/点击)
|
|
265
276
|
this.startBehaviorMonitor(options.behaviorMonitor);
|
|
266
277
|
}
|
|
278
|
+
return sdkReadyPromise;
|
|
267
279
|
}
|
|
268
280
|
getBaseFields() {
|
|
269
281
|
let auto = undefined;
|
|
@@ -292,6 +304,97 @@ class ClsLoggerCore {
|
|
|
292
304
|
return auto;
|
|
293
305
|
return undefined;
|
|
294
306
|
}
|
|
307
|
+
/**
|
|
308
|
+
* 设置页面可见性监听
|
|
309
|
+
* - visibilitychange: 页面隐藏时使用 sendBeacon 发送队列
|
|
310
|
+
* - pagehide: 作为移动端 fallback
|
|
311
|
+
* - 子类可覆写此方法以实现平台特定的监听(如小程序的 wx.onAppHide)
|
|
312
|
+
*/
|
|
313
|
+
setupVisibilityListener() {
|
|
314
|
+
if (typeof document === 'undefined' || typeof window === 'undefined')
|
|
315
|
+
return;
|
|
316
|
+
// 避免重复监听
|
|
317
|
+
if (this.visibilityCleanup)
|
|
318
|
+
return;
|
|
319
|
+
const handleVisibilityChange = () => {
|
|
320
|
+
if (document.visibilityState === 'hidden') {
|
|
321
|
+
// 使用微任务延迟 flush,确保 web-vitals 等第三方库的 visibilitychange 回调先执行
|
|
322
|
+
// 这样 LCP/CLS/INP 等指标能先入队,再被 flush 发送
|
|
323
|
+
// 注意:queueMicrotask 比 setTimeout(0) 更可靠,不会被延迟太久
|
|
324
|
+
queueMicrotask(() => {
|
|
325
|
+
this.flushBatchSync();
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
const handlePageHide = () => {
|
|
330
|
+
// pagehide 不能延迟,因为浏览器可能立即关闭页面
|
|
331
|
+
// 但 pagehide 通常在 visibilitychange 之后触发,此时队列应该已经包含 web-vitals 指标
|
|
332
|
+
this.flushBatchSync();
|
|
333
|
+
};
|
|
334
|
+
document.addEventListener('visibilitychange', handleVisibilityChange);
|
|
335
|
+
window.addEventListener('pagehide', handlePageHide);
|
|
336
|
+
this.visibilityCleanup = () => {
|
|
337
|
+
document.removeEventListener('visibilitychange', handleVisibilityChange);
|
|
338
|
+
window.removeEventListener('pagehide', handlePageHide);
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* 同步发送内存队列(使用 sendBeacon)
|
|
343
|
+
* - 用于页面关闭时确保数据发送
|
|
344
|
+
* - sendBeacon 不可用时降级为缓存到 localStorage
|
|
345
|
+
*/
|
|
346
|
+
flushBatchSync() {
|
|
347
|
+
if (this.memoryQueue.length === 0)
|
|
348
|
+
return;
|
|
349
|
+
// 清除定时器
|
|
350
|
+
if (this.batchTimer) {
|
|
351
|
+
try {
|
|
352
|
+
if (this.useIdleCallback && typeof cancelIdleCallback !== 'undefined') {
|
|
353
|
+
cancelIdleCallback(this.batchTimer);
|
|
354
|
+
}
|
|
355
|
+
else {
|
|
356
|
+
clearTimeout(this.batchTimer);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
catch {
|
|
360
|
+
// ignore
|
|
361
|
+
}
|
|
362
|
+
this.batchTimer = null;
|
|
363
|
+
}
|
|
364
|
+
this.batchTimerDueAt = null;
|
|
365
|
+
const logs = [...this.memoryQueue];
|
|
366
|
+
this.memoryQueue = [];
|
|
367
|
+
// 优先使用 sendBeacon(页面关闭时可靠发送)
|
|
368
|
+
if (typeof navigator !== 'undefined' && typeof navigator.sendBeacon === 'function') {
|
|
369
|
+
try {
|
|
370
|
+
const payload = this.buildSendBeaconPayload(logs);
|
|
371
|
+
const blob = new Blob([payload], { type: 'application/json' });
|
|
372
|
+
const url = `${this.endpoint}/structuredlog?topic_id=${this.topicId}`;
|
|
373
|
+
const success = navigator.sendBeacon(url, blob);
|
|
374
|
+
if (!success) {
|
|
375
|
+
// sendBeacon 返回 false 时,降级缓存
|
|
376
|
+
this.cacheFailedReportLogs(logs);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
catch {
|
|
380
|
+
this.cacheFailedReportLogs(logs);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
else {
|
|
384
|
+
// 不支持 sendBeacon,降级缓存到 localStorage
|
|
385
|
+
this.cacheFailedReportLogs(logs);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* 构建 sendBeacon 的 payload
|
|
390
|
+
*/
|
|
391
|
+
buildSendBeaconPayload(logs) {
|
|
392
|
+
const logList = logs.map((log) => this.buildReportFields(log));
|
|
393
|
+
return JSON.stringify({
|
|
394
|
+
source: this.source,
|
|
395
|
+
logs: logList,
|
|
396
|
+
});
|
|
397
|
+
}
|
|
295
398
|
startRequestMonitor(requestMonitor) {
|
|
296
399
|
if (this.requestMonitorStarted)
|
|
297
400
|
return;
|
|
@@ -537,32 +640,75 @@ class ClsLoggerCore {
|
|
|
537
640
|
return;
|
|
538
641
|
}
|
|
539
642
|
this.memoryQueue.push(log);
|
|
540
|
-
|
|
643
|
+
const now = Date.now();
|
|
644
|
+
// 判断是否在启动合并窗口内
|
|
645
|
+
const inStartupWindow = this.startupDelayMs > 0 && now - this.initTs < this.startupDelayMs;
|
|
646
|
+
// 启动窗口内使用 startupMaxSize,正常情况使用 batchMaxSize
|
|
647
|
+
const effectiveMaxSize = inStartupWindow ? this.startupMaxSize : this.batchMaxSize;
|
|
648
|
+
if (this.memoryQueue.length >= effectiveMaxSize) {
|
|
541
649
|
void this.flushBatch();
|
|
542
650
|
return;
|
|
543
651
|
}
|
|
544
|
-
const now = Date.now();
|
|
545
652
|
const desiredDueAt = this.getDesiredBatchFlushDueAt(now);
|
|
546
653
|
const desiredDelay = Math.max(0, desiredDueAt - now);
|
|
547
654
|
if (!this.batchTimer) {
|
|
548
655
|
this.batchTimerDueAt = desiredDueAt;
|
|
656
|
+
this.scheduleFlush(desiredDelay);
|
|
657
|
+
return;
|
|
658
|
+
}
|
|
659
|
+
// 启动合并窗口内:如果当前 timer 会"更早"触发,则延后到窗口结束,尽量减少多次发送
|
|
660
|
+
if (this.batchTimerDueAt !== null && this.batchTimerDueAt < desiredDueAt) {
|
|
661
|
+
this.cancelScheduledFlush();
|
|
662
|
+
this.batchTimerDueAt = desiredDueAt;
|
|
663
|
+
this.scheduleFlush(desiredDelay);
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
/**
|
|
667
|
+
* 调度批量发送
|
|
668
|
+
* - 先使用 setTimeout 保证最小延迟(desiredDelay)
|
|
669
|
+
* - 若开启 useIdleCallback,在延迟结束后等待浏览器空闲再执行
|
|
670
|
+
*/
|
|
671
|
+
scheduleFlush(desiredDelay) {
|
|
672
|
+
if (this.useIdleCallback && typeof requestIdleCallback !== 'undefined') {
|
|
673
|
+
// 先 setTimeout 保证最小延迟,再 requestIdleCallback 在空闲时执行
|
|
674
|
+
this.batchTimer = setTimeout(() => {
|
|
675
|
+
const idleId = requestIdleCallback(() => {
|
|
676
|
+
this.pendingIdleCallback = null;
|
|
677
|
+
void this.flushBatch();
|
|
678
|
+
}, { timeout: this.idleTimeout });
|
|
679
|
+
this.pendingIdleCallback = idleId;
|
|
680
|
+
}, desiredDelay);
|
|
681
|
+
}
|
|
682
|
+
else {
|
|
549
683
|
this.batchTimer = setTimeout(() => {
|
|
550
684
|
void this.flushBatch();
|
|
551
685
|
}, desiredDelay);
|
|
552
|
-
return;
|
|
553
686
|
}
|
|
554
|
-
|
|
555
|
-
|
|
687
|
+
}
|
|
688
|
+
/**
|
|
689
|
+
* 取消已调度的批量发送
|
|
690
|
+
* - 同时清理 setTimeout 和可能的 requestIdleCallback
|
|
691
|
+
*/
|
|
692
|
+
cancelScheduledFlush() {
|
|
693
|
+
// 清理 setTimeout
|
|
694
|
+
if (this.batchTimer) {
|
|
556
695
|
try {
|
|
557
696
|
clearTimeout(this.batchTimer);
|
|
558
697
|
}
|
|
559
698
|
catch {
|
|
560
699
|
// ignore
|
|
561
700
|
}
|
|
562
|
-
this.
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
701
|
+
this.batchTimer = null;
|
|
702
|
+
}
|
|
703
|
+
// 清理可能的 pendingIdleCallback
|
|
704
|
+
if (this.pendingIdleCallback !== null && typeof cancelIdleCallback !== 'undefined') {
|
|
705
|
+
try {
|
|
706
|
+
cancelIdleCallback(this.pendingIdleCallback);
|
|
707
|
+
}
|
|
708
|
+
catch {
|
|
709
|
+
// ignore
|
|
710
|
+
}
|
|
711
|
+
this.pendingIdleCallback = null;
|
|
566
712
|
}
|
|
567
713
|
}
|
|
568
714
|
getDesiredBatchFlushDueAt(nowTs) {
|
|
@@ -575,7 +721,7 @@ class ClsLoggerCore {
|
|
|
575
721
|
}
|
|
576
722
|
return nowTs + this.batchIntervalMs;
|
|
577
723
|
}
|
|
578
|
-
info(message, data = {}) {
|
|
724
|
+
info(message, data = {}, options) {
|
|
579
725
|
let msg = '';
|
|
580
726
|
let extra = {};
|
|
581
727
|
if (message instanceof Error) {
|
|
@@ -591,9 +737,18 @@ class ClsLoggerCore {
|
|
|
591
737
|
extra = data;
|
|
592
738
|
}
|
|
593
739
|
const payload = normalizeFlatFields({ message: msg, ...extra }, 'info');
|
|
594
|
-
|
|
740
|
+
const log = { type: 'info', data: payload, timestamp: Date.now() };
|
|
741
|
+
// info 默认走批量队列,支持 immediate 选项立即发送
|
|
742
|
+
if (options?.immediate) {
|
|
743
|
+
void this.sendReportLogs([log]).catch(() => {
|
|
744
|
+
this.cacheFailedReportLogs([log]);
|
|
745
|
+
});
|
|
746
|
+
}
|
|
747
|
+
else {
|
|
748
|
+
this.report(log);
|
|
749
|
+
}
|
|
595
750
|
}
|
|
596
|
-
warn(message, data = {}) {
|
|
751
|
+
warn(message, data = {}, options) {
|
|
597
752
|
let msg = '';
|
|
598
753
|
let extra = {};
|
|
599
754
|
if (message instanceof Error) {
|
|
@@ -609,9 +764,18 @@ class ClsLoggerCore {
|
|
|
609
764
|
extra = data;
|
|
610
765
|
}
|
|
611
766
|
const payload = normalizeFlatFields({ message: msg, ...extra }, 'warn');
|
|
612
|
-
|
|
767
|
+
const log = { type: 'warn', data: payload, timestamp: Date.now() };
|
|
768
|
+
// warn 默认走批量队列,支持 immediate 选项立即发送
|
|
769
|
+
if (options?.immediate) {
|
|
770
|
+
void this.sendReportLogs([log]).catch(() => {
|
|
771
|
+
this.cacheFailedReportLogs([log]);
|
|
772
|
+
});
|
|
773
|
+
}
|
|
774
|
+
else {
|
|
775
|
+
this.report(log);
|
|
776
|
+
}
|
|
613
777
|
}
|
|
614
|
-
error(message, data = {}) {
|
|
778
|
+
error(message, data = {}, options) {
|
|
615
779
|
let msg = '';
|
|
616
780
|
let extra = {};
|
|
617
781
|
if (message instanceof Error) {
|
|
@@ -627,7 +791,17 @@ class ClsLoggerCore {
|
|
|
627
791
|
extra = data;
|
|
628
792
|
}
|
|
629
793
|
const payload = normalizeFlatFields({ message: msg, ...extra }, 'error');
|
|
630
|
-
|
|
794
|
+
const log = { type: 'error', data: payload, timestamp: Date.now() };
|
|
795
|
+
// error 默认即时上报,除非显式指定 immediate: false
|
|
796
|
+
const immediate = options?.immediate ?? true;
|
|
797
|
+
if (immediate) {
|
|
798
|
+
void this.sendReportLogs([log]).catch(() => {
|
|
799
|
+
this.cacheFailedReportLogs([log]);
|
|
800
|
+
});
|
|
801
|
+
}
|
|
802
|
+
else {
|
|
803
|
+
this.report(log);
|
|
804
|
+
}
|
|
631
805
|
}
|
|
632
806
|
track(trackType, data = {}) {
|
|
633
807
|
if (!trackType)
|
|
@@ -642,10 +816,7 @@ class ClsLoggerCore {
|
|
|
642
816
|
* 立即发送内存队列
|
|
643
817
|
*/
|
|
644
818
|
async flushBatch() {
|
|
645
|
-
|
|
646
|
-
clearTimeout(this.batchTimer);
|
|
647
|
-
this.batchTimer = null;
|
|
648
|
-
}
|
|
819
|
+
this.cancelScheduledFlush();
|
|
649
820
|
this.batchTimerDueAt = null;
|
|
650
821
|
if (this.memoryQueue.length === 0)
|
|
651
822
|
return;
|
|
@@ -751,7 +922,14 @@ class ClsLoggerCore {
|
|
|
751
922
|
// 先清空,再尝试发送
|
|
752
923
|
writeStringStorage(this.failedCacheKey, JSON.stringify([]));
|
|
753
924
|
this.memoryQueue.unshift(...logs);
|
|
754
|
-
|
|
925
|
+
// 触发定时器而非直接 flush,以尊重 startupDelayMs 配置
|
|
926
|
+
if (!this.batchTimer) {
|
|
927
|
+
const now = Date.now();
|
|
928
|
+
const desiredDueAt = this.getDesiredBatchFlushDueAt(now);
|
|
929
|
+
const desiredDelay = Math.max(0, desiredDueAt - now);
|
|
930
|
+
this.batchTimerDueAt = desiredDueAt;
|
|
931
|
+
this.scheduleFlush(desiredDelay);
|
|
932
|
+
}
|
|
755
933
|
}
|
|
756
934
|
/**
|
|
757
935
|
* 统计/计数类日志:按字段展开上报(若 data 为空默认 1)
|
|
@@ -1045,6 +1223,13 @@ function installWebRequestMonitor(report, opts = {}) {
|
|
|
1045
1223
|
const DEFAULT_MAX_TEXT = 4000;
|
|
1046
1224
|
const DEFAULT_DEDUPE_WINDOW_MS = 3000;
|
|
1047
1225
|
const DEFAULT_DEDUPE_MAX_KEYS = 200;
|
|
1226
|
+
/** 默认忽略的无意义错误信息 */
|
|
1227
|
+
const DEFAULT_IGNORE_MESSAGES = [
|
|
1228
|
+
'Script error.',
|
|
1229
|
+
'Script error',
|
|
1230
|
+
/^ResizeObserver loop/,
|
|
1231
|
+
'Permission was denied',
|
|
1232
|
+
];
|
|
1048
1233
|
function truncate$2(s, maxLen) {
|
|
1049
1234
|
if (!s)
|
|
1050
1235
|
return s;
|
|
@@ -1057,11 +1242,22 @@ function sampleHit$1(sampleRate) {
|
|
|
1057
1242
|
return false;
|
|
1058
1243
|
return Math.random() < sampleRate;
|
|
1059
1244
|
}
|
|
1245
|
+
/** 检查消息是否应该被忽略 */
|
|
1246
|
+
function shouldIgnoreMessage(message, ignorePatterns) {
|
|
1247
|
+
if (!message || ignorePatterns.length === 0)
|
|
1248
|
+
return false;
|
|
1249
|
+
return ignorePatterns.some((pattern) => {
|
|
1250
|
+
if (typeof pattern === 'string') {
|
|
1251
|
+
return message === pattern || message.includes(pattern);
|
|
1252
|
+
}
|
|
1253
|
+
return pattern.test(message);
|
|
1254
|
+
});
|
|
1255
|
+
}
|
|
1060
1256
|
function getPagePath$1() {
|
|
1061
1257
|
try {
|
|
1062
1258
|
if (typeof window === 'undefined')
|
|
1063
1259
|
return '';
|
|
1064
|
-
return window.location?.
|
|
1260
|
+
return window.location?.href ?? '';
|
|
1065
1261
|
}
|
|
1066
1262
|
catch {
|
|
1067
1263
|
return '';
|
|
@@ -1152,6 +1348,9 @@ function installBrowserErrorMonitor(report, options) {
|
|
|
1152
1348
|
};
|
|
1153
1349
|
if (event && typeof event === 'object' && 'message' in event) {
|
|
1154
1350
|
payload.message = truncate$2(String(event.message ?? ''), options.maxTextLength);
|
|
1351
|
+
// 检查是否应该忽略此错误
|
|
1352
|
+
if (shouldIgnoreMessage(String(event.message ?? ''), options.ignoreMessages))
|
|
1353
|
+
return;
|
|
1155
1354
|
payload.filename = truncate$2(String(event.filename ?? ''), 500);
|
|
1156
1355
|
payload.lineno = typeof event.lineno === 'number' ? event.lineno : undefined;
|
|
1157
1356
|
payload.colno = typeof event.colno === 'number' ? event.colno : undefined;
|
|
@@ -1190,6 +1389,9 @@ function installBrowserErrorMonitor(report, options) {
|
|
|
1190
1389
|
return;
|
|
1191
1390
|
const reason = event?.reason;
|
|
1192
1391
|
const e = normalizeErrorLike(reason, options.maxTextLength);
|
|
1392
|
+
// 检查是否应该忽略此错误
|
|
1393
|
+
if (shouldIgnoreMessage(e.message, options.ignoreMessages))
|
|
1394
|
+
return;
|
|
1193
1395
|
const payload = {
|
|
1194
1396
|
pagePath: getPagePath$1(),
|
|
1195
1397
|
source: 'unhandledrejection',
|
|
@@ -1219,6 +1421,7 @@ function installWebErrorMonitor(report, opts = {}) {
|
|
|
1219
1421
|
maxTextLength: raw.maxTextLength ?? DEFAULT_MAX_TEXT,
|
|
1220
1422
|
dedupeWindowMs: raw.dedupeWindowMs ?? DEFAULT_DEDUPE_WINDOW_MS,
|
|
1221
1423
|
dedupeMaxKeys: raw.dedupeMaxKeys ?? DEFAULT_DEDUPE_MAX_KEYS,
|
|
1424
|
+
ignoreMessages: raw.ignoreMessages ?? DEFAULT_IGNORE_MESSAGES,
|
|
1222
1425
|
};
|
|
1223
1426
|
installBrowserErrorMonitor(report, options);
|
|
1224
1427
|
}
|
|
@@ -1257,7 +1460,7 @@ function getPagePath() {
|
|
|
1257
1460
|
try {
|
|
1258
1461
|
if (typeof window === 'undefined')
|
|
1259
1462
|
return '';
|
|
1260
|
-
return window.location?.
|
|
1463
|
+
return window.location?.href ?? '';
|
|
1261
1464
|
}
|
|
1262
1465
|
catch {
|
|
1263
1466
|
return '';
|
|
@@ -1429,9 +1632,25 @@ function installBrowserPerformanceMonitor(report, options) {
|
|
|
1429
1632
|
if (!name || shouldIgnoreUrl(name, ignoreUrls))
|
|
1430
1633
|
continue;
|
|
1431
1634
|
const initiatorType = String(entry?.initiatorType ?? '');
|
|
1432
|
-
//
|
|
1635
|
+
// 关注 fetch/xhr/img/script(同时允许 css/other 但不强制)
|
|
1433
1636
|
if (!['xmlhttprequest', 'fetch', 'img', 'script', 'css'].includes(initiatorType))
|
|
1434
1637
|
continue;
|
|
1638
|
+
// 时序分解(便于定位慢在哪个阶段)
|
|
1639
|
+
const domainLookupStart = entry.domainLookupStart ?? 0;
|
|
1640
|
+
const domainLookupEnd = entry.domainLookupEnd ?? 0;
|
|
1641
|
+
const connectStart = entry.connectStart ?? 0;
|
|
1642
|
+
const connectEnd = entry.connectEnd ?? 0;
|
|
1643
|
+
const requestStart = entry.requestStart ?? 0;
|
|
1644
|
+
const responseStart = entry.responseStart ?? 0;
|
|
1645
|
+
const responseEnd = entry.responseEnd ?? 0;
|
|
1646
|
+
const dns = domainLookupEnd - domainLookupStart;
|
|
1647
|
+
const tcp = connectEnd - connectStart;
|
|
1648
|
+
const ttfb = responseStart - requestStart;
|
|
1649
|
+
const download = responseEnd - responseStart;
|
|
1650
|
+
// 缓存检测:transferSize=0 且 decodedBodySize>0 表示缓存命中
|
|
1651
|
+
const transferSize = entry.transferSize ?? 0;
|
|
1652
|
+
const decodedBodySize = entry.decodedBodySize ?? 0;
|
|
1653
|
+
const cached = transferSize === 0 && decodedBodySize > 0;
|
|
1435
1654
|
const payload = {
|
|
1436
1655
|
pagePath: getPagePath(),
|
|
1437
1656
|
metric: 'resource',
|
|
@@ -1439,16 +1658,26 @@ function installBrowserPerformanceMonitor(report, options) {
|
|
|
1439
1658
|
url: truncate$1(name, options.maxTextLength),
|
|
1440
1659
|
startTime: typeof entry?.startTime === 'number' ? entry.startTime : undefined,
|
|
1441
1660
|
duration: typeof entry?.duration === 'number' ? entry.duration : undefined,
|
|
1661
|
+
// 时序分解(跨域资源可能为 0)
|
|
1662
|
+
dns: dns > 0 ? Math.round(dns) : undefined,
|
|
1663
|
+
tcp: tcp > 0 ? Math.round(tcp) : undefined,
|
|
1664
|
+
ttfb: ttfb > 0 ? Math.round(ttfb) : undefined,
|
|
1665
|
+
download: download > 0 ? Math.round(download) : undefined,
|
|
1666
|
+
// 缓存标记
|
|
1667
|
+
cached,
|
|
1442
1668
|
};
|
|
1443
|
-
//
|
|
1444
|
-
if (
|
|
1445
|
-
payload.transferSize =
|
|
1446
|
-
if (typeof entry?.encodedBodySize === 'number')
|
|
1669
|
+
// 尺寸字段(跨域资源可能为 0)
|
|
1670
|
+
if (transferSize > 0)
|
|
1671
|
+
payload.transferSize = transferSize;
|
|
1672
|
+
if (typeof entry?.encodedBodySize === 'number' && entry.encodedBodySize > 0) {
|
|
1447
1673
|
payload.encodedBodySize = entry.encodedBodySize;
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1674
|
+
}
|
|
1675
|
+
if (decodedBodySize > 0)
|
|
1676
|
+
payload.decodedBodySize = decodedBodySize;
|
|
1677
|
+
// 协议和状态
|
|
1678
|
+
if (typeof entry?.nextHopProtocol === 'string' && entry.nextHopProtocol) {
|
|
1451
1679
|
payload.nextHopProtocol = entry.nextHopProtocol;
|
|
1680
|
+
}
|
|
1452
1681
|
if (typeof entry?.responseStatus === 'number')
|
|
1453
1682
|
payload.status = entry.responseStatus;
|
|
1454
1683
|
report(options.reportType, payload);
|
|
@@ -1526,7 +1755,7 @@ function writeUvMeta(key, meta) {
|
|
|
1526
1755
|
function getWebPagePath() {
|
|
1527
1756
|
if (typeof window === 'undefined')
|
|
1528
1757
|
return '';
|
|
1529
|
-
return window.location?.
|
|
1758
|
+
return window.location?.href || '';
|
|
1530
1759
|
}
|
|
1531
1760
|
function buildCommonUvFields(uvId, uvMeta, isFirstVisit) {
|
|
1532
1761
|
return {
|
|
@@ -1837,7 +2066,7 @@ function getBrowserDeviceInfo(options) {
|
|
|
1837
2066
|
if (options.includeNetwork) {
|
|
1838
2067
|
try {
|
|
1839
2068
|
const conn = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
|
|
1840
|
-
if (conn &&
|
|
2069
|
+
if (conn && typeof conn === 'object') {
|
|
1841
2070
|
if (typeof conn.effectiveType === 'string')
|
|
1842
2071
|
out.netEffectiveType = conn.effectiveType;
|
|
1843
2072
|
if (typeof conn.downlink === 'number')
|
|
@@ -1852,6 +2081,43 @@ function getBrowserDeviceInfo(options) {
|
|
|
1852
2081
|
// ignore
|
|
1853
2082
|
}
|
|
1854
2083
|
}
|
|
2084
|
+
if (options.includeNetworkType) {
|
|
2085
|
+
try {
|
|
2086
|
+
const conn = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
|
|
2087
|
+
if (conn && typeof conn === 'object') {
|
|
2088
|
+
const type = conn.type;
|
|
2089
|
+
const eff = conn.effectiveType;
|
|
2090
|
+
let val = '';
|
|
2091
|
+
if (type === 'wifi' || type === 'ethernet') {
|
|
2092
|
+
val = 'WIFI';
|
|
2093
|
+
}
|
|
2094
|
+
else if (type === 'cellular' || type === 'wimax') {
|
|
2095
|
+
if (eff === 'slow-2g' || eff === '2g')
|
|
2096
|
+
val = '2G';
|
|
2097
|
+
else if (eff === '3g')
|
|
2098
|
+
val = '3G';
|
|
2099
|
+
else if (eff === '4g')
|
|
2100
|
+
val = '4G';
|
|
2101
|
+
else
|
|
2102
|
+
val = '4G';
|
|
2103
|
+
}
|
|
2104
|
+
else {
|
|
2105
|
+
// type 未知或不支持,降级使用 effectiveType
|
|
2106
|
+
if (eff === 'slow-2g' || eff === '2g')
|
|
2107
|
+
val = '2G';
|
|
2108
|
+
else if (eff === '3g')
|
|
2109
|
+
val = '3G';
|
|
2110
|
+
else if (eff === '4g')
|
|
2111
|
+
val = '4G';
|
|
2112
|
+
}
|
|
2113
|
+
if (val)
|
|
2114
|
+
out.networkType = val;
|
|
2115
|
+
}
|
|
2116
|
+
}
|
|
2117
|
+
catch {
|
|
2118
|
+
// ignore
|
|
2119
|
+
}
|
|
2120
|
+
}
|
|
1855
2121
|
return out;
|
|
1856
2122
|
}
|
|
1857
2123
|
function createWebDeviceInfoBaseFields(opts) {
|
|
@@ -1881,7 +2147,7 @@ function createWebDeviceInfoBaseFields(opts) {
|
|
|
1881
2147
|
try {
|
|
1882
2148
|
const g = globalThis;
|
|
1883
2149
|
const extra = g[globalKey];
|
|
1884
|
-
if (extra &&
|
|
2150
|
+
if (extra && typeof extra === 'object')
|
|
1885
2151
|
return { ...base, ...extra };
|
|
1886
2152
|
}
|
|
1887
2153
|
catch {
|