**refactor: improve FileSystemStorage compatibility and remove outdated test**

- Modified `storageFactory` to ensure `FileSystemStorage` gracefully degrades to `MemoryStorage` in browser environments, with proper warnings added.
- Enhanced `opfsStorage` adapter to support recursive directory removal with the `recursive` option.
- Removed `test-fix.js` script, as it is no longer relevant with recent storage fixes and updates.

**Purpose**: Streamline and ensure cross-environment compatibility for `FileSystemStorage`, while removing outdated test artifacts for better maintainability.
This commit is contained in:
David Snelling 2025-07-30 11:25:36 -07:00
parent 116d6cea79
commit db67ccd34f
3 changed files with 17 additions and 24 deletions

View file

@ -632,7 +632,8 @@ export class OPFSStorage extends BaseStorage {
): Promise<void> => {
try {
for await (const [name, handle] of dirHandle.entries()) {
await dirHandle.removeEntry(name)
// Use recursive option to handle directories that may contain files
await dirHandle.removeEntry(name, { recursive: true })
}
} catch (error) {
console.error(`Error removing directory contents:`, error)

View file

@ -6,8 +6,9 @@
import {StorageAdapter} from '../coreTypes.js'
import {MemoryStorage} from './adapters/memoryStorage.js'
import {OPFSStorage} from './adapters/opfsStorage.js'
import {FileSystemStorage} from './adapters/fileSystemStorage.js'
import {S3CompatibleStorage, R2Storage} from './adapters/s3CompatibleStorage.js'
import {FileSystemStorage} from './adapters/fileSystemStorage.js'
import {isBrowser} from '../utils/environment.js'
/**
* Options for creating a storage adapter
@ -182,7 +183,12 @@ export async function createStorage(
// If file system storage is forced, use it regardless of other options
if (options.forceFileSystemStorage) {
if (isBrowser()) {
console.warn('FileSystemStorage is not available in browser environments, falling back to memory storage')
return new MemoryStorage()
}
console.log('Using file system storage (forced)')
const {FileSystemStorage} = await import('./adapters/fileSystemStorage.js')
return new FileSystemStorage(options.rootDirectory || './brainy-data')
}
@ -213,9 +219,15 @@ export async function createStorage(
}
}
case 'filesystem':
case 'filesystem': {
if (isBrowser()) {
console.warn('FileSystemStorage is not available in browser environments, falling back to memory storage')
return new MemoryStorage()
}
console.log('Using file system storage')
const {FileSystemStorage} = await import('./adapters/fileSystemStorage.js')
return new FileSystemStorage(options.rootDirectory || './brainy-data')
}
case 's3':
if (options.s3Storage) {
@ -362,4 +374,4 @@ export {
FileSystemStorage,
S3CompatibleStorage,
R2Storage
}
}

View file

@ -1,20 +0,0 @@
// Simple test script to verify the FileSystemStorage fix
const { createStorage } = require('./dist/unified.js');
async function testFileSystemStorage() {
try {
console.log('Creating storage with forceFileSystemStorage: true');
const storage = await createStorage({ forceFileSystemStorage: true });
console.log('Storage created successfully');
console.log('Initializing storage');
await storage.init();
console.log('Storage initialized successfully');
console.log('Test passed: No TypeError about undefined path.join');
} catch (error) {
console.error('Test failed with error:', error);
}
}
testFileSystemStorage();