234 lines
8.1 KiB
JavaScript
234 lines
8.1 KiB
JavaScript
"use client";
|
|
import L, { useState as F, useRef as R, useEffect as H, useCallback as z } from "react";
|
|
import { d as j } from "./chunks/feature-registry-B2iuEiEx.js";
|
|
import { u as G } from "./chunks/provider-runtime-CxEpkUq5.js";
|
|
const N = [87, 77], W = 24, q = 20, A = 10, B = W * A, K = q * A, D = 160, X = 3, S = {
|
|
cool: { r: 0, g: 195, b: 255 },
|
|
warm: { r: 250, g: 130, b: 0 }
|
|
};
|
|
function P(r) {
|
|
if (typeof r != "string")
|
|
throw new TypeError("User id must be a UUID string");
|
|
const t = r.toLowerCase();
|
|
if (!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.test(t))
|
|
throw new TypeError("User id must be a canonical 36-character UUID");
|
|
return t;
|
|
}
|
|
function Y(r) {
|
|
let t = 65535;
|
|
for (const i of r) {
|
|
t ^= i << 8;
|
|
for (let e = 0; e < 8; e++)
|
|
t = t & 32768 ? (t << 1 ^ 4129) & 65535 : t << 1 & 65535;
|
|
}
|
|
return t;
|
|
}
|
|
function Z(r) {
|
|
const i = P(r).replaceAll("-", ""), e = new Uint8Array(20);
|
|
e.set(N, 0);
|
|
for (let o = 0; o < 16; o++)
|
|
e[o + 2] = Number.parseInt(i.slice(o * 2, o * 2 + 2), 16);
|
|
const n = Y(e.subarray(0, 18));
|
|
e[18] = n >> 8, e[19] = n & 255;
|
|
const s = [];
|
|
for (const o of e)
|
|
for (let c = 7; c >= 0; c--) s.push(o >> c & 1);
|
|
return s;
|
|
}
|
|
function J(r) {
|
|
let t = Math.imul(r + 1, 2654435761) + 2135587861 >>> 0;
|
|
return t ^= t >>> 16, t = Math.imul(t, 2246822507) >>> 0, t ^= t >>> 13, t >>> 0;
|
|
}
|
|
function Q(r) {
|
|
const t = J(r), i = {
|
|
x: 1 + (t & 1),
|
|
y: 2 + (t >> 1 & 1),
|
|
w: 3,
|
|
h: 3
|
|
}, e = {
|
|
x: 5 + (t >> 2 & 1),
|
|
y: 2 + (t >> 3 & 1),
|
|
w: 3,
|
|
h: 3
|
|
};
|
|
return { left: i, right: e };
|
|
}
|
|
function V(r, { opacity: t = 0.014 } = {}) {
|
|
if (!(t > 0 && t <= 0.08))
|
|
throw new RangeError("opacity must be between 0 and 0.08");
|
|
const i = Z(r), e = document.createElement("canvas");
|
|
e.width = B, e.height = K;
|
|
const n = e.getContext("2d", { alpha: !0 });
|
|
if (!n) throw new Error("2D canvas is unavailable");
|
|
const s = `rgba(${S.cool.r},${S.cool.g},${S.cool.b},${t})`, o = `rgba(${S.warm.r},${S.warm.g},${S.warm.b},${t})`;
|
|
for (let c = 0; c < D * X; c++) {
|
|
const b = i[c % D], d = c % W * A, a = Math.floor(c / W) * A, { left: w, right: l } = Q(c);
|
|
n.fillStyle = b ? o : s, n.fillRect(d + w.x, a + w.y, w.w, w.h), n.fillStyle = b ? s : o, n.fillRect(d + l.x, a + l.y, l.w, l.h);
|
|
}
|
|
return e;
|
|
}
|
|
function tt(r, t, i = 1) {
|
|
const e = Math.max(1, window.innerWidth), n = Math.max(1, window.innerHeight), s = Math.max(1, i);
|
|
r.width = Math.round(e * s), r.height = Math.round(n * s), r.style.width = `${e}px`, r.style.height = `${n}px`;
|
|
const o = r.getContext("2d", { alpha: !0 });
|
|
if (!o) return !1;
|
|
o.setTransform(s, 0, 0, s, 0, 0), o.imageSmoothingEnabled = !1;
|
|
const c = o.createPattern(t, "repeat");
|
|
return c ? (o.clearRect(0, 0, e, n), o.fillStyle = c, o.fillRect(0, 0, e, n), !0) : !1;
|
|
}
|
|
const et = "https://api.d.aiengines.ir/user_api/v1/users/my_user";
|
|
function rt({
|
|
getAccessToken: r,
|
|
enabled: t = !0,
|
|
userUrl: i = et,
|
|
opacity: e = 0.014,
|
|
sessionKey: n,
|
|
onError: s,
|
|
onReady: o,
|
|
onStatusChange: c
|
|
}) {
|
|
const [b, d] = F({ sessionKey: n, userId: null }), a = R({ onError: s, onReady: o, onStatusChange: c, getAccessToken: r });
|
|
a.current = { onError: s, onReady: o, onStatusChange: c, getAccessToken: r }, H(() => {
|
|
var h, y, k, x;
|
|
let l = !1;
|
|
const f = new AbortController();
|
|
return d({ sessionKey: n, userId: null }), (y = (h = a.current).onStatusChange) == null || y.call(h, "loading"), t ? ((async () => {
|
|
var v, I, g, E;
|
|
try {
|
|
if (typeof a.current.getAccessToken != "function")
|
|
throw new Error("Watermark requires getAccessToken or an authenticated Keycloak token");
|
|
const u = await a.current.getAccessToken();
|
|
if (l) return;
|
|
if (typeof u != "string" || !u.trim())
|
|
throw new Error("Watermark access token is unavailable");
|
|
const m = await fetch(i, {
|
|
method: "GET",
|
|
headers: { Authorization: `Bearer ${u}`, Accept: "application/json" },
|
|
cache: "no-store",
|
|
credentials: "omit",
|
|
signal: f.signal
|
|
});
|
|
if (!m.ok) throw new Error(`Watermark identity HTTP ${m.status}`);
|
|
const p = await m.json(), C = P(p == null ? void 0 : p.id);
|
|
l || d({ sessionKey: n, userId: C });
|
|
} catch (u) {
|
|
if (l || (u == null ? void 0 : u.name) === "AbortError") return;
|
|
d({ sessionKey: n, userId: null }), (I = (v = a.current).onStatusChange) == null || I.call(v, "error"), (E = (g = a.current).onError) == null || E.call(g, u);
|
|
}
|
|
})(), () => {
|
|
l = !0, f.abort();
|
|
}) : ((x = (k = a.current).onStatusChange) == null || x.call(k, "disabled"), () => f.abort());
|
|
}, [t, i, n]);
|
|
const w = b.sessionKey === n ? b.userId : null;
|
|
return H(() => {
|
|
var y, k, x, v, I, g, E, u, m, p, C, $, M, U, _, O;
|
|
if (!t || !w || typeof document > "u") return;
|
|
let l;
|
|
try {
|
|
l = V(w, { opacity: e });
|
|
} catch (T) {
|
|
(k = (y = a.current).onStatusChange) == null || k.call(y, "error"), (v = (x = a.current).onError) == null || v.call(x, T);
|
|
return;
|
|
}
|
|
if (document.querySelector("canvas[data-forensic-watermark]")) {
|
|
const T = new Error("A forensic watermark canvas is already mounted");
|
|
(g = (I = a.current).onStatusChange) == null || g.call(I, "error"), (u = (E = a.current).onError) == null || u.call(E, T);
|
|
return;
|
|
}
|
|
const f = document.createElement("canvas");
|
|
f.setAttribute("aria-hidden", "true"), f.setAttribute("data-forensic-watermark", ""), Object.assign(f.style, {
|
|
position: "fixed",
|
|
inset: "0",
|
|
zIndex: "2147483647",
|
|
pointerEvents: "none",
|
|
userSelect: "none",
|
|
width: "100vw",
|
|
height: "100vh",
|
|
display: "block"
|
|
});
|
|
const h = () => tt(f, l, window.devicePixelRatio || 1);
|
|
document.body.appendChild(f);
|
|
try {
|
|
if (h() !== !0) throw new Error("Could not paint watermark canvas");
|
|
(p = (m = a.current).onStatusChange) == null || p.call(m, "ready"), ($ = (C = a.current).onReady) == null || $.call(C);
|
|
} catch (T) {
|
|
f.remove(), (U = (M = a.current).onStatusChange) == null || U.call(M, "error"), (O = (_ = a.current).onError) == null || O.call(_, T);
|
|
return;
|
|
}
|
|
return window.addEventListener("resize", h, { passive: !0 }), () => {
|
|
window.removeEventListener("resize", h), f.remove();
|
|
};
|
|
}, [t, w, e, n]), null;
|
|
}
|
|
async function nt(r, t) {
|
|
var i;
|
|
if (r != null && r.authenticated) {
|
|
const e = r.instance;
|
|
try {
|
|
e != null && e.refreshToken && await ((i = r.updateToken) == null ? void 0 : i.call(r, 5));
|
|
} catch {
|
|
}
|
|
let n = !1;
|
|
try {
|
|
n = typeof (e == null ? void 0 : e.isTokenExpired) == "function" ? e.isTokenExpired(0) : !1;
|
|
} catch {
|
|
n = !0;
|
|
}
|
|
if (!n) {
|
|
const s = (e == null ? void 0 : e.token) || r.token;
|
|
if (typeof s == "string" && s.trim()) return s;
|
|
}
|
|
}
|
|
return typeof t == "function" && await t() || null;
|
|
}
|
|
function ot({
|
|
children: r,
|
|
enabled: t = !0,
|
|
getAccessToken: i,
|
|
getToken: e,
|
|
sessionKey: n,
|
|
blockUntilReady: s = !1,
|
|
onReady: o,
|
|
onError: c,
|
|
...b
|
|
}) {
|
|
var g, E;
|
|
const d = G(), a = R(d);
|
|
a.current = d;
|
|
const w = R(i ?? e);
|
|
w.current = i ?? e;
|
|
const l = R({ onReady: o, onError: c });
|
|
l.current = { onReady: o, onError: c };
|
|
const f = z(
|
|
() => nt(a.current, w.current),
|
|
[]
|
|
), h = ((E = (g = d == null ? void 0 : d.instance) == null ? void 0 : g.tokenParsed) == null ? void 0 : E.sub) ?? n, [y, k] = F({ sessionKey: h, status: "loading" }), x = y.sessionKey === h ? y.status : "loading", v = z((u) => {
|
|
k({ sessionKey: h, status: u });
|
|
}, [h]), I = t && x === "ready";
|
|
return L.createElement(
|
|
L.Fragment,
|
|
null,
|
|
L.createElement(rt, {
|
|
...b,
|
|
enabled: t,
|
|
sessionKey: h,
|
|
getAccessToken: f,
|
|
onStatusChange: v,
|
|
onReady: () => {
|
|
var u, m;
|
|
return (m = (u = l.current).onReady) == null ? void 0 : m.call(u);
|
|
},
|
|
onError: (u) => {
|
|
var m, p;
|
|
return (p = (m = l.current).onError) == null ? void 0 : p.call(m, u);
|
|
}
|
|
}),
|
|
!s || !t || I ? r : null
|
|
);
|
|
}
|
|
const it = j("Watermark", 65, ot);
|
|
export {
|
|
rt as ForensicWatermark,
|
|
it as Watermark
|
|
};
|