Skip to content

A browser-based virtual container runtime that enables server-like JavaScript execution environments directly in the browser. OpenWebContainer provides a sandboxed environment with a virtual file system, process management, and shell capabilities, making it possible to run server-side JavaScript applications entirely in the browser.

License

Notifications You must be signed in to change notification settings

thecodacus/OpenWebContainer

Repository files navigation

OpenWebContainer

A browser-based virtual container runtime that enables server-like JavaScript execution environments directly in the browser. OpenWebContainer provides a sandboxed environment with a virtual file system, process management, and shell capabilities, making it possible to run server-side JavaScript applications entirely in the browser.

GitHub license PRs Welcome

πŸ—ΊοΈ Architecture Overview

OpenWebContainer Architecture

The architecture consists of three main layers:

  1. UI Layer: React-based user interface with terminal and file explorer
  2. Container Manager: Handles communication between UI and Worker
  3. Web Worker: Runs the actual container in an isolated thread

⚑ Current Progress

βœ… Implemented Features

  • Core Container Infrastructure

    • Web Worker integration for isolated execution
    • Container API abstraction for Worker communication
    • Event-based messaging system
    • Process lifecycle management
  • Virtual File System

    • Full directory structure
    • Basic file operations
    • Path resolution
    • Module loading
  • Shell Environment

    • Interactive shell with command history
    • Built-in commands:
      • ls - List directory contents
      • cd - Change directory
      • pwd - Print working directory
      • mkdir - Create directory
      • touch - Create file
      • rm - Remove file
      • rmdir - Remove directory
      • cat - Display file contents
      • echo - Display text
      • cp - Copy file
      • mv - Move file
    • File redirection (>, >>)
  • JavaScript Runtime

    • QuickJS-based execution
    • ES Modules support
    • Isolated contexts
    • Console output integration

🚧 In Progress

  • Network Simulation
    • Web Worker based network interceptor
    • HTTP request handling
    • Sandbox security

πŸ“‹ Upcoming Features

  • Additional shell commands
  • NPM package manager simulation
  • Process signals (SIGTERM, SIGKILL, etc.)
  • Pipe operator (|) support
  • WebSocket simulation

πŸš€ Features

  • Virtual File System

    • Full directory structure support
    • File operations (create, read, write, delete)
    • Path resolution and normalization
    • Module loading capabilities
  • Process Management

    • Multiple process types (Shell, JavaScript)
    • Process lifecycle management
    • Inter-process communication
    • Event-based architecture
  • Shell Environment

    • UNIX-like shell commands
    • File redirection (>, >>)
    • Interactive shell support
    • Working directory management
  • JavaScript Runtime

    • Isolated execution environments
    • ES Modules support
    • Resource management and cleanup
    • Based on QuickJS for reliable JavaScript execution

πŸ“¦ Project Structure

.
β”œβ”€β”€ apps/                      # Application packages
β”‚   └── playground/           # Web-based playground
β”‚       β”œβ”€β”€ src/             
β”‚       β”‚   β”œβ”€β”€ components/   # React components
β”‚       β”‚   β”‚   β”œβ”€β”€ Editor/
β”‚       β”‚   β”‚   β”œβ”€β”€ FileExplorer/
β”‚       β”‚   β”‚   └── Terminal/
β”‚       β”‚   β”œβ”€β”€ hooks/       # React hooks
β”‚       β”‚   └── ...
β”‚       └── ...
β”œβ”€β”€ packages/
β”‚   β”œβ”€β”€ api/                 # API interface package
β”‚   β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”‚   β”œβ”€β”€ container/   # Container API
β”‚   β”‚   β”‚   β”œβ”€β”€ process/     # Process-related types
β”‚   β”‚   β”‚   └── worker/      # Worker bridge implementation
β”‚   β”‚   └── ...
β”‚   └── core/               # Core implementation
β”‚       β”œβ”€β”€ src/
β”‚       β”‚   β”œβ”€β”€ filesystem/  # Virtual filesystem implementation
β”‚       β”‚   β”œβ”€β”€ interfaces/  # Core interfaces
β”‚       β”‚   β”œβ”€β”€ network/     # Network simulation
β”‚       β”‚   β”œβ”€β”€ process/     # Process implementation
β”‚       β”‚   β”‚   β”œβ”€β”€ base/    # Base process classes
β”‚       β”‚   β”‚   β”œβ”€β”€ executors/ # Process type executors
β”‚       β”‚   β”‚   └── manager/   # Process management
β”‚       β”‚   β”œβ”€β”€ shell/      # Shell implementation
β”‚       β”‚   β”‚   β”œβ”€β”€ commands/ # Shell command implementations
β”‚       β”‚   β”‚   └── ...
β”‚       β”‚   └── utils/      # Utility functions
β”‚       └── ...

