Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions code-executor/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
node_modules
dist
.temp
*.log
.git
.gitignore
README.md
.env*

7 changes: 7 additions & 0 deletions code-executor/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
node_modules/
dist/
.temp/
.env
.env.*
*.log

29 changes: 29 additions & 0 deletions code-executor/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
FROM node:20-slim

RUN apt-get update && apt-get install -y --no-install-recommends \
ca-certificates \
&& rm -rf /var/lib/apt/lists/*

RUN groupadd -r executor && useradd -r -g executor executor

WORKDIR /app

COPY package*.json ./
RUN npm ci --only=production

COPY tsconfig.json ./
COPY src ./src

RUN npm run build || npx tsc
Comment on lines +11 to +17
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Build will fail: TypeScript not available.

The Dockerfile installs only production dependencies (npm ci --only=production) before building, but TypeScript is typically a dev dependency. The build step on Line 17 will fail because the TypeScript compiler won't be installed.

🔎 Recommended fix using multi-stage build
 FROM node:20-slim
 
 RUN apt-get update && apt-get install -y --no-install-recommends \
     ca-certificates \
     && rm -rf /var/lib/apt/lists/*
 
-RUN groupadd -r executor && useradd -r -g executor executor
-
 WORKDIR /app
 
 COPY package*.json ./
-RUN npm ci --only=production
+RUN npm ci
 
 COPY tsconfig.json ./
 COPY src ./src
 
-RUN npm run build || npx tsc
+RUN npm run build
+
+# Production stage
+FROM node:20-slim
+
+RUN apt-get update && apt-get install -y --no-install-recommends \
+    ca-certificates \
+    && rm -rf /var/lib/apt/lists/*
+
+RUN groupadd -r executor && useradd -r -g executor executor
+
+WORKDIR /app
+
+COPY package*.json ./
+RUN npm ci --only=production
+
+COPY --from=0 /app/dist ./dist
 
 RUN mkdir -p .temp && chown -R executor:executor /app
 
 USER executor
 
 ENV NODE_ENV=production
 ENV PORT=3040
 
 EXPOSE 3040
 
 CMD ["node", "dist/server.js"]

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In code-executor/Dockerfile around lines 11 to 17, the Dockerfile runs npm ci
--only=production before a TypeScript build so the tsc compiler (a
devDependency) is not installed and the build will fail; fix by using a
multi-stage build or installing devDependencies for the build stage: in the
first stage install all dependencies (no --only=production) or run npm ci, run
the TypeScript build, then in a final runtime stage copy only the produced dist
and production deps (or run npm prune --production) so the final image contains
only production packages while the build stage has tsc.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major

Remove the build command fallback.

The fallback pattern || npx tsc masks failures from the primary npm run build command. If the build script fails for any reason (missing dependencies, TypeScript errors, etc.), the fallback will execute and potentially hide the actual error.

🔎 Proposed fix
-RUN npm run build || npx tsc
+RUN npm run build
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
RUN npm run build || npx tsc
RUN npm run build
🤖 Prompt for AI Agents
In code-executor/Dockerfile around line 17, the RUN step uses "npm run build ||
npx tsc" which masks failures from the primary build; remove the fallback and
change the step to run only "npm run build" so the Docker build fails on real
build errors, and ensure any required dev/build dependencies are installed
earlier in the Dockerfile so the build can run successfully.


RUN mkdir -p .temp && chown -R executor:executor /app

USER executor

ENV NODE_ENV=production
ENV PORT=3040

EXPOSE 3040

CMD ["node", "dist/server.js"]

124 changes: 124 additions & 0 deletions code-executor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# Light Code Executor

Isolated code execution service for Light Protocol documentation.

## Security Features

- API key authentication required
- Rate limiting (30 req/min)
- Code pattern blocking (no fs, child_process, eval, etc.)
- Execution timeout (30s)
- Output size limits (50KB)
- Memory limits (128MB heap)
- Runs as non-root user in Docker
- Read-only filesystem with tmpfs for execution

## Environment Variables

```bash
# Required: API key for authenticating requests
EXECUTOR_API_KEY=your-secure-api-key-here

# Optional: Port (default: 3040)
PORT=3040
```

## Local Development

```bash
cd code-executor

# Install dependencies
npm install

# Set environment variable
export EXECUTOR_API_KEY=dev-key-12345

# Run dev server
npm run dev
```

## Docker Deployment

### Build and Run

```bash
# Build image
docker build -t light-code-executor .

# Run container
docker run -d \
--name code-executor \
-p 3040:3040 \
-e EXECUTOR_API_KEY=your-secure-key \
--memory=512m \
--cpus=0.5 \
--read-only \
--tmpfs /app/.temp:size=50M \
--security-opt=no-new-privileges:true \
light-code-executor
```

### Using Docker Compose

```bash
export EXECUTOR_API_KEY=your-secure-key
docker-compose up -d
```

## Deployment Options

### Railway

1. Connect your repo
2. Set `EXECUTOR_API_KEY` environment variable
3. Deploy

### Fly.io

```bash
fly launch
fly secrets set EXECUTOR_API_KEY=your-secure-key
fly deploy
```

### Render

1. Create new Web Service
2. Set environment variables
3. Deploy from Docker

## API

### Health Check

```
GET /health
Response: { "status": "ok" }
```

### Execute TypeScript

```
POST /typescript
Headers:
Authorization: Bearer <EXECUTOR_API_KEY>
Content-Type: application/json

Body:
{ "code": "console.log('Hello');" }

Response:
{ "stdout": "Hello", "stderr": "" }
```

## Integration with code-runner

The code-runner Next.js app proxies requests to this service. Configure:

```bash
# In code-runner environment
CODE_EXECUTOR_URL=https://your-executor-url.com
EXECUTOR_API_KEY=same-key-as-executor
```

22 changes: 22 additions & 0 deletions code-executor/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: "3.8"

services:
code-executor:
build: .
ports:
- "3040:3040"
environment:
- EXECUTOR_API_KEY=${EXECUTOR_API_KEY}
- PORT=3040
restart: unless-stopped
deploy:
resources:
limits:
memory: 512M
cpus: "0.5"
security_opt:
- no-new-privileges:true
read_only: true
tmpfs:
- /app/.temp:size=50M,mode=1777

21 changes: 21 additions & 0 deletions code-executor/fly.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
app = "light-code-executor"
primary_region = "iad"

[build]
dockerfile = "Dockerfile"

[env]
PORT = "3040"

[http_service]
internal_port = 3040
force_https = true
auto_stop_machines = true
auto_start_machines = true
min_machines_running = 0

[[vm]]
cpu_kind = "shared"
cpus = 1
memory_mb = 512

Loading