@xyo-network/bridge-pub-sub 2.94.16 → 2.94.18

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 (29) hide show
  1. package/dist/browser/AsyncQueryBus/AsyncQueryBusBase.d.cts +7 -72
  2. package/dist/browser/AsyncQueryBus/AsyncQueryBusBase.d.cts.map +1 -1
  3. package/dist/browser/AsyncQueryBus/AsyncQueryBusBase.d.mts +7 -72
  4. package/dist/browser/AsyncQueryBus/AsyncQueryBusBase.d.mts.map +1 -1
  5. package/dist/browser/AsyncQueryBus/AsyncQueryBusBase.d.ts +7 -72
  6. package/dist/browser/AsyncQueryBus/AsyncQueryBusBase.d.ts.map +1 -1
  7. package/dist/browser/AsyncQueryBus/AsyncQueryBusHost.d.cts.map +1 -1
  8. package/dist/browser/AsyncQueryBus/AsyncQueryBusHost.d.mts.map +1 -1
  9. package/dist/browser/AsyncQueryBus/AsyncQueryBusHost.d.ts.map +1 -1
  10. package/dist/browser/index.cjs +64 -118
  11. package/dist/browser/index.cjs.map +1 -1
  12. package/dist/browser/index.js +60 -114
  13. package/dist/browser/index.js.map +1 -1
  14. package/dist/node/AsyncQueryBus/AsyncQueryBusBase.d.cts +7 -72
  15. package/dist/node/AsyncQueryBus/AsyncQueryBusBase.d.cts.map +1 -1
  16. package/dist/node/AsyncQueryBus/AsyncQueryBusBase.d.mts +7 -72
  17. package/dist/node/AsyncQueryBus/AsyncQueryBusBase.d.mts.map +1 -1
  18. package/dist/node/AsyncQueryBus/AsyncQueryBusBase.d.ts +7 -72
  19. package/dist/node/AsyncQueryBus/AsyncQueryBusBase.d.ts.map +1 -1
  20. package/dist/node/AsyncQueryBus/AsyncQueryBusHost.d.cts.map +1 -1
  21. package/dist/node/AsyncQueryBus/AsyncQueryBusHost.d.mts.map +1 -1
  22. package/dist/node/AsyncQueryBus/AsyncQueryBusHost.d.ts.map +1 -1
  23. package/dist/node/index.cjs +69 -122
  24. package/dist/node/index.cjs.map +1 -1
  25. package/dist/node/index.js +65 -118
  26. package/dist/node/index.js.map +1 -1
  27. package/package.json +18 -18
  28. package/src/AsyncQueryBus/AsyncQueryBusBase.ts +58 -101
  29. package/src/AsyncQueryBus/AsyncQueryBusHost.ts +8 -5
@@ -10,7 +10,7 @@ var AbstractModuleHost = class extends Base {
10
10
  };
11
11
 
12
12
  // src/AsyncQueryBus/AsyncQueryBusClient.ts
13
- import { assertEx } from "@xylabs/assert";
13
+ import { assertEx as assertEx2 } from "@xylabs/assert";
14
14
  import { delay } from "@xylabs/delay";
15
15
  import { forget } from "@xylabs/forget";
16
16
  import { clearTimeoutEx, setTimeoutEx } from "@xylabs/timer";
@@ -20,9 +20,11 @@ import { PayloadBuilder } from "@xyo-network/payload-builder";
20
20
  import { LRUCache as LRUCache2 } from "lru-cache";
21
21
 
22
22
  // src/AsyncQueryBus/AsyncQueryBusBase.ts
23
- import { Base as Base2, toJsonString } from "@xylabs/object";
24
- import { asArchivistInstance } from "@xyo-network/archivist-model";
25
- import { asDivinerInstance } from "@xyo-network/diviner-model";
23
+ import { assertEx } from "@xylabs/assert";
24
+ import { Base as Base2 } from "@xylabs/object";
25
+ import { isArchivistInstance } from "@xyo-network/archivist-model";
26
+ import { isDivinerInstance } from "@xyo-network/diviner-model";
27
+ import { resolveModuleIdentifier, traceModuleIdentifier } from "@xyo-network/module-model";
26
28
  import { Mutex } from "async-mutex";
