@yysports/tools 2.2.7 → 3.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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 +19 -2
- package/dist/Track/Replay.js +97 -48
- 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 +4 -2
- 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 +19 -2
- package/lib/Track/Replay.js +53 -26
- 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 +2 -2
- 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/System.js
CHANGED
|
@@ -7,10 +7,16 @@ var System = /*#__PURE__*/function () {
|
|
|
7
7
|
_classCallCheck(this, System);
|
|
8
8
|
}
|
|
9
9
|
_createClass(System, null, [{
|
|
10
|
+
key: "imei",
|
|
11
|
+
get: /** 会话串,作为信封必填的 imei 上报(生成逻辑不变) */
|
|
12
|
+
function get() {
|
|
13
|
+
return this._imei;
|
|
14
|
+
}
|
|
15
|
+
}, {
|
|
10
16
|
key: "profile",
|
|
11
17
|
get: function get() {
|
|
12
18
|
return {
|
|
13
|
-
|
|
19
|
+
imei: this._imei,
|
|
14
20
|
innerHeight: typeof window !== 'undefined' ? window.innerHeight : 0,
|
|
15
21
|
innerWidth: typeof window !== 'undefined' ? window.innerWidth : 0,
|
|
16
22
|
href: typeof location !== 'undefined' ? location.href : '',
|
|
@@ -20,5 +26,5 @@ var System = /*#__PURE__*/function () {
|
|
|
20
26
|
}]);
|
|
21
27
|
return System;
|
|
22
28
|
}();
|
|
23
|
-
_defineProperty(System, "
|
|
29
|
+
_defineProperty(System, "_imei", generateRandomString(6));
|
|
24
30
|
export { System as default };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { TrackV3Envelope, TrackV3Event } from '../interfaces/protocol';
|
|
2
|
+
/** 请求级公共段(信封):字段与实体属性同名(camelCase),每次请求仅一份 */
|
|
3
|
+
export declare function buildEnvelopeBase(): TrackV3Envelope;
|
|
4
|
+
/** 条级事件公共字段:href / 视口 / 毫秒时间戳在批量窗口内会变,逐条携带 */
|
|
5
|
+
export declare function buildEventBase(): Pick<TrackV3Event, 'systemTime' | 'href' | 'innerWidth' | 'innerHeight'>;
|
|
6
|
+
/** V3 上报地址:鉴权参数位统一走 URL query(sendBeacon 无法自定义 header) */
|
|
7
|
+
export declare function trackUrl(path: '/batch' | '/error'): string;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
2
|
+
import packageJson from "../../package.json";
|
|
3
|
+
import Config from "./Config";
|
|
4
|
+
import System from "./System";
|
|
5
|
+
import User from "./User";
|
|
6
|
+
|
|
7
|
+
/** 请求级公共段(信封):字段与实体属性同名(camelCase),每次请求仅一份 */
|
|
8
|
+
export function buildEnvelopeBase() {
|
|
9
|
+
var profile = User.profile;
|
|
10
|
+
return _objectSpread({
|
|
11
|
+
appId: Config.data.appId,
|
|
12
|
+
sdkVersion: packageJson.version,
|
|
13
|
+
imei: System.imei
|
|
14
|
+
}, profile ? {
|
|
15
|
+
user: {
|
|
16
|
+
userId: profile.userId,
|
|
17
|
+
userName: profile.userName,
|
|
18
|
+
orgCode: profile.orgCode,
|
|
19
|
+
orgTitle: profile.orgTitle,
|
|
20
|
+
dataScopeZonePermissions: profile.dataScopeZonePermissions,
|
|
21
|
+
dataScopeRoles: profile.dataScopeRoles
|
|
22
|
+
}
|
|
23
|
+
} : {});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** 条级事件公共字段:href / 视口 / 毫秒时间戳在批量窗口内会变,逐条携带 */
|
|
27
|
+
export function buildEventBase() {
|
|
28
|
+
return {
|
|
29
|
+
systemTime: Date.now(),
|
|
30
|
+
href: typeof location !== 'undefined' ? location.href : '',
|
|
31
|
+
innerWidth: typeof window !== 'undefined' ? window.innerWidth : 0,
|
|
32
|
+
innerHeight: typeof window !== 'undefined' ? window.innerHeight : 0
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** V3 上报地址:鉴权参数位统一走 URL query(sendBeacon 无法自定义 header) */
|
|
37
|
+
export function trackUrl(path) {
|
|
38
|
+
var appId = encodeURIComponent(Config.data.appId);
|
|
39
|
+
return "".concat(Config.data.domain, "/pousheng-guard/api/track/v3").concat(path, "?appId=").concat(appId);
|
|
40
|
+
}
|
package/dist/Track/index.js
CHANGED
|
@@ -44,8 +44,10 @@ var Track = /*#__PURE__*/function () {
|
|
|
44
44
|
key: "run",
|
|
45
45
|
value: function run() {
|
|
46
46
|
Listener.run();
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
// 回放为异步启动(rrweb 懒加载),不阻塞 init;未开回放时不会加载 rrweb
|
|
48
|
+
void Replay.run();
|
|
49
|
+
// 版本上报已并入信封 sdkVersion,不再有独立请求
|
|
50
|
+
Beacon.start();
|
|
49
51
|
}
|
|
50
52
|
|
|
51
53
|
// 获取当前页面模块名称
|
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,23 @@
|
|
|
1
|
+
type RRWebEvent = any;
|
|
1
2
|
export default class Replay {
|
|
2
3
|
private static _events;
|
|
3
4
|
private static _stop;
|
|
4
|
-
static
|
|
5
|
-
static
|
|
5
|
+
private static _loading;
|
|
6
|
+
private static _bytes;
|
|
7
|
+
/**
|
|
8
|
+
* 启动录制。
|
|
9
|
+
*
|
|
10
|
+
* rrweb(record 与 console 插件)改为动态 import:`enableReplay: false` 时完全不加载,
|
|
11
|
+
* 接入方的打包产物里也就不含 rrweb;开启回放时 rrweb 由接入方打包器拆成独立异步 chunk。
|
|
12
|
+
* 代价是启动异步化——加载完成前发生的错误不带回放(`snapshot()` 返回空数组,
|
|
13
|
+
* 错误本体的上报不受影响)。
|
|
14
|
+
*/
|
|
15
|
+
static run(): Promise<void>;
|
|
16
|
+
/** 截断到体积上限,保留最近片段 */
|
|
17
|
+
private static truncate;
|
|
18
|
+
/** 快照当前回放片段(不移除,待上报成功后 commitSnapshot) */
|
|
19
|
+
static snapshot(): RRWebEvent[];
|
|
20
|
+
/** 上报成功后移除已携带的事件,继续录制——不调用 stop,避免下一个错误重复携带上一段 */
|
|
21
|
+
static commitSnapshot(snapshot: RRWebEvent[]): void;
|
|
6
22
|
}
|
|
23
|
+
export {};
|