import type { CID, Link, Version } from "multiformats"; import { Attachable, Attached, CarTransaction, CRDT, DocFileMeta, Falsy, GatewayUrls, StoreType, SuperThis, TraceFn, KeyBagRuntime, KeyBagIf, CompactStrategy, KeysByFingerprint, KeyWithFingerPrint } from "@fireproof/core-types-base"; import { CoerceURI, CryptoRuntime, CTCryptoKey, Future, Logger, Result, URI, AppContext } from "@adviser/cement"; import { EventBlock } from "@web3-storage/pail/clock"; import { SerdeGateway, SerdeGatewayInterceptor } from "./serde-gateway.js"; import { AsyncBlockCodec } from "@fireproof/core-types-runtime"; import { TaskManagerIf, TaskManagerParams } from "./task-manager-if.js"; import { CommitQueueIf } from "./commit-queue-if.js"; export type AnyLink = Link; export type CarGroup = AnyLink[]; export type FroozenCarLog = CarGroup[]; export declare class CarLog { readonly _logs: CarGroup[]; get length(): number; last(): AnyLink[]; unshift(logs: CarGroup): void; update(logs: FroozenCarLog): void; asArray(): FroozenCarLog; } export type AnyAnyLink = Link; export type AnyLinkFn = (cid: AnyLink) => Promise; export interface AnyBlock { readonly cid: Link; readonly bytes: Uint8Array; } export interface CIDBlock { readonly cid: CID; readonly bytes: Uint8Array; } export declare function toCIDBlock(block: AnyBlock): CIDBlock; export interface AnyAnyBlock { readonly cid: AnyAnyLink; readonly bytes: Uint8Array; } export interface IvKeyIdData { readonly iv: Uint8Array; readonly keyId: Uint8Array; readonly data: Uint8Array; } export interface IvAndKeyAndBytes { readonly bytes: Uint8Array; readonly key: CTCryptoKey; readonly iv: Uint8Array; } export interface BytesAndKeyWithIv { readonly bytes: Uint8Array; readonly key: CTCryptoKey; readonly iv?: Uint8Array; } export interface AnyDecodedBlock { readonly cid: AnyLink; readonly bytes: Uint8Array; readonly value: Uint8Array; } export interface CarMakeable { entries(): AsyncIterable; get(cid: AnyLink): Promise; } export interface CarHeader { readonly cars: FroozenCarLog; readonly compact: FroozenCarLog; readonly meta: T; } export interface TransactionWrapper { meta: M; cars?: CarGroup; t: CarTransaction; } export type TransactionMeta = unknown; export interface EncryptedBlock { readonly value: IvAndKeyAndBytes; } export interface CodecOpts { readonly ivCalc: "random" | "hash"; readonly noIVVerify: boolean; } export interface KeyUpsertResultModified { readonly modified: true; readonly kfp: KeyWithFingerPrint; } export declare function isKeyUpsertResultModified(r: KeyUpsertResult): r is KeyUpsertResultModified; export interface KeyUpsertResultNotModified { readonly modified: false; } export type KeyUpsertResult = KeyUpsertResultModified | KeyUpsertResultNotModified; export interface CryptoAction { readonly ivLength: number; readonly logger: Logger; readonly crypto: CryptoRuntime; readonly url: URI; readonly key: KeysByFingerprint; algo(iv?: Uint8Array): { name: string; iv: Uint8Array; tagLength: number; }; codec(iv?: Uint8Array, codecOpts?: Partial): AsyncBlockCodec<24, Uint8Array, IvKeyIdData>; _decrypt(data: IvAndKeyAndBytes): Promise; _encrypt(data: BytesAndKeyWithIv): Promise; } export interface StoreFactory { encodeFile?: (blob: Blob) => Promise<{ cid: AnyLink; blocks: AnyBlock[]; }>; decodeFile?: (blocks: unknown, cid: AnyLink, meta: DocFileMeta) => Promise; } export interface StoreUrls { readonly meta: CoerceURI; readonly car: CoerceURI; readonly file: CoerceURI; readonly wal: CoerceURI; } export interface StoreEnDeFile { readonly encodeFile: (blob: Blob) => Promise<{ cid: AnyLink; blocks: AnyBlock[]; }>; readonly decodeFile: (blocks: unknown, cid: AnyLink, meta: DocFileMeta) => Promise>; } export interface StoreUrlsOpts { readonly base?: CoerceURI; readonly data?: Partial; readonly idx?: Partial; } export interface StoreURIs { readonly meta: URI; readonly car: URI; readonly file: URI; readonly wal: URI; } export interface StoreURIRuntime { readonly data: StoreURIs; readonly idx: StoreURIs; } export interface UrlAndInterceptor { readonly url: URI; readonly gatewayInterceptor?: SerdeGatewayInterceptor; } export interface StoreFactoryItem { readonly byStore: GatewayUrls; readonly loader: Loadable; } export interface StoreRuntime { makeStores(sfi: StoreFactoryItem): Promise; encodeFile(blob: Blob): Promise<{ cid: AnyLink; blocks: AnyBlock[]; }>; decodeFile(blocks: unknown, cid: AnyLink, meta: DocFileMeta): Promise>; } export interface CommitOpts { readonly noLoader?: boolean; readonly compact?: boolean; } export interface WriteableDataAndMetaStore { file: FileStore; car: CarStore; meta: MetaStore; } export type DataAndMetaStore = Readonly; export interface WriteableDataAndMetaAndWalStore extends WriteableDataAndMetaStore { wal?: WALStore; } export type DataAndMetaAndWalStore = Readonly; export type LocalDataAndMetaAndWalStore = Readonly> & { readonly wal: WALStore; }; export interface DbMeta { readonly cars: CarGroup; } export type LoadHandler = (dbMetas: DbMeta[]) => Promise; export interface RefLoadable { readonly loader: Loadable; } export interface RefBlockstore { readonly blockstore: RefLoadable; } export interface Connection { loaded(): Future; readonly context: AppContext; connectStorage(ref: RefLoadable | RefBlockstore): void; } export interface BaseStore { readonly storeType: StoreType; readonly realGateway: SerdeGateway; readonly logger: Logger; url(): URI; id(): string; keyedCrypto(): Promise; close(): Promise>; destroy(): Promise>; readonly ready?: () => Promise; start(dam: DataAndMetaStore): Promise>; } export interface DbMetaEvent { readonly eventCid: CarClockLink; readonly parents: CarClockHead; readonly dbMeta: DbMeta; } export declare function DbMetaEventEqual(a: DbMetaEvent, b: DbMetaEvent): boolean; export declare function DbMetaEventsEqual(a: DbMetaEvent[], b: DbMetaEvent[]): boolean; export interface MetaStore extends BaseStore { readonly storeType: "meta"; stream(branch?: string): ReadableStream; save(meta: DbMeta, branch?: string): Promise>; } export interface DataSaveOpts { readonly public: boolean; } export interface CarStore extends BaseStore { readonly storeType: "car"; load(cid: AnyLink): Promise; save(car: AnyBlock, opts?: DataSaveOpts): Promise; remove(cid: AnyLink): Promise>; } export interface FileStore extends BaseStore { readonly storeType: "file"; load(cid: AnyLink): Promise; save(car: AnyBlock, opts?: DataSaveOpts): Promise; remove(cid: AnyLink): Promise>; } export interface WALState { readonly operations: DbMeta[]; readonly noLoaderOps: DbMeta[]; readonly fileOperations: { readonly cid: AnyLink; readonly public: boolean; }[]; } export interface WALStore extends BaseStore { readonly storeType: "wal"; ready(): Promise; readonly processing?: Promise | undefined; readonly processQueue: CommitQueueIf; process(): Promise; enqueue(dbMeta: DbMeta, opts: CommitOpts): Promise; enqueueFile(fileCid: AnyLink): Promise; load(): Promise; save(state: WALState): Promise; } export type CompactFetcher = BlockFetcher & { readonly loggedBlocks: CarTransaction; }; export interface StoreRuntimeUrls { readonly meta: URI; readonly data: URI; readonly wal: URI; } export interface BlockstoreParams { readonly logger: Logger; readonly applyMeta: (meta: TransactionMeta, snap?: boolean) => Promise; readonly compactStrategy: CompactStrategy; readonly autoCompact: number; readonly crypto: CryptoRuntime; readonly public: boolean; readonly meta: DbMeta; readonly threshold: number; readonly gatewayInterceptor?: SerdeGatewayInterceptor; readonly storeEnDeFile: Partial; readonly keyBag: KeyBagRuntime; readonly storeUrls: StoreURIs; readonly storeRuntime: StoreRuntime; readonly taskManager: TaskManagerParams; } export type BlockstoreOpts = Partial & { readonly keyBag: KeyBagRuntime; readonly storeUrls: StoreURIs; readonly storeRuntime: StoreRuntime; readonly tracer: TraceFn; }; export interface BlockstoreRuntime { readonly logger: Logger; readonly applyMeta: (meta: TransactionMeta, snap?: boolean) => Promise; readonly compactStrategy: CompactStrategy; readonly autoCompact: number; readonly crypto: CryptoRuntime; readonly storeRuntime: StoreRuntime; readonly keyBag: KeyBagRuntime; readonly storeUrls: StoreURIs; readonly gatewayInterceptor?: SerdeGatewayInterceptor; readonly taskManager: TaskManagerParams; readonly meta?: DbMeta; readonly threshold: number; } export type LocalActiveStore = Omit & { readonly active: LocalDataAndMetaAndWalStore; }; export interface AttachedStores { local(): LocalActiveStore; forRemotes(actionFn: (store: ActiveStore) => Promise): Promise; remotes(): ActiveStore[]; activate(store: DataAndMetaStore | CoerceURI): ActiveStore; attach(attached: Attachable, onAttach: (at: Attached) => Promise): Promise; detach(): Promise; } export interface BaseAttachedStores { local(): BaseStore; remotes(): BaseStore[]; } export interface CarAttachedStores extends BaseAttachedStores { local(): CarStore; remotes(): CarStore[]; } export declare abstract class BaseActiveStore { abstract readonly ref: ActiveStore; abstract readonly active: BaseStore; abstract local(): BaseStore; abstract remotes(): BaseStore[]; protected abstract readonly attached: BaseAttachedStores; } export interface FileAttachedStores extends BaseAttachedStores { local(): FileStore; remotes(): FileStore[]; } export declare abstract class CarActiveStore extends BaseActiveStore { protected abstract readonly attached: CarAttachedStores; abstract local(): CarStore; abstract remotes(): CarStore[]; } export declare abstract class FileActiveStore extends BaseActiveStore { protected abstract readonly attached: FileAttachedStores; abstract local(): FileStore; abstract remotes(): FileStore[]; } export type CIDActiveStore = CarActiveStore | FileActiveStore; export interface MetaAttachedStores extends BaseAttachedStores { local(): MetaStore; remotes(): MetaStore[]; } export declare abstract class MetaActiveStore extends BaseActiveStore { protected abstract readonly attached: MetaAttachedStores; abstract local(): MetaStore; abstract remotes(): MetaStore[]; } export interface WALAttachedStores extends BaseAttachedStores { local(): WALStore; remotes(): WALStore[]; } export declare abstract class WALActiveStore extends BaseActiveStore { protected abstract readonly attached: WALAttachedStores; abstract local(): WALStore; abstract remotes(): WALStore[]; } export interface ActiveStore { readonly active: DataAndMetaAndWalStore; baseStores(): BaseStore[]; carStore(): CarActiveStore; fileStore(): FileActiveStore; metaStore(): MetaActiveStore; walStore(): WALActiveStore; local(): LocalActiveStore; remotes(): ActiveStore[]; readonly attached: AttachedStores; } export interface Loadable { readonly sthis: SuperThis; readonly logger: Logger; readonly blockstoreParent?: BlockFetcher; readonly ebOpts: BlockstoreRuntime; readonly carLog: CarLog; readonly attachedStores: AttachedStores; readonly commitQueue: CommitQueueIf; attach(attached: Attachable): Promise; readonly taskManager: TaskManagerIf; ready(): Promise; close(): Promise; keyBag(): Promise; handleDbMetasFromStore(metas: DbMeta[], store: ActiveStore): Promise; commit(t: CarTransaction, done: T, opts: CommitOpts): Promise; destroy(): Promise; getBlock(cid: AnyLink, store: ActiveStore): Promise; loadFileCar(cid: AnyLink, store: ActiveStore): Promise>; loadCar(cid: AnyLink, store: ActiveStore): Promise>; commitFiles(t: CarTransaction, done: TransactionMeta): Promise; entries(cache?: boolean): AsyncIterableIterator; } export interface DbMetaBinary { readonly dbMeta: Uint8Array; } export type DbMetaEventBlock = EventBlock; export type CarClockLink = Link; export type CarClockHead = CarClockLink[]; export interface BlockItemBase { readonly type: string; readonly status: "ready" | "stale"; readonly value: T; } export declare function isDocBlockItem(item: BlockItem): item is DocBlockItem; interface DocBlockItemValue { readonly _id: string; readonly value: unknown; } export interface DocBlockItem extends BlockItemBase { readonly type: "doc"; readonly value: DocBlockItemValue; } export declare function isFPBlockItem(fpb: FPBlock): fpb is FPBlock>; interface FPBlockItemValue { readonly fp: CarHeader; } export interface FPBlockItem extends BlockItemBase> { readonly type: "fp"; readonly value: FPBlockItemValue; } export declare function isDataBlockItem(item: BlockItem): item is DataBlockItem; interface DataBlockItemValue { readonly data: { readonly ops: { readonly key: string; readonly type: "put" | "delete"; readonly value: CID; }[]; readonly root: CID; readonly type: "batch" | "single"; }; readonly parents: CID[]; } export interface DataBlockItem extends BlockItemBase { readonly type: "data"; readonly value: DataBlockItemValue; } interface DelBlockItemValue { readonly del: boolean; } export interface DelBlockItem extends BlockItemBase { readonly type: "del"; readonly value: DelBlockItemValue; } export interface LeafBlockItemValue { readonly leaf: [string, string[]][]; readonly closed: boolean; } export interface LeafBlockItem extends BlockItemBase { readonly type: "leaf"; readonly value: LeafBlockItemValue; } export interface BranchBlockItemValue { branch: [number, [string[], CID], [string[], CID][]]; closed: boolean; } export interface BranchBlockItem extends BlockItemBase { readonly type: "branch"; readonly value: BranchBlockItemValue; } export interface UnknownBlockItem extends BlockItemBase { readonly type: "unknown"; readonly value: unknown; } export declare function isEntriesBlockItem(item: BlockItem): item is EntriesBlockItem; interface EntriesBlockItemValue { readonly entries: { readonly key: string; readonly value: CID[]; }[]; } export interface EntriesBlockItem extends BlockItemBase { readonly type: "entries"; readonly value: EntriesBlockItemValue; } export interface StaleCarBlockItem extends BlockItemBase { readonly type: "car"; readonly status: "stale"; readonly statusCause: Error; readonly origin: string; } interface ReadyCarBlockItemValue { readonly car: { readonly blocks: FPBlock[]; readonly roots: CID[]; }; } export interface ReadyCarBlockItem extends BlockItemBase { readonly type: "car"; readonly status: "ready"; readonly origin: string; readonly value: ReadyCarBlockItemValue; } export type CarBlockItem = StaleCarBlockItem | ReadyCarBlockItem; export declare function isBlockReady(ifp: unknown): ifp is FPBlock; export declare function isBlockNotReady(ifp: unknown): ifp is FPBlock; export declare function isCarBlockItemReady(ifp: unknown): ifp is FPBlock; export declare function isCarBlockItemStale(ifp: unknown): ifp is FPBlock; export type BlockItem = UnknownBlockItem | DocBlockItem | DataBlockItem | EntriesBlockItem | FPBlockItem | CarBlockItem | DelBlockItem | LeafBlockItem | BranchBlockItem; export interface FPBlock extends AnyBlock { readonly cid: AnyLink; readonly bytes: Uint8Array; readonly item: T; } export interface BlockFetcher { readonly crdtParent?: CRDT; get(link: AnyLink): Promise; } export {};