@fz-firefly/experience 0.0.0-stage → 0.6.0
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/dist/index.cjs +120 -0
- package/dist/index.d.cts +15 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +119 -0
- package/dist/objectSpread2-BYC7ck9c.cjs +107 -0
- package/dist/objectSpread2-lWxa9t4g.js +78 -0
- package/dist/options-C2iLgUnA.d.cts +115 -0
- package/dist/options-C2iLgUnA.d.ts +115 -0
- package/dist/remote.cjs +236 -0
- package/dist/remote.d.cts +10 -0
- package/dist/remote.d.ts +10 -0
- package/dist/remote.js +235 -0
- package/dist/replay.cjs +301 -0
- package/dist/replay.d.cts +19 -0
- package/dist/replay.d.ts +19 -0
- package/dist/replay.js +299 -0
- package/dist/white-screen.cjs +275 -0
- package/dist/white-screen.d.cts +35 -0
- package/dist/white-screen.d.ts +35 -0
- package/dist/white-screen.js +273 -0
- package/package.json +69 -3
- package/README.md +0 -3
package/dist/replay.cjs
ADDED
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_objectSpread2 = require("./objectSpread2-BYC7ck9c.cjs");
|
|
3
|
+
// src/replay.ts
|
|
4
|
+
const controllers = /* @__PURE__ */ new WeakMap();
|
|
5
|
+
/** 读取某个客户端上的回放控制器;用户连线需要按需开始/停止录屏。 */
|
|
6
|
+
function getReplayController(client) {
|
|
7
|
+
return controllers.get(client);
|
|
8
|
+
}
|
|
9
|
+
function eventSize(event) {
|
|
10
|
+
try {
|
|
11
|
+
return JSON.stringify(event).length;
|
|
12
|
+
} catch (_unused) {
|
|
13
|
+
return 1024;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
function chunkEvents(events, maxBytes) {
|
|
17
|
+
const chunks = [];
|
|
18
|
+
let current = [];
|
|
19
|
+
let size = 2;
|
|
20
|
+
for (const event of events) {
|
|
21
|
+
const nextSize = eventSize(event);
|
|
22
|
+
if (current.length > 0 && size + nextSize > maxBytes) {
|
|
23
|
+
chunks.push(current);
|
|
24
|
+
current = [];
|
|
25
|
+
size = 2;
|
|
26
|
+
}
|
|
27
|
+
current.push(event);
|
|
28
|
+
size += nextSize + 1;
|
|
29
|
+
}
|
|
30
|
+
if (current.length > 0) chunks.push(current);
|
|
31
|
+
return chunks;
|
|
32
|
+
}
|
|
33
|
+
/** 找到包含全量快照的起点,保证回放能从完整 DOM 开始。 */
|
|
34
|
+
function findReplayStart(events, cutoff) {
|
|
35
|
+
let start = events.findIndex((event) => event.timestamp >= cutoff);
|
|
36
|
+
if (start < 0) start = 0;
|
|
37
|
+
let found = false;
|
|
38
|
+
for (let i = start; i >= 0; i -= 1) {
|
|
39
|
+
var _events$i;
|
|
40
|
+
if (((_events$i = events[i]) === null || _events$i === void 0 ? void 0 : _events$i.type) === 2) {
|
|
41
|
+
start = i;
|
|
42
|
+
found = true;
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return found ? start : 0;
|
|
47
|
+
}
|
|
48
|
+
async function createReplaySession(client, meta) {
|
|
49
|
+
const runtime = client.getRuntimeInfo();
|
|
50
|
+
const url = `${require_objectSpread2.trimEndpoint(runtime.endpoint)}/api/ingest/v1/replays?key=${encodeURIComponent(runtime.dsn)}`;
|
|
51
|
+
try {
|
|
52
|
+
const response = await fetch(url, {
|
|
53
|
+
method: "POST",
|
|
54
|
+
headers: { "content-type": "application/json" },
|
|
55
|
+
body: JSON.stringify(meta),
|
|
56
|
+
keepalive: true
|
|
57
|
+
});
|
|
58
|
+
if (!response.ok) return null;
|
|
59
|
+
const data = await response.json().catch(() => ({}));
|
|
60
|
+
return typeof data.id === "string" ? data.id : null;
|
|
61
|
+
} catch (_unused2) {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
async function uploadReplayChunk(client, replayId, seq, events) {
|
|
66
|
+
const runtime = client.getRuntimeInfo();
|
|
67
|
+
const url = `${require_objectSpread2.trimEndpoint(runtime.endpoint)}/api/ingest/v1/replays/${encodeURIComponent(replayId)}/chunks/${seq}?key=${encodeURIComponent(runtime.dsn)}`;
|
|
68
|
+
const raw = new TextEncoder().encode(JSON.stringify(events));
|
|
69
|
+
const compressed = await require_objectSpread2.gzipBytes(raw);
|
|
70
|
+
const body = compressed !== null && compressed !== void 0 ? compressed : raw;
|
|
71
|
+
try {
|
|
72
|
+
return (await fetch(url, {
|
|
73
|
+
method: "PUT",
|
|
74
|
+
headers: require_objectSpread2._objectSpread2({ "content-type": "application/x-firefly-replay" }, compressed ? { "content-encoding": "gzip" } : {}),
|
|
75
|
+
body,
|
|
76
|
+
keepalive: body.byteLength < 61440
|
|
77
|
+
})).ok;
|
|
78
|
+
} catch (_unused3) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async function completeReplaySession(client, replayId, meta) {
|
|
83
|
+
const runtime = client.getRuntimeInfo();
|
|
84
|
+
const url = `${require_objectSpread2.trimEndpoint(runtime.endpoint)}/api/ingest/v1/replays/${encodeURIComponent(replayId)}/complete?key=${encodeURIComponent(runtime.dsn)}`;
|
|
85
|
+
try {
|
|
86
|
+
await fetch(url, {
|
|
87
|
+
method: "POST",
|
|
88
|
+
headers: { "content-type": "application/json" },
|
|
89
|
+
body: JSON.stringify(meta),
|
|
90
|
+
keepalive: true
|
|
91
|
+
});
|
|
92
|
+
} catch (_unused4) {}
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* 安装会话回放。
|
|
96
|
+
*
|
|
97
|
+
* 默认 on-error:内存里持续保留最近一段 rrweb 事件,报错时上传;
|
|
98
|
+
* always:按采样率持续分片上传;manual:用户连线时再开始。
|
|
99
|
+
*/
|
|
100
|
+
function installReplay(client, options) {
|
|
101
|
+
if (!require_objectSpread2.isBrowser()) return () => {};
|
|
102
|
+
const controller = new ReplayRecorder(client, options);
|
|
103
|
+
controllers.set(client, controller);
|
|
104
|
+
controller.start();
|
|
105
|
+
const unsubscribe = client.onEvent((item) => {
|
|
106
|
+
if (item.type === "error" || item.type === "message" && item.level === "error") controller.handleError(item.event_id);
|
|
107
|
+
});
|
|
108
|
+
const onPageHide = () => {
|
|
109
|
+
controller.stop();
|
|
110
|
+
};
|
|
111
|
+
window.addEventListener("pagehide", onPageHide);
|
|
112
|
+
return () => {
|
|
113
|
+
window.removeEventListener("pagehide", onPageHide);
|
|
114
|
+
unsubscribe();
|
|
115
|
+
controllers.delete(client);
|
|
116
|
+
controller.stop();
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
var ReplayRecorder = class {
|
|
120
|
+
constructor(client, options) {
|
|
121
|
+
require_objectSpread2._defineProperty(this, "client", void 0);
|
|
122
|
+
require_objectSpread2._defineProperty(this, "options", void 0);
|
|
123
|
+
require_objectSpread2._defineProperty(this, "sampled", void 0);
|
|
124
|
+
require_objectSpread2._defineProperty(this, "manualRequested", false);
|
|
125
|
+
require_objectSpread2._defineProperty(this, "manualCapture", false);
|
|
126
|
+
require_objectSpread2._defineProperty(this, "recording", false);
|
|
127
|
+
require_objectSpread2._defineProperty(this, "stopRecord", null);
|
|
128
|
+
require_objectSpread2._defineProperty(this, "buffer", []);
|
|
129
|
+
require_objectSpread2._defineProperty(this, "uploadedCount", 0);
|
|
130
|
+
require_objectSpread2._defineProperty(this, "sessionId", null);
|
|
131
|
+
require_objectSpread2._defineProperty(this, "chunkSeq", 0);
|
|
132
|
+
require_objectSpread2._defineProperty(this, "startedAt", 0);
|
|
133
|
+
require_objectSpread2._defineProperty(this, "postRollUntil", 0);
|
|
134
|
+
require_objectSpread2._defineProperty(this, "errorEventId", "");
|
|
135
|
+
require_objectSpread2._defineProperty(this, "timer", null);
|
|
136
|
+
require_objectSpread2._defineProperty(this, "uploadQueue", Promise.resolve());
|
|
137
|
+
this.client = client;
|
|
138
|
+
this.options = options;
|
|
139
|
+
this.sampled = Math.random() < options.sampleRate;
|
|
140
|
+
}
|
|
141
|
+
isRecording() {
|
|
142
|
+
return this.recording;
|
|
143
|
+
}
|
|
144
|
+
start() {
|
|
145
|
+
if (this.recording) return;
|
|
146
|
+
if (!this.sampled) return;
|
|
147
|
+
if (this.options.mode === "manual" && !this.manualRequested) return;
|
|
148
|
+
this.recording = true;
|
|
149
|
+
this.startedAt = Date.now();
|
|
150
|
+
this.buffer = [];
|
|
151
|
+
this.uploadedCount = 0;
|
|
152
|
+
this.sessionId = null;
|
|
153
|
+
this.chunkSeq = 0;
|
|
154
|
+
this.postRollUntil = 0;
|
|
155
|
+
this.errorEventId = "";
|
|
156
|
+
this.startRecording();
|
|
157
|
+
this.startTimer();
|
|
158
|
+
if (this.options.mode === "always" || this.options.mode === "manual") this.enqueue(async () => {
|
|
159
|
+
await this.ensureSession();
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
/** 用户连线按需开始时调用;显式连接不受采样率限制。 */
|
|
163
|
+
startManual() {
|
|
164
|
+
this.manualRequested = true;
|
|
165
|
+
this.manualCapture = true;
|
|
166
|
+
this.sampled = true;
|
|
167
|
+
this.start();
|
|
168
|
+
}
|
|
169
|
+
async stop() {
|
|
170
|
+
var _this$stopRecord;
|
|
171
|
+
if (!this.recording && this.buffer.length === 0) return;
|
|
172
|
+
this.recording = false;
|
|
173
|
+
this.stopTimer();
|
|
174
|
+
(_this$stopRecord = this.stopRecord) === null || _this$stopRecord === void 0 || _this$stopRecord.call(this);
|
|
175
|
+
this.stopRecord = null;
|
|
176
|
+
if (this.options.mode === "on-error" && this.postRollUntil === 0 && !this.manualCapture) {
|
|
177
|
+
this.buffer = [];
|
|
178
|
+
this.uploadedCount = 0;
|
|
179
|
+
this.sessionId = null;
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
await this.enqueue(() => this.flushBuffered(true));
|
|
183
|
+
this.manualCapture = false;
|
|
184
|
+
}
|
|
185
|
+
handleError(eventId) {
|
|
186
|
+
if (this.options.mode !== "on-error") return;
|
|
187
|
+
if (!this.recording) return;
|
|
188
|
+
if (this.postRollUntil > 0) return;
|
|
189
|
+
this.errorEventId = eventId;
|
|
190
|
+
this.postRollUntil = Date.now() + this.options.postRollMs;
|
|
191
|
+
this.enqueue(() => this.flushBuffered(false));
|
|
192
|
+
}
|
|
193
|
+
async startRecording() {
|
|
194
|
+
try {
|
|
195
|
+
const rrweb = await import("rrweb");
|
|
196
|
+
this.stopRecord = rrweb.record({
|
|
197
|
+
emit: (event) => this.handleRrwebEvent(event),
|
|
198
|
+
maskAllInputs: this.options.maskAllInputs,
|
|
199
|
+
maskTextSelector: this.options.maskTextSelector,
|
|
200
|
+
blockSelector: this.options.blockSelector,
|
|
201
|
+
recordCanvas: this.options.recordCanvas,
|
|
202
|
+
sampling: {
|
|
203
|
+
mousemove: 50,
|
|
204
|
+
scroll: 100,
|
|
205
|
+
media: 800,
|
|
206
|
+
input: "last"
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
} catch (_unused5) {
|
|
210
|
+
this.recording = false;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
handleRrwebEvent(event) {
|
|
214
|
+
if (!this.recording) return;
|
|
215
|
+
this.buffer.push(event);
|
|
216
|
+
this.trimBuffer();
|
|
217
|
+
if (this.options.mode === "always") {
|
|
218
|
+
if (this.buffer.length - this.uploadedCount >= 200) this.enqueue(() => this.flushBuffered(false));
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
trimBuffer() {
|
|
222
|
+
if (this.postRollUntil > 0) return;
|
|
223
|
+
const cutoff = Date.now() - this.options.maxDurationMs;
|
|
224
|
+
if (this.buffer.length === 0) return;
|
|
225
|
+
const first = this.buffer[0];
|
|
226
|
+
if (first && first.timestamp >= cutoff) return;
|
|
227
|
+
const start = findReplayStart(this.buffer, cutoff);
|
|
228
|
+
if (start <= 0) return;
|
|
229
|
+
this.buffer = this.buffer.slice(start);
|
|
230
|
+
this.uploadedCount = Math.max(0, this.uploadedCount - start);
|
|
231
|
+
}
|
|
232
|
+
startTimer() {
|
|
233
|
+
if (this.timer !== null) return;
|
|
234
|
+
this.timer = window.setInterval(() => {
|
|
235
|
+
if (!this.recording) return;
|
|
236
|
+
if (this.postRollUntil > 0 && Date.now() >= this.postRollUntil) {
|
|
237
|
+
this.postRollUntil = 0;
|
|
238
|
+
this.enqueue(() => this.finishSession());
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
if (this.options.mode === "always") {
|
|
242
|
+
if (this.buffer.length - this.uploadedCount > 0) this.enqueue(() => this.flushBuffered(false));
|
|
243
|
+
}
|
|
244
|
+
}, 5e3);
|
|
245
|
+
}
|
|
246
|
+
stopTimer() {
|
|
247
|
+
if (this.timer === null) return;
|
|
248
|
+
window.clearInterval(this.timer);
|
|
249
|
+
this.timer = null;
|
|
250
|
+
}
|
|
251
|
+
enqueue(task) {
|
|
252
|
+
this.uploadQueue = this.uploadQueue.then(task, task);
|
|
253
|
+
return this.uploadQueue;
|
|
254
|
+
}
|
|
255
|
+
async ensureSession() {
|
|
256
|
+
if (this.sessionId) return this.sessionId;
|
|
257
|
+
const runtime = this.client.getRuntimeInfo();
|
|
258
|
+
const meta = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({
|
|
259
|
+
pageSessionId: runtime.pageSession.id,
|
|
260
|
+
startedAt: new Date(this.startedAt || Date.now()).toISOString()
|
|
261
|
+
}, runtime.pageSession.path ? { route: runtime.pageSession.path } : {}), typeof window !== "undefined" ? { url: window.location.href } : {}), this.errorEventId ? { errorEventId: this.errorEventId } : {});
|
|
262
|
+
this.sessionId = await createReplaySession(this.client, meta);
|
|
263
|
+
return this.sessionId;
|
|
264
|
+
}
|
|
265
|
+
async flushBuffered(final) {
|
|
266
|
+
if (this.buffer.length === 0 && !final) return;
|
|
267
|
+
const sessionId = await this.ensureSession();
|
|
268
|
+
if (!sessionId) return;
|
|
269
|
+
const pending = this.buffer.slice(this.uploadedCount);
|
|
270
|
+
if (pending.length > 0) {
|
|
271
|
+
const chunks = chunkEvents(pending, this.options.maxChunkBytes);
|
|
272
|
+
for (const chunk of chunks) {
|
|
273
|
+
const nextSeq = this.chunkSeq + 1;
|
|
274
|
+
if (!await uploadReplayChunk(this.client, sessionId, nextSeq, chunk)) return;
|
|
275
|
+
this.chunkSeq = nextSeq;
|
|
276
|
+
this.uploadedCount += chunk.length;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
if (!final) return;
|
|
280
|
+
const runtime = this.client.getRuntimeInfo();
|
|
281
|
+
await completeReplaySession(this.client, sessionId, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({
|
|
282
|
+
pageSessionId: runtime.pageSession.id,
|
|
283
|
+
startedAt: new Date(this.startedAt || Date.now()).toISOString(),
|
|
284
|
+
endedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
285
|
+
}, runtime.pageSession.path ? { route: runtime.pageSession.path } : {}), typeof window !== "undefined" ? { url: window.location.href } : {}), this.errorEventId ? { errorEventId: this.errorEventId } : {}), {}, { eventCount: this.uploadedCount }));
|
|
286
|
+
this.sessionId = null;
|
|
287
|
+
this.chunkSeq = 0;
|
|
288
|
+
this.uploadedCount = 0;
|
|
289
|
+
this.buffer = [];
|
|
290
|
+
this.errorEventId = "";
|
|
291
|
+
this.startedAt = Date.now();
|
|
292
|
+
this.postRollUntil = 0;
|
|
293
|
+
}
|
|
294
|
+
async finishSession() {
|
|
295
|
+
await this.flushBuffered(true);
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
exports.getReplayController = getReplayController;
|
|
299
|
+
exports.installReplay = installReplay;
|
|
300
|
+
|
|
301
|
+
//# sourceMappingURL=replay.cjs.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { a as ResolvedReplayOptions } from "./options-C2iLgUnA.cjs";
|
|
2
|
+
import { FireflyClient } from "@fz-firefly/core";
|
|
3
|
+
export interface ReplayController {
|
|
4
|
+
start(): void;
|
|
5
|
+
/** 用户连线时按需开始,忽略采样率。 */
|
|
6
|
+
startManual(): void;
|
|
7
|
+
stop(): void | Promise<void>;
|
|
8
|
+
isRecording(): boolean;
|
|
9
|
+
}
|
|
10
|
+
/** 读取某个客户端上的回放控制器;用户连线需要按需开始/停止录屏。 */
|
|
11
|
+
export declare function getReplayController(client: FireflyClient): ReplayController | undefined;
|
|
12
|
+
/**
|
|
13
|
+
* 安装会话回放。
|
|
14
|
+
*
|
|
15
|
+
* 默认 on-error:内存里持续保留最近一段 rrweb 事件,报错时上传;
|
|
16
|
+
* always:按采样率持续分片上传;manual:用户连线时再开始。
|
|
17
|
+
*/
|
|
18
|
+
export declare function installReplay(client: FireflyClient, options: ResolvedReplayOptions): () => void;
|
|
19
|
+
//# sourceMappingURL=replay.d.cts.map
|
package/dist/replay.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { a as ResolvedReplayOptions } from "./options-C2iLgUnA.js";
|
|
2
|
+
import { FireflyClient } from "@fz-firefly/core";
|
|
3
|
+
export interface ReplayController {
|
|
4
|
+
start(): void;
|
|
5
|
+
/** 用户连线时按需开始,忽略采样率。 */
|
|
6
|
+
startManual(): void;
|
|
7
|
+
stop(): void | Promise<void>;
|
|
8
|
+
isRecording(): boolean;
|
|
9
|
+
}
|
|
10
|
+
/** 读取某个客户端上的回放控制器;用户连线需要按需开始/停止录屏。 */
|
|
11
|
+
export declare function getReplayController(client: FireflyClient): ReplayController | undefined;
|
|
12
|
+
/**
|
|
13
|
+
* 安装会话回放。
|
|
14
|
+
*
|
|
15
|
+
* 默认 on-error:内存里持续保留最近一段 rrweb 事件,报错时上传;
|
|
16
|
+
* always:按采样率持续分片上传;manual:用户连线时再开始。
|
|
17
|
+
*/
|
|
18
|
+
export declare function installReplay(client: FireflyClient, options: ResolvedReplayOptions): () => void;
|
|
19
|
+
//# sourceMappingURL=replay.d.ts.map
|
package/dist/replay.js
ADDED
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import { a as trimEndpoint, i as isBrowser, n as _defineProperty, r as gzipBytes, t as _objectSpread2 } from "./objectSpread2-lWxa9t4g.js";
|
|
2
|
+
// src/replay.ts
|
|
3
|
+
const controllers = /* @__PURE__ */ new WeakMap();
|
|
4
|
+
/** 读取某个客户端上的回放控制器;用户连线需要按需开始/停止录屏。 */
|
|
5
|
+
function getReplayController(client) {
|
|
6
|
+
return controllers.get(client);
|
|
7
|
+
}
|
|
8
|
+
function eventSize(event) {
|
|
9
|
+
try {
|
|
10
|
+
return JSON.stringify(event).length;
|
|
11
|
+
} catch (_unused) {
|
|
12
|
+
return 1024;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function chunkEvents(events, maxBytes) {
|
|
16
|
+
const chunks = [];
|
|
17
|
+
let current = [];
|
|
18
|
+
let size = 2;
|
|
19
|
+
for (const event of events) {
|
|
20
|
+
const nextSize = eventSize(event);
|
|
21
|
+
if (current.length > 0 && size + nextSize > maxBytes) {
|
|
22
|
+
chunks.push(current);
|
|
23
|
+
current = [];
|
|
24
|
+
size = 2;
|
|
25
|
+
}
|
|
26
|
+
current.push(event);
|
|
27
|
+
size += nextSize + 1;
|
|
28
|
+
}
|
|
29
|
+
if (current.length > 0) chunks.push(current);
|
|
30
|
+
return chunks;
|
|
31
|
+
}
|
|
32
|
+
/** 找到包含全量快照的起点,保证回放能从完整 DOM 开始。 */
|
|
33
|
+
function findReplayStart(events, cutoff) {
|
|
34
|
+
let start = events.findIndex((event) => event.timestamp >= cutoff);
|
|
35
|
+
if (start < 0) start = 0;
|
|
36
|
+
let found = false;
|
|
37
|
+
for (let i = start; i >= 0; i -= 1) {
|
|
38
|
+
var _events$i;
|
|
39
|
+
if (((_events$i = events[i]) === null || _events$i === void 0 ? void 0 : _events$i.type) === 2) {
|
|
40
|
+
start = i;
|
|
41
|
+
found = true;
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return found ? start : 0;
|
|
46
|
+
}
|
|
47
|
+
async function createReplaySession(client, meta) {
|
|
48
|
+
const runtime = client.getRuntimeInfo();
|
|
49
|
+
const url = `${trimEndpoint(runtime.endpoint)}/api/ingest/v1/replays?key=${encodeURIComponent(runtime.dsn)}`;
|
|
50
|
+
try {
|
|
51
|
+
const response = await fetch(url, {
|
|
52
|
+
method: "POST",
|
|
53
|
+
headers: { "content-type": "application/json" },
|
|
54
|
+
body: JSON.stringify(meta),
|
|
55
|
+
keepalive: true
|
|
56
|
+
});
|
|
57
|
+
if (!response.ok) return null;
|
|
58
|
+
const data = await response.json().catch(() => ({}));
|
|
59
|
+
return typeof data.id === "string" ? data.id : null;
|
|
60
|
+
} catch (_unused2) {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
async function uploadReplayChunk(client, replayId, seq, events) {
|
|
65
|
+
const runtime = client.getRuntimeInfo();
|
|
66
|
+
const url = `${trimEndpoint(runtime.endpoint)}/api/ingest/v1/replays/${encodeURIComponent(replayId)}/chunks/${seq}?key=${encodeURIComponent(runtime.dsn)}`;
|
|
67
|
+
const raw = new TextEncoder().encode(JSON.stringify(events));
|
|
68
|
+
const compressed = await gzipBytes(raw);
|
|
69
|
+
const body = compressed !== null && compressed !== void 0 ? compressed : raw;
|
|
70
|
+
try {
|
|
71
|
+
return (await fetch(url, {
|
|
72
|
+
method: "PUT",
|
|
73
|
+
headers: _objectSpread2({ "content-type": "application/x-firefly-replay" }, compressed ? { "content-encoding": "gzip" } : {}),
|
|
74
|
+
body,
|
|
75
|
+
keepalive: body.byteLength < 61440
|
|
76
|
+
})).ok;
|
|
77
|
+
} catch (_unused3) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
async function completeReplaySession(client, replayId, meta) {
|
|
82
|
+
const runtime = client.getRuntimeInfo();
|
|
83
|
+
const url = `${trimEndpoint(runtime.endpoint)}/api/ingest/v1/replays/${encodeURIComponent(replayId)}/complete?key=${encodeURIComponent(runtime.dsn)}`;
|
|
84
|
+
try {
|
|
85
|
+
await fetch(url, {
|
|
86
|
+
method: "POST",
|
|
87
|
+
headers: { "content-type": "application/json" },
|
|
88
|
+
body: JSON.stringify(meta),
|
|
89
|
+
keepalive: true
|
|
90
|
+
});
|
|
91
|
+
} catch (_unused4) {}
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* 安装会话回放。
|
|
95
|
+
*
|
|
96
|
+
* 默认 on-error:内存里持续保留最近一段 rrweb 事件,报错时上传;
|
|
97
|
+
* always:按采样率持续分片上传;manual:用户连线时再开始。
|
|
98
|
+
*/
|
|
99
|
+
function installReplay(client, options) {
|
|
100
|
+
if (!isBrowser()) return () => {};
|
|
101
|
+
const controller = new ReplayRecorder(client, options);
|
|
102
|
+
controllers.set(client, controller);
|
|
103
|
+
controller.start();
|
|
104
|
+
const unsubscribe = client.onEvent((item) => {
|
|
105
|
+
if (item.type === "error" || item.type === "message" && item.level === "error") controller.handleError(item.event_id);
|
|
106
|
+
});
|
|
107
|
+
const onPageHide = () => {
|
|
108
|
+
controller.stop();
|
|
109
|
+
};
|
|
110
|
+
window.addEventListener("pagehide", onPageHide);
|
|
111
|
+
return () => {
|
|
112
|
+
window.removeEventListener("pagehide", onPageHide);
|
|
113
|
+
unsubscribe();
|
|
114
|
+
controllers.delete(client);
|
|
115
|
+
controller.stop();
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
var ReplayRecorder = class {
|
|
119
|
+
constructor(client, options) {
|
|
120
|
+
_defineProperty(this, "client", void 0);
|
|
121
|
+
_defineProperty(this, "options", void 0);
|
|
122
|
+
_defineProperty(this, "sampled", void 0);
|
|
123
|
+
_defineProperty(this, "manualRequested", false);
|
|
124
|
+
_defineProperty(this, "manualCapture", false);
|
|
125
|
+
_defineProperty(this, "recording", false);
|
|
126
|
+
_defineProperty(this, "stopRecord", null);
|
|
127
|
+
_defineProperty(this, "buffer", []);
|
|
128
|
+
_defineProperty(this, "uploadedCount", 0);
|
|
129
|
+
_defineProperty(this, "sessionId", null);
|
|
130
|
+
_defineProperty(this, "chunkSeq", 0);
|
|
131
|
+
_defineProperty(this, "startedAt", 0);
|
|
132
|
+
_defineProperty(this, "postRollUntil", 0);
|
|
133
|
+
_defineProperty(this, "errorEventId", "");
|
|
134
|
+
_defineProperty(this, "timer", null);
|
|
135
|
+
_defineProperty(this, "uploadQueue", Promise.resolve());
|
|
136
|
+
this.client = client;
|
|
137
|
+
this.options = options;
|
|
138
|
+
this.sampled = Math.random() < options.sampleRate;
|
|
139
|
+
}
|
|
140
|
+
isRecording() {
|
|
141
|
+
return this.recording;
|
|
142
|
+
}
|
|
143
|
+
start() {
|
|
144
|
+
if (this.recording) return;
|
|
145
|
+
if (!this.sampled) return;
|
|
146
|
+
if (this.options.mode === "manual" && !this.manualRequested) return;
|
|
147
|
+
this.recording = true;
|
|
148
|
+
this.startedAt = Date.now();
|
|
149
|
+
this.buffer = [];
|
|
150
|
+
this.uploadedCount = 0;
|
|
151
|
+
this.sessionId = null;
|
|
152
|
+
this.chunkSeq = 0;
|
|
153
|
+
this.postRollUntil = 0;
|
|
154
|
+
this.errorEventId = "";
|
|
155
|
+
this.startRecording();
|
|
156
|
+
this.startTimer();
|
|
157
|
+
if (this.options.mode === "always" || this.options.mode === "manual") this.enqueue(async () => {
|
|
158
|
+
await this.ensureSession();
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
/** 用户连线按需开始时调用;显式连接不受采样率限制。 */
|
|
162
|
+
startManual() {
|
|
163
|
+
this.manualRequested = true;
|
|
164
|
+
this.manualCapture = true;
|
|
165
|
+
this.sampled = true;
|
|
166
|
+
this.start();
|
|
167
|
+
}
|
|
168
|
+
async stop() {
|
|
169
|
+
var _this$stopRecord;
|
|
170
|
+
if (!this.recording && this.buffer.length === 0) return;
|
|
171
|
+
this.recording = false;
|
|
172
|
+
this.stopTimer();
|
|
173
|
+
(_this$stopRecord = this.stopRecord) === null || _this$stopRecord === void 0 || _this$stopRecord.call(this);
|
|
174
|
+
this.stopRecord = null;
|
|
175
|
+
if (this.options.mode === "on-error" && this.postRollUntil === 0 && !this.manualCapture) {
|
|
176
|
+
this.buffer = [];
|
|
177
|
+
this.uploadedCount = 0;
|
|
178
|
+
this.sessionId = null;
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
await this.enqueue(() => this.flushBuffered(true));
|
|
182
|
+
this.manualCapture = false;
|
|
183
|
+
}
|
|
184
|
+
handleError(eventId) {
|
|
185
|
+
if (this.options.mode !== "on-error") return;
|
|
186
|
+
if (!this.recording) return;
|
|
187
|
+
if (this.postRollUntil > 0) return;
|
|
188
|
+
this.errorEventId = eventId;
|
|
189
|
+
this.postRollUntil = Date.now() + this.options.postRollMs;
|
|
190
|
+
this.enqueue(() => this.flushBuffered(false));
|
|
191
|
+
}
|
|
192
|
+
async startRecording() {
|
|
193
|
+
try {
|
|
194
|
+
const rrweb = await import("rrweb");
|
|
195
|
+
this.stopRecord = rrweb.record({
|
|
196
|
+
emit: (event) => this.handleRrwebEvent(event),
|
|
197
|
+
maskAllInputs: this.options.maskAllInputs,
|
|
198
|
+
maskTextSelector: this.options.maskTextSelector,
|
|
199
|
+
blockSelector: this.options.blockSelector,
|
|
200
|
+
recordCanvas: this.options.recordCanvas,
|
|
201
|
+
sampling: {
|
|
202
|
+
mousemove: 50,
|
|
203
|
+
scroll: 100,
|
|
204
|
+
media: 800,
|
|
205
|
+
input: "last"
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
} catch (_unused5) {
|
|
209
|
+
this.recording = false;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
handleRrwebEvent(event) {
|
|
213
|
+
if (!this.recording) return;
|
|
214
|
+
this.buffer.push(event);
|
|
215
|
+
this.trimBuffer();
|
|
216
|
+
if (this.options.mode === "always") {
|
|
217
|
+
if (this.buffer.length - this.uploadedCount >= 200) this.enqueue(() => this.flushBuffered(false));
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
trimBuffer() {
|
|
221
|
+
if (this.postRollUntil > 0) return;
|
|
222
|
+
const cutoff = Date.now() - this.options.maxDurationMs;
|
|
223
|
+
if (this.buffer.length === 0) return;
|
|
224
|
+
const first = this.buffer[0];
|
|
225
|
+
if (first && first.timestamp >= cutoff) return;
|
|
226
|
+
const start = findReplayStart(this.buffer, cutoff);
|
|
227
|
+
if (start <= 0) return;
|
|
228
|
+
this.buffer = this.buffer.slice(start);
|
|
229
|
+
this.uploadedCount = Math.max(0, this.uploadedCount - start);
|
|
230
|
+
}
|
|
231
|
+
startTimer() {
|
|
232
|
+
if (this.timer !== null) return;
|
|
233
|
+
this.timer = window.setInterval(() => {
|
|
234
|
+
if (!this.recording) return;
|
|
235
|
+
if (this.postRollUntil > 0 && Date.now() >= this.postRollUntil) {
|
|
236
|
+
this.postRollUntil = 0;
|
|
237
|
+
this.enqueue(() => this.finishSession());
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
if (this.options.mode === "always") {
|
|
241
|
+
if (this.buffer.length - this.uploadedCount > 0) this.enqueue(() => this.flushBuffered(false));
|
|
242
|
+
}
|
|
243
|
+
}, 5e3);
|
|
244
|
+
}
|
|
245
|
+
stopTimer() {
|
|
246
|
+
if (this.timer === null) return;
|
|
247
|
+
window.clearInterval(this.timer);
|
|
248
|
+
this.timer = null;
|
|
249
|
+
}
|
|
250
|
+
enqueue(task) {
|
|
251
|
+
this.uploadQueue = this.uploadQueue.then(task, task);
|
|
252
|
+
return this.uploadQueue;
|
|
253
|
+
}
|
|
254
|
+
async ensureSession() {
|
|
255
|
+
if (this.sessionId) return this.sessionId;
|
|
256
|
+
const runtime = this.client.getRuntimeInfo();
|
|
257
|
+
const meta = _objectSpread2(_objectSpread2(_objectSpread2({
|
|
258
|
+
pageSessionId: runtime.pageSession.id,
|
|
259
|
+
startedAt: new Date(this.startedAt || Date.now()).toISOString()
|
|
260
|
+
}, runtime.pageSession.path ? { route: runtime.pageSession.path } : {}), typeof window !== "undefined" ? { url: window.location.href } : {}), this.errorEventId ? { errorEventId: this.errorEventId } : {});
|
|
261
|
+
this.sessionId = await createReplaySession(this.client, meta);
|
|
262
|
+
return this.sessionId;
|
|
263
|
+
}
|
|
264
|
+
async flushBuffered(final) {
|
|
265
|
+
if (this.buffer.length === 0 && !final) return;
|
|
266
|
+
const sessionId = await this.ensureSession();
|
|
267
|
+
if (!sessionId) return;
|
|
268
|
+
const pending = this.buffer.slice(this.uploadedCount);
|
|
269
|
+
if (pending.length > 0) {
|
|
270
|
+
const chunks = chunkEvents(pending, this.options.maxChunkBytes);
|
|
271
|
+
for (const chunk of chunks) {
|
|
272
|
+
const nextSeq = this.chunkSeq + 1;
|
|
273
|
+
if (!await uploadReplayChunk(this.client, sessionId, nextSeq, chunk)) return;
|
|
274
|
+
this.chunkSeq = nextSeq;
|
|
275
|
+
this.uploadedCount += chunk.length;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
if (!final) return;
|
|
279
|
+
const runtime = this.client.getRuntimeInfo();
|
|
280
|
+
await completeReplaySession(this.client, sessionId, _objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2({
|
|
281
|
+
pageSessionId: runtime.pageSession.id,
|
|
282
|
+
startedAt: new Date(this.startedAt || Date.now()).toISOString(),
|
|
283
|
+
endedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
284
|
+
}, runtime.pageSession.path ? { route: runtime.pageSession.path } : {}), typeof window !== "undefined" ? { url: window.location.href } : {}), this.errorEventId ? { errorEventId: this.errorEventId } : {}), {}, { eventCount: this.uploadedCount }));
|
|
285
|
+
this.sessionId = null;
|
|
286
|
+
this.chunkSeq = 0;
|
|
287
|
+
this.uploadedCount = 0;
|
|
288
|
+
this.buffer = [];
|
|
289
|
+
this.errorEventId = "";
|
|
290
|
+
this.startedAt = Date.now();
|
|
291
|
+
this.postRollUntil = 0;
|
|
292
|
+
}
|
|
293
|
+
async finishSession() {
|
|
294
|
+
await this.flushBuffered(true);
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
export { getReplayController, installReplay };
|
|
298
|
+
|
|
299
|
+
//# sourceMappingURL=replay.js.map
|