|
| 1 | +# Repository Guideline |
| 2 | + |
| 3 | +## Project Overview |
| 4 | + |
| 5 | +Piston is a high-performance, high-security code execution engine supporting 90+ programming languages. It enables safe code execution via an API server and operates in a sandboxed environment using Linux Isolate (namespaces + chroot + cgroup). |
| 6 | + |
| 7 | +## Development Commands |
| 8 | + |
| 9 | +### Starting and Stopping the Environment |
| 10 | + |
| 11 | +```bash |
| 12 | +./piston select dev # Select development environment (first time only) |
| 13 | +./piston start # Start |
| 14 | +./piston stop # Stop |
| 15 | +./piston restart # Restart |
| 16 | +./piston logs # Show logs |
| 17 | +./piston bash # Open container shell |
| 18 | +./piston rebuild # Build and restart |
| 19 | +``` |
| 20 | +
|
| 21 | +### Code Formatting (Lint) |
| 22 | +
|
| 23 | +```bash |
| 24 | +./piston lint # Format all files with Prettier |
| 25 | +npx prettier --write <path> # Format specific files only |
| 26 | +``` |
| 27 | +
|
| 28 | +### Package Management |
| 29 | +
|
| 30 | +```bash |
| 31 | +./piston list-pkgs # List available packages |
| 32 | +./piston build-pkg <pkg> <ver> # Build package |
| 33 | +./piston clean-pkgs # Clean build artifacts |
| 34 | +``` |
| 35 | +
|
| 36 | +### CLI Setup |
| 37 | +
|
| 38 | +```bash |
| 39 | +cd cli && npm i && cd - |
| 40 | +``` |
| 41 | +
|
| 42 | +## Architecture |
| 43 | +
|
| 44 | +``` |
| 45 | +┌─────────────────────────────────────────────────────────────┐ |
| 46 | +│ Docker Container │ |
| 47 | +├─────────────────────────────────────────────────────────────┤ |
| 48 | +│ api/ cli/ │ |
| 49 | +│ ├─ Express Server ├─ yargs CLI │ |
| 50 | +│ ├─ Routes (api/v2.js) └─ commands/ │ |
| 51 | +│ ├─ Job Manager (job.js) ├─ execute.js │ |
| 52 | +│ ├─ Runtime Manager └─ ppman.js │ |
| 53 | +│ └─ Package Manager │ |
| 54 | +├─────────────────────────────────────────────────────────────┤ |
| 55 | +│ Isolate Sandbox │ |
| 56 | +│ (Linux namespaces + chroot + cgroup) │ |
| 57 | +├─────────────────────────────────────────────────────────────┤ |
| 58 | +│ packages/ │ |
| 59 | +│ └─ <lang>/<version>/ │ |
| 60 | +│ ├─ metadata.json (language info, aliases) │ |
| 61 | +│ ├─ build.sh (build script) │ |
| 62 | +│ ├─ run (execution script) │ |
| 63 | +│ └─ environment (environment variables) │ |
| 64 | +└─────────────────────────────────────────────────────────────┘ |
| 65 | +``` |
| 66 | +
|
| 67 | +### Main Components |
| 68 | +
|
| 69 | +| File | Role | |
| 70 | +|---------|------| |
| 71 | +| `api/src/index.js` | Express server initialization | |
| 72 | +| `api/src/api/v2.js` | API endpoint definitions | |
| 73 | +| `api/src/job.js` | Job execution management (READY → PRIMED → EXECUTED) | |
| 74 | +| `api/src/runtime.js` | Language runtime management | |
| 75 | +| `api/src/package.js` | Package installation and management | |
| 76 | +| `api/src/config.js` | Configuration management via environment variables | |
| 77 | +
|
| 78 | +### API Endpoints |
| 79 | +
|
| 80 | +| Method | Path | Purpose | |
| 81 | +|---------|------|------| |
| 82 | +| GET | `/api/v2/runtimes` | List installed languages | |
| 83 | +| POST | `/api/v2/execute` | Execute code | |
| 84 | +| WebSocket | `/api/v2/connect` | Interactive execution | |
| 85 | +
|
| 86 | +## Configuration (Environment Variables) |
| 87 | +
|
| 88 | +Main environment variables (`PISTON_` prefix): |
| 89 | +
|
| 90 | +| Variable | Default | Description | |
| 91 | +|-----|---------|------| |
| 92 | +| `PISTON_LOG_LEVEL` | INFO | Log level | |
| 93 | +| `PISTON_BIND_ADDRESS` | 0.0.0.0:2000 | API bind address | |
| 94 | +| `PISTON_DISABLE_NETWORKING` | true | Disable networking | |
| 95 | +| `PISTON_COMPILE_TIMEOUT` | 10000 | Compile timeout (ms) | |
| 96 | +| `PISTON_RUN_TIMEOUT` | 3000 | Execution timeout (ms) | |
| 97 | +| `PISTON_MAX_PROCESS_COUNT` | 64 | Maximum process count | |
| 98 | +| `PISTON_OUTPUT_MAX_SIZE` | 1024 | Maximum output size | |
| 99 | +
|
| 100 | +See `docs/configuration.md` for details. |
| 101 | +
|
| 102 | +## Testing |
| 103 | +
|
| 104 | +Security tests are located in `/tests/`: |
| 105 | +
|
| 106 | +```bash |
| 107 | +python3 tests/fork.py # Fork bomb test |
| 108 | +python3 tests/fallocate.py # Disk fill attack test |
| 109 | +python3 tests/network.py # Network access test |
| 110 | +``` |
| 111 | +
|
| 112 | +Package tests are automatically executed via GitHub Actions (`package-pr.yaml`). |
| 113 | +
|
| 114 | +## Adding Language Packages |
| 115 | +
|
| 116 | +1. Create `packages/<lang>/<version>/` directory |
| 117 | +2. Create required files: |
| 118 | + - `metadata.json` - Language name, version, aliases |
| 119 | + - `build.sh` - Build script |
| 120 | + - `run` - Execution script |
| 121 | +3. Build with `./piston build-pkg <lang> <version>` |
| 122 | +4. Add badge to README.md |
| 123 | +
|
| 124 | +## Code Style |
| 125 | +
|
| 126 | +Prettier configuration (`.prettierrc.yaml`): |
| 127 | +- Use single quotes |
| 128 | +- Tab width: 4 |
| 129 | +- Omit arrow function parentheses |
| 130 | +
|
| 131 | +### Commit Messages |
| 132 | +
|
| 133 | +- **Do not use Conventional Commits** (no need for prefixes like `fix:`, `feat:`) |
| 134 | +- Write in normal format with concise description of changes |
| 135 | +
|
| 136 | +## GitHub Actions |
| 137 | +
|
| 138 | +### permissions Configuration |
| 139 | +
|
| 140 | +Workflows using ghcr.io or GitHub Releases require explicit permissions configuration: |
| 141 | +
|
| 142 | +```yaml |
| 143 | +jobs: |
| 144 | + job_name: |
| 145 | + runs-on: ubuntu-latest |
| 146 | + permissions: |
| 147 | + contents: write # When uploading to releases |
| 148 | + packages: write # When pushing to ghcr.io |
| 149 | + packages: read # When pulling from ghcr.io |
| 150 | +``` |
| 151 | +
|
| 152 | +### Docker Image Workflows |
| 153 | +
|
| 154 | +| Workflow | Purpose | Trigger Path | |
| 155 | +|-------------|------|-------------| |
| 156 | +| `api-push.yaml` | API image | `api/**` | |
| 157 | +| `repo-push.yaml` | Repo Builder image | `repo/**` | |
| 158 | +| `package-push.yaml` | Package build | `packages/**` | |
| 159 | +
|
| 160 | +## Prerequisites |
| 161 | +
|
| 162 | +- Docker & Docker Compose |
| 163 | +- cgroup v2 enabled (cgroup v1 disabled) |
| 164 | +- Node.js >= 15 (for CLI development) |
0 commit comments