1 stable release
new 32.0.0 | Apr 21, 2025 |
---|
#52 in #linker
119 downloads per month
Used in wasmtime-cli
3.5MB
59K
SLoC
Wasmtime's wasi-tls (Transport Layer Security) Implementation
This crate provides the Wasmtime host implementation for the wasi-tls API. The wasi-tls world allows WebAssembly modules to perform SSL/TLS operations, such as establishing secure connections to servers. TLS often relies on other wasi networking systems to provide the stream so it will be common to enable the wasi:cli world as well with the networking features enabled.
An example of how to configure wasi-tls is the following:
use wasmtime_wasi::{IoView, WasiCtx, WasiCtxBuilder, WasiView};
use wasmtime::{
component::{Linker, ResourceTable},
Store, Engine, Result, Config
};
use wasmtime_wasi_tls::{LinkOptions, WasiTlsCtx};
struct Ctx {
table: ResourceTable,
wasi_ctx: WasiCtx,
}
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
}
}
#[tokio::main]
async fn main() -> Result<()> {
let ctx = Ctx {
table: ResourceTable::new(),
wasi_ctx: WasiCtxBuilder::new()
.inherit_stderr()
.inherit_network()
.allow_ip_name_lookup(true)
.build(),
};
let mut config = Config::new();
config.async_support(true);
let engine = Engine::new(&config)?;
// Set up wasi-cli
let mut store = Store::new(&engine, ctx);
let mut linker = Linker::new(&engine);
wasmtime_wasi::add_to_linker_async(&mut linker)?;
// Add wasi-tls types and turn on the feature in linker
let mut opts = LinkOptions::default();
opts.tls(true);
wasmtime_wasi_tls::add_to_linker(&mut linker, &mut opts, |h: &mut Ctx| {
WasiTlsCtx::new(&mut h.table)
})?;
// ... use `linker` to instantiate within `store` ...
Ok(())
}
Dependencies
~36–50MB
~1M SLoC