import { a as Storage, h as SerializedResourceTransactionEvent, d as StorageStartOptions, T as Trace, S as Selector, e as CreateResourceOptions, I as InstanceFor, M as MetadataFor, f as DatabaseMetadata, N as NamespaceMetadata } from '../shared/hive.DlaRxYsk.js'; import { RowObject, RowValues, Transaction, TransactionResult } from '../sdk/transaction.js'; import { ByteStream } from '../streams/index.js'; import { W as WebSocketClient, P as Protocol } from '../shared/hive.C0j2V6KY.js'; import '../sdk/logger.js'; import 'zod'; //#region src/index.d.ts type ReplicationStorageConfig = { /** * Address to reach the API of the remote `Server` instance at. * * The `ReplicationStorage` will automatically append the API version to the * configured address. * * @example * ```ts * new ReplicationStorage({ * remote: 'http://localhost:3000/api', * // => 'http://localhost:3000/api/v1' * }); * ``` * * @example * ```ts * new ReplicationStorage({ * remote: 'https://api.acme.co', * // => 'https://api.acme.co/v1' * }); * ``` */ remote: string; /** * Token to use for authentication. * * @default null */ token?: string | null | undefined; /** * Path to the directory in which to persist state. * * Either an absolute path or a path relative to the current * working directory. */ dir: string; }; declare class ReplicationStorage extends Storage { static readonly name = "replication-storage"; private readonly remote; private readonly token; private readonly dir; wsc: WebSocketClient; private driver; private events; private logger; private onError; private readonly pending; /** * Buffer of inflight transactions received while a Resource is being * replicated. A key exists in this Map only while buffering inflight * transactions for that Resource; after draining, the entry is removed. */ inflight: Map; /** * ApiClient instance to use for interacting with the remote `Hive` * instance through its REST API. */ private readonly api; /** * Promise tracking the cleanup operation when connection closes. */ private cleanup; /** * Map containing references and metadata of all open Resources. */ private readonly open; constructor(config: ReplicationStorageConfig); /** * Start the ReplicationStorage instance. * * Flow: * - Validates required dependencies (Driver, EventEmitter) * - Establishes WebSocket connection to remote * - Sets up disconnect handler for cleanup * - Sets up message handler for events * - Emits `StorageStartEvent` * * @param opts Configuration options * * @returns Promise resolving the `ReplicationStorage` instance once started. */ start(opts: Omit & { driver?: StorageStartOptions['driver'] | null | undefined; logger?: StorageStartOptions['logger'] | null | undefined; onError?: StorageStartOptions['onError'] | null | undefined; trace?: Trace | null | undefined; emit?: boolean | null | undefined; }): Promise; /** * Stop the ReplicationStorage instance. * * Flow: * - Disconnects from WebSocket * - Frees all currently open Resources * - Emits `StorageStopEvent` * * @param opts Configuration options * * @returns Promise resolving the `ReplicationStorage` instance once stopped. */ stop(opts?: { trace?: Trace | null | undefined; emit?: boolean | null | undefined; }): Promise; /** * Create a new Resource on the remote and replicate it locally. * * Flow: * - Sends HTTP POST to create Resource remotely * - Calls `get` to replicate the created Resource * - Emits `StorageCreateEvent` * * For Databases/Snapshots/Branches: * - Accepts `ByteStream`, `Uint8Array`, or `Selector` as contents * * For Namespaces: * - Does not accept any contents * * @param selector Selector of the Resource to create. * @param opts Creation options including contents. * * @returns Promise resolving the created Resource and its metadata. */ create(selector: S, opts?: CreateResourceOptions): Promise<{ resource: InstanceFor; metadata: MetadataFor; }>; /** * List Resources. * * Flow: * - Fetches list from remote via HTTP * - Emits `StorageListEvent` * * @param opts List options including optional parent filter. * * @returns Promise resolving the Metadata for the listed Resources. */ list(opts?: { parent?: Selector<'namespace' | 'database'> | null | undefined; trace?: Trace | null | undefined; emit?: boolean | null | undefined; }): Promise>; /** * Check if a Resource exists. * * Flow (Resource open): * - Resolves `true` immediately * - Emits `StorageHasEvent` * * Flow (Resource not open): * - Checks existence on remote via HTTP * - Emits `StorageHasEvent` * * @param selector Selector of the Resource to check. * @param opts Options for checking the Resource. * * @returns Promise resolving a boolean indicating whether the Resource exists. */ has(selector: Selector, opts?: { trace?: Trace | null | undefined; emit?: boolean | null | undefined; }): Promise; /** * Get a Resource, replicating it locally if not already open. * * Flow (already open): * - Returns open Resource and metadata immediately * - Emits `StorageGetEvent` * * Flow (not open - Databases/Snapshots/Branches): * - Creates inflight Transactions buffer * - Subscribes to Resource channel * - Fetches contents snapshot from remote via HTTP * - Writes contents to disk * - Opens connection through Driver * - Applies buffered Transactions received while replicating * - Adds to open map * - Emits `StorageGetEvent` * * Flow (not open - Namespaces): * - Subscribes to Resource channel * - Fetches metadata from remote via HTTP * - Adds to open map (no connection) * - Emits `StorageGetEvent` * * Buffer handling: * - Buffer cleared if `setContents`/`delete` occurs during replication * - Replication aborted and error thrown if buffer was cleared * * @param selector Selector of the Resource to get. * @param opts Options for getting the Resource. * * @returns Promise resolving the Resource and its metadata. */ get(selector: S, opts?: { trace?: Trace | null | undefined; emit?: boolean | null | undefined; }): Promise<{ resource: InstanceFor; metadata: MetadataFor; }>; /** * Get the metadata of a Resource. * * Flow (Resource open): * - Returns cached metadata immediately * - Emits `ResourceGetMetadataEvent` * * Flow (Resource not open): * - Fetches metadata from remote via HTTP * - Returns the metadata * - Emits `ResourceGetMetadataEvent` * * @param selector Selector of the Resource to get the metadata of. * @param opts Options for getting the metadata of the Resource. * * @returns Promise resolving the metadata of the Resource. */ getMetadata(selector: S, opts?: { trace?: Trace | null | undefined; emit?: boolean | null | undefined; }): Promise>; /** * Get the contents of a Resource. * * Flow (Resource open): * - Gets contents from the replicated Resource * - Returns immediately with the cached metadata * - Emits `ResourceGetContentsEvent` * * Flow (Resource not open): * - Invokes `get` to replicate the Resource * - Gets contents from the replicated Resource * - Emits `ResourceGetContentsEvent` * * @param selector Selector of the Resource to get the contents of. * @param opts Options for getting the contents of the Resource. * * @returns Promise resolving the contents and metadata of the Resource. */ getContents(selector: Selector<'database' | 'snapshot' | 'branch'>, opts?: { trace?: Trace | null | undefined; emit?: boolean | null | undefined; }): Promise<{ contents: ByteStream; metadata: DatabaseMetadata; }>; /** * Update the contents of a Resource. * * Flow (Resource not open): * - Sends HTTP PUT to update contents remotely * - Resolves immediately (no WebSocket broadcast expected) * - Emits `ResourceSetContentsEvent` * * Flow (Resource open): * - Sends HTTP PUT to update contents remotely * - Promise remains pending (waits for WebSocket broadcast) * - WS handler receives broadcast: * - Closes the connection * - Removes from open map * - Deletes directory * - Replicates Resource fresh by invoking `get` * - Resolves pending promise * - Emits `ResourceSetContentsEvent` * * Event emission: * - Non-open: Emitted in `setContents` method * - Open: Emitted in WebSocket handler (prevents double emission) * * @param selector Selector of the Resource to update the contents of. * @param contents New contents as `ByteStream`, `Uint8Array`, or `Selector`. * @param opts Options for updating the contents of the Resource. * * @returns Promise resolving when the contents of the Resource have been updated. */ setContents(selector: Selector<'database' | 'snapshot' | 'branch'>, contents: ByteStream | Uint8Array | Selector<'database' | 'snapshot' | 'branch'>, opts?: { trace?: Trace | null | undefined; emit?: boolean | null | undefined; }): Promise; /** * Execute a Transaction on a Resource. * * Flow (read-only query on open Resource): * - Executes directly on locally replicated Resource * - Returns results immediately * - Emits `ResourceTransactionEvent` * * Flow (write query on open Resource): * - Sends query via WebSocket request * - Promise remains pending (waits for WebSocket broadcast) * - WS handler receives broadcast: * - Applies transaction to local connection * - Updates the `modifiedAt` timestamp of the Resource * - Resolves pending promise with results * - Emits `ResourceTransactionEvent` * * Flow (query on non-open resource): * - Sends query via WebSocket request * - Resolves immediately from response (no broadcast sent) * - Emits `ResourceTransactionEvent` * - Kicks off background replication for future queries * * Event emission: * - Read-only local: Emitted in `query` method * - Non-open: Emitted in `query` method * - Write on open: Emitted in WebSocket handler * * Query ordering: * - WebSocket FIFO guarantees proper ordering * - Pending map keyed by trace (allows concurrent queries) * * @param selector Selector of the Resource to query. * @param transaction Transaction to execute. * @param opts Options for querying the Resource. * * @returns Promise resolving the results of the Transaction. */ query = ReadonlyArray>(selector: Selector<'database' | 'snapshot' | 'branch'>, transaction: Transaction, opts?: { trace?: Trace | null | undefined; emit?: boolean | null | undefined; }): Promise>; /** * Free a Resource, closing its connection and cleaning up the local state. * * Flow (Resource not open): * - Returns immediately (nothing to free) * - Emits `StorageFreeEvent` * * Flow (Resource open): * - Waits for all pending Operations to complete * - Closes the connection * - Removes from open map * - Deletes replicated state * - Clears inflight buffer * - Leaves channel * - Emits `StorageFreeEvent` * * Pending operations awaited: * - `query`, `get`, `create`, `getContents`, `setContents`, `getMetadata` * - Errors from pending operations are ignored * * @param selector Selector of the Resource to free. * @param opts Options for freeing the Resource. * * @returns Promise resolving when the Resource has been freed. */ free(selector: Selector, opts?: { trace?: Trace | null | undefined; emit?: boolean | null | undefined; }): Promise; /** * Delete a Resource from the remote and clean up the local state. * * Flow (Resource not open): * - Sends HTTP DELETE to remote * - Resolves immediately (no WebSocket broadcast expected) * - Emits `StorageDeleteEvent` * * Flow (Resource open): * - Sends HTTP DELETE to remote * - Promise remains pending (waits for WebSocket broadcast) * - WS handler receives broadcast: * - Closes connection * - Removes from open map * - Rejects all pending operations for this Resource * - Deletes replicated state * - Clears inflight buffer * - Leaves channel * - Resolves pending Promise * - Emits `StorageDeleteEvent` * * Event emission: * - Non-open: Emitted in `delete` method * - Open: Emitted in WebSocket handler * * Pending operations rejected: * - `query`, `get`, `create`, `getContents`, `setContents`, `getMetadata`, `free` * - Rejected with ResourceNotFoundError * * @param selector Selector of the Resource to delete. * @param opts Options for deleting the Resource. * * @returns Promise resolving when the Resource has been deleted. */ delete(selector: Selector, opts?: { trace?: Trace | null | undefined; emit?: boolean | null | undefined; }): Promise; } export { ReplicationStorage }; export type { ReplicationStorageConfig };