import { S3ClientConfig } from './s3-client.js'; import { a as Storage, D as Database, i as Namespace, C as Connection, S as Selector, T as Trace, d as StorageStartOptions, 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 '../sdk/logger.js'; import 'zod'; //#region src/lib/index.d.ts type S3StorageIndexDatabaseMetadata = DatabaseMetadata & { scheduledAt: Date | null; synchronizedAt: Date | null; }; type S3StorageIndexNamespaceMetadata = NamespaceMetadata & { scheduledAt: Date | null; synchronizedAt: Date | null; }; //#endregion //#region src/index.d.ts type S3StorageConfig = { /** * S3 configuration. */ s3: S3ClientConfig; /** * Directory to use for caching Resource contents on the local filesystem. * * Either an absolute path or a path relative to the current * working directory. */ dir: string; /** * Whether to enable seamless handover of existing state to the next instance. * * Must be explicitly enabled since if configured incorrectly, this can lead * to data loss. * * If enabled, the `S3Storage` instance will keep accepting Transactions and * other operations while it is being stopped. * * This serves the purpose of seamlessly rotating services without causing * downtime. It must be ensured that both processes have the read and write * permission to the configured directory. * * @default `false` */ handover?: boolean | null | undefined; /** * Interval in milliseconds at which open Resources shall be synchronized * with S3. * * @default `900_000` // 15 * 60 * 1000, equal to 15 minutes. */ syncInterval?: number | null | undefined; }; declare class S3Storage extends Storage { static readonly name = "s3-storage"; /** * Directory to use for caching Resource contents on the local filesystem. */ private readonly dir; /** * `S3Client` instance to use for interacting with the S3 API. */ private readonly client; /** * Whether to enable seamless handover of existing state to the next instance. */ private readonly handover; /** * Driver instance wrapping a runtime-specific implementation of SQLite. */ private driver; /** * EventEmitter instance to use for emitting events. */ private events; /** * Logger instance to use for logging. */ private logger; /** * Error handler to invoke with any errors which might occur during * background tasks. */ private onError; /** * Interval in milliseconds at which open Resources shall be synchronized * with S3. */ private readonly syncInterval; /** * Index instance to use for managing the index of Resources. */ private index; /** * Map containing references to all currently open Resources. */ readonly open: Map; /** * Reference to the Promise of a running start operation. */ private starting; /** * Reference to the Promise of a running stop operation. */ private stopping; /** * Reference to the Promise of a running index synchronization operation. */ private indexSynchronize; /** * Map containing a set of pending operations for each Resource. */ pending: { synchronize: Map>; has: Map>; create: Map>; get: Map>; getMetadata: Map>; getContents: Map>>; setContents: Map>>; query: Map>; free: Map>; delete: Map>; }; /** * Map keeping track of all Resource which currently have synchronizations * scheduled for them. */ private readonly scheduled; constructor(config: S3StorageConfig); /** * Synchronize a specific Resource that has been modified since its last * synchronization with S3. * * If the specified Resource has not been modified since the last * synchronization, synchronization will be skipped. * * @param selector Selector of the Resource to synchronize. * @param opts Options for synchronizing the Resource. * * @returns Promise resolving when the Resource has been synchronized. */ synchronizeResource( /** * Selector of the Resource to synchronize. */ selector: Selector, /** * Options for synchronizing the Resource. */ opts?: { /** * Whether to force synchronization even if the Resource is not scheduled * for synchronization. * * @default `false` */ force?: boolean | null | undefined; /** * Trace to associate the operation with. * * @default `null` */ trace?: Trace | null | undefined; }): Promise; /** * Schedule the synchronization of a specific Resource. * * No-op if the Resource is already scheduled for synchronization. * * @param selector Selector of the Resource to schedule the synchronization of. * @param opts Options for scheduling the synchronization of the Resource. * * @returns Promise resolving when the Resource has been scheduled for synchronization. */ scheduleResource( /** * Selector of the Resource to schedule the synchronization of. */ selector: Selector, /** * Options for scheduling the synchronization of the Resource. */ opts?: { /** * Trace to associate the operation with. * * @default `null` */ trace?: Trace | null | undefined; }): Promise; /** * Synchronize the state of the Index with S3. * * @param opts Options for synchronizing the Index. * * @returns Promise resolving when the Index has been synchronized. */ synchronizeIndex(opts?: { /** * Trace to associate the operation with. * * @default `null` */ trace?: Trace | null | undefined; /** * Whether to emit an event when the Index has been synchronized. * * @default `true` */ emit?: boolean | null | undefined; }): Promise; /** * Start the `S3Storage` instance. * * @param opts Options for starting the `S3Storage` instance. * * @returns Promise resolving with the `S3Storage` instance once it * has finished starting. * * @throws `Error` if an error prevents the `S3Storage` from starting. * * @emits `StorageStartEvent` when the `S3Storage` instance has started. */ start(opts: Omit & { logger?: StorageStartOptions['logger'] | null | undefined; onError?: StorageStartOptions['onError'] | null | undefined; /** * Trace to associate the operation with. * * @default `null` */ trace?: Trace | null | undefined; /** * Whether to emit a `StorageStartEvent` when the `S3Storage` instance * has started. * * @default `true` */ emit?: boolean | null | undefined; }): Promise; /** * Stop the `S3Storage` instance. * * @param opts Options for stopping the `S3Storage` instance. * * @returns Promise resolving with the `S3Storage` instance once it * has finished stopping. * * @throws `Error` if an error prevents the `S3Storage` from stopping. * * @emits `StorageStopEvent` when the `S3Storage` instance has stopped. */ stop(opts?: { /** * Trace to associate the operation with. * * @default `null` */ trace?: Trace | null | undefined; /** * Whether to emit a `StorageStopEvent` when the `S3Storage` instance * has stopped. * * @default `true` */ emit?: boolean | null | undefined; }): Promise; /** * Create a new Resource. * * @param selector Selector of the Resource to create. * @param opts Options for creating the Resource. * * @returns Promise resolving with the created Resource. * * @throws `ResourceNotFoundError` if the parent Resource does not exist. * @throws `ResourceExistsError` if the Resource already exists. * * @emits `StorageCreateEvent` when the Resource has been created. */ create( /** * Selector of the Resource to create. */ selector: S, opts?: CreateResourceOptions): Promise<{ resource: InstanceFor; metadata: MetadataFor & { scheduledAt: Date | null; synchronizedAt: Date | null; }; }>; /** * List all Resources. * * @param opts Options for listing the Resources. * * @returns Promise resolving an Array containing the Metadata for the * listed Resources. * * @throws `ResourceNotFoundError` if the parent Resource does not exist. * * @emits `StorageListEvent` when the Resources have been listed. */ list(opts?: { /** * Selector of the parent Resource to list all child Resources of. * * @default `null` */ parent?: Selector<'namespace' | 'database'> | null | undefined; /** * Trace to associate the operation with. * * @default `null` */ trace?: Trace | null | undefined; /** * Whether to emit a `StorageListEvent` when the Resources have been * listed. * * @default `true` */ emit?: boolean | null | undefined; /** * Whether to use the Index to list the Resources. * * @default `true` */ index?: boolean | null | undefined; }): Promise>; /** * Check if a specific Resource exists. * * @param selector Selector of the Resource to check. * @param opts Options for checking the Resource. * * @returns Promise resolving with a boolean indicating whether the Resource * exists. * * @emits `StorageHasEvent` when the Resource has been checked. */ has( /** * Selector of the Resource to check. */ selector: Selector, /** * Options for checking the Resource. */ opts?: { /** * Optional Trace to associate the operation with. * * @default `null` */ trace?: Trace | null | undefined; /** * Whether to emit a `StorageHasEvent` when the Resource has been * checked. * * @default `true` */ emit?: boolean | null | undefined; /** * Whether to use the Index to check if the Resource exists. * * @default `true` */ index?: boolean | null | undefined; }): Promise; /** * Get a specific Resource. * * @param selector Selector of the Resource to get. * * @param opts Options for getting the Resource. * * @returns Promise resolving with the requested Resource and its metadata. * * @throws `ResourceNotFoundError` if the Resource was not found. * * @emits `StorageGetEvent` when the Resource has been retrieved. */ get( /** * Selector of the Resource to get. */ selector: Selector, /** * Options for getting the Resource. */ opts?: { /** * Trace to associate the operation with. * * @default `null` */ trace?: Trace | null | undefined; /** * Whether to emit a `StorageGetEvent` when the Resource has been * retrieved. * * @default `true` */ emit?: boolean | null | undefined; }): Promise<{ resource: InstanceFor; metadata: MetadataFor & { scheduledAt: Date | null; synchronizedAt: Date | null; }; }>; /** * Retrieve the metadata of a specific Resource. * * @param selector Selector of the Resource to retrieve the metadata of. * @param opts Options for retrieving the metadata of the Resource. * * @returns Promise resolving with the metadata of the Resource. * * @throws `ResourceNotFoundError` if the Resource was not found. * * @emits `ResourceGetMetadataEvent` when the metadata of the Resource has * been retrieved. */ getMetadata( /** * Selector of the Resource to retrieve the metadata of. */ selector: S, /** * Options for retrieving the metadata of the Resource. */ opts?: { /** * Trace to associate the operation with. * * @default `null` */ trace?: Trace | null | undefined; /** * Whether to emit a `ResourceGetMetadataEvent` when the metadata of the * Resource has been retrieved. * * @default `true` */ emit?: boolean | null | undefined; }): Promise & { scheduledAt: Date | null; synchronizedAt: Date | null; }>; /** * Get the contents of a specific Resource. * * @param selector Selector of the Resource to get the contents of. * @param opts Options for getting the contents of the Resource. * * @returns Promise resolving with the contents of the Resource. * * @throws `ResourceNotFoundError` if the Resource was not found. * * @emits `ResourceGetContentsEvent` when the contents of the Resource have been retrieved. */ getContents( /** * Selector of the Resource to get the contents of. */ selector: Selector<'database' | 'snapshot' | 'branch'>, /** * Options for getting the contents of the Resource. */ opts?: { /** * Trace to associate the operation with. * * @default `null` */ trace?: Trace | null | undefined; /** * Whether to emit a `ResourceGetContentsEvent` when the contents of the * Resource have been retrieved. * * @default `true` */ emit?: boolean | null | undefined; }): Promise<{ contents: ByteStream; metadata: S3StorageIndexDatabaseMetadata; }>; /** * Set the contents of a specific Resource. * * @param selector Selector of the Resource to set the contents of. * @param source Contents to set. * @param opts Options for setting the contents of the Resource. * * @returns Promise resolving when the contents of the Resource have been set. * * @throws `ResourceNotFoundError` if the Resource was not found. * * @emits `ResourceSetContentsEvent` when the contents of the Resource have been set. */ setContents( /** * Selector of the Resource to set the contents of. */ selector: Selector<'database' | 'snapshot' | 'branch'>, /** * Contents to set. */ source: ByteStream | Uint8Array | Selector<'database' | 'snapshot' | 'branch'>, /** * Options for setting the contents of the Resource. */ opts?: { /** * Trace to associate the operation with. * * @default `null` */ trace?: Trace | null | undefined; /** * Whether to emit a `ResourceSetContentsEvent` when the contents of the * Resource have been set. * * @default `true` */ emit?: boolean | null | undefined; }): Promise; /** * Apply a Transaction to a specific Resource. * * @param selector Selector of the Resource to apply the Transaction to. * @param transaction Transaction to apply. * @param opts Options for applying the Transaction. * * @returns Promise resolving with the results of the Transaction. * * @throws `ResourceNotFoundError` if the Resource was not found. * * @emits `ResourceTransactionEvent` when the Transaction has been applied. * @returns */ query = ReadonlyArray>( /** * Selector of the Resource to apply the Transaction to. */ selector: Selector<'database' | 'snapshot' | 'branch'>, /** * Transaction to apply. */ transaction: Transaction, /** * Options for applying the Transaction. */ opts?: { /** * Trace to associate the operation with. * * @default `null` */ trace?: Trace | null | undefined; /** * Whether to emit a `ResourceTransactionEvent` when the Transaction * has been applied. * * @default `true` */ emit?: boolean | null | undefined; }): Promise>; /** * Free up locally replicate state for a specific Resource. * * @param selector Selector of the Resource to free up locally replicate state for. * @param opts Options for freeing up locally replicate state for the Resource. * * @returns Promise resolving when the locally replicate state for the Resource has been freed. * * @throws `ResourceNotFoundError` if the Resource was not found. * * @emits `StorageFreeEvent` when the locally replicate state for the Resource has been freed. */ free(selector: Selector, opts?: { trace?: Trace | null | undefined; emit?: boolean | null | undefined; }): Promise; /** * Delete a specific Resource. * * This will also cause all children Resources to be deleted! * * All files associated with the Resource and its children will be deleted both * locally and in S3. * * @param selector Selector of the Resource to delete. * @param opts Options for deleting the Resource. * * @returns Promise resolving when the Resource and its children have * been deleted. * * @throws `ResourceNotFoundError` if the Resource to delete was not found. * * @emits `StorageDeleteEvent` when the Resource and its children have been * deleted. */ delete(selector: Selector, opts?: { trace?: Trace | null | undefined; emit?: boolean | null | undefined; }): Promise; } export { S3Storage }; export type { S3StorageConfig };