@xcodleox/devinspector 9.1.0 → 9.2.1

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/node.js CHANGED
@@ -1 +1 @@
1
- import{AsyncLocalStorage as B}from"async_hooks";var T="https://api.devinspector.com.br/api/ingest/track",M="https://api.devinspector.com.br/api/ingest/apm";var x=["password","senha","token","authorization","auth","bearer","credit_card","creditcard","cartao","cvv","cpf","rg","secret","private_key","privatekey"];function P(t){let e=t.toLowerCase().replace(/[\s-]/g,"_");return x.some(n=>e.includes(n))}function y(t,e=0,n=new WeakSet){if(t==null)return t;if(typeof t=="string")return t.length>1e4?`${t.slice(0,1e4)}...[truncated]`:t;if(typeof t=="number"||typeof t=="boolean")return t;if(typeof t=="bigint")return String(t);if(typeof t=="function")return"[Function]";if(e>=6)return"[MaxDepth]";if(typeof t!="object")return String(t);if(n.has(t))return"[Circular]";n.add(t);try{if(Array.isArray(t))return t.slice(0,100).map(o=>y(o,e+1,n));let r=t,s={};for(let[o,i]of Object.entries(r)){if(P(o)){s[o]="[REDACTED]";continue}s[o]=y(i,e+1,n)}return s}finally{n.delete(t)}}function w(t){if(t instanceof Error)return t.message||"Erro desconhecido";if(typeof t=="string")return t||"Erro desconhecido";try{let e=y(t);return typeof e=="string"?e||"Erro desconhecido":JSON.stringify(e)||"Erro desconhecido"}catch{return"Erro desconhecido"}}function v(t){return t&&String(t).trim().toUpperCase()||void 0}function R(t){if(!(typeof t!="number"||!Number.isFinite(t)))return Math.round(t)}function S(t){if(!(typeof t!="number"||!Number.isFinite(t)))return Math.max(0,Math.round(t))}function N(t){return String(t??"").trim().slice(0,200)}function O(t){let e=Number(t);if(!Number.isFinite(e))throw new Error("metricValue deve ser um n\xFAmero finito.");return e}function b(t){let e=typeof globalThis<"u"?globalThis.crypto:void 0;return e&&typeof e.randomUUID=="function"?`${t}-${e.randomUUID()}`:`${t}-${Date.now()}-${Math.random().toString(36).slice(2,12)}`}function l(t){return y(t)}var E=class{constructor(){this.apiKey="";this.endpoint=T;this.apmEndpoint=M;this.environment="production";this.slowThresholdMs=300;this.requestTimeoutMs=1e4;this.breadcrumbs=[];this.initialized=!1;this.listenersAttached=!1;this.sendQueue=[];this.isProcessingQueue=!1;this.flushResolvers=[]}init(e){if(!e?.apiKey?.trim())throw new Error("DevInspector: apiKey \xE9 obrigat\xF3rio.");this.apiKey=e.apiKey.trim(),this.endpoint=e.endpoint?.trim()||T,this.apmEndpoint=e.apmEndpoint?.trim()||M,this.environment=e.environment?.trim()||"production",this.release=e.release?.trim()||void 0,this.slowThresholdMs=typeof e.slowThresholdMs=="number"&&Number.isFinite(e.slowThresholdMs)&&e.slowThresholdMs>=0?Math.round(e.slowThresholdMs):300,this.requestTimeoutMs=typeof e.requestTimeoutMs=="number"&&Number.isFinite(e.requestTimeoutMs)&&e.requestTimeoutMs>0?Math.round(e.requestTimeoutMs):1e4,this.initialized=!0,this.attachGlobalListeners(),this.processQueue()}getApiKey(){return this.apiKey}getEndpoint(){return this.endpoint}getApmEndpoint(){return this.apmEndpoint}getEnvironment(){return this.environment}getRelease(){return this.release}getSlowThresholdMs(){return this.slowThresholdMs}getRequestTimeoutMs(){return this.requestTimeoutMs}isInitialized(){return this.initialized}setUser(e){this.user=e?y(e):void 0}getUser(){return this.user}clearUser(){this.user=void 0}addBreadcrumb(e){if(!e||!e.type?.trim())return;let n={timestamp:e.timestamp||new Date().toISOString(),type:e.type.trim().slice(0,200),message:e.message?.slice(0,1e4),metadata:e.metadata?y(e.metadata):void 0};this.breadcrumbs.push(n),this.breadcrumbs.length>50&&(this.breadcrumbs=this.breadcrumbs.slice(-50))}getBreadcrumbs(){return this.breadcrumbs.map(e=>l(e))}clearBreadcrumbs(){this.breadcrumbs=[]}getContext(){return{environment:this.environment,release:this.release,user:l(this.user),breadcrumbs:this.getBreadcrumbs()}}isIngestUrl(e){if(!e)return!1;try{let n=new URL(this.endpoint),r=new URL(this.apmEndpoint),s=new URL(e,n.origin),o=s.origin,i=s.pathname.replace(/\/$/,""),u=n.pathname.replace(/\/$/,""),d=r.pathname.replace(/\/$/,"");return o===n.origin&&i===u||o===r.origin&&i===d}catch{return e.includes(this.endpoint)||e.includes(this.apmEndpoint)}}createRequestId(){return b("req")}createTraceId(){return b("trace")}createSpanId(){return b("span")}captureRequest(e){let n=v(e.method)||"GET",r=String(e.url||"").trim();if(!r)return Promise.resolve();let s=R(e.statusCode)??0,o=S(e.durationMs)??0,i={...e.metadata||{},...e.route!==void 0?{route:e.route}:{},...e.dbQueriesCount!==void 0?{dbQueriesCount:e.dbQueriesCount}:{},...e.slowQueryMs!==void 0?{slowQueryMs:e.slowQueryMs}:{},timestamp:new Date().toISOString()};return this.sendApm({type:"request_metric",method:n,url:r,statusCode:s,durationMs:o,browser:e.userAgent,requestId:e.requestId,traceId:e.traceId,spanId:e.spanId,environment:this.environment,release:this.release,user:l(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:l(i)})}captureDbQuery(e){let n=String(e.operation||"").trim();if(!n)return Promise.resolve();let r=S(e.durationMs)??0,s={...e.metadata||{},operation:n,collection:e.collection||"Unknown",durationMs:r,timestamp:new Date().toISOString()};return this.sendApm({type:"db_query",message:`Database operation: ${n}`,url:e.url,method:"DB",durationMs:r,requestId:e.requestId,traceId:e.traceId,spanId:e.spanId,environment:this.environment,release:this.release,user:l(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:l(s)})}captureMetric(e,n,r={},s={}){let o=N(e);if(!o)return Promise.resolve();let i;try{i=O(n)}catch{return Promise.resolve()}return this.sendApm({type:"custom_metric",metricName:o,metricValue:i,requestId:s.requestId,traceId:s.traceId,spanId:s.spanId,environment:this.environment,release:this.release,user:l(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:l({...r,timestamp:new Date().toISOString()})})}captureCustomMetric(e){return this.captureMetric(e.metricName,e.metricValue,e.metadata||{},{requestId:e.requestId,traceId:e.traceId,spanId:e.spanId})}captureError(e,n={}){let r=e?.message?.trim()||"Erro desconhecido",s={...n},o=v(s.method),i=R(s.statusCode??s.status),u=s.url!==void 0&&s.url!==null?String(s.url).trim():void 0,d=typeof s.requestId=="string"&&s.requestId.trim()||void 0,p=typeof s.traceId=="string"&&s.traceId.trim()||void 0,f=typeof s.spanId=="string"&&s.spanId.trim()||void 0;return delete s.method,delete s.statusCode,delete s.status,delete s.requestId,delete s.traceId,delete s.spanId,delete s.durationMs,this.sendError({type:"error",message:r,stackTrace:e?.stack||"",...u?{url:u}:{},...o?{method:o}:{},...i!==void 0?{statusCode:i}:{},...d?{requestId:d}:{},...p?{traceId:p}:{},...f?{spanId:f}:{},environment:this.environment,release:this.release,user:l(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:l({...s,timestamp:new Date().toISOString()})})}captureException(e,n={}){let r=e instanceof Error?e:new Error(w(e));return this.captureError(r,n)}captureMessage(e,n={}){let r=String(e||"").trim();return r?this.sendApm({type:"message",message:r.slice(0,1e4),environment:this.environment,release:this.release,user:l(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:l({...n,timestamp:new Date().toISOString()})}):Promise.resolve()}async flush(){if(!(this.sendQueue.length===0&&!this.isProcessingQueue))return new Promise(e=>{this.flushResolvers.push(e),this.processQueue()})}attachGlobalListeners(){if(this.listenersAttached||typeof globalThis>"u")return;this.listenersAttached=!0;let e=globalThis;if(typeof e.ErrorUtils<"u"){let n=e.ErrorUtils.getGlobalHandler?.();e.ErrorUtils.setGlobalHandler?.((r,s)=>{this.captureError(r,{platform:"react-native",fatal:s}).finally(()=>{typeof n=="function"&&n(r,s)})})}if(typeof window<"u"&&typeof window.addEventListener=="function"){window.addEventListener("error",n=>{let r=n.error instanceof Error?n.error:new Error(n.message||"Erro global");this.captureError(r,{url:n.filename||window.location.href,line:n.lineno,column:n.colno,platform:"browser"})}),window.addEventListener("unhandledrejection",n=>{let r=n.reason instanceof Error?n.reason:new Error(w(n.reason));this.captureError(r,{platform:"browser",url:window.location.href})});return}typeof process<"u"&&typeof process.on=="function"&&(process.on("uncaughtException",n=>{this.captureError(n,{platform:"node"})}),process.on("unhandledRejection",n=>{this.captureException(n,{platform:"node"})}))}sendError(e){return this.enqueue(this.endpoint,e)}sendApm(e){return this.enqueue(this.apmEndpoint,e)}enqueue(e,n){if(!this.initialized||!this.apiKey)return Promise.resolve();let r={id:b("event"),endpoint:e,payload:l(n),resolve:()=>{}};return new Promise(s=>{r.resolve=s,this.sendQueue.push(r),this.processQueue()})}async processQueue(){if(!(this.isProcessingQueue||!this.initialized||!this.apiKey)){this.isProcessingQueue=!0;try{for(;this.sendQueue.length>0;){let e=this.sendQueue.shift();if(e)try{await this.sendToEndpoint(e.endpoint,e.payload)}finally{e.resolve()}}}finally{if(this.isProcessingQueue=!1,this.sendQueue.length===0){let e=this.flushResolvers.splice(0);for(let n of e)n()}this.sendQueue.length>0&&this.processQueue()}}}async sendToEndpoint(e,n){let r=typeof fetch=="function"?fetch:void 0;if(!r)return;let s=y(n),o,i;try{typeof AbortController<"u"&&(o=new AbortController,i=setTimeout(()=>{o?.abort()},this.requestTimeoutMs));let u=typeof window<"u"&&typeof window.document<"u",d={method:"POST",headers:{"content-type":"application/json","x-api-key":this.apiKey},body:JSON.stringify(s),signal:o?.signal};if(u&&(d.keepalive=!0),!(await r(e,d)).ok)return}catch{return}finally{i!==void 0&&clearTimeout(i)}}},U="__DEVINSPECT_AUDIT_CORE_INSTANCE__",A=typeof globalThis<"u"?globalThis:{},a=A[U]||new E;A[U]=a;async function q(t,e,n={}){let r=n.nativeFetch||(typeof fetch=="function"?fetch:void 0);if(!r)throw new Error("fetch n\xE3o est\xE1 dispon\xEDvel.");let s=typeof t=="string"?t:t instanceof URL?t.toString():String(t.url||""),o=v(e?.method||(typeof t!="string"&&!(t instanceof URL)?t.method:"GET"))||"GET";if(s&&a.isIngestUrl(s))return r(t,e);let i=a.createRequestId(),u=a.createTraceId(),d=a.createSpanId(),p=new Headers(e?.headers||(typeof t!="string"&&!(t instanceof URL)?t.headers:void 0));p.set("x-devinspector-request-id",i),p.set("x-devinspector-trace-id",u),p.set("x-devinspector-span-id",d);let f={...e,headers:p},h=typeof performance<"u"?performance.now():Date.now();try{let c=await r(t,f),m=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-h)),g=c.status>=400||m>=a.getSlowThresholdMs();return a.addBreadcrumb({type:"http",message:`${o} ${s}`,metadata:{phase:"complete",statusCode:c.status,durationMs:m,requestId:i,traceId:u,spanId:d}}),g&&a.captureRequest({method:o,url:s,statusCode:c.status,durationMs:m,userAgent:typeof navigator<"u"?navigator.userAgent:n.defaultUserAgent,requestId:i,traceId:u,spanId:d}),c}catch(c){let m=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-h));a.addBreadcrumb({type:"http_error",message:`${o} ${s}`,metadata:{phase:"error",statusCode:0,durationMs:m,requestId:i,traceId:u,spanId:d}}),a.captureRequest({method:o,url:s,statusCode:0,durationMs:m,userAgent:typeof navigator<"u"?navigator.userAgent:n.defaultUserAgent,requestId:i,traceId:u,spanId:d});let g=c instanceof Error?c:new Error(w(c));throw a.captureError(g,{url:s,method:o,type:n.errorType||"network_error",requestId:i,traceId:u,spanId:d}),c}}var D=new B;function L(t){if(!t)return{};let e={},n=["authorization","cookie","set-cookie","x-api-key","proxy-authorization"];for(let[r,s]of Object.entries(t)){let o=r.toLowerCase();e[r]=n.includes(o)?"[REDACTED]":s}return e}function I(t,e){if(!t?.headers)return;let n=e.toLowerCase(),r=t.headers,s=r[n]??r[e];if(Array.isArray(s))return s[0];if(s!=null)return String(s);for(let[o,i]of Object.entries(r))if(o.toLowerCase()===n)return Array.isArray(i)?i[0]:i?String(i):void 0}function z(t,e){return typeof e=="string"?t.startsWith(e):(e.lastIndex=0,e.test(t))}function j(t){a.init(t);let e=t.slowThresholdMs??a.getSlowThresholdMs(),n=t.ignorePaths??["/uploads","/favicon.ico"];return(r,s,o)=>{let i=r.originalUrl||r.url||"";if(i&&a.isIngestUrl(i)||n.some(m=>z(i,m)))return o();let d=typeof performance<"u"?performance.now():Date.now(),p=I(r,"x-devinspector-request-id")||a.createRequestId(),f=I(r,"x-devinspector-trace-id")||a.createTraceId(),h=I(r,"x-devinspector-span-id")||a.createSpanId();r.headers&&(r.headers["x-devinspector-request-id"]=p,r.headers["x-devinspector-trace-id"]=f,r.headers["x-devinspector-span-id"]=h);let c={req:r,queriesCount:0,slowQueries:0,requestId:p,traceId:f,spanId:h};D.run(c,()=>{a.addBreadcrumb({type:"http",message:`${r.method} ${i}`,metadata:{framework:"Node.js",phase:"start",requestId:p,traceId:f,spanId:h}}),s.on("finish",()=>{let m=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-d)),g=Number(s.statusCode)||500,k=g>=400,_=m>=e,Q=c.slowQueries>0,C=r.route?.path?`${r.baseUrl||""}${r.route.path}`:r.baseUrl||i;(k||_||Q)&&a.captureRequest({method:r.method||"GET",url:i,statusCode:g,durationMs:m,userAgent:r.headers?.["user-agent"],route:C,dbQueriesCount:c.queriesCount,slowQueryMs:c.slowQueries,requestId:p,traceId:f,spanId:h}),a.addBreadcrumb({type:"http",message:`${r.method} ${i}`,metadata:{framework:"Node.js",phase:"complete",statusCode:g,durationMs:m,requestId:p,traceId:f,spanId:h,dbQueriesCount:c.queriesCount,slowQueryMs:c.slowQueries}})}),o()})}}async function H(t,e){return q(t,e,{errorType:"node_network_error"})}async function K(t,e,n){let r=D.getStore(),s=n??a.getSlowThresholdMs(),o=typeof performance<"u"?performance.now():Date.now();r&&r.queriesCount++;try{let i=await e(),u=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-o));if(u>=s){r&&(r.slowQueries=Math.max(r.slowQueries,u));let d=r?.req?.originalUrl||r?.req?.url||"";a.addBreadcrumb({type:"db_query",message:`Slow Query detectada em [${t}]`,metadata:{operation:t,durationMs:u,requestId:r?.requestId,traceId:r?.traceId,spanId:r?.spanId,url:d}}),a.captureDbQuery({operation:t,collection:"Unknown",durationMs:u,url:d,requestId:r?.requestId,traceId:r?.traceId,spanId:r?.spanId,metadata:{operation:t,operationName:t}})}return i}catch(i){let u=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-o)),d=r?.req?.originalUrl||r?.req?.url||"";throw a.captureDbQuery({operation:t,collection:"Unknown",durationMs:u,url:d,requestId:r?.requestId,traceId:r?.traceId,spanId:r?.spanId,metadata:{operation:t,operationName:t,failed:!0,error:i instanceof Error?i.message:String(i)}}),a.captureException(i instanceof Error?i:new Error(String(i)),{operationName:t,durationMs:u,url:d,method:"DB",requestId:r?.requestId,traceId:r?.traceId,spanId:r?.spanId}),a.addBreadcrumb({type:"db_query_error",message:`Erro na opera\xE7\xE3o [${t}]`,metadata:{operation:t,operationName:t,durationMs:u,requestId:r?.requestId,traceId:r?.traceId,spanId:r?.spanId}}),i}}function X(){return(t,e,n,r)=>{let s=I(e,"x-devinspector-request-id"),o=I(e,"x-devinspector-trace-id"),i=I(e,"x-devinspector-span-id");a.captureException(t instanceof Error?t:new Error(String(t)),{url:e.originalUrl||e.url,method:e.method,statusCode:n?.statusCode||500,headers:L(e.headers||{}),requestId:s,traceId:o,spanId:i}),a.addBreadcrumb({type:"http_error",message:`${e.method||"HTTP"} ${e.originalUrl||e.url||""}`,metadata:{framework:"Node.js",statusCode:n?.statusCode||500,requestId:s,traceId:o,spanId:i}}),r(t)}}export{a as audit,H as auditFetch,K as auditOperation,j as devInspector,X as devInspectorErrorHandler};
1
+ import{AsyncLocalStorage as K}from"async_hooks";var P="https://api.devinspector.com.br/api/ingest/track",x="https://api.devinspector.com.br/api/ingest/apm";var G=["password","senha","token","authorization","auth","bearer","credit_card","creditcard","cartao","cvv","cpf","rg","secret","private_key","privatekey"];function F(t){let e=t.toLowerCase().replace(/[\s-]/g,"_");return G.some(s=>e.includes(s))}function y(t,e=0,s=new WeakSet){if(t==null)return t;if(typeof t=="string")return t.length>1e4?`${t.slice(0,1e4)}...[truncated]`:t;if(typeof t=="number"||typeof t=="boolean")return t;if(typeof t=="bigint")return String(t);if(typeof t=="function")return"[Function]";if(e>=6)return"[MaxDepth]";if(typeof t!="object")return String(t);if(s.has(t))return"[Circular]";s.add(t);try{if(Array.isArray(t))return t.slice(0,100).map(o=>y(o,e+1,s));let r=t,n={};for(let[o,i]of Object.entries(r)){if(F(o)){n[o]="[REDACTED]";continue}n[o]=y(i,e+1,s)}return n}finally{s.delete(t)}}function E(t){if(t instanceof Error)return t.message||"Erro desconhecido";if(typeof t=="string")return t||"Erro desconhecido";try{let e=y(t);return typeof e=="string"?e||"Erro desconhecido":JSON.stringify(e)||"Erro desconhecido"}catch{return"Erro desconhecido"}}function T(t){return t&&String(t).trim().toUpperCase()||void 0}function C(t){if(!(typeof t!="number"||!Number.isFinite(t)))return Math.round(t)}function v(t){if(!(typeof t!="number"||!Number.isFinite(t)))return Math.max(0,Math.round(t))}function N(t){return t==null?void 0:String(t).trim().slice(0,100)||void 0}function j(t){return String(t??"").trim().slice(0,200)}function H(t){let e=Number(t);if(!Number.isFinite(e))throw new Error("metricValue deve ser um n\xFAmero finito.");return e}function b(t){let e=typeof globalThis<"u"?globalThis.crypto:void 0;return e&&typeof e.randomUUID=="function"?`${t}-${e.randomUUID()}`:`${t}-${Date.now()}-${Math.random().toString(36).slice(2,12)}`}function m(t){return y(t)}var M=class{constructor(){this.apiKey="";this.endpoint=P;this.apmEndpoint=x;this.environment="production";this.slowThresholdMs=300;this.requestTimeoutMs=1e4;this.breadcrumbs=[];this.initialized=!1;this.listenersAttached=!1;this.sendQueue=[];this.isProcessingQueue=!1;this.flushResolvers=[]}init(e){if(!e?.apiKey?.trim())throw new Error("DevInspector: apiKey \xE9 obrigat\xF3rio.");this.apiKey=e.apiKey.trim(),this.endpoint=e.endpoint?.trim()||P,this.apmEndpoint=e.apmEndpoint?.trim()||x,this.environment=e.environment?.trim()||"production",this.release=e.release?.trim()||void 0,this.slowThresholdMs=typeof e.slowThresholdMs=="number"&&Number.isFinite(e.slowThresholdMs)&&e.slowThresholdMs>=0?Math.round(e.slowThresholdMs):300,this.requestTimeoutMs=typeof e.requestTimeoutMs=="number"&&Number.isFinite(e.requestTimeoutMs)&&e.requestTimeoutMs>0?Math.round(e.requestTimeoutMs):1e4,this.initialized=!0,this.attachGlobalListeners(),this.processQueue()}getApiKey(){return this.apiKey}getEndpoint(){return this.endpoint}getApmEndpoint(){return this.apmEndpoint}getEnvironment(){return this.environment}getRelease(){return this.release}getSlowThresholdMs(){return this.slowThresholdMs}getRequestTimeoutMs(){return this.requestTimeoutMs}isInitialized(){return this.initialized}setUser(e){this.user=e?y(e):void 0}getUser(){return this.user}clearUser(){this.user=void 0}addBreadcrumb(e){if(!e||!e.type?.trim())return;let s={timestamp:e.timestamp||new Date().toISOString(),type:e.type.trim().slice(0,200),message:e.message?.slice(0,1e4),metadata:e.metadata?y(e.metadata):void 0};this.breadcrumbs.push(s),this.breadcrumbs.length>50&&(this.breadcrumbs=this.breadcrumbs.slice(-50))}getBreadcrumbs(){return this.breadcrumbs.map(e=>m(e))}clearBreadcrumbs(){this.breadcrumbs=[]}getContext(){return{environment:this.environment,release:this.release,user:m(this.user),breadcrumbs:this.getBreadcrumbs()}}isIngestUrl(e){if(!e)return!1;try{let s=new URL(this.endpoint),r=new URL(this.apmEndpoint),n=new URL(e,s.origin),o=n.origin,i=n.pathname.replace(/\/$/,""),a=s.pathname.replace(/\/$/,""),d=r.pathname.replace(/\/$/,"");return o===s.origin&&i===a||o===r.origin&&i===d}catch{return e.includes(this.endpoint)||e.includes(this.apmEndpoint)}}createRequestId(){return b("req")}createTraceId(){return b("trace")}createSpanId(){return b("span")}captureRequest(e){let s=T(e.method)||"GET",r=String(e.url||"").trim();if(!r)return Promise.resolve();let n=C(e.statusCode)??0,o=v(e.durationMs)??0,i=N(e.ip),a={...e.metadata||{},timestamp:new Date().toISOString()};return this.sendApm({type:"request_metric",method:s,url:r,statusCode:n,durationMs:o,...e.userAgent?{userAgent:e.userAgent}:{},...e.route!==void 0?{route:e.route}:{},...i?{ip:i}:{},...e.dbQueriesCount!==void 0?{dbQueriesCount:e.dbQueriesCount}:{},...e.slowQueryMs!==void 0?{slowQueryMs:e.slowQueryMs}:{},...e.requestId?{requestId:e.requestId}:{},...e.traceId?{traceId:e.traceId}:{},...e.spanId?{spanId:e.spanId}:{},environment:this.environment,release:this.release,user:m(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:m(a)})}captureDbQuery(e){let s=String(e.operation||"").trim();if(!s)return Promise.resolve();let r=v(e.durationMs)??0,n={...e.metadata||{},operation:s,collection:e.collection||"Unknown",durationMs:r,timestamp:new Date().toISOString()};return this.sendApm({type:"db_query",message:`Database operation: ${s}`,url:e.url,method:"DB",durationMs:r,requestId:e.requestId,traceId:e.traceId,spanId:e.spanId,environment:this.environment,release:this.release,user:m(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:m(n)})}captureMetric(e,s,r={},n={}){let o=j(e);if(!o)return Promise.resolve();let i;try{i=H(s)}catch{return Promise.resolve()}return this.sendApm({type:"custom_metric",metricName:o,metricValue:i,requestId:n.requestId,traceId:n.traceId,spanId:n.spanId,environment:this.environment,release:this.release,user:m(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:m({...r,timestamp:new Date().toISOString()})})}captureCustomMetric(e){return this.captureMetric(e.metricName,e.metricValue,e.metadata||{},{requestId:e.requestId,traceId:e.traceId,spanId:e.spanId})}captureError(e,s={}){let r=e?.message?.trim()||"Erro desconhecido",n={...s},o=T(n.method),i=C(n.statusCode??n.status),a=v(n.durationMs),d=N(n.ip),p=n.url!==void 0&&n.url!==null?String(n.url).trim():void 0,f=typeof n.requestId=="string"&&n.requestId.trim()||void 0,h=typeof n.traceId=="string"&&n.traceId.trim()||void 0,l=typeof n.spanId=="string"&&n.spanId.trim()||void 0;return delete n.method,delete n.statusCode,delete n.status,delete n.durationMs,delete n.ip,delete n.requestId,delete n.traceId,delete n.spanId,this.sendError({type:"error",message:r,stackTrace:e?.stack||"",...p?{url:p}:{},...o?{method:o}:{},...i!==void 0?{statusCode:i}:{},...a!==void 0?{durationMs:a}:{},...d?{ip:d}:{},...f?{requestId:f}:{},...h?{traceId:h}:{},...l?{spanId:l}:{},environment:this.environment,release:this.release,user:m(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:m({...n,timestamp:new Date().toISOString()})})}captureException(e,s={}){let r=e instanceof Error?e:new Error(E(e));return this.captureError(r,s)}captureMessage(e,s={}){let r=String(e||"").trim();return r?this.sendApm({type:"message",message:r.slice(0,1e4),environment:this.environment,release:this.release,user:m(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:m({...s,timestamp:new Date().toISOString()})}):Promise.resolve()}async flush(){if(!(this.sendQueue.length===0&&!this.isProcessingQueue))return new Promise(e=>{this.flushResolvers.push(e),this.processQueue()})}attachGlobalListeners(){if(this.listenersAttached||typeof globalThis>"u")return;this.listenersAttached=!0;let e=globalThis;if(typeof e.ErrorUtils<"u"){let s=e.ErrorUtils.getGlobalHandler?.();e.ErrorUtils.setGlobalHandler?.((r,n)=>{this.captureError(r,{platform:"react-native",fatal:n}).finally(()=>{typeof s=="function"&&s(r,n)})})}if(typeof window<"u"&&typeof window.addEventListener=="function"){window.addEventListener("error",s=>{let r=s.error instanceof Error?s.error:new Error(s.message||"Erro global");this.captureError(r,{url:s.filename||window.location.href,line:s.lineno,column:s.colno,platform:"browser"})}),window.addEventListener("unhandledrejection",s=>{let r=s.reason instanceof Error?s.reason:new Error(E(s.reason));this.captureError(r,{platform:"browser",url:window.location.href})});return}typeof process<"u"&&typeof process.on=="function"&&(process.on("uncaughtException",s=>{this.captureError(s,{platform:"node"})}),process.on("unhandledRejection",s=>{this.captureException(s,{platform:"node"})}))}sendError(e){return this.enqueue(this.endpoint,e)}sendApm(e){return this.enqueue(this.apmEndpoint,e)}enqueue(e,s){if(!this.initialized||!this.apiKey)return Promise.resolve();let r={id:b("event"),endpoint:e,payload:m(s),resolve:()=>{}};return new Promise(n=>{r.resolve=n,this.sendQueue.push(r),this.processQueue()})}async processQueue(){if(!(this.isProcessingQueue||!this.initialized||!this.apiKey)){this.isProcessingQueue=!0;try{for(;this.sendQueue.length>0;){let e=this.sendQueue.shift();if(e)try{await this.sendToEndpoint(e.endpoint,e.payload)}finally{e.resolve()}}}finally{if(this.isProcessingQueue=!1,this.sendQueue.length===0){let e=this.flushResolvers.splice(0);for(let s of e)s()}this.sendQueue.length>0&&this.processQueue()}}}async sendToEndpoint(e,s){let r=typeof fetch=="function"?fetch:void 0;if(!r)return;let n=y(s),o,i;try{typeof AbortController<"u"&&(o=new AbortController,i=setTimeout(()=>{o?.abort()},this.requestTimeoutMs));let a=typeof window<"u"&&typeof window.document<"u",d={method:"POST",headers:{"content-type":"application/json","x-api-key":this.apiKey},body:JSON.stringify(n),signal:o?.signal};if(a&&(d.keepalive=!0),!(await r(e,d)).ok)return}catch{return}finally{i!==void 0&&clearTimeout(i)}}},O="__DEVINSPECT_AUDIT_CORE_INSTANCE__",B=typeof globalThis<"u"?globalThis:{},u=B[O]||new M;B[O]=u;async function L(t,e,s={}){let r=s.nativeFetch||(typeof fetch=="function"?fetch:void 0);if(!r)throw new Error("fetch n\xE3o est\xE1 dispon\xEDvel.");let n=typeof t=="string"?t:t instanceof URL?t.toString():String(t.url||""),o=T(e?.method||(typeof t!="string"&&!(t instanceof URL)?t.method:"GET"))||"GET";if(n&&u.isIngestUrl(n))return r(t,e);let i=u.createRequestId(),a=u.createTraceId(),d=u.createSpanId(),p=new Headers(e?.headers||(typeof t!="string"&&!(t instanceof URL)?t.headers:void 0));p.set("x-devinspector-request-id",i),p.set("x-devinspector-trace-id",a),p.set("x-devinspector-span-id",d);let f={...e,headers:p},h=typeof performance<"u"?performance.now():Date.now();try{let l=await r(t,f),c=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-h)),g=l.status>=400||c>=u.getSlowThresholdMs();return u.addBreadcrumb({type:"http",message:`${o} ${n}`,metadata:{phase:"complete",statusCode:l.status,durationMs:c,requestId:i,traceId:a,spanId:d}}),g&&u.captureRequest({method:o,url:n,statusCode:l.status,durationMs:c,userAgent:typeof navigator<"u"?navigator.userAgent:s.defaultUserAgent,requestId:i,traceId:a,spanId:d}),l}catch(l){let c=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-h));u.addBreadcrumb({type:"http_error",message:`${o} ${n}`,metadata:{phase:"error",statusCode:0,durationMs:c,requestId:i,traceId:a,spanId:d}}),u.captureRequest({method:o,url:n,statusCode:0,durationMs:c,userAgent:typeof navigator<"u"?navigator.userAgent:s.defaultUserAgent,requestId:i,traceId:a,spanId:d});let g=l instanceof Error?l:new Error(E(l));throw u.captureError(g,{url:n,method:o,type:s.errorType||"network_error",requestId:i,traceId:a,spanId:d}),l}}var z=new K;function X(t){if(!t)return{};let e={},s=new Set(["authorization","cookie","set-cookie","x-api-key","proxy-authorization"]);for(let[r,n]of Object.entries(t)){let o=r.toLowerCase();e[r]=s.has(o)?"[REDACTED]":n}return e}function I(t,e){if(!t?.headers)return;let s=e.toLowerCase(),r=t.headers,n=r[s]??r[e];if(Array.isArray(n))return n.length>0?String(n[0]):void 0;if(n!=null)return String(n);for(let[o,i]of Object.entries(r))if(o.toLowerCase()===s)return Array.isArray(i)?i.length>0?String(i[0]):void 0:i!=null?String(i):void 0}function $(t){if(t){if(typeof t.ip=="string"&&t.ip.trim())return t.ip.trim();if(typeof t.socket?.remoteAddress=="string"&&t.socket.remoteAddress.trim())return t.socket.remoteAddress.trim();if(typeof t.connection?.remoteAddress=="string"&&t.connection.remoteAddress.trim())return t.connection.remoteAddress.trim()}}function V(t,e){return typeof e=="string"?t.startsWith(e):(e.lastIndex=0,e.test(t))}function Z(t){u.init(t);let e=t.slowThresholdMs??u.getSlowThresholdMs(),s=t.ignorePaths??["/uploads","/favicon.ico"];return(r,n,o)=>{let i=r?.originalUrl||r?.url||"";if(i&&u.isIngestUrl(i)||s.some(g=>V(i,g)))return o();let d=Date.now(),p=I(r,"x-devinspector-request-id")||u.createRequestId(),f=I(r,"x-devinspector-trace-id")||u.createTraceId(),h=I(r,"x-devinspector-span-id")||u.createSpanId(),l=$(r);r?.headers&&(r.headers["x-devinspector-request-id"]=p,r.headers["x-devinspector-trace-id"]=f,r.headers["x-devinspector-span-id"]=h);let c={req:r,queriesCount:0,slowQueries:0,slowestQueryMs:0,requestId:p,traceId:f,spanId:h,ip:l};return z.run(c,()=>{u.addBreadcrumb({type:"http",message:`${r?.method||"HTTP"} ${i}`,metadata:{framework:"Node.js",phase:"start",requestId:p,traceId:f,spanId:h,ip:c.ip}}),n.on("finish",()=>{let g=Math.max(0,Date.now()-d),w=typeof n?.statusCode=="number"?n.statusCode:0,S=w>=400,R=g>=e,A=c.slowQueries>0,U=typeof r?.route?.path=="string"?r.route.path:void 0,k=typeof r?.baseUrl=="string"?r.baseUrl:"",D=U?`${k}${U}`:k||i,q=c.queriesCount,_=c.slowQueries,Q=c.slowestQueryMs;u.captureRequest({method:r?.method||"GET",url:i,statusCode:w,durationMs:g,ip:c.ip,userAgent:r?.headers?.["user-agent"],route:D,dbQueriesCount:q,slowQueryMs:Q,requestId:c.requestId,traceId:c.traceId,spanId:c.spanId,metadata:{framework:"Node.js",isError:S,isSlow:R,hasSlowQuery:A,slowQueries:_}}),u.addBreadcrumb({type:"http",message:`${r?.method||"HTTP"} ${i}`,metadata:{framework:"Node.js",phase:"complete",statusCode:w,durationMs:g,requestId:c.requestId,traceId:c.traceId,spanId:c.spanId,ip:c.ip,route:D,dbQueriesCount:q,slowQueries:_,slowQueryMs:Q,isError:S,isSlow:R,hasSlowQuery:A}})}),o()})}}async function ee(t,e){return L(t,e,{errorType:"node_network_error"})}async function te(t,e,s){let r=z.getStore(),n=s??u.getSlowThresholdMs(),o=Date.now();r&&r.queriesCount++;let i=r?.req?.originalUrl||r?.req?.url||"";try{let a=await e(),d=Math.max(0,Date.now()-o),p=d>=n;return r&&p&&(r.slowQueries++,r.slowestQueryMs=Math.max(r.slowestQueryMs,d)),u.captureDbQuery({operation:t,collection:"Unknown",durationMs:d,url:i,requestId:r?.requestId,traceId:r?.traceId,spanId:r?.spanId,metadata:{operation:t,operationName:t,slow:p,thresholdMs:n,ip:r?.ip}}),p&&u.addBreadcrumb({type:"db_query",message:`Slow Query detectada em [${t}]`,metadata:{operation:t,operationName:t,durationMs:d,thresholdMs:n,requestId:r?.requestId,traceId:r?.traceId,spanId:r?.spanId,url:i,ip:r?.ip}}),a}catch(a){let d=Math.max(0,Date.now()-o);throw u.captureDbQuery({operation:t,collection:"Unknown",durationMs:d,url:i,requestId:r?.requestId,traceId:r?.traceId,spanId:r?.spanId,metadata:{operation:t,operationName:t,failed:!0,ip:r?.ip,error:a instanceof Error?a.message:String(a)}}),u.captureException(a instanceof Error?a:new Error(String(a)),{operationName:t,durationMs:d,url:i,method:"DB",ip:r?.ip,requestId:r?.requestId,traceId:r?.traceId,spanId:r?.spanId}),u.addBreadcrumb({type:"db_query_error",message:`Erro na opera\xE7\xE3o [${t}]`,metadata:{operation:t,operationName:t,durationMs:d,requestId:r?.requestId,traceId:r?.traceId,spanId:r?.spanId,ip:r?.ip,error:a instanceof Error?a.message:String(a)}}),a}}function re(){return async(t,e,s,r)=>{let n=I(e,"x-devinspector-request-id"),o=I(e,"x-devinspector-trace-id"),i=I(e,"x-devinspector-span-id"),a=$(e),d=typeof s?.statusCode=="number"&&s.statusCode>=400?s.statusCode:500,p=t instanceof Error?t:new Error(typeof t=="string"?t:String(t));u.addBreadcrumb({type:"http_error",message:`${e?.method||"HTTP"} ${e?.originalUrl||e?.url||""}`,metadata:{framework:"Node.js",statusCode:d,requestId:n,traceId:o,spanId:i,ip:a}});try{await u.captureException(p,{source:"express",framework:"Node.js",url:e?.originalUrl||e?.url,method:e?.method,statusCode:d,headers:X(e?.headers||{}),ip:a,requestId:n,traceId:o,spanId:i}),await u.flush()}catch(f){console.error("[DevInspector] Falha ao capturar erro do Express:",f)}return r(p)}}export{u as audit,ee as auditFetch,te as auditOperation,Z as devInspector,re as devInspectorErrorHandler};
@@ -1 +1 @@
1
- "use strict";var E=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var P=Object.getOwnPropertyNames;var N=Object.prototype.hasOwnProperty;var C=(r,e)=>{for(var t in e)E(r,t,{get:e[t],enumerable:!0})},O=(r,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of P(e))!N.call(r,n)&&n!==t&&E(r,n,{get:()=>e[n],enumerable:!(i=k(e,n))||i.enumerable});return r};var Q=r=>O(E({},"__esModule",{value:!0}),r);var H={};C(H,{AuditErrorBoundary:()=>I,audit:()=>a,auditFetch:()=>z,enableNetworkTracing:()=>G});module.exports=Q(H);var q=require("react");var R="https://api.devinspector.com.br/api/ingest/track",M="https://api.devinspector.com.br/api/ingest/apm";var L=["password","senha","token","authorization","auth","bearer","credit_card","creditcard","cartao","cvv","cpf","rg","secret","private_key","privatekey"];function B(r){let e=r.toLowerCase().replace(/[\s-]/g,"_");return L.some(t=>e.includes(t))}function m(r,e=0,t=new WeakSet){if(r==null)return r;if(typeof r=="string")return r.length>1e4?`${r.slice(0,1e4)}...[truncated]`:r;if(typeof r=="number"||typeof r=="boolean")return r;if(typeof r=="bigint")return String(r);if(typeof r=="function")return"[Function]";if(e>=6)return"[MaxDepth]";if(typeof r!="object")return String(r);if(t.has(r))return"[Circular]";t.add(r);try{if(Array.isArray(r))return r.slice(0,100).map(s=>m(s,e+1,t));let i=r,n={};for(let[s,o]of Object.entries(i)){if(B(s)){n[s]="[REDACTED]";continue}n[s]=m(o,e+1,t)}return n}finally{t.delete(r)}}function y(r){if(r instanceof Error)return r.message||"Erro desconhecido";if(typeof r=="string")return r||"Erro desconhecido";try{let e=m(r);return typeof e=="string"?e||"Erro desconhecido":JSON.stringify(e)||"Erro desconhecido"}catch{return"Erro desconhecido"}}function T(r){return r&&String(r).trim().toUpperCase()||void 0}function S(r){if(!(typeof r!="number"||!Number.isFinite(r)))return Math.round(r)}function A(r){if(!(typeof r!="number"||!Number.isFinite(r)))return Math.max(0,Math.round(r))}function F(r){return String(r??"").trim().slice(0,200)}function x(r){let e=Number(r);if(!Number.isFinite(e))throw new Error("metricValue deve ser um n\xFAmero finito.");return e}function g(r){let e=typeof globalThis<"u"?globalThis.crypto:void 0;return e&&typeof e.randomUUID=="function"?`${r}-${e.randomUUID()}`:`${r}-${Date.now()}-${Math.random().toString(36).slice(2,12)}`}function d(r){return m(r)}var v=class{constructor(){this.apiKey="";this.endpoint=R;this.apmEndpoint=M;this.environment="production";this.slowThresholdMs=300;this.requestTimeoutMs=1e4;this.breadcrumbs=[];this.initialized=!1;this.listenersAttached=!1;this.sendQueue=[];this.isProcessingQueue=!1;this.flushResolvers=[]}init(e){if(!e?.apiKey?.trim())throw new Error("DevInspector: apiKey \xE9 obrigat\xF3rio.");this.apiKey=e.apiKey.trim(),this.endpoint=e.endpoint?.trim()||R,this.apmEndpoint=e.apmEndpoint?.trim()||M,this.environment=e.environment?.trim()||"production",this.release=e.release?.trim()||void 0,this.slowThresholdMs=typeof e.slowThresholdMs=="number"&&Number.isFinite(e.slowThresholdMs)&&e.slowThresholdMs>=0?Math.round(e.slowThresholdMs):300,this.requestTimeoutMs=typeof e.requestTimeoutMs=="number"&&Number.isFinite(e.requestTimeoutMs)&&e.requestTimeoutMs>0?Math.round(e.requestTimeoutMs):1e4,this.initialized=!0,this.attachGlobalListeners(),this.processQueue()}getApiKey(){return this.apiKey}getEndpoint(){return this.endpoint}getApmEndpoint(){return this.apmEndpoint}getEnvironment(){return this.environment}getRelease(){return this.release}getSlowThresholdMs(){return this.slowThresholdMs}getRequestTimeoutMs(){return this.requestTimeoutMs}isInitialized(){return this.initialized}setUser(e){this.user=e?m(e):void 0}getUser(){return this.user}clearUser(){this.user=void 0}addBreadcrumb(e){if(!e||!e.type?.trim())return;let t={timestamp:e.timestamp||new Date().toISOString(),type:e.type.trim().slice(0,200),message:e.message?.slice(0,1e4),metadata:e.metadata?m(e.metadata):void 0};this.breadcrumbs.push(t),this.breadcrumbs.length>50&&(this.breadcrumbs=this.breadcrumbs.slice(-50))}getBreadcrumbs(){return this.breadcrumbs.map(e=>d(e))}clearBreadcrumbs(){this.breadcrumbs=[]}getContext(){return{environment:this.environment,release:this.release,user:d(this.user),breadcrumbs:this.getBreadcrumbs()}}isIngestUrl(e){if(!e)return!1;try{let t=new URL(this.endpoint),i=new URL(this.apmEndpoint),n=new URL(e,t.origin),s=n.origin,o=n.pathname.replace(/\/$/,""),c=t.pathname.replace(/\/$/,""),u=i.pathname.replace(/\/$/,"");return s===t.origin&&o===c||s===i.origin&&o===u}catch{return e.includes(this.endpoint)||e.includes(this.apmEndpoint)}}createRequestId(){return g("req")}createTraceId(){return g("trace")}createSpanId(){return g("span")}captureRequest(e){let t=T(e.method)||"GET",i=String(e.url||"").trim();if(!i)return Promise.resolve();let n=S(e.statusCode)??0,s=A(e.durationMs)??0,o={...e.metadata||{},...e.route!==void 0?{route:e.route}:{},...e.dbQueriesCount!==void 0?{dbQueriesCount:e.dbQueriesCount}:{},...e.slowQueryMs!==void 0?{slowQueryMs:e.slowQueryMs}:{},timestamp:new Date().toISOString()};return this.sendApm({type:"request_metric",method:t,url:i,statusCode:n,durationMs:s,browser:e.userAgent,requestId:e.requestId,traceId:e.traceId,spanId:e.spanId,environment:this.environment,release:this.release,user:d(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:d(o)})}captureDbQuery(e){let t=String(e.operation||"").trim();if(!t)return Promise.resolve();let i=A(e.durationMs)??0,n={...e.metadata||{},operation:t,collection:e.collection||"Unknown",durationMs:i,timestamp:new Date().toISOString()};return this.sendApm({type:"db_query",message:`Database operation: ${t}`,url:e.url,method:"DB",durationMs:i,requestId:e.requestId,traceId:e.traceId,spanId:e.spanId,environment:this.environment,release:this.release,user:d(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:d(n)})}captureMetric(e,t,i={},n={}){let s=F(e);if(!s)return Promise.resolve();let o;try{o=x(t)}catch{return Promise.resolve()}return this.sendApm({type:"custom_metric",metricName:s,metricValue:o,requestId:n.requestId,traceId:n.traceId,spanId:n.spanId,environment:this.environment,release:this.release,user:d(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:d({...i,timestamp:new Date().toISOString()})})}captureCustomMetric(e){return this.captureMetric(e.metricName,e.metricValue,e.metadata||{},{requestId:e.requestId,traceId:e.traceId,spanId:e.spanId})}captureError(e,t={}){let i=e?.message?.trim()||"Erro desconhecido",n={...t},s=T(n.method),o=S(n.statusCode??n.status),c=n.url!==void 0&&n.url!==null?String(n.url).trim():void 0,u=typeof n.requestId=="string"&&n.requestId.trim()||void 0,l=typeof n.traceId=="string"&&n.traceId.trim()||void 0,h=typeof n.spanId=="string"&&n.spanId.trim()||void 0;return delete n.method,delete n.statusCode,delete n.status,delete n.requestId,delete n.traceId,delete n.spanId,delete n.durationMs,this.sendError({type:"error",message:i,stackTrace:e?.stack||"",...c?{url:c}:{},...s?{method:s}:{},...o!==void 0?{statusCode:o}:{},...u?{requestId:u}:{},...l?{traceId:l}:{},...h?{spanId:h}:{},environment:this.environment,release:this.release,user:d(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:d({...n,timestamp:new Date().toISOString()})})}captureException(e,t={}){let i=e instanceof Error?e:new Error(y(e));return this.captureError(i,t)}captureMessage(e,t={}){let i=String(e||"").trim();return i?this.sendApm({type:"message",message:i.slice(0,1e4),environment:this.environment,release:this.release,user:d(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:d({...t,timestamp:new Date().toISOString()})}):Promise.resolve()}async flush(){if(!(this.sendQueue.length===0&&!this.isProcessingQueue))return new Promise(e=>{this.flushResolvers.push(e),this.processQueue()})}attachGlobalListeners(){if(this.listenersAttached||typeof globalThis>"u")return;this.listenersAttached=!0;let e=globalThis;if(typeof e.ErrorUtils<"u"){let t=e.ErrorUtils.getGlobalHandler?.();e.ErrorUtils.setGlobalHandler?.((i,n)=>{this.captureError(i,{platform:"react-native",fatal:n}).finally(()=>{typeof t=="function"&&t(i,n)})})}if(typeof window<"u"&&typeof window.addEventListener=="function"){window.addEventListener("error",t=>{let i=t.error instanceof Error?t.error:new Error(t.message||"Erro global");this.captureError(i,{url:t.filename||window.location.href,line:t.lineno,column:t.colno,platform:"browser"})}),window.addEventListener("unhandledrejection",t=>{let i=t.reason instanceof Error?t.reason:new Error(y(t.reason));this.captureError(i,{platform:"browser",url:window.location.href})});return}typeof process<"u"&&typeof process.on=="function"&&(process.on("uncaughtException",t=>{this.captureError(t,{platform:"node"})}),process.on("unhandledRejection",t=>{this.captureException(t,{platform:"node"})}))}sendError(e){return this.enqueue(this.endpoint,e)}sendApm(e){return this.enqueue(this.apmEndpoint,e)}enqueue(e,t){if(!this.initialized||!this.apiKey)return Promise.resolve();let i={id:g("event"),endpoint:e,payload:d(t),resolve:()=>{}};return new Promise(n=>{i.resolve=n,this.sendQueue.push(i),this.processQueue()})}async processQueue(){if(!(this.isProcessingQueue||!this.initialized||!this.apiKey)){this.isProcessingQueue=!0;try{for(;this.sendQueue.length>0;){let e=this.sendQueue.shift();if(e)try{await this.sendToEndpoint(e.endpoint,e.payload)}finally{e.resolve()}}}finally{if(this.isProcessingQueue=!1,this.sendQueue.length===0){let e=this.flushResolvers.splice(0);for(let t of e)t()}this.sendQueue.length>0&&this.processQueue()}}}async sendToEndpoint(e,t){let i=typeof fetch=="function"?fetch:void 0;if(!i)return;let n=m(t),s,o;try{typeof AbortController<"u"&&(s=new AbortController,o=setTimeout(()=>{s?.abort()},this.requestTimeoutMs));let c=typeof window<"u"&&typeof window.document<"u",u={method:"POST",headers:{"content-type":"application/json","x-api-key":this.apiKey},body:JSON.stringify(n),signal:s?.signal};if(c&&(u.keepalive=!0),!(await i(e,u)).ok)return}catch{return}finally{o!==void 0&&clearTimeout(o)}}},U="__DEVINSPECT_AUDIT_CORE_INSTANCE__",D=typeof globalThis<"u"?globalThis:{},a=D[U]||new v;D[U]=a;async function w(r,e,t={}){let i=t.nativeFetch||(typeof fetch=="function"?fetch:void 0);if(!i)throw new Error("fetch n\xE3o est\xE1 dispon\xEDvel.");let n=typeof r=="string"?r:r instanceof URL?r.toString():String(r.url||""),s=T(e?.method||(typeof r!="string"&&!(r instanceof URL)?r.method:"GET"))||"GET";if(n&&a.isIngestUrl(n))return i(r,e);let o=a.createRequestId(),c=a.createTraceId(),u=a.createSpanId(),l=new Headers(e?.headers||(typeof r!="string"&&!(r instanceof URL)?r.headers:void 0));l.set("x-devinspector-request-id",o),l.set("x-devinspector-trace-id",c),l.set("x-devinspector-span-id",u);let h={...e,headers:l},_=typeof performance<"u"?performance.now():Date.now();try{let p=await i(r,h),f=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-_)),b=p.status>=400||f>=a.getSlowThresholdMs();return a.addBreadcrumb({type:"http",message:`${s} ${n}`,metadata:{phase:"complete",statusCode:p.status,durationMs:f,requestId:o,traceId:c,spanId:u}}),b&&a.captureRequest({method:s,url:n,statusCode:p.status,durationMs:f,userAgent:typeof navigator<"u"?navigator.userAgent:t.defaultUserAgent,requestId:o,traceId:c,spanId:u}),p}catch(p){let f=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-_));a.addBreadcrumb({type:"http_error",message:`${s} ${n}`,metadata:{phase:"error",statusCode:0,durationMs:f,requestId:o,traceId:c,spanId:u}}),a.captureRequest({method:s,url:n,statusCode:0,durationMs:f,userAgent:typeof navigator<"u"?navigator.userAgent:t.defaultUserAgent,requestId:o,traceId:c,spanId:u});let b=p instanceof Error?p:new Error(y(p));throw a.captureError(b,{url:n,method:s,type:t.errorType||"network_error",requestId:o,traceId:c,spanId:u}),p}}var I=class extends q.Component{constructor(){super(...arguments);this.state={hasError:!1,error:null}}static getDerivedStateFromError(t){return{hasError:!0,error:t}}componentDidCatch(t,i){a.addBreadcrumb({type:"react_native_error_boundary",message:t.message||"Erro capturado pelo React Native Error Boundary",metadata:{componentStack:i.componentStack||""}}),a.captureError(t,{platform:"react-native",framework:"React Native",componentStack:i.componentStack}),this.props.onError&&this.props.onError(t,i)}render(){if(!this.state.hasError)return this.props.children;let{fallback:t}=this.props,i=this.state.error||new Error("Erro desconhecido");return typeof t=="function"?t(i):t??null}};async function z(r,e){return w(r,e,{errorType:"react_native_network_error",defaultUserAgent:"React-Native"})}function G(){if(typeof globalThis>"u")return;let r=globalThis;if(r.__DEVINSPECTOR_FETCH_PATCHED__)return;let e=globalThis.fetch;if(typeof e!="function")return;let t;try{t=Object.getOwnPropertyDescriptor(globalThis,"fetch")}catch{return}if(t&&t.get&&!t.set||t&&t.writable===!1&&!t.set)return;let i=e.bind(globalThis),n=function(s,o){return w(s,o,{errorType:"react_native_network_error",defaultUserAgent:"React-Native",nativeFetch:i})};try{t&&t.configurable?Object.defineProperty(globalThis,"fetch",{...t,value:n}):globalThis.fetch=n}catch{return}globalThis.fetch===n&&(r.__DEVINSPECTOR_NATIVE_FETCH__=i,r.__DEVINSPECTOR_FETCH_PATCHED__=!0)}0&&(module.exports={AuditErrorBoundary,audit,auditFetch,enableNetworkTracing});
1
+ "use strict";var y=Object.defineProperty;var P=Object.getOwnPropertyDescriptor;var N=Object.getOwnPropertyNames;var C=Object.prototype.hasOwnProperty;var O=(t,e)=>{for(var r in e)y(t,r,{get:e[r],enumerable:!0})},Q=(t,e,r,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of N(e))!C.call(t,n)&&n!==r&&y(t,n,{get:()=>e[n],enumerable:!(i=P(e,n))||i.enumerable});return t};var L=t=>Q(y({},"__esModule",{value:!0}),t);var V={};O(V,{AuditErrorBoundary:()=>R,audit:()=>u,auditFetch:()=>G,enableNetworkTracing:()=>H});module.exports=L(V);var k=require("react");var M="https://api.devinspector.com.br/api/ingest/track",S="https://api.devinspector.com.br/api/ingest/apm";var B=["password","senha","token","authorization","auth","bearer","credit_card","creditcard","cartao","cvv","cpf","rg","secret","private_key","privatekey"];function F(t){let e=t.toLowerCase().replace(/[\s-]/g,"_");return B.some(r=>e.includes(r))}function m(t,e=0,r=new WeakSet){if(t==null)return t;if(typeof t=="string")return t.length>1e4?`${t.slice(0,1e4)}...[truncated]`:t;if(typeof t=="number"||typeof t=="boolean")return t;if(typeof t=="bigint")return String(t);if(typeof t=="function")return"[Function]";if(e>=6)return"[MaxDepth]";if(typeof t!="object")return String(t);if(r.has(t))return"[Circular]";r.add(t);try{if(Array.isArray(t))return t.slice(0,100).map(s=>m(s,e+1,r));let i=t,n={};for(let[s,o]of Object.entries(i)){if(F(s)){n[s]="[REDACTED]";continue}n[s]=m(o,e+1,r)}return n}finally{r.delete(t)}}function v(t){if(t instanceof Error)return t.message||"Erro desconhecido";if(typeof t=="string")return t||"Erro desconhecido";try{let e=m(t);return typeof e=="string"?e||"Erro desconhecido":JSON.stringify(e)||"Erro desconhecido"}catch{return"Erro desconhecido"}}function T(t){return t&&String(t).trim().toUpperCase()||void 0}function A(t){if(!(typeof t!="number"||!Number.isFinite(t)))return Math.round(t)}function I(t){if(!(typeof t!="number"||!Number.isFinite(t)))return Math.max(0,Math.round(t))}function U(t){return t==null?void 0:String(t).trim().slice(0,100)||void 0}function x(t){return String(t??"").trim().slice(0,200)}function z(t){let e=Number(t);if(!Number.isFinite(e))throw new Error("metricValue deve ser um n\xFAmero finito.");return e}function b(t){let e=typeof globalThis<"u"?globalThis.crypto:void 0;return e&&typeof e.randomUUID=="function"?`${t}-${e.randomUUID()}`:`${t}-${Date.now()}-${Math.random().toString(36).slice(2,12)}`}function c(t){return m(t)}var w=class{constructor(){this.apiKey="";this.endpoint=M;this.apmEndpoint=S;this.environment="production";this.slowThresholdMs=300;this.requestTimeoutMs=1e4;this.breadcrumbs=[];this.initialized=!1;this.listenersAttached=!1;this.sendQueue=[];this.isProcessingQueue=!1;this.flushResolvers=[]}init(e){if(!e?.apiKey?.trim())throw new Error("DevInspector: apiKey \xE9 obrigat\xF3rio.");this.apiKey=e.apiKey.trim(),this.endpoint=e.endpoint?.trim()||M,this.apmEndpoint=e.apmEndpoint?.trim()||S,this.environment=e.environment?.trim()||"production",this.release=e.release?.trim()||void 0,this.slowThresholdMs=typeof e.slowThresholdMs=="number"&&Number.isFinite(e.slowThresholdMs)&&e.slowThresholdMs>=0?Math.round(e.slowThresholdMs):300,this.requestTimeoutMs=typeof e.requestTimeoutMs=="number"&&Number.isFinite(e.requestTimeoutMs)&&e.requestTimeoutMs>0?Math.round(e.requestTimeoutMs):1e4,this.initialized=!0,this.attachGlobalListeners(),this.processQueue()}getApiKey(){return this.apiKey}getEndpoint(){return this.endpoint}getApmEndpoint(){return this.apmEndpoint}getEnvironment(){return this.environment}getRelease(){return this.release}getSlowThresholdMs(){return this.slowThresholdMs}getRequestTimeoutMs(){return this.requestTimeoutMs}isInitialized(){return this.initialized}setUser(e){this.user=e?m(e):void 0}getUser(){return this.user}clearUser(){this.user=void 0}addBreadcrumb(e){if(!e||!e.type?.trim())return;let r={timestamp:e.timestamp||new Date().toISOString(),type:e.type.trim().slice(0,200),message:e.message?.slice(0,1e4),metadata:e.metadata?m(e.metadata):void 0};this.breadcrumbs.push(r),this.breadcrumbs.length>50&&(this.breadcrumbs=this.breadcrumbs.slice(-50))}getBreadcrumbs(){return this.breadcrumbs.map(e=>c(e))}clearBreadcrumbs(){this.breadcrumbs=[]}getContext(){return{environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs()}}isIngestUrl(e){if(!e)return!1;try{let r=new URL(this.endpoint),i=new URL(this.apmEndpoint),n=new URL(e,r.origin),s=n.origin,o=n.pathname.replace(/\/$/,""),a=r.pathname.replace(/\/$/,""),d=i.pathname.replace(/\/$/,"");return s===r.origin&&o===a||s===i.origin&&o===d}catch{return e.includes(this.endpoint)||e.includes(this.apmEndpoint)}}createRequestId(){return b("req")}createTraceId(){return b("trace")}createSpanId(){return b("span")}captureRequest(e){let r=T(e.method)||"GET",i=String(e.url||"").trim();if(!i)return Promise.resolve();let n=A(e.statusCode)??0,s=I(e.durationMs)??0,o=U(e.ip),a={...e.metadata||{},timestamp:new Date().toISOString()};return this.sendApm({type:"request_metric",method:r,url:i,statusCode:n,durationMs:s,...e.userAgent?{userAgent:e.userAgent}:{},...e.route!==void 0?{route:e.route}:{},...o?{ip:o}:{},...e.dbQueriesCount!==void 0?{dbQueriesCount:e.dbQueriesCount}:{},...e.slowQueryMs!==void 0?{slowQueryMs:e.slowQueryMs}:{},...e.requestId?{requestId:e.requestId}:{},...e.traceId?{traceId:e.traceId}:{},...e.spanId?{spanId:e.spanId}:{},environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c(a)})}captureDbQuery(e){let r=String(e.operation||"").trim();if(!r)return Promise.resolve();let i=I(e.durationMs)??0,n={...e.metadata||{},operation:r,collection:e.collection||"Unknown",durationMs:i,timestamp:new Date().toISOString()};return this.sendApm({type:"db_query",message:`Database operation: ${r}`,url:e.url,method:"DB",durationMs:i,requestId:e.requestId,traceId:e.traceId,spanId:e.spanId,environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c(n)})}captureMetric(e,r,i={},n={}){let s=x(e);if(!s)return Promise.resolve();let o;try{o=z(r)}catch{return Promise.resolve()}return this.sendApm({type:"custom_metric",metricName:s,metricValue:o,requestId:n.requestId,traceId:n.traceId,spanId:n.spanId,environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c({...i,timestamp:new Date().toISOString()})})}captureCustomMetric(e){return this.captureMetric(e.metricName,e.metricValue,e.metadata||{},{requestId:e.requestId,traceId:e.traceId,spanId:e.spanId})}captureError(e,r={}){let i=e?.message?.trim()||"Erro desconhecido",n={...r},s=T(n.method),o=A(n.statusCode??n.status),a=I(n.durationMs),d=U(n.ip),l=n.url!==void 0&&n.url!==null?String(n.url).trim():void 0,g=typeof n.requestId=="string"&&n.requestId.trim()||void 0,h=typeof n.traceId=="string"&&n.traceId.trim()||void 0,p=typeof n.spanId=="string"&&n.spanId.trim()||void 0;return delete n.method,delete n.statusCode,delete n.status,delete n.durationMs,delete n.ip,delete n.requestId,delete n.traceId,delete n.spanId,this.sendError({type:"error",message:i,stackTrace:e?.stack||"",...l?{url:l}:{},...s?{method:s}:{},...o!==void 0?{statusCode:o}:{},...a!==void 0?{durationMs:a}:{},...d?{ip:d}:{},...g?{requestId:g}:{},...h?{traceId:h}:{},...p?{spanId:p}:{},environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c({...n,timestamp:new Date().toISOString()})})}captureException(e,r={}){let i=e instanceof Error?e:new Error(v(e));return this.captureError(i,r)}captureMessage(e,r={}){let i=String(e||"").trim();return i?this.sendApm({type:"message",message:i.slice(0,1e4),environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c({...r,timestamp:new Date().toISOString()})}):Promise.resolve()}async flush(){if(!(this.sendQueue.length===0&&!this.isProcessingQueue))return new Promise(e=>{this.flushResolvers.push(e),this.processQueue()})}attachGlobalListeners(){if(this.listenersAttached||typeof globalThis>"u")return;this.listenersAttached=!0;let e=globalThis;if(typeof e.ErrorUtils<"u"){let r=e.ErrorUtils.getGlobalHandler?.();e.ErrorUtils.setGlobalHandler?.((i,n)=>{this.captureError(i,{platform:"react-native",fatal:n}).finally(()=>{typeof r=="function"&&r(i,n)})})}if(typeof window<"u"&&typeof window.addEventListener=="function"){window.addEventListener("error",r=>{let i=r.error instanceof Error?r.error:new Error(r.message||"Erro global");this.captureError(i,{url:r.filename||window.location.href,line:r.lineno,column:r.colno,platform:"browser"})}),window.addEventListener("unhandledrejection",r=>{let i=r.reason instanceof Error?r.reason:new Error(v(r.reason));this.captureError(i,{platform:"browser",url:window.location.href})});return}typeof process<"u"&&typeof process.on=="function"&&(process.on("uncaughtException",r=>{this.captureError(r,{platform:"node"})}),process.on("unhandledRejection",r=>{this.captureException(r,{platform:"node"})}))}sendError(e){return this.enqueue(this.endpoint,e)}sendApm(e){return this.enqueue(this.apmEndpoint,e)}enqueue(e,r){if(!this.initialized||!this.apiKey)return Promise.resolve();let i={id:b("event"),endpoint:e,payload:c(r),resolve:()=>{}};return new Promise(n=>{i.resolve=n,this.sendQueue.push(i),this.processQueue()})}async processQueue(){if(!(this.isProcessingQueue||!this.initialized||!this.apiKey)){this.isProcessingQueue=!0;try{for(;this.sendQueue.length>0;){let e=this.sendQueue.shift();if(e)try{await this.sendToEndpoint(e.endpoint,e.payload)}finally{e.resolve()}}}finally{if(this.isProcessingQueue=!1,this.sendQueue.length===0){let e=this.flushResolvers.splice(0);for(let r of e)r()}this.sendQueue.length>0&&this.processQueue()}}}async sendToEndpoint(e,r){let i=typeof fetch=="function"?fetch:void 0;if(!i)return;let n=m(r),s,o;try{typeof AbortController<"u"&&(s=new AbortController,o=setTimeout(()=>{s?.abort()},this.requestTimeoutMs));let a=typeof window<"u"&&typeof window.document<"u",d={method:"POST",headers:{"content-type":"application/json","x-api-key":this.apiKey},body:JSON.stringify(n),signal:s?.signal};if(a&&(d.keepalive=!0),!(await i(e,d)).ok)return}catch{return}finally{o!==void 0&&clearTimeout(o)}}},q="__DEVINSPECT_AUDIT_CORE_INSTANCE__",D=typeof globalThis<"u"?globalThis:{},u=D[q]||new w;D[q]=u;async function _(t,e,r={}){let i=r.nativeFetch||(typeof fetch=="function"?fetch:void 0);if(!i)throw new Error("fetch n\xE3o est\xE1 dispon\xEDvel.");let n=typeof t=="string"?t:t instanceof URL?t.toString():String(t.url||""),s=T(e?.method||(typeof t!="string"&&!(t instanceof URL)?t.method:"GET"))||"GET";if(n&&u.isIngestUrl(n))return i(t,e);let o=u.createRequestId(),a=u.createTraceId(),d=u.createSpanId(),l=new Headers(e?.headers||(typeof t!="string"&&!(t instanceof URL)?t.headers:void 0));l.set("x-devinspector-request-id",o),l.set("x-devinspector-trace-id",a),l.set("x-devinspector-span-id",d);let g={...e,headers:l},h=typeof performance<"u"?performance.now():Date.now();try{let p=await i(t,g),f=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-h)),E=p.status>=400||f>=u.getSlowThresholdMs();return u.addBreadcrumb({type:"http",message:`${s} ${n}`,metadata:{phase:"complete",statusCode:p.status,durationMs:f,requestId:o,traceId:a,spanId:d}}),E&&u.captureRequest({method:s,url:n,statusCode:p.status,durationMs:f,userAgent:typeof navigator<"u"?navigator.userAgent:r.defaultUserAgent,requestId:o,traceId:a,spanId:d}),p}catch(p){let f=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-h));u.addBreadcrumb({type:"http_error",message:`${s} ${n}`,metadata:{phase:"error",statusCode:0,durationMs:f,requestId:o,traceId:a,spanId:d}}),u.captureRequest({method:s,url:n,statusCode:0,durationMs:f,userAgent:typeof navigator<"u"?navigator.userAgent:r.defaultUserAgent,requestId:o,traceId:a,spanId:d});let E=p instanceof Error?p:new Error(v(p));throw u.captureError(E,{url:n,method:s,type:r.errorType||"network_error",requestId:o,traceId:a,spanId:d}),p}}var R=class extends k.Component{constructor(){super(...arguments);this.state={hasError:!1,error:null}}static getDerivedStateFromError(r){return{hasError:!0,error:r}}componentDidCatch(r,i){u.addBreadcrumb({type:"react_native_error_boundary",message:r.message||"Erro capturado pelo React Native Error Boundary",metadata:{componentStack:i.componentStack||""}}),u.captureError(r,{platform:"react-native",framework:"React Native",componentStack:i.componentStack}),this.props.onError&&this.props.onError(r,i)}render(){if(!this.state.hasError)return this.props.children;let{fallback:r}=this.props,i=this.state.error||new Error("Erro desconhecido");return typeof r=="function"?r(i):r??null}};async function G(t,e){return _(t,e,{errorType:"react_native_network_error",defaultUserAgent:"React-Native"})}function H(){if(typeof globalThis>"u")return;let t=globalThis;if(t.__DEVINSPECTOR_FETCH_PATCHED__)return;let e=globalThis.fetch;if(typeof e!="function")return;let r;try{r=Object.getOwnPropertyDescriptor(globalThis,"fetch")}catch{return}if(r&&r.get&&!r.set||r&&r.writable===!1&&!r.set)return;let i=e.bind(globalThis),n=function(s,o){return _(s,o,{errorType:"react_native_network_error",defaultUserAgent:"React-Native",nativeFetch:i})};try{r&&r.configurable?Object.defineProperty(globalThis,"fetch",{...r,value:n}):globalThis.fetch=n}catch{return}globalThis.fetch===n&&(t.__DEVINSPECTOR_NATIVE_FETCH__=i,t.__DEVINSPECTOR_FETCH_PATCHED__=!0)}0&&(module.exports={AuditErrorBoundary,audit,auditFetch,enableNetworkTracing});
@@ -1 +1 @@
1
- import{Component as N}from"react";var I="https://api.devinspector.com.br/api/ingest/track",_="https://api.devinspector.com.br/api/ingest/apm";var D=["password","senha","token","authorization","auth","bearer","credit_card","creditcard","cartao","cvv","cpf","rg","secret","private_key","privatekey"];function q(r){let e=r.toLowerCase().replace(/[\s-]/g,"_");return D.some(t=>e.includes(t))}function m(r,e=0,t=new WeakSet){if(r==null)return r;if(typeof r=="string")return r.length>1e4?`${r.slice(0,1e4)}...[truncated]`:r;if(typeof r=="number"||typeof r=="boolean")return r;if(typeof r=="bigint")return String(r);if(typeof r=="function")return"[Function]";if(e>=6)return"[MaxDepth]";if(typeof r!="object")return String(r);if(t.has(r))return"[Circular]";t.add(r);try{if(Array.isArray(r))return r.slice(0,100).map(s=>m(s,e+1,t));let i=r,n={};for(let[s,o]of Object.entries(i)){if(q(s)){n[s]="[REDACTED]";continue}n[s]=m(o,e+1,t)}return n}finally{t.delete(r)}}function E(r){if(r instanceof Error)return r.message||"Erro desconhecido";if(typeof r=="string")return r||"Erro desconhecido";try{let e=m(r);return typeof e=="string"?e||"Erro desconhecido":JSON.stringify(e)||"Erro desconhecido"}catch{return"Erro desconhecido"}}function y(r){return r&&String(r).trim().toUpperCase()||void 0}function R(r){if(!(typeof r!="number"||!Number.isFinite(r)))return Math.round(r)}function M(r){if(!(typeof r!="number"||!Number.isFinite(r)))return Math.max(0,Math.round(r))}function k(r){return String(r??"").trim().slice(0,200)}function P(r){let e=Number(r);if(!Number.isFinite(e))throw new Error("metricValue deve ser um n\xFAmero finito.");return e}function g(r){let e=typeof globalThis<"u"?globalThis.crypto:void 0;return e&&typeof e.randomUUID=="function"?`${r}-${e.randomUUID()}`:`${r}-${Date.now()}-${Math.random().toString(36).slice(2,12)}`}function d(r){return m(r)}var T=class{constructor(){this.apiKey="";this.endpoint=I;this.apmEndpoint=_;this.environment="production";this.slowThresholdMs=300;this.requestTimeoutMs=1e4;this.breadcrumbs=[];this.initialized=!1;this.listenersAttached=!1;this.sendQueue=[];this.isProcessingQueue=!1;this.flushResolvers=[]}init(e){if(!e?.apiKey?.trim())throw new Error("DevInspector: apiKey \xE9 obrigat\xF3rio.");this.apiKey=e.apiKey.trim(),this.endpoint=e.endpoint?.trim()||I,this.apmEndpoint=e.apmEndpoint?.trim()||_,this.environment=e.environment?.trim()||"production",this.release=e.release?.trim()||void 0,this.slowThresholdMs=typeof e.slowThresholdMs=="number"&&Number.isFinite(e.slowThresholdMs)&&e.slowThresholdMs>=0?Math.round(e.slowThresholdMs):300,this.requestTimeoutMs=typeof e.requestTimeoutMs=="number"&&Number.isFinite(e.requestTimeoutMs)&&e.requestTimeoutMs>0?Math.round(e.requestTimeoutMs):1e4,this.initialized=!0,this.attachGlobalListeners(),this.processQueue()}getApiKey(){return this.apiKey}getEndpoint(){return this.endpoint}getApmEndpoint(){return this.apmEndpoint}getEnvironment(){return this.environment}getRelease(){return this.release}getSlowThresholdMs(){return this.slowThresholdMs}getRequestTimeoutMs(){return this.requestTimeoutMs}isInitialized(){return this.initialized}setUser(e){this.user=e?m(e):void 0}getUser(){return this.user}clearUser(){this.user=void 0}addBreadcrumb(e){if(!e||!e.type?.trim())return;let t={timestamp:e.timestamp||new Date().toISOString(),type:e.type.trim().slice(0,200),message:e.message?.slice(0,1e4),metadata:e.metadata?m(e.metadata):void 0};this.breadcrumbs.push(t),this.breadcrumbs.length>50&&(this.breadcrumbs=this.breadcrumbs.slice(-50))}getBreadcrumbs(){return this.breadcrumbs.map(e=>d(e))}clearBreadcrumbs(){this.breadcrumbs=[]}getContext(){return{environment:this.environment,release:this.release,user:d(this.user),breadcrumbs:this.getBreadcrumbs()}}isIngestUrl(e){if(!e)return!1;try{let t=new URL(this.endpoint),i=new URL(this.apmEndpoint),n=new URL(e,t.origin),s=n.origin,o=n.pathname.replace(/\/$/,""),c=t.pathname.replace(/\/$/,""),a=i.pathname.replace(/\/$/,"");return s===t.origin&&o===c||s===i.origin&&o===a}catch{return e.includes(this.endpoint)||e.includes(this.apmEndpoint)}}createRequestId(){return g("req")}createTraceId(){return g("trace")}createSpanId(){return g("span")}captureRequest(e){let t=y(e.method)||"GET",i=String(e.url||"").trim();if(!i)return Promise.resolve();let n=R(e.statusCode)??0,s=M(e.durationMs)??0,o={...e.metadata||{},...e.route!==void 0?{route:e.route}:{},...e.dbQueriesCount!==void 0?{dbQueriesCount:e.dbQueriesCount}:{},...e.slowQueryMs!==void 0?{slowQueryMs:e.slowQueryMs}:{},timestamp:new Date().toISOString()};return this.sendApm({type:"request_metric",method:t,url:i,statusCode:n,durationMs:s,browser:e.userAgent,requestId:e.requestId,traceId:e.traceId,spanId:e.spanId,environment:this.environment,release:this.release,user:d(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:d(o)})}captureDbQuery(e){let t=String(e.operation||"").trim();if(!t)return Promise.resolve();let i=M(e.durationMs)??0,n={...e.metadata||{},operation:t,collection:e.collection||"Unknown",durationMs:i,timestamp:new Date().toISOString()};return this.sendApm({type:"db_query",message:`Database operation: ${t}`,url:e.url,method:"DB",durationMs:i,requestId:e.requestId,traceId:e.traceId,spanId:e.spanId,environment:this.environment,release:this.release,user:d(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:d(n)})}captureMetric(e,t,i={},n={}){let s=k(e);if(!s)return Promise.resolve();let o;try{o=P(t)}catch{return Promise.resolve()}return this.sendApm({type:"custom_metric",metricName:s,metricValue:o,requestId:n.requestId,traceId:n.traceId,spanId:n.spanId,environment:this.environment,release:this.release,user:d(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:d({...i,timestamp:new Date().toISOString()})})}captureCustomMetric(e){return this.captureMetric(e.metricName,e.metricValue,e.metadata||{},{requestId:e.requestId,traceId:e.traceId,spanId:e.spanId})}captureError(e,t={}){let i=e?.message?.trim()||"Erro desconhecido",n={...t},s=y(n.method),o=R(n.statusCode??n.status),c=n.url!==void 0&&n.url!==null?String(n.url).trim():void 0,a=typeof n.requestId=="string"&&n.requestId.trim()||void 0,l=typeof n.traceId=="string"&&n.traceId.trim()||void 0,h=typeof n.spanId=="string"&&n.spanId.trim()||void 0;return delete n.method,delete n.statusCode,delete n.status,delete n.requestId,delete n.traceId,delete n.spanId,delete n.durationMs,this.sendError({type:"error",message:i,stackTrace:e?.stack||"",...c?{url:c}:{},...s?{method:s}:{},...o!==void 0?{statusCode:o}:{},...a?{requestId:a}:{},...l?{traceId:l}:{},...h?{spanId:h}:{},environment:this.environment,release:this.release,user:d(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:d({...n,timestamp:new Date().toISOString()})})}captureException(e,t={}){let i=e instanceof Error?e:new Error(E(e));return this.captureError(i,t)}captureMessage(e,t={}){let i=String(e||"").trim();return i?this.sendApm({type:"message",message:i.slice(0,1e4),environment:this.environment,release:this.release,user:d(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:d({...t,timestamp:new Date().toISOString()})}):Promise.resolve()}async flush(){if(!(this.sendQueue.length===0&&!this.isProcessingQueue))return new Promise(e=>{this.flushResolvers.push(e),this.processQueue()})}attachGlobalListeners(){if(this.listenersAttached||typeof globalThis>"u")return;this.listenersAttached=!0;let e=globalThis;if(typeof e.ErrorUtils<"u"){let t=e.ErrorUtils.getGlobalHandler?.();e.ErrorUtils.setGlobalHandler?.((i,n)=>{this.captureError(i,{platform:"react-native",fatal:n}).finally(()=>{typeof t=="function"&&t(i,n)})})}if(typeof window<"u"&&typeof window.addEventListener=="function"){window.addEventListener("error",t=>{let i=t.error instanceof Error?t.error:new Error(t.message||"Erro global");this.captureError(i,{url:t.filename||window.location.href,line:t.lineno,column:t.colno,platform:"browser"})}),window.addEventListener("unhandledrejection",t=>{let i=t.reason instanceof Error?t.reason:new Error(E(t.reason));this.captureError(i,{platform:"browser",url:window.location.href})});return}typeof process<"u"&&typeof process.on=="function"&&(process.on("uncaughtException",t=>{this.captureError(t,{platform:"node"})}),process.on("unhandledRejection",t=>{this.captureException(t,{platform:"node"})}))}sendError(e){return this.enqueue(this.endpoint,e)}sendApm(e){return this.enqueue(this.apmEndpoint,e)}enqueue(e,t){if(!this.initialized||!this.apiKey)return Promise.resolve();let i={id:g("event"),endpoint:e,payload:d(t),resolve:()=>{}};return new Promise(n=>{i.resolve=n,this.sendQueue.push(i),this.processQueue()})}async processQueue(){if(!(this.isProcessingQueue||!this.initialized||!this.apiKey)){this.isProcessingQueue=!0;try{for(;this.sendQueue.length>0;){let e=this.sendQueue.shift();if(e)try{await this.sendToEndpoint(e.endpoint,e.payload)}finally{e.resolve()}}}finally{if(this.isProcessingQueue=!1,this.sendQueue.length===0){let e=this.flushResolvers.splice(0);for(let t of e)t()}this.sendQueue.length>0&&this.processQueue()}}}async sendToEndpoint(e,t){let i=typeof fetch=="function"?fetch:void 0;if(!i)return;let n=m(t),s,o;try{typeof AbortController<"u"&&(s=new AbortController,o=setTimeout(()=>{s?.abort()},this.requestTimeoutMs));let c=typeof window<"u"&&typeof window.document<"u",a={method:"POST",headers:{"content-type":"application/json","x-api-key":this.apiKey},body:JSON.stringify(n),signal:s?.signal};if(c&&(a.keepalive=!0),!(await i(e,a)).ok)return}catch{return}finally{o!==void 0&&clearTimeout(o)}}},S="__DEVINSPECT_AUDIT_CORE_INSTANCE__",A=typeof globalThis<"u"?globalThis:{},u=A[S]||new T;A[S]=u;async function v(r,e,t={}){let i=t.nativeFetch||(typeof fetch=="function"?fetch:void 0);if(!i)throw new Error("fetch n\xE3o est\xE1 dispon\xEDvel.");let n=typeof r=="string"?r:r instanceof URL?r.toString():String(r.url||""),s=y(e?.method||(typeof r!="string"&&!(r instanceof URL)?r.method:"GET"))||"GET";if(n&&u.isIngestUrl(n))return i(r,e);let o=u.createRequestId(),c=u.createTraceId(),a=u.createSpanId(),l=new Headers(e?.headers||(typeof r!="string"&&!(r instanceof URL)?r.headers:void 0));l.set("x-devinspector-request-id",o),l.set("x-devinspector-trace-id",c),l.set("x-devinspector-span-id",a);let h={...e,headers:l},w=typeof performance<"u"?performance.now():Date.now();try{let p=await i(r,h),f=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-w)),b=p.status>=400||f>=u.getSlowThresholdMs();return u.addBreadcrumb({type:"http",message:`${s} ${n}`,metadata:{phase:"complete",statusCode:p.status,durationMs:f,requestId:o,traceId:c,spanId:a}}),b&&u.captureRequest({method:s,url:n,statusCode:p.status,durationMs:f,userAgent:typeof navigator<"u"?navigator.userAgent:t.defaultUserAgent,requestId:o,traceId:c,spanId:a}),p}catch(p){let f=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-w));u.addBreadcrumb({type:"http_error",message:`${s} ${n}`,metadata:{phase:"error",statusCode:0,durationMs:f,requestId:o,traceId:c,spanId:a}}),u.captureRequest({method:s,url:n,statusCode:0,durationMs:f,userAgent:typeof navigator<"u"?navigator.userAgent:t.defaultUserAgent,requestId:o,traceId:c,spanId:a});let b=p instanceof Error?p:new Error(E(p));throw u.captureError(b,{url:n,method:s,type:t.errorType||"network_error",requestId:o,traceId:c,spanId:a}),p}}var U=class extends N{constructor(){super(...arguments);this.state={hasError:!1,error:null}}static getDerivedStateFromError(t){return{hasError:!0,error:t}}componentDidCatch(t,i){u.addBreadcrumb({type:"react_native_error_boundary",message:t.message||"Erro capturado pelo React Native Error Boundary",metadata:{componentStack:i.componentStack||""}}),u.captureError(t,{platform:"react-native",framework:"React Native",componentStack:i.componentStack}),this.props.onError&&this.props.onError(t,i)}render(){if(!this.state.hasError)return this.props.children;let{fallback:t}=this.props,i=this.state.error||new Error("Erro desconhecido");return typeof t=="function"?t(i):t??null}};async function x(r,e){return v(r,e,{errorType:"react_native_network_error",defaultUserAgent:"React-Native"})}function z(){if(typeof globalThis>"u")return;let r=globalThis;if(r.__DEVINSPECTOR_FETCH_PATCHED__)return;let e=globalThis.fetch;if(typeof e!="function")return;let t;try{t=Object.getOwnPropertyDescriptor(globalThis,"fetch")}catch{return}if(t&&t.get&&!t.set||t&&t.writable===!1&&!t.set)return;let i=e.bind(globalThis),n=function(s,o){return v(s,o,{errorType:"react_native_network_error",defaultUserAgent:"React-Native",nativeFetch:i})};try{t&&t.configurable?Object.defineProperty(globalThis,"fetch",{...t,value:n}):globalThis.fetch=n}catch{return}globalThis.fetch===n&&(r.__DEVINSPECTOR_NATIVE_FETCH__=i,r.__DEVINSPECTOR_FETCH_PATCHED__=!0)}export{U as AuditErrorBoundary,u as audit,x as auditFetch,z as enableNetworkTracing};
1
+ import{Component as C}from"react";var _="https://api.devinspector.com.br/api/ingest/track",R="https://api.devinspector.com.br/api/ingest/apm";var D=["password","senha","token","authorization","auth","bearer","credit_card","creditcard","cartao","cvv","cpf","rg","secret","private_key","privatekey"];function k(r){let e=r.toLowerCase().replace(/[\s-]/g,"_");return D.some(t=>e.includes(t))}function m(r,e=0,t=new WeakSet){if(r==null)return r;if(typeof r=="string")return r.length>1e4?`${r.slice(0,1e4)}...[truncated]`:r;if(typeof r=="number"||typeof r=="boolean")return r;if(typeof r=="bigint")return String(r);if(typeof r=="function")return"[Function]";if(e>=6)return"[MaxDepth]";if(typeof r!="object")return String(r);if(t.has(r))return"[Circular]";t.add(r);try{if(Array.isArray(r))return r.slice(0,100).map(s=>m(s,e+1,t));let i=r,n={};for(let[s,o]of Object.entries(i)){if(k(s)){n[s]="[REDACTED]";continue}n[s]=m(o,e+1,t)}return n}finally{t.delete(r)}}function I(r){if(r instanceof Error)return r.message||"Erro desconhecido";if(typeof r=="string")return r||"Erro desconhecido";try{let e=m(r);return typeof e=="string"?e||"Erro desconhecido":JSON.stringify(e)||"Erro desconhecido"}catch{return"Erro desconhecido"}}function v(r){return r&&String(r).trim().toUpperCase()||void 0}function M(r){if(!(typeof r!="number"||!Number.isFinite(r)))return Math.round(r)}function y(r){if(!(typeof r!="number"||!Number.isFinite(r)))return Math.max(0,Math.round(r))}function S(r){return r==null?void 0:String(r).trim().slice(0,100)||void 0}function P(r){return String(r??"").trim().slice(0,200)}function N(r){let e=Number(r);if(!Number.isFinite(e))throw new Error("metricValue deve ser um n\xFAmero finito.");return e}function b(r){let e=typeof globalThis<"u"?globalThis.crypto:void 0;return e&&typeof e.randomUUID=="function"?`${r}-${e.randomUUID()}`:`${r}-${Date.now()}-${Math.random().toString(36).slice(2,12)}`}function c(r){return m(r)}var T=class{constructor(){this.apiKey="";this.endpoint=_;this.apmEndpoint=R;this.environment="production";this.slowThresholdMs=300;this.requestTimeoutMs=1e4;this.breadcrumbs=[];this.initialized=!1;this.listenersAttached=!1;this.sendQueue=[];this.isProcessingQueue=!1;this.flushResolvers=[]}init(e){if(!e?.apiKey?.trim())throw new Error("DevInspector: apiKey \xE9 obrigat\xF3rio.");this.apiKey=e.apiKey.trim(),this.endpoint=e.endpoint?.trim()||_,this.apmEndpoint=e.apmEndpoint?.trim()||R,this.environment=e.environment?.trim()||"production",this.release=e.release?.trim()||void 0,this.slowThresholdMs=typeof e.slowThresholdMs=="number"&&Number.isFinite(e.slowThresholdMs)&&e.slowThresholdMs>=0?Math.round(e.slowThresholdMs):300,this.requestTimeoutMs=typeof e.requestTimeoutMs=="number"&&Number.isFinite(e.requestTimeoutMs)&&e.requestTimeoutMs>0?Math.round(e.requestTimeoutMs):1e4,this.initialized=!0,this.attachGlobalListeners(),this.processQueue()}getApiKey(){return this.apiKey}getEndpoint(){return this.endpoint}getApmEndpoint(){return this.apmEndpoint}getEnvironment(){return this.environment}getRelease(){return this.release}getSlowThresholdMs(){return this.slowThresholdMs}getRequestTimeoutMs(){return this.requestTimeoutMs}isInitialized(){return this.initialized}setUser(e){this.user=e?m(e):void 0}getUser(){return this.user}clearUser(){this.user=void 0}addBreadcrumb(e){if(!e||!e.type?.trim())return;let t={timestamp:e.timestamp||new Date().toISOString(),type:e.type.trim().slice(0,200),message:e.message?.slice(0,1e4),metadata:e.metadata?m(e.metadata):void 0};this.breadcrumbs.push(t),this.breadcrumbs.length>50&&(this.breadcrumbs=this.breadcrumbs.slice(-50))}getBreadcrumbs(){return this.breadcrumbs.map(e=>c(e))}clearBreadcrumbs(){this.breadcrumbs=[]}getContext(){return{environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs()}}isIngestUrl(e){if(!e)return!1;try{let t=new URL(this.endpoint),i=new URL(this.apmEndpoint),n=new URL(e,t.origin),s=n.origin,o=n.pathname.replace(/\/$/,""),a=t.pathname.replace(/\/$/,""),u=i.pathname.replace(/\/$/,"");return s===t.origin&&o===a||s===i.origin&&o===u}catch{return e.includes(this.endpoint)||e.includes(this.apmEndpoint)}}createRequestId(){return b("req")}createTraceId(){return b("trace")}createSpanId(){return b("span")}captureRequest(e){let t=v(e.method)||"GET",i=String(e.url||"").trim();if(!i)return Promise.resolve();let n=M(e.statusCode)??0,s=y(e.durationMs)??0,o=S(e.ip),a={...e.metadata||{},timestamp:new Date().toISOString()};return this.sendApm({type:"request_metric",method:t,url:i,statusCode:n,durationMs:s,...e.userAgent?{userAgent:e.userAgent}:{},...e.route!==void 0?{route:e.route}:{},...o?{ip:o}:{},...e.dbQueriesCount!==void 0?{dbQueriesCount:e.dbQueriesCount}:{},...e.slowQueryMs!==void 0?{slowQueryMs:e.slowQueryMs}:{},...e.requestId?{requestId:e.requestId}:{},...e.traceId?{traceId:e.traceId}:{},...e.spanId?{spanId:e.spanId}:{},environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c(a)})}captureDbQuery(e){let t=String(e.operation||"").trim();if(!t)return Promise.resolve();let i=y(e.durationMs)??0,n={...e.metadata||{},operation:t,collection:e.collection||"Unknown",durationMs:i,timestamp:new Date().toISOString()};return this.sendApm({type:"db_query",message:`Database operation: ${t}`,url:e.url,method:"DB",durationMs:i,requestId:e.requestId,traceId:e.traceId,spanId:e.spanId,environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c(n)})}captureMetric(e,t,i={},n={}){let s=P(e);if(!s)return Promise.resolve();let o;try{o=N(t)}catch{return Promise.resolve()}return this.sendApm({type:"custom_metric",metricName:s,metricValue:o,requestId:n.requestId,traceId:n.traceId,spanId:n.spanId,environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c({...i,timestamp:new Date().toISOString()})})}captureCustomMetric(e){return this.captureMetric(e.metricName,e.metricValue,e.metadata||{},{requestId:e.requestId,traceId:e.traceId,spanId:e.spanId})}captureError(e,t={}){let i=e?.message?.trim()||"Erro desconhecido",n={...t},s=v(n.method),o=M(n.statusCode??n.status),a=y(n.durationMs),u=S(n.ip),l=n.url!==void 0&&n.url!==null?String(n.url).trim():void 0,g=typeof n.requestId=="string"&&n.requestId.trim()||void 0,h=typeof n.traceId=="string"&&n.traceId.trim()||void 0,p=typeof n.spanId=="string"&&n.spanId.trim()||void 0;return delete n.method,delete n.statusCode,delete n.status,delete n.durationMs,delete n.ip,delete n.requestId,delete n.traceId,delete n.spanId,this.sendError({type:"error",message:i,stackTrace:e?.stack||"",...l?{url:l}:{},...s?{method:s}:{},...o!==void 0?{statusCode:o}:{},...a!==void 0?{durationMs:a}:{},...u?{ip:u}:{},...g?{requestId:g}:{},...h?{traceId:h}:{},...p?{spanId:p}:{},environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c({...n,timestamp:new Date().toISOString()})})}captureException(e,t={}){let i=e instanceof Error?e:new Error(I(e));return this.captureError(i,t)}captureMessage(e,t={}){let i=String(e||"").trim();return i?this.sendApm({type:"message",message:i.slice(0,1e4),environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c({...t,timestamp:new Date().toISOString()})}):Promise.resolve()}async flush(){if(!(this.sendQueue.length===0&&!this.isProcessingQueue))return new Promise(e=>{this.flushResolvers.push(e),this.processQueue()})}attachGlobalListeners(){if(this.listenersAttached||typeof globalThis>"u")return;this.listenersAttached=!0;let e=globalThis;if(typeof e.ErrorUtils<"u"){let t=e.ErrorUtils.getGlobalHandler?.();e.ErrorUtils.setGlobalHandler?.((i,n)=>{this.captureError(i,{platform:"react-native",fatal:n}).finally(()=>{typeof t=="function"&&t(i,n)})})}if(typeof window<"u"&&typeof window.addEventListener=="function"){window.addEventListener("error",t=>{let i=t.error instanceof Error?t.error:new Error(t.message||"Erro global");this.captureError(i,{url:t.filename||window.location.href,line:t.lineno,column:t.colno,platform:"browser"})}),window.addEventListener("unhandledrejection",t=>{let i=t.reason instanceof Error?t.reason:new Error(I(t.reason));this.captureError(i,{platform:"browser",url:window.location.href})});return}typeof process<"u"&&typeof process.on=="function"&&(process.on("uncaughtException",t=>{this.captureError(t,{platform:"node"})}),process.on("unhandledRejection",t=>{this.captureException(t,{platform:"node"})}))}sendError(e){return this.enqueue(this.endpoint,e)}sendApm(e){return this.enqueue(this.apmEndpoint,e)}enqueue(e,t){if(!this.initialized||!this.apiKey)return Promise.resolve();let i={id:b("event"),endpoint:e,payload:c(t),resolve:()=>{}};return new Promise(n=>{i.resolve=n,this.sendQueue.push(i),this.processQueue()})}async processQueue(){if(!(this.isProcessingQueue||!this.initialized||!this.apiKey)){this.isProcessingQueue=!0;try{for(;this.sendQueue.length>0;){let e=this.sendQueue.shift();if(e)try{await this.sendToEndpoint(e.endpoint,e.payload)}finally{e.resolve()}}}finally{if(this.isProcessingQueue=!1,this.sendQueue.length===0){let e=this.flushResolvers.splice(0);for(let t of e)t()}this.sendQueue.length>0&&this.processQueue()}}}async sendToEndpoint(e,t){let i=typeof fetch=="function"?fetch:void 0;if(!i)return;let n=m(t),s,o;try{typeof AbortController<"u"&&(s=new AbortController,o=setTimeout(()=>{s?.abort()},this.requestTimeoutMs));let a=typeof window<"u"&&typeof window.document<"u",u={method:"POST",headers:{"content-type":"application/json","x-api-key":this.apiKey},body:JSON.stringify(n),signal:s?.signal};if(a&&(u.keepalive=!0),!(await i(e,u)).ok)return}catch{return}finally{o!==void 0&&clearTimeout(o)}}},A="__DEVINSPECT_AUDIT_CORE_INSTANCE__",U=typeof globalThis<"u"?globalThis:{},d=U[A]||new T;U[A]=d;async function w(r,e,t={}){let i=t.nativeFetch||(typeof fetch=="function"?fetch:void 0);if(!i)throw new Error("fetch n\xE3o est\xE1 dispon\xEDvel.");let n=typeof r=="string"?r:r instanceof URL?r.toString():String(r.url||""),s=v(e?.method||(typeof r!="string"&&!(r instanceof URL)?r.method:"GET"))||"GET";if(n&&d.isIngestUrl(n))return i(r,e);let o=d.createRequestId(),a=d.createTraceId(),u=d.createSpanId(),l=new Headers(e?.headers||(typeof r!="string"&&!(r instanceof URL)?r.headers:void 0));l.set("x-devinspector-request-id",o),l.set("x-devinspector-trace-id",a),l.set("x-devinspector-span-id",u);let g={...e,headers:l},h=typeof performance<"u"?performance.now():Date.now();try{let p=await i(r,g),f=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-h)),E=p.status>=400||f>=d.getSlowThresholdMs();return d.addBreadcrumb({type:"http",message:`${s} ${n}`,metadata:{phase:"complete",statusCode:p.status,durationMs:f,requestId:o,traceId:a,spanId:u}}),E&&d.captureRequest({method:s,url:n,statusCode:p.status,durationMs:f,userAgent:typeof navigator<"u"?navigator.userAgent:t.defaultUserAgent,requestId:o,traceId:a,spanId:u}),p}catch(p){let f=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-h));d.addBreadcrumb({type:"http_error",message:`${s} ${n}`,metadata:{phase:"error",statusCode:0,durationMs:f,requestId:o,traceId:a,spanId:u}}),d.captureRequest({method:s,url:n,statusCode:0,durationMs:f,userAgent:typeof navigator<"u"?navigator.userAgent:t.defaultUserAgent,requestId:o,traceId:a,spanId:u});let E=p instanceof Error?p:new Error(I(p));throw d.captureError(E,{url:n,method:s,type:t.errorType||"network_error",requestId:o,traceId:a,spanId:u}),p}}var q=class extends C{constructor(){super(...arguments);this.state={hasError:!1,error:null}}static getDerivedStateFromError(t){return{hasError:!0,error:t}}componentDidCatch(t,i){d.addBreadcrumb({type:"react_native_error_boundary",message:t.message||"Erro capturado pelo React Native Error Boundary",metadata:{componentStack:i.componentStack||""}}),d.captureError(t,{platform:"react-native",framework:"React Native",componentStack:i.componentStack}),this.props.onError&&this.props.onError(t,i)}render(){if(!this.state.hasError)return this.props.children;let{fallback:t}=this.props,i=this.state.error||new Error("Erro desconhecido");return typeof t=="function"?t(i):t??null}};async function z(r,e){return w(r,e,{errorType:"react_native_network_error",defaultUserAgent:"React-Native"})}function G(){if(typeof globalThis>"u")return;let r=globalThis;if(r.__DEVINSPECTOR_FETCH_PATCHED__)return;let e=globalThis.fetch;if(typeof e!="function")return;let t;try{t=Object.getOwnPropertyDescriptor(globalThis,"fetch")}catch{return}if(t&&t.get&&!t.set||t&&t.writable===!1&&!t.set)return;let i=e.bind(globalThis),n=function(s,o){return w(s,o,{errorType:"react_native_network_error",defaultUserAgent:"React-Native",nativeFetch:i})};try{t&&t.configurable?Object.defineProperty(globalThis,"fetch",{...t,value:n}):globalThis.fetch=n}catch{return}globalThis.fetch===n&&(r.__DEVINSPECTOR_NATIVE_FETCH__=i,r.__DEVINSPECTOR_FETCH_PATCHED__=!0)}export{q as AuditErrorBoundary,d as audit,z as auditFetch,G as enableNetworkTracing};
package/dist/react.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var E=Object.defineProperty;var P=Object.getOwnPropertyDescriptor;var C=Object.getOwnPropertyNames;var N=Object.prototype.hasOwnProperty;var O=(r,e)=>{for(var t in e)E(r,t,{get:e[t],enumerable:!0})},Q=(r,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of C(e))!N.call(r,n)&&n!==t&&E(r,n,{get:()=>e[n],enumerable:!(i=P(e,n))||i.enumerable});return r};var L=r=>Q(E({},"__esModule",{value:!0}),r);var V={};O(V,{AuditErrorBoundary:()=>v,audit:()=>a,auditFetch:()=>G,enableNetworkTracing:()=>H});module.exports=L(V);var q=require("react");var R="https://api.devinspector.com.br/api/ingest/track",M="https://api.devinspector.com.br/api/ingest/apm";var B=["password","senha","token","authorization","auth","bearer","credit_card","creditcard","cartao","cvv","cpf","rg","secret","private_key","privatekey"];function F(r){let e=r.toLowerCase().replace(/[\s-]/g,"_");return B.some(t=>e.includes(t))}function m(r,e=0,t=new WeakSet){if(r==null)return r;if(typeof r=="string")return r.length>1e4?`${r.slice(0,1e4)}...[truncated]`:r;if(typeof r=="number"||typeof r=="boolean")return r;if(typeof r=="bigint")return String(r);if(typeof r=="function")return"[Function]";if(e>=6)return"[MaxDepth]";if(typeof r!="object")return String(r);if(t.has(r))return"[Circular]";t.add(r);try{if(Array.isArray(r))return r.slice(0,100).map(s=>m(s,e+1,t));let i=r,n={};for(let[s,o]of Object.entries(i)){if(F(s)){n[s]="[REDACTED]";continue}n[s]=m(o,e+1,t)}return n}finally{t.delete(r)}}function y(r){if(r instanceof Error)return r.message||"Erro desconhecido";if(typeof r=="string")return r||"Erro desconhecido";try{let e=m(r);return typeof e=="string"?e||"Erro desconhecido":JSON.stringify(e)||"Erro desconhecido"}catch{return"Erro desconhecido"}}function w(r){return r&&String(r).trim().toUpperCase()||void 0}function S(r){if(!(typeof r!="number"||!Number.isFinite(r)))return Math.round(r)}function A(r){if(!(typeof r!="number"||!Number.isFinite(r)))return Math.max(0,Math.round(r))}function x(r){return String(r??"").trim().slice(0,200)}function z(r){let e=Number(r);if(!Number.isFinite(e))throw new Error("metricValue deve ser um n\xFAmero finito.");return e}function g(r){let e=typeof globalThis<"u"?globalThis.crypto:void 0;return e&&typeof e.randomUUID=="function"?`${r}-${e.randomUUID()}`:`${r}-${Date.now()}-${Math.random().toString(36).slice(2,12)}`}function c(r){return m(r)}var T=class{constructor(){this.apiKey="";this.endpoint=R;this.apmEndpoint=M;this.environment="production";this.slowThresholdMs=300;this.requestTimeoutMs=1e4;this.breadcrumbs=[];this.initialized=!1;this.listenersAttached=!1;this.sendQueue=[];this.isProcessingQueue=!1;this.flushResolvers=[]}init(e){if(!e?.apiKey?.trim())throw new Error("DevInspector: apiKey \xE9 obrigat\xF3rio.");this.apiKey=e.apiKey.trim(),this.endpoint=e.endpoint?.trim()||R,this.apmEndpoint=e.apmEndpoint?.trim()||M,this.environment=e.environment?.trim()||"production",this.release=e.release?.trim()||void 0,this.slowThresholdMs=typeof e.slowThresholdMs=="number"&&Number.isFinite(e.slowThresholdMs)&&e.slowThresholdMs>=0?Math.round(e.slowThresholdMs):300,this.requestTimeoutMs=typeof e.requestTimeoutMs=="number"&&Number.isFinite(e.requestTimeoutMs)&&e.requestTimeoutMs>0?Math.round(e.requestTimeoutMs):1e4,this.initialized=!0,this.attachGlobalListeners(),this.processQueue()}getApiKey(){return this.apiKey}getEndpoint(){return this.endpoint}getApmEndpoint(){return this.apmEndpoint}getEnvironment(){return this.environment}getRelease(){return this.release}getSlowThresholdMs(){return this.slowThresholdMs}getRequestTimeoutMs(){return this.requestTimeoutMs}isInitialized(){return this.initialized}setUser(e){this.user=e?m(e):void 0}getUser(){return this.user}clearUser(){this.user=void 0}addBreadcrumb(e){if(!e||!e.type?.trim())return;let t={timestamp:e.timestamp||new Date().toISOString(),type:e.type.trim().slice(0,200),message:e.message?.slice(0,1e4),metadata:e.metadata?m(e.metadata):void 0};this.breadcrumbs.push(t),this.breadcrumbs.length>50&&(this.breadcrumbs=this.breadcrumbs.slice(-50))}getBreadcrumbs(){return this.breadcrumbs.map(e=>c(e))}clearBreadcrumbs(){this.breadcrumbs=[]}getContext(){return{environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs()}}isIngestUrl(e){if(!e)return!1;try{let t=new URL(this.endpoint),i=new URL(this.apmEndpoint),n=new URL(e,t.origin),s=n.origin,o=n.pathname.replace(/\/$/,""),d=t.pathname.replace(/\/$/,""),u=i.pathname.replace(/\/$/,"");return s===t.origin&&o===d||s===i.origin&&o===u}catch{return e.includes(this.endpoint)||e.includes(this.apmEndpoint)}}createRequestId(){return g("req")}createTraceId(){return g("trace")}createSpanId(){return g("span")}captureRequest(e){let t=w(e.method)||"GET",i=String(e.url||"").trim();if(!i)return Promise.resolve();let n=S(e.statusCode)??0,s=A(e.durationMs)??0,o={...e.metadata||{},...e.route!==void 0?{route:e.route}:{},...e.dbQueriesCount!==void 0?{dbQueriesCount:e.dbQueriesCount}:{},...e.slowQueryMs!==void 0?{slowQueryMs:e.slowQueryMs}:{},timestamp:new Date().toISOString()};return this.sendApm({type:"request_metric",method:t,url:i,statusCode:n,durationMs:s,browser:e.userAgent,requestId:e.requestId,traceId:e.traceId,spanId:e.spanId,environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c(o)})}captureDbQuery(e){let t=String(e.operation||"").trim();if(!t)return Promise.resolve();let i=A(e.durationMs)??0,n={...e.metadata||{},operation:t,collection:e.collection||"Unknown",durationMs:i,timestamp:new Date().toISOString()};return this.sendApm({type:"db_query",message:`Database operation: ${t}`,url:e.url,method:"DB",durationMs:i,requestId:e.requestId,traceId:e.traceId,spanId:e.spanId,environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c(n)})}captureMetric(e,t,i={},n={}){let s=x(e);if(!s)return Promise.resolve();let o;try{o=z(t)}catch{return Promise.resolve()}return this.sendApm({type:"custom_metric",metricName:s,metricValue:o,requestId:n.requestId,traceId:n.traceId,spanId:n.spanId,environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c({...i,timestamp:new Date().toISOString()})})}captureCustomMetric(e){return this.captureMetric(e.metricName,e.metricValue,e.metadata||{},{requestId:e.requestId,traceId:e.traceId,spanId:e.spanId})}captureError(e,t={}){let i=e?.message?.trim()||"Erro desconhecido",n={...t},s=w(n.method),o=S(n.statusCode??n.status),d=n.url!==void 0&&n.url!==null?String(n.url).trim():void 0,u=typeof n.requestId=="string"&&n.requestId.trim()||void 0,l=typeof n.traceId=="string"&&n.traceId.trim()||void 0,h=typeof n.spanId=="string"&&n.spanId.trim()||void 0;return delete n.method,delete n.statusCode,delete n.status,delete n.requestId,delete n.traceId,delete n.spanId,delete n.durationMs,this.sendError({type:"error",message:i,stackTrace:e?.stack||"",...d?{url:d}:{},...s?{method:s}:{},...o!==void 0?{statusCode:o}:{},...u?{requestId:u}:{},...l?{traceId:l}:{},...h?{spanId:h}:{},environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c({...n,timestamp:new Date().toISOString()})})}captureException(e,t={}){let i=e instanceof Error?e:new Error(y(e));return this.captureError(i,t)}captureMessage(e,t={}){let i=String(e||"").trim();return i?this.sendApm({type:"message",message:i.slice(0,1e4),environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c({...t,timestamp:new Date().toISOString()})}):Promise.resolve()}async flush(){if(!(this.sendQueue.length===0&&!this.isProcessingQueue))return new Promise(e=>{this.flushResolvers.push(e),this.processQueue()})}attachGlobalListeners(){if(this.listenersAttached||typeof globalThis>"u")return;this.listenersAttached=!0;let e=globalThis;if(typeof e.ErrorUtils<"u"){let t=e.ErrorUtils.getGlobalHandler?.();e.ErrorUtils.setGlobalHandler?.((i,n)=>{this.captureError(i,{platform:"react-native",fatal:n}).finally(()=>{typeof t=="function"&&t(i,n)})})}if(typeof window<"u"&&typeof window.addEventListener=="function"){window.addEventListener("error",t=>{let i=t.error instanceof Error?t.error:new Error(t.message||"Erro global");this.captureError(i,{url:t.filename||window.location.href,line:t.lineno,column:t.colno,platform:"browser"})}),window.addEventListener("unhandledrejection",t=>{let i=t.reason instanceof Error?t.reason:new Error(y(t.reason));this.captureError(i,{platform:"browser",url:window.location.href})});return}typeof process<"u"&&typeof process.on=="function"&&(process.on("uncaughtException",t=>{this.captureError(t,{platform:"node"})}),process.on("unhandledRejection",t=>{this.captureException(t,{platform:"node"})}))}sendError(e){return this.enqueue(this.endpoint,e)}sendApm(e){return this.enqueue(this.apmEndpoint,e)}enqueue(e,t){if(!this.initialized||!this.apiKey)return Promise.resolve();let i={id:g("event"),endpoint:e,payload:c(t),resolve:()=>{}};return new Promise(n=>{i.resolve=n,this.sendQueue.push(i),this.processQueue()})}async processQueue(){if(!(this.isProcessingQueue||!this.initialized||!this.apiKey)){this.isProcessingQueue=!0;try{for(;this.sendQueue.length>0;){let e=this.sendQueue.shift();if(e)try{await this.sendToEndpoint(e.endpoint,e.payload)}finally{e.resolve()}}}finally{if(this.isProcessingQueue=!1,this.sendQueue.length===0){let e=this.flushResolvers.splice(0);for(let t of e)t()}this.sendQueue.length>0&&this.processQueue()}}}async sendToEndpoint(e,t){let i=typeof fetch=="function"?fetch:void 0;if(!i)return;let n=m(t),s,o;try{typeof AbortController<"u"&&(s=new AbortController,o=setTimeout(()=>{s?.abort()},this.requestTimeoutMs));let d=typeof window<"u"&&typeof window.document<"u",u={method:"POST",headers:{"content-type":"application/json","x-api-key":this.apiKey},body:JSON.stringify(n),signal:s?.signal};if(d&&(u.keepalive=!0),!(await i(e,u)).ok)return}catch{return}finally{o!==void 0&&clearTimeout(o)}}},U="__DEVINSPECT_AUDIT_CORE_INSTANCE__",D=typeof globalThis<"u"?globalThis:{},a=D[U]||new T;D[U]=a;async function I(r,e,t={}){let i=t.nativeFetch||(typeof fetch=="function"?fetch:void 0);if(!i)throw new Error("fetch n\xE3o est\xE1 dispon\xEDvel.");let n=typeof r=="string"?r:r instanceof URL?r.toString():String(r.url||""),s=w(e?.method||(typeof r!="string"&&!(r instanceof URL)?r.method:"GET"))||"GET";if(n&&a.isIngestUrl(n))return i(r,e);let o=a.createRequestId(),d=a.createTraceId(),u=a.createSpanId(),l=new Headers(e?.headers||(typeof r!="string"&&!(r instanceof URL)?r.headers:void 0));l.set("x-devinspector-request-id",o),l.set("x-devinspector-trace-id",d),l.set("x-devinspector-span-id",u);let h={...e,headers:l},_=typeof performance<"u"?performance.now():Date.now();try{let p=await i(r,h),f=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-_)),b=p.status>=400||f>=a.getSlowThresholdMs();return a.addBreadcrumb({type:"http",message:`${s} ${n}`,metadata:{phase:"complete",statusCode:p.status,durationMs:f,requestId:o,traceId:d,spanId:u}}),b&&a.captureRequest({method:s,url:n,statusCode:p.status,durationMs:f,userAgent:typeof navigator<"u"?navigator.userAgent:t.defaultUserAgent,requestId:o,traceId:d,spanId:u}),p}catch(p){let f=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-_));a.addBreadcrumb({type:"http_error",message:`${s} ${n}`,metadata:{phase:"error",statusCode:0,durationMs:f,requestId:o,traceId:d,spanId:u}}),a.captureRequest({method:s,url:n,statusCode:0,durationMs:f,userAgent:typeof navigator<"u"?navigator.userAgent:t.defaultUserAgent,requestId:o,traceId:d,spanId:u});let b=p instanceof Error?p:new Error(y(p));throw a.captureError(b,{url:n,method:s,type:t.errorType||"network_error",requestId:o,traceId:d,spanId:u}),p}}var k=require("react/jsx-runtime"),v=class extends q.Component{constructor(){super(...arguments);this.state={hasError:!1,error:null}}static getDerivedStateFromError(t){return{hasError:!0,error:t}}componentDidCatch(t,i){a.addBreadcrumb({type:"react_error_boundary",message:t.message||"Erro capturado pelo React Error Boundary",metadata:{componentStack:i.componentStack||""}}),a.captureError(t,{framework:"React",componentStack:i.componentStack,url:typeof window<"u"?window.location.href:""}),this.props.onError&&this.props.onError(t,i)}render(){if(this.state.hasError){let{fallback:t}=this.props,i=this.state.error||new Error("Erro desconhecido no componente");return typeof t=="function"?t(i):t??(0,k.jsx)("h1",{children:"Algo deu errado na aplica\xE7\xE3o."})}return this.props.children}};async function G(r,e){return I(r,e,{errorType:"react_network_error"})}function H(){if(typeof globalThis>"u")return;let r=globalThis;if(r.__DEVINSPECTOR_FETCH_PATCHED__)return;let e=globalThis.fetch;if(typeof e!="function")return;let t;try{t=Object.getOwnPropertyDescriptor(globalThis,"fetch")}catch{return}if(t&&t.get&&!t.set||t&&t.writable===!1&&!t.set)return;let i=e.bind(globalThis),n=function(s,o){return I(s,o,{errorType:"react_network_error",nativeFetch:i})};try{t&&t.configurable?Object.defineProperty(globalThis,"fetch",{...t,value:n}):globalThis.fetch=n}catch{return}globalThis.fetch===n&&(r.__DEVINSPECTOR_NATIVE_FETCH__=i,r.__DEVINSPECTOR_FETCH_PATCHED__=!0)}0&&(module.exports={AuditErrorBoundary,audit,auditFetch,enableNetworkTracing});
1
+ "use strict";var y=Object.defineProperty;var C=Object.getOwnPropertyDescriptor;var N=Object.getOwnPropertyNames;var O=Object.prototype.hasOwnProperty;var Q=(t,e)=>{for(var r in e)y(t,r,{get:e[r],enumerable:!0})},L=(t,e,r,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of N(e))!O.call(t,n)&&n!==r&&y(t,n,{get:()=>e[n],enumerable:!(i=C(e,n))||i.enumerable});return t};var B=t=>L(y({},"__esModule",{value:!0}),t);var K={};Q(K,{AuditErrorBoundary:()=>R,audit:()=>u,auditFetch:()=>H,enableNetworkTracing:()=>V});module.exports=B(K);var k=require("react");var M="https://api.devinspector.com.br/api/ingest/track",S="https://api.devinspector.com.br/api/ingest/apm";var F=["password","senha","token","authorization","auth","bearer","credit_card","creditcard","cartao","cvv","cpf","rg","secret","private_key","privatekey"];function x(t){let e=t.toLowerCase().replace(/[\s-]/g,"_");return F.some(r=>e.includes(r))}function m(t,e=0,r=new WeakSet){if(t==null)return t;if(typeof t=="string")return t.length>1e4?`${t.slice(0,1e4)}...[truncated]`:t;if(typeof t=="number"||typeof t=="boolean")return t;if(typeof t=="bigint")return String(t);if(typeof t=="function")return"[Function]";if(e>=6)return"[MaxDepth]";if(typeof t!="object")return String(t);if(r.has(t))return"[Circular]";r.add(t);try{if(Array.isArray(t))return t.slice(0,100).map(s=>m(s,e+1,r));let i=t,n={};for(let[s,o]of Object.entries(i)){if(x(s)){n[s]="[REDACTED]";continue}n[s]=m(o,e+1,r)}return n}finally{r.delete(t)}}function I(t){if(t instanceof Error)return t.message||"Erro desconhecido";if(typeof t=="string")return t||"Erro desconhecido";try{let e=m(t);return typeof e=="string"?e||"Erro desconhecido":JSON.stringify(e)||"Erro desconhecido"}catch{return"Erro desconhecido"}}function T(t){return t&&String(t).trim().toUpperCase()||void 0}function A(t){if(!(typeof t!="number"||!Number.isFinite(t)))return Math.round(t)}function w(t){if(!(typeof t!="number"||!Number.isFinite(t)))return Math.max(0,Math.round(t))}function U(t){return t==null?void 0:String(t).trim().slice(0,100)||void 0}function z(t){return String(t??"").trim().slice(0,200)}function G(t){let e=Number(t);if(!Number.isFinite(e))throw new Error("metricValue deve ser um n\xFAmero finito.");return e}function b(t){let e=typeof globalThis<"u"?globalThis.crypto:void 0;return e&&typeof e.randomUUID=="function"?`${t}-${e.randomUUID()}`:`${t}-${Date.now()}-${Math.random().toString(36).slice(2,12)}`}function c(t){return m(t)}var v=class{constructor(){this.apiKey="";this.endpoint=M;this.apmEndpoint=S;this.environment="production";this.slowThresholdMs=300;this.requestTimeoutMs=1e4;this.breadcrumbs=[];this.initialized=!1;this.listenersAttached=!1;this.sendQueue=[];this.isProcessingQueue=!1;this.flushResolvers=[]}init(e){if(!e?.apiKey?.trim())throw new Error("DevInspector: apiKey \xE9 obrigat\xF3rio.");this.apiKey=e.apiKey.trim(),this.endpoint=e.endpoint?.trim()||M,this.apmEndpoint=e.apmEndpoint?.trim()||S,this.environment=e.environment?.trim()||"production",this.release=e.release?.trim()||void 0,this.slowThresholdMs=typeof e.slowThresholdMs=="number"&&Number.isFinite(e.slowThresholdMs)&&e.slowThresholdMs>=0?Math.round(e.slowThresholdMs):300,this.requestTimeoutMs=typeof e.requestTimeoutMs=="number"&&Number.isFinite(e.requestTimeoutMs)&&e.requestTimeoutMs>0?Math.round(e.requestTimeoutMs):1e4,this.initialized=!0,this.attachGlobalListeners(),this.processQueue()}getApiKey(){return this.apiKey}getEndpoint(){return this.endpoint}getApmEndpoint(){return this.apmEndpoint}getEnvironment(){return this.environment}getRelease(){return this.release}getSlowThresholdMs(){return this.slowThresholdMs}getRequestTimeoutMs(){return this.requestTimeoutMs}isInitialized(){return this.initialized}setUser(e){this.user=e?m(e):void 0}getUser(){return this.user}clearUser(){this.user=void 0}addBreadcrumb(e){if(!e||!e.type?.trim())return;let r={timestamp:e.timestamp||new Date().toISOString(),type:e.type.trim().slice(0,200),message:e.message?.slice(0,1e4),metadata:e.metadata?m(e.metadata):void 0};this.breadcrumbs.push(r),this.breadcrumbs.length>50&&(this.breadcrumbs=this.breadcrumbs.slice(-50))}getBreadcrumbs(){return this.breadcrumbs.map(e=>c(e))}clearBreadcrumbs(){this.breadcrumbs=[]}getContext(){return{environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs()}}isIngestUrl(e){if(!e)return!1;try{let r=new URL(this.endpoint),i=new URL(this.apmEndpoint),n=new URL(e,r.origin),s=n.origin,o=n.pathname.replace(/\/$/,""),a=r.pathname.replace(/\/$/,""),d=i.pathname.replace(/\/$/,"");return s===r.origin&&o===a||s===i.origin&&o===d}catch{return e.includes(this.endpoint)||e.includes(this.apmEndpoint)}}createRequestId(){return b("req")}createTraceId(){return b("trace")}createSpanId(){return b("span")}captureRequest(e){let r=T(e.method)||"GET",i=String(e.url||"").trim();if(!i)return Promise.resolve();let n=A(e.statusCode)??0,s=w(e.durationMs)??0,o=U(e.ip),a={...e.metadata||{},timestamp:new Date().toISOString()};return this.sendApm({type:"request_metric",method:r,url:i,statusCode:n,durationMs:s,...e.userAgent?{userAgent:e.userAgent}:{},...e.route!==void 0?{route:e.route}:{},...o?{ip:o}:{},...e.dbQueriesCount!==void 0?{dbQueriesCount:e.dbQueriesCount}:{},...e.slowQueryMs!==void 0?{slowQueryMs:e.slowQueryMs}:{},...e.requestId?{requestId:e.requestId}:{},...e.traceId?{traceId:e.traceId}:{},...e.spanId?{spanId:e.spanId}:{},environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c(a)})}captureDbQuery(e){let r=String(e.operation||"").trim();if(!r)return Promise.resolve();let i=w(e.durationMs)??0,n={...e.metadata||{},operation:r,collection:e.collection||"Unknown",durationMs:i,timestamp:new Date().toISOString()};return this.sendApm({type:"db_query",message:`Database operation: ${r}`,url:e.url,method:"DB",durationMs:i,requestId:e.requestId,traceId:e.traceId,spanId:e.spanId,environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c(n)})}captureMetric(e,r,i={},n={}){let s=z(e);if(!s)return Promise.resolve();let o;try{o=G(r)}catch{return Promise.resolve()}return this.sendApm({type:"custom_metric",metricName:s,metricValue:o,requestId:n.requestId,traceId:n.traceId,spanId:n.spanId,environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c({...i,timestamp:new Date().toISOString()})})}captureCustomMetric(e){return this.captureMetric(e.metricName,e.metricValue,e.metadata||{},{requestId:e.requestId,traceId:e.traceId,spanId:e.spanId})}captureError(e,r={}){let i=e?.message?.trim()||"Erro desconhecido",n={...r},s=T(n.method),o=A(n.statusCode??n.status),a=w(n.durationMs),d=U(n.ip),l=n.url!==void 0&&n.url!==null?String(n.url).trim():void 0,g=typeof n.requestId=="string"&&n.requestId.trim()||void 0,h=typeof n.traceId=="string"&&n.traceId.trim()||void 0,p=typeof n.spanId=="string"&&n.spanId.trim()||void 0;return delete n.method,delete n.statusCode,delete n.status,delete n.durationMs,delete n.ip,delete n.requestId,delete n.traceId,delete n.spanId,this.sendError({type:"error",message:i,stackTrace:e?.stack||"",...l?{url:l}:{},...s?{method:s}:{},...o!==void 0?{statusCode:o}:{},...a!==void 0?{durationMs:a}:{},...d?{ip:d}:{},...g?{requestId:g}:{},...h?{traceId:h}:{},...p?{spanId:p}:{},environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c({...n,timestamp:new Date().toISOString()})})}captureException(e,r={}){let i=e instanceof Error?e:new Error(I(e));return this.captureError(i,r)}captureMessage(e,r={}){let i=String(e||"").trim();return i?this.sendApm({type:"message",message:i.slice(0,1e4),environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c({...r,timestamp:new Date().toISOString()})}):Promise.resolve()}async flush(){if(!(this.sendQueue.length===0&&!this.isProcessingQueue))return new Promise(e=>{this.flushResolvers.push(e),this.processQueue()})}attachGlobalListeners(){if(this.listenersAttached||typeof globalThis>"u")return;this.listenersAttached=!0;let e=globalThis;if(typeof e.ErrorUtils<"u"){let r=e.ErrorUtils.getGlobalHandler?.();e.ErrorUtils.setGlobalHandler?.((i,n)=>{this.captureError(i,{platform:"react-native",fatal:n}).finally(()=>{typeof r=="function"&&r(i,n)})})}if(typeof window<"u"&&typeof window.addEventListener=="function"){window.addEventListener("error",r=>{let i=r.error instanceof Error?r.error:new Error(r.message||"Erro global");this.captureError(i,{url:r.filename||window.location.href,line:r.lineno,column:r.colno,platform:"browser"})}),window.addEventListener("unhandledrejection",r=>{let i=r.reason instanceof Error?r.reason:new Error(I(r.reason));this.captureError(i,{platform:"browser",url:window.location.href})});return}typeof process<"u"&&typeof process.on=="function"&&(process.on("uncaughtException",r=>{this.captureError(r,{platform:"node"})}),process.on("unhandledRejection",r=>{this.captureException(r,{platform:"node"})}))}sendError(e){return this.enqueue(this.endpoint,e)}sendApm(e){return this.enqueue(this.apmEndpoint,e)}enqueue(e,r){if(!this.initialized||!this.apiKey)return Promise.resolve();let i={id:b("event"),endpoint:e,payload:c(r),resolve:()=>{}};return new Promise(n=>{i.resolve=n,this.sendQueue.push(i),this.processQueue()})}async processQueue(){if(!(this.isProcessingQueue||!this.initialized||!this.apiKey)){this.isProcessingQueue=!0;try{for(;this.sendQueue.length>0;){let e=this.sendQueue.shift();if(e)try{await this.sendToEndpoint(e.endpoint,e.payload)}finally{e.resolve()}}}finally{if(this.isProcessingQueue=!1,this.sendQueue.length===0){let e=this.flushResolvers.splice(0);for(let r of e)r()}this.sendQueue.length>0&&this.processQueue()}}}async sendToEndpoint(e,r){let i=typeof fetch=="function"?fetch:void 0;if(!i)return;let n=m(r),s,o;try{typeof AbortController<"u"&&(s=new AbortController,o=setTimeout(()=>{s?.abort()},this.requestTimeoutMs));let a=typeof window<"u"&&typeof window.document<"u",d={method:"POST",headers:{"content-type":"application/json","x-api-key":this.apiKey},body:JSON.stringify(n),signal:s?.signal};if(a&&(d.keepalive=!0),!(await i(e,d)).ok)return}catch{return}finally{o!==void 0&&clearTimeout(o)}}},q="__DEVINSPECT_AUDIT_CORE_INSTANCE__",D=typeof globalThis<"u"?globalThis:{},u=D[q]||new v;D[q]=u;async function _(t,e,r={}){let i=r.nativeFetch||(typeof fetch=="function"?fetch:void 0);if(!i)throw new Error("fetch n\xE3o est\xE1 dispon\xEDvel.");let n=typeof t=="string"?t:t instanceof URL?t.toString():String(t.url||""),s=T(e?.method||(typeof t!="string"&&!(t instanceof URL)?t.method:"GET"))||"GET";if(n&&u.isIngestUrl(n))return i(t,e);let o=u.createRequestId(),a=u.createTraceId(),d=u.createSpanId(),l=new Headers(e?.headers||(typeof t!="string"&&!(t instanceof URL)?t.headers:void 0));l.set("x-devinspector-request-id",o),l.set("x-devinspector-trace-id",a),l.set("x-devinspector-span-id",d);let g={...e,headers:l},h=typeof performance<"u"?performance.now():Date.now();try{let p=await i(t,g),f=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-h)),E=p.status>=400||f>=u.getSlowThresholdMs();return u.addBreadcrumb({type:"http",message:`${s} ${n}`,metadata:{phase:"complete",statusCode:p.status,durationMs:f,requestId:o,traceId:a,spanId:d}}),E&&u.captureRequest({method:s,url:n,statusCode:p.status,durationMs:f,userAgent:typeof navigator<"u"?navigator.userAgent:r.defaultUserAgent,requestId:o,traceId:a,spanId:d}),p}catch(p){let f=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-h));u.addBreadcrumb({type:"http_error",message:`${s} ${n}`,metadata:{phase:"error",statusCode:0,durationMs:f,requestId:o,traceId:a,spanId:d}}),u.captureRequest({method:s,url:n,statusCode:0,durationMs:f,userAgent:typeof navigator<"u"?navigator.userAgent:r.defaultUserAgent,requestId:o,traceId:a,spanId:d});let E=p instanceof Error?p:new Error(I(p));throw u.captureError(E,{url:n,method:s,type:r.errorType||"network_error",requestId:o,traceId:a,spanId:d}),p}}var P=require("react/jsx-runtime"),R=class extends k.Component{constructor(){super(...arguments);this.state={hasError:!1,error:null}}static getDerivedStateFromError(r){return{hasError:!0,error:r}}componentDidCatch(r,i){u.addBreadcrumb({type:"react_error_boundary",message:r.message||"Erro capturado pelo React Error Boundary",metadata:{componentStack:i.componentStack||""}}),u.captureError(r,{framework:"React",componentStack:i.componentStack,url:typeof window<"u"?window.location.href:""}),this.props.onError&&this.props.onError(r,i)}render(){if(this.state.hasError){let{fallback:r}=this.props,i=this.state.error||new Error("Erro desconhecido no componente");return typeof r=="function"?r(i):r??(0,P.jsx)("h1",{children:"Algo deu errado na aplica\xE7\xE3o."})}return this.props.children}};async function H(t,e){return _(t,e,{errorType:"react_network_error"})}function V(){if(typeof globalThis>"u")return;let t=globalThis;if(t.__DEVINSPECTOR_FETCH_PATCHED__)return;let e=globalThis.fetch;if(typeof e!="function")return;let r;try{r=Object.getOwnPropertyDescriptor(globalThis,"fetch")}catch{return}if(r&&r.get&&!r.set||r&&r.writable===!1&&!r.set)return;let i=e.bind(globalThis),n=function(s,o){return _(s,o,{errorType:"react_network_error",nativeFetch:i})};try{r&&r.configurable?Object.defineProperty(globalThis,"fetch",{...r,value:n}):globalThis.fetch=n}catch{return}globalThis.fetch===n&&(t.__DEVINSPECTOR_NATIVE_FETCH__=i,t.__DEVINSPECTOR_FETCH_PATCHED__=!0)}0&&(module.exports={AuditErrorBoundary,audit,auditFetch,enableNetworkTracing});
package/dist/react.js CHANGED
@@ -1 +1 @@
1
- import{Component as C}from"react";var v="https://api.devinspector.com.br/api/ingest/track",_="https://api.devinspector.com.br/api/ingest/apm";var D=["password","senha","token","authorization","auth","bearer","credit_card","creditcard","cartao","cvv","cpf","rg","secret","private_key","privatekey"];function q(r){let e=r.toLowerCase().replace(/[\s-]/g,"_");return D.some(t=>e.includes(t))}function m(r,e=0,t=new WeakSet){if(r==null)return r;if(typeof r=="string")return r.length>1e4?`${r.slice(0,1e4)}...[truncated]`:r;if(typeof r=="number"||typeof r=="boolean")return r;if(typeof r=="bigint")return String(r);if(typeof r=="function")return"[Function]";if(e>=6)return"[MaxDepth]";if(typeof r!="object")return String(r);if(t.has(r))return"[Circular]";t.add(r);try{if(Array.isArray(r))return r.slice(0,100).map(s=>m(s,e+1,t));let i=r,n={};for(let[s,o]of Object.entries(i)){if(q(s)){n[s]="[REDACTED]";continue}n[s]=m(o,e+1,t)}return n}finally{t.delete(r)}}function E(r){if(r instanceof Error)return r.message||"Erro desconhecido";if(typeof r=="string")return r||"Erro desconhecido";try{let e=m(r);return typeof e=="string"?e||"Erro desconhecido":JSON.stringify(e)||"Erro desconhecido"}catch{return"Erro desconhecido"}}function y(r){return r&&String(r).trim().toUpperCase()||void 0}function R(r){if(!(typeof r!="number"||!Number.isFinite(r)))return Math.round(r)}function M(r){if(!(typeof r!="number"||!Number.isFinite(r)))return Math.max(0,Math.round(r))}function k(r){return String(r??"").trim().slice(0,200)}function P(r){let e=Number(r);if(!Number.isFinite(e))throw new Error("metricValue deve ser um n\xFAmero finito.");return e}function g(r){let e=typeof globalThis<"u"?globalThis.crypto:void 0;return e&&typeof e.randomUUID=="function"?`${r}-${e.randomUUID()}`:`${r}-${Date.now()}-${Math.random().toString(36).slice(2,12)}`}function c(r){return m(r)}var w=class{constructor(){this.apiKey="";this.endpoint=v;this.apmEndpoint=_;this.environment="production";this.slowThresholdMs=300;this.requestTimeoutMs=1e4;this.breadcrumbs=[];this.initialized=!1;this.listenersAttached=!1;this.sendQueue=[];this.isProcessingQueue=!1;this.flushResolvers=[]}init(e){if(!e?.apiKey?.trim())throw new Error("DevInspector: apiKey \xE9 obrigat\xF3rio.");this.apiKey=e.apiKey.trim(),this.endpoint=e.endpoint?.trim()||v,this.apmEndpoint=e.apmEndpoint?.trim()||_,this.environment=e.environment?.trim()||"production",this.release=e.release?.trim()||void 0,this.slowThresholdMs=typeof e.slowThresholdMs=="number"&&Number.isFinite(e.slowThresholdMs)&&e.slowThresholdMs>=0?Math.round(e.slowThresholdMs):300,this.requestTimeoutMs=typeof e.requestTimeoutMs=="number"&&Number.isFinite(e.requestTimeoutMs)&&e.requestTimeoutMs>0?Math.round(e.requestTimeoutMs):1e4,this.initialized=!0,this.attachGlobalListeners(),this.processQueue()}getApiKey(){return this.apiKey}getEndpoint(){return this.endpoint}getApmEndpoint(){return this.apmEndpoint}getEnvironment(){return this.environment}getRelease(){return this.release}getSlowThresholdMs(){return this.slowThresholdMs}getRequestTimeoutMs(){return this.requestTimeoutMs}isInitialized(){return this.initialized}setUser(e){this.user=e?m(e):void 0}getUser(){return this.user}clearUser(){this.user=void 0}addBreadcrumb(e){if(!e||!e.type?.trim())return;let t={timestamp:e.timestamp||new Date().toISOString(),type:e.type.trim().slice(0,200),message:e.message?.slice(0,1e4),metadata:e.metadata?m(e.metadata):void 0};this.breadcrumbs.push(t),this.breadcrumbs.length>50&&(this.breadcrumbs=this.breadcrumbs.slice(-50))}getBreadcrumbs(){return this.breadcrumbs.map(e=>c(e))}clearBreadcrumbs(){this.breadcrumbs=[]}getContext(){return{environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs()}}isIngestUrl(e){if(!e)return!1;try{let t=new URL(this.endpoint),i=new URL(this.apmEndpoint),n=new URL(e,t.origin),s=n.origin,o=n.pathname.replace(/\/$/,""),d=t.pathname.replace(/\/$/,""),a=i.pathname.replace(/\/$/,"");return s===t.origin&&o===d||s===i.origin&&o===a}catch{return e.includes(this.endpoint)||e.includes(this.apmEndpoint)}}createRequestId(){return g("req")}createTraceId(){return g("trace")}createSpanId(){return g("span")}captureRequest(e){let t=y(e.method)||"GET",i=String(e.url||"").trim();if(!i)return Promise.resolve();let n=R(e.statusCode)??0,s=M(e.durationMs)??0,o={...e.metadata||{},...e.route!==void 0?{route:e.route}:{},...e.dbQueriesCount!==void 0?{dbQueriesCount:e.dbQueriesCount}:{},...e.slowQueryMs!==void 0?{slowQueryMs:e.slowQueryMs}:{},timestamp:new Date().toISOString()};return this.sendApm({type:"request_metric",method:t,url:i,statusCode:n,durationMs:s,browser:e.userAgent,requestId:e.requestId,traceId:e.traceId,spanId:e.spanId,environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c(o)})}captureDbQuery(e){let t=String(e.operation||"").trim();if(!t)return Promise.resolve();let i=M(e.durationMs)??0,n={...e.metadata||{},operation:t,collection:e.collection||"Unknown",durationMs:i,timestamp:new Date().toISOString()};return this.sendApm({type:"db_query",message:`Database operation: ${t}`,url:e.url,method:"DB",durationMs:i,requestId:e.requestId,traceId:e.traceId,spanId:e.spanId,environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c(n)})}captureMetric(e,t,i={},n={}){let s=k(e);if(!s)return Promise.resolve();let o;try{o=P(t)}catch{return Promise.resolve()}return this.sendApm({type:"custom_metric",metricName:s,metricValue:o,requestId:n.requestId,traceId:n.traceId,spanId:n.spanId,environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c({...i,timestamp:new Date().toISOString()})})}captureCustomMetric(e){return this.captureMetric(e.metricName,e.metricValue,e.metadata||{},{requestId:e.requestId,traceId:e.traceId,spanId:e.spanId})}captureError(e,t={}){let i=e?.message?.trim()||"Erro desconhecido",n={...t},s=y(n.method),o=R(n.statusCode??n.status),d=n.url!==void 0&&n.url!==null?String(n.url).trim():void 0,a=typeof n.requestId=="string"&&n.requestId.trim()||void 0,l=typeof n.traceId=="string"&&n.traceId.trim()||void 0,h=typeof n.spanId=="string"&&n.spanId.trim()||void 0;return delete n.method,delete n.statusCode,delete n.status,delete n.requestId,delete n.traceId,delete n.spanId,delete n.durationMs,this.sendError({type:"error",message:i,stackTrace:e?.stack||"",...d?{url:d}:{},...s?{method:s}:{},...o!==void 0?{statusCode:o}:{},...a?{requestId:a}:{},...l?{traceId:l}:{},...h?{spanId:h}:{},environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c({...n,timestamp:new Date().toISOString()})})}captureException(e,t={}){let i=e instanceof Error?e:new Error(E(e));return this.captureError(i,t)}captureMessage(e,t={}){let i=String(e||"").trim();return i?this.sendApm({type:"message",message:i.slice(0,1e4),environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c({...t,timestamp:new Date().toISOString()})}):Promise.resolve()}async flush(){if(!(this.sendQueue.length===0&&!this.isProcessingQueue))return new Promise(e=>{this.flushResolvers.push(e),this.processQueue()})}attachGlobalListeners(){if(this.listenersAttached||typeof globalThis>"u")return;this.listenersAttached=!0;let e=globalThis;if(typeof e.ErrorUtils<"u"){let t=e.ErrorUtils.getGlobalHandler?.();e.ErrorUtils.setGlobalHandler?.((i,n)=>{this.captureError(i,{platform:"react-native",fatal:n}).finally(()=>{typeof t=="function"&&t(i,n)})})}if(typeof window<"u"&&typeof window.addEventListener=="function"){window.addEventListener("error",t=>{let i=t.error instanceof Error?t.error:new Error(t.message||"Erro global");this.captureError(i,{url:t.filename||window.location.href,line:t.lineno,column:t.colno,platform:"browser"})}),window.addEventListener("unhandledrejection",t=>{let i=t.reason instanceof Error?t.reason:new Error(E(t.reason));this.captureError(i,{platform:"browser",url:window.location.href})});return}typeof process<"u"&&typeof process.on=="function"&&(process.on("uncaughtException",t=>{this.captureError(t,{platform:"node"})}),process.on("unhandledRejection",t=>{this.captureException(t,{platform:"node"})}))}sendError(e){return this.enqueue(this.endpoint,e)}sendApm(e){return this.enqueue(this.apmEndpoint,e)}enqueue(e,t){if(!this.initialized||!this.apiKey)return Promise.resolve();let i={id:g("event"),endpoint:e,payload:c(t),resolve:()=>{}};return new Promise(n=>{i.resolve=n,this.sendQueue.push(i),this.processQueue()})}async processQueue(){if(!(this.isProcessingQueue||!this.initialized||!this.apiKey)){this.isProcessingQueue=!0;try{for(;this.sendQueue.length>0;){let e=this.sendQueue.shift();if(e)try{await this.sendToEndpoint(e.endpoint,e.payload)}finally{e.resolve()}}}finally{if(this.isProcessingQueue=!1,this.sendQueue.length===0){let e=this.flushResolvers.splice(0);for(let t of e)t()}this.sendQueue.length>0&&this.processQueue()}}}async sendToEndpoint(e,t){let i=typeof fetch=="function"?fetch:void 0;if(!i)return;let n=m(t),s,o;try{typeof AbortController<"u"&&(s=new AbortController,o=setTimeout(()=>{s?.abort()},this.requestTimeoutMs));let d=typeof window<"u"&&typeof window.document<"u",a={method:"POST",headers:{"content-type":"application/json","x-api-key":this.apiKey},body:JSON.stringify(n),signal:s?.signal};if(d&&(a.keepalive=!0),!(await i(e,a)).ok)return}catch{return}finally{o!==void 0&&clearTimeout(o)}}},S="__DEVINSPECT_AUDIT_CORE_INSTANCE__",A=typeof globalThis<"u"?globalThis:{},u=A[S]||new w;A[S]=u;async function T(r,e,t={}){let i=t.nativeFetch||(typeof fetch=="function"?fetch:void 0);if(!i)throw new Error("fetch n\xE3o est\xE1 dispon\xEDvel.");let n=typeof r=="string"?r:r instanceof URL?r.toString():String(r.url||""),s=y(e?.method||(typeof r!="string"&&!(r instanceof URL)?r.method:"GET"))||"GET";if(n&&u.isIngestUrl(n))return i(r,e);let o=u.createRequestId(),d=u.createTraceId(),a=u.createSpanId(),l=new Headers(e?.headers||(typeof r!="string"&&!(r instanceof URL)?r.headers:void 0));l.set("x-devinspector-request-id",o),l.set("x-devinspector-trace-id",d),l.set("x-devinspector-span-id",a);let h={...e,headers:l},I=typeof performance<"u"?performance.now():Date.now();try{let p=await i(r,h),f=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-I)),b=p.status>=400||f>=u.getSlowThresholdMs();return u.addBreadcrumb({type:"http",message:`${s} ${n}`,metadata:{phase:"complete",statusCode:p.status,durationMs:f,requestId:o,traceId:d,spanId:a}}),b&&u.captureRequest({method:s,url:n,statusCode:p.status,durationMs:f,userAgent:typeof navigator<"u"?navigator.userAgent:t.defaultUserAgent,requestId:o,traceId:d,spanId:a}),p}catch(p){let f=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-I));u.addBreadcrumb({type:"http_error",message:`${s} ${n}`,metadata:{phase:"error",statusCode:0,durationMs:f,requestId:o,traceId:d,spanId:a}}),u.captureRequest({method:s,url:n,statusCode:0,durationMs:f,userAgent:typeof navigator<"u"?navigator.userAgent:t.defaultUserAgent,requestId:o,traceId:d,spanId:a});let b=p instanceof Error?p:new Error(E(p));throw u.captureError(b,{url:n,method:s,type:t.errorType||"network_error",requestId:o,traceId:d,spanId:a}),p}}import{jsx as N}from"react/jsx-runtime";var U=class extends C{constructor(){super(...arguments);this.state={hasError:!1,error:null}}static getDerivedStateFromError(t){return{hasError:!0,error:t}}componentDidCatch(t,i){u.addBreadcrumb({type:"react_error_boundary",message:t.message||"Erro capturado pelo React Error Boundary",metadata:{componentStack:i.componentStack||""}}),u.captureError(t,{framework:"React",componentStack:i.componentStack,url:typeof window<"u"?window.location.href:""}),this.props.onError&&this.props.onError(t,i)}render(){if(this.state.hasError){let{fallback:t}=this.props,i=this.state.error||new Error("Erro desconhecido no componente");return typeof t=="function"?t(i):t??N("h1",{children:"Algo deu errado na aplica\xE7\xE3o."})}return this.props.children}};async function z(r,e){return T(r,e,{errorType:"react_network_error"})}function G(){if(typeof globalThis>"u")return;let r=globalThis;if(r.__DEVINSPECTOR_FETCH_PATCHED__)return;let e=globalThis.fetch;if(typeof e!="function")return;let t;try{t=Object.getOwnPropertyDescriptor(globalThis,"fetch")}catch{return}if(t&&t.get&&!t.set||t&&t.writable===!1&&!t.set)return;let i=e.bind(globalThis),n=function(s,o){return T(s,o,{errorType:"react_network_error",nativeFetch:i})};try{t&&t.configurable?Object.defineProperty(globalThis,"fetch",{...t,value:n}):globalThis.fetch=n}catch{return}globalThis.fetch===n&&(r.__DEVINSPECTOR_NATIVE_FETCH__=i,r.__DEVINSPECTOR_FETCH_PATCHED__=!0)}export{U as AuditErrorBoundary,u as audit,z as auditFetch,G as enableNetworkTracing};
1
+ import{Component as N}from"react";var _="https://api.devinspector.com.br/api/ingest/track",R="https://api.devinspector.com.br/api/ingest/apm";var D=["password","senha","token","authorization","auth","bearer","credit_card","creditcard","cartao","cvv","cpf","rg","secret","private_key","privatekey"];function k(r){let e=r.toLowerCase().replace(/[\s-]/g,"_");return D.some(t=>e.includes(t))}function m(r,e=0,t=new WeakSet){if(r==null)return r;if(typeof r=="string")return r.length>1e4?`${r.slice(0,1e4)}...[truncated]`:r;if(typeof r=="number"||typeof r=="boolean")return r;if(typeof r=="bigint")return String(r);if(typeof r=="function")return"[Function]";if(e>=6)return"[MaxDepth]";if(typeof r!="object")return String(r);if(t.has(r))return"[Circular]";t.add(r);try{if(Array.isArray(r))return r.slice(0,100).map(s=>m(s,e+1,t));let i=r,n={};for(let[s,o]of Object.entries(i)){if(k(s)){n[s]="[REDACTED]";continue}n[s]=m(o,e+1,t)}return n}finally{t.delete(r)}}function w(r){if(r instanceof Error)return r.message||"Erro desconhecido";if(typeof r=="string")return r||"Erro desconhecido";try{let e=m(r);return typeof e=="string"?e||"Erro desconhecido":JSON.stringify(e)||"Erro desconhecido"}catch{return"Erro desconhecido"}}function I(r){return r&&String(r).trim().toUpperCase()||void 0}function M(r){if(!(typeof r!="number"||!Number.isFinite(r)))return Math.round(r)}function y(r){if(!(typeof r!="number"||!Number.isFinite(r)))return Math.max(0,Math.round(r))}function S(r){return r==null?void 0:String(r).trim().slice(0,100)||void 0}function P(r){return String(r??"").trim().slice(0,200)}function C(r){let e=Number(r);if(!Number.isFinite(e))throw new Error("metricValue deve ser um n\xFAmero finito.");return e}function b(r){let e=typeof globalThis<"u"?globalThis.crypto:void 0;return e&&typeof e.randomUUID=="function"?`${r}-${e.randomUUID()}`:`${r}-${Date.now()}-${Math.random().toString(36).slice(2,12)}`}function c(r){return m(r)}var T=class{constructor(){this.apiKey="";this.endpoint=_;this.apmEndpoint=R;this.environment="production";this.slowThresholdMs=300;this.requestTimeoutMs=1e4;this.breadcrumbs=[];this.initialized=!1;this.listenersAttached=!1;this.sendQueue=[];this.isProcessingQueue=!1;this.flushResolvers=[]}init(e){if(!e?.apiKey?.trim())throw new Error("DevInspector: apiKey \xE9 obrigat\xF3rio.");this.apiKey=e.apiKey.trim(),this.endpoint=e.endpoint?.trim()||_,this.apmEndpoint=e.apmEndpoint?.trim()||R,this.environment=e.environment?.trim()||"production",this.release=e.release?.trim()||void 0,this.slowThresholdMs=typeof e.slowThresholdMs=="number"&&Number.isFinite(e.slowThresholdMs)&&e.slowThresholdMs>=0?Math.round(e.slowThresholdMs):300,this.requestTimeoutMs=typeof e.requestTimeoutMs=="number"&&Number.isFinite(e.requestTimeoutMs)&&e.requestTimeoutMs>0?Math.round(e.requestTimeoutMs):1e4,this.initialized=!0,this.attachGlobalListeners(),this.processQueue()}getApiKey(){return this.apiKey}getEndpoint(){return this.endpoint}getApmEndpoint(){return this.apmEndpoint}getEnvironment(){return this.environment}getRelease(){return this.release}getSlowThresholdMs(){return this.slowThresholdMs}getRequestTimeoutMs(){return this.requestTimeoutMs}isInitialized(){return this.initialized}setUser(e){this.user=e?m(e):void 0}getUser(){return this.user}clearUser(){this.user=void 0}addBreadcrumb(e){if(!e||!e.type?.trim())return;let t={timestamp:e.timestamp||new Date().toISOString(),type:e.type.trim().slice(0,200),message:e.message?.slice(0,1e4),metadata:e.metadata?m(e.metadata):void 0};this.breadcrumbs.push(t),this.breadcrumbs.length>50&&(this.breadcrumbs=this.breadcrumbs.slice(-50))}getBreadcrumbs(){return this.breadcrumbs.map(e=>c(e))}clearBreadcrumbs(){this.breadcrumbs=[]}getContext(){return{environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs()}}isIngestUrl(e){if(!e)return!1;try{let t=new URL(this.endpoint),i=new URL(this.apmEndpoint),n=new URL(e,t.origin),s=n.origin,o=n.pathname.replace(/\/$/,""),a=t.pathname.replace(/\/$/,""),u=i.pathname.replace(/\/$/,"");return s===t.origin&&o===a||s===i.origin&&o===u}catch{return e.includes(this.endpoint)||e.includes(this.apmEndpoint)}}createRequestId(){return b("req")}createTraceId(){return b("trace")}createSpanId(){return b("span")}captureRequest(e){let t=I(e.method)||"GET",i=String(e.url||"").trim();if(!i)return Promise.resolve();let n=M(e.statusCode)??0,s=y(e.durationMs)??0,o=S(e.ip),a={...e.metadata||{},timestamp:new Date().toISOString()};return this.sendApm({type:"request_metric",method:t,url:i,statusCode:n,durationMs:s,...e.userAgent?{userAgent:e.userAgent}:{},...e.route!==void 0?{route:e.route}:{},...o?{ip:o}:{},...e.dbQueriesCount!==void 0?{dbQueriesCount:e.dbQueriesCount}:{},...e.slowQueryMs!==void 0?{slowQueryMs:e.slowQueryMs}:{},...e.requestId?{requestId:e.requestId}:{},...e.traceId?{traceId:e.traceId}:{},...e.spanId?{spanId:e.spanId}:{},environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c(a)})}captureDbQuery(e){let t=String(e.operation||"").trim();if(!t)return Promise.resolve();let i=y(e.durationMs)??0,n={...e.metadata||{},operation:t,collection:e.collection||"Unknown",durationMs:i,timestamp:new Date().toISOString()};return this.sendApm({type:"db_query",message:`Database operation: ${t}`,url:e.url,method:"DB",durationMs:i,requestId:e.requestId,traceId:e.traceId,spanId:e.spanId,environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c(n)})}captureMetric(e,t,i={},n={}){let s=P(e);if(!s)return Promise.resolve();let o;try{o=C(t)}catch{return Promise.resolve()}return this.sendApm({type:"custom_metric",metricName:s,metricValue:o,requestId:n.requestId,traceId:n.traceId,spanId:n.spanId,environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c({...i,timestamp:new Date().toISOString()})})}captureCustomMetric(e){return this.captureMetric(e.metricName,e.metricValue,e.metadata||{},{requestId:e.requestId,traceId:e.traceId,spanId:e.spanId})}captureError(e,t={}){let i=e?.message?.trim()||"Erro desconhecido",n={...t},s=I(n.method),o=M(n.statusCode??n.status),a=y(n.durationMs),u=S(n.ip),l=n.url!==void 0&&n.url!==null?String(n.url).trim():void 0,g=typeof n.requestId=="string"&&n.requestId.trim()||void 0,h=typeof n.traceId=="string"&&n.traceId.trim()||void 0,p=typeof n.spanId=="string"&&n.spanId.trim()||void 0;return delete n.method,delete n.statusCode,delete n.status,delete n.durationMs,delete n.ip,delete n.requestId,delete n.traceId,delete n.spanId,this.sendError({type:"error",message:i,stackTrace:e?.stack||"",...l?{url:l}:{},...s?{method:s}:{},...o!==void 0?{statusCode:o}:{},...a!==void 0?{durationMs:a}:{},...u?{ip:u}:{},...g?{requestId:g}:{},...h?{traceId:h}:{},...p?{spanId:p}:{},environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c({...n,timestamp:new Date().toISOString()})})}captureException(e,t={}){let i=e instanceof Error?e:new Error(w(e));return this.captureError(i,t)}captureMessage(e,t={}){let i=String(e||"").trim();return i?this.sendApm({type:"message",message:i.slice(0,1e4),environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c({...t,timestamp:new Date().toISOString()})}):Promise.resolve()}async flush(){if(!(this.sendQueue.length===0&&!this.isProcessingQueue))return new Promise(e=>{this.flushResolvers.push(e),this.processQueue()})}attachGlobalListeners(){if(this.listenersAttached||typeof globalThis>"u")return;this.listenersAttached=!0;let e=globalThis;if(typeof e.ErrorUtils<"u"){let t=e.ErrorUtils.getGlobalHandler?.();e.ErrorUtils.setGlobalHandler?.((i,n)=>{this.captureError(i,{platform:"react-native",fatal:n}).finally(()=>{typeof t=="function"&&t(i,n)})})}if(typeof window<"u"&&typeof window.addEventListener=="function"){window.addEventListener("error",t=>{let i=t.error instanceof Error?t.error:new Error(t.message||"Erro global");this.captureError(i,{url:t.filename||window.location.href,line:t.lineno,column:t.colno,platform:"browser"})}),window.addEventListener("unhandledrejection",t=>{let i=t.reason instanceof Error?t.reason:new Error(w(t.reason));this.captureError(i,{platform:"browser",url:window.location.href})});return}typeof process<"u"&&typeof process.on=="function"&&(process.on("uncaughtException",t=>{this.captureError(t,{platform:"node"})}),process.on("unhandledRejection",t=>{this.captureException(t,{platform:"node"})}))}sendError(e){return this.enqueue(this.endpoint,e)}sendApm(e){return this.enqueue(this.apmEndpoint,e)}enqueue(e,t){if(!this.initialized||!this.apiKey)return Promise.resolve();let i={id:b("event"),endpoint:e,payload:c(t),resolve:()=>{}};return new Promise(n=>{i.resolve=n,this.sendQueue.push(i),this.processQueue()})}async processQueue(){if(!(this.isProcessingQueue||!this.initialized||!this.apiKey)){this.isProcessingQueue=!0;try{for(;this.sendQueue.length>0;){let e=this.sendQueue.shift();if(e)try{await this.sendToEndpoint(e.endpoint,e.payload)}finally{e.resolve()}}}finally{if(this.isProcessingQueue=!1,this.sendQueue.length===0){let e=this.flushResolvers.splice(0);for(let t of e)t()}this.sendQueue.length>0&&this.processQueue()}}}async sendToEndpoint(e,t){let i=typeof fetch=="function"?fetch:void 0;if(!i)return;let n=m(t),s,o;try{typeof AbortController<"u"&&(s=new AbortController,o=setTimeout(()=>{s?.abort()},this.requestTimeoutMs));let a=typeof window<"u"&&typeof window.document<"u",u={method:"POST",headers:{"content-type":"application/json","x-api-key":this.apiKey},body:JSON.stringify(n),signal:s?.signal};if(a&&(u.keepalive=!0),!(await i(e,u)).ok)return}catch{return}finally{o!==void 0&&clearTimeout(o)}}},A="__DEVINSPECT_AUDIT_CORE_INSTANCE__",U=typeof globalThis<"u"?globalThis:{},d=U[A]||new T;U[A]=d;async function v(r,e,t={}){let i=t.nativeFetch||(typeof fetch=="function"?fetch:void 0);if(!i)throw new Error("fetch n\xE3o est\xE1 dispon\xEDvel.");let n=typeof r=="string"?r:r instanceof URL?r.toString():String(r.url||""),s=I(e?.method||(typeof r!="string"&&!(r instanceof URL)?r.method:"GET"))||"GET";if(n&&d.isIngestUrl(n))return i(r,e);let o=d.createRequestId(),a=d.createTraceId(),u=d.createSpanId(),l=new Headers(e?.headers||(typeof r!="string"&&!(r instanceof URL)?r.headers:void 0));l.set("x-devinspector-request-id",o),l.set("x-devinspector-trace-id",a),l.set("x-devinspector-span-id",u);let g={...e,headers:l},h=typeof performance<"u"?performance.now():Date.now();try{let p=await i(r,g),f=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-h)),E=p.status>=400||f>=d.getSlowThresholdMs();return d.addBreadcrumb({type:"http",message:`${s} ${n}`,metadata:{phase:"complete",statusCode:p.status,durationMs:f,requestId:o,traceId:a,spanId:u}}),E&&d.captureRequest({method:s,url:n,statusCode:p.status,durationMs:f,userAgent:typeof navigator<"u"?navigator.userAgent:t.defaultUserAgent,requestId:o,traceId:a,spanId:u}),p}catch(p){let f=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-h));d.addBreadcrumb({type:"http_error",message:`${s} ${n}`,metadata:{phase:"error",statusCode:0,durationMs:f,requestId:o,traceId:a,spanId:u}}),d.captureRequest({method:s,url:n,statusCode:0,durationMs:f,userAgent:typeof navigator<"u"?navigator.userAgent:t.defaultUserAgent,requestId:o,traceId:a,spanId:u});let E=p instanceof Error?p:new Error(w(p));throw d.captureError(E,{url:n,method:s,type:t.errorType||"network_error",requestId:o,traceId:a,spanId:u}),p}}import{jsx as O}from"react/jsx-runtime";var q=class extends N{constructor(){super(...arguments);this.state={hasError:!1,error:null}}static getDerivedStateFromError(t){return{hasError:!0,error:t}}componentDidCatch(t,i){d.addBreadcrumb({type:"react_error_boundary",message:t.message||"Erro capturado pelo React Error Boundary",metadata:{componentStack:i.componentStack||""}}),d.captureError(t,{framework:"React",componentStack:i.componentStack,url:typeof window<"u"?window.location.href:""}),this.props.onError&&this.props.onError(t,i)}render(){if(this.state.hasError){let{fallback:t}=this.props,i=this.state.error||new Error("Erro desconhecido no componente");return typeof t=="function"?t(i):t??O("h1",{children:"Algo deu errado na aplica\xE7\xE3o."})}return this.props.children}};async function G(r,e){return v(r,e,{errorType:"react_network_error"})}function H(){if(typeof globalThis>"u")return;let r=globalThis;if(r.__DEVINSPECTOR_FETCH_PATCHED__)return;let e=globalThis.fetch;if(typeof e!="function")return;let t;try{t=Object.getOwnPropertyDescriptor(globalThis,"fetch")}catch{return}if(t&&t.get&&!t.set||t&&t.writable===!1&&!t.set)return;let i=e.bind(globalThis),n=function(s,o){return v(s,o,{errorType:"react_network_error",nativeFetch:i})};try{t&&t.configurable?Object.defineProperty(globalThis,"fetch",{...t,value:n}):globalThis.fetch=n}catch{return}globalThis.fetch===n&&(r.__DEVINSPECTOR_NATIVE_FETCH__=i,r.__DEVINSPECTOR_FETCH_PATCHED__=!0)}export{q as AuditErrorBoundary,d as audit,G as auditFetch,H as enableNetworkTracing};
package/dist/svelte.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var w=Object.defineProperty;var D=Object.getOwnPropertyDescriptor;var k=Object.getOwnPropertyNames;var P=Object.prototype.hasOwnProperty;var C=(t,e)=>{for(var r in e)w(t,r,{get:e[r],enumerable:!0})},O=(t,e,r,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of k(e))!P.call(t,n)&&n!==r&&w(t,n,{get:()=>e[n],enumerable:!(s=D(e,n))||s.enumerable});return t};var N=t=>O(w({},"__esModule",{value:!0}),t);var $={};C($,{audit:()=>a,auditFetch:()=>K,auditHandle:()=>z,enableNetworkTracing:()=>H,handleSvelteKitError:()=>G,initSvelteDevInspector:()=>F});module.exports=N($);var M="https://api.devinspector.com.br/api/ingest/track",R="https://api.devinspector.com.br/api/ingest/apm";var x=["password","senha","token","authorization","auth","bearer","credit_card","creditcard","cartao","cvv","cpf","rg","secret","private_key","privatekey"];function Q(t){let e=t.toLowerCase().replace(/[\s-]/g,"_");return x.some(r=>e.includes(r))}function h(t,e=0,r=new WeakSet){if(t==null)return t;if(typeof t=="string")return t.length>1e4?`${t.slice(0,1e4)}...[truncated]`:t;if(typeof t=="number"||typeof t=="boolean")return t;if(typeof t=="bigint")return String(t);if(typeof t=="function")return"[Function]";if(e>=6)return"[MaxDepth]";if(typeof t!="object")return String(t);if(r.has(t))return"[Circular]";r.add(t);try{if(Array.isArray(t))return t.slice(0,100).map(i=>h(i,e+1,r));let s=t,n={};for(let[i,o]of Object.entries(s)){if(Q(i)){n[i]="[REDACTED]";continue}n[i]=h(o,e+1,r)}return n}finally{r.delete(t)}}function I(t){if(t instanceof Error)return t.message||"Erro desconhecido";if(typeof t=="string")return t||"Erro desconhecido";try{let e=h(t);return typeof e=="string"?e||"Erro desconhecido":JSON.stringify(e)||"Erro desconhecido"}catch{return"Erro desconhecido"}}function T(t){return t&&String(t).trim().toUpperCase()||void 0}function S(t){if(!(typeof t!="number"||!Number.isFinite(t)))return Math.round(t)}function A(t){if(!(typeof t!="number"||!Number.isFinite(t)))return Math.max(0,Math.round(t))}function B(t){return String(t??"").trim().slice(0,200)}function L(t){let e=Number(t);if(!Number.isFinite(e))throw new Error("metricValue deve ser um n\xFAmero finito.");return e}function y(t){let e=typeof globalThis<"u"?globalThis.crypto:void 0;return e&&typeof e.randomUUID=="function"?`${t}-${e.randomUUID()}`:`${t}-${Date.now()}-${Math.random().toString(36).slice(2,12)}`}function l(t){return h(t)}var v=class{constructor(){this.apiKey="";this.endpoint=M;this.apmEndpoint=R;this.environment="production";this.slowThresholdMs=300;this.requestTimeoutMs=1e4;this.breadcrumbs=[];this.initialized=!1;this.listenersAttached=!1;this.sendQueue=[];this.isProcessingQueue=!1;this.flushResolvers=[]}init(e){if(!e?.apiKey?.trim())throw new Error("DevInspector: apiKey \xE9 obrigat\xF3rio.");this.apiKey=e.apiKey.trim(),this.endpoint=e.endpoint?.trim()||M,this.apmEndpoint=e.apmEndpoint?.trim()||R,this.environment=e.environment?.trim()||"production",this.release=e.release?.trim()||void 0,this.slowThresholdMs=typeof e.slowThresholdMs=="number"&&Number.isFinite(e.slowThresholdMs)&&e.slowThresholdMs>=0?Math.round(e.slowThresholdMs):300,this.requestTimeoutMs=typeof e.requestTimeoutMs=="number"&&Number.isFinite(e.requestTimeoutMs)&&e.requestTimeoutMs>0?Math.round(e.requestTimeoutMs):1e4,this.initialized=!0,this.attachGlobalListeners(),this.processQueue()}getApiKey(){return this.apiKey}getEndpoint(){return this.endpoint}getApmEndpoint(){return this.apmEndpoint}getEnvironment(){return this.environment}getRelease(){return this.release}getSlowThresholdMs(){return this.slowThresholdMs}getRequestTimeoutMs(){return this.requestTimeoutMs}isInitialized(){return this.initialized}setUser(e){this.user=e?h(e):void 0}getUser(){return this.user}clearUser(){this.user=void 0}addBreadcrumb(e){if(!e||!e.type?.trim())return;let r={timestamp:e.timestamp||new Date().toISOString(),type:e.type.trim().slice(0,200),message:e.message?.slice(0,1e4),metadata:e.metadata?h(e.metadata):void 0};this.breadcrumbs.push(r),this.breadcrumbs.length>50&&(this.breadcrumbs=this.breadcrumbs.slice(-50))}getBreadcrumbs(){return this.breadcrumbs.map(e=>l(e))}clearBreadcrumbs(){this.breadcrumbs=[]}getContext(){return{environment:this.environment,release:this.release,user:l(this.user),breadcrumbs:this.getBreadcrumbs()}}isIngestUrl(e){if(!e)return!1;try{let r=new URL(this.endpoint),s=new URL(this.apmEndpoint),n=new URL(e,r.origin),i=n.origin,o=n.pathname.replace(/\/$/,""),u=r.pathname.replace(/\/$/,""),d=s.pathname.replace(/\/$/,"");return i===r.origin&&o===u||i===s.origin&&o===d}catch{return e.includes(this.endpoint)||e.includes(this.apmEndpoint)}}createRequestId(){return y("req")}createTraceId(){return y("trace")}createSpanId(){return y("span")}captureRequest(e){let r=T(e.method)||"GET",s=String(e.url||"").trim();if(!s)return Promise.resolve();let n=S(e.statusCode)??0,i=A(e.durationMs)??0,o={...e.metadata||{},...e.route!==void 0?{route:e.route}:{},...e.dbQueriesCount!==void 0?{dbQueriesCount:e.dbQueriesCount}:{},...e.slowQueryMs!==void 0?{slowQueryMs:e.slowQueryMs}:{},timestamp:new Date().toISOString()};return this.sendApm({type:"request_metric",method:r,url:s,statusCode:n,durationMs:i,browser:e.userAgent,requestId:e.requestId,traceId:e.traceId,spanId:e.spanId,environment:this.environment,release:this.release,user:l(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:l(o)})}captureDbQuery(e){let r=String(e.operation||"").trim();if(!r)return Promise.resolve();let s=A(e.durationMs)??0,n={...e.metadata||{},operation:r,collection:e.collection||"Unknown",durationMs:s,timestamp:new Date().toISOString()};return this.sendApm({type:"db_query",message:`Database operation: ${r}`,url:e.url,method:"DB",durationMs:s,requestId:e.requestId,traceId:e.traceId,spanId:e.spanId,environment:this.environment,release:this.release,user:l(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:l(n)})}captureMetric(e,r,s={},n={}){let i=B(e);if(!i)return Promise.resolve();let o;try{o=L(r)}catch{return Promise.resolve()}return this.sendApm({type:"custom_metric",metricName:i,metricValue:o,requestId:n.requestId,traceId:n.traceId,spanId:n.spanId,environment:this.environment,release:this.release,user:l(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:l({...s,timestamp:new Date().toISOString()})})}captureCustomMetric(e){return this.captureMetric(e.metricName,e.metricValue,e.metadata||{},{requestId:e.requestId,traceId:e.traceId,spanId:e.spanId})}captureError(e,r={}){let s=e?.message?.trim()||"Erro desconhecido",n={...r},i=T(n.method),o=S(n.statusCode??n.status),u=n.url!==void 0&&n.url!==null?String(n.url).trim():void 0,d=typeof n.requestId=="string"&&n.requestId.trim()||void 0,c=typeof n.traceId=="string"&&n.traceId.trim()||void 0,p=typeof n.spanId=="string"&&n.spanId.trim()||void 0;return delete n.method,delete n.statusCode,delete n.status,delete n.requestId,delete n.traceId,delete n.spanId,delete n.durationMs,this.sendError({type:"error",message:s,stackTrace:e?.stack||"",...u?{url:u}:{},...i?{method:i}:{},...o!==void 0?{statusCode:o}:{},...d?{requestId:d}:{},...c?{traceId:c}:{},...p?{spanId:p}:{},environment:this.environment,release:this.release,user:l(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:l({...n,timestamp:new Date().toISOString()})})}captureException(e,r={}){let s=e instanceof Error?e:new Error(I(e));return this.captureError(s,r)}captureMessage(e,r={}){let s=String(e||"").trim();return s?this.sendApm({type:"message",message:s.slice(0,1e4),environment:this.environment,release:this.release,user:l(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:l({...r,timestamp:new Date().toISOString()})}):Promise.resolve()}async flush(){if(!(this.sendQueue.length===0&&!this.isProcessingQueue))return new Promise(e=>{this.flushResolvers.push(e),this.processQueue()})}attachGlobalListeners(){if(this.listenersAttached||typeof globalThis>"u")return;this.listenersAttached=!0;let e=globalThis;if(typeof e.ErrorUtils<"u"){let r=e.ErrorUtils.getGlobalHandler?.();e.ErrorUtils.setGlobalHandler?.((s,n)=>{this.captureError(s,{platform:"react-native",fatal:n}).finally(()=>{typeof r=="function"&&r(s,n)})})}if(typeof window<"u"&&typeof window.addEventListener=="function"){window.addEventListener("error",r=>{let s=r.error instanceof Error?r.error:new Error(r.message||"Erro global");this.captureError(s,{url:r.filename||window.location.href,line:r.lineno,column:r.colno,platform:"browser"})}),window.addEventListener("unhandledrejection",r=>{let s=r.reason instanceof Error?r.reason:new Error(I(r.reason));this.captureError(s,{platform:"browser",url:window.location.href})});return}typeof process<"u"&&typeof process.on=="function"&&(process.on("uncaughtException",r=>{this.captureError(r,{platform:"node"})}),process.on("unhandledRejection",r=>{this.captureException(r,{platform:"node"})}))}sendError(e){return this.enqueue(this.endpoint,e)}sendApm(e){return this.enqueue(this.apmEndpoint,e)}enqueue(e,r){if(!this.initialized||!this.apiKey)return Promise.resolve();let s={id:y("event"),endpoint:e,payload:l(r),resolve:()=>{}};return new Promise(n=>{s.resolve=n,this.sendQueue.push(s),this.processQueue()})}async processQueue(){if(!(this.isProcessingQueue||!this.initialized||!this.apiKey)){this.isProcessingQueue=!0;try{for(;this.sendQueue.length>0;){let e=this.sendQueue.shift();if(e)try{await this.sendToEndpoint(e.endpoint,e.payload)}finally{e.resolve()}}}finally{if(this.isProcessingQueue=!1,this.sendQueue.length===0){let e=this.flushResolvers.splice(0);for(let r of e)r()}this.sendQueue.length>0&&this.processQueue()}}}async sendToEndpoint(e,r){let s=typeof fetch=="function"?fetch:void 0;if(!s)return;let n=h(r),i,o;try{typeof AbortController<"u"&&(i=new AbortController,o=setTimeout(()=>{i?.abort()},this.requestTimeoutMs));let u=typeof window<"u"&&typeof window.document<"u",d={method:"POST",headers:{"content-type":"application/json","x-api-key":this.apiKey},body:JSON.stringify(n),signal:i?.signal};if(u&&(d.keepalive=!0),!(await s(e,d)).ok)return}catch{return}finally{o!==void 0&&clearTimeout(o)}}},q="__DEVINSPECT_AUDIT_CORE_INSTANCE__",U=typeof globalThis<"u"?globalThis:{},a=U[q]||new v;U[q]=a;async function _(t,e,r={}){let s=r.nativeFetch||(typeof fetch=="function"?fetch:void 0);if(!s)throw new Error("fetch n\xE3o est\xE1 dispon\xEDvel.");let n=typeof t=="string"?t:t instanceof URL?t.toString():String(t.url||""),i=T(e?.method||(typeof t!="string"&&!(t instanceof URL)?t.method:"GET"))||"GET";if(n&&a.isIngestUrl(n))return s(t,e);let o=a.createRequestId(),u=a.createTraceId(),d=a.createSpanId(),c=new Headers(e?.headers||(typeof t!="string"&&!(t instanceof URL)?t.headers:void 0));c.set("x-devinspector-request-id",o),c.set("x-devinspector-trace-id",u),c.set("x-devinspector-span-id",d);let p={...e,headers:c},f=typeof performance<"u"?performance.now():Date.now();try{let m=await s(t,p),g=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-f)),E=m.status>=400||g>=a.getSlowThresholdMs();return a.addBreadcrumb({type:"http",message:`${i} ${n}`,metadata:{phase:"complete",statusCode:m.status,durationMs:g,requestId:o,traceId:u,spanId:d}}),E&&a.captureRequest({method:i,url:n,statusCode:m.status,durationMs:g,userAgent:typeof navigator<"u"?navigator.userAgent:r.defaultUserAgent,requestId:o,traceId:u,spanId:d}),m}catch(m){let g=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-f));a.addBreadcrumb({type:"http_error",message:`${i} ${n}`,metadata:{phase:"error",statusCode:0,durationMs:g,requestId:o,traceId:u,spanId:d}}),a.captureRequest({method:i,url:n,statusCode:0,durationMs:g,userAgent:typeof navigator<"u"?navigator.userAgent:r.defaultUserAgent,requestId:o,traceId:u,spanId:d});let E=m instanceof Error?m:new Error(I(m));throw a.captureError(E,{url:n,method:i,type:r.errorType||"network_error",requestId:o,traceId:u,spanId:d}),m}}function F(t){a.init(t)}async function K(t,e){return _(t,e,{errorType:"svelte_network_error",defaultUserAgent:"SvelteKit"})}async function z({event:t,resolve:e}){let r=typeof performance<"u"?performance.now():Date.now(),s=t.request?.method||"GET",n=t.url?.href||t.url?.pathname||"/",i=t.route?.id||n;if(n&&a.isIngestUrl(n))return e(t);let o=typeof t.request?.headers?.get=="function"&&t.request.headers.get("user-agent")||"",u=b(t.request,"x-devinspector-request-id")||a.createRequestId(),d=b(t.request,"x-devinspector-trace-id")||a.createTraceId(),c=b(t.request,"x-devinspector-span-id")||a.createSpanId();a.addBreadcrumb({type:"http",message:`${s} ${n}`,metadata:{framework:"SvelteKit",phase:"start",requestId:u,traceId:d,spanId:c,route:i}});try{let p=await e(t),f=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-r));return(p.status>=400||f>=a.getSlowThresholdMs())&&a.captureRequest({method:s,url:n,statusCode:p.status,durationMs:f,route:i,userAgent:o,requestId:u,traceId:d,spanId:c}),a.addBreadcrumb({type:"http",message:`${s} ${n}`,metadata:{framework:"SvelteKit",phase:"complete",statusCode:p.status,durationMs:f,requestId:u,traceId:d,spanId:c,route:i}}),p}catch(p){let f=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-r));a.captureRequest({method:s,url:n,statusCode:500,durationMs:f,route:i,userAgent:o,requestId:u,traceId:d,spanId:c}),a.addBreadcrumb({type:"http_error",message:`${s} ${n}`,metadata:{framework:"SvelteKit",statusCode:500,durationMs:f,requestId:u,traceId:d,spanId:c,route:i}});let m=p instanceof Error?p:new Error(String(p));throw a.captureError(m,{framework:"SvelteKit",url:n,method:s,route:i,requestId:u,traceId:d,spanId:c}),p}}function G({error:t,event:e,status:r,message:s}){let n=t instanceof Error?t:new Error(String(t||s||"Erro SvelteKit")),i=b(e?.request,"x-devinspector-request-id")||a.createRequestId(),o=b(e?.request,"x-devinspector-trace-id")||a.createTraceId(),u=b(e?.request,"x-devinspector-span-id")||a.createSpanId();a.addBreadcrumb({type:"sveltekit_error",message:n.message||"Erro SvelteKit",metadata:{statusCode:r||500,url:e?.url?.href||"",route:e?.route?.id||"",requestId:i,traceId:o,spanId:u}}),a.captureError(n,{framework:"SvelteKit",statusCode:r||500,url:e?.url?.href||"",route:e?.route?.id||"",requestId:i,traceId:o,spanId:u})}function H(){if(typeof globalThis>"u")return;let t=globalThis;if(t.__DEVINSPECTOR_FETCH_PATCHED__)return;let e=globalThis.fetch;if(typeof e!="function")return;let r;try{r=Object.getOwnPropertyDescriptor(globalThis,"fetch")}catch{return}if(r&&r.get&&!r.set||r&&r.writable===!1&&!r.set)return;let s=e.bind(globalThis),n=function(i,o){return _(i,o,{errorType:"svelte_network_error",defaultUserAgent:"SvelteKit",nativeFetch:s})};try{r&&r.configurable?Object.defineProperty(globalThis,"fetch",{...r,value:n}):globalThis.fetch=n}catch{return}globalThis.fetch===n&&(t.__DEVINSPECTOR_NATIVE_FETCH__=s,t.__DEVINSPECTOR_FETCH_PATCHED__=!0)}function b(t,e){if(!t?.headers)return;if(typeof t.headers.get=="function")return t.headers.get(e)||void 0;let r=t.headers[e]??t.headers[e.toLowerCase()];return Array.isArray(r)?r[0]:r?String(r):void 0}0&&(module.exports={audit,auditFetch,auditHandle,enableNetworkTracing,handleSvelteKitError,initSvelteDevInspector});
1
+ "use strict";var I=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var P=Object.getOwnPropertyNames;var C=Object.prototype.hasOwnProperty;var O=(t,e)=>{for(var r in e)I(t,r,{get:e[r],enumerable:!0})},N=(t,e,r,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of P(e))!C.call(t,n)&&n!==r&&I(t,n,{get:()=>e[n],enumerable:!(s=k(e,n))||s.enumerable});return t};var x=t=>N(I({},"__esModule",{value:!0}),t);var V={};O(V,{audit:()=>a,auditFetch:()=>z,auditHandle:()=>G,enableNetworkTracing:()=>$,handleSvelteKitError:()=>H,initSvelteDevInspector:()=>K});module.exports=x(V);var S="https://api.devinspector.com.br/api/ingest/track",R="https://api.devinspector.com.br/api/ingest/apm";var Q=["password","senha","token","authorization","auth","bearer","credit_card","creditcard","cartao","cvv","cpf","rg","secret","private_key","privatekey"];function B(t){let e=t.toLowerCase().replace(/[\s-]/g,"_");return Q.some(r=>e.includes(r))}function h(t,e=0,r=new WeakSet){if(t==null)return t;if(typeof t=="string")return t.length>1e4?`${t.slice(0,1e4)}...[truncated]`:t;if(typeof t=="number"||typeof t=="boolean")return t;if(typeof t=="bigint")return String(t);if(typeof t=="function")return"[Function]";if(e>=6)return"[MaxDepth]";if(typeof t!="object")return String(t);if(r.has(t))return"[Circular]";r.add(t);try{if(Array.isArray(t))return t.slice(0,100).map(i=>h(i,e+1,r));let s=t,n={};for(let[i,o]of Object.entries(s)){if(B(i)){n[i]="[REDACTED]";continue}n[i]=h(o,e+1,r)}return n}finally{r.delete(t)}}function T(t){if(t instanceof Error)return t.message||"Erro desconhecido";if(typeof t=="string")return t||"Erro desconhecido";try{let e=h(t);return typeof e=="string"?e||"Erro desconhecido":JSON.stringify(e)||"Erro desconhecido"}catch{return"Erro desconhecido"}}function v(t){return t&&String(t).trim().toUpperCase()||void 0}function A(t){if(!(typeof t!="number"||!Number.isFinite(t)))return Math.round(t)}function w(t){if(!(typeof t!="number"||!Number.isFinite(t)))return Math.max(0,Math.round(t))}function q(t){return t==null?void 0:String(t).trim().slice(0,100)||void 0}function L(t){return String(t??"").trim().slice(0,200)}function F(t){let e=Number(t);if(!Number.isFinite(e))throw new Error("metricValue deve ser um n\xFAmero finito.");return e}function y(t){let e=typeof globalThis<"u"?globalThis.crypto:void 0;return e&&typeof e.randomUUID=="function"?`${t}-${e.randomUUID()}`:`${t}-${Date.now()}-${Math.random().toString(36).slice(2,12)}`}function m(t){return h(t)}var M=class{constructor(){this.apiKey="";this.endpoint=S;this.apmEndpoint=R;this.environment="production";this.slowThresholdMs=300;this.requestTimeoutMs=1e4;this.breadcrumbs=[];this.initialized=!1;this.listenersAttached=!1;this.sendQueue=[];this.isProcessingQueue=!1;this.flushResolvers=[]}init(e){if(!e?.apiKey?.trim())throw new Error("DevInspector: apiKey \xE9 obrigat\xF3rio.");this.apiKey=e.apiKey.trim(),this.endpoint=e.endpoint?.trim()||S,this.apmEndpoint=e.apmEndpoint?.trim()||R,this.environment=e.environment?.trim()||"production",this.release=e.release?.trim()||void 0,this.slowThresholdMs=typeof e.slowThresholdMs=="number"&&Number.isFinite(e.slowThresholdMs)&&e.slowThresholdMs>=0?Math.round(e.slowThresholdMs):300,this.requestTimeoutMs=typeof e.requestTimeoutMs=="number"&&Number.isFinite(e.requestTimeoutMs)&&e.requestTimeoutMs>0?Math.round(e.requestTimeoutMs):1e4,this.initialized=!0,this.attachGlobalListeners(),this.processQueue()}getApiKey(){return this.apiKey}getEndpoint(){return this.endpoint}getApmEndpoint(){return this.apmEndpoint}getEnvironment(){return this.environment}getRelease(){return this.release}getSlowThresholdMs(){return this.slowThresholdMs}getRequestTimeoutMs(){return this.requestTimeoutMs}isInitialized(){return this.initialized}setUser(e){this.user=e?h(e):void 0}getUser(){return this.user}clearUser(){this.user=void 0}addBreadcrumb(e){if(!e||!e.type?.trim())return;let r={timestamp:e.timestamp||new Date().toISOString(),type:e.type.trim().slice(0,200),message:e.message?.slice(0,1e4),metadata:e.metadata?h(e.metadata):void 0};this.breadcrumbs.push(r),this.breadcrumbs.length>50&&(this.breadcrumbs=this.breadcrumbs.slice(-50))}getBreadcrumbs(){return this.breadcrumbs.map(e=>m(e))}clearBreadcrumbs(){this.breadcrumbs=[]}getContext(){return{environment:this.environment,release:this.release,user:m(this.user),breadcrumbs:this.getBreadcrumbs()}}isIngestUrl(e){if(!e)return!1;try{let r=new URL(this.endpoint),s=new URL(this.apmEndpoint),n=new URL(e,r.origin),i=n.origin,o=n.pathname.replace(/\/$/,""),u=r.pathname.replace(/\/$/,""),d=s.pathname.replace(/\/$/,"");return i===r.origin&&o===u||i===s.origin&&o===d}catch{return e.includes(this.endpoint)||e.includes(this.apmEndpoint)}}createRequestId(){return y("req")}createTraceId(){return y("trace")}createSpanId(){return y("span")}captureRequest(e){let r=v(e.method)||"GET",s=String(e.url||"").trim();if(!s)return Promise.resolve();let n=A(e.statusCode)??0,i=w(e.durationMs)??0,o=q(e.ip),u={...e.metadata||{},timestamp:new Date().toISOString()};return this.sendApm({type:"request_metric",method:r,url:s,statusCode:n,durationMs:i,...e.userAgent?{userAgent:e.userAgent}:{},...e.route!==void 0?{route:e.route}:{},...o?{ip:o}:{},...e.dbQueriesCount!==void 0?{dbQueriesCount:e.dbQueriesCount}:{},...e.slowQueryMs!==void 0?{slowQueryMs:e.slowQueryMs}:{},...e.requestId?{requestId:e.requestId}:{},...e.traceId?{traceId:e.traceId}:{},...e.spanId?{spanId:e.spanId}:{},environment:this.environment,release:this.release,user:m(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:m(u)})}captureDbQuery(e){let r=String(e.operation||"").trim();if(!r)return Promise.resolve();let s=w(e.durationMs)??0,n={...e.metadata||{},operation:r,collection:e.collection||"Unknown",durationMs:s,timestamp:new Date().toISOString()};return this.sendApm({type:"db_query",message:`Database operation: ${r}`,url:e.url,method:"DB",durationMs:s,requestId:e.requestId,traceId:e.traceId,spanId:e.spanId,environment:this.environment,release:this.release,user:m(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:m(n)})}captureMetric(e,r,s={},n={}){let i=L(e);if(!i)return Promise.resolve();let o;try{o=F(r)}catch{return Promise.resolve()}return this.sendApm({type:"custom_metric",metricName:i,metricValue:o,requestId:n.requestId,traceId:n.traceId,spanId:n.spanId,environment:this.environment,release:this.release,user:m(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:m({...s,timestamp:new Date().toISOString()})})}captureCustomMetric(e){return this.captureMetric(e.metricName,e.metricValue,e.metadata||{},{requestId:e.requestId,traceId:e.traceId,spanId:e.spanId})}captureError(e,r={}){let s=e?.message?.trim()||"Erro desconhecido",n={...r},i=v(n.method),o=A(n.statusCode??n.status),u=w(n.durationMs),d=q(n.ip),c=n.url!==void 0&&n.url!==null?String(n.url).trim():void 0,p=typeof n.requestId=="string"&&n.requestId.trim()||void 0,f=typeof n.traceId=="string"&&n.traceId.trim()||void 0,l=typeof n.spanId=="string"&&n.spanId.trim()||void 0;return delete n.method,delete n.statusCode,delete n.status,delete n.durationMs,delete n.ip,delete n.requestId,delete n.traceId,delete n.spanId,this.sendError({type:"error",message:s,stackTrace:e?.stack||"",...c?{url:c}:{},...i?{method:i}:{},...o!==void 0?{statusCode:o}:{},...u!==void 0?{durationMs:u}:{},...d?{ip:d}:{},...p?{requestId:p}:{},...f?{traceId:f}:{},...l?{spanId:l}:{},environment:this.environment,release:this.release,user:m(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:m({...n,timestamp:new Date().toISOString()})})}captureException(e,r={}){let s=e instanceof Error?e:new Error(T(e));return this.captureError(s,r)}captureMessage(e,r={}){let s=String(e||"").trim();return s?this.sendApm({type:"message",message:s.slice(0,1e4),environment:this.environment,release:this.release,user:m(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:m({...r,timestamp:new Date().toISOString()})}):Promise.resolve()}async flush(){if(!(this.sendQueue.length===0&&!this.isProcessingQueue))return new Promise(e=>{this.flushResolvers.push(e),this.processQueue()})}attachGlobalListeners(){if(this.listenersAttached||typeof globalThis>"u")return;this.listenersAttached=!0;let e=globalThis;if(typeof e.ErrorUtils<"u"){let r=e.ErrorUtils.getGlobalHandler?.();e.ErrorUtils.setGlobalHandler?.((s,n)=>{this.captureError(s,{platform:"react-native",fatal:n}).finally(()=>{typeof r=="function"&&r(s,n)})})}if(typeof window<"u"&&typeof window.addEventListener=="function"){window.addEventListener("error",r=>{let s=r.error instanceof Error?r.error:new Error(r.message||"Erro global");this.captureError(s,{url:r.filename||window.location.href,line:r.lineno,column:r.colno,platform:"browser"})}),window.addEventListener("unhandledrejection",r=>{let s=r.reason instanceof Error?r.reason:new Error(T(r.reason));this.captureError(s,{platform:"browser",url:window.location.href})});return}typeof process<"u"&&typeof process.on=="function"&&(process.on("uncaughtException",r=>{this.captureError(r,{platform:"node"})}),process.on("unhandledRejection",r=>{this.captureException(r,{platform:"node"})}))}sendError(e){return this.enqueue(this.endpoint,e)}sendApm(e){return this.enqueue(this.apmEndpoint,e)}enqueue(e,r){if(!this.initialized||!this.apiKey)return Promise.resolve();let s={id:y("event"),endpoint:e,payload:m(r),resolve:()=>{}};return new Promise(n=>{s.resolve=n,this.sendQueue.push(s),this.processQueue()})}async processQueue(){if(!(this.isProcessingQueue||!this.initialized||!this.apiKey)){this.isProcessingQueue=!0;try{for(;this.sendQueue.length>0;){let e=this.sendQueue.shift();if(e)try{await this.sendToEndpoint(e.endpoint,e.payload)}finally{e.resolve()}}}finally{if(this.isProcessingQueue=!1,this.sendQueue.length===0){let e=this.flushResolvers.splice(0);for(let r of e)r()}this.sendQueue.length>0&&this.processQueue()}}}async sendToEndpoint(e,r){let s=typeof fetch=="function"?fetch:void 0;if(!s)return;let n=h(r),i,o;try{typeof AbortController<"u"&&(i=new AbortController,o=setTimeout(()=>{i?.abort()},this.requestTimeoutMs));let u=typeof window<"u"&&typeof window.document<"u",d={method:"POST",headers:{"content-type":"application/json","x-api-key":this.apiKey},body:JSON.stringify(n),signal:i?.signal};if(u&&(d.keepalive=!0),!(await s(e,d)).ok)return}catch{return}finally{o!==void 0&&clearTimeout(o)}}},U="__DEVINSPECT_AUDIT_CORE_INSTANCE__",D=typeof globalThis<"u"?globalThis:{},a=D[U]||new M;D[U]=a;async function _(t,e,r={}){let s=r.nativeFetch||(typeof fetch=="function"?fetch:void 0);if(!s)throw new Error("fetch n\xE3o est\xE1 dispon\xEDvel.");let n=typeof t=="string"?t:t instanceof URL?t.toString():String(t.url||""),i=v(e?.method||(typeof t!="string"&&!(t instanceof URL)?t.method:"GET"))||"GET";if(n&&a.isIngestUrl(n))return s(t,e);let o=a.createRequestId(),u=a.createTraceId(),d=a.createSpanId(),c=new Headers(e?.headers||(typeof t!="string"&&!(t instanceof URL)?t.headers:void 0));c.set("x-devinspector-request-id",o),c.set("x-devinspector-trace-id",u),c.set("x-devinspector-span-id",d);let p={...e,headers:c},f=typeof performance<"u"?performance.now():Date.now();try{let l=await s(t,p),g=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-f)),E=l.status>=400||g>=a.getSlowThresholdMs();return a.addBreadcrumb({type:"http",message:`${i} ${n}`,metadata:{phase:"complete",statusCode:l.status,durationMs:g,requestId:o,traceId:u,spanId:d}}),E&&a.captureRequest({method:i,url:n,statusCode:l.status,durationMs:g,userAgent:typeof navigator<"u"?navigator.userAgent:r.defaultUserAgent,requestId:o,traceId:u,spanId:d}),l}catch(l){let g=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-f));a.addBreadcrumb({type:"http_error",message:`${i} ${n}`,metadata:{phase:"error",statusCode:0,durationMs:g,requestId:o,traceId:u,spanId:d}}),a.captureRequest({method:i,url:n,statusCode:0,durationMs:g,userAgent:typeof navigator<"u"?navigator.userAgent:r.defaultUserAgent,requestId:o,traceId:u,spanId:d});let E=l instanceof Error?l:new Error(T(l));throw a.captureError(E,{url:n,method:i,type:r.errorType||"network_error",requestId:o,traceId:u,spanId:d}),l}}function K(t){a.init(t)}async function z(t,e){return _(t,e,{errorType:"svelte_network_error",defaultUserAgent:"SvelteKit"})}async function G({event:t,resolve:e}){let r=typeof performance<"u"?performance.now():Date.now(),s=t.request?.method||"GET",n=t.url?.href||t.url?.pathname||"/",i=t.route?.id||n;if(n&&a.isIngestUrl(n))return e(t);let o=typeof t.request?.headers?.get=="function"&&t.request.headers.get("user-agent")||"",u=b(t.request,"x-devinspector-request-id")||a.createRequestId(),d=b(t.request,"x-devinspector-trace-id")||a.createTraceId(),c=b(t.request,"x-devinspector-span-id")||a.createSpanId();a.addBreadcrumb({type:"http",message:`${s} ${n}`,metadata:{framework:"SvelteKit",phase:"start",requestId:u,traceId:d,spanId:c,route:i}});try{let p=await e(t),f=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-r));return(p.status>=400||f>=a.getSlowThresholdMs())&&a.captureRequest({method:s,url:n,statusCode:p.status,durationMs:f,route:i,userAgent:o,requestId:u,traceId:d,spanId:c}),a.addBreadcrumb({type:"http",message:`${s} ${n}`,metadata:{framework:"SvelteKit",phase:"complete",statusCode:p.status,durationMs:f,requestId:u,traceId:d,spanId:c,route:i}}),p}catch(p){let f=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-r));a.captureRequest({method:s,url:n,statusCode:500,durationMs:f,route:i,userAgent:o,requestId:u,traceId:d,spanId:c}),a.addBreadcrumb({type:"http_error",message:`${s} ${n}`,metadata:{framework:"SvelteKit",statusCode:500,durationMs:f,requestId:u,traceId:d,spanId:c,route:i}});let l=p instanceof Error?p:new Error(String(p));throw a.captureError(l,{framework:"SvelteKit",url:n,method:s,route:i,requestId:u,traceId:d,spanId:c}),p}}function H({error:t,event:e,status:r,message:s}){let n=t instanceof Error?t:new Error(String(t||s||"Erro SvelteKit")),i=b(e?.request,"x-devinspector-request-id")||a.createRequestId(),o=b(e?.request,"x-devinspector-trace-id")||a.createTraceId(),u=b(e?.request,"x-devinspector-span-id")||a.createSpanId();a.addBreadcrumb({type:"sveltekit_error",message:n.message||"Erro SvelteKit",metadata:{statusCode:r||500,url:e?.url?.href||"",route:e?.route?.id||"",requestId:i,traceId:o,spanId:u}}),a.captureError(n,{framework:"SvelteKit",statusCode:r||500,url:e?.url?.href||"",route:e?.route?.id||"",requestId:i,traceId:o,spanId:u})}function $(){if(typeof globalThis>"u")return;let t=globalThis;if(t.__DEVINSPECTOR_FETCH_PATCHED__)return;let e=globalThis.fetch;if(typeof e!="function")return;let r;try{r=Object.getOwnPropertyDescriptor(globalThis,"fetch")}catch{return}if(r&&r.get&&!r.set||r&&r.writable===!1&&!r.set)return;let s=e.bind(globalThis),n=function(i,o){return _(i,o,{errorType:"svelte_network_error",defaultUserAgent:"SvelteKit",nativeFetch:s})};try{r&&r.configurable?Object.defineProperty(globalThis,"fetch",{...r,value:n}):globalThis.fetch=n}catch{return}globalThis.fetch===n&&(t.__DEVINSPECTOR_NATIVE_FETCH__=s,t.__DEVINSPECTOR_FETCH_PATCHED__=!0)}function b(t,e){if(!t?.headers)return;if(typeof t.headers.get=="function")return t.headers.get(e)||void 0;let r=t.headers[e]??t.headers[e.toLowerCase()];return Array.isArray(r)?r[0]:r?String(r):void 0}0&&(module.exports={audit,auditFetch,auditHandle,enableNetworkTracing,handleSvelteKitError,initSvelteDevInspector});
package/dist/svelte.js CHANGED
@@ -1 +1 @@
1
- var _="https://api.devinspector.com.br/api/ingest/track",M="https://api.devinspector.com.br/api/ingest/apm";var U=["password","senha","token","authorization","auth","bearer","credit_card","creditcard","cartao","cvv","cpf","rg","secret","private_key","privatekey"];function D(t){let e=t.toLowerCase().replace(/[\s-]/g,"_");return U.some(r=>e.includes(r))}function h(t,e=0,r=new WeakSet){if(t==null)return t;if(typeof t=="string")return t.length>1e4?`${t.slice(0,1e4)}...[truncated]`:t;if(typeof t=="number"||typeof t=="boolean")return t;if(typeof t=="bigint")return String(t);if(typeof t=="function")return"[Function]";if(e>=6)return"[MaxDepth]";if(typeof t!="object")return String(t);if(r.has(t))return"[Circular]";r.add(t);try{if(Array.isArray(t))return t.slice(0,100).map(i=>h(i,e+1,r));let s=t,n={};for(let[i,o]of Object.entries(s)){if(D(i)){n[i]="[REDACTED]";continue}n[i]=h(o,e+1,r)}return n}finally{r.delete(t)}}function w(t){if(t instanceof Error)return t.message||"Erro desconhecido";if(typeof t=="string")return t||"Erro desconhecido";try{let e=h(t);return typeof e=="string"?e||"Erro desconhecido":JSON.stringify(e)||"Erro desconhecido"}catch{return"Erro desconhecido"}}function I(t){return t&&String(t).trim().toUpperCase()||void 0}function R(t){if(!(typeof t!="number"||!Number.isFinite(t)))return Math.round(t)}function S(t){if(!(typeof t!="number"||!Number.isFinite(t)))return Math.max(0,Math.round(t))}function k(t){return String(t??"").trim().slice(0,200)}function P(t){let e=Number(t);if(!Number.isFinite(e))throw new Error("metricValue deve ser um n\xFAmero finito.");return e}function y(t){let e=typeof globalThis<"u"?globalThis.crypto:void 0;return e&&typeof e.randomUUID=="function"?`${t}-${e.randomUUID()}`:`${t}-${Date.now()}-${Math.random().toString(36).slice(2,12)}`}function l(t){return h(t)}var T=class{constructor(){this.apiKey="";this.endpoint=_;this.apmEndpoint=M;this.environment="production";this.slowThresholdMs=300;this.requestTimeoutMs=1e4;this.breadcrumbs=[];this.initialized=!1;this.listenersAttached=!1;this.sendQueue=[];this.isProcessingQueue=!1;this.flushResolvers=[]}init(e){if(!e?.apiKey?.trim())throw new Error("DevInspector: apiKey \xE9 obrigat\xF3rio.");this.apiKey=e.apiKey.trim(),this.endpoint=e.endpoint?.trim()||_,this.apmEndpoint=e.apmEndpoint?.trim()||M,this.environment=e.environment?.trim()||"production",this.release=e.release?.trim()||void 0,this.slowThresholdMs=typeof e.slowThresholdMs=="number"&&Number.isFinite(e.slowThresholdMs)&&e.slowThresholdMs>=0?Math.round(e.slowThresholdMs):300,this.requestTimeoutMs=typeof e.requestTimeoutMs=="number"&&Number.isFinite(e.requestTimeoutMs)&&e.requestTimeoutMs>0?Math.round(e.requestTimeoutMs):1e4,this.initialized=!0,this.attachGlobalListeners(),this.processQueue()}getApiKey(){return this.apiKey}getEndpoint(){return this.endpoint}getApmEndpoint(){return this.apmEndpoint}getEnvironment(){return this.environment}getRelease(){return this.release}getSlowThresholdMs(){return this.slowThresholdMs}getRequestTimeoutMs(){return this.requestTimeoutMs}isInitialized(){return this.initialized}setUser(e){this.user=e?h(e):void 0}getUser(){return this.user}clearUser(){this.user=void 0}addBreadcrumb(e){if(!e||!e.type?.trim())return;let r={timestamp:e.timestamp||new Date().toISOString(),type:e.type.trim().slice(0,200),message:e.message?.slice(0,1e4),metadata:e.metadata?h(e.metadata):void 0};this.breadcrumbs.push(r),this.breadcrumbs.length>50&&(this.breadcrumbs=this.breadcrumbs.slice(-50))}getBreadcrumbs(){return this.breadcrumbs.map(e=>l(e))}clearBreadcrumbs(){this.breadcrumbs=[]}getContext(){return{environment:this.environment,release:this.release,user:l(this.user),breadcrumbs:this.getBreadcrumbs()}}isIngestUrl(e){if(!e)return!1;try{let r=new URL(this.endpoint),s=new URL(this.apmEndpoint),n=new URL(e,r.origin),i=n.origin,o=n.pathname.replace(/\/$/,""),u=r.pathname.replace(/\/$/,""),d=s.pathname.replace(/\/$/,"");return i===r.origin&&o===u||i===s.origin&&o===d}catch{return e.includes(this.endpoint)||e.includes(this.apmEndpoint)}}createRequestId(){return y("req")}createTraceId(){return y("trace")}createSpanId(){return y("span")}captureRequest(e){let r=I(e.method)||"GET",s=String(e.url||"").trim();if(!s)return Promise.resolve();let n=R(e.statusCode)??0,i=S(e.durationMs)??0,o={...e.metadata||{},...e.route!==void 0?{route:e.route}:{},...e.dbQueriesCount!==void 0?{dbQueriesCount:e.dbQueriesCount}:{},...e.slowQueryMs!==void 0?{slowQueryMs:e.slowQueryMs}:{},timestamp:new Date().toISOString()};return this.sendApm({type:"request_metric",method:r,url:s,statusCode:n,durationMs:i,browser:e.userAgent,requestId:e.requestId,traceId:e.traceId,spanId:e.spanId,environment:this.environment,release:this.release,user:l(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:l(o)})}captureDbQuery(e){let r=String(e.operation||"").trim();if(!r)return Promise.resolve();let s=S(e.durationMs)??0,n={...e.metadata||{},operation:r,collection:e.collection||"Unknown",durationMs:s,timestamp:new Date().toISOString()};return this.sendApm({type:"db_query",message:`Database operation: ${r}`,url:e.url,method:"DB",durationMs:s,requestId:e.requestId,traceId:e.traceId,spanId:e.spanId,environment:this.environment,release:this.release,user:l(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:l(n)})}captureMetric(e,r,s={},n={}){let i=k(e);if(!i)return Promise.resolve();let o;try{o=P(r)}catch{return Promise.resolve()}return this.sendApm({type:"custom_metric",metricName:i,metricValue:o,requestId:n.requestId,traceId:n.traceId,spanId:n.spanId,environment:this.environment,release:this.release,user:l(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:l({...s,timestamp:new Date().toISOString()})})}captureCustomMetric(e){return this.captureMetric(e.metricName,e.metricValue,e.metadata||{},{requestId:e.requestId,traceId:e.traceId,spanId:e.spanId})}captureError(e,r={}){let s=e?.message?.trim()||"Erro desconhecido",n={...r},i=I(n.method),o=R(n.statusCode??n.status),u=n.url!==void 0&&n.url!==null?String(n.url).trim():void 0,d=typeof n.requestId=="string"&&n.requestId.trim()||void 0,c=typeof n.traceId=="string"&&n.traceId.trim()||void 0,p=typeof n.spanId=="string"&&n.spanId.trim()||void 0;return delete n.method,delete n.statusCode,delete n.status,delete n.requestId,delete n.traceId,delete n.spanId,delete n.durationMs,this.sendError({type:"error",message:s,stackTrace:e?.stack||"",...u?{url:u}:{},...i?{method:i}:{},...o!==void 0?{statusCode:o}:{},...d?{requestId:d}:{},...c?{traceId:c}:{},...p?{spanId:p}:{},environment:this.environment,release:this.release,user:l(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:l({...n,timestamp:new Date().toISOString()})})}captureException(e,r={}){let s=e instanceof Error?e:new Error(w(e));return this.captureError(s,r)}captureMessage(e,r={}){let s=String(e||"").trim();return s?this.sendApm({type:"message",message:s.slice(0,1e4),environment:this.environment,release:this.release,user:l(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:l({...r,timestamp:new Date().toISOString()})}):Promise.resolve()}async flush(){if(!(this.sendQueue.length===0&&!this.isProcessingQueue))return new Promise(e=>{this.flushResolvers.push(e),this.processQueue()})}attachGlobalListeners(){if(this.listenersAttached||typeof globalThis>"u")return;this.listenersAttached=!0;let e=globalThis;if(typeof e.ErrorUtils<"u"){let r=e.ErrorUtils.getGlobalHandler?.();e.ErrorUtils.setGlobalHandler?.((s,n)=>{this.captureError(s,{platform:"react-native",fatal:n}).finally(()=>{typeof r=="function"&&r(s,n)})})}if(typeof window<"u"&&typeof window.addEventListener=="function"){window.addEventListener("error",r=>{let s=r.error instanceof Error?r.error:new Error(r.message||"Erro global");this.captureError(s,{url:r.filename||window.location.href,line:r.lineno,column:r.colno,platform:"browser"})}),window.addEventListener("unhandledrejection",r=>{let s=r.reason instanceof Error?r.reason:new Error(w(r.reason));this.captureError(s,{platform:"browser",url:window.location.href})});return}typeof process<"u"&&typeof process.on=="function"&&(process.on("uncaughtException",r=>{this.captureError(r,{platform:"node"})}),process.on("unhandledRejection",r=>{this.captureException(r,{platform:"node"})}))}sendError(e){return this.enqueue(this.endpoint,e)}sendApm(e){return this.enqueue(this.apmEndpoint,e)}enqueue(e,r){if(!this.initialized||!this.apiKey)return Promise.resolve();let s={id:y("event"),endpoint:e,payload:l(r),resolve:()=>{}};return new Promise(n=>{s.resolve=n,this.sendQueue.push(s),this.processQueue()})}async processQueue(){if(!(this.isProcessingQueue||!this.initialized||!this.apiKey)){this.isProcessingQueue=!0;try{for(;this.sendQueue.length>0;){let e=this.sendQueue.shift();if(e)try{await this.sendToEndpoint(e.endpoint,e.payload)}finally{e.resolve()}}}finally{if(this.isProcessingQueue=!1,this.sendQueue.length===0){let e=this.flushResolvers.splice(0);for(let r of e)r()}this.sendQueue.length>0&&this.processQueue()}}}async sendToEndpoint(e,r){let s=typeof fetch=="function"?fetch:void 0;if(!s)return;let n=h(r),i,o;try{typeof AbortController<"u"&&(i=new AbortController,o=setTimeout(()=>{i?.abort()},this.requestTimeoutMs));let u=typeof window<"u"&&typeof window.document<"u",d={method:"POST",headers:{"content-type":"application/json","x-api-key":this.apiKey},body:JSON.stringify(n),signal:i?.signal};if(u&&(d.keepalive=!0),!(await s(e,d)).ok)return}catch{return}finally{o!==void 0&&clearTimeout(o)}}},A="__DEVINSPECT_AUDIT_CORE_INSTANCE__",q=typeof globalThis<"u"?globalThis:{},a=q[A]||new T;q[A]=a;async function v(t,e,r={}){let s=r.nativeFetch||(typeof fetch=="function"?fetch:void 0);if(!s)throw new Error("fetch n\xE3o est\xE1 dispon\xEDvel.");let n=typeof t=="string"?t:t instanceof URL?t.toString():String(t.url||""),i=I(e?.method||(typeof t!="string"&&!(t instanceof URL)?t.method:"GET"))||"GET";if(n&&a.isIngestUrl(n))return s(t,e);let o=a.createRequestId(),u=a.createTraceId(),d=a.createSpanId(),c=new Headers(e?.headers||(typeof t!="string"&&!(t instanceof URL)?t.headers:void 0));c.set("x-devinspector-request-id",o),c.set("x-devinspector-trace-id",u),c.set("x-devinspector-span-id",d);let p={...e,headers:c},f=typeof performance<"u"?performance.now():Date.now();try{let m=await s(t,p),g=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-f)),E=m.status>=400||g>=a.getSlowThresholdMs();return a.addBreadcrumb({type:"http",message:`${i} ${n}`,metadata:{phase:"complete",statusCode:m.status,durationMs:g,requestId:o,traceId:u,spanId:d}}),E&&a.captureRequest({method:i,url:n,statusCode:m.status,durationMs:g,userAgent:typeof navigator<"u"?navigator.userAgent:r.defaultUserAgent,requestId:o,traceId:u,spanId:d}),m}catch(m){let g=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-f));a.addBreadcrumb({type:"http_error",message:`${i} ${n}`,metadata:{phase:"error",statusCode:0,durationMs:g,requestId:o,traceId:u,spanId:d}}),a.captureRequest({method:i,url:n,statusCode:0,durationMs:g,userAgent:typeof navigator<"u"?navigator.userAgent:r.defaultUserAgent,requestId:o,traceId:u,spanId:d});let E=m instanceof Error?m:new Error(w(m));throw a.captureError(E,{url:n,method:i,type:r.errorType||"network_error",requestId:o,traceId:u,spanId:d}),m}}function N(t){a.init(t)}async function x(t,e){return v(t,e,{errorType:"svelte_network_error",defaultUserAgent:"SvelteKit"})}async function Q({event:t,resolve:e}){let r=typeof performance<"u"?performance.now():Date.now(),s=t.request?.method||"GET",n=t.url?.href||t.url?.pathname||"/",i=t.route?.id||n;if(n&&a.isIngestUrl(n))return e(t);let o=typeof t.request?.headers?.get=="function"&&t.request.headers.get("user-agent")||"",u=b(t.request,"x-devinspector-request-id")||a.createRequestId(),d=b(t.request,"x-devinspector-trace-id")||a.createTraceId(),c=b(t.request,"x-devinspector-span-id")||a.createSpanId();a.addBreadcrumb({type:"http",message:`${s} ${n}`,metadata:{framework:"SvelteKit",phase:"start",requestId:u,traceId:d,spanId:c,route:i}});try{let p=await e(t),f=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-r));return(p.status>=400||f>=a.getSlowThresholdMs())&&a.captureRequest({method:s,url:n,statusCode:p.status,durationMs:f,route:i,userAgent:o,requestId:u,traceId:d,spanId:c}),a.addBreadcrumb({type:"http",message:`${s} ${n}`,metadata:{framework:"SvelteKit",phase:"complete",statusCode:p.status,durationMs:f,requestId:u,traceId:d,spanId:c,route:i}}),p}catch(p){let f=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-r));a.captureRequest({method:s,url:n,statusCode:500,durationMs:f,route:i,userAgent:o,requestId:u,traceId:d,spanId:c}),a.addBreadcrumb({type:"http_error",message:`${s} ${n}`,metadata:{framework:"SvelteKit",statusCode:500,durationMs:f,requestId:u,traceId:d,spanId:c,route:i}});let m=p instanceof Error?p:new Error(String(p));throw a.captureError(m,{framework:"SvelteKit",url:n,method:s,route:i,requestId:u,traceId:d,spanId:c}),p}}function B({error:t,event:e,status:r,message:s}){let n=t instanceof Error?t:new Error(String(t||s||"Erro SvelteKit")),i=b(e?.request,"x-devinspector-request-id")||a.createRequestId(),o=b(e?.request,"x-devinspector-trace-id")||a.createTraceId(),u=b(e?.request,"x-devinspector-span-id")||a.createSpanId();a.addBreadcrumb({type:"sveltekit_error",message:n.message||"Erro SvelteKit",metadata:{statusCode:r||500,url:e?.url?.href||"",route:e?.route?.id||"",requestId:i,traceId:o,spanId:u}}),a.captureError(n,{framework:"SvelteKit",statusCode:r||500,url:e?.url?.href||"",route:e?.route?.id||"",requestId:i,traceId:o,spanId:u})}function L(){if(typeof globalThis>"u")return;let t=globalThis;if(t.__DEVINSPECTOR_FETCH_PATCHED__)return;let e=globalThis.fetch;if(typeof e!="function")return;let r;try{r=Object.getOwnPropertyDescriptor(globalThis,"fetch")}catch{return}if(r&&r.get&&!r.set||r&&r.writable===!1&&!r.set)return;let s=e.bind(globalThis),n=function(i,o){return v(i,o,{errorType:"svelte_network_error",defaultUserAgent:"SvelteKit",nativeFetch:s})};try{r&&r.configurable?Object.defineProperty(globalThis,"fetch",{...r,value:n}):globalThis.fetch=n}catch{return}globalThis.fetch===n&&(t.__DEVINSPECTOR_NATIVE_FETCH__=s,t.__DEVINSPECTOR_FETCH_PATCHED__=!0)}function b(t,e){if(!t?.headers)return;if(typeof t.headers.get=="function")return t.headers.get(e)||void 0;let r=t.headers[e]??t.headers[e.toLowerCase()];return Array.isArray(r)?r[0]:r?String(r):void 0}export{a as audit,x as auditFetch,Q as auditHandle,L as enableNetworkTracing,B as handleSvelteKitError,N as initSvelteDevInspector};
1
+ var _="https://api.devinspector.com.br/api/ingest/track",S="https://api.devinspector.com.br/api/ingest/apm";var D=["password","senha","token","authorization","auth","bearer","credit_card","creditcard","cartao","cvv","cpf","rg","secret","private_key","privatekey"];function k(t){let e=t.toLowerCase().replace(/[\s-]/g,"_");return D.some(r=>e.includes(r))}function h(t,e=0,r=new WeakSet){if(t==null)return t;if(typeof t=="string")return t.length>1e4?`${t.slice(0,1e4)}...[truncated]`:t;if(typeof t=="number"||typeof t=="boolean")return t;if(typeof t=="bigint")return String(t);if(typeof t=="function")return"[Function]";if(e>=6)return"[MaxDepth]";if(typeof t!="object")return String(t);if(r.has(t))return"[Circular]";r.add(t);try{if(Array.isArray(t))return t.slice(0,100).map(i=>h(i,e+1,r));let s=t,n={};for(let[i,o]of Object.entries(s)){if(k(i)){n[i]="[REDACTED]";continue}n[i]=h(o,e+1,r)}return n}finally{r.delete(t)}}function w(t){if(t instanceof Error)return t.message||"Erro desconhecido";if(typeof t=="string")return t||"Erro desconhecido";try{let e=h(t);return typeof e=="string"?e||"Erro desconhecido":JSON.stringify(e)||"Erro desconhecido"}catch{return"Erro desconhecido"}}function T(t){return t&&String(t).trim().toUpperCase()||void 0}function R(t){if(!(typeof t!="number"||!Number.isFinite(t)))return Math.round(t)}function I(t){if(!(typeof t!="number"||!Number.isFinite(t)))return Math.max(0,Math.round(t))}function A(t){return t==null?void 0:String(t).trim().slice(0,100)||void 0}function P(t){return String(t??"").trim().slice(0,200)}function C(t){let e=Number(t);if(!Number.isFinite(e))throw new Error("metricValue deve ser um n\xFAmero finito.");return e}function y(t){let e=typeof globalThis<"u"?globalThis.crypto:void 0;return e&&typeof e.randomUUID=="function"?`${t}-${e.randomUUID()}`:`${t}-${Date.now()}-${Math.random().toString(36).slice(2,12)}`}function m(t){return h(t)}var v=class{constructor(){this.apiKey="";this.endpoint=_;this.apmEndpoint=S;this.environment="production";this.slowThresholdMs=300;this.requestTimeoutMs=1e4;this.breadcrumbs=[];this.initialized=!1;this.listenersAttached=!1;this.sendQueue=[];this.isProcessingQueue=!1;this.flushResolvers=[]}init(e){if(!e?.apiKey?.trim())throw new Error("DevInspector: apiKey \xE9 obrigat\xF3rio.");this.apiKey=e.apiKey.trim(),this.endpoint=e.endpoint?.trim()||_,this.apmEndpoint=e.apmEndpoint?.trim()||S,this.environment=e.environment?.trim()||"production",this.release=e.release?.trim()||void 0,this.slowThresholdMs=typeof e.slowThresholdMs=="number"&&Number.isFinite(e.slowThresholdMs)&&e.slowThresholdMs>=0?Math.round(e.slowThresholdMs):300,this.requestTimeoutMs=typeof e.requestTimeoutMs=="number"&&Number.isFinite(e.requestTimeoutMs)&&e.requestTimeoutMs>0?Math.round(e.requestTimeoutMs):1e4,this.initialized=!0,this.attachGlobalListeners(),this.processQueue()}getApiKey(){return this.apiKey}getEndpoint(){return this.endpoint}getApmEndpoint(){return this.apmEndpoint}getEnvironment(){return this.environment}getRelease(){return this.release}getSlowThresholdMs(){return this.slowThresholdMs}getRequestTimeoutMs(){return this.requestTimeoutMs}isInitialized(){return this.initialized}setUser(e){this.user=e?h(e):void 0}getUser(){return this.user}clearUser(){this.user=void 0}addBreadcrumb(e){if(!e||!e.type?.trim())return;let r={timestamp:e.timestamp||new Date().toISOString(),type:e.type.trim().slice(0,200),message:e.message?.slice(0,1e4),metadata:e.metadata?h(e.metadata):void 0};this.breadcrumbs.push(r),this.breadcrumbs.length>50&&(this.breadcrumbs=this.breadcrumbs.slice(-50))}getBreadcrumbs(){return this.breadcrumbs.map(e=>m(e))}clearBreadcrumbs(){this.breadcrumbs=[]}getContext(){return{environment:this.environment,release:this.release,user:m(this.user),breadcrumbs:this.getBreadcrumbs()}}isIngestUrl(e){if(!e)return!1;try{let r=new URL(this.endpoint),s=new URL(this.apmEndpoint),n=new URL(e,r.origin),i=n.origin,o=n.pathname.replace(/\/$/,""),u=r.pathname.replace(/\/$/,""),d=s.pathname.replace(/\/$/,"");return i===r.origin&&o===u||i===s.origin&&o===d}catch{return e.includes(this.endpoint)||e.includes(this.apmEndpoint)}}createRequestId(){return y("req")}createTraceId(){return y("trace")}createSpanId(){return y("span")}captureRequest(e){let r=T(e.method)||"GET",s=String(e.url||"").trim();if(!s)return Promise.resolve();let n=R(e.statusCode)??0,i=I(e.durationMs)??0,o=A(e.ip),u={...e.metadata||{},timestamp:new Date().toISOString()};return this.sendApm({type:"request_metric",method:r,url:s,statusCode:n,durationMs:i,...e.userAgent?{userAgent:e.userAgent}:{},...e.route!==void 0?{route:e.route}:{},...o?{ip:o}:{},...e.dbQueriesCount!==void 0?{dbQueriesCount:e.dbQueriesCount}:{},...e.slowQueryMs!==void 0?{slowQueryMs:e.slowQueryMs}:{},...e.requestId?{requestId:e.requestId}:{},...e.traceId?{traceId:e.traceId}:{},...e.spanId?{spanId:e.spanId}:{},environment:this.environment,release:this.release,user:m(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:m(u)})}captureDbQuery(e){let r=String(e.operation||"").trim();if(!r)return Promise.resolve();let s=I(e.durationMs)??0,n={...e.metadata||{},operation:r,collection:e.collection||"Unknown",durationMs:s,timestamp:new Date().toISOString()};return this.sendApm({type:"db_query",message:`Database operation: ${r}`,url:e.url,method:"DB",durationMs:s,requestId:e.requestId,traceId:e.traceId,spanId:e.spanId,environment:this.environment,release:this.release,user:m(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:m(n)})}captureMetric(e,r,s={},n={}){let i=P(e);if(!i)return Promise.resolve();let o;try{o=C(r)}catch{return Promise.resolve()}return this.sendApm({type:"custom_metric",metricName:i,metricValue:o,requestId:n.requestId,traceId:n.traceId,spanId:n.spanId,environment:this.environment,release:this.release,user:m(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:m({...s,timestamp:new Date().toISOString()})})}captureCustomMetric(e){return this.captureMetric(e.metricName,e.metricValue,e.metadata||{},{requestId:e.requestId,traceId:e.traceId,spanId:e.spanId})}captureError(e,r={}){let s=e?.message?.trim()||"Erro desconhecido",n={...r},i=T(n.method),o=R(n.statusCode??n.status),u=I(n.durationMs),d=A(n.ip),c=n.url!==void 0&&n.url!==null?String(n.url).trim():void 0,p=typeof n.requestId=="string"&&n.requestId.trim()||void 0,f=typeof n.traceId=="string"&&n.traceId.trim()||void 0,l=typeof n.spanId=="string"&&n.spanId.trim()||void 0;return delete n.method,delete n.statusCode,delete n.status,delete n.durationMs,delete n.ip,delete n.requestId,delete n.traceId,delete n.spanId,this.sendError({type:"error",message:s,stackTrace:e?.stack||"",...c?{url:c}:{},...i?{method:i}:{},...o!==void 0?{statusCode:o}:{},...u!==void 0?{durationMs:u}:{},...d?{ip:d}:{},...p?{requestId:p}:{},...f?{traceId:f}:{},...l?{spanId:l}:{},environment:this.environment,release:this.release,user:m(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:m({...n,timestamp:new Date().toISOString()})})}captureException(e,r={}){let s=e instanceof Error?e:new Error(w(e));return this.captureError(s,r)}captureMessage(e,r={}){let s=String(e||"").trim();return s?this.sendApm({type:"message",message:s.slice(0,1e4),environment:this.environment,release:this.release,user:m(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:m({...r,timestamp:new Date().toISOString()})}):Promise.resolve()}async flush(){if(!(this.sendQueue.length===0&&!this.isProcessingQueue))return new Promise(e=>{this.flushResolvers.push(e),this.processQueue()})}attachGlobalListeners(){if(this.listenersAttached||typeof globalThis>"u")return;this.listenersAttached=!0;let e=globalThis;if(typeof e.ErrorUtils<"u"){let r=e.ErrorUtils.getGlobalHandler?.();e.ErrorUtils.setGlobalHandler?.((s,n)=>{this.captureError(s,{platform:"react-native",fatal:n}).finally(()=>{typeof r=="function"&&r(s,n)})})}if(typeof window<"u"&&typeof window.addEventListener=="function"){window.addEventListener("error",r=>{let s=r.error instanceof Error?r.error:new Error(r.message||"Erro global");this.captureError(s,{url:r.filename||window.location.href,line:r.lineno,column:r.colno,platform:"browser"})}),window.addEventListener("unhandledrejection",r=>{let s=r.reason instanceof Error?r.reason:new Error(w(r.reason));this.captureError(s,{platform:"browser",url:window.location.href})});return}typeof process<"u"&&typeof process.on=="function"&&(process.on("uncaughtException",r=>{this.captureError(r,{platform:"node"})}),process.on("unhandledRejection",r=>{this.captureException(r,{platform:"node"})}))}sendError(e){return this.enqueue(this.endpoint,e)}sendApm(e){return this.enqueue(this.apmEndpoint,e)}enqueue(e,r){if(!this.initialized||!this.apiKey)return Promise.resolve();let s={id:y("event"),endpoint:e,payload:m(r),resolve:()=>{}};return new Promise(n=>{s.resolve=n,this.sendQueue.push(s),this.processQueue()})}async processQueue(){if(!(this.isProcessingQueue||!this.initialized||!this.apiKey)){this.isProcessingQueue=!0;try{for(;this.sendQueue.length>0;){let e=this.sendQueue.shift();if(e)try{await this.sendToEndpoint(e.endpoint,e.payload)}finally{e.resolve()}}}finally{if(this.isProcessingQueue=!1,this.sendQueue.length===0){let e=this.flushResolvers.splice(0);for(let r of e)r()}this.sendQueue.length>0&&this.processQueue()}}}async sendToEndpoint(e,r){let s=typeof fetch=="function"?fetch:void 0;if(!s)return;let n=h(r),i,o;try{typeof AbortController<"u"&&(i=new AbortController,o=setTimeout(()=>{i?.abort()},this.requestTimeoutMs));let u=typeof window<"u"&&typeof window.document<"u",d={method:"POST",headers:{"content-type":"application/json","x-api-key":this.apiKey},body:JSON.stringify(n),signal:i?.signal};if(u&&(d.keepalive=!0),!(await s(e,d)).ok)return}catch{return}finally{o!==void 0&&clearTimeout(o)}}},q="__DEVINSPECT_AUDIT_CORE_INSTANCE__",U=typeof globalThis<"u"?globalThis:{},a=U[q]||new v;U[q]=a;async function M(t,e,r={}){let s=r.nativeFetch||(typeof fetch=="function"?fetch:void 0);if(!s)throw new Error("fetch n\xE3o est\xE1 dispon\xEDvel.");let n=typeof t=="string"?t:t instanceof URL?t.toString():String(t.url||""),i=T(e?.method||(typeof t!="string"&&!(t instanceof URL)?t.method:"GET"))||"GET";if(n&&a.isIngestUrl(n))return s(t,e);let o=a.createRequestId(),u=a.createTraceId(),d=a.createSpanId(),c=new Headers(e?.headers||(typeof t!="string"&&!(t instanceof URL)?t.headers:void 0));c.set("x-devinspector-request-id",o),c.set("x-devinspector-trace-id",u),c.set("x-devinspector-span-id",d);let p={...e,headers:c},f=typeof performance<"u"?performance.now():Date.now();try{let l=await s(t,p),g=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-f)),E=l.status>=400||g>=a.getSlowThresholdMs();return a.addBreadcrumb({type:"http",message:`${i} ${n}`,metadata:{phase:"complete",statusCode:l.status,durationMs:g,requestId:o,traceId:u,spanId:d}}),E&&a.captureRequest({method:i,url:n,statusCode:l.status,durationMs:g,userAgent:typeof navigator<"u"?navigator.userAgent:r.defaultUserAgent,requestId:o,traceId:u,spanId:d}),l}catch(l){let g=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-f));a.addBreadcrumb({type:"http_error",message:`${i} ${n}`,metadata:{phase:"error",statusCode:0,durationMs:g,requestId:o,traceId:u,spanId:d}}),a.captureRequest({method:i,url:n,statusCode:0,durationMs:g,userAgent:typeof navigator<"u"?navigator.userAgent:r.defaultUserAgent,requestId:o,traceId:u,spanId:d});let E=l instanceof Error?l:new Error(w(l));throw a.captureError(E,{url:n,method:i,type:r.errorType||"network_error",requestId:o,traceId:u,spanId:d}),l}}function x(t){a.init(t)}async function Q(t,e){return M(t,e,{errorType:"svelte_network_error",defaultUserAgent:"SvelteKit"})}async function B({event:t,resolve:e}){let r=typeof performance<"u"?performance.now():Date.now(),s=t.request?.method||"GET",n=t.url?.href||t.url?.pathname||"/",i=t.route?.id||n;if(n&&a.isIngestUrl(n))return e(t);let o=typeof t.request?.headers?.get=="function"&&t.request.headers.get("user-agent")||"",u=b(t.request,"x-devinspector-request-id")||a.createRequestId(),d=b(t.request,"x-devinspector-trace-id")||a.createTraceId(),c=b(t.request,"x-devinspector-span-id")||a.createSpanId();a.addBreadcrumb({type:"http",message:`${s} ${n}`,metadata:{framework:"SvelteKit",phase:"start",requestId:u,traceId:d,spanId:c,route:i}});try{let p=await e(t),f=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-r));return(p.status>=400||f>=a.getSlowThresholdMs())&&a.captureRequest({method:s,url:n,statusCode:p.status,durationMs:f,route:i,userAgent:o,requestId:u,traceId:d,spanId:c}),a.addBreadcrumb({type:"http",message:`${s} ${n}`,metadata:{framework:"SvelteKit",phase:"complete",statusCode:p.status,durationMs:f,requestId:u,traceId:d,spanId:c,route:i}}),p}catch(p){let f=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-r));a.captureRequest({method:s,url:n,statusCode:500,durationMs:f,route:i,userAgent:o,requestId:u,traceId:d,spanId:c}),a.addBreadcrumb({type:"http_error",message:`${s} ${n}`,metadata:{framework:"SvelteKit",statusCode:500,durationMs:f,requestId:u,traceId:d,spanId:c,route:i}});let l=p instanceof Error?p:new Error(String(p));throw a.captureError(l,{framework:"SvelteKit",url:n,method:s,route:i,requestId:u,traceId:d,spanId:c}),p}}function L({error:t,event:e,status:r,message:s}){let n=t instanceof Error?t:new Error(String(t||s||"Erro SvelteKit")),i=b(e?.request,"x-devinspector-request-id")||a.createRequestId(),o=b(e?.request,"x-devinspector-trace-id")||a.createTraceId(),u=b(e?.request,"x-devinspector-span-id")||a.createSpanId();a.addBreadcrumb({type:"sveltekit_error",message:n.message||"Erro SvelteKit",metadata:{statusCode:r||500,url:e?.url?.href||"",route:e?.route?.id||"",requestId:i,traceId:o,spanId:u}}),a.captureError(n,{framework:"SvelteKit",statusCode:r||500,url:e?.url?.href||"",route:e?.route?.id||"",requestId:i,traceId:o,spanId:u})}function F(){if(typeof globalThis>"u")return;let t=globalThis;if(t.__DEVINSPECTOR_FETCH_PATCHED__)return;let e=globalThis.fetch;if(typeof e!="function")return;let r;try{r=Object.getOwnPropertyDescriptor(globalThis,"fetch")}catch{return}if(r&&r.get&&!r.set||r&&r.writable===!1&&!r.set)return;let s=e.bind(globalThis),n=function(i,o){return M(i,o,{errorType:"svelte_network_error",defaultUserAgent:"SvelteKit",nativeFetch:s})};try{r&&r.configurable?Object.defineProperty(globalThis,"fetch",{...r,value:n}):globalThis.fetch=n}catch{return}globalThis.fetch===n&&(t.__DEVINSPECTOR_NATIVE_FETCH__=s,t.__DEVINSPECTOR_FETCH_PATCHED__=!0)}function b(t,e){if(!t?.headers)return;if(typeof t.headers.get=="function")return t.headers.get(e)||void 0;let r=t.headers[e]??t.headers[e.toLowerCase()];return Array.isArray(r)?r[0]:r?String(r):void 0}export{a as audit,Q as auditFetch,B as auditHandle,F as enableNetworkTracing,L as handleSvelteKitError,x as initSvelteDevInspector};