@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.
- package/dist/bundle/utiliti-es.cjs +1 -1
- package/dist/bundle/utiliti-es.iife.js +1 -1
- package/dist/bundle/utiliti-es.js +125 -94
- package/dist/bundle/utiliti-es.umd.cjs +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/package.json +13 -9
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});exports.LogLevel=void 0;(function(
|
|
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(
|
|
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
|
|
2
|
-
(function(
|
|
3
|
-
|
|
4
|
-
})(
|
|
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 =
|
|
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
|
|
56
|
-
for (const
|
|
57
|
-
|
|
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
|
|
81
|
-
e.includes(
|
|
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 !==
|
|
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,
|
|
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
|
|
@@ -181,11 +181,11 @@ class b {
|
|
|
181
181
|
const e = s;
|
|
182
182
|
if (e === null)
|
|
183
183
|
break;
|
|
184
|
-
const
|
|
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
|
|
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;
|
|
@@ -394,55 +394,86 @@ class E {
|
|
|
394
394
|
}
|
|
395
395
|
_sendMessagesBatch(s) {
|
|
396
396
|
return new Promise((e) => {
|
|
397
|
-
const
|
|
397
|
+
const t = () => {
|
|
398
398
|
e(!1);
|
|
399
|
-
},
|
|
400
|
-
|
|
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
|
-
},
|
|
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
|
|
423
|
-
if (!
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
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 (!
|
|
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
|
|
466
|
+
const r = {
|
|
436
467
|
topic: s.topic,
|
|
437
468
|
message: s.message
|
|
438
469
|
};
|
|
439
|
-
this._channel.postMessage(
|
|
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
|
|
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 =
|
|
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 = (
|
|
496
|
+
const l = (i, s) => {
|
|
466
497
|
clearTimeout(s.ref), s.ref = void 0;
|
|
467
|
-
const e =
|
|
468
|
-
|
|
498
|
+
const e = i.timeouts.indexOf(s);
|
|
499
|
+
i.timeouts.splice(e, 1);
|
|
469
500
|
};
|
|
470
|
-
class
|
|
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
|
|
514
|
+
const t = {
|
|
484
515
|
topic: s,
|
|
485
516
|
message: e
|
|
486
517
|
};
|
|
487
518
|
if (this._options?.plugins)
|
|
488
|
-
for (const
|
|
489
|
-
|
|
490
|
-
if (!
|
|
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 (!
|
|
523
|
+
if (!t.message)
|
|
493
524
|
throw new Error("Invalid message.");
|
|
494
|
-
const
|
|
495
|
-
if (
|
|
496
|
-
for (const
|
|
525
|
+
const r = this._subscriptions.get(t.topic);
|
|
526
|
+
if (r)
|
|
527
|
+
for (const o of r.values()) {
|
|
497
528
|
const c = {};
|
|
498
|
-
|
|
499
|
-
u.handler(g,
|
|
500
|
-
}, 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));
|
|
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
|
|
510
|
-
|
|
511
|
-
const
|
|
512
|
-
return
|
|
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
|
|
549
|
+
for (const t of e.values())
|
|
519
550
|
for (; ; ) {
|
|
520
|
-
const
|
|
521
|
-
if (!
|
|
551
|
+
const r = t.timeouts.pop();
|
|
552
|
+
if (!r)
|
|
522
553
|
break;
|
|
523
|
-
l(
|
|
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
|
|
542
|
-
return new Promise((e,
|
|
572
|
+
function p(i = 1, s) {
|
|
573
|
+
return new Promise((e, t) => {
|
|
543
574
|
setTimeout(() => {
|
|
544
|
-
s ?
|
|
545
|
-
},
|
|
575
|
+
s ? t(s) : e();
|
|
576
|
+
}, i);
|
|
546
577
|
});
|
|
547
578
|
}
|
|
548
|
-
function m(
|
|
549
|
-
return
|
|
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
|
-
|
|
556
|
-
|
|
586
|
+
T as InMemoryReporter,
|
|
587
|
+
n as LogLevel,
|
|
557
588
|
a as LogMessage,
|
|
558
589
|
b as Logger,
|
|
559
590
|
v as LoggerOptions,
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
591
|
+
E as LoggerPlugin,
|
|
592
|
+
w as MultipleReporter,
|
|
593
|
+
M as PubSubHub,
|
|
563
594
|
d as ValuesEnricher,
|
|
564
|
-
|
|
595
|
+
I as XhrReporter,
|
|
565
596
|
P as XhrReporterOptions,
|
|
566
|
-
|
|
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(
|
|
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
|
|
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/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": "4.0.
|
|
57
|
-
"jsdom": "
|
|
58
|
-
"msw": "2.12.
|
|
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.
|
|
61
|
+
"vite": "7.3.1",
|
|
62
62
|
"vitepress": "1.6.4",
|
|
63
|
-
"vitest": "4.0.
|
|
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
|
}
|