@xyo-network/bridge-pub-sub 2.94.15 → 2.94.16

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.
Files changed (42) hide show
  1. package/dist/browser/AsyncQueryBus/AsyncQueryBusBase.d.cts +4 -0
  2. package/dist/browser/AsyncQueryBus/AsyncQueryBusBase.d.cts.map +1 -1
  3. package/dist/browser/AsyncQueryBus/AsyncQueryBusBase.d.mts +4 -0
  4. package/dist/browser/AsyncQueryBus/AsyncQueryBusBase.d.mts.map +1 -1
  5. package/dist/browser/AsyncQueryBus/AsyncQueryBusBase.d.ts +4 -0
  6. package/dist/browser/AsyncQueryBus/AsyncQueryBusBase.d.ts.map +1 -1
  7. package/dist/browser/AsyncQueryBus/AsyncQueryBusHost.d.cts +24 -3
  8. package/dist/browser/AsyncQueryBus/AsyncQueryBusHost.d.cts.map +1 -1
  9. package/dist/browser/AsyncQueryBus/AsyncQueryBusHost.d.mts +24 -3
  10. package/dist/browser/AsyncQueryBus/AsyncQueryBusHost.d.mts.map +1 -1
  11. package/dist/browser/AsyncQueryBus/AsyncQueryBusHost.d.ts +24 -3
  12. package/dist/browser/AsyncQueryBus/AsyncQueryBusHost.d.ts.map +1 -1
  13. package/dist/browser/PubSubBridge.d.cts.map +1 -1
  14. package/dist/browser/PubSubBridge.d.mts.map +1 -1
  15. package/dist/browser/PubSubBridge.d.ts.map +1 -1
  16. package/dist/browser/index.cjs +130 -101
  17. package/dist/browser/index.cjs.map +1 -1
  18. package/dist/browser/index.js +130 -101
  19. package/dist/browser/index.js.map +1 -1
  20. package/dist/node/AsyncQueryBus/AsyncQueryBusBase.d.cts +4 -0
  21. package/dist/node/AsyncQueryBus/AsyncQueryBusBase.d.cts.map +1 -1
  22. package/dist/node/AsyncQueryBus/AsyncQueryBusBase.d.mts +4 -0
  23. package/dist/node/AsyncQueryBus/AsyncQueryBusBase.d.mts.map +1 -1
  24. package/dist/node/AsyncQueryBus/AsyncQueryBusBase.d.ts +4 -0
  25. package/dist/node/AsyncQueryBus/AsyncQueryBusBase.d.ts.map +1 -1
  26. package/dist/node/AsyncQueryBus/AsyncQueryBusHost.d.cts +24 -3
  27. package/dist/node/AsyncQueryBus/AsyncQueryBusHost.d.cts.map +1 -1
  28. package/dist/node/AsyncQueryBus/AsyncQueryBusHost.d.mts +24 -3
  29. package/dist/node/AsyncQueryBus/AsyncQueryBusHost.d.mts.map +1 -1
  30. package/dist/node/AsyncQueryBus/AsyncQueryBusHost.d.ts +24 -3
  31. package/dist/node/AsyncQueryBus/AsyncQueryBusHost.d.ts.map +1 -1
  32. package/dist/node/PubSubBridge.d.cts.map +1 -1
  33. package/dist/node/PubSubBridge.d.mts.map +1 -1
  34. package/dist/node/PubSubBridge.d.ts.map +1 -1
  35. package/dist/node/index.cjs +134 -105
  36. package/dist/node/index.cjs.map +1 -1
  37. package/dist/node/index.js +134 -105
  38. package/dist/node/index.js.map +1 -1
  39. package/package.json +19 -18
  40. package/src/AsyncQueryBus/AsyncQueryBusBase.ts +85 -72
  41. package/src/AsyncQueryBus/AsyncQueryBusHost.ts +29 -14
  42. package/src/PubSubBridge.ts +21 -20
@@ -4,7 +4,7 @@ import { Address } from '@xylabs/hex'
4
4
  import { clearTimeoutEx, setTimeoutEx } from '@xylabs/timer'
5
5
  import { isQueryBoundWitnessWithMeta, QueryBoundWitness } from '@xyo-network/boundwitness-model'
