Vana SDK - v2.2.2
    Preparing search index...

    Class CallbackStorage

    Delegates storage operations to user-provided callback functions.

    This provider enables custom storage integrations by delegating all operations to user-defined callbacks. It follows the same flexible pattern as relayer callbacks, allowing implementations via HTTP, WebSocket, direct cloud APIs, local filesystem, or any other backend.

    The provider validates callback results and wraps errors in consistent StorageError types for uniform error handling across the SDK.

    // HTTP-based implementation
    const httpCallbacks: StorageCallbacks = {
    async upload(blob, filename) {
    const formData = new FormData();
    formData.append('file', blob, filename);
    const response = await fetch('/api/storage/upload', {
    method: 'POST',
    body: formData
    });
    const data = await response.json();
    return {
    url: data.url,
    size: blob.size,
    contentType: blob.type
    };
    },
    async download(identifier) {
    const response = await fetch(`/api/storage/download/${identifier}`);
    return response.blob();
    }
    };

    const storage = new CallbackStorage(httpCallbacks);

    // Direct S3 implementation
    const s3Callbacks: StorageCallbacks = {
    async upload(blob, filename) {
    const url = await getPresignedUploadUrl(filename);
    await fetch(url, { method: 'PUT', body: blob });
    return {
    url: `s3://my-bucket/${filename}`,
    size: blob.size,
    contentType: blob.type
    };
    },
    async download(identifier) {
    const url = await getPresignedDownloadUrl(identifier);
    const response = await fetch(url);
    return response.blob();
    }
    };

    Implements

    Index

    Constructors

    Methods

    • Uploads a file using the user-provided callback.

      Parameters

      • file: Blob

        The blob to upload. Can be any Blob-compatible object including File.

      • Optionalfilename: string

        Optional filename for the upload. If not provided, callback may generate a name.

      Returns Promise<StorageUploadResult>

      Upload result containing URL and metadata

      With code 'INVALID_UPLOAD_RESULT' if callback returns invalid data

      With code 'UPLOAD_ERROR' if upload fails

      const file = new File(['content'], 'data.json');
      const result = await storage.upload(file);
      console.log('Uploaded to:', result.url);
    • Downloads a file using the user-provided callback.

      Parameters

      • url: string

        The URL or identifier to download. If extractIdentifier callback is provided, it will be used to extract the identifier.

      Returns Promise<Blob>

      The downloaded file as a Blob

      With code 'INVALID_DOWNLOAD_RESULT' if callback returns non-Blob

      With code 'DOWNLOAD_ERROR' if download fails

      const blob = await storage.download('https://storage.example.com/file123');
      const text = await blob.text();
    • Lists files using the user-provided callback.

      Parameters

      • Optionaloptions: StorageListOptions

        Optional list options.

        • namePattern

          Pattern to filter files by name. Implementation depends on callback.

        • limit

          Maximum number of files to return. Implementation depends on callback.

      Returns Promise<StorageFile[]>

      Array of storage file metadata

      With code 'NOT_SUPPORTED' if list callback not provided

      With code 'LIST_ERROR' if listing fails

      This operation is optional and only available if a list callback is provided during construction.

      const files = await storage.list({ namePattern: '*.json' });
      files.forEach(file => console.log(file.name, file.size));
    • Deletes a file using the user-provided callback.

      Parameters

      • url: string

        The URL or identifier to delete. If extractIdentifier callback is provided, it will be used to extract the identifier.

      Returns Promise<boolean>

      True if deletion succeeded, false otherwise

      With code 'NOT_SUPPORTED' if delete callback not provided

      With code 'DELETE_ERROR' if deletion fails

      This operation is optional and only available if a delete callback is provided during construction.

      const deleted = await storage.delete('https://storage.example.com/file123');
      if (deleted) {
      console.log('File deleted successfully');
      }