@xcodleox/devinspector 9.3.1 → 9.3.2

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/angular.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var b=Object.defineProperty;var z=Object.getOwnPropertyDescriptor;var N=Object.getOwnPropertyNames;var L=Object.prototype.hasOwnProperty;var H=(t,e)=>{for(var n in e)b(t,n,{get:e[n],enumerable:!0})},B=(t,e,n,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of N(e))!L.call(t,r)&&r!==n&&b(t,r,{get:()=>e[r],enumerable:!(i=z(e,r))||i.enumerable});return t};var V=t=>B(b({},"__esModule",{value:!0}),t);var Z={};H(Z,{AuditAngularErrorHandler:()=>h,audit:()=>c,auditInterceptor:()=>Y,devInspectorInterceptor:()=>Q});module.exports=V(Z);var _=require("@angular/core"),T=require("@angular/common/http"),O=require("rxjs/operators");var A="9.3.1",$=new Set(["angular","vanilla-js","react","vue","svelte","node","python","java","csharp","php","nextjs","react-native"]),R="https://api.devinspector.com.br/api/ingest/track",k="https://api.devinspector.com.br/api/ingest/apm",M=50,v=1e4,j=6,S=1e4,F=5e3,G=["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 G.some(n=>e.includes(n))}function f(t,e=0,n=new WeakSet){if(t==null)return t;if(typeof t=="string")return t.length>v?`${t.slice(0,v)}...[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>=j)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(s=>f(s,e+1,n));let i=t,r={};for(let[s,o]of Object.entries(i)){if(K(s)){r[s]="[REDACTED]";continue}r[s]=f(o,e+1,n)}return r}finally{n.delete(t)}}function q(t){if(t instanceof Error)return t.message||"Erro desconhecido";if(typeof t=="string")return t||"Erro desconhecido";try{let e=f(t);return typeof e=="string"?e||"Erro desconhecido":JSON.stringify(e)||"Erro desconhecido"}catch{return"Erro desconhecido"}}function U(t){return t&&String(t).trim().toUpperCase()||void 0}function D(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 C(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 J(t){let e=Number(t);if(!Number.isFinite(e))throw new Error("metricValue deve ser um n\xFAmero finito.");return e}function I(t,e=200){return t==null?void 0:String(t).trim().slice(0,e)||void 0}function W(t){let e=I(t,50);if(e&&$.has(e))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 u(t){return f(t)}var E=class{constructor(){this.apiKey="";this.endpoint=R;this.apmEndpoint=k;this.environment="production";this.sdkVersion=A;this.slowThresholdMs=300;this.requestTimeoutMs=S;this.breadcrumbs=[];this.initialized=!1;this.listenersAttached=!1;this.sendQueue=[];this.isProcessingQueue=!1;this.flushResolvers=[];this.connectionTestLastCheckedAt=0}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()||k,this.environment=e.environment?.trim()||"production",this.release=e.release?.trim()||void 0,this.technology=W(e.technology),this.installationId=I(e.installationId,200),this.sdkVersion=I(e.sdkVersion,100)||A,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):S,this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0,this.connectionTestLastCheckedAt=0,this.connectionTestRequest=void 0,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}getTechnology(){return this.technology}getInstallationId(){return this.installationId}getSdkVersion(){return this.sdkVersion}getSlowThresholdMs(){return this.slowThresholdMs}getRequestTimeoutMs(){return this.requestTimeoutMs}isInitialized(){return this.initialized}setUser(e){this.user=e?f(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,v),metadata:e.metadata?f(e.metadata):void 0};this.breadcrumbs.push(n),this.breadcrumbs.length>M&&(this.breadcrumbs=this.breadcrumbs.slice(-M))}getBreadcrumbs(){return this.breadcrumbs.map(e=>u(e))}clearBreadcrumbs(){this.breadcrumbs=[]}getContext(){return{environment:this.environment,release:this.release,technology:this.technology,installationId:this.installationId,sdkVersion:this.sdkVersion,user:u(this.user),breadcrumbs:this.getBreadcrumbs()}}isIngestUrl(e){if(!e)return!1;try{let n=new URL(this.endpoint),i=new URL(this.apmEndpoint),r=new URL(e,n.origin),s=r.origin,o=r.pathname.replace(/\/$/,""),d=n.pathname.replace(/\/$/,""),a=i.pathname.replace(/\/$/,"");return s===n.origin&&o===d||s===i.origin&&o===a}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 n=U(e.method)||"GET",i=String(e.url||"").trim();if(!i)return Promise.resolve();let r=D(e.statusCode)??0,s=w(e.durationMs)??0,o=C(e.ip),d={...e.metadata||{},timestamp:new Date().toISOString()};return this.sendApm({type:"request_metric",method:n,url:i,statusCode:r,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:u(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:u(d)})}captureDbQuery(e){let n=String(e.operation||"").trim();if(!n)return Promise.resolve();let i=w(e.durationMs)??0,r={...e.metadata||{},operation:n,collection:e.collection||"Unknown",durationMs:i,timestamp:new Date().toISOString()};return this.sendApm({type:"db_query",message:`Database operation: ${n}`,url:e.url,method:"DB",durationMs:i,requestId:e.requestId,traceId:e.traceId,spanId:e.spanId,environment:this.environment,release:this.release,user:u(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:u(r)})}captureMetric(e,n,i={},r={}){let s=X(e);if(!s)return Promise.resolve();let o;try{o=J(n)}catch{return Promise.resolve()}return this.sendApm({type:"custom_metric",metricName:s,metricValue:o,requestId:r.requestId,traceId:r.traceId,spanId:r.spanId,environment:this.environment,release:this.release,user:u(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:u({...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,n={}){let i=e?.message?.trim()||"Erro desconhecido",r={...n},s=U(r.method),o=D(r.statusCode??r.status),d=w(r.durationMs),a=C(r.ip),p=r.url!==void 0&&r.url!==null?String(r.url).trim():void 0,l=typeof r.requestId=="string"&&r.requestId.trim()||void 0,m=typeof r.traceId=="string"&&r.traceId.trim()||void 0,g=typeof r.spanId=="string"&&r.spanId.trim()||void 0;return delete r.method,delete r.statusCode,delete r.status,delete r.durationMs,delete r.ip,delete r.requestId,delete r.traceId,delete r.spanId,this.sendError({type:"error",message:i,stackTrace:e?.stack||"",...p?{url:p}:{},...s?{method:s}:{},...o!==void 0?{statusCode:o}:{},...d!==void 0?{durationMs:d}:{},...a?{ip:a}:{},...l?{requestId:l}:{},...m?{traceId:m}:{},...g?{spanId:g}:{},environment:this.environment,release:this.release,user:u(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:u({...r,timestamp:new Date().toISOString()})})}captureException(e,n={}){let i=e instanceof Error?e:new Error(q(e));return this.captureError(i,n)}captureMessage(e,n={}){let i=String(e||"").trim();return i?this.sendApm({type:"message",message:i.slice(0,v),environment:this.environment,release:this.release,user:u(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:u({...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()})}getIntegrationContext(){return{...this.technology?{technology:this.technology}:{},...this.installationId?{installationId:this.installationId}:{},...this.connectionTestId?{connectionTestId:this.connectionTestId}:{},sdkVersion:this.sdkVersion}}getPendingConnectionTestEndpoint(){try{let e=new URL(this.endpoint),n=new URL("/api/integrations/sdk/pending-test",e.origin);return this.technology&&n.searchParams.set("technology",this.technology),n.toString()}catch{return}}async refreshPendingConnectionTest(e=!1){if(!this.initialized||!this.apiKey||!this.technology)return;let n=Date.now();if(!e&&this.connectionTestLastCheckedAt>0&&n-this.connectionTestLastCheckedAt<F){this.connectionTestExpiresAt&&n>=this.connectionTestExpiresAt&&(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0);return}if(this.connectionTestRequest)return this.connectionTestRequest;let i=this.getPendingConnectionTestEndpoint();if(i)return this.connectionTestRequest=this.fetchPendingConnectionTest(i).finally(()=>{this.connectionTestRequest=void 0}),this.connectionTestRequest}async fetchPendingConnectionTest(e){this.connectionTestLastCheckedAt=Date.now();let n=typeof fetch=="function"?fetch:void 0;if(!n)return;let i,r;try{typeof AbortController<"u"&&(i=new AbortController,r=setTimeout(()=>{i?.abort()},this.requestTimeoutMs));let s=await n(e,{method:"GET",headers:{"x-api-key":this.apiKey},signal:i?.signal});if(!s.ok)return;let o=await s.json(),d=I(o.connectionTestId??o.testId,200);if(!d){this.connectionTestExpiresAt&&Date.now()>=this.connectionTestExpiresAt&&(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0);return}this.connectionTestId=d;let a=o.expiresAt?new Date(o.expiresAt).getTime():void 0;this.connectionTestExpiresAt=a&&Number.isFinite(a)?a:void 0}catch{return}finally{r!==void 0&&clearTimeout(r)}}clearConnectionTest(e){e&&this.connectionTestId!==e||(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0,this.connectionTestLastCheckedAt=Date.now())}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?.((i,r)=>{this.captureError(i,{platform:"react-native",fatal:r}).finally(()=>{typeof n=="function"&&n(i,r)})})}if(typeof window<"u"&&typeof window.addEventListener=="function"){window.addEventListener("error",n=>{let i=n.error instanceof Error?n.error:new Error(n.message||"Erro global");this.captureError(i,{url:n.filename||window.location.href,line:n.lineno,column:n.colno,platform:"browser"})}),window.addEventListener("unhandledrejection",n=>{let i=n.reason instanceof Error?n.reason:new Error(q(n.reason));this.captureError(i,{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 i=y("event"),r={...n,...this.getIntegrationContext(),eventId:i},s={id:i,endpoint:e,payload:u(r),resolve:()=>{}};return new Promise(o=>{s.resolve=o,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.refreshPendingConnectionTest();let n={...e.payload,...this.getIntegrationContext()},i=typeof n.connectionTestId=="string"?n.connectionTestId:void 0;await this.sendToEndpoint(e.endpoint,u(n))&&i&&this.clearConnectionTest(i)}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 i=typeof fetch=="function"?fetch:void 0;if(!i)return!1;let r=f(n),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(r),signal:s?.signal};return d&&(a.keepalive=!0),(await i(e,a)).ok}catch{return!1}finally{o!==void 0&&clearTimeout(o)}}},P="__DEVINSPECT_AUDIT_CORE_INSTANCE__",x=typeof globalThis<"u"?globalThis:{},c=x[P]||new E;x[P]=c;var h=class{handleError(e){let n=e instanceof Error?e:new Error(String(e));c.captureError(n,{framework:"Angular",platform:typeof window<"u"?"browser":"server",url:typeof window<"u"?window.location.href:""})}};h.\u0275fac=t=>new(t||h);h.\u0275prov=(0,_.\u0275\u0275defineInjectable)({token:h,factory:h.\u0275fac,providedIn:"root"});var Q=(t,e)=>{let n=t.urlWithParams||t.url||"";if(n&&c.isIngestUrl(n))return e(t);let i=c.createRequestId(),r=c.createTraceId(),s=c.createSpanId(),o=t.clone({setHeaders:{"x-devinspector-request-id":i,"x-devinspector-trace-id":r,"x-devinspector-span-id":s}}),d=typeof performance<"u"?performance.now():Date.now();return e(o).pipe((0,O.tap)({next:a=>{if(!(a instanceof T.HttpResponse))return;let p=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-d)),l=a.status||0;(l>=400||p>=c.getSlowThresholdMs())&&c.captureRequest({method:t.method||"GET",url:n,statusCode:l,durationMs:p,route:t.url,requestId:i,traceId:r,spanId:s,userAgent:typeof navigator<"u"?navigator.userAgent:void 0}),c.addBreadcrumb({type:"http",message:`${t.method||"GET"} ${n}`,metadata:{statusCode:l,durationMs:p,requestId:i,traceId:r,spanId:s}})},error:a=>{let p=a instanceof T.HttpErrorResponse?a:null,l=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-d)),m=p?.status||0,g=p?.message||p?.statusText||"Erro de rede no cliente";c.captureRequest({method:t.method||"GET",url:n,statusCode:m,durationMs:l,route:t.url,requestId:i,traceId:r,spanId:s,userAgent:typeof navigator<"u"?navigator.userAgent:void 0}),c.captureException(new Error(`HTTP Error ${m}: ${g}`),{url:n,method:t.method||"GET",statusCode:m,statusText:p?.statusText||"",type:"angular_network_error",requestId:i,traceId:r,spanId:s}),c.addBreadcrumb({type:"http_error",message:`${t.method||"GET"} ${n}`,metadata:{statusCode:m,durationMs:l,requestId:i,traceId:r,spanId:s,error:g}})}}))},Y=Q;0&&(module.exports={AuditAngularErrorHandler,audit,auditInterceptor,devInspectorInterceptor});
1
+ "use strict";var b=Object.defineProperty;var N=Object.getOwnPropertyDescriptor;var z=Object.getOwnPropertyNames;var L=Object.prototype.hasOwnProperty;var H=(t,e)=>{for(var n in e)b(t,n,{get:e[n],enumerable:!0})},B=(t,e,n,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of z(e))!L.call(t,r)&&r!==n&&b(t,r,{get:()=>e[r],enumerable:!(i=N(e,r))||i.enumerable});return t};var V=t=>B(b({},"__esModule",{value:!0}),t);var Z={};H(Z,{AuditAngularErrorHandler:()=>h,audit:()=>c,auditInterceptor:()=>Y,devInspectorInterceptor:()=>Q});module.exports=V(Z);var _=require("@angular/core"),T=require("@angular/common/http"),O=require("rxjs/operators");var A="9.3.2",$=new Set(["angular","vanilla-js","react","vue","svelte","node","python","java","csharp","php","nextjs","react-native"]),R="https://api.devinspector.com.br/api/ingest/track",M="https://api.devinspector.com.br/api/ingest/apm",k=50,v=1e4,j=6,S=1e4,F=5e3,G=["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 G.some(n=>e.includes(n))}function m(t,e=0,n=new WeakSet){if(t==null)return t;if(typeof t=="string")return t.length>v?`${t.slice(0,v)}...[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>=j)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(s=>m(s,e+1,n));let i=t,r={};for(let[s,o]of Object.entries(i)){if(K(s)){r[s]="[REDACTED]";continue}r[s]=m(o,e+1,n)}return r}finally{n.delete(t)}}function q(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 U(t){return t&&String(t).trim().toUpperCase()||void 0}function C(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 D(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 J(t){let e=Number(t);if(!Number.isFinite(e))throw new Error("metricValue deve ser um n\xFAmero finito.");return e}function I(t,e=200){return t==null?void 0:String(t).trim().slice(0,e)||void 0}function W(t){let e=I(t,50);if(e&&$.has(e))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 u(t){return m(t)}var E=class{constructor(){this.apiKey="";this.endpoint=R;this.apmEndpoint=M;this.environment="production";this.sdkVersion=A;this.slowThresholdMs=300;this.requestTimeoutMs=S;this.breadcrumbs=[];this.initialized=!1;this.listenersAttached=!1;this.sendQueue=[];this.isProcessingQueue=!1;this.flushResolvers=[];this.connectionTestLastCheckedAt=0}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.technology=W(e.technology),this.installationId=I(e.installationId,200),this.sdkVersion=I(e.sdkVersion,100)||A,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):S,this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0,this.connectionTestLastCheckedAt=0,this.connectionTestRequest=void 0,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}getTechnology(){return this.technology}getInstallationId(){return this.installationId}getSdkVersion(){return this.sdkVersion}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 n={timestamp:e.timestamp||new Date().toISOString(),type:e.type.trim().slice(0,200),message:e.message?.slice(0,v),metadata:e.metadata?m(e.metadata):void 0};this.breadcrumbs.push(n),this.breadcrumbs.length>k&&(this.breadcrumbs=this.breadcrumbs.slice(-k))}getBreadcrumbs(){return this.breadcrumbs.map(e=>u(e))}clearBreadcrumbs(){this.breadcrumbs=[]}getContext(){return{environment:this.environment,release:this.release,technology:this.technology,installationId:this.installationId,sdkVersion:this.sdkVersion,user:u(this.user),breadcrumbs:this.getBreadcrumbs()}}isIngestUrl(e){if(!e)return!1;try{let n=new URL(this.endpoint),i=new URL(this.apmEndpoint),r=new URL(e,n.origin),s=r.origin,o=r.pathname.replace(/\/$/,""),d=n.pathname.replace(/\/$/,""),a=i.pathname.replace(/\/$/,"");return s===n.origin&&o===d||s===i.origin&&o===a}catch{return e.includes(this.endpoint)||e.includes(this.apmEndpoint)}}createRequestId(){return y("req")}createTraceId(){return y("trace")}createSpanId(){return y("span")}async captureRequest(e){let n=U(e.method)||"GET",i=String(e.url||"").trim();if(!i)return;let r=C(e.statusCode)??0,s=w(e.durationMs)??0,o=D(e.ip),d={...e.metadata||{},timestamp:new Date().toISOString()};!(r>=400||s>=this.getSlowThresholdMs())&&(await this.refreshPendingConnectionTest(!0),!this.connectionTestId)||await this.sendApm({type:"request_metric",method:n,url:i,statusCode:r,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:u(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:u(d)})}captureDbQuery(e){let n=String(e.operation||"").trim();if(!n)return Promise.resolve();let i=w(e.durationMs)??0,r={...e.metadata||{},operation:n,collection:e.collection||"Unknown",durationMs:i,timestamp:new Date().toISOString()};return this.sendApm({type:"db_query",message:`Database operation: ${n}`,url:e.url,method:"DB",durationMs:i,requestId:e.requestId,traceId:e.traceId,spanId:e.spanId,environment:this.environment,release:this.release,user:u(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:u(r)})}captureMetric(e,n,i={},r={}){let s=X(e);if(!s)return Promise.resolve();let o;try{o=J(n)}catch{return Promise.resolve()}return this.sendApm({type:"custom_metric",metricName:s,metricValue:o,requestId:r.requestId,traceId:r.traceId,spanId:r.spanId,environment:this.environment,release:this.release,user:u(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:u({...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,n={}){let i=e?.message?.trim()||"Erro desconhecido",r={...n},s=U(r.method),o=C(r.statusCode??r.status),d=w(r.durationMs),a=D(r.ip),p=r.url!==void 0&&r.url!==null?String(r.url).trim():void 0,l=typeof r.requestId=="string"&&r.requestId.trim()||void 0,f=typeof r.traceId=="string"&&r.traceId.trim()||void 0,g=typeof r.spanId=="string"&&r.spanId.trim()||void 0;return delete r.method,delete r.statusCode,delete r.status,delete r.durationMs,delete r.ip,delete r.requestId,delete r.traceId,delete r.spanId,this.sendError({type:"error",message:i,stackTrace:e?.stack||"",...p?{url:p}:{},...s?{method:s}:{},...o!==void 0?{statusCode:o}:{},...d!==void 0?{durationMs:d}:{},...a?{ip:a}:{},...l?{requestId:l}:{},...f?{traceId:f}:{},...g?{spanId:g}:{},environment:this.environment,release:this.release,user:u(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:u({...r,timestamp:new Date().toISOString()})})}captureException(e,n={}){let i=e instanceof Error?e:new Error(q(e));return this.captureError(i,n)}captureMessage(e,n={}){let i=String(e||"").trim();return i?this.sendApm({type:"message",message:i.slice(0,v),environment:this.environment,release:this.release,user:u(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:u({...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()})}getIntegrationContext(){return{...this.technology?{technology:this.technology}:{},...this.installationId?{installationId:this.installationId}:{},...this.connectionTestId?{connectionTestId:this.connectionTestId}:{},sdkVersion:this.sdkVersion}}getPendingConnectionTestEndpoint(){try{let e=new URL(this.endpoint),n=new URL("/api/integrations/sdk/pending-test",e.origin);return this.technology&&n.searchParams.set("technology",this.technology),n.toString()}catch{return}}async refreshPendingConnectionTest(e=!1){if(!this.initialized||!this.apiKey||!this.technology)return;let n=Date.now();if(!e&&this.connectionTestLastCheckedAt>0&&n-this.connectionTestLastCheckedAt<F){this.connectionTestExpiresAt&&n>=this.connectionTestExpiresAt&&(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0);return}if(this.connectionTestRequest)return this.connectionTestRequest;let i=this.getPendingConnectionTestEndpoint();if(i)return this.connectionTestRequest=this.fetchPendingConnectionTest(i).finally(()=>{this.connectionTestRequest=void 0}),this.connectionTestRequest}async fetchPendingConnectionTest(e){this.connectionTestLastCheckedAt=Date.now();let n=typeof fetch=="function"?fetch:void 0;if(!n)return;let i,r;try{typeof AbortController<"u"&&(i=new AbortController,r=setTimeout(()=>{i?.abort()},this.requestTimeoutMs));let s=await n(e,{method:"GET",headers:{"x-api-key":this.apiKey},signal:i?.signal});if(!s.ok)return;let o=await s.json(),d=I(o.connectionTestId??o.testId,200);if(!d){this.connectionTestExpiresAt&&Date.now()>=this.connectionTestExpiresAt&&(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0);return}this.connectionTestId=d;let a=o.expiresAt?new Date(o.expiresAt).getTime():void 0;this.connectionTestExpiresAt=a&&Number.isFinite(a)?a:void 0}catch{return}finally{r!==void 0&&clearTimeout(r)}}clearConnectionTest(e){e&&this.connectionTestId!==e||(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0,this.connectionTestLastCheckedAt=Date.now())}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?.((i,r)=>{this.captureError(i,{platform:"react-native",fatal:r}).finally(()=>{typeof n=="function"&&n(i,r)})})}if(typeof window<"u"&&typeof window.addEventListener=="function"){window.addEventListener("error",n=>{let i=n.error instanceof Error?n.error:new Error(n.message||"Erro global");this.captureError(i,{url:n.filename||window.location.href,line:n.lineno,column:n.colno,platform:"browser"})}),window.addEventListener("unhandledrejection",n=>{let i=n.reason instanceof Error?n.reason:new Error(q(n.reason));this.captureError(i,{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 i=y("event"),r={...n,...this.getIntegrationContext(),eventId:i},s={id:i,endpoint:e,payload:u(r),resolve:()=>{}};return new Promise(o=>{s.resolve=o,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.refreshPendingConnectionTest();let n={...e.payload,...this.getIntegrationContext()},i=typeof n.connectionTestId=="string"?n.connectionTestId:void 0;await this.sendToEndpoint(e.endpoint,u(n))&&i&&this.clearConnectionTest(i)}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 i=typeof fetch=="function"?fetch:void 0;if(!i)return!1;let r=m(n),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(r),signal:s?.signal};return d&&(a.keepalive=!0),(await i(e,a)).ok}catch{return!1}finally{o!==void 0&&clearTimeout(o)}}},P="__DEVINSPECT_AUDIT_CORE_INSTANCE__",x=typeof globalThis<"u"?globalThis:{},c=x[P]||new E;x[P]=c;var h=class{handleError(e){let n=e instanceof Error?e:new Error(String(e));c.captureError(n,{framework:"Angular",platform:typeof window<"u"?"browser":"server",url:typeof window<"u"?window.location.href:""})}};h.\u0275fac=t=>new(t||h);h.\u0275prov=(0,_.\u0275\u0275defineInjectable)({token:h,factory:h.\u0275fac,providedIn:"root"});var Q=(t,e)=>{let n=t.urlWithParams||t.url||"";if(n&&c.isIngestUrl(n))return e(t);let i=c.createRequestId(),r=c.createTraceId(),s=c.createSpanId(),o=t.clone({setHeaders:{"x-devinspector-request-id":i,"x-devinspector-trace-id":r,"x-devinspector-span-id":s}}),d=typeof performance<"u"?performance.now():Date.now();return e(o).pipe((0,O.tap)({next:a=>{if(!(a instanceof T.HttpResponse))return;let p=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-d)),l=a.status||0;(l>=400||p>=c.getSlowThresholdMs())&&c.captureRequest({method:t.method||"GET",url:n,statusCode:l,durationMs:p,route:t.url,requestId:i,traceId:r,spanId:s,userAgent:typeof navigator<"u"?navigator.userAgent:void 0}),c.addBreadcrumb({type:"http",message:`${t.method||"GET"} ${n}`,metadata:{statusCode:l,durationMs:p,requestId:i,traceId:r,spanId:s}})},error:a=>{let p=a instanceof T.HttpErrorResponse?a:null,l=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-d)),f=p?.status||0,g=p?.message||p?.statusText||"Erro de rede no cliente";c.captureRequest({method:t.method||"GET",url:n,statusCode:f,durationMs:l,route:t.url,requestId:i,traceId:r,spanId:s,userAgent:typeof navigator<"u"?navigator.userAgent:void 0}),c.captureException(new Error(`HTTP Error ${f}: ${g}`),{url:n,method:t.method||"GET",statusCode:f,statusText:p?.statusText||"",type:"angular_network_error",requestId:i,traceId:r,spanId:s}),c.addBreadcrumb({type:"http_error",message:`${t.method||"GET"} ${n}`,metadata:{statusCode:f,durationMs:l,requestId:i,traceId:r,spanId:s,error:g}})}}))},Y=Q;0&&(module.exports={AuditAngularErrorHandler,audit,auditInterceptor,devInspectorInterceptor});
package/dist/angular.js CHANGED
@@ -1 +1 @@
1
- import{\u0275\u0275defineInjectable as H}from"@angular/core";import{HttpResponse as B,HttpErrorResponse as V}from"@angular/common/http";import{tap as $}from"rxjs/operators";var w="9.3.1",P=new Set(["angular","vanilla-js","react","vue","svelte","node","python","java","csharp","php","nextjs","react-native"]),E="https://api.devinspector.com.br/api/ingest/track",A="https://api.devinspector.com.br/api/ingest/apm",R=50,v=1e4,x=6,k=1e4,_=5e3,O=["password","senha","token","authorization","auth","bearer","credit_card","creditcard","cartao","cvv","cpf","rg","secret","private_key","privatekey"];function Q(n){let e=n.toLowerCase().replace(/[\s-]/g,"_");return O.some(t=>e.includes(t))}function m(n,e=0,t=new WeakSet){if(n==null)return n;if(typeof n=="string")return n.length>v?`${n.slice(0,v)}...[truncated]`:n;if(typeof n=="number"||typeof n=="boolean")return n;if(typeof n=="bigint")return String(n);if(typeof n=="function")return"[Function]";if(e>=x)return"[MaxDepth]";if(typeof n!="object")return String(n);if(t.has(n))return"[Circular]";t.add(n);try{if(Array.isArray(n))return n.slice(0,100).map(s=>m(s,e+1,t));let i=n,r={};for(let[s,o]of Object.entries(i)){if(Q(s)){r[s]="[REDACTED]";continue}r[s]=m(o,e+1,t)}return r}finally{t.delete(n)}}function M(n){if(n instanceof Error)return n.message||"Erro desconhecido";if(typeof n=="string")return n||"Erro desconhecido";try{let e=m(n);return typeof e=="string"?e||"Erro desconhecido":JSON.stringify(e)||"Erro desconhecido"}catch{return"Erro desconhecido"}}function S(n){return n&&String(n).trim().toUpperCase()||void 0}function q(n){if(!(typeof n!="number"||!Number.isFinite(n)))return Math.round(n)}function T(n){if(!(typeof n!="number"||!Number.isFinite(n)))return Math.max(0,Math.round(n))}function U(n){return n==null?void 0:String(n).trim().slice(0,100)||void 0}function z(n){return String(n??"").trim().slice(0,200)}function N(n){let e=Number(n);if(!Number.isFinite(e))throw new Error("metricValue deve ser um n\xFAmero finito.");return e}function I(n,e=200){return n==null?void 0:String(n).trim().slice(0,e)||void 0}function L(n){let e=I(n,50);if(e&&P.has(e))return e}function y(n){let e=typeof globalThis<"u"?globalThis.crypto:void 0;return e&&typeof e.randomUUID=="function"?`${n}-${e.randomUUID()}`:`${n}-${Date.now()}-${Math.random().toString(36).slice(2,12)}`}function u(n){return m(n)}var b=class{constructor(){this.apiKey="";this.endpoint=E;this.apmEndpoint=A;this.environment="production";this.sdkVersion=w;this.slowThresholdMs=300;this.requestTimeoutMs=k;this.breadcrumbs=[];this.initialized=!1;this.listenersAttached=!1;this.sendQueue=[];this.isProcessingQueue=!1;this.flushResolvers=[];this.connectionTestLastCheckedAt=0}init(e){if(!e?.apiKey?.trim())throw new Error("DevInspector: apiKey \xE9 obrigat\xF3rio.");this.apiKey=e.apiKey.trim(),this.endpoint=e.endpoint?.trim()||E,this.apmEndpoint=e.apmEndpoint?.trim()||A,this.environment=e.environment?.trim()||"production",this.release=e.release?.trim()||void 0,this.technology=L(e.technology),this.installationId=I(e.installationId,200),this.sdkVersion=I(e.sdkVersion,100)||w,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):k,this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0,this.connectionTestLastCheckedAt=0,this.connectionTestRequest=void 0,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}getTechnology(){return this.technology}getInstallationId(){return this.installationId}getSdkVersion(){return this.sdkVersion}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,v),metadata:e.metadata?m(e.metadata):void 0};this.breadcrumbs.push(t),this.breadcrumbs.length>R&&(this.breadcrumbs=this.breadcrumbs.slice(-R))}getBreadcrumbs(){return this.breadcrumbs.map(e=>u(e))}clearBreadcrumbs(){this.breadcrumbs=[]}getContext(){return{environment:this.environment,release:this.release,technology:this.technology,installationId:this.installationId,sdkVersion:this.sdkVersion,user:u(this.user),breadcrumbs:this.getBreadcrumbs()}}isIngestUrl(e){if(!e)return!1;try{let t=new URL(this.endpoint),i=new URL(this.apmEndpoint),r=new URL(e,t.origin),s=r.origin,o=r.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 y("req")}createTraceId(){return y("trace")}createSpanId(){return y("span")}captureRequest(e){let t=S(e.method)||"GET",i=String(e.url||"").trim();if(!i)return Promise.resolve();let r=q(e.statusCode)??0,s=T(e.durationMs)??0,o=U(e.ip),d={...e.metadata||{},timestamp:new Date().toISOString()};return this.sendApm({type:"request_metric",method:t,url:i,statusCode:r,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:u(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:u(d)})}captureDbQuery(e){let t=String(e.operation||"").trim();if(!t)return Promise.resolve();let i=T(e.durationMs)??0,r={...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:u(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:u(r)})}captureMetric(e,t,i={},r={}){let s=z(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:r.requestId,traceId:r.traceId,spanId:r.spanId,environment:this.environment,release:this.release,user:u(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:u({...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",r={...t},s=S(r.method),o=q(r.statusCode??r.status),d=T(r.durationMs),a=U(r.ip),p=r.url!==void 0&&r.url!==null?String(r.url).trim():void 0,l=typeof r.requestId=="string"&&r.requestId.trim()||void 0,h=typeof r.traceId=="string"&&r.traceId.trim()||void 0,g=typeof r.spanId=="string"&&r.spanId.trim()||void 0;return delete r.method,delete r.statusCode,delete r.status,delete r.durationMs,delete r.ip,delete r.requestId,delete r.traceId,delete r.spanId,this.sendError({type:"error",message:i,stackTrace:e?.stack||"",...p?{url:p}:{},...s?{method:s}:{},...o!==void 0?{statusCode:o}:{},...d!==void 0?{durationMs:d}:{},...a?{ip:a}:{},...l?{requestId:l}:{},...h?{traceId:h}:{},...g?{spanId:g}:{},environment:this.environment,release:this.release,user:u(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:u({...r,timestamp:new Date().toISOString()})})}captureException(e,t={}){let i=e instanceof Error?e:new Error(M(e));return this.captureError(i,t)}captureMessage(e,t={}){let i=String(e||"").trim();return i?this.sendApm({type:"message",message:i.slice(0,v),environment:this.environment,release:this.release,user:u(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:u({...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()})}getIntegrationContext(){return{...this.technology?{technology:this.technology}:{},...this.installationId?{installationId:this.installationId}:{},...this.connectionTestId?{connectionTestId:this.connectionTestId}:{},sdkVersion:this.sdkVersion}}getPendingConnectionTestEndpoint(){try{let e=new URL(this.endpoint),t=new URL("/api/integrations/sdk/pending-test",e.origin);return this.technology&&t.searchParams.set("technology",this.technology),t.toString()}catch{return}}async refreshPendingConnectionTest(e=!1){if(!this.initialized||!this.apiKey||!this.technology)return;let t=Date.now();if(!e&&this.connectionTestLastCheckedAt>0&&t-this.connectionTestLastCheckedAt<_){this.connectionTestExpiresAt&&t>=this.connectionTestExpiresAt&&(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0);return}if(this.connectionTestRequest)return this.connectionTestRequest;let i=this.getPendingConnectionTestEndpoint();if(i)return this.connectionTestRequest=this.fetchPendingConnectionTest(i).finally(()=>{this.connectionTestRequest=void 0}),this.connectionTestRequest}async fetchPendingConnectionTest(e){this.connectionTestLastCheckedAt=Date.now();let t=typeof fetch=="function"?fetch:void 0;if(!t)return;let i,r;try{typeof AbortController<"u"&&(i=new AbortController,r=setTimeout(()=>{i?.abort()},this.requestTimeoutMs));let s=await t(e,{method:"GET",headers:{"x-api-key":this.apiKey},signal:i?.signal});if(!s.ok)return;let o=await s.json(),d=I(o.connectionTestId??o.testId,200);if(!d){this.connectionTestExpiresAt&&Date.now()>=this.connectionTestExpiresAt&&(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0);return}this.connectionTestId=d;let a=o.expiresAt?new Date(o.expiresAt).getTime():void 0;this.connectionTestExpiresAt=a&&Number.isFinite(a)?a:void 0}catch{return}finally{r!==void 0&&clearTimeout(r)}}clearConnectionTest(e){e&&this.connectionTestId!==e||(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0,this.connectionTestLastCheckedAt=Date.now())}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,r)=>{this.captureError(i,{platform:"react-native",fatal:r}).finally(()=>{typeof t=="function"&&t(i,r)})})}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(M(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=y("event"),r={...t,...this.getIntegrationContext(),eventId:i},s={id:i,endpoint:e,payload:u(r),resolve:()=>{}};return new Promise(o=>{s.resolve=o,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.refreshPendingConnectionTest();let t={...e.payload,...this.getIntegrationContext()},i=typeof t.connectionTestId=="string"?t.connectionTestId:void 0;await this.sendToEndpoint(e.endpoint,u(t))&&i&&this.clearConnectionTest(i)}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!1;let r=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(r),signal:s?.signal};return d&&(a.keepalive=!0),(await i(e,a)).ok}catch{return!1}finally{o!==void 0&&clearTimeout(o)}}},D="__DEVINSPECT_AUDIT_CORE_INSTANCE__",C=typeof globalThis<"u"?globalThis:{},c=C[D]||new b;C[D]=c;var f=class{handleError(e){let t=e instanceof Error?e:new Error(String(e));c.captureError(t,{framework:"Angular",platform:typeof window<"u"?"browser":"server",url:typeof window<"u"?window.location.href:""})}};f.\u0275fac=n=>new(n||f);f.\u0275prov=H({token:f,factory:f.\u0275fac,providedIn:"root"});var j=(n,e)=>{let t=n.urlWithParams||n.url||"";if(t&&c.isIngestUrl(t))return e(n);let i=c.createRequestId(),r=c.createTraceId(),s=c.createSpanId(),o=n.clone({setHeaders:{"x-devinspector-request-id":i,"x-devinspector-trace-id":r,"x-devinspector-span-id":s}}),d=typeof performance<"u"?performance.now():Date.now();return e(o).pipe($({next:a=>{if(!(a instanceof B))return;let p=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-d)),l=a.status||0;(l>=400||p>=c.getSlowThresholdMs())&&c.captureRequest({method:n.method||"GET",url:t,statusCode:l,durationMs:p,route:n.url,requestId:i,traceId:r,spanId:s,userAgent:typeof navigator<"u"?navigator.userAgent:void 0}),c.addBreadcrumb({type:"http",message:`${n.method||"GET"} ${t}`,metadata:{statusCode:l,durationMs:p,requestId:i,traceId:r,spanId:s}})},error:a=>{let p=a instanceof V?a:null,l=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-d)),h=p?.status||0,g=p?.message||p?.statusText||"Erro de rede no cliente";c.captureRequest({method:n.method||"GET",url:t,statusCode:h,durationMs:l,route:n.url,requestId:i,traceId:r,spanId:s,userAgent:typeof navigator<"u"?navigator.userAgent:void 0}),c.captureException(new Error(`HTTP Error ${h}: ${g}`),{url:t,method:n.method||"GET",statusCode:h,statusText:p?.statusText||"",type:"angular_network_error",requestId:i,traceId:r,spanId:s}),c.addBreadcrumb({type:"http_error",message:`${n.method||"GET"} ${t}`,metadata:{statusCode:h,durationMs:l,requestId:i,traceId:r,spanId:s,error:g}})}}))},ne=j;export{f as AuditAngularErrorHandler,c as audit,ne as auditInterceptor,j as devInspectorInterceptor};
1
+ import{\u0275\u0275defineInjectable as H}from"@angular/core";import{HttpResponse as B,HttpErrorResponse as V}from"@angular/common/http";import{tap as $}from"rxjs/operators";var w="9.3.2",P=new Set(["angular","vanilla-js","react","vue","svelte","node","python","java","csharp","php","nextjs","react-native"]),E="https://api.devinspector.com.br/api/ingest/track",A="https://api.devinspector.com.br/api/ingest/apm",R=50,v=1e4,x=6,M=1e4,_=5e3,O=["password","senha","token","authorization","auth","bearer","credit_card","creditcard","cartao","cvv","cpf","rg","secret","private_key","privatekey"];function Q(n){let e=n.toLowerCase().replace(/[\s-]/g,"_");return O.some(t=>e.includes(t))}function f(n,e=0,t=new WeakSet){if(n==null)return n;if(typeof n=="string")return n.length>v?`${n.slice(0,v)}...[truncated]`:n;if(typeof n=="number"||typeof n=="boolean")return n;if(typeof n=="bigint")return String(n);if(typeof n=="function")return"[Function]";if(e>=x)return"[MaxDepth]";if(typeof n!="object")return String(n);if(t.has(n))return"[Circular]";t.add(n);try{if(Array.isArray(n))return n.slice(0,100).map(s=>f(s,e+1,t));let i=n,r={};for(let[s,o]of Object.entries(i)){if(Q(s)){r[s]="[REDACTED]";continue}r[s]=f(o,e+1,t)}return r}finally{t.delete(n)}}function k(n){if(n instanceof Error)return n.message||"Erro desconhecido";if(typeof n=="string")return n||"Erro desconhecido";try{let e=f(n);return typeof e=="string"?e||"Erro desconhecido":JSON.stringify(e)||"Erro desconhecido"}catch{return"Erro desconhecido"}}function S(n){return n&&String(n).trim().toUpperCase()||void 0}function q(n){if(!(typeof n!="number"||!Number.isFinite(n)))return Math.round(n)}function T(n){if(!(typeof n!="number"||!Number.isFinite(n)))return Math.max(0,Math.round(n))}function U(n){return n==null?void 0:String(n).trim().slice(0,100)||void 0}function N(n){return String(n??"").trim().slice(0,200)}function z(n){let e=Number(n);if(!Number.isFinite(e))throw new Error("metricValue deve ser um n\xFAmero finito.");return e}function I(n,e=200){return n==null?void 0:String(n).trim().slice(0,e)||void 0}function L(n){let e=I(n,50);if(e&&P.has(e))return e}function y(n){let e=typeof globalThis<"u"?globalThis.crypto:void 0;return e&&typeof e.randomUUID=="function"?`${n}-${e.randomUUID()}`:`${n}-${Date.now()}-${Math.random().toString(36).slice(2,12)}`}function u(n){return f(n)}var b=class{constructor(){this.apiKey="";this.endpoint=E;this.apmEndpoint=A;this.environment="production";this.sdkVersion=w;this.slowThresholdMs=300;this.requestTimeoutMs=M;this.breadcrumbs=[];this.initialized=!1;this.listenersAttached=!1;this.sendQueue=[];this.isProcessingQueue=!1;this.flushResolvers=[];this.connectionTestLastCheckedAt=0}init(e){if(!e?.apiKey?.trim())throw new Error("DevInspector: apiKey \xE9 obrigat\xF3rio.");this.apiKey=e.apiKey.trim(),this.endpoint=e.endpoint?.trim()||E,this.apmEndpoint=e.apmEndpoint?.trim()||A,this.environment=e.environment?.trim()||"production",this.release=e.release?.trim()||void 0,this.technology=L(e.technology),this.installationId=I(e.installationId,200),this.sdkVersion=I(e.sdkVersion,100)||w,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):M,this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0,this.connectionTestLastCheckedAt=0,this.connectionTestRequest=void 0,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}getTechnology(){return this.technology}getInstallationId(){return this.installationId}getSdkVersion(){return this.sdkVersion}getSlowThresholdMs(){return this.slowThresholdMs}getRequestTimeoutMs(){return this.requestTimeoutMs}isInitialized(){return this.initialized}setUser(e){this.user=e?f(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,v),metadata:e.metadata?f(e.metadata):void 0};this.breadcrumbs.push(t),this.breadcrumbs.length>R&&(this.breadcrumbs=this.breadcrumbs.slice(-R))}getBreadcrumbs(){return this.breadcrumbs.map(e=>u(e))}clearBreadcrumbs(){this.breadcrumbs=[]}getContext(){return{environment:this.environment,release:this.release,technology:this.technology,installationId:this.installationId,sdkVersion:this.sdkVersion,user:u(this.user),breadcrumbs:this.getBreadcrumbs()}}isIngestUrl(e){if(!e)return!1;try{let t=new URL(this.endpoint),i=new URL(this.apmEndpoint),r=new URL(e,t.origin),s=r.origin,o=r.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 y("req")}createTraceId(){return y("trace")}createSpanId(){return y("span")}async captureRequest(e){let t=S(e.method)||"GET",i=String(e.url||"").trim();if(!i)return;let r=q(e.statusCode)??0,s=T(e.durationMs)??0,o=U(e.ip),d={...e.metadata||{},timestamp:new Date().toISOString()};!(r>=400||s>=this.getSlowThresholdMs())&&(await this.refreshPendingConnectionTest(!0),!this.connectionTestId)||await this.sendApm({type:"request_metric",method:t,url:i,statusCode:r,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:u(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:u(d)})}captureDbQuery(e){let t=String(e.operation||"").trim();if(!t)return Promise.resolve();let i=T(e.durationMs)??0,r={...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:u(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:u(r)})}captureMetric(e,t,i={},r={}){let s=N(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:r.requestId,traceId:r.traceId,spanId:r.spanId,environment:this.environment,release:this.release,user:u(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:u({...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",r={...t},s=S(r.method),o=q(r.statusCode??r.status),d=T(r.durationMs),a=U(r.ip),p=r.url!==void 0&&r.url!==null?String(r.url).trim():void 0,l=typeof r.requestId=="string"&&r.requestId.trim()||void 0,h=typeof r.traceId=="string"&&r.traceId.trim()||void 0,g=typeof r.spanId=="string"&&r.spanId.trim()||void 0;return delete r.method,delete r.statusCode,delete r.status,delete r.durationMs,delete r.ip,delete r.requestId,delete r.traceId,delete r.spanId,this.sendError({type:"error",message:i,stackTrace:e?.stack||"",...p?{url:p}:{},...s?{method:s}:{},...o!==void 0?{statusCode:o}:{},...d!==void 0?{durationMs:d}:{},...a?{ip:a}:{},...l?{requestId:l}:{},...h?{traceId:h}:{},...g?{spanId:g}:{},environment:this.environment,release:this.release,user:u(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:u({...r,timestamp:new Date().toISOString()})})}captureException(e,t={}){let i=e instanceof Error?e:new Error(k(e));return this.captureError(i,t)}captureMessage(e,t={}){let i=String(e||"").trim();return i?this.sendApm({type:"message",message:i.slice(0,v),environment:this.environment,release:this.release,user:u(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:u({...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()})}getIntegrationContext(){return{...this.technology?{technology:this.technology}:{},...this.installationId?{installationId:this.installationId}:{},...this.connectionTestId?{connectionTestId:this.connectionTestId}:{},sdkVersion:this.sdkVersion}}getPendingConnectionTestEndpoint(){try{let e=new URL(this.endpoint),t=new URL("/api/integrations/sdk/pending-test",e.origin);return this.technology&&t.searchParams.set("technology",this.technology),t.toString()}catch{return}}async refreshPendingConnectionTest(e=!1){if(!this.initialized||!this.apiKey||!this.technology)return;let t=Date.now();if(!e&&this.connectionTestLastCheckedAt>0&&t-this.connectionTestLastCheckedAt<_){this.connectionTestExpiresAt&&t>=this.connectionTestExpiresAt&&(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0);return}if(this.connectionTestRequest)return this.connectionTestRequest;let i=this.getPendingConnectionTestEndpoint();if(i)return this.connectionTestRequest=this.fetchPendingConnectionTest(i).finally(()=>{this.connectionTestRequest=void 0}),this.connectionTestRequest}async fetchPendingConnectionTest(e){this.connectionTestLastCheckedAt=Date.now();let t=typeof fetch=="function"?fetch:void 0;if(!t)return;let i,r;try{typeof AbortController<"u"&&(i=new AbortController,r=setTimeout(()=>{i?.abort()},this.requestTimeoutMs));let s=await t(e,{method:"GET",headers:{"x-api-key":this.apiKey},signal:i?.signal});if(!s.ok)return;let o=await s.json(),d=I(o.connectionTestId??o.testId,200);if(!d){this.connectionTestExpiresAt&&Date.now()>=this.connectionTestExpiresAt&&(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0);return}this.connectionTestId=d;let a=o.expiresAt?new Date(o.expiresAt).getTime():void 0;this.connectionTestExpiresAt=a&&Number.isFinite(a)?a:void 0}catch{return}finally{r!==void 0&&clearTimeout(r)}}clearConnectionTest(e){e&&this.connectionTestId!==e||(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0,this.connectionTestLastCheckedAt=Date.now())}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,r)=>{this.captureError(i,{platform:"react-native",fatal:r}).finally(()=>{typeof t=="function"&&t(i,r)})})}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(k(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=y("event"),r={...t,...this.getIntegrationContext(),eventId:i},s={id:i,endpoint:e,payload:u(r),resolve:()=>{}};return new Promise(o=>{s.resolve=o,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.refreshPendingConnectionTest();let t={...e.payload,...this.getIntegrationContext()},i=typeof t.connectionTestId=="string"?t.connectionTestId:void 0;await this.sendToEndpoint(e.endpoint,u(t))&&i&&this.clearConnectionTest(i)}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!1;let r=f(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(r),signal:s?.signal};return d&&(a.keepalive=!0),(await i(e,a)).ok}catch{return!1}finally{o!==void 0&&clearTimeout(o)}}},C="__DEVINSPECT_AUDIT_CORE_INSTANCE__",D=typeof globalThis<"u"?globalThis:{},c=D[C]||new b;D[C]=c;var m=class{handleError(e){let t=e instanceof Error?e:new Error(String(e));c.captureError(t,{framework:"Angular",platform:typeof window<"u"?"browser":"server",url:typeof window<"u"?window.location.href:""})}};m.\u0275fac=n=>new(n||m);m.\u0275prov=H({token:m,factory:m.\u0275fac,providedIn:"root"});var j=(n,e)=>{let t=n.urlWithParams||n.url||"";if(t&&c.isIngestUrl(t))return e(n);let i=c.createRequestId(),r=c.createTraceId(),s=c.createSpanId(),o=n.clone({setHeaders:{"x-devinspector-request-id":i,"x-devinspector-trace-id":r,"x-devinspector-span-id":s}}),d=typeof performance<"u"?performance.now():Date.now();return e(o).pipe($({next:a=>{if(!(a instanceof B))return;let p=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-d)),l=a.status||0;(l>=400||p>=c.getSlowThresholdMs())&&c.captureRequest({method:n.method||"GET",url:t,statusCode:l,durationMs:p,route:n.url,requestId:i,traceId:r,spanId:s,userAgent:typeof navigator<"u"?navigator.userAgent:void 0}),c.addBreadcrumb({type:"http",message:`${n.method||"GET"} ${t}`,metadata:{statusCode:l,durationMs:p,requestId:i,traceId:r,spanId:s}})},error:a=>{let p=a instanceof V?a:null,l=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-d)),h=p?.status||0,g=p?.message||p?.statusText||"Erro de rede no cliente";c.captureRequest({method:n.method||"GET",url:t,statusCode:h,durationMs:l,route:n.url,requestId:i,traceId:r,spanId:s,userAgent:typeof navigator<"u"?navigator.userAgent:void 0}),c.captureException(new Error(`HTTP Error ${h}: ${g}`),{url:t,method:n.method||"GET",statusCode:h,statusText:p?.statusText||"",type:"angular_network_error",requestId:i,traceId:r,spanId:s}),c.addBreadcrumb({type:"http_error",message:`${n.method||"GET"} ${t}`,metadata:{statusCode:h,durationMs:l,requestId:i,traceId:r,spanId:s,error:g}})}}))},ne=j;export{m as AuditAngularErrorHandler,c as audit,ne as auditInterceptor,j as devInspectorInterceptor};
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var E=Object.defineProperty;var O=Object.getOwnPropertyDescriptor;var Q=Object.getOwnPropertyNames;var N=Object.prototype.hasOwnProperty;var z=(t,e)=>{for(var n in e)E(t,n,{get:e[n],enumerable:!0})},L=(t,e,n,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of Q(e))!N.call(t,r)&&r!==n&&E(t,r,{get:()=>e[r],enumerable:!(i=O(e,r))||i.enumerable});return t};var V=t=>L(E({},"__esModule",{value:!0}),t);var J={};z(J,{AuditCore:()=>g,DEVINSPECTOR_SDK_VERSION:()=>T,audit:()=>u,coreAuditFetch:()=>_});module.exports=V(J);var T="9.3.1",B=new Set(["angular","vanilla-js","react","vue","svelte","node","python","java","csharp","php","nextjs","react-native"]),k="https://api.devinspector.com.br/api/ingest/track",S="https://api.devinspector.com.br/api/ingest/apm",q=50,v=1e4,j=6,D=1e4,K=5e3,F=["password","senha","token","authorization","auth","bearer","credit_card","creditcard","cartao","cvv","cpf","rg","secret","private_key","privatekey"];function $(t){let e=t.toLowerCase().replace(/[\s-]/g,"_");return F.some(n=>e.includes(n))}function h(t,e=0,n=new WeakSet){if(t==null)return t;if(typeof t=="string")return t.length>v?`${t.slice(0,v)}...[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>=j)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(s=>h(s,e+1,n));let i=t,r={};for(let[s,o]of Object.entries(i)){if($(s)){r[s]="[REDACTED]";continue}r[s]=h(o,e+1,n)}return r}finally{n.delete(t)}}function R(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 M(t){return t&&String(t).trim().toUpperCase()||void 0}function U(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 C(t){return t==null?void 0:String(t).trim().slice(0,100)||void 0}function G(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,e=200){return t==null?void 0:String(t).trim().slice(0,e)||void 0}function X(t){let e=b(t,50);if(e&&B.has(e))return e}function I(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 h(t)}var g=class{constructor(){this.apiKey="";this.endpoint=k;this.apmEndpoint=S;this.environment="production";this.sdkVersion=T;this.slowThresholdMs=300;this.requestTimeoutMs=D;this.breadcrumbs=[];this.initialized=!1;this.listenersAttached=!1;this.sendQueue=[];this.isProcessingQueue=!1;this.flushResolvers=[];this.connectionTestLastCheckedAt=0}init(e){if(!e?.apiKey?.trim())throw new Error("DevInspector: apiKey \xE9 obrigat\xF3rio.");this.apiKey=e.apiKey.trim(),this.endpoint=e.endpoint?.trim()||k,this.apmEndpoint=e.apmEndpoint?.trim()||S,this.environment=e.environment?.trim()||"production",this.release=e.release?.trim()||void 0,this.technology=X(e.technology),this.installationId=b(e.installationId,200),this.sdkVersion=b(e.sdkVersion,100)||T,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):D,this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0,this.connectionTestLastCheckedAt=0,this.connectionTestRequest=void 0,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}getTechnology(){return this.technology}getInstallationId(){return this.installationId}getSdkVersion(){return this.sdkVersion}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 n={timestamp:e.timestamp||new Date().toISOString(),type:e.type.trim().slice(0,200),message:e.message?.slice(0,v),metadata:e.metadata?h(e.metadata):void 0};this.breadcrumbs.push(n),this.breadcrumbs.length>q&&(this.breadcrumbs=this.breadcrumbs.slice(-q))}getBreadcrumbs(){return this.breadcrumbs.map(e=>c(e))}clearBreadcrumbs(){this.breadcrumbs=[]}getContext(){return{environment:this.environment,release:this.release,technology:this.technology,installationId:this.installationId,sdkVersion:this.sdkVersion,user:c(this.user),breadcrumbs:this.getBreadcrumbs()}}isIngestUrl(e){if(!e)return!1;try{let n=new URL(this.endpoint),i=new URL(this.apmEndpoint),r=new URL(e,n.origin),s=r.origin,o=r.pathname.replace(/\/$/,""),a=n.pathname.replace(/\/$/,""),d=i.pathname.replace(/\/$/,"");return s===n.origin&&o===a||s===i.origin&&o===d}catch{return e.includes(this.endpoint)||e.includes(this.apmEndpoint)}}createRequestId(){return I("req")}createTraceId(){return I("trace")}createSpanId(){return I("span")}captureRequest(e){let n=M(e.method)||"GET",i=String(e.url||"").trim();if(!i)return Promise.resolve();let r=U(e.statusCode)??0,s=A(e.durationMs)??0,o=C(e.ip),a={...e.metadata||{},timestamp:new Date().toISOString()};return this.sendApm({type:"request_metric",method:n,url:i,statusCode:r,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 n=String(e.operation||"").trim();if(!n)return Promise.resolve();let i=A(e.durationMs)??0,r={...e.metadata||{},operation:n,collection:e.collection||"Unknown",durationMs:i,timestamp:new Date().toISOString()};return this.sendApm({type:"db_query",message:`Database operation: ${n}`,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(r)})}captureMetric(e,n,i={},r={}){let s=G(e);if(!s)return Promise.resolve();let o;try{o=H(n)}catch{return Promise.resolve()}return this.sendApm({type:"custom_metric",metricName:s,metricValue:o,requestId:r.requestId,traceId:r.traceId,spanId:r.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,n={}){let i=e?.message?.trim()||"Erro desconhecido",r={...n},s=M(r.method),o=U(r.statusCode??r.status),a=A(r.durationMs),d=C(r.ip),p=r.url!==void 0&&r.url!==null?String(r.url).trim():void 0,y=typeof r.requestId=="string"&&r.requestId.trim()||void 0,f=typeof r.traceId=="string"&&r.traceId.trim()||void 0,l=typeof r.spanId=="string"&&r.spanId.trim()||void 0;return delete r.method,delete r.statusCode,delete r.status,delete r.durationMs,delete r.ip,delete r.requestId,delete r.traceId,delete r.spanId,this.sendError({type:"error",message:i,stackTrace:e?.stack||"",...p?{url:p}:{},...s?{method:s}:{},...o!==void 0?{statusCode:o}:{},...a!==void 0?{durationMs:a}:{},...d?{ip:d}:{},...y?{requestId:y}:{},...f?{traceId:f}:{},...l?{spanId:l}:{},environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c({...r,timestamp:new Date().toISOString()})})}captureException(e,n={}){let i=e instanceof Error?e:new Error(R(e));return this.captureError(i,n)}captureMessage(e,n={}){let i=String(e||"").trim();return i?this.sendApm({type:"message",message:i.slice(0,v),environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c({...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()})}getIntegrationContext(){return{...this.technology?{technology:this.technology}:{},...this.installationId?{installationId:this.installationId}:{},...this.connectionTestId?{connectionTestId:this.connectionTestId}:{},sdkVersion:this.sdkVersion}}getPendingConnectionTestEndpoint(){try{let e=new URL(this.endpoint),n=new URL("/api/integrations/sdk/pending-test",e.origin);return this.technology&&n.searchParams.set("technology",this.technology),n.toString()}catch{return}}async refreshPendingConnectionTest(e=!1){if(!this.initialized||!this.apiKey||!this.technology)return;let n=Date.now();if(!e&&this.connectionTestLastCheckedAt>0&&n-this.connectionTestLastCheckedAt<K){this.connectionTestExpiresAt&&n>=this.connectionTestExpiresAt&&(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0);return}if(this.connectionTestRequest)return this.connectionTestRequest;let i=this.getPendingConnectionTestEndpoint();if(i)return this.connectionTestRequest=this.fetchPendingConnectionTest(i).finally(()=>{this.connectionTestRequest=void 0}),this.connectionTestRequest}async fetchPendingConnectionTest(e){this.connectionTestLastCheckedAt=Date.now();let n=typeof fetch=="function"?fetch:void 0;if(!n)return;let i,r;try{typeof AbortController<"u"&&(i=new AbortController,r=setTimeout(()=>{i?.abort()},this.requestTimeoutMs));let s=await n(e,{method:"GET",headers:{"x-api-key":this.apiKey},signal:i?.signal});if(!s.ok)return;let o=await s.json(),a=b(o.connectionTestId??o.testId,200);if(!a){this.connectionTestExpiresAt&&Date.now()>=this.connectionTestExpiresAt&&(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0);return}this.connectionTestId=a;let d=o.expiresAt?new Date(o.expiresAt).getTime():void 0;this.connectionTestExpiresAt=d&&Number.isFinite(d)?d:void 0}catch{return}finally{r!==void 0&&clearTimeout(r)}}clearConnectionTest(e){e&&this.connectionTestId!==e||(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0,this.connectionTestLastCheckedAt=Date.now())}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?.((i,r)=>{this.captureError(i,{platform:"react-native",fatal:r}).finally(()=>{typeof n=="function"&&n(i,r)})})}if(typeof window<"u"&&typeof window.addEventListener=="function"){window.addEventListener("error",n=>{let i=n.error instanceof Error?n.error:new Error(n.message||"Erro global");this.captureError(i,{url:n.filename||window.location.href,line:n.lineno,column:n.colno,platform:"browser"})}),window.addEventListener("unhandledrejection",n=>{let i=n.reason instanceof Error?n.reason:new Error(R(n.reason));this.captureError(i,{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 i=I("event"),r={...n,...this.getIntegrationContext(),eventId:i},s={id:i,endpoint:e,payload:c(r),resolve:()=>{}};return new Promise(o=>{s.resolve=o,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.refreshPendingConnectionTest();let n={...e.payload,...this.getIntegrationContext()},i=typeof n.connectionTestId=="string"?n.connectionTestId:void 0;await this.sendToEndpoint(e.endpoint,c(n))&&i&&this.clearConnectionTest(i)}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 i=typeof fetch=="function"?fetch:void 0;if(!i)return!1;let r=h(n),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(r),signal:s?.signal};return a&&(d.keepalive=!0),(await i(e,d)).ok}catch{return!1}finally{o!==void 0&&clearTimeout(o)}}},P="__DEVINSPECT_AUDIT_CORE_INSTANCE__",x=typeof globalThis<"u"?globalThis:{},u=x[P]||new g;x[P]=u;async function _(t,e,n={}){let i=n.nativeFetch||(typeof fetch=="function"?fetch:void 0);if(!i)throw new Error("fetch n\xE3o est\xE1 dispon\xEDvel.");let r=typeof t=="string"?t:t instanceof URL?t.toString():String(t.url||""),s=M(e?.method||(typeof t!="string"&&!(t instanceof URL)?t.method:"GET"))||"GET";if(r&&u.isIngestUrl(r))return i(t,e);let o=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",o),p.set("x-devinspector-trace-id",a),p.set("x-devinspector-span-id",d);let y={...e,headers:p},f=typeof performance<"u"?performance.now():Date.now();try{let l=await i(t,y),m=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-f)),w=l.status>=400||m>=u.getSlowThresholdMs();return u.addBreadcrumb({type:"http",message:`${s} ${r}`,metadata:{phase:"complete",statusCode:l.status,durationMs:m,requestId:o,traceId:a,spanId:d}}),w&&u.captureRequest({method:s,url:r,statusCode:l.status,durationMs:m,userAgent:typeof navigator<"u"?navigator.userAgent:n.defaultUserAgent,requestId:o,traceId:a,spanId:d}),l}catch(l){let m=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-f));u.addBreadcrumb({type:"http_error",message:`${s} ${r}`,metadata:{phase:"error",statusCode:0,durationMs:m,requestId:o,traceId:a,spanId:d}}),u.captureRequest({method:s,url:r,statusCode:0,durationMs:m,userAgent:typeof navigator<"u"?navigator.userAgent:n.defaultUserAgent,requestId:o,traceId:a,spanId:d});let w=l instanceof Error?l:new Error(R(l));throw u.captureError(w,{url:r,method:s,type:n.errorType||"network_error",requestId:o,traceId:a,spanId:d}),l}}0&&(module.exports={AuditCore,DEVINSPECTOR_SDK_VERSION,audit,coreAuditFetch});
1
+ "use strict";var E=Object.defineProperty;var O=Object.getOwnPropertyDescriptor;var Q=Object.getOwnPropertyNames;var N=Object.prototype.hasOwnProperty;var z=(t,e)=>{for(var n in e)E(t,n,{get:e[n],enumerable:!0})},L=(t,e,n,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of Q(e))!N.call(t,r)&&r!==n&&E(t,r,{get:()=>e[r],enumerable:!(i=O(e,r))||i.enumerable});return t};var V=t=>L(E({},"__esModule",{value:!0}),t);var J={};z(J,{AuditCore:()=>g,DEVINSPECTOR_SDK_VERSION:()=>b,audit:()=>c,coreAuditFetch:()=>_});module.exports=V(J);var b="9.3.2",B=new Set(["angular","vanilla-js","react","vue","svelte","node","python","java","csharp","php","nextjs","react-native"]),k="https://api.devinspector.com.br/api/ingest/track",S="https://api.devinspector.com.br/api/ingest/apm",q=50,v=1e4,j=6,D=1e4,K=5e3,F=["password","senha","token","authorization","auth","bearer","credit_card","creditcard","cartao","cvv","cpf","rg","secret","private_key","privatekey"];function $(t){let e=t.toLowerCase().replace(/[\s-]/g,"_");return F.some(n=>e.includes(n))}function m(t,e=0,n=new WeakSet){if(t==null)return t;if(typeof t=="string")return t.length>v?`${t.slice(0,v)}...[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>=j)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(s=>m(s,e+1,n));let i=t,r={};for(let[s,o]of Object.entries(i)){if($(s)){r[s]="[REDACTED]";continue}r[s]=m(o,e+1,n)}return r}finally{n.delete(t)}}function R(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 M(t){return t&&String(t).trim().toUpperCase()||void 0}function U(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 C(t){return t==null?void 0:String(t).trim().slice(0,100)||void 0}function G(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 T(t,e=200){return t==null?void 0:String(t).trim().slice(0,e)||void 0}function X(t){let e=T(t,50);if(e&&B.has(e))return e}function I(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 u(t){return m(t)}var g=class{constructor(){this.apiKey="";this.endpoint=k;this.apmEndpoint=S;this.environment="production";this.sdkVersion=b;this.slowThresholdMs=300;this.requestTimeoutMs=D;this.breadcrumbs=[];this.initialized=!1;this.listenersAttached=!1;this.sendQueue=[];this.isProcessingQueue=!1;this.flushResolvers=[];this.connectionTestLastCheckedAt=0}init(e){if(!e?.apiKey?.trim())throw new Error("DevInspector: apiKey \xE9 obrigat\xF3rio.");this.apiKey=e.apiKey.trim(),this.endpoint=e.endpoint?.trim()||k,this.apmEndpoint=e.apmEndpoint?.trim()||S,this.environment=e.environment?.trim()||"production",this.release=e.release?.trim()||void 0,this.technology=X(e.technology),this.installationId=T(e.installationId,200),this.sdkVersion=T(e.sdkVersion,100)||b,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):D,this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0,this.connectionTestLastCheckedAt=0,this.connectionTestRequest=void 0,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}getTechnology(){return this.technology}getInstallationId(){return this.installationId}getSdkVersion(){return this.sdkVersion}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 n={timestamp:e.timestamp||new Date().toISOString(),type:e.type.trim().slice(0,200),message:e.message?.slice(0,v),metadata:e.metadata?m(e.metadata):void 0};this.breadcrumbs.push(n),this.breadcrumbs.length>q&&(this.breadcrumbs=this.breadcrumbs.slice(-q))}getBreadcrumbs(){return this.breadcrumbs.map(e=>u(e))}clearBreadcrumbs(){this.breadcrumbs=[]}getContext(){return{environment:this.environment,release:this.release,technology:this.technology,installationId:this.installationId,sdkVersion:this.sdkVersion,user:u(this.user),breadcrumbs:this.getBreadcrumbs()}}isIngestUrl(e){if(!e)return!1;try{let n=new URL(this.endpoint),i=new URL(this.apmEndpoint),r=new URL(e,n.origin),s=r.origin,o=r.pathname.replace(/\/$/,""),d=n.pathname.replace(/\/$/,""),a=i.pathname.replace(/\/$/,"");return s===n.origin&&o===d||s===i.origin&&o===a}catch{return e.includes(this.endpoint)||e.includes(this.apmEndpoint)}}createRequestId(){return I("req")}createTraceId(){return I("trace")}createSpanId(){return I("span")}async captureRequest(e){let n=M(e.method)||"GET",i=String(e.url||"").trim();if(!i)return;let r=U(e.statusCode)??0,s=A(e.durationMs)??0,o=C(e.ip),d={...e.metadata||{},timestamp:new Date().toISOString()};!(r>=400||s>=this.getSlowThresholdMs())&&(await this.refreshPendingConnectionTest(!0),!this.connectionTestId)||await this.sendApm({type:"request_metric",method:n,url:i,statusCode:r,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:u(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:u(d)})}captureDbQuery(e){let n=String(e.operation||"").trim();if(!n)return Promise.resolve();let i=A(e.durationMs)??0,r={...e.metadata||{},operation:n,collection:e.collection||"Unknown",durationMs:i,timestamp:new Date().toISOString()};return this.sendApm({type:"db_query",message:`Database operation: ${n}`,url:e.url,method:"DB",durationMs:i,requestId:e.requestId,traceId:e.traceId,spanId:e.spanId,environment:this.environment,release:this.release,user:u(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:u(r)})}captureMetric(e,n,i={},r={}){let s=G(e);if(!s)return Promise.resolve();let o;try{o=H(n)}catch{return Promise.resolve()}return this.sendApm({type:"custom_metric",metricName:s,metricValue:o,requestId:r.requestId,traceId:r.traceId,spanId:r.spanId,environment:this.environment,release:this.release,user:u(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:u({...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,n={}){let i=e?.message?.trim()||"Erro desconhecido",r={...n},s=M(r.method),o=U(r.statusCode??r.status),d=A(r.durationMs),a=C(r.ip),p=r.url!==void 0&&r.url!==null?String(r.url).trim():void 0,y=typeof r.requestId=="string"&&r.requestId.trim()||void 0,f=typeof r.traceId=="string"&&r.traceId.trim()||void 0,l=typeof r.spanId=="string"&&r.spanId.trim()||void 0;return delete r.method,delete r.statusCode,delete r.status,delete r.durationMs,delete r.ip,delete r.requestId,delete r.traceId,delete r.spanId,this.sendError({type:"error",message:i,stackTrace:e?.stack||"",...p?{url:p}:{},...s?{method:s}:{},...o!==void 0?{statusCode:o}:{},...d!==void 0?{durationMs:d}:{},...a?{ip:a}:{},...y?{requestId:y}:{},...f?{traceId:f}:{},...l?{spanId:l}:{},environment:this.environment,release:this.release,user:u(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:u({...r,timestamp:new Date().toISOString()})})}captureException(e,n={}){let i=e instanceof Error?e:new Error(R(e));return this.captureError(i,n)}captureMessage(e,n={}){let i=String(e||"").trim();return i?this.sendApm({type:"message",message:i.slice(0,v),environment:this.environment,release:this.release,user:u(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:u({...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()})}getIntegrationContext(){return{...this.technology?{technology:this.technology}:{},...this.installationId?{installationId:this.installationId}:{},...this.connectionTestId?{connectionTestId:this.connectionTestId}:{},sdkVersion:this.sdkVersion}}getPendingConnectionTestEndpoint(){try{let e=new URL(this.endpoint),n=new URL("/api/integrations/sdk/pending-test",e.origin);return this.technology&&n.searchParams.set("technology",this.technology),n.toString()}catch{return}}async refreshPendingConnectionTest(e=!1){if(!this.initialized||!this.apiKey||!this.technology)return;let n=Date.now();if(!e&&this.connectionTestLastCheckedAt>0&&n-this.connectionTestLastCheckedAt<K){this.connectionTestExpiresAt&&n>=this.connectionTestExpiresAt&&(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0);return}if(this.connectionTestRequest)return this.connectionTestRequest;let i=this.getPendingConnectionTestEndpoint();if(i)return this.connectionTestRequest=this.fetchPendingConnectionTest(i).finally(()=>{this.connectionTestRequest=void 0}),this.connectionTestRequest}async fetchPendingConnectionTest(e){this.connectionTestLastCheckedAt=Date.now();let n=typeof fetch=="function"?fetch:void 0;if(!n)return;let i,r;try{typeof AbortController<"u"&&(i=new AbortController,r=setTimeout(()=>{i?.abort()},this.requestTimeoutMs));let s=await n(e,{method:"GET",headers:{"x-api-key":this.apiKey},signal:i?.signal});if(!s.ok)return;let o=await s.json(),d=T(o.connectionTestId??o.testId,200);if(!d){this.connectionTestExpiresAt&&Date.now()>=this.connectionTestExpiresAt&&(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0);return}this.connectionTestId=d;let a=o.expiresAt?new Date(o.expiresAt).getTime():void 0;this.connectionTestExpiresAt=a&&Number.isFinite(a)?a:void 0}catch{return}finally{r!==void 0&&clearTimeout(r)}}clearConnectionTest(e){e&&this.connectionTestId!==e||(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0,this.connectionTestLastCheckedAt=Date.now())}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?.((i,r)=>{this.captureError(i,{platform:"react-native",fatal:r}).finally(()=>{typeof n=="function"&&n(i,r)})})}if(typeof window<"u"&&typeof window.addEventListener=="function"){window.addEventListener("error",n=>{let i=n.error instanceof Error?n.error:new Error(n.message||"Erro global");this.captureError(i,{url:n.filename||window.location.href,line:n.lineno,column:n.colno,platform:"browser"})}),window.addEventListener("unhandledrejection",n=>{let i=n.reason instanceof Error?n.reason:new Error(R(n.reason));this.captureError(i,{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 i=I("event"),r={...n,...this.getIntegrationContext(),eventId:i},s={id:i,endpoint:e,payload:u(r),resolve:()=>{}};return new Promise(o=>{s.resolve=o,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.refreshPendingConnectionTest();let n={...e.payload,...this.getIntegrationContext()},i=typeof n.connectionTestId=="string"?n.connectionTestId:void 0;await this.sendToEndpoint(e.endpoint,u(n))&&i&&this.clearConnectionTest(i)}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 i=typeof fetch=="function"?fetch:void 0;if(!i)return!1;let r=m(n),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(r),signal:s?.signal};return d&&(a.keepalive=!0),(await i(e,a)).ok}catch{return!1}finally{o!==void 0&&clearTimeout(o)}}},P="__DEVINSPECT_AUDIT_CORE_INSTANCE__",x=typeof globalThis<"u"?globalThis:{},c=x[P]||new g;x[P]=c;async function _(t,e,n={}){let i=n.nativeFetch||(typeof fetch=="function"?fetch:void 0);if(!i)throw new Error("fetch n\xE3o est\xE1 dispon\xEDvel.");let r=typeof t=="string"?t:t instanceof URL?t.toString():String(t.url||""),s=M(e?.method||(typeof t!="string"&&!(t instanceof URL)?t.method:"GET"))||"GET";if(r&&c.isIngestUrl(r))return i(t,e);let o=c.createRequestId(),d=c.createTraceId(),a=c.createSpanId(),p=new Headers(e?.headers||(typeof t!="string"&&!(t instanceof URL)?t.headers:void 0));p.set("x-devinspector-request-id",o),p.set("x-devinspector-trace-id",d),p.set("x-devinspector-span-id",a);let y={...e,headers:p},f=typeof performance<"u"?performance.now():Date.now();try{let l=await i(t,y),h=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-f)),w=l.status>=400||h>=c.getSlowThresholdMs();return c.addBreadcrumb({type:"http",message:`${s} ${r}`,metadata:{phase:"complete",statusCode:l.status,durationMs:h,requestId:o,traceId:d,spanId:a}}),w?c.captureRequest({method:s,url:r,statusCode:l.status,durationMs:h,userAgent:typeof navigator<"u"?navigator.userAgent:n.defaultUserAgent,requestId:o,traceId:d,spanId:a}):c.captureRequest({method:s,url:r,statusCode:l.status,durationMs:h,userAgent:typeof navigator<"u"?navigator.userAgent:n.defaultUserAgent,requestId:o,traceId:d,spanId:a}),l}catch(l){let h=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-f));c.addBreadcrumb({type:"http_error",message:`${s} ${r}`,metadata:{phase:"error",statusCode:0,durationMs:h,requestId:o,traceId:d,spanId:a}}),c.captureRequest({method:s,url:r,statusCode:0,durationMs:h,userAgent:typeof navigator<"u"?navigator.userAgent:n.defaultUserAgent,requestId:o,traceId:d,spanId:a});let w=l instanceof Error?l:new Error(R(l));throw c.captureError(w,{url:r,method:s,type:n.errorType||"network_error",requestId:o,traceId:d,spanId:a}),l}}0&&(module.exports={AuditCore,DEVINSPECTOR_SDK_VERSION,audit,coreAuditFetch});
package/dist/index.d.cts CHANGED
@@ -1,6 +1,6 @@
1
1
  type UnknownRecord = Record<string, unknown>;
2
2
  type DevInspectorTechnology = "angular" | "vanilla-js" | "react" | "vue" | "svelte" | "node" | "python" | "java" | "csharp" | "php" | "nextjs" | "react-native";
3
- declare const DEVINSPECTOR_SDK_VERSION = "9.3.1";
3
+ declare const DEVINSPECTOR_SDK_VERSION = "9.3.2";
4
4
  interface AuditUser {
5
5
  id?: string;
6
6
  email?: string;
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  type UnknownRecord = Record<string, unknown>;
2
2
  type DevInspectorTechnology = "angular" | "vanilla-js" | "react" | "vue" | "svelte" | "node" | "python" | "java" | "csharp" | "php" | "nextjs" | "react-native";
3
- declare const DEVINSPECTOR_SDK_VERSION = "9.3.1";
3
+ declare const DEVINSPECTOR_SDK_VERSION = "9.3.2";
4
4
  interface AuditUser {
5
5
  id?: string;
6
6
  email?: string;
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- var E="9.3.1",x=new Set(["angular","vanilla-js","react","vue","svelte","node","python","java","csharp","php","nextjs","react-native"]),M="https://api.devinspector.com.br/api/ingest/track",k="https://api.devinspector.com.br/api/ingest/apm",S=50,b=1e4,_=6,q=1e4,O=5e3,Q=["password","senha","token","authorization","auth","bearer","credit_card","creditcard","cartao","cvv","cpf","rg","secret","private_key","privatekey"];function N(t){let e=t.toLowerCase().replace(/[\s-]/g,"_");return Q.some(n=>e.includes(n))}function h(t,e=0,n=new WeakSet){if(t==null)return t;if(typeof t=="string")return t.length>b?`${t.slice(0,b)}...[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>=_)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(s=>h(s,e+1,n));let i=t,r={};for(let[s,o]of Object.entries(i)){if(N(s)){r[s]="[REDACTED]";continue}r[s]=h(o,e+1,n)}return r}finally{n.delete(t)}}function A(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 R(t){return t&&String(t).trim().toUpperCase()||void 0}function D(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 L(t){let e=Number(t);if(!Number.isFinite(e))throw new Error("metricValue deve ser um n\xFAmero finito.");return e}function I(t,e=200){return t==null?void 0:String(t).trim().slice(0,e)||void 0}function V(t){let e=I(t,50);if(e&&x.has(e))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 c(t){return h(t)}var T=class{constructor(){this.apiKey="";this.endpoint=M;this.apmEndpoint=k;this.environment="production";this.sdkVersion=E;this.slowThresholdMs=300;this.requestTimeoutMs=q;this.breadcrumbs=[];this.initialized=!1;this.listenersAttached=!1;this.sendQueue=[];this.isProcessingQueue=!1;this.flushResolvers=[];this.connectionTestLastCheckedAt=0}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()||k,this.environment=e.environment?.trim()||"production",this.release=e.release?.trim()||void 0,this.technology=V(e.technology),this.installationId=I(e.installationId,200),this.sdkVersion=I(e.sdkVersion,100)||E,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):q,this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0,this.connectionTestLastCheckedAt=0,this.connectionTestRequest=void 0,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}getTechnology(){return this.technology}getInstallationId(){return this.installationId}getSdkVersion(){return this.sdkVersion}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 n={timestamp:e.timestamp||new Date().toISOString(),type:e.type.trim().slice(0,200),message:e.message?.slice(0,b),metadata:e.metadata?h(e.metadata):void 0};this.breadcrumbs.push(n),this.breadcrumbs.length>S&&(this.breadcrumbs=this.breadcrumbs.slice(-S))}getBreadcrumbs(){return this.breadcrumbs.map(e=>c(e))}clearBreadcrumbs(){this.breadcrumbs=[]}getContext(){return{environment:this.environment,release:this.release,technology:this.technology,installationId:this.installationId,sdkVersion:this.sdkVersion,user:c(this.user),breadcrumbs:this.getBreadcrumbs()}}isIngestUrl(e){if(!e)return!1;try{let n=new URL(this.endpoint),i=new URL(this.apmEndpoint),r=new URL(e,n.origin),s=r.origin,o=r.pathname.replace(/\/$/,""),a=n.pathname.replace(/\/$/,""),d=i.pathname.replace(/\/$/,"");return s===n.origin&&o===a||s===i.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 n=R(e.method)||"GET",i=String(e.url||"").trim();if(!i)return Promise.resolve();let r=D(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:n,url:i,statusCode:r,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 n=String(e.operation||"").trim();if(!n)return Promise.resolve();let i=w(e.durationMs)??0,r={...e.metadata||{},operation:n,collection:e.collection||"Unknown",durationMs:i,timestamp:new Date().toISOString()};return this.sendApm({type:"db_query",message:`Database operation: ${n}`,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(r)})}captureMetric(e,n,i={},r={}){let s=z(e);if(!s)return Promise.resolve();let o;try{o=L(n)}catch{return Promise.resolve()}return this.sendApm({type:"custom_metric",metricName:s,metricValue:o,requestId:r.requestId,traceId:r.traceId,spanId:r.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,n={}){let i=e?.message?.trim()||"Erro desconhecido",r={...n},s=R(r.method),o=D(r.statusCode??r.status),a=w(r.durationMs),d=U(r.ip),p=r.url!==void 0&&r.url!==null?String(r.url).trim():void 0,g=typeof r.requestId=="string"&&r.requestId.trim()||void 0,f=typeof r.traceId=="string"&&r.traceId.trim()||void 0,u=typeof r.spanId=="string"&&r.spanId.trim()||void 0;return delete r.method,delete r.statusCode,delete r.status,delete r.durationMs,delete r.ip,delete r.requestId,delete r.traceId,delete r.spanId,this.sendError({type:"error",message:i,stackTrace:e?.stack||"",...p?{url:p}:{},...s?{method:s}:{},...o!==void 0?{statusCode:o}:{},...a!==void 0?{durationMs:a}:{},...d?{ip:d}:{},...g?{requestId:g}:{},...f?{traceId:f}:{},...u?{spanId:u}:{},environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c({...r,timestamp:new Date().toISOString()})})}captureException(e,n={}){let i=e instanceof Error?e:new Error(A(e));return this.captureError(i,n)}captureMessage(e,n={}){let i=String(e||"").trim();return i?this.sendApm({type:"message",message:i.slice(0,b),environment:this.environment,release:this.release,user:c(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:c({...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()})}getIntegrationContext(){return{...this.technology?{technology:this.technology}:{},...this.installationId?{installationId:this.installationId}:{},...this.connectionTestId?{connectionTestId:this.connectionTestId}:{},sdkVersion:this.sdkVersion}}getPendingConnectionTestEndpoint(){try{let e=new URL(this.endpoint),n=new URL("/api/integrations/sdk/pending-test",e.origin);return this.technology&&n.searchParams.set("technology",this.technology),n.toString()}catch{return}}async refreshPendingConnectionTest(e=!1){if(!this.initialized||!this.apiKey||!this.technology)return;let n=Date.now();if(!e&&this.connectionTestLastCheckedAt>0&&n-this.connectionTestLastCheckedAt<O){this.connectionTestExpiresAt&&n>=this.connectionTestExpiresAt&&(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0);return}if(this.connectionTestRequest)return this.connectionTestRequest;let i=this.getPendingConnectionTestEndpoint();if(i)return this.connectionTestRequest=this.fetchPendingConnectionTest(i).finally(()=>{this.connectionTestRequest=void 0}),this.connectionTestRequest}async fetchPendingConnectionTest(e){this.connectionTestLastCheckedAt=Date.now();let n=typeof fetch=="function"?fetch:void 0;if(!n)return;let i,r;try{typeof AbortController<"u"&&(i=new AbortController,r=setTimeout(()=>{i?.abort()},this.requestTimeoutMs));let s=await n(e,{method:"GET",headers:{"x-api-key":this.apiKey},signal:i?.signal});if(!s.ok)return;let o=await s.json(),a=I(o.connectionTestId??o.testId,200);if(!a){this.connectionTestExpiresAt&&Date.now()>=this.connectionTestExpiresAt&&(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0);return}this.connectionTestId=a;let d=o.expiresAt?new Date(o.expiresAt).getTime():void 0;this.connectionTestExpiresAt=d&&Number.isFinite(d)?d:void 0}catch{return}finally{r!==void 0&&clearTimeout(r)}}clearConnectionTest(e){e&&this.connectionTestId!==e||(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0,this.connectionTestLastCheckedAt=Date.now())}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?.((i,r)=>{this.captureError(i,{platform:"react-native",fatal:r}).finally(()=>{typeof n=="function"&&n(i,r)})})}if(typeof window<"u"&&typeof window.addEventListener=="function"){window.addEventListener("error",n=>{let i=n.error instanceof Error?n.error:new Error(n.message||"Erro global");this.captureError(i,{url:n.filename||window.location.href,line:n.lineno,column:n.colno,platform:"browser"})}),window.addEventListener("unhandledrejection",n=>{let i=n.reason instanceof Error?n.reason:new Error(A(n.reason));this.captureError(i,{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 i=y("event"),r={...n,...this.getIntegrationContext(),eventId:i},s={id:i,endpoint:e,payload:c(r),resolve:()=>{}};return new Promise(o=>{s.resolve=o,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.refreshPendingConnectionTest();let n={...e.payload,...this.getIntegrationContext()},i=typeof n.connectionTestId=="string"?n.connectionTestId:void 0;await this.sendToEndpoint(e.endpoint,c(n))&&i&&this.clearConnectionTest(i)}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 i=typeof fetch=="function"?fetch:void 0;if(!i)return!1;let r=h(n),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(r),signal:s?.signal};return a&&(d.keepalive=!0),(await i(e,d)).ok}catch{return!1}finally{o!==void 0&&clearTimeout(o)}}},C="__DEVINSPECT_AUDIT_CORE_INSTANCE__",P=typeof globalThis<"u"?globalThis:{},l=P[C]||new T;P[C]=l;async function B(t,e,n={}){let i=n.nativeFetch||(typeof fetch=="function"?fetch:void 0);if(!i)throw new Error("fetch n\xE3o est\xE1 dispon\xEDvel.");let r=typeof t=="string"?t:t instanceof URL?t.toString():String(t.url||""),s=R(e?.method||(typeof t!="string"&&!(t instanceof URL)?t.method:"GET"))||"GET";if(r&&l.isIngestUrl(r))return i(t,e);let o=l.createRequestId(),a=l.createTraceId(),d=l.createSpanId(),p=new Headers(e?.headers||(typeof t!="string"&&!(t instanceof URL)?t.headers:void 0));p.set("x-devinspector-request-id",o),p.set("x-devinspector-trace-id",a),p.set("x-devinspector-span-id",d);let g={...e,headers:p},f=typeof performance<"u"?performance.now():Date.now();try{let u=await i(t,g),m=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-f)),v=u.status>=400||m>=l.getSlowThresholdMs();return l.addBreadcrumb({type:"http",message:`${s} ${r}`,metadata:{phase:"complete",statusCode:u.status,durationMs:m,requestId:o,traceId:a,spanId:d}}),v&&l.captureRequest({method:s,url:r,statusCode:u.status,durationMs:m,userAgent:typeof navigator<"u"?navigator.userAgent:n.defaultUserAgent,requestId:o,traceId:a,spanId:d}),u}catch(u){let m=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-f));l.addBreadcrumb({type:"http_error",message:`${s} ${r}`,metadata:{phase:"error",statusCode:0,durationMs:m,requestId:o,traceId:a,spanId:d}}),l.captureRequest({method:s,url:r,statusCode:0,durationMs:m,userAgent:typeof navigator<"u"?navigator.userAgent:n.defaultUserAgent,requestId:o,traceId:a,spanId:d});let v=u instanceof Error?u:new Error(A(u));throw l.captureError(v,{url:r,method:s,type:n.errorType||"network_error",requestId:o,traceId:a,spanId:d}),u}}export{T as AuditCore,E as DEVINSPECTOR_SDK_VERSION,l as audit,B as coreAuditFetch};
1
+ var E="9.3.2",x=new Set(["angular","vanilla-js","react","vue","svelte","node","python","java","csharp","php","nextjs","react-native"]),M="https://api.devinspector.com.br/api/ingest/track",k="https://api.devinspector.com.br/api/ingest/apm",S=50,T=1e4,_=6,q=1e4,O=5e3,Q=["password","senha","token","authorization","auth","bearer","credit_card","creditcard","cartao","cvv","cpf","rg","secret","private_key","privatekey"];function N(t){let e=t.toLowerCase().replace(/[\s-]/g,"_");return Q.some(n=>e.includes(n))}function m(t,e=0,n=new WeakSet){if(t==null)return t;if(typeof t=="string")return t.length>T?`${t.slice(0,T)}...[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>=_)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(s=>m(s,e+1,n));let i=t,r={};for(let[s,o]of Object.entries(i)){if(N(s)){r[s]="[REDACTED]";continue}r[s]=m(o,e+1,n)}return r}finally{n.delete(t)}}function A(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 R(t){return t&&String(t).trim().toUpperCase()||void 0}function D(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 L(t){let e=Number(t);if(!Number.isFinite(e))throw new Error("metricValue deve ser um n\xFAmero finito.");return e}function I(t,e=200){return t==null?void 0:String(t).trim().slice(0,e)||void 0}function V(t){let e=I(t,50);if(e&&x.has(e))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 u(t){return m(t)}var b=class{constructor(){this.apiKey="";this.endpoint=M;this.apmEndpoint=k;this.environment="production";this.sdkVersion=E;this.slowThresholdMs=300;this.requestTimeoutMs=q;this.breadcrumbs=[];this.initialized=!1;this.listenersAttached=!1;this.sendQueue=[];this.isProcessingQueue=!1;this.flushResolvers=[];this.connectionTestLastCheckedAt=0}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()||k,this.environment=e.environment?.trim()||"production",this.release=e.release?.trim()||void 0,this.technology=V(e.technology),this.installationId=I(e.installationId,200),this.sdkVersion=I(e.sdkVersion,100)||E,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):q,this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0,this.connectionTestLastCheckedAt=0,this.connectionTestRequest=void 0,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}getTechnology(){return this.technology}getInstallationId(){return this.installationId}getSdkVersion(){return this.sdkVersion}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 n={timestamp:e.timestamp||new Date().toISOString(),type:e.type.trim().slice(0,200),message:e.message?.slice(0,T),metadata:e.metadata?m(e.metadata):void 0};this.breadcrumbs.push(n),this.breadcrumbs.length>S&&(this.breadcrumbs=this.breadcrumbs.slice(-S))}getBreadcrumbs(){return this.breadcrumbs.map(e=>u(e))}clearBreadcrumbs(){this.breadcrumbs=[]}getContext(){return{environment:this.environment,release:this.release,technology:this.technology,installationId:this.installationId,sdkVersion:this.sdkVersion,user:u(this.user),breadcrumbs:this.getBreadcrumbs()}}isIngestUrl(e){if(!e)return!1;try{let n=new URL(this.endpoint),i=new URL(this.apmEndpoint),r=new URL(e,n.origin),s=r.origin,o=r.pathname.replace(/\/$/,""),d=n.pathname.replace(/\/$/,""),a=i.pathname.replace(/\/$/,"");return s===n.origin&&o===d||s===i.origin&&o===a}catch{return e.includes(this.endpoint)||e.includes(this.apmEndpoint)}}createRequestId(){return y("req")}createTraceId(){return y("trace")}createSpanId(){return y("span")}async captureRequest(e){let n=R(e.method)||"GET",i=String(e.url||"").trim();if(!i)return;let r=D(e.statusCode)??0,s=w(e.durationMs)??0,o=U(e.ip),d={...e.metadata||{},timestamp:new Date().toISOString()};!(r>=400||s>=this.getSlowThresholdMs())&&(await this.refreshPendingConnectionTest(!0),!this.connectionTestId)||await this.sendApm({type:"request_metric",method:n,url:i,statusCode:r,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:u(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:u(d)})}captureDbQuery(e){let n=String(e.operation||"").trim();if(!n)return Promise.resolve();let i=w(e.durationMs)??0,r={...e.metadata||{},operation:n,collection:e.collection||"Unknown",durationMs:i,timestamp:new Date().toISOString()};return this.sendApm({type:"db_query",message:`Database operation: ${n}`,url:e.url,method:"DB",durationMs:i,requestId:e.requestId,traceId:e.traceId,spanId:e.spanId,environment:this.environment,release:this.release,user:u(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:u(r)})}captureMetric(e,n,i={},r={}){let s=z(e);if(!s)return Promise.resolve();let o;try{o=L(n)}catch{return Promise.resolve()}return this.sendApm({type:"custom_metric",metricName:s,metricValue:o,requestId:r.requestId,traceId:r.traceId,spanId:r.spanId,environment:this.environment,release:this.release,user:u(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:u({...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,n={}){let i=e?.message?.trim()||"Erro desconhecido",r={...n},s=R(r.method),o=D(r.statusCode??r.status),d=w(r.durationMs),a=U(r.ip),p=r.url!==void 0&&r.url!==null?String(r.url).trim():void 0,g=typeof r.requestId=="string"&&r.requestId.trim()||void 0,f=typeof r.traceId=="string"&&r.traceId.trim()||void 0,c=typeof r.spanId=="string"&&r.spanId.trim()||void 0;return delete r.method,delete r.statusCode,delete r.status,delete r.durationMs,delete r.ip,delete r.requestId,delete r.traceId,delete r.spanId,this.sendError({type:"error",message:i,stackTrace:e?.stack||"",...p?{url:p}:{},...s?{method:s}:{},...o!==void 0?{statusCode:o}:{},...d!==void 0?{durationMs:d}:{},...a?{ip:a}:{},...g?{requestId:g}:{},...f?{traceId:f}:{},...c?{spanId:c}:{},environment:this.environment,release:this.release,user:u(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:u({...r,timestamp:new Date().toISOString()})})}captureException(e,n={}){let i=e instanceof Error?e:new Error(A(e));return this.captureError(i,n)}captureMessage(e,n={}){let i=String(e||"").trim();return i?this.sendApm({type:"message",message:i.slice(0,T),environment:this.environment,release:this.release,user:u(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:u({...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()})}getIntegrationContext(){return{...this.technology?{technology:this.technology}:{},...this.installationId?{installationId:this.installationId}:{},...this.connectionTestId?{connectionTestId:this.connectionTestId}:{},sdkVersion:this.sdkVersion}}getPendingConnectionTestEndpoint(){try{let e=new URL(this.endpoint),n=new URL("/api/integrations/sdk/pending-test",e.origin);return this.technology&&n.searchParams.set("technology",this.technology),n.toString()}catch{return}}async refreshPendingConnectionTest(e=!1){if(!this.initialized||!this.apiKey||!this.technology)return;let n=Date.now();if(!e&&this.connectionTestLastCheckedAt>0&&n-this.connectionTestLastCheckedAt<O){this.connectionTestExpiresAt&&n>=this.connectionTestExpiresAt&&(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0);return}if(this.connectionTestRequest)return this.connectionTestRequest;let i=this.getPendingConnectionTestEndpoint();if(i)return this.connectionTestRequest=this.fetchPendingConnectionTest(i).finally(()=>{this.connectionTestRequest=void 0}),this.connectionTestRequest}async fetchPendingConnectionTest(e){this.connectionTestLastCheckedAt=Date.now();let n=typeof fetch=="function"?fetch:void 0;if(!n)return;let i,r;try{typeof AbortController<"u"&&(i=new AbortController,r=setTimeout(()=>{i?.abort()},this.requestTimeoutMs));let s=await n(e,{method:"GET",headers:{"x-api-key":this.apiKey},signal:i?.signal});if(!s.ok)return;let o=await s.json(),d=I(o.connectionTestId??o.testId,200);if(!d){this.connectionTestExpiresAt&&Date.now()>=this.connectionTestExpiresAt&&(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0);return}this.connectionTestId=d;let a=o.expiresAt?new Date(o.expiresAt).getTime():void 0;this.connectionTestExpiresAt=a&&Number.isFinite(a)?a:void 0}catch{return}finally{r!==void 0&&clearTimeout(r)}}clearConnectionTest(e){e&&this.connectionTestId!==e||(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0,this.connectionTestLastCheckedAt=Date.now())}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?.((i,r)=>{this.captureError(i,{platform:"react-native",fatal:r}).finally(()=>{typeof n=="function"&&n(i,r)})})}if(typeof window<"u"&&typeof window.addEventListener=="function"){window.addEventListener("error",n=>{let i=n.error instanceof Error?n.error:new Error(n.message||"Erro global");this.captureError(i,{url:n.filename||window.location.href,line:n.lineno,column:n.colno,platform:"browser"})}),window.addEventListener("unhandledrejection",n=>{let i=n.reason instanceof Error?n.reason:new Error(A(n.reason));this.captureError(i,{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 i=y("event"),r={...n,...this.getIntegrationContext(),eventId:i},s={id:i,endpoint:e,payload:u(r),resolve:()=>{}};return new Promise(o=>{s.resolve=o,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.refreshPendingConnectionTest();let n={...e.payload,...this.getIntegrationContext()},i=typeof n.connectionTestId=="string"?n.connectionTestId:void 0;await this.sendToEndpoint(e.endpoint,u(n))&&i&&this.clearConnectionTest(i)}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 i=typeof fetch=="function"?fetch:void 0;if(!i)return!1;let r=m(n),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(r),signal:s?.signal};return d&&(a.keepalive=!0),(await i(e,a)).ok}catch{return!1}finally{o!==void 0&&clearTimeout(o)}}},C="__DEVINSPECT_AUDIT_CORE_INSTANCE__",P=typeof globalThis<"u"?globalThis:{},l=P[C]||new b;P[C]=l;async function B(t,e,n={}){let i=n.nativeFetch||(typeof fetch=="function"?fetch:void 0);if(!i)throw new Error("fetch n\xE3o est\xE1 dispon\xEDvel.");let r=typeof t=="string"?t:t instanceof URL?t.toString():String(t.url||""),s=R(e?.method||(typeof t!="string"&&!(t instanceof URL)?t.method:"GET"))||"GET";if(r&&l.isIngestUrl(r))return i(t,e);let o=l.createRequestId(),d=l.createTraceId(),a=l.createSpanId(),p=new Headers(e?.headers||(typeof t!="string"&&!(t instanceof URL)?t.headers:void 0));p.set("x-devinspector-request-id",o),p.set("x-devinspector-trace-id",d),p.set("x-devinspector-span-id",a);let g={...e,headers:p},f=typeof performance<"u"?performance.now():Date.now();try{let c=await i(t,g),h=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-f)),v=c.status>=400||h>=l.getSlowThresholdMs();return l.addBreadcrumb({type:"http",message:`${s} ${r}`,metadata:{phase:"complete",statusCode:c.status,durationMs:h,requestId:o,traceId:d,spanId:a}}),v?l.captureRequest({method:s,url:r,statusCode:c.status,durationMs:h,userAgent:typeof navigator<"u"?navigator.userAgent:n.defaultUserAgent,requestId:o,traceId:d,spanId:a}):l.captureRequest({method:s,url:r,statusCode:c.status,durationMs:h,userAgent:typeof navigator<"u"?navigator.userAgent:n.defaultUserAgent,requestId:o,traceId:d,spanId:a}),c}catch(c){let h=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-f));l.addBreadcrumb({type:"http_error",message:`${s} ${r}`,metadata:{phase:"error",statusCode:0,durationMs:h,requestId:o,traceId:d,spanId:a}}),l.captureRequest({method:s,url:r,statusCode:0,durationMs:h,userAgent:typeof navigator<"u"?navigator.userAgent:n.defaultUserAgent,requestId:o,traceId:d,spanId:a});let v=c instanceof Error?c:new Error(A(c));throw l.captureError(v,{url:r,method:s,type:n.errorType||"network_error",requestId:o,traceId:d,spanId:a}),c}}export{b as AuditCore,E as DEVINSPECTOR_SDK_VERSION,l as audit,B as coreAuditFetch};
package/dist/next.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var b=Object.defineProperty;var Q=Object.getOwnPropertyDescriptor;var L=Object.getOwnPropertyNames;var z=Object.prototype.hasOwnProperty;var j=(t,e)=>{for(var n in e)b(t,n,{get:e[n],enumerable:!0})},B=(t,e,n,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of L(e))!z.call(t,r)&&r!==n&&b(t,r,{get:()=>e[r],enumerable:!(i=Q(e,r))||i.enumerable});return t};var V=t=>B(b({},"__esModule",{value:!0}),t);var ne={};j(ne,{audit:()=>a,auditFetch:()=>Z,captureNextError:()=>k,initNextDevInspector:()=>W,withAuditAction:()=>te,withAuditApi:()=>ee});module.exports=V(ne);var q="9.3.1",$=new Set(["angular","vanilla-js","react","vue","svelte","node","python","java","csharp","php","nextjs","react-native"]),S="https://api.devinspector.com.br/api/ingest/track",x="https://api.devinspector.com.br/api/ingest/apm",C=50,w=1e4,F=6,D=1e4,K=5e3,G=["password","senha","token","authorization","auth","bearer","credit_card","creditcard","cartao","cvv","cpf","rg","secret","private_key","privatekey"];function H(t){let e=t.toLowerCase().replace(/[\s-]/g,"_");return G.some(n=>e.includes(n))}function g(t,e=0,n=new WeakSet){if(t==null)return t;if(typeof t=="string")return t.length>w?`${t.slice(0,w)}...[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>=F)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=>g(o,e+1,n));let i=t,r={};for(let[o,s]of Object.entries(i)){if(H(o)){r[o]="[REDACTED]";continue}r[o]=g(s,e+1,n)}return r}finally{n.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=g(t);return typeof e=="string"?e||"Erro desconhecido":JSON.stringify(e)||"Erro desconhecido"}catch{return"Erro desconhecido"}}function A(t){return t&&String(t).trim().toUpperCase()||void 0}function U(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 P(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 J(t){let e=Number(t);if(!Number.isFinite(e))throw new Error("metricValue deve ser um n\xFAmero finito.");return e}function I(t,e=200){return t==null?void 0:String(t).trim().slice(0,e)||void 0}function Y(t){let e=I(t,50);if(e&&$.has(e))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 g(t)}var M=class{constructor(){this.apiKey="";this.endpoint=S;this.apmEndpoint=x;this.environment="production";this.sdkVersion=q;this.slowThresholdMs=300;this.requestTimeoutMs=D;this.breadcrumbs=[];this.initialized=!1;this.listenersAttached=!1;this.sendQueue=[];this.isProcessingQueue=!1;this.flushResolvers=[];this.connectionTestLastCheckedAt=0}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()||x,this.environment=e.environment?.trim()||"production",this.release=e.release?.trim()||void 0,this.technology=Y(e.technology),this.installationId=I(e.installationId,200),this.sdkVersion=I(e.sdkVersion,100)||q,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):D,this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0,this.connectionTestLastCheckedAt=0,this.connectionTestRequest=void 0,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}getTechnology(){return this.technology}getInstallationId(){return this.installationId}getSdkVersion(){return this.sdkVersion}getSlowThresholdMs(){return this.slowThresholdMs}getRequestTimeoutMs(){return this.requestTimeoutMs}isInitialized(){return this.initialized}setUser(e){this.user=e?g(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,w),metadata:e.metadata?g(e.metadata):void 0};this.breadcrumbs.push(n),this.breadcrumbs.length>C&&(this.breadcrumbs=this.breadcrumbs.slice(-C))}getBreadcrumbs(){return this.breadcrumbs.map(e=>l(e))}clearBreadcrumbs(){this.breadcrumbs=[]}getContext(){return{environment:this.environment,release:this.release,technology:this.technology,installationId:this.installationId,sdkVersion:this.sdkVersion,user:l(this.user),breadcrumbs:this.getBreadcrumbs()}}isIngestUrl(e){if(!e)return!1;try{let n=new URL(this.endpoint),i=new URL(this.apmEndpoint),r=new URL(e,n.origin),o=r.origin,s=r.pathname.replace(/\/$/,""),u=n.pathname.replace(/\/$/,""),d=i.pathname.replace(/\/$/,"");return o===n.origin&&s===u||o===i.origin&&s===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 n=A(e.method)||"GET",i=String(e.url||"").trim();if(!i)return Promise.resolve();let r=U(e.statusCode)??0,o=v(e.durationMs)??0,s=P(e.ip),u={...e.metadata||{},timestamp:new Date().toISOString()};return this.sendApm({type:"request_metric",method:n,url:i,statusCode:r,durationMs:o,...e.userAgent?{userAgent:e.userAgent}:{},...e.route!==void 0?{route:e.route}:{},...s?{ip:s}:{},...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:l(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:l(u)})}captureDbQuery(e){let n=String(e.operation||"").trim();if(!n)return Promise.resolve();let i=v(e.durationMs)??0,r={...e.metadata||{},operation:n,collection:e.collection||"Unknown",durationMs:i,timestamp:new Date().toISOString()};return this.sendApm({type:"db_query",message:`Database operation: ${n}`,url:e.url,method:"DB",durationMs:i,requestId:e.requestId,traceId:e.traceId,spanId:e.spanId,environment:this.environment,release:this.release,user:l(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:l(r)})}captureMetric(e,n,i={},r={}){let o=X(e);if(!o)return Promise.resolve();let s;try{s=J(n)}catch{return Promise.resolve()}return this.sendApm({type:"custom_metric",metricName:o,metricValue:s,requestId:r.requestId,traceId:r.traceId,spanId:r.spanId,environment:this.environment,release:this.release,user:l(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:l({...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,n={}){let i=e?.message?.trim()||"Erro desconhecido",r={...n},o=A(r.method),s=U(r.statusCode??r.status),u=v(r.durationMs),d=P(r.ip),p=r.url!==void 0&&r.url!==null?String(r.url).trim():void 0,h=typeof r.requestId=="string"&&r.requestId.trim()||void 0,f=typeof r.traceId=="string"&&r.traceId.trim()||void 0,c=typeof r.spanId=="string"&&r.spanId.trim()||void 0;return delete r.method,delete r.statusCode,delete r.status,delete r.durationMs,delete r.ip,delete r.requestId,delete r.traceId,delete r.spanId,this.sendError({type:"error",message:i,stackTrace:e?.stack||"",...p?{url:p}:{},...o?{method:o}:{},...s!==void 0?{statusCode:s}:{},...u!==void 0?{durationMs:u}:{},...d?{ip:d}:{},...h?{requestId:h}:{},...f?{traceId:f}:{},...c?{spanId:c}:{},environment:this.environment,release:this.release,user:l(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:l({...r,timestamp:new Date().toISOString()})})}captureException(e,n={}){let i=e instanceof Error?e:new Error(E(e));return this.captureError(i,n)}captureMessage(e,n={}){let i=String(e||"").trim();return i?this.sendApm({type:"message",message:i.slice(0,w),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()})}getIntegrationContext(){return{...this.technology?{technology:this.technology}:{},...this.installationId?{installationId:this.installationId}:{},...this.connectionTestId?{connectionTestId:this.connectionTestId}:{},sdkVersion:this.sdkVersion}}getPendingConnectionTestEndpoint(){try{let e=new URL(this.endpoint),n=new URL("/api/integrations/sdk/pending-test",e.origin);return this.technology&&n.searchParams.set("technology",this.technology),n.toString()}catch{return}}async refreshPendingConnectionTest(e=!1){if(!this.initialized||!this.apiKey||!this.technology)return;let n=Date.now();if(!e&&this.connectionTestLastCheckedAt>0&&n-this.connectionTestLastCheckedAt<K){this.connectionTestExpiresAt&&n>=this.connectionTestExpiresAt&&(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0);return}if(this.connectionTestRequest)return this.connectionTestRequest;let i=this.getPendingConnectionTestEndpoint();if(i)return this.connectionTestRequest=this.fetchPendingConnectionTest(i).finally(()=>{this.connectionTestRequest=void 0}),this.connectionTestRequest}async fetchPendingConnectionTest(e){this.connectionTestLastCheckedAt=Date.now();let n=typeof fetch=="function"?fetch:void 0;if(!n)return;let i,r;try{typeof AbortController<"u"&&(i=new AbortController,r=setTimeout(()=>{i?.abort()},this.requestTimeoutMs));let o=await n(e,{method:"GET",headers:{"x-api-key":this.apiKey},signal:i?.signal});if(!o.ok)return;let s=await o.json(),u=I(s.connectionTestId??s.testId,200);if(!u){this.connectionTestExpiresAt&&Date.now()>=this.connectionTestExpiresAt&&(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0);return}this.connectionTestId=u;let d=s.expiresAt?new Date(s.expiresAt).getTime():void 0;this.connectionTestExpiresAt=d&&Number.isFinite(d)?d:void 0}catch{return}finally{r!==void 0&&clearTimeout(r)}}clearConnectionTest(e){e&&this.connectionTestId!==e||(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0,this.connectionTestLastCheckedAt=Date.now())}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?.((i,r)=>{this.captureError(i,{platform:"react-native",fatal:r}).finally(()=>{typeof n=="function"&&n(i,r)})})}if(typeof window<"u"&&typeof window.addEventListener=="function"){window.addEventListener("error",n=>{let i=n.error instanceof Error?n.error:new Error(n.message||"Erro global");this.captureError(i,{url:n.filename||window.location.href,line:n.lineno,column:n.colno,platform:"browser"})}),window.addEventListener("unhandledrejection",n=>{let i=n.reason instanceof Error?n.reason:new Error(E(n.reason));this.captureError(i,{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 i=y("event"),r={...n,...this.getIntegrationContext(),eventId:i},o={id:i,endpoint:e,payload:l(r),resolve:()=>{}};return new Promise(s=>{o.resolve=s,this.sendQueue.push(o),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.refreshPendingConnectionTest();let n={...e.payload,...this.getIntegrationContext()},i=typeof n.connectionTestId=="string"?n.connectionTestId:void 0;await this.sendToEndpoint(e.endpoint,l(n))&&i&&this.clearConnectionTest(i)}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 i=typeof fetch=="function"?fetch:void 0;if(!i)return!1;let r=g(n),o,s;try{typeof AbortController<"u"&&(o=new AbortController,s=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(r),signal:o?.signal};return u&&(d.keepalive=!0),(await i(e,d)).ok}catch{return!1}finally{s!==void 0&&clearTimeout(s)}}},_="__DEVINSPECT_AUDIT_CORE_INSTANCE__",O=typeof globalThis<"u"?globalThis:{},a=O[_]||new M;O[_]=a;async function N(t,e,n={}){let i=n.nativeFetch||(typeof fetch=="function"?fetch:void 0);if(!i)throw new Error("fetch n\xE3o est\xE1 dispon\xEDvel.");let r=typeof t=="string"?t:t instanceof URL?t.toString():String(t.url||""),o=A(e?.method||(typeof t!="string"&&!(t instanceof URL)?t.method:"GET"))||"GET";if(r&&a.isIngestUrl(r))return i(t,e);let s=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",s),p.set("x-devinspector-trace-id",u),p.set("x-devinspector-span-id",d);let h={...e,headers:p},f=typeof performance<"u"?performance.now():Date.now();try{let c=await i(t,h),m=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-f)),T=c.status>=400||m>=a.getSlowThresholdMs();return a.addBreadcrumb({type:"http",message:`${o} ${r}`,metadata:{phase:"complete",statusCode:c.status,durationMs:m,requestId:s,traceId:u,spanId:d}}),T&&a.captureRequest({method:o,url:r,statusCode:c.status,durationMs:m,userAgent:typeof navigator<"u"?navigator.userAgent:n.defaultUserAgent,requestId:s,traceId:u,spanId:d}),c}catch(c){let m=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-f));a.addBreadcrumb({type:"http_error",message:`${o} ${r}`,metadata:{phase:"error",statusCode:0,durationMs:m,requestId:s,traceId:u,spanId:d}}),a.captureRequest({method:o,url:r,statusCode:0,durationMs:m,userAgent:typeof navigator<"u"?navigator.userAgent:n.defaultUserAgent,requestId:s,traceId:u,spanId:d});let T=c instanceof Error?c:new Error(E(c));throw a.captureError(T,{url:r,method:o,type:n.errorType||"network_error",requestId:s,traceId:u,spanId:d}),c}}function W(t){a.init(t)}function k(t,e={}){let n=t instanceof Error?t:new Error(String(t));a.captureError(n,{...e,framework:"Next.js"})}async function Z(t,e){return N(t,e,{errorType:"nextjs_network_error"})}function ee(t){return async function(...e){let n=typeof performance<"u"?performance.now():Date.now(),i=e[0],r=e[1],o=i?.method||"GET",s=i?.url||i?.nextUrl?.href||"/api",u=typeof i?.headers?.get=="function"?i.headers.get("user-agent")||"":i?.headers?.["user-agent"]||"",d=R(i,"x-devinspector-request-id")||a.createRequestId(),p=R(i,"x-devinspector-trace-id")||a.createTraceId(),h=R(i,"x-devinspector-span-id")||a.createSpanId();a.addBreadcrumb({type:"http",message:`${o} ${s}`,metadata:{framework:"Next.js",phase:"start",requestId:d,traceId:p,spanId:h}});try{let f=await t(...e),c=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-n)),m=Number(f?.status??r?.statusCode??200)||200;return(m>=400||c>=a.getSlowThresholdMs())&&a.captureRequest({method:o,url:s,statusCode:m,durationMs:c,route:s,userAgent:u,requestId:d,traceId:p,spanId:h}),a.addBreadcrumb({type:"http",message:`${o} ${s}`,metadata:{framework:"Next.js",phase:"complete",statusCode:m,durationMs:c,requestId:d,traceId:p,spanId:h}}),f}catch(f){let c=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-n));throw a.captureRequest({method:o,url:s,statusCode:500,durationMs:c,route:s,userAgent:u,requestId:d,traceId:p,spanId:h}),a.addBreadcrumb({type:"http_error",message:`${o} ${s}`,metadata:{framework:"Next.js",statusCode:500,durationMs:c,requestId:d,traceId:p,spanId:h}}),k(f,{url:s,method:o,type:"api_route_error",requestId:d,traceId:p,spanId:h}),f}}}function te(t,e){return(async(...n)=>{let i=typeof performance<"u"?performance.now():Date.now(),r=a.createRequestId(),o=a.createTraceId(),s=a.createSpanId();a.addBreadcrumb({type:"server_action",message:t,metadata:{framework:"Next.js",phase:"start",requestId:r,traceId:o,spanId:s}});try{let u=await e(...n),d=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-i));return d>=a.getSlowThresholdMs()&&a.captureRequest({method:"ACTION",url:t,statusCode:200,durationMs:d,route:t,requestId:r,traceId:o,spanId:s}),a.addBreadcrumb({type:"server_action",message:t,metadata:{framework:"Next.js",phase:"complete",statusCode:200,durationMs:d,requestId:r,traceId:o,spanId:s}}),u}catch(u){let d=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-i));throw a.captureRequest({method:"ACTION",url:t,statusCode:500,durationMs:d,route:t,requestId:r,traceId:o,spanId:s}),a.addBreadcrumb({type:"server_action_error",message:t,metadata:{framework:"Next.js",statusCode:500,durationMs:d,requestId:r,traceId:o,spanId:s}}),k(u,{actionName:t,type:"server_action_error",requestId:r,traceId:o,spanId:s}),u}})}function R(t,e){if(!t?.headers)return;if(typeof t.headers.get=="function")return t.headers.get(e)||void 0;let n=t.headers[e]??t.headers[e.toLowerCase()];return Array.isArray(n)?n[0]:n?String(n):void 0}0&&(module.exports={audit,auditFetch,captureNextError,initNextDevInspector,withAuditAction,withAuditApi});
1
+ "use strict";var v=Object.defineProperty;var Q=Object.getOwnPropertyDescriptor;var L=Object.getOwnPropertyNames;var z=Object.prototype.hasOwnProperty;var j=(t,e)=>{for(var n in e)v(t,n,{get:e[n],enumerable:!0})},B=(t,e,n,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of L(e))!z.call(t,r)&&r!==n&&v(t,r,{get:()=>e[r],enumerable:!(s=Q(e,r))||s.enumerable});return t};var V=t=>B(v({},"__esModule",{value:!0}),t);var ne={};j(ne,{audit:()=>d,auditFetch:()=>Z,captureNextError:()=>k,initNextDevInspector:()=>W,withAuditAction:()=>te,withAuditApi:()=>ee});module.exports=V(ne);var q="9.3.2",$=new Set(["angular","vanilla-js","react","vue","svelte","node","python","java","csharp","php","nextjs","react-native"]),S="https://api.devinspector.com.br/api/ingest/track",C="https://api.devinspector.com.br/api/ingest/apm",x=50,w=1e4,F=6,D=1e4,K=5e3,G=["password","senha","token","authorization","auth","bearer","credit_card","creditcard","cartao","cvv","cpf","rg","secret","private_key","privatekey"];function H(t){let e=t.toLowerCase().replace(/[\s-]/g,"_");return G.some(n=>e.includes(n))}function g(t,e=0,n=new WeakSet){if(t==null)return t;if(typeof t=="string")return t.length>w?`${t.slice(0,w)}...[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>=F)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(i=>g(i,e+1,n));let s=t,r={};for(let[i,o]of Object.entries(s)){if(H(i)){r[i]="[REDACTED]";continue}r[i]=g(o,e+1,n)}return r}finally{n.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=g(t);return typeof e=="string"?e||"Erro desconhecido":JSON.stringify(e)||"Erro desconhecido"}catch{return"Erro desconhecido"}}function A(t){return t&&String(t).trim().toUpperCase()||void 0}function U(t){if(!(typeof t!="number"||!Number.isFinite(t)))return Math.round(t)}function b(t){if(!(typeof t!="number"||!Number.isFinite(t)))return Math.max(0,Math.round(t))}function P(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 J(t){let e=Number(t);if(!Number.isFinite(e))throw new Error("metricValue deve ser um n\xFAmero finito.");return e}function I(t,e=200){return t==null?void 0:String(t).trim().slice(0,e)||void 0}function Y(t){let e=I(t,50);if(e&&$.has(e))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 g(t)}var M=class{constructor(){this.apiKey="";this.endpoint=S;this.apmEndpoint=C;this.environment="production";this.sdkVersion=q;this.slowThresholdMs=300;this.requestTimeoutMs=D;this.breadcrumbs=[];this.initialized=!1;this.listenersAttached=!1;this.sendQueue=[];this.isProcessingQueue=!1;this.flushResolvers=[];this.connectionTestLastCheckedAt=0}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()||C,this.environment=e.environment?.trim()||"production",this.release=e.release?.trim()||void 0,this.technology=Y(e.technology),this.installationId=I(e.installationId,200),this.sdkVersion=I(e.sdkVersion,100)||q,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):D,this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0,this.connectionTestLastCheckedAt=0,this.connectionTestRequest=void 0,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}getTechnology(){return this.technology}getInstallationId(){return this.installationId}getSdkVersion(){return this.sdkVersion}getSlowThresholdMs(){return this.slowThresholdMs}getRequestTimeoutMs(){return this.requestTimeoutMs}isInitialized(){return this.initialized}setUser(e){this.user=e?g(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,w),metadata:e.metadata?g(e.metadata):void 0};this.breadcrumbs.push(n),this.breadcrumbs.length>x&&(this.breadcrumbs=this.breadcrumbs.slice(-x))}getBreadcrumbs(){return this.breadcrumbs.map(e=>l(e))}clearBreadcrumbs(){this.breadcrumbs=[]}getContext(){return{environment:this.environment,release:this.release,technology:this.technology,installationId:this.installationId,sdkVersion:this.sdkVersion,user:l(this.user),breadcrumbs:this.getBreadcrumbs()}}isIngestUrl(e){if(!e)return!1;try{let n=new URL(this.endpoint),s=new URL(this.apmEndpoint),r=new URL(e,n.origin),i=r.origin,o=r.pathname.replace(/\/$/,""),u=n.pathname.replace(/\/$/,""),a=s.pathname.replace(/\/$/,"");return i===n.origin&&o===u||i===s.origin&&o===a}catch{return e.includes(this.endpoint)||e.includes(this.apmEndpoint)}}createRequestId(){return y("req")}createTraceId(){return y("trace")}createSpanId(){return y("span")}async captureRequest(e){let n=A(e.method)||"GET",s=String(e.url||"").trim();if(!s)return;let r=U(e.statusCode)??0,i=b(e.durationMs)??0,o=P(e.ip),u={...e.metadata||{},timestamp:new Date().toISOString()};!(r>=400||i>=this.getSlowThresholdMs())&&(await this.refreshPendingConnectionTest(!0),!this.connectionTestId)||await this.sendApm({type:"request_metric",method:n,url:s,statusCode:r,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:l(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:l(u)})}captureDbQuery(e){let n=String(e.operation||"").trim();if(!n)return Promise.resolve();let s=b(e.durationMs)??0,r={...e.metadata||{},operation:n,collection:e.collection||"Unknown",durationMs:s,timestamp:new Date().toISOString()};return this.sendApm({type:"db_query",message:`Database operation: ${n}`,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(r)})}captureMetric(e,n,s={},r={}){let i=X(e);if(!i)return Promise.resolve();let o;try{o=J(n)}catch{return Promise.resolve()}return this.sendApm({type:"custom_metric",metricName:i,metricValue:o,requestId:r.requestId,traceId:r.traceId,spanId:r.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,n={}){let s=e?.message?.trim()||"Erro desconhecido",r={...n},i=A(r.method),o=U(r.statusCode??r.status),u=b(r.durationMs),a=P(r.ip),p=r.url!==void 0&&r.url!==null?String(r.url).trim():void 0,h=typeof r.requestId=="string"&&r.requestId.trim()||void 0,f=typeof r.traceId=="string"&&r.traceId.trim()||void 0,c=typeof r.spanId=="string"&&r.spanId.trim()||void 0;return delete r.method,delete r.statusCode,delete r.status,delete r.durationMs,delete r.ip,delete r.requestId,delete r.traceId,delete r.spanId,this.sendError({type:"error",message:s,stackTrace:e?.stack||"",...p?{url:p}:{},...i?{method:i}:{},...o!==void 0?{statusCode:o}:{},...u!==void 0?{durationMs:u}:{},...a?{ip:a}:{},...h?{requestId:h}:{},...f?{traceId:f}:{},...c?{spanId:c}:{},environment:this.environment,release:this.release,user:l(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:l({...r,timestamp:new Date().toISOString()})})}captureException(e,n={}){let s=e instanceof Error?e:new Error(E(e));return this.captureError(s,n)}captureMessage(e,n={}){let s=String(e||"").trim();return s?this.sendApm({type:"message",message:s.slice(0,w),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()})}getIntegrationContext(){return{...this.technology?{technology:this.technology}:{},...this.installationId?{installationId:this.installationId}:{},...this.connectionTestId?{connectionTestId:this.connectionTestId}:{},sdkVersion:this.sdkVersion}}getPendingConnectionTestEndpoint(){try{let e=new URL(this.endpoint),n=new URL("/api/integrations/sdk/pending-test",e.origin);return this.technology&&n.searchParams.set("technology",this.technology),n.toString()}catch{return}}async refreshPendingConnectionTest(e=!1){if(!this.initialized||!this.apiKey||!this.technology)return;let n=Date.now();if(!e&&this.connectionTestLastCheckedAt>0&&n-this.connectionTestLastCheckedAt<K){this.connectionTestExpiresAt&&n>=this.connectionTestExpiresAt&&(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0);return}if(this.connectionTestRequest)return this.connectionTestRequest;let s=this.getPendingConnectionTestEndpoint();if(s)return this.connectionTestRequest=this.fetchPendingConnectionTest(s).finally(()=>{this.connectionTestRequest=void 0}),this.connectionTestRequest}async fetchPendingConnectionTest(e){this.connectionTestLastCheckedAt=Date.now();let n=typeof fetch=="function"?fetch:void 0;if(!n)return;let s,r;try{typeof AbortController<"u"&&(s=new AbortController,r=setTimeout(()=>{s?.abort()},this.requestTimeoutMs));let i=await n(e,{method:"GET",headers:{"x-api-key":this.apiKey},signal:s?.signal});if(!i.ok)return;let o=await i.json(),u=I(o.connectionTestId??o.testId,200);if(!u){this.connectionTestExpiresAt&&Date.now()>=this.connectionTestExpiresAt&&(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0);return}this.connectionTestId=u;let a=o.expiresAt?new Date(o.expiresAt).getTime():void 0;this.connectionTestExpiresAt=a&&Number.isFinite(a)?a:void 0}catch{return}finally{r!==void 0&&clearTimeout(r)}}clearConnectionTest(e){e&&this.connectionTestId!==e||(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0,this.connectionTestLastCheckedAt=Date.now())}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?.((s,r)=>{this.captureError(s,{platform:"react-native",fatal:r}).finally(()=>{typeof n=="function"&&n(s,r)})})}if(typeof window<"u"&&typeof window.addEventListener=="function"){window.addEventListener("error",n=>{let s=n.error instanceof Error?n.error:new Error(n.message||"Erro global");this.captureError(s,{url:n.filename||window.location.href,line:n.lineno,column:n.colno,platform:"browser"})}),window.addEventListener("unhandledrejection",n=>{let s=n.reason instanceof Error?n.reason:new Error(E(n.reason));this.captureError(s,{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 s=y("event"),r={...n,...this.getIntegrationContext(),eventId:s},i={id:s,endpoint:e,payload:l(r),resolve:()=>{}};return new Promise(o=>{i.resolve=o,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.refreshPendingConnectionTest();let n={...e.payload,...this.getIntegrationContext()},s=typeof n.connectionTestId=="string"?n.connectionTestId:void 0;await this.sendToEndpoint(e.endpoint,l(n))&&s&&this.clearConnectionTest(s)}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 s=typeof fetch=="function"?fetch:void 0;if(!s)return!1;let r=g(n),i,o;try{typeof AbortController<"u"&&(i=new AbortController,o=setTimeout(()=>{i?.abort()},this.requestTimeoutMs));let u=typeof window<"u"&&typeof window.document<"u",a={method:"POST",headers:{"content-type":"application/json","x-api-key":this.apiKey},body:JSON.stringify(r),signal:i?.signal};return u&&(a.keepalive=!0),(await s(e,a)).ok}catch{return!1}finally{o!==void 0&&clearTimeout(o)}}},_="__DEVINSPECT_AUDIT_CORE_INSTANCE__",O=typeof globalThis<"u"?globalThis:{},d=O[_]||new M;O[_]=d;async function N(t,e,n={}){let s=n.nativeFetch||(typeof fetch=="function"?fetch:void 0);if(!s)throw new Error("fetch n\xE3o est\xE1 dispon\xEDvel.");let r=typeof t=="string"?t:t instanceof URL?t.toString():String(t.url||""),i=A(e?.method||(typeof t!="string"&&!(t instanceof URL)?t.method:"GET"))||"GET";if(r&&d.isIngestUrl(r))return s(t,e);let o=d.createRequestId(),u=d.createTraceId(),a=d.createSpanId(),p=new Headers(e?.headers||(typeof t!="string"&&!(t instanceof URL)?t.headers:void 0));p.set("x-devinspector-request-id",o),p.set("x-devinspector-trace-id",u),p.set("x-devinspector-span-id",a);let h={...e,headers:p},f=typeof performance<"u"?performance.now():Date.now();try{let c=await s(t,h),m=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-f)),T=c.status>=400||m>=d.getSlowThresholdMs();return d.addBreadcrumb({type:"http",message:`${i} ${r}`,metadata:{phase:"complete",statusCode:c.status,durationMs:m,requestId:o,traceId:u,spanId:a}}),T?d.captureRequest({method:i,url:r,statusCode:c.status,durationMs:m,userAgent:typeof navigator<"u"?navigator.userAgent:n.defaultUserAgent,requestId:o,traceId:u,spanId:a}):d.captureRequest({method:i,url:r,statusCode:c.status,durationMs:m,userAgent:typeof navigator<"u"?navigator.userAgent:n.defaultUserAgent,requestId:o,traceId:u,spanId:a}),c}catch(c){let m=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-f));d.addBreadcrumb({type:"http_error",message:`${i} ${r}`,metadata:{phase:"error",statusCode:0,durationMs:m,requestId:o,traceId:u,spanId:a}}),d.captureRequest({method:i,url:r,statusCode:0,durationMs:m,userAgent:typeof navigator<"u"?navigator.userAgent:n.defaultUserAgent,requestId:o,traceId:u,spanId:a});let T=c instanceof Error?c:new Error(E(c));throw d.captureError(T,{url:r,method:i,type:n.errorType||"network_error",requestId:o,traceId:u,spanId:a}),c}}function W(t){d.init(t)}function k(t,e={}){let n=t instanceof Error?t:new Error(String(t));d.captureError(n,{...e,framework:"Next.js"})}async function Z(t,e){return N(t,e,{errorType:"nextjs_network_error"})}function ee(t){return async function(...e){let n=typeof performance<"u"?performance.now():Date.now(),s=e[0],r=e[1],i=s?.method||"GET",o=s?.url||s?.nextUrl?.href||"/api",u=typeof s?.headers?.get=="function"?s.headers.get("user-agent")||"":s?.headers?.["user-agent"]||"",a=R(s,"x-devinspector-request-id")||d.createRequestId(),p=R(s,"x-devinspector-trace-id")||d.createTraceId(),h=R(s,"x-devinspector-span-id")||d.createSpanId();d.addBreadcrumb({type:"http",message:`${i} ${o}`,metadata:{framework:"Next.js",phase:"start",requestId:a,traceId:p,spanId:h}});try{let f=await t(...e),c=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-n)),m=Number(f?.status??r?.statusCode??200)||200;return(m>=400||c>=d.getSlowThresholdMs())&&d.captureRequest({method:i,url:o,statusCode:m,durationMs:c,route:o,userAgent:u,requestId:a,traceId:p,spanId:h}),d.addBreadcrumb({type:"http",message:`${i} ${o}`,metadata:{framework:"Next.js",phase:"complete",statusCode:m,durationMs:c,requestId:a,traceId:p,spanId:h}}),f}catch(f){let c=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-n));throw d.captureRequest({method:i,url:o,statusCode:500,durationMs:c,route:o,userAgent:u,requestId:a,traceId:p,spanId:h}),d.addBreadcrumb({type:"http_error",message:`${i} ${o}`,metadata:{framework:"Next.js",statusCode:500,durationMs:c,requestId:a,traceId:p,spanId:h}}),k(f,{url:o,method:i,type:"api_route_error",requestId:a,traceId:p,spanId:h}),f}}}function te(t,e){return(async(...n)=>{let s=typeof performance<"u"?performance.now():Date.now(),r=d.createRequestId(),i=d.createTraceId(),o=d.createSpanId();d.addBreadcrumb({type:"server_action",message:t,metadata:{framework:"Next.js",phase:"start",requestId:r,traceId:i,spanId:o}});try{let u=await e(...n),a=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-s));return a>=d.getSlowThresholdMs()&&d.captureRequest({method:"ACTION",url:t,statusCode:200,durationMs:a,route:t,requestId:r,traceId:i,spanId:o}),d.addBreadcrumb({type:"server_action",message:t,metadata:{framework:"Next.js",phase:"complete",statusCode:200,durationMs:a,requestId:r,traceId:i,spanId:o}}),u}catch(u){let a=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-s));throw d.captureRequest({method:"ACTION",url:t,statusCode:500,durationMs:a,route:t,requestId:r,traceId:i,spanId:o}),d.addBreadcrumb({type:"server_action_error",message:t,metadata:{framework:"Next.js",statusCode:500,durationMs:a,requestId:r,traceId:i,spanId:o}}),k(u,{actionName:t,type:"server_action_error",requestId:r,traceId:i,spanId:o}),u}})}function R(t,e){if(!t?.headers)return;if(typeof t.headers.get=="function")return t.headers.get(e)||void 0;let n=t.headers[e]??t.headers[e.toLowerCase()];return Array.isArray(n)?n[0]:n?String(n):void 0}0&&(module.exports={audit,auditFetch,captureNextError,initNextDevInspector,withAuditAction,withAuditApi});
package/dist/next.js CHANGED
@@ -1 +1 @@
1
- var R="9.3.1",N=new Set(["angular","vanilla-js","react","vue","svelte","node","python","java","csharp","php","nextjs","react-native"]),k="https://api.devinspector.com.br/api/ingest/track",q="https://api.devinspector.com.br/api/ingest/apm",S=50,w=1e4,Q=6,x=1e4,L=5e3,z=["password","senha","token","authorization","auth","bearer","credit_card","creditcard","cartao","cvv","cpf","rg","secret","private_key","privatekey"];function j(t){let e=t.toLowerCase().replace(/[\s-]/g,"_");return z.some(n=>e.includes(n))}function g(t,e=0,n=new WeakSet){if(t==null)return t;if(typeof t=="string")return t.length>w?`${t.slice(0,w)}...[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>=Q)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=>g(o,e+1,n));let i=t,r={};for(let[o,s]of Object.entries(i)){if(j(o)){r[o]="[REDACTED]";continue}r[o]=g(s,e+1,n)}return r}finally{n.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=g(t);return typeof e=="string"?e||"Erro desconhecido":JSON.stringify(e)||"Erro desconhecido"}catch{return"Erro desconhecido"}}function E(t){return t&&String(t).trim().toUpperCase()||void 0}function C(t){if(!(typeof t!="number"||!Number.isFinite(t)))return Math.round(t)}function b(t){if(!(typeof t!="number"||!Number.isFinite(t)))return Math.max(0,Math.round(t))}function D(t){return t==null?void 0:String(t).trim().slice(0,100)||void 0}function B(t){return String(t??"").trim().slice(0,200)}function V(t){let e=Number(t);if(!Number.isFinite(e))throw new Error("metricValue deve ser um n\xFAmero finito.");return e}function I(t,e=200){return t==null?void 0:String(t).trim().slice(0,e)||void 0}function $(t){let e=I(t,50);if(e&&N.has(e))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 g(t)}var A=class{constructor(){this.apiKey="";this.endpoint=k;this.apmEndpoint=q;this.environment="production";this.sdkVersion=R;this.slowThresholdMs=300;this.requestTimeoutMs=x;this.breadcrumbs=[];this.initialized=!1;this.listenersAttached=!1;this.sendQueue=[];this.isProcessingQueue=!1;this.flushResolvers=[];this.connectionTestLastCheckedAt=0}init(e){if(!e?.apiKey?.trim())throw new Error("DevInspector: apiKey \xE9 obrigat\xF3rio.");this.apiKey=e.apiKey.trim(),this.endpoint=e.endpoint?.trim()||k,this.apmEndpoint=e.apmEndpoint?.trim()||q,this.environment=e.environment?.trim()||"production",this.release=e.release?.trim()||void 0,this.technology=$(e.technology),this.installationId=I(e.installationId,200),this.sdkVersion=I(e.sdkVersion,100)||R,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):x,this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0,this.connectionTestLastCheckedAt=0,this.connectionTestRequest=void 0,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}getTechnology(){return this.technology}getInstallationId(){return this.installationId}getSdkVersion(){return this.sdkVersion}getSlowThresholdMs(){return this.slowThresholdMs}getRequestTimeoutMs(){return this.requestTimeoutMs}isInitialized(){return this.initialized}setUser(e){this.user=e?g(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,w),metadata:e.metadata?g(e.metadata):void 0};this.breadcrumbs.push(n),this.breadcrumbs.length>S&&(this.breadcrumbs=this.breadcrumbs.slice(-S))}getBreadcrumbs(){return this.breadcrumbs.map(e=>l(e))}clearBreadcrumbs(){this.breadcrumbs=[]}getContext(){return{environment:this.environment,release:this.release,technology:this.technology,installationId:this.installationId,sdkVersion:this.sdkVersion,user:l(this.user),breadcrumbs:this.getBreadcrumbs()}}isIngestUrl(e){if(!e)return!1;try{let n=new URL(this.endpoint),i=new URL(this.apmEndpoint),r=new URL(e,n.origin),o=r.origin,s=r.pathname.replace(/\/$/,""),u=n.pathname.replace(/\/$/,""),a=i.pathname.replace(/\/$/,"");return o===n.origin&&s===u||o===i.origin&&s===a}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 n=E(e.method)||"GET",i=String(e.url||"").trim();if(!i)return Promise.resolve();let r=C(e.statusCode)??0,o=b(e.durationMs)??0,s=D(e.ip),u={...e.metadata||{},timestamp:new Date().toISOString()};return this.sendApm({type:"request_metric",method:n,url:i,statusCode:r,durationMs:o,...e.userAgent?{userAgent:e.userAgent}:{},...e.route!==void 0?{route:e.route}:{},...s?{ip:s}:{},...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:l(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:l(u)})}captureDbQuery(e){let n=String(e.operation||"").trim();if(!n)return Promise.resolve();let i=b(e.durationMs)??0,r={...e.metadata||{},operation:n,collection:e.collection||"Unknown",durationMs:i,timestamp:new Date().toISOString()};return this.sendApm({type:"db_query",message:`Database operation: ${n}`,url:e.url,method:"DB",durationMs:i,requestId:e.requestId,traceId:e.traceId,spanId:e.spanId,environment:this.environment,release:this.release,user:l(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:l(r)})}captureMetric(e,n,i={},r={}){let o=B(e);if(!o)return Promise.resolve();let s;try{s=V(n)}catch{return Promise.resolve()}return this.sendApm({type:"custom_metric",metricName:o,metricValue:s,requestId:r.requestId,traceId:r.traceId,spanId:r.spanId,environment:this.environment,release:this.release,user:l(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:l({...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,n={}){let i=e?.message?.trim()||"Erro desconhecido",r={...n},o=E(r.method),s=C(r.statusCode??r.status),u=b(r.durationMs),a=D(r.ip),p=r.url!==void 0&&r.url!==null?String(r.url).trim():void 0,h=typeof r.requestId=="string"&&r.requestId.trim()||void 0,f=typeof r.traceId=="string"&&r.traceId.trim()||void 0,c=typeof r.spanId=="string"&&r.spanId.trim()||void 0;return delete r.method,delete r.statusCode,delete r.status,delete r.durationMs,delete r.ip,delete r.requestId,delete r.traceId,delete r.spanId,this.sendError({type:"error",message:i,stackTrace:e?.stack||"",...p?{url:p}:{},...o?{method:o}:{},...s!==void 0?{statusCode:s}:{},...u!==void 0?{durationMs:u}:{},...a?{ip:a}:{},...h?{requestId:h}:{},...f?{traceId:f}:{},...c?{spanId:c}:{},environment:this.environment,release:this.release,user:l(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:l({...r,timestamp:new Date().toISOString()})})}captureException(e,n={}){let i=e instanceof Error?e:new Error(v(e));return this.captureError(i,n)}captureMessage(e,n={}){let i=String(e||"").trim();return i?this.sendApm({type:"message",message:i.slice(0,w),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()})}getIntegrationContext(){return{...this.technology?{technology:this.technology}:{},...this.installationId?{installationId:this.installationId}:{},...this.connectionTestId?{connectionTestId:this.connectionTestId}:{},sdkVersion:this.sdkVersion}}getPendingConnectionTestEndpoint(){try{let e=new URL(this.endpoint),n=new URL("/api/integrations/sdk/pending-test",e.origin);return this.technology&&n.searchParams.set("technology",this.technology),n.toString()}catch{return}}async refreshPendingConnectionTest(e=!1){if(!this.initialized||!this.apiKey||!this.technology)return;let n=Date.now();if(!e&&this.connectionTestLastCheckedAt>0&&n-this.connectionTestLastCheckedAt<L){this.connectionTestExpiresAt&&n>=this.connectionTestExpiresAt&&(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0);return}if(this.connectionTestRequest)return this.connectionTestRequest;let i=this.getPendingConnectionTestEndpoint();if(i)return this.connectionTestRequest=this.fetchPendingConnectionTest(i).finally(()=>{this.connectionTestRequest=void 0}),this.connectionTestRequest}async fetchPendingConnectionTest(e){this.connectionTestLastCheckedAt=Date.now();let n=typeof fetch=="function"?fetch:void 0;if(!n)return;let i,r;try{typeof AbortController<"u"&&(i=new AbortController,r=setTimeout(()=>{i?.abort()},this.requestTimeoutMs));let o=await n(e,{method:"GET",headers:{"x-api-key":this.apiKey},signal:i?.signal});if(!o.ok)return;let s=await o.json(),u=I(s.connectionTestId??s.testId,200);if(!u){this.connectionTestExpiresAt&&Date.now()>=this.connectionTestExpiresAt&&(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0);return}this.connectionTestId=u;let a=s.expiresAt?new Date(s.expiresAt).getTime():void 0;this.connectionTestExpiresAt=a&&Number.isFinite(a)?a:void 0}catch{return}finally{r!==void 0&&clearTimeout(r)}}clearConnectionTest(e){e&&this.connectionTestId!==e||(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0,this.connectionTestLastCheckedAt=Date.now())}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?.((i,r)=>{this.captureError(i,{platform:"react-native",fatal:r}).finally(()=>{typeof n=="function"&&n(i,r)})})}if(typeof window<"u"&&typeof window.addEventListener=="function"){window.addEventListener("error",n=>{let i=n.error instanceof Error?n.error:new Error(n.message||"Erro global");this.captureError(i,{url:n.filename||window.location.href,line:n.lineno,column:n.colno,platform:"browser"})}),window.addEventListener("unhandledrejection",n=>{let i=n.reason instanceof Error?n.reason:new Error(v(n.reason));this.captureError(i,{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 i=y("event"),r={...n,...this.getIntegrationContext(),eventId:i},o={id:i,endpoint:e,payload:l(r),resolve:()=>{}};return new Promise(s=>{o.resolve=s,this.sendQueue.push(o),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.refreshPendingConnectionTest();let n={...e.payload,...this.getIntegrationContext()},i=typeof n.connectionTestId=="string"?n.connectionTestId:void 0;await this.sendToEndpoint(e.endpoint,l(n))&&i&&this.clearConnectionTest(i)}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 i=typeof fetch=="function"?fetch:void 0;if(!i)return!1;let r=g(n),o,s;try{typeof AbortController<"u"&&(o=new AbortController,s=setTimeout(()=>{o?.abort()},this.requestTimeoutMs));let u=typeof window<"u"&&typeof window.document<"u",a={method:"POST",headers:{"content-type":"application/json","x-api-key":this.apiKey},body:JSON.stringify(r),signal:o?.signal};return u&&(a.keepalive=!0),(await i(e,a)).ok}catch{return!1}finally{s!==void 0&&clearTimeout(s)}}},U="__DEVINSPECT_AUDIT_CORE_INSTANCE__",P=typeof globalThis<"u"?globalThis:{},d=P[U]||new A;P[U]=d;async function _(t,e,n={}){let i=n.nativeFetch||(typeof fetch=="function"?fetch:void 0);if(!i)throw new Error("fetch n\xE3o est\xE1 dispon\xEDvel.");let r=typeof t=="string"?t:t instanceof URL?t.toString():String(t.url||""),o=E(e?.method||(typeof t!="string"&&!(t instanceof URL)?t.method:"GET"))||"GET";if(r&&d.isIngestUrl(r))return i(t,e);let s=d.createRequestId(),u=d.createTraceId(),a=d.createSpanId(),p=new Headers(e?.headers||(typeof t!="string"&&!(t instanceof URL)?t.headers:void 0));p.set("x-devinspector-request-id",s),p.set("x-devinspector-trace-id",u),p.set("x-devinspector-span-id",a);let h={...e,headers:p},f=typeof performance<"u"?performance.now():Date.now();try{let c=await i(t,h),m=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-f)),T=c.status>=400||m>=d.getSlowThresholdMs();return d.addBreadcrumb({type:"http",message:`${o} ${r}`,metadata:{phase:"complete",statusCode:c.status,durationMs:m,requestId:s,traceId:u,spanId:a}}),T&&d.captureRequest({method:o,url:r,statusCode:c.status,durationMs:m,userAgent:typeof navigator<"u"?navigator.userAgent:n.defaultUserAgent,requestId:s,traceId:u,spanId:a}),c}catch(c){let m=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-f));d.addBreadcrumb({type:"http_error",message:`${o} ${r}`,metadata:{phase:"error",statusCode:0,durationMs:m,requestId:s,traceId:u,spanId:a}}),d.captureRequest({method:o,url:r,statusCode:0,durationMs:m,userAgent:typeof navigator<"u"?navigator.userAgent:n.defaultUserAgent,requestId:s,traceId:u,spanId:a});let T=c instanceof Error?c:new Error(v(c));throw d.captureError(T,{url:r,method:o,type:n.errorType||"network_error",requestId:s,traceId:u,spanId:a}),c}}function G(t){d.init(t)}function O(t,e={}){let n=t instanceof Error?t:new Error(String(t));d.captureError(n,{...e,framework:"Next.js"})}async function H(t,e){return _(t,e,{errorType:"nextjs_network_error"})}function X(t){return async function(...e){let n=typeof performance<"u"?performance.now():Date.now(),i=e[0],r=e[1],o=i?.method||"GET",s=i?.url||i?.nextUrl?.href||"/api",u=typeof i?.headers?.get=="function"?i.headers.get("user-agent")||"":i?.headers?.["user-agent"]||"",a=M(i,"x-devinspector-request-id")||d.createRequestId(),p=M(i,"x-devinspector-trace-id")||d.createTraceId(),h=M(i,"x-devinspector-span-id")||d.createSpanId();d.addBreadcrumb({type:"http",message:`${o} ${s}`,metadata:{framework:"Next.js",phase:"start",requestId:a,traceId:p,spanId:h}});try{let f=await t(...e),c=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-n)),m=Number(f?.status??r?.statusCode??200)||200;return(m>=400||c>=d.getSlowThresholdMs())&&d.captureRequest({method:o,url:s,statusCode:m,durationMs:c,route:s,userAgent:u,requestId:a,traceId:p,spanId:h}),d.addBreadcrumb({type:"http",message:`${o} ${s}`,metadata:{framework:"Next.js",phase:"complete",statusCode:m,durationMs:c,requestId:a,traceId:p,spanId:h}}),f}catch(f){let c=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-n));throw d.captureRequest({method:o,url:s,statusCode:500,durationMs:c,route:s,userAgent:u,requestId:a,traceId:p,spanId:h}),d.addBreadcrumb({type:"http_error",message:`${o} ${s}`,metadata:{framework:"Next.js",statusCode:500,durationMs:c,requestId:a,traceId:p,spanId:h}}),O(f,{url:s,method:o,type:"api_route_error",requestId:a,traceId:p,spanId:h}),f}}}function J(t,e){return(async(...n)=>{let i=typeof performance<"u"?performance.now():Date.now(),r=d.createRequestId(),o=d.createTraceId(),s=d.createSpanId();d.addBreadcrumb({type:"server_action",message:t,metadata:{framework:"Next.js",phase:"start",requestId:r,traceId:o,spanId:s}});try{let u=await e(...n),a=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-i));return a>=d.getSlowThresholdMs()&&d.captureRequest({method:"ACTION",url:t,statusCode:200,durationMs:a,route:t,requestId:r,traceId:o,spanId:s}),d.addBreadcrumb({type:"server_action",message:t,metadata:{framework:"Next.js",phase:"complete",statusCode:200,durationMs:a,requestId:r,traceId:o,spanId:s}}),u}catch(u){let a=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-i));throw d.captureRequest({method:"ACTION",url:t,statusCode:500,durationMs:a,route:t,requestId:r,traceId:o,spanId:s}),d.addBreadcrumb({type:"server_action_error",message:t,metadata:{framework:"Next.js",statusCode:500,durationMs:a,requestId:r,traceId:o,spanId:s}}),O(u,{actionName:t,type:"server_action_error",requestId:r,traceId:o,spanId:s}),u}})}function M(t,e){if(!t?.headers)return;if(typeof t.headers.get=="function")return t.headers.get(e)||void 0;let n=t.headers[e]??t.headers[e.toLowerCase()];return Array.isArray(n)?n[0]:n?String(n):void 0}export{d as audit,H as auditFetch,O as captureNextError,G as initNextDevInspector,J as withAuditAction,X as withAuditApi};
1
+ var R="9.3.2",N=new Set(["angular","vanilla-js","react","vue","svelte","node","python","java","csharp","php","nextjs","react-native"]),k="https://api.devinspector.com.br/api/ingest/track",q="https://api.devinspector.com.br/api/ingest/apm",S=50,w=1e4,Q=6,C=1e4,L=5e3,z=["password","senha","token","authorization","auth","bearer","credit_card","creditcard","cartao","cvv","cpf","rg","secret","private_key","privatekey"];function j(t){let e=t.toLowerCase().replace(/[\s-]/g,"_");return z.some(n=>e.includes(n))}function g(t,e=0,n=new WeakSet){if(t==null)return t;if(typeof t=="string")return t.length>w?`${t.slice(0,w)}...[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>=Q)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(i=>g(i,e+1,n));let s=t,r={};for(let[i,o]of Object.entries(s)){if(j(i)){r[i]="[REDACTED]";continue}r[i]=g(o,e+1,n)}return r}finally{n.delete(t)}}function b(t){if(t instanceof Error)return t.message||"Erro desconhecido";if(typeof t=="string")return t||"Erro desconhecido";try{let e=g(t);return typeof e=="string"?e||"Erro desconhecido":JSON.stringify(e)||"Erro desconhecido"}catch{return"Erro desconhecido"}}function E(t){return t&&String(t).trim().toUpperCase()||void 0}function x(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 D(t){return t==null?void 0:String(t).trim().slice(0,100)||void 0}function B(t){return String(t??"").trim().slice(0,200)}function V(t){let e=Number(t);if(!Number.isFinite(e))throw new Error("metricValue deve ser um n\xFAmero finito.");return e}function I(t,e=200){return t==null?void 0:String(t).trim().slice(0,e)||void 0}function $(t){let e=I(t,50);if(e&&N.has(e))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 g(t)}var A=class{constructor(){this.apiKey="";this.endpoint=k;this.apmEndpoint=q;this.environment="production";this.sdkVersion=R;this.slowThresholdMs=300;this.requestTimeoutMs=C;this.breadcrumbs=[];this.initialized=!1;this.listenersAttached=!1;this.sendQueue=[];this.isProcessingQueue=!1;this.flushResolvers=[];this.connectionTestLastCheckedAt=0}init(e){if(!e?.apiKey?.trim())throw new Error("DevInspector: apiKey \xE9 obrigat\xF3rio.");this.apiKey=e.apiKey.trim(),this.endpoint=e.endpoint?.trim()||k,this.apmEndpoint=e.apmEndpoint?.trim()||q,this.environment=e.environment?.trim()||"production",this.release=e.release?.trim()||void 0,this.technology=$(e.technology),this.installationId=I(e.installationId,200),this.sdkVersion=I(e.sdkVersion,100)||R,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):C,this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0,this.connectionTestLastCheckedAt=0,this.connectionTestRequest=void 0,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}getTechnology(){return this.technology}getInstallationId(){return this.installationId}getSdkVersion(){return this.sdkVersion}getSlowThresholdMs(){return this.slowThresholdMs}getRequestTimeoutMs(){return this.requestTimeoutMs}isInitialized(){return this.initialized}setUser(e){this.user=e?g(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,w),metadata:e.metadata?g(e.metadata):void 0};this.breadcrumbs.push(n),this.breadcrumbs.length>S&&(this.breadcrumbs=this.breadcrumbs.slice(-S))}getBreadcrumbs(){return this.breadcrumbs.map(e=>l(e))}clearBreadcrumbs(){this.breadcrumbs=[]}getContext(){return{environment:this.environment,release:this.release,technology:this.technology,installationId:this.installationId,sdkVersion:this.sdkVersion,user:l(this.user),breadcrumbs:this.getBreadcrumbs()}}isIngestUrl(e){if(!e)return!1;try{let n=new URL(this.endpoint),s=new URL(this.apmEndpoint),r=new URL(e,n.origin),i=r.origin,o=r.pathname.replace(/\/$/,""),u=n.pathname.replace(/\/$/,""),a=s.pathname.replace(/\/$/,"");return i===n.origin&&o===u||i===s.origin&&o===a}catch{return e.includes(this.endpoint)||e.includes(this.apmEndpoint)}}createRequestId(){return y("req")}createTraceId(){return y("trace")}createSpanId(){return y("span")}async captureRequest(e){let n=E(e.method)||"GET",s=String(e.url||"").trim();if(!s)return;let r=x(e.statusCode)??0,i=v(e.durationMs)??0,o=D(e.ip),u={...e.metadata||{},timestamp:new Date().toISOString()};!(r>=400||i>=this.getSlowThresholdMs())&&(await this.refreshPendingConnectionTest(!0),!this.connectionTestId)||await this.sendApm({type:"request_metric",method:n,url:s,statusCode:r,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:l(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:l(u)})}captureDbQuery(e){let n=String(e.operation||"").trim();if(!n)return Promise.resolve();let s=v(e.durationMs)??0,r={...e.metadata||{},operation:n,collection:e.collection||"Unknown",durationMs:s,timestamp:new Date().toISOString()};return this.sendApm({type:"db_query",message:`Database operation: ${n}`,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(r)})}captureMetric(e,n,s={},r={}){let i=B(e);if(!i)return Promise.resolve();let o;try{o=V(n)}catch{return Promise.resolve()}return this.sendApm({type:"custom_metric",metricName:i,metricValue:o,requestId:r.requestId,traceId:r.traceId,spanId:r.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,n={}){let s=e?.message?.trim()||"Erro desconhecido",r={...n},i=E(r.method),o=x(r.statusCode??r.status),u=v(r.durationMs),a=D(r.ip),p=r.url!==void 0&&r.url!==null?String(r.url).trim():void 0,h=typeof r.requestId=="string"&&r.requestId.trim()||void 0,f=typeof r.traceId=="string"&&r.traceId.trim()||void 0,c=typeof r.spanId=="string"&&r.spanId.trim()||void 0;return delete r.method,delete r.statusCode,delete r.status,delete r.durationMs,delete r.ip,delete r.requestId,delete r.traceId,delete r.spanId,this.sendError({type:"error",message:s,stackTrace:e?.stack||"",...p?{url:p}:{},...i?{method:i}:{},...o!==void 0?{statusCode:o}:{},...u!==void 0?{durationMs:u}:{},...a?{ip:a}:{},...h?{requestId:h}:{},...f?{traceId:f}:{},...c?{spanId:c}:{},environment:this.environment,release:this.release,user:l(this.user),breadcrumbs:this.getBreadcrumbs(),metadata:l({...r,timestamp:new Date().toISOString()})})}captureException(e,n={}){let s=e instanceof Error?e:new Error(b(e));return this.captureError(s,n)}captureMessage(e,n={}){let s=String(e||"").trim();return s?this.sendApm({type:"message",message:s.slice(0,w),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()})}getIntegrationContext(){return{...this.technology?{technology:this.technology}:{},...this.installationId?{installationId:this.installationId}:{},...this.connectionTestId?{connectionTestId:this.connectionTestId}:{},sdkVersion:this.sdkVersion}}getPendingConnectionTestEndpoint(){try{let e=new URL(this.endpoint),n=new URL("/api/integrations/sdk/pending-test",e.origin);return this.technology&&n.searchParams.set("technology",this.technology),n.toString()}catch{return}}async refreshPendingConnectionTest(e=!1){if(!this.initialized||!this.apiKey||!this.technology)return;let n=Date.now();if(!e&&this.connectionTestLastCheckedAt>0&&n-this.connectionTestLastCheckedAt<L){this.connectionTestExpiresAt&&n>=this.connectionTestExpiresAt&&(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0);return}if(this.connectionTestRequest)return this.connectionTestRequest;let s=this.getPendingConnectionTestEndpoint();if(s)return this.connectionTestRequest=this.fetchPendingConnectionTest(s).finally(()=>{this.connectionTestRequest=void 0}),this.connectionTestRequest}async fetchPendingConnectionTest(e){this.connectionTestLastCheckedAt=Date.now();let n=typeof fetch=="function"?fetch:void 0;if(!n)return;let s,r;try{typeof AbortController<"u"&&(s=new AbortController,r=setTimeout(()=>{s?.abort()},this.requestTimeoutMs));let i=await n(e,{method:"GET",headers:{"x-api-key":this.apiKey},signal:s?.signal});if(!i.ok)return;let o=await i.json(),u=I(o.connectionTestId??o.testId,200);if(!u){this.connectionTestExpiresAt&&Date.now()>=this.connectionTestExpiresAt&&(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0);return}this.connectionTestId=u;let a=o.expiresAt?new Date(o.expiresAt).getTime():void 0;this.connectionTestExpiresAt=a&&Number.isFinite(a)?a:void 0}catch{return}finally{r!==void 0&&clearTimeout(r)}}clearConnectionTest(e){e&&this.connectionTestId!==e||(this.connectionTestId=void 0,this.connectionTestExpiresAt=void 0,this.connectionTestLastCheckedAt=Date.now())}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?.((s,r)=>{this.captureError(s,{platform:"react-native",fatal:r}).finally(()=>{typeof n=="function"&&n(s,r)})})}if(typeof window<"u"&&typeof window.addEventListener=="function"){window.addEventListener("error",n=>{let s=n.error instanceof Error?n.error:new Error(n.message||"Erro global");this.captureError(s,{url:n.filename||window.location.href,line:n.lineno,column:n.colno,platform:"browser"})}),window.addEventListener("unhandledrejection",n=>{let s=n.reason instanceof Error?n.reason:new Error(b(n.reason));this.captureError(s,{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 s=y("event"),r={...n,...this.getIntegrationContext(),eventId:s},i={id:s,endpoint:e,payload:l(r),resolve:()=>{}};return new Promise(o=>{i.resolve=o,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.refreshPendingConnectionTest();let n={...e.payload,...this.getIntegrationContext()},s=typeof n.connectionTestId=="string"?n.connectionTestId:void 0;await this.sendToEndpoint(e.endpoint,l(n))&&s&&this.clearConnectionTest(s)}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 s=typeof fetch=="function"?fetch:void 0;if(!s)return!1;let r=g(n),i,o;try{typeof AbortController<"u"&&(i=new AbortController,o=setTimeout(()=>{i?.abort()},this.requestTimeoutMs));let u=typeof window<"u"&&typeof window.document<"u",a={method:"POST",headers:{"content-type":"application/json","x-api-key":this.apiKey},body:JSON.stringify(r),signal:i?.signal};return u&&(a.keepalive=!0),(await s(e,a)).ok}catch{return!1}finally{o!==void 0&&clearTimeout(o)}}},U="__DEVINSPECT_AUDIT_CORE_INSTANCE__",P=typeof globalThis<"u"?globalThis:{},d=P[U]||new A;P[U]=d;async function _(t,e,n={}){let s=n.nativeFetch||(typeof fetch=="function"?fetch:void 0);if(!s)throw new Error("fetch n\xE3o est\xE1 dispon\xEDvel.");let r=typeof t=="string"?t:t instanceof URL?t.toString():String(t.url||""),i=E(e?.method||(typeof t!="string"&&!(t instanceof URL)?t.method:"GET"))||"GET";if(r&&d.isIngestUrl(r))return s(t,e);let o=d.createRequestId(),u=d.createTraceId(),a=d.createSpanId(),p=new Headers(e?.headers||(typeof t!="string"&&!(t instanceof URL)?t.headers:void 0));p.set("x-devinspector-request-id",o),p.set("x-devinspector-trace-id",u),p.set("x-devinspector-span-id",a);let h={...e,headers:p},f=typeof performance<"u"?performance.now():Date.now();try{let c=await s(t,h),m=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-f)),T=c.status>=400||m>=d.getSlowThresholdMs();return d.addBreadcrumb({type:"http",message:`${i} ${r}`,metadata:{phase:"complete",statusCode:c.status,durationMs:m,requestId:o,traceId:u,spanId:a}}),T?d.captureRequest({method:i,url:r,statusCode:c.status,durationMs:m,userAgent:typeof navigator<"u"?navigator.userAgent:n.defaultUserAgent,requestId:o,traceId:u,spanId:a}):d.captureRequest({method:i,url:r,statusCode:c.status,durationMs:m,userAgent:typeof navigator<"u"?navigator.userAgent:n.defaultUserAgent,requestId:o,traceId:u,spanId:a}),c}catch(c){let m=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-f));d.addBreadcrumb({type:"http_error",message:`${i} ${r}`,metadata:{phase:"error",statusCode:0,durationMs:m,requestId:o,traceId:u,spanId:a}}),d.captureRequest({method:i,url:r,statusCode:0,durationMs:m,userAgent:typeof navigator<"u"?navigator.userAgent:n.defaultUserAgent,requestId:o,traceId:u,spanId:a});let T=c instanceof Error?c:new Error(b(c));throw d.captureError(T,{url:r,method:i,type:n.errorType||"network_error",requestId:o,traceId:u,spanId:a}),c}}function G(t){d.init(t)}function O(t,e={}){let n=t instanceof Error?t:new Error(String(t));d.captureError(n,{...e,framework:"Next.js"})}async function H(t,e){return _(t,e,{errorType:"nextjs_network_error"})}function X(t){return async function(...e){let n=typeof performance<"u"?performance.now():Date.now(),s=e[0],r=e[1],i=s?.method||"GET",o=s?.url||s?.nextUrl?.href||"/api",u=typeof s?.headers?.get=="function"?s.headers.get("user-agent")||"":s?.headers?.["user-agent"]||"",a=M(s,"x-devinspector-request-id")||d.createRequestId(),p=M(s,"x-devinspector-trace-id")||d.createTraceId(),h=M(s,"x-devinspector-span-id")||d.createSpanId();d.addBreadcrumb({type:"http",message:`${i} ${o}`,metadata:{framework:"Next.js",phase:"start",requestId:a,traceId:p,spanId:h}});try{let f=await t(...e),c=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-n)),m=Number(f?.status??r?.statusCode??200)||200;return(m>=400||c>=d.getSlowThresholdMs())&&d.captureRequest({method:i,url:o,statusCode:m,durationMs:c,route:o,userAgent:u,requestId:a,traceId:p,spanId:h}),d.addBreadcrumb({type:"http",message:`${i} ${o}`,metadata:{framework:"Next.js",phase:"complete",statusCode:m,durationMs:c,requestId:a,traceId:p,spanId:h}}),f}catch(f){let c=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-n));throw d.captureRequest({method:i,url:o,statusCode:500,durationMs:c,route:o,userAgent:u,requestId:a,traceId:p,spanId:h}),d.addBreadcrumb({type:"http_error",message:`${i} ${o}`,metadata:{framework:"Next.js",statusCode:500,durationMs:c,requestId:a,traceId:p,spanId:h}}),O(f,{url:o,method:i,type:"api_route_error",requestId:a,traceId:p,spanId:h}),f}}}function J(t,e){return(async(...n)=>{let s=typeof performance<"u"?performance.now():Date.now(),r=d.createRequestId(),i=d.createTraceId(),o=d.createSpanId();d.addBreadcrumb({type:"server_action",message:t,metadata:{framework:"Next.js",phase:"start",requestId:r,traceId:i,spanId:o}});try{let u=await e(...n),a=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-s));return a>=d.getSlowThresholdMs()&&d.captureRequest({method:"ACTION",url:t,statusCode:200,durationMs:a,route:t,requestId:r,traceId:i,spanId:o}),d.addBreadcrumb({type:"server_action",message:t,metadata:{framework:"Next.js",phase:"complete",statusCode:200,durationMs:a,requestId:r,traceId:i,spanId:o}}),u}catch(u){let a=Math.max(0,Math.round((typeof performance<"u"?performance.now():Date.now())-s));throw d.captureRequest({method:"ACTION",url:t,statusCode:500,durationMs:a,route:t,requestId:r,traceId:i,spanId:o}),d.addBreadcrumb({type:"server_action_error",message:t,metadata:{framework:"Next.js",statusCode:500,durationMs:a,requestId:r,traceId:i,spanId:o}}),O(u,{actionName:t,type:"server_action_error",requestId:r,traceId:i,spanId:o}),u}})}function M(t,e){if(!t?.headers)return;if(typeof t.headers.get=="function")return t.headers.get(e)||void 0;let n=t.headers[e]??t.headers[e.toLowerCase()];return Array.isArray(n)?n[0]:n?String(n):void 0}export{d as audit,H as auditFetch,O as captureNextError,G as initNextDevInspector,J as withAuditAction,X as withAuditApi};