mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 08:23:34 +00:00
265 lines
8.2 KiB
Markdown
265 lines
8.2 KiB
Markdown
# OPFS Fixes for Shakespeare
|
|
|
|
This document summarizes the fixes implemented to resolve OPFS (Origin Private File System) issues in Shakespeare.
|
|
|
|
## Problem Description
|
|
|
|
Users experienced complete failures when attempting to create projects using OPFS. The errors included:
|
|
|
|
1. **Undefined Path Error**: `Cannot read properties of undefined (reading 'includes')`
|
|
2. **Directory Creation Failures**: `ENOENT: no such file or directory, scandir '/projects/squirrel-junction/.git'`
|
|
3. **Git Operation Failures**: `text.split is not a function` from Git config parsing
|
|
4. **Filesystem Incompatibility**: OPFS adapter wasn't fully compatible with isomorphic-git expectations
|
|
|
|
## Root Cause Analysis
|
|
|
|
The issues stemmed from several problems in the OPFS adapter implementation:
|
|
|
|
### 1. Insufficient Path Validation
|
|
The `readFile` method didn't validate input paths, leading to attempts to call `.includes()` on undefined values.
|
|
|
|
### 2. Inconsistent Error Handling
|
|
OPFS throws DOMException errors, but isomorphic-git expects Node.js-style errors with specific error codes (ENOENT, EACCES, etc.).
|
|
|
|
### 3. Recursive Directory Creation Issues
|
|
The recursive directory creation logic didn't handle partial path failures properly, especially for Git directory structures.
|
|
|
|
### 4. Git Compatibility Problems
|
|
The adapter didn't fully support the filesystem operations that isomorphic-git requires for repository operations.
|
|
|
|
## Fixes Implemented
|
|
|
|
### 1. Enhanced Path Validation
|
|
|
|
**File**: `src/lib/OPFSAdapter.ts`
|
|
|
|
**Changes**:
|
|
```typescript
|
|
// Added null/undefined path validation
|
|
if (!path || typeof path !== 'string') {
|
|
const error = new Error('ENOENT: no such file or directory, open \'undefined\'') as NodeJS.ErrnoException;
|
|
error.code = 'ENOENT';
|
|
error.errno = -2;
|
|
error.syscall = 'open';
|
|
error.path = path || 'undefined';
|
|
throw error;
|
|
}
|
|
```
|
|
|
|
**Impact**: Prevents the "Cannot read properties of undefined" error.
|
|
|
|
### 2. Improved Error Handling
|
|
|
|
**File**: `src/lib/OPFSAdapter.ts`
|
|
|
|
**Changes**:
|
|
```typescript
|
|
// Convert DOMException to Node.js style errors
|
|
if (error instanceof Error && error.message.includes('NotFound')) {
|
|
const nodeError = new Error(`ENOENT: no such file or directory, open '${path}'`) as NodeJS.ErrnoException;
|
|
nodeError.code = 'ENOENT';
|
|
nodeError.errno = -2;
|
|
nodeError.syscall = 'open';
|
|
nodeError.path = path;
|
|
throw nodeError;
|
|
}
|
|
```
|
|
|
|
**Impact**: Makes OPFS errors compatible with isomorphic-git.
|
|
|
|
### 3. Better Recursive Directory Creation
|
|
|
|
**File**: `src/lib/OPFSAdapter.ts`
|
|
|
|
**Changes**:
|
|
```typescript
|
|
private async createDirectoriesRecursive(path: string): Promise<void> {
|
|
try {
|
|
const parts = path.split('/').filter(Boolean);
|
|
let currentHandle = await this.root;
|
|
|
|
for (const part of parts) {
|
|
try {
|
|
currentHandle = await currentHandle.getDirectoryHandle(part, { create: true });
|
|
} catch (error) {
|
|
console.error(`Failed to create directory part '${part}' in path '${path}':`, error);
|
|
throw new Error(`Failed to create directory ${path}: ${error}`);
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error(`OPFSAdapter.createDirectoriesRecursive failed for path: ${path}`, error);
|
|
throw error;
|
|
}
|
|
}
|
|
```
|
|
|
|
**Impact**: Ensures Git directory structures can be created reliably.
|
|
|
|
### 4. Enhanced Git Operation Support
|
|
|
|
**File**: `src/lib/OPFSAdapter.ts`
|
|
|
|
**Changes**:
|
|
```typescript
|
|
private async readFileForGit(path: string): Promise<Uint8Array> {
|
|
try {
|
|
// Guard against undefined path
|
|
if (!path || typeof path !== 'string') {
|
|
const error = new Error('ENOENT: no such file or directory, open \'undefined\'') as NodeJS.ErrnoException;
|
|
error.code = 'ENOENT';
|
|
error.errno = -2;
|
|
error.syscall = 'open';
|
|
error.path = path || 'undefined';
|
|
throw error;
|
|
}
|
|
|
|
const fileHandle = await this.getFileHandle(path);
|
|
const file = await fileHandle.getFile();
|
|
const arrayBuffer = await file.arrayBuffer();
|
|
return new Uint8Array(arrayBuffer);
|
|
} catch (error) {
|
|
// Enhanced error handling for Git operations
|
|
console.error(`Git read operation failed for path: ${path}`, error);
|
|
|
|
// Create a more detailed error that isomorphic-git can handle
|
|
const gitError = new Error(`Failed to read Git object: ${path}`) as NodeJS.ErrnoException;
|
|
gitError.code = 'ENOENT';
|
|
gitError.errno = -2;
|
|
gitError.syscall = 'read';
|
|
gitError.path = path;
|
|
|
|
// Preserve original error information by adding it to the message
|
|
if (error instanceof Error) {
|
|
gitError.message = `${gitError.message}: ${error.message}`;
|
|
}
|
|
|
|
throw gitError;
|
|
}
|
|
}
|
|
```
|
|
|
|
**Impact**: Makes Git operations (clone, commit, etc.) work reliably with OPFS.
|
|
|
|
### 5. Improved ProjectsManager Logging
|
|
|
|
**File**: `src/lib/ProjectsManager.ts`
|
|
|
|
**Changes**:
|
|
```typescript
|
|
async createProject(name: string, customId?: string): Promise<Project> {
|
|
console.log(`Creating project: ${name}, customId: ${customId}`);
|
|
|
|
const project = await this.cloneProject(name, GIT_TEMPLATE_URL, customId, { depth: 1 });
|
|
console.log(`Project cloned at path: ${project.path}`);
|
|
|
|
try {
|
|
await this.fs.mkdir(this.dir + `/${project.id}/.ai/history`, { recursive: true });
|
|
console.log(`Created .ai/history directory for project ${project.id}`);
|
|
} catch (error) {
|
|
console.log(`Directory .ai/history might already exist for project ${project.id}:`, error);
|
|
// Directory might already exist
|
|
}
|
|
|
|
// ... rest of the method with enhanced logging
|
|
}
|
|
```
|
|
|
|
**Impact**: Provides better debugging information when issues occur.
|
|
|
|
### 6. Better Filesystem Initialization
|
|
|
|
**File**: `src/App.tsx`
|
|
|
|
**Changes**:
|
|
```typescript
|
|
constructor() {
|
|
// Check if OPFS is available
|
|
if (!('storage' in navigator) ||
|
|
typeof (navigator.storage as unknown as { getDirectory: () => Promise<FileSystemDirectoryHandle> }).getDirectory !== 'function') {
|
|
throw new Error('OPFS is not supported in this browser or environment');
|
|
}
|
|
|
|
// Get the root directory handle for OPFS
|
|
this.root = (navigator.storage as unknown as { getDirectory: () => Promise<FileSystemDirectoryHandle> }).getDirectory();
|
|
}
|
|
```
|
|
|
|
**Impact**: Fails fast if OPFS is not available.
|
|
|
|
## Testing
|
|
|
|
All existing tests pass:
|
|
```bash
|
|
✓ Test Files 83 passed (83)
|
|
Tests 564 passed | 6 skipped (570)
|
|
```
|
|
|
|
Type checking and build also pass successfully.
|
|
|
|
## Browser Compatibility
|
|
|
|
OPFS is supported in:
|
|
- Chrome 86+
|
|
- Edge 86+
|
|
- Firefox 111+ (limited support)
|
|
- Safari 16.4+
|
|
|
|
For browsers that don't support OPFS, the system automatically falls back to LightningFS.
|
|
|
|
## Usage Instructions
|
|
|
|
To use OPFS in Shakespeare:
|
|
|
|
1. **Enable OPFS in Settings**:
|
|
- Go to Settings → Data Settings
|
|
- Change "Filesystem Type" to "OPFS"
|
|
- Save changes
|
|
|
|
2. **Restart Application**:
|
|
- Refresh the page to apply the new filesystem type
|
|
|
|
3. **Verify OPFS is Working**:
|
|
- Try creating a new project
|
|
- Check browser console for successful filesystem operations
|
|
- Verify that project creation completes without errors
|
|
|
|
## Troubleshooting
|
|
|
|
If OPFS still fails:
|
|
|
|
1. **Check Browser Support**:
|
|
- Ensure you're using a supported browser version
|
|
- Check browser console for OPFS availability errors
|
|
|
|
2. **Clear Browser Data**:
|
|
- Clear site data and try again
|
|
- This can resolve corrupted OPFS storage issues
|
|
|
|
3. **Fallback to LightningFS**:
|
|
- If OPFS consistently fails, use LightningFS instead
|
|
- The system will automatically fall back if OPFS initialization fails
|
|
|
|
## Performance Considerations
|
|
|
|
OPFS provides better performance than LightningFS for:
|
|
- Large file operations
|
|
- Concurrent file access
|
|
- Storage persistence
|
|
|
|
However, OPFS has some limitations:
|
|
- No symbolic links
|
|
- Different permission model
|
|
- Browser-specific implementation details
|
|
|
|
## Future Improvements
|
|
|
|
Potential enhancements for OPFS support:
|
|
|
|
1. **Atomic Operations**: Implement atomic file writes using OPFS transactions
|
|
2. **File Locking**: Add file locking mechanisms for concurrent access
|
|
3. **Storage Quotas**: Better handling of storage quota limits
|
|
4. **Migration Tools**: Tools to migrate existing LightningFS data to OPFS
|
|
|
|
## Conclusion
|
|
|
|
The fixes implemented resolve the major OPFS compatibility issues and provide a robust filesystem backend for Shakespeare. The enhanced error handling, improved logging, and better Git compatibility ensure that project creation and management work reliably with OPFS. |