**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:
parent
116d6cea79
commit
db67ccd34f
3 changed files with 17 additions and 24 deletions
|
|
@ -632,7 +632,8 @@ export class OPFSStorage extends BaseStorage {
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
for await (const [name, handle] of dirHandle.entries()) {
|
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) {
|
} catch (error) {
|
||||||
console.error(`Error removing directory contents:`, error)
|
console.error(`Error removing directory contents:`, error)
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,9 @@
|
||||||
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 {S3CompatibleStorage, R2Storage} from './adapters/s3CompatibleStorage.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
|
* 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 file system storage is forced, use it regardless of other options
|
||||||
if (options.forceFileSystemStorage) {
|
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)')
|
console.log('Using file system storage (forced)')
|
||||||
|
const {FileSystemStorage} = await import('./adapters/fileSystemStorage.js')
|
||||||
return new FileSystemStorage(options.rootDirectory || './brainy-data')
|
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')
|
console.log('Using file system storage')
|
||||||
|
const {FileSystemStorage} = await import('./adapters/fileSystemStorage.js')
|
||||||
return new FileSystemStorage(options.rootDirectory || './brainy-data')
|
return new FileSystemStorage(options.rootDirectory || './brainy-data')
|
||||||
|
}
|
||||||
|
|
||||||
case 's3':
|
case 's3':
|
||||||
if (options.s3Storage) {
|
if (options.s3Storage) {
|
||||||
|
|
@ -362,4 +374,4 @@ export {
|
||||||
FileSystemStorage,
|
FileSystemStorage,
|
||||||
S3CompatibleStorage,
|
S3CompatibleStorage,
|
||||||
R2Storage
|
R2Storage
|
||||||
}
|
}
|
||||||
|
|
|
||||||
20
test-fix.js
20
test-fix.js
|
|
@ -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();
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue