@extn/segi-js 0.2.14 → 0.2.15

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.d.mts CHANGED
@@ -16,7 +16,14 @@ interface InitOptions {
16
16
  type SegiInitOptions = SegiClientOptions & {
17
17
  replay?: ReplayConfig;
18
18
  };
19
+ type ErrorSignal = {
20
+ ts: string;
21
+ url?: string;
22
+ };
19
23
  type ReplayApi = {
24
+ _captureError(signal: ErrorSignal): void;
25
+ _sessionUid(): string | null;
26
+ _stop(): void;
20
27
  pause(): void;
21
28
  resume(): void;
22
29
  setUser(user: {
package/dist/index.d.ts CHANGED
@@ -16,7 +16,14 @@ interface InitOptions {
16
16
  type SegiInitOptions = SegiClientOptions & {
17
17
  replay?: ReplayConfig;
18
18
  };
19
+ type ErrorSignal = {
20
+ ts: string;
21
+ url?: string;
22
+ };
19
23
  type ReplayApi = {
24
+ _captureError(signal: ErrorSignal): void;
25
+ _sessionUid(): string | null;
26
+ _stop(): void;
20
27
  pause(): void;
21
28
  resume(): void;
22
29
  setUser(user: {
package/dist/index.js CHANGED
@@ -178,7 +178,18 @@ function detachListeners() {
178
178
  window.removeEventListener("error", captureWindowError, true);
179
179
  window.removeEventListener("unhandledrejection", captureWindowError, true);
180
180
  }
181
+ var pendingReplayErrors = [];
182
+ var replayEnabled = false;
183
+ var replayEpoch = 0;
181
184
  var noopReplay = {
185
+ _captureError(signal) {
186
+ if (pendingReplayErrors.length < 20) pendingReplayErrors.push(signal);
187
+ },
188
+ _sessionUid() {
189
+ return null;
190
+ },
191
+ _stop() {
192
+ },
182
193
  pause() {
183
194
  },
184
195
  resume() {
@@ -215,14 +226,22 @@ var Segi = {
215
226
  return;
216
227
  }
217
228
  if (state.initialized) detachListeners();
229
+ Segi.replay._stop();
230
+ Segi.replay = noopReplay;
231
+ pendingReplayErrors = [];
232
+ const epoch = ++replayEpoch;
218
233
  const { replay, ...core } = options;
234
+ replayEnabled = !!_optionalChain([replay, 'optionalAccess', _3 => _3.__segi_replay]) && replay.options.enabled !== false;
219
235
  state.options = { ...core };
220
236
  state.initialized = true;
221
237
  if (w) w.__segiInitialized = true;
222
238
  attachListeners();
223
- if (_optionalChain([replay, 'optionalAccess', _3 => _3.__segi_replay])) {
239
+ if (_optionalChain([replay, 'optionalAccess', _4 => _4.__segi_replay])) {
224
240
  void Promise.resolve().then(() => _interopRequireWildcard(require("./replay/index.js"))).then((mod) => {
241
+ if (epoch !== replayEpoch || !state.initialized) return;
225
242
  Segi.replay = mod.replayApi;
243
+ for (const error of pendingReplayErrors) mod.replayApi._captureError(error);
244
+ pendingReplayErrors = [];
226
245
  if (state.user) {
227
246
  mod.replayApi.setUser({
228
247
  id: state.user.id,
@@ -250,6 +269,11 @@ var Segi = {
250
269
  close() {
251
270
  try {
252
271
  detachListeners();
272
+ replayEpoch++;
273
+ replayEnabled = false;
274
+ Segi.replay._stop();
275
+ Segi.replay = noopReplay;
276
+ pendingReplayErrors = [];
253
277
  state.initialized = false;
254
278
  state.user = null;
255
279
  state.tags = {};
@@ -275,8 +299,15 @@ var Segi = {
275
299
  runtime: "browser",
276
300
  url: _nullishCoalesce(context.url, () => ( (typeof location !== "undefined" ? location.href : void 0))),
277
301
  userAgent: _nullishCoalesce(context.userAgent, () => ( (typeof navigator !== "undefined" ? navigator.userAgent : void 0))),
278
- ...context
302
+ ...context,
303
+ extra: {
304
+ ...context.extra,
305
+ ...Segi.replay._sessionUid() ? { sessionUid: Segi.replay._sessionUid() } : {}
306
+ }
279
307
  });
308
+ if (replayEnabled && !evt._drop) {
309
+ Segi.replay._captureError({ ts: evt.timestamp || (/* @__PURE__ */ new Date()).toISOString(), url: evt.url });
310
+ }
280
311
  send(state.options.endpoint, state.options.projectKey, evt);
281
312
  } catch (e7) {
282
313
  }
@@ -292,8 +323,15 @@ var Segi = {
292
323
  runtime: "browser",
293
324
  url: typeof location !== "undefined" ? location.href : void 0,
294
325
  userAgent: typeof navigator !== "undefined" ? navigator.userAgent : void 0,
295
- ...context
326
+ ...context,
327
+ extra: {
328
+ ...context.extra,
329
+ ...Segi.replay._sessionUid() ? { sessionUid: Segi.replay._sessionUid() } : {}
330
+ }
296
331
  });
332
+ if (replayEnabled && (evt.level === "error" || evt.level === "fatal") && !evt._drop) {
333
+ Segi.replay._captureError({ ts: evt.timestamp || (/* @__PURE__ */ new Date()).toISOString(), url: evt.url });
334
+ }
297
335
  send(state.options.endpoint, state.options.projectKey, evt);
298
336
  } catch (e8) {
299
337
  }
package/dist/index.mjs CHANGED
@@ -178,7 +178,18 @@ function detachListeners() {
178
178
  window.removeEventListener("error", captureWindowError, true);
179
179
  window.removeEventListener("unhandledrejection", captureWindowError, true);
180
180
  }
181
+ var pendingReplayErrors = [];
182
+ var replayEnabled = false;
183
+ var replayEpoch = 0;
181
184
  var noopReplay = {
185
+ _captureError(signal) {
186
+ if (pendingReplayErrors.length < 20) pendingReplayErrors.push(signal);
187
+ },
188
+ _sessionUid() {
189
+ return null;
190
+ },
191
+ _stop() {
192
+ },
182
193
  pause() {
183
194
  },
184
195
  resume() {
@@ -215,14 +226,22 @@ var Segi = {
215
226
  return;
216
227
  }
217
228
  if (state.initialized) detachListeners();
229
+ Segi.replay._stop();
230
+ Segi.replay = noopReplay;
231
+ pendingReplayErrors = [];
232
+ const epoch = ++replayEpoch;
218
233
  const { replay, ...core } = options;
234
+ replayEnabled = !!replay?.__segi_replay && replay.options.enabled !== false;
219
235
  state.options = { ...core };
220
236
  state.initialized = true;
221
237
  if (w) w.__segiInitialized = true;
222
238
  attachListeners();
223
239
  if (replay?.__segi_replay) {
224
240
  void import("./replay/index.mjs").then((mod) => {
241
+ if (epoch !== replayEpoch || !state.initialized) return;
225
242
  Segi.replay = mod.replayApi;
243
+ for (const error of pendingReplayErrors) mod.replayApi._captureError(error);
244
+ pendingReplayErrors = [];
226
245
  if (state.user) {
227
246
  mod.replayApi.setUser({
228
247
  id: state.user.id,
@@ -250,6 +269,11 @@ var Segi = {
250
269
  close() {
251
270
  try {
252
271
  detachListeners();
272
+ replayEpoch++;
273
+ replayEnabled = false;
274
+ Segi.replay._stop();
275
+ Segi.replay = noopReplay;
276
+ pendingReplayErrors = [];
253
277
  state.initialized = false;
254
278
  state.user = null;
255
279
  state.tags = {};
@@ -275,8 +299,15 @@ var Segi = {
275
299
  runtime: "browser",
276
300
  url: context.url ?? (typeof location !== "undefined" ? location.href : void 0),
277
301
  userAgent: context.userAgent ?? (typeof navigator !== "undefined" ? navigator.userAgent : void 0),
278
- ...context
302
+ ...context,
303
+ extra: {
304
+ ...context.extra,
305
+ ...Segi.replay._sessionUid() ? { sessionUid: Segi.replay._sessionUid() } : {}
306
+ }
279
307
  });
308
+ if (replayEnabled && !evt._drop) {
309
+ Segi.replay._captureError({ ts: evt.timestamp || (/* @__PURE__ */ new Date()).toISOString(), url: evt.url });
310
+ }
280
311
  send(state.options.endpoint, state.options.projectKey, evt);
281
312
  } catch {
282
313
  }
@@ -292,8 +323,15 @@ var Segi = {
292
323
  runtime: "browser",
293
324
  url: typeof location !== "undefined" ? location.href : void 0,
294
325
  userAgent: typeof navigator !== "undefined" ? navigator.userAgent : void 0,
295
- ...context
326
+ ...context,
327
+ extra: {
328
+ ...context.extra,
329
+ ...Segi.replay._sessionUid() ? { sessionUid: Segi.replay._sessionUid() } : {}
330
+ }
296
331
  });
332
+ if (replayEnabled && (evt.level === "error" || evt.level === "fatal") && !evt._drop) {
333
+ Segi.replay._captureError({ ts: evt.timestamp || (/* @__PURE__ */ new Date()).toISOString(), url: evt.url });
334
+ }
297
335
  send(state.options.endpoint, state.options.projectKey, evt);
298
336
  } catch {
299
337
  }
@@ -30,6 +30,10 @@ import { R as ReplayConfig, a as ReplayOptions } from '../types-CsgcNSc1.mjs';
30
30
  */
31
31
 
32
32
  declare function withReplay(options?: ReplayOptions): ReplayConfig;
33
+ type ErrorSignal = {
34
+ ts: string;
35
+ url?: string;
36
+ };
33
37
  /**
34
38
  * Internal entry-point invoked by Segi.init() when a ReplayConfig was
35
39
  * passed. Not part of the public surface.
@@ -42,6 +46,9 @@ declare function activateReplay(args: {
42
46
  }): Promise<void>;
43
47
  /** Public Segi.replay namespace, attached by the main index.ts. */
44
48
  declare const replayApi: {
49
+ /** Internal bridge from the error SDK; no stack/message is copied to trace. */
50
+ _captureError(signal: ErrorSignal): void;
51
+ _stop(): void;
45
52
  pause(): void;
46
53
  resume(): void;
47
54
  setUser(user: {
@@ -30,6 +30,10 @@ import { R as ReplayConfig, a as ReplayOptions } from '../types-CsgcNSc1.js';
30
30
  */
31
31
 
32
32
  declare function withReplay(options?: ReplayOptions): ReplayConfig;
33
+ type ErrorSignal = {
34
+ ts: string;
35
+ url?: string;
36
+ };
33
37
  /**
34
38
  * Internal entry-point invoked by Segi.init() when a ReplayConfig was
35
39
  * passed. Not part of the public surface.
@@ -42,6 +46,9 @@ declare function activateReplay(args: {
42
46
  }): Promise<void>;
43
47
  /** Public Segi.replay namespace, attached by the main index.ts. */
44
48
  declare const replayApi: {
49
+ /** Internal bridge from the error SDK; no stack/message is copied to trace. */
50
+ _captureError(signal: ErrorSignal): void;
51
+ _stop(): void;
45
52
  pause(): void;
46
53
  resume(): void;
47
54
  setUser(user: {
@@ -2,9 +2,24 @@
2
2
  var UID_KEY = "_bd_session_uid";
3
3
  var UID_TS_KEY = "_bd_session_at";
4
4
  var ANON_KEY = "_bd_anon";
5
+ var PROGRESS_KEY = "_segi_replay_progress";
5
6
  var IDLE_MS = 30 * 6e4;
6
7
  var memUid = null;
7
8
  var memAnon = null;
9
+ function readReplayProgress(sessionUid) {
10
+ try {
11
+ const p = JSON.parse(sessionStorage.getItem(PROGRESS_KEY) || "null");
12
+ if (_optionalChain([p, 'optionalAccess', _ => _.sessionUid]) === sessionUid && Number.isFinite(Date.parse(p.startedAt)) && [p.clickCount, p.pagesVisited].every((n) => Number.isSafeInteger(n) && n >= 0)) return p;
13
+ } catch (e) {
14
+ }
15
+ return { sessionUid, startedAt: (/* @__PURE__ */ new Date()).toISOString(), clickCount: 0, pagesVisited: 0 };
16
+ }
17
+ function writeReplayProgress(progress) {
18
+ try {
19
+ sessionStorage.setItem(PROGRESS_KEY, JSON.stringify(progress));
20
+ } catch (e2) {
21
+ }
22
+ }
8
23
  function uuid() {
9
24
  if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
10
25
  return crypto.randomUUID();
@@ -22,7 +37,7 @@ function readSession() {
22
37
  const at = Number(_nullishCoalesce(sessionStorage.getItem(UID_TS_KEY), () => ( "0")));
23
38
  if (!uid || !at) return null;
24
39
  return { uid, at };
25
- } catch (e) {
40
+ } catch (e3) {
26
41
  return null;
27
42
  }
28
43
  }
@@ -31,13 +46,13 @@ function writeSession(uid, at) {
31
46
  if (typeof sessionStorage === "undefined") return;
32
47
  sessionStorage.setItem(UID_KEY, uid);
33
48
  sessionStorage.setItem(UID_TS_KEY, String(at));
34
- } catch (e2) {
49
+ } catch (e4) {
35
50
  }
36
51
  }
37
52
  function getOrCreateSessionUid() {
38
53
  const now = Date.now();
39
54
  const existing = readSession();
40
- if (existing && now - existing.at < IDLE_MS) {
55
+ if (existing && now - existing.at < IDLE_MS && now - Date.parse(readReplayProgress(existing.uid).startedAt) < IDLE_MS) {
41
56
  writeSession(existing.uid, now);
42
57
  return existing.uid;
43
58
  }
@@ -61,7 +76,7 @@ function getOrCreateAnonId() {
61
76
  const fresh = uuid();
62
77
  localStorage.setItem(ANON_KEY, fresh);
63
78
  return fresh;
64
- } catch (e3) {
79
+ } catch (e5) {
65
80
  if (!memAnon) memAnon = uuid();
66
81
  return memAnon;
67
82
  }
@@ -90,7 +105,7 @@ async function postTrace(cfg, path, body) {
90
105
  // instead (separate API, no throw).
91
106
  });
92
107
  return res;
93
- } catch (e4) {
108
+ } catch (e6) {
94
109
  return null;
95
110
  }
96
111
  }
@@ -100,7 +115,7 @@ function beaconTrace(cfg, path, body) {
100
115
  const url = `${normalizeBase(cfg.endpoint)}/api/trace${path}?pk=${encodeURIComponent(cfg.projectKey)}`;
101
116
  const blob = new Blob([JSON.stringify(body)], { type: "application/json" });
102
117
  return navigator.sendBeacon(url, blob);
103
- } catch (e5) {
118
+ } catch (e7) {
104
119
  return false;
105
120
  }
106
121
  }
@@ -118,11 +133,33 @@ async function gzipString(str) {
118
133
  void writer.close();
119
134
  const buf = await new Response(cs.readable).arrayBuffer();
120
135
  return new Uint8Array(buf);
121
- } catch (e6) {
136
+ } catch (e8) {
122
137
  return null;
123
138
  }
124
139
  }
125
140
  var active = null;
141
+ var activationEpoch = 0;
142
+ var cleanup = [];
143
+ var pendingErrors = [];
144
+ function saveProgress() {
145
+ if (!active) return;
146
+ writeReplayProgress({
147
+ sessionUid: active.sessionUid,
148
+ startedAt: active.startedAt,
149
+ clickCount: active.clickCount,
150
+ pagesVisited: active.pagesVisited
151
+ });
152
+ }
153
+ function sendError(signal) {
154
+ if (!active || !active.recordingEnabled || active.paused) return;
155
+ void postTrace(active.config, "/events", { events: [{
156
+ sessionUid: active.sessionUid,
157
+ ts: signal.ts,
158
+ url: signal.url,
159
+ type: "error",
160
+ name: "sdk_error"
161
+ }] });
162
+ }
126
163
  var pendingUser = void 0;
127
164
  function deviceClass() {
128
165
  if (typeof window === "undefined") return "desktop";
@@ -135,7 +172,7 @@ function readQuery(name) {
135
172
  if (typeof location === "undefined") return void 0;
136
173
  try {
137
174
  return _nullishCoalesce(new URLSearchParams(location.search).get(name), () => ( void 0));
138
- } catch (e7) {
175
+ } catch (e9) {
139
176
  return void 0;
140
177
  }
141
178
  }
@@ -146,6 +183,7 @@ function isPathBlocked(opts, path) {
146
183
  async function activateReplay(args) {
147
184
  if (active) return;
148
185
  if (typeof window === "undefined") return;
186
+ const epoch = ++activationEpoch;
149
187
  const options = args.config.options;
150
188
  if (options.enabled === false) return;
151
189
  const sample = Math.max(0, Math.min(100, _nullishCoalesce(options.sample, () => ( 100))));
@@ -153,6 +191,7 @@ async function activateReplay(args) {
153
191
  if (isPathBlocked(options, location.pathname)) return;
154
192
  const sessionUid = getOrCreateSessionUid();
155
193
  const anonId = getOrCreateAnonId();
194
+ const progress = readReplayProgress(sessionUid);
156
195
  const transportCfg = {
157
196
  endpoint: _nullishCoalesce(options.endpoint, () => ( args.endpoint)),
158
197
  projectKey: args.projectKey
@@ -160,11 +199,11 @@ async function activateReplay(args) {
160
199
  const dto = {
161
200
  sessionUid,
162
201
  anonId,
163
- startedAt: (/* @__PURE__ */ new Date()).toISOString(),
202
+ startedAt: progress.startedAt,
164
203
  device: deviceClass(),
165
204
  viewportW: window.innerWidth,
166
205
  viewportH: window.innerHeight,
167
- ua: _optionalChain([navigator, 'access', _ => _.userAgent, 'optionalAccess', _2 => _2.slice, 'call', _3 => _3(0, 512)]),
206
+ ua: _optionalChain([navigator, 'access', _2 => _2.userAgent, 'optionalAccess', _3 => _3.slice, 'call', _4 => _4(0, 512)]),
168
207
  referrer: document.referrer || void 0,
169
208
  landingUrl: location.href.slice(0, 512),
170
209
  utmSource: readQuery("utm_source"),
@@ -175,6 +214,19 @@ async function activateReplay(args) {
175
214
  fbclid: readQuery("fbclid"),
176
215
  gclid: readQuery("gclid")
177
216
  };
217
+ const onClick = (event) => {
218
+ if (!event.isTrusted || epoch !== activationEpoch) return;
219
+ if (!active) {
220
+ progress.clickCount++;
221
+ return;
222
+ }
223
+ if (active.paused || active.replayBlocked || !active.recordingEnabled) return;
224
+ active.clickCount++;
225
+ saveProgress();
226
+ bumpSessionActivity();
227
+ };
228
+ window.addEventListener("click", onClick, true);
229
+ cleanup.push(() => window.removeEventListener("click", onClick, true));
178
230
  let recordingEnabled = true;
179
231
  try {
180
232
  const res = await postTrace(transportCfg, "/session/start", dto);
@@ -182,9 +234,12 @@ async function activateReplay(args) {
182
234
  const body = await res.json().catch(() => ({}));
183
235
  if (body.recordingEnabled === false) recordingEnabled = false;
184
236
  }
185
- } catch (e8) {
237
+ } catch (e10) {
186
238
  }
239
+ if (epoch !== activationEpoch) return;
187
240
  if (!recordingEnabled) {
241
+ window.removeEventListener("click", onClick, true);
242
+ pendingErrors = [];
188
243
  active = {
189
244
  sessionUid,
190
245
  config: transportCfg,
@@ -210,10 +265,13 @@ async function activateReplay(args) {
210
265
  paused: false,
211
266
  userId: null,
212
267
  startedAt: dto.startedAt,
213
- pagesVisited: 1,
214
- clickCount: 0,
268
+ pagesVisited: progress.pagesVisited + 1,
269
+ clickCount: progress.clickCount,
215
270
  recordingEnabled: true
216
271
  };
272
+ saveProgress();
273
+ for (const error of pendingErrors) sendError(error);
274
+ pendingErrors = [];
217
275
  if (pendingUser !== void 0) replayApi.setUser(pendingUser);
218
276
  let lastSessionBump = Date.now();
219
277
  const bumpIfStale = () => {
@@ -224,6 +282,7 @@ async function activateReplay(args) {
224
282
  };
225
283
  for (const ev of ["mousedown", "keydown", "scroll", "touchstart"]) {
226
284
  window.addEventListener(ev, bumpIfStale, { passive: true, capture: true });
285
+ cleanup.push(() => window.removeEventListener(ev, bumpIfStale, true));
227
286
  }
228
287
  const w = window;
229
288
  const scheduleRrwebStart = (run) => {
@@ -247,11 +306,15 @@ async function activateReplay(args) {
247
306
  void Promise.resolve().then(() => _interopRequireWildcard(require("../rrweb-VXG3W6SS.js"))).then(
248
307
  ({ startRrweb }) => new Promise((resolve) => {
249
308
  scheduleRrwebStart(() => {
309
+ if (epoch !== activationEpoch) {
310
+ resolve(null);
311
+ return;
312
+ }
250
313
  startRrweb({
251
314
  sessionUid,
252
315
  options,
253
316
  sendChunk: async (chunk, useBeacon) => {
254
- if (!active || active.replayBlocked) return;
317
+ if (epoch !== activationEpoch || !active || active.replayBlocked) return;
255
318
  const bodyStr = `{"sessionUid":${JSON.stringify(sessionUid)},"chunk":[${chunk.join(",")}]}`;
256
319
  const base = (transportCfg.endpoint || "https://segiapi.extn.ai").replace(/\/+$/, "").replace(/\/api\/ingest\/events$/, "").replace(/\/api\/ingest$/, "").replace(/\/api$/, "");
257
320
  if (useBeacon) {
@@ -263,7 +326,7 @@ async function activateReplay(args) {
263
326
  if (typeof navigator !== "undefined" && typeof navigator.sendBeacon === "function") {
264
327
  navigator.sendBeacon(url, blob);
265
328
  }
266
- } catch (e9) {
329
+ } catch (e11) {
267
330
  }
268
331
  return;
269
332
  }
@@ -284,18 +347,19 @@ async function activateReplay(args) {
284
347
  if (res.status === 429) {
285
348
  if (active) {
286
349
  active.replayBlocked = true;
287
- _optionalChain([active, 'access', _4 => _4.rrweb, 'optionalAccess', _5 => _5.stop, 'call', _6 => _6()]);
350
+ _optionalChain([active, 'access', _5 => _5.rrweb, 'optionalAccess', _6 => _6.stop, 'call', _7 => _7()]);
288
351
  active.rrweb = null;
289
352
  }
290
353
  }
291
- } catch (e10) {
354
+ } catch (e12) {
292
355
  }
293
356
  }
294
357
  }).then(resolve, () => resolve(null));
295
358
  });
296
359
  })
297
360
  ).then((handles) => {
298
- if (handles && active) active.rrweb = handles;
361
+ if (handles && active && epoch === activationEpoch) active.rrweb = handles;
362
+ else _optionalChain([handles, 'optionalAccess', _8 => _8.stop, 'call', _9 => _9()]);
299
363
  }).catch(() => {
300
364
  });
301
365
  void postTrace(transportCfg, "/events", {
@@ -309,9 +373,9 @@ async function activateReplay(args) {
309
373
  }
310
374
  ]
311
375
  });
312
- window.addEventListener("pagehide", () => {
376
+ const onPageHide = () => {
313
377
  if (!active) return;
314
- _optionalChain([active, 'access', _7 => _7.rrweb, 'optionalAccess', _8 => _8.flush, 'call', _9 => _9(true)]);
378
+ _optionalChain([active, 'access', _10 => _10.rrweb, 'optionalAccess', _11 => _11.flush, 'call', _12 => _12(true)]);
315
379
  beaconTrace(active.config, "/session/end", {
316
380
  sessionUid: active.sessionUid,
317
381
  endedAt: (/* @__PURE__ */ new Date()).toISOString(),
@@ -319,12 +383,14 @@ async function activateReplay(args) {
319
383
  pagesVisited: active.pagesVisited,
320
384
  clickCount: active.clickCount
321
385
  });
322
- });
386
+ };
387
+ window.addEventListener("pagehide", onPageHide);
388
+ cleanup.push(() => window.removeEventListener("pagehide", onPageHide));
323
389
  const MAX_SESSION_DURATION_MS = 30 * 6e4;
324
- setTimeout(() => {
390
+ const endTimer = setTimeout(() => {
325
391
  if (!active) return;
326
- _optionalChain([active, 'access', _10 => _10.rrweb, 'optionalAccess', _11 => _11.flush, 'call', _12 => _12(true)]);
327
- _optionalChain([active, 'access', _13 => _13.rrweb, 'optionalAccess', _14 => _14.stop, 'call', _15 => _15()]);
392
+ _optionalChain([active, 'access', _13 => _13.rrweb, 'optionalAccess', _14 => _14.flush, 'call', _15 => _15(true)]);
393
+ _optionalChain([active, 'access', _16 => _16.rrweb, 'optionalAccess', _17 => _17.stop, 'call', _18 => _18()]);
328
394
  active.rrweb = null;
329
395
  beaconTrace(active.config, "/session/end", {
330
396
  sessionUid: active.sessionUid,
@@ -335,22 +401,40 @@ async function activateReplay(args) {
335
401
  });
336
402
  active.paused = true;
337
403
  active.replayBlocked = true;
338
- }, MAX_SESSION_DURATION_MS);
404
+ }, Math.max(0, MAX_SESSION_DURATION_MS - (Date.now() - Date.parse(progress.startedAt))));
405
+ cleanup.push(() => clearTimeout(endTimer));
339
406
  }
340
407
  var replayApi = {
408
+ /** Internal bridge from the error SDK; no stack/message is copied to trace. */
409
+ _captureError(signal) {
410
+ if (!active) {
411
+ if (pendingErrors.length < 20) pendingErrors.push(signal);
412
+ return;
413
+ }
414
+ sendError(signal);
415
+ },
416
+ _stop() {
417
+ activationEpoch++;
418
+ _optionalChain([active, 'optionalAccess', _19 => _19.rrweb, 'optionalAccess', _20 => _20.stop, 'call', _21 => _21()]);
419
+ for (const remove of cleanup) remove();
420
+ cleanup = [];
421
+ active = null;
422
+ pendingErrors = [];
423
+ pendingUser = void 0;
424
+ },
341
425
  pause() {
342
- _optionalChain([active, 'optionalAccess', _16 => _16.rrweb, 'optionalAccess', _17 => _17.pause, 'call', _18 => _18()]);
426
+ _optionalChain([active, 'optionalAccess', _22 => _22.rrweb, 'optionalAccess', _23 => _23.pause, 'call', _24 => _24()]);
343
427
  if (active) active.paused = true;
344
428
  },
345
429
  resume() {
346
- _optionalChain([active, 'optionalAccess', _19 => _19.rrweb, 'optionalAccess', _20 => _20.resume, 'call', _21 => _21()]);
430
+ _optionalChain([active, 'optionalAccess', _25 => _25.rrweb, 'optionalAccess', _26 => _26.resume, 'call', _27 => _27()]);
347
431
  if (active) active.paused = false;
348
432
  bumpSessionActivity();
349
433
  },
350
434
  setUser(user) {
351
435
  pendingUser = user;
352
436
  if (!active) return;
353
- active.userId = _nullishCoalesce(_optionalChain([user, 'optionalAccess', _22 => _22.id, 'optionalAccess', _23 => _23.slice, 'call', _24 => _24(0, 128)]), () => ( null));
437
+ active.userId = _nullishCoalesce(_optionalChain([user, 'optionalAccess', _28 => _28.id, 'optionalAccess', _29 => _29.slice, 'call', _30 => _30(0, 128)]), () => ( null));
354
438
  void postTrace(active.config, "/session/start", {
355
439
  sessionUid: active.sessionUid,
356
440
  anonId: getOrCreateAnonId(),
@@ -386,17 +470,18 @@ var replayApi = {
386
470
  const nowBlocked = isPathBlocked(active.options, location.pathname);
387
471
  if (nowBlocked) {
388
472
  if (!active.paused) {
389
- _optionalChain([active, 'access', _25 => _25.rrweb, 'optionalAccess', _26 => _26.pause, 'call', _27 => _27()]);
473
+ _optionalChain([active, 'access', _31 => _31.rrweb, 'optionalAccess', _32 => _32.pause, 'call', _33 => _33()]);
390
474
  active.paused = true;
391
475
  }
392
476
  return;
393
477
  }
394
478
  if (active.paused) {
395
- _optionalChain([active, 'access', _28 => _28.rrweb, 'optionalAccess', _29 => _29.resume, 'call', _30 => _30()]);
479
+ _optionalChain([active, 'access', _34 => _34.rrweb, 'optionalAccess', _35 => _35.resume, 'call', _36 => _36()]);
396
480
  active.paused = false;
397
481
  }
398
482
  active.pagesVisited += 1;
399
- _optionalChain([active, 'access', _31 => _31.rrweb, 'optionalAccess', _32 => _32.takeFullSnapshot, 'call', _33 => _33()]);
483
+ saveProgress();
484
+ _optionalChain([active, 'access', _37 => _37.rrweb, 'optionalAccess', _38 => _38.takeFullSnapshot, 'call', _39 => _39()]);
400
485
  void postTrace(active.config, "/events", {
401
486
  events: [
402
487
  {
@@ -411,7 +496,7 @@ var replayApi = {
411
496
  },
412
497
  /** Test seam: returns null when replay was not activated for this session. */
413
498
  _sessionUid() {
414
- return _nullishCoalesce(_optionalChain([active, 'optionalAccess', _34 => _34.sessionUid]), () => ( null));
499
+ return _nullishCoalesce(_optionalChain([active, 'optionalAccess', _40 => _40.sessionUid]), () => ( null));
415
500
  }
416
501
  };
417
502