@walkeros/collector 3.0.0 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var n,e=Object.defineProperty,t=Object.getOwnPropertyDescriptor,o=Object.getOwnPropertyNames,s=Object.prototype.hasOwnProperty,r={};((n,t)=>{for(var o in t)e(n,o,{get:t[o],enumerable:!0})})(r,{Code:()=>i,Commands:()=>a,Const:()=>c,addDestination:()=>P,callDestinationOn:()=>A,commonHandleCommand:()=>B,createEvent:()=>U,createPush:()=>J,createPushResult:()=>R,destinationInit:()=>x,destinationPush:()=>$,extractTransformerNextMap:()=>y,initDestinations:()=>T,initSource:()=>j,initSources:()=>q,mergeEnvironments:()=>I,on:()=>D,onApply:()=>E,processConsent:()=>f,pushToDestinations:()=>S,registerDestination:()=>H,runCollector:()=>_,simulate:()=>X,startFlow:()=>W,walkChain:()=>w,wrapEnv:()=>K}),module.exports=(n=r,((n,r,i,a)=>{if(r&&"object"==typeof r||"function"==typeof r)for(let c of o(r))s.call(n,c)||c===i||e(n,c,{get:()=>r[c],enumerable:!(a=t(r,c))||a.enumerable});return n})(e({},"__esModule",{value:!0}),n));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 f(n,e){let t=!1;const o={};return Object.entries(e).forEach(([n,e])=>{const s=!!e;o[n]=s,t=t||s}),n.consent=(0,u.assign)(n.consent,o),{update:o,runQueue:t}}var l=require("@walkeros/core"),g=require("@walkeros/core"),d=require("@walkeros/core"),p=require("@walkeros/core"),m=require("@walkeros/core"),h=require("@walkeros/core");function y(n){const e={};for(const[t,o]of Object.entries(n))o.config?.next?e[t]={next:o.config.next}:e[t]={};return e}function b(n,e){const t=n.config||{},o=n[e];return void 0!==o?{config:{...t,[e]:o},chainValue:o}:{config:t,chainValue:void 0}}function w(n,e={}){if(!n)return[];if(Array.isArray(n))return n;const t=[],o=new Set;let s=n;for(;s&&e[s]&&!o.has(s);){o.add(s),t.push(s);const n=e[s].next;if(Array.isArray(n)){t.push(...n);break}s=n}return t}async function v(n,e,t){if(e.init&&!e.config.init){const o=e.type||"unknown",s=n.logger.scope(`transformer:${o}`),r={collector:n,logger:s,id:t,config:e.config,env:C(e.config.env)};s.debug("init");const i=await(0,h.useHooks)(e.init,"TransformerInit",n.hooks)(r);if(!1===i)return!1;e.config={...i||e.config,env:i?.env||e.config.env,init:!0},s.debug("init done")}return!0}async function k(n,e,t,o,s,r){const i=e.type||"unknown",a=n.logger.scope(`transformer:${i}`),c={collector:n,logger:a,id:t,ingest:s,config:e.config,env:{...C(e.config.env),...r?{respond:r}:{}}};a.debug("push",{event:o.name});const u=await(0,h.useHooks)(e.push,"TransformerPush",n.hooks)(o,c);return a.debug("push done"),u}async function O(n,e,t,o,s,r){let i=o,a=r;for(const o of t){const t=e[o];if(!t){n.logger.warn(`Transformer not found: ${o}`);continue}if(!await(0,h.tryCatchAsync)(v)(n,t,o))return n.logger.error(`Transformer init failed: ${o}`),null;const r=await(0,h.tryCatchAsync)(k,e=>(n.logger.scope(`transformer:${t.type||"unknown"}`).error("Push failed",{error:e}),!1))(n,t,o,i,s,a);if(!1===r)return null;if(r&&"object"==typeof r){const{event:t,respond:o,next:c}=r;if(o&&(a=o),c){const o=w(c,y(e));return o.length>0?O(n,e,o,t||i,s,a):(n.logger.warn(`Branch target not found: ${JSON.stringify(c)}`),null)}t&&(i=t)}}return i}function C(n){return n&&(0,h.isObject)(n)?n:{}}async function j(n,e,t){const{code:o,config:s={},env:r={},primary:i,next:a}=t;let c,u;const f=w(a,y(n.transformers)),l=n.logger.scope("source").scope(e),g={push:(t,o={})=>n.push(t,{...o,id:e,ingest:c,respond:u,mapping:s,preChain:f}),command:n.command,sources:n.sources,elb:n.sources.elb.push,logger:l,...r},d={collector:n,logger:l,id:e,config:s,env:g,setIngest:async e=>{c=s.ingest?await(0,m.getMappingValue)(e,s.ingest,{collector:n}):void 0},setRespond:n=>{u=n}},p=await(0,m.tryCatchAsync)(o)(d);if(!p)return;const h=p.type||"unknown",b=n.logger.scope(h).scope(e);return g.logger=b,i&&(p.config={...p.config,primary:i}),p}async function q(n,e={}){const t={};for(const[o,s]of Object.entries(e)){const{config:e={}}=s;if(e.require&&e.require.length>0){n.pending.sources[o]=s;continue}const r=await j(n,o,s);r&&(t[o]=r)}return t}async function D(n,e,t){const o=n.on,s=o[e]||[],r=(0,d.isArray)(t)?t:[t];r.forEach(n=>{s.push(n)}),o[e]=s,await E(n,e,r)}function A(n,e,t,o,s){if(!e.on)return;const r=e.type||"unknown",i=n.logger.scope(r).scope("on").scope(o),a={collector:n,logger:i,id:t,config:e.config,data:s,env:I(e.env,e.config.env)};(0,p.tryCatch)(e.on)(o,a)}async function E(n,e,t,o){let s,r=t||[];switch(t||(r=n.on[e]||[]),e){case c.Commands.Consent:s=o||n.consent;break;case c.Commands.Session:s=n.session;break;case c.Commands.User:s=o||n.user;break;case c.Commands.Custom:s=o||n.custom;break;case c.Commands.Globals:s=o||n.globals;break;case c.Commands.Config:s=o||n.config;break;case c.Commands.Ready:case c.Commands.Run:default:s=void 0}let i=!1;for(const t of Object.values(n.sources))if(t.on){!1===await(0,p.tryCatchAsync)(t.on)(e,s)&&(i=!0)}if(Object.entries(n.destinations).forEach(([t,o])=>{if(o.on){if(!o.config.init)return o.queueOn=o.queueOn||[],void o.queueOn.push({type:e,data:s});A(n,o,t,e,s)}}),(Object.keys(n.pending.sources).length>0||Object.keys(n.pending.destinations).length>0)&&await async function(n,e){for(const[t,o]of Object.entries(n.pending.sources)){if(!n.pending.sources[t]||n.sources[t])continue;const s=o.config?.require;if(!s)continue;const r=s.indexOf(e);if(-1===r)continue;if(s.splice(r,1),s.length>0)continue;delete n.pending.sources[t];const i=await j(n,t,o);i&&(n.sources[t]=i)}for(const[t,o]of Object.entries(n.pending.destinations)){if(!n.pending.destinations[t]||n.destinations[t])continue;const s=o.config?.require;if(!s)continue;const r=s.indexOf(e);if(-1===r)continue;if(s.splice(r,1),s.length>0)continue;delete n.pending.destinations[t];const i=H(o);!1!==i.config.queue&&(i.queuePush=[...n.queue]),n.destinations[t]=i}}(n,e),!r.length)return!i;switch(e){case c.Commands.Consent:!function(n,e,t){const o=t||n.consent;e.forEach(e=>{Object.keys(o).filter(n=>n in e).forEach(t=>{(0,p.tryCatch)(e[t])(n,o)})})}(n,r,o);break;case c.Commands.Ready:case c.Commands.Run:!function(n,e){n.allowed&&e.forEach(e=>{(0,p.tryCatch)(e)(n)})}(n,r);break;case c.Commands.Session:!function(n,e){if(!n.session)return;e.forEach(e=>{(0,p.tryCatch)(e)(n,n.session)})}(n,r);break;default:r.forEach(e=>{"function"==typeof e&&(0,p.tryCatch)(e)(n,s)})}return!i}async function P(n,e,t){const{code:o,config:s={},env:r={},before:i}=e;if(!(0,g.isFunction)(o.push))return R({ok:!1,failed:{invalid:{type:"invalid",error:"Destination code must have a push method"}}});const a=t||s||{init:!1},c=i?{...a,before:i}:a,u={...o,config:c,env:I(o.env,r)};let f=u.config.id;if(!f)do{f=(0,g.getId)(4)}while(n.destinations[f]);return n.destinations[f]=u,!1!==u.config.queue&&(u.queuePush=[...n.queue]),S(n,void 0,{},{[f]:u})}async function S(n,e,t={},o){const{allowed:s,consent:r,globals:i,user:a}=n;if(!s)return R({ok:!1});e&&(n.queue.push(e),n.status.in++),o||(o=n.destinations);const c=await Promise.all(Object.entries(o||{}).map(async([o,s])=>{let c=(s.queuePush||[]).map(n=>({...n,consent:r}));if(s.queuePush=[],e){const n=(0,g.clone)(e);c.push(n)}if(!c.length&&!s.queueOn?.length)return{id:o,destination:s,skipped:!0};if(!c.length&&s.queueOn?.length){const e=await(0,g.tryCatchAsync)(x)(n,s,o);return{id:o,destination:s,skipped:!e}}const u=[],f=c.filter(n=>{const e=(0,g.getGrantedConsent)(s.config.consent,r,n.consent);return!e||(n.consent=e,u.push(n),!1)});if(s.queuePush.push(...f),!u.length)return{id:o,destination:s,queue:c};if(!await(0,g.tryCatchAsync)(x)(n,s,o))return{id:o,destination:s,queue:c};let l,d;s.dlq||(s.dlq=[]);const p=function(n,e){const t=n.config.before;return t?w(t,y(e)):[]}(s,n.transformers);let m=0;return await Promise.all(u.map(async e=>{e.globals=(0,g.assign)(i,e.globals),e.user=(0,g.assign)(a,e.user);let r=e;if(p.length>0&&n.transformers&&Object.keys(n.transformers).length>0){const o=await O(n,n.transformers,p,e,t.ingest,t.respond);if(null===o)return e;r=o}const c=Date.now(),u=await(0,g.tryCatchAsync)($,e=>{const t=s.type||"unknown";n.logger.scope(t).error("Push failed",{error:e,event:r.name}),l=e,s.dlq.push([r,e])})(n,s,o,r,t.ingest,t.respond);return m+=Date.now()-c,void 0!==u&&(d=u),e})),{id:o,destination:s,error:l,response:d,totalDuration:m}})),u={},f={},l={};for(const e of c){if(e.skipped)continue;const t={type:e.destination.type||"unknown",data:e.response};n.status.destinations[e.id]||(n.status.destinations[e.id]={count:0,failed:0,duration:0});const o=n.status.destinations[e.id],s=Date.now();e.error?(t.error=e.error,l[e.id]=t,o.failed++,o.lastAt=s,o.duration+=e.totalDuration||0,n.status.failed++):e.queue&&e.queue.length?f[e.id]=t:(u[e.id]=t,o.count++,o.lastAt=s,o.duration+=e.totalDuration||0,n.status.out++)}return R({event:e,...Object.keys(u).length&&{done:u},...Object.keys(f).length&&{queued:f},...Object.keys(l).length&&{failed:l}})}async function x(n,e,t){if(e.init&&!e.config.init){const o=e.type||"unknown",s=n.logger.scope(o),r={collector:n,logger:s,id:t,config:e.config,env:I(e.env,e.config.env)};s.debug("init");const i=await(0,g.useHooks)(e.init,"DestinationInit",n.hooks)(r);if(!1===i)return i;if(e.config={...i||e.config,init:!0},e.queueOn?.length){const o=e.queueOn;e.queueOn=[];for(const{type:s,data:r}of o)A(n,e,t,s,r)}s.debug("init done")}return!0}async function $(n,e,t,o,s,r){const{config:i}=e,a=await(0,g.processEventMapping)(o,i,n);if(a.ignore)return!1;const c=e.type||"unknown",u=n.logger.scope(c),f={collector:n,logger:u,id:t,config:i,data:a.data,rule:a.mapping,ingest:s,env:{...I(e.env,i.env),...r?{respond:r}:{}}},l=a.mapping,d=a.mappingKey||"* *";if(!l?.batch||!e.pushBatch){u.debug("push",{event:a.event.name});const t=await(0,g.useHooks)(e.push,"DestinationPush",n.hooks)(a.event,f);return u.debug("push done"),t}{if(e.batches=e.batches||{},!e.batches[d]){const o={key:d,events:[],data:[]};e.batches[d]={batched:o,batchFn:(0,g.debounce)(()=>{const o=e.batches[d].batched,a={collector:n,logger:u,id:t,config:i,data:void 0,rule:l,ingest:s,env:{...I(e.env,i.env),...r?{respond:r}:{}}};u.debug("push batch",{events:o.events.length}),(0,g.useHooks)(e.pushBatch,"DestinationPushBatch",n.hooks)(o,a),u.debug("push batch done"),o.events=[],o.data=[]},l.batch)}}const o=e.batches[d];o.batched.events.push(a.event),(0,g.isDefined)(a.data)&&o.batched.data.push(a.data),o.batchFn()}return!0}function R(n){return{ok:!n?.failed,...n}}function H(n){const{code:e,config:t={},env:o={}}=n,{config:s}=b(n,"before"),r={...e.config,...t,...s},i=I(e.env,o);return{...e,config:r,env:i}}async function T(n,e={}){const t={};for(const[o,s]of Object.entries(e))s.config?.require?.length?n.pending.destinations[o]=s:t[o]=H(s);return t}function I(n,e){return n||e?e?n&&(0,g.isObject)(n)&&(0,g.isObject)(e)?{...n,...e}:e:n:{}}var M=require("@walkeros/core"),F=require("@walkeros/core"),G=5e3;async function N(n,e,t){const o=Object.entries(n).map(async([n,o])=>{const s=o.destroy;if(!s)return;const r=o.type||"unknown",i=t.scope(r),a={id:n,config:o.config,env:o.env??{},logger:i};try{await Promise.race([s(a),new Promise((t,o)=>setTimeout(()=>o(new Error(`${e} '${n}' destroy timed out`)),G))])}catch(t){i.error(`${e} '${n}' destroy failed: ${t}`)}});await Promise.allSettled(o)}async function B(n,e,t,o){let s,r,i=!1,a=!1;switch(e){case c.Commands.Config:(0,F.isObject)(t)&&((0,M.assign)(n.config,t,{shallow:!1}),r=t,i=!0);break;case c.Commands.Consent:if((0,F.isObject)(t)){const{update:e,runQueue:o}=f(n,t);r=e,i=!0,a=o}break;case c.Commands.Custom:(0,F.isObject)(t)&&(n.custom=(0,M.assign)(n.custom,t),r=t,i=!0);break;case c.Commands.Destination:(0,F.isObject)(t)&&("code"in t&&(0,F.isObject)(t.code)?s=await P(n,t,o):(0,M.isFunction)(t.push)&&(s=await P(n,{code:t},o)));break;case c.Commands.Globals:(0,F.isObject)(t)&&(n.globals=(0,M.assign)(n.globals,t),r=t,i=!0);break;case c.Commands.On:(0,M.isString)(t)&&await D(n,t,o);break;case c.Commands.Ready:i=!0;break;case c.Commands.Run:s=await _(n,t),i=!0;break;case c.Commands.Session:i=!0;break;case c.Commands.Shutdown:await async function(n){const e=n.logger;await N(n.sources,"source",e),await N(n.destinations,"destination",e),await N(n.transformers,"transformer",e),await N(n.stores,"store",e)}(n);break;case c.Commands.User:(0,F.isObject)(t)&&((0,M.assign)(n.user,t,{shallow:!1}),r=t,i=!0)}return i&&await E(n,e,void 0,r),a&&(s=await S(n)),s||R({ok:!0})}function U(n,e){if(!e.name)throw new Error("Event name is required");const[t,o]=e.name.split(" ");if(!t||!o)throw new Error("Event name is invalid");++n.count;const{timestamp:s=Date.now(),group:r=n.group,count:i=n.count}=e,{name:a=`${t} ${o}`,data:c={},context:u={},globals:f=n.globals,custom:l={},user:g=n.user,nested:d=[],consent:p=n.consent,id:m=`${s}-${r}-${i}`,trigger:h="",entity:y=t,action:b=o,timing:w=0,version:v={source:n.version,tagging:n.config.tagging||0},source:k={type:"collector",id:"",previous_id:""}}=e;return{name:a,data:c,context:u,globals:f,custom:l,user:g,nested:d,consent:p,id:m,trigger:h,entity:y,action:b,timestamp:s,timing:w,group:r,count:i,version:v,source:k}}async function _(n,e){n.allowed=!0,n.count=0,n.group=(0,M.getId)(),n.timing=Date.now(),e&&(e.consent&&(n.consent=(0,M.assign)(n.consent,e.consent)),e.user&&(n.user=(0,M.assign)(n.user,e.user)),e.globals&&(n.globals=(0,M.assign)(n.config.globalsStatic||{},e.globals)),e.custom&&(n.custom=(0,M.assign)(n.custom,e.custom))),Object.values(n.destinations).forEach(n=>{n.queuePush=[]}),n.queue=[],n.round++;return await S(n)}var z=require("@walkeros/core");function J(n,e){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}=o;let f=t;const l=i?Object.freeze(i):void 0;if(c){const e=await(0,z.processEventMapping)(f,c,n);if(e.ignore)return R({ok:!0});if(c.consent){if(!(0,z.getGrantedConsent)(c.consent,n.consent,e.event.consent))return R({ok:!0})}f=e.event}if(u?.length&&n.transformers&&Object.keys(n.transformers).length>0){const e=await O(n,n.transformers,u,f,l,a);if(null===e)return R({ok:!0});f=e}const g=e(f),d=U(n,g),p=await S(n,d,{id:r,ingest:l,respond:a});if(r){n.status.sources[r]||(n.status.sources[r]={count:0,duration:0});const e=n.status.sources[r];e.count++,e.lastAt=Date.now(),e.duration+=Date.now()-s}return p},()=>R({ok:!1}))(),"Push",n.hooks)}var L=require("@walkeros/core");async function V(n){const e=(0,l.assign)({globalsStatic:{},sessionStatic:{},tagging:0,run:!0},n,{merge:!1,extend:!1}),t={level:n.logger?.level,handler:n.logger?.handler},o=(0,l.createLogger)(t),s={...e.globalsStatic,...n.globals},r={allowed:!1,config:e,consent:n.consent||{},count:0,custom:n.custom||{},destinations:{},transformers:{},stores:{},globals:s,group:"",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:n.user||{},version:"2.1.1",sources:{},pending:{sources:{},destinations:{}},push:void 0,command:void 0};r.push=J(r,n=>({timing:Math.round((Date.now()-r.timing)/10)/100,source:{type:"collector",id:"",previous_id:""},...n})),r.command=function(n,e){return(0,L.useHooks)(async(t,o,s)=>await(0,L.tryCatchAsync)(async()=>await e(n,t,o,s),()=>R({ok:!1}))(),"Command",n.hooks)}(r,B);const i=n.stores||{};return r.stores=await async function(n,e={}){const t={};for(const[o,s]of Object.entries(e)){const{code:e,config:r={},env:i={}}=s,a=n.logger.scope("store").scope(o),c={collector:n,logger:a,id:o,config:r,env:i},u=await e(c);t[o]=u}return t}(r,i),function(n,e,t){const o=new Map;for(const[t,s]of Object.entries(n))e[t]&&o.set(s,e[t]);if(0!==o.size)for(const n of[t.transformers,t.destinations,t.sources])if(n)for(const e of Object.values(n))s(e.env);function s(n){if(n)for(const[e,t]of Object.entries(n))if("object"==typeof t&&null!==t){const s=o.get(t);s&&(n[e]=s)}}}(i,r.stores,n),r.destinations=await T(r,n.destinations||{}),r.transformers=await async function(n,e={}){const t={};for(const[o,s]of Object.entries(e)){const{code:e,env:r={}}=s,{config:i}=b(s,"next"),a=Object.keys(r).length>0?{...i,env:r}:i,c=n.logger.scope("transformer").scope(o),u={collector:n,logger:c,id:o,config:a,env:r},f=await e(u);t[o]=f}return t}(r,n.transformers||{}),r}async function W(n){n=n||{};const e=await V(n),t=(o=e,{type:"elb",config:{},push:async(n,e,t,s,r,i)=>{if("string"==typeof n&&n.startsWith("walker ")){const s=n.replace("walker ","");return o.command(s,e,t)}let a;if("string"==typeof n)a={name:n},e&&"object"==typeof e&&!Array.isArray(e)&&(a.data=e);else{if(!n||"object"!=typeof n)return R({ok:!1});a=n,e&&"object"==typeof e&&!Array.isArray(e)&&(a.data={...a.data||{},...e})}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;e.sources.elb=t;const s=await q(e,n.sources||{});Object.assign(e.sources,s);const{consent:r,user:i,globals:a,custom:c}=n;r&&await e.command("consent",r),i&&await e.command("user",i),a&&Object.assign(e.globals,a),c&&Object.assign(e.custom,c),e.config.run&&await e.command("run");let u=t.push;const f=Object.values(e.sources).filter(n=>"elb"!==n.type),l=f.find(n=>n.config.primary);return l?u=l.push:f.length>0&&(u=f[0].push),{collector:e,elb:u}}function Q(n){if(null===n||"object"!=typeof n)return n;if(Array.isArray(n))return n.map(Q);const e={};for(const[t,o]of Object.entries(n))e[t]="function"==typeof o?o:Q(o);return e}function K(n){const e=[],{simulation:t,...o}=n,s=Q(o);for(const n of t){const t=n.startsWith("call:")?n.slice(5):n,o=t.split(".");let r=s;for(let n=0;n<o.length-1&&null!=r[o[n]];n++)r=r[o[n]];const i=o[o.length-1];if(null==r||!(i in r))continue;const a=r[i];"function"==typeof a&&(r[i]=function(...n){return e.push({fn:t,args:n,ts:Date.now()}),a.apply(this,n)})}return{wrappedEnv:s,calls:e}}async function X(n){const e=Date.now();try{switch(n.step){case"transformer":return await async function(n,e){const{code:t,config:o={},event:s}=n,{collector:r}=await W({transformers:{sim:{code:t,config:o}}}),i=r.transformers?.sim;if(!i)throw new Error("Transformer failed to initialize");const a=await i.push(s,{collector:r,logger:r.logger.scope("transformer").scope("sim"),id:"sim",config:i.config,env:i.config?.env||{}});let c;c=!1===a?[]:null==a?[s]:[a.event||s];return{step:"transformer",name:n.name,events:c,calls:[],duration:Date.now()-e}}(n,e);case"source":return await async function(n,e){const{code:t,config:o={},setup:s,input:r,env:i,consent:a}=n,c={functional:!0,marketing:!0,analytics:!0};let u;if(s){const n=s(r,i);"function"==typeof n&&(u=n)}const f=[],{collector:l}=await W({consent:a||c,sources:{sim:{code:t,config:o,env:i,next:"spy"}},transformers:{spy:{code:()=>({type:"spy",config:{},push:n=>(f.push(JSON.parse(JSON.stringify(n))),{event:n})})}}});u&&u();return{step:"source",name:n.name,events:f,calls:[],duration:Date.now()-e}}(n,e);case"destination":return await async function(n,e){const{code:t,config:o={},event:s,consent:r,env:i,track:a}=n,c={functional:!0,marketing:!0,analytics:!0};let u=[],f=i;if(i&&a&&a.length>0){const n=K({...i,simulation:a});f=n.wrappedEnv,u=n.calls}const l={...o};f&&(l.env=f);const{collector:g}=await W({consent:r||c,destinations:{sim:{code:t,config:l}}});return await g.push(s),{step:"destination",name:n.name,events:[],calls:u,duration:Date.now()-e}}(n,e)}}catch(t){return{step:n.step,name:n.name,events:[],calls:[],duration:Date.now()-e,error:t instanceof Error?t:new Error(String(t))}}}//# sourceMappingURL=index.js.map
|
|
1
|
+
"use strict";var n,e=Object.defineProperty,t=Object.getOwnPropertyDescriptor,o=Object.getOwnPropertyNames,s=Object.prototype.hasOwnProperty,r={};((n,t)=>{for(var o in t)e(n,o,{get:t[o],enumerable:!0})})(r,{Code:()=>i,Commands:()=>a,Const:()=>c,addDestination:()=>P,callDestinationOn:()=>A,commonHandleCommand:()=>B,createEvent:()=>U,createPush:()=>J,createPushResult:()=>R,destinationInit:()=>x,destinationPush:()=>$,extractTransformerNextMap:()=>y,initDestinations:()=>T,initSource:()=>j,initSources:()=>q,mergeEnvironments:()=>I,on:()=>D,onApply:()=>E,processConsent:()=>f,pushToDestinations:()=>S,registerDestination:()=>H,runCollector:()=>_,simulate:()=>X,startFlow:()=>W,walkChain:()=>w,wrapEnv:()=>K}),module.exports=(n=r,((n,r,i,a)=>{if(r&&"object"==typeof r||"function"==typeof r)for(let c of o(r))s.call(n,c)||c===i||e(n,c,{get:()=>r[c],enumerable:!(a=t(r,c))||a.enumerable});return n})(e({},"__esModule",{value:!0}),n));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 f(n,e){let t=!1;const o={};return Object.entries(e).forEach(([n,e])=>{const s=!!e;o[n]=s,t=t||s}),n.consent=(0,u.assign)(n.consent,o),{update:o,runQueue:t}}var l=require("@walkeros/core"),g=require("@walkeros/core"),d=require("@walkeros/core"),p=require("@walkeros/core"),m=require("@walkeros/core"),h=require("@walkeros/core");function y(n){const e={};for(const[t,o]of Object.entries(n))o.config?.next?e[t]={next:o.config.next}:e[t]={};return e}function b(n,e){const t=n.config||{},o=n[e];return void 0!==o?{config:{...t,[e]:o},chainValue:o}:{config:t,chainValue:void 0}}function w(n,e={}){if(!n)return[];if(Array.isArray(n))return n;const t=[],o=new Set;let s=n;for(;s&&e[s]&&!o.has(s);){o.add(s),t.push(s);const n=e[s].next;if(Array.isArray(n)){t.push(...n);break}s=n}return t}async function v(n,e,t){if(e.init&&!e.config.init){const o=e.type||"unknown",s=n.logger.scope(`transformer:${o}`),r={collector:n,logger:s,id:t,config:e.config,env:C(e.config.env)};s.debug("init");const i=await(0,h.useHooks)(e.init,"TransformerInit",n.hooks)(r);if(!1===i)return!1;e.config={...i||e.config,env:i?.env||e.config.env,init:!0},s.debug("init done")}return!0}async function k(n,e,t,o,s,r){const i=e.type||"unknown",a=n.logger.scope(`transformer:${i}`),c={collector:n,logger:a,id:t,ingest:s,config:e.config,env:{...C(e.config.env),...r?{respond:r}:{}}};a.debug("push",{event:o.name});const u=await(0,h.useHooks)(e.push,"TransformerPush",n.hooks)(o,c);return a.debug("push done"),u}async function O(n,e,t,o,s,r){let i=o,a=r;for(const o of t){const t=e[o];if(!t){n.logger.warn(`Transformer not found: ${o}`);continue}if(!await(0,h.tryCatchAsync)(v)(n,t,o))return n.logger.error(`Transformer init failed: ${o}`),null;const r=await(0,h.tryCatchAsync)(k,e=>(n.logger.scope(`transformer:${t.type||"unknown"}`).error("Push failed",{error:e}),!1))(n,t,o,i,s,a);if(!1===r)return null;if(r&&"object"==typeof r){const{event:t,respond:o,next:c}=r;if(o&&(a=o),c){const o=w(c,y(e));return o.length>0?O(n,e,o,t||i,s,a):(n.logger.warn(`Branch target not found: ${JSON.stringify(c)}`),null)}t&&(i=t)}}return i}function C(n){return n&&(0,h.isObject)(n)?n:{}}async function j(n,e,t){const{code:o,config:s={},env:r={},primary:i,next:a}=t;let c,u;const f=w(a,y(n.transformers)),l=n.logger.scope("source").scope(e),g={push:(t,o={})=>n.push(t,{...o,id:e,ingest:c,respond:u,mapping:s,preChain:f}),command:n.command,sources:n.sources,elb:n.sources.elb.push,logger:l,...r},d={collector:n,logger:l,id:e,config:s,env:g,setIngest:async e=>{c=s.ingest?await(0,m.getMappingValue)(e,s.ingest,{collector:n}):void 0},setRespond:n=>{u=n}},p=await(0,m.tryCatchAsync)(o)(d);if(!p)return;const h=p.type||"unknown",b=n.logger.scope(h).scope(e);return g.logger=b,i&&(p.config={...p.config,primary:i}),p}async function q(n,e={}){const t={};for(const[o,s]of Object.entries(e)){const{config:e={}}=s;if(e.require&&e.require.length>0){n.pending.sources[o]=s;continue}const r=await j(n,o,s);r&&(t[o]=r)}return t}async function D(n,e,t){const o=n.on,s=o[e]||[],r=(0,d.isArray)(t)?t:[t];r.forEach(n=>{s.push(n)}),o[e]=s,await E(n,e,r)}function A(n,e,t,o,s){if(!e.on)return;const r=e.type||"unknown",i=n.logger.scope(r).scope("on").scope(o),a={collector:n,logger:i,id:t,config:e.config,data:s,env:I(e.env,e.config.env)};(0,p.tryCatch)(e.on)(o,a)}async function E(n,e,t,o){let s,r=t||[];switch(t||(r=n.on[e]||[]),e){case c.Commands.Consent:s=o||n.consent;break;case c.Commands.Session:s=n.session;break;case c.Commands.User:s=o||n.user;break;case c.Commands.Custom:s=o||n.custom;break;case c.Commands.Globals:s=o||n.globals;break;case c.Commands.Config:s=o||n.config;break;case c.Commands.Ready:case c.Commands.Run:default:s=void 0}let i=!1;for(const t of Object.values(n.sources))if(t.on){!1===await(0,p.tryCatchAsync)(t.on)(e,s)&&(i=!0)}if(Object.entries(n.destinations).forEach(([t,o])=>{if(o.on){if(!o.config.init)return o.queueOn=o.queueOn||[],void o.queueOn.push({type:e,data:s});A(n,o,t,e,s)}}),(Object.keys(n.pending.sources).length>0||Object.keys(n.pending.destinations).length>0)&&await async function(n,e){for(const[t,o]of Object.entries(n.pending.sources)){if(!n.pending.sources[t]||n.sources[t])continue;const s=o.config?.require;if(!s)continue;const r=s.indexOf(e);if(-1===r)continue;if(s.splice(r,1),s.length>0)continue;delete n.pending.sources[t];const i=await j(n,t,o);i&&(n.sources[t]=i)}for(const[t,o]of Object.entries(n.pending.destinations)){if(!n.pending.destinations[t]||n.destinations[t])continue;const s=o.config?.require;if(!s)continue;const r=s.indexOf(e);if(-1===r)continue;if(s.splice(r,1),s.length>0)continue;delete n.pending.destinations[t];const i=H(o);!1!==i.config.queue&&(i.queuePush=[...n.queue]),n.destinations[t]=i}}(n,e),!r.length)return!i;switch(e){case c.Commands.Consent:!function(n,e,t){const o=t||n.consent;e.forEach(e=>{Object.keys(o).filter(n=>n in e).forEach(t=>{(0,p.tryCatch)(e[t])(n,o)})})}(n,r,o);break;case c.Commands.Ready:case c.Commands.Run:!function(n,e){n.allowed&&e.forEach(e=>{(0,p.tryCatch)(e)(n)})}(n,r);break;case c.Commands.Session:!function(n,e){if(!n.session)return;e.forEach(e=>{(0,p.tryCatch)(e)(n,n.session)})}(n,r);break;default:r.forEach(e=>{"function"==typeof e&&(0,p.tryCatch)(e)(n,s)})}return!i}async function P(n,e,t){const{code:o,config:s={},env:r={},before:i}=e;if(!(0,g.isFunction)(o.push))return R({ok:!1,failed:{invalid:{type:"invalid",error:"Destination code must have a push method"}}});const a=t||s||{init:!1},c=i?{...a,before:i}:a,u={...o,config:c,env:I(o.env,r)};let f=u.config.id;if(!f)do{f=(0,g.getId)(4)}while(n.destinations[f]);return n.destinations[f]=u,!1!==u.config.queue&&(u.queuePush=[...n.queue]),S(n,void 0,{},{[f]:u})}async function S(n,e,t={},o){const{allowed:s,consent:r,globals:i,user:a}=n;if(!s)return R({ok:!1});e&&(n.queue.push(e),n.status.in++),o||(o=n.destinations);const c=await Promise.all(Object.entries(o||{}).map(async([o,s])=>{let c=(s.queuePush||[]).map(n=>({...n,consent:r}));if(s.queuePush=[],e){const n=(0,g.clone)(e);c.push(n)}if(!c.length&&!s.queueOn?.length)return{id:o,destination:s,skipped:!0};if(!c.length&&s.queueOn?.length){const e=await(0,g.tryCatchAsync)(x)(n,s,o);return{id:o,destination:s,skipped:!e}}const u=[],f=c.filter(n=>{const e=(0,g.getGrantedConsent)(s.config.consent,r,n.consent);return!e||(n.consent=e,u.push(n),!1)});if(s.queuePush.push(...f),!u.length)return{id:o,destination:s,queue:c};if(!await(0,g.tryCatchAsync)(x)(n,s,o))return{id:o,destination:s,queue:c};let l,d;s.dlq||(s.dlq=[]);const p=function(n,e){const t=n.config.before;return t?w(t,y(e)):[]}(s,n.transformers);let m=0;return await Promise.all(u.map(async e=>{e.globals=(0,g.assign)(i,e.globals),e.user=(0,g.assign)(a,e.user);let r=e;if(p.length>0&&n.transformers&&Object.keys(n.transformers).length>0){const o=await O(n,n.transformers,p,e,t.ingest,t.respond);if(null===o)return e;r=o}const c=Date.now(),u=await(0,g.tryCatchAsync)($,e=>{const t=s.type||"unknown";n.logger.scope(t).error("Push failed",{error:e,event:r.name}),l=e,s.dlq.push([r,e])})(n,s,o,r,t.ingest,t.respond);return m+=Date.now()-c,void 0!==u&&(d=u),e})),{id:o,destination:s,error:l,response:d,totalDuration:m}})),u={},f={},l={};for(const e of c){if(e.skipped)continue;const t={type:e.destination.type||"unknown",data:e.response};n.status.destinations[e.id]||(n.status.destinations[e.id]={count:0,failed:0,duration:0});const o=n.status.destinations[e.id],s=Date.now();e.error?(t.error=e.error,l[e.id]=t,o.failed++,o.lastAt=s,o.duration+=e.totalDuration||0,n.status.failed++):e.queue&&e.queue.length?f[e.id]=t:(u[e.id]=t,o.count++,o.lastAt=s,o.duration+=e.totalDuration||0,n.status.out++)}return R({event:e,...Object.keys(u).length&&{done:u},...Object.keys(f).length&&{queued:f},...Object.keys(l).length&&{failed:l}})}async function x(n,e,t){if(e.init&&!e.config.init){const o=e.type||"unknown",s=n.logger.scope(o),r={collector:n,logger:s,id:t,config:e.config,env:I(e.env,e.config.env)};s.debug("init");const i=await(0,g.useHooks)(e.init,"DestinationInit",n.hooks)(r);if(!1===i)return i;if(e.config={...i||e.config,init:!0},e.queueOn?.length){const o=e.queueOn;e.queueOn=[];for(const{type:s,data:r}of o)A(n,e,t,s,r)}s.debug("init done")}return!0}async function $(n,e,t,o,s,r){const{config:i}=e,a=await(0,g.processEventMapping)(o,i,n);if(a.ignore)return!1;const c=e.type||"unknown",u=n.logger.scope(c),f={collector:n,logger:u,id:t,config:i,data:a.data,rule:a.mapping,ingest:s,env:{...I(e.env,i.env),...r?{respond:r}:{}}},l=a.mapping,d=a.mappingKey||"* *";if(!l?.batch||!e.pushBatch){u.debug("push",{event:a.event.name});const t=await(0,g.useHooks)(e.push,"DestinationPush",n.hooks)(a.event,f);return u.debug("push done"),t}{if(e.batches=e.batches||{},!e.batches[d]){const o={key:d,events:[],data:[]};e.batches[d]={batched:o,batchFn:(0,g.debounce)(()=>{const o=e.batches[d].batched,a={collector:n,logger:u,id:t,config:i,data:void 0,rule:l,ingest:s,env:{...I(e.env,i.env),...r?{respond:r}:{}}};u.debug("push batch",{events:o.events.length}),(0,g.useHooks)(e.pushBatch,"DestinationPushBatch",n.hooks)(o,a),u.debug("push batch done"),o.events=[],o.data=[]},l.batch)}}const o=e.batches[d];o.batched.events.push(a.event),(0,g.isDefined)(a.data)&&o.batched.data.push(a.data),o.batchFn()}return!0}function R(n){return{ok:!n?.failed,...n}}function H(n){const{code:e,config:t={},env:o={}}=n,{config:s}=b(n,"before"),r={...e.config,...t,...s},i=I(e.env,o);return{...e,config:r,env:i}}async function T(n,e={}){const t={};for(const[o,s]of Object.entries(e))s.config?.require?.length?n.pending.destinations[o]=s:t[o]=H(s);return t}function I(n,e){return n||e?e?n&&(0,g.isObject)(n)&&(0,g.isObject)(e)?{...n,...e}:e:n:{}}var M=require("@walkeros/core"),F=require("@walkeros/core"),G=5e3;async function N(n,e,t){const o=Object.entries(n).map(async([n,o])=>{const s=o.destroy;if(!s)return;const r=o.type||"unknown",i=t.scope(r),a={id:n,config:o.config,env:o.env??{},logger:i};try{await Promise.race([s(a),new Promise((t,o)=>setTimeout(()=>o(new Error(`${e} '${n}' destroy timed out`)),G))])}catch(t){i.error(`${e} '${n}' destroy failed: ${t}`)}});await Promise.allSettled(o)}async function B(n,e,t,o){let s,r,i=!1,a=!1;switch(e){case c.Commands.Config:(0,F.isObject)(t)&&((0,M.assign)(n.config,t,{shallow:!1}),r=t,i=!0);break;case c.Commands.Consent:if((0,F.isObject)(t)){const{update:e,runQueue:o}=f(n,t);r=e,i=!0,a=o}break;case c.Commands.Custom:(0,F.isObject)(t)&&(n.custom=(0,M.assign)(n.custom,t),r=t,i=!0);break;case c.Commands.Destination:(0,F.isObject)(t)&&("code"in t&&(0,F.isObject)(t.code)?s=await P(n,t,o):(0,M.isFunction)(t.push)&&(s=await P(n,{code:t},o)));break;case c.Commands.Globals:(0,F.isObject)(t)&&(n.globals=(0,M.assign)(n.globals,t),r=t,i=!0);break;case c.Commands.On:(0,M.isString)(t)&&await D(n,t,o);break;case c.Commands.Ready:i=!0;break;case c.Commands.Run:s=await _(n,t),i=!0;break;case c.Commands.Session:i=!0;break;case c.Commands.Shutdown:await async function(n){const e=n.logger;await N(n.sources,"source",e),await N(n.destinations,"destination",e),await N(n.transformers,"transformer",e),await N(n.stores,"store",e)}(n);break;case c.Commands.User:(0,F.isObject)(t)&&((0,M.assign)(n.user,t,{shallow:!1}),r=t,i=!0)}return i&&await E(n,e,void 0,r),a&&(s=await S(n)),s||R({ok:!0})}function U(n,e){if(!e.name)throw new Error("Event name is required");const[t,o]=e.name.split(" ");if(!t||!o)throw new Error("Event name is invalid");++n.count;const{timestamp:s=Date.now(),group:r=n.group,count:i=n.count}=e,{name:a=`${t} ${o}`,data:c={},context:u={},globals:f=n.globals,custom:l={},user:g=n.user,nested:d=[],consent:p=n.consent,id:m=`${s}-${r}-${i}`,trigger:h="",entity:y=t,action:b=o,timing:w=0,version:v={source:n.version,tagging:n.config.tagging||0},source:k={type:"collector",id:"",previous_id:""}}=e;return{name:a,data:c,context:u,globals:f,custom:l,user:g,nested:d,consent:p,id:m,trigger:h,entity:y,action:b,timestamp:s,timing:w,group:r,count:i,version:v,source:k}}async function _(n,e){n.allowed=!0,n.count=0,n.group=(0,M.getId)(),n.timing=Date.now(),e&&(e.consent&&(n.consent=(0,M.assign)(n.consent,e.consent)),e.user&&(n.user=(0,M.assign)(n.user,e.user)),e.globals&&(n.globals=(0,M.assign)(n.config.globalsStatic||{},e.globals)),e.custom&&(n.custom=(0,M.assign)(n.custom,e.custom))),Object.values(n.destinations).forEach(n=>{n.queuePush=[]}),n.queue=[],n.round++;return await S(n)}var z=require("@walkeros/core");function J(n,e){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}=o;let f=t;const l=i?Object.freeze(i):void 0;if(c){const e=await(0,z.processEventMapping)(f,c,n);if(e.ignore)return R({ok:!0});if(c.consent){if(!(0,z.getGrantedConsent)(c.consent,n.consent,e.event.consent))return R({ok:!0})}f=e.event}if(u?.length&&n.transformers&&Object.keys(n.transformers).length>0){const e=await O(n,n.transformers,u,f,l,a);if(null===e)return R({ok:!0});f=e}const g=e(f),d=U(n,g),p=await S(n,d,{id:r,ingest:l,respond:a});if(r){n.status.sources[r]||(n.status.sources[r]={count:0,duration:0});const e=n.status.sources[r];e.count++,e.lastAt=Date.now(),e.duration+=Date.now()-s}return p},()=>R({ok:!1}))(),"Push",n.hooks)}var L=require("@walkeros/core");async function V(n){const e=(0,l.assign)({globalsStatic:{},sessionStatic:{},tagging:0,run:!0},n,{merge:!1,extend:!1}),t={level:n.logger?.level,handler:n.logger?.handler},o=(0,l.createLogger)(t),s={...e.globalsStatic,...n.globals},r={allowed:!1,config:e,consent:n.consent||{},count:0,custom:n.custom||{},destinations:{},transformers:{},stores:{},globals:s,group:"",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:n.user||{},version:"3.0.0",sources:{},pending:{sources:{},destinations:{}},push:void 0,command:void 0};r.push=J(r,n=>({timing:Math.round((Date.now()-r.timing)/10)/100,source:{type:"collector",id:"",previous_id:""},...n})),r.command=function(n,e){return(0,L.useHooks)(async(t,o,s)=>await(0,L.tryCatchAsync)(async()=>await e(n,t,o,s),()=>R({ok:!1}))(),"Command",n.hooks)}(r,B);const i=n.stores||{};return r.stores=await async function(n,e={}){const t={};for(const[o,s]of Object.entries(e)){const{code:e,config:r={},env:i={}}=s,a=n.logger.scope("store").scope(o),c={collector:n,logger:a,id:o,config:r,env:i},u=await e(c);t[o]=u}return t}(r,i),function(n,e,t){const o=new Map;for(const[t,s]of Object.entries(n))e[t]&&o.set(s,e[t]);if(0!==o.size)for(const n of[t.transformers,t.destinations,t.sources])if(n)for(const e of Object.values(n))s(e.env);function s(n){if(n)for(const[e,t]of Object.entries(n))if("object"==typeof t&&null!==t){const s=o.get(t);s&&(n[e]=s)}}}(i,r.stores,n),r.destinations=await T(r,n.destinations||{}),r.transformers=await async function(n,e={}){const t={};for(const[o,s]of Object.entries(e)){const{code:e,env:r={}}=s,{config:i}=b(s,"next"),a=Object.keys(r).length>0?{...i,env:r}:i,c=n.logger.scope("transformer").scope(o),u={collector:n,logger:c,id:o,config:a,env:r},f=await e(u);t[o]=f}return t}(r,n.transformers||{}),r}async function W(n){n=n||{};const e=await V(n),t=(o=e,{type:"elb",config:{},push:async(n,e,t,s,r,i)=>{if("string"==typeof n&&n.startsWith("walker ")){const s=n.replace("walker ","");return o.command(s,e,t)}let a;if("string"==typeof n)a={name:n},e&&"object"==typeof e&&!Array.isArray(e)&&(a.data=e);else{if(!n||"object"!=typeof n)return R({ok:!1});a=n,e&&"object"==typeof e&&!Array.isArray(e)&&(a.data={...a.data||{},...e})}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;e.sources.elb=t;const s=await q(e,n.sources||{});Object.assign(e.sources,s);const{consent:r,user:i,globals:a,custom:c}=n;r&&await e.command("consent",r),i&&await e.command("user",i),a&&Object.assign(e.globals,a),c&&Object.assign(e.custom,c),e.config.run&&await e.command("run");let u=t.push;const f=Object.values(e.sources).filter(n=>"elb"!==n.type),l=f.find(n=>n.config.primary);return l?u=l.push:f.length>0&&(u=f[0].push),{collector:e,elb:u}}function Q(n){if(null===n||"object"!=typeof n)return n;if(Array.isArray(n))return n.map(Q);const e={};for(const[t,o]of Object.entries(n))e[t]="function"==typeof o?o:Q(o);return e}function K(n){const e=[],{simulation:t,...o}=n,s=Q(o);for(const n of t){const t=n.startsWith("call:")?n.slice(5):n,o=t.split(".");let r=s;for(let n=0;n<o.length-1&&null!=r[o[n]];n++)r=r[o[n]];const i=o[o.length-1];if(null==r||!(i in r))continue;const a=r[i];"function"==typeof a&&(r[i]=function(...n){return e.push({fn:t,args:n,ts:Date.now()}),a.apply(this,n)})}return{wrappedEnv:s,calls:e}}async function X(n){const e=Date.now();try{switch(n.step){case"transformer":return await async function(n,e){const{code:t,config:o={},event:s}=n,{collector:r}=await W({transformers:{sim:{code:t,config:o}}}),i=r.transformers?.sim;if(!i)throw new Error("Transformer failed to initialize");const a=await i.push(s,{collector:r,logger:r.logger.scope("transformer").scope("sim"),id:"sim",config:i.config,env:i.config?.env||{}});let c;c=!1===a?[]:null==a?[s]:[a.event||s];return{step:"transformer",name:n.name,events:c,calls:[],duration:Date.now()-e}}(n,e);case"source":return await async function(n,e){const{code:t,config:o={},setup:s,input:r,env:i,consent:a}=n,c={functional:!0,marketing:!0,analytics:!0};let u;if(s){const n=s(r,i);"function"==typeof n&&(u=n)}const f=[],{collector:l}=await W({consent:a||c,sources:{sim:{code:t,config:o,env:i,next:"spy"}},transformers:{spy:{code:()=>({type:"spy",config:{},push:n=>(f.push(JSON.parse(JSON.stringify(n))),{event:n})})}}});u&&u();return{step:"source",name:n.name,events:f,calls:[],duration:Date.now()-e}}(n,e);case"destination":return await async function(n,e){const{code:t,config:o={},event:s,consent:r,env:i,track:a}=n,c={functional:!0,marketing:!0,analytics:!0};let u=[],f=i;if(i&&a&&a.length>0){const n=K({...i,simulation:a});f=n.wrappedEnv,u=n.calls}const l={...o};f&&(l.env=f);const{collector:g}=await W({consent:r||c,destinations:{sim:{code:t,config:l}}});return await g.push(s),{step:"destination",name:n.name,events:[],calls:u,duration:Date.now()-e}}(n,e)}}catch(t){return{step:n.step,name:n.name,events:[],calls:[],duration:Date.now()-e,error:t instanceof Error?t:new Error(String(t))}}}//# sourceMappingURL=index.js.map
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var n={},e={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:e,Utils:{Storage:{Cookie:"cookie",Local:"local",Session:"session"}}};import{assign as o}from"@walkeros/core";function s(n,e){let t=!1;const s={};return Object.entries(e).forEach(([n,e])=>{const o=!!e;s[n]=o,t=t||o}),n.consent=o(n.consent,s),{update:s,runQueue:t}}import{assign as r,createLogger as i}from"@walkeros/core";import{assign as a,clone as c,debounce as u,getId as f,getGrantedConsent as l,isDefined as g,isFunction as d,isObject as m,processEventMapping as p,tryCatchAsync as h,useHooks as w}from"@walkeros/core";import{isArray as b}from"@walkeros/core";import{tryCatch as y,tryCatchAsync as v}from"@walkeros/core";import{getMappingValue as k,tryCatchAsync as O}from"@walkeros/core";import{isObject as C,tryCatchAsync as j,useHooks as q}from"@walkeros/core";function D(n){const e={};for(const[t,o]of Object.entries(n))o.config?.next?e[t]={next:o.config.next}:e[t]={};return e}function A(n,e){const t=n.config||{},o=n[e];return void 0!==o?{config:{...t,[e]:o},chainValue:o}:{config:t,chainValue:void 0}}function E(n,e={}){if(!n)return[];if(Array.isArray(n))return n;const t=[],o=new Set;let s=n;for(;s&&e[s]&&!o.has(s);){o.add(s),t.push(s);const n=e[s].next;if(Array.isArray(n)){t.push(...n);break}s=n}return t}async function x(n,e,t){if(e.init&&!e.config.init){const o=e.type||"unknown",s=n.logger.scope(`transformer:${o}`),r={collector:n,logger:s,id:t,config:e.config,env:$(e.config.env)};s.debug("init");const i=await q(e.init,"TransformerInit",n.hooks)(r);if(!1===i)return!1;e.config={...i||e.config,env:i?.env||e.config.env,init:!0},s.debug("init done")}return!0}async function P(n,e,t,o,s,r){const i=e.type||"unknown",a=n.logger.scope(`transformer:${i}`),c={collector:n,logger:a,id:t,ingest:s,config:e.config,env:{...$(e.config.env),...r?{respond:r}:{}}};a.debug("push",{event:o.name});const u=await q(e.push,"TransformerPush",n.hooks)(o,c);return a.debug("push done"),u}async function S(n,e,t,o,s,r){let i=o,a=r;for(const o of t){const t=e[o];if(!t){n.logger.warn(`Transformer not found: ${o}`);continue}if(!await j(x)(n,t,o))return n.logger.error(`Transformer init failed: ${o}`),null;const r=await j(P,e=>(n.logger.scope(`transformer:${t.type||"unknown"}`).error("Push failed",{error:e}),!1))(n,t,o,i,s,a);if(!1===r)return null;if(r&&"object"==typeof r){const{event:t,respond:o,next:c}=r;if(o&&(a=o),c){const o=E(c,D(e));return o.length>0?S(n,e,o,t||i,s,a):(n.logger.warn(`Branch target not found: ${JSON.stringify(c)}`),null)}t&&(i=t)}}return i}function $(n){return n&&C(n)?n:{}}async function R(n,e,t){const{code:o,config:s={},env:r={},primary:i,next:a}=t;let c,u;const f=E(a,D(n.transformers)),l=n.logger.scope("source").scope(e),g={push:(t,o={})=>n.push(t,{...o,id:e,ingest:c,respond:u,mapping:s,preChain:f}),command:n.command,sources:n.sources,elb:n.sources.elb.push,logger:l,...r},d={collector:n,logger:l,id:e,config:s,env:g,setIngest:async e=>{c=s.ingest?await k(e,s.ingest,{collector:n}):void 0},setRespond:n=>{u=n}},m=await O(o)(d);if(!m)return;const p=m.type||"unknown",h=n.logger.scope(p).scope(e);return g.logger=h,i&&(m.config={...m.config,primary:i}),m}async function T(n,e={}){const t={};for(const[o,s]of Object.entries(e)){const{config:e={}}=s;if(e.require&&e.require.length>0){n.pending.sources[o]=s;continue}const r=await R(n,o,s);r&&(t[o]=r)}return t}async function I(n,e,t){const o=n.on,s=o[e]||[],r=b(t)?t:[t];r.forEach(n=>{s.push(n)}),o[e]=s,await G(n,e,r)}function B(n,e,t,o,s){if(!e.on)return;const r=e.type||"unknown",i=n.logger.scope(r).scope("on").scope(o),a={collector:n,logger:i,id:t,config:e.config,data:s,env:W(e.env,e.config.env)};y(e.on)(o,a)}async function G(n,e,o,s){let r,i=o||[];switch(o||(i=n.on[e]||[]),e){case t.Commands.Consent:r=s||n.consent;break;case t.Commands.Session:r=n.session;break;case t.Commands.User:r=s||n.user;break;case t.Commands.Custom:r=s||n.custom;break;case t.Commands.Globals:r=s||n.globals;break;case t.Commands.Config:r=s||n.config;break;case t.Commands.Ready:case t.Commands.Run:default:r=void 0}let a=!1;for(const t of Object.values(n.sources))if(t.on){!1===await v(t.on)(e,r)&&(a=!0)}if(Object.entries(n.destinations).forEach(([t,o])=>{if(o.on){if(!o.config.init)return o.queueOn=o.queueOn||[],void o.queueOn.push({type:e,data:r});B(n,o,t,e,r)}}),(Object.keys(n.pending.sources).length>0||Object.keys(n.pending.destinations).length>0)&&await async function(n,e){for(const[t,o]of Object.entries(n.pending.sources)){if(!n.pending.sources[t]||n.sources[t])continue;const s=o.config?.require;if(!s)continue;const r=s.indexOf(e);if(-1===r)continue;if(s.splice(r,1),s.length>0)continue;delete n.pending.sources[t];const i=await R(n,t,o);i&&(n.sources[t]=i)}for(const[t,o]of Object.entries(n.pending.destinations)){if(!n.pending.destinations[t]||n.destinations[t])continue;const s=o.config?.require;if(!s)continue;const r=s.indexOf(e);if(-1===r)continue;if(s.splice(r,1),s.length>0)continue;delete n.pending.destinations[t];const i=M(o);!1!==i.config.queue&&(i.queuePush=[...n.queue]),n.destinations[t]=i}}(n,e),!i.length)return!a;switch(e){case t.Commands.Consent:!function(n,e,t){const o=t||n.consent;e.forEach(e=>{Object.keys(o).filter(n=>n in e).forEach(t=>{y(e[t])(n,o)})})}(n,i,s);break;case t.Commands.Ready:case t.Commands.Run:!function(n,e){n.allowed&&e.forEach(e=>{y(e)(n)})}(n,i);break;case t.Commands.Session:!function(n,e){if(!n.session)return;e.forEach(e=>{y(e)(n,n.session)})}(n,i);break;default:i.forEach(e=>{"function"==typeof e&&y(e)(n,r)})}return!a}async function H(n,e,t){const{code:o,config:s={},env:r={},before:i}=e;if(!d(o.push))return J({ok:!1,failed:{invalid:{type:"invalid",error:"Destination code must have a push method"}}});const a=t||s||{init:!1},c=i?{...a,before:i}:a,u={...o,config:c,env:W(o.env,r)};let l=u.config.id;if(!l)do{l=f(4)}while(n.destinations[l]);return n.destinations[l]=u,!1!==u.config.queue&&(u.queuePush=[...n.queue]),U(n,void 0,{},{[l]:u})}async function U(n,e,t={},o){const{allowed:s,consent:r,globals:i,user:u}=n;if(!s)return J({ok:!1});e&&(n.queue.push(e),n.status.in++),o||(o=n.destinations);const f=await Promise.all(Object.entries(o||{}).map(async([o,s])=>{let f=(s.queuePush||[]).map(n=>({...n,consent:r}));if(s.queuePush=[],e){const n=c(e);f.push(n)}if(!f.length&&!s.queueOn?.length)return{id:o,destination:s,skipped:!0};if(!f.length&&s.queueOn?.length){const e=await h(z)(n,s,o);return{id:o,destination:s,skipped:!e}}const g=[],d=f.filter(n=>{const e=l(s.config.consent,r,n.consent);return!e||(n.consent=e,g.push(n),!1)});if(s.queuePush.push(...d),!g.length)return{id:o,destination:s,queue:f};if(!await h(z)(n,s,o))return{id:o,destination:s,queue:f};let m,p;s.dlq||(s.dlq=[]);const w=function(n,e){const t=n.config.before;return t?E(t,D(e)):[]}(s,n.transformers);let b=0;return await Promise.all(g.map(async e=>{e.globals=a(i,e.globals),e.user=a(u,e.user);let r=e;if(w.length>0&&n.transformers&&Object.keys(n.transformers).length>0){const o=await S(n,n.transformers,w,e,t.ingest,t.respond);if(null===o)return e;r=o}const c=Date.now(),f=await h(F,e=>{const t=s.type||"unknown";n.logger.scope(t).error("Push failed",{error:e,event:r.name}),m=e,s.dlq.push([r,e])})(n,s,o,r,t.ingest,t.respond);return b+=Date.now()-c,void 0!==f&&(p=f),e})),{id:o,destination:s,error:m,response:p,totalDuration:b}})),g={},d={},m={};for(const e of f){if(e.skipped)continue;const t={type:e.destination.type||"unknown",data:e.response};n.status.destinations[e.id]||(n.status.destinations[e.id]={count:0,failed:0,duration:0});const o=n.status.destinations[e.id],s=Date.now();e.error?(t.error=e.error,m[e.id]=t,o.failed++,o.lastAt=s,o.duration+=e.totalDuration||0,n.status.failed++):e.queue&&e.queue.length?d[e.id]=t:(g[e.id]=t,o.count++,o.lastAt=s,o.duration+=e.totalDuration||0,n.status.out++)}return J({event:e,...Object.keys(g).length&&{done:g},...Object.keys(d).length&&{queued:d},...Object.keys(m).length&&{failed:m}})}async function z(n,e,t){if(e.init&&!e.config.init){const o=e.type||"unknown",s=n.logger.scope(o),r={collector:n,logger:s,id:t,config:e.config,env:W(e.env,e.config.env)};s.debug("init");const i=await w(e.init,"DestinationInit",n.hooks)(r);if(!1===i)return i;if(e.config={...i||e.config,init:!0},e.queueOn?.length){const o=e.queueOn;e.queueOn=[];for(const{type:s,data:r}of o)B(n,e,t,s,r)}s.debug("init done")}return!0}async function F(n,e,t,o,s,r){const{config:i}=e,a=await p(o,i,n);if(a.ignore)return!1;const c=e.type||"unknown",f=n.logger.scope(c),l={collector:n,logger:f,id:t,config:i,data:a.data,rule:a.mapping,ingest:s,env:{...W(e.env,i.env),...r?{respond:r}:{}}},d=a.mapping,m=a.mappingKey||"* *";if(!d?.batch||!e.pushBatch){f.debug("push",{event:a.event.name});const t=await w(e.push,"DestinationPush",n.hooks)(a.event,l);return f.debug("push done"),t}{if(e.batches=e.batches||{},!e.batches[m]){const o={key:m,events:[],data:[]};e.batches[m]={batched:o,batchFn:u(()=>{const o=e.batches[m].batched,a={collector:n,logger:f,id:t,config:i,data:void 0,rule:d,ingest:s,env:{...W(e.env,i.env),...r?{respond:r}:{}}};f.debug("push batch",{events:o.events.length}),w(e.pushBatch,"DestinationPushBatch",n.hooks)(o,a),f.debug("push batch done"),o.events=[],o.data=[]},d.batch)}}const o=e.batches[m];o.batched.events.push(a.event),g(a.data)&&o.batched.data.push(a.data),o.batchFn()}return!0}function J(n){return{ok:!n?.failed,...n}}function M(n){const{code:e,config:t={},env:o={}}=n,{config:s}=A(n,"before"),r={...e.config,...t,...s},i=W(e.env,o);return{...e,config:r,env:i}}async function N(n,e={}){const t={};for(const[o,s]of Object.entries(e))s.config?.require?.length?n.pending.destinations[o]=s:t[o]=M(s);return t}function W(n,e){return n||e?e?n&&m(n)&&m(e)?{...n,...e}:e:n:{}}import{assign as L,getId as Q,isFunction as V,isString as _}from"@walkeros/core";import{isObject as K}from"@walkeros/core";async function X(n,e,t){const o=Object.entries(n).map(async([n,o])=>{const s=o.destroy;if(!s)return;const r=o.type||"unknown",i=t.scope(r),a={id:n,config:o.config,env:o.env??{},logger:i};try{await Promise.race([s(a),new Promise((t,o)=>setTimeout(()=>o(new Error(`${e} '${n}' destroy timed out`)),5e3))])}catch(t){i.error(`${e} '${n}' destroy failed: ${t}`)}});await Promise.allSettled(o)}async function Y(n,e,o,r){let i,a,c=!1,u=!1;switch(e){case t.Commands.Config:K(o)&&(L(n.config,o,{shallow:!1}),a=o,c=!0);break;case t.Commands.Consent:if(K(o)){const{update:e,runQueue:t}=s(n,o);a=e,c=!0,u=t}break;case t.Commands.Custom:K(o)&&(n.custom=L(n.custom,o),a=o,c=!0);break;case t.Commands.Destination:K(o)&&("code"in o&&K(o.code)?i=await H(n,o,r):V(o.push)&&(i=await H(n,{code:o},r)));break;case t.Commands.Globals:K(o)&&(n.globals=L(n.globals,o),a=o,c=!0);break;case t.Commands.On:_(o)&&await I(n,o,r);break;case t.Commands.Ready:c=!0;break;case t.Commands.Run:i=await nn(n,o),c=!0;break;case t.Commands.Session:c=!0;break;case t.Commands.Shutdown:await async function(n){const e=n.logger;await X(n.sources,"source",e),await X(n.destinations,"destination",e),await X(n.transformers,"transformer",e),await X(n.stores,"store",e)}(n);break;case t.Commands.User:K(o)&&(L(n.user,o,{shallow:!1}),a=o,c=!0)}return c&&await G(n,e,void 0,a),u&&(i=await U(n)),i||J({ok:!0})}function Z(n,e){if(!e.name)throw new Error("Event name is required");const[t,o]=e.name.split(" ");if(!t||!o)throw new Error("Event name is invalid");++n.count;const{timestamp:s=Date.now(),group:r=n.group,count:i=n.count}=e,{name:a=`${t} ${o}`,data:c={},context:u={},globals:f=n.globals,custom:l={},user:g=n.user,nested:d=[],consent:m=n.consent,id:p=`${s}-${r}-${i}`,trigger:h="",entity:w=t,action:b=o,timing:y=0,version:v={source:n.version,tagging:n.config.tagging||0},source:k={type:"collector",id:"",previous_id:""}}=e;return{name:a,data:c,context:u,globals:f,custom:l,user:g,nested:d,consent:m,id:p,trigger:h,entity:w,action:b,timestamp:s,timing:y,group:r,count:i,version:v,source:k}}async function nn(n,e){n.allowed=!0,n.count=0,n.group=Q(),n.timing=Date.now(),e&&(e.consent&&(n.consent=L(n.consent,e.consent)),e.user&&(n.user=L(n.user,e.user)),e.globals&&(n.globals=L(n.config.globalsStatic||{},e.globals)),e.custom&&(n.custom=L(n.custom,e.custom))),Object.values(n.destinations).forEach(n=>{n.queuePush=[]}),n.queue=[],n.round++;return await U(n)}import{getGrantedConsent as en,processEventMapping as tn,tryCatchAsync as on,useHooks as sn}from"@walkeros/core";function rn(n,e){return sn(async(t,o={})=>await on(async()=>{const s=Date.now(),{id:r,ingest:i,respond:a,mapping:c,preChain:u}=o;let f=t;const l=i?Object.freeze(i):void 0;if(c){const e=await tn(f,c,n);if(e.ignore)return J({ok:!0});if(c.consent){if(!en(c.consent,n.consent,e.event.consent))return J({ok:!0})}f=e.event}if(u?.length&&n.transformers&&Object.keys(n.transformers).length>0){const e=await S(n,n.transformers,u,f,l,a);if(null===e)return J({ok:!0});f=e}const g=e(f),d=Z(n,g),m=await U(n,d,{id:r,ingest:l,respond:a});if(r){n.status.sources[r]||(n.status.sources[r]={count:0,duration:0});const e=n.status.sources[r];e.count++,e.lastAt=Date.now(),e.duration+=Date.now()-s}return m},()=>J({ok:!1}))(),"Push",n.hooks)}import{useHooks as an,tryCatchAsync as cn}from"@walkeros/core";async function un(n){const e=r({globalsStatic:{},sessionStatic:{},tagging:0,run:!0},n,{merge:!1,extend:!1}),t={level:n.logger?.level,handler:n.logger?.handler},o=i(t),s={...e.globalsStatic,...n.globals},a={allowed:!1,config:e,consent:n.consent||{},count:0,custom:n.custom||{},destinations:{},transformers:{},stores:{},globals:s,group:"",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:n.user||{},version:"2.1.1",sources:{},pending:{sources:{},destinations:{}},push:void 0,command:void 0};a.push=rn(a,n=>({timing:Math.round((Date.now()-a.timing)/10)/100,source:{type:"collector",id:"",previous_id:""},...n})),a.command=function(n,e){return an(async(t,o,s)=>await cn(async()=>await e(n,t,o,s),()=>J({ok:!1}))(),"Command",n.hooks)}(a,Y);const c=n.stores||{};return a.stores=await async function(n,e={}){const t={};for(const[o,s]of Object.entries(e)){const{code:e,config:r={},env:i={}}=s,a=n.logger.scope("store").scope(o),c={collector:n,logger:a,id:o,config:r,env:i},u=await e(c);t[o]=u}return t}(a,c),function(n,e,t){const o=new Map;for(const[t,s]of Object.entries(n))e[t]&&o.set(s,e[t]);if(0!==o.size)for(const n of[t.transformers,t.destinations,t.sources])if(n)for(const e of Object.values(n))s(e.env);function s(n){if(n)for(const[e,t]of Object.entries(n))if("object"==typeof t&&null!==t){const s=o.get(t);s&&(n[e]=s)}}}(c,a.stores,n),a.destinations=await N(a,n.destinations||{}),a.transformers=await async function(n,e={}){const t={};for(const[o,s]of Object.entries(e)){const{code:e,env:r={}}=s,{config:i}=A(s,"next"),a=Object.keys(r).length>0?{...i,env:r}:i,c=n.logger.scope("transformer").scope(o),u={collector:n,logger:c,id:o,config:a,env:r},f=await e(u);t[o]=f}return t}(a,n.transformers||{}),a}async function fn(n){n=n||{};const e=await un(n),t=(o=e,{type:"elb",config:{},push:async(n,e,t,s,r,i)=>{if("string"==typeof n&&n.startsWith("walker ")){const s=n.replace("walker ","");return o.command(s,e,t)}let a;if("string"==typeof n)a={name:n},e&&"object"==typeof e&&!Array.isArray(e)&&(a.data=e);else{if(!n||"object"!=typeof n)return J({ok:!1});a=n,e&&"object"==typeof e&&!Array.isArray(e)&&(a.data={...a.data||{},...e})}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;e.sources.elb=t;const s=await T(e,n.sources||{});Object.assign(e.sources,s);const{consent:r,user:i,globals:a,custom:c}=n;r&&await e.command("consent",r),i&&await e.command("user",i),a&&Object.assign(e.globals,a),c&&Object.assign(e.custom,c),e.config.run&&await e.command("run");let u=t.push;const f=Object.values(e.sources).filter(n=>"elb"!==n.type),l=f.find(n=>n.config.primary);return l?u=l.push:f.length>0&&(u=f[0].push),{collector:e,elb:u}}function ln(n){if(null===n||"object"!=typeof n)return n;if(Array.isArray(n))return n.map(ln);const e={};for(const[t,o]of Object.entries(n))e[t]="function"==typeof o?o:ln(o);return e}function gn(n){const e=[],{simulation:t,...o}=n,s=ln(o);for(const n of t){const t=n.startsWith("call:")?n.slice(5):n,o=t.split(".");let r=s;for(let n=0;n<o.length-1&&null!=r[o[n]];n++)r=r[o[n]];const i=o[o.length-1];if(null==r||!(i in r))continue;const a=r[i];"function"==typeof a&&(r[i]=function(...n){return e.push({fn:t,args:n,ts:Date.now()}),a.apply(this,n)})}return{wrappedEnv:s,calls:e}}async function dn(n){const e=Date.now();try{switch(n.step){case"transformer":return await async function(n,e){const{code:t,config:o={},event:s}=n,{collector:r}=await fn({transformers:{sim:{code:t,config:o}}}),i=r.transformers?.sim;if(!i)throw new Error("Transformer failed to initialize");const a=await i.push(s,{collector:r,logger:r.logger.scope("transformer").scope("sim"),id:"sim",config:i.config,env:i.config?.env||{}});let c;c=!1===a?[]:null==a?[s]:[a.event||s];return{step:"transformer",name:n.name,events:c,calls:[],duration:Date.now()-e}}(n,e);case"source":return await async function(n,e){const{code:t,config:o={},setup:s,input:r,env:i,consent:a}=n,c={functional:!0,marketing:!0,analytics:!0};let u;if(s){const n=s(r,i);"function"==typeof n&&(u=n)}const f=[],{collector:l}=await fn({consent:a||c,sources:{sim:{code:t,config:o,env:i,next:"spy"}},transformers:{spy:{code:()=>({type:"spy",config:{},push:n=>(f.push(JSON.parse(JSON.stringify(n))),{event:n})})}}});u&&u();return{step:"source",name:n.name,events:f,calls:[],duration:Date.now()-e}}(n,e);case"destination":return await async function(n,e){const{code:t,config:o={},event:s,consent:r,env:i,track:a}=n,c={functional:!0,marketing:!0,analytics:!0};let u=[],f=i;if(i&&a&&a.length>0){const n=gn({...i,simulation:a});f=n.wrappedEnv,u=n.calls}const l={...o};f&&(l.env=f);const{collector:g}=await fn({consent:r||c,destinations:{sim:{code:t,config:l}}});return await g.push(s),{step:"destination",name:n.name,events:[],calls:u,duration:Date.now()-e}}(n,e)}}catch(t){return{step:n.step,name:n.name,events:[],calls:[],duration:Date.now()-e,error:t instanceof Error?t:new Error(String(t))}}}export{n as Code,e as Commands,t as Const,H as addDestination,B as callDestinationOn,Y as commonHandleCommand,Z as createEvent,rn as createPush,J as createPushResult,z as destinationInit,F as destinationPush,D as extractTransformerNextMap,N as initDestinations,R as initSource,T as initSources,W as mergeEnvironments,I as on,G as onApply,s as processConsent,U as pushToDestinations,M as registerDestination,nn as runCollector,dn as simulate,fn as startFlow,E as walkChain,gn as wrapEnv};//# sourceMappingURL=index.mjs.map
|
|
1
|
+
var n={},e={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:e,Utils:{Storage:{Cookie:"cookie",Local:"local",Session:"session"}}};import{assign as o}from"@walkeros/core";function s(n,e){let t=!1;const s={};return Object.entries(e).forEach(([n,e])=>{const o=!!e;s[n]=o,t=t||o}),n.consent=o(n.consent,s),{update:s,runQueue:t}}import{assign as r,createLogger as i}from"@walkeros/core";import{assign as a,clone as c,debounce as u,getId as f,getGrantedConsent as l,isDefined as g,isFunction as d,isObject as m,processEventMapping as p,tryCatchAsync as h,useHooks as w}from"@walkeros/core";import{isArray as b}from"@walkeros/core";import{tryCatch as y,tryCatchAsync as v}from"@walkeros/core";import{getMappingValue as k,tryCatchAsync as O}from"@walkeros/core";import{isObject as C,tryCatchAsync as j,useHooks as q}from"@walkeros/core";function D(n){const e={};for(const[t,o]of Object.entries(n))o.config?.next?e[t]={next:o.config.next}:e[t]={};return e}function A(n,e){const t=n.config||{},o=n[e];return void 0!==o?{config:{...t,[e]:o},chainValue:o}:{config:t,chainValue:void 0}}function E(n,e={}){if(!n)return[];if(Array.isArray(n))return n;const t=[],o=new Set;let s=n;for(;s&&e[s]&&!o.has(s);){o.add(s),t.push(s);const n=e[s].next;if(Array.isArray(n)){t.push(...n);break}s=n}return t}async function x(n,e,t){if(e.init&&!e.config.init){const o=e.type||"unknown",s=n.logger.scope(`transformer:${o}`),r={collector:n,logger:s,id:t,config:e.config,env:$(e.config.env)};s.debug("init");const i=await q(e.init,"TransformerInit",n.hooks)(r);if(!1===i)return!1;e.config={...i||e.config,env:i?.env||e.config.env,init:!0},s.debug("init done")}return!0}async function P(n,e,t,o,s,r){const i=e.type||"unknown",a=n.logger.scope(`transformer:${i}`),c={collector:n,logger:a,id:t,ingest:s,config:e.config,env:{...$(e.config.env),...r?{respond:r}:{}}};a.debug("push",{event:o.name});const u=await q(e.push,"TransformerPush",n.hooks)(o,c);return a.debug("push done"),u}async function S(n,e,t,o,s,r){let i=o,a=r;for(const o of t){const t=e[o];if(!t){n.logger.warn(`Transformer not found: ${o}`);continue}if(!await j(x)(n,t,o))return n.logger.error(`Transformer init failed: ${o}`),null;const r=await j(P,e=>(n.logger.scope(`transformer:${t.type||"unknown"}`).error("Push failed",{error:e}),!1))(n,t,o,i,s,a);if(!1===r)return null;if(r&&"object"==typeof r){const{event:t,respond:o,next:c}=r;if(o&&(a=o),c){const o=E(c,D(e));return o.length>0?S(n,e,o,t||i,s,a):(n.logger.warn(`Branch target not found: ${JSON.stringify(c)}`),null)}t&&(i=t)}}return i}function $(n){return n&&C(n)?n:{}}async function R(n,e,t){const{code:o,config:s={},env:r={},primary:i,next:a}=t;let c,u;const f=E(a,D(n.transformers)),l=n.logger.scope("source").scope(e),g={push:(t,o={})=>n.push(t,{...o,id:e,ingest:c,respond:u,mapping:s,preChain:f}),command:n.command,sources:n.sources,elb:n.sources.elb.push,logger:l,...r},d={collector:n,logger:l,id:e,config:s,env:g,setIngest:async e=>{c=s.ingest?await k(e,s.ingest,{collector:n}):void 0},setRespond:n=>{u=n}},m=await O(o)(d);if(!m)return;const p=m.type||"unknown",h=n.logger.scope(p).scope(e);return g.logger=h,i&&(m.config={...m.config,primary:i}),m}async function T(n,e={}){const t={};for(const[o,s]of Object.entries(e)){const{config:e={}}=s;if(e.require&&e.require.length>0){n.pending.sources[o]=s;continue}const r=await R(n,o,s);r&&(t[o]=r)}return t}async function I(n,e,t){const o=n.on,s=o[e]||[],r=b(t)?t:[t];r.forEach(n=>{s.push(n)}),o[e]=s,await G(n,e,r)}function B(n,e,t,o,s){if(!e.on)return;const r=e.type||"unknown",i=n.logger.scope(r).scope("on").scope(o),a={collector:n,logger:i,id:t,config:e.config,data:s,env:W(e.env,e.config.env)};y(e.on)(o,a)}async function G(n,e,o,s){let r,i=o||[];switch(o||(i=n.on[e]||[]),e){case t.Commands.Consent:r=s||n.consent;break;case t.Commands.Session:r=n.session;break;case t.Commands.User:r=s||n.user;break;case t.Commands.Custom:r=s||n.custom;break;case t.Commands.Globals:r=s||n.globals;break;case t.Commands.Config:r=s||n.config;break;case t.Commands.Ready:case t.Commands.Run:default:r=void 0}let a=!1;for(const t of Object.values(n.sources))if(t.on){!1===await v(t.on)(e,r)&&(a=!0)}if(Object.entries(n.destinations).forEach(([t,o])=>{if(o.on){if(!o.config.init)return o.queueOn=o.queueOn||[],void o.queueOn.push({type:e,data:r});B(n,o,t,e,r)}}),(Object.keys(n.pending.sources).length>0||Object.keys(n.pending.destinations).length>0)&&await async function(n,e){for(const[t,o]of Object.entries(n.pending.sources)){if(!n.pending.sources[t]||n.sources[t])continue;const s=o.config?.require;if(!s)continue;const r=s.indexOf(e);if(-1===r)continue;if(s.splice(r,1),s.length>0)continue;delete n.pending.sources[t];const i=await R(n,t,o);i&&(n.sources[t]=i)}for(const[t,o]of Object.entries(n.pending.destinations)){if(!n.pending.destinations[t]||n.destinations[t])continue;const s=o.config?.require;if(!s)continue;const r=s.indexOf(e);if(-1===r)continue;if(s.splice(r,1),s.length>0)continue;delete n.pending.destinations[t];const i=M(o);!1!==i.config.queue&&(i.queuePush=[...n.queue]),n.destinations[t]=i}}(n,e),!i.length)return!a;switch(e){case t.Commands.Consent:!function(n,e,t){const o=t||n.consent;e.forEach(e=>{Object.keys(o).filter(n=>n in e).forEach(t=>{y(e[t])(n,o)})})}(n,i,s);break;case t.Commands.Ready:case t.Commands.Run:!function(n,e){n.allowed&&e.forEach(e=>{y(e)(n)})}(n,i);break;case t.Commands.Session:!function(n,e){if(!n.session)return;e.forEach(e=>{y(e)(n,n.session)})}(n,i);break;default:i.forEach(e=>{"function"==typeof e&&y(e)(n,r)})}return!a}async function H(n,e,t){const{code:o,config:s={},env:r={},before:i}=e;if(!d(o.push))return J({ok:!1,failed:{invalid:{type:"invalid",error:"Destination code must have a push method"}}});const a=t||s||{init:!1},c=i?{...a,before:i}:a,u={...o,config:c,env:W(o.env,r)};let l=u.config.id;if(!l)do{l=f(4)}while(n.destinations[l]);return n.destinations[l]=u,!1!==u.config.queue&&(u.queuePush=[...n.queue]),U(n,void 0,{},{[l]:u})}async function U(n,e,t={},o){const{allowed:s,consent:r,globals:i,user:u}=n;if(!s)return J({ok:!1});e&&(n.queue.push(e),n.status.in++),o||(o=n.destinations);const f=await Promise.all(Object.entries(o||{}).map(async([o,s])=>{let f=(s.queuePush||[]).map(n=>({...n,consent:r}));if(s.queuePush=[],e){const n=c(e);f.push(n)}if(!f.length&&!s.queueOn?.length)return{id:o,destination:s,skipped:!0};if(!f.length&&s.queueOn?.length){const e=await h(z)(n,s,o);return{id:o,destination:s,skipped:!e}}const g=[],d=f.filter(n=>{const e=l(s.config.consent,r,n.consent);return!e||(n.consent=e,g.push(n),!1)});if(s.queuePush.push(...d),!g.length)return{id:o,destination:s,queue:f};if(!await h(z)(n,s,o))return{id:o,destination:s,queue:f};let m,p;s.dlq||(s.dlq=[]);const w=function(n,e){const t=n.config.before;return t?E(t,D(e)):[]}(s,n.transformers);let b=0;return await Promise.all(g.map(async e=>{e.globals=a(i,e.globals),e.user=a(u,e.user);let r=e;if(w.length>0&&n.transformers&&Object.keys(n.transformers).length>0){const o=await S(n,n.transformers,w,e,t.ingest,t.respond);if(null===o)return e;r=o}const c=Date.now(),f=await h(F,e=>{const t=s.type||"unknown";n.logger.scope(t).error("Push failed",{error:e,event:r.name}),m=e,s.dlq.push([r,e])})(n,s,o,r,t.ingest,t.respond);return b+=Date.now()-c,void 0!==f&&(p=f),e})),{id:o,destination:s,error:m,response:p,totalDuration:b}})),g={},d={},m={};for(const e of f){if(e.skipped)continue;const t={type:e.destination.type||"unknown",data:e.response};n.status.destinations[e.id]||(n.status.destinations[e.id]={count:0,failed:0,duration:0});const o=n.status.destinations[e.id],s=Date.now();e.error?(t.error=e.error,m[e.id]=t,o.failed++,o.lastAt=s,o.duration+=e.totalDuration||0,n.status.failed++):e.queue&&e.queue.length?d[e.id]=t:(g[e.id]=t,o.count++,o.lastAt=s,o.duration+=e.totalDuration||0,n.status.out++)}return J({event:e,...Object.keys(g).length&&{done:g},...Object.keys(d).length&&{queued:d},...Object.keys(m).length&&{failed:m}})}async function z(n,e,t){if(e.init&&!e.config.init){const o=e.type||"unknown",s=n.logger.scope(o),r={collector:n,logger:s,id:t,config:e.config,env:W(e.env,e.config.env)};s.debug("init");const i=await w(e.init,"DestinationInit",n.hooks)(r);if(!1===i)return i;if(e.config={...i||e.config,init:!0},e.queueOn?.length){const o=e.queueOn;e.queueOn=[];for(const{type:s,data:r}of o)B(n,e,t,s,r)}s.debug("init done")}return!0}async function F(n,e,t,o,s,r){const{config:i}=e,a=await p(o,i,n);if(a.ignore)return!1;const c=e.type||"unknown",f=n.logger.scope(c),l={collector:n,logger:f,id:t,config:i,data:a.data,rule:a.mapping,ingest:s,env:{...W(e.env,i.env),...r?{respond:r}:{}}},d=a.mapping,m=a.mappingKey||"* *";if(!d?.batch||!e.pushBatch){f.debug("push",{event:a.event.name});const t=await w(e.push,"DestinationPush",n.hooks)(a.event,l);return f.debug("push done"),t}{if(e.batches=e.batches||{},!e.batches[m]){const o={key:m,events:[],data:[]};e.batches[m]={batched:o,batchFn:u(()=>{const o=e.batches[m].batched,a={collector:n,logger:f,id:t,config:i,data:void 0,rule:d,ingest:s,env:{...W(e.env,i.env),...r?{respond:r}:{}}};f.debug("push batch",{events:o.events.length}),w(e.pushBatch,"DestinationPushBatch",n.hooks)(o,a),f.debug("push batch done"),o.events=[],o.data=[]},d.batch)}}const o=e.batches[m];o.batched.events.push(a.event),g(a.data)&&o.batched.data.push(a.data),o.batchFn()}return!0}function J(n){return{ok:!n?.failed,...n}}function M(n){const{code:e,config:t={},env:o={}}=n,{config:s}=A(n,"before"),r={...e.config,...t,...s},i=W(e.env,o);return{...e,config:r,env:i}}async function N(n,e={}){const t={};for(const[o,s]of Object.entries(e))s.config?.require?.length?n.pending.destinations[o]=s:t[o]=M(s);return t}function W(n,e){return n||e?e?n&&m(n)&&m(e)?{...n,...e}:e:n:{}}import{assign as L,getId as Q,isFunction as V,isString as _}from"@walkeros/core";import{isObject as K}from"@walkeros/core";async function X(n,e,t){const o=Object.entries(n).map(async([n,o])=>{const s=o.destroy;if(!s)return;const r=o.type||"unknown",i=t.scope(r),a={id:n,config:o.config,env:o.env??{},logger:i};try{await Promise.race([s(a),new Promise((t,o)=>setTimeout(()=>o(new Error(`${e} '${n}' destroy timed out`)),5e3))])}catch(t){i.error(`${e} '${n}' destroy failed: ${t}`)}});await Promise.allSettled(o)}async function Y(n,e,o,r){let i,a,c=!1,u=!1;switch(e){case t.Commands.Config:K(o)&&(L(n.config,o,{shallow:!1}),a=o,c=!0);break;case t.Commands.Consent:if(K(o)){const{update:e,runQueue:t}=s(n,o);a=e,c=!0,u=t}break;case t.Commands.Custom:K(o)&&(n.custom=L(n.custom,o),a=o,c=!0);break;case t.Commands.Destination:K(o)&&("code"in o&&K(o.code)?i=await H(n,o,r):V(o.push)&&(i=await H(n,{code:o},r)));break;case t.Commands.Globals:K(o)&&(n.globals=L(n.globals,o),a=o,c=!0);break;case t.Commands.On:_(o)&&await I(n,o,r);break;case t.Commands.Ready:c=!0;break;case t.Commands.Run:i=await nn(n,o),c=!0;break;case t.Commands.Session:c=!0;break;case t.Commands.Shutdown:await async function(n){const e=n.logger;await X(n.sources,"source",e),await X(n.destinations,"destination",e),await X(n.transformers,"transformer",e),await X(n.stores,"store",e)}(n);break;case t.Commands.User:K(o)&&(L(n.user,o,{shallow:!1}),a=o,c=!0)}return c&&await G(n,e,void 0,a),u&&(i=await U(n)),i||J({ok:!0})}function Z(n,e){if(!e.name)throw new Error("Event name is required");const[t,o]=e.name.split(" ");if(!t||!o)throw new Error("Event name is invalid");++n.count;const{timestamp:s=Date.now(),group:r=n.group,count:i=n.count}=e,{name:a=`${t} ${o}`,data:c={},context:u={},globals:f=n.globals,custom:l={},user:g=n.user,nested:d=[],consent:m=n.consent,id:p=`${s}-${r}-${i}`,trigger:h="",entity:w=t,action:b=o,timing:y=0,version:v={source:n.version,tagging:n.config.tagging||0},source:k={type:"collector",id:"",previous_id:""}}=e;return{name:a,data:c,context:u,globals:f,custom:l,user:g,nested:d,consent:m,id:p,trigger:h,entity:w,action:b,timestamp:s,timing:y,group:r,count:i,version:v,source:k}}async function nn(n,e){n.allowed=!0,n.count=0,n.group=Q(),n.timing=Date.now(),e&&(e.consent&&(n.consent=L(n.consent,e.consent)),e.user&&(n.user=L(n.user,e.user)),e.globals&&(n.globals=L(n.config.globalsStatic||{},e.globals)),e.custom&&(n.custom=L(n.custom,e.custom))),Object.values(n.destinations).forEach(n=>{n.queuePush=[]}),n.queue=[],n.round++;return await U(n)}import{getGrantedConsent as en,processEventMapping as tn,tryCatchAsync as on,useHooks as sn}from"@walkeros/core";function rn(n,e){return sn(async(t,o={})=>await on(async()=>{const s=Date.now(),{id:r,ingest:i,respond:a,mapping:c,preChain:u}=o;let f=t;const l=i?Object.freeze(i):void 0;if(c){const e=await tn(f,c,n);if(e.ignore)return J({ok:!0});if(c.consent){if(!en(c.consent,n.consent,e.event.consent))return J({ok:!0})}f=e.event}if(u?.length&&n.transformers&&Object.keys(n.transformers).length>0){const e=await S(n,n.transformers,u,f,l,a);if(null===e)return J({ok:!0});f=e}const g=e(f),d=Z(n,g),m=await U(n,d,{id:r,ingest:l,respond:a});if(r){n.status.sources[r]||(n.status.sources[r]={count:0,duration:0});const e=n.status.sources[r];e.count++,e.lastAt=Date.now(),e.duration+=Date.now()-s}return m},()=>J({ok:!1}))(),"Push",n.hooks)}import{useHooks as an,tryCatchAsync as cn}from"@walkeros/core";async function un(n){const e=r({globalsStatic:{},sessionStatic:{},tagging:0,run:!0},n,{merge:!1,extend:!1}),t={level:n.logger?.level,handler:n.logger?.handler},o=i(t),s={...e.globalsStatic,...n.globals},a={allowed:!1,config:e,consent:n.consent||{},count:0,custom:n.custom||{},destinations:{},transformers:{},stores:{},globals:s,group:"",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:n.user||{},version:"3.0.0",sources:{},pending:{sources:{},destinations:{}},push:void 0,command:void 0};a.push=rn(a,n=>({timing:Math.round((Date.now()-a.timing)/10)/100,source:{type:"collector",id:"",previous_id:""},...n})),a.command=function(n,e){return an(async(t,o,s)=>await cn(async()=>await e(n,t,o,s),()=>J({ok:!1}))(),"Command",n.hooks)}(a,Y);const c=n.stores||{};return a.stores=await async function(n,e={}){const t={};for(const[o,s]of Object.entries(e)){const{code:e,config:r={},env:i={}}=s,a=n.logger.scope("store").scope(o),c={collector:n,logger:a,id:o,config:r,env:i},u=await e(c);t[o]=u}return t}(a,c),function(n,e,t){const o=new Map;for(const[t,s]of Object.entries(n))e[t]&&o.set(s,e[t]);if(0!==o.size)for(const n of[t.transformers,t.destinations,t.sources])if(n)for(const e of Object.values(n))s(e.env);function s(n){if(n)for(const[e,t]of Object.entries(n))if("object"==typeof t&&null!==t){const s=o.get(t);s&&(n[e]=s)}}}(c,a.stores,n),a.destinations=await N(a,n.destinations||{}),a.transformers=await async function(n,e={}){const t={};for(const[o,s]of Object.entries(e)){const{code:e,env:r={}}=s,{config:i}=A(s,"next"),a=Object.keys(r).length>0?{...i,env:r}:i,c=n.logger.scope("transformer").scope(o),u={collector:n,logger:c,id:o,config:a,env:r},f=await e(u);t[o]=f}return t}(a,n.transformers||{}),a}async function fn(n){n=n||{};const e=await un(n),t=(o=e,{type:"elb",config:{},push:async(n,e,t,s,r,i)=>{if("string"==typeof n&&n.startsWith("walker ")){const s=n.replace("walker ","");return o.command(s,e,t)}let a;if("string"==typeof n)a={name:n},e&&"object"==typeof e&&!Array.isArray(e)&&(a.data=e);else{if(!n||"object"!=typeof n)return J({ok:!1});a=n,e&&"object"==typeof e&&!Array.isArray(e)&&(a.data={...a.data||{},...e})}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;e.sources.elb=t;const s=await T(e,n.sources||{});Object.assign(e.sources,s);const{consent:r,user:i,globals:a,custom:c}=n;r&&await e.command("consent",r),i&&await e.command("user",i),a&&Object.assign(e.globals,a),c&&Object.assign(e.custom,c),e.config.run&&await e.command("run");let u=t.push;const f=Object.values(e.sources).filter(n=>"elb"!==n.type),l=f.find(n=>n.config.primary);return l?u=l.push:f.length>0&&(u=f[0].push),{collector:e,elb:u}}function ln(n){if(null===n||"object"!=typeof n)return n;if(Array.isArray(n))return n.map(ln);const e={};for(const[t,o]of Object.entries(n))e[t]="function"==typeof o?o:ln(o);return e}function gn(n){const e=[],{simulation:t,...o}=n,s=ln(o);for(const n of t){const t=n.startsWith("call:")?n.slice(5):n,o=t.split(".");let r=s;for(let n=0;n<o.length-1&&null!=r[o[n]];n++)r=r[o[n]];const i=o[o.length-1];if(null==r||!(i in r))continue;const a=r[i];"function"==typeof a&&(r[i]=function(...n){return e.push({fn:t,args:n,ts:Date.now()}),a.apply(this,n)})}return{wrappedEnv:s,calls:e}}async function dn(n){const e=Date.now();try{switch(n.step){case"transformer":return await async function(n,e){const{code:t,config:o={},event:s}=n,{collector:r}=await fn({transformers:{sim:{code:t,config:o}}}),i=r.transformers?.sim;if(!i)throw new Error("Transformer failed to initialize");const a=await i.push(s,{collector:r,logger:r.logger.scope("transformer").scope("sim"),id:"sim",config:i.config,env:i.config?.env||{}});let c;c=!1===a?[]:null==a?[s]:[a.event||s];return{step:"transformer",name:n.name,events:c,calls:[],duration:Date.now()-e}}(n,e);case"source":return await async function(n,e){const{code:t,config:o={},setup:s,input:r,env:i,consent:a}=n,c={functional:!0,marketing:!0,analytics:!0};let u;if(s){const n=s(r,i);"function"==typeof n&&(u=n)}const f=[],{collector:l}=await fn({consent:a||c,sources:{sim:{code:t,config:o,env:i,next:"spy"}},transformers:{spy:{code:()=>({type:"spy",config:{},push:n=>(f.push(JSON.parse(JSON.stringify(n))),{event:n})})}}});u&&u();return{step:"source",name:n.name,events:f,calls:[],duration:Date.now()-e}}(n,e);case"destination":return await async function(n,e){const{code:t,config:o={},event:s,consent:r,env:i,track:a}=n,c={functional:!0,marketing:!0,analytics:!0};let u=[],f=i;if(i&&a&&a.length>0){const n=gn({...i,simulation:a});f=n.wrappedEnv,u=n.calls}const l={...o};f&&(l.env=f);const{collector:g}=await fn({consent:r||c,destinations:{sim:{code:t,config:l}}});return await g.push(s),{step:"destination",name:n.name,events:[],calls:u,duration:Date.now()-e}}(n,e)}}catch(t){return{step:n.step,name:n.name,events:[],calls:[],duration:Date.now()-e,error:t instanceof Error?t:new Error(String(t))}}}export{n as Code,e as Commands,t as Const,H as addDestination,B as callDestinationOn,Y as commonHandleCommand,Z as createEvent,rn as createPush,J as createPushResult,z as destinationInit,F as destinationPush,D as extractTransformerNextMap,N as initDestinations,R as initSource,T as initSources,W as mergeEnvironments,I as on,G as onApply,s as processConsent,U as pushToDestinations,M as registerDestination,nn as runCollector,dn as simulate,fn as startFlow,E as walkChain,gn 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.0.
|
|
4
|
+
"version": "3.0.1",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
@@ -60,6 +60,6 @@
|
|
|
60
60
|
}
|
|
61
61
|
],
|
|
62
62
|
"dependencies": {
|
|
63
|
-
"@walkeros/core": "^3.0.
|
|
63
|
+
"@walkeros/core": "^3.0.1"
|
|
64
64
|
}
|
|
65
65
|
}
|