@xyo-network/module-abstract 2.67.0 → 2.69.0-rc.1

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.
@@ -1,13 +1,14 @@
1
1
  /* eslint-disable max-lines */
2
2
  import { assertEx } from '@xylabs/assert'
3
3
  import { exists } from '@xylabs/exists'
4
- import { Account, HDWallet } from '@xyo-network/account'
4
+ import { HDWallet } from '@xyo-network/account'
5
5
  import { AccountInstance } from '@xyo-network/account-model'
6
6
  import { AddressPayload, AddressSchema } from '@xyo-network/address-payload-plugin'
7
7
  import { ArchivistInstance, asArchivistInstance } from '@xyo-network/archivist-model'
8
8
  import { BoundWitnessBuilder, QueryBoundWitness, QueryBoundWitnessBuilder, QueryBoundWitnessWrapper } from '@xyo-network/boundwitness-builder'
9
9
  import { BoundWitness } from '@xyo-network/boundwitness-model'
10
10
  import { ConfigPayload, ConfigSchema } from '@xyo-network/config-payload-plugin'
11
+ import { PayloadHasher } from '@xyo-network/core'
11
12
  import { handleError, handleErrorAsync } from '@xyo-network/error'
12
13
  import { ModuleManifestPayload, ModuleManifestPayloadSchema } from '@xyo-network/manifest-model'
13
14
  import {
@@ -36,6 +37,7 @@ import {
36
37
  ModuleQueriedEventArgs,
37
38
  ModuleQuery,
38
39
  ModuleQueryBase,
40
+ ModuleQueryHandlerResult,
39
41
  ModuleQueryResult,
40
42
  ModuleResolver,
41
43
  ModuleSubscribeQuerySchema,
@@ -238,12 +240,32 @@ export abstract class AbstractModule<
238
240
  queryConfig?: TConfig,
239
241
  ): Promise<ModuleQueryResult> {
240
242
  return await this.busy(async () => {
241
- await this.started('throw')
242
- const result = await this.queryHandler(assertEx(QueryBoundWitnessWrapper.unwrap(query)), payloads, queryConfig)
243
-
243
+ const resultPayloads: Payload[] = []
244
+ const errorPayloads: ModuleError[] = []
245
+ const queryAccount = this.ephemeralQueryAccountEnabled ? await HDWallet.random() : undefined
246
+ try {
247
+ await this.started('throw')
248
+ if (!this.allowAnonymous) {
249
+ if (query.addresses.length === 0) {
250
+ throw Error(`Anonymous Queries not allowed, but running anyway [${this.config.name}], [${this.address}]`)
251
+ }
252
+ }
253
+ resultPayloads.push(...(await this.queryHandler(assertEx(QueryBoundWitnessWrapper.unwrap(query)), payloads, queryConfig)))
254
+ } catch (ex) {
255
+ await handleErrorAsync(ex, async (error) => {
256
+ errorPayloads.push(
257
+ new ModuleErrorBuilder()
258
+ .sources([await PayloadHasher.hashAsync(query)])
259
+ .name(this.config.name ?? '<Unknown>')
260
+ .query(query.schema)
261
+ .message(error.message)
262
+ .build(),
263
+ )
264
+ })
265
+ }
266
+ const result = await this.bindQueryResult(query, resultPayloads, queryAccount ? [queryAccount] : [], errorPayloads)
244
267
  const args: ModuleQueriedEventArgs = { module: this, payloads, query, result }
245
268
  await this.emit('moduleQueried', args)
246
-
247
269
  return result
248
270
  })
249
271
  }
@@ -392,13 +414,13 @@ export abstract class AbstractModule<
392
414
  payloads: Payload[],
393
415
  additionalWitnesses: AccountInstance[] = [],
394
416
  errors?: ModuleError[],
395
- ): Promise<[ModuleQueryResult, AccountInstance[]]> {
417
+ ): Promise<ModuleQueryResult> {
396
418
  const builder = new BoundWitnessBuilder().payloads(payloads).errors(errors)
397
419
  const queryWitnessAccount = this.queryAccounts[query.schema as ModuleQueryBase['schema']]
398
420
  const witnesses = [this.account, queryWitnessAccount, ...additionalWitnesses].filter(exists)
399
421
  builder.witnesses(witnesses)
400
422
  const result: ModuleQueryResult = [(await builder.build())[0], payloads, errors ?? []]
401
- return [result, witnesses]
423
+ return result
402
424
  }
