@yysports/tools 2.2.7 → 3.0.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.
- package/README.md +1 -6
- package/dist/Track/Beacon.d.ts +22 -3
- package/dist/Track/Beacon.js +93 -33
- package/dist/Track/Config.d.ts +4 -0
- package/dist/Track/Config.js +5 -1
- package/dist/Track/Listener.js +8 -4
- package/dist/Track/Log.d.ts +12 -2
- package/dist/Track/Log.js +71 -48
- package/dist/Track/Replay.d.ts +9 -1
- package/dist/Track/Replay.js +49 -44
- package/dist/Track/System.d.ts +3 -1
- package/dist/Track/System.js +8 -2
- package/dist/Track/envelope.d.ts +7 -0
- package/dist/Track/envelope.js +40 -0
- package/dist/Track/index.js +2 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -1
- package/dist/interfaces/protocol.d.ts +94 -0
- package/dist/interfaces/protocol.js +65 -0
- package/dist/interfaces/track.interface.d.ts +3 -8
- package/dist/interfaces/track.interface.js +3 -8
- package/lib/Track/Beacon.d.ts +22 -3
- package/lib/Track/Beacon.js +70 -28
- package/lib/Track/Config.d.ts +4 -0
- package/lib/Track/Config.js +5 -1
- package/lib/Track/Listener.js +7 -3
- package/lib/Track/Log.d.ts +12 -2
- package/lib/Track/Log.js +46 -35
- package/lib/Track/Replay.d.ts +9 -1
- package/lib/Track/Replay.js +32 -20
- package/lib/Track/System.d.ts +3 -1
- package/lib/Track/System.js +6 -2
- package/lib/Track/envelope.d.ts +7 -0
- package/lib/Track/envelope.js +76 -0
- package/lib/Track/index.js +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +3 -1
- package/lib/interfaces/protocol.d.ts +94 -0
- package/lib/interfaces/protocol.js +66 -0
- package/lib/interfaces/track.interface.d.ts +3 -8
- package/lib/interfaces/track.interface.js +5 -9
- package/package.json +18 -18
- package/LICENSE +0 -21
package/dist/Track/index.js
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 探针上报协议(V3)的唯一真源:类型与限额常量,供 SDK / 服务端 / 管理端共同复用。
|
|
3
|
+
*
|
|
4
|
+
* 约束:本文件只放无运行时依赖的类型与常量,不得引入 class-validator / typeorm / nest
|
|
5
|
+
* 等框架依赖;服务端的校验装饰器与 ORM 实体各自保留,不在此定义。
|
|
6
|
+
* 协议字段统一 camelCase,与 guard_track 的实体属性同名;库列的 snake_case
|
|
7
|
+
* (如 appId → app_id)由服务端 ORM 命名策略负责,协议层不感知列名。
|
|
8
|
+
*/
|
|
9
|
+
/** 上报类型取值(与服务端 guard_track.type 枚举一致) */
|
|
10
|
+
export type TrackType = 'error' | 'visit' | 'event' | 'api';
|
|
11
|
+
/** 类型取值列表(服务端校验 / 管理端筛选用) */
|
|
12
|
+
export declare const TRACK_TYPE_VALUES: readonly TrackType[];
|
|
13
|
+
/** 类型展示标签(管理端表格、下拉用) */
|
|
14
|
+
export declare const TrackTypeLabel: Record<TrackType, string>;
|
|
15
|
+
/**
|
|
16
|
+
* 协议限额:服务端 DTO 校验与两侧截断共用同一份定义。
|
|
17
|
+
* 除 maxItems(协议约定)外,其余取值均对齐 guard_track 的列长,
|
|
18
|
+
* 不做无依据的额外限制——超长字段本身即会写入失败。
|
|
19
|
+
*/
|
|
20
|
+
export declare const TRACK_LIMITS: {
|
|
21
|
+
/** 单次批量 events[] 条数上限(SDK batchSize 与服务端 DTO 共用) */
|
|
22
|
+
readonly maxItems: 20;
|
|
23
|
+
/** 探针版本号,guard_conf.version varchar(20) */
|
|
24
|
+
readonly sdkVersion: 20;
|
|
25
|
+
/** 会话串,列默认 varchar(255) */
|
|
26
|
+
readonly imei: 255;
|
|
27
|
+
/** 页面 URL,varchar(1000) */
|
|
28
|
+
readonly href: 1000;
|
|
29
|
+
/** 内容类字段(content / module / user.*),列默认 varchar(255) */
|
|
30
|
+
readonly content: 255;
|
|
31
|
+
readonly module: 255;
|
|
32
|
+
/** 用户画像单个字段长度 */
|
|
33
|
+
readonly userField: 255;
|
|
34
|
+
/** 用户 UA,varchar(1000) */
|
|
35
|
+
readonly userAgent: 1000;
|
|
36
|
+
/** V3 通道整体 body 上限(含内嵌回放),main.ts 直接引用 */
|
|
37
|
+
readonly bodyBytes: number;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* 上报请求体 Content-Type:固定 text/plain(body 仍是 JSON 文本)。
|
|
41
|
+
* text/plain 属 CORS safelist,跨域不触发预检——sendBeacon / fetch 在页面卸载时
|
|
42
|
+
* 无需等待 OPTIONS 完成,避免「预检未及时返回」导致的丢数;服务端据此收原始文本再解析 JSON。
|
|
43
|
+
*/
|
|
44
|
+
export declare const TRACK_CONTENT_TYPE = "text/plain;charset=UTF-8";
|
|
45
|
+
/** 用户画像(请求级恒定);未登录时整段缺省 */
|
|
46
|
+
export interface TrackV3User {
|
|
47
|
+
userId?: string;
|
|
48
|
+
userName?: string;
|
|
49
|
+
orgCode?: string;
|
|
50
|
+
orgTitle?: string;
|
|
51
|
+
dataScopeZonePermissions?: string;
|
|
52
|
+
dataScopeRoles?: string;
|
|
53
|
+
}
|
|
54
|
+
/** 条级事件:href / 视口在一个批量窗口内会变,逐条携带 */
|
|
55
|
+
export interface TrackV3Event {
|
|
56
|
+
/** 毫秒时间戳 */
|
|
57
|
+
systemTime: number;
|
|
58
|
+
type: Exclude<TrackType, 'error'>;
|
|
59
|
+
href: string;
|
|
60
|
+
innerWidth: number;
|
|
61
|
+
innerHeight: number;
|
|
62
|
+
module?: string;
|
|
63
|
+
/** visit / event 使用 */
|
|
64
|
+
content?: string;
|
|
65
|
+
/** api 使用 */
|
|
66
|
+
log?: string;
|
|
67
|
+
}
|
|
68
|
+
/** 请求级公共段(信封):一个请求只带一份 */
|
|
69
|
+
export interface TrackV3Envelope {
|
|
70
|
+
appId: string;
|
|
71
|
+
sdkVersion: string;
|
|
72
|
+
/** 会话串,前端初始化时生成;与库列名 `imei` 一致 */
|
|
73
|
+
imei: string;
|
|
74
|
+
user?: TrackV3User;
|
|
75
|
+
}
|
|
76
|
+
/** 通道 1:批量日志 */
|
|
77
|
+
export interface TrackV3BatchPayload extends TrackV3Envelope {
|
|
78
|
+
events: TrackV3Event[];
|
|
79
|
+
}
|
|
80
|
+
/** 通道 2 的错误明细 */
|
|
81
|
+
export interface TrackV3ErrorDetail {
|
|
82
|
+
systemTime: number;
|
|
83
|
+
href: string;
|
|
84
|
+
innerWidth: number;
|
|
85
|
+
innerHeight: number;
|
|
86
|
+
message: string;
|
|
87
|
+
stack?: string;
|
|
88
|
+
module?: string;
|
|
89
|
+
}
|
|
90
|
+
/** 通道 2:单条错误,可内嵌 rrweb 回放事件 */
|
|
91
|
+
export interface TrackV3ErrorPayload extends TrackV3Envelope {
|
|
92
|
+
error: TrackV3ErrorDetail;
|
|
93
|
+
replay?: unknown[];
|
|
94
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 探针上报协议(V3)的唯一真源:类型与限额常量,供 SDK / 服务端 / 管理端共同复用。
|
|
3
|
+
*
|
|
4
|
+
* 约束:本文件只放无运行时依赖的类型与常量,不得引入 class-validator / typeorm / nest
|
|
5
|
+
* 等框架依赖;服务端的校验装饰器与 ORM 实体各自保留,不在此定义。
|
|
6
|
+
* 协议字段统一 camelCase,与 guard_track 的实体属性同名;库列的 snake_case
|
|
7
|
+
* (如 appId → app_id)由服务端 ORM 命名策略负责,协议层不感知列名。
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** 上报类型取值(与服务端 guard_track.type 枚举一致) */
|
|
11
|
+
|
|
12
|
+
/** 类型取值列表(服务端校验 / 管理端筛选用) */
|
|
13
|
+
export var TRACK_TYPE_VALUES = ['error', 'visit', 'event', 'api'];
|
|
14
|
+
|
|
15
|
+
/** 类型展示标签(管理端表格、下拉用) */
|
|
16
|
+
export var TrackTypeLabel = {
|
|
17
|
+
error: '错误信息',
|
|
18
|
+
visit: '页面访问',
|
|
19
|
+
event: '用户触发',
|
|
20
|
+
api: '接口触发'
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 协议限额:服务端 DTO 校验与两侧截断共用同一份定义。
|
|
25
|
+
* 除 maxItems(协议约定)外,其余取值均对齐 guard_track 的列长,
|
|
26
|
+
* 不做无依据的额外限制——超长字段本身即会写入失败。
|
|
27
|
+
*/
|
|
28
|
+
export var TRACK_LIMITS = {
|
|
29
|
+
/** 单次批量 events[] 条数上限(SDK batchSize 与服务端 DTO 共用) */
|
|
30
|
+
maxItems: 20,
|
|
31
|
+
/** 探针版本号,guard_conf.version varchar(20) */
|
|
32
|
+
sdkVersion: 20,
|
|
33
|
+
/** 会话串,列默认 varchar(255) */
|
|
34
|
+
imei: 255,
|
|
35
|
+
/** 页面 URL,varchar(1000) */
|
|
36
|
+
href: 1000,
|
|
37
|
+
/** 内容类字段(content / module / user.*),列默认 varchar(255) */
|
|
38
|
+
content: 255,
|
|
39
|
+
module: 255,
|
|
40
|
+
/** 用户画像单个字段长度 */
|
|
41
|
+
userField: 255,
|
|
42
|
+
/** 用户 UA,varchar(1000) */
|
|
43
|
+
userAgent: 1000,
|
|
44
|
+
/** V3 通道整体 body 上限(含内嵌回放),main.ts 直接引用 */
|
|
45
|
+
bodyBytes: 1024 * 1024
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 上报请求体 Content-Type:固定 text/plain(body 仍是 JSON 文本)。
|
|
50
|
+
* text/plain 属 CORS safelist,跨域不触发预检——sendBeacon / fetch 在页面卸载时
|
|
51
|
+
* 无需等待 OPTIONS 完成,避免「预检未及时返回」导致的丢数;服务端据此收原始文本再解析 JSON。
|
|
52
|
+
*/
|
|
53
|
+
export var TRACK_CONTENT_TYPE = 'text/plain;charset=UTF-8';
|
|
54
|
+
|
|
55
|
+
/** 用户画像(请求级恒定);未登录时整段缺省 */
|
|
56
|
+
|
|
57
|
+
/** 条级事件:href / 视口在一个批量窗口内会变,逐条携带 */
|
|
58
|
+
|
|
59
|
+
/** 请求级公共段(信封):一个请求只带一份 */
|
|
60
|
+
|
|
61
|
+
/** 通道 1:批量日志 */
|
|
62
|
+
|
|
63
|
+
/** 通道 2 的错误明细 */
|
|
64
|
+
|
|
65
|
+
/** 通道 2:单条错误,可内嵌 rrweb 回放事件 */
|
|
@@ -1,10 +1,5 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
visit = "\u9875\u9762\u8BBF\u95EE",
|
|
4
|
-
event = "\u7528\u6237\u89E6\u53D1",
|
|
5
|
-
api = "\u63A5\u53E3\u89E6\u53D1"
|
|
6
|
-
}
|
|
7
|
-
export type TrackType = keyof typeof TrackTypeEnum;
|
|
1
|
+
export { TrackTypeLabel, TRACK_TYPE_VALUES } from './protocol';
|
|
2
|
+
export type { TrackType } from './protocol';
|
|
8
3
|
export interface UserProfile {
|
|
9
4
|
userId: string;
|
|
10
5
|
userName: string;
|
|
@@ -30,7 +25,7 @@ export interface AuthCenterUserProfile {
|
|
|
30
25
|
authLogMap?: Record<string, string>;
|
|
31
26
|
}
|
|
32
27
|
export interface SystemProfile {
|
|
33
|
-
|
|
28
|
+
imei: string;
|
|
34
29
|
innerHeight: number;
|
|
35
30
|
innerWidth: number;
|
|
36
31
|
href: string;
|
|
@@ -1,11 +1,6 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
TrackTypeEnum["visit"] = "\u9875\u9762\u8BBF\u95EE";
|
|
5
|
-
TrackTypeEnum["event"] = "\u7528\u6237\u89E6\u53D1";
|
|
6
|
-
TrackTypeEnum["api"] = "\u63A5\u53E3\u89E6\u53D1";
|
|
7
|
-
return TrackTypeEnum;
|
|
8
|
-
}({});
|
|
1
|
+
// 上报类型取值与展示标签统一由 protocol.ts 维护(唯一真源),此处仅做出口。
|
|
2
|
+
// 类型必须用 `export type` 再导出:值语法会让构建产物出现运行时再导出(TrackType 在运行时不存在)
|
|
3
|
+
export { TrackTypeLabel, TRACK_TYPE_VALUES } from "./protocol";
|
|
9
4
|
|
|
10
5
|
// 用户信息
|
|
11
6
|
|
package/lib/Track/Beacon.d.ts
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
|
+
import { type TrackV3Event } from '../interfaces/protocol';
|
|
2
|
+
/**
|
|
3
|
+
* 批量日志队列(通道 1)。
|
|
4
|
+
*
|
|
5
|
+
* 发送语义:sendBeacon 返回值只表示浏览器是否成功入队,无法感知服务端 4xx/413——
|
|
6
|
+
* 因此只对「入队失败」重试,被服务端拒绝的丢数由服务端侧计数告警补偿。
|
|
7
|
+
* 无 sendBeacon 环境(部分 WebView / 老浏览器)降级用 fetch(keepalive) 发送。
|
|
8
|
+
*/
|
|
1
9
|
export default class Beacon {
|
|
2
10
|
private static _queue;
|
|
3
|
-
static
|
|
4
|
-
|
|
5
|
-
static
|
|
11
|
+
private static _timer;
|
|
12
|
+
/** 连续发送失败次数,超过重试上限后丢弃当前批次,避免队列无限增长 */
|
|
13
|
+
private static _failCount;
|
|
14
|
+
private static readonly MAX_RETRIES;
|
|
15
|
+
/** 启动定时 flush(Track.init 时调用) */
|
|
16
|
+
static start(): void;
|
|
17
|
+
/** 入队,达到 batchSize 立即 flush */
|
|
18
|
+
static push(event: TrackV3Event): void;
|
|
19
|
+
/** 发送成功后再出队;失败保留队列等待下次 flush 重试 */
|
|
20
|
+
static flush(): void;
|
|
21
|
+
private static sendBatch;
|
|
22
|
+
private static trySendBeacon;
|
|
23
|
+
/** fetch keepalive:无 sendBeacon 环境的主要通道,已发出即视为送达(不等响应、不阻塞队列) */
|
|
24
|
+
private static trySendKeepalive;
|
|
6
25
|
}
|
package/lib/Track/Beacon.js
CHANGED
|
@@ -32,42 +32,84 @@ __export(Beacon_exports, {
|
|
|
32
32
|
default: () => Beacon
|
|
33
33
|
});
|
|
34
34
|
module.exports = __toCommonJS(Beacon_exports);
|
|
35
|
-
var
|
|
35
|
+
var import_protocol = require("../interfaces/protocol");
|
|
36
36
|
var import_Config = __toESM(require("./Config"));
|
|
37
|
+
var import_envelope = require("./envelope");
|
|
37
38
|
var Beacon = class {
|
|
38
|
-
|
|
39
|
-
static
|
|
40
|
-
this.
|
|
41
|
-
|
|
42
|
-
|
|
39
|
+
/** 启动定时 flush(Track.init 时调用) */
|
|
40
|
+
static start() {
|
|
41
|
+
if (this._timer)
|
|
42
|
+
return;
|
|
43
|
+
this._timer = setInterval(
|
|
44
|
+
() => this.flush(),
|
|
45
|
+
import_Config.default.data.flushInterval ?? 1e4
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
/** 入队,达到 batchSize 立即 flush */
|
|
49
|
+
static push(event) {
|
|
50
|
+
this._queue.push(event);
|
|
51
|
+
if (this._queue.length >= (import_Config.default.data.batchSize ?? import_protocol.TRACK_LIMITS.maxItems)) {
|
|
52
|
+
this.flush();
|
|
43
53
|
}
|
|
44
54
|
}
|
|
45
|
-
|
|
46
|
-
static
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
55
|
+
/** 发送成功后再出队;失败保留队列等待下次 flush 重试 */
|
|
56
|
+
static flush() {
|
|
57
|
+
while (this._queue.length > 0) {
|
|
58
|
+
const batchSize = import_Config.default.data.batchSize ?? import_protocol.TRACK_LIMITS.maxItems;
|
|
59
|
+
const batch = this._queue.slice(0, batchSize);
|
|
60
|
+
const payload = {
|
|
61
|
+
...(0, import_envelope.buildEnvelopeBase)(),
|
|
62
|
+
events: batch
|
|
63
|
+
};
|
|
64
|
+
if (this.sendBatch(JSON.stringify(payload)) === "retry")
|
|
65
|
+
return;
|
|
66
|
+
this._queue.splice(0, batch.length);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
static sendBatch(payload) {
|
|
70
|
+
const sent = this.trySendBeacon(payload) || this.trySendKeepalive(payload);
|
|
71
|
+
if (sent) {
|
|
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";
|
|
50
79
|
}
|
|
51
|
-
|
|
52
|
-
type: "application/json"
|
|
53
|
-
});
|
|
54
|
-
navigator == null ? void 0 : navigator.sendBeacon(`${import_Config.default.data.domain}${api}`, blob);
|
|
80
|
+
return "retry";
|
|
55
81
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
{
|
|
63
|
-
appId: import_Config.default.data.appId,
|
|
64
|
-
version: import_package.default.version
|
|
65
|
-
},
|
|
66
|
-
"/pousheng-guard/api/apps/version/update"
|
|
82
|
+
static trySendBeacon(payload) {
|
|
83
|
+
if (typeof navigator === "undefined" || !navigator.sendBeacon)
|
|
84
|
+
return false;
|
|
85
|
+
try {
|
|
86
|
+
return navigator.sendBeacon(
|
|
87
|
+
(0, import_envelope.trackUrl)("/batch"),
|
|
88
|
+
new Blob([payload], { type: import_protocol.TRACK_CONTENT_TYPE })
|
|
67
89
|
);
|
|
68
|
-
|
|
90
|
+
} catch (error) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/** fetch keepalive:无 sendBeacon 环境的主要通道,已发出即视为送达(不等响应、不阻塞队列) */
|
|
95
|
+
static trySendKeepalive(payload) {
|
|
96
|
+
if (typeof fetch !== "function")
|
|
97
|
+
return false;
|
|
98
|
+
try {
|
|
99
|
+
void fetch((0, import_envelope.trackUrl)("/batch"), {
|
|
100
|
+
method: "POST",
|
|
101
|
+
headers: { "Content-Type": import_protocol.TRACK_CONTENT_TYPE },
|
|
102
|
+
body: payload,
|
|
103
|
+
keepalive: true
|
|
104
|
+
}).catch(() => void 0);
|
|
105
|
+
return true;
|
|
106
|
+
} catch (error) {
|
|
107
|
+
return false;
|
|
69
108
|
}
|
|
70
109
|
}
|
|
71
110
|
};
|
|
72
|
-
// 消息队列
|
|
73
111
|
Beacon._queue = [];
|
|
112
|
+
Beacon._timer = null;
|
|
113
|
+
/** 连续发送失败次数,超过重试上限后丢弃当前批次,避免队列无限增长 */
|
|
114
|
+
Beacon._failCount = 0;
|
|
115
|
+
Beacon.MAX_RETRIES = 3;
|
package/lib/Track/Config.d.ts
CHANGED
|
@@ -3,6 +3,10 @@ export interface IConfig {
|
|
|
3
3
|
domain: string;
|
|
4
4
|
autoReportError?: boolean;
|
|
5
5
|
enableReplay?: boolean;
|
|
6
|
+
batchSize?: number;
|
|
7
|
+
flushInterval?: number;
|
|
8
|
+
replayMaxBytes?: number;
|
|
9
|
+
replayMaskTextClass?: string | RegExp;
|
|
6
10
|
}
|
|
7
11
|
export default class Config {
|
|
8
12
|
private static _data;
|
package/lib/Track/Config.js
CHANGED
|
@@ -22,6 +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");
|
|
25
26
|
var Config = class {
|
|
26
27
|
static validate(config) {
|
|
27
28
|
var _a, _b;
|
|
@@ -41,5 +42,8 @@ Config._data = {
|
|
|
41
42
|
appId: "",
|
|
42
43
|
domain: "",
|
|
43
44
|
autoReportError: true,
|
|
44
|
-
enableReplay: true
|
|
45
|
+
enableReplay: true,
|
|
46
|
+
batchSize: import_protocol.TRACK_LIMITS.maxItems,
|
|
47
|
+
flushInterval: 1e4,
|
|
48
|
+
replayMaxBytes: 1024 * 1024
|
|
45
49
|
};
|
package/lib/Track/Listener.js
CHANGED
|
@@ -45,16 +45,20 @@ var Listener = class {
|
|
|
45
45
|
clearTimeout(this._reporter);
|
|
46
46
|
}
|
|
47
47
|
this._reporter = setTimeout(() => {
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
var _a;
|
|
49
|
+
import_Beacon.default.flush();
|
|
50
|
+
import_Log.default.errorLog(event.message, (_a = event.error) == null ? void 0 : _a.stack);
|
|
50
51
|
this._reporter = null;
|
|
51
52
|
}, 1e3);
|
|
52
53
|
});
|
|
53
54
|
document.addEventListener("visibilitychange", () => {
|
|
54
55
|
if ((document == null ? void 0 : document.visibilityState) === "hidden") {
|
|
55
|
-
import_Beacon.default.
|
|
56
|
+
import_Beacon.default.flush();
|
|
56
57
|
}
|
|
57
58
|
});
|
|
59
|
+
window.addEventListener("pagehide", () => {
|
|
60
|
+
import_Beacon.default.flush();
|
|
61
|
+
});
|
|
58
62
|
this._loaded = true;
|
|
59
63
|
}
|
|
60
64
|
};
|
package/lib/Track/Log.d.ts
CHANGED
|
@@ -1,9 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 日志上报入口。
|
|
3
|
+
*
|
|
4
|
+
* 配置语义:`autoReportError` 只控制是否自动监听 window.onerror(见 Listener);
|
|
5
|
+
* 回放是否随错误一起上报只看 `enableReplay`(见 Replay.run)。
|
|
6
|
+
*/
|
|
1
7
|
export default class Log {
|
|
2
8
|
private static _module;
|
|
3
9
|
static get module(): string;
|
|
4
|
-
private static _commonProps;
|
|
5
10
|
static visitLog(module: string): void;
|
|
6
11
|
static eventLog(content: string): void;
|
|
7
|
-
static errorLog(message: string): Promise<void>;
|
|
8
12
|
static apiLog(log: string): void;
|
|
13
|
+
/**
|
|
14
|
+
* 错误 + 回放一次请求内嵌上报(通道 2)。
|
|
15
|
+
* fetch 失败或页面卸载导致未送达时,用 sendBeacon 补发不含回放的纯错误:
|
|
16
|
+
* 卸载场景 fetch 不可靠,至少保住错误本体,回放可接受丢失。
|
|
17
|
+
*/
|
|
18
|
+
static errorLog(message: string, stack?: string): Promise<void>;
|
|
9
19
|
}
|
package/lib/Track/Log.js
CHANGED
|
@@ -32,68 +32,79 @@ __export(Log_exports, {
|
|
|
32
32
|
default: () => Log
|
|
33
33
|
});
|
|
34
34
|
module.exports = __toCommonJS(Log_exports);
|
|
35
|
+
var import_protocol = require("../interfaces/protocol");
|
|
35
36
|
var import_Beacon = __toESM(require("./Beacon"));
|
|
36
|
-
var
|
|
37
|
+
var import_envelope = require("./envelope");
|
|
37
38
|
var import_Replay = __toESM(require("./Replay"));
|
|
38
|
-
var import_System = __toESM(require("./System"));
|
|
39
|
-
var import_User = __toESM(require("./User"));
|
|
40
39
|
var Log = class {
|
|
41
40
|
// 获取当前模块名称
|
|
42
41
|
static get module() {
|
|
43
42
|
return this._module;
|
|
44
43
|
}
|
|
45
|
-
// 上报日志的公共属性
|
|
46
|
-
static _commonProps() {
|
|
47
|
-
return {
|
|
48
|
-
...import_User.default.profile,
|
|
49
|
-
...import_System.default.profile,
|
|
50
|
-
appId: import_Config.default.data.appId,
|
|
51
|
-
module: this._module
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
44
|
static visitLog(module2) {
|
|
55
45
|
this._module = module2;
|
|
56
46
|
const lastModule = this._module.split("-").pop();
|
|
57
47
|
import_Beacon.default.push({
|
|
58
|
-
...
|
|
48
|
+
...(0, import_envelope.buildEventBase)(),
|
|
59
49
|
type: "visit",
|
|
50
|
+
module: module2,
|
|
60
51
|
content: `访问页面:${lastModule}`
|
|
61
52
|
});
|
|
62
53
|
}
|
|
63
54
|
static eventLog(content) {
|
|
64
55
|
import_Beacon.default.push({
|
|
65
|
-
...
|
|
56
|
+
...(0, import_envelope.buildEventBase)(),
|
|
66
57
|
type: "event",
|
|
58
|
+
module: this._module,
|
|
67
59
|
content
|
|
68
60
|
});
|
|
69
61
|
}
|
|
70
|
-
static async errorLog(message) {
|
|
71
|
-
const res = await fetch(
|
|
72
|
-
`${import_Config.default.data.domain}/pousheng-guard/api/track/error`,
|
|
73
|
-
{
|
|
74
|
-
method: "POST",
|
|
75
|
-
headers: {
|
|
76
|
-
"Content-Type": "application/json"
|
|
77
|
-
},
|
|
78
|
-
body: JSON.stringify({
|
|
79
|
-
...this._commonProps(),
|
|
80
|
-
type: "error",
|
|
81
|
-
log: JSON.stringify({ message })
|
|
82
|
-
})
|
|
83
|
-
}
|
|
84
|
-
);
|
|
85
|
-
const { data: id } = await res.json();
|
|
86
|
-
if (import_Config.default.data.autoReportError) {
|
|
87
|
-
import_Replay.default.reportSnapshot(id);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
62
|
static apiLog(log) {
|
|
91
63
|
import_Beacon.default.push({
|
|
92
|
-
...
|
|
64
|
+
...(0, import_envelope.buildEventBase)(),
|
|
93
65
|
type: "api",
|
|
66
|
+
module: this._module,
|
|
94
67
|
log
|
|
95
68
|
});
|
|
96
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* 错误 + 回放一次请求内嵌上报(通道 2)。
|
|
72
|
+
* fetch 失败或页面卸载导致未送达时,用 sendBeacon 补发不含回放的纯错误:
|
|
73
|
+
* 卸载场景 fetch 不可靠,至少保住错误本体,回放可接受丢失。
|
|
74
|
+
*/
|
|
75
|
+
static async errorLog(message, stack) {
|
|
76
|
+
var _a;
|
|
77
|
+
const replay = import_Replay.default.snapshot();
|
|
78
|
+
const pureError = {
|
|
79
|
+
...(0, import_envelope.buildEnvelopeBase)(),
|
|
80
|
+
error: {
|
|
81
|
+
...(0, import_envelope.buildEventBase)(),
|
|
82
|
+
message,
|
|
83
|
+
stack,
|
|
84
|
+
module: this._module
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
const payload = replay.length ? { ...pureError, replay } : pureError;
|
|
88
|
+
try {
|
|
89
|
+
const res = await fetch((0, import_envelope.trackUrl)("/error"), {
|
|
90
|
+
method: "POST",
|
|
91
|
+
headers: { "Content-Type": import_protocol.TRACK_CONTENT_TYPE },
|
|
92
|
+
body: JSON.stringify(payload)
|
|
93
|
+
});
|
|
94
|
+
if (!res.ok)
|
|
95
|
+
throw new Error(`HTTP ${res.status}`);
|
|
96
|
+
import_Replay.default.commitSnapshot(replay);
|
|
97
|
+
} catch (error) {
|
|
98
|
+
try {
|
|
99
|
+
(_a = navigator == null ? void 0 : navigator.sendBeacon) == null ? void 0 : _a.call(
|
|
100
|
+
navigator,
|
|
101
|
+
(0, import_envelope.trackUrl)("/error"),
|
|
102
|
+
new Blob([JSON.stringify(pureError)], { type: import_protocol.TRACK_CONTENT_TYPE })
|
|
103
|
+
);
|
|
104
|
+
} catch (beaconError) {
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
97
108
|
};
|
|
98
109
|
// 当前模块
|
|
99
110
|
Log._module = "";
|
package/lib/Track/Replay.d.ts
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
|
+
type RRWebEvent = any;
|
|
1
2
|
export default class Replay {
|
|
2
3
|
private static _events;
|
|
3
4
|
private static _stop;
|
|
5
|
+
private static _bytes;
|
|
4
6
|
static run(): void;
|
|
5
|
-
|
|
7
|
+
/** 截断到体积上限,保留最近片段 */
|
|
8
|
+
private static truncate;
|
|
9
|
+
/** 快照当前回放片段(不移除,待上报成功后 commitSnapshot) */
|
|
10
|
+
static snapshot(): RRWebEvent[];
|
|
11
|
+
/** 上报成功后移除已携带的事件,继续录制——不调用 stop,避免下一个错误重复携带上一段 */
|
|
12
|
+
static commitSnapshot(snapshot: RRWebEvent[]): void;
|
|
6
13
|
}
|
|
14
|
+
export {};
|
package/lib/Track/Replay.js
CHANGED
|
@@ -44,10 +44,13 @@ var Replay = class {
|
|
|
44
44
|
this._stop = (0, import_record.record)({
|
|
45
45
|
emit: (e) => {
|
|
46
46
|
this._events.push(e);
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
this._events.splice(0, over);
|
|
47
|
+
this._bytes += JSON.stringify(e).length;
|
|
48
|
+
this.truncate();
|
|
50
49
|
},
|
|
50
|
+
// 输入掩码:password 类型强制掩码,防止敏感输入进入回放文件;
|
|
51
|
+
// 其余类型可由接入方通过 replayMaskTextClass 扩展
|
|
52
|
+
maskInputOptions: { password: true },
|
|
53
|
+
...import_Config.default.data.replayMaskTextClass ? { maskTextClass: import_Config.default.data.replayMaskTextClass } : {},
|
|
51
54
|
plugins: [
|
|
52
55
|
(0, import_rrweb_plugin_console_record.getRecordConsolePlugin)({
|
|
53
56
|
level: ["log", "info", "warn", "error", "debug"]
|
|
@@ -58,25 +61,34 @@ var Replay = class {
|
|
|
58
61
|
this._stop = void 0;
|
|
59
62
|
}
|
|
60
63
|
}
|
|
61
|
-
|
|
62
|
-
static
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
64
|
+
/** 截断到体积上限,保留最近片段 */
|
|
65
|
+
static truncate() {
|
|
66
|
+
const maxBytes = import_Config.default.data.replayMaxBytes ?? 1024 * 1024;
|
|
67
|
+
while (this._bytes > maxBytes && this._events.length > 1) {
|
|
68
|
+
const removed = this._events.shift();
|
|
69
|
+
this._bytes -= removed ? JSON.stringify(removed).length : 0;
|
|
66
70
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
71
|
+
}
|
|
72
|
+
/** 快照当前回放片段(不移除,待上报成功后 commitSnapshot) */
|
|
73
|
+
static snapshot() {
|
|
74
|
+
return this._stop ? [...this._events] : [];
|
|
75
|
+
}
|
|
76
|
+
/** 上报成功后移除已携带的事件,继续录制——不调用 stop,避免下一个错误重复携带上一段 */
|
|
77
|
+
static commitSnapshot(snapshot) {
|
|
78
|
+
if (!snapshot.length)
|
|
79
|
+
return;
|
|
80
|
+
const sent = new Set(snapshot);
|
|
81
|
+
this._events = this._events.filter((e) => {
|
|
82
|
+
if (sent.has(e)) {
|
|
83
|
+
this._bytes -= JSON.stringify(e).length;
|
|
84
|
+
return false;
|
|
75
85
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
this.
|
|
79
|
-
|
|
86
|
+
return true;
|
|
87
|
+
});
|
|
88
|
+
if (this._bytes < 0)
|
|
89
|
+
this._bytes = 0;
|
|
80
90
|
}
|
|
81
91
|
};
|
|
82
92
|
Replay._events = [];
|
|
93
|
+
// 录制阶段的累计体积(近似值),达到 replayMaxBytes 后从最早事件开始截断
|
|
94
|
+
Replay._bytes = 0;
|
package/lib/Track/System.d.ts
CHANGED