fix: prevent fs adapter from throwing on import in browser
- Add BrowserFS no-op implementation instead of throwing immediately - Methods still throw when called (not at import time) - exists() returns false instead of throwing - readdir() returns empty arrays instead of throwing This allows Brainy to be imported in browser environments without errors
This commit is contained in:
parent
ac9ec22c0d
commit
b7e5f84af6
1 changed files with 42 additions and 1 deletions
|
|
@ -86,13 +86,54 @@ class NodeFS implements UniversalFS {
|
|||
}
|
||||
}
|
||||
|
||||
// Browser-safe no-op implementation
|
||||
class BrowserFS implements UniversalFS {
|
||||
async readFile(path: string, encoding = 'utf-8'): Promise<string> {
|
||||
throw new Error('File system operations not available in browser. Use OPFS, Memory, or S3 storage adapters instead.')
|
||||
}
|
||||
|
||||
async writeFile(path: string, data: string, encoding = 'utf-8'): Promise<void> {
|
||||
throw new Error('File system operations not available in browser. Use OPFS, Memory, or S3 storage adapters instead.')
|
||||
}
|
||||
|
||||
async mkdir(path: string, options = { recursive: true }): Promise<void> {
|
||||
throw new Error('File system operations not available in browser. Use OPFS, Memory, or S3 storage adapters instead.')
|
||||
}
|
||||
|
||||
async exists(path: string): Promise<boolean> {
|
||||
return false // Always return false in browser
|
||||
}
|
||||
|
||||
async readdir(path: string): Promise<string[]>
|
||||
async readdir(path: string, options: { withFileTypes: true }): Promise<{ name: string, isDirectory(): boolean, isFile(): boolean }[]>
|
||||
async readdir(path: string, options?: { withFileTypes?: boolean }): Promise<string[] | { name: string, isDirectory(): boolean, isFile(): boolean }[]> {
|
||||
if (options?.withFileTypes) {
|
||||
return []
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
async unlink(path: string): Promise<void> {
|
||||
throw new Error('File system operations not available in browser. Use OPFS, Memory, or S3 storage adapters instead.')
|
||||
}
|
||||
|
||||
async stat(path: string): Promise<{ isFile(): boolean, isDirectory(): boolean }> {
|
||||
throw new Error('File system operations not available in browser. Use OPFS, Memory, or S3 storage adapters instead.')
|
||||
}
|
||||
|
||||
async access(path: string, mode?: number): Promise<void> {
|
||||
throw new Error('File system operations not available in browser. Use OPFS, Memory, or S3 storage adapters instead.')
|
||||
}
|
||||
}
|
||||
|
||||
// Create the appropriate filesystem implementation
|
||||
let fsImpl: UniversalFS
|
||||
|
||||
if (isNode() && nodeFs) {
|
||||
fsImpl = new NodeFS()
|
||||
} else {
|
||||
throw new Error('File system operations not available. Framework bundlers should provide fs polyfills or use storage adapters like OPFS, Memory, or S3.')
|
||||
// Use browser-safe no-op implementation instead of throwing
|
||||
fsImpl = new BrowserFS()
|
||||
}
|
||||
|
||||
// Export the filesystem operations
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue