|
1 | | -# backend |
| 1 | +# Stellar IDE Backend |
2 | 2 |
|
3 | | -To install dependencies: |
| 3 | +## Overview |
| 4 | + |
| 5 | +The backend component of the Stellar IDE provides a secure and efficient server for compiling and testing Rust smart contracts for the Stellar blockchain. Built with Node.js, Express, and TypeScript, it handles code compilation requests from the frontend, executes Rust and Stellar CLI commands in a controlled environment, and returns the results to the user. |
| 6 | + |
| 7 | +## Table of Contents |
| 8 | + |
| 9 | +1. [Architecture](#architecture) |
| 10 | +2. [Core Components](#core-components) |
| 11 | +3. [Technologies](#technologies) |
| 12 | +4. [Setup and Installation](#setup-and-installation) |
| 13 | +5. [API Endpoints](#api-endpoints) |
| 14 | +6. [Security Measures](#security-measures) |
| 15 | +7. [Development Workflow](#development-workflow) |
| 16 | +8. [Testing](#testing) |
| 17 | + |
| 18 | +## Architecture |
| 19 | + |
| 20 | +The backend follows a modular architecture with clear separation of concerns: |
| 21 | + |
| 22 | +```mermaid |
| 23 | +graph TD |
| 24 | + A[Express Server] --> B[Controller Layer] |
| 25 | + B --> C[Compilation Service] |
| 26 | + B --> D[Testing Service] |
| 27 | + C --> E[File System Utils] |
| 28 | + D --> E |
| 29 | + C --> F[Process Execution Utils] |
| 30 | + D --> F |
| 31 | + F --> G[Rust Compiler] |
| 32 | + F --> H[Stellar CLI] |
| 33 | +``` |
| 34 | + |
| 35 | +## Core Components |
| 36 | + |
| 37 | +| Component | Description | Responsibility | |
| 38 | +|-----------|-------------|----------------| |
| 39 | +| **Controllers** | API endpoint handlers | Process HTTP requests and responses | |
| 40 | +| **Compilation Service** | Code compilation logic | Manages Rust and WASM compilation workflow | |
| 41 | +| **Testing Service** | Test execution logic | Manages test execution and result formatting | |
| 42 | +| **File System Utils** | File operations | Handles temporary directories and file I/O | |
| 43 | +| **Process Execution** | Command execution | Safely executes shell commands with timeouts | |
| 44 | + |
| 45 | +## Technologies |
| 46 | + |
| 47 | +| Technology | Version | Purpose | |
| 48 | +|------------|---------|----------| |
| 49 | +| Node.js | 20.x | Runtime environment | |
| 50 | +| Express.js | Latest | Web framework | |
| 51 | +| TypeScript | Latest | Programming language | |
| 52 | +| Bun | Latest | Package manager and runtime | |
| 53 | +| Rust | Latest | Smart contract compilation | |
| 54 | +| Stellar CLI | Latest | Contract optimization | |
| 55 | + |
| 56 | +## Setup and Installation |
| 57 | + |
| 58 | +### Prerequisites |
| 59 | + |
| 60 | +- Node.js (v20.x or later) |
| 61 | +- Bun package manager |
| 62 | +- Rust toolchain |
| 63 | +- Stellar CLI |
| 64 | +- WASM target for Rust |
| 65 | + |
| 66 | +### Installation Steps |
4 | 67 |
|
5 | 68 | ```bash |
| 69 | +# Install Rust (if not already installed) |
| 70 | +curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh |
| 71 | + |
| 72 | +# Add WASM target |
| 73 | +rustup target add wasm32-unknown-unknown |
| 74 | + |
| 75 | +# Install Stellar CLI |
| 76 | +cargo install --locked stellar-cli |
| 77 | + |
| 78 | +# Navigate to the backend directory |
| 79 | +cd apps/backend |
| 80 | + |
| 81 | +# Install dependencies |
6 | 82 | bun install |
| 83 | + |
| 84 | +# Start the server |
| 85 | +bun run src/index.ts |
| 86 | +``` |
| 87 | + |
| 88 | +The server will start on port 3000 by default (configurable via environment variables). |
| 89 | + |
| 90 | +## API Endpoints |
| 91 | + |
| 92 | +| Endpoint | Method | Description | Request Body | Response | |
| 93 | +|----------|--------|-------------|--------------|----------| |
| 94 | +| `/api/compile` | POST | Compiles Rust code to WASM | `{ code: string }` | `{ success: boolean, output: string, error?: string }` | |
| 95 | +| `/api/test` | POST | Runs tests for Rust code | `{ code: string }` | `{ success: boolean, output: string, error?: string }` | |
| 96 | +| `/api/health` | GET | Server health check | None | `{ status: "ok" }` | |
| 97 | + |
| 98 | +## Security Measures |
| 99 | + |
| 100 | +### Input Validation |
| 101 | + |
| 102 | +- All incoming requests are validated for proper structure and content |
| 103 | +- Empty or non-string code is rejected |
| 104 | + |
| 105 | +### Command Execution |
| 106 | + |
| 107 | +- Uses `child_process.spawn` instead of `exec` to prevent command injection |
| 108 | +- All commands run with strict timeouts (30 seconds by default) |
| 109 | + |
| 110 | +### File System Safety |
| 111 | + |
| 112 | +- Uses `sanitize-filename` for directory names |
| 113 | +- Creates isolated temporary directories for each compilation/test request |
| 114 | +- Automatically cleans up directories after processing |
| 115 | + |
| 116 | +### API Protection |
| 117 | + |
| 118 | +- Implements Helmet for HTTP header security |
| 119 | +- Configures CORS to restrict access to trusted origins |
| 120 | +- Rate limiting to prevent abuse |
| 121 | + |
| 122 | +## Development Workflow |
| 123 | + |
| 124 | +### Code Organization |
| 125 | + |
| 126 | +``` |
| 127 | +backend/ |
| 128 | +├── src/ |
| 129 | +│ ├── controllers/ |
| 130 | +│ │ ├── compile.controller.ts |
| 131 | +│ │ └── test.controller.ts |
| 132 | +│ ├── utils/ |
| 133 | +│ │ ├── file.utils.ts |
| 134 | +│ │ └── process.utils.ts |
| 135 | +│ ├── services/ |
| 136 | +│ │ ├── compilation.service.ts |
| 137 | +│ │ └── testing.service.ts |
| 138 | +│ ├── middleware/ |
| 139 | +│ │ ├── validation.middleware.ts |
| 140 | +│ │ └── error.middleware.ts |
| 141 | +│ └── index.ts |
| 142 | +├── tests/ |
| 143 | +│ ├── unit/ |
| 144 | +│ └── integration/ |
| 145 | +├── tsconfig.json |
| 146 | +└── package.json |
7 | 147 | ``` |
8 | 148 |
|
9 | | -To run: |
| 149 | +## Testing |
| 150 | + |
| 151 | +### Unit Tests |
10 | 152 |
|
11 | 153 | ```bash |
12 | | -bun run index.ts |
| 154 | +bun test |
13 | 155 | ``` |
14 | 156 |
|
15 | | -This project was created using `bun init` in bun v1.2.19. [Bun](https://bun.com) is a fast all-in-one JavaScript runtime. |
| 157 | +### Integration Tests |
| 158 | + |
| 159 | +```bash |
| 160 | +bun test:integration |
| 161 | +``` |
| 162 | + |
| 163 | +## Performance Considerations |
| 164 | + |
| 165 | +| Aspect | Implementation | Benefit | |
| 166 | +|--------|---------------|--------| |
| 167 | +| **Concurrency** | Worker threads for compilation | Handles multiple requests efficiently | |
| 168 | +| **Caching** | In-memory cache for repeated compilations | Reduces compilation time for identical code | |
| 169 | +| **Timeouts** | Configurable command timeouts | Prevents resource exhaustion | |
| 170 | +| **Resource Limits** | Memory and CPU constraints | Ensures system stability | |
| 171 | + |
| 172 | +## Error Handling |
| 173 | + |
| 174 | +The backend implements a comprehensive error handling strategy: |
| 175 | + |
| 176 | +1. **Validation Errors**: Returns 400 Bad Request with details |
| 177 | +2. **Compilation Errors**: Returns 200 OK with error details in response |
| 178 | +3. **System Errors**: Returns 500 Internal Server Error with safe error message |
| 179 | +4. **Timeout Errors**: Returns 408 Request Timeout when commands exceed time limit |
| 180 | + |
| 181 | +## Environment Variables |
| 182 | + |
| 183 | +| Variable | Description | Default | |
| 184 | +|----------|-------------|--------| |
| 185 | +| `PORT` | Server port | 3000 | |
| 186 | +| `CORS_ORIGIN` | Allowed CORS origin | http://localhost:4200 | |
| 187 | +| `COMMAND_TIMEOUT` | Command execution timeout (ms) | 30000 | |
| 188 | +| `MAX_PAYLOAD_SIZE` | Maximum request body size | 1mb | |
| 189 | + |
| 190 | +## Additional Resources |
| 191 | + |
| 192 | +- [Express.js Documentation](https://expressjs.com/) |
| 193 | +- [Rust Documentation](https://www.rust-lang.org/learn) |
| 194 | +- [Stellar Soroban Documentation](https://developers.stellar.org/) |
0 commit comments