@stackone/transport 2.1.0 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +2 -2
- package/dist/index.mjs +2 -2
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -243,6 +243,7 @@ declare abstract class ScriptManager<T extends string, S extends ScriptManager<T
|
|
|
243
243
|
protected cacheClient: ICacheClient<RedisClientType>;
|
|
244
244
|
protected logger: ILogger;
|
|
245
245
|
protected additionalArgs: unknown[];
|
|
246
|
+
protected isInitialConnection: boolean;
|
|
246
247
|
protected initInstance({
|
|
247
248
|
redisClientConfig,
|
|
248
249
|
cacheClient,
|
|
@@ -252,6 +253,8 @@ declare abstract class ScriptManager<T extends string, S extends ScriptManager<T
|
|
|
252
253
|
additionalArgs
|
|
253
254
|
}: ScriptManagerOptions<T>): Promise<S>;
|
|
254
255
|
protected abstract additionalInitialization(...args: unknown[]): Promise<void>;
|
|
256
|
+
protected setupReconnectHandler(): void;
|
|
257
|
+
protected reloadScripts(): Promise<void>;
|
|
255
258
|
protected loadScripts(scripts: Scripts<T>, scriptMap?: Map<T, string>): Promise<void>;
|
|
256
259
|
protected executeScript<U>(method: T, keys: string[], args: string[]): Promise<U | null>;
|
|
257
260
|
protected isRedisReady(): boolean;
|
|
@@ -903,6 +906,7 @@ declare class SubscriptionManager {
|
|
|
903
906
|
private subscriptionMap;
|
|
904
907
|
private subscriptionClient;
|
|
905
908
|
private logger?;
|
|
909
|
+
name: string;
|
|
906
910
|
constructor(options: SubscriptionManagerOptions);
|
|
907
911
|
initialize(options?: SubscriptionManagerOptions): Promise<void>;
|
|
908
912
|
subscribe<T extends boolean = false>(key: string, listener: PubSubListener<T>): Promise<boolean>;
|
package/dist/index.d.ts
CHANGED
|
@@ -243,6 +243,7 @@ declare abstract class ScriptManager<T extends string, S extends ScriptManager<T
|
|
|
243
243
|
protected cacheClient: ICacheClient<RedisClientType>;
|
|
244
244
|
protected logger: ILogger;
|
|
245
245
|
protected additionalArgs: unknown[];
|
|
246
|
+
protected isInitialConnection: boolean;
|
|
246
247
|
protected initInstance({
|
|
247
248
|
redisClientConfig,
|
|
248
249
|
cacheClient,
|
|
@@ -252,6 +253,8 @@ declare abstract class ScriptManager<T extends string, S extends ScriptManager<T
|
|
|
252
253
|
additionalArgs
|
|
253
254
|
}: ScriptManagerOptions<T>): Promise<S>;
|
|
254
255
|
protected abstract additionalInitialization(...args: unknown[]): Promise<void>;
|
|
256
|
+
protected setupReconnectHandler(): void;
|
|
257
|
+
protected reloadScripts(): Promise<void>;
|
|
255
258
|
protected loadScripts(scripts: Scripts<T>, scriptMap?: Map<T, string>): Promise<void>;
|
|
256
259
|
protected executeScript<U>(method: T, keys: string[], args: string[]): Promise<U | null>;
|
|
257
260
|
protected isRedisReady(): boolean;
|
|
@@ -903,6 +906,7 @@ declare class SubscriptionManager {
|
|
|
903
906
|
private subscriptionMap;
|
|
904
907
|
private subscriptionClient;
|
|
905
908
|
private logger?;
|
|
909
|
+
name: string;
|
|
906
910
|
constructor(options: SubscriptionManagerOptions);
|
|
907
911
|
initialize(options?: SubscriptionManagerOptions): Promise<void>;
|
|
908
912
|
subscribe<T extends boolean = false>(key: string, listener: PubSubListener<T>): Promise<boolean>;
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports),s=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},c=(n,r,a)=>(a=n==null?{}:e(i(n)),s(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));const l=c(require(`@stackone/utils`)),u=c(require(`redis`)),d=c(require(`axios`)),f=c(require(`node:https`)),p=c(require(`crypto`)),m=c(require(`node:timers`)),h=c(require(`jsonpath`)),g=c(require(`@stackone/redaction`)),_=c(require(`qs`)),v=c(require(`@stackone/expressions`)),y=e=>{switch(e.type){case`none`:return{};case`basic`:return b(e);case`bearer`:return x(e);case`oauth2`:return x(e);default:throw Error(`Invalid authentication type`)}},b=({username:e=``,password:t=``,encoding:n=`base64`})=>{let r=n,i=Buffer.from(`${e}:${t}`).toString(r);return{authorization:`Basic ${i}`}},x=({token:e,includeBearer:t})=>({authorization:`${t?`Bearer `:``}${e}`});var S=class{locks;constructor(){this.locks=new Map}async withLock(e,t){await this.lock(e);try{return t()}finally{this.unlock(e)}}async lock(e){let t,n=new Promise(e=>t=e),r=this.locks.has(e),i=this.locks.get(e);if(r&&i){i.push({lock:n,unlock:t});let e=i.length-2;await i[e].lock}else this.locks.set(e,[{lock:n,unlock:t}])}unlock(e){let t=this.locks.has(e),n=this.locks.get(e);if(t&&n&&n.length>0){let e=n.shift()?.unlock;e?.()}this.queueLength(e)===0&&this.locks.delete(e)}queueLength(e){return this.locks.get(e)?.length}close(){this.locks.clear()}};const ee=1e3,C=100,te=60,w=6e4,T=te,ne=w,E=w*10,re=C,D=10;let O=function(e){return e.MemoryStorePruneError=`MemoryStorePruneError`,e}({});var ie=class{config;instantiator;dataStore;lockManager;expiryMap;evictionFrequency;staleDataThreshold;truncateThreshold;truncationPercentage;logger;typeGuard;dispose;evictionInterval;lastAccessedAt=Date.now();constructor(e={}){this.config=e,this.initialize(e)}initialize(e=this.config){this.instantiator=e?.instantiator??`Unknown`,this.logger=e?.logger,this.dataStore=e?.dataStore??new Map,this.lockManager=e?.lockManager??new S,this.expiryMap=e?.expiryMap??new Map,this.evictionFrequency=e?.evictionFrequency??ne,this.staleDataThreshold=e?.staleDataThreshold??E,this.truncateThreshold=e?.truncateThreshold??re,this.truncationPercentage=e?.truncationPercentage??D,this.typeGuard=e?.typeGuard,this.dispose=e?.dispose,this.startEvictionTask()}async getData(e){return this.isReady()||this.initialize(),this.updateLastAccessedAt(),this.lockManager.withLock(e,async()=>this.dataStore.get(e)??null)}async setData({key:e,value:t,cacheTTL:n=T}){if(this.isReady()||this.initialize(),this.updateLastAccessedAt(),(0,l.notMissing)(this.typeGuard)&&!this.typeGuard(t))return!1;let r=n*ee,i=Date.now()+r;return await this.lockManager.withLock(e,async()=>{((0,l.notMissing)(this.typeGuard)&&this.typeGuard(t)||(0,l.isMissing)(this.typeGuard)&&this.typeGuardBypass(t))&&this.dataStore.set(e,t),this.expiryMap.set(e,i)}),!0}typeGuardBypass(e){return this.logger?.debug({category:`MemoryStore`,message:`${this.instantiator} MemoryStore setting data without type guard - you should probably configure one`}),!0}async delete(e){return this.isReady()||this.initialize(),this.updateLastAccessedAt(),this.lockManager.withLock(e,async()=>{if(this.dispose){let t=this.dataStore.get(e);await this.dispose(e,t)}return this.dataStore.delete(e)})}async pruneExpiredKeys(){let e=this.dataStore.size,t=e>=this.truncateThreshold;if(e<=0)return;let n=[],r=0;this.dataStore.forEach(async(e,i)=>{let a=Date.now(),o=this.expiryMap.get(i)??0,s=this.truncateThreshold*this.truncationPercentage/C;(o<=a||t&&r>=0&&r<=s)&&n.push(this.lockManager.withLock(i,async()=>(this.dispose&&await this.dispose(i,e),this.dataStore.delete(i)))),r++}),await Promise.all(n);let i=this.dataStore.size;return{dataStoreSize:e,prunedDataStoreSize:i}}startEvictionTask(){if((0,l.notMissing)(this.evictionInterval))return;let e=async()=>{let t;try{let n=this.lastAccessedAt+this.staleDataThreshold,r=Date.now();if(n<r){this.logger?.warning({message:`Closing the ${this.instantiator}'s MemoryStore instance - received no requests for a while.`,category:`MemoryStore`}),this.close();return}t=await this.pruneExpiredKeys(),this.evictionInterval=setTimeout(e,this.evictionFrequency)}catch(t){t instanceof Error&&this.logger?.error({message:`Error during pruning expired keys:`,category:`MemoryStore`,error:t,code:O.MemoryStorePruneError}),this.evictionInterval=setTimeout(e,this.evictionFrequency)}finally{if((0,l.notMissing)(t?.dataStoreSize)&&(0,l.notMissing)(t?.prunedDataStoreSize)){let{dataStoreSize:e,prunedDataStoreSize:n}=t,r=e-n;this.logger?.debug({message:`Pruned ${r} expired keys, ${n} remain, scheduling next prune.`,category:`MemoryStore`,context:{instantiator:this.instantiator}})}}};this.evictionInterval=setTimeout(e,this.evictionFrequency)}stopEvictionTask(){this.evictionInterval&&(clearTimeout(this.evictionInterval),this.evictionInterval=void 0)}updateLastAccessedAt(){this.lastAccessedAt=Date.now()}isReady(){return(0,l.notMissing)(this.evictionInterval)&&(0,l.notMissing)(this.dataStore)&&(0,l.notMissing)(this.expiryMap)&&(0,l.notMissing)(this.lockManager)}close(){this.stopEvictionTask(),this.dataStore.clear(),this.expiryMap.clear(),this.lockManager.close()}async listData({partialKey:e,cursor:t,limit:n}){let r=Array.from(this.dataStore.keys()),i=r.filter(t=>t.includes(e)),a=[],o=t?parseInt(t,10):0;for(let e=o;e<n+o;e++){let t=i[e];if(!t)break;let r=await this.getData(t);if(r&&a.push(r),a.length>=n)break}return{items:a,cursor:a.length<n?void 0:(o+n).toString()}}};const ae=1e3,oe=6e4;let se=function(e){return e.EventClientResolveError=`EventClientResolveError`,e}({});var ce=class{executorMethodStore=null;promiseStore=null;logger;eventClientConfig=null;executorMethodStoreConfig=null;pendingPromiseStoreConfig=null;constructor(e,t,n){this.eventClientConfig=e,this.executorMethodStoreConfig=t,this.pendingPromiseStoreConfig=n;let r=e?.instantiator?`${e?.instantiator}(${this.constructor.name})`:this.constructor.name,i={logger:this.logger,dispose:this.defaultExecutorMethodDispose,typeGuard:this.defaultExecutorMethodTypeGuard,...this.executorMethodStoreConfig??{},instantiator:r},a={logger:this.logger,dispose:this.defaultPendingPromiseDispose,typeGuard:this.defaultPendingPromiseTypeGuard,...this.pendingPromiseStoreConfig??{},instantiator:r};this.executorMethodStore=new ie(i),this.promiseStore=new ie(a)}async setPendingEvent(e,t){let n={resolve:void 0,reject:void 0},r=new Promise((e,t)=>{n.resolve=e,n.reject=t}),i=t/ae;await this.executorMethodStore?.setData({key:e,value:n,cacheTTL:i}),await this.promiseStore?.setData({key:e,value:r,cacheTTL:i})}async waitForEvent(e,t){let n=await this.promiseStore?.getData(e);return(0,l.notMissing)(n)?n:(await this.setPendingEvent(e,t),this.getPendingEvent(e))}async getPendingEvent(e){return this.promiseStore?.getData(e)??null}async deleteEvent(e){return(await Promise.all([this.promiseStore?.delete(e),this.executorMethodStore?.delete(e)])).every(Boolean)}async resolveEvent(e,t){try{let n=await this.executorMethodStore?.getData(e);(0,l.isMissing)(n)&&(await this.setPendingEvent(e,this.eventClientConfig?.defaultTimeoutMS??oe),n=await this.executorMethodStore?.getData(e)),n?.resolve?.(t)}catch(t){t instanceof Error&&this.logger.error({message:`Error handling event for key ${e}: ${t.message}`,category:this.constructor.name,context:{eventKey:e},error:t,code:se.EventClientResolveError})}}async defaultExecutorMethodDispose(e,t){(0,l.notMissing)(t?.resolve)?t.resolve(this.eventClientConfig?.timeoutResolveValue):(0,l.notMissing)(t?.reject)&&t.reject(Error(`Event key: ${e} was not resolved or the event was disposed`))}defaultExecutorMethodTypeGuard(e){return(0,l.isObject)(e)&&e.hasOwnProperty(`resolve`)&&e.hasOwnProperty(`reject`)}async defaultPendingPromiseDispose(e,t){t instanceof Promise&&t?.finally(()=>{})}defaultPendingPromiseTypeGuard(e){return e instanceof Promise}},le=class{instance=null;initPromise=null;retryTimeout=null;status=`uninitialized`;initOptions=null;initArgs=[];getRetryDelay(){return 5e3}getSingleton(e){throw Error(`getSingleton method not implemented`)}async getInstance(e,...t){return this.instance?this.instance:((0,l.isMissing)(this.initPromise)&&(this.initOptions=e??null,this.initArgs=t,this.initPromise=this.init(e??null,...t)),this.initPromise)}getInstanceIfReady(){return this.instance}isReady(){return this.status===`ready`}hasInitFailed(){return this.status===`failed`}async init(e,...t){try{let n=await this.initInstance(e,...t);return this.instance=n,this.status=`ready`,n}catch(e){throw this.status=`failed`,this.initPromise=null,(0,l.isMissing)(this.retryTimeout)&&(this.retryTimeout=setTimeout(()=>{this.retryTimeout=null,this.getInstance(this.initOptions,...this.initArgs).catch(()=>{})},this.getRetryDelay())),e}}reset(){this.instance=null,this.initPromise=null,this.initOptions=null,this.status=`uninitialized`,this.retryTimeout&&clearTimeout(this.retryTimeout),this.retryTimeout=null}};let k=function(e){return e.RedisClientError=`RedisClientError`,e.RedisClientCommandError=`RedisClientCommandError`,e.RedisClientDeleteError=`RedisClientDeleteError`,e.RedisClientInvalidTTL=`RedisClientInvalidTTL`,e.RedisClientListError=`RedisClientListError`,e.RedisClientNotInitialized=`RedisClientNotInitialized`,e.RedisClientPublishError=`RedisClientPublishError`,e.RedisClientReadError=`RedisClientReadError`,e.RedisClientRedisNotReady=`RedisClientRedisNotReady`,e.RedisClientScriptError=`RedisClientScriptError`,e.RedisClientScriptExecuteError=`RedisClientScriptExecuteError`,e.RedisClientScriptLoadError=`RedisClientScriptLoadError`,e.RedisClientSubscribeError=`RedisClientSubscribeError`,e.RedisClientUnsubscribeError=`RedisClientUnsubscribeError`,e.RedisClientWriteError=`RedisClientWriteError`,e}({});const ue=async(e,t)=>{try{let n=await de.build(e,t);return n??void 0}catch(e){let n=e;t?.error({message:`Error building Cache Manager`,error:n,code:k.RedisClientError,category:`buildRedisClientInstance`});return}};var de=class e{#redisClient;#logger;static async build({getRedisClient:t=u.createClient,host:n,port:r,tls:i,reconnect:a=!0,database:o},s){let c=new e;c.#logger=s;try{c.#redisClient=t({socket:{reconnectStrategy:a?e=>Math.min(e*20,5e3):!1,host:n,port:r,tls:i},database:o,disableOfflineQueue:!0});let s=pe(e.name,c.#logger);return fe(c.#redisClient,s),await c.#redisClient.connect(),c}catch(t){let n=t;return c.#logger?.error({message:`Error building Cache Manager.`,error:n,code:k.RedisClientError,category:e.name}),null}}#parseData(e){try{return JSON.parse(e)}catch{return e}}async getData(t){if(!this.#isRedisClientAvailable())return null;try{let e=await this.#redisClient.get(t);if(!e)return null;let n=this.#parseData(e);return(0,l.isString)(n)?e:n}catch(t){let n=t;return this.#logger?.error({message:`Error getting data`,error:n,code:k.RedisClientReadError,category:e.name}),null}}async setData({key:t,value:n,cacheTTL:r,groupKey:i}){if(!this.#isRedisClientAvailable())return!1;try{let e=(0,l.isString)(n)?n:JSON.stringify(n);return await this.#redisClient.set(t,e,{EX:r}),(0,l.notMissing)(i)&&await this.#redisClient.sAdd(i,[t]),!0}catch(t){let n=t;return this.#logger?.error({message:`Error setting data`,error:n,code:k.RedisClientWriteError,category:e.name}),!1}}async executeScript({sha1:t,keys:n,args:r}){if(!this.#isRedisClientAvailable())return null;try{return await this.#redisClient.evalSha(t,{keys:n,arguments:r})}catch(t){let n=t;return this.#logger?.error({message:`Error executing script`,error:n,code:k.RedisClientScriptExecuteError,category:e.name}),null}}async loadScript(t){if(!this.#isRedisClientAvailable())return null;try{return await this.#redisClient.scriptLoad(t)}catch(t){let n=t;return this.#logger?.error({message:`Error loading script`,error:n,code:k.RedisClientScriptLoadError,category:e.name}),null}}async#executeCounterCommand(t,n,r){if(!this.#isRedisClientAvailable())return null;if(!Number.isFinite(r)||r<=0)return this.#logger?.error({message:`Invalid cacheTTL parameter`,category:e.name,code:k.RedisClientInvalidTTL}),null;try{let e=[{args:[t,n]},{args:[`expire`,n,r.toString()]}],i=await this.#redisClient.multiExecutor(e),a=i?.[0];return typeof a==`number`?a:null}catch(n){let r=n;return this.#logger?.error({message:`Error executing ${t} operation`,error:r,code:k.RedisClientCommandError,category:e.name}),null}}async increment(e,t){return this.#executeCounterCommand(`incr`,e,t)}async decrement(e,t){return this.#executeCounterCommand(`decr`,e,t)}async subscribe(t,n){if(!this.#isRedisClientAvailable())return!1;try{return await this.#redisClient.pSubscribe(t,n),!0}catch(n){let r=n;return this.#logger?.error({message:`Error subscribing to ${t}`,error:r,code:k.RedisClientSubscribeError,category:e.name}),!1}}async unsubscribe(t){if(!this.#isRedisClientAvailable())return!1;try{return await this.#redisClient.pUnsubscribe(t),!0}catch(n){let r=n;return this.#logger?.error({message:`Error unsubscribing from ${t}`,error:r,code:k.RedisClientUnsubscribeError,category:e.name}),!1}}async publish(t,n){if(!this.#isRedisClientAvailable())return null;try{let r=await this.#redisClient.publish(t,n);return r===0&&this.#logger?.debug({message:`No subscribers found for channel ${t}`,category:e.name}),r}catch(n){let r=n;return this.#logger?.error({message:`Error publishing to ${t}`,error:r,code:k.RedisClientPublishError,category:e.name}),null}}getClient(){return this.#isRedisClientAvailable()?this.#redisClient:null}#isRedisClientAvailable(){let t=this.#redisClient.isReady&&this.#redisClient.isOpen;return t||this.#logger?.error({message:`Redis Client is not ready.`,category:e.name,code:k.RedisClientRedisNotReady}),t}async listData({partialKey:t,limit:n,cursor:r}){try{let e=r?parseInt(r,10):0,i=await this.#redisClient.sScan(t,e,{COUNT:n}),a=[];for(let e of i.members){let t=await this.getData(e);(0,l.notMissing)(t)&&a.push(t)}return{items:a,cursor:i.cursor>0?i.cursor.toString():void 0}}catch(t){let n=t;return this.#logger?.error({message:`Error listing data`,error:n,code:k.RedisClientListError,category:e.name}),{items:null}}}async deleteData(t){try{if(!this.#isRedisClientAvailable())return!1;let n=await this.#redisClient.del(t);return n===0&&this.#logger?.debug({message:`No keys deleted for ${t}`,category:e.name}),n>0}catch(t){let n=t;return this.#logger?.error({message:`Error deleting data`,error:n,code:k.RedisClientDeleteError,category:e.name}),!1}}};const fe=(e,t)=>{t.forEach(({eventName:t,listener:n})=>{e.on(t,n)})},pe=(e=`RedisClient`,t)=>[{eventName:`error`,listener:n=>{t?.error({message:`Redis client error ${n.message}`,error:n,code:k.RedisClientError,category:e})}},{eventName:`ready`,listener:()=>{t?.info({message:`Redis client ready`,category:e})}},{eventName:`connect`,listener:()=>{t?.info({message:`Redis client connected`,category:e})}},{eventName:`reconnecting`,listener:()=>{t?.info({message:`Redis client reconnecting`,category:e})}},{eventName:`end`,listener:()=>{t?.info({message:`Redis client disconnected`,category:e})}}];let me=function(e){return e.ScriptManagerExecutionError=`ScriptManagerExecutionError`,e.ScriptManagerLoadingError=`ScriptManagerLoadingError`,e}({});var he=class extends le{redisClientConfig;scripts;scriptMap;cacheClient;logger;additionalArgs;async initInstance({redisClientConfig:e=this.redisClientConfig,cacheClient:t=this.cacheClient,scripts:n=this.scripts,scriptMap:r=this.scriptMap,logger:i=this.logger,additionalArgs:a=this.additionalArgs}){let o=t??await ue(e,i);if((0,l.isMissing)(o))throw Error(`Failed to build Redis client`);return this.redisClientConfig=e,this.logger=i,this.cacheClient=o,this.scripts=n,this.scriptMap=r??new Map,this.additionalArgs=a??[],await this.loadScripts(n,r),await this.additionalInitialization(...a??[]),this}async loadScripts(e,t){if((0,l.notMissing)(t))for(let[e,n]of t)this.scriptMap.set(e,n);for(let[t,n]of Object.entries(e)){let e=await this.cacheClient?.loadScript?.(n);(0,l.notMissing)(e)?this.scriptMap?.set(t,e):this.logger?.error({message:`Error loading script ${t}`,category:this.constructor.name,code:me.ScriptManagerLoadingError})}}async executeScript(e,t,n){try{if(!this.isRedisReady())throw Error(`Redis client is not ready`);let r=this.scriptMap?.get(e);if((0,l.isMissing)(r))throw Error(`Script for ${e} not found`);return await this.cacheClient?.executeScript?.({sha1:r,keys:t,args:n})??null}catch(r){throw r instanceof Error&&this.logger?.error({message:`Error executing script ${e}: ${r.message}`,category:this.constructor.name,context:{keys:t,args:n},error:r,code:me.ScriptManagerExecutionError}),r}}isRedisReady(){return this.isReady()&&(0,l.notMissing)(this.cacheClient)}},ge=class e{options={getCacheClient:ue};subscriptionMap=null;subscriptionClient=null;logger;constructor(e){this.options={...this.options,...e}}async initialize(t=this.options){let{config:n,getCacheClient:r,logger:i}=t;if(this.logger=i,this.subscriptionMap=new ie({instantiator:`${t?.instantiator}(${this.constructor.name})`,evictionFrequency:t?.subscriptionTTL,staleDataThreshold:t?.staleSubscriptionsThreshold,truncateThreshold:t?.truncateThreshold,truncationPercentage:t?.truncationPercentage,typeGuard:e=>typeof e==`string`,dispose:async e=>{await this.subscriptionClient?.unsubscribe?.(e)}}),(0,l.notMissing)(n))this.subscriptionClient=await r?.(n,this.logger)??null,this.logger?.info({message:`${e.name} initialized for ${t.instantiator}.`,category:e.name});else throw Error(`SubscriptionManager requires a Redis client configuration.`)}async subscribe(e,t){this.isReady()||await this.initialize(this.options);let n=await this.subscriptionMap?.getData(e),r=(0,l.getContentHash)(t.toString());return await this.subscriptionMap?.setData({key:e,value:r}),n===r?!0:await this.subscriptionClient?.subscribe?.(e,t)??!1}async unsubscribe(e){return this.isReady()||await this.initialize(this.options),this.subscriptionClient?.unsubscribe?.(e)??!1}isReady(){return(0,l.notMissing)(this.subscriptionClient)&&(0,l.notMissing)(this.subscriptionMap)}close(){this.subscriptionMap?.close(),this.subscriptionMap=null,this.subscriptionClient=null}};const _e=6e4,ve=15e3,ye=!1,be=6e4,xe=15e3,Se=1e4,Ce=1;let we=function(e){return e.QueueManagerInitializationError=`QueueManagerInitializationError`,e.QueueManagerJoinAndWaitTurnError=`QueueManagerJoinAndWaitTurnError`,e.QueueManagerLengthError=`QueueManagerLengthError`,e.QueueManagerPopError=`QueueManagerPopError`,e}({}),A=function(e){return e.rPush=`rPush`,e.lPush=`lPush`,e.lPop=`lPop`,e.llen=`llen`,e}({});var j=class extends he{subscriptionManager=null;eventClient=null;name=`QueueManager`;async additionalInitialization(){if((0,l.isMissing)(this.redisClientConfig))throw Error(`Redis client configuration is required to initialize QueueManager.`);this.subscriptionManager=new ge({config:this.redisClientConfig,instantiator:this.name,subscriptionTTL:be,truncateThreshold:Se,truncationPercentage:Ce}),await this.subscriptionManager?.initialize(),this.eventClient=new ce({instantiator:this.name,timeoutResolveValue:ye},{instantiator:this.name,evictionFrequency:xe,truncateThreshold:Se,truncationPercentage:Ce},{instantiator:this.name,evictionFrequency:xe,truncateThreshold:Se,truncationPercentage:Ce}),this.logger?.info({category:this.name,message:`QueueManager initialized.`})}async joinAndWaitTurn(e,t,n,r,i=!0){try{let a=`${e}:${t}`;await this.subscriptionManager?.subscribe(a,async(n,r)=>{if(r.includes(a)&&r.includes(t)){let t=`${e}:${n}`;return this.eventClient?.resolveEvent(t,!0)}});let o=await this.executeScript(n?A.lPush:A.rPush,[e],[t,_e.toString()]),s=o===1,c=i?!s:!0;return c&&await this.eventClient?.waitForEvent(a,ve),await this.eventClient?.deleteEvent(a),await r(e,t)}catch(n){throw n instanceof Error&&this.logger?.error({message:`Error in joinAndWaitTurn`,category:this.name,error:n,code:we.QueueManagerJoinAndWaitTurnError,context:{queueName:e,value:t}}),n}}async pop(e){try{return await this.executeScript(A.lPop,[e],[])}catch(t){throw t instanceof Error&&this.logger?.error({message:`Error in pop`,category:this.name,error:t,code:we.QueueManagerPopError,context:{queueName:e}}),t}}async length(e){try{return await this.executeScript(A.llen,[e],[])}catch(t){throw t instanceof Error&&this.logger?.error({message:`Error in length`,category:this.name,error:t,code:we.QueueManagerLengthError,context:{queueName:e}}),t}}isReady(){return(0,l.notMissing)(this.subscriptionManager)&&(0,l.notMissing)(this.cacheClient)&&(0,l.notMissing)(this.scriptMap)&&this.scriptMap.size>0}close(){this.subscriptionManager?.close(),this.scriptMap?.clear()}};const Te=5,Ee=12e4,De=1e4,Oe=1,ke=60,Ae=90,je=`__keyevent@0__:`,Me=`${je}set`,Ne=`${je}expire`,Pe=`${je}expired`,Fe=`${je}srem`,Ie=`config_check_test_key`,Le=[Me,Ne,Pe],Re=1,ze=2500,Be={mainMaxConcurrency:30};let Ve=function(e){return e.ConcurrencyManagerInitializationError=`ConcurrencyManagerInitializationError`,e.ConcurrencyManagerRedisEventsEmitError=`ConcurrencyManagerRedisEventsEmitError`,e.ConcurrencyManagerRegistrationFailed=`ConcurrencyManagerRegistrationFailed`,e}({}),He=function(e){return e.tryConcurrency=`tryConcurrency`,e.removeFromSet=`removeFromSet`,e.publishNextItem=`publishNextItem`,e}({});const Ue={[He.tryConcurrency]:`
|
|
1
|
+
var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports),s=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},c=(n,r,a)=>(a=n==null?{}:e(i(n)),s(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));const l=c(require(`@stackone/utils`)),u=c(require(`redis`)),d=c(require(`axios`)),f=c(require(`node:https`)),p=c(require(`crypto`)),m=c(require(`node:timers`)),h=c(require(`jsonpath`)),g=c(require(`@stackone/redaction`)),_=c(require(`qs`)),v=c(require(`@stackone/expressions`)),y=e=>{switch(e.type){case`none`:return{};case`basic`:return b(e);case`bearer`:return x(e);case`oauth2`:return x(e);default:throw Error(`Invalid authentication type`)}},b=({username:e=``,password:t=``,encoding:n=`base64`})=>{let r=n,i=Buffer.from(`${e}:${t}`).toString(r);return{authorization:`Basic ${i}`}},x=({token:e,includeBearer:t})=>({authorization:`${t?`Bearer `:``}${e}`});var S=class{locks;constructor(){this.locks=new Map}async withLock(e,t){await this.lock(e);try{return t()}finally{this.unlock(e)}}async lock(e){let t,n=new Promise(e=>t=e),r=this.locks.has(e),i=this.locks.get(e);if(r&&i){i.push({lock:n,unlock:t});let e=i.length-2;await i[e].lock}else this.locks.set(e,[{lock:n,unlock:t}])}unlock(e){let t=this.locks.has(e),n=this.locks.get(e);if(t&&n&&n.length>0){let e=n.shift()?.unlock;e?.()}this.queueLength(e)===0&&this.locks.delete(e)}queueLength(e){return this.locks.get(e)?.length}close(){this.locks.clear()}};const ee=1e3,C=100,te=60,w=6e4,T=te,ne=w,E=w*10,re=C,D=10;let O=function(e){return e.MemoryStorePruneError=`MemoryStorePruneError`,e}({});var ie=class{config;instantiator;dataStore;lockManager;expiryMap;evictionFrequency;staleDataThreshold;truncateThreshold;truncationPercentage;logger;typeGuard;dispose;evictionInterval;lastAccessedAt=Date.now();constructor(e={}){this.config=e,this.initialize(e)}initialize(e=this.config){this.instantiator=e?.instantiator??`Unknown`,this.logger=e?.logger,this.dataStore=e?.dataStore??new Map,this.lockManager=e?.lockManager??new S,this.expiryMap=e?.expiryMap??new Map,this.evictionFrequency=e?.evictionFrequency??ne,this.staleDataThreshold=e?.staleDataThreshold??E,this.truncateThreshold=e?.truncateThreshold??re,this.truncationPercentage=e?.truncationPercentage??D,this.typeGuard=e?.typeGuard,this.dispose=e?.dispose,this.startEvictionTask()}async getData(e){return this.isReady()||this.initialize(),this.updateLastAccessedAt(),this.lockManager.withLock(e,async()=>this.dataStore.get(e)??null)}async setData({key:e,value:t,cacheTTL:n=T}){if(this.isReady()||this.initialize(),this.updateLastAccessedAt(),(0,l.notMissing)(this.typeGuard)&&!this.typeGuard(t))return!1;let r=n*ee,i=Date.now()+r;return await this.lockManager.withLock(e,async()=>{((0,l.notMissing)(this.typeGuard)&&this.typeGuard(t)||(0,l.isMissing)(this.typeGuard)&&this.typeGuardBypass(t))&&this.dataStore.set(e,t),this.expiryMap.set(e,i)}),!0}typeGuardBypass(e){return this.logger?.debug({category:`MemoryStore`,message:`${this.instantiator} MemoryStore setting data without type guard - you should probably configure one`}),!0}async delete(e){return this.isReady()||this.initialize(),this.updateLastAccessedAt(),this.lockManager.withLock(e,async()=>{if(this.dispose){let t=this.dataStore.get(e);await this.dispose(e,t)}return this.dataStore.delete(e)})}async pruneExpiredKeys(){let e=this.dataStore.size,t=e>=this.truncateThreshold;if(e<=0)return;let n=[],r=0;this.dataStore.forEach(async(e,i)=>{let a=Date.now(),o=this.expiryMap.get(i)??0,s=this.truncateThreshold*this.truncationPercentage/C;(o<=a||t&&r>=0&&r<=s)&&n.push(this.lockManager.withLock(i,async()=>(this.dispose&&await this.dispose(i,e),this.dataStore.delete(i)))),r++}),await Promise.all(n);let i=this.dataStore.size;return{dataStoreSize:e,prunedDataStoreSize:i}}startEvictionTask(){if((0,l.notMissing)(this.evictionInterval))return;let e=async()=>{let t;try{let n=this.lastAccessedAt+this.staleDataThreshold,r=Date.now();if(n<r){this.logger?.warning({message:`Closing the ${this.instantiator}'s MemoryStore instance - received no requests for a while.`,category:`MemoryStore`}),this.close();return}t=await this.pruneExpiredKeys(),this.evictionInterval=setTimeout(e,this.evictionFrequency)}catch(t){t instanceof Error&&this.logger?.error({message:`Error during pruning expired keys:`,category:`MemoryStore`,error:t,code:O.MemoryStorePruneError}),this.evictionInterval=setTimeout(e,this.evictionFrequency)}finally{if((0,l.notMissing)(t?.dataStoreSize)&&(0,l.notMissing)(t?.prunedDataStoreSize)){let{dataStoreSize:e,prunedDataStoreSize:n}=t,r=e-n;this.logger?.debug({message:`Pruned ${r} expired keys, ${n} remain, scheduling next prune.`,category:`MemoryStore`,context:{instantiator:this.instantiator}})}}};this.evictionInterval=setTimeout(e,this.evictionFrequency)}stopEvictionTask(){this.evictionInterval&&(clearTimeout(this.evictionInterval),this.evictionInterval=void 0)}updateLastAccessedAt(){this.lastAccessedAt=Date.now()}isReady(){return(0,l.notMissing)(this.evictionInterval)&&(0,l.notMissing)(this.dataStore)&&(0,l.notMissing)(this.expiryMap)&&(0,l.notMissing)(this.lockManager)}close(){this.stopEvictionTask(),this.dataStore.clear(),this.expiryMap.clear(),this.lockManager.close()}async listData({partialKey:e,cursor:t,limit:n}){let r=Array.from(this.dataStore.keys()),i=r.filter(t=>t.includes(e)),a=[],o=t?parseInt(t,10):0;for(let e=o;e<n+o;e++){let t=i[e];if(!t)break;let r=await this.getData(t);if(r&&a.push(r),a.length>=n)break}return{items:a,cursor:a.length<n?void 0:(o+n).toString()}}};const ae=1e3,oe=6e4;let se=function(e){return e.EventClientResolveError=`EventClientResolveError`,e}({});var ce=class{executorMethodStore=null;promiseStore=null;logger;eventClientConfig=null;executorMethodStoreConfig=null;pendingPromiseStoreConfig=null;constructor(e,t,n){this.eventClientConfig=e,this.executorMethodStoreConfig=t,this.pendingPromiseStoreConfig=n;let r=e?.instantiator?`${e?.instantiator}(${this.constructor.name})`:this.constructor.name,i={logger:this.logger,dispose:this.defaultExecutorMethodDispose,typeGuard:this.defaultExecutorMethodTypeGuard,...this.executorMethodStoreConfig??{},instantiator:r},a={logger:this.logger,dispose:this.defaultPendingPromiseDispose,typeGuard:this.defaultPendingPromiseTypeGuard,...this.pendingPromiseStoreConfig??{},instantiator:r};this.executorMethodStore=new ie(i),this.promiseStore=new ie(a)}async setPendingEvent(e,t){let n={resolve:void 0,reject:void 0},r=new Promise((e,t)=>{n.resolve=e,n.reject=t}),i=t/ae;await this.executorMethodStore?.setData({key:e,value:n,cacheTTL:i}),await this.promiseStore?.setData({key:e,value:r,cacheTTL:i})}async waitForEvent(e,t){let n=await this.promiseStore?.getData(e);return(0,l.notMissing)(n)?n:(await this.setPendingEvent(e,t),this.getPendingEvent(e))}async getPendingEvent(e){return this.promiseStore?.getData(e)??null}async deleteEvent(e){return(await Promise.all([this.promiseStore?.delete(e),this.executorMethodStore?.delete(e)])).every(Boolean)}async resolveEvent(e,t){try{let n=await this.executorMethodStore?.getData(e);(0,l.isMissing)(n)&&(await this.setPendingEvent(e,this.eventClientConfig?.defaultTimeoutMS??oe),n=await this.executorMethodStore?.getData(e)),n?.resolve?.(t)}catch(t){t instanceof Error&&this.logger.error({message:`Error handling event for key ${e}: ${t.message}`,category:this.constructor.name,context:{eventKey:e},error:t,code:se.EventClientResolveError})}}async defaultExecutorMethodDispose(e,t){(0,l.notMissing)(t?.resolve)?t.resolve(this.eventClientConfig?.timeoutResolveValue):(0,l.notMissing)(t?.reject)&&t.reject(Error(`Event key: ${e} was not resolved or the event was disposed`))}defaultExecutorMethodTypeGuard(e){return(0,l.isObject)(e)&&e.hasOwnProperty(`resolve`)&&e.hasOwnProperty(`reject`)}async defaultPendingPromiseDispose(e,t){t instanceof Promise&&t?.finally(()=>{})}defaultPendingPromiseTypeGuard(e){return e instanceof Promise}},le=class{instance=null;initPromise=null;retryTimeout=null;status=`uninitialized`;initOptions=null;initArgs=[];getRetryDelay(){return 5e3}getSingleton(e){throw Error(`getSingleton method not implemented`)}async getInstance(e,...t){return this.instance?this.instance:((0,l.isMissing)(this.initPromise)&&(this.initOptions=e??null,this.initArgs=t,this.initPromise=this.init(e??null,...t)),this.initPromise)}getInstanceIfReady(){return this.instance}isReady(){return this.status===`ready`}hasInitFailed(){return this.status===`failed`}async init(e,...t){try{let n=await this.initInstance(e,...t);return this.instance=n,this.status=`ready`,n}catch(e){throw this.status=`failed`,this.initPromise=null,(0,l.isMissing)(this.retryTimeout)&&(this.retryTimeout=setTimeout(()=>{this.retryTimeout=null,this.getInstance(this.initOptions,...this.initArgs).catch(()=>{})},this.getRetryDelay())),e}}reset(){this.instance=null,this.initPromise=null,this.initOptions=null,this.status=`uninitialized`,this.retryTimeout&&clearTimeout(this.retryTimeout),this.retryTimeout=null}};let k=function(e){return e.RedisClientError=`RedisClientError`,e.RedisClientCommandError=`RedisClientCommandError`,e.RedisClientDeleteError=`RedisClientDeleteError`,e.RedisClientInvalidTTL=`RedisClientInvalidTTL`,e.RedisClientListError=`RedisClientListError`,e.RedisClientNotInitialized=`RedisClientNotInitialized`,e.RedisClientPublishError=`RedisClientPublishError`,e.RedisClientReadError=`RedisClientReadError`,e.RedisClientRedisNotReady=`RedisClientRedisNotReady`,e.RedisClientScriptError=`RedisClientScriptError`,e.RedisClientScriptExecuteError=`RedisClientScriptExecuteError`,e.RedisClientScriptLoadError=`RedisClientScriptLoadError`,e.RedisClientSubscribeError=`RedisClientSubscribeError`,e.RedisClientUnsubscribeError=`RedisClientUnsubscribeError`,e.RedisClientWriteError=`RedisClientWriteError`,e}({});const ue=async(e,t)=>{try{let n=await de.build(e,t);return n??void 0}catch(e){let n=e;t?.error({message:`Error building Cache Manager`,error:n,code:k.RedisClientError,category:`buildRedisClientInstance`});return}};var de=class e{#redisClient;#logger;static async build({getRedisClient:t=u.createClient,host:n,port:r,tls:i,reconnect:a=!0,database:o},s){let c=new e;c.#logger=s;try{c.#redisClient=t({socket:{reconnectStrategy:a?e=>Math.min(e*20,5e3):!1,host:n,port:r,tls:i},database:o,disableOfflineQueue:!0});let s=pe(e.name,c.#logger);return fe(c.#redisClient,s),await c.#redisClient.connect(),c}catch(t){let n=t;return c.#logger?.error({message:`Error building Cache Manager.`,error:n,code:k.RedisClientError,category:e.name}),null}}#parseData(e){try{return JSON.parse(e)}catch{return e}}async getData(t){if(!this.#isRedisClientAvailable())return null;try{let e=await this.#redisClient.get(t);if(!e)return null;let n=this.#parseData(e);return(0,l.isString)(n)?e:n}catch(t){let n=t;return this.#logger?.error({message:`Error getting data`,error:n,code:k.RedisClientReadError,category:e.name}),null}}async setData({key:t,value:n,cacheTTL:r,groupKey:i}){if(!this.#isRedisClientAvailable())return!1;try{let e=(0,l.isString)(n)?n:JSON.stringify(n);return await this.#redisClient.set(t,e,{EX:r}),(0,l.notMissing)(i)&&await this.#redisClient.sAdd(i,[t]),!0}catch(t){let n=t;return this.#logger?.error({message:`Error setting data`,error:n,code:k.RedisClientWriteError,category:e.name}),!1}}async executeScript({sha1:t,keys:n,args:r}){if(!this.#isRedisClientAvailable())return null;try{return await this.#redisClient.evalSha(t,{keys:n,arguments:r})}catch(t){let n=t;return this.#logger?.error({message:`Error executing script`,error:n,code:k.RedisClientScriptExecuteError,category:e.name}),null}}async loadScript(t){if(!this.#isRedisClientAvailable())return null;try{return await this.#redisClient.scriptLoad(t)}catch(t){let n=t;return this.#logger?.error({message:`Error loading script`,error:n,code:k.RedisClientScriptLoadError,category:e.name}),null}}async#executeCounterCommand(t,n,r){if(!this.#isRedisClientAvailable())return null;if(!Number.isFinite(r)||r<=0)return this.#logger?.error({message:`Invalid cacheTTL parameter`,category:e.name,code:k.RedisClientInvalidTTL}),null;try{let e=[{args:[t,n]},{args:[`expire`,n,r.toString()]}],i=await this.#redisClient.multiExecutor(e),a=i?.[0];return typeof a==`number`?a:null}catch(n){let r=n;return this.#logger?.error({message:`Error executing ${t} operation`,error:r,code:k.RedisClientCommandError,category:e.name}),null}}async increment(e,t){return this.#executeCounterCommand(`incr`,e,t)}async decrement(e,t){return this.#executeCounterCommand(`decr`,e,t)}async subscribe(t,n){if(!this.#isRedisClientAvailable())return!1;try{return await this.#redisClient.pSubscribe(t,n),!0}catch(n){let r=n;return this.#logger?.error({message:`Error subscribing to ${t}`,error:r,code:k.RedisClientSubscribeError,category:e.name}),!1}}async unsubscribe(t){if(!this.#isRedisClientAvailable())return!1;try{return await this.#redisClient.pUnsubscribe(t),!0}catch(n){let r=n;return this.#logger?.error({message:`Error unsubscribing from ${t}`,error:r,code:k.RedisClientUnsubscribeError,category:e.name}),!1}}async publish(t,n){if(!this.#isRedisClientAvailable())return null;try{let r=await this.#redisClient.publish(t,n);return r===0&&this.#logger?.debug({message:`No subscribers found for channel ${t}`,category:e.name}),r}catch(n){let r=n;return this.#logger?.error({message:`Error publishing to ${t}`,error:r,code:k.RedisClientPublishError,category:e.name}),null}}getClient(){return this.#isRedisClientAvailable()?this.#redisClient:null}#isRedisClientAvailable(){let t=this.#redisClient.isReady&&this.#redisClient.isOpen;return t||this.#logger?.error({message:`Redis Client is not ready.`,category:e.name,code:k.RedisClientRedisNotReady}),t}async listData({partialKey:t,limit:n,cursor:r}){try{let e=r?parseInt(r,10):0,i=await this.#redisClient.sScan(t,e,{COUNT:n}),a=[];for(let e of i.members){let t=await this.getData(e);(0,l.notMissing)(t)&&a.push(t)}return{items:a,cursor:i.cursor>0?i.cursor.toString():void 0}}catch(t){let n=t;return this.#logger?.error({message:`Error listing data`,error:n,code:k.RedisClientListError,category:e.name}),{items:null}}}async deleteData(t){try{if(!this.#isRedisClientAvailable())return!1;let n=await this.#redisClient.del(t);return n===0&&this.#logger?.debug({message:`No keys deleted for ${t}`,category:e.name}),n>0}catch(t){let n=t;return this.#logger?.error({message:`Error deleting data`,error:n,code:k.RedisClientDeleteError,category:e.name}),!1}}};const fe=(e,t)=>{t.forEach(({eventName:t,listener:n})=>{e.on(t,n)})},pe=(e=`RedisClient`,t)=>[{eventName:`error`,listener:n=>{t?.error({message:`Redis client error ${n.message}`,error:n,code:k.RedisClientError,category:e})}},{eventName:`ready`,listener:()=>{t?.info({message:`Redis client ready`,category:e})}},{eventName:`connect`,listener:()=>{t?.info({message:`Redis client connected`,category:e})}},{eventName:`reconnecting`,listener:()=>{t?.info({message:`Redis client reconnecting`,category:e})}},{eventName:`end`,listener:()=>{t?.info({message:`Redis client disconnected`,category:e})}}];let me=function(e){return e.ScriptManagerExecutionError=`ScriptManagerExecutionError`,e.ScriptManagerLoadingError=`ScriptManagerLoadingError`,e}({});var he=class extends le{redisClientConfig;scripts;scriptMap;cacheClient;logger;additionalArgs;isInitialConnection=!0;async initInstance({redisClientConfig:e=this.redisClientConfig,cacheClient:t=this.cacheClient,scripts:n=this.scripts,scriptMap:r=this.scriptMap,logger:i=this.logger,additionalArgs:a=this.additionalArgs}){let o=t??await ue(e,i);if((0,l.isMissing)(o))throw Error(`Failed to build Redis client`);return this.redisClientConfig=e,this.logger=i,this.cacheClient=o,this.scripts=n,this.scriptMap=r??new Map,this.additionalArgs=a??[],await this.loadScripts(n,r),this.setupReconnectHandler(),await this.additionalInitialization(...a??[]),this}setupReconnectHandler(){let e=this.cacheClient?.getClient?.();(0,l.notMissing)(e)&&(e.on(`ready`,async()=>{this.isInitialConnection?this.isInitialConnection=!1:(this.logger?.info({message:`Redis reconnected - reloading scripts`,category:this.constructor.name}),await this.reloadScripts())}),e.on(`end`,()=>{this.logger?.info({message:`Redis connection closed`,category:this.constructor.name})}))}async reloadScripts(){(0,l.isMissing)(this.scripts)||Object.keys(this.scripts).length===0||(this.logger?.info({message:`Reloading ${Object.keys(this.scripts).length} scripts after Redis reconnection`,category:this.constructor.name}),this.scriptMap.clear(),await this.loadScripts(this.scripts))}async loadScripts(e,t){if((0,l.notMissing)(t))for(let[e,n]of t)this.scriptMap.set(e,n);for(let[t,n]of Object.entries(e)){let e=await this.cacheClient?.loadScript?.(n);(0,l.notMissing)(e)?this.scriptMap?.set(t,e):this.logger?.error({message:`Error loading script ${t}`,category:this.constructor.name,code:me.ScriptManagerLoadingError})}}async executeScript(e,t,n){try{if(!this.isRedisReady())throw Error(`Redis client is not ready`);let r=this.scriptMap?.get(e);if((0,l.isMissing)(r))throw Error(`Script for ${e} not found`);try{return await this.cacheClient?.executeScript?.({sha1:r,keys:t,args:n})??null}catch(i){if(i instanceof Error&&i.message.includes(`NOSCRIPT`)){if(this.logger?.info({message:`Script ${e} not found in Redis, reloading scripts`,category:this.constructor.name}),await this.reloadScripts(),r=this.scriptMap?.get(e),(0,l.isMissing)(r))throw Error(`Script for ${e} could not be reloaded`);return await this.cacheClient?.executeScript?.({sha1:r,keys:t,args:n})??null}throw i}}catch(r){throw r instanceof Error&&this.logger?.error({message:`Error executing script ${e}: ${r.message}`,category:this.constructor.name,context:{keys:t,args:n},error:r,code:me.ScriptManagerExecutionError}),r}}isRedisReady(){return this.isReady()&&(0,l.notMissing)(this.cacheClient)}},ge=class{options={getCacheClient:ue};subscriptionMap=null;subscriptionClient=null;logger;name=`SubscriptionManager`;constructor(e){this.options={...this.options,...e}}async initialize(e=this.options){let{config:t,getCacheClient:n,logger:r}=e;if(this.logger=r,this.subscriptionMap=new ie({instantiator:`${e?.instantiator}(${this.name})`,evictionFrequency:e?.subscriptionTTL,staleDataThreshold:e?.staleSubscriptionsThreshold,truncateThreshold:e?.truncateThreshold,truncationPercentage:e?.truncationPercentage,typeGuard:e=>typeof e==`string`,dispose:async e=>{await this.subscriptionClient?.unsubscribe?.(e)}}),(0,l.notMissing)(t))this.subscriptionClient=await n?.(t,this.logger)??null,this.logger?.info({message:`${this.name} initialized for ${e.instantiator}.`,category:this.name});else throw Error(`SubscriptionManager requires a Redis client configuration.`)}async subscribe(e,t){this.isReady()||await this.initialize(this.options);let n=await this.subscriptionMap?.getData(e),r=(0,l.getContentHash)(t.toString());return await this.subscriptionMap?.setData({key:e,value:r}),n===r?!0:await this.subscriptionClient?.subscribe?.(e,t)??!1}async unsubscribe(e){return this.isReady()||await this.initialize(this.options),this.subscriptionClient?.unsubscribe?.(e)??!1}isReady(){return(0,l.notMissing)(this.subscriptionClient)&&(0,l.notMissing)(this.subscriptionMap)}close(){this.subscriptionMap?.close(),this.subscriptionMap=null,this.subscriptionClient=null}};const _e=6e4,ve=15e3,ye=!1,be=6e4,xe=15e3,Se=1e4,Ce=1;let we=function(e){return e.QueueManagerInitializationError=`QueueManagerInitializationError`,e.QueueManagerJoinAndWaitTurnError=`QueueManagerJoinAndWaitTurnError`,e.QueueManagerLengthError=`QueueManagerLengthError`,e.QueueManagerPopError=`QueueManagerPopError`,e}({}),A=function(e){return e.rPush=`rPush`,e.lPush=`lPush`,e.lPop=`lPop`,e.llen=`llen`,e}({});var j=class extends he{subscriptionManager=null;eventClient=null;name=`QueueManager`;async additionalInitialization(){if((0,l.isMissing)(this.redisClientConfig))throw Error(`Redis client configuration is required to initialize QueueManager.`);this.subscriptionManager=new ge({config:this.redisClientConfig,instantiator:this.name,subscriptionTTL:be,truncateThreshold:Se,truncationPercentage:Ce}),await this.subscriptionManager?.initialize(),this.eventClient=new ce({instantiator:this.name,timeoutResolveValue:ye},{instantiator:this.name,evictionFrequency:xe,truncateThreshold:Se,truncationPercentage:Ce},{instantiator:this.name,evictionFrequency:xe,truncateThreshold:Se,truncationPercentage:Ce}),this.logger?.info({category:this.name,message:`QueueManager initialized.`})}async joinAndWaitTurn(e,t,n,r,i=!0){try{let a=`${e}:${t}`;await this.subscriptionManager?.subscribe(a,async(n,r)=>{if(r.includes(a)&&r.includes(t)){let t=`${e}:${n}`;return this.eventClient?.resolveEvent(t,!0)}});let o=await this.executeScript(n?A.lPush:A.rPush,[e],[t,_e.toString()]),s=o===1,c=i?!s:!0;return c&&await this.eventClient?.waitForEvent(a,ve),await this.eventClient?.deleteEvent(a),await r(e,t)}catch(n){throw n instanceof Error&&this.logger?.error({message:`Error in joinAndWaitTurn`,category:this.name,error:n,code:we.QueueManagerJoinAndWaitTurnError,context:{queueName:e,value:t}}),n}}async pop(e){try{return await this.executeScript(A.lPop,[e],[])}catch(t){throw t instanceof Error&&this.logger?.error({message:`Error in pop`,category:this.name,error:t,code:we.QueueManagerPopError,context:{queueName:e}}),t}}async length(e){try{return await this.executeScript(A.llen,[e],[])}catch(t){throw t instanceof Error&&this.logger?.error({message:`Error in length`,category:this.name,error:t,code:we.QueueManagerLengthError,context:{queueName:e}}),t}}isReady(){return(0,l.notMissing)(this.subscriptionManager)&&(0,l.notMissing)(this.cacheClient)&&(0,l.notMissing)(this.scriptMap)&&this.scriptMap.size>0}close(){this.subscriptionManager?.close(),this.scriptMap?.clear()}};const Te=5,Ee=12e4,De=1e4,Oe=1,ke=60,Ae=90,je=`__keyevent@0__:`,Me=`${je}set`,Ne=`${je}expire`,Pe=`${je}expired`,Fe=`${je}srem`,Ie=`config_check_test_key`,Le=[Me,Ne,Pe],Re=1,ze=1e4,Be={mainMaxConcurrency:30};let Ve=function(e){return e.ConcurrencyManagerInitializationError=`ConcurrencyManagerInitializationError`,e.ConcurrencyManagerRedisEventsEmitError=`ConcurrencyManagerRedisEventsEmitError`,e.ConcurrencyManagerRegistrationFailed=`ConcurrencyManagerRegistrationFailed`,e}({}),He=function(e){return e.tryConcurrency=`tryConcurrency`,e.removeFromSet=`removeFromSet`,e.publishNextItem=`publishNextItem`,e}({});const Ue={[He.tryConcurrency]:`
|
|
2
2
|
local requestId = KEYS[1]
|
|
3
3
|
local targetKey = KEYS[2]
|
|
4
4
|
local queueKey = KEYS[3]
|
|
@@ -98,7 +98,7 @@ var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=
|
|
|
98
98
|
|
|
99
99
|
redis.call('publish', nextItemKey, nextItem)
|
|
100
100
|
end
|
|
101
|
-
`};var We=class e extends he{hasValidRedisConfig=!1;subscriptionManager=null;queueManager=null;generateUUID;name=`ConcurrencyManager`;async additionalInitialization(e){if(this.generateUUID=e??l.generateRequestId,this.subscriptionManager=new ge({config:this.redisClientConfig,logger:this.logger,instantiator:this.name,subscriptionTTL:Ee,truncateThreshold:De,truncationPercentage:Oe}),await this.subscriptionManager?.initialize(),this.hasValidRedisConfig=await this.checkRedisEventsEmit(),!this.hasValidRedisConfig)throw Error(`Failed to validate Redis configuration for ConcurrencyManager`);if((0,l.isMissing)(this.getSingleton))throw Error(`getSingleton method not available`);this.queueManager=this.getSingleton?.(j)}async addTestSubscription(t,n,r,i){return(0,l.isMissing)(this.subscriptionManager)?!1:this.subscriptionManager.subscribe(t,async(a,o)=>{o===t&&a.includes(n)&&(r.push(o),r.includes(Me)&&r.includes(Ne)&&r.includes(Pe)&&(i(),this.logger?.info({category:e.name,message:`Redis events are being emitted.`,context:{receivedEventCount:r.length}}),r.length=0))})}async checkRedisEventsEmit(){let t=[],n=async(e,n)=>{let r=()=>e(!0),i=this.generateUUID(),a=`${Ie}:${i}`,o=(await Promise.all(Le.map(async e=>this.addTestSubscription(e,a,t,r)))).every(Boolean);if(!o)return n(Error(`Failed to subscribe to event channels.`));await this.cacheClient?.setData({key:a,value:`test`,cacheTTL:Re}),setTimeout(()=>{n(Error(`Timeout waiting for Redis events to emit.`))},ze)};try{return await new Promise(n)}catch(n){return this.logger?.error({category:e.name,message:`Redis events failed to emit.`,context:{receivedEventCount:t.length,reason:n?.message},error:n,code:Ve.ConcurrencyManagerRedisEventsEmitError}),!1}}isRedisConfigured(){return this.hasValidRedisConfig}async subscribeToLeaseExpiry(e,t){let n=await this.subscriptionManager?.subscribe(Pe,async(n,r)=>{r===Pe&&n.includes(t)&&n.includes(e)&&await this.releaseRequest(e,t)});return n??!1}async subscribeToSetRemoval(e,t){let n=await this.subscriptionManager?.subscribe(Fe,async(n,r)=>{r===Fe&&n.includes(e)&&await this.executeScript(He.publishNextItem,[t],[])});return n??!1}async registerRequest(t,n,r,i){let a=`concurrency:${t}`,o=`queue:${t}`,s=n.subPools?.find(e=>e.urlPattern instanceof RegExp?e.urlPattern.test(i??``):i?.includes(e.urlPattern))?.subPoolKey,c=(0,l.notMissing)(s)?`${a}-${s}`:a,{mainMaxConcurrency:u,subPools:d}=n,f=[{key:a,maxConcurrency:u},...d?.map(e=>{let{subPoolKey:t,maxConcurrency:n}=e;return{key:`${a}-${t}`,maxConcurrency:n}})??[]],p=f.map(e=>e.key),m=f.map(e=>e.maxConcurrency),[h,g]=await Promise.all([this.subscribeToLeaseExpiry(r,c),this.subscribeToSetRemoval(c,o)]),_={requestId:r,targetConcurrencyKey:c,leaseSubscription:h,setRemovalSubscription:g,queueKey:o},[v,y]=await this.executeScript(He.tryConcurrency,[r,c,o,...p],[ke.toString(),u.toString(),Ae.toString(),...m.map(String)])??[];if(v)return{..._,joinCondition:`optimistic`,failureReason:y};let b=0,x=[];for(;b<Te;){let t=b>1,[n,i]=await this.queueManager?.joinAndWaitTurn(o,r,t,async(e,t)=>await this.executeScript(He.tryConcurrency,[r,c,o,...p],[ke.toString(),u.toString(),Ae.toString(),...m.map(String)]),!1)??[!1,``];if((0,l.notMissing)(i)&&i!==``&&x.push(i),b++,n)return b>1&&this.logger?.warning({category:e.name,message:`Register request succeeded after ${b} attempts.`,context:{..._,registerAttempts:b,joinCondition:`attempts`,failureMessages:x}}),{..._,joinCondition:`attempts`};let a=(0,l.exponentialBackoffInMS)(b,100);await(0,l.delay)(a)}return this.logger?.error({category:e.name,message:`Failed to register request after ${b} attempts.`,context:{..._,registerAttempts:b,joinCondition:`failed`,failureMessages:x},code:Ve.ConcurrencyManagerRegistrationFailed}),{..._,joinCondition:`failed`}}async releaseRequest(e,t){return(0,l.isString)(t)&&(0,l.isString)(e)?!!await this.executeScript(He.removeFromSet,[t],[e]):!1}isReady(){return this.subscriptionManager!==null&&this.cacheClient!==null&&this.scriptMap!==null&&this.scriptMap.size>0&&this.hasValidRedisConfig}close(){this.subscriptionManager?.close(),this.queueManager?.close(),this.scriptMap?.clear(),this.hasValidRedisConfig=!1}};const Ge=(0,l.zStrictObject)({receivedStatus:l.z.number(),targetStatus:l.z.number(),message:l.z.string().optional(),condition:l.z.string().optional()}),Ke={100:`Continue`,101:`Switching Protocols`,102:`Processing`,200:`OK`,201:`Created`,202:`Accepted`,203:`Non-Authoritative Information`,204:`No Content`,205:`Reset Content`,206:`Partial Content`,207:`Multi-Status`,208:`Already Reported`,226:`IM Used`,300:`Multiple Choices`,301:`Moved Permanently`,302:`Found`,303:`See Other`,304:`Not Modified`,305:`Use Proxy`,307:`Temporary Redirect`,308:`Permanent Redirect`,400:`Bad request`,401:`Unauthorized`,402:`Payment Required`,403:`Forbidden`,404:`Not Found`,405:`Method Not Allowed`,406:`Not Acceptable`,407:`Proxy Authentication Required`,408:`Request Timeout`,409:`Conflict`,410:`Gone`,411:`Length Required`,412:`Precondition Failed`,413:`Payload Too Large`,414:`URI Too Long`,415:`Unsupported Media Type`,416:`Range Not Satisfiable`,417:`Expectation Failed`,418:`I'm a teapot`,421:`Misdirected Request`,422:`Unprocessable Entity`,423:`Locked`,424:`Failed Dependency`,425:`Too Early`,426:`Upgrade Required`,428:`Precondition Required`,429:`Too many Requests`,431:`Request Header Fields Too Large`,451:`Unavailable For Legal Reasons`,499:`Client Closed Request`,500:`Internal server error`,501:`Not Implemented`,502:`Bad Gateway`,503:`Unavailable`,504:`Gateway Timeout`,505:`HTTP Version Not Supported`,506:`Variant Also Negotiates`,507:`Insufficient Storage`,508:`Loop Detected`,510:`Not Extended`,511:`Network Authentication Required`};var qe=class e extends Error{response;constructor(t,n){super(n),this.name=`HttpResponseError`,this.response=t,this.message=n??`HTTP error: ${t.status}`,Error.captureStackTrace&&Error.captureStackTrace(this,e)}toString(){let e=this.message?`: ${this.message}`:``;return`${this.name} [${this.response.status}]${e}`}};const Je=e=>e instanceof Error&&e.name===`HttpResponseError`&&`response`in e&&typeof e.response==`object`&&e.response!==null,Ye=(e,t=new WeakSet)=>{if(typeof e!=`object`||!e)return typeof e==`bigint`?e.toString():e;if(t.has(e))return`[Circular Reference]`;if(e instanceof Date)return e.toISOString();if(Array.isArray(e)){t.add(e);let n=e.map(e=>Ye(e,t));return t.delete(e),n}let n=e,r={};t.add(n);for(let[e,i]of Object.entries(n))r[e]=Ye(i,t);return t.delete(n),r},Xe=e=>{if(Je(e)){let t=e.response;return{name:e.name,message:e.message,stack:e.stack,response:Ye(t)}}return e instanceof Error?{name:e.name,message:e.message,stack:e.stack}:{error:String(e)}};var Ze=class{static createInstance({interceptors:e,instanceConfig:t,httpsAgentConfig:n,logger:r,context:i,requestConfig:a,concurrencyManager:o,rateLimitManager:s}){let c=new f.default.Agent(n),l=d.default.create({...t,httpsAgent:c});if(e){let t={axiosInstance:l,logger:r,context:i,requestConfig:a,concurrencyManager:o,rateLimitManager:s};this.applyInterceptors(l,e,t)}return l}static applyInterceptors(e,t,n){let{requestConfigs:r=[],responseConfigs:i=[]}=t,a=[{type:`request`,configs:r},{type:`response`,configs:i}];a.forEach(t=>{let{configs:r,type:i}=t;r.forEach(t=>{let{onFulfilled:r,onRejected:a,options:o}=t;if(i===`request`){let t=[this.resolveInterceptorInstance(r,n),this.resolveInterceptorInstance(a,n),o];e.interceptors.request.use(...t)}if(i===`response`){let t=[this.resolveInterceptorInstance(r,n),this.resolveInterceptorInstance(a,n)];e.interceptors.response.use(...t)}})})}static resolveInterceptorInstance(e,t){return(0,l.notMissing)(e)&&(0,l.isFunction)(e)&&typeof e(t)==`function`?e(t):e??null}};const Qe=300;var $e=class e{static instance=null;static logger=null;dataStore=null;constructor(e){this.initialize(e)}static getInstance(t){return(0,l.isMissing)(e.instance)&&(e.instance=new e(t)),e.instance}initialize(t){this.dataStore=new ie({instantiator:e.name,logger:t,typeGuard:e=>(0,l.notMissing)(e)}),e.logger=t??null,e.logger?.info({category:e.name,message:`InstanceManager initialized.`})}async get(t){return this.ensureReady(),await e.getInstance().dataStore?.getData(t)??null}async set(t,n,r){return this.ensureReady(),await e.getInstance().dataStore?.setData({key:t,value:n,cacheTTL:(0,l.notMissing)(r)?r:Qe})??!1}ensureReady(){this.isReady()||this.initialize()}isReady(){return(0,l.notMissing)(this?.dataStore)}close(){e.instance=null,this.dataStore=null}};const et=$e.getInstance(),tt=()=>{let e=e=>{let{response:t}=e;return e?.code===`ERR_CANCELED`&&(e.response={...t,status:499,statusText:`Request aborted`}),Promise.reject(e)};return e},nt=7.5,rt={mainRatelimit:25};let it=function(e){return e.incr=`incr`,e}({}),at=function(e){return e.RateLimiterReleaseRequestFailed=`RateLimiterReleaseRequestFailed`,e.RateLimitOrConcurrencyManagerNotInitialized=`RateLimitOrConcurrencyManagerNotInitialized`,e.InterceptorContextNotPresent=`InterceptorContextNotPresent`,e.InterceptorMaxWaitTimeExceeded=`InterceptorMaxWaitTimeExceeded`,e}({});const ot=(e,t,n,r)=>{let i=Error(e);return i.config={...t,headers:{}},i.code=n,i.response=r,i.isAxiosError=!0,i},st=(e,t,n)=>{let r=t.signal;return r?r.aborted?Promise.reject(ot(`Request aborted`,t,`ERR_CANCELED`,n)):new Promise((i,a)=>{let o=!1,s,c=ot(`Request aborted`,t,`ERR_CANCELED`,n),l=()=>{if(!o){o=!0,s&&(0,m.clearTimeout)(s);try{r.removeEventListener?.(`abort`,u)}catch{}}},u=()=>{l(),a(c)},d=()=>{l(),i()};try{r.addEventListener?.(`abort`,u,{once:!0})}catch{s=setTimeout(()=>{i()},e);return}if(r.aborted){l(),a(c);return}s=setTimeout(d,e)}):new Promise(t=>setTimeout(t,e))},ct=`checkConcurrencyInterceptor`,lt=({logger:e,context:t,requestConfig:n,concurrencyManager:r,rateLimitManager:i})=>{let a=async a=>{if(a.signal?.aborted)return Promise.reject(ot(`Request aborted`,a,`ERR_CANCELED`));if((0,l.isMissing)(t))e?.warning({category:`HttpClient`,message:`No context for this request - that doesn't seem right.`,context:{interceptor:ct,...a,headers:void 0,hasContext:(0,l.notMissing)(t),hasConfig:(0,l.notMissing)(a)},code:at.InterceptorContextNotPresent});else if((0,l.isMissing)(i)||(0,l.isMissing)(r))e?.error({category:`HttpClient`,message:`RateLimitManager or ConcurrencyManager is not initialized`,context:{interceptor:ct,...a,headers:void 0,rateLimitManagerInitialized:(0,l.notMissing)(i),concurrencyManagerInitialized:(0,l.notMissing)(r)},code:at.RateLimitOrConcurrencyManagerNotInitialized});else if((0,l.notMissing)(t.provider)&&(0,l.notMissing)(t.accountSecureId)){let o=(0,p.randomUUID)(),{provider:s,accountSecureId:c}=t,l=n?.rateLimits??rt,u=n?.concurrency??Be,d=await i.getDynamicMaxWaitTime(l,u,a.url),f=async()=>r.registerRequest(`${c}-${s}`,u,o,a.url),m,h=0;for(;h<=d;){let n=Math.floor(Date.now()/1e3),r=await i.getWaitTime(`${c}-${s}`,l,a.url)??0,o=Math.max(r-n,0);if(r===0){m=await f();break}if(await st(o*1e3,a),h+=o,h>=d){m=await f(),e?.warning({category:`HttpClient`,message:`Max Wait Time Exceeded for ${s} - Account: ${c}`,context:{...t,interceptor:ct,...a,headers:void 0,dynamicMaxWaitTime:d},code:at.InterceptorMaxWaitTimeExceeded});break}}return{...a,requestMetadata:m}}return a};return a},ut=1,dt=10,ft=5,pt=3250368e4,mt=1e3,ht=/^\d+(\.\d+)?$/,gt=1e3,_t=e=>e<pt?e*mt:e;var vt=o(((exports,t)=>{function n(){return t.exports=n=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)({}).hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},t.exports.__esModule=!0,t.exports.default=t.exports,n.apply(null,arguments)}t.exports=n,t.exports.__esModule=!0,t.exports.default=t.exports})),yt={epsilon:1e-12,matrix:`Matrix`,number:`number`,precision:64,predictable:!1,randomSeed:null};function M(e){return typeof e==`number`}function bt(e){return!e||typeof e!=`object`||typeof e.constructor!=`function`?!1:e.isBigNumber===!0&&typeof e.constructor.prototype==`object`&&e.constructor.prototype.isBigNumber===!0||typeof e.constructor.isDecimal==`function`&&e.constructor.isDecimal(e)===!0}function xt(e){return e&&typeof e==`object`&&Object.getPrototypeOf(e).isComplex===!0||!1}function St(e){return e&&typeof e==`object`&&Object.getPrototypeOf(e).isFraction===!0||!1}function Ct(e){return e&&e.constructor.prototype.isUnit===!0||!1}function wt(e){return typeof e==`string`}var N=Array.isArray;function Tt(e){return e&&e.constructor.prototype.isMatrix===!0||!1}function Et(e){return Array.isArray(e)||Tt(e)}function Dt(e){return e&&e.isDenseMatrix&&e.constructor.prototype.isMatrix===!0||!1}function Ot(e){return e&&e.isSparseMatrix&&e.constructor.prototype.isMatrix===!0||!1}function kt(e){return e&&e.constructor.prototype.isRange===!0||!1}function At(e){return e&&e.constructor.prototype.isIndex===!0||!1}function jt(e){return typeof e==`boolean`}function Mt(e){return e&&e.constructor.prototype.isResultSet===!0||!1}function Nt(e){return e&&e.constructor.prototype.isHelp===!0||!1}function Pt(e){return typeof e==`function`}function Ft(e){return e instanceof Date}function It(e){return e instanceof RegExp}function Lt(e){return!!(e&&typeof e==`object`&&e.constructor===Object&&!xt(e)&&!St(e))}function Rt(e){return e===null}function zt(e){return e===void 0}function Bt(e){return e&&e.isAccessorNode===!0&&e.constructor.prototype.isNode===!0||!1}function Vt(e){return e&&e.isArrayNode===!0&&e.constructor.prototype.isNode===!0||!1}function Ht(e){return e&&e.isAssignmentNode===!0&&e.constructor.prototype.isNode===!0||!1}function Ut(e){return e&&e.isBlockNode===!0&&e.constructor.prototype.isNode===!0||!1}function Wt(e){return e&&e.isConditionalNode===!0&&e.constructor.prototype.isNode===!0||!1}function Gt(e){return e&&e.isConstantNode===!0&&e.constructor.prototype.isNode===!0||!1}function Kt(e){return e&&e.isFunctionAssignmentNode===!0&&e.constructor.prototype.isNode===!0||!1}function qt(e){return e&&e.isFunctionNode===!0&&e.constructor.prototype.isNode===!0||!1}function Jt(e){return e&&e.isIndexNode===!0&&e.constructor.prototype.isNode===!0||!1}function Yt(e){return e&&e.isNode===!0&&e.constructor.prototype.isNode===!0||!1}function Xt(e){return e&&e.isObjectNode===!0&&e.constructor.prototype.isNode===!0||!1}function Zt(e){return e&&e.isOperatorNode===!0&&e.constructor.prototype.isNode===!0||!1}function Qt(e){return e&&e.isParenthesisNode===!0&&e.constructor.prototype.isNode===!0||!1}function $t(e){return e&&e.isRangeNode===!0&&e.constructor.prototype.isNode===!0||!1}function en(e){return e&&e.isRelationalNode===!0&&e.constructor.prototype.isNode===!0||!1}function tn(e){return e&&e.isSymbolNode===!0&&e.constructor.prototype.isNode===!0||!1}function nn(e){return e&&e.constructor.prototype.isChain===!0||!1}function rn(e){var t=typeof e;return t===`object`?e===null?`null`:bt(e)?`BigNumber`:e.constructor&&e.constructor.name?e.constructor.name:`Object`:t}function an(e){var t=typeof e;if(t===`number`||t===`string`||t===`boolean`||e==null)return e;if(typeof e.clone==`function`)return e.clone();if(Array.isArray(e))return e.map(function(e){return an(e)});if(e instanceof Date)return new Date(e.valueOf());if(bt(e))return e;if(Lt(e))return on(e,an);throw TypeError(`Cannot clone: unknown type of value (value: ${e})`)}function on(e,t){var n={};for(var r in e)cn(e,r)&&(n[r]=t(e[r]));return n}function sn(e,t){var n,r,i;if(Array.isArray(e)){if(!Array.isArray(t)||e.length!==t.length)return!1;for(r=0,i=e.length;r<i;r++)if(!sn(e[r],t[r]))return!1;return!0}else if(typeof e==`function`)return e===t;else if(e instanceof Object){if(Array.isArray(t)||!(t instanceof Object))return!1;for(n in e)if(!(n in t)||!sn(e[n],t[n]))return!1;for(n in t)if(!(n in e))return!1;return!0}else return e===t}function cn(e,t){return e&&Object.hasOwnProperty.call(e,t)}function ln(e,t){for(var n={},r=0;r<t.length;r++){var i=t[r],a=e[i];a!==void 0&&(n[i]=a)}return n}var un=[`Matrix`,`Array`],dn=[`number`,`BigNumber`,`Fraction`],fn=c(vt(),1),pn=function(e){if(e)throw Error(`The global config is readonly.
|
|
101
|
+
`};var We=class e extends he{hasValidRedisConfig=!1;subscriptionManager=null;queueManager=null;generateUUID;name=`ConcurrencyManager`;async additionalInitialization(e){if(this.generateUUID=e??l.generateRequestId,this.subscriptionManager=new ge({config:this.redisClientConfig,logger:this.logger,instantiator:this.name,subscriptionTTL:Ee,truncateThreshold:De,truncationPercentage:Oe}),await this.subscriptionManager?.initialize(),this.hasValidRedisConfig=await this.checkRedisEventsEmit(),!this.hasValidRedisConfig)throw Error(`Failed to validate Redis configuration for ConcurrencyManager`);if((0,l.isMissing)(this.getSingleton))throw Error(`getSingleton method not available`);this.queueManager=this.getSingleton?.(j)}async addTestSubscription(e,t,n,r){return(0,l.isMissing)(this.subscriptionManager)?!1:this.subscriptionManager.subscribe(e,async(i,a)=>{a===e&&i.includes(t)&&(n.push(a),n.includes(Me)&&n.includes(Ne)&&n.includes(Pe)&&(r(),n.length=0))})}async checkRedisEventsEmit(){let t=[],n=Date.now(),r=async(r,i)=>{let a=()=>{let i=Date.now()-n;this.logger?.info({category:e.name,message:`All Redis events received successfully.`,context:{receivedEventCount:t.length,elapsedTimeMs:i}}),r(!0)},o=this.generateUUID(),s=`${Ie}:${o}`,c=(await Promise.all(Le.map(async e=>this.addTestSubscription(e,s,t,a)))).every(Boolean);if(!c)return i(Error(`Failed to subscribe to event channels.`));await this.cacheClient?.setData({key:s,value:`test`,cacheTTL:Re}),setTimeout(()=>{let e=Date.now()-n;i(Error(`Timeout waiting for Redis events to emit after ${e}ms.`))},ze)};try{return await new Promise(r)}catch(r){let i=Date.now()-n;return this.logger?.error({category:e.name,message:`Redis events failed to emit.`,context:{receivedEventCount:t.length,elapsedTimeMs:i,reason:r?.message},error:r,code:Ve.ConcurrencyManagerRedisEventsEmitError}),!1}}isRedisConfigured(){return this.hasValidRedisConfig}async subscribeToLeaseExpiry(e,t){let n=await this.subscriptionManager?.subscribe(Pe,async(n,r)=>{r===Pe&&n.includes(t)&&n.includes(e)&&await this.releaseRequest(e,t)});return n??!1}async subscribeToSetRemoval(e,t){let n=await this.subscriptionManager?.subscribe(Fe,async(n,r)=>{r===Fe&&n.includes(e)&&await this.executeScript(He.publishNextItem,[t],[])});return n??!1}async registerRequest(t,n,r,i){let a=`concurrency:${t}`,o=`queue:${t}`,s=n.subPools?.find(e=>e.urlPattern instanceof RegExp?e.urlPattern.test(i??``):i?.includes(e.urlPattern))?.subPoolKey,c=(0,l.notMissing)(s)?`${a}-${s}`:a,{mainMaxConcurrency:u,subPools:d}=n,f=[{key:a,maxConcurrency:u},...d?.map(e=>{let{subPoolKey:t,maxConcurrency:n}=e;return{key:`${a}-${t}`,maxConcurrency:n}})??[]],p=f.map(e=>e.key),m=f.map(e=>e.maxConcurrency),[h,g]=await Promise.all([this.subscribeToLeaseExpiry(r,c),this.subscribeToSetRemoval(c,o)]),_={requestId:r,targetConcurrencyKey:c,leaseSubscription:h,setRemovalSubscription:g,queueKey:o},[v,y]=await this.executeScript(He.tryConcurrency,[r,c,o,...p],[ke.toString(),u.toString(),Ae.toString(),...m.map(String)])??[];if(v)return{..._,joinCondition:`optimistic`,failureReason:y};let b=0,x=[];for(;b<Te;){let t=b>1,[n,i]=await this.queueManager?.joinAndWaitTurn(o,r,t,async(e,t)=>await this.executeScript(He.tryConcurrency,[r,c,o,...p],[ke.toString(),u.toString(),Ae.toString(),...m.map(String)]),!1)??[!1,``];if((0,l.notMissing)(i)&&i!==``&&x.push(i),b++,n)return b>1&&this.logger?.warning({category:e.name,message:`Register request succeeded after ${b} attempts.`,context:{..._,registerAttempts:b,joinCondition:`attempts`,failureMessages:x}}),{..._,joinCondition:`attempts`};let a=(0,l.exponentialBackoffInMS)(b,100);await(0,l.delay)(a)}return this.logger?.error({category:e.name,message:`Failed to register request after ${b} attempts.`,context:{..._,registerAttempts:b,joinCondition:`failed`,failureMessages:x},code:Ve.ConcurrencyManagerRegistrationFailed}),{..._,joinCondition:`failed`}}async releaseRequest(e,t){return(0,l.isString)(t)&&(0,l.isString)(e)?!!await this.executeScript(He.removeFromSet,[t],[e]):!1}isReady(){return this.subscriptionManager!==null&&this.cacheClient!==null&&this.scriptMap!==null&&this.scriptMap.size>0&&this.hasValidRedisConfig}close(){this.subscriptionManager?.close(),this.queueManager?.close(),this.scriptMap?.clear(),this.hasValidRedisConfig=!1}};const Ge=(0,l.zStrictObject)({receivedStatus:l.z.number(),targetStatus:l.z.number(),message:l.z.string().optional(),condition:l.z.string().optional()}),Ke={100:`Continue`,101:`Switching Protocols`,102:`Processing`,200:`OK`,201:`Created`,202:`Accepted`,203:`Non-Authoritative Information`,204:`No Content`,205:`Reset Content`,206:`Partial Content`,207:`Multi-Status`,208:`Already Reported`,226:`IM Used`,300:`Multiple Choices`,301:`Moved Permanently`,302:`Found`,303:`See Other`,304:`Not Modified`,305:`Use Proxy`,307:`Temporary Redirect`,308:`Permanent Redirect`,400:`Bad request`,401:`Unauthorized`,402:`Payment Required`,403:`Forbidden`,404:`Not Found`,405:`Method Not Allowed`,406:`Not Acceptable`,407:`Proxy Authentication Required`,408:`Request Timeout`,409:`Conflict`,410:`Gone`,411:`Length Required`,412:`Precondition Failed`,413:`Payload Too Large`,414:`URI Too Long`,415:`Unsupported Media Type`,416:`Range Not Satisfiable`,417:`Expectation Failed`,418:`I'm a teapot`,421:`Misdirected Request`,422:`Unprocessable Entity`,423:`Locked`,424:`Failed Dependency`,425:`Too Early`,426:`Upgrade Required`,428:`Precondition Required`,429:`Too many Requests`,431:`Request Header Fields Too Large`,451:`Unavailable For Legal Reasons`,499:`Client Closed Request`,500:`Internal server error`,501:`Not Implemented`,502:`Bad Gateway`,503:`Unavailable`,504:`Gateway Timeout`,505:`HTTP Version Not Supported`,506:`Variant Also Negotiates`,507:`Insufficient Storage`,508:`Loop Detected`,510:`Not Extended`,511:`Network Authentication Required`};var qe=class e extends Error{response;constructor(t,n){super(n),this.name=`HttpResponseError`,this.response=t,this.message=n??`HTTP error: ${t.status}`,Error.captureStackTrace&&Error.captureStackTrace(this,e)}toString(){let e=this.message?`: ${this.message}`:``;return`${this.name} [${this.response.status}]${e}`}};const Je=e=>e instanceof Error&&e.name===`HttpResponseError`&&`response`in e&&typeof e.response==`object`&&e.response!==null,Ye=(e,t=new WeakSet)=>{if(typeof e!=`object`||!e)return typeof e==`bigint`?e.toString():e;if(t.has(e))return`[Circular Reference]`;if(e instanceof Date)return e.toISOString();if(Array.isArray(e)){t.add(e);let n=e.map(e=>Ye(e,t));return t.delete(e),n}let n=e,r={};t.add(n);for(let[e,i]of Object.entries(n))r[e]=Ye(i,t);return t.delete(n),r},Xe=e=>{if(Je(e)){let t=e.response;return{name:e.name,message:e.message,stack:e.stack,response:Ye(t)}}return e instanceof Error?{name:e.name,message:e.message,stack:e.stack}:{error:String(e)}};var Ze=class{static createInstance({interceptors:e,instanceConfig:t,httpsAgentConfig:n,logger:r,context:i,requestConfig:a,concurrencyManager:o,rateLimitManager:s}){let c=new f.default.Agent(n),l=d.default.create({...t,httpsAgent:c});if(e){let t={axiosInstance:l,logger:r,context:i,requestConfig:a,concurrencyManager:o,rateLimitManager:s};this.applyInterceptors(l,e,t)}return l}static applyInterceptors(e,t,n){let{requestConfigs:r=[],responseConfigs:i=[]}=t,a=[{type:`request`,configs:r},{type:`response`,configs:i}];a.forEach(t=>{let{configs:r,type:i}=t;r.forEach(t=>{let{onFulfilled:r,onRejected:a,options:o}=t;if(i===`request`){let t=[this.resolveInterceptorInstance(r,n),this.resolveInterceptorInstance(a,n),o];e.interceptors.request.use(...t)}if(i===`response`){let t=[this.resolveInterceptorInstance(r,n),this.resolveInterceptorInstance(a,n)];e.interceptors.response.use(...t)}})})}static resolveInterceptorInstance(e,t){return(0,l.notMissing)(e)&&(0,l.isFunction)(e)&&typeof e(t)==`function`?e(t):e??null}};const Qe=300;var $e=class e{static instance=null;static logger=null;dataStore=null;constructor(e){this.initialize(e)}static getInstance(t){return(0,l.isMissing)(e.instance)&&(e.instance=new e(t)),e.instance}initialize(t){this.dataStore=new ie({instantiator:e.name,logger:t,typeGuard:e=>(0,l.notMissing)(e)}),e.logger=t??null,e.logger?.info({category:e.name,message:`InstanceManager initialized.`})}async get(t){return this.ensureReady(),await e.getInstance().dataStore?.getData(t)??null}async set(t,n,r){return this.ensureReady(),await e.getInstance().dataStore?.setData({key:t,value:n,cacheTTL:(0,l.notMissing)(r)?r:Qe})??!1}ensureReady(){this.isReady()||this.initialize()}isReady(){return(0,l.notMissing)(this?.dataStore)}close(){e.instance=null,this.dataStore=null}};const et=$e.getInstance(),tt=()=>{let e=e=>{let{response:t}=e;return e?.code===`ERR_CANCELED`&&(e.response={...t,status:499,statusText:`Request aborted`}),Promise.reject(e)};return e},nt=7.5,rt={mainRatelimit:25};let it=function(e){return e.incr=`incr`,e}({}),at=function(e){return e.RateLimiterReleaseRequestFailed=`RateLimiterReleaseRequestFailed`,e.RateLimitOrConcurrencyManagerNotInitialized=`RateLimitOrConcurrencyManagerNotInitialized`,e.InterceptorContextNotPresent=`InterceptorContextNotPresent`,e.InterceptorMaxWaitTimeExceeded=`InterceptorMaxWaitTimeExceeded`,e}({});const ot=(e,t,n,r)=>{let i=Error(e);return i.config={...t,headers:{}},i.code=n,i.response=r,i.isAxiosError=!0,i},st=(e,t,n)=>{let r=t.signal;return r?r.aborted?Promise.reject(ot(`Request aborted`,t,`ERR_CANCELED`,n)):new Promise((i,a)=>{let o=!1,s,c=ot(`Request aborted`,t,`ERR_CANCELED`,n),l=()=>{if(!o){o=!0,s&&(0,m.clearTimeout)(s);try{r.removeEventListener?.(`abort`,u)}catch{}}},u=()=>{l(),a(c)},d=()=>{l(),i()};try{r.addEventListener?.(`abort`,u,{once:!0})}catch{s=setTimeout(()=>{i()},e);return}if(r.aborted){l(),a(c);return}s=setTimeout(d,e)}):new Promise(t=>setTimeout(t,e))},ct=`checkConcurrencyInterceptor`,lt=({logger:e,context:t,requestConfig:n,concurrencyManager:r,rateLimitManager:i})=>{let a=async a=>{if(a.signal?.aborted)return Promise.reject(ot(`Request aborted`,a,`ERR_CANCELED`));if((0,l.isMissing)(t))e?.warning({category:`HttpClient`,message:`No context for this request - that doesn't seem right.`,context:{interceptor:ct,...a,headers:void 0,hasContext:(0,l.notMissing)(t),hasConfig:(0,l.notMissing)(a)},code:at.InterceptorContextNotPresent});else if((0,l.isMissing)(i)||(0,l.isMissing)(r))e?.error({category:`HttpClient`,message:`RateLimitManager or ConcurrencyManager is not initialized`,context:{interceptor:ct,...a,headers:void 0,rateLimitManagerInitialized:(0,l.notMissing)(i),concurrencyManagerInitialized:(0,l.notMissing)(r)},code:at.RateLimitOrConcurrencyManagerNotInitialized});else if((0,l.notMissing)(t.provider)&&(0,l.notMissing)(t.accountSecureId)){let o=(0,p.randomUUID)(),{provider:s,accountSecureId:c}=t,l=n?.rateLimits??rt,u=n?.concurrency??Be,d=await i.getDynamicMaxWaitTime(l,u,a.url),f=async()=>r.registerRequest(`${c}-${s}`,u,o,a.url),m,h=0;for(;h<=d;){let n=Math.floor(Date.now()/1e3),r=await i.getWaitTime(`${c}-${s}`,l,a.url)??0,o=Math.max(r-n,0);if(r===0){m=await f();break}if(await st(o*1e3,a),h+=o,h>=d){m=await f(),e?.warning({category:`HttpClient`,message:`Max Wait Time Exceeded for ${s} - Account: ${c}`,context:{...t,interceptor:ct,...a,headers:void 0,dynamicMaxWaitTime:d},code:at.InterceptorMaxWaitTimeExceeded});break}}return{...a,requestMetadata:m}}return a};return a},ut=1,dt=10,ft=5,pt=3250368e4,mt=1e3,ht=/^\d+(\.\d+)?$/,gt=1e3,_t=e=>e<pt?e*mt:e;var vt=o(((exports,t)=>{function n(){return t.exports=n=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)({}).hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},t.exports.__esModule=!0,t.exports.default=t.exports,n.apply(null,arguments)}t.exports=n,t.exports.__esModule=!0,t.exports.default=t.exports})),yt={epsilon:1e-12,matrix:`Matrix`,number:`number`,precision:64,predictable:!1,randomSeed:null};function M(e){return typeof e==`number`}function bt(e){return!e||typeof e!=`object`||typeof e.constructor!=`function`?!1:e.isBigNumber===!0&&typeof e.constructor.prototype==`object`&&e.constructor.prototype.isBigNumber===!0||typeof e.constructor.isDecimal==`function`&&e.constructor.isDecimal(e)===!0}function xt(e){return e&&typeof e==`object`&&Object.getPrototypeOf(e).isComplex===!0||!1}function St(e){return e&&typeof e==`object`&&Object.getPrototypeOf(e).isFraction===!0||!1}function Ct(e){return e&&e.constructor.prototype.isUnit===!0||!1}function wt(e){return typeof e==`string`}var N=Array.isArray;function Tt(e){return e&&e.constructor.prototype.isMatrix===!0||!1}function Et(e){return Array.isArray(e)||Tt(e)}function Dt(e){return e&&e.isDenseMatrix&&e.constructor.prototype.isMatrix===!0||!1}function Ot(e){return e&&e.isSparseMatrix&&e.constructor.prototype.isMatrix===!0||!1}function kt(e){return e&&e.constructor.prototype.isRange===!0||!1}function At(e){return e&&e.constructor.prototype.isIndex===!0||!1}function jt(e){return typeof e==`boolean`}function Mt(e){return e&&e.constructor.prototype.isResultSet===!0||!1}function Nt(e){return e&&e.constructor.prototype.isHelp===!0||!1}function Pt(e){return typeof e==`function`}function Ft(e){return e instanceof Date}function It(e){return e instanceof RegExp}function Lt(e){return!!(e&&typeof e==`object`&&e.constructor===Object&&!xt(e)&&!St(e))}function Rt(e){return e===null}function zt(e){return e===void 0}function Bt(e){return e&&e.isAccessorNode===!0&&e.constructor.prototype.isNode===!0||!1}function Vt(e){return e&&e.isArrayNode===!0&&e.constructor.prototype.isNode===!0||!1}function Ht(e){return e&&e.isAssignmentNode===!0&&e.constructor.prototype.isNode===!0||!1}function Ut(e){return e&&e.isBlockNode===!0&&e.constructor.prototype.isNode===!0||!1}function Wt(e){return e&&e.isConditionalNode===!0&&e.constructor.prototype.isNode===!0||!1}function Gt(e){return e&&e.isConstantNode===!0&&e.constructor.prototype.isNode===!0||!1}function Kt(e){return e&&e.isFunctionAssignmentNode===!0&&e.constructor.prototype.isNode===!0||!1}function qt(e){return e&&e.isFunctionNode===!0&&e.constructor.prototype.isNode===!0||!1}function Jt(e){return e&&e.isIndexNode===!0&&e.constructor.prototype.isNode===!0||!1}function Yt(e){return e&&e.isNode===!0&&e.constructor.prototype.isNode===!0||!1}function Xt(e){return e&&e.isObjectNode===!0&&e.constructor.prototype.isNode===!0||!1}function Zt(e){return e&&e.isOperatorNode===!0&&e.constructor.prototype.isNode===!0||!1}function Qt(e){return e&&e.isParenthesisNode===!0&&e.constructor.prototype.isNode===!0||!1}function $t(e){return e&&e.isRangeNode===!0&&e.constructor.prototype.isNode===!0||!1}function en(e){return e&&e.isRelationalNode===!0&&e.constructor.prototype.isNode===!0||!1}function tn(e){return e&&e.isSymbolNode===!0&&e.constructor.prototype.isNode===!0||!1}function nn(e){return e&&e.constructor.prototype.isChain===!0||!1}function rn(e){var t=typeof e;return t===`object`?e===null?`null`:bt(e)?`BigNumber`:e.constructor&&e.constructor.name?e.constructor.name:`Object`:t}function an(e){var t=typeof e;if(t===`number`||t===`string`||t===`boolean`||e==null)return e;if(typeof e.clone==`function`)return e.clone();if(Array.isArray(e))return e.map(function(e){return an(e)});if(e instanceof Date)return new Date(e.valueOf());if(bt(e))return e;if(Lt(e))return on(e,an);throw TypeError(`Cannot clone: unknown type of value (value: ${e})`)}function on(e,t){var n={};for(var r in e)cn(e,r)&&(n[r]=t(e[r]));return n}function sn(e,t){var n,r,i;if(Array.isArray(e)){if(!Array.isArray(t)||e.length!==t.length)return!1;for(r=0,i=e.length;r<i;r++)if(!sn(e[r],t[r]))return!1;return!0}else if(typeof e==`function`)return e===t;else if(e instanceof Object){if(Array.isArray(t)||!(t instanceof Object))return!1;for(n in e)if(!(n in t)||!sn(e[n],t[n]))return!1;for(n in t)if(!(n in e))return!1;return!0}else return e===t}function cn(e,t){return e&&Object.hasOwnProperty.call(e,t)}function ln(e,t){for(var n={},r=0;r<t.length;r++){var i=t[r],a=e[i];a!==void 0&&(n[i]=a)}return n}var un=[`Matrix`,`Array`],dn=[`number`,`BigNumber`,`Fraction`],fn=c(vt(),1),pn=function(e){if(e)throw Error(`The global config is readonly.
|
|
102
102
|
Please create a mathjs instance if you want to change the default configuration.
|
|
103
103
|
Example:
|
|
104
104
|
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{delay as e,exponentialBackoffInMS as t,generateRequestId as n,getContentHash as r,isFunction as i,isFutureUnixTimestamp as a,isMissing as o,isNumber as s,isObject as c,isString as l,notMissing as u,z as d,zStrictObject as f}from"@stackone/utils";import*as p from"redis";import m,{isAxiosError as h}from"axios";import g from"node:https";import{randomUUID as _}from"crypto";import{clearTimeout as v}from"node:timers";import y from"jsonpath";import{redactUrl as b}from"@stackone/redaction";import x from"qs";import{safeEvaluate as S}from"@stackone/expressions";var C=Object.create,w=Object.defineProperty,T=Object.getOwnPropertyDescriptor,E=Object.getOwnPropertyNames,D=Object.getPrototypeOf,ee=Object.prototype.hasOwnProperty,O=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports),te=(e,t,n,r)=>{if(t&&typeof t==`object`||typeof t==`function`)for(var i=E(t),a=0,o=i.length,s;a<o;a++)s=i[a],!ee.call(e,s)&&s!==n&&w(e,s,{get:(e=>t[e]).bind(null,s),enumerable:!(r=T(t,s))||r.enumerable});return e},k=(e,t,n)=>(n=e==null?{}:C(D(e)),te(t||!e||!e.__esModule?w(n,`default`,{value:e,enumerable:!0}):n,e));const A=e=>{switch(e.type){case`none`:return{};case`basic`:return ne(e);case`bearer`:return re(e);case`oauth2`:return re(e);default:throw Error(`Invalid authentication type`)}},ne=({username:e=``,password:t=``,encoding:n=`base64`})=>{let r=n,i=Buffer.from(`${e}:${t}`).toString(r);return{authorization:`Basic ${i}`}},re=({token:e,includeBearer:t})=>({authorization:`${t?`Bearer `:``}${e}`});var ie=class{locks;constructor(){this.locks=new Map}async withLock(e,t){await this.lock(e);try{return t()}finally{this.unlock(e)}}async lock(e){let t,n=new Promise(e=>t=e),r=this.locks.has(e),i=this.locks.get(e);if(r&&i){i.push({lock:n,unlock:t});let e=i.length-2;await i[e].lock}else this.locks.set(e,[{lock:n,unlock:t}])}unlock(e){let t=this.locks.has(e),n=this.locks.get(e);if(t&&n&&n.length>0){let e=n.shift()?.unlock;e?.()}this.queueLength(e)===0&&this.locks.delete(e)}queueLength(e){return this.locks.get(e)?.length}close(){this.locks.clear()}};const ae=1e3,oe=100,se=60,ce=6e4,le=se,ue=ce,de=ce*10,fe=oe,pe=10;let me=function(e){return e.MemoryStorePruneError=`MemoryStorePruneError`,e}({});var he=class{config;instantiator;dataStore;lockManager;expiryMap;evictionFrequency;staleDataThreshold;truncateThreshold;truncationPercentage;logger;typeGuard;dispose;evictionInterval;lastAccessedAt=Date.now();constructor(e={}){this.config=e,this.initialize(e)}initialize(e=this.config){this.instantiator=e?.instantiator??`Unknown`,this.logger=e?.logger,this.dataStore=e?.dataStore??new Map,this.lockManager=e?.lockManager??new ie,this.expiryMap=e?.expiryMap??new Map,this.evictionFrequency=e?.evictionFrequency??ue,this.staleDataThreshold=e?.staleDataThreshold??de,this.truncateThreshold=e?.truncateThreshold??fe,this.truncationPercentage=e?.truncationPercentage??pe,this.typeGuard=e?.typeGuard,this.dispose=e?.dispose,this.startEvictionTask()}async getData(e){return this.isReady()||this.initialize(),this.updateLastAccessedAt(),this.lockManager.withLock(e,async()=>this.dataStore.get(e)??null)}async setData({key:e,value:t,cacheTTL:n=le}){if(this.isReady()||this.initialize(),this.updateLastAccessedAt(),u(this.typeGuard)&&!this.typeGuard(t))return!1;let r=n*ae,i=Date.now()+r;return await this.lockManager.withLock(e,async()=>{(u(this.typeGuard)&&this.typeGuard(t)||o(this.typeGuard)&&this.typeGuardBypass(t))&&this.dataStore.set(e,t),this.expiryMap.set(e,i)}),!0}typeGuardBypass(e){return this.logger?.debug({category:`MemoryStore`,message:`${this.instantiator} MemoryStore setting data without type guard - you should probably configure one`}),!0}async delete(e){return this.isReady()||this.initialize(),this.updateLastAccessedAt(),this.lockManager.withLock(e,async()=>{if(this.dispose){let t=this.dataStore.get(e);await this.dispose(e,t)}return this.dataStore.delete(e)})}async pruneExpiredKeys(){let e=this.dataStore.size,t=e>=this.truncateThreshold;if(e<=0)return;let n=[],r=0;this.dataStore.forEach(async(e,i)=>{let a=Date.now(),o=this.expiryMap.get(i)??0,s=this.truncateThreshold*this.truncationPercentage/oe;(o<=a||t&&r>=0&&r<=s)&&n.push(this.lockManager.withLock(i,async()=>(this.dispose&&await this.dispose(i,e),this.dataStore.delete(i)))),r++}),await Promise.all(n);let i=this.dataStore.size;return{dataStoreSize:e,prunedDataStoreSize:i}}startEvictionTask(){if(u(this.evictionInterval))return;let e=async()=>{let t;try{let n=this.lastAccessedAt+this.staleDataThreshold,r=Date.now();if(n<r){this.logger?.warning({message:`Closing the ${this.instantiator}'s MemoryStore instance - received no requests for a while.`,category:`MemoryStore`}),this.close();return}t=await this.pruneExpiredKeys(),this.evictionInterval=setTimeout(e,this.evictionFrequency)}catch(t){t instanceof Error&&this.logger?.error({message:`Error during pruning expired keys:`,category:`MemoryStore`,error:t,code:me.MemoryStorePruneError}),this.evictionInterval=setTimeout(e,this.evictionFrequency)}finally{if(u(t?.dataStoreSize)&&u(t?.prunedDataStoreSize)){let{dataStoreSize:e,prunedDataStoreSize:n}=t,r=e-n;this.logger?.debug({message:`Pruned ${r} expired keys, ${n} remain, scheduling next prune.`,category:`MemoryStore`,context:{instantiator:this.instantiator}})}}};this.evictionInterval=setTimeout(e,this.evictionFrequency)}stopEvictionTask(){this.evictionInterval&&(clearTimeout(this.evictionInterval),this.evictionInterval=void 0)}updateLastAccessedAt(){this.lastAccessedAt=Date.now()}isReady(){return u(this.evictionInterval)&&u(this.dataStore)&&u(this.expiryMap)&&u(this.lockManager)}close(){this.stopEvictionTask(),this.dataStore.clear(),this.expiryMap.clear(),this.lockManager.close()}async listData({partialKey:e,cursor:t,limit:n}){let r=Array.from(this.dataStore.keys()),i=r.filter(t=>t.includes(e)),a=[],o=t?parseInt(t,10):0;for(let e=o;e<n+o;e++){let t=i[e];if(!t)break;let r=await this.getData(t);if(r&&a.push(r),a.length>=n)break}return{items:a,cursor:a.length<n?void 0:(o+n).toString()}}};const ge=1e3,_e=6e4;let ve=function(e){return e.EventClientResolveError=`EventClientResolveError`,e}({});var ye=class{executorMethodStore=null;promiseStore=null;logger;eventClientConfig=null;executorMethodStoreConfig=null;pendingPromiseStoreConfig=null;constructor(e,t,n){this.eventClientConfig=e,this.executorMethodStoreConfig=t,this.pendingPromiseStoreConfig=n;let r=e?.instantiator?`${e?.instantiator}(${this.constructor.name})`:this.constructor.name,i={logger:this.logger,dispose:this.defaultExecutorMethodDispose,typeGuard:this.defaultExecutorMethodTypeGuard,...this.executorMethodStoreConfig??{},instantiator:r},a={logger:this.logger,dispose:this.defaultPendingPromiseDispose,typeGuard:this.defaultPendingPromiseTypeGuard,...this.pendingPromiseStoreConfig??{},instantiator:r};this.executorMethodStore=new he(i),this.promiseStore=new he(a)}async setPendingEvent(e,t){let n={resolve:void 0,reject:void 0},r=new Promise((e,t)=>{n.resolve=e,n.reject=t}),i=t/ge;await this.executorMethodStore?.setData({key:e,value:n,cacheTTL:i}),await this.promiseStore?.setData({key:e,value:r,cacheTTL:i})}async waitForEvent(e,t){let n=await this.promiseStore?.getData(e);return u(n)?n:(await this.setPendingEvent(e,t),this.getPendingEvent(e))}async getPendingEvent(e){return this.promiseStore?.getData(e)??null}async deleteEvent(e){return(await Promise.all([this.promiseStore?.delete(e),this.executorMethodStore?.delete(e)])).every(Boolean)}async resolveEvent(e,t){try{let n=await this.executorMethodStore?.getData(e);o(n)&&(await this.setPendingEvent(e,this.eventClientConfig?.defaultTimeoutMS??_e),n=await this.executorMethodStore?.getData(e)),n?.resolve?.(t)}catch(t){t instanceof Error&&this.logger.error({message:`Error handling event for key ${e}: ${t.message}`,category:this.constructor.name,context:{eventKey:e},error:t,code:ve.EventClientResolveError})}}async defaultExecutorMethodDispose(e,t){u(t?.resolve)?t.resolve(this.eventClientConfig?.timeoutResolveValue):u(t?.reject)&&t.reject(Error(`Event key: ${e} was not resolved or the event was disposed`))}defaultExecutorMethodTypeGuard(e){return c(e)&&e.hasOwnProperty(`resolve`)&&e.hasOwnProperty(`reject`)}async defaultPendingPromiseDispose(e,t){t instanceof Promise&&t?.finally(()=>{})}defaultPendingPromiseTypeGuard(e){return e instanceof Promise}},be=class{instance=null;initPromise=null;retryTimeout=null;status=`uninitialized`;initOptions=null;initArgs=[];getRetryDelay(){return 5e3}getSingleton(e){throw Error(`getSingleton method not implemented`)}async getInstance(e,...t){return this.instance?this.instance:(o(this.initPromise)&&(this.initOptions=e??null,this.initArgs=t,this.initPromise=this.init(e??null,...t)),this.initPromise)}getInstanceIfReady(){return this.instance}isReady(){return this.status===`ready`}hasInitFailed(){return this.status===`failed`}async init(e,...t){try{let n=await this.initInstance(e,...t);return this.instance=n,this.status=`ready`,n}catch(e){throw this.status=`failed`,this.initPromise=null,o(this.retryTimeout)&&(this.retryTimeout=setTimeout(()=>{this.retryTimeout=null,this.getInstance(this.initOptions,...this.initArgs).catch(()=>{})},this.getRetryDelay())),e}}reset(){this.instance=null,this.initPromise=null,this.initOptions=null,this.status=`uninitialized`,this.retryTimeout&&clearTimeout(this.retryTimeout),this.retryTimeout=null}};let j=function(e){return e.RedisClientError=`RedisClientError`,e.RedisClientCommandError=`RedisClientCommandError`,e.RedisClientDeleteError=`RedisClientDeleteError`,e.RedisClientInvalidTTL=`RedisClientInvalidTTL`,e.RedisClientListError=`RedisClientListError`,e.RedisClientNotInitialized=`RedisClientNotInitialized`,e.RedisClientPublishError=`RedisClientPublishError`,e.RedisClientReadError=`RedisClientReadError`,e.RedisClientRedisNotReady=`RedisClientRedisNotReady`,e.RedisClientScriptError=`RedisClientScriptError`,e.RedisClientScriptExecuteError=`RedisClientScriptExecuteError`,e.RedisClientScriptLoadError=`RedisClientScriptLoadError`,e.RedisClientSubscribeError=`RedisClientSubscribeError`,e.RedisClientUnsubscribeError=`RedisClientUnsubscribeError`,e.RedisClientWriteError=`RedisClientWriteError`,e}({});const xe=async(e,t)=>{try{let n=await Se.build(e,t);return n??void 0}catch(e){let n=e;t?.error({message:`Error building Cache Manager`,error:n,code:j.RedisClientError,category:`buildRedisClientInstance`});return}};var Se=class e{#redisClient;#logger;static async build({getRedisClient:t=p.createClient,host:n,port:r,tls:i,reconnect:a=!0,database:o},s){let c=new e;c.#logger=s;try{c.#redisClient=t({socket:{reconnectStrategy:a?e=>Math.min(e*20,5e3):!1,host:n,port:r,tls:i},database:o,disableOfflineQueue:!0});let s=we(e.name,c.#logger);return Ce(c.#redisClient,s),await c.#redisClient.connect(),c}catch(t){let n=t;return c.#logger?.error({message:`Error building Cache Manager.`,error:n,code:j.RedisClientError,category:e.name}),null}}#parseData(e){try{return JSON.parse(e)}catch{return e}}async getData(t){if(!this.#isRedisClientAvailable())return null;try{let e=await this.#redisClient.get(t);if(!e)return null;let n=this.#parseData(e);return l(n)?e:n}catch(t){let n=t;return this.#logger?.error({message:`Error getting data`,error:n,code:j.RedisClientReadError,category:e.name}),null}}async setData({key:t,value:n,cacheTTL:r,groupKey:i}){if(!this.#isRedisClientAvailable())return!1;try{let e=l(n)?n:JSON.stringify(n);return await this.#redisClient.set(t,e,{EX:r}),u(i)&&await this.#redisClient.sAdd(i,[t]),!0}catch(t){let n=t;return this.#logger?.error({message:`Error setting data`,error:n,code:j.RedisClientWriteError,category:e.name}),!1}}async executeScript({sha1:t,keys:n,args:r}){if(!this.#isRedisClientAvailable())return null;try{return await this.#redisClient.evalSha(t,{keys:n,arguments:r})}catch(t){let n=t;return this.#logger?.error({message:`Error executing script`,error:n,code:j.RedisClientScriptExecuteError,category:e.name}),null}}async loadScript(t){if(!this.#isRedisClientAvailable())return null;try{return await this.#redisClient.scriptLoad(t)}catch(t){let n=t;return this.#logger?.error({message:`Error loading script`,error:n,code:j.RedisClientScriptLoadError,category:e.name}),null}}async#executeCounterCommand(t,n,r){if(!this.#isRedisClientAvailable())return null;if(!Number.isFinite(r)||r<=0)return this.#logger?.error({message:`Invalid cacheTTL parameter`,category:e.name,code:j.RedisClientInvalidTTL}),null;try{let e=[{args:[t,n]},{args:[`expire`,n,r.toString()]}],i=await this.#redisClient.multiExecutor(e),a=i?.[0];return typeof a==`number`?a:null}catch(n){let r=n;return this.#logger?.error({message:`Error executing ${t} operation`,error:r,code:j.RedisClientCommandError,category:e.name}),null}}async increment(e,t){return this.#executeCounterCommand(`incr`,e,t)}async decrement(e,t){return this.#executeCounterCommand(`decr`,e,t)}async subscribe(t,n){if(!this.#isRedisClientAvailable())return!1;try{return await this.#redisClient.pSubscribe(t,n),!0}catch(n){let r=n;return this.#logger?.error({message:`Error subscribing to ${t}`,error:r,code:j.RedisClientSubscribeError,category:e.name}),!1}}async unsubscribe(t){if(!this.#isRedisClientAvailable())return!1;try{return await this.#redisClient.pUnsubscribe(t),!0}catch(n){let r=n;return this.#logger?.error({message:`Error unsubscribing from ${t}`,error:r,code:j.RedisClientUnsubscribeError,category:e.name}),!1}}async publish(t,n){if(!this.#isRedisClientAvailable())return null;try{let r=await this.#redisClient.publish(t,n);return r===0&&this.#logger?.debug({message:`No subscribers found for channel ${t}`,category:e.name}),r}catch(n){let r=n;return this.#logger?.error({message:`Error publishing to ${t}`,error:r,code:j.RedisClientPublishError,category:e.name}),null}}getClient(){return this.#isRedisClientAvailable()?this.#redisClient:null}#isRedisClientAvailable(){let t=this.#redisClient.isReady&&this.#redisClient.isOpen;return t||this.#logger?.error({message:`Redis Client is not ready.`,category:e.name,code:j.RedisClientRedisNotReady}),t}async listData({partialKey:t,limit:n,cursor:r}){try{let e=r?parseInt(r,10):0,i=await this.#redisClient.sScan(t,e,{COUNT:n}),a=[];for(let e of i.members){let t=await this.getData(e);u(t)&&a.push(t)}return{items:a,cursor:i.cursor>0?i.cursor.toString():void 0}}catch(t){let n=t;return this.#logger?.error({message:`Error listing data`,error:n,code:j.RedisClientListError,category:e.name}),{items:null}}}async deleteData(t){try{if(!this.#isRedisClientAvailable())return!1;let n=await this.#redisClient.del(t);return n===0&&this.#logger?.debug({message:`No keys deleted for ${t}`,category:e.name}),n>0}catch(t){let n=t;return this.#logger?.error({message:`Error deleting data`,error:n,code:j.RedisClientDeleteError,category:e.name}),!1}}};const Ce=(e,t)=>{t.forEach(({eventName:t,listener:n})=>{e.on(t,n)})},we=(e=`RedisClient`,t)=>[{eventName:`error`,listener:n=>{t?.error({message:`Redis client error ${n.message}`,error:n,code:j.RedisClientError,category:e})}},{eventName:`ready`,listener:()=>{t?.info({message:`Redis client ready`,category:e})}},{eventName:`connect`,listener:()=>{t?.info({message:`Redis client connected`,category:e})}},{eventName:`reconnecting`,listener:()=>{t?.info({message:`Redis client reconnecting`,category:e})}},{eventName:`end`,listener:()=>{t?.info({message:`Redis client disconnected`,category:e})}}];let Te=function(e){return e.ScriptManagerExecutionError=`ScriptManagerExecutionError`,e.ScriptManagerLoadingError=`ScriptManagerLoadingError`,e}({});var Ee=class extends be{redisClientConfig;scripts;scriptMap;cacheClient;logger;additionalArgs;async initInstance({redisClientConfig:e=this.redisClientConfig,cacheClient:t=this.cacheClient,scripts:n=this.scripts,scriptMap:r=this.scriptMap,logger:i=this.logger,additionalArgs:a=this.additionalArgs}){let s=t??await xe(e,i);if(o(s))throw Error(`Failed to build Redis client`);return this.redisClientConfig=e,this.logger=i,this.cacheClient=s,this.scripts=n,this.scriptMap=r??new Map,this.additionalArgs=a??[],await this.loadScripts(n,r),await this.additionalInitialization(...a??[]),this}async loadScripts(e,t){if(u(t))for(let[e,n]of t)this.scriptMap.set(e,n);for(let[t,n]of Object.entries(e)){let e=await this.cacheClient?.loadScript?.(n);u(e)?this.scriptMap?.set(t,e):this.logger?.error({message:`Error loading script ${t}`,category:this.constructor.name,code:Te.ScriptManagerLoadingError})}}async executeScript(e,t,n){try{if(!this.isRedisReady())throw Error(`Redis client is not ready`);let r=this.scriptMap?.get(e);if(o(r))throw Error(`Script for ${e} not found`);return await this.cacheClient?.executeScript?.({sha1:r,keys:t,args:n})??null}catch(r){throw r instanceof Error&&this.logger?.error({message:`Error executing script ${e}: ${r.message}`,category:this.constructor.name,context:{keys:t,args:n},error:r,code:Te.ScriptManagerExecutionError}),r}}isRedisReady(){return this.isReady()&&u(this.cacheClient)}},De=class e{options={getCacheClient:xe};subscriptionMap=null;subscriptionClient=null;logger;constructor(e){this.options={...this.options,...e}}async initialize(t=this.options){let{config:n,getCacheClient:r,logger:i}=t;if(this.logger=i,this.subscriptionMap=new he({instantiator:`${t?.instantiator}(${this.constructor.name})`,evictionFrequency:t?.subscriptionTTL,staleDataThreshold:t?.staleSubscriptionsThreshold,truncateThreshold:t?.truncateThreshold,truncationPercentage:t?.truncationPercentage,typeGuard:e=>typeof e==`string`,dispose:async e=>{await this.subscriptionClient?.unsubscribe?.(e)}}),u(n))this.subscriptionClient=await r?.(n,this.logger)??null,this.logger?.info({message:`${e.name} initialized for ${t.instantiator}.`,category:e.name});else throw Error(`SubscriptionManager requires a Redis client configuration.`)}async subscribe(e,t){this.isReady()||await this.initialize(this.options);let n=await this.subscriptionMap?.getData(e),i=r(t.toString());return await this.subscriptionMap?.setData({key:e,value:i}),n===i?!0:await this.subscriptionClient?.subscribe?.(e,t)??!1}async unsubscribe(e){return this.isReady()||await this.initialize(this.options),this.subscriptionClient?.unsubscribe?.(e)??!1}isReady(){return u(this.subscriptionClient)&&u(this.subscriptionMap)}close(){this.subscriptionMap?.close(),this.subscriptionMap=null,this.subscriptionClient=null}};const Oe=6e4,ke=15e3,Ae=!1,je=6e4,Me=15e3,Ne=1e4,Pe=1;let Fe=function(e){return e.QueueManagerInitializationError=`QueueManagerInitializationError`,e.QueueManagerJoinAndWaitTurnError=`QueueManagerJoinAndWaitTurnError`,e.QueueManagerLengthError=`QueueManagerLengthError`,e.QueueManagerPopError=`QueueManagerPopError`,e}({}),Ie=function(e){return e.rPush=`rPush`,e.lPush=`lPush`,e.lPop=`lPop`,e.llen=`llen`,e}({});var Le=class extends Ee{subscriptionManager=null;eventClient=null;name=`QueueManager`;async additionalInitialization(){if(o(this.redisClientConfig))throw Error(`Redis client configuration is required to initialize QueueManager.`);this.subscriptionManager=new De({config:this.redisClientConfig,instantiator:this.name,subscriptionTTL:je,truncateThreshold:Ne,truncationPercentage:Pe}),await this.subscriptionManager?.initialize(),this.eventClient=new ye({instantiator:this.name,timeoutResolveValue:Ae},{instantiator:this.name,evictionFrequency:Me,truncateThreshold:Ne,truncationPercentage:Pe},{instantiator:this.name,evictionFrequency:Me,truncateThreshold:Ne,truncationPercentage:Pe}),this.logger?.info({category:this.name,message:`QueueManager initialized.`})}async joinAndWaitTurn(e,t,n,r,i=!0){try{let a=`${e}:${t}`;await this.subscriptionManager?.subscribe(a,async(n,r)=>{if(r.includes(a)&&r.includes(t)){let t=`${e}:${n}`;return this.eventClient?.resolveEvent(t,!0)}});let o=await this.executeScript(n?Ie.lPush:Ie.rPush,[e],[t,Oe.toString()]),s=o===1,c=i?!s:!0;return c&&await this.eventClient?.waitForEvent(a,ke),await this.eventClient?.deleteEvent(a),await r(e,t)}catch(n){throw n instanceof Error&&this.logger?.error({message:`Error in joinAndWaitTurn`,category:this.name,error:n,code:Fe.QueueManagerJoinAndWaitTurnError,context:{queueName:e,value:t}}),n}}async pop(e){try{return await this.executeScript(Ie.lPop,[e],[])}catch(t){throw t instanceof Error&&this.logger?.error({message:`Error in pop`,category:this.name,error:t,code:Fe.QueueManagerPopError,context:{queueName:e}}),t}}async length(e){try{return await this.executeScript(Ie.llen,[e],[])}catch(t){throw t instanceof Error&&this.logger?.error({message:`Error in length`,category:this.name,error:t,code:Fe.QueueManagerLengthError,context:{queueName:e}}),t}}isReady(){return u(this.subscriptionManager)&&u(this.cacheClient)&&u(this.scriptMap)&&this.scriptMap.size>0}close(){this.subscriptionManager?.close(),this.scriptMap?.clear()}};const Re=5,ze=12e4,Be=1e4,Ve=1,He=60,Ue=90,We=`__keyevent@0__:`,Ge=`${We}set`,Ke=`${We}expire`,qe=`${We}expired`,Je=`${We}srem`,Ye=`config_check_test_key`,Xe=[Ge,Ke,qe],Ze=1,Qe=2500,$e={mainMaxConcurrency:30};let et=function(e){return e.ConcurrencyManagerInitializationError=`ConcurrencyManagerInitializationError`,e.ConcurrencyManagerRedisEventsEmitError=`ConcurrencyManagerRedisEventsEmitError`,e.ConcurrencyManagerRegistrationFailed=`ConcurrencyManagerRegistrationFailed`,e}({}),tt=function(e){return e.tryConcurrency=`tryConcurrency`,e.removeFromSet=`removeFromSet`,e.publishNextItem=`publishNextItem`,e}({});const nt={[tt.tryConcurrency]:`
|
|
1
|
+
import{delay as e,exponentialBackoffInMS as t,generateRequestId as n,getContentHash as r,isFunction as i,isFutureUnixTimestamp as a,isMissing as o,isNumber as s,isObject as c,isString as l,notMissing as u,z as d,zStrictObject as f}from"@stackone/utils";import*as p from"redis";import m,{isAxiosError as h}from"axios";import g from"node:https";import{randomUUID as _}from"crypto";import{clearTimeout as v}from"node:timers";import y from"jsonpath";import{redactUrl as b}from"@stackone/redaction";import x from"qs";import{safeEvaluate as S}from"@stackone/expressions";var C=Object.create,w=Object.defineProperty,T=Object.getOwnPropertyDescriptor,E=Object.getOwnPropertyNames,D=Object.getPrototypeOf,ee=Object.prototype.hasOwnProperty,O=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports),te=(e,t,n,r)=>{if(t&&typeof t==`object`||typeof t==`function`)for(var i=E(t),a=0,o=i.length,s;a<o;a++)s=i[a],!ee.call(e,s)&&s!==n&&w(e,s,{get:(e=>t[e]).bind(null,s),enumerable:!(r=T(t,s))||r.enumerable});return e},k=(e,t,n)=>(n=e==null?{}:C(D(e)),te(t||!e||!e.__esModule?w(n,`default`,{value:e,enumerable:!0}):n,e));const A=e=>{switch(e.type){case`none`:return{};case`basic`:return ne(e);case`bearer`:return re(e);case`oauth2`:return re(e);default:throw Error(`Invalid authentication type`)}},ne=({username:e=``,password:t=``,encoding:n=`base64`})=>{let r=n,i=Buffer.from(`${e}:${t}`).toString(r);return{authorization:`Basic ${i}`}},re=({token:e,includeBearer:t})=>({authorization:`${t?`Bearer `:``}${e}`});var ie=class{locks;constructor(){this.locks=new Map}async withLock(e,t){await this.lock(e);try{return t()}finally{this.unlock(e)}}async lock(e){let t,n=new Promise(e=>t=e),r=this.locks.has(e),i=this.locks.get(e);if(r&&i){i.push({lock:n,unlock:t});let e=i.length-2;await i[e].lock}else this.locks.set(e,[{lock:n,unlock:t}])}unlock(e){let t=this.locks.has(e),n=this.locks.get(e);if(t&&n&&n.length>0){let e=n.shift()?.unlock;e?.()}this.queueLength(e)===0&&this.locks.delete(e)}queueLength(e){return this.locks.get(e)?.length}close(){this.locks.clear()}};const ae=1e3,oe=100,se=60,ce=6e4,le=se,ue=ce,de=ce*10,fe=oe,pe=10;let me=function(e){return e.MemoryStorePruneError=`MemoryStorePruneError`,e}({});var he=class{config;instantiator;dataStore;lockManager;expiryMap;evictionFrequency;staleDataThreshold;truncateThreshold;truncationPercentage;logger;typeGuard;dispose;evictionInterval;lastAccessedAt=Date.now();constructor(e={}){this.config=e,this.initialize(e)}initialize(e=this.config){this.instantiator=e?.instantiator??`Unknown`,this.logger=e?.logger,this.dataStore=e?.dataStore??new Map,this.lockManager=e?.lockManager??new ie,this.expiryMap=e?.expiryMap??new Map,this.evictionFrequency=e?.evictionFrequency??ue,this.staleDataThreshold=e?.staleDataThreshold??de,this.truncateThreshold=e?.truncateThreshold??fe,this.truncationPercentage=e?.truncationPercentage??pe,this.typeGuard=e?.typeGuard,this.dispose=e?.dispose,this.startEvictionTask()}async getData(e){return this.isReady()||this.initialize(),this.updateLastAccessedAt(),this.lockManager.withLock(e,async()=>this.dataStore.get(e)??null)}async setData({key:e,value:t,cacheTTL:n=le}){if(this.isReady()||this.initialize(),this.updateLastAccessedAt(),u(this.typeGuard)&&!this.typeGuard(t))return!1;let r=n*ae,i=Date.now()+r;return await this.lockManager.withLock(e,async()=>{(u(this.typeGuard)&&this.typeGuard(t)||o(this.typeGuard)&&this.typeGuardBypass(t))&&this.dataStore.set(e,t),this.expiryMap.set(e,i)}),!0}typeGuardBypass(e){return this.logger?.debug({category:`MemoryStore`,message:`${this.instantiator} MemoryStore setting data without type guard - you should probably configure one`}),!0}async delete(e){return this.isReady()||this.initialize(),this.updateLastAccessedAt(),this.lockManager.withLock(e,async()=>{if(this.dispose){let t=this.dataStore.get(e);await this.dispose(e,t)}return this.dataStore.delete(e)})}async pruneExpiredKeys(){let e=this.dataStore.size,t=e>=this.truncateThreshold;if(e<=0)return;let n=[],r=0;this.dataStore.forEach(async(e,i)=>{let a=Date.now(),o=this.expiryMap.get(i)??0,s=this.truncateThreshold*this.truncationPercentage/oe;(o<=a||t&&r>=0&&r<=s)&&n.push(this.lockManager.withLock(i,async()=>(this.dispose&&await this.dispose(i,e),this.dataStore.delete(i)))),r++}),await Promise.all(n);let i=this.dataStore.size;return{dataStoreSize:e,prunedDataStoreSize:i}}startEvictionTask(){if(u(this.evictionInterval))return;let e=async()=>{let t;try{let n=this.lastAccessedAt+this.staleDataThreshold,r=Date.now();if(n<r){this.logger?.warning({message:`Closing the ${this.instantiator}'s MemoryStore instance - received no requests for a while.`,category:`MemoryStore`}),this.close();return}t=await this.pruneExpiredKeys(),this.evictionInterval=setTimeout(e,this.evictionFrequency)}catch(t){t instanceof Error&&this.logger?.error({message:`Error during pruning expired keys:`,category:`MemoryStore`,error:t,code:me.MemoryStorePruneError}),this.evictionInterval=setTimeout(e,this.evictionFrequency)}finally{if(u(t?.dataStoreSize)&&u(t?.prunedDataStoreSize)){let{dataStoreSize:e,prunedDataStoreSize:n}=t,r=e-n;this.logger?.debug({message:`Pruned ${r} expired keys, ${n} remain, scheduling next prune.`,category:`MemoryStore`,context:{instantiator:this.instantiator}})}}};this.evictionInterval=setTimeout(e,this.evictionFrequency)}stopEvictionTask(){this.evictionInterval&&(clearTimeout(this.evictionInterval),this.evictionInterval=void 0)}updateLastAccessedAt(){this.lastAccessedAt=Date.now()}isReady(){return u(this.evictionInterval)&&u(this.dataStore)&&u(this.expiryMap)&&u(this.lockManager)}close(){this.stopEvictionTask(),this.dataStore.clear(),this.expiryMap.clear(),this.lockManager.close()}async listData({partialKey:e,cursor:t,limit:n}){let r=Array.from(this.dataStore.keys()),i=r.filter(t=>t.includes(e)),a=[],o=t?parseInt(t,10):0;for(let e=o;e<n+o;e++){let t=i[e];if(!t)break;let r=await this.getData(t);if(r&&a.push(r),a.length>=n)break}return{items:a,cursor:a.length<n?void 0:(o+n).toString()}}};const ge=1e3,_e=6e4;let ve=function(e){return e.EventClientResolveError=`EventClientResolveError`,e}({});var ye=class{executorMethodStore=null;promiseStore=null;logger;eventClientConfig=null;executorMethodStoreConfig=null;pendingPromiseStoreConfig=null;constructor(e,t,n){this.eventClientConfig=e,this.executorMethodStoreConfig=t,this.pendingPromiseStoreConfig=n;let r=e?.instantiator?`${e?.instantiator}(${this.constructor.name})`:this.constructor.name,i={logger:this.logger,dispose:this.defaultExecutorMethodDispose,typeGuard:this.defaultExecutorMethodTypeGuard,...this.executorMethodStoreConfig??{},instantiator:r},a={logger:this.logger,dispose:this.defaultPendingPromiseDispose,typeGuard:this.defaultPendingPromiseTypeGuard,...this.pendingPromiseStoreConfig??{},instantiator:r};this.executorMethodStore=new he(i),this.promiseStore=new he(a)}async setPendingEvent(e,t){let n={resolve:void 0,reject:void 0},r=new Promise((e,t)=>{n.resolve=e,n.reject=t}),i=t/ge;await this.executorMethodStore?.setData({key:e,value:n,cacheTTL:i}),await this.promiseStore?.setData({key:e,value:r,cacheTTL:i})}async waitForEvent(e,t){let n=await this.promiseStore?.getData(e);return u(n)?n:(await this.setPendingEvent(e,t),this.getPendingEvent(e))}async getPendingEvent(e){return this.promiseStore?.getData(e)??null}async deleteEvent(e){return(await Promise.all([this.promiseStore?.delete(e),this.executorMethodStore?.delete(e)])).every(Boolean)}async resolveEvent(e,t){try{let n=await this.executorMethodStore?.getData(e);o(n)&&(await this.setPendingEvent(e,this.eventClientConfig?.defaultTimeoutMS??_e),n=await this.executorMethodStore?.getData(e)),n?.resolve?.(t)}catch(t){t instanceof Error&&this.logger.error({message:`Error handling event for key ${e}: ${t.message}`,category:this.constructor.name,context:{eventKey:e},error:t,code:ve.EventClientResolveError})}}async defaultExecutorMethodDispose(e,t){u(t?.resolve)?t.resolve(this.eventClientConfig?.timeoutResolveValue):u(t?.reject)&&t.reject(Error(`Event key: ${e} was not resolved or the event was disposed`))}defaultExecutorMethodTypeGuard(e){return c(e)&&e.hasOwnProperty(`resolve`)&&e.hasOwnProperty(`reject`)}async defaultPendingPromiseDispose(e,t){t instanceof Promise&&t?.finally(()=>{})}defaultPendingPromiseTypeGuard(e){return e instanceof Promise}},be=class{instance=null;initPromise=null;retryTimeout=null;status=`uninitialized`;initOptions=null;initArgs=[];getRetryDelay(){return 5e3}getSingleton(e){throw Error(`getSingleton method not implemented`)}async getInstance(e,...t){return this.instance?this.instance:(o(this.initPromise)&&(this.initOptions=e??null,this.initArgs=t,this.initPromise=this.init(e??null,...t)),this.initPromise)}getInstanceIfReady(){return this.instance}isReady(){return this.status===`ready`}hasInitFailed(){return this.status===`failed`}async init(e,...t){try{let n=await this.initInstance(e,...t);return this.instance=n,this.status=`ready`,n}catch(e){throw this.status=`failed`,this.initPromise=null,o(this.retryTimeout)&&(this.retryTimeout=setTimeout(()=>{this.retryTimeout=null,this.getInstance(this.initOptions,...this.initArgs).catch(()=>{})},this.getRetryDelay())),e}}reset(){this.instance=null,this.initPromise=null,this.initOptions=null,this.status=`uninitialized`,this.retryTimeout&&clearTimeout(this.retryTimeout),this.retryTimeout=null}};let j=function(e){return e.RedisClientError=`RedisClientError`,e.RedisClientCommandError=`RedisClientCommandError`,e.RedisClientDeleteError=`RedisClientDeleteError`,e.RedisClientInvalidTTL=`RedisClientInvalidTTL`,e.RedisClientListError=`RedisClientListError`,e.RedisClientNotInitialized=`RedisClientNotInitialized`,e.RedisClientPublishError=`RedisClientPublishError`,e.RedisClientReadError=`RedisClientReadError`,e.RedisClientRedisNotReady=`RedisClientRedisNotReady`,e.RedisClientScriptError=`RedisClientScriptError`,e.RedisClientScriptExecuteError=`RedisClientScriptExecuteError`,e.RedisClientScriptLoadError=`RedisClientScriptLoadError`,e.RedisClientSubscribeError=`RedisClientSubscribeError`,e.RedisClientUnsubscribeError=`RedisClientUnsubscribeError`,e.RedisClientWriteError=`RedisClientWriteError`,e}({});const xe=async(e,t)=>{try{let n=await Se.build(e,t);return n??void 0}catch(e){let n=e;t?.error({message:`Error building Cache Manager`,error:n,code:j.RedisClientError,category:`buildRedisClientInstance`});return}};var Se=class e{#redisClient;#logger;static async build({getRedisClient:t=p.createClient,host:n,port:r,tls:i,reconnect:a=!0,database:o},s){let c=new e;c.#logger=s;try{c.#redisClient=t({socket:{reconnectStrategy:a?e=>Math.min(e*20,5e3):!1,host:n,port:r,tls:i},database:o,disableOfflineQueue:!0});let s=we(e.name,c.#logger);return Ce(c.#redisClient,s),await c.#redisClient.connect(),c}catch(t){let n=t;return c.#logger?.error({message:`Error building Cache Manager.`,error:n,code:j.RedisClientError,category:e.name}),null}}#parseData(e){try{return JSON.parse(e)}catch{return e}}async getData(t){if(!this.#isRedisClientAvailable())return null;try{let e=await this.#redisClient.get(t);if(!e)return null;let n=this.#parseData(e);return l(n)?e:n}catch(t){let n=t;return this.#logger?.error({message:`Error getting data`,error:n,code:j.RedisClientReadError,category:e.name}),null}}async setData({key:t,value:n,cacheTTL:r,groupKey:i}){if(!this.#isRedisClientAvailable())return!1;try{let e=l(n)?n:JSON.stringify(n);return await this.#redisClient.set(t,e,{EX:r}),u(i)&&await this.#redisClient.sAdd(i,[t]),!0}catch(t){let n=t;return this.#logger?.error({message:`Error setting data`,error:n,code:j.RedisClientWriteError,category:e.name}),!1}}async executeScript({sha1:t,keys:n,args:r}){if(!this.#isRedisClientAvailable())return null;try{return await this.#redisClient.evalSha(t,{keys:n,arguments:r})}catch(t){let n=t;return this.#logger?.error({message:`Error executing script`,error:n,code:j.RedisClientScriptExecuteError,category:e.name}),null}}async loadScript(t){if(!this.#isRedisClientAvailable())return null;try{return await this.#redisClient.scriptLoad(t)}catch(t){let n=t;return this.#logger?.error({message:`Error loading script`,error:n,code:j.RedisClientScriptLoadError,category:e.name}),null}}async#executeCounterCommand(t,n,r){if(!this.#isRedisClientAvailable())return null;if(!Number.isFinite(r)||r<=0)return this.#logger?.error({message:`Invalid cacheTTL parameter`,category:e.name,code:j.RedisClientInvalidTTL}),null;try{let e=[{args:[t,n]},{args:[`expire`,n,r.toString()]}],i=await this.#redisClient.multiExecutor(e),a=i?.[0];return typeof a==`number`?a:null}catch(n){let r=n;return this.#logger?.error({message:`Error executing ${t} operation`,error:r,code:j.RedisClientCommandError,category:e.name}),null}}async increment(e,t){return this.#executeCounterCommand(`incr`,e,t)}async decrement(e,t){return this.#executeCounterCommand(`decr`,e,t)}async subscribe(t,n){if(!this.#isRedisClientAvailable())return!1;try{return await this.#redisClient.pSubscribe(t,n),!0}catch(n){let r=n;return this.#logger?.error({message:`Error subscribing to ${t}`,error:r,code:j.RedisClientSubscribeError,category:e.name}),!1}}async unsubscribe(t){if(!this.#isRedisClientAvailable())return!1;try{return await this.#redisClient.pUnsubscribe(t),!0}catch(n){let r=n;return this.#logger?.error({message:`Error unsubscribing from ${t}`,error:r,code:j.RedisClientUnsubscribeError,category:e.name}),!1}}async publish(t,n){if(!this.#isRedisClientAvailable())return null;try{let r=await this.#redisClient.publish(t,n);return r===0&&this.#logger?.debug({message:`No subscribers found for channel ${t}`,category:e.name}),r}catch(n){let r=n;return this.#logger?.error({message:`Error publishing to ${t}`,error:r,code:j.RedisClientPublishError,category:e.name}),null}}getClient(){return this.#isRedisClientAvailable()?this.#redisClient:null}#isRedisClientAvailable(){let t=this.#redisClient.isReady&&this.#redisClient.isOpen;return t||this.#logger?.error({message:`Redis Client is not ready.`,category:e.name,code:j.RedisClientRedisNotReady}),t}async listData({partialKey:t,limit:n,cursor:r}){try{let e=r?parseInt(r,10):0,i=await this.#redisClient.sScan(t,e,{COUNT:n}),a=[];for(let e of i.members){let t=await this.getData(e);u(t)&&a.push(t)}return{items:a,cursor:i.cursor>0?i.cursor.toString():void 0}}catch(t){let n=t;return this.#logger?.error({message:`Error listing data`,error:n,code:j.RedisClientListError,category:e.name}),{items:null}}}async deleteData(t){try{if(!this.#isRedisClientAvailable())return!1;let n=await this.#redisClient.del(t);return n===0&&this.#logger?.debug({message:`No keys deleted for ${t}`,category:e.name}),n>0}catch(t){let n=t;return this.#logger?.error({message:`Error deleting data`,error:n,code:j.RedisClientDeleteError,category:e.name}),!1}}};const Ce=(e,t)=>{t.forEach(({eventName:t,listener:n})=>{e.on(t,n)})},we=(e=`RedisClient`,t)=>[{eventName:`error`,listener:n=>{t?.error({message:`Redis client error ${n.message}`,error:n,code:j.RedisClientError,category:e})}},{eventName:`ready`,listener:()=>{t?.info({message:`Redis client ready`,category:e})}},{eventName:`connect`,listener:()=>{t?.info({message:`Redis client connected`,category:e})}},{eventName:`reconnecting`,listener:()=>{t?.info({message:`Redis client reconnecting`,category:e})}},{eventName:`end`,listener:()=>{t?.info({message:`Redis client disconnected`,category:e})}}];let Te=function(e){return e.ScriptManagerExecutionError=`ScriptManagerExecutionError`,e.ScriptManagerLoadingError=`ScriptManagerLoadingError`,e}({});var Ee=class extends be{redisClientConfig;scripts;scriptMap;cacheClient;logger;additionalArgs;isInitialConnection=!0;async initInstance({redisClientConfig:e=this.redisClientConfig,cacheClient:t=this.cacheClient,scripts:n=this.scripts,scriptMap:r=this.scriptMap,logger:i=this.logger,additionalArgs:a=this.additionalArgs}){let s=t??await xe(e,i);if(o(s))throw Error(`Failed to build Redis client`);return this.redisClientConfig=e,this.logger=i,this.cacheClient=s,this.scripts=n,this.scriptMap=r??new Map,this.additionalArgs=a??[],await this.loadScripts(n,r),this.setupReconnectHandler(),await this.additionalInitialization(...a??[]),this}setupReconnectHandler(){let e=this.cacheClient?.getClient?.();u(e)&&(e.on(`ready`,async()=>{this.isInitialConnection?this.isInitialConnection=!1:(this.logger?.info({message:`Redis reconnected - reloading scripts`,category:this.constructor.name}),await this.reloadScripts())}),e.on(`end`,()=>{this.logger?.info({message:`Redis connection closed`,category:this.constructor.name})}))}async reloadScripts(){o(this.scripts)||Object.keys(this.scripts).length===0||(this.logger?.info({message:`Reloading ${Object.keys(this.scripts).length} scripts after Redis reconnection`,category:this.constructor.name}),this.scriptMap.clear(),await this.loadScripts(this.scripts))}async loadScripts(e,t){if(u(t))for(let[e,n]of t)this.scriptMap.set(e,n);for(let[t,n]of Object.entries(e)){let e=await this.cacheClient?.loadScript?.(n);u(e)?this.scriptMap?.set(t,e):this.logger?.error({message:`Error loading script ${t}`,category:this.constructor.name,code:Te.ScriptManagerLoadingError})}}async executeScript(e,t,n){try{if(!this.isRedisReady())throw Error(`Redis client is not ready`);let r=this.scriptMap?.get(e);if(o(r))throw Error(`Script for ${e} not found`);try{return await this.cacheClient?.executeScript?.({sha1:r,keys:t,args:n})??null}catch(i){if(i instanceof Error&&i.message.includes(`NOSCRIPT`)){if(this.logger?.info({message:`Script ${e} not found in Redis, reloading scripts`,category:this.constructor.name}),await this.reloadScripts(),r=this.scriptMap?.get(e),o(r))throw Error(`Script for ${e} could not be reloaded`);return await this.cacheClient?.executeScript?.({sha1:r,keys:t,args:n})??null}throw i}}catch(r){throw r instanceof Error&&this.logger?.error({message:`Error executing script ${e}: ${r.message}`,category:this.constructor.name,context:{keys:t,args:n},error:r,code:Te.ScriptManagerExecutionError}),r}}isRedisReady(){return this.isReady()&&u(this.cacheClient)}},De=class{options={getCacheClient:xe};subscriptionMap=null;subscriptionClient=null;logger;name=`SubscriptionManager`;constructor(e){this.options={...this.options,...e}}async initialize(e=this.options){let{config:t,getCacheClient:n,logger:r}=e;if(this.logger=r,this.subscriptionMap=new he({instantiator:`${e?.instantiator}(${this.name})`,evictionFrequency:e?.subscriptionTTL,staleDataThreshold:e?.staleSubscriptionsThreshold,truncateThreshold:e?.truncateThreshold,truncationPercentage:e?.truncationPercentage,typeGuard:e=>typeof e==`string`,dispose:async e=>{await this.subscriptionClient?.unsubscribe?.(e)}}),u(t))this.subscriptionClient=await n?.(t,this.logger)??null,this.logger?.info({message:`${this.name} initialized for ${e.instantiator}.`,category:this.name});else throw Error(`SubscriptionManager requires a Redis client configuration.`)}async subscribe(e,t){this.isReady()||await this.initialize(this.options);let n=await this.subscriptionMap?.getData(e),i=r(t.toString());return await this.subscriptionMap?.setData({key:e,value:i}),n===i?!0:await this.subscriptionClient?.subscribe?.(e,t)??!1}async unsubscribe(e){return this.isReady()||await this.initialize(this.options),this.subscriptionClient?.unsubscribe?.(e)??!1}isReady(){return u(this.subscriptionClient)&&u(this.subscriptionMap)}close(){this.subscriptionMap?.close(),this.subscriptionMap=null,this.subscriptionClient=null}};const Oe=6e4,ke=15e3,Ae=!1,je=6e4,Me=15e3,Ne=1e4,Pe=1;let Fe=function(e){return e.QueueManagerInitializationError=`QueueManagerInitializationError`,e.QueueManagerJoinAndWaitTurnError=`QueueManagerJoinAndWaitTurnError`,e.QueueManagerLengthError=`QueueManagerLengthError`,e.QueueManagerPopError=`QueueManagerPopError`,e}({}),Ie=function(e){return e.rPush=`rPush`,e.lPush=`lPush`,e.lPop=`lPop`,e.llen=`llen`,e}({});var Le=class extends Ee{subscriptionManager=null;eventClient=null;name=`QueueManager`;async additionalInitialization(){if(o(this.redisClientConfig))throw Error(`Redis client configuration is required to initialize QueueManager.`);this.subscriptionManager=new De({config:this.redisClientConfig,instantiator:this.name,subscriptionTTL:je,truncateThreshold:Ne,truncationPercentage:Pe}),await this.subscriptionManager?.initialize(),this.eventClient=new ye({instantiator:this.name,timeoutResolveValue:Ae},{instantiator:this.name,evictionFrequency:Me,truncateThreshold:Ne,truncationPercentage:Pe},{instantiator:this.name,evictionFrequency:Me,truncateThreshold:Ne,truncationPercentage:Pe}),this.logger?.info({category:this.name,message:`QueueManager initialized.`})}async joinAndWaitTurn(e,t,n,r,i=!0){try{let a=`${e}:${t}`;await this.subscriptionManager?.subscribe(a,async(n,r)=>{if(r.includes(a)&&r.includes(t)){let t=`${e}:${n}`;return this.eventClient?.resolveEvent(t,!0)}});let o=await this.executeScript(n?Ie.lPush:Ie.rPush,[e],[t,Oe.toString()]),s=o===1,c=i?!s:!0;return c&&await this.eventClient?.waitForEvent(a,ke),await this.eventClient?.deleteEvent(a),await r(e,t)}catch(n){throw n instanceof Error&&this.logger?.error({message:`Error in joinAndWaitTurn`,category:this.name,error:n,code:Fe.QueueManagerJoinAndWaitTurnError,context:{queueName:e,value:t}}),n}}async pop(e){try{return await this.executeScript(Ie.lPop,[e],[])}catch(t){throw t instanceof Error&&this.logger?.error({message:`Error in pop`,category:this.name,error:t,code:Fe.QueueManagerPopError,context:{queueName:e}}),t}}async length(e){try{return await this.executeScript(Ie.llen,[e],[])}catch(t){throw t instanceof Error&&this.logger?.error({message:`Error in length`,category:this.name,error:t,code:Fe.QueueManagerLengthError,context:{queueName:e}}),t}}isReady(){return u(this.subscriptionManager)&&u(this.cacheClient)&&u(this.scriptMap)&&this.scriptMap.size>0}close(){this.subscriptionManager?.close(),this.scriptMap?.clear()}};const Re=5,ze=12e4,Be=1e4,Ve=1,He=60,Ue=90,We=`__keyevent@0__:`,Ge=`${We}set`,Ke=`${We}expire`,qe=`${We}expired`,Je=`${We}srem`,Ye=`config_check_test_key`,Xe=[Ge,Ke,qe],Ze=1,Qe=1e4,$e={mainMaxConcurrency:30};let et=function(e){return e.ConcurrencyManagerInitializationError=`ConcurrencyManagerInitializationError`,e.ConcurrencyManagerRedisEventsEmitError=`ConcurrencyManagerRedisEventsEmitError`,e.ConcurrencyManagerRegistrationFailed=`ConcurrencyManagerRegistrationFailed`,e}({}),tt=function(e){return e.tryConcurrency=`tryConcurrency`,e.removeFromSet=`removeFromSet`,e.publishNextItem=`publishNextItem`,e}({});const nt={[tt.tryConcurrency]:`
|
|
2
2
|
local requestId = KEYS[1]
|
|
3
3
|
local targetKey = KEYS[2]
|
|
4
4
|
local queueKey = KEYS[3]
|
|
@@ -98,7 +98,7 @@ import{delay as e,exponentialBackoffInMS as t,generateRequestId as n,getContentH
|
|
|
98
98
|
|
|
99
99
|
redis.call('publish', nextItemKey, nextItem)
|
|
100
100
|
end
|
|
101
|
-
`};var rt=class r extends Ee{hasValidRedisConfig=!1;subscriptionManager=null;queueManager=null;generateUUID;name=`ConcurrencyManager`;async additionalInitialization(e){if(this.generateUUID=e??n,this.subscriptionManager=new De({config:this.redisClientConfig,logger:this.logger,instantiator:this.name,subscriptionTTL:ze,truncateThreshold:Be,truncationPercentage:Ve}),await this.subscriptionManager?.initialize(),this.hasValidRedisConfig=await this.checkRedisEventsEmit(),!this.hasValidRedisConfig)throw Error(`Failed to validate Redis configuration for ConcurrencyManager`);if(o(this.getSingleton))throw Error(`getSingleton method not available`);this.queueManager=this.getSingleton?.(Le)}async addTestSubscription(e,t,n,i){return o(this.subscriptionManager)?!1:this.subscriptionManager.subscribe(e,async(a,o)=>{o===e&&a.includes(t)&&(n.push(o),n.includes(Ge)&&n.includes(Ke)&&n.includes(qe)&&(i(),this.logger?.info({category:r.name,message:`Redis events are being emitted.`,context:{receivedEventCount:n.length}}),n.length=0))})}async checkRedisEventsEmit(){let e=[],t=async(t,n)=>{let r=()=>t(!0),i=this.generateUUID(),a=`${Ye}:${i}`,o=(await Promise.all(Xe.map(async t=>this.addTestSubscription(t,a,e,r)))).every(Boolean);if(!o)return n(Error(`Failed to subscribe to event channels.`));await this.cacheClient?.setData({key:a,value:`test`,cacheTTL:Ze}),setTimeout(()=>{n(Error(`Timeout waiting for Redis events to emit.`))},Qe)};try{return await new Promise(t)}catch(t){return this.logger?.error({category:r.name,message:`Redis events failed to emit.`,context:{receivedEventCount:e.length,reason:t?.message},error:t,code:et.ConcurrencyManagerRedisEventsEmitError}),!1}}isRedisConfigured(){return this.hasValidRedisConfig}async subscribeToLeaseExpiry(e,t){let n=await this.subscriptionManager?.subscribe(qe,async(n,r)=>{r===qe&&n.includes(t)&&n.includes(e)&&await this.releaseRequest(e,t)});return n??!1}async subscribeToSetRemoval(e,t){let n=await this.subscriptionManager?.subscribe(Je,async(n,r)=>{r===Je&&n.includes(e)&&await this.executeScript(tt.publishNextItem,[t],[])});return n??!1}async registerRequest(n,i,a,o){let s=`concurrency:${n}`,c=`queue:${n}`,l=i.subPools?.find(e=>e.urlPattern instanceof RegExp?e.urlPattern.test(o??``):o?.includes(e.urlPattern))?.subPoolKey,d=u(l)?`${s}-${l}`:s,{mainMaxConcurrency:f,subPools:p}=i,m=[{key:s,maxConcurrency:f},...p?.map(e=>{let{subPoolKey:t,maxConcurrency:n}=e;return{key:`${s}-${t}`,maxConcurrency:n}})??[]],h=m.map(e=>e.key),g=m.map(e=>e.maxConcurrency),[_,v]=await Promise.all([this.subscribeToLeaseExpiry(a,d),this.subscribeToSetRemoval(d,c)]),y={requestId:a,targetConcurrencyKey:d,leaseSubscription:_,setRemovalSubscription:v,queueKey:c},[b,x]=await this.executeScript(tt.tryConcurrency,[a,d,c,...h],[He.toString(),f.toString(),Ue.toString(),...g.map(String)])??[];if(b)return{...y,joinCondition:`optimistic`,failureReason:x};let S=0,C=[];for(;S<Re;){let n=S>1,[i,o]=await this.queueManager?.joinAndWaitTurn(c,a,n,async(e,t)=>await this.executeScript(tt.tryConcurrency,[a,d,c,...h],[He.toString(),f.toString(),Ue.toString(),...g.map(String)]),!1)??[!1,``];if(u(o)&&o!==``&&C.push(o),S++,i)return S>1&&this.logger?.warning({category:r.name,message:`Register request succeeded after ${S} attempts.`,context:{...y,registerAttempts:S,joinCondition:`attempts`,failureMessages:C}}),{...y,joinCondition:`attempts`};let s=t(S,100);await e(s)}return this.logger?.error({category:r.name,message:`Failed to register request after ${S} attempts.`,context:{...y,registerAttempts:S,joinCondition:`failed`,failureMessages:C},code:et.ConcurrencyManagerRegistrationFailed}),{...y,joinCondition:`failed`}}async releaseRequest(e,t){return l(t)&&l(e)?!!await this.executeScript(tt.removeFromSet,[t],[e]):!1}isReady(){return this.subscriptionManager!==null&&this.cacheClient!==null&&this.scriptMap!==null&&this.scriptMap.size>0&&this.hasValidRedisConfig}close(){this.subscriptionManager?.close(),this.queueManager?.close(),this.scriptMap?.clear(),this.hasValidRedisConfig=!1}};const it=f({receivedStatus:d.number(),targetStatus:d.number(),message:d.string().optional(),condition:d.string().optional()}),at={100:`Continue`,101:`Switching Protocols`,102:`Processing`,200:`OK`,201:`Created`,202:`Accepted`,203:`Non-Authoritative Information`,204:`No Content`,205:`Reset Content`,206:`Partial Content`,207:`Multi-Status`,208:`Already Reported`,226:`IM Used`,300:`Multiple Choices`,301:`Moved Permanently`,302:`Found`,303:`See Other`,304:`Not Modified`,305:`Use Proxy`,307:`Temporary Redirect`,308:`Permanent Redirect`,400:`Bad request`,401:`Unauthorized`,402:`Payment Required`,403:`Forbidden`,404:`Not Found`,405:`Method Not Allowed`,406:`Not Acceptable`,407:`Proxy Authentication Required`,408:`Request Timeout`,409:`Conflict`,410:`Gone`,411:`Length Required`,412:`Precondition Failed`,413:`Payload Too Large`,414:`URI Too Long`,415:`Unsupported Media Type`,416:`Range Not Satisfiable`,417:`Expectation Failed`,418:`I'm a teapot`,421:`Misdirected Request`,422:`Unprocessable Entity`,423:`Locked`,424:`Failed Dependency`,425:`Too Early`,426:`Upgrade Required`,428:`Precondition Required`,429:`Too many Requests`,431:`Request Header Fields Too Large`,451:`Unavailable For Legal Reasons`,499:`Client Closed Request`,500:`Internal server error`,501:`Not Implemented`,502:`Bad Gateway`,503:`Unavailable`,504:`Gateway Timeout`,505:`HTTP Version Not Supported`,506:`Variant Also Negotiates`,507:`Insufficient Storage`,508:`Loop Detected`,510:`Not Extended`,511:`Network Authentication Required`};var ot=class e extends Error{response;constructor(t,n){super(n),this.name=`HttpResponseError`,this.response=t,this.message=n??`HTTP error: ${t.status}`,Error.captureStackTrace&&Error.captureStackTrace(this,e)}toString(){let e=this.message?`: ${this.message}`:``;return`${this.name} [${this.response.status}]${e}`}};const st=e=>e instanceof Error&&e.name===`HttpResponseError`&&`response`in e&&typeof e.response==`object`&&e.response!==null,ct=(e,t=new WeakSet)=>{if(typeof e!=`object`||!e)return typeof e==`bigint`?e.toString():e;if(t.has(e))return`[Circular Reference]`;if(e instanceof Date)return e.toISOString();if(Array.isArray(e)){t.add(e);let n=e.map(e=>ct(e,t));return t.delete(e),n}let n=e,r={};t.add(n);for(let[e,i]of Object.entries(n))r[e]=ct(i,t);return t.delete(n),r},lt=e=>{if(st(e)){let t=e.response;return{name:e.name,message:e.message,stack:e.stack,response:ct(t)}}return e instanceof Error?{name:e.name,message:e.message,stack:e.stack}:{error:String(e)}};var ut=class{static createInstance({interceptors:e,instanceConfig:t,httpsAgentConfig:n,logger:r,context:i,requestConfig:a,concurrencyManager:o,rateLimitManager:s}){let c=new g.Agent(n),l=m.create({...t,httpsAgent:c});if(e){let t={axiosInstance:l,logger:r,context:i,requestConfig:a,concurrencyManager:o,rateLimitManager:s};this.applyInterceptors(l,e,t)}return l}static applyInterceptors(e,t,n){let{requestConfigs:r=[],responseConfigs:i=[]}=t,a=[{type:`request`,configs:r},{type:`response`,configs:i}];a.forEach(t=>{let{configs:r,type:i}=t;r.forEach(t=>{let{onFulfilled:r,onRejected:a,options:o}=t;if(i===`request`){let t=[this.resolveInterceptorInstance(r,n),this.resolveInterceptorInstance(a,n),o];e.interceptors.request.use(...t)}if(i===`response`){let t=[this.resolveInterceptorInstance(r,n),this.resolveInterceptorInstance(a,n)];e.interceptors.response.use(...t)}})})}static resolveInterceptorInstance(e,t){return u(e)&&i(e)&&typeof e(t)==`function`?e(t):e??null}};const dt=300;var ft=class e{static instance=null;static logger=null;dataStore=null;constructor(e){this.initialize(e)}static getInstance(t){return o(e.instance)&&(e.instance=new e(t)),e.instance}initialize(t){this.dataStore=new he({instantiator:e.name,logger:t,typeGuard:e=>u(e)}),e.logger=t??null,e.logger?.info({category:e.name,message:`InstanceManager initialized.`})}async get(t){return this.ensureReady(),await e.getInstance().dataStore?.getData(t)??null}async set(t,n,r){return this.ensureReady(),await e.getInstance().dataStore?.setData({key:t,value:n,cacheTTL:u(r)?r:dt})??!1}ensureReady(){this.isReady()||this.initialize()}isReady(){return u(this?.dataStore)}close(){e.instance=null,this.dataStore=null}};const pt=ft.getInstance(),mt=()=>{let e=e=>{let{response:t}=e;return e?.code===`ERR_CANCELED`&&(e.response={...t,status:499,statusText:`Request aborted`}),Promise.reject(e)};return e},ht=7.5,gt={mainRatelimit:25};let _t=function(e){return e.incr=`incr`,e}({}),vt=function(e){return e.RateLimiterReleaseRequestFailed=`RateLimiterReleaseRequestFailed`,e.RateLimitOrConcurrencyManagerNotInitialized=`RateLimitOrConcurrencyManagerNotInitialized`,e.InterceptorContextNotPresent=`InterceptorContextNotPresent`,e.InterceptorMaxWaitTimeExceeded=`InterceptorMaxWaitTimeExceeded`,e}({});const yt=(e,t,n,r)=>{let i=Error(e);return i.config={...t,headers:{}},i.code=n,i.response=r,i.isAxiosError=!0,i},bt=(e,t,n)=>{let r=t.signal;return r?r.aborted?Promise.reject(yt(`Request aborted`,t,`ERR_CANCELED`,n)):new Promise((i,a)=>{let o=!1,s,c=yt(`Request aborted`,t,`ERR_CANCELED`,n),l=()=>{if(!o){o=!0,s&&v(s);try{r.removeEventListener?.(`abort`,u)}catch{}}},u=()=>{l(),a(c)},d=()=>{l(),i()};try{r.addEventListener?.(`abort`,u,{once:!0})}catch{s=setTimeout(()=>{i()},e);return}if(r.aborted){l(),a(c);return}s=setTimeout(d,e)}):new Promise(t=>setTimeout(t,e))},xt=`checkConcurrencyInterceptor`,St=({logger:e,context:t,requestConfig:n,concurrencyManager:r,rateLimitManager:i})=>{let a=async a=>{if(a.signal?.aborted)return Promise.reject(yt(`Request aborted`,a,`ERR_CANCELED`));if(o(t))e?.warning({category:`HttpClient`,message:`No context for this request - that doesn't seem right.`,context:{interceptor:xt,...a,headers:void 0,hasContext:u(t),hasConfig:u(a)},code:vt.InterceptorContextNotPresent});else if(o(i)||o(r))e?.error({category:`HttpClient`,message:`RateLimitManager or ConcurrencyManager is not initialized`,context:{interceptor:xt,...a,headers:void 0,rateLimitManagerInitialized:u(i),concurrencyManagerInitialized:u(r)},code:vt.RateLimitOrConcurrencyManagerNotInitialized});else if(u(t.provider)&&u(t.accountSecureId)){let o=_(),{provider:s,accountSecureId:c}=t,l=n?.rateLimits??gt,u=n?.concurrency??$e,d=await i.getDynamicMaxWaitTime(l,u,a.url),f=async()=>r.registerRequest(`${c}-${s}`,u,o,a.url),p,m=0;for(;m<=d;){let n=Math.floor(Date.now()/1e3),r=await i.getWaitTime(`${c}-${s}`,l,a.url)??0,o=Math.max(r-n,0);if(r===0){p=await f();break}if(await bt(o*1e3,a),m+=o,m>=d){p=await f(),e?.warning({category:`HttpClient`,message:`Max Wait Time Exceeded for ${s} - Account: ${c}`,context:{...t,interceptor:xt,...a,headers:void 0,dynamicMaxWaitTime:d},code:vt.InterceptorMaxWaitTimeExceeded});break}}return{...a,requestMetadata:p}}return a};return a},Ct=1,wt=10,Tt=5,Et=3250368e4,Dt=1e3,Ot=/^\d+(\.\d+)?$/,kt=1e3,At=e=>e<Et?e*Dt:e;var jt=O(((exports,t)=>{function n(){return t.exports=n=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)({}).hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},t.exports.__esModule=!0,t.exports.default=t.exports,n.apply(null,arguments)}t.exports=n,t.exports.__esModule=!0,t.exports.default=t.exports})),Mt={epsilon:1e-12,matrix:`Matrix`,number:`number`,precision:64,predictable:!1,randomSeed:null};function M(e){return typeof e==`number`}function N(e){return!e||typeof e!=`object`||typeof e.constructor!=`function`?!1:e.isBigNumber===!0&&typeof e.constructor.prototype==`object`&&e.constructor.prototype.isBigNumber===!0||typeof e.constructor.isDecimal==`function`&&e.constructor.isDecimal(e)===!0}function Nt(e){return e&&typeof e==`object`&&Object.getPrototypeOf(e).isComplex===!0||!1}function Pt(e){return e&&typeof e==`object`&&Object.getPrototypeOf(e).isFraction===!0||!1}function Ft(e){return e&&e.constructor.prototype.isUnit===!0||!1}function It(e){return typeof e==`string`}var P=Array.isArray;function Lt(e){return e&&e.constructor.prototype.isMatrix===!0||!1}function Rt(e){return Array.isArray(e)||Lt(e)}function zt(e){return e&&e.isDenseMatrix&&e.constructor.prototype.isMatrix===!0||!1}function Bt(e){return e&&e.isSparseMatrix&&e.constructor.prototype.isMatrix===!0||!1}function Vt(e){return e&&e.constructor.prototype.isRange===!0||!1}function Ht(e){return e&&e.constructor.prototype.isIndex===!0||!1}function Ut(e){return typeof e==`boolean`}function Wt(e){return e&&e.constructor.prototype.isResultSet===!0||!1}function Gt(e){return e&&e.constructor.prototype.isHelp===!0||!1}function Kt(e){return typeof e==`function`}function qt(e){return e instanceof Date}function Jt(e){return e instanceof RegExp}function Yt(e){return!!(e&&typeof e==`object`&&e.constructor===Object&&!Nt(e)&&!Pt(e))}function Xt(e){return e===null}function Zt(e){return e===void 0}function Qt(e){return e&&e.isAccessorNode===!0&&e.constructor.prototype.isNode===!0||!1}function $t(e){return e&&e.isArrayNode===!0&&e.constructor.prototype.isNode===!0||!1}function en(e){return e&&e.isAssignmentNode===!0&&e.constructor.prototype.isNode===!0||!1}function tn(e){return e&&e.isBlockNode===!0&&e.constructor.prototype.isNode===!0||!1}function nn(e){return e&&e.isConditionalNode===!0&&e.constructor.prototype.isNode===!0||!1}function rn(e){return e&&e.isConstantNode===!0&&e.constructor.prototype.isNode===!0||!1}function an(e){return e&&e.isFunctionAssignmentNode===!0&&e.constructor.prototype.isNode===!0||!1}function on(e){return e&&e.isFunctionNode===!0&&e.constructor.prototype.isNode===!0||!1}function sn(e){return e&&e.isIndexNode===!0&&e.constructor.prototype.isNode===!0||!1}function cn(e){return e&&e.isNode===!0&&e.constructor.prototype.isNode===!0||!1}function ln(e){return e&&e.isObjectNode===!0&&e.constructor.prototype.isNode===!0||!1}function un(e){return e&&e.isOperatorNode===!0&&e.constructor.prototype.isNode===!0||!1}function dn(e){return e&&e.isParenthesisNode===!0&&e.constructor.prototype.isNode===!0||!1}function fn(e){return e&&e.isRangeNode===!0&&e.constructor.prototype.isNode===!0||!1}function pn(e){return e&&e.isRelationalNode===!0&&e.constructor.prototype.isNode===!0||!1}function mn(e){return e&&e.isSymbolNode===!0&&e.constructor.prototype.isNode===!0||!1}function hn(e){return e&&e.constructor.prototype.isChain===!0||!1}function gn(e){var t=typeof e;return t===`object`?e===null?`null`:N(e)?`BigNumber`:e.constructor&&e.constructor.name?e.constructor.name:`Object`:t}function _n(e){var t=typeof e;if(t===`number`||t===`string`||t===`boolean`||e==null)return e;if(typeof e.clone==`function`)return e.clone();if(Array.isArray(e))return e.map(function(e){return _n(e)});if(e instanceof Date)return new Date(e.valueOf());if(N(e))return e;if(Yt(e))return vn(e,_n);throw TypeError(`Cannot clone: unknown type of value (value: ${e})`)}function vn(e,t){var n={};for(var r in e)bn(e,r)&&(n[r]=t(e[r]));return n}function yn(e,t){var n,r,i;if(Array.isArray(e)){if(!Array.isArray(t)||e.length!==t.length)return!1;for(r=0,i=e.length;r<i;r++)if(!yn(e[r],t[r]))return!1;return!0}else if(typeof e==`function`)return e===t;else if(e instanceof Object){if(Array.isArray(t)||!(t instanceof Object))return!1;for(n in e)if(!(n in t)||!yn(e[n],t[n]))return!1;for(n in t)if(!(n in e))return!1;return!0}else return e===t}function bn(e,t){return e&&Object.hasOwnProperty.call(e,t)}function xn(e,t){for(var n={},r=0;r<t.length;r++){var i=t[r],a=e[i];a!==void 0&&(n[i]=a)}return n}var Sn=[`Matrix`,`Array`],Cn=[`number`,`BigNumber`,`Fraction`],wn=k(jt(),1),Tn=function(e){if(e)throw Error(`The global config is readonly.
|
|
101
|
+
`};var rt=class r extends Ee{hasValidRedisConfig=!1;subscriptionManager=null;queueManager=null;generateUUID;name=`ConcurrencyManager`;async additionalInitialization(e){if(this.generateUUID=e??n,this.subscriptionManager=new De({config:this.redisClientConfig,logger:this.logger,instantiator:this.name,subscriptionTTL:ze,truncateThreshold:Be,truncationPercentage:Ve}),await this.subscriptionManager?.initialize(),this.hasValidRedisConfig=await this.checkRedisEventsEmit(),!this.hasValidRedisConfig)throw Error(`Failed to validate Redis configuration for ConcurrencyManager`);if(o(this.getSingleton))throw Error(`getSingleton method not available`);this.queueManager=this.getSingleton?.(Le)}async addTestSubscription(e,t,n,r){return o(this.subscriptionManager)?!1:this.subscriptionManager.subscribe(e,async(i,a)=>{a===e&&i.includes(t)&&(n.push(a),n.includes(Ge)&&n.includes(Ke)&&n.includes(qe)&&(r(),n.length=0))})}async checkRedisEventsEmit(){let e=[],t=Date.now(),n=async(n,i)=>{let a=()=>{let i=Date.now()-t;this.logger?.info({category:r.name,message:`All Redis events received successfully.`,context:{receivedEventCount:e.length,elapsedTimeMs:i}}),n(!0)},o=this.generateUUID(),s=`${Ye}:${o}`,c=(await Promise.all(Xe.map(async t=>this.addTestSubscription(t,s,e,a)))).every(Boolean);if(!c)return i(Error(`Failed to subscribe to event channels.`));await this.cacheClient?.setData({key:s,value:`test`,cacheTTL:Ze}),setTimeout(()=>{let e=Date.now()-t;i(Error(`Timeout waiting for Redis events to emit after ${e}ms.`))},Qe)};try{return await new Promise(n)}catch(n){let i=Date.now()-t;return this.logger?.error({category:r.name,message:`Redis events failed to emit.`,context:{receivedEventCount:e.length,elapsedTimeMs:i,reason:n?.message},error:n,code:et.ConcurrencyManagerRedisEventsEmitError}),!1}}isRedisConfigured(){return this.hasValidRedisConfig}async subscribeToLeaseExpiry(e,t){let n=await this.subscriptionManager?.subscribe(qe,async(n,r)=>{r===qe&&n.includes(t)&&n.includes(e)&&await this.releaseRequest(e,t)});return n??!1}async subscribeToSetRemoval(e,t){let n=await this.subscriptionManager?.subscribe(Je,async(n,r)=>{r===Je&&n.includes(e)&&await this.executeScript(tt.publishNextItem,[t],[])});return n??!1}async registerRequest(n,i,a,o){let s=`concurrency:${n}`,c=`queue:${n}`,l=i.subPools?.find(e=>e.urlPattern instanceof RegExp?e.urlPattern.test(o??``):o?.includes(e.urlPattern))?.subPoolKey,d=u(l)?`${s}-${l}`:s,{mainMaxConcurrency:f,subPools:p}=i,m=[{key:s,maxConcurrency:f},...p?.map(e=>{let{subPoolKey:t,maxConcurrency:n}=e;return{key:`${s}-${t}`,maxConcurrency:n}})??[]],h=m.map(e=>e.key),g=m.map(e=>e.maxConcurrency),[_,v]=await Promise.all([this.subscribeToLeaseExpiry(a,d),this.subscribeToSetRemoval(d,c)]),y={requestId:a,targetConcurrencyKey:d,leaseSubscription:_,setRemovalSubscription:v,queueKey:c},[b,x]=await this.executeScript(tt.tryConcurrency,[a,d,c,...h],[He.toString(),f.toString(),Ue.toString(),...g.map(String)])??[];if(b)return{...y,joinCondition:`optimistic`,failureReason:x};let S=0,C=[];for(;S<Re;){let n=S>1,[i,o]=await this.queueManager?.joinAndWaitTurn(c,a,n,async(e,t)=>await this.executeScript(tt.tryConcurrency,[a,d,c,...h],[He.toString(),f.toString(),Ue.toString(),...g.map(String)]),!1)??[!1,``];if(u(o)&&o!==``&&C.push(o),S++,i)return S>1&&this.logger?.warning({category:r.name,message:`Register request succeeded after ${S} attempts.`,context:{...y,registerAttempts:S,joinCondition:`attempts`,failureMessages:C}}),{...y,joinCondition:`attempts`};let s=t(S,100);await e(s)}return this.logger?.error({category:r.name,message:`Failed to register request after ${S} attempts.`,context:{...y,registerAttempts:S,joinCondition:`failed`,failureMessages:C},code:et.ConcurrencyManagerRegistrationFailed}),{...y,joinCondition:`failed`}}async releaseRequest(e,t){return l(t)&&l(e)?!!await this.executeScript(tt.removeFromSet,[t],[e]):!1}isReady(){return this.subscriptionManager!==null&&this.cacheClient!==null&&this.scriptMap!==null&&this.scriptMap.size>0&&this.hasValidRedisConfig}close(){this.subscriptionManager?.close(),this.queueManager?.close(),this.scriptMap?.clear(),this.hasValidRedisConfig=!1}};const it=f({receivedStatus:d.number(),targetStatus:d.number(),message:d.string().optional(),condition:d.string().optional()}),at={100:`Continue`,101:`Switching Protocols`,102:`Processing`,200:`OK`,201:`Created`,202:`Accepted`,203:`Non-Authoritative Information`,204:`No Content`,205:`Reset Content`,206:`Partial Content`,207:`Multi-Status`,208:`Already Reported`,226:`IM Used`,300:`Multiple Choices`,301:`Moved Permanently`,302:`Found`,303:`See Other`,304:`Not Modified`,305:`Use Proxy`,307:`Temporary Redirect`,308:`Permanent Redirect`,400:`Bad request`,401:`Unauthorized`,402:`Payment Required`,403:`Forbidden`,404:`Not Found`,405:`Method Not Allowed`,406:`Not Acceptable`,407:`Proxy Authentication Required`,408:`Request Timeout`,409:`Conflict`,410:`Gone`,411:`Length Required`,412:`Precondition Failed`,413:`Payload Too Large`,414:`URI Too Long`,415:`Unsupported Media Type`,416:`Range Not Satisfiable`,417:`Expectation Failed`,418:`I'm a teapot`,421:`Misdirected Request`,422:`Unprocessable Entity`,423:`Locked`,424:`Failed Dependency`,425:`Too Early`,426:`Upgrade Required`,428:`Precondition Required`,429:`Too many Requests`,431:`Request Header Fields Too Large`,451:`Unavailable For Legal Reasons`,499:`Client Closed Request`,500:`Internal server error`,501:`Not Implemented`,502:`Bad Gateway`,503:`Unavailable`,504:`Gateway Timeout`,505:`HTTP Version Not Supported`,506:`Variant Also Negotiates`,507:`Insufficient Storage`,508:`Loop Detected`,510:`Not Extended`,511:`Network Authentication Required`};var ot=class e extends Error{response;constructor(t,n){super(n),this.name=`HttpResponseError`,this.response=t,this.message=n??`HTTP error: ${t.status}`,Error.captureStackTrace&&Error.captureStackTrace(this,e)}toString(){let e=this.message?`: ${this.message}`:``;return`${this.name} [${this.response.status}]${e}`}};const st=e=>e instanceof Error&&e.name===`HttpResponseError`&&`response`in e&&typeof e.response==`object`&&e.response!==null,ct=(e,t=new WeakSet)=>{if(typeof e!=`object`||!e)return typeof e==`bigint`?e.toString():e;if(t.has(e))return`[Circular Reference]`;if(e instanceof Date)return e.toISOString();if(Array.isArray(e)){t.add(e);let n=e.map(e=>ct(e,t));return t.delete(e),n}let n=e,r={};t.add(n);for(let[e,i]of Object.entries(n))r[e]=ct(i,t);return t.delete(n),r},lt=e=>{if(st(e)){let t=e.response;return{name:e.name,message:e.message,stack:e.stack,response:ct(t)}}return e instanceof Error?{name:e.name,message:e.message,stack:e.stack}:{error:String(e)}};var ut=class{static createInstance({interceptors:e,instanceConfig:t,httpsAgentConfig:n,logger:r,context:i,requestConfig:a,concurrencyManager:o,rateLimitManager:s}){let c=new g.Agent(n),l=m.create({...t,httpsAgent:c});if(e){let t={axiosInstance:l,logger:r,context:i,requestConfig:a,concurrencyManager:o,rateLimitManager:s};this.applyInterceptors(l,e,t)}return l}static applyInterceptors(e,t,n){let{requestConfigs:r=[],responseConfigs:i=[]}=t,a=[{type:`request`,configs:r},{type:`response`,configs:i}];a.forEach(t=>{let{configs:r,type:i}=t;r.forEach(t=>{let{onFulfilled:r,onRejected:a,options:o}=t;if(i===`request`){let t=[this.resolveInterceptorInstance(r,n),this.resolveInterceptorInstance(a,n),o];e.interceptors.request.use(...t)}if(i===`response`){let t=[this.resolveInterceptorInstance(r,n),this.resolveInterceptorInstance(a,n)];e.interceptors.response.use(...t)}})})}static resolveInterceptorInstance(e,t){return u(e)&&i(e)&&typeof e(t)==`function`?e(t):e??null}};const dt=300;var ft=class e{static instance=null;static logger=null;dataStore=null;constructor(e){this.initialize(e)}static getInstance(t){return o(e.instance)&&(e.instance=new e(t)),e.instance}initialize(t){this.dataStore=new he({instantiator:e.name,logger:t,typeGuard:e=>u(e)}),e.logger=t??null,e.logger?.info({category:e.name,message:`InstanceManager initialized.`})}async get(t){return this.ensureReady(),await e.getInstance().dataStore?.getData(t)??null}async set(t,n,r){return this.ensureReady(),await e.getInstance().dataStore?.setData({key:t,value:n,cacheTTL:u(r)?r:dt})??!1}ensureReady(){this.isReady()||this.initialize()}isReady(){return u(this?.dataStore)}close(){e.instance=null,this.dataStore=null}};const pt=ft.getInstance(),mt=()=>{let e=e=>{let{response:t}=e;return e?.code===`ERR_CANCELED`&&(e.response={...t,status:499,statusText:`Request aborted`}),Promise.reject(e)};return e},ht=7.5,gt={mainRatelimit:25};let _t=function(e){return e.incr=`incr`,e}({}),vt=function(e){return e.RateLimiterReleaseRequestFailed=`RateLimiterReleaseRequestFailed`,e.RateLimitOrConcurrencyManagerNotInitialized=`RateLimitOrConcurrencyManagerNotInitialized`,e.InterceptorContextNotPresent=`InterceptorContextNotPresent`,e.InterceptorMaxWaitTimeExceeded=`InterceptorMaxWaitTimeExceeded`,e}({});const yt=(e,t,n,r)=>{let i=Error(e);return i.config={...t,headers:{}},i.code=n,i.response=r,i.isAxiosError=!0,i},bt=(e,t,n)=>{let r=t.signal;return r?r.aborted?Promise.reject(yt(`Request aborted`,t,`ERR_CANCELED`,n)):new Promise((i,a)=>{let o=!1,s,c=yt(`Request aborted`,t,`ERR_CANCELED`,n),l=()=>{if(!o){o=!0,s&&v(s);try{r.removeEventListener?.(`abort`,u)}catch{}}},u=()=>{l(),a(c)},d=()=>{l(),i()};try{r.addEventListener?.(`abort`,u,{once:!0})}catch{s=setTimeout(()=>{i()},e);return}if(r.aborted){l(),a(c);return}s=setTimeout(d,e)}):new Promise(t=>setTimeout(t,e))},xt=`checkConcurrencyInterceptor`,St=({logger:e,context:t,requestConfig:n,concurrencyManager:r,rateLimitManager:i})=>{let a=async a=>{if(a.signal?.aborted)return Promise.reject(yt(`Request aborted`,a,`ERR_CANCELED`));if(o(t))e?.warning({category:`HttpClient`,message:`No context for this request - that doesn't seem right.`,context:{interceptor:xt,...a,headers:void 0,hasContext:u(t),hasConfig:u(a)},code:vt.InterceptorContextNotPresent});else if(o(i)||o(r))e?.error({category:`HttpClient`,message:`RateLimitManager or ConcurrencyManager is not initialized`,context:{interceptor:xt,...a,headers:void 0,rateLimitManagerInitialized:u(i),concurrencyManagerInitialized:u(r)},code:vt.RateLimitOrConcurrencyManagerNotInitialized});else if(u(t.provider)&&u(t.accountSecureId)){let o=_(),{provider:s,accountSecureId:c}=t,l=n?.rateLimits??gt,u=n?.concurrency??$e,d=await i.getDynamicMaxWaitTime(l,u,a.url),f=async()=>r.registerRequest(`${c}-${s}`,u,o,a.url),p,m=0;for(;m<=d;){let n=Math.floor(Date.now()/1e3),r=await i.getWaitTime(`${c}-${s}`,l,a.url)??0,o=Math.max(r-n,0);if(r===0){p=await f();break}if(await bt(o*1e3,a),m+=o,m>=d){p=await f(),e?.warning({category:`HttpClient`,message:`Max Wait Time Exceeded for ${s} - Account: ${c}`,context:{...t,interceptor:xt,...a,headers:void 0,dynamicMaxWaitTime:d},code:vt.InterceptorMaxWaitTimeExceeded});break}}return{...a,requestMetadata:p}}return a};return a},Ct=1,wt=10,Tt=5,Et=3250368e4,Dt=1e3,Ot=/^\d+(\.\d+)?$/,kt=1e3,At=e=>e<Et?e*Dt:e;var jt=O(((exports,t)=>{function n(){return t.exports=n=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)({}).hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},t.exports.__esModule=!0,t.exports.default=t.exports,n.apply(null,arguments)}t.exports=n,t.exports.__esModule=!0,t.exports.default=t.exports})),Mt={epsilon:1e-12,matrix:`Matrix`,number:`number`,precision:64,predictable:!1,randomSeed:null};function M(e){return typeof e==`number`}function N(e){return!e||typeof e!=`object`||typeof e.constructor!=`function`?!1:e.isBigNumber===!0&&typeof e.constructor.prototype==`object`&&e.constructor.prototype.isBigNumber===!0||typeof e.constructor.isDecimal==`function`&&e.constructor.isDecimal(e)===!0}function Nt(e){return e&&typeof e==`object`&&Object.getPrototypeOf(e).isComplex===!0||!1}function Pt(e){return e&&typeof e==`object`&&Object.getPrototypeOf(e).isFraction===!0||!1}function Ft(e){return e&&e.constructor.prototype.isUnit===!0||!1}function It(e){return typeof e==`string`}var P=Array.isArray;function Lt(e){return e&&e.constructor.prototype.isMatrix===!0||!1}function Rt(e){return Array.isArray(e)||Lt(e)}function zt(e){return e&&e.isDenseMatrix&&e.constructor.prototype.isMatrix===!0||!1}function Bt(e){return e&&e.isSparseMatrix&&e.constructor.prototype.isMatrix===!0||!1}function Vt(e){return e&&e.constructor.prototype.isRange===!0||!1}function Ht(e){return e&&e.constructor.prototype.isIndex===!0||!1}function Ut(e){return typeof e==`boolean`}function Wt(e){return e&&e.constructor.prototype.isResultSet===!0||!1}function Gt(e){return e&&e.constructor.prototype.isHelp===!0||!1}function Kt(e){return typeof e==`function`}function qt(e){return e instanceof Date}function Jt(e){return e instanceof RegExp}function Yt(e){return!!(e&&typeof e==`object`&&e.constructor===Object&&!Nt(e)&&!Pt(e))}function Xt(e){return e===null}function Zt(e){return e===void 0}function Qt(e){return e&&e.isAccessorNode===!0&&e.constructor.prototype.isNode===!0||!1}function $t(e){return e&&e.isArrayNode===!0&&e.constructor.prototype.isNode===!0||!1}function en(e){return e&&e.isAssignmentNode===!0&&e.constructor.prototype.isNode===!0||!1}function tn(e){return e&&e.isBlockNode===!0&&e.constructor.prototype.isNode===!0||!1}function nn(e){return e&&e.isConditionalNode===!0&&e.constructor.prototype.isNode===!0||!1}function rn(e){return e&&e.isConstantNode===!0&&e.constructor.prototype.isNode===!0||!1}function an(e){return e&&e.isFunctionAssignmentNode===!0&&e.constructor.prototype.isNode===!0||!1}function on(e){return e&&e.isFunctionNode===!0&&e.constructor.prototype.isNode===!0||!1}function sn(e){return e&&e.isIndexNode===!0&&e.constructor.prototype.isNode===!0||!1}function cn(e){return e&&e.isNode===!0&&e.constructor.prototype.isNode===!0||!1}function ln(e){return e&&e.isObjectNode===!0&&e.constructor.prototype.isNode===!0||!1}function un(e){return e&&e.isOperatorNode===!0&&e.constructor.prototype.isNode===!0||!1}function dn(e){return e&&e.isParenthesisNode===!0&&e.constructor.prototype.isNode===!0||!1}function fn(e){return e&&e.isRangeNode===!0&&e.constructor.prototype.isNode===!0||!1}function pn(e){return e&&e.isRelationalNode===!0&&e.constructor.prototype.isNode===!0||!1}function mn(e){return e&&e.isSymbolNode===!0&&e.constructor.prototype.isNode===!0||!1}function hn(e){return e&&e.constructor.prototype.isChain===!0||!1}function gn(e){var t=typeof e;return t===`object`?e===null?`null`:N(e)?`BigNumber`:e.constructor&&e.constructor.name?e.constructor.name:`Object`:t}function _n(e){var t=typeof e;if(t===`number`||t===`string`||t===`boolean`||e==null)return e;if(typeof e.clone==`function`)return e.clone();if(Array.isArray(e))return e.map(function(e){return _n(e)});if(e instanceof Date)return new Date(e.valueOf());if(N(e))return e;if(Yt(e))return vn(e,_n);throw TypeError(`Cannot clone: unknown type of value (value: ${e})`)}function vn(e,t){var n={};for(var r in e)bn(e,r)&&(n[r]=t(e[r]));return n}function yn(e,t){var n,r,i;if(Array.isArray(e)){if(!Array.isArray(t)||e.length!==t.length)return!1;for(r=0,i=e.length;r<i;r++)if(!yn(e[r],t[r]))return!1;return!0}else if(typeof e==`function`)return e===t;else if(e instanceof Object){if(Array.isArray(t)||!(t instanceof Object))return!1;for(n in e)if(!(n in t)||!yn(e[n],t[n]))return!1;for(n in t)if(!(n in e))return!1;return!0}else return e===t}function bn(e,t){return e&&Object.hasOwnProperty.call(e,t)}function xn(e,t){for(var n={},r=0;r<t.length;r++){var i=t[r],a=e[i];a!==void 0&&(n[i]=a)}return n}var Sn=[`Matrix`,`Array`],Cn=[`number`,`BigNumber`,`Fraction`],wn=k(jt(),1),Tn=function(e){if(e)throw Error(`The global config is readonly.
|
|
102
102
|
Please create a mathjs instance if you want to change the default configuration.
|
|
103
103
|
Example:
|
|
104
104
|
|