Overview
The@tinyclaw/sandbox package provides lightweight sandboxed JavaScript/TypeScript code execution for Tiny Claw using Bun Worker threads. It offers sub-millisecond boot time, minimal memory overhead, and a multi-layered security model.
Why Bun Workers?
| Feature | Docker (mrcloudchase) | Bun Workers (Tiny Claw) |
|---|---|---|
| Boot time | 1-3 seconds | <1ms |
| Memory overhead | ~50MB per container | ~1MB per worker |
| Dependency | Docker daemon required | None — built into Bun |
| Portability | Linux/Mac (daemon) | Everywhere Bun runs |
| Teardown | Container cleanup | Worker.terminate() |
Security Model
process,require, andBunglobals are blockedfetch/WebSocketblocked by default (controllable viaallowNet)- Filesystem access blocked by default (controllable via
allowFs) - Configurable timeout with hard kill (default: 10s, max: 30s)
- Each execution runs in a fresh worker (no state leakage)
Installation
Usage
Basic Execution
Execution with Input
Configuration Options
Cleanup
API Reference
createSandbox()
Creates a new sandbox instance.
Returns: Sandbox
Sandbox
Sandbox instance for executing code in isolation.
execute(code, config?)
Execute JavaScript/TypeScript code in a sandboxed environment.
The code to execute. Can use
await for async operations.Execution configuration options.
Execution timeout in milliseconds. Maximum: 30000 (30 seconds).
Allow network access (fetch, WebSocket).
Allow filesystem access.
Execution result containing output, status, and timing information.
executeWithInput(code, input, config?)
Execute code with input data accessible via the input variable.
The code to execute.
Data to pass to the sandbox (available as
input variable).Execution configuration options (same as
execute).Execution result.
shutdown()
Terminate all running workers and clean up resources.
SandboxResult
Result object returned by execution methods.
Whether the code executed successfully without errors or timeout.
The return value of the code (stringified).
Error message if execution failed.
Execution time in milliseconds.