@buddhilive/sandbox 0.1.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +110 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +219 -0
- package/dist/index.js +351 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 BuddhiAI
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# @buddhilive/sandbox
|
|
2
|
+
|
|
3
|
+
> Ultra-lightweight, zero-backend, client-side Node.js sandbox running entirely in WebAssembly & Web Workers.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- ⚡ **Zero Backend**: Runs 100% in the user's browser using WebAssembly and dedicated Web Workers.
|
|
8
|
+
- 📁 **In-Memory POSIX VirtualFS**: Synchronous and asynchronous filesystem APIs (`readFile`, `writeFile`, `mkdir`, `readdir`, `stat`, `symlink`).
|
|
9
|
+
- 🌐 **In-Browser Web Application Previews**: Intercepts HTTP/TCP listeners (`http.createServer().listen(3000)`) via Service Worker and renders them live at `/__preview/:port/` inside `<iframe>` tags.
|
|
10
|
+
- 📦 **Client-Side NPM Package Installer**: Installs packages directly from `registry.npmjs.org` using pure-JS decompression (`fflate`).
|
|
11
|
+
- 🛠️ **On-Demand Dynamic Native Toolchain**: Lazily loads Python and Clang/Musl WASM compilers only when packages require `node-gyp` builds, keeping initial bundle size minimal.
|
|
12
|
+
- 🔒 **Cross-Origin Opener / Embedder Isolated**: High-performance I/O streaming using `SharedArrayBuffer` lock-free ring buffers and Atomics.
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @buddhilive/sandbox
|
|
20
|
+
# or
|
|
21
|
+
pnpm add @buddhilive/sandbox
|
|
22
|
+
# or
|
|
23
|
+
yarn add @buddhilive/sandbox
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## Quickstart
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { Sandbox } from '@buddhilive/sandbox';
|
|
32
|
+
|
|
33
|
+
// 1. Initialize sandbox instance
|
|
34
|
+
const sandbox = await Sandbox.create();
|
|
35
|
+
|
|
36
|
+
// 2. Write a script to the in-memory VirtualFS
|
|
37
|
+
await sandbox.fs.writeFile(
|
|
38
|
+
'/workspace/index.js',
|
|
39
|
+
`console.log("Hello from inside the client-side Node.js Sandbox!");`
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
// 3. Spawn a process and stream stdout
|
|
43
|
+
const proc = await sandbox.process.spawn('node', ['/workspace/index.js']);
|
|
44
|
+
|
|
45
|
+
const reader = proc.stdout.getReader();
|
|
46
|
+
const decoder = new TextDecoder();
|
|
47
|
+
|
|
48
|
+
while (true) {
|
|
49
|
+
const { value, done } = await reader.read();
|
|
50
|
+
if (done) break;
|
|
51
|
+
console.log(decoder.decode(value));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const exitCode = await proc.exit;
|
|
55
|
+
console.log('Exited with code:', exitCode);
|
|
56
|
+
|
|
57
|
+
// 4. Clean up
|
|
58
|
+
await sandbox.dispose();
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
## Web Server Previews
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
// Listen for virtual port events
|
|
67
|
+
sandbox.ports.on('listen', ({ port, url }) => {
|
|
68
|
+
console.log(`Port ${port} opened at preview URL: ${url}`);
|
|
69
|
+
previewIframe.src = url;
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// Run a web server inside the sandbox
|
|
73
|
+
await sandbox.fs.writeFile('/workspace/server.js', `
|
|
74
|
+
const http = require('http');
|
|
75
|
+
const server = http.createServer((req, res) => {
|
|
76
|
+
res.writeHead(200, { 'Content-Type': 'text/html' });
|
|
77
|
+
res.end('<h1>Live Preview from In-Browser Sandbox!</h1>');
|
|
78
|
+
});
|
|
79
|
+
server.listen(3000);
|
|
80
|
+
`);
|
|
81
|
+
|
|
82
|
+
await sandbox.process.spawn('node', ['/workspace/server.js']);
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
---
|
|
86
|
+
|
|
87
|
+
## Architecture Overview
|
|
88
|
+
|
|
89
|
+
```text
|
|
90
|
+
┌─────────────────────────────────────────────────────────────┐
|
|
91
|
+
│ Browser Main Thread (Host Application) │
|
|
92
|
+
│ - import { Sandbox } from '@buddhilive/sandbox' │
|
|
93
|
+
│ - sandbox.fs / sandbox.process / sandbox.ports │
|
|
94
|
+
└──────────────┬──────────────────────────────▲───────────────┘
|
|
95
|
+
│ postMessage │ SAB Streams
|
|
96
|
+
▼ │
|
|
97
|
+
┌─────────────────────────────────────────────┴───────────────┐
|
|
98
|
+
│ Web Worker (Execution Sandbox) │
|
|
99
|
+
│ - Rust WebAssembly (buddhilive-sandbox-core) │
|
|
100
|
+
│ - In-Memory VirtualFS (Inode Table) │
|
|
101
|
+
│ - QuickJS Virtual Process Context │
|
|
102
|
+
│ - SPSC Lock-Free SharedArrayBuffer Ring Buffer │
|
|
103
|
+
└─────────────────────────────────────────────────────────────┘
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
---
|
|
107
|
+
|
|
108
|
+
## License
|
|
109
|
+
|
|
110
|
+
MIT © BuddhiLive
|