@yysports/tools 2.0.2 → 2.2.1

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.
Files changed (92) hide show
  1. package/dist/Track/Beacon.d.ts +6 -0
  2. package/dist/Track/Beacon.js +58 -0
  3. package/dist/Track/Config.d.ts +11 -0
  4. package/dist/Track/Config.js +34 -0
  5. package/dist/Track/Listener.d.ts +5 -0
  6. package/dist/Track/Listener.js +46 -0
  7. package/dist/Track/Log.d.ts +8 -0
  8. package/dist/Track/Log.js +100 -0
  9. package/dist/Track/Replay.d.ts +6 -0
  10. package/dist/Track/Replay.js +77 -0
  11. package/dist/Track/System.d.ts +5 -0
  12. package/dist/Track/System.js +24 -0
  13. package/dist/Track/User.d.ts +6 -0
  14. package/dist/Track/User.js +29 -0
  15. package/dist/Track/index.d.ts +15 -17
  16. package/dist/Track/index.js +56 -67
  17. package/dist/Utils/auth.d.ts +9 -2
  18. package/dist/Utils/auth.js +80 -20
  19. package/dist/Utils/route.d.ts +11 -3
  20. package/dist/Utils/route.js +49 -56
  21. package/dist/Utils/url.js +3 -13
  22. package/dist/interfaces/track.interface.d.ts +2 -80
  23. package/dist/interfaces/track.interface.js +1 -5
  24. package/lib/Track/Beacon.d.ts +6 -0
  25. package/lib/Track/Beacon.js +73 -0
  26. package/lib/Track/Config.d.ts +11 -0
  27. package/lib/Track/{core/queue.js → Config.js} +23 -27
  28. package/lib/Track/Listener.d.ts +5 -0
  29. package/lib/Track/Listener.js +62 -0
  30. package/lib/Track/Log.d.ts +8 -0
  31. package/lib/Track/Log.js +95 -0
  32. package/lib/Track/Replay.d.ts +6 -0
  33. package/lib/Track/Replay.js +79 -0
  34. package/lib/Track/System.d.ts +5 -0
  35. package/lib/Track/{core/router.js → System.js} +18 -20
  36. package/lib/Track/User.d.ts +6 -0
  37. package/lib/Track/User.js +40 -0
  38. package/lib/Track/index.d.ts +15 -17
  39. package/lib/Track/index.js +45 -39
  40. package/lib/Utils/auth.d.ts +9 -2
  41. package/lib/Utils/auth.js +52 -17
  42. package/lib/Utils/route.d.ts +11 -3
  43. package/lib/Utils/route.js +34 -49
  44. package/lib/Utils/url.js +3 -3
  45. package/lib/interfaces/track.interface.d.ts +2 -80
  46. package/package.json +13 -13
  47. package/dist/Track/core/auth.d.ts +0 -16
  48. package/dist/Track/core/auth.js +0 -83
  49. package/dist/Track/core/constants.d.ts +0 -17
  50. package/dist/Track/core/constants.js +0 -34
  51. package/dist/Track/core/envelope.d.ts +0 -9
  52. package/dist/Track/core/envelope.js +0 -54
  53. package/dist/Track/core/index.d.ts +0 -22
  54. package/dist/Track/core/index.js +0 -23
  55. package/dist/Track/core/instance.d.ts +0 -108
  56. package/dist/Track/core/instance.js +0 -375
  57. package/dist/Track/core/lifecycle.d.ts +0 -18
  58. package/dist/Track/core/lifecycle.js +0 -59
  59. package/dist/Track/core/profile.d.ts +0 -5
  60. package/dist/Track/core/profile.js +0 -30
  61. package/dist/Track/core/queue.d.ts +0 -13
  62. package/dist/Track/core/queue.js +0 -36
  63. package/dist/Track/core/replay.d.ts +0 -17
  64. package/dist/Track/core/replay.js +0 -117
  65. package/dist/Track/core/router.d.ts +0 -6
  66. package/dist/Track/core/router.js +0 -15
  67. package/dist/Track/core/system.d.ts +0 -14
  68. package/dist/Track/core/system.js +0 -36
  69. package/dist/Track/core/transport.d.ts +0 -18
  70. package/dist/Track/core/transport.js +0 -165
  71. package/lib/Track/core/auth.d.ts +0 -16
  72. package/lib/Track/core/auth.js +0 -67
  73. package/lib/Track/core/constants.d.ts +0 -17
  74. package/lib/Track/core/constants.js +0 -65
  75. package/lib/Track/core/envelope.d.ts +0 -9
  76. package/lib/Track/core/envelope.js +0 -61
  77. package/lib/Track/core/index.d.ts +0 -22
  78. package/lib/Track/core/index.js +0 -51
  79. package/lib/Track/core/instance.d.ts +0 -108
  80. package/lib/Track/core/instance.js +0 -266
  81. package/lib/Track/core/lifecycle.d.ts +0 -18
  82. package/lib/Track/core/lifecycle.js +0 -74
  83. package/lib/Track/core/profile.d.ts +0 -5
  84. package/lib/Track/core/profile.js +0 -47
  85. package/lib/Track/core/queue.d.ts +0 -13
  86. package/lib/Track/core/replay.d.ts +0 -17
  87. package/lib/Track/core/replay.js +0 -131
  88. package/lib/Track/core/router.d.ts +0 -6
  89. package/lib/Track/core/system.d.ts +0 -14
  90. package/lib/Track/core/system.js +0 -53
  91. package/lib/Track/core/transport.d.ts +0 -18
  92. package/lib/Track/core/transport.js +0 -128
