Skip to content

Commit 7179592

Browse files
chore(adapters): add README (#9059)
This commit adds a README to the `wasi-preview1-component-adapter-provider` crate to make it clearer how to use it and what it's for. While the README is mostly for those who are already familiar, a section giving a brief overview of context around the adapters was added. Signed-off-by: Victor Adossi <vadossi@cosmonic.com>
1 parent bba5e68 commit 7179592

1 file changed

Lines changed: 77 additions & 0 deletions

File tree

  • crates/wasi-preview1-component-adapter/provider
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<div align="center">
2+
<h1><code>wasi-preview1-component-adapter-provider</code></h1>
3+
<strong>A <a href="https://bytecodealliance.org/">Bytecode Alliance</a> project</strong>
4+
<p>
5+
<strong>
6+
A utility library containing binaries for WASI Preview1 adapters for easy use from Rust.
7+
</strong>
8+
</p>
9+
10+
<p>
11+
<a href="https://crates.io/crates/wasi-preview1-component-adapter-provider"><img src="https://img.shields.io/crates/v/wasi-preview1-component-adapter-provider.svg?style=flat-square" alt="Crates.io version" /></a>
12+
<a href="https://crates.io/crates/wasi-preview1-component-adapter-provider"><img src="https://img.shields.io/crates/d/wasi-preview1-component-adapter-provider.svg?style=flat-square" alt="Download" /></a>
13+
<a href="https://docs.rs/wasi-preview1-component-adapter-provider/"><img src="https://img.shields.io/badge/docs-latest-blue.svg?style=flat-square" alt="docs.rs docs" /></a>
14+
</p>
15+
</div>
16+
17+
`wasi-preview1-component-adapter-provider` contains the raw bytes of the WASI Preview1 to Preview2 adapters (Reactor, Command, and Proxy).
18+
19+
For example, if you wanted to write the adapter bytes back into a `.wasm` binary:
20+
21+
```rust
22+
use wasi_preview1_component_adapter_provider::WASI_SNAPSHOT_PREVIEW1_REACTOR_ADAPTER;
23+
24+
fn main() {
25+
std::fs::write(
26+
"wasi_snapshot_preview1.reactor.wasm",
27+
WASI_SNAPSHOT_PREVIEW1_REACTOR_ADAPTER
28+
).expect("failed to write bytes to file");
29+
}
30+
```
31+
32+
A more realistic use-case is performing the *adaptation* step of preparing a WASI Preview2 component from an existing WASI Preview1 component:
33+
34+
```rust
35+
use wasi_preview1_component_adapter_provider::WASI_SNAPSHOT_PREVIEW1_REACTOR_ADAPTER;
36+
use wit_component::ComponentEncoder;
37+
38+
fn main() -> Result<(), Box<dyn std::error::Error>> {
39+
let wasm_p1_bytes = std::fs::read("path/to/your/your-component.p1.wasm");
40+
41+
let wasm_p2_bytes = ComponentEncoder::default()
42+
.module(&wasm_module_bytes)?
43+
.adapter(
44+
"wasi_snapshot_preview1",
45+
WASI_SNAPSHOT_PREVIEW1_REACTOR_ADAPTER,
46+
)?
47+
.validate(true)
48+
.encode()?;
49+
50+
std::fs::write("your-component.p2.wasm", wasm_p2_bytes)?;
51+
52+
Ok(())
53+
}
54+
```
55+
56+
## What is a component adapter?
57+
58+
Code compiled to WebAssembly as described by the [base WebAssembly Spec][wasm-spec] is considered a WebAssembly "module" or "core module".
59+
60+
To robustly support rich types, composition, and easier interoperability, the [Component Model][cm] was created and is the spec that governs the idea of a "WebAssembly component". The component model wraps any existing WebAssembly core module(s) and extends them with additional features.
61+
62+
To standardize underlying system interoperability (ex. reading files, system time) in code compiled to WebAssembly, the [WebAssembly System Interface ("WASI")][wasi] was created. WASI is implemented by language tool chains (ex. Rust supports `wasm32-wasi`/`wasm32-wasip1` as a target, with [support for `wasm32-wasip2` on the way][rust-target-wasm32-wasi]), and enables compiling a WebAssembly component that utilizes [the interfaces that make up WASI Preview1][wasi-p1-interfaces].
63+
64+
In the ongoing work of building WASI, WASI Preview2 which contains more functionality [has been released][wasi-p2-release] -- but building directly to Preview2 is not yet integrated into language toolchains. However, Preview1 components (which *can* be produced by curren toolchains) can be *adapted* to WASI Preview2.
65+
66+
This is where component adapters come in.
67+
68+
**Component Adapters are WebAssembly binaries that contain logic that can take any WebAssembly binary implemented in terms of WASI Preview1 and convert them to WASI Preview2.**
69+
70+
This crate contains the binary content of those the adapter WebAssembly binaries, made accessible as constant arrays of bytes (`const &[u8]`).
71+
72+
[wasm-spec]: https://webassembly.github.io/spec/core
73+
[cm]: https://component-model.bytecodealliance.org
74+
[wasi]: https://wasi.dev/
75+
[wasi-p2-release]: https://bytecodealliance.org/articles/WASI-0.2
76+
[rust-target-wasm32-wasi]: https://blog.rust-lang.org/2024/04/09/updates-to-rusts-wasi-targets.html
77+
[wasi-p1-interfaces]: https://github.com/WebAssembly/WASI/blob/main/legacy/preview1/witx/wasi_snapshot_preview1.witx

0 commit comments

Comments
 (0)