feat: reformat imports and exports for consistency and readability

- Updated all imports and exports in `src/index.ts` and `src/storage/storageFactory.ts` to use a consistent formatting style.
- Enhanced readability by standardizing spacing, alignment, and indentation across the codebase.
- Applied consistent import/export styling for structured comments and multi-line objects.

**Purpose**: Improve code maintainability and readability by enforcing uniform formatting, aligning imports and exports for better clarity.
This commit is contained in:
David Snelling 2025-07-21 13:59:33 -07:00
parent 6c36cb80b1
commit ac4a615fdc
2 changed files with 527 additions and 527 deletions

View file

@ -13,226 +13,226 @@
import './setup.js' import './setup.js'
// Export main BrainyData class and related types // Export main BrainyData class and related types
import { BrainyData, BrainyDataConfig } from './brainyData.js' import {BrainyData, BrainyDataConfig} from './brainyData.js'
export { BrainyData } export {BrainyData}
export type { BrainyDataConfig } export type {BrainyDataConfig}
// Export distance functions for convenience // Export distance functions for convenience
import { import {
euclideanDistance, euclideanDistance,
cosineDistance, cosineDistance,
manhattanDistance, manhattanDistance,
dotProductDistance dotProductDistance
} from './utils/index.js' } from './utils/index.js'
export { export {
euclideanDistance, euclideanDistance,
cosineDistance, cosineDistance,
manhattanDistance, manhattanDistance,
dotProductDistance dotProductDistance
} }
// Export embedding functionality // Export embedding functionality
import { import {
UniversalSentenceEncoder, UniversalSentenceEncoder,
createEmbeddingFunction, createEmbeddingFunction,
createTensorFlowEmbeddingFunction, createTensorFlowEmbeddingFunction,
createThreadedEmbeddingFunction, createThreadedEmbeddingFunction,
defaultEmbeddingFunction defaultEmbeddingFunction
} from './utils/embedding.js' } from './utils/embedding.js'
// Export worker utilities // Export worker utilities
import { executeInThread, cleanupWorkerPools } from './utils/workerUtils.js' import {executeInThread, cleanupWorkerPools} from './utils/workerUtils.js'
// Export environment utilities // Export environment utilities
import { import {
isBrowser, isBrowser,
isNode, isNode,
isWebWorker, isWebWorker,
areWebWorkersAvailable, areWebWorkersAvailable,
areWorkerThreadsAvailable, areWorkerThreadsAvailable,
areWorkerThreadsAvailableSync, areWorkerThreadsAvailableSync,
isThreadingAvailable, isThreadingAvailable,
isThreadingAvailableAsync isThreadingAvailableAsync
} from './utils/environment.js' } from './utils/environment.js'
export { export {
UniversalSentenceEncoder, UniversalSentenceEncoder,
createEmbeddingFunction, createEmbeddingFunction,
createTensorFlowEmbeddingFunction, createTensorFlowEmbeddingFunction,
createThreadedEmbeddingFunction, createThreadedEmbeddingFunction,
defaultEmbeddingFunction, defaultEmbeddingFunction,
// Worker utilities // Worker utilities
executeInThread, executeInThread,
cleanupWorkerPools, cleanupWorkerPools,
// Environment utilities // Environment utilities
isBrowser, isBrowser,
isNode, isNode,
isWebWorker, isWebWorker,
areWebWorkersAvailable, areWebWorkersAvailable,
areWorkerThreadsAvailable, areWorkerThreadsAvailable,
areWorkerThreadsAvailableSync, areWorkerThreadsAvailableSync,
isThreadingAvailable, isThreadingAvailable,
isThreadingAvailableAsync isThreadingAvailableAsync
} }
// Export storage adapters // Export storage adapters
import { import {
OPFSStorage, OPFSStorage,
MemoryStorage, MemoryStorage,
FileSystemStorage, FileSystemStorage,
R2Storage, R2Storage,
S3CompatibleStorage, S3CompatibleStorage,
createStorage createStorage
} from './storage/storageFactory.js' } from './storage/storageFactory.js'
export { export {
OPFSStorage, OPFSStorage,
MemoryStorage, MemoryStorage,
FileSystemStorage, FileSystemStorage,
R2Storage, R2Storage,
S3CompatibleStorage, S3CompatibleStorage,
createStorage createStorage
} }
// Export unified pipeline // Export unified pipeline
import { import {
Pipeline, Pipeline,
pipeline, pipeline,
augmentationPipeline, augmentationPipeline,
ExecutionMode, ExecutionMode,
PipelineOptions, PipelineOptions,
PipelineResult, PipelineResult,
executeStreamlined, executeStreamlined,
executeByType, executeByType,
executeSingle, executeSingle,
processStaticData, processStaticData,
processStreamingData, processStreamingData,
createPipeline, createPipeline,
createStreamingPipeline, createStreamingPipeline,
StreamlinedExecutionMode, StreamlinedExecutionMode,
StreamlinedPipelineOptions, StreamlinedPipelineOptions,
StreamlinedPipelineResult StreamlinedPipelineResult
} from './pipeline.js' } from './pipeline.js'
// Export sequential pipeline (for backward compatibility) // Export sequential pipeline (for backward compatibility)
import { import {
SequentialPipeline, SequentialPipeline,
sequentialPipeline, sequentialPipeline,
SequentialPipelineOptions SequentialPipelineOptions
} from './sequentialPipeline.js' } from './sequentialPipeline.js'
// Export augmentation factory // Export augmentation factory
import { import {
createSenseAugmentation, createSenseAugmentation,
addWebSocketSupport, addWebSocketSupport,
executeAugmentation, executeAugmentation,
loadAugmentationModule, loadAugmentationModule,
AugmentationOptions AugmentationOptions
} from './augmentationFactory.js' } from './augmentationFactory.js'
export { export {
// Unified pipeline exports // Unified pipeline exports
Pipeline, Pipeline,
pipeline, pipeline,
augmentationPipeline, augmentationPipeline,
ExecutionMode, ExecutionMode,
SequentialPipeline, SequentialPipeline,
sequentialPipeline, sequentialPipeline,
// Streamlined pipeline exports (now part of unified pipeline) // Streamlined pipeline exports (now part of unified pipeline)
executeStreamlined, executeStreamlined,
executeByType, executeByType,
executeSingle, executeSingle,
processStaticData, processStaticData,
processStreamingData, processStreamingData,
createPipeline, createPipeline,
createStreamingPipeline, createStreamingPipeline,
StreamlinedExecutionMode, StreamlinedExecutionMode,
// Augmentation factory exports // Augmentation factory exports
createSenseAugmentation, createSenseAugmentation,
addWebSocketSupport, addWebSocketSupport,
executeAugmentation, executeAugmentation,
loadAugmentationModule loadAugmentationModule
} }
export type { export type {
PipelineOptions, PipelineOptions,
PipelineResult, PipelineResult,
SequentialPipelineOptions, SequentialPipelineOptions,
StreamlinedPipelineOptions, StreamlinedPipelineOptions,
StreamlinedPipelineResult, StreamlinedPipelineResult,
AugmentationOptions AugmentationOptions
} }
// Export augmentation registry for build-time loading // Export augmentation registry for build-time loading
import { import {
availableAugmentations, availableAugmentations,
registerAugmentation, registerAugmentation,
initializeAugmentationPipeline, initializeAugmentationPipeline,
setAugmentationEnabled, setAugmentationEnabled,
getAugmentationsByType getAugmentationsByType
} from './augmentationRegistry.js' } from './augmentationRegistry.js'
export { export {
availableAugmentations, availableAugmentations,
registerAugmentation, registerAugmentation,
initializeAugmentationPipeline, initializeAugmentationPipeline,
setAugmentationEnabled, setAugmentationEnabled,
getAugmentationsByType getAugmentationsByType
} }
// Export augmentation registry loader for build tools // Export augmentation registry loader for build tools
import { import {
loadAugmentationsFromModules, loadAugmentationsFromModules,
createAugmentationRegistryPlugin, createAugmentationRegistryPlugin,
createAugmentationRegistryRollupPlugin createAugmentationRegistryRollupPlugin
} from './augmentationRegistryLoader.js' } from './augmentationRegistryLoader.js'
import type { import type {
AugmentationRegistryLoaderOptions, AugmentationRegistryLoaderOptions,
AugmentationLoadResult AugmentationLoadResult
} from './augmentationRegistryLoader.js' } from './augmentationRegistryLoader.js'
export { export {
loadAugmentationsFromModules, loadAugmentationsFromModules,
createAugmentationRegistryPlugin, createAugmentationRegistryPlugin,
createAugmentationRegistryRollupPlugin createAugmentationRegistryRollupPlugin
} }
export type { AugmentationRegistryLoaderOptions, AugmentationLoadResult } export type {AugmentationRegistryLoaderOptions, AugmentationLoadResult}
// Export augmentation implementations // Export augmentation implementations
import { import {
MemoryStorageAugmentation, MemoryStorageAugmentation,
FileSystemStorageAugmentation, FileSystemStorageAugmentation,
OPFSStorageAugmentation, OPFSStorageAugmentation,
createMemoryAugmentation createMemoryAugmentation
} from './augmentations/memoryAugmentations.js' } from './augmentations/memoryAugmentations.js'
import { import {
WebSocketConduitAugmentation, WebSocketConduitAugmentation,
WebRTCConduitAugmentation, WebRTCConduitAugmentation,
createConduitAugmentation createConduitAugmentation
} from './augmentations/conduitAugmentations.js' } from './augmentations/conduitAugmentations.js'
import { import {
ServerSearchConduitAugmentation, ServerSearchConduitAugmentation,
ServerSearchActivationAugmentation, ServerSearchActivationAugmentation,
createServerSearchAugmentations createServerSearchAugmentations
} from './augmentations/serverSearchAugmentations.js' } from './augmentations/serverSearchAugmentations.js'
// Non-LLM exports // Non-LLM exports
export { export {
MemoryStorageAugmentation, MemoryStorageAugmentation,
FileSystemStorageAugmentation, FileSystemStorageAugmentation,
OPFSStorageAugmentation, OPFSStorageAugmentation,
createMemoryAugmentation, createMemoryAugmentation,
WebSocketConduitAugmentation, WebSocketConduitAugmentation,
WebRTCConduitAugmentation, WebRTCConduitAugmentation,
createConduitAugmentation, createConduitAugmentation,
ServerSearchConduitAugmentation, ServerSearchConduitAugmentation,
ServerSearchActivationAugmentation, ServerSearchActivationAugmentation,
createServerSearchAugmentations createServerSearchAugmentations
} }
// LLM augmentations are optional and not imported by default // LLM augmentations are optional and not imported by default
@ -241,142 +241,142 @@ export {
// Export types // Export types
import type { import type {
Vector, Vector,
VectorDocument, VectorDocument,
SearchResult, SearchResult,
DistanceFunction, DistanceFunction,
EmbeddingFunction, EmbeddingFunction,
EmbeddingModel, EmbeddingModel,
HNSWNoun, HNSWNoun,
GraphVerb, GraphVerb,
HNSWConfig, HNSWConfig,
StorageAdapter StorageAdapter
} from './coreTypes.js' } from './coreTypes.js'
// Export HNSW index and optimized version // Export HNSW index and optimized version
import { HNSWIndex } from './hnsw/hnswIndex.js' import {HNSWIndex} from './hnsw/hnswIndex.js'
import { import {
HNSWIndexOptimized, HNSWIndexOptimized,
HNSWOptimizedConfig HNSWOptimizedConfig
} from './hnsw/hnswIndexOptimized.js' } from './hnsw/hnswIndexOptimized.js'
export { HNSWIndex, HNSWIndexOptimized } export {HNSWIndex, HNSWIndexOptimized}
export type { export type {
Vector, Vector,
VectorDocument, VectorDocument,
SearchResult, SearchResult,
DistanceFunction, DistanceFunction,
EmbeddingFunction, EmbeddingFunction,
EmbeddingModel, EmbeddingModel,
HNSWNoun, HNSWNoun,
GraphVerb, GraphVerb,
HNSWConfig, HNSWConfig,
HNSWOptimizedConfig, HNSWOptimizedConfig,
StorageAdapter StorageAdapter
} }
// Export augmentation types // Export augmentation types
import type { import type {
IAugmentation, IAugmentation,
AugmentationResponse, AugmentationResponse,
IWebSocketSupport, IWebSocketSupport,
ISenseAugmentation, ISenseAugmentation,
IConduitAugmentation, IConduitAugmentation,
ICognitionAugmentation, ICognitionAugmentation,
IMemoryAugmentation, IMemoryAugmentation,
IPerceptionAugmentation, IPerceptionAugmentation,
IDialogAugmentation, IDialogAugmentation,
IActivationAugmentation IActivationAugmentation
} from './types/augmentations.js' } from './types/augmentations.js'
import { AugmentationType, BrainyAugmentations } from './types/augmentations.js' import {AugmentationType, BrainyAugmentations} from './types/augmentations.js'
export type { IAugmentation, AugmentationResponse, IWebSocketSupport } export type {IAugmentation, AugmentationResponse, IWebSocketSupport}
export { export {
AugmentationType, AugmentationType,
BrainyAugmentations, BrainyAugmentations,
ISenseAugmentation, ISenseAugmentation,
IConduitAugmentation, IConduitAugmentation,
ICognitionAugmentation, ICognitionAugmentation,
IMemoryAugmentation, IMemoryAugmentation,
IPerceptionAugmentation, IPerceptionAugmentation,
IDialogAugmentation, IDialogAugmentation,
IActivationAugmentation IActivationAugmentation
} }
// Export combined WebSocket augmentation interfaces // Export combined WebSocket augmentation interfaces
export type { export type {
IWebSocketCognitionAugmentation, IWebSocketCognitionAugmentation,
IWebSocketSenseAugmentation, IWebSocketSenseAugmentation,
IWebSocketPerceptionAugmentation, IWebSocketPerceptionAugmentation,
IWebSocketActivationAugmentation, IWebSocketActivationAugmentation,
IWebSocketDialogAugmentation, IWebSocketDialogAugmentation,
IWebSocketConduitAugmentation, IWebSocketConduitAugmentation,
IWebSocketMemoryAugmentation IWebSocketMemoryAugmentation
} from './types/augmentations.js' } from './types/augmentations.js'
// Export graph types // Export graph types
import type { import type {
GraphNoun, GraphNoun,
EmbeddedGraphVerb, EmbeddedGraphVerb,
Person, Person,
Place, Place,
Thing, Thing,
Event, Event,
Concept, Concept,
Content Content
} from './types/graphTypes.js' } from './types/graphTypes.js'
import { NounType, VerbType } from './types/graphTypes.js' import {NounType, VerbType} from './types/graphTypes.js'
export type { export type {
GraphNoun, GraphNoun,
EmbeddedGraphVerb, EmbeddedGraphVerb,
Person, Person,
Place, Place,
Thing, Thing,
Event, Event,
Concept, Concept,
Content Content
} }
export { NounType, VerbType } export {NounType, VerbType}
// Export MCP (Model Control Protocol) components // Export MCP (Model Control Protocol) components
import { import {
BrainyMCPAdapter, BrainyMCPAdapter,
MCPAugmentationToolset, MCPAugmentationToolset,
BrainyMCPService BrainyMCPService
} from './mcp/index.js' // Import from mcp/index.js } from './mcp/index.js' // Import from mcp/index.js
import { import {
MCPRequest, MCPRequest,
MCPResponse, MCPResponse,
MCPDataAccessRequest, MCPDataAccessRequest,
MCPToolExecutionRequest, MCPToolExecutionRequest,
MCPSystemInfoRequest, MCPSystemInfoRequest,
MCPAuthenticationRequest, MCPAuthenticationRequest,
MCPRequestType, MCPRequestType,
MCPServiceOptions, MCPServiceOptions,
MCPTool, MCPTool,
MCP_VERSION MCP_VERSION
} from './types/mcpTypes.js' } from './types/mcpTypes.js'
export { export {
// MCP classes // MCP classes
BrainyMCPAdapter, BrainyMCPAdapter,
MCPAugmentationToolset, MCPAugmentationToolset,
BrainyMCPService, BrainyMCPService,
// MCP types // MCP types
MCPRequestType, MCPRequestType,
MCP_VERSION MCP_VERSION
} }
export type { export type {
MCPRequest, MCPRequest,
MCPResponse, MCPResponse,
MCPDataAccessRequest, MCPDataAccessRequest,
MCPToolExecutionRequest, MCPToolExecutionRequest,
MCPSystemInfoRequest, MCPSystemInfoRequest,
MCPAuthenticationRequest, MCPAuthenticationRequest,
MCPServiceOptions, MCPServiceOptions,
MCPTool MCPTool
} }

View file

@ -3,167 +3,167 @@
* Creates the appropriate storage adapter based on the environment and configuration * Creates the appropriate storage adapter based on the environment and configuration
*/ */
import { StorageAdapter } from '../coreTypes.js' import {StorageAdapter} from '../coreTypes.js'
import { MemoryStorage } from './adapters/memoryStorage.js' import {MemoryStorage} from './adapters/memoryStorage.js'
import { OPFSStorage } from './adapters/opfsStorage.js' import {OPFSStorage} from './adapters/opfsStorage.js'
import { FileSystemStorage } from './adapters/fileSystemStorage.js' import {FileSystemStorage} from './adapters/fileSystemStorage.js'
import { S3CompatibleStorage, R2Storage } from './adapters/s3CompatibleStorage.js' import {S3CompatibleStorage, R2Storage} from './adapters/s3CompatibleStorage.js'
/** /**
* Options for creating a storage adapter * Options for creating a storage adapter
*/ */
export interface StorageOptions { export interface StorageOptions {
/**
* The type of storage to use
* - 'auto': Automatically select the best storage adapter based on the environment
* - 'memory': Use in-memory storage
* - 'opfs': Use Origin Private File System storage (browser only)
* - 'filesystem': Use file system storage (Node.js only)
* - 's3': Use Amazon S3 storage
* - 'r2': Use Cloudflare R2 storage
* - 'gcs': Use Google Cloud Storage
*/
type?: 'auto' | 'memory' | 'opfs' | 'filesystem' | 's3' | 'r2' | 'gcs'
/**
* Force the use of memory storage even if other storage types are available
*/
forceMemoryStorage?: boolean
/**
* Force the use of file system storage even if other storage types are available
*/
forceFileSystemStorage?: boolean
/**
* Request persistent storage permission from the user (browser only)
*/
requestPersistentStorage?: boolean
/**
* Root directory for file system storage (Node.js only)
*/
rootDirectory?: string
/**
* Configuration for Amazon S3 storage
*/
s3Storage?: {
/** /**
* S3 bucket name * The type of storage to use
* - 'auto': Automatically select the best storage adapter based on the environment
* - 'memory': Use in-memory storage
* - 'opfs': Use Origin Private File System storage (browser only)
* - 'filesystem': Use file system storage (Node.js only)
* - 's3': Use Amazon S3 storage
* - 'r2': Use Cloudflare R2 storage
* - 'gcs': Use Google Cloud Storage
*/ */
bucketName: string type?: 'auto' | 'memory' | 'opfs' | 'filesystem' | 's3' | 'r2' | 'gcs'
/** /**
* AWS region (e.g., 'us-east-1') * Force the use of memory storage even if other storage types are available
*/ */
region?: string forceMemoryStorage?: boolean
/** /**
* AWS access key ID * Force the use of file system storage even if other storage types are available
*/ */
accessKeyId: string forceFileSystemStorage?: boolean
/** /**
* AWS secret access key * Request persistent storage permission from the user (browser only)
*/ */
secretAccessKey: string requestPersistentStorage?: boolean
/** /**
* AWS session token (optional) * Root directory for file system storage (Node.js only)
*/ */
sessionToken?: string rootDirectory?: string
}
/**
* Configuration for Cloudflare R2 storage
*/
r2Storage?: {
/**
* R2 bucket name
*/
bucketName: string
/** /**
* Cloudflare account ID * Configuration for Amazon S3 storage
*/ */
accountId: string s3Storage?: {
/**
* S3 bucket name
*/
bucketName: string
/**
* AWS region (e.g., 'us-east-1')
*/
region?: string
/**
* AWS access key ID
*/
accessKeyId: string
/**
* AWS secret access key
*/
secretAccessKey: string
/**
* AWS session token (optional)
*/
sessionToken?: string
}
/** /**
* R2 access key ID * Configuration for Cloudflare R2 storage
*/ */
accessKeyId: string r2Storage?: {
/**
* R2 bucket name
*/
bucketName: string
/**
* Cloudflare account ID
*/
accountId: string
/**
* R2 access key ID
*/
accessKeyId: string
/**
* R2 secret access key
*/
secretAccessKey: string
}
/** /**
* R2 secret access key * Configuration for Google Cloud Storage
*/ */
secretAccessKey: string gcsStorage?: {
} /**
* GCS bucket name
*/
bucketName: string
/** /**
* Configuration for Google Cloud Storage * GCS region (e.g., 'us-central1')
*/ */
gcsStorage?: { region?: string
/**
* GCS bucket name /**
*/ * GCS access key ID
bucketName: string */
accessKeyId: string
/**
* GCS secret access key
*/
secretAccessKey: string
/**
* GCS endpoint (e.g., 'https://storage.googleapis.com')
*/
endpoint?: string
}
/** /**
* GCS region (e.g., 'us-central1') * Configuration for custom S3-compatible storage
*/ */
region?: string customS3Storage?: {
/**
* S3-compatible bucket name
*/
bucketName: string
/** /**
* GCS access key ID * S3-compatible region
*/ */
accessKeyId: string region?: string
/** /**
* GCS secret access key * S3-compatible endpoint URL
*/ */
secretAccessKey: string endpoint: string
/** /**
* GCS endpoint (e.g., 'https://storage.googleapis.com') * S3-compatible access key ID
*/ */
endpoint?: string accessKeyId: string
}
/** /**
* Configuration for custom S3-compatible storage * S3-compatible secret access key
*/ */
customS3Storage?: { secretAccessKey: string
/**
* S3-compatible bucket name
*/
bucketName: string
/** /**
* S3-compatible region * S3-compatible service type (for logging and error messages)
*/ */
region?: string serviceType?: string
}
/**
* S3-compatible endpoint URL
*/
endpoint: string
/**
* S3-compatible access key ID
*/
accessKeyId: string
/**
* S3-compatible secret access key
*/
secretAccessKey: string
/**
* S3-compatible service type (for logging and error messages)
*/
serviceType?: string
}
} }
/** /**
@ -172,194 +172,194 @@ export interface StorageOptions {
* @returns Promise that resolves to a storage adapter * @returns Promise that resolves to a storage adapter
*/ */
export async function createStorage( export async function createStorage(
options: StorageOptions = {} options: StorageOptions = {}
): Promise<StorageAdapter> { ): Promise<StorageAdapter> {
// If memory storage is forced, use it regardless of other options // If memory storage is forced, use it regardless of other options
if (options.forceMemoryStorage) { if (options.forceMemoryStorage) {
console.log('Using memory storage (forced)') console.log('Using memory storage (forced)')
return new MemoryStorage()
}
// If file system storage is forced, use it regardless of other options
if (options.forceFileSystemStorage) {
console.log('Using file system storage (forced)')
return new FileSystemStorage(options.rootDirectory || './brainy-data')
}
// If a specific storage type is specified, use it
if (options.type && options.type !== 'auto') {
switch (options.type) {
case 'memory':
console.log('Using memory storage')
return new MemoryStorage() return new MemoryStorage()
}
case 'opfs': { // If file system storage is forced, use it regardless of other options
// Check if OPFS is available if (options.forceFileSystemStorage) {
const opfsStorage = new OPFSStorage() console.log('Using file system storage (forced)')
if (opfsStorage.isOPFSAvailable()) {
console.log('Using OPFS storage')
await opfsStorage.init()
// Request persistent storage if specified
if (options.requestPersistentStorage) {
const isPersistent = await opfsStorage.requestPersistentStorage()
console.log(`Persistent storage ${isPersistent ? 'granted' : 'denied'}`)
}
return opfsStorage
} else {
console.warn('OPFS storage is not available, falling back to memory storage')
return new MemoryStorage()
}
}
case 'filesystem':
console.log('Using file system storage')
return new FileSystemStorage(options.rootDirectory || './brainy-data') return new FileSystemStorage(options.rootDirectory || './brainy-data')
}
case 's3': // If a specific storage type is specified, use it
if (options.s3Storage) { if (options.type && options.type !== 'auto') {
console.log('Using Amazon S3 storage') switch (options.type) {
return new S3CompatibleStorage({ case 'memory':
console.log('Using memory storage')
return new MemoryStorage()
case 'opfs': {
// Check if OPFS is available
const opfsStorage = new OPFSStorage()
if (opfsStorage.isOPFSAvailable()) {
console.log('Using OPFS storage')
await opfsStorage.init()
// Request persistent storage if specified
if (options.requestPersistentStorage) {
const isPersistent = await opfsStorage.requestPersistentStorage()
console.log(`Persistent storage ${isPersistent ? 'granted' : 'denied'}`)
}
return opfsStorage
} else {
console.warn('OPFS storage is not available, falling back to memory storage')
return new MemoryStorage()
}
}
case 'filesystem':
console.log('Using file system storage')
return new FileSystemStorage(options.rootDirectory || './brainy-data')
case 's3':
if (options.s3Storage) {
console.log('Using Amazon S3 storage')
return new S3CompatibleStorage({
bucketName: options.s3Storage.bucketName,
region: options.s3Storage.region,
accessKeyId: options.s3Storage.accessKeyId,
secretAccessKey: options.s3Storage.secretAccessKey,
sessionToken: options.s3Storage.sessionToken,
serviceType: 's3'
})
} else {
console.warn('S3 storage configuration is missing, falling back to memory storage')
return new MemoryStorage()
}
case 'r2':
if (options.r2Storage) {
console.log('Using Cloudflare R2 storage')
return new R2Storage({
bucketName: options.r2Storage.bucketName,
accountId: options.r2Storage.accountId,
accessKeyId: options.r2Storage.accessKeyId,
secretAccessKey: options.r2Storage.secretAccessKey,
serviceType: 'r2'
})
} else {
console.warn('R2 storage configuration is missing, falling back to memory storage')
return new MemoryStorage()
}
case 'gcs':
if (options.gcsStorage) {
console.log('Using Google Cloud Storage')
return new S3CompatibleStorage({
bucketName: options.gcsStorage.bucketName,
region: options.gcsStorage.region,
endpoint: options.gcsStorage.endpoint || 'https://storage.googleapis.com',
accessKeyId: options.gcsStorage.accessKeyId,
secretAccessKey: options.gcsStorage.secretAccessKey,
serviceType: 'gcs'
})
} else {
console.warn('GCS storage configuration is missing, falling back to memory storage')
return new MemoryStorage()
}
default:
console.warn(`Unknown storage type: ${options.type}, falling back to memory storage`)
return new MemoryStorage()
}
}
// If custom S3-compatible storage is specified, use it
if (options.customS3Storage) {
console.log(`Using custom S3-compatible storage: ${options.customS3Storage.serviceType || 'custom'}`)
return new S3CompatibleStorage({
bucketName: options.customS3Storage.bucketName,
region: options.customS3Storage.region,
endpoint: options.customS3Storage.endpoint,
accessKeyId: options.customS3Storage.accessKeyId,
secretAccessKey: options.customS3Storage.secretAccessKey,
serviceType: options.customS3Storage.serviceType || 'custom'
})
}
// If R2 storage is specified, use it
if (options.r2Storage) {
console.log('Using Cloudflare R2 storage')
return new R2Storage({
bucketName: options.r2Storage.bucketName,
accountId: options.r2Storage.accountId,
accessKeyId: options.r2Storage.accessKeyId,
secretAccessKey: options.r2Storage.secretAccessKey,
serviceType: 'r2'
})
}
// If S3 storage is specified, use it
if (options.s3Storage) {
console.log('Using Amazon S3 storage')
return new S3CompatibleStorage({
bucketName: options.s3Storage.bucketName, bucketName: options.s3Storage.bucketName,
region: options.s3Storage.region, region: options.s3Storage.region,
accessKeyId: options.s3Storage.accessKeyId, accessKeyId: options.s3Storage.accessKeyId,
secretAccessKey: options.s3Storage.secretAccessKey, secretAccessKey: options.s3Storage.secretAccessKey,
sessionToken: options.s3Storage.sessionToken, sessionToken: options.s3Storage.sessionToken,
serviceType: 's3' serviceType: 's3'
}) })
} else { }
console.warn('S3 storage configuration is missing, falling back to memory storage')
return new MemoryStorage()
}
case 'r2': // If GCS storage is specified, use it
if (options.r2Storage) { if (options.gcsStorage) {
console.log('Using Cloudflare R2 storage') console.log('Using Google Cloud Storage')
return new R2Storage({ return new S3CompatibleStorage({
bucketName: options.r2Storage.bucketName,
accountId: options.r2Storage.accountId,
accessKeyId: options.r2Storage.accessKeyId,
secretAccessKey: options.r2Storage.secretAccessKey,
serviceType: 'r2'
})
} else {
console.warn('R2 storage configuration is missing, falling back to memory storage')
return new MemoryStorage()
}
case 'gcs':
if (options.gcsStorage) {
console.log('Using Google Cloud Storage')
return new S3CompatibleStorage({
bucketName: options.gcsStorage.bucketName, bucketName: options.gcsStorage.bucketName,
region: options.gcsStorage.region, region: options.gcsStorage.region,
endpoint: options.gcsStorage.endpoint || 'https://storage.googleapis.com', endpoint: options.gcsStorage.endpoint || 'https://storage.googleapis.com',
accessKeyId: options.gcsStorage.accessKeyId, accessKeyId: options.gcsStorage.accessKeyId,
secretAccessKey: options.gcsStorage.secretAccessKey, secretAccessKey: options.gcsStorage.secretAccessKey,
serviceType: 'gcs' serviceType: 'gcs'
}) })
} else { }
console.warn('GCS storage configuration is missing, falling back to memory storage')
return new MemoryStorage() // Auto-detect the best storage adapter based on the environment
// First, try OPFS (browser only)
const opfsStorage = new OPFSStorage()
if (opfsStorage.isOPFSAvailable()) {
console.log('Using OPFS storage (auto-detected)')
await opfsStorage.init()
// Request persistent storage if specified
if (options.requestPersistentStorage) {
const isPersistent = await opfsStorage.requestPersistentStorage()
console.log(`Persistent storage ${isPersistent ? 'granted' : 'denied'}`)
} }
default: return opfsStorage
console.warn(`Unknown storage type: ${options.type}, falling back to memory storage`)
return new MemoryStorage()
} }
}
// If custom S3-compatible storage is specified, use it // Next, try file system storage (Node.js only)
if (options.customS3Storage) { try {
console.log(`Using custom S3-compatible storage: ${options.customS3Storage.serviceType || 'custom'}`) // Check if we're in a Node.js environment
return new S3CompatibleStorage({ if (typeof process !== 'undefined' && process.versions && process.versions.node) {
bucketName: options.customS3Storage.bucketName, console.log('Using file system storage (auto-detected)')
region: options.customS3Storage.region, return new FileSystemStorage(options.rootDirectory || './brainy-data')
endpoint: options.customS3Storage.endpoint, }
accessKeyId: options.customS3Storage.accessKeyId, } catch (error) {
secretAccessKey: options.customS3Storage.secretAccessKey, // Not in a Node.js environment or file system is not available
serviceType: options.customS3Storage.serviceType || 'custom'
})
}
// If R2 storage is specified, use it
if (options.r2Storage) {
console.log('Using Cloudflare R2 storage')
return new R2Storage({
bucketName: options.r2Storage.bucketName,
accountId: options.r2Storage.accountId,
accessKeyId: options.r2Storage.accessKeyId,
secretAccessKey: options.r2Storage.secretAccessKey,
serviceType: 'r2'
})
}
// If S3 storage is specified, use it
if (options.s3Storage) {
console.log('Using Amazon S3 storage')
return new S3CompatibleStorage({
bucketName: options.s3Storage.bucketName,
region: options.s3Storage.region,
accessKeyId: options.s3Storage.accessKeyId,
secretAccessKey: options.s3Storage.secretAccessKey,
sessionToken: options.s3Storage.sessionToken,
serviceType: 's3'
})
}
// If GCS storage is specified, use it
if (options.gcsStorage) {
console.log('Using Google Cloud Storage')
return new S3CompatibleStorage({
bucketName: options.gcsStorage.bucketName,
region: options.gcsStorage.region,
endpoint: options.gcsStorage.endpoint || 'https://storage.googleapis.com',
accessKeyId: options.gcsStorage.accessKeyId,
secretAccessKey: options.gcsStorage.secretAccessKey,
serviceType: 'gcs'
})
}
// Auto-detect the best storage adapter based on the environment
// First, try OPFS (browser only)
const opfsStorage = new OPFSStorage()
if (opfsStorage.isOPFSAvailable()) {
console.log('Using OPFS storage (auto-detected)')
await opfsStorage.init()
// Request persistent storage if specified
if (options.requestPersistentStorage) {
const isPersistent = await opfsStorage.requestPersistentStorage()
console.log(`Persistent storage ${isPersistent ? 'granted' : 'denied'}`)
} }
return opfsStorage
}
// Next, try file system storage (Node.js only) // Finally, fall back to memory storage
try { console.log('Using memory storage (auto-detected)')
// Check if we're in a Node.js environment return new MemoryStorage()
if (typeof process !== 'undefined' && process.versions && process.versions.node) {
console.log('Using file system storage (auto-detected)')
return new FileSystemStorage(options.rootDirectory || './brainy-data')
}
} catch (error) {
// Not in a Node.js environment or file system is not available
}
// Finally, fall back to memory storage
console.log('Using memory storage (auto-detected)')
return new MemoryStorage()
} }
/** /**
* Export all storage adapters * Export all storage adapters
*/ */
export { export {
MemoryStorage, MemoryStorage,
OPFSStorage, OPFSStorage,
FileSystemStorage, FileSystemStorage,
S3CompatibleStorage, S3CompatibleStorage,
R2Storage R2Storage
} }