A progressive Node.js framework for building efficient and scalable server-side applications.
- - +## 架构 -## Description - -[Nest](https://github.com/nestjs/nest) framework TypeScript starter repository. - -## Installation - -```bash -$ pnpm install +``` +HTTP Request → Hono Server → Process Pool → Worker (long-lived) → Result + ↓ + ┌──────────────┐ + │ JS Workers │ bun run worker.ts (×N) + │ Py Workers │ python3 worker.py (×N) + └──────────────┘ + stdin: JSON task → stdout: JSON result ``` -## Running the app +- **进程池**:启动时预热 N 个 worker 进程(默认 20),请求到达时直接分配空闲 worker,执行完归还池中 +- **JS 执行**:Bun worker 进程 + 安全 shim(禁用 Bun API、冻结 Function 构造器、require 白名单) +- **Python 执行**:python3 worker 进程 + `__import__` 拦截 + resource 资源限制 +- **网络请求**:统一通过 `SystemHelper.httpRequest()` / `system_helper.http_request()` 收口,内置 SSRF 防护 +- **并发控制**:请求数超过池大小时自动排队,worker 崩溃自动重启补充 + +## 性能 + +进程池 vs 旧版 spawn-per-request 对比(SANDBOX_POOL_SIZE=20): + +| 场景 | 旧版 QPS / P50 | 进程池 QPS / P50 | 提升 | +|------|----------------|------------------|------| +| JS 简单函数 (c50) | 22 / 1,938ms | 1,414 / 7ms | **64x** | +| JS IO 500ms (c50) | 22 / 2,107ms | 38 / 1,005ms | 1.7x | +| JS 高 CPU (c10) | 9 / 1,079ms | 12 / 796ms | 1.3x | +| JS 高内存 (c10) | — | 13 / 787ms | — | +| Python 简单函数 (c50) | 14.7 / 2,897ms | 4,247 / 4ms | **289x** | +| Python IO 500ms (c50) | 14.2 / 3,066ms | 38 / 1,003ms | 2.7x | +| Python 高 CPU (c10) | 3.1 / 2,845ms | 4 / 2,191ms | 1.3x | +| Python 高内存 (c10) | — | 11 / 893ms | — | + +资源占用(20+20 workers):空闲 ~1.5GB RSS,压测峰值 ~2GB RSS。 + +## 快速开始 ```bash -# development -$ pnpm run start +# 安装依赖 +bun install -# watch mode -$ pnpm run start:dev +# 开发运行 +bun run src/index.ts -# production mode -$ pnpm run start:prod +# 运行测试 +bun run test ``` -## Test +## Docker ```bash -# unit tests -$ pnpm run test +# 构建 +docker build -f projects/sandbox/Dockerfile -t fastgpt-sandbox . -# e2e tests -$ pnpm run test:e2e - -# test coverage -$ pnpm run test:cov +# 运行 +docker run -p 3000:3000 \ + -e SANDBOX_TOKEN=your-secret-token \ + -e SANDBOX_POOL_SIZE=20 \ + fastgpt-sandbox ``` -## Support +## API -Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support). +### `POST /sandbox/js` -## Stay in touch +执行 JavaScript 代码。 -- Author - [Kamil Myśliwiec](https://kamilmysliwiec.com) -- Website - [https://nestjs.com](https://nestjs.com/) -- Twitter - [@nestframework](https://twitter.com/nestframework) +```json +{ + "code": "async function main(variables) {\n return { result: variables.a + variables.b }\n}", + "variables": { "a": 1, "b": 2 } +} +``` -## License +### `POST /sandbox/python` -Nest is [MIT licensed](LICENSE). +执行 Python 代码。 + +```json +{ + "code": "def main(variables):\n return {'result': variables['a'] + variables['b']}", + "variables": { "a": 1, "b": 2 } +} +``` + +### `GET /health` + +健康检查,返回进程池状态。 + +```json +{ + "status": "ok", + "version": "5.0.0", + "jsPool": { "total": 20, "idle": 18, "busy": 2, "queued": 0 }, + "pythonPool": { "total": 20, "idle": 20, "busy": 0, "queued": 0 } +} +``` + +### 响应格式 + +成功: + +```json +{ + "success": true, + "data": { + "codeReturn": { "result": 3 }, + "log": "console.log 输出内容" + } +} +``` + +失败: + +```json +{ + "success": false, + "message": "错误信息" +} +``` + +## 环境变量 + +### 服务配置 + +| 变量 | 说明 | 默认值 | +|------|------|--------| +| `SANDBOX_PORT` | 服务端口 | `3000` | +| `SANDBOX_TOKEN` | Bearer Token 认证密钥 | 空(不鉴权) | + +### 进程池 + +| 变量 | 说明 | 默认值 | +|------|------|--------| +| `SANDBOX_POOL_SIZE` | 每种语言的 worker 进程数 | `20` | + +### 资源限制 + +| 变量 | 说明 | 默认值 | +|------|------|--------| +| `SANDBOX_MAX_TIMEOUT` | 超时上限(ms),请求不可超过此值 | `60000` | +| `SANDBOX_MAX_MEMORY_MB` | 内存上限(MB) | `256` | + +### 网络请求限制 + +| 变量 | 说明 | 默认值 | +|------|------|--------| +| `SANDBOX_REQUEST_MAX_COUNT` | 单次执行最大 HTTP 请求数 | `30` | +| `SANDBOX_REQUEST_TIMEOUT` | 单次 HTTP 请求超时(ms) | `60000` | +| `SANDBOX_REQUEST_MAX_RESPONSE_MB` | 最大响应体大小(MB) | `10` | +| `SANDBOX_REQUEST_MAX_BODY_MB` | 最大请求体大小(MB) | `5` | + +## 项目结构 + +``` +src/ +├── index.ts # 入口:Hono 服务 + 进程池初始化 +├── env.ts # 环境变量校验(zod) +├── config.ts # 配置导出 +├── types.ts # 类型定义 +├── pool/ +│ ├── process-pool.ts # JS 进程池管理 +│ ├── python-process-pool.ts # Python 进程池管理 +│ ├── worker.ts # JS worker(长驻进程,含安全 shim) +│ └── worker.py # Python worker(长驻进程,含安全沙箱) +└── utils/ + └── semaphore.ts # 信号量(通用并发控制) + +test/ +├── unit/ # 单元测试(进程池、信号量) +├── integration/ # 集成测试(API 路由) +├── boundary/ # 边界测试(超时、内存限制) +├── security/ # 安全测试(沙箱逃逸防护) +├── compat/ # 兼容性测试(旧版代码格式) +├── examples/ # 示例测试(常用包) +└── benchmark/ # 压测脚本 +``` + +## 添加 JS 包 + +沙盒内的 JS 代码通过 `require()` 加载包,但仅允许白名单内的包。 + +### 当前白名单 + +`lodash`、`dayjs`、`moment`、`uuid`、`crypto-js`、`qs`、`url`、`querystring` + +### 添加新包步骤 + +1. **安装包**: + +```bash +cd projects/sandbox +bun add