@adapt-arch/utiliti-es 0.4.3 → 0.4.4

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.
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});exports.LogLevel=void 0;(function(t){t[t.Trace=0]="Trace",t[t.Debug=1]="Debug",t[t.Information=2]="Information",t[t.Warning=3]="Warning",t[t.Error=4]="Error",t[t.Critical=5]="Critical",t[t.None=6]="None"})(exports.LogLevel||(exports.LogLevel={}));class n{timestamp=Date.now();level=exports.LogLevel.None;name="";message="";errorMessage;stackTrace;extraParams}class m{_valuesFn;_overrideExisting;constructor(e,s){this._valuesFn=e,this._overrideExisting=s}enrich(e){const s=typeof this._valuesFn=="function"?this._valuesFn():void 0;if(!s)return;e.extraParams=e.extraParams||{};const i=Object.keys(e.extraParams);for(const o in s)i.includes(o)&&!this._overrideExisting||(e.extraParams[o]=s[o])}}class f{_values;_overrideExisting;constructor(e,s){this._values=e,this._overrideExisting=s}enrich(e){if(!this._values)return;e.extraParams=e.extraParams||{};const s=Object.keys(e.extraParams);for(const i in this._values)s.includes(i)&&!this._overrideExisting||(e.extraParams[i]=this._values[i])}}class d{_options;constructor(e){this._options=e}logMessageCore(e){e.name=this._options.name;for(const s of this._options.enrichers)s.enrich(e);this._options.reporter?.register(e)}async[Symbol.asyncDispose](){await this._options.reporter?.[Symbol.asyncDispose]()}isEnabled(e){return e!==exports.LogLevel.None&&e>=this._options.minimumLevel}trace(e){const s=new n;s.level=exports.LogLevel.Trace,s.message=e,this.logMessage(s)}debug(e){const s=new n;s.level=exports.LogLevel.Debug,s.message=e,this.logMessage(s)}info(e){const s=new n;s.level=exports.LogLevel.Information,s.message=e,this.logMessage(s)}warn(e){const s=new n;s.level=exports.LogLevel.Warning,s.message=e,this.logMessage(s)}error(e){const s=new n;s.level=exports.LogLevel.Error,s.message=e,this.logMessage(s)}crit(e){const s=new n;s.level=exports.LogLevel.Critical,s.message=e,this.logMessage(s)}log(e,s,i,o){const r=new n,a=this._extractErrorDetails(i);r.level=e,r.message=s,r.errorMessage=a?.message,r.stackTrace=a?.stack,r.extraParams=o,this.logMessage(r)}logMessage(e){this.isEnabled(e.level)&&setTimeout(()=>{this.logMessageCore(e)},1)}_extractErrorDetails(e){if(e instanceof Error)return{message:e.message,stack:e.stack};switch(typeof e){case"string":return{message:e};case"object":{const s=e;if(s===null)break;const i={message:s.message??s.Message,stack:s.stack??s.Stack??s.stackTrace??s.StackTrace};return i.message??=JSON.stringify(e),i}default:{const s=e;if(typeof s?.toString=="function")return{message:s.toString()};break}}}}class b{name="";reporter=null;minimumLevel=exports.LogLevel.Warning;enrichers=[];static getLevel(e){switch((e||"").toUpperCase()){case"TRACE":return exports.LogLevel.Trace;case"DEBUG":return exports.LogLevel.Debug;case"INFORMATION":return exports.LogLevel.Information;case"WARNING":return exports.LogLevel.Warning;case"ERROR":return exports.LogLevel.Error;case"CRITICAL":return exports.LogLevel.Critical;case"NONE":return exports.LogLevel.None;default:return exports.LogLevel.None}}}class v{_console;constructor(e){this._console=e}register(e){let s;if(this._console)switch(e.level){case exports.LogLevel.Trace:s=this._console.trace||this._console.log;break;case exports.LogLevel.Debug:s=this._console.debug||this._console.log;break;case exports.LogLevel.Information:s=this._console.info||this._console.log;break;case exports.LogLevel.Warning:s=this._console.warn||this._console.log;break;case exports.LogLevel.Error:s=this._console.error||this._console.log;break;case exports.LogLevel.Critical:s=this._console.error||this._console.log;break;default:s=null;break}typeof s=="function"&&s.call(this._console,e.message,e)}[Symbol.asyncDispose](){return Promise.resolve()}}class L{_messages=[];get messages(){return this._messages.slice()}register(e){this._messages.push(e)}[Symbol.asyncDispose](){return Promise.resolve()}}class y{_reporters;constructor(e){this._reporters=e||[]}register(e){for(const s of this._reporters)s.register(e)}async[Symbol.asyncDispose](){const e=[];for(const s of this._reporters)e.push(s[Symbol.asyncDispose]());e.length&&await Promise.all(e)}}class P{endpoint="";verb="POST";batchSize=20;interval=2e3;requestTransform}class T{_messageQueue;_options;_reportActionTimeoutRef;_reportActionPromise;_disposed;constructor(e){if(!e)throw new Error('Argument "options" is required');this._messageQueue=[],this._options=e,this._reportActionTimeoutRef=void 0,this._reportActionPromise=null,this._disposed=!1}register(e){this._disposed||(this._messageQueue.push(e),this._scheduleNextProcessAction())}async[Symbol.asyncDispose](){this._disposed||(await(this._reportActionPromise??this._processMessages()),this._disposed=!0)}_scheduleNextProcessAction(){if(this._reportActionTimeoutRef)return;const e=this._messageQueue.length>=this._options.batchSize?0:this._options.interval;this._reportActionTimeoutRef=setTimeout(()=>{this._reportActionPromise=this._processMessages().then(()=>{const s=this._reportActionTimeoutRef;this._reportActionTimeoutRef=void 0,clearTimeout(s),this._reportActionPromise=null,this._scheduleNextProcessAction()})},e)}async _processMessages(){let e,s;for(;this._messageQueue.length>0;)if(e=this._messageQueue.splice(0,Math.min(this._messageQueue.length,this._options.batchSize)),s=await this._sendMessagesBatch(e),!s){this._messageQueue.unshift(...e);return}}_sendMessagesBatch(e){return new Promise(s=>{const i=()=>{s(!1)},o=new XMLHttpRequest;o.open(this._options.verb,this._options.endpoint),o.setRequestHeader("Content-Type","application/json;charset=UTF-8"),this._options.requestTransform&&this._options.requestTransform(o),o.onload=function(){s(this.status>=200&&this.status<300)},o.onerror=i,o.onabort=i,o.send(JSON.stringify(e))})}}class w{_options;_channel;_eventListeners=null;_broadcastEnabled=!0;constructor(e){this._options=e,this._channel=new BroadcastChannel(this._options.channelName)}init(e){this._eventListeners||(this._eventListeners=s=>{const i=s.data;if(!(!i.topic||!i.message))try{this._broadcastEnabled=!1,e.publish(i.topic,i.message)}finally{this._broadcastEnabled=!0}},this._channel.addEventListener("message",this._eventListeners))}onPublish(e){if(!this._broadcastEnabled||!e.topic||!e.message)return;const s={topic:e.topic,message:e.message};this._channel.postMessage(s)}[Symbol.dispose](){this._eventListeners&&this._channel.removeEventListener("message",this._eventListeners),this._channel.close()}}class E{_logger;_logLevel;constructor(e,s=exports.LogLevel.Information){this._logger=e,this._logLevel=s}onPublish(e){this._logger.log(this._logLevel,`Publishing message to topic: ${e.topic}`,void 0,{topic:e.topic??null,message:e.message??null})}}const c=(t,e)=>{clearTimeout(e.ref),e.ref=void 0;const s=t.timeouts.indexOf(e);t.timeouts.splice(s,1)};class M{_subscriptions=new Map;_options;constructor(e){if(this._options=e,this._options?.plugins)for(const s of this._options.plugins)s.init&&s.init(this)}publish(e,s){const i={topic:e,message:s};if(this._options?.plugins)for(const r of this._options.plugins)r.onPublish&&r.onPublish(i);if(!i.topic)throw new Error("Invalid topic.");if(!i.message)throw new Error("Invalid message.");const o=this._subscriptions.get(i.topic);if(o)for(const r of o.values()){const a={};r.timeouts.push(a),a.ref=setTimeout((h,g,p,_)=>{h.handler(p,_),c(r,g)},0,r,a,structuredClone(i.topic),structuredClone(i.message))}}subscribe(e,s){if(!e)throw new Error("Invalid topic.");if(!s)throw new Error("Invalid handler.");let i=this._subscriptions.get(e);i||(i=new Map,this._subscriptions.set(structuredClone(e),i));const o=`sub-${Date.now()}-${Math.random().toString(16).slice(2)}`;return i.set(o,{handler:s,timeouts:[]}),o}unsubscribe(e){if(e)for(const s of this._subscriptions.values()){for(const i of s.values())for(;;){const o=i.timeouts.pop();if(!o)break;c(i,o)}if(s.delete(e))return}}[Symbol.dispose](){for(const e of this._subscriptions.values())for(const s of e.keys())this.unsubscribe(s);if(this._subscriptions.clear(),this._options?.plugins)for(const e of this._options.plugins){const s=e[Symbol.dispose];s&&s.call(e)}return Promise.resolve()}}function l(t=1,e){return new Promise((s,i)=>{setTimeout(()=>{e?i(e):s()},t)})}function u(t=1){return t<=0?Promise.resolve():l(0).then(()=>u(t-1))}exports.BroadcastChannelPlugin=w;exports.ConsoleReporter=v;exports.DynamicValuesEnricher=m;exports.InMemoryReporter=L;exports.LogMessage=n;exports.Logger=d;exports.LoggerOptions=b;exports.LoggerPlugin=E;exports.MultipleReporter=y;exports.PubSubHub=M;exports.ValuesEnricher=f;exports.XhrReporter=T;exports.XhrReporterOptions=P;exports.delay=l;exports.nextTicks=u;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});exports.LogLevel=void 0;(function(i){i[i.Trace=0]="Trace",i[i.Debug=1]="Debug",i[i.Information=2]="Information",i[i.Warning=3]="Warning",i[i.Error=4]="Error",i[i.Critical=5]="Critical",i[i.None=6]="None"})(exports.LogLevel||(exports.LogLevel={}));class n{timestamp=Date.now();level=exports.LogLevel.None;name="";message="";errorMessage;stackTrace;extraParams}class m{_valuesFn;_overrideExisting;constructor(e,s){this._valuesFn=e,this._overrideExisting=s}enrich(e){const s=typeof this._valuesFn=="function"?this._valuesFn():void 0;if(!s)return;e.extraParams=e.extraParams||{};const t=Object.keys(e.extraParams);for(const r in s)t.includes(r)&&!this._overrideExisting||(e.extraParams[r]=s[r])}}class f{_values;_overrideExisting;constructor(e,s){this._values=e,this._overrideExisting=s}enrich(e){if(!this._values)return;e.extraParams=e.extraParams||{};const s=Object.keys(e.extraParams);for(const t in this._values)s.includes(t)&&!this._overrideExisting||(e.extraParams[t]=this._values[t])}}class d{_options;constructor(e){this._options=e}logMessageCore(e){e.name=this._options.name;for(const s of this._options.enrichers)s.enrich(e);this._options.reporter?.register(e)}async[Symbol.asyncDispose](){await this._options.reporter?.[Symbol.asyncDispose]()}isEnabled(e){return e!==exports.LogLevel.None&&e>=this._options.minimumLevel}trace(e){const s=new n;s.level=exports.LogLevel.Trace,s.message=e,this.logMessage(s)}debug(e){const s=new n;s.level=exports.LogLevel.Debug,s.message=e,this.logMessage(s)}info(e){const s=new n;s.level=exports.LogLevel.Information,s.message=e,this.logMessage(s)}warn(e){const s=new n;s.level=exports.LogLevel.Warning,s.message=e,this.logMessage(s)}error(e){const s=new n;s.level=exports.LogLevel.Error,s.message=e,this.logMessage(s)}crit(e){const s=new n;s.level=exports.LogLevel.Critical,s.message=e,this.logMessage(s)}log(e,s,t,r){const o=new n,a=this._extractErrorDetails(t);o.level=e,o.message=s,o.errorMessage=a?.message,o.stackTrace=a?.stack,o.extraParams=r,this.logMessage(o)}logMessage(e){this.isEnabled(e.level)&&setTimeout(()=>{this.logMessageCore(e)},1)}_extractErrorDetails(e){if(e instanceof Error)return{message:e.message,stack:e.stack};switch(typeof e){case"string":return{message:e};case"object":{const s=e;if(s===null)break;const t={message:s.message??s.Message,stack:s.stack??s.Stack??s.stackTrace??s.StackTrace};return t.message??=JSON.stringify(e),t}default:{const s=e;if(typeof s?.toString=="function")return{message:s.toString()};break}}}}class v{name="";reporter=null;minimumLevel=exports.LogLevel.Warning;enrichers=[];static getLevel(e){switch((e||"").toUpperCase()){case"TRACE":return exports.LogLevel.Trace;case"DEBUG":return exports.LogLevel.Debug;case"INFORMATION":return exports.LogLevel.Information;case"WARNING":return exports.LogLevel.Warning;case"ERROR":return exports.LogLevel.Error;case"CRITICAL":return exports.LogLevel.Critical;case"NONE":return exports.LogLevel.None;default:return exports.LogLevel.None}}}class b{_console;constructor(e){this._console=e}register(e){let s;if(this._console)switch(e.level){case exports.LogLevel.Trace:s=this._console.trace||this._console.log;break;case exports.LogLevel.Debug:s=this._console.debug||this._console.log;break;case exports.LogLevel.Information:s=this._console.info||this._console.log;break;case exports.LogLevel.Warning:s=this._console.warn||this._console.log;break;case exports.LogLevel.Error:s=this._console.error||this._console.log;break;case exports.LogLevel.Critical:s=this._console.error||this._console.log;break;default:s=null;break}typeof s=="function"&&s.call(this._console,e.message,e)}[Symbol.asyncDispose](){return Promise.resolve()}}class L{_messages=[];get messages(){return this._messages.slice()}register(e){this._messages.push(e)}[Symbol.asyncDispose](){return Promise.resolve()}}class y{_reporters;constructor(e){this._reporters=e||[]}register(e){for(const s of this._reporters)s.register(e)}async[Symbol.asyncDispose](){const e=[];for(const s of this._reporters)e.push(s[Symbol.asyncDispose]());e.length&&await Promise.all(e)}}class T{endpoint="";verb="POST";batchSize=20;interval=2e3;requestTransform}class P{_messageQueue;_options;_reportActionTimeoutRef;_reportActionPromise;_disposed;constructor(e){if(!e)throw new Error('Argument "options" is required');this._messageQueue=[],this._options=e,this._reportActionTimeoutRef=void 0,this._reportActionPromise=null,this._disposed=!1}register(e){this._disposed||(this._messageQueue.push(e),this._scheduleNextProcessAction())}async[Symbol.asyncDispose](){this._disposed||(await(this._reportActionPromise??this._processMessages()),this._disposed=!0)}_scheduleNextProcessAction(){if(this._reportActionTimeoutRef)return;const e=this._messageQueue.length>=this._options.batchSize?0:this._options.interval;this._reportActionTimeoutRef=setTimeout(()=>{this._reportActionPromise=this._processMessages().then(()=>{const s=this._reportActionTimeoutRef;this._reportActionTimeoutRef=void 0,clearTimeout(s),this._reportActionPromise=null,this._scheduleNextProcessAction()})},e)}async _processMessages(){let e,s;for(;this._messageQueue.length>0;)if(e=this._messageQueue.splice(0,Math.min(this._messageQueue.length,this._options.batchSize)),s=await this._sendMessagesBatch(e),!s){this._messageQueue.unshift(...e);return}}_sendMessagesBatch(e){return new Promise(s=>{const t=()=>{s(!1)},r=new XMLHttpRequest;r.open(this._options.verb,this._options.endpoint),r.setRequestHeader("Content-Type","application/json;charset=UTF-8"),this._options.requestTransform&&this._options.requestTransform(r),r.onload=function(){s(this.status>=200&&this.status<300)},r.onerror=t,r.onabort=t,r.send(JSON.stringify(e))})}}class w{_options;_channel;_instanceId;_eventListeners=null;constructor(e){this._options=e,this._channel=new BroadcastChannel(this._options.channelName),this._instanceId=crypto.randomUUID()}_addInternals(e,s){const t=e.__adaInternals||{};return{...e,__adaInternals:{...t,...s}}}_removeInternals(e){const{__adaInternals:s,...t}=e;return t}init(e){this._eventListeners||(this._eventListeners=s=>{const t=s.data;if(!t.topic||!t.message)return;const r=this._addInternals(t.message,{fromBroadcast:this._instanceId});e.publish(t.topic,r)},this._channel.addEventListener("message",this._eventListeners))}onPublish(e){if(!e.topic||!e.message)return;const s=e.message.__adaInternals,t=s?.fromBroadcast===this._instanceId;if(s&&(e.message=this._removeInternals(e.message)),t)return;const r={topic:e.topic,message:e.message};this._channel.postMessage(r)}[Symbol.dispose](){this._eventListeners&&this._channel.removeEventListener("message",this._eventListeners),this._channel.close()}}class M{_logger;_logLevel;constructor(e,s=exports.LogLevel.Information){this._logger=e,this._logLevel=s}onPublish(e){this._logger.log(this._logLevel,`Publishing message to topic: ${e.topic}`,void 0,{topic:e.topic??null,message:e.message??null})}}const c=(i,e)=>{clearTimeout(e.ref),e.ref=void 0;const s=i.timeouts.indexOf(e);i.timeouts.splice(s,1)};class I{_subscriptions=new Map;_options;constructor(e){if(this._options=e,this._options?.plugins)for(const s of this._options.plugins)s.init&&s.init(this)}publish(e,s){const t={topic:e,message:s};if(this._options?.plugins)for(const o of this._options.plugins)o.onPublish&&o.onPublish(t);if(!t.topic)throw new Error("Invalid topic.");if(!t.message)throw new Error("Invalid message.");const r=this._subscriptions.get(t.topic);if(r)for(const o of r.values()){const a={};o.timeouts.push(a),a.ref=setTimeout((h,g,p,_)=>{h.handler(p,_),c(o,g)},0,o,a,structuredClone(t.topic),structuredClone(t.message))}}subscribe(e,s){if(!e)throw new Error("Invalid topic.");if(!s)throw new Error("Invalid handler.");let t=this._subscriptions.get(e);t||(t=new Map,this._subscriptions.set(structuredClone(e),t));const r=`sub-${Date.now()}-${Math.random().toString(16).slice(2)}`;return t.set(r,{handler:s,timeouts:[]}),r}unsubscribe(e){if(e)for(const s of this._subscriptions.values()){for(const t of s.values())for(;;){const r=t.timeouts.pop();if(!r)break;c(t,r)}if(s.delete(e))return}}[Symbol.dispose](){for(const e of this._subscriptions.values())for(const s of e.keys())this.unsubscribe(s);if(this._subscriptions.clear(),this._options?.plugins)for(const e of this._options.plugins){const s=e[Symbol.dispose];s&&s.call(e)}return Promise.resolve()}}function l(i=1,e){return new Promise((s,t)=>{setTimeout(()=>{e?t(e):s()},i)})}function u(i=1){return i<=0?Promise.resolve():l(0).then(()=>u(i-1))}exports.BroadcastChannelPlugin=w;exports.ConsoleReporter=b;exports.DynamicValuesEnricher=m;exports.InMemoryReporter=L;exports.LogMessage=n;exports.Logger=d;exports.LoggerOptions=v;exports.LoggerPlugin=M;exports.MultipleReporter=y;exports.PubSubHub=I;exports.ValuesEnricher=f;exports.XhrReporter=P;exports.XhrReporterOptions=T;exports.delay=l;exports.nextTicks=u;
@@ -1 +1 @@
1
- var adaptArch_utilitiEs=(function(t){"use strict";t.LogLevel=void 0,(function(i){i[i.Trace=0]="Trace",i[i.Debug=1]="Debug",i[i.Information=2]="Information",i[i.Warning=3]="Warning",i[i.Error=4]="Error",i[i.Critical=5]="Critical",i[i.None=6]="None"})(t.LogLevel||(t.LogLevel={}));class a{timestamp=Date.now();level=t.LogLevel.None;name="";message="";errorMessage;stackTrace;extraParams}class g{_valuesFn;_overrideExisting;constructor(e,s){this._valuesFn=e,this._overrideExisting=s}enrich(e){const s=typeof this._valuesFn=="function"?this._valuesFn():void 0;if(!s)return;e.extraParams=e.extraParams||{};const n=Object.keys(e.extraParams);for(const o in s)n.includes(o)&&!this._overrideExisting||(e.extraParams[o]=s[o])}}class _{_values;_overrideExisting;constructor(e,s){this._values=e,this._overrideExisting=s}enrich(e){if(!this._values)return;e.extraParams=e.extraParams||{};const s=Object.keys(e.extraParams);for(const n in this._values)s.includes(n)&&!this._overrideExisting||(e.extraParams[n]=this._values[n])}}class m{_options;constructor(e){this._options=e}logMessageCore(e){e.name=this._options.name;for(const s of this._options.enrichers)s.enrich(e);this._options.reporter?.register(e)}async[Symbol.asyncDispose](){await this._options.reporter?.[Symbol.asyncDispose]()}isEnabled(e){return e!==t.LogLevel.None&&e>=this._options.minimumLevel}trace(e){const s=new a;s.level=t.LogLevel.Trace,s.message=e,this.logMessage(s)}debug(e){const s=new a;s.level=t.LogLevel.Debug,s.message=e,this.logMessage(s)}info(e){const s=new a;s.level=t.LogLevel.Information,s.message=e,this.logMessage(s)}warn(e){const s=new a;s.level=t.LogLevel.Warning,s.message=e,this.logMessage(s)}error(e){const s=new a;s.level=t.LogLevel.Error,s.message=e,this.logMessage(s)}crit(e){const s=new a;s.level=t.LogLevel.Critical,s.message=e,this.logMessage(s)}log(e,s,n,o){const r=new a,c=this._extractErrorDetails(n);r.level=e,r.message=s,r.errorMessage=c?.message,r.stackTrace=c?.stack,r.extraParams=o,this.logMessage(r)}logMessage(e){this.isEnabled(e.level)&&setTimeout(()=>{this.logMessageCore(e)},1)}_extractErrorDetails(e){if(e instanceof Error)return{message:e.message,stack:e.stack};switch(typeof e){case"string":return{message:e};case"object":{const s=e;if(s===null)break;const n={message:s.message??s.Message,stack:s.stack??s.Stack??s.stackTrace??s.StackTrace};return n.message??=JSON.stringify(e),n}default:{const s=e;if(typeof s?.toString=="function")return{message:s.toString()};break}}}}class f{name="";reporter=null;minimumLevel=t.LogLevel.Warning;enrichers=[];static getLevel(e){switch((e||"").toUpperCase()){case"TRACE":return t.LogLevel.Trace;case"DEBUG":return t.LogLevel.Debug;case"INFORMATION":return t.LogLevel.Information;case"WARNING":return t.LogLevel.Warning;case"ERROR":return t.LogLevel.Error;case"CRITICAL":return t.LogLevel.Critical;case"NONE":return t.LogLevel.None;default:return t.LogLevel.None}}}class p{_console;constructor(e){this._console=e}register(e){let s;if(this._console)switch(e.level){case t.LogLevel.Trace:s=this._console.trace||this._console.log;break;case t.LogLevel.Debug:s=this._console.debug||this._console.log;break;case t.LogLevel.Information:s=this._console.info||this._console.log;break;case t.LogLevel.Warning:s=this._console.warn||this._console.log;break;case t.LogLevel.Error:s=this._console.error||this._console.log;break;case t.LogLevel.Critical:s=this._console.error||this._console.log;break;default:s=null;break}typeof s=="function"&&s.call(this._console,e.message,e)}[Symbol.asyncDispose](){return Promise.resolve()}}class d{_messages=[];get messages(){return this._messages.slice()}register(e){this._messages.push(e)}[Symbol.asyncDispose](){return Promise.resolve()}}class b{_reporters;constructor(e){this._reporters=e||[]}register(e){for(const s of this._reporters)s.register(e)}async[Symbol.asyncDispose](){const e=[];for(const s of this._reporters)e.push(s[Symbol.asyncDispose]());e.length&&await Promise.all(e)}}class v{endpoint="";verb="POST";batchSize=20;interval=2e3;requestTransform}class L{_messageQueue;_options;_reportActionTimeoutRef;_reportActionPromise;_disposed;constructor(e){if(!e)throw new Error('Argument "options" is required');this._messageQueue=[],this._options=e,this._reportActionTimeoutRef=void 0,this._reportActionPromise=null,this._disposed=!1}register(e){this._disposed||(this._messageQueue.push(e),this._scheduleNextProcessAction())}async[Symbol.asyncDispose](){this._disposed||(await(this._reportActionPromise??this._processMessages()),this._disposed=!0)}_scheduleNextProcessAction(){if(this._reportActionTimeoutRef)return;const e=this._messageQueue.length>=this._options.batchSize?0:this._options.interval;this._reportActionTimeoutRef=setTimeout(()=>{this._reportActionPromise=this._processMessages().then(()=>{const s=this._reportActionTimeoutRef;this._reportActionTimeoutRef=void 0,clearTimeout(s),this._reportActionPromise=null,this._scheduleNextProcessAction()})},e)}async _processMessages(){let e,s;for(;this._messageQueue.length>0;)if(e=this._messageQueue.splice(0,Math.min(this._messageQueue.length,this._options.batchSize)),s=await this._sendMessagesBatch(e),!s){this._messageQueue.unshift(...e);return}}_sendMessagesBatch(e){return new Promise(s=>{const n=()=>{s(!1)},o=new XMLHttpRequest;o.open(this._options.verb,this._options.endpoint),o.setRequestHeader("Content-Type","application/json;charset=UTF-8"),this._options.requestTransform&&this._options.requestTransform(o),o.onload=function(){s(this.status>=200&&this.status<300)},o.onerror=n,o.onabort=n,o.send(JSON.stringify(e))})}}class y{_options;_channel;_eventListeners=null;_broadcastEnabled=!0;constructor(e){this._options=e,this._channel=new BroadcastChannel(this._options.channelName)}init(e){this._eventListeners||(this._eventListeners=s=>{const n=s.data;if(!(!n.topic||!n.message))try{this._broadcastEnabled=!1,e.publish(n.topic,n.message)}finally{this._broadcastEnabled=!0}},this._channel.addEventListener("message",this._eventListeners))}onPublish(e){if(!this._broadcastEnabled||!e.topic||!e.message)return;const s={topic:e.topic,message:e.message};this._channel.postMessage(s)}[Symbol.dispose](){this._eventListeners&&this._channel.removeEventListener("message",this._eventListeners),this._channel.close()}}class P{_logger;_logLevel;constructor(e,s=t.LogLevel.Information){this._logger=e,this._logLevel=s}onPublish(e){this._logger.log(this._logLevel,`Publishing message to topic: ${e.topic}`,void 0,{topic:e.topic??null,message:e.message??null})}}const l=(i,e)=>{clearTimeout(e.ref),e.ref=void 0;const s=i.timeouts.indexOf(e);i.timeouts.splice(s,1)};class T{_subscriptions=new Map;_options;constructor(e){if(this._options=e,this._options?.plugins)for(const s of this._options.plugins)s.init&&s.init(this)}publish(e,s){const n={topic:e,message:s};if(this._options?.plugins)for(const r of this._options.plugins)r.onPublish&&r.onPublish(n);if(!n.topic)throw new Error("Invalid topic.");if(!n.message)throw new Error("Invalid message.");const o=this._subscriptions.get(n.topic);if(o)for(const r of o.values()){const c={};r.timeouts.push(c),c.ref=setTimeout((w,E,M,k)=>{w.handler(M,k),l(r,E)},0,r,c,structuredClone(n.topic),structuredClone(n.message))}}subscribe(e,s){if(!e)throw new Error("Invalid topic.");if(!s)throw new Error("Invalid handler.");let n=this._subscriptions.get(e);n||(n=new Map,this._subscriptions.set(structuredClone(e),n));const o=`sub-${Date.now()}-${Math.random().toString(16).slice(2)}`;return n.set(o,{handler:s,timeouts:[]}),o}unsubscribe(e){if(e)for(const s of this._subscriptions.values()){for(const n of s.values())for(;;){const o=n.timeouts.pop();if(!o)break;l(n,o)}if(s.delete(e))return}}[Symbol.dispose](){for(const e of this._subscriptions.values())for(const s of e.keys())this.unsubscribe(s);if(this._subscriptions.clear(),this._options?.plugins)for(const e of this._options.plugins){const s=e[Symbol.dispose];s&&s.call(e)}return Promise.resolve()}}function u(i=1,e){return new Promise((s,n)=>{setTimeout(()=>{e?n(e):s()},i)})}function h(i=1){return i<=0?Promise.resolve():u(0).then(()=>h(i-1))}return t.BroadcastChannelPlugin=y,t.ConsoleReporter=p,t.DynamicValuesEnricher=g,t.InMemoryReporter=d,t.LogMessage=a,t.Logger=m,t.LoggerOptions=f,t.LoggerPlugin=P,t.MultipleReporter=b,t.PubSubHub=T,t.ValuesEnricher=_,t.XhrReporter=L,t.XhrReporterOptions=v,t.delay=u,t.nextTicks=h,Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),t})({});
1
+ var adaptArch_utilitiEs=(function(t){"use strict";t.LogLevel=void 0,(function(n){n[n.Trace=0]="Trace",n[n.Debug=1]="Debug",n[n.Information=2]="Information",n[n.Warning=3]="Warning",n[n.Error=4]="Error",n[n.Critical=5]="Critical",n[n.None=6]="None"})(t.LogLevel||(t.LogLevel={}));class a{timestamp=Date.now();level=t.LogLevel.None;name="";message="";errorMessage;stackTrace;extraParams}class g{_valuesFn;_overrideExisting;constructor(e,s){this._valuesFn=e,this._overrideExisting=s}enrich(e){const s=typeof this._valuesFn=="function"?this._valuesFn():void 0;if(!s)return;e.extraParams=e.extraParams||{};const i=Object.keys(e.extraParams);for(const r in s)i.includes(r)&&!this._overrideExisting||(e.extraParams[r]=s[r])}}class _{_values;_overrideExisting;constructor(e,s){this._values=e,this._overrideExisting=s}enrich(e){if(!this._values)return;e.extraParams=e.extraParams||{};const s=Object.keys(e.extraParams);for(const i in this._values)s.includes(i)&&!this._overrideExisting||(e.extraParams[i]=this._values[i])}}class m{_options;constructor(e){this._options=e}logMessageCore(e){e.name=this._options.name;for(const s of this._options.enrichers)s.enrich(e);this._options.reporter?.register(e)}async[Symbol.asyncDispose](){await this._options.reporter?.[Symbol.asyncDispose]()}isEnabled(e){return e!==t.LogLevel.None&&e>=this._options.minimumLevel}trace(e){const s=new a;s.level=t.LogLevel.Trace,s.message=e,this.logMessage(s)}debug(e){const s=new a;s.level=t.LogLevel.Debug,s.message=e,this.logMessage(s)}info(e){const s=new a;s.level=t.LogLevel.Information,s.message=e,this.logMessage(s)}warn(e){const s=new a;s.level=t.LogLevel.Warning,s.message=e,this.logMessage(s)}error(e){const s=new a;s.level=t.LogLevel.Error,s.message=e,this.logMessage(s)}crit(e){const s=new a;s.level=t.LogLevel.Critical,s.message=e,this.logMessage(s)}log(e,s,i,r){const o=new a,c=this._extractErrorDetails(i);o.level=e,o.message=s,o.errorMessage=c?.message,o.stackTrace=c?.stack,o.extraParams=r,this.logMessage(o)}logMessage(e){this.isEnabled(e.level)&&setTimeout(()=>{this.logMessageCore(e)},1)}_extractErrorDetails(e){if(e instanceof Error)return{message:e.message,stack:e.stack};switch(typeof e){case"string":return{message:e};case"object":{const s=e;if(s===null)break;const i={message:s.message??s.Message,stack:s.stack??s.Stack??s.stackTrace??s.StackTrace};return i.message??=JSON.stringify(e),i}default:{const s=e;if(typeof s?.toString=="function")return{message:s.toString()};break}}}}class f{name="";reporter=null;minimumLevel=t.LogLevel.Warning;enrichers=[];static getLevel(e){switch((e||"").toUpperCase()){case"TRACE":return t.LogLevel.Trace;case"DEBUG":return t.LogLevel.Debug;case"INFORMATION":return t.LogLevel.Information;case"WARNING":return t.LogLevel.Warning;case"ERROR":return t.LogLevel.Error;case"CRITICAL":return t.LogLevel.Critical;case"NONE":return t.LogLevel.None;default:return t.LogLevel.None}}}class p{_console;constructor(e){this._console=e}register(e){let s;if(this._console)switch(e.level){case t.LogLevel.Trace:s=this._console.trace||this._console.log;break;case t.LogLevel.Debug:s=this._console.debug||this._console.log;break;case t.LogLevel.Information:s=this._console.info||this._console.log;break;case t.LogLevel.Warning:s=this._console.warn||this._console.log;break;case t.LogLevel.Error:s=this._console.error||this._console.log;break;case t.LogLevel.Critical:s=this._console.error||this._console.log;break;default:s=null;break}typeof s=="function"&&s.call(this._console,e.message,e)}[Symbol.asyncDispose](){return Promise.resolve()}}class d{_messages=[];get messages(){return this._messages.slice()}register(e){this._messages.push(e)}[Symbol.asyncDispose](){return Promise.resolve()}}class v{_reporters;constructor(e){this._reporters=e||[]}register(e){for(const s of this._reporters)s.register(e)}async[Symbol.asyncDispose](){const e=[];for(const s of this._reporters)e.push(s[Symbol.asyncDispose]());e.length&&await Promise.all(e)}}class b{endpoint="";verb="POST";batchSize=20;interval=2e3;requestTransform}class L{_messageQueue;_options;_reportActionTimeoutRef;_reportActionPromise;_disposed;constructor(e){if(!e)throw new Error('Argument "options" is required');this._messageQueue=[],this._options=e,this._reportActionTimeoutRef=void 0,this._reportActionPromise=null,this._disposed=!1}register(e){this._disposed||(this._messageQueue.push(e),this._scheduleNextProcessAction())}async[Symbol.asyncDispose](){this._disposed||(await(this._reportActionPromise??this._processMessages()),this._disposed=!0)}_scheduleNextProcessAction(){if(this._reportActionTimeoutRef)return;const e=this._messageQueue.length>=this._options.batchSize?0:this._options.interval;this._reportActionTimeoutRef=setTimeout(()=>{this._reportActionPromise=this._processMessages().then(()=>{const s=this._reportActionTimeoutRef;this._reportActionTimeoutRef=void 0,clearTimeout(s),this._reportActionPromise=null,this._scheduleNextProcessAction()})},e)}async _processMessages(){let e,s;for(;this._messageQueue.length>0;)if(e=this._messageQueue.splice(0,Math.min(this._messageQueue.length,this._options.batchSize)),s=await this._sendMessagesBatch(e),!s){this._messageQueue.unshift(...e);return}}_sendMessagesBatch(e){return new Promise(s=>{const i=()=>{s(!1)},r=new XMLHttpRequest;r.open(this._options.verb,this._options.endpoint),r.setRequestHeader("Content-Type","application/json;charset=UTF-8"),this._options.requestTransform&&this._options.requestTransform(r),r.onload=function(){s(this.status>=200&&this.status<300)},r.onerror=i,r.onabort=i,r.send(JSON.stringify(e))})}}class y{_options;_channel;_instanceId;_eventListeners=null;constructor(e){this._options=e,this._channel=new BroadcastChannel(this._options.channelName),this._instanceId=crypto.randomUUID()}_addInternals(e,s){const i=e.__adaInternals||{};return{...e,__adaInternals:{...i,...s}}}_removeInternals(e){const{__adaInternals:s,...i}=e;return i}init(e){this._eventListeners||(this._eventListeners=s=>{const i=s.data;if(!i.topic||!i.message)return;const r=this._addInternals(i.message,{fromBroadcast:this._instanceId});e.publish(i.topic,r)},this._channel.addEventListener("message",this._eventListeners))}onPublish(e){if(!e.topic||!e.message)return;const s=e.message.__adaInternals,i=s?.fromBroadcast===this._instanceId;if(s&&(e.message=this._removeInternals(e.message)),i)return;const r={topic:e.topic,message:e.message};this._channel.postMessage(r)}[Symbol.dispose](){this._eventListeners&&this._channel.removeEventListener("message",this._eventListeners),this._channel.close()}}class T{_logger;_logLevel;constructor(e,s=t.LogLevel.Information){this._logger=e,this._logLevel=s}onPublish(e){this._logger.log(this._logLevel,`Publishing message to topic: ${e.topic}`,void 0,{topic:e.topic??null,message:e.message??null})}}const l=(n,e)=>{clearTimeout(e.ref),e.ref=void 0;const s=n.timeouts.indexOf(e);n.timeouts.splice(s,1)};class P{_subscriptions=new Map;_options;constructor(e){if(this._options=e,this._options?.plugins)for(const s of this._options.plugins)s.init&&s.init(this)}publish(e,s){const i={topic:e,message:s};if(this._options?.plugins)for(const o of this._options.plugins)o.onPublish&&o.onPublish(i);if(!i.topic)throw new Error("Invalid topic.");if(!i.message)throw new Error("Invalid message.");const r=this._subscriptions.get(i.topic);if(r)for(const o of r.values()){const c={};o.timeouts.push(c),c.ref=setTimeout((w,M,E,I)=>{w.handler(E,I),l(o,M)},0,o,c,structuredClone(i.topic),structuredClone(i.message))}}subscribe(e,s){if(!e)throw new Error("Invalid topic.");if(!s)throw new Error("Invalid handler.");let i=this._subscriptions.get(e);i||(i=new Map,this._subscriptions.set(structuredClone(e),i));const r=`sub-${Date.now()}-${Math.random().toString(16).slice(2)}`;return i.set(r,{handler:s,timeouts:[]}),r}unsubscribe(e){if(e)for(const s of this._subscriptions.values()){for(const i of s.values())for(;;){const r=i.timeouts.pop();if(!r)break;l(i,r)}if(s.delete(e))return}}[Symbol.dispose](){for(const e of this._subscriptions.values())for(const s of e.keys())this.unsubscribe(s);if(this._subscriptions.clear(),this._options?.plugins)for(const e of this._options.plugins){const s=e[Symbol.dispose];s&&s.call(e)}return Promise.resolve()}}function u(n=1,e){return new Promise((s,i)=>{setTimeout(()=>{e?i(e):s()},n)})}function h(n=1){return n<=0?Promise.resolve():u(0).then(()=>h(n-1))}return t.BroadcastChannelPlugin=y,t.ConsoleReporter=p,t.DynamicValuesEnricher=g,t.InMemoryReporter=d,t.LogMessage=a,t.Logger=m,t.LoggerOptions=f,t.LoggerPlugin=T,t.MultipleReporter=v,t.PubSubHub=P,t.ValuesEnricher=_,t.XhrReporter=L,t.XhrReporterOptions=b,t.delay=u,t.nextTicks=h,Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),t})({});
@@ -1,7 +1,7 @@
1
- var r;
2
- (function(t) {
3
- t[t.Trace = 0] = "Trace", t[t.Debug = 1] = "Debug", t[t.Information = 2] = "Information", t[t.Warning = 3] = "Warning", t[t.Error = 4] = "Error", t[t.Critical = 5] = "Critical", t[t.None = 6] = "None";
4
- })(r || (r = {}));
1
+ var n;
2
+ (function(i) {
3
+ i[i.Trace = 0] = "Trace", i[i.Debug = 1] = "Debug", i[i.Information = 2] = "Information", i[i.Warning = 3] = "Warning", i[i.Error = 4] = "Error", i[i.Critical = 5] = "Critical", i[i.None = 6] = "None";
4
+ })(n || (n = {}));
5
5
  class a {
6
6
  /**
7
7
  * The timestamp of the log message.
@@ -10,7 +10,7 @@ class a {
10
10
  /**
11
11
  * The level of the log message.
12
12
  */
13
- level = r.None;
13
+ level = n.None;
14
14
  /**
15
15
  * The name of the logger.
16
16
  */
@@ -52,9 +52,9 @@ class f {
52
52
  if (!e)
53
53
  return;
54
54
  s.extraParams = s.extraParams || {};
55
- const i = Object.keys(s.extraParams);
56
- for (const o in e)
57
- i.includes(o) && !this._overrideExisting || (s.extraParams[o] = e[o]);
55
+ const t = Object.keys(s.extraParams);
56
+ for (const r in e)
57
+ t.includes(r) && !this._overrideExisting || (s.extraParams[r] = e[r]);
58
58
  }
59
59
  }
