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