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

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 (42) hide show
  1. package/dist/browser/AsyncQueryBus/AsyncQueryBusBase.d.cts +4 -0
  2. package/dist/browser/AsyncQueryBus/AsyncQueryBusBase.d.cts.map +1 -1
  3. package/dist/browser/AsyncQueryBus/AsyncQueryBusBase.d.mts +4 -0
  4. package/dist/browser/AsyncQueryBus/AsyncQueryBusBase.d.mts.map +1 -1
  5. package/dist/browser/AsyncQueryBus/AsyncQueryBusBase.d.ts +4 -0
  6. package/dist/browser/AsyncQueryBus/AsyncQueryBusBase.d.ts.map +1 -1
  7. package/dist/browser/AsyncQueryBus/AsyncQueryBusHost.d.cts +24 -3
  8. package/dist/browser/AsyncQueryBus/AsyncQueryBusHost.d.cts.map +1 -1
  9. package/dist/browser/AsyncQueryBus/AsyncQueryBusHost.d.mts +24 -3
  10. package/dist/browser/AsyncQueryBus/AsyncQueryBusHost.d.mts.map +1 -1
  11. package/dist/browser/AsyncQueryBus/AsyncQueryBusHost.d.ts +24 -3
  12. package/dist/browser/AsyncQueryBus/AsyncQueryBusHost.d.ts.map +1 -1
  13. package/dist/browser/PubSubBridge.d.cts.map +1 -1
  14. package/dist/browser/PubSubBridge.d.mts.map +1 -1
  15. package/dist/browser/PubSubBridge.d.ts.map +1 -1
  16. package/dist/browser/index.cjs +130 -101
  17. package/dist/browser/index.cjs.map +1 -1
  18. package/dist/browser/index.js +130 -101
  19. package/dist/browser/index.js.map +1 -1
  20. package/dist/node/AsyncQueryBus/AsyncQueryBusBase.d.cts +4 -0
  21. package/dist/node/AsyncQueryBus/AsyncQueryBusBase.d.cts.map +1 -1
  22. package/dist/node/AsyncQueryBus/AsyncQueryBusBase.d.mts +4 -0
  23. package/dist/node/AsyncQueryBus/AsyncQueryBusBase.d.mts.map +1 -1
  24. package/dist/node/AsyncQueryBus/AsyncQueryBusBase.d.ts +4 -0
  25. package/dist/node/AsyncQueryBus/AsyncQueryBusBase.d.ts.map +1 -1
  26. package/dist/node/AsyncQueryBus/AsyncQueryBusHost.d.cts +24 -3
  27. package/dist/node/AsyncQueryBus/AsyncQueryBusHost.d.cts.map +1 -1
  28. package/dist/node/AsyncQueryBus/AsyncQueryBusHost.d.mts +24 -3
  29. package/dist/node/AsyncQueryBus/AsyncQueryBusHost.d.mts.map +1 -1
  30. package/dist/node/AsyncQueryBus/AsyncQueryBusHost.d.ts +24 -3
  31. package/dist/node/AsyncQueryBus/AsyncQueryBusHost.d.ts.map +1 -1
  32. package/dist/node/PubSubBridge.d.cts.map +1 -1
  33. package/dist/node/PubSubBridge.d.mts.map +1 -1
  34. package/dist/node/PubSubBridge.d.ts.map +1 -1
  35. package/dist/node/index.cjs +134 -105
  36. package/dist/node/index.cjs.map +1 -1
  37. package/dist/node/index.js +134 -105
  38. package/dist/node/index.js.map +1 -1
  39. package/package.json +19 -18
  40. package/src/AsyncQueryBus/AsyncQueryBusBase.ts +85 -72
  41. package/src/AsyncQueryBus/AsyncQueryBusHost.ts +29 -14
  42. package/src/PubSubBridge.ts +21 -20
@@ -27,6 +27,7 @@ import { LRUCache as LRUCache2 } from "lru-cache";
27
27
  import { Base as Base2, toJsonString } from "@xylabs/object";
28
28
  import { asArchivistInstance } from "@xyo-network/archivist-model";
