@xyo-network/sentinel 2.84.2 → 2.84.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/node/{index.mjs → index.cjs} +69 -38
- package/dist/node/index.cjs.map +1 -0
- package/dist/node/index.js +37 -68
- package/dist/node/index.js.map +1 -1
- package/package.json +25 -27
- package/dist/node/index.mjs.map +0 -1
|
@@ -1,20 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/index.ts
|
|
22
|
+
var src_exports = {};
|
|
23
|
+
__export(src_exports, {
|
|
24
|
+
MemorySentinel: () => MemorySentinel,
|
|
25
|
+
SentinelAutomationSchema: () => SentinelAutomationSchema,
|
|
26
|
+
SentinelEventAutomationSchema: () => SentinelEventAutomationSchema,
|
|
27
|
+
SentinelIntervalAutomationSchema: () => SentinelIntervalAutomationSchema,
|
|
28
|
+
SentinelIntervalAutomationWrapper: () => SentinelIntervalAutomationWrapper,
|
|
29
|
+
SentinelRunner: () => SentinelRunner,
|
|
30
|
+
SentinelWrapper: () => SentinelWrapper,
|
|
31
|
+
isSentinelIntervalAutomation: () => isSentinelIntervalAutomation
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(src_exports);
|
|
34
|
+
|
|
1
35
|
// src/Automation.ts
|
|
2
|
-
|
|
36
|
+
var import_payload_model = require("@xyo-network/payload-model");
|
|
3
37
|
var SentinelAutomationSchema = "network.xyo.automation";
|
|
4
38
|
var SentinelIntervalAutomationSchema = "network.xyo.automation.interval";
|
|
5
39
|
var SentinelEventAutomationSchema = "network.xyo.automation.event";
|
|
6
|
-
var isSentinelIntervalAutomation = isPayloadOfSchemaType(SentinelIntervalAutomationSchema);
|
|
40
|
+
var isSentinelIntervalAutomation = (0, import_payload_model.isPayloadOfSchemaType)(SentinelIntervalAutomationSchema);
|
|
7
41
|
|
|
8
42
|
// src/MemorySentinel.ts
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
var MemorySentinel = class extends AbstractSentinel {
|
|
17
|
-
static configSchemas = [SentinelConfigSchema];
|
|
43
|
+
var import_promise = require("@xylabs/promise");
|
|
44
|
+
var import_diviner_model = require("@xyo-network/diviner-model");
|
|
45
|
+
var import_sentinel_abstract = require("@xyo-network/sentinel-abstract");
|
|
46
|
+
var import_sentinel_model = require("@xyo-network/sentinel-model");
|
|
47
|
+
var import_witness_model = require("@xyo-network/witness-model");
|
|
48
|
+
var MemorySentinel = class extends import_sentinel_abstract.AbstractSentinel {
|
|
49
|
+
static configSchemas = [import_sentinel_model.SentinelConfigSchema];
|
|
18
50
|
async reportHandler(inPayloads = []) {
|
|
19
51
|
await this.started("throw");
|
|
20
52
|
const job = await this.jobPromise;
|
|
@@ -30,13 +62,13 @@ var MemorySentinel = class extends AbstractSentinel {
|
|
|
30
62
|
async generateResults(tasks, previousResults, inPayloads) {
|
|
31
63
|
const results = await Promise.allSettled(
|
|
32
64
|
tasks == null ? void 0 : tasks.map(async (task) => {
|
|
33
|
-
const witness = asWitnessInstance(task.module);
|
|
65
|
+
const witness = (0, import_witness_model.asWitnessInstance)(task.module);
|
|
34
66
|
const input = task.input ?? false;
|
|
35
67
|
const inPayloadsFound = input === true ? inPayloads : input === false ? [] : this.processPreviousResults(previousResults, await this.inputAddresses(input));
|
|
36
68
|
if (witness) {
|
|
37
69
|
return [witness.address, await witness.observe(inPayloadsFound)];
|
|
38
70
|
}
|
|
39
|
-
const diviner = asDivinerInstance(task.module);
|
|
71
|
+
const diviner = (0, import_diviner_model.asDivinerInstance)(task.module);
|
|
40
72
|
if (diviner) {
|
|
41
73
|
return [diviner.address, await diviner.divine(inPayloadsFound)];
|
|
42
74
|
}
|
|
@@ -44,7 +76,7 @@ var MemorySentinel = class extends AbstractSentinel {
|
|
|
44
76
|
})
|
|
45
77
|
);
|
|
46
78
|
const finalResult = {};
|
|
47
|
-
results.filter(fulfilled).forEach((result) => {
|
|
79
|
+
results.filter(import_promise.fulfilled).forEach((result) => {
|
|
48
80
|
const [address, payloads] = result.value;
|
|
49
81
|
finalResult[address] = finalResult[address] ?? [];
|
|
50
82
|
finalResult[address].push(...payloads);
|
|
@@ -65,8 +97,8 @@ var MemorySentinel = class extends AbstractSentinel {
|
|
|
65
97
|
};
|
|
66
98
|
|
|
67
99
|
// src/SentinelIntervalAutomationWrapper.ts
|
|
68
|
-
|
|
69
|
-
var SentinelIntervalAutomationWrapper = class extends PayloadWrapper {
|
|
100
|
+
var import_payload_wrapper = require("@xyo-network/payload-wrapper");
|
|
101
|
+
var SentinelIntervalAutomationWrapper = class extends import_payload_wrapper.PayloadWrapper {
|
|
70
102
|
constructor(payload) {
|
|
71
103
|
super(payload);
|
|
72
104
|
}
|
|
@@ -109,8 +141,8 @@ var SentinelIntervalAutomationWrapper = class extends PayloadWrapper {
|
|
|
109
141
|
};
|
|
110
142
|
|
|
111
143
|
// src/SentinelRunner.ts
|
|
112
|
-
|
|
113
|
-
|
|
144
|
+
var import_assert = require("@xylabs/assert");
|
|
145
|
+
var import_payload_wrapper2 = require("@xyo-network/payload-wrapper");
|
|
114
146
|
var SentinelRunner = class {
|
|
115
147
|
_automations = {};
|
|
116
148
|
onTriggerResult;
|
|
@@ -135,7 +167,7 @@ var SentinelRunner = class {
|
|
|
135
167
|
}, void 0);
|
|
136
168
|
}
|
|
137
169
|
async add(automation, restart = true) {
|
|
138
|
-
const hash = await
|
|
170
|
+
const hash = await import_payload_wrapper2.PayloadWrapper.hashAsync(automation);
|
|
139
171
|
this._automations[hash] = automation;
|
|
140
172
|
if (restart)
|
|
141
173
|
await this.restart();
|
|
@@ -158,7 +190,7 @@ var SentinelRunner = class {
|
|
|
158
190
|
await this.start();
|
|
159
191
|
}
|
|
160
192
|
async start() {
|
|
161
|
-
assertEx(this.timeoutId === void 0, "Already started");
|
|
193
|
+
(0, import_assert.assertEx)(this.timeoutId === void 0, "Already started");
|
|
162
194
|
const automation = this.next;
|
|
163
195
|
if (isSentinelIntervalAutomation(automation)) {
|
|
164
196
|
const delay = automation.start - Date.now();
|
|
@@ -200,22 +232,18 @@ var SentinelRunner = class {
|
|
|
200
232
|
};
|
|
201
233
|
|
|
202
234
|
// src/Wrapper.ts
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
var SentinelWrapper = class extends ModuleWrapper {
|
|
211
|
-
static instanceIdentityCheck = isSentinelInstance;
|
|
212
|
-
static moduleIdentityCheck = isSentinelModule;
|
|
213
|
-
static requiredQueries = [SentinelReportQuerySchema, ...super.requiredQueries];
|
|
235
|
+
var import_module_wrapper = require("@xyo-network/module-wrapper");
|
|
236
|
+
var import_sentinel_model2 = require("@xyo-network/sentinel-model");
|
|
237
|
+
(0, import_module_wrapper.constructableModuleWrapper)();
|
|
238
|
+
var SentinelWrapper = class extends import_module_wrapper.ModuleWrapper {
|
|
239
|
+
static instanceIdentityCheck = import_sentinel_model2.isSentinelInstance;
|
|
240
|
+
static moduleIdentityCheck = import_sentinel_model2.isSentinelModule;
|
|
241
|
+
static requiredQueries = [import_sentinel_model2.SentinelReportQuerySchema, ...super.requiredQueries];
|
|
214
242
|
archivists() {
|
|
215
243
|
throw Error("Not supported");
|
|
216
244
|
}
|
|
217
245
|
async report(payloads) {
|
|
218
|
-
const queryPayload = { schema: SentinelReportQuerySchema };
|
|
246
|
+
const queryPayload = { schema: import_sentinel_model2.SentinelReportQuerySchema };
|
|
219
247
|
const result = await this.sendQuery(queryPayload, payloads);
|
|
220
248
|
return result;
|
|
221
249
|
}
|
|
@@ -225,9 +253,10 @@ var SentinelWrapper = class extends ModuleWrapper {
|
|
|
225
253
|
};
|
|
226
254
|
|
|
227
255
|
// src/index.ts
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
export
|
|
256
|
+
__reExport(src_exports, require("@xyo-network/sentinel-abstract"), module.exports);
|
|
257
|
+
__reExport(src_exports, require("@xyo-network/sentinel-model"), module.exports);
|
|
258
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
259
|
+
0 && (module.exports = {
|
|
231
260
|
MemorySentinel,
|
|
232
261
|
SentinelAutomationSchema,
|
|
233
262
|
SentinelEventAutomationSchema,
|
|
@@ -235,6 +264,8 @@ export {
|
|
|
235
264
|
SentinelIntervalAutomationWrapper,
|
|
236
265
|
SentinelRunner,
|
|
237
266
|
SentinelWrapper,
|
|
238
|
-
isSentinelIntervalAutomation
|
|
239
|
-
|
|
240
|
-
|
|
267
|
+
isSentinelIntervalAutomation,
|
|
268
|
+
...require("@xyo-network/sentinel-abstract"),
|
|
269
|
+
...require("@xyo-network/sentinel-model")
|
|
270
|
+
});
|
|
271
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts","../../src/Automation.ts","../../src/MemorySentinel.ts","../../src/SentinelIntervalAutomationWrapper.ts","../../src/SentinelRunner.ts","../../src/Wrapper.ts"],"sourcesContent":["export * from './Automation'\nexport * from './MemorySentinel'\nexport * from './SentinelIntervalAutomationWrapper'\nexport * from './SentinelRunner'\nexport * from './Wrapper'\nexport * from '@xyo-network/sentinel-abstract'\nexport * from '@xyo-network/sentinel-model'\n","import { isPayloadOfSchemaType, Payload } from '@xyo-network/payload-model'\nimport { Task } from '@xyo-network/sentinel-model'\n\nexport type SentinelAutomationSchema = 'network.xyo.automation'\nexport const SentinelAutomationSchema: SentinelAutomationSchema = 'network.xyo.automation'\n\nexport type SentinelIntervalAutomationSchema = 'network.xyo.automation.interval'\nexport const SentinelIntervalAutomationSchema: SentinelIntervalAutomationSchema = 'network.xyo.automation.interval'\n\nexport type SentinelEventAutomationSchema = 'network.xyo.automation.event'\nexport const SentinelEventAutomationSchema: SentinelEventAutomationSchema = 'network.xyo.automation.event'\n\nexport type SentinelBaseAutomationPayload<T extends Payload> = Payload<\n {\n tasks?: Task[]\n type?: 'interval' | 'event'\n } & T\n>\n\n/** Settings for an Interval Automation */\nexport type SentinelIntervalAutomationPayload = SentinelBaseAutomationPayload<{\n /** Epoch after which any reoccurrence stops */\n end?: number\n\n /** Time between triggers [non-repeating if undefined] */\n frequency?: number\n\n /** Units for frequency field [hour if undefined] */\n frequencyUnits?: 'second' | 'minute' | 'hour' | 'day'\n\n /** Remaining triggers [infinite if undefined] */\n remaining?: number\n\n schema: SentinelIntervalAutomationSchema\n\n /** Epoch of the next trigger */\n start: number\n\n /** The type of automation */\n type: 'interval'\n}>\n\nexport const isSentinelIntervalAutomation = isPayloadOfSchemaType<SentinelIntervalAutomationPayload>(SentinelIntervalAutomationSchema)\n\n/** Settings for an Event Automation */\nexport type SentinelEventAutomationPayload = SentinelBaseAutomationPayload<{\n schema: SentinelEventAutomationSchema\n type: 'event'\n}>\n\n/** Settings for an Automation */\nexport type SentinelAutomationPayload = Payload<\n SentinelIntervalAutomationPayload | SentinelEventAutomationPayload,\n SentinelAutomationSchema | SentinelIntervalAutomationSchema | SentinelEventAutomationSchema\n>\n","import { Address } from '@xylabs/hex'\nimport { fulfilled } from '@xylabs/promise'\nimport { asDivinerInstance } from '@xyo-network/diviner-model'\nimport { AnyConfigSchema } from '@xyo-network/module-model'\nimport { Payload } from '@xyo-network/payload-model'\nimport { AbstractSentinel } from '@xyo-network/sentinel-abstract'\nimport {\n ResolvedTask,\n SentinelConfig,\n SentinelConfigSchema,\n SentinelInstance,\n SentinelModuleEventData,\n SentinelParams,\n} from '@xyo-network/sentinel-model'\nimport { asWitnessInstance } from '@xyo-network/witness-model'\n\nexport type MemorySentinelParams<TConfig extends AnyConfigSchema<SentinelConfig> = AnyConfigSchema<SentinelConfig>> = SentinelParams<TConfig>\n\nexport class MemorySentinel<\n TParams extends MemorySentinelParams = MemorySentinelParams,\n TEventData extends SentinelModuleEventData<SentinelInstance<TParams>> = SentinelModuleEventData<SentinelInstance<TParams>>,\n> extends AbstractSentinel<TParams, TEventData> {\n static override configSchemas = [SentinelConfigSchema]\n\n async reportHandler(inPayloads: Payload[] = []): Promise<Payload[]> {\n await this.started('throw')\n const job = await this.jobPromise\n\n let index = 0\n let previousResults: Record<Address, Payload[]> = {}\n while (index < job.tasks.length) {\n const generatedPayloads = await this.generateResults(job.tasks[index], previousResults, inPayloads)\n previousResults = generatedPayloads\n index++\n }\n return Object.values(previousResults).flat()\n }\n\n private async generateResults(\n tasks: ResolvedTask[],\n previousResults: Record<Address, Payload[]>,\n inPayloads?: Payload[],\n ): Promise<Record<Address, Payload[]>> {\n const results: PromiseSettledResult<[Address, Payload[]]>[] = await Promise.allSettled(\n tasks?.map(async (task) => {\n const witness = asWitnessInstance(task.module)\n const input = task.input ?? false\n const inPayloadsFound =\n input === true ? inPayloads : input === false ? [] : this.processPreviousResults(previousResults, await this.inputAddresses(input))\n if (witness) {\n return [witness.address, await witness.observe(inPayloadsFound)]\n }\n const diviner = asDivinerInstance(task.module)\n if (diviner) {\n return [diviner.address, await diviner.divine(inPayloadsFound)]\n }\n throw Error('Unsupported module type')\n }),\n )\n const finalResult: Record<Address, Payload[]> = {}\n results.filter(fulfilled).forEach((result) => {\n const [address, payloads] = result.value\n finalResult[address] = finalResult[address] ?? []\n finalResult[address].push(...payloads)\n })\n /*const errors = results.filter(rejected).map((result) => result.reason)\n if (errors.length > 0) {\n throw Error('At least one module failed')\n }*/\n return finalResult\n }\n\n private async inputAddresses(input: string | string[]): Promise<string[]> {\n if (Array.isArray(input)) {\n return (await Promise.all(input.map(async (inputItem) => await this.inputAddresses(inputItem)))).flat()\n } else {\n const resolved = await this.resolve(input)\n return resolved ? [resolved.address] : []\n }\n }\n\n private processPreviousResults(payloads: Record<string, Payload[]>, inputs: string[]) {\n return inputs.map((input) => payloads[input] ?? []).flat()\n }\n}\n","import { PayloadWrapper } from '@xyo-network/payload-wrapper'\n\nimport { SentinelIntervalAutomationPayload } from './Automation'\n\nexport class SentinelIntervalAutomationWrapper<\n T extends SentinelIntervalAutomationPayload = SentinelIntervalAutomationPayload,\n> extends PayloadWrapper<T> {\n constructor(payload: T) {\n super(payload)\n }\n\n protected get frequencyMillis() {\n const frequency = this.jsonPayload().frequency\n if (frequency === undefined) return Infinity\n switch (this.jsonPayload().frequencyUnits ?? 'hour') {\n case 'second':\n return frequency * 1000\n case 'minute':\n return frequency * 60 * 1000\n case 'hour':\n return frequency * 60 * 60 * 1000\n case 'day':\n return frequency * 24 * 60 * 60 * 1000\n }\n }\n\n protected get remaining() {\n //if remaining is not defined, we assume Infinity\n return this.payload().remaining ?? Infinity\n }\n\n next() {\n this.jsonPayload().start = this.jsonPayload().start + this.frequencyMillis\n this.consumeRemaining()\n this.checkEnd()\n return this\n }\n\n protected checkEnd() {\n if (this.jsonPayload().start > (this.jsonPayload().end ?? Infinity)) {\n this.jsonPayload().start = Infinity\n }\n }\n\n protected consumeRemaining(count = 1) {\n const remaining = this.remaining - count\n this.jsonPayload().remaining = remaining\n\n if (remaining <= 0) {\n this.jsonPayload().start = Infinity\n }\n }\n}\n","import { assertEx } from '@xylabs/assert'\nimport { Payload } from '@xyo-network/payload-model'\nimport { PayloadWrapper } from '@xyo-network/payload-wrapper'\nimport { SentinelInstance } from '@xyo-network/sentinel-model'\n\nimport { isSentinelIntervalAutomation, SentinelAutomationPayload, SentinelIntervalAutomationPayload } from './Automation'\nimport { SentinelIntervalAutomationWrapper } from './SentinelIntervalAutomationWrapper'\n\nexport type OnSentinelRunnerTriggerResult = (result: Payload[]) => void\n\nexport class SentinelRunner {\n protected _automations: Record<string, SentinelAutomationPayload> = {}\n protected onTriggerResult: OnSentinelRunnerTriggerResult | undefined\n protected sentinel: SentinelInstance\n protected timeoutId?: NodeJS.Timeout | string | number\n\n constructor(sentinel: SentinelInstance, automations?: SentinelAutomationPayload[], onTriggerResult?: OnSentinelRunnerTriggerResult) {\n this.sentinel = sentinel\n this.onTriggerResult = onTriggerResult\n automations?.forEach((automation) => this.add(automation))\n }\n\n get automations() {\n return this._automations\n }\n\n private get next() {\n return Object.values(this._automations).reduce<SentinelAutomationPayload | undefined>((previous, current) => {\n if (isSentinelIntervalAutomation(current)) {\n if (isSentinelIntervalAutomation(previous)) {\n return current.start < (previous?.start ?? Infinity) ? current : previous\n }\n }\n return current\n }, undefined)\n }\n\n async add(automation: SentinelAutomationPayload, restart = true) {\n const hash = await PayloadWrapper.hashAsync(automation)\n this._automations[hash] = automation\n if (restart) await this.restart()\n return hash\n }\n\n find(hash: string) {\n Object.entries(this._automations).find(([key]) => key === hash)\n }\n\n async remove(hash: string, restart = true) {\n delete this._automations[hash]\n if (restart) await this.restart()\n }\n\n removeAll() {\n this.stop()\n this._automations = {}\n }\n\n async restart() {\n this.stop()\n await this.start()\n }\n\n async start() {\n assertEx(this.timeoutId === undefined, 'Already started')\n const automation = this.next\n if (isSentinelIntervalAutomation(automation)) {\n const delay = automation.start - Date.now()\n if (delay < 0) {\n //automation is due, just do it\n await this.trigger(automation)\n } else {\n this.timeoutId = setTimeout(\n async () => {\n this.timeoutId = undefined\n await this.start()\n },\n delay > 0 ? delay : 0,\n )\n }\n }\n }\n\n stop() {\n if (this.timeoutId) {\n clearTimeout(this.timeoutId)\n this.timeoutId = undefined\n }\n }\n\n async update(hash: string, automation: SentinelAutomationPayload, restart = true) {\n await this.remove(hash, false)\n await this.add(automation, false)\n if (restart) await this.restart()\n }\n\n private async trigger(automation: SentinelIntervalAutomationPayload) {\n const wrapper = new SentinelIntervalAutomationWrapper(automation)\n await this.remove(await wrapper.hashAsync(), false)\n wrapper.next()\n await this.add(wrapper.jsonPayload(), false)\n const triggerResult = await this.sentinel.report()\n this.onTriggerResult?.(triggerResult)\n await this.start()\n }\n}\n","import { ArchivistInstance } from '@xyo-network/archivist'\nimport { constructableModuleWrapper, ModuleWrapper } from '@xyo-network/module-wrapper'\nimport { Payload } from '@xyo-network/payload-model'\nimport {\n isSentinelInstance,\n isSentinelModule,\n SentinelInstance,\n SentinelModule,\n SentinelReportQuery,\n SentinelReportQuerySchema,\n} from '@xyo-network/sentinel-model'\nimport { WitnessInstance } from '@xyo-network/witness-model'\n\nconstructableModuleWrapper()\nexport class SentinelWrapper<TModule extends SentinelModule = SentinelModule>\n extends ModuleWrapper<TModule>\n implements SentinelInstance<TModule['params']>\n{\n static override instanceIdentityCheck = isSentinelInstance\n static override moduleIdentityCheck = isSentinelModule\n static override requiredQueries = [SentinelReportQuerySchema, ...super.requiredQueries]\n\n archivists(): Promise<ArchivistInstance[]> {\n throw Error('Not supported')\n }\n\n async report(payloads?: Payload[]): Promise<Payload[]> {\n const queryPayload: SentinelReportQuery = { schema: SentinelReportQuerySchema }\n const result = await this.sendQuery(queryPayload, payloads)\n return result\n }\n\n witnesses(): Promise<WitnessInstance[]> {\n throw Error('Not supported')\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAA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UAAM,KAAK,IAAI,QAAQ,YAAY,GAAG,KAAK;AAC3C,UAAM,gBAAgB,MAAM,KAAK,SAAS,OAAO;AACjD,eAAK,oBAAL,8BAAuB;AACvB,UAAM,KAAK,MAAM;AAAA,EACnB;AACF;;;ACxGA,4BAA0D;AAE1D,IAAAC,yBAOO;AAAA,IAGP,kDAA2B;AACpB,IAAM,kBAAN,cACG,oCAEV;AAAA,EACE,OAAgB,wBAAwB;AAAA,EACxC,OAAgB,sBAAsB;AAAA,EACtC,OAAgB,kBAAkB,CAAC,kDAA2B,GAAG,MAAM,eAAe;AAAA,EAEtF,aAA2C;AACzC,UAAM,MAAM,eAAe;AAAA,EAC7B;AAAA,EAEA,MAAM,OAAO,UAA0C;AACrD,UAAM,eAAoC,EAAE,QAAQ,iDAA0B;AAC9E,UAAM,SAAS,MAAM,KAAK,UAAU,cAAc,QAAQ;AAC1D,WAAO;AAAA,EACT;AAAA,EAEA,YAAwC;AACtC,UAAM,MAAM,eAAe;AAAA,EAC7B;AACF;;;AL9BA,wBAAc,2CALd;AAMA,wBAAc,wCANd;","names":["import_payload_wrapper","import_sentinel_model"]}
|
package/dist/node/index.js
CHANGED
|
@@ -1,52 +1,20 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
19
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
-
|
|
21
|
-
// src/index.ts
|
|
22
|
-
var src_exports = {};
|
|
23
|
-
__export(src_exports, {
|
|
24
|
-
MemorySentinel: () => MemorySentinel,
|
|
25
|
-
SentinelAutomationSchema: () => SentinelAutomationSchema,
|
|
26
|
-
SentinelEventAutomationSchema: () => SentinelEventAutomationSchema,
|
|
27
|
-
SentinelIntervalAutomationSchema: () => SentinelIntervalAutomationSchema,
|
|
28
|
-
SentinelIntervalAutomationWrapper: () => SentinelIntervalAutomationWrapper,
|
|
29
|
-
SentinelRunner: () => SentinelRunner,
|
|
30
|
-
SentinelWrapper: () => SentinelWrapper,
|
|
31
|
-
isSentinelIntervalAutomation: () => isSentinelIntervalAutomation
|
|
32
|
-
});
|
|
33
|
-
module.exports = __toCommonJS(src_exports);
|
|
34
|
-
|
|
35
1
|
// src/Automation.ts
|
|
36
|
-
|
|
2
|
+
import { isPayloadOfSchemaType } from "@xyo-network/payload-model";
|
|
37
3
|
var SentinelAutomationSchema = "network.xyo.automation";
|
|
38
4
|
var SentinelIntervalAutomationSchema = "network.xyo.automation.interval";
|
|
39
5
|
var SentinelEventAutomationSchema = "network.xyo.automation.event";
|
|
40
|
-
var isSentinelIntervalAutomation =
|
|
6
|
+
var isSentinelIntervalAutomation = isPayloadOfSchemaType(SentinelIntervalAutomationSchema);
|
|
41
7
|
|
|
42
8
|
// src/MemorySentinel.ts
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
9
|
+
import { fulfilled } from "@xylabs/promise";
|
|
10
|
+
import { asDivinerInstance } from "@xyo-network/diviner-model";
|
|
11
|
+
import { AbstractSentinel } from "@xyo-network/sentinel-abstract";
|
|
12
|
+
import {
|
|
13
|
+
SentinelConfigSchema
|
|
14
|
+
} from "@xyo-network/sentinel-model";
|
|
15
|
+
import { asWitnessInstance } from "@xyo-network/witness-model";
|
|
16
|
+
var MemorySentinel = class extends AbstractSentinel {
|
|
17
|
+
static configSchemas = [SentinelConfigSchema];
|
|
50
18
|
async reportHandler(inPayloads = []) {
|
|
51
19
|
await this.started("throw");
|
|
52
20
|
const job = await this.jobPromise;
|
|
@@ -62,13 +30,13 @@ var MemorySentinel = class extends import_sentinel_abstract.AbstractSentinel {
|
|
|
62
30
|
async generateResults(tasks, previousResults, inPayloads) {
|
|
63
31
|
const results = await Promise.allSettled(
|
|
64
32
|
tasks == null ? void 0 : tasks.map(async (task) => {
|
|
65
|
-
const witness =
|
|
33
|
+
const witness = asWitnessInstance(task.module);
|
|
66
34
|
const input = task.input ?? false;
|
|
67
35
|
const inPayloadsFound = input === true ? inPayloads : input === false ? [] : this.processPreviousResults(previousResults, await this.inputAddresses(input));
|
|
68
36
|
if (witness) {
|
|
69
37
|
return [witness.address, await witness.observe(inPayloadsFound)];
|
|
70
38
|
}
|
|
71
|
-
const diviner =
|
|
39
|
+
const diviner = asDivinerInstance(task.module);
|
|
72
40
|
if (diviner) {
|
|
73
41
|
return [diviner.address, await diviner.divine(inPayloadsFound)];
|
|
74
42
|
}
|
|
@@ -76,7 +44,7 @@ var MemorySentinel = class extends import_sentinel_abstract.AbstractSentinel {
|
|
|
76
44
|
})
|
|
77
45
|
);
|
|
78
46
|
const finalResult = {};
|
|
79
|
-
results.filter(
|
|
47
|
+
results.filter(fulfilled).forEach((result) => {
|
|
80
48
|
const [address, payloads] = result.value;
|
|
81
49
|
finalResult[address] = finalResult[address] ?? [];
|
|
82
50
|
finalResult[address].push(...payloads);
|
|
@@ -97,8 +65,8 @@ var MemorySentinel = class extends import_sentinel_abstract.AbstractSentinel {
|
|
|
97
65
|
};
|
|
98
66
|
|
|
99
67
|
// src/SentinelIntervalAutomationWrapper.ts
|
|
100
|
-
|
|
101
|
-
var SentinelIntervalAutomationWrapper = class extends
|
|
68
|
+
import { PayloadWrapper } from "@xyo-network/payload-wrapper";
|
|
69
|
+
var SentinelIntervalAutomationWrapper = class extends PayloadWrapper {
|
|
102
70
|
constructor(payload) {
|
|
103
71
|
super(payload);
|
|
104
72
|
}
|
|
@@ -141,8 +109,8 @@ var SentinelIntervalAutomationWrapper = class extends import_payload_wrapper.Pay
|
|
|
141
109
|
};
|
|
142
110
|
|
|
143
111
|
// src/SentinelRunner.ts
|
|
144
|
-
|
|
145
|
-
|
|
112
|
+
import { assertEx } from "@xylabs/assert";
|
|
113
|
+
import { PayloadWrapper as PayloadWrapper2 } from "@xyo-network/payload-wrapper";
|
|
146
114
|
var SentinelRunner = class {
|
|
147
115
|
_automations = {};
|
|
148
116
|
onTriggerResult;
|
|
@@ -167,7 +135,7 @@ var SentinelRunner = class {
|
|
|
167
135
|
}, void 0);
|
|
168
136
|
}
|
|
169
137
|
async add(automation, restart = true) {
|
|
170
|
-
const hash = await
|
|
138
|
+
const hash = await PayloadWrapper2.hashAsync(automation);
|
|
171
139
|
this._automations[hash] = automation;
|
|
172
140
|
if (restart)
|
|
173
141
|
await this.restart();
|
|
@@ -190,7 +158,7 @@ var SentinelRunner = class {
|
|
|
190
158
|
await this.start();
|
|
191
159
|
}
|
|
192
160
|
async start() {
|
|
193
|
-
|
|
161
|
+
assertEx(this.timeoutId === void 0, "Already started");
|
|
194
162
|
const automation = this.next;
|
|
195
163
|
if (isSentinelIntervalAutomation(automation)) {
|
|
196
164
|
const delay = automation.start - Date.now();
|
|
@@ -232,18 +200,22 @@ var SentinelRunner = class {
|
|
|
232
200
|
};
|
|
233
201
|
|
|
234
202
|
// src/Wrapper.ts
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
203
|
+
import { constructableModuleWrapper, ModuleWrapper } from "@xyo-network/module-wrapper";
|
|
204
|
+
import {
|
|
205
|
+
isSentinelInstance,
|
|
206
|
+
isSentinelModule,
|
|
207
|
+
SentinelReportQuerySchema
|
|
208
|
+
} from "@xyo-network/sentinel-model";
|
|
209
|
+
constructableModuleWrapper();
|
|
210
|
+
var SentinelWrapper = class extends ModuleWrapper {
|
|
211
|
+
static instanceIdentityCheck = isSentinelInstance;
|
|
212
|
+
static moduleIdentityCheck = isSentinelModule;
|
|
213
|
+
static requiredQueries = [SentinelReportQuerySchema, ...super.requiredQueries];
|
|
242
214
|
archivists() {
|
|
243
215
|
throw Error("Not supported");
|
|
244
216
|
}
|
|
245
217
|
async report(payloads) {
|
|
246
|
-
const queryPayload = { schema:
|
|
218
|
+
const queryPayload = { schema: SentinelReportQuerySchema };
|
|
247
219
|
const result = await this.sendQuery(queryPayload, payloads);
|
|
248
220
|
return result;
|
|
249
221
|
}
|
|
@@ -253,10 +225,9 @@ var SentinelWrapper = class extends import_module_wrapper.ModuleWrapper {
|
|
|
253
225
|
};
|
|
254
226
|
|
|
255
227
|
// src/index.ts
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
0 && (module.exports = {
|
|
228
|
+
export * from "@xyo-network/sentinel-abstract";
|
|
229
|
+
export * from "@xyo-network/sentinel-model";
|
|
230
|
+
export {
|
|
260
231
|
MemorySentinel,
|
|
261
232
|
SentinelAutomationSchema,
|
|
262
233
|
SentinelEventAutomationSchema,
|
|
@@ -264,8 +235,6 @@ __reExport(src_exports, require("@xyo-network/sentinel-model"), module.exports);
|
|
|
264
235
|
SentinelIntervalAutomationWrapper,
|
|
265
236
|
SentinelRunner,
|
|
266
237
|
SentinelWrapper,
|
|
267
|
-
isSentinelIntervalAutomation
|
|
268
|
-
|
|
269
|
-
...require("@xyo-network/sentinel-model")
|
|
270
|
-
});
|
|
238
|
+
isSentinelIntervalAutomation
|
|
239
|
+
};
|
|
271
240
|
//# sourceMappingURL=index.js.map
|
package/dist/node/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts","../../src/Automation.ts","../../src/MemorySentinel.ts","../../src/SentinelIntervalAutomationWrapper.ts","../../src/SentinelRunner.ts","../../src/Wrapper.ts"],"sourcesContent":["export * from './Automation'\nexport * from './MemorySentinel'\nexport * from './SentinelIntervalAutomationWrapper'\nexport * from './SentinelRunner'\nexport * from './Wrapper'\nexport * from '@xyo-network/sentinel-abstract'\nexport * from '@xyo-network/sentinel-model'\n","import { isPayloadOfSchemaType, Payload } from '@xyo-network/payload-model'\nimport { Task } from '@xyo-network/sentinel-model'\n\nexport type SentinelAutomationSchema = 'network.xyo.automation'\nexport const SentinelAutomationSchema: SentinelAutomationSchema = 'network.xyo.automation'\n\nexport type SentinelIntervalAutomationSchema = 'network.xyo.automation.interval'\nexport const SentinelIntervalAutomationSchema: SentinelIntervalAutomationSchema = 'network.xyo.automation.interval'\n\nexport type SentinelEventAutomationSchema = 'network.xyo.automation.event'\nexport const SentinelEventAutomationSchema: SentinelEventAutomationSchema = 'network.xyo.automation.event'\n\nexport type SentinelBaseAutomationPayload<T extends Payload> = Payload<\n {\n tasks?: Task[]\n type?: 'interval' | 'event'\n } & T\n>\n\n/** Settings for an Interval Automation */\nexport type SentinelIntervalAutomationPayload = SentinelBaseAutomationPayload<{\n /** Epoch after which any reoccurrence stops */\n end?: number\n\n /** Time between triggers [non-repeating if undefined] */\n frequency?: number\n\n /** Units for frequency field [hour if undefined] */\n frequencyUnits?: 'second' | 'minute' | 'hour' | 'day'\n\n /** Remaining triggers [infinite if undefined] */\n remaining?: number\n\n schema: SentinelIntervalAutomationSchema\n\n /** Epoch of the next trigger */\n start: number\n\n /** The type of automation */\n type: 'interval'\n}>\n\nexport const isSentinelIntervalAutomation = isPayloadOfSchemaType<SentinelIntervalAutomationPayload>(SentinelIntervalAutomationSchema)\n\n/** Settings for an Event Automation */\nexport type SentinelEventAutomationPayload = SentinelBaseAutomationPayload<{\n schema: SentinelEventAutomationSchema\n type: 'event'\n}>\n\n/** Settings for an Automation */\nexport type SentinelAutomationPayload = Payload<\n SentinelIntervalAutomationPayload | SentinelEventAutomationPayload,\n SentinelAutomationSchema | SentinelIntervalAutomationSchema | SentinelEventAutomationSchema\n>\n","import { Address } from '@xylabs/hex'\nimport { fulfilled } from '@xylabs/promise'\nimport { asDivinerInstance } from '@xyo-network/diviner-model'\nimport { AnyConfigSchema } from '@xyo-network/module-model'\nimport { Payload } from '@xyo-network/payload-model'\nimport { AbstractSentinel } from '@xyo-network/sentinel-abstract'\nimport {\n ResolvedTask,\n SentinelConfig,\n SentinelConfigSchema,\n SentinelInstance,\n SentinelModuleEventData,\n SentinelParams,\n} from '@xyo-network/sentinel-model'\nimport { asWitnessInstance } from '@xyo-network/witness-model'\n\nexport type MemorySentinelParams<TConfig extends AnyConfigSchema<SentinelConfig> = AnyConfigSchema<SentinelConfig>> = SentinelParams<TConfig>\n\nexport class MemorySentinel<\n TParams extends MemorySentinelParams = MemorySentinelParams,\n TEventData extends SentinelModuleEventData<SentinelInstance<TParams>> = SentinelModuleEventData<SentinelInstance<TParams>>,\n> extends AbstractSentinel<TParams, TEventData> {\n static override configSchemas = [SentinelConfigSchema]\n\n async reportHandler(inPayloads: Payload[] = []): Promise<Payload[]> {\n await this.started('throw')\n const job = await this.jobPromise\n\n let index = 0\n let previousResults: Record<Address, Payload[]> = {}\n while (index < job.tasks.length) {\n const generatedPayloads = await this.generateResults(job.tasks[index], previousResults, inPayloads)\n previousResults = generatedPayloads\n index++\n }\n return Object.values(previousResults).flat()\n }\n\n private async generateResults(\n tasks: ResolvedTask[],\n previousResults: Record<Address, Payload[]>,\n inPayloads?: Payload[],\n ): Promise<Record<Address, Payload[]>> {\n const results: PromiseSettledResult<[Address, Payload[]]>[] = await Promise.allSettled(\n tasks?.map(async (task) => {\n const witness = asWitnessInstance(task.module)\n const input = task.input ?? false\n const inPayloadsFound =\n input === true ? inPayloads : input === false ? [] : this.processPreviousResults(previousResults, await this.inputAddresses(input))\n if (witness) {\n return [witness.address, await witness.observe(inPayloadsFound)]\n }\n const diviner = asDivinerInstance(task.module)\n if (diviner) {\n return [diviner.address, await diviner.divine(inPayloadsFound)]\n }\n throw Error('Unsupported module type')\n }),\n )\n const finalResult: Record<Address, Payload[]> = {}\n results.filter(fulfilled).forEach((result) => {\n const [address, payloads] = result.value\n finalResult[address] = finalResult[address] ?? []\n finalResult[address].push(...payloads)\n })\n /*const errors = results.filter(rejected).map((result) => result.reason)\n if (errors.length > 0) {\n throw Error('At least one module failed')\n }*/\n return finalResult\n }\n\n private async inputAddresses(input: string | string[]): Promise<string[]> {\n if (Array.isArray(input)) {\n return (await Promise.all(input.map(async (inputItem) => await this.inputAddresses(inputItem)))).flat()\n } else {\n const resolved = await this.resolve(input)\n return resolved ? [resolved.address] : []\n }\n }\n\n private processPreviousResults(payloads: Record<string, Payload[]>, inputs: string[]) {\n return inputs.map((input) => payloads[input] ?? []).flat()\n }\n}\n","import { PayloadWrapper } from '@xyo-network/payload-wrapper'\n\nimport { SentinelIntervalAutomationPayload } from './Automation'\n\nexport class SentinelIntervalAutomationWrapper<\n T extends SentinelIntervalAutomationPayload = SentinelIntervalAutomationPayload,\n> extends PayloadWrapper<T> {\n constructor(payload: T) {\n super(payload)\n }\n\n protected get frequencyMillis() {\n const frequency = this.jsonPayload().frequency\n if (frequency === undefined) return Infinity\n switch (this.jsonPayload().frequencyUnits ?? 'hour') {\n case 'second':\n return frequency * 1000\n case 'minute':\n return frequency * 60 * 1000\n case 'hour':\n return frequency * 60 * 60 * 1000\n case 'day':\n return frequency * 24 * 60 * 60 * 1000\n }\n }\n\n protected get remaining() {\n //if remaining is not defined, we assume Infinity\n return this.payload().remaining ?? Infinity\n }\n\n next() {\n this.jsonPayload().start = this.jsonPayload().start + this.frequencyMillis\n this.consumeRemaining()\n this.checkEnd()\n return this\n }\n\n protected checkEnd() {\n if (this.jsonPayload().start > (this.jsonPayload().end ?? Infinity)) {\n this.jsonPayload().start = Infinity\n }\n }\n\n protected consumeRemaining(count = 1) {\n const remaining = this.remaining - count\n this.jsonPayload().remaining = remaining\n\n if (remaining <= 0) {\n this.jsonPayload().start = Infinity\n }\n }\n}\n","import { assertEx } from '@xylabs/assert'\nimport { Payload } from '@xyo-network/payload-model'\nimport { PayloadWrapper } from '@xyo-network/payload-wrapper'\nimport { SentinelInstance } from '@xyo-network/sentinel-model'\n\nimport { isSentinelIntervalAutomation, SentinelAutomationPayload, SentinelIntervalAutomationPayload } from './Automation'\nimport { SentinelIntervalAutomationWrapper } from './SentinelIntervalAutomationWrapper'\n\nexport type OnSentinelRunnerTriggerResult = (result: Payload[]) => void\n\nexport class SentinelRunner {\n protected _automations: Record<string, SentinelAutomationPayload> = {}\n protected onTriggerResult: OnSentinelRunnerTriggerResult | undefined\n protected sentinel: SentinelInstance\n protected timeoutId?: NodeJS.Timeout | string | number\n\n constructor(sentinel: SentinelInstance, automations?: SentinelAutomationPayload[], onTriggerResult?: OnSentinelRunnerTriggerResult) {\n this.sentinel = sentinel\n this.onTriggerResult = onTriggerResult\n automations?.forEach((automation) => this.add(automation))\n }\n\n get automations() {\n return this._automations\n }\n\n private get next() {\n return Object.values(this._automations).reduce<SentinelAutomationPayload | undefined>((previous, current) => {\n if (isSentinelIntervalAutomation(current)) {\n if (isSentinelIntervalAutomation(previous)) {\n return current.start < (previous?.start ?? Infinity) ? current : previous\n }\n }\n return current\n }, undefined)\n }\n\n async add(automation: SentinelAutomationPayload, restart = true) {\n const hash = await PayloadWrapper.hashAsync(automation)\n this._automations[hash] = automation\n if (restart) await this.restart()\n return hash\n }\n\n find(hash: string) {\n Object.entries(this._automations).find(([key]) => key === hash)\n }\n\n async remove(hash: string, restart = true) {\n delete this._automations[hash]\n if (restart) await this.restart()\n }\n\n removeAll() {\n this.stop()\n this._automations = {}\n }\n\n async restart() {\n this.stop()\n await this.start()\n }\n\n async start() {\n assertEx(this.timeoutId === undefined, 'Already started')\n const automation = this.next\n if (isSentinelIntervalAutomation(automation)) {\n const delay = automation.start - Date.now()\n if (delay < 0) {\n //automation is due, just do it\n await this.trigger(automation)\n } else {\n this.timeoutId = setTimeout(\n async () => {\n this.timeoutId = undefined\n await this.start()\n },\n delay > 0 ? delay : 0,\n )\n }\n }\n }\n\n stop() {\n if (this.timeoutId) {\n clearTimeout(this.timeoutId)\n this.timeoutId = undefined\n }\n }\n\n async update(hash: string, automation: SentinelAutomationPayload, restart = true) {\n await this.remove(hash, false)\n await this.add(automation, false)\n if (restart) await this.restart()\n }\n\n private async trigger(automation: SentinelIntervalAutomationPayload) {\n const wrapper = new SentinelIntervalAutomationWrapper(automation)\n await this.remove(await wrapper.hashAsync(), false)\n wrapper.next()\n await this.add(wrapper.jsonPayload(), false)\n const triggerResult = await this.sentinel.report()\n this.onTriggerResult?.(triggerResult)\n await this.start()\n }\n}\n","import { ArchivistInstance } from '@xyo-network/archivist'\nimport { constructableModuleWrapper, ModuleWrapper } from '@xyo-network/module-wrapper'\nimport { Payload } from '@xyo-network/payload-model'\nimport {\n isSentinelInstance,\n isSentinelModule,\n SentinelInstance,\n SentinelModule,\n SentinelReportQuery,\n SentinelReportQuerySchema,\n} from '@xyo-network/sentinel-model'\nimport { WitnessInstance } from '@xyo-network/witness-model'\n\nconstructableModuleWrapper()\nexport class SentinelWrapper<TModule extends SentinelModule = SentinelModule>\n extends ModuleWrapper<TModule>\n implements SentinelInstance<TModule['params']>\n{\n static override instanceIdentityCheck = isSentinelInstance\n static override moduleIdentityCheck = isSentinelModule\n static override requiredQueries = [SentinelReportQuerySchema, ...super.requiredQueries]\n\n archivists(): Promise<ArchivistInstance[]> {\n throw Error('Not supported')\n }\n\n async report(payloads?: Payload[]): Promise<Payload[]> {\n const queryPayload: SentinelReportQuery = { schema: SentinelReportQuerySchema }\n const result = await this.sendQuery(queryPayload, payloads)\n return result\n }\n\n witnesses(): Promise<WitnessInstance[]> {\n throw Error('Not supported')\n }\n}\n"],"mapp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UAAM,KAAK,OAAO,MAAM,QAAQ,UAAU,GAAG,KAAK;AAClD,YAAQ,KAAK;AACb,UAAM,KAAK,IAAI,QAAQ,YAAY,GAAG,KAAK;AAC3C,UAAM,gBAAgB,MAAM,KAAK,SAAS,OAAO;AACjD,eAAK,oBAAL,8BAAuB;AACvB,UAAM,KAAK,MAAM;AAAA,EACnB;AACF;;;ACxGA,4BAA0D;AAE1D,IAAAC,yBAOO;AAAA,IAGP,kDAA2B;AACpB,IAAM,kBAAN,cACG,oCAEV;AAAA,EACE,OAAgB,wBAAwB;AAAA,EACxC,OAAgB,sBAAsB;AAAA,EACtC,OAAgB,kBAAkB,CAAC,kDAA2B,GAAG,MAAM,eAAe;AAAA,EAEtF,aAA2C;AACzC,UAAM,MAAM,eAAe;AAAA,EAC7B;AAAA,EAEA,MAAM,OAAO,UAA0C;AACrD,UAAM,eAAoC,EAAE,QAAQ,iDAA0B;AAC9E,UAAM,SAAS,MAAM,KAAK,UAAU,cAAc,QAAQ;AAC1D,WAAO;AAAA,EACT;AAAA,EAEA,YAAwC;AACtC,UAAM,MAAM,eAAe;AAAA,EAC7B;AACF;;;AL9BA,wBAAc,2CALd;AAMA,wBAAc,wCANd;","names":["import_payload_wrapper","import_sentinel_model"]}
|
|
1
|
+
{"version":3,"sources":["../../src/Automation.ts","../../src/MemorySentinel.ts","../../src/SentinelIntervalAutomationWrapper.ts","../../src/SentinelRunner.ts","../../src/Wrapper.ts","../../src/index.ts"],"sourcesContent":["import { isPayloadOfSchemaType, Payload } from '@xyo-network/payload-model'\nimport { Task } from '@xyo-network/sentinel-model'\n\nexport type SentinelAutomationSchema = 'network.xyo.automation'\nexport const SentinelAutomationSchema: SentinelAutomationSchema = 'network.xyo.automation'\n\nexport type SentinelIntervalAutomationSchema = 'network.xyo.automation.interval'\nexport const SentinelIntervalAutomationSchema: SentinelIntervalAutomationSchema = 'network.xyo.automation.interval'\n\nexport type SentinelEventAutomationSchema = 'network.xyo.automation.event'\nexport const SentinelEventAutomationSchema: SentinelEventAutomationSchema = 'network.xyo.automation.event'\n\nexport type SentinelBaseAutomationPayload<T extends Payload> = Payload<\n {\n tasks?: Task[]\n type?: 'interval' | 'event'\n } & T\n>\n\n/** Settings for an Interval Automation */\nexport type SentinelIntervalAutomationPayload = SentinelBaseAutomationPayload<{\n /** Epoch after which any reoccurrence stops */\n end?: number\n\n /** Time between triggers [non-repeating if undefined] */\n frequency?: number\n\n /** Units for frequency field [hour if undefined] */\n frequencyUnits?: 'second' | 'minute' | 'hour' | 'day'\n\n /** Remaining triggers [infinite if undefined] */\n remaining?: number\n\n schema: SentinelIntervalAutomationSchema\n\n /** Epoch of the next trigger */\n start: number\n\n /** The type of automation */\n type: 'interval'\n}>\n\nexport const isSentinelIntervalAutomation = isPayloadOfSchemaType<SentinelIntervalAutomationPayload>(SentinelIntervalAutomationSchema)\n\n/** Settings for an Event Automation */\nexport type SentinelEventAutomationPayload = SentinelBaseAutomationPayload<{\n schema: SentinelEventAutomationSchema\n type: 'event'\n}>\n\n/** Settings for an Automation */\nexport type SentinelAutomationPayload = Payload<\n SentinelIntervalAutomationPayload | SentinelEventAutomationPayload,\n SentinelAutomationSchema | SentinelIntervalAutomationSchema | SentinelEventAutomationSchema\n>\n","import { Address } from '@xylabs/hex'\nimport { fulfilled } from '@xylabs/promise'\nimport { asDivinerInstance } from '@xyo-network/diviner-model'\nimport { AnyConfigSchema } from '@xyo-network/module-model'\nimport { Payload } from '@xyo-network/payload-model'\nimport { AbstractSentinel } from '@xyo-network/sentinel-abstract'\nimport {\n ResolvedTask,\n SentinelConfig,\n SentinelConfigSchema,\n SentinelInstance,\n SentinelModuleEventData,\n SentinelParams,\n} from '@xyo-network/sentinel-model'\nimport { asWitnessInstance } from '@xyo-network/witness-model'\n\nexport type MemorySentinelParams<TConfig extends AnyConfigSchema<SentinelConfig> = AnyConfigSchema<SentinelConfig>> = SentinelParams<TConfig>\n\nexport class MemorySentinel<\n TParams extends MemorySentinelParams = MemorySentinelParams,\n TEventData extends SentinelModuleEventData<SentinelInstance<TParams>> = SentinelModuleEventData<SentinelInstance<TParams>>,\n> extends AbstractSentinel<TParams, TEventData> {\n static override configSchemas = [SentinelConfigSchema]\n\n async reportHandler(inPayloads: Payload[] = []): Promise<Payload[]> {\n await this.started('throw')\n const job = await this.jobPromise\n\n let index = 0\n let previousResults: Record<Address, Payload[]> = {}\n while (index < job.tasks.length) {\n const generatedPayloads = await this.generateResults(job.tasks[index], previousResults, inPayloads)\n previousResults = generatedPayloads\n index++\n }\n return Object.values(previousResults).flat()\n }\n\n private async generateResults(\n tasks: ResolvedTask[],\n previousResults: Record<Address, Payload[]>,\n inPayloads?: Payload[],\n ): Promise<Record<Address, Payload[]>> {\n const results: PromiseSettledResult<[Address, Payload[]]>[] = await Promise.allSettled(\n tasks?.map(async (task) => {\n const witness = asWitnessInstance(task.module)\n const input = task.input ?? false\n const inPayloadsFound =\n input === true ? inPayloads : input === false ? [] : this.processPreviousResults(previousResults, await this.inputAddresses(input))\n if (witness) {\n return [witness.address, await witness.observe(inPayloadsFound)]\n }\n const diviner = asDivinerInstance(task.module)\n if (diviner) {\n return [diviner.address, await diviner.divine(inPayloadsFound)]\n }\n throw Error('Unsupported module type')\n }),\n )\n const finalResult: Record<Address, Payload[]> = {}\n results.filter(fulfilled).forEach((result) => {\n const [address, payloads] = result.value\n finalResult[address] = finalResult[address] ?? []\n finalResult[address].push(...payloads)\n })\n /*const errors = results.filter(rejected).map((result) => result.reason)\n if (errors.length > 0) {\n throw Error('At least one module failed')\n }*/\n return finalResult\n }\n\n private async inputAddresses(input: string | string[]): Promise<string[]> {\n if (Array.isArray(input)) {\n return (await Promise.all(input.map(async (inputItem) => await this.inputAddresses(inputItem)))).flat()\n } else {\n const resolved = await this.resolve(input)\n return resolved ? [resolved.address] : []\n }\n }\n\n private processPreviousResults(payloads: Record<string, Payload[]>, inputs: string[]) {\n return inputs.map((input) => payloads[input] ?? []).flat()\n }\n}\n","import { PayloadWrapper } from '@xyo-network/payload-wrapper'\n\nimport { SentinelIntervalAutomationPayload } from './Automation'\n\nexport class SentinelIntervalAutomationWrapper<\n T extends SentinelIntervalAutomationPayload = SentinelIntervalAutomationPayload,\n> extends PayloadWrapper<T> {\n constructor(payload: T) {\n super(payload)\n }\n\n protected get frequencyMillis() {\n const frequency = this.jsonPayload().frequency\n if (frequency === undefined) return Infinity\n switch (this.jsonPayload().frequencyUnits ?? 'hour') {\n case 'second':\n return frequency * 1000\n case 'minute':\n return frequency * 60 * 1000\n case 'hour':\n return frequency * 60 * 60 * 1000\n case 'day':\n return frequency * 24 * 60 * 60 * 1000\n }\n }\n\n protected get remaining() {\n //if remaining is not defined, we assume Infinity\n return this.payload().remaining ?? Infinity\n }\n\n next() {\n this.jsonPayload().start = this.jsonPayload().start + this.frequencyMillis\n this.consumeRemaining()\n this.checkEnd()\n return this\n }\n\n protected checkEnd() {\n if (this.jsonPayload().start > (this.jsonPayload().end ?? Infinity)) {\n this.jsonPayload().start = Infinity\n }\n }\n\n protected consumeRemaining(count = 1) {\n const remaining = this.remaining - count\n this.jsonPayload().remaining = remaining\n\n if (remaining <= 0) {\n this.jsonPayload().start = Infinity\n }\n }\n}\n","import { assertEx } from '@xylabs/assert'\nimport { Payload } from '@xyo-network/payload-model'\nimport { PayloadWrapper } from '@xyo-network/payload-wrapper'\nimport { SentinelInstance } from '@xyo-network/sentinel-model'\n\nimport { isSentinelIntervalAutomation, SentinelAutomationPayload, SentinelIntervalAutomationPayload } from './Automation'\nimport { SentinelIntervalAutomationWrapper } from './SentinelIntervalAutomationWrapper'\n\nexport type OnSentinelRunnerTriggerResult = (result: Payload[]) => void\n\nexport class SentinelRunner {\n protected _automations: Record<string, SentinelAutomationPayload> = {}\n protected onTriggerResult: OnSentinelRunnerTriggerResult | undefined\n protected sentinel: SentinelInstance\n protected timeoutId?: NodeJS.Timeout | string | number\n\n constructor(sentinel: SentinelInstance, automations?: SentinelAutomationPayload[], onTriggerResult?: OnSentinelRunnerTriggerResult) {\n this.sentinel = sentinel\n this.onTriggerResult = onTriggerResult\n automations?.forEach((automation) => this.add(automation))\n }\n\n get automations() {\n return this._automations\n }\n\n private get next() {\n return Object.values(this._automations).reduce<SentinelAutomationPayload | undefined>((previous, current) => {\n if (isSentinelIntervalAutomation(current)) {\n if (isSentinelIntervalAutomation(previous)) {\n return current.start < (previous?.start ?? Infinity) ? current : previous\n }\n }\n return current\n }, undefined)\n }\n\n async add(automation: SentinelAutomationPayload, restart = true) {\n const hash = await PayloadWrapper.hashAsync(automation)\n this._automations[hash] = automation\n if (restart) await this.restart()\n return hash\n }\n\n find(hash: string) {\n Object.entries(this._automations).find(([key]) => key === hash)\n }\n\n async remove(hash: string, restart = true) {\n delete this._automations[hash]\n if (restart) await this.restart()\n }\n\n removeAll() {\n this.stop()\n this._automations = {}\n }\n\n async restart() {\n this.stop()\n await this.start()\n }\n\n async start() {\n assertEx(this.timeoutId === undefined, 'Already started')\n const automation = this.next\n if (isSentinelIntervalAutomation(automation)) {\n const delay = automation.start - Date.now()\n if (delay < 0) {\n //automation is due, just do it\n await this.trigger(automation)\n } else {\n this.timeoutId = setTimeout(\n async () => {\n this.timeoutId = undefined\n await this.start()\n },\n delay > 0 ? delay : 0,\n )\n }\n }\n }\n\n stop() {\n if (this.timeoutId) {\n clearTimeout(this.timeoutId)\n this.timeoutId = undefined\n }\n }\n\n async update(hash: string, automation: SentinelAutomationPayload, restart = true) {\n await this.remove(hash, false)\n await this.add(automation, false)\n if (restart) await this.restart()\n }\n\n private async trigger(automation: SentinelIntervalAutomationPayload) {\n const wrapper = new SentinelIntervalAutomationWrapper(automation)\n await this.remove(await wrapper.hashAsync(), false)\n wrapper.next()\n await this.add(wrapper.jsonPayload(), false)\n const triggerResult = await this.sentinel.report()\n this.onTriggerResult?.(triggerResult)\n await this.start()\n }\n}\n","import { ArchivistInstance } from '@xyo-network/archivist'\nimport { constructableModuleWrapper, ModuleWrapper } from '@xyo-network/module-wrapper'\nimport { Payload } from '@xyo-network/payload-model'\nimport {\n isSentinelInstance,\n isSentinelModule,\n SentinelInstance,\n SentinelModule,\n SentinelReportQuery,\n SentinelReportQuerySchema,\n} from '@xyo-network/sentinel-model'\nimport { WitnessInstance } from '@xyo-network/witness-model'\n\nconstructableModuleWrapper()\nexport class SentinelWrapper<TModule extends SentinelModule = SentinelModule>\n extends ModuleWrapper<TModule>\n implements SentinelInstance<TModule['params']>\n{\n static override instanceIdentityCheck = isSentinelInstance\n static override moduleIdentityCheck = isSentinelModule\n static override requiredQueries = [SentinelReportQuerySchema, ...super.requiredQueries]\n\n archivists(): Promise<ArchivistInstance[]> {\n throw Error('Not supported')\n }\n\n async report(payloads?: Payload[]): Promise<Payload[]> {\n const queryPayload: SentinelReportQuery = { schema: SentinelReportQuerySchema }\n const result = await this.sendQuery(queryPayload, payloads)\n return result\n }\n\n witnesses(): Promise<WitnessInstance[]> {\n throw Error('Not supported')\n }\n}\n","export * from './Automation'\nexport * from './MemorySentinel'\nexport * from './SentinelIntervalAutomationWrapper'\nexport * from './SentinelRunner'\nexport * from './Wrapper'\nexport * from '@xyo-network/sentinel-abstract'\nexport * from '@xyo-network/sentinel-model'\n"],"mappings":";AAAA,SAAS,6BAAsC;AAIxC,IAAM,2BAAqD;AAG3D,IAAM,mCAAqE;AAG3E,IAAM,gCAA+D;AAgCrE,IAAM,+BAA+B,sBAAyD,gCAAgC;;;ACzCrI,SAAS,iBAAiB;AAC1B,SAAS,yBAAyB;AAGlC,SAAS,wBAAwB;AACjC;AAAA,EAGE;AAAA,OAIK;AACP,SAAS,yBAAyB;AAI3B,IAAM,iBAAN,cAGG,iBAAsC;AAAA,EAC9C,OAAgB,gBAAgB,CAAC,oBAAoB;AAAA,EAErD,MAAM,cAAc,aAAwB,CAAC,GAAuB;AAClE,UAAM,KAAK,QAAQ,OAAO;AAC1B,UAAM,MAAM,MAAM,KAAK;AAEvB,QAAI,QAAQ;AACZ,QAAI,kBAA8C,CAAC;AACnD,WAAO,QAAQ,IAAI,MAAM,QAAQ;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AO,UAA0C;AACrD,UAAM,eAAoC,EAAE,QAAQ,0BAA0B;AAC9E,UAAM,SAAS,MAAM,KAAK,UAAU,cAAc,QAAQ;AAC1D,WAAO;AAAA,EACT;AAAA,EAEA,YAAwC;AACtC,UAAM,MAAM,eAAe;AAAA,EAC7B;AACF;;;AC9BA,cAAc;AACd,cAAc;","names":["PayloadWrapper","PayloadWrapper"]}
|
package/package.json
CHANGED
|
@@ -13,33 +13,30 @@
|
|
|
13
13
|
"@xylabs/assert": "^2.13.20",
|
|
14
14
|
"@xylabs/hex": "^2.13.20",
|
|
15
15
|
"@xylabs/promise": "^2.13.20",
|
|
16
|
-
"@xyo-network/archivist": "~2.84.
|
|
17
|
-
"@xyo-network/diviner-model": "~2.84.
|
|
18
|
-
"@xyo-network/module-model": "~2.84.
|
|
19
|
-
"@xyo-network/module-wrapper": "~2.84.
|
|
20
|
-
"@xyo-network/payload-model": "~2.84.
|
|
21
|
-
"@xyo-network/payload-wrapper": "~2.84.
|
|
22
|
-
"@xyo-network/sentinel-abstract": "~2.84.
|
|
23
|
-
"@xyo-network/sentinel-model": "~2.84.
|
|
24
|
-
"@xyo-network/witness-model": "~2.84.
|
|
16
|
+
"@xyo-network/archivist": "~2.84.3",
|
|
17
|
+
"@xyo-network/diviner-model": "~2.84.3",
|
|
18
|
+
"@xyo-network/module-model": "~2.84.3",
|
|
19
|
+
"@xyo-network/module-wrapper": "~2.84.3",
|
|
20
|
+
"@xyo-network/payload-model": "~2.84.3",
|
|
21
|
+
"@xyo-network/payload-wrapper": "~2.84.3",
|
|
22
|
+
"@xyo-network/sentinel-abstract": "~2.84.3",
|
|
23
|
+
"@xyo-network/sentinel-model": "~2.84.3",
|
|
24
|
+
"@xyo-network/witness-model": "~2.84.3"
|
|
25
25
|
},
|
|
26
26
|
"description": "Primary SDK for using XYO Protocol 2.0",
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"@xylabs/delay": "^2.13.20",
|
|
29
|
-
"@xylabs/ts-scripts-yarn3": "^3.2.
|
|
30
|
-
"@xylabs/tsconfig": "^3.2.
|
|
31
|
-
"@xyo-network/abstract-witness": "~2.84.
|
|
32
|
-
"@xyo-network/account": "~2.84.
|
|
33
|
-
"@xyo-network/boundwitness-model": "~2.84.
|
|
34
|
-
"@xyo-network/hash": "~2.84.
|
|
35
|
-
"@xyo-network/id-payload-plugin": "^2.
|
|
36
|
-
"@xyo-network/
|
|
37
|
-
"@xyo-network/
|
|
38
|
-
"@xyo-network/node-system-info-plugin": "^2.83.0",
|
|
39
|
-
"@xyo-network/witnesses": "~2.84.2",
|
|
29
|
+
"@xylabs/ts-scripts-yarn3": "^3.2.19",
|
|
30
|
+
"@xylabs/tsconfig": "^3.2.19",
|
|
31
|
+
"@xyo-network/abstract-witness": "~2.84.3",
|
|
32
|
+
"@xyo-network/account": "~2.84.3",
|
|
33
|
+
"@xyo-network/boundwitness-model": "~2.84.3",
|
|
34
|
+
"@xyo-network/hash": "~2.84.3",
|
|
35
|
+
"@xyo-network/id-payload-plugin": "^2.84.0",
|
|
36
|
+
"@xyo-network/node-memory": "~2.84.3",
|
|
37
|
+
"@xyo-network/witnesses": "~2.84.3",
|
|
40
38
|
"typescript": "^5.3.3"
|
|
41
39
|
},
|
|
42
|
-
"docs": "dist/docs.json",
|
|
43
40
|
"types": "dist/node/index.d.ts",
|
|
44
41
|
"exports": {
|
|
45
42
|
".": {
|
|
@@ -55,19 +52,19 @@
|
|
|
55
52
|
},
|
|
56
53
|
"node": {
|
|
57
54
|
"require": {
|
|
58
|
-
"types": "./dist/node/index.d.
|
|
59
|
-
"default": "./dist/node/index.
|
|
55
|
+
"types": "./dist/node/index.d.cts",
|
|
56
|
+
"default": "./dist/node/index.cjs"
|
|
60
57
|
},
|
|
61
58
|
"import": {
|
|
62
59
|
"types": "./dist/node/index.d.mts",
|
|
63
|
-
"default": "./dist/node/index.
|
|
60
|
+
"default": "./dist/node/index.js"
|
|
64
61
|
}
|
|
65
62
|
}
|
|
66
63
|
},
|
|
67
64
|
"./package.json": "./package.json"
|
|
68
65
|
},
|
|
69
|
-
"main": "dist/node/index.
|
|
70
|
-
"module": "dist/node/index.
|
|
66
|
+
"main": "dist/node/index.cjs",
|
|
67
|
+
"module": "dist/node/index.js",
|
|
71
68
|
"homepage": "https://xyo.network",
|
|
72
69
|
"license": "LGPL-3.0-only",
|
|
73
70
|
"publishConfig": {
|
|
@@ -78,5 +75,6 @@
|
|
|
78
75
|
"url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js.git"
|
|
79
76
|
},
|
|
80
77
|
"sideEffects": false,
|
|
81
|
-
"version": "2.84.
|
|
78
|
+
"version": "2.84.3",
|
|
79
|
+
"type": "module"
|
|
82
80
|
}
|
package/dist/node/index.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/Automation.ts","../../src/MemorySentinel.ts","../../src/SentinelIntervalAutomationWrapper.ts","../../src/SentinelRunner.ts","../../src/Wrapper.ts","../../src/index.ts"],"sourcesContent":["import { isPayloadOfSchemaType, Payload } from '@xyo-network/payload-model'\nimport { Task } from '@xyo-network/sentinel-model'\n\nexport type SentinelAutomationSchema = 'network.xyo.automation'\nexport const SentinelAutomationSchema: SentinelAutomationSchema = 'network.xyo.automation'\n\nexport type SentinelIntervalAutomationSchema = 'network.xyo.automation.interval'\nexport const SentinelIntervalAutomationSchema: SentinelIntervalAutomationSchema = 'network.xyo.automation.interval'\n\nexport type SentinelEventAutomationSchema = 'network.xyo.automation.event'\nexport const SentinelEventAutomationSchema: SentinelEventAutomationSchema = 'network.xyo.automation.event'\n\nexport type SentinelBaseAutomationPayload<T extends Payload> = Payload<\n {\n tasks?: Task[]\n type?: 'interval' | 'event'\n } & T\n>\n\n/** Settings for an Interval Automation */\nexport type SentinelIntervalAutomationPayload = SentinelBaseAutomationPayload<{\n /** Epoch after which any reoccurrence stops */\n end?: number\n\n /** Time between triggers [non-repeating if undefined] */\n frequency?: number\n\n /** Units for frequency field [hour if undefined] */\n frequencyUnits?: 'second' | 'minute' | 'hour' | 'day'\n\n /** Remaining triggers [infinite if undefined] */\n remaining?: number\n\n schema: SentinelIntervalAutomationSchema\n\n /** Epoch of the next trigger */\n start: number\n\n /** The type of automation */\n type: 'interval'\n}>\n\nexport const isSentinelIntervalAutomation = isPayloadOfSchemaType<SentinelIntervalAutomationPayload>(SentinelIntervalAutomationSchema)\n\n/** Settings for an Event Automation */\nexport type SentinelEventAutomationPayload = SentinelBaseAutomationPayload<{\n schema: SentinelEventAutomationSchema\n type: 'event'\n}>\n\n/** Settings for an Automation */\nexport type SentinelAutomationPayload = Payload<\n SentinelIntervalAutomationPayload | SentinelEventAutomationPayload,\n SentinelAutomationSchema | SentinelIntervalAutomationSchema | SentinelEventAutomationSchema\n>\n","import { Address } from '@xylabs/hex'\nimport { fulfilled } from '@xylabs/promise'\nimport { asDivinerInstance } from '@xyo-network/diviner-model'\nimport { AnyConfigSchema } from '@xyo-network/module-model'\nimport { Payload } from '@xyo-network/payload-model'\nimport { AbstractSentinel } from '@xyo-network/sentinel-abstract'\nimport {\n ResolvedTask,\n SentinelConfig,\n SentinelConfigSchema,\n SentinelInstance,\n SentinelModuleEventData,\n SentinelParams,\n} from '@xyo-network/sentinel-model'\nimport { asWitnessInstance } from '@xyo-network/witness-model'\n\nexport type MemorySentinelParams<TConfig extends AnyConfigSchema<SentinelConfig> = AnyConfigSchema<SentinelConfig>> = SentinelParams<TConfig>\n\nexport class MemorySentinel<\n TParams extends MemorySentinelParams = MemorySentinelParams,\n TEventData extends SentinelModuleEventData<SentinelInstance<TParams>> = SentinelModuleEventData<SentinelInstance<TParams>>,\n> extends AbstractSentinel<TParams, TEventData> {\n static override configSchemas = [SentinelConfigSchema]\n\n async reportHandler(inPayloads: Payload[] = []): Promise<Payload[]> {\n await this.started('throw')\n const job = await this.jobPromise\n\n let index = 0\n let previousResults: Record<Address, Payload[]> = {}\n while (index < job.tasks.length) {\n const generatedPayloads = await this.generateResults(job.tasks[index], previousResults, inPayloads)\n previousResults = generatedPayloads\n index++\n }\n return Object.values(previousResults).flat()\n }\n\n private async generateResults(\n tasks: ResolvedTask[],\n previousResults: Record<Address, Payload[]>,\n inPayloads?: Payload[],\n ): Promise<Record<Address, Payload[]>> {\n const results: PromiseSettledResult<[Address, Payload[]]>[] = await Promise.allSettled(\n tasks?.map(async (task) => {\n const witness = asWitnessInstance(task.module)\n const input = task.input ?? false\n const inPayloadsFound =\n input === true ? inPayloads : input === false ? [] : this.processPreviousResults(previousResults, await this.inputAddresses(input))\n if (witness) {\n return [witness.address, await witness.observe(inPayloadsFound)]\n }\n const diviner = asDivinerInstance(task.module)\n if (diviner) {\n return [diviner.address, await diviner.divine(inPayloadsFound)]\n }\n throw Error('Unsupported module type')\n }),\n )\n const finalResult: Record<Address, Payload[]> = {}\n results.filter(fulfilled).forEach((result) => {\n const [address, payloads] = result.value\n finalResult[address] = finalResult[address] ?? []\n finalResult[address].push(...payloads)\n })\n /*const errors = results.filter(rejected).map((result) => result.reason)\n if (errors.length > 0) {\n throw Error('At least one module failed')\n }*/\n return finalResult\n }\n\n private async inputAddresses(input: string | string[]): Promise<string[]> {\n if (Array.isArray(input)) {\n return (await Promise.all(input.map(async (inputItem) => await this.inputAddresses(inputItem)))).flat()\n } else {\n const resolved = await this.resolve(input)\n return resolved ? [resolved.address] : []\n }\n }\n\n private processPreviousResults(payloads: Record<string, Payload[]>, inputs: string[]) {\n return inputs.map((input) => payloads[input] ?? []).flat()\n }\n}\n","import { PayloadWrapper } from '@xyo-network/payload-wrapper'\n\nimport { SentinelIntervalAutomationPayload } from './Automation'\n\nexport class SentinelIntervalAutomationWrapper<\n T extends SentinelIntervalAutomationPayload = SentinelIntervalAutomationPayload,\n> extends PayloadWrapper<T> {\n constructor(payload: T) {\n super(payload)\n }\n\n protected get frequencyMillis() {\n const frequency = this.jsonPayload().frequency\n if (frequency === undefined) return Infinity\n switch (this.jsonPayload().frequencyUnits ?? 'hour') {\n case 'second':\n return frequency * 1000\n case 'minute':\n return frequency * 60 * 1000\n case 'hour':\n return frequency * 60 * 60 * 1000\n case 'day':\n return frequency * 24 * 60 * 60 * 1000\n }\n }\n\n protected get remaining() {\n //if remaining is not defined, we assume Infinity\n return this.payload().remaining ?? Infinity\n }\n\n next() {\n this.jsonPayload().start = this.jsonPayload().start + this.frequencyMillis\n this.consumeRemaining()\n this.checkEnd()\n return this\n }\n\n protected checkEnd() {\n if (this.jsonPayload().start > (this.jsonPayload().end ?? Infinity)) {\n this.jsonPayload().start = Infinity\n }\n }\n\n protected consumeRemaining(count = 1) {\n const remaining = this.remaining - count\n this.jsonPayload().remaining = remaining\n\n if (remaining <= 0) {\n this.jsonPayload().start = Infinity\n }\n }\n}\n","import { assertEx } from '@xylabs/assert'\nimport { Payload } from '@xyo-network/payload-model'\nimport { PayloadWrapper } from '@xyo-network/payload-wrapper'\nimport { SentinelInstance } from '@xyo-network/sentinel-model'\n\nimport { isSentinelIntervalAutomation, SentinelAutomationPayload, SentinelIntervalAutomationPayload } from './Automation'\nimport { SentinelIntervalAutomationWrapper } from './SentinelIntervalAutomationWrapper'\n\nexport type OnSentinelRunnerTriggerResult = (result: Payload[]) => void\n\nexport class SentinelRunner {\n protected _automations: Record<string, SentinelAutomationPayload> = {}\n protected onTriggerResult: OnSentinelRunnerTriggerResult | undefined\n protected sentinel: SentinelInstance\n protected timeoutId?: NodeJS.Timeout | string | number\n\n constructor(sentinel: SentinelInstance, automations?: SentinelAutomationPayload[], onTriggerResult?: OnSentinelRunnerTriggerResult) {\n this.sentinel = sentinel\n this.onTriggerResult = onTriggerResult\n automations?.forEach((automation) => this.add(automation))\n }\n\n get automations() {\n return this._automations\n }\n\n private get next() {\n return Object.values(this._automations).reduce<SentinelAutomationPayload | undefined>((previous, current) => {\n if (isSentinelIntervalAutomation(current)) {\n if (isSentinelIntervalAutomation(previous)) {\n return current.start < (previous?.start ?? Infinity) ? current : previous\n }\n }\n return current\n }, undefined)\n }\n\n async add(automation: SentinelAutomationPayload, restart = true) {\n const hash = await PayloadWrapper.hashAsync(automation)\n this._automations[hash] = automation\n if (restart) await this.restart()\n return hash\n }\n\n find(hash: string) {\n Object.entries(this._automations).find(([key]) => key === hash)\n }\n\n async remove(hash: string, restart = true) {\n delete this._automations[hash]\n if (restart) await this.restart()\n }\n\n removeAll() {\n this.stop()\n this._automations = {}\n }\n\n async restart() {\n this.stop()\n await this.start()\n }\n\n async start() {\n assertEx(this.timeoutId === undefined, 'Already started')\n const automation = this.next\n if (isSentinelIntervalAutomation(automation)) {\n const delay = automation.start - Date.now()\n if (delay < 0) {\n //automation is due, just do it\n await this.trigger(automation)\n } else {\n this.timeoutId = setTimeout(\n async () => {\n this.timeoutId = undefined\n await this.start()\n },\n delay > 0 ? delay : 0,\n )\n }\n }\n }\n\n stop() {\n if (this.timeoutId) {\n clearTimeout(this.timeoutId)\n this.timeoutId = undefined\n }\n }\n\n async update(hash: string, automation: SentinelAutomationPayload, restart = true) {\n await this.remove(hash, false)\n await this.add(automation, false)\n if (restart) await this.restart()\n }\n\n private async trigger(automation: SentinelIntervalAutomationPayload) {\n const wrapper = new SentinelIntervalAutomationWrapper(automation)\n await this.remove(await wrapper.hashAsync(), false)\n wrapper.next()\n await this.add(wrapper.jsonPayload(), false)\n const triggerResult = await this.sentinel.report()\n this.onTriggerResult?.(triggerResult)\n await this.start()\n }\n}\n","import { ArchivistInstance } from '@xyo-network/archivist'\nimport { constructableModuleWrapper, ModuleWrapper } from '@xyo-network/module-wrapper'\nimport { Payload } from '@xyo-network/payload-model'\nimport {\n isSentinelInstance,\n isSentinelModule,\n SentinelInstance,\n SentinelModule,\n SentinelReportQuery,\n SentinelReportQuerySchema,\n} from '@xyo-network/sentinel-model'\nimport { WitnessInstance } from '@xyo-network/witness-model'\n\nconstructableModuleWrapper()\nexport class SentinelWrapper<TModule extends SentinelModule = SentinelModule>\n extends ModuleWrapper<TModule>\n implements SentinelInstance<TModule['params']>\n{\n static override instanceIdentityCheck = isSentinelInstance\n static override moduleIdentityCheck = isSentinelModule\n static override requiredQueries = [SentinelReportQuerySchema, ...super.requiredQueries]\n\n archivists(): Promise<ArchivistInstance[]> {\n throw Error('Not supported')\n }\n\n async report(payloads?: Payload[]): Promise<Payload[]> {\n const queryPayload: SentinelReportQuery = { schema: SentinelReportQuerySchema }\n const result = await this.sendQuery(queryPayload, payloads)\n return result\n }\n\n witnesses(): Promise<WitnessInstance[]> {\n throw Error('Not supported')\n }\n}\n","export * from './Automation'\nexport * from './MemorySentinel'\nexport * from './SentinelIntervalAutomationWrapper'\nexport * from './SentinelRunner'\nexport * from './Wrapper'\nexport * from '@xyo-network/sentinel-abstract'\nexport * from '@xyo-network/sentinel-model'\n"],"mappings":";AAAA,SAAS,6BAAsC;AAIxC,IAAM,2BAAqD;AAG3D,IAAM,mCAAqE;AAG3E,IAAM,gCAA+D;AAgCrE,IAAM,+BAA+B,sBAAyD,gCAAgC;;;ACzCrI,SAAS,iBAAiB;AAC1B,SAAS,yBAAyB;AAGlC,SAAS,wBAAwB;AACjC;AAAA,EAGE;AAAA,OAIK;AACP,SAAS,yBAAyB;AAI3B,IAAM,iBAAN,cAGG,iBAAsC;AAAA,EAC9C,OAAgB,gBAAgB,CAAC,oBAAoB;AAAA,EAErD,MAAM,cAAc,aAAwB,CAAC,GAAuB;AAClE,UAAM,KAAK,QAAQ,OAAO;AAC1B,UAAM,MAAM,MAAM,KAAK;AAEvB,QAAI,QAAQ;AACZ,QAAI,kBAA8C,CAAC;AACnD,WAAO,QAAQ,IAAI,MAAM,QAAQ;AAC/B,YAAM,oBAAoB,MAAM,KAAK,gBAAgB,IAAI,MAAM,KAAK,GAAG,iBAAiB,UAAU;AAClG,wBAAkB;AAClB;AAAA,IACF;AACA,WAAO,OAAO,OAAO,eAAe,EAAE,KAAK;AAAA,EAC7C;AAAA,EAEA,MAAc,gBACZ,OACA,iBACA,YACqC;AACrC,UAAM,UAAwD,MAAM,QAAQ;AAAA,MAC1E,+BAAO,IAAI,OAAO,SAAS;AACzB,cAAM,U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