@xyo-network/bridge-pub-sub 2.94.7 → 2.94.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.
Files changed (36) hide show
  1. package/dist/browser/AsyncQueryBus/AsyncQueryBusBase.d.cts +16 -6
  2. package/dist/browser/AsyncQueryBus/AsyncQueryBusBase.d.cts.map +1 -1
  3. package/dist/browser/AsyncQueryBus/AsyncQueryBusBase.d.mts +16 -6
  4. package/dist/browser/AsyncQueryBus/AsyncQueryBusBase.d.mts.map +1 -1
  5. package/dist/browser/AsyncQueryBus/AsyncQueryBusBase.d.ts +16 -6
  6. package/dist/browser/AsyncQueryBus/AsyncQueryBusBase.d.ts.map +1 -1
  7. package/dist/browser/AsyncQueryBus/AsyncQueryBusClient.d.cts.map +1 -1
  8. package/dist/browser/AsyncQueryBus/AsyncQueryBusClient.d.mts.map +1 -1
  9. package/dist/browser/AsyncQueryBus/AsyncQueryBusClient.d.ts.map +1 -1
  10. package/dist/browser/AsyncQueryBus/AsyncQueryBusHost.d.cts.map +1 -1
  11. package/dist/browser/AsyncQueryBus/AsyncQueryBusHost.d.mts.map +1 -1
  12. package/dist/browser/AsyncQueryBus/AsyncQueryBusHost.d.ts.map +1 -1
  13. package/dist/browser/index.cjs +62 -24
  14. package/dist/browser/index.cjs.map +1 -1
  15. package/dist/browser/index.js +62 -24
  16. package/dist/browser/index.js.map +1 -1
  17. package/dist/node/AsyncQueryBus/AsyncQueryBusBase.d.cts +16 -6
  18. package/dist/node/AsyncQueryBus/AsyncQueryBusBase.d.cts.map +1 -1
  19. package/dist/node/AsyncQueryBus/AsyncQueryBusBase.d.mts +16 -6
  20. package/dist/node/AsyncQueryBus/AsyncQueryBusBase.d.mts.map +1 -1
  21. package/dist/node/AsyncQueryBus/AsyncQueryBusBase.d.ts +16 -6
  22. package/dist/node/AsyncQueryBus/AsyncQueryBusBase.d.ts.map +1 -1
  23. package/dist/node/AsyncQueryBus/AsyncQueryBusClient.d.cts.map +1 -1
  24. package/dist/node/AsyncQueryBus/AsyncQueryBusClient.d.mts.map +1 -1
  25. package/dist/node/AsyncQueryBus/AsyncQueryBusClient.d.ts.map +1 -1
  26. package/dist/node/AsyncQueryBus/AsyncQueryBusHost.d.cts.map +1 -1
  27. package/dist/node/AsyncQueryBus/AsyncQueryBusHost.d.mts.map +1 -1
  28. package/dist/node/AsyncQueryBus/AsyncQueryBusHost.d.ts.map +1 -1
  29. package/dist/node/index.cjs +80 -24
  30. package/dist/node/index.cjs.map +1 -1
  31. package/dist/node/index.js +80 -24
  32. package/dist/node/index.js.map +1 -1
  33. package/package.json +19 -19
  34. package/src/AsyncQueryBus/AsyncQueryBusBase.ts +44 -4
  35. package/src/AsyncQueryBus/AsyncQueryBusClient.ts +13 -3
  36. package/src/AsyncQueryBus/AsyncQueryBusHost.ts +13 -4
@@ -1,7 +1,7 @@
1
1
  import { assertEx } from '@xylabs/assert'
2
2
  import { Address } from '@xylabs/hex'
3
3
  import { Base, toJsonString } from '@xylabs/object'
4
- import { asArchivistInstance } from '@xyo-network/archivist-model'
4
+ import { ArchivistInstance, asArchivistInstance } from '@xyo-network/archivist-model'
5
5
  import { BoundWitness, QueryBoundWitness } from '@xyo-network/boundwitness-model'
6
6
  import { BoundWitnessDivinerParams, BoundWitnessDivinerQueryPayload } from '@xyo-network/diviner-boundwitness-model'
7
7
  import { asDivinerInstance, DivinerInstance } from '@xyo-network/diviner-model'
@@ -15,6 +15,16 @@ export class AsyncQueryBusBase<TParams extends AsyncQueryBusParams = AsyncQueryB
15
15
  protected _targetConfigs: Record<Address, ModuleConfig> = {}
16
16
  protected _targetQueries: Record<Address, string[]> = {}
17
17
 
