import type { EventLink } from "@web3-storage/pail/clock/api"; import type { Operation } from "@web3-storage/pail/crdt/api"; import type { Block } from "multiformats"; import { EnvFactoryOpts, Env, Logger, CryptoRuntime, Result, CoerceURI, AppContext, URI, CTCryptoKey } from "@adviser/cement"; import type { DbMeta, AnyLink, StoreUrlsOpts, StoreEnDeFile, Loadable, TransactionWrapper, BlockstoreRuntime, StoreURIRuntime, DataAndMetaAndWalStore, UrlAndInterceptor, MetaStore, WALStore, BaseStore, FileStore, CarStore, FPBlock, BlockFetcher } from "@fireproof/core-types-blockstore"; import type { IndexIf } from "./indexer.js"; import { SerdeGatewayInterceptor } from "@fireproof/core-types-blockstore"; import { KeysItem, V2StorageKeyItem } from "./keybag-storage.zod.js"; export declare class NotFoundError extends Error { readonly code = "ENOENT"; } export type { DbMeta }; export type Falsy = false | null | undefined; export declare function isFalsy(value: unknown): value is Falsy; export declare const PARAM: { SUFFIX: string; URL_GEN: string; STORE_KEY: string; STORE: string; KEY: string; INDEX: string; NAME: string; VERSION: string; RUNTIME: string; FRAG_SIZE: string; IV_VERIFY: string; IV_HASH: string; FRAG_FID: string; FRAG_OFS: string; FRAG_LEN: string; FRAG_HEAD: string; EXTRACTKEY: string; SELF_REFLECT: string; CAR_PARALLEL: string; CAR_CACHE_SIZE: string; CAR_COMPACT_CACHE_SIZE: string; CAR_META_CACHE_SIZE: string; GENESIS_CID: string; LOCAL_NAME: string; }; export type PARAMS = (typeof PARAM)[keyof typeof PARAM]; export declare function throwFalsy(value: T | Falsy): T; export declare function falsyToUndef(value: T | Falsy): T | undefined; export type StoreType = "car" | "file" | "wal" | "meta"; export interface FPStats { isFile(): boolean; isDirectory(): boolean; isBlockDevice(): boolean; isCharacterDevice(): boolean; isSymbolicLink(): boolean; isFIFO(): boolean; isSocket(): boolean; uid: number | Falsy; gid: number | Falsy; size: number | Falsy; atime: Date | Falsy; mtime: Date | Falsy; ctime: Date | Falsy; birthtime: Date | Falsy; } export interface SysFileSystem { start(): Promise; mkdir(path: string, options?: { recursive: boolean; }): Promise; readdir(path: string): Promise; rm(path: string, options?: { recursive: boolean; }): Promise; copyFile(source: string, destination: string): Promise; readfile(path: string): Promise; stat(path: string): Promise; unlink(path: string): Promise; writefile(path: string, data: Uint8Array | string): Promise; } export interface PathOps { join(...args: string[]): string; dirname(path: string): string; basename(path: string): string; } export type ToUInt8 = Uint8Array | Result; export type PromiseToUInt8 = ToUInt8 | Promise | Promise>; export interface BaseXXEndeCoder { encode(input: string | ToUInt8): string; decodeUint8(input: string): Uint8Array; decode(input: string): string; } export interface TextEndeCoder { id(): string; encode(input: string): Uint8Array; decode(input: ToUInt8): string; readonly base64: BaseXXEndeCoder; readonly base58: BaseXXEndeCoder; } export interface TextEndeCodable { txt: TextEndeCoder; } export interface SuperThisOpts { readonly logger: Logger; readonly pathOps: PathOps; readonly crypto: CryptoRuntime; readonly env: Partial; readonly txt: TextEndeCoder; readonly ctx: AppContext; } export interface SuperThis { readonly logger: Logger; readonly loggerCollector?: Logger; readonly env: Env; readonly pathOps: PathOps; readonly ctx: AppContext; readonly txt: TextEndeCoder; timeOrderedNextId(time?: number): { str: string; toString: () => string; }; nextId(bytes?: number): { str: string; bin: Uint8Array; toString: () => string; }; start(): Promise; clone(override: Partial): SuperThis; } export interface IdleEventFromCommitQueue { readonly event: "idleFromCommitQueue"; } export interface IdleEventFromBlockstore { readonly event: "idleFromBlockstore"; readonly blockstore: "data" | "index"; readonly ledger?: Ledger; } export interface BusyEventFromCommitQueue { readonly event: "busyFromCommitQueue"; readonly queueLen: number; } export interface BusyEventFromBlockstore extends Omit { readonly event: "busyFromBlockstore"; readonly queueLen: number; } export declare function EventIsIdleFromBlockstore(event: TraceEvent): event is IdleEventFromBlockstore; export declare function EventIsBusyFromBlockstore(event: TraceEvent): event is BusyEventFromBlockstore; export type TraceEvent = IdleEventFromCommitQueue | IdleEventFromBlockstore | BusyEventFromBlockstore | BusyEventFromCommitQueue; export interface ConfigOpts extends Partial { readonly public?: boolean; readonly meta?: DbMeta; readonly writeQueue?: Partial; readonly gatewayInterceptor?: SerdeGatewayInterceptor; readonly autoCompact?: number; readonly compactStrategy?: string; readonly storeUrls?: StoreUrlsOpts; readonly storeEnDe?: StoreEnDeFile; readonly threshold?: number; readonly keyBag?: Partial; readonly tracer?: TraceFn; } export type ClockLink = EventLink; export type ClockHead = ClockLink[]; export type DocFragment = Uint8Array | string | number | boolean | null | AnyLink | DocFragment[] | object; export type DocLiteral = string | number | boolean | Uint8Array | unknown; export type DocObject = NonNullable; export type DocTypes = DocObject; export type DocRecord = T; export type UnknownDoc = DocRecord; export type DocFiles = Record; export interface DocBase { readonly _id: string; readonly _files?: DocFiles; readonly _publicFiles?: DocFiles; readonly _deleted?: boolean; } export type DocWithId = DocBase & T; export type DocSet = Partial & T; export interface DocFileMeta { readonly type: string; readonly size: number; readonly cid: AnyLink; readonly car?: AnyLink; readonly lastModified?: number; url?: string; file?: () => Promise; } export interface DocUpdate { readonly id: string; readonly value?: DocSet; readonly del?: boolean; readonly clock?: ClockLink; } export interface DocValue { readonly doc: DocWithId; readonly del: boolean; readonly cid: AnyLink; } export type KeyLiteral = string | number | boolean; export type IndexKeyType = KeyLiteral | KeyLiteral[]; export type IndexKey = [K, string]; export interface IndexUpdate { readonly key: IndexKey; readonly value?: DocFragment; readonly del?: boolean; } export interface IndexUpdateString { readonly key: string; readonly value?: DocFragment; readonly del?: boolean; } export interface FPIndexRow { readonly id: string; readonly key: K; readonly value: R; readonly doc?: DocWithId; } export interface IndexRowsWithoutDocs { readonly rows: FPIndexRow[]; } export interface IndexRowsWithDocs extends IndexRowsWithoutDocs { readonly docs: DocWithId[]; } export type IndexRows = IndexRowsWithoutDocs | IndexRowsWithDocs; export interface CRDTMeta { readonly head: ClockHead; } export interface IndexTransactionMeta { readonly indexes: Record; } export interface FileTransactionMeta { readonly files?: AnyLink[]; } export type MetaType = CRDTMeta | IndexTransactionMeta | FileTransactionMeta; export interface IdxMeta { readonly byId: AnyLink; readonly byKey: AnyLink; readonly map: string; readonly name: string; readonly head: ClockHead; } export interface IdxMetaMap { readonly indexes?: Map; } export interface QueryOpts { readonly descending: boolean; readonly limit: number; readonly includeDocs: boolean; readonly range: [IndexKeyType, IndexKeyType]; readonly key: DocFragment; readonly keys: DocFragment[]; readonly prefix: IndexKeyType; } export interface AllDocsQueryOpts extends Omit, "includeDocs"> { readonly key: string; readonly keys: string[]; readonly prefix: string; readonly includeDeleted: boolean; } export interface AllDocsResponse { readonly rows: { readonly key: string; readonly value: DocWithId; }[]; readonly clock: ClockHead; readonly name?: string; } type EmitFn = (k: IndexKeyType, v?: DocFragment) => void; export type MapFn = (doc: DocWithId, emit: EmitFn) => DocFragment | unknown; export interface ChangesOptions { readonly dirty?: boolean; readonly limit?: number; } export interface ChangesResponseRow { readonly key: string; readonly value: DocWithId; readonly clock?: ClockLink; } export interface ChangesResponse { readonly clock: ClockHead; readonly rows: ChangesResponseRow[]; readonly name?: string; } export interface DocResponse { readonly id: string; readonly clock: ClockHead; readonly name?: string; } export interface BulkResponse { readonly ids: string[]; readonly clock: ClockHead; readonly name?: string; } export type UpdateListenerFn = (docs: DocWithId[]) => Promise | void; export type NoUpdateListenerFn = () => Promise | void; export type ListenerFn = UpdateListenerFn | NoUpdateListenerFn; export interface CRDTEntry { readonly data: string; readonly parents: string[]; readonly cid: string; } export type AsyncVoidFn = () => Promise; export type VoidFn = () => void; export type UnReg = () => void; export interface CRDTClock { readonly head: ClockHead; readonly watchers: Map[]) => void>; readonly noPayloadWatchers: Map; onTock(fn: VoidFn): UnReg; onTick(fn: (updates: DocUpdate[]) => void): UnReg; applyHead(newHead: ClockHead, prevHead: ClockHead, updates?: DocUpdate[]): Promise; onZoom(fn: VoidFn): UnReg; close(): Promise; ready(): Promise; } export interface CarTransaction { readonly parent: BaseBlockstore; get(cid: AnyLink): Promise; superGet(cid: AnyLink): Promise; unshift(fb: FPBlock): void; putSync(fb: FPBlock): void; put(fb: FPBlock): Promise; entries(): AsyncIterableIterator; } export interface CarTransactionOpts { readonly add: boolean; readonly noLoader: boolean; } export interface BaseBlockstore { readonly crdtParent?: CRDT; readonly transactions: Set; readonly sthis: SuperThis; readonly loader: Loadable; readonly ebOpts: BlockstoreRuntime; ready(): Promise; close(): Promise; destroy(): Promise; compact(): Promise; readonly logger: Logger; get(cid: AnyLink): Promise; put(fp: FPBlock): Promise; transaction(fn: (t: CarTransaction) => Promise, _opts?: CarTransactionOpts): Promise>; openTransaction(opts: CarTransactionOpts): CarTransaction; commitTransaction(t: CarTransaction, done: M, opts: CarTransactionOpts): Promise>; entries(): AsyncIterableIterator; } export interface CRDT extends ReadyCloseDestroy, HasLogger, HasSuperThis, HasCRDT { readonly ledgerParent?: Ledger; readonly logger: Logger; readonly sthis: SuperThis; readonly crdt: CRDT; readonly clock: CRDTClock; readonly blockstore: BaseBlockstore; readonly indexBlockstore?: BaseBlockstore; readonly indexers: Map>; bulk(updates: DocUpdate[]): Promise; ready(): Promise; close(): Promise; destroy(): Promise; allDocs(): Promise<{ result: DocUpdate[]; head: ClockHead; }>; vis(): Promise; getBlock(cidString: string): Promise; get(key: string): Promise | Falsy>; changes(since?: ClockHead, opts?: ChangesOptions): Promise<{ result: DocUpdate[]; head: ClockHead; }>; compact(): Promise; } export interface HasCRDT { readonly crdt: CRDT; } export interface RefLedger { readonly ledger: Ledger; } export interface HasLogger { readonly logger: Logger; } export interface HasSuperThis { readonly sthis: SuperThis; } export interface ReadyCloseDestroy { close(): Promise; destroy(): Promise; ready(): Promise; } export interface CoerceURIandInterceptor { readonly url: CoerceURI; readonly gatewayInterceptor?: SerdeGatewayInterceptor; } export interface AttachContext { detach(): Promise; readonly ctx: AppContext; } export interface GatewayUrlsParam extends Partial { readonly car: CoerceURIandInterceptor; readonly file: CoerceURIandInterceptor; readonly meta: CoerceURIandInterceptor; readonly wal?: CoerceURIandInterceptor; } export interface GatewayUrls { readonly car: UrlAndInterceptor; readonly file: UrlAndInterceptor; readonly meta: UrlAndInterceptor; readonly wal?: UrlAndInterceptor; } export interface Attachable { readonly name: string; prepare(db?: Ledger): Promise; configHash(db?: Ledger): string; } export declare class DataAndMetaAndWalAndBaseStore implements DataAndMetaAndWalStore { readonly wal?: WALStore | undefined; readonly file: FileStore; readonly car: CarStore; readonly meta: MetaStore; readonly baseStores: BaseStore[]; constructor(dam: DataAndMetaAndWalStore); } export interface Attached { readonly keyed: string; readonly gatewayUrls: GatewayUrls; readonly stores: DataAndMetaAndWalAndBaseStore; detach(): Promise; status(): "attached" | "loading" | "loaded" | "error" | "detached" | "syncing" | "idle"; ctx(): AppContext; } export interface Database extends ReadyCloseDestroy, HasLogger, HasSuperThis { readonly ledger: Ledger; readonly logger: Logger; readonly sthis: SuperThis; readonly name: string; onClosed(fn: () => void): void; attach(a: Attachable): Promise; get(id: string): Promise>; put(doc: DocSet): Promise; bulk(docs: DocSet[]): Promise; del(id: string): Promise; remove(id: string): Promise; changes(since?: ClockHead, opts?: ChangesOptions): Promise>; allDocs(opts?: Partial): Promise>; allDocuments(opts?: Partial): Promise>; subscribe(listener: ListenerFn, updates?: boolean): () => void; query> = Partial>>(field: string | MapFn, opts?: O): Promise>; compact(): Promise; } export type QueryResult> = Partial>> = O extends { includeDocs: false; } ? IndexRowsWithoutDocs : IndexRowsWithDocs; export interface WriteQueue, S extends DocTypes = DocTypes> { push(task: T): Promise; bulk(tasks: T[]): Promise; close(): Promise; } export type TraceFn = (traceEvent: TraceEvent) => void; export interface Tracer { readonly tracer: TraceFn; } export type TransactionMeta = unknown; export interface CompactStrategyContext extends BlockFetcher { readonly transactions: Set; readonly clock?: CRDTClock; readonly lastTxMeta: TransactionMeta; readonly loader: Loadable; readonly logger: Logger; readonly blockstore: BlockFetcher; readonly loggedBlocks: CarTransaction; get(cid: AnyLink): Promise; } export interface CompactStrategy { readonly name: string; compact(block: CompactStrategyContext): Promise; } export interface LedgerOpts extends Tracer { readonly name: string; readonly meta?: DbMeta; readonly gatewayInterceptor?: SerdeGatewayInterceptor; readonly compactStrategy?: string; readonly ctx: AppContext; readonly writeQueue: WriteQueueParams; readonly storeUrls: StoreURIRuntime; readonly storeEnDe: StoreEnDeFile; readonly keyBag: KeyBagRuntime; } export type LedgerOptsOptionalTracer = Omit & Partial; export interface Ledger extends HasCRDT { readonly opts: LedgerOpts; readonly writeQueue: WriteQueue>; readonly logger: Logger; readonly sthis: SuperThis; readonly name: string; readonly ctx: AppContext; refId(): string; onClosed(fn: () => void): () => void; attach(a: Attachable): Promise; close(): Promise; destroy(): Promise; ready(): Promise; subscribe(listener: ListenerFn, updates?: boolean): () => void; } export interface KeyMaterial { readonly key: Uint8Array; readonly keyStr: string; } export interface KeyWithFingerPrint { readonly default: boolean; readonly fingerPrint: string; readonly key: CTCryptoKey; extract(): Promise; asKeysItem(): Promise; } 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 UpSertOpts { readonly def?: boolean; readonly modified?: boolean; readonly doNotWrite?: boolean; } export interface KeysByFingerprint { readonly id: string; readonly name: string; get(fingerPrint?: string | Uint8Array): Promise; upsert(key: string | Uint8Array, opts?: UpSertOpts): Promise>; asV2StorageKeyItem(): Promise; } export interface KeyBagProvider { get(id: string): Promise | undefined>; set(id: string, item: NonNullable): Promise; del(id: string): Promise; } export interface KeyBagRuntime { readonly url: URI; readonly crypto: CryptoRuntime; readonly logger: Logger; readonly sthis: SuperThis; readonly keyLength: number; getBagProvider(): Promise; id(): string; } export interface KeyBagOpts { readonly url: CoerceURI; readonly crypto: CryptoRuntime; readonly keyLength: number; readonly keyRuntime: KeyBagRuntime; } export interface WriteQueueParams { readonly chunkSize: number; } export declare function defaultWriteQueueOpts(opts?: Partial): WriteQueueParams; export type Readonly2Writeable = { -readonly [P in keyof T]: T[P]; }; export declare function isNotFoundError(e: Error | Result | unknown): e is NotFoundError;