@xyo-network/sentinel 2.84.6 → 2.84.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/dist/browser/MemorySentinel.d.cts +3 -0
  2. package/dist/browser/MemorySentinel.d.cts.map +1 -1
  3. package/dist/browser/MemorySentinel.d.mts +3 -0
  4. package/dist/browser/MemorySentinel.d.mts.map +1 -1
  5. package/dist/browser/MemorySentinel.d.ts +3 -0
  6. package/dist/browser/MemorySentinel.d.ts.map +1 -1
  7. package/dist/browser/SentinelIntervalAutomationWrapper.d.cts +11 -1
  8. package/dist/browser/SentinelIntervalAutomationWrapper.d.cts.map +1 -1
  9. package/dist/browser/SentinelIntervalAutomationWrapper.d.mts +11 -1
  10. package/dist/browser/SentinelIntervalAutomationWrapper.d.mts.map +1 -1
  11. package/dist/browser/SentinelIntervalAutomationWrapper.d.ts +11 -1
  12. package/dist/browser/SentinelIntervalAutomationWrapper.d.ts.map +1 -1
  13. package/dist/browser/SentinelRunner.d.cts +1 -2
  14. package/dist/browser/SentinelRunner.d.cts.map +1 -1
  15. package/dist/browser/SentinelRunner.d.mts +1 -2
  16. package/dist/browser/SentinelRunner.d.mts.map +1 -1
  17. package/dist/browser/SentinelRunner.d.ts +1 -2
  18. package/dist/browser/SentinelRunner.d.ts.map +1 -1
  19. package/dist/browser/index.cjs +148 -110
  20. package/dist/browser/index.cjs.map +1 -1
  21. package/dist/browser/index.d.cts +0 -1
  22. package/dist/browser/index.d.cts.map +1 -1
  23. package/dist/browser/index.d.mts +0 -1
  24. package/dist/browser/index.d.mts.map +1 -1
  25. package/dist/browser/index.d.ts +0 -1
  26. package/dist/browser/index.d.ts.map +1 -1
  27. package/dist/browser/index.js +143 -103
  28. package/dist/browser/index.js.map +1 -1
  29. package/dist/node/MemorySentinel.d.cts +3 -0
  30. package/dist/node/MemorySentinel.d.cts.map +1 -1
  31. package/dist/node/MemorySentinel.d.mts +3 -0
  32. package/dist/node/MemorySentinel.d.mts.map +1 -1
  33. package/dist/node/MemorySentinel.d.ts +3 -0
  34. package/dist/node/MemorySentinel.d.ts.map +1 -1
  35. package/dist/node/SentinelIntervalAutomationWrapper.d.cts +11 -1
  36. package/dist/node/SentinelIntervalAutomationWrapper.d.cts.map +1 -1
  37. package/dist/node/SentinelIntervalAutomationWrapper.d.mts +11 -1
  38. package/dist/node/SentinelIntervalAutomationWrapper.d.mts.map +1 -1
  39. package/dist/node/SentinelIntervalAutomationWrapper.d.ts +11 -1
  40. package/dist/node/SentinelIntervalAutomationWrapper.d.ts.map +1 -1
  41. package/dist/node/SentinelRunner.d.cts +1 -2
  42. package/dist/node/SentinelRunner.d.cts.map +1 -1
  43. package/dist/node/SentinelRunner.d.mts +1 -2
  44. package/dist/node/SentinelRunner.d.mts.map +1 -1
  45. package/dist/node/SentinelRunner.d.ts +1 -2
  46. package/dist/node/SentinelRunner.d.ts.map +1 -1
  47. package/dist/node/index.cjs +150 -114
  48. package/dist/node/index.cjs.map +1 -1
  49. package/dist/node/index.d.cts +0 -1
  50. package/dist/node/index.d.cts.map +1 -1
  51. package/dist/node/index.d.mts +0 -1
  52. package/dist/node/index.d.mts.map +1 -1
  53. package/dist/node/index.d.ts +0 -1
  54. package/dist/node/index.d.ts.map +1 -1
  55. package/dist/node/index.js +145 -103
  56. package/dist/node/index.js.map +1 -1
  57. package/package.json +18 -18
  58. package/src/MemorySentinel.ts +28 -5
  59. package/src/SentinelIntervalAutomationWrapper.ts +41 -18
  60. package/src/SentinelRunner.ts +27 -19
  61. package/src/Wrapper.ts +2 -2
  62. package/src/index.ts +0 -1
  63. package/dist/browser/Automation.d.cts +0 -71
  64. package/dist/browser/Automation.d.cts.map +0 -1
  65. package/dist/browser/Automation.d.mts +0 -71
  66. package/dist/browser/Automation.d.mts.map +0 -1
  67. package/dist/browser/Automation.d.ts +0 -71
  68. package/dist/browser/Automation.d.ts.map +0 -1
  69. package/dist/node/Automation.d.cts +0 -71
  70. package/dist/node/Automation.d.cts.map +0 -1
  71. package/dist/node/Automation.d.mts +0 -71
  72. package/dist/node/Automation.d.mts.map +0 -1
  73. package/dist/node/Automation.d.ts +0 -71
  74. package/dist/node/Automation.d.ts.map +0 -1
  75. package/src/Automation.ts +0 -55
