@adapt-arch/utiliti-es 0.4.2 → 0.4.3
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 +38 -40
- 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/pubsub.js +9 -9
- package/package.json +8 -8
|
@@ -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(t){t[t.Trace=0]="Trace",t[t.Debug=1]="Debug",t[t.Information=2]="Information",t[t.Warning=3]="Warning",t[t.Error=4]="Error",t[t.Critical=5]="Critical",t[t.None=6]="None"})(exports.LogLevel||(exports.LogLevel={}));class n{timestamp=Date.now();level=exports.LogLevel.None;name="";message="";errorMessage;stackTrace;extraParams}class m{_valuesFn;_overrideExisting;constructor(e,s){this._valuesFn=e,this._overrideExisting=s}enrich(e){const s=typeof this._valuesFn=="function"?this._valuesFn():void 0;if(!s)return;e.extraParams=e.extraParams||{};const i=Object.keys(e.extraParams);for(const o in s)i.includes(o)&&!this._overrideExisting||(e.extraParams[o]=s[o])}}class f{_values;_overrideExisting;constructor(e,s){this._values=e,this._overrideExisting=s}enrich(e){if(!this._values)return;e.extraParams=e.extraParams||{};const s=Object.keys(e.extraParams);for(const i in this._values)s.includes(i)&&!this._overrideExisting||(e.extraParams[i]=this._values[i])}}class d{_options;constructor(e){this._options=e}logMessageCore(e){e.name=this._options.name;for(const s of this._options.enrichers)s.enrich(e);this._options.reporter?.register(e)}async[Symbol.asyncDispose](){await this._options.reporter?.[Symbol.asyncDispose]()}isEnabled(e){return e!==exports.LogLevel.None&&e>=this._options.minimumLevel}trace(e){const s=new n;s.level=exports.LogLevel.Trace,s.message=e,this.logMessage(s)}debug(e){const s=new n;s.level=exports.LogLevel.Debug,s.message=e,this.logMessage(s)}info(e){const s=new n;s.level=exports.LogLevel.Information,s.message=e,this.logMessage(s)}warn(e){const s=new n;s.level=exports.LogLevel.Warning,s.message=e,this.logMessage(s)}error(e){const s=new n;s.level=exports.LogLevel.Error,s.message=e,this.logMessage(s)}crit(e){const s=new n;s.level=exports.LogLevel.Critical,s.message=e,this.logMessage(s)}log(e,s,i,o){const r=new n,a=this._extractErrorDetails(i);r.level=e,r.message=s,r.errorMessage=a?.message,r.stackTrace=a?.stack,r.extraParams=o,this.logMessage(r)}logMessage(e){this.isEnabled(e.level)&&setTimeout(()=>{this.logMessageCore(e)},1)}_extractErrorDetails(e){if(e instanceof Error)return{message:e.message,stack:e.stack};switch(typeof e){case"string":return{message:e};case"object":{const s=e;if(s===null)break;const i={message:s.message??s.Message,stack:s.stack??s.Stack??s.stackTrace??s.StackTrace};return i.message??=JSON.stringify(e),i}default:{const s=e;if(typeof s?.toString=="function")return{message:s.toString()};break}}}}class b{name="";reporter=null;minimumLevel=exports.LogLevel.Warning;enrichers=[];static getLevel(e){switch((e||"").toUpperCase()){case"TRACE":return exports.LogLevel.Trace;case"DEBUG":return exports.LogLevel.Debug;case"INFORMATION":return exports.LogLevel.Information;case"WARNING":return exports.LogLevel.Warning;case"ERROR":return exports.LogLevel.Error;case"CRITICAL":return exports.LogLevel.Critical;case"NONE":return exports.LogLevel.None;default:return exports.LogLevel.None}}}class v{_console;constructor(e){this._console=e}register(e){let s;if(this._console)switch(e.level){case exports.LogLevel.Trace:s=this._console.trace||this._console.log;break;case exports.LogLevel.Debug:s=this._console.debug||this._console.log;break;case exports.LogLevel.Information:s=this._console.info||this._console.log;break;case exports.LogLevel.Warning:s=this._console.warn||this._console.log;break;case exports.LogLevel.Error:s=this._console.error||this._console.log;break;case exports.LogLevel.Critical:s=this._console.error||this._console.log;break;default:s=null;break}typeof s=="function"&&s.call(this._console,e.message,e)}[Symbol.asyncDispose](){return Promise.resolve()}}class L{_messages=[];get messages(){return this._messages.slice()}register(e){this._messages.push(e)}[Symbol.asyncDispose](){return Promise.resolve()}}class y{_reporters;constructor(e){this._reporters=e||[]}register(e){for(const s of this._reporters)s.register(e)}async[Symbol.asyncDispose](){const e=[];for(const s of this._reporters)e.push(s[Symbol.asyncDispose]());e.length&&await Promise.all(e)}}class P{endpoint="";verb="POST";batchSize=20;interval=2e3;requestTransform}class T{_messageQueue;_options;_reportActionTimeoutRef;_reportActionPromise;_disposed;constructor(e){if(!e)throw new Error('Argument "options" is required');this._messageQueue=[],this._options=e,this._reportActionTimeoutRef=void 0,this._reportActionPromise=null,this._disposed=!1}register(e){this._disposed||(this._messageQueue.push(e),this._scheduleNextProcessAction())}async[Symbol.asyncDispose](){this._disposed||(await(this._reportActionPromise??this._processMessages()),this._disposed=!0)}_scheduleNextProcessAction(){if(this._reportActionTimeoutRef)return;const e=this._messageQueue.length>=this._options.batchSize?0:this._options.interval;this._reportActionTimeoutRef=setTimeout(()=>{this._reportActionPromise=this._processMessages().then(()=>{const s=this._reportActionTimeoutRef;this._reportActionTimeoutRef=void 0,clearTimeout(s),this._reportActionPromise=null,this._scheduleNextProcessAction()})},e)}async _processMessages(){let e,s;for(;this._messageQueue.length>0;)if(e=this._messageQueue.splice(0,Math.min(this._messageQueue.length,this._options.batchSize)),s=await this._sendMessagesBatch(e),!s){this._messageQueue.unshift(...e);return}}_sendMessagesBatch(e){return new Promise(s=>{const i=()=>{s(!1)},o=new XMLHttpRequest;o.open(this._options.verb,this._options.endpoint),o.setRequestHeader("Content-Type","application/json;charset=UTF-8"),this._options.requestTransform&&this._options.requestTransform(o),o.onload=function(){s(this.status>=200&&this.status<300)},o.onerror=i,o.onabort=i,o.send(JSON.stringify(e))})}}class w{_options;_channel;_eventListeners=null;_broadcastEnabled=!0;constructor(e){this._options=e,this._channel=new BroadcastChannel(this._options.channelName)}init(e){this._eventListeners||(this._eventListeners=s=>{const i=s.data;if(!(!i.topic||!i.message))try{this._broadcastEnabled=!1,e.publish(i.topic,i.message)}finally{this._broadcastEnabled=!0}},this._channel.addEventListener("message",this._eventListeners))}onPublish(e){if(!this._broadcastEnabled||!e.topic||!e.message)return;const s={topic:e.topic,message:e.message};this._channel.postMessage(s)}[Symbol.dispose](){this._eventListeners&&this._channel.removeEventListener("message",this._eventListeners),this._channel.close()}}class E{_logger;_logLevel;constructor(e,s=exports.LogLevel.Information){this._logger=e,this._logLevel=s}onPublish(e){this._logger.log(this._logLevel,`Publishing message to topic: ${e.topic}`,void 0,{topic:e.topic??null,message:e.message??null})}}const c=(t,e)=>{clearTimeout(e.ref),e.ref=void 0;const s=t.timeouts.indexOf(e);t.timeouts.splice(s,1)};class M{_subscriptions=new Map;_options;constructor(e){if(this._options=e,this._options?.plugins)for(const s of this._options.plugins)s.init&&s.init(this)}publish(e,s){const i={topic:e,message:s};if(this._options?.plugins)for(const r of this._options.plugins)r.onPublish&&r.onPublish(i);if(!i.topic)throw new Error("Invalid topic.");if(!i.message)throw new Error("Invalid message.");const o=this._subscriptions.get(i.topic);if(o)for(const r of o.values()){const a={};r.timeouts.push(a),a.ref=setTimeout((h,g,p,_)=>{h.handler(p,_),c(r,g)},0,r,a,structuredClone(i.topic),structuredClone(i.message))}}subscribe(e,s){if(!e)throw new Error("Invalid topic.");if(!s)throw new Error("Invalid handler.");let i=this._subscriptions.get(e);i||(i=new Map,this._subscriptions.set(structuredClone(e),i));const o=`sub-${Date.now()}-${Math.random().toString(16).slice(2)}`;return i.set(o,{handler:s,timeouts:[]}),o}unsubscribe(e){if(e)for(const s of this._subscriptions.values()){for(const i of s.values())for(;;){const o=i.timeouts.pop();if(!o)break;c(i,o)}if(s.delete(e))return}}[Symbol.dispose](){for(const e of this._subscriptions.values())for(const s of e.keys())this.unsubscribe(s);if(this._subscriptions.clear(),this._options?.plugins)for(const e of this._options.plugins){const s=e[Symbol.dispose];s&&s.call(e)}return Promise.resolve()}}function l(t=1,e){return new Promise((s,i)=>{setTimeout(()=>{e?i(e):s()},t)})}function u(t=1){return t<=0?Promise.resolve():l(0).then(()=>u(t-1))}exports.BroadcastChannelPlugin=w;exports.ConsoleReporter=v;exports.DynamicValuesEnricher=m;exports.InMemoryReporter=L;exports.LogMessage=n;exports.Logger=d;exports.LoggerOptions=b;exports.LoggerPlugin=E;exports.MultipleReporter=y;exports.PubSubHub=M;exports.ValuesEnricher=f;exports.XhrReporter=T;exports.XhrReporterOptions=P;exports.delay=l;exports.nextTicks=u;
|
|
@@ -1 +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(i){i[i.Trace=0]="Trace",i[i.Debug=1]="Debug",i[i.Information=2]="Information",i[i.Warning=3]="Warning",i[i.Error=4]="Error",i[i.Critical=5]="Critical",i[i.None=6]="None"})(t.LogLevel||(t.LogLevel={}));class a{timestamp=Date.now();level=t.LogLevel.None;name="";message="";errorMessage;stackTrace;extraParams}class g{_valuesFn;_overrideExisting;constructor(e,s){this._valuesFn=e,this._overrideExisting=s}enrich(e){const s=typeof this._valuesFn=="function"?this._valuesFn():void 0;if(!s)return;e.extraParams=e.extraParams||{};const n=Object.keys(e.extraParams);for(const o in s)n.includes(o)&&!this._overrideExisting||(e.extraParams[o]=s[o])}}class _{_values;_overrideExisting;constructor(e,s){this._values=e,this._overrideExisting=s}enrich(e){if(!this._values)return;e.extraParams=e.extraParams||{};const s=Object.keys(e.extraParams);for(const n in this._values)s.includes(n)&&!this._overrideExisting||(e.extraParams[n]=this._values[n])}}class m{_options;constructor(e){this._options=e}logMessageCore(e){e.name=this._options.name;for(const s of this._options.enrichers)s.enrich(e);this._options.reporter?.register(e)}async[Symbol.asyncDispose](){await this._options.reporter?.[Symbol.asyncDispose]()}isEnabled(e){return e!==t.LogLevel.None&&e>=this._options.minimumLevel}trace(e){const s=new a;s.level=t.LogLevel.Trace,s.message=e,this.logMessage(s)}debug(e){const s=new a;s.level=t.LogLevel.Debug,s.message=e,this.logMessage(s)}info(e){const s=new a;s.level=t.LogLevel.Information,s.message=e,this.logMessage(s)}warn(e){const s=new a;s.level=t.LogLevel.Warning,s.message=e,this.logMessage(s)}error(e){const s=new a;s.level=t.LogLevel.Error,s.message=e,this.logMessage(s)}crit(e){const s=new a;s.level=t.LogLevel.Critical,s.message=e,this.logMessage(s)}log(e,s,n,o){const r=new a,c=this._extractErrorDetails(n);r.level=e,r.message=s,r.errorMessage=c?.message,r.stackTrace=c?.stack,r.extraParams=o,this.logMessage(r)}logMessage(e){this.isEnabled(e.level)&&setTimeout(()=>{this.logMessageCore(e)},1)}_extractErrorDetails(e){if(e instanceof Error)return{message:e.message,stack:e.stack};switch(typeof e){case"string":return{message:e};case"object":{const s=e;if(s===null)break;const n={message:s.message??s.Message,stack:s.stack??s.Stack??s.stackTrace??s.StackTrace};return n.message??=JSON.stringify(e),n}default:{const s=e;if(typeof s?.toString=="function")return{message:s.toString()};break}}}}class f{name="";reporter=null;minimumLevel=t.LogLevel.Warning;enrichers=[];static getLevel(e){switch((e||"").toUpperCase()){case"TRACE":return t.LogLevel.Trace;case"DEBUG":return t.LogLevel.Debug;case"INFORMATION":return t.LogLevel.Information;case"WARNING":return t.LogLevel.Warning;case"ERROR":return t.LogLevel.Error;case"CRITICAL":return t.LogLevel.Critical;case"NONE":return t.LogLevel.None;default:return t.LogLevel.None}}}class p{_console;constructor(e){this._console=e}register(e){let s;if(this._console)switch(e.level){case t.LogLevel.Trace:s=this._console.trace||this._console.log;break;case t.LogLevel.Debug:s=this._console.debug||this._console.log;break;case t.LogLevel.Information:s=this._console.info||this._console.log;break;case t.LogLevel.Warning:s=this._console.warn||this._console.log;break;case t.LogLevel.Error:s=this._console.error||this._console.log;break;case t.LogLevel.Critical:s=this._console.error||this._console.log;break;default:s=null;break}typeof s=="function"&&s.call(this._console,e.message,e)}[Symbol.asyncDispose](){return Promise.resolve()}}class d{_messages=[];get messages(){return this._messages.slice()}register(e){this._messages.push(e)}[Symbol.asyncDispose](){return Promise.resolve()}}class b{_reporters;constructor(e){this._reporters=e||[]}register(e){for(const s of this._reporters)s.register(e)}async[Symbol.asyncDispose](){const e=[];for(const s of this._reporters)e.push(s[Symbol.asyncDispose]());e.length&&await Promise.all(e)}}class v{endpoint="";verb="POST";batchSize=20;interval=2e3;requestTransform}class L{_messageQueue;_options;_reportActionTimeoutRef;_reportActionPromise;_disposed;constructor(e){if(!e)throw new Error('Argument "options" is required');this._messageQueue=[],this._options=e,this._reportActionTimeoutRef=void 0,this._reportActionPromise=null,this._disposed=!1}register(e){this._disposed||(this._messageQueue.push(e),this._scheduleNextProcessAction())}async[Symbol.asyncDispose](){this._disposed||(await(this._reportActionPromise??this._processMessages()),this._disposed=!0)}_scheduleNextProcessAction(){if(this._reportActionTimeoutRef)return;const e=this._messageQueue.length>=this._options.batchSize?0:this._options.interval;this._reportActionTimeoutRef=setTimeout(()=>{this._reportActionPromise=this._processMessages().then(()=>{const s=this._reportActionTimeoutRef;this._reportActionTimeoutRef=void 0,clearTimeout(s),this._reportActionPromise=null,this._scheduleNextProcessAction()})},e)}async _processMessages(){let e,s;for(;this._messageQueue.length>0;)if(e=this._messageQueue.splice(0,Math.min(this._messageQueue.length,this._options.batchSize)),s=await this._sendMessagesBatch(e),!s){this._messageQueue.unshift(...e);return}}_sendMessagesBatch(e){return new Promise(s=>{const n=()=>{s(!1)},o=new XMLHttpRequest;o.open(this._options.verb,this._options.endpoint),o.setRequestHeader("Content-Type","application/json;charset=UTF-8"),this._options.requestTransform&&this._options.requestTransform(o),o.onload=function(){s(this.status>=200&&this.status<300)},o.onerror=n,o.onabort=n,o.send(JSON.stringify(e))})}}class y{_options;_channel;_eventListeners=null;_broadcastEnabled=!0;constructor(e){this._options=e,this._channel=new BroadcastChannel(this._options.channelName)}init(e){this._eventListeners||(this._eventListeners=s=>{const n=s.data;if(!(!n.topic||!n.message))try{this._broadcastEnabled=!1,e.publish(n.topic,n.message)}finally{this._broadcastEnabled=!0}},this._channel.addEventListener("message",this._eventListeners))}onPublish(e){if(!this._broadcastEnabled||!e.topic||!e.message)return;const s={topic:e.topic,message:e.message};this._channel.postMessage(s)}[Symbol.dispose](){this._eventListeners&&this._channel.removeEventListener("message",this._eventListeners),this._channel.close()}}class P{_logger;_logLevel;constructor(e,s=t.LogLevel.Information){this._logger=e,this._logLevel=s}onPublish(e){this._logger.log(this._logLevel,`Publishing message to topic: ${e.topic}`,void 0,{topic:e.topic??null,message:e.message??null})}}const l=(i,e)=>{clearTimeout(e.ref),e.ref=void 0;const s=i.timeouts.indexOf(e);i.timeouts.splice(s,1)};class T{_subscriptions=new Map;_options;constructor(e){if(this._options=e,this._options?.plugins)for(const s of this._options.plugins)s.init&&s.init(this)}publish(e,s){const n={topic:e,message:s};if(this._options?.plugins)for(const r of this._options.plugins)r.onPublish&&r.onPublish(n);if(!n.topic)throw new Error("Invalid topic.");if(!n.message)throw new Error("Invalid message.");const o=this._subscriptions.get(n.topic);if(o)for(const r of o.values()){const c={};r.timeouts.push(c),c.ref=setTimeout((w,E,M,k)=>{w.handler(M,k),l(r,E)},0,r,c,structuredClone(n.topic),structuredClone(n.message))}}subscribe(e,s){if(!e)throw new Error("Invalid topic.");if(!s)throw new Error("Invalid handler.");let n=this._subscriptions.get(e);n||(n=new Map,this._subscriptions.set(structuredClone(e),n));const o=`sub-${Date.now()}-${Math.random().toString(16).slice(2)}`;return n.set(o,{handler:s,timeouts:[]}),o}unsubscribe(e){if(e)for(const s of this._subscriptions.values()){for(const n of s.values())for(;;){const o=n.timeouts.pop();if(!o)break;l(n,o)}if(s.delete(e))return}}[Symbol.dispose](){for(const e of this._subscriptions.values())for(const s of e.keys())this.unsubscribe(s);if(this._subscriptions.clear(),this._options?.plugins)for(const e of this._options.plugins){const s=e[Symbol.dispose];s&&s.call(e)}return Promise.resolve()}}function u(i=1,e){return new Promise((s,n)=>{setTimeout(()=>{e?n(e):s()},i)})}function h(i=1){return i<=0?Promise.resolve():u(0).then(()=>h(i-1))}return t.BroadcastChannelPlugin=y,t.ConsoleReporter=p,t.DynamicValuesEnricher=g,t.InMemoryReporter=d,t.LogMessage=a,t.Logger=m,t.LoggerOptions=f,t.LoggerPlugin=P,t.MultipleReporter=b,t.PubSubHub=T,t.ValuesEnricher=_,t.XhrReporter=L,t.XhrReporterOptions=v,t.delay=u,t.nextTicks=h,Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),t})({});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
var r;
|
|
2
|
-
(function(
|
|
3
|
-
|
|
2
|
+
(function(t) {
|
|
3
|
+
t[t.Trace = 0] = "Trace", t[t.Debug = 1] = "Debug", t[t.Information = 2] = "Information", t[t.Warning = 3] = "Warning", t[t.Error = 4] = "Error", t[t.Critical = 5] = "Critical", t[t.None = 6] = "None";
|
|
4
4
|
})(r || (r = {}));
|
|
5
5
|
class a {
|
|
6
6
|
/**
|
|
@@ -52,9 +52,9 @@ class f {
|
|
|
52
52
|
if (!e)
|
|
53
53
|
return;
|
|
54
54
|
s.extraParams = s.extraParams || {};
|
|
55
|
-
const
|
|
55
|
+
const i = Object.keys(s.extraParams);
|
|
56
56
|
for (const o in e)
|
|
57
|
-
|
|
57
|
+
i.includes(o) && !this._overrideExisting || (s.extraParams[o] = e[o]);
|
|
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.
|
|
80
|
+
for (const i in this._values)
|
|
81
|
+
e.includes(i) && !this._overrideExisting || (s.extraParams[i] = this._values[i]);
|
|
82
82
|
}
|
|
83
83
|
}
|
|
84
84
|
class b {
|
|
@@ -159,8 +159,8 @@ class b {
|
|
|
159
159
|
/**
|
|
160
160
|
* @inheritdoc
|
|
161
161
|
*/
|
|
162
|
-
log(s, e,
|
|
163
|
-
const n = new a(), c = this._extractErrorDetails(
|
|
162
|
+
log(s, e, i, o) {
|
|
163
|
+
const n = new a(), c = this._extractErrorDetails(i);
|
|
164
164
|
n.level = s, n.message = e, n.errorMessage = c?.message, n.stackTrace = c?.stack, n.extraParams = o, this.logMessage(n);
|
|
165
165
|
}
|
|
166
166
|
/**
|
|
@@ -181,11 +181,11 @@ class b {
|
|
|
181
181
|
const e = s;
|
|
182
182
|
if (e === null)
|
|
183
183
|
break;
|
|
184
|
-
const
|
|
184
|
+
const i = {
|
|
185
185
|
message: e.message ?? e.Message,
|
|
186
186
|
stack: e.stack ?? e.Stack ?? e.stackTrace ?? e.StackTrace
|
|
187
187
|
};
|
|
188
|
-
return
|
|
188
|
+
return i.message ??= JSON.stringify(s), i;
|
|
189
189
|
}
|
|
190
190
|
default: {
|
|
191
191
|
const e = s;
|
|
@@ -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)
|
|
@@ -396,12 +394,12 @@ class E {
|
|
|
396
394
|
}
|
|
397
395
|
_sendMessagesBatch(s) {
|
|
398
396
|
return new Promise((e) => {
|
|
399
|
-
const
|
|
397
|
+
const i = () => {
|
|
400
398
|
e(!1);
|
|
401
399
|
}, o = new XMLHttpRequest();
|
|
402
400
|
o.open(this._options.verb, this._options.endpoint), o.setRequestHeader("Content-Type", "application/json;charset=UTF-8"), this._options.requestTransform && this._options.requestTransform(o), o.onload = function() {
|
|
403
401
|
e(this.status >= 200 && this.status < 300);
|
|
404
|
-
}, o.onerror =
|
|
402
|
+
}, o.onerror = i, o.onabort = i, o.send(JSON.stringify(s));
|
|
405
403
|
});
|
|
406
404
|
}
|
|
407
405
|
}
|
|
@@ -421,10 +419,10 @@ class k {
|
|
|
421
419
|
/** @inheritdoc */
|
|
422
420
|
init(s) {
|
|
423
421
|
this._eventListeners || (this._eventListeners = (e) => {
|
|
424
|
-
const
|
|
425
|
-
if (!(!
|
|
422
|
+
const i = e.data;
|
|
423
|
+
if (!(!i.topic || !i.message))
|
|
426
424
|
try {
|
|
427
|
-
this._broadcastEnabled = !1, s.publish(
|
|
425
|
+
this._broadcastEnabled = !1, s.publish(i.topic, i.message);
|
|
428
426
|
} finally {
|
|
429
427
|
this._broadcastEnabled = !0;
|
|
430
428
|
}
|
|
@@ -464,10 +462,10 @@ class M {
|
|
|
464
462
|
});
|
|
465
463
|
}
|
|
466
464
|
}
|
|
467
|
-
const l = (
|
|
465
|
+
const l = (t, s) => {
|
|
468
466
|
clearTimeout(s.ref), s.ref = void 0;
|
|
469
|
-
const e =
|
|
470
|
-
|
|
467
|
+
const e = t.timeouts.indexOf(s);
|
|
468
|
+
t.timeouts.splice(e, 1);
|
|
471
469
|
};
|
|
472
470
|
class x {
|
|
473
471
|
_subscriptions = /* @__PURE__ */ new Map();
|
|
@@ -482,24 +480,24 @@ class x {
|
|
|
482
480
|
}
|
|
483
481
|
/** @inheritdoc */
|
|
484
482
|
publish(s, e) {
|
|
485
|
-
const
|
|
483
|
+
const i = {
|
|
486
484
|
topic: s,
|
|
487
485
|
message: e
|
|
488
486
|
};
|
|
489
487
|
if (this._options?.plugins)
|
|
490
488
|
for (const n of this._options.plugins)
|
|
491
|
-
n.onPublish && n.onPublish(
|
|
492
|
-
if (!
|
|
489
|
+
n.onPublish && n.onPublish(i);
|
|
490
|
+
if (!i.topic)
|
|
493
491
|
throw new Error("Invalid topic.");
|
|
494
|
-
if (!
|
|
492
|
+
if (!i.message)
|
|
495
493
|
throw new Error("Invalid message.");
|
|
496
|
-
const o = this._subscriptions.get(
|
|
494
|
+
const o = this._subscriptions.get(i.topic);
|
|
497
495
|
if (o)
|
|
498
496
|
for (const n of o.values()) {
|
|
499
497
|
const c = {};
|
|
500
498
|
n.timeouts.push(c), c.ref = setTimeout((u, h, g, p) => {
|
|
501
499
|
u.handler(g, p), l(n, h);
|
|
502
|
-
}, 0, n, c, structuredClone(
|
|
500
|
+
}, 0, n, c, structuredClone(i.topic), structuredClone(i.message));
|
|
503
501
|
}
|
|
504
502
|
}
|
|
505
503
|
/** @inheritdoc */
|
|
@@ -508,21 +506,21 @@ class x {
|
|
|
508
506
|
throw new Error("Invalid topic.");
|
|
509
507
|
if (!e)
|
|
510
508
|
throw new Error("Invalid handler.");
|
|
511
|
-
let
|
|
512
|
-
|
|
513
|
-
const o = `sub-${Date.now()}`;
|
|
514
|
-
return
|
|
509
|
+
let i = this._subscriptions.get(s);
|
|
510
|
+
i || (i = /* @__PURE__ */ new Map(), this._subscriptions.set(structuredClone(s), i));
|
|
511
|
+
const o = `sub-${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
512
|
+
return i.set(o, { handler: e, timeouts: [] }), o;
|
|
515
513
|
}
|
|
516
514
|
/** @inheritdoc */
|
|
517
515
|
unsubscribe(s) {
|
|
518
516
|
if (s)
|
|
519
517
|
for (const e of this._subscriptions.values()) {
|
|
520
|
-
for (const
|
|
518
|
+
for (const i of e.values())
|
|
521
519
|
for (; ; ) {
|
|
522
|
-
const o =
|
|
520
|
+
const o = i.timeouts.pop();
|
|
523
521
|
if (!o)
|
|
524
522
|
break;
|
|
525
|
-
l(
|
|
523
|
+
l(i, o);
|
|
526
524
|
}
|
|
527
525
|
if (e.delete(s))
|
|
528
526
|
return;
|
|
@@ -540,15 +538,15 @@ class x {
|
|
|
540
538
|
return Promise.resolve();
|
|
541
539
|
}
|
|
542
540
|
}
|
|
543
|
-
function _(
|
|
544
|
-
return new Promise((e,
|
|
541
|
+
function _(t = 1, s) {
|
|
542
|
+
return new Promise((e, i) => {
|
|
545
543
|
setTimeout(() => {
|
|
546
|
-
s ?
|
|
547
|
-
},
|
|
544
|
+
s ? i(s) : e();
|
|
545
|
+
}, t);
|
|
548
546
|
});
|
|
549
547
|
}
|
|
550
|
-
function m(
|
|
551
|
-
return
|
|
548
|
+
function m(t = 1) {
|
|
549
|
+
return t <= 0 ? Promise.resolve() : _(0).then(() => m(t - 1));
|
|
552
550
|
}
|
|
553
551
|
export {
|
|
554
552
|
k as BroadcastChannelPlugin,
|
|
@@ -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(i){i[i.Trace=0]="Trace",i[i.Debug=1]="Debug",i[i.Information=2]="Information",i[i.Warning=3]="Warning",i[i.Error=4]="Error",i[i.Critical=5]="Critical",i[i.None=6]="None"})(t.LogLevel||(t.LogLevel={}));class a{timestamp=Date.now();level=t.LogLevel.None;name="";message="";errorMessage;stackTrace;extraParams}class g{_valuesFn;_overrideExisting;constructor(e,s){this._valuesFn=e,this._overrideExisting=s}enrich(e){const s=typeof this._valuesFn=="function"?this._valuesFn():void 0;if(!s)return;e.extraParams=e.extraParams||{};const n=Object.keys(e.extraParams);for(const o in s)n.includes(o)&&!this._overrideExisting||(e.extraParams[o]=s[o])}}class _{_values;_overrideExisting;constructor(e,s){this._values=e,this._overrideExisting=s}enrich(e){if(!this._values)return;e.extraParams=e.extraParams||{};const s=Object.keys(e.extraParams);for(const n in this._values)s.includes(n)&&!this._overrideExisting||(e.extraParams[n]=this._values[n])}}class m{_options;constructor(e){this._options=e}logMessageCore(e){e.name=this._options.name;for(const s of this._options.enrichers)s.enrich(e);this._options.reporter?.register(e)}async[Symbol.asyncDispose](){await this._options.reporter?.[Symbol.asyncDispose]()}isEnabled(e){return e!==t.LogLevel.None&&e>=this._options.minimumLevel}trace(e){const s=new a;s.level=t.LogLevel.Trace,s.message=e,this.logMessage(s)}debug(e){const s=new a;s.level=t.LogLevel.Debug,s.message=e,this.logMessage(s)}info(e){const s=new a;s.level=t.LogLevel.Information,s.message=e,this.logMessage(s)}warn(e){const s=new a;s.level=t.LogLevel.Warning,s.message=e,this.logMessage(s)}error(e){const s=new a;s.level=t.LogLevel.Error,s.message=e,this.logMessage(s)}crit(e){const s=new a;s.level=t.LogLevel.Critical,s.message=e,this.logMessage(s)}log(e,s,n,o){const r=new a,c=this._extractErrorDetails(n);r.level=e,r.message=s,r.errorMessage=c?.message,r.stackTrace=c?.stack,r.extraParams=o,this.logMessage(r)}logMessage(e){this.isEnabled(e.level)&&setTimeout(()=>{this.logMessageCore(e)},1)}_extractErrorDetails(e){if(e instanceof Error)return{message:e.message,stack:e.stack};switch(typeof e){case"string":return{message:e};case"object":{const s=e;if(s===null)break;const n={message:s.message??s.Message,stack:s.stack??s.Stack??s.stackTrace??s.StackTrace};return n.message??=JSON.stringify(e),n}default:{const s=e;if(typeof s?.toString=="function")return{message:s.toString()};break}}}}class f{name="";reporter=null;minimumLevel=t.LogLevel.Warning;enrichers=[];static getLevel(e){switch((e||"").toUpperCase()){case"TRACE":return t.LogLevel.Trace;case"DEBUG":return t.LogLevel.Debug;case"INFORMATION":return t.LogLevel.Information;case"WARNING":return t.LogLevel.Warning;case"ERROR":return t.LogLevel.Error;case"CRITICAL":return t.LogLevel.Critical;case"NONE":return t.LogLevel.None;default:return t.LogLevel.None}}}class p{_console;constructor(e){this._console=e}register(e){let s;if(this._console)switch(e.level){case t.LogLevel.Trace:s=this._console.trace||this._console.log;break;case t.LogLevel.Debug:s=this._console.debug||this._console.log;break;case t.LogLevel.Information:s=this._console.info||this._console.log;break;case t.LogLevel.Warning:s=this._console.warn||this._console.log;break;case t.LogLevel.Error:s=this._console.error||this._console.log;break;case t.LogLevel.Critical:s=this._console.error||this._console.log;break;default:s=null;break}typeof s=="function"&&s.call(this._console,e.message,e)}[Symbol.asyncDispose](){return Promise.resolve()}}class d{_messages=[];get messages(){return this._messages.slice()}register(e){this._messages.push(e)}[Symbol.asyncDispose](){return Promise.resolve()}}class b{_reporters;constructor(e){this._reporters=e||[]}register(e){for(const s of this._reporters)s.register(e)}async[Symbol.asyncDispose](){const e=[];for(const s of this._reporters)e.push(s[Symbol.asyncDispose]());e.length&&await Promise.all(e)}}class v{endpoint="";verb="POST";batchSize=20;interval=2e3;requestTransform}class L{_messageQueue;_options;_reportActionTimeoutRef;_reportActionPromise;_disposed;constructor(e){if(!e)throw new Error('Argument "options" is required');this._messageQueue=[],this._options=e,this._reportActionTimeoutRef=void 0,this._reportActionPromise=null,this._disposed=!1}register(e){this._disposed||(this._messageQueue.push(e),this._scheduleNextProcessAction())}async[Symbol.asyncDispose](){this._disposed||(await(this._reportActionPromise??this._processMessages()),this._disposed=!0)}_scheduleNextProcessAction(){if(this._reportActionTimeoutRef)return;const e=this._messageQueue.length>=this._options.batchSize?0:this._options.interval;this._reportActionTimeoutRef=setTimeout(()=>{this._reportActionPromise=this._processMessages().then(()=>{const s=this._reportActionTimeoutRef;this._reportActionTimeoutRef=void 0,clearTimeout(s),this._reportActionPromise=null,this._scheduleNextProcessAction()})},e)}async _processMessages(){let e,s;for(;this._messageQueue.length>0;)if(e=this._messageQueue.splice(0,Math.min(this._messageQueue.length,this._options.batchSize)),s=await this._sendMessagesBatch(e),!s){this._messageQueue.unshift(...e);return}}_sendMessagesBatch(e){return new Promise(s=>{const n=()=>{s(!1)},o=new XMLHttpRequest;o.open(this._options.verb,this._options.endpoint),o.setRequestHeader("Content-Type","application/json;charset=UTF-8"),this._options.requestTransform&&this._options.requestTransform(o),o.onload=function(){s(this.status>=200&&this.status<300)},o.onerror=n,o.onabort=n,o.send(JSON.stringify(e))})}}class y{_options;_channel;_eventListeners=null;_broadcastEnabled=!0;constructor(e){this._options=e,this._channel=new BroadcastChannel(this._options.channelName)}init(e){this._eventListeners||(this._eventListeners=s=>{const n=s.data;if(!(!n.topic||!n.message))try{this._broadcastEnabled=!1,e.publish(n.topic,n.message)}finally{this._broadcastEnabled=!0}},this._channel.addEventListener("message",this._eventListeners))}onPublish(e){if(!this._broadcastEnabled||!e.topic||!e.message)return;const s={topic:e.topic,message:e.message};this._channel.postMessage(s)}[Symbol.dispose](){this._eventListeners&&this._channel.removeEventListener("message",this._eventListeners),this._channel.close()}}class T{_logger;_logLevel;constructor(e,s=t.LogLevel.Information){this._logger=e,this._logLevel=s}onPublish(e){this._logger.log(this._logLevel,`Publishing message to topic: ${e.topic}`,void 0,{topic:e.topic??null,message:e.message??null})}}const l=(i,e)=>{clearTimeout(e.ref),e.ref=void 0;const s=i.timeouts.indexOf(e);i.timeouts.splice(s,1)};class P{_subscriptions=new Map;_options;constructor(e){if(this._options=e,this._options?.plugins)for(const s of this._options.plugins)s.init&&s.init(this)}publish(e,s){const n={topic:e,message:s};if(this._options?.plugins)for(const r of this._options.plugins)r.onPublish&&r.onPublish(n);if(!n.topic)throw new Error("Invalid topic.");if(!n.message)throw new Error("Invalid message.");const o=this._subscriptions.get(n.topic);if(o)for(const r of o.values()){const c={};r.timeouts.push(c),c.ref=setTimeout((w,E,M,k)=>{w.handler(M,k),l(r,E)},0,r,c,structuredClone(n.topic),structuredClone(n.message))}}subscribe(e,s){if(!e)throw new Error("Invalid topic.");if(!s)throw new Error("Invalid handler.");let n=this._subscriptions.get(e);n||(n=new Map,this._subscriptions.set(structuredClone(e),n));const o=`sub-${Date.now()}-${Math.random().toString(16).slice(2)}`;return n.set(o,{handler:s,timeouts:[]}),o}unsubscribe(e){if(e)for(const s of this._subscriptions.values()){for(const n of s.values())for(;;){const o=n.timeouts.pop();if(!o)break;l(n,o)}if(s.delete(e))return}}[Symbol.dispose](){for(const e of this._subscriptions.values())for(const s of e.keys())this.unsubscribe(s);if(this._subscriptions.clear(),this._options?.plugins)for(const e of this._options.plugins){const s=e[Symbol.dispose];s&&s.call(e)}return Promise.resolve()}}function u(i=1,e){return new Promise((s,n)=>{setTimeout(()=>{e?n(e):s()},i)})}function h(i=1){return i<=0?Promise.resolve():u(0).then(()=>h(i-1))}t.BroadcastChannelPlugin=y,t.ConsoleReporter=p,t.DynamicValuesEnricher=g,t.InMemoryReporter=d,t.LogMessage=a,t.Logger=m,t.LoggerOptions=f,t.LoggerPlugin=T,t.MultipleReporter=b,t.PubSubHub=P,t.ValuesEnricher=_,t.XhrReporter=L,t.XhrReporterOptions=v,t.delay=u,t.nextTicks=h,Object.defineProperty(t,Symbol.toStringTag,{value:"Module"})}));
|
|
@@ -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: {
|
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.3",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "ECMAScript common utilities library",
|
|
6
6
|
"keywords": [
|
|
@@ -51,16 +51,16 @@
|
|
|
51
51
|
"docs:preview": "vitepress preview docs"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
|
-
"@biomejs/biome": "2.
|
|
55
|
-
"@types/node": "24.
|
|
56
|
-
"@vitest/coverage-v8": "
|
|
57
|
-
"jsdom": "27.
|
|
58
|
-
"msw": "2.
|
|
54
|
+
"@biomejs/biome": "2.3.4",
|
|
55
|
+
"@types/node": "24.10.0",
|
|
56
|
+
"@vitest/coverage-v8": "4.0.8",
|
|
57
|
+
"jsdom": "27.1.0",
|
|
58
|
+
"msw": "2.12.0",
|
|
59
59
|
"typedoc": "0.28.14",
|
|
60
60
|
"typescript": "5.9.3",
|
|
61
|
-
"vite": "7.
|
|
61
|
+
"vite": "7.2.2",
|
|
62
62
|
"vitepress": "1.6.4",
|
|
63
|
-
"vitest": "
|
|
63
|
+
"vitest": "4.0.8"
|
|
64
64
|
},
|
|
65
65
|
"msw": {
|
|
66
66
|
"workerDirectory": [
|