From 05793e1658c42fa923447fcfca2118a63954ddda Mon Sep 17 00:00:00 2001 From: Brad Anderson Date: Mon, 13 May 2024 20:11:36 -0400 Subject: [PATCH] Make CI happy --- .github/workflows/ci.yaml | 4 +- .../encrypted-blockstore/src/connection.ts | 21 ++-- .../src/encrypt-helpers.ts | 6 +- packages/encrypted-blockstore/src/index.ts | 4 +- packages/encrypted-blockstore/src/loader.ts | 30 +++-- .../encrypted-blockstore/src/remote-wal.ts | 6 +- .../encrypted-blockstore/src/store-memory.ts | 11 +- .../encrypted-blockstore/src/store-remote.ts | 5 +- .../encrypted-blockstore/src/transaction.ts | 9 +- packages/encrypted-blockstore/src/types.ts | 7 +- packages/encrypted-blockstore/src/version.ts | 2 +- .../encrypted-blockstore/test/loader.test.js | 8 +- .../test/transaction.test.js | 20 ++- packages/react-native/README.md | 2 +- packages/react-native/package.json | 4 +- packages/react-native/src/index.tsx | 30 ++--- packages/react-native/src/store-native.ts | 119 +++++++++--------- packages/react-native/tsconfig.json | 4 +- packages/react/package.json | 2 +- packages/react/tsconfig.json | 4 +- packages/solid-js/tsconfig.json | 4 +- 21 files changed, 150 insertions(+), 152 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 3be95919..d6730f3b 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -17,9 +17,9 @@ jobs: node-version: 20.10.0 - name: Install pnpm - uses: pnpm/action-setup@v2 + uses: pnpm/action-setup@v4 with: - version: 8.15.0 + version: 9 run_install: false - name: Install Dependencies diff --git a/packages/encrypted-blockstore/src/connection.ts b/packages/encrypted-blockstore/src/connection.ts index 92d5f59c..aedbb26f 100644 --- a/packages/encrypted-blockstore/src/connection.ts +++ b/packages/encrypted-blockstore/src/connection.ts @@ -1,11 +1,11 @@ import { RemoteDataStore, RemoteMetaStore } from './store-remote' import type { - UploadMetaFnParams, - UploadDataFnParams, + AnyLink, + DownloadDataFnParams, DownloadMetaFnParams, - DownloadDataFnParams + UploadDataFnParams, + UploadMetaFnParams } from './types' -import type { AnyLink } from './types' import { type Loader } from './loader' import { EventBlock, decodeEventBlock } from '@web3-storage/pail/clock' import { EventView } from '@web3-storage/pail/clock/api' @@ -35,6 +35,7 @@ export abstract class Connection { params: UploadDataFnParams, opts?: { public?: boolean } ): Promise + abstract metaDownload(params: DownloadMetaFnParams): Promise abstract dataDownload(params: DownloadDataFnParams): Promise @@ -59,15 +60,15 @@ export abstract class Connection { this.loader = loader this.taskManager = new TaskManager(loader) this.onConnect() - const remote = new RemoteMetaStore(this.loader!.name, this) + const remote = new RemoteMetaStore(this.loader.name, this) remote.onLoad('main', async metas => { if (metas) { await this.loader!.handleDbMetasFromStore(metas) } }) - this.loader!.remoteMetaStore = remote - this.loaded = this.loader!.ready.then(async () => { - remote!.load('main').then(() => { + this.loader.remoteMetaStore = remote + this.loaded = this.loader.ready.then(async () => { + remote.load('main').then(() => { void this.loader!.remoteWAL?._process() }) }) @@ -78,8 +79,8 @@ export abstract class Connection { connectStorage({ loader }: { loader?: Loader }) { if (!loader) throw new Error('loader is required') this.loader = loader - loader!.remoteCarStore = new RemoteDataStore(this.loader!.name, this) - loader!.remoteFileStore = new RemoteDataStore(this.loader!.name, this) + loader.remoteCarStore = new RemoteDataStore(this.loader.name, this) + loader.remoteFileStore = new RemoteDataStore(this.loader.name, this) } async createEventBlock(bytes: Uint8Array): Promise { diff --git a/packages/encrypted-blockstore/src/encrypt-helpers.ts b/packages/encrypted-blockstore/src/encrypt-helpers.ts index b4b788da..90b80229 100644 --- a/packages/encrypted-blockstore/src/encrypt-helpers.ts +++ b/packages/encrypted-blockstore/src/encrypt-helpers.ts @@ -22,7 +22,7 @@ import type { AnyBlock, CarMakeable, AnyLink, AnyDecodedBlock, CryptoOpts } from function makeEncDec(crypto: any, randomBytes: (size: number) => Uint8Array) { const codec = makeCodec(crypto, randomBytes) - const encrypt = async function* ({ + const encrypt = async function * ({ get, cids, hasher, @@ -66,7 +66,7 @@ function makeEncDec(crypto: any, randomBytes: (size: number) => Uint8Array) { yield block } - const decrypt = async function* ({ + const decrypt = async function * ({ root, get, key, @@ -120,7 +120,7 @@ function makeEncDec(crypto: any, randomBytes: (size: number) => Uint8Array) { for (const { cid } of nodes) { if (!rootBlock.cid.equals(cid)) promises.push(getWithDecrypt(cid).then(unwrap)) } - yield* promises + yield * promises yield unwrap(rootBlock) } return { encrypt, decrypt } diff --git a/packages/encrypted-blockstore/src/index.ts b/packages/encrypted-blockstore/src/index.ts index b7540e43..15e3c5ba 100644 --- a/packages/encrypted-blockstore/src/index.ts +++ b/packages/encrypted-blockstore/src/index.ts @@ -1,13 +1,13 @@ import { ConnectREST } from './connect-rest' import { Connection, CarClockHead, Connectable, DbMetaEventBlock } from './connection' -export { makeStores } from './store-remote' -import { AnyLink } from './types' import { + AnyLink, UploadDataFnParams, UploadMetaFnParams, DownloadDataFnParams, DownloadMetaFnParams } from './types' +export { makeStores } from './store-remote' type RawConnectionParams = { metaUpload: (bytes: Uint8Array, params: UploadMetaFnParams) => Promise diff --git a/packages/encrypted-blockstore/src/loader.ts b/packages/encrypted-blockstore/src/loader.ts index 03a71c36..ed0bca0b 100644 --- a/packages/encrypted-blockstore/src/loader.ts +++ b/packages/encrypted-blockstore/src/loader.ts @@ -3,17 +3,15 @@ import { CarReader } from '@ipld/car' import { CID } from 'multiformats' import type { AnyBlock, AnyLink, CarHeader, CommitOpts, DbMeta, TransactionMeta } from './types' -import type { BlockstoreOpts } from './transaction' +import type { BlockstoreOpts, CarTransaction } from './transaction' import { encodeCarFile, encodeCarHeader, parseCarFile } from './loader-helpers' import { decodeEncryptedCar, encryptedEncodeCarFile } from './encrypt-helpers' import { getCrypto, randomBytes } from './crypto-web' -import { DataStore, MetaStore } from './store' +import { DataStore, MetaStore, DataStore as AbstractDataStore, MetaStore as AbstractMetaStore } from './store' import { RemoteWAL } from './remote-wal' -import { DataStore as AbstractDataStore, MetaStore as AbstractMetaStore } from './store' -import type { CarTransaction } from './transaction' import { CommitQueue } from './commit-queue' export function cidListIncludes(list: AnyLink[], cid: AnyLink) { @@ -82,9 +80,8 @@ export class Loader implements Loadable { this.remoteWAL = ebOpts.store.makeRemoteWAL(this) this.ready = Promise.resolve().then(async () => { this.metaStore = ebOpts.store.makeMetaStore(this) - if (!this.metaStore || !this.carStore || !this.remoteWAL) - throw new Error('stores not initialized') - const metas = this.ebOpts.meta ? [this.ebOpts.meta] : await this.metaStore!.load('main') + if (!this.metaStore || !this.carStore || !this.remoteWAL) { throw new Error('stores not initialized') } + const metas = this.ebOpts.meta ? [this.ebOpts.meta] : await this.metaStore.load('main') if (metas) { await this.handleDbMetasFromStore(metas) } @@ -127,7 +124,7 @@ export class Loader implements Loadable { if (cidListIncludes(this.carLog, meta.car)) { return } - const carHeader = (await this.loadCarHeaderFromMeta(meta)) as CarHeader + const carHeader = (await this.loadCarHeaderFromMeta(meta)) // fetch other cars down the compact log? // todo we should use a CID set for the compacted cids (how to expire?) // console.log('merge carHeader', carHeader.head.length, carHeader.head.toString(), meta.car.toString()) @@ -170,6 +167,7 @@ export class Loader implements Loadable { ): Promise { return this.commitQueue.enqueue(() => this._commitInternalFiles(t, done, opts)) } + // can these skip the queue? or have a file queue? async _commitInternalFiles( t: CarTransaction, @@ -181,8 +179,8 @@ export class Loader implements Loadable { files: AnyLink[] } const { cid, bytes } = await this.prepareCarFile(roots[0], t, !!opts.public) - await this.fileStore!.save({ cid, bytes }) - await this.remoteWAL!.enqueueFile(cid, !!opts.public) + await this.fileStore.save({ cid, bytes }) + await this.remoteWAL.enqueueFile(cid, !!opts.public) return cid } @@ -224,13 +222,13 @@ export class Loader implements Loadable { opts: CommitOpts = { noLoader: false, compact: false } ): Promise { await this.ready - const fp = this.makeCarHeader(done, this.carLog, !!opts.compact) as CarHeader - let roots: AnyLink[] = await this.prepareRoots(fp, t) + const fp = this.makeCarHeader(done, this.carLog, !!opts.compact) + const roots: AnyLink[] = await this.prepareRoots(fp, t) const { cid, bytes } = await this.prepareCarFile(roots[0], t, !!opts.public) - await this.carStore!.save({ cid, bytes }) + await this.carStore.save({ cid, bytes }) await this.cacheTransaction(t) const newDbMeta = { car: cid, key: this.key || null } as DbMeta - await this.remoteWAL!.enqueue(newDbMeta, opts) + await this.remoteWAL.enqueue(newDbMeta, opts) await this.metaStore!.save(newDbMeta) await this.updateCarLog(cid, fp, !!opts.compact) return cid @@ -281,7 +279,7 @@ export class Loader implements Loadable { car: cid } as unknown as DbMeta) for (const cid of carHeader.compact) { - await this.carStore!.remove(cid) + await this.carStore.remove(cid) } } @@ -294,7 +292,7 @@ export class Loader implements Loadable { // } // } - async *entries(cache = true): AsyncIterableIterator { + async * entries(cache = true): AsyncIterableIterator { await this.ready if (cache) { for (const [, block] of this.getBlockCache) { diff --git a/packages/encrypted-blockstore/src/remote-wal.ts b/packages/encrypted-blockstore/src/remote-wal.ts index 68d18340..bc275894 100644 --- a/packages/encrypted-blockstore/src/remote-wal.ts +++ b/packages/encrypted-blockstore/src/remote-wal.ts @@ -81,8 +81,7 @@ export abstract class RemoteWAL { const uploadP = limit(async () => { const car = await this.loader.carStore!.load(dbMeta.car).catch(() => null) if (!car) { - if (cidListIncludes(this.loader.carLog, dbMeta.car)) - throw new Error(`missing local car ${dbMeta.car.toString()}`) + if (cidListIncludes(this.loader.carLog, dbMeta.car)) { throw new Error(`missing local car ${dbMeta.car.toString()}`) } } else { await this.loader.remoteCarStore!.save(car) } @@ -95,8 +94,7 @@ export abstract class RemoteWAL { const uploadP = limit(async () => { const car = await this.loader.carStore!.load(dbMeta.car).catch(() => null) if (!car) { - if (cidListIncludes(this.loader.carLog, dbMeta.car)) - throw new Error(`missing local car ${dbMeta.car.toString()}`) + if (cidListIncludes(this.loader.carLog, dbMeta.car)) { throw new Error(`missing local car ${dbMeta.car.toString()}`) } } else { await this.loader.remoteCarStore!.save(car) } diff --git a/packages/encrypted-blockstore/src/store-memory.ts b/packages/encrypted-blockstore/src/store-memory.ts index a5e26d76..5181f8d6 100644 --- a/packages/encrypted-blockstore/src/store-memory.ts +++ b/packages/encrypted-blockstore/src/store-memory.ts @@ -6,10 +6,9 @@ import { RemoteWAL as RemoteWALBase, WALState } from './remote-wal' import type { Loadable, Loader } from './loader' -export const makeDataStore = (name: string) => new DataStore(name); -export const makeMetaStore = (loader: Loader) => new MetaStore(loader.name); -export const makeRemoteWAL = (loader: Loadable) => new RemoteWAL(loader); - +export const makeDataStore = (name: string) => new DataStore(name) +export const makeMetaStore = (loader: Loader) => new MetaStore(loader.name) +export const makeRemoteWAL = (loader: Loadable) => new RemoteWAL(loader) export class DataStore extends DataStoreBase { tag: string = 'car-mem' @@ -30,7 +29,6 @@ export class DataStore extends DataStoreBase { } } - export class MetaStore extends MetaStoreBase { tag: string = 'header-mem' store = new Map() @@ -66,7 +64,7 @@ export class MetaStore extends MetaStoreBase { } } -// +// export class RemoteWAL extends RemoteWALBase { tag: string = 'wal-mem' store = new Map() @@ -85,6 +83,7 @@ export class RemoteWAL extends RemoteWALBase { return null } } + // eslint-disable-next-line @typescript-eslint/require-await async save(state: WALState, branch = 'main'): Promise { try { diff --git a/packages/encrypted-blockstore/src/store-remote.ts b/packages/encrypted-blockstore/src/store-remote.ts index ad6e799a..9fe2e1fc 100644 --- a/packages/encrypted-blockstore/src/store-remote.ts +++ b/packages/encrypted-blockstore/src/store-remote.ts @@ -4,9 +4,9 @@ import type { AnyBlock, AnyLink, DbMeta } from './types' import { type Loadable, type Loader } from './loader' import { DataStore as DataStoreBase, - MetaStore as MetaStoreBase, + MetaStore as MetaStoreBase } from './store' -import { +import { RemoteWAL as RemoteWALBase, WALState } from './remote-wal' @@ -151,6 +151,7 @@ export class RemoteWAL extends RemoteWALBase { if (!bytesString) return null return parse(bytesString) } + async save(state: WALState, branch = 'main'): Promise { const encoded: ToString = format(state) this.store.set(this.headerKey(branch), encoded) diff --git a/packages/encrypted-blockstore/src/transaction.ts b/packages/encrypted-blockstore/src/transaction.ts index 671380f8..41a8d8af 100644 --- a/packages/encrypted-blockstore/src/transaction.ts +++ b/packages/encrypted-blockstore/src/transaction.ts @@ -2,11 +2,10 @@ import { MemoryBlockstore } from '@web3-storage/pail/block' // todo get these from multiformats? import { BlockFetcher as BlockFetcherAPI } from '@web3-storage/pail/api' -import { AnyAnyBlock, AnyAnyLink, AnyBlock, AnyLink, CarMakeable, DbMeta, TransactionMeta as TM } from './types' +import { AnyAnyBlock, AnyAnyLink, AnyBlock, AnyLink, CarMakeable, DbMeta, TransactionMeta as TM, CryptoOpts, StoreOpts } from './types' import { Loader } from './loader' import type { CID } from 'multiformats' -import { CryptoOpts, StoreOpts } from './types' export type BlockFetcher = BlockFetcherAPI // = { get: (link: AnyLink) => Promise } @@ -108,7 +107,7 @@ export class EncryptedBlockstore implements BlockFetcher { const blockLog = new CompactionFetcher(this) this.compacting = true const meta = await compactFn(blockLog) - await this.loader!.commit(blockLog.loggedBlocks, meta, { + await this.loader.commit(blockLog.loggedBlocks, meta, { compact: true, noLoader: true }) @@ -131,10 +130,10 @@ export class EncryptedBlockstore implements BlockFetcher { blocks.loggedBlocks.putSync(blk.cid, blk.bytes) } } - return this.lastTxMeta as TransactionMeta + return this.lastTxMeta } - async *entries(): AsyncIterableIterator { + async * entries(): AsyncIterableIterator { const seen: Set = new Set() if (this.loader) { for await (const blk of this.loader.entries()) { diff --git a/packages/encrypted-blockstore/src/types.ts b/packages/encrypted-blockstore/src/types.ts index 4527adb3..5864661d 100644 --- a/packages/encrypted-blockstore/src/types.ts +++ b/packages/encrypted-blockstore/src/types.ts @@ -1,7 +1,7 @@ import type { Link } from 'multiformats' -import { DataStore, MetaStore } from './store'; -import { RemoteWAL } from './remote-wal'; -import type { Loader } from './loader'; +import { DataStore, MetaStore } from './store' +import { RemoteWAL } from './remote-wal' +import type { Loader } from './loader' export type AnyLink = Link export type AnyAnyLink = Link @@ -70,4 +70,3 @@ export type DownloadMetaFnParams = { name: string branch: string } - diff --git a/packages/encrypted-blockstore/src/version.ts b/packages/encrypted-blockstore/src/version.ts index 68e03659..db194160 100644 --- a/packages/encrypted-blockstore/src/version.ts +++ b/packages/encrypted-blockstore/src/version.ts @@ -1 +1 @@ -export const PACKAGE_VERSION = "0.18.0"; +export const PACKAGE_VERSION = '0.18.0' diff --git a/packages/encrypted-blockstore/test/loader.test.js b/packages/encrypted-blockstore/test/loader.test.js index 3db17bca..05f395ea 100644 --- a/packages/encrypted-blockstore/test/loader.test.js +++ b/packages/encrypted-blockstore/test/loader.test.js @@ -54,7 +54,7 @@ describe('basic Loader', function () { const mockM = new MemoryBlockstore() mockM.transactions = new Set() t = new CarTransaction(mockM) - loader = new Loader('test-loader-commit', { ...loaderOpts, public: true }) + loader = new Loader('test-loader-commit', { ...loaderOpts, public: true }) block = await encode({ value: { hello: 'world' }, hasher, @@ -62,9 +62,11 @@ describe('basic Loader', function () { }) await t.put(block.cid, block.bytes) }) + it('should have an empty car log', function () { equals(loader.carLog.length, 0) }) + it('should commit', async function () { const carCid = await loader.commit(t, { head: [block.cid] }) equals(loader.carLog.length, 1) @@ -178,7 +180,7 @@ describe('basic Loader with index commits', function () { beforeEach(async function () { await resetDirectory(dataDir, 'test-loader-index') // t = new CarTransaction() - ib = new EncryptedBlockstore({...indexLoaderOpts, name: 'test-loader-index'}) + ib = new EncryptedBlockstore({ ...indexLoaderOpts, name: 'test-loader-index' }) block = await encode({ value: { hello: 'world' }, hasher, @@ -199,7 +201,7 @@ describe('basic Loader with index commits', function () { } indexMap = new Map() }) - + it('should start with an empty car log', function () { equals(ib.loader.carLog.length, 0) }) diff --git a/packages/encrypted-blockstore/test/transaction.test.js b/packages/encrypted-blockstore/test/transaction.test.js index 122b67ae..ece0236b 100644 --- a/packages/encrypted-blockstore/test/transaction.test.js +++ b/packages/encrypted-blockstore/test/transaction.test.js @@ -7,26 +7,31 @@ import * as nodeCrypto from '../dist/lib/crypto-node.js' import * as nodeStore from '../dist/lib/store-node.js' const loaderOpts = { - store : nodeStore, + store: nodeStore, crypto: nodeCrypto } describe('Fresh TransactionBlockstore', function () { /** @type {Blockstore} */ let blocks + beforeEach(function () { blocks = new Blockstore(loaderOpts) }) + it('should not have a name', function () { assert(!blocks.name) }) + it('should not have a loader', function () { assert(!blocks._loader) }) + it('should not put', async function () { const e = await blocks.put('key', 'value').catch(e => e) matches(e.message, /transaction/) }) + it('should yield a transaction', async function () { const txR = await blocks.transaction((tblocks) => { assert(tblocks) @@ -41,15 +46,19 @@ describe('Fresh TransactionBlockstore', function () { describe('TransactionBlockstore with name', function () { /** @type {Blockstore} */ let blocks + beforeEach(function () { - blocks = new Blockstore({name:'test', ...loaderOpts}) + blocks = new Blockstore({ name: 'test', ...loaderOpts }) }) + it('should have a name', function () { equals(blocks.name, 'test') }) + it('should have a loader', function () { assert(blocks.loader) }) + it('should get from loader', async function () { blocks.loader.getBlock = async (cid) => { return { cid, bytes: 'bytes' } @@ -62,11 +71,13 @@ describe('TransactionBlockstore with name', function () { describe('A transaction', function () { /** @type {CarTransaction} */ let tblocks, blocks + beforeEach(async function () { blocks = new Blockstore(loaderOpts) tblocks = new CarTransaction(blocks) blocks.transactions.add(tblocks) }) + it('should put and get', async function () { const cid = CID.parse('bafybeia4luuns6dgymy5kau5rm7r4qzrrzg6cglpzpogussprpy42cmcn4') @@ -96,10 +107,12 @@ describe('TransactionBlockstore with a completed transaction', function () { return await tblocks.put(cid2, 'value2') }) }) + it('should have transactions', async function () { const ts = blocks.transactions equals(ts.size, 2) }) + it('should get', async function () { const value = await blocks.get(cid) equals(value.cid, cid) @@ -108,6 +121,7 @@ describe('TransactionBlockstore with a completed transaction', function () { const value2 = await blocks.get(cid2) equals(value2.bytes, 'value2') }) + it('should yield entries', async function () { const blz = [] for await (const blk of blocks.entries()) { @@ -117,4 +131,4 @@ describe('TransactionBlockstore with a completed transaction', function () { }) }) -// test compact \ No newline at end of file +// test compact diff --git a/packages/react-native/README.md b/packages/react-native/README.md index 68065ecc..bc4b4d98 100644 --- a/packages/react-native/README.md +++ b/packages/react-native/README.md @@ -4,6 +4,6 @@ React Native bindings for Fireproof ## Installation -Add `@fireproof/react-native` dependency. We also need to add any native module dependencies so they autolink properly. +Add `@fireproof/react-native` dependency. We also need to add any native module dependencies so they autolink properly. pnpm add @fireproof/react-native react-native-quick-crypto diff --git a/packages/react-native/package.json b/packages/react-native/package.json index 324c23ff..2dfe6051 100644 --- a/packages/react-native/package.json +++ b/packages/react-native/package.json @@ -1,6 +1,6 @@ { "name": "@fireproof/react-native", - "version": "0.17.11", + "version": "0.18.0", "description": "Fireproof live database, JavaScript API and React Native hooks", "type": "module", "module": "src/index.js", @@ -79,4 +79,4 @@ "randombytes": "^2.1.0", "typescript": "^5.4.5" } -} \ No newline at end of file +} diff --git a/packages/react-native/src/index.tsx b/packages/react-native/src/index.tsx index 049b8ea0..45a69088 100644 --- a/packages/react-native/src/index.tsx +++ b/packages/react-native/src/index.tsx @@ -1,39 +1,27 @@ +import { StoreOpts } from '@fireproof/encrypted-blockstore'; import { type ConfigOpts, type Database, - useFireproof as useFireproofReact, + FireproofCtx, useDocument, + useFireproof as useFireproofReact, useLiveQuery, - FireproofCtx } from 'use-fireproof'; -import { StoreOpts } from '@fireproof/encrypted-blockstore' -import { - makeDataStore, - makeMetaStore, - makeRemoteWAL -} from './store-native' +import { makeDataStore, makeMetaStore, makeRemoteWAL } from './store-native'; const store = { makeDataStore, makeMetaStore, - makeRemoteWAL -} as unknown as StoreOpts + makeRemoteWAL, +} as unknown as StoreOpts; // override with a new 'useFireproof' for React Native -const useFireproof = ( - name?: string | Database | undefined, - config?: ConfigOpts | undefined -) => { +const useFireproof = (name?: string | Database | undefined, config?: ConfigOpts | undefined) => { return useFireproofReact(name, { ...config, store, - }) + }); }; -export { - useFireproof, - useDocument, - useLiveQuery, - FireproofCtx, -}; +export { FireproofCtx, useDocument, useFireproof, useLiveQuery }; diff --git a/packages/react-native/src/store-native.ts b/packages/react-native/src/store-native.ts index 7df95ee6..82974e1f 100644 --- a/packages/react-native/src/store-native.ts +++ b/packages/react-native/src/store-native.ts @@ -1,128 +1,127 @@ -/* eslint-disable import/first */ -import { format, parse, ToString } from '@ipld/dag-json' -import { MMKV } from 'react-native-mmkv' -import { AnyBlock, AnyLink, DbMeta } from '@fireproof/encrypted-blockstore/src/types' -import { DataStore as DataStoreBase, MetaStore as MetaStoreBase } from '@fireproof/encrypted-blockstore/src/store' -import { RemoteWAL as RemoteWALBase, WALState } from '@fireproof/encrypted-blockstore/src/remote-wal' -import type { Loadable, Loader } from '@fireproof/encrypted-blockstore/src/loader' - -export const makeDataStore = (name: string) => new DataStore(name) -export const makeMetaStore = (loader: Loader) => new MetaStore(loader.name) -export const makeRemoteWAL = (loader: Loadable) => new RemoteWAL(loader) +import type { Loadable, Loader } from '@fireproof/encrypted-blockstore/src/loader'; +import { RemoteWAL as RemoteWALBase, WALState } from '@fireproof/encrypted-blockstore/src/remote-wal'; +import { DataStore as DataStoreBase, MetaStore as MetaStoreBase } from '@fireproof/encrypted-blockstore/src/store'; +import { AnyBlock, AnyLink, DbMeta } from '@fireproof/encrypted-blockstore/src/types'; +import { format, parse, ToString } from '@ipld/dag-json'; +import { MMKV } from 'react-native-mmkv'; + +export const makeDataStore = (name: string) => new DataStore(name); +export const makeMetaStore = (loader: Loader) => new MetaStore(loader.name); +export const makeRemoteWAL = (loader: Loadable) => new RemoteWAL(loader); export class DataStore extends DataStoreBase { - tag: string = 'car-native-mmkv' - store: MMKV | null = null + tag: string = 'car-native-mmkv'; + store: MMKV | null = null; - async _withDB(dbWorkFun: (arg0: any) => any) { + async _withDB(dbWorkFun: (arg0: MMKV) => Promise) { if (!this.store) { - const dbName = `fp.${this.STORAGE_VERSION}.${this.name}` + const dbName = `fp.${this.STORAGE_VERSION}.${this.name}`; this.store = new MMKV({ id: dbName, - }) + }); } // eslint-disable-next-line @typescript-eslint/no-unsafe-return - return await dbWorkFun(this.store) + return await dbWorkFun(this.store); } async load(cid: AnyLink): Promise { - return await this._withDB(async (db: MMKV) => { - const bytes = db.getBuffer(cid.toString()) - if (!bytes) throw new Error(`missing db block ${cid.toString()}`) - return { cid, bytes } - }) + return (await this._withDB(async (db: MMKV) => { + const bytes = db.getBuffer(cid.toString()); + if (!bytes) throw new Error(`missing db block ${cid.toString()}`); + return { cid, bytes }; + })) as AnyBlock; } async save(car: AnyBlock): Promise { - return await this._withDB(async (db: MMKV) => { - db.set(car.cid.toString(), car.bytes) - }) + return (await this._withDB(async (db: MMKV) => { + db.set(car.cid.toString(), car.bytes); + })) as void; } async remove(cid: AnyLink): Promise { - return await this._withDB(async (db: MMKV) => { - db.delete(cid.toString()) - }) + return (await this._withDB(async (db: MMKV) => { + db.delete(cid.toString()); + })) as void; } } export class RemoteWAL extends RemoteWALBase { - tag: string = 'wal-native-mmkv' - store: MMKV | null = null + tag: string = 'wal-native-mmkv'; + store: MMKV | null = null; headerKey(branch: string) { // remove 'public' on next storage version bump - return `fp.${this.STORAGE_VERSION}.wal.${this.loader.name}.${branch}` + return `fp.${this.STORAGE_VERSION}.wal.${this.loader.name}.${branch}`; } - async _withDB(dbWorkFun: (arg0: any) => any) { + async _withDB(dbWorkFun: (arg0: MMKV) => Promise) { if (!this.store) { - const dbName = `fp.${this.STORAGE_VERSION}.wal` + const dbName = `fp.${this.STORAGE_VERSION}.wal`; this.store = new MMKV({ id: dbName, - }) + }); } // eslint-disable-next-line @typescript-eslint/no-unsafe-return - return await dbWorkFun(this.store) + return await dbWorkFun(this.store); } // eslint-disable-next-line @typescript-eslint/require-await async load(branch = 'main'): Promise { - return await this._withDB(async (db: MMKV) => { - const doc = db.getString(this.headerKey(branch)) - if (!doc) return null - return parse(doc) - }) + return (await this._withDB(async (db: MMKV) => { + const doc = db.getString(this.headerKey(branch)); + if (!doc) return null; + return parse(doc); + })) as WALState | null; } // eslint-disable-next-line @typescript-eslint/require-await async save(state: WALState, branch = 'main'): Promise { - return await this._withDB(async (db: MMKV) => { - const encoded: ToString = format(state) - db.set(this.headerKey(branch), encoded) - }) + return (await this._withDB(async (db: MMKV) => { + const encoded: ToString = format(state); + db.set(this.headerKey(branch), encoded); + })) as void; } } export class MetaStore extends MetaStoreBase { - tag: string = 'header-native-mmkv' - store: MMKV | null = null + tag: string = 'header-native-mmkv'; + store: MMKV | null = null; headerKey(branch: string) { - return `fp.${this.STORAGE_VERSION}.meta.${this.name}.${branch}` + return `fp.${this.STORAGE_VERSION}.meta.${this.name}.${branch}`; } - async _withDB(dbWorkFun: (arg0: any) => any) { + async _withDB(dbWorkFun: (arg0: MMKV) => Promise) { if (!this.store) { - const dbName = `fp.${this.STORAGE_VERSION}.meta` + const dbName = `fp.${this.STORAGE_VERSION}.meta`; this.store = new MMKV({ id: dbName, - }) + }); } // eslint-disable-next-line @typescript-eslint/no-unsafe-return - return await dbWorkFun(this.store) + return await dbWorkFun(this.store); } // eslint-disable-next-line @typescript-eslint/require-await async load(branch: string = 'main'): Promise { return await this._withDB(async (db: MMKV) => { - const doc = db.getString(this.headerKey(branch)) - if (!doc) return null + const doc = db.getString(this.headerKey(branch)); + if (!doc) return null; // TODO: react native wrt below? // browser assumes a single writer process // to support concurrent updates to the same database across multiple tabs // we need to implement the same kind of mvcc.crdt solution as in store-fs and connect-s3 - return [this.parseHeader(doc)] - }) + return [this.parseHeader(doc)]; + }); } // eslint-disable-next-line @typescript-eslint/require-await async save(meta: DbMeta, branch: string = 'main') { return await this._withDB(async (db: MMKV) => { - const headerKey = this.headerKey(branch) - const bytes = this.makeHeader(meta) - db.set(headerKey, bytes) - return null - }) + const headerKey = this.headerKey(branch); + const bytes = this.makeHeader(meta); + db.set(headerKey, bytes); + return null; + }); } } diff --git a/packages/react-native/tsconfig.json b/packages/react-native/tsconfig.json index 920f873c..18937ea5 100644 --- a/packages/react-native/tsconfig.json +++ b/packages/react-native/tsconfig.json @@ -16,8 +16,8 @@ "noUnusedLocals": true, "noUnusedParameters": true, "sourceMap": true, - "strict": true, + "strict": true }, "include": ["src"], - "exclude": ["coverage", "dist", "node_modules"], + "exclude": ["coverage", "dist", "node_modules"] } diff --git a/packages/react/package.json b/packages/react/package.json index 93cd89f6..c43a4ad2 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -76,4 +76,4 @@ "live", "sync" ] -} \ No newline at end of file +} diff --git a/packages/react/tsconfig.json b/packages/react/tsconfig.json index 920f873c..18937ea5 100644 --- a/packages/react/tsconfig.json +++ b/packages/react/tsconfig.json @@ -16,8 +16,8 @@ "noUnusedLocals": true, "noUnusedParameters": true, "sourceMap": true, - "strict": true, + "strict": true }, "include": ["src"], - "exclude": ["coverage", "dist", "node_modules"], + "exclude": ["coverage", "dist", "node_modules"] } diff --git a/packages/solid-js/tsconfig.json b/packages/solid-js/tsconfig.json index 54fc4a13..40bfd772 100644 --- a/packages/solid-js/tsconfig.json +++ b/packages/solid-js/tsconfig.json @@ -32,8 +32,8 @@ "noImplicitThis": true, "noImplicitAny": false, "strictNullChecks": true, - "esModuleInterop": true, + "esModuleInterop": true }, "include": ["src"], - "exclude": ["node_modules", "dist"], + "exclude": ["node_modules", "dist"] }