@yysports/tools 2.0.1 → 2.0.2

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.
@@ -12,3 +12,6 @@ export declare const DEFAULT_REPLAY_CHECKOUT_NMS: number;
12
12
  export declare const DEFAULT_REPLAY_MAX_EVENTS = 1000;
13
13
  export declare const DEFAULT_REPLAY_MAX_BYTES: number;
14
14
  export declare const DEFAULT_ERROR_THROTTLE_MS = 3000;
15
+ export declare const DEV_DOMAIN = "//track-t.yysports.com";
16
+ export declare const TEST_DOMAIN = "//track-t.yysports.com";
17
+ export declare const PROD_DOMAIN = "//track.yysports.com";
@@ -22,4 +22,13 @@ export var DEFAULT_REPLAY_MAX_EVENTS = 1000;
22
22
  export var DEFAULT_REPLAY_MAX_BYTES = 1024 * 1024; // 1MB
23
23
 
24
24
  // 错误节流默认窗口(毫秒)
25
- export var DEFAULT_ERROR_THROTTLE_MS = 3000;
25
+ export var DEFAULT_ERROR_THROTTLE_MS = 3000;
26
+
27
+ // 开发环境域名
28
+ export var DEV_DOMAIN = '//track-t.yysports.com';
29
+
30
+ // 测试环境域名
31
+ export var TEST_DOMAIN = '//track-t.yysports.com';
32
+
33
+ // 生产环境域名
34
+ export var PROD_DOMAIN = '//track.yysports.com';
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Barrel exports for Track core modules.
3
+ *
4
+ * Purpose:
5
+ * - Provide a single import surface for core primitives (TrackerInstance, queue,
6
+ * replay, transport, lifecycle, etc.).
7
+ * - Make future reorganization easier without touching external callers.
8
+ *
9
+ * Note: this file only re-exports existing modules and does not change runtime
10
+ * behavior. Existing import paths that reference specific files (e.g.
11
+ * './core/instance') will continue to work.
12
+ */
13
+ export { TrackerInstance } from './instance';
14
+ export { TrackQueue } from './queue';
15
+ export { ReplayManager } from './replay';
16
+ export { LifecycleManager } from './lifecycle';
17
+ export * from './transport';
18
+ export * from './auth';
19
+ export * from './envelope';
20
+ export * from './profile';
21
+ export * from './router';
22
+ export * from './constants';
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Barrel exports for Track core modules.
3
+ *
4
+ * Purpose:
5
+ * - Provide a single import surface for core primitives (TrackerInstance, queue,
6
+ * replay, transport, lifecycle, etc.).
7
+ * - Make future reorganization easier without touching external callers.
8
+ *
9
+ * Note: this file only re-exports existing modules and does not change runtime
10
+ * behavior. Existing import paths that reference specific files (e.g.
11
+ * './core/instance') will continue to work.
12
+ */
13
+
14
+ export { TrackerInstance } from "./instance";
15
+ export { TrackQueue } from "./queue";
16
+ export { ReplayManager } from "./replay";
17
+ export { LifecycleManager } from "./lifecycle";
18
+ export * from "./transport";
19
+ export * from "./auth";
20
+ export * from "./envelope";
21
+ export * from "./profile";
22
+ export * from "./router";
23
+ export * from "./constants";
@@ -70,8 +70,8 @@ export declare class TrackerInstance {
70
70
  zoneId: number;
71
71
  zoneName: string;
72
72
  }[]>) | undefined;
73
- domain: string;
74
73
  enableReplay?: boolean | undefined;
74
+ env: "test" | "dev" | "prod";
75
75
  };
76
76
  /** 配置校验:缺少关键字段直接抛错 */
77
77
  private validate;
@@ -7,7 +7,6 @@ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
7
7
  /**
8
8
  * TrackerInstance - 多实例核心组合:权限初始化 + 队列 + 采样 + 生命周期
9
9
  */
10
- // rrweb 录制管理
11
10
 
12
11
  import { initAuthCenterData } from "./auth";
13
12
  import { DEFAULT_ERROR_THROTTLE_MS, DEFAULT_FLUSH_THRESHOLD, DEFAULT_MAX_WAIT_MS } from "./constants";