18
+ private _lastQueriesArchivistAttempt?: number
19
+ private _lastQueriesDivinerAttempt?: number
20
+ private _lastResponseArchivistAttempt?: number
21
+ private _lastResponseDivinerAttempt?: number
22
+ private _queriesArchivist?: ArchivistInstance
23
+ private _queriesDiviner?: DivinerInstance<BoundWitnessDivinerParams, BoundWitnessDivinerQueryPayload, QueryBoundWitness>
24
+ private _reResolveDelay = 5000
25
+ private _responseArchivist?: ArchivistInstance
26
+ private _responseDiviner?: DivinerInstance<BoundWitnessDivinerParams, BoundWitnessDivinerQueryPayload, BoundWitness>
27
+
18
28
  constructor(params: TParams) {
19
29
  super(params)
20
30
  }
@@ -41,6 +51,12 @@ export class AsyncQueryBusBase<TParams extends AsyncQueryBusParams = AsyncQueryB
41
51
  }
42
52
 
43
53
  async queriesArchivist() {
54
+ if (this._queriesArchivist) {
55
+ return this._queriesArchivist
56
+ }
57
+ if (this._lastQueriesArchivistAttempt && Date.now() - this._lastQueriesArchivistAttempt < this._reResolveDelay) {
58
+ return
59
+ }
44
60
  const resolved = await this.resolver.resolve(this.config?.intersect?.queries?.archivist, { direction: 'up' })
45
61
  const existingResolved = assertEx(resolved, () => `Unable to resolve queriesArchivist [${this.config?.intersect?.queries?.archivist}]`)
46
62
  const result = asArchivistInstance(
@@ -48,28 +64,52 @@ export class AsyncQueryBusBase<TParams extends AsyncQueryBusParams = AsyncQueryB
48
64
  () =>
49
65
  `Unable to resolve queriesArchivist as correct type [${this.config?.intersect?.queries?.archivist}][${existingResolved?.constructor?.name}]: ${toJsonString(existingResolved)}`,
50
66
  )
67
+ this._queriesArchivist = result
51
68
  return result
52
69
  }
53
70
 
