@xyo-network/diviner-boundwitness-indexeddb 2.107.3 → 2.107.4

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.
@@ -1,2 +1,159 @@
1
- var y=Object.defineProperty;var W=Object.getPrototypeOf;var j=Reflect.get;var z=(r,e,t)=>e in r?y(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t;var l=(r,e)=>y(r,"name",{value:e,configurable:!0});var h=(r,e,t)=>z(r,typeof e!="symbol"?e+"":e,t);var g=(r,e,t)=>j(W(r),t,e);import{BoundWitnessDivinerSchema as A}from"@xyo-network/diviner-boundwitness-model";var x=`${A}.indexeddb`;var u=`${x}.config`;import{containsAll as F}from"@xylabs/array";import{exists as G}from"@xylabs/exists";import{IndexedDbArchivist as c}from"@xyo-network/archivist-indexeddb";import{BoundWitnessSchema as U,isBoundWitness as $}from"@xyo-network/boundwitness-model";import{BoundWitnessDiviner as k}from"@xyo-network/diviner-boundwitness-abstract";import{isBoundWitnessDivinerQueryPayload as q}from"@xyo-network/diviner-boundwitness-model";import{PayloadBuilder as K}from"@xyo-network/payload-builder";import{openDB as L}from"idb";var p=l((r,e)=>{if(!(!e||(e==null?void 0:e.length)===0))return t=>t?F(t[r],e):!1},"bwValueFilter"),o=class o extends k{get dbName(){var e,t;return((e=this.config)==null?void 0:e.dbName)??((t=this.config)==null?void 0:t.archivist)??c.defaultDbName}get dbVersion(){var e;return((e=this.config)==null?void 0:e.dbVersion)??c.defaultDbVersion}get storeName(){var e;return((e=this.config)==null?void 0:e.storeName)??c.defaultStoreName}async divineHandler(e){var m;let t=(m=e==null?void 0:e.filter(q))==null?void 0:m.pop();return t?await this.tryUseDb(async N=>{let{addresses:S,payload_hashes:D,payload_schemas:B,limit:V,offset:v,order:C}=t,b=N.transaction(this.storeName,"readonly"),E=b.objectStore(this.storeName),a=[],d=v??0,H=V??10,I=C==="desc"?"prev":"next",f=[p("addresses",S),p("payload_hashes",D),p("payload_schemas",B)].filter(G),i=await E.index(c.schemaIndexName).openCursor(IDBKeyRange.only(U),I);if(f.length===0)for(;i&&d>0;)i=await i.advance(d),d=0;for(;i&&a.length<H;){let n=i.value;n&&(f.length>0?f.every(P=>P(n))&&a.push(n):a.push(n));try{i=await i.continue()}catch{break}}return await b.done,await Promise.all(a.filter($).map(n=>K.build(n)))})??[]:[]}async startHandler(){return await super.startHandler(),!0}async tryGetInitializedDb(){if((await indexedDB.databases()).some(s=>s.name===this.dbName&&s.version===this.dbVersion)){let s=await L(this.dbName,this.dbVersion);if(s.objectStoreNames.contains(this.storeName))return s;s.close()}}async tryUseDb(e){let t=await this.tryGetInitializedDb();if(t)try{return await e(t)}finally{t.close()}}};l(o,"IndexedDbBoundWitnessDiviner"),h(o,"configSchemas",[...g(o,o,"configSchemas"),u]),h(o,"defaultConfigSchema",u);var w=o;export{w as IndexedDbBoundWitnessDiviner,u as IndexedDbBoundWitnessDivinerConfigSchema,x as IndexedDbBoundWitnessDivinerSchema};
1
+ var __defProp = Object.defineProperty;
2
+ var __getProtoOf = Object.getPrototypeOf;
3
+ var __reflectGet = Reflect.get;
4
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
5
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
7
+ var __superGet = (cls, obj, key) => __reflectGet(__getProtoOf(cls), key, obj);
8
+
9
+ // src/Schema.ts
10
+ import { BoundWitnessDivinerSchema } from "@xyo-network/diviner-boundwitness-model";
11
+ var IndexedDbBoundWitnessDivinerSchema = `${BoundWitnessDivinerSchema}.indexeddb`;
12
+
13
+ // src/Config.ts
14
+ var IndexedDbBoundWitnessDivinerConfigSchema = `${IndexedDbBoundWitnessDivinerSchema}.config`;
15
+
16
+ // src/Diviner.ts
17
+ import { containsAll } from "@xylabs/array";
18
+ import { exists } from "@xylabs/exists";
19
+ import { IndexedDbArchivist } from "@xyo-network/archivist-indexeddb";
20
+ import { BoundWitnessSchema, isBoundWitness } from "@xyo-network/boundwitness-model";
21
+ import { BoundWitnessDiviner } from "@xyo-network/diviner-boundwitness-abstract";
22
+ import { isBoundWitnessDivinerQueryPayload } from "@xyo-network/diviner-boundwitness-model";
23
+ import { PayloadBuilder } from "@xyo-network/payload-builder";
24
+ import { openDB } from "idb";
25
+ var bwValueFilter = /* @__PURE__ */ __name((key, values) => {
26
+ if (!values || (values == null ? void 0 : values.length) === 0) return void 0;
27
+ return (bw) => {
28
+ if (!bw) return false;
29
+ return containsAll(bw[key], values);
30
+ };
31
+ }, "bwValueFilter");
32
+ var _IndexedDbBoundWitnessDiviner = class _IndexedDbBoundWitnessDiviner extends BoundWitnessDiviner {
33
+ /**
34
+ * The database name. If not supplied via config, it defaults
35
+ * to the archivist's name and if archivist's name is not supplied,
36
+ * it defaults to `archivist`. This behavior
37
+ * biases towards a single, isolated DB per archivist which seems to
38
+ * make the most sense for 99% of use cases.
39
+ */
40
+ get dbName() {
41
+ var _a, _b;
42
+ return ((_a = this.config) == null ? void 0 : _a.dbName) ?? ((_b = this.config) == null ? void 0 : _b.archivist) ?? IndexedDbArchivist.defaultDbName;
43
+ }
44
+ /**
45
+ * The database version. If not supplied via config, it defaults to 1.
46
+ */
47
+ get dbVersion() {
48
+ var _a;
49
+ return ((_a = this.config) == null ? void 0 : _a.dbVersion) ?? IndexedDbArchivist.defaultDbVersion;
50
+ }
51
+ /**
52
+ * The name of the object store. If not supplied via config, it defaults
53
+ * to `payloads`.
54
+ */
55
+ get storeName() {
56
+ var _a;
57
+ return ((_a = this.config) == null ? void 0 : _a.storeName) ?? IndexedDbArchivist.defaultStoreName;
58
+ }
59
+ async divineHandler(payloads) {
60
+ var _a;
61
+ const query = (_a = payloads == null ? void 0 : payloads.filter(isBoundWitnessDivinerQueryPayload)) == null ? void 0 : _a.pop();
62
+ if (!query) return [];
63
+ const result = await this.tryUseDb(async (db) => {
64
+ const { addresses, payload_hashes, payload_schemas, limit, offset, order } = query;
65
+ const tx = db.transaction(this.storeName, "readonly");
66
+ const store = tx.objectStore(this.storeName);
67
+ const results = [];
68
+ let parsedOffset = offset ?? 0;
69
+ const parsedLimit = limit ?? 10;
70
+ const direction = order === "desc" ? "prev" : "next";
71
+ const valueFilters = [
72
+ bwValueFilter("addresses", addresses),
73
+ bwValueFilter("payload_hashes", payload_hashes),
74
+ bwValueFilter("payload_schemas", payload_schemas)
75
+ ].filter(exists);
76
+ let cursor = await store.index(IndexedDbArchivist.schemaIndexName).openCursor(IDBKeyRange.only(BoundWitnessSchema), direction);
77
+ if (valueFilters.length === 0) {
78
+ while (cursor && parsedOffset > 0) {
79
+ cursor = await cursor.advance(parsedOffset);
80
+ parsedOffset = 0;
81
+ }
82
+ }
83
+ while (cursor && results.length < parsedLimit) {
84
+ const value = cursor.value;
85
+ if (value) {
86
+ if (valueFilters.length > 0) {
87
+ if (valueFilters.every((filter) => filter(value))) {
88
+ results.push(value);
89
+ }
90
+ } else {
91
+ results.push(value);
92
+ }
93
+ }
94
+ try {
95
+ cursor = await cursor.continue();
96
+ } catch {
97
+ break;
98
+ }
99
+ }
100
+ await tx.done;
101
+ return await Promise.all(results.filter(isBoundWitness).map((bw) => {
102
+ return PayloadBuilder.build(bw);
103
+ }));
104
+ });
105
+ return result ?? [];
106
+ }
107
+ async startHandler() {
108
+ await super.startHandler();
109
+ return true;
110
+ }
111
+ /**
112
+ * Checks that the desired DB/objectStore exists and is initialized to the correct version
113
+ * @returns The initialized DB or undefined if it does not exist in the desired state
114
+ */
115
+ async tryGetInitializedDb() {
116
+ const dbs = await indexedDB.databases();
117
+ const dbExists = dbs.some((db) => {
118
+ return db.name === this.dbName && db.version === this.dbVersion;
119
+ });
120
+ if (dbExists) {
121
+ const db = await openDB(this.dbName, this.dbVersion);
122
+ const storeExists = db.objectStoreNames.contains(this.storeName);
123
+ if (storeExists) {
124
+ return db;
125
+ } else {
126
+ db.close();
127
+ }
128
+ }
129
+ }
130
+ /**
131
+ * Executes a callback with the initialized DB and then closes the db
132
+ * @param callback The method to execute with the initialized DB
133
+ * @returns
134
+ */
135
+ async tryUseDb(callback) {
136
+ const db = await this.tryGetInitializedDb();
137
+ if (db) {
138
+ try {
139
+ return await callback(db);
140
+ } finally {
141
+ db.close();
142
+ }
143
+ }
144
+ return void 0;
145
+ }
146
+ };
147
+ __name(_IndexedDbBoundWitnessDiviner, "IndexedDbBoundWitnessDiviner");
148
+ __publicField(_IndexedDbBoundWitnessDiviner, "configSchemas", [
149
+ ...__superGet(_IndexedDbBoundWitnessDiviner, _IndexedDbBoundWitnessDiviner, "configSchemas"),
150
+ IndexedDbBoundWitnessDivinerConfigSchema
151
+ ]);
152
+ __publicField(_IndexedDbBoundWitnessDiviner, "defaultConfigSchema", IndexedDbBoundWitnessDivinerConfigSchema);
153
+ var IndexedDbBoundWitnessDiviner = _IndexedDbBoundWitnessDiviner;
154
+ export {
155
+ IndexedDbBoundWitnessDiviner,
156
+ IndexedDbBoundWitnessDivinerConfigSchema,
157
+ IndexedDbBoundWitnessDivinerSchema
158
+ };
2
159
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/Schema.ts","../../src/Config.ts","../../src/Diviner.ts"],"sourcesContent":["import { BoundWitnessDivinerSchema } from '@xyo-network/diviner-boundwitness-model'\n\nexport const IndexedDbBoundWitnessDivinerSchema = `${BoundWitnessDivinerSchema}.indexeddb` as const\nexport type IndexedDbBoundWitnessDivinerSchema = typeof IndexedDbBoundWitnessDivinerSchema\n","import { IndexDescription } from '@xyo-network/archivist-model'\nimport { DivinerConfig } from '@xyo-network/diviner-model'\n\nimport { IndexedDbBoundWitnessDivinerSchema } from './Schema'\n\nexport const IndexedDbBoundWitnessDivinerConfigSchema = `${IndexedDbBoundWitnessDivinerSchema}.config` as const\nexport type IndexedDbBoundWitnessDivinerConfigSchema = typeof IndexedDbBoundWitnessDivinerConfigSchema\n\nexport type IndexedDbBoundWitnessDivinerConfig = DivinerConfig<{\n /**\n * The database name\n */\n dbName?: string\n /**\n * The version of the DB, defaults to 1\n */\n dbVersion?: number\n schema: IndexedDbBoundWitnessDivinerConfigSchema\n /**\n * The storage configuration\n * // TODO: Hoist to main diviner config\n */\n storage?: {\n /**\n * The indexes to create on the object store\n */\n indexes?: IndexDescription[]\n }\n /**\n * The name of the object store\n */\n storeName?: string\n}>\n","import { containsAll } from '@xylabs/array'\nimport { exists } from '@xylabs/exists'\nimport { IndexedDbArchivist } from '@xyo-network/archivist-indexeddb'\nimport { BoundWitness, BoundWitnessSchema, isBoundWitness } from '@xyo-network/boundwitness-model'\nimport { BoundWitnessDiviner } from '@xyo-network/diviner-boundwitness-abstract'\nimport { BoundWitnessDivinerQueryPayload, isBoundWitnessDivinerQueryPayload } from '@xyo-network/diviner-boundwitness-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport { Schema } from '@xyo-network/payload-model'\nimport { IDBPDatabase, openDB } from 'idb'\n\nimport { IndexedDbBoundWitnessDivinerConfigSchema } from './Config'\nimport { IndexedDbBoundWitnessDivinerParams } from './Params'\n\ninterface BoundWitnessStore {\n [s: string]: BoundWitness\n}\n\ntype ValueFilter = (bw?: BoundWitness | null) => boolean\n\nconst bwValueFilter = (\n key: keyof Pick<BoundWitness, 'addresses' | 'payload_hashes' | 'payload_schemas'>,\n values?: string[],\n): ValueFilter | undefined => {\n if (!values || values?.length === 0) return undefined\n return (bw) => {\n if (!bw) return false\n return containsAll(bw[key], values)\n }\n}\n\nexport class IndexedDbBoundWitnessDiviner<\n TParams extends IndexedDbBoundWitnessDivinerParams = IndexedDbBoundWitnessDivinerParams,\n TIn extends BoundWitnessDivinerQueryPayload = BoundWitnessDivinerQueryPayload,\n TOut extends BoundWitness = BoundWitness,\n> extends BoundWitnessDiviner<TParams, TIn, TOut> {\n static override readonly configSchemas: Schema[] = [...super.configSchemas, IndexedDbBoundWitnessDivinerConfigSchema]\n static override readonly defaultConfigSchema: Schema = IndexedDbBoundWitnessDivinerConfigSchema\n\n /**\n * The database name. If not supplied via config, it defaults\n * to the archivist's name and if archivist's name is not supplied,\n * it defaults to `archivist`. This behavior\n * biases towards a single, isolated DB per archivist which seems to\n * make the most sense for 99% of use cases.\n */\n get dbName() {\n return this.config?.dbName ?? this.config?.archivist ?? IndexedDbArchivist.defaultDbName\n }\n\n /**\n * The database version. If not supplied via config, it defaults to 1.\n */\n get dbVersion() {\n return this.config?.dbVersion ?? IndexedDbArchivist.defaultDbVersion\n }\n\n /**\n * The name of the object store. If not supplied via config, it defaults\n * to `payloads`.\n */\n get storeName() {\n return this.config?.storeName ?? IndexedDbArchivist.defaultStoreName\n }\n\n protected override async divineHandler(payloads?: TIn[]): Promise<TOut[]> {\n const query = payloads?.filter(isBoundWitnessDivinerQueryPayload)?.pop()\n if (!query) return []\n\n const result = await this.tryUseDb(async (db) => {\n const { addresses, payload_hashes, payload_schemas, limit, offset, order } = query\n const tx = db.transaction(this.storeName, 'readonly')\n const store = tx.objectStore(this.storeName)\n const results: TOut[] = []\n let parsedOffset = offset ?? 0\n const parsedLimit = limit ?? 10\n const direction: IDBCursorDirection = order === 'desc' ? 'prev' : 'next'\n const valueFilters: ValueFilter[] = [\n bwValueFilter('addresses', addresses),\n bwValueFilter('payload_hashes', payload_hashes),\n bwValueFilter('payload_schemas', payload_schemas),\n ].filter(exists)\n // Only iterate over BWs\n let cursor = await store.index(IndexedDbArchivist.schemaIndexName).openCursor(IDBKeyRange.only(BoundWitnessSchema), direction)\n\n // If we're filtering on more than just the schema, we need to\n // iterate through all the results\n if (valueFilters.length === 0) {\n // Skip records until the offset is reached\n while (cursor && parsedOffset > 0) {\n cursor = await cursor.advance(parsedOffset)\n parsedOffset = 0 // Reset offset after skipping\n }\n }\n // Collect results up to the limit\n while (cursor && results.length < parsedLimit) {\n const value = cursor.value\n if (value) {\n // If we're filtering on more than just the schema\n if (valueFilters.length > 0) {\n // Ensure all filters pass\n if (valueFilters.every((filter) => filter(value))) {\n // Then save the value\n results.push(value)\n }\n } else {\n // Otherwise just save the value\n results.push(value)\n }\n }\n try {\n cursor = await cursor.continue()\n } catch {\n break\n }\n }\n await tx.done\n // Remove any metadata before returning to the client\n return await Promise.all(\n results.filter(isBoundWitness).map((bw) => {\n return PayloadBuilder.build(bw)\n }),\n )\n })\n return result ?? []\n }\n\n protected override async startHandler() {\n await super.startHandler()\n // NOTE: Do not eager initialize the DB here. It will cause the\n // DB to be created by this process and then the DB will be\n // in a bad state for other processes that need to create the DB.\n return true\n }\n\n /**\n * Checks that the desired DB/objectStore exists and is initialized to the correct version\n * @returns The initialized DB or undefined if it does not exist in the desired state\n */\n private async tryGetInitializedDb(): Promise<IDBPDatabase<BoundWitnessStore> | undefined> {\n // Enumerate the DBs\n const dbs = await indexedDB.databases()\n // Check that the DB exists at the desired version\n const dbExists = dbs.some((db) => {\n return db.name === this.dbName && db.version === this.dbVersion\n })\n // If the DB exists at the desired version\n if (dbExists) {\n // If the db does exist, open it\n const db = await openDB<BoundWitnessStore>(this.dbName, this.dbVersion)\n // Check that the desired objectStore exists\n const storeExists = db.objectStoreNames.contains(this.storeName)\n // If the correct db/version/objectStore exists\n if (storeExists) {\n return db\n } else {\n // Otherwise close the db so the process that is going to update the\n // db can open it\n db.close()\n }\n }\n }\n\n /**\n * Executes a callback with the initialized DB and then closes the db\n * @param callback The method to execute with the initialized DB\n * @returns\n */\n private async tryUseDb<T>(callback: (db: IDBPDatabase<BoundWitnessStore>) => Promise<T> | T): Promise<T | undefined> {\n // Get the initialized DB\n const db = await this.tryGetInitializedDb()\n if (db) {\n try {\n // Perform the callback\n return await callback(db)\n } finally {\n // Close the DB\n db.close()\n }\n }\n return undefined\n }\n}\n"],"mappings":"gSAAA,OAASA,6BAAAA,MAAiC,0CAEnC,IAAMC,EAAqC,GAAGD,CAAAA,aCG9C,IAAME,EAA2C,GAAGC,CAAAA,UCL3D,OAASC,eAAAA,MAAmB,gBAC5B,OAASC,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AEZ,CACF,CAOA,MAAc/C,SAAYgD,EAA2F,CAEnH,IAAM/C,EAAK,MAAM,KAAKqC,oBAAmB,EACzC,GAAIrC,EACF,GAAI,CAEF,OAAO,MAAM+C,EAAS/C,CAAAA,CACxB,QAAA,CAEEA,EAAG8C,MAAK,CACV,CAGJ,CACF,EAnJUjE,EAAAA,EAAAA,gCACRmE,EALWpE,EAKcqE,gBAA0B,IAAIC,EAAAC,IAAMF,iBAAeG,IAC5EJ,EANWpE,EAMcyE,sBAA8BD,GANlD,IAAMxE,EAANuE","names":["BoundWitnessDivinerSchema","IndexedDbBoundWitnessDivinerSchema","IndexedDbBoundWitnessDivinerConfigSchema","IndexedDbBoundWitnessDivinerSchema","containsAll","exists","IndexedDbArchivist","BoundWitnessSchema","isBoundWitness","BoundWitnessDiviner","isBoundWitnessDivinerQueryPayload","PayloadBuilder","openDB","bwValueFilter","__name","key","values","length","bw","containsAll","IndexedDbBoundWitnessDiviner","BoundWitnessDiviner","dbName","_a","_b","config","archivist","IndexedDbArchivist","defaultDbName","dbVersion","defaultDbVersion","storeName","defaultStoreName","divineHandler","payloads","query","filter","isBoundWitnessDivinerQueryPayload","pop","tryUseDb","db","addresses","payload_hashes","payload_schemas","limit","offset","order","tx","transaction","store","objectStore","results","parsedOffset","parsedLimit","direction","valueFilters","exists","cursor","index","schemaIndexName","openCursor","IDBKeyRange","only","BoundWitnessSchema","advance","value","every","push","continue","done","Promise","all","isBoundWitness","map","PayloadBuilder","build","startHandler","tryGetInitializedDb","indexedDB","databases","some","name","version","openDB","objectStoreNames","contains","close","callback","__publicField","configSchemas","__superGet","_IndexedDbBoundWitnessDiviner","IndexedDbBoundWitnessDivinerConfigSchema","defaultConfigSchema"]}
1
+ {"version":3,"sources":["../../src/Schema.ts","../../src/Config.ts","../../src/Diviner.ts"],"sourcesContent":["import { BoundWitnessDivinerSchema } from '@xyo-network/diviner-boundwitness-model'\n\nexport const IndexedDbBoundWitnessDivinerSchema = `${BoundWitnessDivinerSchema}.indexeddb` as const\nexport type IndexedDbBoundWitnessDivinerSchema = typeof IndexedDbBoundWitnessDivinerSchema\n","import { IndexDescription } from '@xyo-network/archivist-model'\nimport { DivinerConfig } from '@xyo-network/diviner-model'\n\nimport { IndexedDbBoundWitnessDivinerSchema } from './Schema'\n\nexport const IndexedDbBoundWitnessDivinerConfigSchema = `${IndexedDbBoundWitnessDivinerSchema}.config` as const\nexport type IndexedDbBoundWitnessDivinerConfigSchema = typeof IndexedDbBoundWitnessDivinerConfigSchema\n\nexport type IndexedDbBoundWitnessDivinerConfig = DivinerConfig<{\n /**\n * The database name\n */\n dbName?: string\n /**\n * The version of the DB, defaults to 1\n */\n dbVersion?: number\n schema: IndexedDbBoundWitnessDivinerConfigSchema\n /**\n * The storage configuration\n * // TODO: Hoist to main diviner config\n */\n storage?: {\n /**\n * The indexes to create on the object store\n */\n indexes?: IndexDescription[]\n }\n /**\n * The name of the object store\n */\n storeName?: string\n}>\n","import { containsAll } from '@xylabs/array'\nimport { exists } from '@xylabs/exists'\nimport { IndexedDbArchivist } from '@xyo-network/archivist-indexeddb'\nimport { BoundWitness, BoundWitnessSchema, isBoundWitness } from '@xyo-network/boundwitness-model'\nimport { BoundWitnessDiviner } from '@xyo-network/diviner-boundwitness-abstract'\nimport { BoundWitnessDivinerQueryPayload, isBoundWitnessDivinerQueryPayload } from '@xyo-network/diviner-boundwitness-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport { Schema } from '@xyo-network/payload-model'\nimport { IDBPDatabase, openDB } from 'idb'\n\nimport { IndexedDbBoundWitnessDivinerConfigSchema } from './Config'\nimport { IndexedDbBoundWitnessDivinerParams } from './Params'\n\ninterface BoundWitnessStore {\n [s: string]: BoundWitness\n}\n\ntype ValueFilter = (bw?: BoundWitness | null) => boolean\n\nconst bwValueFilter = (\n key: keyof Pick<BoundWitness, 'addresses' | 'payload_hashes' | 'payload_schemas'>,\n values?: string[],\n): ValueFilter | undefined => {\n if (!values || values?.length === 0) return undefined\n return (bw) => {\n if (!bw) return false\n return containsAll(bw[key], values)\n }\n}\n\nexport class IndexedDbBoundWitnessDiviner<\n TParams extends IndexedDbBoundWitnessDivinerParams = IndexedDbBoundWitnessDivinerParams,\n TIn extends BoundWitnessDivinerQueryPayload = BoundWitnessDivinerQueryPayload,\n TOut extends BoundWitness = BoundWitness,\n> extends BoundWitnessDiviner<TParams, TIn, TOut> {\n static override readonly configSchemas: Schema[] = [...super.configSchemas, IndexedDbBoundWitnessDivinerConfigSchema]\n static override readonly defaultConfigSchema: Schema = IndexedDbBoundWitnessDivinerConfigSchema\n\n /**\n * The database name. If not supplied via config, it defaults\n * to the archivist's name and if archivist's name is not supplied,\n * it defaults to `archivist`. This behavior\n * biases towards a single, isolated DB per archivist which seems to\n * make the most sense for 99% of use cases.\n */\n get dbName() {\n return this.config?.dbName ?? this.config?.archivist ?? IndexedDbArchivist.defaultDbName\n }\n\n /**\n * The database version. If not supplied via config, it defaults to 1.\n */\n get dbVersion() {\n return this.config?.dbVersion ?? IndexedDbArchivist.defaultDbVersion\n }\n\n /**\n * The name of the object store. If not supplied via config, it defaults\n * to `payloads`.\n */\n get storeName() {\n return this.config?.storeName ?? IndexedDbArchivist.defaultStoreName\n }\n\n protected override async divineHandler(payloads?: TIn[]): Promise<TOut[]> {\n const query = payloads?.filter(isBoundWitnessDivinerQueryPayload)?.pop()\n if (!query) return []\n\n const result = await this.tryUseDb(async (db) => {\n const { addresses, payload_hashes, payload_schemas, limit, offset, order } = query\n const tx = db.transaction(this.storeName, 'readonly')\n const store = tx.objectStore(this.storeName)\n const results: TOut[] = []\n let parsedOffset = offset ?? 0\n const parsedLimit = limit ?? 10\n const direction: IDBCursorDirection = order === 'desc' ? 'prev' : 'next'\n const valueFilters: ValueFilter[] = [\n bwValueFilter('addresses', addresses),\n bwValueFilter('payload_hashes', payload_hashes),\n bwValueFilter('payload_schemas', payload_schemas),\n ].filter(exists)\n // Only iterate over BWs\n let cursor = await store.index(IndexedDbArchivist.schemaIndexName).openCursor(IDBKeyRange.only(BoundWitnessSchema), direction)\n\n // If we're filtering on more than just the schema, we need to\n // iterate through all the results\n if (valueFilters.length === 0) {\n // Skip records until the offset is reached\n while (cursor && parsedOffset > 0) {\n cursor = await cursor.advance(parsedOffset)\n parsedOffset = 0 // Reset offset after skipping\n }\n }\n // Collect results up to the limit\n while (cursor && results.length < parsedLimit) {\n const value = cursor.value\n if (value) {\n // If we're filtering on more than just the schema\n if (valueFilters.length > 0) {\n // Ensure all filters pass\n if (valueFilters.every((filter) => filter(value))) {\n // Then save the value\n results.push(value)\n }\n } else {\n // Otherwise just save the value\n results.push(value)\n }\n }\n try {\n cursor = await cursor.continue()\n } catch {\n break\n }\n }\n await tx.done\n // Remove any metadata before returning to the client\n return await Promise.all(\n results.filter(isBoundWitness).map((bw) => {\n return PayloadBuilder.build(bw)\n }),\n )\n })\n return result ?? []\n }\n\n protected override async startHandler() {\n await super.startHandler()\n // NOTE: Do not eager initialize the DB here. It will cause the\n // DB to be created by this process and then the DB will be\n // in a bad state for other processes that need to create the DB.\n return true\n }\n\n /**\n * Checks that the desired DB/objectStore exists and is initialized to the correct version\n * @returns The initialized DB or undefined if it does not exist in the desired state\n */\n private async tryGetInitializedDb(): Promise<IDBPDatabase<BoundWitnessStore> | undefined> {\n // Enumerate the DBs\n const dbs = await indexedDB.databases()\n // Check that the DB exists at the desired version\n const dbExists = dbs.some((db) => {\n return db.name === this.dbName && db.version === this.dbVersion\n })\n // If the DB exists at the desired version\n if (dbExists) {\n // If the db does exist, open it\n const db = await openDB<BoundWitnessStore>(this.dbName, this.dbVersion)\n // Check that the desired objectStore exists\n const storeExists = db.objectStoreNames.contains(this.storeName)\n // If the correct db/version/objectStore exists\n if (storeExists) {\n return db\n } else {\n // Otherwise close the db so the process that is going to update the\n // db can open it\n db.close()\n }\n }\n }\n\n /**\n * Executes a callback with the initialized DB and then closes the db\n * @param callback The method to execute with the initialized DB\n * @returns\n */\n private async tryUseDb<T>(callback: (db: IDBPDatabase<BoundWitnessStore>) => Promise<T> | T): Promise<T | undefined> {\n // Get the initialized DB\n const db = await this.tryGetInitializedDb()\n if (db) {\n try {\n // Perform the callback\n return await callback(db)\n } finally {\n // Close the DB\n db.close()\n }\n }\n return undefined\n }\n}\n"],"mappings":";;;;;;;;;AAAA,SAASA,iCAAiC;AAEnC,IAAMC,qCAAqC,GAAGD,yBAAAA;;;ACG9C,IAAME,2CAA2C,GAAGC,kCAAAA;;;ACL3D,SAASC,mBAAmB;AAC5B,SAASC,cAAc;AACvB,SAASC,0BAA0B;AACnC,SAAuBC,oBAAoBC,sBAAsB;AACjE,SAASC,2BAA2B;AACpC,SAA0CC,yCAAyC;AACnF,SAASC,sBAAsB;AAE/B,SAAuBC,cAAc;AAWrC,IAAMC,gBAAgB,wBACpBC,KACAC,WAAAA;AAEA,MAAI,CAACA,WAAUA,iCAAQC,YAAW,EAAG,QAAOC;AAC5C,SAAO,CAACC,OAAAA;AACN,QAAI,CAACA,GAAI,QAAO;AAChB,WAAOC,YAAYD,GAAGJ,GAAAA,GAAMC,MAAAA;EAC9B;AACF,GATsB;AAWf,IAAMK,gCAAN,MAAMA,sCAIHC,oBAAAA;;;;;;;;EAWR,IAAIC,SAAS;AA7Cf;AA8CI,aAAO,UAAKC,WAAL,mBAAaD,aAAU,UAAKC,WAAL,mBAAaC,cAAaC,mBAAmBC;EAC7E;;;;EAKA,IAAIC,YAAY;AApDlB;AAqDI,aAAO,UAAKJ,WAAL,mBAAaI,cAAaF,mBAAmBG;EACtD;;;;;EAMA,IAAIC,YAAY;AA5DlB;AA6DI,aAAO,UAAKN,WAAL,mBAAaM,cAAaJ,mBAAmBK;EACtD;EAEA,MAAyBC,cAAcC,UAAmC;AAhE5E;AAiEI,UAAMC,SAAQD,0CAAUE,OAAOC,uCAAjBH,mBAAqDI;AACnE,QAAI,CAACH,MAAO,QAAO,CAAA;AAEnB,UAAMI,SAAS,MAAM,KAAKC,SAAS,OAAOC,OAAAA;AACxC,YAAM,EAAEC,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,CAAA;EACnB;EAEA,MAAyBsC,eAAe;AACtC,UAAM,MAAMA,aAAAA;AAIZ,WAAO;EACT;;;;;EAMA,MAAcC,sBAA4E;AAExF,UAAMC,MAAM,MAAMC,UAAUC,UAAS;AAErC,UAAMC,WAAWH,IAAII,KAAK,CAAC1C,OAAAA;AACzB,aAAOA,GAAG2C,SAAS,KAAK5D,UAAUiB,GAAG4C,YAAY,KAAKxD;IACxD,CAAA;AAEA,QAAIqD,UAAU;AAEZ,YAAMzC,KAAK,MAAM6C,OAA0B,KAAK9D,QAAQ,KAAKK,SAAS;AAEtE,YAAM0D,cAAc9C,GAAG+C,iBAAiBC,SAAS,KAAK1D,SAAS;AAE/D,UAAIwD,aAAa;AACf,eAAO9C;MACT,OAAO;AAGLA,WAAGiD,MAAK;MACV;IACF;EACF;;;;;;EAOA,MAAclD,SAAYmD,UAA2F;AAEnH,UAAMlD,KAAK,MAAM,KAAKqC,oBAAmB;AACzC,QAAIrC,IAAI;AACN,UAAI;AAEF,eAAO,MAAMkD,SAASlD,EAAAA;MACxB,UAAA;AAEEA,WAAGiD,MAAK;MACV;IACF;AACA,WAAOvE;EACT;AACF;AAnJUI;AACR,cALWD,+BAKcsE,iBAA0B;KAAI,yEAAMA;EAAeC;;AAC5E,cANWvE,+BAMcwE,uBAA8BD;AANlD,IAAMvE,+BAAN;","names":["BoundWitnessDivinerSchema","IndexedDbBoundWitnessDivinerSchema","IndexedDbBoundWitnessDivinerConfigSchema","IndexedDbBoundWitnessDivinerSchema","containsAll","exists","IndexedDbArchivist","BoundWitnessSchema","isBoundWitness","BoundWitnessDiviner","isBoundWitnessDivinerQueryPayload","PayloadBuilder","openDB","bwValueFilter","key","values","length","undefined","bw","containsAll","IndexedDbBoundWitnessDiviner","BoundWitnessDiviner","dbName","config","archivist","IndexedDbArchivist","defaultDbName","dbVersion","defaultDbVersion","storeName","defaultStoreName","divineHandler","payloads","query","filter","isBoundWitnessDivinerQueryPayload","pop","result","tryUseDb","db","addresses","payload_hashes","payload_schemas","limit","offset","order","tx","transaction","store","objectStore","results","parsedOffset","parsedLimit","direction","valueFilters","exists","cursor","index","schemaIndexName","openCursor","IDBKeyRange","only","BoundWitnessSchema","advance","value","every","push","continue","done","Promise","all","isBoundWitness","map","PayloadBuilder","build","startHandler","tryGetInitializedDb","dbs","indexedDB","databases","dbExists","some","name","version","openDB","storeExists","objectStoreNames","contains","close","callback","configSchemas","IndexedDbBoundWitnessDivinerConfigSchema","defaultConfigSchema"]}
package/package.json CHANGED
@@ -12,25 +12,25 @@
12
12
  "dependencies": {
13
13
  "@xylabs/array": "^3.5.1",
14
14
  "@xylabs/exists": "^3.5.1",
15
- "@xyo-network/archivist-indexeddb": "~2.107.3",
16
- "@xyo-network/archivist-model": "~2.107.3",
17
- "@xyo-network/boundwitness-model": "~2.107.3",
18
- "@xyo-network/diviner-boundwitness-abstract": "~2.107.3",
19
- "@xyo-network/diviner-boundwitness-model": "~2.107.3",
20
- "@xyo-network/diviner-model": "~2.107.3",
21
- "@xyo-network/module-model": "~2.107.3",
22
- "@xyo-network/payload-builder": "~2.107.3",
23
- "@xyo-network/payload-model": "~2.107.3",
15
+ "@xyo-network/archivist-indexeddb": "~2.107.4",
16
+ "@xyo-network/archivist-model": "~2.107.4",
17
+ "@xyo-network/boundwitness-model": "~2.107.4",
18
+ "@xyo-network/diviner-boundwitness-abstract": "~2.107.4",
19
+ "@xyo-network/diviner-boundwitness-model": "~2.107.4",
20
+ "@xyo-network/diviner-model": "~2.107.4",
21
+ "@xyo-network/module-model": "~2.107.4",
22
+ "@xyo-network/payload-builder": "~2.107.4",
23
+ "@xyo-network/payload-model": "~2.107.4",
24
24
  "idb": "^8.0.0"
25
25
  },
26
26
  "devDependencies": {
27
- "@xylabs/ts-scripts-yarn3": "^3.11.9",
28
- "@xylabs/tsconfig": "^3.11.9",
29
- "@xyo-network/account": "~2.107.3",
30
- "@xyo-network/archivist-indexeddb": "~2.107.3",
31
- "@xyo-network/boundwitness-builder": "~2.107.3",
32
- "@xyo-network/node-memory": "~2.107.3",
33
- "@xyo-network/payload-builder": "~2.107.3",
27
+ "@xylabs/ts-scripts-yarn3": "^3.11.10",
28
+ "@xylabs/tsconfig": "^3.11.10",
29
+ "@xyo-network/account": "~2.107.4",
30
+ "@xyo-network/archivist-indexeddb": "~2.107.4",
31
+ "@xyo-network/boundwitness-builder": "~2.107.4",
32
+ "@xyo-network/node-memory": "~2.107.4",
33
+ "@xyo-network/payload-builder": "~2.107.4",
34
34
  "fake-indexeddb": "^5.0.2",
35
35
  "typescript": "^5.5.2"
36
36
  },
@@ -73,6 +73,6 @@
73
73
  "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js.git"
74
74
  },
75
75
  "sideEffects": false,
76
- "version": "2.107.3",
76
+ "version": "2.107.4",
77
77
  "type": "module"
78
78
  }