@be-link/cls-logger 1.0.1-beta.5 → 1.0.1-beta.6

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.js CHANGED
@@ -1898,27 +1898,6 @@ function createAutoDeviceInfoBaseFields(envType, opts) {
1898
1898
  };
1899
1899
  }
1900
1900
 
1901
- function readGlobal(key) {
1902
- try {
1903
- const g = globalThis;
1904
- return g[key] ?? null;
1905
- }
1906
- catch {
1907
- return null;
1908
- }
1909
- }
1910
- function tryRequire(moduleName) {
1911
- try {
1912
- // eslint-disable-next-line @typescript-eslint/no-implied-eval
1913
- const req = (typeof require === 'function' ? require : null);
1914
- if (!req)
1915
- return null;
1916
- return req(moduleName);
1917
- }
1918
- catch {
1919
- return null;
1920
- }
1921
- }
1922
1901
  function enterClsSendingGuard() {
1923
1902
  const g = globalThis;
1924
1903
  const next = (g.__beLinkClsLoggerSendingCount__ ?? 0) + 1;
@@ -1928,10 +1907,18 @@ function enterClsSendingGuard() {
1928
1907
  g.__beLinkClsLoggerSendingCount__ = cur > 0 ? cur - 1 : 0;
1929
1908
  };
1930
1909
  }
