@xyo-network/bridge 2.46.3 → 2.47.0
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/AbstractBridge.js +1 -7
- package/dist/cjs/AbstractBridge.js.map +1 -1
- package/dist/cjs/BridgeWrapper.js +32 -0
- package/dist/cjs/BridgeWrapper.js.map +1 -1
- package/dist/cjs/HttpBridge.js +53 -33
- package/dist/cjs/HttpBridge.js.map +1 -1
- package/dist/cjs/HttpProxyModule.js +39 -0
- package/dist/cjs/HttpProxyModule.js.map +1 -0
- package/dist/cjs/RemoteModuleResolver.js +15 -38
- package/dist/cjs/RemoteModuleResolver.js.map +1 -1
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/docs.json +18938 -13129
- package/dist/esm/AbstractBridge.js +2 -7
- package/dist/esm/AbstractBridge.js.map +1 -1
- package/dist/esm/BridgeWrapper.js +23 -1
- package/dist/esm/BridgeWrapper.js.map +1 -1
- package/dist/esm/HttpBridge.js +48 -32
- package/dist/esm/HttpBridge.js.map +1 -1
- package/dist/esm/HttpProxyModule.js +32 -0
- package/dist/esm/HttpProxyModule.js.map +1 -0
- package/dist/esm/RemoteModuleResolver.js +10 -29
- package/dist/esm/RemoteModuleResolver.js.map +1 -1
- package/dist/esm/index.js +1 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/types/AbstractBridge.d.ts +6 -2
- package/dist/types/AbstractBridge.d.ts.map +1 -1
- package/dist/types/Bridge.d.ts +8 -2
- package/dist/types/Bridge.d.ts.map +1 -1
- package/dist/types/BridgeWrapper.d.ts +29 -3
- package/dist/types/BridgeWrapper.d.ts.map +1 -1
- package/dist/types/HttpBridge.d.ts +30 -10
- package/dist/types/HttpBridge.d.ts.map +1 -1
- package/dist/types/HttpProxyModule.d.ts +25 -0
- package/dist/types/HttpProxyModule.d.ts.map +1 -0
- package/dist/types/RemoteModuleResolver.d.ts +3 -3
- package/dist/types/RemoteModuleResolver.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +11 -11
- package/src/AbstractBridge.ts +22 -7
- package/src/Bridge.ts +10 -2
- package/src/BridgeWrapper.ts +49 -3
- package/src/HttpBridge.ts +56 -32
- package/src/HttpProxyModule.ts +37 -0
- package/src/RemoteModuleResolver.ts +13 -41
- package/src/index.ts +1 -0
- package/src/spec/HttpBridge.spec.ts +6 -4
package/dist/types/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA;AAChC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,UAAU,CAAA;AACxB,cAAc,cAAc,CAAA;AAC5B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,WAAW,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA;AAChC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,UAAU,CAAA;AACxB,cAAc,cAAc,CAAA;AAC5B,cAAc,oBAAoB,CAAA;AAClC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,WAAW,CAAA"}
|
package/package.json
CHANGED
|
@@ -11,16 +11,16 @@
|
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
13
|
"@xylabs/assert": "^2.7.1",
|
|
14
|
-
"@xyo-network/account": "^2.
|
|
15
|
-
"@xyo-network/address-payload-plugin": "^2.
|
|
16
|
-
"@xyo-network/api-models": "^2.
|
|
17
|
-
"@xyo-network/axios": "^2.
|
|
18
|
-
"@xyo-network/module": "^2.
|
|
19
|
-
"@xyo-network/module-model": "^2.
|
|
20
|
-
"@xyo-network/payload-model": "^2.
|
|
21
|
-
"@xyo-network/payload-wrapper": "^2.
|
|
22
|
-
"@xyo-network/promise": "^2.
|
|
23
|
-
"@xyo-network/query-payload-plugin": "^2.
|
|
14
|
+
"@xyo-network/account": "^2.47.0",
|
|
15
|
+
"@xyo-network/address-payload-plugin": "^2.47.0",
|
|
16
|
+
"@xyo-network/api-models": "^2.47.0",
|
|
17
|
+
"@xyo-network/axios": "^2.47.0",
|
|
18
|
+
"@xyo-network/module": "^2.47.0",
|
|
19
|
+
"@xyo-network/module-model": "^2.47.0",
|
|
20
|
+
"@xyo-network/payload-model": "^2.47.0",
|
|
21
|
+
"@xyo-network/payload-wrapper": "^2.47.0",
|
|
22
|
+
"@xyo-network/promise": "^2.47.0",
|
|
23
|
+
"@xyo-network/query-payload-plugin": "^2.47.0",
|
|
24
24
|
"lodash": "^4.17.21"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
@@ -61,5 +61,5 @@
|
|
|
61
61
|
},
|
|
62
62
|
"sideEffects": false,
|
|
63
63
|
"types": "dist/types/index.d.ts",
|
|
64
|
-
"version": "2.
|
|
64
|
+
"version": "2.47.0"
|
|
65
65
|
}
|
package/src/AbstractBridge.ts
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
import { Account } from '@xyo-network/account'
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
AbstractModule,
|
|
4
|
+
Module,
|
|
5
|
+
ModuleConfig,
|
|
6
|
+
ModuleFilter,
|
|
7
|
+
ModuleQueryResult,
|
|
8
|
+
ModuleResolver,
|
|
9
|
+
QueryBoundWitnessWrapper,
|
|
10
|
+
XyoErrorBuilder,
|
|
11
|
+
XyoQuery,
|
|
12
|
+
XyoQueryBoundWitness,
|
|
13
|
+
} from '@xyo-network/module'
|
|
3
14
|
import { XyoPayload } from '@xyo-network/payload-model'
|
|
4
15
|
import { Promisable } from '@xyo-network/promise'
|
|
5
16
|
|
|
@@ -8,6 +19,8 @@ import { BridgeConfig } from './Config'
|
|
|
8
19
|
import { XyoBridgeConnectQuerySchema, XyoBridgeDisconnectQuerySchema, XyoBridgeQuery } from './Queries'
|
|
9
20
|
|
|
10
21
|
export abstract class AbstractBridge<TConfig extends BridgeConfig = BridgeConfig> extends AbstractModule<TConfig> implements BridgeModule {
|
|
22
|
+
abstract targetResolver: ModuleResolver
|
|
23
|
+
|
|
11
24
|
override get queries(): string[] {
|
|
12
25
|
return [XyoBridgeConnectQuerySchema, XyoBridgeDisconnectQuerySchema, ...super.queries]
|
|
13
26
|
}
|
|
@@ -27,11 +40,7 @@ export abstract class AbstractBridge<TConfig extends BridgeConfig = BridgeConfig
|
|
|
27
40
|
break
|
|
28
41
|
}
|
|
29
42
|
default:
|
|
30
|
-
|
|
31
|
-
return super.query(query, payloads)
|
|
32
|
-
} else {
|
|
33
|
-
return this.forward(typedQuery)
|
|
34
|
-
}
|
|
43
|
+
return super.query(query, payloads)
|
|
35
44
|
}
|
|
36
45
|
} catch (ex) {
|
|
37
46
|
const error = ex as Error
|
|
@@ -43,5 +52,11 @@ export abstract class AbstractBridge<TConfig extends BridgeConfig = BridgeConfig
|
|
|
43
52
|
abstract connect(): Promisable<boolean>
|
|
44
53
|
abstract disconnect(): Promisable<boolean>
|
|
45
54
|
|
|
46
|
-
abstract
|
|
55
|
+
abstract targetConfig(address: string): Promisable<ModuleConfig>
|
|
56
|
+
|
|
57
|
+
abstract targetQuery(address: string, query: XyoQuery, payloads?: XyoPayload[]): Promisable<ModuleQueryResult>
|
|
58
|
+
|
|
59
|
+
abstract targetQueryable(address: string, query: XyoQueryBoundWitness, payloads?: XyoPayload[], queryConfig?: ModuleConfig): boolean
|
|
60
|
+
|
|
61
|
+
abstract targetResolve(address: string, filter?: ModuleFilter): Promisable<Module[]>
|
|
47
62
|
}
|
package/src/Bridge.ts
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
|
-
import { Module } from '@xyo-network/module-model'
|
|
1
|
+
import { Module, ModuleConfig, ModuleFilter, ModuleQueryResult, XyoQuery, XyoQueryBoundWitness } from '@xyo-network/module-model'
|
|
2
|
+
import { XyoPayload } from '@xyo-network/payload-model'
|
|
2
3
|
import { Promisable } from '@xyo-network/promise'
|
|
3
4
|
|
|
5
|
+
import { BridgeConfig } from './Config'
|
|
6
|
+
|
|
4
7
|
export interface Bridge {
|
|
5
8
|
connect: () => Promisable<boolean>
|
|
6
9
|
disconnect: () => Promisable<boolean>
|
|
7
10
|
}
|
|
8
11
|
|
|
9
|
-
export interface BridgeModule extends Bridge, Module {
|
|
12
|
+
export interface BridgeModule<TConfig extends BridgeConfig = BridgeConfig> extends Bridge, Module<TConfig> {
|
|
13
|
+
targetResolver: Module['resolver']
|
|
14
|
+
targetQuery(address: string, query: XyoQuery, payloads?: XyoPayload[]): Promisable<ModuleQueryResult>
|
|
15
|
+
targetQueryable(address: string, query: XyoQueryBoundWitness, payloads?: XyoPayload[], queryConfig?: ModuleConfig): Promisable<boolean>
|
|
16
|
+
targetResolve(address: string, filter?: ModuleFilter): Promisable<Module[]>
|
|
17
|
+
}
|
package/src/BridgeWrapper.ts
CHANGED
|
@@ -1,10 +1,24 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
ModuleConfig,
|
|
3
|
+
ModuleDiscoverQuery,
|
|
4
|
+
ModuleDiscoverQuerySchema,
|
|
5
|
+
ModuleFilter,
|
|
6
|
+
ModuleQueryResult,
|
|
7
|
+
ModuleWrapper,
|
|
8
|
+
XyoQuery,
|
|
9
|
+
XyoQueryBoundWitness,
|
|
10
|
+
} from '@xyo-network/module'
|
|
11
|
+
import { XyoPayload, XyoPayloads } from '@xyo-network/payload-model'
|
|
2
12
|
import { PayloadWrapper } from '@xyo-network/payload-wrapper'
|
|
3
13
|
|
|
4
|
-
import {
|
|
14
|
+
import { BridgeModule } from './Bridge'
|
|
5
15
|
import { XyoBridgeConnectQuerySchema, XyoBridgeDisconnectQuerySchema, XyoBridgeQuery } from './Queries'
|
|
6
16
|
|
|
7
|
-
export class BridgeWrapper extends ModuleWrapper implements
|
|
17
|
+
export class BridgeWrapper extends ModuleWrapper<BridgeModule> implements BridgeModule {
|
|
18
|
+
public get targetResolver() {
|
|
19
|
+
return this.module.targetResolver
|
|
20
|
+
}
|
|
21
|
+
|
|
8
22
|
async connect(uri?: string): Promise<boolean> {
|
|
9
23
|
const queryPayload = PayloadWrapper.parse<XyoBridgeQuery>({ schema: XyoBridgeConnectQuerySchema, uri })
|
|
10
24
|
await this.sendQuery(queryPayload)
|
|
@@ -16,4 +30,36 @@ export class BridgeWrapper extends ModuleWrapper implements Bridge {
|
|
|
16
30
|
await this.sendQuery(queryPayload)
|
|
17
31
|
return true
|
|
18
32
|
}
|
|
33
|
+
|
|
34
|
+
async targetDiscover(address: string): Promise<XyoPayload[]> {
|
|
35
|
+
const queryPayload = PayloadWrapper.parse<ModuleDiscoverQuery>({ schema: ModuleDiscoverQuerySchema })
|
|
36
|
+
return await this.sendTargetQuery(address, queryPayload)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async targetQuery<T extends XyoQueryBoundWitness = XyoQueryBoundWitness>(
|
|
40
|
+
address: string,
|
|
41
|
+
query: T,
|
|
42
|
+
payloads?: XyoPayload[],
|
|
43
|
+
): Promise<ModuleQueryResult> {
|
|
44
|
+
return await this.module.targetQuery(address, query, payloads)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async targetQueryable(address: string, query: XyoQueryBoundWitness, payloads?: XyoPayload[], queryConfig?: ModuleConfig): Promise<boolean> {
|
|
48
|
+
return await this.module.targetQueryable(address, query, payloads, queryConfig)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async targetResolve(address: string, filter?: ModuleFilter) {
|
|
52
|
+
return await this.module.targetResolve(address, filter)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
protected async sendTargetQuery<T extends XyoQuery | PayloadWrapper<XyoQuery>>(
|
|
56
|
+
address: string,
|
|
57
|
+
queryPayload: T,
|
|
58
|
+
payloads?: XyoPayloads,
|
|
59
|
+
): Promise<XyoPayload[]> {
|
|
60
|
+
const query = await this.bindQuery(queryPayload, payloads)
|
|
61
|
+
const result = await this.module.targetQuery(address, query[0], query[1])
|
|
62
|
+
this.throwErrors(query, result)
|
|
63
|
+
return result[1]
|
|
64
|
+
}
|
|
19
65
|
}
|
package/src/HttpBridge.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import { assertEx } from '@xylabs/assert'
|
|
2
2
|
import { Account } from '@xyo-network/account'
|
|
3
|
+
import { AddressPayload, AddressSchema } from '@xyo-network/address-payload-plugin'
|
|
3
4
|
import { XyoApiEnvelope } from '@xyo-network/api-models'
|
|
4
5
|
import { AxiosError, AxiosJson } from '@xyo-network/axios'
|
|
5
6
|
import {
|
|
6
7
|
AbstractModule,
|
|
8
|
+
ModuleConfig,
|
|
7
9
|
ModuleDiscoverQuery,
|
|
8
10
|
ModuleDiscoverQuerySchema,
|
|
9
11
|
ModuleFilter,
|
|
10
12
|
ModuleParams,
|
|
11
13
|
ModuleQueryResult,
|
|
12
14
|
ModuleResolver,
|
|
13
|
-
ModuleWrapper,
|
|
14
15
|
QueryBoundWitnessWrapper,
|
|
15
16
|
XyoErrorBuilder,
|
|
16
17
|
XyoQuery,
|
|
@@ -31,40 +32,37 @@ export interface XyoHttpBridgeParams<TConfig extends HttpBridgeConfig = HttpBrid
|
|
|
31
32
|
axios?: AxiosJson
|
|
32
33
|
}
|
|
33
34
|
|
|
34
|
-
export class HttpBridge<TConfig extends HttpBridgeConfig = HttpBridgeConfig> extends AbstractModule<TConfig> implements BridgeModule {
|
|
35
|
+
export class HttpBridge<TConfig extends HttpBridgeConfig = HttpBridgeConfig> extends AbstractModule<TConfig> implements BridgeModule<TConfig> {
|
|
36
|
+
private _targetQueries: Record<string, string[]> = {}
|
|
37
|
+
private _targetResolver: ModuleResolver
|
|
35
38
|
private axios: AxiosJson
|
|
36
|
-
private remoteQueries: string[] = []
|
|
37
|
-
private remoteResolver: ModuleResolver
|
|
38
39
|
|
|
39
40
|
protected constructor(params: XyoHttpBridgeParams<TConfig>) {
|
|
40
41
|
super(params)
|
|
41
42
|
this.axios = params.axios ?? new AxiosJson()
|
|
42
|
-
this.
|
|
43
|
+
this._targetResolver = new RemoteModuleResolver(this)
|
|
43
44
|
}
|
|
44
45
|
|
|
45
46
|
public get nodeUri() {
|
|
46
47
|
return assertEx(this.config?.nodeUri, 'Missing nodeUri')
|
|
47
48
|
}
|
|
48
49
|
|
|
49
|
-
public
|
|
50
|
-
return
|
|
50
|
+
public get targetAddress() {
|
|
51
|
+
return assertEx(this.config?.targetAddress, 'targetAddress not set')
|
|
51
52
|
}
|
|
52
53
|
|
|
53
|
-
public get
|
|
54
|
-
return this.
|
|
54
|
+
public get targetResolver() {
|
|
55
|
+
return this._targetResolver
|
|
55
56
|
}
|
|
56
57
|
|
|
57
58
|
static override async create(params: XyoHttpBridgeParams): Promise<HttpBridge> {
|
|
58
59
|
const result = (await super.create(params)) as HttpBridge
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
result.remoteQueries = compact(
|
|
60
|
+
const discover = await result.targetDiscover(params.config.targetAddress ?? '')
|
|
61
|
+
await Promise.all(
|
|
62
62
|
discover.map((payload) => {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
return
|
|
66
|
-
} else {
|
|
67
|
-
return null
|
|
63
|
+
const addressPayload = payload as AddressPayload
|
|
64
|
+
if (addressPayload.schema === AddressSchema) {
|
|
65
|
+
return result.targetDiscover(addressPayload.address)
|
|
68
66
|
}
|
|
69
67
|
}),
|
|
70
68
|
)
|
|
@@ -79,11 +77,6 @@ export class HttpBridge<TConfig extends HttpBridgeConfig = HttpBridgeConfig> ext
|
|
|
79
77
|
return true
|
|
80
78
|
}
|
|
81
79
|
|
|
82
|
-
override async discover() {
|
|
83
|
-
const queryPayload = PayloadWrapper.parse<ModuleDiscoverQuery>({ schema: ModuleDiscoverQuerySchema })
|
|
84
|
-
return (await this.forward(queryPayload))[1]
|
|
85
|
-
}
|
|
86
|
-
|
|
87
80
|
override async query<T extends XyoQueryBoundWitness = XyoQueryBoundWitness>(query: T, payloads?: XyoPayload[]): Promise<ModuleQueryResult> {
|
|
88
81
|
const wrapper = QueryBoundWitnessWrapper.parseQuery<XyoBridgeQuery>(query, payloads)
|
|
89
82
|
const typedQuery = wrapper.query.payload
|
|
@@ -100,11 +93,7 @@ export class HttpBridge<TConfig extends HttpBridgeConfig = HttpBridgeConfig> ext
|
|
|
100
93
|
break
|
|
101
94
|
}
|
|
102
95
|
default:
|
|
103
|
-
|
|
104
|
-
return await super.query(query, payloads)
|
|
105
|
-
} else {
|
|
106
|
-
return await this.forward(query, payloads)
|
|
107
|
-
}
|
|
96
|
+
return await super.query(query, payloads)
|
|
108
97
|
}
|
|
109
98
|
} catch (ex) {
|
|
110
99
|
const error = ex as Error
|
|
@@ -114,19 +103,54 @@ export class HttpBridge<TConfig extends HttpBridgeConfig = HttpBridgeConfig> ext
|
|
|
114
103
|
}
|
|
115
104
|
|
|
116
105
|
override async resolve(filter?: ModuleFilter) {
|
|
117
|
-
return [...(await super.resolve(filter)), ...(await this.
|
|
106
|
+
return [...(await super.resolve(filter)), ...(await this.targetResolver.resolve(filter))]
|
|
118
107
|
}
|
|
119
108
|
|
|
120
|
-
|
|
109
|
+
async targetDiscover(address: string): Promise<XyoPayload[]> {
|
|
110
|
+
const queryPayload = PayloadWrapper.parse<ModuleDiscoverQuery>({ schema: ModuleDiscoverQuerySchema })
|
|
111
|
+
const discover = assertEx(await this.targetQuery(address, queryPayload), `Unable to resolve [${address}]`)[1]
|
|
112
|
+
|
|
113
|
+
this._targetQueries[address] = compact(
|
|
114
|
+
discover?.map((payload) => {
|
|
115
|
+
if (payload.schema === QuerySchema) {
|
|
116
|
+
const schemaPayload = payload as QueryPayload
|
|
117
|
+
return schemaPayload.query
|
|
118
|
+
} else {
|
|
119
|
+
return null
|
|
120
|
+
}
|
|
121
|
+
}) ?? [],
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
return discover
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
public targetQueries(address: string): string[] {
|
|
128
|
+
return assertEx(this._targetQueries[address], `targetConfig not set [${address}]`)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async targetQuery(address: string, query: XyoQuery, payloads: XyoPayload[] = []): Promise<ModuleQueryResult> {
|
|
121
132
|
try {
|
|
122
133
|
const boundQuery = await this.bindQuery(query, payloads)
|
|
123
|
-
const
|
|
134
|
+
const path = `${this.nodeUri}/${address}`
|
|
135
|
+
const result = await this.axios.post<XyoApiEnvelope<ModuleQueryResult>>(path, boundQuery)
|
|
136
|
+
if (result.status >= 400) {
|
|
137
|
+
this.logger?.error(`targetQuery failed [${path}]`)
|
|
138
|
+
throw `targetQuery failed [${path}] [${result.status}]`
|
|
139
|
+
}
|
|
124
140
|
return result.data.data
|
|
125
141
|
} catch (ex) {
|
|
126
142
|
const error = ex as AxiosError
|
|
127
|
-
|
|
128
|
-
|
|
143
|
+
this.logger?.error(`Error Status: ${error.status}`)
|
|
144
|
+
this.logger?.error(`Error Cause: ${JSON.stringify(error.cause, null, 2)}`)
|
|
129
145
|
throw error
|
|
130
146
|
}
|
|
131
147
|
}
|
|
148
|
+
|
|
149
|
+
targetQueryable(_address: string, _query: XyoQueryBoundWitness, _payloads?: XyoPayload[], _queryConfig?: ModuleConfig): boolean {
|
|
150
|
+
throw 'targetQueryable not available'
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
async targetResolve(address: string, filter?: ModuleFilter) {
|
|
154
|
+
return await this.targetResolver.resolve(filter)
|
|
155
|
+
}
|
|
132
156
|
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Module, ModuleConfig, ModuleConfigSchema, ModuleFilter, ModuleQueryResult, XyoQueryBoundWitness } from '@xyo-network/module'
|
|
2
|
+
import { XyoPayload } from '@xyo-network/payload-model'
|
|
3
|
+
|
|
4
|
+
import { HttpBridge } from './HttpBridge'
|
|
5
|
+
|
|
6
|
+
export class HttpProxyModule implements Module {
|
|
7
|
+
constructor(protected readonly bridge: HttpBridge, protected readonly _address: string) {}
|
|
8
|
+
|
|
9
|
+
get address() {
|
|
10
|
+
return this._address
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
get config(): ModuleConfig {
|
|
14
|
+
return { schema: ModuleConfigSchema }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
get queries() {
|
|
18
|
+
return this.bridge.targetQueries(this.address)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
get resolver() {
|
|
22
|
+
return this.bridge.targetResolver
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async query<T extends XyoQueryBoundWitness = XyoQueryBoundWitness>(query: T, payloads?: XyoPayload[]): Promise<ModuleQueryResult> {
|
|
26
|
+
return await this.bridge.targetQuery(this.address, query, payloads)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
queryable(query: XyoQueryBoundWitness, payloads?: XyoPayload[], queryConfig?: ModuleConfig): boolean {
|
|
30
|
+
return this.bridge.targetQueryable(this.address, query, payloads, queryConfig)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/* Resolves a filter from the perspective of the module, including through the parent/gateway module */
|
|
34
|
+
async resolve(filter?: ModuleFilter): Promise<Module[]> {
|
|
35
|
+
return await this.bridge.targetResolve(this.address, filter)
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -1,25 +1,14 @@
|
|
|
1
|
-
import { assertEx } from '@xylabs/assert'
|
|
2
1
|
import { AddressPayload, AddressSchema } from '@xyo-network/address-payload-plugin'
|
|
3
|
-
import {
|
|
2
|
+
import { CompositeModuleResolver } from '@xyo-network/module'
|
|
4
3
|
import { Module, ModuleFilter } from '@xyo-network/module-model'
|
|
5
4
|
import compact from 'lodash/compact'
|
|
6
5
|
import flatten from 'lodash/flatten'
|
|
7
6
|
|
|
8
7
|
import { HttpBridge } from './HttpBridge'
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
interface LocalModuleFilter {
|
|
12
|
-
config?: string[]
|
|
13
|
-
query?: string[][]
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
interface RemoteModuleFilter {
|
|
17
|
-
address?: string[]
|
|
18
|
-
name?: string[]
|
|
19
|
-
}
|
|
8
|
+
import { HttpProxyModule } from './HttpProxyModule'
|
|
20
9
|
|
|
21
10
|
export class RemoteModuleResolver extends CompositeModuleResolver {
|
|
22
|
-
private resolvedModules: Record<string,
|
|
11
|
+
private resolvedModules: Record<string, HttpProxyModule> = {}
|
|
23
12
|
|
|
24
13
|
// TODO: Allow optional ctor param for supplying address for nested Nodes
|
|
25
14
|
// protected readonly address?: string,
|
|
@@ -44,22 +33,12 @@ export class RemoteModuleResolver extends CompositeModuleResolver {
|
|
|
44
33
|
throw new Error('Method not implemented.')
|
|
45
34
|
}
|
|
46
35
|
|
|
47
|
-
async resolve(filter?: ModuleFilter): Promise<
|
|
48
|
-
|
|
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
|
|
36
|
+
async resolve(filter?: ModuleFilter): Promise<HttpProxyModule[]> {
|
|
37
|
+
return await Promise.all(flatten(await this.resolveRemoteModules(filter)))
|
|
58
38
|
}
|
|
59
39
|
|
|
60
40
|
private async getRemoteAddresses() {
|
|
61
|
-
const
|
|
62
|
-
const discover = await nodeWrapper.discover()
|
|
41
|
+
const discover = await this.bridge.targetDiscover('')
|
|
63
42
|
return compact(
|
|
64
43
|
discover.map((payload) => {
|
|
65
44
|
if (payload.schema === AddressSchema) {
|
|
@@ -72,32 +51,25 @@ export class RemoteModuleResolver extends CompositeModuleResolver {
|
|
|
72
51
|
)
|
|
73
52
|
}
|
|
74
53
|
|
|
75
|
-
private
|
|
54
|
+
private resolveByAddress(targetAddress: string): HttpProxyModule | undefined {
|
|
76
55
|
const cached = this.resolvedModules[targetAddress]
|
|
77
56
|
if (cached) return cached
|
|
78
|
-
const mod =
|
|
79
|
-
config: { nodeUri: this.bridge.nodeUri, schema: HttpBridgeConfigSchema, targetAddress },
|
|
80
|
-
})
|
|
57
|
+
const mod = new HttpProxyModule(this.bridge, targetAddress)
|
|
81
58
|
this.resolvedModules[targetAddress] = mod
|
|
82
59
|
return mod
|
|
83
60
|
}
|
|
84
61
|
|
|
85
|
-
private
|
|
62
|
+
private resolveByName(name: string): HttpProxyModule | undefined {
|
|
86
63
|
const cached = this.resolvedModules[name]
|
|
87
64
|
if (cached) return cached
|
|
88
|
-
|
|
89
|
-
config: { name, nodeUri: this.bridge.nodeUri, schema: HttpBridgeConfigSchema },
|
|
90
|
-
})
|
|
91
|
-
this.resolvedModules[name] = mod
|
|
92
|
-
this.resolvedModules[mod.address] = mod
|
|
93
|
-
return mod
|
|
65
|
+
return undefined
|
|
94
66
|
}
|
|
95
67
|
|
|
96
|
-
private async resolveRemoteModules(filter?:
|
|
68
|
+
private async resolveRemoteModules(filter?: ModuleFilter): Promise<HttpProxyModule[]> {
|
|
97
69
|
const addresses = filter ? filter?.address : await this.getRemoteAddresses()
|
|
98
70
|
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)) ?? [])
|
|
71
|
+
const byAddress = compact(await Promise.all(addresses?.map((address) => this.resolveByAddress(address)) ?? []))
|
|
72
|
+
const byName = compact(await Promise.all(names?.map((name) => this.resolveByName(name)) ?? []))
|
|
101
73
|
return [...byAddress, ...byName]
|
|
102
74
|
}
|
|
103
75
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { assertEx } from '@xylabs/assert'
|
|
1
2
|
import { AxiosJson } from '@xyo-network/axios'
|
|
2
3
|
import { NodeWrapper } from '@xyo-network/node'
|
|
3
4
|
|
|
@@ -6,17 +7,18 @@ import { HttpBridgeConfigSchema } from '../HttpBridgeConfig'
|
|
|
6
7
|
|
|
7
8
|
test('HttpBridge', async () => {
|
|
8
9
|
const nodeUri = `${process.env.API_DOMAIN}` ?? 'http://localhost:8080'
|
|
10
|
+
const targetAddress = ''
|
|
9
11
|
|
|
10
12
|
const bridge = await HttpBridge.create({
|
|
11
13
|
axios: new AxiosJson(),
|
|
12
|
-
config: { nodeUri
|
|
14
|
+
config: { nodeUri: `${nodeUri}/node`, schema: HttpBridgeConfigSchema, security: { allowAnonymous: true }, targetAddress },
|
|
13
15
|
})
|
|
14
|
-
const
|
|
16
|
+
const modules = await bridge.resolve()
|
|
17
|
+
const rootNode = assertEx(modules[0])
|
|
18
|
+
const wrapper = NodeWrapper.wrap(rootNode)
|
|
15
19
|
const description = await wrapper.describe()
|
|
16
20
|
expect(description.children).toBeArray()
|
|
17
21
|
expect(description.children?.length).toBeGreaterThan(0)
|
|
18
22
|
expect(description.queries).toBeArray()
|
|
19
23
|
expect(description.queries?.length).toBeGreaterThan(0)
|
|
20
|
-
|
|
21
|
-
console.log(JSON.stringify(description, null, 2))
|
|
22
24
|
})
|