@xyo-network/bridge-pub-sub 2.107.3 → 2.107.5
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/browser/index.cjs +1054 -1
- package/dist/browser/index.cjs.map +1 -1
- package/dist/browser/index.js +1033 -1
- package/dist/browser/index.js.map +1 -1
- package/dist/neutral/index.cjs +1054 -1
- package/dist/neutral/index.cjs.map +1 -1
- package/dist/neutral/index.js +1033 -1
- package/dist/neutral/index.js.map +1 -1
- package/dist/node/index.cjs +1110 -1
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.js +1076 -1
- package/dist/node/index.js.map +1 -1
- package/package.json +21 -21
package/dist/browser/index.cjs
CHANGED
|
@@ -1,2 +1,1055 @@
|
|
|
1
|
-
"use strict";var T=Object.defineProperty;var De=Object.getOwnPropertyDescriptor;var Me=Object.getOwnPropertyNames;var Re=Object.prototype.hasOwnProperty;var d=(a,e)=>T(a,"name",{value:e,configurable:!0});var Ee=(a,e)=>{for(var s in e)T(a,s,{get:e[s],enumerable:!0})},Pe=(a,e,s,t)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of Me(e))!Re.call(a,r)&&r!==s&&T(a,r,{get:()=>e[r],enumerable:!(t=De(e,r))||t.enumerable});return a};var Qe=a=>Pe(T({},"__esModule",{value:!0}),a);var We={};Ee(We,{AbstractModuleHost:()=>$,AsyncQueryBusClient:()=>M,AsyncQueryBusHost:()=>_,AsyncQueryBusModuleHost:()=>X,AsyncQueryBusModuleProxy:()=>E,Pending:()=>b,PubSubBridge:()=>H,PubSubBridgeConfigSchema:()=>L,PubSubBridgeModuleResolver:()=>Q,PubSubBridgeSchema:()=>V});module.exports=Qe(We);var ee=require("@xylabs/object");var $=class extends ee.Base{static{d(this,"AbstractModuleHost")}};var oe=require("@xylabs/assert"),ne=require("@xylabs/delay"),ae=require("@xylabs/forget"),D=require("@xylabs/timer"),le=require("@xyo-network/boundwitness-model"),de=require("@xyo-network/diviner-boundwitness-model"),j=require("@xyo-network/payload-builder"),ce=require("lru-cache");var S=require("@xylabs/assert"),te=require("@xylabs/object"),U=require("@xyo-network/archivist-model"),O=require("@xyo-network/diviner-model"),W=require("@xyo-network/module-model"),re=require("async-mutex"),ie=require("lru-cache");var Fe=100,He=6e4,se=1e3,q=class extends te.Base{static{d(this,"AsyncQueryBusBase")}_lastState;_targetConfigs={};_targetQueries={};_lastResolveFailure={};_queriesArchivist;_queriesDiviner;_reResolveDelay=1e3*5;_resolveMutex=new re.Mutex;_responsesArchivist;_responsesDiviner;constructor(e){super(e)}get config(){return this.params.config}get pollFrequency(){let e=this.config?.pollFrequency??se;return e<Fe||e>He?se:e}get rootModule(){return this.params.rootModule}get lastState(){let e={max:1e3,ttl:0};return this._lastState=this._lastState??new ie.LRUCache(e),this._lastState}async queriesArchivist(){return await this._resolveMutex.runExclusive(async()=>(this._queriesArchivist=this._queriesArchivist??await this.resolve((0,S.assertEx)(this.config?.intersect?.queries?.archivist,()=>"No queries Archivist defined"),U.isArchivistInstance),this._queriesArchivist))}async queriesDiviner(){return await this._resolveMutex.runExclusive(async()=>(this._queriesDiviner=this._queriesDiviner??await this.resolve((0,S.assertEx)(this.config?.intersect?.queries?.boundWitnessDiviner,()=>"No queries Diviner defined"),O.isDivinerInstance),this._queriesDiviner))}async responsesArchivist(){return await this._resolveMutex.runExclusive(async()=>(this._responsesArchivist=this._responsesArchivist??await this.resolve((0,S.assertEx)(this.config?.intersect?.responses?.archivist,()=>"No responses Archivist defined"),U.isArchivistInstance),this._responsesArchivist))}async responsesDiviner(){return await this._resolveMutex.runExclusive(async()=>(this._responsesDiviner=this._responsesDiviner??await this.resolve((0,S.assertEx)(this.config?.intersect?.responses?.boundWitnessDiviner,()=>"No responses Diviner defined"),O.isDivinerInstance),this._responsesDiviner))}async commitState(e,s){await Promise.resolve();let t=this.lastState.get(e);t&&t>=s||this.lastState.set(e,s)}async retrieveState(e){await Promise.resolve();let s=this.lastState.get(e);if(s===void 0){let t=Date.now()-1e3;return this.lastState.set(e,t),t}else return s}async resolve(e,s){if(Date.now()-(this._lastResolveFailure[e]??0)<this._reResolveDelay)return;this._lastResolveFailure[e]=Date.now();let t=await W.ResolveHelper.resolveModuleIdentifier(this.rootModule,e);if(t){if(s(t))return delete this._lastResolveFailure[e],t;this.logger?.warn(`Unable to resolve responsesDiviner as correct type [${e}][${t?.constructor?.name}]: ${t.id}`)}else this.logger?.debug(`Unable to resolve queriesArchivist [${e}] [${await W.ResolveHelper.traceModuleIdentifier(this.rootModule,e)}]`)}};var b="pending";var M=class extends q{static{d(this,"AsyncQueryBusClient")}_queryCache;_pollCount=0;_pollId;constructor(e){super(e)}get queryCacheConfig(){let e=this.config?.queryCache===!0?{}:this.config?.queryCache;return{max:100,ttl:1e3*60,...e}}get started(){return!!this._pollId}get queryCache(){let e=this.queryCacheConfig,s={noUpdateTTL:!1,ttlAutopurge:!0};return this._queryCache=this._queryCache??new ce.LRUCache({...e,...s}),this._queryCache}listeningAddresses(){return this._queryCache?.keys()}async send(e,s,t){this.logger?.debug(`Begin issuing query to: ${e}`);let r={...s?.$meta,destination:[e]},i=await j.PayloadBuilder.build({...s,$meta:r}),o=(0,oe.assertEx)(await this.queriesArchivist(),()=>`Unable to contact queriesArchivist [${this.config?.intersect?.queries?.archivist}]`),n=i?.$hash??Object.keys(await j.PayloadBuilder.toDataHashMap([i]))[0];this.logger?.debug(`Issuing query: ${n} to: ${e}`);let l=t?[i,...t]:[i],c=await o.insert?.(l);if(this.logger?.debug(`Issued query: ${n} to: ${e}`),this.queryCache.set(n,b),!c)throw new Error("Unable to issue query to queryArchivist");return new Promise((p,y)=>{this.logger?.debug(`Polling for response to query: ${n}`);let u=100;(0,ae.forget)(d(async()=>{try{this.start();let f=this.queryCache.get(n);for(;f!==void 0;){if(await(0,ne.delay)(u),f=this.queryCache.get(n),f&&f!==b){this.logger?.log(`Returning response to query: ${n}`),p(f);return}u=Math.floor(u*1.2),u>1e3&&(u=1e3)}this.logger?.error("Timeout waiting for query response"),y({message:"Timeout waiting for query response",query:"network.xyo.boundwitness",schema:"network.xyo.error.module",sources:[n]});return}finally{this.stop()}},"pollForResponse")())})}poll(){this._pollId=(0,D.setTimeoutEx)(async()=>{try{await this.processIncomingResponses()}catch(e){this.logger?.error?.(`Error in main loop: ${e}`)}finally{this._pollId&&(0,D.clearTimeoutEx)(this._pollId),this._pollId=void 0,this.poll()}},this.pollFrequency)}processIncomingResponses=d(async()=>{let e=await this.responsesArchivist();if(e){let s=await this.responsesDiviner();if(s){let t=[...this.queryCache.entries()].filter(([r,i])=>i===b);await Promise.allSettled(t.map(async([r,i])=>{if(i===b){let o={limit:1,order:"desc",schema:de.BoundWitnessDivinerQuerySchema,sourceQuery:r},n=await s.divine([o]);if(n&&n.length>0){let l=n.find(le.isBoundWitnessWithMeta);if(l&&l?.$meta?.sourceQuery===r){this.logger?.debug(`Found response to query: ${r}`);let c=l.payload_hashes?.length>0?await e.get(l.payload_hashes):[];this.queryCache.set(r,[l,c,[]])}}}}))}}},"processIncomingResponses");start(){this._pollCount===0&&this.poll(),this._pollCount++}stop(){this._pollCount--,this._pollCount<=0&&(this._pollId&&(0,D.clearTimeoutEx)(this._pollId),this._pollId=void 0,this._pollCount=0)}};var he=require("@xylabs/array"),v=require("@xylabs/assert"),R=require("@xylabs/timer"),ue=require("@xyo-network/boundwitness-model"),pe=require("@xyo-network/bridge-model"),fe=require("@xyo-network/diviner-boundwitness-model"),g=require("@xyo-network/module-model"),k=require("@xyo-network/payload-builder");var G=4,Y=64,Te=16,Le=4,Ne=64,Be=16,_=class extends q{static{d(this,"AsyncQueryBusHost")}_exposedAddresses=new Set;_exposeOptions={};_idle=!1;_lastQueryTime;_pollId;constructor(e){super(e)}get exposedAddresses(){return this._exposedAddresses}get idlePollFrequency(){let e=this.config?.idlePollFrequency??Te*this.pollFrequency;return e<this.pollFrequency*G?G*this.pollFrequency:e>this.pollFrequency*Y?Y*this.pollFrequency:e}get idleThreshold(){let e=this.config?.idleThreshold??Be*this.idlePollFrequency;return e<this.idlePollFrequency*Le?G*this.pollFrequency:e>this.idlePollFrequency*Ne?Y*this.pollFrequency:e}get perAddressBatchQueryLimit(){return this.config?.perAddressBatchQueryLimit??10}get started(){return!!this._pollId}expose(e,s){let{failOnAlreadyExposed:t}=s??{};if((0,pe.isBridgeInstance)(e))this.logger?.warn(`Attempted to expose a BridgeModule [${e.id}] - Not exposing`);else return(0,v.assertEx)(!t||!this._exposedAddresses.has(e.address),()=>`Address already exposed: ${e.id} [${e.address}]`),this._exposedAddresses.add(e.address),this._exposeOptions[e.address]={...s},this.logger?.debug(`${e.id} exposed [${e.address}]`),e}async listeningModules(){let e=[...this.config?.listeningModules??[],...this.exposedAddresses.values()];return await Promise.all(e.map(async t=>(0,v.assertEx)((0,g.asModuleInstance)(await(0,g.resolveAddressToInstance)(this.rootModule,t)),()=>`Unable to resolve listeningModule [${t}]`)))}start(){this.started&&console.warn("AsyncQueryBus starting when already started"),this.poll()}stop(){this.started||console.warn("AsyncQueryBus stopping when already stopped"),this._pollId&&(0,R.clearTimeoutEx)(this._pollId),this._pollId=void 0}async unexpose(e,s=!0){let t=(0,g.asModuleInstance)(await this.rootModule.resolve(e,{maxDepth:10}));return t&&((0,v.assertEx)(!s||this._exposedAddresses.has(t.address),()=>`Address not exposed [${t.address}][${t.id}]`),this._exposedAddresses.delete(t.address),delete this._exposeOptions[t.address],this.logger?.debug(`${t.address} [${t.id}] unexposed`)),t}callLocalModule=d(async(e,s)=>{this._idle=!1,this._lastQueryTime=Date.now();let t=e.id,r=(0,v.assertEx)(await this.queriesArchivist(),()=>`Unable to contact queriesArchivist [${this.config?.intersect?.queries?.archivist}]`),i=(0,v.assertEx)(await this.responsesArchivist(),()=>`Unable to contact responsesArchivist [${this.config?.intersect?.queries?.archivist}]`),o=s.$meta?.destination;if(o&&o?.includes(e.address)){let n=s.payload_hashes.indexOf(s.query);if(n!==-1){let l=s.payload_schemas[n];if(e.queries.includes(l)){let c=await r.get(s.payload_hashes);this.params.onQueryFulfillStarted?.({payloads:c,query:s});let m=await k.PayloadBuilder.toAllHashMap(c),p=(await k.PayloadBuilder.build(s)).$hash;if(!(0,he.containsAll)(Object.keys(m),s.payload_hashes)){this.logger?.error(`Error processing command ${p} for module ${t}, missing payloads`);return}try{let y=m[s.query].schema;this.logger?.debug(`Issuing query ${y} (${p}) addressed to module: ${t}`);let u=await e.query(s,c,{allowedQueries:this._exposeOptions[e.address]?.allowedQueries,schema:g.ModuleConfigSchema}),[A,f,I]=u;this.logger?.debug(`Replying to query ${p} addressed to module: ${t}`),(await i.insert([A,...f,...I])).length===0&&this.logger?.error(`Error replying to query ${p} addressed to module: ${t}`),s?.timestamp&&await this.commitState(e.address,s.timestamp),this.params.onQueryFulfillFinished?.({payloads:c,query:s,result:u,status:"success"})}catch(y){this.params.onQueryFulfillFinished?.({payloads:c,query:s,status:"failure"}),this.logger?.error(`Error processing query ${p} for module ${t}: ${y}`)}}}}},"callLocalModule");findQueriesToAddress=d(async e=>{let s=(0,v.assertEx)(this.config?.intersect?.queries?.boundWitnessDiviner,()=>"No queries Diviner defined"),t=await this.queriesDiviner();if(t){let r=await this.retrieveState(e),i=[e],o=this.perAddressBatchQueryLimit,n={destination:i,limit:o,order:"asc",schema:fe.BoundWitnessDivinerQuerySchema,timestamp:r+1},c=(await t.divine([n])).filter(ue.isQueryBoundWitnessWithMeta),m=c.length>0?Math.max(...c.map(p=>p.timestamp??r))+1:Date.now();return await this.commitState(e,m),this.logger?.debug("findQueriesToAddress",e,r,m),c}else this.logger?.warn(`Unable to resolve queriesBoundWitnessDiviner [${s}] [${await g.ResolveHelper.traceModuleIdentifier(this.rootModule,s)}]`)},"findQueriesToAddress");poll(){this._pollId=(0,R.setTimeoutEx)(async()=>{try{await this.processIncomingQueries()}catch(s){this.logger?.error?.(`Error in main loop: ${s}`)}finally{this._pollId&&(0,R.clearTimeoutEx)(this._pollId),this._pollId=void 0,this.poll()}let e=Date.now();this.idleThreshold<e-(this._lastQueryTime??e)&&(this._idle=!0)},this._idle?this.idlePollFrequency:this.pollFrequency)}processIncomingQueries=d(async()=>{this.logger?.debug("Checking for inbound queries");let e=await this.listeningModules();await Promise.allSettled(e.map(async s=>{try{let t=s.id;this.logger?.debug(`Checking for inbound queries to ${t} [${s.address}]`);let r=await this.findQueriesToAddress(s.address)??[];if(r.length===0)return;this.logger?.debug(`Found queries addressed to local module: ${t}`);for(let i of r)await this.callLocalModule(s,i)}catch(t){this.logger?.error(`Error processing queries for address ${s.address}: ${t}`)}}))},"processIncomingQueries")};var X=class extends ${static{d(this,"AsyncQueryBusModuleHost")}_busHost;constructor(e){super(e)}async start(){let e=this.params.config.listeningModules??(await this.params.module.resolve("*",{direction:"down"})).map(s=>s.address);this._busHost=new _({config:{...this.params.config,listeningModules:e},logger:this.params.logger,rootModule:this.params.module}),this._busHost?.start()}stop(){this._busHost?.stop()}};var me=require("@xylabs/assert"),J=require("@xylabs/exists"),z=require("@xylabs/forget"),ge=require("@xylabs/hex"),ye=require("@xyo-network/bridge-abstract"),K=require("@xyo-network/module-model");var E=class a extends ye.AbstractModuleProxy{static{d(this,"AsyncQueryBusModuleProxy")}static createCount=0;constructor(e){a.createCount=a.createCount+1,Math.floor(a.createCount/10)===a.createCount/10&&console.log(`AsyncQueryBusModuleProxy.createCount: ${a.createCount}`),super(e)}get id(){return this.address}get modName(){}async proxyQueryHandler(e,s){this.archiving&&this.isAllowedArchivingQuery(e.schema)&&(0,z.forget)(this.storeToArchivists([e,...s??[]]));let t=await this.params.busClient.send(this.address,e,s);return this.archiving&&this.isAllowedArchivingQuery(e.schema)&&(0,z.forget)(this.storeToArchivists(t.flat())),t}async publicChildren(){return(await Promise.all(Object.values(await this.childAddressMap()).filter(J.exists).map(e=>this.resolve(e)))).filter(J.exists)}async resolve(e="*",s={}){let t={address:this.address,dead:this.dead,downResolver:this.downResolver,logger:this.logger,module:this,transformers:this.moduleIdentifierTransformers,upResolver:this.upResolver};if(e==="*")return await this.publicChildren();switch(typeof e){case"string":{let r=e.split(":"),i=(0,me.assertEx)(r.shift(),()=>"Missing first"),o=r.join(":"),n=(0,ge.isAddress)(i)?i:this.childAddressByName(i);if(!n)return;let l=await this.params.host.resolve(n);return o?await l?.resolve(o):l}case"object":return(await K.ResolveHelper.resolve(t,e,s)).filter(r=>r.address!==this.address);default:return(await K.ResolveHelper.resolve(t,e,s)).filter(r=>r.address!==this.address)}}async startHandler(){return await super.startHandler()}};var V="network.xyo.bridge.pubsub";var L=`${V}.config`;var w=require("@xylabs/assert"),F=require("@xylabs/exists"),x=require("@xylabs/forget"),qe=require("@xylabs/hex"),be=require("@xylabs/object"),Ce=require("@xyo-network/bridge-abstract"),h=require("@xyo-network/module-model"),Ie=require("@xyo-network/node-model"),$e=require("@xyo-network/payload-model"),Se=require("async-mutex");var P=require("@xylabs/assert"),ve=require("@xylabs/hex"),_e=require("@xyo-network/account"),N=require("@xyo-network/bridge-abstract"),we=require("@xyo-network/config-payload-plugin"),C=require("@xyo-network/module-model"),xe=require("async-mutex"),Ae=require("lru-cache");var Q=class extends N.AbstractBridgeModuleResolver{static{d(this,"PubSubBridgeModuleResolver")}_resolvedCache=new Ae.LRUCache({max:1e3});_resolvedCacheMutex=new xe.Mutex;async resolveHandler(e,s){let t=await super.resolveHandler(e,s);if(t.length>0)return t;let r=e.split(":"),i=(0,P.assertEx)(r.shift(),()=>"Missing module identifier"),o=await C.ResolveHelper.transformModuleIdentifier(i);(0,P.assertEx)((0,ve.isAddress)(o),()=>`Invalid module address: ${o}`);let n=r.join(":"),l=await this._resolvedCacheMutex.runExclusive(async()=>{let m=this._resolvedCache.get(o);if(m)return r.length<=0?m:m.resolve(n,{...s,maxDepth:(s?.maxDepth??5)-1});let p=_e.Account.randomSync(),y={account:p,archiving:this.params.archiving,busClient:this.params.busClient,config:{schema:C.ModuleConfigSchema},host:this,moduleAddress:o,onQuerySendFinished:this.params.onQuerySendFinished,onQuerySendStarted:this.params.onQuerySendStarted},u=new E(y),A=await u.state();if(A){let I=A.find(B=>B.schema===we.ConfigSchema)?.config,Z=(0,P.assertEx)(A.find(B=>B.schema===I),()=>"Unable to locate config");u.setConfig(Z)}await u.start?.();let f=(0,N.wrapModuleWithType)(u,p);return(0,P.assertEx)((0,C.asModuleInstance)(f,{}),()=>`Failed to asModuleInstance [${e}]`),this._resolvedCache.set(f.address,f),f}),c=n.length>0?await l.resolve(n,s):l;return c?[c]:[]}};function Ue(a,e,s,t){var r=arguments.length,i=r<3?e:t===null?t=Object.getOwnPropertyDescriptor(e,s):t,o;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")i=Reflect.decorate(a,e,s,t);else for(var n=a.length-1;n>=0;n--)(o=a[n])&&(i=(r<3?o(i):r>3?o(e,s,i):o(e,s))||i);return r>3&&i&&Object.defineProperty(e,s,i),i}d(Ue,"_ts_decorate");var Oe="PubSubBridge",H=class extends Ce.AbstractBridge{static{d(this,"PubSubBridge")}static configSchemas=[...super.configSchemas,L];static defaultConfigSchema=L;_configRootAddress="";_configStateStoreArchivist="";_configStateStoreBoundWitnessDiviner="";_exposedAddresses=[];_lastState;_busClient;_busHost;_discoverRootsMutex=new Se.Mutex;_resolver;get resolver(){return this._resolver=this._resolver??new Q({additionalSigners:this.additionalSigners,archiving:{...this.archiving,resolveArchivists:this.resolveArchivingArchivists.bind(this)},bridge:this,busClient:(0,w.assertEx)(this.busClient(),()=>"busClient not configured"),onQuerySendFinished:d(e=>{(0,x.forget)(this.emit("querySendFinished",{module:this,...e}))},"onQuerySendFinished"),onQuerySendStarted:d(e=>{(0,x.forget)(this.emit("querySendStarted",{module:this,...e}))},"onQuerySendStarted"),root:this,wrapperAccount:this.account}),this._resolver}get moduleName(){return this.modName??Oe}async connect(e,s=5){let t=(0,w.assertEx)(await h.ResolveHelper.transformModuleIdentifier(e),()=>`Unable to transform module identifier: ${e}`),r=await this.resolve(t);if(r)return r.address;let[i]=await this.resolver.resolveHandler(e);return await this.connectInstance(i,s)}async disconnect(e){let s=(0,w.assertEx)(await h.ResolveHelper.transformModuleIdentifier(e),()=>`Unable to transform module identifier: ${e}`),t=await this.resolve(s);if(t)return this.downResolver.remove(t.address),t.address}async exposeChild(e,s){let{maxDepth:t=5}=s??{};console.log(`exposeChild: ${e.address} ${e?.id} ${t}`),(0,w.assertEx)(this.busHost(),()=>"Not configured as a host").expose(e);let i=t>0?await e.publicChildren?.()??[]:[];this.logger.log(`childrenToExpose [${e.id}][${e.address}]: ${(0,be.toJsonString)(i.map(l=>l.id))}`);let o=(await Promise.all(i.map(l=>this.exposeChild(l,{maxDepth:t-1,required:!1})))).flat().filter(F.exists),n=[e,...o];for(let l of n)this.logger?.log(`exposed: ${l.address} [${e.id}]`);return n}async exposeHandler(e,s){let{required:t=!0}=s??{},r=await(0,h.resolveAddressToInstanceUp)(this,e);if(console.log(`exposeHandler: ${e} ${r?.id}`),t&&!r)throw new Error(`Unable to find required module: ${e}`);return r?this.exposeChild(r,s):[]}exposedHandler(){let e=this.busHost()?.exposedAddresses;return e?[...e]:[]}async getRoots(e){return await this._discoverRootsMutex.runExclusive(async()=>{if(this._roots===void 0||e){let s=(await Promise.all((this.config.roots??[]).map(r=>{try{return h.ResolveHelper.transformModuleIdentifier(r)}catch(i){this.logger?.warn("Unable to transform module identifier:",r,i);return}}))).filter(F.exists),t=(await Promise.all(s.map(async r=>{try{return await this.resolver.resolveHandler(r)}catch(i){this.logger?.warn("Unable to resolve root:",r,i);return}}))).flat().filter(F.exists);for(let r of t)this.downResolver.add(r);this._roots=t}return this._roots})}async resolve(e="*",s={}){let t=this._roots??[],r=s.direction==="up"?[this]:[...t,this];if(e==="*"){let i=(s.maxDepth??1)-1;return i<=0?r:[...r,...(await Promise.all(t.map(o=>o.resolve("*",{...s,maxDepth:i})))).flat()]}switch(typeof e){case"string":{let i=e.split(":"),o=(0,w.assertEx)(i.shift(),()=>"Missing first part"),n=(0,qe.isAddress)(o)?await(0,h.resolveAddressToInstance)(this,o,void 0,[],s.direction):this._roots?.find(l=>l.id===o);return i.length===0?n:n?.resolve(i.join(":"),s)}case"object":{let i=[];if((0,h.isAddressModuleFilter)(e))for(let o of r)o.modName&&e.address.includes(o.address)&&i.push(o);return i}default:return}}async startHandler(){return this.busHost()?.start(),await super.startHandler()}async unexposeHandler(e,s){let{maxDepth:t=2,required:r=!0}=s??{},o=await(0,w.assertEx)(this.busHost(),()=>"Not configured as a host").unexpose(e,r);if(o){let n=t>0?await o.publicChildren?.()??[]:[],l=(await Promise.all(n.map(c=>this.unexposeHandler(c.address,{maxDepth:t-1,required:!1})))).flat().filter(F.exists);return[o,...l]}return[]}busClient(){return!this._busClient&&this.config.client&&(this._busClient=new M({config:this.config.client,logger:this.logger,rootModule:this})),this._busClient}busHost(){return!this._busHost&&this.config.host&&(this._busHost=new _({config:this.config.host,logger:this.logger,onQueryFulfillFinished:d(e=>{this.archiving&&this.isAllowedArchivingQuery(e.query.schema)&&(0,x.forget)(this.storeToArchivists(e.result?.flat()??[])),(0,x.forget)(this.emit("queryFulfillFinished",{module:this,...e}))},"onQueryFulfillFinished"),onQueryFulfillStarted:d(e=>{this.archiving&&this.isAllowedArchivingQuery(e.query.schema)&&(0,x.forget)(this.storeToArchivists([e.query,...e.payloads??[]])),(0,x.forget)(this.emit("queryFulfillStarted",{module:this,...e}))},"onQueryFulfillStarted"),rootModule:this})),this._busHost}async connectInstance(e,s=5){if(e){if(this.downResolver.add(e),s>0){let t=(0,Ie.asNodeInstance)(e);if(t){let i=((await t.state())?.filter((0,$e.isPayloadOfSchemaType)(h.AddressSchema)).map(o=>o.address)??[]).filter(o=>o!==e.address);await Promise.all(i.map(o=>this.connect(o,s-1)))}}return this.logger?.log(`Connect: ${e.id}`),e.address}}stopHandler(e){return this.busHost()?.stop(),!0}};H=Ue([(0,h.creatableModule)()],H);
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/index.ts
|
|
22
|
+
var src_exports = {};
|
|
23
|
+
__export(src_exports, {
|
|
24
|
+
AbstractModuleHost: () => AbstractModuleHost,
|
|
25
|
+
AsyncQueryBusClient: () => AsyncQueryBusClient,
|
|
26
|
+
AsyncQueryBusHost: () => AsyncQueryBusHost,
|
|
27
|
+
AsyncQueryBusModuleHost: () => AsyncQueryBusModuleHost,
|
|
28
|
+
AsyncQueryBusModuleProxy: () => AsyncQueryBusModuleProxy,
|
|
29
|
+
Pending: () => Pending,
|
|
30
|
+
PubSubBridge: () => PubSubBridge,
|
|
31
|
+
PubSubBridgeConfigSchema: () => PubSubBridgeConfigSchema,
|
|
32
|
+
PubSubBridgeModuleResolver: () => PubSubBridgeModuleResolver,
|
|
33
|
+
PubSubBridgeSchema: () => PubSubBridgeSchema
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(src_exports);
|
|
36
|
+
|
|
37
|
+
// src/AbstractModuleHost/AbstractModuleHost.ts
|
|
38
|
+
var import_object = require("@xylabs/object");
|
|
39
|
+
var AbstractModuleHost = class extends import_object.Base {
|
|
40
|
+
static {
|
|
41
|
+
__name(this, "AbstractModuleHost");
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// src/AsyncQueryBus/AsyncQueryBusClient.ts
|
|
46
|
+
var import_assert2 = require("@xylabs/assert");
|
|
47
|
+
var import_delay = require("@xylabs/delay");
|
|
48
|
+
var import_forget = require("@xylabs/forget");
|
|
49
|
+
var import_timer = require("@xylabs/timer");
|
|
50
|
+
var import_boundwitness_model = require("@xyo-network/boundwitness-model");
|
|
51
|
+
var import_diviner_boundwitness_model = require("@xyo-network/diviner-boundwitness-model");
|
|
52
|
+
var import_payload_builder = require("@xyo-network/payload-builder");
|
|
53
|
+
var import_lru_cache2 = require("lru-cache");
|
|
54
|
+
|
|
55
|
+
// src/AsyncQueryBus/AsyncQueryBusBase.ts
|
|
56
|
+
var import_assert = require("@xylabs/assert");
|
|
57
|
+
var import_object2 = require("@xylabs/object");
|
|
58
|
+
var import_archivist_model = require("@xyo-network/archivist-model");
|
|
59
|
+
var import_diviner_model = require("@xyo-network/diviner-model");
|
|
60
|
+
var import_module_model = require("@xyo-network/module-model");
|
|
61
|
+
var import_async_mutex = require("async-mutex");
|
|
62
|
+
var import_lru_cache = require("lru-cache");
|
|
63
|
+
var POLLING_FREQUENCY_MIN = 100;
|
|
64
|
+
var POLLING_FREQUENCY_MAX = 6e4;
|
|
65
|
+
var POLLING_FREQUENCY_DEFAULT = 1e3;
|
|
66
|
+
var AsyncQueryBusBase = class extends import_object2.Base {
|
|
67
|
+
static {
|
|
68
|
+
__name(this, "AsyncQueryBusBase");
|
|
69
|
+
}
|
|
70
|
+
_lastState;
|
|
71
|
+
_targetConfigs = {};
|
|
72
|
+
_targetQueries = {};
|
|
73
|
+
_lastResolveFailure = {};
|
|
74
|
+
_queriesArchivist;
|
|
75
|
+
_queriesDiviner;
|
|
76
|
+
_reResolveDelay = 1e3 * 5;
|
|
77
|
+
_resolveMutex = new import_async_mutex.Mutex();
|
|
78
|
+
_responsesArchivist;
|
|
79
|
+
_responsesDiviner;
|
|
80
|
+
constructor(params) {
|
|
81
|
+
super(params);
|
|
82
|
+
}
|
|
83
|
+
get config() {
|
|
84
|
+
return this.params.config;
|
|
85
|
+
}
|
|
86
|
+
get pollFrequency() {
|
|
87
|
+
const frequency = this.config?.pollFrequency ?? POLLING_FREQUENCY_DEFAULT;
|
|
88
|
+
if (frequency < POLLING_FREQUENCY_MIN || frequency > POLLING_FREQUENCY_MAX) {
|
|
89
|
+
return POLLING_FREQUENCY_DEFAULT;
|
|
90
|
+
}
|
|
91
|
+
return frequency;
|
|
92
|
+
}
|
|
93
|
+
get rootModule() {
|
|
94
|
+
return this.params.rootModule;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* A cache of the last offset of the Diviner process per address
|
|
98
|
+
*/
|
|
99
|
+
get lastState() {
|
|
100
|
+
const requiredConfig = {
|
|
101
|
+
max: 1e3,
|
|
102
|
+
ttl: 0
|
|
103
|
+
};
|
|
104
|
+
this._lastState = this._lastState ?? new import_lru_cache.LRUCache(requiredConfig);
|
|
105
|
+
return this._lastState;
|
|
106
|
+
}
|
|
107
|
+
async queriesArchivist() {
|
|
108
|
+
return await this._resolveMutex.runExclusive(async () => {
|
|
109
|
+
this._queriesArchivist = this._queriesArchivist ?? await this.resolve((0, import_assert.assertEx)(this.config?.intersect?.queries?.archivist, () => "No queries Archivist defined"), import_archivist_model.isArchivistInstance);
|
|
110
|
+
return this._queriesArchivist;
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
async queriesDiviner() {
|
|
114
|
+
return await this._resolveMutex.runExclusive(async () => {
|
|
115
|
+
this._queriesDiviner = this._queriesDiviner ?? await this.resolve((0, import_assert.assertEx)(this.config?.intersect?.queries?.boundWitnessDiviner, () => "No queries Diviner defined"), import_diviner_model.isDivinerInstance);
|
|
116
|
+
return this._queriesDiviner;
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
async responsesArchivist() {
|
|
120
|
+
return await this._resolveMutex.runExclusive(async () => {
|
|
121
|
+
this._responsesArchivist = this._responsesArchivist ?? await this.resolve((0, import_assert.assertEx)(this.config?.intersect?.responses?.archivist, () => "No responses Archivist defined"), import_archivist_model.isArchivistInstance);
|
|
122
|
+
return this._responsesArchivist;
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
async responsesDiviner() {
|
|
126
|
+
return await this._resolveMutex.runExclusive(async () => {
|
|
127
|
+
this._responsesDiviner = this._responsesDiviner ?? await this.resolve((0, import_assert.assertEx)(this.config?.intersect?.responses?.boundWitnessDiviner, () => "No responses Diviner defined"), import_diviner_model.isDivinerInstance);
|
|
128
|
+
return this._responsesDiviner;
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Commit the internal state of the process. This is similar
|
|
133
|
+
* to a transaction completion in a database and should only be called
|
|
134
|
+
* when results have been successfully persisted to the appropriate
|
|
135
|
+
* external stores.
|
|
136
|
+
* @param address The module address to commit the state for
|
|
137
|
+
* @param nextState The state to commit
|
|
138
|
+
*/
|
|
139
|
+
async commitState(address, nextState) {
|
|
140
|
+
await Promise.resolve();
|
|
141
|
+
const lastState = this.lastState.get(address);
|
|
142
|
+
if (lastState && lastState >= nextState) return;
|
|
143
|
+
this.lastState.set(address, nextState);
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Retrieves the last state of the process. Used to recover state after
|
|
147
|
+
* preemptions, reboots, etc.
|
|
148
|
+
*/
|
|
149
|
+
async retrieveState(address) {
|
|
150
|
+
await Promise.resolve();
|
|
151
|
+
const state = this.lastState.get(address);
|
|
152
|
+
if (state === void 0) {
|
|
153
|
+
const newState = Date.now() - 1e3;
|
|
154
|
+
this.lastState.set(address, newState);
|
|
155
|
+
return newState;
|
|
156
|
+
} else {
|
|
157
|
+
return state;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
async resolve(id, typeCheck) {
|
|
161
|
+
if (Date.now() - (this._lastResolveFailure[id] ?? 0) < this._reResolveDelay) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
this._lastResolveFailure[id] = Date.now();
|
|
165
|
+
const resolved = await import_module_model.ResolveHelper.resolveModuleIdentifier(this.rootModule, id);
|
|
166
|
+
if (resolved) {
|
|
167
|
+
if (typeCheck(resolved)) {
|
|
168
|
+
delete this._lastResolveFailure[id];
|
|
169
|
+
return resolved;
|
|
170
|
+
} else {
|
|
171
|
+
this.logger?.warn(`Unable to resolve responsesDiviner as correct type [${id}][${resolved?.constructor?.name}]: ${resolved.id}`);
|
|
172
|
+
}
|
|
173
|
+
} else {
|
|
174
|
+
this.logger?.debug(`Unable to resolve queriesArchivist [${id}] [${await import_module_model.ResolveHelper.traceModuleIdentifier(this.rootModule, id)}]`);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
// src/AsyncQueryBus/model/QueryStatus.ts
|
|
180
|
+
var Pending = "pending";
|
|
181
|
+
|
|
182
|
+
// src/AsyncQueryBus/AsyncQueryBusClient.ts
|
|
183
|
+
var AsyncQueryBusClient = class extends AsyncQueryBusBase {
|
|
184
|
+
static {
|
|
185
|
+
__name(this, "AsyncQueryBusClient");
|
|
186
|
+
}
|
|
187
|
+
_queryCache;
|
|
188
|
+
_pollCount = 0;
|
|
189
|
+
_pollId;
|
|
190
|
+
constructor(params) {
|
|
191
|
+
super(params);
|
|
192
|
+
}
|
|
193
|
+
get queryCacheConfig() {
|
|
194
|
+
const queryCacheConfig = this.config?.queryCache === true ? {} : this.config?.queryCache;
|
|
195
|
+
return {
|
|
196
|
+
max: 100,
|
|
197
|
+
ttl: 1e3 * 60,
|
|
198
|
+
...queryCacheConfig
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
get started() {
|
|
202
|
+
return !!this._pollId;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* A cache of queries that have been issued
|
|
206
|
+
*/
|
|
207
|
+
get queryCache() {
|
|
208
|
+
const config = this.queryCacheConfig;
|
|
209
|
+
const requiredConfig = {
|
|
210
|
+
noUpdateTTL: false,
|
|
211
|
+
ttlAutopurge: true
|
|
212
|
+
};
|
|
213
|
+
this._queryCache = this._queryCache ?? new import_lru_cache2.LRUCache({
|
|
214
|
+
...config,
|
|
215
|
+
...requiredConfig
|
|
216
|
+
});
|
|
217
|
+
return this._queryCache;
|
|
218
|
+
}
|
|
219
|
+
listeningAddresses() {
|
|
220
|
+
return this._queryCache?.keys();
|
|
221
|
+
}
|
|
222
|
+
async send(address, query, payloads) {
|
|
223
|
+
this.logger?.debug(`Begin issuing query to: ${address}`);
|
|
224
|
+
const $meta = {
|
|
225
|
+
...query?.$meta,
|
|
226
|
+
destination: [
|
|
227
|
+
address
|
|
228
|
+
]
|
|
229
|
+
};
|
|
230
|
+
const routedQuery = await import_payload_builder.PayloadBuilder.build({
|
|
231
|
+
...query,
|
|
232
|
+
$meta
|
|
233
|
+
});
|
|
234
|
+
const queryArchivist = (0, import_assert2.assertEx)(await this.queriesArchivist(), () => `Unable to contact queriesArchivist [${this.config?.intersect?.queries?.archivist}]`);
|
|
235
|
+
const routedQueryHash = (
|
|
236
|
+
// Trust the signed hash if it's there
|
|
237
|
+
routedQuery?.$hash ?? // TODO: What is the right way to find the dataHash
|
|
238
|
+
Object.keys(await import_payload_builder.PayloadBuilder.toDataHashMap([
|
|
239
|
+
routedQuery
|
|
240
|
+
]))[0]
|
|
241
|
+
);
|
|
242
|
+
this.logger?.debug(`Issuing query: ${routedQueryHash} to: ${address}`);
|
|
243
|
+
const data = payloads ? [
|
|
244
|
+
routedQuery,
|
|
245
|
+
...payloads
|
|
246
|
+
] : [
|
|
247
|
+
routedQuery
|
|
248
|
+
];
|
|
249
|
+
const insertResult = await queryArchivist.insert?.(data);
|
|
250
|
+
this.logger?.debug(`Issued query: ${routedQueryHash} to: ${address}`);
|
|
251
|
+
this.queryCache.set(routedQueryHash, Pending);
|
|
252
|
+
if (!insertResult) throw new Error("Unable to issue query to queryArchivist");
|
|
253
|
+
const context = new Promise((resolve, reject) => {
|
|
254
|
+
this.logger?.debug(`Polling for response to query: ${routedQueryHash}`);
|
|
255
|
+
let nextDelay = 100;
|
|
256
|
+
const pollForResponse = /* @__PURE__ */ __name(async () => {
|
|
257
|
+
try {
|
|
258
|
+
this.start();
|
|
259
|
+
let response = this.queryCache.get(routedQueryHash);
|
|
260
|
+
while (response !== void 0) {
|
|
261
|
+
await (0, import_delay.delay)(nextDelay);
|
|
262
|
+
response = this.queryCache.get(routedQueryHash);
|
|
263
|
+
if (response && response !== Pending) {
|
|
264
|
+
this.logger?.log(`Returning response to query: ${routedQueryHash}`);
|
|
265
|
+
resolve(response);
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
nextDelay = Math.floor(nextDelay * 1.2);
|
|
269
|
+
if (nextDelay > 1e3) nextDelay = 1e3;
|
|
270
|
+
}
|
|
271
|
+
this.logger?.error("Timeout waiting for query response");
|
|
272
|
+
const error = {
|
|
273
|
+
message: "Timeout waiting for query response",
|
|
274
|
+
query: "network.xyo.boundwitness",
|
|
275
|
+
schema: "network.xyo.error.module",
|
|
276
|
+
sources: [
|
|
277
|
+
routedQueryHash
|
|
278
|
+
]
|
|
279
|
+
};
|
|
280
|
+
reject(error);
|
|
281
|
+
return;
|
|
282
|
+
} finally {
|
|
283
|
+
this.stop();
|
|
284
|
+
}
|
|
285
|
+
}, "pollForResponse");
|
|
286
|
+
(0, import_forget.forget)(pollForResponse());
|
|
287
|
+
});
|
|
288
|
+
return context;
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Runs the background divine process on a loop with a delay
|
|
292
|
+
* specified by the `config.pollFrequency`
|
|
293
|
+
*/
|
|
294
|
+
poll() {
|
|
295
|
+
this._pollId = (0, import_timer.setTimeoutEx)(async () => {
|
|
296
|
+
try {
|
|
297
|
+
await this.processIncomingResponses();
|
|
298
|
+
} catch (e) {
|
|
299
|
+
this.logger?.error?.(`Error in main loop: ${e}`);
|
|
300
|
+
} finally {
|
|
301
|
+
if (this._pollId) (0, import_timer.clearTimeoutEx)(this._pollId);
|
|
302
|
+
this._pollId = void 0;
|
|
303
|
+
this.poll();
|
|
304
|
+
}
|
|
305
|
+
}, this.pollFrequency);
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Background process for processing incoming responses to previously issued queries
|
|
309
|
+
*/
|
|
310
|
+
processIncomingResponses = /* @__PURE__ */ __name(async () => {
|
|
311
|
+
const responseArchivist = await this.responsesArchivist();
|
|
312
|
+
if (responseArchivist) {
|
|
313
|
+
const responseBoundWitnessDiviner = await this.responsesDiviner();
|
|
314
|
+
if (responseBoundWitnessDiviner) {
|
|
315
|
+
const pendingCommands = [
|
|
316
|
+
...this.queryCache.entries()
|
|
317
|
+
].filter(([_, status]) => status === Pending);
|
|
318
|
+
await Promise.allSettled(pendingCommands.map(async ([sourceQuery, status]) => {
|
|
319
|
+
if (status === Pending) {
|
|
320
|
+
const divinerQuery = {
|
|
321
|
+
limit: 1,
|
|
322
|
+
order: "desc",
|
|
323
|
+
schema: import_diviner_boundwitness_model.BoundWitnessDivinerQuerySchema,
|
|
324
|
+
sourceQuery
|
|
325
|
+
};
|
|
326
|
+
const result = await responseBoundWitnessDiviner.divine([
|
|
327
|
+
divinerQuery
|
|
328
|
+
]);
|
|
329
|
+
if (result && result.length > 0) {
|
|
330
|
+
const response = result.find(import_boundwitness_model.isBoundWitnessWithMeta);
|
|
331
|
+
if (response && response?.$meta?.sourceQuery === sourceQuery) {
|
|
332
|
+
this.logger?.debug(`Found response to query: ${sourceQuery}`);
|
|
333
|
+
const payloads = response.payload_hashes?.length > 0 ? await responseArchivist.get(response.payload_hashes) : [];
|
|
334
|
+
this.queryCache.set(sourceQuery, [
|
|
335
|
+
response,
|
|
336
|
+
payloads,
|
|
337
|
+
[]
|
|
338
|
+
]);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}));
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
}, "processIncomingResponses");
|
|
346
|
+
start() {
|
|
347
|
+
if (this._pollCount === 0) {
|
|
348
|
+
this.poll();
|
|
349
|
+
}
|
|
350
|
+
this._pollCount++;
|
|
351
|
+
}
|
|
352
|
+
stop() {
|
|
353
|
+
this._pollCount--;
|
|
354
|
+
if (this._pollCount <= 0) {
|
|
355
|
+
if (this._pollId) (0, import_timer.clearTimeoutEx)(this._pollId);
|
|
356
|
+
this._pollId = void 0;
|
|
357
|
+
this._pollCount = 0;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
// src/AsyncQueryBus/AsyncQueryBusHost.ts
|
|
363
|
+
var import_array = require("@xylabs/array");
|
|
364
|
+
var import_assert3 = require("@xylabs/assert");
|
|
365
|
+
var import_timer2 = require("@xylabs/timer");
|
|
366
|
+
var import_boundwitness_model2 = require("@xyo-network/boundwitness-model");
|
|
367
|
+
var import_bridge_model = require("@xyo-network/bridge-model");
|
|
368
|
+
var import_diviner_boundwitness_model2 = require("@xyo-network/diviner-boundwitness-model");
|
|
369
|
+
var import_module_model2 = require("@xyo-network/module-model");
|
|
370
|
+
var import_payload_builder2 = require("@xyo-network/payload-builder");
|
|
371
|
+
var IDLE_POLLING_FREQUENCY_RATIO_MIN = 4;
|
|
372
|
+
var IDLE_POLLING_FREQUENCY_RATIO_MAX = 64;
|
|
373
|
+
var IDLE_POLLING_FREQUENCY_RATIO_DEFAULT = 16;
|
|
374
|
+
var IDLE_THRESHOLD_RATIO_MIN = 4;
|
|
375
|
+
var IDLE_THRESHOLD_RATIO_MAX = 64;
|
|
376
|
+
var IDLE_THRESHOLD_RATIO_DEFAULT = 16;
|
|
377
|
+
var AsyncQueryBusHost = class extends AsyncQueryBusBase {
|
|
378
|
+
static {
|
|
379
|
+
__name(this, "AsyncQueryBusHost");
|
|
380
|
+
}
|
|
381
|
+
_exposedAddresses = /* @__PURE__ */ new Set();
|
|
382
|
+
_exposeOptions = {};
|
|
383
|
+
_idle = false;
|
|
384
|
+
_lastQueryTime;
|
|
385
|
+
_pollId;
|
|
386
|
+
constructor(params) {
|
|
387
|
+
super(params);
|
|
388
|
+
}
|
|
389
|
+
get exposedAddresses() {
|
|
390
|
+
return this._exposedAddresses;
|
|
391
|
+
}
|
|
392
|
+
get idlePollFrequency() {
|
|
393
|
+
const frequency = this.config?.idlePollFrequency ?? IDLE_POLLING_FREQUENCY_RATIO_DEFAULT * this.pollFrequency;
|
|
394
|
+
if (frequency < this.pollFrequency * IDLE_POLLING_FREQUENCY_RATIO_MIN) {
|
|
395
|
+
return IDLE_POLLING_FREQUENCY_RATIO_MIN * this.pollFrequency;
|
|
396
|
+
}
|
|
397
|
+
if (frequency > this.pollFrequency * IDLE_POLLING_FREQUENCY_RATIO_MAX) {
|
|
398
|
+
return IDLE_POLLING_FREQUENCY_RATIO_MAX * this.pollFrequency;
|
|
399
|
+
}
|
|
400
|
+
return frequency;
|
|
401
|
+
}
|
|
402
|
+
get idleThreshold() {
|
|
403
|
+
const threshold = this.config?.idleThreshold ?? IDLE_THRESHOLD_RATIO_DEFAULT * this.idlePollFrequency;
|
|
404
|
+
if (threshold < this.idlePollFrequency * IDLE_THRESHOLD_RATIO_MIN) {
|
|
405
|
+
return IDLE_POLLING_FREQUENCY_RATIO_MIN * this.pollFrequency;
|
|
406
|
+
}
|
|
407
|
+
if (threshold > this.idlePollFrequency * IDLE_THRESHOLD_RATIO_MAX) {
|
|
408
|
+
return IDLE_POLLING_FREQUENCY_RATIO_MAX * this.pollFrequency;
|
|
409
|
+
}
|
|
410
|
+
return threshold;
|
|
411
|
+
}
|
|
412
|
+
get perAddressBatchQueryLimit() {
|
|
413
|
+
return this.config?.perAddressBatchQueryLimit ?? 10;
|
|
414
|
+
}
|
|
415
|
+
get started() {
|
|
416
|
+
return !!this._pollId;
|
|
417
|
+
}
|
|
418
|
+
expose(module2, options) {
|
|
419
|
+
const { failOnAlreadyExposed } = options ?? {};
|
|
420
|
+
if ((0, import_bridge_model.isBridgeInstance)(module2)) {
|
|
421
|
+
this.logger?.warn(`Attempted to expose a BridgeModule [${module2.id}] - Not exposing`);
|
|
422
|
+
} else {
|
|
423
|
+
(0, import_assert3.assertEx)(!failOnAlreadyExposed || !this._exposedAddresses.has(module2.address), () => `Address already exposed: ${module2.id} [${module2.address}]`);
|
|
424
|
+
this._exposedAddresses.add(module2.address);
|
|
425
|
+
this._exposeOptions[module2.address] = {
|
|
426
|
+
...options
|
|
427
|
+
};
|
|
428
|
+
this.logger?.debug(`${module2.id} exposed [${module2.address}]`);
|
|
429
|
+
return module2;
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
async listeningModules() {
|
|
433
|
+
const exposedModules = [
|
|
434
|
+
...this.config?.listeningModules ?? [],
|
|
435
|
+
...this.exposedAddresses.values()
|
|
436
|
+
];
|
|
437
|
+
const mods = await Promise.all(exposedModules.map(async (exposedModule) => (0, import_assert3.assertEx)((0, import_module_model2.asModuleInstance)(await (0, import_module_model2.resolveAddressToInstance)(this.rootModule, exposedModule)), () => `Unable to resolve listeningModule [${exposedModule}]`)));
|
|
438
|
+
return mods;
|
|
439
|
+
}
|
|
440
|
+
start() {
|
|
441
|
+
if (this.started) {
|
|
442
|
+
console.warn("AsyncQueryBus starting when already started");
|
|
443
|
+
}
|
|
444
|
+
this.poll();
|
|
445
|
+
}
|
|
446
|
+
stop() {
|
|
447
|
+
if (!this.started) {
|
|
448
|
+
console.warn("AsyncQueryBus stopping when already stopped");
|
|
449
|
+
}
|
|
450
|
+
if (this._pollId) (0, import_timer2.clearTimeoutEx)(this._pollId);
|
|
451
|
+
this._pollId = void 0;
|
|
452
|
+
}
|
|
453
|
+
async unexpose(id, validate = true) {
|
|
454
|
+
const module2 = (0, import_module_model2.asModuleInstance)(await this.rootModule.resolve(id, {
|
|
455
|
+
maxDepth: 10
|
|
456
|
+
}));
|
|
457
|
+
if (module2) {
|
|
458
|
+
(0, import_assert3.assertEx)(!validate || this._exposedAddresses.has(module2.address), () => `Address not exposed [${module2.address}][${module2.id}]`);
|
|
459
|
+
this._exposedAddresses.delete(module2.address);
|
|
460
|
+
delete this._exposeOptions[module2.address];
|
|
461
|
+
this.logger?.debug(`${module2.address} [${module2.id}] unexposed`);
|
|
462
|
+
}
|
|
463
|
+
return module2;
|
|
464
|
+
}
|
|
465
|
+
// eslint-disable-next-line complexity
|
|
466
|
+
callLocalModule = /* @__PURE__ */ __name(async (localModule, query) => {
|
|
467
|
+
this._idle = false;
|
|
468
|
+
this._lastQueryTime = Date.now();
|
|
469
|
+
const localModuleName = localModule.id;
|
|
470
|
+
const queryArchivist = (0, import_assert3.assertEx)(await this.queriesArchivist(), () => `Unable to contact queriesArchivist [${this.config?.intersect?.queries?.archivist}]`);
|
|
471
|
+
const responsesArchivist = (0, import_assert3.assertEx)(await this.responsesArchivist(), () => `Unable to contact responsesArchivist [${this.config?.intersect?.queries?.archivist}]`);
|
|
472
|
+
const queryDestination = query.$meta?.destination;
|
|
473
|
+
if (queryDestination && queryDestination?.includes(localModule.address)) {
|
|
474
|
+
const queryIndex = query.payload_hashes.indexOf(query.query);
|
|
475
|
+
if (queryIndex !== -1) {
|
|
476
|
+
const querySchema = query.payload_schemas[queryIndex];
|
|
477
|
+
if (localModule.queries.includes(querySchema)) {
|
|
478
|
+
const queryPayloads = await queryArchivist.get(query.payload_hashes);
|
|
479
|
+
this.params.onQueryFulfillStarted?.({
|
|
480
|
+
payloads: queryPayloads,
|
|
481
|
+
query
|
|
482
|
+
});
|
|
483
|
+
const queryPayloadsDict = await import_payload_builder2.PayloadBuilder.toAllHashMap(queryPayloads);
|
|
484
|
+
const queryHash = (await import_payload_builder2.PayloadBuilder.build(query)).$hash;
|
|
485
|
+
if (!(0, import_array.containsAll)(Object.keys(queryPayloadsDict), query.payload_hashes)) {
|
|
486
|
+
this.logger?.error(`Error processing command ${queryHash} for module ${localModuleName}, missing payloads`);
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
try {
|
|
490
|
+
const querySchema2 = queryPayloadsDict[query.query].schema;
|
|
491
|
+
this.logger?.debug(`Issuing query ${querySchema2} (${queryHash}) addressed to module: ${localModuleName}`);
|
|
492
|
+
const result = await localModule.query(query, queryPayloads, {
|
|
493
|
+
allowedQueries: this._exposeOptions[localModule.address]?.allowedQueries,
|
|
494
|
+
schema: import_module_model2.ModuleConfigSchema
|
|
495
|
+
});
|
|
496
|
+
const [bw, payloads, errors] = result;
|
|
497
|
+
this.logger?.debug(`Replying to query ${queryHash} addressed to module: ${localModuleName}`);
|
|
498
|
+
const insertResult = await responsesArchivist.insert([
|
|
499
|
+
bw,
|
|
500
|
+
...payloads,
|
|
501
|
+
...errors
|
|
502
|
+
]);
|
|
503
|
+
if (insertResult.length === 0) {
|
|
504
|
+
this.logger?.error(`Error replying to query ${queryHash} addressed to module: ${localModuleName}`);
|
|
505
|
+
}
|
|
506
|
+
if (query?.timestamp) {
|
|
507
|
+
await this.commitState(localModule.address, query.timestamp);
|
|
508
|
+
}
|
|
509
|
+
this.params.onQueryFulfillFinished?.({
|
|
510
|
+
payloads: queryPayloads,
|
|
511
|
+
query,
|
|
512
|
+
result,
|
|
513
|
+
status: "success"
|
|
514
|
+
});
|
|
515
|
+
} catch (error) {
|
|
516
|
+
this.params.onQueryFulfillFinished?.({
|
|
517
|
+
payloads: queryPayloads,
|
|
518
|
+
query,
|
|
519
|
+
status: "failure"
|
|
520
|
+
});
|
|
521
|
+
this.logger?.error(`Error processing query ${queryHash} for module ${localModuleName}: ${error}`);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
}, "callLocalModule");
|
|
527
|
+
/**
|
|
528
|
+
* Finds unprocessed commands addressed to the supplied address
|
|
529
|
+
* @param address The address to find commands for
|
|
530
|
+
*/
|
|
531
|
+
findQueriesToAddress = /* @__PURE__ */ __name(async (address) => {
|
|
532
|
+
const queriesDivinerId = (0, import_assert3.assertEx)(this.config?.intersect?.queries?.boundWitnessDiviner, () => "No queries Diviner defined");
|
|
533
|
+
const queriesBoundWitnessDiviner = await this.queriesDiviner();
|
|
534
|
+
if (queriesBoundWitnessDiviner) {
|
|
535
|
+
const prevState = await this.retrieveState(address);
|
|
536
|
+
const destination = [
|
|
537
|
+
address
|
|
538
|
+
];
|
|
539
|
+
const limit = this.perAddressBatchQueryLimit;
|
|
540
|
+
const divinerQuery = {
|
|
541
|
+
destination,
|
|
542
|
+
limit,
|
|
543
|
+
order: "asc",
|
|
544
|
+
schema: import_diviner_boundwitness_model2.BoundWitnessDivinerQuerySchema,
|
|
545
|
+
timestamp: prevState + 1
|
|
546
|
+
};
|
|
547
|
+
const result = await queriesBoundWitnessDiviner.divine([
|
|
548
|
+
divinerQuery
|
|
549
|
+
]);
|
|
550
|
+
const queries = result.filter(import_boundwitness_model2.isQueryBoundWitnessWithMeta);
|
|
551
|
+
const nextState = queries.length > 0 ? Math.max(...queries.map((c) => c.timestamp ?? prevState)) + 1 : Date.now();
|
|
552
|
+
await this.commitState(address, nextState);
|
|
553
|
+
this.logger?.debug("findQueriesToAddress", address, prevState, nextState);
|
|
554
|
+
return queries;
|
|
555
|
+
} else {
|
|
556
|
+
this.logger?.warn(`Unable to resolve queriesBoundWitnessDiviner [${queriesDivinerId}] [${await import_module_model2.ResolveHelper.traceModuleIdentifier(this.rootModule, queriesDivinerId)}]`);
|
|
557
|
+
}
|
|
558
|
+
}, "findQueriesToAddress");
|
|
559
|
+
/**
|
|
560
|
+
* Runs the background divine process on a loop with a delay
|
|
561
|
+
* specified by the `config.pollFrequency`
|
|
562
|
+
*/
|
|
563
|
+
poll() {
|
|
564
|
+
this._pollId = (0, import_timer2.setTimeoutEx)(async () => {
|
|
565
|
+
try {
|
|
566
|
+
await this.processIncomingQueries();
|
|
567
|
+
} catch (e) {
|
|
568
|
+
this.logger?.error?.(`Error in main loop: ${e}`);
|
|
569
|
+
} finally {
|
|
570
|
+
if (this._pollId) (0, import_timer2.clearTimeoutEx)(this._pollId);
|
|
571
|
+
this._pollId = void 0;
|
|
572
|
+
this.poll();
|
|
573
|
+
}
|
|
574
|
+
const now = Date.now();
|
|
575
|
+
if (this.idleThreshold < now - (this._lastQueryTime ?? now)) {
|
|
576
|
+
this._idle = true;
|
|
577
|
+
}
|
|
578
|
+
}, this._idle ? this.idlePollFrequency : this.pollFrequency);
|
|
579
|
+
}
|
|
580
|
+
/**
|
|
581
|
+
* Background process for checking for inbound queries
|
|
582
|
+
*/
|
|
583
|
+
processIncomingQueries = /* @__PURE__ */ __name(async () => {
|
|
584
|
+
this.logger?.debug("Checking for inbound queries");
|
|
585
|
+
const localModules = await this.listeningModules();
|
|
586
|
+
await Promise.allSettled(localModules.map(async (localModule) => {
|
|
587
|
+
try {
|
|
588
|
+
const localModuleName = localModule.id;
|
|
589
|
+
this.logger?.debug(`Checking for inbound queries to ${localModuleName} [${localModule.address}]`);
|
|
590
|
+
const queries = await this.findQueriesToAddress(localModule.address) ?? [];
|
|
591
|
+
if (queries.length === 0) return;
|
|
592
|
+
this.logger?.debug(`Found queries addressed to local module: ${localModuleName}`);
|
|
593
|
+
for (const query of queries) {
|
|
594
|
+
await this.callLocalModule(localModule, query);
|
|
595
|
+
}
|
|
596
|
+
} catch (error) {
|
|
597
|
+
this.logger?.error(`Error processing queries for address ${localModule.address}: ${error}`);
|
|
598
|
+
}
|
|
599
|
+
}));
|
|
600
|
+
}, "processIncomingQueries");
|
|
601
|
+
};
|
|
602
|
+
|
|
603
|
+
// src/AsyncQueryBus/ModuleHost/ModuleHost.ts
|
|
604
|
+
var AsyncQueryBusModuleHost = class extends AbstractModuleHost {
|
|
605
|
+
static {
|
|
606
|
+
__name(this, "AsyncQueryBusModuleHost");
|
|
607
|
+
}
|
|
608
|
+
_busHost;
|
|
609
|
+
constructor(params) {
|
|
610
|
+
super(params);
|
|
611
|
+
}
|
|
612
|
+
async start() {
|
|
613
|
+
const listeningModules = this.params.config.listeningModules ?? (await this.params.module.resolve("*", {
|
|
614
|
+
direction: "down"
|
|
615
|
+
})).map((m) => m.address);
|
|
616
|
+
this._busHost = new AsyncQueryBusHost({
|
|
617
|
+
config: {
|
|
618
|
+
...this.params.config,
|
|
619
|
+
listeningModules
|
|
620
|
+
},
|
|
621
|
+
logger: this.params.logger,
|
|
622
|
+
rootModule: this.params.module
|
|
623
|
+
});
|
|
624
|
+
this._busHost?.start();
|
|
625
|
+
}
|
|
626
|
+
stop() {
|
|
627
|
+
this._busHost?.stop();
|
|
628
|
+
}
|
|
629
|
+
};
|
|
630
|
+
|
|
631
|
+
// src/AsyncQueryBus/ModuleProxy/ModuleProxy.ts
|
|
632
|
+
var import_assert4 = require("@xylabs/assert");
|
|
633
|
+
var import_exists = require("@xylabs/exists");
|
|
634
|
+
var import_forget2 = require("@xylabs/forget");
|
|
635
|
+
var import_hex = require("@xylabs/hex");
|
|
636
|
+
var import_bridge_abstract = require("@xyo-network/bridge-abstract");
|
|
637
|
+
var import_module_model3 = require("@xyo-network/module-model");
|
|
638
|
+
var AsyncQueryBusModuleProxy = class _AsyncQueryBusModuleProxy extends import_bridge_abstract.AbstractModuleProxy {
|
|
639
|
+
static {
|
|
640
|
+
__name(this, "AsyncQueryBusModuleProxy");
|
|
641
|
+
}
|
|
642
|
+
static createCount = 0;
|
|
643
|
+
constructor(params) {
|
|
644
|
+
_AsyncQueryBusModuleProxy.createCount = _AsyncQueryBusModuleProxy.createCount + 1;
|
|
645
|
+
if (Math.floor(_AsyncQueryBusModuleProxy.createCount / 10) === _AsyncQueryBusModuleProxy.createCount / 10) {
|
|
646
|
+
console.log(`AsyncQueryBusModuleProxy.createCount: ${_AsyncQueryBusModuleProxy.createCount}`);
|
|
647
|
+
}
|
|
648
|
+
super(params);
|
|
649
|
+
}
|
|
650
|
+
get id() {
|
|
651
|
+
return this.address;
|
|
652
|
+
}
|
|
653
|
+
get modName() {
|
|
654
|
+
return void 0;
|
|
655
|
+
}
|
|
656
|
+
async proxyQueryHandler(query, payloads) {
|
|
657
|
+
if (this.archiving && this.isAllowedArchivingQuery(query.schema)) {
|
|
658
|
+
(0, import_forget2.forget)(this.storeToArchivists([
|
|
659
|
+
query,
|
|
660
|
+
...payloads ?? []
|
|
661
|
+
]));
|
|
662
|
+
}
|
|
663
|
+
const result = await this.params.busClient.send(this.address, query, payloads);
|
|
664
|
+
if (this.archiving && this.isAllowedArchivingQuery(query.schema)) {
|
|
665
|
+
(0, import_forget2.forget)(this.storeToArchivists(result.flat()));
|
|
666
|
+
}
|
|
667
|
+
return result;
|
|
668
|
+
}
|
|
669
|
+
async publicChildren() {
|
|
670
|
+
return (await Promise.all(Object.values(await this.childAddressMap()).filter(import_exists.exists).map((address) => this.resolve(address)))).filter(import_exists.exists);
|
|
671
|
+
}
|
|
672
|
+
async resolve(idOrFilter = "*", options = {}) {
|
|
673
|
+
const config = {
|
|
674
|
+
address: this.address,
|
|
675
|
+
dead: this.dead,
|
|
676
|
+
downResolver: this.downResolver,
|
|
677
|
+
logger: this.logger,
|
|
678
|
+
module: this,
|
|
679
|
+
transformers: this.moduleIdentifierTransformers,
|
|
680
|
+
upResolver: this.upResolver
|
|
681
|
+
};
|
|
682
|
+
if (idOrFilter === "*") {
|
|
683
|
+
return await this.publicChildren();
|
|
684
|
+
}
|
|
685
|
+
switch (typeof idOrFilter) {
|
|
686
|
+
case "string": {
|
|
687
|
+
const parts = idOrFilter.split(":");
|
|
688
|
+
const first = (0, import_assert4.assertEx)(parts.shift(), () => "Missing first");
|
|
689
|
+
const remainingPath = parts.join(":");
|
|
690
|
+
const address = (0, import_hex.isAddress)(first) ? first : this.childAddressByName(first);
|
|
691
|
+
if (!address) return void 0;
|
|
692
|
+
const firstInstance = await this.params.host.resolve(address);
|
|
693
|
+
return remainingPath ? await firstInstance?.resolve(remainingPath) : firstInstance;
|
|
694
|
+
}
|
|
695
|
+
case "object": {
|
|
696
|
+
return (await import_module_model3.ResolveHelper.resolve(config, idOrFilter, options)).filter((mod) => mod.address !== this.address);
|
|
697
|
+
}
|
|
698
|
+
default: {
|
|
699
|
+
return (await import_module_model3.ResolveHelper.resolve(config, idOrFilter, options)).filter((mod) => mod.address !== this.address);
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
async startHandler() {
|
|
704
|
+
return await super.startHandler();
|
|
705
|
+
}
|
|
706
|
+
};
|
|
707
|
+
|
|
708
|
+
// src/Schema.ts
|
|
709
|
+
var PubSubBridgeSchema = "network.xyo.bridge.pubsub";
|
|
710
|
+
|
|
711
|
+
// src/Config.ts
|
|
712
|
+
var PubSubBridgeConfigSchema = `${PubSubBridgeSchema}.config`;
|
|
713
|
+
|
|
714
|
+
// src/PubSubBridge.ts
|
|
715
|
+
var import_assert6 = require("@xylabs/assert");
|
|
716
|
+
var import_exists2 = require("@xylabs/exists");
|
|
717
|
+
var import_forget3 = require("@xylabs/forget");
|
|
718
|
+
var import_hex3 = require("@xylabs/hex");
|
|
719
|
+
var import_object3 = require("@xylabs/object");
|
|
720
|
+
var import_bridge_abstract3 = require("@xyo-network/bridge-abstract");
|
|
721
|
+
var import_module_model5 = require("@xyo-network/module-model");
|
|
722
|
+
var import_node_model = require("@xyo-network/node-model");
|
|
723
|
+
var import_payload_model = require("@xyo-network/payload-model");
|
|
724
|
+
var import_async_mutex3 = require("async-mutex");
|
|
725
|
+
|
|
726
|
+
// src/PubSubBridgeModuleResolver.ts
|
|
727
|
+
var import_assert5 = require("@xylabs/assert");
|
|
728
|
+
var import_hex2 = require("@xylabs/hex");
|
|
729
|
+
var import_account = require("@xyo-network/account");
|
|
730
|
+
var import_bridge_abstract2 = require("@xyo-network/bridge-abstract");
|
|
731
|
+
var import_config_payload_plugin = require("@xyo-network/config-payload-plugin");
|
|
732
|
+
var import_module_model4 = require("@xyo-network/module-model");
|
|
733
|
+
var import_async_mutex2 = require("async-mutex");
|
|
734
|
+
var import_lru_cache3 = require("lru-cache");
|
|
735
|
+
var PubSubBridgeModuleResolver = class extends import_bridge_abstract2.AbstractBridgeModuleResolver {
|
|
736
|
+
static {
|
|
737
|
+
__name(this, "PubSubBridgeModuleResolver");
|
|
738
|
+
}
|
|
739
|
+
_resolvedCache = new import_lru_cache3.LRUCache({
|
|
740
|
+
max: 1e3
|
|
741
|
+
});
|
|
742
|
+
_resolvedCacheMutex = new import_async_mutex2.Mutex();
|
|
743
|
+
async resolveHandler(id, options) {
|
|
744
|
+
const parentResult = await super.resolveHandler(id, options);
|
|
745
|
+
if (parentResult.length > 0) {
|
|
746
|
+
return parentResult;
|
|
747
|
+
}
|
|
748
|
+
const idParts = id.split(":");
|
|
749
|
+
const untransformedFirstPart = (0, import_assert5.assertEx)(idParts.shift(), () => "Missing module identifier");
|
|
750
|
+
const firstPart = await import_module_model4.ResolveHelper.transformModuleIdentifier(untransformedFirstPart);
|
|
751
|
+
(0, import_assert5.assertEx)((0, import_hex2.isAddress)(firstPart), () => `Invalid module address: ${firstPart}`);
|
|
752
|
+
const remainderParts = idParts.join(":");
|
|
753
|
+
const instance = await this._resolvedCacheMutex.runExclusive(async () => {
|
|
754
|
+
const cachedMod = this._resolvedCache.get(firstPart);
|
|
755
|
+
if (cachedMod) {
|
|
756
|
+
const result2 = idParts.length <= 0 ? cachedMod : cachedMod.resolve(remainderParts, {
|
|
757
|
+
...options,
|
|
758
|
+
maxDepth: (options?.maxDepth ?? 5) - 1
|
|
759
|
+
});
|
|
760
|
+
return result2;
|
|
761
|
+
}
|
|
762
|
+
const account = import_account.Account.randomSync();
|
|
763
|
+
const finalParams = {
|
|
764
|
+
account,
|
|
765
|
+
archiving: this.params.archiving,
|
|
766
|
+
busClient: this.params.busClient,
|
|
767
|
+
config: {
|
|
768
|
+
schema: import_module_model4.ModuleConfigSchema
|
|
769
|
+
},
|
|
770
|
+
host: this,
|
|
771
|
+
moduleAddress: firstPart,
|
|
772
|
+
onQuerySendFinished: this.params.onQuerySendFinished,
|
|
773
|
+
onQuerySendStarted: this.params.onQuerySendStarted
|
|
774
|
+
};
|
|
775
|
+
const proxy = new AsyncQueryBusModuleProxy(finalParams);
|
|
776
|
+
const state = await proxy.state();
|
|
777
|
+
if (state) {
|
|
778
|
+
const configSchema = state.find((payload) => payload.schema === import_config_payload_plugin.ConfigSchema)?.config;
|
|
779
|
+
const config = (0, import_assert5.assertEx)(state.find((payload) => payload.schema === configSchema), () => "Unable to locate config");
|
|
780
|
+
proxy.setConfig(config);
|
|
781
|
+
}
|
|
782
|
+
await proxy.start?.();
|
|
783
|
+
const wrapped = (0, import_bridge_abstract2.wrapModuleWithType)(proxy, account);
|
|
784
|
+
(0, import_assert5.assertEx)((0, import_module_model4.asModuleInstance)(wrapped, {}), () => `Failed to asModuleInstance [${id}]`);
|
|
785
|
+
this._resolvedCache.set(wrapped.address, wrapped);
|
|
786
|
+
return wrapped;
|
|
787
|
+
});
|
|
788
|
+
const result = remainderParts.length > 0 ? await instance.resolve(remainderParts, options) : instance;
|
|
789
|
+
return result ? [
|
|
790
|
+
result
|
|
791
|
+
] : [];
|
|
792
|
+
}
|
|
793
|
+
};
|
|
794
|
+
|
|
795
|
+
// src/PubSubBridge.ts
|
|
796
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
797
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
798
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
799
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
800
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
801
|
+
}
|
|
802
|
+
__name(_ts_decorate, "_ts_decorate");
|
|
803
|
+
var moduleName = "PubSubBridge";
|
|
804
|
+
var PubSubBridge = class extends import_bridge_abstract3.AbstractBridge {
|
|
805
|
+
static {
|
|
806
|
+
__name(this, "PubSubBridge");
|
|
807
|
+
}
|
|
808
|
+
static configSchemas = [
|
|
809
|
+
...super.configSchemas,
|
|
810
|
+
PubSubBridgeConfigSchema
|
|
811
|
+
];
|
|
812
|
+
static defaultConfigSchema = PubSubBridgeConfigSchema;
|
|
813
|
+
_configRootAddress = "";
|
|
814
|
+
_configStateStoreArchivist = "";
|
|
815
|
+
_configStateStoreBoundWitnessDiviner = "";
|
|
816
|
+
_exposedAddresses = [];
|
|
817
|
+
_lastState;
|
|
818
|
+
_busClient;
|
|
819
|
+
_busHost;
|
|
820
|
+
_discoverRootsMutex = new import_async_mutex3.Mutex();
|
|
821
|
+
_resolver;
|
|
822
|
+
get resolver() {
|
|
823
|
+
this._resolver = this._resolver ?? new PubSubBridgeModuleResolver({
|
|
824
|
+
additionalSigners: this.additionalSigners,
|
|
825
|
+
archiving: {
|
|
826
|
+
...this.archiving,
|
|
827
|
+
resolveArchivists: this.resolveArchivingArchivists.bind(this)
|
|
828
|
+
},
|
|
829
|
+
bridge: this,
|
|
830
|
+
busClient: (0, import_assert6.assertEx)(this.busClient(), () => "busClient not configured"),
|
|
831
|
+
onQuerySendFinished: /* @__PURE__ */ __name((args) => {
|
|
832
|
+
(0, import_forget3.forget)(this.emit("querySendFinished", {
|
|
833
|
+
module: this,
|
|
834
|
+
...args
|
|
835
|
+
}));
|
|
836
|
+
}, "onQuerySendFinished"),
|
|
837
|
+
onQuerySendStarted: /* @__PURE__ */ __name((args) => {
|
|
838
|
+
(0, import_forget3.forget)(this.emit("querySendStarted", {
|
|
839
|
+
module: this,
|
|
840
|
+
...args
|
|
841
|
+
}));
|
|
842
|
+
}, "onQuerySendStarted"),
|
|
843
|
+
root: this,
|
|
844
|
+
wrapperAccount: this.account
|
|
845
|
+
});
|
|
846
|
+
return this._resolver;
|
|
847
|
+
}
|
|
848
|
+
get moduleName() {
|
|
849
|
+
return this.modName ?? moduleName;
|
|
850
|
+
}
|
|
851
|
+
async connect(id, maxDepth = 5) {
|
|
852
|
+
const transformedId = (0, import_assert6.assertEx)(await import_module_model5.ResolveHelper.transformModuleIdentifier(id), () => `Unable to transform module identifier: ${id}`);
|
|
853
|
+
const existingInstance = await this.resolve(transformedId);
|
|
854
|
+
if (existingInstance) {
|
|
855
|
+
return existingInstance.address;
|
|
856
|
+
}
|
|
857
|
+
const [instance] = await this.resolver.resolveHandler(id);
|
|
858
|
+
return await this.connectInstance(instance, maxDepth);
|
|
859
|
+
}
|
|
860
|
+
async disconnect(id) {
|
|
861
|
+
const transformedId = (0, import_assert6.assertEx)(await import_module_model5.ResolveHelper.transformModuleIdentifier(id), () => `Unable to transform module identifier: ${id}`);
|
|
862
|
+
const instance = await this.resolve(transformedId);
|
|
863
|
+
if (instance) {
|
|
864
|
+
this.downResolver.remove(instance.address);
|
|
865
|
+
return instance.address;
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
async exposeChild(mod, options) {
|
|
869
|
+
const { maxDepth = 5 } = options ?? {};
|
|
870
|
+
console.log(`exposeChild: ${mod.address} ${mod?.id} ${maxDepth}`);
|
|
871
|
+
const host = (0, import_assert6.assertEx)(this.busHost(), () => "Not configured as a host");
|
|
872
|
+
host.expose(mod);
|
|
873
|
+
const children = maxDepth > 0 ? await mod.publicChildren?.() ?? [] : [];
|
|
874
|
+
this.logger.log(`childrenToExpose [${mod.id}][${mod.address}]: ${(0, import_object3.toJsonString)(children.map((child) => child.id))}`);
|
|
875
|
+
const exposedChildren = (await Promise.all(children.map((child) => this.exposeChild(child, {
|
|
876
|
+
maxDepth: maxDepth - 1,
|
|
877
|
+
required: false
|
|
878
|
+
})))).flat().filter(import_exists2.exists);
|
|
879
|
+
const allExposed = [
|
|
880
|
+
mod,
|
|
881
|
+
...exposedChildren
|
|
882
|
+
];
|
|
883
|
+
for (const exposedMod of allExposed) this.logger?.log(`exposed: ${exposedMod.address} [${mod.id}]`);
|
|
884
|
+
return allExposed;
|
|
885
|
+
}
|
|
886
|
+
async exposeHandler(address, options) {
|
|
887
|
+
const { required = true } = options ?? {};
|
|
888
|
+
const mod = await (0, import_module_model5.resolveAddressToInstanceUp)(this, address);
|
|
889
|
+
console.log(`exposeHandler: ${address} ${mod?.id}`);
|
|
890
|
+
if (required && !mod) {
|
|
891
|
+
throw new Error(`Unable to find required module: ${address}`);
|
|
892
|
+
}
|
|
893
|
+
if (mod) {
|
|
894
|
+
return this.exposeChild(mod, options);
|
|
895
|
+
}
|
|
896
|
+
return [];
|
|
897
|
+
}
|
|
898
|
+
exposedHandler() {
|
|
899
|
+
const exposedSet = this.busHost()?.exposedAddresses;
|
|
900
|
+
return exposedSet ? [
|
|
901
|
+
...exposedSet
|
|
902
|
+
] : [];
|
|
903
|
+
}
|
|
904
|
+
async getRoots(force) {
|
|
905
|
+
return await this._discoverRootsMutex.runExclusive(async () => {
|
|
906
|
+
if (this._roots === void 0 || force) {
|
|
907
|
+
const rootAddresses = (await Promise.all((this.config.roots ?? []).map((id) => {
|
|
908
|
+
try {
|
|
909
|
+
return import_module_model5.ResolveHelper.transformModuleIdentifier(id);
|
|
910
|
+
} catch (ex) {
|
|
911
|
+
this.logger?.warn("Unable to transform module identifier:", id, ex);
|
|
912
|
+
return;
|
|
913
|
+
}
|
|
914
|
+
}))).filter(import_exists2.exists);
|
|
915
|
+
const rootInstances = (await Promise.all(rootAddresses.map(async (root) => {
|
|
916
|
+
try {
|
|
917
|
+
return await this.resolver.resolveHandler(root);
|
|
918
|
+
} catch (ex) {
|
|
919
|
+
this.logger?.warn("Unable to resolve root:", root, ex);
|
|
920
|
+
return;
|
|
921
|
+
}
|
|
922
|
+
}))).flat().filter(import_exists2.exists);
|
|
923
|
+
for (const instance of rootInstances) {
|
|
924
|
+
this.downResolver.add(instance);
|
|
925
|
+
}
|
|
926
|
+
this._roots = rootInstances;
|
|
927
|
+
}
|
|
928
|
+
return this._roots;
|
|
929
|
+
});
|
|
930
|
+
}
|
|
931
|
+
async resolve(idOrFilter = "*", options = {}) {
|
|
932
|
+
const roots = this._roots ?? [];
|
|
933
|
+
const workingSet = options.direction === "up" ? [
|
|
934
|
+
this
|
|
935
|
+
] : [
|
|
936
|
+
...roots,
|
|
937
|
+
this
|
|
938
|
+
];
|
|
939
|
+
if (idOrFilter === "*") {
|
|
940
|
+
const remainingDepth = (options.maxDepth ?? 1) - 1;
|
|
941
|
+
return remainingDepth <= 0 ? workingSet : [
|
|
942
|
+
...workingSet,
|
|
943
|
+
...(await Promise.all(roots.map((mod) => mod.resolve("*", {
|
|
944
|
+
...options,
|
|
945
|
+
maxDepth: remainingDepth
|
|
946
|
+
})))).flat()
|
|
947
|
+
];
|
|
948
|
+
}
|
|
949
|
+
switch (typeof idOrFilter) {
|
|
950
|
+
case "string": {
|
|
951
|
+
const parts = idOrFilter.split(":");
|
|
952
|
+
const first = (0, import_assert6.assertEx)(parts.shift(), () => "Missing first part");
|
|
953
|
+
const firstInstance = (0, import_hex3.isAddress)(first) ? await (0, import_module_model5.resolveAddressToInstance)(this, first, void 0, [], options.direction) : this._roots?.find((mod) => mod.id === first);
|
|
954
|
+
return parts.length === 0 ? firstInstance : firstInstance?.resolve(parts.join(":"), options);
|
|
955
|
+
}
|
|
956
|
+
case "object": {
|
|
957
|
+
const results = [];
|
|
958
|
+
if ((0, import_module_model5.isAddressModuleFilter)(idOrFilter)) {
|
|
959
|
+
for (const mod of workingSet) {
|
|
960
|
+
if (mod.modName && idOrFilter.address.includes(mod.address)) results.push(mod);
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
return results;
|
|
964
|
+
}
|
|
965
|
+
default: {
|
|
966
|
+
return;
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
async startHandler() {
|
|
971
|
+
this.busHost()?.start();
|
|
972
|
+
return await super.startHandler();
|
|
973
|
+
}
|
|
974
|
+
async unexposeHandler(id, options) {
|
|
975
|
+
const { maxDepth = 2, required = true } = options ?? {};
|
|
976
|
+
const host = (0, import_assert6.assertEx)(this.busHost(), () => "Not configured as a host");
|
|
977
|
+
const module2 = await host.unexpose(id, required);
|
|
978
|
+
if (module2) {
|
|
979
|
+
const children = maxDepth > 0 ? await module2.publicChildren?.() ?? [] : [];
|
|
980
|
+
const exposedChildren = (await Promise.all(children.map((child) => this.unexposeHandler(child.address, {
|
|
981
|
+
maxDepth: maxDepth - 1,
|
|
982
|
+
required: false
|
|
983
|
+
})))).flat().filter(import_exists2.exists);
|
|
984
|
+
return [
|
|
985
|
+
module2,
|
|
986
|
+
...exposedChildren
|
|
987
|
+
];
|
|
988
|
+
}
|
|
989
|
+
return [];
|
|
990
|
+
}
|
|
991
|
+
busClient() {
|
|
992
|
+
if (!this._busClient && this.config.client) {
|
|
993
|
+
this._busClient = new AsyncQueryBusClient({
|
|
994
|
+
config: this.config.client,
|
|
995
|
+
logger: this.logger,
|
|
996
|
+
rootModule: this
|
|
997
|
+
});
|
|
998
|
+
}
|
|
999
|
+
return this._busClient;
|
|
1000
|
+
}
|
|
1001
|
+
busHost() {
|
|
1002
|
+
if (!this._busHost && this.config.host) {
|
|
1003
|
+
this._busHost = new AsyncQueryBusHost({
|
|
1004
|
+
config: this.config.host,
|
|
1005
|
+
logger: this.logger,
|
|
1006
|
+
onQueryFulfillFinished: /* @__PURE__ */ __name((args) => {
|
|
1007
|
+
if (this.archiving && this.isAllowedArchivingQuery(args.query.schema)) {
|
|
1008
|
+
(0, import_forget3.forget)(this.storeToArchivists(args.result?.flat() ?? []));
|
|
1009
|
+
}
|
|
1010
|
+
(0, import_forget3.forget)(this.emit("queryFulfillFinished", {
|
|
1011
|
+
module: this,
|
|
1012
|
+
...args
|
|
1013
|
+
}));
|
|
1014
|
+
}, "onQueryFulfillFinished"),
|
|
1015
|
+
onQueryFulfillStarted: /* @__PURE__ */ __name((args) => {
|
|
1016
|
+
if (this.archiving && this.isAllowedArchivingQuery(args.query.schema)) {
|
|
1017
|
+
(0, import_forget3.forget)(this.storeToArchivists([
|
|
1018
|
+
args.query,
|
|
1019
|
+
...args.payloads ?? []
|
|
1020
|
+
]));
|
|
1021
|
+
}
|
|
1022
|
+
(0, import_forget3.forget)(this.emit("queryFulfillStarted", {
|
|
1023
|
+
module: this,
|
|
1024
|
+
...args
|
|
1025
|
+
}));
|
|
1026
|
+
}, "onQueryFulfillStarted"),
|
|
1027
|
+
rootModule: this
|
|
1028
|
+
});
|
|
1029
|
+
}
|
|
1030
|
+
return this._busHost;
|
|
1031
|
+
}
|
|
1032
|
+
async connectInstance(instance, maxDepth = 5) {
|
|
1033
|
+
if (instance) {
|
|
1034
|
+
this.downResolver.add(instance);
|
|
1035
|
+
if (maxDepth > 0) {
|
|
1036
|
+
const node = (0, import_node_model.asNodeInstance)(instance);
|
|
1037
|
+
if (node) {
|
|
1038
|
+
const state = await node.state();
|
|
1039
|
+
const children = (state?.filter((0, import_payload_model.isPayloadOfSchemaType)(import_module_model5.AddressSchema)).map((s) => s.address) ?? []).filter((a) => a !== instance.address);
|
|
1040
|
+
await Promise.all(children.map((child) => this.connect(child, maxDepth - 1)));
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
this.logger?.log(`Connect: ${instance.id}`);
|
|
1044
|
+
return instance.address;
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
stopHandler(_timeout) {
|
|
1048
|
+
this.busHost()?.stop();
|
|
1049
|
+
return true;
|
|
1050
|
+
}
|
|
1051
|
+
};
|
|
1052
|
+
PubSubBridge = _ts_decorate([
|
|
1053
|
+
(0, import_module_model5.creatableModule)()
|
|
1054
|
+
], PubSubBridge);
|
|
2
1055
|
//# sourceMappingURL=index.cjs.map
|