@walkeros/collector 2.0.0 → 2.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 +5 -5
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,i={};((e,t)=>{for(var o in t)n(e,o,{get:t[o],enumerable:!0})})(i,{Code:()=>r,Commands:()=>a,Const:()=>c,addDestination:()=>E,callDestinationOn:()=>P,commonHandleCommand:()=>G,createEvent:()=>B,createPush:()=>N,createPushResult:()=>R,destinationInit:()=>$,destinationPush:()=>H,extractTransformerNextMap:()=>y,initDestinations:()=>M,initSource:()=>j,initSources:()=>A,mergeEnvironments:()=>T,on:()=>D,onApply:()=>x,processConsent:()=>l,pushToDestinations:()=>S,registerDestination:()=>I,runCollector:()=>U,startFlow:()=>W,walkChain:()=>k}),module.exports=(e=i,((e,i,r,a)=>{if(i&&"object"==typeof i||"function"==typeof i)for(let c of o(i))s.call(e,c)||c===r||n(e,c,{get:()=>i[c],enumerable:!(a=t(i,c))||a.enumerable});return e})(n({},"__esModule",{value:!0}),e));var r={},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",User:"user",Walker:"walker"},c={Commands:a,Utils:{Storage:{Cookie:"cookie",Local:"local",Session:"session"}}},u=require("@walkeros/core");function l(e,n){let t=!1;const o={};return Object.entries(n).forEach(([e,n])=>{const s=!!n;o[e]=s,t=t||s}),e.consent=(0,u.assign)(e.consent,o),{update:o,runQueue:t}}var g=require("@walkeros/core"),f=require("@walkeros/core"),d=require("@walkeros/core"),m=require("@walkeros/core"),h=require("@walkeros/core"),p=require("@walkeros/core");function b(e){return null!=e&&!1!==e&&"object"==typeof e&&!0===e.__branch}function y(e){const n={};for(const[t,o]of Object.entries(e))o.config?.next?n[t]={next:o.config.next}:n[t]={};return n}function w(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 v(e,n,t){if(n.init&&!n.config.init){const o=n.type||"unknown",s=e.logger.scope(`transformer:${o}`),i={collector:e,logger:s,id:t,config:n.config,env:q(n.config.env)};s.debug("init");const r=await(0,p.useHooks)(n.init,"TransformerInit",e.hooks)(i);if(!1===r)return!1;n.config={...r||n.config,init:!0},s.debug("init done")}return!0}async function C(e,n,t,o,s){const i=n.type||"unknown",r=e.logger.scope(`transformer:${i}`),a={collector:e,logger:r,id:t,ingest:s,config:n.config,env:q(n.config.env)};r.debug("push",{event:o.name});const c=await(0,p.useHooks)(n.push,"TransformerPush",e.hooks)(o,a);return r.debug("push done"),c}async function O(e,n,t,o,s){let i=o;for(const o of t){const t=n[o];if(!t){e.logger.info(`Transformer not found: ${o}`);continue}if(!await(0,p.tryCatchAsync)(v)(e,t,o))return e.logger.info(`Transformer init failed: ${o}`),null;const r=await(0,p.tryCatchAsync)(C,n=>(e.logger.scope(`transformer:${t.type||"unknown"}`).error("Push failed",{error:n}),!1))(e,t,o,i,s);if(!1===r)return null;if(b(r)){const t=k(r.next,y(n));return t.length>0?O(e,n,t,r.event,s):(e.logger.info(`Branch target not found: ${JSON.stringify(r.next)}`),null)}void 0!==r&&(i=r)}return i}function q(e){return e&&(0,p.isObject)(e)?e:{}}async function j(e,n,t){const{code:o,config:s={},env:i={},primary:r,next:a}=t;let c;const u=k(a,y(e.transformers)),l=e.logger.scope("source").scope(n),g={push:(t,o={})=>e.push(t,{...o,id:n,ingest:c,mapping:s,preChain:u}),command:e.command,sources:e.sources,elb:e.sources.elb.push,logger:l,...i},f={collector:e,logger:l,id:n,config:s,env:g,setIngest:async n=>{c=s.ingest?await(0,h.getMappingValue)(n,s.ingest,{collector:e}):void 0}},d=await(0,h.tryCatchAsync)(o)(f);if(!d)return;const m=d.type||"unknown",p=e.logger.scope(m).scope(n);return g.logger=p,r&&(d.config={...d.config,primary:r}),d}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 i=await j(e,o,s);i&&(t[o]=i)}return t}async function D(e,n,t){const o=e.on,s=o[n]||[],i=(0,d.isArray)(t)?t:[t];i.forEach(e=>{s.push(e)}),o[n]=s,await x(e,n,i)}function P(e,n,t,o,s){if(!n.on)return;const i=n.type||"unknown",r=e.logger.scope(i).scope("on").scope(o),a={collector:e,logger:r,id:t,config:n.config,data:s,env:T(n.env,n.config.env)};(0,m.tryCatch)(n.on)(o,a)}async function x(e,n,t,o){let s,i=t||[];switch(t||(i=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 r=!1;for(const t of Object.values(e.sources))if(t.on){!1===await(0,m.tryCatchAsync)(t.on)(n,s)&&(r=!0)}if(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});P(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 i=s.indexOf(n);if(-1===i)continue;if(s.splice(i,1),s.length>0)continue;delete e.pending.sources[t];const r=await j(e,t,o);r&&(e.sources[t]=r)}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 i=s.indexOf(n);if(-1===i)continue;if(s.splice(i,1),s.length>0)continue;delete e.pending.destinations[t];const r=I(o);!1!==r.config.queue&&(r.queuePush=[...e.queue]),e.destinations[t]=r}}(e,n),!i.length)return!r;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,m.tryCatch)(n[t])(e,o)})})}(e,i,o);break;case c.Commands.Ready:case c.Commands.Run:!function(e,n){e.allowed&&n.forEach(n=>{(0,m.tryCatch)(n)(e)})}(e,i);break;case c.Commands.Session:!function(e,n){if(!e.session)return;n.forEach(n=>{(0,m.tryCatch)(n)(e,e.session)})}(e,i);break;default:i.forEach(n=>{"function"==typeof n&&(0,m.tryCatch)(n)(e,s)})}return!r}async function E(e,n,t){const{code:o,config:s={},env:i={},before:r}=n;if(!(0,f.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=r?{...a,before:r}:a,u={...o,config:c,env:T(o.env,i)};let l=u.config.id;if(!l)do{l=(0,f.getId)(4)}while(e.destinations[l]);return e.destinations[l]=u,!1!==u.config.queue&&(u.queuePush=[...e.queue]),S(e,void 0,{},{[l]:u})}async function S(e,n,t={},o){const{allowed:s,consent:i,globals:r,user:a}=e;if(!s)return R({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])=>{let c=(s.queuePush||[]).map(e=>({...e,consent:i}));if(s.queuePush=[],n){const e=(0,f.clone)(n);c.push(e)}if(!c.length&&!s.queueOn?.length)return{id:o,destination:s,skipped:!0};if(!c.length&&s.queueOn?.length){const n=await(0,f.tryCatchAsync)($)(e,s,o);return{id:o,destination:s,skipped:!n}}const u=[],l=c.filter(e=>{const n=(0,f.getGrantedConsent)(s.config.consent,i,e.consent);return!n||(e.consent=n,u.push(e),!1)});if(s.queuePush.push(...l),!u.length)return{id:o,destination:s,queue:c};if(!await(0,f.tryCatchAsync)($)(e,s,o))return{id:o,destination:s,queue:c};let g,d;s.dlq||(s.dlq=[]);const m=function(e,n){const t=e.config.before;return t?k(t,y(n)):[]}(s,e.transformers);let h=0;return await Promise.all(u.map(async n=>{n.globals=(0,f.assign)(r,n.globals),n.user=(0,f.assign)(a,n.user);let i=n;if(m.length>0&&e.transformers&&Object.keys(e.transformers).length>0){const o=await O(e,e.transformers,m,n,t.ingest);if(null===o)return n;i=o}const c=Date.now(),u=await(0,f.tryCatchAsync)(H,n=>{const t=s.type||"unknown";e.logger.scope(t).error("Push failed",{error:n,event:i.name}),g=n,s.dlq.push([i,n])})(e,s,o,i,t.ingest);return h+=Date.now()-c,void 0!==u&&(d=u),n})),{id:o,destination:s,error:g,response:d,totalDuration:h}})),u={},l={},g={};for(const n of c){if(n.skipped)continue;const t=n.destination,o={type:t.type||"unknown",data:n.response};e.status.destinations[n.id]||(e.status.destinations[n.id]={count:0,failed:0,duration:0});const s=e.status.destinations[n.id],i=Date.now();n.error?(o.error=n.error,g[n.id]=o,s.failed++,s.lastAt=i,s.duration+=n.totalDuration||0,e.status.failed++):n.queue&&n.queue.length?(t.queuePush=(t.queuePush||[]).concat(n.queue),l[n.id]=o):(u[n.id]=o,s.count++,s.lastAt=i,s.duration+=n.totalDuration||0,e.status.out++)}return R({event:n,...Object.keys(u).length&&{done:u},...Object.keys(l).length&&{queued:l},...Object.keys(g).length&&{failed:g}})}async function $(e,n,t){if(n.init&&!n.config.init){const o=n.type||"unknown",s=e.logger.scope(o),i={collector:e,logger:s,id:t,config:n.config,env:T(n.env,n.config.env)};s.debug("init");const r=await(0,f.useHooks)(n.init,"DestinationInit",e.hooks)(i);if(!1===r)return r;if(n.config={...r||n.config,init:!0},n.queueOn?.length){const o=n.queueOn;n.queueOn=[];for(const{type:s,data:i}of o)P(e,n,t,s,i)}s.debug("init done")}return!0}async function H(e,n,t,o,s){const{config:i}=n,r=await(0,f.processEventMapping)(o,i,e);if(r.ignore)return!1;const a=n.type||"unknown",c=e.logger.scope(a),u={collector:e,logger:c,id:t,config:i,data:r.data,rule:r.mapping,ingest:s,env:T(n.env,i.env)},l=r.mapping,g=r.mappingKey||"* *";if(!l?.batch||!n.pushBatch){c.debug("push",{event:r.event.name});const t=await(0,f.useHooks)(n.push,"DestinationPush",e.hooks)(r.event,u);return c.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,f.debounce)(()=>{const o=n.batches[g].batched,r={collector:e,logger:c,id:t,config:i,data:void 0,rule:l,ingest:s,env:T(n.env,i.env)};c.debug("push batch",{events:o.events.length}),(0,f.useHooks)(n.pushBatch,"DestinationPushBatch",e.hooks)(o,r),c.debug("push batch done"),o.events=[],o.data=[]},l.batch)}}const o=n.batches[g];o.batched.events.push(r.event),(0,f.isDefined)(r.data)&&o.batched.data.push(r.data),o.batchFn()}return!0}function R(e){return{ok:!e?.failed,...e}}function I(e){const{code:n,config:t={},env:o={}}=e,{config:s}=w(e,"before"),i={...n.config,...t,...s},r=T(n.env,o);return{...n,config:i,env:r}}async function M(e,n={}){const t={};for(const[o,s]of Object.entries(n))s.config?.require?.length?e.pending.destinations[o]=s:t[o]=I(s);return t}function T(e,n){return e||n?n?e&&(0,f.isObject)(e)&&(0,f.isObject)(n)?{...e,...n}:n:e:{}}var _=require("@walkeros/core"),F=require("@walkeros/core");async function G(e,n,t,o){let s,i,r=!1,a=!1;switch(n){case c.Commands.Config:(0,F.isObject)(t)&&((0,_.assign)(e.config,t,{shallow:!1}),i=t,r=!0);break;case c.Commands.Consent:if((0,F.isObject)(t)){const{update:n,runQueue:o}=l(e,t);i=n,r=!0,a=o}break;case c.Commands.Custom:(0,F.isObject)(t)&&(e.custom=(0,_.assign)(e.custom,t),i=t,r=!0);break;case c.Commands.Destination:(0,F.isObject)(t)&&("code"in t&&(0,F.isObject)(t.code)?s=await E(e,t,o):(0,_.isFunction)(t.push)&&(s=await E(e,{code:t},o)));break;case c.Commands.Globals:(0,F.isObject)(t)&&(e.globals=(0,_.assign)(e.globals,t),i=t,r=!0);break;case c.Commands.On:(0,_.isString)(t)&&await D(e,t,o);break;case c.Commands.Ready:r=!0;break;case c.Commands.Run:s=await U(e,t),r=!0;break;case c.Commands.Session:r=!0;break;case c.Commands.User:(0,F.isObject)(t)&&((0,_.assign)(e.user,t,{shallow:!1}),i=t,r=!0)}return r&&await x(e,n,void 0,i),a&&(s=await S(e)),s||R({ok:!0})}function B(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:i=e.group,count:r=e.count}=n,{name:a=`${t} ${o}`,data:c={},context:u={},globals:l=e.globals,custom:g={},user:f=e.user,nested:d=[],consent:m=e.consent,id:h=`${s}-${i}-${r}`,trigger:p="",entity:b=t,action:y=o,timing:w=0,version:k={source:e.version,tagging:e.config.tagging||0},source:v={type:"collector",id:"",previous_id:""}}=n;return{name:a,data:c,context:u,globals:l,custom:g,user:f,nested:d,consent:m,id:h,trigger:p,entity:b,action:y,timestamp:s,timing:w,group:i,count:r,version:k,source:v}}async function U(e,n){e.allowed=!0,e.count=0,e.group=(0,_.getId)(),e.timing=Date.now(),n&&(n.consent&&(e.consent=(0,_.assign)(e.consent,n.consent)),n.user&&(e.user=(0,_.assign)(e.user,n.user)),n.globals&&(e.globals=(0,_.assign)(e.config.globalsStatic||{},n.globals)),n.custom&&(e.custom=(0,_.assign)(e.custom,n.custom))),Object.values(e.destinations).forEach(e=>{e.queuePush=[]}),e.queue=[],e.round++;return await S(e)}var L=require("@walkeros/core");function N(e,n){return(0,L.useHooks)(async(t,o={})=>await(0,L.tryCatchAsync)(async()=>{const s=Date.now(),{id:i,ingest:r,mapping:a,preChain:c}=o;let u=t;const l=r?Object.freeze(r):void 0;if(a){const n=await(0,L.processEventMapping)(u,a,e);if(n.ignore)return R({ok:!0});if(a.consent){if(!(0,L.getGrantedConsent)(a.consent,e.consent,n.event.consent))return R({ok:!0})}u=n.event}if(c?.length&&e.transformers&&Object.keys(e.transformers).length>0){const n=await O(e,e.transformers,c,u,l);if(null===n)return R({ok:!0});u=n}const g=n(u),f=B(e,g),d=await S(e,f,{id:i,ingest:l});if(i){e.status.sources[i]||(e.status.sources[i]={count:0,duration:0});const n=e.status.sources[i];n.count++,n.lastAt=Date.now(),n.duration+=Date.now()-s}return d},()=>R({ok:!1}))(),"Push",e.hooks)}var V=require("@walkeros/core");async function Q(e){const n=(0,g.assign)({globalsStatic:{},sessionStatic:{},tagging:0,run:!0},e,{merge:!1,extend:!1}),t={level:e.logger?.level,handler:e.logger?.handler},o=(0,g.createLogger)(t),s={...n.globalsStatic,...e.globals},i={allowed:!1,config:n,consent:e.consent||{},count:0,custom:e.custom||{},destinations:{},transformers:{},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:e.user||{},version:"1.2.0",sources:{},pending:{sources:{},destinations:{}},push:void 0,command:void 0};return i.push=N(i,e=>({timing:Math.round((Date.now()-i.timing)/10)/100,source:{type:"collector",id:"",previous_id:""},...e})),i.command=function(e,n){return(0,V.useHooks)(async(t,o,s)=>await(0,V.tryCatchAsync)(async()=>await n(e,t,o,s),()=>R({ok:!1}))(),"Command",e.hooks)}(i,G),i.destinations=await M(i,e.destinations||{}),i.transformers=await async function(e,n={}){const t={};for(const[o,s]of Object.entries(n)){const{code:n,env:i={}}=s,{config:r}=w(s,"next"),a=e.logger.scope("transformer").scope(o),c={collector:e,logger:a,id:o,config:r,env:i},u=await n(c);t[o]=u}return t}(i,e.transformers||{}),i}async function W(e){e=e||{};const n=await Q(e),t=(o=n,{type:"elb",config:{},push:async(e,n,t,s,i,r)=>{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 R({ok:!1});a=e,n&&"object"==typeof n&&!Array.isArray(n)&&(a.data={...a.data||{},...n})}return s&&"object"==typeof s&&(a.context=s),i&&Array.isArray(i)&&(a.nested=i),r&&"object"==typeof r&&(a.custom=r),o.push(a)}});var o;n.sources.elb=t;const s=await A(n,e.sources||{});Object.assign(n.sources,s);const{consent:i,user:r,globals:a,custom:c}=e;i&&await n.command("consent",i),r&&await n.command("user",r),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),g=l.find(e=>e.config.primary);return g?u=g.push:l.length>0&&(u=l[0].push),{collector:n,elb:u}}//# sourceMappingURL=index.js.map
|
|
1
|
+
"use strict";var e,n=Object.defineProperty,t=Object.getOwnPropertyDescriptor,o=Object.getOwnPropertyNames,s=Object.prototype.hasOwnProperty,i={};((e,t)=>{for(var o in t)n(e,o,{get:t[o],enumerable:!0})})(i,{Code:()=>r,Commands:()=>a,Const:()=>c,addDestination:()=>E,callDestinationOn:()=>P,commonHandleCommand:()=>G,createEvent:()=>B,createPush:()=>N,createPushResult:()=>R,destinationInit:()=>$,destinationPush:()=>H,extractTransformerNextMap:()=>y,initDestinations:()=>M,initSource:()=>j,initSources:()=>A,mergeEnvironments:()=>T,on:()=>D,onApply:()=>x,processConsent:()=>l,pushToDestinations:()=>S,registerDestination:()=>I,runCollector:()=>U,startFlow:()=>W,walkChain:()=>k}),module.exports=(e=i,((e,i,r,a)=>{if(i&&"object"==typeof i||"function"==typeof i)for(let c of o(i))s.call(e,c)||c===r||n(e,c,{get:()=>i[c],enumerable:!(a=t(i,c))||a.enumerable});return e})(n({},"__esModule",{value:!0}),e));var r={},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",User:"user",Walker:"walker"},c={Commands:a,Utils:{Storage:{Cookie:"cookie",Local:"local",Session:"session"}}},u=require("@walkeros/core");function l(e,n){let t=!1;const o={};return Object.entries(n).forEach(([e,n])=>{const s=!!n;o[e]=s,t=t||s}),e.consent=(0,u.assign)(e.consent,o),{update:o,runQueue:t}}var g=require("@walkeros/core"),f=require("@walkeros/core"),d=require("@walkeros/core"),m=require("@walkeros/core"),h=require("@walkeros/core"),p=require("@walkeros/core");function b(e){return null!=e&&!1!==e&&"object"==typeof e&&!0===e.__branch}function y(e){const n={};for(const[t,o]of Object.entries(e))o.config?.next?n[t]={next:o.config.next}:n[t]={};return n}function w(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 v(e,n,t){if(n.init&&!n.config.init){const o=n.type||"unknown",s=e.logger.scope(`transformer:${o}`),i={collector:e,logger:s,id:t,config:n.config,env:q(n.config.env)};s.debug("init");const r=await(0,p.useHooks)(n.init,"TransformerInit",e.hooks)(i);if(!1===r)return!1;n.config={...r||n.config,init:!0},s.debug("init done")}return!0}async function C(e,n,t,o,s){const i=n.type||"unknown",r=e.logger.scope(`transformer:${i}`),a={collector:e,logger:r,id:t,ingest:s,config:n.config,env:q(n.config.env)};r.debug("push",{event:o.name});const c=await(0,p.useHooks)(n.push,"TransformerPush",e.hooks)(o,a);return r.debug("push done"),c}async function O(e,n,t,o,s){let i=o;for(const o of t){const t=n[o];if(!t){e.logger.info(`Transformer not found: ${o}`);continue}if(!await(0,p.tryCatchAsync)(v)(e,t,o))return e.logger.info(`Transformer init failed: ${o}`),null;const r=await(0,p.tryCatchAsync)(C,n=>(e.logger.scope(`transformer:${t.type||"unknown"}`).error("Push failed",{error:n}),!1))(e,t,o,i,s);if(!1===r)return null;if(b(r)){const t=k(r.next,y(n));return t.length>0?O(e,n,t,r.event,s):(e.logger.info(`Branch target not found: ${JSON.stringify(r.next)}`),null)}void 0!==r&&(i=r)}return i}function q(e){return e&&(0,p.isObject)(e)?e:{}}async function j(e,n,t){const{code:o,config:s={},env:i={},primary:r,next:a}=t;let c;const u=k(a,y(e.transformers)),l=e.logger.scope("source").scope(n),g={push:(t,o={})=>e.push(t,{...o,id:n,ingest:c,mapping:s,preChain:u}),command:e.command,sources:e.sources,elb:e.sources.elb.push,logger:l,...i},f={collector:e,logger:l,id:n,config:s,env:g,setIngest:async n=>{c=s.ingest?await(0,h.getMappingValue)(n,s.ingest,{collector:e}):void 0}},d=await(0,h.tryCatchAsync)(o)(f);if(!d)return;const m=d.type||"unknown",p=e.logger.scope(m).scope(n);return g.logger=p,r&&(d.config={...d.config,primary:r}),d}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 i=await j(e,o,s);i&&(t[o]=i)}return t}async function D(e,n,t){const o=e.on,s=o[n]||[],i=(0,d.isArray)(t)?t:[t];i.forEach(e=>{s.push(e)}),o[n]=s,await x(e,n,i)}function P(e,n,t,o,s){if(!n.on)return;const i=n.type||"unknown",r=e.logger.scope(i).scope("on").scope(o),a={collector:e,logger:r,id:t,config:n.config,data:s,env:T(n.env,n.config.env)};(0,m.tryCatch)(n.on)(o,a)}async function x(e,n,t,o){let s,i=t||[];switch(t||(i=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 r=!1;for(const t of Object.values(e.sources))if(t.on){!1===await(0,m.tryCatchAsync)(t.on)(n,s)&&(r=!0)}if(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});P(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 i=s.indexOf(n);if(-1===i)continue;if(s.splice(i,1),s.length>0)continue;delete e.pending.sources[t];const r=await j(e,t,o);r&&(e.sources[t]=r)}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 i=s.indexOf(n);if(-1===i)continue;if(s.splice(i,1),s.length>0)continue;delete e.pending.destinations[t];const r=I(o);!1!==r.config.queue&&(r.queuePush=[...e.queue]),e.destinations[t]=r}}(e,n),!i.length)return!r;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,m.tryCatch)(n[t])(e,o)})})}(e,i,o);break;case c.Commands.Ready:case c.Commands.Run:!function(e,n){e.allowed&&n.forEach(n=>{(0,m.tryCatch)(n)(e)})}(e,i);break;case c.Commands.Session:!function(e,n){if(!e.session)return;n.forEach(n=>{(0,m.tryCatch)(n)(e,e.session)})}(e,i);break;default:i.forEach(n=>{"function"==typeof n&&(0,m.tryCatch)(n)(e,s)})}return!r}async function E(e,n,t){const{code:o,config:s={},env:i={},before:r}=n;if(!(0,f.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=r?{...a,before:r}:a,u={...o,config:c,env:T(o.env,i)};let l=u.config.id;if(!l)do{l=(0,f.getId)(4)}while(e.destinations[l]);return e.destinations[l]=u,!1!==u.config.queue&&(u.queuePush=[...e.queue]),S(e,void 0,{},{[l]:u})}async function S(e,n,t={},o){const{allowed:s,consent:i,globals:r,user:a}=e;if(!s)return R({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])=>{let c=(s.queuePush||[]).map(e=>({...e,consent:i}));if(s.queuePush=[],n){const e=(0,f.clone)(n);c.push(e)}if(!c.length&&!s.queueOn?.length)return{id:o,destination:s,skipped:!0};if(!c.length&&s.queueOn?.length){const n=await(0,f.tryCatchAsync)($)(e,s,o);return{id:o,destination:s,skipped:!n}}const u=[],l=c.filter(e=>{const n=(0,f.getGrantedConsent)(s.config.consent,i,e.consent);return!n||(e.consent=n,u.push(e),!1)});if(s.queuePush.push(...l),!u.length)return{id:o,destination:s,queue:c};if(!await(0,f.tryCatchAsync)($)(e,s,o))return{id:o,destination:s,queue:c};let g,d;s.dlq||(s.dlq=[]);const m=function(e,n){const t=e.config.before;return t?k(t,y(n)):[]}(s,e.transformers);let h=0;return await Promise.all(u.map(async n=>{n.globals=(0,f.assign)(r,n.globals),n.user=(0,f.assign)(a,n.user);let i=n;if(m.length>0&&e.transformers&&Object.keys(e.transformers).length>0){const o=await O(e,e.transformers,m,n,t.ingest);if(null===o)return n;i=o}const c=Date.now(),u=await(0,f.tryCatchAsync)(H,n=>{const t=s.type||"unknown";e.logger.scope(t).error("Push failed",{error:n,event:i.name}),g=n,s.dlq.push([i,n])})(e,s,o,i,t.ingest);return h+=Date.now()-c,void 0!==u&&(d=u),n})),{id:o,destination:s,error:g,response:d,totalDuration:h}})),u={},l={},g={};for(const n of c){if(n.skipped)continue;const t=n.destination,o={type:t.type||"unknown",data:n.response};e.status.destinations[n.id]||(e.status.destinations[n.id]={count:0,failed:0,duration:0});const s=e.status.destinations[n.id],i=Date.now();n.error?(o.error=n.error,g[n.id]=o,s.failed++,s.lastAt=i,s.duration+=n.totalDuration||0,e.status.failed++):n.queue&&n.queue.length?(t.queuePush=(t.queuePush||[]).concat(n.queue),l[n.id]=o):(u[n.id]=o,s.count++,s.lastAt=i,s.duration+=n.totalDuration||0,e.status.out++)}return R({event:n,...Object.keys(u).length&&{done:u},...Object.keys(l).length&&{queued:l},...Object.keys(g).length&&{failed:g}})}async function $(e,n,t){if(n.init&&!n.config.init){const o=n.type||"unknown",s=e.logger.scope(o),i={collector:e,logger:s,id:t,config:n.config,env:T(n.env,n.config.env)};s.debug("init");const r=await(0,f.useHooks)(n.init,"DestinationInit",e.hooks)(i);if(!1===r)return r;if(n.config={...r||n.config,init:!0},n.queueOn?.length){const o=n.queueOn;n.queueOn=[];for(const{type:s,data:i}of o)P(e,n,t,s,i)}s.debug("init done")}return!0}async function H(e,n,t,o,s){const{config:i}=n,r=await(0,f.processEventMapping)(o,i,e);if(r.ignore)return!1;const a=n.type||"unknown",c=e.logger.scope(a),u={collector:e,logger:c,id:t,config:i,data:r.data,rule:r.mapping,ingest:s,env:T(n.env,i.env)},l=r.mapping,g=r.mappingKey||"* *";if(!l?.batch||!n.pushBatch){c.debug("push",{event:r.event.name});const t=await(0,f.useHooks)(n.push,"DestinationPush",e.hooks)(r.event,u);return c.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,f.debounce)(()=>{const o=n.batches[g].batched,r={collector:e,logger:c,id:t,config:i,data:void 0,rule:l,ingest:s,env:T(n.env,i.env)};c.debug("push batch",{events:o.events.length}),(0,f.useHooks)(n.pushBatch,"DestinationPushBatch",e.hooks)(o,r),c.debug("push batch done"),o.events=[],o.data=[]},l.batch)}}const o=n.batches[g];o.batched.events.push(r.event),(0,f.isDefined)(r.data)&&o.batched.data.push(r.data),o.batchFn()}return!0}function R(e){return{ok:!e?.failed,...e}}function I(e){const{code:n,config:t={},env:o={}}=e,{config:s}=w(e,"before"),i={...n.config,...t,...s},r=T(n.env,o);return{...n,config:i,env:r}}async function M(e,n={}){const t={};for(const[o,s]of Object.entries(n))s.config?.require?.length?e.pending.destinations[o]=s:t[o]=I(s);return t}function T(e,n){return e||n?n?e&&(0,f.isObject)(e)&&(0,f.isObject)(n)?{...e,...n}:n:e:{}}var _=require("@walkeros/core"),F=require("@walkeros/core");async function G(e,n,t,o){let s,i,r=!1,a=!1;switch(n){case c.Commands.Config:(0,F.isObject)(t)&&((0,_.assign)(e.config,t,{shallow:!1}),i=t,r=!0);break;case c.Commands.Consent:if((0,F.isObject)(t)){const{update:n,runQueue:o}=l(e,t);i=n,r=!0,a=o}break;case c.Commands.Custom:(0,F.isObject)(t)&&(e.custom=(0,_.assign)(e.custom,t),i=t,r=!0);break;case c.Commands.Destination:(0,F.isObject)(t)&&("code"in t&&(0,F.isObject)(t.code)?s=await E(e,t,o):(0,_.isFunction)(t.push)&&(s=await E(e,{code:t},o)));break;case c.Commands.Globals:(0,F.isObject)(t)&&(e.globals=(0,_.assign)(e.globals,t),i=t,r=!0);break;case c.Commands.On:(0,_.isString)(t)&&await D(e,t,o);break;case c.Commands.Ready:r=!0;break;case c.Commands.Run:s=await U(e,t),r=!0;break;case c.Commands.Session:r=!0;break;case c.Commands.User:(0,F.isObject)(t)&&((0,_.assign)(e.user,t,{shallow:!1}),i=t,r=!0)}return r&&await x(e,n,void 0,i),a&&(s=await S(e)),s||R({ok:!0})}function B(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:i=e.group,count:r=e.count}=n,{name:a=`${t} ${o}`,data:c={},context:u={},globals:l=e.globals,custom:g={},user:f=e.user,nested:d=[],consent:m=e.consent,id:h=`${s}-${i}-${r}`,trigger:p="",entity:b=t,action:y=o,timing:w=0,version:k={source:e.version,tagging:e.config.tagging||0},source:v={type:"collector",id:"",previous_id:""}}=n;return{name:a,data:c,context:u,globals:l,custom:g,user:f,nested:d,consent:m,id:h,trigger:p,entity:b,action:y,timestamp:s,timing:w,group:i,count:r,version:k,source:v}}async function U(e,n){e.allowed=!0,e.count=0,e.group=(0,_.getId)(),e.timing=Date.now(),n&&(n.consent&&(e.consent=(0,_.assign)(e.consent,n.consent)),n.user&&(e.user=(0,_.assign)(e.user,n.user)),n.globals&&(e.globals=(0,_.assign)(e.config.globalsStatic||{},n.globals)),n.custom&&(e.custom=(0,_.assign)(e.custom,n.custom))),Object.values(e.destinations).forEach(e=>{e.queuePush=[]}),e.queue=[],e.round++;return await S(e)}var L=require("@walkeros/core");function N(e,n){return(0,L.useHooks)(async(t,o={})=>await(0,L.tryCatchAsync)(async()=>{const s=Date.now(),{id:i,ingest:r,mapping:a,preChain:c}=o;let u=t;const l=r?Object.freeze(r):void 0;if(a){const n=await(0,L.processEventMapping)(u,a,e);if(n.ignore)return R({ok:!0});if(a.consent){if(!(0,L.getGrantedConsent)(a.consent,e.consent,n.event.consent))return R({ok:!0})}u=n.event}if(c?.length&&e.transformers&&Object.keys(e.transformers).length>0){const n=await O(e,e.transformers,c,u,l);if(null===n)return R({ok:!0});u=n}const g=n(u),f=B(e,g),d=await S(e,f,{id:i,ingest:l});if(i){e.status.sources[i]||(e.status.sources[i]={count:0,duration:0});const n=e.status.sources[i];n.count++,n.lastAt=Date.now(),n.duration+=Date.now()-s}return d},()=>R({ok:!1}))(),"Push",e.hooks)}var V=require("@walkeros/core");async function Q(e){const n=(0,g.assign)({globalsStatic:{},sessionStatic:{},tagging:0,run:!0},e,{merge:!1,extend:!1}),t={level:e.logger?.level,handler:e.logger?.handler},o=(0,g.createLogger)(t),s={...n.globalsStatic,...e.globals},i={allowed:!1,config:n,consent:e.consent||{},count:0,custom:e.custom||{},destinations:{},transformers:{},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:e.user||{},version:"2.0.0",sources:{},pending:{sources:{},destinations:{}},push:void 0,command:void 0};return i.push=N(i,e=>({timing:Math.round((Date.now()-i.timing)/10)/100,source:{type:"collector",id:"",previous_id:""},...e})),i.command=function(e,n){return(0,V.useHooks)(async(t,o,s)=>await(0,V.tryCatchAsync)(async()=>await n(e,t,o,s),()=>R({ok:!1}))(),"Command",e.hooks)}(i,G),i.destinations=await M(i,e.destinations||{}),i.transformers=await async function(e,n={}){const t={};for(const[o,s]of Object.entries(n)){const{code:n,env:i={}}=s,{config:r}=w(s,"next"),a=e.logger.scope("transformer").scope(o),c={collector:e,logger:a,id:o,config:r,env:i},u=await n(c);t[o]=u}return t}(i,e.transformers||{}),i}async function W(e){e=e||{};const n=await Q(e),t=(o=n,{type:"elb",config:{},push:async(e,n,t,s,i,r)=>{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 R({ok:!1});a=e,n&&"object"==typeof n&&!Array.isArray(n)&&(a.data={...a.data||{},...n})}return s&&"object"==typeof s&&(a.context=s),i&&Array.isArray(i)&&(a.nested=i),r&&"object"==typeof r&&(a.custom=r),o.push(a)}});var o;n.sources.elb=t;const s=await A(n,e.sources||{});Object.assign(n.sources,s);const{consent:i,user:r,globals:a,custom:c}=e;i&&await n.command("consent",i),r&&await n.command("user",r),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),g=l.find(e=>e.config.primary);return g?u=g.push:l.length>0&&(u=l[0].push),{collector:n,elb:u}}//# 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",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 i,createLogger as r}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 b}from"@walkeros/core";import{isArray as y}from"@walkeros/core";import{tryCatch as w,tryCatchAsync as k}from"@walkeros/core";import{getMappingValue as v,tryCatchAsync as C}from"@walkeros/core";import{isObject as O,tryCatchAsync as q,useHooks as j}from"@walkeros/core";function A(n){return null!=n&&!1!==n&&"object"==typeof n&&!0===n.__branch}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 x(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 P(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 E(n,e,t){if(e.init&&!e.config.init){const o=e.type||"unknown",s=n.logger.scope(`transformer:${o}`),i={collector:n,logger:s,id:t,config:e.config,env:R(e.config.env)};s.debug("init");const r=await j(e.init,"TransformerInit",n.hooks)(i);if(!1===r)return!1;e.config={...r||e.config,init:!0},s.debug("init done")}return!0}async function S(n,e,t,o,s){const i=e.type||"unknown",r=n.logger.scope(`transformer:${i}`),a={collector:n,logger:r,id:t,ingest:s,config:e.config,env:R(e.config.env)};r.debug("push",{event:o.name});const c=await j(e.push,"TransformerPush",n.hooks)(o,a);return r.debug("push done"),c}async function $(n,e,t,o,s){let i=o;for(const o of t){const t=e[o];if(!t){n.logger.info(`Transformer not found: ${o}`);continue}if(!await q(E)(n,t,o))return n.logger.info(`Transformer init failed: ${o}`),null;const r=await q(S,e=>(n.logger.scope(`transformer:${t.type||"unknown"}`).error("Push failed",{error:e}),!1))(n,t,o,i,s);if(!1===r)return null;if(A(r)){const t=P(r.next,D(e));return t.length>0?$(n,e,t,r.event,s):(n.logger.info(`Branch target not found: ${JSON.stringify(r.next)}`),null)}void 0!==r&&(i=r)}return i}function R(n){return n&&O(n)?n:{}}async function I(n,e,t){const{code:o,config:s={},env:i={},primary:r,next:a}=t;let c;const u=P(a,D(n.transformers)),f=n.logger.scope("source").scope(e),l={push:(t,o={})=>n.push(t,{...o,id:e,ingest:c,mapping:s,preChain:u}),command:n.command,sources:n.sources,elb:n.sources.elb.push,logger:f,...i},g={collector:n,logger:f,id:e,config:s,env:l,setIngest:async e=>{c=s.ingest?await v(e,s.ingest,{collector:n}):void 0}},d=await C(o)(g);if(!d)return;const m=d.type||"unknown",p=n.logger.scope(m).scope(e);return l.logger=p,r&&(d.config={...d.config,primary:r}),d}async function B(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 i=await I(n,o,s);i&&(t[o]=i)}return t}async function G(n,e,t){const o=n.on,s=o[e]||[],i=y(t)?t:[t];i.forEach(n=>{s.push(n)}),o[e]=s,await T(n,e,i)}function H(n,e,t,o,s){if(!e.on)return;const i=e.type||"unknown",r=n.logger.scope(i).scope("on").scope(o),a={collector:n,logger:r,id:t,config:e.config,data:s,env:W(e.env,e.config.env)};w(e.on)(o,a)}async function T(n,e,o,s){let i,r=o||[];switch(o||(r=n.on[e]||[]),e){case t.Commands.Consent:i=s||n.consent;break;case t.Commands.Session:i=n.session;break;case t.Commands.User:i=s||n.user;break;case t.Commands.Custom:i=s||n.custom;break;case t.Commands.Globals:i=s||n.globals;break;case t.Commands.Config:i=s||n.config;break;case t.Commands.Ready:case t.Commands.Run:default:i=void 0}let a=!1;for(const t of Object.values(n.sources))if(t.on){!1===await k(t.on)(e,i)&&(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:i});H(n,o,t,e,i)}}),(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 i=s.indexOf(e);if(-1===i)continue;if(s.splice(i,1),s.length>0)continue;delete n.pending.sources[t];const r=await I(n,t,o);r&&(n.sources[t]=r)}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 i=s.indexOf(e);if(-1===i)continue;if(s.splice(i,1),s.length>0)continue;delete n.pending.destinations[t];const r=Q(o);!1!==r.config.queue&&(r.queuePush=[...n.queue]),n.destinations[t]=r}}(n,e),!r.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=>{w(e[t])(n,o)})})}(n,r,s);break;case t.Commands.Ready:case t.Commands.Run:!function(n,e){n.allowed&&e.forEach(e=>{w(e)(n)})}(n,r);break;case t.Commands.Session:!function(n,e){if(!n.session)return;e.forEach(e=>{w(e)(n,n.session)})}(n,r);break;default:r.forEach(e=>{"function"==typeof e&&w(e)(n,i)})}return!a}async function U(n,e,t){const{code:o,config:s={},env:i={},before:r}=e;if(!d(o.push))return M({ok:!1,failed:{invalid:{type:"invalid",error:"Destination code must have a push method"}}});const a=t||s||{init:!1},c=r?{...a,before:r}:a,u={...o,config:c,env:W(o.env,i)};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]),_(n,void 0,{},{[l]:u})}async function _(n,e,t={},o){const{allowed:s,consent:i,globals:r,user:u}=n;if(!s)return M({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:i}));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(F)(n,s,o);return{id:o,destination:s,skipped:!e}}const g=[],d=f.filter(n=>{const e=l(s.config.consent,i,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(F)(n,s,o))return{id:o,destination:s,queue:f};let m,p;s.dlq||(s.dlq=[]);const b=function(n,e){const t=n.config.before;return t?P(t,D(e)):[]}(s,n.transformers);let y=0;return await Promise.all(g.map(async e=>{e.globals=a(r,e.globals),e.user=a(u,e.user);let i=e;if(b.length>0&&n.transformers&&Object.keys(n.transformers).length>0){const o=await $(n,n.transformers,b,e,t.ingest);if(null===o)return e;i=o}const c=Date.now(),f=await h(L,e=>{const t=s.type||"unknown";n.logger.scope(t).error("Push failed",{error:e,event:i.name}),m=e,s.dlq.push([i,e])})(n,s,o,i,t.ingest);return y+=Date.now()-c,void 0!==f&&(p=f),e})),{id:o,destination:s,error:m,response:p,totalDuration:y}})),g={},d={},m={};for(const e of f){if(e.skipped)continue;const t=e.destination,o={type:t.type||"unknown",data:e.response};n.status.destinations[e.id]||(n.status.destinations[e.id]={count:0,failed:0,duration:0});const s=n.status.destinations[e.id],i=Date.now();e.error?(o.error=e.error,m[e.id]=o,s.failed++,s.lastAt=i,s.duration+=e.totalDuration||0,n.status.failed++):e.queue&&e.queue.length?(t.queuePush=(t.queuePush||[]).concat(e.queue),d[e.id]=o):(g[e.id]=o,s.count++,s.lastAt=i,s.duration+=e.totalDuration||0,n.status.out++)}return M({event:e,...Object.keys(g).length&&{done:g},...Object.keys(d).length&&{queued:d},...Object.keys(m).length&&{failed:m}})}async function F(n,e,t){if(e.init&&!e.config.init){const o=e.type||"unknown",s=n.logger.scope(o),i={collector:n,logger:s,id:t,config:e.config,env:W(e.env,e.config.env)};s.debug("init");const r=await b(e.init,"DestinationInit",n.hooks)(i);if(!1===r)return r;if(e.config={...r||e.config,init:!0},e.queueOn?.length){const o=e.queueOn;e.queueOn=[];for(const{type:s,data:i}of o)H(n,e,t,s,i)}s.debug("init done")}return!0}async function L(n,e,t,o,s){const{config:i}=e,r=await p(o,i,n);if(r.ignore)return!1;const a=e.type||"unknown",c=n.logger.scope(a),f={collector:n,logger:c,id:t,config:i,data:r.data,rule:r.mapping,ingest:s,env:W(e.env,i.env)},l=r.mapping,d=r.mappingKey||"* *";if(!l?.batch||!e.pushBatch){c.debug("push",{event:r.event.name});const t=await b(e.push,"DestinationPush",n.hooks)(r.event,f);return c.debug("push done"),t}{if(e.batches=e.batches||{},!e.batches[d]){const o={key:d,events:[],data:[]};e.batches[d]={batched:o,batchFn:u(()=>{const o=e.batches[d].batched,r={collector:n,logger:c,id:t,config:i,data:void 0,rule:l,ingest:s,env:W(e.env,i.env)};c.debug("push batch",{events:o.events.length}),b(e.pushBatch,"DestinationPushBatch",n.hooks)(o,r),c.debug("push batch done"),o.events=[],o.data=[]},l.batch)}}const o=e.batches[d];o.batched.events.push(r.event),g(r.data)&&o.batched.data.push(r.data),o.batchFn()}return!0}function M(n){return{ok:!n?.failed,...n}}function Q(n){const{code:e,config:t={},env:o={}}=n,{config:s}=x(n,"before"),i={...e.config,...t,...s},r=W(e.env,o);return{...e,config:i,env:r}}async function V(n,e={}){const t={};for(const[o,s]of Object.entries(e))s.config?.require?.length?n.pending.destinations[o]=s:t[o]=Q(s);return t}function W(n,e){return n||e?e?n&&m(n)&&m(e)?{...n,...e}:e:n:{}}import{assign as z,getId as J,isFunction as K,isString as N}from"@walkeros/core";import{isObject as X}from"@walkeros/core";async function Y(n,e,o,i){let r,a,c=!1,u=!1;switch(e){case t.Commands.Config:X(o)&&(z(n.config,o,{shallow:!1}),a=o,c=!0);break;case t.Commands.Consent:if(X(o)){const{update:e,runQueue:t}=s(n,o);a=e,c=!0,u=t}break;case t.Commands.Custom:X(o)&&(n.custom=z(n.custom,o),a=o,c=!0);break;case t.Commands.Destination:X(o)&&("code"in o&&X(o.code)?r=await U(n,o,i):K(o.push)&&(r=await U(n,{code:o},i)));break;case t.Commands.Globals:X(o)&&(n.globals=z(n.globals,o),a=o,c=!0);break;case t.Commands.On:N(o)&&await G(n,o,i);break;case t.Commands.Ready:c=!0;break;case t.Commands.Run:r=await nn(n,o),c=!0;break;case t.Commands.Session:c=!0;break;case t.Commands.User:X(o)&&(z(n.user,o,{shallow:!1}),a=o,c=!0)}return c&&await T(n,e,void 0,a),u&&(r=await _(n)),r||M({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:i=n.group,count:r=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}-${i}-${r}`,trigger:h="",entity:b=t,action:y=o,timing:w=0,version:k={source:n.version,tagging:n.config.tagging||0},source:v={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:b,action:y,timestamp:s,timing:w,group:i,count:r,version:k,source:v}}async function nn(n,e){n.allowed=!0,n.count=0,n.group=J(),n.timing=Date.now(),e&&(e.consent&&(n.consent=z(n.consent,e.consent)),e.user&&(n.user=z(n.user,e.user)),e.globals&&(n.globals=z(n.config.globalsStatic||{},e.globals)),e.custom&&(n.custom=z(n.custom,e.custom))),Object.values(n.destinations).forEach(n=>{n.queuePush=[]}),n.queue=[],n.round++;return await _(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:i,ingest:r,mapping:a,preChain:c}=o;let u=t;const f=r?Object.freeze(r):void 0;if(a){const e=await tn(u,a,n);if(e.ignore)return M({ok:!0});if(a.consent){if(!en(a.consent,n.consent,e.event.consent))return M({ok:!0})}u=e.event}if(c?.length&&n.transformers&&Object.keys(n.transformers).length>0){const e=await $(n,n.transformers,c,u,f);if(null===e)return M({ok:!0});u=e}const l=e(u),g=Z(n,l),d=await _(n,g,{id:i,ingest:f});if(i){n.status.sources[i]||(n.status.sources[i]={count:0,duration:0});const e=n.status.sources[i];e.count++,e.lastAt=Date.now(),e.duration+=Date.now()-s}return d},()=>M({ok:!1}))(),"Push",n.hooks)}import{useHooks as an,tryCatchAsync as cn}from"@walkeros/core";async function un(n){const e=i({globalsStatic:{},sessionStatic:{},tagging:0,run:!0},n,{merge:!1,extend:!1}),t={level:n.logger?.level,handler:n.logger?.handler},o=r(t),s={...e.globalsStatic,...n.globals},a={allowed:!1,config:e,consent:n.consent||{},count:0,custom:n.custom||{},destinations:{},transformers:{},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:"1.2.0",sources:{},pending:{sources:{},destinations:{}},push:void 0,command:void 0};return 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),()=>M({ok:!1}))(),"Command",n.hooks)}(a,Y),a.destinations=await V(a,n.destinations||{}),a.transformers=await async function(n,e={}){const t={};for(const[o,s]of Object.entries(e)){const{code:e,env:i={}}=s,{config:r}=x(s,"next"),a=n.logger.scope("transformer").scope(o),c={collector:n,logger:a,id:o,config:r,env:i},u=await e(c);t[o]=u}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,i,r)=>{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 M({ok:!1});a=n,e&&"object"==typeof e&&!Array.isArray(e)&&(a.data={...a.data||{},...e})}return s&&"object"==typeof s&&(a.context=s),i&&Array.isArray(i)&&(a.nested=i),r&&"object"==typeof r&&(a.custom=r),o.push(a)}});var o;e.sources.elb=t;const s=await B(e,n.sources||{});Object.assign(e.sources,s);const{consent:i,user:r,globals:a,custom:c}=n;i&&await e.command("consent",i),r&&await e.command("user",r),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}}export{n as Code,e as Commands,t as Const,U as addDestination,H as callDestinationOn,Y as commonHandleCommand,Z as createEvent,rn as createPush,M as createPushResult,F as destinationInit,L as destinationPush,D as extractTransformerNextMap,V as initDestinations,I as initSource,B as initSources,W as mergeEnvironments,G as on,T as onApply,s as processConsent,_ as pushToDestinations,Q as registerDestination,nn as runCollector,fn as startFlow,P as walkChain};//# 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",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 i,createLogger as r}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 b}from"@walkeros/core";import{isArray as y}from"@walkeros/core";import{tryCatch as w,tryCatchAsync as k}from"@walkeros/core";import{getMappingValue as v,tryCatchAsync as C}from"@walkeros/core";import{isObject as O,tryCatchAsync as q,useHooks as j}from"@walkeros/core";function A(n){return null!=n&&!1!==n&&"object"==typeof n&&!0===n.__branch}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 x(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 P(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 E(n,e,t){if(e.init&&!e.config.init){const o=e.type||"unknown",s=n.logger.scope(`transformer:${o}`),i={collector:n,logger:s,id:t,config:e.config,env:R(e.config.env)};s.debug("init");const r=await j(e.init,"TransformerInit",n.hooks)(i);if(!1===r)return!1;e.config={...r||e.config,init:!0},s.debug("init done")}return!0}async function S(n,e,t,o,s){const i=e.type||"unknown",r=n.logger.scope(`transformer:${i}`),a={collector:n,logger:r,id:t,ingest:s,config:e.config,env:R(e.config.env)};r.debug("push",{event:o.name});const c=await j(e.push,"TransformerPush",n.hooks)(o,a);return r.debug("push done"),c}async function $(n,e,t,o,s){let i=o;for(const o of t){const t=e[o];if(!t){n.logger.info(`Transformer not found: ${o}`);continue}if(!await q(E)(n,t,o))return n.logger.info(`Transformer init failed: ${o}`),null;const r=await q(S,e=>(n.logger.scope(`transformer:${t.type||"unknown"}`).error("Push failed",{error:e}),!1))(n,t,o,i,s);if(!1===r)return null;if(A(r)){const t=P(r.next,D(e));return t.length>0?$(n,e,t,r.event,s):(n.logger.info(`Branch target not found: ${JSON.stringify(r.next)}`),null)}void 0!==r&&(i=r)}return i}function R(n){return n&&O(n)?n:{}}async function I(n,e,t){const{code:o,config:s={},env:i={},primary:r,next:a}=t;let c;const u=P(a,D(n.transformers)),f=n.logger.scope("source").scope(e),l={push:(t,o={})=>n.push(t,{...o,id:e,ingest:c,mapping:s,preChain:u}),command:n.command,sources:n.sources,elb:n.sources.elb.push,logger:f,...i},g={collector:n,logger:f,id:e,config:s,env:l,setIngest:async e=>{c=s.ingest?await v(e,s.ingest,{collector:n}):void 0}},d=await C(o)(g);if(!d)return;const m=d.type||"unknown",p=n.logger.scope(m).scope(e);return l.logger=p,r&&(d.config={...d.config,primary:r}),d}async function B(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 i=await I(n,o,s);i&&(t[o]=i)}return t}async function G(n,e,t){const o=n.on,s=o[e]||[],i=y(t)?t:[t];i.forEach(n=>{s.push(n)}),o[e]=s,await T(n,e,i)}function H(n,e,t,o,s){if(!e.on)return;const i=e.type||"unknown",r=n.logger.scope(i).scope("on").scope(o),a={collector:n,logger:r,id:t,config:e.config,data:s,env:W(e.env,e.config.env)};w(e.on)(o,a)}async function T(n,e,o,s){let i,r=o||[];switch(o||(r=n.on[e]||[]),e){case t.Commands.Consent:i=s||n.consent;break;case t.Commands.Session:i=n.session;break;case t.Commands.User:i=s||n.user;break;case t.Commands.Custom:i=s||n.custom;break;case t.Commands.Globals:i=s||n.globals;break;case t.Commands.Config:i=s||n.config;break;case t.Commands.Ready:case t.Commands.Run:default:i=void 0}let a=!1;for(const t of Object.values(n.sources))if(t.on){!1===await k(t.on)(e,i)&&(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:i});H(n,o,t,e,i)}}),(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 i=s.indexOf(e);if(-1===i)continue;if(s.splice(i,1),s.length>0)continue;delete n.pending.sources[t];const r=await I(n,t,o);r&&(n.sources[t]=r)}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 i=s.indexOf(e);if(-1===i)continue;if(s.splice(i,1),s.length>0)continue;delete n.pending.destinations[t];const r=Q(o);!1!==r.config.queue&&(r.queuePush=[...n.queue]),n.destinations[t]=r}}(n,e),!r.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=>{w(e[t])(n,o)})})}(n,r,s);break;case t.Commands.Ready:case t.Commands.Run:!function(n,e){n.allowed&&e.forEach(e=>{w(e)(n)})}(n,r);break;case t.Commands.Session:!function(n,e){if(!n.session)return;e.forEach(e=>{w(e)(n,n.session)})}(n,r);break;default:r.forEach(e=>{"function"==typeof e&&w(e)(n,i)})}return!a}async function U(n,e,t){const{code:o,config:s={},env:i={},before:r}=e;if(!d(o.push))return M({ok:!1,failed:{invalid:{type:"invalid",error:"Destination code must have a push method"}}});const a=t||s||{init:!1},c=r?{...a,before:r}:a,u={...o,config:c,env:W(o.env,i)};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]),_(n,void 0,{},{[l]:u})}async function _(n,e,t={},o){const{allowed:s,consent:i,globals:r,user:u}=n;if(!s)return M({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:i}));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(F)(n,s,o);return{id:o,destination:s,skipped:!e}}const g=[],d=f.filter(n=>{const e=l(s.config.consent,i,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(F)(n,s,o))return{id:o,destination:s,queue:f};let m,p;s.dlq||(s.dlq=[]);const b=function(n,e){const t=n.config.before;return t?P(t,D(e)):[]}(s,n.transformers);let y=0;return await Promise.all(g.map(async e=>{e.globals=a(r,e.globals),e.user=a(u,e.user);let i=e;if(b.length>0&&n.transformers&&Object.keys(n.transformers).length>0){const o=await $(n,n.transformers,b,e,t.ingest);if(null===o)return e;i=o}const c=Date.now(),f=await h(L,e=>{const t=s.type||"unknown";n.logger.scope(t).error("Push failed",{error:e,event:i.name}),m=e,s.dlq.push([i,e])})(n,s,o,i,t.ingest);return y+=Date.now()-c,void 0!==f&&(p=f),e})),{id:o,destination:s,error:m,response:p,totalDuration:y}})),g={},d={},m={};for(const e of f){if(e.skipped)continue;const t=e.destination,o={type:t.type||"unknown",data:e.response};n.status.destinations[e.id]||(n.status.destinations[e.id]={count:0,failed:0,duration:0});const s=n.status.destinations[e.id],i=Date.now();e.error?(o.error=e.error,m[e.id]=o,s.failed++,s.lastAt=i,s.duration+=e.totalDuration||0,n.status.failed++):e.queue&&e.queue.length?(t.queuePush=(t.queuePush||[]).concat(e.queue),d[e.id]=o):(g[e.id]=o,s.count++,s.lastAt=i,s.duration+=e.totalDuration||0,n.status.out++)}return M({event:e,...Object.keys(g).length&&{done:g},...Object.keys(d).length&&{queued:d},...Object.keys(m).length&&{failed:m}})}async function F(n,e,t){if(e.init&&!e.config.init){const o=e.type||"unknown",s=n.logger.scope(o),i={collector:n,logger:s,id:t,config:e.config,env:W(e.env,e.config.env)};s.debug("init");const r=await b(e.init,"DestinationInit",n.hooks)(i);if(!1===r)return r;if(e.config={...r||e.config,init:!0},e.queueOn?.length){const o=e.queueOn;e.queueOn=[];for(const{type:s,data:i}of o)H(n,e,t,s,i)}s.debug("init done")}return!0}async function L(n,e,t,o,s){const{config:i}=e,r=await p(o,i,n);if(r.ignore)return!1;const a=e.type||"unknown",c=n.logger.scope(a),f={collector:n,logger:c,id:t,config:i,data:r.data,rule:r.mapping,ingest:s,env:W(e.env,i.env)},l=r.mapping,d=r.mappingKey||"* *";if(!l?.batch||!e.pushBatch){c.debug("push",{event:r.event.name});const t=await b(e.push,"DestinationPush",n.hooks)(r.event,f);return c.debug("push done"),t}{if(e.batches=e.batches||{},!e.batches[d]){const o={key:d,events:[],data:[]};e.batches[d]={batched:o,batchFn:u(()=>{const o=e.batches[d].batched,r={collector:n,logger:c,id:t,config:i,data:void 0,rule:l,ingest:s,env:W(e.env,i.env)};c.debug("push batch",{events:o.events.length}),b(e.pushBatch,"DestinationPushBatch",n.hooks)(o,r),c.debug("push batch done"),o.events=[],o.data=[]},l.batch)}}const o=e.batches[d];o.batched.events.push(r.event),g(r.data)&&o.batched.data.push(r.data),o.batchFn()}return!0}function M(n){return{ok:!n?.failed,...n}}function Q(n){const{code:e,config:t={},env:o={}}=n,{config:s}=x(n,"before"),i={...e.config,...t,...s},r=W(e.env,o);return{...e,config:i,env:r}}async function V(n,e={}){const t={};for(const[o,s]of Object.entries(e))s.config?.require?.length?n.pending.destinations[o]=s:t[o]=Q(s);return t}function W(n,e){return n||e?e?n&&m(n)&&m(e)?{...n,...e}:e:n:{}}import{assign as z,getId as J,isFunction as K,isString as N}from"@walkeros/core";import{isObject as X}from"@walkeros/core";async function Y(n,e,o,i){let r,a,c=!1,u=!1;switch(e){case t.Commands.Config:X(o)&&(z(n.config,o,{shallow:!1}),a=o,c=!0);break;case t.Commands.Consent:if(X(o)){const{update:e,runQueue:t}=s(n,o);a=e,c=!0,u=t}break;case t.Commands.Custom:X(o)&&(n.custom=z(n.custom,o),a=o,c=!0);break;case t.Commands.Destination:X(o)&&("code"in o&&X(o.code)?r=await U(n,o,i):K(o.push)&&(r=await U(n,{code:o},i)));break;case t.Commands.Globals:X(o)&&(n.globals=z(n.globals,o),a=o,c=!0);break;case t.Commands.On:N(o)&&await G(n,o,i);break;case t.Commands.Ready:c=!0;break;case t.Commands.Run:r=await nn(n,o),c=!0;break;case t.Commands.Session:c=!0;break;case t.Commands.User:X(o)&&(z(n.user,o,{shallow:!1}),a=o,c=!0)}return c&&await T(n,e,void 0,a),u&&(r=await _(n)),r||M({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:i=n.group,count:r=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}-${i}-${r}`,trigger:h="",entity:b=t,action:y=o,timing:w=0,version:k={source:n.version,tagging:n.config.tagging||0},source:v={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:b,action:y,timestamp:s,timing:w,group:i,count:r,version:k,source:v}}async function nn(n,e){n.allowed=!0,n.count=0,n.group=J(),n.timing=Date.now(),e&&(e.consent&&(n.consent=z(n.consent,e.consent)),e.user&&(n.user=z(n.user,e.user)),e.globals&&(n.globals=z(n.config.globalsStatic||{},e.globals)),e.custom&&(n.custom=z(n.custom,e.custom))),Object.values(n.destinations).forEach(n=>{n.queuePush=[]}),n.queue=[],n.round++;return await _(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:i,ingest:r,mapping:a,preChain:c}=o;let u=t;const f=r?Object.freeze(r):void 0;if(a){const e=await tn(u,a,n);if(e.ignore)return M({ok:!0});if(a.consent){if(!en(a.consent,n.consent,e.event.consent))return M({ok:!0})}u=e.event}if(c?.length&&n.transformers&&Object.keys(n.transformers).length>0){const e=await $(n,n.transformers,c,u,f);if(null===e)return M({ok:!0});u=e}const l=e(u),g=Z(n,l),d=await _(n,g,{id:i,ingest:f});if(i){n.status.sources[i]||(n.status.sources[i]={count:0,duration:0});const e=n.status.sources[i];e.count++,e.lastAt=Date.now(),e.duration+=Date.now()-s}return d},()=>M({ok:!1}))(),"Push",n.hooks)}import{useHooks as an,tryCatchAsync as cn}from"@walkeros/core";async function un(n){const e=i({globalsStatic:{},sessionStatic:{},tagging:0,run:!0},n,{merge:!1,extend:!1}),t={level:n.logger?.level,handler:n.logger?.handler},o=r(t),s={...e.globalsStatic,...n.globals},a={allowed:!1,config:e,consent:n.consent||{},count:0,custom:n.custom||{},destinations:{},transformers:{},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.0.0",sources:{},pending:{sources:{},destinations:{}},push:void 0,command:void 0};return 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),()=>M({ok:!1}))(),"Command",n.hooks)}(a,Y),a.destinations=await V(a,n.destinations||{}),a.transformers=await async function(n,e={}){const t={};for(const[o,s]of Object.entries(e)){const{code:e,env:i={}}=s,{config:r}=x(s,"next"),a=n.logger.scope("transformer").scope(o),c={collector:n,logger:a,id:o,config:r,env:i},u=await e(c);t[o]=u}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,i,r)=>{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 M({ok:!1});a=n,e&&"object"==typeof e&&!Array.isArray(e)&&(a.data={...a.data||{},...e})}return s&&"object"==typeof s&&(a.context=s),i&&Array.isArray(i)&&(a.nested=i),r&&"object"==typeof r&&(a.custom=r),o.push(a)}});var o;e.sources.elb=t;const s=await B(e,n.sources||{});Object.assign(e.sources,s);const{consent:i,user:r,globals:a,custom:c}=n;i&&await e.command("consent",i),r&&await e.command("user",r),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}}export{n as Code,e as Commands,t as Const,U as addDestination,H as callDestinationOn,Y as commonHandleCommand,Z as createEvent,rn as createPush,M as createPushResult,F as destinationInit,L as destinationPush,D as extractTransformerNextMap,V as initDestinations,I as initSource,B as initSources,W as mergeEnvironments,G as on,T as onApply,s as processConsent,_ as pushToDestinations,Q as registerDestination,nn as runCollector,fn as startFlow,P as walkChain};//# 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": "2.0.
|
|
4
|
+
"version": "2.0.1",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
@@ -30,9 +30,6 @@
|
|
|
30
30
|
"test": "jest",
|
|
31
31
|
"update": "npx npm-check-updates -u && npm update"
|
|
32
32
|
},
|
|
33
|
-
"peerDependencies": {
|
|
34
|
-
"@walkeros/core": ">=1.4.0"
|
|
35
|
-
},
|
|
36
33
|
"devDependencies": {
|
|
37
34
|
"@walkeros/core": "*"
|
|
38
35
|
},
|
|
@@ -61,5 +58,8 @@
|
|
|
61
58
|
"type": "GitHub Sponsors",
|
|
62
59
|
"url": "https://github.com/sponsors/elbwalker"
|
|
63
60
|
}
|
|
64
|
-
]
|
|
61
|
+
],
|
|
62
|
+
"dependencies": {
|
|
63
|
+
"@walkeros/core": "^2.0.0"
|
|
64
|
+
}
|
|
65
65
|
}
|