@@ -1,10 +1,3 @@
1
- // src/Automation.ts
2
- import { isPayloadOfSchemaType } from "@xyo-network/payload-model";
3
- var SentinelAutomationSchema = "network.xyo.automation";
4
- var SentinelIntervalAutomationSchema = "network.xyo.automation.interval";
5
- var SentinelEventAutomationSchema = "network.xyo.automation.event";
6
- var isSentinelIntervalAutomation = isPayloadOfSchemaType(SentinelIntervalAutomationSchema);
7
-
8
1
  // src/MemorySentinel.ts
9
2
  import { fulfilled, rejected } from "@xylabs/promise";
10
3
  import { asDivinerInstance } from "@xyo-network/diviner-model";
@@ -13,62 +6,13 @@ import {
13
6
  SentinelConfigSchema
14
7
  } from "@xyo-network/sentinel-model";
15
8
  import { asWitnessInstance } from "@xyo-network/witness-model";
16
- var MemorySentinel = class extends AbstractSentinel {
17
- static configSchemas = [SentinelConfigSchema];
18
- async reportHandler(inPayloads = []) {
19
- await this.started("throw");
20
- const job = await this.jobPromise;
21
- let index = 0;
22
- let previousResults = {};
23
- while (index < job.tasks.length) {
24
- const generatedPayloads = await this.generateResults(job.tasks[index], previousResults, inPayloads);
25
- previousResults = generatedPayloads;
26
- index++;
27
- }
28
- return Object.values(previousResults).flat();
29
- }
30
- async generateResults(tasks, previousResults, inPayloads) {
31
- const results = await Promise.allSettled(
32
- tasks == null ? void 0 : tasks.map(async (task) => {
33
- const witness = asWitnessInstance(task.module);
34
- const input = task.input ?? false;
35
- const inPayloadsFound = input === true ? inPayloads : input === false ? [] : this.processPreviousResults(previousResults, await this.inputAddresses(input));
36
- if (witness) {
37
- return [witness.address, await witness.observe(inPayloadsFound)];
38
- }
39
- const diviner = asDivinerInstance(task.module);
40
- if (diviner) {
41
- return [diviner.address, await diviner.divine(inPayloadsFound)];
42
- }
43
- throw Error("Unsupported module type");
44
- })
45
- );
46
- const finalResult = {};
47
- results.filter(fulfilled).forEach((result) => {
48
- const [address, payloads] = result.value;
49
- finalResult[address] = finalResult[address] ?? [];
50
- finalResult[address].push(...payloads);
51
- });
52
- if (this.throwErrors) {
53
- const errors = results.filter(rejected).map((result) => result.reason);
54
- if (errors.length > 0) {
55
- throw Error("At least one module failed");
56
- }
57
- }
58
- return finalResult;
59
- }
60
- async inputAddresses(input) {
61
- if (Array.isArray(input)) {
62
- return (await Promise.all(input.map(async (inputItem) => await this.inputAddresses(inputItem)))).flat();
63
- } else {
64
- const resolved = await this.resolve(input);
65
- return resolved ? [resolved.address] : [];
66
- }
67
- }
68
- processPreviousResults(payloads, inputs) {
69
- return inputs.map((input) => payloads[input] ?? []).flat();
70
- }
71
- };
9
+
10
+ // src/SentinelRunner.ts
11
+ import { assertEx } from "@xylabs/assert";
12
+ import { PayloadWrapper as PayloadWrapper2 } from "@xyo-network/payload-wrapper";
13
+ import {
14
+ isSentinelIntervalAutomation
15
+ } from "@xyo-network/sentinel-model";
72
16
 
73
17
  // src/SentinelIntervalAutomationWrapper.ts
74
18
  import { PayloadWrapper } from "@xyo-network/payload-wrapper";
@@ -79,44 +23,68 @@ var SentinelIntervalAutomationWrapper = class extends PayloadWrapper {
79
23
  get frequencyMillis() {
80
24
  const frequency = this.jsonPayload().frequency;
81
25
  if (frequency === void 0)
82
- return Infinity;
83
- switch (this.jsonPayload().frequencyUnits ?? "hour") {
84
- case "second":
26
+ return Number.POSITIVE_INFINITY;
27
+ const frequencyUnits = this.jsonPayload().frequencyUnits;
28
+ switch (frequencyUnits ?? "hour") {
29
+ case "second": {
85
30
  return frequency * 1e3;
86
- case "minute":
31
+ }
32
+ case "minute": {
87
33
  return frequency * 60 * 1e3;
88
- case "hour":
34
+ }
35
+ case "hour": {
89
36
  return frequency * 60 * 60 * 1e3;
90
- case "day":
37
+ }
38
+ case "day": {
91
39
  return frequency * 24 * 60 * 60 * 1e3;
40
+ }
41
+ default: {
42
+ return Number.POSITIVE_INFINITY;
43
+ }
92
44
  }
93
45
  }
94
46
  get remaining() {
95
- return this.payload().remaining ?? Infinity;
47
+ return this.jsonPayload().remaining ?? Number.POSITIVE_INFINITY;
96
48
  }
97
49
  next() {
98
- this.jsonPayload().start = this.jsonPayload().start + this.frequencyMillis;
50
+ var _a;
51
+ const now = Date.now();
52
+ const previousStart = ((_a = this.jsonPayload()) == null ? void 0 : _a.start) ?? now;
53
+ const start = Math.max(previousStart, now);
54
+ const nextStart = start + this.frequencyMillis;
55
+ this.setStart(nextStart);
99
56
  this.consumeRemaining();
100
57
  this.checkEnd();
101
58
  return this;
102
59
  }
103
60
  checkEnd() {
104
- if (this.jsonPayload().start > (this.jsonPayload().end ?? Infinity)) {
105
- this.jsonPayload().start = Infinity;
61
+ if (this.jsonPayload().start > (this.jsonPayload().end ?? Number.POSITIVE_INFINITY)) {
62
+ this.setStart(Number.POSITIVE_INFINITY);
106
63
  }
107
64
  }
108
65
  consumeRemaining(count = 1) {
109
- const remaining = this.remaining - count;
110
- this.jsonPayload().remaining = remaining;
111
- if (remaining <= 0) {
112
- this.jsonPayload().start = Infinity;
113
- }
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;
114
84
  }
115
85
  };
116
86
 
117
87
  // src/SentinelRunner.ts
118
- import { assertEx } from "@xylabs/assert";
119
- import { PayloadWrapper as PayloadWrapper2 } from "@xyo-network/payload-wrapper";
120
88
  var SentinelRunner = class {
121
89
  _automations = {};
122
90
  onTriggerResult;
@@ -125,17 +93,17 @@ var SentinelRunner = class {
125
93
  constructor(sentinel, automations, onTriggerResult) {
126
94
  this.sentinel = sentinel;
127
95
  this.onTriggerResult = onTriggerResult;
128
- automations == null ? void 0 : automations.forEach((automation) => this.add(automation));
96
+ if (automations)
97
+ for (const automation of automations)
98
+ this.add(automation);
129
99
  }
130
100
  get automations() {
131
101
  return this._automations;
132
102
  }
133
103
  get next() {
134
104
  return Object.values(this._automations).reduce((previous, current) => {
135
- if (isSentinelIntervalAutomation(current)) {
136
- if (isSentinelIntervalAutomation(previous)) {
137
- return current.start < ((previous == null ? void 0 : previous.start) ?? Infinity) ? current : previous;
138
- }
105
+ if (isSentinelIntervalAutomation(current) && isSentinelIntervalAutomation(previous)) {
106
+ return current.start < ((previous == null ? void 0 : previous.start) ?? Number.POSITIVE_INFINITY) ? current : previous;
139
107
  }
140
108
  return current;
141
109
  }, void 0);
@@ -164,20 +132,22 @@ var SentinelRunner = class {
164
132
  await this.start();
165
133
  }
166
134
  async start() {
135
+ await Promise.resolve();
167
136
  assertEx(this.timeoutId === void 0, "Already started");
168
137
  const automation = this.next;
169
138
  if (isSentinelIntervalAutomation(automation)) {
170
- const delay = automation.start - Date.now();
171
- if (delay < 0) {
172
- await this.trigger(automation);
173
- } else {
174
- this.timeoutId = setTimeout(
175
- async () => {
176
- this.timeoutId = void 0;
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 {
177
148
  await this.start();
178
- },
179
- delay > 0 ? delay : 0
180
- );
149
+ }
150
+ }, delay);
181
151
  }
182
152
  }
183
153
  }
@@ -201,7 +171,83 @@ var SentinelRunner = class {
201
171
  await this.add(wrapper.jsonPayload(), false);
202
172
  const triggerResult = await this.sentinel.report();
203
173
  (_a = this.onTriggerResult) == null ? void 0 : _a.call(this, triggerResult);
204
- await this.start();
174
+ }
175
+ };
176
+
177
+ // src/MemorySentinel.ts
178
+ var MemorySentinel = class extends AbstractSentinel {
179
+ static configSchemas = [SentinelConfigSchema];
180
+ runner;
181
+ async reportHandler(inPayloads = []) {
182
+ await this.started("throw");
183
+ const job = await this.jobPromise;
184
+ let index = 0;
185
+ let previousResults = {};
186
+ while (index < job.tasks.length) {
187
+ const generatedPayloads = await this.generateResults(job.tasks[index], previousResults, inPayloads);
188
+ previousResults = generatedPayloads;
189
+ index++;
190
+ }
191
+ return Object.values(previousResults).flat();
192
+ }
193
+ async start(timeout) {
194
+ var _a;
195
+ if (await super.start(timeout)) {
196
+ if ((((_a = this.config.automations) == null ? void 0 : _a.length) ?? 0) > 0) {
197
+ this.runner = new SentinelRunner(this, this.config.automations);
198
+ await this.runner.start();
199
+ }
200
+ return true;
201
+ }
202
+ return false;
203
+ }
204
+ async stop(timeout) {
205
+ if (this.runner) {
206
+ this.runner.stop();
207
+ this.runner = void 0;
208
+ }
209
+ return await super.stop(timeout);
210
+ }
211
+ async generateResults(tasks, previousResults, inPayloads) {
212
+ const results = await Promise.allSettled(
213
+ tasks == null ? void 0 : tasks.map(async (task) => {
214
+ const witness = asWitnessInstance(task.module);
215
+ const input = task.input ?? false;
216
+ const inPayloadsFound = input === true ? inPayloads : input === false ? [] : this.processPreviousResults(previousResults, await this.inputAddresses(input));
217
+ if (witness) {
218
+ return [witness.address, await witness.observe(inPayloadsFound)];
219
+ }
220
+ const diviner = asDivinerInstance(task.module);
221
+ if (diviner) {
222
+ return [diviner.address, await diviner.divine(inPayloadsFound)];
223
+ }
224
+ throw new Error("Unsupported module type");
225
+ })
226
+ );
227
+ const finalResult = {};
228
+ for (const result of results.filter(fulfilled)) {
229
+ const [address, payloads] = result.value;
230
+ finalResult[address] = finalResult[address] ?? [];
231
+ finalResult[address].push(...payloads);
232
+ }
233
+ if (this.throwErrors) {
234
+ const errors = results.filter(rejected).map((result) => result.reason);
235
+ if (errors.length > 0) {
236
+ throw new Error("At least one module failed");
237
+ }
238
+ }
239
+ return finalResult;
240
+ }
241
+ async inputAddresses(input) {
242
+ if (Array.isArray(input)) {
243
+ return (await Promise.all(input.map(async (inputItem) => await this.inputAddresses(inputItem)))).flat();
244
+ } else {
245
+ const resolved = await this.resolve(input);
246
+ return resolved ? [resolved.address] : [];
247
+ }
248
+ }
249
+ processPreviousResults(payloads, inputs) {
250
+ return inputs.flatMap((input) => payloads[input] ?? []);
205
251
  }
206
252
  };
207
253
 
@@ -218,7 +264,7 @@ var SentinelWrapper = class extends ModuleWrapper {
218
264
  static moduleIdentityCheck = isSentinelModule;
219
265
  static requiredQueries = [SentinelReportQuerySchema, ...super.requiredQueries];
220
266
  archivists() {
221
- throw Error("Not supported");
267
+ throw new Error("Not supported");
222
268
  }
223
269
  async report(payloads) {
224
270
  const queryPayload = { schema: SentinelReportQuerySchema };
@@ -226,7 +272,7 @@ var SentinelWrapper = class extends ModuleWrapper {
226
272
  return result;
227
273
  }
228
274
  witnesses() {
229
- throw Error("Not supported");
275
+ throw new Error("Not supported");
230
276
  }
231
277
  };
232
278
 
@@ -235,12 +281,8 @@ export * from "@xyo-network/sentinel-abstract";
235
281
  export * from "@xyo-network/sentinel-model";
236
282
  export {
237
283
  MemorySentinel,
238
- SentinelAutomationSchema,
239
- SentinelEventAutomationSchema,
240
- SentinelIntervalAutomationSchema,
241
284
  SentinelIntervalAutomationWrapper,
242
285
  SentinelRunner,
243
- SentinelWrapper,
244
- isSentinelIntervalAutomation
286
+ SentinelWrapper
245
287
  };
246
288
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
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, 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 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 if (this.throwErrors) {\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 }\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,WAAW,gBAAgB;AACpC,SAAS,yBAAyB;AAGlC,SAAS,wBAAwB;AACjC;AAAA,EAGE;AAAA,OAIK;AACP,SAAS,yBAAyB;AAI3B,IAAM,iBAAN,cAGG,iBAAsC;AAAA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KAAK,SAAS,OAAO;AACjD,eAAK,oBAAL,8BAAuB;AACvB,UAAM,KAAK,MAAM;AAAA,EACnB;AACF;;;ACxGA,SAAS,4BAA4B,qBAAqB;AAE1D;AAAA,EACE;AAAA,EACA;AAAA,EAIA;AAAA,OACK;AAGP,2BAA2B;AACpB,IAAM,kBAAN,cACG,cAEV;AAAA,EACE,OAAgB,wBAAwB;AAAA,EACxC,OAAgB,sBAAsB;AAAA,EACtC,OAAgB,kBAAkB,CAAC,2BAA2B,GAAG,MAAM,eAAe;AAAA,EAEtF,aAA2C;AACzC,UAAM,MAAM,eAAe;AAAA,EAC7B;AAAA,EAEA,MAAM,OAAO,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"]}
1
+ {"version":3,"sources":["../../src/MemorySentinel.ts","../../src/SentinelRunner.ts","../../src/SentinelIntervalAutomationWrapper.ts","../../src/Wrapper.ts","../../src/index.ts"],"sourcesContent":["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 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 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 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","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"],"mappings":";AACA,SAAS,WAAW,gBAAgB;AACpC,SAAS,yBAAyB;AAGlC,SAAS,wBAAwB;AACjC;AAAA,EAGE;AAAA,OAIK;AACP,SAAS,yBAAyB;;;ACdlC,SAAS,gBAAgB;AAEzB,SAAS,kBAAAA,uBAAsB;AAC/B;AAAA,EACE;AAAA,OAIK;;;ACRP,SAAS,sBAAsB;AAGxB,IAAM,oCAAN,cAEG,eAAkB;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,KAAK,YAAY,EAAE;AAC1C,YAAQ,kBAAkB,QAAQ;AAAA,MAChC,KAAK,UAAU;AACb,eAAO,YAAY;AAAA,MACrB;AAAA,MACA,KAAK,UAAU;AACb,eAAO,YAAY,KAAK;AAAA,MAC1B;AAAA,MACA,KAAK,QAAQ;AACX,eAAO,YAAY,KAAK,KAAK;AAAA,MAC/B;AAAA,MACA,KAAK,OAAO;AACV,eAAO,YAAY,KAAK,KAAK,KAAK;AAAA,MACpC;AAAA,MACA,SAAS;AACP,eAAO,OAAO;AAAA,MAChB;AAAA,IACF;AAAA,EACF;AAAA,EAEA,IAAc,YAAY;AACxB,WAAO,KAAK,YAAY,EAAE,aAAa,OAAO;AAAA,EAChD;AAAA,EAEA,OAAO;AArCT;AAsCI,UAAM,MAAM,KAAK,IAAI;AACrB,UA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AM,IAAI,OAAO,cAAc,MAAM,KAAK,eAAe,SAAS,CAAC,CAAC,GAAG,KAAK;AAAA,IACxG,OAAO;AACL,YAAM,WAAW,MAAM,KAAK,QAAQ,KAAK;AACzC,aAAO,WAAW,CAAC,SAAS,OAAO,IAAI,CAAC;AAAA,IAC1C;AAAA,EACF;AAAA,EAEQ,uBAAuB,UAAqC,QAAkB;AACpF,WAAO,OAAO,QAAQ,CAAC,UAAU,SAAS,KAAK,KAAK,CAAC,CAAC;AAAA,EACxD;AACF;;;AG5GA,SAAS,4BAA4B,qBAAqB;AAE1D;AAAA,EACE;AAAA,EACA;AAAA,EAIA;AAAA,OACK;AAGP,2BAA2B;AACpB,IAAM,kBAAN,cACG,cAEV;AAAA,EACE,OAAgB,wBAAwB;AAAA,EACxC,OAAgB,sBAAsB;AAAA,EACtC,OAAgB,kBAAkB,CAAC,2BAA2B,GAAG,MAAM,eAAe;AAAA,EAEtF,aAA2C;AACzC,UAAM,IAAI,MAAM,eAAe;AAAA,EACjC;AAAA,EAEA,MAAM,OAAO,UAA0C;AACrD,UAAM,eAAoC,EAAE,QAAQ,0BAA0B;AAC9E,UAAM,SAAS,MAAM,KAAK,UAAU,cAAc,QAAQ;AAC1D,WAAO;AAAA,EACT;AAAA,EAEA,YAAwC;AACtC,UAAM,IAAI,MAAM,eAAe;AAAA,EACjC;AACF;;;AC/BA,cAAc;AACd,cAAc;","names":["PayloadWrapper","PayloadWrapper"]}
package/package.json CHANGED
@@ -13,28 +13,28 @@
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.6",
17
- "@xyo-network/diviner-model": "~2.84.6",
18
- "@xyo-network/module-model": "~2.84.6",
19
- "@xyo-network/module-wrapper": "~2.84.6",
20
- "@xyo-network/payload-model": "~2.84.6",
21
- "@xyo-network/payload-wrapper": "~2.84.6",
22
- "@xyo-network/sentinel-abstract": "~2.84.6",
23
- "@xyo-network/sentinel-model": "~2.84.6",
24
- "@xyo-network/witness-model": "~2.84.6"
16
+ "@xyo-network/archivist": "~2.84.8",
17
+ "@xyo-network/diviner-model": "~2.84.8",
18
+ "@xyo-network/module-model": "~2.84.8",
19
+ "@xyo-network/module-wrapper": "~2.84.8",
20
+ "@xyo-network/payload-model": "~2.84.8",
21
+ "@xyo-network/payload-wrapper": "~2.84.8",
22
+ "@xyo-network/sentinel-abstract": "~2.84.8",
23
+ "@xyo-network/sentinel-model": "~2.84.8",
24
+ "@xyo-network/witness-model": "~2.84.8"
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.19",
30
- "@xylabs/tsconfig": "^3.2.19",
31
- "@xyo-network/abstract-witness": "~2.84.6",
32
- "@xyo-network/account": "~2.84.6",
33
- "@xyo-network/boundwitness-model": "~2.84.6",
34
- "@xyo-network/hash": "~2.84.6",
29
+ "@xylabs/ts-scripts-yarn3": "^3.2.24",
30
+ "@xylabs/tsconfig": "^3.2.24",
31
+ "@xyo-network/abstract-witness": "~2.84.8",
32
+ "@xyo-network/account": "~2.84.8",
33
+ "@xyo-network/boundwitness-model": "~2.84.8",
34
+ "@xyo-network/hash": "~2.84.8",
35
35
  "@xyo-network/id-payload-plugin": "^2.84.2",
36
- "@xyo-network/node-memory": "~2.84.6",
37
- "@xyo-network/witnesses": "~2.84.6",
36
+ "@xyo-network/node-memory": "~2.84.8",
37
+ "@xyo-network/witness-adhoc": "~2.84.7",
38
38
  "typescript": "^5.3.3"
39
39
  },
40
40
  "types": "dist/node/index.d.ts",
@@ -75,6 +75,6 @@
75
75
  "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js.git"
76
76
  },
77
77
  "sideEffects": false,
78
- "version": "2.84.6",
78
+ "version": "2.84.8",
79
79
  "type": "module"
80
80
  }
@@ -14,6 +14,8 @@ import {
14
14
  } from '@xyo-network/sentinel-model'
15
15
  import { asWitnessInstance } from '@xyo-network/witness-model'
16
16
 
17
+ import { SentinelRunner } from './SentinelRunner'
18
+
17
19
  export type MemorySentinelParams<TConfig extends AnyConfigSchema<SentinelConfig> = AnyConfigSchema<SentinelConfig>> = SentinelParams<TConfig>
18
20
 
19
21
  export class MemorySentinel<
@@ -22,6 +24,8 @@ export class MemorySentinel<
22
24
  > extends AbstractSentinel<TParams, TEventData> {
23
25
  static override configSchemas = [SentinelConfigSchema]
24
26
 
27
+ private runner?: SentinelRunner
28
+
25
29
  async reportHandler(inPayloads: Payload[] = []): Promise<Payload[]> {
26
30
  await this.started('throw')
27
31
  const job = await this.jobPromise
@@ -36,6 +40,25 @@ export class MemorySentinel<
36
40
  return Object.values(previousResults).flat()
37
41
  }
38
42
 
43
+ override async start(timeout?: number | undefined): Promise<boolean> {
44
+ if (await super.start(timeout)) {
45
+ if ((this.config.automations?.length ?? 0) > 0) {
46
+ this.runner = new SentinelRunner(this, this.config.automations)
47
+ await this.runner.start()
48
+ }
49
+ return true
50
+ }
51
+ return false
52
+ }
53
+
54
+ override async stop(timeout?: number | undefined): Promise<boolean> {
55
+ if (this.runner) {
56
+ this.runner.stop()
57
+ this.runner = undefined
58
+ }
59
+ return await super.stop(timeout)
60
+ }
61
+
39
62
  private async generateResults(
40
63
  tasks: ResolvedTask[],
41
64
  previousResults: Record<Address, Payload[]>,
@@ -54,19 +77,19 @@ export class MemorySentinel<
54
77
  if (diviner) {
55
78
  return [diviner.address, await diviner.divine(inPayloadsFound)]
56
79
  }
57
- throw Error('Unsupported module type')
80
+ throw new Error('Unsupported module type')
58
81
  }),
59
82
  )
