@nivaro/react 0.3.11 → 0.3.12

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.
@@ -1,15 +1,16 @@
1
- import { endSessionRecording as $, appendSessionRecordingEvents as X, sessionRecordingEnabled as ce, startSessionRecording as T } from "@nivaro/sdk";
1
+ import { endSessionRecording as $, appendSessionRecordingEvents as X, sessionRecordingEnabled as ce, startSessionRecording as L } from "@nivaro/sdk";
2
2
  import { useRef as de, useEffect as fe } from "react";
3
- import { sessionStore as pe, sharedChunkStore as he, ReplayUploader as me, clearResume as P, ORPHAN_TTL_MS as we, readResume as ge, isResumable as ve, splitBySize as Z, writeResume as ye, KEEPALIVE_CHUNK_BYTES as be, CHUNK_BYTES as Re, backoffDelay as Se, errorStatus as ke, clipChunkFailure as Ee } from "./replayUploadQueue.js";
4
- import { useAutoRecord as Ae } from "./useAutoRecord.js";
5
- import { useOptionalNivaroClient as Ce } from "../shared/context.js";
3
+ import { sessionStore as pe, sharedChunkStore as he, ReplayUploader as me, clearResume as P, ORPHAN_TTL_MS as we, readResume as ye, isResumable as ge, splitBySize as Z, writeResume as ve, KEEPALIVE_CHUNK_BYTES as be, CHUNK_BYTES as Re, backoffDelay as Se, errorStatus as ke, clipChunkFailure as Ce } from "./replayUploadQueue.js";
4
+ import { useAutoRecord as Ee } from "./useAutoRecord.js";
5
+ import { useOptionalNivaroClient as Ae } from "../shared/context.js";
6
6
  import { normalizeReplayMasking as B, buildReplayMaskingOptions as z } from "../shared/lib/replay-masking.js";
