@xyo-network/module-abstract 2.64.8 → 2.64.9
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/AbstractIndirectModule.js +411 -0
- package/dist/cjs/AbstractIndirectModule.js.map +1 -0
- package/dist/cjs/AbstractModule.js +24 -371
- package/dist/cjs/AbstractModule.js.map +1 -1
- package/dist/cjs/QueryValidator/SupportedQueryValidator.js.map +1 -1
- package/dist/cjs/Resolver/CompositeModuleResolver.js +2 -3
- package/dist/cjs/Resolver/CompositeModuleResolver.js.map +1 -1
- package/dist/cjs/Resolver/ResolverEventEmitter.js.map +1 -1
- package/dist/cjs/Resolver/SimpleModuleResolver.js.map +1 -1
- package/dist/cjs/lib/duplicateModules.js.map +1 -1
- package/dist/docs.json +4173 -3806
- package/dist/esm/AbstractIndirectModule.js +373 -0
- package/dist/esm/AbstractIndirectModule.js.map +1 -0
- package/dist/esm/AbstractModule.js +16 -350
- package/dist/esm/AbstractModule.js.map +1 -1
- package/dist/esm/QueryValidator/SupportedQueryValidator.js.map +1 -1
- package/dist/esm/Resolver/CompositeModuleResolver.js +2 -3
- package/dist/esm/Resolver/CompositeModuleResolver.js.map +1 -1
- package/dist/esm/Resolver/ResolverEventEmitter.js.map +1 -1
- package/dist/esm/Resolver/SimpleModuleResolver.js.map +1 -1
- package/dist/esm/lib/duplicateModules.js.map +1 -1
- package/dist/types/AbstractIndirectModule.d.ts +66 -0
- package/dist/types/AbstractIndirectModule.d.ts.map +1 -0
- package/dist/types/AbstractModule.d.ts +7 -58
- package/dist/types/AbstractModule.d.ts.map +1 -1
- package/dist/types/QueryValidator/SupportedQueryValidator.d.ts +4 -4
- package/dist/types/QueryValidator/SupportedQueryValidator.d.ts.map +1 -1
- package/dist/types/Resolver/CompositeModuleResolver.d.ts +5 -5
- package/dist/types/Resolver/CompositeModuleResolver.d.ts.map +1 -1
- package/dist/types/Resolver/ResolverEventEmitter.d.ts +2 -2
- package/dist/types/Resolver/ResolverEventEmitter.d.ts.map +1 -1
- package/dist/types/Resolver/SimpleModuleResolver.d.ts +5 -5
- package/dist/types/Resolver/SimpleModuleResolver.d.ts.map +1 -1
- package/dist/types/lib/duplicateModules.d.ts +2 -2
- package/dist/types/lib/duplicateModules.d.ts.map +1 -1
- package/package.json +24 -24
- package/src/AbstractIndirectModule.ts +497 -0
- package/src/AbstractModule.ts +20 -460
- package/src/QueryValidator/SupportedQueryValidator.ts +3 -3
- package/src/Resolver/CompositeModuleResolver.ts +9 -10
- package/src/Resolver/ResolverEventEmitter.ts +5 -5
- package/src/Resolver/SimpleModuleResolver.ts +11 -11
- package/src/lib/duplicateModules.ts +2 -2
package/src/AbstractModule.ts
CHANGED
|
@@ -1,484 +1,44 @@
|
|
|
1
1
|
import { assertEx } from '@xylabs/assert'
|
|
2
|
-
import { exists } from '@xylabs/exists'
|
|
3
|
-
import { Account, HDWallet } from '@xyo-network/account'
|
|
4
2
|
import { AccountInstance } from '@xyo-network/account-model'
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder'
|
|
8
|
-
import { BoundWitness } from '@xyo-network/boundwitness-model'
|
|
9
|
-
import { ConfigPayload, ConfigSchema } from '@xyo-network/config-payload-plugin'
|
|
10
|
-
import { handleErrorAsync } from '@xyo-network/error'
|
|
11
|
-
import { ModuleManifestPayload, ModuleManifestPayloadSchema } from '@xyo-network/manifest-model'
|
|
12
|
-
import {
|
|
13
|
-
AccountModuleParams,
|
|
14
|
-
AddressPreviousHashPayload,
|
|
15
|
-
AddressPreviousHashSchema,
|
|
16
|
-
CreatableModule,
|
|
17
|
-
CreatableModuleFactory,
|
|
18
|
-
IndividualArchivistConfig,
|
|
19
|
-
Module,
|
|
20
|
-
ModuleAddressQuerySchema,
|
|
21
|
-
ModuleConfig,
|
|
22
|
-
ModuleDescription,
|
|
23
|
-
ModuleDiscoverQuerySchema,
|
|
24
|
-
ModuleError,
|
|
25
|
-
ModuleEventData,
|
|
26
|
-
ModuleFilter,
|
|
27
|
-
ModuleParams,
|
|
28
|
-
ModuleQueriedEventArgs,
|
|
29
|
-
ModuleQuery,
|
|
30
|
-
ModuleQueryBase,
|
|
31
|
-
ModuleQueryResult,
|
|
32
|
-
ModuleSubscribeQuerySchema,
|
|
33
|
-
Query,
|
|
34
|
-
QueryBoundWitness,
|
|
35
|
-
SchemaString,
|
|
36
|
-
WalletModuleParams,
|
|
37
|
-
} from '@xyo-network/module-model'
|
|
38
|
-
import { PayloadBuilder } from '@xyo-network/payload-builder'
|
|
3
|
+
import { ModuleManifestPayload } from '@xyo-network/manifest-model'
|
|
4
|
+
import { AddressPreviousHashPayload, Module, ModuleDescriptionPayload, ModuleEventData, ModuleParams } from '@xyo-network/module-model'
|
|
39
5
|
import { Payload } from '@xyo-network/payload-model'
|
|
40
|
-
import { PayloadWrapper } from '@xyo-network/payload-wrapper'
|
|
41
|
-
import { Promisable, PromiseEx } from '@xyo-network/promise'
|
|
42
|
-
import { QueryPayload, QuerySchema } from '@xyo-network/query-payload-plugin'
|
|
43
|
-
import { IdLogger } from '@xyo-network/shared'
|
|
44
|
-
import compact from 'lodash/compact'
|
|
45
6
|
|
|
46
|
-
import {
|
|
47
|
-
import { ModuleErrorBuilder } from './Error'
|
|
48
|
-
import { duplicateModules, serializableField } from './lib'
|
|
49
|
-
import { ModuleFactory } from './ModuleFactory'
|
|
50
|
-
import { QueryBoundWitnessBuilder, QueryBoundWitnessWrapper } from './Query'
|
|
51
|
-
import { ModuleConfigQueryValidator, Queryable, SupportedQueryValidator } from './QueryValidator'
|
|
52
|
-
import { CompositeModuleResolver } from './Resolver'
|
|
7
|
+
import { AbstractIndirectModule } from './AbstractIndirectModule'
|
|
53
8
|
|
|
54
9
|
export abstract class AbstractModule<TParams extends ModuleParams = ModuleParams, TEventData extends ModuleEventData = ModuleEventData>
|
|
55
|
-
extends
|
|
10
|
+
extends AbstractIndirectModule<TParams, TEventData>
|
|
56
11
|
implements Module<TParams, TEventData>
|
|
57
12
|
{
|
|
58
|
-
static configSchemas: string[]
|
|
59
|
-
|
|
60
|
-
protected static privateConstructorKey = Date.now().toString()
|
|
61
|
-
|
|
62
|
-
readonly downResolver = new CompositeModuleResolver()
|
|
63
|
-
readonly upResolver = new CompositeModuleResolver()
|
|
64
|
-
|
|
65
|
-
protected _account: AccountInstance | undefined = undefined
|
|
66
|
-
protected readonly _baseModuleQueryAccountPaths: Record<ModuleQueryBase['schema'], string> = {
|
|
67
|
-
'network.xyo.query.module.address': '1',
|
|
68
|
-
'network.xyo.query.module.discover': '2',
|
|
69
|
-
'network.xyo.query.module.subscribe': '3',
|
|
70
|
-
}
|
|
71
|
-
protected readonly _queryAccounts: Record<ModuleQueryBase['schema'], AccountInstance | undefined> = {
|
|
72
|
-
'network.xyo.query.module.address': undefined,
|
|
73
|
-
'network.xyo.query.module.discover': undefined,
|
|
74
|
-
'network.xyo.query.module.subscribe': undefined,
|
|
75
|
-
}
|
|
76
|
-
protected _started = false
|
|
77
|
-
protected readonly moduleConfigQueryValidator: Queryable
|
|
78
|
-
protected readonly supportedQueryValidator: Queryable
|
|
79
|
-
|
|
80
13
|
constructor(privateConstructorKey: string, params: TParams) {
|
|
81
14
|
assertEx(AbstractModule.privateConstructorKey === privateConstructorKey, 'Use create function instead of constructor')
|
|
82
15
|
// Clone params to prevent mutation of the incoming object
|
|
83
16
|
const mutatedParams = { ...params } as TParams
|
|
84
|
-
super(mutatedParams)
|
|
85
|
-
|
|
86
|
-
this.supportedQueryValidator = new SupportedQueryValidator(this as Module).queryable
|
|
87
|
-
this.moduleConfigQueryValidator = new ModuleConfigQueryValidator(mutatedParams?.config).queryable
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
static get configSchema(): string {
|
|
91
|
-
return this.configSchemas[0]
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
get account() {
|
|
95
|
-
return assertEx(this._account, 'Missing account')
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
get address() {
|
|
99
|
-
return this.account.address
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
get allowAnonymous() {
|
|
103
|
-
return !!this.config.security?.allowAnonymous
|
|
17
|
+
super(privateConstructorKey, mutatedParams)
|
|
104
18
|
}
|
|
105
19
|
|
|
106
|
-
|
|
107
|
-
|
|
20
|
+
/* make it public */
|
|
21
|
+
override async addressPreviousHash(): Promise<AddressPreviousHashPayload> {
|
|
22
|
+
return await super.addressPreviousHash()
|
|
108
23
|
}
|
|
109
24
|
|
|
110
|
-
|
|
111
|
-
|
|
25
|
+
/* make it public */
|
|
26
|
+
override async describe(): Promise<ModuleDescriptionPayload> {
|
|
27
|
+
return await super.describe()
|
|
112
28
|
}
|
|
113
29
|
|
|
114
|
-
|
|
115
|
-
|
|
30
|
+
/* make it public */
|
|
31
|
+
override async discover(): Promise<Payload[]> {
|
|
32
|
+
return await super.discover()
|
|
116
33
|
}
|
|
117
34
|
|
|
118
|
-
|
|
119
|
-
|
|
35
|
+
/* make it public */
|
|
36
|
+
override async manifest(): Promise<ModuleManifestPayload> {
|
|
37
|
+
return await super.manifest()
|
|
120
38
|
}
|
|
121
39
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
protected abstract get _queryAccountPaths(): Record<Query['schema'], string>
|
|
127
|
-
|
|
128
|
-
static async create<TModule extends Module>(this: CreatableModule<TModule>, params?: TModule['params']) {
|
|
129
|
-
if (!this.configSchemas || this.configSchemas.length === 0) {
|
|
130
|
-
throw Error(`Missing configSchema [${params?.config?.schema}][${this.name}]`)
|
|
131
|
-
}
|
|
132
|
-
const schema: string = params?.config?.schema ?? this.configSchema
|
|
133
|
-
const allowedSchemas: string[] = this.configSchemas
|
|
134
|
-
|
|
135
|
-
assertEx(
|
|
136
|
-
allowedSchemas.filter((allowedSchema) => allowedSchema === schema).length > 0,
|
|
137
|
-
`Bad Config Schema [Received ${schema}] [Expected ${JSON.stringify(allowedSchemas)}]`,
|
|
138
|
-
)
|
|
139
|
-
const mutatedConfig: TModule['params']['config'] = { ...params?.config, schema }
|
|
140
|
-
params?.logger?.debug(`config: ${JSON.stringify(mutatedConfig, null, 2)}`)
|
|
141
|
-
const mutatedParams = { ...params, config: mutatedConfig } as TModule['params']
|
|
142
|
-
const newModule = new this(AbstractModule.privateConstructorKey, mutatedParams)
|
|
143
|
-
await newModule.loadAccount?.()
|
|
144
|
-
await newModule.start?.()
|
|
145
|
-
return newModule
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
static factory<TModule extends Module>(this: CreatableModule<TModule>, params?: TModule['params']): CreatableModuleFactory<TModule> {
|
|
149
|
-
return ModuleFactory.withParams(this, params)
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
addressPreviousHash(): Promisable<AddressPreviousHashPayload> {
|
|
153
|
-
return { address: this.address, previousHash: this._account?.previousHash, schema: AddressPreviousHashSchema }
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
async describe(): Promise<ModuleDescription> {
|
|
157
|
-
const description: ModuleDescription = {
|
|
158
|
-
address: this.address,
|
|
159
|
-
queries: this.queries,
|
|
160
|
-
}
|
|
161
|
-
if (this.config.name) {
|
|
162
|
-
description.name = this.config.name
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
const discover = await this.discover()
|
|
166
|
-
|
|
167
|
-
description.children = compact(
|
|
168
|
-
discover?.map((payload) => {
|
|
169
|
-
const address = payload.schema === AddressSchema ? (payload as AddressPayload).address : undefined
|
|
170
|
-
return address != this.address ? address : undefined
|
|
171
|
-
}) ?? [],
|
|
172
|
-
)
|
|
173
|
-
|
|
174
|
-
return description
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
discover(): Promisable<Payload[]> {
|
|
178
|
-
const config = this.config
|
|
179
|
-
const address = new PayloadBuilder<AddressPayload>({ schema: AddressSchema }).fields({ address: this.address, name: this.config.name }).build()
|
|
180
|
-
const queries = this.queries.map((query) => {
|
|
181
|
-
return new PayloadBuilder<QueryPayload>({ schema: QuerySchema }).fields({ query }).build()
|
|
182
|
-
})
|
|
183
|
-
const configSchema: ConfigPayload = {
|
|
184
|
-
config: config.schema,
|
|
185
|
-
schema: ConfigSchema,
|
|
186
|
-
}
|
|
187
|
-
return compact([config, configSchema, address, ...queries])
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
async loadAccount() {
|
|
191
|
-
if (!this._account) {
|
|
192
|
-
const activeLogger = this.params.logger ?? AbstractModule.defaultLogger
|
|
193
|
-
let { account } = this.params as AccountModuleParams<TParams['config']>
|
|
194
|
-
const { wallet, accountDerivationPath } = this.params as WalletModuleParams<TParams['config']>
|
|
195
|
-
if (wallet) {
|
|
196
|
-
account = assertEx(accountDerivationPath ? await wallet.derivePath(accountDerivationPath) : wallet, 'Failed to derive account from path')
|
|
197
|
-
}
|
|
198
|
-
this.params.logger = activeLogger ? new IdLogger(activeLogger, () => `0x${account.address}`) : undefined
|
|
199
|
-
if (!account) console.warn(`AbstractModule.loadAccount: No account provided - Creating Random account [${this.config.schema}]`)
|
|
200
|
-
this._account = account ?? (await HDWallet.random())
|
|
201
|
-
}
|
|
202
|
-
this.downResolver.add(this as Module)
|
|
203
|
-
return this._account
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
manifest(): Promisable<ModuleManifestPayload> {
|
|
207
|
-
const name = assertEx(this.config.name, 'Calling manifest on un-named module is not supported')
|
|
208
|
-
return { config: { name, ...this.config }, schema: ModuleManifestPayloadSchema }
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
moduleAddressQuery(): Promisable<(AddressPayload | AddressPreviousHashPayload)[]> {
|
|
212
|
-
// Return array of all addresses and their previous hash
|
|
213
|
-
const queryAccounts = Object.entries(this.queryAccounts)
|
|
214
|
-
.filter((value): value is [string, AccountInstance] => {
|
|
215
|
-
return exists(value[1])
|
|
216
|
-
})
|
|
217
|
-
.map(([name, account]) => {
|
|
218
|
-
const address = account.address
|
|
219
|
-
const previousHash = account.previousHash
|
|
220
|
-
return [
|
|
221
|
-
{ address, name, schema: AddressSchema },
|
|
222
|
-
{ address, previousHash, schema: AddressPreviousHashSchema },
|
|
223
|
-
]
|
|
224
|
-
})
|
|
225
|
-
const address = this.address
|
|
226
|
-
const name = this.config.name
|
|
227
|
-
const previousHash = this.address
|
|
228
|
-
const moduleAccount = name ? { address, name, schema: AddressSchema } : { address, schema: AddressSchema }
|
|
229
|
-
const moduleAccountPreviousHash = previousHash
|
|
230
|
-
? { address, previousHash, schema: AddressPreviousHashSchema }
|
|
231
|
-
: { address, schema: AddressPreviousHashSchema }
|
|
232
|
-
return [moduleAccount, moduleAccountPreviousHash, ...queryAccounts].flat()
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
previousHash(): Promisable<string | undefined> {
|
|
236
|
-
return this.account.previousHash
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
async query<T extends QueryBoundWitness = QueryBoundWitness, TConfig extends ModuleConfig = ModuleConfig>(
|
|
240
|
-
query: T,
|
|
241
|
-
payloads?: Payload[],
|
|
242
|
-
queryConfig?: TConfig,
|
|
243
|
-
): Promise<ModuleQueryResult> {
|
|
244
|
-
this.started('throw')
|
|
245
|
-
const result = await this.queryHandler(assertEx(QueryBoundWitnessWrapper.unwrap(query)), payloads, queryConfig)
|
|
246
|
-
|
|
247
|
-
const args: ModuleQueriedEventArgs = { module: this as Module, payloads, query, result }
|
|
248
|
-
await this.emit('moduleQueried', args)
|
|
249
|
-
|
|
250
|
-
return result
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
queryable<T extends QueryBoundWitness = QueryBoundWitness, TConfig extends ModuleConfig = ModuleConfig>(
|
|
254
|
-
query: T,
|
|
255
|
-
payloads?: Payload[],
|
|
256
|
-
queryConfig?: TConfig,
|
|
257
|
-
): boolean {
|
|
258
|
-
if (!this.started('warn')) return false
|
|
259
|
-
const configValidator = queryConfig
|
|
260
|
-
? new ModuleConfigQueryValidator(Object.assign({}, this.config, queryConfig)).queryable
|
|
261
|
-
: this.moduleConfigQueryValidator
|
|
262
|
-
const validators = [this.supportedQueryValidator, configValidator]
|
|
263
|
-
|
|
264
|
-
return validators.every((validator) => validator(query, payloads))
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
async resolve<TModule extends Module = Module>(filter?: ModuleFilter): Promise<TModule[]>
|
|
268
|
-
async resolve<TModule extends Module = Module>(nameOrAddress: string): Promise<TModule | undefined>
|
|
269
|
-
async resolve<TModule extends Module = Module>(nameOrAddressOrFilter?: ModuleFilter | string): Promise<TModule | TModule[] | undefined> {
|
|
270
|
-
switch (typeof nameOrAddressOrFilter) {
|
|
271
|
-
case 'string': {
|
|
272
|
-
const byAddress = Account.isAddress(nameOrAddressOrFilter)
|
|
273
|
-
? (await this.resolve<TModule>({ address: [nameOrAddressOrFilter] })).pop()
|
|
274
|
-
: undefined
|
|
275
|
-
return byAddress ?? (await this.resolve<TModule>({ name: [nameOrAddressOrFilter] })).pop()
|
|
276
|
-
}
|
|
277
|
-
default: {
|
|
278
|
-
const filter: ModuleFilter | undefined = nameOrAddressOrFilter
|
|
279
|
-
return [...(await this.downResolver.resolve<TModule>(filter)), ...(await this.upResolver.resolve<TModule>(filter))].filter(duplicateModules)
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
async start(_timeout?: number): Promise<void> {
|
|
285
|
-
this.validateConfig()
|
|
286
|
-
await this.initializeQueryAccounts()
|
|
287
|
-
this._started = true
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
started(notStartedAction?: 'error' | 'throw' | 'warn' | 'log' | 'none') {
|
|
291
|
-
if (!this._started) {
|
|
292
|
-
switch (notStartedAction) {
|
|
293
|
-
case 'throw':
|
|
294
|
-
throw Error(`Module not Started [${this.address}]`)
|
|
295
|
-
case 'warn':
|
|
296
|
-
this.logger?.warn('Module not started')
|
|
297
|
-
break
|
|
298
|
-
case 'error':
|
|
299
|
-
this.logger?.error('Module not started')
|
|
300
|
-
break
|
|
301
|
-
case 'none':
|
|
302
|
-
break
|
|
303
|
-
case 'log':
|
|
304
|
-
default:
|
|
305
|
-
this.logger?.log('Module not started')
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
return this._started
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
subscribe(_queryAccount?: AccountInstance) {
|
|
312
|
-
return
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
protected bindHashes(hashes: string[], schema: SchemaString[], account?: AccountInstance) {
|
|
316
|
-
const promise = new PromiseEx((resolve) => {
|
|
317
|
-
const result = this.bindHashesInternal(hashes, schema, account)
|
|
318
|
-
resolve?.(result)
|
|
319
|
-
return result
|
|
320
|
-
}, account)
|
|
321
|
-
return promise
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
protected async bindHashesInternal(hashes: string[], schema: SchemaString[], account?: AccountInstance): Promise<BoundWitness> {
|
|
325
|
-
const builder = new BoundWitnessBuilder().hashes(hashes, schema).witness(this.account)
|
|
326
|
-
const result = (await (account ? builder.witness(account) : builder).build())[0]
|
|
327
|
-
this.logger?.debug(`result: ${JSON.stringify(result, null, 2)}`)
|
|
328
|
-
return result
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
protected bindQuery<T extends Query | PayloadWrapper<Query>>(
|
|
332
|
-
query: T,
|
|
333
|
-
payloads?: Payload[],
|
|
334
|
-
account?: AccountInstance,
|
|
335
|
-
): PromiseEx<[QueryBoundWitness, Payload[], Payload[]], AccountInstance> {
|
|
336
|
-
const promise = new PromiseEx<[QueryBoundWitness, Payload[], Payload[]], AccountInstance>(async (resolve) => {
|
|
337
|
-
const result = await this.bindQueryInternal(query, payloads, account)
|
|
338
|
-
resolve?.(result)
|
|
339
|
-
return result
|
|
340
|
-
}, account)
|
|
341
|
-
return promise
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
protected async bindQueryInternal<T extends Query | PayloadWrapper<Query>>(
|
|
345
|
-
query: T,
|
|
346
|
-
payloads?: Payload[],
|
|
347
|
-
account?: AccountInstance,
|
|
348
|
-
): Promise<[QueryBoundWitness, Payload[], Payload[]]> {
|
|
349
|
-
const builder = new QueryBoundWitnessBuilder().payloads(payloads).witness(this.account).query(query)
|
|
350
|
-
const result = await (account ? builder.witness(account) : builder).build()
|
|
351
|
-
return result
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
protected async bindQueryResult<T extends Query | PayloadWrapper<Query>>(
|
|
355
|
-
query: T,
|
|
356
|
-
payloads: Payload[],
|
|
357
|
-
additionalWitnesses: AccountInstance[] = [],
|
|
358
|
-
errors?: ModuleError[],
|
|
359
|
-
): Promise<[ModuleQueryResult, AccountInstance[]]> {
|
|
360
|
-
const builder = new BoundWitnessBuilder().payloads(payloads).errors(errors)
|
|
361
|
-
const queryWitnessAccount = this.queryAccounts[query.schema as ModuleQueryBase['schema']]
|
|
362
|
-
const witnesses = [this.account, queryWitnessAccount, ...additionalWitnesses].filter(exists)
|
|
363
|
-
builder.witnesses(witnesses)
|
|
364
|
-
const result: ModuleQueryResult = [(await builder.build())[0], payloads, errors ?? []]
|
|
365
|
-
return [result, witnesses]
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
protected commitArchivist = () => this.getArchivist('commit')
|
|
369
|
-
|
|
370
|
-
protected async initializeQueryAccounts() {
|
|
371
|
-
// Ensure distinct/unique wallet paths
|
|
372
|
-
const paths = Object.values(this.queryAccountPaths).filter(exists)
|
|
373
|
-
const distinctPaths = new Set<string>(paths)
|
|
374
|
-
assertEx(distinctPaths.size === paths.length, `${this.config?.name ? this.config.name + ': ' : ''}Duplicate query account paths`)
|
|
375
|
-
// Create an account for query this module supports
|
|
376
|
-
const wallet = this.account as unknown as HDWallet
|
|
377
|
-
if (wallet?.derivePath) {
|
|
378
|
-
for (const key in this.queryAccountPaths) {
|
|
379
|
-
if (Object.prototype.hasOwnProperty.call(this.queryAccountPaths, key)) {
|
|
380
|
-
const query = key as ModuleQueryBase['schema']
|
|
381
|
-
const queryAccountPath = this.queryAccountPaths[query]
|
|
382
|
-
if (queryAccountPath) {
|
|
383
|
-
this._queryAccounts[query] = await wallet.derivePath?.(queryAccountPath)
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
protected async queryHandler<T extends QueryBoundWitness = QueryBoundWitness, TConfig extends ModuleConfig = ModuleConfig>(
|
|
391
|
-
query: T,
|
|
392
|
-
payloads?: Payload[],
|
|
393
|
-
queryConfig?: TConfig,
|
|
394
|
-
): Promise<ModuleQueryResult> {
|
|
395
|
-
this.started('throw')
|
|
396
|
-
const wrapper = QueryBoundWitnessWrapper.parseQuery<ModuleQuery>(query, payloads)
|
|
397
|
-
if (!this.allowAnonymous) {
|
|
398
|
-
if (query.addresses.length === 0) {
|
|
399
|
-
console.warn(`Anonymous Queries not allowed, but running anyway [${this.config.name}], [${this.address}]`)
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
const queryPayload = await wrapper.getQuery()
|
|
403
|
-
assertEx(this.queryable(query, payloads, queryConfig))
|
|
404
|
-
const resultPayloads: Payload[] = []
|
|
405
|
-
const errorPayloads: ModuleError[] = []
|
|
406
|
-
const queryAccount = this.ephemeralQueryAccountEnabled ? await HDWallet.random() : undefined
|
|
407
|
-
try {
|
|
408
|
-
switch (queryPayload.schema) {
|
|
409
|
-
case ModuleDiscoverQuerySchema: {
|
|
410
|
-
resultPayloads.push(...(await this.discover()))
|
|
411
|
-
break
|
|
412
|
-
}
|
|
413
|
-
case ModuleAddressQuerySchema: {
|
|
414
|
-
resultPayloads.push(...(await this.moduleAddressQuery()))
|
|
415
|
-
break
|
|
416
|
-
}
|
|
417
|
-
case ModuleSubscribeQuerySchema: {
|
|
418
|
-
this.subscribe(queryAccount)
|
|
419
|
-
break
|
|
420
|
-
}
|
|
421
|
-
default:
|
|
422
|
-
console.error(`Unsupported Query [${(queryPayload as Payload).schema}]`)
|
|
423
|
-
}
|
|
424
|
-
} catch (ex) {
|
|
425
|
-
await handleErrorAsync(ex, async (error) => {
|
|
426
|
-
errorPayloads.push(
|
|
427
|
-
new ModuleErrorBuilder()
|
|
428
|
-
.sources([await wrapper.hashAsync()])
|
|
429
|
-
.name(this.config.name ?? '<Unknown>')
|
|
430
|
-
.query(query.schema)
|
|
431
|
-
.message(error.message)
|
|
432
|
-
.build(),
|
|
433
|
-
)
|
|
434
|
-
})
|
|
435
|
-
}
|
|
436
|
-
return (await this.bindQueryResult(queryPayload, resultPayloads, queryAccount ? [queryAccount] : [], errorPayloads))[0]
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
protected readArchivist = () => this.getArchivist('read')
|
|
440
|
-
|
|
441
|
-
protected stop(_timeout?: number): Promisable<this> {
|
|
442
|
-
this._started = false
|
|
443
|
-
return this
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
protected validateConfig(config?: unknown, parents: string[] = []): boolean {
|
|
447
|
-
return Object.entries(config ?? this.config ?? {}).reduce((valid, [key, value]) => {
|
|
448
|
-
switch (typeof value) {
|
|
449
|
-
case 'function':
|
|
450
|
-
this.logger?.warn(`Fields of type function not allowed in config [${parents?.join('.')}.${key}]`)
|
|
451
|
-
return false
|
|
452
|
-
case 'object': {
|
|
453
|
-
if (Array.isArray(value)) {
|
|
454
|
-
return (
|
|
455
|
-
value.reduce((valid, value) => {
|
|
456
|
-
return this.validateConfig(value, [...parents, key]) && valid
|
|
457
|
-
}, true) && valid
|
|
458
|
-
)
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
if (!serializableField(value)) {
|
|
462
|
-
this.logger?.warn(`Fields that are not serializable to JSON are not allowed in config [${parents?.join('.')}.${key}]`)
|
|
463
|
-
return false
|
|
464
|
-
}
|
|
465
|
-
return value ? this.validateConfig(value, [...parents, key]) && valid : true
|
|
466
|
-
}
|
|
467
|
-
default:
|
|
468
|
-
return valid
|
|
469
|
-
}
|
|
470
|
-
}, true)
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
protected writeArchivist = () => this.getArchivist('write')
|
|
474
|
-
|
|
475
|
-
private async getArchivist(kind: keyof IndividualArchivistConfig): Promise<ArchivistModule | undefined> {
|
|
476
|
-
if (!this.config.archivist) return undefined
|
|
477
|
-
const filter =
|
|
478
|
-
typeof this.config.archivist === 'string' || this.config.archivist instanceof String
|
|
479
|
-
? (this.config.archivist as string)
|
|
480
|
-
: (this.config?.archivist?.[kind] as string)
|
|
481
|
-
const resolved = await this.upResolver.resolveOne(filter)
|
|
482
|
-
return resolved ? (resolved as ArchivistModule) : undefined
|
|
40
|
+
/* make it public */
|
|
41
|
+
override subscribe(_queryAccount?: AccountInstance) {
|
|
42
|
+
return super.subscribe()
|
|
483
43
|
}
|
|
484
44
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { IndirectModule, ModuleQuery, QueryBoundWitness } from '@xyo-network/module-model'
|
|
2
2
|
import { Payload } from '@xyo-network/payload-model'
|
|
3
3
|
|
|
4
4
|
import { QueryBoundWitnessWrapper } from '../Query'
|
|
5
5
|
import { Queryable, QueryValidator } from './QueryValidator'
|
|
6
6
|
|
|
7
7
|
export const isQuerySupportedByModule = async <T extends QueryBoundWitness = QueryBoundWitness>(
|
|
8
|
-
mod:
|
|
8
|
+
mod: IndirectModule,
|
|
9
9
|
query: T,
|
|
10
10
|
payloads?: Payload[],
|
|
11
11
|
): Promise<boolean> => {
|
|
@@ -15,7 +15,7 @@ export const isQuerySupportedByModule = async <T extends QueryBoundWitness = Que
|
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
export class SupportedQueryValidator implements QueryValidator {
|
|
18
|
-
constructor(protected readonly mod:
|
|
18
|
+
constructor(protected readonly mod: IndirectModule) {}
|
|
19
19
|
queryable: Queryable = (query, payloads) => {
|
|
20
20
|
return isQuerySupportedByModule(this.mod, query, payloads)
|
|
21
21
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { fulfilled } from '@xylabs/promise'
|
|
2
2
|
import { Base, BaseParams } from '@xyo-network/core'
|
|
3
|
-
import {
|
|
3
|
+
import { IndirectModule, ModuleFilter, ModuleRepository, ModuleResolver } from '@xyo-network/module-model'
|
|
4
4
|
|
|
5
5
|
import { duplicateModules } from '../lib'
|
|
6
6
|
import { SimpleModuleResolver } from './SimpleModuleResolver'
|
|
@@ -20,9 +20,9 @@ export class CompositeModuleResolver extends Base implements ModuleRepository, M
|
|
|
20
20
|
return true
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
add(module:
|
|
24
|
-
add(module:
|
|
25
|
-
add(module:
|
|
23
|
+
add(module: IndirectModule): this
|
|
24
|
+
add(module: IndirectModule[]): this
|
|
25
|
+
add(module: IndirectModule | IndirectModule[]): this {
|
|
26
26
|
if (Array.isArray(module)) {
|
|
27
27
|
module.forEach((module) => this.addSingleModule(module))
|
|
28
28
|
} else {
|
|
@@ -50,10 +50,9 @@ export class CompositeModuleResolver extends Base implements ModuleRepository, M
|
|
|
50
50
|
return this
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
async resolve<T extends
|
|
54
|
-
const modules = this.resolvers.map((resolver) => resolver.resolve(filter))
|
|
55
|
-
const
|
|
56
|
-
const result = settled
|
|
53
|
+
async resolve<T extends IndirectModule = IndirectModule>(filter?: ModuleFilter): Promise<T[]> {
|
|
54
|
+
const modules = await Promise.allSettled(this.resolvers.map((resolver) => resolver.resolve(filter)))
|
|
55
|
+
const result = modules
|
|
57
56
|
.filter(fulfilled)
|
|
58
57
|
.map((r) => r.value)
|
|
59
58
|
.flat()
|
|
@@ -61,7 +60,7 @@ export class CompositeModuleResolver extends Base implements ModuleRepository, M
|
|
|
61
60
|
return result as T[]
|
|
62
61
|
}
|
|
63
62
|
|
|
64
|
-
async resolveOne<T extends
|
|
63
|
+
async resolveOne<T extends IndirectModule = IndirectModule>(addressOrName: string): Promise<T | undefined> {
|
|
65
64
|
for (let i = 0; i < this.resolvers.length; i++) {
|
|
66
65
|
const resolver = this.resolvers[i]
|
|
67
66
|
const result = await resolver.resolveOne<T>(addressOrName)
|
|
@@ -70,7 +69,7 @@ export class CompositeModuleResolver extends Base implements ModuleRepository, M
|
|
|
70
69
|
return undefined
|
|
71
70
|
}
|
|
72
71
|
|
|
73
|
-
private addSingleModule(module?:
|
|
72
|
+
private addSingleModule(module?: IndirectModule) {
|
|
74
73
|
if (module) {
|
|
75
74
|
this.localResolver.add(module)
|
|
76
75
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { IndirectModule, ModuleFilter, ModuleResolver } from '@xyo-network/module-model'
|
|
2
2
|
|
|
3
3
|
export interface ModuleResolvedEventArgs {
|
|
4
4
|
filter?: ModuleFilter
|
|
5
|
-
module:
|
|
5
|
+
module: IndirectModule
|
|
6
6
|
}
|
|
7
7
|
|
|
8
8
|
export interface ResolverEventEmitter {
|
|
@@ -18,7 +18,7 @@ const getMixin = <T extends ModuleResolver = ModuleResolver>(resolver: T) => {
|
|
|
18
18
|
listeners.map((listener) => listener(args))
|
|
19
19
|
return true
|
|
20
20
|
}
|
|
21
|
-
const onModuleResolved = (module:
|
|
21
|
+
const onModuleResolved = (module: IndirectModule, filter?: ModuleFilter) => {
|
|
22
22
|
const args = { filter, module }
|
|
23
23
|
emit('moduleResolved', args)
|
|
24
24
|
}
|
|
@@ -31,8 +31,8 @@ const getMixin = <T extends ModuleResolver = ModuleResolver>(resolver: T) => {
|
|
|
31
31
|
on: (event: 'moduleResolved', listener: (args: ModuleResolvedEventArgs) => void) => {
|
|
32
32
|
listeners.push(listener)
|
|
33
33
|
},
|
|
34
|
-
resolve: async (filter?: ModuleFilter): Promise<
|
|
35
|
-
const modules:
|
|
34
|
+
resolve: async (filter?: ModuleFilter): Promise<IndirectModule[]> => {
|
|
35
|
+
const modules: IndirectModule[] = await originalResolve(filter)
|
|
36
36
|
await Promise.allSettled(modules.map((mod) => onModuleResolved(mod, filter)))
|
|
37
37
|
return modules
|
|
38
38
|
},
|