@xyo-network/module-abstract 2.92.6 → 2.92.8
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/browser/AbstractModule.d.cts +19 -7
- package/dist/browser/AbstractModule.d.cts.map +1 -1
- package/dist/browser/AbstractModule.d.mts +19 -7
- package/dist/browser/AbstractModule.d.mts.map +1 -1
- package/dist/browser/AbstractModule.d.ts +19 -7
- package/dist/browser/AbstractModule.d.ts.map +1 -1
- package/dist/browser/AbstractModuleInstance.d.cts +2 -1
- package/dist/browser/AbstractModuleInstance.d.cts.map +1 -1
- package/dist/browser/AbstractModuleInstance.d.mts +2 -1
- package/dist/browser/AbstractModuleInstance.d.mts.map +1 -1
- package/dist/browser/AbstractModuleInstance.d.ts +2 -1
- package/dist/browser/AbstractModuleInstance.d.ts.map +1 -1
- package/dist/browser/determineAccount.d.cts.map +1 -1
- package/dist/browser/determineAccount.d.mts.map +1 -1
- package/dist/browser/determineAccount.d.ts.map +1 -1
- package/dist/browser/index.cjs +94 -38
- package/dist/browser/index.cjs.map +1 -1
- package/dist/browser/index.js +95 -39
- package/dist/browser/index.js.map +1 -1
- package/dist/node/AbstractModule.d.cts +19 -7
- package/dist/node/AbstractModule.d.cts.map +1 -1
- package/dist/node/AbstractModule.d.mts +19 -7
- package/dist/node/AbstractModule.d.mts.map +1 -1
- package/dist/node/AbstractModule.d.ts +19 -7
- package/dist/node/AbstractModule.d.ts.map +1 -1
- package/dist/node/AbstractModuleInstance.d.cts +2 -1
- package/dist/node/AbstractModuleInstance.d.cts.map +1 -1
- package/dist/node/AbstractModuleInstance.d.mts +2 -1
- package/dist/node/AbstractModuleInstance.d.mts.map +1 -1
- package/dist/node/AbstractModuleInstance.d.ts +2 -1
- package/dist/node/AbstractModuleInstance.d.ts.map +1 -1
- package/dist/node/determineAccount.d.cts.map +1 -1
- package/dist/node/determineAccount.d.mts.map +1 -1
- package/dist/node/determineAccount.d.ts.map +1 -1
- package/dist/node/index.cjs +101 -42
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.js +102 -43
- package/dist/node/index.js.map +1 -1
- package/package.json +25 -25
- package/src/AbstractModule.ts +111 -44
- package/src/AbstractModuleInstance.ts +9 -2
- package/src/determineAccount.ts +6 -3
package/dist/browser/index.js
CHANGED
|
@@ -16,7 +16,7 @@ import { BoundWitnessBuilder, QueryBoundWitnessBuilder } from "@xyo-network/boun
|
|
|
16
16
|
import { QueryBoundWitnessWrapper as QueryBoundWitnessWrapper3 } from "@xyo-network/boundwitness-wrapper";
|
|
17
17
|
import { ConfigSchema } from "@xyo-network/config-payload-plugin";
|
|
18
18
|
import { ModuleManifestPayloadSchema } from "@xyo-network/manifest-model";
|
|
19
|
-
import { AddressPreviousHashSchema, duplicateModules, isModuleName, ModuleAddressQuerySchema, ModuleDescribeQuerySchema, ModuleDescriptionSchema, ModuleDiscoverQuerySchema, ModuleFactory, ModuleManifestQuerySchema, ModuleStateQuerySchema, ModuleSubscribeQuerySchema, serializableField } from "@xyo-network/module-model";
|
|
19
|
+
import { AddressPreviousHashSchema, DeadModuleError, duplicateModules, isModuleName, ModuleAddressQuerySchema, ModuleDescribeQuerySchema, ModuleDescriptionSchema, ModuleDiscoverQuerySchema, ModuleFactory, ModuleManifestQuerySchema, ModuleStateQuerySchema, ModuleSubscribeQuerySchema, serializableField } from "@xyo-network/module-model";
|
|
20
20
|
import { CompositeModuleResolver } from "@xyo-network/module-resolver";
|
|
21
21
|
import { PayloadBuilder as PayloadBuilder2 } from "@xyo-network/payload-builder";
|
|
22
22
|
import { QuerySchema } from "@xyo-network/query-payload-plugin";
|
|
@@ -68,7 +68,7 @@ var BaseEmitter = class extends Base {
|
|
|
68
68
|
import { assertEx } from "@xylabs/assert";
|
|
69
69
|
import { Account } from "@xyo-network/account";
|
|
70
70
|
var isDetermineAccountFromAccountParams = /* @__PURE__ */ __name((params) => {
|
|
71
|
-
assertEx(!params.accountPath, "accountPath may not be provided when account is provided");
|
|
71
|
+
assertEx(!params.accountPath, () => "accountPath may not be provided when account is provided");
|
|
72
72
|
return !!params.account;
|
|
73
73
|
}, "isDetermineAccountFromAccountParams");
|
|
74
74
|
var isDetermineAccountFromWalletParams = /* @__PURE__ */ __name((params) => {
|
|
@@ -77,13 +77,13 @@ var isDetermineAccountFromWalletParams = /* @__PURE__ */ __name((params) => {
|
|
|
77
77
|
async function determineAccount(params, allowRandomAccount = true) {
|
|
78
78
|
if (isDetermineAccountFromAccountParams(params)) {
|
|
79
79
|
if (params.account === "random") {
|
|
80
|
-
assertEx(allowRandomAccount, "Random address not allowed");
|
|
80
|
+
assertEx(allowRandomAccount, () => "Random address not allowed");
|
|
81
81
|
return Account.randomSync();
|
|
82
82
|
}
|
|
83
83
|
return params.account;
|
|
84
84
|
}
|
|
85
85
|
if (isDetermineAccountFromWalletParams(params)) {
|
|
86
|
-
return assertEx(params.accountPath ? await params.wallet.derivePath(params.accountPath) : params.wallet, "Failed to derive account from path");
|
|
86
|
+
return assertEx(params.accountPath ? await params.wallet.derivePath(params.accountPath) : params.wallet, () => "Failed to derive account from path");
|
|
87
87
|
}
|
|
88
88
|
console.warn("AbstractModule.determineAccount: No account or wallet provided - Creating Random account");
|
|
89
89
|
return Account.randomSync();
|
|
@@ -234,6 +234,7 @@ var AbstractModule = class _AbstractModule extends BaseEmitter {
|
|
|
234
234
|
[ModuleStateQuerySchema]: "6",
|
|
235
235
|
[ModuleSubscribeQuerySchema]: "3"
|
|
236
236
|
};
|
|
237
|
+
_lastError;
|
|
237
238
|
_queryAccounts = {
|
|
238
239
|
[ModuleAddressQuerySchema]: void 0,
|
|
239
240
|
[ModuleDescribeQuerySchema]: void 0,
|
|
@@ -247,8 +248,9 @@ var AbstractModule = class _AbstractModule extends BaseEmitter {
|
|
|
247
248
|
moduleConfigQueryValidator;
|
|
248
249
|
supportedQueryValidator;
|
|
249
250
|
_busyCount = 0;
|
|
251
|
+
_status = "stopped";
|
|
250
252
|
constructor(privateConstructorKey, params, account) {
|
|
251
|
-
assertEx2(_AbstractModule.privateConstructorKey === privateConstructorKey, "Use create function instead of constructor");
|
|
253
|
+
assertEx2(_AbstractModule.privateConstructorKey === privateConstructorKey, () => "Use create function instead of constructor");
|
|
252
254
|
const mutatedParams = {
|
|
253
255
|
...params
|
|
254
256
|
};
|
|
@@ -261,7 +263,7 @@ var AbstractModule = class _AbstractModule extends BaseEmitter {
|
|
|
261
263
|
return this.configSchemas[0];
|
|
262
264
|
}
|
|
263
265
|
get account() {
|
|
264
|
-
return assertEx2(this._account, "Missing account");
|
|
266
|
+
return assertEx2(this._account, () => "Missing account");
|
|
265
267
|
}
|
|
266
268
|
get address() {
|
|
267
269
|
return this.account.address;
|
|
@@ -272,6 +274,9 @@ var AbstractModule = class _AbstractModule extends BaseEmitter {
|
|
|
272
274
|
get config() {
|
|
273
275
|
return this.params.config;
|
|
274
276
|
}
|
|
277
|
+
get dead() {
|
|
278
|
+
return this.status === "dead";
|
|
279
|
+
}
|
|
275
280
|
get ephemeralQueryAccountEnabled() {
|
|
276
281
|
return !!this.params.ephemeralQueryAccountEnabled;
|
|
277
282
|
}
|
|
@@ -297,6 +302,9 @@ var AbstractModule = class _AbstractModule extends BaseEmitter {
|
|
|
297
302
|
get queryAccounts() {
|
|
298
303
|
return this._queryAccounts;
|
|
299
304
|
}
|
|
305
|
+
get status() {
|
|
306
|
+
return this._status;
|
|
307
|
+
}
|
|
300
308
|
get timestamp() {
|
|
301
309
|
return this.config.timestamp ?? false;
|
|
302
310
|
}
|
|
@@ -306,6 +314,11 @@ var AbstractModule = class _AbstractModule extends BaseEmitter {
|
|
|
306
314
|
get logger() {
|
|
307
315
|
return this.params?.logger ?? _AbstractModule.defaultLogger ?? Base2.defaultLogger;
|
|
308
316
|
}
|
|
317
|
+
set status(value) {
|
|
318
|
+
if (this._status !== "dead") {
|
|
319
|
+
this._status = value;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
309
322
|
static _getRootFunction(funcName) {
|
|
310
323
|
let anyThis = this;
|
|
311
324
|
while (anyThis.__proto__[funcName]) {
|
|
@@ -316,14 +329,14 @@ var AbstractModule = class _AbstractModule extends BaseEmitter {
|
|
|
316
329
|
static _noOverride(functionName) {
|
|
317
330
|
const thisFunc = this[functionName];
|
|
318
331
|
const rootFunc = this._getRootFunction(functionName);
|
|
319
|
-
assertEx2(thisFunc === rootFunc, `Override not allowed for [${functionName}] - override ${functionName}Handler instead`);
|
|
332
|
+
assertEx2(thisFunc === rootFunc, () => `Override not allowed for [${functionName}] - override ${functionName}Handler instead`);
|
|
320
333
|
}
|
|
321
334
|
static async create(params) {
|
|
322
335
|
this._noOverride("create");
|
|
323
336
|
if (!this.configSchemas || this.configSchemas.length === 0) {
|
|
324
337
|
throw new Error(`Missing configSchema [${params?.config?.schema}][${this.name}]`);
|
|
325
338
|
}
|
|
326
|
-
assertEx2(params?.config?.name === void 0 || isModuleName(params.config.name), `Invalid module name: ${params?.config?.name}`);
|
|
339
|
+
assertEx2(params?.config?.name === void 0 || isModuleName(params.config.name), () => `Invalid module name: ${params?.config?.name}`);
|
|
327
340
|
const { account } = params ?? {};
|
|
328
341
|
const schema = params?.config?.schema ?? this.configSchema;
|
|
329
342
|
const allowedSchemas = this.configSchemas;
|
|
@@ -362,11 +375,6 @@ var AbstractModule = class _AbstractModule extends BaseEmitter {
|
|
|
362
375
|
}
|
|
363
376
|
return anyThis[funcName];
|
|
364
377
|
}
|
|
365
|
-
_noOverride(functionName) {
|
|
366
|
-
const thisFunc = this[functionName];
|
|
367
|
-
const rootFunc = this._getRootFunction(functionName);
|
|
368
|
-
assertEx2(thisFunc === rootFunc, () => `Override not allowed for [${functionName}] - override ${functionName}Handler instead`);
|
|
369
|
-
}
|
|
370
378
|
async busy(closure) {
|
|
371
379
|
if (this._busyCount <= 0) {
|
|
372
380
|
this._busyCount = 0;
|
|
@@ -395,11 +403,13 @@ var AbstractModule = class _AbstractModule extends BaseEmitter {
|
|
|
395
403
|
return super.emit(eventName, eventArgs);
|
|
396
404
|
}
|
|
397
405
|
previousHash() {
|
|
406
|
+
this._checkDead();
|
|
398
407
|
return this.account.previousHash;
|
|
399
408
|
}
|
|
400
409
|
async query(query, payloads, queryConfig) {
|
|
410
|
+
this._checkDead();
|
|
401
411
|
this._noOverride("query");
|
|
402
|
-
const sourceQuery = await PayloadBuilder2.build(assertEx2(QueryBoundWitnessWrapper3.unwrap(query), "Invalid query"));
|
|
412
|
+
const sourceQuery = await PayloadBuilder2.build(assertEx2(QueryBoundWitnessWrapper3.unwrap(query), () => "Invalid query"));
|
|
403
413
|
return await this.busy(async () => {
|
|
404
414
|
const resultPayloads = [];
|
|
405
415
|
const errorPayloads = [];
|
|
@@ -409,9 +419,14 @@ var AbstractModule = class _AbstractModule extends BaseEmitter {
|
|
|
409
419
|
if (!this.allowAnonymous && query.addresses.length === 0) {
|
|
410
420
|
throw new Error(`Anonymous Queries not allowed, but running anyway [${this.config.name}], [${this.address}]`);
|
|
411
421
|
}
|
|
422
|
+
if (queryConfig?.allowedQueries) {
|
|
423
|
+
assertEx2(queryConfig?.allowedQueries.includes(sourceQuery.schema), () => `Query not allowed [${sourceQuery.schema}]`);
|
|
424
|
+
}
|
|
412
425
|
resultPayloads.push(...await this.queryHandler(sourceQuery, payloads, queryConfig));
|
|
413
426
|
} catch (ex) {
|
|
414
427
|
await handleErrorAsync(ex, async (error) => {
|
|
428
|
+
this._lastError = error;
|
|
429
|
+
this.status = "dead";
|
|
415
430
|
errorPayloads.push(await new ModuleErrorBuilder().sources([
|
|
416
431
|
sourceQuery.$hash
|
|
417
432
|
]).name(this.config.name ?? "<Unknown>").query(sourceQuery.schema).message(error.message).build());
|
|
@@ -438,6 +453,9 @@ var AbstractModule = class _AbstractModule extends BaseEmitter {
|
|
|
438
453
|
});
|
|
439
454
|
}
|
|
440
455
|
async queryable(query, payloads, queryConfig) {
|
|
456
|
+
if (this.dead) {
|
|
457
|
+
return false;
|
|
458
|
+
}
|
|
441
459
|
if (!await this.started("warn"))
|
|
442
460
|
return false;
|
|
443
461
|
const configValidator = queryConfig ? new ModuleConfigQueryValidator(Object.assign({}, this.config, queryConfig)).queryable : this.moduleConfigQueryValidator;
|
|
@@ -447,7 +465,7 @@ var AbstractModule = class _AbstractModule extends BaseEmitter {
|
|
|
447
465
|
];
|
|
448
466
|
return validators.every((validator) => validator(query, payloads));
|
|
449
467
|
}
|
|
450
|
-
async resolve(idOrFilter, { required = "log", ...options } = {}) {
|
|
468
|
+
async resolve(idOrFilter = "*", { required = "log", ...options } = {}) {
|
|
451
469
|
const childOptions = {
|
|
452
470
|
...options,
|
|
453
471
|
required: false
|
|
@@ -456,41 +474,44 @@ var AbstractModule = class _AbstractModule extends BaseEmitter {
|
|
|
456
474
|
const up = direction === "up" || direction === "all";
|
|
457
475
|
const down = direction === "down" || direction === "all";
|
|
458
476
|
let result;
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
break;
|
|
477
|
+
if (idOrFilter === "*") {
|
|
478
|
+
if (this.dead) {
|
|
479
|
+
return [];
|
|
463
480
|
}
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
case "warn": {
|
|
476
|
-
this.logger.warn("resolve failed", idOrFilter);
|
|
477
|
-
break;
|
|
478
|
-
}
|
|
479
|
-
case "log": {
|
|
480
|
-
this.logger.log("resolve failed", idOrFilter);
|
|
481
|
+
return [
|
|
482
|
+
...down ? await this.downResolver.resolve("*", childOptions) : [],
|
|
483
|
+
...up ? await this.upResolver.resolve("*", childOptions) : []
|
|
484
|
+
].filter(duplicateModules);
|
|
485
|
+
} else {
|
|
486
|
+
switch (typeof idOrFilter) {
|
|
487
|
+
case "string": {
|
|
488
|
+
if (this.dead) {
|
|
489
|
+
return void 0;
|
|
490
|
+
}
|
|
491
|
+
result = (down ? await this.downResolver.resolve(idOrFilter, childOptions) : void 0) ?? (up ? await this.upResolver.resolve(idOrFilter, childOptions) : void 0);
|
|
481
492
|
break;
|
|
482
493
|
}
|
|
483
494
|
default: {
|
|
484
|
-
this.
|
|
495
|
+
if (this.dead) {
|
|
496
|
+
return [];
|
|
497
|
+
}
|
|
498
|
+
const filter = idOrFilter;
|
|
499
|
+
result = [
|
|
500
|
+
...down ? await this.downResolver.resolve(filter, childOptions) : [],
|
|
501
|
+
...up ? await this.upResolver.resolve(filter, childOptions) : []
|
|
502
|
+
].filter(duplicateModules);
|
|
485
503
|
break;
|
|
486
504
|
}
|
|
487
505
|
}
|
|
488
506
|
}
|
|
507
|
+
this.validateRequiredResolve(required, result, idOrFilter);
|
|
489
508
|
return result;
|
|
490
509
|
}
|
|
491
510
|
start(_timeout) {
|
|
492
511
|
this._startPromise = this._startPromise ?? this.startHandler();
|
|
493
|
-
|
|
512
|
+
const result = this._startPromise;
|
|
513
|
+
this.status = result ? "started" : "dead";
|
|
514
|
+
return result;
|
|
494
515
|
}
|
|
495
516
|
async started(notStartedAction = "log", tryStart = true) {
|
|
496
517
|
const started = await this._started;
|
|
@@ -543,9 +564,20 @@ var AbstractModule = class _AbstractModule extends BaseEmitter {
|
|
|
543
564
|
const result = await this.stopHandler();
|
|
544
565
|
this._started = void 0;
|
|
545
566
|
this._startPromise = void 0;
|
|
567
|
+
this.status = result ? "stopped" : "dead";
|
|
546
568
|
return result;
|
|
547
569
|
});
|
|
548
570
|
}
|
|
571
|
+
_checkDead() {
|
|
572
|
+
if (this.dead) {
|
|
573
|
+
throw new DeadModuleError(this.id, this._lastError);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
_noOverride(functionName) {
|
|
577
|
+
const thisFunc = this[functionName];
|
|
578
|
+
const rootFunc = this._getRootFunction(functionName);
|
|
579
|
+
assertEx2(thisFunc === rootFunc, () => `Override not allowed for [${functionName}] - override ${functionName}Handler instead`);
|
|
580
|
+
}
|
|
549
581
|
bindHashes(hashes, schema, account) {
|
|
550
582
|
const promise = new PromiseEx((resolve) => {
|
|
551
583
|
const result = this.bindHashesInternal(hashes, schema, account);
|
|
@@ -819,6 +851,24 @@ var AbstractModule = class _AbstractModule extends BaseEmitter {
|
|
|
819
851
|
}
|
|
820
852
|
}, true);
|
|
821
853
|
}
|
|
854
|
+
validateRequiredResolve(required, result, idOrFilter) {
|
|
855
|
+
if (required && (result === void 0 || Array.isArray(result) && result.length > 0)) {
|
|
856
|
+
switch (required) {
|
|
857
|
+
case "warn": {
|
|
858
|
+
this.logger.warn("resolve failed", idOrFilter);
|
|
859
|
+
break;
|
|
860
|
+
}
|
|
861
|
+
case "log": {
|
|
862
|
+
this.logger.log("resolve failed", idOrFilter);
|
|
863
|
+
break;
|
|
864
|
+
}
|
|
865
|
+
default: {
|
|
866
|
+
this.logger.error("resolve failed", idOrFilter);
|
|
867
|
+
break;
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
}
|
|
822
872
|
};
|
|
823
873
|
|
|
824
874
|
// src/AbstractModuleInstance.ts
|
|
@@ -828,7 +878,7 @@ var AbstractModuleInstance = class extends AbstractModule {
|
|
|
828
878
|
__name(this, "AbstractModuleInstance");
|
|
829
879
|
}
|
|
830
880
|
constructor(privateConstructorKey, params, account) {
|
|
831
|
-
assertEx3(AbstractModule.privateConstructorKey === privateConstructorKey, "Use create function instead of constructor");
|
|
881
|
+
assertEx3(AbstractModule.privateConstructorKey === privateConstructorKey, () => "Use create function instead of constructor");
|
|
832
882
|
const mutatedParams = {
|
|
833
883
|
...params
|
|
834
884
|
};
|
|
@@ -837,31 +887,37 @@ var AbstractModuleInstance = class extends AbstractModule {
|
|
|
837
887
|
this.downResolver.add(this);
|
|
838
888
|
}
|
|
839
889
|
describe() {
|
|
890
|
+
this._checkDead();
|
|
840
891
|
return this.busy(async () => {
|
|
841
892
|
return await this.describeHandler();
|
|
842
893
|
});
|
|
843
894
|
}
|
|
844
895
|
discover() {
|
|
896
|
+
this._checkDead();
|
|
845
897
|
return this.busy(async () => {
|
|
846
898
|
return await this.discoverHandler();
|
|
847
899
|
});
|
|
848
900
|
}
|
|
849
901
|
manifest(maxDepth, ignoreAddresses) {
|
|
902
|
+
this._checkDead();
|
|
850
903
|
return this.busy(async () => {
|
|
851
904
|
return await this.manifestHandler(maxDepth, ignoreAddresses);
|
|
852
905
|
});
|
|
853
906
|
}
|
|
854
907
|
moduleAddress() {
|
|
908
|
+
this._checkDead();
|
|
855
909
|
return this.busy(async () => {
|
|
856
910
|
return await this.moduleAddressHandler();
|
|
857
911
|
});
|
|
858
912
|
}
|
|
859
913
|
state() {
|
|
914
|
+
this._checkDead();
|
|
860
915
|
return this.busy(async () => {
|
|
861
916
|
return await this.stateHandler();
|
|
862
917
|
});
|
|
863
918
|
}
|
|
864
919
|
subscribe(_queryAccount) {
|
|
920
|
+
this._checkDead();
|
|
865
921
|
return this.subscribeHandler();
|
|
866
922
|
}
|
|
867
923
|
};
|