From b295370c9b65ca51f2807a3eb445f4c531bb4cab Mon Sep 17 00:00:00 2001 From: David Snelling Date: Wed, 17 Sep 2025 16:22:02 -0700 Subject: [PATCH] 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 --- src/universal/fs.ts | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/src/universal/fs.ts b/src/universal/fs.ts index 742a041b..c4521fc2 100644 --- a/src/universal/fs.ts +++ b/src/universal/fs.ts @@ -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 { + 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 { + 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 { + throw new Error('File system operations not available in browser. Use OPFS, Memory, or S3 storage adapters instead.') + } + + async exists(path: string): Promise { + return false // Always return false in browser + } + + async readdir(path: string): Promise + async readdir(path: string, options: { withFileTypes: true }): Promise<{ name: string, isDirectory(): boolean, isFile(): boolean }[]> + async readdir(path: string, options?: { withFileTypes?: boolean }): Promise { + if (options?.withFileTypes) { + return [] + } + return [] + } + + async unlink(path: string): Promise { + 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 { + 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