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