27
29
  import { LRUCache } from "lru-cache";
28
30
  var AsyncQueryBusBase = class extends Base2 {
@@ -32,19 +34,13 @@ var AsyncQueryBusBase = class extends Base2 {
32
34
  _lastState;
33
35
  _targetConfigs = {};
34
36
  _targetQueries = {};
35
- _lastQueriesArchivistAttempt;
36
- _lastQueriesDivinerAttempt;
37
- _lastResponsesArchivistAttempt;
38
- _lastResponsesDivinerAttempt;
37
+ _lastResolveAttempt = {};
39
38
  _queriesArchivist;
40
- _queriesArchivistMutex = new Mutex();
41
39
  _queriesDiviner;
42
- _queriesDivinerMutex = new Mutex();
43
- _reResolveDelay = 5e4;
40
+ _reResolveDelay = 1e3 * 5;
41
+ _resolveMutex = new Mutex();
44
42
  _responsesArchivist;
45
- _responsesArchivistMutex = new Mutex();
46
43
  _responsesDiviner;
47
- _responsesDivinerMutex = new Mutex();
48
44
  constructor(params) {
49
45
  super(params);
50
46
  }
@@ -69,92 +65,20 @@ var AsyncQueryBusBase = class extends Base2 {
69
65
  return this._lastState;
70
66
  }
71
67
  async queriesArchivist() {
72
- return await this._queriesArchivistMutex.runExclusive(async () => {
73
- if (this._queriesArchivist) {
74
- return this._queriesArchivist;
75
- }
76
- if (Date.now() - (this._lastQueriesArchivistAttempt ?? 0) < this._reResolveDelay) {
77
- return;
78
- }
79
- this._lastQueriesArchivistAttempt = Date.now();
80
- const resolved = await this.resolver.resolve(this.config?.intersect?.queries?.archivist);
81
- if (resolved) {
82
- const typedResolve = asArchivistInstance(resolved);
83
- if (typedResolve) {
84
- return typedResolve;
85
- } else {
86
- this.logger?.warn(`Unable to resolve queriesArchivist as correct type [${this.config?.intersect?.queries?.archivist}][${resolved?.constructor?.name}]: ${toJsonString(resolved)}`);
87
- }
88
- } else {
89
- this.logger?.log(`Unable to resolve queriesArchivist [${this.config?.intersect?.queries?.archivist}]`);
90
- }
91
- });
68
+ this._queriesArchivist = this._queriesArchivist ?? await this.resolve(assertEx(this.config?.intersect?.queries?.archivist, () => "No queries Archivist defined"), isArchivistInstance);
69
+ return this._queriesArchivist;
92
70
  }
93
71
  async queriesDiviner() {
94
- return await this._queriesDivinerMutex.runExclusive(async () => {
95
- if (this._queriesDiviner) {
96
- return this._queriesDiviner;
97
- }
98
- if (Date.now() - (this._lastQueriesDivinerAttempt ?? 0) < this._reResolveDelay) {
99
- return;
100
- }
101
- this._lastQueriesDivinerAttempt = Date.now();
102
- const resolved = await this.resolver.resolve(this.config?.intersect?.queries?.boundWitnessDiviner);
103
- if (resolved) {
104
- const typedResolve = asDivinerInstance(resolved);
105
- if (typedResolve) {
106
- return typedResolve;
107
- } else {
108
- this.logger?.warn(`Unable to resolve queriesDiviner as correct type [${this.config?.intersect?.queries?.boundWitnessDiviner}][${resolved?.constructor?.name}]: ${toJsonString(resolved)}`);
109
- }
110
- } else {
111
- this.logger?.log(`Unable to resolve queriesDiviner [${this.config?.intersect?.queries?.boundWitnessDiviner}]`);
112
- }
113
- });
72
+ this._queriesDiviner = this._queriesDiviner ?? await this.resolve(assertEx(this.config?.intersect?.queries?.boundWitnessDiviner, () => "No queries Diviner defined"), isDivinerInstance);
73
+ return this._queriesDiviner;
114
74
  }
115
75
  async responsesArchivist() {
116
- return await this._responsesArchivistMutex.runExclusive(async () => {
117
- if (this._responsesArchivist) {
118
- return this._responsesArchivist;
119
- }
120
- if (Date.now() - (this._lastResponsesArchivistAttempt ?? 0) < this._reResolveDelay) {
121
- return;
122
- }
123
- this._lastResponsesArchivistAttempt = Date.now();
124
- const resolved = await this.resolver.resolve(this.config?.intersect?.responses?.archivist);
125
- if (resolved) {
126
- const typedResolve = asArchivistInstance(resolved);
127
- if (typedResolve) {
128
- return typedResolve;
129
- } else {
130
- this.logger?.warn(`Unable to resolve responseArchivist as correct type [${this.config?.intersect?.responses?.archivist}][${resolved?.constructor?.name}]: ${toJsonString(resolved)}`);
131
- }
132
- } else {
133
- this.logger?.log(`Unable to resolve responseArchivist [${this.config?.intersect?.responses?.archivist}]`);
134
- }
135
- });
76
+ this._responsesArchivist = this._responsesArchivist ?? await this.resolve(assertEx(this.config?.intersect?.responses?.archivist, () => "No responses Archivist defined"), isArchivistInstance);
77
+ return this._responsesArchivist;
136
78
  }
137
79
  async responsesDiviner() {
138
- return await this._responsesDivinerMutex.runExclusive(async () => {
139
- if (this._responsesDiviner) {
140
- return this._responsesDiviner;
141
- }
142
- if (Date.now() - (this._lastResponsesDivinerAttempt ?? 0) < this._reResolveDelay) {
143
- return;
144
- }
145
- this._lastResponsesDivinerAttempt = Date.now();
146
- const resolved = await this.resolver.resolve(this.config?.intersect?.responses?.boundWitnessDiviner);
147
- if (resolved) {
148
- const typedResolve = asDivinerInstance(resolved);
149
- if (typedResolve) {
150
- return typedResolve;
151
- } else {
152
- this.logger?.warn(`Unable to resolve responsesDiviner as correct type [${this.config?.intersect?.responses?.boundWitnessDiviner}][${resolved?.constructor?.name}]: ${toJsonString(resolved)}`);
153
- }
154
- } else {
155
- this.logger?.log(`Unable to resolve responsesDiviner [${this.config?.intersect?.responses?.boundWitnessDiviner}]`);
156
- }
157
- });
80
+ this._responsesDiviner = this._responsesDiviner ?? await this.resolve(assertEx(this.config?.intersect?.responses?.boundWitnessDiviner, () => "No responses Diviner defined"), isDivinerInstance);
81
+ return this._responsesDiviner;
158
82
  }
159
83
  /**
160
84
  * Commit the internal state of the process. This is similar
@@ -186,6 +110,27 @@ var AsyncQueryBusBase = class extends Base2 {
186
110
  return state;
187
111
  }
188
112
  }
113
+ async resolve(id, typeCheck) {
114
+ return await this._resolveMutex.runExclusive(async () => {
115
+ if (this._queriesArchivist) {
116
+ return this._queriesArchivist;
117
+ }
118
+ if (Date.now() - (this._lastResolveAttempt[id] ?? 0) < this._reResolveDelay) {
119
+ return;
120
+ }
121
+ this._lastResolveAttempt[id] = Date.now();
122
+ const resolved = await resolveModuleIdentifier(this.resolver, id);
123
+ if (resolved) {
124
+ if (typeCheck(resolved)) {
125
+ return resolved;
126
+ } else {
127
+ this.logger?.warn(`Unable to resolve responsesDiviner as correct type [${id}][${resolved?.constructor?.name}]: ${resolved.id}`);
128
+ }
129
+ } else {
130
+ this.logger?.debug(`Unable to resolve queriesArchivist [${id}] [${await traceModuleIdentifier(this.resolver, id)}]`);
131
+ }
132
+ });
133
+ }
189
134
  };
190
135
 
191
136
  // src/AsyncQueryBus/model/QueryStatus.ts
@@ -243,7 +188,7 @@ var AsyncQueryBusClient = class extends AsyncQueryBusBase {
243
188
  ...query,
244
189
  $meta
245
190
  });
246
- const queryArchivist = assertEx(await this.queriesArchivist(), () => `Unable to contact queriesArchivist [${this.config?.intersect?.queries?.archivist}]`);
191
+ const queryArchivist = assertEx2(await this.queriesArchivist(), () => `Unable to contact queriesArchivist [${this.config?.intersect?.queries?.archivist}]`);
247
192
  const routedQueryHash = (
248
193
  // Trust the signed hash if it's there
249
194
  routedQuery?.$hash ?? // TODO: What is the right way to find the dataHash
@@ -371,11 +316,11 @@ var AsyncQueryBusClient = class extends AsyncQueryBusBase {
371
316
 
372
317
  // src/AsyncQueryBus/AsyncQueryBusHost.ts
373
318
  import { containsAll } from "@xylabs/array";
374
- import { assertEx as assertEx2 } from "@xylabs/assert";
319
+ import { assertEx as assertEx3 } from "@xylabs/assert";
375
320
  import { clearTimeoutEx as clearTimeoutEx2, setTimeoutEx as setTimeoutEx2 } from "@xylabs/timer";
376
321
  import { isQueryBoundWitnessWithMeta } from "@xyo-network/boundwitness-model";
377
322
  import { BoundWitnessDivinerQuerySchema as BoundWitnessDivinerQuerySchema2 } from "@xyo-network/diviner-boundwitness-model";
378
- import { asModuleInstance, ModuleConfigSchema } from "@xyo-network/module-model";
323
+ import { asModuleInstance, ModuleConfigSchema, traceModuleIdentifier as traceModuleIdentifier2 } from "@xyo-network/module-model";
379
324
  import { PayloadBuilder as PayloadBuilder2 } from "@xyo-network/payload-builder";
380
325
  var AsyncQueryBusHost = class extends AsyncQueryBusBase {
381
326
  static {
@@ -405,7 +350,7 @@ var AsyncQueryBusHost = class extends AsyncQueryBusBase {
405
350
  throw new Error(`Unable to resolve module to expose [${id}]`);
406
351
  }
407
352
  if (module) {
408
- assertEx2(!failOnAlreadyExposed || !this._exposedAddresses.has(module.address), () => `Address already exposed: ${id} [${module.address}]`);
353
+ assertEx3(!failOnAlreadyExposed || !this._exposedAddresses.has(module.address), () => `Address already exposed: ${id} [${module.address}]`);
409
354
  this._exposedAddresses.add(module.address);
410
355
  this._exposeOptions[module.address] = {
411
356
  ...options
@@ -419,7 +364,7 @@ var AsyncQueryBusHost = class extends AsyncQueryBusBase {
419
364
  ...this.config?.listeningModules ?? [],
420
365
  ...this.exposedAddresses.values()
421
366
  ];
422
- const mods = await Promise.all(exposedModules.map(async (exposedModule) => assertEx2(asModuleInstance(await this.resolver.resolve(exposedModule, {
367
+ const mods = await Promise.all(exposedModules.map(async (exposedModule) => assertEx3(asModuleInstance(await this.resolver.resolve(exposedModule, {
423
368
  maxDepth: 10
424
369
  })), () => `Unable to resolve listeningModule [${exposedModule}]`)));
425
370
  return mods;
@@ -443,7 +388,7 @@ var AsyncQueryBusHost = class extends AsyncQueryBusBase {
443
388
  maxDepth: 10
444
389
  }));
445
390
  if (module) {
446
- assertEx2(!validate || this._exposedAddresses.has(module.address), () => `Address not exposed [${module.address}][${module.id}]`);
391
+ assertEx3(!validate || this._exposedAddresses.has(module.address), () => `Address not exposed [${module.address}][${module.id}]`);
447
392
  this._exposedAddresses.delete(module.address);
448
393
  delete this._exposeOptions[module.address];
449
394
  this.logger?.debug(`${module.address} [${module.id}] unexposed`);
@@ -452,8 +397,8 @@ var AsyncQueryBusHost = class extends AsyncQueryBusBase {
452
397
  }
453
398
  callLocalModule = async (localModule, query) => {
454
399
  const localModuleName = localModule.config.name ?? localModule.address;
455
- const queryArchivist = assertEx2(await this.queriesArchivist(), () => `Unable to contact queriesArchivist [${this.config?.intersect?.queries?.archivist}]`);
456
- const responsesArchivist = assertEx2(await this.responsesArchivist(), () => `Unable to contact responsesArchivist [${this.config?.intersect?.queries?.archivist}]`);
400
+ const queryArchivist = assertEx3(await this.queriesArchivist(), () => `Unable to contact queriesArchivist [${this.config?.intersect?.queries?.archivist}]`);
401
+ const responsesArchivist = assertEx3(await this.responsesArchivist(), () => `Unable to contact responsesArchivist [${this.config?.intersect?.queries?.archivist}]`);
457
402
  const queryDestination = query.$meta?.destination;
458
403
  if (queryDestination && queryDestination?.includes(localModule.address)) {
459
404
  const queryIndex = query.payload_hashes.indexOf(query.query);
@@ -500,8 +445,9 @@ var AsyncQueryBusHost = class extends AsyncQueryBusBase {
500
445
  * @param address The address to find commands for
501
446
  */
502
447
  findQueriesToAddress = async (address) => {
503
- const queryBoundWitnessDiviner = await this.queriesDiviner();
504
- if (queryBoundWitnessDiviner) {
448
+ const queriesDivinerId = assertEx3(this.config?.intersect?.queries?.boundWitnessDiviner, () => "No queries Diviner defined");
449
+ const queriesBoundWitnessDiviner = await this.queriesDiviner();
450
+ if (queriesBoundWitnessDiviner) {
505
451
  const timestamp = await this.retrieveState(address);
506
452
  const destination = [
507
453
  address
@@ -514,7 +460,7 @@ var AsyncQueryBusHost = class extends AsyncQueryBusBase {
514
460
  sort: "asc",
515
461
  timestamp
516
462
  };
517
- const result = await queryBoundWitnessDiviner.divine([
463
+ const result = await queriesBoundWitnessDiviner.divine([
518
464
  divinerQuery
519
465
  ]);
520
466
  const queries = result.filter(isQueryBoundWitnessWithMeta);
@@ -522,7 +468,7 @@ var AsyncQueryBusHost = class extends AsyncQueryBusBase {
522
468
  await this.commitState(address, nextState);
523
469
  return queries;
524
470
  } else {
525
- this.logger?.warn(`Unable to resolve queryBoundWitnessDiviner [${this.config?.intersect?.queries?.boundWitnessDiviner}]`);
471
+ this.logger?.warn(`Unable to resolve queriesBoundWitnessDiviner [${queriesDivinerId}] [${await traceModuleIdentifier2(this.resolver, queriesDivinerId)}]`);
526
472
  }
527
473
  };
528
474
  /**
@@ -616,13 +562,13 @@ var PubSubBridgeSchema = "network.xyo.bridge.pubsub";
616
562
  var PubSubBridgeConfigSchema = `${PubSubBridgeSchema}.config`;
617
563
 
618
564
  // src/PubSubBridge.ts
619
- import { assertEx as assertEx4 } from "@xylabs/assert";
565
+ import { assertEx as assertEx5 } from "@xylabs/assert";
620
566
  import { exists } from "@xylabs/exists";
621
567
  import { AbstractBridge } from "@xyo-network/abstract-bridge";
622
568
  import { creatableModule } from "@xyo-network/module-model";
623
569
 
624
570
  // src/PubSubBridgeModuleResolver.ts
625
- import { assertEx as assertEx3 } from "@xylabs/assert";
571
+ import { assertEx as assertEx4 } from "@xylabs/assert";
626
572
  import { isAddress } from "@xylabs/hex";
627
573
  import { AbstractBridgeModuleResolver, wrapModuleWithType } from "@xyo-network/abstract-bridge";
628
574
  import { Account } from "@xyo-network/account";
@@ -639,7 +585,7 @@ var PubSubBridgeModuleResolver = class extends AbstractBridgeModuleResolver {
639
585
  }
640
586
  const idParts = id.split(":");
641
587
  const firstPart = idParts.shift();
642
- assertEx3(isAddress(firstPart), () => `Invalid module address: ${firstPart}`);
588
+ assertEx4(isAddress(firstPart), () => `Invalid module address: ${firstPart}`);
643
589
  const remainderParts = idParts.join(":");
644
590
  const account = Account.randomSync();
645
591
  const params = {
@@ -656,13 +602,13 @@ var PubSubBridgeModuleResolver = class extends AbstractBridgeModuleResolver {
656
602
  const state = await proxy.state();
657
603
  if (state) {
658
604
  const configSchema = state.find((payload) => payload.schema === ConfigSchema)?.config;
659
- const config = assertEx3(state.find((payload) => payload.schema === configSchema), () => "Unable to locate config");
605
+ const config = assertEx4(state.find((payload) => payload.schema === configSchema), () => "Unable to locate config");
660
606
  proxy.setConfig(config);
661
607
  }
662
608
  }
663
609
  await proxy.start?.();
664
610
  const wrapped = wrapModuleWithType(proxy, account);
665
- const as = assertEx3(asModuleInstance2(wrapped, {}), () => `Failed to asModuleInstance [${id}]`);
611
+ const as = assertEx4(asModuleInstance2(wrapped, {}), () => `Failed to asModuleInstance [${id}]`);
666
612
  proxy.upResolver.add(as);
667
613
  proxy.downResolver.add(as);
668
614
  this.add(as);
@@ -701,7 +647,7 @@ var PubSubBridge = class extends AbstractBridge {
701
647
  get resolver() {
702
648
  this._resolver = this._resolver ?? new PubSubBridgeModuleResolver({
703
649
  bridge: this,
704
- busClient: assertEx4(this.busClient(), () => "busClient not configured"),
650
+ busClient: assertEx5(this.busClient(), () => "busClient not configured"),
705
651
  wrapperAccount: this.account
706
652
  });
707
653
  return this._resolver;
@@ -710,7 +656,7 @@ var PubSubBridge = class extends AbstractBridge {
710
656
  return `${this.config.name ?? moduleName}`;
711
657
  }
712
658
  get roots() {
713
- return assertEx4(this.config.roots, () => "roots not configured");
659
+ return assertEx5(this.config.roots, () => "roots not configured");
714
660
  }
715
661
  async discoverRoots() {
716
662
  const rootInstances = (await Promise.all(this.roots.map(async (root) => await this.resolver.resolve(root)))).filter(exists);
@@ -721,7 +667,7 @@ var PubSubBridge = class extends AbstractBridge {
721
667
  }
722
668
  async exposeHandler(id, options) {
723
669
  const { maxDepth = 2, direction = "all", required = true } = options ?? {};
724
- const host = assertEx4(this.busHost(), () => "Not configured as a host");
670
+ const host = assertEx5(this.busHost(), () => "Not configured as a host");
725
671
  const module = await host.expose(id, {
726
672
  required
727
673
  });
@@ -754,7 +700,7 @@ var PubSubBridge = class extends AbstractBridge {
754
700
  }
755
701
  async unexposeHandler(id, options) {
756
702
  const { maxDepth = 2, direction = "all", required = true } = options ?? {};
757
- const host = assertEx4(this.busHost(), () => "Not configured as a host");
703
+ const host = assertEx5(this.busHost(), () => "Not configured as a host");
758
704
  const module = await host.unexpose(id, required);
759
705
  if (module) {
760
706
  const children = maxDepth > 0 ? await module.resolve("*", {