@xyo-network/diviner-boundwitness-memory 3.1.13 → 3.1.14
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/MemoryBoundWitnessDiviner.d.ts.map +1 -1
- package/dist/neutral/applyBoundWitnessDivinerQueryPayload.d.ts +6 -0
- package/dist/neutral/applyBoundWitnessDivinerQueryPayload.d.ts.map +1 -0
- package/dist/neutral/index.d.ts +1 -0
- package/dist/neutral/index.d.ts.map +1 -1
- package/dist/neutral/index.mjs +36 -27
- package/dist/neutral/index.mjs.map +1 -1
- package/package.json +10 -10
- package/src/MemoryBoundWitnessDiviner.ts +3 -39
- package/src/applyBoundWitnessDivinerQueryPayload.ts +50 -0
- package/src/index.ts +1 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MemoryBoundWitnessDiviner.d.ts","sourceRoot":"","sources":["../../src/MemoryBoundWitnessDiviner.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"MemoryBoundWitnessDiviner.d.ts","sourceRoot":"","sources":["../../src/MemoryBoundWitnessDiviner.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iCAAiC,CAAA;AAEnE,OAAO,EAAE,mBAAmB,EAAE,MAAM,4CAA4C,CAAA;AAChF,OAAO,KAAK,EACV,yBAAyB,EACzB,+BAA+B,EAChC,MAAM,yCAAyC,CAAA;AAEhD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,4BAA4B,CAAA;AAI1D,MAAM,WAAW,2BAA2B;IAO1C,IAAI,EAAE,MAAM,CAAA;IAQZ,GAAG,EAAE,MAAM,CAAA;IAQX,IAAI,EAAE,MAAM,CAAA;IAQZ,GAAG,EAAE,MAAM,CAAA;IAQX,GAAG,EAAE,MAAM,CAAA;IAQX,IAAI,EAAE,MAAM,CAAA;CACb;AAED,qBAAa,yBAAyB,CACpC,OAAO,SAAS,yBAAyB,GAAG,yBAAyB,EACrE,GAAG,SAAS,+BAA+B,GAAG,+BAA+B,EAC7E,IAAI,SAAS,YAAY,GAAG,YAAY,CACxC,SAAQ,mBAAmB,CAAC,OAAO,EAAE,GAAG,EAAE,IAAI,CAAC;cACtB,aAAa,CAAC,QAAQ,CAAC,EAAE,GAAG,EAAE;CAOxD"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { BoundWitnessDivinerQueryPayload } from '@xyo-network/diviner-boundwitness-model';
|
|
2
|
+
import type { Payload, WithMeta } from '@xyo-network/payload-model';
|
|
3
|
+
export declare const applyBoundWitnessDivinerQueryPayload: (filter?: BoundWitnessDivinerQueryPayload, payloads?: Payload[]) => WithMeta<import("@xyo-network/payload-model").SchemaFields & import("@xyo-network/payload-model").PayloadFields & import("@xyo-network/boundwitness-model").BoundWitnessFields & {
|
|
4
|
+
schema: "network.xyo.boundwitness";
|
|
5
|
+
}>[];
|
|
6
|
+
//# sourceMappingURL=applyBoundWitnessDivinerQueryPayload.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"applyBoundWitnessDivinerQueryPayload.d.ts","sourceRoot":"","sources":["../../src/applyBoundWitnessDivinerQueryPayload.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,+BAA+B,EAAE,MAAM,yCAAyC,CAAA;AAC9F,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,4BAA4B,CAAA;AAMnE,eAAO,MAAM,oCAAoC,YAAa,+BAA+B,aAAY,OAAO,EAAE;;IAoCjH,CAAA"}
|
package/dist/neutral/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gCAAgC,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,2CAA2C,CAAA;AACzD,cAAc,gCAAgC,CAAA"}
|
package/dist/neutral/index.mjs
CHANGED
|
@@ -1,48 +1,57 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
3
|
|
|
4
|
-
// src/
|
|
4
|
+
// src/applyBoundWitnessDivinerQueryPayload.ts
|
|
5
5
|
import { containsAll } from "@xylabs/array";
|
|
6
6
|
import { assertEx } from "@xylabs/assert";
|
|
7
7
|
import { exists } from "@xylabs/exists";
|
|
8
8
|
import { hexFromHexString } from "@xylabs/hex";
|
|
9
9
|
import { isBoundWitness } from "@xyo-network/boundwitness-model";
|
|
10
|
+
var hasTimestamp = /* @__PURE__ */ __name((bw) => bw.timestamp !== void 0, "hasTimestamp");
|
|
11
|
+
var applyBoundWitnessDivinerQueryPayload = /* @__PURE__ */ __name((filter, payloads = []) => {
|
|
12
|
+
if (!filter) return [];
|
|
13
|
+
const { addresses, payload_hashes, payload_schemas, limit, offset, order = "desc", sourceQuery, destination, timestamp } = filter;
|
|
14
|
+
let bws = payloads.filter(isBoundWitness);
|
|
15
|
+
if (order === "desc") bws = bws.reverse();
|
|
16
|
+
const allAddresses = addresses?.map((address) => hexFromHexString(address)).filter(exists);
|
|
17
|
+
if (allAddresses?.length) bws = bws.filter((bw) => containsAll(bw.addresses, allAddresses));
|
|
18
|
+
if (payload_hashes?.length) bws = bws.filter((bw) => containsAll(bw.payload_hashes, payload_hashes));
|
|
19
|
+
if (payload_schemas?.length) bws = bws.filter((bw) => containsAll(bw.payload_schemas, payload_schemas));
|
|
20
|
+
if (sourceQuery) bws = bws.filter((bw) => bw?.$meta?.sourceQuery === sourceQuery);
|
|
21
|
+
if (destination && Array.isArray(destination) && destination?.length > 0) {
|
|
22
|
+
const targetFilter = assertEx(destination, () => "Missing destination");
|
|
23
|
+
bws = bws.filter((bw) => {
|
|
24
|
+
const targetDestinationField = bw?.$meta?.destination;
|
|
25
|
+
return targetDestinationField !== void 0 && Array.isArray(targetDestinationField) && targetDestinationField.length > 0 ? containsAll(targetFilter, targetDestinationField ?? []) : false;
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
if (timestamp !== void 0) {
|
|
29
|
+
bws = order === "desc" ? bws.filter(hasTimestamp).filter((bw) => bw.timestamp <= timestamp) : bws.filter(hasTimestamp).filter((bw) => bw.timestamp >= timestamp);
|
|
30
|
+
}
|
|
31
|
+
const parsedLimit = limit ?? bws.length;
|
|
32
|
+
const parsedOffset = offset ?? 0;
|
|
33
|
+
return bws.slice(parsedOffset, parsedLimit);
|
|
34
|
+
}, "applyBoundWitnessDivinerQueryPayload");
|
|
35
|
+
|
|
36
|
+
// src/MemoryBoundWitnessDiviner.ts
|
|
37
|
+
import { assertEx as assertEx2 } from "@xylabs/assert";
|
|
38
|
+
import { isBoundWitness as isBoundWitness2 } from "@xyo-network/boundwitness-model";
|
|
10
39
|
import { BoundWitnessDiviner } from "@xyo-network/diviner-boundwitness-abstract";
|
|
11
40
|
import { isBoundWitnessDivinerQueryPayload } from "@xyo-network/diviner-boundwitness-model";
|
|
12
|
-
var hasTimestamp = /* @__PURE__ */ __name((bw) => bw.timestamp !== void 0, "hasTimestamp");
|
|
13
41
|
var MemoryBoundWitnessDiviner = class extends BoundWitnessDiviner {
|
|
14
42
|
static {
|
|
15
43
|
__name(this, "MemoryBoundWitnessDiviner");
|
|
16
44
|
}
|
|
17
|
-
// eslint-disable-next-line complexity
|
|
18
45
|
async divineHandler(payloads) {
|
|
19
|
-
const filter =
|
|
46
|
+
const filter = assertEx2(payloads?.filter(isBoundWitnessDivinerQueryPayload)?.pop(), () => "Missing query payload");
|
|
20
47
|
if (!filter) return [];
|
|
21
|
-
const archivist =
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
if (order === "desc") bws = bws.reverse();
|
|
25
|
-
const allAddresses = addresses?.map((address) => hexFromHexString(address)).filter(exists);
|
|
26
|
-
if (allAddresses?.length) bws = bws.filter((bw) => containsAll(bw.addresses, allAddresses));
|
|
27
|
-
if (payload_hashes?.length) bws = bws.filter((bw) => containsAll(bw.payload_hashes, payload_hashes));
|
|
28
|
-
if (payload_schemas?.length) bws = bws.filter((bw) => containsAll(bw.payload_schemas, payload_schemas));
|
|
29
|
-
if (sourceQuery) bws = bws.filter((bw) => bw?.$meta?.sourceQuery === sourceQuery);
|
|
30
|
-
if (destination && Array.isArray(destination) && destination?.length > 0) {
|
|
31
|
-
const targetFilter = assertEx(destination, () => "Missing destination");
|
|
32
|
-
bws = bws.filter((bw) => {
|
|
33
|
-
const targetDestinationField = bw?.$meta?.destination;
|
|
34
|
-
return targetDestinationField !== void 0 && Array.isArray(targetDestinationField) && targetDestinationField.length > 0 ? containsAll(targetFilter, targetDestinationField ?? []) : false;
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
if (timestamp !== void 0) {
|
|
38
|
-
bws = order === "desc" ? bws.filter(hasTimestamp).filter((bw) => bw.timestamp <= timestamp) : bws.filter(hasTimestamp).filter((bw) => bw.timestamp >= timestamp);
|
|
39
|
-
}
|
|
40
|
-
const parsedLimit = limit ?? bws.length;
|
|
41
|
-
const parsedOffset = offset ?? 0;
|
|
42
|
-
return bws.slice(parsedOffset, parsedLimit);
|
|
48
|
+
const archivist = assertEx2(await this.archivistInstance(), () => "Unable to resolve archivist");
|
|
49
|
+
let bws = (await archivist?.all?.() ?? []).filter(isBoundWitness2);
|
|
50
|
+
return applyBoundWitnessDivinerQueryPayload(filter, bws);
|
|
43
51
|
}
|
|
44
52
|
};
|
|
45
53
|
export {
|
|
46
|
-
MemoryBoundWitnessDiviner
|
|
54
|
+
MemoryBoundWitnessDiviner,
|
|
55
|
+
applyBoundWitnessDivinerQueryPayload
|
|
47
56
|
};
|
|
48
57
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/MemoryBoundWitnessDiviner.ts"],"sourcesContent":["import { containsAll } from '@xylabs/array'\nimport { assertEx } from '@xylabs/assert'\nimport { exists } from '@xylabs/exists'\nimport { hexFromHexString } from '@xylabs/hex'\nimport type { BoundWitness } from '@xyo-network/boundwitness-model'\nimport { isBoundWitness } from '@xyo-network/boundwitness-model'\nimport { BoundWitnessDiviner } from '@xyo-network/diviner-boundwitness-abstract'\nimport type {\n BoundWitnessDivinerParams,\n BoundWitnessDivinerQueryPayload,\n} from '@xyo-network/diviner-boundwitness-model'\nimport { isBoundWitnessDivinerQueryPayload } from '@xyo-network/diviner-boundwitness-model'\nimport type { WithMeta } from '@xyo-network/payload-model'\n\nexport interface EqualityComparisonOperators {\n /**\n * 'Not Equal To' comparison operator.\n * Compares the field with the specified string value,\n * selecting records where the field value does not match the provided string.\n * Example: field != 'value'\n */\n '!=': string\n\n /**\n * 'Less Than' comparison operator.\n * Compares the field with the specified string value,\n * selecting records where the field value is lexicographically less than the provided string.\n * Example: field < 'value'\n */\n '<': string\n\n /**\n * 'Less Than or Equal To' comparison operator.\n * Compares the field with the specified string value,\n * selecting records where the field value is lexicographically less than or equal to the provided string.\n * Example: field <= 'value'\n */\n '<=': string\n\n /**\n * 'Equal To' comparison operator.\n * Compares the field with the specified string value,\n * selecting records where the field value matches the provided string exactly.\n * Example: field = 'value'\n */\n '=': string\n\n /**\n * 'Greater Than' comparison operator.\n * Compares the field with the specified string value,\n * selecting records where the field value is lexicographically greater than the provided string.\n * Example: field > 'value'\n */\n '>': string\n\n /**\n * 'Greater Than or Equal To' comparison operator.\n * Compares the field with the specified string value,\n * selecting records where the field value is lexicographically greater than or equal to the provided string.\n * Example: field >= 'value'\n */\n '>=': string\n}\n\
|
|
1
|
+
{"version":3,"sources":["../../src/applyBoundWitnessDivinerQueryPayload.ts","../../src/MemoryBoundWitnessDiviner.ts"],"sourcesContent":["import { containsAll } from '@xylabs/array'\nimport { assertEx } from '@xylabs/assert'\nimport { exists } from '@xylabs/exists'\nimport { hexFromHexString } from '@xylabs/hex'\nimport type { BoundWitness } from '@xyo-network/boundwitness-model'\nimport { isBoundWitness } from '@xyo-network/boundwitness-model'\nimport type { BoundWitnessDivinerQueryPayload } from '@xyo-network/diviner-boundwitness-model'\nimport type { Payload, WithMeta } from '@xyo-network/payload-model'\n\ntype WithTimestamp = BoundWitness & { timestamp: number }\nconst hasTimestamp = (bw: BoundWitness): bw is WithTimestamp => bw.timestamp !== undefined\n\n// eslint-disable-next-line complexity\nexport const applyBoundWitnessDivinerQueryPayload = (filter?: BoundWitnessDivinerQueryPayload, payloads: Payload[] = []) => {\n if (!filter) return []\n const {\n addresses, payload_hashes, payload_schemas, limit, offset, order = 'desc', sourceQuery, destination, timestamp,\n } = filter\n\n let bws = payloads.filter(isBoundWitness) as WithMeta<BoundWitness>[]\n if (order === 'desc') bws = bws.reverse()\n const allAddresses = addresses?.map(address => hexFromHexString(address)).filter(exists)\n if (allAddresses?.length) bws = bws.filter(bw => containsAll(bw.addresses, allAddresses))\n if (payload_hashes?.length) bws = bws.filter(bw => containsAll(bw.payload_hashes, payload_hashes))\n if (payload_schemas?.length) bws = bws.filter(bw => containsAll(bw.payload_schemas, payload_schemas))\n if (sourceQuery) bws = bws.filter(bw => (bw?.$meta as { sourceQuery?: string })?.sourceQuery === sourceQuery)\n // If there's a destination filter of the right kind\n if (destination && Array.isArray(destination) && destination?.length > 0) {\n const targetFilter = assertEx(destination, () => 'Missing destination')\n // Find all BWs that satisfy the destination constraint\n bws = bws.filter((bw) => {\n const targetDestinationField = (bw?.$meta as { destination?: string[] })?.destination\n // If the destination field is an array and contains at least one element\n return targetDestinationField !== undefined && Array.isArray(targetDestinationField) && targetDestinationField.length > 0\n // Check that the targetDestinationField contains all the elements in the targetFilter\n ? containsAll(targetFilter, targetDestinationField ?? [])\n // Otherwise, filter it out\n : false\n })\n }\n if (timestamp !== undefined) {\n bws\n = order === 'desc'\n ? bws.filter(hasTimestamp).filter(bw => bw.timestamp <= timestamp)\n : bws.filter(hasTimestamp).filter(bw => bw.timestamp >= timestamp)\n }\n const parsedLimit = limit ?? bws.length\n const parsedOffset = offset ?? 0\n return bws.slice(parsedOffset, parsedLimit)\n}\n","import { assertEx } from '@xylabs/assert'\nimport type { BoundWitness } from '@xyo-network/boundwitness-model'\nimport { isBoundWitness } from '@xyo-network/boundwitness-model'\nimport { BoundWitnessDiviner } from '@xyo-network/diviner-boundwitness-abstract'\nimport type {\n BoundWitnessDivinerParams,\n BoundWitnessDivinerQueryPayload,\n} from '@xyo-network/diviner-boundwitness-model'\nimport { isBoundWitnessDivinerQueryPayload } from '@xyo-network/diviner-boundwitness-model'\nimport type { WithMeta } from '@xyo-network/payload-model'\n\nimport { applyBoundWitnessDivinerQueryPayload } from './applyBoundWitnessDivinerQueryPayload.ts'\n\nexport interface EqualityComparisonOperators {\n /**\n * 'Not Equal To' comparison operator.\n * Compares the field with the specified string value,\n * selecting records where the field value does not match the provided string.\n * Example: field != 'value'\n */\n '!=': string\n\n /**\n * 'Less Than' comparison operator.\n * Compares the field with the specified string value,\n * selecting records where the field value is lexicographically less than the provided string.\n * Example: field < 'value'\n */\n '<': string\n\n /**\n * 'Less Than or Equal To' comparison operator.\n * Compares the field with the specified string value,\n * selecting records where the field value is lexicographically less than or equal to the provided string.\n * Example: field <= 'value'\n */\n '<=': string\n\n /**\n * 'Equal To' comparison operator.\n * Compares the field with the specified string value,\n * selecting records where the field value matches the provided string exactly.\n * Example: field = 'value'\n */\n '=': string\n\n /**\n * 'Greater Than' comparison operator.\n * Compares the field with the specified string value,\n * selecting records where the field value is lexicographically greater than the provided string.\n * Example: field > 'value'\n */\n '>': string\n\n /**\n * 'Greater Than or Equal To' comparison operator.\n * Compares the field with the specified string value,\n * selecting records where the field value is lexicographically greater than or equal to the provided string.\n * Example: field >= 'value'\n */\n '>=': string\n}\n\nexport class MemoryBoundWitnessDiviner<\n TParams extends BoundWitnessDivinerParams = BoundWitnessDivinerParams,\n TIn extends BoundWitnessDivinerQueryPayload = BoundWitnessDivinerQueryPayload,\n TOut extends BoundWitness = BoundWitness,\n> extends BoundWitnessDiviner<TParams, TIn, TOut> {\n protected override async divineHandler(payloads?: TIn[]) {\n const filter = assertEx(payloads?.filter(isBoundWitnessDivinerQueryPayload)?.pop(), () => 'Missing query payload')\n if (!filter) return []\n const archivist = assertEx(await this.archivistInstance(), () => 'Unable to resolve archivist')\n let bws = ((await archivist?.all?.()) ?? []).filter(isBoundWitness) as WithMeta<BoundWitness>[]\n return applyBoundWitnessDivinerQueryPayload(filter, bws) as WithMeta<TOut>[]\n }\n}\n"],"mappings":";;;;AAAA,SAASA,mBAAmB;AAC5B,SAASC,gBAAgB;AACzB,SAASC,cAAc;AACvB,SAASC,wBAAwB;AAEjC,SAASC,sBAAsB;AAK/B,IAAMC,eAAe,wBAACC,OAA0CA,GAAGC,cAAcC,QAA5D;AAGd,IAAMC,uCAAuC,wBAACC,QAA0CC,WAAsB,CAAA,MAAE;AACrH,MAAI,CAACD,OAAQ,QAAO,CAAA;AACpB,QAAM,EACJE,WAAWC,gBAAgBC,iBAAiBC,OAAOC,QAAQC,QAAQ,QAAQC,aAAaC,aAAaZ,UAAS,IAC5GG;AAEJ,MAAIU,MAAMT,SAASD,OAAOW,cAAAA;AAC1B,MAAIJ,UAAU,OAAQG,OAAMA,IAAIE,QAAO;AACvC,QAAMC,eAAeX,WAAWY,IAAIC,CAAAA,YAAWC,iBAAiBD,OAAAA,CAAAA,EAAUf,OAAOiB,MAAAA;AACjF,MAAIJ,cAAcK,OAAQR,OAAMA,IAAIV,OAAOJ,CAAAA,OAAMuB,YAAYvB,GAAGM,WAAWW,YAAAA,CAAAA;AAC3E,MAAIV,gBAAgBe,OAAQR,OAAMA,IAAIV,OAAOJ,CAAAA,OAAMuB,YAAYvB,GAAGO,gBAAgBA,cAAAA,CAAAA;AAClF,MAAIC,iBAAiBc,OAAQR,OAAMA,IAAIV,OAAOJ,CAAAA,OAAMuB,YAAYvB,GAAGQ,iBAAiBA,eAAAA,CAAAA;AACpF,MAAII,YAAaE,OAAMA,IAAIV,OAAOJ,CAAAA,OAAOA,IAAIwB,OAAoCZ,gBAAgBA,WAAAA;AAEjG,MAAIC,eAAeY,MAAMC,QAAQb,WAAAA,KAAgBA,aAAaS,SAAS,GAAG;AACxE,UAAMK,eAAeC,SAASf,aAAa,MAAM,qBAAA;AAEjDC,UAAMA,IAAIV,OAAO,CAACJ,OAAAA;AAChB,YAAM6B,yBAA0B7B,IAAIwB,OAAsCX;AAE1E,aAAOgB,2BAA2B3B,UAAauB,MAAMC,QAAQG,sBAAAA,KAA2BA,uBAAuBP,SAAS,IAEpHC,YAAYI,cAAcE,0BAA0B,CAAA,CAAE,IAEtD;IACN,CAAA;EACF;AACA,MAAI5B,cAAcC,QAAW;AAC3BY,UACIH,UAAU,SACRG,IAAIV,OAAOL,YAAAA,EAAcK,OAAOJ,CAAAA,OAAMA,GAAGC,aAAaA,SAAAA,IACtDa,IAAIV,OAAOL,YAAAA,EAAcK,OAAOJ,CAAAA,OAAMA,GAAGC,aAAaA,SAAAA;EAC9D;AACA,QAAM6B,cAAcrB,SAASK,IAAIQ;AACjC,QAAMS,eAAerB,UAAU;AAC/B,SAAOI,IAAIkB,MAAMD,cAAcD,WAAAA;AACjC,GApCoD;;;ACbpD,SAASG,YAAAA,iBAAgB;AAEzB,SAASC,kBAAAA,uBAAsB;AAC/B,SAASC,2BAA2B;AAKpC,SAASC,yCAAyC;AAuD3C,IAAMC,4BAAN,cAIGC,oBAAAA;EAnEV,OAmEUA;;;EACR,MAAyBC,cAAcC,UAAkB;AACvD,UAAMC,SAASC,UAASF,UAAUC,OAAOE,iCAAAA,GAAoCC,IAAAA,GAAO,MAAM,uBAAA;AAC1F,QAAI,CAACH,OAAQ,QAAO,CAAA;AACpB,UAAMI,YAAYH,UAAS,MAAM,KAAKI,kBAAiB,GAAI,MAAM,6BAAA;AACjE,QAAIC,OAAQ,MAAMF,WAAWG,MAAAA,KAAY,CAAA,GAAIP,OAAOQ,eAAAA;AACpD,WAAOC,qCAAqCT,QAAQM,GAAAA;EACtD;AACF;","names":["containsAll","assertEx","exists","hexFromHexString","isBoundWitness","hasTimestamp","bw","timestamp","undefined","applyBoundWitnessDivinerQueryPayload","filter","payloads","addresses","payload_hashes","payload_schemas","limit","offset","order","sourceQuery","destination","bws","isBoundWitness","reverse","allAddresses","map","address","hexFromHexString","exists","length","containsAll","$meta","Array","isArray","targetFilter","assertEx","targetDestinationField","parsedLimit","parsedOffset","slice","assertEx","isBoundWitness","BoundWitnessDiviner","isBoundWitnessDivinerQueryPayload","MemoryBoundWitnessDiviner","BoundWitnessDiviner","divineHandler","payloads","filter","assertEx","isBoundWitnessDivinerQueryPayload","pop","archivist","archivistInstance","bws","all","isBoundWitness","applyBoundWitnessDivinerQueryPayload"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xyo-network/diviner-boundwitness-memory",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.14",
|
|
4
4
|
"description": "Primary SDK for using XYO Protocol 2.0",
|
|
5
5
|
"homepage": "https://xyo.network",
|
|
6
6
|
"bugs": {
|
|
@@ -33,19 +33,19 @@
|
|
|
33
33
|
"@xylabs/assert": "^4.0.9",
|
|
34
34
|
"@xylabs/exists": "^4.0.9",
|
|
35
35
|
"@xylabs/hex": "^4.0.9",
|
|
36
|
-
"@xyo-network/boundwitness-model": "^3.1.
|
|
37
|
-
"@xyo-network/diviner-boundwitness-abstract": "^3.1.
|
|
38
|
-
"@xyo-network/diviner-boundwitness-model": "^3.1.
|
|
39
|
-
"@xyo-network/payload-model": "^3.1.
|
|
36
|
+
"@xyo-network/boundwitness-model": "^3.1.14",
|
|
37
|
+
"@xyo-network/diviner-boundwitness-abstract": "^3.1.14",
|
|
38
|
+
"@xyo-network/diviner-boundwitness-model": "^3.1.14",
|
|
39
|
+
"@xyo-network/payload-model": "^3.1.14"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@xylabs/ts-scripts-yarn3": "^4.0.7",
|
|
43
43
|
"@xylabs/tsconfig": "^4.0.7",
|
|
44
|
-
"@xyo-network/account": "^3.1.
|
|
45
|
-
"@xyo-network/archivist-memory": "^3.1.
|
|
46
|
-
"@xyo-network/boundwitness-builder": "^3.1.
|
|
47
|
-
"@xyo-network/node-memory": "^3.1.
|
|
48
|
-
"@xyo-network/payload-builder": "^3.1.
|
|
44
|
+
"@xyo-network/account": "^3.1.14",
|
|
45
|
+
"@xyo-network/archivist-memory": "^3.1.14",
|
|
46
|
+
"@xyo-network/boundwitness-builder": "^3.1.14",
|
|
47
|
+
"@xyo-network/node-memory": "^3.1.14",
|
|
48
|
+
"@xyo-network/payload-builder": "^3.1.14",
|
|
49
49
|
"typescript": "^5.5.4"
|
|
50
50
|
},
|
|
51
51
|
"publishConfig": {
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
import { containsAll } from '@xylabs/array'
|
|
2
1
|
import { assertEx } from '@xylabs/assert'
|
|
3
|
-
import { exists } from '@xylabs/exists'
|
|
4
|
-
import { hexFromHexString } from '@xylabs/hex'
|
|
5
2
|
import type { BoundWitness } from '@xyo-network/boundwitness-model'
|
|
6
3
|
import { isBoundWitness } from '@xyo-network/boundwitness-model'
|
|
7
4
|
import { BoundWitnessDiviner } from '@xyo-network/diviner-boundwitness-abstract'
|
|
@@ -12,6 +9,8 @@ import type {
|
|
|
12
9
|
import { isBoundWitnessDivinerQueryPayload } from '@xyo-network/diviner-boundwitness-model'
|
|
13
10
|
import type { WithMeta } from '@xyo-network/payload-model'
|
|
14
11
|
|
|
12
|
+
import { applyBoundWitnessDivinerQueryPayload } from './applyBoundWitnessDivinerQueryPayload.ts'
|
|
13
|
+
|
|
15
14
|
export interface EqualityComparisonOperators {
|
|
16
15
|
/**
|
|
17
16
|
* 'Not Equal To' comparison operator.
|
|
@@ -62,51 +61,16 @@ export interface EqualityComparisonOperators {
|
|
|
62
61
|
'>=': string
|
|
63
62
|
}
|
|
64
63
|
|
|
65
|
-
type WithTimestamp = BoundWitness & { timestamp: number }
|
|
66
|
-
const hasTimestamp = (bw: BoundWitness): bw is WithTimestamp => bw.timestamp !== undefined
|
|
67
|
-
|
|
68
64
|
export class MemoryBoundWitnessDiviner<
|
|
69
65
|
TParams extends BoundWitnessDivinerParams = BoundWitnessDivinerParams,
|
|
70
66
|
TIn extends BoundWitnessDivinerQueryPayload = BoundWitnessDivinerQueryPayload,
|
|
71
67
|
TOut extends BoundWitness = BoundWitness,
|
|
72
68
|
> extends BoundWitnessDiviner<TParams, TIn, TOut> {
|
|
73
|
-
// eslint-disable-next-line complexity
|
|
74
69
|
protected override async divineHandler(payloads?: TIn[]) {
|
|
75
70
|
const filter = assertEx(payloads?.filter(isBoundWitnessDivinerQueryPayload)?.pop(), () => 'Missing query payload')
|
|
76
71
|
if (!filter) return []
|
|
77
72
|
const archivist = assertEx(await this.archivistInstance(), () => 'Unable to resolve archivist')
|
|
78
|
-
const {
|
|
79
|
-
addresses, payload_hashes, payload_schemas, limit, offset, order = 'desc', sourceQuery, destination, timestamp,
|
|
80
|
-
} = filter
|
|
81
73
|
let bws = ((await archivist?.all?.()) ?? []).filter(isBoundWitness) as WithMeta<BoundWitness>[]
|
|
82
|
-
|
|
83
|
-
const allAddresses = addresses?.map(address => hexFromHexString(address)).filter(exists)
|
|
84
|
-
if (allAddresses?.length) bws = bws.filter(bw => containsAll(bw.addresses, allAddresses))
|
|
85
|
-
if (payload_hashes?.length) bws = bws.filter(bw => containsAll(bw.payload_hashes, payload_hashes))
|
|
86
|
-
if (payload_schemas?.length) bws = bws.filter(bw => containsAll(bw.payload_schemas, payload_schemas))
|
|
87
|
-
if (sourceQuery) bws = bws.filter(bw => (bw?.$meta as { sourceQuery?: string })?.sourceQuery === sourceQuery)
|
|
88
|
-
// If there's a destination filter of the right kind
|
|
89
|
-
if (destination && Array.isArray(destination) && destination?.length > 0) {
|
|
90
|
-
const targetFilter = assertEx(destination, () => 'Missing destination')
|
|
91
|
-
// Find all BWs that satisfy the destination constraint
|
|
92
|
-
bws = bws.filter((bw) => {
|
|
93
|
-
const targetDestinationField = (bw?.$meta as { destination?: string[] })?.destination
|
|
94
|
-
// If the destination field is an array and contains at least one element
|
|
95
|
-
return targetDestinationField !== undefined && Array.isArray(targetDestinationField) && targetDestinationField.length > 0
|
|
96
|
-
// Check that the targetDestinationField contains all the elements in the targetFilter
|
|
97
|
-
? containsAll(targetFilter, targetDestinationField ?? [])
|
|
98
|
-
// Otherwise, filter it out
|
|
99
|
-
: false
|
|
100
|
-
})
|
|
101
|
-
}
|
|
102
|
-
if (timestamp !== undefined) {
|
|
103
|
-
bws
|
|
104
|
-
= order === 'desc'
|
|
105
|
-
? bws.filter(hasTimestamp).filter(bw => bw.timestamp <= timestamp)
|
|
106
|
-
: bws.filter(hasTimestamp).filter(bw => bw.timestamp >= timestamp)
|
|
107
|
-
}
|
|
108
|
-
const parsedLimit = limit ?? bws.length
|
|
109
|
-
const parsedOffset = offset ?? 0
|
|
110
|
-
return bws.slice(parsedOffset, parsedLimit) as WithMeta<TOut>[]
|
|
74
|
+
return applyBoundWitnessDivinerQueryPayload(filter, bws) as WithMeta<TOut>[]
|
|
111
75
|
}
|
|
112
76
|
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { containsAll } from '@xylabs/array'
|
|
2
|
+
import { assertEx } from '@xylabs/assert'
|
|
3
|
+
import { exists } from '@xylabs/exists'
|
|
4
|
+
import { hexFromHexString } from '@xylabs/hex'
|
|
5
|
+
import type { BoundWitness } from '@xyo-network/boundwitness-model'
|
|
6
|
+
import { isBoundWitness } from '@xyo-network/boundwitness-model'
|
|
7
|
+
import type { BoundWitnessDivinerQueryPayload } from '@xyo-network/diviner-boundwitness-model'
|
|
8
|
+
import type { Payload, WithMeta } from '@xyo-network/payload-model'
|
|
9
|
+
|
|
10
|
+
type WithTimestamp = BoundWitness & { timestamp: number }
|
|
11
|
+
const hasTimestamp = (bw: BoundWitness): bw is WithTimestamp => bw.timestamp !== undefined
|
|
12
|
+
|
|
13
|
+
// eslint-disable-next-line complexity
|
|
14
|
+
export const applyBoundWitnessDivinerQueryPayload = (filter?: BoundWitnessDivinerQueryPayload, payloads: Payload[] = []) => {
|
|
15
|
+
if (!filter) return []
|
|
16
|
+
const {
|
|
17
|
+
addresses, payload_hashes, payload_schemas, limit, offset, order = 'desc', sourceQuery, destination, timestamp,
|
|
18
|
+
} = filter
|
|
19
|
+
|
|
20
|
+
let bws = payloads.filter(isBoundWitness) as WithMeta<BoundWitness>[]
|
|
21
|
+
if (order === 'desc') bws = bws.reverse()
|
|
22
|
+
const allAddresses = addresses?.map(address => hexFromHexString(address)).filter(exists)
|
|
23
|
+
if (allAddresses?.length) bws = bws.filter(bw => containsAll(bw.addresses, allAddresses))
|
|
24
|
+
if (payload_hashes?.length) bws = bws.filter(bw => containsAll(bw.payload_hashes, payload_hashes))
|
|
25
|
+
if (payload_schemas?.length) bws = bws.filter(bw => containsAll(bw.payload_schemas, payload_schemas))
|
|
26
|
+
if (sourceQuery) bws = bws.filter(bw => (bw?.$meta as { sourceQuery?: string })?.sourceQuery === sourceQuery)
|
|
27
|
+
// If there's a destination filter of the right kind
|
|
28
|
+
if (destination && Array.isArray(destination) && destination?.length > 0) {
|
|
29
|
+
const targetFilter = assertEx(destination, () => 'Missing destination')
|
|
30
|
+
// Find all BWs that satisfy the destination constraint
|
|
31
|
+
bws = bws.filter((bw) => {
|
|
32
|
+
const targetDestinationField = (bw?.$meta as { destination?: string[] })?.destination
|
|
33
|
+
// If the destination field is an array and contains at least one element
|
|
34
|
+
return targetDestinationField !== undefined && Array.isArray(targetDestinationField) && targetDestinationField.length > 0
|
|
35
|
+
// Check that the targetDestinationField contains all the elements in the targetFilter
|
|
36
|
+
? containsAll(targetFilter, targetDestinationField ?? [])
|
|
37
|
+
// Otherwise, filter it out
|
|
38
|
+
: false
|
|
39
|
+
})
|
|
40
|
+
}
|
|
41
|
+
if (timestamp !== undefined) {
|
|
42
|
+
bws
|
|
43
|
+
= order === 'desc'
|
|
44
|
+
? bws.filter(hasTimestamp).filter(bw => bw.timestamp <= timestamp)
|
|
45
|
+
: bws.filter(hasTimestamp).filter(bw => bw.timestamp >= timestamp)
|
|
46
|
+
}
|
|
47
|
+
const parsedLimit = limit ?? bws.length
|
|
48
|
+
const parsedOffset = offset ?? 0
|
|
49
|
+
return bws.slice(parsedOffset, parsedLimit)
|
|
50
|
+
}
|
package/src/index.ts
CHANGED