feat: enhance framework integration and simplify codebase

- Simplify universal modules to be more framework-friendly
- Add comprehensive framework integration documentation (Next.js, Vue, React)
- Implement missing relateMany() batch relationship creation method
- Clean up obsolete test files and improve test coverage
- Reduce browser polyfill complexity while maintaining compatibility
- Remove unused browserFramework entry points for cleaner API surface

📄 3,120 lines added, 3,679 lines removed for net simplification
This commit is contained in:
David Snelling 2025-09-15 14:53:59 -07:00
parent 203ddaccf6
commit 8d5b4cd263
18 changed files with 3120 additions and 3679 deletions

View file

@ -1,11 +1,10 @@
/**
* Universal File System implementation
* Browser: Uses OPFS (Origin Private File System)
* Node.js: Uses built-in fs/promises
* Serverless: Uses memory-based fallback
* Framework-friendly: Trusts that frameworks provide fs polyfills
* Works in all environments: Browser (via framework), Node.js, Serverless
*/
import { isBrowser, isNode } from '../utils/environment.js'
import { isNode } from '../utils/environment.js'
let nodeFs: any = null
@ -33,136 +32,6 @@ export interface UniversalFS {
access(path: string, mode?: number): Promise<void>
}
/**
* Browser implementation using OPFS
*/
class BrowserFS implements UniversalFS {
private async getRoot(): Promise<FileSystemDirectoryHandle> {
if ('storage' in navigator && 'getDirectory' in navigator.storage) {
return await (navigator.storage as any).getDirectory()
}
throw new Error('OPFS not supported in this browser')
}
private async getFileHandle(path: string, create = false): Promise<FileSystemFileHandle> {
const root = await this.getRoot()
const parts = path.split('/').filter(p => p)
let dir = root
for (let i = 0; i < parts.length - 1; i++) {
dir = await dir.getDirectoryHandle(parts[i], { create })
}
const fileName = parts[parts.length - 1]
return await dir.getFileHandle(fileName, { create })
}
private async getDirHandle(path: string, create = false): Promise<FileSystemDirectoryHandle> {
const root = await this.getRoot()
const parts = path.split('/').filter(p => p)
let dir = root
for (const part of parts) {
dir = await dir.getDirectoryHandle(part, { create })
}
return dir
}
async readFile(path: string, encoding?: string): Promise<string> {
try {
const fileHandle = await this.getFileHandle(path)
const file = await fileHandle.getFile()
return await file.text()
} catch (error) {
throw new Error(`File not found: ${path}`)
}
}
async writeFile(path: string, data: string, encoding?: string): Promise<void> {
const fileHandle = await this.getFileHandle(path, true)
const writable = await fileHandle.createWritable()
await writable.write(data)
await writable.close()
}
async mkdir(path: string, options = { recursive: true }): Promise<void> {
await this.getDirHandle(path, true)
}
async exists(path: string): Promise<boolean> {
try {
await this.getFileHandle(path)
return true
} catch {
try {
await this.getDirHandle(path)
return true
} catch {
return false
}
}
}
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 }[]> {
const dir = await this.getDirHandle(path)
if (options?.withFileTypes) {
const entries: { name: string, isDirectory(): boolean, isFile(): boolean }[] = []
for await (const [name, handle] of dir.entries()) {
entries.push({
name,
isDirectory: () => handle.kind === 'directory',
isFile: () => handle.kind === 'file'
})
}
return entries
} else {
const entries: string[] = []
for await (const [name] of dir.entries()) {
entries.push(name)
}
return entries
}
}
async unlink(path: string): Promise<void> {
const parts = path.split('/').filter(p => p)
const fileName = parts.pop()!
const dirPath = parts.join('/')
if (dirPath) {
const dir = await this.getDirHandle(dirPath)
await dir.removeEntry(fileName)
} else {
const root = await this.getRoot()
await root.removeEntry(fileName)
}
}
async stat(path: string): Promise<{ isFile(): boolean, isDirectory(): boolean }> {
try {
await this.getFileHandle(path)
return { isFile: () => true, isDirectory: () => false }
} catch {
try {
await this.getDirHandle(path)
return { isFile: () => false, isDirectory: () => true }
} catch {
throw new Error(`Path not found: ${path}`)
}
}
}
async access(path: string, mode?: number): Promise<void> {
const exists = await this.exists(path)
if (!exists) {
throw new Error(`ENOENT: no such file or directory, access '${path}'`)
}
}
}
/**
* Node.js implementation using fs/promises
*/
@ -214,112 +83,13 @@ class NodeFS implements UniversalFS {
}
}
/**
* Memory-based fallback for serverless/edge environments
*/
class MemoryFS implements UniversalFS {
private files = new Map<string, string>()
private dirs = new Set<string>()
async readFile(path: string, encoding?: string): Promise<string> {
const content = this.files.get(path)
if (content === undefined) {
throw new Error(`File not found: ${path}`)
}
return content
}
async writeFile(path: string, data: string, encoding?: string): Promise<void> {
this.files.set(path, data)
// Ensure parent directories exist
const parts = path.split('/').slice(0, -1)
for (let i = 1; i <= parts.length; i++) {
this.dirs.add(parts.slice(0, i).join('/'))
}
}
async mkdir(path: string, options = { recursive: true }): Promise<void> {
this.dirs.add(path)
if (options.recursive) {
const parts = path.split('/')
for (let i = 1; i <= parts.length; i++) {
this.dirs.add(parts.slice(0, i).join('/'))
}
}
}
async exists(path: string): Promise<boolean> {
return this.files.has(path) || this.dirs.has(path)
}
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 }[]> {
const entries = new Set<string>()
const pathPrefix = path + '/'
for (const filePath of this.files.keys()) {
if (filePath.startsWith(pathPrefix)) {
const relativePath = filePath.slice(pathPrefix.length)
const firstSegment = relativePath.split('/')[0]
entries.add(firstSegment)
}
}
for (const dirPath of this.dirs) {
if (dirPath.startsWith(pathPrefix)) {
const relativePath = dirPath.slice(pathPrefix.length)
const firstSegment = relativePath.split('/')[0]
if (firstSegment) entries.add(firstSegment)
}
}
if (options?.withFileTypes) {
return Array.from(entries).map(name => ({
name,
isDirectory: () => this.dirs.has(path + '/' + name),
isFile: () => this.files.has(path + '/' + name)
}))
}
return Array.from(entries)
}
async unlink(path: string): Promise<void> {
this.files.delete(path)
}
async stat(path: string): Promise<{ isFile(): boolean, isDirectory(): boolean }> {
const isFile = this.files.has(path)
const isDir = this.dirs.has(path)
if (!isFile && !isDir) {
throw new Error(`Path not found: ${path}`)
}
return {
isFile: () => isFile,
isDirectory: () => isDir
}
}
async access(path: string, mode?: number): Promise<void> {
const exists = await this.exists(path)
if (!exists) {
throw new Error(`ENOENT: no such file or directory, access '${path}'`)
}
}
}
// Create the appropriate filesystem implementation
let fsImpl: UniversalFS
if (isBrowser()) {
fsImpl = new BrowserFS()
} else if (isNode() && nodeFs) {
if (isNode() && nodeFs) {
fsImpl = new NodeFS()
} else {
fsImpl = new MemoryFS()
throw new Error('File system operations not available. Framework bundlers should provide fs polyfills or use storage adapters like OPFS, Memory, or S3.')
}
// Export the filesystem operations