@nivaro/react 0.3.8 → 0.3.9

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,34 +1,35 @@
1
- let m = !1, v, c = (e) => e;
2
- const d = [];
3
- let h = null, p = "/api", f, w = null, u = 0, g = null;
1
+ import { isSignedOut as E, isSignedOutError as T, noteUnauthorized as k } from "./signed-out.js";
2
+ let m = !1, v, d = (e) => e;
3
+ const o = [];
4
+ let h = null, p = "/api", f, w = null, c = 0, g = null;
4
5
  function y(e) {
5
6
  const t = e.tagName.toLowerCase(), n = (e.textContent ?? "").trim().slice(0, 40), r = e.getAttribute("aria-label"), i = e.getAttribute("data-testid");
6
7
  return `${t}${i ? `[${i}]` : ""}${r ? ` '${r}'` : n ? ` '${n}'` : ""}`;
7
8
  }
8
- function E(e) {
9
+ function _(e) {
9
10
  typeof window > "u" || window.addEventListener(
10
11
  "click",
11
12
  (t) => {
12
13
  const n = t.target instanceof Element ? t.target.closest('button, a, [role="button"], input, [data-testid]') : null;
13
- n && (n === w ? (u++, u === 3 && (s({
14
+ n && (n === w ? (c++, c === 3 && (s({
14
15
  kind: "rage",
15
16
  route: `${e()} :: ${y(n)}`.slice(0, 300),
16
- duration_ms: u
17
- }), o())) : (w = n, u = 1), g && clearTimeout(g), g = setTimeout(() => {
18
- w = null, u = 0;
17
+ duration_ms: c
18
+ }), u())) : (w = n, c = 1), g && clearTimeout(g), g = setTimeout(() => {
19
+ w = null, c = 0;
19
20
  }, 600));
20
21
  },
21
22
  { capture: !0, passive: !0 }
22
23
  );
23
24
  }
24
- const T = 1e3, k = 1e4, b = 3;
25
- function _(e = b, t = k) {
25
+ const $ = 1e3, M = 1e4, b = 3;
26
+ function D(e = b, t = M) {
26
27
  let n = [];
27
28
  return (r) => (n = n.filter((i) => r - i < t), n.push(r), n.length >= e ? (n = [], !0) : !1);
28
29
  }
29
- function $(e) {
30
+ function A(e) {
30
31
  if (typeof window > "u" || typeof MutationObserver > "u") return;
31
- const t = _();
32
+ const t = D();
32
33
  window.addEventListener(
33
34
  "click",
34
35
  (n) => {
@@ -54,15 +55,19 @@ function $(e) {
54
55
  kind: "dead",
55
56
  route: `${e()} :: ${y(r)}`.slice(0, 300),
56
57
  duration_ms: b
57
- }), o());
58
- }, T);
58
+ }), u());
59
+ }, $);
59
60
  },
60
61
  { capture: !0, passive: !0 }
61
62
  );
62
63
  }