60
83
  const finalResult: Record<Address, Payload[]> = {}
61
- results.filter(fulfilled).forEach((result) => {
84
+ for (const result of results.filter(fulfilled)) {
62
85
  const [address, payloads] = result.value
63
86
  finalResult[address] = finalResult[address] ?? []
64
87
  finalResult[address].push(...payloads)
65
- })
88
+ }
66
89
  if (this.throwErrors) {
67
90
  const errors = results.filter(rejected).map((result) => result.reason)
68
91
  if (errors.length > 0) {
69
- throw Error('At least one module failed')
92
+ throw new Error('At least one module failed')
70
93
  }
71
94
  }
72
95
  return finalResult
@@ -82,6 +105,6 @@ export class MemorySentinel<
82
105
  }
83
106
 
84
107
  private processPreviousResults(payloads: Record<string, Payload[]>, inputs: string[]) {
85
- return inputs.map((input) => payloads[input] ?? []).flat()
108
+ return inputs.flatMap((input) => payloads[input] ?? [])
86
109
  }
87
110
  }
@@ -1,6 +1,5 @@
1
1
  import { PayloadWrapper } from '@xyo-network/payload-wrapper'
2
-
3
- import { SentinelIntervalAutomationPayload } from './Automation'
2
+ import { SentinelIntervalAutomationPayload } from '@xyo-network/sentinel-model'
4
3
 
