@xyo-network/bridge-abstract 2.102.0
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/LICENSE +165 -0
- package/README.md +13 -0
- package/dist/browser/AbstractBridge.d.cts +36 -0
- package/dist/browser/AbstractBridge.d.cts.map +1 -0
- package/dist/browser/AbstractBridge.d.mts +36 -0
- package/dist/browser/AbstractBridge.d.mts.map +1 -0
- package/dist/browser/AbstractBridge.d.ts +36 -0
- package/dist/browser/AbstractBridge.d.ts.map +1 -0
- package/dist/browser/AbstractBridgeModuleResolver.d.cts +21 -0
- package/dist/browser/AbstractBridgeModuleResolver.d.cts.map +1 -0
- package/dist/browser/AbstractBridgeModuleResolver.d.mts +21 -0
- package/dist/browser/AbstractBridgeModuleResolver.d.mts.map +1 -0
- package/dist/browser/AbstractBridgeModuleResolver.d.ts +21 -0
- package/dist/browser/AbstractBridgeModuleResolver.d.ts.map +1 -0
- package/dist/browser/AbstractModuleProxy/AbstractModuleProxy.d.cts +74 -0
- package/dist/browser/AbstractModuleProxy/AbstractModuleProxy.d.cts.map +1 -0
- package/dist/browser/AbstractModuleProxy/AbstractModuleProxy.d.mts +74 -0
- package/dist/browser/AbstractModuleProxy/AbstractModuleProxy.d.mts.map +1 -0
- package/dist/browser/AbstractModuleProxy/AbstractModuleProxy.d.ts +74 -0
- package/dist/browser/AbstractModuleProxy/AbstractModuleProxy.d.ts.map +1 -0
- package/dist/browser/AbstractModuleProxy/ModuleProxyResolver.d.cts +65 -0
- package/dist/browser/AbstractModuleProxy/ModuleProxyResolver.d.cts.map +1 -0
- package/dist/browser/AbstractModuleProxy/ModuleProxyResolver.d.mts +65 -0
- package/dist/browser/AbstractModuleProxy/ModuleProxyResolver.d.mts.map +1 -0
- package/dist/browser/AbstractModuleProxy/ModuleProxyResolver.d.ts +65 -0
- package/dist/browser/AbstractModuleProxy/ModuleProxyResolver.d.ts.map +1 -0
- package/dist/browser/AbstractModuleProxy/index.d.cts +3 -0
- package/dist/browser/AbstractModuleProxy/index.d.cts.map +1 -0
- package/dist/browser/AbstractModuleProxy/index.d.mts +3 -0
- package/dist/browser/AbstractModuleProxy/index.d.mts.map +1 -0
- package/dist/browser/AbstractModuleProxy/index.d.ts +3 -0
- package/dist/browser/AbstractModuleProxy/index.d.ts.map +1 -0
- package/dist/browser/index.cjs +585 -0
- package/dist/browser/index.cjs.map +1 -0
- package/dist/browser/index.d.cts +5 -0
- package/dist/browser/index.d.cts.map +1 -0
- package/dist/browser/index.d.mts +5 -0
- package/dist/browser/index.d.mts.map +1 -0
- package/dist/browser/index.d.ts +5 -0
- package/dist/browser/index.d.ts.map +1 -0
- package/dist/browser/index.js +564 -0
- package/dist/browser/index.js.map +1 -0
- package/dist/browser/wrapModuleWithType.d.cts +5 -0
- package/dist/browser/wrapModuleWithType.d.cts.map +1 -0
- package/dist/browser/wrapModuleWithType.d.mts +5 -0
- package/dist/browser/wrapModuleWithType.d.mts.map +1 -0
- package/dist/browser/wrapModuleWithType.d.ts +5 -0
- package/dist/browser/wrapModuleWithType.d.ts.map +1 -0
- package/dist/neutral/AbstractBridge.d.cts +36 -0
- package/dist/neutral/AbstractBridge.d.cts.map +1 -0
- package/dist/neutral/AbstractBridge.d.mts +36 -0
- package/dist/neutral/AbstractBridge.d.mts.map +1 -0
- package/dist/neutral/AbstractBridge.d.ts +36 -0
- package/dist/neutral/AbstractBridge.d.ts.map +1 -0
- package/dist/neutral/AbstractBridgeModuleResolver.d.cts +21 -0
- package/dist/neutral/AbstractBridgeModuleResolver.d.cts.map +1 -0
- package/dist/neutral/AbstractBridgeModuleResolver.d.mts +21 -0
- package/dist/neutral/AbstractBridgeModuleResolver.d.mts.map +1 -0
- package/dist/neutral/AbstractBridgeModuleResolver.d.ts +21 -0
- package/dist/neutral/AbstractBridgeModuleResolver.d.ts.map +1 -0
- package/dist/neutral/AbstractModuleProxy/AbstractModuleProxy.d.cts +74 -0
- package/dist/neutral/AbstractModuleProxy/AbstractModuleProxy.d.cts.map +1 -0
- package/dist/neutral/AbstractModuleProxy/AbstractModuleProxy.d.mts +74 -0
- package/dist/neutral/AbstractModuleProxy/AbstractModuleProxy.d.mts.map +1 -0
- package/dist/neutral/AbstractModuleProxy/AbstractModuleProxy.d.ts +74 -0
- package/dist/neutral/AbstractModuleProxy/AbstractModuleProxy.d.ts.map +1 -0
- package/dist/neutral/AbstractModuleProxy/ModuleProxyResolver.d.cts +65 -0
- package/dist/neutral/AbstractModuleProxy/ModuleProxyResolver.d.cts.map +1 -0
- package/dist/neutral/AbstractModuleProxy/ModuleProxyResolver.d.mts +65 -0
- package/dist/neutral/AbstractModuleProxy/ModuleProxyResolver.d.mts.map +1 -0
- package/dist/neutral/AbstractModuleProxy/ModuleProxyResolver.d.ts +65 -0
- package/dist/neutral/AbstractModuleProxy/ModuleProxyResolver.d.ts.map +1 -0
- package/dist/neutral/AbstractModuleProxy/index.d.cts +3 -0
- package/dist/neutral/AbstractModuleProxy/index.d.cts.map +1 -0
- package/dist/neutral/AbstractModuleProxy/index.d.mts +3 -0
- package/dist/neutral/AbstractModuleProxy/index.d.mts.map +1 -0
- package/dist/neutral/AbstractModuleProxy/index.d.ts +3 -0
- package/dist/neutral/AbstractModuleProxy/index.d.ts.map +1 -0
- package/dist/neutral/index.cjs +585 -0
- package/dist/neutral/index.cjs.map +1 -0
- package/dist/neutral/index.d.cts +5 -0
- package/dist/neutral/index.d.cts.map +1 -0
- package/dist/neutral/index.d.mts +5 -0
- package/dist/neutral/index.d.mts.map +1 -0
- package/dist/neutral/index.d.ts +5 -0
- package/dist/neutral/index.d.ts.map +1 -0
- package/dist/neutral/index.js +564 -0
- package/dist/neutral/index.js.map +1 -0
- package/dist/neutral/wrapModuleWithType.d.cts +5 -0
- package/dist/neutral/wrapModuleWithType.d.cts.map +1 -0
- package/dist/neutral/wrapModuleWithType.d.mts +5 -0
- package/dist/neutral/wrapModuleWithType.d.mts.map +1 -0
- package/dist/neutral/wrapModuleWithType.d.ts +5 -0
- package/dist/neutral/wrapModuleWithType.d.ts.map +1 -0
- package/dist/node/AbstractBridge.d.cts +36 -0
- package/dist/node/AbstractBridge.d.cts.map +1 -0
- package/dist/node/AbstractBridge.d.mts +36 -0
- package/dist/node/AbstractBridge.d.mts.map +1 -0
- package/dist/node/AbstractBridge.d.ts +36 -0
- package/dist/node/AbstractBridge.d.ts.map +1 -0
- package/dist/node/AbstractBridgeModuleResolver.d.cts +21 -0
- package/dist/node/AbstractBridgeModuleResolver.d.cts.map +1 -0
- package/dist/node/AbstractBridgeModuleResolver.d.mts +21 -0
- package/dist/node/AbstractBridgeModuleResolver.d.mts.map +1 -0
- package/dist/node/AbstractBridgeModuleResolver.d.ts +21 -0
- package/dist/node/AbstractBridgeModuleResolver.d.ts.map +1 -0
- package/dist/node/AbstractModuleProxy/AbstractModuleProxy.d.cts +74 -0
- package/dist/node/AbstractModuleProxy/AbstractModuleProxy.d.cts.map +1 -0
- package/dist/node/AbstractModuleProxy/AbstractModuleProxy.d.mts +74 -0
- package/dist/node/AbstractModuleProxy/AbstractModuleProxy.d.mts.map +1 -0
- package/dist/node/AbstractModuleProxy/AbstractModuleProxy.d.ts +74 -0
- package/dist/node/AbstractModuleProxy/AbstractModuleProxy.d.ts.map +1 -0
- package/dist/node/AbstractModuleProxy/ModuleProxyResolver.d.cts +65 -0
- package/dist/node/AbstractModuleProxy/ModuleProxyResolver.d.cts.map +1 -0
- package/dist/node/AbstractModuleProxy/ModuleProxyResolver.d.mts +65 -0
- package/dist/node/AbstractModuleProxy/ModuleProxyResolver.d.mts.map +1 -0
- package/dist/node/AbstractModuleProxy/ModuleProxyResolver.d.ts +65 -0
- package/dist/node/AbstractModuleProxy/ModuleProxyResolver.d.ts.map +1 -0
- package/dist/node/AbstractModuleProxy/index.d.cts +3 -0
- package/dist/node/AbstractModuleProxy/index.d.cts.map +1 -0
- package/dist/node/AbstractModuleProxy/index.d.mts +3 -0
- package/dist/node/AbstractModuleProxy/index.d.mts.map +1 -0
- package/dist/node/AbstractModuleProxy/index.d.ts +3 -0
- package/dist/node/AbstractModuleProxy/index.d.ts.map +1 -0
- package/dist/node/index.cjs +609 -0
- package/dist/node/index.cjs.map +1 -0
- package/dist/node/index.d.cts +5 -0
- package/dist/node/index.d.cts.map +1 -0
- package/dist/node/index.d.mts +5 -0
- package/dist/node/index.d.mts.map +1 -0
- package/dist/node/index.d.ts +5 -0
- package/dist/node/index.d.ts.map +1 -0
- package/dist/node/index.js +580 -0
- package/dist/node/index.js.map +1 -0
- package/dist/node/wrapModuleWithType.d.cts +5 -0
- package/dist/node/wrapModuleWithType.d.cts.map +1 -0
- package/dist/node/wrapModuleWithType.d.mts +5 -0
- package/dist/node/wrapModuleWithType.d.mts.map +1 -0
- package/dist/node/wrapModuleWithType.d.ts +5 -0
- package/dist/node/wrapModuleWithType.d.ts.map +1 -0
- package/package.json +92 -0
- package/src/AbstractBridge.ts +232 -0
- package/src/AbstractBridgeModuleResolver.ts +42 -0
- package/src/AbstractModuleProxy/AbstractModuleProxy.ts +268 -0
- package/src/AbstractModuleProxy/ModuleProxyResolver.ts +143 -0
- package/src/AbstractModuleProxy/index.ts +2 -0
- package/src/index.ts +4 -0
- package/src/wrapModuleWithType.ts +32 -0
- package/typedoc.json +5 -0
|
@@ -0,0 +1,564 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// src/AbstractBridge.ts
|
|
5
|
+
import { assertEx } from "@xylabs/assert";
|
|
6
|
+
import { exists } from "@xylabs/exists";
|
|
7
|
+
import { forget } from "@xylabs/forget";
|
|
8
|
+
import { globallyUnique } from "@xylabs/object";
|
|
9
|
+
import { AddressSchema } from "@xyo-network/address-payload-plugin";
|
|
10
|
+
import { asArchivistInstance } from "@xyo-network/archivist-model";
|
|
11
|
+
import { QueryBoundWitnessWrapper } from "@xyo-network/boundwitness-wrapper";
|
|
12
|
+
import { BridgeConfigSchema, BridgeConnectQuerySchema, BridgeDisconnectQuerySchema, BridgeExposeQuerySchema, BridgeUnexposeQuerySchema, ModuleFilterPayloadSchema } from "@xyo-network/bridge-model";
|
|
13
|
+
import { AbstractModuleInstance } from "@xyo-network/module-abstract";
|
|
14
|
+
import { isAddressModuleFilter, isNameModuleFilter, resolveAddressToInstance, resolvePathToAddress } from "@xyo-network/module-model";
|
|
15
|
+
import { isPayloadOfSchemaType } from "@xyo-network/payload-model";
|
|
16
|
+
var AbstractBridge = class _AbstractBridge extends AbstractModuleInstance {
|
|
17
|
+
static {
|
|
18
|
+
__name(this, "AbstractBridge");
|
|
19
|
+
}
|
|
20
|
+
static configSchemas = [
|
|
21
|
+
...super.configSchemas,
|
|
22
|
+
BridgeConfigSchema
|
|
23
|
+
];
|
|
24
|
+
static defaultConfigSchema = BridgeConfigSchema;
|
|
25
|
+
static uniqueName = globallyUnique("AbstractBridge", _AbstractBridge, "xyo");
|
|
26
|
+
_roots;
|
|
27
|
+
get allowNameResolution() {
|
|
28
|
+
return this.params.allowNameResolution ?? true;
|
|
29
|
+
}
|
|
30
|
+
get queries() {
|
|
31
|
+
return [
|
|
32
|
+
BridgeConnectQuerySchema,
|
|
33
|
+
BridgeDisconnectQuerySchema,
|
|
34
|
+
BridgeExposeQuerySchema,
|
|
35
|
+
BridgeUnexposeQuerySchema,
|
|
36
|
+
...super.queries
|
|
37
|
+
];
|
|
38
|
+
}
|
|
39
|
+
get resolver() {
|
|
40
|
+
return assertEx(this.params.resolver, () => "No resolver provided");
|
|
41
|
+
}
|
|
42
|
+
get _queryAccountPaths() {
|
|
43
|
+
return {
|
|
44
|
+
"network.xyo.query.bridge.connect": "1/1",
|
|
45
|
+
"network.xyo.query.bridge.disconnect": "1/2",
|
|
46
|
+
"network.xyo.query.bridge.expose": "1/4",
|
|
47
|
+
"network.xyo.query.bridge.unexpose": "1/5"
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
async expose(id, options) {
|
|
51
|
+
this._noOverride("expose");
|
|
52
|
+
assertEx(id !== "*", () => "Exposing '*' not supported");
|
|
53
|
+
const addressToExpose = assertEx(await resolvePathToAddress(this, id), () => `Module to expose not found [${id}]`);
|
|
54
|
+
console.log(`expose: ${addressToExpose}`);
|
|
55
|
+
const modules = await this.exposeHandler(addressToExpose, options);
|
|
56
|
+
await this.emit("exposed", {
|
|
57
|
+
module: this,
|
|
58
|
+
modules
|
|
59
|
+
});
|
|
60
|
+
return modules;
|
|
61
|
+
}
|
|
62
|
+
async exposed() {
|
|
63
|
+
this._noOverride("exposed");
|
|
64
|
+
return await this.exposedHandler();
|
|
65
|
+
}
|
|
66
|
+
// eslint-disable-next-line complexity
|
|
67
|
+
async resolve(idOrFilter = "*", options = {}) {
|
|
68
|
+
const roots = this._roots ?? [];
|
|
69
|
+
const workingSet = options.direction === "up" ? [
|
|
70
|
+
this
|
|
71
|
+
] : [
|
|
72
|
+
...roots,
|
|
73
|
+
this
|
|
74
|
+
];
|
|
75
|
+
if (idOrFilter === "*") {
|
|
76
|
+
const remainingDepth = (options.maxDepth ?? 5) - 1;
|
|
77
|
+
return remainingDepth <= 0 ? workingSet : [
|
|
78
|
+
...workingSet,
|
|
79
|
+
...(await Promise.all(roots.map((mod) => mod.resolve("*", {
|
|
80
|
+
...options,
|
|
81
|
+
maxDepth: remainingDepth
|
|
82
|
+
})))).flat()
|
|
83
|
+
];
|
|
84
|
+
}
|
|
85
|
+
switch (typeof idOrFilter) {
|
|
86
|
+
case "string": {
|
|
87
|
+
const parts = idOrFilter.split(":");
|
|
88
|
+
const first = parts.shift();
|
|
89
|
+
const result = workingSet.find((mod) => {
|
|
90
|
+
return first === mod.address || first === mod.modName;
|
|
91
|
+
});
|
|
92
|
+
return parts.length === 0 ? result : result?.resolve(parts.join(":"), options);
|
|
93
|
+
}
|
|
94
|
+
case "object": {
|
|
95
|
+
const results = [];
|
|
96
|
+
if (isNameModuleFilter(idOrFilter)) {
|
|
97
|
+
for (const mod of workingSet) {
|
|
98
|
+
if (mod.modName && idOrFilter.name.includes(mod.modName))
|
|
99
|
+
results.push(mod);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (isAddressModuleFilter(idOrFilter)) {
|
|
103
|
+
for (const mod of workingSet) {
|
|
104
|
+
if (mod.modName && idOrFilter.address.includes(mod.address))
|
|
105
|
+
results.push(mod);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return results;
|
|
109
|
+
}
|
|
110
|
+
default: {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
async startHandler() {
|
|
116
|
+
const { discoverRoots } = this.config;
|
|
117
|
+
if (discoverRoots === "lazy") {
|
|
118
|
+
forget(this.getRoots());
|
|
119
|
+
} else {
|
|
120
|
+
await this.getRoots();
|
|
121
|
+
}
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
async unexpose(id, options) {
|
|
125
|
+
this._noOverride("unexpose");
|
|
126
|
+
const addressToUnexpose = assertEx(await resolvePathToAddress(this, id), () => `Module to unexpose not found [${id}]`);
|
|
127
|
+
const modules = await this.unexposeHandler(addressToUnexpose, options);
|
|
128
|
+
await this.emit("unexposed", {
|
|
129
|
+
module: this,
|
|
130
|
+
modules
|
|
131
|
+
});
|
|
132
|
+
return modules;
|
|
133
|
+
}
|
|
134
|
+
async queryHandler(query, payloads) {
|
|
135
|
+
const wrapper = await QueryBoundWitnessWrapper.parseQuery(query, payloads);
|
|
136
|
+
const queryPayload = await wrapper.getQuery();
|
|
137
|
+
const resultPayloads = [];
|
|
138
|
+
switch (queryPayload.schema) {
|
|
139
|
+
case BridgeExposeQuerySchema: {
|
|
140
|
+
const filterPayloads = (payloads ?? []).filter(isPayloadOfSchemaType(ModuleFilterPayloadSchema));
|
|
141
|
+
assertEx(filterPayloads, () => "At least one filter is required");
|
|
142
|
+
await Promise.all(filterPayloads.map(async (filter) => {
|
|
143
|
+
const { id, ...options } = filter;
|
|
144
|
+
const modules = await this.expose(id, options);
|
|
145
|
+
modules.map((module) => {
|
|
146
|
+
const addressPayload = {
|
|
147
|
+
address: module.address,
|
|
148
|
+
schema: AddressSchema
|
|
149
|
+
};
|
|
150
|
+
resultPayloads.push(addressPayload);
|
|
151
|
+
});
|
|
152
|
+
}));
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
case BridgeUnexposeQuerySchema: {
|
|
156
|
+
const filterPayloads = (payloads ?? []).filter(isPayloadOfSchemaType(ModuleFilterPayloadSchema));
|
|
157
|
+
assertEx(filterPayloads, () => "At least one filter is required");
|
|
158
|
+
await Promise.all(filterPayloads.map(async (filter) => {
|
|
159
|
+
const { id, ...options } = filter;
|
|
160
|
+
const modules = await this.unexpose(id, options);
|
|
161
|
+
modules.map((module) => {
|
|
162
|
+
const addressPayload = {
|
|
163
|
+
address: module.address,
|
|
164
|
+
schema: AddressSchema
|
|
165
|
+
};
|
|
166
|
+
resultPayloads.push(addressPayload);
|
|
167
|
+
});
|
|
168
|
+
}));
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
default: {
|
|
172
|
+
return await super.queryHandler(query, payloads);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return resultPayloads;
|
|
176
|
+
}
|
|
177
|
+
async resolveArchivingArchivists() {
|
|
178
|
+
const archivists = this.archiving?.archivists;
|
|
179
|
+
if (!archivists)
|
|
180
|
+
return [];
|
|
181
|
+
const resolvedAddresses = (await Promise.all(archivists.map(async (archivist) => (await Promise.all((await this.parents()).map((parent) => resolvePathToAddress(parent, archivist)))).filter(exists)))).flat().filter(exists);
|
|
182
|
+
const resolved = (await Promise.all(resolvedAddresses.map((address) => resolveAddressToInstance(this, address)))).filter(exists);
|
|
183
|
+
return resolved.map((mod) => asArchivistInstance(mod)).filter(exists);
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
// src/AbstractBridgeModuleResolver.ts
|
|
188
|
+
import { ObjectResolverPriority } from "@xyo-network/module-model";
|
|
189
|
+
import { CompositeModuleResolver } from "@xyo-network/module-resolver";
|
|
190
|
+
var AbstractBridgeModuleResolver = class extends CompositeModuleResolver {
|
|
191
|
+
static {
|
|
192
|
+
__name(this, "AbstractBridgeModuleResolver");
|
|
193
|
+
}
|
|
194
|
+
get priority() {
|
|
195
|
+
return ObjectResolverPriority.VeryLow;
|
|
196
|
+
}
|
|
197
|
+
async resolveHandler(id, options, _params) {
|
|
198
|
+
if (id === "*") {
|
|
199
|
+
return [];
|
|
200
|
+
}
|
|
201
|
+
return await super.resolveHandler(id, options);
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
// src/AbstractModuleProxy/AbstractModuleProxy.ts
|
|
206
|
+
import { assertEx as assertEx3 } from "@xylabs/assert";
|
|
207
|
+
import { forget as forget2 } from "@xylabs/forget";
|
|
208
|
+
import { asAddress } from "@xylabs/hex";
|
|
209
|
+
import { compact } from "@xylabs/lodash";
|
|
210
|
+
import { toJsonString } from "@xylabs/object";
|
|
211
|
+
import { BoundWitnessWrapper, QueryBoundWitnessWrapper as QueryBoundWitnessWrapper2 } from "@xyo-network/boundwitness-wrapper";
|
|
212
|
+
import { ModuleManifestPayloadSchema, NodeManifestPayloadSchema } from "@xyo-network/manifest-model";
|
|
213
|
+
import { AbstractModuleInstance as AbstractModuleInstance2 } from "@xyo-network/module-abstract";
|
|
214
|
+
import { AddressPreviousHashSchema, DeadModuleError, ModuleAddressQuerySchema, ModuleManifestQuerySchema, ModuleStateQuerySchema } from "@xyo-network/module-model";
|
|
215
|
+
import { ModuleWrapper } from "@xyo-network/module-wrapper";
|
|
216
|
+
import { PayloadBuilder } from "@xyo-network/payload-builder";
|
|
217
|
+
import { isPayloadOfSchemaType as isPayloadOfSchemaType2, ModuleErrorSchema } from "@xyo-network/payload-model";
|
|
218
|
+
import { QuerySchema } from "@xyo-network/query-payload-plugin";
|
|
219
|
+
import { LRUCache } from "lru-cache";
|
|
220
|
+
|
|
221
|
+
// src/AbstractModuleProxy/ModuleProxyResolver.ts
|
|
222
|
+
import { assertEx as assertEx2 } from "@xylabs/assert";
|
|
223
|
+
import { exists as exists2 } from "@xylabs/exists";
|
|
224
|
+
import { Account } from "@xyo-network/account";
|
|
225
|
+
import { isAddressModuleFilter as isAddressModuleFilter2, isNameModuleFilter as isNameModuleFilter2, ObjectResolverPriority as ObjectResolverPriority2 } from "@xyo-network/module-model";
|
|
226
|
+
import { CompositeModuleResolver as CompositeModuleResolver2 } from "@xyo-network/module-resolver";
|
|
227
|
+
|
|
228
|
+
// src/wrapModuleWithType.ts
|
|
229
|
+
import { isArchivistModule } from "@xyo-network/archivist-model";
|
|
230
|
+
import { ArchivistWrapper } from "@xyo-network/archivist-wrapper";
|
|
231
|
+
import { isDivinerModule } from "@xyo-network/diviner-model";
|
|
232
|
+
import { DivinerWrapper } from "@xyo-network/diviner-wrapper";
|
|
233
|
+
import { isNodeModule } from "@xyo-network/node-model";
|
|
234
|
+
import { NodeWrapper } from "@xyo-network/node-wrapper";
|
|
235
|
+
import { isSentinelModule } from "@xyo-network/sentinel-model";
|
|
236
|
+
import { SentinelWrapper } from "@xyo-network/sentinel-wrapper";
|
|
237
|
+
import { isWitnessModule } from "@xyo-network/witness-model";
|
|
238
|
+
import { WitnessWrapper } from "@xyo-network/witness-wrapper";
|
|
239
|
+
var wrapModuleWithType = /* @__PURE__ */ __name((module, account) => {
|
|
240
|
+
if (isArchivistModule(module)) {
|
|
241
|
+
return ArchivistWrapper.wrap(module, account);
|
|
242
|
+
}
|
|
243
|
+
if (isDivinerModule(module)) {
|
|
244
|
+
return DivinerWrapper.wrap(module, account);
|
|
245
|
+
}
|
|
246
|
+
if (isNodeModule(module)) {
|
|
247
|
+
return NodeWrapper.wrap(module, account);
|
|
248
|
+
}
|
|
249
|
+
if (isSentinelModule(module)) {
|
|
250
|
+
return SentinelWrapper.wrap(module, account);
|
|
251
|
+
}
|
|
252
|
+
if (isWitnessModule(module)) {
|
|
253
|
+
return WitnessWrapper.wrap(module, account);
|
|
254
|
+
}
|
|
255
|
+
throw "Failed to wrap";
|
|
256
|
+
}, "wrapModuleWithType");
|
|
257
|
+
|
|
258
|
+
// src/AbstractModuleProxy/ModuleProxyResolver.ts
|
|
259
|
+
var ModuleProxyResolver = class {
|
|
260
|
+
static {
|
|
261
|
+
__name(this, "ModuleProxyResolver");
|
|
262
|
+
}
|
|
263
|
+
options;
|
|
264
|
+
downResolver;
|
|
265
|
+
constructor(options) {
|
|
266
|
+
this.options = options;
|
|
267
|
+
this.downResolver = new CompositeModuleResolver2({
|
|
268
|
+
moduleIdentifierTransformers: options.moduleIdentifierTransformers,
|
|
269
|
+
root: this.root
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
get priority() {
|
|
273
|
+
return ObjectResolverPriority2.VeryLow;
|
|
274
|
+
}
|
|
275
|
+
get root() {
|
|
276
|
+
return this.options.module;
|
|
277
|
+
}
|
|
278
|
+
get childAddressMap() {
|
|
279
|
+
return this.options.childAddressMap;
|
|
280
|
+
}
|
|
281
|
+
get host() {
|
|
282
|
+
return this.options.host;
|
|
283
|
+
}
|
|
284
|
+
get module() {
|
|
285
|
+
return this.options.module;
|
|
286
|
+
}
|
|
287
|
+
addResolver(_resolver) {
|
|
288
|
+
throw new Error("Not supported");
|
|
289
|
+
}
|
|
290
|
+
removeResolver(_resolver) {
|
|
291
|
+
throw new Error("Not supported");
|
|
292
|
+
}
|
|
293
|
+
async resolve(idOrFilter = "*", options) {
|
|
294
|
+
const direction = options?.direction ?? "all";
|
|
295
|
+
if (idOrFilter === "*") {
|
|
296
|
+
const childAddresses = Object.keys(this.childAddressMap);
|
|
297
|
+
const resolvedChildren = await Promise.all(childAddresses.map((address) => this.resolve(address, options)));
|
|
298
|
+
return resolvedChildren.filter(exists2);
|
|
299
|
+
} else if (typeof idOrFilter === "string") {
|
|
300
|
+
const idParts = idOrFilter.split(":");
|
|
301
|
+
const firstPart = assertEx2(idParts.shift(), () => "Invalid module identifier at first position");
|
|
302
|
+
const firstPartAddress = await this.resolveIdentifier(firstPart);
|
|
303
|
+
if (firstPartAddress) {
|
|
304
|
+
const remainingParts = idParts.length > 0 ? idParts.join(":") : void 0;
|
|
305
|
+
if (direction === "down" || direction === "all") {
|
|
306
|
+
const downResolverModule = await this.downResolver.resolve(firstPartAddress);
|
|
307
|
+
if (downResolverModule) {
|
|
308
|
+
return remainingParts ? downResolverModule.resolve(remainingParts, options) : downResolverModule;
|
|
309
|
+
}
|
|
310
|
+
const addressToProxy = Object.keys(this.childAddressMap).includes(firstPartAddress) ? firstPartAddress : Object.entries(this.childAddressMap).find(([_, value]) => value === firstPartAddress)?.[0];
|
|
311
|
+
if (addressToProxy) {
|
|
312
|
+
const proxy = await this.host.resolve(addressToProxy, {
|
|
313
|
+
...options,
|
|
314
|
+
direction: "down"
|
|
315
|
+
});
|
|
316
|
+
if (proxy) {
|
|
317
|
+
const wrapped = wrapModuleWithType(proxy, Account.randomSync());
|
|
318
|
+
return remainingParts ? wrapped?.resolve(remainingParts, options) : wrapped;
|
|
319
|
+
}
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
return;
|
|
325
|
+
} else {
|
|
326
|
+
const filter = idOrFilter;
|
|
327
|
+
if (isAddressModuleFilter2(filter)) {
|
|
328
|
+
const results = (await Promise.all(filter.address.map((item) => this.resolve(item, options)))).filter(exists2);
|
|
329
|
+
return results;
|
|
330
|
+
} else if (isNameModuleFilter2(filter)) {
|
|
331
|
+
return (await Promise.all(filter.name.map((item) => this.resolve(item, options)))).filter(exists2);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
resolveIdentifier(id, _options) {
|
|
336
|
+
if (this.childAddressMap[id]) {
|
|
337
|
+
return id;
|
|
338
|
+
}
|
|
339
|
+
const addressFromName = Object.entries(this.childAddressMap).find(([, name]) => name === id)?.[0];
|
|
340
|
+
if (addressFromName) {
|
|
341
|
+
return addressFromName;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
async resolvePrivate(id, _options) {
|
|
345
|
+
if (id === "*")
|
|
346
|
+
return await Promise.resolve([]);
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
// src/AbstractModuleProxy/AbstractModuleProxy.ts
|
|
351
|
+
var AbstractModuleProxy = class _AbstractModuleProxy extends AbstractModuleInstance2 {
|
|
352
|
+
static {
|
|
353
|
+
__name(this, "AbstractModuleProxy");
|
|
354
|
+
}
|
|
355
|
+
static requiredQueries = [
|
|
356
|
+
ModuleStateQuerySchema
|
|
357
|
+
];
|
|
358
|
+
_config;
|
|
359
|
+
_publicChildren;
|
|
360
|
+
_state = void 0;
|
|
361
|
+
_stateInProcess = false;
|
|
362
|
+
_spamTrap = new LRUCache({
|
|
363
|
+
max: 1e3,
|
|
364
|
+
ttl: 1e3 * 60,
|
|
365
|
+
ttlAutopurge: true
|
|
366
|
+
});
|
|
367
|
+
constructor(params) {
|
|
368
|
+
params.addToResolvers = false;
|
|
369
|
+
super(_AbstractModuleProxy.privateConstructorKey, params, params.account);
|
|
370
|
+
}
|
|
371
|
+
get address() {
|
|
372
|
+
return this.params.moduleAddress;
|
|
373
|
+
}
|
|
374
|
+
get archiving() {
|
|
375
|
+
return this.params?.archiving;
|
|
376
|
+
}
|
|
377
|
+
get config() {
|
|
378
|
+
return assertEx3(this._config, () => "Config not set");
|
|
379
|
+
}
|
|
380
|
+
get queries() {
|
|
381
|
+
const queryPayloads = assertEx3(this._state, () => "Module state not found. Make sure proxy has been started").filter((item) => isPayloadOfSchemaType2(QuerySchema)(item));
|
|
382
|
+
return queryPayloads.map((payload) => payload.query);
|
|
383
|
+
}
|
|
384
|
+
get _queryAccountPaths() {
|
|
385
|
+
return this._baseModuleQueryAccountPaths;
|
|
386
|
+
}
|
|
387
|
+
static hasRequiredQueries(module) {
|
|
388
|
+
return this.missingRequiredQueries(module).length === 0;
|
|
389
|
+
}
|
|
390
|
+
static missingRequiredQueries(module) {
|
|
391
|
+
const moduleQueries = module.queries;
|
|
392
|
+
return compact(this.requiredQueries.map((query) => {
|
|
393
|
+
return moduleQueries.includes(query) ? null : query;
|
|
394
|
+
}));
|
|
395
|
+
}
|
|
396
|
+
async addressPreviousHash() {
|
|
397
|
+
const queryPayload = {
|
|
398
|
+
schema: ModuleAddressQuerySchema
|
|
399
|
+
};
|
|
400
|
+
const result = assertEx3((await this.sendQuery(queryPayload, void 0, this.account)).find(isPayloadOfSchemaType2(AddressPreviousHashSchema)), () => "Result did not include correct payload");
|
|
401
|
+
return result;
|
|
402
|
+
}
|
|
403
|
+
childAddressByName(name) {
|
|
404
|
+
const nodeManifests = this._state?.filter(isPayloadOfSchemaType2(NodeManifestPayloadSchema));
|
|
405
|
+
const childPairs = nodeManifests?.flatMap((nodeManifest) => Object.entries(nodeManifest.status?.children ?? {}));
|
|
406
|
+
return asAddress(childPairs?.find(([_, childName]) => childName === name)?.[0]);
|
|
407
|
+
}
|
|
408
|
+
async childAddressMap() {
|
|
409
|
+
const state = await this.state();
|
|
410
|
+
const result = {};
|
|
411
|
+
const nodeManifests = state.filter(isPayloadOfSchemaType2(NodeManifestPayloadSchema));
|
|
412
|
+
for (const manifest of nodeManifests) {
|
|
413
|
+
const children = manifest.modules?.public ?? [];
|
|
414
|
+
for (const child of children) {
|
|
415
|
+
if (typeof child === "object") {
|
|
416
|
+
const address = child.status?.address;
|
|
417
|
+
if (address) {
|
|
418
|
+
result[address] = child.config.name ?? null;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
return result;
|
|
424
|
+
}
|
|
425
|
+
async manifest(maxDepth) {
|
|
426
|
+
const queryPayload = {
|
|
427
|
+
schema: ModuleManifestQuerySchema,
|
|
428
|
+
...maxDepth === void 0 ? {} : {
|
|
429
|
+
maxDepth
|
|
430
|
+
}
|
|
431
|
+
};
|
|
432
|
+
return (await this.sendQuery(queryPayload))[0];
|
|
433
|
+
}
|
|
434
|
+
async moduleAddress() {
|
|
435
|
+
const queryPayload = {
|
|
436
|
+
schema: ModuleAddressQuerySchema
|
|
437
|
+
};
|
|
438
|
+
return await this.sendQuery(queryPayload);
|
|
439
|
+
}
|
|
440
|
+
async previousHash() {
|
|
441
|
+
const queryPayload = {
|
|
442
|
+
schema: ModuleAddressQuerySchema
|
|
443
|
+
};
|
|
444
|
+
return (await this.sendQuery(queryPayload)).pop().previousHash;
|
|
445
|
+
}
|
|
446
|
+
async publicChildren() {
|
|
447
|
+
this._publicChildren = this._publicChildren ?? await super.publicChildren();
|
|
448
|
+
return this._publicChildren;
|
|
449
|
+
}
|
|
450
|
+
async query(query, payloads) {
|
|
451
|
+
this._checkDead();
|
|
452
|
+
return await this.busy(async () => {
|
|
453
|
+
try {
|
|
454
|
+
await this.checkSpam(query);
|
|
455
|
+
this.params.onQuerySendStarted?.({
|
|
456
|
+
payloads,
|
|
457
|
+
query
|
|
458
|
+
});
|
|
459
|
+
const result = await this.proxyQueryHandler(query, payloads);
|
|
460
|
+
this.params.onQuerySendFinished?.({
|
|
461
|
+
payloads,
|
|
462
|
+
query,
|
|
463
|
+
result,
|
|
464
|
+
status: "success"
|
|
465
|
+
});
|
|
466
|
+
if (this.archiving && this.isAllowedArchivingQuery(query.schema)) {
|
|
467
|
+
forget2(this.storeToArchivists(result.flat()));
|
|
468
|
+
}
|
|
469
|
+
forget2(this.emit("moduleQueried", {
|
|
470
|
+
module: this,
|
|
471
|
+
payloads,
|
|
472
|
+
query,
|
|
473
|
+
result
|
|
474
|
+
}));
|
|
475
|
+
return result;
|
|
476
|
+
} catch (ex) {
|
|
477
|
+
this.params.onQuerySendFinished?.({
|
|
478
|
+
payloads,
|
|
479
|
+
query,
|
|
480
|
+
status: "failure"
|
|
481
|
+
});
|
|
482
|
+
const error = ex;
|
|
483
|
+
this._lastError = error;
|
|
484
|
+
const deadError = new DeadModuleError(this.address, error);
|
|
485
|
+
const errorPayload = {
|
|
486
|
+
message: deadError.message,
|
|
487
|
+
name: deadError.name,
|
|
488
|
+
schema: ModuleErrorSchema
|
|
489
|
+
};
|
|
490
|
+
const sourceQuery = await PayloadBuilder.build(assertEx3(QueryBoundWitnessWrapper2.unwrap(query), () => "Invalid query"));
|
|
491
|
+
return await this.bindQueryResult(sourceQuery, [], void 0, [
|
|
492
|
+
errorPayload
|
|
493
|
+
]);
|
|
494
|
+
}
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
queryHandler(_query, _payloads, _queryConfig) {
|
|
498
|
+
throw new Error("queryHandler should never be called");
|
|
499
|
+
}
|
|
500
|
+
async queryable(_query, _payloads, _queryConfig) {
|
|
501
|
+
return await Promise.resolve(true);
|
|
502
|
+
}
|
|
503
|
+
async resolveArchivingArchivists() {
|
|
504
|
+
return await this.params.archiving?.resolveArchivists() ?? [];
|
|
505
|
+
}
|
|
506
|
+
setConfig(config) {
|
|
507
|
+
this._config = config;
|
|
508
|
+
}
|
|
509
|
+
async startHandler() {
|
|
510
|
+
const state = await this.state();
|
|
511
|
+
const manifestPayload = state.find((payload) => isPayloadOfSchemaType2(NodeManifestPayloadSchema)(payload) || isPayloadOfSchemaType2(ModuleManifestPayloadSchema)(payload));
|
|
512
|
+
const manifest = assertEx3(manifestPayload, () => "Can't find manifest payload");
|
|
513
|
+
this.setConfig({
|
|
514
|
+
...manifest.config
|
|
515
|
+
});
|
|
516
|
+
this.downResolver.addResolver(new ModuleProxyResolver({
|
|
517
|
+
childAddressMap: await this.childAddressMap(),
|
|
518
|
+
host: this.params.host,
|
|
519
|
+
module: this,
|
|
520
|
+
moduleIdentifierTransformers: this.params.moduleIdentifierTransformers
|
|
521
|
+
}));
|
|
522
|
+
return await super.startHandler();
|
|
523
|
+
}
|
|
524
|
+
async state() {
|
|
525
|
+
if (this._state === void 0) {
|
|
526
|
+
const stateQueryPayload = {
|
|
527
|
+
query: ModuleStateQuerySchema,
|
|
528
|
+
schema: QuerySchema
|
|
529
|
+
};
|
|
530
|
+
const manifestQueryPayload = {
|
|
531
|
+
query: ModuleManifestQuerySchema,
|
|
532
|
+
schema: QuerySchema
|
|
533
|
+
};
|
|
534
|
+
this._state = [
|
|
535
|
+
stateQueryPayload,
|
|
536
|
+
manifestQueryPayload
|
|
537
|
+
];
|
|
538
|
+
const wrapper = ModuleWrapper.wrap(this, this.account);
|
|
539
|
+
this._state = await wrapper.state();
|
|
540
|
+
}
|
|
541
|
+
return this._state;
|
|
542
|
+
}
|
|
543
|
+
async filterErrors(result) {
|
|
544
|
+
const wrapper = await BoundWitnessWrapper.wrap(result[0], result[1]);
|
|
545
|
+
return wrapper.payloadsBySchema(ModuleErrorSchema);
|
|
546
|
+
}
|
|
547
|
+
//this checks and warns if we are getting spammed by the same query
|
|
548
|
+
async checkSpam(query) {
|
|
549
|
+
const hash = await PayloadBuilder.hash(query);
|
|
550
|
+
const previousCount = this._spamTrap.get(hash) ?? 0;
|
|
551
|
+
if (previousCount > 0) {
|
|
552
|
+
this.logger?.warn(`Spam trap triggered for query: ${hash} from ${toJsonString(query.addresses)}`);
|
|
553
|
+
}
|
|
554
|
+
this._spamTrap.set(hash, previousCount + 1);
|
|
555
|
+
}
|
|
556
|
+
};
|
|
557
|
+
export {
|
|
558
|
+
AbstractBridge,
|
|
559
|
+
AbstractBridgeModuleResolver,
|
|
560
|
+
AbstractModuleProxy,
|
|
561
|
+
ModuleProxyResolver,
|
|
562
|
+
wrapModuleWithType
|
|
563
|
+
};
|
|
564
|
+
//# sourceMappingURL=index.js.map
|