@xyo-network/sentinel-memory 2.85.5

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.
Files changed (64) hide show
  1. package/LICENSE +165 -0
  2. package/README.md +13 -0
  3. package/dist/browser/MemorySentinel.d.cts +16 -0
  4. package/dist/browser/MemorySentinel.d.cts.map +1 -0
  5. package/dist/browser/MemorySentinel.d.mts +16 -0
  6. package/dist/browser/MemorySentinel.d.mts.map +1 -0
  7. package/dist/browser/MemorySentinel.d.ts +16 -0
  8. package/dist/browser/MemorySentinel.d.ts.map +1 -0
  9. package/dist/browser/SentinelIntervalAutomationWrapper.d.cts +21 -0
  10. package/dist/browser/SentinelIntervalAutomationWrapper.d.cts.map +1 -0
  11. package/dist/browser/SentinelIntervalAutomationWrapper.d.mts +21 -0
  12. package/dist/browser/SentinelIntervalAutomationWrapper.d.mts.map +1 -0
  13. package/dist/browser/SentinelIntervalAutomationWrapper.d.ts +21 -0
  14. package/dist/browser/SentinelIntervalAutomationWrapper.d.ts.map +1 -0
  15. package/dist/browser/SentinelRunner.d.cts +23 -0
  16. package/dist/browser/SentinelRunner.d.cts.map +1 -0
  17. package/dist/browser/SentinelRunner.d.mts +23 -0
  18. package/dist/browser/SentinelRunner.d.mts.map +1 -0
  19. package/dist/browser/SentinelRunner.d.ts +23 -0
  20. package/dist/browser/SentinelRunner.d.ts.map +1 -0
  21. package/dist/browser/index.cjs +310 -0
  22. package/dist/browser/index.cjs.map +1 -0
  23. package/dist/browser/index.d.cts +2 -0
  24. package/dist/browser/index.d.cts.map +1 -0
  25. package/dist/browser/index.d.mts +2 -0
  26. package/dist/browser/index.d.mts.map +1 -0
  27. package/dist/browser/index.d.ts +2 -0
  28. package/dist/browser/index.d.ts.map +1 -0
  29. package/dist/browser/index.js +289 -0
  30. package/dist/browser/index.js.map +1 -0
  31. package/dist/node/MemorySentinel.d.cts +16 -0
  32. package/dist/node/MemorySentinel.d.cts.map +1 -0
  33. package/dist/node/MemorySentinel.d.mts +16 -0
  34. package/dist/node/MemorySentinel.d.mts.map +1 -0
  35. package/dist/node/MemorySentinel.d.ts +16 -0
  36. package/dist/node/MemorySentinel.d.ts.map +1 -0
  37. package/dist/node/SentinelIntervalAutomationWrapper.d.cts +21 -0
  38. package/dist/node/SentinelIntervalAutomationWrapper.d.cts.map +1 -0
  39. package/dist/node/SentinelIntervalAutomationWrapper.d.mts +21 -0
  40. package/dist/node/SentinelIntervalAutomationWrapper.d.mts.map +1 -0
  41. package/dist/node/SentinelIntervalAutomationWrapper.d.ts +21 -0
  42. package/dist/node/SentinelIntervalAutomationWrapper.d.ts.map +1 -0
  43. package/dist/node/SentinelRunner.d.cts +23 -0
  44. package/dist/node/SentinelRunner.d.cts.map +1 -0
  45. package/dist/node/SentinelRunner.d.mts +23 -0
  46. package/dist/node/SentinelRunner.d.mts.map +1 -0
  47. package/dist/node/SentinelRunner.d.ts +23 -0
  48. package/dist/node/SentinelRunner.d.ts.map +1 -0
  49. package/dist/node/index.cjs +322 -0
  50. package/dist/node/index.cjs.map +1 -0
  51. package/dist/node/index.d.cts +2 -0
  52. package/dist/node/index.d.cts.map +1 -0
  53. package/dist/node/index.d.mts +2 -0
  54. package/dist/node/index.d.mts.map +1 -0
  55. package/dist/node/index.d.ts +2 -0
  56. package/dist/node/index.d.ts.map +1 -0
  57. package/dist/node/index.js +297 -0
  58. package/dist/node/index.js.map +1 -0
  59. package/package.json +77 -0
  60. package/src/MemorySentinel.ts +128 -0
  61. package/src/SentinelIntervalAutomationWrapper.ts +76 -0
  62. package/src/SentinelRunner.ts +114 -0
  63. package/src/index.ts +1 -0
  64. package/typedoc.json +5 -0
