Remove WebContainers

This commit is contained in:
Alex Gleason committed 2025-09-07 21:07:42 -05:00
1 parent 26635a4b6a
commit d49c52a8d8
9 files changed
+4 -281

No files matched your search

+2 -8
View File
@@ -16,9 +16,8 @@ import { AppConfig } from '@/contexts/AppContext';
import { AISettingsProvider } from '@/components/AISettingsProvider';
import { SessionManagerProvider } from '@/components/SessionManagerProvider';
import { FSProvider } from '@/components/FSProvider';
import { JSRuntimeProvider } from '@/components/JSRuntimeProvider';
import { LightningFSAdapter } from '@/lib/LightningFSAdapter';
import { WebContainerAdapter } from '@/lib/WebContainerAdapter';
import AppRouter from './AppRouter';
const head = createHead({
@@ -54,16 +53,12 @@ const presetRelays = [
const lightningFS = new LightningFS('shakespeare-fs');
const fs = new LightningFSAdapter(lightningFS.promises);
// Initialize JSRuntime with WebContainer adapter
const jsRuntime = new WebContainerAdapter();
export function App() {
return (
<UnheadProvider head={head}>
<AppProvider storageKey="nostr:app-config" defaultConfig={defaultConfig} presetRelays={presetRelays}>
<FSProvider fs={fs}>
<JSRuntimeProvider runtime={jsRuntime}>
<QueryClientProvider client={queryClient}>
<QueryClientProvider client={queryClient}>
<NostrLoginProvider storageKey='nostr:login'>
<NostrProvider>
<AISettingsProvider>
@@ -80,7 +75,6 @@ export function App() {
</NostrProvider>
</NostrLoginProvider>
</QueryClientProvider>
</JSRuntimeProvider>
</FSProvider>
</AppProvider>
</UnheadProvider>
-18
View File
@@ -1,18 +0,0 @@
import { ReactNode } from 'react';
import { JSRuntimeContext, type JSRuntimeContextType } from '@/contexts/JSRuntimeContext';
import type { JSRuntime } from '@/lib/JSRuntime';
interface JSRuntimeProviderProps {
children: ReactNode;
runtime: JSRuntime;
}
export function JSRuntimeProvider({ children, runtime }: JSRuntimeProviderProps) {
const contextValue: JSRuntimeContextType = { runtime };
return (
<JSRuntimeContext.Provider value={contextValue}>
{children}
</JSRuntimeContext.Provider>
);
}
-8
View File
@@ -1,8 +0,0 @@
import { createContext } from 'react';
import type { JSRuntime } from '@/lib/JSRuntime';
export interface JSRuntimeContextType {
runtime: JSRuntime;
}
export const JSRuntimeContext = createContext<JSRuntimeContextType | undefined>(undefined);
-14
View File
@@ -1,14 +0,0 @@
import { useContext } from 'react';
import { JSRuntimeContext, type JSRuntimeContextType } from '@/contexts/JSRuntimeContext';
/**
* Hook to access the JavaScript runtime
* @returns JSRuntime context with runtime instance
*/
export function useJSRuntime(): JSRuntimeContextType {
const context = useContext(JSRuntimeContext);
if (context === undefined) {
throw new Error('useJSRuntime must be used within a JSRuntimeProvider');
}
return context;
}
+2 -58
View File
@@ -1,25 +1,6 @@
/**
* JavaScript runtime interface for executing code and managing files
*/
export interface JSRuntime {
/**
* Filesystem access
*/
fs(): Promise<JSRuntimeFS>;
/**
* Spawn a process in the runtime environment
* @param command Command to execute
* @param args Command arguments
* @param options Spawn options
* @returns Process handle
*/
spawn(command: string, args?: string[], options?: SpawnOptions): Promise<JSRuntimeChildProcess>;
}
/**
* Filesystem interface compatible with both WebContainer and LightningFS
* This interface unifies the APIs of both filesystem implementations
* Filesystem interface for LightningFS
* This interface provides a unified API for filesystem operations
*/
export interface JSRuntimeFS {
// Core file operations with proper overloads for type safety
@@ -64,41 +45,4 @@ export interface DirectoryEntry {
name: string;
isDirectory(): boolean;
isFile(): boolean;
}
/**
* Unified child process interface
*/
export interface JSRuntimeChildProcess {
/**
* A promise for the exit code of the process.
*/
exit: Promise<number>;
/**
* An input stream for the attached pseudoterminal device.
*/
input: WritableStream<string>;
/**
* A stream that receives all terminal output, including the stdout and stderr emitted by the spawned process
* _and_ its descendants.
*/
output: ReadableStream<string>;
/**
* Kills the process.
*/
kill(): void;
}
/**
* Options that control process spawning.
*/
export interface SpawnOptions {
/**
* Current working directory for the process, relative to the workdir of this instance.
*/
cwd?: string;
/**
* Environment variables to set for the process.
*/
env?: Record<string, string | number | boolean>;
}
-165
View File
@@ -1,165 +0,0 @@
import { WebContainer } from '@webcontainer/api';
import type { JSRuntime, JSRuntimeFS, JSRuntimeChildProcess, DirectoryEntry, SpawnOptions } from './JSRuntime';
/**
* WebContainer adapter that implements the JSRuntime interface
*/
export class WebContainerAdapter implements JSRuntime {
private webcontainer: WebContainer | null = null;
private bootPromise: Promise<WebContainer> | null = null;
/**
* Get the filesystem interface
*/
async fs(): Promise<JSRuntimeFS> {
const webcontainer = await this.ensureBooted();
return new WebContainerFSAdapter(webcontainer);
}
/**
* Ensure WebContainer is booted
*/
private async ensureBooted(): Promise<WebContainer> {
if (this.webcontainer) {
return this.webcontainer;
}
if (!this.bootPromise) {
this.bootPromise = WebContainer.boot();
}
this.webcontainer = await this.bootPromise;
return this.webcontainer;
}
/**
* Spawn a process in the WebContainer
* Automatically boots the container on first call
*/
async spawn(command: string, args: string[] = [], options: SpawnOptions = {}): Promise<JSRuntimeChildProcess> {
const webcontainer = await this.ensureBooted();
return webcontainer.spawn(command, args, options);
}
}
/**
* WebContainer filesystem adapter that implements JSRuntimeFS
*/
class WebContainerFSAdapter implements JSRuntimeFS {
constructor(private webcontainer: WebContainer) {}
async readFile(path: string): Promise<Uint8Array>;
async readFile(path: string, encoding: 'utf8'): Promise<string>;
async readFile(path: string, encoding: string): Promise<string>;
async readFile(path: string, encoding?: string): Promise<string | Uint8Array>;
async readFile(path: string, encoding?: string): Promise<string | Uint8Array> {
if (encoding) {
return this.webcontainer.fs.readFile(path, encoding as 'utf8');
} else {
return this.webcontainer.fs.readFile(path);
}
}
async writeFile(path: string, data: string | Uint8Array, encoding?: string): Promise<void> {
if (encoding) {
return this.webcontainer.fs.writeFile(path, data, encoding as 'utf8');
} else {
return this.webcontainer.fs.writeFile(path, data);
}
}
async readdir(path: string): Promise<string[]>;
async readdir(path: string, options: { withFileTypes: true }): Promise<DirectoryEntry[]>;
async readdir(path: string, options?: { withFileTypes?: boolean }): Promise<string[] | DirectoryEntry[]>;
async readdir(path: string, options?: { withFileTypes?: boolean }): Promise<string[] | DirectoryEntry[]> {
if (options?.withFileTypes) {
return this.webcontainer.fs.readdir(path, { withFileTypes: true });
} else {
return this.webcontainer.fs.readdir(path);
}
}
async mkdir(path: string, options?: { recursive?: boolean }): Promise<void> {
if (options?.recursive) {
await this.webcontainer.fs.mkdir(path, { recursive: true });
} else {
await this.webcontainer.fs.mkdir(path);
}
}
async stat(path: string): Promise<{
isDirectory(): boolean;
isFile(): boolean;
size?: number;
mtimeMs?: number;
}> {
// WebContainer doesn't have a stat method, so we'll implement a workaround
try {
// Try to read as directory first
await this.webcontainer.fs.readdir(path);
return {
isDirectory: () => true,
isFile: () => false,
size: undefined,
mtimeMs: undefined,
};
} catch {
// If readdir fails, assume it's a file
try {
const content = await this.webcontainer.fs.readFile(path) as string | Uint8Array;
const size = typeof content === 'string' ? content.length : content.byteLength;
return {
isDirectory: () => false,
isFile: () => true,
size,
mtimeMs: Date.now(), // WebContainer doesn't provide modification time
};
} catch {
return {
isDirectory: () => false,
isFile: () => true,
size: undefined,
mtimeMs: undefined,
};
}
}
}
async lstat(path: string): Promise<{
isDirectory(): boolean;
isFile(): boolean;
size?: number;
mtimeMs?: number;
}> {
// WebContainer doesn't have a lstat method, so we can use stat directly
return this.stat(path);
}
async unlink(_path: string): Promise<void> {
// WebContainer doesn't expose unlink, so we'll throw an error for now
throw new Error('unlink operation not supported in WebContainer');
}
async rmdir(_path: string): Promise<void> {
// WebContainer doesn't expose rmdir, so we'll throw an error for now
throw new Error('rmdir operation not supported in WebContainer');
}
async rename(oldPath: string, newPath: string): Promise<void> {
// WebContainer doesn't expose rename, so we'll implement it by copying and deleting
const content = await this.webcontainer.fs.readFile(oldPath);
await this.webcontainer.fs.writeFile(newPath, content);
// Note: We can't delete the old file since WebContainer doesn't expose unlink
console.warn('WebContainer rename: copied file but could not delete original');
}
async readlink(_path: string): Promise<string> {
// WebContainer doesn't support symlinks, so we'll throw an error
throw new Error('readlink operation not supported in WebContainer');
}
async symlink(_target: string, _path: string): Promise<void> {
// WebContainer doesn't support symlinks, so we'll throw an error
throw new Error('symlink operation not supported in WebContainer');
}
}
-2
View File
@@ -14,7 +14,6 @@ import { ActionsMenu } from '@/components/ActionsMenu';
import { useIsMobile } from '@/hooks/useIsMobile';
import { useFS } from '@/hooks/useFS';
import { useJSRuntime } from '@/hooks/useJSRuntime';
import { useKeepAlive } from '@/hooks/useKeepAlive';
import { GitStatusIndicator } from '@/components/GitStatusIndicator';
import { StarButton } from '@/components/StarButton';
@@ -40,7 +39,6 @@ export function ProjectView() {
const navigate = useNavigate();
const isMobile = useIsMobile();
const { fs: browserFS } = useFS();
const { runtime: _runtime } = useJSRuntime();
const { user } = useCurrentUser();
const { config } = useAppContext();
const { toast } = useToast();