@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.
- package/dist/cjs/HttpBridge.js +48 -20
- package/dist/cjs/HttpBridge.js.map +1 -1
- package/dist/cjs/HttpBridgeConfig.js +5 -0
- package/dist/cjs/HttpBridgeConfig.js.map +1 -0
- package/dist/cjs/RemoteModuleResolver.js +95 -0
- package/dist/cjs/RemoteModuleResolver.js.map +1 -0
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/docs.json +2144 -9201
- package/dist/esm/HttpBridge.js +41 -18
- package/dist/esm/HttpBridge.js.map +1 -1
- package/dist/esm/HttpBridgeConfig.js +2 -0
- package/dist/esm/HttpBridgeConfig.js.map +1 -0
- package/dist/esm/RemoteModuleResolver.js +80 -0
- package/dist/esm/RemoteModuleResolver.js.map +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/types/Config.d.ts +1 -4
- package/dist/types/Config.d.ts.map +1 -1
- package/dist/types/HttpBridge.d.ts +20 -15
- package/dist/types/HttpBridge.d.ts.map +1 -1
- package/dist/types/HttpBridgeConfig.d.ts +10 -0
- package/dist/types/HttpBridgeConfig.d.ts.map +1 -0
- package/dist/types/RemoteModuleResolver.d.ts +21 -0
- package/dist/types/RemoteModuleResolver.d.ts.map +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +12 -8
- package/src/Config.ts +1 -6
- package/src/HttpBridge.ts +54 -30
- package/src/HttpBridgeConfig.ts +14 -0
- package/src/RemoteModuleResolver.ts +103 -0
- package/src/index.ts +1 -0
- package/src/spec/HttpBridge.spec.ts +15 -21
|
@@ -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
|
@@ -1,26 +1,20 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
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 {
|
|
4
|
+
import { HttpBridge } from '../HttpBridge'
|
|
5
|
+
import { HttpBridgeConfigSchema } from '../HttpBridgeConfig'
|
|
7
6
|
|
|
8
|
-
test('
|
|
9
|
-
const nodeUri = `${process.env.API_DOMAIN}` ?? '
|
|
10
|
-
const targetAddress = 'temp'
|
|
7
|
+
test('HttpBridge', async () => {
|
|
8
|
+
const nodeUri = `${process.env.API_DOMAIN}` ?? 'http://localhost:8080'
|
|
11
9
|
|
|
12
|
-
const bridge = await
|
|
13
|
-
axios: new
|
|
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 =
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
})
|