tdem-web-sdk 1.0.1 → 1.0.7

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.
@@ -20,6 +20,10 @@ export interface ClickDetectorOptions {
20
20
  onSlowClick?: (data: SlowClickData) => void;
21
21
  /** 愤怒点击回调 */
22
22
  onRageClick?: (data: RageClickData) => void;
23
+ /** 点击开始回调 */
24
+ onClickStart?: (data: ClickStartData) => void;
25
+ /** 点击产生有效响应回调 */
26
+ onEffectiveAction?: (data: EffectiveActionData) => void;
23
27
  /** 死点击回调 */
24
28
  onDeadClick?: (data: DeadClickData) => void;
25
29
  }
@@ -63,6 +67,22 @@ export interface ClickNodeInfo {
63
67
  /** CSS 选择器 */
64
68
  selector?: string;
65
69
  }
70
+ /** 有效动作原因 */
71
+ export declare type EffectiveActionReason = 'dom_mutation' | 'spa_navigation' | 'page_unload' | 'external_navigation' | 'window_open' | 'download' | 'form_submit' | 'form_change' | 'api_success' | 'clipboard' | 'custom';
72
+ /** 点击开始数据 */
73
+ export interface ClickStartData {
74
+ node: ClickNodeInfo;
75
+ timestamp: number;
76
+ target: HTMLElement;
77
+ event: PointerEvent;
78
+ effectiveActionReason?: EffectiveActionReason;
79
+ }
80
+ /** 有效动作数据 */
81
+ export interface EffectiveActionData {
82
+ reason: EffectiveActionReason;
83
+ timestamp: number;
84
+ node?: ClickNodeInfo;
85
+ }
66
86
  /**
67
87
  * ClickDetector - 点击行为检测器
68
88
  */
