@walkeros/collector 1.1.2 → 1.2.0

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.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { WalkerOS, Collector, Elb, Mapping, Destination, On, Source } from '@walkeros/core';
1
+ import { WalkerOS, Collector, Elb, Mapping, Destination, On, Source, Transformer } from '@walkeros/core';
2
2
 
3
3
  interface RunState {
4
4
  consent?: WalkerOS.Consent;
@@ -65,13 +65,13 @@ declare const Const: {
65
65
  };
66
66
 
67
67
  /**
68
- * Sets the consent state and processes the queue.
69
- *
70
- * @param collector - The walkerOS collector instance.
71
- * @param data - The consent data to set.
72
- * @returns The result of the push operation.
68
+ * Processes consent data: coerces to boolean, updates collector state.
69
+ * Does NOT notify or process queues — caller handles that.
73
70
  */
74
- declare function setConsent(collector: Collector.Instance, data: WalkerOS.Consent): Promise<Elb.PushResult>;
71
+ declare function processConsent(collector: Collector.Instance, data: WalkerOS.Consent): {
72
+ update: WalkerOS.Consent;
73
+ runQueue: boolean;
74
+ };
75
75
 
76
76
  declare function startFlow<ElbPush extends Elb.Fn = Elb.Fn>(initConfig?: Collector.InitConfig): Promise<StartFlow<ElbPush>>;
77
77
 
@@ -137,6 +137,12 @@ declare function destinationPush<Destination extends Destination.Instance>(colle
137
137
  * @returns The push result.
138
138
  */
139
139
  declare function createPushResult(partialResult?: Partial<Elb.PushResult>): Elb.PushResult;
140
+ /**
141
+ * Register a single destination from its init definition.
142
+ * Merges code config, user config, and chain config.
143
+ * Used by initDestinations and activatePending.
144
+ */
145
+ declare function registerDestination(def: Destination.Init): Destination.Instance;
140
146
  /**
141
147
  * Initializes a map of destinations using ONLY the unified code/config/env pattern.
142
148
  * Does NOT call destination.init() - that happens later during push with proper consent checks.
@@ -145,7 +151,7 @@ declare function createPushResult(partialResult?: Partial<Elb.PushResult>): Elb.
145
151
  * @param collector - The collector instance for destination init context.
146
152
  * @returns The initialized destinations.
147
153
  */
148
- declare function initDestinations(_collector: Collector.Instance, destinations?: Destination.InitDestinations): Promise<Collector.Destinations>;
154
+ declare function initDestinations(collector: Collector.Instance, destinations?: Destination.InitDestinations): Promise<Collector.Destinations>;
149
155
  /**
150
156
  * Merges destination environment with config environment
151
157
  * Config env takes precedence over destination env for overrides
@@ -186,7 +192,7 @@ declare function runCollector(collector: Collector.Instance, state?: RunState):
186
192
  * @param type The type of the event to listen for.
187
193
  * @param option The callback function or an array of callback functions.
188
194
  */
189
- declare function on(collector: Collector.Instance, type: On.Types, option: WalkerOS.SingleOrArray<On.Options>): void;
195
+ declare function on(collector: Collector.Instance, type: On.Types, option: WalkerOS.SingleOrArray<On.Options>): Promise<void>;
190
196
  /**
191
197
  * Calls a destination's on() handler with proper context.
192
198
  * Used by both onApply() for immediate calls and destinationInit() for flushing queued events.
@@ -200,15 +206,91 @@ declare function callDestinationOn(collector: Collector.Instance, destination: D
200
206
  * @param options The options for the callbacks.
201
207
  * @param config The consent configuration.
202
208
  */
203
- declare function onApply(collector: Collector.Instance, type: On.Types, options?: Array<On.Options>, config?: WalkerOS.Consent): void;
209
+ declare function onApply(collector: Collector.Instance, type: On.Types, options?: Array<On.Options>, config?: unknown): Promise<boolean>;
204
210
 
205
211
  /**
206
- * Initialize sources using the code/config/env pattern
207
- *
208
- * @param collector - The WalkerOS collector instance
209
- * @param sources - Map of source definitions with code/config/env
210
- * @returns Initialized sources
212
+ * Initialize a single source. Extracted from the initSources loop body
213
+ * so it can be reused by the pending-source activator.
214
+ */
215
+ declare function initSource(collector: Collector.Instance, sourceId: string, sourceDefinition: Source.InitSource): Promise<Source.Instance | undefined>;
216
+ /**
217
+ * Initialize sources. Sources with `require` are deferred to collector.pending.
211
218
  */
212
219
  declare function initSources(collector: Collector.Instance, sources?: Source.InitSources): Promise<Collector.Sources>;
213
220
 
214
- export { code as Code, type CommandTypes, Commands, Const, type CreateCollector, type HandleCommandFn, type RunState, type StartFlow, type StorageType, addDestination, callDestinationOn, commonHandleCommand, createEvent, createPush, createPushResult, destinationInit, destinationPush, initDestinations, initSources, mergeEnvironments, on, onApply, pushToDestinations, runCollector, setConsent, startFlow };
221
+ /**
222
+ * @module transformer
223
+ *
224
+ * Transformer Chain Utilities
225
+ * ==========================
226
+ *
227
+ * This module provides the unified implementation for transformer chains in walkerOS.
228
+ * Chains are used at two points in the data flow:
229
+ *
230
+ * 1. Pre-collector chains (source.next):
231
+ * Source → [Transformer Chain] → Collector
232
+ * Events are processed before the collector sees them.
233
+ *
234
+ * 2. Post-collector chains (destination.before):
235
+ * Collector → [Transformer Chain] → Destination
236
+ * Events are processed before reaching specific destinations.
237
+ *
238
+ * Key Functions:
239
+ * - extractTransformerNextMap(): Extracts next links from transformer instances
240
+ * - extractChainProperty(): Unified extraction of chain properties from definitions
241
+ * - walkChain(): Resolves chain IDs from starting point
242
+ * - runTransformerChain(): Executes a chain of transformers on an event
243
+ *
244
+ * Chain Resolution:
245
+ * - String start: Walk transformer.next links until chain ends
246
+ * - Array start: Use array directly (explicit chain, ignores transformer.next)
247
+ *
248
+ * Chain Termination:
249
+ * - Transformer returns false → chain stops, event is dropped
250
+ * - Transformer throws error → chain stops, event is dropped
251
+ * - Transformer returns void → continue with unchanged event
252
+ * - Transformer returns event → continue with modified event
253
+ */
254
+
255
+ /**
256
+ * Extracts transformer next configuration for chain walking.
257
+ * Maps transformer instances to their config.next values.
258
+ *
259
+ * This is the single source of truth for extracting chain links.
260
+ * Used by both source.ts (pre-collector chains) and destination.ts (post-collector chains).
261
+ *
262
+ * @param transformers - Map of transformer instances
263
+ * @returns Map of transformer IDs to their next configuration
264
+ */
265
+ declare function extractTransformerNextMap(transformers: Transformer.Transformers): Record<string, {
266
+ next?: string | string[];
267
+ }>;
268
+ /**
269
+ * Walks a transformer chain starting from a given transformer ID.
270
+ * Returns ordered array of transformer IDs in the chain.
271
+ *
272
+ * Used for on-demand chain resolution:
273
+ * - Called from destination.ts with destination.config.before
274
+ * - Called from source.ts with source.config.next
275
+ *
276
+ * @param startId - First transformer in chain, or explicit array of transformer IDs
277
+ * @param transformers - Available transformer configs with optional `next` field
278
+ * @returns Ordered array of transformer IDs to execute
279
+ *
280
+ * @example
281
+ * // Single transformer
282
+ * walkChain('redact', { redact: {} }) // ['redact']
283
+ *
284
+ * @example
285
+ * // Chain via next
286
+ * walkChain('a', { a: { next: 'b' }, b: { next: 'c' }, c: {} }) // ['a', 'b', 'c']
287
+ *
288
+ * @example
289
+ * // Explicit array
290
+ * walkChain(['x', 'y'], {}) // ['x', 'y']
291
+ */
292
+ declare function walkChain(startId: string | string[] | undefined, transformers?: Record<string, {
293
+ next?: string | string[];
294
+ }>): string[];
295
+
296
+ export { code as Code, type CommandTypes, Commands, Const, type CreateCollector, type HandleCommandFn, type RunState, type StartFlow, type StorageType, addDestination, callDestinationOn, commonHandleCommand, createEvent, createPush, createPushResult, destinationInit, destinationPush, extractTransformerNextMap, initDestinations, initSource, initSources, mergeEnvironments, on, onApply, processConsent, pushToDestinations, registerDestination, runCollector, startFlow, walkChain };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { WalkerOS, Collector, Elb, Mapping, Destination, On, Source } from '@walkeros/core';
1
+ import { WalkerOS, Collector, Elb, Mapping, Destination, On, Source, Transformer } from '@walkeros/core';
2
2
 
3
3
  interface RunState {
4
4
  consent?: WalkerOS.Consent;
@@ -65,13 +65,13 @@ declare const Const: {
65
65
  };
66
66
 
67
67
  /**
68
- * Sets the consent state and processes the queue.
69
- *
70
- * @param collector - The walkerOS collector instance.
71
- * @param data - The consent data to set.
72
- * @returns The result of the push operation.
68
+ * Processes consent data: coerces to boolean, updates collector state.
69
+ * Does NOT notify or process queues — caller handles that.
73
70
  */
74
- declare function setConsent(collector: Collector.Instance, data: WalkerOS.Consent): Promise<Elb.PushResult>;
71
+ declare function processConsent(collector: Collector.Instance, data: WalkerOS.Consent): {
72
+ update: WalkerOS.Consent;
73
+ runQueue: boolean;
74
+ };
75
75
 
76
76
  declare function startFlow<ElbPush extends Elb.Fn = Elb.Fn>(initConfig?: Collector.InitConfig): Promise<StartFlow<ElbPush>>;
77
77
 
@@ -137,6 +137,12 @@ declare function destinationPush<Destination extends Destination.Instance>(colle
137
137
  * @returns The push result.
138
138
  */
139
139
  declare function createPushResult(partialResult?: Partial<Elb.PushResult>): Elb.PushResult;
140
+ /**
141
+ * Register a single destination from its init definition.
142
+ * Merges code config, user config, and chain config.
143
+ * Used by initDestinations and activatePending.
144
+ */
145
+ declare function registerDestination(def: Destination.Init): Destination.Instance;
140
146
  /**
141
147
  * Initializes a map of destinations using ONLY the unified code/config/env pattern.
142
148
  * Does NOT call destination.init() - that happens later during push with proper consent checks.
@@ -145,7 +151,7 @@ declare function createPushResult(partialResult?: Partial<Elb.PushResult>): Elb.
145
151
  * @param collector - The collector instance for destination init context.
146
152
  * @returns The initialized destinations.
147
153
  */
148
- declare function initDestinations(_collector: Collector.Instance, destinations?: Destination.InitDestinations): Promise<Collector.Destinations>;
154
+ declare function initDestinations(collector: Collector.Instance, destinations?: Destination.InitDestinations): Promise<Collector.Destinations>;
149
155
  /**
150
156
  * Merges destination environment with config environment
151
157
  * Config env takes precedence over destination env for overrides
@@ -186,7 +192,7 @@ declare function runCollector(collector: Collector.Instance, state?: RunState):
186
192
  * @param type The type of the event to listen for.
187
193
  * @param option The callback function or an array of callback functions.
188
194
  */
189
- declare function on(collector: Collector.Instance, type: On.Types, option: WalkerOS.SingleOrArray<On.Options>): void;
195
+ declare function on(collector: Collector.Instance, type: On.Types, option: WalkerOS.SingleOrArray<On.Options>): Promise<void>;
190
196
  /**
191
197
  * Calls a destination's on() handler with proper context.
192
198
  * Used by both onApply() for immediate calls and destinationInit() for flushing queued events.
@@ -200,15 +206,91 @@ declare function callDestinationOn(collector: Collector.Instance, destination: D
200
206
  * @param options The options for the callbacks.
201
207
  * @param config The consent configuration.
202
208
  */
203
- declare function onApply(collector: Collector.Instance, type: On.Types, options?: Array<On.Options>, config?: WalkerOS.Consent): void;
209
+ declare function onApply(collector: Collector.Instance, type: On.Types, options?: Array<On.Options>, config?: unknown): Promise<boolean>;
204
210
 
205
211
  /**
206
- * Initialize sources using the code/config/env pattern
207
- *
208
- * @param collector - The WalkerOS collector instance
209
- * @param sources - Map of source definitions with code/config/env
210
- * @returns Initialized sources
212
+ * Initialize a single source. Extracted from the initSources loop body
213
+ * so it can be reused by the pending-source activator.
214
+ */
215
+ declare function initSource(collector: Collector.Instance, sourceId: string, sourceDefinition: Source.InitSource): Promise<Source.Instance | undefined>;
216
+ /**
217
+ * Initialize sources. Sources with `require` are deferred to collector.pending.
211
218
  */
212
219
  declare function initSources(collector: Collector.Instance, sources?: Source.InitSources): Promise<Collector.Sources>;
213
220
 
214
- export { code as Code, type CommandTypes, Commands, Const, type CreateCollector, type HandleCommandFn, type RunState, type StartFlow, type StorageType, addDestination, callDestinationOn, commonHandleCommand, createEvent, createPush, createPushResult, destinationInit, destinationPush, initDestinations, initSources, mergeEnvironments, on, onApply, pushToDestinations, runCollector, setConsent, startFlow };
221
+ /**
222
+ * @module transformer
223
+ *
224
+ * Transformer Chain Utilities
225
+ * ==========================
226
+ *
227
+ * This module provides the unified implementation for transformer chains in walkerOS.
228
+ * Chains are used at two points in the data flow:
229
+ *
230
+ * 1. Pre-collector chains (source.next):
231
+ * Source → [Transformer Chain] → Collector
232
+ * Events are processed before the collector sees them.
233
+ *
234
+ * 2. Post-collector chains (destination.before):
235
+ * Collector → [Transformer Chain] → Destination
236
+ * Events are processed before reaching specific destinations.
237
+ *
238
+ * Key Functions:
239
+ * - extractTransformerNextMap(): Extracts next links from transformer instances
240
+ * - extractChainProperty(): Unified extraction of chain properties from definitions
241
+ * - walkChain(): Resolves chain IDs from starting point
242
+ * - runTransformerChain(): Executes a chain of transformers on an event
243
+ *
244
+ * Chain Resolution:
245
+ * - String start: Walk transformer.next links until chain ends
246
+ * - Array start: Use array directly (explicit chain, ignores transformer.next)
247
+ *
248
+ * Chain Termination:
249
+ * - Transformer returns false → chain stops, event is dropped
250
+ * - Transformer throws error → chain stops, event is dropped
251
+ * - Transformer returns void → continue with unchanged event
252
+ * - Transformer returns event → continue with modified event
253
+ */
254
+
255
+ /**
256
+ * Extracts transformer next configuration for chain walking.
257
+ * Maps transformer instances to their config.next values.
258
+ *
259
+ * This is the single source of truth for extracting chain links.
260
+ * Used by both source.ts (pre-collector chains) and destination.ts (post-collector chains).
261
+ *
262
+ * @param transformers - Map of transformer instances
263
+ * @returns Map of transformer IDs to their next configuration
264
+ */
265
+ declare function extractTransformerNextMap(transformers: Transformer.Transformers): Record<string, {
266
+ next?: string | string[];
267
+ }>;
268
+ /**
269
+ * Walks a transformer chain starting from a given transformer ID.
270
+ * Returns ordered array of transformer IDs in the chain.
271
+ *
272
+ * Used for on-demand chain resolution:
273
+ * - Called from destination.ts with destination.config.before
274
+ * - Called from source.ts with source.config.next
275
+ *
276
+ * @param startId - First transformer in chain, or explicit array of transformer IDs
277
+ * @param transformers - Available transformer configs with optional `next` field
278
+ * @returns Ordered array of transformer IDs to execute
279
+ *
280
+ * @example
281
+ * // Single transformer
282
+ * walkChain('redact', { redact: {} }) // ['redact']
283
+ *
284
+ * @example
285
+ * // Chain via next
286
+ * walkChain('a', { a: { next: 'b' }, b: { next: 'c' }, c: {} }) // ['a', 'b', 'c']
287
+ *
288
+ * @example
289
+ * // Explicit array
290
+ * walkChain(['x', 'y'], {}) // ['x', 'y']
291
+ */
292
+ declare function walkChain(startId: string | string[] | undefined, transformers?: Record<string, {
293
+ next?: string | string[];
294
+ }>): string[];
295
+
296
+ export { code as Code, type CommandTypes, Commands, Const, type CreateCollector, type HandleCommandFn, type RunState, type StartFlow, type StorageType, addDestination, callDestinationOn, commonHandleCommand, createEvent, createPush, createPushResult, destinationInit, destinationPush, extractTransformerNextMap, initDestinations, initSource, initSources, mergeEnvironments, on, onApply, processConsent, pushToDestinations, registerDestination, runCollector, startFlow, walkChain };
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:()=>c,Const:()=>a,addDestination:()=>O,callDestinationOn:()=>m,commonHandleCommand:()=>R,createEvent:()=>I,createPush:()=>F,createPushResult:()=>A,destinationInit:()=>q,destinationPush:()=>P,initDestinations:()=>E,initSources:()=>U,mergeEnvironments:()=>x,on:()=>d,onApply:()=>h,pushToDestinations:()=>j,runCollector:()=>M,setConsent:()=>D,startFlow:()=>W}),module.exports=(e=r,((e,r,i,c)=>{if(r&&"object"==typeof r||"function"==typeof r)for(let a of o(r))s.call(e,a)||a===i||n(e,a,{get:()=>r[a],enumerable:!(c=t(r,a))||c.enumerable});return e})(n({},"__esModule",{value:!0}),e));var i={},c={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"},a={Commands:c,Utils:{Storage:{Cookie:"cookie",Local:"local",Session:"session"}}},u=require("@walkeros/core"),g=require("@walkeros/core"),l=require("@walkeros/core"),f=require("@walkeros/core");function d(e,n,t){const o=e.on,s=o[n]||[],r=(0,l.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),c={collector:e,logger:i,id:t,config:n.config,data:s,env:x(n.env,n.config.env)};(0,f.tryCatch)(n.on)(o,c)}function h(e,n,t,o){let s,r=t||[];switch(t||(r=e.on[n]||[]),n){case a.Commands.Consent:s=o||e.consent;break;case a.Commands.Session:s=e.session;break;case a.Commands.Ready:case a.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 a.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 a.Commands.Ready:case a.Commands.Run:!function(e,n){e.allowed&&n.forEach(n=>{(0,f.tryCatch)(n)(e)})}(e,r);break;case a.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:k(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}`),c={collector:e,logger:i,id:t,ingest:s,config:n.config,env:k(n.config.env)};i.debug("push",{event:o.name});const a=await(0,p.useHooks)(n.push,"TransformerPush",e.hooks)(o,c);return i.debug("push done"),a}async function w(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 k(e){return e&&(0,p.isObject)(e)?e:{}}function C(e,n){const t=e.config.before;return t?b(t,function(e){const n={};for(const[t,o]of Object.entries(e))o.config?.next?n[t]={next:o.config.next}:n[t]={};return n}(n)):[]}async function O(e,n,t){const{code:o,config:s={},env:r={},before:i}=n,c=t||s||{init:!1},a=i?{...c,before:i}:c,u={...o,config:a,env:x(o.env,r)};let l=u.config.id;if(!l)do{l=(0,g.getId)(4)}while(e.destinations[l]);return e.destinations[l]=u,!1!==u.config.queue&&(u.queuePush=[...e.queue]),j(e,void 0,{},{[l]:u})}async function j(e,n,t={},o){const{allowed:s,consent:r,globals:i,user:c}=e;if(!s)return A({ok:!1});n&&e.queue.push(n),o||(o=e.destinations);const a=await Promise.all(Object.entries(o||{}).map(async([o,s])=>{let a=(s.queuePush||[]).map(e=>({...e,consent:r}));if(s.queuePush=[],n){const e=(0,g.clone)(n);a.push(e)}if(!a.length&&!s.queueOn?.length)return{id:o,destination:s,skipped:!0};if(!a.length&&s.queueOn?.length){const n=await(0,g.tryCatchAsync)(q)(e,s,o);return{id:o,destination:s,skipped:!n}}const u=[],l=a.filter(e=>{const n=(0,g.getGrantedConsent)(s.config.consent,r,e.consent);return!n||(e.consent=n,u.push(e),!1)});if(s.queuePush.concat(l),!u.length)return{id:o,destination:s,queue:a};if(!await(0,g.tryCatchAsync)(q)(e,s,o))return{id:o,destination:s,queue:a};let f,d;s.dlq||(s.dlq=[]);const m=C(s,e.transformers);return await Promise.all(u.map(async n=>{n.globals=(0,g.assign)(i,n.globals),n.user=(0,g.assign)(c,n.user);let r=n;if(m.length>0&&e.transformers&&Object.keys(e.transformers).length>0){const o=await w(e,e.transformers,m,n,t.ingest);if(null===o)return n;r=o}const a=await(0,g.tryCatchAsync)(P,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!==a&&(d=a),n})),{id:o,destination:s,error:f,response:d}})),u={},l={},f={};for(const e of a){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),l[e.id]=t):u[e.id]=t}return A({event:n,...Object.keys(u).length&&{done:u},...Object.keys(l).length&&{queued:l},...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:x(n.env,n.config.env)};s.debug("init");const i=await(0,g.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 P(e,n,t,o,s){const{config:r}=n,i=await(0,g.processEventMapping)(o,r,e);if(i.ignore)return!1;const c=n.type||"unknown",a=e.logger.scope(c),u={collector:e,logger:a,id:t,config:r,data:i.data,rule:i.mapping,ingest:s,env:x(n.env,r.env)},l=i.mapping,f=i.mappingKey||"* *";if(!l?.batch||!n.pushBatch){a.debug("push",{event:i.event.name});const t=await(0,g.useHooks)(n.push,"DestinationPush",e.hooks)(i.event,u);return a.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,g.debounce)(()=>{const o=n.batches[f].batched,i={collector:e,logger:a,id:t,config:r,data:void 0,rule:l,ingest:s,env:x(n.env,r.env)};a.debug("push batch",{events:o.events.length}),(0,g.useHooks)(n.pushBatch,"DestinationPushBatch",e.hooks)(o,i),a.debug("push batch done"),o.events=[],o.data=[]},l.batch)}}const o=n.batches[f];o.batched.events.push(i.event),(0,g.isDefined)(i.data)&&o.batched.data.push(i.data),o.batchFn()}return!0}function A(e){return{ok:!e?.failed,...e}}async function E(e,n={}){const t={};for(const[e,o]of Object.entries(n)){const{code:n,config:s={},env:r={},before:i}=o,c={...n.config,...s,...i&&{before:i}},a=x(n.env,r);t[e]={...n,config:c,env:a}}return t}function x(e,n){return e||n?n?e&&(0,g.isObject)(e)&&(0,g.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?j(e):A({ok:!0})}var S=require("@walkeros/core"),$=require("@walkeros/core"),H=require("@walkeros/core");async function R(e,n,t,o){let s;switch(n){case a.Commands.Config:(0,H.isObject)(t)&&(0,$.assign)(e.config,t,{shallow:!1});break;case a.Commands.Consent:(0,H.isObject)(t)&&(s=await D(e,t));break;case a.Commands.Custom:(0,H.isObject)(t)&&(e.custom=(0,$.assign)(e.custom,t));break;case a.Commands.Destination:(0,H.isObject)(t)&&("code"in t&&(0,H.isObject)(t.code)?s=await O(e,t,o):(0,$.isFunction)(t.push)&&(s=await O(e,{code:t},o)));break;case a.Commands.Globals:(0,H.isObject)(t)&&(e.globals=(0,$.assign)(e.globals,t));break;case a.Commands.On:(0,$.isString)(t)&&d(e,t,o);break;case a.Commands.Ready:h(e,"ready");break;case a.Commands.Run:s=await M(e,t);break;case a.Commands.Session:h(e,"session");break;case a.Commands.User:(0,H.isObject)(t)&&(0,$.assign)(e.user,t,{shallow:!1})}return s||A({ok:!0})}function I(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:c=`${t} ${o}`,data:a={},context:u={},globals:g=e.globals,custom:l={},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:w={source:e.version,tagging:e.config.tagging||0},source:k={type:"collector",id:"",previous_id:""}}=n;return{name:c,data:a,context:u,globals:g,custom:l,user:f,nested:d,consent:m,id:h,trigger:p,entity:b,action:y,timestamp:s,timing:v,group:r,count:i,version:w,source:k}}async function M(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++;const t=await j(e);return h(e,"run"),t}var T=require("@walkeros/core");function F(e,n){return(0,T.useHooks)(async(t,o={})=>await(0,T.tryCatchAsync)(async()=>{const{id:s,ingest:r,mapping:i,preChain:c}=o;let a=t;const u=r?Object.freeze(r):void 0;if(i){const n=await(0,T.processEventMapping)(a,i,e);if(n.ignore)return A({ok:!0});if(i.consent){if(!(0,T.getGrantedConsent)(i.consent,e.consent,n.event.consent))return A({ok:!0})}a=n.event}if(c?.length&&e.transformers&&Object.keys(e.transformers).length>0){const n=await w(e,e.transformers,c,a,u);if(null===n)return A({ok:!0});a=n}const g=n(a),l=I(e,g);return await j(e,l,{id:s,ingest:u})},()=>A({ok:!1}))(),"Push",e.hooks)}var G=require("@walkeros/core");async function _(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:{},globals:s,group:"",hooks:{},logger:o,on:{},queue:[],round:0,session:void 0,timing:Date.now(),user:e.user||{},version:"1.1.1",sources:{},push:void 0,command:void 0};return r.push=F(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,G.useHooks)(async(t,o,s)=>await(0,G.tryCatchAsync)(async()=>await n(e,t,o,s),()=>A({ok:!1}))(),"Command",e.hooks)}(r,R),r.destinations=await E(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,c=e.logger.scope("transformer").scope(o),a={collector:e,logger:c,id:o,config:r,env:i},u=await n(a);t[o]=u}return t}(r,e.transformers||{}),r}var B=require("@walkeros/core");function L(e){const n={};for(const[t,o]of Object.entries(e))n[t]={next:o.config.next};return n}async function U(e,n={}){const t={};for(const[o,s]of Object.entries(n)){const{code:n,config:r={},env:i={},primary:c,next:a}=s;let u;const g=b(a,L(e.transformers)),l=(n,t={})=>e.push(n,{...t,id:o,ingest:u,mapping:r,preChain:g}),f=e.logger.scope("source").scope(o),d={push:l,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,B.getMappingValue)(n,r.ingest,{collector:e}):void 0}},h=await(0,B.tryCatchAsync)(n)(m);if(!h)continue;const p=h.type||"unknown",y=e.logger.scope(p).scope(o);d.logger=y,c&&(h.config={...h.config,primary:c}),t[o]=h}return t}async function W(e){e=e||{};const n=await _(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 c;if("string"==typeof e)c={name:e},n&&"object"==typeof n&&!Array.isArray(n)&&(c.data=n);else{if(!e||"object"!=typeof e)return A({ok:!1});c=e,n&&"object"==typeof n&&!Array.isArray(n)&&(c.data={...c.data||{},...n})}return s&&"object"==typeof s&&(c.context=s),r&&Array.isArray(r)&&(c.nested=r),i&&"object"==typeof i&&(c.custom=i),o.push(c)}});var o;n.sources.elb=t;const s=await U(n,e.sources||{});Object.assign(n.sources,s);const{consent:r,user:i,globals:c,custom:a}=e;r&&await n.command("consent",r),i&&await n.command("user",i),c&&Object.assign(n.globals,c),a&&Object.assign(n.custom,a),n.config.run&&await n.command("run");let u=t.push;const g=Object.values(n.sources).filter(e=>"elb"!==e.type),l=g.find(e=>e.config.primary);return l?u=l.push:g.length>0&&(u=g[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:()=>x,callDestinationOn:()=>D,commonHandleCommand:()=>G,createEvent:()=>U,createPush:()=>L,createPushResult:()=>H,destinationInit:()=>S,destinationPush:()=>$,extractTransformerNextMap:()=>b,initDestinations:()=>I,initSource:()=>q,initSources:()=>j,mergeEnvironments:()=>M,on:()=>A,onApply:()=>P,processConsent:()=>l,pushToDestinations:()=>E,registerDestination:()=>R,runCollector:()=>_,startFlow:()=>Q,walkChain:()=>w}),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"),p=require("@walkeros/core"),h=require("@walkeros/core");function b(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 y(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 w(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 k(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:O(n.config.env)};s.debug("init");const r=await(0,h.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 v(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:O(n.config.env)};r.debug("push",{event:o.name});const c=await(0,h.useHooks)(n.push,"TransformerPush",e.hooks)(o,a);return r.debug("push done"),c}async function C(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,h.tryCatchAsync)(k)(e,t,o))return e.logger.info(`Transformer init failed: ${o}`),null;const r=await(0,h.tryCatchAsync)(v,n=>(e.logger.scope(`transformer:${t.type||"unknown"}`).error("Push failed",{error:n}),!1))(e,t,o,i,s);if(!1===r)return null;void 0!==r&&(i=r)}return i}function O(e){return e&&(0,h.isObject)(e)?e:{}}async function q(e,n,t){const{code:o,config:s={},env:i={},primary:r,next:a}=t;let c;const u=w(a,b(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,p.getMappingValue)(n,s.ingest,{collector:e}):void 0}},d=await(0,p.tryCatchAsync)(o)(f);if(!d)return;const m=d.type||"unknown",h=e.logger.scope(m).scope(n);return g.logger=h,r&&(d.config={...d.config,primary:r}),d}async function j(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 q(e,o,s);i&&(t[o]=i)}return t}async function A(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 P(e,n,i)}function D(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:M(n.env,n.config.env)};(0,m.tryCatch)(n.on)(o,a)}async function P(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});D(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 q(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=R(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 x(e,n,t){const{code:o,config:s={},env:i={},before:r}=n;if(!(0,f.isFunction)(o.push))return H({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:M(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]),E(e,void 0,{},{[l]:u})}async function E(e,n,t={},o){const{allowed:s,consent:i,globals:r,user:a}=e;if(!s)return H({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)(S)(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)(S)(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?w(t,b(n)):[]}(s,e.transformers);let p=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 C(e,e.transformers,m,n,t.ingest);if(null===o)return n;i=o}const c=Date.now(),u=await(0,f.tryCatchAsync)($,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 p+=Date.now()-c,void 0!==u&&(d=u),n})),{id:o,destination:s,error:g,response:d,totalDuration:p}})),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 H({event:n,...Object.keys(u).length&&{done:u},...Object.keys(l).length&&{queued:l},...Object.keys(g).length&&{failed:g}})}async function S(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:M(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)D(e,n,t,s,i)}s.debug("init done")}return!0}async function $(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:M(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:M(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 H(e){return{ok:!e?.failed,...e}}function R(e){const{code:n,config:t={},env:o={}}=e,{config:s}=y(e,"before"),i={...n.config,...t,...s},r=M(n.env,o);return{...n,config:i,env:r}}async function I(e,n={}){const t={};for(const[o,s]of Object.entries(n))s.config?.require?.length?e.pending.destinations[o]=s:t[o]=R(s);return t}function M(e,n){return e||n?n?e&&(0,f.isObject)(e)&&(0,f.isObject)(n)?{...e,...n}:n:e:{}}var T=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,T.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,T.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 x(e,t,o):(0,T.isFunction)(t.push)&&(s=await x(e,{code:t},o)));break;case c.Commands.Globals:(0,F.isObject)(t)&&(e.globals=(0,T.assign)(e.globals,t),i=t,r=!0);break;case c.Commands.On:(0,T.isString)(t)&&await A(e,t,o);break;case c.Commands.Ready:r=!0;break;case c.Commands.Run:s=await _(e,t),r=!0;break;case c.Commands.Session:r=!0;break;case c.Commands.User:(0,F.isObject)(t)&&((0,T.assign)(e.user,t,{shallow:!1}),i=t,r=!0)}return r&&await P(e,n,void 0,i),a&&(s=await E(e)),s||H({ok:!0})}function U(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:p=`${s}-${i}-${r}`,trigger:h="",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:p,trigger:h,entity:b,action:y,timestamp:s,timing:w,group:i,count:r,version:k,source:v}}async function _(e,n){e.allowed=!0,e.count=0,e.group=(0,T.getId)(),e.timing=Date.now(),n&&(n.consent&&(e.consent=(0,T.assign)(e.consent,n.consent)),n.user&&(e.user=(0,T.assign)(e.user,n.user)),n.globals&&(e.globals=(0,T.assign)(e.config.globalsStatic||{},n.globals)),n.custom&&(e.custom=(0,T.assign)(e.custom,n.custom))),Object.values(e.destinations).forEach(e=>{e.queuePush=[]}),e.queue=[],e.round++;return await E(e)}var B=require("@walkeros/core");function L(e,n){return(0,B.useHooks)(async(t,o={})=>await(0,B.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,B.processEventMapping)(u,a,e);if(n.ignore)return H({ok:!0});if(a.consent){if(!(0,B.getGrantedConsent)(a.consent,e.consent,n.event.consent))return H({ok:!0})}u=n.event}if(c?.length&&e.transformers&&Object.keys(e.transformers).length>0){const n=await C(e,e.transformers,c,u,l);if(null===n)return H({ok:!0});u=n}const g=n(u),f=U(e,g),d=await E(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},()=>H({ok:!1}))(),"Push",e.hooks)}var V=require("@walkeros/core");async function N(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.1.2",sources:{},pending:{sources:{},destinations:{}},push:void 0,command:void 0};return i.push=L(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),()=>H({ok:!1}))(),"Command",e.hooks)}(i,G),i.destinations=await I(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}=y(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 Q(e){e=e||{};const n=await N(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 H({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 j(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