60
60
  class d {
@@ -77,8 +77,8 @@ class d {
77
77
  return;
78
78
  s.extraParams = s.extraParams || {};
79
79
  const e = Object.keys(s.extraParams);
80
- for (const i in this._values)
81
- e.includes(i) && !this._overrideExisting || (s.extraParams[i] = this._values[i]);
80
+ for (const t in this._values)
81
+ e.includes(t) && !this._overrideExisting || (s.extraParams[t] = this._values[t]);
82
82
  }
83
83
  }
84
84
  class b {
@@ -112,56 +112,56 @@ class b {
112
112
  * @inheritdoc
113
113
  */
114
114
  isEnabled(s) {
115
- return s !== r.None && s >= this._options.minimumLevel;
115
+ return s !== n.None && s >= this._options.minimumLevel;
116
116
  }
117
117
  /**
118
118
  * @inheritdoc
119
119
  */
120
120
  trace(s) {
121
121
  const e = new a();
122
- e.level = r.Trace, e.message = s, this.logMessage(e);
122
+ e.level = n.Trace, e.message = s, this.logMessage(e);
123
123
  }
124
124
  /**
125
125
  * @inheritdoc
126
126
  */
127
127
  debug(s) {
128
128
  const e = new a();
129
- e.level = r.Debug, e.message = s, this.logMessage(e);
129
+ e.level = n.Debug, e.message = s, this.logMessage(e);
130
130
  }
131
131
  /**
132
132
  * @inheritdoc
133
133
  */
134
134
  info(s) {
135
135
  const e = new a();
136
- e.level = r.Information, e.message = s, this.logMessage(e);
136
+ e.level = n.Information, e.message = s, this.logMessage(e);
137
137
  }
138
138
  /**
139
139
  * @inheritdoc
140
140
  */
141
141
  warn(s) {
142
142
  const e = new a();
143
- e.level = r.Warning, e.message = s, this.logMessage(e);
143
+ e.level = n.Warning, e.message = s, this.logMessage(e);
144
144
  }
145
145
  /**
146
146
  * @inheritdoc
147
147
  */
148
148
  error(s) {
149
149
  const e = new a();
150
- e.level = r.Error, e.message = s, this.logMessage(e);
150
+ e.level = n.Error, e.message = s, this.logMessage(e);
151
151
  }
152
152
  /**
153
153
  * @inheritdoc
154
154
  */
155
155
  crit(s) {
156
156
  const e = new a();
157
- e.level = r.Critical, e.message = s, this.logMessage(e);
157
+ e.level = n.Critical, e.message = s, this.logMessage(e);
158
158
  }
159
159
  /**
160
160
  * @inheritdoc
161
161
  */
162
- log(s, e, i, o) {
163
- const n = new a(), c = this._extractErrorDetails(i);
164
- n.level = s, n.message = e, n.errorMessage = c?.message, n.stackTrace = c?.stack, n.extraParams = o, this.logMessage(n);
162
+ log(s, e, t, r) {
163
+ const o = new a(), c = this._extractErrorDetails(t);
164
+ o.level = s, o.message = e, o.errorMessage = c?.message, o.stackTrace = c?.stack, o.extraParams = r, this.logMessage(o);
165
165
  }
166
166
  /**
167
167
  * @inheritdoc
@@ -181,11 +181,11 @@ class b {
181
181
  const e = s;
182
182
  if (e === null)
183
183
  break;
184
- const i = {
184
+ const t = {
185
185
  message: e.message ?? e.Message,
186
186
  stack: e.stack ?? e.Stack ?? e.stackTrace ?? e.StackTrace
187
187
  };
188
- return i.message ??= JSON.stringify(s), i;
188
+ return t.message ??= JSON.stringify(s), t;
189
189
  }
190
190
  default: {
191
191
  const e = s;
@@ -208,7 +208,7 @@ class v {
208
208
  /**
209
209
  * The minimum log level.
210
210
  */
211
- minimumLevel = r.Warning;
211
+ minimumLevel = n.Warning;
212
212
  /**
213
213
  * Log enrichers.
214
214
  */
@@ -221,21 +221,21 @@ class v {
221
221
  static getLevel(s) {
222
222
  switch ((s || "").toUpperCase()) {
223
223
  case "TRACE":
224
- return r.Trace;
224
+ return n.Trace;
225
225
  case "DEBUG":
226
- return r.Debug;
226
+ return n.Debug;
227
227
  case "INFORMATION":
228
- return r.Information;
228
+ return n.Information;
229
229
  case "WARNING":
230
- return r.Warning;
230
+ return n.Warning;
231
231
  case "ERROR":
232
- return r.Error;
232
+ return n.Error;
233
233
  case "CRITICAL":
234
- return r.Critical;
234
+ return n.Critical;
235
235
  case "NONE":
236
- return r.None;
236
+ return n.None;
237
237
  default:
238
- return r.None;
238
+ return n.None;
239
239
  }
240
240
  }
241
241
  }
@@ -256,22 +256,22 @@ class y {
256
256
  let e;
257
257
  if (this._console)
258
258
  switch (s.level) {
259
- case r.Trace:
259
+ case n.Trace:
260
260
  e = this._console.trace || this._console.log;
261
261
  break;
262
- case r.Debug:
262
+ case n.Debug:
263
263
  e = this._console.debug || this._console.log;
264
264
  break;
265
- case r.Information:
265
+ case n.Information:
266
266
  e = this._console.info || this._console.log;
267
267
  break;
268
- case r.Warning:
268
+ case n.Warning:
269
269
  e = this._console.warn || this._console.log;
270
270
  break;
271
- case r.Error:
271
+ case n.Error:
272
272
  e = this._console.error || this._console.log;
273
273
  break;
274
- case r.Critical:
274
+ case n.Critical:
275
275
  e = this._console.error || this._console.log;
276
276
  break;
277
277
  // case LogLevel.None: // Do not log.
@@ -288,7 +288,7 @@ class y {
288
288
  return Promise.resolve();
289
289
  }
290
290
  }
291
- class w {
291
+ class T {
292
292
  _messages = [];
293
293
  get messages() {
294
294
  return this._messages.slice();
@@ -306,7 +306,7 @@ class w {
306
306
  return Promise.resolve();
307
307
  }
308
308
  }
309
- class T {
309
+ class w {
310
310
  _reporters;
311
311
  constructor(s) {
312
312
  this._reporters = s || [];
@@ -350,7 +350,7 @@ class P {
350
350
  */
351
351
  requestTransform;
352
352
  }
353
- class E {
353
+ class I {
354
354
  _messageQueue;
355
355
  _options;
356
356
  _reportActionTimeoutRef;
@@ -394,55 +394,86 @@ class E {
394
394
  }
395
395
  _sendMessagesBatch(s) {
396
396
  return new Promise((e) => {
397
- const i = () => {
397
+ const t = () => {
398
398
  e(!1);
399
- }, o = new XMLHttpRequest();
400
- o.open(this._options.verb, this._options.endpoint), o.setRequestHeader("Content-Type", "application/json;charset=UTF-8"), this._options.requestTransform && this._options.requestTransform(o), o.onload = function() {
399
+ }, r = new XMLHttpRequest();
400
+ r.open(this._options.verb, this._options.endpoint), r.setRequestHeader("Content-Type", "application/json;charset=UTF-8"), this._options.requestTransform && this._options.requestTransform(r), r.onload = function() {
401
401
  e(this.status >= 200 && this.status < 300);
402
- }, o.onerror = i, o.onabort = i, o.send(JSON.stringify(s));
402
+ }, r.onerror = t, r.onabort = t, r.send(JSON.stringify(s));
403
403
  });
404
404
  }
405
405
  }
406
406
  class k {
407
407
  _options;
408
408
  _channel;
409
+ _instanceId;
409
410
  _eventListeners = null;
410
- _broadcastEnabled = !0;
411
411
  /**
412
412
  * Constructor.
413
413
  *
414
414
  * @param {Options} options The options.
415
415
  */
416
416
  constructor(s) {
417
- this._options = s, this._channel = new BroadcastChannel(this._options.channelName);
417
+ this._options = s, this._channel = new BroadcastChannel(this._options.channelName), this._instanceId = crypto.randomUUID();
418
+ }
419
+ /**
420
+ * Adds internal metadata to a message.
421
+ * Merges with existing __adaInternals if present.
422
+ *
423
+ * @param {MessageData} message The message to add internals to.
424
+ * @param {Record<string, unknown>} internals The internals to add.
425
+ * @returns {MessageData} The message with internals added.
426
+ */
427
+ _addInternals(s, e) {
428
+ const t = s.__adaInternals || {};
429
+ return {
430
+ ...s,
431
+ __adaInternals: {
432
+ ...t,
433
+ ...e
434
+ }
435
+ };
436
+ }
437
+ /**
438
+ * Removes __adaInternals from a message before it reaches subscribers.
439
+ *
440
+ * @param {MessageData} message The message to remove internals from.
441
+ * @returns {MessageData} The message with internals removed.
442
+ */
443
+ _removeInternals(s) {
444
+ const { __adaInternals: e, ...t } = s;
445
+ return t;
418
446
  }
419
447
  /** @inheritdoc */
420
448
  init(s) {
421
449
  this._eventListeners || (this._eventListeners = (e) => {
422
- const i = e.data;
423
- if (!(!i.topic || !i.message))
424
- try {
425
- this._broadcastEnabled = !1, s.publish(i.topic, i.message);
426
- } finally {
427
- this._broadcastEnabled = !0;
428
- }
450
+ const t = e.data;
451
+ if (!t.topic || !t.message)
452
+ return;
453
+ const r = this._addInternals(t.message, {
454
+ fromBroadcast: this._instanceId
455
+ });
456
+ s.publish(t.topic, r);
429
457
  }, this._channel.addEventListener("message", this._eventListeners));
430
458
  }
431
459
  /** @inheritdoc */
432
460
  onPublish(s) {
433
- if (!this._broadcastEnabled || !s.topic || !s.message)
461
+ if (!s.topic || !s.message)
462
+ return;
463
+ const e = s.message.__adaInternals, t = e?.fromBroadcast === this._instanceId;
464
+ if (e && (s.message = this._removeInternals(s.message)), t)
434
465
  return;
435
- const e = {
466
+ const r = {
436
467
  topic: s.topic,
437
468
  message: s.message
438
469
  };
439
- this._channel.postMessage(e);
470
+ this._channel.postMessage(r);
440
471
  }
441
472
  [Symbol.dispose]() {
442
473
  this._eventListeners && this._channel.removeEventListener("message", this._eventListeners), this._channel.close();
443
474
  }
444
475
  }
445
- class M {
476
+ class E {
446
477
  _logger;
447
478
  _logLevel;
448
479
  /**
@@ -451,7 +482,7 @@ class M {
451
482
  * @param {Logger} logger The logger.
452
483
  * @param {LogLevel} logLevel The log level.
453
484
  */
454
- constructor(s, e = r.Information) {
485
+ constructor(s, e = n.Information) {
455
486
  this._logger = s, this._logLevel = e;
456
487
  }
457
488
  /** @inheritdoc */
@@ -462,12 +493,12 @@ class M {
462
493
  });
463
494
  }
464
495
  }
465
- const l = (t, s) => {
496
+ const l = (i, s) => {
466
497
  clearTimeout(s.ref), s.ref = void 0;
467
- const e = t.timeouts.indexOf(s);
468
- t.timeouts.splice(e, 1);
498
+ const e = i.timeouts.indexOf(s);
499
+ i.timeouts.splice(e, 1);
469
500
  };
470
- class x {
501
+ class M {
471
502
  _subscriptions = /* @__PURE__ */ new Map();
472
503
  _options;
473
504
  /**
@@ -480,24 +511,24 @@ class x {
480
511
  }
481
512
  /** @inheritdoc */
482
513
  publish(s, e) {
483
- const i = {
514
+ const t = {
484
515
  topic: s,
485
516
  message: e
486
517
  };
487
518
  if (this._options?.plugins)
488
- for (const n of this._options.plugins)
489
- n.onPublish && n.onPublish(i);
490
- if (!i.topic)
519
+ for (const o of this._options.plugins)
520
+ o.onPublish && o.onPublish(t);
521
+ if (!t.topic)
491
522
  throw new Error("Invalid topic.");
492
- if (!i.message)
523
+ if (!t.message)
493
524
  throw new Error("Invalid message.");
494
- const o = this._subscriptions.get(i.topic);
495
- if (o)
496
- for (const n of o.values()) {
525
+ const r = this._subscriptions.get(t.topic);
526
+ if (r)
527
+ for (const o of r.values()) {
497
528
  const c = {};
498
- n.timeouts.push(c), c.ref = setTimeout((u, h, g, p) => {
499
- u.handler(g, p), l(n, h);
500
- }, 0, n, c, structuredClone(i.topic), structuredClone(i.message));
529
+ o.timeouts.push(c), c.ref = setTimeout((u, h, g, _) => {
530
+ u.handler(g, _), l(o, h);
531
+ }, 0, o, c, structuredClone(t.topic), structuredClone(t.message));
501
532
  }
502
533
  }
503
534
  /** @inheritdoc */
@@ -506,21 +537,21 @@ class x {
506
537
  throw new Error("Invalid topic.");
507
538
  if (!e)
508
539
  throw new Error("Invalid handler.");
509
- let i = this._subscriptions.get(s);
510
- i || (i = /* @__PURE__ */ new Map(), this._subscriptions.set(structuredClone(s), i));
511
- const o = `sub-${Date.now()}-${Math.random().toString(16).slice(2)}`;
512
- return i.set(o, { handler: e, timeouts: [] }), o;
540
+ let t = this._subscriptions.get(s);
541
+ t || (t = /* @__PURE__ */ new Map(), this._subscriptions.set(structuredClone(s), t));
542
+ const r = `sub-${Date.now()}-${Math.random().toString(16).slice(2)}`;
543
+ return t.set(r, { handler: e, timeouts: [] }), r;
513
544
  }
514
545
  /** @inheritdoc */
515
546
  unsubscribe(s) {
516
547
  if (s)
517
548
  for (const e of this._subscriptions.values()) {
518
- for (const i of e.values())
549
+ for (const t of e.values())
519
550
  for (; ; ) {
520
- const o = i.timeouts.pop();
521
- if (!o)
551
+ const r = t.timeouts.pop();
552
+ if (!r)
522
553
  break;
523
- l(i, o);
554
+ l(t, r);
524
555
  }
525
556
  if (e.delete(s))
526
557
  return;
@@ -538,31 +569,31 @@ class x {
538
569
  return Promise.resolve();
539
570
  }
540
571
  }
541
- function _(t = 1, s) {
542
- return new Promise((e, i) => {
572
+ function p(i = 1, s) {
573
+ return new Promise((e, t) => {
543
574
  setTimeout(() => {
544
- s ? i(s) : e();
545
- }, t);
575
+ s ? t(s) : e();
576
+ }, i);
546
577
  });
547
578
  }
548
- function m(t = 1) {
549
- return t <= 0 ? Promise.resolve() : _(0).then(() => m(t - 1));
579
+ function m(i = 1) {
580
+ return i <= 0 ? Promise.resolve() : p(0).then(() => m(i - 1));
550
581
  }
551
582
  export {
552
583
  k as BroadcastChannelPlugin,
553
584
  y as ConsoleReporter,
554
585
  f as DynamicValuesEnricher,
555
- w as InMemoryReporter,
556
- r as LogLevel,
586
+ T as InMemoryReporter,
587
+ n as LogLevel,
557
588
  a as LogMessage,
558
589
  b as Logger,
559
590
  v as LoggerOptions,
560
- M as LoggerPlugin,
561
- T as MultipleReporter,
562
- x as PubSubHub,
591
+ E as LoggerPlugin,
592
+ w as MultipleReporter,
593
+ M as PubSubHub,
563
594
  d as ValuesEnricher,
564
- E as XhrReporter,
595
+ I as XhrReporter,
565
596
  P as XhrReporterOptions,
566
- _ as delay,
597
+ p as delay,
567
598
  m as nextTicks
568
599
  };
@@ -1 +1 @@
1
- (function(t,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(t=typeof globalThis<"u"?globalThis:t||self,a(t.adaptArch_utilitiEs={}))})(this,(function(t){"use strict";t.LogLevel=void 0,(function(i){i[i.Trace=0]="Trace",i[i.Debug=1]="Debug",i[i.Information=2]="Information",i[i.Warning=3]="Warning",i[i.Error=4]="Error",i[i.Critical=5]="Critical",i[i.None=6]="None"})(t.LogLevel||(t.LogLevel={}));class a{timestamp=Date.now();level=t.LogLevel.None;name="";message="";errorMessage;stackTrace;extraParams}class g{_valuesFn;_overrideExisting;constructor(e,s){this._valuesFn=e,this._overrideExisting=s}enrich(e){const s=typeof this._valuesFn=="function"?this._valuesFn():void 0;if(!s)return;e.extraParams=e.extraParams||{};const n=Object.keys(e.extraParams);for(const o in s)n.includes(o)&&!this._overrideExisting||(e.extraParams[o]=s[o])}}class _{_values;_overrideExisting;constructor(e,s){this._values=e,this._overrideExisting=s}enrich(e){if(!this._values)return;e.extraParams=e.extraParams||{};const s=Object.keys(e.extraParams);for(const n in this._values)s.includes(n)&&!this._overrideExisting||(e.extraParams[n]=this._values[n])}}class m{_options;constructor(e){this._options=e}logMessageCore(e){e.name=this._options.name;for(const s of this._options.enrichers)s.enrich(e);this._options.reporter?.register(e)}async[Symbol.asyncDispose](){await this._options.reporter?.[Symbol.asyncDispose]()}isEnabled(e){return e!==t.LogLevel.None&&e>=this._options.minimumLevel}trace(e){const s=new a;s.level=t.LogLevel.Trace,s.message=e,this.logMessage(s)}debug(e){const s=new a;s.level=t.LogLevel.Debug,s.message=e,this.logMessage(s)}info(e){const s=new a;s.level=t.LogLevel.Information,s.message=e,this.logMessage(s)}warn(e){const s=new a;s.level=t.LogLevel.Warning,s.message=e,this.logMessage(s)}error(e){const s=new a;s.level=t.LogLevel.Error,s.message=e,this.logMessage(s)}crit(e){const s=new a;s.level=t.LogLevel.Critical,s.message=e,this.logMessage(s)}log(e,s,n,o){const r=new a,c=this._extractErrorDetails(n);r.level=e,r.message=s,r.errorMessage=c?.message,r.stackTrace=c?.stack,r.extraParams=o,this.logMessage(r)}logMessage(e){this.isEnabled(e.level)&&setTimeout(()=>{this.logMessageCore(e)},1)}_extractErrorDetails(e){if(e instanceof Error)return{message:e.message,stack:e.stack};switch(typeof e){case"string":return{message:e};case"object":{const s=e;if(s===null)break;const n={message:s.message??s.Message,stack:s.stack??s.Stack??s.stackTrace??s.StackTrace};return n.message??=JSON.stringify(e),n}default:{const s=e;if(typeof s?.toString=="function")return{message:s.toString()};break}}}}class f{name="";reporter=null;minimumLevel=t.LogLevel.Warning;enrichers=[];static getLevel(e){switch((e||"").toUpperCase()){case"TRACE":return t.LogLevel.Trace;case"DEBUG":return t.LogLevel.Debug;case"INFORMATION":return t.LogLevel.Information;case"WARNING":return t.LogLevel.Warning;case"ERROR":return t.LogLevel.Error;case"CRITICAL":return t.LogLevel.Critical;case"NONE":return t.LogLevel.None;default:return t.LogLevel.None}}}class p{_console;constructor(e){this._console=e}register(e){let s;if(this._console)switch(e.level){case t.LogLevel.Trace:s=this._console.trace||this._console.log;break;case t.LogLevel.Debug:s=this._console.debug||this._console.log;break;case t.LogLevel.Information:s=this._console.info||this._console.log;break;case t.LogLevel.Warning:s=this._console.warn||this._console.log;break;case t.LogLevel.Error:s=this._console.error||this._console.log;break;case t.LogLevel.Critical:s=this._console.error||this._console.log;break;default:s=null;break}typeof s=="function"&&s.call(this._console,e.message,e)}[Symbol.asyncDispose](){return Promise.resolve()}}class d{_messages=[];get messages(){return this._messages.slice()}register(e){this._messages.push(e)}[Symbol.asyncDispose](){return Promise.resolve()}}class b{_reporters;constructor(e){this._reporters=e||[]}register(e){for(const s of this._reporters)s.register(e)}async[Symbol.asyncDispose](){const e=[];for(const s of this._reporters)e.push(s[Symbol.asyncDispose]());e.length&&await Promise.all(e)}}class v{endpoint="";verb="POST";batchSize=20;interval=2e3;requestTransform}class L{_messageQueue;_options;_reportActionTimeoutRef;_reportActionPromise;_disposed;constructor(e){if(!e)throw new Error('Argument "options" is required');this._messageQueue=[],this._options=e,this._reportActionTimeoutRef=void 0,this._reportActionPromise=null,this._disposed=!1}register(e){this._disposed||(this._messageQueue.push(e),this._scheduleNextProcessAction())}async[Symbol.asyncDispose](){this._disposed||(await(this._reportActionPromise??this._processMessages()),this._disposed=!0)}_scheduleNextProcessAction(){if(this._reportActionTimeoutRef)return;const e=this._messageQueue.length>=this._options.batchSize?0:this._options.interval;this._reportActionTimeoutRef=setTimeout(()=>{this._reportActionPromise=this._processMessages().then(()=>{const s=this._reportActionTimeoutRef;this._reportActionTimeoutRef=void 0,clearTimeout(s),this._reportActionPromise=null,this._scheduleNextProcessAction()})},e)}async _processMessages(){let e,s;for(;this._messageQueue.length>0;)if(e=this._messageQueue.splice(0,Math.min(this._messageQueue.length,this._options.batchSize)),s=await this._sendMessagesBatch(e),!s){this._messageQueue.unshift(...e);return}}_sendMessagesBatch(e){return new Promise(s=>{const n=()=>{s(!1)},o=new XMLHttpRequest;o.open(this._options.verb,this._options.endpoint),o.setRequestHeader("Content-Type","application/json;charset=UTF-8"),this._options.requestTransform&&this._options.requestTransform(o),o.onload=function(){s(this.status>=200&&this.status<300)},o.onerror=n,o.onabort=n,o.send(JSON.stringify(e))})}}class y{_options;_channel;_eventListeners=null;_broadcastEnabled=!0;constructor(e){this._options=e,this._channel=new BroadcastChannel(this._options.channelName)}init(e){this._eventListeners||(this._eventListeners=s=>{const n=s.data;if(!(!n.topic||!n.message))try{this._broadcastEnabled=!1,e.publish(n.topic,n.message)}finally{this._broadcastEnabled=!0}},this._channel.addEventListener("message",this._eventListeners))}onPublish(e){if(!this._broadcastEnabled||!e.topic||!e.message)return;const s={topic:e.topic,message:e.message};this._channel.postMessage(s)}[Symbol.dispose](){this._eventListeners&&this._channel.removeEventListener("message",this._eventListeners),this._channel.close()}}class T{_logger;_logLevel;constructor(e,s=t.LogLevel.Information){this._logger=e,this._logLevel=s}onPublish(e){this._logger.log(this._logLevel,`Publishing message to topic: ${e.topic}`,void 0,{topic:e.topic??null,message:e.message??null})}}const l=(i,e)=>{clearTimeout(e.ref),e.ref=void 0;const s=i.timeouts.indexOf(e);i.timeouts.splice(s,1)};class P{_subscriptions=new Map;_options;constructor(e){if(this._options=e,this._options?.plugins)for(const s of this._options.plugins)s.init&&s.init(this)}publish(e,s){const n={topic:e,message:s};if(this._options?.plugins)for(const r of this._options.plugins)r.onPublish&&r.onPublish(n);if(!n.topic)throw new Error("Invalid topic.");if(!n.message)throw new Error("Invalid message.");const o=this._subscriptions.get(n.topic);if(o)for(const r of o.values()){const c={};r.timeouts.push(c),c.ref=setTimeout((w,E,M,k)=>{w.handler(M,k),l(r,E)},0,r,c,structuredClone(n.topic),structuredClone(n.message))}}subscribe(e,s){if(!e)throw new Error("Invalid topic.");if(!s)throw new Error("Invalid handler.");let n=this._subscriptions.get(e);n||(n=new Map,this._subscriptions.set(structuredClone(e),n));const o=`sub-${Date.now()}-${Math.random().toString(16).slice(2)}`;return n.set(o,{handler:s,timeouts:[]}),o}unsubscribe(e){if(e)for(const s of this._subscriptions.values()){for(const n of s.values())for(;;){const o=n.timeouts.pop();if(!o)break;l(n,o)}if(s.delete(e))return}}[Symbol.dispose](){for(const e of this._subscriptions.values())for(const s of e.keys())this.unsubscribe(s);if(this._subscriptions.clear(),this._options?.plugins)for(const e of this._options.plugins){const s=e[Symbol.dispose];s&&s.call(e)}return Promise.resolve()}}function u(i=1,e){return new Promise((s,n)=>{setTimeout(()=>{e?n(e):s()},i)})}function h(i=1){return i<=0?Promise.resolve():u(0).then(()=>h(i-1))}t.BroadcastChannelPlugin=y,t.ConsoleReporter=p,t.DynamicValuesEnricher=g,t.InMemoryReporter=d,t.LogMessage=a,t.Logger=m,t.LoggerOptions=f,t.LoggerPlugin=T,t.MultipleReporter=b,t.PubSubHub=P,t.ValuesEnricher=_,t.XhrReporter=L,t.XhrReporterOptions=v,t.delay=u,t.nextTicks=h,Object.defineProperty(t,Symbol.toStringTag,{value:"Module"})}));
1
+ (function(t,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(t=typeof globalThis<"u"?globalThis:t||self,a(t.adaptArch_utilitiEs={}))})(this,(function(t){"use strict";t.LogLevel=void 0,(function(n){n[n.Trace=0]="Trace",n[n.Debug=1]="Debug",n[n.Information=2]="Information",n[n.Warning=3]="Warning",n[n.Error=4]="Error",n[n.Critical=5]="Critical",n[n.None=6]="None"})(t.LogLevel||(t.LogLevel={}));class a{timestamp=Date.now();level=t.LogLevel.None;name="";message="";errorMessage;stackTrace;extraParams}class g{_valuesFn;_overrideExisting;constructor(e,s){this._valuesFn=e,this._overrideExisting=s}enrich(e){const s=typeof this._valuesFn=="function"?this._valuesFn():void 0;if(!s)return;e.extraParams=e.extraParams||{};const i=Object.keys(e.extraParams);for(const o in s)i.includes(o)&&!this._overrideExisting||(e.extraParams[o]=s[o])}}class _{_values;_overrideExisting;constructor(e,s){this._values=e,this._overrideExisting=s}enrich(e){if(!this._values)return;e.extraParams=e.extraParams||{};const s=Object.keys(e.extraParams);for(const i in this._values)s.includes(i)&&!this._overrideExisting||(e.extraParams[i]=this._values[i])}}class m{_options;constructor(e){this._options=e}logMessageCore(e){e.name=this._options.name;for(const s of this._options.enrichers)s.enrich(e);this._options.reporter?.register(e)}async[Symbol.asyncDispose](){await this._options.reporter?.[Symbol.asyncDispose]()}isEnabled(e){return e!==t.LogLevel.None&&e>=this._options.minimumLevel}trace(e){const s=new a;s.level=t.LogLevel.Trace,s.message=e,this.logMessage(s)}debug(e){const s=new a;s.level=t.LogLevel.Debug,s.message=e,this.logMessage(s)}info(e){const s=new a;s.level=t.LogLevel.Information,s.message=e,this.logMessage(s)}warn(e){const s=new a;s.level=t.LogLevel.Warning,s.message=e,this.logMessage(s)}error(e){const s=new a;s.level=t.LogLevel.Error,s.message=e,this.logMessage(s)}crit(e){const s=new a;s.level=t.LogLevel.Critical,s.message=e,this.logMessage(s)}log(e,s,i,o){const r=new a,c=this._extractErrorDetails(i);r.level=e,r.message=s,r.errorMessage=c?.message,r.stackTrace=c?.stack,r.extraParams=o,this.logMessage(r)}logMessage(e){this.isEnabled(e.level)&&setTimeout(()=>{this.logMessageCore(e)},1)}_extractErrorDetails(e){if(e instanceof Error)return{message:e.message,stack:e.stack};switch(typeof e){case"string":return{message:e};case"object":{const s=e;if(s===null)break;const i={message:s.message??s.Message,stack:s.stack??s.Stack??s.stackTrace??s.StackTrace};return i.message??=JSON.stringify(e),i}default:{const s=e;if(typeof s?.toString=="function")return{message:s.toString()};break}}}}class f{name="";reporter=null;minimumLevel=t.LogLevel.Warning;enrichers=[];static getLevel(e){switch((e||"").toUpperCase()){case"TRACE":return t.LogLevel.Trace;case"DEBUG":return t.LogLevel.Debug;case"INFORMATION":return t.LogLevel.Information;case"WARNING":return t.LogLevel.Warning;case"ERROR":return t.LogLevel.Error;case"CRITICAL":return t.LogLevel.Critical;case"NONE":return t.LogLevel.None;default:return t.LogLevel.None}}}class p{_console;constructor(e){this._console=e}register(e){let s;if(this._console)switch(e.level){case t.LogLevel.Trace:s=this._console.trace||this._console.log;break;case t.LogLevel.Debug:s=this._console.debug||this._console.log;break;case t.LogLevel.Information:s=this._console.info||this._console.log;break;case t.LogLevel.Warning:s=this._console.warn||this._console.log;break;case t.LogLevel.Error:s=this._console.error||this._console.log;break;case t.LogLevel.Critical:s=this._console.error||this._console.log;break;default:s=null;break}typeof s=="function"&&s.call(this._console,e.message,e)}[Symbol.asyncDispose](){return Promise.resolve()}}class d{_messages=[];get messages(){return this._messages.slice()}register(e){this._messages.push(e)}[Symbol.asyncDispose](){return Promise.resolve()}}class v{_reporters;constructor(e){this._reporters=e||[]}register(e){for(const s of this._reporters)s.register(e)}async[Symbol.asyncDispose](){const e=[];for(const s of this._reporters)e.push(s[Symbol.asyncDispose]());e.length&&await Promise.all(e)}}class b{endpoint="";verb="POST";batchSize=20;interval=2e3;requestTransform}class L{_messageQueue;_options;_reportActionTimeoutRef;_reportActionPromise;_disposed;constructor(e){if(!e)throw new Error('Argument "options" is required');this._messageQueue=[],this._options=e,this._reportActionTimeoutRef=void 0,this._reportActionPromise=null,this._disposed=!1}register(e){this._disposed||(this._messageQueue.push(e),this._scheduleNextProcessAction())}async[Symbol.asyncDispose](){this._disposed||(await(this._reportActionPromise??this._processMessages()),this._disposed=!0)}_scheduleNextProcessAction(){if(this._reportActionTimeoutRef)return;const e=this._messageQueue.length>=this._options.batchSize?0:this._options.interval;this._reportActionTimeoutRef=setTimeout(()=>{this._reportActionPromise=this._processMessages().then(()=>{const s=this._reportActionTimeoutRef;this._reportActionTimeoutRef=void 0,clearTimeout(s),this._reportActionPromise=null,this._scheduleNextProcessAction()})},e)}async _processMessages(){let e,s;for(;this._messageQueue.length>0;)if(e=this._messageQueue.splice(0,Math.min(this._messageQueue.length,this._options.batchSize)),s=await this._sendMessagesBatch(e),!s){this._messageQueue.unshift(...e);return}}_sendMessagesBatch(e){return new Promise(s=>{const i=()=>{s(!1)},o=new XMLHttpRequest;o.open(this._options.verb,this._options.endpoint),o.setRequestHeader("Content-Type","application/json;charset=UTF-8"),this._options.requestTransform&&this._options.requestTransform(o),o.onload=function(){s(this.status>=200&&this.status<300)},o.onerror=i,o.onabort=i,o.send(JSON.stringify(e))})}}class y{_options;_channel;_instanceId;_eventListeners=null;constructor(e){this._options=e,this._channel=new BroadcastChannel(this._options.channelName),this._instanceId=crypto.randomUUID()}_addInternals(e,s){const i=e.__adaInternals||{};return{...e,__adaInternals:{...i,...s}}}_removeInternals(e){const{__adaInternals:s,...i}=e;return i}init(e){this._eventListeners||(this._eventListeners=s=>{const i=s.data;if(!i.topic||!i.message)return;const o=this._addInternals(i.message,{fromBroadcast:this._instanceId});e.publish(i.topic,o)},this._channel.addEventListener("message",this._eventListeners))}onPublish(e){if(!e.topic||!e.message)return;const s=e.message.__adaInternals,i=s?.fromBroadcast===this._instanceId;if(s&&(e.message=this._removeInternals(e.message)),i)return;const o={topic:e.topic,message:e.message};this._channel.postMessage(o)}[Symbol.dispose](){this._eventListeners&&this._channel.removeEventListener("message",this._eventListeners),this._channel.close()}}class T{_logger;_logLevel;constructor(e,s=t.LogLevel.Information){this._logger=e,this._logLevel=s}onPublish(e){this._logger.log(this._logLevel,`Publishing message to topic: ${e.topic}`,void 0,{topic:e.topic??null,message:e.message??null})}}const l=(n,e)=>{clearTimeout(e.ref),e.ref=void 0;const s=n.timeouts.indexOf(e);n.timeouts.splice(s,1)};class P{_subscriptions=new Map;_options;constructor(e){if(this._options=e,this._options?.plugins)for(const s of this._options.plugins)s.init&&s.init(this)}publish(e,s){const i={topic:e,message:s};if(this._options?.plugins)for(const r of this._options.plugins)r.onPublish&&r.onPublish(i);if(!i.topic)throw new Error("Invalid topic.");if(!i.message)throw new Error("Invalid message.");const o=this._subscriptions.get(i.topic);if(o)for(const r of o.values()){const c={};r.timeouts.push(c),c.ref=setTimeout((w,M,E,I)=>{w.handler(E,I),l(r,M)},0,r,c,structuredClone(i.topic),structuredClone(i.message))}}subscribe(e,s){if(!e)throw new Error("Invalid topic.");if(!s)throw new Error("Invalid handler.");let i=this._subscriptions.get(e);i||(i=new Map,this._subscriptions.set(structuredClone(e),i));const o=`sub-${Date.now()}-${Math.random().toString(16).slice(2)}`;return i.set(o,{handler:s,timeouts:[]}),o}unsubscribe(e){if(e)for(const s of this._subscriptions.values()){for(const i of s.values())for(;;){const o=i.timeouts.pop();if(!o)break;l(i,o)}if(s.delete(e))return}}[Symbol.dispose](){for(const e of this._subscriptions.values())for(const s of e.keys())this.unsubscribe(s);if(this._subscriptions.clear(),this._options?.plugins)for(const e of this._options.plugins){const s=e[Symbol.dispose];s&&s.call(e)}return Promise.resolve()}}function u(n=1,e){return new Promise((s,i)=>{setTimeout(()=>{e?i(e):s()},n)})}function h(n=1){return n<=0?Promise.resolve():u(0).then(()=>h(n-1))}t.BroadcastChannelPlugin=y,t.ConsoleReporter=p,t.DynamicValuesEnricher=g,t.InMemoryReporter=d,t.LogMessage=a,t.Logger=m,t.LoggerOptions=f,t.LoggerPlugin=T,t.MultipleReporter=v,t.PubSubHub=P,t.ValuesEnricher=_,t.XhrReporter=L,t.XhrReporterOptions=b,t.delay=u,t.nextTicks=h,Object.defineProperty(t,Symbol.toStringTag,{value:"Module"})}));
@@ -0,0 +1,17 @@
1
+ import type { MessageData } from "./contracts";
2
+ /**
3
+ * Internal metadata used by utiliti-es plugins and components.
4
+ * This property is reserved for internal use and may be stripped or modified by the framework.
5
+ * Users should not set this property manually.
6
+ */
7
+ export type AdaInternals = {
8
+ /** UUID of the BroadcastChannelPlugin instance that added this message */
9
+ fromBroadcast?: string;
10
+ };
11
+ /**
12
+ * Extended message data that may contain internal metadata.
13
+ * @internal
14
+ */
15
+ export type MessageDataWithInternals = MessageData & {
16
+ __adaInternals?: AdaInternals;
17
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -8,19 +8,38 @@ export type Options = {
8
8
  channelName: string;
9
9
  };
10
10
  /**
11
- * A plugin to log messages.
11
+ * A plugin that broadcasts PubSub messages across browser tabs/windows using BroadcastChannel API.
12
+ *
13
+ * Automatically prevents infinite loops by tagging messages with a unique instance ID.
14
+ * The internal metadata (__adaInternals) is stripped before messages reach subscribers.
12
15
  */
13
16
  export declare class BroadcastChannelPlugin implements PubSubPlugin {
14
17
  private readonly _options;
15
18
  private readonly _channel;
19
+ private readonly _instanceId;
16
20
  private _eventListeners;
17
- private _broadcastEnabled;
18
21
  /**
19
22
  * Constructor.
20
23
  *
21
24
  * @param {Options} options The options.
22
25
  */
23
26
  constructor(options: Options);
27
+ /**
28
+ * Adds internal metadata to a message.
29
+ * Merges with existing __adaInternals if present.
30
+ *
31
+ * @param {MessageData} message The message to add internals to.
32
+ * @param {Record<string, unknown>} internals The internals to add.
33
+ * @returns {MessageData} The message with internals added.
34
+ */
35
+ private _addInternals;
36
+ /**
37
+ * Removes __adaInternals from a message before it reaches subscribers.
38
+ *
39
+ * @param {MessageData} message The message to remove internals from.
40
+ * @returns {MessageData} The message with internals removed.
41
+ */
42
+ private _removeInternals;
24
43
  /** @inheritdoc */
25
44
  init(hub: IPubSubHub): void;
26
45
  /** @inheritdoc */
@@ -1,11 +1,14 @@
1
1
  /**
2
- * A plugin to log messages.
2
+ * A plugin that broadcasts PubSub messages across browser tabs/windows using BroadcastChannel API.
3
+ *
4
+ * Automatically prevents infinite loops by tagging messages with a unique instance ID.
5
+ * The internal metadata (__adaInternals) is stripped before messages reach subscribers.
3
6
  */
4
7
  export class BroadcastChannelPlugin {
5
8
  _options;
6
9
  _channel;
10
+ _instanceId;
7
11
  _eventListeners = null;
8
- _broadcastEnabled = true;
9
12
  /**
10
13
  * Constructor.
11
14
  *
@@ -14,6 +17,35 @@ export class BroadcastChannelPlugin {
14
17
  constructor(options) {
15
18
  this._options = options;
16
19
  this._channel = new BroadcastChannel(this._options.channelName);
20
+ this._instanceId = crypto.randomUUID();
21
+ }
22
+ /**
23
+ * Adds internal metadata to a message.
24
+ * Merges with existing __adaInternals if present.
25
+ *
26
+ * @param {MessageData} message The message to add internals to.
27
+ * @param {Record<string, unknown>} internals The internals to add.
28
+ * @returns {MessageData} The message with internals added.
29
+ */
30
+ _addInternals(message, internals) {
31
+ const existing = message.__adaInternals || {};
32
+ return {
33
+ ...message,
34
+ __adaInternals: {
35
+ ...existing,
36
+ ...internals,
37
+ },
38
+ };
39
+ }
40
+ /**
41
+ * Removes __adaInternals from a message before it reaches subscribers.
42
+ *
43
+ * @param {MessageData} message The message to remove internals from.
44
+ * @returns {MessageData} The message with internals removed.
45
+ */
46
+ _removeInternals(message) {
47
+ const { __adaInternals, ...clean } = message;
48
+ return clean;
17
49
  }
18
50
  /** @inheritdoc */
19
51
  init(hub) {
@@ -25,21 +57,32 @@ export class BroadcastChannelPlugin {
25
57
  if (!message.topic || !message.message) {
26
58
  return;
27
59
  }
28
- try {
29
- this._broadcastEnabled = false; // Prevent infinite loop due to broadcast
30
- hub.publish(message.topic, message.message);
31
- }
32
- finally {
33
- this._broadcastEnabled = true;
34
- }
60
+ // Add instance ID to prevent infinite loop due to broadcast
61
+ const messageWithMetadata = this._addInternals(message.message, {
62
+ fromBroadcast: this._instanceId,
63
+ });
64
+ hub.publish(message.topic, messageWithMetadata);
35
65
  };
36
66
  this._channel.addEventListener("message", this._eventListeners);
37
67
  }
38
68
  /** @inheritdoc */
39
69
  onPublish(context) {
40
- if (!this._broadcastEnabled || !context.topic || !context.message) {
70
+ if (!context.topic || !context.message) {
71
+ return;
72
+ }
73
+ // Check if this message originated from this plugin instance
74
+ const internals = context.message.__adaInternals;
75
+ const isFromThisInstance = internals?.fromBroadcast === this._instanceId;
76
+ // Always clean __adaInternals metadata before subscribers receive it
77
+ // (whether it came from broadcast or was manually added by user)
78
+ if (internals) {
79
+ context.message = this._removeInternals(context.message);
80
+ }
81
+ // Don't re-broadcast if this message came from this instance's broadcast channel
82
+ if (isFromThisInstance) {
41
83
  return;
42
84
  }
85
+ // Broadcast to other tabs (use the cleaned message)
43
86
  const message = {
44
87
  topic: context.topic,
45
88
  message: context.message,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adapt-arch/utiliti-es",
3
- "version": "0.4.3",
3
+ "version": "0.4.4",
4
4
  "license": "MIT",
5
5
  "description": "ECMAScript common utilities library",
6
6
  "keywords": [
@@ -51,20 +51,24 @@
51
51
  "docs:preview": "vitepress preview docs"
52
52
  },
53
53
  "devDependencies": {
54
- "@biomejs/biome": "2.3.4",
55
- "@types/node": "24.10.0",
56
- "@vitest/coverage-v8": "4.0.8",
57
- "jsdom": "27.1.0",
58
- "msw": "2.12.0",
59
- "typedoc": "0.28.14",
54
+ "@biomejs/biome": "2.4.1",
55
+ "@types/node": "25.2.3",
56
+ "@vitest/coverage-v8": "4.0.18",
57
+ "jsdom": "28.1.0",
58
+ "msw": "2.12.10",
59
+ "typedoc": "0.28.17",
60
60
  "typescript": "5.9.3",
61
- "vite": "7.2.2",
61
+ "vite": "7.3.1",
62
62
  "vitepress": "1.6.4",
63
- "vitest": "4.0.8"
63
+ "vitest": "4.0.18"
64
64
  },
65
65
  "msw": {
66
66
  "workerDirectory": [
67
67
  "public"
68
68
  ]
69
+ },
70
+ "overrides": {
71
+ "esbuild": ">=0.25.0",
72
+ "minimatch": ">=10.2.1"
69
73
  }
70
74
  }