@@ -111,9 +131,15 @@ export declare class ClickDetector {
111
131
  private cleanupSlowClick;
112
132
  private isSameTarget;
113
133
  /**
114
- * 检查是否为可点击元素
134
+ * 标记当前点击已产生有效动作
135
+ */
136
+ markEffectiveAction(reason: EffectiveActionReason): void;
137
+ private emitEffectiveAction;
138
+ /**
139
+ * 获取可点击元素
115
140
  */
116
- private isClickableElement;
141
+ private getClickableElement;
142
+ private getExternalNavigationReason;
117
143
  /**
118
144
  * 获取节点信息
119
145
  */
@@ -13,8 +13,18 @@ export declare const MAX_REPLAY_DURATION: number;
13
13
  export declare const MIN_REPLAY_DURATION: number;
14
14
  /** 事件缓冲区最大大小 (5MB) */
15
15
  export declare const MAX_BUFFER_SIZE: number;
16
- /** 单个 Segment 上报最大大小 (3MB),需小于后端 maxEventsSizeBytes(5MB) */
16
+ /** 单个 Segment 上报最大大小 (3MB),需小于后端 maxEventsSizeBytes(5MB),仅用于 fetch 上报路径 */
17
17
  export declare const MAX_SEGMENT_SIZE: number;
18
+ /**
19
+ * sendBeacon 单个分片最大大小 (50KB)。
20
+ * navigator.sendBeacon 和 fetch(keepalive:true) 共享浏览器强制的 64KB 排队上限
21
+ * (Fetch 规范定义,Chrome/Safari 均严格执行,超出直接同步返回 false,请求根本不会发出)。
22
+ * 这里按 events 字段压缩前的原始 JSON 长度计算,留出安全余量给:
23
+ * 1. 上报体里 project_key/session_id/replay_id 等其它字段的开销(约几百字节);
24
+ * 2. events 字段即使开启 gzip+base64 压缩,也不保证压缩后一定比原始尺寸小很多;
25
+ * 仅用于 unload 时的 sendBeacon 上报路径,不影响 fetch 路径的 MAX_SEGMENT_SIZE。
26
+ */
27
+ export declare const BEACON_MAX_CHUNK_SIZE: number;
18
28
  /** 默认 flush 间隔 (20秒) */
19
29
  export declare const DEFAULT_FLUSH_INTERVAL: number;
20
30
  /** 最小 flush 事件数量 (至少10个事件才上报) */
@@ -27,6 +37,8 @@ export declare const BUFFER_CHECKOUT_EVERY_NMS: number;
27
37
  export declare const DEFAULT_SESSION_SAMPLE_RATE = 0.1;
28
38
  /** 默认错误采样率 (100%) */
29
39
  export declare const DEFAULT_ERROR_SAMPLE_RATE = 1;
40
+ /** buffer 模式下默认仅错误时上报(降本意图) */
41
+ export declare const DEFAULT_ERROR_ONLY_UPLOAD = true;
30
42
  /** Session 存储 key */
31
43
  export declare const SESSION_STORAGE_KEY = "tdem_session";
32
44
  /** 上报 API 路径 */
@@ -1,38 +1,35 @@
1
1
  import { Session, SamplingMode } from './types';
2
2
  export interface SessionManagerConfig {
3
- /** 是否使用 sticky session(跨页面保持会话) */
3
+ /**
4
+ * @deprecated 已废弃,构造函数不再读取此字段,不产生任何效果。
5
+ * 保留仅为兼容旧调用方传参不报错。
6
+ */
4
7
  stickySession?: boolean;
5
- /** 会话空闲过期时间 (ms) */
6
- idleExpireDuration?: number;
7
- /** 会话最大时长 (ms) */
8
- maxSessionDuration?: number;
9
8
  }
10
9
  export declare class SessionManager {
11
10
  private session;
12
- private config;
13
11
  /** TDEM 实例引用,用于同步 bean.sessionId */
14
12
  private tdemInstance;
15
- constructor(config?: SessionManagerConfig);
13
+ constructor(_config?: SessionManagerConfig);
16
14
  /**
17
15
  * 设置 TDEM 实例引用(由 session-replay 插件调用)
18
- * 用于在 session 创建/恢复后同步 bean.sessionId
16
+ * 用于在 session 创建后同步 bean.sessionId
19
17
  */
20
18
  setTdemInstance(tdem: any): void;
21
19
  /**
22
- * 创建新会话
20
+ * 创建当前文档生命周期的 replay 会话
23
21
  */
24
22
  createSession(sampled?: SamplingMode): Session;
25
23
  /**
26
- * 从 SessionStorage 恢复会话
27
- * @returns 恢复的会话,如果无法恢复则返回 null
24
+ * 只恢复当前文档内存中的 session,不从 sessionStorage 恢复 replay_id。
28
25
  */
29
26
  restoreSession(): Session | null;
30
27
  /**
31
- * 保存会话到 SessionStorage
28
+ * 新口径下 replay_id 只保存在内存/window 文档级上下文中,不持久化到 sessionStorage。
32
29
  */
33
30
  saveSession(): void;
34
31
  /**
35
- * 刷新会话(创建新会话,保留关联)
32
+ * 显式刷新:仅用于 reset / invalidate 等主动场景,不再由 idle/max duration 触发。
36
33
  */
37
34
  refreshSession(): Session;
38
35
  /**
@@ -44,13 +41,9 @@ export declare class SessionManager {
44
41
  */
45
42
  incrementSegmentId(): number;
46
43
  /**
47
- * 检查是否应该刷新会话
44
+ * SDK 侧已取消 15 分钟 idle / 60 分钟 max duration 自动刷新。
48
45
  */
49
46
  shouldRefreshSession(): boolean;
50
- /**
51
- * 检查会话是否过期
52
- */
53
- private isSessionExpired;
54
47
  /**
55
48
  * 获取当前会话
56
49
  */
@@ -63,12 +56,9 @@ export declare class SessionManager {
63
56
  * 获取录屏 ID
64
57
  */
65
58
  getReplayId(): string;
66
- /**
67
- * 获取 SessionStorage(处理不可用情况)
68
- */
69
- private getSessionStorage;
70
59
  /**
71
60
  * 清除会话
72
61
  */
73
62
  clearSession(): void;
63
+ private syncSharedSession;
74
64
  }
@@ -3,6 +3,7 @@
3
3
  */
4
4
  import type { eventWithTime } from '@rrweb/types';
5
5
  import type { recordOptions } from 'rrweb';
6
+ import type { Transport } from "../../../../core/src";
6
7
  /** 会话回放插件配置 */
7
8
  export interface SessionReplayConfig {
8
9
  /** 是否启用 */
@@ -11,7 +12,24 @@ export interface SessionReplayConfig {
11
12
  sessionSampleRate?: number;
12
13
  /** 错误采样率 (0-1) */
13
14
  errorSampleRate?: number;
14
- /** 是否使用 sticky session(跨页面保持会话) */
15
+ /**
16
+ * buffer 模式下是否仅错误时上报(默认 true)
17
+ * 开启时:buffer 模式下 visibilitychange/unload/rrweb checkout 不触发 flush,
18
+ * 仅错误触发时才上报回放数据(降本意图)
19
+ * 关闭时:buffer 模式下上述触发点照常 flush(兼容旧行为)
20
+ *
21
+ * 优先级:本地配置 > 远程配置。
22
+ * 若此处显式传入了该字段(true 或 false),远程配置接口返回的 error_only_upload
23
+ * 字段将被忽略;仅当本地未传入(undefined)时,远程配置才会生效(远程接口待开发)。
24
+ */
25
+ errorOnlyUpload?: boolean;
26
+ /**
27
+ * @deprecated 已废弃,不再产生任何效果。
28
+ * 旧口径下用于控制是否从 sessionStorage 恢复 replay_id;
29
+ * 现在会话共享改为基于 Cookie 的站点级 sessionId 机制(见 shared-session.ts),
30
+ * SessionManager 构造函数已不再读取此字段(见 session-manager.ts 的 SessionManagerConfig)。
31
+ * 保留此字段仅为兼容旧调用方传参不报错,计划在下一个大版本移除。
32
+ */
15
33
  stickySession?: boolean;
16
34
  /** 隐私配置 */
17
35
  privacy?: PrivacyConfig;
@@ -64,6 +82,11 @@ export interface SessionReplayConfig {
64
82
  * 浏览器不支持 Worker 或 CSP 限制时自动降级为同步模式,无需额外处理。
65
83
  */
66
84
  useCompression?: boolean;
85
+ /**
86
+ * 是否启用 replay events 字段级 gzip+base64 压缩(默认 true)
87
+ * 仅压缩上报体中的 events 字段,HTTP 请求体仍保持 application/json。
88
+ */
89
+ eventsCompression?: boolean;
67
90
  /**
68
91
  * 是否开启调试日志(默认 false)
69
92
  * 开启后会输出 session-replay 所有模块的运行日志,方便排查问题。
@@ -103,7 +126,7 @@ export interface NetworkRequest {
103
126
  }
104
127
  /** 会话数据结构 */
105
128
  export interface Session {
106
- /** 会话ID: uuid4() */
129
+ /** 站点级会话 ID,来自 _tdem_s Cookie */
107
130
  id: string;
108
131
  /** 录屏ID: rep_${32位UUID} */
109
132
  replayId: string;
@@ -115,8 +138,8 @@ export interface Session {
115
138
  segmentId: number;
116
139
  /** 采样模式 */
117
140
  sampled: 'session' | 'buffer' | false;
118
- /** 前一个会话ID */
119
- previousSessionId?: string;
141
+ /** 前一个文档生命周期 replay ID */
142
+ previousReplayId?: string;
120
143
  }
121
144
  /** 采样模式类型 */
122
145
  export declare type SamplingMode = 'session' | 'buffer' | false;
@@ -165,6 +188,8 @@ export interface BufferFinishResult {
165
188
  /** 未压缩大小 */
166
189
  uncompressedSize: number;
167
190
  }
191
+ /** replay events 字段编码方式 */
192
+ export declare type ReplayEventsEncoding = 'plain_json' | 'gzip_base64';
168
193
  /**
169
194
  * 录屏 Segment 上报数据
170
195
  * @see 后端 API: POST /api/v1/collect/replay
@@ -182,7 +207,13 @@ export interface ReplayUploadPayload {
182
207
  timestamp: number;
183
208
  /** 录屏开始时间 (毫秒) */
184
209
  replay_start_timestamp: number;
185
- /** rrweb events JSON 数组字符串 */
210
+ /** 回放格式,Web SDK 固定为 rrweb */
211
+ replay_format?: 'rrweb';
212
+ /** 平台标识,Web SDK 固定为 web */
213
+ platform?: 'web';
214
+ /** events 字段编码方式;为空时后端按 plain_json 兼容旧协议 */
215
+ events_encoding?: ReplayEventsEncoding;
216
+ /** rrweb events JSON 数组字符串,或 base64(gzip(JSON数组字符串)) */
186
217
  events: string;
187
218
  }
188
219
  /** 上报响应 */
@@ -212,6 +243,10 @@ export interface UploaderConfig {
212
243
  beforeSend?: (payload: ReplayUploadPayload) => ReplayUploadPayload | null;
213
244
  /** 错误回调 */
214
245
  onError?: (error: Error) => void;
246
+ /** 是否启用 events 字段级 gzip+base64 压缩 */
247
+ eventsCompression?: boolean;
248
+ /** SDK 内部 Transport(不属于公开配置) */
249
+ transport?: Transport;
215
250
  }
216
251
  /** Replay ID 生成器 */
217
252
  export declare class ReplayIdGenerator {
@@ -2,7 +2,7 @@ import { ReplayUploadPayload, UploaderConfig } from './types';
2
2
  export declare class Uploader {
3
3
  private config;
4
4
  private isUploading;
5
- /** 是否启用 gzip 压缩,默认关闭,方便开发调试 */
5
+ /** @deprecated 旧的整包 gzip 开关已废弃,replay 上报改为 events 字段级压缩 */
6
6
  useCompression: boolean;
7
7
  constructor(config: UploaderConfig);
8
8
  /**
@@ -14,11 +14,15 @@ export declare class Uploader {
14
14
  */
15
15
  private doUploadWithRetry;
16
16
  /**
17
- * 压缩数据
17
+ * 准备网络上报 payload:仅压缩 events 字段,HTTP body 仍保持 JSON。
18
18
  */
19
- private compressData;
19
+ private prepareUploadPayload;
20
20
  /**
21
- * 执行上报(根据配置决定是否压缩)
21
+ * Uint8Array 转标准 base64,分片处理避免大数组调用栈溢出。
22
+ */
23
+ private uint8ArrayToBase64;
24
+ /**
25
+ * 执行上报
22
26
  */
23
27
  private doUpload;
24
28
  /**
@@ -4,10 +4,20 @@ export interface StruggleMonitorConfig {
4
4
  ruleVersion?: string;
5
5
  payloadAllowlist?: string[];
6
6
  click?: {
7
+ enabled?: boolean;
7
8
  deadClickWindowMs?: number;
8
9
  rageClickWindowMs?: number;
9
10
  rageClickCount?: number;
10
11
  errorClickWindowMs?: number;
12
+ resolveOnDomMutation?: boolean;
13
+ resolveOnSpaNavigation?: boolean;
14
+ resolveOnExternalNavigation?: boolean;
15
+ resolveOnWindowOpen?: boolean;
16
+ resolveOnPageUnload?: boolean;
17
+ resolveOnFormAction?: boolean;
18
+ resolveOnApiSuccess?: boolean;
19
+ ignoreDeadClickSelectors?: string[];
20
+ effectiveActionSelectors?: string[];
11
21
  };
12
22
  performance?: {
13
23
  enabled?: boolean;
@@ -1,5 +1,5 @@
1
- import Core, { SendOption, SendSuccess, SendFail } from "../../core/src";
2
- import { MarkDoms, WebConfig } from './interface';
1
+ import Core, { SendOption, SendSuccess, SendFail, InternalSdkOptions } from "../../core/src";
2
+ import { MarkDoms, InternalWebConfig } from './interface';
3
3
  export default class TDEM extends Core {
4
4
  static sessionID: string;
5
5
  sendNow: boolean;
@@ -14,8 +14,53 @@ export default class TDEM extends Core {
14
14
  timing: number;
15
15
  markDoms: MarkDoms;
16
16
  };
17
- constructor(config: WebConfig);
18
- private addExtBean;
17
+ /**
18
+ * SDK 是否已完成初始化(远程配置返回 enabled=true 后才会变为 true)
19
+ * 临时方案:先拉远程配置,enabled 时才初始化插件,确保发布初期稳定可控
20
+ */
21
+ _sdkInitialized: boolean;
22
+ constructor(config: InternalWebConfig, internalOptions?: InternalSdkOptions);
23
+ /**
24
+ * 初始化启动入口。
25
+ *
26
+ * 这里同时保留两套路径:
27
+ * 1. 临时方案:初始化前先拉远程配置(当前默认)
28
+ * 2. 原始方案:构造时立即初始化 SDK
29
+ *
30
+ * 后续如果要恢复原逻辑,不需要再重构代码,
31
+ * 只需把 ENABLE_REMOTE_CONFIG_BEFORE_INIT 改为 false。
32
+ */
33
+ private _bootstrapInit;
34
+ /**
35
+ * 原始初始化方式:构造函数中直接初始化 SDK。
36
+ *
37
+ * 这条路径保留不删,方便发布观察期结束后快速切回。
38
+ * 当 ENABLE_REMOTE_CONFIG_BEFORE_INIT=false 时,就会恢复为原来的初始化行为。
39
+ */
40
+ private _initImmediately;
41
+ /**
42
+ * 临时方案:前置远程配置获取,根据 enabled 决定是否初始化插件。
43
+ *
44
+ * 说明:
45
+ * - 这只是发布初期的兜底方案,用于确保功能开关可控
46
+ * - 稳定后建议切回 _initImmediately(config) 路径
47
+ */
48
+ private _prefetchConfigAndInit;
49
+ private _buildPrefetchConfigUrl;
50
+ private _loadEnabledFromCache;
51
+ /**
52
+ * 临时方案下的初始化收口:
53
+ * setConfig + setConfigUrl 已在 _bootstrapInit 中提前执行过,
54
+ * 因此这里只补做插件 patch,避免重复触发 onConfigChange。
55
+ */
56
+ private _initAfterPreparedConfig;
57
+ /**
58
+ * init() 完成后的公共收尾逻辑。
59
+ *
60
+ * 无论走原始初始化路径还是临时前置拉配置路径,
61
+ * 这部分后置逻辑都应该保持一致,避免两套流程继续分叉。
62
+ */
63
+ private _afterInit;
19
64
  getBean(filter?: string[]): string;
20
65
  private getCurrentPageUrl;
21
66
  getOriginFrom(): string;
@@ -0,0 +1,6 @@
1
+ import type { Transport, TransportCallbacks, TransportRequest, TransportResult } from "../../../core/src";
2
+ export declare class BrowserTransport implements Transport {
3
+ request(request: TransportRequest, callbacks?: TransportCallbacks): Promise<TransportResult>;
4
+ handoff(request: TransportRequest, callbacks?: TransportCallbacks): Promise<TransportResult>;
5
+ private applyHeaders;
6
+ }
@@ -4,8 +4,8 @@
4
4
  * Aegis SDK 与 TDEM SDK 共享 Session ID 的协议模块。
5
5
  * 两个 SDK 各内联一份完全相同的代码(零包级依赖)。
6
6
  *
7
- * ⚠️ 修改时需同步更新 TDEM SDK 的同名文件:
8
- * tdem/sdk/web/packages/web-sdk/src/util/shared-session.ts
7
+ * ⚠️ 修改时需同步更新 Aegis SDK 的同名文件:
8
+ * aegis-sdk/packages/web-sdk/src/util/shared-session.ts
9
9
  */
10
10
  interface MonitorSession {
11
11
  /** 共享的 session ID(纯 UUID v4 格式) */
@@ -21,7 +21,7 @@ interface MonitorSession {
21
21
  }
22
22
  /**
23
23
  * 获取或创建共享 session
24
- * 优先级:window 全局对象 > sessionStorage > 新建
24
+ * 优先级:Cookie _tdem_s > window 全局对象 > 新建
25
25
  */
26
26
  export declare function getOrCreateSharedSession(source: 'aegis' | 'tdem', generateId: () => string): MonitorSession;
27
27
  /**
@@ -30,7 +30,7 @@ export declare function getOrCreateSharedSession(source: 'aegis' | 'tdem', gener
30
30
  export declare function getSharedSessionId(): string | null;
31
31
  /**
32
32
  * 当 session 刷新时更新共享对象并通知其他 SDK
33
- * 同时更新 sessionStorage 持久化
33
+ * 同时更新 Cookie 和 sessionStorage 缓存
34
34
  */
35
35
  export declare function updateSharedSession(newSessionId: string, source: 'aegis' | 'tdem'): void;
36
36
  /**