1931
- class ClsLogger {
1910
+ /**
1911
+ * CLS Logger 核心基类
1912
+ * - 负责所有上报、队列、监控逻辑
1913
+ * - 不包含具体的 SDK 加载实现(由子类负责)
1914
+ * - 这样可以把 web/mini 的 SDK 依赖彻底解耦到子入口
1915
+ */
1916
+ class ClsLoggerCore {
1932
1917
  constructor() {
1933
1918
  this.sdk = null;
1934
1919
  this.sdkPromise = null;
1920
+ this.sdkOverride = null;
1921
+ this.sdkLoaderOverride = null;
1935
1922
  this.client = null;
1936
1923
  this.clientPromise = null;
1937
1924
  this.topicId = null;
@@ -1964,6 +1951,15 @@ class ClsLogger {
1964
1951
  this.behaviorMonitorStarted = false;
1965
1952
  this.behaviorMonitorCleanup = null;
1966
1953
  }
1954
+ /**
1955
+ * 子类可按需重写(默认检测 wx)
1956
+ */
1957
+ detectEnvType() {
1958
+ const wxAny = globalThis.wx;
1959
+ if (wxAny && typeof wxAny.getSystemInfoSync === 'function')
1960
+ return 'miniprogram';
1961
+ return 'browser';
1962
+ }
1967
1963
  init(options) {
1968
1964
  this.initTs = Date.now();
1969
1965
  const topicId = options?.tencentCloud?.topicID ?? options?.topic_id ?? options?.topicID ?? this.topicId ?? null;
@@ -1999,6 +1995,9 @@ class ClsLogger {
1999
1995
  this.appId = options.appId ?? this.appId;
2000
1996
  this.appVersion = options.appVersion ?? this.appVersion;
2001
1997
  this.envType = nextEnvType;
1998
+ // 可选:外部注入 SDK(优先级:sdkLoader > sdk)
1999
+ this.sdkLoaderOverride = options.sdkLoader ?? this.sdkLoaderOverride;
2000
+ this.sdkOverride = options.sdk ?? this.sdkOverride;
2002
2001
  this.userGenerateBaseFields = options.generateBaseFields ?? this.userGenerateBaseFields;
2003
2002
  this.autoGenerateBaseFields = createAutoDeviceInfoBaseFields(this.envType, options.deviceInfo);
2004
2003
  this.storageKey = options.storageKey ?? this.storageKey;
@@ -2117,8 +2116,6 @@ class ClsLogger {
2117
2116
  }
2118
2117
  /**
2119
2118
  * 获取 CLS client(按环境懒加载 SDK)
2120
- * - browser: 优先走 UMD 全局变量 `tencentcloudClsSdkJsWeb`
2121
- * - miniprogram: 优先走 require(webpack/taro 可解析),否则 fallback import()
2122
2119
  */
2123
2120
  async getInstance() {
2124
2121
  if (this.client)
@@ -2141,51 +2138,6 @@ class ClsLogger {
2141
2138
  });
2142
2139
  return this.clientPromise;
2143
2140
  }
2144
- async loadSdk() {
2145
- if (this.sdk)
2146
- return this.sdk;
2147
- if (this.sdkPromise)
2148
- return this.sdkPromise;
2149
- const isMini = this.envType === 'miniprogram';
2150
- const moduleName = isMini ? 'tencentcloud-cls-sdk-js-mini' : 'tencentcloud-cls-sdk-js-web';
2151
- // UMD(浏览器脚本)优先读全局变量
2152
- if (!isMini) {
2153
- const g = readGlobal('tencentcloudClsSdkJsWeb');
2154
- if (g?.AsyncClient && g?.Log && g?.LogGroup && g?.PutLogsRequest) {
2155
- this.sdk = g;
2156
- return this.sdk;
2157
- }
2158
- }
2159
- // 尽量同步 require(小程序/webpack 里最稳),失败再走 import()
2160
- const reqMod = tryRequire(moduleName);
2161
- if (reqMod?.AsyncClient && reqMod?.Log && reqMod?.LogGroup && reqMod?.PutLogsRequest) {
2162
- this.sdk = reqMod;
2163
- return this.sdk;
2164
- }
2165
- this.sdkPromise = import(moduleName)
2166
- .then((m) => {
2167
- const mod = (m?.default && m.default.AsyncClient ? m.default : m);
2168
- const sdk = {
2169
- AsyncClient: mod.AsyncClient,
2170
- Log: mod.Log,
2171
- LogGroup: mod.LogGroup,
2172
- PutLogsRequest: mod.PutLogsRequest,
2173
- };
2174
- this.sdk = sdk;
2175
- return sdk;
2176
- })
2177
- .catch((err) => {
2178
- this.sdkPromise = null;
2179
- throw err;
2180
- });
2181
- return this.sdkPromise;
2182
- }
2183
- detectEnvType() {
2184
- const wxAny = globalThis.wx;
2185
- if (wxAny && typeof wxAny.getSystemInfoSync === 'function')
2186
- return 'miniprogram';
2187
- return 'browser';
2188
- }
2189
2141
  /**
2190
2142
  * 直接上报:埋点入参必须是一维(扁平)Object
2191
2143
  * - 非原始值(对象/数组等)会被自动 stringify 成 string
@@ -2528,6 +2480,122 @@ class ClsLogger {
2528
2480
  }
2529
2481
  }
2530
2482
 
2483
+ function readGlobal(key) {
2484
+ try {
2485
+ const g = globalThis;
2486
+ return g[key] ?? null;
2487
+ }
2488
+ catch {
2489
+ return null;
2490
+ }
2491
+ }
2492
+ function tryRequire(moduleName) {
2493
+ try {
2494
+ // 说明:
2495
+ // - ESM 构建(exports.import/module)里通常不存在模块作用域的 require
2496
+ // - 一些小程序运行时/构建链路会把 require 挂到 globalThis 上
2497
+ // 因此这里同时探测“模块作用域 require”与 “globalThis.require”
2498
+ // eslint-disable-next-line @typescript-eslint/no-implied-eval
2499
+ const localReq = (typeof require === 'function' ? require : null);
2500
+ const globalReq = readGlobal('require');
2501
+ const candidates = [localReq, globalReq].filter((fn) => typeof fn === 'function');
2502
+ for (const fn of candidates) {
2503
+ try {
2504
+ return fn(moduleName);
2505
+ }
2506
+ catch {
2507
+ // continue
2508
+ }
2509
+ }
2510
+ return null;
2511
+ }
2512
+ catch {
2513
+ return null;
2514
+ }
2515
+ }
2516
+
2517
+ /**
2518
+ * 兼容版 ClsLogger(默认入口)
2519
+ * - 保留了自动识别环境 + 动态 import 的逻辑
2520
+ * - 如果业务想瘦身,建议改用 @be-link/cls-logger/web 或 /mini
2521
+ */
2522
+ class ClsLogger extends ClsLoggerCore {
2523
+ async loadSdk() {
2524
+ if (this.sdk)
2525
+ return this.sdk;
2526
+ if (this.sdkPromise)
2527
+ return this.sdkPromise;
2528
+ const normalizeSdk = (m) => {
2529
+ const mod = (m?.default && m.default.AsyncClient ? m.default : m);
2530
+ if (mod?.AsyncClient && mod?.Log && mod?.LogGroup && mod?.PutLogsRequest) {
2531
+ return {
2532
+ AsyncClient: mod.AsyncClient,
2533
+ Log: mod.Log,
2534
+ LogGroup: mod.LogGroup,
2535
+ PutLogsRequest: mod.PutLogsRequest,
2536
+ };
2537
+ }
2538
+ return null;
2539
+ };
2540
+ // 1) 外部注入的 loader(最高优先级)
2541
+ if (this.sdkLoaderOverride) {
2542
+ try {
2543
+ const loaded = await this.sdkLoaderOverride();
2544
+ const sdk = normalizeSdk(loaded);
2545
+ if (sdk) {
2546
+ this.sdk = sdk;
2547
+ return sdk;
2548
+ }
2549
+ }
2550
+ catch {
2551
+ // ignore and fallback
2552
+ }
2553
+ }
2554
+ // 2) 外部直接注入的 sdk
2555
+ if (this.sdkOverride) {
2556
+ const sdk = normalizeSdk(this.sdkOverride);
2557
+ if (sdk) {
2558
+ this.sdk = sdk;
2559
+ return sdk;
2560
+ }
2561
+ }
2562
+ const isMini = this.envType === 'miniprogram';
2563
+ // 静态字面量,方便打包器识别(但在此兼容入口里,仍然可能被一起分析)
2564
+ const WEB_SDK = 'tencentcloud-cls-sdk-js-web';
2565
+ const MINI_SDK = 'tencentcloud-cls-sdk-js-mini';
2566
+ // UMD(浏览器脚本)优先读全局变量
2567
+ if (!isMini) {
2568
+ const g = readGlobal('tencentcloudClsSdkJsWeb');
2569
+ const sdk = normalizeSdk(g);
2570
+ if (sdk) {
2571
+ this.sdk = sdk;
2572
+ return sdk;
2573
+ }
2574
+ }
2575
+ // 尽量同步 require
2576
+ const reqMod = tryRequire(isMini ? MINI_SDK : WEB_SDK);
2577
+ const reqSdk = normalizeSdk(reqMod);
2578
+ if (reqSdk) {
2579
+ this.sdk = reqSdk;
2580
+ return reqSdk;
2581
+ }
2582
+ // 动态 import
2583
+ this.sdkPromise = (isMini ? import(MINI_SDK) : import(WEB_SDK))
2584
+ .then((m) => {
2585
+ const sdk = normalizeSdk(m);
2586
+ if (!sdk)
2587
+ throw new Error(`ClsLogger.loadSdk: invalid sdk module for ${isMini ? MINI_SDK : WEB_SDK}`);
2588
+ this.sdk = sdk;
2589
+ return sdk;
2590
+ })
2591
+ .catch((err) => {
2592
+ this.sdkPromise = null;
2593
+ throw err;
2594
+ });
2595
+ return this.sdkPromise;
2596
+ }
2597
+ }
2598
+
2531
2599
  const clsLogger = new ClsLogger();
2532
2600
 
2533
2601
  exports.ClsLogger = ClsLogger;
package/dist/index.umd.js CHANGED
@@ -1900,27 +1900,6 @@
1900
1900
  };
1901
1901
  }
1902
1902
 
1903
- function readGlobal(key) {
1904
- try {
1905
- const g = globalThis;
1906
- return g[key] ?? null;
1907
- }
1908
- catch {
1909
- return null;
1910
- }
1911
- }
1912
- function tryRequire(moduleName) {
1913
- try {
1914
- // eslint-disable-next-line @typescript-eslint/no-implied-eval
1915
- const req = (typeof require === 'function' ? require : null);
1916
- if (!req)
1917
- return null;
1918
- return req(moduleName);
1919
- }
1920
- catch {
1921
- return null;
1922
- }
1923
- }
1924
1903
  function enterClsSendingGuard() {
1925
1904
  const g = globalThis;
1926
1905
  const next = (g.__beLinkClsLoggerSendingCount__ ?? 0) + 1;
@@ -1930,10 +1909,18 @@
1930
1909
  g.__beLinkClsLoggerSendingCount__ = cur > 0 ? cur - 1 : 0;
1931
1910
  };
1932
1911
  }
1933
- class ClsLogger {
1912
+ /**
1913
+ * CLS Logger 核心基类
1914
+ * - 负责所有上报、队列、监控逻辑
1915
+ * - 不包含具体的 SDK 加载实现(由子类负责)
1916
+ * - 这样可以把 web/mini 的 SDK 依赖彻底解耦到子入口
1917
+ */
1918
+ class ClsLoggerCore {
1934
1919
  constructor() {
1935
1920
  this.sdk = null;
1936
1921
  this.sdkPromise = null;
1922
+ this.sdkOverride = null;
1923
+ this.sdkLoaderOverride = null;
1937
1924
  this.client = null;
1938
1925
  this.clientPromise = null;
1939
1926
  this.topicId = null;
@@ -1966,6 +1953,15 @@
1966
1953
  this.behaviorMonitorStarted = false;
1967
1954
  this.behaviorMonitorCleanup = null;
1968
1955
  }
1956
+ /**
1957
+ * 子类可按需重写(默认检测 wx)
1958
+ */
1959
+ detectEnvType() {
1960
+ const wxAny = globalThis.wx;
1961
+ if (wxAny && typeof wxAny.getSystemInfoSync === 'function')
1962
+ return 'miniprogram';
1963
+ return 'browser';
1964
+ }
1969
1965
  init(options) {
1970
1966
  this.initTs = Date.now();
1971
1967
  const topicId = options?.tencentCloud?.topicID ?? options?.topic_id ?? options?.topicID ?? this.topicId ?? null;
@@ -2001,6 +1997,9 @@
2001
1997
  this.appId = options.appId ?? this.appId;
2002
1998
  this.appVersion = options.appVersion ?? this.appVersion;
2003
1999
  this.envType = nextEnvType;
2000
+ // 可选:外部注入 SDK(优先级:sdkLoader > sdk)
2001
+ this.sdkLoaderOverride = options.sdkLoader ?? this.sdkLoaderOverride;
2002
+ this.sdkOverride = options.sdk ?? this.sdkOverride;
2004
2003
  this.userGenerateBaseFields = options.generateBaseFields ?? this.userGenerateBaseFields;
2005
2004
  this.autoGenerateBaseFields = createAutoDeviceInfoBaseFields(this.envType, options.deviceInfo);
2006
2005
  this.storageKey = options.storageKey ?? this.storageKey;
@@ -2119,8 +2118,6 @@
2119
2118
  }
2120
2119
  /**
2121
2120
  * 获取 CLS client(按环境懒加载 SDK)
2122
- * - browser: 优先走 UMD 全局变量 `tencentcloudClsSdkJsWeb`
2123
- * - miniprogram: 优先走 require(webpack/taro 可解析),否则 fallback import()
2124
2121
  */
2125
2122
  async getInstance() {
2126
2123
  if (this.client)
@@ -2143,51 +2140,6 @@
2143
2140
  });
2144
2141
  return this.clientPromise;
2145
2142
  }
2146
- async loadSdk() {
2147
- if (this.sdk)
2148
- return this.sdk;
2149
- if (this.sdkPromise)
2150
- return this.sdkPromise;
2151
- const isMini = this.envType === 'miniprogram';
2152
- const moduleName = isMini ? 'tencentcloud-cls-sdk-js-mini' : 'tencentcloud-cls-sdk-js-web';
2153
- // UMD(浏览器脚本)优先读全局变量
2154
- if (!isMini) {
2155
- const g = readGlobal('tencentcloudClsSdkJsWeb');
2156
- if (g?.AsyncClient && g?.Log && g?.LogGroup && g?.PutLogsRequest) {
2157
- this.sdk = g;
2158
- return this.sdk;
2159
- }
2160
- }
2161
- // 尽量同步 require(小程序/webpack 里最稳),失败再走 import()
2162
- const reqMod = tryRequire(moduleName);
2163
- if (reqMod?.AsyncClient && reqMod?.Log && reqMod?.LogGroup && reqMod?.PutLogsRequest) {
2164
- this.sdk = reqMod;
2165
- return this.sdk;
2166
- }
2167
- this.sdkPromise = import(moduleName)
2168
- .then((m) => {
2169
- const mod = (m?.default && m.default.AsyncClient ? m.default : m);
2170
- const sdk = {
2171
- AsyncClient: mod.AsyncClient,
2172
- Log: mod.Log,
2173
- LogGroup: mod.LogGroup,
2174
- PutLogsRequest: mod.PutLogsRequest,
2175
- };
2176
- this.sdk = sdk;
2177
- return sdk;
2178
- })
2179
- .catch((err) => {
2180
- this.sdkPromise = null;
2181
- throw err;
2182
- });
2183
- return this.sdkPromise;
2184
- }
2185
- detectEnvType() {
2186
- const wxAny = globalThis.wx;
2187
- if (wxAny && typeof wxAny.getSystemInfoSync === 'function')
2188
- return 'miniprogram';
2189
- return 'browser';
2190
- }
2191
2143
  /**
2192
2144
  * 直接上报:埋点入参必须是一维(扁平)Object
2193
2145
  * - 非原始值(对象/数组等)会被自动 stringify 成 string
@@ -2530,6 +2482,122 @@
2530
2482
  }
2531
2483
  }
2532
2484
 
2485
+ function readGlobal(key) {
2486
+ try {
2487
+ const g = globalThis;
2488
+ return g[key] ?? null;
2489
+ }
2490
+ catch {
2491
+ return null;
2492
+ }
2493
+ }
2494
+ function tryRequire(moduleName) {
2495
+ try {
2496
+ // 说明:
2497
+ // - ESM 构建(exports.import/module)里通常不存在模块作用域的 require
2498
+ // - 一些小程序运行时/构建链路会把 require 挂到 globalThis 上
2499
+ // 因此这里同时探测“模块作用域 require”与 “globalThis.require”
2500
+ // eslint-disable-next-line @typescript-eslint/no-implied-eval
2501
+ const localReq = (typeof require === 'function' ? require : null);
2502
+ const globalReq = readGlobal('require');
2503
+ const candidates = [localReq, globalReq].filter((fn) => typeof fn === 'function');
2504
+ for (const fn of candidates) {
2505
+ try {
2506
+ return fn(moduleName);
2507
+ }
2508
+ catch {
2509
+ // continue
2510
+ }
2511
+ }
2512
+ return null;
2513
+ }
2514
+ catch {
2515
+ return null;
2516
+ }
2517
+ }
2518
+
2519
+ /**
2520
+ * 兼容版 ClsLogger(默认入口)
2521
+ * - 保留了自动识别环境 + 动态 import 的逻辑
2522
+ * - 如果业务想瘦身,建议改用 @be-link/cls-logger/web 或 /mini
2523
+ */
2524
+ class ClsLogger extends ClsLoggerCore {
2525
+ async loadSdk() {
2526
+ if (this.sdk)
2527
+ return this.sdk;
2528
+ if (this.sdkPromise)
2529
+ return this.sdkPromise;
2530
+ const normalizeSdk = (m) => {
2531
+ const mod = (m?.default && m.default.AsyncClient ? m.default : m);
2532
+ if (mod?.AsyncClient && mod?.Log && mod?.LogGroup && mod?.PutLogsRequest) {
2533
+ return {
2534
+ AsyncClient: mod.AsyncClient,
2535
+ Log: mod.Log,
2536
+ LogGroup: mod.LogGroup,
2537
+ PutLogsRequest: mod.PutLogsRequest,
2538
+ };
2539
+ }
2540
+ return null;
2541
+ };
2542
+ // 1) 外部注入的 loader(最高优先级)
2543
+ if (this.sdkLoaderOverride) {
2544
+ try {
2545
+ const loaded = await this.sdkLoaderOverride();
2546
+ const sdk = normalizeSdk(loaded);
2547
+ if (sdk) {
2548
+ this.sdk = sdk;
2549
+ return sdk;
2550
+ }
2551
+ }
2552
+ catch {
2553
+ // ignore and fallback
2554
+ }
2555
+ }
2556
+ // 2) 外部直接注入的 sdk
2557
+ if (this.sdkOverride) {
2558
+ const sdk = normalizeSdk(this.sdkOverride);
2559
+ if (sdk) {
2560
+ this.sdk = sdk;
2561
+ return sdk;
2562
+ }
2563
+ }
2564
+ const isMini = this.envType === 'miniprogram';
2565
+ // 静态字面量,方便打包器识别(但在此兼容入口里,仍然可能被一起分析)
2566
+ const WEB_SDK = 'tencentcloud-cls-sdk-js-web';
2567
+ const MINI_SDK = 'tencentcloud-cls-sdk-js-mini';
2568
+ // UMD(浏览器脚本)优先读全局变量
2569
+ if (!isMini) {
2570
+ const g = readGlobal('tencentcloudClsSdkJsWeb');
2571
+ const sdk = normalizeSdk(g);
2572
+ if (sdk) {
2573
+ this.sdk = sdk;
2574
+ return sdk;
2575
+ }
2576
+ }
2577
+ // 尽量同步 require
2578
+ const reqMod = tryRequire(isMini ? MINI_SDK : WEB_SDK);
2579
+ const reqSdk = normalizeSdk(reqMod);
2580
+ if (reqSdk) {
2581
+ this.sdk = reqSdk;
2582
+ return reqSdk;
2583
+ }
2584
+ // 动态 import
2585
+ this.sdkPromise = (isMini ? import(MINI_SDK) : import(WEB_SDK))
2586
+ .then((m) => {
2587
+ const sdk = normalizeSdk(m);
2588
+ if (!sdk)
2589
+ throw new Error(`ClsLogger.loadSdk: invalid sdk module for ${isMini ? MINI_SDK : WEB_SDK}`);
2590
+ this.sdk = sdk;
2591
+ return sdk;
2592
+ })
2593
+ .catch((err) => {
2594
+ this.sdkPromise = null;
2595
+ throw err;
2596
+ });
2597
+ return this.sdkPromise;
2598
+ }
2599
+ }
2600
+
2533
2601
  const clsLogger = new ClsLogger();
2534
2602
 
2535
2603
  exports.ClsLogger = ClsLogger;
package/dist/mini.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import { ClsLoggerMini } from './ClsLoggerMini';
2
+ export * from './types';
3
+ export { ClsLoggerMini as ClsLogger };
4
+ export declare const clsLogger: ClsLoggerMini;
5
+ export default clsLogger;
6
+ //# sourceMappingURL=mini.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mini.d.ts","sourceRoot":"","sources":["../src/mini.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAEhD,cAAc,SAAS,CAAC;AACxB,OAAO,EAAE,aAAa,IAAI,SAAS,EAAE,CAAC;AACtC,eAAO,MAAM,SAAS,eAAsB,CAAC;AAC7C,eAAe,SAAS,CAAC"}