UNPKG

datadog-ux-utils

Version:

Datadog RUM focused UX & performance toolkit: API guards (retry, breaker, rate), React telemetry (error boundary, profiler, Suspense), web vitals & resource observers, offline queues.

229 lines (228 loc) 7.13 kB
import { a as L } from "./datadog-irY4zUa4.js"; let w = !1, u = null, a; const k = /* @__PURE__ */ new Map(), E = { errorRate: 20, warnRate: 5, logRate: 0, dedupeWindowMs: 5e3, maxStringLen: 1e3, maxArgs: 5, includeTrace: !1, captureInDev: !1, sanitize: N }; function W(e = {}) { return w || (w = !0, a = { ...E, ...e }, u = { log: console.log.bind(console), warn: console.warn.bind(console), error: console.error.bind(console) }, console.error = v("error", u.error, a.errorRate), console.warn = v("warn", u.warn, a.warnRate), console.log = v("log", u.log, a.logRate)), M; } function M() { w && (w = !1, u && (console.log = u.log, console.warn = u.warn, console.error = u.error, u = null), k.clear()); } function v(e, t, r) { const n = (s) => Math.random() * 100 < A(s); return function(...i) { try { t.apply(console, i); } catch { } if (typeof process < "u" && process.env && process.env.NODE_ENV !== "production" && !a.captureInDev && e !== "error" || r <= 0 || !n(r)) return; const m = i.slice(0, a.maxArgs).map((l) => a.sanitize(l)), p = O(m), o = `${e}:${p}`, c = Date.now(), f = k.get(o); if (!(f && c - f < a.dedupeWindowMs)) { if (k.set(o, c), R(p, a.maxStringLen), i.length, a.includeTrace && e !== "error") { const l = new Error().stack; l && $(l); } x(i); } }; } function x(e) { for (const t of e) { if (t instanceof Error) return t; if (t && typeof t == "object" && ("message" in t || "stack" in t)) try { const r = new Error(t.message ?? "Error"); return t.name && (r.name = t.name), t.stack && (r.stack = t.stack), r; } catch { } } return null; } function O(e) { if (!e.length) return ""; const t = e[0]; if (typeof t == "string") return t; try { return JSON.stringify(t); } catch { return String(t); } } function N(e) { if (e == null) return e; const t = typeof e; if (t === "string") return R(e, E.maxStringLen); if (t === "number" || t === "boolean") return e; if (e instanceof Error) return { name: e.name, message: R(e.message, E.maxStringLen), stack: $(e.stack ?? "") }; if (Array.isArray(e)) return e.slice(0, 10).map(N); if (t === "function") return "[function]"; if (t === "object") { const r = {}, n = e; let s = 0; for (const i in n) if (Object.prototype.hasOwnProperty.call(n, i) && (r[i] = N(n[i]), ++s >= 30)) break; return r; } try { return JSON.stringify(e); } catch { return String(e); } } function R(e, t) { return e.length <= t ? e : e.slice(0, t) + "…"; } function $(e) { return e.split(` `).slice(0, 5).join(` `); } function A(e) { return Number.isNaN(e) ? 0 : Math.max(0, Math.min(100, Math.round(e))); } const z = { sampleRate: 20, dedupeWindowMs: 6e4, maxPerMinute: 60, includeElementInfo: !0, captureCspViolations: !0, actionName: "resource_error", cspActionName: "csp_violation" }; let g = !1; function F(e = {}) { if (g) return p; g = !0; const t = { ...z, ...e }; let r = t.maxPerMinute, n = Date.now(); const s = /* @__PURE__ */ new Map(), i = () => { const o = Date.now(); o - n >= 6e4 && (r = t.maxPerMinute, n = o); }, d = (o) => { const c = o.target; if (!c || !(c instanceof Element)) return; const f = c.tagName.toLowerCase(); if (!_(f)) return; const l = I(c), h = D(f, l, "load_error"); if (!C(t.sampleRate) || (i(), r <= 0) || P(s, h, t.dedupeWindowMs)) return; r--; const b = { tag: f, url: l, reason: "load_error" }; t.includeElementInfo && (b.el = j(c)); try { L(t.actionName, b, t.sampleRate); } catch { } }, m = (o) => { if (!t.captureCspViolations || !C(t.sampleRate) || (i(), r <= 0)) return; const c = o.effectiveDirective || o.violatedDirective || "unknown", f = o.blockedURI || "unknown", l = D("csp", `${c}:${f}`, "csp_violation"); if (P(s, l, t.dedupeWindowMs)) return; r--; const h = { reason: "csp_violation", effectiveDirective: o.effectiveDirective, blockedURI: o.blockedURI, lineNumber: o.lineNumber, sourceFile: o.sourceFile, statusCode: o.statusCode, sample: o.sample, disposition: o.disposition, originalPolicy: y(o.originalPolicy ?? "", 512), referrer: document && document.referrer || void 0 }; try { L(t.cspActionName, h, t.sampleRate); } catch { } }; window.addEventListener("error", d, !0), t.captureCspViolations && S() && window.addEventListener("securitypolicyviolation", m); function p() { g && (g = !1, window.removeEventListener("error", d, !0), t.captureCspViolations && S() && window.removeEventListener("securitypolicyviolation", m), s.clear()); } return p; } function _(e) { return e === "img" || e === "script" || e === "link"; } function I(e) { const t = e.tagName.toLowerCase(); return t === "img" ? e.currentSrc || e.src || null : t === "script" ? e.src || null : t === "link" && e.href || null; } function j(e) { const t = e.tagName.toLowerCase(), r = { tag: t }; if (t === "img") { const n = e; r.loading = n.loading || void 0, r.decoding = n.decoding || void 0, r.referrerPolicy = n.referrerPolicy || void 0, r.crossOrigin = n.crossOrigin || void 0, r.sizes = n.sizes || void 0, r.srcset = y(n.srcset || "", 256), r.width = n.width || void 0, r.height = n.height || void 0; } else if (t === "script") { const n = e; r.async = !!n.async, r.defer = !!n.defer, r.type = n.type || void 0, r.crossOrigin = n.crossOrigin || void 0, r.referrerPolicy = n.referrerPolicy || void 0, r.integrity = y(n.integrity || "", 128); } else if (t === "link") { const n = e; r.rel = n.rel || void 0, r.as = n.as || void 0, r.media = n.media || void 0, r.type = n.type || void 0, r.crossOrigin = n.crossOrigin || void 0, r.referrerPolicy = n.referrerPolicy || void 0, r.sizes = n.sizes || void 0, r.integrity = y(n.integrity || "", 128); } return r.selector = U(e), r; } function U(e) { try { const t = []; let r = e, n = 0; for (; r && n < 4; ) { const s = r.tagName.toLowerCase(), i = r.id ? `#${r.id}` : "", d = r.classList && r.classList.length ? "." + Array.from(r.classList).slice(0, 2).join(".") : ""; t.unshift(s + i + d), r = r.parentElement, n++; } return t.join(" > "); } catch { return e.tagName.toLowerCase(); } } function D(e, t, r) { return `${r}:${e}:${t ?? "null"}`; } function P(e, t, r) { const n = Date.now(), s = e.get(t); if (s && n - s < r) return !0; if (e.set(t, n), e.size > 2e3) for (const [i, d] of e) n - d > r && e.delete(i); return !1; } function S() { return "SecurityPolicyViolationEvent" in window; } function C(e) { return Math.random() * 100 < V(e); } function y(e, t) { return e.length <= t ? e : e.slice(0, t) + "…"; } function V(e) { return Number.isNaN(e) ? 0 : Math.max(0, Math.min(100, Math.round(e))); } export { F as a, W as c, M as u }; //# sourceMappingURL=resourceErrors-CfUjbaiC.js.map