UNPKG

@gensx/storage

Version:

Cloud storage, blobs, sqlite, and vector database providers/hooks for GenSX.

1 lines 10.2 kB
{"version":3,"file":"types.cjs","sources":["@gensx/storage/../../../../src/blob/types.ts"],"sourcesContent":["import { Readable } from \"stream\";\n\n/**\n * Error types for blob storage operations\n */\nexport type BlobErrorCode =\n | \"NOT_FOUND\"\n | \"PERMISSION_DENIED\"\n | \"CONFLICT\"\n | \"INVALID_ARGUMENT\"\n | \"INTERNAL_ERROR\"\n | \"NOT_IMPLEMENTED\"\n | \"NETWORK_ERROR\";\n\n/**\n * Abstract base error class for blob storage operations\n */\nexport abstract class BlobError extends Error {\n constructor(\n public readonly code: BlobErrorCode,\n message: string,\n public readonly cause?: Error,\n ) {\n super(message);\n this.name = \"BlobError\";\n }\n}\n\n/**\n * Error class for when a blob is not found\n */\nexport class BlobNotFoundError extends BlobError {\n constructor(message: string, cause?: Error) {\n super(\"NOT_FOUND\", message, cause);\n this.name = \"BlobNotFoundError\";\n }\n}\n\n/**\n * Error class for permission denied errors\n */\nexport class BlobPermissionDeniedError extends BlobError {\n constructor(message: string, cause?: Error) {\n super(\"PERMISSION_DENIED\", message, cause);\n this.name = \"BlobPermissionDeniedError\";\n }\n}\n\n/**\n * Error class for conflict errors (e.g., ETag mismatch)\n */\nexport class BlobConflictError extends BlobError {\n constructor(message: string, cause?: Error) {\n super(\"CONFLICT\", message, cause);\n this.name = \"BlobConflictError\";\n }\n}\n\n/**\n * Error class for invalid argument errors\n */\nexport class BlobInvalidArgumentError extends BlobError {\n constructor(message: string, cause?: Error) {\n super(\"INVALID_ARGUMENT\", message, cause);\n this.name = \"BlobInvalidArgumentError\";\n }\n}\n\n/**\n * Error class for internal errors\n */\nexport class BlobInternalError extends BlobError {\n constructor(message: string, cause?: Error) {\n super(\"INTERNAL_ERROR\", message, cause);\n this.name = \"BlobInternalError\";\n }\n}\n\n/**\n * Error class for not implemented errors\n */\nexport class BlobNotImplementedError extends BlobError {\n constructor(message: string, cause?: Error) {\n super(\"NOT_IMPLEMENTED\", message, cause);\n this.name = \"BlobNotImplementedError\";\n }\n}\n\n/**\n * Error class for network errors\n */\nexport class BlobNetworkError extends BlobError {\n constructor(message: string, cause?: Error) {\n super(\"NETWORK_ERROR\", message, cause);\n this.name = \"BlobNetworkError\";\n }\n}\n\n/**\n * Options for blob operations\n */\nexport interface BlobOptions {\n /**\n * ETag for conditional operations (optimistic concurrency control)\n */\n etag?: string;\n\n /**\n * Content type of the blob\n */\n contentType?: string;\n\n /**\n * Custom metadata associated with the blob\n */\n metadata?: Record<string, string>;\n}\n\n/**\n * Result of deleting a blob\n */\nexport interface DeleteBlobResult {\n deleted: boolean;\n}\n\n/**\n * A response from a blob operation that includes metadata\n */\nexport interface BlobResponse<T> {\n /**\n * The data content of the blob\n */\n content: T;\n\n /**\n * ETag of the blob\n */\n etag?: string;\n\n /**\n * Last modified timestamp\n */\n lastModified?: Date;\n\n /**\n * Size of the blob in bytes\n */\n size?: number;\n\n /**\n * Content type of the blob\n */\n contentType?: string;\n\n /**\n * Custom metadata associated with the blob\n */\n metadata?: Record<string, string>;\n}\n\n/**\n * Interface for a typed blob object\n */\nexport interface Blob<T> {\n /**\n * Get the blob as JSON data.\n * @returns The blob data as JSON, or null if not found.\n */\n getJSON(): Promise<T | null>;\n\n /**\n * Get the blob as a string.\n * @returns The blob data as a string, or null if not found.\n */\n getString(): Promise<string | null>;\n\n /**\n * Get the raw blob response with metadata.\n * @returns The blob response with metadata, or null if not found.\n */\n getRaw(): Promise<BlobResponse<Buffer> | null>;\n\n /**\n * Get a readable stream of the blob's content.\n * @returns A readable stream of the blob's content.\n */\n getStream(): Promise<Readable>;\n\n /**\n * Put JSON data into the blob.\n * @param value The JSON data to store.\n * @param options Optional metadata and etag for conditional updates.\n * @returns The etag of the stored blob.\n */\n putJSON(value: T, options?: BlobOptions): Promise<{ etag: string }>;\n\n /**\n * Put string data into the blob.\n * @param value The string data to store.\n * @param options Optional metadata and etag for conditional updates.\n * @returns The etag of the stored blob.\n */\n putString(value: string, options?: BlobOptions): Promise<{ etag: string }>;\n\n /**\n * Put raw data into the blob with metadata.\n * @param value The data to store.\n * @param options Optional metadata and etag for conditional updates.\n * @returns The etag of the stored blob.\n */\n putRaw(value: Buffer, options?: BlobOptions): Promise<{ etag: string }>;\n\n /**\n * Put a readable stream into the blob.\n * @param stream The readable stream to store.\n * @param options Optional metadata and etag for conditional updates.\n * @returns The etag of the stored blob.\n */\n putStream(stream: Readable, options?: BlobOptions): Promise<{ etag: string }>;\n\n /**\n * Delete the blob.\n */\n delete(): Promise<void>;\n\n /**\n * Check if the blob exists.\n * @returns True if the blob exists, false otherwise.\n */\n exists(): Promise<boolean>;\n\n /**\n * Get metadata associated with the blob\n * @returns The metadata or null if the blob doesn't exist\n */\n getMetadata(): Promise<Record<string, string> | null>;\n\n /**\n * Update metadata associated with the blob\n * @param metadata The new metadata to store\n * @param options Optional etag for conditional update\n */\n updateMetadata(\n metadata: Record<string, string>,\n options?: BlobOptions,\n ): Promise<void>;\n}\n\n/**\n * Options for listing blobs\n */\nexport interface ListBlobsOptions {\n /**\n * Prefix to filter blobs by\n */\n prefix?: string;\n\n /**\n * Maximum number of results to return per page\n */\n limit?: number;\n\n /**\n * Cursor for pagination. This should be the cursor from the previous page's response.\n * If not provided, returns the first page.\n */\n cursor?: string;\n}\n\n/**\n * Response from listing blobs with pagination\n */\nexport interface ListBlobsResponse {\n /**\n * List of blobs\n */\n blobs: { key: string; lastModified: string; size: number }[];\n\n /**\n * Cursor to get the next page of results.\n * If null, there are no more results.\n */\n nextCursor?: string;\n}\n\n/**\n * Interface for blob storage\n */\nexport interface BlobStorage {\n /**\n * Get a blob object for a specific key\n */\n getBlob<T>(key: string): Blob<T>;\n\n /**\n * List blobs with cursor-based pagination\n * @param options Options for listing blobs including prefix, pagination, and filtering\n */\n listBlobs(options?: ListBlobsOptions): Promise<ListBlobsResponse>;\n\n /**\n * Check if a blob exists\n */\n blobExists(key: string): Promise<boolean>;\n\n /**\n * Delete a blob\n */\n deleteBlob(key: string): Promise<DeleteBlobResult>;\n}\n\n/**\n * Provider configuration kinds\n */\nexport type BlobStorageKind = \"filesystem\" | \"cloud\";\n\n/**\n * Base storage configuration\n */\nexport interface BaseBlobStorageOptions {\n /**\n * Storage kind - if not provided, will be determined from environment\n */\n kind?: BlobStorageKind;\n\n /**\n * Default prefix for all blob keys\n */\n defaultPrefix?: string;\n\n /**\n * Optional project name. By default, the GENSX_PROJECT environment variable will be used then the projectName from the gensx.yaml file.\n */\n project?: string;\n\n /**\n * Optional environment name. By default, the GENSX_ENV environment variable will be used then the currently selected environment in the CLI (e.g. `gensx env select`).\n */\n environment?: string;\n}\n\n/**\n * Filesystem storage configuration\n */\nexport interface FileSystemBlobStorageOptions extends BaseBlobStorageOptions {\n kind?: \"filesystem\";\n\n /**\n * Root directory for storing blobs\n */\n rootDir?: string;\n}\n\n/**\n * Cloud storage configuration\n */\nexport interface CloudBlobStorageOptions extends BaseBlobStorageOptions {\n kind?: \"cloud\";\n}\n\n/**\n * Union type for blob storage configuration\n */\nexport type BlobStorageOptions =\n | FileSystemBlobStorageOptions\n | CloudBlobStorageOptions;\n"],"names":[],"mappings":";;;;;;;;AAcA;;AAEG;AACG,MAAgB,SAAU,SAAQ,KAAK,CAAA;AAEzB,IAAA,IAAA;AAEA,IAAA,KAAA;AAHlB,IAAA,WAAA,CACkB,IAAmB,EACnC,OAAe,EACC,KAAa,EAAA;QAE7B,KAAK,CAAC,OAAO,CAAC;QAJE,IAAI,CAAA,IAAA,GAAJ,IAAI;QAEJ,IAAK,CAAA,KAAA,GAAL,KAAK;AAGrB,QAAA,IAAI,CAAC,IAAI,GAAG,WAAW;;AAE1B;AAED;;AAEG;AACG,MAAO,iBAAkB,SAAQ,SAAS,CAAA;IAC9C,WAAY,CAAA,OAAe,EAAE,KAAa,EAAA;AACxC,QAAA,KAAK,CAAC,WAAW,EAAE,OAAO,EAAE,KAAK,CAAC;AAClC,QAAA,IAAI,CAAC,IAAI,GAAG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