54
71
  async queriesDiviner() {
55
- return assertEx(
72
+ if (this._queriesDiviner) {
73
+ return this._queriesDiviner
74
+ }
75
+ if (this._lastQueriesDivinerAttempt && Date.now() - this._lastQueriesDivinerAttempt < this._reResolveDelay) {
76
+ return
77
+ }
78
+ this._queriesDiviner = assertEx(
56
79
  asDivinerInstance(await this.resolver.resolve(this.config?.intersect?.queries?.boundWitnessDiviner)),
57
80
  () => `Unable to resolve queriesDiviner [${this.config?.intersect?.queries?.boundWitnessDiviner}]`,
58
81
  ) as DivinerInstance<BoundWitnessDivinerParams, BoundWitnessDivinerQueryPayload, QueryBoundWitness>
82
+
83
+ return this._queriesDiviner
59
84
  }
60
85
 
61
86
  async responsesArchivist() {
62
- return assertEx(
87
+ if (this._responseArchivist) {
88
+ return this._responseArchivist
89
+ }
90
+ if (this._lastResponseArchivistAttempt && Date.now() - this._lastResponseArchivistAttempt < this._reResolveDelay) {
91
+ return
92
+ }
93
+ this._responseArchivist = assertEx(
63
94
  asArchivistInstance(await this.resolver.resolve(this.config?.intersect?.responses?.archivist)),
64
95
  () => `Unable to resolve responsesArchivist [${this.config?.intersect?.responses?.archivist}]`,
65
96
  )
97
+ return this._responseArchivist
66
98
  }
67
99
 
68
100
  async responsesDiviner() {
69
- return assertEx(
101
+ if (this._responseDiviner) {
102
+ return this._responseDiviner
103
+ }
104
+ if (this._lastResponseDivinerAttempt && Date.now() - this._lastResponseDivinerAttempt < this._reResolveDelay) {
105
+ return
106
+ }
107
+ this._responseDiviner = assertEx(
70
108
  asDivinerInstance(await this.resolver.resolve(this.config?.intersect?.responses?.boundWitnessDiviner)),
71
109
  () => `Unable to resolve responsesDiviner [${this.config?.intersect?.responses?.boundWitnessDiviner}]`,
72
110
  ) as DivinerInstance<BoundWitnessDivinerParams, BoundWitnessDivinerQueryPayload, BoundWitness>
111
+
112
+ return this._responseDiviner
73
113
  }
74
114
 
75
115
  /**
@@ -1,3 +1,4 @@
1
+ import { assertEx } from '@xylabs/assert'
1
2
  import { delay } from '@xylabs/delay'
2
3
  import { forget } from '@xylabs/forget'
3
4
  import { Address } from '@xylabs/hex'
@@ -50,7 +51,10 @@ export class AsyncQueryBusClient<TParams extends AsyncQueryBusClientParams = Asy
50
51
  const $meta = { ...query?.$meta, destination: [address] }
51
52
  const routedQuery = await PayloadBuilder.build({ ...query, $meta })
52
53
  //console.log('queryArchivist - calling')
53
- const queryArchivist = await this.queriesArchivist()
54
+ const queryArchivist = assertEx(
55
+ await this.queriesArchivist(),
56
+ () => `Unable to contact queriesArchivist [${this.config?.intersect?.queries?.archivist}]`,
57
+ )
54
58
  //console.log('queryArchivist')
55
59
 
56
60
  // TODO: Should we always re-hash to true up timestamps? We can't
@@ -134,8 +138,14 @@ export class AsyncQueryBusClient<TParams extends AsyncQueryBusClientParams = Asy
134
138
  * Background process for processing incoming responses to previously issued queries
135
139
  */
136
140
  private processIncomingResponses = async () => {
137
- const responseArchivist = await this.responsesArchivist()
138
- const responseBoundWitnessDiviner = await this.responsesDiviner()
141
+ const responseArchivist = assertEx(
142
+ await this.responsesArchivist(),
143
+ () => `Unable to contact the responsesArchivist [${this.config?.intersect?.responses?.archivist}]`,
144
+ )
145
+ const responseBoundWitnessDiviner = assertEx(
146
+ await this.responsesDiviner(),
147
+ () => `Unable to contact responsesDiviner [${this.config?.intersect?.responses?.boundWitnessDiviner}]`,
148
+ )
139
149
  const pendingCommands = [...this.queryCache.entries()].filter(([_, status]) => status === Pending)
140
150
  // TODO: Do in throttled batches
141
151
  await Promise.allSettled(
@@ -79,8 +79,14 @@ export class AsyncQueryBusHost<TParams extends AsyncQueryBusHostParams = AsyncQu
79
79
 
80
80
  protected callLocalModule = async (localModule: ModuleInstance, query: WithMeta<QueryBoundWitness>) => {
81
81
  const localModuleName = localModule.config.name ?? localModule.address
82
- const queryArchivist = await this.queriesArchivist()
83
- const responseArchivist = await this.responsesArchivist()
82
+ const queryArchivist = assertEx(
83
+ await this.queriesArchivist(),
84
+ () => `Unable to contact queriesArchivist [${this.config?.intersect?.queries?.archivist}]`,
85
+ )
86
+ const responsesArchivist = assertEx(
87
+ await this.responsesArchivist(),
88
+ () => `Unable to contact responsesArchivist [${this.config?.intersect?.queries?.archivist}]`,
89
+ )
84
90
  const queryDestination = (query.$meta as { destination?: string[] })?.destination
85
91
  if (queryDestination && queryDestination?.includes(localModule.address)) {
86
92
  // Find the query
@@ -108,7 +114,7 @@ export class AsyncQueryBusHost<TParams extends AsyncQueryBusHostParams = AsyncQu
108
114
  })
109
115
  const [bw, payloads, errors] = response
110
116
  this.logger?.debug(`Replying to query ${queryHash} addressed to module: ${localModuleName}`)
111
- const insertResult = await responseArchivist.insert([bw, ...payloads, ...errors])
117
+ const insertResult = await responsesArchivist.insert([bw, ...payloads, ...errors])
112
118
  // NOTE: If all archivists support the contract that numPayloads inserted === numPayloads returned we can
113
119
  // do some deeper assertions here like lenIn === lenOut, but for now this should be good enough since BWs
114
120
  // should always be unique causing at least one insertion
@@ -135,7 +141,10 @@ export class AsyncQueryBusHost<TParams extends AsyncQueryBusHostParams = AsyncQu
135
141
  * @param address The address to find commands for
136
142
  */
137
143
  protected findQueriesToAddress = async (address: Address) => {
138
- const queryBoundWitnessDiviner = await this.queriesDiviner()
144
+ const queryBoundWitnessDiviner = assertEx(
145
+ await this.queriesDiviner(),
146
+ () => `Unable to resolve queriesDiviner [${this.config?.intersect?.queries?.boundWitnessDiviner}]`,
147
+ )
139
148
  // Retrieve last offset from state store
140
149
  const timestamp = await this.retrieveState(address)
141
150
  const destination = [address]