import type { CoValueHeader, Transaction } from "../coValueCore/verifiedState.js"; import { Signature } from "../crypto/crypto.js"; import type { CoValueCore, RawCoID, SessionID } from "../exports.js"; import { NewContentMessage } from "../sync.js"; import type { PeerID } from "../sync.js"; import { CoValueKnownState } from "../knownState.js"; import { StorageStreamingQueue } from "../queue/StorageStreamingQueue.js"; export type CorrectionCallback = (correction: CoValueKnownState) => NewContentMessage[] | undefined; export type StorageReconciliationAcquireResult = { acquired: true; lastProcessedOffset: number; } | { acquired: false; reason: "not_due" | "lock_held"; }; /** * Deletion work queue status for `deletedCoValues` (SQLite). * * Stored as an INTEGER in SQLite: * - 0 = pending * - 1 = done */ export declare enum DeletedCoValueDeletionStatus { Pending = 0, Done = 1 } /** * The StorageAPI is the interface that the StorageSync and StorageAsync classes implement. * * It uses callbacks instead of promises to have no overhead when using the StorageSync and less overhead when using the StorageAsync. */ export interface StorageAPI { /** * Flags that the coValue delete is valid. * * When the delete tx is stored, the storage will mark the coValue as deleted. */ markDeleteAsValid(id: RawCoID): void; /** * Enable the background erasure scheduler that drains the `deletedCoValues` work queue. * This is intentionally opt-in and should be activated by `LocalNode`. */ enableDeletedCoValuesErasure(): void; /** * Batch physical deletion for coValues queued in `deletedCoValues` with status `Pending`. * Must preserve tombstones (header + delete session(s) + their tx/signatures). */ eraseAllDeletedCoValues(): Promise; load(id: string, callback: (data: NewContentMessage) => void, done?: (found: boolean) => void): void; /** * Stores the content. `done` is invoked only after the content is durably * stored (including any correction round-trip). It is NOT invoked when the * write fails or is dropped — callers rely on that to detect content that * was sent to peers but never persisted. Depending on the implementation, * done may fire synchronously before store() returns (sync storage) or * later (async queue). */ store(data: NewContentMessage, handleCorrection: CorrectionCallback, done?: () => void): void; streamingQueue?: StorageStreamingQueue; getKnownState(id: string): CoValueKnownState; waitForSync(id: string, coValue: CoValueCore): Promise; /** * Track multiple sync status updates. * Does not guarantee the updates will be applied in order, so only one * update per CoValue ID + Peer ID combination should be tracked at a time. */ trackCoValuesSyncState(updates: { id: RawCoID; peerId: PeerID; synced: boolean; }[], done?: () => void): void; /** * Get all CoValue IDs that have at least one unsynced peer. */ getUnsyncedCoValueIDs(callback: (unsyncedCoValueIDs: RawCoID[]) => void): void; /** * Stop tracking sync status for a CoValue (remove all peer entries). */ stopTrackingSyncState(id: RawCoID): void; /** * Get a batch of CoValue IDs from storage. * Used for full storage reconciliation. Call repeatedly with increasing offset * until the returned batch has length < limit (or 0) to enumerate all IDs. * @param limit - Max number of IDs to return (e.g. 100). * @param offset - Number of IDs to skip (0 for first batch). * @param callback - Called with the batch. Ordering must be stable (e.g. by id). */ getCoValueIDs(limit: number, offset: number, callback: (batch: { id: RawCoID; }[]) => void): void; /** * Get the total number of CoValues in storage. */ getCoValueCount(callback: (count: number) => void): void; /** * Try to acquire the storage reconciliation lock for a given peer. * Atomically checks if reconciliation is due for this peer (lastRun older than 30 days or missing) * and if no other process/tab holds the lock for this peer, then acquires it. */ tryAcquireStorageReconciliationLock(sessionId: SessionID, peerId: PeerID, callback: (result: StorageReconciliationAcquireResult) => void): void; /** * Update the last processed offset for the storage reconciliation lock held for this peer. * Only call after a batch has been acked; used to resume from this offset on interrupt. */ renewStorageReconciliationLock(sessionId: SessionID, peerId: PeerID, offset: number): void; /** * Release the storage reconciliation lock for a peer and record completion. Only call on successful completion. * On failure/interrupt, do not call; the lock expires after LOCK_TTL_MS and another process can retry for this peer. */ releaseStorageReconciliationLock(sessionId: SessionID, peerId: PeerID): void; /** * Load only the knownState (header presence + session counters) for a CoValue. * This is more efficient than load() when we only need to check if a peer needs new content. * * @param id - The CoValue ID * @param callback - Called with the knownState, or undefined if CoValue not found */ loadKnownState(id: string, callback: (knownState: CoValueKnownState | undefined) => void): void; /** * Called when a CoValue is unmounted from memory. * Used to clean up the metadata associated with that CoValue. */ onCoValueUnmounted(id: RawCoID): void; close(): Promise | undefined; } export type CoValueRow = { id: RawCoID; header: CoValueHeader; }; export type StoredCoValueRow = CoValueRow & { rowID: number; }; export type SessionRow = { coValue: number; sessionID: SessionID; lastIdx: number; lastSignature: Signature; bytesSinceLastSignature?: number; }; export type StoredSessionRow = SessionRow & { rowID: number; }; export type TransactionRow = { ses: number; idx: number; tx: Transaction; }; export type SignatureAfterRow = { ses: number; idx: number; signature: Signature; }; export type StorageReconciliationLockRow = { key: string; holderSessionId: SessionID; acquiredAt: number; releasedAt?: number; /** Offset up to which all batches have been acked; used to resume after interrupt. */ lastProcessedOffset: number; }; export interface DBTransactionInterfaceAsync { getSingleCoValueSession(coValueRowId: number, sessionID: SessionID): Promise; /** * Persist a "deleted coValue" marker in storage (work queue entry). * This is an enqueue signal: implementations should set status to `Pending`. * This is expected to be idempotent (safe to call repeatedly). */ markCoValueAsDeleted(id: RawCoID): Promise; addSessionUpdate({ sessionUpdate, sessionRow, }: { sessionUpdate: SessionRow; sessionRow?: StoredSessionRow; }): Promise; addTransaction(sessionRowID: number, idx: number, newTransaction: Transaction): Promise | undefined | unknown; addSignatureAfter({ sessionRowID, idx, signature, }: { sessionRowID: number; idx: number; signature: Signature; }): Promise; deleteCoValueContent(coValueRow: Pick): Promise; getStorageReconciliationLock(key: string): Promise; putStorageReconciliationLock(entry: StorageReconciliationLockRow): Promise; } export interface DBClientInterfaceAsync { getCoValue(coValueId: string): Promise | undefined; upsertCoValue(id: string, header?: CoValueHeader): Promise; /** * Enumerate all coValue IDs currently pending in the "deleted coValues" work queue. */ getAllCoValuesWaitingForDelete(): Promise; getCoValueSessions(coValueRowId: number): Promise; getNewTransactionInSession(sessionRowId: number, fromIdx: number, toIdx: number): Promise; getSignatures(sessionRowId: number, firstNewTxIdx: number): Promise; transaction(callback: (tx: DBTransactionInterfaceAsync) => Promise): Promise; trackCoValuesSyncState(updates: { id: RawCoID; peerId: PeerID; synced: boolean; }[]): Promise; getUnsyncedCoValueIDs(): Promise; stopTrackingSyncState(id: RawCoID): Promise; /** * Physical deletion primitive: erase all persisted history for a deleted coValue, * while preserving the tombstone (header + delete session(s)). * Must run inside a single storage transaction. */ eraseCoValueButKeepTombstone(coValueID: RawCoID): Promise; /** * Get the knownState for a CoValue without loading transactions. * Returns undefined if the CoValue doesn't exist. */ getCoValueKnownState(coValueId: string): Promise; getCoValueIDs(limit: number, offset: number): Promise<{ id: RawCoID; }[]>; getCoValueCount(): Promise; tryAcquireStorageReconciliationLock(sessionId: SessionID, peerId: PeerID): Promise; renewStorageReconciliationLock(sessionId: SessionID, peerId: PeerID, offset: number): Promise; releaseStorageReconciliationLock(sessionId: SessionID, peerId: PeerID): Promise; } export interface DBTransactionInterfaceSync { getSingleCoValueSession(coValueRowId: number, sessionID: SessionID): StoredSessionRow | undefined; /** * Persist a "deleted coValue" marker in storage (work queue entry). * This is an enqueue signal: implementations should set status to `"pending"`. * This is expected to be idempotent (safe to call repeatedly). */ markCoValueAsDeleted(id: RawCoID): unknown; addSessionUpdate({ sessionUpdate, sessionRow, }: { sessionUpdate: SessionRow; sessionRow?: StoredSessionRow; }): number; addTransaction(sessionRowID: number, idx: number, newTransaction: Transaction): number | undefined | unknown; addSignatureAfter({ sessionRowID, idx, signature, }: { sessionRowID: number; idx: number; signature: Signature; }): number | undefined | unknown; getStorageReconciliationLock(key: string): StorageReconciliationLockRow | undefined; putStorageReconciliationLock(entry: StorageReconciliationLockRow): void; } export interface DBClientInterfaceSync { getCoValue(coValueId: string): StoredCoValueRow | undefined; upsertCoValue(id: string, header?: CoValueHeader): number | undefined; /** * Enumerate all coValue IDs currently pending in the "deleted coValues" work queue. */ getAllCoValuesWaitingForDelete(): RawCoID[]; getCoValueSessions(coValueRowId: number): StoredSessionRow[]; getNewTransactionInSession(sessionRowId: number, fromIdx: number, toIdx: number): TransactionRow[]; getSignatures(sessionRowId: number, firstNewTxIdx: number): Pick[]; transaction(callback: (tx: DBTransactionInterfaceSync) => unknown): unknown; trackCoValuesSyncState(updates: { id: RawCoID; peerId: PeerID; synced: boolean; }[]): void; getUnsyncedCoValueIDs(): RawCoID[]; stopTrackingSyncState(id: RawCoID): void; /** * Physical deletion primitive: erase all persisted history for a deleted coValue, * while preserving the tombstone (header + delete session(s)). * Must run inside a single storage transaction. */ eraseCoValueButKeepTombstone(coValueID: RawCoID): unknown; /** * Get the knownState for a CoValue without loading transactions. * Returns undefined if the CoValue doesn't exist. */ getCoValueKnownState(coValueId: string): CoValueKnownState | undefined; getCoValueIDs(limit: number, offset: number): { id: RawCoID; }[]; getCoValueCount(): number; tryAcquireStorageReconciliationLock(sessionId: SessionID, peerId: PeerID): StorageReconciliationAcquireResult; renewStorageReconciliationLock(sessionId: SessionID, peerId: PeerID, offset: number): void; releaseStorageReconciliationLock(sessionId: SessionID, peerId: PeerID): void; } //# sourceMappingURL=types.d.ts.map