@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.
- package/LICENSE +165 -0
- package/README.md +13 -0
- package/dist/browser/MemorySentinel.d.cts +16 -0
- package/dist/browser/MemorySentinel.d.cts.map +1 -0
- package/dist/browser/MemorySentinel.d.mts +16 -0
- package/dist/browser/MemorySentinel.d.mts.map +1 -0
- package/dist/browser/MemorySentinel.d.ts +16 -0
- package/dist/browser/MemorySentinel.d.ts.map +1 -0
- package/dist/browser/SentinelIntervalAutomationWrapper.d.cts +21 -0
- package/dist/browser/SentinelIntervalAutomationWrapper.d.cts.map +1 -0
- package/dist/browser/SentinelIntervalAutomationWrapper.d.mts +21 -0
- package/dist/browser/SentinelIntervalAutomationWrapper.d.mts.map +1 -0
- package/dist/browser/SentinelIntervalAutomationWrapper.d.ts +21 -0
- package/dist/browser/SentinelIntervalAutomationWrapper.d.ts.map +1 -0
- package/dist/browser/SentinelRunner.d.cts +23 -0
- package/dist/browser/SentinelRunner.d.cts.map +1 -0
- package/dist/browser/SentinelRunner.d.mts +23 -0
- package/dist/browser/SentinelRunner.d.mts.map +1 -0
- package/dist/browser/SentinelRunner.d.ts +23 -0
- package/dist/browser/SentinelRunner.d.ts.map +1 -0
- package/dist/browser/index.cjs +310 -0
- package/dist/browser/index.cjs.map +1 -0
- package/dist/browser/index.d.cts +2 -0
- package/dist/browser/index.d.cts.map +1 -0
- package/dist/browser/index.d.mts +2 -0
- package/dist/browser/index.d.mts.map +1 -0
- package/dist/browser/index.d.ts +2 -0
- package/dist/browser/index.d.ts.map +1 -0
- package/dist/browser/index.js +289 -0
- package/dist/browser/index.js.map +1 -0
- package/dist/node/MemorySentinel.d.cts +16 -0
- package/dist/node/MemorySentinel.d.cts.map +1 -0
- package/dist/node/MemorySentinel.d.mts +16 -0
- package/dist/node/MemorySentinel.d.mts.map +1 -0
- package/dist/node/MemorySentinel.d.ts +16 -0
- package/dist/node/MemorySentinel.d.ts.map +1 -0
- package/dist/node/SentinelIntervalAutomationWrapper.d.cts +21 -0
- package/dist/node/SentinelIntervalAutomationWrapper.d.cts.map +1 -0
- package/dist/node/SentinelIntervalAutomationWrapper.d.mts +21 -0
- package/dist/node/SentinelIntervalAutomationWrapper.d.mts.map +1 -0
- package/dist/node/SentinelIntervalAutomationWrapper.d.ts +21 -0
- package/dist/node/SentinelIntervalAutomationWrapper.d.ts.map +1 -0
- package/dist/node/SentinelRunner.d.cts +23 -0
- package/dist/node/SentinelRunner.d.cts.map +1 -0
- package/dist/node/SentinelRunner.d.mts +23 -0
- package/dist/node/SentinelRunner.d.mts.map +1 -0
- package/dist/node/SentinelRunner.d.ts +23 -0
- package/dist/node/SentinelRunner.d.ts.map +1 -0
- package/dist/node/index.cjs +322 -0
- package/dist/node/index.cjs.map +1 -0
- package/dist/node/index.d.cts +2 -0
- package/dist/node/index.d.cts.map +1 -0
- package/dist/node/index.d.mts +2 -0
- package/dist/node/index.d.mts.map +1 -0
- package/dist/node/index.d.ts +2 -0
- package/dist/node/index.d.ts.map +1 -0
- package/dist/node/index.js +297 -0
- package/dist/node/index.js.map +1 -0
- package/package.json +77 -0
- package/src/MemorySentinel.ts +128 -0
- package/src/SentinelIntervalAutomationWrapper.ts +76 -0
- package/src/SentinelRunner.ts +114 -0
- package/src/index.ts +1 -0
- package/typedoc.json +5 -0
|
@@ -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,qCAAN,MAAMA,2CAEHC,sCAAAA;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,YAAW,EAAGK,aAAaH,OAAOC;EAChD;EAEAG,OAAO;AArCT;AAsCI,UAAMC,MAAMC,KAAKD,IAAG;AACpB,UAAME,kBAAgB,UAAKT,YAAW,MAAhB,mBAAoBU,UAASH;AACnD,UAAMG,QAAQC,KAAKC,IAAIH,eAAeF,GAAAA;AACtC,UAAMM,YAA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,CAACrC,UAAUuB,SAASvB,KAAAA,KAAU,CAAA,CAAE;EACxD;AACF;AAvGUpC;AACR,cAJWD,iBAIK2E,iBAAgB;EAACC;;AAJ5B,IAAM5E,iBAAN;","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","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","_a","_b","_c","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","configSchemas","SentinelConfigSchema"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA"}
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
4
|
+
var __publicField = (obj, key, value) => {
|
|
5
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
6
|
+
return value;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
// src/MemorySentinel.ts
|
|
10
|
+
import { fulfilled, rejected } from "@xylabs/promise";
|
|
11
|
+
import { asDivinerInstance } from "@xyo-network/diviner-model";
|
|
12
|
+
import { AbstractSentinel } from "@xyo-network/sentinel-abstract";
|
|
13
|
+
import { asSentinelInstance, SentinelConfigSchema } from "@xyo-network/sentinel-model";
|
|
14
|
+
import { asWitnessInstance } from "@xyo-network/witness-model";
|
|
15
|
+
|
|
16
|
+
// src/SentinelRunner.ts
|
|
17
|
+
import { assertEx } from "@xylabs/assert";
|
|
18
|
+
import { PayloadWrapper as PayloadWrapper2 } from "@xyo-network/payload-wrapper";
|
|
19
|
+
import { isSentinelIntervalAutomation } from "@xyo-network/sentinel-model";
|
|
20
|
+
|
|
21
|
+
// src/SentinelIntervalAutomationWrapper.ts
|
|
22
|
+
import { PayloadWrapper } from "@xyo-network/payload-wrapper";
|
|
23
|
+
var _SentinelIntervalAutomationWrapper = class _SentinelIntervalAutomationWrapper extends PayloadWrapper {
|
|
24
|
+
constructor(payload) {
|
|
25
|
+
super(payload);
|
|
26
|
+
}
|
|
27
|
+
get frequencyMillis() {
|
|
28
|
+
const frequency = this.jsonPayload().frequency;
|
|
29
|
+
if (frequency === void 0)
|
|
30
|
+
return Number.POSITIVE_INFINITY;
|
|
31
|
+
const frequencyUnits = this.jsonPayload().frequencyUnits;
|
|
32
|
+
switch (frequencyUnits ?? "hour") {
|
|
33
|
+
case "second": {
|
|
34
|
+
return frequency * 1e3;
|
|
35
|
+
}
|
|
36
|
+
case "minute": {
|
|
37
|
+
return frequency * 60 * 1e3;
|
|
38
|
+
}
|
|
39
|
+
case "hour": {
|
|
40
|
+
return frequency * 60 * 60 * 1e3;
|
|
41
|
+
}
|
|
42
|
+
case "day": {
|
|
43
|
+
return frequency * 24 * 60 * 60 * 1e3;
|
|
44
|
+
}
|
|
45
|
+
default: {
|
|
46
|
+
return Number.POSITIVE_INFINITY;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
get remaining() {
|
|
51
|
+
return this.jsonPayload().remaining ?? Number.POSITIVE_INFINITY;
|
|
52
|
+
}
|
|
53
|
+
next() {
|
|
54
|
+
var _a;
|
|
55
|
+
const now = Date.now();
|
|
56
|
+
const previousStart = ((_a = this.jsonPayload()) == null ? void 0 : _a.start) ?? now;
|
|
57
|
+
const start = Math.max(previousStart, now);
|
|
58
|
+
const nextStart = start + this.frequencyMillis;
|
|
59
|
+
this.setStart(nextStart);
|
|
60
|
+
this.consumeRemaining();
|
|
61
|
+
this.checkEnd();
|
|
62
|
+
return this;
|
|
63
|
+
}
|
|
64
|
+
checkEnd() {
|
|
65
|
+
if (this.jsonPayload().start > (this.jsonPayload().end ?? Number.POSITIVE_INFINITY)) {
|
|
66
|
+
this.setStart(Number.POSITIVE_INFINITY);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
consumeRemaining(count = 1) {
|
|
70
|
+
const remaining = Math.max(this.remaining - count, 0);
|
|
71
|
+
this.setRemaining(remaining);
|
|
72
|
+
if (remaining <= 0)
|
|
73
|
+
this.setStart(Number.POSITIVE_INFINITY);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Sets the remaining of the wrapped automation
|
|
77
|
+
* @param remaining The remaining time in milliseconds
|
|
78
|
+
*/
|
|
79
|
+
setRemaining(remaining) {
|
|
80
|
+
this.obj.remaining = remaining;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Sets the start of the wrapped automation
|
|
84
|
+
* @param start The start time in milliseconds
|
|
85
|
+
*/
|
|
86
|
+
setStart(start) {
|
|
87
|
+
this.obj.start = start;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
__name(_SentinelIntervalAutomationWrapper, "SentinelIntervalAutomationWrapper");
|
|
91
|
+
var SentinelIntervalAutomationWrapper = _SentinelIntervalAutomationWrapper;
|
|
92
|
+
|
|
93
|
+
// src/SentinelRunner.ts
|
|
94
|
+
var _SentinelRunner = class _SentinelRunner {
|
|
95
|
+
_automations = {};
|
|
96
|
+
onTriggerResult;
|
|
97
|
+
sentinel;
|
|
98
|
+
timeoutId;
|
|
99
|
+
constructor(sentinel, automations, onTriggerResult) {
|
|
100
|
+
this.sentinel = sentinel;
|
|
101
|
+
this.onTriggerResult = onTriggerResult;
|
|
102
|
+
if (automations)
|
|
103
|
+
for (const automation of automations)
|
|
104
|
+
this.add(automation);
|
|
105
|
+
}
|
|
106
|
+
get automations() {
|
|
107
|
+
return this._automations;
|
|
108
|
+
}
|
|
109
|
+
get next() {
|
|
110
|
+
return Object.values(this._automations).reduce((previous, current) => {
|
|
111
|
+
if (isSentinelIntervalAutomation(current) && isSentinelIntervalAutomation(previous)) {
|
|
112
|
+
return current.start < ((previous == null ? void 0 : previous.start) ?? Number.POSITIVE_INFINITY) ? current : previous;
|
|
113
|
+
}
|
|
114
|
+
return current;
|
|
115
|
+
}, void 0);
|
|
116
|
+
}
|
|
117
|
+
async add(automation, restart = true) {
|
|
118
|
+
const hash = await PayloadWrapper2.hashAsync(automation);
|
|
119
|
+
this._automations[hash] = automation;
|
|
120
|
+
if (restart)
|
|
121
|
+
await this.restart();
|
|
122
|
+
return hash;
|
|
123
|
+
}
|
|
124
|
+
find(hash) {
|
|
125
|
+
Object.entries(this._automations).find(([key]) => key === hash);
|
|
126
|
+
}
|
|
127
|
+
async remove(hash, restart = true) {
|
|
128
|
+
delete this._automations[hash];
|
|
129
|
+
if (restart)
|
|
130
|
+
await this.restart();
|
|
131
|
+
}
|
|
132
|
+
removeAll() {
|
|
133
|
+
this.stop();
|
|
134
|
+
this._automations = {};
|
|
135
|
+
}
|
|
136
|
+
async restart() {
|
|
137
|
+
this.stop();
|
|
138
|
+
await this.start();
|
|
139
|
+
}
|
|
140
|
+
async start() {
|
|
141
|
+
await Promise.resolve();
|
|
142
|
+
assertEx(this.timeoutId === void 0, "Already started");
|
|
143
|
+
const automation = this.next;
|
|
144
|
+
if (isSentinelIntervalAutomation(automation)) {
|
|
145
|
+
const now = Date.now();
|
|
146
|
+
const start = Math.max(automation.start ?? now, now);
|
|
147
|
+
const delay = Math.max(start - now, 0);
|
|
148
|
+
if (delay < Number.POSITIVE_INFINITY) {
|
|
149
|
+
this.timeoutId = setTimeout(async () => {
|
|
150
|
+
try {
|
|
151
|
+
await this.trigger(automation);
|
|
152
|
+
this.stop();
|
|
153
|
+
} finally {
|
|
154
|
+
await this.start();
|
|
155
|
+
}
|
|
156
|
+
}, delay);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
stop() {
|
|
161
|
+
if (this.timeoutId) {
|
|
162
|
+
clearTimeout(this.timeoutId);
|
|
163
|
+
this.timeoutId = void 0;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
async update(hash, automation, restart = true) {
|
|
167
|
+
await this.remove(hash, false);
|
|
168
|
+
await this.add(automation, false);
|
|
169
|
+
if (restart)
|
|
170
|
+
await this.restart();
|
|
171
|
+
}
|
|
172
|
+
async trigger(automation) {
|
|
173
|
+
var _a;
|
|
174
|
+
const wrapper = new SentinelIntervalAutomationWrapper(automation);
|
|
175
|
+
await this.remove(await wrapper.hashAsync(), false);
|
|
176
|
+
wrapper.next();
|
|
177
|
+
await this.add(wrapper.jsonPayload(), false);
|
|
178
|
+
const triggerResult = await this.sentinel.report();
|
|
179
|
+
(_a = this.onTriggerResult) == null ? void 0 : _a.call(this, triggerResult);
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
__name(_SentinelRunner, "SentinelRunner");
|
|
183
|
+
var SentinelRunner = _SentinelRunner;
|
|
184
|
+
|
|
185
|
+
// src/MemorySentinel.ts
|
|
186
|
+
var _MemorySentinel = class _MemorySentinel extends AbstractSentinel {
|
|
187
|
+
runner;
|
|
188
|
+
async reportHandler(inPayloads = []) {
|
|
189
|
+
var _a, _b;
|
|
190
|
+
await this.started("throw");
|
|
191
|
+
(_a = this.logger) == null ? void 0 : _a.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
|
+
(_b = this.logger) == null ? void 0 : _b.debug(`reportHandler:out: ${JSON.stringify(result)}`);
|
|
202
|
+
return result;
|
|
203
|
+
}
|
|
204
|
+
async start(timeout) {
|
|
205
|
+
var _a;
|
|
206
|
+
if (await super.start(timeout)) {
|
|
207
|
+
if ((((_a = this.config.automations) == null ? void 0 : _a.length) ?? 0) > 0) {
|
|
208
|
+
this.runner = new SentinelRunner(this, this.config.automations);
|
|
209
|
+
await this.runner.start();
|
|
210
|
+
}
|
|
211
|
+
return true;
|
|
212
|
+
}
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
async stop(timeout) {
|
|
216
|
+
if (this.runner) {
|
|
217
|
+
this.runner.stop();
|
|
218
|
+
this.runner = void 0;
|
|
219
|
+
}
|
|
220
|
+
return await super.stop(timeout);
|
|
221
|
+
}
|
|
222
|
+
async generateResults(tasks, previousResults, inPayloads) {
|
|
223
|
+
var _a, _b, _c, _d;
|
|
224
|
+
(_a = this.logger) == null ? void 0 : _a.debug(`generateResults:tasks: ${JSON.stringify(tasks.length)}`);
|
|
225
|
+
(_b = this.logger) == null ? void 0 : _b.debug(`generateResults:previous: ${JSON.stringify(previousResults)}`);
|
|
226
|
+
(_c = this.logger) == null ? void 0 : _c.debug(`generateResults:in: ${JSON.stringify(inPayloads)}`);
|
|
227
|
+
const results = await Promise.allSettled(tasks == null ? void 0 : tasks.map(async (task) => {
|
|
228
|
+
var _a2, _b2, _c2;
|
|
229
|
+
const input = task.input ?? false;
|
|
230
|
+
const inPayloadsFound = input === true ? inPayloads : input === false ? [] : this.processPreviousResults(previousResults, await this.inputAddresses(input));
|
|
231
|
+
const witness = asWitnessInstance(task.module);
|
|
232
|
+
if (witness) {
|
|
233
|
+
const observed = await witness.observe(inPayloadsFound);
|
|
234
|
+
(_a2 = this.logger) == null ? void 0 : _a2.debug(`observed [${witness.id}]: ${JSON.stringify(observed)}`);
|
|
235
|
+
return [
|
|
236
|
+
witness.address,
|
|
237
|
+
observed
|
|
238
|
+
];
|
|
239
|
+
}
|
|
240
|
+
const diviner = asDivinerInstance(task.module);
|
|
241
|
+
if (diviner) {
|
|
242
|
+
const divined = await diviner.divine(inPayloadsFound);
|
|
243
|
+
(_b2 = this.logger) == null ? void 0 : _b2.debug(`divined [${diviner.id}]: ${JSON.stringify(divined)}`);
|
|
244
|
+
return [
|
|
245
|
+
diviner.address,
|
|
246
|
+
divined
|
|
247
|
+
];
|
|
248
|
+
}
|
|
249
|
+
const sentinel = asSentinelInstance(task.module);
|
|
250
|
+
if (sentinel) {
|
|
251
|
+
const reported = await sentinel.report(inPayloadsFound);
|
|
252
|
+
(_c2 = this.logger) == null ? void 0 : _c2.debug(`reported [${sentinel.id}]: ${JSON.stringify(reported)}`);
|
|
253
|
+
return [
|
|
254
|
+
sentinel.address,
|
|
255
|
+
reported
|
|
256
|
+
];
|
|
257
|
+
}
|
|
258
|
+
throw new Error("Unsupported module type");
|
|
259
|
+
}));
|
|
260
|
+
const finalResult = {};
|
|
261
|
+
for (const result of results.filter(fulfilled)) {
|
|
262
|
+
const [address, payloads] = result.value;
|
|
263
|
+
finalResult[address] = finalResult[address] ?? [];
|
|
264
|
+
finalResult[address].push(...payloads);
|
|
265
|
+
}
|
|
266
|
+
if (this.throwErrors) {
|
|
267
|
+
const errors = results.filter(rejected).map((result) => result.reason);
|
|
268
|
+
if (errors.length > 0) {
|
|
269
|
+
throw new Error("At least one module failed");
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
(_d = this.logger) == null ? void 0 : _d.debug(`generateResults:out: ${JSON.stringify(finalResult)}`);
|
|
273
|
+
return finalResult;
|
|
274
|
+
}
|
|
275
|
+
async inputAddresses(input) {
|
|
276
|
+
if (Array.isArray(input)) {
|
|
277
|
+
return (await Promise.all(input.map(async (inputItem) => await this.inputAddresses(inputItem)))).flat();
|
|
278
|
+
} else {
|
|
279
|
+
const resolved = await this.resolve(input);
|
|
280
|
+
return resolved ? [
|
|
281
|
+
resolved.address
|
|
282
|
+
] : [];
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
processPreviousResults(payloads, inputs) {
|
|
286
|
+
return inputs.flatMap((input) => payloads[input] ?? []);
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
__name(_MemorySentinel, "MemorySentinel");
|
|
290
|
+
__publicField(_MemorySentinel, "configSchemas", [
|
|
291
|
+
SentinelConfigSchema
|
|
292
|
+
]);
|
|
293
|
+
var MemorySentinel = _MemorySentinel;
|
|
294
|
+
export {
|
|
295
|
+
MemorySentinel
|
|
296
|
+
};
|
|
297
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/MemorySentinel.ts","../../src/SentinelRunner.ts","../../src/SentinelIntervalAutomationWrapper.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 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":";;;;;;;;;AACA,SAASA,WAAWC,gBAAgB;AACpC,SAASC,yBAAyB;AAGlC,SAASC,wBAAwB;AACjC,SACEC,oBAGAC,4BAIK;AACP,SAASC,yBAAyB;;;ACflC,SAASC,gBAAgB;AAEzB,SAASC,kBAAAA,uBAAsB;AAC/B,SACEC,oCAIK;;;ACRP,SAASC,sBAAsB;AAGxB,IAAMC,qCAAN,MAAMA,2CAEHC,eAAAA;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;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K9B,eAAe8B,SAAAA,CAAAA,CAAAA,GAAcjD,KAAI;IACvG,OAAO;AACL,YAAMkD,WAAW,MAAM,KAAKC,QAAQnC,KAAAA;AACpC,aAAOkC,WAAW;QAACA,SAASxB;UAAW,CAAA;IACzC;EACF;EAEQR,uBAAuBqB,UAAqCa,QAAkB;AACpF,WAAOA,OAAOC,QAAQ,CAACrC,UAAUuB,SAASvB,KAAAA,KAAU,CAAA,CAAE;EACxD;AACF;AAvGUpC;AACR,cAJWD,iBAIK2E,iBAAgB;EAACC;;AAJ5B,IAAM5E,iBAAN;","names":["fulfilled","rejected","asDivinerInstance","AbstractSentinel","asSentinelInstance","SentinelConfigSchema","asWitnessInstance","assertEx","PayloadWrapper","isSentinelIntervalAutomation","PayloadWrapper","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","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","_a","_b","_c","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","configSchemas","SentinelConfigSchema"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@xyo-network/sentinel-memory",
|
|
3
|
+
"author": {
|
|
4
|
+
"email": "support@xyo.network",
|
|
5
|
+
"name": "XYO Development Team",
|
|
6
|
+
"url": "https://xyo.network"
|
|
7
|
+
},
|
|
8
|
+
"bugs": {
|
|
9
|
+
"email": "support@xyo.network",
|
|
10
|
+
"url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/issues"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@xylabs/assert": "^2.13.23",
|
|
14
|
+
"@xylabs/hex": "^2.13.23",
|
|
15
|
+
"@xylabs/promise": "^2.13.23",
|
|
16
|
+
"@xyo-network/diviner-model": "~2.85.5",
|
|
17
|
+
"@xyo-network/module-model": "~2.85.5",
|
|
18
|
+
"@xyo-network/payload-model": "~2.85.5",
|
|
19
|
+
"@xyo-network/payload-wrapper": "~2.85.5",
|
|
20
|
+
"@xyo-network/sentinel-abstract": "~2.85.5",
|
|
21
|
+
"@xyo-network/sentinel-model": "~2.85.5",
|
|
22
|
+
"@xyo-network/witness-model": "~2.85.5"
|
|
23
|
+
},
|
|
24
|
+
"description": "Primary SDK for using XYO Protocol 2.0",
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@xylabs/delay": "^2.13.23",
|
|
27
|
+
"@xylabs/ts-scripts-yarn3": "^3.2.25",
|
|
28
|
+
"@xylabs/tsconfig": "^3.2.25",
|
|
29
|
+
"@xyo-network/abstract-witness": "~2.85.5",
|
|
30
|
+
"@xyo-network/account": "~2.85.5",
|
|
31
|
+
"@xyo-network/archivist-memory": "~2.85.5",
|
|
32
|
+
"@xyo-network/id-payload-plugin": "~2.85.5",
|
|
33
|
+
"@xyo-network/node-memory": "~2.85.5",
|
|
34
|
+
"@xyo-network/witness-adhoc": "~2.85.5",
|
|
35
|
+
"typescript": "^5.3.3"
|
|
36
|
+
},
|
|
37
|
+
"types": "dist/node/index.d.ts",
|
|
38
|
+
"exports": {
|
|
39
|
+
".": {
|
|
40
|
+
"browser": {
|
|
41
|
+
"require": {
|
|
42
|
+
"types": "./dist/browser/index.d.cts",
|
|
43
|
+
"default": "./dist/browser/index.cjs"
|
|
44
|
+
},
|
|
45
|
+
"import": {
|
|
46
|
+
"types": "./dist/browser/index.d.mts",
|
|
47
|
+
"default": "./dist/browser/index.js"
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"node": {
|
|
51
|
+
"require": {
|
|
52
|
+
"types": "./dist/node/index.d.cts",
|
|
53
|
+
"default": "./dist/node/index.cjs"
|
|
54
|
+
},
|
|
55
|
+
"import": {
|
|
56
|
+
"types": "./dist/node/index.d.mts",
|
|
57
|
+
"default": "./dist/node/index.js"
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
"./package.json": "./package.json"
|
|
62
|
+
},
|
|
63
|
+
"main": "dist/node/index.cjs",
|
|
64
|
+
"module": "dist/node/index.js",
|
|
65
|
+
"homepage": "https://xyo.network",
|
|
66
|
+
"license": "LGPL-3.0-only",
|
|
67
|
+
"publishConfig": {
|
|
68
|
+
"access": "public"
|
|
69
|
+
},
|
|
70
|
+
"repository": {
|
|
71
|
+
"type": "git",
|
|
72
|
+
"url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js.git"
|
|
73
|
+
},
|
|
74
|
+
"sideEffects": false,
|
|
75
|
+
"version": "2.85.5",
|
|
76
|
+
"type": "module"
|
|
77
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { Address } from '@xylabs/hex'
|
|
2
|
+
import { fulfilled, rejected } from '@xylabs/promise'
|
|
3
|
+
import { asDivinerInstance } from '@xyo-network/diviner-model'
|
|
4
|
+
import { AnyConfigSchema } from '@xyo-network/module-model'
|
|
5
|
+
import { Payload } from '@xyo-network/payload-model'
|
|
6
|
+
import { AbstractSentinel } from '@xyo-network/sentinel-abstract'
|
|
7
|
+
import {
|
|
8
|
+
asSentinelInstance,
|
|
9
|
+
ResolvedTask,
|
|
10
|
+
SentinelConfig,
|
|
11
|
+
SentinelConfigSchema,
|
|
12
|
+
SentinelInstance,
|
|
13
|
+
SentinelModuleEventData,
|
|
14
|
+
SentinelParams,
|
|
15
|
+
} from '@xyo-network/sentinel-model'
|
|
16
|
+
import { asWitnessInstance } from '@xyo-network/witness-model'
|
|
17
|
+
|
|
18
|
+
import { SentinelRunner } from './SentinelRunner'
|
|
19
|
+
|
|
20
|
+
export type MemorySentinelParams<TConfig extends AnyConfigSchema<SentinelConfig> = AnyConfigSchema<SentinelConfig>> = SentinelParams<TConfig>
|
|
21
|
+
|
|
22
|
+
export class MemorySentinel<
|
|
23
|
+
TParams extends MemorySentinelParams = MemorySentinelParams,
|
|
24
|
+
TEventData extends SentinelModuleEventData<SentinelInstance<TParams>> = SentinelModuleEventData<SentinelInstance<TParams>>,
|
|
25
|
+
> extends AbstractSentinel<TParams, TEventData> {
|
|
26
|
+
static override configSchemas = [SentinelConfigSchema]
|
|
27
|
+
|
|
28
|
+
private runner?: SentinelRunner
|
|
29
|
+
|
|
30
|
+
async reportHandler(inPayloads: Payload[] = []): Promise<Payload[]> {
|
|
31
|
+
await this.started('throw')
|
|
32
|
+
this.logger?.debug(`reportHandler:in: ${JSON.stringify(inPayloads)}`)
|
|
33
|
+
const job = await this.jobPromise
|
|
34
|
+
|
|
35
|
+
let index = 0
|
|
36
|
+
let previousResults: Record<Address, Payload[]> = {}
|
|
37
|
+
while (index < job.tasks.length) {
|
|
38
|
+
const generatedPayloads = await this.generateResults(job.tasks[index], previousResults, inPayloads)
|
|
39
|
+
previousResults = generatedPayloads
|
|
40
|
+
index++
|
|
41
|
+
}
|
|
42
|
+
const result = Object.values(previousResults).flat()
|
|
43
|
+
this.logger?.debug(`reportHandler:out: ${JSON.stringify(result)}`)
|
|
44
|
+
return result
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
override async start(timeout?: number | undefined): Promise<boolean> {
|
|
48
|
+
if (await super.start(timeout)) {
|
|
49
|
+
if ((this.config.automations?.length ?? 0) > 0) {
|
|
50
|
+
this.runner = new SentinelRunner(this, this.config.automations)
|
|
51
|
+
await this.runner.start()
|
|
52
|
+
}
|
|
53
|
+
return true
|
|
54
|
+
}
|
|
55
|
+
return false
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
override async stop(timeout?: number | undefined): Promise<boolean> {
|
|
59
|
+
if (this.runner) {
|
|
60
|
+
this.runner.stop()
|
|
61
|
+
this.runner = undefined
|
|
62
|
+
}
|
|
63
|
+
return await super.stop(timeout)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
private async generateResults(
|
|
67
|
+
tasks: ResolvedTask[],
|
|
68
|
+
previousResults: Record<Address, Payload[]>,
|
|
69
|
+
inPayloads?: Payload[],
|
|
70
|
+
): Promise<Record<Address, Payload[]>> {
|
|
71
|
+
this.logger?.debug(`generateResults:tasks: ${JSON.stringify(tasks.length)}`)
|
|
72
|
+
this.logger?.debug(`generateResults:previous: ${JSON.stringify(previousResults)}`)
|
|
73
|
+
this.logger?.debug(`generateResults:in: ${JSON.stringify(inPayloads)}`)
|
|
74
|
+
const results: PromiseSettledResult<[Address, Payload[]]>[] = await Promise.allSettled(
|
|
75
|
+
tasks?.map(async (task) => {
|
|
76
|
+
const input = task.input ?? false
|
|
77
|
+
const inPayloadsFound =
|
|
78
|
+
input === true ? inPayloads : input === false ? [] : this.processPreviousResults(previousResults, await this.inputAddresses(input))
|
|
79
|
+
const witness = asWitnessInstance(task.module)
|
|
80
|
+
if (witness) {
|
|
81
|
+
const observed = await witness.observe(inPayloadsFound)
|
|
82
|
+
this.logger?.debug(`observed [${witness.id}]: ${JSON.stringify(observed)}`)
|
|
83
|
+
return [witness.address, observed]
|
|
84
|
+
}
|
|
85
|
+
const diviner = asDivinerInstance(task.module)
|
|
86
|
+
if (diviner) {
|
|
87
|
+
const divined = await diviner.divine(inPayloadsFound)
|
|
88
|
+
this.logger?.debug(`divined [${diviner.id}]: ${JSON.stringify(divined)}`)
|
|
89
|
+
return [diviner.address, divined]
|
|
90
|
+
}
|
|
91
|
+
const sentinel = asSentinelInstance(task.module)
|
|
92
|
+
if (sentinel) {
|
|
93
|
+
const reported = await sentinel.report(inPayloadsFound)
|
|
94
|
+
this.logger?.debug(`reported [${sentinel.id}]: ${JSON.stringify(reported)}`)
|
|
95
|
+
return [sentinel.address, reported]
|
|
96
|
+
}
|
|
97
|
+
throw new Error('Unsupported module type')
|
|
98
|
+
}),
|
|
99
|
+
)
|
|
100
|
+
const finalResult: Record<Address, Payload[]> = {}
|
|
101
|
+
for (const result of results.filter(fulfilled)) {
|
|
102
|
+
const [address, payloads] = result.value
|
|
103
|
+
finalResult[address] = finalResult[address] ?? []
|
|
104
|
+
finalResult[address].push(...payloads)
|
|
105
|
+
}
|
|
106
|
+
if (this.throwErrors) {
|
|
107
|
+
const errors = results.filter(rejected).map((result) => result.reason)
|
|
108
|
+
if (errors.length > 0) {
|
|
109
|
+
throw new Error('At least one module failed')
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
this.logger?.debug(`generateResults:out: ${JSON.stringify(finalResult)}`)
|
|
113
|
+
return finalResult
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
private async inputAddresses(input: string | string[]): Promise<string[]> {
|
|
117
|
+
if (Array.isArray(input)) {
|
|
118
|
+
return (await Promise.all(input.map(async (inputItem) => await this.inputAddresses(inputItem)))).flat()
|
|
119
|
+
} else {
|
|
120
|
+
const resolved = await this.resolve(input)
|
|
121
|
+
return resolved ? [resolved.address] : []
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
private processPreviousResults(payloads: Record<string, Payload[]>, inputs: string[]) {
|
|
126
|
+
return inputs.flatMap((input) => payloads[input] ?? [])
|
|
127
|
+
}
|
|
128
|
+
}
|