@xyo-network/diviner-boundwitness-indexeddb 2.90.2 → 2.90.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.
- package/dist/browser/Diviner.d.cts.map +1 -1
- package/dist/browser/Diviner.d.mts.map +1 -1
- package/dist/browser/Diviner.d.ts.map +1 -1
- package/dist/browser/index.cjs +4 -4
- package/dist/browser/index.cjs.map +1 -1
- package/dist/browser/index.js +4 -4
- package/dist/browser/index.js.map +1 -1
- package/dist/node/Diviner.d.cts.map +1 -1
- package/dist/node/Diviner.d.mts.map +1 -1
- package/dist/node/Diviner.d.ts.map +1 -1
- package/dist/node/index.cjs +4 -4
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.js +4 -4
- package/dist/node/index.js.map +1 -1
- package/package.json +17 -17
- package/src/Diviner.ts +6 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Diviner.d.ts","sourceRoot":"","sources":["../../src/Diviner.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAsC,MAAM,iCAAiC,CAAA;AAClG,OAAO,EAAE,mBAAmB,EAAE,MAAM,4CAA4C,CAAA;AAGhF,OAAO,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAA;AAIpD,OAAO,EAAE,kCAAkC,EAAE,MAAM,UAAU,CAAA;AAmB7D,qBAAa,4BAA4B,CACvC,OAAO,SAAS,kCAAkC,GAAG,kCAAkC,CACvF,SAAQ,mBAAmB,CAAC,OAAO,CAAC;IACpC,OAAgB,aAAa,WAA6C;IAE1E;;;;;;OAMG;IACH,IAAI,MAAM,WAET;IAED;;OAEG;IACH,IAAI,SAAS,WAEZ;IAED;;;OAGG;IACH,IAAI,SAAS,WAEZ;cAEwB,aAAa,CAAC,QAAQ,CAAC,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"Diviner.d.ts","sourceRoot":"","sources":["../../src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AAC,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"Diviner.d.ts","sourceRoot":"","sources":["../../src/Diviner.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAsC,MAAM,iCAAiC,CAAA;AAClG,OAAO,EAAE,mBAAmB,EAAE,MAAM,4CAA4C,CAAA;AAGhF,OAAO,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAA;AAIpD,OAAO,EAAE,kCAAkC,EAAE,MAAM,UAAU,CAAA;AAmB7D,qBAAa,4BAA4B,CACvC,OAAO,SAAS,kCAAkC,GAAG,kCAAkC,CACvF,SAAQ,mBAAmB,CAAC,OAAO,CAAC;IACpC,OAAgB,aAAa,WAA6C;IAE1E;;;;;;OAMG;IACH,IAAI,MAAM,WAET;IAED;;OAEG;IACH,IAAI,SAAS,WAEZ;IAED;;;OAGG;IACH,IAAI,SAAS,WAEZ;cAEwB,aAAa,CAAC,QAAQ,CAAC,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC;cA2D5D,YAAY;IAQrC;;;OAGG;YACW,mBAAmB;IAwBjC;;;;OAIG;YACW,QAAQ;CAcvB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Diviner.d.ts","sourceRoot":"","sources":["../../src/Diviner.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAsC,MAAM,iCAAiC,CAAA;AAClG,OAAO,EAAE,mBAAmB,EAAE,MAAM,4CAA4C,CAAA;AAGhF,OAAO,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAA;AAIpD,OAAO,EAAE,kCAAkC,EAAE,MAAM,UAAU,CAAA;AAmB7D,qBAAa,4BAA4B,CACvC,OAAO,SAAS,kCAAkC,GAAG,kCAAkC,CACvF,SAAQ,mBAAmB,CAAC,OAAO,CAAC;IACpC,OAAgB,aAAa,WAA6C;IAE1E;;;;;;OAMG;IACH,IAAI,MAAM,WAET;IAED;;OAEG;IACH,IAAI,SAAS,WAEZ;IAED;;;OAGG;IACH,IAAI,SAAS,WAEZ;cAEwB,aAAa,CAAC,QAAQ,CAAC,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"Diviner.d.ts","sourceRoot":"","sources":["../../src/Diviner.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAsC,MAAM,iCAAiC,CAAA;AAClG,OAAO,EAAE,mBAAmB,EAAE,MAAM,4CAA4C,CAAA;AAGhF,OAAO,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAA;AAIpD,OAAO,EAAE,kCAAkC,EAAE,MAAM,UAAU,CAAA;AAmB7D,qBAAa,4BAA4B,CACvC,OAAO,SAAS,kCAAkC,GAAG,kCAAkC,CACvF,SAAQ,mBAAmB,CAAC,OAAO,CAAC;IACpC,OAAgB,aAAa,WAA6C;IAE1E;;;;;;OAMG;IACH,IAAI,MAAM,WAET;IAED;;OAEG;IACH,IAAI,SAAS,WAEZ;IAED;;;OAGG;IACH,IAAI,SAAS,WAEZ;cAEwB,aAAa,CAAC,QAAQ,CAAC,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC;cA2D5D,YAAY;IAQrC;;;OAGG;YACW,mBAAmB;IAwBjC;;;;OAIG;YACW,QAAQ;CAcvB"}
|
package/dist/browser/index.cjs
CHANGED
|
@@ -41,7 +41,7 @@ var import_archivist_indexeddb = require("@xyo-network/archivist-indexeddb");
|
|
|
41
41
|
var import_boundwitness_model = require("@xyo-network/boundwitness-model");
|
|
42
42
|
var import_diviner_boundwitness_abstract = require("@xyo-network/diviner-boundwitness-abstract");
|
|
43
43
|
var import_diviner_boundwitness_model2 = require("@xyo-network/diviner-boundwitness-model");
|
|
44
|
-
var
|
|
44
|
+
var import_payload_builder = require("@xyo-network/payload-builder");
|
|
45
45
|
var import_idb = require("idb");
|
|
46
46
|
var bwValueFilter = /* @__PURE__ */ __name((key, values) => {
|
|
47
47
|
if (!values || values?.length === 0)
|
|
@@ -120,9 +120,9 @@ var IndexedDbBoundWitnessDiviner = class extends import_diviner_boundwitness_abs
|
|
|
120
120
|
cursor = await cursor.continue();
|
|
121
121
|
}
|
|
122
122
|
await tx.done;
|
|
123
|
-
return results.filter(import_boundwitness_model.isBoundWitness).map((bw) => {
|
|
124
|
-
return
|
|
125
|
-
});
|
|
123
|
+
return await Promise.all(results.filter(import_boundwitness_model.isBoundWitness).map((bw) => {
|
|
124
|
+
return import_payload_builder.PayloadBuilder.build(bw);
|
|
125
|
+
}));
|
|
126
126
|
});
|
|
127
127
|
return result ?? [];
|
|
128
128
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts","../../src/Schema.ts","../../src/Config.ts","../../src/Diviner.ts"],"sourcesContent":["export * from './Config'\nexport * from './Diviner'\nexport * from './Params'\nexport * from './Schema'\n","import { BoundWitnessDivinerSchema } from '@xyo-network/diviner-boundwitness-model'\n\nexport const IndexedDbBoundWitnessDivinerSchema = `${BoundWitnessDivinerSchema}.indexeddb`\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`\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 { isBoundWitnessDivinerQueryPayload } from '@xyo-network/diviner-boundwitness-model'\nimport { PayloadHasher } from '@xyo-network/hash'\nimport { Payload } 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> extends BoundWitnessDiviner<TParams> {\n static override configSchemas = [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?: Payload[]): Promise<BoundWitness[]> {\n const query = payloads?.filter(isBoundWitnessDivinerQueryPayload)?.pop()\n if (!query) return []\n\n const result = await this.tryUseDb(async (db) => {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\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: BoundWitness[] = []\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 cursor = await cursor.continue()\n }\n await tx.done\n // Remove any metadata before returning to the client\n return results.filter(isBoundWitness).map((bw) => {\n return PayloadHasher.jsonPayload(bw)\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;;;;;;;;;ACAA,wCAA0C;AAEnC,IAAMA,qCAAqC,GAAGC,2DAAAA;;;ACG9C,IAAMC,2CAA2C,GAAGC,kCAAAA;;;ACL3D,mBAA4B;AAC5B,oBAAuB;AACvB,iCAAmC;AACnC,gCAAiE;AACjE,2CAAoC;AACpC,IAAAC,qCAAkD;AAClD,kBAA8B;AAE9B,iBAAqC;AAWrC,IAAMC,gBAAgB,wBACpBC,KACAC,WAAAA;AAEA,MAAI,CAACA,UAAUA,QAAQC,WAAW;AAAG,WAAOC;AAC5C,SAAO,CAACC,OAAAA;AACN,QAAI,CAACA;AAAI,aAAO;AAChB,eAAOC,0BAAYD,GAAGJ,GAAAA,GAAMC,MAAAA;EAC9B;AACF,GATsB;AAWf,IAAMK,+BAAN,cAEGC,yDAAAA;EAhCV,OAgCUA;;;E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,OAAO;AAET,cAAIV,aAAaxC,SAAS,GAAG;AAE3B,gBAAIwC,aAAaW,MAAM,CAAC/B,WAAWA,OAAO8B,KAAAA,CAAAA,GAAS;AAEjDd,sBAAQgB,KAAKF,KAAAA;YACf;UACF,OAAO;AAELd,oBAAQgB,KAAKF,KAAAA;UACf;QACF;AACAR,iBAAS,MAAMA,OAAOW,SAAQ;MAChC;AACA,YAAMrB,GAAGsB;AAET,aAAOlB,QAAQhB,OAAOmC,wCAAAA,EAAgBC,IAAI,CAACtD,OAAAA;AACzC,eAAOuD,0BAAcC,YAAYxD,EAAAA;MACnC,CAAA;IACF,CAAA;AACA,WAAOqB,UAAU,CAAA;EACnB;EAEA,MAAyBoC,eAAe;AACtC,UAAM,MAAMA,aAAAA;AAIZ,WAAO;EACT;;;;;EAMA,MAAcC,sBAA4E;AAExF,UAAMC,MAAM,MAAMC,UAAUC,UAAS;AAErC,UAAMC,WAAWH,IAAII,KAAK,CAACxC,OAAAA;AACzB,aAAOA,GAAGyC,SAAS,KAAK1D,UAAUiB,GAAG0C,YAAY,KAAKtD;IACxD,CAAA;AAEA,QAAImD,UAAU;AAEZ,YAAMvC,KAAK,UAAM2C,mBAA0B,KAAK5D,QAAQ,KAAKK,SAAS;AAEtE,YAAMwD,cAAc5C,GAAG6C,iBAAiBC,SAAS,KAAKxD,SAAS;AAE/D,UAAIsD,aAAa;AACf,eAAO5C;MACT,OAAO;AAGLA,WAAG+C,MAAK;MACV;IACF;EACF;;;;;;EAOA,MAAchD,SAAYiD,UAA2F;AAEnH,UAAMhD,KAAK,MAAM,KAAKmC,oBAAmB;AACzC,QAAInC,IAAI;AACN,UAAI;AAEF,eAAO,MAAMgD,SAAShD,EAAAA;MACxB,UAAA;AAEEA,WAAG+C,MAAK;MACV;IACF;AACA,WAAOvE;EACT;AACF;","names":["IndexedDbBoundWitnessDivinerSchema","BoundWitnessDivinerSchema","IndexedDbBoundWitnessDivinerConfigSchema","IndexedDbBoundWitnessDivinerSchema","import_diviner_boundwitness_model","bwValueFilter","key","values","length","undefined","bw","containsAll","IndexedDbBoundWitnessDiviner","BoundWitnessDiviner","configSchemas","IndexedDbBoundWitnessDivinerConfigSchema","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","isBoundWitness","map","PayloadHasher","jsonPayload","startHandler","tryGetInitializedDb","dbs","indexedDB","databases","dbExists","some","name","version","openDB","storeExists","objectStoreNames","contains","close","callback"]}
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts","../../src/Schema.ts","../../src/Config.ts","../../src/Diviner.ts"],"sourcesContent":["export * from './Config'\nexport * from './Diviner'\nexport * from './Params'\nexport * from './Schema'\n","import { BoundWitnessDivinerSchema } from '@xyo-network/diviner-boundwitness-model'\n\nexport const IndexedDbBoundWitnessDivinerSchema = `${BoundWitnessDivinerSchema}.indexeddb`\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`\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 { isBoundWitnessDivinerQueryPayload } from '@xyo-network/diviner-boundwitness-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport { Payload } 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> extends BoundWitnessDiviner<TParams> {\n static override configSchemas = [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?: Payload[]): Promise<BoundWitness[]> {\n const query = payloads?.filter(isBoundWitnessDivinerQueryPayload)?.pop()\n if (!query) return []\n\n const result = await this.tryUseDb(async (db) => {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\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: BoundWitness[] = []\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 cursor = await cursor.continue()\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;;;;;;;;;ACAA,wCAA0C;AAEnC,IAAMA,qCAAqC,GAAGC,2DAAAA;;;ACG9C,IAAMC,2CAA2C,GAAGC,kCAAAA;;;ACL3D,mBAA4B;AAC5B,oBAAuB;AACvB,iCAAmC;AACnC,gCAAiE;AACjE,2CAAoC;AACpC,IAAAC,qCAAkD;AAClD,6BAA+B;AAE/B,iBAAqC;AAWrC,IAAMC,gBAAgB,wBACpBC,KACAC,WAAAA;AAEA,MAAI,CAACA,UAAUA,QAAQC,WAAW;AAAG,WAAOC;AAC5C,SAAO,CAACC,OAAAA;AACN,QAAI,CAACA;AAAI,aAAO;AAChB,eAAOC,0BAAYD,GAAGJ,GAAAA,GAAMC,MAAAA;EAC9B;AACF,GATsB;AAWf,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erSchema","BoundWitnessDivinerSchema","IndexedDbBoundWitnessDivinerConfigSchema","IndexedDbBoundWitnessDivinerSchema","import_diviner_boundwitness_model","bwValueFilter","key","values","length","undefined","bw","containsAll","IndexedDbBoundWitnessDiviner","BoundWitnessDiviner","configSchemas","IndexedDbBoundWitnessDivinerConfigSchema","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"]}
|
package/dist/browser/index.js
CHANGED
|
@@ -15,7 +15,7 @@ import { IndexedDbArchivist } from "@xyo-network/archivist-indexeddb";
|
|
|
15
15
|
import { BoundWitnessSchema, isBoundWitness } from "@xyo-network/boundwitness-model";
|
|
16
16
|
import { BoundWitnessDiviner } from "@xyo-network/diviner-boundwitness-abstract";
|
|
17
17
|
import { isBoundWitnessDivinerQueryPayload } from "@xyo-network/diviner-boundwitness-model";
|
|
18
|
-
import {
|
|
18
|
+
import { PayloadBuilder } from "@xyo-network/payload-builder";
|
|
19
19
|
import { openDB } from "idb";
|
|
20
20
|
var bwValueFilter = /* @__PURE__ */ __name((key, values) => {
|
|
21
21
|
if (!values || values?.length === 0)
|
|
@@ -94,9 +94,9 @@ var IndexedDbBoundWitnessDiviner = class extends BoundWitnessDiviner {
|
|
|
94
94
|
cursor = await cursor.continue();
|
|
95
95
|
}
|
|
96
96
|
await tx.done;
|
|
97
|
-
return results.filter(isBoundWitness).map((bw) => {
|
|
98
|
-
return
|
|
99
|
-
});
|
|
97
|
+
return await Promise.all(results.filter(isBoundWitness).map((bw) => {
|
|
98
|
+
return PayloadBuilder.build(bw);
|
|
99
|
+
}));
|
|
100
100
|
});
|
|
101
101
|
return result ?? [];
|
|
102
102
|
}
|
|
@@ -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`\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`\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 { isBoundWitnessDivinerQueryPayload } from '@xyo-network/diviner-boundwitness-model'\nimport { PayloadHasher } from '@xyo-network/hash'\nimport { Payload } 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> extends BoundWitnessDiviner<TParams> {\n static override configSchemas = [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?: Payload[]): Promise<BoundWitness[]> {\n const query = payloads?.filter(isBoundWitnessDivinerQueryPayload)?.pop()\n if (!query) return []\n\n const result = await this.tryUseDb(async (db) => {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\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: BoundWitness[] = []\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 cursor = await cursor.continue()\n }\n await tx.done\n // Remove any metadata before returning to the client\n return results.filter(isBoundWitness).map((bw) => {\n return PayloadHasher.jsonPayload(bw)\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,SAASC,yCAAyC;AAClD,SAASC,qBAAqB;AAE9B,SAAuBC,cAAc;AAWrC,IAAMC,gBAAgB,wBACpBC,KACAC,WAAAA;AAEA,MAAI,CAACA,UAAUA,QAAQC,WAAW;AAAG,WAAOC;AAC5C,SAAO,CAACC,OAAAA;AACN,QAAI,CAACA;AAAI,aAAO;AAChB,WAAOC,YAAYD,GAAGJ,GAAAA,GAAMC,MAAAA;EAC9B;AACF,GATsB;AAWf,IAAMK,+BAAN,cAEGC,oBAAAA;EAhCV,OAgCUA;;;EACR,OAAgBC,gBAAgB;IAACC;;;;;;;;;EASjC,IAAIC,SAAS;AACX,WAAO,KAAKC,QAAQD,UAAU,KAAKC,QAAQC,aAAaC,mBAAmBC;EAC7E;;;;EAKA,IAAIC,YAAY;AACd,WAAO,KAAKJ,QAAQI,aAAaF,mBAAmBG;EACtD;;;;;EAMA,IAAIC,YAAY;AACd,WAAO,KAAKN,QAAQM,aAAaJ,mBAAmBK;EACtD;EAEA,MAAyBC,cAAcC,UAA+C;AACpF,UAAMC,QAAQD,UAAUE,OAAOC,iCAAAA,GAAoCC,IAAAA;AAC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AAAA;MACnC,CAAA;IACF,CAAA;AACA,WAAOqB,UAAU,CAAA;EACnB;EAEA,MAAyBoC,eAAe;AACtC,UAAM,MAAMA,aAAAA;AAIZ,WAAO;EACT;;;;;EAMA,MAAcC,sBAA4E;AAExF,UAAMC,MAAM,MAAMC,UAAUC,UAAS;AAErC,UAAMC,WAAWH,IAAII,KAAK,CAACxC,OAAAA;AACzB,aAAOA,GAAGyC,SAAS,KAAK1D,UAAUiB,GAAG0C,YAAY,KAAKtD;IACxD,CAAA;AAEA,QAAImD,UAAU;AAEZ,YAAMvC,KAAK,MAAM2C,OAA0B,KAAK5D,QAAQ,KAAKK,SAAS;AAEtE,YAAMwD,cAAc5C,GAAG6C,iBAAiBC,SAAS,KAAKxD,SAAS;AAE/D,UAAIsD,aAAa;AACf,eAAO5C;MACT,OAAO;AAGLA,WAAG+C,MAAK;MACV;IACF;EACF;;;;;;EAOA,MAAchD,SAAYiD,UAA2F;AAEnH,UAAMhD,KAAK,MAAM,KAAKmC,oBAAmB;AACzC,QAAInC,IAAI;AACN,UAAI;AAEF,eAAO,MAAMgD,SAAShD,EAAAA;MACxB,UAAA;AAEEA,WAAG+C,MAAK;MACV;IACF;AACA,WAAOvE;EACT;AACF;","names":["BoundWitnessDivinerSchema","IndexedDbBoundWitnessDivinerSchema","IndexedDbBoundWitnessDivinerConfigSchema","IndexedDbBoundWitnessDivinerSchema","containsAll","exists","IndexedDbArchivist","BoundWitnessSchema","isBoundWitness","BoundWitnessDiviner","isBoundWitnessDivinerQueryPayload","PayloadHasher","openDB","bwValueFilter","key","values","length","undefined","bw","containsAll","IndexedDbBoundWitnessDiviner","BoundWitnessDiviner","configSchemas","IndexedDbBoundWitnessDivinerConfigSchema","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","isBoundWitness","map","PayloadHasher","jsonPayload","startHandler","tryGetInitializedDb","dbs","indexedDB","databases","dbExists","some","name","version","openDB","storeExists","objectStoreNames","contains","close","callback"]}
|
|
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`\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`\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 { isBoundWitnessDivinerQueryPayload } from '@xyo-network/diviner-boundwitness-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport { Payload } 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> extends BoundWitnessDiviner<TParams> {\n static override configSchemas = [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?: Payload[]): Promise<BoundWitness[]> {\n const query = payloads?.filter(isBoundWitnessDivinerQueryPayload)?.pop()\n if (!query) return []\n\n const result = await this.tryUseDb(async (db) => {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\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: BoundWitness[] = []\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 cursor = await cursor.continue()\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,SAASC,yCAAyC;AAClD,SAASC,sBAAsB;AAE/B,SAAuBC,cAAc;AAWrC,IAAMC,gBAAgB,wBACpBC,KACAC,WAAAA;AAEA,MAAI,CAACA,UAAUA,QAAQC,WAAW;AAAG,WAAOC;AAC5C,SAAO,CAACC,OAAAA;AACN,QAAI,CAACA;AAAI,aAAO;AAChB,WAAOC,YAAYD,GAAGJ,GAAAA,GAAMC,MAAAA;EAC9B;AACF,GATsB;AAWf,IAAMK,+BAAN,cAEGC,oBAAAA;EAhCV,OAgCUA;;;EACR,OAAgBC,gBAAgB;IAACC;;;;;;;;;EASjC,IAAIC,SAAS;AACX,WAAO,KAAKC,QAAQD,UAAU,KAAKC,QAAQC,aAAaC,mBAAmBC;EAC7E;;;;EAKA,IAAIC,YAAY;AACd,WAAO,KAAKJ,QAAQI,aAAaF,mBAAmBG;EACtD;;;;;EAMA,IAAIC,YAAY;AACd,WAAO,KAAKN,QAAQM,aAAaJ,mBAAmBK;EACtD;EAEA,MAA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,QAAQC,IACnBpB,QAAQhB,OAAOqC,cAAAA,EAAgBC,IAAI,CAACxD,OAAAA;AAClC,eAAOyD,eAAeC,MAAM1D,EAAAA;MAC9B,CAAA,CAAA;IAEJ,CAAA;AACA,WAAOqB,UAAU,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,WAAOzE;EACT;AACF;","names":["BoundWitnessDivinerSchema","IndexedDbBoundWitnessDivinerSchema","IndexedDbBoundWitnessDivinerConfigSchema","IndexedDbBoundWitnessDivinerSchema","containsAll","exists","IndexedDbArchivist","BoundWitnessSchema","isBoundWitness","BoundWitnessDiviner","isBoundWitnessDivinerQueryPayload","PayloadBuilder","openDB","bwValueFilter","key","values","length","undefined","bw","containsAll","IndexedDbBoundWitnessDiviner","BoundWitnessDiviner","configSchemas","IndexedDbBoundWitnessDivinerConfigSchema","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"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Diviner.d.ts","sourceRoot":"","sources":["../../src/Diviner.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAsC,MAAM,iCAAiC,CAAA;AAClG,OAAO,EAAE,mBAAmB,EAAE,MAAM,4CAA4C,CAAA;AAGhF,OAAO,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAA;AAIpD,OAAO,EAAE,kCAAkC,EAAE,MAAM,UAAU,CAAA;AAmB7D,qBAAa,4BAA4B,CACvC,OAAO,SAAS,kCAAkC,GAAG,kCAAkC,CACvF,SAAQ,mBAAmB,CAAC,OAAO,CAAC;IACpC,OAAgB,aAAa,WAA6C;IAE1E;;;;;;OAMG;IACH,IAAI,MAAM,WAET;IAED;;OAEG;IACH,IAAI,SAAS,WAEZ;IAED;;;OAGG;IACH,IAAI,SAAS,WAEZ;cAEwB,aAAa,CAAC,QAAQ,CAAC,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"Diviner.d.ts","sourceRoot":"","sources":["../../src/Diviner.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAsC,MAAM,iCAAiC,CAAA;AAClG,OAAO,EAAE,mBAAmB,EAAE,MAAM,4CAA4C,CAAA;AAGhF,OAAO,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAA;AAIpD,OAAO,EAAE,kCAAkC,EAAE,MAAM,UAAU,CAAA;AAmB7D,qBAAa,4BAA4B,CACvC,OAAO,SAAS,kCAAkC,GAAG,kCAAkC,CACvF,SAAQ,mBAAmB,CAAC,OAAO,CAAC;IACpC,OAAgB,aAAa,WAA6C;IAE1E;;;;;;OAMG;IACH,IAAI,MAAM,WAET;IAED;;OAEG;IACH,IAAI,SAAS,WAEZ;IAED;;;OAGG;IACH,IAAI,SAAS,WAEZ;cAEwB,aAAa,CAAC,QAAQ,CAAC,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC;cA2D5D,YAAY;IAQrC;;;OAGG;YACW,mBAAmB;IAwBjC;;;;OAIG;YACW,QAAQ;CAcvB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Diviner.d.ts","sourceRoot":"","sources":["../../src/Diviner.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAsC,MAAM,iCAAiC,CAAA;AAClG,OAAO,EAAE,mBAAmB,EAAE,MAAM,4CAA4C,CAAA;AAGhF,OAAO,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAA;AAIpD,OAAO,EAAE,kCAAkC,EAAE,MAAM,UAAU,CAAA;AAmB7D,qBAAa,4BAA4B,CACvC,OAAO,SAAS,kCAAkC,GAAG,kCAAkC,CACvF,SAAQ,mBAAmB,CAAC,OAAO,CAAC;IACpC,OAAgB,aAAa,WAA6C;IAE1E;;;;;;OAMG;IACH,IAAI,MAAM,WAET;IAED;;OAEG;IACH,IAAI,SAAS,WAEZ;IAED;;;OAGG;IACH,IAAI,SAAS,WAEZ;cAEwB,aAAa,CAAC,QAAQ,CAAC,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"Diviner.d.ts","sourceRoot":"","sources":["../../src/Diviner.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAsC,MAAM,iCAAiC,CAAA;AAClG,OA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diviner.d.ts","sourceRoot":"","sources":["../../src/Diviner.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAsC,MAAM,iCAAiC,CAAA;AAClG,OAAO,EAAE,mBAAmB,EAAE,MAAM,4CAA4C,CAAA;AAGhF,OAAO,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAA;AAIpD,OAAO,EAAE,kCAAkC,EAAE,MAAM,UAAU,CAAA;AAmB7D,qBAAa,4BAA4B,CACvC,OAAO,SAAS,kCAAkC,GAAG,kCAAkC,CACvF,SAAQ,mBAAmB,CAAC,OAAO,CAAC;IACpC,OAAgB,aAAa,WAA6C;IAE1E;;;;;;OAMG;IACH,IAAI,MAAM,WAET;IAED;;OAEG;IACH,IAAI,SAAS,WAEZ;IAED;;;OAGG;IACH,IAAI,SAAS,WAEZ;cAEwB,aAAa,CAAC,QAAQ,CAAC,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC;cA2D5D,YAAY;IAQrC;;;OAGG;YACW,mBAAmB;IAwBjC;;;;OAIG;YACW,QAAQ;CAcvB"}
|
package/dist/node/index.cjs
CHANGED
|
@@ -46,7 +46,7 @@ var import_archivist_indexeddb = require("@xyo-network/archivist-indexeddb");
|
|
|
46
46
|
var import_boundwitness_model = require("@xyo-network/boundwitness-model");
|
|
47
47
|
var import_diviner_boundwitness_abstract = require("@xyo-network/diviner-boundwitness-abstract");
|
|
48
48
|
var import_diviner_boundwitness_model2 = require("@xyo-network/diviner-boundwitness-model");
|
|
49
|
-
var
|
|
49
|
+
var import_payload_builder = require("@xyo-network/payload-builder");
|
|
50
50
|
var import_idb = require("idb");
|
|
51
51
|
var bwValueFilter = /* @__PURE__ */ __name((key, values) => {
|
|
52
52
|
if (!values || (values == null ? void 0 : values.length) === 0)
|
|
@@ -123,9 +123,9 @@ var _IndexedDbBoundWitnessDiviner = class _IndexedDbBoundWitnessDiviner extends
|
|
|
123
123
|
cursor = await cursor.continue();
|
|
124
124
|
}
|
|
125
125
|
await tx.done;
|
|
126
|
-
return results.filter(import_boundwitness_model.isBoundWitness).map((bw) => {
|
|
127
|
-
return
|
|
128
|
-
});
|
|
126
|
+
return await Promise.all(results.filter(import_boundwitness_model.isBoundWitness).map((bw) => {
|
|
127
|
+
return import_payload_builder.PayloadBuilder.build(bw);
|
|
128
|
+
}));
|
|
129
129
|
});
|
|
130
130
|
return result ?? [];
|
|
131
131
|
}
|
package/dist/node/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts","../../src/Schema.ts","../../src/Config.ts","../../src/Diviner.ts"],"sourcesContent":["export * from './Config'\nexport * from './Diviner'\nexport * from './Params'\nexport * from './Schema'\n","import { BoundWitnessDivinerSchema } from '@xyo-network/diviner-boundwitness-model'\n\nexport const IndexedDbBoundWitnessDivinerSchema = `${BoundWitnessDivinerSchema}.indexeddb`\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`\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 { isBoundWitnessDivinerQueryPayload } from '@xyo-network/diviner-boundwitness-model'\nimport { PayloadHasher } from '@xyo-network/hash'\nimport { Payload } 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> extends BoundWitnessDiviner<TParams> {\n static override configSchemas = [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?: Payload[]): Promise<BoundWitness[]> {\n const query = payloads?.filter(isBoundWitnessDivinerQueryPayload)?.pop()\n if (!query) return []\n\n const result = await this.tryUseDb(async (db) => {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\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: BoundWitness[] = []\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 cursor = await cursor.continue()\n }\n await tx.done\n // Remove any metadata before returning to the client\n return results.filter(isBoundWitness).map((bw) => {\n return PayloadHasher.jsonPayload(bw)\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;;;;;;;;;ACAA,wCAA0C;AAEnC,IAAMA,qCAAqC,GAAGC,2DAAAA;;;ACG9C,IAAMC,2CAA2C,GAAGC,kCAAAA;;;ACL3D,mBAA4B;AAC5B,oBAAuB;AACvB,iCAAmC;AACnC,gCAAiE;AACjE,2CAAoC;AACpC,IAAAC,qCAAkD;AAClD,kBAA8B;AAE9B,iBAAqC;AAWrC,IAAMC,gBAAgB,wBACpBC,KACAC,WAAAA;AAEA,MAAI,CAACA,WAAUA,iCAAQC,YAAW;AAAG,WAAOC;AAC5C,SAAO,CAACC,OAAAA;AACN,QAAI,CAACA;AAAI,aAAO;AAChB,eAAOC,0BAAYD,GAAGJ,GAAAA,GAAMC,MAAAA;EAC9B;AACF,GATsB;AAWf,IAAMK,gCAAN,MAAMA,sCAEHC,yDAAAA;;;;;;;;EAUR,IAAIC,SAAS;AA1Cf;AA2CI,aAAO,UAAKC,WAAL,mBAAaD,aAAU,UAAKC,WAAL,mBAAaC,cAAaC,8CAAmBC;EAC7E;;;;EAKA,IAAIC,YAAY;AAjDlB;AAkDI,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BAAQgB,KAAKF,KAAAA;YACf;UACF,OAAO;AAELd,oBAAQgB,KAAKF,KAAAA;UACf;QACF;AACAR,iBAAS,MAAMA,OAAOW,SAAQ;MAChC;AACA,YAAMrB,GAAGsB;AAET,aAAOlB,QAAQhB,OAAOmC,wCAAAA,EAAgBC,IAAI,CAACpD,OAAAA;AACzC,eAAOqD,0BAAcC,YAAYtD,EAAAA;MACnC,CAAA;IACF,CAAA;AACA,WAAOmB,UAAU,CAAA;EACnB;EAEA,MAAyBoC,eAAe;AACtC,UAAM,MAAMA,aAAAA;AAIZ,WAAO;EACT;;;;;EAMA,MAAcC,sBAA4E;AAExF,UAAMC,MAAM,MAAMC,UAAUC,UAAS;AAErC,UAAMC,WAAWH,IAAII,KAAK,CAACxC,OAAAA;AACzB,aAAOA,GAAGyC,SAAS,KAAK1D,UAAUiB,GAAG0C,YAAY,KAAKtD;IACxD,CAAA;AAEA,QAAImD,UAAU;AAEZ,YAAMvC,KAAK,UAAM2C,mBAA0B,KAAK5D,QAAQ,KAAKK,SAAS;AAEtE,YAAMwD,cAAc5C,GAAG6C,iBAAiBC,SAAS,KAAKxD,SAAS;AAE/D,UAAIsD,aAAa;AACf,eAAO5C;MACT,OAAO;AAGLA,WAAG+C,MAAK;MACV;IACF;EACF;;;;;;EAOA,MAAchD,SAAYiD,UAA2F;AAEnH,UAAMhD,KAAK,MAAM,KAAKmC,oBAAmB;AACzC,QAAInC,IAAI;AACN,UAAI;AAEF,eAAO,MAAMgD,SAAShD,EAAAA;MACxB,UAAA;AAEEA,WAAG+C,MAAK;MACV;IACF;AACA,WAAOrE;EACT;AACF;AA7IUI;AACR,cAHWD,+BAGKoE,iBAAgB;EAACC;;AAH5B,IAAMrE,+BAAN;","names":["IndexedDbBoundWitnessDivinerSchema","BoundWitnessDivinerSchema","IndexedDbBoundWitnessDivinerConfigSchema","IndexedDbBoundWitnessDivinerSchema","import_diviner_boundwitness_model","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","isBoundWitness","map","PayloadHasher","jsonPayload","startHandler","tryGetInitializedDb","dbs","indexedDB","databases","dbExists","some","name","version","openDB","storeExists","objectStoreNames","contains","close","callback","configSchemas","IndexedDbBoundWitnessDivinerConfigSchema"]}
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts","../../src/Schema.ts","../../src/Config.ts","../../src/Diviner.ts"],"sourcesContent":["export * from './Config'\nexport * from './Diviner'\nexport * from './Params'\nexport * from './Schema'\n","import { BoundWitnessDivinerSchema } from '@xyo-network/diviner-boundwitness-model'\n\nexport const IndexedDbBoundWitnessDivinerSchema = `${BoundWitnessDivinerSchema}.indexeddb`\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`\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 { isBoundWitnessDivinerQueryPayload } from '@xyo-network/diviner-boundwitness-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport { Payload } 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> extends BoundWitnessDiviner<TParams> {\n static override configSchemas = [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?: Payload[]): Promise<BoundWitness[]> {\n const query = payloads?.filter(isBoundWitnessDivinerQueryPayload)?.pop()\n if (!query) return []\n\n const result = await this.tryUseDb(async (db) => {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\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: BoundWitness[] = []\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 cursor = await cursor.continue()\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;;;;;;;;;ACAA,wCAA0C;AAEnC,IAAMA,qCAAqC,GAAGC,2DAAAA;;;ACG9C,IAAMC,2CAA2C,GAAGC,kCAAAA;;;ACL3D,mBAA4B;AAC5B,oBAAuB;AACvB,iCAAmC;AACnC,gCAAiE;AACjE,2CAAoC;AACpC,IAAAC,qCAAkD;AAClD,6BAA+B;AAE/B,iBAAqC;AAWrC,IAAMC,gBAAgB,wBACpBC,KACAC,WAAAA;AAEA,MAAI,CAACA,WAAUA,iCAAQC,YAAW;AAAG,WAAOC;AAC5C,SAAO,CAACC,OAAAA;AACN,QAAI,CAACA;AAAI,aAAO;AAChB,eAAOC,0BAAYD,GAAGJ,GAAAA,GAAMC,MAAAA;EAC9B;AACF,GATsB;AAWf,IAAMK,gCAAN,MAAMA,sCAEHC,yDAAAA;;;;;;;;E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AAH5B,IAAMvE,+BAAN;","names":["IndexedDbBoundWitnessDivinerSchema","BoundWitnessDivinerSchema","IndexedDbBoundWitnessDivinerConfigSchema","IndexedDbBoundWitnessDivinerSchema","import_diviner_boundwitness_model","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"]}
|
package/dist/node/index.js
CHANGED
|
@@ -20,7 +20,7 @@ import { IndexedDbArchivist } from "@xyo-network/archivist-indexeddb";
|
|
|
20
20
|
import { BoundWitnessSchema, isBoundWitness } from "@xyo-network/boundwitness-model";
|
|
21
21
|
import { BoundWitnessDiviner } from "@xyo-network/diviner-boundwitness-abstract";
|
|
22
22
|
import { isBoundWitnessDivinerQueryPayload } from "@xyo-network/diviner-boundwitness-model";
|
|
23
|
-
import {
|
|
23
|
+
import { PayloadBuilder } from "@xyo-network/payload-builder";
|
|
24
24
|
import { openDB } from "idb";
|
|
25
25
|
var bwValueFilter = /* @__PURE__ */ __name((key, values) => {
|
|
26
26
|
if (!values || (values == null ? void 0 : values.length) === 0)
|
|
@@ -97,9 +97,9 @@ var _IndexedDbBoundWitnessDiviner = class _IndexedDbBoundWitnessDiviner extends
|
|
|
97
97
|
cursor = await cursor.continue();
|
|
98
98
|
}
|
|
99
99
|
await tx.done;
|
|
100
|
-
return results.filter(isBoundWitness).map((bw) => {
|
|
101
|
-
return
|
|
102
|
-
});
|
|
100
|
+
return await Promise.all(results.filter(isBoundWitness).map((bw) => {
|
|
101
|
+
return PayloadBuilder.build(bw);
|
|
102
|
+
}));
|
|
103
103
|
});
|
|
104
104
|
return result ?? [];
|
|
105
105
|
}
|
package/dist/node/index.js.map
CHANGED
|
@@ -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`\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`\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 { isBoundWitnessDivinerQueryPayload } from '@xyo-network/diviner-boundwitness-model'\nimport { PayloadHasher } from '@xyo-network/hash'\nimport { Payload } 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> extends BoundWitnessDiviner<TParams> {\n static override configSchemas = [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?: Payload[]): Promise<BoundWitness[]> {\n const query = payloads?.filter(isBoundWitnessDivinerQueryPayload)?.pop()\n if (!query) return []\n\n const result = await this.tryUseDb(async (db) => {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\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: BoundWitness[] = []\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 cursor = await cursor.continue()\n }\n await tx.done\n // Remove any metadata before returning to the client\n return results.filter(isBoundWitness).map((bw) => {\n return PayloadHasher.jsonPayload(bw)\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,SAASC,yCAAyC;AAClD,SAASC,qBAAqB;AAE9B,SAAuBC,cAAc;AAWrC,IAAMC,gBAAgB,wBACpBC,KACAC,WAAAA;AAEA,MAAI,CAACA,WAAUA,iCAAQC,YAAW;AAAG,WAAOC;AAC5C,SAAO,CAACC,OAAAA;AACN,QAAI,CAACA;AAAI,aAAO;AAChB,WAAOC,YAAYD,GAAGJ,GAAAA,GAAMC,MAAAA;EAC9B;AACF,GATsB;AAWf,IAAMK,gCAAN,MAAMA,sCAEHC,oBAAAA;;;;;;;;EAUR,IAAIC,SAAS;AA1Cf;AA2CI,aAAO,UAAKC,WAAL,mBAAaD,aAAU,UAAKC,WAAL,mBAAaC,cAAaC,mBAAmBC;EAC7E;;;;EAKA,IAAIC,YAAY;AAjDlB;AAkDI,aAAO,UAAKJ,WAAL,mBAAaI,cAAaF,mBAAmBG;EACtD;;;;;EAMA,IAAIC,YAAY;AAzDlB;AA0DI,aAAO,UAAKN,WAAL,mB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AChC;AACA,YAAMrB,GAAGsB;AAET,aAAOlB,QAAQhB,OAAOmC,cAAAA,EAAgBC,IAAI,CAACpD,OAAAA;AACzC,eAAOqD,cAAcC,YAAYtD,EAAAA;MACnC,CAAA;IACF,CAAA;AACA,WAAOmB,UAAU,CAAA;EACnB;EAEA,MAAyBoC,eAAe;AACtC,UAAM,MAAMA,aAAAA;AAIZ,WAAO;EACT;;;;;EAMA,MAAcC,sBAA4E;AAExF,UAAMC,MAAM,MAAMC,UAAUC,UAAS;AAErC,UAAMC,WAAWH,IAAII,KAAK,CAACxC,OAAAA;AACzB,aAAOA,GAAGyC,SAAS,KAAK1D,UAAUiB,GAAG0C,YAAY,KAAKtD;IACxD,CAAA;AAEA,QAAImD,UAAU;AAEZ,YAAMvC,KAAK,MAAM2C,OAA0B,KAAK5D,QAAQ,KAAKK,SAAS;AAEtE,YAAMwD,cAAc5C,GAAG6C,iBAAiBC,SAAS,KAAKxD,SAAS;AAE/D,UAAIsD,aAAa;AACf,eAAO5C;MACT,OAAO;AAGLA,WAAG+C,MAAK;MACV;IACF;EACF;;;;;;EAOA,MAAchD,SAAYiD,UAA2F;AAEnH,UAAMhD,KAAK,MAAM,KAAKmC,oBAAmB;AACzC,QAAInC,IAAI;AACN,UAAI;AAEF,eAAO,MAAMgD,SAAShD,EAAAA;MACxB,UAAA;AAEEA,WAAG+C,MAAK;MACV;IACF;AACA,WAAOrE;EACT;AACF;AA7IUI;AACR,cAHWD,+BAGKoE,iBAAgB;EAACC;;AAH5B,IAAMrE,+BAAN;","names":["BoundWitnessDivinerSchema","IndexedDbBoundWitnessDivinerSchema","IndexedDbBoundWitnessDivinerConfigSchema","IndexedDbBoundWitnessDivinerSchema","containsAll","exists","IndexedDbArchivist","BoundWitnessSchema","isBoundWitness","BoundWitnessDiviner","isBoundWitnessDivinerQueryPayload","PayloadHasher","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","isBoundWitness","map","PayloadHasher","jsonPayload","startHandler","tryGetInitializedDb","dbs","indexedDB","databases","dbExists","some","name","version","openDB","storeExists","objectStoreNames","contains","close","callback","configSchemas","IndexedDbBoundWitnessDivinerConfigSchema"]}
|
|
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`\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`\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 { isBoundWitnessDivinerQueryPayload } from '@xyo-network/diviner-boundwitness-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport { Payload } 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> extends BoundWitnessDiviner<TParams> {\n static override configSchemas = [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?: Payload[]): Promise<BoundWitness[]> {\n const query = payloads?.filter(isBoundWitnessDivinerQueryPayload)?.pop()\n if (!query) return []\n\n const result = await this.tryUseDb(async (db) => {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\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: BoundWitness[] = []\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 cursor = await cursor.continue()\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,SAASC,yCAAyC;AAClD,SAASC,sBAAsB;AAE/B,SAAuBC,cAAc;AAWrC,IAAMC,gBAAgB,wBACpBC,KACAC,WAAAA;AAEA,MAAI,CAACA,WAAUA,iCAAQC,YAAW;AAAG,WAAOC;AAC5C,SAAO,CAACC,OAAAA;AACN,QAAI,CAACA;AAAI,aAAO;AAChB,WAAOC,YAAYD,GAAGJ,GAAAA,GAAMC,MAAAA;EAC9B;AACF,GATsB;AAWf,IAAMK,gCAAN,MAAMA,sCAEHC,oBAAAA;;;;;;;;EAUR,IAAIC,SAAS;AA1Cf;AA2CI,aAAO,UAAKC,WAAL,mBAAaD,aAAU,UAAKC,WAAL,m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DivinerConfigSchema","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"]}
|
package/package.json
CHANGED
|
@@ -10,27 +10,27 @@
|
|
|
10
10
|
"url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/issues"
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@xylabs/array": "^2.14.
|
|
14
|
-
"@xylabs/exists": "^2.14.
|
|
15
|
-
"@xyo-network/archivist-indexeddb": "~2.90.
|
|
16
|
-
"@xyo-network/archivist-model": "~2.90.
|
|
17
|
-
"@xyo-network/boundwitness-model": "~2.90.
|
|
18
|
-
"@xyo-network/diviner-boundwitness-abstract": "~2.90.
|
|
19
|
-
"@xyo-network/diviner-boundwitness-model": "~2.90.
|
|
20
|
-
"@xyo-network/diviner-model": "~2.90.
|
|
21
|
-
"@xyo-network/
|
|
22
|
-
"@xyo-network/
|
|
23
|
-
"@xyo-network/payload-model": "~2.90.
|
|
13
|
+
"@xylabs/array": "^2.14.1",
|
|
14
|
+
"@xylabs/exists": "^2.14.1",
|
|
15
|
+
"@xyo-network/archivist-indexeddb": "~2.90.4",
|
|
16
|
+
"@xyo-network/archivist-model": "~2.90.4",
|
|
17
|
+
"@xyo-network/boundwitness-model": "~2.90.4",
|
|
18
|
+
"@xyo-network/diviner-boundwitness-abstract": "~2.90.4",
|
|
19
|
+
"@xyo-network/diviner-boundwitness-model": "~2.90.4",
|
|
20
|
+
"@xyo-network/diviner-model": "~2.90.4",
|
|
21
|
+
"@xyo-network/module-model": "~2.90.4",
|
|
22
|
+
"@xyo-network/payload-builder": "~2.90.4",
|
|
23
|
+
"@xyo-network/payload-model": "~2.90.4",
|
|
24
24
|
"idb": "^8.0.0"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@xylabs/ts-scripts-yarn3": "^3.2.42",
|
|
28
28
|
"@xylabs/tsconfig": "^3.2.42",
|
|
29
|
-
"@xyo-network/account": "~2.90.
|
|
30
|
-
"@xyo-network/archivist-indexeddb": "~2.90.
|
|
31
|
-
"@xyo-network/boundwitness-builder": "~2.90.
|
|
32
|
-
"@xyo-network/node-memory": "~2.90.
|
|
33
|
-
"@xyo-network/payload-builder": "~2.90.
|
|
29
|
+
"@xyo-network/account": "~2.90.4",
|
|
30
|
+
"@xyo-network/archivist-indexeddb": "~2.90.4",
|
|
31
|
+
"@xyo-network/boundwitness-builder": "~2.90.4",
|
|
32
|
+
"@xyo-network/node-memory": "~2.90.4",
|
|
33
|
+
"@xyo-network/payload-builder": "~2.90.4",
|
|
34
34
|
"fake-indexeddb": "^5.0.2",
|
|
35
35
|
"typescript": "^5.3.3"
|
|
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.90.
|
|
76
|
+
"version": "2.90.4",
|
|
77
77
|
"type": "module"
|
|
78
78
|
}
|
package/src/Diviner.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { IndexedDbArchivist } from '@xyo-network/archivist-indexeddb'
|
|
|
4
4
|
import { BoundWitness, BoundWitnessSchema, isBoundWitness } from '@xyo-network/boundwitness-model'
|
|
5
5
|
import { BoundWitnessDiviner } from '@xyo-network/diviner-boundwitness-abstract'
|
|
6
6
|
import { isBoundWitnessDivinerQueryPayload } from '@xyo-network/diviner-boundwitness-model'
|
|
7
|
-
import {
|
|
7
|
+
import { PayloadBuilder } from '@xyo-network/payload-builder'
|
|
8
8
|
import { Payload } from '@xyo-network/payload-model'
|
|
9
9
|
import { IDBPDatabase, openDB } from 'idb'
|
|
10
10
|
|
|
@@ -109,9 +109,11 @@ export class IndexedDbBoundWitnessDiviner<
|
|
|
109
109
|
}
|
|
110
110
|
await tx.done
|
|
111
111
|
// Remove any metadata before returning to the client
|
|
112
|
-
return
|
|
113
|
-
|
|
114
|
-
|
|
112
|
+
return await Promise.all(
|
|
113
|
+
results.filter(isBoundWitness).map((bw) => {
|
|
114
|
+
return PayloadBuilder.build(bw)
|
|
115
|
+
}),
|
|
116
|
+
)
|
|
115
117
|
})
|
|
116
118
|
return result ?? []
|
|
117
119
|
}
|