@@ -0,0 +1,310 @@
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 __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
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
+ });
26
+ module.exports = __toCommonJS(src_exports);
27
+
28
+ // src/MemorySentinel.ts
29
+ var import_promise = require("@xylabs/promise");
30
+ var import_diviner_model = require("@xyo-network/diviner-model");
31
+ var import_sentinel_abstract = require("@xyo-network/sentinel-abstract");
32
+ var import_sentinel_model2 = require("@xyo-network/sentinel-model");
33
+ var import_witness_model = require("@xyo-network/witness-model");
34
+
35
+ // src/SentinelRunner.ts
36
+ var import_assert = require("@xylabs/assert");
37
+ var import_payload_wrapper2 = require("@xyo-network/payload-wrapper");
38
+ var import_sentinel_model = require("@xyo-network/sentinel-model");
39
+
40
+ // src/SentinelIntervalAutomationWrapper.ts
41
+ var import_payload_wrapper = require("@xyo-network/payload-wrapper");
42
+ var SentinelIntervalAutomationWrapper = class extends import_payload_wrapper.PayloadWrapper {
43
+ static {
44
+ __name(this, "SentinelIntervalAutomationWrapper");
45
+ }
46
+ constructor(payload) {
47
+ super(payload);
48
+ }
49
+ get frequencyMillis() {
50
+ const frequency = this.jsonPayload().frequency;
51
+ if (frequency === void 0)
52
+ return Number.POSITIVE_INFINITY;
53
+ const frequencyUnits = this.jsonPayload().frequencyUnits;
54
+ switch (frequencyUnits ?? "hour") {
55
+ case "second": {
56
+ return frequency * 1e3;
57
+ }
58
+ case "minute": {
59
+ return frequency * 60 * 1e3;
60
+ }
61
+ case "hour": {
62
+ return frequency * 60 * 60 * 1e3;
63
+ }
64
+ case "day": {
65
+ return frequency * 24 * 60 * 60 * 1e3;
66
+ }
67
+ default: {
68
+ return Number.POSITIVE_INFINITY;
69
+ }
70
+ }
71
+ }
72
+ get remaining() {
73
+ return this.jsonPayload().remaining ?? Number.POSITIVE_INFINITY;
74
+ }
75
+ next() {
76
+ const now = Date.now();
77
+ const previousStart = this.jsonPayload()?.start ?? now;
78
+ const start = Math.max(previousStart, now);
79
+ const nextStart = start + this.frequencyMillis;
80
+ this.setStart(nextStart);
81
+ this.consumeRemaining();
82
+ this.checkEnd();
83
+ return this;
84
+ }
85
+ checkEnd() {
86
+ if (this.jsonPayload().start > (this.jsonPayload().end ?? Number.POSITIVE_INFINITY)) {
87
+ this.setStart(Number.POSITIVE_INFINITY);
88
+ }
89
+ }
90
+ consumeRemaining(count = 1) {
91
+ const remaining = Math.max(this.remaining - count, 0);
92
+ this.setRemaining(remaining);
93
+ if (remaining <= 0)
94
+ this.setStart(Number.POSITIVE_INFINITY);
95
+ }
96
+ /**
97
+ * Sets the remaining of the wrapped automation
98
+ * @param remaining The remaining time in milliseconds
99
+ */
100
+ setRemaining(remaining) {
101
+ this.obj.remaining = remaining;
102
+ }
103
+ /**
104
+ * Sets the start of the wrapped automation
105
+ * @param start The start time in milliseconds
106
+ */
107
+ setStart(start) {
108
+ this.obj.start = start;
109
+ }
110
+ };
111
+
112
+ // src/SentinelRunner.ts
113
+ var SentinelRunner = class {
114
+ static {
115
+ __name(this, "SentinelRunner");
116
+ }
117
+ _automations = {};
118
+ onTriggerResult;
119
+ sentinel;
120
+ timeoutId;
121
+ constructor(sentinel, automations, onTriggerResult) {
122
+ this.sentinel = sentinel;
123
+ this.onTriggerResult = onTriggerResult;
124
+ if (automations)
125
+ for (const automation of automations)
126
+ this.add(automation);
127
+ }
128
+ get automations() {
129
+ return this._automations;
130
+ }
131
+ get next() {
132
+ return Object.values(this._automations).reduce((previous, current) => {
133
+ if ((0, import_sentinel_model.isSentinelIntervalAutomation)(current) && (0, import_sentinel_model.isSentinelIntervalAutomation)(previous)) {
134
+ return current.start < (previous?.start ?? Number.POSITIVE_INFINITY) ? current : previous;
135
+ }
136
+ return current;
137
+ }, void 0);
138
+ }
139
+ async add(automation, restart = true) {
140
+ const hash = await import_payload_wrapper2.PayloadWrapper.hashAsync(automation);
141
+ this._automations[hash] = automation;
142
+ if (restart)
143
+ await this.restart();
144
+ return hash;
145
+ }
146
+ find(hash) {
147
+ Object.entries(this._automations).find(([key]) => key === hash);
148
+ }
149
+ async remove(hash, restart = true) {
150
+ delete this._automations[hash];
151
+ if (restart)
152
+ await this.restart();
153
+ }
154
+ removeAll() {
155
+ this.stop();
156
+ this._automations = {};
157
+ }
158
+ async restart() {
159
+ this.stop();
160
+ await this.start();
161
+ }
162
+ async start() {
163
+ await Promise.resolve();
164
+ (0, import_assert.assertEx)(this.timeoutId === void 0, "Already started");
165
+ const automation = this.next;
166
+ if ((0, import_sentinel_model.isSentinelIntervalAutomation)(automation)) {
167
+ const now = Date.now();
168
+ const start = Math.max(automation.start ?? now, now);
169
+ const delay = Math.max(start - now, 0);
170
+ if (delay < Number.POSITIVE_INFINITY) {
171
+ this.timeoutId = setTimeout(async () => {
172
+ try {
173
+ await this.trigger(automation);
174
+ this.stop();
175
+ } finally {
176
+ await this.start();
177
+ }
178
+ }, delay);
179
+ }
180
+ }
181
+ }
182
+ stop() {
183
+ if (this.timeoutId) {
184
+ clearTimeout(this.timeoutId);
185
+ this.timeoutId = void 0;
186
+ }
187
+ }
188
+ async update(hash, automation, restart = true) {
189
+ await this.remove(hash, false);
190
+ await this.add(automation, false);
191
+ if (restart)
192
+ await this.restart();
193
+ }
194
+ async trigger(automation) {
195
+ const wrapper = new SentinelIntervalAutomationWrapper(automation);
196
+ await this.remove(await wrapper.hashAsync(), false);
197
+ wrapper.next();
198
+ await this.add(wrapper.jsonPayload(), false);
199
+ const triggerResult = await this.sentinel.report();
200
+ this.onTriggerResult?.(triggerResult);
201
+ }
202
+ };
203
+
204
+ // src/MemorySentinel.ts
205
+ var MemorySentinel = class extends import_sentinel_abstract.AbstractSentinel {
206
+ static {
207
+ __name(this, "MemorySentinel");
208
+ }
209
+ static configSchemas = [
210
+ import_sentinel_model2.SentinelConfigSchema
211
+ ];
212
+ runner;
213
+ async reportHandler(inPayloads = []) {
214
+ await this.started("throw");
215
+ this.logger?.debug(`reportHandler:in: ${JSON.stringify(inPayloads)}`);
216
+ const job = await this.jobPromise;
217
+ let index = 0;
218
+ let previousResults = {};
219
+ while (index < job.tasks.length) {
220
+ const generatedPayloads = await this.generateResults(job.tasks[index], previousResults, inPayloads);
221
+ previousResults = generatedPayloads;
222
+ index++;
223
+ }
224
+ const result = Object.values(previousResults).flat();
225
+ this.logger?.debug(`reportHandler:out: ${JSON.stringify(result)}`);
226
+ return result;
227
+ }
228
+ async start(timeout) {
229
+ if (await super.start(timeout)) {
230
+ if ((this.config.automations?.length ?? 0) > 0) {
231
+ this.runner = new SentinelRunner(this, this.config.automations);
232
+ await this.runner.start();
233
+ }
234
+ return true;
235
+ }
236
+ return false;
237
+ }
238
+ async stop(timeout) {
239
+ if (this.runner) {
240
+ this.runner.stop();
241
+ this.runner = void 0;
242
+ }
243
+ return await super.stop(timeout);
244
+ }
245
+ async generateResults(tasks, previousResults, inPayloads) {
246
+ this.logger?.debug(`generateResults:tasks: ${JSON.stringify(tasks.length)}`);
247
+ this.logger?.debug(`generateResults:previous: ${JSON.stringify(previousResults)}`);
248
+ this.logger?.debug(`generateResults:in: ${JSON.stringify(inPayloads)}`);
249
+ const results = await Promise.allSettled(tasks?.map(async (task) => {
250
+ const input = task.input ?? false;
251
+ const inPayloadsFound = input === true ? inPayloads : input === false ? [] : this.processPreviousResults(previousResults, await this.inputAddresses(input));
252
+ const witness = (0, import_witness_model.asWitnessInstance)(task.module);
253
+ if (witness) {
254
+ const observed = await witness.observe(inPayloadsFound);
255
+ this.logger?.debug(`observed [${witness.id}]: ${JSON.stringify(observed)}`);
256
+ return [
257
+ witness.address,
258
+ observed
259
+ ];
260
+ }
261
+ const diviner = (0, import_diviner_model.asDivinerInstance)(task.module);
262
+ if (diviner) {
263
+ const divined = await diviner.divine(inPayloadsFound);
264
+ this.logger?.debug(`divined [${diviner.id}]: ${JSON.stringify(divined)}`);
265
+ return [
266
+ diviner.address,
267
+ divined
268
+ ];
269
+ }
270
+ const sentinel = (0, import_sentinel_model2.asSentinelInstance)(task.module);
271
+ if (sentinel) {
272
+ const reported = await sentinel.report(inPayloadsFound);
273
+ this.logger?.debug(`reported [${sentinel.id}]: ${JSON.stringify(reported)}`);
274
+ return [
275
+ sentinel.address,
276
+ reported
277
+ ];
278
+ }
279
+ throw new Error("Unsupported module type");
280
+ }));
281
+ const finalResult = {};
282
+ for (const result of results.filter(import_promise.fulfilled)) {
283
+ const [address, payloads] = result.value;
284
+ finalResult[address] = finalResult[address] ?? [];
285
+ finalResult[address].push(...payloads);
286
+ }
287
+ if (this.throwErrors) {
288
+ const errors = results.filter(import_promise.rejected).map((result) => result.reason);
289
+ if (errors.length > 0) {
290
+ throw new Error("At least one module failed");
291
+ }
292
+ }
293
+ this.logger?.debug(`generateResults:out: ${JSON.stringify(finalResult)}`);
294
+ return finalResult;
295
+ }
296
+ async inputAddresses(input) {
297
+ if (Array.isArray(input)) {
298
+ return (await Promise.all(input.map(async (inputItem) => await this.inputAddresses(inputItem)))).flat();
299
+ } else {
300
+ const resolved = await this.resolve(input);
301
+ return resolved ? [
302
+ resolved.address
303
+ ] : [];
304
+ }
305
+ }
306
+ processPreviousResults(payloads, inputs) {
307
+ return inputs.flatMap((input) => payloads[input] ?? []);
308
+ }
309
+ };
310
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/index.ts","../../src/MemorySentinel.ts","../../src/SentinelRunner.ts","../../src/SentinelIntervalAutomationWrapper.ts"],"sourcesContent":["export * from './MemorySentinel'\n","import { Address } from '@xylabs/hex'\nimport { fulfilled, rejected } 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 asSentinelInstance,\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\nimport { SentinelRunner } from './SentinelRunner'\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 private runner?: SentinelRunner\n\n async reportHandler(inPayloads: Payload[] = []): Promise<Payload[]> {\n await this.started('throw')\n this.logger?.debug(`reportHandler:in: ${JSON.stringify(inPayloads)}`)\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 const result = Object.values(previousResults).flat()\n this.logger?.debug(`reportHandler:out: ${JSON.stringify(result)}`)\n return result\n }\n\n override async start(timeout?: number | undefined): Promise<boolean> {\n if (await super.start(timeout)) {\n if ((this.config.automations?.length ?? 0) > 0) {\n this.runner = new SentinelRunner(this, this.config.automations)\n await this.runner.start()\n }\n return true\n }\n return false\n }\n\n override async stop(timeout?: number | undefined): Promise<boolean> {\n if (this.runner) {\n this.runner.stop()\n this.runner = undefined\n }\n return await super.stop(timeout)\n }\n\n private async generateResults(\n tasks: ResolvedTask[],\n previousResults: Record<Address, Payload[]>,\n inPayloads?: Payload[],\n ): Promise<Record<Address, Payload[]>> {\n this.logger?.debug(`generateResults:tasks: ${JSON.stringify(tasks.length)}`)\n this.logger?.debug(`generateResults:previous: ${JSON.stringify(previousResults)}`)\n this.logger?.debug(`generateResults:in: ${JSON.stringify(inPayloads)}`)\n const results: PromiseSettledResult<[Address, Payload[]]>[] = await Promise.allSettled(\n tasks?.map(async (task) => {\n const input = task.input ?? false\n const inPayloadsFound =\n input === true ? inPayloads : input === false ? [] : this.processPreviousResults(previousResults, await this.inputAddresses(input))\n const witness = asWitnessInstance(task.module)\n if (witness) {\n const observed = await witness.observe(inPayloadsFound)\n this.logger?.debug(`observed [${witness.id}]: ${JSON.stringify(observed)}`)\n return [witness.address, observed]\n }\n const diviner = asDivinerInstance(task.module)\n if (diviner) {\n const divined = await diviner.divine(inPayloadsFound)\n this.logger?.debug(`divined [${diviner.id}]: ${JSON.stringify(divined)}`)\n return [diviner.address, divined]\n }\n const sentinel = asSentinelInstance(task.module)\n if (sentinel) {\n const reported = await sentinel.report(inPayloadsFound)\n this.logger?.debug(`reported [${sentinel.id}]: ${JSON.stringify(reported)}`)\n return [sentinel.address, reported]\n }\n throw new Error('Unsupported module type')\n }),\n )\n const finalResult: Record<Address, Payload[]> = {}\n for (const result of results.filter(fulfilled)) {\n const [address, payloads] = result.value\n finalResult[address] = finalResult[address] ?? []\n finalResult[address].push(...payloads)\n }\n if (this.throwErrors) {\n const errors = results.filter(rejected).map((result) => result.reason)\n if (errors.length > 0) {\n throw new Error('At least one module failed')\n }\n }\n this.logger?.debug(`generateResults:out: ${JSON.stringify(finalResult)}`)\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.flatMap((input) => payloads[input] ?? [])\n }\n}\n","import { assertEx } from '@xylabs/assert'\nimport { Payload } from '@xyo-network/payload-model'\nimport { PayloadWrapper } from '@xyo-network/payload-wrapper'\nimport {\n isSentinelIntervalAutomation,\n SentinelAutomationPayload,\n SentinelInstance,\n SentinelIntervalAutomationPayload,\n} from '@xyo-network/sentinel-model'\n\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 if (automations) for (const automation of automations) this.add(automation)\n }\n\n get automations() {\n return this._automations\n }\n\n private get next() {\n // eslint-disable-next-line unicorn/no-array-reduce\n return Object.values(this._automations).reduce<SentinelAutomationPayload | undefined>((previous, current) => {\n if (isSentinelIntervalAutomation(current) && isSentinelIntervalAutomation(previous)) {\n return current.start < (previous?.start ?? Number.POSITIVE_INFINITY) ? current : previous\n }\n return current\n // eslint-disable-next-line unicorn/no-useless-undefined\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 // NOTE: Keep async to match module start signature\n await Promise.resolve()\n assertEx(this.timeoutId === undefined, 'Already started')\n const automation = this.next\n if (isSentinelIntervalAutomation(automation)) {\n const now = Date.now()\n const start = Math.max(automation.start ?? now, now)\n const delay = Math.max(start - now, 0)\n if (delay < Number.POSITIVE_INFINITY) {\n this.timeoutId = setTimeout(async () => {\n try {\n // Run the automation\n await this.trigger(automation)\n this.stop()\n } finally {\n // No matter what start the next automation\n await this.start()\n }\n }, delay)\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 { PayloadWrapper } from '@xyo-network/payload-wrapper'\nimport { SentinelIntervalAutomationPayload } from '@xyo-network/sentinel-model'\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 Number.POSITIVE_INFINITY\n const frequencyUnits = this.jsonPayload().frequencyUnits\n switch (frequencyUnits ?? 'hour') {\n case 'second': {\n return frequency * 1000\n }\n case 'minute': {\n return frequency * 60 * 1000\n }\n case 'hour': {\n return frequency * 60 * 60 * 1000\n }\n case 'day': {\n return frequency * 24 * 60 * 60 * 1000\n }\n default: {\n return Number.POSITIVE_INFINITY\n }\n }\n }\n\n protected get remaining() {\n return this.jsonPayload().remaining ?? Number.POSITIVE_INFINITY\n }\n\n next() {\n const now = Date.now()\n const previousStart = this.jsonPayload()?.start ?? now\n const start = Math.max(previousStart, now)\n const nextStart = start + this.frequencyMillis\n this.setStart(nextStart)\n this.consumeRemaining()\n this.checkEnd()\n return this\n }\n\n protected checkEnd() {\n if (this.jsonPayload().start > (this.jsonPayload().end ?? Number.POSITIVE_INFINITY)) {\n this.setStart(Number.POSITIVE_INFINITY)\n }\n }\n\n protected consumeRemaining(count = 1) {\n const remaining = Math.max(this.remaining - count, 0)\n this.setRemaining(remaining)\n if (remaining <= 0) this.setStart(Number.POSITIVE_INFINITY)\n }\n\n /**\n * Sets the remaining of the wrapped automation\n * @param remaining The remaining time in milliseconds\n */\n protected setRemaining(remaining: number) {\n this.obj.remaining = remaining\n }\n\n /**\n * Sets the start of the wrapped automation\n * @param start The start time in milliseconds\n */\n protected setStart(start: number) {\n this.obj.start = start\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;ACCA,qBAAoC;AACpC,2BAAkC;AAGlC,+BAAiC;AACjC,IAAAA,yBAQO;AACP,2BAAkC;;;ACflC,oBAAyB;AAEzB,IAAAC,0BAA+B;AAC/B,4BAKO;;;ACRP,6BAA+B;AAGxB,IAAMC,oCAAN,cAEGC,sCAAAA;EALV,OAKUA;;;EACRC,YAAYC,SAAY;AACtB,UAAMA,OAAAA;EACR;EAEA,IAAcC,kBAAkB;AAC9B,UAAMC,YAAY,KAAKC,YAAW,EAAGD;AACrC,QAAIA,cAAcE;AAAW,aAAOC,OAAOC;AAC3C,UAAMC,iBAAiB,KAAKJ,YAAW,EAAGI;AAC1C,YAAQA,kBAAkB,QAAA;MACxB,KAAK,UAAU;AACb,eAAOL,YAAY;MACrB;MACA,KAAK,UAAU;AACb,eAAOA,YAAY,KAAK;MAC1B;MACA,KAAK,QAAQ;AACX,eAAOA,YAAY,KAAK,KAAK;MAC/B;MACA,KAAK,OAAO;AACV,eAAOA,YAAY,KAAK,KAAK,KAAK;MACpC;MACA,SAAS;AACP,eAAOG,OAAOC;MAChB;IACF;EACF;EAEA,IAAcE,YAAY;AACxB,WAAO,KAAKL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ACpF,WAAOA,OAAOC,QAAQ,CAACrC,UAAUuB,SAASvB,KAAAA,KAAU,CAAA,CAAE;EACxD;AACF;","names":["import_sentinel_model","import_payload_wrapper","SentinelIntervalAutomationWrapper","PayloadWrapper","constructor","payload","frequencyMillis","frequency","jsonPayload","undefined","Number","POSITIVE_INFINITY","frequencyUnits","remaining","next","now","Date","previousStart","start","Math","max","nextStart","setStart","consumeRemaining","checkEnd","end","count","setRemaining","obj","SentinelRunner","_automations","onTriggerResult","sentinel","timeoutId","constructor","automations","automation","add","next","Object","values","reduce","previous","current","isSentinelIntervalAutomation","start","Number","POSITIVE_INFINITY","undefined","restart","hash","PayloadWrapper","hashAsync","find","entries","key","remove","removeAll","stop","Promise","resolve","assertEx","now","Date","Math","max","delay","setTimeout","trigger","clearTimeout","update","wrapper","SentinelIntervalAutomationWrapper","jsonPayload","triggerResult","report","MemorySentinel","AbstractSentinel","configSchemas","SentinelConfigSchema","runner","reportHandler","inPayloads","started","logger","debug","JSON","stringify","job","jobPromise","index","previousResults","tasks","length","generatedPayloads","generateResults","result","Object","values","flat","start","timeout","config","automations","SentinelRunner","stop","undefined","results","Promise","allSettled","map","task","input","inPayloadsFound","processPreviousResults","inputAddresses","witness","asWitnessInstance","module","observed","observe","id","address","diviner","asDivinerInstance","divined","divine","sentinel","asSentinelInstance","reported","report","Error","finalResult","filter","fulfilled","payloads","value","push","throwErrors","errors","rejected","reason","Array","isArray","all","inputItem","resolved","resolve","inputs","flatMap"]}
@@ -0,0 +1,2 @@
1
+ export * from './MemorySentinel';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA"}
@@ -0,0 +1,2 @@
1
+ export * from './MemorySentinel';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA"}
@@ -0,0 +1,2 @@
1
+ export * from './MemorySentinel';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA"}
@@ -0,0 +1,289 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // src/MemorySentinel.ts
5
+ import { fulfilled, rejected } from "@xylabs/promise";
6
+ import { asDivinerInstance } from "@xyo-network/diviner-model";
7
+ import { AbstractSentinel } from "@xyo-network/sentinel-abstract";
8
+ import { asSentinelInstance, SentinelConfigSchema } from "@xyo-network/sentinel-model";
9
+ import { asWitnessInstance } from "@xyo-network/witness-model";
10
+
11
+ // src/SentinelRunner.ts
12
+ import { assertEx } from "@xylabs/assert";
13
+ import { PayloadWrapper as PayloadWrapper2 } from "@xyo-network/payload-wrapper";
14
+ import { isSentinelIntervalAutomation } from "@xyo-network/sentinel-model";
15
+
16
+ // src/SentinelIntervalAutomationWrapper.ts
17
+ import { PayloadWrapper } from "@xyo-network/payload-wrapper";
18
+ var SentinelIntervalAutomationWrapper = class extends PayloadWrapper {
19
+ static {
20
+ __name(this, "SentinelIntervalAutomationWrapper");
21
+ }
22
+ constructor(payload) {
23
+ super(payload);
24
+ }
25
+ get frequencyMillis() {
26
+ const frequency = this.jsonPayload().frequency;
27
+ if (frequency === void 0)
28
+ return Number.POSITIVE_INFINITY;
29
+ const frequencyUnits = this.jsonPayload().frequencyUnits;
30
+ switch (frequencyUnits ?? "hour") {
31
+ case "second": {
32
+ return frequency * 1e3;
33
+ }
34
+ case "minute": {
35
+ return frequency * 60 * 1e3;
36
+ }
37
+ case "hour": {
38
+ return frequency * 60 * 60 * 1e3;
39
+ }
40
+ case "day": {
41
+ return frequency * 24 * 60 * 60 * 1e3;
42
+ }
43
+ default: {
44
+ return Number.POSITIVE_INFINITY;
45
+ }
46
+ }
47
+ }
48
+ get remaining() {
49
+ return this.jsonPayload().remaining ?? Number.POSITIVE_INFINITY;
50
+ }
51
+ next() {
52
+ const now = Date.now();
53
+ const previousStart = this.jsonPayload()?.start ?? now;
54
+ const start = Math.max(previousStart, now);
55
+ const nextStart = start + this.frequencyMillis;
56
+ this.setStart(nextStart);
57
+ this.consumeRemaining();
58
+ this.checkEnd();
59
+ return this;
60
+ }
61
+ checkEnd() {
62
+ if (this.jsonPayload().start > (this.jsonPayload().end ?? Number.POSITIVE_INFINITY)) {
63
+ this.setStart(Number.POSITIVE_INFINITY);
64
+ }
65
+ }
66
+ consumeRemaining(count = 1) {
67
+ const remaining = Math.max(this.remaining - count, 0);
68
+ this.setRemaining(remaining);
69
+ if (remaining <= 0)
70
+ this.setStart(Number.POSITIVE_INFINITY);
71
+ }
72
+ /**
73
+ * Sets the remaining of the wrapped automation
74
+ * @param remaining The remaining time in milliseconds
75
+ */
76
+ setRemaining(remaining) {
77
+ this.obj.remaining = remaining;
78
+ }
79
+ /**
80
+ * Sets the start of the wrapped automation
81
+ * @param start The start time in milliseconds
82
+ */
83
+ setStart(start) {
84
+ this.obj.start = start;
85
+ }
86
+ };
87
+
88
+ // src/SentinelRunner.ts
89
+ var SentinelRunner = class {
90
+ static {
91
+ __name(this, "SentinelRunner");
92
+ }
93
+ _automations = {};
94
+ onTriggerResult;
95
+ sentinel;
96
+ timeoutId;
97
+ constructor(sentinel, automations, onTriggerResult) {
98
+ this.sentinel = sentinel;
99
+ this.onTriggerResult = onTriggerResult;
100
+ if (automations)
101
+ for (const automation of automations)
102
+ this.add(automation);
103
+ }
104
+ get automations() {
105
+ return this._automations;
106
+ }
107
+ get next() {
108
+ return Object.values(this._automations).reduce((previous, current) => {
109
+ if (isSentinelIntervalAutomation(current) && isSentinelIntervalAutomation(previous)) {
110
+ return current.start < (previous?.start ?? Number.POSITIVE_INFINITY) ? current : previous;
111
+ }
112
+ return current;
113
+ }, void 0);
114
+ }
115
+ async add(automation, restart = true) {
116
+ const hash = await PayloadWrapper2.hashAsync(automation);
117
+ this._automations[hash] = automation;
118
+ if (restart)
119
+ await this.restart();
120
+ return hash;
121
+ }
122
+ find(hash) {
123
+ Object.entries(this._automations).find(([key]) => key === hash);
124
+ }
125
+ async remove(hash, restart = true) {
126
+ delete this._automations[hash];
127
+ if (restart)
128
+ await this.restart();
129
+ }
130
+ removeAll() {
131
+ this.stop();
132
+ this._automations = {};
133
+ }
134
+ async restart() {
135
+ this.stop();
136
+ await this.start();
137
+ }
138
+ async start() {
139
+ await Promise.resolve();
140
+ assertEx(this.timeoutId === void 0, "Already started");
141
+ const automation = this.next;
142
+ if (isSentinelIntervalAutomation(automation)) {
143
+ const now = Date.now();
144
+ const start = Math.max(automation.start ?? now, now);
145
+ const delay = Math.max(start - now, 0);
146
+ if (delay < Number.POSITIVE_INFINITY) {
147
+ this.timeoutId = setTimeout(async () => {
148
+ try {
149
+ await this.trigger(automation);
150
+ this.stop();
151
+ } finally {
152
+ await this.start();
153
+ }
154
+ }, delay);
155
+ }
156
+ }
157
+ }
158
+ stop() {
159
+ if (this.timeoutId) {
160
+ clearTimeout(this.timeoutId);
161
+ this.timeoutId = void 0;
162
+ }
163
+ }
164
+ async update(hash, automation, restart = true) {
165
+ await this.remove(hash, false);
166
+ await this.add(automation, false);
167
+ if (restart)
168
+ await this.restart();
169
+ }
170
+ async trigger(automation) {
171
+ const wrapper = new SentinelIntervalAutomationWrapper(automation);
172
+ await this.remove(await wrapper.hashAsync(), false);
173
+ wrapper.next();
174
+ await this.add(wrapper.jsonPayload(), false);
175
+ const triggerResult = await this.sentinel.report();
176
+ this.onTriggerResult?.(triggerResult);
177
+ }
178
+ };
179
+
180
+ // src/MemorySentinel.ts
181
+ var MemorySentinel = class extends AbstractSentinel {
182
+ static {
183
+ __name(this, "MemorySentinel");
184
+ }
185
+ static configSchemas = [
186
+ SentinelConfigSchema
187
+ ];
188
+ runner;
189
+ async reportHandler(inPayloads = []) {
190
+ await this.started("throw");
191
+ this.logger?.debug(`reportHandler:in: ${JSON.stringify(inPayloads)}`);
192
+ const job = await this.jobPromise;
193
+ let index = 0;
194
+ let previousResults = {};
195
+ while (index < job.tasks.length) {
196
+ const generatedPayloads = await this.generateResults(job.tasks[index], previousResults, inPayloads);
197
+ previousResults = generatedPayloads;
198
+ index++;
199
+ }
200
+ const result = Object.values(previousResults).flat();
201
+ this.logger?.debug(`reportHandler:out: ${JSON.stringify(result)}`);
202
+ return result;
203
+ }
204
+ async start(timeout) {
205
+ if (await super.start(timeout)) {
206
+ if ((this.config.automations?.length ?? 0) > 0) {
207
+ this.runner = new SentinelRunner(this, this.config.automations);
208
+ await this.runner.start();
209
+ }
210
+ return true;
211
+ }
212
+ return false;
213
+ }
214
+ async stop(timeout) {
215
+ if (this.runner) {
216
+ this.runner.stop();
217
+ this.runner = void 0;
218
+ }
219
+ return await super.stop(timeout);
220
+ }
221
+ async generateResults(tasks, previousResults, inPayloads) {
222
+ this.logger?.debug(`generateResults:tasks: ${JSON.stringify(tasks.length)}`);
223
+ this.logger?.debug(`generateResults:previous: ${JSON.stringify(previousResults)}`);
224
+ this.logger?.debug(`generateResults:in: ${JSON.stringify(inPayloads)}`);
225
+ const results = await Promise.allSettled(tasks?.map(async (task) => {
226
+ const input = task.input ?? false;
227
+ const inPayloadsFound = input === true ? inPayloads : input === false ? [] : this.processPreviousResults(previousResults, await this.inputAddresses(input));
228
+ const witness = asWitnessInstance(task.module);
229
+ if (witness) {
230
+ const observed = await witness.observe(inPayloadsFound);
231
+ this.logger?.debug(`observed [${witness.id}]: ${JSON.stringify(observed)}`);
232
+ return [
233
+ witness.address,
234
+ observed
235
+ ];
236
+ }
237
+ const diviner = asDivinerInstance(task.module);
238
+ if (diviner) {
239
+ const divined = await diviner.divine(inPayloadsFound);
240
+ this.logger?.debug(`divined [${diviner.id}]: ${JSON.stringify(divined)}`);
241
+ return [
242
+ diviner.address,
243
+ divined
244
+ ];
245
+ }
246
+ const sentinel = asSentinelInstance(task.module);
247
+ if (sentinel) {
248
+ const reported = await sentinel.report(inPayloadsFound);
249
+ this.logger?.debug(`reported [${sentinel.id}]: ${JSON.stringify(reported)}`);
250
+ return [
251
+ sentinel.address,
252
+ reported
253
+ ];
254
+ }
255
+ throw new Error("Unsupported module type");
256
+ }));
257
+ const finalResult = {};
258
+ for (const result of results.filter(fulfilled)) {
259
+ const [address, payloads] = result.value;
260
+ finalResult[address] = finalResult[address] ?? [];
261
+ finalResult[address].push(...payloads);
262
+ }
263
+ if (this.throwErrors) {
264
+ const errors = results.filter(rejected).map((result) => result.reason);
265
+ if (errors.length > 0) {
266
+ throw new Error("At least one module failed");
267
+ }
268
+ }
269
+ this.logger?.debug(`generateResults:out: ${JSON.stringify(finalResult)}`);
270
+ return finalResult;
271
+ }
272
+ async inputAddresses(input) {
273
+ if (Array.isArray(input)) {
274
+ return (await Promise.all(input.map(async (inputItem) => await this.inputAddresses(inputItem)))).flat();
275
+ } else {
276
+ const resolved = await this.resolve(input);
277
+ return resolved ? [
278
+ resolved.address
279
+ ] : [];
280
+ }
281
+ }
282
+ processPreviousResults(payloads, inputs) {
283
+ return inputs.flatMap((input) => payloads[input] ?? []);
284
+ }
285
+ };
286
+ export {
287
+ MemorySentinel
288
+ };
289
+ //# sourceMappingURL=index.js.map