@yysports/tools 3.0.3 → 3.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/dist/Track/Beacon.d.ts +3 -6
- package/dist/Track/Beacon.js +13 -22
- package/dist/Track/Config.js +9 -1
- package/dist/Track/Listener.js +21 -3
- package/dist/Track/Log.d.ts +7 -0
- package/dist/Track/Log.js +41 -15
- package/dist/Track/Replay.d.ts +26 -2
- package/dist/Track/Replay.js +102 -17
- package/dist/Track/envelope.d.ts +19 -2
- package/dist/Track/envelope.js +36 -12
- package/dist/Track/index.js +8 -2
- package/dist/interfaces/protocol.d.ts +35 -0
- package/dist/interfaces/protocol.js +9 -1
- package/lib/Track/Beacon.d.ts +3 -6
- package/lib/Track/Beacon.js +9 -19
- package/lib/Track/Config.js +4 -1
- package/lib/Track/Listener.js +24 -11
- package/lib/Track/Log.d.ts +7 -0
- package/lib/Track/Log.js +25 -2
- package/lib/Track/Replay.d.ts +26 -2
- package/lib/Track/Replay.js +63 -10
- package/lib/Track/envelope.d.ts +19 -2
- package/lib/Track/envelope.js +31 -13
- package/lib/Track/index.js +5 -1
- package/lib/interfaces/protocol.d.ts +35 -0
- package/package.json +1 -9
package/dist/Track/index.js
CHANGED
|
@@ -44,8 +44,14 @@ var Track = /*#__PURE__*/function () {
|
|
|
44
44
|
key: "run",
|
|
45
45
|
value: function run() {
|
|
46
46
|
Listener.run();
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
if (Config.data.enableReplay) {
|
|
48
|
+
// 回放为异步启动(rrweb 懒加载),不阻塞 init;未开回放时不会加载 rrweb
|
|
49
|
+
void Replay.run();
|
|
50
|
+
} else {
|
|
51
|
+
// 已启动录制后再 init({ enableReplay: false }):Replay.run 的幂等只看 _stop,
|
|
52
|
+
// 这里显式停止,否则回放仍在录制并随错误上报
|
|
53
|
+
Replay.stop();
|
|
54
|
+
}
|
|
49
55
|
// 版本上报已并入信封 sdkVersion,不再有独立请求
|
|
50
56
|
Beacon.start();
|
|
51
57
|
}
|
|
@@ -100,3 +100,38 @@ export interface TrackV3ErrorPayload extends TrackV3Envelope {
|
|
|
100
100
|
error: TrackV3ErrorDetail;
|
|
101
101
|
replay?: unknown[];
|
|
102
102
|
}
|
|
103
|
+
/**
|
|
104
|
+
* 通道 3:单条扁平记录(原生 APP 直连)。
|
|
105
|
+
*
|
|
106
|
+
* 与 batch / error 的信封结构不同:字段全部顶层,设备标识由客户端直传
|
|
107
|
+
* (服务端不解析 UA、不从请求头取 UA);仅 `appId` / `type` 必填,其余字段
|
|
108
|
+
* 缺省时由服务端填默认值或落 `NULL`。不支持 replay。
|
|
109
|
+
*/
|
|
110
|
+
export interface TrackV3SinglePayload {
|
|
111
|
+
type: TrackType;
|
|
112
|
+
appId: string;
|
|
113
|
+
userAgent?: string;
|
|
114
|
+
userId?: string;
|
|
115
|
+
userName?: string;
|
|
116
|
+
imei?: string;
|
|
117
|
+
innerHeight?: number;
|
|
118
|
+
innerWidth?: number;
|
|
119
|
+
href?: string;
|
|
120
|
+
/** 毫秒时间戳;缺省时服务端以接收时间落库 */
|
|
121
|
+
systemTime?: number;
|
|
122
|
+
log?: string;
|
|
123
|
+
orgCode?: string;
|
|
124
|
+
orgTitle?: string;
|
|
125
|
+
dataScopeZonePermissions?: string;
|
|
126
|
+
dataScopeRoles?: string;
|
|
127
|
+
module?: string;
|
|
128
|
+
content?: string;
|
|
129
|
+
browserName?: string;
|
|
130
|
+
browserVersion?: string;
|
|
131
|
+
osName?: string;
|
|
132
|
+
osVersion?: string;
|
|
133
|
+
deviceType?: string;
|
|
134
|
+
deviceVendor?: string;
|
|
135
|
+
deviceModel?: string;
|
|
136
|
+
appVersion?: string;
|
|
137
|
+
}
|
|
@@ -64,4 +64,12 @@ export var TRACK_CONTENT_TYPE = 'text/plain;charset=UTF-8';
|
|
|
64
64
|
|
|
65
65
|
/** 通道 2 的错误明细 */
|
|
66
66
|
|
|
67
|
-
/** 通道 2:单条错误,可内嵌 rrweb 回放事件 */
|
|
67
|
+
/** 通道 2:单条错误,可内嵌 rrweb 回放事件 */
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 通道 3:单条扁平记录(原生 APP 直连)。
|
|
71
|
+
*
|
|
72
|
+
* 与 batch / error 的信封结构不同:字段全部顶层,设备标识由客户端直传
|
|
73
|
+
* (服务端不解析 UA、不从请求头取 UA);仅 `appId` / `type` 必填,其余字段
|
|
74
|
+
* 缺省时由服务端填默认值或落 `NULL`。不支持 replay。
|
|
75
|
+
*/
|
package/lib/Track/Beacon.d.ts
CHANGED
|
@@ -2,21 +2,18 @@ import { type TrackV3Event } from '../interfaces/protocol';
|
|
|
2
2
|
/**
|
|
3
3
|
* 批量日志队列(通道 1)。
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
5
|
+
* 发送语义是**尽力而为**:`sendBeacon` / `fetch` 都无法感知服务端 4xx/413,因此不做队列
|
|
6
|
+
* 驻留与重试,入队失败即丢弃该批,被拒的丢数由服务端侧计数告警补偿。
|
|
7
7
|
* 无 sendBeacon 环境(部分 WebView / 老浏览器)降级用 fetch(keepalive) 发送。
|
|
8
8
|
*/
|
|
9
9
|
export default class Beacon {
|
|
10
10
|
private static _queue;
|
|
11
11
|
private static _timer;
|
|
12
|
-
/** 连续发送失败次数,超过重试上限后丢弃当前批次,避免队列无限增长 */
|
|
13
|
-
private static _failCount;
|
|
14
|
-
private static readonly MAX_RETRIES;
|
|
15
12
|
/** 启动定时 flush(Track.init 时调用) */
|
|
16
13
|
static start(): void;
|
|
17
14
|
/** 入队,达到 batchSize 立即 flush */
|
|
18
15
|
static push(event: TrackV3Event): void;
|
|
19
|
-
/**
|
|
16
|
+
/** 按批发送并出队;发送失败同样出队(尽力而为,不驻留重试) */
|
|
20
17
|
static flush(): void;
|
|
21
18
|
private static sendBatch;
|
|
22
19
|
private static trySendBeacon;
|
package/lib/Track/Beacon.js
CHANGED
|
@@ -47,12 +47,16 @@ var Beacon = class {
|
|
|
47
47
|
}
|
|
48
48
|
/** 入队,达到 batchSize 立即 flush */
|
|
49
49
|
static push(event) {
|
|
50
|
+
if (!import_Config.default.data.appId) {
|
|
51
|
+
console.warn("[Track] 尚未调用 Track.init(appId 为空),该条上报已丢弃");
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
50
54
|
this._queue.push(event);
|
|
51
55
|
if (this._queue.length >= (import_Config.default.data.batchSize ?? import_protocol.TRACK_LIMITS.maxItems)) {
|
|
52
56
|
this.flush();
|
|
53
57
|
}
|
|
54
58
|
}
|
|
55
|
-
/**
|
|
59
|
+
/** 按批发送并出队;发送失败同样出队(尽力而为,不驻留重试) */
|
|
56
60
|
static flush() {
|
|
57
61
|
while (this._queue.length > 0) {
|
|
58
62
|
const batchSize = import_Config.default.data.batchSize ?? import_protocol.TRACK_LIMITS.maxItems;
|
|
@@ -61,23 +65,14 @@ var Beacon = class {
|
|
|
61
65
|
...(0, import_envelope.buildEnvelopeBase)(),
|
|
62
66
|
events: batch
|
|
63
67
|
};
|
|
64
|
-
|
|
65
|
-
return;
|
|
68
|
+
this.sendBatch(JSON.stringify(payload));
|
|
66
69
|
this._queue.splice(0, batch.length);
|
|
67
70
|
}
|
|
68
71
|
}
|
|
69
72
|
static sendBatch(payload) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
this._failCount = 0;
|
|
73
|
-
return "sent";
|
|
74
|
-
}
|
|
75
|
-
this._failCount += 1;
|
|
76
|
-
if (this._failCount > this.MAX_RETRIES) {
|
|
77
|
-
this._failCount = 0;
|
|
78
|
-
return "drop";
|
|
73
|
+
if (!this.trySendBeacon(payload)) {
|
|
74
|
+
this.trySendKeepalive(payload);
|
|
79
75
|
}
|
|
80
|
-
return "retry";
|
|
81
76
|
}
|
|
82
77
|
static trySendBeacon(payload) {
|
|
83
78
|
if (typeof navigator === "undefined" || !navigator.sendBeacon)
|
|
@@ -94,7 +89,7 @@ var Beacon = class {
|
|
|
94
89
|
/** fetch keepalive:无 sendBeacon 环境的主要通道,已发出即视为送达(不等响应、不阻塞队列) */
|
|
95
90
|
static trySendKeepalive(payload) {
|
|
96
91
|
if (typeof fetch !== "function")
|
|
97
|
-
return
|
|
92
|
+
return;
|
|
98
93
|
try {
|
|
99
94
|
void fetch((0, import_envelope.trackUrl)("/batch"), {
|
|
100
95
|
method: "POST",
|
|
@@ -102,14 +97,9 @@ var Beacon = class {
|
|
|
102
97
|
body: payload,
|
|
103
98
|
keepalive: true
|
|
104
99
|
}).catch(() => void 0);
|
|
105
|
-
return true;
|
|
106
100
|
} catch (error) {
|
|
107
|
-
return false;
|
|
108
101
|
}
|
|
109
102
|
}
|
|
110
103
|
};
|
|
111
104
|
Beacon._queue = [];
|
|
112
105
|
Beacon._timer = null;
|
|
113
|
-
/** 连续发送失败次数,超过重试上限后丢弃当前批次,避免队列无限增长 */
|
|
114
|
-
Beacon._failCount = 0;
|
|
115
|
-
Beacon.MAX_RETRIES = 3;
|
package/lib/Track/Config.js
CHANGED
|
@@ -32,7 +32,10 @@ var Config = class {
|
|
|
32
32
|
if (!((_b = config.domain) == null ? void 0 : _b.trim())) {
|
|
33
33
|
throw new Error("Config: domain is required");
|
|
34
34
|
}
|
|
35
|
-
Object.
|
|
35
|
+
const defined = Object.fromEntries(
|
|
36
|
+
Object.entries(config).filter(([, value]) => value !== void 0)
|
|
37
|
+
);
|
|
38
|
+
Object.assign(this._data, defined);
|
|
36
39
|
}
|
|
37
40
|
static get data() {
|
|
38
41
|
return this._data;
|
package/lib/Track/Listener.js
CHANGED
|
@@ -35,22 +35,35 @@ module.exports = __toCommonJS(Listener_exports);
|
|
|
35
35
|
var import_Beacon = __toESM(require("./Beacon"));
|
|
36
36
|
var import_Config = __toESM(require("./Config"));
|
|
37
37
|
var import_Log = __toESM(require("./Log"));
|
|
38
|
+
function resolveErrorMessage(event) {
|
|
39
|
+
var _a;
|
|
40
|
+
const message = (_a = event.message) == null ? void 0 : _a.trim();
|
|
41
|
+
if (message)
|
|
42
|
+
return message;
|
|
43
|
+
const target = event.target;
|
|
44
|
+
const failedUrl = (target == null ? void 0 : target.src) ?? (target == null ? void 0 : target.href);
|
|
45
|
+
return failedUrl ? `资源加载失败:${failedUrl}` : "";
|
|
46
|
+
}
|
|
38
47
|
var Listener = class {
|
|
39
48
|
static run() {
|
|
40
49
|
if (this._loaded || !import_Config.default.data.autoReportError) {
|
|
41
50
|
return;
|
|
42
51
|
}
|
|
43
|
-
window.addEventListener(
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
+
);
|
|
54
67
|
document.addEventListener("visibilitychange", () => {
|
|
55
68
|
if ((document == null ? void 0 : document.visibilityState) === "hidden") {
|
|
56
69
|
import_Beacon.default.flush();
|
package/lib/Track/Log.d.ts
CHANGED
|
@@ -7,6 +7,13 @@
|
|
|
7
7
|
export default class Log {
|
|
8
8
|
private static _module;
|
|
9
9
|
static get module(): string;
|
|
10
|
+
/**
|
|
11
|
+
* 条级业务内容超列宽时丢弃该条。
|
|
12
|
+
*
|
|
13
|
+
* `content` 是接入方传入的业务内容:截断会改变语义、把「数据丢失」伪装成「数据完整」,
|
|
14
|
+
* 故选择丢弃 + `console.warn`——联调期即可暴露,且不会让同批其他条一起被 400 拒掉。
|
|
15
|
+
*/
|
|
16
|
+
private static isContentOverlong;
|
|
10
17
|
static visitLog(module: string): void;
|
|
11
18
|
static eventLog(content: string): void;
|
|
12
19
|
static apiLog(log: string): void;
|
package/lib/Track/Log.js
CHANGED
|
@@ -41,17 +41,34 @@ var Log = class {
|
|
|
41
41
|
static get module() {
|
|
42
42
|
return this._module;
|
|
43
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* 条级业务内容超列宽时丢弃该条。
|
|
46
|
+
*
|
|
47
|
+
* `content` 是接入方传入的业务内容:截断会改变语义、把「数据丢失」伪装成「数据完整」,
|
|
48
|
+
* 故选择丢弃 + `console.warn`——联调期即可暴露,且不会让同批其他条一起被 400 拒掉。
|
|
49
|
+
*/
|
|
50
|
+
static isContentOverlong(content) {
|
|
51
|
+
if (typeof content !== "string" || content.length <= import_protocol.TRACK_LIMITS.content) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
console.warn(
|
|
55
|
+
`[Track] content 长度 ${content.length} 超过上限 ${import_protocol.TRACK_LIMITS.content}(服务端会整批 400),已丢弃该条日志`
|
|
56
|
+
);
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
44
59
|
static visitLog(module2) {
|
|
45
|
-
this._module = module2;
|
|
60
|
+
this._module = (0, import_envelope.truncate)(module2, import_protocol.TRACK_LIMITS.module);
|
|
46
61
|
const lastModule = this._module.split("-").pop();
|
|
47
62
|
import_Beacon.default.push({
|
|
48
63
|
...(0, import_envelope.buildEventBase)(),
|
|
49
64
|
type: "visit",
|
|
50
|
-
module:
|
|
65
|
+
module: this._module,
|
|
51
66
|
content: `访问页面:${lastModule}`
|
|
52
67
|
});
|
|
53
68
|
}
|
|
54
69
|
static eventLog(content) {
|
|
70
|
+
if (this.isContentOverlong(content))
|
|
71
|
+
return;
|
|
55
72
|
import_Beacon.default.push({
|
|
56
73
|
...(0, import_envelope.buildEventBase)(),
|
|
57
74
|
type: "event",
|
|
@@ -74,6 +91,12 @@ var Log = class {
|
|
|
74
91
|
*/
|
|
75
92
|
static async errorLog(message, stack) {
|
|
76
93
|
var _a;
|
|
94
|
+
if (typeof message !== "string" || !message.trim()) {
|
|
95
|
+
console.warn(
|
|
96
|
+
"[Track] errorLog 的 message 为空(服务端会整条 400),已丢弃该条错误上报"
|
|
97
|
+
);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
77
100
|
const replay = import_Replay.default.snapshot();
|
|
78
101
|
const pureError = {
|
|
79
102
|
...(0, import_envelope.buildEnvelopeBase)(),
|
package/lib/Track/Replay.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ type RRWebEvent = any;
|
|
|
2
2
|
export default class Replay {
|
|
3
3
|
private static _events;
|
|
4
4
|
private static _stop;
|
|
5
|
+
private static _record;
|
|
5
6
|
private static _loading;
|
|
6
7
|
private static _bytes;
|
|
7
8
|
/**
|
|
@@ -13,11 +14,34 @@ export default class Replay {
|
|
|
13
14
|
* 错误本体的上报不受影响)。
|
|
14
15
|
*/
|
|
15
16
|
static run(): Promise<void>;
|
|
16
|
-
/**
|
|
17
|
+
/**
|
|
18
|
+
* 停止录制并清空缓存。
|
|
19
|
+
*
|
|
20
|
+
* `Replay.run` 的幂等只看 `_stop`,`init({ enableReplay: false })` 不会让已启动的录制停下,
|
|
21
|
+
* 因此 `Track.run` 在关闭回放时显式调用本方法。
|
|
22
|
+
*/
|
|
23
|
+
static stop(): void;
|
|
24
|
+
/**
|
|
25
|
+
* 截断到体积上限:整体重开一段,并请求一张新的完整快照。
|
|
26
|
+
*
|
|
27
|
+
* 片段必须自带播放起点(在缓冲区头部),丢掉起点整段就无法播放,而起点之后的事件
|
|
28
|
+
* 全是增量、丢了画面又不完整,所以这里不做「从头丢事件」,直接重开一段。
|
|
29
|
+
*/
|
|
17
30
|
private static truncate;
|
|
31
|
+
/** 缓冲区头部的起点事件(Meta / FullSnapshot);无起点时返回空数组 */
|
|
32
|
+
private static replayStartEvents;
|
|
33
|
+
/** 请求 rrweb 立刻产出一张 FullSnapshot(失败静默,仅影响回放可用性) */
|
|
34
|
+
private static requestFullSnapshot;
|
|
35
|
+
/** 事件数组的近似序列化体积(与 emit 中的累计口径一致) */
|
|
36
|
+
private static bytesOf;
|
|
18
37
|
/** 快照当前回放片段(不移除,待上报成功后 commitSnapshot) */
|
|
19
38
|
static snapshot(): RRWebEvent[];
|
|
20
|
-
/**
|
|
39
|
+
/**
|
|
40
|
+
* 上报成功后移除已携带的事件,继续录制——不调用 stop,避免下一个错误重复携带上一段。
|
|
41
|
+
*
|
|
42
|
+
* 起点事件(Meta / FullSnapshot)保留在缓冲区:随本次上报一起移除后,下一段回放
|
|
43
|
+
* 只剩增量事件、播放器无法独立渲染。
|
|
44
|
+
*/
|
|
21
45
|
static commitSnapshot(snapshot: RRWebEvent[]): void;
|
|
22
46
|
}
|
|
23
47
|
export {};
|
package/lib/Track/Replay.js
CHANGED
|
@@ -33,7 +33,10 @@ __export(Replay_exports, {
|
|
|
33
33
|
});
|
|
34
34
|
module.exports = __toCommonJS(Replay_exports);
|
|
35
35
|
var import_Config = __toESM(require("./Config"));
|
|
36
|
-
var
|
|
36
|
+
var RRWEB_META = 4;
|
|
37
|
+
var RRWEB_FULL_SNAPSHOT = 2;
|
|
38
|
+
var isReplayStartEvent = (event) => (event == null ? void 0 : event.type) === RRWEB_META || (event == null ? void 0 : event.type) === RRWEB_FULL_SNAPSHOT;
|
|
39
|
+
var _Replay = class {
|
|
37
40
|
/**
|
|
38
41
|
* 启动录制。
|
|
39
42
|
*
|
|
@@ -52,6 +55,7 @@ var Replay = class {
|
|
|
52
55
|
import("@rrweb/record"),
|
|
53
56
|
import("@rrweb/rrweb-plugin-console-record")
|
|
54
57
|
]);
|
|
58
|
+
this._record = startRecord;
|
|
55
59
|
this._stop = startRecord({
|
|
56
60
|
emit: (e) => {
|
|
57
61
|
this._events.push(e);
|
|
@@ -70,30 +74,77 @@ var Replay = class {
|
|
|
70
74
|
});
|
|
71
75
|
} catch (err) {
|
|
72
76
|
this._stop = void 0;
|
|
77
|
+
this._record = void 0;
|
|
73
78
|
} finally {
|
|
74
79
|
this._loading = false;
|
|
75
80
|
}
|
|
76
81
|
}
|
|
77
|
-
/**
|
|
82
|
+
/**
|
|
83
|
+
* 停止录制并清空缓存。
|
|
84
|
+
*
|
|
85
|
+
* `Replay.run` 的幂等只看 `_stop`,`init({ enableReplay: false })` 不会让已启动的录制停下,
|
|
86
|
+
* 因此 `Track.run` 在关闭回放时显式调用本方法。
|
|
87
|
+
*/
|
|
88
|
+
static stop() {
|
|
89
|
+
if (this._stop) {
|
|
90
|
+
this._stop();
|
|
91
|
+
}
|
|
92
|
+
this._stop = void 0;
|
|
93
|
+
this._events = [];
|
|
94
|
+
this._bytes = 0;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* 截断到体积上限:整体重开一段,并请求一张新的完整快照。
|
|
98
|
+
*
|
|
99
|
+
* 片段必须自带播放起点(在缓冲区头部),丢掉起点整段就无法播放,而起点之后的事件
|
|
100
|
+
* 全是增量、丢了画面又不完整,所以这里不做「从头丢事件」,直接重开一段。
|
|
101
|
+
*/
|
|
78
102
|
static truncate() {
|
|
79
103
|
const maxBytes = import_Config.default.data.replayMaxBytes ?? 1024 * 1024;
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
104
|
+
if (this._bytes <= maxBytes)
|
|
105
|
+
return;
|
|
106
|
+
const startBytes = _Replay.bytesOf(_Replay.replayStartEvents(this._events));
|
|
107
|
+
if (startBytes > maxBytes)
|
|
108
|
+
return;
|
|
109
|
+
this._events = this._events.filter((e) => (e == null ? void 0 : e.type) === RRWEB_META).slice(0, 1);
|
|
110
|
+
this._bytes = _Replay.bytesOf(this._events);
|
|
111
|
+
_Replay.requestFullSnapshot();
|
|
112
|
+
}
|
|
113
|
+
/** 缓冲区头部的起点事件(Meta / FullSnapshot);无起点时返回空数组 */
|
|
114
|
+
static replayStartEvents(events) {
|
|
115
|
+
return events.slice(0, 2).filter(isReplayStartEvent);
|
|
116
|
+
}
|
|
117
|
+
/** 请求 rrweb 立刻产出一张 FullSnapshot(失败静默,仅影响回放可用性) */
|
|
118
|
+
static requestFullSnapshot() {
|
|
119
|
+
var _a, _b;
|
|
120
|
+
try {
|
|
121
|
+
(_b = (_a = _Replay._record) == null ? void 0 : _a.takeFullSnapshot) == null ? void 0 : _b.call(_a, true);
|
|
122
|
+
} catch (err) {
|
|
83
123
|
}
|
|
84
124
|
}
|
|
125
|
+
/** 事件数组的近似序列化体积(与 emit 中的累计口径一致) */
|
|
126
|
+
static bytesOf(events) {
|
|
127
|
+
return events.reduce((sum, e) => sum + JSON.stringify(e).length, 0);
|
|
128
|
+
}
|
|
85
129
|
/** 快照当前回放片段(不移除,待上报成功后 commitSnapshot) */
|
|
86
130
|
static snapshot() {
|
|
87
131
|
return this._stop ? [...this._events] : [];
|
|
88
132
|
}
|
|
89
|
-
/**
|
|
133
|
+
/**
|
|
134
|
+
* 上报成功后移除已携带的事件,继续录制——不调用 stop,避免下一个错误重复携带上一段。
|
|
135
|
+
*
|
|
136
|
+
* 起点事件(Meta / FullSnapshot)保留在缓冲区:随本次上报一起移除后,下一段回放
|
|
137
|
+
* 只剩增量事件、播放器无法独立渲染。
|
|
138
|
+
*/
|
|
90
139
|
static commitSnapshot(snapshot) {
|
|
91
140
|
if (!snapshot.length)
|
|
92
141
|
return;
|
|
93
142
|
const sent = new Set(snapshot);
|
|
94
|
-
this._events = this._events.filter((
|
|
95
|
-
if (
|
|
96
|
-
|
|
143
|
+
this._events = this._events.filter((event) => {
|
|
144
|
+
if (isReplayStartEvent(event))
|
|
145
|
+
return true;
|
|
146
|
+
if (sent.has(event)) {
|
|
147
|
+
this._bytes -= JSON.stringify(event).length;
|
|
97
148
|
return false;
|
|
98
149
|
}
|
|
99
150
|
return true;
|
|
@@ -102,8 +153,10 @@ var Replay = class {
|
|
|
102
153
|
this._bytes = 0;
|
|
103
154
|
}
|
|
104
155
|
};
|
|
156
|
+
var Replay = _Replay;
|
|
157
|
+
// 缓冲区:起点事件(Meta / FullSnapshot)始终保留在头部
|
|
105
158
|
Replay._events = [];
|
|
106
159
|
// rrweb 异步加载中的标记:避免 init 被重复调用时启动两个录制实例
|
|
107
160
|
Replay._loading = false;
|
|
108
|
-
// 录制阶段的累计体积(近似值),达到 replayMaxBytes
|
|
161
|
+
// 录制阶段的累计体积(近似值),达到 replayMaxBytes 后整体重开一段
|
|
109
162
|
Replay._bytes = 0;
|
package/lib/Track/envelope.d.ts
CHANGED
|
@@ -1,7 +1,24 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type TrackV3Envelope, type TrackV3Event } from '../interfaces/protocol';
|
|
2
|
+
/**
|
|
3
|
+
* 按列宽截断。
|
|
4
|
+
*
|
|
5
|
+
* 只用于「接入方拿不到也改不了」的字段:请求级信封字段(`imei` / `sdkVersion` /
|
|
6
|
+
* `appVersion` / `user.*`)、逐条自动采集的 `href`、以及会话级缓存的 `module`。
|
|
7
|
+
* 这些值一旦超列宽,服务端会因 `@MaxLength` 直接 400(整批 / 整请求被拒),
|
|
8
|
+
* 丢弃等于让该应用或该页面的上报整体消失;截断至少保住定位信息。
|
|
9
|
+
*
|
|
10
|
+
* 反之,接入方自己传的业务内容(`content`)超限时**丢弃该条**而不是截断,
|
|
11
|
+
* 见 `Log.isContentOverlong`——截断会把「数据丢失」伪装成「数据完整」。
|
|
12
|
+
*/
|
|
13
|
+
export declare function truncate(value: string, limit: number): string;
|
|
14
|
+
export declare function truncate(value: string | undefined, limit: number): string | undefined;
|
|
2
15
|
/** 请求级公共段(信封):字段与实体属性同名(camelCase),每次请求仅一份 */
|
|
3
16
|
export declare function buildEnvelopeBase(): TrackV3Envelope;
|
|
4
17
|
/** 条级事件公共字段:href / 视口 / 毫秒时间戳在批量窗口内会变,逐条携带 */
|
|
5
18
|
export declare function buildEventBase(): Pick<TrackV3Event, 'systemTime' | 'href' | 'innerWidth' | 'innerHeight'>;
|
|
6
|
-
/**
|
|
19
|
+
/**
|
|
20
|
+
* V3 上报地址:鉴权**仅 appId**,走 URL query(sendBeacon 无法自定义 header)。
|
|
21
|
+
* 刻意不实现 `ts` / `nonce` / `sign`:探针运行在公开前端页面,密钥无法保密,签名不构成防线;
|
|
22
|
+
* 准入由服务端 TrackAuthGuard(appId 在册 + 停收开关 + 限流)负责。
|
|
23
|
+
*/
|
|
7
24
|
export declare function trackUrl(path: '/batch' | '/error'): string;
|
package/lib/Track/envelope.js
CHANGED
|
@@ -31,29 +31,42 @@ var envelope_exports = {};
|
|
|
31
31
|
__export(envelope_exports, {
|
|
32
32
|
buildEnvelopeBase: () => buildEnvelopeBase,
|
|
33
33
|
buildEventBase: () => buildEventBase,
|
|
34
|
-
trackUrl: () => trackUrl
|
|
34
|
+
trackUrl: () => trackUrl,
|
|
35
|
+
truncate: () => truncate
|
|
35
36
|
});
|
|
36
37
|
module.exports = __toCommonJS(envelope_exports);
|
|
37
38
|
var import_package = __toESM(require("../../package.json"));
|
|
39
|
+
var import_protocol = require("../interfaces/protocol");
|
|
38
40
|
var import_Config = __toESM(require("./Config"));
|
|
39
41
|
var import_System = __toESM(require("./System"));
|
|
40
42
|
var import_User = __toESM(require("./User"));
|
|
43
|
+
function truncate(value, limit) {
|
|
44
|
+
if (value === null || value === void 0)
|
|
45
|
+
return void 0;
|
|
46
|
+
return value.length > limit ? value.slice(0, limit) : value;
|
|
47
|
+
}
|
|
41
48
|
function buildEnvelopeBase() {
|
|
42
49
|
const profile = import_User.default.profile;
|
|
43
50
|
return {
|
|
44
51
|
appId: import_Config.default.data.appId,
|
|
45
|
-
sdkVersion: import_package.default.version,
|
|
52
|
+
sdkVersion: truncate(import_package.default.version, import_protocol.TRACK_LIMITS.sdkVersion),
|
|
46
53
|
// 调用方应用版本:未配置时为 undefined,JSON.stringify 会丢弃该键(服务端落 NULL)
|
|
47
|
-
appVersion: import_Config.default.data.appVersion,
|
|
48
|
-
imei: import_System.default.imei,
|
|
54
|
+
appVersion: truncate(import_Config.default.data.appVersion, import_protocol.TRACK_LIMITS.appVersion),
|
|
55
|
+
imei: truncate(import_System.default.imei, import_protocol.TRACK_LIMITS.imei),
|
|
49
56
|
...profile ? {
|
|
50
57
|
user: {
|
|
51
|
-
userId: profile.userId,
|
|
52
|
-
userName: profile.userName,
|
|
53
|
-
orgCode: profile.orgCode,
|
|
54
|
-
orgTitle: profile.orgTitle,
|
|
55
|
-
dataScopeZonePermissions:
|
|
56
|
-
|
|
58
|
+
userId: truncate(profile.userId, import_protocol.TRACK_LIMITS.userField),
|
|
59
|
+
userName: truncate(profile.userName, import_protocol.TRACK_LIMITS.userField),
|
|
60
|
+
orgCode: truncate(profile.orgCode, import_protocol.TRACK_LIMITS.userField),
|
|
61
|
+
orgTitle: truncate(profile.orgTitle, import_protocol.TRACK_LIMITS.userField),
|
|
62
|
+
dataScopeZonePermissions: truncate(
|
|
63
|
+
profile.dataScopeZonePermissions,
|
|
64
|
+
import_protocol.TRACK_LIMITS.userField
|
|
65
|
+
),
|
|
66
|
+
dataScopeRoles: truncate(
|
|
67
|
+
profile.dataScopeRoles,
|
|
68
|
+
import_protocol.TRACK_LIMITS.userField
|
|
69
|
+
)
|
|
57
70
|
}
|
|
58
71
|
} : {}
|
|
59
72
|
};
|
|
@@ -61,18 +74,23 @@ function buildEnvelopeBase() {
|
|
|
61
74
|
function buildEventBase() {
|
|
62
75
|
return {
|
|
63
76
|
systemTime: Date.now(),
|
|
64
|
-
href:
|
|
77
|
+
href: truncate(
|
|
78
|
+
typeof location !== "undefined" ? location.href : "",
|
|
79
|
+
import_protocol.TRACK_LIMITS.href
|
|
80
|
+
),
|
|
65
81
|
innerWidth: typeof window !== "undefined" ? window.innerWidth : 0,
|
|
66
82
|
innerHeight: typeof window !== "undefined" ? window.innerHeight : 0
|
|
67
83
|
};
|
|
68
84
|
}
|
|
69
85
|
function trackUrl(path) {
|
|
70
86
|
const appId = encodeURIComponent(import_Config.default.data.appId);
|
|
71
|
-
|
|
87
|
+
const domain = import_Config.default.data.domain.replace(/\/+$/, "");
|
|
88
|
+
return `${domain}/pousheng-guard/api/track/v3${path}?appId=${appId}`;
|
|
72
89
|
}
|
|
73
90
|
// Annotate the CommonJS export names for ESM import in node:
|
|
74
91
|
0 && (module.exports = {
|
|
75
92
|
buildEnvelopeBase,
|
|
76
93
|
buildEventBase,
|
|
77
|
-
trackUrl
|
|
94
|
+
trackUrl,
|
|
95
|
+
truncate
|
|
78
96
|
});
|
package/lib/Track/index.js
CHANGED
|
@@ -59,7 +59,11 @@ var Track = class {
|
|
|
59
59
|
// 需要初始化执行的组件都需在这里初始化调用
|
|
60
60
|
static run() {
|
|
61
61
|
import_Listener.default.run();
|
|
62
|
-
|
|
62
|
+
if (import_Config.default.data.enableReplay) {
|
|
63
|
+
void import_Replay.default.run();
|
|
64
|
+
} else {
|
|
65
|
+
import_Replay.default.stop();
|
|
66
|
+
}
|
|
63
67
|
import_Beacon.default.start();
|
|
64
68
|
}
|
|
65
69
|
// 获取当前页面模块名称
|
|
@@ -100,3 +100,38 @@ export interface TrackV3ErrorPayload extends TrackV3Envelope {
|
|
|
100
100
|
error: TrackV3ErrorDetail;
|
|
101
101
|
replay?: unknown[];
|
|
102
102
|
}
|
|
103
|
+
/**
|
|
104
|
+
* 通道 3:单条扁平记录(原生 APP 直连)。
|
|
105
|
+
*
|
|
106
|
+
* 与 batch / error 的信封结构不同:字段全部顶层,设备标识由客户端直传
|
|
107
|
+
* (服务端不解析 UA、不从请求头取 UA);仅 `appId` / `type` 必填,其余字段
|
|
108
|
+
* 缺省时由服务端填默认值或落 `NULL`。不支持 replay。
|
|
109
|
+
*/
|
|
110
|
+
export interface TrackV3SinglePayload {
|
|
111
|
+
type: TrackType;
|
|
112
|
+
appId: string;
|
|
113
|
+
userAgent?: string;
|
|
114
|
+
userId?: string;
|
|
115
|
+
userName?: string;
|
|
116
|
+
imei?: string;
|
|
117
|
+
innerHeight?: number;
|
|
118
|
+
innerWidth?: number;
|
|
119
|
+
href?: string;
|
|
120
|
+
/** 毫秒时间戳;缺省时服务端以接收时间落库 */
|
|
121
|
+
systemTime?: number;
|
|
122
|
+
log?: string;
|
|
123
|
+
orgCode?: string;
|
|
124
|
+
orgTitle?: string;
|
|
125
|
+
dataScopeZonePermissions?: string;
|
|
126
|
+
dataScopeRoles?: string;
|
|
127
|
+
module?: string;
|
|
128
|
+
content?: string;
|
|
129
|
+
browserName?: string;
|
|
130
|
+
browserVersion?: string;
|
|
131
|
+
osName?: string;
|
|
132
|
+
osVersion?: string;
|
|
133
|
+
deviceType?: string;
|
|
134
|
+
deviceVendor?: string;
|
|
135
|
+
deviceModel?: string;
|
|
136
|
+
appVersion?: string;
|
|
137
|
+
}
|
package/package.json
CHANGED
|
@@ -1,15 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yysports/tools",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.5",
|
|
4
4
|
"description": "常用工具函数库",
|
|
5
|
-
"homepage": "https://git.pousheng.com/sport-service-department/pousheng-guard/yysports_tools#readme",
|
|
6
|
-
"bugs": {
|
|
7
|
-
"url": "https://git.pousheng.com/sport-service-department/pousheng-guard/yysports_tools/issues"
|
|
8
|
-
},
|
|
9
|
-
"repository": {
|
|
10
|
-
"type": "git",
|
|
11
|
-
"url": "git+https://git.pousheng.com/sport-service-department/pousheng-guard/yysports_tools.git"
|
|
12
|
-
},
|
|
13
5
|
"license": "UNLICENSED",
|
|
14
6
|
"author": "daiwenyu",
|
|
15
7
|
"main": "lib/index.js",
|