sobey-monitor-sdk 1.1.1 → 1.1.3

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.
@@ -9,7 +9,6 @@ declare class ConfigManager {
9
9
  private config;
10
10
  /**
11
11
  * 初始化配置
12
- * @param skipValidation 是否跳过必填校验(用于远程配置已获取 appId/dsn 的场景)
13
12
  */
14
13
  init(userConfig: MonitorConfig, skipValidation?: boolean): void;
15
14
  /**
@@ -43,7 +43,7 @@ export interface ErrorBoundaryState {
43
43
  * );
44
44
  * ```
45
45
  */
46
- export declare function createReactErrorBoundary(React: any, config: MonitorConfig): any;
46
+ export declare function createReactErrorBoundary(React: any, config?: MonitorConfig): any;
47
47
  /**
48
48
  * React Hook: 用于手动上报错误
49
49
  *
@@ -16,6 +16,6 @@ import type { MonitorConfig } from '../types';
16
16
  * ```
17
17
  */
18
18
  export declare const VueMonitorPlugin: {
19
- install(app: any, options: MonitorConfig): void;
19
+ install(app: any, options?: MonitorConfig): void;
20
20
  };
21
21
  export default VueMonitorPlugin;
package/dist/index.cjs.js CHANGED
@@ -49,27 +49,14 @@ class ConfigManager {
49
49
  }
50
50
  /**
51
51
  * 初始化配置
52
- * @param skipValidation 是否跳过必填校验(用于远程配置已获取 appId/dsn 的场景)
53
52
  */
54
53
  init(userConfig, skipValidation = false) {
55
54
  if (!skipValidation) {
56
- // 如果没有 configUrl,则 appId 和 dsn 是必填的
57
- if (!userConfig.configUrl) {
58
- if (!userConfig.appId) {
59
- throw new Error('[Monitor] appId is required when configUrl is not provided');
60
- }
61
- if (!userConfig.dsn) {
62
- throw new Error('[Monitor] dsn is required when configUrl is not provided');
63
- }
64
- }
65
- }
66
- else {
67
- // 远程配置模式下,校验合并后的配置
68
55
  if (!userConfig.appId) {
69
- throw new Error('[Monitor] appId is required (not found in remote config)');
56
+ throw new Error('[Monitor] appId is required');
70
57
  }
71
58
  if (!userConfig.dsn) {
72
- throw new Error('[Monitor] dsn is required (not found in remote config)');
59
+ throw new Error('[Monitor] dsn is required');
73
60
  }
74
61
  }
75
62
  this.config = this.mergeConfig(DEFAULT_CONFIG, userConfig);
@@ -293,6 +280,13 @@ class Sender {
293
280
  doSend(data) {
294
281
  const cfg = config.get();
295
282
  const dsn = cfg.dsn;
283
+ // dsn 未配置,跳过发送
284
+ if (!dsn) {
285
+ if (cfg.debug) {
286
+ console.warn('[Monitor] dsn not configured, skip sending');
287
+ }
288
+ return;
289
+ }
296
290
  const payload = JSON.stringify(data);
297
291
  // 优先使用 sendBeacon(更可靠、异步、不阻塞页面)
298
292
  if (supportsSendBeacon()) {
@@ -391,13 +385,63 @@ const SDK_VERSION$1 = '1.0.0';
391
385
  * 上报器类
392
386
  */
393
387
  class Reporter {
388
+ constructor() {
389
+ /** SDK 是否就绪 */
390
+ this.ready = false;
391
+ /** 早期数据缓存队列(SDK 未就绪时缓存) */
392
+ this.earlyBuffer = [];
393
+ /** 最大缓存数量 */
394
+ this.maxEarlyBufferSize = 50;
395
+ }
396
+ /**
397
+ * 设置 SDK 就绪状态
398
+ */
399
+ setReady(ready) {
400
+ this.ready = ready;
401
+ // 就绪后发送缓存的早期数据
402
+ if (ready && this.earlyBuffer.length > 0) {
403
+ console.log(`[Monitor] Flushing ${this.earlyBuffer.length} early buffered items`);
404
+ this.flushEarlyBuffer();
405
+ }
406
+ }
407
+ /**
408
+ * 发送早期缓存的数据
409
+ */
410
+ flushEarlyBuffer() {
411
+ while (this.earlyBuffer.length > 0) {
412
+ const item = this.earlyBuffer.shift();
413
+ if (!item)
414
+ continue;
415
+ switch (item.type) {
416
+ case 'error':
417
+ this.reportError(item.data);
418
+ break;
419
+ case 'performance':
420
+ this.reportPerformance(item.data);
421
+ break;
422
+ case 'behavior':
423
+ this.reportBehavior(item.data);
424
+ break;
425
+ }
426
+ }
427
+ }
428
+ /**
429
+ * 缓存早期数据
430
+ */
431
+ bufferEarly(type, data) {
432
+ if (this.earlyBuffer.length >= this.maxEarlyBufferSize) {
433
+ // 缓存已满,丢弃最早的数据
434
+ this.earlyBuffer.shift();
435
+ }
436
+ this.earlyBuffer.push({ type, data });
437
+ }
394
438
  /**
395
439
  * 构建基础数据
396
440
  */
397
441
  buildBaseData() {
398
442
  const cfg = config.get();
399
443
  return {
400
- appId: cfg.appId,
444
+ appId: cfg.appId || '',
401
445
  userId: cfg.user?.userId,
402
446
  sessionId: context.getSessionId(),
403
447
  pageUrl: getPageUrl(),
@@ -418,6 +462,11 @@ class Reporter {
418
462
  * 上报错误
419
463
  */
420
464
  reportError(data) {
465
+ // SDK 未就绪时缓存数据
466
+ if (!this.ready) {
467
+ this.bufferEarly('error', data);
468
+ return;
469
+ }
421
470
  if (!this.shouldSample('error'))
422
471
  return;
423
472
  const cfg = config.get();
@@ -439,6 +488,11 @@ class Reporter {
439
488
  * 上报性能
440
489
  */
441
490
  reportPerformance(data) {
491
+ // SDK 未就绪时缓存数据
492
+ if (!this.ready) {
493
+ this.bufferEarly('performance', data);
494
+ return;
495
+ }
442
496
  if (!this.shouldSample('performance'))
443
497
  return;
444
498
  const reportData = {
@@ -451,6 +505,11 @@ class Reporter {
451
505
  * 上报行为
452
506
  */
453
507
  reportBehavior(data) {
508
+ // SDK 未就绪时缓存数据
509
+ if (!this.ready) {
510
+ this.bufferEarly('behavior', data);
511
+ return;
512
+ }
454
513
  if (!this.shouldSample('behavior'))
455
514
  return;
456
515
  const reportData = {
@@ -1439,12 +1498,8 @@ function installBehaviorMonitor() {
1439
1498
  * ```
1440
1499
  */
1441
1500
  const VueMonitorPlugin = {
1442
- install(app, options) {
1443
- if (!options || !options.appId || !options.dsn) {
1444
- console.warn('[Monitor] VueMonitorPlugin requires appId and dsn in options');
1445
- return;
1446
- }
1447
- // 初始化 SDK(如果尚未初始化)
1501
+ install(app, options = {}) {
1502
+ // 初始化 SDK(支持空配置,后续通过 updateConfig 激活)
1448
1503
  try {
1449
1504
  monitor.init(options);
1450
1505
  }
@@ -1540,17 +1595,8 @@ const VueMonitorPlugin = {
1540
1595
  * );
1541
1596
  * ```
1542
1597
  */
1543
- function createReactErrorBoundary(React, config) {
1544
- if (!config || !config.appId || !config.dsn) {
1545
- console.warn('[Monitor] createReactErrorBoundary requires appId and dsn in config');
1546
- // 返回一个空的组件
1547
- return class EmptyBoundary extends React.Component {
1548
- render() {
1549
- return this.props.children;
1550
- }
1551
- };
1552
- }
1553
- // 初始化 SDK(如果尚未初始化)
1598
+ function createReactErrorBoundary(React, config = {}) {
1599
+ // 初始化 SDK(支持空配置,后续通过 updateConfig 激活)
1554
1600
  try {
1555
1601
  monitor.init(config);
1556
1602
  }
@@ -1646,64 +1692,131 @@ const SDK_VERSION = '1.0.0';
1646
1692
  */
1647
1693
  class MonitorSDK {
1648
1694
  constructor() {
1695
+ /** SDK 是否已初始化(基础初始化,可能还没有完整配置) */
1649
1696
  this.initialized = false;
1650
- this.initializing = false;
1697
+ /** SDK 是否已就绪(配置完整,可以上报数据) */
1698
+ this.ready = false;
1699
+ /** 是否正在加载远程配置 */
1700
+ this.loading = false;
1701
+ /** 监控模块是否已安装 */
1702
+ this.monitorsInstalled = false;
1651
1703
  }
1652
1704
  /**
1653
1705
  * 初始化 SDK
1654
- * 如果配置了 configUrl,将先从远程获取配置(仅获取一次)
1655
- * 无需 await,SDK 内部会自动处理异步逻辑
1706
+ * 支持空配置初始化,后续通过 updateConfig 传入 configUrl 完成配置
1656
1707
  */
1657
- init(userConfig) {
1658
- if (this.initialized || this.initializing) {
1659
- if (this.initialized) {
1660
- console.warn('[Monitor] SDK already initialized');
1661
- }
1708
+ init(userConfig = {}) {
1709
+ if (this.initialized) {
1710
+ console.warn('[Monitor] SDK already initialized');
1662
1711
  return;
1663
1712
  }
1664
- // 如果配置了 configUrl,异步获取远程配置后再初始化
1713
+ // 如果配置了 configUrl,异步获取远程配置
1665
1714
  if (userConfig.configUrl) {
1666
- this.initializing = true;
1715
+ this.loading = true;
1716
+ this.initBasic(userConfig);
1667
1717
  this.fetchRemoteConfig(userConfig.configUrl)
1668
1718
  .then((remoteConfig) => {
1669
- // 远程配置作为基础,用户本地配置覆盖远程配置
1670
1719
  const finalConfig = this.mergeUserConfig(remoteConfig, userConfig);
1671
1720
  if (userConfig.debug) {
1672
1721
  console.log('[Monitor] Remote config loaded:', remoteConfig);
1673
1722
  }
1674
- this.doInit(finalConfig, true);
1723
+ this.activate(finalConfig);
1675
1724
  })
1676
1725
  .catch((error) => {
1677
- this.initializing = false;
1726
+ this.loading = false;
1678
1727
  console.error('[Monitor] Failed to fetch remote config:', error);
1679
1728
  });
1680
1729
  }
1730
+ else if (userConfig.appId && userConfig.dsn) {
1731
+ // 有完整配置,直接激活
1732
+ this.initBasic(userConfig);
1733
+ this.activate(userConfig);
1734
+ }
1681
1735
  else {
1682
- // 没有 configUrl,直接同步初始化
1683
- this.doInit(userConfig, false);
1736
+ // 空配置或不完整配置,只做基础初始化,等待后续 updateConfig
1737
+ this.initBasic(userConfig);
1738
+ console.log('[Monitor] SDK initialized in pending mode. Call updateConfig({configUrl}) to activate.');
1684
1739
  }
1685
1740
  }
1686
1741
  /**
1687
- * 执行实际的初始化逻辑
1742
+ * 基础初始化(安装监控模块,但不开始上报)
1688
1743
  */
1689
- doInit(finalConfig, isRemoteConfig) {
1690
- // 初始化配置
1691
- config.init({
1692
- ...finalConfig,
1693
- version: finalConfig.version || SDK_VERSION,
1694
- }, isRemoteConfig);
1744
+ initBasic(userConfig) {
1695
1745
  // 初始化上下文
1696
- const maxBreadcrumbs = config.get().behavior?.maxBreadcrumbs || 20;
1746
+ const maxBreadcrumbs = userConfig.behavior?.maxBreadcrumbs || 20;
1697
1747
  context.init(maxBreadcrumbs);
1748
+ // 安装监控模块(早期捕获的数据会被缓存)
1749
+ if (!this.monitorsInstalled) {
1750
+ installErrorHandlers();
1751
+ installPerformanceMonitor();
1752
+ installBehaviorMonitor();
1753
+ this.monitorsInstalled = true;
1754
+ }
1698
1755
  this.initialized = true;
1699
- this.initializing = false;
1756
+ if (userConfig.debug) {
1757
+ console.log('[Monitor] SDK basic initialized (monitors installed, waiting for config)');
1758
+ }
1759
+ }
1760
+ /**
1761
+ * 激活 SDK(配置完整后调用)
1762
+ */
1763
+ activate(finalConfig) {
1764
+ // 初始化配置管理器
1765
+ config.init({
1766
+ ...finalConfig,
1767
+ version: finalConfig.version || SDK_VERSION,
1768
+ }, true);
1769
+ this.ready = true;
1770
+ this.loading = false;
1771
+ // 通知 reporter 可以开始上报(会自动发送缓存的早期数据)
1772
+ reporter.setReady(true);
1700
1773
  if (config.get().debug) {
1701
- console.log('[Monitor] SDK initialized', config.get());
1774
+ console.log('[Monitor] SDK activated with config:', config.get());
1775
+ }
1776
+ }
1777
+ /**
1778
+ * 动态更新配置
1779
+ * 如果传入 configUrl,会从远程获取配置并激活 SDK
1780
+ */
1781
+ updateConfig(partialConfig) {
1782
+ // 如果传入了 configUrl,从远程获取配置
1783
+ if (partialConfig.configUrl) {
1784
+ console.log('[Monitor] Fetching remote config from:', partialConfig.configUrl);
1785
+ this.loading = true;
1786
+ this.fetchRemoteConfig(partialConfig.configUrl)
1787
+ .then((remoteConfig) => {
1788
+ const mergedConfig = this.mergeUserConfig(remoteConfig, partialConfig);
1789
+ // 移除 configUrl
1790
+ delete mergedConfig.configUrl;
1791
+ if (this.ready) {
1792
+ // 已就绪,更新配置
1793
+ config.update(mergedConfig);
1794
+ this.loading = false;
1795
+ if (config.get().debug) {
1796
+ console.log('[Monitor] Config updated from remote:', mergedConfig);
1797
+ }
1798
+ }
1799
+ else {
1800
+ // 未就绪,激活 SDK
1801
+ this.activate(mergedConfig);
1802
+ }
1803
+ })
1804
+ .catch((error) => {
1805
+ this.loading = false;
1806
+ console.error('[Monitor] Failed to fetch remote config:', error);
1807
+ });
1808
+ }
1809
+ else {
1810
+ // 普通配置更新
1811
+ if (!this.ready) {
1812
+ console.warn('[Monitor] SDK not ready. Please provide configUrl or (appId + dsn) first.');
1813
+ return;
1814
+ }
1815
+ config.update(partialConfig);
1816
+ if (config.get().debug) {
1817
+ console.log('[Monitor] Config updated', partialConfig);
1818
+ }
1702
1819
  }
1703
- // 安装监控模块
1704
- installErrorHandlers();
1705
- installPerformanceMonitor();
1706
- installBehaviorMonitor();
1707
1820
  }
1708
1821
  /**
1709
1822
  * 从远程获取配置
@@ -1729,7 +1842,7 @@ class MonitorSDK {
1729
1842
  if (Object.prototype.hasOwnProperty.call(local, key)) {
1730
1843
  const localValue = local[key];
1731
1844
  const remoteValue = remote[key];
1732
- // 跳过 configUrl,不需要传递给最终配置
1845
+ // 跳过 configUrl
1733
1846
  if (key === 'configUrl')
1734
1847
  continue;
1735
1848
  if (localValue !== null &&
@@ -1750,55 +1863,33 @@ class MonitorSDK {
1750
1863
  return result;
1751
1864
  }
1752
1865
  /**
1753
- * 设置用户信息
1866
+ * 检查 SDK 是否就绪
1754
1867
  */
1755
- setUser(user) {
1756
- this.checkInit();
1757
- config.setUser(user);
1868
+ isReady() {
1869
+ return this.ready;
1758
1870
  }
1759
1871
  /**
1760
- * 动态更新配置
1761
- * @description 可以在运行时更新 SDK 配置,如 debug、sampling、report 等
1762
- * 如果传入 configUrl,会从远程获取配置并合并
1872
+ * 设置用户信息
1763
1873
  */
1764
- updateConfig(partialConfig) {
1765
- this.checkInit();
1766
- // 如果传入了 configUrl,从远程获取配置并更新
1767
- if (partialConfig.configUrl) {
1768
- this.fetchRemoteConfig(partialConfig.configUrl)
1769
- .then((remoteConfig) => {
1770
- // 远程配置作为基础,传入的其他本地配置覆盖远程配置
1771
- const mergedConfig = this.mergeUserConfig(remoteConfig, partialConfig);
1772
- // 移除 configUrl,不需要存到配置里
1773
- delete mergedConfig.configUrl;
1774
- config.update(mergedConfig);
1775
- if (config.get().debug) {
1776
- console.log('[Monitor] Config updated from remote:', mergedConfig);
1777
- }
1778
- })
1779
- .catch((error) => {
1780
- console.error('[Monitor] Failed to fetch remote config:', error);
1781
- });
1782
- }
1783
- else {
1784
- config.update(partialConfig);
1785
- if (config.get().debug) {
1786
- console.log('[Monitor] Config updated', partialConfig);
1787
- }
1788
- }
1874
+ setUser(user) {
1875
+ if (!this.ready)
1876
+ return;
1877
+ config.setUser(user);
1789
1878
  }
1790
1879
  /**
1791
1880
  * 获取当前配置
1792
1881
  */
1793
1882
  getConfig() {
1794
- this.checkInit();
1883
+ if (!this.ready)
1884
+ return null;
1795
1885
  return config.get();
1796
1886
  }
1797
1887
  /**
1798
1888
  * 手动上报错误
1799
1889
  */
1800
1890
  captureError(error, extra) {
1801
- this.checkInit();
1891
+ if (!this.ready)
1892
+ return;
1802
1893
  const errorData = {
1803
1894
  type: 'js_error',
1804
1895
  message: typeof error === 'string' ? error : error.message,
@@ -1811,7 +1902,8 @@ class MonitorSDK {
1811
1902
  * 手动上报性能数据
1812
1903
  */
1813
1904
  capturePerformance(metrics) {
1814
- this.checkInit();
1905
+ if (!this.ready)
1906
+ return;
1815
1907
  reporter.reportPerformance({
1816
1908
  type: 'performance',
1817
1909
  metrics,
@@ -1821,7 +1913,8 @@ class MonitorSDK {
1821
1913
  * 手动上报行为数据
1822
1914
  */
1823
1915
  captureBehavior(action, data) {
1824
- this.checkInit();
1916
+ if (!this.ready)
1917
+ return;
1825
1918
  reporter.reportBehavior({
1826
1919
  type: 'behavior',
1827
1920
  action,
@@ -1832,14 +1925,16 @@ class MonitorSDK {
1832
1925
  * 添加面包屑
1833
1926
  */
1834
1927
  addBreadcrumb(crumb) {
1835
- this.checkInit();
1928
+ if (!this.initialized)
1929
+ return;
1836
1930
  context.addBreadcrumb(crumb);
1837
1931
  }
1838
1932
  /**
1839
1933
  * 立即发送缓冲区数据
1840
1934
  */
1841
1935
  flush() {
1842
- this.checkInit();
1936
+ if (!this.ready)
1937
+ return;
1843
1938
  reporter.flush();
1844
1939
  }
1845
1940
  /**
@@ -1848,14 +1943,6 @@ class MonitorSDK {
1848
1943
  getVersion() {
1849
1944
  return SDK_VERSION;
1850
1945
  }
1851
- /**
1852
- * 检查是否已初始化
1853
- */
1854
- checkInit() {
1855
- if (!this.initialized) {
1856
- throw new Error('[Monitor] SDK not initialized. Please call init() first.');
1857
- }
1858
- }
1859
1946
  }
1860
1947
  // 导出单例
1861
1948
  const monitor = new MonitorSDK();