7
- import { normalizeReplaySampling as x, replaySampleDraw as Ie, currentReplayRoute as _, isReplayRouteSampled as Y, attributeChunkRoutes as ee } from "../shared/lib/replay-sampling.js";
8
- import { emitReplayTap as _e, setReplayRecorderMode as N, registerReplayCapture as De, clipEndOffset as qe, withClipKind as Le } from "../shared/lib/replay-capture.js";
9
- import { instrumentConsole as Te, instrumentRequests as Pe, redactSecrets as Ne } from "../shared/lib/replay-context.js";
10
- import { instrumentSocket as Me } from "../shared/lib/replay-socket.js";
11
- const Oe = "nivaro-react@0.3.11", j = 1e4, Ue = 150, We = 3e3, Fe = 600, He = (n) => `nvr_replay_resume${n ? `:${n}` : ""}`, G = () => typeof navigator > "u" || navigator.onLine !== !1;
12
- function Ke(n) {
7
+ import { normalizeReplaySampling as x, replaySampleDraw as Ie, currentReplayRoute as D, isReplayRouteSampled as Y, attributeChunkRoutes as ee } from "../shared/lib/replay-sampling.js";
8
+ import { noteReplayEvent as _e, createReplayCheckoutState as De } from "../shared/lib/replay-checkout.js";
9
+ import { emitReplayTap as Te, setReplayRecorderMode as N, registerReplayCapture as qe, clipEndOffset as Le, withClipKind as Pe } from "../shared/lib/replay-capture.js";
10
+ import { instrumentConsole as Ne, instrumentRequests as Fe, redactSecrets as Me } from "../shared/lib/replay-context.js";
11
+ import { instrumentSocket as Oe } from "../shared/lib/replay-socket.js";
12
+ const Ue = "nivaro-react@0.3.12", j = 1e4, We = 150, He = 3e3, Ke = 600, $e = (n) => `nvr_replay_resume${n ? `:${n}` : ""}`, G = () => typeof navigator > "u" || navigator.onLine !== !1;
13
+ function Be(n) {
13
14
  return typeof n.url != "string" || typeof fetch != "function" ? null : (f, a) => {
14
15
  const u = typeof n.getToken == "function" ? n.getToken() : void 0, d = { "Content-Type": "application/json" };
15
16
  return u && (d.Authorization = `Bearer ${u}`), fetch(`${n.url}/api/session-recordings/${f}/events`, {
@@ -21,32 +22,32 @@ function Ke(n) {
21
22
  });
22
23
  };
23
24
  }
24
- async function $e(n, f, a, u, d) {
25
- const l = Z(u), { routes: b } = ee(l, d.startRoute);
25
+ async function ze(n, f, a, u, d) {
26
+ const l = Z(u), { routes: R } = ee(l, d.startRoute);
26
27
  for (const [c, e] of l.entries()) {
27
28
  const g = a();
28
29
  try {
29
30
  await n.request(
30
31
  X(f, g, e, {
31
- route: b[c],
32
+ route: R[c],
32
33
  ...d.build ? { build: d.build } : {}
33
34
  })
34
35
  );
35
36
  } catch (v) {
36
- const y = ke(v);
37
- if (Ee(y) === "stop") return "closed";
38
- y === 413 && console.warn(`[session-recorder] chunk ${g} too large — dropped`);
37
+ const b = ke(v);
38
+ if (Ce(b) === "stop") return "closed";
39
+ b === 413 && console.warn(`[session-recorder] chunk ${g} too large — dropped`);
39
40
  }
40
41
  }
41
42
  return "ok";
42
43
  }
43
- const Be = 3e4, ze = 3e4;
44
- let J = !1, w = null, S = null;
45
- async function xe(n = "error") {
46
- if (!S) return null;
44
+ const xe = 3e4, Ye = 3e4;
45
+ let J = !1, y = null, k = null;
46
+ async function je(n = "error") {
47
+ if (!k) return null;
47
48
  try {
48
49
  return await Promise.race([
49
- S(n),
50
+ k(n),
50
51
  new Promise((f) => setTimeout(() => f(null), 4e3))
51
52
  ]);
52
53
  } catch {
@@ -63,27 +64,27 @@ async function Q() {
63
64
  }
64
65
  }
65
66
  function V(n, f) {
66
- const a = Te(n), u = Pe(n), d = Me(n), l = () => {
67
+ const a = Ne(n), u = Fe(n), d = Oe(n), l = () => {
67
68
  f == null || f();
68
69
  try {
69
70
  n("route", {
70
- path: Ne(window.location.pathname + window.location.search)
71
+ path: Me(window.location.pathname + window.location.search)
71
72
  });
72
73
  } catch {
73
74
  }
74
- }, b = history.pushState.bind(history), c = history.replaceState.bind(history);
75
+ }, R = history.pushState.bind(history), c = history.replaceState.bind(history);
75
76
  return history.pushState = (...e) => {
76
- b(...e), l();
77
+ R(...e), l();
77
78
  }, history.replaceState = (...e) => {
78
79
  c(...e), l();
79
80
  }, window.addEventListener("popstate", l), l(), () => {
80
- a(), u(), d(), history.pushState = b, history.replaceState = c, window.removeEventListener("popstate", l);
81
+ a(), u(), d(), history.pushState = R, history.replaceState = c, window.removeEventListener("popstate", l);
81
82
  };
82
83
  }
83
- function nt(n = {}) {
84
- const f = Ce(), a = n.client ?? f, u = n.app, d = n.disabled ?? !1, l = n.build ?? Oe;
85
- Ae({ client: a, disabled: d });
86
- const b = de({
84
+ function st(n = {}) {
85
+ const f = Ae(), a = n.client ?? f, u = n.app, d = n.disabled ?? !1, l = n.build ?? Ue;
86
+ Ee({ client: a, disabled: d });
87
+ const R = de({
87
88
  buffer: [],
88
89
  stop: null,
89
90
  uninstrument: null,
@@ -102,94 +103,94 @@ function nt(n = {}) {
102
103
  fe(() => {
103
104
  if (!a || d || typeof window > "u") return;
104
105
  let c = !1;
105
- const e = b.current;
106
+ const e = R.current;
106
107
  e.dead = !1;
107
- const g = a, v = pe(), y = He(u), k = he(), E = Ke(g), te = async (t, r, i, h, s = {}) => {
108
+ const g = a, v = pe(), b = $e(u), C = he(), E = Be(g), te = async (t, r, i, h, w = {}) => {
108
109
  if (!h || !E) {
109
- await g.request(X(t, r, i, s));
110
+ await g.request(X(t, r, i, w));
110
111
  return;
111
112
  }
112
- const p = await E(t, JSON.stringify({ seq: r, events: i, ...s }));
113
- if (!p.ok) throw Object.assign(new Error(`HTTP ${p.status}`), { status: p.status });
114
- }, o = new me({ store: k, send: te, online: G });
115
- let R = null, A = 0;
116
- const re = () => document.readyState === "complete" && Date.now() - e.lastNavAt >= We;
117
- function C() {
118
- o.recordingId && ye(v, y, {
113
+ const s = await E(t, JSON.stringify({ seq: r, events: i, ...w }));
114
+ if (!s.ok) throw Object.assign(new Error(`HTTP ${s.status}`), { status: s.status });
115
+ }, o = new me({ store: C, send: te, online: G });
116
+ let S = null, A = 0;
117
+ const re = () => document.readyState === "complete" && Date.now() - e.lastNavAt >= He;
118
+ function I() {
119
+ o.recordingId && ve(v, b, {
119
120
  recordingId: o.recordingId,
120
121
  nextSeq: o.nextSeq,
121
122
  startedAt: e.startedAt,
122
123
  lastActivityAt: Date.now()
123
124
  });
124
125
  }
125
- function I() {
126
- R && clearTimeout(R), R = null, A = 0;
126
+ function _() {
127
+ S && clearTimeout(S), S = null, A = 0;
127
128
  }
128
129
  function ne() {
129
- I();
130
+ _();
130
131
  const t = Se(o.attempt);
131
- A = Date.now() + t, R = setTimeout(() => {
132
- R = null, A = 0, m(!0);
132
+ A = Date.now() + t, S = setTimeout(() => {
133
+ S = null, A = 0, m(!0);
133
134
  }, t);
134
135
  }
135
- function M() {
136
+ function F() {
136
137
  var t;
137
- e.dead = !0, (t = e.stop) == null || t.call(e), e.stop = null, e.buffer = [], I(), o.discard(), o.stopped = !0, P(v, y);
138
+ e.dead = !0, (t = e.stop) == null || t.call(e), e.stop = null, e.buffer = [], _(), o.discard(), o.stopped = !0, P(v, b);
138
139
  }
139
- function O(t) {
140
+ function M(t) {
140
141
  if (e.buffer.length === 0 || !o.recordingId) return;
141
142
  const r = e.buffer;
142
143
  e.buffer = [];
143
144
  const i = Z(r, t ? be : Re), { routes: h } = ee(i, e.bufferRoute);
144
- for (const [s, p] of i.entries())
145
- o.enqueue(p, { route: h[s], ...l ? { build: l } : {} });
146
- C();
145
+ for (const [w, s] of i.entries())
146
+ o.enqueue(s, { route: h[w], ...l ? { build: l } : {} });
147
+ I();
147
148
  }
148
149
  async function oe(t) {
149
150
  if (!c) {
150
151
  if (t === "retry") ne();
151
- else if (t === "closed" || t === "abandoned") M();
152
+ else if (t === "closed" || t === "abandoned") F();
152
153
  else if (t === "stale")
153
154
  try {
154
- const r = await g.request(T(u));
155
+ const r = await g.request(L(u));
155
156
  if (c) return;
156
- o.rebase(r.data.id), e.startedAt = Date.now(), C(), e.flushAfter = !0;
157
+ o.rebase(r.data.id), e.startedAt = Date.now(), I(), e.flushAfter = !0;
157
158
  } catch {
158
- M();
159
+ F();
159
160
  }
160
161
  }
161
162
  }
162
163
  function m(t = !1, r = !1) {
163
- return e.inFlight ? (t && (e.flushAfter = !0), e.inFlight) : e.dead || !o.recordingId || !t && !re() && e.buffer.length < Fe || (O(r), !o.hasPending() || Date.now() < A) ? Promise.resolve() : (e.inFlight = o.drain(r).then(oe).catch(() => {
164
+ return e.inFlight ? (t && (e.flushAfter = !0), e.inFlight) : e.dead || !o.recordingId || !t && !re() && e.buffer.length < Ke || (M(r), !o.hasPending() || Date.now() < A) ? Promise.resolve() : (e.inFlight = o.drain(r).then(oe).catch(() => {
164
165
  }).finally(() => {
165
166
  e.inFlight = null, e.flushAfter && (e.flushAfter = !1, m(!0));
166
167
  }), e.inFlight);
167
168
  }
168
- function U() {
169
+ function O() {
169
170
  const t = window.requestIdleCallback;
170
171
  t ? t(() => void m(), { timeout: j }) : m();
171
172
  }
172
173
  async function ie() {
173
174
  const t = await Q();
174
175
  if (c || !t) return;
175
- k.prune(Date.now() - we);
176
- const r = ge(v, y);
177
- if (r && ve(r, Date.now())) {
178
- const s = await k.list(r.recordingId);
176
+ C.prune(Date.now() - we);
177
+ const r = ye(v, b);
178
+ if (r && ge(r, Date.now())) {
179
+ const s = await C.list(r.recordingId);
179
180
  if (c) return;
180
181
  o.begin(r.recordingId, r.nextSeq, { trusted: !1, restored: s }), e.startedAt = r.startedAt;
181
182
  } else {
182
- r && (k.clear(r.recordingId), P(v, y));
183
- const s = await g.request(T(u));
183
+ r && (C.clear(r.recordingId), P(v, b));
184
+ const s = await g.request(L(u));
184
185
  if (c) return;
185
186
  o.begin(s.data.id), e.startedAt = Date.now();
186
187
  }
187
- C(), e.lastNavAt = Date.now();
188
+ I(), e.lastNavAt = Date.now();
188
189
  const i = Ie(v);
189
- e.route = _(), e.bufferRoute = e.route, e.sampled = Y(q, e.route, i);
190
+ e.route = D(), e.bufferRoute = e.route, e.sampled = Y(q, e.route, i);
190
191
  const h = () => {
191
192
  var p;
192
- e.route = _();
193
+ e.route = D();
193
194
  const s = e.sampled;
194
195
  if (e.sampled = Y(q, e.route, i), e.sampled && !s)
195
196
  try {
@@ -199,13 +200,19 @@ function nt(n = {}) {
199
200
  );
200
201
  } catch {
201
202
  }
202
- };
203
+ }, w = De();
203
204
  e.stop = t({
204
205
  emit(s) {
205
- e.sampled && (e.buffer.length === 0 && (e.bufferRoute = e.route), e.buffer.push(s), e.buffer.length >= Ue && U());
206
+ e.sampled && (e.buffer.length === 0 && (e.bufferRoute = e.route), e.buffer.push(s), e.buffer.length >= We && O(), _e(w, s) && setTimeout(() => {
207
+ var p;
208
+ try {
209
+ (p = t.takeFullSnapshot) == null || p.call(t, !0);
210
+ } catch {
211
+ w.pending = !1;
212
+ }
213
+ }, 0));
206
214
  },
207
- ...z(() => D),
208
- checkoutEveryNms: 6e4
215
+ ...z(() => T)
209
216
  }) ?? null, e.uninstrument = V(
210
217
  (s, p) => t.addCustomEvent(
211
218
  s,
@@ -214,12 +221,12 @@ function nt(n = {}) {
214
221
  () => {
215
222
  e.lastNavAt = Date.now(), h();
216
223
  }
217
- ), S = async () => {
224
+ ), k = async () => {
218
225
  if (!o.recordingId) return null;
219
226
  const s = Date.now() - e.startedAt;
220
227
  return await m(!0).catch(() => {
221
228
  }), o.recordingId ? { recording_id: o.recordingId, offset_ms: s } : null;
222
- }, N("full"), w = () => {
229
+ }, N("full"), y = () => {
223
230
  if (!o.recordingId) return null;
224
231
  const s = Date.now() - e.startedAt;
225
232
  return async () => (await m(!0).catch(() => {
@@ -228,67 +235,67 @@ function nt(n = {}) {
228
235
  }
229
236
  async function se() {
230
237
  const t = await Q();
231
- c || !t || (e.buffer = [], e.prevWindow = [], e.route = _(), e.bufferRoute = e.route, e.prevWindowRoute = e.route, e.stop = t({
238
+ c || !t || (e.buffer = [], e.prevWindow = [], e.route = D(), e.bufferRoute = e.route, e.prevWindowRoute = e.route, e.stop = t({
232
239
  emit(r, i) {
233
- _e(r), i ? (e.prevWindow = e.buffer, e.prevWindowRoute = e.bufferRoute, e.buffer = [r], e.bufferRoute = e.route) : e.buffer.push(r);
240
+ Te(r), i ? (e.prevWindow = e.buffer, e.prevWindowRoute = e.bufferRoute, e.buffer = [r], e.bufferRoute = e.route) : e.buffer.push(r);
234
241
  },
235
- ...z(() => D),
236
- checkoutEveryNms: Be
242
+ ...z(() => T),
243
+ checkoutEveryNms: xe
237
244
  }) ?? null, e.uninstrument = V(
238
245
  (r, i) => t.addCustomEvent(
239
246
  r,
240
247
  i
241
248
  ),
242
249
  () => {
243
- e.route = _();
250
+ e.route = D();
244
251
  }
245
- ), N("clip"), w = (r) => {
252
+ ), N("clip"), y = (r) => {
246
253
  const i = [...e.prevWindow, ...e.buffer], h = e.prevWindow.length > 0 ? e.prevWindowRoute : e.bufferRoute;
247
254
  if (i.length === 0 || !a) return null;
248
- const s = qe(i, r);
255
+ const w = Le(i, r);
249
256
  return async () => {
250
- const L = (await a.request(
251
- Le(T(u ?? "error-clip", void 0, !0), r)
257
+ const p = (await a.request(
258
+ Pe(L(u ?? "error-clip", void 0, !0), r)
252
259
  )).data.id;
253
260
  let le = 0;
254
- return await $e(a, L, () => le++, i, {
261
+ return await ze(a, p, () => le++, i, {
255
262
  startRoute: h,
256
263
  build: l
257
- }) === "ok" && await a.request($(L)).catch(() => {
258
- }), { recording_id: L, offset_ms: s };
264
+ }) === "ok" && await a.request($(p)).catch(() => {
265
+ }), { recording_id: p, offset_ms: w };
259
266
  };
260
- }, S = async (r) => {
267
+ }, k = async (r) => {
261
268
  if (r === "error") {
262
- if (e.lastClip && Date.now() - e.lastClip.at < ze)
269
+ if (e.lastClip && Date.now() - e.lastClip.at < Ye)
263
270
  return e.lastClip.link;
264
271
  e.lastClip = { at: Date.now(), link: null };
265
272
  }
266
- const i = w == null ? void 0 : w(r);
273
+ const i = y == null ? void 0 : y(r);
267
274
  if (!i) return null;
268
275
  const h = await i();
269
276
  return r === "error" && (e.lastClip = { at: Date.now(), link: h }), h;
270
277
  });
271
278
  }
272
- let W = !1, D = B(null), q = x(null);
279
+ let U = !1, T = B(null), q = x(null);
273
280
  async function ae() {
274
281
  try {
275
282
  const t = await a.request(ce());
276
283
  if (c) return;
277
- D = B(t.data.masking), q = x(t.data.sampling);
284
+ T = B(t.data.masking), q = x(t.data.sampling);
278
285
  const r = t.data.cap_reached === !0;
279
- W = t.data.enabled === !0 && !r;
286
+ U = t.data.enabled === !0 && !r;
280
287
  const i = t.data.enabled !== !0 && t.data.error_replay === !0 && !r;
281
- W ? await ie() : i && await se();
288
+ U ? await ie() : i && await se();
282
289
  } catch {
283
290
  }
284
291
  }
285
292
  ae();
286
- const ue = setInterval(U, j), F = () => {
293
+ const ue = setInterval(O, j), W = () => {
287
294
  document.visibilityState === "hidden" && m(!0, !0);
288
295
  };
289
- document.addEventListener("visibilitychange", F);
296
+ document.addEventListener("visibilitychange", W);
290
297
  const H = () => {
291
- if (!(e.dead || !o.recordingId) && (O(!0), C(), !(!G() || !E)))
298
+ if (!(e.dead || !o.recordingId) && (M(!0), I(), !(!G() || !E)))
292
299
  for (const { chunk: t, body: r } of o.keepaliveBatch())
293
300
  try {
294
301
  E(t.recordingId, r).catch(() => {
@@ -298,23 +305,23 @@ function nt(n = {}) {
298
305
  };
299
306
  window.addEventListener("pagehide", H);
300
307
  const K = () => {
301
- I(), m(!0);
308
+ _(), m(!0);
302
309
  };
303
310
  return window.addEventListener("online", K), () => {
304
311
  var t, r;
305
- c = !0, S = null, w = null, N(null), document.removeEventListener("visibilitychange", F), window.removeEventListener("pagehide", H), window.removeEventListener("online", K), clearInterval(ue), I(), (t = e.uninstrument) == null || t.call(e), e.uninstrument = null, (r = e.stop) == null || r.call(e), e.stop = null, e.prevWindow = [], m(!0).then(() => {
312
+ c = !0, k = null, y = null, N(null), document.removeEventListener("visibilitychange", W), window.removeEventListener("pagehide", H), window.removeEventListener("online", K), clearInterval(ue), _(), (t = e.uninstrument) == null || t.call(e), e.uninstrument = null, (r = e.stop) == null || r.call(e), e.stop = null, e.prevWindow = [], m(!0).then(() => {
306
313
  const i = o.recordingId;
307
314
  i && (e.dead || g.request($(i)).catch(() => {
308
- }), o.discard(), o.recordingId = null, P(v, y));
315
+ }), o.discard(), o.recordingId = null, P(v, b));
309
316
  });
310
317
  };
311
318
  }, [a, d, u, l]);
312
319
  }
313
- De(
314
- (n) => xe(n),
315
- (n) => (w == null ? void 0 : w(n)) ?? null
320
+ qe(
321
+ (n) => je(n),
322
+ (n) => (y == null ? void 0 : y(n)) ?? null
316
323
  );
317
324
  export {
318
- xe as captureErrorClip,
319
- nt as useSessionRecorder
325
+ je as captureErrorClip,
326
+ st as useSessionRecorder
320
327
  };