πŸ› οΈ Development Setup

Prerequisites

  • Node.js (v16 or higher)
  • pnpm (v8 or higher)
  • Git

Installation

# Clone the repository
git clone https://github.com/thecodacus/OpenWebContainer.git
cd OpenWebContainer

# Install dependencies
pnpm install

# Start development
pnpm dev          # Start all packages
pnpm playground   # Start only the playground

πŸ“š Usage

Using the Core Package

import { OpenWebContainer } from '@open-web-container/core';

async function main() {
  // Create a new container
  const container = new OpenWebContainer();

  // Create a directory and write a JavaScript file
  container.writeFile('/app/hello.js', `
    console.log('Hello from the container!');
    export const message = 'Hello World';
  `);

  // Run a shell command
  const shell = await container.spawn('sh', ['echo', 'Hello', '>', '/app/greeting.txt']);
  
  // Listen for process events
  shell.addEventListener('exit', ({ exitCode }) => {
    console.log('Shell process exited with code:', exitCode);
  });

  // Run a JavaScript file
  const jsProcess = await container.spawn('node', '/app/hello.js');
  
  // Clean up when done
  await container.dispose();
}

main().catch(console.error);

API Examples

File System Operations

// Directory operations
container.createDirectory('/app');
container.listDirectory('/app');
container.deleteDirectory('/app');

// File operations
container.writeFile('/app/script.js', 'console.log("Hello")');
const content = container.readFile('/app/script.js');
container.deleteFile('/app/script.js');

Process Management

// Spawn a shell process
const shell = await container.spawn('sh', ['ls']);

// Spawn a JavaScript process
const process = await container.spawn('node', '/app/script.js');

// Process events
process.addEventListener('start', (data) => { /* ... */ });
process.addEventListener('exit', (data) => { /* ... */ });
process.addEventListener('error', (data) => { /* ... */ });
process.addEventListener('message', (data) => { /* ... */ });

Shell Commands

// Interactive shell
const shell = await container.spawn('sh');
if (shell instanceof ShellProcess) {
  // Execute commands
  await shell.executeCommand('mkdir /app');
  await shell.executeCommand('echo "Hello" > /app/hello.txt');
  await shell.executeCommand('cat /app/hello.txt');
}

πŸ§ͺ Development Commands

# Start development
pnpm dev              # Start all packages
pnpm playground       # Start only the playground
pnpm core:dev         # Start core package development

# Building
pnpm build           # Build all packages
pnpm core:build      # Build only core package

# Testing
pnpm test            # Run all tests
pnpm lint            # Run linter
pnpm format          # Format code

# Release
pnpm changeset       # Create a changeset
pnpm version-packages # Update versions
pnpm release         # Publish to npm

πŸ› οΈ Contributing

We welcome contributions! See our Contributing Guide for details on:

  • Development workflow
  • Project structure
  • Testing guidelines
  • Pull request process

Areas for Contribution

  • Shell command improvements
    • Add pipe (|) support
    • Additional built-in commands
    • Command argument parsing
  • Process management enhancements
    • Process signals
    • Job control
    • Process groups
  • Network features
    • HTTP request simulation
    • WebSocket support
    • Network isolation
  • Developer tools
    • Debugging capabilities
    • Process inspection
    • Performance monitoring

πŸ“ Roadmap

Current focus areas:

  • Network simulation implementation
  • NPM package manager integration
  • Additional shell commands
  • WebSocket support

Future plans:

  • Add pipe support for shell commands
  • Implement environment variables
  • Add signal handling (SIGTERM, SIGKILL, etc.)
  • Create process groups and job control
  • Create development tools and debugging capabilities

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

🀝 Acknowledgments

πŸ’¬ Support

  • Create an issue for bug reports or feature requests

Similar Projects

About

A browser-based virtual container runtime that enables server-like JavaScript execution environments directly in the browser. OpenWebContainer provides a sandboxed environment with a virtual file system, process management, and shell capabilities, making it possible to run server-side JavaScript applications entirely in the browser.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages