@xyo-network/module-abstract 2.99.1 → 2.99.3
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/dist/neutral/AbstractModule.d.cts +108 -0
- package/dist/neutral/AbstractModule.d.cts.map +1 -0
- package/dist/neutral/AbstractModule.d.mts +108 -0
- package/dist/neutral/AbstractModule.d.mts.map +1 -0
- package/dist/neutral/AbstractModule.d.ts +108 -0
- package/dist/neutral/AbstractModule.d.ts.map +1 -0
- package/dist/neutral/AbstractModuleInstance.d.cts +53 -0
- package/dist/neutral/AbstractModuleInstance.d.cts.map +1 -0
- package/dist/neutral/AbstractModuleInstance.d.mts +53 -0
- package/dist/neutral/AbstractModuleInstance.d.mts.map +1 -0
- package/dist/neutral/AbstractModuleInstance.d.ts +53 -0
- package/dist/neutral/AbstractModuleInstance.d.ts.map +1 -0
- package/dist/neutral/BaseEmitter.d.cts +17 -0
- package/dist/neutral/BaseEmitter.d.cts.map +1 -0
- package/dist/neutral/BaseEmitter.d.mts +17 -0
- package/dist/neutral/BaseEmitter.d.mts.map +1 -0
- package/dist/neutral/BaseEmitter.d.ts +17 -0
- package/dist/neutral/BaseEmitter.d.ts.map +1 -0
- package/dist/neutral/Error.d.cts +16 -0
- package/dist/neutral/Error.d.cts.map +1 -0
- package/dist/neutral/Error.d.mts +16 -0
- package/dist/neutral/Error.d.mts.map +1 -0
- package/dist/neutral/Error.d.ts +16 -0
- package/dist/neutral/Error.d.ts.map +1 -0
- package/dist/neutral/QueryValidator/ModuleConfigQueryValidator.d.cts +17 -0
- package/dist/neutral/QueryValidator/ModuleConfigQueryValidator.d.cts.map +1 -0
- package/dist/neutral/QueryValidator/ModuleConfigQueryValidator.d.mts +17 -0
- package/dist/neutral/QueryValidator/ModuleConfigQueryValidator.d.mts.map +1 -0
- package/dist/neutral/QueryValidator/ModuleConfigQueryValidator.d.ts +17 -0
- package/dist/neutral/QueryValidator/ModuleConfigQueryValidator.d.ts.map +1 -0
- package/dist/neutral/QueryValidator/QueryValidator.d.cts +8 -0
- package/dist/neutral/QueryValidator/QueryValidator.d.cts.map +1 -0
- package/dist/neutral/QueryValidator/QueryValidator.d.mts +8 -0
- package/dist/neutral/QueryValidator/QueryValidator.d.mts.map +1 -0
- package/dist/neutral/QueryValidator/QueryValidator.d.ts +8 -0
- package/dist/neutral/QueryValidator/QueryValidator.d.ts.map +1 -0
- package/dist/neutral/QueryValidator/SupportedQueryValidator.d.cts +22 -0
- package/dist/neutral/QueryValidator/SupportedQueryValidator.d.cts.map +1 -0
- package/dist/neutral/QueryValidator/SupportedQueryValidator.d.mts +22 -0
- package/dist/neutral/QueryValidator/SupportedQueryValidator.d.mts.map +1 -0
- package/dist/neutral/QueryValidator/SupportedQueryValidator.d.ts +22 -0
- package/dist/neutral/QueryValidator/SupportedQueryValidator.d.ts.map +1 -0
- package/dist/neutral/QueryValidator/index.d.cts +4 -0
- package/dist/neutral/QueryValidator/index.d.cts.map +1 -0
- package/dist/neutral/QueryValidator/index.d.mts +4 -0
- package/dist/neutral/QueryValidator/index.d.mts.map +1 -0
- package/dist/neutral/QueryValidator/index.d.ts +4 -0
- package/dist/neutral/QueryValidator/index.d.ts.map +1 -0
- package/dist/neutral/determineAccount.d.cts +14 -0
- package/dist/neutral/determineAccount.d.cts.map +1 -0
- package/dist/neutral/determineAccount.d.mts +14 -0
- package/dist/neutral/determineAccount.d.mts.map +1 -0
- package/dist/neutral/determineAccount.d.ts +14 -0
- package/dist/neutral/determineAccount.d.ts.map +1 -0
- package/dist/neutral/index.cjs +1059 -0
- package/dist/neutral/index.cjs.map +1 -0
- package/dist/neutral/index.d.cts +6 -0
- package/dist/neutral/index.d.cts.map +1 -0
- package/dist/neutral/index.d.mts +6 -0
- package/dist/neutral/index.d.mts.map +1 -0
- package/dist/neutral/index.d.ts +6 -0
- package/dist/neutral/index.d.ts.map +1 -0
- package/dist/neutral/index.js +1038 -0
- package/dist/neutral/index.js.map +1 -0
- package/package.json +28 -28
|
@@ -0,0 +1,1038 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// src/AbstractModule.ts
|
|
5
|
+
import { assertEx as assertEx2 } from "@xylabs/assert";
|
|
6
|
+
import { handleError, handleErrorAsync } from "@xylabs/error";
|
|
7
|
+
import { exists } from "@xylabs/exists";
|
|
8
|
+
import { compact } from "@xylabs/lodash";
|
|
9
|
+
import { ConsoleLogger, IdLogger, LogLevel } from "@xylabs/logger";
|
|
10
|
+
import { Base as Base2, globallyUnique } from "@xylabs/object";
|
|
11
|
+
import { PromiseEx } from "@xylabs/promise";
|
|
12
|
+
import { Account as Account2 } from "@xyo-network/account";
|
|
13
|
+
import { AddressSchema } from "@xyo-network/address-payload-plugin";
|
|
14
|
+
import { asArchivistInstance } from "@xyo-network/archivist-model";
|
|
15
|
+
import { BoundWitnessBuilder, QueryBoundWitnessBuilder } from "@xyo-network/boundwitness-builder";
|
|
16
|
+
import { QueryBoundWitnessWrapper as QueryBoundWitnessWrapper3 } from "@xyo-network/boundwitness-wrapper";
|
|
17
|
+
import { ConfigSchema } from "@xyo-network/config-payload-plugin";
|
|
18
|
+
import { AddressPreviousHashSchema, DeadModuleError, isModuleName, ModuleAddressQuerySchema, ModuleConfigSchema, ModuleDescriptionSchema, ModuleFactory, ModuleManifestQuerySchema, ModuleStateQuerySchema, ModuleSubscribeQuerySchema, ObjectResolverPriority, serializableField } from "@xyo-network/module-model";
|
|
19
|
+
import { PayloadBuilder as PayloadBuilder2 } from "@xyo-network/payload-builder";
|
|
20
|
+
import { QuerySchema } from "@xyo-network/query-payload-plugin";
|
|
21
|
+
import { LRUCache } from "lru-cache";
|
|
22
|
+
|
|
23
|
+
// src/BaseEmitter.ts
|
|
24
|
+
import { Base } from "@xylabs/object";
|
|
25
|
+
import { Events } from "@xyo-network/module-events";
|
|
26
|
+
var BaseEmitter = class extends Base {
|
|
27
|
+
static {
|
|
28
|
+
__name(this, "BaseEmitter");
|
|
29
|
+
}
|
|
30
|
+
//just here to query types
|
|
31
|
+
eventData = {};
|
|
32
|
+
events;
|
|
33
|
+
constructor(params) {
|
|
34
|
+
super(params);
|
|
35
|
+
this.events = new Events();
|
|
36
|
+
}
|
|
37
|
+
clearListeners(eventNames) {
|
|
38
|
+
return this.events.clearListeners(eventNames);
|
|
39
|
+
}
|
|
40
|
+
emit(eventName, eventArgs) {
|
|
41
|
+
return this.events.emit(eventName, eventArgs);
|
|
42
|
+
}
|
|
43
|
+
emitSerial(eventName, eventArgs) {
|
|
44
|
+
return this.events.emitSerial(eventName, eventArgs);
|
|
45
|
+
}
|
|
46
|
+
listenerCount(eventNames) {
|
|
47
|
+
return this.events.listenerCount(eventNames);
|
|
48
|
+
}
|
|
49
|
+
off(eventNames, listener) {
|
|
50
|
+
return this.events.off(eventNames, listener);
|
|
51
|
+
}
|
|
52
|
+
offAny(listener) {
|
|
53
|
+
return this.events.offAny(listener);
|
|
54
|
+
}
|
|
55
|
+
on(eventNames, listener) {
|
|
56
|
+
return this.events.on(eventNames, listener);
|
|
57
|
+
}
|
|
58
|
+
onAny(listener) {
|
|
59
|
+
return this.events.onAny(listener);
|
|
60
|
+
}
|
|
61
|
+
once(eventName, listener) {
|
|
62
|
+
return this.events.once(eventName, listener);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// src/determineAccount.ts
|
|
67
|
+
import { assertEx } from "@xylabs/assert";
|
|
68
|
+
import { Account } from "@xyo-network/account";
|
|
69
|
+
var isDetermineAccountFromAccountParams = /* @__PURE__ */ __name((params) => {
|
|
70
|
+
assertEx(!params.accountPath, () => "accountPath may not be provided when account is provided");
|
|
71
|
+
return !!params.account;
|
|
72
|
+
}, "isDetermineAccountFromAccountParams");
|
|
73
|
+
var isDetermineAccountFromWalletParams = /* @__PURE__ */ __name((params) => {
|
|
74
|
+
return !!params.wallet;
|
|
75
|
+
}, "isDetermineAccountFromWalletParams");
|
|
76
|
+
async function determineAccount(params, allowRandomAccount = true) {
|
|
77
|
+
if (isDetermineAccountFromAccountParams(params)) {
|
|
78
|
+
if (params.account === "random") {
|
|
79
|
+
assertEx(allowRandomAccount, () => "Random address not allowed");
|
|
80
|
+
return Account.randomSync();
|
|
81
|
+
}
|
|
82
|
+
return params.account;
|
|
83
|
+
}
|
|
84
|
+
if (isDetermineAccountFromWalletParams(params)) {
|
|
85
|
+
return assertEx(params.accountPath ? await params.wallet.derivePath(params.accountPath) : params.wallet, () => "Failed to derive account from path");
|
|
86
|
+
}
|
|
87
|
+
console.warn("AbstractModule.determineAccount: No account or wallet provided - Creating Random account");
|
|
88
|
+
return Account.randomSync();
|
|
89
|
+
}
|
|
90
|
+
__name(determineAccount, "determineAccount");
|
|
91
|
+
|
|
92
|
+
// src/Error.ts
|
|
93
|
+
import { PayloadBuilder } from "@xyo-network/payload-builder";
|
|
94
|
+
import { ModuleErrorSchema } from "@xyo-network/payload-model";
|
|
95
|
+
var ModuleErrorBuilder = class extends PayloadBuilder {
|
|
96
|
+
static {
|
|
97
|
+
__name(this, "ModuleErrorBuilder");
|
|
98
|
+
}
|
|
99
|
+
_message;
|
|
100
|
+
_name;
|
|
101
|
+
_query;
|
|
102
|
+
_sources;
|
|
103
|
+
constructor() {
|
|
104
|
+
super({
|
|
105
|
+
schema: ModuleErrorSchema
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
build() {
|
|
109
|
+
this.fields({
|
|
110
|
+
message: this._message,
|
|
111
|
+
name: this._name,
|
|
112
|
+
query: this._query,
|
|
113
|
+
schema: ModuleErrorSchema,
|
|
114
|
+
sources: this._sources
|
|
115
|
+
});
|
|
116
|
+
return super.build();
|
|
117
|
+
}
|
|
118
|
+
message(message) {
|
|
119
|
+
this._message = message;
|
|
120
|
+
return this;
|
|
121
|
+
}
|
|
122
|
+
name(name) {
|
|
123
|
+
this._name = name;
|
|
124
|
+
return this;
|
|
125
|
+
}
|
|
126
|
+
query(query) {
|
|
127
|
+
this._query = query;
|
|
128
|
+
return this;
|
|
129
|
+
}
|
|
130
|
+
sources(sources) {
|
|
131
|
+
this._sources = sources;
|
|
132
|
+
return this;
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
// src/QueryValidator/ModuleConfigQueryValidator.ts
|
|
137
|
+
import { QueryBoundWitnessWrapper } from "@xyo-network/boundwitness-wrapper";
|
|
138
|
+
var delimiter = "";
|
|
139
|
+
var ModuleConfigQueryValidator = class {
|
|
140
|
+
static {
|
|
141
|
+
__name(this, "ModuleConfigQueryValidator");
|
|
142
|
+
}
|
|
143
|
+
allowed = {};
|
|
144
|
+
disallowed = {};
|
|
145
|
+
hasAllowedRules;
|
|
146
|
+
hasDisallowedRules;
|
|
147
|
+
hasRules;
|
|
148
|
+
constructor(config) {
|
|
149
|
+
if (config?.security?.allowed) {
|
|
150
|
+
for (const [schema, addresses] of Object.entries(config.security?.allowed)) {
|
|
151
|
+
this.allowed[schema] = addresses.map(toAddressesString);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
if (config?.security?.disallowed) {
|
|
155
|
+
for (const [schema, addresses] of Object.entries(config.security?.disallowed)) {
|
|
156
|
+
this.disallowed[schema] = addresses.map(toAddressesString);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
this.hasAllowedRules = Object.keys(this.allowed).length > 0;
|
|
160
|
+
this.hasDisallowedRules = Object.keys(this.disallowed).length > 0;
|
|
161
|
+
this.hasRules = this.hasAllowedRules || this.hasDisallowedRules;
|
|
162
|
+
}
|
|
163
|
+
queryable = async (query, payloads) => {
|
|
164
|
+
if (!this.hasRules)
|
|
165
|
+
return true;
|
|
166
|
+
const addresses = query.addresses;
|
|
167
|
+
if (addresses.length === 0)
|
|
168
|
+
return false;
|
|
169
|
+
const wrapper = await QueryBoundWitnessWrapper.parseQuery(query, payloads);
|
|
170
|
+
const schema = (await wrapper.getQuery()).schema;
|
|
171
|
+
return this.queryAllowed(schema, addresses) && !this.queryDisallowed(schema, addresses);
|
|
172
|
+
};
|
|
173
|
+
queryAllowed = (schema, addresses) => {
|
|
174
|
+
if (!this.hasAllowedRules)
|
|
175
|
+
return true;
|
|
176
|
+
if (addresses.length > 1) {
|
|
177
|
+
const signatories = toAddressesString(addresses);
|
|
178
|
+
const validCosigners = this.allowed?.[schema]?.includes(signatories);
|
|
179
|
+
if (validCosigners)
|
|
180
|
+
return true;
|
|
181
|
+
}
|
|
182
|
+
return addresses.every((address) => this.allowed?.[schema]?.includes(address) || false);
|
|
183
|
+
};
|
|
184
|
+
queryDisallowed = (schema, addresses) => {
|
|
185
|
+
if (!this.hasDisallowedRules)
|
|
186
|
+
return false;
|
|
187
|
+
return addresses.some((address) => this.disallowed?.[schema]?.includes(address));
|
|
188
|
+
};
|
|
189
|
+
};
|
|
190
|
+
var toAddressesString = /* @__PURE__ */ __name((addresses) => {
|
|
191
|
+
return Array.isArray(addresses) ? addresses.sort().map((address) => address.toLowerCase()).join(delimiter) : addresses.toLowerCase();
|
|
192
|
+
}, "toAddressesString");
|
|
193
|
+
|
|
194
|
+
// src/QueryValidator/SupportedQueryValidator.ts
|
|
195
|
+
import { QueryBoundWitnessWrapper as QueryBoundWitnessWrapper2 } from "@xyo-network/boundwitness-wrapper";
|
|
196
|
+
var isQuerySupportedByModule = /* @__PURE__ */ __name(async (mod, query, payloads) => {
|
|
197
|
+
const wrapper = await QueryBoundWitnessWrapper2.parseQuery(query, payloads);
|
|
198
|
+
const schema = (await wrapper.getQuery()).schema;
|
|
199
|
+
return mod.queries.includes(schema);
|
|
200
|
+
}, "isQuerySupportedByModule");
|
|
201
|
+
var SupportedQueryValidator = class {
|
|
202
|
+
static {
|
|
203
|
+
__name(this, "SupportedQueryValidator");
|
|
204
|
+
}
|
|
205
|
+
mod;
|
|
206
|
+
constructor(mod) {
|
|
207
|
+
this.mod = mod;
|
|
208
|
+
this.queryable = (query, payloads) => {
|
|
209
|
+
return isQuerySupportedByModule(this.mod, query, payloads);
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
queryable;
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
// src/AbstractModule.ts
|
|
216
|
+
var AbstractModule = class _AbstractModule extends BaseEmitter {
|
|
217
|
+
static {
|
|
218
|
+
__name(this, "AbstractModule");
|
|
219
|
+
}
|
|
220
|
+
static allowRandomAccount = true;
|
|
221
|
+
static configSchemas = [
|
|
222
|
+
ModuleConfigSchema
|
|
223
|
+
];
|
|
224
|
+
static defaultConfigSchema = ModuleConfigSchema;
|
|
225
|
+
static defaultLogger = new ConsoleLogger(LogLevel.warn);
|
|
226
|
+
static enableLazyLoad = false;
|
|
227
|
+
static labels = {};
|
|
228
|
+
static uniqueName = globallyUnique("AbstractModule", _AbstractModule, "xyo");
|
|
229
|
+
static privateConstructorKey = Date.now().toString();
|
|
230
|
+
_account = void 0;
|
|
231
|
+
_baseModuleQueryAccountPaths = {
|
|
232
|
+
[ModuleAddressQuerySchema]: "1",
|
|
233
|
+
[ModuleManifestQuerySchema]: "5",
|
|
234
|
+
[ModuleStateQuerySchema]: "6",
|
|
235
|
+
[ModuleSubscribeQuerySchema]: "3"
|
|
236
|
+
};
|
|
237
|
+
//cache manifest based on maxDepth
|
|
238
|
+
_cachedManifests = new LRUCache({
|
|
239
|
+
max: 10,
|
|
240
|
+
ttl: 1e3 * 60 * 5
|
|
241
|
+
});
|
|
242
|
+
_lastError;
|
|
243
|
+
_queryAccounts = {
|
|
244
|
+
[ModuleAddressQuerySchema]: void 0,
|
|
245
|
+
[ModuleManifestQuerySchema]: void 0,
|
|
246
|
+
[ModuleStateQuerySchema]: void 0,
|
|
247
|
+
[ModuleSubscribeQuerySchema]: void 0
|
|
248
|
+
};
|
|
249
|
+
_startPromise = void 0;
|
|
250
|
+
_started = void 0;
|
|
251
|
+
moduleConfigQueryValidator;
|
|
252
|
+
supportedQueryValidator;
|
|
253
|
+
_busyCount = 0;
|
|
254
|
+
_status = "stopped";
|
|
255
|
+
constructor(privateConstructorKey, params, account) {
|
|
256
|
+
assertEx2(_AbstractModule.privateConstructorKey === privateConstructorKey, () => "Use create function instead of constructor");
|
|
257
|
+
const mutatedParams = {
|
|
258
|
+
...params
|
|
259
|
+
};
|
|
260
|
+
super(mutatedParams);
|
|
261
|
+
this._account = account;
|
|
262
|
+
this.supportedQueryValidator = new SupportedQueryValidator(this).queryable;
|
|
263
|
+
this.moduleConfigQueryValidator = new ModuleConfigQueryValidator(mutatedParams?.config).queryable;
|
|
264
|
+
}
|
|
265
|
+
get account() {
|
|
266
|
+
return assertEx2(this._account, () => "Missing account");
|
|
267
|
+
}
|
|
268
|
+
get address() {
|
|
269
|
+
return this.account.address;
|
|
270
|
+
}
|
|
271
|
+
get allowAnonymous() {
|
|
272
|
+
return !!this.config.security?.allowAnonymous;
|
|
273
|
+
}
|
|
274
|
+
get allowNameResolution() {
|
|
275
|
+
return this.params.allowNameResolution ?? true;
|
|
276
|
+
}
|
|
277
|
+
get archiving() {
|
|
278
|
+
return this.config.archiving;
|
|
279
|
+
}
|
|
280
|
+
get archivist() {
|
|
281
|
+
return this.config.archivist;
|
|
282
|
+
}
|
|
283
|
+
get config() {
|
|
284
|
+
return this.params.config;
|
|
285
|
+
}
|
|
286
|
+
get dead() {
|
|
287
|
+
return this.status === "dead";
|
|
288
|
+
}
|
|
289
|
+
get ephemeralQueryAccountEnabled() {
|
|
290
|
+
return !!this.params.ephemeralQueryAccountEnabled;
|
|
291
|
+
}
|
|
292
|
+
get id() {
|
|
293
|
+
return this.config.name ?? this.address;
|
|
294
|
+
}
|
|
295
|
+
get logger() {
|
|
296
|
+
return this.params?.logger ?? _AbstractModule.defaultLogger ?? Base2.defaultLogger;
|
|
297
|
+
}
|
|
298
|
+
get priority() {
|
|
299
|
+
return ObjectResolverPriority.Normal;
|
|
300
|
+
}
|
|
301
|
+
get queries() {
|
|
302
|
+
return [
|
|
303
|
+
ModuleAddressQuerySchema,
|
|
304
|
+
ModuleSubscribeQuerySchema,
|
|
305
|
+
ModuleManifestQuerySchema,
|
|
306
|
+
ModuleStateQuerySchema
|
|
307
|
+
];
|
|
308
|
+
}
|
|
309
|
+
get queryAccountPaths() {
|
|
310
|
+
return {
|
|
311
|
+
...this._baseModuleQueryAccountPaths,
|
|
312
|
+
...this._queryAccountPaths
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
get queryAccounts() {
|
|
316
|
+
return this._queryAccounts;
|
|
317
|
+
}
|
|
318
|
+
get status() {
|
|
319
|
+
return this._status;
|
|
320
|
+
}
|
|
321
|
+
get timestamp() {
|
|
322
|
+
return this.config.timestamp ?? false;
|
|
323
|
+
}
|
|
324
|
+
get baseModuleQueryAccountPaths() {
|
|
325
|
+
return this._baseModuleQueryAccountPaths;
|
|
326
|
+
}
|
|
327
|
+
set status(value) {
|
|
328
|
+
if (this._status !== "dead") {
|
|
329
|
+
this._status = value;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
static _getRootFunction(funcName) {
|
|
333
|
+
let anyThis = this;
|
|
334
|
+
while (anyThis.__proto__[funcName]) {
|
|
335
|
+
anyThis = anyThis.__proto__;
|
|
336
|
+
}
|
|
337
|
+
return anyThis[funcName];
|
|
338
|
+
}
|
|
339
|
+
static _noOverride(functionName) {
|
|
340
|
+
const thisFunc = this[functionName];
|
|
341
|
+
const rootFunc = this._getRootFunction(functionName);
|
|
342
|
+
assertEx2(thisFunc === rootFunc, () => `Override not allowed for [${functionName}] - override ${functionName}Handler instead`);
|
|
343
|
+
}
|
|
344
|
+
static async create(params) {
|
|
345
|
+
this._noOverride("create");
|
|
346
|
+
if (!this.configSchemas || this.configSchemas.length === 0) {
|
|
347
|
+
throw new Error(`Missing configSchema [${params?.config?.schema}][${this.name}]`);
|
|
348
|
+
}
|
|
349
|
+
if (!this.defaultConfigSchema) {
|
|
350
|
+
throw new Error(`Missing defaultConfigSchema [${params?.config?.schema}][${this.name}]`);
|
|
351
|
+
}
|
|
352
|
+
assertEx2(params?.config?.name === void 0 || isModuleName(params.config.name), () => `Invalid module name: ${params?.config?.name}`);
|
|
353
|
+
const { account } = params ?? {};
|
|
354
|
+
const schema = params?.config?.schema ?? this.defaultConfigSchema;
|
|
355
|
+
const allowedSchemas = this.configSchemas;
|
|
356
|
+
assertEx2(allowedSchemas.includes(schema), () => `Bad Config Schema [Received ${schema}] [Expected ${JSON.stringify(allowedSchemas)}]`);
|
|
357
|
+
const mutatedConfig = {
|
|
358
|
+
...params?.config,
|
|
359
|
+
schema
|
|
360
|
+
};
|
|
361
|
+
params?.logger?.debug(`config: ${JSON.stringify(mutatedConfig, null, 2)}`);
|
|
362
|
+
const mutatedParams = {
|
|
363
|
+
...params,
|
|
364
|
+
config: mutatedConfig
|
|
365
|
+
};
|
|
366
|
+
const activeLogger = params?.logger ?? _AbstractModule.defaultLogger;
|
|
367
|
+
const generatedAccount = await _AbstractModule.determineAccount({
|
|
368
|
+
account
|
|
369
|
+
});
|
|
370
|
+
const address = generatedAccount.address;
|
|
371
|
+
mutatedParams.logger = activeLogger ? new IdLogger(activeLogger, () => `0x${address}`) : void 0;
|
|
372
|
+
const newModule = new this(_AbstractModule.privateConstructorKey, mutatedParams, generatedAccount);
|
|
373
|
+
if (!_AbstractModule.enableLazyLoad) {
|
|
374
|
+
await newModule.start?.();
|
|
375
|
+
}
|
|
376
|
+
return newModule;
|
|
377
|
+
}
|
|
378
|
+
static async determineAccount(params) {
|
|
379
|
+
return await determineAccount(params, this.allowRandomAccount);
|
|
380
|
+
}
|
|
381
|
+
static factory(params) {
|
|
382
|
+
return ModuleFactory.withParams(this, params);
|
|
383
|
+
}
|
|
384
|
+
_getRootFunction(funcName) {
|
|
385
|
+
let anyThis = this;
|
|
386
|
+
while (anyThis.__proto__[funcName]) {
|
|
387
|
+
anyThis = anyThis.__proto__;
|
|
388
|
+
}
|
|
389
|
+
return anyThis[funcName];
|
|
390
|
+
}
|
|
391
|
+
async busy(closure) {
|
|
392
|
+
if (this._busyCount <= 0) {
|
|
393
|
+
this._busyCount = 0;
|
|
394
|
+
const args = {
|
|
395
|
+
busy: true,
|
|
396
|
+
module: this
|
|
397
|
+
};
|
|
398
|
+
await this.emit("moduleBusy", args);
|
|
399
|
+
}
|
|
400
|
+
this._busyCount++;
|
|
401
|
+
try {
|
|
402
|
+
return await closure();
|
|
403
|
+
} finally {
|
|
404
|
+
this._busyCount--;
|
|
405
|
+
if (this._busyCount <= 0) {
|
|
406
|
+
this._busyCount = 0;
|
|
407
|
+
const args = {
|
|
408
|
+
busy: false,
|
|
409
|
+
module: this
|
|
410
|
+
};
|
|
411
|
+
await this.emit("moduleBusy", args);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
emit(eventName, eventArgs) {
|
|
416
|
+
return super.emit(eventName, eventArgs);
|
|
417
|
+
}
|
|
418
|
+
previousHash() {
|
|
419
|
+
this._checkDead();
|
|
420
|
+
return this.account.previousHash;
|
|
421
|
+
}
|
|
422
|
+
async query(query, payloads, queryConfig) {
|
|
423
|
+
this._checkDead();
|
|
424
|
+
this._noOverride("query");
|
|
425
|
+
const sourceQuery = await PayloadBuilder2.build(assertEx2(QueryBoundWitnessWrapper3.unwrap(query), () => "Invalid query"));
|
|
426
|
+
return await this.busy(async () => {
|
|
427
|
+
const resultPayloads = [];
|
|
428
|
+
const errorPayloads = [];
|
|
429
|
+
const queryAccount = this.ephemeralQueryAccountEnabled ? Account2.randomSync() : void 0;
|
|
430
|
+
try {
|
|
431
|
+
await this.started("throw");
|
|
432
|
+
if (!this.allowAnonymous && query.addresses.length === 0) {
|
|
433
|
+
throw new Error(`Anonymous Queries not allowed, but running anyway [${this.config.name}], [${this.address}]`);
|
|
434
|
+
}
|
|
435
|
+
if (queryConfig?.allowedQueries) {
|
|
436
|
+
assertEx2(queryConfig?.allowedQueries.includes(sourceQuery.schema), () => `Query not allowed [${sourceQuery.schema}]`);
|
|
437
|
+
}
|
|
438
|
+
resultPayloads.push(...await this.queryHandler(sourceQuery, payloads, queryConfig));
|
|
439
|
+
} catch (ex) {
|
|
440
|
+
await handleErrorAsync(ex, async (error) => {
|
|
441
|
+
this._lastError = error;
|
|
442
|
+
errorPayloads.push(await new ModuleErrorBuilder().sources([
|
|
443
|
+
sourceQuery.$hash
|
|
444
|
+
]).name(this.config.name ?? "<Unknown>").query(sourceQuery.schema).message(error.message).build());
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
if (this.timestamp) {
|
|
448
|
+
const timestamp = {
|
|
449
|
+
schema: "network.xyo.timestamp",
|
|
450
|
+
timestamp: Date.now()
|
|
451
|
+
};
|
|
452
|
+
resultPayloads.push(timestamp);
|
|
453
|
+
}
|
|
454
|
+
const result = await this.bindQueryResult(sourceQuery, resultPayloads, queryAccount ? [
|
|
455
|
+
queryAccount
|
|
456
|
+
] : [], errorPayloads);
|
|
457
|
+
const args = {
|
|
458
|
+
module: this,
|
|
459
|
+
payloads,
|
|
460
|
+
query: sourceQuery,
|
|
461
|
+
result
|
|
462
|
+
};
|
|
463
|
+
await this.emit("moduleQueried", args);
|
|
464
|
+
return result;
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
async queryable(query, payloads, queryConfig) {
|
|
468
|
+
if (this.dead) {
|
|
469
|
+
return false;
|
|
470
|
+
}
|
|
471
|
+
if (!await this.started("warn"))
|
|
472
|
+
return false;
|
|
473
|
+
const configValidator = queryConfig ? new ModuleConfigQueryValidator(Object.assign({}, this.config, queryConfig)).queryable : this.moduleConfigQueryValidator;
|
|
474
|
+
const validators = [
|
|
475
|
+
this.supportedQueryValidator,
|
|
476
|
+
configValidator
|
|
477
|
+
];
|
|
478
|
+
return validators.every((validator) => validator(query, payloads));
|
|
479
|
+
}
|
|
480
|
+
start(_timeout) {
|
|
481
|
+
this._startPromise = this._startPromise ?? this.startHandler();
|
|
482
|
+
const result = this._startPromise;
|
|
483
|
+
this.status = result ? "started" : "dead";
|
|
484
|
+
return result;
|
|
485
|
+
}
|
|
486
|
+
async started(notStartedAction = "log", tryStart = true) {
|
|
487
|
+
const started = await this._started;
|
|
488
|
+
if (started === true) {
|
|
489
|
+
return true;
|
|
490
|
+
}
|
|
491
|
+
if (!started) {
|
|
492
|
+
this._started = (async () => {
|
|
493
|
+
if (tryStart) {
|
|
494
|
+
try {
|
|
495
|
+
await this.start();
|
|
496
|
+
return true;
|
|
497
|
+
} catch (ex) {
|
|
498
|
+
handleError(ex, (error) => {
|
|
499
|
+
this.logger?.warn(`Autostart of Module Failed: ${error.message})`);
|
|
500
|
+
this._started = void 0;
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
switch (notStartedAction) {
|
|
505
|
+
case "throw": {
|
|
506
|
+
throw new Error(`Module not Started [${this.address}]`);
|
|
507
|
+
}
|
|
508
|
+
case "warn": {
|
|
509
|
+
this.logger?.warn("Module not started");
|
|
510
|
+
break;
|
|
511
|
+
}
|
|
512
|
+
case "error": {
|
|
513
|
+
this.logger?.error("Module not started");
|
|
514
|
+
break;
|
|
515
|
+
}
|
|
516
|
+
case "none": {
|
|
517
|
+
break;
|
|
518
|
+
}
|
|
519
|
+
default: {
|
|
520
|
+
this.logger?.log("Module not started");
|
|
521
|
+
break;
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
return false;
|
|
525
|
+
})();
|
|
526
|
+
}
|
|
527
|
+
if (!this._started) {
|
|
528
|
+
throw "Failed to create start promise";
|
|
529
|
+
}
|
|
530
|
+
return await this._started;
|
|
531
|
+
}
|
|
532
|
+
async stop(_timeout) {
|
|
533
|
+
return await this.busy(async () => {
|
|
534
|
+
const result = await this.stopHandler();
|
|
535
|
+
this._started = void 0;
|
|
536
|
+
this._startPromise = void 0;
|
|
537
|
+
this.status = result ? "stopped" : "dead";
|
|
538
|
+
return result;
|
|
539
|
+
});
|
|
540
|
+
}
|
|
541
|
+
_checkDead() {
|
|
542
|
+
if (this.dead) {
|
|
543
|
+
throw new DeadModuleError(this.id, this._lastError);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
_noOverride(functionName) {
|
|
547
|
+
const thisFunc = this[functionName];
|
|
548
|
+
const rootFunc = this._getRootFunction(functionName);
|
|
549
|
+
assertEx2(thisFunc === rootFunc, () => `Override not allowed for [${functionName}] - override ${functionName}Handler instead`);
|
|
550
|
+
}
|
|
551
|
+
async archivistInstance(required = false) {
|
|
552
|
+
const archivist = this.archivist;
|
|
553
|
+
if (!archivist) {
|
|
554
|
+
if (required) {
|
|
555
|
+
throw new Error("No archivist specified");
|
|
556
|
+
}
|
|
557
|
+
return void 0;
|
|
558
|
+
}
|
|
559
|
+
const resolved = await this.upResolver.resolve(archivist) ?? await this.downResolver.resolve(archivist);
|
|
560
|
+
if (required) {
|
|
561
|
+
assertEx2(resolved, () => `Unable to resolve archivist [${archivist}]`);
|
|
562
|
+
}
|
|
563
|
+
return resolved ? asArchivistInstance(resolved, () => `Specified archivist is not an Archivist [${archivist}]`) : void 0;
|
|
564
|
+
}
|
|
565
|
+
bindHashes(hashes, schema, account) {
|
|
566
|
+
const promise = new PromiseEx((resolve) => {
|
|
567
|
+
const result = this.bindHashesInternal(hashes, schema, account);
|
|
568
|
+
resolve?.(result);
|
|
569
|
+
return result;
|
|
570
|
+
}, account);
|
|
571
|
+
return promise;
|
|
572
|
+
}
|
|
573
|
+
async bindHashesInternal(hashes, schema, account) {
|
|
574
|
+
const builder = new BoundWitnessBuilder().hashes(hashes, schema).witness(this.account);
|
|
575
|
+
const result = (await (account ? builder.witness(account) : builder).build())[0];
|
|
576
|
+
this.logger?.debug(`result: ${JSON.stringify(result, null, 2)}`);
|
|
577
|
+
return result;
|
|
578
|
+
}
|
|
579
|
+
bindQuery(query, payloads, account) {
|
|
580
|
+
const promise = new PromiseEx(async (resolve) => {
|
|
581
|
+
const result = await this.bindQueryInternal(query, payloads, account);
|
|
582
|
+
resolve?.(result);
|
|
583
|
+
return result;
|
|
584
|
+
}, account);
|
|
585
|
+
return promise;
|
|
586
|
+
}
|
|
587
|
+
async bindQueryInternal(query, payloads, account) {
|
|
588
|
+
const builder = await new QueryBoundWitnessBuilder().payloads(payloads).witness(this.account).query(query);
|
|
589
|
+
const result = await (account ? builder.witness(account) : builder).build();
|
|
590
|
+
return result;
|
|
591
|
+
}
|
|
592
|
+
async bindQueryResult(query, payloads, additionalWitnesses = [], errors) {
|
|
593
|
+
const builder = new BoundWitnessBuilder().payloads(payloads).errors(errors).sourceQuery(query.$hash);
|
|
594
|
+
const queryWitnessAccount = this.queryAccounts[query.schema];
|
|
595
|
+
const witnesses = [
|
|
596
|
+
this.account,
|
|
597
|
+
queryWitnessAccount,
|
|
598
|
+
...additionalWitnesses
|
|
599
|
+
].filter(exists);
|
|
600
|
+
builder.witnesses(witnesses);
|
|
601
|
+
const result = [
|
|
602
|
+
(await builder.build())[0],
|
|
603
|
+
await Promise.all(payloads.map((payload) => PayloadBuilder2.build(payload))),
|
|
604
|
+
await Promise.all((errors ?? [])?.map((error) => PayloadBuilder2.build(error)))
|
|
605
|
+
];
|
|
606
|
+
if (this.archiving && this.isAllowedArchivingQuery(query.schema)) {
|
|
607
|
+
await this.storeToArchivists(result.flat());
|
|
608
|
+
}
|
|
609
|
+
return result;
|
|
610
|
+
}
|
|
611
|
+
async generateConfigAndAddress(_maxDepth) {
|
|
612
|
+
const config = await PayloadBuilder2.build(this.config);
|
|
613
|
+
const address = await new PayloadBuilder2({
|
|
614
|
+
schema: AddressSchema
|
|
615
|
+
}).fields({
|
|
616
|
+
address: this.address,
|
|
617
|
+
name: this.config?.name
|
|
618
|
+
}).build();
|
|
619
|
+
const queries = await Promise.all(this.queries.map(async (query) => {
|
|
620
|
+
return await new PayloadBuilder2({
|
|
621
|
+
schema: QuerySchema
|
|
622
|
+
}).fields({
|
|
623
|
+
query
|
|
624
|
+
}).build();
|
|
625
|
+
}));
|
|
626
|
+
const configSchema = await PayloadBuilder2.build({
|
|
627
|
+
config: config.schema,
|
|
628
|
+
schema: ConfigSchema
|
|
629
|
+
});
|
|
630
|
+
return compact([
|
|
631
|
+
config,
|
|
632
|
+
configSchema,
|
|
633
|
+
address,
|
|
634
|
+
...queries
|
|
635
|
+
]);
|
|
636
|
+
}
|
|
637
|
+
async generateDescribe() {
|
|
638
|
+
const description = {
|
|
639
|
+
address: this.address,
|
|
640
|
+
queries: this.queries,
|
|
641
|
+
schema: ModuleDescriptionSchema
|
|
642
|
+
};
|
|
643
|
+
if (this.config?.name) {
|
|
644
|
+
description.name = this.config.name;
|
|
645
|
+
}
|
|
646
|
+
const discover = await this.generateConfigAndAddress();
|
|
647
|
+
description.children = compact(discover?.map((payload) => {
|
|
648
|
+
const address = payload.schema === AddressSchema ? payload.address : void 0;
|
|
649
|
+
return address == this.address ? void 0 : address;
|
|
650
|
+
}) ?? []);
|
|
651
|
+
return description;
|
|
652
|
+
}
|
|
653
|
+
/** @deprecated use archivistInstance() instead */
|
|
654
|
+
async getArchivist() {
|
|
655
|
+
return await this.archivistInstance();
|
|
656
|
+
}
|
|
657
|
+
async initializeQueryAccounts() {
|
|
658
|
+
const paths = Object.values(this.queryAccountPaths).filter(exists);
|
|
659
|
+
const distinctPaths = new Set(paths);
|
|
660
|
+
assertEx2(distinctPaths.size === paths.length, () => `${this.config?.name ? this.config.name + ": " : ""}Duplicate query account paths`);
|
|
661
|
+
const wallet = this.account;
|
|
662
|
+
if (wallet?.derivePath) {
|
|
663
|
+
for (const key in this.queryAccountPaths) {
|
|
664
|
+
if (Object.prototype.hasOwnProperty.call(this.queryAccountPaths, key)) {
|
|
665
|
+
const query = key;
|
|
666
|
+
const queryAccountPath = this.queryAccountPaths[query];
|
|
667
|
+
if (queryAccountPath) {
|
|
668
|
+
this._queryAccounts[query] = await wallet.derivePath?.(queryAccountPath);
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
manifestHandler(_maxDepth = 1, _ignoreAddresses = []) {
|
|
675
|
+
throw new Error("Not supported");
|
|
676
|
+
}
|
|
677
|
+
moduleAddressHandler() {
|
|
678
|
+
const queryAccounts = Object.entries(this.queryAccounts).filter((value) => {
|
|
679
|
+
return exists(value[1]);
|
|
680
|
+
}).map(([name2, account]) => {
|
|
681
|
+
const address2 = account.address;
|
|
682
|
+
const previousHash2 = account.previousHash;
|
|
683
|
+
return [
|
|
684
|
+
{
|
|
685
|
+
address: address2,
|
|
686
|
+
name: name2,
|
|
687
|
+
schema: AddressSchema
|
|
688
|
+
},
|
|
689
|
+
{
|
|
690
|
+
address: address2,
|
|
691
|
+
previousHash: previousHash2,
|
|
692
|
+
schema: AddressPreviousHashSchema
|
|
693
|
+
}
|
|
694
|
+
];
|
|
695
|
+
});
|
|
696
|
+
const address = this.address;
|
|
697
|
+
const name = this.config.name;
|
|
698
|
+
const previousHash = this.address;
|
|
699
|
+
const moduleAccount = name ? {
|
|
700
|
+
address,
|
|
701
|
+
name,
|
|
702
|
+
schema: AddressSchema
|
|
703
|
+
} : {
|
|
704
|
+
address,
|
|
705
|
+
schema: AddressSchema
|
|
706
|
+
};
|
|
707
|
+
const moduleAccountPreviousHash = previousHash ? {
|
|
708
|
+
address,
|
|
709
|
+
previousHash,
|
|
710
|
+
schema: AddressPreviousHashSchema
|
|
711
|
+
} : {
|
|
712
|
+
address,
|
|
713
|
+
schema: AddressPreviousHashSchema
|
|
714
|
+
};
|
|
715
|
+
return [
|
|
716
|
+
moduleAccount,
|
|
717
|
+
moduleAccountPreviousHash,
|
|
718
|
+
...queryAccounts
|
|
719
|
+
].flat();
|
|
720
|
+
}
|
|
721
|
+
async queryHandler(query, payloads, queryConfig) {
|
|
722
|
+
await this.started("throw");
|
|
723
|
+
const wrapper = await QueryBoundWitnessWrapper3.parseQuery(query, payloads);
|
|
724
|
+
const queryPayload = await wrapper.getQuery();
|
|
725
|
+
assertEx2(await this.queryable(query, payloads, queryConfig));
|
|
726
|
+
const resultPayloads = [];
|
|
727
|
+
switch (queryPayload.schema) {
|
|
728
|
+
case ModuleManifestQuerySchema: {
|
|
729
|
+
resultPayloads.push(await this.manifestHandler(queryPayload.maxDepth));
|
|
730
|
+
break;
|
|
731
|
+
}
|
|
732
|
+
case ModuleAddressQuerySchema: {
|
|
733
|
+
resultPayloads.push(...await this.moduleAddressHandler());
|
|
734
|
+
break;
|
|
735
|
+
}
|
|
736
|
+
case ModuleStateQuerySchema: {
|
|
737
|
+
resultPayloads.push(...await this.stateHandler());
|
|
738
|
+
break;
|
|
739
|
+
}
|
|
740
|
+
case ModuleSubscribeQuerySchema: {
|
|
741
|
+
this.subscribeHandler();
|
|
742
|
+
break;
|
|
743
|
+
}
|
|
744
|
+
default: {
|
|
745
|
+
throw new Error(`Unsupported Query [${queryPayload.schema}]`);
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
return resultPayloads;
|
|
749
|
+
}
|
|
750
|
+
async startHandler() {
|
|
751
|
+
this.validateConfig();
|
|
752
|
+
await this.initializeQueryAccounts();
|
|
753
|
+
this._started = true;
|
|
754
|
+
return true;
|
|
755
|
+
}
|
|
756
|
+
async stateHandler() {
|
|
757
|
+
return [
|
|
758
|
+
await this.manifestHandler(),
|
|
759
|
+
...await this.generateConfigAndAddress(),
|
|
760
|
+
await this.generateDescribe()
|
|
761
|
+
];
|
|
762
|
+
}
|
|
763
|
+
stopHandler(_timeout) {
|
|
764
|
+
this._started = void 0;
|
|
765
|
+
return true;
|
|
766
|
+
}
|
|
767
|
+
subscribeHandler() {
|
|
768
|
+
return;
|
|
769
|
+
}
|
|
770
|
+
validateConfig(config, parents = []) {
|
|
771
|
+
return Object.entries(config ?? this.config ?? {}).reduce((valid, [key, value]) => {
|
|
772
|
+
switch (typeof value) {
|
|
773
|
+
case "function": {
|
|
774
|
+
this.logger?.warn(`Fields of type function not allowed in config [${parents?.join(".")}.${key}]`);
|
|
775
|
+
return false;
|
|
776
|
+
}
|
|
777
|
+
case "object": {
|
|
778
|
+
if (Array.isArray(value)) {
|
|
779
|
+
return (
|
|
780
|
+
// eslint-disable-next-line unicorn/no-array-reduce
|
|
781
|
+
value.reduce((valid2, value2) => {
|
|
782
|
+
return this.validateConfig(value2, [
|
|
783
|
+
...parents,
|
|
784
|
+
key
|
|
785
|
+
]) && valid2;
|
|
786
|
+
}, true) && valid
|
|
787
|
+
);
|
|
788
|
+
}
|
|
789
|
+
if (!serializableField(value)) {
|
|
790
|
+
this.logger?.warn(`Fields that are not serializable to JSON are not allowed in config [${parents?.join(".")}.${key}]`);
|
|
791
|
+
return false;
|
|
792
|
+
}
|
|
793
|
+
return value ? this.validateConfig(value, [
|
|
794
|
+
...parents,
|
|
795
|
+
key
|
|
796
|
+
]) && valid : true;
|
|
797
|
+
}
|
|
798
|
+
default: {
|
|
799
|
+
return valid;
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
}, true);
|
|
803
|
+
}
|
|
804
|
+
isAllowedArchivingQuery(schema) {
|
|
805
|
+
const queries = this.archiving?.queries;
|
|
806
|
+
if (queries) {
|
|
807
|
+
return queries.includes(schema);
|
|
808
|
+
}
|
|
809
|
+
return true;
|
|
810
|
+
}
|
|
811
|
+
};
|
|
812
|
+
|
|
813
|
+
// src/AbstractModuleInstance.ts
|
|
814
|
+
import { assertEx as assertEx3 } from "@xylabs/assert";
|
|
815
|
+
import { compact as compact2 } from "@xylabs/lodash";
|
|
816
|
+
import { globallyUnique as globallyUnique2 } from "@xylabs/object";
|
|
817
|
+
import { asArchivistInstance as asArchivistInstance2 } from "@xyo-network/archivist-model";
|
|
818
|
+
import { ModuleManifestPayloadSchema } from "@xyo-network/manifest-model";
|
|
819
|
+
import { duplicateModules, ModuleManifestQuerySchema as ModuleManifestQuerySchema2, ModuleStateQuerySchema as ModuleStateQuerySchema2, ResolveHelper } from "@xyo-network/module-model";
|
|
820
|
+
import { CompositeModuleResolver } from "@xyo-network/module-resolver";
|
|
821
|
+
var AbstractModuleInstance = class _AbstractModuleInstance extends AbstractModule {
|
|
822
|
+
static {
|
|
823
|
+
__name(this, "AbstractModuleInstance");
|
|
824
|
+
}
|
|
825
|
+
static uniqueName = globallyUnique2("AbstractModuleInstance", _AbstractModuleInstance, "xyo");
|
|
826
|
+
_downResolver;
|
|
827
|
+
_parents = [];
|
|
828
|
+
_privateResolver;
|
|
829
|
+
_upResolver;
|
|
830
|
+
constructor(privateConstructorKey, params, account) {
|
|
831
|
+
assertEx3(AbstractModule.privateConstructorKey === privateConstructorKey, () => "Use create function instead of constructor");
|
|
832
|
+
const mutatedParams = {
|
|
833
|
+
...params
|
|
834
|
+
};
|
|
835
|
+
const addToResolvers = mutatedParams.addToResolvers ?? true;
|
|
836
|
+
super(privateConstructorKey, mutatedParams, account);
|
|
837
|
+
if (addToResolvers) {
|
|
838
|
+
this.upResolver.add(this);
|
|
839
|
+
this.downResolver.add(this);
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
get downResolver() {
|
|
843
|
+
this._downResolver = this._downResolver ?? new CompositeModuleResolver({
|
|
844
|
+
allowNameResolution: this.allowNameResolution,
|
|
845
|
+
moduleIdentifierTransformers: this.params.moduleIdentifierTransformers,
|
|
846
|
+
root: this
|
|
847
|
+
});
|
|
848
|
+
return this._downResolver;
|
|
849
|
+
}
|
|
850
|
+
get localName() {
|
|
851
|
+
return this.config.name;
|
|
852
|
+
}
|
|
853
|
+
get moduleIdentifierTransformers() {
|
|
854
|
+
return this.params.moduleIdentifierTransformers ?? ResolveHelper.transformers;
|
|
855
|
+
}
|
|
856
|
+
get privateResolver() {
|
|
857
|
+
this._privateResolver = this._privateResolver ?? new CompositeModuleResolver({
|
|
858
|
+
allowNameResolution: this.allowNameResolution,
|
|
859
|
+
moduleIdentifierTransformers: this.params.moduleIdentifierTransformers,
|
|
860
|
+
root: this
|
|
861
|
+
});
|
|
862
|
+
return this._privateResolver;
|
|
863
|
+
}
|
|
864
|
+
get root() {
|
|
865
|
+
return this;
|
|
866
|
+
}
|
|
867
|
+
get upResolver() {
|
|
868
|
+
this._upResolver = this._upResolver ?? new CompositeModuleResolver({
|
|
869
|
+
allowNameResolution: this.allowNameResolution,
|
|
870
|
+
moduleIdentifierTransformers: this.params.moduleIdentifierTransformers,
|
|
871
|
+
root: this
|
|
872
|
+
});
|
|
873
|
+
return this._upResolver;
|
|
874
|
+
}
|
|
875
|
+
addParent(module) {
|
|
876
|
+
const existingEntry = this._parents.find((parent) => parent.address === module.address);
|
|
877
|
+
if (!existingEntry) {
|
|
878
|
+
this._parents.push(module);
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
manifest(maxDepth) {
|
|
882
|
+
this._checkDead();
|
|
883
|
+
return this.busy(async () => {
|
|
884
|
+
return await this.manifestHandler(maxDepth);
|
|
885
|
+
});
|
|
886
|
+
}
|
|
887
|
+
async manifestQuery(account, maxDepth) {
|
|
888
|
+
const queryPayload = {
|
|
889
|
+
schema: ModuleManifestQuerySchema2,
|
|
890
|
+
...maxDepth === void 0 ? {} : {
|
|
891
|
+
maxDepth
|
|
892
|
+
}
|
|
893
|
+
};
|
|
894
|
+
return await this.sendQueryRaw(queryPayload, void 0, account);
|
|
895
|
+
}
|
|
896
|
+
moduleAddress() {
|
|
897
|
+
this._checkDead();
|
|
898
|
+
return this.busy(async () => {
|
|
899
|
+
return await this.moduleAddressHandler();
|
|
900
|
+
});
|
|
901
|
+
}
|
|
902
|
+
parents() {
|
|
903
|
+
return this._parents;
|
|
904
|
+
}
|
|
905
|
+
privateChildren() {
|
|
906
|
+
return [];
|
|
907
|
+
}
|
|
908
|
+
publicChildren() {
|
|
909
|
+
return [];
|
|
910
|
+
}
|
|
911
|
+
removeParent(address) {
|
|
912
|
+
this._parents = this._parents.filter((item) => item.address !== address);
|
|
913
|
+
}
|
|
914
|
+
async resolve(idOrFilter = "*", options = {}) {
|
|
915
|
+
const config = {
|
|
916
|
+
address: this.address,
|
|
917
|
+
dead: this.dead,
|
|
918
|
+
downResolver: this.downResolver,
|
|
919
|
+
logger: this.logger,
|
|
920
|
+
module: this,
|
|
921
|
+
transformers: this.moduleIdentifierTransformers,
|
|
922
|
+
upResolver: this.upResolver
|
|
923
|
+
};
|
|
924
|
+
if (idOrFilter === "*") {
|
|
925
|
+
return (await ResolveHelper.resolve(config, idOrFilter, options)).filter((mod) => mod.address !== this.address);
|
|
926
|
+
}
|
|
927
|
+
switch (typeof idOrFilter) {
|
|
928
|
+
case "string": {
|
|
929
|
+
return await ResolveHelper.resolve(config, idOrFilter, options);
|
|
930
|
+
}
|
|
931
|
+
case "object": {
|
|
932
|
+
return (await ResolveHelper.resolve(config, idOrFilter, options)).filter((mod) => mod.address !== this.address);
|
|
933
|
+
}
|
|
934
|
+
default: {
|
|
935
|
+
return (await ResolveHelper.resolve(config, idOrFilter, options)).filter((mod) => mod.address !== this.address);
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
resolveIdentifier(id, options) {
|
|
940
|
+
const { direction = "all" } = options ?? {};
|
|
941
|
+
switch (direction) {
|
|
942
|
+
case "down": {
|
|
943
|
+
return this.downResolver.resolveIdentifier(id, options);
|
|
944
|
+
}
|
|
945
|
+
default: {
|
|
946
|
+
const mutatedOptions = {
|
|
947
|
+
...options,
|
|
948
|
+
direction: "all"
|
|
949
|
+
};
|
|
950
|
+
return this.upResolver.resolveIdentifier(id, mutatedOptions);
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
async resolvePrivate(id = "*", options = {}) {
|
|
955
|
+
return await this.privateResolver.resolve(id, options) ?? await this.upResolver.resolve(id, options) ?? await this.downResolver.resolve(id, options);
|
|
956
|
+
}
|
|
957
|
+
async siblings() {
|
|
958
|
+
return (await Promise.all((await this.parents()).map((parent) => parent.publicChildren()))).flat().filter(duplicateModules);
|
|
959
|
+
}
|
|
960
|
+
state() {
|
|
961
|
+
this._checkDead();
|
|
962
|
+
return this.busy(async () => {
|
|
963
|
+
return await this.stateHandler();
|
|
964
|
+
});
|
|
965
|
+
}
|
|
966
|
+
async stateQuery(account) {
|
|
967
|
+
const queryPayload = {
|
|
968
|
+
schema: ModuleStateQuerySchema2
|
|
969
|
+
};
|
|
970
|
+
return await this.sendQueryRaw(queryPayload, void 0, account);
|
|
971
|
+
}
|
|
972
|
+
subscribe(_queryAccount) {
|
|
973
|
+
this._checkDead();
|
|
974
|
+
return this.subscribeHandler();
|
|
975
|
+
}
|
|
976
|
+
async manifestHandler(maxDepth = 1, _ignoreAddresses = []) {
|
|
977
|
+
const cachedResult = this._cachedManifests.get(maxDepth);
|
|
978
|
+
if (cachedResult) {
|
|
979
|
+
return cachedResult;
|
|
980
|
+
}
|
|
981
|
+
const name = this.config.name ?? "Anonymous";
|
|
982
|
+
const children = await this.publicChildren();
|
|
983
|
+
const childAddressToName = {};
|
|
984
|
+
for (const child of children) {
|
|
985
|
+
if (child.address !== this.address) {
|
|
986
|
+
childAddressToName[child.address] = child.config.name ?? null;
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
const result = {
|
|
990
|
+
config: {
|
|
991
|
+
name,
|
|
992
|
+
...this.config
|
|
993
|
+
},
|
|
994
|
+
schema: ModuleManifestPayloadSchema,
|
|
995
|
+
status: {
|
|
996
|
+
address: this.address,
|
|
997
|
+
children: childAddressToName
|
|
998
|
+
}
|
|
999
|
+
};
|
|
1000
|
+
this._cachedManifests.set(maxDepth, result);
|
|
1001
|
+
return result;
|
|
1002
|
+
}
|
|
1003
|
+
async resolveArchivingArchivists() {
|
|
1004
|
+
const archivists = this.config.archiving?.archivists;
|
|
1005
|
+
if (!archivists)
|
|
1006
|
+
return [];
|
|
1007
|
+
const resolved = await Promise.all(archivists.map((archivist) => this.resolve(archivist)));
|
|
1008
|
+
return compact2(resolved.map((mod) => asArchivistInstance2(mod)));
|
|
1009
|
+
}
|
|
1010
|
+
async sendQuery(queryPayload, payloads, account) {
|
|
1011
|
+
const queryResults = await this.sendQueryRaw(queryPayload, payloads, account);
|
|
1012
|
+
const [, resultPayloads, errors] = queryResults;
|
|
1013
|
+
if (errors && errors.length > 0) {
|
|
1014
|
+
throw errors[0];
|
|
1015
|
+
}
|
|
1016
|
+
return resultPayloads;
|
|
1017
|
+
}
|
|
1018
|
+
async sendQueryRaw(queryPayload, payloads, account) {
|
|
1019
|
+
const query = await this.bindQuery(queryPayload, payloads, account);
|
|
1020
|
+
return await this.query(query[0], query[1]);
|
|
1021
|
+
}
|
|
1022
|
+
async storeToArchivists(payloads) {
|
|
1023
|
+
const archivists = await this.resolveArchivingArchivists();
|
|
1024
|
+
return (await Promise.all(archivists.map((archivist) => {
|
|
1025
|
+
return archivist.insert?.(payloads);
|
|
1026
|
+
}))).map(([bw]) => bw);
|
|
1027
|
+
}
|
|
1028
|
+
};
|
|
1029
|
+
export {
|
|
1030
|
+
AbstractModule,
|
|
1031
|
+
AbstractModuleInstance,
|
|
1032
|
+
BaseEmitter,
|
|
1033
|
+
ModuleConfigQueryValidator,
|
|
1034
|
+
ModuleErrorBuilder,
|
|
1035
|
+
SupportedQueryValidator,
|
|
1036
|
+
isQuerySupportedByModule
|
|
1037
|
+
};
|
|
1038
|
+
//# sourceMappingURL=index.js.map
|