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.
The architecture consists of three main layers:
- UI Layer: React-based user interface with terminal and file explorer
- Container Manager: Handles communication between UI and Worker
- Web Worker: Runs the actual container in an isolated thread
-
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 contentscd
- Change directorypwd
- Print working directorymkdir
- Create directorytouch
- Create filerm
- Remove filermdir
- Remove directorycat
- Display file contentsecho
- Display textcp
- Copy filemv
- Move file
- File redirection (
>
,>>
)
-
JavaScript Runtime
- QuickJS-based execution
- ES Modules support
- Isolated contexts
- Console output integration
- Network Simulation
- Web Worker based network interceptor
- HTTP request handling
- Sandbox security
- Additional shell commands
- NPM package manager simulation
- Process signals (SIGTERM, SIGKILL, etc.)
- Pipe operator (
|
) support - WebSocket simulation
-
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
.
βββ 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
β βββ ...
- Node.js (v16 or higher)
- pnpm (v8 or higher)
- Git
# 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
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);
// 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');
// 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) => { /* ... */ });
// 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');
}
# 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
We welcome contributions! See our Contributing Guide for details on:
- Development workflow
- Project structure
- Testing guidelines
- Pull request process
- Shell command improvements
- Add pipe (
|
) support - Additional built-in commands
- Command argument parsing
- Add pipe (
- 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
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
This project is licensed under the MIT License - see the LICENSE file for details.
- QuickJS - The JavaScript engine used in this project
- quickjs-emscripten - WebAssembly build of QuickJS
- Create an issue for bug reports or feature requests