From d49c52a8d89c8ee2ff45a2d959e93d219a1c0014 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 7 Sep 2025 20:58:35 -0500 Subject: [PATCH] Remove WebContainers --- package-lock.json | 7 -- package.json | 1 - src/App.tsx | 10 +- src/components/JSRuntimeProvider.tsx | 18 --- src/contexts/JSRuntimeContext.ts | 8 -- src/hooks/useJSRuntime.ts | 14 --- src/lib/JSRuntime.ts | 60 +--------- src/lib/WebContainerAdapter.ts | 165 --------------------------- src/pages/ProjectView.tsx | 2 - 9 files changed, 4 insertions(+), 281 deletions(-) delete mode 100644 src/components/JSRuntimeProvider.tsx delete mode 100644 src/contexts/JSRuntimeContext.ts delete mode 100644 src/hooks/useJSRuntime.ts delete mode 100644 src/lib/WebContainerAdapter.ts diff --git a/package-lock.json b/package-lock.json index df49c874..77a3e523 100644 --- a/package-lock.json +++ b/package-lock.json @@ -46,7 +46,6 @@ "@tanstack/react-query": "^5.56.2", "@unhead/addons": "^2.0.10", "@unhead/react": "^2.0.10", - "@webcontainer/api": "^1.6.1", "buffer": "^6.0.3", "class-variance-authority": "^0.7.1", "clsx": "^2.1.1", @@ -4165,12 +4164,6 @@ "url": "https://opencollective.com/vitest" } }, - "node_modules/@webcontainer/api": { - "version": "1.6.1", - "resolved": "https://registry.npmjs.org/@webcontainer/api/-/api-1.6.1.tgz", - "integrity": "sha512-2RS2KiIw32BY1Icf6M1DvqSmcon9XICZCDgS29QJb2NmF12ZY2V5Ia+949hMKB3Wno+P/Y8W+sPP59PZeXSELg==", - "license": "MIT" - }, "node_modules/acorn": { "version": "8.15.0", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.15.0.tgz", diff --git a/package.json b/package.json index d93725ec..e74f2993 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,6 @@ "@tanstack/react-query": "^5.56.2", "@unhead/addons": "^2.0.10", "@unhead/react": "^2.0.10", - "@webcontainer/api": "^1.6.1", "buffer": "^6.0.3", "class-variance-authority": "^0.7.1", "clsx": "^2.1.1", diff --git a/src/App.tsx b/src/App.tsx index 37c079d0..1aba3f37 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -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 ( - - + @@ -80,7 +75,6 @@ export function App() { - diff --git a/src/components/JSRuntimeProvider.tsx b/src/components/JSRuntimeProvider.tsx deleted file mode 100644 index e9f9c984..00000000 --- a/src/components/JSRuntimeProvider.tsx +++ /dev/null @@ -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 ( - - {children} - - ); -} \ No newline at end of file diff --git a/src/contexts/JSRuntimeContext.ts b/src/contexts/JSRuntimeContext.ts deleted file mode 100644 index 8f8ec5c7..00000000 --- a/src/contexts/JSRuntimeContext.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { createContext } from 'react'; -import type { JSRuntime } from '@/lib/JSRuntime'; - -export interface JSRuntimeContextType { - runtime: JSRuntime; -} - -export const JSRuntimeContext = createContext(undefined); \ No newline at end of file diff --git a/src/hooks/useJSRuntime.ts b/src/hooks/useJSRuntime.ts deleted file mode 100644 index 3d6b8771..00000000 --- a/src/hooks/useJSRuntime.ts +++ /dev/null @@ -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; -} \ No newline at end of file diff --git a/src/lib/JSRuntime.ts b/src/lib/JSRuntime.ts index b6151aba..349d789c 100644 --- a/src/lib/JSRuntime.ts +++ b/src/lib/JSRuntime.ts @@ -1,25 +1,6 @@ /** - * JavaScript runtime interface for executing code and managing files - */ -export interface JSRuntime { - /** - * Filesystem access - */ - fs(): Promise; - - /** - * 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; -} - -/** - * 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; - /** - * An input stream for the attached pseudoterminal device. - */ - input: WritableStream; - /** - * A stream that receives all terminal output, including the stdout and stderr emitted by the spawned process - * _and_ its descendants. - */ - output: ReadableStream; - /** - * 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; } \ No newline at end of file diff --git a/src/lib/WebContainerAdapter.ts b/src/lib/WebContainerAdapter.ts deleted file mode 100644 index 5b307430..00000000 --- a/src/lib/WebContainerAdapter.ts +++ /dev/null @@ -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 | null = null; - - /** - * Get the filesystem interface - */ - async fs(): Promise { - const webcontainer = await this.ensureBooted(); - return new WebContainerFSAdapter(webcontainer); - } - - /** - * Ensure WebContainer is booted - */ - private async ensureBooted(): Promise { - 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 { - 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; - async readFile(path: string, encoding: 'utf8'): Promise; - async readFile(path: string, encoding: string): Promise; - async readFile(path: string, encoding?: string): Promise; - async readFile(path: string, encoding?: string): Promise { - 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 { - 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; - async readdir(path: string, options: { withFileTypes: true }): Promise; - async readdir(path: string, options?: { withFileTypes?: boolean }): Promise; - async readdir(path: string, options?: { withFileTypes?: boolean }): Promise { - 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 { - 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 { - // 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 { - // 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 { - // 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 { - // 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 { - // WebContainer doesn't support symlinks, so we'll throw an error - throw new Error('symlink operation not supported in WebContainer'); - } -} \ No newline at end of file diff --git a/src/pages/ProjectView.tsx b/src/pages/ProjectView.tsx index 2e38a5af..f133f789 100644 --- a/src/pages/ProjectView.tsx +++ b/src/pages/ProjectView.tsx @@ -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();