home / skills / secondsky / claude-skills / cloudflare-workers-multi-lang

This skill guides multi-language Cloudflare Workers development by integrating Rust, Python, and WASM to boost performance and flexibility.

npx playbooks add skill secondsky/claude-skills --skill cloudflare-workers-multi-lang

Review the files below or copy the command above to add this skill to your agents.

Files (11)
SKILL.md
5.0 KB
---
name: workers-multi-lang
description: Multi-language Workers development with Rust, Python, and WebAssembly. Use when building Workers in languages other than JavaScript/TypeScript, or when integrating WASM modules for performance-critical code.
version: 1.0.0
---

# Multi-Language Workers Development

Build Cloudflare Workers using Rust, Python, or WebAssembly for performance-critical operations.

## Language Comparison

| Feature | JavaScript/TS | Rust | Python |
|---------|---------------|------|--------|
| **Startup** | Fast | Fastest (WASM) | Moderate |
| **CPU Perf** | Good | Excellent | Good |
| **Memory** | Higher | Lower | Higher |
| **Bundle Size** | Smaller | Medium | Larger |
| **Type Safety** | Optional (TS) | Strict | Optional |
| **Best For** | General apps | CPU-intensive | Data/ML |

## Quick Decision

```
Need maximum performance? → Rust/WASM
Heavy computation (crypto, image processing)? → Rust/WASM
Data processing, ML inference? → Python
General web apps? → JavaScript/TypeScript
```

## Top 10 Multi-Lang Errors

| Error | Language | Cause | Solution |
|-------|----------|-------|----------|
| `WebAssembly.instantiate() failed` | Rust | Invalid WASM | Check wasm-pack build output |
| `Module parse failed: Unexpected token` | Rust | ESM/CJS mismatch | Use `--target bundler` |
| `Cannot find module` | Python | Missing dep | Add to pyproject.toml |
| `Out of memory` | All | Large WASM | Enable streaming instantiation |
| `Exceeded CPU time limit` | All | Long computation | Chunk processing |
| `wasm-bindgen version mismatch` | Rust | Dep conflict | Align versions in Cargo.toml |
| `RuntimeError: unreachable` | Rust | Panic in WASM | Add proper error handling |
| `TypeError: not a function` | Rust | Missing export | Add `#[wasm_bindgen]` attribute |
| `Python worker startup timeout` | Python | Slow init | Minimize imports |
| `SharedArrayBuffer not supported` | All | Security | Add COOP/COEP headers |

## Rust Quick Start

```bash
# Install tools
cargo install wasm-pack

# Create project
cargo new --lib my-worker
cd my-worker

# Add to Cargo.toml
cat >> Cargo.toml << 'EOF'
[lib]
crate-type = ["cdylib"]

[dependencies]
wasm-bindgen = "0.2"
worker = "0.3"
console_error_panic_hook = "0.1"

[profile.release]
opt-level = "s"
lto = true
EOF
```

```rust
// src/lib.rs
use worker::*;

#[event(fetch)]
async fn fetch(req: Request, env: Env, _ctx: Context) -> Result<Response> {
    console_error_panic_hook::set_once();

    Router::new()
        .get("/", |_, _| Response::ok("Hello from Rust!"))
        .get("/compute", |_, _| {
            // CPU-intensive computation
            let result = heavy_computation();
            Response::ok(format!("Result: {}", result))
        })
        .run(req, env)
        .await
}

fn heavy_computation() -> u64 {
    (1..1_000_000).filter(|n| is_prime(*n)).count() as u64
}

fn is_prime(n: u64) -> bool {
    if n < 2 { return false; }
    (2..=(n as f64).sqrt() as u64).all(|i| n % i != 0)
}
```

## Python Quick Start (Workers for Platforms)

```toml
# pyproject.toml
[project]
name = "my-worker"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = []

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
```

```python
# src/entry.py
from js import Response, Headers

async def on_fetch(request, env):
    url = request.url

    if "/compute" in url:
        result = heavy_computation()
        return Response.new(f"Result: {result}")

    return Response.new("Hello from Python!")

def heavy_computation():
    """CPU-intensive computation"""
    return sum(1 for n in range(2, 100000) if is_prime(n))

def is_prime(n):
    if n < 2:
        return False
    return all(n % i != 0 for i in range(2, int(n**0.5) + 1))
```

