@xyo-network/sentinel 2.85.4 → 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 (74) hide show
  1. package/dist/browser/index.cjs +1 -283
  2. package/dist/browser/index.cjs.map +1 -1
  3. package/dist/browser/index.d.cts +1 -4
  4. package/dist/browser/index.d.cts.map +1 -1
  5. package/dist/browser/index.d.mts +1 -4
  6. package/dist/browser/index.d.mts.map +1 -1
  7. package/dist/browser/index.d.ts +1 -4
  8. package/dist/browser/index.d.ts.map +1 -1
  9. package/dist/browser/index.js +1 -286
  10. package/dist/browser/index.js.map +1 -1
  11. package/dist/node/index.cjs +2 -290
  12. package/dist/node/index.cjs.map +1 -1
  13. package/dist/node/index.d.cts +1 -4
  14. package/dist/node/index.d.cts.map +1 -1
  15. package/dist/node/index.d.mts +1 -4
  16. package/dist/node/index.d.mts.map +1 -1
  17. package/dist/node/index.d.ts +1 -4
  18. package/dist/node/index.d.ts.map +1 -1
  19. package/dist/node/index.js +1 -289
  20. package/dist/node/index.js.map +1 -1
  21. package/package.json +4 -21
  22. package/src/index.ts +1 -4
  23. package/dist/browser/MemorySentinel.d.cts +0 -16
  24. package/dist/browser/MemorySentinel.d.cts.map +0 -1
  25. package/dist/browser/MemorySentinel.d.mts +0 -16
  26. package/dist/browser/MemorySentinel.d.mts.map +0 -1
  27. package/dist/browser/MemorySentinel.d.ts +0 -16
  28. package/dist/browser/MemorySentinel.d.ts.map +0 -1
  29. package/dist/browser/SentinelIntervalAutomationWrapper.d.cts +0 -21
  30. package/dist/browser/SentinelIntervalAutomationWrapper.d.cts.map +0 -1
  31. package/dist/browser/SentinelIntervalAutomationWrapper.d.mts +0 -21
  32. package/dist/browser/SentinelIntervalAutomationWrapper.d.mts.map +0 -1
  33. package/dist/browser/SentinelIntervalAutomationWrapper.d.ts +0 -21
  34. package/dist/browser/SentinelIntervalAutomationWrapper.d.ts.map +0 -1
  35. package/dist/browser/SentinelRunner.d.cts +0 -23
  36. package/dist/browser/SentinelRunner.d.cts.map +0 -1
  37. package/dist/browser/SentinelRunner.d.mts +0 -23
  38. package/dist/browser/SentinelRunner.d.mts.map +0 -1
  39. package/dist/browser/SentinelRunner.d.ts +0 -23
  40. package/dist/browser/SentinelRunner.d.ts.map +0 -1
  41. package/dist/browser/Wrapper.d.cts +0 -14
  42. package/dist/browser/Wrapper.d.cts.map +0 -1
  43. package/dist/browser/Wrapper.d.mts +0 -14
  44. package/dist/browser/Wrapper.d.mts.map +0 -1
  45. package/dist/browser/Wrapper.d.ts +0 -14
  46. package/dist/browser/Wrapper.d.ts.map +0 -1
  47. package/dist/node/MemorySentinel.d.cts +0 -16
  48. package/dist/node/MemorySentinel.d.cts.map +0 -1
  49. package/dist/node/MemorySentinel.d.mts +0 -16
  50. package/dist/node/MemorySentinel.d.mts.map +0 -1
  51. package/dist/node/MemorySentinel.d.ts +0 -16
  52. package/dist/node/MemorySentinel.d.ts.map +0 -1
  53. package/dist/node/SentinelIntervalAutomationWrapper.d.cts +0 -21
  54. package/dist/node/SentinelIntervalAutomationWrapper.d.cts.map +0 -1
  55. package/dist/node/SentinelIntervalAutomationWrapper.d.mts +0 -21
  56. package/dist/node/SentinelIntervalAutomationWrapper.d.mts.map +0 -1
  57. package/dist/node/SentinelIntervalAutomationWrapper.d.ts +0 -21
  58. package/dist/node/SentinelIntervalAutomationWrapper.d.ts.map +0 -1
  59. package/dist/node/SentinelRunner.d.cts +0 -23
  60. package/dist/node/SentinelRunner.d.cts.map +0 -1
  61. package/dist/node/SentinelRunner.d.mts +0 -23
  62. package/dist/node/SentinelRunner.d.mts.map +0 -1
  63. package/dist/node/SentinelRunner.d.ts +0 -23
  64. package/dist/node/SentinelRunner.d.ts.map +0 -1
  65. package/dist/node/Wrapper.d.cts +0 -14
  66. package/dist/node/Wrapper.d.cts.map +0 -1
  67. package/dist/node/Wrapper.d.mts +0 -14
  68. package/dist/node/Wrapper.d.mts.map +0 -1
  69. package/dist/node/Wrapper.d.ts +0 -14
  70. package/dist/node/Wrapper.d.ts.map +0 -1
  71. package/src/MemorySentinel.ts +0 -115
  72. package/src/SentinelIntervalAutomationWrapper.ts +0 -76
  73. package/src/SentinelRunner.ts +0 -114
  74. package/src/Wrapper.ts +0 -36