63
- function o() {
64
- if (d.length === 0) return;
65
- const e = JSON.stringify({ events: d.splice(0, d.length) });
64
+ function u() {
65
+ if (o.length === 0) return;
66
+ if (E()) {
67
+ o.length = 0;
68
+ return;
69
+ }
70
+ const e = JSON.stringify({ events: o.splice(0, o.length) });
66
71
  try {
67
72
  const t = f == null ? void 0 : f();
68
73
  !t && typeof navigator.sendBeacon == "function" && navigator.sendBeacon(`${p}/rum`, new Blob([e], { type: "application/json" })) || fetch(`${p}/rum`, {
@@ -74,22 +79,26 @@ function o() {
74
79
  },
75
80
  body: e,
76
81
  keepalive: !0
82
+ }).then(async (r) => {
83
+ if (r.status !== 401) return;
84
+ const i = await r.json().catch(() => null);
85
+ T({ status: 401, response: i }) && k(!1);
77
86
  }).catch(() => {
78
87
  });
79
88
  } catch {
80
89
  }
81
90
  }
82
91
  function s(e) {
83
- d.push({
92
+ o.push({
84
93
  ...e,
85
94
  app: v,
86
95
  vw: typeof window > "u" ? void 0 : window.innerWidth
87
- }), d.length >= 10 ? o() : (h && clearTimeout(h), h = setTimeout(o, 1e4));
96
+ }), o.length >= 10 ? u() : (h && clearTimeout(h), h = setTimeout(u, 1e4));
88
97
  }
89
- const M = /^[A-Za-z0-9._:-]{1,80}$/, D = /^[0-9A-Fa-f-]{36}$/;
90
- function A(e) {
91
- if (!m || !M.test(e.key)) return;
92
- const t = e.roleId && D.test(e.roleId) ? e.roleId.toUpperCase() : void 0;
98
+ const C = /^[A-Za-z0-9._:-]{1,80}$/, B = /^[0-9A-Fa-f-]{36}$/;
99
+ function O(e) {
100
+ if (!m || !C.test(e.key)) return;
101
+ const t = e.roleId && B.test(e.roleId) ? e.roleId.toUpperCase() : void 0;
93
102
  s({
94
103
  kind: "widget",
95
104
  route: `widget:${e.key}`,
@@ -99,19 +108,19 @@ function A(e) {
99
108
  paint_ms: Math.max(0, Math.round(e.paintMs))
100
109
  });
101
110
  }
102
- function C(e) {
111
+ function S(e) {
103
112
  if (!m || typeof window > "u") return;
104
113
  const t = performance.now();
105
114
  requestAnimationFrame(() => {
106
115
  requestAnimationFrame(() => {
107
116
  const n = Math.round(performance.now() - t);
108
- s({ route: c(e), kind: "route", duration_ms: n });
117
+ s({ route: d(e), kind: "route", duration_ms: n });
109
118
  });
110
119
  });
111
120
  }
112
- function B(e) {
121
+ function I(e) {
113
122
  if (m || typeof window > "u" || typeof performance > "u") return;
114
- m = !0, v = e == null ? void 0 : e.app, e != null && e.routePattern && (c = e.routePattern), e != null && e.apiBase && (p = e.apiBase), f = e == null ? void 0 : e.getToken, E(() => c(window.location.pathname)), $(() => c(window.location.pathname));
123
+ m = !0, v = e == null ? void 0 : e.app, e != null && e.routePattern && (d = e.routePattern), e != null && e.apiBase && (p = e.apiBase), f = e == null ? void 0 : e.getToken, _(() => d(window.location.pathname)), A(() => d(window.location.pathname));
115
124
  const t = performance.getEntriesByType("navigation")[0];
116
125
  let n = null;
117
126
  try {
@@ -124,23 +133,23 @@ function B(e) {
124
133
  setTimeout(() => {
125
134
  const i = performance.getEntriesByName("first-contentful-paint")[0];
126
135
  s({
127
- route: c(window.location.pathname),
136
+ route: d(window.location.pathname),
128
137
  kind: "load",
129
138
  ttfb_ms: t ? Math.round(t.responseStart) : void 0,
130
139
  fcp_ms: i ? Math.round(i.startTime) : void 0,
131
140
  lcp_ms: n ?? void 0,
132
141
  duration_ms: t ? Math.round(t.loadEventEnd || t.domContentLoadedEventEnd) : void 0
133
142
  });
134
- }, 6e3), window.addEventListener("pagehide", o), document.addEventListener("visibilitychange", () => {
135
- document.visibilityState === "hidden" && o();
143
+ }, 6e3), window.addEventListener("pagehide", u), document.addEventListener("visibilitychange", () => {
144
+ document.visibilityState === "hidden" && u();
136
145
  });
137
146
  }
138
147
  export {
139
148
  b as DEAD_BURST,
140
- T as DEAD_WAIT_MS,
141
- k as DEAD_WINDOW_MS,
142
- _ as createDeadClickCounter,
143
- C as rumRouteChange,
144
- A as rumWidget,
145
- B as startRum
149
+ $ as DEAD_WAIT_MS,
150
+ M as DEAD_WINDOW_MS,
151
+ D as createDeadClickCounter,
152
+ S as rumRouteChange,
153
+ O as rumWidget,
154
+ I as startRum
146
155
  };
@@ -0,0 +1,114 @@
1
+ const T = 1e3, y = 3e5, S = /* @__PURE__ */ new Set([
2
+ "NOT_SIGNED_IN",
3
+ "SESSION_EXPIRED",
4
+ "TOKEN_INVALID",
5
+ "ACCOUNT_NOT_ACTIVE"
6
+ ]), _ = /^(MASQUERADE_|KEY_SIM_)/, E = /* @__PURE__ */ new Set(["/auth/me", "/users/me", "/auth/ws-token"]);
7
+ let r = !1, u = 0, i = 0;
8
+ const a = /* @__PURE__ */ new Set();
9
+ let o, f = !1;
10
+ function d(t, n = Math.random) {
11
+ const e = Math.min(1e3 * 2 ** Math.max(0, t - 1), 3e5);
12
+ return Math.min(3e5, Math.round(e * (0.75 + n() * 0.5)));
13
+ }
14
+ function I(t) {
15
+ var s;
16
+ const n = t, e = typeof (n == null ? void 0 : n.status) == "number" ? n.status : (s = n == null ? void 0 : n.response) == null ? void 0 : s.status;
17
+ return typeof e == "number" ? e : void 0;
18
+ }
19
+ function N(t) {
20
+ var s;
21
+ const n = t == null ? void 0 : t.response, e = (n == null ? void 0 : n.code) ?? ((s = n == null ? void 0 : n.data) == null ? void 0 : s.code);
22
+ return typeof e == "string" ? e : void 0;
23
+ }
24
+ function O(t, n = !1) {
25
+ if (I(t) !== 401) return !1;
26
+ const e = N(t);
27
+ return e && _.test(e) ? !1 : n || e != null && S.has(e);
28
+ }
29
+ function D() {
30
+ return r;
31
+ }
32
+ function g(t = !0) {
33
+ r && !t || (u += 1, r = !0, i = Date.now() + d(u));
34
+ }
35
+ function c() {
36
+ const t = r;
37
+ if (r = !1, u = 0, i = 0, t)
38
+ for (const n of [...a])
39
+ try {
40
+ n();
41
+ } catch {
42
+ }
43
+ return t;
44
+ }
45
+ function l() {
46
+ if (o !== void 0) return o;
47
+ o = null;
48
+ try {
49
+ typeof BroadcastChannel == "function" && (o = new BroadcastChannel("nvr-auth"), o.onmessage = (t) => {
50
+ var n;
51
+ ((n = t.data) == null ? void 0 : n.t) === "signed-in" && c();
52
+ });
53
+ } catch {
54
+ o = null;
55
+ }
56
+ return o;
57
+ }
58
+ function h() {
59
+ const t = l();
60
+ if (!(!c() && f)) {
61
+ f = !0;
62
+ try {
63
+ t == null || t.postMessage({ t: "signed-in" });
64
+ } catch {
65
+ }
66
+ }
67
+ }
68
+ function w() {
69
+ c();
70
+ }
71
+ function C(t) {
72
+ return l(), a.add(t), () => {
73
+ a.delete(t);
74
+ };
75
+ }
76
+ function M() {
77
+ if (!r) return !0;
78
+ const t = Date.now();
79
+ return t < i ? !1 : (i = t + d(u), !0);
80
+ }
81
+ function G() {
82
+ return Object.assign(new Error("Not signed in"), {
83
+ status: 401,
84
+ response: { error: "Not signed in", code: "NOT_SIGNED_IN" },
85
+ signedOutGate: !0
86
+ });
87
+ }
88
+ function p(t, n, e) {
89
+ return (t ?? "GET").toUpperCase() !== "GET" || !n || e && typeof e == "object" && Object.keys(e).length > 0 ? !1 : E.has(n);
90
+ }
91
+ async function A(t, n) {
92
+ if (!M()) return n();
93
+ try {
94
+ const e = await t();
95
+ return h(), e;
96
+ } catch (e) {
97
+ throw O(e, !0) && g(), e;
98
+ }
99
+ }
100
+ export {
101
+ y as SIGNED_OUT_MAX_MS,
102
+ T as SIGNED_OUT_MIN_MS,
103
+ M as claimIdentityCall,
104
+ A as guardIdentityCall,
105
+ p as isIdentityRead,
106
+ D as isSignedOut,
107
+ O as isSignedOutError,
108
+ w as noteCredentialChanged,
109
+ h as noteSignedIn,
110
+ g as noteUnauthorized,
111
+ C as onSignedIn,
112
+ d as signedOutBackoff,
113
+ G as signedOutError
114
+ };
@@ -1,6 +1,6 @@
1
1
  var N = Object.defineProperty;
2
2
  var U = (i, n, r) => n in i ? N(i, n, { enumerable: !0, configurable: !0, writable: !0, value: r }) : i[n] = r;
3
- var b = (i, n, r) => U(i, typeof n != "symbol" ? n + "" : n, r);
3
+ var _ = (i, n, r) => U(i, typeof n != "symbol" ? n + "" : n, r);
4
4
  import { bindClientChaosSocket as k } from "./client-chaos.js";
5
5
  import { deliveryAckFor as L } from "./delivery-ack.js";
6
6
  import { forgetSettledReads as y } from "./request-gate.js";
@@ -11,9 +11,9 @@ const p = {
11
11
  configUpdate: "nvr:config-update",
12
12
  forceRefresh: "nvr:force-refresh"
13
13
  };
14
- class E {
14
+ class C {
15
15
  constructor(n, r) {
16
- b(this, "refs", /* @__PURE__ */ new Map());
16
+ _(this, "refs", /* @__PURE__ */ new Map());
17
17
  this.join = n, this.leave = r;
18
18
  }
19
19
  add(n) {
@@ -45,33 +45,35 @@ function G(i) {
45
45
  }, R = () => {
46
46
  var e, o, t;
47
47
  return ((t = (o = (e = n == null ? void 0 : n.io) == null ? void 0 : e.engine) == null ? void 0 : o.transport) == null ? void 0 : t.name) ?? null;
48
- }, C = (e, o) => {
48
+ }, b = (e, o) => {
49
49
  l && clearTimeout(l), l = null, g({ state: "live", transport: R(), refreshed: e, resumed: o });
50
50
  }, u = (e, o) => {
51
51
  typeof window > "u" || window.dispatchEvent(new CustomEvent(e, { detail: o }));
52
52
  }, h = (e, o) => {
53
53
  n != null && n.connected && n.emit(e, o);
54
- }, F = (e, o) => {
54
+ }, E = (e, o) => {
55
55
  var t;
56
56
  for (const s of ((t = n == null ? void 0 : n.listeners) == null ? void 0 : t.call(n, e)) ?? []) s(o);
57
- }, _ = (e) => {
58
- for (const o of W(e)) F(o.event, o.payload);
57
+ }, F = (e) => {
58
+ for (const o of W(e)) E(o.event, o.payload);
59
59
  u("nvr:realtime-stale", { rooms: e });
60
60
  }, m = M({
61
61
  request: (e) => h("catchup:room", e),
62
- dispatch: F,
63
- refetch: _
64
- }), q = new E(
62
+ dispatch: E,
63
+ refetch: F
64
+ }), q = new C(
65
65
  (e) => {
66
66
  m.reset([`collection:${e}`]), h("collection:join", { collection: e });
67
67
  },
68
68
  (e) => {
69
69
  m.reset([`collection:${e}`]), h("collection:leave", { collection: e });
70
70
  }
71
- ), A = new E(
72
- (e) => h("admin:join", { room: e }),
71
+ ), A = new C(
72
+ // The Traffic Map page reads batched frames (api traffic-map-batch.ts); twin: admin
73
+ // lib/socket.ts watchJoinPayload.
74
+ (e) => h("admin:join", e === "traffic-map" ? { room: e, frames: "batch" } : { room: e }),
73
75
  (e) => h("admin:leave", { room: e })
74
- ), S = new E(
76
+ ), S = new C(
75
77
  (e) => h("ops:join", { topic: e }),
76
78
  (e) => h("ops:leave", { topic: e })
77
79
  ), w = () => {
@@ -81,7 +83,7 @@ function G(i) {
81
83
  return n = e, k(e), j = e.connected, e.on("auth:ok", () => {
82
84
  if (m.reset(), q.rejoinAll(), A.rejoinAll(), S.rejoinAll(), j && r > 0) {
83
85
  const t = r;
84
- g({ transport: R() }), l && clearTimeout(l), l = setTimeout(() => C(null, "unverified"), i.catchupTimeoutMs ?? 8e3), e.emit("catchup", { cursor: t });
86
+ g({ transport: R() }), l && clearTimeout(l), l = setTimeout(() => b(null, "unverified"), i.catchupTimeoutMs ?? 8e3), e.emit("catchup", { cursor: t });
85
87
  } else
86
88
  g({
87
89
  state: "live",
@@ -101,7 +103,7 @@ function G(i) {
101
103
  d && e.emit("rt:ack", d);
102
104
  }), e.on("catchup:room:events", (t) => m.replayed(t)), e.on("realtime:stale", (t) => {
103
105
  const s = Array.isArray(t == null ? void 0 : t.rooms) ? t.rooms.filter((c) => typeof c == "string") : [];
104
- m.reset(s), t != null && t.all ? (v("catchupFull") && u(p.catchupFull, {}), u("nvr:realtime-stale", { rooms: s, all: !0 })) : s.length && _(s);
106
+ m.reset(s), t != null && t.all ? (v("catchupFull") && u(p.catchupFull, {}), u("nvr:realtime-stale", { rooms: s, all: !0 })) : s.length && F(s);
105
107
  }), e.on(
106
108
  "catchup:events",
107
109
  (t) => {
@@ -110,10 +112,10 @@ function G(i) {
110
112
  c.seq > r && (r = c.seq);
111
113
  for (const d of ((s = e.listeners) == null ? void 0 : s.call(e, c.event)) ?? []) d({ ...c.payload, _seq: c.seq });
112
114
  }
113
- C(null, "replayed");
115
+ b(null, "replayed");
114
116
  }
115
117
  ), e.on("catchup:full", (t) => {
116
- v("catchupFull") && u(p.catchupFull, {}), C(
118
+ v("catchupFull") && u(p.catchupFull, {}), b(
117
119
  (t == null ? void 0 : t.reason) === "slow-consumer" ? "Live updates resumed. This tab fell too far behind to send each change, so the lists and records on this page were reloaded." : "Live updates resumed. The connection was gone too long to replay each change, so the lists and records on this page were reloaded.",
118
120
  "reloaded"
119
121
  );
@@ -1,18 +1,19 @@
1
- import { readOnlinePresence as i } from "@nivaro/sdk";
2
- import { useQuery as a } from "@tanstack/react-query";
3
- import { useNivaroClient as l } from "../context.js";
4
- function m(r = !0) {
5
- const t = l(), { data: n } = a({
1
+ import { readOnlinePresence as l } from "@nivaro/sdk";
2
+ import { useQuery as s } from "@tanstack/react-query";
3
+ import { useNivaroClient as u } from "../context.js";
4
+ import { usePresenceOnlinePoll as a } from "./presence-live.js";
5
+ function y(r = !0) {
6
+ const t = u(), n = a(3e4, r), { data: o } = s({
6
7
  queryKey: ["presence-online"],
7
- queryFn: () => t.request(i()),
8
- select: (e) => ((e == null ? void 0 : e.data) ?? []).map((o) => String(o.user_id ?? "").toUpperCase()).filter(Boolean),
8
+ queryFn: () => t.request(l()),
9
+ select: (e) => ((e == null ? void 0 : e.data) ?? []).map((i) => String(i.user_id ?? "").toUpperCase()).filter(Boolean),
9
10
  enabled: r,
10
- refetchInterval: 3e4,
11
+ refetchInterval: n,
11
12
  staleTime: 25e3,
12
13
  retry: !1
13
14
  });
14
- return new Set(n ?? []);
15
+ return new Set(o ?? []);
15
16
  }
16
17
  export {
17
- m as useOnlineUsers
18
+ y as useOnlineUsers
18
19
  };