## WASM Module Integration

```typescript
// Load and use WASM module in TypeScript Worker
import wasmModule from './pkg/my_lib_bg.wasm';
import { init, process_data } from './pkg/my_lib';

let wasmInstance: WebAssembly.Instance;

export default {
  async fetch(request: Request, env: Env): Promise<Response> {
    // Initialize WASM once
    if (!wasmInstance) {
      wasmInstance = await WebAssembly.instantiate(wasmModule);
      init();
    }

    // Use WASM function
    const result = process_data(inputData);

    return Response.json({ result });
  },
};
```

## When to Load References

| Reference | Load When |
|-----------|-----------|
| `references/rust-workers.md` | Building Workers with Rust/WASM |
| `references/python-workers.md` | Using Python on Workers for Platforms |
| `references/wasm-integration.md` | Integrating WASM modules in any Worker |

## Performance Tips

1. **WASM Initialization**: Cache instance, use streaming
2. **Memory**: Use typed arrays for data transfer
3. **Bundle Size**: Enable LTO, strip debug info
4. **Cold Starts**: Keep WASM modules small
5. **Data Transfer**: Minimize JS/WASM boundary crossings

## See Also

- `workers-performance` - General optimization techniques
- `workers-testing` - Testing multi-language Workers
- `cloudflare-worker-base` - Basic Workers setup

Overview

This skill enables building Cloudflare Workers using Rust, Python, and WebAssembly to handle performance-critical tasks beyond JavaScript/TypeScript. It provides language guidance, quick-starts, troubleshooting for common multi-language errors, and patterns for integrating WASM modules into Workers. Use it to pick the right language, optimize runtime behavior, and ship production-ready multi-language Workers.

How this skill works

The skill inspects language-specific tradeoffs and supplies starter configurations, example handlers, and integration patterns for Rust, Python, and WASM in Worker environments. It offers practical fixes for the top runtime and build errors, plus performance tips like caching WASM instances, streaming instantiation, and minimizing JS/WASM boundary crossings. It also includes quick-start snippets and recommended dependency settings for fast development and deployment.

When to use it

  • You need maximum CPU performance or low-memory footprint (use Rust/WASM).
  • You want to run Python code or ML/data-processing within a Worker platform.
  • You must integrate a compiled WASM module for heavy computation or legacy native code.
  • You face build/runtime errors when mixing ESM/CJS, wasm-bindgen, or Python startup issues.
  • You want production optimizations: LTO, stripped builds, streaming WASM instantiation.

Best practices

  • Cache and reuse WASM instances; prefer streaming instantiation to save memory and startup time.
  • Minimize imports and heavyweight initialization in Python to avoid startup timeouts.
  • Use typed arrays and minimize crossings between JS and WASM for large data transfers.
  • Enable LTO and strip debug info for Rust release builds to reduce bundle size and improve performance.
  • Break long computations into chunks or offload to background workers to avoid CPU/time limits.

Example use cases

  • A Rust/WASM Worker that performs image processing or cryptographic operations for an API endpoint.
  • A Python Worker that runs lightweight ML inference or data aggregation near users.
  • A TypeScript Worker that initializes and calls a compiled WASM module for hot CPU paths.
  • Porting a CPU-heavy Node.js function to Rust/WASM to reduce memory and improve throughput.
  • Handling large binary datasets with typed arrays and streaming WASM instantiation for low-latency processing.

FAQ

Which language gives the best performance for CPU-bound tasks?

Rust compiled to WASM generally provides the best CPU performance and lowest memory use for CPU-bound workloads.

How do I avoid Python worker startup timeouts?

Minimize top-level imports, lazy-load heavy libraries, and keep the runtime initialization lightweight to reduce startup time.

What fixes a WebAssembly.instantiate() failed error?

Verify the wasm-pack build output, use the correct target (e.g., --target bundler), ensure wasm-bindgen versions match, and enable streaming instantiation.