Skip to content

Commit

Permalink
Make CI happy
Browse files Browse the repository at this point in the history
  • Loading branch information
boorad committed May 14, 2024
1 parent 24bad99 commit 05793e1
Show file tree
Hide file tree
Showing 21 changed files with 150 additions and 152 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 11 additions & 10 deletions packages/encrypted-blockstore/src/connection.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -35,6 +35,7 @@ export abstract class Connection {
params: UploadDataFnParams,
opts?: { public?: boolean }
): Promise<void | AnyLink>

abstract metaDownload(params: DownloadMetaFnParams): Promise<Uint8Array[] | null>
abstract dataDownload(params: DownloadDataFnParams): Promise<Uint8Array | null>

Expand All @@ -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()
})
})
Expand All @@ -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<DbMetaEventBlock> {
Expand Down
6 changes: 3 additions & 3 deletions packages/encrypted-blockstore/src/encrypt-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 }
Expand Down
4 changes: 2 additions & 2 deletions packages/encrypted-blockstore/src/index.ts
Original file line number Diff line number Diff line change
@@ -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<Uint8Array[] | null>
Expand Down
30 changes: 14 additions & 16 deletions packages/encrypted-blockstore/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -170,6 +167,7 @@ export class Loader implements Loadable {
): Promise<AnyLink> {
return this.commitQueue.enqueue(() => this._commitInternalFiles(t, done, opts))
}

// can these skip the queue? or have a file queue?
async _commitInternalFiles(
t: CarTransaction,
Expand All @@ -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
}

Expand Down Expand Up @@ -224,13 +222,13 @@ export class Loader implements Loadable {
opts: CommitOpts = { noLoader: false, compact: false }
): Promise<AnyLink> {
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
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -294,7 +292,7 @@ export class Loader implements Loadable {
// }
// }

async *entries(cache = true): AsyncIterableIterator<AnyBlock> {
async * entries(cache = true): AsyncIterableIterator<AnyBlock> {
await this.ready
if (cache) {
for (const [, block] of this.getBlockCache) {
Expand Down
6 changes: 2 additions & 4 deletions packages/encrypted-blockstore/src/remote-wal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
11 changes: 5 additions & 6 deletions packages/encrypted-blockstore/src/store-memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -30,7 +29,6 @@ export class DataStore extends DataStoreBase {
}
}


export class MetaStore extends MetaStoreBase {
tag: string = 'header-mem'
store = new Map<string, string>()
Expand Down Expand Up @@ -66,7 +64,7 @@ export class MetaStore extends MetaStoreBase {
}
}

//
//
export class RemoteWAL extends RemoteWALBase {
tag: string = 'wal-mem'
store = new Map<string, string>()
Expand All @@ -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<void> {
try {
Expand Down
5 changes: 3 additions & 2 deletions packages/encrypted-blockstore/src/store-remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -151,6 +151,7 @@ export class RemoteWAL extends RemoteWALBase {
if (!bytesString) return null
return parse<WALState>(bytesString)
}

async save(state: WALState, branch = 'main'): Promise<void> {
const encoded: ToString<WALState> = format(state)
this.store.set(this.headerKey(branch), encoded)
Expand Down
9 changes: 4 additions & 5 deletions packages/encrypted-blockstore/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AnyBlock | undefined> }
Expand Down Expand Up @@ -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
})
Expand All @@ -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<AnyBlock> {
async * entries(): AsyncIterableIterator<AnyBlock> {
const seen: Set<string> = new Set()
if (this.loader) {
for await (const blk of this.loader.entries()) {
Expand Down
7 changes: 3 additions & 4 deletions packages/encrypted-blockstore/src/types.ts
Original file line number Diff line number Diff line change
@@ -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<any, number, number, 1 | 0>
export type AnyAnyLink = Link<any, any, any, any>
Expand Down Expand Up @@ -70,4 +70,3 @@ export type DownloadMetaFnParams = {
name: string
branch: string
}

2 changes: 1 addition & 1 deletion packages/encrypted-blockstore/src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const PACKAGE_VERSION = "0.18.0";
export const PACKAGE_VERSION = '0.18.0'
8 changes: 5 additions & 3 deletions packages/encrypted-blockstore/test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,19 @@ 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,
codec
})
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)
Expand Down Expand Up @@ -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,
Expand All @@ -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)
})
Expand Down
Loading

0 comments on commit 05793e1

Please sign in to comment.