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