29
29
  import { asDivinerInstance } from "@xyo-network/diviner-model";
30
+ import { Mutex } from "async-mutex";
30
31
  import { LRUCache } from "lru-cache";
31
32
  var _AsyncQueryBusBase = class _AsyncQueryBusBase extends Base2 {
32
33
  _lastState;
@@ -37,10 +38,14 @@ var _AsyncQueryBusBase = class _AsyncQueryBusBase extends Base2 {
37
38
  _lastResponsesArchivistAttempt;
38
39
  _lastResponsesDivinerAttempt;
39
40
  _queriesArchivist;
41
+ _queriesArchivistMutex = new Mutex();
40
42
  _queriesDiviner;
43
+ _queriesDivinerMutex = new Mutex();
41
44
  _reResolveDelay = 5e4;
42
45
  _responsesArchivist;
46
+ _responsesArchivistMutex = new Mutex();
43
47
  _responsesDiviner;
48
+ _responsesDivinerMutex = new Mutex();
44
49
  constructor(params) {
45
50
  super(params);
46
51
  }
@@ -66,88 +71,96 @@ var _AsyncQueryBusBase = class _AsyncQueryBusBase extends Base2 {
66
71
  return this._lastState;
67
72
  }
68
73
  async queriesArchivist() {
69
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
70
- if (this._queriesArchivist) {
71
- return this._queriesArchivist;
72
- }
73
- if (Date.now() - (this._lastQueriesArchivistAttempt ?? 0) < this._reResolveDelay) {
74
- return;
75
- }
76
- this._lastQueriesArchivistAttempt = Date.now();
77
- const resolved = await this.resolver.resolve((_c = (_b = (_a = this.config) == null ? void 0 : _a.intersect) == null ? void 0 : _b.queries) == null ? void 0 : _c.archivist);
78
- if (resolved) {
79
- const typedResolve = asArchivistInstance(resolved);
80
- if (typedResolve) {
81
- return typedResolve;
74
+ return await this._queriesArchivistMutex.runExclusive(async () => {
75
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
76
+ if (this._queriesArchivist) {
77
+ return this._queriesArchivist;
78
+ }
79
+ if (Date.now() - (this._lastQueriesArchivistAttempt ?? 0) < this._reResolveDelay) {
80
+ return;
81
+ }
82
+ this._lastQueriesArchivistAttempt = Date.now();
83
+ const resolved = await this.resolver.resolve((_c = (_b = (_a = this.config) == null ? void 0 : _a.intersect) == null ? void 0 : _b.queries) == null ? void 0 : _c.archivist);
84
+ if (resolved) {
85
+ const typedResolve = asArchivistInstance(resolved);
86
+ if (typedResolve) {
87
+ return typedResolve;
88
+ } else {
89
+ (_h = this.logger) == null ? void 0 : _h.warn(`Unable to resolve queriesArchivist as correct type [${(_f = (_e = (_d = this.config) == null ? void 0 : _d.intersect) == null ? void 0 : _e.queries) == null ? void 0 : _f.archivist}][${(_g = resolved == null ? void 0 : resolved.constructor) == null ? void 0 : _g.name}]: ${toJsonString(resolved)}`);
90
+ }
82
91
  } else {
83
- (_h = this.logger) == null ? void 0 : _h.warn(`Unable to resolve queriesArchivist as correct type [${(_f = (_e = (_d = this.config) == null ? void 0 : _d.intersect) == null ? void 0 : _e.queries) == null ? void 0 : _f.archivist}][${(_g = resolved == null ? void 0 : resolved.constructor) == null ? void 0 : _g.name}]: ${toJsonString(resolved)}`);
92
+ (_l = this.logger) == null ? void 0 : _l.log(`Unable to resolve queriesArchivist [${(_k = (_j = (_i = this.config) == null ? void 0 : _i.intersect) == null ? void 0 : _j.queries) == null ? void 0 : _k.archivist}]`);
84
93
  }
85
- } else {
86
- (_l = this.logger) == null ? void 0 : _l.log(`Unable to resolve queriesArchivist [${(_k = (_j = (_i = this.config) == null ? void 0 : _i.intersect) == null ? void 0 : _j.queries) == null ? void 0 : _k.archivist}]`);
87
- }
94
+ });
88
95
  }
89
96
  async queriesDiviner() {
90
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
91
- if (this._queriesDiviner) {
92
- return this._queriesDiviner;
93
- }
94
- if (Date.now() - (this._lastQueriesDivinerAttempt ?? 0) < this._reResolveDelay) {
95
- return;
96
- }
97
- this._lastQueriesDivinerAttempt = Date.now();
98
- const resolved = await this.resolver.resolve((_c = (_b = (_a = this.config) == null ? void 0 : _a.intersect) == null ? void 0 : _b.queries) == null ? void 0 : _c.boundWitnessDiviner);
99
- if (resolved) {
100
- const typedResolve = asDivinerInstance(resolved);
101
- if (typedResolve) {
102
- return typedResolve;
97
+ return await this._queriesDivinerMutex.runExclusive(async () => {
98
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
99
+ if (this._queriesDiviner) {
100
+ return this._queriesDiviner;
101
+ }
102
+ if (Date.now() - (this._lastQueriesDivinerAttempt ?? 0) < this._reResolveDelay) {
103
+ return;
104
+ }
105
+ this._lastQueriesDivinerAttempt = Date.now();
106
+ const resolved = await this.resolver.resolve((_c = (_b = (_a = this.config) == null ? void 0 : _a.intersect) == null ? void 0 : _b.queries) == null ? void 0 : _c.boundWitnessDiviner);
107
+ if (resolved) {
108
+ const typedResolve = asDivinerInstance(resolved);
109
+ if (typedResolve) {
110
+ return typedResolve;
111
+ } else {
112
+ (_h = this.logger) == null ? void 0 : _h.warn(`Unable to resolve queriesDiviner as correct type [${(_f = (_e = (_d = this.config) == null ? void 0 : _d.intersect) == null ? void 0 : _e.queries) == null ? void 0 : _f.boundWitnessDiviner}][${(_g = resolved == null ? void 0 : resolved.constructor) == null ? void 0 : _g.name}]: ${toJsonString(resolved)}`);
113
+ }
103
114
  } else {
104
- (_h = this.logger) == null ? void 0 : _h.warn(`Unable to resolve queriesDiviner as correct type [${(_f = (_e = (_d = this.config) == null ? void 0 : _d.intersect) == null ? void 0 : _e.queries) == null ? void 0 : _f.boundWitnessDiviner}][${(_g = resolved == null ? void 0 : resolved.constructor) == null ? void 0 : _g.name}]: ${toJsonString(resolved)}`);
115
+ (_l = this.logger) == null ? void 0 : _l.log(`Unable to resolve queriesDiviner [${(_k = (_j = (_i = this.config) == null ? void 0 : _i.intersect) == null ? void 0 : _j.queries) == null ? void 0 : _k.boundWitnessDiviner}]`);
105
116
  }
106
- } else {
107
- (_l = this.logger) == null ? void 0 : _l.log(`Unable to resolve queriesDiviner [${(_k = (_j = (_i = this.config) == null ? void 0 : _i.intersect) == null ? void 0 : _j.queries) == null ? void 0 : _k.boundWitnessDiviner}]`);
108
- }
117
+ });
109
118
  }
110
119
  async responsesArchivist() {
111
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
112
- if (this._responsesArchivist) {
113
- return this._responsesArchivist;
114
- }
115
- if (Date.now() - (this._lastResponsesArchivistAttempt ?? 0) < this._reResolveDelay) {
116
- return;
117
- }
118
- this._lastResponsesArchivistAttempt = Date.now();
119
- const resolved = await this.resolver.resolve((_c = (_b = (_a = this.config) == null ? void 0 : _a.intersect) == null ? void 0 : _b.responses) == null ? void 0 : _c.archivist);
120
- if (resolved) {
121
- const typedResolve = asArchivistInstance(resolved);
122
- if (typedResolve) {
123
- return typedResolve;
120
+ return await this._responsesArchivistMutex.runExclusive(async () => {
121
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
122
+ if (this._responsesArchivist) {
123
+ return this._responsesArchivist;
124
+ }
125
+ if (Date.now() - (this._lastResponsesArchivistAttempt ?? 0) < this._reResolveDelay) {
126
+ return;
127
+ }
128
+ this._lastResponsesArchivistAttempt = Date.now();
129
+ const resolved = await this.resolver.resolve((_c = (_b = (_a = this.config) == null ? void 0 : _a.intersect) == null ? void 0 : _b.responses) == null ? void 0 : _c.archivist);
130
+ if (resolved) {
131
+ const typedResolve = asArchivistInstance(resolved);
132
+ if (typedResolve) {
133
+ return typedResolve;
134
+ } else {
135
+ (_h = this.logger) == null ? void 0 : _h.warn(`Unable to resolve responseArchivist as correct type [${(_f = (_e = (_d = this.config) == null ? void 0 : _d.intersect) == null ? void 0 : _e.responses) == null ? void 0 : _f.archivist}][${(_g = resolved == null ? void 0 : resolved.constructor) == null ? void 0 : _g.name}]: ${toJsonString(resolved)}`);
136
+ }
124
137
  } else {
125
- (_h = this.logger) == null ? void 0 : _h.warn(`Unable to resolve responseArchivist as correct type [${(_f = (_e = (_d = this.config) == null ? void 0 : _d.intersect) == null ? void 0 : _e.responses) == null ? void 0 : _f.archivist}][${(_g = resolved == null ? void 0 : resolved.constructor) == null ? void 0 : _g.name}]: ${toJsonString(resolved)}`);
138
+ (_l = this.logger) == null ? void 0 : _l.log(`Unable to resolve responseArchivist [${(_k = (_j = (_i = this.config) == null ? void 0 : _i.intersect) == null ? void 0 : _j.responses) == null ? void 0 : _k.archivist}]`);
126
139
  }
127
- } else {
128
- (_l = this.logger) == null ? void 0 : _l.log(`Unable to resolve responseArchivist [${(_k = (_j = (_i = this.config) == null ? void 0 : _i.intersect) == null ? void 0 : _j.responses) == null ? void 0 : _k.archivist}]`);
129
- }
140
+ });
130
141
  }
131
142
  async responsesDiviner() {
132
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
133
- if (this._responsesDiviner) {
134
- return this._responsesDiviner;
135
- }
136
- if (Date.now() - (this._lastResponsesDivinerAttempt ?? 0) < this._reResolveDelay) {
137
- return;
138
- }
139
- this._lastResponsesDivinerAttempt = Date.now();
140
- const resolved = await this.resolver.resolve((_c = (_b = (_a = this.config) == null ? void 0 : _a.intersect) == null ? void 0 : _b.responses) == null ? void 0 : _c.boundWitnessDiviner);
141
- if (resolved) {
142
- const typedResolve = asDivinerInstance(resolved);
143
- if (typedResolve) {
144
- return typedResolve;
143
+ return await this._responsesDivinerMutex.runExclusive(async () => {
144
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
145
+ if (this._responsesDiviner) {
146
+ return this._responsesDiviner;
147
+ }
148
+ if (Date.now() - (this._lastResponsesDivinerAttempt ?? 0) < this._reResolveDelay) {
149
+ return;
150
+ }
151
+ this._lastResponsesDivinerAttempt = Date.now();
152
+ const resolved = await this.resolver.resolve((_c = (_b = (_a = this.config) == null ? void 0 : _a.intersect) == null ? void 0 : _b.responses) == null ? void 0 : _c.boundWitnessDiviner);
153
+ if (resolved) {
154
+ const typedResolve = asDivinerInstance(resolved);
155
+ if (typedResolve) {
156
+ return typedResolve;
157
+ } else {
158
+ (_h = this.logger) == null ? void 0 : _h.warn(`Unable to resolve responsesDiviner as correct type [${(_f = (_e = (_d = this.config) == null ? void 0 : _d.intersect) == null ? void 0 : _e.responses) == null ? void 0 : _f.boundWitnessDiviner}][${(_g = resolved == null ? void 0 : resolved.constructor) == null ? void 0 : _g.name}]: ${toJsonString(resolved)}`);
159
+ }
145
160
  } else {
146
- (_h = this.logger) == null ? void 0 : _h.warn(`Unable to resolve responsesDiviner as correct type [${(_f = (_e = (_d = this.config) == null ? void 0 : _d.intersect) == null ? void 0 : _e.responses) == null ? void 0 : _f.boundWitnessDiviner}][${(_g = resolved == null ? void 0 : resolved.constructor) == null ? void 0 : _g.name}]: ${toJsonString(resolved)}`);
161
+ (_l = this.logger) == null ? void 0 : _l.log(`Unable to resolve responsesDiviner [${(_k = (_j = (_i = this.config) == null ? void 0 : _i.intersect) == null ? void 0 : _j.responses) == null ? void 0 : _k.boundWitnessDiviner}]`);
147
162
  }
148
- } else {
149
- (_l = this.logger) == null ? void 0 : _l.log(`Unable to resolve responsesDiviner [${(_k = (_j = (_i = this.config) == null ? void 0 : _i.intersect) == null ? void 0 : _j.responses) == null ? void 0 : _k.boundWitnessDiviner}]`);
150
- }
163
+ });
151
164
  }
152
165
  /**
153
166
  * Commit the internal state of the process. This is similar
@@ -398,15 +411,24 @@ var _AsyncQueryBusHost = class _AsyncQueryBusHost extends AsyncQueryBusBase {
398
411
  get started() {
399
412
  return !!this._pollId;
400
413
  }
401
- expose(address, options = {}) {
414
+ async expose(id, options) {
402
415
  var _a;
403
- const { failOnAlreadyExposed } = options;
404
- assertEx2(!failOnAlreadyExposed || !this._exposedAddresses.has(address), () => `Address already exposed [${address}]`);
405
- this._exposedAddresses.add(address);
406
- this._exposeOptions[address] = {
407
- ...options
408
- };
409
- (_a = this.logger) == null ? void 0 : _a.debug(`${address} exposed`);
416
+ const { failOnAlreadyExposed, required = true } = options ?? {};
417
+ const module = asModuleInstance(await this.resolver.resolve(id, {
418
+ maxDepth: 10
419
+ }));
420
+ if (!module && required) {
421
+ throw new Error(`Unable to resolve module to expose [${id}]`);
422
+ }
423
+ if (module) {
424
+ assertEx2(!failOnAlreadyExposed || !this._exposedAddresses.has(module.address), () => `Address already exposed: ${id} [${module.address}]`);
425
+ this._exposedAddresses.add(module.address);
426
+ this._exposeOptions[module.address] = {
427
+ ...options
428
+ };
429
+ (_a = this.logger) == null ? void 0 : _a.debug(`${id} exposed [${module.address}]`);
430
+ return module;
431
+ }
410
432
  }
411
433
  async listeningModules() {
412
434
  var _a;
@@ -414,7 +436,9 @@ var _AsyncQueryBusHost = class _AsyncQueryBusHost extends AsyncQueryBusBase {
414
436
  ...((_a = this.config) == null ? void 0 : _a.listeningModules) ?? [],
415
437
  ...this.exposedAddresses.values()
416
438
  ];
417
- const mods = await Promise.all(exposedModules.map(async (listeningModule) => assertEx2(asModuleInstance(await this.resolver.resolve(listeningModule)), () => `Unable to resolve listeningModule [${listeningModule}]`)));
439
+ const mods = await Promise.all(exposedModules.map(async (exposedModule) => assertEx2(asModuleInstance(await this.resolver.resolve(exposedModule, {
440
+ maxDepth: 10
441
+ })), () => `Unable to resolve listeningModule [${exposedModule}]`)));
418
442
  return mods;
419
443
  }
420
444
  start() {
@@ -431,12 +455,18 @@ var _AsyncQueryBusHost = class _AsyncQueryBusHost extends AsyncQueryBusBase {
431
455
  clearTimeoutEx2(this._pollId);
432
456
  this._pollId = void 0;
433
457
  }
434
- unexpose(address, validate = true) {
458
+ async unexpose(id, validate = true) {
435
459
  var _a;
436
- assertEx2(!validate || this._exposedAddresses.has(address), () => `Address not exposed [${address}]`);
437
- this._exposedAddresses.delete(address);
438
- delete this._exposeOptions[address];
439
- (_a = this.logger) == null ? void 0 : _a.debug(`${address} unexposed`);
460
+ const module = asModuleInstance(await this.resolver.resolve(id, {
461
+ maxDepth: 10
462
+ }));
463
+ if (module) {
464
+ assertEx2(!validate || this._exposedAddresses.has(module.address), () => `Address not exposed [${module.address}][${module.id}]`);
465
+ this._exposedAddresses.delete(module.address);
466
+ delete this._exposeOptions[module.address];
467
+ (_a = this.logger) == null ? void 0 : _a.debug(`${module.address} [${module.id}] unexposed`);
468
+ }
469
+ return module;
440
470
  }
441
471
  callLocalModule = async (localModule, query) => {
442
472
  var _a, _b, _c, _d, _e, _f, _g;
@@ -715,22 +745,24 @@ var _PubSubBridge = class _PubSubBridge extends AbstractBridge {
715
745
  return rootInstances;
716
746
  }
717
747
  async exposeHandler(id, options) {
718
- const { maxDepth = 2, direction = "all" } = options ?? {};
719
- const module = assertEx4(await super.resolve(id), () => `Expose failed to locate module [${id}]`);
748
+ const { maxDepth = 2, direction = "all", required = true } = options ?? {};
749
+ const host = assertEx4(this.busHost(), () => "Not configured as a host");
750
+ const module = await host.expose(id, {
751
+ required
752
+ });
720
753
  if (module) {
721
- const host = assertEx4(this.busHost(), () => "Not configured as a host");
722
- host.expose(module.address);
723
- const children = await module.resolve("*", {
754
+ const children = maxDepth > 0 ? await module.resolve("*", {
724
755
  direction,
725
756
  maxDepth,
726
757
  visibility: "public"
727
- });
728
- for (const child of children) {
729
- host.expose(child.address);
730
- }
758
+ }) : [];
759
+ const exposedChildren = (await Promise.all(children.map((child) => this.exposeHandler(child.address, {
760
+ maxDepth: maxDepth - 1,
761
+ required: false
762
+ })))).flat().filter(exists);
731
763
  return [
732
764
  module,
733
- ...children
765
+ ...exposedChildren
734
766
  ];
735
767
  }
736
768
  return [];
@@ -748,25 +780,22 @@ var _PubSubBridge = class _PubSubBridge extends AbstractBridge {
748
780
  return await super.startHandler();
749
781
  }
750
782
  async unexposeHandler(id, options) {
751
- const { maxDepth = 10, direction } = options ?? {};
752
- const filterOptions = {
753
- direction
754
- };
755
- const module = await super.resolve(id, filterOptions);
783
+ const { maxDepth = 2, direction = "all", required = true } = options ?? {};
784
+ const host = assertEx4(this.busHost(), () => "Not configured as a host");
785
+ const module = await host.unexpose(id, required);
756
786
  if (module) {
757
- const host = assertEx4(this.busHost(), () => "Not configured as a host");
758
- host.unexpose(module.address);
759
- const children = await module.resolve("*", {
787
+ const children = maxDepth > 0 ? await module.resolve("*", {
760
788
  direction,
761
789
  maxDepth,
762
790
  visibility: "public"
763
- });
764
- for (const child of children) {
765
- host.unexpose(child.address);
766
- }
791
+ }) : [];
792
+ const exposedChildren = (await Promise.all(children.map((child) => this.unexposeHandler(child.address, {
793
+ maxDepth: maxDepth - 1,
794
+ required: false
795
+ })))).flat().filter(exists);
767
796
  return [
768
797
  module,
769
- ...children
798
+ ...exposedChildren
770
799
  ];
771
800
  }
772
801
  return [];