@walkeros/collector 1.1.0 → 1.1.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 e,n=Object.defineProperty,t=Object.getOwnPropertyDescriptor,o=Object.getOwnPropertyNames,s=Object.prototype.hasOwnProperty,r={};((e,t)=>{for(var o in t)n(e,o,{get:t[o],enumerable:!0})})(r,{Code:()=>i,Commands:()=>a,Const:()=>c,addDestination:()=>C,callDestinationOn:()=>m,commonHandleCommand:()=>H,createEvent:()=>R,createPush:()=>T,createPushResult:()=>P,destinationInit:()=>q,destinationPush:()=>j,initDestinations:()=>A,initSources:()=>L,mergeEnvironments:()=>E,on:()=>d,onApply:()=>h,pushToDestinations:()=>O,runCollector:()=>I,setConsent:()=>D,startFlow:()=>U}),module.exports=(e=r,((e,r,i,a)=>{if(r&&"object"==typeof r||"function"==typeof r)for(let c of o(r))s.call(e,c)||c===i||n(e,c,{get:()=>r[c],enumerable:!(a=t(r,c))||a.enumerable});return e})(n({},"__esModule",{value:!0}),e));var i={},a={Action:"action",Actions:"actions",Config:"config",Consent:"consent",Context:"context",Custom:"custom",Destination:"destination",Elb:"elb",Globals:"globals",Hook:"hook",Init:"init",Link:"link",On:"on",Prefix:"data-elb",Ready:"ready",Run:"run",Session:"session",User:"user",Walker:"walker"},c={Commands:a,Utils:{Storage:{Cookie:"cookie",Local:"local",Session:"session"}}},u=require("@walkeros/core"),l=require("@walkeros/core"),g=require("@walkeros/core"),f=require("@walkeros/core");function d(e,n,t){const o=e.on,s=o[n]||[],r=(0,g.isArray)(t)?t:[t];r.forEach(e=>{s.push(e)}),o[n]=s,h(e,n,r)}function m(e,n,t,o,s){if(!n.on)return;const r=n.type||"unknown",i=e.logger.scope(r).scope("on").scope(o),a={collector:e,logger:i,id:t,config:n.config,data:s,env:E(n.env,n.config.env)};(0,f.tryCatch)(n.on)(o,a)}function h(e,n,t,o){let s,r=t||[];switch(t||(r=e.on[n]||[]),n){case c.Commands.Consent:s=o||e.consent;break;case c.Commands.Session:s=e.session;break;case c.Commands.Ready:case c.Commands.Run:default:s=void 0}if(Object.values(e.sources).forEach(e=>{e.on&&(0,f.tryCatch)(e.on)(n,s)}),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});m(e,o,t,n,s)}}),r.length)switch(n){case c.Commands.Consent:!function(e,n,t){const o=t||e.consent;n.forEach(n=>{Object.keys(o).filter(e=>e in n).forEach(t=>{(0,f.tryCatch)(n[t])(e,o)})})}(e,r,o);break;case c.Commands.Ready:case c.Commands.Run:!function(e,n){e.allowed&&n.forEach(n=>{(0,f.tryCatch)(n)(e)})}(e,r);break;case c.Commands.Session:!function(e,n){if(!e.session)return;n.forEach(n=>{(0,f.tryCatch)(n)(e,e.session)})}(e,r)}}var p=require("@walkeros/core");function b(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 y(e,n,t){if(n.init&&!n.config.init){const o=n.type||"unknown",s=e.logger.scope(`transformer:${o}`),r={collector:e,logger:s,id:t,config:n.config,env:w(n.config.env)};s.debug("init");const i=await(0,p.useHooks)(n.init,"TransformerInit",e.hooks)(r);if(!1===i)return!1;n.config={...i||n.config,init:!0},s.debug("init done")}return!0}async function v(e,n,t,o,s){const r=n.type||"unknown",i=e.logger.scope(`transformer:${r}`),a={collector:e,logger:i,id:t,ingest:s,config:n.config,env:w(n.config.env)};i.debug("push",{event:o.name});const c=await(0,p.useHooks)(n.push,"TransformerPush",e.hooks)(o,a);return i.debug("push done"),c}async function k(e,n,t,o,s){let r=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)(y)(e,t,o))return e.logger.info(`Transformer init failed: ${o}`),null;const i=await(0,p.tryCatchAsync)(v,n=>(e.logger.scope(`transformer:${t.type||"unknown"}`).error("Push failed",{error:n}),!1))(e,t,o,r,s);if(!1===i)return null;void 0!==i&&(r=i)}return r}function w(e){return e&&(0,p.isObject)(e)?e:{}}async function C(e,n,t){const{code:o,config:s={},env:r={}}=n,i=t||s||{init:!1},a={...o,config:i,env:E(o.env,r)};let c=a.config.id;if(!c)do{c=(0,l.getId)(4)}while(e.destinations[c]);return e.destinations[c]=a,!1!==a.config.queue&&(a.queuePush=[...e.queue]),O(e,void 0,{},{[c]:a})}async function O(e,n,t={},o){const{allowed:s,consent:r,globals:i,user:a}=e;if(!s)return P({ok:!1});n&&e.queue.push(n),o||(o=e.destinations);const c=await Promise.all(Object.entries(o||{}).map(async([o,s])=>{let c=(s.queuePush||[]).map(e=>({...e,consent:r}));if(s.queuePush=[],n){const e=(0,l.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,l.tryCatchAsync)(q)(e,s,o);return{id:o,destination:s,skipped:!n}}const u=[],g=c.filter(e=>{const n=(0,l.getGrantedConsent)(s.config.consent,r,e.consent);return!n||(e.consent=n,u.push(e),!1)});if(s.queuePush.concat(g),!u.length)return{id:o,destination:s,queue:c};if(!await(0,l.tryCatchAsync)(q)(e,s,o))return{id:o,destination:s,queue:c};let f,d;s.dlq||(s.dlq=[]);const m=e.transformerChain?.post?.[o]||[];return await Promise.all(u.map(async n=>{n.globals=(0,l.assign)(i,n.globals),n.user=(0,l.assign)(a,n.user);let r=n;if(m.length>0&&e.transformers&&Object.keys(e.transformers).length>0){const o=await k(e,e.transformers,m,n,t.ingest);if(null===o)return n;r=o}const c=await(0,l.tryCatchAsync)(j,n=>{const t=s.type||"unknown";e.logger.scope(t).error("Push failed",{error:n,event:r.name}),f=n,s.dlq.push([r,n])})(e,s,o,r,t.ingest);return void 0!==c&&(d=c),n})),{id:o,destination:s,error:f,response:d}})),u={},g={},f={};for(const e of c){if(e.skipped)continue;const n=e.destination,t={type:n.type||"unknown",data:e.response};e.error?(t.error=e.error,f[e.id]=t):e.queue&&e.queue.length?(n.queuePush=(n.queuePush||[]).concat(e.queue),g[e.id]=t):u[e.id]=t}return P({event:n,...Object.keys(u).length&&{done:u},...Object.keys(g).length&&{queued:g},...Object.keys(f).length&&{failed:f}})}async function q(e,n,t){if(n.init&&!n.config.init){const o=n.type||"unknown",s=e.logger.scope(o),r={collector:e,logger:s,id:t,config:n.config,env:E(n.env,n.config.env)};s.debug("init");const i=await(0,l.useHooks)(n.init,"DestinationInit",e.hooks)(r);if(!1===i)return i;if(n.config={...i||n.config,init:!0},n.queueOn?.length){const o=n.queueOn;n.queueOn=[];for(const{type:s,data:r}of o)m(e,n,t,s,r)}s.debug("init done")}return!0}async function j(e,n,t,o,s){const{config:r}=n,i=await(0,l.processEventMapping)(o,r,e);if(i.ignore)return!1;const a=n.type||"unknown",c=e.logger.scope(a),u={collector:e,logger:c,id:t,config:r,data:i.data,rule:i.mapping,ingest:s,env:E(n.env,r.env)},g=i.mapping,f=i.mappingKey||"* *";if(!g?.batch||!n.pushBatch){c.debug("push",{event:i.event.name});const t=await(0,l.useHooks)(n.push,"DestinationPush",e.hooks)(i.event,u);return c.debug("push done"),t}{if(n.batches=n.batches||{},!n.batches[f]){const o={key:f,events:[],data:[]};n.batches[f]={batched:o,batchFn:(0,l.debounce)(()=>{const o=n.batches[f].batched,i={collector:e,logger:c,id:t,config:r,data:void 0,rule:g,ingest:s,env:E(n.env,r.env)};c.debug("push batch",{events:o.events.length}),(0,l.useHooks)(n.pushBatch,"DestinationPushBatch",e.hooks)(o,i),c.debug("push batch done"),o.events=[],o.data=[]},g.batch)}}const o=n.batches[f];o.batched.events.push(i.event),(0,l.isDefined)(i.data)&&o.batched.data.push(i.data),o.batchFn()}return!0}function P(e){return{ok:!e?.failed,...e}}async function A(e,n={}){const t={};for(const[e,o]of Object.entries(n)){const{code:n,config:s={},env:r={}}=o,i={...n.config,...s},a=E(n.env,r);t[e]={...n,config:i,env:a}}return t}function E(e,n){return e||n?n?e&&(0,l.isObject)(e)&&(0,l.isObject)(n)?{...e,...n}:n:e:{}}async function D(e,n){const{consent:t}=e;let o=!1;const s={};return Object.entries(n).forEach(([e,n])=>{const t=!!n;s[e]=t,o=o||t}),e.consent=(0,u.assign)(t,s),h(e,"consent",void 0,s),o?O(e):P({ok:!0})}var S=require("@walkeros/core"),x=require("@walkeros/core"),$=require("@walkeros/core");async function H(e,n,t,o){let s;switch(n){case c.Commands.Config:(0,$.isObject)(t)&&(0,x.assign)(e.config,t,{shallow:!1});break;case c.Commands.Consent:(0,$.isObject)(t)&&(s=await D(e,t));break;case c.Commands.Custom:(0,$.isObject)(t)&&(e.custom=(0,x.assign)(e.custom,t));break;case c.Commands.Destination:(0,$.isObject)(t)&&(0,x.isFunction)(t.push)&&(s=await C(e,{code:t},o));break;case c.Commands.Globals:(0,$.isObject)(t)&&(e.globals=(0,x.assign)(e.globals,t));break;case c.Commands.On:(0,x.isString)(t)&&d(e,t,o);break;case c.Commands.Ready:h(e,"ready");break;case c.Commands.Run:s=await I(e,t);break;case c.Commands.Session:h(e,"session");break;case c.Commands.User:(0,$.isObject)(t)&&(0,x.assign)(e.user,t,{shallow:!1})}return s||P({ok:!0})}function R(e,n){if(!n.name)throw new Error("Event name is required");const[t,o]=n.name.split(" ");if(!t||!o)throw new Error("Event name is invalid");++e.count;const{timestamp:s=Date.now(),group:r=e.group,count:i=e.count}=n,{name:a=`${t} ${o}`,data:c={},context:u={},globals:l=e.globals,custom:g={},user:f=e.user,nested:d=[],consent:m=e.consent,id:h=`${s}-${r}-${i}`,trigger:p="",entity:b=t,action:y=o,timing:v=0,version:k={source:e.version,tagging:e.config.tagging||0},source:w={type:"collector",id:"",previous_id:""}}=n;return{name:a,data:c,context:u,globals:l,custom:g,user:f,nested:d,consent:m,id:h,trigger:p,entity:b,action:y,timestamp:s,timing:v,group:r,count:i,version:k,source:w}}async function I(e,n){e.allowed=!0,e.count=0,e.group=(0,x.getId)(),e.timing=Date.now(),n&&(n.consent&&(e.consent=(0,x.assign)(e.consent,n.consent)),n.user&&(e.user=(0,x.assign)(e.user,n.user)),n.globals&&(e.globals=(0,x.assign)(e.config.globalsStatic||{},n.globals)),n.custom&&(e.custom=(0,x.assign)(e.custom,n.custom))),Object.values(e.destinations).forEach(e=>{e.queuePush=[]}),e.queue=[],e.round++;const t=await O(e);return h(e,"run"),t}var M=require("@walkeros/core");function T(e,n){return(0,M.useHooks)(async(t,o={})=>await(0,M.tryCatchAsync)(async()=>{const{id:s,ingest:r,mapping:i,preChain:a}=o;let c=t;const u=r?Object.freeze(r):void 0;if(i){const n=await(0,M.processEventMapping)(c,i,e);if(n.ignore)return P({ok:!0});if(i.consent){if(!(0,M.getGrantedConsent)(i.consent,e.consent,n.event.consent))return P({ok:!0})}c=n.event}if(a?.length&&e.transformers&&Object.keys(e.transformers).length>0){const n=await k(e,e.transformers,a,c,u);if(null===n)return P({ok:!0});c=n}const l=n(c),g=R(e,l);return await O(e,g,{id:s,ingest:u})},()=>P({ok:!1}))(),"Push",e.hooks)}var F=require("@walkeros/core");async function G(e){const n=(0,S.assign)({globalsStatic:{},sessionStatic:{},tagging:0,run:!0},e,{merge:!1,extend:!1}),t={level:e.logger?.level,handler:e.logger?.handler},o=(0,S.createLogger)(t),s={...n.globalsStatic,...e.globals},r={allowed:!1,config:n,consent:e.consent||{},count:0,custom:e.custom||{},destinations:{},transformers:{},transformerChain:{pre:[],post:{}},globals:s,group:"",hooks:{},logger:o,on:{},queue:[],round:0,session:void 0,timing:Date.now(),user:e.user||{},version:"1.0.1",sources:{},push:void 0,command:void 0};return r.push=T(r,e=>({timing:Math.round((Date.now()-r.timing)/10)/100,source:{type:"collector",id:"",previous_id:""},...e})),r.command=function(e,n){return(0,F.useHooks)(async(t,o,s)=>await(0,F.tryCatchAsync)(async()=>await n(e,t,o,s),()=>P({ok:!1}))(),"Command",e.hooks)}(r,H),r.destinations=await A(0,e.destinations||{}),r.transformers=await async function(e,n={}){const t={};for(const[o,s]of Object.entries(n)){const{code:n,config:r={},env:i={}}=s,a=e.logger.scope("transformer").scope(o),c={collector:e,logger:a,id:o,config:r,env:i},u=await n(c);t[o]=u}return t}(r,e.transformers||{}),r}var _=require("@walkeros/core");function B(e){const n={};for(const[t,o]of Object.entries(e))n[t]={next:o.config.next};return n}async function L(e,n={}){const t={};for(const[o,s]of Object.entries(n)){const{code:n,config:r={},env:i={},primary:a,next:c}=s;let u;const l=b(c,B(e.transformers)),g=(n,t={})=>e.push(n,{...t,id:o,ingest:u,mapping:r,preChain:l}),f=e.logger.scope("source").scope(o),d={push:g,command:e.command,sources:e.sources,elb:e.sources.elb.push,logger:f,...i},m={collector:e,logger:f,id:o,config:r,env:d,setIngest:async n=>{u=r.ingest?await(0,_.getMappingValue)(n,r.ingest,{collector:e}):void 0}},h=await(0,_.tryCatchAsync)(n)(m);if(!h)continue;const p=h.type||"unknown",y=e.logger.scope(p).scope(o);d.logger=y,a&&(h.config={...h.config,primary:a}),t[o]=h}return t}async function U(e){e=e||{};const n=await G(e),t=(o=n,{type:"elb",config:{},push:async(e,n,t,s,r,i)=>{if("string"==typeof e&&e.startsWith("walker ")){const s=e.replace("walker ","");return o.command(s,n,t)}let a;if("string"==typeof e)a={name:e},n&&"object"==typeof n&&!Array.isArray(n)&&(a.data=n);else{if(!e||"object"!=typeof e)return P({ok:!1});a=e,n&&"object"==typeof n&&!Array.isArray(n)&&(a.data={...a.data||{},...n})}return s&&"object"==typeof s&&(a.context=s),r&&Array.isArray(r)&&(a.nested=r),i&&"object"==typeof i&&(a.custom=i),o.push(a)}});var o;n.sources.elb=t;const s=await L(n,e.sources||{});Object.assign(n.sources,s);const{consent:r,user:i,globals:a,custom:c}=e;r&&await n.command("consent",r),i&&await n.command("user",i),a&&Object.assign(n.globals,a),c&&Object.assign(n.custom,c),n.config.run&&await n.command("run");let u=t.push;const l=Object.values(n.sources).filter(e=>"elb"!==e.type),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,r={};((e,t)=>{for(var o in t)n(e,o,{get:t[o],enumerable:!0})})(r,{Code:()=>i,Commands:()=>a,Const:()=>c,addDestination:()=>C,callDestinationOn:()=>m,commonHandleCommand:()=>H,createEvent:()=>R,createPush:()=>T,createPushResult:()=>P,destinationInit:()=>q,destinationPush:()=>j,initDestinations:()=>A,initSources:()=>L,mergeEnvironments:()=>E,on:()=>d,onApply:()=>h,pushToDestinations:()=>O,runCollector:()=>I,setConsent:()=>D,startFlow:()=>U}),module.exports=(e=r,((e,r,i,a)=>{if(r&&"object"==typeof r||"function"==typeof r)for(let c of o(r))s.call(e,c)||c===i||n(e,c,{get:()=>r[c],enumerable:!(a=t(r,c))||a.enumerable});return e})(n({},"__esModule",{value:!0}),e));var i={},a={Action:"action",Actions:"actions",Config:"config",Consent:"consent",Context:"context",Custom:"custom",Destination:"destination",Elb:"elb",Globals:"globals",Hook:"hook",Init:"init",Link:"link",On:"on",Prefix:"data-elb",Ready:"ready",Run:"run",Session:"session",User:"user",Walker:"walker"},c={Commands:a,Utils:{Storage:{Cookie:"cookie",Local:"local",Session:"session"}}},u=require("@walkeros/core"),l=require("@walkeros/core"),g=require("@walkeros/core"),f=require("@walkeros/core");function d(e,n,t){const o=e.on,s=o[n]||[],r=(0,g.isArray)(t)?t:[t];r.forEach(e=>{s.push(e)}),o[n]=s,h(e,n,r)}function m(e,n,t,o,s){if(!n.on)return;const r=n.type||"unknown",i=e.logger.scope(r).scope("on").scope(o),a={collector:e,logger:i,id:t,config:n.config,data:s,env:E(n.env,n.config.env)};(0,f.tryCatch)(n.on)(o,a)}function h(e,n,t,o){let s,r=t||[];switch(t||(r=e.on[n]||[]),n){case c.Commands.Consent:s=o||e.consent;break;case c.Commands.Session:s=e.session;break;case c.Commands.Ready:case c.Commands.Run:default:s=void 0}if(Object.values(e.sources).forEach(e=>{e.on&&(0,f.tryCatch)(e.on)(n,s)}),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});m(e,o,t,n,s)}}),r.length)switch(n){case c.Commands.Consent:!function(e,n,t){const o=t||e.consent;n.forEach(n=>{Object.keys(o).filter(e=>e in n).forEach(t=>{(0,f.tryCatch)(n[t])(e,o)})})}(e,r,o);break;case c.Commands.Ready:case c.Commands.Run:!function(e,n){e.allowed&&n.forEach(n=>{(0,f.tryCatch)(n)(e)})}(e,r);break;case c.Commands.Session:!function(e,n){if(!e.session)return;n.forEach(n=>{(0,f.tryCatch)(n)(e,e.session)})}(e,r)}}var p=require("@walkeros/core");function b(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 y(e,n,t){if(n.init&&!n.config.init){const o=n.type||"unknown",s=e.logger.scope(`transformer:${o}`),r={collector:e,logger:s,id:t,config:n.config,env:w(n.config.env)};s.debug("init");const i=await(0,p.useHooks)(n.init,"TransformerInit",e.hooks)(r);if(!1===i)return!1;n.config={...i||n.config,init:!0},s.debug("init done")}return!0}async function v(e,n,t,o,s){const r=n.type||"unknown",i=e.logger.scope(`transformer:${r}`),a={collector:e,logger:i,id:t,ingest:s,config:n.config,env:w(n.config.env)};i.debug("push",{event:o.name});const c=await(0,p.useHooks)(n.push,"TransformerPush",e.hooks)(o,a);return i.debug("push done"),c}async function k(e,n,t,o,s){let r=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)(y)(e,t,o))return e.logger.info(`Transformer init failed: ${o}`),null;const i=await(0,p.tryCatchAsync)(v,n=>(e.logger.scope(`transformer:${t.type||"unknown"}`).error("Push failed",{error:n}),!1))(e,t,o,r,s);if(!1===i)return null;void 0!==i&&(r=i)}return r}function w(e){return e&&(0,p.isObject)(e)?e:{}}async function C(e,n,t){const{code:o,config:s={},env:r={}}=n,i=t||s||{init:!1},a={...o,config:i,env:E(o.env,r)};let c=a.config.id;if(!c)do{c=(0,l.getId)(4)}while(e.destinations[c]);return e.destinations[c]=a,!1!==a.config.queue&&(a.queuePush=[...e.queue]),O(e,void 0,{},{[c]:a})}async function O(e,n,t={},o){const{allowed:s,consent:r,globals:i,user:a}=e;if(!s)return P({ok:!1});n&&e.queue.push(n),o||(o=e.destinations);const c=await Promise.all(Object.entries(o||{}).map(async([o,s])=>{let c=(s.queuePush||[]).map(e=>({...e,consent:r}));if(s.queuePush=[],n){const e=(0,l.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,l.tryCatchAsync)(q)(e,s,o);return{id:o,destination:s,skipped:!n}}const u=[],g=c.filter(e=>{const n=(0,l.getGrantedConsent)(s.config.consent,r,e.consent);return!n||(e.consent=n,u.push(e),!1)});if(s.queuePush.concat(g),!u.length)return{id:o,destination:s,queue:c};if(!await(0,l.tryCatchAsync)(q)(e,s,o))return{id:o,destination:s,queue:c};let f,d;s.dlq||(s.dlq=[]);const m=e.transformerChain?.post?.[o]||[];return await Promise.all(u.map(async n=>{n.globals=(0,l.assign)(i,n.globals),n.user=(0,l.assign)(a,n.user);let r=n;if(m.length>0&&e.transformers&&Object.keys(e.transformers).length>0){const o=await k(e,e.transformers,m,n,t.ingest);if(null===o)return n;r=o}const c=await(0,l.tryCatchAsync)(j,n=>{const t=s.type||"unknown";e.logger.scope(t).error("Push failed",{error:n,event:r.name}),f=n,s.dlq.push([r,n])})(e,s,o,r,t.ingest);return void 0!==c&&(d=c),n})),{id:o,destination:s,error:f,response:d}})),u={},g={},f={};for(const e of c){if(e.skipped)continue;const n=e.destination,t={type:n.type||"unknown",data:e.response};e.error?(t.error=e.error,f[e.id]=t):e.queue&&e.queue.length?(n.queuePush=(n.queuePush||[]).concat(e.queue),g[e.id]=t):u[e.id]=t}return P({event:n,...Object.keys(u).length&&{done:u},...Object.keys(g).length&&{queued:g},...Object.keys(f).length&&{failed:f}})}async function q(e,n,t){if(n.init&&!n.config.init){const o=n.type||"unknown",s=e.logger.scope(o),r={collector:e,logger:s,id:t,config:n.config,env:E(n.env,n.config.env)};s.debug("init");const i=await(0,l.useHooks)(n.init,"DestinationInit",e.hooks)(r);if(!1===i)return i;if(n.config={...i||n.config,init:!0},n.queueOn?.length){const o=n.queueOn;n.queueOn=[];for(const{type:s,data:r}of o)m(e,n,t,s,r)}s.debug("init done")}return!0}async function j(e,n,t,o,s){const{config:r}=n,i=await(0,l.processEventMapping)(o,r,e);if(i.ignore)return!1;const a=n.type||"unknown",c=e.logger.scope(a),u={collector:e,logger:c,id:t,config:r,data:i.data,rule:i.mapping,ingest:s,env:E(n.env,r.env)},g=i.mapping,f=i.mappingKey||"* *";if(!g?.batch||!n.pushBatch){c.debug("push",{event:i.event.name});const t=await(0,l.useHooks)(n.push,"DestinationPush",e.hooks)(i.event,u);return c.debug("push done"),t}{if(n.batches=n.batches||{},!n.batches[f]){const o={key:f,events:[],data:[]};n.batches[f]={batched:o,batchFn:(0,l.debounce)(()=>{const o=n.batches[f].batched,i={collector:e,logger:c,id:t,config:r,data:void 0,rule:g,ingest:s,env:E(n.env,r.env)};c.debug("push batch",{events:o.events.length}),(0,l.useHooks)(n.pushBatch,"DestinationPushBatch",e.hooks)(o,i),c.debug("push batch done"),o.events=[],o.data=[]},g.batch)}}const o=n.batches[f];o.batched.events.push(i.event),(0,l.isDefined)(i.data)&&o.batched.data.push(i.data),o.batchFn()}return!0}function P(e){return{ok:!e?.failed,...e}}async function A(e,n={}){const t={};for(const[e,o]of Object.entries(n)){const{code:n,config:s={},env:r={}}=o,i={...n.config,...s},a=E(n.env,r);t[e]={...n,config:i,env:a}}return t}function E(e,n){return e||n?n?e&&(0,l.isObject)(e)&&(0,l.isObject)(n)?{...e,...n}:n:e:{}}async function D(e,n){const{consent:t}=e;let o=!1;const s={};return Object.entries(n).forEach(([e,n])=>{const t=!!n;s[e]=t,o=o||t}),e.consent=(0,u.assign)(t,s),h(e,"consent",void 0,s),o?O(e):P({ok:!0})}var S=require("@walkeros/core"),x=require("@walkeros/core"),$=require("@walkeros/core");async function H(e,n,t,o){let s;switch(n){case c.Commands.Config:(0,$.isObject)(t)&&(0,x.assign)(e.config,t,{shallow:!1});break;case c.Commands.Consent:(0,$.isObject)(t)&&(s=await D(e,t));break;case c.Commands.Custom:(0,$.isObject)(t)&&(e.custom=(0,x.assign)(e.custom,t));break;case c.Commands.Destination:(0,$.isObject)(t)&&(0,x.isFunction)(t.push)&&(s=await C(e,{code:t},o));break;case c.Commands.Globals:(0,$.isObject)(t)&&(e.globals=(0,x.assign)(e.globals,t));break;case c.Commands.On:(0,x.isString)(t)&&d(e,t,o);break;case c.Commands.Ready:h(e,"ready");break;case c.Commands.Run:s=await I(e,t);break;case c.Commands.Session:h(e,"session");break;case c.Commands.User:(0,$.isObject)(t)&&(0,x.assign)(e.user,t,{shallow:!1})}return s||P({ok:!0})}function R(e,n){if(!n.name)throw new Error("Event name is required");const[t,o]=n.name.split(" ");if(!t||!o)throw new Error("Event name is invalid");++e.count;const{timestamp:s=Date.now(),group:r=e.group,count:i=e.count}=n,{name:a=`${t} ${o}`,data:c={},context:u={},globals:l=e.globals,custom:g={},user:f=e.user,nested:d=[],consent:m=e.consent,id:h=`${s}-${r}-${i}`,trigger:p="",entity:b=t,action:y=o,timing:v=0,version:k={source:e.version,tagging:e.config.tagging||0},source:w={type:"collector",id:"",previous_id:""}}=n;return{name:a,data:c,context:u,globals:l,custom:g,user:f,nested:d,consent:m,id:h,trigger:p,entity:b,action:y,timestamp:s,timing:v,group:r,count:i,version:k,source:w}}async function I(e,n){e.allowed=!0,e.count=0,e.group=(0,x.getId)(),e.timing=Date.now(),n&&(n.consent&&(e.consent=(0,x.assign)(e.consent,n.consent)),n.user&&(e.user=(0,x.assign)(e.user,n.user)),n.globals&&(e.globals=(0,x.assign)(e.config.globalsStatic||{},n.globals)),n.custom&&(e.custom=(0,x.assign)(e.custom,n.custom))),Object.values(e.destinations).forEach(e=>{e.queuePush=[]}),e.queue=[],e.round++;const t=await O(e);return h(e,"run"),t}var M=require("@walkeros/core");function T(e,n){return(0,M.useHooks)(async(t,o={})=>await(0,M.tryCatchAsync)(async()=>{const{id:s,ingest:r,mapping:i,preChain:a}=o;let c=t;const u=r?Object.freeze(r):void 0;if(i){const n=await(0,M.processEventMapping)(c,i,e);if(n.ignore)return P({ok:!0});if(i.consent){if(!(0,M.getGrantedConsent)(i.consent,e.consent,n.event.consent))return P({ok:!0})}c=n.event}if(a?.length&&e.transformers&&Object.keys(e.transformers).length>0){const n=await k(e,e.transformers,a,c,u);if(null===n)return P({ok:!0});c=n}const l=n(c),g=R(e,l);return await O(e,g,{id:s,ingest:u})},()=>P({ok:!1}))(),"Push",e.hooks)}var F=require("@walkeros/core");async function G(e){const n=(0,S.assign)({globalsStatic:{},sessionStatic:{},tagging:0,run:!0},e,{merge:!1,extend:!1}),t={level:e.logger?.level,handler:e.logger?.handler},o=(0,S.createLogger)(t),s={...n.globalsStatic,...e.globals},r={allowed:!1,config:n,consent:e.consent||{},count:0,custom:e.custom||{},destinations:{},transformers:{},transformerChain:{pre:[],post:{}},globals:s,group:"",hooks:{},logger:o,on:{},queue:[],round:0,session:void 0,timing:Date.now(),user:e.user||{},version:"1.1.0",sources:{},push:void 0,command:void 0};return r.push=T(r,e=>({timing:Math.round((Date.now()-r.timing)/10)/100,source:{type:"collector",id:"",previous_id:""},...e})),r.command=function(e,n){return(0,F.useHooks)(async(t,o,s)=>await(0,F.tryCatchAsync)(async()=>await n(e,t,o,s),()=>P({ok:!1}))(),"Command",e.hooks)}(r,H),r.destinations=await A(0,e.destinations||{}),r.transformers=await async function(e,n={}){const t={};for(const[o,s]of Object.entries(n)){const{code:n,config:r={},env:i={}}=s,a=e.logger.scope("transformer").scope(o),c={collector:e,logger:a,id:o,config:r,env:i},u=await n(c);t[o]=u}return t}(r,e.transformers||{}),r}var _=require("@walkeros/core");function B(e){const n={};for(const[t,o]of Object.entries(e))n[t]={next:o.config.next};return n}async function L(e,n={}){const t={};for(const[o,s]of Object.entries(n)){const{code:n,config:r={},env:i={},primary:a,next:c}=s;let u;const l=b(c,B(e.transformers)),g=(n,t={})=>e.push(n,{...t,id:o,ingest:u,mapping:r,preChain:l}),f=e.logger.scope("source").scope(o),d={push:g,command:e.command,sources:e.sources,elb:e.sources.elb.push,logger:f,...i},m={collector:e,logger:f,id:o,config:r,env:d,setIngest:async n=>{u=r.ingest?await(0,_.getMappingValue)(n,r.ingest,{collector:e}):void 0}},h=await(0,_.tryCatchAsync)(n)(m);if(!h)continue;const p=h.type||"unknown",y=e.logger.scope(p).scope(o);d.logger=y,a&&(h.config={...h.config,primary:a}),t[o]=h}return t}async function U(e){e=e||{};const n=await G(e),t=(o=n,{type:"elb",config:{},push:async(e,n,t,s,r,i)=>{if("string"==typeof e&&e.startsWith("walker ")){const s=e.replace("walker ","");return o.command(s,n,t)}let a;if("string"==typeof e)a={name:e},n&&"object"==typeof n&&!Array.isArray(n)&&(a.data=n);else{if(!e||"object"!=typeof e)return P({ok:!1});a=e,n&&"object"==typeof n&&!Array.isArray(n)&&(a.data={...a.data||{},...n})}return s&&"object"==typeof s&&(a.context=s),r&&Array.isArray(r)&&(a.nested=r),i&&"object"==typeof i&&(a.custom=i),o.push(a)}});var o;n.sources.elb=t;const s=await L(n,e.sources||{});Object.assign(n.sources,s);const{consent:r,user:i,globals:a,custom:c}=e;r&&await n.command("consent",r),i&&await n.command("user",i),a&&Object.assign(n.globals,a),c&&Object.assign(n.custom,c),n.config.run&&await n.command("run");let u=t.push;const l=Object.values(n.sources).filter(e=>"elb"!==e.type),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"},o={Commands:e,Utils:{Storage:{Cookie:"cookie",Local:"local",Session:"session"}}};import{assign as t}from"@walkeros/core";import{assign as s,clone as r,debounce as i,getId as a,getGrantedConsent as c,isDefined as u,isObject as l,processEventMapping as g,tryCatchAsync as f,useHooks as d}from"@walkeros/core";import{isArray as m}from"@walkeros/core";import{tryCatch as p}from"@walkeros/core";function h(n,e,o){const t=n.on,s=t[e]||[],r=m(o)?o:[o];r.forEach(n=>{s.push(n)}),t[e]=s,y(n,e,r)}function b(n,e,o,t,s){if(!e.on)return;const r=e.type||"unknown",i=n.logger.scope(r).scope("on").scope(t),a={collector:n,logger:i,id:o,config:e.config,data:s,env:R(e.env,e.config.env)};p(e.on)(t,a)}function y(n,e,t,s){let r,i=t||[];switch(t||(i=n.on[e]||[]),e){case o.Commands.Consent:r=s||n.consent;break;case o.Commands.Session:r=n.session;break;case o.Commands.Ready:case o.Commands.Run:default:r=void 0}if(Object.values(n.sources).forEach(n=>{n.on&&p(n.on)(e,r)}),Object.entries(n.destinations).forEach(([o,t])=>{if(t.on){if(!t.config.init)return t.queueOn=t.queueOn||[],void t.queueOn.push({type:e,data:r});b(n,t,o,e,r)}}),i.length)switch(e){case o.Commands.Consent:!function(n,e,o){const t=o||n.consent;e.forEach(e=>{Object.keys(t).filter(n=>n in e).forEach(o=>{p(e[o])(n,t)})})}(n,i,s);break;case o.Commands.Ready:case o.Commands.Run:!function(n,e){n.allowed&&e.forEach(e=>{p(e)(n)})}(n,i);break;case o.Commands.Session:!function(n,e){if(!n.session)return;e.forEach(e=>{p(e)(n,n.session)})}(n,i)}}import{isObject as k,tryCatchAsync as w,useHooks as v}from"@walkeros/core";function C(n,e={}){if(!n)return[];if(Array.isArray(n))return n;const o=[],t=new Set;let s=n;for(;s&&e[s]&&!t.has(s);){t.add(s),o.push(s);const n=e[s].next;if(Array.isArray(n)){o.push(...n);break}s=n}return o}async function O(n,e,o){if(e.init&&!e.config.init){const t=e.type||"unknown",s=n.logger.scope(`transformer:${t}`),r={collector:n,logger:s,id:o,config:e.config,env:A(e.config.env)};s.debug("init");const i=await v(e.init,"TransformerInit",n.hooks)(r);if(!1===i)return!1;e.config={...i||e.config,init:!0},s.debug("init done")}return!0}async function q(n,e,o,t,s){const r=e.type||"unknown",i=n.logger.scope(`transformer:${r}`),a={collector:n,logger:i,id:o,ingest:s,config:e.config,env:A(e.config.env)};i.debug("push",{event:t.name});const c=await v(e.push,"TransformerPush",n.hooks)(t,a);return i.debug("push done"),c}async function j(n,e,o,t,s){let r=t;for(const t of o){const o=e[t];if(!o){n.logger.info(`Transformer not found: ${t}`);continue}if(!await w(O)(n,o,t))return n.logger.info(`Transformer init failed: ${t}`),null;const i=await w(q,e=>(n.logger.scope(`transformer:${o.type||"unknown"}`).error("Push failed",{error:e}),!1))(n,o,t,r,s);if(!1===i)return null;void 0!==i&&(r=i)}return r}function A(n){return n&&k(n)?n:{}}async function P(n,e,o){const{code:t,config:s={},env:r={}}=e,i=o||s||{init:!1},c={...t,config:i,env:R(t.env,r)};let u=c.config.id;if(!u)do{u=a(4)}while(n.destinations[u]);return n.destinations[u]=c,!1!==c.config.queue&&(c.queuePush=[...n.queue]),E(n,void 0,{},{[u]:c})}async function E(n,e,o={},t){const{allowed:i,consent:a,globals:u,user:l}=n;if(!i)return $({ok:!1});e&&n.queue.push(e),t||(t=n.destinations);const g=await Promise.all(Object.entries(t||{}).map(async([t,i])=>{let g=(i.queuePush||[]).map(n=>({...n,consent:a}));if(i.queuePush=[],e){const n=r(e);g.push(n)}if(!g.length&&!i.queueOn?.length)return{id:t,destination:i,skipped:!0};if(!g.length&&i.queueOn?.length){const e=await f(x)(n,i,t);return{id:t,destination:i,skipped:!e}}const d=[],m=g.filter(n=>{const e=c(i.config.consent,a,n.consent);return!e||(n.consent=e,d.push(n),!1)});if(i.queuePush.concat(m),!d.length)return{id:t,destination:i,queue:g};if(!await f(x)(n,i,t))return{id:t,destination:i,queue:g};let p,h;i.dlq||(i.dlq=[]);const b=n.transformerChain?.post?.[t]||[];return await Promise.all(d.map(async e=>{e.globals=s(u,e.globals),e.user=s(l,e.user);let r=e;if(b.length>0&&n.transformers&&Object.keys(n.transformers).length>0){const t=await j(n,n.transformers,b,e,o.ingest);if(null===t)return e;r=t}const a=await f(S,e=>{const o=i.type||"unknown";n.logger.scope(o).error("Push failed",{error:e,event:r.name}),p=e,i.dlq.push([r,e])})(n,i,t,r,o.ingest);return void 0!==a&&(h=a),e})),{id:t,destination:i,error:p,response:h}})),d={},m={},p={};for(const n of g){if(n.skipped)continue;const e=n.destination,o={type:e.type||"unknown",data:n.response};n.error?(o.error=n.error,p[n.id]=o):n.queue&&n.queue.length?(e.queuePush=(e.queuePush||[]).concat(n.queue),m[n.id]=o):d[n.id]=o}return $({event:e,...Object.keys(d).length&&{done:d},...Object.keys(m).length&&{queued:m},...Object.keys(p).length&&{failed:p}})}async function x(n,e,o){if(e.init&&!e.config.init){const t=e.type||"unknown",s=n.logger.scope(t),r={collector:n,logger:s,id:o,config:e.config,env:R(e.env,e.config.env)};s.debug("init");const i=await d(e.init,"DestinationInit",n.hooks)(r);if(!1===i)return i;if(e.config={...i||e.config,init:!0},e.queueOn?.length){const t=e.queueOn;e.queueOn=[];for(const{type:s,data:r}of t)b(n,e,o,s,r)}s.debug("init done")}return!0}async function S(n,e,o,t,s){const{config:r}=e,a=await g(t,r,n);if(a.ignore)return!1;const c=e.type||"unknown",l=n.logger.scope(c),f={collector:n,logger:l,id:o,config:r,data:a.data,rule:a.mapping,ingest:s,env:R(e.env,r.env)},m=a.mapping,p=a.mappingKey||"* *";if(!m?.batch||!e.pushBatch){l.debug("push",{event:a.event.name});const o=await d(e.push,"DestinationPush",n.hooks)(a.event,f);return l.debug("push done"),o}{if(e.batches=e.batches||{},!e.batches[p]){const t={key:p,events:[],data:[]};e.batches[p]={batched:t,batchFn:i(()=>{const t=e.batches[p].batched,i={collector:n,logger:l,id:o,config:r,data:void 0,rule:m,ingest:s,env:R(e.env,r.env)};l.debug("push batch",{events:t.events.length}),d(e.pushBatch,"DestinationPushBatch",n.hooks)(t,i),l.debug("push batch done"),t.events=[],t.data=[]},m.batch)}}const t=e.batches[p];t.batched.events.push(a.event),u(a.data)&&t.batched.data.push(a.data),t.batchFn()}return!0}function $(n){return{ok:!n?.failed,...n}}async function D(n,e={}){const o={};for(const[n,t]of Object.entries(e)){const{code:e,config:s={},env:r={}}=t,i={...e.config,...s},a=R(e.env,r);o[n]={...e,config:i,env:a}}return o}function R(n,e){return n||e?e?n&&l(n)&&l(e)?{...n,...e}:e:n:{}}async function I(n,e){const{consent:o}=n;let s=!1;const r={};return Object.entries(e).forEach(([n,e])=>{const o=!!e;r[n]=o,s=s||o}),n.consent=t(o,r),y(n,"consent",void 0,r),s?E(n):$({ok:!0})}import{assign as H,createLogger as T}from"@walkeros/core";import{assign as B,getId as G,isFunction as U,isString as F}from"@walkeros/core";import{isObject as L}from"@walkeros/core";async function M(n,e,t,s){let r;switch(e){case o.Commands.Config:L(t)&&B(n.config,t,{shallow:!1});break;case o.Commands.Consent:L(t)&&(r=await I(n,t));break;case o.Commands.Custom:L(t)&&(n.custom=B(n.custom,t));break;case o.Commands.Destination:L(t)&&U(t.push)&&(r=await P(n,{code:t},s));break;case o.Commands.Globals:L(t)&&(n.globals=B(n.globals,t));break;case o.Commands.On:F(t)&&h(n,t,s);break;case o.Commands.Ready:y(n,"ready");break;case o.Commands.Run:r=await _(n,t);break;case o.Commands.Session:y(n,"session");break;case o.Commands.User:L(t)&&B(n.user,t,{shallow:!1})}return r||$({ok:!0})}function W(n,e){if(!e.name)throw new Error("Event name is required");const[o,t]=e.name.split(" ");if(!o||!t)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=`${o} ${t}`,data:c={},context:u={},globals:l=n.globals,custom:g={},user:f=n.user,nested:d=[],consent:m=n.consent,id:p=`${s}-${r}-${i}`,trigger:h="",entity:b=o,action:y=t,timing:k=0,version:w={source:n.version,tagging:n.config.tagging||0},source:v={type:"collector",id:"",previous_id:""}}=e;return{name:a,data:c,context:u,globals:l,custom:g,user:f,nested:d,consent:m,id:p,trigger:h,entity:b,action:y,timestamp:s,timing:k,group:r,count:i,version:w,source:v}}async function _(n,e){n.allowed=!0,n.count=0,n.group=G(),n.timing=Date.now(),e&&(e.consent&&(n.consent=B(n.consent,e.consent)),e.user&&(n.user=B(n.user,e.user)),e.globals&&(n.globals=B(n.config.globalsStatic||{},e.globals)),e.custom&&(n.custom=B(n.custom,e.custom))),Object.values(n.destinations).forEach(n=>{n.queuePush=[]}),n.queue=[],n.round++;const o=await E(n);return y(n,"run"),o}import{getGrantedConsent as z,processEventMapping as K,tryCatchAsync as J,useHooks as N}from"@walkeros/core";function Q(n,e){return N(async(o,t={})=>await J(async()=>{const{id:s,ingest:r,mapping:i,preChain:a}=t;let c=o;const u=r?Object.freeze(r):void 0;if(i){const e=await K(c,i,n);if(e.ignore)return $({ok:!0});if(i.consent){if(!z(i.consent,n.consent,e.event.consent))return $({ok:!0})}c=e.event}if(a?.length&&n.transformers&&Object.keys(n.transformers).length>0){const e=await j(n,n.transformers,a,c,u);if(null===e)return $({ok:!0});c=e}const l=e(c),g=W(n,l);return await E(n,g,{id:s,ingest:u})},()=>$({ok:!1}))(),"Push",n.hooks)}import{useHooks as V,tryCatchAsync as X}from"@walkeros/core";async function Y(n){const e=H({globalsStatic:{},sessionStatic:{},tagging:0,run:!0},n,{merge:!1,extend:!1}),o={level:n.logger?.level,handler:n.logger?.handler},t=T(o),s={...e.globalsStatic,...n.globals},r={allowed:!1,config:e,consent:n.consent||{},count:0,custom:n.custom||{},destinations:{},transformers:{},transformerChain:{pre:[],post:{}},globals:s,group:"",hooks:{},logger:t,on:{},queue:[],round:0,session:void 0,timing:Date.now(),user:n.user||{},version:"1.0.1",sources:{},push:void 0,command:void 0};return r.push=Q(r,n=>({timing:Math.round((Date.now()-r.timing)/10)/100,source:{type:"collector",id:"",previous_id:""},...n})),r.command=function(n,e){return V(async(o,t,s)=>await X(async()=>await e(n,o,t,s),()=>$({ok:!1}))(),"Command",n.hooks)}(r,M),r.destinations=await D(0,n.destinations||{}),r.transformers=await async function(n,e={}){const o={};for(const[t,s]of Object.entries(e)){const{code:e,config:r={},env:i={}}=s,a=n.logger.scope("transformer").scope(t),c={collector:n,logger:a,id:t,config:r,env:i},u=await e(c);o[t]=u}return o}(r,n.transformers||{}),r}import{getMappingValue as Z,tryCatchAsync as nn}from"@walkeros/core";function en(n){const e={};for(const[o,t]of Object.entries(n))e[o]={next:t.config.next};return e}async function on(n,e={}){const o={};for(const[t,s]of Object.entries(e)){const{code:e,config:r={},env:i={},primary:a,next:c}=s;let u;const l=C(c,en(n.transformers)),g=(e,o={})=>n.push(e,{...o,id:t,ingest:u,mapping:r,preChain:l}),f=n.logger.scope("source").scope(t),d={push:g,command:n.command,sources:n.sources,elb:n.sources.elb.push,logger:f,...i},m={collector:n,logger:f,id:t,config:r,env:d,setIngest:async e=>{u=r.ingest?await Z(e,r.ingest,{collector:n}):void 0}},p=await nn(e)(m);if(!p)continue;const h=p.type||"unknown",b=n.logger.scope(h).scope(t);d.logger=b,a&&(p.config={...p.config,primary:a}),o[t]=p}return o}async function tn(n){n=n||{};const e=await Y(n),o=(t=e,{type:"elb",config:{},push:async(n,e,o,s,r,i)=>{if("string"==typeof n&&n.startsWith("walker ")){const s=n.replace("walker ","");return t.command(s,e,o)}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 $({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),t.push(a)}});var t;e.sources.elb=o;const s=await on(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=o.push;const l=Object.values(e.sources).filter(n=>"elb"!==n.type),g=l.find(n=>n.config.primary);return g?u=g.push:l.length>0&&(u=l[0].push),{collector:e,elb:u}}export{n as Code,e as Commands,o as Const,P as addDestination,b as callDestinationOn,M as commonHandleCommand,W as createEvent,Q as createPush,$ as createPushResult,x as destinationInit,S as destinationPush,D as initDestinations,on as initSources,R as mergeEnvironments,h as on,y as onApply,E as pushToDestinations,_ as runCollector,I as setConsent,tn as startFlow};//# 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"},o={Commands:e,Utils:{Storage:{Cookie:"cookie",Local:"local",Session:"session"}}};import{assign as t}from"@walkeros/core";import{assign as s,clone as r,debounce as i,getId as a,getGrantedConsent as c,isDefined as u,isObject as l,processEventMapping as g,tryCatchAsync as f,useHooks as d}from"@walkeros/core";import{isArray as m}from"@walkeros/core";import{tryCatch as p}from"@walkeros/core";function h(n,e,o){const t=n.on,s=t[e]||[],r=m(o)?o:[o];r.forEach(n=>{s.push(n)}),t[e]=s,y(n,e,r)}function b(n,e,o,t,s){if(!e.on)return;const r=e.type||"unknown",i=n.logger.scope(r).scope("on").scope(t),a={collector:n,logger:i,id:o,config:e.config,data:s,env:R(e.env,e.config.env)};p(e.on)(t,a)}function y(n,e,t,s){let r,i=t||[];switch(t||(i=n.on[e]||[]),e){case o.Commands.Consent:r=s||n.consent;break;case o.Commands.Session:r=n.session;break;case o.Commands.Ready:case o.Commands.Run:default:r=void 0}if(Object.values(n.sources).forEach(n=>{n.on&&p(n.on)(e,r)}),Object.entries(n.destinations).forEach(([o,t])=>{if(t.on){if(!t.config.init)return t.queueOn=t.queueOn||[],void t.queueOn.push({type:e,data:r});b(n,t,o,e,r)}}),i.length)switch(e){case o.Commands.Consent:!function(n,e,o){const t=o||n.consent;e.forEach(e=>{Object.keys(t).filter(n=>n in e).forEach(o=>{p(e[o])(n,t)})})}(n,i,s);break;case o.Commands.Ready:case o.Commands.Run:!function(n,e){n.allowed&&e.forEach(e=>{p(e)(n)})}(n,i);break;case o.Commands.Session:!function(n,e){if(!n.session)return;e.forEach(e=>{p(e)(n,n.session)})}(n,i)}}import{isObject as k,tryCatchAsync as w,useHooks as v}from"@walkeros/core";function C(n,e={}){if(!n)return[];if(Array.isArray(n))return n;const o=[],t=new Set;let s=n;for(;s&&e[s]&&!t.has(s);){t.add(s),o.push(s);const n=e[s].next;if(Array.isArray(n)){o.push(...n);break}s=n}return o}async function O(n,e,o){if(e.init&&!e.config.init){const t=e.type||"unknown",s=n.logger.scope(`transformer:${t}`),r={collector:n,logger:s,id:o,config:e.config,env:A(e.config.env)};s.debug("init");const i=await v(e.init,"TransformerInit",n.hooks)(r);if(!1===i)return!1;e.config={...i||e.config,init:!0},s.debug("init done")}return!0}async function q(n,e,o,t,s){const r=e.type||"unknown",i=n.logger.scope(`transformer:${r}`),a={collector:n,logger:i,id:o,ingest:s,config:e.config,env:A(e.config.env)};i.debug("push",{event:t.name});const c=await v(e.push,"TransformerPush",n.hooks)(t,a);return i.debug("push done"),c}async function j(n,e,o,t,s){let r=t;for(const t of o){const o=e[t];if(!o){n.logger.info(`Transformer not found: ${t}`);continue}if(!await w(O)(n,o,t))return n.logger.info(`Transformer init failed: ${t}`),null;const i=await w(q,e=>(n.logger.scope(`transformer:${o.type||"unknown"}`).error("Push failed",{error:e}),!1))(n,o,t,r,s);if(!1===i)return null;void 0!==i&&(r=i)}return r}function A(n){return n&&k(n)?n:{}}async function P(n,e,o){const{code:t,config:s={},env:r={}}=e,i=o||s||{init:!1},c={...t,config:i,env:R(t.env,r)};let u=c.config.id;if(!u)do{u=a(4)}while(n.destinations[u]);return n.destinations[u]=c,!1!==c.config.queue&&(c.queuePush=[...n.queue]),E(n,void 0,{},{[u]:c})}async function E(n,e,o={},t){const{allowed:i,consent:a,globals:u,user:l}=n;if(!i)return $({ok:!1});e&&n.queue.push(e),t||(t=n.destinations);const g=await Promise.all(Object.entries(t||{}).map(async([t,i])=>{let g=(i.queuePush||[]).map(n=>({...n,consent:a}));if(i.queuePush=[],e){const n=r(e);g.push(n)}if(!g.length&&!i.queueOn?.length)return{id:t,destination:i,skipped:!0};if(!g.length&&i.queueOn?.length){const e=await f(x)(n,i,t);return{id:t,destination:i,skipped:!e}}const d=[],m=g.filter(n=>{const e=c(i.config.consent,a,n.consent);return!e||(n.consent=e,d.push(n),!1)});if(i.queuePush.concat(m),!d.length)return{id:t,destination:i,queue:g};if(!await f(x)(n,i,t))return{id:t,destination:i,queue:g};let p,h;i.dlq||(i.dlq=[]);const b=n.transformerChain?.post?.[t]||[];return await Promise.all(d.map(async e=>{e.globals=s(u,e.globals),e.user=s(l,e.user);let r=e;if(b.length>0&&n.transformers&&Object.keys(n.transformers).length>0){const t=await j(n,n.transformers,b,e,o.ingest);if(null===t)return e;r=t}const a=await f(S,e=>{const o=i.type||"unknown";n.logger.scope(o).error("Push failed",{error:e,event:r.name}),p=e,i.dlq.push([r,e])})(n,i,t,r,o.ingest);return void 0!==a&&(h=a),e})),{id:t,destination:i,error:p,response:h}})),d={},m={},p={};for(const n of g){if(n.skipped)continue;const e=n.destination,o={type:e.type||"unknown",data:n.response};n.error?(o.error=n.error,p[n.id]=o):n.queue&&n.queue.length?(e.queuePush=(e.queuePush||[]).concat(n.queue),m[n.id]=o):d[n.id]=o}return $({event:e,...Object.keys(d).length&&{done:d},...Object.keys(m).length&&{queued:m},...Object.keys(p).length&&{failed:p}})}async function x(n,e,o){if(e.init&&!e.config.init){const t=e.type||"unknown",s=n.logger.scope(t),r={collector:n,logger:s,id:o,config:e.config,env:R(e.env,e.config.env)};s.debug("init");const i=await d(e.init,"DestinationInit",n.hooks)(r);if(!1===i)return i;if(e.config={...i||e.config,init:!0},e.queueOn?.length){const t=e.queueOn;e.queueOn=[];for(const{type:s,data:r}of t)b(n,e,o,s,r)}s.debug("init done")}return!0}async function S(n,e,o,t,s){const{config:r}=e,a=await g(t,r,n);if(a.ignore)return!1;const c=e.type||"unknown",l=n.logger.scope(c),f={collector:n,logger:l,id:o,config:r,data:a.data,rule:a.mapping,ingest:s,env:R(e.env,r.env)},m=a.mapping,p=a.mappingKey||"* *";if(!m?.batch||!e.pushBatch){l.debug("push",{event:a.event.name});const o=await d(e.push,"DestinationPush",n.hooks)(a.event,f);return l.debug("push done"),o}{if(e.batches=e.batches||{},!e.batches[p]){const t={key:p,events:[],data:[]};e.batches[p]={batched:t,batchFn:i(()=>{const t=e.batches[p].batched,i={collector:n,logger:l,id:o,config:r,data:void 0,rule:m,ingest:s,env:R(e.env,r.env)};l.debug("push batch",{events:t.events.length}),d(e.pushBatch,"DestinationPushBatch",n.hooks)(t,i),l.debug("push batch done"),t.events=[],t.data=[]},m.batch)}}const t=e.batches[p];t.batched.events.push(a.event),u(a.data)&&t.batched.data.push(a.data),t.batchFn()}return!0}function $(n){return{ok:!n?.failed,...n}}async function D(n,e={}){const o={};for(const[n,t]of Object.entries(e)){const{code:e,config:s={},env:r={}}=t,i={...e.config,...s},a=R(e.env,r);o[n]={...e,config:i,env:a}}return o}function R(n,e){return n||e?e?n&&l(n)&&l(e)?{...n,...e}:e:n:{}}async function I(n,e){const{consent:o}=n;let s=!1;const r={};return Object.entries(e).forEach(([n,e])=>{const o=!!e;r[n]=o,s=s||o}),n.consent=t(o,r),y(n,"consent",void 0,r),s?E(n):$({ok:!0})}import{assign as H,createLogger as T}from"@walkeros/core";import{assign as B,getId as G,isFunction as U,isString as F}from"@walkeros/core";import{isObject as L}from"@walkeros/core";async function M(n,e,t,s){let r;switch(e){case o.Commands.Config:L(t)&&B(n.config,t,{shallow:!1});break;case o.Commands.Consent:L(t)&&(r=await I(n,t));break;case o.Commands.Custom:L(t)&&(n.custom=B(n.custom,t));break;case o.Commands.Destination:L(t)&&U(t.push)&&(r=await P(n,{code:t},s));break;case o.Commands.Globals:L(t)&&(n.globals=B(n.globals,t));break;case o.Commands.On:F(t)&&h(n,t,s);break;case o.Commands.Ready:y(n,"ready");break;case o.Commands.Run:r=await _(n,t);break;case o.Commands.Session:y(n,"session");break;case o.Commands.User:L(t)&&B(n.user,t,{shallow:!1})}return r||$({ok:!0})}function W(n,e){if(!e.name)throw new Error("Event name is required");const[o,t]=e.name.split(" ");if(!o||!t)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=`${o} ${t}`,data:c={},context:u={},globals:l=n.globals,custom:g={},user:f=n.user,nested:d=[],consent:m=n.consent,id:p=`${s}-${r}-${i}`,trigger:h="",entity:b=o,action:y=t,timing:k=0,version:w={source:n.version,tagging:n.config.tagging||0},source:v={type:"collector",id:"",previous_id:""}}=e;return{name:a,data:c,context:u,globals:l,custom:g,user:f,nested:d,consent:m,id:p,trigger:h,entity:b,action:y,timestamp:s,timing:k,group:r,count:i,version:w,source:v}}async function _(n,e){n.allowed=!0,n.count=0,n.group=G(),n.timing=Date.now(),e&&(e.consent&&(n.consent=B(n.consent,e.consent)),e.user&&(n.user=B(n.user,e.user)),e.globals&&(n.globals=B(n.config.globalsStatic||{},e.globals)),e.custom&&(n.custom=B(n.custom,e.custom))),Object.values(n.destinations).forEach(n=>{n.queuePush=[]}),n.queue=[],n.round++;const o=await E(n);return y(n,"run"),o}import{getGrantedConsent as z,processEventMapping as K,tryCatchAsync as J,useHooks as N}from"@walkeros/core";function Q(n,e){return N(async(o,t={})=>await J(async()=>{const{id:s,ingest:r,mapping:i,preChain:a}=t;let c=o;const u=r?Object.freeze(r):void 0;if(i){const e=await K(c,i,n);if(e.ignore)return $({ok:!0});if(i.consent){if(!z(i.consent,n.consent,e.event.consent))return $({ok:!0})}c=e.event}if(a?.length&&n.transformers&&Object.keys(n.transformers).length>0){const e=await j(n,n.transformers,a,c,u);if(null===e)return $({ok:!0});c=e}const l=e(c),g=W(n,l);return await E(n,g,{id:s,ingest:u})},()=>$({ok:!1}))(),"Push",n.hooks)}import{useHooks as V,tryCatchAsync as X}from"@walkeros/core";async function Y(n){const e=H({globalsStatic:{},sessionStatic:{},tagging:0,run:!0},n,{merge:!1,extend:!1}),o={level:n.logger?.level,handler:n.logger?.handler},t=T(o),s={...e.globalsStatic,...n.globals},r={allowed:!1,config:e,consent:n.consent||{},count:0,custom:n.custom||{},destinations:{},transformers:{},transformerChain:{pre:[],post:{}},globals:s,group:"",hooks:{},logger:t,on:{},queue:[],round:0,session:void 0,timing:Date.now(),user:n.user||{},version:"1.1.0",sources:{},push:void 0,command:void 0};return r.push=Q(r,n=>({timing:Math.round((Date.now()-r.timing)/10)/100,source:{type:"collector",id:"",previous_id:""},...n})),r.command=function(n,e){return V(async(o,t,s)=>await X(async()=>await e(n,o,t,s),()=>$({ok:!1}))(),"Command",n.hooks)}(r,M),r.destinations=await D(0,n.destinations||{}),r.transformers=await async function(n,e={}){const o={};for(const[t,s]of Object.entries(e)){const{code:e,config:r={},env:i={}}=s,a=n.logger.scope("transformer").scope(t),c={collector:n,logger:a,id:t,config:r,env:i},u=await e(c);o[t]=u}return o}(r,n.transformers||{}),r}import{getMappingValue as Z,tryCatchAsync as nn}from"@walkeros/core";function en(n){const e={};for(const[o,t]of Object.entries(n))e[o]={next:t.config.next};return e}async function on(n,e={}){const o={};for(const[t,s]of Object.entries(e)){const{code:e,config:r={},env:i={},primary:a,next:c}=s;let u;const l=C(c,en(n.transformers)),g=(e,o={})=>n.push(e,{...o,id:t,ingest:u,mapping:r,preChain:l}),f=n.logger.scope("source").scope(t),d={push:g,command:n.command,sources:n.sources,elb:n.sources.elb.push,logger:f,...i},m={collector:n,logger:f,id:t,config:r,env:d,setIngest:async e=>{u=r.ingest?await Z(e,r.ingest,{collector:n}):void 0}},p=await nn(e)(m);if(!p)continue;const h=p.type||"unknown",b=n.logger.scope(h).scope(t);d.logger=b,a&&(p.config={...p.config,primary:a}),o[t]=p}return o}async function tn(n){n=n||{};const e=await Y(n),o=(t=e,{type:"elb",config:{},push:async(n,e,o,s,r,i)=>{if("string"==typeof n&&n.startsWith("walker ")){const s=n.replace("walker ","");return t.command(s,e,o)}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 $({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),t.push(a)}});var t;e.sources.elb=o;const s=await on(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=o.push;const l=Object.values(e.sources).filter(n=>"elb"!==n.type),g=l.find(n=>n.config.primary);return g?u=g.push:l.length>0&&(u=l[0].push),{collector:e,elb:u}}export{n as Code,e as Commands,o as Const,P as addDestination,b as callDestinationOn,M as commonHandleCommand,W as createEvent,Q as createPush,$ as createPushResult,x as destinationInit,S as destinationPush,D as initDestinations,on as initSources,R as mergeEnvironments,h as on,y as onApply,E as pushToDestinations,_ as runCollector,I as setConsent,tn as startFlow};//# 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": "1.1.
|
|
4
|
+
"version": "1.1.1",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"update": "npx npm-check-updates -u && npm update"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@walkeros/core": "1.2.
|
|
33
|
+
"@walkeros/core": "1.2.1"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {},
|
|
36
36
|
"repository": {
|