@yysports/tools 3.0.5 → 3.1.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 (42) hide show
  1. package/dist/Track/Beacon.d.ts +9 -2
  2. package/dist/Track/Beacon.js +24 -7
  3. package/dist/Track/Config.d.ts +5 -1
  4. package/dist/Track/Config.js +16 -2
  5. package/dist/Track/Listener.d.ts +13 -0
  6. package/dist/Track/Listener.js +38 -24
  7. package/dist/Track/Log.d.ts +7 -1
  8. package/dist/Track/Log.js +42 -17
  9. package/dist/Track/Replay.d.ts +1 -0
  10. package/dist/Track/Replay.js +33 -13
  11. package/dist/Track/index.js +6 -1
  12. package/dist/Utils/file.js +9 -2
  13. package/dist/Utils/index.d.ts +2 -2
  14. package/dist/Utils/index.js +3 -3
  15. package/dist/Utils/route.js +9 -8
  16. package/dist/index.d.ts +1 -1
  17. package/dist/index.js +2 -2
  18. package/dist/interfaces/protocol.d.ts +1 -1
  19. package/dist/interfaces/protocol.js +1 -1
  20. package/dist/interfaces/track.interface.d.ts +1 -1
  21. package/dist/interfaces/track.interface.js +1 -1
  22. package/lib/Track/Beacon.d.ts +9 -2
  23. package/lib/Track/Beacon.js +22 -5
  24. package/lib/Track/Config.d.ts +5 -1
  25. package/lib/Track/Config.js +22 -3
  26. package/lib/Track/Listener.d.ts +13 -0
  27. package/lib/Track/Listener.js +35 -24
  28. package/lib/Track/Log.d.ts +7 -1
  29. package/lib/Track/Log.js +28 -2
  30. package/lib/Track/Replay.d.ts +1 -0
  31. package/lib/Track/Replay.js +12 -1
  32. package/lib/Track/index.js +5 -1
  33. package/lib/Utils/file.js +8 -2
  34. package/lib/Utils/index.d.ts +2 -2
  35. package/lib/Utils/index.js +5 -5
  36. package/lib/Utils/route.js +7 -8
  37. package/lib/index.d.ts +1 -1
  38. package/lib/index.js +3 -3
  39. package/lib/interfaces/protocol.d.ts +1 -1
  40. package/lib/interfaces/protocol.js +1 -1
  41. package/lib/interfaces/track.interface.d.ts +1 -1
  42. package/package.json +24 -19
