@virke/wasm-component
v1.1.0
Published
Wasm Component Model support for Virke applications.
Readme
@virke/wasm-component
Wasm Component Model support for Virke applications.
Overview
This package enables building Virke applications using the Wasm Component Model, a portable binary interface standard for WebAssembly. Component Model applications are more portable, composable, and future-proof than traditional Wasm modules.
Features
- Type-safe runtime bindings for Virke services (KV, DB, OS3, logging)
- Standard HTTP interface via
wasi:http/incoming-handler - Componentization tooling powered by
componentize-js - WIT definitions for the Virke world
Installation
bun add @virke/wasm-componentUsage
Basic Handler
import { defineHandler, json } from "@virke/wasm-component";
export default defineHandler(async (request) => {
return json({ message: "Hello from Wasm Component Model!" });
});Using Virke Runtime Services
import { defineHandler, json, kv, db, os3, logging } from "@virke/wasm-component";
export default defineHandler(async (request) => {
// KV Store
const value = kv.get("my-key");
const setResult = kv.set("my-key", "my-value", 3600);
// Database
const queryResult = db.query("SELECT * FROM users WHERE id = ?", ["123"]);
// Object Storage
const getResult = os3.get("files/document.pdf");
// Logging
logging.log("info", "Request processed");
return json({ value, queryResult });
});Componentization
The componentization happens automatically during deployment via the Virke CLI. The CLI detects Component Model projects and uses componentize-js to build them.
Project Setup
Add to your virke.toml:
[project]
name = "my-component-app"
type = "component"
[compute]
entry = "src/handler.ts"WIT Interface
The Virke runtime provides these interfaces:
virke:runtime/kv- Key-value storevirke:runtime/db- SQL databasevirke:runtime/os3- Object storagevirke:runtime/logging- Structured logging
Your component exports:
wasi:http/[email protected]- HTTP request handler
Deployment
virke deployThe CLI will:
- Detect the
type = "component"configuration - Bundle your TypeScript/JavaScript code
- Run
componentize-jsto generate a Wasm component - Upload the component to Fastly Compute@Edge
Architecture
Component Model applications on Virke run as dedicated Fastly Compute services. The Virke runtime provides host implementations of the imported interfaces (KV, DB, OS3, logging) using Fastly's native services.
┌─────────────────────────────────────┐
│ Your Component (Wasm) │
│ │
│ import virke:runtime/kv │
│ import virke:runtime/db │
│ export wasi:http/incoming-handler │
└─────────────────────────────────────┘
↓
┌─────────────────────────────────────┐
│ Virke Runtime (Fastly C@E) │
│ │
│ - KV Store bindings │
│ - SQLite bindings │
│ - Object Storage bindings │
└─────────────────────────────────────┘Why Component Model?
- Portability: Components run on any Component Model runtime
- Composability: Components can import/export interfaces
- Future-proof: Standard evolving with WebAssembly
- Performance: Near-native execution speed
- Security: Capability-based security model
License
MIT
