#!/usr/bin/env bash # Build wai-quantum-serve as a PURE `wasi:http/proxy` component. # # Why this exists: a plain `--target wasm32-wasip2` build imports # `wasi:cli/environment` (Rust's std pulls it in at startup), which the bare # proxy world does not provide — so it only runs with `wasmtime serve -S cli=y`. # Building a wasip1 core module and adapting it with the Bytecode Alliance's # proxy adapter produces a component whose imports all sit inside the proxy # world, so it runs on ANY wasi:http host with no host flags. set -euo pipefail # Keep this in step with the wasmtime you serve with: the adapter is tied to a # WASI version, and a mismatch shows up as an unresolved-import error. WASMTIME_VERSION="${WASMTIME_VERSION:-v48.0.1}" CACHE="${WASI_ADAPTER_CACHE:-$HOME/.cache/wasi-adapters}" ADAPTER="$CACHE/wasi_snapshot_preview1.proxy.$WASMTIME_VERSION.wasm" OUT="${1:-target/wai_quantum_serve.proxy.wasm}" command -v wasm-tools >/dev/null || { echo "need wasm-tools (cargo install wasm-tools)" >&2; exit 1; } rustup target list --installed | grep -qx wasm32-wasip1 || rustup target add wasm32-wasip1 if [ ! -f "$ADAPTER" ]; then mkdir -p "$CACHE" url="https://github.com/bytecodealliance/wasmtime/releases/download/$WASMTIME_VERSION/wasi_snapshot_preview1.proxy.wasm" echo "fetching proxy adapter $WASMTIME_VERSION" node -e ' const fs=require("fs"); (async()=>{const r=await fetch(process.argv[1],{redirect:"follow"}); if(!r.ok){console.error("download failed",r.status);process.exit(1);} fs.writeFileSync(process.argv[2],Buffer.from(await r.arrayBuffer()));})(); ' "$url" "$ADAPTER" fi cargo build --release --target wasm32-wasip1 wasm-tools component new target/wasm32-wasip1/release/wai_quantum_serve.wasm \ --adapt "wasi_snapshot_preview1=$ADAPTER" -o "$OUT" # Fail loudly if anything outside the proxy world crept back in. if wasm-tools component wit "$OUT" | grep -q "wasi:cli/environment"; then echo "ERROR: component still imports wasi:cli/environment — not a proxy-world component" >&2 exit 1 fi echo "built $OUT" echo "run: wasmtime serve --addr 127.0.0.1:8080 $OUT # no -S flags needed"