5
4
  export class SentinelIntervalAutomationWrapper<
6
5
  T extends SentinelIntervalAutomationPayload = SentinelIntervalAutomationPayload,
@@ -11,43 +10,67 @@ export class SentinelIntervalAutomationWrapper<
11
10
 
12
11
  protected get frequencyMillis() {
13
12
  const frequency = this.jsonPayload().frequency
14
- if (frequency === undefined) return Infinity
15
- switch (this.jsonPayload().frequencyUnits ?? 'hour') {
16
- case 'second':
13
+ if (frequency === undefined) return Number.POSITIVE_INFINITY
14
+ const frequencyUnits = this.jsonPayload().frequencyUnits
15
+ switch (frequencyUnits ?? 'hour') {
16
+ case 'second': {
17
17
  return frequency * 1000
18
- case 'minute':
18
+ }
19
+ case 'minute': {
19
20
  return frequency * 60 * 1000
20
- case 'hour':
21
+ }
22
+ case 'hour': {
21
23
  return frequency * 60 * 60 * 1000
22
- case 'day':
24
+ }
25
+ case 'day': {
23
26
  return frequency * 24 * 60 * 60 * 1000
27
+ }
28
+ default: {
29
+ return Number.POSITIVE_INFINITY
30
+ }
24
31
  }
25
32
  }
26
33
 
27
34
  protected get remaining() {
28
- //if remaining is not defined, we assume Infinity
29
- return this.payload().remaining ?? Infinity
35
+ return this.jsonPayload().remaining ?? Number.POSITIVE_INFINITY
30
36
  }
31
37
 
32
38
  next() {
33
- this.jsonPayload().start = this.jsonPayload().start + this.frequencyMillis
39
+ const now = Date.now()
40
+ const previousStart = this.jsonPayload()?.start ?? now
41
+ const start = Math.max(previousStart, now)
42
+ const nextStart = start + this.frequencyMillis
43
+ this.setStart(nextStart)
34
44
  this.consumeRemaining()
35
45
  this.checkEnd()
36
46
  return this
37
47
  }
38
48
 
39
49
  protected checkEnd() {
40
- if (this.jsonPayload().start > (this.jsonPayload().end ?? Infinity)) {
41
- this.jsonPayload().start = Infinity
50
+ if (this.jsonPayload().start > (this.jsonPayload().end ?? Number.POSITIVE_INFINITY)) {
51
+ this.setStart(Number.POSITIVE_INFINITY)
42
52
  }
43
53
  }
44
54
 
45
55
  protected consumeRemaining(count = 1) {
46
- const remaining = this.remaining - count
47
- this.jsonPayload().remaining = remaining
56
+ const remaining = Math.max(this.remaining - count, 0)
57
+ this.setRemaining(remaining)
58
+ if (remaining <= 0) this.setStart(Number.POSITIVE_INFINITY)
59
+ }
48
60
 
49
- if (remaining <= 0) {
50
- this.jsonPayload().start = Infinity
51
- }
61
+ /**
62
+ * Sets the remaining of the wrapped automation
63
+ * @param remaining The remaining time in milliseconds
64
+ */
65
+ protected setRemaining(remaining: number) {
66
+ this.obj.remaining = remaining
67
+ }
68
+
69
+ /**
70
+ * Sets the start of the wrapped automation
71
+ * @param start The start time in milliseconds
72
+ */
73
+ protected setStart(start: number) {
74
+ this.obj.start = start
52
75
  }
53
76
  }