@xyo-network/diviner-payload-generic 3.6.0-rc.8 → 3.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/neutral/Diviner.d.ts +3 -4
- package/dist/neutral/Diviner.d.ts.map +1 -1
- package/dist/neutral/index.mjs +3 -2
- package/dist/neutral/index.mjs.map +1 -1
- package/package.json +21 -22
- package/src/Diviner.ts +11 -13
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
import { type EmptyObject } from '@xylabs/object';
|
|
1
|
+
import { type Hex } from '@xylabs/hex';
|
|
3
2
|
import type { ArchivistInstance, ArchivistModuleEventData } from '@xyo-network/archivist-model';
|
|
4
3
|
import type { DivinerInstance, DivinerModuleEventData } from '@xyo-network/diviner-model';
|
|
5
4
|
import { PayloadDiviner } from '@xyo-network/diviner-payload-abstract';
|
|
6
|
-
import type
|
|
5
|
+
import { type Order, type PayloadDivinerConfig, type PayloadDivinerParams, type PayloadDivinerQueryPayload } from '@xyo-network/diviner-payload-model';
|
|
7
6
|
import type { EventListener } from '@xyo-network/module-events';
|
|
8
7
|
import { type Payload, type Schema, type WithStorageMeta } from '@xyo-network/payload-model';
|
|
9
8
|
export declare const GenericPayloadDivinerConfigSchema: "network.xyo.diviner.payload.generic.config";
|
|
@@ -11,7 +10,7 @@ export type GenericPayloadDivinerConfigSchema = typeof GenericPayloadDivinerConf
|
|
|
11
10
|
export type GenericPayloadDivinerConfig = PayloadDivinerConfig<{
|
|
12
11
|
indexes?: string[];
|
|
13
12
|
}, GenericPayloadDivinerConfigSchema>;
|
|
14
|
-
export declare class GenericPayloadDiviner<TParams extends PayloadDivinerParams<GenericPayloadDivinerConfig> = PayloadDivinerParams<GenericPayloadDivinerConfig>, TIn extends PayloadDivinerQueryPayload
|
|
13
|
+
export declare class GenericPayloadDiviner<TParams extends PayloadDivinerParams<GenericPayloadDivinerConfig> = PayloadDivinerParams<GenericPayloadDivinerConfig>, TIn extends PayloadDivinerQueryPayload = PayloadDivinerQueryPayload, TOut extends WithStorageMeta<Payload> = WithStorageMeta<Payload>, TEventData extends DivinerModuleEventData<DivinerInstance<TParams, TIn, TOut>, TIn, TOut> = DivinerModuleEventData<DivinerInstance<TParams, TIn, TOut>, TIn, TOut>> extends PayloadDiviner<TParams, TIn, TOut, TEventData> {
|
|
15
14
|
static readonly configSchemas: Schema[];
|
|
16
15
|
static readonly defaultConfigSchema: Schema;
|
|
17
16
|
protected indexMaps: Record<string, WithStorageMeta<TOut>[]>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Diviner.d.ts","sourceRoot":"","sources":["../../src/Diviner.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Diviner.d.ts","sourceRoot":"","sources":["../../src/Diviner.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,GAAG,EAAE,MAAM,aAAa,CAAA;AACtC,OAAO,KAAK,EAAE,iBAAiB,EAAE,wBAAwB,EAAE,MAAM,8BAA8B,CAAA;AAC/F,OAAO,KAAK,EAAE,eAAe,EAAE,sBAAsB,EAAE,MAAM,4BAA4B,CAAA;AACzF,OAAO,EAAE,cAAc,EAAE,MAAM,uCAAuC,CAAA;AACtE,OAAO,EAEL,KAAK,KAAK,EACV,KAAK,oBAAoB,EACzB,KAAK,oBAAoB,EACzB,KAAK,0BAA0B,EAChC,MAAM,oCAAoC,CAAA;AAC3C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAA;AAE/D,OAAO,EACL,KAAK,OAAO,EAAE,KAAK,MAAM,EACzB,KAAK,eAAe,EACrB,MAAM,4BAA4B,CAAA;AAMnC,eAAO,MAAM,iCAAiC,EAAG,4CAAqD,CAAA;AACtG,MAAM,MAAM,iCAAiC,GAAG,OAAO,iCAAiC,CAAA;AAExF,MAAM,MAAM,2BAA2B,GAAG,oBAAoB,CAC5D;IACE,OAAO,CAAC,EAAE,MAAM,EAAE,CAAA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Aa,CAAC,QAAQ,CAAC,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC;IAkCzE,SAAS,CAAC,kBAAkB,EAAE,aAAa,CAAC,wBAAwB,CAAC,SAAS,CAAC,CAAC,CAO/E;IAGD,SAAS,CAAC,kBAAkB,EAAE,aAAa,CAAC,wBAAwB,CAAC,SAAS,CAAC,CAAC,CAO/E;IAED,SAAS,CAAC,mBAAmB,EAAE,aAAa,CAAC,wBAAwB,CAAC,UAAU,CAAC,CAAC,CAEjF;cAEwB,WAAW,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO,CAAC,OAAO,CAAC;cASrE,WAAW;IAa3B,OAAO,CAAC,aAAa;CAYtB"}
|
package/dist/neutral/index.mjs
CHANGED
|
@@ -2,10 +2,11 @@ var __defProp = Object.defineProperty;
|
|
|
2
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
3
|
|
|
4
4
|
// src/Diviner.ts
|
|
5
|
+
import { filterAs } from "@xylabs/array";
|
|
5
6
|
import { assertEx } from "@xylabs/assert";
|
|
6
7
|
import { forget } from "@xylabs/forget";
|
|
7
8
|
import { PayloadDiviner } from "@xyo-network/diviner-payload-abstract";
|
|
8
|
-
import {
|
|
9
|
+
import { asPayloadDivinerQueryPayload } from "@xyo-network/diviner-payload-model";
|
|
9
10
|
import { PayloadBuilder } from "@xyo-network/payload-builder";
|
|
10
11
|
import { Mutex } from "async-mutex";
|
|
11
12
|
var DEFAULT_INDEX_BATCH_SIZE = 100;
|
|
@@ -64,7 +65,7 @@ var GenericPayloadDiviner = class extends PayloadDiviner {
|
|
|
64
65
|
});
|
|
65
66
|
}
|
|
66
67
|
async divineHandler(payloads) {
|
|
67
|
-
const filters = payloads
|
|
68
|
+
const filters = filterAs(payloads ?? [], asPayloadDivinerQueryPayload);
|
|
68
69
|
assertEx(filters.length < 2, () => "Multiple PayloadDivinerQuery payloads may not be specified");
|
|
69
70
|
const filter = assertEx(filters.shift(), () => "No PayloadDivinerQuery specified");
|
|
70
71
|
await this.updateIndex();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/Diviner.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport { forget } from '@xylabs/forget'\nimport { type Hash, type Hex } from '@xylabs/hex'\nimport { type EmptyObject } from '@xylabs/object'\nimport type { ArchivistInstance, ArchivistModuleEventData } from '@xyo-network/archivist-model'\nimport type { DivinerInstance, DivinerModuleEventData } from '@xyo-network/diviner-model'\nimport { PayloadDiviner } from '@xyo-network/diviner-payload-abstract'\nimport type {\n Order,\n PayloadDivinerConfig,\n PayloadDivinerParams,\n PayloadDivinerQueryPayload,\n} from '@xyo-network/diviner-payload-model'\nimport { isPayloadDivinerQueryPayload } from '@xyo-network/diviner-payload-model'\nimport type { EventListener } from '@xyo-network/module-events'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport {\n type Payload, type Schema,\n type WithStorageMeta,\n} from '@xyo-network/payload-model'\nimport { Mutex } from 'async-mutex'\n\nconst DEFAULT_INDEX_BATCH_SIZE = 100 as const\nconst DEFAULT_MAX_INDEX_SIZE = 8000 as const\n\nexport const GenericPayloadDivinerConfigSchema = 'network.xyo.diviner.payload.generic.config' as const\nexport type GenericPayloadDivinerConfigSchema = typeof GenericPayloadDivinerConfigSchema\n\nexport type GenericPayloadDivinerConfig = PayloadDivinerConfig<\n {\n indexes?: string[]\n },\n GenericPayloadDivinerConfigSchema\n>\n\nexport class GenericPayloadDiviner<\n TParams extends PayloadDivinerParams<GenericPayloadDivinerConfig> = PayloadDivinerParams<GenericPayloadDivinerConfig>,\n TIn extends PayloadDivinerQueryPayload<EmptyObject, Hash> = PayloadDivinerQueryPayload<EmptyObject, Hash>,\n TOut extends WithStorageMeta<Payload> = WithStorageMeta<Payload>,\n TEventData extends DivinerModuleEventData<DivinerInstance<TParams, TIn, TOut>, TIn, TOut> = DivinerModuleEventData<\n DivinerInstance<TParams, TIn, TOut>,\n TIn,\n TOut\n >,\n> extends PayloadDiviner<TParams, TIn, TOut, TEventData> {\n static override readonly configSchemas: Schema[] = [...super.configSchemas, GenericPayloadDivinerConfigSchema]\n static override readonly defaultConfigSchema: Schema = GenericPayloadDivinerConfigSchema\n\n protected indexMaps: Record<string, WithStorageMeta<TOut>[]> = {}\n protected payloadsWithMeta: WithStorageMeta<TOut>[] = []\n\n private _archivistInstance?: ArchivistInstance\n private _cursor?: Hex\n private _updatePayloadPairsMutex = new Mutex()\n\n protected get indexBatchSize() {\n return this.config.indexBatchSize ?? DEFAULT_INDEX_BATCH_SIZE\n }\n\n protected get indexes(): string[] {\n return ['schema', ...(this.config.indexes ?? [])]\n }\n\n protected get maxIndexSize() {\n return this.config.maxIndexSize ?? DEFAULT_MAX_INDEX_SIZE\n }\n\n protected all(order: Order = 'desc', cursor?: Hex) {\n const payloads = this.payloadsWithMeta.toSorted(PayloadBuilder.compareStorageMeta)\n if (order === 'desc') payloads.reverse()\n const startIndex = (cursor ? (payloads.findIndex(payload => payload._sequence === cursor) ?? -1) : -1) + 1\n return payloads.slice(startIndex)\n }\n\n protected override async archivistInstance(): Promise<ArchivistInstance | undefined>\n protected override async archivistInstance(required: true): Promise<ArchivistInstance>\n protected override async archivistInstance(required = false): Promise<ArchivistInstance | undefined> {\n if (!this._archivistInstance) {\n const archivist = await super.archivistInstance()\n if (required && !archivist) {\n throw new Error('Failed to find archivist')\n }\n archivist?.on('inserted', this.onArchivistInserted)\n archivist?.on('cleared', this.onArchivistCleared)\n archivist?.on('deleted', this.onArchivistDeleted)\n this._archivistInstance = archivist\n }\n return this._archivistInstance\n }\n\n protected async clearIndex() {\n await this._updatePayloadPairsMutex.runExclusive(() => {\n this._cursor = undefined\n this.payloadsWithMeta = []\n this.indexMaps = {}\n })\n }\n\n protected override async divineHandler(payloads?: TIn[]): Promise<TOut[]> {\n const filters = payloads?.filter(isPayloadDivinerQueryPayload) ?? []\n assertEx(filters.length < 2, () => 'Multiple PayloadDivinerQuery payloads may not be specified')\n const filter = assertEx(filters.shift(), () => 'No PayloadDivinerQuery specified') as unknown as\n PayloadDivinerQueryPayload<EmptyObject, Hash>\n\n await this.updateIndex()\n\n const {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n schema, schemas, order, limit, cursor, ...props\n } = filter\n let all: TOut[] = this.all(order, cursor)\n if (all) {\n if (schemas?.length) all = all.filter(payload => schemas.includes(payload.schema))\n if (Object.keys(props).length > 0) {\n const additionalFilterCriteria = Object.entries(props)\n for (const [prop, filter] of additionalFilterCriteria) {\n const property = prop as keyof TOut\n all\n = Array.isArray(filter)\n ? all.filter(payload =>\n filter.every((value) => {\n const prop = payload?.[property]\n // TODO: This seems to be written just to check arrays, and now that $meta is there, need to check type?\n return Array.isArray(prop) && prop.includes?.(value)\n }))\n : all.filter(payload => payload?.[property] === filter)\n }\n }\n return limit ? all.slice(0, limit) : all\n } else {\n throw new Error('Archivist does not support \"all\"')\n }\n }\n\n protected onArchivistCleared: EventListener<ArchivistModuleEventData['cleared']> = () => {\n forget(\n (async () => {\n await this.clearIndex()\n await this.updateIndex()\n })(),\n )\n }\n\n // we are just rebuilding the entire index at this point on delete since large archivists do not support delete\n protected onArchivistDeleted: EventListener<ArchivistModuleEventData['deleted']> = () => {\n forget(\n (async () => {\n await this.clearIndex()\n await this.updateIndex()\n })(),\n )\n }\n\n protected onArchivistInserted: EventListener<ArchivistModuleEventData['inserted']> = () => {\n forget(this.updateIndex())\n }\n\n protected override async stopHandler(_timeout?: number | undefined): Promise<boolean> {\n const archivist = await this.archivistInstance(true)\n archivist.off('inserted', this.onArchivistInserted)\n archivist.off('deleted', this.onArchivistDeleted)\n archivist.off('cleared', this.onArchivistCleared)\n return await super.stopHandler()\n }\n\n // index any new payloads\n protected async updateIndex() {\n await this._updatePayloadPairsMutex.runExclusive(async () => {\n const archivist = await this.archivistInstance(true)\n let newPayloads = await archivist.next({ limit: 100, cursor: this._cursor }) as TOut[]\n while (newPayloads.length > 0) {\n this._cursor = newPayloads.at(-1)?._sequence\n assertEx(this.payloadsWithMeta.length + newPayloads.length <= this.maxIndexSize, () => 'maxIndexSize exceeded')\n this.indexPayloads(newPayloads)\n newPayloads = await archivist.next({ limit: 100, cursor: this._cursor }) as TOut[]\n }\n })\n }\n\n private indexPayloads(payloads: WithStorageMeta<TOut>[]): Hex {\n this.payloadsWithMeta.push(...payloads)\n\n // update the custom indexes\n for (const index of this.indexes ?? []) {\n this.indexMaps[index] = this.indexMaps[index] ?? []\n for (const payload of payloads) {\n this.indexMaps[index].push(payload)\n }\n }\n return assertEx(payloads.at(-1), () => 'No payloads to index')._sequence\n }\n}\n"],"mappings":";;;;AAAA,SAASA,gBAAgB;AACzB,SAASC,cAAc;AAKvB,SAASC,sBAAsB;AAO/B,SAASC,oCAAoC;AAE7C,SAASC,sBAAsB;AAK/B,SAASC,aAAa;AAEtB,IAAMC,2BAA2B;AACjC,IAAMC,yBAAyB;AAExB,IAAMC,oCAAoC;AAU1C,IAAMC,wBAAN,cASGC,eAAAA;EA5CV,OA4CUA;;;EACR,OAAyBC,gBAA0B;OAAI,MAAMA;IAAeH;;EAC5E,OAAyBI,sBAA8BJ;EAE7CK,YAAqD,CAAC;EACtDC,mBAA4C,CAAA;EAE9CC;EACAC;EACAC,2BAA2B,IAAIC,MAAAA;EAEvC,IAAcC,iBAAiB;AAC7B,WAAO,KAAKC,OAAOD,kBAAkBb;EACvC;EAEA,IAAce,UAAoB;AAChC,WAAO;MAAC;SAAc,KAAKD,OAAOC,WAAW,CAAA;;EAC/C;EAEA,IAAcC,eAAe;AAC3B,WAAO,KAAKF,OAAOE,gBAAgBf;EACrC;EAEUgB,IAAIC,QAAe,QAAQC,QAAc;AACjD,UAAMC,WAAW,KAAKZ,iBAAiBa,SAASC,eAAeC,kBAAkB;AACjF,QAAIL,UAAU,OAAQE,UAASI,QAAO;AACtC,UAAMC,cAAcN,SAAUC,SAASM,UAAUC,CAAAA,YAAWA,QAAQC,cAAcT,MAAAA,K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AYtB,SAAS,GAAG;AAC7B,aAAKpC,UAAU0D,YAAYE,GAAG,EAAC,GAAI1C;AACnCiB,iBAAS,KAAKrC,iBAAiBsC,SAASsB,YAAYtB,UAAU,KAAK9B,cAAc,MAAM,uBAAA;AACvF,aAAKuD,cAAcH,WAAAA;AACnBA,sBAAc,MAAMpC,UAAUqC,KAAK;UAAElB,OAAO;UAAKhC,QAAQ,KAAKT;QAAQ,CAAA;MACxE;IACF,CAAA;EACF;EAEQ6D,cAAcnD,UAAwC;AAC5D,SAAKZ,iBAAiBgE,KAAI,GAAIpD,QAAAA;AAG9B,eAAWqD,SAAS,KAAK1D,WAAW,CAAA,GAAI;AACtC,WAAKR,UAAUkE,KAAAA,IAAS,KAAKlE,UAAUkE,KAAAA,KAAU,CAAA;AACjD,iBAAW9C,WAAWP,UAAU;AAC9B,aAAKb,UAAUkE,KAAAA,EAAOD,KAAK7C,OAAAA;MAC7B;IACF;AACA,WAAOkB,SAASzB,SAASkD,GAAG,EAAC,GAAI,MAAM,sBAAA,EAAwB1C;EACjE;AACF;","names":["assertEx","forget","PayloadDiviner","isPayloadDivinerQueryPayload","PayloadBuilder","Mutex","DEFAULT_INDEX_BATCH_SIZE","DEFAULT_MAX_INDEX_SIZE","GenericPayloadDivinerConfigSchema","GenericPayloadDiviner","PayloadDiviner","configSchemas","defaultConfigSchema","indexMaps","payloadsWithMeta","_archivistInstance","_cursor","_updatePayloadPairsMutex","Mutex","indexBatchSize","config","indexes","maxIndexSize","all","order","cursor","payloads","toSorted","PayloadBuilder","compareStorageMeta","reverse","startIndex","findIndex","payload","_sequence","slice","archivistInstance","required","archivist","Error","on","onArchivistInserted","onArchivistCleared","onArchivistDeleted","clearIndex","runExclusive","undefined","divineHandler","filters","filter","isPayloadDivinerQueryPayload","assertEx","length","shift","updateIndex","schema","schemas","limit","props","includes","Object","keys","additionalFilterCriteria","entries","prop","property","Array","isArray","every","value","forget","stopHandler","_timeout","off","newPayloads","next","at","indexPayloads","push","index"]}
|
|
1
|
+
{"version":3,"sources":["../../src/Diviner.ts"],"sourcesContent":["import { filterAs } from '@xylabs/array'\nimport { assertEx } from '@xylabs/assert'\nimport { forget } from '@xylabs/forget'\nimport { type Hex } from '@xylabs/hex'\nimport type { ArchivistInstance, ArchivistModuleEventData } from '@xyo-network/archivist-model'\nimport type { DivinerInstance, DivinerModuleEventData } from '@xyo-network/diviner-model'\nimport { PayloadDiviner } from '@xyo-network/diviner-payload-abstract'\nimport {\n asPayloadDivinerQueryPayload,\n type Order,\n type PayloadDivinerConfig,\n type PayloadDivinerParams,\n type PayloadDivinerQueryPayload,\n} from '@xyo-network/diviner-payload-model'\nimport type { EventListener } from '@xyo-network/module-events'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport {\n type Payload, type Schema,\n type WithStorageMeta,\n} from '@xyo-network/payload-model'\nimport { Mutex } from 'async-mutex'\n\nconst DEFAULT_INDEX_BATCH_SIZE = 100 as const\nconst DEFAULT_MAX_INDEX_SIZE = 8000 as const\n\nexport const GenericPayloadDivinerConfigSchema = 'network.xyo.diviner.payload.generic.config' as const\nexport type GenericPayloadDivinerConfigSchema = typeof GenericPayloadDivinerConfigSchema\n\nexport type GenericPayloadDivinerConfig = PayloadDivinerConfig<\n {\n indexes?: string[]\n },\n GenericPayloadDivinerConfigSchema\n>\n\nexport class GenericPayloadDiviner<\n TParams extends PayloadDivinerParams<GenericPayloadDivinerConfig> = PayloadDivinerParams<GenericPayloadDivinerConfig>,\n TIn extends PayloadDivinerQueryPayload = PayloadDivinerQueryPayload,\n TOut extends WithStorageMeta<Payload> = WithStorageMeta<Payload>,\n TEventData extends DivinerModuleEventData<DivinerInstance<TParams, TIn, TOut>, TIn, TOut> = DivinerModuleEventData<\n DivinerInstance<TParams, TIn, TOut>,\n TIn,\n TOut\n >,\n> extends PayloadDiviner<TParams, TIn, TOut, TEventData> {\n static override readonly configSchemas: Schema[] = [...super.configSchemas, GenericPayloadDivinerConfigSchema]\n static override readonly defaultConfigSchema: Schema = GenericPayloadDivinerConfigSchema\n\n protected indexMaps: Record<string, WithStorageMeta<TOut>[]> = {}\n protected payloadsWithMeta: WithStorageMeta<TOut>[] = []\n\n private _archivistInstance?: ArchivistInstance\n private _cursor?: Hex\n private _updatePayloadPairsMutex = new Mutex()\n\n protected get indexBatchSize() {\n return this.config.indexBatchSize ?? DEFAULT_INDEX_BATCH_SIZE\n }\n\n protected get indexes(): string[] {\n return ['schema', ...(this.config.indexes ?? [])]\n }\n\n protected get maxIndexSize() {\n return this.config.maxIndexSize ?? DEFAULT_MAX_INDEX_SIZE\n }\n\n protected all(order: Order = 'desc', cursor?: Hex) {\n const payloads = this.payloadsWithMeta.toSorted(PayloadBuilder.compareStorageMeta)\n if (order === 'desc') payloads.reverse()\n const startIndex = (cursor ? (payloads.findIndex(payload => payload._sequence === cursor) ?? -1) : -1) + 1\n return payloads.slice(startIndex)\n }\n\n protected override async archivistInstance(): Promise<ArchivistInstance | undefined>\n protected override async archivistInstance(required: true): Promise<ArchivistInstance>\n protected override async archivistInstance(required = false): Promise<ArchivistInstance | undefined> {\n if (!this._archivistInstance) {\n const archivist = await super.archivistInstance()\n if (required && !archivist) {\n throw new Error('Failed to find archivist')\n }\n archivist?.on('inserted', this.onArchivistInserted)\n archivist?.on('cleared', this.onArchivistCleared)\n archivist?.on('deleted', this.onArchivistDeleted)\n this._archivistInstance = archivist\n }\n return this._archivistInstance\n }\n\n protected async clearIndex() {\n await this._updatePayloadPairsMutex.runExclusive(() => {\n this._cursor = undefined\n this.payloadsWithMeta = []\n this.indexMaps = {}\n })\n }\n\n protected override async divineHandler(payloads?: TIn[]): Promise<TOut[]> {\n const filters = filterAs(payloads ?? [], asPayloadDivinerQueryPayload)\n assertEx(filters.length < 2, () => 'Multiple PayloadDivinerQuery payloads may not be specified')\n const filter = assertEx(filters.shift(), () => 'No PayloadDivinerQuery specified') as PayloadDivinerQueryPayload\n await this.updateIndex()\n\n const {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n schema, schemas, order, limit, cursor, ...props\n } = filter\n let all: TOut[] = this.all(order, cursor)\n if (all) {\n if (schemas?.length) all = all.filter(payload => schemas.includes(payload.schema))\n if (Object.keys(props).length > 0) {\n const additionalFilterCriteria = Object.entries(props)\n for (const [prop, filter] of additionalFilterCriteria) {\n const property = prop as keyof TOut\n all\n = Array.isArray(filter)\n ? all.filter(payload =>\n filter.every((value) => {\n const prop = payload?.[property]\n // TODO: This seems to be written just to check arrays, and now that $meta is there, need to check type?\n return Array.isArray(prop) && prop.includes?.(value)\n }))\n : all.filter(payload => payload?.[property] === filter)\n }\n }\n return limit ? all.slice(0, limit) : all\n } else {\n throw new Error('Archivist does not support \"all\"')\n }\n }\n\n protected onArchivistCleared: EventListener<ArchivistModuleEventData['cleared']> = () => {\n forget(\n (async () => {\n await this.clearIndex()\n await this.updateIndex()\n })(),\n )\n }\n\n // we are just rebuilding the entire index at this point on delete since large archivists do not support delete\n protected onArchivistDeleted: EventListener<ArchivistModuleEventData['deleted']> = () => {\n forget(\n (async () => {\n await this.clearIndex()\n await this.updateIndex()\n })(),\n )\n }\n\n protected onArchivistInserted: EventListener<ArchivistModuleEventData['inserted']> = () => {\n forget(this.updateIndex())\n }\n\n protected override async stopHandler(_timeout?: number | undefined): Promise<boolean> {\n const archivist = await this.archivistInstance(true)\n archivist.off('inserted', this.onArchivistInserted)\n archivist.off('deleted', this.onArchivistDeleted)\n archivist.off('cleared', this.onArchivistCleared)\n return await super.stopHandler()\n }\n\n // index any new payloads\n protected async updateIndex() {\n await this._updatePayloadPairsMutex.runExclusive(async () => {\n const archivist = await this.archivistInstance(true)\n let newPayloads = await archivist.next({ limit: 100, cursor: this._cursor }) as TOut[]\n while (newPayloads.length > 0) {\n this._cursor = newPayloads.at(-1)?._sequence\n assertEx(this.payloadsWithMeta.length + newPayloads.length <= this.maxIndexSize, () => 'maxIndexSize exceeded')\n this.indexPayloads(newPayloads)\n newPayloads = await archivist.next({ limit: 100, cursor: this._cursor }) as TOut[]\n }\n })\n }\n\n private indexPayloads(payloads: WithStorageMeta<TOut>[]): Hex {\n this.payloadsWithMeta.push(...payloads)\n\n // update the custom indexes\n for (const index of this.indexes ?? []) {\n this.indexMaps[index] = this.indexMaps[index] ?? []\n for (const payload of payloads) {\n this.indexMaps[index].push(payload)\n }\n }\n return assertEx(payloads.at(-1), () => 'No payloads to index')._sequence\n }\n}\n"],"mappings":";;;;AAAA,SAASA,gBAAgB;AACzB,SAASC,gBAAgB;AACzB,SAASC,cAAc;AAIvB,SAASC,sBAAsB;AAC/B,SACEC,oCAKK;AAEP,SAASC,sBAAsB;AAK/B,SAASC,aAAa;AAEtB,IAAMC,2BAA2B;AACjC,IAAMC,yBAAyB;AAExB,IAAMC,oCAAoC;AAU1C,IAAMC,wBAAN,cASGC,eAAAA;EA5CV,OA4CUA;;;EACR,OAAyBC,gBAA0B;OAAI,MAAMA;IAAeH;;EAC5E,OAAyBI,sBAA8BJ;EAE7CK,YAAqD,CAAC;EACtDC,mBAA4C,CAAA;EAE9CC;EACAC;EACAC,2BAA2B,IAAIC,MAAAA;EAEvC,IAAcC,iBAAiB;AAC7B,WAAO,KAAKC,OAAOD,kBAAkBb;EACvC;EAEA,IAAce,UAAoB;AAChC,WAAO;MAAC;SAAc,KAAKD,OAAOC,WAAW,CAAA;;EAC/C;EAEA,IAAcC,eAAe;AAC3B,WAAO,KAAKF,OAAOE,gBAAgBf;EACrC;EAEUgB,IAAIC,QAAe,QAAQC,QAAc;AACjD,UAAMC,WAAW,KAAKZ,iBAAiBa,SAASC,eAAeC,kBAAkB;AACjF,QAAIL,UAAU,OAAQE,UAASI,QAAO;AACtC,UAAMC,cAAcN,SAAUC,SAASM,UAAUC,CAAAA,YAAWA,QAAQC,cAAcT,MAAAA,KAAW,KAAM,MAAM;AACzG,WAAOC,SAASS,MAAMJ,UAAAA;EACxB;EAIA,MAAyBK,kBAAkBC,WAAW,OAA+C;AACnG,QAAI,CAAC,KAAKtB,oBAAoB;AAC5B,YAAMuB,YAAY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AACvF,aAAKwD,cAAcH,WAAAA;AACnBA,sBAAc,MAAMrC,UAAUsC,KAAK;UAAElB,OAAO;UAAKjC,QAAQ,KAAKT;QAAQ,CAAA;MACxE;IACF,CAAA;EACF;EAEQ8D,cAAcpD,UAAwC;AAC5D,SAAKZ,iBAAiBiE,KAAI,GAAIrD,QAAAA;AAG9B,eAAWsD,SAAS,KAAK3D,WAAW,CAAA,GAAI;AACtC,WAAKR,UAAUmE,KAAAA,IAAS,KAAKnE,UAAUmE,KAAAA,KAAU,CAAA;AACjD,iBAAW/C,WAAWP,UAAU;AAC9B,aAAKb,UAAUmE,KAAAA,EAAOD,KAAK9C,OAAAA;MAC7B;IACF;AACA,WAAOkB,SAASzB,SAASmD,GAAG,EAAC,GAAI,MAAM,sBAAA,EAAwB3C;EACjE;AACF;","names":["filterAs","assertEx","forget","PayloadDiviner","asPayloadDivinerQueryPayload","PayloadBuilder","Mutex","DEFAULT_INDEX_BATCH_SIZE","DEFAULT_MAX_INDEX_SIZE","GenericPayloadDivinerConfigSchema","GenericPayloadDiviner","PayloadDiviner","configSchemas","defaultConfigSchema","indexMaps","payloadsWithMeta","_archivistInstance","_cursor","_updatePayloadPairsMutex","Mutex","indexBatchSize","config","indexes","maxIndexSize","all","order","cursor","payloads","toSorted","PayloadBuilder","compareStorageMeta","reverse","startIndex","findIndex","payload","_sequence","slice","archivistInstance","required","archivist","Error","on","onArchivistInserted","onArchivistCleared","onArchivistDeleted","clearIndex","runExclusive","undefined","divineHandler","filters","filterAs","asPayloadDivinerQueryPayload","assertEx","length","filter","shift","updateIndex","schema","schemas","limit","props","includes","Object","keys","additionalFilterCriteria","entries","prop","property","Array","isArray","every","value","forget","stopHandler","_timeout","off","newPayloads","next","at","indexPayloads","push","index"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xyo-network/diviner-payload-generic",
|
|
3
|
-
"version": "3.6.0
|
|
3
|
+
"version": "3.6.0",
|
|
4
4
|
"description": "Primary SDK for using XYO Protocol 2.0",
|
|
5
5
|
"homepage": "https://xyo.network",
|
|
6
6
|
"bugs": {
|
|
@@ -29,34 +29,33 @@
|
|
|
29
29
|
"module": "dist/neutral/index.mjs",
|
|
30
30
|
"types": "dist/neutral/index.d.ts",
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@xylabs/
|
|
33
|
-
"@xylabs/
|
|
34
|
-
"@xylabs/
|
|
35
|
-
"@xylabs/
|
|
36
|
-
"@xyo-network/archivist-model": "^3.6.0
|
|
37
|
-
"@xyo-network/diviner-model": "^3.6.0
|
|
38
|
-
"@xyo-network/diviner-payload-abstract": "^3.6.0
|
|
39
|
-
"@xyo-network/diviner-payload-model": "^3.6.0
|
|
40
|
-
"@xyo-network/module-events": "^3.6.0
|
|
41
|
-
"@xyo-network/payload-builder": "^3.6.0
|
|
42
|
-
"@xyo-network/payload-model": "^3.6.0
|
|
32
|
+
"@xylabs/array": "^4.4.26",
|
|
33
|
+
"@xylabs/assert": "^4.4.26",
|
|
34
|
+
"@xylabs/forget": "^4.4.26",
|
|
35
|
+
"@xylabs/hex": "^4.4.26",
|
|
36
|
+
"@xyo-network/archivist-model": "^3.6.0",
|
|
37
|
+
"@xyo-network/diviner-model": "^3.6.0",
|
|
38
|
+
"@xyo-network/diviner-payload-abstract": "^3.6.0",
|
|
39
|
+
"@xyo-network/diviner-payload-model": "^3.6.0",
|
|
40
|
+
"@xyo-network/module-events": "^3.6.0",
|
|
41
|
+
"@xyo-network/payload-builder": "^3.6.0",
|
|
42
|
+
"@xyo-network/payload-model": "^3.6.0",
|
|
43
43
|
"async-mutex": "^0.5.0"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@xylabs/delay": "^4.4.
|
|
47
|
-
"@xylabs/ts-scripts-yarn3": "^4.2.
|
|
48
|
-
"@xylabs/tsconfig": "^4.2.
|
|
49
|
-
"@xylabs/vitest-extended": "^4.4.
|
|
50
|
-
"@xyo-network/archivist-indexeddb": "^3.6.0
|
|
51
|
-
"@xyo-network/archivist-memory": "^3.6.0
|
|
52
|
-
"@xyo-network/node-memory": "^3.6.0
|
|
53
|
-
"@xyo-network/payload-builder": "^3.6.0
|
|
46
|
+
"@xylabs/delay": "^4.4.26",
|
|
47
|
+
"@xylabs/ts-scripts-yarn3": "^4.2.6",
|
|
48
|
+
"@xylabs/tsconfig": "^4.2.6",
|
|
49
|
+
"@xylabs/vitest-extended": "^4.4.26",
|
|
50
|
+
"@xyo-network/archivist-indexeddb": "^3.6.0",
|
|
51
|
+
"@xyo-network/archivist-memory": "^3.6.0",
|
|
52
|
+
"@xyo-network/node-memory": "^3.6.0",
|
|
53
|
+
"@xyo-network/payload-builder": "^3.6.0",
|
|
54
54
|
"fake-indexeddb": "^6.0.0",
|
|
55
55
|
"typescript": "^5.7.2",
|
|
56
56
|
"vitest": "^2.1.8"
|
|
57
57
|
},
|
|
58
58
|
"publishConfig": {
|
|
59
59
|
"access": "public"
|
|
60
|
-
}
|
|
61
|
-
"stableVersion": "3.5.2"
|
|
60
|
+
}
|
|
62
61
|
}
|
package/src/Diviner.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
+
import { filterAs } from '@xylabs/array'
|
|
1
2
|
import { assertEx } from '@xylabs/assert'
|
|
2
3
|
import { forget } from '@xylabs/forget'
|
|
3
|
-
import { type
|
|
4
|
-
import { type EmptyObject } from '@xylabs/object'
|
|
4
|
+
import { type Hex } from '@xylabs/hex'
|
|
5
5
|
import type { ArchivistInstance, ArchivistModuleEventData } from '@xyo-network/archivist-model'
|
|
6
6
|
import type { DivinerInstance, DivinerModuleEventData } from '@xyo-network/diviner-model'
|
|
7
7
|
import { PayloadDiviner } from '@xyo-network/diviner-payload-abstract'
|
|
8
|
-
import
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
import {
|
|
9
|
+
asPayloadDivinerQueryPayload,
|
|
10
|
+
type Order,
|
|
11
|
+
type PayloadDivinerConfig,
|
|
12
|
+
type PayloadDivinerParams,
|
|
13
|
+
type PayloadDivinerQueryPayload,
|
|
13
14
|
} from '@xyo-network/diviner-payload-model'
|
|
14
|
-
import { isPayloadDivinerQueryPayload } from '@xyo-network/diviner-payload-model'
|
|
15
15
|
import type { EventListener } from '@xyo-network/module-events'
|
|
16
16
|
import { PayloadBuilder } from '@xyo-network/payload-builder'
|
|
17
17
|
import {
|
|
@@ -35,7 +35,7 @@ export type GenericPayloadDivinerConfig = PayloadDivinerConfig<
|
|
|
35
35
|
|
|
36
36
|
export class GenericPayloadDiviner<
|
|
37
37
|
TParams extends PayloadDivinerParams<GenericPayloadDivinerConfig> = PayloadDivinerParams<GenericPayloadDivinerConfig>,
|
|
38
|
-
TIn extends PayloadDivinerQueryPayload
|
|
38
|
+
TIn extends PayloadDivinerQueryPayload = PayloadDivinerQueryPayload,
|
|
39
39
|
TOut extends WithStorageMeta<Payload> = WithStorageMeta<Payload>,
|
|
40
40
|
TEventData extends DivinerModuleEventData<DivinerInstance<TParams, TIn, TOut>, TIn, TOut> = DivinerModuleEventData<
|
|
41
41
|
DivinerInstance<TParams, TIn, TOut>,
|
|
@@ -97,11 +97,9 @@ export class GenericPayloadDiviner<
|
|
|
97
97
|
}
|
|
98
98
|
|
|
99
99
|
protected override async divineHandler(payloads?: TIn[]): Promise<TOut[]> {
|
|
100
|
-
const filters = payloads
|
|
100
|
+
const filters = filterAs(payloads ?? [], asPayloadDivinerQueryPayload)
|
|
101
101
|
assertEx(filters.length < 2, () => 'Multiple PayloadDivinerQuery payloads may not be specified')
|
|
102
|
-
const filter = assertEx(filters.shift(), () => 'No PayloadDivinerQuery specified') as
|
|
103
|
-
PayloadDivinerQueryPayload<EmptyObject, Hash>
|
|
104
|
-
|
|
102
|
+
const filter = assertEx(filters.shift(), () => 'No PayloadDivinerQuery specified') as PayloadDivinerQueryPayload
|
|
105
103
|
await this.updateIndex()
|
|
106
104
|
|
|
107
105
|
const {
|