sobey-monitor-sdk 1.1.9 → 1.1.11

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/index.umd.js CHANGED
@@ -420,10 +420,12 @@
420
420
  }
421
421
  /**
422
422
  * 发送早期缓存的数据(根据最新配置过滤)
423
+ * 所有数据会先累积到 sender 的缓冲区,最后统一发送一次
423
424
  */
424
425
  flushEarlyBuffer() {
425
426
  const cfg = config.get();
426
427
  let skippedCount = 0;
428
+ let processedCount = 0;
427
429
  // 全局开关检查:如果 enabled 为 false,清空缓存不上报
428
430
  if (cfg.enabled === false) {
429
431
  const totalCount = this.earlyBuffer.length;
@@ -464,6 +466,7 @@
464
466
  continue;
465
467
  }
466
468
  this.reportError(item.data);
469
+ processedCount++;
467
470
  break;
468
471
  case 'performance':
469
472
  // 检查性能监控是否启用
@@ -472,6 +475,7 @@
472
475
  continue;
473
476
  }
474
477
  this.reportPerformance(item.data);
478
+ processedCount++;
475
479
  break;
476
480
  case 'behavior':
477
481
  // 检查行为监控是否启用
@@ -490,9 +494,17 @@
490
494
  continue;
491
495
  }
492
496
  this.reportBehavior(item.data);
497
+ processedCount++;
493
498
  break;
494
499
  }
495
500
  }
501
+ // 所有早期数据已添加到 sender 缓冲区,立即统一发送
502
+ if (processedCount > 0) {
503
+ sender.flush();
504
+ if (cfg.debug) {
505
+ console.log(`[Monitor] Flushed ${processedCount} early buffered items in single batch`);
506
+ }
507
+ }
496
508
  if (skippedCount > 0 && cfg.debug) {
497
509
  console.log(`[Monitor] Filtered out ${skippedCount} early buffered items based on config`);
498
510
  }
@@ -770,12 +782,20 @@
770
782
  * 检查是否是 SDK 自身的请求
771
783
  */
772
784
  function isSdkRequest(url) {
773
- if (!config.isInitialized())
774
- return false;
775
- const cfg = config.get();
776
- if (!cfg.dsn)
777
- return false;
778
- return url.includes(cfg.dsn);
785
+ // 检查是否匹配常见的 SDK 上报路径
786
+ if (url.includes('/monitor/api/report') ||
787
+ url.includes('/monitor/api/beacon') ||
788
+ url.includes('/monitor/api/config')) {
789
+ return true;
790
+ }
791
+ // 配置已初始化时,检查 dsn
792
+ if (config.isInitialized()) {
793
+ const cfg = config.get();
794
+ if (cfg.dsn && url.includes(cfg.dsn)) {
795
+ return true;
796
+ }
797
+ }
798
+ return false;
779
799
  }
780
800
  /**
781
801
  * 获取嵌套对象的字段值
@@ -965,35 +985,39 @@
965
985
  },
966
986
  });
967
987
  }
968
- // 克隆响应以读取 body(无论成功与否都需要,用于业务错误检测)
969
- const cloned = response.clone();
970
- let responseBody;
971
- try {
972
- responseBody = await cloned.text();
973
- responseBody = responseBody.substring(0, 1000);
974
- }
975
- catch { }
976
- if (!response.ok) {
977
- // HTTP 错误
978
- reportHttpError({
979
- method,
980
- url,
981
- status,
982
- duration,
983
- requestBody,
984
- responseBody,
985
- });
986
- }
987
- else if (detectBusinessError(responseBody)) {
988
- // HTTP 200 但检测到业务错误
989
- reportHttpError({
990
- method,
991
- url,
992
- status,
993
- duration,
994
- requestBody,
995
- responseBody,
996
- }, true);
988
+ // 检查是否配置了业务错误规则
989
+ const hasBusinessRules = cfg?.error?.businessErrorRules && cfg.error.businessErrorRules.length > 0;
990
+ // 只有在请求失败或配置了业务规则时才读取响应体
991
+ if (!response.ok || hasBusinessRules) {
992
+ const cloned = response.clone();
993
+ let responseBody;
994
+ try {
995
+ responseBody = await cloned.text();
996
+ responseBody = responseBody.substring(0, 1000);
997
+ }
998
+ catch { }
999
+ if (!response.ok) {
1000
+ // HTTP 错误
1001
+ reportHttpError({
1002
+ method,
1003
+ url,
1004
+ status,
1005
+ duration,
1006
+ requestBody,
1007
+ responseBody,
1008
+ });
1009
+ }
1010
+ else if (hasBusinessRules && detectBusinessError(responseBody)) {
1011
+ // HTTP 200 但检测到业务错误
1012
+ reportHttpError({
1013
+ method,
1014
+ url,
1015
+ status,
1016
+ duration,
1017
+ requestBody,
1018
+ responseBody,
1019
+ }, true);
1020
+ }
997
1021
  }
998
1022
  return response;
999
1023
  }