@@ -133,6 +132,7 @@ export var TrackerInstance = /*#__PURE__*/function () {
133
132
  }, {
134
133
  key: "eventLog",
135
134
  value: function eventLog(_eventLog) {
135
+ if (!this.internal.initialized) return;
136
136
  var data = {
137
137
  module: '',
138
138
  content: ''
@@ -156,6 +156,7 @@ export var TrackerInstance = /*#__PURE__*/function () {
156
156
  }, {
157
157
  key: "visitLog",
158
158
  value: function visitLog(_visitLog) {
159
+ if (!this.internal.initialized) return;
159
160
  var routeInfo = this.routesMap.get(this.getFormatPath());
160
161
  if (routeInfo !== null && routeInfo !== void 0 && routeInfo.isRedirect) return;
161
162
  var mods = this.getFilteredModules();
@@ -183,15 +184,21 @@ export var TrackerInstance = /*#__PURE__*/function () {
183
184
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
184
185
  while (1) switch (_context2.prev = _context2.next) {
185
186
  case 0:
187
+ if (this.internal.initialized) {
188
+ _context2.next = 2;
189
+ break;
190
+ }
191
+ return _context2.abrupt("return");
192
+ case 2:
186
193
  // 简单节流:同一窗口只发送首条
187
194
  throttle = DEFAULT_ERROR_THROTTLE_MS;
188
195
  now = Date.now();
189
196
  if (!(this.internal.lastErrorSentAt && now - this.internal.lastErrorSentAt < throttle)) {
190
- _context2.next = 4;
197
+ _context2.next = 6;
191
198
  break;
192
199
  }
193
200
  return _context2.abrupt("return");
194
- case 4:
201
+ case 6:
195
202
  // 先把报错信息写入回放流,便于回放中定位
196
203
  (_this$replay = this.replay) === null || _this$replay === void 0 || _this$replay.markError(error);
197
204
 
@@ -202,7 +209,7 @@ export var TrackerInstance = /*#__PURE__*/function () {
202
209
 
203
210
  // 上报完成后再更新节流时间戳
204
211
  this.internal.lastErrorSentAt = now;
205
- case 9:
212
+ case 11:
206
213
  case "end":
207
214
  return _context2.stop();
208
215
  }
@@ -220,6 +227,7 @@ export var TrackerInstance = /*#__PURE__*/function () {
220
227
  }, {
221
228
  key: "apiLog",
222
229
  value: function apiLog(_apiLog) {
230
+ if (!this.internal.initialized) return;
223
231
  var env = buildEnvelope(this.config, this.userProfile, 'api', _apiLog);
224
232
  reportApiLog(this.config, _apiLog, env);
225
233
  }
@@ -244,7 +252,7 @@ export var TrackerInstance = /*#__PURE__*/function () {
244
252
  }, {
245
253
  key: "validate",
246
254
  value: function validate() {
247
- if (!this.config.domain) throw new Error('缺少 domain');
255
+ if (!this.config.env) throw new Error('缺少 env');
248
256
  if (!this.config.appId) throw new Error('缺少 appId');
249
257
  }
250
258
 
@@ -303,6 +311,8 @@ export var TrackerInstance = /*#__PURE__*/function () {
303
311
  this.lifecycle.mount();
304
312
  }
305
313
 
314
+ // history 监听功能已移除
315
+
306
316
  /** 启动/重置周期性 flush 计时器 */
307
317
  }, {
308
318
  key: "scheduleFlushTimer",
@@ -5,7 +5,21 @@ import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
5
5
  * 传输层:统一封装 sendBeacon 调用与批量 / 版本上报逻辑。
6
6
  */
7
7
  import { version } from "../../../package.json";
8
- import { APP_VERSION_ENDPOINT, ERROR_REPLAY_ENDPOINT, ERROR_TRACK_ENDPOINT, TRACK_ENDPOINT } from "./constants";
8
+ import { APP_VERSION_ENDPOINT, DEV_DOMAIN, ERROR_REPLAY_ENDPOINT, ERROR_TRACK_ENDPOINT, PROD_DOMAIN, TEST_DOMAIN, TRACK_ENDPOINT } from "./constants";
9
+
10
+ /** 根据 TrackConfig.env 解析要使用的域名(兼容向后默认 test) */
11
+ function resolveDomain(config) {
12
+ var env = config && config.env || 'test';
13
+ switch (env) {
14
+ case 'dev':
15
+ return DEV_DOMAIN;
16
+ case 'prod':
17
+ return PROD_DOMAIN;
18
+ case 'test':
19
+ default:
20
+ return TEST_DOMAIN;
21
+ }
22
+ }
9
23
 
10
24
  /**
11
25
  * 包装fetch,方便async/await调用
@@ -66,8 +80,8 @@ export function beaconSend(url, payload) {
66
80
  * 上报当前应用版本:避免重复上报,通过 localStorage 绑定 appId 做缓存
67
81
  */
68
82
  export function reportAppVersion(config) {
69
- var appId = config.appId,
70
- domain = config.domain;
83
+ var appId = config.appId;
84
+ var domain = resolveDomain(config);
71
85
  var storageKey = "__Track__".concat(appId);
72
86
  try {
73
87
  var cached = localStorage.getItem(storageKey);
@@ -99,13 +113,13 @@ export function reportAppVersion(config) {
99
113
  /** 批量上报日志事件数组 */
100
114
  export function reportBatch(config, batch) {
101
115
  if (!batch.length) return;
102
- var domain = config.domain;
116
+ var domain = resolveDomain(config);
103
117
  beaconSend("".concat(domain).concat(TRACK_ENDPOINT), batch);
104
118
  }
105
119
 
106
120
  /** 上报单条 API 日志(独立通道) */
107
121
  export function reportApiLog(config, apiLog, envelope) {
108
- var domain = config.domain;
122
+ var domain = resolveDomain(config);
109
123
  beaconSend("".concat(domain).concat(TRACK_ENDPOINT), [envelope]);
110
124
  }
111
125
 
@@ -119,7 +133,7 @@ function _reportError() {
119
133
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
120
134
  while (1) switch (_context2.prev = _context2.next) {
121
135
  case 0:
122
- domain = config.domain;
136
+ domain = resolveDomain(config);
123
137
  _context2.next = 3;
124
138
  return fetchWrapper("".concat(domain).concat(ERROR_TRACK_ENDPOINT), {
125
139
  method: 'POST',
@@ -1,20 +1,17 @@
1
1
  /**
2
- * 简化版埋点入口(单实例)
3
2
  * 提供统一 Track.init(config) 及若干日志方法,未初始化即抛错
4
3
  */
5
4
  import { AuthCenterUserProfile, TrackConfig } from '../interfaces/track.interface';
6
- import { TrackerInstance } from './core/instance';
5
+ import { TrackerInstance } from './core';
7
6
  export type { TrackerInstance };
8
7
  type TrackAPI = {
9
8
  init: (config: TrackConfig) => Promise<AuthCenterUserProfile>;
10
9
  eventLog: TrackerInstance['eventLog'];
11
10
  visitLog: TrackerInstance['visitLog'];
12
- errorLog: (...args: Parameters<TrackerInstance['errorLog']>) => Promise<void>;
13
- apiLog: TrackerInstance['apiLog'];
14
- flush: () => void;
15
- destroy: () => void;
16
11
  setUserProfile: (profile: Parameters<TrackerInstance['setUserProfile']>[0]) => void;
17
- getConfig: () => TrackConfig;
12
+ config: TrackConfig;
13
+ };
14
+ declare const Track: TrackAPI & {
15
+ getInstance?: (() => TrackerInstance) | undefined;
18
16
  };
19
- declare const Track: TrackAPI;
20
17
  export default Track;
@@ -1,181 +1,68 @@
1
1
  import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
2
2
  import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
3
- import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
4
- import _createClass from "@babel/runtime/helpers/esm/createClass";
5
- import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
6
3
  /**
7
- * 简化版埋点入口(单实例)
8
4
  * 提供统一 Track.init(config) 及若干日志方法,未初始化即抛错
9
5
  */
10
6
 
11
- import { TrackerInstance } from "./core/instance";
12
- var TrackFacade = /*#__PURE__*/function () {
13
- function TrackFacade() {
14
- _classCallCheck(this, TrackFacade);
15
- }
16
- _createClass(TrackFacade, null, [{
17
- key: "init",
18
- value: ( /** 初始化(必须先调用一次)。重复调用直接返回已存在实例 */function () {
19
- var _init = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(config) {
20
- var _this = this;
21
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
22
- while (1) switch (_context2.prev = _context2.next) {
23
- case 0:
24
- if (!this._instance) {
25
- _context2.next = 2;
26
- break;
27
- }
28
- return _context2.abrupt("return", Promise.resolve(this._profile));
29
- case 2:
30
- if (!this._initPromise) {
31
- this._initPromise = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
32
- var ins, profile;
33
- return _regeneratorRuntime().wrap(function _callee$(_context) {
34
- while (1) switch (_context.prev = _context.next) {
35
- case 0:
36
- ins = new TrackerInstance(config);
37
- _context.next = 3;
38
- return ins.init();
39
- case 3:
40
- profile = _context.sent;
41
- // AuthCenterUserProfile
42
- _this._instance = ins;
43
- _this._profile = profile;
44
- return _context.abrupt("return", profile);
45
- case 7:
46
- case "end":
47
- return _context.stop();
48
- }
49
- }, _callee);
50
- }))();
51
- }
52
- return _context2.abrupt("return", this._initPromise);
53
- case 4:
54
- case "end":
55
- return _context2.stop();
7
+ import { TrackerInstance } from "./core";
8
+ // 模块级单例,简洁实现
9
+ var _instance = null;
10
+ var _profile = null;
11
+ function init(_x) {
12
+ return _init.apply(this, arguments);
13
+ }
14
+ function _init() {
15
+ _init = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(config) {
16
+ var ins, profile;
17
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
18
+ while (1) switch (_context.prev = _context.next) {
19
+ case 0:
20
+ if (!_instance) {
21
+ _context.next = 2;
22
+ break;
56
23
  }
57
- }, _callee2, this);
58
- }));
59
- function init(_x) {
60
- return _init.apply(this, arguments);
24
+ return _context.abrupt("return", Promise.resolve(_profile));
25
+ case 2:
26
+ ins = new TrackerInstance(config);
27
+ _context.next = 5;
28
+ return ins.init();
29
+ case 5:
30
+ profile = _context.sent;
31
+ _instance = ins;
32
+ _profile = profile;
33
+ return _context.abrupt("return", profile);
34
+ case 9:
35
+ case "end":
36
+ return _context.stop();
61
37
  }
62
- return init;
63
- }() /** 工具:确保已初始化,否则抛错 */)
64
- }, {
65
- key: "ensure",
66
- value: function ensure() {
67
- if (!this._instance) throw new Error('Track 未初始化,请先调用 Track.init(config)');
68
- return this._instance;
69
- }
38
+ }, _callee);
39
+ }));
40
+ return _init.apply(this, arguments);
41
+ }
42
+ function ensure() {
43
+ if (!_instance) throw new Error('Track 未初始化,请先调用 Track.init(config)');
44
+ return _instance;
45
+ }
70
46
 
71
- /**
72
- * 发送事件日志(通用事件)
73
- * - 若未初始化:自动排队等待 init 完成后回放
74
- */
75
- }, {
76
- key: "eventLog",
77
- value: function eventLog() {
78
- var _this$ensure;
79
- (_this$ensure = this.ensure()).eventLog.apply(_this$ensure, arguments);
80
- }
81
- /**
82
- * 发送访问/页面曝光日志
83
- * - 用于记录页面 / 场景进入等行为
84
- */
85
- }, {
86
- key: "visitLog",
87
- value: function visitLog() {
88
- var _this$ensure2;
89
- (_this$ensure2 = this.ensure()).visitLog.apply(_this$ensure2, arguments);
90
- }
91
- /**
92
- * 发送错误日志
93
- * - 适用于运行时异常、资源加载失败、接口异常包装等
94
- */
95
- }, {
96
- key: "errorLog",
97
- value: (function () {
98
- var _errorLog = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
99
- var _this$ensure3;
100
- var _args3 = arguments;
101
- return _regeneratorRuntime().wrap(function _callee3$(_context3) {
102
- while (1) switch (_context3.prev = _context3.next) {
103
- case 0:
104
- return _context3.abrupt("return", (_this$ensure3 = this.ensure()).errorLog.apply(_this$ensure3, _args3));
105
- case 1:
106
- case "end":
107
- return _context3.stop();
108
- }
109
- }, _callee3, this);
110
- }));
111
- function errorLog() {
112
- return _errorLog.apply(this, arguments);
47
+ // 更安全的导出:显式 API 形状 + 绑定 this
48
+
49
+ // 懒代理:除 init 外,所有方法直接代理到内部单例实例(调用时会 ensure)
50
+ var Track = new Proxy({}, {
51
+ get: function get(_, prop) {
52
+ if (prop === 'init') return init;
53
+ if (prop === 'config') return ensure().getConfig();
54
+ // 返回一个函数,调用时转发到实例对应方法
55
+ return function () {
56
+ var inst = ensure();
57
+ var fn = inst[prop];
58
+ if (typeof fn !== 'function') {
59
+ throw new Error("Track: no method named \"".concat(String(prop), "\" on instance"));
113
60
  }
114
- return errorLog;
115
- }()
116
- /**
117
- * 发送接口调用相关日志
118
- * - 记录接口耗时 / 结果状态
119
- */
120
- )
121
- }, {
122
- key: "apiLog",
123
- value: function apiLog() {
124
- var _this$ensure4;
125
- (_this$ensure4 = this.ensure()).apiLog.apply(_this$ensure4, arguments);
126
- }
127
- /**
128
- * 强制立即 flush 已缓冲的日志(若实例策略存在缓冲)
129
- */
130
- }, {
131
- key: "flush",
132
- value: function flush() {
133
- this.ensure().flush();
134
- }
135
- /**
136
- * 销毁当前单例实例
137
- * - 停止上报 / 释放资源
138
- * - 再次使用需重新 init
139
- */
140
- }, {
141
- key: "destroy",
142
- value: function destroy() {
143
- if (!this._instance) return;
144
- this._instance.destroy();
145
- this._instance = null;
146
- this._initPromise = null;
147
- this._profile = null;
148
- }
149
- /**
150
- * 设置用户画像 / 用户基础属性
151
- * - 若在 init 前调用:会延迟到实例 ready 后再真正执行
152
- */
153
- }, {
154
- key: "setUserProfile",
155
- value: function setUserProfile(profile) {
156
- this.ensure().setUserProfile(profile);
157
- }
158
- /** 获取当前配置(未初始化时返回 undefined) */
159
- }, {
160
- key: "getConfig",
161
- value: function getConfig() {
162
- return this.ensure().getConfig();
163
- }
164
- }]);
165
- return TrackFacade;
166
- }(); // 更安全的导出:显式 API 形状 + 绑定 this
167
- _defineProperty(TrackFacade, "_instance", null);
168
- _defineProperty(TrackFacade, "_initPromise", null);
169
- _defineProperty(TrackFacade, "_profile", null);
170
- var Track = {
171
- init: TrackFacade.init.bind(TrackFacade),
172
- eventLog: TrackFacade.eventLog.bind(TrackFacade),
173
- visitLog: TrackFacade.visitLog.bind(TrackFacade),
174
- errorLog: TrackFacade.errorLog.bind(TrackFacade),
175
- apiLog: TrackFacade.apiLog.bind(TrackFacade),
176
- flush: TrackFacade.flush.bind(TrackFacade),
177
- destroy: TrackFacade.destroy.bind(TrackFacade),
178
- setUserProfile: TrackFacade.setUserProfile.bind(TrackFacade),
179
- getConfig: TrackFacade.getConfig.bind(TrackFacade)
180
- };
61
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
62
+ args[_key] = arguments[_key];
63
+ }
64
+ return fn.apply(inst, args);
65
+ };
66
+ }
67
+ });
181
68
  export default Track;
@@ -31,8 +31,8 @@ export interface TrackConfig {
31
31
  zoneId: number;
32
32
  zoneName: string;
33
33
  }[]>;
34
- domain: string;
35
34
  enableReplay?: boolean;
35
+ env: 'dev' | 'test' | 'prod';
36
36
  }
37
37
  export declare enum TrackTypeEnum {
38
38
  error = "\u9519\u8BEF\u4FE1\u606F",
@@ -12,3 +12,6 @@ export declare const DEFAULT_REPLAY_CHECKOUT_NMS: number;
12
12
  export declare const DEFAULT_REPLAY_MAX_EVENTS = 1000;
13
13
  export declare const DEFAULT_REPLAY_MAX_BYTES: number;
14
14
  export declare const DEFAULT_ERROR_THROTTLE_MS = 3000;
15
+ export declare const DEV_DOMAIN = "//track-t.yysports.com";
16
+ export declare const TEST_DOMAIN = "//track-t.yysports.com";
17
+ export declare const PROD_DOMAIN = "//track.yysports.com";
@@ -26,8 +26,11 @@ __export(constants_exports, {
26
26
  DEFAULT_REPLAY_CHECKOUT_NMS: () => DEFAULT_REPLAY_CHECKOUT_NMS,
27
27
  DEFAULT_REPLAY_MAX_BYTES: () => DEFAULT_REPLAY_MAX_BYTES,
28
28
  DEFAULT_REPLAY_MAX_EVENTS: () => DEFAULT_REPLAY_MAX_EVENTS,
29
+ DEV_DOMAIN: () => DEV_DOMAIN,
29
30
  ERROR_REPLAY_ENDPOINT: () => ERROR_REPLAY_ENDPOINT,
30
31
  ERROR_TRACK_ENDPOINT: () => ERROR_TRACK_ENDPOINT,
32
+ PROD_DOMAIN: () => PROD_DOMAIN,
33
+ TEST_DOMAIN: () => TEST_DOMAIN,
31
34
  TRACK_ENDPOINT: () => TRACK_ENDPOINT
32
35
  });
33
36
  module.exports = __toCommonJS(constants_exports);
@@ -41,6 +44,9 @@ var DEFAULT_REPLAY_CHECKOUT_NMS = 5 * 60 * 1e3;
41
44
  var DEFAULT_REPLAY_MAX_EVENTS = 1e3;
42
45
  var DEFAULT_REPLAY_MAX_BYTES = 1024 * 1024;
43
46
  var DEFAULT_ERROR_THROTTLE_MS = 3e3;
47
+ var DEV_DOMAIN = "//track-t.yysports.com";
48
+ var TEST_DOMAIN = "//track-t.yysports.com";
49
+ var PROD_DOMAIN = "//track.yysports.com";
44
50
  // Annotate the CommonJS export names for ESM import in node:
45
51
  0 && (module.exports = {
46
52
  APP_VERSION_ENDPOINT,
@@ -50,7 +56,10 @@ var DEFAULT_ERROR_THROTTLE_MS = 3e3;
50
56
  DEFAULT_REPLAY_CHECKOUT_NMS,
51
57
  DEFAULT_REPLAY_MAX_BYTES,
52
58
  DEFAULT_REPLAY_MAX_EVENTS,
59
+ DEV_DOMAIN,
53
60
  ERROR_REPLAY_ENDPOINT,
54
61
  ERROR_TRACK_ENDPOINT,
62
+ PROD_DOMAIN,
63
+ TEST_DOMAIN,
55
64
  TRACK_ENDPOINT
56
65
  });
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Barrel exports for Track core modules.
3
+ *
4
+ * Purpose:
5
+ * - Provide a single import surface for core primitives (TrackerInstance, queue,
6
+ * replay, transport, lifecycle, etc.).
7
+ * - Make future reorganization easier without touching external callers.
8
+ *
9
+ * Note: this file only re-exports existing modules and does not change runtime
10
+ * behavior. Existing import paths that reference specific files (e.g.
11
+ * './core/instance') will continue to work.
12
+ */
13
+ export { TrackerInstance } from './instance';
14
+ export { TrackQueue } from './queue';
15
+ export { ReplayManager } from './replay';
16
+ export { LifecycleManager } from './lifecycle';
17
+ export * from './transport';
18
+ export * from './auth';
19
+ export * from './envelope';
20
+ export * from './profile';
21
+ export * from './router';
22
+ export * from './constants';
@@ -0,0 +1,51 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/Track/core/index.ts
21
+ var core_exports = {};
22
+ __export(core_exports, {
23
+ LifecycleManager: () => import_lifecycle.LifecycleManager,
24
+ ReplayManager: () => import_replay.ReplayManager,
25
+ TrackQueue: () => import_queue.TrackQueue,
26
+ TrackerInstance: () => import_instance.TrackerInstance
27
+ });
28
+ module.exports = __toCommonJS(core_exports);
29
+ var import_instance = require("./instance");
30
+ var import_queue = require("./queue");
31
+ var import_replay = require("./replay");
32
+ var import_lifecycle = require("./lifecycle");
33
+ __reExport(core_exports, require("./transport"), module.exports);
34
+ __reExport(core_exports, require("./auth"), module.exports);
35
+ __reExport(core_exports, require("./envelope"), module.exports);
36
+ __reExport(core_exports, require("./profile"), module.exports);
37
+ __reExport(core_exports, require("./router"), module.exports);
38
+ __reExport(core_exports, require("./constants"), module.exports);
39
+ // Annotate the CommonJS export names for ESM import in node:
40
+ 0 && (module.exports = {
41
+ LifecycleManager,
42
+ ReplayManager,
43
+ TrackQueue,
44
+ TrackerInstance,
45
+ ...require("./transport"),
46
+ ...require("./auth"),
47
+ ...require("./envelope"),
48
+ ...require("./profile"),
49
+ ...require("./router"),
50
+ ...require("./constants")
51
+ });
@@ -70,8 +70,8 @@ export declare class TrackerInstance {
70
70
  zoneId: number;
71
71
  zoneName: string;
72
72
  }[]>) | undefined;
73
- domain: string;
74
73
  enableReplay?: boolean | undefined;
74
+ env: "test" | "dev" | "prod";
75
75
  };
76
76
  /** 配置校验:缺少关键字段直接抛错 */
77
77
  private validate;
@@ -100,6 +100,8 @@ var TrackerInstance = class {
100
100
  * - 自动补全 module(基于当前路由)
101
101
  */
102
102
  eventLog(eventLog) {
103
+ if (!this.internal.initialized)
104
+ return;
103
105
  const data = { module: "", content: "" };
104
106
  if (typeof eventLog === "string") {
105
107
  const action = this.authMap.get(eventLog);
@@ -117,6 +119,8 @@ var TrackerInstance = class {
117
119
  * - 若路由标记 isRedirect 则跳过(避免中转页统计)
118
120
  */
119
121
  visitLog(visitLog) {
122
+ if (!this.internal.initialized)
123
+ return;
120
124
  const routeInfo = this.routesMap.get(this.getFormatPath());
121
125
  if (routeInfo == null ? void 0 : routeInfo.isRedirect)
122
126
  return;
@@ -138,6 +142,8 @@ var TrackerInstance = class {
138
142
  */
139
143
  async errorLog(error) {
140
144
  var _a;
145
+ if (!this.internal.initialized)
146
+ return;
141
147
  const throttle = import_constants.DEFAULT_ERROR_THROTTLE_MS;
142
148
  const now = Date.now();
143
149
  if (this.internal.lastErrorSentAt && now - this.internal.lastErrorSentAt < throttle) {
@@ -153,6 +159,8 @@ var TrackerInstance = class {
153
159
  * API 日志:直接即时上报(不进入公共队列)
154
160
  */
155
161
  apiLog(apiLog) {
162
+ if (!this.internal.initialized)
163
+ return;
156
164
  const env = (0, import_envelope.buildEnvelope)(this.config, this.userProfile, "api", apiLog);
157
165
  (0, import_transport.reportApiLog)(this.config, apiLog, env);
158
166
  }
@@ -168,8 +176,8 @@ var TrackerInstance = class {
168
176
  }
169
177
  /** 配置校验:缺少关键字段直接抛错 */
170
178
  validate() {
171
- if (!this.config.domain)
172
- throw new Error("缺少 domain");
179
+ if (!this.config.env)
180
+ throw new Error("缺少 env");
173
181
  if (!this.config.appId)
174
182
  throw new Error("缺少 appId");
175
183
  }
@@ -212,6 +220,7 @@ var TrackerInstance = class {
212
220
  });
213
221
  this.lifecycle.mount();
214
222
  }
223
+ // history 监听功能已移除
215
224
  /** 启动/重置周期性 flush 计时器 */
216
225
  scheduleFlushTimer() {
217
226
  this.internal.flushTimer && clearInterval(this.internal.flushTimer);
@@ -28,6 +28,18 @@ __export(transport_exports, {
28
28
  module.exports = __toCommonJS(transport_exports);
29
29
  var import_package = require("../../../package.json");
30
30
  var import_constants = require("./constants");
31
+ function resolveDomain(config) {
32
+ const env = config && config.env || "test";
33
+ switch (env) {
34
+ case "dev":
35
+ return import_constants.DEV_DOMAIN;
36
+ case "prod":
37
+ return import_constants.PROD_DOMAIN;
38
+ case "test":
39
+ default:
40
+ return import_constants.TEST_DOMAIN;
41
+ }
42
+ }
31
43
  async function fetchWrapper(url, options) {
32
44
  return new Promise((resolve, reject) => {
33
45
  fetch(url, options).then((response) => {
@@ -54,7 +66,8 @@ function beaconSend(url, payload) {
54
66
  }
55
67
  }
56
68
  function reportAppVersion(config) {
57
- const { appId, domain } = config;
69
+ const { appId } = config;
70
+ const domain = resolveDomain(config);
58
71
  const storageKey = `__Track__${appId}`;
59
72
  try {
60
73
  const cached = localStorage.getItem(storageKey);
@@ -79,15 +92,15 @@ function reportAppVersion(config) {
79
92
  function reportBatch(config, batch) {
80
93
  if (!batch.length)
81
94
  return;
82
- const { domain } = config;
95
+ const domain = resolveDomain(config);
83
96
  beaconSend(`${domain}${import_constants.TRACK_ENDPOINT}`, batch);
84
97
  }
85
98
  function reportApiLog(config, apiLog, envelope) {
86
- const { domain } = config;
99
+ const domain = resolveDomain(config);
87
100
  beaconSend(`${domain}${import_constants.TRACK_ENDPOINT}`, [envelope]);
88
101
  }
89
102
  async function reportError(config, envelope, replayPayload) {
90
- const { domain } = config;
103
+ const domain = resolveDomain(config);
91
104
  const { data: id } = await fetchWrapper(`${domain}${import_constants.ERROR_TRACK_ENDPOINT}`, {
92
105
  method: "POST",
93
106
  headers: {
@@ -1,20 +1,17 @@
1
1
  /**
2
- * 简化版埋点入口(单实例)
3
2
  * 提供统一 Track.init(config) 及若干日志方法,未初始化即抛错
4
3
  */
5
4
  import { AuthCenterUserProfile, TrackConfig } from '../interfaces/track.interface';
6
- import { TrackerInstance } from './core/instance';
5
+ import { TrackerInstance } from './core';
7
6
  export type { TrackerInstance };
8
7
  type TrackAPI = {
9
8
  init: (config: TrackConfig) => Promise<AuthCenterUserProfile>;
10
9
  eventLog: TrackerInstance['eventLog'];
11
10
  visitLog: TrackerInstance['visitLog'];
12
- errorLog: (...args: Parameters<TrackerInstance['errorLog']>) => Promise<void>;
13
- apiLog: TrackerInstance['apiLog'];
14
- flush: () => void;
15
- destroy: () => void;
16
11
  setUserProfile: (profile: Parameters<TrackerInstance['setUserProfile']>[0]) => void;
17
- getConfig: () => TrackConfig;
12
+ config: TrackConfig;
13
+ };
14
+ declare const Track: TrackAPI & {
15
+ getInstance?: (() => TrackerInstance) | undefined;
18
16
  };
19
- declare const Track: TrackAPI;
20
17
  export default Track;
@@ -22,100 +22,42 @@ __export(Track_exports, {
22
22
  default: () => Track_default
23
23
  });
24
24
  module.exports = __toCommonJS(Track_exports);
25
- var import_instance = require("./core/instance");
26
- var TrackFacade = class {
27
- /** 初始化(必须先调用一次)。重复调用直接返回已存在实例 */
28
- static async init(config) {
29
- if (this._instance)
30
- return Promise.resolve(this._profile);
31
- if (!this._initPromise) {
32
- this._initPromise = (async () => {
33
- const ins = new import_instance.TrackerInstance(config);
34
- const profile = await ins.init();
35
- this._instance = ins;
36
- this._profile = profile;
37
- return profile;
38
- })();
25
+ var import_core = require("./core");
26
+ var _instance = null;
27
+ var _profile = null;
28
+ async function init(config) {
29
+ if (_instance)
30
+ return Promise.resolve(_profile);
31
+ const ins = new import_core.TrackerInstance(config);
32
+ const profile = await ins.init();
33
+ _instance = ins;
34
+ _profile = profile;
35
+ return profile;
36
+ }
37
+ function ensure() {
38
+ if (!_instance)
39
+ throw new Error("Track 未初始化,请先调用 Track.init(config)");
40
+ return _instance;
41
+ }
42
+ var Track = new Proxy(
43
+ {},
44
+ {
45
+ get(_, prop) {
46
+ if (prop === "init")
47
+ return init;
48
+ if (prop === "config")
49
+ return ensure().getConfig();
50
+ return (...args) => {
51
+ const inst = ensure();
52
+ const fn = inst[prop];
53
+ if (typeof fn !== "function") {
54
+ throw new Error(
55
+ `Track: no method named "${String(prop)}" on instance`
56
+ );
57
+ }
58
+ return fn.apply(inst, args);
59
+ };
39
60
  }
40
- return this._initPromise;
41
61
  }
42
- /** 工具:确保已初始化,否则抛错 */
43
- static ensure() {
44
- if (!this._instance)
45
- throw new Error("Track 未初始化,请先调用 Track.init(config)");
46
- return this._instance;
47
- }
48
- /**
49
- * 发送事件日志(通用事件)
50
- * - 若未初始化:自动排队等待 init 完成后回放
51
- */
52
- static eventLog(...args) {
53
- this.ensure().eventLog(...args);
54
- }
55
- /**
56
- * 发送访问/页面曝光日志
57
- * - 用于记录页面 / 场景进入等行为
58
- */
59
- static visitLog(...args) {
60
- this.ensure().visitLog(...args);
61
- }
62
- /**
63
- * 发送错误日志
64
- * - 适用于运行时异常、资源加载失败、接口异常包装等
65
- */
66
- static async errorLog(...args) {
67
- return this.ensure().errorLog(...args);
68
- }
69
- /**
70
- * 发送接口调用相关日志
71
- * - 记录接口耗时 / 结果状态
72
- */
73
- static apiLog(...args) {
74
- this.ensure().apiLog(...args);
75
- }
76
- /**
77
- * 强制立即 flush 已缓冲的日志(若实例策略存在缓冲)
78
- */
79
- static flush() {
80
- this.ensure().flush();
81
- }
82
- /**
83
- * 销毁当前单例实例
84
- * - 停止上报 / 释放资源
85
- * - 再次使用需重新 init
86
- */
87
- static destroy() {
88
- if (!this._instance)
89
- return;
90
- this._instance.destroy();
91
- this._instance = null;
92
- this._initPromise = null;
93
- this._profile = null;
94
- }
95
- /**
96
- * 设置用户画像 / 用户基础属性
97
- * - 若在 init 前调用:会延迟到实例 ready 后再真正执行
98
- */
99
- static setUserProfile(profile) {
100
- this.ensure().setUserProfile(profile);
101
- }
102
- /** 获取当前配置(未初始化时返回 undefined) */
103
- static getConfig() {
104
- return this.ensure().getConfig();
105
- }
106
- };
107
- TrackFacade._instance = null;
108
- TrackFacade._initPromise = null;
109
- TrackFacade._profile = null;
110
- var Track = {
111
- init: TrackFacade.init.bind(TrackFacade),
112
- eventLog: TrackFacade.eventLog.bind(TrackFacade),
113
- visitLog: TrackFacade.visitLog.bind(TrackFacade),
114
- errorLog: TrackFacade.errorLog.bind(TrackFacade),
115
- apiLog: TrackFacade.apiLog.bind(TrackFacade),
116
- flush: TrackFacade.flush.bind(TrackFacade),
117
- destroy: TrackFacade.destroy.bind(TrackFacade),
118
- setUserProfile: TrackFacade.setUserProfile.bind(TrackFacade),
119
- getConfig: TrackFacade.getConfig.bind(TrackFacade)
120
- };
62
+ );
121
63
  var Track_default = Track;
@@ -31,8 +31,8 @@ export interface TrackConfig {
31
31
  zoneId: number;
32
32
  zoneName: string;
33
33
  }[]>;
34
- domain: string;
35
34
  enableReplay?: boolean;
35
+ env: 'dev' | 'test' | 'prod';
36
36
  }
37
37
  export declare enum TrackTypeEnum {
38
38
  error = "\u9519\u8BEF\u4FE1\u606F",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yysports/tools",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "常用工具函数库",
5
5
  "homepage": "https://git.pousheng.com/sport-service-department/pousheng-guard/yysports_tools#readme",
6
6
  "bugs": {
@@ -69,13 +69,13 @@
69
69
  "@ant-design/pro-components": "^2.8.10",
70
70
  "@commitlint/cli": "^19.8.1",
71
71
  "@commitlint/config-conventional": "^19.8.1",
72
- "@types/react": "^18.3.23",
72
+ "@types/react": "^18.3.24",
73
73
  "@types/react-dom": "^18.3.7",
74
74
  "@umijs/lint": "^4.4.12",
75
- "antd": "^5.27.0",
75
+ "antd": "^5.27.1",
76
76
  "dumi": "^2.4.21",
77
- "eslint": "^9.33.0",
78
- "father": "^4.6.2",
77
+ "eslint": "^9.34.0",
78
+ "father": "^4.6.3",
79
79
  "husky": "^9.1.7",
80
80
  "lint-staged": "^16.1.5",
81
81
  "prettier": "^3.6.2",