WASI: WebAssembly di Luar Browser
WASI (WebAssembly System Interface) memungkinkan Wasm berjalan di server, CLI, dan embedded systems — bukan hanya browser. WASI menyediakan interface standar ke filesystem, network, clock, dan resource sistem lainnya.
Kenapa WASI?
- Universal binary — Compile sekali, jalan di mana saja (Linux, macOS, Windows, Edge)
- Sandboxed by default — Tidak bisa akses file/network tanpa izin eksplisit
- Near-native speed — Lebih cepat dari container Docker untuk cold start
- Tiny footprint — Wasm module biasanya beberapa MB vs ratusan MB Docker image
Runtime: Wasmtime
# Install wasmtime
curl https://wasmtime.dev/install.sh -sSf | bash
# Rust → WASI
rustup target add wasm32-wasip1
# Compile
cargo build --target wasm32-wasip1 --release
# Jalankan
wasmtime target/wasm32-wasip1/release/myapp.wasm
# Dengan akses filesystem (sandboxed)
wasmtime --dir=./data target/wasm32-wasip1/release/myapp.wasm
Contoh: CLI Tool
// src/main.rs (target: wasm32-wasip1)
use std::env;
use std::fs;
use std::io::{self, BufRead};
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() < 2 {
eprintln!("Usage: wordcount <file>");
std::process::exit(1);
}
let content = fs::read_to_string(&args[1])
.expect("Cannot read file");
let lines = content.lines().count();
let words = content.split_whitespace().count();
let chars = content.chars().count();
println!("{} lines, {} words, {} chars", lines, words, chars);
}
WASI Preview
- Preview 1 — Stabil. File I/O, args, env vars, clock, random
- Preview 2 — Networking (HTTP), streams, component model integration
WASI vs Docker
- Startup time — WASI: ~1ms, Docker: ~500ms-2s
- Image size — WASI: 1-5MB, Docker: 50-500MB
- Security — WASI: capability-based (deny by default), Docker: namespace isolation
- Portability — WASI: true cross-platform binary, Docker: Linux-only (VM di macOS/Windows)
Use Cases Server-Side
- Serverless functions — Cold start super cepat (Fermyon Spin, Wasmtime)
- Plugin system — Jalankan kode user dalam sandbox aman
- Edge computing — Deploy binary kecil ke CDN edge nodes
- Embedded — IoT devices dengan resource terbatas