@@ -1,266 +0,0 @@
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
-
19
- // src/Track/core/instance.ts
20
- var instance_exports = {};
21
- __export(instance_exports, {
22
- TrackerInstance: () => TrackerInstance
23
- });
24
- module.exports = __toCommonJS(instance_exports);
25
- var import_auth = require("./auth");
26
- var import_constants = require("./constants");
27
- var import_envelope = require("./envelope");
28
- var import_lifecycle = require("./lifecycle");
29
- var import_profile = require("./profile");
30
- var import_queue = require("./queue");
31
- var import_replay = require("./replay");
32
- var import_router = require("./router");
33
- var import_transport = require("./transport");
34
- var TrackerInstance = class {
35
- /**
36
- * 构造函数:合并配置 + 参数校验
37
- */
38
- constructor(cfg) {
39
- this.userProfile = { userId: "", userName: "" };
40
- this.authCenterUserProfile = {};
41
- this.routesMap = /* @__PURE__ */ new Map();
42
- this.authMap = /* @__PURE__ */ new Map();
43
- this.queue = new import_queue.TrackQueue();
44
- this.internal = { initialized: false, destroyed: false };
45
- this.config = {
46
- // 回放默认开启
47
- enableReplay: true,
48
- ...cfg
49
- };
50
- this.validate();
51
- }
52
- /**
53
- * 初始化:
54
- * 1. 拉取权限/菜单/数据域信息
55
- * 2. 合并用户画像(优先 AuthCenter,再叠加外部传入)
56
- * 3. 挂载生命周期监听(visibilitychange / error)
57
- * 4. 上报版本信息 & 启动定时 flush
58
- */
59
- async init() {
60
- if (this.internal.initialized)
61
- return this.authCenterUserProfile;
62
- const { authCenterUserProfile, authMap, routesMap } = await (0, import_auth.initAuthCenterData)(this.config);
63
- this.authCenterUserProfile = authCenterUserProfile;
64
- this.authMap = authMap;
65
- this.routesMap = routesMap;
66
- this.mergeUserProfile();
67
- this.mountLifecycle();
68
- this.maybeStartReplay();
69
- (0, import_transport.reportAppVersion)(this.config);
70
- this.internal.initialized = true;
71
- this.scheduleFlushTimer();
72
- return this.authCenterUserProfile;
73
- }
74
- /**
75
- * Flush:
76
- * - 取出队列中全部事件
77
- */
78
- flush() {
79
- if (!this.queue.size())
80
- return;
81
- const batch = this.queue.drain();
82
- (0, import_transport.reportBatch)(this.config, batch);
83
- }
84
- /**
85
- * 销毁:清理定时器 & 事件监听,并 flush 剩余数据
86
- */
87
- destroy() {
88
- var _a;
89
- if (this.internal.destroyed)
90
- return;
91
- this.flush();
92
- (_a = this.lifecycle) == null ? void 0 : _a.unmount();
93
- this.stopReplay();
94
- clearInterval(this.internal.flushTimer);
95
- this.internal.destroyed = true;
96
- }
97
- /**
98
- * 事件日志:
99
- * - 支持传入字符串(自动映射为 authMap 描述)或对象
100
- * - 自动补全 module(基于当前路由)
101
- */
102
- eventLog(eventLog) {
103
- if (!this.internal.initialized)
104
- return;
105
- const data = { module: "", content: "" };
106
- if (typeof eventLog === "string") {
107
- const action = this.authMap.get(eventLog);
108
- data.content = action ? "触发: " + action : eventLog;
109
- } else
110
- Object.assign(data, eventLog);
111
- const modules = this.getFilteredModules();
112
- if (!this.fillModuleIfMissing(data, modules))
113
- return;
114
- this.enqueue("event", data);
115
- }
116
- /**
117
- * 访问日志:
118
- * - 默认按当前路由模块组装文案
119
- * - 若路由标记 isRedirect 则跳过(避免中转页统计)
120
- */
121
- visitLog(visitLog) {
122
- if (!this.internal.initialized)
123
- return;
124
- const routeInfo = this.routesMap.get(this.getFormatPath());
125
- if (routeInfo == null ? void 0 : routeInfo.isRedirect)
126
- return;
127
- const mods = this.getFilteredModules();
128
- if (!visitLog) {
129
- if (!mods.length)
130
- return;
131
- visitLog = {
132
- module: mods.join("-"),
133
- content: `访问页面: ${mods[mods.length - 1]}`
134
- };
135
- }
136
- if (!this.fillModuleIfMissing(visitLog, mods))
137
- return;
138
- this.enqueue("visit", visitLog);
139
- }
140
- /**
141
- * 错误日志:force = true 不受采样率影响
142
- */
143
- async errorLog(error) {
144
- var _a;
145
- if (!this.internal.initialized)
146
- return;
147
- const throttle = import_constants.DEFAULT_ERROR_THROTTLE_MS;
148
- const now = Date.now();
149
- if (this.internal.lastErrorSentAt && now - this.internal.lastErrorSentAt < throttle) {
150
- return;
151
- }
152
- (_a = this.replay) == null ? void 0 : _a.markError(error);
153
- const replayPayload = this.buildReplayAttachment();
154
- const env = (0, import_envelope.buildEnvelope)(this.config, this.userProfile, "error", error);
155
- (0, import_transport.reportError)(this.config, env, replayPayload);
156
- this.internal.lastErrorSentAt = now;
157
- }
158
- /**
159
- * API 日志:直接即时上报(不进入公共队列)
160
- */
161
- apiLog(apiLog) {
162
- if (!this.internal.initialized)
163
- return;
164
- const env = (0, import_envelope.buildEnvelope)(this.config, this.userProfile, "api", apiLog);
165
- (0, import_transport.reportApiLog)(this.config, apiLog, env);
166
- }
167
- /**
168
- * 动态更新用户画像(不会重置已有字段)
169
- */
170
- setUserProfile(patch) {
171
- Object.assign(this.userProfile, patch);
172
- }
173
- /** 获取当前实例配置副本(避免外部直接修改内部对象) */
174
- getConfig() {
175
- return { ...this.config };
176
- }
177
- /** 配置校验:缺少关键字段直接抛错 */
178
- validate() {
179
- if (!this.config.env)
180
- throw new Error("缺少 env");
181
- if (!this.config.appId)
182
- throw new Error("缺少 appId");
183
- }
184
- /**
185
- * 合并用户画像:
186
- * - 若权限中心未返回 userName,则只使用外部提供
187
- * - 否则使用 AuthCenter 信息并叠加外部 patch
188
- */
189
- mergeUserProfile() {
190
- this.userProfile = (0, import_profile.mergeUserProfile)(
191
- this.authCenterUserProfile,
192
- this.config.userProfile
193
- );
194
- }
195
- /**
196
- * 入队封装:
197
- * - 全量采样
198
- * - 生成 envelope
199
- * - 触发阈值自动 flush
200
- */
201
- enqueue(type, log, opts) {
202
- const env = (0, import_envelope.buildEnvelope)(this.config, this.userProfile, type, log);
203
- this.queue.push(env);
204
- if (this.queue.size() >= import_constants.DEFAULT_FLUSH_THRESHOLD)
205
- this.flush();
206
- }
207
- /** 获取当前路径(剥离 base 前缀) */
208
- getFormatPath() {
209
- return (0, import_router.getFormattedPath)(this.config);
210
- }
211
- /** 获取当前页面的模块层级(由 routesMap 提供) */
212
- getPageModules() {
213
- return (0, import_router.getPageModules)(this.config, this.routesMap);
214
- }
215
- /** 挂载浏览器事件监听 */
216
- mountLifecycle() {
217
- this.lifecycle = new import_lifecycle.LifecycleManager({
218
- onHidden: () => this.flush(),
219
- onError: (err) => this.errorLog(err)
220
- });
221
- this.lifecycle.mount();
222
- }
223
- // history 监听功能已移除
224
- /** 启动/重置周期性 flush 计时器 */
225
- scheduleFlushTimer() {
226
- this.internal.flushTimer && clearInterval(this.internal.flushTimer);
227
- this.internal.flushTimer = setInterval(
228
- () => this.flush(),
229
- import_constants.DEFAULT_MAX_WAIT_MS
230
- );
231
- }
232
- /** 校验 module 是否有效(非空且非纯空白) */
233
- isValidModule(mod) {
234
- return !!(mod && mod.trim());
235
- }
236
- /** 获取过滤后的模块层级(去除空项和空白) */
237
- getFilteredModules() {
238
- return (this.getPageModules() || []).filter((m) => m && m.trim());
239
- }
240
- /** 若日志缺少 module,则尝试用路由模块补齐;返回是否有效 */
241
- fillModuleIfMissing(profile, modules) {
242
- if (!profile.module && modules.length) {
243
- profile.module = modules.join("-");
244
- }
245
- return this.isValidModule(profile.module);
246
- }
247
- /* ================= rrweb 录制相关 ================= */
248
- maybeStartReplay() {
249
- if (!this.config.enableReplay)
250
- return;
251
- this.replay = new import_replay.ReplayManager(this.config);
252
- this.replay.start();
253
- }
254
- stopReplay() {
255
- var _a;
256
- (_a = this.replay) == null ? void 0 : _a.stop();
257
- }
258
- buildReplayAttachment() {
259
- var _a;
260
- return (_a = this.replay) == null ? void 0 : _a.buildAttachment();
261
- }
262
- };
263
- // Annotate the CommonJS export names for ESM import in node:
264
- 0 && (module.exports = {
265
- TrackerInstance
266
- });
@@ -1,18 +0,0 @@
1
- /**
2
- * LifecycleManager - 浏览器生命周期与错误监听的封装
3
- */
4
- import { ErrorProfile } from '../../interfaces/track.interface';
5
- type Options = {
6
- onHidden?: () => void;
7
- onError?: (error: ErrorProfile) => void;
8
- };
9
- export declare class LifecycleManager {
10
- private opts;
11
- constructor(opts: Options);
12
- private onVisibilityChange;
13
- private onWindowError;
14
- private onUnhandledRejection;
15
- mount(): void;
16
- unmount(): void;
17
- }
18
- export {};
@@ -1,74 +0,0 @@
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
-
19
- // src/Track/core/lifecycle.ts
20
- var lifecycle_exports = {};
21
- __export(lifecycle_exports, {
22
- LifecycleManager: () => LifecycleManager
23
- });
24
- module.exports = __toCommonJS(lifecycle_exports);
25
- var LifecycleManager = class {
26
- constructor(opts) {
27
- this.onVisibilityChange = () => {
28
- var _a, _b;
29
- try {
30
- if (typeof document === "undefined")
31
- return;
32
- if (document.visibilityState === "hidden")
33
- (_b = (_a = this.opts).onHidden) == null ? void 0 : _b.call(_a);
34
- } catch {
35
- }
36
- };
37
- this.onWindowError = (e) => {
38
- var _a, _b;
39
- const err = {
40
- message: e.message,
41
- fileName: e.filename
42
- };
43
- (_b = (_a = this.opts).onError) == null ? void 0 : _b.call(_a, err);
44
- };
45
- this.onUnhandledRejection = (e) => {
46
- var _a, _b, _c;
47
- const reason = e.reason;
48
- const message = reason && (reason.message || ((_a = reason.toString) == null ? void 0 : _a.call(reason))) || "unhandledrejection";
49
- (_c = (_b = this.opts).onError) == null ? void 0 : _c.call(_b, { message, fileName: "unhandledrejection" });
50
- };
51
- this.opts = opts;
52
- }
53
- mount() {
54
- if (typeof addEventListener === "undefined")
55
- return;
56
- addEventListener("visibilitychange", this.onVisibilityChange);
57
- addEventListener("error", this.onWindowError);
58
- addEventListener("unhandledrejection", this.onUnhandledRejection);
59
- }
60
- unmount() {
61
- if (typeof removeEventListener === "undefined")
62
- return;
63
- removeEventListener("visibilitychange", this.onVisibilityChange);
64
- removeEventListener("error", this.onWindowError);
65
- removeEventListener(
66
- "unhandledrejection",
67
- this.onUnhandledRejection
68
- );
69
- }
70
- };
71
- // Annotate the CommonJS export names for ESM import in node:
72
- 0 && (module.exports = {
73
- LifecycleManager
74
- });
@@ -1,5 +0,0 @@
1
- /**
2
- * 用户画像合并:优先使用权限中心信息,并叠加外部传入的 patch
3
- */
4
- import { AuthCenterUserProfile, UserProfile } from '../../interfaces/track.interface';
5
- export declare function mergeUserProfile(authCenter: AuthCenterUserProfile, external?: Partial<UserProfile>): UserProfile;
@@ -1,47 +0,0 @@
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
-
19
- // src/Track/core/profile.ts
20
- var profile_exports = {};
21
- __export(profile_exports, {
22
- mergeUserProfile: () => mergeUserProfile
23
- });
24
- module.exports = __toCommonJS(profile_exports);
25
- function mergeUserProfile(authCenter, external) {
26
- var _a, _b, _c;
27
- if (!(authCenter == null ? void 0 : authCenter.userName)) {
28
- return {
29
- userId: "",
30
- userName: "",
31
- ...external || {}
32
- };
33
- }
34
- const base = {
35
- userId: authCenter.userName,
36
- userName: authCenter.nickName,
37
- orgCode: authCenter.orgCode,
38
- orgTitle: authCenter.orgTitle,
39
- dataScopeRoles: (_a = authCenter.dataScopeRoles) == null ? void 0 : _a.map((r) => r.name).join(","),
40
- dataScopeZonePermissions: (_c = (_b = authCenter.dataScopeZonePermissions) == null ? void 0 : _b.list) == null ? void 0 : _c.map((g) => g.list.map((z) => z.zoneId).join(",")).join(",")
41
- };
42
- return { ...base, ...external || {} };
43
- }
44
- // Annotate the CommonJS export names for ESM import in node:
45
- 0 && (module.exports = {
46
- mergeUserProfile
47
- });
@@ -1,13 +0,0 @@
1
- /**
2
- * TrackQueue - 轻量埋点队列实现
3
- * 提供 push / size / drain 三个最小操作,避免引入额外依赖。
4
- */
5
- export declare class TrackQueue<T> {
6
- private items;
7
- /** 入队一个元素 */
8
- push(item: T): void;
9
- /** 当前元素数量 */
10
- size(): number;
11
- /** 取出并清空所有元素(返回快照) */
12
- drain(): T[];
13
- }
@@ -1,17 +0,0 @@
1
- import { ErrorProfile, TrackConfig } from '../../interfaces/track.interface';
2
- export declare class ReplayManager {
3
- private config;
4
- private stopRecord?;
5
- private segments;
6
- constructor(config: TrackConfig);
7
- start(): void;
8
- stop(): void;
9
- /**
10
- * 构建随错包携带的回放事件,受条数与字节限制
11
- */
12
- buildAttachment(): any[] | undefined;
13
- /**
14
- * 在回放流中注入一条“错误”标记事件,便于后续定位
15
- */
16
- markError(error: ErrorProfile): void;
17
- }
@@ -1,131 +0,0 @@
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
-
19
- // src/Track/core/replay.ts
20
- var replay_exports = {};
21
- __export(replay_exports, {
22
- ReplayManager: () => ReplayManager
23
- });
24
- module.exports = __toCommonJS(replay_exports);
25
- var import_record = require("@rrweb/record");
26
- var import_rrweb_plugin_console_record = require("@rrweb/rrweb-plugin-console-record");
27
- var import_constants = require("./constants");
28
- var ReplayManager = class {
29
- // 仅保留最近两段
30
- constructor(config) {
31
- this.segments = [[]];
32
- this.config = config;
33
- }
34
- start() {
35
- if (!this.config.enableReplay)
36
- return;
37
- if (typeof window === "undefined" || typeof document === "undefined")
38
- return;
39
- try {
40
- const maxEvents = import_constants.DEFAULT_REPLAY_MAX_EVENTS;
41
- const rrwebOptions = {
42
- checkoutEveryNms: import_constants.DEFAULT_REPLAY_CHECKOUT_NMS
43
- };
44
- this.stopRecord = (0, import_record.record)({
45
- emit: (event, isCheckout) => {
46
- if (isCheckout)
47
- this.segments.push([]);
48
- const last = this.segments[this.segments.length - 1];
49
- last.push(event);
50
- if (this.segments.length > 2)
51
- this.segments.shift();
52
- if (last.length > maxEvents)
53
- last.splice(0, last.length - maxEvents);
54
- },
55
- plugins: [(0, import_rrweb_plugin_console_record.getRecordConsolePlugin)()],
56
- ...rrwebOptions
57
- });
58
- } catch (err) {
59
- console.warn("[track:debug] replay start failed", err);
60
- }
61
- }
62
- stop() {
63
- try {
64
- this.stopRecord && this.stopRecord();
65
- this.stopRecord = void 0;
66
- } catch {
67
- }
68
- }
69
- /**
70
- * 构建随错包携带的回放事件,受条数与字节限制
71
- */
72
- buildAttachment() {
73
- if (!this.config.enableReplay)
74
- return void 0;
75
- if (!this.segments.length)
76
- return void 0;
77
- const joined = (this.segments[this.segments.length - 2] || []).concat(
78
- this.segments[this.segments.length - 1] || []
79
- );
80
- if (!joined.length)
81
- return void 0;
82
- const maxEvents = import_constants.DEFAULT_REPLAY_MAX_EVENTS;
83
- let sliced = joined.slice(-maxEvents);
84
- const maxBytes = import_constants.DEFAULT_REPLAY_MAX_BYTES;
85
- let json = JSON.stringify(sliced);
86
- if (json.length > maxBytes) {
87
- const step = Math.max(50, Math.floor(sliced.length * 0.1));
88
- while (json.length > maxBytes && sliced.length > step) {
89
- sliced = sliced.slice(step);
90
- json = JSON.stringify(sliced);
91
- }
92
- }
93
- return sliced;
94
- }
95
- /**
96
- * 在回放流中注入一条“错误”标记事件,便于后续定位
97
- */
98
- markError(error) {
99
- if (!this.config.enableReplay)
100
- return;
101
- try {
102
- const event = {
103
- // rrweb 自定义事件类型:5(EventType.Custom)。避免直接依赖枚举,使用字面量。
104
- type: 5,
105
- timestamp: Date.now(),
106
- data: {
107
- tag: "error",
108
- payload: {
109
- message: error.message,
110
- fileName: error.fileName,
111
- href: typeof location !== "undefined" && (location == null ? void 0 : location.href) ? location.href : "",
112
- userAgent: typeof navigator !== "undefined" && (navigator == null ? void 0 : navigator.userAgent) ? navigator.userAgent : ""
113
- }
114
- }
115
- };
116
- if (!this.segments.length)
117
- this.segments.push([]);
118
- const last = this.segments[this.segments.length - 1];
119
- last.push(event);
120
- const maxEvents = import_constants.DEFAULT_REPLAY_MAX_EVENTS;
121
- if (last.length > maxEvents)
122
- last.splice(0, last.length - maxEvents);
123
- } catch (e) {
124
- console.warn("[track:debug] markError failed", e);
125
- }
126
- }
127
- };
128
- // Annotate the CommonJS export names for ESM import in node:
129
- 0 && (module.exports = {
130
- ReplayManager
131
- });
@@ -1,6 +0,0 @@
1
- /**
2
- * 路由辅助:基于 base 前缀规范化路径,并从 routesMap 提取模块层级
3
- */
4
- import { RouteMapValueType, TrackConfig } from '../../interfaces/track.interface';
5
- export declare function getFormattedPath(config: TrackConfig): string;
6
- export declare function getPageModules(config: TrackConfig, routesMap: Map<string, RouteMapValueType>): string[];
@@ -1,14 +0,0 @@
1
- /**
2
- * 系统/运行环境信息采集:IMEI 生成 + 视口 + 当前 URL 等。
3
- */
4
- import { SystemProfile } from '../../interfaces/track.interface';
5
- /**
6
- * 确保存在一个持久化 IMEI(匿名设备标识)
7
- * - 优先 localStorage
8
- * - 不可用时降级随机字符串
9
- */
10
- export declare function ensureIMEI(): string;
11
- /**
12
- * 收集系统(会话)级别指标:窗口尺寸 / URL / 时间戳 / IMEI
13
- */
14
- export declare function getSystemProfile(): SystemProfile;
@@ -1,53 +0,0 @@
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
-
19
- // src/Track/core/system.ts
20
- var system_exports = {};
21
- __export(system_exports, {
22
- ensureIMEI: () => ensureIMEI,
23
- getSystemProfile: () => getSystemProfile
24
- });
25
- module.exports = __toCommonJS(system_exports);
26
- var import_Utils = require("../../Utils");
27
- var IMEI_KEY = "__IMEI__";
28
- function ensureIMEI() {
29
- try {
30
- const val = localStorage.getItem(IMEI_KEY);
31
- if (val)
32
- return val;
33
- const imei = (0, import_Utils.generateRandomString)(16);
34
- localStorage.setItem(IMEI_KEY, imei);
35
- return imei;
36
- } catch {
37
- return (0, import_Utils.generateRandomString)(16);
38
- }
39
- }
40
- function getSystemProfile() {
41
- return {
42
- innerHeight: typeof window !== "undefined" ? window.innerHeight : 0,
43
- innerWidth: typeof window !== "undefined" ? window.innerWidth : 0,
44
- href: typeof location !== "undefined" ? location.href : "",
45
- systemTime: /* @__PURE__ */ new Date(),
46
- IMEI: ensureIMEI()
47
- };
48
- }
49
- // Annotate the CommonJS export names for ESM import in node:
50
- 0 && (module.exports = {
51
- ensureIMEI,
52
- getSystemProfile
53
- });
@@ -1,18 +0,0 @@
1
- import { TrackConfig } from '../../interfaces/track.interface';
2
- /**
3
- * sendBeacon 包装:
4
- * - 统一 JSON 序列化
5
- * - debug 输出
6
- * - 失败仅打印(不抛错,避免影响业务)
7
- */
8
- export declare function beaconSend(url: string, payload: object | any[]): void;
9
- /**
10
- * 上报当前应用版本:避免重复上报,通过 localStorage 绑定 appId 做缓存
11
- */
12
- export declare function reportAppVersion(config: TrackConfig): void;
13
- /** 批量上报日志事件数组 */
14
- export declare function reportBatch(config: TrackConfig, batch: any[]): void;
15
- /** 上报单条 API 日志(独立通道) */
16
- export declare function reportApiLog(config: TrackConfig, apiLog: any, envelope: any): void;
17
- /** 上报单条错误日志到专用端点 */
18
- export declare function reportError(config: TrackConfig, envelope: any, replayPayload?: any): Promise<void>;