@adapt-arch/utiliti-es 0.4.2 → 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.
- package/dist/bundle/utiliti-es.cjs +1 -1
- package/dist/bundle/utiliti-es.iife.js +1 -1
- package/dist/bundle/utiliti-es.js +106 -77
- package/dist/bundle/utiliti-es.umd.cjs +1 -1
- package/dist/logger/enrichers/dynamicValuesEnricher.js +1 -1
- package/dist/logger/enrichers/valuesEnricher.js +1 -1
- package/dist/logger/logger.js +1 -3
- package/dist/logger/reporters/xhrReporter.js +1 -1
- package/dist/pubsub/internalContracts.d.ts +17 -0
- package/dist/pubsub/internalContracts.js +1 -0
- package/dist/pubsub/plugins/broadcastChannelPlugin.d.ts +21 -2
- package/dist/pubsub/plugins/broadcastChannelPlugin.js +53 -10
- package/dist/pubsub/pubsub.js +9 -9
- package/package.json +13 -9
|
@@ -1 +1 @@
|
|
|
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
|
|
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(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
|
|
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
|
|
1
|
+
var n;
|
|
2
2
|
(function(i) {
|
|
3
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
|
-
})(
|
|
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 =
|
|
13
|
+
level = n.None;
|
|
14
14
|
/**
|
|
15
15
|
* The name of the logger.
|
|
16
16
|
*/
|
|
@@ -53,8 +53,8 @@ class f {
|
|
|
53
53
|
return;
|
|
54
54
|
s.extraParams = s.extraParams || {};
|
|
55
55
|
const t = Object.keys(s.extraParams);
|
|
56
|
-
for (const
|
|
57
|
-
t.
|
|
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 {
|
|
@@ -78,7 +78,7 @@ class d {
|
|
|
78
78
|
s.extraParams = s.extraParams || {};
|
|
79
79
|
const e = Object.keys(s.extraParams);
|
|
80
80
|
for (const t in this._values)
|
|
81
|
-
e.
|
|
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 !==
|
|
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 =
|
|
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 =
|
|
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 =
|
|
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 =
|
|
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 =
|
|
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 =
|
|
157
|
+
e.level = n.Critical, e.message = s, this.logMessage(e);
|
|
158
158
|
}
|
|
159
159
|
/**
|
|
160
160
|
* @inheritdoc
|
|
161
161
|
*/
|
|
162
|
-
log(s, e, t,
|
|
163
|
-
const
|
|
164
|
-
|
|
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
|
|
@@ -185,7 +185,7 @@ class b {
|
|
|
185
185
|
message: e.message ?? e.Message,
|
|
186
186
|
stack: e.stack ?? e.Stack ?? e.stackTrace ?? e.StackTrace
|
|
187
187
|
};
|
|
188
|
-
return t.message
|
|
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 =
|
|
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
|
|
224
|
+
return n.Trace;
|
|
225
225
|
case "DEBUG":
|
|
226
|
-
return
|
|
226
|
+
return n.Debug;
|
|
227
227
|
case "INFORMATION":
|
|
228
|
-
return
|
|
228
|
+
return n.Information;
|
|
229
229
|
case "WARNING":
|
|
230
|
-
return
|
|
230
|
+
return n.Warning;
|
|
231
231
|
case "ERROR":
|
|
232
|
-
return
|
|
232
|
+
return n.Error;
|
|
233
233
|
case "CRITICAL":
|
|
234
|
-
return
|
|
234
|
+
return n.Critical;
|
|
235
235
|
case "NONE":
|
|
236
|
-
return
|
|
236
|
+
return n.None;
|
|
237
237
|
default:
|
|
238
|
-
return
|
|
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
|
|
259
|
+
case n.Trace:
|
|
260
260
|
e = this._console.trace || this._console.log;
|
|
261
261
|
break;
|
|
262
|
-
case
|
|
262
|
+
case n.Debug:
|
|
263
263
|
e = this._console.debug || this._console.log;
|
|
264
264
|
break;
|
|
265
|
-
case
|
|
265
|
+
case n.Information:
|
|
266
266
|
e = this._console.info || this._console.log;
|
|
267
267
|
break;
|
|
268
|
-
case
|
|
268
|
+
case n.Warning:
|
|
269
269
|
e = this._console.warn || this._console.log;
|
|
270
270
|
break;
|
|
271
|
-
case
|
|
271
|
+
case n.Error:
|
|
272
272
|
e = this._console.error || this._console.log;
|
|
273
273
|
break;
|
|
274
|
-
case
|
|
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
|
|
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
|
|
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
|
|
353
|
+
class I {
|
|
354
354
|
_messageQueue;
|
|
355
355
|
_options;
|
|
356
356
|
_reportActionTimeoutRef;
|
|
@@ -371,9 +371,7 @@ class E {
|
|
|
371
371
|
* @inheritdoc
|
|
372
372
|
*/
|
|
373
373
|
async [Symbol.asyncDispose]() {
|
|
374
|
-
|
|
375
|
-
return Promise.resolve();
|
|
376
|
-
await (this._reportActionPromise ?? this._processMessages()), this._disposed = !0;
|
|
374
|
+
this._disposed || (await (this._reportActionPromise ?? this._processMessages()), this._disposed = !0);
|
|
377
375
|
}
|
|
378
376
|
_scheduleNextProcessAction() {
|
|
379
377
|
if (this._reportActionTimeoutRef)
|
|
@@ -398,53 +396,84 @@ class E {
|
|
|
398
396
|
return new Promise((e) => {
|
|
399
397
|
const t = () => {
|
|
400
398
|
e(!1);
|
|
401
|
-
},
|
|
402
|
-
|
|
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() {
|
|
403
401
|
e(this.status >= 200 && this.status < 300);
|
|
404
|
-
},
|
|
402
|
+
}, r.onerror = t, r.onabort = t, r.send(JSON.stringify(s));
|
|
405
403
|
});
|
|
406
404
|
}
|
|
407
405
|
}
|
|
408
406
|
class k {
|
|
409
407
|
_options;
|
|
410
408
|
_channel;
|
|
409
|
+
_instanceId;
|
|
411
410
|
_eventListeners = null;
|
|
412
|
-
_broadcastEnabled = !0;
|
|
413
411
|
/**
|
|
414
412
|
* Constructor.
|
|
415
413
|
*
|
|
416
414
|
* @param {Options} options The options.
|
|
417
415
|
*/
|
|
418
416
|
constructor(s) {
|
|
419
|
-
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;
|
|
420
446
|
}
|
|
421
447
|
/** @inheritdoc */
|
|
422
448
|
init(s) {
|
|
423
449
|
this._eventListeners || (this._eventListeners = (e) => {
|
|
424
450
|
const t = e.data;
|
|
425
|
-
if (!
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
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);
|
|
431
457
|
}, this._channel.addEventListener("message", this._eventListeners));
|
|
432
458
|
}
|
|
433
459
|
/** @inheritdoc */
|
|
434
460
|
onPublish(s) {
|
|
435
|
-
if (!
|
|
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)
|
|
436
465
|
return;
|
|
437
|
-
const
|
|
466
|
+
const r = {
|
|
438
467
|
topic: s.topic,
|
|
439
468
|
message: s.message
|
|
440
469
|
};
|
|
441
|
-
this._channel.postMessage(
|
|
470
|
+
this._channel.postMessage(r);
|
|
442
471
|
}
|
|
443
472
|
[Symbol.dispose]() {
|
|
444
473
|
this._eventListeners && this._channel.removeEventListener("message", this._eventListeners), this._channel.close();
|
|
445
474
|
}
|
|
446
475
|
}
|
|
447
|
-
class
|
|
476
|
+
class E {
|
|
448
477
|
_logger;
|
|
449
478
|
_logLevel;
|
|
450
479
|
/**
|
|
@@ -453,7 +482,7 @@ class M {
|
|
|
453
482
|
* @param {Logger} logger The logger.
|
|
454
483
|
* @param {LogLevel} logLevel The log level.
|
|
455
484
|
*/
|
|
456
|
-
constructor(s, e =
|
|
485
|
+
constructor(s, e = n.Information) {
|
|
457
486
|
this._logger = s, this._logLevel = e;
|
|
458
487
|
}
|
|
459
488
|
/** @inheritdoc */
|
|
@@ -469,7 +498,7 @@ const l = (i, s) => {
|
|
|
469
498
|
const e = i.timeouts.indexOf(s);
|
|
470
499
|
i.timeouts.splice(e, 1);
|
|
471
500
|
};
|
|
472
|
-
class
|
|
501
|
+
class M {
|
|
473
502
|
_subscriptions = /* @__PURE__ */ new Map();
|
|
474
503
|
_options;
|
|
475
504
|
/**
|
|
@@ -487,19 +516,19 @@ class x {
|
|
|
487
516
|
message: e
|
|
488
517
|
};
|
|
489
518
|
if (this._options?.plugins)
|
|
490
|
-
for (const
|
|
491
|
-
|
|
519
|
+
for (const o of this._options.plugins)
|
|
520
|
+
o.onPublish && o.onPublish(t);
|
|
492
521
|
if (!t.topic)
|
|
493
522
|
throw new Error("Invalid topic.");
|
|
494
523
|
if (!t.message)
|
|
495
524
|
throw new Error("Invalid message.");
|
|
496
|
-
const
|
|
497
|
-
if (
|
|
498
|
-
for (const
|
|
525
|
+
const r = this._subscriptions.get(t.topic);
|
|
526
|
+
if (r)
|
|
527
|
+
for (const o of r.values()) {
|
|
499
528
|
const c = {};
|
|
500
|
-
|
|
501
|
-
u.handler(g,
|
|
502
|
-
}, 0,
|
|
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));
|
|
503
532
|
}
|
|
504
533
|
}
|
|
505
534
|
/** @inheritdoc */
|
|
@@ -510,8 +539,8 @@ class x {
|
|
|
510
539
|
throw new Error("Invalid handler.");
|
|
511
540
|
let t = this._subscriptions.get(s);
|
|
512
541
|
t || (t = /* @__PURE__ */ new Map(), this._subscriptions.set(structuredClone(s), t));
|
|
513
|
-
const
|
|
514
|
-
return t.set(
|
|
542
|
+
const r = `sub-${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
543
|
+
return t.set(r, { handler: e, timeouts: [] }), r;
|
|
515
544
|
}
|
|
516
545
|
/** @inheritdoc */
|
|
517
546
|
unsubscribe(s) {
|
|
@@ -519,10 +548,10 @@ class x {
|
|
|
519
548
|
for (const e of this._subscriptions.values()) {
|
|
520
549
|
for (const t of e.values())
|
|
521
550
|
for (; ; ) {
|
|
522
|
-
const
|
|
523
|
-
if (!
|
|
551
|
+
const r = t.timeouts.pop();
|
|
552
|
+
if (!r)
|
|
524
553
|
break;
|
|
525
|
-
l(t,
|
|
554
|
+
l(t, r);
|
|
526
555
|
}
|
|
527
556
|
if (e.delete(s))
|
|
528
557
|
return;
|
|
@@ -540,7 +569,7 @@ class x {
|
|
|
540
569
|
return Promise.resolve();
|
|
541
570
|
}
|
|
542
571
|
}
|
|
543
|
-
function
|
|
572
|
+
function p(i = 1, s) {
|
|
544
573
|
return new Promise((e, t) => {
|
|
545
574
|
setTimeout(() => {
|
|
546
575
|
s ? t(s) : e();
|
|
@@ -548,23 +577,23 @@ function _(i = 1, s) {
|
|
|
548
577
|
});
|
|
549
578
|
}
|
|
550
579
|
function m(i = 1) {
|
|
551
|
-
return i <= 0 ? Promise.resolve() :
|
|
580
|
+
return i <= 0 ? Promise.resolve() : p(0).then(() => m(i - 1));
|
|
552
581
|
}
|
|
553
582
|
export {
|
|
554
583
|
k as BroadcastChannelPlugin,
|
|
555
584
|
y as ConsoleReporter,
|
|
556
585
|
f as DynamicValuesEnricher,
|
|
557
|
-
|
|
558
|
-
|
|
586
|
+
T as InMemoryReporter,
|
|
587
|
+
n as LogLevel,
|
|
559
588
|
a as LogMessage,
|
|
560
589
|
b as Logger,
|
|
561
590
|
v as LoggerOptions,
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
591
|
+
E as LoggerPlugin,
|
|
592
|
+
w as MultipleReporter,
|
|
593
|
+
M as PubSubHub,
|
|
565
594
|
d as ValuesEnricher,
|
|
566
|
-
|
|
595
|
+
I as XhrReporter,
|
|
567
596
|
P as XhrReporterOptions,
|
|
568
|
-
|
|
597
|
+
p as delay,
|
|
569
598
|
m as nextTicks
|
|
570
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(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.
|
|
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"})}));
|
|
@@ -22,7 +22,7 @@ export class DynamicValuesEnricher {
|
|
|
22
22
|
message.extraParams = message.extraParams || {};
|
|
23
23
|
const existingKeys = Object.keys(message.extraParams);
|
|
24
24
|
for (const name in values) {
|
|
25
|
-
if (existingKeys.
|
|
25
|
+
if (existingKeys.includes(name) && !this._overrideExisting) {
|
|
26
26
|
continue;
|
|
27
27
|
}
|
|
28
28
|
message.extraParams[name] = values[name];
|
|
@@ -21,7 +21,7 @@ export class ValuesEnricher {
|
|
|
21
21
|
message.extraParams = message.extraParams || {};
|
|
22
22
|
const existingKeys = Object.keys(message.extraParams);
|
|
23
23
|
for (const name in this._values) {
|
|
24
|
-
if (existingKeys.
|
|
24
|
+
if (existingKeys.includes(name) && !this._overrideExisting) {
|
|
25
25
|
continue;
|
|
26
26
|
}
|
|
27
27
|
message.extraParams[name] = this._values[name];
|
package/dist/logger/logger.js
CHANGED
|
@@ -129,9 +129,7 @@ export class Logger {
|
|
|
129
129
|
message: (record.message ?? record.Message),
|
|
130
130
|
stack: (record.stack ?? record.Stack ?? record.stackTrace ?? record.StackTrace),
|
|
131
131
|
};
|
|
132
|
-
|
|
133
|
-
r.message = JSON.stringify(error);
|
|
134
|
-
}
|
|
132
|
+
r.message ??= JSON.stringify(error);
|
|
135
133
|
return r;
|
|
136
134
|
}
|
|
137
135
|
default: {
|
|
@@ -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
|
|
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
|
|
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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
|
|
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 (!
|
|
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/dist/pubsub/pubsub.js
CHANGED
|
@@ -64,13 +64,13 @@ export class PubSubHub {
|
|
|
64
64
|
if (!handler) {
|
|
65
65
|
throw new Error("Invalid handler.");
|
|
66
66
|
}
|
|
67
|
-
let
|
|
68
|
-
if (!
|
|
69
|
-
|
|
70
|
-
this._subscriptions.set(structuredClone(topic),
|
|
67
|
+
let subscriptionTrackers = this._subscriptions.get(topic);
|
|
68
|
+
if (!subscriptionTrackers) {
|
|
69
|
+
subscriptionTrackers = new Map();
|
|
70
|
+
this._subscriptions.set(structuredClone(topic), subscriptionTrackers);
|
|
71
71
|
}
|
|
72
|
-
const subscriptionId = `sub-${Date.now()}`;
|
|
73
|
-
|
|
72
|
+
const subscriptionId = `sub-${Date.now()}-${Math.random().toString(16).slice(2)}`; // NOSONAR S2245 Non-cryptographic randomness is acceptable here
|
|
73
|
+
subscriptionTrackers.set(subscriptionId, { handler: handler, timeouts: [] });
|
|
74
74
|
return subscriptionId;
|
|
75
75
|
}
|
|
76
76
|
/** @inheritdoc */
|
|
@@ -78,8 +78,8 @@ export class PubSubHub {
|
|
|
78
78
|
if (!subscriptionId) {
|
|
79
79
|
return;
|
|
80
80
|
}
|
|
81
|
-
for (const
|
|
82
|
-
for (const tracker of
|
|
81
|
+
for (const subscriptionTrackers of this._subscriptions.values()) {
|
|
82
|
+
for (const tracker of subscriptionTrackers.values()) {
|
|
83
83
|
while (true) {
|
|
84
84
|
const timeout = tracker.timeouts.pop();
|
|
85
85
|
if (!timeout) {
|
|
@@ -88,7 +88,7 @@ export class PubSubHub {
|
|
|
88
88
|
clearTimeoutRef(tracker, timeout);
|
|
89
89
|
}
|
|
90
90
|
}
|
|
91
|
-
if (
|
|
91
|
+
if (subscriptionTrackers.delete(subscriptionId)) {
|
|
92
92
|
return;
|
|
93
93
|
}
|
|
94
94
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adapt-arch/utiliti-es",
|
|
3
|
-
"version": "0.4.
|
|
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.
|
|
55
|
-
"@types/node": "
|
|
56
|
-
"@vitest/coverage-v8": "
|
|
57
|
-
"jsdom": "
|
|
58
|
-
"msw": "2.
|
|
59
|
-
"typedoc": "0.28.
|
|
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.1
|
|
61
|
+
"vite": "7.3.1",
|
|
62
62
|
"vitepress": "1.6.4",
|
|
63
|
-
"vitest": "
|
|
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
|
}
|