#key-value #key-value-store #wasmtime #wasi #run-time #secure

wasmtime-wasi-keyvalue

Wasmtime implementation of the wasi-keyvalue API

15 stable releases (6 major)

new 30.0.0 Feb 20, 2025
29.0.1 Jan 21, 2025
28.0.1 Jan 14, 2025
28.0.0 Dec 20, 2024
24.0.2 Nov 5, 2024

#755 in WebAssembly

Download history 81/week @ 2024-10-29 583/week @ 2024-11-05 127/week @ 2024-11-12 300/week @ 2024-11-19 55/week @ 2024-11-26 217/week @ 2024-12-03 129/week @ 2024-12-10 237/week @ 2024-12-17 97/week @ 2024-12-24 35/week @ 2024-12-31 211/week @ 2025-01-07 273/week @ 2025-01-14 360/week @ 2025-01-21 70/week @ 2025-01-28 176/week @ 2025-02-04 91/week @ 2025-02-11

766 downloads per month
Used in 5 crates (3 directly)

Apache-2.0 WITH LLVM-exception

3MB
46K SLoC

Wasmtime's wasi-keyvalue Implementation

This crate provides a Wasmtime host implementation of the wasi-keyvalue API. With this crate, the runtime can run components that call APIs in wasi-keyvalue and provide components with access to key-value storages.

Currently supported storage backends:

  • In-Memory (empty identifier)

Examples

The usage of this crate is very similar to other WASI API implementations such as wasi:cli and wasi:http.

A common scenario is accessing KV store in a wasi:cli component. A standalone example of doing all this looks like:

use wasmtime::{
    component::{Linker, ResourceTable},
    Config, Engine, Result, Store,
};
use wasmtime_wasi::{IoView, WasiCtx, WasiCtxBuilder, WasiView};
use wasmtime_wasi_keyvalue::{WasiKeyValue, WasiKeyValueCtx, WasiKeyValueCtxBuilder};

#[tokio::main]
async fn main() -> Result<()> {
    let mut config = Config::new();
    config.async_support(true);
    let engine = Engine::new(&config)?;

    let mut store = Store::new(&engine, Ctx {
        table: ResourceTable::new(),
        wasi_ctx: WasiCtxBuilder::new().build(),
        wasi_keyvalue_ctx: WasiKeyValueCtxBuilder::new().build(),
    });

    let mut linker = Linker::<Ctx>::new(&engine);
    wasmtime_wasi::add_to_linker_async(&mut linker)?;
    // add `wasi-keyvalue` world's interfaces to the linker
    wasmtime_wasi_keyvalue::add_to_linker(&mut linker, |h: &mut Ctx| {
        WasiKeyValue::new(&h.wasi_keyvalue_ctx, &mut h.table)
    })?;

    // ... use `linker` to instantiate within `store` ...

    Ok(())
}

struct Ctx {
    table: ResourceTable,
    wasi_ctx: WasiCtx,
    wasi_keyvalue_ctx: WasiKeyValueCtx,
}

impl IoView for Ctx {
    fn table(&mut self) -> &mut ResourceTable { &mut self.table }
}
impl WasiView for Ctx {
    fn ctx(&mut self) -> &mut WasiCtx { &mut self.wasi_ctx }
}

Dependencies

~21–32MB
~625K SLoC