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