@eiaifactory/monitor 0.1.1 → 0.1.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/browser.js CHANGED
@@ -1,2 +1,2 @@
1
1
  function ue(e){let t=2166136261;for(let n=0;n<e.length;n++)t^=e.charCodeAt(n),t=Math.imul(t,16777619);return t>>>0}function N(e){let t=(e.stack??"").split(`
2
- `).find(n=>/^\s*at\s|@/.test(n))??"";return ue([e.origen,e.tipo_error??"",e.mensaje,t.trim()].join("|"))}function I(){return{items:[],minuto:0,enMinuto:0}}function L(e,t,n,r,i){let o=N(t),l=e.items.find(v=>!v.v&&v.h===o&&n-v.t<1e4);if(l)return l.e.repeticiones=(l.e.repeticiones??1)+1,"colapsado";let P=Math.floor(n/6e4);return P!==e.minuto&&(e.minuto=P,e.enMinuto=0),e.enMinuto>=i?"tope_por_minuto":(e.enMinuto++,e.items.push({e:t,h:o,t:n}),e.items.length>r&&e.items.splice(0,e.items.length-r),"encolado")}var le=128*1024;function V(e){let t=e.map(({e:i,h:o,t:l})=>JSON.stringify({e:i,h:o,t:l})),n=2,r=t.length;for(;r>0&&(n+t[r-1].length+1)*2<=le;)n+=t[--r].length+1;return`[${t.slice(r).join(",")}]`}function U(e){try{let t=e?JSON.parse(e):[];return Array.isArray(t)?t.filter(n=>!!n&&typeof n=="object"&&!!n.e&&typeof n.e.id_externo=="string"&&typeof n.e.mensaje=="string"&&typeof n.h=="number"&&typeof n.t=="number"):[]}catch{return[]}}var z="https://qoyskdjcjyjxcnhruhni.supabase.co/functions/v1/monitor-ingesta";var s={mensaje:1024,tipo_error:128,codigo:64,stack:8192,ubicacion:256,ruta:512,release:64,usuario_id:64,rol:64,dispositivo_id:64,user_agent:512,huella_manual:128},D="\u2026[cortado]";function g(e,t){return e.length<=t?e:e.slice(0,t-D.length)+D}var k=[1e3,2e3,4e3,3e4],X=e=>new TextEncoder().encode(e).length;function B(e,t=20){let n=[],r=X('{"eventos":[]}');for(let i of e){if(n.length>=t)break;let o=X(JSON.stringify(i.e))+(n.length?1:0);if(n.length&&r+o>61440)break;r+=o,n.push(i)}return n}function de(e){return 6e4-e%6e4}function fe(e,t,n){if(e>=200&&e<300)return{r:"ok"};if(e===429){let r=Number(t);return{r:"cuota",esperaMs:t&&r>0?r*1e3:de(n)}}return e===401?{r:"apagar"}:e===413?{r:"partir"}:e>=500?{r:"reintentar"}:{r:"descartar"}}async function $(e){let t;try{let n=typeof AbortController=="function"?new AbortController:void 0,r=new Promise(o=>{t=setTimeout(()=>{e.debug?.("timeout"),n?.abort(),o({r:"reintentar"})},e.timeoutMs)}),i=(async()=>{try{let o=await fetch(e.url,{method:"POST",headers:{authorization:`Bearer ${e.clave}`,"content-type":"application/json"},body:JSON.stringify({eventos:e.eventos}),keepalive:!!e.keepalive,signal:n?.signal}),l=await o.text();return e.debug?.(o.status,l),fe(o.status,o.headers.get("retry-after"),Date.now())}catch(o){return e.debug?.("sin respuesta",o),{r:"reintentar"}}})();return await Promise.race([i,r])}catch{return{r:"reintentar"}}finally{clearTimeout(t)}}function pe(e){return e.status??e.statusCode??e.context?.status}function J(e){try{if(!e||typeof e!="object")return!1;let t=e;if(t.name==="AbortError"||typeof t.code=="string"&&t.code.startsWith("PT4"))return!0;let n=Number(pe(t));return n===401||n===403?!0:typeof t.name=="string"&&t.name.startsWith("Auth")&&n>=400&&n<500}catch{return!1}}function q(e,t){return t.some(n=>typeof n=="string"?e.includes(n):e.search(n)>=0)}function m(){let e=typeof crypto>"u"?void 0:crypto;if(e&&typeof e.randomUUID=="function")try{return e.randomUUID()}catch{}let t=new Uint8Array(16);if(e&&typeof e.getRandomValues=="function")e.getRandomValues(t);else for(let r=0;r<16;r++)t[r]=Math.random()*256|0;t[6]=t[6]&15|64,t[8]=t[8]&63|128;let n=Array.from(t,r=>r.toString(16).padStart(2,"0")).join("");return`${n.slice(0,8)}-${n.slice(8,12)}-${n.slice(12,16)}-${n.slice(16,20)}-${n.slice(20)}`}var x="[redactado]",ge=/mail|tel|phone|dni|cuit|pass|token|secret|key|clave/i,me=/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g,xe=/(^|[^\w.-])\+?\d(?:[\s-]?\d){7,14}(?![\w.-])/g;function R(e){return e.replace(me,x).replace(xe,`$1${x}`)}function K(e){return e===void 0?e:R(e)}function Y(e){let t;if(e.contexto){t={};for(let n of Object.keys(e.contexto)){let r=e.contexto[n];t[n]=ge.test(n)?x:typeof r=="string"?R(r):r}}return{...e,mensaje:R(e.mensaje),stack:K(e.stack),ruta:K(e.ruta),usuario_id:e.usuario_id&&e.usuario_id.includes("@")?x:e.usuario_id,contexto:t}}function u(e,t){if((typeof e=="number"||typeof e=="boolean")&&(e=String(e)),typeof e!="string")return;let n=e.trim();return n?g(n,t):void 0}function be(e){let t=Object.prototype.toString.call(e);if(t==="[object Object]"||t==="[object Array]")try{let n=JSON.stringify(e);if(n&&n!=="{}")return n}catch{}return t}function Ee(e){try{if(e===null||typeof e!="object"&&typeof e!="function")return{mensaje:u(String(e),s.mensaje)??"Error sin mensaje"};let t=e,n=t.context,r=typeof t.code=="string"&&("details"in t||"hint"in t);return{mensaje:u(t.message,s.mensaje)??g(be(t),s.mensaje),tipo_error:u(t.name,s.tipo_error)??(r?"PostgrestError":void 0),codigo:u(t.code??t.status??t.statusCode??n?.status,s.codigo),stack:u(t.stack,s.stack)}}catch{return{mensaje:"Error no legible"}}}function _e(e){if(!e||typeof e!="object")return;let t={};for(let r of Object.keys(e)){let i=e[r];i===null||typeof i=="boolean"?t[r]=i:typeof i=="number"?t[r]=Number.isFinite(i)?i:null:typeof i=="string"&&(t[r]=g(i,256))}let n=Object.keys(t);if(n.length){for(;n.length&&new TextEncoder().encode(JSON.stringify(t)).length>2048;)delete t[n.pop()],t._cortado=!0;return t}}function he(e){return u(e?.split(/[?#]/)[0],s.ruta)}function F(e){return Y({id_externo:m(),origen:e.origen,...Ee(e.error),ocurrido_at:new Date(e.ocurrido).toISOString(),ubicacion:u(e.ubicacion,s.ubicacion),ruta:he(e.ruta),release:u(e.release,s.release),usuario_id:u(e.usuario_id,s.usuario_id),rol:u(e.rol,s.rol),dispositivo_id:u(e.dispositivo_id,s.dispositivo_id),user_agent:u(e.user_agent,s.user_agent),contexto:_e(e.contexto),repeticiones:1})}var ee="eiai_monitor_cola",Z="eiai_monitor_dispositivo",te=2e3,ne=10,ve=3e3,Oe=100,a,b=[],c,f,T={},M,y=!1,w=!1,d,G=0,W,E=!1,H=0,C=0,S=20;function p(...e){try{a?.debug&&console.debug("[monitor]",...e)}catch{}}function Q(e){try{return localStorage.getItem(e)}catch{return null}}function re(e,t){try{localStorage.setItem(e,t)}catch{}}function Fe(e){try{if(a)return;if(typeof window>"u"||!e?.clave){e?.debug&&console.debug("[monitor] no-op: sin clave");return}a={clave:e.clave,url:e.url??z,release:e.release,ignorar:e.ignorar??[],maxBuffer:e.maxBuffer??50,maxPorMinuto:e.maxPorMinuto??20,debug:!!e.debug},addEventListener("error",ke),addEventListener("unhandledrejection",Re),addEventListener("pagehide",ie),document.addEventListener("visibilitychange",Se),ae(),p("iniciado",a.url)}catch{}}function h(e,t){try{if(!a||E||y||b.length>=Oe)return;b.push({error:e,extra:t,t:Date.now(),ruta:location.pathname,contexto:T,usuario:M}),ae()}catch{}}function Ze(e){try{T={...T,...e}}catch{}}function Ge(e){M=e}function We(){M=void 0}async function He(){try{if(!a)return;for(_();;){let e=await j();if(!e||e.r!=="ok"&&e.r!=="descartar"&&e.r!=="partir")return}}catch{}}function Qe(e,t){try{h(e,{ubicacion:oe(t?.queryKey)})}catch{}}function et(e,t,n,r){try{h(e,{ubicacion:oe(r?.options?.mutationKey)})}catch{}}function oe(e){return Array.isArray(e)&&typeof e[0]=="string"?e[0]:void 0}function ke(e){try{h(e.error??{message:e.message,stack:e.filename?`at ${e.filename}:${e.lineno}:${e.colno}`:void 0})}catch{}}function Re(e){try{h(e.reason)}catch{}}function ie(){try{_(),clearTimeout(d),d=void 0,j(!0)}catch{}}function Se(){document.visibilityState==="hidden"&&ie()}function ae(){w||(w=!0,typeof requestIdleCallback=="function"?requestIdleCallback(_,{timeout:1e3}):setTimeout(_,0))}function Te(){return c||(c=I(),c.items=U(Q(ee)),f=Q(Z)??void 0,f||(f=m(),re(Z,f)),c.items.length&&p("recuperados",c.items.length),c)}function se(){c&&re(ee,V(c.items))}function _(){if(w=!1,!(!a||y)){y=!0;try{let e=Te(),t=b;b=[];let n=0;for(let r of t)try{if(J(r.error))continue;let i=F({error:r.error,origen:"browser",ocurrido:r.t,ruta:r.extra?.ruta??r.ruta,ubicacion:r.extra?.ubicacion,release:a.release,usuario_id:r.usuario?.id,rol:r.usuario?.rol,dispositivo_id:f,user_agent:navigator.userAgent,contexto:{...r.contexto,...r.extra?.contexto}});if(q(i.mensaje,a.ignorar))continue;let o=L(e,i,r.t,a.maxBuffer,a.maxPorMinuto);p(o,i.mensaje),o!=="tope_por_minuto"&&n++}catch{}n&&se(),e.items.length&&A(e.items.length>=ne?0:te)}catch{}finally{y=!1}}}function A(e){if(E)return;let t=Math.max(Date.now()+e,C);if(d!==void 0){if(t>=G)return;clearTimeout(d)}G=t,d=setTimeout(()=>{d=void 0,j()},t-Date.now())}function j(e=!1){return W??(W=we(e).catch(()=>{}).finally(()=>{W=void 0}))}async function we(e){try{if(!a||!c||E)return;let t=c;if(Date.now()<C){A(0);return}let n=B(t.items,S);if(!n.length)return;for(let o of n)o.v=!0;let r=await $({url:a.url,clave:a.clave,eventos:n.map(o=>o.e),timeoutMs:ve,keepalive:e,debug:a.debug?p:void 0});for(let o of n)o.v=!1;let i=()=>{t.items=t.items.filter(o=>!n.includes(o))};return r.r==="ok"||r.r==="descartar"||r.r==="partir"&&n.length===1?(i(),r.r==="ok"&&(H=0),S=20):r.r==="partir"?S=n.length>>1:r.r==="apagar"?(E=!0,clearTimeout(d),p("clave rechazada: apagado")):C=Date.now()+(r.r==="cuota"?r.esperaMs:k[Math.min(H++,k.length-1)]),se(),t.items.length&&A(t.items.length>=ne||r.r!=="ok"&&r.r!=="descartar"?0:te),r}catch{return}}export{h as capturar,J as esEsperado,He as flush,Fe as initMonitor,We as limpiarUsuario,et as onErrorMutation,Qe as onErrorQuery,Ze as setContext,Ge as setUsuario};
2
+ `).find(n=>/^\s*at\s|@/.test(n))??"";return ue([e.origen,e.tipo_error??"",e.mensaje,t.trim()].join("|"))}function I(){return{items:[],minuto:0,enMinuto:0}}function L(e,t,n,r,i){let o=N(t),l=e.items.find(v=>!v.v&&v.h===o&&n-v.t<1e4);if(l)return l.e.repeticiones=(l.e.repeticiones??1)+1,"colapsado";let P=Math.floor(n/6e4);return P!==e.minuto&&(e.minuto=P,e.enMinuto=0),e.enMinuto>=i?"tope_por_minuto":(e.enMinuto++,e.items.push({e:t,h:o,t:n}),e.items.length>r&&e.items.splice(0,e.items.length-r),"encolado")}var le=128*1024;function V(e){let t=e.map(({e:i,h:o,t:l})=>JSON.stringify({e:i,h:o,t:l})),n=2,r=t.length;for(;r>0&&(n+t[r-1].length+1)*2<=le;)n+=t[--r].length+1;return`[${t.slice(r).join(",")}]`}function U(e){try{let t=e?JSON.parse(e):[];return Array.isArray(t)?t.filter(n=>!!n&&typeof n=="object"&&!!n.e&&typeof n.e.id_externo=="string"&&typeof n.e.mensaje=="string"&&typeof n.h=="number"&&typeof n.t=="number"):[]}catch{return[]}}var z="https://qoyskdjcjyjxcnhruhni.supabase.co/functions/v1/monitor-ingesta";var s={mensaje:1024,tipo_error:128,codigo:64,stack:8192,ubicacion:256,ruta:512,release:64,usuario_id:64,rol:64,dispositivo_id:64,user_agent:512,huella_manual:128},D="\u2026[cortado]";function g(e,t){return e.length<=t?e:e.slice(0,t-D.length)+D}var k=[1e3,2e3,4e3,3e4],X=e=>new TextEncoder().encode(e).length;function B(e,t=20){let n=[],r=X('{"eventos":[]}');for(let i of e){if(n.length>=t)break;let o=X(JSON.stringify(i.e))+(n.length?1:0);if(n.length&&r+o>61440)break;r+=o,n.push(i)}return n}function de(e){return 6e4-e%6e4}function fe(e,t,n){if(e>=200&&e<300)return{r:"ok"};if(e===429){let r=Number(t);return{r:"cuota",esperaMs:t&&r>0?r*1e3:de(n)}}return e===401?{r:"apagar"}:e===413?{r:"partir"}:e>=500?{r:"reintentar"}:{r:"descartar"}}async function $(e){let t;try{let n=typeof AbortController=="function"?new AbortController:void 0,r=new Promise(o=>{t=setTimeout(()=>{e.debug?.("timeout"),n?.abort(),o({r:"reintentar"})},e.timeoutMs)}),i=(async()=>{try{let o=await fetch(e.url,{method:"POST",headers:{authorization:`Bearer ${e.clave}`,"content-type":"application/json"},body:JSON.stringify({eventos:e.eventos}),keepalive:!!e.keepalive,signal:n?.signal}),l=await o.text();return e.debug?.(o.status,l),fe(o.status,o.headers.get("retry-after"),Date.now())}catch(o){return e.debug?.("sin respuesta",o),{r:"reintentar"}}})();return await Promise.race([i,r])}catch{return{r:"reintentar"}}finally{clearTimeout(t)}}function pe(e){return e.status??e.statusCode??e.context?.status}function J(e){try{if(!e||typeof e!="object")return!1;let t=e;if(t.name==="AbortError"||typeof t.code=="string"&&t.code.startsWith("PT4"))return!0;let n=Number(pe(t));return n===401||n===403?!0:typeof t.name=="string"&&t.name.startsWith("Auth")&&n>=400&&n<500}catch{return!1}}function q(e,t){return t.some(n=>typeof n=="string"?e.includes(n):e.search(n)>=0)}function m(){let e=typeof crypto>"u"?void 0:crypto;if(e&&typeof e.randomUUID=="function")try{return e.randomUUID()}catch{}let t=new Uint8Array(16);if(e&&typeof e.getRandomValues=="function")e.getRandomValues(t);else for(let r=0;r<16;r++)t[r]=Math.random()*256|0;t[6]=t[6]&15|64,t[8]=t[8]&63|128;let n=Array.from(t,r=>r.toString(16).padStart(2,"0")).join("");return`${n.slice(0,8)}-${n.slice(8,12)}-${n.slice(12,16)}-${n.slice(16,20)}-${n.slice(20)}`}var x="[redactado]",ge=/mail|tel|phone|dni|cuit|pass|token|secret|key|clave/i,me=/[A-Za-z0-9._%+-]{1,64}@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g,xe=/(^|[^\w.-])\+?\d(?:[\s-]?\d){7,14}(?![\w.-])/g;function R(e){return e.replace(me,x).replace(xe,`$1${x}`)}function K(e){return e===void 0?e:R(e)}function Y(e){let t;if(e.contexto){t={};for(let n of Object.keys(e.contexto)){let r=e.contexto[n];t[n]=ge.test(n)?x:typeof r=="string"?R(r):r}}return{...e,mensaje:R(e.mensaje),stack:K(e.stack),ruta:K(e.ruta),usuario_id:e.usuario_id&&e.usuario_id.includes("@")?x:e.usuario_id,contexto:t}}function u(e,t){if((typeof e=="number"||typeof e=="boolean")&&(e=String(e)),typeof e!="string")return;let n=e.trim();return n?g(n,t):void 0}function be(e){let t=Object.prototype.toString.call(e);if(t==="[object Object]"||t==="[object Array]")try{let n=JSON.stringify(e);if(n&&n!=="{}")return n}catch{}return t}function Ee(e){try{if(e===null||typeof e!="object"&&typeof e!="function")return{mensaje:u(String(e),s.mensaje)??"Error sin mensaje"};let t=e,n=t.context,r=typeof t.code=="string"&&("details"in t||"hint"in t);return{mensaje:u(t.message,s.mensaje)??g(be(t),s.mensaje),tipo_error:u(t.name,s.tipo_error)??(r?"PostgrestError":void 0),codigo:u(t.code??t.status??t.statusCode??n?.status,s.codigo),stack:u(t.stack,s.stack)}}catch{return{mensaje:"Error no legible"}}}function _e(e){if(!e||typeof e!="object")return;let t={};for(let r of Object.keys(e)){let i=e[r];i===null||typeof i=="boolean"?t[r]=i:typeof i=="number"?t[r]=Number.isFinite(i)?i:null:typeof i=="string"&&(t[r]=g(i,256))}let n=Object.keys(t);if(n.length){for(;n.length&&new TextEncoder().encode(JSON.stringify(t)).length>2048;)delete t[n.pop()],t._cortado=!0;return t}}function he(e){return u(e?.split(/[?#]/)[0],s.ruta)}function F(e){return Y({id_externo:m(),origen:e.origen,...Ee(e.error),ocurrido_at:new Date(e.ocurrido).toISOString(),ubicacion:u(e.ubicacion,s.ubicacion),ruta:he(e.ruta),release:u(e.release,s.release),usuario_id:u(e.usuario_id,s.usuario_id),rol:u(e.rol,s.rol),dispositivo_id:u(e.dispositivo_id,s.dispositivo_id),user_agent:u(e.user_agent,s.user_agent),contexto:_e(e.contexto),repeticiones:1})}var ee="eiai_monitor_cola",Z="eiai_monitor_dispositivo",te=2e3,ne=10,ve=3e3,Oe=100,a,b=[],c,f,T={},M,y=!1,w=!1,d,G=0,W,E=!1,H=0,C=0,S=20;function p(...e){try{a?.debug&&console.debug("[monitor]",...e)}catch{}}function Q(e){try{return localStorage.getItem(e)}catch{return null}}function re(e,t){try{localStorage.setItem(e,t)}catch{}}function Fe(e){try{if(a)return;if(typeof window>"u"||!e?.clave){e?.debug&&console.debug("[monitor] no-op: sin clave");return}a={clave:e.clave,url:e.url??z,release:e.release,ignorar:e.ignorar??[],maxBuffer:e.maxBuffer??50,maxPorMinuto:e.maxPorMinuto??20,debug:!!e.debug},addEventListener("error",ke),addEventListener("unhandledrejection",Re),addEventListener("pagehide",ie),document.addEventListener("visibilitychange",Se),ae(),p("iniciado",a.url)}catch{}}function h(e,t){try{if(!a||E||y||b.length>=Oe)return;b.push({error:e,extra:t,t:Date.now(),ruta:location.pathname,contexto:T,usuario:M}),ae()}catch{}}function Ze(e){try{T={...T,...e}}catch{}}function Ge(e){M=e}function We(){M=void 0}async function He(){try{if(!a)return;for(_();;){let e=await j();if(!e||e.r!=="ok"&&e.r!=="descartar"&&e.r!=="partir")return}}catch{}}function Qe(e,t){try{h(e,{ubicacion:oe(t?.queryKey)})}catch{}}function et(e,t,n,r){try{h(e,{ubicacion:oe(r?.options?.mutationKey)})}catch{}}function oe(e){return Array.isArray(e)&&typeof e[0]=="string"?e[0]:void 0}function ke(e){try{h(e.error??{message:e.message,stack:e.filename?`at ${e.filename}:${e.lineno}:${e.colno}`:void 0})}catch{}}function Re(e){try{h(e.reason)}catch{}}function ie(){try{_(),clearTimeout(d),d=void 0,j(!0)}catch{}}function Se(){document.visibilityState==="hidden"&&ie()}function ae(){w||(w=!0,typeof requestIdleCallback=="function"?requestIdleCallback(_,{timeout:1e3}):setTimeout(_,0))}function Te(){return c||(c=I(),c.items=U(Q(ee)),f=Q(Z)??void 0,f||(f=m(),re(Z,f)),c.items.length&&p("recuperados",c.items.length),c)}function se(){c&&re(ee,V(c.items))}function _(){if(w=!1,!(!a||y)){y=!0;try{let e=Te(),t=b;b=[];let n=0;for(let r of t)try{if(J(r.error))continue;let i=F({error:r.error,origen:"browser",ocurrido:r.t,ruta:r.extra?.ruta??r.ruta,ubicacion:r.extra?.ubicacion,release:a.release,usuario_id:r.usuario?.id,rol:r.usuario?.rol,dispositivo_id:f,user_agent:navigator.userAgent,contexto:{...r.contexto,...r.extra?.contexto}});if(q(i.mensaje,a.ignorar))continue;let o=L(e,i,r.t,a.maxBuffer,a.maxPorMinuto);p(o,i.mensaje),o!=="tope_por_minuto"&&n++}catch{}n&&se(),e.items.length&&A(e.items.length>=ne?0:te)}catch{}finally{y=!1}}}function A(e){if(E)return;let t=Math.max(Date.now()+e,C);if(d!==void 0){if(t>=G)return;clearTimeout(d)}G=t,d=setTimeout(()=>{d=void 0,j()},t-Date.now())}function j(e=!1){return W??(W=we(e).catch(()=>{}).finally(()=>{W=void 0}))}async function we(e){try{if(!a||!c||E)return;let t=c;if(Date.now()<C){A(0);return}let n=B(t.items,S);if(!n.length)return;for(let o of n)o.v=!0;let r=await $({url:a.url,clave:a.clave,eventos:n.map(o=>o.e),timeoutMs:ve,keepalive:e,debug:a.debug?p:void 0});for(let o of n)o.v=!1;let i=()=>{t.items=t.items.filter(o=>!n.includes(o))};return r.r==="ok"||r.r==="descartar"||r.r==="partir"&&n.length===1?(i(),r.r==="ok"&&(H=0),S=20):r.r==="partir"?S=n.length>>1:r.r==="apagar"?(E=!0,clearTimeout(d),p("clave rechazada: apagado")):C=Date.now()+(r.r==="cuota"?r.esperaMs:k[Math.min(H++,k.length-1)]),se(),t.items.length&&A(t.items.length>=ne||r.r!=="ok"&&r.r!=="descartar"?0:te),r}catch{return}}export{h as capturar,J as esEsperado,He as flush,Fe as initMonitor,We as limpiarUsuario,et as onErrorMutation,Qe as onErrorQuery,Ze as setContext,Ge as setUsuario};
package/dist/deno.js CHANGED
@@ -1 +1 @@
1
- var E="https://qoyskdjcjyjxcnhruhni.supabase.co/functions/v1/monitor-ingesta";var s={mensaje:1024,tipo_error:128,codigo:64,stack:8192,ubicacion:256,ruta:512,release:64,usuario_id:64,rol:64,dispositivo_id:64,user_agent:512,huella_manual:128},m="\u2026[cortado]";function l(e,t){return e.length<=t?e:e.slice(0,t-m.length)+m}var x=e=>new TextEncoder().encode(e).length;function _(e,t=20){let n=[],r=x('{"eventos":[]}');for(let i of e){if(n.length>=t)break;let o=x(JSON.stringify(i.e))+(n.length?1:0);if(n.length&&r+o>61440)break;r+=o,n.push(i)}return n}function T(e){return 6e4-e%6e4}function S(e,t,n){if(e>=200&&e<300)return{r:"ok"};if(e===429){let r=Number(t);return{r:"cuota",esperaMs:t&&r>0?r*1e3:T(n)}}return e===401?{r:"apagar"}:e===413?{r:"partir"}:e>=500?{r:"reintentar"}:{r:"descartar"}}async function b(e){let t;try{let n=typeof AbortController=="function"?new AbortController:void 0,r=new Promise(o=>{t=setTimeout(()=>{e.debug?.("timeout"),n?.abort(),o({r:"reintentar"})},e.timeoutMs)}),i=(async()=>{try{let o=await fetch(e.url,{method:"POST",headers:{authorization:`Bearer ${e.clave}`,"content-type":"application/json"},body:JSON.stringify({eventos:e.eventos}),keepalive:!!e.keepalive,signal:n?.signal}),a=await o.text();return e.debug?.(o.status,a),S(o.status,o.headers.get("retry-after"),Date.now())}catch(o){return e.debug?.("sin respuesta",o),{r:"reintentar"}}})();return await Promise.race([i,r])}catch{return{r:"reintentar"}}finally{clearTimeout(t)}}function j(e){return e.status??e.statusCode??e.context?.status}function h(e){try{if(!e||typeof e!="object")return!1;let t=e;if(t.name==="AbortError"||typeof t.code=="string"&&t.code.startsWith("PT4"))return!0;let n=Number(j(t));return n===401||n===403?!0:typeof t.name=="string"&&t.name.startsWith("Auth")&&n>=400&&n<500}catch{return!1}}function R(){let e=typeof crypto>"u"?void 0:crypto;if(e&&typeof e.randomUUID=="function")try{return e.randomUUID()}catch{}let t=new Uint8Array(16);if(e&&typeof e.getRandomValues=="function")e.getRandomValues(t);else for(let r=0;r<16;r++)t[r]=Math.random()*256|0;t[6]=t[6]&15|64,t[8]=t[8]&63|128;let n=Array.from(t,r=>r.toString(16).padStart(2,"0")).join("");return`${n.slice(0,8)}-${n.slice(8,12)}-${n.slice(12,16)}-${n.slice(16,20)}-${n.slice(20)}`}var p="[redactado]",A=/mail|tel|phone|dni|cuit|pass|token|secret|key|clave/i,M=/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g,N=/(^|[^\w.-])\+?\d(?:[\s-]?\d){7,14}(?![\w.-])/g;function g(e){return e.replace(M,p).replace(N,`$1${p}`)}function O(e){return e===void 0?e:g(e)}function v(e){let t;if(e.contexto){t={};for(let n of Object.keys(e.contexto)){let r=e.contexto[n];t[n]=A.test(n)?p:typeof r=="string"?g(r):r}}return{...e,mensaje:g(e.mensaje),stack:O(e.stack),ruta:O(e.ruta),usuario_id:e.usuario_id&&e.usuario_id.includes("@")?p:e.usuario_id,contexto:t}}function u(e,t){if((typeof e=="number"||typeof e=="boolean")&&(e=String(e)),typeof e!="string")return;let n=e.trim();return n?l(n,t):void 0}function C(e){let t=Object.prototype.toString.call(e);if(t==="[object Object]"||t==="[object Array]")try{let n=JSON.stringify(e);if(n&&n!=="{}")return n}catch{}return t}function V(e){try{if(e===null||typeof e!="object"&&typeof e!="function")return{mensaje:u(String(e),s.mensaje)??"Error sin mensaje"};let t=e,n=t.context,r=typeof t.code=="string"&&("details"in t||"hint"in t);return{mensaje:u(t.message,s.mensaje)??l(C(t),s.mensaje),tipo_error:u(t.name,s.tipo_error)??(r?"PostgrestError":void 0),codigo:u(t.code??t.status??t.statusCode??n?.status,s.codigo),stack:u(t.stack,s.stack)}}catch{return{mensaje:"Error no legible"}}}function L(e){if(!e||typeof e!="object")return;let t={};for(let r of Object.keys(e)){let i=e[r];i===null||typeof i=="boolean"?t[r]=i:typeof i=="number"?t[r]=Number.isFinite(i)?i:null:typeof i=="string"&&(t[r]=l(i,256))}let n=Object.keys(t);if(n.length){for(;n.length&&new TextEncoder().encode(JSON.stringify(t)).length>2048;)delete t[n.pop()],t._cortado=!0;return t}}function I(e){return u(e?.split(/[?#]/)[0],s.ruta)}function w(e){return v({id_externo:R(),origen:e.origen,...V(e.error),ocurrido_at:new Date(e.ocurrido).toISOString(),ubicacion:u(e.ubicacion,s.ubicacion),ruta:I(e.ruta),release:u(e.release,s.release),usuario_id:u(e.usuario_id,s.usuario_id),rol:u(e.rol,s.rol),dispositivo_id:u(e.dispositivo_id,s.dispositivo_id),user_agent:u(e.user_agent,s.user_agent),contexto:L(e.contexto),repeticiones:1})}var X=2e3,U=50;function D(e){return new Promise(t=>setTimeout(t,e))}function re(e){let t=[],n=!e.clave,r,i=a=>b({url:e.url??E,clave:e.clave??"",eventos:a,timeoutMs:X});async function o(){let a=20;for(;t.length&&!n;){let c=_(t.map(f=>({e:f})),a).map(f=>f.e),d=await i(c);if((d.r==="reintentar"||d.r==="cuota")&&(await D(d.r==="cuota"?Math.min(d.esperaMs,1e4):1e3),d=await i(c)),d.r==="apagar"){n=!0,t.length=0;return}if(d.r==="partir"&&c.length>1){a=c.length>>1;continue}t.splice(0,c.length)}}return{funcion:e.funcion,capturar(a,c){try{if(n||h(a))return;t.length>=U&&t.shift(),t.push(w({error:a,origen:e.origen??"edge",ocurrido:Date.now(),ruta:c?.ruta,ubicacion:c?.ubicacion??e.funcion,release:e.release,contexto:c?.contexto}))}catch{}},flush(){return r??(r=o().catch(()=>{}).finally(()=>{r=void 0}))}}}function $(e){let t=e.flush();try{globalThis.EdgeRuntime?.waitUntil(t)}catch{}}function k(e){try{return new URL(e.url).pathname}catch{return}}function z(e,t){try{if(t.aRespuesta)return t.aRespuesta(e)}catch{}return new Response(JSON.stringify({error:"error_interno"}),{status:500,headers:{"content-type":"application/json"}})}function oe(e,t,n={}){return async(r,...i)=>{try{let o=await t(r,...i);return o.status>=500&&e.capturar({name:"HttpError",message:`${e.funcion}: respondi\xF3 ${o.status}`,status:o.status},{ruta:k(r)}),o}catch(o){let a=!1;try{a=!!n.esEsperado?.(o)}catch{}return a||e.capturar(o,{ruta:k(r)}),z(o,n)}finally{$(e)}}}export{h as esEsperado,re as initMonitor,oe as withHandler};
1
+ var E="https://qoyskdjcjyjxcnhruhni.supabase.co/functions/v1/monitor-ingesta";var s={mensaje:1024,tipo_error:128,codigo:64,stack:8192,ubicacion:256,ruta:512,release:64,usuario_id:64,rol:64,dispositivo_id:64,user_agent:512,huella_manual:128},m="\u2026[cortado]";function l(e,t){return e.length<=t?e:e.slice(0,t-m.length)+m}var x=e=>new TextEncoder().encode(e).length;function _(e,t=20){let n=[],r=x('{"eventos":[]}');for(let i of e){if(n.length>=t)break;let o=x(JSON.stringify(i.e))+(n.length?1:0);if(n.length&&r+o>61440)break;r+=o,n.push(i)}return n}function T(e){return 6e4-e%6e4}function S(e,t,n){if(e>=200&&e<300)return{r:"ok"};if(e===429){let r=Number(t);return{r:"cuota",esperaMs:t&&r>0?r*1e3:T(n)}}return e===401?{r:"apagar"}:e===413?{r:"partir"}:e>=500?{r:"reintentar"}:{r:"descartar"}}async function b(e){let t;try{let n=typeof AbortController=="function"?new AbortController:void 0,r=new Promise(o=>{t=setTimeout(()=>{e.debug?.("timeout"),n?.abort(),o({r:"reintentar"})},e.timeoutMs)}),i=(async()=>{try{let o=await fetch(e.url,{method:"POST",headers:{authorization:`Bearer ${e.clave}`,"content-type":"application/json"},body:JSON.stringify({eventos:e.eventos}),keepalive:!!e.keepalive,signal:n?.signal}),a=await o.text();return e.debug?.(o.status,a),S(o.status,o.headers.get("retry-after"),Date.now())}catch(o){return e.debug?.("sin respuesta",o),{r:"reintentar"}}})();return await Promise.race([i,r])}catch{return{r:"reintentar"}}finally{clearTimeout(t)}}function j(e){return e.status??e.statusCode??e.context?.status}function h(e){try{if(!e||typeof e!="object")return!1;let t=e;if(t.name==="AbortError"||typeof t.code=="string"&&t.code.startsWith("PT4"))return!0;let n=Number(j(t));return n===401||n===403?!0:typeof t.name=="string"&&t.name.startsWith("Auth")&&n>=400&&n<500}catch{return!1}}function R(){let e=typeof crypto>"u"?void 0:crypto;if(e&&typeof e.randomUUID=="function")try{return e.randomUUID()}catch{}let t=new Uint8Array(16);if(e&&typeof e.getRandomValues=="function")e.getRandomValues(t);else for(let r=0;r<16;r++)t[r]=Math.random()*256|0;t[6]=t[6]&15|64,t[8]=t[8]&63|128;let n=Array.from(t,r=>r.toString(16).padStart(2,"0")).join("");return`${n.slice(0,8)}-${n.slice(8,12)}-${n.slice(12,16)}-${n.slice(16,20)}-${n.slice(20)}`}var p="[redactado]",A=/mail|tel|phone|dni|cuit|pass|token|secret|key|clave/i,M=/[A-Za-z0-9._%+-]{1,64}@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g,N=/(^|[^\w.-])\+?\d(?:[\s-]?\d){7,14}(?![\w.-])/g;function g(e){return e.replace(M,p).replace(N,`$1${p}`)}function O(e){return e===void 0?e:g(e)}function v(e){let t;if(e.contexto){t={};for(let n of Object.keys(e.contexto)){let r=e.contexto[n];t[n]=A.test(n)?p:typeof r=="string"?g(r):r}}return{...e,mensaje:g(e.mensaje),stack:O(e.stack),ruta:O(e.ruta),usuario_id:e.usuario_id&&e.usuario_id.includes("@")?p:e.usuario_id,contexto:t}}function u(e,t){if((typeof e=="number"||typeof e=="boolean")&&(e=String(e)),typeof e!="string")return;let n=e.trim();return n?l(n,t):void 0}function C(e){let t=Object.prototype.toString.call(e);if(t==="[object Object]"||t==="[object Array]")try{let n=JSON.stringify(e);if(n&&n!=="{}")return n}catch{}return t}function V(e){try{if(e===null||typeof e!="object"&&typeof e!="function")return{mensaje:u(String(e),s.mensaje)??"Error sin mensaje"};let t=e,n=t.context,r=typeof t.code=="string"&&("details"in t||"hint"in t);return{mensaje:u(t.message,s.mensaje)??l(C(t),s.mensaje),tipo_error:u(t.name,s.tipo_error)??(r?"PostgrestError":void 0),codigo:u(t.code??t.status??t.statusCode??n?.status,s.codigo),stack:u(t.stack,s.stack)}}catch{return{mensaje:"Error no legible"}}}function L(e){if(!e||typeof e!="object")return;let t={};for(let r of Object.keys(e)){let i=e[r];i===null||typeof i=="boolean"?t[r]=i:typeof i=="number"?t[r]=Number.isFinite(i)?i:null:typeof i=="string"&&(t[r]=l(i,256))}let n=Object.keys(t);if(n.length){for(;n.length&&new TextEncoder().encode(JSON.stringify(t)).length>2048;)delete t[n.pop()],t._cortado=!0;return t}}function I(e){return u(e?.split(/[?#]/)[0],s.ruta)}function w(e){return v({id_externo:R(),origen:e.origen,...V(e.error),ocurrido_at:new Date(e.ocurrido).toISOString(),ubicacion:u(e.ubicacion,s.ubicacion),ruta:I(e.ruta),release:u(e.release,s.release),usuario_id:u(e.usuario_id,s.usuario_id),rol:u(e.rol,s.rol),dispositivo_id:u(e.dispositivo_id,s.dispositivo_id),user_agent:u(e.user_agent,s.user_agent),contexto:L(e.contexto),repeticiones:1})}var X=2e3,U=50;function D(e){return new Promise(t=>setTimeout(t,e))}function re(e){let t=[],n=!e.clave,r,i=a=>b({url:e.url??E,clave:e.clave??"",eventos:a,timeoutMs:X});async function o(){let a=20;for(;t.length&&!n;){let c=_(t.map(f=>({e:f})),a).map(f=>f.e),d=await i(c);if((d.r==="reintentar"||d.r==="cuota")&&(await D(d.r==="cuota"?Math.min(d.esperaMs,1e4):1e3),d=await i(c)),d.r==="apagar"){n=!0,t.length=0;return}if(d.r==="partir"&&c.length>1){a=c.length>>1;continue}t.splice(0,c.length)}}return{funcion:e.funcion,capturar(a,c){try{if(n||h(a))return;t.length>=U&&t.shift(),t.push(w({error:a,origen:e.origen??"edge",ocurrido:Date.now(),ruta:c?.ruta,ubicacion:c?.ubicacion??e.funcion,release:e.release,contexto:c?.contexto}))}catch{}},flush(){return r??(r=o().catch(()=>{}).finally(()=>{r=void 0}))}}}function $(e){let t=e.flush();try{globalThis.EdgeRuntime?.waitUntil(t)}catch{}}function k(e){try{return new URL(e.url).pathname}catch{return}}function z(e,t){try{if(t.aRespuesta)return t.aRespuesta(e)}catch{}return new Response(JSON.stringify({error:"error_interno"}),{status:500,headers:{"content-type":"application/json"}})}function oe(e,t,n={}){return async(r,...i)=>{try{let o=await t(r,...i);return o.status>=500&&e.capturar({name:"HttpError",message:`${e.funcion}: respondi\xF3 ${o.status}`,status:o.status},{ruta:k(r)}),o}catch(o){let a=!1;try{a=!!n.esEsperado?.(o)}catch{}return a||e.capturar(o,{ruta:k(r)}),z(o,n)}finally{$(e)}}}export{h as esEsperado,re as initMonitor,oe as withHandler};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eiaifactory/monitor",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Manda los errores de un sistema de cliente al Monitor de EI AI Factory, sin que el usuario final lo note.",
5
5
  "license": "MIT",
6
6
  "type": "module",