@@ -9,9 +9,16 @@ import { type TrackV3Event } from '../interfaces/protocol';
9
9
  export default class Beacon {
10
10
  private static _queue;
11
11
  private static _timer;
12
- /** 启动定时 flush(Track.init 时调用) */
12
+ /**
13
+ * 启动定时 flush 与卸载兜底(Track.init 时调用,重复调用无副作用)。
14
+ *
15
+ * 兜底 flush 放在这里、不放 `Listener`:那会让「是否采集错误」与「通道能否送达」耦合,
16
+ * 关掉 `autoReportError` 会连带丢掉所有不满一批的数据。
17
+ *
18
+ * `flushInterval` 只在首次创建定时器时读取,热更新重建留待 3.2.0(P1-6)。
19
+ */
13
20
  static start(): void;
14
- /** 入队,达到 batchSize 立即 flush */
21
+ /** 入队,达到协议上限立即 flush */
15
22
  static push(event: TrackV3Event): void;
16
23
  /** 按批发送并出队;发送失败同样出队(尽力而为,不驻留重试) */
17
24
  static flush(): void;
@@ -19,7 +19,15 @@ var Beacon = /*#__PURE__*/function () {
19
19
  }
20
20
  _createClass(Beacon, null, [{
21
21
  key: "start",
22
- value: /** 启动定时 flush(Track.init 时调用) */
22
+ value:
23
+ /**
24
+ * 启动定时 flush 与卸载兜底(Track.init 时调用,重复调用无副作用)。
25
+ *
26
+ * 兜底 flush 放在这里、不放 `Listener`:那会让「是否采集错误」与「通道能否送达」耦合,
27
+ * 关掉 `autoReportError` 会连带丢掉所有不满一批的数据。
28
+ *
29
+ * `flushInterval` 只在首次创建定时器时读取,热更新重建留待 3.2.0(P1-6)。
30
+ */
23
31
  function start() {
24
32
  var _this = this,
25
33
  _Config$data$flushInt;
@@ -27,20 +35,31 @@ var Beacon = /*#__PURE__*/function () {
27
35
  this._timer = setInterval(function () {
28
36
  return _this.flush();
29
37
  }, (_Config$data$flushInt = Config.data.flushInterval) !== null && _Config$data$flushInt !== void 0 ? _Config$data$flushInt : 10000);
38
+ // Node / Worker 等无 DOM 环境无法监听生命周期,只跑定时 flush
39
+ if (typeof document === 'undefined' || typeof window === 'undefined') {
40
+ return;
41
+ }
42
+ document.addEventListener('visibilitychange', function () {
43
+ if (document.visibilityState === 'hidden') {
44
+ _this.flush();
45
+ }
46
+ });
47
+ window.addEventListener('pagehide', function () {
48
+ _this.flush();
49
+ });
30
50
  }
31
51
 
32
- /** 入队,达到 batchSize 立即 flush */
52
+ /** 入队,达到协议上限立即 flush */
33
53
  }, {
34
54
  key: "push",
35
55
  value: function push(event) {
36
- var _Config$data$batchSiz;
37
56
  // 未 init(appId 为空)时报出的数据无归属、必被服务端 403/400 拒,直接丢弃并提示调用方
38
57
  if (!Config.data.appId) {
39
58
  console.warn('[Track] 尚未调用 Track.init(appId 为空),该条上报已丢弃');
40
59
  return;
41
60
  }
42
61
  this._queue.push(event);
43
- if (this._queue.length >= ((_Config$data$batchSiz = Config.data.batchSize) !== null && _Config$data$batchSiz !== void 0 ? _Config$data$batchSiz : TRACK_LIMITS.maxItems)) {
62
+ if (this._queue.length >= TRACK_LIMITS.maxItems) {
44
63
  this.flush();
45
64
  }
46
65
  }
@@ -50,9 +69,7 @@ var Beacon = /*#__PURE__*/function () {
50
69
  key: "flush",
51
70
  value: function flush() {
52
71
  while (this._queue.length > 0) {
53
- var _Config$data$batchSiz2;
54
- var batchSize = (_Config$data$batchSiz2 = Config.data.batchSize) !== null && _Config$data$batchSiz2 !== void 0 ? _Config$data$batchSiz2 : TRACK_LIMITS.maxItems;
55
- var batch = this._queue.slice(0, batchSize);
72
+ var batch = this._queue.slice(0, TRACK_LIMITS.maxItems);
56
73
  var payload = _objectSpread(_objectSpread({}, buildEnvelopeBase()), {}, {
57
74
  events: batch
58
75
  });
@@ -9,7 +9,6 @@ export interface IConfig {
9
9
  appVersion?: string;
10
10
  autoReportError?: boolean;
11
11
  enableReplay?: boolean;
12
- batchSize?: number;
13
12
  flushInterval?: number;
14
13
  replayMaxBytes?: number;
15
14
  replayMaskTextClass?: string | RegExp;
@@ -17,5 +16,10 @@ export interface IConfig {
17
16
  export default class Config {
18
17
  private static _data;
19
18
  static validate(config: IConfig): void;
19
+ /**
20
+ * `0` / 非数字 / `null` 回落默认值,其余小于下限的收敛到下限。
21
+ * `0` 按非法处理而非下限:`Number(null)` 也是 0,混在一起会让 `replayMaxBytes` 变成 1 字节。
22
+ */
23
+ private static normalize;
20
24
  static get data(): IConfig;
21
25
  }
@@ -2,7 +2,6 @@ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
2
  import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
3
3
  import _createClass from "@babel/runtime/helpers/esm/createClass";
4
4
  import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
5
- import { TRACK_LIMITS } from "../interfaces/protocol";
6
5
  var Config = /*#__PURE__*/function () {
7
6
  function Config() {
8
7
  _classCallCheck(this, Config);
@@ -25,6 +24,22 @@ var Config = /*#__PURE__*/function () {
25
24
  return value !== undefined;
26
25
  }));
27
26
  Object.assign(this._data, defined);
27
+
28
+ // 数值项归一化:setInterval(0) 会空转占满主线程,回放阈值 <= 0 会把片段截成空
29
+ this._data.flushInterval = Config.normalize(this._data.flushInterval, 10000, 1000);
30
+ this._data.replayMaxBytes = Config.normalize(this._data.replayMaxBytes, 1024 * 1024, 1);
31
+ }
32
+
33
+ /**
34
+ * `0` / 非数字 / `null` 回落默认值,其余小于下限的收敛到下限。
35
+ * `0` 按非法处理而非下限:`Number(null)` 也是 0,混在一起会让 `replayMaxBytes` 变成 1 字节。
36
+ */
37
+ }, {
38
+ key: "normalize",
39
+ value: function normalize(value, fallback, min) {
40
+ var num = Math.floor(Number(value));
41
+ if (!Number.isFinite(num) || num <= 0) return fallback;
42
+ return num < min ? min : num;
28
43
  }
29
44
  }, {
30
45
  key: "data",
@@ -39,7 +54,6 @@ _defineProperty(Config, "_data", {
39
54
  domain: '',
40
55
  autoReportError: true,
41
56
  enableReplay: true,
42
- batchSize: TRACK_LIMITS.maxItems,
43
57
  flushInterval: 10000,
44
58
  replayMaxBytes: 1024 * 1024
45
59
  });
@@ -1,5 +1,18 @@
1
1
  export default class Listener {
2
2
  private static _loaded;
3
3
  private static _reporter;
4
+ /**
5
+ * 具名 handler:`removeEventListener` 必须拿到与注册时相同的函数引用,
6
+ * 匿名函数会让 `stop()` 失效(`autoReportError` 从 true 改 false 后仍在采集)。
7
+ */
8
+ private static readonly _onError;
4
9
  static run(): void;
10
+ /**
11
+ * 停止自动上报,供 `init({ autoReportError: false })` 热关闭。
12
+ *
13
+ * 必须复位 `_loaded`:否则再次 `init({ autoReportError: true })` 时 `run()`
14
+ * 直接命中幂等判断返回,监听再也装不回来。挂起的防抖定时器同样要清掉,
15
+ * 否则关闭后仍会补报一条。
16
+ */
17
+ static stop(): void;
5
18
  }
@@ -1,6 +1,7 @@
1
1
  import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
2
2
  import _createClass from "@babel/runtime/helpers/esm/createClass";
3
3
  import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
4
+ var _class;
4
5
  import Beacon from "./Beacon";
5
6
  import Config from "./Config";
6
7
  import Log from "./Log";
@@ -27,7 +28,6 @@ var Listener = /*#__PURE__*/function () {
27
28
  _createClass(Listener, null, [{
28
29
  key: "run",
29
30
  value: function run() {
30
- var _this = this;
31
31
  if (this._loaded || !Config.data.autoReportError) {
32
32
  return;
33
33
  }
@@ -35,34 +35,48 @@ var Listener = /*#__PURE__*/function () {
35
35
  // 监听全局未捕获的错误,同时进行防抖处理,高频报错时只上报最后一条。
36
36
  // capture: true 必须保留——资源加载错误(img / script / link)不冒泡,
37
37
  // 只有捕获阶段才能在 window 上监听到,否则这类错误完全采集不到
38
- window.addEventListener('error', function (event) {
39
- if (_this._reporter) {
40
- clearTimeout(_this._reporter);
41
- }
42
- _this._reporter = setTimeout(function () {
43
- var _event$error;
44
- // 立即触发上报当前剩余的 Beacon 数据
45
- Beacon.flush();
46
- Log.errorLog(resolveErrorMessage(event), (_event$error = event.error) === null || _event$error === void 0 ? void 0 : _event$error.stack);
47
- _this._reporter = null;
48
- }, 1000);
49
- }, true);
50
-
51
- // 页面隐藏 / 卸载前兜底 flush,保证队列数据送达
52
- document.addEventListener('visibilitychange', function () {
53
- var _document;
54
- if (((_document = document) === null || _document === void 0 ? void 0 : _document.visibilityState) === 'hidden') {
55
- Beacon.flush();
56
- }
57
- });
58
- window.addEventListener('pagehide', function () {
59
- Beacon.flush();
60
- });
38
+ window.addEventListener('error', this._onError, true);
61
39
  this._loaded = true;
62
40
  }
41
+
42
+ /**
43
+ * 停止自动上报,供 `init({ autoReportError: false })` 热关闭。
44
+ *
45
+ * 必须复位 `_loaded`:否则再次 `init({ autoReportError: true })` 时 `run()`
46
+ * 直接命中幂等判断返回,监听再也装不回来。挂起的防抖定时器同样要清掉,
47
+ * 否则关闭后仍会补报一条。
48
+ */
49
+ }, {
50
+ key: "stop",
51
+ value: function stop() {
52
+ if (!this._loaded) return;
53
+ window.removeEventListener('error', this._onError, true);
54
+ if (this._reporter) {
55
+ clearTimeout(this._reporter);
56
+ this._reporter = null;
57
+ }
58
+ this._loaded = false;
59
+ }
63
60
  }]);
64
61
  return Listener;
65
62
  }();
63
+ _class = Listener;
66
64
  _defineProperty(Listener, "_loaded", false);
67
65
  _defineProperty(Listener, "_reporter", null);
66
+ /**
67
+ * 具名 handler:`removeEventListener` 必须拿到与注册时相同的函数引用,
68
+ * 匿名函数会让 `stop()` 失效(`autoReportError` 从 true 改 false 后仍在采集)。
69
+ */
70
+ _defineProperty(Listener, "_onError", function (event) {
71
+ if (_class._reporter) {
72
+ clearTimeout(_class._reporter);
73
+ }
74
+ _class._reporter = setTimeout(function () {
75
+ var _event$error;
76
+ // 立即触发上报当前剩余的 Beacon 数据
77
+ Beacon.flush();
78
+ Log.errorLog(resolveErrorMessage(event), (_event$error = event.error) === null || _event$error === void 0 ? void 0 : _event$error.stack);
79
+ _class._reporter = null;
80
+ }, 1000);
81
+ });
68
82
  export { Listener as default };
@@ -14,7 +14,13 @@ export default class Log {
14
14
  * 故选择丢弃 + `console.warn`——联调期即可暴露,且不会让同批其他条一起被 400 拒掉。
15
15
  */
16
16
  private static isContentOverlong;
17
- static visitLog(module: string): void;
17
+ /**
18
+ * 页面上报。
19
+ *
20
+ * `module` 为空属调用误用(如路由工具返回 `undefined`):`_module` 会被后续事件复用,
21
+ * 记成空串会让之后所有事件的归属一起变空,故丢弃并告警,与 `errorLog` 处理一致。
22
+ */
23
+ static visitLog(module?: string): void;
18
24
  static eventLog(content: string): void;
19
25
  static apiLog(log: string): void;
20
26
  /**
package/dist/Track/Log.js CHANGED
@@ -6,6 +6,7 @@ import _createClass from "@babel/runtime/helpers/esm/createClass";
6
6
  import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
7
7
  import { TRACK_CONTENT_TYPE, TRACK_LIMITS } from "../interfaces/protocol";
8
8
  import Beacon from "./Beacon";
9
+ import Config from "./Config";
9
10
  import { buildEnvelopeBase, buildEventBase, trackUrl, truncate } from "./envelope";
10
11
  import Replay from "./Replay";
11
12
 
@@ -42,22 +43,39 @@ var Log = /*#__PURE__*/function () {
42
43
  console.warn("[Track] content \u957F\u5EA6 ".concat(content.length, " \u8D85\u8FC7\u4E0A\u9650 ").concat(TRACK_LIMITS.content, "\uFF08\u670D\u52A1\u7AEF\u4F1A\u6574\u6279 400\uFF09\uFF0C\u5DF2\u4E22\u5F03\u8BE5\u6761\u65E5\u5FD7"));
43
44
  return true;
44
45
  }
46
+
47
+ /**
48
+ * 页面上报。
49
+ *
50
+ * `module` 为空属调用误用(如路由工具返回 `undefined`):`_module` 会被后续事件复用,
51
+ * 记成空串会让之后所有事件的归属一起变空,故丢弃并告警,与 `errorLog` 处理一致。
52
+ */
45
53
  }, {
46
54
  key: "visitLog",
47
55
  value: function visitLog(module) {
48
- // module 会缓存在 _module 上被后续 event / api 事件复用,超列宽时截断而非丢弃:
49
- // 丢弃不仅丢掉本次访问,还会让后续所有带 module 的事件一并消失
56
+ var _module$split$pop;
57
+ if (typeof module !== 'string' || !module) {
58
+ console.warn('[Track] visitLog 的 module 为空(服务端 module 为可选项,空值会让后续事件失去归属),已丢弃该条');
59
+ return;
60
+ }
50
61
  this._module = truncate(module, TRACK_LIMITS.module);
51
- var lastModule = this._module.split('-').pop();
62
+ // 末段取自原始 module:`_module` 已按 255 截断,从它切会把末段从中间切断
63
+ var lastModule = (_module$split$pop = module.split('-').pop()) !== null && _module$split$pop !== void 0 ? _module$split$pop : '';
52
64
  Beacon.push(_objectSpread(_objectSpread({}, buildEventBase()), {}, {
53
65
  type: 'visit',
54
66
  module: this._module,
55
- content: "\u8BBF\u95EE\u9875\u9762\uFF1A".concat(lastModule)
67
+ // content 列宽 255 且自带 5 字前缀,必须整体截断,否则超限会让同批其他日志一并 400
68
+ content: truncate("\u8BBF\u95EE\u9875\u9762\uFF1A".concat(lastModule), TRACK_LIMITS.content)
56
69
  }));
57
70
  }
58
71
  }, {
59
72
  key: "eventLog",
60
73
  value: function eventLog(content) {
74
+ // 非字符串会被原样入队,服务端校验失败会连坐同批;与超长处理一致,丢弃并告警
75
+ if (typeof content !== 'string') {
76
+ console.warn('[Track] eventLog 的 content 不是字符串(服务端会整批 400),已丢弃该条日志');
77
+ return;
78
+ }
61
79
  if (this.isContentOverlong(content)) return;
62
80
  Beacon.push(_objectSpread(_objectSpread({}, buildEventBase()), {}, {
63
81
  type: 'event',
@@ -88,13 +106,20 @@ var Log = /*#__PURE__*/function () {
88
106
  return _regeneratorRuntime().wrap(function _callee$(_context) {
89
107
  while (1) switch (_context.prev = _context.next) {
90
108
  case 0:
91
- if (!(typeof message !== 'string' || !message.trim())) {
109
+ if (Config.data.appId) {
92
110
  _context.next = 3;
93
111
  break;
94
112
  }
95
- console.warn('[Track] errorLog 的 message 为空(服务端会整条 400),已丢弃该条错误上报');
113
+ console.warn('[Track] 尚未调用 Track.init(appId 为空),该条错误上报已丢弃');
96
114
  return _context.abrupt("return");
97
115
  case 3:
116
+ if (!(typeof message !== 'string' || !message.trim())) {
117
+ _context.next = 6;
118
+ break;
119
+ }
120
+ console.warn('[Track] errorLog 的 message 为空(服务端会整条 400),已丢弃该条错误上报');
121
+ return _context.abrupt("return");
122
+ case 6:
98
123
  replay = Replay.snapshot();
99
124
  pureError = _objectSpread(_objectSpread({}, buildEnvelopeBase()), {}, {
100
125
  error: _objectSpread(_objectSpread({}, buildEventBase()), {}, {
@@ -106,8 +131,8 @@ var Log = /*#__PURE__*/function () {
106
131
  payload = replay.length ? _objectSpread(_objectSpread({}, pureError), {}, {
107
132
  replay: replay
108
133
  }) : pureError;
109
- _context.prev = 6;
110
- _context.next = 9;
134
+ _context.prev = 9;
135
+ _context.next = 12;
111
136
  return fetch(trackUrl('/error'), {
112
137
  method: 'POST',
113
138
  headers: {
@@ -115,21 +140,21 @@ var Log = /*#__PURE__*/function () {
115
140
  },
116
141
  body: JSON.stringify(payload)
117
142
  });
118
- case 9:
143
+ case 12:
119
144
  res = _context.sent;
120
145
  if (res.ok) {
121
- _context.next = 12;
146
+ _context.next = 15;
122
147
  break;
123
148
  }
124
149
  throw new Error("HTTP ".concat(res.status));
125
- case 12:
150
+ case 15:
126
151
  // 上报成功后才移除已携带的回放事件,继续录制(不 stop)
127
152
  Replay.commitSnapshot(replay);
128
- _context.next = 18;
153
+ _context.next = 21;
129
154
  break;
130
- case 15:
131
- _context.prev = 15;
132
- _context.t0 = _context["catch"](6);
155
+ case 18:
156
+ _context.prev = 18;
157
+ _context.t0 = _context["catch"](9);
133
158
  try {
134
159
  (_navigator = navigator) === null || _navigator === void 0 || (_navigator$sendBeacon = _navigator.sendBeacon) === null || _navigator$sendBeacon === void 0 || _navigator$sendBeacon.call(_navigator, trackUrl('/error'), new Blob([JSON.stringify(pureError)], {
135
160
  type: TRACK_CONTENT_TYPE
@@ -137,11 +162,11 @@ var Log = /*#__PURE__*/function () {
137
162
  } catch (beaconError) {
138
163
  // beacon 也不可用(如页面已卸载),放弃本次错误上报,不阻断业务
139
164
  }
140
- case 18:
165
+ case 21:
141
166
  case "end":
142
167
  return _context.stop();
143
168
  }
144
- }, _callee, this, [[6, 15]]);
169
+ }, _callee, this, [[9, 18]]);
145
170
  }));
146
171
  function errorLog(_x, _x2) {
147
172
  return _errorLog.apply(this, arguments);
@@ -4,6 +4,7 @@ export default class Replay {
4
4
  private static _stop;
5
5
  private static _record;
6
6
  private static _loading;
7
+ private static _stopRequested;
7
8
  private static _bytes;
8
9
  /**
9
10
  * 启动录制。
@@ -42,21 +42,36 @@ var Replay = /*#__PURE__*/function () {
42
42
  return _regeneratorRuntime().wrap(function _callee$(_context) {
43
43
  while (1) switch (_context.prev = _context.next) {
44
44
  case 0:
45
- if (!(this._stop || this._loading || !Config.data.enableReplay)) {
45
+ if (Config.data.enableReplay) {
46
46
  _context.next = 2;
47
47
  break;
48
48
  }
49
49
  return _context.abrupt("return");
50
50
  case 2:
51
+ // 复位必须在 `_loading` 判断之前:加载中关掉再打开时,入口会被 `_loading` 挡回,
52
+ // 复位不到标志,加载完成后就会误判为「已请求停止」而放弃启动
53
+ this._stopRequested = false;
54
+ if (!(this._stop || this._loading)) {
55
+ _context.next = 5;
56
+ break;
57
+ }
58
+ return _context.abrupt("return");
59
+ case 5:
51
60
  this._loading = true;
52
- _context.prev = 3;
53
- _context.next = 6;
61
+ _context.prev = 6;
62
+ _context.next = 9;
54
63
  return Promise.all([import('@rrweb/record'), import('@rrweb/rrweb-plugin-console-record')]);
55
- case 6:
64
+ case 9:
56
65
  _yield$Promise$all = _context.sent;
57
66
  _yield$Promise$all2 = _slicedToArray(_yield$Promise$all, 2);
58
67
  startRecord = _yield$Promise$all2[0].record;
59
68
  getRecordConsolePlugin = _yield$Promise$all2[1].getRecordConsolePlugin;
69
+ if (!(this._stopRequested || !Config.data.enableReplay)) {
70
+ _context.next = 15;
71
+ break;
72
+ }
73
+ return _context.abrupt("return");
74
+ case 15:
60
75
  this._record = startRecord;
61
76
  this._stop = startRecord(_objectSpread(_objectSpread({
62
77
  emit: function emit(e) {
@@ -76,23 +91,23 @@ var Replay = /*#__PURE__*/function () {
76
91
  level: ['log', 'info', 'warn', 'error', 'debug']
77
92
  })]
78
93
  }));
79
- _context.next = 18;
94
+ _context.next = 23;
80
95
  break;
81
- case 14:
82
- _context.prev = 14;
83
- _context.t0 = _context["catch"](3);
96
+ case 19:
97
+ _context.prev = 19;
98
+ _context.t0 = _context["catch"](6);
84
99
  // 加载或启动失败:静默降级,仅回放不可用
85
100
  this._stop = undefined;
86
101
  this._record = undefined;
87
- case 18:
88
- _context.prev = 18;
102
+ case 23:
103
+ _context.prev = 23;
89
104
  this._loading = false;
90
- return _context.finish(18);
91
- case 21:
105
+ return _context.finish(23);
106
+ case 26:
92
107
  case "end":
93
108
  return _context.stop();
94
109
  }
95
- }, _callee, this, [[3, 14, 18, 21]]);
110
+ }, _callee, this, [[6, 19, 23, 26]]);
96
111
  }));
97
112
  function run() {
98
113
  return _run.apply(this, arguments);
@@ -109,10 +124,13 @@ var Replay = /*#__PURE__*/function () {
109
124
  }, {
110
125
  key: "stop",
111
126
  value: function stop() {
127
+ // 先置标志:rrweb 仍在加载时 `_stop` 还不存在,只有它能拦住随后的启动
128
+ this._stopRequested = true;
112
129
  if (this._stop) {
113
130
  this._stop();
114
131
  }
115
132
  this._stop = undefined;
133
+ this._record = undefined;
116
134
  this._events = [];
117
135
  this._bytes = 0;
118
136
  }
@@ -210,6 +228,8 @@ _defineProperty(Replay, "_stop", void 0);
210
228
  _defineProperty(Replay, "_record", void 0);
211
229
  // rrweb 异步加载中的标记:避免 init 被重复调用时启动两个录制实例
212
230
  _defineProperty(Replay, "_loading", false);
231
+ // 加载窗口内被 stop 的标记:rrweb 就绪后据此放弃启动
232
+ _defineProperty(Replay, "_stopRequested", false);
213
233
  // 录制阶段的累计体积(近似值),达到 replayMaxBytes 后整体重开一段
214
234
  _defineProperty(Replay, "_bytes", 0);
215
235
  export { Replay as default };
@@ -43,7 +43,12 @@ var Track = /*#__PURE__*/function () {
43
43
  }, {
44
44
  key: "run",
45
45
  value: function run() {
46
- Listener.run();
46
+ // 布尔项可热更新:关掉时显式卸载监听,打开时(幂等标志已复位)重新注册
47
+ if (Config.data.autoReportError) {
48
+ Listener.run();
49
+ } else {
50
+ Listener.stop();
51
+ }
47
52
  if (Config.data.enableReplay) {
48
53
  // 回放为异步启动(rrweb 懒加载),不阻塞 init;未开回放时不会加载 rrweb
49
54
  void Replay.run();
@@ -35,14 +35,21 @@ export var checkFileType = function checkFileType(file, accept) {
35
35
  * @param file File 图片文件对象
36
36
  */
37
37
  export var getImageResolution = function getImageResolution(file) {
38
- return new Promise(function (resolve) {
38
+ return new Promise(function (resolve, reject) {
39
39
  var img = new Image();
40
- img.src = URL.createObjectURL(file);
40
+ var url = URL.createObjectURL(file);
41
41
  img.onload = function () {
42
+ URL.revokeObjectURL(url);
42
43
  resolve({
43
44
  width: img.width,
44
45
  height: img.height
45
46
  });
46
47
  };
48
+ // 不处理 error 会让 Promise 永远挂起,调用方的 await 无法结束
49
+ img.onerror = function () {
50
+ URL.revokeObjectURL(url);
51
+ reject(new Error('getImageResolution: 图片加载失败'));
52
+ };
53
+ img.src = url;
47
54
  });
48
55
  };
@@ -1,6 +1,6 @@
1
+ export * from './auth';
1
2
  export * from './common';
2
3
  export * from './file';
4
+ export * from './route';
3
5
  export * from './url';
4
6
  export * from './wx';
5
- export * from './route';
6
- export * from './auth';
@@ -1,6 +1,6 @@
1
+ export * from "./auth";
1
2
  export * from "./common";
2
3
  export * from "./file";
3
- export * from "./url";
4
- export * from "./wx";
5
4
  export * from "./route";
6
- export * from "./auth";
5
+ export * from "./url";
6
+ export * from "./wx";
@@ -9,10 +9,11 @@ export var getModuleNameByUmi = function getModuleNameByUmi(onRouteChangeArgs, m
9
9
  routes = onRouteChangeArgs.routes,
10
10
  basename = onRouteChangeArgs.basename;
11
11
  var pathname = location.pathname;
12
- var routePath = pathname;
13
- if (basename !== '/') {
14
- routePath = pathname.replace(basename, '/');
15
- }
12
+ // 只去前缀:裸 replace 会把路径中段出现的 basename 一并替换掉;
13
+ // 末尾斜杠先规整,且要求是完整路径段,避免 /app 误匹配 /application
14
+ var basePath = basename.replace(/\/+$/, '');
15
+ var isUnderBase = basePath !== '' && basePath !== '/' && (pathname === basePath || pathname.startsWith("".concat(basePath, "/")));
16
+ var routePath = isUnderBase ? pathname.slice(basePath.length) || '/' : pathname;
16
17
  var currentRouteKey = Object.keys(routes).find(function (v) {
17
18
  return routes[v].path === routePath;
18
19
  });
@@ -43,14 +44,14 @@ export var getModuleNameByUmi = function getModuleNameByUmi(onRouteChangeArgs, m
43
44
  }
44
45
 
45
46
  // 构建完整的菜单显示名称数组
47
+ var nameMap = menuNameMap !== null && menuNameMap !== void 0 ? menuNameMap : {};
46
48
  var menuDisplayNames = [];
47
49
  for (var i = 1; i < menuHierarchyKeys.length; i++) {
48
50
  var menuKey = menuHierarchyKeys.slice(0, i + 1).join('.');
49
- var displayName = menuNameMap[menuKey];
51
+ var displayName = nameMap[menuKey];
52
+ // 缺映射时整段不可用:拼进 "undefined" 会让 visitLog 记成脏模块名
53
+ if (!displayName) return;
50
54
  menuDisplayNames.push(displayName);
51
55
  }
52
- if (menuDisplayNames.length === 0) {
53
- return;
54
- }
55
56
  return menuDisplayNames.join('-');
56
57
  };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export { default as Track } from './Track';
2
2
  export * as Utils from './Utils';
3
- export * from './interfaces/track.interface';
4
3
  export * from './interfaces/protocol';
4
+ export * from './interfaces/track.interface';
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  export { default as Track } from "./Track";
2
2
  import * as _Utils from "./Utils";
3
3
  export { _Utils as Utils };
4
- export * from "./interfaces/track.interface";
5
- export * from "./interfaces/protocol";
4
+ export * from "./interfaces/protocol";
5
+ export * from "./interfaces/track.interface";
@@ -18,7 +18,7 @@ export declare const TrackTypeLabel: Record<TrackType, string>;
18
18
  * 不做无依据的额外限制——超长字段本身即会写入失败。
19
19
  */
20
20
  export declare const TRACK_LIMITS: {
21
- /** 单次批量 events[] 条数上限(SDK batchSize 与服务端 DTO 共用) */
21
+ /** 单次批量 events[] 条数上限(SDK 切批与服务端 DTO 共用,双方都不可调) */
22
22
  readonly maxItems: 20;
23
23
  /** 探针版本号,guard_conf.version varchar(20) */
24
24
  readonly sdkVersion: 20;
@@ -26,7 +26,7 @@ export var TrackTypeLabel = {
26
26
  * 不做无依据的额外限制——超长字段本身即会写入失败。
27
27
  */
28
28
  export var TRACK_LIMITS = {
29
- /** 单次批量 events[] 条数上限(SDK batchSize 与服务端 DTO 共用) */
29
+ /** 单次批量 events[] 条数上限(SDK 切批与服务端 DTO 共用,双方都不可调) */
30
30
  maxItems: 20,
31
31
  /** 探针版本号,guard_conf.version varchar(20) */
32
32
  sdkVersion: 20,
@@ -1,4 +1,4 @@
1
- export { TrackTypeLabel, TRACK_TYPE_VALUES } from './protocol';
1
+ export { TRACK_TYPE_VALUES, TrackTypeLabel } from './protocol';
2
2
  export type { TrackType } from './protocol';
3
3
  export interface UserProfile {
4
4
  userId: string;
@@ -1,6 +1,6 @@
1
1
  // 上报类型取值与展示标签统一由 protocol.ts 维护(唯一真源),此处仅做出口。
2
2
  // 类型必须用 `export type` 再导出:值语法会让构建产物出现运行时再导出(TrackType 在运行时不存在)
3
- export { TrackTypeLabel, TRACK_TYPE_VALUES } from "./protocol";
3
+ export { TRACK_TYPE_VALUES, TrackTypeLabel } from "./protocol";
4
4
 
5
5
  // 用户信息
6
6
 
@@ -9,9 +9,16 @@ import { type TrackV3Event } from '../interfaces/protocol';
9
9
  export default class Beacon {
10
10
  private static _queue;
11
11
  private static _timer;
12
- /** 启动定时 flush(Track.init 时调用) */
12
+ /**
13
+ * 启动定时 flush 与卸载兜底(Track.init 时调用,重复调用无副作用)。
14
+ *
15
+ * 兜底 flush 放在这里、不放 `Listener`:那会让「是否采集错误」与「通道能否送达」耦合,
16
+ * 关掉 `autoReportError` 会连带丢掉所有不满一批的数据。
17
+ *
18
+ * `flushInterval` 只在首次创建定时器时读取,热更新重建留待 3.2.0(P1-6)。
19
+ */
13
20
  static start(): void;
14
- /** 入队,达到 batchSize 立即 flush */
21
+ /** 入队,达到协议上限立即 flush */
15
22
  static push(event: TrackV3Event): void;
16
23
  /** 按批发送并出队;发送失败同样出队(尽力而为,不驻留重试) */
17
24
  static flush(): void;
@@ -36,7 +36,14 @@ var import_protocol = require("../interfaces/protocol");
36
36
  var import_Config = __toESM(require("./Config"));
37
37
  var import_envelope = require("./envelope");
38
38
  var Beacon = class {
39
- /** 启动定时 flush(Track.init 时调用) */
39
+ /**
40
+ * 启动定时 flush 与卸载兜底(Track.init 时调用,重复调用无副作用)。
41
+ *
42
+ * 兜底 flush 放在这里、不放 `Listener`:那会让「是否采集错误」与「通道能否送达」耦合,
43
+ * 关掉 `autoReportError` 会连带丢掉所有不满一批的数据。
44
+ *
45
+ * `flushInterval` 只在首次创建定时器时读取,热更新重建留待 3.2.0(P1-6)。
46
+ */
40
47
  static start() {
41
48
  if (this._timer)
42
49
  return;
@@ -44,23 +51,33 @@ var Beacon = class {
44
51
  () => this.flush(),
45
52
  import_Config.default.data.flushInterval ?? 1e4
46
53
  );
54
+ if (typeof document === "undefined" || typeof window === "undefined") {
55
+ return;
56
+ }
57
+ document.addEventListener("visibilitychange", () => {
58
+ if (document.visibilityState === "hidden") {
59
+ this.flush();
60
+ }
61
+ });
62
+ window.addEventListener("pagehide", () => {
63
+ this.flush();
64
+ });
47
65
  }
48
- /** 入队,达到 batchSize 立即 flush */
66
+ /** 入队,达到协议上限立即 flush */
49
67
  static push(event) {
50
68
  if (!import_Config.default.data.appId) {
51
69
  console.warn("[Track] 尚未调用 Track.init(appId 为空),该条上报已丢弃");
52
70
  return;
53
71
  }
54
72
  this._queue.push(event);
55
- if (this._queue.length >= (import_Config.default.data.batchSize ?? import_protocol.TRACK_LIMITS.maxItems)) {
73
+ if (this._queue.length >= import_protocol.TRACK_LIMITS.maxItems) {
56
74
  this.flush();
57
75
  }
58
76
  }
59
77
  /** 按批发送并出队;发送失败同样出队(尽力而为,不驻留重试) */
60
78
  static flush() {
61
79
  while (this._queue.length > 0) {
62
- const batchSize = import_Config.default.data.batchSize ?? import_protocol.TRACK_LIMITS.maxItems;
63
- const batch = this._queue.slice(0, batchSize);
80
+ const batch = this._queue.slice(0, import_protocol.TRACK_LIMITS.maxItems);
64
81
  const payload = {
65
82
  ...(0, import_envelope.buildEnvelopeBase)(),
66
83
  events: batch
@@ -9,7 +9,6 @@ export interface IConfig {
9
9
  appVersion?: string;
10
10
  autoReportError?: boolean;
11
11
  enableReplay?: boolean;
12
- batchSize?: number;
13
12
  flushInterval?: number;
14
13
  replayMaxBytes?: number;
15
14
  replayMaskTextClass?: string | RegExp;
@@ -17,5 +16,10 @@ export interface IConfig {
17
16
  export default class Config {
18
17
  private static _data;
19
18
  static validate(config: IConfig): void;
19
+ /**
20
+ * `0` / 非数字 / `null` 回落默认值,其余小于下限的收敛到下限。
21
+ * `0` 按非法处理而非下限:`Number(null)` 也是 0,混在一起会让 `replayMaxBytes` 变成 1 字节。
22
+ */
23
+ private static normalize;
20
24
  static get data(): IConfig;
21
25
  }
@@ -22,8 +22,7 @@ __export(Config_exports, {
22
22
  default: () => Config
23
23
  });
24
24
  module.exports = __toCommonJS(Config_exports);
25
- var import_protocol = require("../interfaces/protocol");
26
- var Config = class {
25
+ var _Config = class {
27
26
  static validate(config) {
28
27
  var _a, _b;
29
28
  if (!((_a = config.appId) == null ? void 0 : _a.trim())) {
@@ -36,17 +35,37 @@ var Config = class {
36
35
  Object.entries(config).filter(([, value]) => value !== void 0)
37
36
  );
38
37
  Object.assign(this._data, defined);
38
+ this._data.flushInterval = _Config.normalize(
39
+ this._data.flushInterval,
40
+ 1e4,
41
+ 1e3
42
+ );
43
+ this._data.replayMaxBytes = _Config.normalize(
44
+ this._data.replayMaxBytes,
45
+ 1024 * 1024,
46
+ 1
47
+ );
48
+ }
49
+ /**
50
+ * `0` / 非数字 / `null` 回落默认值,其余小于下限的收敛到下限。
51
+ * `0` 按非法处理而非下限:`Number(null)` 也是 0,混在一起会让 `replayMaxBytes` 变成 1 字节。
52
+ */
53
+ static normalize(value, fallback, min) {
54
+ const num = Math.floor(Number(value));
55
+ if (!Number.isFinite(num) || num <= 0)
56
+ return fallback;
57
+ return num < min ? min : num;
39
58
  }
40
59
  static get data() {
41
60
  return this._data;
42
61
  }
43
62
  };
63
+ var Config = _Config;
44
64
  Config._data = {
45
65
  appId: "",
46
66
  domain: "",
47
67
  autoReportError: true,
48
68
  enableReplay: true,
49
- batchSize: import_protocol.TRACK_LIMITS.maxItems,
50
69
  flushInterval: 1e4,
51
70
  replayMaxBytes: 1024 * 1024
52
71
  };
@@ -1,5 +1,18 @@
1
1
  export default class Listener {
2
2
  private static _loaded;
3
3
  private static _reporter;
4
+ /**
5
+ * 具名 handler:`removeEventListener` 必须拿到与注册时相同的函数引用,
6
+ * 匿名函数会让 `stop()` 失效(`autoReportError` 从 true 改 false 后仍在采集)。
7
+ */
8
+ private static readonly _onError;
4
9
  static run(): void;
10
+ /**
11
+ * 停止自动上报,供 `init({ autoReportError: false })` 热关闭。
12
+ *
13
+ * 必须复位 `_loaded`:否则再次 `init({ autoReportError: true })` 时 `run()`
14
+ * 直接命中幂等判断返回,监听再也装不回来。挂起的防抖定时器同样要清掉,
15
+ * 否则关闭后仍会补报一条。
16
+ */
17
+ static stop(): void;
5
18
  }
@@ -44,36 +44,47 @@ function resolveErrorMessage(event) {
44
44
  const failedUrl = (target == null ? void 0 : target.src) ?? (target == null ? void 0 : target.href);
45
45
  return failedUrl ? `资源加载失败:${failedUrl}` : "";
46
46
  }
47
- var Listener = class {
47
+ var _Listener = class {
48
48
  static run() {
49
49
  if (this._loaded || !import_Config.default.data.autoReportError) {
50
50
  return;
51
51
  }
52
- window.addEventListener(
53
- "error",
54
- (event) => {
55
- if (this._reporter) {
56
- clearTimeout(this._reporter);
57
- }
58
- this._reporter = setTimeout(() => {
59
- var _a;
60
- import_Beacon.default.flush();
61
- import_Log.default.errorLog(resolveErrorMessage(event), (_a = event.error) == null ? void 0 : _a.stack);
62
- this._reporter = null;
63
- }, 1e3);
64
- },
65
- true
66
- );
67
- document.addEventListener("visibilitychange", () => {
68
- if ((document == null ? void 0 : document.visibilityState) === "hidden") {
69
- import_Beacon.default.flush();
70
- }
71
- });
72
- window.addEventListener("pagehide", () => {
73
- import_Beacon.default.flush();
74
- });
52
+ window.addEventListener("error", this._onError, true);
75
53
  this._loaded = true;
76
54
  }
55
+ /**
56
+ * 停止自动上报,供 `init({ autoReportError: false })` 热关闭。
57
+ *
58
+ * 必须复位 `_loaded`:否则再次 `init({ autoReportError: true })` 时 `run()`
59
+ * 直接命中幂等判断返回,监听再也装不回来。挂起的防抖定时器同样要清掉,
60
+ * 否则关闭后仍会补报一条。
61
+ */
62
+ static stop() {
63
+ if (!this._loaded)
64
+ return;
65
+ window.removeEventListener("error", this._onError, true);
66
+ if (this._reporter) {
67
+ clearTimeout(this._reporter);
68
+ this._reporter = null;
69
+ }
70
+ this._loaded = false;
71
+ }
77
72
  };
73
+ var Listener = _Listener;
78
74
  Listener._loaded = false;
79
75
  Listener._reporter = null;
76
+ /**
77
+ * 具名 handler:`removeEventListener` 必须拿到与注册时相同的函数引用,
78
+ * 匿名函数会让 `stop()` 失效(`autoReportError` 从 true 改 false 后仍在采集)。
79
+ */
80
+ Listener._onError = (event) => {
81
+ if (_Listener._reporter) {
82
+ clearTimeout(_Listener._reporter);
83
+ }
84
+ _Listener._reporter = setTimeout(() => {
85
+ var _a;
86
+ import_Beacon.default.flush();
87
+ import_Log.default.errorLog(resolveErrorMessage(event), (_a = event.error) == null ? void 0 : _a.stack);
88
+ _Listener._reporter = null;
89
+ }, 1e3);
90
+ };
@@ -14,7 +14,13 @@ export default class Log {
14
14
  * 故选择丢弃 + `console.warn`——联调期即可暴露,且不会让同批其他条一起被 400 拒掉。
15
15
  */
16
16
  private static isContentOverlong;
17
- static visitLog(module: string): void;
17
+ /**
18
+ * 页面上报。
19
+ *
20
+ * `module` 为空属调用误用(如路由工具返回 `undefined`):`_module` 会被后续事件复用,
21
+ * 记成空串会让之后所有事件的归属一起变空,故丢弃并告警,与 `errorLog` 处理一致。
22
+ */
23
+ static visitLog(module?: string): void;
18
24
  static eventLog(content: string): void;
19
25
  static apiLog(log: string): void;
20
26
  /**
package/lib/Track/Log.js CHANGED
@@ -34,6 +34,7 @@ __export(Log_exports, {
34
34
  module.exports = __toCommonJS(Log_exports);
35
35
  var import_protocol = require("../interfaces/protocol");
36
36
  var import_Beacon = __toESM(require("./Beacon"));
37
+ var import_Config = __toESM(require("./Config"));
37
38
  var import_envelope = require("./envelope");
38
39
  var import_Replay = __toESM(require("./Replay"));
39
40
  var Log = class {
@@ -56,17 +57,36 @@ var Log = class {
56
57
  );
57
58
  return true;
58
59
  }
60
+ /**
61
+ * 页面上报。
62
+ *
63
+ * `module` 为空属调用误用(如路由工具返回 `undefined`):`_module` 会被后续事件复用,
64
+ * 记成空串会让之后所有事件的归属一起变空,故丢弃并告警,与 `errorLog` 处理一致。
65
+ */
59
66
  static visitLog(module2) {
67
+ if (typeof module2 !== "string" || !module2) {
68
+ console.warn(
69
+ "[Track] visitLog 的 module 为空(服务端 module 为可选项,空值会让后续事件失去归属),已丢弃该条"
70
+ );
71
+ return;
72
+ }
60
73
  this._module = (0, import_envelope.truncate)(module2, import_protocol.TRACK_LIMITS.module);
61
- const lastModule = this._module.split("-").pop();
74
+ const lastModule = module2.split("-").pop() ?? "";
62
75
  import_Beacon.default.push({
63
76
  ...(0, import_envelope.buildEventBase)(),
64
77
  type: "visit",
65
78
  module: this._module,
66
- content: `访问页面:${lastModule}`
79
+ // content 列宽 255 且自带 5 字前缀,必须整体截断,否则超限会让同批其他日志一并 400
80
+ content: (0, import_envelope.truncate)(`访问页面:${lastModule}`, import_protocol.TRACK_LIMITS.content)
67
81
  });
68
82
  }
69
83
  static eventLog(content) {
84
+ if (typeof content !== "string") {
85
+ console.warn(
86
+ "[Track] eventLog 的 content 不是字符串(服务端会整批 400),已丢弃该条日志"
87
+ );
88
+ return;
89
+ }
70
90
  if (this.isContentOverlong(content))
71
91
  return;
72
92
  import_Beacon.default.push({
@@ -91,6 +111,12 @@ var Log = class {
91
111
  */
92
112
  static async errorLog(message, stack) {
93
113
  var _a;
114
+ if (!import_Config.default.data.appId) {
115
+ console.warn(
116
+ "[Track] 尚未调用 Track.init(appId 为空),该条错误上报已丢弃"
117
+ );
118
+ return;
119
+ }
94
120
  if (typeof message !== "string" || !message.trim()) {
95
121
  console.warn(
96
122
  "[Track] errorLog 的 message 为空(服务端会整条 400),已丢弃该条错误上报"
@@ -4,6 +4,7 @@ export default class Replay {
4
4
  private static _stop;
5
5
  private static _record;
6
6
  private static _loading;
7
+ private static _stopRequested;
7
8
  private static _bytes;
8
9
  /**
9
10
  * 启动录制。
@@ -46,7 +46,11 @@ var _Replay = class {
46
46
  * 错误本体的上报不受影响)。
47
47
  */
48
48
  static async run() {
49
- if (this._stop || this._loading || !import_Config.default.data.enableReplay) {
49
+ if (!import_Config.default.data.enableReplay) {
50
+ return;
51
+ }
52
+ this._stopRequested = false;
53
+ if (this._stop || this._loading) {
50
54
  return;
51
55
  }
52
56
  this._loading = true;
@@ -55,6 +59,9 @@ var _Replay = class {
55
59
  import("@rrweb/record"),
56
60
  import("@rrweb/rrweb-plugin-console-record")
57
61
  ]);
62
+ if (this._stopRequested || !import_Config.default.data.enableReplay) {
63
+ return;
64
+ }
58
65
  this._record = startRecord;
59
66
  this._stop = startRecord({
60
67
  emit: (e) => {
@@ -86,10 +93,12 @@ var _Replay = class {
86
93
  * 因此 `Track.run` 在关闭回放时显式调用本方法。
87
94
  */
88
95
  static stop() {
96
+ this._stopRequested = true;
89
97
  if (this._stop) {
90
98
  this._stop();
91
99
  }
92
100
  this._stop = void 0;
101
+ this._record = void 0;
93
102
  this._events = [];
94
103
  this._bytes = 0;
95
104
  }
@@ -158,5 +167,7 @@ var Replay = _Replay;
158
167
  Replay._events = [];
159
168
  // rrweb 异步加载中的标记:避免 init 被重复调用时启动两个录制实例
160
169
  Replay._loading = false;
170
+ // 加载窗口内被 stop 的标记:rrweb 就绪后据此放弃启动
171
+ Replay._stopRequested = false;
161
172
  // 录制阶段的累计体积(近似值),达到 replayMaxBytes 后整体重开一段
162
173
  Replay._bytes = 0;
@@ -58,7 +58,11 @@ var Track = class {
58
58
  }
59
59
  // 需要初始化执行的组件都需在这里初始化调用
60
60
  static run() {
61
- import_Listener.default.run();
61
+ if (import_Config.default.data.autoReportError) {
62
+ import_Listener.default.run();
63
+ } else {
64
+ import_Listener.default.stop();
65
+ }
62
66
  if (import_Config.default.data.enableReplay) {
63
67
  void import_Replay.default.run();
64
68
  } else {
package/lib/Utils/file.js CHANGED
@@ -44,12 +44,18 @@ var checkFileType = (file, accept) => {
44
44
  return true;
45
45
  };
46
46
  var getImageResolution = (file) => {
47
- return new Promise((resolve) => {
47
+ return new Promise((resolve, reject) => {
48
48
  const img = new Image();
49
- img.src = URL.createObjectURL(file);
49
+ const url = URL.createObjectURL(file);
50
50
  img.onload = () => {
51
+ URL.revokeObjectURL(url);
51
52
  resolve({ width: img.width, height: img.height });
52
53
  };
54
+ img.onerror = () => {
55
+ URL.revokeObjectURL(url);
56
+ reject(new Error("getImageResolution: 图片加载失败"));
57
+ };
58
+ img.src = url;
53
59
  });
54
60
  };
55
61
  // Annotate the CommonJS export names for ESM import in node:
@@ -1,6 +1,6 @@
1
+ export * from './auth';
1
2
  export * from './common';
2
3
  export * from './file';
4
+ export * from './route';
3
5
  export * from './url';
4
6
  export * from './wx';
5
- export * from './route';
6
- export * from './auth';
@@ -16,18 +16,18 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
16
16
  // src/Utils/index.ts
17
17
  var Utils_exports = {};
18
18
  module.exports = __toCommonJS(Utils_exports);
19
+ __reExport(Utils_exports, require("./auth"), module.exports);
19
20
  __reExport(Utils_exports, require("./common"), module.exports);
20
21
  __reExport(Utils_exports, require("./file"), module.exports);
22
+ __reExport(Utils_exports, require("./route"), module.exports);
21
23
  __reExport(Utils_exports, require("./url"), module.exports);
22
24
  __reExport(Utils_exports, require("./wx"), module.exports);
23
- __reExport(Utils_exports, require("./route"), module.exports);
24
- __reExport(Utils_exports, require("./auth"), module.exports);
25
25
  // Annotate the CommonJS export names for ESM import in node:
26
26
  0 && (module.exports = {
27
+ ...require("./auth"),
27
28
  ...require("./common"),
28
29
  ...require("./file"),
29
- ...require("./url"),
30
- ...require("./wx"),
31
30
  ...require("./route"),
32
- ...require("./auth")
31
+ ...require("./url"),
32
+ ...require("./wx")
33
33
  });
@@ -25,10 +25,9 @@ module.exports = __toCommonJS(route_exports);
25
25
  var getModuleNameByUmi = (onRouteChangeArgs, menuNameMap) => {
26
26
  const { location, routes, basename } = onRouteChangeArgs;
27
27
  const { pathname } = location;
28
- let routePath = pathname;
29
- if (basename !== "/") {
30
- routePath = pathname.replace(basename, "/");
31
- }
28
+ const basePath = basename.replace(/\/+$/, "");
29
+ const isUnderBase = basePath !== "" && basePath !== "/" && (pathname === basePath || pathname.startsWith(`${basePath}/`));
30
+ const routePath = isUnderBase ? pathname.slice(basePath.length) || "/" : pathname;
32
31
  const currentRouteKey = Object.keys(routes).find(
33
32
  (v) => routes[v].path === routePath
34
33
  );
@@ -49,15 +48,15 @@ var getModuleNameByUmi = (onRouteChangeArgs, menuNameMap) => {
49
48
  if (menuHierarchyKeys.length === 1) {
50
49
  return;
51
50
  }
51
+ const nameMap = menuNameMap ?? {};
52
52
  const menuDisplayNames = [];
53
53
  for (let i = 1; i < menuHierarchyKeys.length; i++) {
54
54
  const menuKey = menuHierarchyKeys.slice(0, i + 1).join(".");
55
- const displayName = menuNameMap[menuKey];
55
+ const displayName = nameMap[menuKey];
56
+ if (!displayName)
57
+ return;
56
58
  menuDisplayNames.push(displayName);
57
59
  }
58
- if (menuDisplayNames.length === 0) {
59
- return;
60
- }
61
60
  return menuDisplayNames.join("-");
62
61
  };
63
62
  // Annotate the CommonJS export names for ESM import in node:
package/lib/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export { default as Track } from './Track';
2
2
  export * as Utils from './Utils';
3
- export * from './interfaces/track.interface';
4
3
  export * from './interfaces/protocol';
4
+ export * from './interfaces/track.interface';
package/lib/index.js CHANGED
@@ -36,12 +36,12 @@ __export(src_exports, {
36
36
  module.exports = __toCommonJS(src_exports);
37
37
  var import_Track = __toESM(require("./Track"));
38
38
  var Utils = __toESM(require("./Utils"));
39
- __reExport(src_exports, require("./interfaces/track.interface"), module.exports);
40
39
  __reExport(src_exports, require("./interfaces/protocol"), module.exports);
40
+ __reExport(src_exports, require("./interfaces/track.interface"), module.exports);
41
41
  // Annotate the CommonJS export names for ESM import in node:
42
42
  0 && (module.exports = {
43
43
  Track,
44
44
  Utils,
45
- ...require("./interfaces/track.interface"),
46
- ...require("./interfaces/protocol")
45
+ ...require("./interfaces/protocol"),
46
+ ...require("./interfaces/track.interface")
47
47
  });
@@ -18,7 +18,7 @@ export declare const TrackTypeLabel: Record<TrackType, string>;
18
18
  * 不做无依据的额外限制——超长字段本身即会写入失败。
19
19
  */
20
20
  export declare const TRACK_LIMITS: {
21
- /** 单次批量 events[] 条数上限(SDK batchSize 与服务端 DTO 共用) */
21
+ /** 单次批量 events[] 条数上限(SDK 切批与服务端 DTO 共用,双方都不可调) */
22
22
  readonly maxItems: 20;
23
23
  /** 探针版本号,guard_conf.version varchar(20) */
24
24
  readonly sdkVersion: 20;
@@ -38,7 +38,7 @@ var TrackTypeLabel = {
38
38
  api: "接口触发"
39
39
  };
40
40
  var TRACK_LIMITS = {
41
- /** 单次批量 events[] 条数上限(SDK batchSize 与服务端 DTO 共用) */
41
+ /** 单次批量 events[] 条数上限(SDK 切批与服务端 DTO 共用,双方都不可调) */
42
42
  maxItems: 20,
43
43
  /** 探针版本号,guard_conf.version varchar(20) */
44
44
  sdkVersion: 20,
@@ -1,4 +1,4 @@
1
- export { TrackTypeLabel, TRACK_TYPE_VALUES } from './protocol';
1
+ export { TRACK_TYPE_VALUES, TrackTypeLabel } from './protocol';
2
2
  export type { TrackType } from './protocol';
3
3
  export interface UserProfile {
4
4
  userId: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yysports/tools",
3
- "version": "3.0.5",
3
+ "version": "3.1.1",
4
4
  "description": "常用工具函数库",
5
5
  "license": "UNLICENSED",
6
6
  "author": "daiwenyu",
@@ -25,9 +25,10 @@
25
25
  "lint:css": "stylelint \"{src,test}/**/*.{css,less}\" --allow-empty-input",
26
26
  "lint:es": "eslint \"{src,test}/**/*.{js,jsx,ts,tsx}\"",
27
27
  "prepare": "husky && dumi setup",
28
- "prepublishOnly": "father doctor && npm run build",
29
- "release": "npm run build && npm publish --access public --registry https://registry.npmjs.org",
30
- "start": "npm run dev"
28
+ "prepublishOnly": "father doctor && npm run build && npm test",
29
+ "release": "npm run build && npm login --registry https://registry.npmjs.org && npm publish --access public --registry https://registry.npmjs.org",
30
+ "start": "npm run dev",
31
+ "test": "vitest run"
31
32
  },
32
33
  "commitlint": {
33
34
  "extends": [
@@ -52,28 +53,32 @@
52
53
  ]
53
54
  },
54
55
  "dependencies": {
55
- "@babel/runtime": "^7.29.7",
56
- "@rrweb/record": "2.1.6",
57
- "@rrweb/rrweb-plugin-console-record": "2.1.6",
56
+ "@babel/runtime": "^7.29.10",
57
+ "@rrweb/record": "2.1.7",
58
+ "@rrweb/rrweb-plugin-console-record": "2.1.7",
58
59
  "image-conversion": "^2.1.1"
59
60
  },
60
61
  "devDependencies": {
61
- "@commitlint/cli": "^20.5.3",
62
- "@commitlint/config-conventional": "^20.5.3",
62
+ "@commitlint/cli": "^21.2.4",
63
+ "@commitlint/config-conventional": "^21.2.4",
63
64
  "@types/react": "^19.3.0",
64
- "@umijs/lint": "^4.7.21",
65
- "antd": "^6.6.5",
66
- "dumi": "^2.4.49",
65
+ "@umijs/lint": "^4.7.27",
66
+ "antd": "^6.6.7",
67
+ "dumi": "^2.4.53",
67
68
  "eslint": "^8.57.1",
68
- "father": "^4.6.38",
69
+ "father": "^4.6.41",
69
70
  "husky": "^9.1.7",
70
- "lint-staged": "^16.4.0",
71
- "prettier": "^3.9.9",
71
+ "lint-staged": "^17.6.0",
72
+ "prettier": "^3.9.10",
72
73
  "prettier-plugin-organize-imports": "^4.3.0",
73
- "prettier-plugin-packagejson": "^2.5.22",
74
- "react": "^19.3.0",
75
- "react-dom": "^19.3.0",
76
- "stylelint": "^16.26.1"
74
+ "prettier-plugin-packagejson": "^3.0.2",
75
+ "react": "^19.3.1",
76
+ "react-dom": "^19.3.1",
77
+ "stylelint": "^14.16.1",
78
+ "vitest": "^5.0.5"
79
+ },
80
+ "engines": {
81
+ "node": ">=22.22.1"
77
82
  },
78
83
  "publishConfig": {
79
84
  "access": "public"