@walkeros/collector 3.4.1-next-1776790594143 → 3.4.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/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e,n=Object.defineProperty,t=Object.getOwnPropertyDescriptor,o=Object.getOwnPropertyNames,s=Object.prototype.hasOwnProperty,r={};((e,t)=>{for(var o in t)n(e,o,{get:t[o],enumerable:!0})})(r,{Code:()=>i,Commands:()=>a,Const:()=>c,addDestination:()=>$,callDestinationOn:()=>S,commonHandleCommand:()=>L,createEvent:()=>V,createPush:()=>J,createPushResult:()=>N,destinationInit:()=>E,destinationPush:()=>R,extractTransformerNextMap:()=>b,getCacheStore:()=>y,initDestinations:()=>H,initSource:()=>j,initSources:()=>A,mergeEnvironments:()=>T,on:()=>q,onApply:()=>I,processConsent:()=>l,pushToDestinations:()=>_,registerDestination:()=>M,runCollector:()=>W,runTransformerChain:()=>x,startFlow:()=>Y,transformerInit:()=>w,transformerPush:()=>C,walkChain:()=>k,wrapEnv:()=>ee}),module.exports=(e=r,((e,r,i,a)=>{if(r&&"object"==typeof r||"function"==typeof r)for(let c of o(r))s.call(e,c)||c===i||n(e,c,{get:()=>r[c],enumerable:!(a=t(r,c))||a.enumerable});return e})(n({},"__esModule",{value:!0}),e));var i={},a={Action:"action",Actions:"actions",Config:"config",Consent:"consent",Context:"context",Custom:"custom",Destination:"destination",Elb:"elb",Globals:"globals",Hook:"hook",Init:"init",Link:"link",On:"on",Prefix:"data-elb",Ready:"ready",Run:"run",Session:"session",Shutdown:"shutdown",User:"user",Walker:"walker"},c={Commands:a,Utils:{Storage:{Cookie:"cookie",Local:"local",Session:"session"}}},u=require("@walkeros/core");function l(e,n){const t={};return Object.entries(n).forEach(([e,n])=>{t[e]=!!n}),e.consent=(0,u.assign)(e.consent,t),{update:t}}var f=require("@walkeros/core"),d=require("@walkeros/core"),g=require("@walkeros/core"),p=require("@walkeros/core"),m=require("@walkeros/core"),h=require("@walkeros/core");function y(e,n){return e.storeId&&n.stores[e.storeId]?n.stores[e.storeId]:n.stores.__cache}function b(e){const n={};for(const[t,o]of Object.entries(e)){const e=o.config?.next;e&&!(0,h.isRouteArray)(e)?n[t]={next:e}:n[t]={}}return n}function v(e,n){const t=e.config||{},o=e[n];return void 0!==o?{config:{...t,[n]:o},chainValue:o}:{config:t,chainValue:void 0}}function k(e,n={}){if(!e)return[];if(Array.isArray(e))return e;const t=[],o=new Set;let s=e;for(;s&&n[s]&&!o.has(s);){o.add(s),t.push(s);const e=n[s].next;if(Array.isArray(e)){t.push(...e);break}s=e}return t}async function w(e,n,t){if(n.init&&!n.config.init){const o=n.type||"unknown",s=e.logger.scope(`transformer:${o}`),r={collector:e,logger:s,id:t,ingest:(0,h.createIngest)(t),config:n.config,env:O(n.config.env)};s.debug("init");const i=await(0,h.useHooks)(n.init,"TransformerInit",e.hooks,e.logger)(r);if(!1===i)return!1;n.config={...i||n.config,env:i?.env||n.config.env,init:!0},s.debug("init done")}return!0}async function C(e,n,t,o,s,r){const i=n.type||"unknown",a=e.logger.scope(`transformer:${i}`),c={collector:e,logger:a,id:t,ingest:s,config:n.config,env:{...O(n.config.env),...r?{respond:r}:{}}};a.debug("push",{event:o.name});const u=await(0,h.useHooks)(n.push,"TransformerPush",e.hooks,e.logger)(o,c);return a.debug("push done"),u}async function x(e,n,t,o,s,r,i){i&&s?._meta&&(s._meta.chainPath=i);let a=o,c=r;for(const o of t){const r=n[o];if(!r){e.logger.warn(`Transformer not found: ${o}`);continue}if(s&&s._meta&&s._meta.path.length>256)return e.logger.error(`Max path length exceeded at ${o}`),{event:null,respond:c};s&&s._meta&&(s._meta.hops++,s._meta.path.push(o));if(!await(0,h.tryCatchAsync)(w)(e,r,o))return e.logger.error(`Transformer init failed: ${o}`),{event:null,respond:c};if(i&&void 0!==r.config?.chainMocks?.[i]){const n=r.config.chainMocks[i];e.logger.scope(`transformer:${r.type||"unknown"}`).debug("chainMock",{chain:i}),a=n;continue}if(void 0!==r.config?.mock){e.logger.scope(`transformer:${r.type||"unknown"}`).debug("mock"),a=r.config.mock;continue}if(r.config?.disabled)continue;const u=r.config?.cache,l=u?(0,h.compileCache)(u):void 0,f=l?y(l,e):void 0;let d;if(l&&f){const e=(0,h.buildCacheContext)(s,a),n=(0,h.checkCache)(l,f,e,`t:${o}`);if("HIT"===n?.status&&n.value){if(a=n.value,l.full)return{event:a,respond:c};continue}"MISS"===n?.status&&(d={key:n.key,ttl:n.rule.ttl})}const g=r.config.before;if(g){const t=k("string"==typeof g||Array.isArray(g)&&!(0,h.isRouteArray)(g)?g:(0,h.resolveNext)((0,h.compileNext)(g),(0,h.buildCacheContext)(s,a))||void 0,b(n));if(t.length>0){const o=await x(e,n,t,a,s,c,i);if(null===o.event)return{event:null,respond:o.respond??c};o.respond&&(c=o.respond),a=Array.isArray(o.event)?o.event[0]:o.event}}const p=await(0,h.tryCatchAsync)(C,n=>(e.logger.scope(`transformer:${r.type||"unknown"}`).error("Push failed",{error:n}),!1))(e,r,o,a,s,c);if(!1===p)return{event:null,respond:c};if(Array.isArray(p)){const r=t.slice(t.indexOf(o)+1),u=await Promise.all(p.map(async t=>{const o=t.event||a,u=s?{...s,_meta:{...s._meta,path:[...s._meta.path]}}:(0,h.createIngest)("unknown");if(t.next){let s=t.next;if((0,h.isRouteArray)(t.next)){const e=(0,h.compileNext)(t.next);s=(0,h.resolveNext)(e,(0,h.buildCacheContext)(u,o))}if(s){const t=k(s,b(n));if(t.length>0)return x(e,n,t,o,u,c,i)}return{event:o,respond:c}}return r.length>0?x(e,n,r,o,u,c,i):{event:o,respond:c}}));let l=c;const f=[];for(const e of u.flat())if(null!==e)if(e&&"object"==typeof e&&"event"in e){const n=e;if(n.respond&&(l=n.respond),null===n.event)continue;Array.isArray(n.event)?f.push(...n.event):f.push(n.event)}else f.push(e);return 0===f.length?{event:null,respond:l}:1===f.length?{event:f[0],respond:l}:{event:f,respond:l}}if(p&&"object"==typeof p){const{event:t,respond:o,next:r}=p;if(o&&(c=o),r){let o=r;if((0,h.isRouteArray)(r)){const e=(0,h.compileNext)(r);if(o=(0,h.resolveNext)(e,(0,h.buildCacheContext)(s,a)),!o){t&&(a=t);continue}}const u=k(o,b(n));return u.length>0?x(e,n,u,t||a,s,c,i):(e.logger.warn(`Branch target not found: ${JSON.stringify(r)}`),{event:null,respond:c})}t&&(a=t)}if(d&&f&&(0,h.storeCache)(f,d.key,a,d.ttl),(!p||"object"==typeof p&&!p.next)&&r.config.next&&(0,h.isRouteArray)(r.config.next)){const t=r.config.next,o=(0,h.compileNext)(t),u=(0,h.resolveNext)(o,(0,h.buildCacheContext)(s,a));if(u){const t=k(u,b(n));if(t.length>0)return x(e,n,t,a,s,c,i)}return{event:a,respond:c}}}return{event:a,respond:c}}function O(e){return e&&(0,h.isObject)(e)?e:{}}async function j(e,n,t){const{code:o,config:s={},env:r={},primary:i,next:a,before:c,cache:u}=t;let l,f=(0,m.createIngest)(n);const d=u?(0,m.compileCache)({...u,full:u.full??!0}):void 0,g=(0,m.compileNext)(a),p=!(Array.isArray(a)&&(0,m.isRouteArray)(a))&&g?k((0,m.resolveNext)(g),b(e.transformers)):void 0,h=(0,m.compileNext)(c),v=!(Array.isArray(c)&&(0,m.isRouteArray)(c))&&h?k((0,m.resolveNext)(h),b(e.transformers)):void 0,w=e.logger.scope("source").scope(n),C={push:async(t,o={})=>{let r,i=t;const a=v??(h?k((0,m.resolveNext)(h,(0,m.buildCacheContext)(f)),b(e.transformers)):[]);if(a.length>0&&e.transformers&&Object.keys(e.transformers).length>0){const t=await x(e,e.transformers,a,i,f,l,`source.${n}.before`);if(null===t.event)return{ok:!0};t.respond&&(l=t.respond),i=Array.isArray(t.event)?t.event[0]:t.event}if(d){const t=y(d,e);if(t){const e=(0,m.buildCacheContext)(f),o=(0,m.checkCache)(d,t,e,`s:${n}`);if(o){if("HIT"===o.status&&void 0!==o.value&&d.full){let n=o.value;return o.rule.update&&(n=await(0,m.applyUpdate)(n,o.rule.update,{...e,cache:{status:"HIT"}})),l?.(n),{ok:!0}}if("MISS"===o.status&&d.full&&l){const n=l,s=o.rule.update,i={...e,cache:{status:"MISS"}},a=o.key,c=o.rule.ttl;l=e=>{(0,m.storeCache)(t,a,e,c),s?r=(async()=>{const t=await(0,m.applyUpdate)(e,s,i);n(t)})():n(e)}}"MISS"!==o.status||d.full||(0,m.storeCache)(t,o.key,!0,o.rule.ttl)}}}const c=p??(g?k((0,m.resolveNext)(g,(0,m.buildCacheContext)(f)),b(e.transformers)):[]),u=await e.push(i,{...o,id:n,ingest:f,respond:l,mapping:s,preChain:c});return r&&await r,u},command:e.command,sources:e.sources,elb:e.sources.elb.push,logger:w,...r},O={collector:e,logger:w,id:n,config:s,env:C,setIngest:async t=>{if(!s.ingest)return void(f=(0,m.createIngest)(n));const o=await(0,m.getMappingValue)(t,s.ingest,{collector:e}),r=(0,m.createIngest)(n);f={...r,...o,_meta:r._meta}},setRespond:e=>{l=e}},j=await(0,m.tryCatchAsync)(o)(O);if(!j)return;const A=j.type||"unknown",q=e.logger.scope(A).scope(n);return C.logger=q,i&&(j.config={...j.config,primary:i}),j}async function A(e,n={}){const t={};for(const[o,s]of Object.entries(n)){const{config:n={}}=s;if(n.require&&n.require.length>0){e.pending.sources[o]=s;continue}const r=await j(e,o,s);r&&(t[o]=r)}return t}async function q(e,n,t){const o=e.on,s=o[n]||[],r=(0,g.isArray)(t)?t:[t];r.forEach(e=>{s.push(e)}),o[n]=s,D(e,n,r)}function S(e,n,t,o,s){if(!n.on)return;const r=n.type||"unknown",i=e.logger.scope(r).scope("on").scope(o),a={collector:e,logger:i,id:t,config:n.config,data:s,env:T(n.env,n.config.env)};(0,p.tryCatch)(n.on)(o,a)}function D(e,n,t,o){let s;switch(n){case c.Commands.Consent:s=o||e.consent;break;case c.Commands.Session:s=e.session;break;case c.Commands.User:s=o||e.user;break;case c.Commands.Custom:s=o||e.custom;break;case c.Commands.Globals:s=o||e.globals;break;case c.Commands.Config:s=o||e.config;break;case c.Commands.Ready:case c.Commands.Run:default:s=void 0}if(t.length)switch(n){case c.Commands.Consent:!function(e,n,t){const o=t||e.consent;n.forEach(n=>{Object.keys(o).filter(e=>e in n).forEach(t=>{(0,p.tryCatch)(n[t])(e,o)})})}(e,t,o);break;case c.Commands.Ready:case c.Commands.Run:!function(e,n){e.allowed&&n.forEach(n=>{(0,p.tryCatch)(n)(e)})}(e,t);break;case c.Commands.Session:!function(e,n){if(!e.session)return;n.forEach(n=>{(0,p.tryCatch)(n)(e,e.session)})}(e,t);break;default:t.forEach(n=>{"function"==typeof n&&(0,p.tryCatch)(n)(e,s)})}}async function I(e,n,t,o){let s,r=t||[];switch(t||(r=e.on[n]||[]),n){case c.Commands.Consent:s=o||e.consent;break;case c.Commands.Session:s=e.session;break;case c.Commands.User:s=o||e.user;break;case c.Commands.Custom:s=o||e.custom;break;case c.Commands.Globals:s=o||e.globals;break;case c.Commands.Config:s=o||e.config;break;case c.Commands.Ready:case c.Commands.Run:default:s=void 0}let i=!1;for(const t of Object.values(e.sources))if(t.on){!1===await(0,p.tryCatchAsync)(t.on)(n,s)&&(i=!0)}return Object.entries(e.destinations).forEach(([t,o])=>{if(o.on){if(!o.config.init)return o.queueOn=o.queueOn||[],void o.queueOn.push({type:n,data:s});S(e,o,t,n,s)}}),(Object.keys(e.pending.sources).length>0||Object.keys(e.pending.destinations).length>0)&&await async function(e,n){for(const[t,o]of Object.entries(e.pending.sources)){if(!e.pending.sources[t]||e.sources[t])continue;const s=o.config?.require;if(!s)continue;const r=s.indexOf(n);if(-1===r)continue;if(s.splice(r,1),s.length>0)continue;delete e.pending.sources[t];const i=await j(e,t,o);i&&(e.sources[t]=i)}for(const[t,o]of Object.entries(e.pending.destinations)){if(!e.pending.destinations[t]||e.destinations[t])continue;const s=o.config?.require;if(!s)continue;const r=s.indexOf(n);if(-1===r)continue;if(s.splice(r,1),s.length>0)continue;delete e.pending.destinations[t];const i=M(o);!1!==i.config.queue&&(i.queuePush=[...e.queue]),e.destinations[t]=i}}(e,n),D(e,n,r,o),!i}function P(e,n,t,o){if(!e)return[];if(n){const e=(0,d.resolveNext)(n,(0,d.buildCacheContext)(o));return e?k(e,b(t)):[]}return k(e,b(t))}async function $(e,n,t){const{code:o,config:s={},env:r={},before:i,next:a,cache:c}=n;if(!(0,d.isFunction)(o.push))return N({ok:!1,failed:{invalid:{type:"invalid",error:"Destination code must have a push method"}}});const u=t||s||{init:!1};let l=i?{...u,before:i}:{...u};a&&(l={...l,next:a}),c&&(l={...l,cache:c});const f={...o,config:l,env:T(o.env,r)};let g=f.config.id;if(!g)do{g=(0,d.getId)(5,"abcdefghijklmnopqrstuvwxyz")}while(e.destinations[g]);return e.destinations[g]=f,!1!==f.config.queue&&(f.queuePush=[...e.queue]),_(e,void 0,{},{[g]:f})}async function _(e,n,t={},o){const{allowed:s,consent:r,globals:i,user:a}=e;if(!s)return N({ok:!1});n&&(e.queue.push(n),e.status.in++),o||(o=e.destinations);const c=await Promise.all(Object.entries(o||{}).map(async([o,s])=>{if(s.config.disabled)return{id:o,destination:s,skipped:!0};let c=(s.queuePush||[]).map(e=>({...e,consent:r}));s.queuePush=[],n&&c.push((0,d.clone)(n));const u=t.ingest?{...t.ingest,_meta:{...t.ingest._meta,path:[...t.ingest._meta.path]}}:(0,d.createIngest)("unknown");if(!c.length&&!s.queueOn?.length)return{id:o,destination:s,skipped:!0};if(!c.length&&s.queueOn?.length){const n=await(0,d.tryCatchAsync)(E)(e,s,o);return{id:o,destination:s,skipped:!n}}const l=[],f=c.filter(e=>{const n=(0,d.getGrantedConsent)(s.config.consent,r,e.consent);return!n||(e.consent=n,l.push(e),!1)});if(s.queuePush.push(...f),!l.length)return{id:o,destination:s,queue:c};if(!await(0,d.tryCatchAsync)(E)(e,s,o))return{id:o,destination:s,queue:c};let g,p;s.dlq||(s.dlq=[]);const m=s.config.before,h=P(m,m&&(0,d.isRouteArray)(m)?(0,d.compileNext)(m):void 0,e.transformers,u),b=s.config.next,v=b&&(0,d.isRouteArray)(b)?(0,d.compileNext)(b):void 0,k=s.config?.cache,w=k?(0,d.compileCache)(k):void 0,C=w?y(w,e):void 0;let O=0;return await Promise.all(l.map(async n=>{let r;if(n.globals=(0,d.assign)(i,n.globals),n.user=(0,d.assign)(a,n.user),w?.full&&C){const e=(0,d.buildCacheContext)(u,n),t=(0,d.checkCache)(w,C,e,`d:${o}`);if("HIT"===t?.status)return n;"MISS"===t?.status&&(r={key:t.key,ttl:t.rule.ttl})}let c=n,l=t.respond;if(h.length>0&&e.transformers&&Object.keys(e.transformers).length>0){const s=await x(e,e.transformers,h,n,u,t.respond,`destination.${o}.before`);if(null===s.event)return n;s.respond&&(l=s.respond),c=Array.isArray(s.event)?s.event[0]:s.event}if(w&&!w.full&&C){const e=(0,d.buildCacheContext)(u,c),t=(0,d.checkCache)(w,C,e,`d:${o}`);if("HIT"===t?.status)return n;"MISS"===t?.status&&(r={key:t.key,ttl:t.rule.ttl})}const f=Date.now();let m=!1;const y=await(0,d.tryCatchAsync)(R,n=>{const t=s.type||"unknown";e.logger.scope(t).error("Push failed",{error:n,event:c.name}),g=n,m=!0,s.dlq.push([c,n])})(e,s,o,c,u,l);if(O+=Date.now()-f,r&&C&&void 0===s.config.mock&&(0,d.storeCache)(C,r.key,y??!0,r.ttl),void 0!==y&&(p=y),!m&&b){void 0!==y&&(u._response=y);const n=P(b,v,e.transformers,u);if(n.length>0&&e.transformers&&Object.keys(e.transformers).length>0){const t=await x(e,e.transformers,n,c,u,l,`destination.${o}.next`);t.respond&&(l=t.respond)}}return n})),{id:o,destination:s,error:g,response:p,totalDuration:O}})),u={},l={},f={};for(const n of c){if(n.skipped)continue;const t={type:n.destination.type||"unknown",data:n.response};e.status.destinations[n.id]||(e.status.destinations[n.id]={count:0,failed:0,duration:0});const o=e.status.destinations[n.id],s=Date.now();n.error?(t.error=n.error,f[n.id]=t,o.failed++,o.lastAt=s,o.duration+=n.totalDuration||0,e.status.failed++):n.queue&&n.queue.length?l[n.id]=t:(u[n.id]=t,o.count++,o.lastAt=s,o.duration+=n.totalDuration||0,e.status.out++)}return N({event:n,...Object.keys(u).length&&{done:u},...Object.keys(l).length&&{queued:l},...Object.keys(f).length&&{failed:f}})}async function E(e,n,t){if(n.init&&!n.config.init){const o=n.type||"unknown",s=e.logger.scope(o),r={collector:e,logger:s,id:t,config:n.config,env:T(n.env,n.config.env)};s.debug("init");const i=await(0,d.useHooks)(n.init,"DestinationInit",e.hooks,e.logger)(r);if(!1===i)return i;if(n.config={...i||n.config,init:!0},n.queueOn?.length){const o=n.queueOn;n.queueOn=[];for(const{type:s,data:r}of o)S(e,n,t,s,r)}s.debug("init done")}return!0}async function R(e,n,t,o,s,r){const{config:i}=n,a=await(0,d.processEventMapping)(o,i,e);if(a.ignore)return!1;const c=n.type||"unknown",u=e.logger.scope(c),l={collector:e,logger:u,id:t,config:i,data:a.data,rule:a.mapping,ingest:s,env:{...T(n.env,i.env),...r?{respond:r}:{}}};if(void 0!==i.mock)return u.debug("mock",{event:a.event.name}),i.mock;const f=a.mapping,g=a.mappingKey||"* *";if(!f?.batch||!n.pushBatch||void 0!==i.mock){u.debug("push",{event:a.event.name});const t=await(0,d.useHooks)(n.push,"DestinationPush",e.hooks,e.logger)(a.event,l);return u.debug("push done"),t}{if(n.batches=n.batches||{},!n.batches[g]){const o={key:g,events:[],data:[]};n.batches[g]={batched:o,batchFn:(0,d.debounce)(()=>{const o=n.batches[g].batched,a={collector:e,logger:u,id:t,config:i,data:void 0,rule:f,ingest:s,env:{...T(n.env,i.env),...r?{respond:r}:{}}};u.debug("push batch",{events:o.events.length}),(0,d.useHooks)(n.pushBatch,"DestinationPushBatch",e.hooks,e.logger)(o,a),u.debug("push batch done"),o.events=[],o.data=[]},f.batch)}}const o=n.batches[g];o.batched.events.push(a.event),(0,d.isDefined)(a.data)&&o.batched.data.push(a.data),o.batchFn()}return!0}function N(e){return{ok:!e?.failed,...e}}function M(e){const{code:n,config:t={},env:o={},cache:s}=e,{config:r}=v(e,"before"),{config:i}=v({...e,config:r},"next"),a={...n.config,...t,...i};s&&(a.cache=s);const c=T(n.env,o);return{...n,config:a,env:c}}async function H(e,n={}){const t={};for(const[o,s]of Object.entries(n))s.config?.require?.length?e.pending.destinations[o]=s:t[o]=M(s);return t}function T(e,n){return e||n?n?e&&(0,d.isObject)(e)&&(0,d.isObject)(n)?{...e,...n}:n:e:{}}var G=require("@walkeros/core"),U=require("@walkeros/core"),F=5e3;async function B(e,n,t){const o=Object.entries(e).map(async([e,o])=>{const s=o.destroy;if(!s)return;const r=o.type||"unknown",i=t.scope(r),a={id:e,config:o.config,env:o.env??{},logger:i};try{await Promise.race([s(a),new Promise((t,o)=>setTimeout(()=>o(new Error(`${n} '${e}' destroy timed out`)),F))])}catch(t){i.error(`${n} '${e}' destroy failed: ${t}`)}});await Promise.allSettled(o)}async function L(e,n,t,o){let s,r,i=!1;switch(n){case c.Commands.Config:(0,U.isObject)(t)&&((0,G.assign)(e.config,t,{shallow:!1}),r=t,i=!0);break;case c.Commands.Consent:if((0,U.isObject)(t)){const{update:n}=l(e,t);r=n,i=!0}break;case c.Commands.Custom:(0,U.isObject)(t)&&(e.custom=(0,G.assign)(e.custom,t),r=t,i=!0);break;case c.Commands.Destination:(0,U.isObject)(t)&&("code"in t&&(0,U.isObject)(t.code)?s=await $(e,t,o):(0,G.isFunction)(t.push)&&(s=await $(e,{code:t},o)));break;case c.Commands.Globals:(0,U.isObject)(t)&&(e.globals=(0,G.assign)(e.globals,t),r=t,i=!0);break;case c.Commands.On:(0,G.isString)(t)&&await q(e,t,o);break;case c.Commands.Ready:i=!0;break;case c.Commands.Run:s=await W(e,t),i=!0;break;case c.Commands.Session:i=!0;break;case c.Commands.Shutdown:await async function(e){const n=e.logger;await B(e.sources,"source",n),await B(e.destinations,"destination",n),await B(e.transformers,"transformer",n),await B(e.stores,"store",n)}(e);break;case c.Commands.User:(0,U.isObject)(t)&&((0,G.assign)(e.user,t,{shallow:!1}),r=t,i=!0)}return i&&(await I(e,n,void 0,r),s=await _(e)),s||N({ok:!0})}function V(e,n){if(!n.name)throw new Error("Event name is required");const[t,o]=n.name.split(" ");if(!t||!o)throw new Error("Event name is invalid");++e.count;const{timestamp:s=Date.now(),group:r=e.group,count:i=e.count}=n,{name:a=`${t} ${o}`,data:c={},context:u={},globals:l=e.globals,custom:f={},user:d=e.user,nested:g=[],consent:p=e.consent,id:m=`${s}-${r}-${i}`,trigger:h="",entity:y=t,action:b=o,timing:v=0,version:k={source:e.version,tagging:e.config.tagging||0},source:w={type:"collector",id:"",previous_id:""}}=n;return{name:a,data:c,context:u,globals:l,custom:f,user:d,nested:g,consent:p,id:m,trigger:h,entity:y,action:b,timestamp:s,timing:v,group:r,count:i,version:k,source:w}}async function W(e,n){e.allowed=!0,e.count=0,e.group=(0,G.getId)(),e.timing=Date.now(),n&&(n.consent&&(e.consent=(0,G.assign)(e.consent,n.consent)),n.user&&(e.user=(0,G.assign)(e.user,n.user)),n.globals&&(e.globals=(0,G.assign)(e.config.globalsStatic||{},n.globals)),n.custom&&(e.custom=(0,G.assign)(e.custom,n.custom))),Object.values(e.destinations).forEach(e=>{e.queuePush=[]}),e.queue=[],e.round++;return await _(e)}var z=require("@walkeros/core");function J(e,n){return(0,z.useHooks)(async(t,o={})=>await(0,z.tryCatchAsync)(async()=>{const s=Date.now(),{id:r,ingest:i,respond:a,mapping:c,preChain:u,include:l,exclude:f}=o;let d=a,g=t;const p=l||f?function(e,n,t){let o=e;return n&&(o=Object.fromEntries(Object.entries(o).filter(([e])=>n.includes(e)))),t&&(o=Object.fromEntries(Object.entries(o).filter(([e])=>!t.includes(e)))),o}(e.destinations,l,f):void 0,m=i??(0,z.createIngest)(r||"unknown");if(c){const n=await(0,z.processEventMapping)(g,c,e);if(n.ignore)return N({ok:!0});if(c.consent){if(!(0,z.getGrantedConsent)(c.consent,e.consent,n.event.consent))return N({ok:!0})}g=n.event}if(u?.length&&e.transformers&&Object.keys(e.transformers).length>0){const t=await x(e,e.transformers,u,g,m,d,r?`source.${r}.next`:void 0);if(null===t.event)return N({ok:!0});if(t.respond&&(d=t.respond),Array.isArray(t.event)){const o=await Promise.all(t.event.map(async t=>{const o=n(t),s=V(e,o);return _(e,s,{id:r,ingest:m,respond:d},p)}));if(r){e.status.sources[r]||(e.status.sources[r]={count:0,duration:0});const n=e.status.sources[r];n.count+=t.event.length,n.lastAt=Date.now(),n.duration+=Date.now()-s}return o[0]??N({ok:!0})}g=t.event}const h=n(g),y=V(e,h),b=await _(e,y,{id:r,ingest:m,respond:d},p);if(r){e.status.sources[r]||(e.status.sources[r]={count:0,duration:0});const n=e.status.sources[r];n.count++,n.lastAt=Date.now(),n.duration+=Date.now()-s}return b},()=>N({ok:!1}))(),"Push",e.hooks,e.logger)}var K=require("@walkeros/core");var Q=require("@walkeros/core");async function X(e){const n=(0,f.assign)({globalsStatic:{},sessionStatic:{},tagging:0,run:!0},e,{merge:!1,extend:!1}),t={level:e.logger?.level,handler:e.logger?.handler},o=(0,f.createLogger)(t),s={...n.globalsStatic,...e.globals},r={allowed:!1,config:n,consent:e.consent||{},count:0,custom:e.custom||{},destinations:{},transformers:{},stores:{},globals:s,group:"",hooks:e.hooks||{},logger:o,on:{},queue:[],round:0,session:void 0,status:{startedAt:Date.now(),in:0,out:0,failed:0,sources:{},destinations:{}},timing:Date.now(),user:e.user||{},version:"3.4.1-next-1776790594143",sources:{},pending:{sources:{},destinations:{}},push:void 0,command:void 0};r.push=J(r,e=>({timing:Math.round((Date.now()-r.timing)/10)/100,source:{type:"collector",id:"",previous_id:""},...e})),r.command=function(e,n){return(0,K.useHooks)(async(t,o,s)=>await(0,K.tryCatchAsync)(async()=>await n(e,t,o,s),()=>N({ok:!1}))(),"Command",e.hooks,e.logger)}(r,L);const i=e.stores||{};if(r.stores=await async function(e,n={}){const t={};for(const[o,s]of Object.entries(n)){const{code:n,config:r={},env:i={}}=s,a=e.logger.scope("store").scope(o),c={collector:e,logger:a,id:o,config:r,env:i},u=await n(c),l=u.get,f=u.set,d=u.delete;u.get=(0,Q.useHooks)(l,"StoreGet",e.hooks,e.logger),u.set=(0,Q.useHooks)(f,"StoreSet",e.hooks,e.logger),u.delete=(0,Q.useHooks)(d,"StoreDelete",e.hooks,e.logger),t[o]=u}return t}(r,i),function(e,n,t){const o=new Map;for(const[t,s]of Object.entries(e))n[t]&&o.set(s,n[t]);if(0!==o.size)for(const e of[t.transformers,t.destinations,t.sources])if(e)for(const n of Object.values(e))s(n.env);function s(e){if(e)for(const[n,t]of Object.entries(e))if("object"==typeof t&&null!==t){const s=o.get(t);s&&(e[n]=s)}}}(i,r.stores,e),!r.stores.__cache){const e=new Map;r.stores.__cache={type:"memory",config:{},get:n=>{const t=e.get(n);if(t){if(!(t.expires&&Date.now()>t.expires))return t.value;e.delete(n)}},set:(n,t,o)=>{e.set(n,{value:t,expires:o?Date.now()+o:void 0})},delete:n=>{e.delete(n)}}}return r.destinations=await H(r,e.destinations||{}),r.transformers=await async function(e,n={}){const t={};for(const[o,s]of Object.entries(n)){const{code:n,env:r={}}=s,{config:i}=v(s,"before"),{config:a}=v({...s,config:i},"next"),c=Object.keys(r).length>0?{...a,env:r}:a,{cache:u}=s,l=u?{...c,cache:u}:c,f=e.logger.scope("transformer").scope(o),d={collector:e,logger:f,id:o,ingest:(0,h.createIngest)(o),config:l,env:r},g=await n(d);t[o]=g}return t}(r,e.transformers||{}),r}async function Y(e){e=e||{};const n=await X(e),t=(o=n,{type:"elb",config:{},push:async(e,n,t,s,r,i)=>{if("string"==typeof e&&e.startsWith("walker ")){const s=e.replace("walker ","");return o.command(s,n,t)}let a;if("string"==typeof e)a={name:e},n&&"object"==typeof n&&!Array.isArray(n)&&(a.data=n);else{if(!e||"object"!=typeof e)return N({ok:!1});a=e,n&&"object"==typeof n&&!Array.isArray(n)&&(a.data={...a.data||{},...n})}return s&&"object"==typeof s&&(a.context=s),r&&Array.isArray(r)&&(a.nested=r),i&&"object"==typeof i&&(a.custom=i),o.push(a)}});var o;n.sources.elb=t;const s=await A(n,e.sources||{});Object.assign(n.sources,s);const{consent:r,user:i,globals:a,custom:c}=e;r&&await n.command("consent",r),i&&await n.command("user",i),a&&Object.assign(n.globals,a),c&&Object.assign(n.custom,c),n.config.run&&await n.command("run");let u=t.push;const l=Object.values(n.sources).filter(e=>"elb"!==e.type),f=l.find(e=>e.config.primary);return f?u=f.push:l.length>0&&(u=l[0].push),{collector:n,elb:u}}function Z(e){if(null===e||"object"!=typeof e)return e;if(Array.isArray(e))return e.map(Z);const n={};for(const[t,o]of Object.entries(e))n[t]="function"==typeof o?o:Z(o);return n}function ee(e){const n=[],{simulation:t,...o}=e,s=Z(o);for(const e of t){const t=e.startsWith("call:")?e.slice(5):e,o=t.split(".");let r=s;for(let e=0;e<o.length-1&&null!=r[o[e]];e++)r=r[o[e]];const i=o[o.length-1];if(null==r||!(i in r))continue;const a=r[i];"function"==typeof a&&(r[i]=function(...e){return n.push({fn:t,args:e,ts:Date.now()}),a.apply(this,e)})}return{wrappedEnv:s,calls:n}}//# sourceMappingURL=index.js.map
|
|
1
|
+
"use strict";var e,n=Object.defineProperty,t=Object.getOwnPropertyDescriptor,o=Object.getOwnPropertyNames,s=Object.prototype.hasOwnProperty,r={};((e,t)=>{for(var o in t)n(e,o,{get:t[o],enumerable:!0})})(r,{Code:()=>i,Commands:()=>a,Const:()=>c,addDestination:()=>$,callDestinationOn:()=>S,commonHandleCommand:()=>L,createEvent:()=>V,createPush:()=>J,createPushResult:()=>N,destinationInit:()=>E,destinationPush:()=>R,extractTransformerNextMap:()=>b,getCacheStore:()=>y,initDestinations:()=>H,initSource:()=>j,initSources:()=>A,mergeEnvironments:()=>T,on:()=>q,onApply:()=>I,processConsent:()=>l,pushToDestinations:()=>_,registerDestination:()=>M,runCollector:()=>W,runTransformerChain:()=>x,startFlow:()=>Y,transformerInit:()=>w,transformerPush:()=>C,walkChain:()=>k,wrapEnv:()=>ee}),module.exports=(e=r,((e,r,i,a)=>{if(r&&"object"==typeof r||"function"==typeof r)for(let c of o(r))s.call(e,c)||c===i||n(e,c,{get:()=>r[c],enumerable:!(a=t(r,c))||a.enumerable});return e})(n({},"__esModule",{value:!0}),e));var i={},a={Action:"action",Actions:"actions",Config:"config",Consent:"consent",Context:"context",Custom:"custom",Destination:"destination",Elb:"elb",Globals:"globals",Hook:"hook",Init:"init",Link:"link",On:"on",Prefix:"data-elb",Ready:"ready",Run:"run",Session:"session",Shutdown:"shutdown",User:"user",Walker:"walker"},c={Commands:a,Utils:{Storage:{Cookie:"cookie",Local:"local",Session:"session"}}},u=require("@walkeros/core");function l(e,n){const t={};return Object.entries(n).forEach(([e,n])=>{t[e]=!!n}),e.consent=(0,u.assign)(e.consent,t),{update:t}}var f=require("@walkeros/core"),d=require("@walkeros/core"),g=require("@walkeros/core"),p=require("@walkeros/core"),m=require("@walkeros/core"),h=require("@walkeros/core");function y(e,n){return e.storeId&&n.stores[e.storeId]?n.stores[e.storeId]:n.stores.__cache}function b(e){const n={};for(const[t,o]of Object.entries(e)){const e=o.config?.next;e&&!(0,h.isRouteArray)(e)?n[t]={next:e}:n[t]={}}return n}function v(e,n){const t=e.config||{},o=e[n];return void 0!==o?{config:{...t,[n]:o},chainValue:o}:{config:t,chainValue:void 0}}function k(e,n={}){if(!e)return[];if(Array.isArray(e))return e;const t=[],o=new Set;let s=e;for(;s&&n[s]&&!o.has(s);){o.add(s),t.push(s);const e=n[s].next;if(Array.isArray(e)){t.push(...e);break}s=e}return t}async function w(e,n,t){if(n.init&&!n.config.init){const o=n.type||"unknown",s=e.logger.scope(`transformer:${o}`),r={collector:e,logger:s,id:t,ingest:(0,h.createIngest)(t),config:n.config,env:O(n.config.env)};s.debug("init");const i=await(0,h.useHooks)(n.init,"TransformerInit",e.hooks,e.logger)(r);if(!1===i)return!1;n.config={...i||n.config,env:i?.env||n.config.env,init:!0},s.debug("init done")}return!0}async function C(e,n,t,o,s,r){const i=n.type||"unknown",a=e.logger.scope(`transformer:${i}`),c={collector:e,logger:a,id:t,ingest:s,config:n.config,env:{...O(n.config.env),...r?{respond:r}:{}}};a.debug("push",{event:o.name});const u=await(0,h.useHooks)(n.push,"TransformerPush",e.hooks,e.logger)(o,c);return a.debug("push done"),u}async function x(e,n,t,o,s,r,i){i&&s?._meta&&(s._meta.chainPath=i);let a=o,c=r;for(const o of t){const r=n[o];if(!r){e.logger.warn(`Transformer not found: ${o}`);continue}if(s&&s._meta&&s._meta.path.length>256)return e.logger.error(`Max path length exceeded at ${o}`),{event:null,respond:c};s&&s._meta&&(s._meta.hops++,s._meta.path.push(o));if(!await(0,h.tryCatchAsync)(w)(e,r,o))return e.logger.error(`Transformer init failed: ${o}`),{event:null,respond:c};if(i&&void 0!==r.config?.chainMocks?.[i]){const n=r.config.chainMocks[i];e.logger.scope(`transformer:${r.type||"unknown"}`).debug("chainMock",{chain:i}),a=n;continue}if(void 0!==r.config?.mock){e.logger.scope(`transformer:${r.type||"unknown"}`).debug("mock"),a=r.config.mock;continue}if(r.config?.disabled)continue;const u=r.config?.cache,l=u?(0,h.compileCache)(u):void 0,f=l?y(l,e):void 0;let d;if(l&&f){const e=(0,h.buildCacheContext)(s,a),n=(0,h.checkCache)(l,f,e,`t:${o}`);if("HIT"===n?.status&&n.value){if(a=n.value,l.full)return{event:a,respond:c};continue}"MISS"===n?.status&&(d={key:n.key,ttl:n.rule.ttl})}const g=r.config.before;if(g){const t=k("string"==typeof g||Array.isArray(g)&&!(0,h.isRouteArray)(g)?g:(0,h.resolveNext)((0,h.compileNext)(g),(0,h.buildCacheContext)(s,a))||void 0,b(n));if(t.length>0){const o=await x(e,n,t,a,s,c,i);if(null===o.event)return{event:null,respond:o.respond??c};o.respond&&(c=o.respond),a=Array.isArray(o.event)?o.event[0]:o.event}}const p=await(0,h.tryCatchAsync)(C,n=>(e.logger.scope(`transformer:${r.type||"unknown"}`).error("Push failed",{error:n}),!1))(e,r,o,a,s,c);if(!1===p)return{event:null,respond:c};if(Array.isArray(p)){const r=t.slice(t.indexOf(o)+1),u=await Promise.all(p.map(async t=>{const o=t.event||a,u=s?{...s,_meta:{...s._meta,path:[...s._meta.path]}}:(0,h.createIngest)("unknown");if(t.next){let s=t.next;if((0,h.isRouteArray)(t.next)){const e=(0,h.compileNext)(t.next);s=(0,h.resolveNext)(e,(0,h.buildCacheContext)(u,o))}if(s){const t=k(s,b(n));if(t.length>0)return x(e,n,t,o,u,c,i)}return{event:o,respond:c}}return r.length>0?x(e,n,r,o,u,c,i):{event:o,respond:c}}));let l=c;const f=[];for(const e of u.flat())if(null!==e)if(e&&"object"==typeof e&&"event"in e){const n=e;if(n.respond&&(l=n.respond),null===n.event)continue;Array.isArray(n.event)?f.push(...n.event):f.push(n.event)}else f.push(e);return 0===f.length?{event:null,respond:l}:1===f.length?{event:f[0],respond:l}:{event:f,respond:l}}if(p&&"object"==typeof p){const{event:t,respond:o,next:r}=p;if(o&&(c=o),r){let o=r;if((0,h.isRouteArray)(r)){const e=(0,h.compileNext)(r);if(o=(0,h.resolveNext)(e,(0,h.buildCacheContext)(s,a)),!o){t&&(a=t);continue}}const u=k(o,b(n));return u.length>0?x(e,n,u,t||a,s,c,i):(e.logger.warn(`Branch target not found: ${JSON.stringify(r)}`),{event:null,respond:c})}t&&(a=t)}if(d&&f&&(0,h.storeCache)(f,d.key,a,d.ttl),(!p||"object"==typeof p&&!p.next)&&r.config.next&&(0,h.isRouteArray)(r.config.next)){const t=r.config.next,o=(0,h.compileNext)(t),u=(0,h.resolveNext)(o,(0,h.buildCacheContext)(s,a));if(u){const t=k(u,b(n));if(t.length>0)return x(e,n,t,a,s,c,i)}return{event:a,respond:c}}}return{event:a,respond:c}}function O(e){return e&&(0,h.isObject)(e)?e:{}}async function j(e,n,t){const{code:o,config:s={},env:r={},primary:i,next:a,before:c,cache:u}=t;let l,f=(0,m.createIngest)(n);const d=u?(0,m.compileCache)({...u,full:u.full??!0}):void 0,g=(0,m.compileNext)(a),p=!(Array.isArray(a)&&(0,m.isRouteArray)(a))&&g?k((0,m.resolveNext)(g),b(e.transformers)):void 0,h=(0,m.compileNext)(c),v=!(Array.isArray(c)&&(0,m.isRouteArray)(c))&&h?k((0,m.resolveNext)(h),b(e.transformers)):void 0,w=e.logger.scope("source").scope(n),C={push:async(t,o={})=>{let r,i=t;const a=v??(h?k((0,m.resolveNext)(h,(0,m.buildCacheContext)(f)),b(e.transformers)):[]);if(a.length>0&&e.transformers&&Object.keys(e.transformers).length>0){const t=await x(e,e.transformers,a,i,f,l,`source.${n}.before`);if(null===t.event)return{ok:!0};t.respond&&(l=t.respond),i=Array.isArray(t.event)?t.event[0]:t.event}if(d){const t=y(d,e);if(t){const e=(0,m.buildCacheContext)(f),o=(0,m.checkCache)(d,t,e,`s:${n}`);if(o){if("HIT"===o.status&&void 0!==o.value&&d.full){let n=o.value;return o.rule.update&&(n=await(0,m.applyUpdate)(n,o.rule.update,{...e,cache:{status:"HIT"}})),l?.(n),{ok:!0}}if("MISS"===o.status&&d.full&&l){const n=l,s=o.rule.update,i={...e,cache:{status:"MISS"}},a=o.key,c=o.rule.ttl;l=e=>{(0,m.storeCache)(t,a,e,c),s?r=(async()=>{const t=await(0,m.applyUpdate)(e,s,i);n(t)})():n(e)}}"MISS"!==o.status||d.full||(0,m.storeCache)(t,o.key,!0,o.rule.ttl)}}}const c=p??(g?k((0,m.resolveNext)(g,(0,m.buildCacheContext)(f)),b(e.transformers)):[]),u=await e.push(i,{...o,id:n,ingest:f,respond:l,mapping:s,preChain:c});return r&&await r,u},command:e.command,sources:e.sources,elb:e.sources.elb.push,logger:w,...r},O={collector:e,logger:w,id:n,config:s,env:C,setIngest:async t=>{if(!s.ingest)return void(f=(0,m.createIngest)(n));const o=await(0,m.getMappingValue)(t,s.ingest,{collector:e}),r=(0,m.createIngest)(n);f={...r,...o,_meta:r._meta}},setRespond:e=>{l=e}},j=await(0,m.tryCatchAsync)(o)(O);if(!j)return;const A=j.type||"unknown",q=e.logger.scope(A).scope(n);return C.logger=q,i&&(j.config={...j.config,primary:i}),j}async function A(e,n={}){const t={};for(const[o,s]of Object.entries(n)){const{config:n={}}=s;if(n.require&&n.require.length>0){e.pending.sources[o]=s;continue}const r=await j(e,o,s);r&&(t[o]=r)}return t}async function q(e,n,t){const o=e.on,s=o[n]||[],r=(0,g.isArray)(t)?t:[t];r.forEach(e=>{s.push(e)}),o[n]=s,D(e,n,r)}function S(e,n,t,o,s){if(!n.on)return;const r=n.type||"unknown",i=e.logger.scope(r).scope("on").scope(o),a={collector:e,logger:i,id:t,config:n.config,data:s,env:T(n.env,n.config.env)};(0,p.tryCatch)(n.on)(o,a)}function D(e,n,t,o){let s;switch(n){case c.Commands.Consent:s=o||e.consent;break;case c.Commands.Session:s=e.session;break;case c.Commands.User:s=o||e.user;break;case c.Commands.Custom:s=o||e.custom;break;case c.Commands.Globals:s=o||e.globals;break;case c.Commands.Config:s=o||e.config;break;case c.Commands.Ready:case c.Commands.Run:default:s=void 0}if(t.length)switch(n){case c.Commands.Consent:!function(e,n,t){const o=t||e.consent;n.forEach(n=>{Object.keys(o).filter(e=>e in n).forEach(t=>{(0,p.tryCatch)(n[t])(e,o)})})}(e,t,o);break;case c.Commands.Ready:case c.Commands.Run:!function(e,n){e.allowed&&n.forEach(n=>{(0,p.tryCatch)(n)(e)})}(e,t);break;case c.Commands.Session:!function(e,n){if(!e.session)return;n.forEach(n=>{(0,p.tryCatch)(n)(e,e.session)})}(e,t);break;default:t.forEach(n=>{"function"==typeof n&&(0,p.tryCatch)(n)(e,s)})}}async function I(e,n,t,o){let s,r=t||[];switch(t||(r=e.on[n]||[]),n){case c.Commands.Consent:s=o||e.consent;break;case c.Commands.Session:s=e.session;break;case c.Commands.User:s=o||e.user;break;case c.Commands.Custom:s=o||e.custom;break;case c.Commands.Globals:s=o||e.globals;break;case c.Commands.Config:s=o||e.config;break;case c.Commands.Ready:case c.Commands.Run:default:s=void 0}let i=!1;for(const t of Object.values(e.sources))if(t.on){!1===await(0,p.tryCatchAsync)(t.on)(n,s)&&(i=!0)}return Object.entries(e.destinations).forEach(([t,o])=>{if(o.on){if(!o.config.init)return o.queueOn=o.queueOn||[],void o.queueOn.push({type:n,data:s});S(e,o,t,n,s)}}),(Object.keys(e.pending.sources).length>0||Object.keys(e.pending.destinations).length>0)&&await async function(e,n){for(const[t,o]of Object.entries(e.pending.sources)){if(!e.pending.sources[t]||e.sources[t])continue;const s=o.config?.require;if(!s)continue;const r=s.indexOf(n);if(-1===r)continue;if(s.splice(r,1),s.length>0)continue;delete e.pending.sources[t];const i=await j(e,t,o);i&&(e.sources[t]=i)}for(const[t,o]of Object.entries(e.pending.destinations)){if(!e.pending.destinations[t]||e.destinations[t])continue;const s=o.config?.require;if(!s)continue;const r=s.indexOf(n);if(-1===r)continue;if(s.splice(r,1),s.length>0)continue;delete e.pending.destinations[t];const i=M(o);!1!==i.config.queue&&(i.queuePush=[...e.queue]),e.destinations[t]=i}}(e,n),D(e,n,r,o),!i}function P(e,n,t,o){if(!e)return[];if(n){const e=(0,d.resolveNext)(n,(0,d.buildCacheContext)(o));return e?k(e,b(t)):[]}return k(e,b(t))}async function $(e,n,t){const{code:o,config:s={},env:r={},before:i,next:a,cache:c}=n;if(!(0,d.isFunction)(o.push))return N({ok:!1,failed:{invalid:{type:"invalid",error:"Destination code must have a push method"}}});const u=t||s||{init:!1};let l=i?{...u,before:i}:{...u};a&&(l={...l,next:a}),c&&(l={...l,cache:c});const f={...o,config:l,env:T(o.env,r)};let g=f.config.id;if(!g)do{g=(0,d.getId)(5,"abcdefghijklmnopqrstuvwxyz")}while(e.destinations[g]);return e.destinations[g]=f,!1!==f.config.queue&&(f.queuePush=[...e.queue]),_(e,void 0,{},{[g]:f})}async function _(e,n,t={},o){const{allowed:s,consent:r,globals:i,user:a}=e;if(!s)return N({ok:!1});n&&(e.queue.push(n),e.status.in++),o||(o=e.destinations);const c=await Promise.all(Object.entries(o||{}).map(async([o,s])=>{if(s.config.disabled)return{id:o,destination:s,skipped:!0};let c=(s.queuePush||[]).map(e=>({...e,consent:r}));s.queuePush=[],n&&c.push((0,d.clone)(n));const u=t.ingest?{...t.ingest,_meta:{...t.ingest._meta,path:[...t.ingest._meta.path]}}:(0,d.createIngest)("unknown");if(!c.length&&!s.queueOn?.length)return{id:o,destination:s,skipped:!0};if(!c.length&&s.queueOn?.length){const n=await(0,d.tryCatchAsync)(E)(e,s,o);return{id:o,destination:s,skipped:!n}}const l=[],f=c.filter(e=>{const n=(0,d.getGrantedConsent)(s.config.consent,r,e.consent);return!n||(e.consent=n,l.push(e),!1)});if(s.queuePush.push(...f),!l.length)return{id:o,destination:s,queue:c};if(!await(0,d.tryCatchAsync)(E)(e,s,o))return{id:o,destination:s,queue:c};let g,p;s.dlq||(s.dlq=[]);const m=s.config.before,h=P(m,m&&(0,d.isRouteArray)(m)?(0,d.compileNext)(m):void 0,e.transformers,u),b=s.config.next,v=b&&(0,d.isRouteArray)(b)?(0,d.compileNext)(b):void 0,k=s.config?.cache,w=k?(0,d.compileCache)(k):void 0,C=w?y(w,e):void 0;let O=0;return await Promise.all(l.map(async n=>{let r;if(n.globals=(0,d.assign)(i,n.globals),n.user=(0,d.assign)(a,n.user),w?.full&&C){const e=(0,d.buildCacheContext)(u,n),t=(0,d.checkCache)(w,C,e,`d:${o}`);if("HIT"===t?.status)return n;"MISS"===t?.status&&(r={key:t.key,ttl:t.rule.ttl})}let c=n,l=t.respond;if(h.length>0&&e.transformers&&Object.keys(e.transformers).length>0){const s=await x(e,e.transformers,h,n,u,t.respond,`destination.${o}.before`);if(null===s.event)return n;s.respond&&(l=s.respond),c=Array.isArray(s.event)?s.event[0]:s.event}if(w&&!w.full&&C){const e=(0,d.buildCacheContext)(u,c),t=(0,d.checkCache)(w,C,e,`d:${o}`);if("HIT"===t?.status)return n;"MISS"===t?.status&&(r={key:t.key,ttl:t.rule.ttl})}const f=Date.now();let m=!1;const y=await(0,d.tryCatchAsync)(R,n=>{const t=s.type||"unknown";e.logger.scope(t).error("Push failed",{error:n,event:c.name}),g=n,m=!0,s.dlq.push([c,n])})(e,s,o,c,u,l);if(O+=Date.now()-f,r&&C&&void 0===s.config.mock&&(0,d.storeCache)(C,r.key,y??!0,r.ttl),void 0!==y&&(p=y),!m&&b){void 0!==y&&(u._response=y);const n=P(b,v,e.transformers,u);if(n.length>0&&e.transformers&&Object.keys(e.transformers).length>0){const t=await x(e,e.transformers,n,c,u,l,`destination.${o}.next`);t.respond&&(l=t.respond)}}return n})),{id:o,destination:s,error:g,response:p,totalDuration:O}})),u={},l={},f={};for(const n of c){if(n.skipped)continue;const t={type:n.destination.type||"unknown",data:n.response};e.status.destinations[n.id]||(e.status.destinations[n.id]={count:0,failed:0,duration:0});const o=e.status.destinations[n.id],s=Date.now();n.error?(t.error=n.error,f[n.id]=t,o.failed++,o.lastAt=s,o.duration+=n.totalDuration||0,e.status.failed++):n.queue&&n.queue.length?l[n.id]=t:(u[n.id]=t,o.count++,o.lastAt=s,o.duration+=n.totalDuration||0,e.status.out++)}return N({event:n,...Object.keys(u).length&&{done:u},...Object.keys(l).length&&{queued:l},...Object.keys(f).length&&{failed:f}})}async function E(e,n,t){if(n.init&&!n.config.init){const o=n.type||"unknown",s=e.logger.scope(o),r={collector:e,logger:s,id:t,config:n.config,env:T(n.env,n.config.env)};s.debug("init");const i=await(0,d.useHooks)(n.init,"DestinationInit",e.hooks,e.logger)(r);if(!1===i)return i;if(n.config={...i||n.config,init:!0},n.queueOn?.length){const o=n.queueOn;n.queueOn=[];for(const{type:s,data:r}of o)S(e,n,t,s,r)}s.debug("init done")}return!0}async function R(e,n,t,o,s,r){const{config:i}=n,a=await(0,d.processEventMapping)(o,i,e);if(a.ignore)return!1;const c=n.type||"unknown",u=e.logger.scope(c),l={collector:e,logger:u,id:t,config:i,data:a.data,rule:a.mapping,ingest:s,env:{...T(n.env,i.env),...r?{respond:r}:{}}};if(void 0!==i.mock)return u.debug("mock",{event:a.event.name}),i.mock;const f=a.mapping,g=a.mappingKey||"* *";if(!f?.batch||!n.pushBatch||void 0!==i.mock){u.debug("push",{event:a.event.name});const t=await(0,d.useHooks)(n.push,"DestinationPush",e.hooks,e.logger)(a.event,l);return u.debug("push done"),t}{if(n.batches=n.batches||{},!n.batches[g]){const o={key:g,events:[],data:[]};n.batches[g]={batched:o,batchFn:(0,d.debounce)(()=>{const o=n.batches[g].batched,a={collector:e,logger:u,id:t,config:i,data:void 0,rule:f,ingest:s,env:{...T(n.env,i.env),...r?{respond:r}:{}}};u.debug("push batch",{events:o.events.length}),(0,d.useHooks)(n.pushBatch,"DestinationPushBatch",e.hooks,e.logger)(o,a),u.debug("push batch done"),o.events=[],o.data=[]},f.batch)}}const o=n.batches[g];o.batched.events.push(a.event),(0,d.isDefined)(a.data)&&o.batched.data.push(a.data),o.batchFn()}return!0}function N(e){return{ok:!e?.failed,...e}}function M(e){const{code:n,config:t={},env:o={},cache:s}=e,{config:r}=v(e,"before"),{config:i}=v({...e,config:r},"next"),a={...n.config,...t,...i};s&&(a.cache=s);const c=T(n.env,o);return{...n,config:a,env:c}}async function H(e,n={}){const t={};for(const[o,s]of Object.entries(n))s.config?.require?.length?e.pending.destinations[o]=s:t[o]=M(s);return t}function T(e,n){return e||n?n?e&&(0,d.isObject)(e)&&(0,d.isObject)(n)?{...e,...n}:n:e:{}}var G=require("@walkeros/core"),U=require("@walkeros/core"),F=5e3;async function B(e,n,t){const o=Object.entries(e).map(async([e,o])=>{const s=o.destroy;if(!s)return;const r=o.type||"unknown",i=t.scope(r),a={id:e,config:o.config,env:o.env??{},logger:i};try{await Promise.race([s(a),new Promise((t,o)=>setTimeout(()=>o(new Error(`${n} '${e}' destroy timed out`)),F))])}catch(t){i.error(`${n} '${e}' destroy failed: ${t}`)}});await Promise.allSettled(o)}async function L(e,n,t,o){let s,r,i=!1;switch(n){case c.Commands.Config:(0,U.isObject)(t)&&((0,G.assign)(e.config,t,{shallow:!1}),r=t,i=!0);break;case c.Commands.Consent:if((0,U.isObject)(t)){const{update:n}=l(e,t);r=n,i=!0}break;case c.Commands.Custom:(0,U.isObject)(t)&&(e.custom=(0,G.assign)(e.custom,t),r=t,i=!0);break;case c.Commands.Destination:(0,U.isObject)(t)&&("code"in t&&(0,U.isObject)(t.code)?s=await $(e,t,o):(0,G.isFunction)(t.push)&&(s=await $(e,{code:t},o)));break;case c.Commands.Globals:(0,U.isObject)(t)&&(e.globals=(0,G.assign)(e.globals,t),r=t,i=!0);break;case c.Commands.On:(0,G.isString)(t)&&await q(e,t,o);break;case c.Commands.Ready:i=!0;break;case c.Commands.Run:s=await W(e,t),i=!0;break;case c.Commands.Session:i=!0;break;case c.Commands.Shutdown:await async function(e){const n=e.logger;await B(e.sources,"source",n),await B(e.destinations,"destination",n),await B(e.transformers,"transformer",n),await B(e.stores,"store",n)}(e);break;case c.Commands.User:(0,U.isObject)(t)&&((0,G.assign)(e.user,t,{shallow:!1}),r=t,i=!0)}return i&&(await I(e,n,void 0,r),s=await _(e)),s||N({ok:!0})}function V(e,n){if(!n.name)throw new Error("Event name is required");const[t,o]=n.name.split(" ");if(!t||!o)throw new Error("Event name is invalid");++e.count;const{timestamp:s=Date.now(),group:r=e.group,count:i=e.count}=n,{name:a=`${t} ${o}`,data:c={},context:u={},globals:l=e.globals,custom:f={},user:d=e.user,nested:g=[],consent:p=e.consent,id:m=`${s}-${r}-${i}`,trigger:h="",entity:y=t,action:b=o,timing:v=0,version:k={source:e.version,tagging:e.config.tagging||0},source:w={type:"collector",id:"",previous_id:""}}=n;return{name:a,data:c,context:u,globals:l,custom:f,user:d,nested:g,consent:p,id:m,trigger:h,entity:y,action:b,timestamp:s,timing:v,group:r,count:i,version:k,source:w}}async function W(e,n){e.allowed=!0,e.count=0,e.group=(0,G.getId)(),e.timing=Date.now(),n&&(n.consent&&(e.consent=(0,G.assign)(e.consent,n.consent)),n.user&&(e.user=(0,G.assign)(e.user,n.user)),n.globals&&(e.globals=(0,G.assign)(e.config.globalsStatic||{},n.globals)),n.custom&&(e.custom=(0,G.assign)(e.custom,n.custom))),Object.values(e.destinations).forEach(e=>{e.queuePush=[]}),e.queue=[],e.round++;return await _(e)}var z=require("@walkeros/core");function J(e,n){return(0,z.useHooks)(async(t,o={})=>await(0,z.tryCatchAsync)(async()=>{const s=Date.now(),{id:r,ingest:i,respond:a,mapping:c,preChain:u,include:l,exclude:f}=o;let d=a,g=t;const p=l||f?function(e,n,t){let o=e;return n&&(o=Object.fromEntries(Object.entries(o).filter(([e])=>n.includes(e)))),t&&(o=Object.fromEntries(Object.entries(o).filter(([e])=>!t.includes(e)))),o}(e.destinations,l,f):void 0,m=i??(0,z.createIngest)(r||"unknown");if(c){const n=await(0,z.processEventMapping)(g,c,e);if(n.ignore)return N({ok:!0});if(c.consent){if(!(0,z.getGrantedConsent)(c.consent,e.consent,n.event.consent))return N({ok:!0})}g=n.event}if(u?.length&&e.transformers&&Object.keys(e.transformers).length>0){const t=await x(e,e.transformers,u,g,m,d,r?`source.${r}.next`:void 0);if(null===t.event)return N({ok:!0});if(t.respond&&(d=t.respond),Array.isArray(t.event)){const o=await Promise.all(t.event.map(async t=>{const o=n(t),s=V(e,o);return _(e,s,{id:r,ingest:m,respond:d},p)}));if(r){e.status.sources[r]||(e.status.sources[r]={count:0,duration:0});const n=e.status.sources[r];n.count+=t.event.length,n.lastAt=Date.now(),n.duration+=Date.now()-s}return o[0]??N({ok:!0})}g=t.event}const h=n(g),y=V(e,h),b=await _(e,y,{id:r,ingest:m,respond:d},p);if(r){e.status.sources[r]||(e.status.sources[r]={count:0,duration:0});const n=e.status.sources[r];n.count++,n.lastAt=Date.now(),n.duration+=Date.now()-s}return b},()=>N({ok:!1}))(),"Push",e.hooks,e.logger)}var K=require("@walkeros/core");var Q=require("@walkeros/core");async function X(e){const n=(0,f.assign)({globalsStatic:{},sessionStatic:{},tagging:0,run:!0},e,{merge:!1,extend:!1}),t={level:e.logger?.level,handler:e.logger?.handler},o=(0,f.createLogger)(t),s={...n.globalsStatic,...e.globals},r={allowed:!1,config:n,consent:e.consent||{},count:0,custom:e.custom||{},destinations:{},transformers:{},stores:{},globals:s,group:"",hooks:e.hooks||{},logger:o,on:{},queue:[],round:0,session:void 0,status:{startedAt:Date.now(),in:0,out:0,failed:0,sources:{},destinations:{}},timing:Date.now(),user:e.user||{},version:"3.4.2",sources:{},pending:{sources:{},destinations:{}},push:void 0,command:void 0};r.push=J(r,e=>({timing:Math.round((Date.now()-r.timing)/10)/100,source:{type:"collector",id:"",previous_id:""},...e})),r.command=function(e,n){return(0,K.useHooks)(async(t,o,s)=>await(0,K.tryCatchAsync)(async()=>await n(e,t,o,s),()=>N({ok:!1}))(),"Command",e.hooks,e.logger)}(r,L);const i=e.stores||{};if(r.stores=await async function(e,n={}){const t={};for(const[o,s]of Object.entries(n)){const{code:n,config:r={},env:i={}}=s,a=e.logger.scope("store").scope(o),c={collector:e,logger:a,id:o,config:r,env:i},u=await n(c),l=u.get,f=u.set,d=u.delete;u.get=(0,Q.useHooks)(l,"StoreGet",e.hooks,e.logger),u.set=(0,Q.useHooks)(f,"StoreSet",e.hooks,e.logger),u.delete=(0,Q.useHooks)(d,"StoreDelete",e.hooks,e.logger),t[o]=u}return t}(r,i),function(e,n,t){const o=new Map;for(const[t,s]of Object.entries(e))n[t]&&o.set(s,n[t]);if(0!==o.size)for(const e of[t.transformers,t.destinations,t.sources])if(e)for(const n of Object.values(e))s(n.env);function s(e){if(e)for(const[n,t]of Object.entries(e))if("object"==typeof t&&null!==t){const s=o.get(t);s&&(e[n]=s)}}}(i,r.stores,e),!r.stores.__cache){const e=new Map;r.stores.__cache={type:"memory",config:{},get:n=>{const t=e.get(n);if(t){if(!(t.expires&&Date.now()>t.expires))return t.value;e.delete(n)}},set:(n,t,o)=>{e.set(n,{value:t,expires:o?Date.now()+o:void 0})},delete:n=>{e.delete(n)}}}return r.destinations=await H(r,e.destinations||{}),r.transformers=await async function(e,n={}){const t={};for(const[o,s]of Object.entries(n)){const{code:n,env:r={}}=s,{config:i}=v(s,"before"),{config:a}=v({...s,config:i},"next"),c=Object.keys(r).length>0?{...a,env:r}:a,{cache:u}=s,l=u?{...c,cache:u}:c,f=e.logger.scope("transformer").scope(o),d={collector:e,logger:f,id:o,ingest:(0,h.createIngest)(o),config:l,env:r},g=await n(d);t[o]=g}return t}(r,e.transformers||{}),r}async function Y(e){e=e||{};const n=await X(e),t=(o=n,{type:"elb",config:{},push:async(e,n,t,s,r,i)=>{if("string"==typeof e&&e.startsWith("walker ")){const s=e.replace("walker ","");return o.command(s,n,t)}let a;if("string"==typeof e)a={name:e},n&&"object"==typeof n&&!Array.isArray(n)&&(a.data=n);else{if(!e||"object"!=typeof e)return N({ok:!1});a=e,n&&"object"==typeof n&&!Array.isArray(n)&&(a.data={...a.data||{},...n})}return s&&"object"==typeof s&&(a.context=s),r&&Array.isArray(r)&&(a.nested=r),i&&"object"==typeof i&&(a.custom=i),o.push(a)}});var o;n.sources.elb=t;const s=await A(n,e.sources||{});Object.assign(n.sources,s);const{consent:r,user:i,globals:a,custom:c}=e;r&&await n.command("consent",r),i&&await n.command("user",i),a&&Object.assign(n.globals,a),c&&Object.assign(n.custom,c),n.config.run&&await n.command("run");let u=t.push;const l=Object.values(n.sources).filter(e=>"elb"!==e.type),f=l.find(e=>e.config.primary);return f?u=f.push:l.length>0&&(u=l[0].push),{collector:n,elb:u}}function Z(e){if(null===e||"object"!=typeof e)return e;if(Array.isArray(e))return e.map(Z);const n={};for(const[t,o]of Object.entries(e))n[t]="function"==typeof o?o:Z(o);return n}function ee(e){const n=[],{simulation:t,...o}=e,s=Z(o);for(const e of t){const t=e.startsWith("call:")?e.slice(5):e,o=t.split(".");let r=s;for(let e=0;e<o.length-1&&null!=r[o[e]];e++)r=r[o[e]];const i=o[o.length-1];if(null==r||!(i in r))continue;const a=r[i];"function"==typeof a&&(r[i]=function(...e){return n.push({fn:t,args:e,ts:Date.now()}),a.apply(this,e)})}return{wrappedEnv:s,calls:n}}//# sourceMappingURL=index.js.map
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var e={},n={Action:"action",Actions:"actions",Config:"config",Consent:"consent",Context:"context",Custom:"custom",Destination:"destination",Elb:"elb",Globals:"globals",Hook:"hook",Init:"init",Link:"link",On:"on",Prefix:"data-elb",Ready:"ready",Run:"run",Session:"session",Shutdown:"shutdown",User:"user",Walker:"walker"},t={Commands:n,Utils:{Storage:{Cookie:"cookie",Local:"local",Session:"session"}}};import{assign as o}from"@walkeros/core";function s(e,n){const t={};return Object.entries(n).forEach(([e,n])=>{t[e]=!!n}),e.consent=o(e.consent,t),{update:t}}import{assign as r,createLogger as i}from"@walkeros/core";import{assign as a,buildCacheContext as c,clone as u,compileCache as f,checkCache as l,storeCache as d,compileNext as g,createIngest as m,debounce as p,getId as h,getGrantedConsent as y,isDefined as v,isFunction as b,isObject as k,isRouteArray as w,processEventMapping as C,resolveNext as O,tryCatchAsync as j,useHooks as x}from"@walkeros/core";import{isArray as A}from"@walkeros/core";import{tryCatch as q,tryCatchAsync as S}from"@walkeros/core";import{createIngest as $,getMappingValue as D,tryCatchAsync as _,compileNext as I,resolveNext as P,isRouteArray as E,compileCache as M,checkCache as R,storeCache as H,applyUpdate as T,buildCacheContext as G}from"@walkeros/core";import{createIngest as N,isObject as U,tryCatchAsync as B,useHooks as F,compileNext as W,resolveNext as z,isRouteArray as L,compileCache as V,checkCache as J,storeCache as K,buildCacheContext as Q}from"@walkeros/core";function X(e,n){return e.storeId&&n.stores[e.storeId]?n.stores[e.storeId]:n.stores.__cache}function Y(e){const n={};for(const[t,o]of Object.entries(e)){const e=o.config?.next;e&&!L(e)?n[t]={next:e}:n[t]={}}return n}function Z(e,n){const t=e.config||{},o=e[n];return void 0!==o?{config:{...t,[n]:o},chainValue:o}:{config:t,chainValue:void 0}}function ee(e,n={}){if(!e)return[];if(Array.isArray(e))return e;const t=[],o=new Set;let s=e;for(;s&&n[s]&&!o.has(s);){o.add(s),t.push(s);const e=n[s].next;if(Array.isArray(e)){t.push(...e);break}s=e}return t}async function ne(e,n,t){if(n.init&&!n.config.init){const o=n.type||"unknown",s=e.logger.scope(`transformer:${o}`),r={collector:e,logger:s,id:t,ingest:N(t),config:n.config,env:se(n.config.env)};s.debug("init");const i=await F(n.init,"TransformerInit",e.hooks,e.logger)(r);if(!1===i)return!1;n.config={...i||n.config,env:i?.env||n.config.env,init:!0},s.debug("init done")}return!0}async function te(e,n,t,o,s,r){const i=n.type||"unknown",a=e.logger.scope(`transformer:${i}`),c={collector:e,logger:a,id:t,ingest:s,config:n.config,env:{...se(n.config.env),...r?{respond:r}:{}}};a.debug("push",{event:o.name});const u=await F(n.push,"TransformerPush",e.hooks,e.logger)(o,c);return a.debug("push done"),u}async function oe(e,n,t,o,s,r,i){i&&s?._meta&&(s._meta.chainPath=i);let a=o,c=r;for(const o of t){const r=n[o];if(!r){e.logger.warn(`Transformer not found: ${o}`);continue}if(s&&s._meta&&s._meta.path.length>256)return e.logger.error(`Max path length exceeded at ${o}`),{event:null,respond:c};s&&s._meta&&(s._meta.hops++,s._meta.path.push(o));if(!await B(ne)(e,r,o))return e.logger.error(`Transformer init failed: ${o}`),{event:null,respond:c};if(i&&void 0!==r.config?.chainMocks?.[i]){const n=r.config.chainMocks[i];e.logger.scope(`transformer:${r.type||"unknown"}`).debug("chainMock",{chain:i}),a=n;continue}if(void 0!==r.config?.mock){e.logger.scope(`transformer:${r.type||"unknown"}`).debug("mock"),a=r.config.mock;continue}if(r.config?.disabled)continue;const u=r.config?.cache,f=u?V(u):void 0,l=f?X(f,e):void 0;let d;if(f&&l){const e=Q(s,a),n=J(f,l,e,`t:${o}`);if("HIT"===n?.status&&n.value){if(a=n.value,f.full)return{event:a,respond:c};continue}"MISS"===n?.status&&(d={key:n.key,ttl:n.rule.ttl})}const g=r.config.before;if(g){const t=ee("string"==typeof g||Array.isArray(g)&&!L(g)?g:z(W(g),Q(s,a))||void 0,Y(n));if(t.length>0){const o=await oe(e,n,t,a,s,c,i);if(null===o.event)return{event:null,respond:o.respond??c};o.respond&&(c=o.respond),a=Array.isArray(o.event)?o.event[0]:o.event}}const m=await B(te,n=>(e.logger.scope(`transformer:${r.type||"unknown"}`).error("Push failed",{error:n}),!1))(e,r,o,a,s,c);if(!1===m)return{event:null,respond:c};if(Array.isArray(m)){const r=t.slice(t.indexOf(o)+1),u=await Promise.all(m.map(async t=>{const o=t.event||a,u=s?{...s,_meta:{...s._meta,path:[...s._meta.path]}}:N("unknown");if(t.next){let s=t.next;if(L(t.next)){const e=W(t.next);s=z(e,Q(u,o))}if(s){const t=ee(s,Y(n));if(t.length>0)return oe(e,n,t,o,u,c,i)}return{event:o,respond:c}}return r.length>0?oe(e,n,r,o,u,c,i):{event:o,respond:c}}));let f=c;const l=[];for(const e of u.flat())if(null!==e)if(e&&"object"==typeof e&&"event"in e){const n=e;if(n.respond&&(f=n.respond),null===n.event)continue;Array.isArray(n.event)?l.push(...n.event):l.push(n.event)}else l.push(e);return 0===l.length?{event:null,respond:f}:1===l.length?{event:l[0],respond:f}:{event:l,respond:f}}if(m&&"object"==typeof m){const{event:t,respond:o,next:r}=m;if(o&&(c=o),r){let o=r;if(L(r)){const e=W(r);if(o=z(e,Q(s,a)),!o){t&&(a=t);continue}}const u=ee(o,Y(n));return u.length>0?oe(e,n,u,t||a,s,c,i):(e.logger.warn(`Branch target not found: ${JSON.stringify(r)}`),{event:null,respond:c})}t&&(a=t)}if(d&&l&&K(l,d.key,a,d.ttl),(!m||"object"==typeof m&&!m.next)&&r.config.next&&L(r.config.next)){const t=r.config.next,o=W(t),u=z(o,Q(s,a));if(u){const t=ee(u,Y(n));if(t.length>0)return oe(e,n,t,a,s,c,i)}return{event:a,respond:c}}}return{event:a,respond:c}}function se(e){return e&&U(e)?e:{}}async function re(e,n,t){const{code:o,config:s={},env:r={},primary:i,next:a,before:c,cache:u}=t;let f,l=$(n);const d=u?M({...u,full:u.full??!0}):void 0,g=I(a),m=!(Array.isArray(a)&&E(a))&&g?ee(P(g),Y(e.transformers)):void 0,p=I(c),h=!(Array.isArray(c)&&E(c))&&p?ee(P(p),Y(e.transformers)):void 0,y=e.logger.scope("source").scope(n),v={push:async(t,o={})=>{let r,i=t;const a=h??(p?ee(P(p,G(l)),Y(e.transformers)):[]);if(a.length>0&&e.transformers&&Object.keys(e.transformers).length>0){const t=await oe(e,e.transformers,a,i,l,f,`source.${n}.before`);if(null===t.event)return{ok:!0};t.respond&&(f=t.respond),i=Array.isArray(t.event)?t.event[0]:t.event}if(d){const t=X(d,e);if(t){const e=G(l),o=R(d,t,e,`s:${n}`);if(o){if("HIT"===o.status&&void 0!==o.value&&d.full){let n=o.value;return o.rule.update&&(n=await T(n,o.rule.update,{...e,cache:{status:"HIT"}})),f?.(n),{ok:!0}}if("MISS"===o.status&&d.full&&f){const n=f,s=o.rule.update,i={...e,cache:{status:"MISS"}},a=o.key,c=o.rule.ttl;f=e=>{H(t,a,e,c),s?r=(async()=>{const t=await T(e,s,i);n(t)})():n(e)}}"MISS"!==o.status||d.full||H(t,o.key,!0,o.rule.ttl)}}}const c=m??(g?ee(P(g,G(l)),Y(e.transformers)):[]),u=await e.push(i,{...o,id:n,ingest:l,respond:f,mapping:s,preChain:c});return r&&await r,u},command:e.command,sources:e.sources,elb:e.sources.elb.push,logger:y,...r},b={collector:e,logger:y,id:n,config:s,env:v,setIngest:async t=>{if(!s.ingest)return void(l=$(n));const o=await D(t,s.ingest,{collector:e}),r=$(n);l={...r,...o,_meta:r._meta}},setRespond:e=>{f=e}},k=await _(o)(b);if(!k)return;const w=k.type||"unknown",C=e.logger.scope(w).scope(n);return v.logger=C,i&&(k.config={...k.config,primary:i}),k}async function ie(e,n={}){const t={};for(const[o,s]of Object.entries(n)){const{config:n={}}=s;if(n.require&&n.require.length>0){e.pending.sources[o]=s;continue}const r=await re(e,o,s);r&&(t[o]=r)}return t}async function ae(e,n,t){const o=e.on,s=o[n]||[],r=A(t)?t:[t];r.forEach(e=>{s.push(e)}),o[n]=s,ue(e,n,r)}function ce(e,n,t,o,s){if(!n.on)return;const r=n.type||"unknown",i=e.logger.scope(r).scope("on").scope(o),a={collector:e,logger:i,id:t,config:n.config,data:s,env:be(n.env,n.config.env)};q(n.on)(o,a)}function ue(e,n,o,s){let r;switch(n){case t.Commands.Consent:r=s||e.consent;break;case t.Commands.Session:r=e.session;break;case t.Commands.User:r=s||e.user;break;case t.Commands.Custom:r=s||e.custom;break;case t.Commands.Globals:r=s||e.globals;break;case t.Commands.Config:r=s||e.config;break;case t.Commands.Ready:case t.Commands.Run:default:r=void 0}if(o.length)switch(n){case t.Commands.Consent:!function(e,n,t){const o=t||e.consent;n.forEach(n=>{Object.keys(o).filter(e=>e in n).forEach(t=>{q(n[t])(e,o)})})}(e,o,s);break;case t.Commands.Ready:case t.Commands.Run:!function(e,n){e.allowed&&n.forEach(n=>{q(n)(e)})}(e,o);break;case t.Commands.Session:!function(e,n){if(!e.session)return;n.forEach(n=>{q(n)(e,e.session)})}(e,o);break;default:o.forEach(n=>{"function"==typeof n&&q(n)(e,r)})}}async function fe(e,n,o,s){let r,i=o||[];switch(o||(i=e.on[n]||[]),n){case t.Commands.Consent:r=s||e.consent;break;case t.Commands.Session:r=e.session;break;case t.Commands.User:r=s||e.user;break;case t.Commands.Custom:r=s||e.custom;break;case t.Commands.Globals:r=s||e.globals;break;case t.Commands.Config:r=s||e.config;break;case t.Commands.Ready:case t.Commands.Run:default:r=void 0}let a=!1;for(const t of Object.values(e.sources))if(t.on){!1===await S(t.on)(n,r)&&(a=!0)}return Object.entries(e.destinations).forEach(([t,o])=>{if(o.on){if(!o.config.init)return o.queueOn=o.queueOn||[],void o.queueOn.push({type:n,data:r});ce(e,o,t,n,r)}}),(Object.keys(e.pending.sources).length>0||Object.keys(e.pending.destinations).length>0)&&await async function(e,n){for(const[t,o]of Object.entries(e.pending.sources)){if(!e.pending.sources[t]||e.sources[t])continue;const s=o.config?.require;if(!s)continue;const r=s.indexOf(n);if(-1===r)continue;if(s.splice(r,1),s.length>0)continue;delete e.pending.sources[t];const i=await re(e,t,o);i&&(e.sources[t]=i)}for(const[t,o]of Object.entries(e.pending.destinations)){if(!e.pending.destinations[t]||e.destinations[t])continue;const s=o.config?.require;if(!s)continue;const r=s.indexOf(n);if(-1===r)continue;if(s.splice(r,1),s.length>0)continue;delete e.pending.destinations[t];const i=ye(o);!1!==i.config.queue&&(i.queuePush=[...e.queue]),e.destinations[t]=i}}(e,n),ue(e,n,i,s),!a}function le(e,n,t,o){if(!e)return[];if(n){const e=O(n,c(o));return e?ee(e,Y(t)):[]}return ee(e,Y(t))}async function de(e,n,t){const{code:o,config:s={},env:r={},before:i,next:a,cache:c}=n;if(!b(o.push))return he({ok:!1,failed:{invalid:{type:"invalid",error:"Destination code must have a push method"}}});const u=t||s||{init:!1};let f=i?{...u,before:i}:{...u};a&&(f={...f,next:a}),c&&(f={...f,cache:c});const l={...o,config:f,env:be(o.env,r)};let d=l.config.id;if(!d)do{d=h(5,"abcdefghijklmnopqrstuvwxyz")}while(e.destinations[d]);return e.destinations[d]=l,!1!==l.config.queue&&(l.queuePush=[...e.queue]),ge(e,void 0,{},{[d]:l})}async function ge(e,n,t={},o){const{allowed:s,consent:r,globals:i,user:p}=e;if(!s)return he({ok:!1});n&&(e.queue.push(n),e.status.in++),o||(o=e.destinations);const h=await Promise.all(Object.entries(o||{}).map(async([o,s])=>{if(s.config.disabled)return{id:o,destination:s,skipped:!0};let h=(s.queuePush||[]).map(e=>({...e,consent:r}));s.queuePush=[],n&&h.push(u(n));const v=t.ingest?{...t.ingest,_meta:{...t.ingest._meta,path:[...t.ingest._meta.path]}}:m("unknown");if(!h.length&&!s.queueOn?.length)return{id:o,destination:s,skipped:!0};if(!h.length&&s.queueOn?.length){const n=await j(me)(e,s,o);return{id:o,destination:s,skipped:!n}}const b=[],k=h.filter(e=>{const n=y(s.config.consent,r,e.consent);return!n||(e.consent=n,b.push(e),!1)});if(s.queuePush.push(...k),!b.length)return{id:o,destination:s,queue:h};if(!await j(me)(e,s,o))return{id:o,destination:s,queue:h};let C,O;s.dlq||(s.dlq=[]);const x=s.config.before,A=le(x,x&&w(x)?g(x):void 0,e.transformers,v),q=s.config.next,S=q&&w(q)?g(q):void 0,$=s.config?.cache,D=$?f($):void 0,_=D?X(D,e):void 0;let I=0;return await Promise.all(b.map(async n=>{let r;if(n.globals=a(i,n.globals),n.user=a(p,n.user),D?.full&&_){const e=c(v,n),t=l(D,_,e,`d:${o}`);if("HIT"===t?.status)return n;"MISS"===t?.status&&(r={key:t.key,ttl:t.rule.ttl})}let u=n,f=t.respond;if(A.length>0&&e.transformers&&Object.keys(e.transformers).length>0){const s=await oe(e,e.transformers,A,n,v,t.respond,`destination.${o}.before`);if(null===s.event)return n;s.respond&&(f=s.respond),u=Array.isArray(s.event)?s.event[0]:s.event}if(D&&!D.full&&_){const e=c(v,u),t=l(D,_,e,`d:${o}`);if("HIT"===t?.status)return n;"MISS"===t?.status&&(r={key:t.key,ttl:t.rule.ttl})}const g=Date.now();let m=!1;const h=await j(pe,n=>{const t=s.type||"unknown";e.logger.scope(t).error("Push failed",{error:n,event:u.name}),C=n,m=!0,s.dlq.push([u,n])})(e,s,o,u,v,f);if(I+=Date.now()-g,r&&_&&void 0===s.config.mock&&d(_,r.key,h??!0,r.ttl),void 0!==h&&(O=h),!m&&q){void 0!==h&&(v._response=h);const n=le(q,S,e.transformers,v);if(n.length>0&&e.transformers&&Object.keys(e.transformers).length>0){const t=await oe(e,e.transformers,n,u,v,f,`destination.${o}.next`);t.respond&&(f=t.respond)}}return n})),{id:o,destination:s,error:C,response:O,totalDuration:I}})),v={},b={},k={};for(const n of h){if(n.skipped)continue;const t={type:n.destination.type||"unknown",data:n.response};e.status.destinations[n.id]||(e.status.destinations[n.id]={count:0,failed:0,duration:0});const o=e.status.destinations[n.id],s=Date.now();n.error?(t.error=n.error,k[n.id]=t,o.failed++,o.lastAt=s,o.duration+=n.totalDuration||0,e.status.failed++):n.queue&&n.queue.length?b[n.id]=t:(v[n.id]=t,o.count++,o.lastAt=s,o.duration+=n.totalDuration||0,e.status.out++)}return he({event:n,...Object.keys(v).length&&{done:v},...Object.keys(b).length&&{queued:b},...Object.keys(k).length&&{failed:k}})}async function me(e,n,t){if(n.init&&!n.config.init){const o=n.type||"unknown",s=e.logger.scope(o),r={collector:e,logger:s,id:t,config:n.config,env:be(n.env,n.config.env)};s.debug("init");const i=await x(n.init,"DestinationInit",e.hooks,e.logger)(r);if(!1===i)return i;if(n.config={...i||n.config,init:!0},n.queueOn?.length){const o=n.queueOn;n.queueOn=[];for(const{type:s,data:r}of o)ce(e,n,t,s,r)}s.debug("init done")}return!0}async function pe(e,n,t,o,s,r){const{config:i}=n,a=await C(o,i,e);if(a.ignore)return!1;const c=n.type||"unknown",u=e.logger.scope(c),f={collector:e,logger:u,id:t,config:i,data:a.data,rule:a.mapping,ingest:s,env:{...be(n.env,i.env),...r?{respond:r}:{}}};if(void 0!==i.mock)return u.debug("mock",{event:a.event.name}),i.mock;const l=a.mapping,d=a.mappingKey||"* *";if(!l?.batch||!n.pushBatch||void 0!==i.mock){u.debug("push",{event:a.event.name});const t=await x(n.push,"DestinationPush",e.hooks,e.logger)(a.event,f);return u.debug("push done"),t}{if(n.batches=n.batches||{},!n.batches[d]){const o={key:d,events:[],data:[]};n.batches[d]={batched:o,batchFn:p(()=>{const o=n.batches[d].batched,a={collector:e,logger:u,id:t,config:i,data:void 0,rule:l,ingest:s,env:{...be(n.env,i.env),...r?{respond:r}:{}}};u.debug("push batch",{events:o.events.length}),x(n.pushBatch,"DestinationPushBatch",e.hooks,e.logger)(o,a),u.debug("push batch done"),o.events=[],o.data=[]},l.batch)}}const o=n.batches[d];o.batched.events.push(a.event),v(a.data)&&o.batched.data.push(a.data),o.batchFn()}return!0}function he(e){return{ok:!e?.failed,...e}}function ye(e){const{code:n,config:t={},env:o={},cache:s}=e,{config:r}=Z(e,"before"),{config:i}=Z({...e,config:r},"next"),a={...n.config,...t,...i};s&&(a.cache=s);const c=be(n.env,o);return{...n,config:a,env:c}}async function ve(e,n={}){const t={};for(const[o,s]of Object.entries(n))s.config?.require?.length?e.pending.destinations[o]=s:t[o]=ye(s);return t}function be(e,n){return e||n?n?e&&k(e)&&k(n)?{...e,...n}:n:e:{}}import{assign as ke,getId as we,isFunction as Ce,isString as Oe}from"@walkeros/core";import{isObject as je}from"@walkeros/core";async function xe(e,n,t){const o=Object.entries(e).map(async([e,o])=>{const s=o.destroy;if(!s)return;const r=o.type||"unknown",i=t.scope(r),a={id:e,config:o.config,env:o.env??{},logger:i};try{await Promise.race([s(a),new Promise((t,o)=>setTimeout(()=>o(new Error(`${n} '${e}' destroy timed out`)),5e3))])}catch(t){i.error(`${n} '${e}' destroy failed: ${t}`)}});await Promise.allSettled(o)}async function Ae(e,n,o,r){let i,a,c=!1;switch(n){case t.Commands.Config:je(o)&&(ke(e.config,o,{shallow:!1}),a=o,c=!0);break;case t.Commands.Consent:if(je(o)){const{update:n}=s(e,o);a=n,c=!0}break;case t.Commands.Custom:je(o)&&(e.custom=ke(e.custom,o),a=o,c=!0);break;case t.Commands.Destination:je(o)&&("code"in o&&je(o.code)?i=await de(e,o,r):Ce(o.push)&&(i=await de(e,{code:o},r)));break;case t.Commands.Globals:je(o)&&(e.globals=ke(e.globals,o),a=o,c=!0);break;case t.Commands.On:Oe(o)&&await ae(e,o,r);break;case t.Commands.Ready:c=!0;break;case t.Commands.Run:i=await Se(e,o),c=!0;break;case t.Commands.Session:c=!0;break;case t.Commands.Shutdown:await async function(e){const n=e.logger;await xe(e.sources,"source",n),await xe(e.destinations,"destination",n),await xe(e.transformers,"transformer",n),await xe(e.stores,"store",n)}(e);break;case t.Commands.User:je(o)&&(ke(e.user,o,{shallow:!1}),a=o,c=!0)}return c&&(await fe(e,n,void 0,a),i=await ge(e)),i||he({ok:!0})}function qe(e,n){if(!n.name)throw new Error("Event name is required");const[t,o]=n.name.split(" ");if(!t||!o)throw new Error("Event name is invalid");++e.count;const{timestamp:s=Date.now(),group:r=e.group,count:i=e.count}=n,{name:a=`${t} ${o}`,data:c={},context:u={},globals:f=e.globals,custom:l={},user:d=e.user,nested:g=[],consent:m=e.consent,id:p=`${s}-${r}-${i}`,trigger:h="",entity:y=t,action:v=o,timing:b=0,version:k={source:e.version,tagging:e.config.tagging||0},source:w={type:"collector",id:"",previous_id:""}}=n;return{name:a,data:c,context:u,globals:f,custom:l,user:d,nested:g,consent:m,id:p,trigger:h,entity:y,action:v,timestamp:s,timing:b,group:r,count:i,version:k,source:w}}async function Se(e,n){e.allowed=!0,e.count=0,e.group=we(),e.timing=Date.now(),n&&(n.consent&&(e.consent=ke(e.consent,n.consent)),n.user&&(e.user=ke(e.user,n.user)),n.globals&&(e.globals=ke(e.config.globalsStatic||{},n.globals)),n.custom&&(e.custom=ke(e.custom,n.custom))),Object.values(e.destinations).forEach(e=>{e.queuePush=[]}),e.queue=[],e.round++;return await ge(e)}import{createIngest as $e,getGrantedConsent as De,processEventMapping as _e,tryCatchAsync as Ie,useHooks as Pe}from"@walkeros/core";function Ee(e,n){return Pe(async(t,o={})=>await Ie(async()=>{const s=Date.now(),{id:r,ingest:i,respond:a,mapping:c,preChain:u,include:f,exclude:l}=o;let d=a,g=t;const m=f||l?function(e,n,t){let o=e;return n&&(o=Object.fromEntries(Object.entries(o).filter(([e])=>n.includes(e)))),t&&(o=Object.fromEntries(Object.entries(o).filter(([e])=>!t.includes(e)))),o}(e.destinations,f,l):void 0,p=i??$e(r||"unknown");if(c){const n=await _e(g,c,e);if(n.ignore)return he({ok:!0});if(c.consent){if(!De(c.consent,e.consent,n.event.consent))return he({ok:!0})}g=n.event}if(u?.length&&e.transformers&&Object.keys(e.transformers).length>0){const t=await oe(e,e.transformers,u,g,p,d,r?`source.${r}.next`:void 0);if(null===t.event)return he({ok:!0});if(t.respond&&(d=t.respond),Array.isArray(t.event)){const o=await Promise.all(t.event.map(async t=>{const o=n(t),s=qe(e,o);return ge(e,s,{id:r,ingest:p,respond:d},m)}));if(r){e.status.sources[r]||(e.status.sources[r]={count:0,duration:0});const n=e.status.sources[r];n.count+=t.event.length,n.lastAt=Date.now(),n.duration+=Date.now()-s}return o[0]??he({ok:!0})}g=t.event}const h=n(g),y=qe(e,h),v=await ge(e,y,{id:r,ingest:p,respond:d},m);if(r){e.status.sources[r]||(e.status.sources[r]={count:0,duration:0});const n=e.status.sources[r];n.count++,n.lastAt=Date.now(),n.duration+=Date.now()-s}return v},()=>he({ok:!1}))(),"Push",e.hooks,e.logger)}import{useHooks as Me,tryCatchAsync as Re}from"@walkeros/core";import{useHooks as He}from"@walkeros/core";async function Te(e){const n=r({globalsStatic:{},sessionStatic:{},tagging:0,run:!0},e,{merge:!1,extend:!1}),t={level:e.logger?.level,handler:e.logger?.handler},o=i(t),s={...n.globalsStatic,...e.globals},a={allowed:!1,config:n,consent:e.consent||{},count:0,custom:e.custom||{},destinations:{},transformers:{},stores:{},globals:s,group:"",hooks:e.hooks||{},logger:o,on:{},queue:[],round:0,session:void 0,status:{startedAt:Date.now(),in:0,out:0,failed:0,sources:{},destinations:{}},timing:Date.now(),user:e.user||{},version:"3.4.1-next-1776790594143",sources:{},pending:{sources:{},destinations:{}},push:void 0,command:void 0};a.push=Ee(a,e=>({timing:Math.round((Date.now()-a.timing)/10)/100,source:{type:"collector",id:"",previous_id:""},...e})),a.command=function(e,n){return Me(async(t,o,s)=>await Re(async()=>await n(e,t,o,s),()=>he({ok:!1}))(),"Command",e.hooks,e.logger)}(a,Ae);const c=e.stores||{};if(a.stores=await async function(e,n={}){const t={};for(const[o,s]of Object.entries(n)){const{code:n,config:r={},env:i={}}=s,a=e.logger.scope("store").scope(o),c={collector:e,logger:a,id:o,config:r,env:i},u=await n(c),f=u.get,l=u.set,d=u.delete;u.get=He(f,"StoreGet",e.hooks,e.logger),u.set=He(l,"StoreSet",e.hooks,e.logger),u.delete=He(d,"StoreDelete",e.hooks,e.logger),t[o]=u}return t}(a,c),function(e,n,t){const o=new Map;for(const[t,s]of Object.entries(e))n[t]&&o.set(s,n[t]);if(0!==o.size)for(const e of[t.transformers,t.destinations,t.sources])if(e)for(const n of Object.values(e))s(n.env);function s(e){if(e)for(const[n,t]of Object.entries(e))if("object"==typeof t&&null!==t){const s=o.get(t);s&&(e[n]=s)}}}(c,a.stores,e),!a.stores.__cache){const e=new Map;a.stores.__cache={type:"memory",config:{},get:n=>{const t=e.get(n);if(t){if(!(t.expires&&Date.now()>t.expires))return t.value;e.delete(n)}},set:(n,t,o)=>{e.set(n,{value:t,expires:o?Date.now()+o:void 0})},delete:n=>{e.delete(n)}}}return a.destinations=await ve(a,e.destinations||{}),a.transformers=await async function(e,n={}){const t={};for(const[o,s]of Object.entries(n)){const{code:n,env:r={}}=s,{config:i}=Z(s,"before"),{config:a}=Z({...s,config:i},"next"),c=Object.keys(r).length>0?{...a,env:r}:a,{cache:u}=s,f=u?{...c,cache:u}:c,l=e.logger.scope("transformer").scope(o),d={collector:e,logger:l,id:o,ingest:N(o),config:f,env:r},g=await n(d);t[o]=g}return t}(a,e.transformers||{}),a}async function Ge(e){e=e||{};const n=await Te(e),t=(o=n,{type:"elb",config:{},push:async(e,n,t,s,r,i)=>{if("string"==typeof e&&e.startsWith("walker ")){const s=e.replace("walker ","");return o.command(s,n,t)}let a;if("string"==typeof e)a={name:e},n&&"object"==typeof n&&!Array.isArray(n)&&(a.data=n);else{if(!e||"object"!=typeof e)return he({ok:!1});a=e,n&&"object"==typeof n&&!Array.isArray(n)&&(a.data={...a.data||{},...n})}return s&&"object"==typeof s&&(a.context=s),r&&Array.isArray(r)&&(a.nested=r),i&&"object"==typeof i&&(a.custom=i),o.push(a)}});var o;n.sources.elb=t;const s=await ie(n,e.sources||{});Object.assign(n.sources,s);const{consent:r,user:i,globals:a,custom:c}=e;r&&await n.command("consent",r),i&&await n.command("user",i),a&&Object.assign(n.globals,a),c&&Object.assign(n.custom,c),n.config.run&&await n.command("run");let u=t.push;const f=Object.values(n.sources).filter(e=>"elb"!==e.type),l=f.find(e=>e.config.primary);return l?u=l.push:f.length>0&&(u=f[0].push),{collector:n,elb:u}}function Ne(e){if(null===e||"object"!=typeof e)return e;if(Array.isArray(e))return e.map(Ne);const n={};for(const[t,o]of Object.entries(e))n[t]="function"==typeof o?o:Ne(o);return n}function Ue(e){const n=[],{simulation:t,...o}=e,s=Ne(o);for(const e of t){const t=e.startsWith("call:")?e.slice(5):e,o=t.split(".");let r=s;for(let e=0;e<o.length-1&&null!=r[o[e]];e++)r=r[o[e]];const i=o[o.length-1];if(null==r||!(i in r))continue;const a=r[i];"function"==typeof a&&(r[i]=function(...e){return n.push({fn:t,args:e,ts:Date.now()}),a.apply(this,e)})}return{wrappedEnv:s,calls:n}}export{e as Code,n as Commands,t as Const,de as addDestination,ce as callDestinationOn,Ae as commonHandleCommand,qe as createEvent,Ee as createPush,he as createPushResult,me as destinationInit,pe as destinationPush,Y as extractTransformerNextMap,X as getCacheStore,ve as initDestinations,re as initSource,ie as initSources,be as mergeEnvironments,ae as on,fe as onApply,s as processConsent,ge as pushToDestinations,ye as registerDestination,Se as runCollector,oe as runTransformerChain,Ge as startFlow,ne as transformerInit,te as transformerPush,ee as walkChain,Ue as wrapEnv};//# sourceMappingURL=index.mjs.map
|
|
1
|
+
var e={},n={Action:"action",Actions:"actions",Config:"config",Consent:"consent",Context:"context",Custom:"custom",Destination:"destination",Elb:"elb",Globals:"globals",Hook:"hook",Init:"init",Link:"link",On:"on",Prefix:"data-elb",Ready:"ready",Run:"run",Session:"session",Shutdown:"shutdown",User:"user",Walker:"walker"},t={Commands:n,Utils:{Storage:{Cookie:"cookie",Local:"local",Session:"session"}}};import{assign as o}from"@walkeros/core";function s(e,n){const t={};return Object.entries(n).forEach(([e,n])=>{t[e]=!!n}),e.consent=o(e.consent,t),{update:t}}import{assign as r,createLogger as i}from"@walkeros/core";import{assign as a,buildCacheContext as c,clone as u,compileCache as f,checkCache as l,storeCache as d,compileNext as g,createIngest as m,debounce as p,getId as h,getGrantedConsent as y,isDefined as v,isFunction as b,isObject as k,isRouteArray as w,processEventMapping as C,resolveNext as O,tryCatchAsync as j,useHooks as x}from"@walkeros/core";import{isArray as A}from"@walkeros/core";import{tryCatch as q,tryCatchAsync as S}from"@walkeros/core";import{createIngest as $,getMappingValue as D,tryCatchAsync as _,compileNext as I,resolveNext as P,isRouteArray as E,compileCache as M,checkCache as R,storeCache as H,applyUpdate as T,buildCacheContext as G}from"@walkeros/core";import{createIngest as N,isObject as U,tryCatchAsync as B,useHooks as F,compileNext as W,resolveNext as z,isRouteArray as L,compileCache as V,checkCache as J,storeCache as K,buildCacheContext as Q}from"@walkeros/core";function X(e,n){return e.storeId&&n.stores[e.storeId]?n.stores[e.storeId]:n.stores.__cache}function Y(e){const n={};for(const[t,o]of Object.entries(e)){const e=o.config?.next;e&&!L(e)?n[t]={next:e}:n[t]={}}return n}function Z(e,n){const t=e.config||{},o=e[n];return void 0!==o?{config:{...t,[n]:o},chainValue:o}:{config:t,chainValue:void 0}}function ee(e,n={}){if(!e)return[];if(Array.isArray(e))return e;const t=[],o=new Set;let s=e;for(;s&&n[s]&&!o.has(s);){o.add(s),t.push(s);const e=n[s].next;if(Array.isArray(e)){t.push(...e);break}s=e}return t}async function ne(e,n,t){if(n.init&&!n.config.init){const o=n.type||"unknown",s=e.logger.scope(`transformer:${o}`),r={collector:e,logger:s,id:t,ingest:N(t),config:n.config,env:se(n.config.env)};s.debug("init");const i=await F(n.init,"TransformerInit",e.hooks,e.logger)(r);if(!1===i)return!1;n.config={...i||n.config,env:i?.env||n.config.env,init:!0},s.debug("init done")}return!0}async function te(e,n,t,o,s,r){const i=n.type||"unknown",a=e.logger.scope(`transformer:${i}`),c={collector:e,logger:a,id:t,ingest:s,config:n.config,env:{...se(n.config.env),...r?{respond:r}:{}}};a.debug("push",{event:o.name});const u=await F(n.push,"TransformerPush",e.hooks,e.logger)(o,c);return a.debug("push done"),u}async function oe(e,n,t,o,s,r,i){i&&s?._meta&&(s._meta.chainPath=i);let a=o,c=r;for(const o of t){const r=n[o];if(!r){e.logger.warn(`Transformer not found: ${o}`);continue}if(s&&s._meta&&s._meta.path.length>256)return e.logger.error(`Max path length exceeded at ${o}`),{event:null,respond:c};s&&s._meta&&(s._meta.hops++,s._meta.path.push(o));if(!await B(ne)(e,r,o))return e.logger.error(`Transformer init failed: ${o}`),{event:null,respond:c};if(i&&void 0!==r.config?.chainMocks?.[i]){const n=r.config.chainMocks[i];e.logger.scope(`transformer:${r.type||"unknown"}`).debug("chainMock",{chain:i}),a=n;continue}if(void 0!==r.config?.mock){e.logger.scope(`transformer:${r.type||"unknown"}`).debug("mock"),a=r.config.mock;continue}if(r.config?.disabled)continue;const u=r.config?.cache,f=u?V(u):void 0,l=f?X(f,e):void 0;let d;if(f&&l){const e=Q(s,a),n=J(f,l,e,`t:${o}`);if("HIT"===n?.status&&n.value){if(a=n.value,f.full)return{event:a,respond:c};continue}"MISS"===n?.status&&(d={key:n.key,ttl:n.rule.ttl})}const g=r.config.before;if(g){const t=ee("string"==typeof g||Array.isArray(g)&&!L(g)?g:z(W(g),Q(s,a))||void 0,Y(n));if(t.length>0){const o=await oe(e,n,t,a,s,c,i);if(null===o.event)return{event:null,respond:o.respond??c};o.respond&&(c=o.respond),a=Array.isArray(o.event)?o.event[0]:o.event}}const m=await B(te,n=>(e.logger.scope(`transformer:${r.type||"unknown"}`).error("Push failed",{error:n}),!1))(e,r,o,a,s,c);if(!1===m)return{event:null,respond:c};if(Array.isArray(m)){const r=t.slice(t.indexOf(o)+1),u=await Promise.all(m.map(async t=>{const o=t.event||a,u=s?{...s,_meta:{...s._meta,path:[...s._meta.path]}}:N("unknown");if(t.next){let s=t.next;if(L(t.next)){const e=W(t.next);s=z(e,Q(u,o))}if(s){const t=ee(s,Y(n));if(t.length>0)return oe(e,n,t,o,u,c,i)}return{event:o,respond:c}}return r.length>0?oe(e,n,r,o,u,c,i):{event:o,respond:c}}));let f=c;const l=[];for(const e of u.flat())if(null!==e)if(e&&"object"==typeof e&&"event"in e){const n=e;if(n.respond&&(f=n.respond),null===n.event)continue;Array.isArray(n.event)?l.push(...n.event):l.push(n.event)}else l.push(e);return 0===l.length?{event:null,respond:f}:1===l.length?{event:l[0],respond:f}:{event:l,respond:f}}if(m&&"object"==typeof m){const{event:t,respond:o,next:r}=m;if(o&&(c=o),r){let o=r;if(L(r)){const e=W(r);if(o=z(e,Q(s,a)),!o){t&&(a=t);continue}}const u=ee(o,Y(n));return u.length>0?oe(e,n,u,t||a,s,c,i):(e.logger.warn(`Branch target not found: ${JSON.stringify(r)}`),{event:null,respond:c})}t&&(a=t)}if(d&&l&&K(l,d.key,a,d.ttl),(!m||"object"==typeof m&&!m.next)&&r.config.next&&L(r.config.next)){const t=r.config.next,o=W(t),u=z(o,Q(s,a));if(u){const t=ee(u,Y(n));if(t.length>0)return oe(e,n,t,a,s,c,i)}return{event:a,respond:c}}}return{event:a,respond:c}}function se(e){return e&&U(e)?e:{}}async function re(e,n,t){const{code:o,config:s={},env:r={},primary:i,next:a,before:c,cache:u}=t;let f,l=$(n);const d=u?M({...u,full:u.full??!0}):void 0,g=I(a),m=!(Array.isArray(a)&&E(a))&&g?ee(P(g),Y(e.transformers)):void 0,p=I(c),h=!(Array.isArray(c)&&E(c))&&p?ee(P(p),Y(e.transformers)):void 0,y=e.logger.scope("source").scope(n),v={push:async(t,o={})=>{let r,i=t;const a=h??(p?ee(P(p,G(l)),Y(e.transformers)):[]);if(a.length>0&&e.transformers&&Object.keys(e.transformers).length>0){const t=await oe(e,e.transformers,a,i,l,f,`source.${n}.before`);if(null===t.event)return{ok:!0};t.respond&&(f=t.respond),i=Array.isArray(t.event)?t.event[0]:t.event}if(d){const t=X(d,e);if(t){const e=G(l),o=R(d,t,e,`s:${n}`);if(o){if("HIT"===o.status&&void 0!==o.value&&d.full){let n=o.value;return o.rule.update&&(n=await T(n,o.rule.update,{...e,cache:{status:"HIT"}})),f?.(n),{ok:!0}}if("MISS"===o.status&&d.full&&f){const n=f,s=o.rule.update,i={...e,cache:{status:"MISS"}},a=o.key,c=o.rule.ttl;f=e=>{H(t,a,e,c),s?r=(async()=>{const t=await T(e,s,i);n(t)})():n(e)}}"MISS"!==o.status||d.full||H(t,o.key,!0,o.rule.ttl)}}}const c=m??(g?ee(P(g,G(l)),Y(e.transformers)):[]),u=await e.push(i,{...o,id:n,ingest:l,respond:f,mapping:s,preChain:c});return r&&await r,u},command:e.command,sources:e.sources,elb:e.sources.elb.push,logger:y,...r},b={collector:e,logger:y,id:n,config:s,env:v,setIngest:async t=>{if(!s.ingest)return void(l=$(n));const o=await D(t,s.ingest,{collector:e}),r=$(n);l={...r,...o,_meta:r._meta}},setRespond:e=>{f=e}},k=await _(o)(b);if(!k)return;const w=k.type||"unknown",C=e.logger.scope(w).scope(n);return v.logger=C,i&&(k.config={...k.config,primary:i}),k}async function ie(e,n={}){const t={};for(const[o,s]of Object.entries(n)){const{config:n={}}=s;if(n.require&&n.require.length>0){e.pending.sources[o]=s;continue}const r=await re(e,o,s);r&&(t[o]=r)}return t}async function ae(e,n,t){const o=e.on,s=o[n]||[],r=A(t)?t:[t];r.forEach(e=>{s.push(e)}),o[n]=s,ue(e,n,r)}function ce(e,n,t,o,s){if(!n.on)return;const r=n.type||"unknown",i=e.logger.scope(r).scope("on").scope(o),a={collector:e,logger:i,id:t,config:n.config,data:s,env:be(n.env,n.config.env)};q(n.on)(o,a)}function ue(e,n,o,s){let r;switch(n){case t.Commands.Consent:r=s||e.consent;break;case t.Commands.Session:r=e.session;break;case t.Commands.User:r=s||e.user;break;case t.Commands.Custom:r=s||e.custom;break;case t.Commands.Globals:r=s||e.globals;break;case t.Commands.Config:r=s||e.config;break;case t.Commands.Ready:case t.Commands.Run:default:r=void 0}if(o.length)switch(n){case t.Commands.Consent:!function(e,n,t){const o=t||e.consent;n.forEach(n=>{Object.keys(o).filter(e=>e in n).forEach(t=>{q(n[t])(e,o)})})}(e,o,s);break;case t.Commands.Ready:case t.Commands.Run:!function(e,n){e.allowed&&n.forEach(n=>{q(n)(e)})}(e,o);break;case t.Commands.Session:!function(e,n){if(!e.session)return;n.forEach(n=>{q(n)(e,e.session)})}(e,o);break;default:o.forEach(n=>{"function"==typeof n&&q(n)(e,r)})}}async function fe(e,n,o,s){let r,i=o||[];switch(o||(i=e.on[n]||[]),n){case t.Commands.Consent:r=s||e.consent;break;case t.Commands.Session:r=e.session;break;case t.Commands.User:r=s||e.user;break;case t.Commands.Custom:r=s||e.custom;break;case t.Commands.Globals:r=s||e.globals;break;case t.Commands.Config:r=s||e.config;break;case t.Commands.Ready:case t.Commands.Run:default:r=void 0}let a=!1;for(const t of Object.values(e.sources))if(t.on){!1===await S(t.on)(n,r)&&(a=!0)}return Object.entries(e.destinations).forEach(([t,o])=>{if(o.on){if(!o.config.init)return o.queueOn=o.queueOn||[],void o.queueOn.push({type:n,data:r});ce(e,o,t,n,r)}}),(Object.keys(e.pending.sources).length>0||Object.keys(e.pending.destinations).length>0)&&await async function(e,n){for(const[t,o]of Object.entries(e.pending.sources)){if(!e.pending.sources[t]||e.sources[t])continue;const s=o.config?.require;if(!s)continue;const r=s.indexOf(n);if(-1===r)continue;if(s.splice(r,1),s.length>0)continue;delete e.pending.sources[t];const i=await re(e,t,o);i&&(e.sources[t]=i)}for(const[t,o]of Object.entries(e.pending.destinations)){if(!e.pending.destinations[t]||e.destinations[t])continue;const s=o.config?.require;if(!s)continue;const r=s.indexOf(n);if(-1===r)continue;if(s.splice(r,1),s.length>0)continue;delete e.pending.destinations[t];const i=ye(o);!1!==i.config.queue&&(i.queuePush=[...e.queue]),e.destinations[t]=i}}(e,n),ue(e,n,i,s),!a}function le(e,n,t,o){if(!e)return[];if(n){const e=O(n,c(o));return e?ee(e,Y(t)):[]}return ee(e,Y(t))}async function de(e,n,t){const{code:o,config:s={},env:r={},before:i,next:a,cache:c}=n;if(!b(o.push))return he({ok:!1,failed:{invalid:{type:"invalid",error:"Destination code must have a push method"}}});const u=t||s||{init:!1};let f=i?{...u,before:i}:{...u};a&&(f={...f,next:a}),c&&(f={...f,cache:c});const l={...o,config:f,env:be(o.env,r)};let d=l.config.id;if(!d)do{d=h(5,"abcdefghijklmnopqrstuvwxyz")}while(e.destinations[d]);return e.destinations[d]=l,!1!==l.config.queue&&(l.queuePush=[...e.queue]),ge(e,void 0,{},{[d]:l})}async function ge(e,n,t={},o){const{allowed:s,consent:r,globals:i,user:p}=e;if(!s)return he({ok:!1});n&&(e.queue.push(n),e.status.in++),o||(o=e.destinations);const h=await Promise.all(Object.entries(o||{}).map(async([o,s])=>{if(s.config.disabled)return{id:o,destination:s,skipped:!0};let h=(s.queuePush||[]).map(e=>({...e,consent:r}));s.queuePush=[],n&&h.push(u(n));const v=t.ingest?{...t.ingest,_meta:{...t.ingest._meta,path:[...t.ingest._meta.path]}}:m("unknown");if(!h.length&&!s.queueOn?.length)return{id:o,destination:s,skipped:!0};if(!h.length&&s.queueOn?.length){const n=await j(me)(e,s,o);return{id:o,destination:s,skipped:!n}}const b=[],k=h.filter(e=>{const n=y(s.config.consent,r,e.consent);return!n||(e.consent=n,b.push(e),!1)});if(s.queuePush.push(...k),!b.length)return{id:o,destination:s,queue:h};if(!await j(me)(e,s,o))return{id:o,destination:s,queue:h};let C,O;s.dlq||(s.dlq=[]);const x=s.config.before,A=le(x,x&&w(x)?g(x):void 0,e.transformers,v),q=s.config.next,S=q&&w(q)?g(q):void 0,$=s.config?.cache,D=$?f($):void 0,_=D?X(D,e):void 0;let I=0;return await Promise.all(b.map(async n=>{let r;if(n.globals=a(i,n.globals),n.user=a(p,n.user),D?.full&&_){const e=c(v,n),t=l(D,_,e,`d:${o}`);if("HIT"===t?.status)return n;"MISS"===t?.status&&(r={key:t.key,ttl:t.rule.ttl})}let u=n,f=t.respond;if(A.length>0&&e.transformers&&Object.keys(e.transformers).length>0){const s=await oe(e,e.transformers,A,n,v,t.respond,`destination.${o}.before`);if(null===s.event)return n;s.respond&&(f=s.respond),u=Array.isArray(s.event)?s.event[0]:s.event}if(D&&!D.full&&_){const e=c(v,u),t=l(D,_,e,`d:${o}`);if("HIT"===t?.status)return n;"MISS"===t?.status&&(r={key:t.key,ttl:t.rule.ttl})}const g=Date.now();let m=!1;const h=await j(pe,n=>{const t=s.type||"unknown";e.logger.scope(t).error("Push failed",{error:n,event:u.name}),C=n,m=!0,s.dlq.push([u,n])})(e,s,o,u,v,f);if(I+=Date.now()-g,r&&_&&void 0===s.config.mock&&d(_,r.key,h??!0,r.ttl),void 0!==h&&(O=h),!m&&q){void 0!==h&&(v._response=h);const n=le(q,S,e.transformers,v);if(n.length>0&&e.transformers&&Object.keys(e.transformers).length>0){const t=await oe(e,e.transformers,n,u,v,f,`destination.${o}.next`);t.respond&&(f=t.respond)}}return n})),{id:o,destination:s,error:C,response:O,totalDuration:I}})),v={},b={},k={};for(const n of h){if(n.skipped)continue;const t={type:n.destination.type||"unknown",data:n.response};e.status.destinations[n.id]||(e.status.destinations[n.id]={count:0,failed:0,duration:0});const o=e.status.destinations[n.id],s=Date.now();n.error?(t.error=n.error,k[n.id]=t,o.failed++,o.lastAt=s,o.duration+=n.totalDuration||0,e.status.failed++):n.queue&&n.queue.length?b[n.id]=t:(v[n.id]=t,o.count++,o.lastAt=s,o.duration+=n.totalDuration||0,e.status.out++)}return he({event:n,...Object.keys(v).length&&{done:v},...Object.keys(b).length&&{queued:b},...Object.keys(k).length&&{failed:k}})}async function me(e,n,t){if(n.init&&!n.config.init){const o=n.type||"unknown",s=e.logger.scope(o),r={collector:e,logger:s,id:t,config:n.config,env:be(n.env,n.config.env)};s.debug("init");const i=await x(n.init,"DestinationInit",e.hooks,e.logger)(r);if(!1===i)return i;if(n.config={...i||n.config,init:!0},n.queueOn?.length){const o=n.queueOn;n.queueOn=[];for(const{type:s,data:r}of o)ce(e,n,t,s,r)}s.debug("init done")}return!0}async function pe(e,n,t,o,s,r){const{config:i}=n,a=await C(o,i,e);if(a.ignore)return!1;const c=n.type||"unknown",u=e.logger.scope(c),f={collector:e,logger:u,id:t,config:i,data:a.data,rule:a.mapping,ingest:s,env:{...be(n.env,i.env),...r?{respond:r}:{}}};if(void 0!==i.mock)return u.debug("mock",{event:a.event.name}),i.mock;const l=a.mapping,d=a.mappingKey||"* *";if(!l?.batch||!n.pushBatch||void 0!==i.mock){u.debug("push",{event:a.event.name});const t=await x(n.push,"DestinationPush",e.hooks,e.logger)(a.event,f);return u.debug("push done"),t}{if(n.batches=n.batches||{},!n.batches[d]){const o={key:d,events:[],data:[]};n.batches[d]={batched:o,batchFn:p(()=>{const o=n.batches[d].batched,a={collector:e,logger:u,id:t,config:i,data:void 0,rule:l,ingest:s,env:{...be(n.env,i.env),...r?{respond:r}:{}}};u.debug("push batch",{events:o.events.length}),x(n.pushBatch,"DestinationPushBatch",e.hooks,e.logger)(o,a),u.debug("push batch done"),o.events=[],o.data=[]},l.batch)}}const o=n.batches[d];o.batched.events.push(a.event),v(a.data)&&o.batched.data.push(a.data),o.batchFn()}return!0}function he(e){return{ok:!e?.failed,...e}}function ye(e){const{code:n,config:t={},env:o={},cache:s}=e,{config:r}=Z(e,"before"),{config:i}=Z({...e,config:r},"next"),a={...n.config,...t,...i};s&&(a.cache=s);const c=be(n.env,o);return{...n,config:a,env:c}}async function ve(e,n={}){const t={};for(const[o,s]of Object.entries(n))s.config?.require?.length?e.pending.destinations[o]=s:t[o]=ye(s);return t}function be(e,n){return e||n?n?e&&k(e)&&k(n)?{...e,...n}:n:e:{}}import{assign as ke,getId as we,isFunction as Ce,isString as Oe}from"@walkeros/core";import{isObject as je}from"@walkeros/core";async function xe(e,n,t){const o=Object.entries(e).map(async([e,o])=>{const s=o.destroy;if(!s)return;const r=o.type||"unknown",i=t.scope(r),a={id:e,config:o.config,env:o.env??{},logger:i};try{await Promise.race([s(a),new Promise((t,o)=>setTimeout(()=>o(new Error(`${n} '${e}' destroy timed out`)),5e3))])}catch(t){i.error(`${n} '${e}' destroy failed: ${t}`)}});await Promise.allSettled(o)}async function Ae(e,n,o,r){let i,a,c=!1;switch(n){case t.Commands.Config:je(o)&&(ke(e.config,o,{shallow:!1}),a=o,c=!0);break;case t.Commands.Consent:if(je(o)){const{update:n}=s(e,o);a=n,c=!0}break;case t.Commands.Custom:je(o)&&(e.custom=ke(e.custom,o),a=o,c=!0);break;case t.Commands.Destination:je(o)&&("code"in o&&je(o.code)?i=await de(e,o,r):Ce(o.push)&&(i=await de(e,{code:o},r)));break;case t.Commands.Globals:je(o)&&(e.globals=ke(e.globals,o),a=o,c=!0);break;case t.Commands.On:Oe(o)&&await ae(e,o,r);break;case t.Commands.Ready:c=!0;break;case t.Commands.Run:i=await Se(e,o),c=!0;break;case t.Commands.Session:c=!0;break;case t.Commands.Shutdown:await async function(e){const n=e.logger;await xe(e.sources,"source",n),await xe(e.destinations,"destination",n),await xe(e.transformers,"transformer",n),await xe(e.stores,"store",n)}(e);break;case t.Commands.User:je(o)&&(ke(e.user,o,{shallow:!1}),a=o,c=!0)}return c&&(await fe(e,n,void 0,a),i=await ge(e)),i||he({ok:!0})}function qe(e,n){if(!n.name)throw new Error("Event name is required");const[t,o]=n.name.split(" ");if(!t||!o)throw new Error("Event name is invalid");++e.count;const{timestamp:s=Date.now(),group:r=e.group,count:i=e.count}=n,{name:a=`${t} ${o}`,data:c={},context:u={},globals:f=e.globals,custom:l={},user:d=e.user,nested:g=[],consent:m=e.consent,id:p=`${s}-${r}-${i}`,trigger:h="",entity:y=t,action:v=o,timing:b=0,version:k={source:e.version,tagging:e.config.tagging||0},source:w={type:"collector",id:"",previous_id:""}}=n;return{name:a,data:c,context:u,globals:f,custom:l,user:d,nested:g,consent:m,id:p,trigger:h,entity:y,action:v,timestamp:s,timing:b,group:r,count:i,version:k,source:w}}async function Se(e,n){e.allowed=!0,e.count=0,e.group=we(),e.timing=Date.now(),n&&(n.consent&&(e.consent=ke(e.consent,n.consent)),n.user&&(e.user=ke(e.user,n.user)),n.globals&&(e.globals=ke(e.config.globalsStatic||{},n.globals)),n.custom&&(e.custom=ke(e.custom,n.custom))),Object.values(e.destinations).forEach(e=>{e.queuePush=[]}),e.queue=[],e.round++;return await ge(e)}import{createIngest as $e,getGrantedConsent as De,processEventMapping as _e,tryCatchAsync as Ie,useHooks as Pe}from"@walkeros/core";function Ee(e,n){return Pe(async(t,o={})=>await Ie(async()=>{const s=Date.now(),{id:r,ingest:i,respond:a,mapping:c,preChain:u,include:f,exclude:l}=o;let d=a,g=t;const m=f||l?function(e,n,t){let o=e;return n&&(o=Object.fromEntries(Object.entries(o).filter(([e])=>n.includes(e)))),t&&(o=Object.fromEntries(Object.entries(o).filter(([e])=>!t.includes(e)))),o}(e.destinations,f,l):void 0,p=i??$e(r||"unknown");if(c){const n=await _e(g,c,e);if(n.ignore)return he({ok:!0});if(c.consent){if(!De(c.consent,e.consent,n.event.consent))return he({ok:!0})}g=n.event}if(u?.length&&e.transformers&&Object.keys(e.transformers).length>0){const t=await oe(e,e.transformers,u,g,p,d,r?`source.${r}.next`:void 0);if(null===t.event)return he({ok:!0});if(t.respond&&(d=t.respond),Array.isArray(t.event)){const o=await Promise.all(t.event.map(async t=>{const o=n(t),s=qe(e,o);return ge(e,s,{id:r,ingest:p,respond:d},m)}));if(r){e.status.sources[r]||(e.status.sources[r]={count:0,duration:0});const n=e.status.sources[r];n.count+=t.event.length,n.lastAt=Date.now(),n.duration+=Date.now()-s}return o[0]??he({ok:!0})}g=t.event}const h=n(g),y=qe(e,h),v=await ge(e,y,{id:r,ingest:p,respond:d},m);if(r){e.status.sources[r]||(e.status.sources[r]={count:0,duration:0});const n=e.status.sources[r];n.count++,n.lastAt=Date.now(),n.duration+=Date.now()-s}return v},()=>he({ok:!1}))(),"Push",e.hooks,e.logger)}import{useHooks as Me,tryCatchAsync as Re}from"@walkeros/core";import{useHooks as He}from"@walkeros/core";async function Te(e){const n=r({globalsStatic:{},sessionStatic:{},tagging:0,run:!0},e,{merge:!1,extend:!1}),t={level:e.logger?.level,handler:e.logger?.handler},o=i(t),s={...n.globalsStatic,...e.globals},a={allowed:!1,config:n,consent:e.consent||{},count:0,custom:e.custom||{},destinations:{},transformers:{},stores:{},globals:s,group:"",hooks:e.hooks||{},logger:o,on:{},queue:[],round:0,session:void 0,status:{startedAt:Date.now(),in:0,out:0,failed:0,sources:{},destinations:{}},timing:Date.now(),user:e.user||{},version:"3.4.2",sources:{},pending:{sources:{},destinations:{}},push:void 0,command:void 0};a.push=Ee(a,e=>({timing:Math.round((Date.now()-a.timing)/10)/100,source:{type:"collector",id:"",previous_id:""},...e})),a.command=function(e,n){return Me(async(t,o,s)=>await Re(async()=>await n(e,t,o,s),()=>he({ok:!1}))(),"Command",e.hooks,e.logger)}(a,Ae);const c=e.stores||{};if(a.stores=await async function(e,n={}){const t={};for(const[o,s]of Object.entries(n)){const{code:n,config:r={},env:i={}}=s,a=e.logger.scope("store").scope(o),c={collector:e,logger:a,id:o,config:r,env:i},u=await n(c),f=u.get,l=u.set,d=u.delete;u.get=He(f,"StoreGet",e.hooks,e.logger),u.set=He(l,"StoreSet",e.hooks,e.logger),u.delete=He(d,"StoreDelete",e.hooks,e.logger),t[o]=u}return t}(a,c),function(e,n,t){const o=new Map;for(const[t,s]of Object.entries(e))n[t]&&o.set(s,n[t]);if(0!==o.size)for(const e of[t.transformers,t.destinations,t.sources])if(e)for(const n of Object.values(e))s(n.env);function s(e){if(e)for(const[n,t]of Object.entries(e))if("object"==typeof t&&null!==t){const s=o.get(t);s&&(e[n]=s)}}}(c,a.stores,e),!a.stores.__cache){const e=new Map;a.stores.__cache={type:"memory",config:{},get:n=>{const t=e.get(n);if(t){if(!(t.expires&&Date.now()>t.expires))return t.value;e.delete(n)}},set:(n,t,o)=>{e.set(n,{value:t,expires:o?Date.now()+o:void 0})},delete:n=>{e.delete(n)}}}return a.destinations=await ve(a,e.destinations||{}),a.transformers=await async function(e,n={}){const t={};for(const[o,s]of Object.entries(n)){const{code:n,env:r={}}=s,{config:i}=Z(s,"before"),{config:a}=Z({...s,config:i},"next"),c=Object.keys(r).length>0?{...a,env:r}:a,{cache:u}=s,f=u?{...c,cache:u}:c,l=e.logger.scope("transformer").scope(o),d={collector:e,logger:l,id:o,ingest:N(o),config:f,env:r},g=await n(d);t[o]=g}return t}(a,e.transformers||{}),a}async function Ge(e){e=e||{};const n=await Te(e),t=(o=n,{type:"elb",config:{},push:async(e,n,t,s,r,i)=>{if("string"==typeof e&&e.startsWith("walker ")){const s=e.replace("walker ","");return o.command(s,n,t)}let a;if("string"==typeof e)a={name:e},n&&"object"==typeof n&&!Array.isArray(n)&&(a.data=n);else{if(!e||"object"!=typeof e)return he({ok:!1});a=e,n&&"object"==typeof n&&!Array.isArray(n)&&(a.data={...a.data||{},...n})}return s&&"object"==typeof s&&(a.context=s),r&&Array.isArray(r)&&(a.nested=r),i&&"object"==typeof i&&(a.custom=i),o.push(a)}});var o;n.sources.elb=t;const s=await ie(n,e.sources||{});Object.assign(n.sources,s);const{consent:r,user:i,globals:a,custom:c}=e;r&&await n.command("consent",r),i&&await n.command("user",i),a&&Object.assign(n.globals,a),c&&Object.assign(n.custom,c),n.config.run&&await n.command("run");let u=t.push;const f=Object.values(n.sources).filter(e=>"elb"!==e.type),l=f.find(e=>e.config.primary);return l?u=l.push:f.length>0&&(u=f[0].push),{collector:n,elb:u}}function Ne(e){if(null===e||"object"!=typeof e)return e;if(Array.isArray(e))return e.map(Ne);const n={};for(const[t,o]of Object.entries(e))n[t]="function"==typeof o?o:Ne(o);return n}function Ue(e){const n=[],{simulation:t,...o}=e,s=Ne(o);for(const e of t){const t=e.startsWith("call:")?e.slice(5):e,o=t.split(".");let r=s;for(let e=0;e<o.length-1&&null!=r[o[e]];e++)r=r[o[e]];const i=o[o.length-1];if(null==r||!(i in r))continue;const a=r[i];"function"==typeof a&&(r[i]=function(...e){return n.push({fn:t,args:e,ts:Date.now()}),a.apply(this,e)})}return{wrappedEnv:s,calls:n}}export{e as Code,n as Commands,t as Const,de as addDestination,ce as callDestinationOn,Ae as commonHandleCommand,qe as createEvent,Ee as createPush,he as createPushResult,me as destinationInit,pe as destinationPush,Y as extractTransformerNextMap,X as getCacheStore,ve as initDestinations,re as initSource,ie as initSources,be as mergeEnvironments,ae as on,fe as onApply,s as processConsent,ge as pushToDestinations,ye as registerDestination,Se as runCollector,oe as runTransformerChain,Ge as startFlow,ne as transformerInit,te as transformerPush,ee as walkChain,Ue as wrapEnv};//# sourceMappingURL=index.mjs.map
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@walkeros/collector",
|
|
3
3
|
"description": "Unified platform-agnostic collector for walkerOS",
|
|
4
|
-
"version": "3.4.
|
|
4
|
+
"version": "3.4.2",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"update": "npx npm-check-updates -u && npm update"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@walkeros/core": "3.4.
|
|
34
|
+
"@walkeros/core": "3.4.2"
|
|
35
35
|
},
|
|
36
36
|
"repository": {
|
|
37
37
|
"url": "git+https://github.com/elbwalker/walkerOS.git",
|
|
@@ -60,6 +60,6 @@
|
|
|
60
60
|
}
|
|
61
61
|
],
|
|
62
62
|
"dependencies": {
|
|
63
|
-
"@walkeros/core": "3.4.
|
|
63
|
+
"@walkeros/core": "3.4.2"
|
|
64
64
|
}
|
|
65
65
|
}
|