6
6
  import { BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model'
7
- import { asModuleInstance, ModuleConfigSchema, ModuleInstance } from '@xyo-network/module-model'
7
+ import { asModuleInstance, ModuleConfigSchema, ModuleIdentifier, ModuleInstance } from '@xyo-network/module-model'
8
8
  import { PayloadBuilder } from '@xyo-network/payload-builder'
9
9
  import { Schema, WithMeta } from '@xyo-network/payload-model'
10
10
 
@@ -14,6 +14,7 @@ import { AsyncQueryBusHostParams } from './model'
14
14
  export interface ExposeOptions {
15
15
  allowedQueries?: Schema[]
16
16
  failOnAlreadyExposed?: boolean
17
+ required?: boolean
17
18
  }
18
19
 
19
20
  export class AsyncQueryBusHost<TParams extends AsyncQueryBusHostParams = AsyncQueryBusHostParams> extends AsyncQueryBusBase<TParams> {
@@ -37,19 +38,29 @@ export class AsyncQueryBusHost<TParams extends AsyncQueryBusHostParams = AsyncQu
37
38
  return !!this._pollId
38
39
  }
39
40
 
40
- expose(address: Address, options: ExposeOptions = {}) {
41
- const { failOnAlreadyExposed } = options
42
- assertEx(!failOnAlreadyExposed || !this._exposedAddresses.has(address), () => `Address already exposed [${address}]`)
43
- this._exposedAddresses.add(address)
44
- this._exposeOptions[address] = { ...options }
45
- this.logger?.debug(`${address} exposed`)
41
+ async expose(id: ModuleIdentifier, options?: ExposeOptions) {
42
+ const { failOnAlreadyExposed, required = true } = options ?? {}
43
+ const module = asModuleInstance(await this.resolver.resolve(id, { maxDepth: 10 }))
44
+ if (!module && required) {
45
+ throw new Error(`Unable to resolve module to expose [${id}]`)
46
+ }
47
+ if (module) {
48
+ assertEx(!failOnAlreadyExposed || !this._exposedAddresses.has(module.address), () => `Address already exposed: ${id} [${module.address}]`)
49
+ this._exposedAddresses.add(module.address)
50
+ this._exposeOptions[module.address] = { ...options }
51
+ this.logger?.debug(`${id} exposed [${module.address}]`)
52
+ return module
53
+ }
46
54
  }
47
55
 
48
56
  async listeningModules(): Promise<ModuleInstance[]> {
49
57
  const exposedModules = [...(this.config?.listeningModules ?? []), ...this.exposedAddresses.values()]
50
58
  const mods = await Promise.all(
51
- exposedModules.map(async (listeningModule) =>
52
- assertEx(asModuleInstance(await this.resolver.resolve(listeningModule)), () => `Unable to resolve listeningModule [${listeningModule}]`),
59
+ exposedModules.map(async (exposedModule) =>
60
+ assertEx(
61
+ asModuleInstance(await this.resolver.resolve(exposedModule, { maxDepth: 10 })),
62
+ () => `Unable to resolve listeningModule [${exposedModule}]`,
63
+ ),
53
64
  ),
54
65
  )
55
66
  return mods
@@ -70,11 +81,15 @@ export class AsyncQueryBusHost<TParams extends AsyncQueryBusHostParams = AsyncQu
70
81
  this._pollId = undefined
71
82
  }
72
83
 
73
- unexpose(address: Address, validate = true) {
74
- assertEx(!validate || this._exposedAddresses.has(address), () => `Address not exposed [${address}]`)
75
- this._exposedAddresses.delete(address)
76
- delete this._exposeOptions[address]
77
- this.logger?.debug(`${address} unexposed`)
84
+ async unexpose(id: ModuleIdentifier, validate = true) {
85
+ const module = asModuleInstance(await this.resolver.resolve(id, { maxDepth: 10 }))
86
+ if (module) {
87
+ assertEx(!validate || this._exposedAddresses.has(module.address), () => `Address not exposed [${module.address}][${module.id}]`)
88
+ this._exposedAddresses.delete(module.address)
89
+ delete this._exposeOptions[module.address]
90
+ this.logger?.debug(`${module.address} [${module.id}] unexposed`)
91
+ }
92
+ return module
78
93
  }
79
94
 
80
95
  protected callLocalModule = async (localModule: ModuleInstance, query: WithMeta<QueryBoundWitness>) => {
@@ -3,7 +3,7 @@ import { exists } from '@xylabs/exists'
3
3
  import { Address } from '@xylabs/hex'
4
4
  import { AbstractBridge } from '@xyo-network/abstract-bridge'
5
5
  import { BridgeExposeOptions, BridgeModule, BridgeUnexposeOptions } from '@xyo-network/bridge-model'
6
- import { creatableModule, ModuleFilterOptions, ModuleIdentifier, ModuleInstance, ModuleResolverInstance } from '@xyo-network/module-model'
6
+ import { creatableModule, ModuleIdentifier, ModuleInstance, ModuleResolverInstance } from '@xyo-network/module-model'
7
7
  import { LRUCache } from 'lru-cache'
8
8
 
9
9
  import { AsyncQueryBusClient, AsyncQueryBusHost } from './AsyncQueryBus'
@@ -55,16 +55,17 @@ export class PubSubBridge<TParams extends PubSubBridgeParams = PubSubBridgeParam
55
55
  }
56
56
 
57
57
  async exposeHandler(id: ModuleIdentifier, options?: BridgeExposeOptions | undefined): Promise<ModuleInstance[]> {
58
- const { maxDepth = 2, direction = 'all' } = options ?? {}
59
- const module = assertEx(await super.resolve(id), () => `Expose failed to locate module [${id}]`)
58
+ const { maxDepth = 2, direction = 'all', required = true } = options ?? {}
59
+ const host = assertEx(this.busHost(), () => 'Not configured as a host')
60
+ const module = await host.expose(id, { required })
60
61
  if (module) {
61
- const host = assertEx(this.busHost(), () => 'Not configured as a host')
62
- host.expose(module.address)
63
- const children = await module.resolve('*', { direction, maxDepth, visibility: 'public' })
64
- for (const child of children) {
65
- host.expose(child.address)
66
- }
67
- return [module, ...children]
62
+ const children = maxDepth > 0 ? await module.resolve('*', { direction, maxDepth, visibility: 'public' }) : []
63
+ const exposedChildren = (
64
+ await Promise.all(children.map((child) => this.exposeHandler(child.address, { maxDepth: maxDepth - 1, required: false })))
65
+ )
66
+ .flat()
67
+ .filter(exists)
68
+ return [module, ...exposedChildren]
68
69
  }
69
70
  return []
70
71
  }
@@ -80,17 +81,17 @@ export class PubSubBridge<TParams extends PubSubBridgeParams = PubSubBridgeParam
80
81
  }
81
82
 
82
83
  async unexposeHandler(id: ModuleIdentifier, options?: BridgeUnexposeOptions | undefined): Promise<ModuleInstance[]> {
83
- const { maxDepth = 10, direction } = options ?? {}
84
- const filterOptions: ModuleFilterOptions = { direction }
85
- const module = await super.resolve(id, filterOptions)
84
+ const { maxDepth = 2, direction = 'all', required = true } = options ?? {}
85
+ const host = assertEx(this.busHost(), () => 'Not configured as a host')
86
+ const module = await host.unexpose(id, required)
86
87
  if (module) {
87
- const host = assertEx(this.busHost(), () => 'Not configured as a host')
88
- host.unexpose(module.address)
89
- const children = await module.resolve('*', { direction, maxDepth, visibility: 'public' })
90
- for (const child of children) {
91
- host.unexpose(child.address)
92
- }
93
- return [module, ...children]
88
+ const children = maxDepth > 0 ? await module.resolve('*', { direction, maxDepth, visibility: 'public' }) : []
89
+ const exposedChildren = (
90
+ await Promise.all(children.map((child) => this.unexposeHandler(child.address, { maxDepth: maxDepth - 1, required: false })))
91
+ )
92
+ .flat()
93
+ .filter(exists)
94
+ return [module, ...exposedChildren]
94
95
  }
95
96
  return []
96
97
  }