403
425
 
404
426
  protected commitArchivist = () => this.getArchivist('commit')
@@ -491,57 +513,37 @@ export abstract class AbstractModule<
491
513
  query: T,
492
514
  payloads?: Payload[],
493
515
  queryConfig?: TConfig,
494
- ): Promise<ModuleQueryResult> {
516
+ ): Promise<ModuleQueryHandlerResult> {
495
517
  await this.started('throw')
496
518
  const wrapper = QueryBoundWitnessWrapper.parseQuery<ModuleQuery>(query, payloads)
497
- if (!this.allowAnonymous) {
498
- if (query.addresses.length === 0) {
499
- console.warn(`Anonymous Queries not allowed, but running anyway [${this.config.name}], [${this.address}]`)
500
- }
501
- }
502
519
  const queryPayload = await wrapper.getQuery()
503
520
  assertEx(this.queryable(query, payloads, queryConfig))
504
521
  const resultPayloads: Payload[] = []
505
- const errorPayloads: ModuleError[] = []
506
- const queryAccount = this.ephemeralQueryAccountEnabled ? Account.randomSync() : undefined
507
- try {
508
- switch (queryPayload.schema) {
509
- case ModuleManifestQuerySchema: {
510
- resultPayloads.push(await this.manifestHandler())
511
- break
512
- }
513
- case ModuleDiscoverQuerySchema: {
514
- resultPayloads.push(...(await this.discoverHandler()))
515
- break
516
- }
517
- case ModuleDescribeQuerySchema: {
518
- resultPayloads.push(await this.describeHandler())
519
- break
520
- }
521
- case ModuleAddressQuerySchema: {
522
- resultPayloads.push(...(await this.moduleAddressHandler()))
523
- break
524
- }
525
- case ModuleSubscribeQuerySchema: {
526
- this.subscribeHandler(queryAccount)
527
- break
528
- }
529
- default:
530
- console.error(`Unsupported Query [${(queryPayload as Payload).schema}]`)
522
+ switch (queryPayload.schema) {
523
+ case ModuleManifestQuerySchema: {
524
+ resultPayloads.push(await this.manifestHandler())
525
+ break
531
526
  }
532
- } catch (ex) {
533
- await handleErrorAsync(ex, async (error) => {
534
- errorPayloads.push(
535
- new ModuleErrorBuilder()
536
- .sources([await wrapper.hashAsync()])
537
- .name(this.config.name ?? '<Unknown>')
538
- .query(query.schema)
539
- .message(error.message)
540
- .build(),
541
- )
542
- })
527
+ case ModuleDiscoverQuerySchema: {
528
+ resultPayloads.push(...(await this.discoverHandler()))
529
+ break
530
+ }
531
+ case ModuleDescribeQuerySchema: {
532
+ resultPayloads.push(await this.describeHandler())
533
+ break
534
+ }
535
+ case ModuleAddressQuerySchema: {
536
+ resultPayloads.push(...(await this.moduleAddressHandler()))
537
+ break
538
+ }
539
+ case ModuleSubscribeQuerySchema: {
540
+ this.subscribeHandler()
541
+ break
542
+ }
543
+ default:
544
+ throw Error(`Unsupported Query [${(queryPayload as Payload).schema}]`)
543
545
  }
544
- return (await this.bindQueryResult(queryPayload, resultPayloads, queryAccount ? [queryAccount] : [], errorPayloads))[0]
546
+ return resultPayloads
545
547
  }
546
548
 
547
549
  protected readArchivist = () => this.getArchivist('read')
@@ -558,7 +560,7 @@ export abstract class AbstractModule<
558
560
  return true
559
561
  }
560
562
 
561
- protected subscribeHandler(_queryAccount?: AccountInstance) {
563
+ protected subscribeHandler() {
562
564
  return
563
565
  }
564
566