@xyo-network/bridge 2.46.2 → 2.46.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,103 @@
1
+ import { assertEx } from '@xylabs/assert'
2
+ import { AddressPayload, AddressSchema } from '@xyo-network/address-payload-plugin'
3
+ import { AbstractModule, CompositeModuleResolver, ModuleWrapper } from '@xyo-network/module'
4
+ import { Module, ModuleFilter } from '@xyo-network/module-model'
5
+ import compact from 'lodash/compact'
6
+ import flatten from 'lodash/flatten'
7
+
8
+ import { HttpBridge } from './HttpBridge'
9
+ import { HttpBridgeConfigSchema } from './HttpBridgeConfig'
10
+
11
+ interface LocalModuleFilter {
12
+ config?: string[]
13
+ query?: string[][]
14
+ }
15
+
16
+ interface RemoteModuleFilter {
17
+ address?: string[]
18
+ name?: string[]
19
+ }
20
+
21
+ export class RemoteModuleResolver extends CompositeModuleResolver {
22
+ private resolvedModules: Record<string, HttpBridge> = {}
23
+
24
+ // TODO: Allow optional ctor param for supplying address for nested Nodes
25
+ // protected readonly address?: string,
26
+ constructor(protected readonly bridge: HttpBridge) {
27
+ super()
28
+ }
29
+
30
+ public get isModuleResolver(): boolean {
31
+ return true
32
+ }
33
+
34
+ add(module: Module, name?: string | undefined): this
35
+ add(module: Module[], name?: string[] | undefined): this
36
+ add(module: Module | Module[], name?: string | string[] | undefined): this
37
+ add(_module: unknown, _name?: unknown): this {
38
+ throw new Error('Method not implemented.')
39
+ }
40
+
41
+ remove(name: string | string[]): this
42
+ remove(address: string | string[]): this
43
+ remove(_address: unknown): this {
44
+ throw new Error('Method not implemented.')
45
+ }
46
+
47
+ async resolve(filter?: ModuleFilter): Promise<AbstractModule[]> {
48
+ const mods = await Promise.all(flatten(await this.resolveRemoteModules(filter)))
49
+ return this.filterLocalModules(mods, filter)
50
+ }
51
+
52
+ private filterLocalModules(mods: AbstractModule[], filter?: LocalModuleFilter): AbstractModule[] {
53
+ // TODO: Handle filter?.query
54
+ if (filter?.query) throw new Error('Filtering by query not yet implemented by this resolver')
55
+ const config = filter?.config
56
+ const filtered = config?.length ? mods.filter((mod) => config.includes(mod.config.schema)) : mods
57
+ return filtered
58
+ }
59
+
60
+ private async getRemoteAddresses() {
61
+ const nodeWrapper = assertEx(ModuleWrapper.wrap(this.bridge), 'nodeUri can not be wrapped')
62
+ const discover = await nodeWrapper.discover()
63
+ return compact(
64
+ discover.map((payload) => {
65
+ if (payload.schema === AddressSchema) {
66
+ const schemaPayload = payload as AddressPayload
67
+ return schemaPayload.address
68
+ } else {
69
+ return null
70
+ }
71
+ }),
72
+ )
73
+ }
74
+
75
+ private async resolveByAddress(targetAddress: string): Promise<HttpBridge> {
76
+ const cached = this.resolvedModules[targetAddress]
77
+ if (cached) return cached
78
+ const mod = await HttpBridge.create({
79
+ config: { nodeUri: this.bridge.nodeUri, schema: HttpBridgeConfigSchema, targetAddress },
80
+ })
81
+ this.resolvedModules[targetAddress] = mod
82
+ return mod
83
+ }
84
+
85
+ private async resolveByName(name: string): Promise<HttpBridge> {
86
+ const cached = this.resolvedModules[name]
87
+ if (cached) return cached
88
+ const mod = await HttpBridge.create({
89
+ config: { name, nodeUri: this.bridge.nodeUri, schema: HttpBridgeConfigSchema },
90
+ })
91
+ this.resolvedModules[name] = mod
92
+ this.resolvedModules[mod.address] = mod
93
+ return mod
94
+ }
95
+
96
+ private async resolveRemoteModules(filter?: RemoteModuleFilter): Promise<HttpBridge[]> {
97
+ const addresses = filter ? filter?.address : await this.getRemoteAddresses()
98
+ const names = filter?.name
99
+ const byAddress = await Promise.all(addresses?.map((address) => this.resolveByAddress(address)) ?? [])
100
+ const byName = await Promise.all(names?.map((name) => this.resolveByName(name)) ?? [])
101
+ return [...byAddress, ...byName]
102
+ }
103
+ }
package/src/index.ts CHANGED
@@ -2,5 +2,6 @@ export * from './AbstractBridge'
2
2
  export * from './BridgeWrapper'
3
3
  export * from './Config'
4
4
  export * from './HttpBridge'
5
+ export * from './HttpBridgeConfig'
5
6
  export * from './PartialConfig'
6
7
  export * from './Queries'
@@ -1,26 +1,20 @@
1
- import { ArchivistWrapper } from '@xyo-network/archivist-wrapper'
2
- import { Axios } from '@xyo-network/axios'
3
- import { uuid } from '@xyo-network/core'
4
- import { PayloadWrapper } from '@xyo-network/payload-wrapper'
1
+ import { AxiosJson } from '@xyo-network/axios'
2
+ import { NodeWrapper } from '@xyo-network/node'
5
3
 
6
- import { HttpBridgeConfigSchema, XyoHttpBridge } from '../HttpBridge'
4
+ import { HttpBridge } from '../HttpBridge'
5
+ import { HttpBridgeConfigSchema } from '../HttpBridgeConfig'
7
6
 
8
- test('XyoHttpBridge', async () => {
9
- const nodeUri = `${process.env.API_DOMAIN}` ?? 'https://beta.api.archivist.xyo.network'
10
- const targetAddress = 'temp'
7
+ test('HttpBridge', async () => {
8
+ const nodeUri = `${process.env.API_DOMAIN}` ?? 'http://localhost:8080'
11
9
 
12
- const bridge = await XyoHttpBridge.create({
13
- axios: new Axios(),
14
- config: { nodeUri, schema: HttpBridgeConfigSchema, targetAddress },
10
+ const bridge = await HttpBridge.create({
11
+ axios: new AxiosJson(),
12
+ config: { nodeUri, schema: HttpBridgeConfigSchema, targetAddress: '5111228f724a066ac060fe2e6c8bbaae44b107d5' },
15
13
  })
16
- const wrapper = new ArchivistWrapper(bridge)
17
- const debugPayload = {
18
- nonce: uuid(),
19
- schema: 'network.xyo.debug',
20
- }
21
- const result = await wrapper.insert([debugPayload])
22
- console.log(result)
23
- //expect(result).toBeDefined()
24
- await wrapper.get([new PayloadWrapper(debugPayload).hash])
25
- //expect(result2).toBeDefined()
14
+ const wrapper = NodeWrapper.wrap(bridge)
15
+ const description = await wrapper.describe()
16
+ expect(description.children).toBeArray()
17
+ expect(description.children?.length).toBeGreaterThan(0)
18
+ expect(description.queries).toBeArray()
19
+ expect(description.queries?.length).toBeGreaterThan(0)
26
20
  })