@@ -3,10 +3,6 @@ var __defProp = Object.defineProperty;
3
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
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
6
  var __copyProps = (to, from, except, desc) => {
11
7
  if (from && typeof from === "object" || typeof from === "function") {
12
8
  for (let key of __getOwnPropNames(from))
@@ -20,286 +16,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
16
 
21
17
  // src/index.ts
22
18
  var src_exports = {};
23
- __export(src_exports, {
24
- MemorySentinel: () => MemorySentinel,
25
- SentinelIntervalAutomationWrapper: () => SentinelIntervalAutomationWrapper,
26
- SentinelRunner: () => SentinelRunner,
27
- SentinelWrapper: () => SentinelWrapper
28
- });
29
19
  module.exports = __toCommonJS(src_exports);
30
-
31
- // src/MemorySentinel.ts
32
- var import_promise = require("@xylabs/promise");
33
- var import_diviner_model = require("@xyo-network/diviner-model");
34
- var import_sentinel_abstract = require("@xyo-network/sentinel-abstract");
35
- var import_sentinel_model2 = require("@xyo-network/sentinel-model");
36
- var import_witness_model = require("@xyo-network/witness-model");
37
-
38
- // src/SentinelRunner.ts
39
- var import_assert = require("@xylabs/assert");
40
- var import_payload_wrapper2 = require("@xyo-network/payload-wrapper");
41
- var import_sentinel_model = require("@xyo-network/sentinel-model");
42
-
43
- // src/SentinelIntervalAutomationWrapper.ts
44
- var import_payload_wrapper = require("@xyo-network/payload-wrapper");
45
- var SentinelIntervalAutomationWrapper = class extends import_payload_wrapper.PayloadWrapper {
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
- _automations = {};
115
- onTriggerResult;
116
- sentinel;
117
- timeoutId;
118
- constructor(sentinel, automations, onTriggerResult) {
119
- this.sentinel = sentinel;
120
- this.onTriggerResult = onTriggerResult;
121
- if (automations)
122
- for (const automation of automations)
123
- this.add(automation);
124
- }
125
- get automations() {
126
- return this._automations;
127
- }
128
- get next() {
129
- return Object.values(this._automations).reduce((previous, current) => {
130
- if ((0, import_sentinel_model.isSentinelIntervalAutomation)(current) && (0, import_sentinel_model.isSentinelIntervalAutomation)(previous)) {
131
- return current.start < (previous?.start ?? Number.POSITIVE_INFINITY) ? current : previous;
132
- }
133
- return current;
134
- }, void 0);
135
- }
136
- async add(automation, restart = true) {
137
- const hash = await import_payload_wrapper2.PayloadWrapper.hashAsync(automation);
138
- this._automations[hash] = automation;
139
- if (restart)
140
- await this.restart();
141
- return hash;
142
- }
143
- find(hash) {
144
- Object.entries(this._automations).find(([key]) => key === hash);
145
- }
146
- async remove(hash, restart = true) {
147
- delete this._automations[hash];
148
- if (restart)
149
- await this.restart();
150
- }
151
- removeAll() {
152
- this.stop();
153
- this._automations = {};
154
- }
155
- async restart() {
156
- this.stop();
157
- await this.start();
158
- }
159
- async start() {
160
- await Promise.resolve();
161
- (0, import_assert.assertEx)(this.timeoutId === void 0, "Already started");
162
- const automation = this.next;
163
- if ((0, import_sentinel_model.isSentinelIntervalAutomation)(automation)) {
164
- const now = Date.now();
165
- const start = Math.max(automation.start ?? now, now);
166
- const delay = Math.max(start - now, 0);
167
- if (delay < Number.POSITIVE_INFINITY) {
168
- this.timeoutId = setTimeout(async () => {
169
- try {
170
- await this.trigger(automation);
171
- this.stop();
172
- } finally {
173
- await this.start();
174
- }
175
- }, delay);
176
- }
177
- }
178
- }
179
- stop() {
180
- if (this.timeoutId) {
181
- clearTimeout(this.timeoutId);
182
- this.timeoutId = void 0;
183
- }
184
- }
185
- async update(hash, automation, restart = true) {
186
- await this.remove(hash, false);
187
- await this.add(automation, false);
188
- if (restart)
189
- await this.restart();
190
- }
191
- async trigger(automation) {
192
- const wrapper = new SentinelIntervalAutomationWrapper(automation);
193
- await this.remove(await wrapper.hashAsync(), false);
194
- wrapper.next();
195
- await this.add(wrapper.jsonPayload(), false);
196
- const triggerResult = await this.sentinel.report();
197
- this.onTriggerResult?.(triggerResult);
198
- }
199
- };
200
-
201
- // src/MemorySentinel.ts
202
- var MemorySentinel = class extends import_sentinel_abstract.AbstractSentinel {
203
- static configSchemas = [import_sentinel_model2.SentinelConfigSchema];
204
- runner;
205
- async reportHandler(inPayloads = []) {
206
- await this.started("throw");
207
- const job = await this.jobPromise;
208
- let index = 0;
209
- let previousResults = {};
210
- while (index < job.tasks.length) {
211
- const generatedPayloads = await this.generateResults(job.tasks[index], previousResults, inPayloads);
212
- previousResults = generatedPayloads;
213
- index++;
214
- }
215
- return Object.values(previousResults).flat();
216
- }
217
- async start(timeout) {
218
- if (await super.start(timeout)) {
219
- if ((this.config.automations?.length ?? 0) > 0) {
220
- this.runner = new SentinelRunner(this, this.config.automations);
221
- await this.runner.start();
222
- }
223
- return true;
224
- }
225
- return false;
226
- }
227
- async stop(timeout) {
228
- if (this.runner) {
229
- this.runner.stop();
230
- this.runner = void 0;
231
- }
232
- return await super.stop(timeout);
233
- }
234
- async generateResults(tasks, previousResults, inPayloads) {
235
- const results = await Promise.allSettled(
236
- tasks?.map(async (task) => {
237
- const input = task.input ?? false;
238
- const inPayloadsFound = input === true ? inPayloads : input === false ? [] : this.processPreviousResults(previousResults, await this.inputAddresses(input));
239
- const witness = (0, import_witness_model.asWitnessInstance)(task.module);
240
- if (witness) {
241
- return [witness.address, await witness.observe(inPayloadsFound)];
242
- }
243
- const diviner = (0, import_diviner_model.asDivinerInstance)(task.module);
244
- if (diviner) {
245
- return [diviner.address, await diviner.divine(inPayloadsFound)];
246
- }
247
- const sentinel = (0, import_sentinel_model2.asSentinelInstance)(task.module);
248
- if (sentinel) {
249
- return [sentinel.address, await sentinel.report(inPayloadsFound)];
250
- }
251
- throw new Error("Unsupported module type");
252
- })
253
- );
254
- const finalResult = {};
255
- for (const result of results.filter(import_promise.fulfilled)) {
256
- const [address, payloads] = result.value;
257
- finalResult[address] = finalResult[address] ?? [];
258
- finalResult[address].push(...payloads);
259
- }
260
- if (this.throwErrors) {
261
- const errors = results.filter(import_promise.rejected).map((result) => result.reason);
262
- if (errors.length > 0) {
263
- throw new Error("At least one module failed");
264
- }
265
- }
266
- return finalResult;
267
- }
268
- async inputAddresses(input) {
269
- if (Array.isArray(input)) {
270
- return (await Promise.all(input.map(async (inputItem) => await this.inputAddresses(inputItem)))).flat();
271
- } else {
272
- const resolved = await this.resolve(input);
273
- return resolved ? [resolved.address] : [];
274
- }
275
- }
276
- processPreviousResults(payloads, inputs) {
277
- return inputs.flatMap((input) => payloads[input] ?? []);
278
- }
279
- };
280
-
281
- // src/Wrapper.ts
282
- var import_module_wrapper = require("@xyo-network/module-wrapper");
283
- var import_sentinel_model3 = require("@xyo-network/sentinel-model");
284
- (0, import_module_wrapper.constructableModuleWrapper)();
285
- var SentinelWrapper = class extends import_module_wrapper.ModuleWrapper {
286
- static instanceIdentityCheck = import_sentinel_model3.isSentinelInstance;
287
- static moduleIdentityCheck = import_sentinel_model3.isSentinelModule;
288
- static requiredQueries = [import_sentinel_model3.SentinelReportQuerySchema, ...super.requiredQueries];
289
- archivists() {
290
- throw new Error("Not supported");
291
- }
292
- async report(payloads) {
293
- const queryPayload = { schema: import_sentinel_model3.SentinelReportQuerySchema };
294
- const result = await this.sendQuery(queryPayload, payloads);
295
- return result;
296
- }
297
- witnesses() {
298
- throw new Error("Not supported");
299
- }
300
- };
301
-
302
- // src/index.ts
303
20
  __reExport(src_exports, require("@xyo-network/sentinel-abstract"), module.exports);
21
+ __reExport(src_exports, require("@xyo-network/sentinel-memory"), module.exports);
304
22
  __reExport(src_exports, require("@xyo-network/sentinel-model"), module.exports);
305
23
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts","../../src/MemorySentinel.ts","../../src/SentinelRunner.ts","../../src/SentinelIntervalAutomationWrapper.ts","../../src/Wrapper.ts"],"sourcesContent":["export * 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 { 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 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 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 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 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 const sentinel = asSentinelInstance(task.module)\n if (sentinel) {\n return [sentinel.address, await sentinel.report(inPayloadsFound)]\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 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","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 new 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 new Error('Not supported')\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;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,IAAM,oCAAN,cAEG,sCAAkB;AAAA,EAC1B,YAAY,SAAY;AACtB,UAAM,OAAO;AAAA,EACf;AAAA,EAEA,IAAc,kBAAkB;AAC9B,UAAM,YAAY,KAAK,YAAY,EAAE;AACrC,QAAI,cAAc;AAAW,aAAO,OAAO;AAC3C,UAAM,iBAAiB,KAA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AE,QAAQ,iDAA0B;AAC9E,UAAM,SAAS,MAAM,KAAK,UAAU,cAAc,QAAQ;AAC1D,WAAO;AAAA,EACT;AAAA,EAEA,YAAwC;AACtC,UAAM,IAAI,MAAM,eAAe;AAAA,EACjC;AACF;;;AJ/BA,wBAAc,2CAJd;AAKA,wBAAc,wCALd;","names":["import_sentinel_model","import_payload_wrapper","import_sentinel_model"]}
1
+ {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export * from '@xyo-network/sentinel-abstract'\nexport * from '@xyo-network/sentinel-memory'\nexport * from '@xyo-network/sentinel-model'\n"],"mappings":";;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,wBAAc,2CAAd;AACA,wBAAc,yCADd;AAEA,wBAAc,wCAFd;","names":[]}
@@ -1,7 +1,4 @@
1
- export * from './MemorySentinel';
2
- export * from './SentinelIntervalAutomationWrapper';
3
- export * from './SentinelRunner';
4
- export * from './Wrapper';
5
1
  export * from '@xyo-network/sentinel-abstract';
2
+ export * from '@xyo-network/sentinel-memory';
6
3
  export * from '@xyo-network/sentinel-model';
7
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA;AAChC,cAAc,qCAAqC,CAAA;AACnD,cAAc,kBAAkB,CAAA;AAChC,cAAc,WAAW,CAAA;AACzB,cAAc,gCAAgC,CAAA;AAC9C,cAAc,6BAA6B,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gCAAgC,CAAA;AAC9C,cAAc,8BAA8B,CAAA;AAC5C,cAAc,6BAA6B,CAAA"}
@@ -1,7 +1,4 @@
1
- export * from './MemorySentinel';
2
- export * from './SentinelIntervalAutomationWrapper';
3
- export * from './SentinelRunner';
4
- export * from './Wrapper';
5
1
  export * from '@xyo-network/sentinel-abstract';
2
+ export * from '@xyo-network/sentinel-memory';
6
3
  export * from '@xyo-network/sentinel-model';
7
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA;AAChC,cAAc,qCAAqC,CAAA;AACnD,cAAc,kBAAkB,CAAA;AAChC,cAAc,WAAW,CAAA;AACzB,cAAc,gCAAgC,CAAA;AAC9C,cAAc,6BAA6B,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gCAAgC,CAAA;AAC9C,cAAc,8BAA8B,CAAA;AAC5C,cAAc,6BAA6B,CAAA"}
@@ -1,7 +1,4 @@
1
- export * from './MemorySentinel';
2
- export * from './SentinelIntervalAutomationWrapper';
3
- export * from './SentinelRunner';
4
- export * from './Wrapper';
5
1
  export * from '@xyo-network/sentinel-abstract';
2
+ export * from '@xyo-network/sentinel-memory';
6
3
  export * from '@xyo-network/sentinel-model';
7
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA;AAChC,cAAc,qCAAqC,CAAA;AACnD,cAAc,kBAAkB,CAAA;AAChC,cAAc,WAAW,CAAA;AACzB,cAAc,gCAAgC,CAAA;AAC9C,cAAc,6BAA6B,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gCAAgC,CAAA;AAC9C,cAAc,8BAA8B,CAAA;AAC5C,cAAc,6BAA6B,CAAA"}
@@ -1,290 +1,5 @@
1
- // src/MemorySentinel.ts
2
- import { fulfilled, rejected } from "@xylabs/promise";
3
- import { asDivinerInstance } from "@xyo-network/diviner-model";
4
- import { AbstractSentinel } from "@xyo-network/sentinel-abstract";
5
- import {
6
- asSentinelInstance,
7
- SentinelConfigSchema
8
- } 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 {
15
- isSentinelIntervalAutomation
16
- } from "@xyo-network/sentinel-model";
17
-
18
- // src/SentinelIntervalAutomationWrapper.ts
19
- import { PayloadWrapper } from "@xyo-network/payload-wrapper";
20
- var SentinelIntervalAutomationWrapper = class extends PayloadWrapper {
21
- constructor(payload) {
22
- super(payload);
23
- }
24
- get frequencyMillis() {
25
- const frequency = this.jsonPayload().frequency;
26
- if (frequency === void 0)
27
- return Number.POSITIVE_INFINITY;
28
- const frequencyUnits = this.jsonPayload().frequencyUnits;
29
- switch (frequencyUnits ?? "hour") {
30
- case "second": {
31
- return frequency * 1e3;
32
- }
33
- case "minute": {
34
- return frequency * 60 * 1e3;
35
- }
36
- case "hour": {
37
- return frequency * 60 * 60 * 1e3;
38
- }
39
- case "day": {
40
- return frequency * 24 * 60 * 60 * 1e3;
41
- }
42
- default: {
43
- return Number.POSITIVE_INFINITY;
44
- }
45
- }
46
- }
47
- get remaining() {
48
- return this.jsonPayload().remaining ?? Number.POSITIVE_INFINITY;
49
- }
50
- next() {
51
- const now = Date.now();
52
- const previousStart = this.jsonPayload()?.start ?? now;
53
- const start = Math.max(previousStart, now);
54
- const nextStart = start + this.frequencyMillis;
55
- this.setStart(nextStart);
56
- this.consumeRemaining();
57
- this.checkEnd();
58
- return this;
59
- }
60
- checkEnd() {
61
- if (this.jsonPayload().start > (this.jsonPayload().end ?? Number.POSITIVE_INFINITY)) {
62
- this.setStart(Number.POSITIVE_INFINITY);
63
- }
64
- }
65
- consumeRemaining(count = 1) {
66
- const remaining = Math.max(this.remaining - count, 0);
67
- this.setRemaining(remaining);
68
- if (remaining <= 0)
69
- this.setStart(Number.POSITIVE_INFINITY);
70
- }
71
- /**
72
- * Sets the remaining of the wrapped automation
73
- * @param remaining The remaining time in milliseconds
74
- */
75
- setRemaining(remaining) {
76
- this.obj.remaining = remaining;
77
- }
78
- /**
79
- * Sets the start of the wrapped automation
80
- * @param start The start time in milliseconds
81
- */
82
- setStart(start) {
83
- this.obj.start = start;
84
- }
85
- };
86
-
87
- // src/SentinelRunner.ts
88
- var SentinelRunner = class {
89
- _automations = {};
90
- onTriggerResult;
91
- sentinel;
92
- timeoutId;
93
- constructor(sentinel, automations, onTriggerResult) {
94
- this.sentinel = sentinel;
95
- this.onTriggerResult = onTriggerResult;
96
- if (automations)
97
- for (const automation of automations)
98
- this.add(automation);
99
- }
100
- get automations() {
101
- return this._automations;
102
- }
103
- get next() {
104
- return Object.values(this._automations).reduce((previous, current) => {
105
- if (isSentinelIntervalAutomation(current) && isSentinelIntervalAutomation(previous)) {
106
- return current.start < (previous?.start ?? Number.POSITIVE_INFINITY) ? current : previous;
107
- }
108
- return current;
109
- }, void 0);
110
- }
111
- async add(automation, restart = true) {
112
- const hash = await PayloadWrapper2.hashAsync(automation);
113
- this._automations[hash] = automation;
114
- if (restart)
115
- await this.restart();
116
- return hash;
117
- }
118
- find(hash) {
119
- Object.entries(this._automations).find(([key]) => key === hash);
120
- }
121
- async remove(hash, restart = true) {
122
- delete this._automations[hash];
123
- if (restart)
124
- await this.restart();
125
- }
126
- removeAll() {
127
- this.stop();
128
- this._automations = {};
129
- }
130
- async restart() {
131
- this.stop();
132
- await this.start();
133
- }
134
- async start() {
135
- await Promise.resolve();
136
- assertEx(this.timeoutId === void 0, "Already started");
137
- const automation = this.next;
138
- if (isSentinelIntervalAutomation(automation)) {
139
- const now = Date.now();
140
- const start = Math.max(automation.start ?? now, now);
141
- const delay = Math.max(start - now, 0);
142
- if (delay < Number.POSITIVE_INFINITY) {
143
- this.timeoutId = setTimeout(async () => {
144
- try {
145
- await this.trigger(automation);
146
- this.stop();
147
- } finally {
148
- await this.start();
149
- }
150
- }, delay);
151
- }
152
- }
153
- }
154
- stop() {
155
- if (this.timeoutId) {
156
- clearTimeout(this.timeoutId);
157
- this.timeoutId = void 0;
158
- }
159
- }
160
- async update(hash, automation, restart = true) {
161
- await this.remove(hash, false);
162
- await this.add(automation, false);
163
- if (restart)
164
- await this.restart();
165
- }
166
- async trigger(automation) {
167
- const wrapper = new SentinelIntervalAutomationWrapper(automation);
168
- await this.remove(await wrapper.hashAsync(), false);
169
- wrapper.next();
170
- await this.add(wrapper.jsonPayload(), false);
171
- const triggerResult = await this.sentinel.report();
172
- this.onTriggerResult?.(triggerResult);
173
- }
174
- };
175
-
176
- // src/MemorySentinel.ts
177
- var MemorySentinel = class extends AbstractSentinel {
178
- static configSchemas = [SentinelConfigSchema];
179
- runner;
180
- async reportHandler(inPayloads = []) {
181
- await this.started("throw");
182
- const job = await this.jobPromise;
183
- let index = 0;
184
- let previousResults = {};
185
- while (index < job.tasks.length) {
186
- const generatedPayloads = await this.generateResults(job.tasks[index], previousResults, inPayloads);
187
- previousResults = generatedPayloads;
188
- index++;
189
- }
190
- return Object.values(previousResults).flat();
191
- }
192
- async start(timeout) {
193
- if (await super.start(timeout)) {
194
- if ((this.config.automations?.length ?? 0) > 0) {
195
- this.runner = new SentinelRunner(this, this.config.automations);
196
- await this.runner.start();
197
- }
198
- return true;
199
- }
200
- return false;
201
- }
202
- async stop(timeout) {
203
- if (this.runner) {
204
- this.runner.stop();
205
- this.runner = void 0;
206
- }
207
- return await super.stop(timeout);
208
- }
209
- async generateResults(tasks, previousResults, inPayloads) {
210
- const results = await Promise.allSettled(
211
- tasks?.map(async (task) => {
212
- const input = task.input ?? false;
213
- const inPayloadsFound = input === true ? inPayloads : input === false ? [] : this.processPreviousResults(previousResults, await this.inputAddresses(input));
214
- const witness = asWitnessInstance(task.module);
215
- if (witness) {
216
- return [witness.address, await witness.observe(inPayloadsFound)];
217
- }
218
- const diviner = asDivinerInstance(task.module);
219
- if (diviner) {
220
- return [diviner.address, await diviner.divine(inPayloadsFound)];
221
- }
222
- const sentinel = asSentinelInstance(task.module);
223
- if (sentinel) {
224
- return [sentinel.address, await sentinel.report(inPayloadsFound)];
225
- }
226
- throw new Error("Unsupported module type");
227
- })
228
- );
229
- const finalResult = {};
230
- for (const result of results.filter(fulfilled)) {
231
- const [address, payloads] = result.value;
232
- finalResult[address] = finalResult[address] ?? [];
233
- finalResult[address].push(...payloads);
234
- }
235
- if (this.throwErrors) {
236
- const errors = results.filter(rejected).map((result) => result.reason);
237
- if (errors.length > 0) {
238
- throw new Error("At least one module failed");
239
- }
240
- }
241
- return finalResult;
242
- }
243
- async inputAddresses(input) {
244
- if (Array.isArray(input)) {
245
- return (await Promise.all(input.map(async (inputItem) => await this.inputAddresses(inputItem)))).flat();
246
- } else {
247
- const resolved = await this.resolve(input);
248
- return resolved ? [resolved.address] : [];
249
- }
250
- }
251
- processPreviousResults(payloads, inputs) {
252
- return inputs.flatMap((input) => payloads[input] ?? []);
253
- }
254
- };
255
-
256
- // src/Wrapper.ts
257
- import { constructableModuleWrapper, ModuleWrapper } from "@xyo-network/module-wrapper";
258
- import {
259
- isSentinelInstance,
260
- isSentinelModule,
261
- SentinelReportQuerySchema
262
- } from "@xyo-network/sentinel-model";
263
- constructableModuleWrapper();
264
- var SentinelWrapper = class extends ModuleWrapper {
265
- static instanceIdentityCheck = isSentinelInstance;
266
- static moduleIdentityCheck = isSentinelModule;
267
- static requiredQueries = [SentinelReportQuerySchema, ...super.requiredQueries];
268
- archivists() {
269
- throw new Error("Not supported");
270
- }
271
- async report(payloads) {
272
- const queryPayload = { schema: SentinelReportQuerySchema };
273
- const result = await this.sendQuery(queryPayload, payloads);
274
- return result;
275
- }
276
- witnesses() {
277
- throw new Error("Not supported");
278
- }
279
- };
280
-
281
1
  // src/index.ts
282
2
  export * from "@xyo-network/sentinel-abstract";
3
+ export * from "@xyo-network/sentinel-memory";
283
4
  export * from "@xyo-network/sentinel-model";
284
- export {
285
- MemorySentinel,
286
- SentinelIntervalAutomationWrapper,
287
- SentinelRunner,
288
- SentinelWrapper
289
- };
290
5
  //# sourceMappingURL=index.js.map