sobey-monitor-sdk 1.0.8 → 1.1.0
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/README.md +24 -2
- package/dist/core/config.d.ts +2 -1
- package/dist/index.cjs.js +101 -10
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +26 -4
- package/dist/index.esm.js +101 -10
- package/dist/index.esm.js.map +1 -1
- package/dist/index.umd.js +101 -10
- package/dist/index.umd.js.map +1 -1
- package/dist/types/index.d.ts +11 -4
- package/package.json +1 -1
package/dist/index.umd.js
CHANGED
|
@@ -51,13 +51,28 @@
|
|
|
51
51
|
}
|
|
52
52
|
/**
|
|
53
53
|
* 初始化配置
|
|
54
|
+
* @param skipValidation 是否跳过必填校验(用于远程配置已获取 appId/dsn 的场景)
|
|
54
55
|
*/
|
|
55
|
-
init(userConfig) {
|
|
56
|
-
if (!
|
|
57
|
-
|
|
56
|
+
init(userConfig, skipValidation = false) {
|
|
57
|
+
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
|
+
}
|
|
58
67
|
}
|
|
59
|
-
|
|
60
|
-
|
|
68
|
+
else {
|
|
69
|
+
// 远程配置模式下,校验合并后的配置
|
|
70
|
+
if (!userConfig.appId) {
|
|
71
|
+
throw new Error('[Monitor] appId is required (not found in remote config)');
|
|
72
|
+
}
|
|
73
|
+
if (!userConfig.dsn) {
|
|
74
|
+
throw new Error('[Monitor] dsn is required (not found in remote config)');
|
|
75
|
+
}
|
|
61
76
|
}
|
|
62
77
|
this.config = this.mergeConfig(DEFAULT_CONFIG, userConfig);
|
|
63
78
|
}
|
|
@@ -1634,24 +1649,56 @@
|
|
|
1634
1649
|
class MonitorSDK {
|
|
1635
1650
|
constructor() {
|
|
1636
1651
|
this.initialized = false;
|
|
1652
|
+
this.initializing = false;
|
|
1637
1653
|
}
|
|
1638
1654
|
/**
|
|
1639
1655
|
* 初始化 SDK
|
|
1656
|
+
* 如果配置了 configUrl,将先从远程获取配置(仅获取一次)
|
|
1657
|
+
* 无需 await,SDK 内部会自动处理异步逻辑
|
|
1640
1658
|
*/
|
|
1641
1659
|
init(userConfig) {
|
|
1642
|
-
if (this.initialized) {
|
|
1643
|
-
|
|
1660
|
+
if (this.initialized || this.initializing) {
|
|
1661
|
+
if (this.initialized) {
|
|
1662
|
+
console.warn('[Monitor] SDK already initialized');
|
|
1663
|
+
}
|
|
1644
1664
|
return;
|
|
1645
1665
|
}
|
|
1666
|
+
// 如果配置了 configUrl,异步获取远程配置后再初始化
|
|
1667
|
+
if (userConfig.configUrl) {
|
|
1668
|
+
this.initializing = true;
|
|
1669
|
+
this.fetchRemoteConfig(userConfig.configUrl)
|
|
1670
|
+
.then((remoteConfig) => {
|
|
1671
|
+
// 远程配置作为基础,用户本地配置覆盖远程配置
|
|
1672
|
+
const finalConfig = this.mergeUserConfig(remoteConfig, userConfig);
|
|
1673
|
+
if (userConfig.debug) {
|
|
1674
|
+
console.log('[Monitor] Remote config loaded:', remoteConfig);
|
|
1675
|
+
}
|
|
1676
|
+
this.doInit(finalConfig, true);
|
|
1677
|
+
})
|
|
1678
|
+
.catch((error) => {
|
|
1679
|
+
this.initializing = false;
|
|
1680
|
+
console.error('[Monitor] Failed to fetch remote config:', error);
|
|
1681
|
+
});
|
|
1682
|
+
}
|
|
1683
|
+
else {
|
|
1684
|
+
// 没有 configUrl,直接同步初始化
|
|
1685
|
+
this.doInit(userConfig, false);
|
|
1686
|
+
}
|
|
1687
|
+
}
|
|
1688
|
+
/**
|
|
1689
|
+
* 执行实际的初始化逻辑
|
|
1690
|
+
*/
|
|
1691
|
+
doInit(finalConfig, isRemoteConfig) {
|
|
1646
1692
|
// 初始化配置
|
|
1647
1693
|
config.init({
|
|
1648
|
-
...
|
|
1649
|
-
version:
|
|
1650
|
-
});
|
|
1694
|
+
...finalConfig,
|
|
1695
|
+
version: finalConfig.version || SDK_VERSION,
|
|
1696
|
+
}, isRemoteConfig);
|
|
1651
1697
|
// 初始化上下文
|
|
1652
1698
|
const maxBreadcrumbs = config.get().behavior?.maxBreadcrumbs || 20;
|
|
1653
1699
|
context.init(maxBreadcrumbs);
|
|
1654
1700
|
this.initialized = true;
|
|
1701
|
+
this.initializing = false;
|
|
1655
1702
|
if (config.get().debug) {
|
|
1656
1703
|
console.log('[Monitor] SDK initialized', config.get());
|
|
1657
1704
|
}
|
|
@@ -1660,6 +1707,50 @@
|
|
|
1660
1707
|
installPerformanceMonitor();
|
|
1661
1708
|
installBehaviorMonitor();
|
|
1662
1709
|
}
|
|
1710
|
+
/**
|
|
1711
|
+
* 从远程获取配置
|
|
1712
|
+
*/
|
|
1713
|
+
async fetchRemoteConfig(configUrl) {
|
|
1714
|
+
const response = await fetch(configUrl, {
|
|
1715
|
+
method: 'GET',
|
|
1716
|
+
headers: {
|
|
1717
|
+
'Content-Type': 'application/json',
|
|
1718
|
+
},
|
|
1719
|
+
});
|
|
1720
|
+
if (!response.ok) {
|
|
1721
|
+
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
1722
|
+
}
|
|
1723
|
+
return response.json();
|
|
1724
|
+
}
|
|
1725
|
+
/**
|
|
1726
|
+
* 合并配置:远程配置为基础,用户本地配置优先
|
|
1727
|
+
*/
|
|
1728
|
+
mergeUserConfig(remote, local) {
|
|
1729
|
+
const result = { ...remote };
|
|
1730
|
+
for (const key in local) {
|
|
1731
|
+
if (Object.prototype.hasOwnProperty.call(local, key)) {
|
|
1732
|
+
const localValue = local[key];
|
|
1733
|
+
const remoteValue = remote[key];
|
|
1734
|
+
// 跳过 configUrl,不需要传递给最终配置
|
|
1735
|
+
if (key === 'configUrl')
|
|
1736
|
+
continue;
|
|
1737
|
+
if (localValue !== null &&
|
|
1738
|
+
localValue !== undefined &&
|
|
1739
|
+
typeof localValue === 'object' &&
|
|
1740
|
+
!Array.isArray(localValue) &&
|
|
1741
|
+
remoteValue !== null &&
|
|
1742
|
+
remoteValue !== undefined &&
|
|
1743
|
+
typeof remoteValue === 'object' &&
|
|
1744
|
+
!Array.isArray(remoteValue)) {
|
|
1745
|
+
result[key] = { ...remoteValue, ...localValue };
|
|
1746
|
+
}
|
|
1747
|
+
else if (localValue !== undefined) {
|
|
1748
|
+
result[key] = localValue;
|
|
1749
|
+
}
|
|
1750
|
+
}
|
|
1751
|
+
}
|
|
1752
|
+
return result;
|
|
1753
|
+
}
|
|
1663
1754
|
/**
|
|
1664
1755
|
* 设置用户信息
|
|
1665
1756
|
*/
|