Loading...
Loading...
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.
npx skill4agent add secondsky/claude-skills workers-multi-lang| 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 |
Need maximum performance? → Rust/WASM
Heavy computation (crypto, image processing)? → Rust/WASM
Data processing, ML inference? → Python
General web apps? → JavaScript/TypeScript| Error | Language | Cause | Solution |
|---|---|---|---|
| Rust | Invalid WASM | Check wasm-pack build output |
| Rust | ESM/CJS mismatch | Use |
| Python | Missing dep | Add to pyproject.toml |
| All | Large WASM | Enable streaming instantiation |
| All | Long computation | Chunk processing |
| Rust | Dep conflict | Align versions in Cargo.toml |
| Rust | Panic in WASM | Add proper error handling |
| Rust | Missing export | Add |
| Python | Slow init | Minimize imports |
| All | Security | Add COOP/COEP headers |
# 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// 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)
}# pyproject.toml
[project]
name = "my-worker"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = []
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"# 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))// 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 });
},
};| Reference | Load When |
|---|---|
| Building Workers with Rust/WASM |
| Using Python on Workers for Platforms |
| Integrating WASM modules in any Worker |
workers-performanceworkers-testingcloudflare-worker-base